new copyright; att/bsd/shared
[unix-history] / usr / src / usr.bin / uucp / uuclean / uuclean.c
CommitLineData
d34dfbc0
KB
1/*-
2 * Copyright (c) 1985 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.proprietary.c%
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1985 The Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
31a8699c 14#ifndef lint
d34dfbc0
KB
15static char sccsid[] = "@(#)uuclean.c 5.9 (Berkeley) %G%";
16#endif /* not lint */
31a8699c 17
31a8699c 18#include <signal.h>
2690157d 19#include "uucp.h"
31a8699c 20#include <pwd.h>
31a8699c
SL
21#include <sys/stat.h>
22#ifdef NDIR
23#include "ndir.h"
24#else
ed412ce0 25#include <sys/dir.h>
31a8699c
SL
26#endif
27
2690157d 28/*
31a8699c 29 *
2690157d 30 * this program will search through the spool
31a8699c
SL
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
31a8699c
SL
47#define NOMTIME 72 /* hours to age files before deletion */
48
46b15d8a 49int checkprefix = 0;
db77272c 50struct timeb Now;
46b15d8a 51
31a8699c
SL
52main(argc, argv)
53char *argv[];
54{
37c3f689
RA
55 register DIR *dirp;
56 register struct direct *dentp;
31a8699c
SL
57 time_t nomtime, ptime;
58 struct stat stbuf;
37c3f689 59 int mflg = 0;
31a8699c
SL
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':
46b15d8a 77 checkprefix = 1;
31a8699c
SL
78 if (&argv[1][2] != '\0')
79 stpre(&argv[1][2]);
80 break;
81 case 'x':
46b15d8a 82 chkdebug();
31a8699c
SL
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");
46b15d8a
RC
94 if (chdir(Spool) < 0) { /* NO subdirs in uuclean! rti!trt */
95 printf("%s directory inaccessible\n", Spool);
96 exit(1);
97 }
31a8699c
SL
98
99 if ((dirp = opendir(Spool)) == NULL) {
100 printf("%s directory unreadable\n", Spool);
101 exit(1);
102 }
103
104 time(&ptime);
37c3f689
RA
105 while (dentp = readdir(dirp)) {
106 if (checkprefix && !chkpre(dentp->d_name))
31a8699c
SL
107 continue;
108
37c3f689
RA
109 if (stat(dentp->d_name, &stbuf) == -1) {
110 DEBUG(4, "stat on %s failed\n", dentp->d_name);
31a8699c
SL
111 continue;
112 }
113
114
115 if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
116 continue;
31a8699c
SL
117 if ((ptime - stbuf.st_mtime) < nomtime)
118 continue;
37c3f689
RA
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);
46b15d8a 123 if (mflg)
37c3f689 124 sdmail(dentp->d_name, stbuf.st_uid);
31a8699c
SL
125 }
126
127 closedir(dirp);
128 exit(0);
129}
130
131
132#define MAXPRE 10
133char Pre[MAXPRE][NAMESIZE];
134int 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
144chkpre(file)
145char *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
163stpre(p)
164char *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
177notfyuser(file)
178char *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);
46b15d8a 201 } else {
31a8699c
SL
202 strcat(msg, "FIRST REQUEST: ");
203 strcat(msg, frqst);
204 strcat(msg, "\nLAST REQUEST: ");
205 strcat(msg, lrqst);
206 }
46b15d8a
RC
207 getargs(frqst, args, 10);
208 mailst(args[3], msg, CNULL);
31a8699c
SL
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
222sdmail(file, uid)
223char *file;
224{
225 static struct passwd *pwd;
31a8699c
SL
226 char mstr[40];
227
228 sprintf(mstr, "uuclean deleted file %s\n", file);
46b15d8a
RC
229 if (pwd != NULL && pwd->pw_uid == uid) {
230 mailst(pwd->pw_name, mstr, CNULL);
231 return;
31a8699c
SL
232 }
233
234 setpwent();
46b15d8a
RC
235 if ((pwd = getpwuid(uid)) != NULL)
236 mailst(pwd->pw_name, mstr, CNULL);
31a8699c
SL
237}
238
239cleanup(code)
240int code;
241{
242 exit(code);
243}