[9] | 1 | /* calendar - reminder service Authors: S. & K. Hirabayashi */
|
---|
| 2 |
|
---|
| 3 | /* Permission is hereby granted for nonprofit use. */
|
---|
| 4 |
|
---|
| 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>
|
---|
| 16 |
|
---|
| 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 */
|
---|
| 22 |
|
---|
| 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 */
|
---|
| 27 |
|
---|
| 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];
|
---|
| 32 |
|
---|
| 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));
|
---|
| 40 | /*
|
---|
| 41 | _PROTOTYPE(void regerror, (char *s));
|
---|
| 42 | */
|
---|
| 43 | _PROTOTYPE(void error, (char *s, char *t));
|
---|
| 44 |
|
---|
| 45 | int main(argc, argv)
|
---|
| 46 | int argc;
|
---|
| 47 | char **argv;
|
---|
| 48 | {
|
---|
| 49 | char *s;
|
---|
| 50 |
|
---|
| 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++;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 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);
|
---|
| 67 | }
|
---|
| 68 | nexp = date_exp();
|
---|
| 69 | calendar();
|
---|
| 70 | exit(0);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void calendar()
|
---|
| 74 | {
|
---|
| 75 | int i;
|
---|
| 76 | char *s;
|
---|
| 77 | FILE *fp;
|
---|
| 78 |
|
---|
| 79 | if (!mflg) {
|
---|
| 80 | grep("calendar", "");
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /* Mail sevice */
|
---|
| 85 | if ((fp = fopen(PASSWD, "r")) == (FILE *) NULL)
|
---|
| 86 | error("cannot open %s", PASSWD);
|
---|
| 87 |
|
---|
| 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);
|
---|
| 93 |
|
---|
| 94 | /* Calendar file = ${HOME}/calendar */
|
---|
| 95 | sprintf(calfile, "%s/%s", getstr(buf, 5), "calendar");
|
---|
| 96 |
|
---|
| 97 | if ((access(calfile, R_OK) != 0) || (rflg && !newaccess(calfile)))
|
---|
| 98 | continue;
|
---|
| 99 |
|
---|
| 100 | grep(calfile, getstr(buf, 0));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | fclose(fp);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | char *getstr(s, n)
|
---|
| 107 | char *s;
|
---|
| 108 | int n;
|
---|
| 109 | {
|
---|
| 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];
|
---|
| 114 |
|
---|
| 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;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | int newaccess(file)
|
---|
| 123 | char *file; /* file name */
|
---|
| 124 | {
|
---|
| 125 | /* Check whether the file has been touched today. */
|
---|
| 126 |
|
---|
| 127 | int r = 0;
|
---|
| 128 | struct tm *tm;
|
---|
| 129 | struct stat stbuf;
|
---|
| 130 | time_t clk;
|
---|
| 131 | char newdate[8], olddate[8];
|
---|
| 132 |
|
---|
| 133 | time(&clk);
|
---|
| 134 | tm = localtime(&clk);
|
---|
| 135 | sprintf(newdate, "%02d%02d%02d", tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
|
---|
| 136 |
|
---|
| 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);
|
---|
| 140 |
|
---|
| 141 | if (strcmp(newdate, olddate) != 0) {
|
---|
| 142 | utime(file, NULL); /* touch */
|
---|
| 143 | r++;
|
---|
| 144 | }
|
---|
| 145 | return r;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 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;
|
---|
| 154 |
|
---|
| 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;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 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;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | fclose(ifp);
|
---|
| 177 | if (ofp == stdout)
|
---|
| 178 | fflush(ofp);
|
---|
| 179 | else if (ofp != (FILE *) NULL)
|
---|
| 180 | pclose(ofp);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | int date_exp()
|
---|
| 184 | {
|
---|
| 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;
|
---|
| 189 |
|
---|
| 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 */
|
---|
| 197 | }
|
---|
| 198 | return(r);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 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"
|
---|
| 207 | };
|
---|
| 208 | static char str[512];
|
---|
| 209 | struct tm *tm;
|
---|
| 210 |
|
---|
| 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);
|
---|
| 215 |
|
---|
| 216 | return str;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void regerror(s)
|
---|
| 220 | const char *s;
|
---|
| 221 | { /* regcomp() needs this */
|
---|
| 222 | error("REGULAR EXPRESSION ERROR (%s)", (char *) s);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void error(s, t)
|
---|
| 226 | char *s, *t;
|
---|
| 227 | {
|
---|
| 228 | fprintf(stderr, "%s: ", cmd);
|
---|
| 229 | fprintf(stderr, s, t);
|
---|
| 230 | fprintf(stderr, "\n");
|
---|
| 231 | exit(1);
|
---|
| 232 | }
|
---|