home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- *
- * RECURCAN.C
- * this is used to traverse a directory tree when doing a
- * recursive can passed the directory to be examined
- *
- ************************************************************************/
-
- #include "can.h"
-
- void recurcan(temp)
- char *temp;
- {
- DIR *DIRP; /* Used to list the directory by pointing at .trashcan */
- struct direct *DP; /* Used to point at consecutive entries when listing */
- char FILENAME[MAXPATHLEN]; /* String containing .trashcan/current file*/
- char *ACTNAME; /* Points to filename without any leading directory */
- struct stat BUF; /* Used to point to mode info about files */
- char TEMP[MAXPATHLEN]; /* this is used to look at the current file */
- char WORKDIR[MAXPATHLEN]; /* the working directory we came from */
-
- DIRP = opendir(temp); /* open the directory being canned */
- getwd(WORKDIR); /* get the current working directory */
- chdir(temp); /* go to the directory to be canned */
- /* get the next directory entry */
- for(DP = readdir(DIRP); DP != NULL; DP= readdir(DIRP))
- {
- strcpy(TEMP, DP->d_name); /* put the name in TEMP */
- stat(TEMP, &BUF); /* get some info about this file */
- /* does it exist and is it not a directory */
- if ((access(TEMP, F_OK) == 0) && (!(BUF.st_mode & 0040000)))
- {
- /* interogate */
- if(INTERACTIVE != 0)
- {
- fprintf(stdout, "can %s\? ", TEMP);
- fscanf(stdin, "%s", ANSWER);
- if((ANSWER[0] != 'y') && (ANSWER[0] != 'Y'))
- continue;
- }
- /* make the name of the trash file */
- sprintf(FILENAME, "%s/%s", CAN, TEMP);
- /* get rid of the trash version if there is one */
- if (access(FILENAME, F_OK) == 0)
- if(unlink(FILENAME) != 0)
- {
- perror("can7");
- continue;
- }
- /* put file in the trash */
- if(link(TEMP, FILENAME) != 0)
- {
- switch(errno)
- {
- case EXDEV:
- crossdevcan(TEMP, FILENAME);
- break;
- default:
- perror("can2");
- continue;
- }
- }
- /* get rid of the file here */
- if(unlink(TEMP) != 0)
- {
- perror("can8");
- continue;
- }
- /* change the access time */
- TIMES.actime= (time_t)TIME;
- TIMES.modtime= BUF.st_mtime;
- if(utime(FILENAME, &TIMES) != 0)
- {
- perror("can9");
- continue;
- }
- /* tell them what happened if they want to know */
- if(VERBOSE)
- fprintf(stdout, "%s: canned\n", TEMP);
- }
- else
- {
- /* is it a directory? */
- if(BUF.st_mode & 0040000)
- {
- /* make sure it is not ".." or "." */
- if((strcmp(TEMP,"..") != 0) && (strcmp(TEMP,".") != 0))
- {
- /* make sure they want to look at this directory */
- if(INTERACTIVE != 0)
- {
- fprintf(stdout, "can check %s directory\? ", TEMP);
- fscanf(stdin, "%s", ANSWER);
- if((ANSWER[0] != 'y') && (ANSWER[0] != 'Y'))
- continue;
- }
- /* get on with recursion using newest directory */
- recurcan(TEMP);
- }
- }
- else
- perror("can10");
- }
- }
- chdir(WORKDIR); /* go back to directory from which started */
- closedir(DIRP); /* close the one we were canning */
- /* make sure they want to get rid of the directory */
- if(INTERACTIVE != 0)
- {
- fprintf(stdout, "can directory %s\? ", temp);
- fscanf(stdin, "%s", ANSWER);
- if((ANSWER[0] != 'y') && (ANSWER[0] != 'Y'))
- return;
- }
- /* get rid of the directory */
- if(rmdir(temp) != 0)
- perror("can11");
- else
- /* tell them if they want to know */
- if(VERBOSE)
- fprintf(stdout, "%s: canned directory\n", temp);
- }
-