replace the BSD implementation by one of our own coding

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7348 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-11-12 15:31:14 +00:00
parent 0d3fc31121
commit 1daa01306f

View File

@@ -1,301 +1,385 @@
/* /*
* Copyright (c) 1989, 1993 * Copyright (c) 1999 Kungliga Tekniska H<>gskolan
* The Regents of the University of California. All rights reserved. * (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * 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 * 1. Redistributions of source code must retain the above copyright
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * notice, this list of conditions and the following disclaimer.
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * 2. Redistributions in binary form must reproduce the above copyright
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * notice, this list of conditions and the following disclaimer in the
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * documentation and/or other materials provided with the distribution.
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * 3. Neither the name of KTH nor the names of its contributors may be
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * used to endorse or promote products derived from this software without
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * specific prior written permission.
* SUCH DAMAGE. *
*/ * THIS SOFTWARE IS PROVIDED BY KTH AND ITS 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 KTH OR ITS 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. */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#ifdef HAVE_SYS_TYPES_H #include "roken.h"
#include <sys/types.h>
#endif
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#elif defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#else
#include <time.h>
#endif
#define TM_YEAR_BASE 1900 /* from <tzfile.h> */
#include <string.h>
static char *afmt[] = { RCSID("$Id$");
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
}; static const char *abb_weekdays[] = {
static char *Afmt[] = { "Sun",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Mon",
"Saturday", "Tue",
}; "Wed",
static char *bfmt[] = { "Thu",
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Fri",
"Oct", "Nov", "Dec", "Sat",
};
static char *Bfmt[] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December",
}; };
static size_t gsize; static const char *full_weekdays[] = {
static char *pt; "Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
};
static int _add (char *); static const char *abb_month[] = {
static int _conv (int, int, int); "Jan",
#ifdef HAVE_MKTIME "Feb",
static int _secs (const struct tm *); "Mar",
#endif /* HAVE_MKTIME */ "Apr",
static size_t _fmt (const char *, const struct tm *); "May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
static const char *full_month[] = {
"January",
"February",
"Mars",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
static const char *ampm[] = {
"AM",
"PM"
};
/*
* Convert hour in [0, 24] to [12 1 - 11 12 1 - 11 12]
*/
static int
hour_24to12 (int hour)
{
int ret = hour % 12;
if (ret == 0)
ret = 12;
return ret;
}
/*
* Return AM or PM for `hour'
*/
static const char *
hour_to_ampm (int hour)
{
return ampm[hour / 12];
}
/*
* Return the week number of `tm' (Sunday being the first day of the week)
* as [0, 53]
*/
static int
week_number_sun (const struct tm *tm)
{
return (tm->tm_yday + 7 - (tm->tm_yday % 7 - tm->tm_wday + 7) % 7) / 7;
}
/*
* Return the week number of `tm' (Monday being the first day of the week)
* as [0, 53]
*/
static int
week_number_mon (const struct tm *tm)
{
int wday = (tm->tm_wday + 6) % 7;
return (tm->tm_yday + 7 - (tm->tm_yday % 7 - wday + 7) % 7) / 7;
}
/*
* Return the week number of `tm' (Monday being the first day of the
* week) as [01, 53]. Week number one is the one that has four or more
* days in that year.
*/
static int
week_number_mon4 (const struct tm *tm)
{
int wday = (tm->tm_wday + 6) % 7;
int w1day = (wday - tm->tm_yday % 7 + 7) % 7;
int ret;
ret = (tm->tm_yday + w1day) / 7;
if (w1day >= 4)
--ret;
if (ret == -1)
ret = 53;
else
++ret;
return ret;
}
/*
*
*/
size_t size_t
strftime(char *s, size_t maxsize, const char *format, const struct tm *t) strftime (char *buf, size_t maxsize, const char *format,
const struct tm *tm)
{ {
size_t n = 0;
size_t ret;
pt = s; while (*format != '\0' && n < maxsize) {
if ((gsize = maxsize) < 1) if (*format == '%') {
return(0); ++format;
if (_fmt(format, t)) { if(*format == 'E' || *format == 'O')
*pt = '\0'; ++format;
return(maxsize - gsize); switch (*format) {
} case 'a' :
return(0); ret = snprintf (buf, maxsize - n,
} "%s", abb_weekdays[tm->tm_wday]);
break;
static size_t case 'A' :
_fmt(const char *format, const struct tm *t) ret = snprintf (buf, maxsize - n,
{ "%s", full_weekdays[tm->tm_wday]);
for (; *format; ++format) { break;
if (*format == '%') case 'h' :
switch(*++format) { case 'b' :
case '\0': ret = snprintf (buf, maxsize - n,
--format; "%s", abb_month[tm->tm_mon]);
break; break;
case 'A': case 'B' :
if (t->tm_wday < 0 || t->tm_wday > 6) ret = snprintf (buf, maxsize - n,
return(0); "%s", full_month[tm->tm_mon]);
if (!_add(Afmt[t->tm_wday])) break;
return(0); case 'c' :
continue; ret = snprintf (buf, maxsize - n,
case 'a': "%d:%02d:%02d %02d:%02d:%02d",
if (t->tm_wday < 0 || t->tm_wday > 6) tm->tm_year,
return(0); tm->tm_mon + 1,
if (!_add(afmt[t->tm_wday])) tm->tm_mday,
return(0); tm->tm_hour,
continue; tm->tm_min,
case 'B': tm->tm_sec);
if (t->tm_mon < 0 || t->tm_mon > 11) break;
return(0); case 'C' :
if (!_add(Bfmt[t->tm_mon])) ret = snprintf (buf, maxsize - n,
return(0); "%02d", (tm->tm_year + 1900) / 100);
continue; break;
case 'b': case 'd' :
case 'h': ret = snprintf (buf, maxsize - n,
if (t->tm_mon < 0 || t->tm_mon > 11) "%02d", tm->tm_mday);
return(0); break;
if (!_add(bfmt[t->tm_mon])) case 'D' :
return(0); ret = snprintf (buf, maxsize - n,
continue; "%02d/%02d/%02d",
case 'C': tm->tm_mon + 1,
if (!_fmt("%a %b %e %H:%M:%S %Y", t)) tm->tm_mday,
return(0); (tm->tm_year + 1900) % 100);
continue; break;
case 'c': case 'e' :
if (!_fmt("%m/%d/%y %H:%M:%S", t)) ret = snprintf (buf, maxsize - n,
return(0); "%2d", tm->tm_mday);
continue; break;
case 'D': case 'F':
if (!_fmt("%m/%d/%y", t)) ret = snprintf (buf, maxsize - n,
return(0); "%04d-%02d-%02d", tm->tm_year + 1900,
continue; tm->tm_mon + 1, tm->tm_mday);
case 'd': break;
if (!_conv(t->tm_mday, 2, '0')) case 'g':
return(0); /* last two digits of week-based year */
continue; abort();
case 'e': case 'G':
if (!_conv(t->tm_mday, 2, ' ')) /* week-based year */
return(0); abort();
continue; case 'H' :
case 'H': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_hour, 2, '0')) "%02d", tm->tm_hour);
return(0); break;
continue; case 'I' :
case 'I': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_hour % 12 ? "%02d",
t->tm_hour % 12 : 12, 2, '0')) hour_24to12 (tm->tm_hour));
return(0); break;
continue; case 'j' :
case 'j': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_yday + 1, 3, '0')) "%03d", tm->tm_yday + 1);
return(0); break;
continue; case 'k' :
case 'k': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_hour, 2, ' ')) "%2d", tm->tm_hour);
return(0); break;
continue; case 'l' :
case 'l': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_hour % 12 ? "%2d",
t->tm_hour % 12 : 12, 2, ' ')) hour_24to12 (tm->tm_hour));
return(0); break;
continue; case 'm' :
case 'M': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_min, 2, '0')) "%02d", tm->tm_mon + 1);
return(0); break;
continue; case 'M' :
case 'm': ret = snprintf (buf, maxsize - n,
if (!_conv(t->tm_mon + 1, 2, '0')) "%02d", tm->tm_min);
return(0); break;
continue; case 'n' :
case 'n': ret = snprintf (buf, maxsize - n, "\n");
if (!_add("\n")) break;
return(0); case 'p' :
continue; ret = snprintf (buf, maxsize - n, "%s",
case 'p': hour_to_ampm (tm->tm_hour));
if (!_add(t->tm_hour >= 12 ? "PM" : "AM")) break;
return(0); case 'r' :
continue; ret = snprintf (buf, maxsize - n,
case 'R': "%02d:%02d:%02d %s",
if (!_fmt("%H:%M", t)) hour_24to12 (tm->tm_hour),
return(0); tm->tm_min,
continue; tm->tm_sec,
case 'r': hour_to_ampm (tm->tm_hour));
if (!_fmt("%I:%M:%S %p", t)) break;
return(0); case 'R' :
continue; ret = snprintf (buf, maxsize - n,
case 'S': "%02d:%02d",
if (!_conv(t->tm_sec, 2, '0')) tm->tm_hour,
return(0); tm->tm_min);
continue;
#ifdef HAVE_MKTIME case 's' :
case 's': ret = snprintf (buf, maxsize - n,
if (!_secs(t)) "%d", (int)mktime((struct tm *)tm));
return(0); break;
continue; case 'S' :
#endif /* HAVE_MKTIME */ ret = snprintf (buf, maxsize - n,
case 'T': "%02d", tm->tm_sec);
case 'X': break;
if (!_fmt("%H:%M:%S", t)) case 't' :
return(0); ret = snprintf (buf, maxsize - n, "\t");
continue; break;
case 't': case 'T' :
if (!_add("\t")) case 'X' :
return(0); ret = snprintf (buf, maxsize - n,
continue; "%02d:%02d:%02d",
case 'U': tm->tm_hour,
if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7, tm->tm_min,
2, '0')) tm->tm_sec);
return(0); break;
continue; case 'u' :
case 'W': ret = snprintf (buf, maxsize - n,
if (!_conv((t->tm_yday + 7 - "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
(t->tm_wday ? (t->tm_wday - 1) : 6)) break;
/ 7, 2, '0')) case 'U' :
return(0); ret = snprintf (buf, maxsize - n,
continue; "%02d", week_number_sun (tm));
case 'w': break;
if (!_conv(t->tm_wday, 1, '0')) case 'V' :
return(0); ret = snprintf (buf, maxsize - n,
continue; "%02d", week_number_mon4 (tm));
case 'x': break;
if (!_fmt("%m/%d/%y", t)) case 'w' :
return(0); ret = snprintf (buf, maxsize - n,
continue; "%d", tm->tm_wday);
case 'y': break;
if (!_conv((t->tm_year + TM_YEAR_BASE) case 'W' :
% 100, 2, '0')) ret = snprintf (buf, maxsize - n,
return(0); "%02d", week_number_mon (tm));
continue; break;
case 'Y': case 'x' :
if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0')) ret = snprintf (buf, maxsize - n,
return(0); "%d:%02d:%02d",
continue; tm->tm_year,
#ifdef notdef tm->tm_mon + 1,
case 'Z': tm->tm_mday);
if (!t->tm_zone || !_add(t->tm_zone)) break;
return(0); case 'y' :
continue; ret = snprintf (buf, maxsize - n,
#endif "%02d", (tm->tm_year + 1900) % 100);
case '%': break;
/* case 'Y' :
* X311J/88-090 (4.12.3.5): if conversion char is ret = snprintf (buf, maxsize - n,
* undefined, behavior is undefined. Print out the "%d", tm->tm_year + 1900);
* character itself as printf(3) does. break;
*/ case 'z':
default: ret = snprintf (buf, maxsize - n,
break; "%ld",
} #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
if (!gsize--) (long)tm->tm_gmtoff
return(0); #elif defined(HAVE_TIMEZONE)
*pt++ = *format; (long)timezone
} #else
return(gsize); #error Where in timezone chaos are you?
} #endif
);
#ifdef HAVE_MKTIME break;
static int case 'Z' :
_secs(const struct tm *t) ret = snprintf (buf, maxsize - n,
{ "%s", tm->tm_zone);
static char buf[15]; break;
time_t s; case '\0' :
char *p; --format;
struct tm tmp; /* FALLTHROUGH */
case '%' :
/* Make a copy, mktime(3) modifies the tm struct. */ ret = snprintf (buf, maxsize - n,
tmp = *t; "%%");
s = mktime(&tmp); break;
for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10) default :
*p-- = s % 10 + '0'; ret = snprintf (buf, maxsize - n,
return(_add(++p)); "%%%c", *format);
} break;
#endif /* HAVE_MKTIME */ }
if (ret >= maxsize - n)
static int return 0;
_conv(int n, int digits, int pad) n += ret;
{ buf += ret;
static char buf[10]; ++format;
char *p; } else {
*buf++ = *format++;
for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits) ++n;
*p-- = n % 10 + '0';
while (p > buf && digits-- > 0)
*p-- = pad;
return(_add(++p));
}
static int
_add(str)
char *str;
{
for (;; ++pt, --gsize) {
if (!gsize)
return(0);
if (!(*pt = *str++))
return(1);
} }
}
*buf++ = '\0';
return n;
} }