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