source: git/src/strftime.c @ 8ba2767

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 8ba2767 was 8ba2767, checked in by Olly Betts <olly@…>, 13 years ago

Fix to not leak if realloc() fails (spotted by Debian mass cppcheck).

git-svn-id: file:///home/survex-svn/survex/trunk@3567 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * strftime.c
3 *
4 * Public-domain relatively quick-and-dirty implemenation of
5 * ANSI library routine for System V Unix systems.
6 *
7 * It's written in old-style C for maximal portability.
8 * However, since I'm used to prototypes, I've included them too.
9 *
10 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
11 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
12 * For complete POSIX semantics, add POSIX_SEMANTICS.
13 *
14 * The code for %c, %x, and %X is my best guess as to what's "appropriate".
15 * This version ignores LOCALE information.
16 * It also doesn't worry about multi-byte characters.
17 * So there.
18 *
19 * Arnold Robbins
20 * January, February, March, 1991
21 * Updated March 1992
22 *
23 * Fixes from ado@elsie.nci.nih.gov
24 * February 1991
25 *
26 * Fixed compiler warnings from GCC
27 * Olly Betts 2005-01-12
28 */
29
30#include <stdio.h>
31#include <ctype.h>
32#include <string.h>
33#include <time.h>
34#include <sys/types.h>
35
36#ifndef __STDC__
37#define const   /**/
38
39extern void *malloc();
40extern void *realloc();
41extern void tzset();
42extern char *strchr();
43extern char *getenv();
44static int weeknumber();
45#else
46extern void *malloc(unsigned count);
47extern void *realloc(void *ptr, unsigned count);
48extern void tzset(void);
49extern char *strchr(const char *str, int ch);
50extern char *getenv(const char *v);
51static int weeknumber(const struct tm *timeptr, int firstweekday);
52#endif
53
54#ifdef __GNUC__
55#define inline  __inline__
56#else
57#define inline  /**/
58#endif
59
60#define range(low, item, hi)    max(low, min(item, hi))
61
62extern char *tzname[2];
63extern int daylight;
64
65#define SYSV_EXT        1       /* stuff in System V ascftime routine */
66#define POSIX2_DATE     1       /* stuff in Posix 1003.2 date command */
67#define VMS_EXT         1       /* include %V for VMS date format */
68#define POSIX_SEMANTICS 1       /* call tzset() if TZ changes */
69
70#if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
71#define SYSV_EXT        1
72#endif
73
74/* min --- return minimum of two numbers */
75
76#ifndef __STDC__
77static inline int
78min(a, b)
79int a, b;
80#else
81static inline int
82min(int a, int b)
83#endif
84{
85        return (a < b ? a : b);
86}
87
88/* max --- return maximum of two numbers */
89
90#ifndef __STDC__
91static inline int
92max(a, b)
93int a, b;
94#else
95static inline int
96max(int a, int b)
97#endif
98{
99        return (a > b ? a : b);
100}
101
102/* strftime --- produce formatted time */
103
104#ifndef __STDC__
105size_t
106strftime(s, maxsize, format, timeptr)
107char *s;
108size_t maxsize;
109const char *format;
110const struct tm *timeptr;
111#else
112size_t
113strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
114#endif
115{
116        char *endp = s + maxsize;
117        char *start = s;
118        char tbuf[100];
119        int i;
120        static short first = 1;
121        static char *savetz = NULL;
122        static int savetzlen = 0;
123        char *tz;
124        int tzlen;
125
126        /* various tables, useful in North America */
127        static const char *days_a[] = {
128                "Sun", "Mon", "Tue", "Wed",
129                "Thu", "Fri", "Sat",
130        };
131        static const char *days_l[] = {
132                "Sunday", "Monday", "Tuesday", "Wednesday",
133                "Thursday", "Friday", "Saturday",
134        };
135        static const char *months_a[] = {
136                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
137                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
138        };
139        static const char *months_l[] = {
140                "January", "February", "March", "April",
141                "May", "June", "July", "August", "September",
142                "October", "November", "December",
143        };
144        static const char *ampm[] = { "AM", "PM", };
145
146        if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
147                return 0;
148
149        if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
150                return 0;
151
152#ifndef POSIX_SEMANTICS
153        if (first) {
154                tzset();
155                first = 0;
156        }
157#else   /* POSIX_SEMANTICS */
158        tz = getenv("TZ");
159        tzlen = strlen(tz);
160        if (first) {
161                if (tz != NULL) {
162                        savetz = (char *) malloc(tzlen + 1);
163                        if (savetz != NULL) {
164                                savetzlen = tzlen + 1;
165                                strcpy(savetz, tz);
166                        }
167                }
168                tzset();
169                first = 0;
170        }
171        /* if we have a saved TZ, and it is different, recapture and reset */
172        if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
173                i = strlen(tz) + 1;
174                if (i > savetzlen) {
175                        char * newsavetz = (char *) realloc(savetz, i);
176                        if (newsavetz) {
177                                savetzlen = i;
178                                strcpy(newsavetz, tz);
179                        } else {
180                                free(savetz);
181                        }
182                        savetz = newsavetz;
183                } else
184                        strcpy(savetz, tz);
185                tzset();
186        }
187#endif /* POSIX_SEMANTICS */
188
189        for (; *format && s < endp - 1; format++) {
190                tbuf[0] = '\0';
191                if (*format != '%') {
192                        *s++ = *format;
193                        continue;
194                }
195        again:
196                switch (*++format) {
197                case '\0':
198                        *s++ = '%';
199                        goto out;
200
201                case '%':
202                        *s++ = '%';
203                        continue;
204
205                case 'a':       /* abbreviated weekday name */
206                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
207                                strcpy(tbuf, "?");
208                        else
209                                strcpy(tbuf, days_a[timeptr->tm_wday]);
210                        break;
211
212                case 'A':       /* full weekday name */
213                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
214                                strcpy(tbuf, "?");
215                        else
216                                strcpy(tbuf, days_l[timeptr->tm_wday]);
217                        break;
218
219#ifdef SYSV_EXT
220                case 'h':       /* abbreviated month name */
221#endif
222                case 'b':       /* abbreviated month name */
223                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
224                                strcpy(tbuf, "?");
225                        else
226                                strcpy(tbuf, months_a[timeptr->tm_mon]);
227                        break;
228
229                case 'B':       /* full month name */
230                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
231                                strcpy(tbuf, "?");
232                        else
233                                strcpy(tbuf, months_l[timeptr->tm_mon]);
234                        break;
235
236                case 'c':       /* appropriate date and time representation */
237                        sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
238                                days_a[range(0, timeptr->tm_wday, 6)],
239                                months_a[range(0, timeptr->tm_mon, 11)],
240                                range(1, timeptr->tm_mday, 31),
241                                range(0, timeptr->tm_hour, 23),
242                                range(0, timeptr->tm_min, 59),
243                                range(0, timeptr->tm_sec, 61),
244                                timeptr->tm_year + 1900);
245                        break;
246
247                case 'd':       /* day of the month, 01 - 31 */
248                        i = range(1, timeptr->tm_mday, 31);
249                        sprintf(tbuf, "%02d", i);
250                        break;
251
252                case 'H':       /* hour, 24-hour clock, 00 - 23 */
253                        i = range(0, timeptr->tm_hour, 23);
254                        sprintf(tbuf, "%02d", i);
255                        break;
256
257                case 'I':       /* hour, 12-hour clock, 01 - 12 */
258                        i = range(0, timeptr->tm_hour, 23);
259                        if (i == 0)
260                                i = 12;
261                        else if (i > 12)
262                                i -= 12;
263                        sprintf(tbuf, "%02d", i);
264                        break;
265
266                case 'j':       /* day of the year, 001 - 366 */
267                        sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
268                        break;
269
270                case 'm':       /* month, 01 - 12 */
271                        i = range(0, timeptr->tm_mon, 11);
272                        sprintf(tbuf, "%02d", i + 1);
273                        break;
274
275                case 'M':       /* minute, 00 - 59 */
276                        i = range(0, timeptr->tm_min, 59);
277                        sprintf(tbuf, "%02d", i);
278                        break;
279
280                case 'p':       /* am or pm based on 12-hour clock */
281                        i = range(0, timeptr->tm_hour, 23);
282                        if (i < 12)
283                                strcpy(tbuf, ampm[0]);
284                        else
285                                strcpy(tbuf, ampm[1]);
286                        break;
287
288                case 'S':       /* second, 00 - 61 */
289                        i = range(0, timeptr->tm_sec, 61);
290                        sprintf(tbuf, "%02d", i);
291                        break;
292
293                case 'U':       /* week of year, Sunday is first day of week */
294                        sprintf(tbuf, "%d", weeknumber(timeptr, 0));
295                        break;
296
297                case 'w':       /* weekday, Sunday == 0, 0 - 6 */
298                        i = range(0, timeptr->tm_wday, 6);
299                        sprintf(tbuf, "%d", i);
300                        break;
301
302                case 'W':       /* week of year, Monday is first day of week */
303                        sprintf(tbuf, "%d", weeknumber(timeptr, 1));
304                        break;
305
306                case 'x':       /* appropriate date representation */
307                        sprintf(tbuf, "%s %s %2d %d",
308                                days_a[range(0, timeptr->tm_wday, 6)],
309                                months_a[range(0, timeptr->tm_mon, 11)],
310                                range(1, timeptr->tm_mday, 31),
311                                timeptr->tm_year + 1900);
312                        break;
313
314                case 'X':       /* appropriate time representation */
315                        sprintf(tbuf, "%02d:%02d:%02d",
316                                range(0, timeptr->tm_hour, 23),
317                                range(0, timeptr->tm_min, 59),
318                                range(0, timeptr->tm_sec, 61));
319                        break;
320
321                case 'y':       /* year without a century, 00 - 99 */
322                        i = timeptr->tm_year % 100;
323                        sprintf(tbuf, "%d", i);
324                        break;
325
326                case 'Y':       /* year with century */
327                        sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
328                        break;
329
330                case 'Z':       /* time zone name or abbrevation */
331                        i = 0;
332                        if (daylight && timeptr->tm_isdst)
333                                i = 1;
334                        strcpy(tbuf, tzname[i]);
335                        break;
336
337#ifdef SYSV_EXT
338                case 'n':       /* same as \n */
339                        tbuf[0] = '\n';
340                        tbuf[1] = '\0';
341                        break;
342
343                case 't':       /* same as \t */
344                        tbuf[0] = '\t';
345                        tbuf[1] = '\0';
346                        break;
347
348                case 'D':       /* date as %m/%d/%y */
349                        strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
350                        break;
351
352                case 'e':       /* day of month, blank padded */
353                        sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
354                        break;
355
356                case 'r':       /* time as %I:%M:%S %p */
357                        strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
358                        break;
359
360                case 'R':       /* time as %H:%M */
361                        strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
362                        break;
363
364                case 'T':       /* time as %H:%M:%S */
365                        strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
366                        break;
367#endif
368
369
370#ifdef VMS_EXT
371                case 'V':       /* date as dd-bbb-YYYY */
372                        sprintf(tbuf, "%2d-%3.3s-%4d",
373                                range(1, timeptr->tm_mday, 31),
374                                months_a[range(0, timeptr->tm_mon, 11)],
375                                timeptr->tm_year + 1900);
376                        for (i = 3; i < 6; i++)
377                                if (islower(tbuf[i]))
378                                        tbuf[i] = toupper(tbuf[i]);
379                        break;
380#endif
381
382
383#ifdef POSIX2_DATE
384                case 'C':
385                        sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
386                        break;
387
388
389                case 'E':
390                case 'O':
391                        /* POSIX locale extensions, ignored for now */
392                        goto again;
393#endif
394                default:
395                        tbuf[0] = '%';
396                        tbuf[1] = *format;
397                        tbuf[2] = '\0';
398                        break;
399                }
400                i = strlen(tbuf);
401                if (i) {
402                        if (s + i < endp - 1) {
403                                strcpy(s, tbuf);
404                                s += i;
405                        } else
406                                return 0;
407                }
408        }
409out:
410        if (s < endp && *format == '\0') {
411                *s = '\0';
412                return (s - start);
413        } else
414                return 0;
415}
416
417/* weeknumber --- figure how many weeks into the year */
418
419/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
420/* Modified byMichal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK> */
421
422#ifndef __STDC__
423static int
424weeknumber(timeptr, firstweekday)
425const struct tm *timeptr;
426int firstweekday;
427#else
428static int
429weeknumber(const struct tm *timeptr, int firstweekday)
430#endif
431/*
432 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
433 */
434{
435    return (timeptr->tm_yday - timeptr->tm_wday +
436            (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
437}
Note: See TracBrowser for help on using the repository browser.