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