]> mj.ucw.cz Git - misc.git/blob - diary.c
Merge branch 'master' of git+ssh://git.ucw.cz/home/mj/GIT/misc
[misc.git] / diary.c
1 /*
2  *      Simple Diary (aka Yet Another Reminder Program)
3  *
4  *      (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <sys/time.h>
11
12 static FILE *f;
13
14 struct entry {
15   struct entry *next;
16   char x[1];
17 };
18
19 struct entry *fe, **le = &fe;
20
21 static void
22 scan(time_t when, int cnt)
23 {
24   struct tm *tm = localtime(&when);
25   char p[10], xb[256];
26   struct entry *e;
27
28   sprintf(p, "%02d-%02d-%02d", tm->tm_mday, tm->tm_mon+1, tm->tm_year % 100);
29   for(e=fe; e; e=e->next)
30     {
31       char *x, *y;
32       x = e->x;
33       y = p;
34       while (*x && *y)
35         {
36           if (*x != '?' && *x != *y)
37             goto xx;
38           x++, y++;
39         }
40       if (!*x)
41         continue;
42       if (*x > ' ')
43         {
44           if (*x - 'A' < cnt)
45             continue;
46           x++;
47         }
48       strcpy(xb, x);
49       y = strchr(xb, '(');
50       if (y && y[1] && y[2] && y[3] == ')')
51         {
52           int q;
53           sscanf(y+1, "%d", &q);
54           sprintf(y+1, "%02d", tm->tm_year - q);
55           y[3] = ')';
56         }
57       printf("%s:%s\n", p, xb);
58     xx:
59     }
60 }
61
62 int
63 main(int argc, char **argv)
64 {
65   char buf[256];
66   int m, cnt;
67   time_t now = time(NULL);
68
69   if (argc < 2 || argc > 3)
70     {
71       fprintf(stderr, "Usage: diary <file> [<days>]\n");
72       return 1;
73     }
74   f = fopen(argv[1], "r");
75   if (!f)
76     {
77       perror("fopen");
78       return 1;
79     }
80   while (fgets(buf, 256, f))
81     {
82       char *b = strchr(buf, '\n');
83       struct entry *e;
84
85       if (!b)
86         break;
87       *b = 0;
88       if (buf[0] <= ' ' || buf[0] == '#')
89         continue;
90       e = malloc(sizeof(struct entry) + strlen(buf));
91       *le = e;
92       le = &e->next;
93       strcpy(e->x, buf);
94     }
95   *le = NULL;
96   m = (argc > 2) ? atol(argv[2]) : 1;
97   cnt = 0;
98   while (m--)
99     {
100       scan(now, cnt);
101       now += 86401;                     /* Dirty trick... */
102       cnt++;
103     }
104   return 0;
105 }