Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include <string.h>
- #include <ftw.h>
- int fileInfo(const char *p, const struct stat *st, int fl, struct FTW *fbuf);
- int openFile(const char *p);
- int closeFile(int fd);
- int openFile(const char *p)
- {
- int dskr;
- dskr = open( p, O_RDONLY, 0);
- if( dskr == -1 ){
- perror( p );
- exit(1);
- }
- return dskr;
- }
- int closeFile(int fd)
- {
- int rv;
- rv = close( fd );
- if( rv != 0 ) perror ( "close() failed" );
- return rv;
- }
- int checkIfScript(int dskr)
- {
- char * buff;
- char toC[15];
- memcpy(toC, "PIR", 3);
- buff = mmap(NULL, 256, PROT_READ, MAP_PRIVATE, dskr, 0);
- int ret;
- ret = memcmp(buff, toC, 3);
- if(ret == 0) return 1;
- return 0;
- }
- int fileInfo(const char *p, const struct stat *st, int fl, struct FTW *fbuf)
- {
- if(fl == FTW_D)
- {
- printf(p);
- printf(" yra katalogas\n");
- }
- if(fl == FTW_F)
- {
- printf(p);
- printf(" yra failas\n");
- int d = -1;
- d = openFile(p);
- int s = -1;
- s = checkIfScript(d);
- if(s == 1)
- {
- printf("Tai pat ir skriptas\n");
- printf((st->st_mode & S_IWGRP) ? "Faila gali modifikuoti kiti\n" : " ");
- printf((st->st_mode & S_IWOTH) ? "Faila gali modifikuoti kiti\n" : " ");
- }
- closeFile(d);
- }
- if(fl == FTW_SL)
- {
- printf(p);
- printf(" yra simboline nuoroda\n");
- }
- return 0;
- }
- int main(int argc, char * argv[])
- {
- if( argc != 2 )
- {
- printf("Netinkamas argumentu skaicius\n");
- exit(255);
- }
- int rv = -1;
- rv = nftw(argv[1], fileInfo, 20, 0);
- if(rv == -1)
- {
- perror("nftw failed");
- exit(255);
- }
- if(rv != 0)
- {
- printf("finished");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement