Advertisement
Guest User

tipaiC

a guest
May 2nd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/mman.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/time.h>
  9. #include <string.h>
  10. #include <ftw.h>
  11.  
  12. int fileInfo(const char *p, const struct stat *st, int fl, struct FTW *fbuf);
  13. int openFile(const char *p);
  14. int closeFile(int fd);
  15.  
  16. int openFile(const char *p)
  17. {
  18.         int dskr;
  19.         dskr = open( p, O_RDONLY, 0);
  20.         if( dskr == -1 ){
  21.         perror( p );
  22.         exit(1);
  23.         }
  24.         return dskr;
  25. }
  26.  
  27. int closeFile(int fd)
  28. {
  29.         int rv;
  30.         rv = close( fd );
  31.         if( rv != 0 ) perror ( "close() failed" );
  32.  
  33.         return rv;
  34. }
  35.  
  36. int checkIfScript(int dskr)
  37. {
  38.         char * buff;
  39.         char toC[15];
  40.         memcpy(toC, "PIR", 3);
  41.  
  42.         buff = mmap(NULL, 256, PROT_READ, MAP_PRIVATE, dskr, 0);
  43.         int ret;
  44.         ret = memcmp(buff, toC, 3);
  45.         if(ret == 0) return 1;
  46.  
  47.         return 0;
  48. }
  49. int fileInfo(const char *p, const struct stat *st, int fl, struct FTW *fbuf)
  50. {
  51.         if(fl == FTW_D)
  52.         {
  53.                 printf(p);
  54.                 printf(" yra katalogas\n");
  55.         }
  56.         if(fl == FTW_F)
  57.         {
  58.                 printf(p);
  59.                 printf(" yra failas\n");
  60.                 int d = -1;
  61.                 d = openFile(p);
  62.                 int s = -1;
  63.                 s = checkIfScript(d);
  64.                 if(s == 1)
  65.                 {
  66.                         printf("Tai pat ir skriptas\n");
  67.                         printf((st->st_mode & S_IWGRP) ? "Faila gali modifikuoti kiti\n" : " ");
  68.                         printf((st->st_mode & S_IWOTH) ? "Faila gali modifikuoti kiti\n" : " ");
  69.                 }
  70.                 closeFile(d);
  71.         }
  72.         if(fl == FTW_SL)
  73.         {
  74.                 printf(p);
  75.                 printf(" yra simboline nuoroda\n");
  76.         }
  77.  
  78.         return 0;
  79. }
  80.  
  81. int main(int argc, char * argv[])
  82. {
  83.         if( argc != 2 )
  84.         {
  85.                 printf("Netinkamas argumentu skaicius\n");
  86.                 exit(255);
  87.         }
  88.  
  89.         int rv = -1;
  90.         rv = nftw(argv[1], fileInfo, 20, 0);
  91.         if(rv == -1)
  92.         {
  93.                 perror("nftw failed");
  94.                 exit(255);
  95.         }
  96.         if(rv != 0)
  97.         {
  98.                 printf("finished");
  99.         }
  100.         return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement