xref: /original-bsd/usr.bin/uucp/uuclean/uuclean.c (revision 886b23cd)
1 /*-
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1985, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)uuclean.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <signal.h>
19 #include "uucp.h"
20 #include <pwd.h>
21 #include <sys/stat.h>
22 #ifdef	NDIR
23 #include "ndir.h"
24 #else
25 #include <sys/dir.h>
26 #endif
27 
28 /*
29  *
30  *	this program will search through the spool
31  *	directory (Spool) and delete all files with a requested
32  *	prefix which are older than (nomtime) seconds.
33  *	If the -m option is set, the program will try to
34  *	send mail to the usid of the file.
35  *
36  *	options:
37  *		-m  -  send mail for deleted file
38  *		-d  -  directory to clean
39  *		-n  -  time to age files before delete (in hours)
40  *		-p  -  prefix for search
41  *		-x  -  turn on debug outputs
42  *	exit status:
43  *		0  -  normal return
44  *		1  -  can not read directory
45  */
46 
47 #define NOMTIME 72	/* hours to age files before deletion */
48 
49 int checkprefix = 0;
50 struct timeb Now;
51 
main(argc,argv)52 main(argc, argv)
53 char *argv[];
54 {
55 	register DIR *dirp;
56 	register struct direct *dentp;
57 	time_t nomtime, ptime;
58 	struct stat stbuf;
59 	int mflg = 0;
60 
61 	strcpy(Progname, "uuclean");
62 	uucpname(Myname);
63 	nomtime = NOMTIME * (time_t)3600;
64 
65 	while (argc>1 && argv[1][0] == '-') {
66 		switch (argv[1][1]) {
67 		case 'd':
68 			Spool = &argv[1][2];
69 			break;
70 		case 'm':
71 			mflg = 1;
72 			break;
73 		case 'n':
74 			nomtime = atoi(&argv[1][2]) * (time_t)3600;
75 			break;
76 		case 'p':
77 			checkprefix = 1;
78 			if (&argv[1][2] != '\0')
79 				stpre(&argv[1][2]);
80 			break;
81 		case 'x':
82 			chkdebug();
83 			Debug = atoi(&argv[1][2]);
84 			if (Debug <= 0)
85 				Debug = 1;
86 			break;
87 		default:
88 			printf("unknown flag %s\n", argv[1]); break;
89 		}
90 		--argc;  argv++;
91 	}
92 
93 	DEBUG(4, "DEBUG# %s\n", "START");
94 	if (chdir(Spool) < 0) {	/* NO subdirs in uuclean!  rti!trt */
95 		printf("%s directory inaccessible\n", Spool);
96 		exit(1);
97 	}
98 
99 	if ((dirp = opendir(Spool)) == NULL) {
100 		printf("%s directory unreadable\n", Spool);
101 		exit(1);
102 	}
103 
104 	time(&ptime);
105 	while (dentp = readdir(dirp)) {
106 		if (checkprefix && !chkpre(dentp->d_name))
107 			continue;
108 
109 		if (stat(dentp->d_name, &stbuf) == -1) {
110 			DEBUG(4, "stat on %s failed\n", dentp->d_name);
111 			continue;
112 		}
113 
114 
115 		if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
116 			continue;
117 		if ((ptime - stbuf.st_mtime) < nomtime)
118 			continue;
119 		if (dentp->d_name[0] == CMDPRE)
120 			notfyuser(dentp->d_name);
121 		DEBUG(4, "unlink file %s\n", dentp->d_name);
122 		unlink(dentp->d_name);
123 		if (mflg)
124 			sdmail(dentp->d_name, stbuf.st_uid);
125 	}
126 
127 	closedir(dirp);
128 	exit(0);
129 }
130 
131 
132 #define MAXPRE 10
133 char Pre[MAXPRE][NAMESIZE];
134 int Npre = 0;
135 /***
136  *	chkpre(file)	check for prefix
137  *	char *file;
138  *
139  *	return codes:
140  *		0  -  not prefix
141  *		1  -  is prefix
142  */
143 
chkpre(file)144 chkpre(file)
145 char *file;
146 {
147 	int i;
148 
149 	for (i = 0; i < Npre; i++) {
150 		if (prefix(Pre[i], file))
151 			return(1);
152 		}
153 	return(0);
154 }
155 
156 /***
157  *	stpre(p)	store prefix
158  *	char *p;
159  *
160  *	return codes:  none
161  */
162 
stpre(p)163 stpre(p)
164 char *p;
165 {
166 	if (Npre < MAXPRE - 2)
167 		strcpy(Pre[Npre++], p);
168 	return;
169 }
170 
171 /***
172  *	notfyuser(file)	- notfiy requestor of deleted requres
173  *
174  *	return code - none
175  */
176 
notfyuser(file)177 notfyuser(file)
178 char *file;
179 {
180 	FILE *fp;
181 	int numrq;
182 	char frqst[100], lrqst[100];
183 	char msg[BUFSIZ];
184 	char *args[10];
185 
186 	if ((fp = fopen(file, "r")) == NULL)
187 		return;
188 	if (fgets(frqst, 100, fp) == NULL) {
189 		fclose(fp);
190 		return;
191 	}
192 	numrq = 1;
193 	while (fgets(lrqst, 100, fp))
194 		numrq++;
195 	fclose(fp);
196 	sprintf(msg,
197 	  "File %s delete. \nCould not contact remote. \n%d requests deleted.\n", file, numrq);
198 	if (numrq == 1) {
199 		strcat(msg, "REQUEST: ");
200 		strcat(msg, frqst);
201 	} else {
202 		strcat(msg, "FIRST REQUEST: ");
203 		strcat(msg, frqst);
204 		strcat(msg, "\nLAST REQUEST: ");
205 		strcat(msg, lrqst);
206 	}
207 	getargs(frqst, args, 10);
208 	mailst(args[3], msg, CNULL);
209 }
210 
211 
212 /***
213  *	sdmail(file, uid)
214  *
215  *	sdmail  -  this routine will determine the owner
216  *	of the file (file), create a message string and
217  *	call "mailst" to send the cleanup message.
218  *	This is only implemented for local system
219  *	mail at this time.
220  */
221 
sdmail(file,uid)222 sdmail(file, uid)
223 char *file;
224 {
225 	static struct passwd *pwd;
226 	char mstr[40];
227 
228 	sprintf(mstr, "uuclean deleted file %s\n", file);
229 	if (pwd != NULL && pwd->pw_uid == uid) {
230 		mailst(pwd->pw_name, mstr, CNULL);
231 		return;
232 	}
233 
234 	setpwent();
235 	if ((pwd = getpwuid(uid)) != NULL)
236 		mailst(pwd->pw_name, mstr, CNULL);
237 }
238 
cleanup(code)239 cleanup(code)
240 int code;
241 {
242 	exit(code);
243 }
244