Merge from vendor branch OPENSSH:
[dragonfly.git] / bin / date / vary.c
1 /*-
2  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/bin/date/vary.c,v 1.16 2004/08/09 13:43:39 yar Exp $
27  * $DragonFly: src/bin/date/vary.c,v 1.4 2005/07/20 06:10:51 cpressey Exp $
28  */
29
30 #include <err.h>
31 #include <time.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include "vary.h"
35
36 struct trans {
37   int val;
38   const char *str;
39 };
40
41 static struct trans trans_mon[] = {
42   { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" },
43   { 5, "may"}, { 6, "june" }, { 7, "july" }, { 8, "august" },
44   { 9, "september" }, { 10, "october" }, { 11, "november" }, { 12, "december" },
45   { -1, NULL }
46 };
47
48 static struct trans trans_wday[] = {
49   { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" },
50   { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" },
51   { -1, NULL }
52 };
53
54 static char digits[] = "0123456789";
55 static int adjhour(struct tm *, char, int, int);
56
57 static int
58 domktime(struct tm *t, char type)
59 {
60   time_t ret;
61
62   while ((ret = mktime(t)) == -1 && t->tm_year > 68 && t->tm_year < 138)
63     /* While mktime() fails, adjust by an hour */
64     adjhour(t, type == '-' ? type : '+', 1, 0);
65
66   return ret;
67 }
68
69 static int
70 trans(const struct trans t[], const char *arg)
71 {
72   int f;
73
74   for (f = 0; t[f].val != -1; f++)
75     if (!strncasecmp(t[f].str, arg, 3) ||
76         !strncasecmp(t[f].str, arg, strlen(t[f].str)))
77       return t[f].val;
78
79   return -1;
80 }
81
82 struct vary *
83 vary_append(struct vary *v, char *arg)
84 {
85   struct vary *result, **nextp;
86
87   if (v) {
88     result = v;
89     while (v->next)
90       v = v->next;
91     nextp = &v->next;
92   } else
93     nextp = &result;
94
95   if ((*nextp = (struct vary *)malloc(sizeof(struct vary))) == NULL)
96     err(1, "malloc");
97   (*nextp)->arg = arg;
98   (*nextp)->next = NULL;
99   return result;
100 }
101
102 static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
103
104 static int
105 daysinmonth(const struct tm *t)
106 {
107   int year;
108
109   year = t->tm_year + 1900;
110
111   if (t->tm_mon == 1)
112     if (!(year % 400))
113       return 29;
114     else if (!(year % 100))
115       return 28;
116     else if (!(year % 4))
117       return 29;
118     else
119       return 28;
120   else if (t->tm_mon >= 0 && t->tm_mon < 12)
121     return mdays[t->tm_mon];
122
123   return 0;
124 }
125
126
127 static int
128 adjyear(struct tm *t, char type, int val, int mk)
129 {
130   switch (type) {
131     case '+':
132       t->tm_year += val;
133       break;
134     case '-':
135       t->tm_year -= val;
136       break;
137     default:
138       t->tm_year = val;
139       if (t->tm_year < 69)
140         t->tm_year += 100;              /* as per date.c */
141       else if (t->tm_year > 1900)
142         t->tm_year -= 1900;             /* struct tm holds years since 1900 */
143       break;
144   }
145   return !mk || domktime(t, type) != -1;
146 }
147
148 static int
149 adjmon(struct tm *t, char type, int val, int istext, int mk)
150 {
151   int lmdays;
152   if (val < 0)
153     return 0;
154
155   switch (type) {
156     case '+':
157       if (istext) {
158         if (val <= t->tm_mon)
159           val += 11 - t->tm_mon;        /* early next year */
160         else
161           val -= t->tm_mon + 1;         /* later this year */
162       }
163       if (val) {
164         if (!adjyear(t, '+', (t->tm_mon + val) / 12, 0))
165           return 0;
166         val %= 12;
167         t->tm_mon += val;
168         if (t->tm_mon > 11)
169           t->tm_mon -= 12;
170       }
171       break;
172
173     case '-':
174       if (istext) {
175         if (val-1 > t->tm_mon)
176           val = 13 - val + t->tm_mon;   /* later last year */
177         else
178           val = t->tm_mon - val + 1;    /* early this year */
179       }
180       if (val) {
181         if (!adjyear(t, '-', val / 12, 0))
182           return 0;
183         val %= 12;
184         if (val > t->tm_mon) {
185           if (!adjyear(t, '-', 1, 0))
186             return 0;
187           val -= 12;
188         }
189         t->tm_mon -= val;
190       }
191       break;
192
193     default:
194       if (val > 12 || val < 1)
195         return 0;
196       t->tm_mon = --val;
197   }
198
199   /* e.g., -v-1m on March, 31 is the last day of February in common sense */
200   lmdays = daysinmonth(t);
201   if (t->tm_mday > lmdays)
202     t->tm_mday = lmdays;
203
204   return !mk || domktime(t, type) != -1;
205 }
206
207 static int
208 adjday(struct tm *t, char type, int val, int mk)
209 {
210   int daycount;
211
212   switch (type) {
213     case '+':
214       while (val) {
215         daycount = daysinmonth(t);
216         if (val > daycount - t->tm_mday) {
217           val -= daycount - t->tm_mday + 1;
218           t->tm_mday = 1;
219           if (!adjmon(t, '+', 1, 0, 0))
220             return 0;
221         } else {
222           t->tm_mday += val;
223           val = 0;
224         }
225       }
226       break;
227     case '-':
228       while (val)
229         if (val >= t->tm_mday) {
230           val -= t->tm_mday;
231           t->tm_mday = 1;
232           if (!adjmon(t, '-', 1, 0, 0))
233             return 0;
234           t->tm_mday = daysinmonth(t);
235         } else {
236           t->tm_mday -= val;
237           val = 0;
238         }
239       break;
240     default:
241       if (val > 0 && val <= daysinmonth(t))
242         t->tm_mday = val;
243       else
244         return 0;
245       break;
246   }
247
248   return !mk || domktime(t, type) != -1;
249 }
250
251 static int
252 adjwday(struct tm *t, char type, int val, int istext, int mk)
253 {
254   if (val < 0)
255     return 0;
256
257   switch (type) {
258     case '+':
259       if (istext)
260         if (val < t->tm_wday)
261           val = 7 - t->tm_wday + val;  /* early next week */
262         else
263           val -= t->tm_wday;           /* later this week */
264       else
265         val *= 7;                      /* "-v+5w" == "5 weeks in the future" */
266       return !val || adjday(t, '+', val, mk);
267     case '-':
268       if (istext) {
269         if (val > t->tm_wday)
270           val = 7 - val + t->tm_wday;  /* later last week */
271         else
272           val = t->tm_wday - val;      /* early this week */
273       } else
274         val *= 7;                      /* "-v-5w" == "5 weeks ago" */
275       return !val || adjday(t, '-', val, mk);
276     default:
277       if (val < t->tm_wday)
278         return adjday(t, '-', t->tm_wday - val, mk);
279       else if (val > 6)
280         return 0;
281       else if (val > t->tm_wday)
282         return adjday(t, '+', val - t->tm_wday, mk);
283   }
284   return 1;
285 }
286
287 static int
288 adjhour(struct tm *t, char type, int val, int mk)
289 {
290   if (val < 0)
291     return 0;
292
293   switch (type) {
294     case '+':
295       if (val) {
296         int days;
297
298         days = (t->tm_hour + val) / 24;
299         val %= 24;
300         t->tm_hour += val;
301         t->tm_hour %= 24;
302         if (!adjday(t, '+', days, 0))
303           return 0;
304       }
305       break;
306
307     case '-':
308       if (val) {
309         int days;
310
311         days = val / 24;
312         val %= 24;
313         if (val > t->tm_hour) {
314           days++;
315           val -= 24;
316         }
317         t->tm_hour -= val;
318         if (!adjday(t, '-', days, 0))
319           return 0;
320       }
321       break;
322
323     default:
324       if (val > 23)
325         return 0;
326       t->tm_hour = val;
327   }
328
329   return !mk || domktime(t, type) != -1;
330 }
331
332 static int
333 adjmin(struct tm *t, char type, int val, int mk)
334 {
335   if (val < 0)
336     return 0;
337
338   switch (type) {
339     case '+':
340       if (val) {
341         if (!adjhour(t, '+', (t->tm_min + val) / 60, 0))
342           return 0;
343         val %= 60;
344         t->tm_min += val;
345         if (t->tm_min > 59)
346           t->tm_min -= 60;
347       }
348       break;
349
350     case '-':
351       if (val) {
352         if (!adjhour(t, '-', val / 60, 0))
353           return 0;
354         val %= 60;
355         if (val > t->tm_min) {
356           if (!adjhour(t, '-', 1, 0))
357             return 0;
358           val -= 60;
359         }
360         t->tm_min -= val;
361       }
362       break;
363
364     default:
365       if (val > 59)
366         return 0;
367       t->tm_min = val;
368   }
369
370   return !mk || domktime(t, type) != -1;
371 }
372
373 static int
374 adjsec(struct tm *t, char type, int val, int mk)
375 {
376   if (val < 0)
377     return 0;
378
379   switch (type) {
380     case '+':
381       if (val) {
382         if (!adjmin(t, '+', (t->tm_sec + val) / 60, 0))
383           return 0;
384         val %= 60;
385         t->tm_sec += val;
386         if (t->tm_sec > 59)
387           t->tm_sec -= 60;
388       }
389       break;
390
391     case '-':
392       if (val) {
393         if (!adjmin(t, '-', val / 60, 0))
394           return 0;
395         val %= 60;
396         if (val > t->tm_sec) {
397           if (!adjmin(t, '-', 1, 0))
398             return 0;
399           val -= 60;
400         }
401         t->tm_sec -= val;
402       }
403       break;
404
405     default:
406       if (val > 59)
407         return 0;
408       t->tm_sec = val;
409   }
410
411   return !mk || domktime(t, type) != -1;
412 }
413
414 const struct vary *
415 vary_apply(const struct vary *v, struct tm *t)
416 {
417   char type;
418   char which;
419   char *arg;
420   size_t len;
421   int val;
422
423   for (; v; v = v->next) {
424     type = *v->arg;
425     arg = v->arg;
426     if (type == '+' || type == '-')
427       arg++;
428     else
429       type = '\0';
430     len = strlen(arg);
431     if (len < 2)
432       return v;
433
434     if (type == '\0')
435       t->tm_isdst = -1;
436
437     if (strspn(arg, digits) != len-1) {
438       val = trans(trans_wday, arg);
439       if (val != -1) {
440           if (!adjwday(t, type, val, 1, 1))
441             return v;
442       } else {
443         val = trans(trans_mon, arg);
444         if (val != -1) {
445           if (!adjmon(t, type, val, 1, 1))
446             return v;
447         } else
448           return v;
449       }
450     } else {
451       val = atoi(arg);
452       which = arg[len-1];
453       
454       switch (which) {
455         case 'S':
456           if (!adjsec(t, type, val, 1))
457             return v;
458           break;
459         case 'M':
460           if (!adjmin(t, type, val, 1))
461             return v;
462           break;
463         case 'H':
464           if (!adjhour(t, type, val, 1))
465             return v;
466           break;
467         case 'd':
468           t->tm_isdst = -1;
469           if (!adjday(t, type, val, 1))
470             return v;
471           break;
472         case 'w':
473           t->tm_isdst = -1;
474           if (!adjwday(t, type, val, 0, 1))
475             return v;
476           break;
477         case 'm':
478           t->tm_isdst = -1;
479           if (!adjmon(t, type, val, 0, 1))
480             return v;
481           break;
482         case 'y':
483           t->tm_isdst = -1;
484           if (!adjyear(t, type, val, 1))
485             return v;
486           break;
487         default:
488           return v;
489       }
490     }
491   }
492   return 0;
493 }
494
495 void
496 vary_destroy(struct vary *v)
497 {
498   struct vary *n;
499
500   while (v) {
501     n = v->next;
502     free(v);
503     v = n;
504   }
505 }