int main(int argc,char*argv[]) { int i,sts,pid,np; np=0;//numero de procesos creados// if (argc >= 1){ for (i = 1; i < argc ; i++){ printf("%d\n", i); pid=fork(); switch(pid){ case -1: err(1, "fork failed"); break; case 0: // esto es el hijo search_in_dir(argv[i], getpid()); break; default: while(wait(&sts) != pid){ if (sts == 1){ printf("%d\n",sts ); np++;//Sumo porque ha ido bien todo// } } } } if (np == (argc)) err(1,"All processes were successful "); if (np!=(argc)) err(1,"%d processes failed dsvfx",(argc-np)); } exit(0); }
void search_in_dir(char* dir, int pid){ DIR *d = opendir(dir); struct dirent *de; int counter=0; if(d == NULL) { err(1,"%s" ,dir); } char *buf =malloc(sizeof (PATH_MAX)); while((de = readdir(d)) != NULL) { buf= built_path(de->d_name,dir); if (is_file(buf)){ if(get_format(de->d_name)==1){ if (unlink(buf)<0){ printf("%d : error removing %s\n", pid ,de->d_name); }else { counter++; printf("%d : %s ok \n",pid ,de->d_name ); } } }else{ if(is_dir(buf)){ if((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..")!=0)){ search_in_dir(buf,pid); } }else{ printf("\t%s It in not a valid file or directory\n", buf); } if ((counter==0) && (strcmp(de->d_name, ".") == 0)) printf("%d : no files to remove in :%s\n", pid ,dir); } } free(buf); closedir(d); }
void search_in_current_file_dir(const gchar* search_text, SearchTools* self) { GtkTextBuffer* buf; GString* url; gint page_num; gchar* dirname; page_num = gtk_notebook_get_current_page(puss_get_doc_panel(self->app)); if( page_num < 0 ) return; buf = self->app->doc_get_buffer_from_page_num(page_num); if( !buf ) return; url = self->app->doc_get_url(buf); if( !url ) return; dirname = g_path_get_dirname(url->str); if( dirname ) { search_in_dir(dirname, search_text, self); g_free(dirname); } }
void search_in_dir(const gchar* dirname, const gchar* search_text, SearchTools* self) { GDir* dir = g_dir_open(dirname, 0, 0); if( !dir ) return; for(;;) { gchar* filename; const gchar* fname = g_dir_read_name(dir); if( !fname ) break; if( fname[0]=='.' ) continue; filename = g_build_filename(dirname, fname, NULL); if( g_file_test(filename, G_FILE_TEST_IS_DIR) ) { search_in_dir(filename, search_text, self); } else { search_in_file(filename, search_text, self); } g_free(filename); } g_dir_close(dir); }