Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <limits.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <time.h>
- #include "hash.h"
- #include <libgen.h>
- #include <sys/wait.h>
- #define _BSD_SOURCE
- char* dir_name;
- char* namepattern;
- char** executionargs;
- struct Hashtable* table;
- int inputsize;
- extern int errno;
- int stringend(char* str)
- {
- int i=0;
- while(str[i]!='\0'){
- i++;
- }
- return i;
- }
- int patterncheck (char* ourstring, char* pattern, int stringbegin, int stringfinal, int patternbegin, int patternfinal);
- int islikepattern(char* str, char * pat){
- return patterncheck(str, pat, 0, stringend(str), 0, stringend(pat) );
- }
- int squarebracketscheck(char* ourstring, char* pattern, int* stringbegin, int* stringfinal, int* patternbegin, int* patternfinal){
- int thelettersuits=0;
- int i=*patternbegin;
- i++;
- int k=i;
- while ( (pattern[k]!='[') && (k!=*patternfinal) ){
- ++k;
- }
- --k;
- while ( (pattern[k]!=']') && (k!=i-1) ){
- --k;
- }
- if(k<i){
- printf("Wrong squarebrackets usage 3\n");
- return 0;
- }
- int rightbracketposition=k;
- char symbol=ourstring[*stringbegin];
- while( i!=rightbracketposition ){
- char firstsymbol, secondsymbol;
- firstsymbol=pattern[i];
- if (pattern[i+1]=='-' ){
- if((i+2==*patternfinal)||(pattern[i+2]==']') || (i+2==rightbracketposition) ){
- printf("Wrong squarebrackets usage 3\n");
- return 0;
- }
- secondsymbol=pattern[i+2];
- i+=2;
- }
- else {
- secondsymbol=ourstring[i];
- }
- if ( ((int)firstsymbol-(int)symbol)*((int)secondsymbol-(int)symbol) <=0 ){
- thelettersuits=1;
- }
- i++;
- }
- *stringbegin=*stringbegin+1;
- *patternbegin=i+1;
- return thelettersuits;
- }
- int patterncheck (char* ourstring, char* pattern, int stringbegin, int stringfinal, int patternbegin, int patternfinal){
- int stringiter=stringbegin;
- int patterniter=patternbegin;
- if ( patterniter != patternfinal)
- {
- char symbol=pattern[patterniter];
- if( (symbol=='*') || (symbol=='?') ){
- int i=patternbegin; int s=stringbegin;
- while( (pattern[i]=='*') || (pattern[i]=='?') ){
- if(pattern[i]=='?'){
- s++;
- if(s-1==stringfinal){return 0;}
- }
- i++;
- }
- int j;
- for(j=s; j!=stringfinal; ++j)
- {
- if( patterncheck(ourstring, pattern, j, stringfinal, i, patternfinal)==1){
- return 1;
- }
- }
- return patterncheck(ourstring, pattern, j, stringfinal, i, patternfinal);
- }
- if(symbol=='['){
- if( squarebracketscheck(ourstring, pattern, &stringbegin, &stringfinal, &patternbegin, &patternfinal) ==1){
- return patterncheck(ourstring, pattern, stringbegin, stringfinal, patternbegin, patternfinal);
- }
- else{
- return 0;
- }
- }
- else{
- if(ourstring[stringbegin]==symbol){
- return patterncheck(ourstring,pattern, stringbegin+1, stringfinal, patternbegin+1, patternfinal);
- }
- else{
- return 0;
- }
- }
- printf("Wrong pattern\n");
- return 0;
- }
- printf("\n");
- return (stringiter==stringfinal);
- }
- int expressioncheck(char* filename)
- {
- return islikepattern(filename, namepattern);
- }
- int executiontry(char* ourpath){
- char** temporaryargs=(char**)malloc(inputsize*sizeof(char*) );
- if(temporaryargs==NULL){
- fprintf(stderr, "Malloc error \n");
- return 0;
- }
- int i;
- int x=0;
- for(i=0; (i<inputsize) && x==0; ++i)
- {
- int t=0;
- if(executionargs[i]!=NULL)
- {
- t=(int)strlen(executionargs[i]);
- if(t<strlen(ourpath)+1 ){t=(int)strlen(ourpath)+1;}
- temporaryargs[i]=(char*)malloc( (t+1)*sizeof(char) );
- if(strcmp(executionargs[i],"{}")!=0 ){
- strcpy( temporaryargs[i], executionargs[i] );
- } else{
- strcpy( temporaryargs[i], ourpath );
- }
- }
- else{x=1;
- temporaryargs[i]=NULL;
- }
- }
- for(; i<inputsize; ++i){
- temporaryargs[i]=NULL;
- }
- int wait_status;
- pid_t child_pid;
- child_pid = fork ();
- if (child_pid == -1){
- fprintf(stderr, "fork error \n");
- int j;
- for(j=0; j<inputsize; ++j)
- {
- free(temporaryargs[j]);
- }
- return 0;
- }
- if (child_pid == 0){
- errno=0;
- execvp (temporaryargs[0], temporaryargs);
- if( (errno!=0) && (errno!=ENOENT) ){
- if(errno==EACCES){
- fprintf(stderr, "Access error, %s \n", ourpath);
- }
- else{
- fprintf(stderr, "Error no %d in filename, %s \n", errno, ourpath);
- }
- }
- _exit(1);
- }
- // while (waitpid (child_pid, &wait_status, 0) == (pid_t) -1){
- // if (errno != EINTR){
- // int j;
- // for(j=0; j<inputsize; ++j)
- // {
- // free(temporaryargs[j]);
- // }
- // return 0;
- // }
- // }
- // if(WIFSIGNALED(wait_status) ){
- // int j;
- // for(j=0; j<inputsize; ++j)
- // {
- // free(temporaryargs[j]);
- // }
- // return 1;
- // }
- wait(&wait_status);
- if(WIFEXITED(wait_status) ) {
- int j;
- for(j=0; j<inputsize; ++j)
- {
- free(temporaryargs[j]);
- }
- if(WEXITSTATUS(wait_status) ==0)
- {
- return 1;
- }
- else{
- return 0;
- }
- }
- int j;
- for(j=0; j<inputsize; ++j)
- {
- free(temporaryargs[j]);
- }
- return 0;
- }
- void* findwithexecution(char* directory);
- void* filehandling(char* ourpath, struct stat entryInfo){
- if(S_ISDIR(entryInfo.st_mode)) {
- findwithexecution(ourpath);
- } else if(S_ISREG(entryInfo.st_mode)) {
- char* filename=(char*)malloc( (PATH_MAX + 1)*sizeof(char) );
- if(filename==NULL){
- fprintf(stderr, "Malloc error \n");
- return NULL;
- }
- strcpy(filename, basename(ourpath) );
- if(expressioncheck( filename ) ){
- int t=executiontry(ourpath);
- if(t){
- printf("Successful execution, %s, user %d, size, %lld \n", ourpath, entryInfo.st_uid, entryInfo.st_size);
- }
- }
- free(filename);
- }
- else if( S_ISLNK( entryInfo.st_mode ) ) {
- char* realname=(char*)malloc( (PATH_MAX + 1)*sizeof(char) );
- if(realname==NULL){
- fprintf(stderr, "Malloc error \n");
- return NULL;
- }
- char* fullname=(char*)malloc( (PATH_MAX + 1)*sizeof(char) );
- if(fullname==NULL){
- fprintf(stderr, "Malloc error \n");
- return NULL;
- }
- ssize_t u;
- u=readlink(ourpath, realname, PATH_MAX ) ;
- if( u== -1 ) {
- fprintf(stderr, "Readlink error 1\n");
- return NULL;
- }
- else{
- realname[u]='\0';
- }
- if( realpath(realname, fullname) ==NULL){
- fprintf(stderr, "Readlink error 2\n");
- return NULL;
- }
- free(realname);
- struct stat state;
- stat(fullname, &state );
- filehandling(fullname, state );
- free(fullname);
- return NULL;
- }
- return NULL;
- }
- void* findwithexecution(char* directory)
- {
- if(Search(table, directory) ){
- return NULL;
- }
- DIR *ourdir = NULL;
- struct dirent ThisElem;
- struct dirent *ThisElemPtr = NULL;
- int er;
- char* ourpath=(char*)malloc( (PATH_MAX + 2)*sizeof(char) );
- ourdir = opendir(directory);
- if( ourdir == NULL ) {
- return NULL;
- }
- Insert(&table, directory);
- if( (er = readdir_r( ourdir, &ThisElem, &ThisElemPtr ) ) != 0){
- fprintf(stderr, " Error %d \n", er);
- return NULL;
- }
- while( ThisElemPtr != NULL ) {
- if( (!strcmp( ThisElem.d_name, ".") ) || (!strcmp( ThisElem.d_name, "..") ) ) {
- if ( (er = readdir_r( ourdir, &ThisElem, &ThisElemPtr ) ) !=0 ){
- fprintf(stderr, " Error %d \n", er);
- }
- continue;
- }
- strcpy(ourpath, directory);
- strcat(ourpath, "/");
- strcat(ourpath, ThisElem.d_name);
- struct stat entryInfo;
- if( lstat(ourpath, &entryInfo )== 0 ) {
- filehandling(ourpath, entryInfo);
- } else {
- fprintf(stderr, "Stat retrieving error \t %s \n", ourpath);
- continue;
- }
- if( (er = readdir_r( ourdir, &ThisElem, &ThisElemPtr ) ) != 0){
- fprintf(stderr, " %d \n", er);
- return NULL;
- }
- }
- closedir( ourdir);
- free(ourpath);
- return NULL;
- }
- int main( int argc, char * argv[]){
- table=Createtable(1);
- inputsize=argc+1;
- namepattern=NULL;
- if(argc<2){
- printf("Wrong usage \n");
- exit(0);
- }
- executionargs=(char**)malloc( inputsize*sizeof(char*) );
- namepattern=(char*)malloc( inputsize*sizeof(char) );
- if(executionargs==NULL){
- fprintf(stderr, "Malloc error \n");
- exit(0);
- }
- int i2;
- for(i2=0; i2<inputsize-1; ++i2){
- int t=(int)strlen(argv[i2]);
- if(t<20){t=20;}
- executionargs[i2]=(char*)malloc(t*sizeof(char));
- }
- executionargs[i2]=(char*)malloc(255*sizeof(char));
- int i=1;
- dir_name=(char*)malloc( (PATH_MAX+1)*sizeof(char) );
- strcpy(dir_name, argv[1] );
- i++;
- int nameexistence=0;
- int execexistence=0;
- while(i<argc){
- if( strcmp(argv[i], "-name")==0 ) {
- i++;
- nameexistence=1;
- if(i==argc){
- printf("Wrong usage 1\n");
- exit(0);
- }
- strcpy(namepattern, argv[i]);
- i++;
- continue;
- }
- if( strcmp(argv[i], "-exec")==0 ){
- i++;
- int j=0;
- execexistence=1;
- while( ( strcmp(argv[i], ";") != 0) && i<argc){
- strcpy(executionargs[j],argv[i]);
- i++;
- j++;
- }
- if(j==0){
- printf("Wrong usage 2\n");
- exit(0);
- }
- if( strcmp(argv[i],";")!= 0){
- printf("Wrong usage 3\n");
- exit(0);
- }
- i++;
- for(; j<inputsize; ++j)
- {
- executionargs[j]=NULL;
- }
- continue;
- }
- printf("Wrong usage 4\n");
- exit(1);
- }
- if( nameexistence==0){
- strcpy(namepattern,"*");
- }
- if (execexistence==0){
- strcpy(executionargs[0],"printf");
- strcpy(executionargs[1],"{}");
- int j=2;
- for(; j<inputsize; ++j)
- {
- executionargs[j]=NULL;
- }
- }
- findwithexecution(dir_name);
- Memoryclean(table);
- free(table);
- for(i=0; i<inputsize; ++i){
- free(executionargs[i] );
- }
- free(dir_name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement