Advertisement
ensiferum888

find.c

Aug 6th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <sys/param.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10.  
  11. /************************************************
  12. *           TP 1                    *
  13. *       Fait par Hugo Mercure           *
  14. *       MERH13038507 - INF3172 gr: 20       *
  15. ************************************************/
  16. /**
  17. * doExec va créer un processus fils pour executer la commande sur le fichier trouvé
  18. */
  19. void doExec(char * szFullName, char** comm){
  20.  
  21.     switch(fork()){
  22.         case 0:
  23.             execvp (comm[0], comm);
  24.             printf("Erreur commande");
  25.             _exit(1);
  26.             break;
  27.         default:
  28.             wait(NULL);
  29. //          printf("parent\n");
  30.         break;
  31.     }
  32.  
  33. }
  34.  
  35. /**
  36. * listdir va regarder tout les elements d'un dossier afin de trouver le fichier voulu
  37. * si un element est également un dossier, listdir cherche dans ce dossier de manière récursive
  38. * en se lanceant avec le nouveau chemin.
  39. * @path est le dossier à ouvrir
  40. * @sName est le nom de fichier à trouver
  41. * @comm est le tableau de commandes l'élément -2 du tableau est mis à jour ici et représente
  42. * le chemin du fichier trouvé
  43. * @commande détermine si on doit executer une commande ou simplement lister le fichier et son
  44. * chemin
  45. * @index est la position dans le tableau comm qui correspond au chemin du fichier
  46. */
  47.  
  48. int listdir(char *path, char *sName, char** comm, int commande, int index){
  49.     struct stat stDirInfo;
  50.     struct dirent * entry;
  51.     DIR *dp;
  52.     char szFullName[MAXPATHLEN];
  53.     char szDirectory[MAXPATHLEN];
  54.     struct stat stFileInfo;
  55.  
  56.     strncpy(szDirectory, path, MAXPATHLEN -1);
  57.  
  58.     lstat(szDirectory, &stDirInfo);
  59.  
  60.     dp = opendir(path);
  61.     if(dp == NULL){
  62.         perror("opendir");
  63.         return 1;
  64.     }
  65.  
  66.     while((entry = readdir(dp))){
  67.         char * testName = entry->d_name;
  68.         if((strcmp(testName, ".") != 0) && (strcmp(testName, "..") != 0)){
  69.             sprintf(szFullName, "%s/%s", szDirectory, entry->d_name);
  70.             lstat(szFullName, &stFileInfo);
  71.  
  72.             if(S_ISDIR(stFileInfo.st_mode)){
  73.                 //Si l'élement lu est un dossier, on l'ouvre
  74.                 listdir(szFullName, sName, comm, commande, index);
  75.             }else{
  76.                 //Sinon on compare le nom pour voir si c'est
  77.                 //celui qu'on cherche.
  78.                 if(strcmp(entry->d_name, sName) == 0){
  79.                     if(commande == 0){
  80.                         printf("- %s\n",   szFullName);
  81.                     }
  82.                     else{
  83.                         comm[index] = szFullName; /*On donne le chemin*/
  84.                         doExec(szFullName, comm);
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     closedir(dp);
  91.     return 0;
  92. }
  93.  
  94.  
  95. /**
  96. * La fonction main recoit les arguments de la ligne de commande. Il faut au minimum 3
  97. * arguments. Soit le nom du programme, le dossier dans lequel faire des recherches ("."
  98. * pour le dossier courant) et le nom du fichier à rechercher. Dans le cas ou un 4eme
  99. * argument est spécifié on assume qu'il s'agit d'une commande suivie d'arguments.
  100. */
  101. int main(int argc, char **argv) {
  102.     if(argc < 3){
  103.         printf("Usage - %s repertoire fichier\n", argv[0]);
  104.         printf("Ou - %s repertoire fichier <commande>\n", argv[0]);
  105.     }
  106.     else if (argc == 3){
  107.         char* comm[1];
  108.         listdir(argv[1], argv[2], comm, 0, 0);
  109.     }
  110.     else if(argc > 3){
  111.         char* comm[argc - 1];
  112.         int i;
  113.  
  114.         for(i = 0 ; i < argc-3 ; i++){
  115.             comm[i] = argv[i+3];
  116.         }
  117.         int fileP = argc - 3;
  118.         comm[fileP] = ".";
  119.         comm[argc -2] = (char*)0;
  120.         listdir(argv[1], argv[2], comm, 1, fileP);
  121.     }
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement