Repository navigation
Expand file tree
/
Copy pathcalendar.c
More file actions
236 lines (201 loc) · 5.09 KB
/
Copy pathcalendar.c
File metadata and controls
236 lines (201 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* calendar - reminder service Authors: S. & K. Hirabayashi */
/* Permission is hereby granted for nonprofit use. */
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#ifdef __NBSD_LIBC
#include <compat/regexp.h>
#else
#include <regexp.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <termcap.h>
#include <unistd.h>
#include <utime.h>
#include <stdio.h>
/* Change these two lines for your system needs. */
#define MAIL1 "/usr/bin/mail"
#define MAIL2 "/bin/mail"
#define PASSWD "/etc/passwd" /* system password file */
#define MAX_EXP 4 /* see date_exp() function */
char *mail; /* mail command path ("/bin/mail" etc) */
regexp *exp[MAX_EXP]; /* date expressions */
int nexp; /* # of the date expressions */
char calfile[PATH_MAX]; /* calendar file for the user */
int rflg; /* consult aged 'calendar' file and touch */
int mflg; /* mail (multi user) service */
char *cmd; /* the name of this command */
char buf[BUFSIZ];
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(void calendar, (void));
_PROTOTYPE(char *getstr, (char *s, int n));
_PROTOTYPE(int newaccess, (char *file));
_PROTOTYPE(void grep, (char *file, char *user));
_PROTOTYPE(int date_exp, (void));
_PROTOTYPE(char *date_pat, (time_t t));
/*
_PROTOTYPE(void regerror, (char *s));
*/
_PROTOTYPE(void error, (char *s, char *t));
int main(argc, argv)
int argc;
char **argv;
{
char *s;
cmd = *argv;
while (--argc > 0 && (*++argv)[0] == '-') {
s = argv[0] + 1;
if (*s == '\0')
mflg++; /* mail service */
else if (strcmp(s, "r") == 0)
rflg++, mflg++;
}
if (mflg) { /* check mailing agent */
if (access(MAIL1, X_OK) == 0)
mail = MAIL1;
else if (access(MAIL2, X_OK) == 0)
mail = MAIL2;
else
error("cannot find %s", MAIL1);
}
nexp = date_exp();
calendar();
exit(0);
}
void calendar()
{
int i;
char *s;
FILE *fp;
if (!mflg) {
grep("calendar", "");
return;
}
/* Mail sevice */
if ((fp = fopen(PASSWD, "r")) == (FILE *) NULL)
error("cannot open %s", PASSWD);
while (fgets(buf, BUFSIZ, fp) != (char *) NULL) {
for (i = 0, s = buf; *s && *s != '\n'; s++)
if (*s == ':') i++;
*s = '\0';
if (i != 6) error("illegal '/etc/passwd' format: %s", buf);
/* Calendar file = ${HOME}/calendar */
sprintf(calfile, "%s/%s", getstr(buf, 5), "calendar");
if ((access(calfile, R_OK) != 0) || (rflg && !newaccess(calfile)))
continue;
grep(calfile, getstr(buf, 0));
}
fclose(fp);
}
char *getstr(s, n)
char *s;
int n;
{
/* Returns the string value of the n-th field in the record (s) */
int i;
char *t;
static char str[512];
for (i = 0; i < n && *s; s++)
if (*s == ':') i++; /* field separator */
for (i = 0, t = str; *s && *s != ':' && i < 511; i++) *t++ = *s++;
*t = '\0';
return str;
}
int newaccess(file)
char *file; /* file name */
{
/* Check whether the file has been touched today. */
int r = 0;
struct tm *tm;
struct stat stbuf;
time_t clk;
char newdate[8], olddate[8];
time(&clk);
tm = localtime(&clk);
sprintf(newdate, "%02d%02d%02d", tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
if (stat(file, &stbuf) == -1) error("cannot stat %s", file);
tm = localtime(&stbuf.st_mtime);
sprintf(olddate, "%02d%02d%02d", tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
if (strcmp(newdate, olddate) != 0) {
utime(file, NULL); /* touch */
r++;
}
return r;
}
void grep(file, user)
char *file, *user;
{ /* grep 'exp[]' [| mail user] */
int i;
char command[128]; /* mail command */
FILE *ifp, *ofp;
if ((ifp = fopen(file, "r")) == (FILE *) NULL)
error("cannot open %s", file);
if (*user != '\0') {
sprintf(command, "%s %s", mail, user);
ofp = (FILE *) NULL;
} else {
ofp = stdout;
}
while (fgets(buf, BUFSIZ, ifp) != (char *) NULL) {
for (i = 0; i < nexp; i++) {
if (regexec(exp[i], buf, 1)) {
if ((ofp == (FILE *) NULL) &&
(ofp = popen(command, "w")) == (FILE *) NULL)
error("cannot popen %s", mail);
fputs(buf, ofp);
break;
}
}
}
fclose(ifp);
if (ofp == stdout)
fflush(ofp);
else if (ofp != (FILE *) NULL)
pclose(ofp);
}
int date_exp()
{
/* Set compiled regular expressions into the exp[] array. */
static int n[] = {2, 2, 2, 2, 2, 4, 3};
int i, r, wday;
time_t clk;
time(&clk);
wday = localtime(&clk)->tm_wday;
r = n[wday];
if (r > MAX_EXP) error("too many date expressions", "");
for (i = 0; i < r; i++) {
exp[i] = regcomp(date_pat(clk));
clk += 60 * 60 * 24L; /* 24 hours */
}
return(r);
}
char *date_pat(t)
time_t t;
{ /* returns date expression for the time (t) */
static char *month[] = {
"[Jj]an", "[Ff]eb", "[Mm]ar", "[Aa]pr", "[Mm]ay", "[Jj]un",
"[Jj]ul", "[Aa]ug", "[Ss]ep", "[Oo]ct", "[Nn]ov", "[Dd]ec"
};
static char str[512];
struct tm *tm;
tm = localtime(&t);
sprintf(str,
"(^|[ \t(,;])(((%s[^ \t]*[ \t])|0*%d/|\\*/)(0*%d|\\*))([^0123456789]|$)",
month[tm->tm_mon], tm->tm_mon + 1, tm->tm_mday);
return str;
}
void regerror(s)
const char *s;
{ /* regcomp() needs this */
error("REGULAR EXPRESSION ERROR (%s)", (char *) s);
}
void error(s, t)
char *s, *t;
{
fprintf(stderr, "%s: ", cmd);
fprintf(stderr, s, t);
fprintf(stderr, "\n");
exit(1);
}