home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104040b < prev    next >
Text File  |  1993-02-27  |  2KB  |  92 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 suidfile(name)
  9. char *name;
  10. {
  11.     struct stat stbuf;
  12.     stat(name, &stbuf);
  13.     if (stbuf.st_mode & S_ISUID) printf("%s\n", name);
  14.     return 0;
  15. }
  16.  
  17. int mapdir(name, filefn)
  18. char *name;
  19. int (*filefn)();
  20. {
  21.     DIR *dirp;
  22.     struct dirent *entry;
  23.     struct stat stbuf;
  24.     char olddir[MAXPATHLEN];
  25.     char cname[MAXPATHLEN];
  26.  
  27.     getwd(olddir);
  28.     strcpy(cname, name);
  29.     if (chdir(name)) {
  30.         printf("Couldn't change to directory %s\n", name);
  31.         return -1;
  32.     }
  33.     if ((dirp = opendir(name)) == NULL){
  34.         printf("Unable tto open DIR %s\n", name);
  35.         chdir(olddir);
  36.         return -1;
  37.     }
  38.     for (entry=readdir(dirp); entry != NULL; entry=readdir(dirp)) {
  39.         if (0 != stat(entry->d_name, &stbuf))
  40.             printf("Can't read file information %s\n", entry->d_name);
  41.         else if (strcmp(entry->d_name, ".") == 0  ||
  42.             strcmp(entry->d_name, "..") == 0)
  43.             /* don't pursue these entries */ ;
  44.         else if (S_ISLNK(stbuf.st_mode))
  45.             /* don't pursue links */ ;
  46.         else if (S_ISDIR(stbuf.st_mode)) {
  47.             if (LASTCHR(cname) != '/') strcat(cname, "/");
  48.             strcat(cname, entry->d_name);
  49.             mapdir(cname, filefn);
  50.             strcpy(cname,name);
  51.         } else
  52.             (*filefn) (entry->d_name);
  53.     }
  54.     closedir(dirp);
  55.     chdir(olddir);
  56.     return 0;
  57. }
  58.  
  59. char *setupdir(buf, name)
  60. char *name;
  61. char *buf;
  62. {
  63.     char curdir[MAXPATHLEN];
  64.  
  65.     getwd(curdir);
  66.     if (name[0] == '/')     /* absolute pathname */
  67.         strcpy(buf, name);
  68.     else {                  /* relative pathname*/
  69.         strcpy(buf, curdir);
  70.         if (buf[strlen(buf)-1] != '/') strcat(buf, "/"); /* may be at root */
  71.         strcat(buf, name);
  72.     }
  73.     return buf;
  74. }
  75.  
  76. int main(argc, argv)
  77. int argc;
  78. char **argv;
  79. {
  80.     char *filefn;
  81.     char name[MAXPATHLEN];
  82.  
  83.     if (argc < 2) {
  84.         printf("Usage: %s <directory>",argv[0]);
  85.         return -1;
  86.     }
  87.     setupdir(name, argv[1]);
  88.     filefn = (char *) suidfile;
  89.     return mapdir(name, filefn);
  90. }
  91.  
  92.