home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104042a < prev    next >
Text File  |  1993-02-27  |  2KB  |  104 lines

  1. #include <string.h>
  2. #include <dirent.h>
  3. #include <sys/stat.h>
  4. #include <sys/param.h>
  5.  
  6. #define LASTCHR(s) s[strlen(s)-1]
  7.  
  8. int do_nothing(name)
  9. char *name;
  10. {       return 0; }
  11.  
  12. int printfile(name)
  13. char *name;
  14. {       printf("%s\n", name);
  15.     return 0;
  16. }
  17.  
  18. int suidfile(name)
  19. char *name;
  20. {       struct stat stbuf;
  21.     stat(name, &stbuf);
  22.     return (stbuf.st_mode & S_ISUID);
  23. }
  24.     
  25. int mapdir(name, predfn, thenfn, elsefn)
  26. char *name;
  27. int (*predfn)(), (*thenfn)(), (*elsefn)();
  28. {
  29.     DIR *dirp;
  30.     struct dirent *entry;
  31.     struct stat stbuf;
  32.     char olddir[MAXPATHLEN];
  33.     char cname[MAXPATHLEN];
  34.  
  35.     getwd(olddir);
  36.     strcpy(cname, name);
  37.     if (chdir(name)) {
  38.         printf("Couldn't change to directory %s\n", name);
  39.         return -1;
  40.     }
  41.     if ((dirp = opendir(name)) == NULL){
  42.         printf("Unable tto open DIR %s\n", name);
  43.         chdir(olddir);
  44.         return -1;
  45.     }
  46.     for (entry=readdir(dirp); entry != NULL; entry=readdir(dirp)) {
  47.         if (0 != stat(entry->d_name, &stbuf))
  48.             printf("Can't read file information %s\n", 
  49.                 entry->d_name);
  50.         else if (strcmp(entry->d_name, ".") == 0  ||
  51.             strcmp(entry->d_name, "..") == 0)
  52.             /* don't pursue these entries */ ;
  53.         else if (S_ISLNK(stbuf.st_mode))
  54.             /* don't pursue links */ ;
  55.         else if (S_ISDIR(stbuf.st_mode)) {
  56.             if (LASTCHR(cname) != '/') strcat(cname, "/");
  57.             strcat(cname, entry->d_name);
  58.             mapdir(cname, predfn, thenfn, elsefn);
  59.             strcpy(cname,name);
  60.         } else
  61.             if ((*predfn) (entry->d_name))
  62.                 (*thenfn) (entry->d_name);
  63.             else (*elsefn) (entry->d_name);
  64.     }
  65.     closedir(dirp);
  66.     chdir(olddir);
  67.     return 0;
  68. }
  69.  
  70. char *setupdir(buf, name)
  71. char *name;
  72. char *buf;
  73. {
  74.     char curdir[MAXPATHLEN];
  75.  
  76.     getwd(curdir);
  77.     if (name[0] == '/')     
  78.         strcpy(buf, name);
  79.     else {  strcpy(buf, curdir);
  80.         if (buf[strlen(buf)-1] != '/') strcat(buf, "/"); /* may be at root */
  81.         strcat(buf, name);
  82.     }
  83.     return buf;
  84. }
  85.  
  86. int main(argc, argv)
  87. int argc;
  88. char **argv;
  89. {
  90.     char *predfn, *thenfn, *elsefn;
  91.     char name[MAXPATHLEN];
  92.  
  93.     if (argc < 2) {
  94.         printf("Usage: %s <directory>",argv[0]);
  95.         return -1;
  96.     }
  97.     setupdir(name, argv[1]);
  98.     predfn = (char *) suidfile;
  99.     thenfn = (char *) printfile;
  100.     elsefn = (char *) do_nothing;
  101.     return mapdir(name, predfn, thenfn, elsefn);
  102. }
  103.  
  104.