]> git.saurik.com Git - apple/libc.git/blobdiff - stdtime/getdate.c
Libc-594.9.5.tar.gz
[apple/libc.git] / stdtime / getdate.c
index 84a7b2803ceca90344dc757176bb23a57315331d..7e3bc79fff1c95eb224a5d5a9abf5b2aaef1cf75 100644 (file)
@@ -3,8 +3,6 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
- * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
@@ -26,6 +24,7 @@
 #include <stdio.h>
 #include <time.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -50,7 +49,7 @@ enum {
 #define        TM_YEAR_SET     0x04
 #define        UNDEFINED       -1
 
-static struct tm tmundef = {
+static const struct tm tmundef = {
     UNDEFINED,
     UNDEFINED,
     UNDEFINED,
@@ -71,10 +70,11 @@ int getdate_err;
 struct tm *
 getdate(const char *str)
 {
-    static struct tm tm, *now, *result = NULL;
+    static struct tm tm;
+    struct tm *now, *result = NULL;
     time_t t;
     FILE *fp;
-    int bufsiz, offset, len, dateset, timeset, saveerrno, wday_set;
+    int bufsiz, offset, len, dateset, timeset, saveerrno, wday_set, save_mon;
     char *buf;
     struct stat st;
     char *file = getenv(DATEMSK);
@@ -220,7 +220,7 @@ getdate(const char *str)
                case TM_YEAR_SET:
                case TM_YEAR_SET | TM_MON_SET:
                    if(!(dateset & TM_MON_SET))
-                       tm.tm_mon = 1;
+                       tm.tm_mon = 0;
                    tm.tm_mday = 1;
                    break;
 
@@ -234,10 +234,13 @@ getdate(const char *str)
                tm.tm_wday = now->tm_wday;
                tm.tm_gmtoff = now->tm_gmtoff;  /* XXX: can't grok timezones */
                tm.tm_isdst = -1;
-
+               save_mon = tm.tm_mon;
                if(mktime(&tm) == (time_t)-1) {
                    getdate_err = INVALID_DATE;
                    break;
+               } else if ((dateset & TM_MON_SET) && (tm.tm_mon != save_mon)) { /* Did mktime fixup an overflow date? */
+                       getdate_err = INVALID_DATE;
+                       break;
                }
                if(wday_set != UNDEFINED) {
                    int delta = wday_set - tm.tm_wday;
@@ -254,6 +257,7 @@ getdate(const char *str)
                    }
                }
                result = &tm;
+               break;
            }
        } while(0);