home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 32 / fsfilt.c next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  3.6 KB  |  149 lines

  1. /*
  2.  * fsfilt - filesystem filter for find (System V).
  3.  * 
  4.  * This program is intended for machines without a -mount option on their 
  5.  * find(1) command.  We use this program for doing backups, when we do not
  6.  * want find to cross mountpoints in the directory structure.  This program
  7.  * will pick up the current working directory, read the mount table
  8.  * (/etc/mnttab on system V machines) and build a list of directories that
  9.  * should NOT be backed up.
  10.  * 
  11.  * This program is inteneded to have find(1) output piped into it.  Each
  12.  * filename is examined, and if it does not contain references to any of
  13.  * the children filesystems of the current filesystem  (if any) then the
  14.  * filename is printed on standard output, otherwise it is trashed.
  15.  *
  16.  * An example command would be:
  17.  *    find . -print | fsfilt | cpio -ocB > /dev/rmt1
  18.  * 
  19.  * In addition to children filesystems, the tmp directory in both / and /usr
  20.  * are not backed up.  This dependancy is hardcoded into the source.
  21.  *
  22.  * Please send any enhancements, comments or bugs to the author.
  23.  * 
  24.  * Author: Mark H. Colburn (mark@ems.mn.org)
  25.  *         EMS/McGraw-Hill
  26.  *
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <mnttab.h>
  33.  
  34. #define STRSIZ        200
  35.  
  36. void        perror();
  37. int        nmnt;
  38. struct mnttab    fs[NMOUNT];
  39. char        except[NMOUNT][32];
  40. int        numex = 0;
  41.  
  42. /* ARGSUSED */
  43. int    main(argc, argv)
  44. int    argc;
  45. char   *argv[];
  46. {
  47.     int             i, found;
  48.     char            str[STRSIZ];
  49.  
  50.     nmnt = read_mnttab();
  51.     get_exceptions();
  52.     while (!feof(stdin)) {
  53.         if (fgets(str, STRSIZ, stdin) == NULL && !feof(stdin))
  54.             perror("Unable to read stdin");
  55.         str[strlen(str) - 1] = '\0';
  56.         found = 0;
  57.         if (numex > 0)
  58.             for (i = 0; !found && i < numex; i++)
  59.                 found = (strncmp(str, except[i],
  60.                          strlen(except[i])) == 0);
  61.         if (!found)
  62.             puts(str);
  63.     }
  64.     return (0);
  65. }
  66.  
  67. /*
  68.  * get_exceptions - get list of children filesystem of this directory
  69.  */
  70.  
  71. int    get_exceptions()
  72. {
  73.     char            fsname[100], work[100], work1[100];
  74.     int        i, j, found = 0;
  75.     char        *cp;
  76.     char        *strindex();
  77.  
  78.     (void) getcwd(fsname, 100);
  79.     if (strcmp(fsname, "/") == 0 || strcmp(fsname,"/usr") == 0)
  80.         (void) strcpy(except[numex++], "./tmp/");
  81.     for (i = 0; i < nmnt; i++) {
  82.         if (strncmp(fsname, fs[i].mt_filsys, strlen(fsname)) == 0 &&
  83.             strcmp(fsname, fs[i].mt_filsys) != 0) {
  84.             if (strcmp(fsname, "/") == 0) 
  85.                 (void) strcpy(work, fs[i].mt_filsys+
  86.                 strlen(fsname));
  87.             else
  88.                 (void) strcpy(work, fs[i].mt_filsys +
  89.                       strlen(fsname)+1);
  90.             if ((cp = strchr(work, '/')) == NULL) {
  91.                 (void) strcpy(work1, work);
  92.                 (void) strcat(work1, "/");
  93.             } else {
  94.                 *(cp + 1) = '\0';
  95.                 (void) strcpy(work1, work);
  96.             }
  97.             found = 0;
  98.             (void) sprintf(work,"./%s", work1);
  99.             for (j = 0; !found && j < numex; j++)
  100.                 found = (strcmp(except[j], work) == 0);
  101.             if (!found)
  102.                 strcpy(except[numex++], work);
  103.         }
  104.     }
  105. }
  106.  
  107. /*
  108.  * read_mount_table - get a list of all currently mounted filesystems
  109.  */
  110.  
  111. int    read_mnttab()
  112. {
  113.     int             i, ret;
  114.     FILE           *fp;
  115.  
  116.     if ((fp = fopen("/etc/mnttab", "r")) == NULL) {
  117.         (void) fprintf("Unable to open /etc/mnttab, exiting\n");
  118.         exit(1);
  119.     }
  120.     i = 0;
  121.     while (!feof(fp))
  122.         if (fread((char *) &fs[i], sizeof(struct mnttab), 1, fp) > 0)
  123.             i++;
  124.     (void) fclose(fp);
  125.     return (i);
  126. }
  127.  
  128. /*
  129.  * char *strindex(s1,s2) - return a pointer to where s2 exists in s1, or
  130.  * NULL.
  131.  */
  132.  
  133. char    *strindex(s1, s2)
  134. char   *s1, *s2;
  135. {
  136.     register int    i;
  137.     register char  *cp;
  138.  
  139.     i = strlen(s2);
  140.     if (strlen(s1) == i)
  141.         return (strcmp(s2, s1) ? NULL : s1);
  142.     if (strlen(s1) > i) {
  143.         for (cp = s1; *cp; cp++)
  144.             if (*cp == *s2 && strncmp(cp, s2, i) == 0)
  145.                 return (cp);
  146.     }
  147.     return (NULL);
  148. }
  149.