BSD 4_1c_2 release
[unix-history] / usr / src / usr.bin / uucp / uuclean.c
CommitLineData
e804469b
C
1static char sccsid[] = "@(#)uuclean.c 4.1 (Berkeley) 9/11/82";
2
fea9ca7b
C
3#include "uucp.h"
4#include "uucpdefs.h"
5#include <signal.h>
6#include <pwd.h>
e804469b 7#include <sys/param.h>
fea9ca7b 8#include <sys/stat.h>
e804469b 9#include <dir.h>
fea9ca7b
C
10
11/*******
12 *
13 * uuclean - this program will search through the spool
14 * directory (Spool) and delete all files with a requested
15 * prefix which are older than (nomtime) seconds.
16 * If the -m option is set, the program will try to
17 * send mail to the usid of the file.
18 *
19 * options:
20 * -m - send mail for deleted file
21 * -d - directory to clean
22 * -n - time to age files before delete (in hours)
23 * -p - prefix for search
24 * -x - turn on debug outputs
25 * exit status:
26 * 0 - normal return
27 * 1 - can not read directory
28 */
29
30#define DPREFIX "U"
31#define NOMTIME 72 /* hours to age files before deletion */
32
33main(argc, argv)
34char *argv[];
35{
e804469b
C
36 DIR *pdirf;
37 register struct direct *dirp;
fea9ca7b
C
38 time_t nomtime, ptime;
39 struct stat stbuf;
40 int mflg=0;
41 extern int onintr();
42
43 nomtime = NOMTIME * 3600L;
44
45 while (argc>1 && argv[1][0] == '-') {
46 switch (argv[1][1]) {
47 case 'd':
48 Spool = &argv[1][2];
49 break;
50 case 'm':
51 mflg = 1;
52 break;
53 case 'n':
54 nomtime = atoi(&argv[1][2]) * 3600L;
55 break;
56 case 'p':
57 if (&argv[1][2] != '\0')
58 stpre(&argv[1][2]);
59 break;
60 case 'x':
61 Debug = atoi(&argv[1][2]);
62 if (Debug <= 0)
63 Debug = 1;
64 break;
65 default:
66 printf("unknown flag %s\n", argv[1]); break;
67 }
68 --argc; argv++;
69 }
70
71 DEBUG(4, "DEBUG# %s\n", "START");
72 chdir(Spool);
73
e804469b 74 if ((pdirf = opendir(Spool, "r")) == NULL) {
fea9ca7b
C
75 printf("%s directory unreadable\n", Spool);
76 exit(1);
77 }
78
79 time(&ptime);
e804469b
C
80 while ((dirp = readdir(pdirf)) != NULL) {
81 if (!chkpre(dirp->d_name))
fea9ca7b
C
82 continue;
83
e804469b
C
84 if (stat(dirp->d_name, &stbuf) == -1) {
85 DEBUG(4, "stat on %s failed\n", dirp->d_name);
fea9ca7b
C
86 continue;
87 }
88
89
90 if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
91 continue;
92 if ((ptime - stbuf.st_ctime) < nomtime)
93 continue;
e804469b
C
94 DEBUG(4, "unlink file %s\n", dirp->d_name);
95 unlink(dirp->d_name);
96 if (mflg) sdmail(dirp->d_name, stbuf.st_uid);
fea9ca7b
C
97 }
98
e804469b 99 closedir(pdirf);
fea9ca7b
C
100 exit(0);
101}
102
103
104#define MAXPRE 10
e804469b 105char Pre[MAXPRE][MAXNAMLEN];
fea9ca7b
C
106int Npre = 0;
107/***
108 * chkpre(file) check for prefix
109 * char *file;
110 *
111 * return codes:
112 * 0 - not prefix
113 * 1 - is prefix
114 */
115
116chkpre(file)
117char *file;
118{
119 int i;
120
121 for (i = 0; i < Npre; i++) {
122 if (prefix(Pre[i], file))
123 return(1);
124 }
125 return(0);
126}
127
128/***
129 * stpre(p) store prefix
130 * char *p;
131 *
132 * return codes: none
133 */
134
135stpre(p)
136char *p;
137{
138 if (Npre < MAXPRE - 2)
139 strcpy(Pre[Npre++], p);
140 return;
141}