]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/rdsched.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / common / rdsched.c
index 2131de51ab3ae60d01828be552c572ea5f2ffabd..7a02357d0f1430581144d56d13a765ec6d925dcb 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  rdsched.c: Read update schedule
- * 
+ *
  *  Known contributors to this file:
- *     Markus Armbruster, 2007
+ *     Markus Armbruster, 2007-2008
  */
 
 #define _XOPEN_SOURCE 500
 
 #include <config.h>
 
-#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
@@ -57,7 +56,7 @@ static int delete_update(time_t, time_t[], int);
  * Read update schedule from file FNAME.
  * Put the first N-1 updates after T0 into SCHED[] in ascending order,
  * terminated with a zero.
- * Use ANCHOR as initial anchor for anchor-relative times. 
+ * Use ANCHOR as initial anchor for anchor-relative times.
  * Return 0 on success, -1 on failure.
  */
 int
@@ -170,9 +169,17 @@ static char *
 parse_time(time_t *t, char *s, time_t *anchor)
 {
     static char *fmt[] = {
+       /*
+        * Absolute time formats
+        * Must set tm_year, tm_mon, tm_mday, tm_hour, tm_min.
+        */
        "%Y-%m-%d %H:%M ",      /* ISO 8601 */
        "%b %d %H:%M %Y ",      /* like ctime(): Dec 22 15:35 2006 */
-       "%d %b %Y %H:%M ",      /* 22 Dec 2006 15:35 */
+       "%d %b %Y %H:%M ",      /* like RFC 2822: 22 Dec 2006 15:35 */
+       /*
+        * Relative to anchor formats
+        * Must set tm_wday, may set tm_hour and tm_min
+        */
        "next %a %H:%M ",       /* next Fri 15:35 */
        "next %a ",             /* next Fri */
        NULL
@@ -186,14 +193,30 @@ parse_time(time_t *t, char *s, time_t *anchor)
     for (i = 0; ; i++) {
        if (!fmt[i])
            return NULL;
+       /*
+        * Carefully initialize tm so we can tell what strptime()
+        * actually set.
+        *
+        * Beware: some losing implementations of strptime() happily
+        * succeed when they fully consumed the first argument,
+        * regardless of whether they matched the full second argument
+        * or not.  Observed on FreeBSD 6.2.
+        */
        memset(&tm, 0, sizeof(tm));
-       tm.tm_hour = -1;
+       tm.tm_hour = -1;        /* to recognize anchor-relat. w/o time */
+       tm.tm_year = INT_MIN;   /* strptime() lossage work-around */
+       tm.tm_mon = tm.tm_mday = tm.tm_min = tm.tm_wday = -1; /* ditto */
        endp = strptime(p, fmt[i], &tm);
-       if (endp)
+       if (endp
+           /* strptime() lossage work-around: */
+           && ((tm.tm_year != INT_MIN && tm.tm_mon != -1
+                && tm.tm_mday != -1 && tm.tm_hour != -1 && tm.tm_min != -1)
+               || (tm.tm_wday != -1
+                   && (tm.tm_hour == -1 || tm.tm_min != -1))))
            break;
     }
 
-    if (tm.tm_mday == 0) {
+    if (tm.tm_mday == -1) {
        /* relative to anchor */
        nexttm = *localtime(anchor);
        if (tm.tm_hour >= 0) {
@@ -230,7 +253,8 @@ parse_every(time_t *secs, char *s)
     else
        sscanf(s, " every %u minutes%n", &delta, &nch);
     if (nch < 0)
-       return NULL;    *secs = 60 * delta;
+       return NULL;
+    *secs = 60 * delta;
     return s + nch;
 }
 
@@ -315,7 +339,6 @@ delete_update(time_t t, time_t sched[], int n)
 {
     int i = find_update(t, sched);
     if (t == sched[i])
-       memmove(sched + i, sched + i + 1,
-               (n - 1 - i) * sizeof(*sched));
+       memmove(sched + i, sched + i + 1, (n - 1 - i) * sizeof(*sched));
     return i;
 }