Mercurial > bday
annotate bday.c @ 19:eb4620a01023
updated the copyright note
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Mon, 24 Feb 2014 21:32:38 +0100 |
parents | c1cd1d444353 |
children |
rev | line source |
---|---|
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
1 /* |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
2 bday -- Birthday/Anniversary reminder |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
3 |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
4 (c) 2007,2014 markus schnalke <meillo@marmaro.de> |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
5 (c) 1994-1999 AS Mortimer |
8 | 6 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
7 This program is free software; you can redistribute it and/or |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
8 modify it under the terms of the GNU General Public License as |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
9 published by the Free Software Foundation; either version 2 of the |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
10 License, or (at your option) any later version. You may also |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
11 distribute it under the Artistic License, as comes with Perl. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
12 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
16 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
18 along with this program; if not, write to the Free Software |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
8 | 20 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
21 You should also have recieved a copy of the Artistic license with |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
22 this program. |
8 | 23 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
24 ===================================================================== |
9
4f48b4f86e3d
added COPYRIGHT and COPYING; adjusted copyright hints in the code
meillo@marmaro.de
parents:
8
diff
changeset
|
25 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
26 Input is read through standard input. For example: bday < ~/.birthdays |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
27 The input (file) has to have the following format: |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
28 |
16 | 29 date flags text |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
30 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
31 where: |
16 | 32 date is YYYY-MM-DD |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
33 flags is optionally |
16 | 34 #ann for an anniversary |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
35 and optionally |
16 | 36 #w<n> to set the warn-in-advance time to n days |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
37 (don't include the brackets! :) |
16 | 38 separated by spaces. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
39 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
40 Lines preceeded by # are treated as comments. |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
41 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
42 Note: If you deviate from this format, I cannot guarantee anything about |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
43 it's behaviour. In most cases, it will just quietly ignore the |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
44 error, which probably isn't ideal behaviour. Oh, well. |
8 | 45 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
46 ===================================================================== |
8 | 47 */ |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
48 |
7 | 49 |
8 | 50 /* standard time to warn in advance, when no explicit w flag is given. */ |
51 #define DEF_WARN 14 | |
7 | 52 |
53 | |
54 #include <stdarg.h> | |
55 #include <stdio.h> | |
56 #include <stdlib.h> | |
57 #include <string.h> | |
58 #include <sys/types.h> | |
59 #include <time.h> | |
60 #include <unistd.h> | |
61 | |
62 | |
63 | |
64 /* ========== Global constants and data types */ | |
65 | |
66 | |
67 /* -------- modifier flags */ | |
68 #define F_MTYPE 0x07 | |
69 #define F_TANNIVERSARY 2 | |
70 | |
71 /* flags processed immediately on encountering */ | |
72 #define F_MIMMEDIATE 0x24 | |
73 #define F_WTIME_P 0x08 | |
74 | |
16 | 75 struct _ftable { |
76 char* txt; | |
77 unsigned flag; | |
78 }; | |
7 | 79 const struct _ftable FTABLE[]; |
80 | |
81 struct date { | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
82 unsigned day; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
83 unsigned month; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
84 unsigned year; |
7 | 85 }; |
86 | |
87 struct event { | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
88 char* text; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
89 struct date date; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
90 int warn; |
7 | 91 }; |
92 | |
93 /* ========== Global Variables */ | |
94 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
95 struct event *readlist(void); |
7 | 96 void gettoday(void); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
97 unsigned delta(struct date *); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
98 unsigned ddiff(struct date *D1, struct date *D2); |
16 | 99 void liststrings(struct event *evl); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
100 char *tdelta(struct date *d); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
101 char *ttime(int yr, int mn, int wk, int dy); |
16 | 102 char *skptok(char *ptr); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
103 int evcmp(const void *e1, const void *e2); |
7 | 104 |
105 | |
106 struct date today; | |
16 | 107 int def_warn = DEF_WARN; |
7 | 108 |
109 | |
110 const struct _ftable FTABLE[] = { | |
16 | 111 {"#ann",F_TANNIVERSARY}, |
112 {"#w", F_WTIME_P}, | |
7 | 113 {NULL, 0} |
114 }; | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 /* | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
122 xmalloc/xrealloc functions |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
123 Note: the x* functions are lifted straight from the GNU libc info docs |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
124 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ |
7 | 125 */ |
126 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
127 void * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
128 xmalloc(size_t size) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
129 { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
130 register void *value = malloc (size); |
7 | 131 if (value == 0) { |
132 fprintf(stderr, "virtual memory exhausted\n"); | |
133 exit(1); | |
134 } | |
135 return value; | |
136 } | |
137 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
138 void * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
139 xrealloc(void *ptr, size_t size) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
140 { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
141 register void *value = realloc (ptr, size); |
7 | 142 if (value == 0) { |
143 fprintf(stderr, "virtual memory exhausted\n"); | |
144 exit(1); | |
145 } | |
146 return value; | |
147 } | |
148 | |
8 | 149 |
7 | 150 /* ========== */ |
151 | |
152 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
153 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
154 like strcat(), but lets the buffer automagically grow :-) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
155 */ |
16 | 156 int |
157 append(char *where, int size, char *what) | |
158 { | |
159 if (strlen(what) > ((size) - strlen(where))) { | |
160 xrealloc(where, (size) + 128 + strlen(what)); | |
161 size += 128 + strlen(what); | |
162 } | |
163 strcat(where, what); | |
164 return size; | |
165 } | |
7 | 166 |
167 /* ========== */ | |
168 | |
16 | 169 |
170 int | |
171 before(struct date a, struct date b) | |
172 { | |
173 if (a.month < b.month) { | |
174 return 1; | |
175 } else if (a.month == b.month && a.day < b.day) { | |
176 return 1; | |
177 } else { | |
178 return 0; | |
179 } | |
180 } | |
181 | |
182 int | |
183 ydelta(struct date a, struct date b) | |
184 { | |
185 return b.year - a.year + before(a, b); | |
186 } | |
187 | |
188 /* | |
189 returns the length of the given month | |
190 */ | |
191 int | |
192 mlen(int month, int year) | |
193 { | |
194 unsigned mlendat[] = {31,0,31,30,31,30,31,31,30,31,30,31}; | |
195 | |
196 if (mlendat[month - 1]) { | |
197 return mlendat[month - 1]; | |
198 } else { | |
199 if (year%4==0 && (year%100!=0 || year%400==0)) { | |
200 return 29; | |
201 } else { | |
202 return 28; | |
203 } | |
204 } | |
205 } | |
206 | |
207 | |
208 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
209 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
210 returns delta(d) in days, weeks, months, etc |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
211 the returned buffer is malloc()ed, do not forget to free() it |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
212 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
213 char * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
214 tdelta(struct date *d) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
215 { |
7 | 216 int dy, wk, mn, yr; |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
217 char *tmp; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
218 char *buf = xmalloc(128); |
7 | 219 int size = 128; |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
220 |
7 | 221 *buf = 0; |
222 switch (delta(d)) { | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
223 case 0: |
16 | 224 size = append(buf, size, "TODAY"); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
225 return buf; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
226 case 1: |
16 | 227 size = append(buf, size, "Tomorrow"); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
228 return buf; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
229 default: |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
230 /* like delta(), we ignore the year */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
231 yr = -before(*d, today); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
232 mn = d->month - today.month; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
233 dy = d->day - today.day; |
7 | 234 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
235 if (dy < 0) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
236 dy += mlen(today.month, today.year); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
237 mn--; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
238 } |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
239 if (mn < 0) { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
240 mn += 12; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
241 yr++; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
242 } |
7 | 243 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
244 wk = (dy / 7); |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
245 dy %= 7; |
7 | 246 |
16 | 247 size = append(buf, size, "In "); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
248 tmp = ttime(yr, mn, wk, dy); |
16 | 249 size = append(buf, size, tmp); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
250 free(tmp); |
7 | 251 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
252 return buf; |
7 | 253 } |
254 } | |
255 | |
256 | |
257 | |
258 | |
259 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
260 void |
16 | 261 donum(char *buf, int size, int n, char *txt, int *terms) |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
262 { |
16 | 263 char tmp[128]; |
264 | |
265 if (n <= 0) { | |
266 return; | |
267 } | |
268 snprintf(tmp, sizeof(tmp), "%d", n); | |
269 size = append(buf, size, tmp); | |
270 size = append(buf, size, " "); | |
271 size = append(buf, size, txt); | |
272 if (n != 1) { | |
273 size = append(buf, size, "s"); | |
274 } | |
275 if (--*terms == 1) { | |
276 size = append(buf, size, " and "); | |
277 } else if (*terms > 1) { | |
278 size = append(buf, size, ", "); | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
279 } |
7 | 280 } |
281 | |
282 | |
283 /* returns allocated buffer, don't forget to free() */ | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
284 char * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
285 ttime(int yr, int mn, int wk, int dy) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
286 { |
7 | 287 int size = 128; |
16 | 288 char *buf = xmalloc(size); |
289 int terms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0); | |
7 | 290 |
16 | 291 *buf = '\0'; /* Initialize buffer */ |
7 | 292 |
16 | 293 donum(buf, size, yr, "year", &terms); |
294 donum(buf, size, mn, "month", &terms); | |
295 donum(buf, size, wk, "week", &terms); | |
296 donum(buf, size, dy, "day", &terms); | |
7 | 297 |
298 return buf; | |
299 } | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
306 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
307 lists the birthdays in their string format, one by one, and passes |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
308 the string to a function. |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
309 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
310 void |
16 | 311 liststrings(struct event *evl) |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
312 { |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
313 int i; |
7 | 314 char *buf, *tmp; |
315 int size; | |
316 | |
16 | 317 for (i=0; evl[i].text; i++) { |
318 size = 128; | |
319 buf = xmalloc(size); | |
7 | 320 *buf = '\0'; |
321 | |
322 if (evl[i].warn == -1 && delta(&(evl[i].date))==0) { | |
16 | 323 size = append(buf, size, evl[i].text); |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
324 } else if (delta(&(evl[i].date)) <= evl[i].warn) { |
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
325 tmp = tdelta(&(evl[i].date)); |
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
326 size = append(buf, size, tmp); |
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
327 size = append(buf, size, ": "); |
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
328 size = append(buf, size, evl[i].text); |
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
329 free(tmp); |
7 | 330 } |
331 if (*buf) { | |
16 | 332 size = append(buf, size, "."); |
333 puts(buf); | |
7 | 334 } |
335 free(buf); | |
336 } | |
337 } | |
338 | |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
346 /* |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
347 sort the events by the time before the next time they come up |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
348 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
349 int |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
350 evcmp(const void *p1, const void *p2) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
351 { |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
352 struct event *e1=(struct event *) p1; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
353 struct event *e2=(struct event *) p2; |
8 | 354 unsigned d1, d2; |
7 | 355 |
356 d1=delta(&(e1->date)); | |
357 d2=delta(&(e2->date)); | |
358 | |
359 if (d1 < d2) return -1; | |
8 | 360 if (d1 > d2) return 1; |
7 | 361 return strcmp(e1->text, e2->text); |
362 } | |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
369 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
370 difference in days between two dates |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
371 it is assumed that D1 < D2, and so the result is always positive |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
372 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
373 unsigned |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
374 ddiff(struct date *D1, struct date *D2) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
375 { |
8 | 376 struct date d1, d2; |
377 int dd, m; | |
7 | 378 |
379 /* make working copies */ | |
8 | 380 d1 = *D1; |
381 d2 = *D2; | |
7 | 382 |
383 /* sort out zero years */ | |
384 if (d1.year == 0 || d2.year==0) { | |
385 if (d1.year != d2.year) { | |
386 if (d1.year == 0) { | |
8 | 387 if (before(d1,d2)) |
388 d1.year = d2.year; | |
389 else | |
390 d1.year = d2.year - 1; | |
391 } else { | |
392 if (before(d1, d2)) | |
393 d2.year = d1.year; | |
394 else | |
395 d2.year = d1.year + 1; | |
7 | 396 } |
397 } else { /* both years zero */ | |
8 | 398 if (before(d1, d2)) |
399 d1.year = d2.year = today.year; | |
7 | 400 else { |
8 | 401 d1.year = today.year; |
402 d2.year = d1.year + 1; | |
7 | 403 } |
404 } | |
405 } | |
406 | |
407 /* now we can actually do the comparison ... */ | |
8 | 408 dd = 0; |
7 | 409 |
410 /* to start with, we work in months */ | |
411 for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) | |
412 dd += mlen(((m-1)%12)+1, d1.year + m/12); | |
413 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
414 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
415 and then we renormalise for the days within the months |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
416 the first month was included in our calculations |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
417 */ |
7 | 418 dd -= d1.day; |
419 /* but the last one wasn't */ | |
420 dd += d2.day; | |
421 | |
422 return dd; | |
423 } | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
431 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
432 /* |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
433 actually until the next anniversary of ... |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
434 */ |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
435 unsigned |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
436 delta(struct date *date) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
437 { |
7 | 438 struct date d; |
439 unsigned dt, mn; | |
440 | |
441 memcpy(&d, date, sizeof(struct date)); | |
442 | |
443 /* past the end of the year */ | |
444 if (before(d, today)) { | |
445 d.year = 1; | |
446 } else { | |
447 d.year = 0; | |
448 } | |
449 | |
8 | 450 for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) { |
7 | 451 dt += mlen(((mn-1)%12) + 1,today.year + mn/12); |
8 | 452 } |
7 | 453 |
454 dt -= today.day; | |
455 dt += d.day; | |
456 | |
457 return dt; | |
458 } | |
459 | |
460 | |
461 | |
462 | |
463 | |
464 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
465 void |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
466 gettoday(void) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
467 { |
7 | 468 struct tm *tm; |
469 time_t t; | |
470 | |
471 time(&t); | |
472 tm = localtime(&t); | |
473 today.day = tm->tm_mday; | |
474 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ | |
475 today.year = tm->tm_year + 1900; | |
476 } | |
477 | |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
487 struct event * |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
488 readlist() |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
489 { |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
490 int i, j, k; |
7 | 491 struct event *evl; |
492 char buf[1024], buf2[1024]; | |
16 | 493 char *ptr, *cp; |
7 | 494 unsigned flags; |
495 | |
496 /* initialise */ | |
497 gettoday(); | |
498 | |
18
c1cd1d444353
removed the type event: bday is only for birthdays and anniversaries
markus schnalke <meillo@marmaro.de>
parents:
17
diff
changeset
|
499 for (i=0, evl=NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { |
7 | 500 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); |
501 | |
502 /* ignore comments and empty lines */ | |
503 if (*buf == '#' || *buf == '\n') { | |
504 i--; | |
505 continue; | |
506 } | |
507 | |
508 /* parse string in buf */ | |
16 | 509 |
510 ptr = strchr(buf, ' '); /* start of text */ | |
7 | 511 |
512 /* not a valid line, so ignore it! Cool, huh? */ | |
513 /* Attention: only recognizes lines without '=' */ | |
16 | 514 if (!ptr) { |
515 fprintf(stderr, "WARNING: Invalid input line:\n\t%s", buf); | |
7 | 516 i--; |
517 continue; | |
518 } | |
519 | |
16 | 520 *(ptr++) = '\0'; |
521 ptr[strlen(ptr)-1] = '\0'; | |
7 | 522 |
16 | 523 j = sscanf(buf, "%u-%u-%u", &(evl[i].date.year), |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
524 &(evl[i].date.month), &(evl[i].date.day)); |
16 | 525 if (j != 3) { |
526 fprintf(stderr, "Error: Invalid date:\t%s\n", buf); | |
527 i--; | |
528 continue; | |
7 | 529 } |
530 | |
531 /* parse flags */ | |
532 | |
16 | 533 evl[i].warn = def_warn; |
7 | 534 flags = 0; |
535 j = 0; | |
16 | 536 cp = skptok(ptr); |
537 for (cp=ptr; *cp && *cp=='#'; cp=skptok(cp)) { | |
538 for (k = 0; FTABLE[k].txt && strncmp(FTABLE[k].txt, cp, strlen(FTABLE[k].txt)); k++) { | |
7 | 539 } |
540 | |
541 switch (FTABLE[k].flag) { | |
16 | 542 case F_WTIME_P: /* #w<n> -- sets warning time */ |
543 sscanf(cp, "#w%u", &(evl[i].warn)); | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
544 break; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
545 case 0: |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
546 break; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
547 default: |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
548 flags |= FTABLE[k].flag; |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
549 break; |
7 | 550 } |
551 } | |
552 | |
553 | |
554 /* construct event text */ | |
555 | |
556 switch(flags & F_MTYPE) { | |
557 default: /* assume it's a birthday */ | |
16 | 558 if (!evl[i].date.year) { |
559 sprintf(buf2, "%s has a birthday", cp); | |
560 break; | |
7 | 561 } |
16 | 562 int tmp_age = ydelta(evl[i].date, today); |
563 sprintf(buf2, "%s is %d year%s old", | |
564 cp, tmp_age, (tmp_age>1)?"s":""); | |
7 | 565 break; |
566 case F_TANNIVERSARY: | |
16 | 567 if (evl[i].date.year) { |
568 sprintf(buf2, "%s %d years ago", | |
569 cp, ydelta(evl[i].date, today)); | |
7 | 570 } else { |
16 | 571 strcpy(buf2, cp); |
7 | 572 } |
573 break; | |
574 } | |
575 evl[i].text = strdup(buf2); | |
576 } | |
577 | |
578 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); | |
579 evl[i].date.day = 0; | |
580 evl[i].date.month = 0; | |
581 evl[i].date.year = 0; | |
582 evl[i].text = (char *) NULL; | |
583 | |
584 fclose(stdin); | |
585 | |
586 /* NB uses i from above */ | |
587 qsort(evl, i, sizeof(struct event), evcmp); | |
588 return evl; | |
589 } | |
590 | |
591 | |
592 | |
593 | |
594 | |
16 | 595 char * |
596 skptok(char *ptr) | |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
597 { |
16 | 598 while (*ptr && (*ptr!=' ' && *ptr!='\t')) { |
599 ptr++; | |
600 } | |
601 while (*ptr && (*ptr==' ' || *ptr=='\t')) { | |
602 ptr++; | |
603 } | |
604 return ptr; | |
7 | 605 } |
606 | |
607 | |
608 | |
609 | |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
610 |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
611 |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
612 int |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
613 main(int argc, char *argv[]) |
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
614 { |
16 | 615 while (--argc > 0 && **++argv == '-') { |
17
d18a3b2b76bd
updated man page; s/-W/-w/
markus schnalke <meillo@marmaro.de>
parents:
16
diff
changeset
|
616 if (strcmp(argv[0], "-w") == 0) { |
6 | 617 /* TODO: catch if no value given */ |
16 | 618 def_warn = atoi((++argv)[0]); |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
619 argc--; |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
620 } else { |
3
dc2f94280b01
new Makefile; removed MinWarn and MaxWarn; adjusted manpage
meillo@marmaro.de
parents:
0
diff
changeset
|
621 fprintf(stderr, "unknown option %s\n", argv[0]); |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
622 exit(1); |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
623 } |
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
624 } |
16 | 625 liststrings(readlist()); |
15
032af48d590b
reformating of the source code
markus schnalke <meillo@marmaro.de>
parents:
14
diff
changeset
|
626 return 0; |
0
22b6e71de68e
initial commit; codebase from birthday; just the needed stuff; substituted getopt by own code
meillo@marmaro.de
parents:
diff
changeset
|
627 } |