Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/param.h>
- #include <sys/wait.h>
- #include <unistd.h>
- /************************************************
- * TP 1 *
- * Fait par Hugo Mercure *
- * MERH13038507 - INF3172 gr: 20 *
- ************************************************/
- /**
- * doExec va créer un processus fils pour executer la commande sur le fichier trouvé
- */
- void doExec(char * szFullName, char** comm){
- switch(fork()){
- case 0:
- execvp (comm[0], comm);
- printf("Erreur commande");
- _exit(1);
- break;
- default:
- wait(NULL);
- // printf("parent\n");
- break;
- }
- }
- /**
- * listdir va regarder tout les elements d'un dossier afin de trouver le fichier voulu
- * si un element est également un dossier, listdir cherche dans ce dossier de manière récursive
- * en se lanceant avec le nouveau chemin.
- * @path est le dossier à ouvrir
- * @sName est le nom de fichier à trouver
- * @comm est le tableau de commandes l'élément -2 du tableau est mis à jour ici et représente
- * le chemin du fichier trouvé
- * @commande détermine si on doit executer une commande ou simplement lister le fichier et son
- * chemin
- * @index est la position dans le tableau comm qui correspond au chemin du fichier
- */
- int listdir(char *path, char *sName, char** comm, int commande, int index){
- struct stat stDirInfo;
- struct dirent * entry;
- DIR *dp;
- char szFullName[MAXPATHLEN];
- char szDirectory[MAXPATHLEN];
- struct stat stFileInfo;
- strncpy(szDirectory, path, MAXPATHLEN -1);
- lstat(szDirectory, &stDirInfo);
- dp = opendir(path);
- if(dp == NULL){
- perror("opendir");
- return 1;
- }
- while((entry = readdir(dp))){
- char * testName = entry->d_name;
- if((strcmp(testName, ".") != 0) && (strcmp(testName, "..") != 0)){
- sprintf(szFullName, "%s/%s", szDirectory, entry->d_name);
- lstat(szFullName, &stFileInfo);
- if(S_ISDIR(stFileInfo.st_mode)){
- //Si l'élement lu est un dossier, on l'ouvre
- listdir(szFullName, sName, comm, commande, index);
- }else{
- //Sinon on compare le nom pour voir si c'est
- //celui qu'on cherche.
- if(strcmp(entry->d_name, sName) == 0){
- if(commande == 0){
- printf("- %s\n", szFullName);
- }
- else{
- comm[index] = szFullName; /*On donne le chemin*/
- doExec(szFullName, comm);
- }
- }
- }
- }
- }
- closedir(dp);
- return 0;
- }
- /**
- * La fonction main recoit les arguments de la ligne de commande. Il faut au minimum 3
- * arguments. Soit le nom du programme, le dossier dans lequel faire des recherches ("."
- * pour le dossier courant) et le nom du fichier à rechercher. Dans le cas ou un 4eme
- * argument est spécifié on assume qu'il s'agit d'une commande suivie d'arguments.
- */
- int main(int argc, char **argv) {
- if(argc < 3){
- printf("Usage - %s repertoire fichier\n", argv[0]);
- printf("Ou - %s repertoire fichier <commande>\n", argv[0]);
- }
- else if (argc == 3){
- char* comm[1];
- listdir(argv[1], argv[2], comm, 0, 0);
- }
- else if(argc > 3){
- char* comm[argc - 1];
- int i;
- for(i = 0 ; i < argc-3 ; i++){
- comm[i] = argv[i+3];
- }
- int fileP = argc - 3;
- comm[fileP] = ".";
- comm[argc -2] = (char*)0;
- listdir(argv[1], argv[2], comm, 1, fileP);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement