Advertisement
lokotochek

Untitled

Apr 1st, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #include <fcntl.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <dirent.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. #include "hash.h"
  13. #include <libgen.h>
  14. #include <sys/wait.h>
  15.  
  16. #define _BSD_SOURCE
  17.  
  18.  
  19.  
  20. char* dir_name;
  21. char* namepattern;
  22. char** executionargs;
  23. struct Hashtable* table;
  24. int inputsize;
  25. extern int errno;
  26.  
  27.  
  28. int stringend(char* str)
  29. {
  30.     int i=0;
  31.     while(str[i]!='\0'){
  32.         i++;
  33.     }
  34.     return i;
  35. }
  36.  
  37. int patterncheck (char* ourstring, char* pattern, int stringbegin, int stringfinal, int patternbegin, int patternfinal);
  38.  
  39. int islikepattern(char* str, char * pat){
  40.     return patterncheck(str, pat, 0, stringend(str), 0, stringend(pat) );
  41. }
  42.  
  43.  
  44. int squarebracketscheck(char* ourstring, char* pattern, int* stringbegin, int* stringfinal, int* patternbegin, int* patternfinal){
  45.     int thelettersuits=0;
  46.     int i=*patternbegin;
  47.     i++;
  48.     int k=i;
  49.     while ( (pattern[k]!='[') && (k!=*patternfinal) ){
  50.         ++k;
  51.     }
  52.     --k;
  53.     while ( (pattern[k]!=']') && (k!=i-1) ){
  54.         --k;
  55.     }
  56.     if(k<i){
  57.         printf("Wrong squarebrackets usage 3\n");
  58.         return 0;
  59.     }
  60.     int rightbracketposition=k;
  61.     char symbol=ourstring[*stringbegin];
  62.     while( i!=rightbracketposition ){
  63.         char firstsymbol, secondsymbol;
  64.         firstsymbol=pattern[i];
  65.         if (pattern[i+1]=='-'  ){
  66.             if((i+2==*patternfinal)||(pattern[i+2]==']') || (i+2==rightbracketposition) ){
  67.                 printf("Wrong squarebrackets usage 3\n");
  68.                 return 0;
  69.             }
  70.             secondsymbol=pattern[i+2];
  71.             i+=2;
  72.         }
  73.         else {
  74.             secondsymbol=ourstring[i];
  75.         }
  76.         if ( ((int)firstsymbol-(int)symbol)*((int)secondsymbol-(int)symbol) <=0   ){
  77.             thelettersuits=1;
  78.         }
  79.         i++;
  80.     }
  81.     *stringbegin=*stringbegin+1;
  82.     *patternbegin=i+1;
  83.     return thelettersuits;
  84. }
  85.  
  86.  
  87.  
  88. int patterncheck (char* ourstring, char* pattern, int stringbegin, int stringfinal, int patternbegin, int patternfinal){
  89.     int stringiter=stringbegin;
  90.     int patterniter=patternbegin;
  91.     if ( patterniter != patternfinal)
  92.     {
  93.         char symbol=pattern[patterniter];
  94.         if( (symbol=='*') || (symbol=='?') ){
  95.             int i=patternbegin; int s=stringbegin;
  96.             while( (pattern[i]=='*') || (pattern[i]=='?') ){
  97.                 if(pattern[i]=='?'){
  98.                     s++;
  99.                     if(s-1==stringfinal){return 0;}
  100.                 }
  101.                 i++;
  102.             }
  103.             int j;
  104.             for(j=s; j!=stringfinal; ++j)
  105.             {
  106.                 if( patterncheck(ourstring, pattern, j, stringfinal, i, patternfinal)==1){
  107.                     return 1;
  108.                 }
  109.             }
  110.             return patterncheck(ourstring, pattern, j, stringfinal, i, patternfinal);
  111.         }
  112.         if(symbol=='['){
  113.             if( squarebracketscheck(ourstring, pattern, &stringbegin, &stringfinal, &patternbegin, &patternfinal)  ==1){
  114.                 return patterncheck(ourstring, pattern, stringbegin, stringfinal, patternbegin, patternfinal);
  115.             }
  116.             else{
  117.                 return 0;
  118.             }
  119.         }
  120.         else{
  121.             if(ourstring[stringbegin]==symbol){
  122.                 return patterncheck(ourstring,pattern, stringbegin+1, stringfinal, patternbegin+1, patternfinal);
  123.             }
  124.             else{
  125.                 return 0;
  126.             }
  127.         }
  128.         printf("Wrong pattern\n");
  129.         return 0;
  130.     }
  131.     printf("\n");
  132.     return (stringiter==stringfinal);
  133. }
  134.  
  135.  
  136.  
  137. int expressioncheck(char* filename)
  138. {
  139.     return islikepattern(filename, namepattern);
  140. }
  141.  
  142.  
  143. int executiontry(char* ourpath){
  144.     char** temporaryargs=(char**)malloc(inputsize*sizeof(char*) );
  145.     if(temporaryargs==NULL){
  146.         fprintf(stderr, "Malloc error \n");
  147.         return 0;
  148.     }
  149.     int i;
  150.     int x=0;
  151.     for(i=0; (i<inputsize) && x==0; ++i)
  152.     {
  153.         int t=0;
  154.         if(executionargs[i]!=NULL)
  155.         {
  156.             t=(int)strlen(executionargs[i]);
  157.             if(t<strlen(ourpath)+1 ){t=(int)strlen(ourpath)+1;}
  158.             temporaryargs[i]=(char*)malloc( (t+1)*sizeof(char) );
  159.             if(strcmp(executionargs[i],"{}")!=0  ){
  160.                 strcpy( temporaryargs[i], executionargs[i] );
  161.             } else{
  162.                 strcpy( temporaryargs[i], ourpath );
  163.             }
  164.         }
  165.         else{x=1;
  166.             temporaryargs[i]=NULL;
  167.         }
  168.     }
  169.     for(; i<inputsize; ++i){
  170.         temporaryargs[i]=NULL;
  171.     }
  172.     int wait_status;
  173.     pid_t child_pid;
  174.     child_pid = fork ();
  175.     if (child_pid == -1){
  176.         fprintf(stderr, "fork error \n");
  177.         int j;
  178.         for(j=0; j<inputsize; ++j)
  179.         {
  180.             free(temporaryargs[j]);
  181.         }
  182.         return 0;
  183.     }
  184.     if (child_pid == 0){
  185.         errno=0;
  186.         execvp (temporaryargs[0], temporaryargs);
  187.        
  188.         if( (errno!=0) && (errno!=ENOENT) ){
  189.             if(errno==EACCES){
  190.                 fprintf(stderr, "Access error, %s \n", ourpath);
  191.             }
  192.             else{
  193.                 fprintf(stderr, "Error no %d in filename, %s \n", errno, ourpath);
  194.             }
  195.         }
  196.         _exit(1);
  197.     }
  198.  
  199. //    while (waitpid (child_pid, &wait_status, 0) == (pid_t) -1){
  200. //        if (errno != EINTR){
  201. //            int j;
  202. //            for(j=0; j<inputsize; ++j)
  203. //            {
  204. //                free(temporaryargs[j]);
  205. //            }
  206. //            return 0;
  207. //        }
  208. //    }
  209. //    if(WIFSIGNALED(wait_status) ){
  210. //        int j;
  211. //        for(j=0; j<inputsize; ++j)
  212. //        {
  213. //            free(temporaryargs[j]);
  214. //        }
  215. //        return 1;
  216. //    }
  217.    wait(&wait_status);
  218.     if(WIFEXITED(wait_status) ) {
  219.         int j;
  220.         for(j=0; j<inputsize; ++j)
  221.         {
  222.             free(temporaryargs[j]);
  223.         }
  224.  
  225.         if(WEXITSTATUS(wait_status) ==0)
  226.         {
  227.             return 1;
  228.         }
  229.         else{
  230.            
  231.              return 0;
  232.         }
  233.  
  234.     }
  235.     int j;
  236.     for(j=0; j<inputsize; ++j)
  237.     {
  238.         free(temporaryargs[j]);
  239.     }
  240.     return 0;
  241. }
  242.  
  243.  
  244.  
  245. void* findwithexecution(char* directory);
  246.  
  247.  
  248. void* filehandling(char* ourpath, struct stat entryInfo){
  249.     if(S_ISDIR(entryInfo.st_mode)) {
  250.         findwithexecution(ourpath);
  251.     } else if(S_ISREG(entryInfo.st_mode)) {
  252.         char* filename=(char*)malloc( (PATH_MAX + 1)*sizeof(char) );
  253.         if(filename==NULL){
  254.             fprintf(stderr, "Malloc error \n");
  255.             return NULL;
  256.         }
  257.         strcpy(filename, basename(ourpath) );
  258.         if(expressioncheck( filename ) ){
  259.             int t=executiontry(ourpath);
  260.             if(t){
  261.                 printf("Successful execution, %s, user %d, size, %lld \n", ourpath, entryInfo.st_uid, entryInfo.st_size);
  262.             }
  263.         }
  264.         free(filename);
  265.     }
  266.     else if( S_ISLNK( entryInfo.st_mode ) ) {
  267.         char* realname=(char*)malloc( (PATH_MAX + 1)*sizeof(char) );
  268.         if(realname==NULL){
  269.             fprintf(stderr, "Malloc error \n");
  270.             return NULL;
  271.         }
  272.         char* fullname=(char*)malloc( (PATH_MAX + 1)*sizeof(char) );
  273.         if(fullname==NULL){
  274.             fprintf(stderr, "Malloc error \n");
  275.             return NULL;
  276.         }
  277.         ssize_t u;
  278.         u=readlink(ourpath, realname, PATH_MAX ) ;
  279.         if( u== -1 ) {
  280.             fprintf(stderr, "Readlink error 1\n");
  281.             return NULL;
  282.         }
  283.         else{
  284.             realname[u]='\0';
  285.         }
  286.         if( realpath(realname, fullname) ==NULL){
  287.             fprintf(stderr, "Readlink error 2\n");
  288.             return NULL;
  289.         }
  290.         free(realname);
  291.         struct stat state;
  292.         stat(fullname, &state );
  293.         filehandling(fullname, state   );
  294.         free(fullname);
  295.         return NULL;
  296.     }
  297.     return NULL;
  298. }
  299.  
  300.  
  301. void* findwithexecution(char* directory)
  302. {
  303.     if(Search(table, directory) ){
  304.         return NULL;
  305.     }
  306.     DIR *ourdir = NULL;
  307.     struct dirent ThisElem;
  308.     struct dirent *ThisElemPtr = NULL;
  309.     int er;
  310.     char* ourpath=(char*)malloc( (PATH_MAX + 2)*sizeof(char) );
  311.     ourdir = opendir(directory);
  312.     if( ourdir == NULL ) {
  313.         return NULL;
  314.     }
  315.     Insert(&table, directory);
  316.     if( (er = readdir_r( ourdir, &ThisElem, &ThisElemPtr ) ) != 0){
  317.         fprintf(stderr, " Error %d \n", er);
  318.         return NULL;
  319.     }
  320.     while( ThisElemPtr != NULL ) {
  321.         if( (!strcmp( ThisElem.d_name, ".") ) || (!strcmp( ThisElem.d_name, "..") ) ) {
  322.             if ( (er = readdir_r( ourdir, &ThisElem, &ThisElemPtr )  ) !=0 ){
  323.                 fprintf(stderr, " Error %d \n", er);
  324.             }
  325.             continue;
  326.         }
  327.         strcpy(ourpath, directory);
  328.         strcat(ourpath, "/");
  329.         strcat(ourpath, ThisElem.d_name);
  330.         struct stat entryInfo;
  331.         if( lstat(ourpath, &entryInfo )== 0 ) {
  332.             filehandling(ourpath, entryInfo);
  333.         } else {
  334.             fprintf(stderr, "Stat retrieving error \t %s \n", ourpath);
  335.             continue;
  336.         }
  337.         if( (er = readdir_r( ourdir, &ThisElem, &ThisElemPtr ) ) != 0){
  338.             fprintf(stderr, " %d \n", er);
  339.             return NULL;
  340.         }
  341.     }
  342.  
  343.     closedir( ourdir);
  344.     free(ourpath);
  345.     return NULL;
  346. }
  347.  
  348. int main( int argc, char * argv[]){
  349.     table=Createtable(1);
  350.     inputsize=argc+1;
  351.     namepattern=NULL;
  352.     if(argc<2){
  353.         printf("Wrong usage \n");
  354.         exit(0);
  355.     }
  356.     executionargs=(char**)malloc( inputsize*sizeof(char*) );
  357.     namepattern=(char*)malloc( inputsize*sizeof(char) );
  358.     if(executionargs==NULL){
  359.         fprintf(stderr, "Malloc error \n");
  360.         exit(0);
  361.     }
  362.     int i2;
  363.     for(i2=0; i2<inputsize-1; ++i2){
  364.         int t=(int)strlen(argv[i2]);
  365.         if(t<20){t=20;}
  366.         executionargs[i2]=(char*)malloc(t*sizeof(char));
  367.     }
  368.     executionargs[i2]=(char*)malloc(255*sizeof(char));
  369.     int i=1;
  370.     dir_name=(char*)malloc( (PATH_MAX+1)*sizeof(char) );
  371.     strcpy(dir_name, argv[1] );
  372.     i++;
  373.     int nameexistence=0;
  374.     int execexistence=0;
  375.     while(i<argc){
  376.         if( strcmp(argv[i], "-name")==0 ) {
  377.             i++;
  378.             nameexistence=1;
  379.             if(i==argc){
  380.                 printf("Wrong usage 1\n");
  381.                 exit(0);
  382.             }
  383.             strcpy(namepattern, argv[i]);
  384.             i++;
  385.             continue;
  386.         }
  387.         if( strcmp(argv[i], "-exec")==0 ){
  388.             i++;
  389.             int j=0;
  390.             execexistence=1;
  391.             while( ( strcmp(argv[i], ";") != 0) && i<argc){
  392.                 strcpy(executionargs[j],argv[i]);
  393.                 i++;
  394.                 j++;
  395.             }
  396.             if(j==0){
  397.                 printf("Wrong usage 2\n");
  398.                 exit(0);
  399.             }
  400.             if( strcmp(argv[i],";")!= 0){
  401.                 printf("Wrong usage 3\n");
  402.                 exit(0);
  403.             }
  404.             i++;
  405.             for(; j<inputsize; ++j)
  406.             {
  407.                 executionargs[j]=NULL;
  408.             }
  409.             continue;
  410.         }
  411.         printf("Wrong usage 4\n");
  412.         exit(1);
  413.     }
  414.     if( nameexistence==0){
  415.         strcpy(namepattern,"*");
  416.     }
  417.     if (execexistence==0){
  418.         strcpy(executionargs[0],"printf");
  419.         strcpy(executionargs[1],"{}");
  420.         int j=2;
  421.         for(; j<inputsize; ++j)
  422.         {
  423.             executionargs[j]=NULL;
  424.         }
  425.     }
  426.    
  427.     findwithexecution(dir_name);
  428.     Memoryclean(table);
  429.     free(table);
  430.     for(i=0; i<inputsize; ++i){
  431.         free(executionargs[i] );
  432.     }
  433.     free(dir_name);
  434.     return 0;
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement