BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libc / string / strftime.c
index 3dc2d90..f50cd49 100644 (file)
@@ -2,26 +2,43 @@
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)strftime.c 5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)strftime.c 5.11 (Berkeley) 2/24/91";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #include <sys/time.h>
 #include <tzfile.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #include <sys/time.h>
 #include <tzfile.h>
+#include <string.h>
 
 static char *afmt[] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
 
 static char *afmt[] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
@@ -41,15 +58,16 @@ static char *Bfmt[] = {
 
 static size_t gsize;
 static char *pt;
 
 static size_t gsize;
 static char *pt;
+static int _add(), _conv(), _secs();
 
 size_t
 strftime(s, maxsize, format, t)
        char *s;
 
 size_t
 strftime(s, maxsize, format, t)
        char *s;
-       char *format;
        size_t maxsize;
        size_t maxsize;
-       struct tm *t;
+       const char *format;
+       const struct tm *t;
 {
 {
-       size_t _fmt();
+       static size_t _fmt();
 
        pt = s;
        if ((gsize = maxsize) < 1)
 
        pt = s;
        if ((gsize = maxsize) < 1)
@@ -66,30 +84,43 @@ _fmt(format, t)
        register char *format;
        struct tm *t;
 {
        register char *format;
        struct tm *t;
 {
-       char *timezone();
-
        for (; *format; ++format) {
                if (*format == '%')
                        switch(*++format) {
        for (; *format; ++format) {
                if (*format == '%')
                        switch(*++format) {
+                       case '\0':
+                               --format;
+                               break;
                        case 'A':
                        case 'A':
-                               if (!_add(Afmt[t->tm_mon]))
+                               if (t->tm_wday < 0 || t->tm_wday > 6)
+                                       return(0);
+                               if (!_add(Afmt[t->tm_wday]))
                                        return(0);
                                continue;
                        case 'a':
                                        return(0);
                                continue;
                        case 'a':
-                               if (!_add(afmt[t->tm_mon]))
+                               if (t->tm_wday < 0 || t->tm_wday > 6)
+                                       return(0);
+                               if (!_add(afmt[t->tm_wday]))
                                        return(0);
                                continue;
                        case 'B':
                                        return(0);
                                continue;
                        case 'B':
+                               if (t->tm_mon < 0 || t->tm_mon > 11)
+                                       return(0);
                                if (!_add(Bfmt[t->tm_mon]))
                                        return(0);
                                continue;
                        case 'b':
                        case 'h':
                                if (!_add(Bfmt[t->tm_mon]))
                                        return(0);
                                continue;
                        case 'b':
                        case 'h':
+                               if (t->tm_mon < 0 || t->tm_mon > 11)
+                                       return(0);
                                if (!_add(bfmt[t->tm_mon]))
                                        return(0);
                                continue;
                                if (!_add(bfmt[t->tm_mon]))
                                        return(0);
                                continue;
+                       case 'C':
+                               if (!_fmt("%a %b %e %H:%M:%S %Y", t))
+                                       return(0);
+                               continue;
                        case 'c':
                        case 'c':
-                               if (!_fmt("%x %X %Z %Y", t))
+                               if (!_fmt("%m/%d/%y %H:%M:%S", t))
                                        return(0);
                                continue;
                        case 'D':
                                        return(0);
                                continue;
                        case 'D':
@@ -97,27 +128,41 @@ _fmt(format, t)
                                        return(0);
                                continue;
                        case 'd':
                                        return(0);
                                continue;
                        case 'd':
-                               if (!_conv(t->tm_mday, 2))
+                               if (!_conv(t->tm_mday, 2, '0'))
+                                       return(0);
+                               continue;
+                       case 'e':
+                               if (!_conv(t->tm_mday, 2, ' '))
                                        return(0);
                                continue;
                        case 'H':
                                        return(0);
                                continue;
                        case 'H':
-                               if (!_conv(t->tm_hour, 2))
+                               if (!_conv(t->tm_hour, 2, '0'))
                                        return(0);
                                continue;
                        case 'I':
                                        return(0);
                                continue;
                        case 'I':
-                               if (!_conv((t->tm_hour - 1) % 12 + 1, 2))
+                               if (!_conv(t->tm_hour % 12 ?
+                                   t->tm_hour % 12 : 12, 2, '0'))
                                        return(0);
                                continue;
                        case 'j':
                                        return(0);
                                continue;
                        case 'j':
-                               if (!_conv(t->tm_yday + 1, 3))
+                               if (!_conv(t->tm_yday + 1, 3, '0'))
+                                       return(0);
+                               continue;
+                       case 'k':
+                               if (!_conv(t->tm_hour, 2, ' '))
+                                       return(0);
+                               continue;
+                       case 'l':
+                               if (!_conv(t->tm_hour % 12 ?
+                                   t->tm_hour % 12 : 12, 2, ' '))
                                        return(0);
                                continue;
                        case 'M':
                                        return(0);
                                continue;
                        case 'M':
-                               if (!_conv(t->tm_min, 2))
+                               if (!_conv(t->tm_min, 2, '0'))
                                        return(0);
                                continue;
                        case 'm':
                                        return(0);
                                continue;
                        case 'm':
-                               if (!_conv(t->tm_mon + 1, 2))
+                               if (!_conv(t->tm_mon + 1, 2, '0'))
                                        return(0);
                                continue;
                        case 'n':
                                        return(0);
                                continue;
                        case 'n':
@@ -125,7 +170,7 @@ _fmt(format, t)
                                        return(0);
                                continue;
                        case 'p':
                                        return(0);
                                continue;
                        case 'p':
-                               if (!_add(t->tm_hour >= 12 ? "AM" : "PM"))
+                               if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
                                        return(0);
                                continue;
                        case 'R':
                                        return(0);
                                continue;
                        case 'R':
@@ -137,7 +182,11 @@ _fmt(format, t)
                                        return(0);
                                continue;
                        case 'S':
                                        return(0);
                                continue;
                        case 'S':
-                               if (!_conv(t->tm_sec, 2))
+                               if (!_conv(t->tm_sec, 2, '0'))
+                                       return(0);
+                               continue;
+                       case 's':
+                               if (!_secs(t))
                                        return(0);
                                continue;
                        case 'T':
                                        return(0);
                                continue;
                        case 'T':
@@ -151,40 +200,41 @@ _fmt(format, t)
                                continue;
                        case 'U':
                                if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
                                continue;
                        case 'U':
                                if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
-                                   2))
+                                   2, '0'))
                                        return(0);
                                continue;
                        case 'W':
                                if (!_conv((t->tm_yday + 7 -
                                        return(0);
                                continue;
                        case 'W':
                                if (!_conv((t->tm_yday + 7 -
-                                   (t->tm_wday ? t->tm_wday : 6)) / 7, 2))
+                                   (t->tm_wday ? (t->tm_wday - 1) : 6))
+                                   / 7, 2, '0'))
                                        return(0);
                                continue;
                        case 'w':
                                        return(0);
                                continue;
                        case 'w':
-                               if (!_conv(t->tm_wday, 1))
+                               if (!_conv(t->tm_wday, 1, '0'))
                                        return(0);
                                continue;
                        case 'x':
                                        return(0);
                                continue;
                        case 'x':
-                               if (!_fmt("%a %b %d", t))
+                               if (!_fmt("%m/%d/%y", t))
                                        return(0);
                                continue;
                        case 'y':
                                if (!_conv((t->tm_year + TM_YEAR_BASE)
                                        return(0);
                                continue;
                        case 'y':
                                if (!_conv((t->tm_year + TM_YEAR_BASE)
-                                   % 100, 2))
+                                   % 100, 2, '0'))
                                        return(0);
                                continue;
                        case 'Y':
                                        return(0);
                                continue;
                        case 'Y':
-                               if (!_conv(t->tm_year + TM_YEAR_BASE, 4))
+                               if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
                                        return(0);
                                continue;
                        case 'Z':
                                        return(0);
                                continue;
                        case 'Z':
-                               if (!_add(t->tm_zone))
+                               if (!t->tm_zone || !_add(t->tm_zone))
                                        return(0);
                                continue;
                        case '%':
                        /*
                         * X311J/88-090 (4.12.3.5): if conversion char is
                         * undefined, behavior is undefined.  Print out the
                                        return(0);
                                continue;
                        case '%':
                        /*
                         * X311J/88-090 (4.12.3.5): if conversion char is
                         * undefined, behavior is undefined.  Print out the
-                        * character itself as printf(3) also does.
+                        * character itself as printf(3) does.
                         */
                        default:
                                break;
                         */
                        default:
                                break;
@@ -197,8 +247,26 @@ _fmt(format, t)
 }
 
 static
 }
 
 static
-_conv(n, digits)
+_secs(t)
+       struct tm *t;
+{
+       static char buf[15];
+       register time_t s;
+       register char *p;
+       struct tm tmp;
+
+       /* Make a copy, mktime(3) modifies the tm struct. */
+       tmp = *t;
+       s = mktime(&tmp);
+       for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
+               *p-- = s % 10 + '0';
+       return(_add(++p));
+}
+
+static
+_conv(n, digits, pad)
        int n, digits;
        int n, digits;
+       char pad;
 {
        static char buf[10];
        register char *p;
 {
        static char buf[10];
        register char *p;
@@ -206,7 +274,7 @@ _conv(n, digits)
        for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
                *p-- = n % 10 + '0';
        while (p > buf && digits-- > 0)
        for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
                *p-- = n % 10 + '0';
        while (p > buf && digits-- > 0)
-               *p-- = '0';
+               *p-- = pad;
        return(_add(++p));
 }
 
        return(_add(++p));
 }