int fileOperations(FILE *outputFile,int* fd,char *directoryName,char *findString){ DIR *openFile; FILE *inputFile=NULL; struct dirent *input; char directory[MAX]; char fullDirectory[MAX]; struct stat statFile; pid_t pid; char strPipe[MAX]; char readpipe[MAX]; char arr[MAX]; int stat = 0; int size = 0; int fifo2; int in; openFile = opendir(directoryName); if(openFile == NULL){ printf("Dosya acilamadi\n"); closedir(openFile); exit (1); } strncpy(directory,directoryName, MAX - 1); //signal(SIGINT, &ctrlcSignal); /*Directory okudum*/ while((input = readdir(openFile)) != NULL){ if(strcmp(input->d_name,".")!=0 && strcmp(input->d_name,"..")!=0){ /*Directory ve altindaki dosyayi bir arraye attim*/ sprintf(fullDirectory, "%s/%s",directory, input -> d_name ); if (lstat(fullDirectory,&statFile) < 0){ perror(fullDirectory); } /*Dosya mi directory mi oldugunu kontrol ettim dosya ise*/ if (S_ISDIR(statFile.st_mode) == 0){ /*Dosya sayimi artirdim*/ totalFile = totalFile + 1; if((pid = fork())== -1){ perror("Not fork\n"); exit (1); }if(pid == 0){ size = findSize(inputFile,fullDirectory); totalW += myGrep(inputFile,outputFile,strPipe,fd,size,fullDirectory,findString); exit (totalW); }else{ // while (r_wait(NULL) > 0); /*Asagidaki fonksiyonlari http://www.kaanaslan.com/resource/article/display_article.php?id=87 * sitesinden referans alarak kullandim */ wait(&stat); totalW = WEXITSTATUS(stat); } /*Childlari topladim*/ totalChild = totalChild + 1; }else{/*Dosya degil ise recursive olarak fonksiyonumu tekrar cagirdim ki dosyayi bulsun*/ /*Directory sayimi buldum*/ totalDirectory = totalDirectory + 1; if((pid = fork())== -1){ perror("Not fork\n"); exit (1); } if(pid == 0){ fileOperations(outputFile,fd,fullDirectory,findString); if((in= read(fd[0], readpipe,MAX)> 0)){ if(in<=0){ perror("read error \n"); exit(1); } //close(fd[0]); if((fifo2 = open(myfifo, O_RDWR ))==-1){ perror("Not fifo"); exit(1); } if(write(fifo2,readpipe,sizeof(char)*MAX)<0) { perror("Failed to write"); exit(1); } close(fifo2); if((fifo2 = open(myfifo,O_RDONLY))==-1){ perror("Not fifo"); exit(1); } in = read( fifo2, arr,sizeof(char)*MAX); if(in<=0) { perror("not read"); exit(1); } fprintf(outputFile,"%s\n",arr); exit(1); } }else{ //while (r_wait(NULL) > 0); //close(fd[1]); //unlink("myfifo"); } } } } //close(fifo2); closedir(openFile); return 0; }
int main(int argc, char* argv[]) { //create a new command structure to imitate argc/argv CmdData* cmd = CmdData_Create(); int i = 1; cmd->argc = argc - 1; //ignore program name cmd->strcmd = (char*) malloc(MAX_ARG); //allocate a string for using system memset(cmd->strcmd, 0, MAX_ARG); for( ; i < argc; ++i) //copy argv { cmd->args[i - 1] = (char*) malloc(strlen(argv[i])); strcpy(cmd->args[i - 1], argv[i]); strcat(cmd->strcmd, argv[i]); if(i + 1 < argc) strcat(cmd->strcmd, " "); } //Example Could Have Used My system Implementation //Rather than popen /** * Test this after the rest is compiled and tested * * Be sure to modify your working system by removing main * including the sys.h file and removing the headers in the .c file * if(strstr(cmd->strcmd, "ping") != 0) { system(cmd->strcmd); } */ do //process accepted commands until user enters q { if(cmd->argc >= 3 && strcmp(cmd->args[0], "grep") == 0) { myGrep(cmd->args, cmd->argc); } else if(cmd->argc >= 1 && strcmp(cmd->args[0],"ps") == 0) { myPS(cmd->args,cmd->argc); } else if(cmd->argc >= 1 && strcmp(cmd->args[0], "ls") == 0) { myLS(cmd->args, cmd->argc); } else if(cmd->argc >= 3 && strcmp(cmd->args[0], "diff") == 0) { myDiff(cmd->args, cmd->argc); } else { fprintf(stderr, "Accepted Commands are grep, ps, ls, diff\n"); } printf("Please enter a command or press q to exit\n"); CmdData_Init(&cmd); } while( get_args(cmd) && strcmp(CmdData_at(cmd, 0),"q")); if(cmd) CmdData_Free(&cmd); return 0; }