Advertisement
razvanth21

Untitled

Oct 29th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     if(argc != 2)
  12.     {
  13.         printf("Usage: %s <dir> <fis>\n",argv[0]);
  14.         exit(EXIT_FAILURE);
  15.     }
  16.    
  17.     DIR *directory;
  18.    
  19.     if((directory=opendir(argv[1])) < 0)
  20.     {
  21.         printf("Eroare la deschiderea directorului sau parametrul dat nu e un director\n");
  22.         exit(EXIT_FAILURE);
  23.     }
  24.    
  25.     int f;
  26.    
  27.     if((f=open(argv[2],O_WRONLY)) < 0)
  28.     {
  29.         printf("Eroare la deschiderea fisierului pentru scriere sau parametrul dat nu e un fisier\n");
  30.         exit(EXIT_FAILURE);
  31.     }
  32.    
  33.     struct dirent *dir_info;
  34.     struct stat file;
  35.     char buff[512];
  36.    
  37.     while((dir_info=readdir(directory)) != NULL)
  38.     {
  39.         int rez;
  40.         if ((rez = stat(dir_info->d_name, &file)) > 0)
  41.         {
  42.             if (S_ISREG(file.st_mode) && file.st_blocks > 2)
  43.             {
  44.                 snprintf(buff, sizeof(buff), "File %s has %d blocks\n", dir_info->d_name, (int)file.st_blocks);
  45.                
  46.                 printf("%s", buff);
  47.                 write(f, buff, strlen(buff));
  48.             }
  49.         }
  50.     }
  51.    
  52.     closedir(directory);
  53.     close(f);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement