home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / string / baserel / strftime.c.removed < prev    next >
Encoding:
Text File  |  1993-12-12  |  5.9 KB  |  278 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 gsize;
  57. static char *pt;
  58.  
  59. size_t
  60. strftime(s, maxsize, format, t)
  61.     char *s;
  62.     const char *format;
  63.     size_t maxsize;
  64.     const struct tm *t;
  65. {
  66.     size_t _fmt();
  67.     size_t res;
  68.  
  69.     ix_lock_base ();
  70.  
  71.     pt = s;
  72.     if ((gsize = maxsize) < 1)
  73.       {
  74.         res = 0;
  75.         goto unlock;
  76.       }
  77.     if (_fmt(format, t)) {
  78.         *pt = '\0';
  79.         res = maxsize - gsize;
  80.         goto unlock;
  81.     }
  82.     res = 0;
  83.  
  84. unlock:
  85.     ix_unlock_base ();
  86.     return res;
  87. }
  88.  
  89. static size_t
  90. _fmt(format, t)
  91.     register char *format;
  92.     struct tm *t;
  93. {
  94.     for (; *format; ++format) {
  95.         if (*format == '%')
  96.             switch(*++format) {
  97.             case '\0':
  98.                 --format;
  99.                 break;
  100.             case 'A':
  101.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  102.                     return(0);
  103.                 if (!_add(Afmt[t->tm_wday]))
  104.                     return(0);
  105.                 continue;
  106.             case 'a':
  107.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  108.                     return(0);
  109.                 if (!_add(afmt[t->tm_wday]))
  110.                     return(0);
  111.                 continue;
  112.             case 'B':
  113.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  114.                     return(0);
  115.                 if (!_add(Bfmt[t->tm_mon]))
  116.                     return(0);
  117.                 continue;
  118.             case 'b':
  119.             case 'h':
  120.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  121.                     return(0);
  122.                 if (!_add(bfmt[t->tm_mon]))
  123.                     return(0);
  124.                 continue;
  125.             case 'C':
  126.                 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  127.                     return(0);
  128.                 continue;
  129.             case 'c':
  130.                 if (!_fmt("%m/%d/%y %H:%M:%S", t))
  131.                     return(0);
  132.                 continue;
  133.             case 'e':
  134.                 if (!_conv(t->tm_mday, 2, ' '))
  135.                     return(0);
  136.                 continue;
  137.             case 'D':
  138.                 if (!_fmt("%m/%d/%y", t))
  139.                     return(0);
  140.                 continue;
  141.             case 'd':
  142.                 if (!_conv(t->tm_mday, 2, '0'))
  143.                     return(0);
  144.                 continue;
  145.             case 'H':
  146.                 if (!_conv(t->tm_hour, 2, '0'))
  147.                     return(0);
  148.                 continue;
  149.             case 'I':
  150.                 if (!_conv(t->tm_hour % 12 ?
  151.                     t->tm_hour % 12 : 12, 2, '0'))
  152.                     return(0);
  153.                 continue;
  154.             case 'j':
  155.                 if (!_conv(t->tm_yday + 1, 3, '0'))
  156.                     return(0);
  157.                 continue;
  158.             case 'k':
  159.                 if (!_conv(t->tm_hour, 2, ' '))
  160.                     return(0);
  161.                 continue;
  162.             case 'l':
  163.                 if (!_conv(t->tm_hour % 12 ?
  164.                     t->tm_hour % 12 : 12, 2, ' '))
  165.                     return(0);
  166.                 continue;
  167.             case 'M':
  168.                 if (!_conv(t->tm_min, 2, '0'))
  169.                     return(0);
  170.                 continue;
  171.             case 'm':
  172.                 if (!_conv(t->tm_mon + 1, 2, '0'))
  173.                     return(0);
  174.                 continue;
  175.             case 'n':
  176.                 if (!_add("\n"))
  177.                     return(0);
  178.                 continue;
  179.             case 'p':
  180.                 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  181.                     return(0);
  182.                 continue;
  183.             case 'R':
  184.                 if (!_fmt("%H:%M", t))
  185.                     return(0);
  186.                 continue;
  187.             case 'r':
  188.                 if (!_fmt("%I:%M:%S %p", t))
  189.                     return(0);
  190.                 continue;
  191.             case 'S':
  192.                 if (!_conv(t->tm_sec, 2, '0'))
  193.                     return(0);
  194.                 continue;
  195.             case 'T':
  196.             case 'X':
  197.                 if (!_fmt("%H:%M:%S", t))
  198.                     return(0);
  199.                 continue;
  200.             case 't':
  201.                 if (!_add("\t"))
  202.                     return(0);
  203.                 continue;
  204.             case 'U':
  205.                 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  206.                     2, '0'))
  207.                     return(0);
  208.                 continue;
  209.             case 'W':
  210.                 if (!_conv((t->tm_yday + 7 -
  211.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  212.                     / 7, 2, '0'))
  213.                     return(0);
  214.                 continue;
  215.             case 'w':
  216.                 if (!_conv(t->tm_wday, 1, '0'))
  217.                     return(0);
  218.                 continue;
  219.             case 'x':
  220.                 if (!_fmt("%m/%d/%y", t))
  221.                     return(0);
  222.                 continue;
  223.             case 'y':
  224.                 if (!_conv((t->tm_year + TM_YEAR_BASE)
  225.                     % 100, 2, '0'))
  226.                     return(0);
  227.                 continue;
  228.             case 'Y':
  229.                 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  230.                     return(0);
  231.                 continue;
  232.             case 'Z':
  233.                 if (!t->tm_zone || !_add(t->tm_zone))
  234.                     return(0);
  235.                 continue;
  236.             case '%':
  237.             /*
  238.              * X311J/88-090 (4.12.3.5): if conversion char is
  239.              * undefined, behavior is undefined.  Print out the
  240.              * character itself as printf(3) does.
  241.              */
  242.             default:
  243.                 break;
  244.         }
  245.         if (!gsize--)
  246.             return(0);
  247.         *pt++ = *format;
  248.     }
  249.     return(gsize);
  250. }
  251.  
  252. static
  253. _conv(n, digits, pad)
  254.     int n, digits;
  255.     char pad;
  256. {
  257.     static char buf[10];
  258.     register char *p;
  259.  
  260.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  261.         *p-- = n % 10 + '0';
  262.     while (p > buf && digits-- > 0)
  263.         *p-- = pad;
  264.     return(_add(++p));
  265. }
  266.  
  267. static
  268. _add(str)
  269.     register char *str;
  270. {
  271.     for (;; ++pt, --gsize) {
  272.         if (!gsize)
  273.             return(0);
  274.         if (!(*pt = *str++))
  275.             return(1);
  276.     }
  277. }
  278.