3. Το Unix παρέχει 6 βασικές κλήσεις συστήματος για λειτουργίες I/O int open(char *name, int mode [ , int permissions ] ); int creat(char *name, int permissions); int close(int fd); int read(int fd, char *buf, int size); int write(int fd, char *buf, int size); off_t lseek(int fd, off_t offset, int whence);
5. main() { int fd = open("myfile" , O_WRONLY | O_CREAT | O_TRUNC , 0644); // ίδιο με : fopen(“myfile”, “w”); if (fd == -1) { perror(" fd == -1 !!! "); exit(1); } }
6. int dup(int fd); Αυτό το system call δημιουργεί ένα αντίγραφο του fd , έτσι ώστε από το σημείο που καλείται και μετά μπορούμε για το ίδιο αρχείο να χρησιμοποιούμε είτε τον ένα file descriptor είτε τον άλλο int dup2(int fd1, int fd2) Το system call dup2 ελευθερώνει τον fd2 - αν είναι δεσμευμένος - και τον αντιστοιχεί στο ίδιο αρχείο με file descriptor fd1
7. Αν κατά την κλήση ενός system call συμβεί ένα λάθος, ενημερώνεται η global μεταβλητή errno με τον κωδικό λάθους. Στη συνέχεια η κλήση της perror( ) εμφανίζει την περιγραφή του λάθους, το δε αλφαριθμητικό όρισμα που της περνάμε, εμφανίζεται πριν την περιγραφή του λάθους που δίνει το σύστημα main() { int fd = open("notexistfile", 0); if (fd == -1) { printf("errno = %d\n", errno); perror("open"); } }
8. int stat(const char *path, struct stat *buf); Πληροφορίες για ένα αρχείο. Αν το αρχείο είναι σύνδεσμος οι πληροφορίες θα αφορούν το αρχείο στο οποίο δείχνει ο σύνδεσμος int fstat(int fd, struct stat *buf); Το ίδιο με την stat( ) int lstat(const char *path, struct stat *buf); Παρόμοια με την stat( ), με τη διαφορά ότι δεν ακολουθεί τον σύνδεσμο Σε περίπτωση επιτυχίας επιστρέφουν 0, σε λάθος -1 με ταυτόχρονη ενημέρωση της errno struct stat { mode_t st_mode; /* File mode (type, perms) */ ino_t st_ino; /* Inode number */ nlink_t st_nlink; /* Number of links */ uid_t st_uid; /* User ID of the file's owner */ gid_t st_gid; /* Group ID of the file's group */ off_t st_size; /* File size in bytes */ time_t st_atime; /* Time of last access */ // ………………….. }
9. int getsize(char *filename) { struct stat attrib; stat(filename, &attrib); return attrib.st_size; } main(int argc, char *argv[]) { int i; for(i=1; i<argc; i++) { printf("%d\n", getsize(argv[i])); } }
10. Macros που δίνουν τον τύπο αρχείου στο stat.h #define S_ISBLK(m) /* is block device */ #define S_ISCHR(m) /* is character device */ #define S_ISDIR(m) /* is directory */ #define S_ISFIFO(m) /* is fifo pipe */ #define S_ISREG(m) /* is regular file */
11. main(int argc, char *argv[]) { struct stat attrib; if (argc != 2){ fprintf(stderr,"Usage: %s file\n", argv[0]); exit(1); } if (stat(argv[1], &attrib) == -1){ perror("stat"); exit(2); } if (S_ISBLK(attrib.st_mode)) printf("%s is a block special file\n", argv[1]); else if (S_ISCHR(attrib.st_mode)) printf("%s is a character special file\n", argv[1]); else if (S_ISREG(attrib.st_mode)) printf("%s is a regular file\n", argv[1]); else if (S_ISDIR(attrib.st_mode)) printf("%s is a directory\n", argv[1]); else if (S_ISFIFO(attrib.st_mode)) printf("%s is a FIFO or PIPE\n", argv[1]); }
12. Το διάβασμα καταλόγων γίνεται με τις παρακάτω κλήσεις συστήματος- sys/dir.h DIR * opendir(const char *path); // Δουλεύει όπως το FILE* fopen() , // το DIR αυτό καθ’ εαυτό δεν χρησιμοποιείται, αλλά το περνάμε στις // συναρτήσεις που κάνουν τη δουλειά struct dirent readdir(DIR *dirp); int closedir(DIR *dirp); struct dirent{ ino_t d_ino; /* i-number */ off_t d_off; /* offset into directory file */ ushort d_reclen; /* length of record */ char d_name[256]; /* file name */ }
15. int chmod(const char *path, mode_t mode); // χρησιμοποιεί το path int fchmod(int fd, mode_t mode); // χρησιμοποιεί τον file descriptor S_IRWXU 00700 Read, write, execute by owner S_IRUSR 00400 Read by owner S_IWUSR 00200 Write by owner S_IXUSR 00100 Execute (search if a directory) by owner S_IRWXG 00070 Read, write, execute by group S_IRGRP 00040 Read by group S_IWGRP 00020 Write by group S_IXGRP 00010 Execute by group S_IRWXO 00007 Read, write, execute (search) by others S_IROTH 00004 Read by others S_IWOTH 00002 Write by others S_IXOTH 00001 Execute by others main(){ chmod("testfile", S_IRWXU | S_IRGRP | S_IXOTH); }