Created
January 2, 2011 23:37
-
-
Save alesya-h/762932 to your computer and use it in GitHub Desktop.
Это решение твоего шестого варианта. Предыдущий файл был для пятого.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <dirent.h> | |
#include <semaphore.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
sem_t * out_sem; | |
int getsize(char *name) | |
{ | |
struct stat buf; | |
stat(name,&buf); | |
return buf.st_size; | |
} | |
int isexecutable(char *name) | |
{ | |
return !access(name,X_OK); | |
} | |
int inbounds(int min,int max,char *name) | |
{ | |
return ((getsize(name) > min) && (getsize(name) < max)); | |
} | |
void processdir(int min,int max,char *name,FILE *outfile) | |
{ | |
pid_t pid; | |
DIR *dir; | |
DIR *dir_desc; | |
struct dirent *cur_entry; | |
char path[1024]; | |
char *filename; | |
int total; | |
pid = getpid(); | |
total = 0; | |
dir=opendir(name); | |
chdir(name); | |
strcpy(path, getwd(NULL)); | |
while(cur_entry = readdir(dir)){ | |
if(strcmp(cur_entry->d_name, "..") && strcmp(cur_entry->d_name,".")){ | |
if(dir_desc = opendir(cur_entry->d_name)){ // this is a directory | |
closedir(dir_desc); | |
processdir(min,max,cur_entry->d_name,outfile); | |
chdir(path); | |
}else if(isexecutable(cur_entry->d_name) // this is an executable file | |
&& inbounds(min,max,cur_entry->d_name)){ // with desired size | |
sem_wait(out_sem); | |
printf("PID: %d | PATH: %s | FILE: %s | SIZE: %d | TOTAL: %d\n", | |
pid, path, cur_entry->d_name, getsize(cur_entry->d_name), total); | |
fprintf(outfile,"PATH: %s | FILE: %s | SIZE: %d\n", | |
path, cur_entry->d_name, getsize(cur_entry->d_name)); | |
sem_post(out_sem); | |
total++; | |
} | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
struct dirent * cur_entry; | |
int min,max; | |
DIR *dir_desc; | |
DIR *dir; | |
FILE *outfile; | |
char *path; | |
int total; | |
int procnum; | |
int N; | |
pid_t p; | |
// check | |
if(argc != 5){ | |
printf("usage: ./exe <min> <max> <dir> <outfile>"); | |
exit(1); | |
} | |
// initialization | |
min = atoi(argv[1]); | |
max = atoi(argv[2]); | |
out_sem = sem_open("max_sem", O_CREAT); | |
sem_post(out_sem); | |
procnum = 0; | |
dir = opendir(argv[3]); | |
outfile = fopen(argv[4],"w"); | |
if(!dir){ | |
printf("\"%s\" is not a directory\n",argv[1]); | |
exit(2); | |
} | |
if(!outfile){ | |
printf("Can't open file \"%s\" for writing. Exiting.\n",argv[1]); | |
exit(3); | |
} | |
chdir(argv[3]); | |
path=getwd(NULL); | |
printf("N = "); | |
scanf("%d",&N); | |
N--; // 4 process maximum means 3 child + 1 parent. Now N will be max child. | |
// main program loop | |
while(cur_entry = readdir(dir)){ | |
if(strcmp(cur_entry->d_name,"..") && strcmp(cur_entry->d_name,".")){ | |
if(dir_desc = opendir(cur_entry->d_name)){ // this is a directory | |
sem_wait(out_sem); | |
// printf("DIR: %s\n", cur_entry->d_name); | |
sem_post(out_sem); | |
closedir(dir_desc); | |
if(procnum<N){ | |
procnum++; | |
}else{ | |
wait3(0,0,0); // wait any child process to exit | |
} | |
p = fork(); | |
if(!p){ | |
processdir(min,max,cur_entry->d_name,outfile); | |
fclose(outfile); | |
exit(0); | |
}else{ | |
sem_wait(out_sem); | |
// printf("started %d for %s\n",p,cur_entry->d_name); | |
sem_post(out_sem); | |
} | |
}else if(isexecutable(cur_entry->d_name) // this is an executable file | |
&& inbounds(min,max,cur_entry->d_name)){ // with desired size | |
sem_wait(out_sem); | |
fprintf(outfile,"PATH: %s | FILE: %s | SIZE: %d\n", | |
path, cur_entry->d_name, getsize(cur_entry->d_name)); | |
sem_post(out_sem); | |
total++; | |
} | |
} | |
} | |
while(procnum--){ | |
wait3(0,0,0); | |
} | |
printf("Total files without subdirectories: %d\n",total); | |
fclose(outfile); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment