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