xref: /csrg-svn/usr.bin/uucp/uuclean/uuclean.c (revision 33573)
113675Ssam #ifndef lint
2*33573Srick static char sccsid[] = "@(#)uuclean.c	5.7	02/24/88";
313675Ssam #endif
413675Ssam 
523678Sbloom #include <signal.h>
613675Ssam #include "uucp.h"
713675Ssam #include <pwd.h>
813675Ssam #include <sys/stat.h>
913675Ssam #ifdef	NDIR
1013675Ssam #include "ndir.h"
1113675Ssam #else
1213702Ssam #include <sys/dir.h>
1313675Ssam #endif
1413675Ssam 
1523678Sbloom /*
1613675Ssam  *
1723678Sbloom  *	this program will search through the spool
1813675Ssam  *	directory (Spool) and delete all files with a requested
1913675Ssam  *	prefix which are older than (nomtime) seconds.
2013675Ssam  *	If the -m option is set, the program will try to
2113675Ssam  *	send mail to the usid of the file.
2213675Ssam  *
2313675Ssam  *	options:
2413675Ssam  *		-m  -  send mail for deleted file
2513675Ssam  *		-d  -  directory to clean
2613675Ssam  *		-n  -  time to age files before delete (in hours)
2713675Ssam  *		-p  -  prefix for search
2813675Ssam  *		-x  -  turn on debug outputs
2913675Ssam  *	exit status:
3013675Ssam  *		0  -  normal return
3113675Ssam  *		1  -  can not read directory
3213675Ssam  */
3313675Ssam 
3413675Ssam #define NOMTIME 72	/* hours to age files before deletion */
3513675Ssam 
3617840Sralph int checkprefix = 0;
3725139Sbloom struct timeb Now;
3817840Sralph 
3913675Ssam main(argc, argv)
4013675Ssam char *argv[];
4113675Ssam {
42*33573Srick 	register DIR *dirp;
43*33573Srick 	register struct direct *dentp;
4413675Ssam 	time_t nomtime, ptime;
4513675Ssam 	struct stat stbuf;
46*33573Srick 	int mflg = 0;
4713675Ssam 
4813675Ssam 	strcpy(Progname, "uuclean");
4913675Ssam 	uucpname(Myname);
5013675Ssam 	nomtime = NOMTIME * (time_t)3600;
5113675Ssam 
5213675Ssam 	while (argc>1 && argv[1][0] == '-') {
5313675Ssam 		switch (argv[1][1]) {
5413675Ssam 		case 'd':
5513675Ssam 			Spool = &argv[1][2];
5613675Ssam 			break;
5713675Ssam 		case 'm':
5813675Ssam 			mflg = 1;
5913675Ssam 			break;
6013675Ssam 		case 'n':
6113675Ssam 			nomtime = atoi(&argv[1][2]) * (time_t)3600;
6213675Ssam 			break;
6313675Ssam 		case 'p':
6417840Sralph 			checkprefix = 1;
6513675Ssam 			if (&argv[1][2] != '\0')
6613675Ssam 				stpre(&argv[1][2]);
6713675Ssam 			break;
6813675Ssam 		case 'x':
6917840Sralph 			chkdebug();
7013675Ssam 			Debug = atoi(&argv[1][2]);
7113675Ssam 			if (Debug <= 0)
7213675Ssam 				Debug = 1;
7313675Ssam 			break;
7413675Ssam 		default:
7513675Ssam 			printf("unknown flag %s\n", argv[1]); break;
7613675Ssam 		}
7713675Ssam 		--argc;  argv++;
7813675Ssam 	}
7913675Ssam 
8013675Ssam 	DEBUG(4, "DEBUG# %s\n", "START");
8117840Sralph 	if (chdir(Spool) < 0) {	/* NO subdirs in uuclean!  rti!trt */
8217840Sralph 		printf("%s directory inaccessible\n", Spool);
8317840Sralph 		exit(1);
8417840Sralph 	}
8513675Ssam 
8613675Ssam 	if ((dirp = opendir(Spool)) == NULL) {
8713675Ssam 		printf("%s directory unreadable\n", Spool);
8813675Ssam 		exit(1);
8913675Ssam 	}
9013675Ssam 
9113675Ssam 	time(&ptime);
92*33573Srick 	while (dentp = readdir(dirp)) {
93*33573Srick 		if (checkprefix && !chkpre(dentp->d_name))
9413675Ssam 			continue;
9513675Ssam 
96*33573Srick 		if (stat(dentp->d_name, &stbuf) == -1) {
97*33573Srick 			DEBUG(4, "stat on %s failed\n", dentp->d_name);
9813675Ssam 			continue;
9913675Ssam 		}
10013675Ssam 
10113675Ssam 
10213675Ssam 		if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
10313675Ssam 			continue;
10413675Ssam 		if ((ptime - stbuf.st_mtime) < nomtime)
10513675Ssam 			continue;
106*33573Srick 		if (dentp->d_name[0] == CMDPRE)
107*33573Srick 			notfyuser(dentp->d_name);
108*33573Srick 		DEBUG(4, "unlink file %s\n", dentp->d_name);
109*33573Srick 		unlink(dentp->d_name);
11017840Sralph 		if (mflg)
111*33573Srick 			sdmail(dentp->d_name, stbuf.st_uid);
11213675Ssam 	}
11313675Ssam 
11413675Ssam 	closedir(dirp);
11513675Ssam 	exit(0);
11613675Ssam }
11713675Ssam 
11813675Ssam 
11913675Ssam #define MAXPRE 10
12013675Ssam char Pre[MAXPRE][NAMESIZE];
12113675Ssam int Npre = 0;
12213675Ssam /***
12313675Ssam  *	chkpre(file)	check for prefix
12413675Ssam  *	char *file;
12513675Ssam  *
12613675Ssam  *	return codes:
12713675Ssam  *		0  -  not prefix
12813675Ssam  *		1  -  is prefix
12913675Ssam  */
13013675Ssam 
13113675Ssam chkpre(file)
13213675Ssam char *file;
13313675Ssam {
13413675Ssam 	int i;
13513675Ssam 
13613675Ssam 	for (i = 0; i < Npre; i++) {
13713675Ssam 		if (prefix(Pre[i], file))
13813675Ssam 			return(1);
13913675Ssam 		}
14013675Ssam 	return(0);
14113675Ssam }
14213675Ssam 
14313675Ssam /***
14413675Ssam  *	stpre(p)	store prefix
14513675Ssam  *	char *p;
14613675Ssam  *
14713675Ssam  *	return codes:  none
14813675Ssam  */
14913675Ssam 
15013675Ssam stpre(p)
15113675Ssam char *p;
15213675Ssam {
15313675Ssam 	if (Npre < MAXPRE - 2)
15413675Ssam 		strcpy(Pre[Npre++], p);
15513675Ssam 	return;
15613675Ssam }
15713675Ssam 
15813675Ssam /***
15913675Ssam  *	notfyuser(file)	- notfiy requestor of deleted requres
16013675Ssam  *
16113675Ssam  *	return code - none
16213675Ssam  */
16313675Ssam 
16413675Ssam notfyuser(file)
16513675Ssam char *file;
16613675Ssam {
16713675Ssam 	FILE *fp;
16813675Ssam 	int numrq;
16913675Ssam 	char frqst[100], lrqst[100];
17013675Ssam 	char msg[BUFSIZ];
17113675Ssam 	char *args[10];
17213675Ssam 
17313675Ssam 	if ((fp = fopen(file, "r")) == NULL)
17413675Ssam 		return;
17513675Ssam 	if (fgets(frqst, 100, fp) == NULL) {
17613675Ssam 		fclose(fp);
17713675Ssam 		return;
17813675Ssam 	}
17913675Ssam 	numrq = 1;
18013675Ssam 	while (fgets(lrqst, 100, fp))
18113675Ssam 		numrq++;
18213675Ssam 	fclose(fp);
18313675Ssam 	sprintf(msg,
18413675Ssam 	  "File %s delete. \nCould not contact remote. \n%d requests deleted.\n", file, numrq);
18513675Ssam 	if (numrq == 1) {
18613675Ssam 		strcat(msg, "REQUEST: ");
18713675Ssam 		strcat(msg, frqst);
18817840Sralph 	} else {
18913675Ssam 		strcat(msg, "FIRST REQUEST: ");
19013675Ssam 		strcat(msg, frqst);
19113675Ssam 		strcat(msg, "\nLAST REQUEST: ");
19213675Ssam 		strcat(msg, lrqst);
19313675Ssam 	}
19417840Sralph 	getargs(frqst, args, 10);
19517840Sralph 	mailst(args[3], msg, CNULL);
19613675Ssam }
19713675Ssam 
19813675Ssam 
19913675Ssam /***
20013675Ssam  *	sdmail(file, uid)
20113675Ssam  *
20213675Ssam  *	sdmail  -  this routine will determine the owner
20313675Ssam  *	of the file (file), create a message string and
20413675Ssam  *	call "mailst" to send the cleanup message.
20513675Ssam  *	This is only implemented for local system
20613675Ssam  *	mail at this time.
20713675Ssam  */
20813675Ssam 
20913675Ssam sdmail(file, uid)
21013675Ssam char *file;
21113675Ssam {
21213675Ssam 	static struct passwd *pwd;
21313675Ssam 	struct passwd *getpwuid();
21413675Ssam 	char mstr[40];
21513675Ssam 
21613675Ssam 	sprintf(mstr, "uuclean deleted file %s\n", file);
21717840Sralph 	if (pwd != NULL && pwd->pw_uid == uid) {
21817840Sralph 		mailst(pwd->pw_name, mstr, CNULL);
21917840Sralph 		return;
22013675Ssam 	}
22113675Ssam 
22213675Ssam 	setpwent();
22317840Sralph 	if ((pwd = getpwuid(uid)) != NULL)
22417840Sralph 		mailst(pwd->pw_name, mstr, CNULL);
22513675Ssam }
22613675Ssam 
22713675Ssam cleanup(code)
22813675Ssam int code;
22913675Ssam {
23013675Ssam 	exit(code);
23113675Ssam }
232