1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <math.h>
#define MAXCARS 256
int FicExt(char *ref,char *ext){
int i,j;
i=0;
char extlue[strlen(ext)];
strcpy(extlue,"");
while(i<strlen(ref)){//tant que le curseur n'est pas a la fin du nom
for(j=0;j<strlen(ext);j++){
if(ref[i]=='\0' || ref[i]=='\n') printf("\0 ou \n detecte");
strncat(extlue,&ref[i],1);//on copie ds extlue taille de extension caractères après le '.' ex: 1 car extension = 'c'
i++;//on bouge le curseur
}
if(strcmp(extlue,ext)==0)
return 0;
else{
i++;
while(ref[i]!='.') i++;
strcpy(extlue,"");
}
}
return -1;
}
void parcoursRec(char *nomrep, char *extension, int *tube){
DIR *rep;
struct dirent *ent;
struct stat buf;
char ref[MAXCARS];
int nbE;
close(tube[0]);
rep=opendir(nomrep);
if(rep != NULL){
while(ent = readdir(rep)){
if(strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")){
strcpy(ref,nomrep);
strcat(ref,"/");
strcat(ref,ent->d_name);
if(lstat(ref,&buf) == 0) {
if(S_ISDIR(buf.st_mode))
parcoursRec(ref,extension,tube);
else{
printf("%s\n",ref);
if(FicExt(ref,extension)==0){
nbE=write(tube[1],ref,sizeof(ref));
if(nbE<0) perror("erreur ecriture");
}
}
}
}
} // fin while
closedir(rep);
close(tube[1]);
} // fin if(rep..)
}
int main (int argc,char **argv){
int tube[2];
if(pipe(tube) == -1){
perror("pipe");
exit(1);
}
if(argc<4){
printf("erreur arguments : crypte nom_repertoire nom_extension(ex:'.cc') nbprocessus(entier)\n");
exit(1);
}
else if(strlen(argv[2])<2){
printf("erreur extension : exemple, '.c'\n");
exit(1);
}
else if(atoi(argv[3])<1){
printf("erreur nbprocessus : il faut au moins 1 processus\n");
exit(1);
}
parcoursRec(argv[1],argv[2],tube);
} |
Partager