Advertisement
alexarcan

lab5_OS_working version

Oct 28th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/dir.h>
  3. #include<stdio.h>
  4. #include<sys/stat.h>
  5. #include<fcntl.h>
  6. #include<unistd.h>
  7. #include<stdlib.h>
  8. #include<ctype.h>
  9. #include<string.h>
  10. #define MAX_CHARS 200
  11.  
  12. int CharsRead,Tmp;
  13. struct direct *DirEntryPtr;
  14. DIR *DirPtr;
  15. struct stat Stat;
  16. char Path[MAX_CHARS];
  17.  
  18. void printInFile(char *buff, char *file)
  19. {
  20. int fis = open(file,O_CREAT | O_APPEND | O_WRONLY);
  21. if(fis == -1)
  22. {
  23. printf("Error on file open!\n");
  24. exit(1);
  25. }
  26. write(fis, buff, strlen(buff));
  27.  
  28. close(fis);
  29. }
  30.  
  31. int function(const char* dirName)
  32. {
  33. int size = 0;
  34. DirPtr = opendir(dirName);
  35. char buff[100];
  36. char buff2[100];
  37.  
  38. while ((DirEntryPtr = readdir(DirPtr)) != NULL)
  39. {
  40. if ((strcmp(DirEntryPtr->d_name,".") != 0) || (strcmp(DirEntryPtr->d_name,"..") != 0))
  41. {
  42. printf("%s \n",DirEntryPtr->d_name);
  43.  
  44. Path[0] = 0;
  45. strcat(Path,dirName);
  46. strcat(Path,"/");
  47. strcat(Path,DirEntryPtr->d_name);
  48. Tmp = stat(Path,&Stat);
  49.  
  50. int n;
  51. int m;
  52.  
  53. if (S_ISDIR(Stat.st_mode))
  54. {
  55. n = sprintf(buff, "Dimensiunea directorului : %lld bytes\n",(long long) Stat.st_size);
  56. printInFile(buff, "output.txt");
  57. }
  58.  
  59. if(S_ISREG(Stat.st_mode))
  60. {
  61. m = sprintf(buff2, "Dimensiunea fisierului : %lld bytes\n",(long long) Stat.st_size);
  62. printInFile(buff2, "output.txt");
  63. }
  64. }
  65. }
  66.  
  67. return size;
  68.  
  69. }
  70.  
  71. int main(int argc, char *argv[])
  72. {
  73. function(argv[1]);
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement