home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / can2 / recurcan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.4 KB  |  123 lines

  1. /************************************************************************
  2. *
  3. *    RECURCAN.C
  4. *        this is used to traverse a directory tree when doing a
  5. *        recursive can passed the directory to be examined
  6. *
  7. ************************************************************************/
  8.  
  9. #include "can.h"
  10.  
  11. void recurcan(temp)
  12. char *temp;
  13. {
  14.     DIR *DIRP;        /* Used to list the directory by pointing at .trashcan */
  15.     struct direct *DP;    /* Used to point at consecutive entries when listing */
  16.     char FILENAME[MAXPATHLEN];    /* String containing .trashcan/current file*/    
  17.     char *ACTNAME;        /* Points to filename without any leading directory */
  18.     struct stat BUF;    /* Used to point to mode info about files */
  19.     char TEMP[MAXPATHLEN];    /* this is used to look at the current file */
  20.     char WORKDIR[MAXPATHLEN];    /* the working directory we came from */
  21.  
  22.     DIRP = opendir(temp);    /* open the directory being canned */
  23.     getwd(WORKDIR);    /* get the current working directory */
  24.     chdir(temp);    /* go to the directory to be canned */
  25.     /* get the next directory entry */
  26.     for(DP = readdir(DIRP); DP != NULL; DP= readdir(DIRP))
  27.     {
  28.         strcpy(TEMP, DP->d_name);    /* put the name in TEMP */
  29.         stat(TEMP, &BUF);    /* get some info about this file */
  30.         /* does it exist and is it not a directory */
  31.         if ((access(TEMP, F_OK) == 0) && (!(BUF.st_mode & 0040000)))
  32.         {
  33.             /* interogate */
  34.             if(INTERACTIVE != 0)
  35.             {
  36.                 fprintf(stdout, "can %s\? ", TEMP);
  37.                 fscanf(stdin, "%s", ANSWER);
  38.                 if((ANSWER[0] != 'y') && (ANSWER[0] != 'Y'))
  39.                     continue;
  40.             }
  41.             /* make the name of the trash file */
  42.             sprintf(FILENAME, "%s/%s", CAN, TEMP);
  43.             /* get rid of the trash version if there is one */
  44.             if (access(FILENAME, F_OK) == 0)
  45.                 if(unlink(FILENAME) != 0)
  46.                 {
  47.                     perror("can7");
  48.                     continue;
  49.                 }
  50.             /* put file in the trash */
  51.             if(link(TEMP, FILENAME) != 0)
  52.             {
  53.                 switch(errno)
  54.                 {
  55.                     case EXDEV:
  56.                         crossdevcan(TEMP, FILENAME);
  57.                         break;
  58.                     default:
  59.                         perror("can2");
  60.                         continue;
  61.                 }
  62.             }
  63.             /* get rid of the file here */
  64.             if(unlink(TEMP) != 0)
  65.             {
  66.                 perror("can8");
  67.                 continue;
  68.             }
  69.             /* change the access time */
  70.             TIMES.actime= (time_t)TIME;
  71.             TIMES.modtime= BUF.st_mtime;
  72.             if(utime(FILENAME, &TIMES) != 0)
  73.             {
  74.                 perror("can9");
  75.                 continue;
  76.             }
  77.             /* tell them what happened if they want to know */
  78.             if(VERBOSE)
  79.                 fprintf(stdout, "%s: canned\n", TEMP);
  80.         }
  81.         else
  82.         {
  83.             /* is it a directory? */
  84.             if(BUF.st_mode & 0040000)
  85.             {
  86.                 /* make sure it is not ".." or "." */
  87.                 if((strcmp(TEMP,"..") != 0) && (strcmp(TEMP,".") != 0))
  88.                 {
  89.                     /* make sure they want to look at this directory */
  90.                     if(INTERACTIVE != 0)
  91.                     {
  92.                         fprintf(stdout, "can check %s directory\? ", TEMP);
  93.                         fscanf(stdin, "%s", ANSWER);
  94.                         if((ANSWER[0] != 'y') && (ANSWER[0] != 'Y'))
  95.                             continue;
  96.                     }
  97.                     /* get on with recursion using newest directory */
  98.                     recurcan(TEMP);
  99.                 }
  100.             }
  101.             else
  102.                 perror("can10");
  103.         }
  104.     }
  105.     chdir(WORKDIR);    /* go back to directory from which started */
  106.     closedir(DIRP);    /* close the one we were canning */
  107.     /* make sure they want to get rid of the directory */
  108.     if(INTERACTIVE != 0)
  109.     {
  110.         fprintf(stdout, "can directory %s\? ", temp);
  111.         fscanf(stdin, "%s", ANSWER);
  112.         if((ANSWER[0] != 'y') && (ANSWER[0] != 'Y'))
  113.             return;
  114.     }
  115.     /* get rid of the directory */
  116.     if(rmdir(temp) != 0)
  117.         perror("can11");
  118.     else
  119.         /* tell them if they want to know */
  120.         if(VERBOSE)
  121.             fprintf(stdout, "%s: canned directory\n", temp);
  122. }
  123.