converted man page
[unix-history] / usr / src / old / vpr / vprm.c
index f16f3ab..ae53f69 100644 (file)
@@ -1,26 +1,18 @@
-#include <sys/types.h>
-#include <dir.h>
-#include <stat.h>
+static char vprmSCCSid[] = "@(#)vprm.c 1.4\t%G%";
+
+#include <sys/param.h>
+#include <sys/dir.h>
+#include <sys/stat.h>
 #include <stdio.h>
 
 #include <stdio.h>
 
-char   line[128];
-int    linel;
-int    wide;
-char   *spooldir;
-FILE   *df;
-FILE   *dfb;
+char *basename();
+
 extern char _sobuf[];
 
 main(argc, argv)
        int argc;
        char *argv[];
 {
 extern char _sobuf[];
 
 main(argc, argv)
        int argc;
        char *argv[];
 {
-       register int i;
-       register char *ap, *cp;
-       int cnt;
-       int oldargc;
-       char **oldargv;
-
        setbuf(stdout, _sobuf);
        argc--, argv++;
        if (argc == 0) {
        setbuf(stdout, _sobuf);
        argc--, argv++;
        if (argc == 0) {
@@ -28,120 +20,168 @@ main(argc, argv)
                exit(1);
        }
 
                exit(1);
        }
 
+       /* Look for something to delete both in Varian and Versatec spool dirs. */
+       delete("Varian", "/usr/spool/vad", argc, argv);
+       delete("Versatec", "/usr/spool/vpd", argc, argv);
+}
+\f
+/*
+ * Look through the argv list for things to delete.
+ */
 
 
-       oldargc = argc; oldargv = argv;
-
-       printf("Varian - ");
-       if (chdir("/usr/spool/vad") < 0)
-               perror("/usr/spool/vad");
-       else {
-               df = fopen(".", "r");
-               if (df == NULL)
-                       perror("/usr/spool/vad");
-               else do {
-                       clobber(*oldargv++);
-               } while (--oldargc);
+delete(devname, spooldir, argc, argv)
+       char *devname, *spooldir, *argv[];
+       int argc;
+{
+       DIR *dir;               /* The spool dir. */
+       struct direct *dirp;    /* An entry read from the spool dir.*/
+       int deletion = 0;       /* Flag noting something has been deleted. */
+
+       /* Change to the spool directory. */
+       if (chdir(spooldir) < 0) {
+               perror(spooldir);
+               return(1);
        }
 
        }
 
-       printf("Versatec - ");
-       if (chdir("/usr/spool/vpd") < 0)
-               perror("/usr/spool/vpd");
-       else {
-               df = fopen(".", "r");
-               if (df == NULL)
-                       perror("/usr/spool/vpd");
-               else do {
-                       clobber(*argv++);
-               } while (--argc);
+       /* Open it. */
+       if ((dir = opendir(".")) == NULL) {
+               perror(spooldir);
+               return(1);
        }
        }
-}
 
 
-clobber(cp)
-       char *cp;
-{
-       struct dir dirent;
-       int did = 0;
-
-       rewind(df);
-       while (fread(&dirent, sizeof dirent, 1, df) == 1) {
-               if (dirent.d_ino == 0)
-                       continue;
-               if (dirent.d_name[0] != 'd' || dirent.d_name[1] != 'f')
-                       continue;
-               if (dirent.d_name[7] == 0 || dirent.d_name[8] != 0)
-                       continue;
-               if (chkclob(cp, dirent.d_name)) {
-                       did++;
-                       printf("removing %s\n", dirent.d_name+3);
-                       unlink(dirent.d_name);
-                       dirent.d_name[0] = 'c'; unlink(dirent.d_name);
-                       dirent.d_name[2] = 'b'; unlink(dirent.d_name);
-                       dirent.d_name[2] = 'a';
-                       dirent.d_name[0] = 'l'; unlink(dirent.d_name);
-                       dirent.d_name[0] = 't'; unlink(dirent.d_name);
-                       dirent.d_name[0] = 'd';
+       printf("%s -", devname);
+
+       /*
+        * Loop through the args and the spool dir, looking for a spool
+        * command file (has a prefix of 'df'),
+        * and trying to match it with the argument.
+        */
+       while (argc-- > 0) {
+               rewinddir(dir);
+               while ((dirp = readdir(dir)) != NULL) {
+                       if (dirp->d_name[0] == 'd' &&
+                           dirp->d_name[1] == 'f' &&
+                           delcheck(dirp->d_name, *argv)) {
+                               printf(" removing %s", &(dirp->d_name[3]));
+                               deletion = 1;
+                       }
                }
                }
+               argv++;
        }
        }
-       if (did == 0)
-               printf("%s: nothing to remove\n", cp);
+       closedir(dir);
+       if (!deletion)
+               printf(" nothing to remove\n");
+       else
+               putchar('\n');
 }
 
 }
 
-chkclob(pattern, file)
-       char *pattern, *file;
+
+/*
+ * delcheck tries to match the given arg against the given command file in
+ * various ways.  For instance, if dfname = 'dfa12345', then there is a match if
+ * arg == 'dfa12345', or
+ * arg == '12345', or
+ * arg == the name given on the L line of the file (the owner), or
+ * arg == the basename of a file given on a command line in the file.
+ * If there is a match, all the U (unlink) commands in the command file
+ * are executed, and then the command file itself is unlinked.
+ */
+
+int
+delcheck(dfname, arg)
+       char *dfname, *arg;
 {
 {
-       register char *id = pattern;
+       FILE *df = NULL;        /* The command file. */
+       int delfile = 0;        /* A match was found, so do a deletion. */
+       char line[256];         /* Command line in command file. */
+
+       if (strcmp(arg, dfname) == 0)
+               delfile = 1;    /* arg == 'dfa12345'. */
+       else if (strcmp(arg, dfname+3) == 0)
+               delfile = 1;    /* arg == '12345' (skip 'dfa'). */
+       else {                  /* No match; look inside on command lines. */
+               if ((df = fopen(dfname, "r")) == NULL)
+                       return(0);
+               while (!delfile && getline(df, line)) {
+                       switch (line[0]) {
+                               case 'L':
+                                       /* Check owner name. */
+                                       if (strcmp(arg, line+1) == 0)
+                                               delfile = 1;
+                                       break;
+
+                               case 'C':
+                               case 'V':
+                               case 'F':
+                               case 'G':
+                               case 'P':
+                               case 'T':
+                                       /* Check command line arg. */
+                                       if (strcmp (basename(arg), basename(line)) == 0)
+                                               delfile = 1;
+                                       break;
+                       }
+               }
+       }
 
 
-       /*
-        * Quick check for matching id
-        */
-       if (any(id[0], "cd") && id[1] == 'f' && id[2] == 'a')
-               id += 3;
-       if (strcmp(file+3, id) == 0)
-               return (1);
-       /*
-        * Now check for matching filename 'B', 'F' or id 'L'
-        */
-       dfb = fopen(file, "r");
-       if (dfb == NULL)
-               return (0);
-       while (getline()) switch (line[0]) {
-
-       case 'L':
-       case 'B':
-       case 'F':
-       case 'T':
-               if (strcmp(line+1, pattern) == 0) {
-                       fclose(dfb);
-                       return (1);
+       if (delfile) {
+               if (df == NULL)         /* File not open yet. */
+                       df = fopen(dfname, "r");
+               if (df == NULL)
+                       return(0);
+               
+               /* Run through the command file, executing Unlink commands. */
+               rewind(df);
+               while (getline(df, line)) {
+                       if (line[0] == 'U')
+                               unlink(line+1);
                }
                }
-               continue;
+
+               unlink(dfname);         /* Now unlink the command file itself. */
        }
        }
-       fclose(dfb);
-       return (0);
+
+       if (df != NULL)
+               fclose(df);
+       return(delfile);
 }
 
 }
 
-any(c, cp)
-       char c;
-       register char *cp;
-{
 
 
-       while (*cp)
-               if (c == *cp++)
-                       return (1);
-       return (0);
-}
 
 
-getline()
+
+getline(file, line)
+       FILE *file;
+       char *line;
 {
        register int i, c;
 
        i = 0;
 {
        register int i, c;
 
        i = 0;
-       while ((c = getc(dfb)) != '\n') {
+       while ((c = getc(file)) != '\n') {
                if (c <= 0)
                        return(0);
                if (c <= 0)
                        return(0);
-               if (i < 100)
+               if (i < 256)
                        line[i++] = c;
        }
        line[i++] = 0;
        return (1);
 }
                        line[i++] = c;
        }
        line[i++] = 0;
        return (1);
 }
+
+
+/*
+ * basename gets the final component of the given pathname. E.g. "c" in
+ * /a/b/c.
+ */
+
+char *
+basename(pathname)
+       char *pathname;
+{
+       register char *lastname;
+
+       lastname = pathname + strlen(pathname)-1; /* Move to last char in name. */
+       while (lastname >= pathname) {
+               if (*lastname == '/')
+                       return(lastname + 1);
+               lastname--;
+       }
+       return(pathname);               /* No /'s at all. */
+}