home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / string / strftime.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  6KB  |  256 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)strftime.c    5.8 (Berkeley) 6/1/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. /*
  25.  * NOTE: I used the older version of this file, since the 4.3bsd-net2
  26.  *     version uses mktime() from ctime.c, and ctime.c is badly suited
  27.  *     for inclusion in a shared library (too much static stuff...)
  28.  */
  29.  
  30.  
  31. #define _KERNEL
  32. #include "ixemul.h"
  33.  
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #include <time.h>
  37. #include <tzfile.h>
  38. #include <string.h>
  39.  
  40. static char *afmt[] = {
  41.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  42. };
  43. static char *Afmt[] = {
  44.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  45.     "Saturday",
  46. };
  47. static char *bfmt[] = {
  48.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  49.     "Oct", "Nov", "Dec",
  50. };
  51. static char *Bfmt[] = {
  52.     "January", "February", "March", "April", "May", "June", "July",
  53.     "August", "September", "October", "November", "December",
  54. };
  55.  
  56. static size_t _fmt(const char *, const struct tm *);
  57. static int _conv (int, int, char);
  58. static int _add (char *);
  59.  
  60. size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
  61. {
  62.     size_t res = 0;
  63.  
  64.     u.u_pt = s;
  65.     if ((u.u_gsize = maxsize) < 1)
  66.           return 0;
  67.     if (_fmt(format, t)) {
  68.         *u.u_pt = '\0';
  69.         res = maxsize - u.u_gsize;
  70.     }
  71.     return res;
  72. }
  73.  
  74. static size_t _fmt(const char *format, const struct tm *t)
  75. {
  76.     for (; *format; ++format) {
  77.         if (*format == '%')
  78.             switch (*++format) {
  79.             case '\0':
  80.                 --format;
  81.                 break;
  82.             case 'A':
  83.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  84.                     return(0);
  85.                 if (!_add(Afmt[t->tm_wday]))
  86.                     return(0);
  87.                 continue;
  88.             case 'a':
  89.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  90.                     return(0);
  91.                 if (!_add(afmt[t->tm_wday]))
  92.                     return(0);
  93.                 continue;
  94.             case 'B':
  95.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  96.                     return(0);
  97.                 if (!_add(Bfmt[t->tm_mon]))
  98.                     return(0);
  99.                 continue;
  100.             case 'b':
  101.             case 'h':
  102.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  103.                     return(0);
  104.                 if (!_add(bfmt[t->tm_mon]))
  105.                     return(0);
  106.                 continue;
  107.             case 'C':
  108.                 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  109.                     return(0);
  110.                 continue;
  111.             case 'c':
  112.                 if (!_fmt("%m/%d/%y %H:%M:%S", t))
  113.                     return(0);
  114.                 continue;
  115.             case 'e':
  116.                 if (!_conv(t->tm_mday, 2, ' '))
  117.                     return(0);
  118.                 continue;
  119.             case 'D':
  120.                 if (!_fmt("%m/%d/%y", t))
  121.                     return(0);
  122.                 continue;
  123.             case 'd':
  124.                 if (!_conv(t->tm_mday, 2, '0'))
  125.                     return(0);
  126.                 continue;
  127.             case 'H':
  128.                 if (!_conv(t->tm_hour, 2, '0'))
  129.                     return(0);
  130.                 continue;
  131.             case 'I':
  132.                 if (!_conv(t->tm_hour % 12 ?
  133.                     t->tm_hour % 12 : 12, 2, '0'))
  134.                     return(0);
  135.                 continue;
  136.             case 'j':
  137.                 if (!_conv(t->tm_yday + 1, 3, '0'))
  138.                     return(0);
  139.                 continue;
  140.             case 'k':
  141.                 if (!_conv(t->tm_hour, 2, ' '))
  142.                     return(0);
  143.                 continue;
  144.             case 'l':
  145.                 if (!_conv(t->tm_hour % 12 ?
  146.                     t->tm_hour % 12 : 12, 2, ' '))
  147.                     return(0);
  148.                 continue;
  149.             case 'M':
  150.                 if (!_conv(t->tm_min, 2, '0'))
  151.                     return(0);
  152.                 continue;
  153.             case 'm':
  154.                 if (!_conv(t->tm_mon + 1, 2, '0'))
  155.                     return(0);
  156.                 continue;
  157.             case 'n':
  158.                 if (!_add("\n"))
  159.                     return(0);
  160.                 continue;
  161.             case 'p':
  162.                 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  163.                     return(0);
  164.                 continue;
  165.             case 'R':
  166.                 if (!_fmt("%H:%M", t))
  167.                     return(0);
  168.                 continue;
  169.             case 'r':
  170.                 if (!_fmt("%I:%M:%S %p", t))
  171.                     return(0);
  172.                 continue;
  173.             case 'S':
  174.                 if (!_conv(t->tm_sec, 2, '0'))
  175.                     return(0);
  176.                 continue;
  177.             case 'T':
  178.             case 'X':
  179.                 if (!_fmt("%H:%M:%S", t))
  180.                     return(0);
  181.                 continue;
  182.             case 't':
  183.                 if (!_add("\t"))
  184.                     return(0);
  185.                 continue;
  186.             case 'U':
  187.                 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  188.                     2, '0'))
  189.                     return(0);
  190.                 continue;
  191.             case 'W':
  192.                 if (!_conv((t->tm_yday + 7 -
  193.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  194.                     / 7, 2, '0'))
  195.                     return(0);
  196.                 continue;
  197.             case 'w':
  198.                 if (!_conv(t->tm_wday, 1, '0'))
  199.                     return(0);
  200.                 continue;
  201.             case 'x':
  202.                 if (!_fmt("%m/%d/%y", t))
  203.                     return(0);
  204.                 continue;
  205.             case 'y':
  206.                 if (!_conv((t->tm_year + TM_YEAR_BASE)
  207.                     % 100, 2, '0'))
  208.                     return(0);
  209.                 continue;
  210.             case 'Y':
  211.                 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  212.                     return(0);
  213.                 continue;
  214.             case 'Z':
  215.                 if (!t->tm_zone || !_add(t->tm_zone))
  216.                     return(0);
  217.                 continue;
  218.             case '%':
  219.             /*
  220.              * X311J/88-090 (4.12.3.5): if conversion char is
  221.              * undefined, behavior is undefined.  Print out the
  222.              * character itself as printf(3) does.
  223.              */
  224.             default:
  225.                 break;
  226.         }
  227.         if (!u.u_gsize--)
  228.             return(0);
  229.         *u.u_pt++ = *format;
  230.     }
  231.     return(u.u_gsize);
  232. }
  233.  
  234. static int _conv(int n, int digits, char pad)
  235. {
  236.     char buf[10];
  237.     register char *p;
  238.  
  239.     buf[sizeof(buf) - 1] = '\0';
  240.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  241.         *p-- = n % 10 + '0';
  242.     while (p > buf && digits-- > 0)
  243.         *p-- = pad;
  244.     return(_add(++p));
  245. }
  246.  
  247. static int _add(char *str)
  248. {
  249.     for (;; ++u.u_pt, --u.u_gsize) {
  250.         if (!u.u_gsize)
  251.             return(0);
  252.         if (!(*u.u_pt = *str++))
  253.             return(1);
  254.     }
  255. }
  256.