FILES- 2 Γ.Μανάρας
Αρχεία και κλήσεις συστήματος Χειρισμός λαθών Ιδιότητες αρχείων Κατάλογοι Αλλαγή δικαιωμάτων πρόσβασης
Το  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);
mode :  τρόπος ανοίγματος O_RDONLY O_WRONLY O_RDWR  O_APPEND O_CREAT O_TRUNC
main() { int fd   = open("myfile"   , O_WRONLY | O_CREAT | O_TRUNC   ,   0644); // ίδιο με :  fopen(“myfile”, “w”); if (fd == -1)  { perror(" fd == -1  !!!  "); exit(1); } }
int dup(int fd);  Αυτό το  system call  δημιουργεί ένα αντίγραφο του  fd ,  έτσι ώστε από το σημείο που καλείται και μετά μπορούμε για το ίδιο αρχείο να χρησιμοποιούμε είτε τον ένα  file descriptor  είτε τον άλλο int dup2(int fd1, int fd2) Το  system call  dup2  ελευθερώνει τον  fd2 - αν είναι δεσμευμένος - και τον αντιστοιχεί στο ίδιο αρχείο με  file descriptor fd1
Αν κατά την κλήση ενός  system call  συμβεί ένα λάθος, ενημερώνεται η  global  μεταβλητή  errno  με τον κωδικό λάθους. Στη συνέχεια η κλήση της  perror( )  εμφανίζει την περιγραφή του λάθους, το δε αλφαριθμητικό όρισμα που της περνάμε, εμφανίζεται πριν την  περιγραφή του λάθους που δίνει το σύστημα main() { int fd = open("notexistfile", 0); if (fd == -1) { printf("errno = %d\n", errno); perror("open"); } }
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 */ // ………………….. }
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(&quot;%d\n&quot;, getsize(argv[i])); } }
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 */
main(int argc, char *argv[]) { struct stat attrib; if (argc != 2){   fprintf(stderr,&quot;Usage: %s file\n&quot;, argv[0]);   exit(1);  } if (stat(argv[1], &attrib) == -1){   perror(&quot;stat&quot;);   exit(2);  } if (S_ISBLK(attrib.st_mode)) printf(&quot;%s is a block special file\n&quot;, argv[1]); else if (S_ISCHR(attrib.st_mode)) printf(&quot;%s is a character special file\n&quot;, argv[1]); else if (S_ISREG(attrib.st_mode)) printf(&quot;%s is a regular file\n&quot;, argv[1]); else if (S_ISDIR(attrib.st_mode)) printf(&quot;%s is a directory\n&quot;, argv[1]); else if (S_ISFIFO(attrib.st_mode)) printf(&quot;%s is a FIFO or PIPE\n&quot;, argv[1]); }
Το διάβασμα καταλόγων γίνεται με τις παρακάτω κλήσεις συστήματος- 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 */ }
main(int argc,char *argv[]) { struct dirent *dptr; DIR *dirp; char directory[256]; if (argc==1) { strcpy(directory,&quot;.&quot;); }   else if (argc>2) { fprintf(stderr,&quot;Usage: %s directory\n&quot;, argv[0]); exit(1); } else { strcpy(directory, argv[1]); }
if((dirp=opendir(directory))==NULL) { fprintf(stderr, &quot;Error opening %s\n&quot;, directory); exit(2); } while(dptr=readdir(dirp)) { printf(&quot;%d %s\n&quot;, dptr->d_ino, dptr->d_name); } closedir(dirp); }
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(&quot;testfile&quot;, S_IRWXU | S_IRGRP | S_IXOTH); }

More Related Content

PPT
Linux εντολή find
PPT
Linux σωληνώσεις sort, tee
PPT
ΛΣ1-001
PPT
Σωληνώσεις cat sort tee
PPT
Eντολές More less head tail
PDF
μάθημα13 18/1
DOC
Διαγώνισμα Πληροφορικής Α Γυμνασίου
PPT
ΛΣ2THREADSv2
Linux εντολή find
Linux σωληνώσεις sort, tee
ΛΣ1-001
Σωληνώσεις cat sort tee
Eντολές More less head tail
μάθημα13 18/1
Διαγώνισμα Πληροφορικής Α Γυμνασίου
ΛΣ2THREADSv2

Viewers also liked (9)

DOC
~$Samuel
ODP
Otsoak
PPTX
taylor swift cynthia gaxiola
XLSX
Excel project ah
PPTX
Endings2 y7
PPTX
Electrónico contra analógico
PPT
Mikro ipuinak
PDF
Dossier Cheap In Madrid 2012
PPTX
admon por competencia
~$Samuel
Otsoak
taylor swift cynthia gaxiola
Excel project ah
Endings2 y7
Electrónico contra analógico
Mikro ipuinak
Dossier Cheap In Madrid 2012
admon por competencia
Ad

Similar to ΛΣ1FILES2v0 (9)

PPT
ΛΣ2_IPC
PPT
ΛΣ1FILES1v0
PPT
ΛΣ2IPCv0
DOCX
13 Προγράμματα C.
PDF
OS_Processes_iteration12333333333333333333333333
PPTX
Processes
DOCX
Πρόγραμμα υπολογισμών σε C - 3
PDF
Function pointers in C
PDF
Η ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 16 - ΣΥΝΑΡΤΗΣΕΙΣ ΕΞΟΔΟΥ (ΕΚΤΥΠΩΣΗ)
ΛΣ2_IPC
ΛΣ1FILES1v0
ΛΣ2IPCv0
13 Προγράμματα C.
OS_Processes_iteration12333333333333333333333333
Processes
Πρόγραμμα υπολογισμών σε C - 3
Function pointers in C
Η ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 16 - ΣΥΝΑΡΤΗΣΕΙΣ ΕΞΟΔΟΥ (ΕΚΤΥΠΩΣΗ)
Ad

More from Yanis Manaras (20)

PPT
Webclass admin 4
PPT
What iswebclass 4
PPT
Webclass teacher 4
PPT
Webclass student 4
PPT
Webclass Student
PPTX
Ads 1 fibonacci
PPTX
Ads 1 alt_e
PPTX
Ads 1 common
PPTX
HTML and Javascript
PPTX
Signals
PPT
ΛΣ1-002
PPTX
PPTX
PPT
PPT
PPT
PPT
PPT
PPT
PPT
ΛΣ2SEMAPHORESv0
Webclass admin 4
What iswebclass 4
Webclass teacher 4
Webclass student 4
Webclass Student
Ads 1 fibonacci
Ads 1 alt_e
Ads 1 common
HTML and Javascript
Signals
ΛΣ1-002
ΛΣ2SEMAPHORESv0

ΛΣ1FILES2v0

  • 2. Αρχεία και κλήσεις συστήματος Χειρισμός λαθών Ιδιότητες αρχείων Κατάλογοι Αλλαγή δικαιωμάτων πρόσβασης
  • 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);
  • 4. mode : τρόπος ανοίγματος O_RDONLY O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC
  • 5. main() { int fd = open(&quot;myfile&quot; , O_WRONLY | O_CREAT | O_TRUNC , 0644); // ίδιο με : fopen(“myfile”, “w”); if (fd == -1) { perror(&quot; fd == -1 !!! &quot;); 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(&quot;notexistfile&quot;, 0); if (fd == -1) { printf(&quot;errno = %d\n&quot;, errno); perror(&quot;open&quot;); } }
  • 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(&quot;%d\n&quot;, 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,&quot;Usage: %s file\n&quot;, argv[0]); exit(1); } if (stat(argv[1], &attrib) == -1){ perror(&quot;stat&quot;); exit(2); } if (S_ISBLK(attrib.st_mode)) printf(&quot;%s is a block special file\n&quot;, argv[1]); else if (S_ISCHR(attrib.st_mode)) printf(&quot;%s is a character special file\n&quot;, argv[1]); else if (S_ISREG(attrib.st_mode)) printf(&quot;%s is a regular file\n&quot;, argv[1]); else if (S_ISDIR(attrib.st_mode)) printf(&quot;%s is a directory\n&quot;, argv[1]); else if (S_ISFIFO(attrib.st_mode)) printf(&quot;%s is a FIFO or PIPE\n&quot;, 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 */ }
  • 13. main(int argc,char *argv[]) { struct dirent *dptr; DIR *dirp; char directory[256]; if (argc==1) { strcpy(directory,&quot;.&quot;); } else if (argc>2) { fprintf(stderr,&quot;Usage: %s directory\n&quot;, argv[0]); exit(1); } else { strcpy(directory, argv[1]); }
  • 14. if((dirp=opendir(directory))==NULL) { fprintf(stderr, &quot;Error opening %s\n&quot;, directory); exit(2); } while(dptr=readdir(dirp)) { printf(&quot;%d %s\n&quot;, dptr->d_ino, dptr->d_name); } closedir(dirp); }
  • 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(&quot;testfile&quot;, S_IRWXU | S_IRGRP | S_IXOTH); }