Advertisement
alexarcan

lab5_OS_afisare directoare + marime

Oct 27th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1.  
  2. /* sample program to illustrate the opendir(), readdir() and stat()
  3. system calls
  4.  
  5. it lists all files in the given directory, and adds an asterisk
  6. to each one which is a subdirectory */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/dir.h>
  10. #include<stdio.h>
  11. #include<sys/stat.h>
  12. #include<fcntl.h>
  13. #include<unistd.h>
  14. #include<stdlib.h>
  15. #include<ctype.h>
  16. #include<string.h>
  17.  
  18. #define MAX_CHARS 200
  19.  
  20. int CharsRead,Tmp; /* number of characters read from a directory entry */
  21. struct direct *DirEntryPtr; /* pointer to a directory entry */
  22. DIR *DirPtr; /* pointer to the directory */
  23. struct stat Stat;
  24. char Path[MAX_CHARS];
  25.  
  26.  
  27.  
  28. int main(int argc, char *argv[])
  29.  
  30.  
  31. { DirPtr = opendir(argv[1]);
  32. while (1) {
  33. DirEntryPtr = readdir(DirPtr);
  34. if (DirEntryPtr == 0) break; /* reached end of directory entries */
  35. /* process file (other than . and ..) */
  36. if (strcmp(DirEntryPtr->d_name,".") != 0 &&
  37. strcmp(DirEntryPtr->d_name,"..") != 0) {
  38. /* print file name */
  39. printf("%s",DirEntryPtr->d_name);
  40. /* build full path name of the file, for stat() */
  41. Path[0] = 0;
  42. strcat(Path,argv[1]);
  43. strcat(Path,"/");
  44. strcat(Path,DirEntryPtr->d_name);
  45. Tmp = stat(Path,&Stat);
  46. /* Stat now contains lots of info about the file,
  47. e.g. its size, though we are not interested in
  48. most of that info here */
  49. if (S_ISDIR(Stat.st_mode)) printf("*");
  50. printf("\n");
  51.  
  52. printf("File size: %lld bytes\n",
  53. (long long) Stat.st_size);
  54.  
  55. }
  56. }
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement