xref: /csrg-svn/old/vpr/vprm.c (revision 32589)
1*32589Sbostic static char vprmSCCSid[] = "@(#)vprm.c	1.4\t11/04/87";
22415Shalbert 
37663Smckusick #include <sys/param.h>
4*32589Sbostic #include <sys/dir.h>
5*32589Sbostic #include <sys/stat.h>
61854Sroot #include <stdio.h>
71854Sroot 
82415Shalbert char *basename();
92415Shalbert 
101854Sroot extern	char _sobuf[];
111854Sroot 
main(argc,argv)121854Sroot main(argc, argv)
131854Sroot 	int argc;
141854Sroot 	char *argv[];
151854Sroot {
161854Sroot 	setbuf(stdout, _sobuf);
171854Sroot 	argc--, argv++;
181854Sroot 	if (argc == 0) {
191854Sroot 		fprintf(stderr, "usage: vprm [ id ... ] [ filename ... ] [ user ... ]\n");
201854Sroot 		exit(1);
211854Sroot 	}
221854Sroot 
232415Shalbert 	/* Look for something to delete both in Varian and Versatec spool dirs. */
242415Shalbert 	delete("Varian", "/usr/spool/vad", argc, argv);
252415Shalbert 	delete("Versatec", "/usr/spool/vpd", argc, argv);
262415Shalbert }
272415Shalbert 
282415Shalbert /*
292415Shalbert  * Look through the argv list for things to delete.
302415Shalbert  */
311854Sroot 
delete(devname,spooldir,argc,argv)322415Shalbert delete(devname, spooldir, argc, argv)
337663Smckusick 	char *devname, *spooldir, *argv[];
347663Smckusick 	int argc;
352415Shalbert {
367663Smckusick 	DIR *dir;		/* The spool dir. */
377663Smckusick 	struct direct *dirp;	/* An entry read from the spool dir.*/
382415Shalbert 	int deletion = 0;	/* Flag noting something has been deleted. */
391854Sroot 
402415Shalbert 	/* Change to the spool directory. */
412415Shalbert 	if (chdir(spooldir) < 0) {
422415Shalbert 		perror(spooldir);
432415Shalbert 		return(1);
441854Sroot 	}
451854Sroot 
462415Shalbert 	/* Open it. */
477663Smckusick 	if ((dir = opendir(".")) == NULL) {
482415Shalbert 		perror(spooldir);
492415Shalbert 		return(1);
501854Sroot 	}
511854Sroot 
522415Shalbert 	printf("%s -", devname);
531854Sroot 
542415Shalbert 	/*
552415Shalbert 	 * Loop through the args and the spool dir, looking for a spool
562415Shalbert 	 * command file (has a prefix of 'df'),
572415Shalbert 	 * and trying to match it with the argument.
582415Shalbert 	 */
592415Shalbert 	while (argc-- > 0) {
607663Smckusick 		rewinddir(dir);
617663Smckusick 		while ((dirp = readdir(dir)) != NULL) {
627663Smckusick 			if (dirp->d_name[0] == 'd' &&
637663Smckusick 			    dirp->d_name[1] == 'f' &&
647663Smckusick 			    delcheck(dirp->d_name, *argv)) {
657663Smckusick 				printf(" removing %s", &(dirp->d_name[3]));
667663Smckusick 				deletion = 1;
672415Shalbert 			}
681854Sroot 		}
692415Shalbert 		argv++;
701854Sroot 	}
717663Smckusick 	closedir(dir);
722415Shalbert 	if (!deletion)
732415Shalbert 		printf(" nothing to remove\n");
742415Shalbert 	else
752415Shalbert 		putchar('\n');
761854Sroot }
771854Sroot 
782415Shalbert 
792415Shalbert /*
802415Shalbert  * delcheck tries to match the given arg against the given command file in
812415Shalbert  * various ways.  For instance, if dfname = 'dfa12345', then there is a match if
822415Shalbert  * arg == 'dfa12345', or
832415Shalbert  * arg == '12345', or
842415Shalbert  * arg == the name given on the L line of the file (the owner), or
852415Shalbert  * arg == the basename of a file given on a command line in the file.
862415Shalbert  * If there is a match, all the U (unlink) commands in the command file
872415Shalbert  * are executed, and then the command file itself is unlinked.
882415Shalbert  */
892415Shalbert 
902415Shalbert int
delcheck(dfname,arg)912415Shalbert delcheck(dfname, arg)
927663Smckusick 	char *dfname, *arg;
931854Sroot {
942415Shalbert 	FILE *df = NULL;	/* The command file. */
952415Shalbert 	int delfile = 0;	/* A match was found, so do a deletion. */
962415Shalbert 	char line[256];		/* Command line in command file. */
971854Sroot 
982415Shalbert 	if (strcmp(arg, dfname) == 0)
992415Shalbert 		delfile = 1;	/* arg == 'dfa12345'. */
1002415Shalbert 	else if (strcmp(arg, dfname+3) == 0)
1012415Shalbert 		delfile = 1;	/* arg == '12345' (skip 'dfa'). */
1022415Shalbert 	else {			/* No match; look inside on command lines. */
1032415Shalbert 		if ((df = fopen(dfname, "r")) == NULL)
1042415Shalbert 			return(0);
1052415Shalbert 		while (!delfile && getline(df, line)) {
1062415Shalbert 			switch (line[0]) {
1072415Shalbert 				case 'L':
1082415Shalbert 					/* Check owner name. */
1092415Shalbert 					if (strcmp(arg, line+1) == 0)
1102415Shalbert 						delfile = 1;
1112415Shalbert 					break;
1121854Sroot 
1132415Shalbert 				case 'C':
1142415Shalbert 				case 'V':
1152415Shalbert 				case 'F':
1162415Shalbert 				case 'G':
1172415Shalbert 				case 'P':
1182415Shalbert 				case 'T':
1192415Shalbert 					/* Check command line arg. */
1202415Shalbert 					if (strcmp (basename(arg), basename(line)) == 0)
1212415Shalbert 						delfile = 1;
1222415Shalbert 					break;
1232415Shalbert 			}
1241854Sroot 		}
1251854Sroot 	}
1261854Sroot 
1272415Shalbert 	if (delfile) {
1282415Shalbert 		if (df == NULL)		/* File not open yet. */
1292415Shalbert 			df = fopen(dfname, "r");
1302415Shalbert 		if (df == NULL)
1312415Shalbert 			return(0);
1322415Shalbert 
1332415Shalbert 		/* Run through the command file, executing Unlink commands. */
1342415Shalbert 		rewind(df);
1352415Shalbert 		while (getline(df, line)) {
1362415Shalbert 			if (line[0] == 'U')
1372415Shalbert 				unlink(line+1);
1382415Shalbert 		}
1391854Sroot 
1402415Shalbert 		unlink(dfname);		/* Now unlink the command file itself. */
1412415Shalbert 	}
1422415Shalbert 
1432415Shalbert 	if (df != NULL)
1442415Shalbert 		fclose(df);
1452415Shalbert 	return(delfile);
1461854Sroot }
1471854Sroot 
1482415Shalbert 
1492415Shalbert 
1502415Shalbert 
getline(file,line)1512415Shalbert getline(file, line)
1527663Smckusick 	FILE *file;
1537663Smckusick 	char *line;
1541854Sroot {
1551854Sroot 	register int i, c;
1561854Sroot 
1571854Sroot 	i = 0;
1582415Shalbert 	while ((c = getc(file)) != '\n') {
1591854Sroot 		if (c <= 0)
1601854Sroot 			return(0);
1612415Shalbert 		if (i < 256)
1621854Sroot 			line[i++] = c;
1631854Sroot 	}
1641854Sroot 	line[i++] = 0;
1651854Sroot 	return (1);
1661854Sroot }
1672415Shalbert 
1682415Shalbert 
1692415Shalbert /*
1702415Shalbert  * basename gets the final component of the given pathname. E.g. "c" in
1712415Shalbert  * /a/b/c.
1722415Shalbert  */
1732415Shalbert 
1742415Shalbert char *
basename(pathname)1752415Shalbert basename(pathname)
1767663Smckusick 	char *pathname;
1772415Shalbert {
1782415Shalbert 	register char *lastname;
1792415Shalbert 
1802415Shalbert 	lastname = pathname + strlen(pathname)-1; /* Move to last char in name. */
1812415Shalbert 	while (lastname >= pathname) {
1822415Shalbert 		if (*lastname == '/')
1832415Shalbert 			return(lastname + 1);
1842415Shalbert 		lastname--;
1852415Shalbert 	}
1862415Shalbert 	return(pathname);		/* No /'s at all. */
1872415Shalbert }
188