don't open target for writing if we're just verifying the tapes
[unix-history] / usr / src / usr.bin / uucp / uuclean / uuclean.c
CommitLineData
31a8699c 1#ifndef lint
37c3f689 2static char sccsid[] = "@(#)uuclean.c 5.7 %G%";
31a8699c
SL
3#endif
4
31a8699c 5#include <signal.h>
2690157d 6#include "uucp.h"
31a8699c 7#include <pwd.h>
31a8699c
SL
8#include <sys/stat.h>
9#ifdef NDIR
10#include "ndir.h"
11#else
ed412ce0 12#include <sys/dir.h>
31a8699c
SL
13#endif
14
2690157d 15/*
31a8699c 16 *
2690157d 17 * this program will search through the spool
31a8699c
SL
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
31a8699c
SL
34#define NOMTIME 72 /* hours to age files before deletion */
35
46b15d8a 36int checkprefix = 0;
db77272c 37struct timeb Now;
46b15d8a 38
31a8699c
SL
39main(argc, argv)
40char *argv[];
41{
37c3f689
RA
42 register DIR *dirp;
43 register struct direct *dentp;
31a8699c
SL
44 time_t nomtime, ptime;
45 struct stat stbuf;
37c3f689 46 int mflg = 0;
31a8699c
SL
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':
46b15d8a 64 checkprefix = 1;
31a8699c
SL
65 if (&argv[1][2] != '\0')
66 stpre(&argv[1][2]);
67 break;
68 case 'x':
46b15d8a 69 chkdebug();
31a8699c
SL
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");
46b15d8a
RC
81 if (chdir(Spool) < 0) { /* NO subdirs in uuclean! rti!trt */
82 printf("%s directory inaccessible\n", Spool);
83 exit(1);
84 }
31a8699c
SL
85
86 if ((dirp = opendir(Spool)) == NULL) {
87 printf("%s directory unreadable\n", Spool);
88 exit(1);
89 }
90
91 time(&ptime);
37c3f689
RA
92 while (dentp = readdir(dirp)) {
93 if (checkprefix && !chkpre(dentp->d_name))
31a8699c
SL
94 continue;
95
37c3f689
RA
96 if (stat(dentp->d_name, &stbuf) == -1) {
97 DEBUG(4, "stat on %s failed\n", dentp->d_name);
31a8699c
SL
98 continue;
99 }
100
101
102 if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
103 continue;
31a8699c
SL
104 if ((ptime - stbuf.st_mtime) < nomtime)
105 continue;
37c3f689
RA
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);
46b15d8a 110 if (mflg)
37c3f689 111 sdmail(dentp->d_name, stbuf.st_uid);
31a8699c
SL
112 }
113
114 closedir(dirp);
115 exit(0);
116}
117
118
119#define MAXPRE 10
120char Pre[MAXPRE][NAMESIZE];
121int 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
131chkpre(file)
132char *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
150stpre(p)
151char *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
164notfyuser(file)
165char *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);
46b15d8a 188 } else {
31a8699c
SL
189 strcat(msg, "FIRST REQUEST: ");
190 strcat(msg, frqst);
191 strcat(msg, "\nLAST REQUEST: ");
192 strcat(msg, lrqst);
193 }
46b15d8a
RC
194 getargs(frqst, args, 10);
195 mailst(args[3], msg, CNULL);
31a8699c
SL
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
209sdmail(file, uid)
210char *file;
211{
212 static struct passwd *pwd;
213 struct passwd *getpwuid();
214 char mstr[40];
215
216 sprintf(mstr, "uuclean deleted file %s\n", file);
46b15d8a
RC
217 if (pwd != NULL && pwd->pw_uid == uid) {
218 mailst(pwd->pw_name, mstr, CNULL);
219 return;
31a8699c
SL
220 }
221
222 setpwent();
46b15d8a
RC
223 if ((pwd = getpwuid(uid)) != NULL)
224 mailst(pwd->pw_name, mstr, CNULL);
31a8699c
SL
225}
226
227cleanup(code)
228int code;
229{
230 exit(code);
231}