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