gonzui


Format: Advanced Search

t2ex/bsd_source/lib/libc/src_bsd/time/asctime.cbare sourcepermlink (0.03 seconds)

Search this content:

    1: /*      $OpenBSD: asctime.c,v 1.16 2010/08/23 22:35:34 millert Exp $ */
    2: /*
    3: ** This file is in the public domain, so clarified as of
    4: ** 1996-06-05 by Arthur David Olson.
    5: */
    6: 
    7: /*
    8: ** Avoid the temptation to punt entirely to strftime;
    9: ** the output of strftime is supposed to be locale specific
   10: ** whereas the output of asctime is supposed to be constant.
   11: */
   12: 
   13: /*LINTLIBRARY*/
   14: 
   15: //#include "private.h"
   16: //#include "tzfile.h"
   17: //#include "thread_private.h"
   18: #include <time.h>
   19: #include <stdio.h>
   20: #include <string.h>
   21: extern size_t strlcpy(char *, const char *, size_t);
   22: 
   23: /*
   24: ** Some systems only handle "%.2d"; others only handle "%02d";
   25: ** "%02.2d" makes (most) everybody happy.
   26: ** At least some versions of gcc warn about the %02.2d;
   27: ** we conditionalize below to avoid the warning.
   28: */
   29: /*
   30: ** All years associated with 32-bit time_t values are exactly four digits long;
   31: ** some years associated with 64-bit time_t values are not.
   32: ** Vintage programs are coded for years that are always four digits long
   33: ** and may assume that the newline always lands in the same place.
   34: ** For years that are less than four digits, we pad the output with
   35: ** leading zeroes to get the newline in the traditional place.
   36: ** The -4 ensures that we get four characters of output even if
   37: ** we call a strftime variant that produces fewer characters for some years.
   38: ** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
   39: ** but many implementations pad anyway; most likely the standards are buggy.
   40: */
   41: #ifdef __GNUC__
   42: #define ASCTIME_FMT     "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
   43: #else /* !defined __GNUC__ */
   44: #define ASCTIME_FMT     "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
   45: #endif /* !defined __GNUC__ */
   46: /*
   47: ** For years that are more than four digits we put extra spaces before the year
   48: ** so that code trying to overwrite the newline won't end up overwriting
   49: ** a digit within a year and truncating the year (operating on the assumption
   50: ** that no output is better than wrong output).
   51: */
   52: #ifdef __GNUC__
   53: #define ASCTIME_FMT_B   "%.3s %.3s%3d %2.2d:%2.2d:%2.2d     %s\n"
   54: #else /* !defined __GNUC__ */
   55: #define ASCTIME_FMT_B   "%.3s %.3s%3d %02.2d:%02.2d:%02.2d     %s\n"
   56: #endif /* !defined __GNUC__ */
   57: 
   58: #define STD_ASCTIME_BUF_SIZE    26
   59: /*
   60: ** Big enough for something such as
   61: ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648     -2147483648\n
   62: ** (two three-character abbreviations, five strings denoting integers,
   63: ** seven explicit spaces, two explicit colons, a newline,
   64: ** and a trailing ASCII nul).
   65: ** The values above are for systems where an int is 32 bits and are provided
   66: ** as an example; the define below calculates the maximum for the system at
   67: ** hand.
   68: */
   69: #define MAX_ASCTIME_BUF_SIZE    (2*3+5* /*INT_STRLEN_MAXIMUM(int)*/ 11+7+2+1+1)
   70: 
   71: static char *
   72: asctime3(timeptr, buf, bufsize)
   73: register const struct tm *      timeptr;
   74: char *                          buf;
   75: int                             bufsize;
   76: {
   77:         static const char      wday_name[][4] = {
   78:                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
   79:         };
   80:         static const char      mon_name[][4] = {
   81:                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
   82:                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
   83:         };
   84:         register const char *  wn;
   85:         register const char *  mn;
   86:         char                   year[/*INT_STRLEN_MAXIMUM(int)*/11 + 2];
   87:         int                    len;
   88: 
   89:         if (timeptr == NULL) {
   90:                 /*errno = EINVAL*/;
   91:                 strlcpy(buf, "??? ??? ?? ??:??:?? ????\n", bufsize);
   92:                 return buf;
   93:         }
   94:         if (timeptr->tm_wday < 0 || timeptr->tm_wday >= /*DAYSPERWEEK*/7)
   95:                 wn = "???";
   96:         else   wn = wday_name[timeptr->tm_wday];
   97:         if (timeptr->tm_mon < 0 || timeptr->tm_mon >= /*MONSPERYEAR*/12)
   98:                 mn = "???";
   99:         else   mn = mon_name[timeptr->tm_mon];
  100:         /*
  101:         ** Use strftime's %Y to generate the year, to avoid overflow problems
  102:         ** when computing timeptr->tm_year + TM_YEAR_BASE.
  103:         ** Assume that strftime is unaffected by other out-of-range members
  104:         ** (e.g., timeptr->tm_mday) when processing "%Y".
  105:         */
  106:         (void) strftime(year, sizeof year, "%Y", timeptr);
  107:         len = snprintf(buf, bufsize,
  108:                 ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
  109:                 wn, mn,
  110:                 timeptr->tm_mday, timeptr->tm_hour,
  111:                 timeptr->tm_min, timeptr->tm_sec,
  112:                 year);
  113:         if (len != -1 && len < bufsize) {
  114:                 return buf;
  115:         } else {
  116: #ifdef EOVERFLOW
  117:                 /*errno = EOVERFLOW*/;
  118: #else /* !defined EOVERFLOW */
  119:                 /*errno = EINVAL*/;
  120: #endif /* !defined EOVERFLOW */
  121:                 return NULL;
  122:         }
  123: }
  124: 
  125: /*
  126: ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
  127: */
  128: 
  129: char *
  130: asctime_r(timeptr, buf)
  131: register const struct tm *      timeptr;
  132: char *                          buf;
  133: {
  134:         /*
  135:         ** P1003 8.3.5.2 says that asctime_r() can only assume at most
  136:         ** a 26 byte buffer.
  137:         */
  138:         return asctime3(timeptr, buf, STD_ASCTIME_BUF_SIZE);
  139: }
  140: 
  141: /*
  142: ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
  143: */
  144: