summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/gen/common/strftime.c
blob: 5267bc447a4204e0f6eb81087729b2fca3e675ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/


#pragma ident	"%Z%%M%	%I%	%E% SMI"

#if !defined(lint) && defined(SCCSIDS)
static char *sccsid = "%Z%%M% %I% %E% SMI"; /* from S5R3.1 cftime.c 1.9 */
#endif

/*LINTLIBRARY*/

#include <locale.h>
#include <time.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>

static char     *getstr(/*char *p, char **strp*/);
static char	*itoa();
extern int	stat();
extern char	*getenv();
extern char	*malloc();
extern 	int 	openlocale(/*char *category, int cat_id, char *locale, char *newlocale */);
extern void 	init_statics();

extern	struct	dtconv	*_dtconv;
extern	char	_locales[MAXLOCALE + 1][MAXLOCALENAME + 1];
extern	char	_my_time[];

char 	*dtconv_str = NULL;
char    *getlocale_time();

int
strftime(buf, maxsize, format, tm)
char	*buf, *format;
struct tm	*tm;
{
	register char	*cp, *p,  c;
	int		size;
	int		i, temp;
	register struct dtconv *dtcp;

	(void) getlocale_time();
	dtcp = localdtconv();	/* get locale's strings */

	/* Build date string by parsing format string */
	cp = buf;
	size = 0;
	while ((c = *format++) != '\0') {
		if (c == '%') {
			switch (*format++) {

			case '%':	/* Percent sign */
				if (++size >= maxsize)
					return (0);
				*cp++ = '%';
				break;

			case 'a':	/* Abbreviated weekday name */
				for (p = dtcp->abbrev_weekday_names[tm->tm_wday];
				    *p != '\0'; p++) {
					if (++size >= maxsize)
						return (0);
					*cp++ = *p;
				}
				break;

			case 'A':	/* Weekday name */
				for (p = dtcp->weekday_names[tm->tm_wday];
				    *p != '\0'; p++) {
					if (++size >= maxsize)
						return (0);
					*cp++ = *p;
				}
				break;

			case 'h':
			case 'b':	/* Abbreviated month name */
				for (p = dtcp->abbrev_month_names[tm->tm_mon];
				    *p != '\0'; p++) {
					if (++size >= maxsize)
						return (0);
					*cp++ = *p;
				}
				break;

			case 'B':	/* Month name */
				for (p = dtcp->month_names[tm->tm_mon];
				    *p != '\0'; p++) {
					if (++size >= maxsize)
						return (0);
					*cp++ = *p;
				}
				break;

			case 'c':	/* date and time representation */
				i = strftime(cp, maxsize - size, "%x %X", tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'C':	/* long date and time representation */
				i = strftime(cp, maxsize - size, 
				    dtcp->ldate_format, tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'd':	/* Day of month, with leading zero */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa(tm->tm_mday, cp, 2);
				break;

			case 'D':	/* Shorthand for %m/%d/%y */
				i = strftime(cp, maxsize - size, "%m/%d/%y",
				    tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'e':       /* Day of month without leading zero */
				if ((size += 2) >= maxsize)
					return (0);
				if (tm->tm_mday < 10) {
					*cp++ = ' ';
                                	cp = itoa(tm->tm_mday, cp, 1);
				} else
					cp = itoa(tm->tm_mday, cp, 2);
                                break;

			case 'H':	/* Hour (24 hour version) */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa(tm->tm_hour, cp, 2);
				break;

			case 'I':	/* Hour (12 hour version) */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa(tm->tm_hour > 12 ?
				    tm->tm_hour - 12 :
				    (tm->tm_hour == 0 ? 12 : tm->tm_hour),
				    cp, 2);
				break;

			case 'j':	/* Julian date */
				if ((size += 3) >= maxsize)
					return (0);
				cp = itoa(tm->tm_yday + 1, cp, 3);
				break;

			case 'k':	/* Hour (24 hour version) */
				if ((size += 2) >= maxsize)
					return (0);
				if (tm->tm_hour < 10) {
					*cp++ = ' ';
					cp = itoa(tm->tm_hour, cp, 1);
				} else
					cp = itoa(tm->tm_hour, cp, 2);
				break;

			case 'l':	/* Hour (12 hour version) */
				if ((size += 2) >= maxsize)
					return (0);
				temp = tm->tm_hour > 12 ?
				    tm->tm_hour - 12 :
				    (tm->tm_hour == 0 ? 12 : tm->tm_hour);
				if (temp < 10) {
					*cp++ = ' ';
					cp = itoa(temp, cp, 1);
				} else
					cp = itoa(temp, cp, 2);
				break;

			case 'm':	/* Month number */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa(tm->tm_mon + 1, cp, 2);
				break;

			case 'M':	/* Minute */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa(tm->tm_min, cp, 2);
				break;

			case 'n':	/* Newline */
				if (++size >= maxsize)
					return (0);
				*cp++ = '\n';
				break;

			case 'p':	/* AM or PM */
				if (tm->tm_hour >= 12) 
					p = dtcp->pm_string;
				else
					p = dtcp->am_string;
				for (; *p != '\0'; p++) {
					if (++size >= maxsize)
						return (0);
					*cp++ = *p;
				}
				break;

			case 'r':	/* Shorthand for %I:%M:%S AM or PM */
				i = strftime(cp, maxsize - size, "%I:%M:%S %p",
				    tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'R':	/* Time as %H:%M */
				i = strftime(cp, maxsize - size, "%H:%M", tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'S':	/* Seconds */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa(tm->tm_sec, cp, 2);
				break;

			case 't':	/* Tab */
				if (++size >= maxsize)
					return (0);
				*cp++ = '\t';
				break;

			case 'T':	/* Shorthand for %H:%M:%S */
				i = strftime(cp, maxsize - size, "%H:%M:%S",
				    tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'U':	/* Weekday number, taking Sunday as
					 * the first day of the week */
				if ((size += 2) >= maxsize)
					return (0);
				temp = tm->tm_yday - tm->tm_wday;
				if (temp >= -3 ) {
					i = (temp + 1) / 7 + 1;	/* +1 for - tm->tm_wday */
					if (temp % 7 >= 4)
						i++;
				} else
					i = 52;
				cp = itoa(i, cp, 2);
				break;

			case 'w':	/* Weekday number */
				if (++size >= maxsize)
					return (0);
				cp = itoa(tm->tm_wday, cp, 1);
				break;

			case 'W':	/* Week number of year, taking Monday as
					 * first day of week */
				if ((size += 2) >= maxsize)
					return (0);
				if (tm->tm_wday == 0)
					temp = tm->tm_yday - 6;
				else
					temp = tm->tm_yday - tm->tm_wday + 1;
				if (temp >= -3) {
					i = (temp + 1) / 7 + 1;	/* 1 for 
								   -tm->tm_wday */
					if (temp % 7 >= 4)
						i++;
				} else
					i = 52; /* less than 4 days in the first
						   week causes it to belong to
						   the tail of prev year */
				cp = itoa(i, cp, 2);
				break;

			case 'x':	/* Localized date format */
				i = strftime(cp, maxsize - size,
				    dtcp->sdate_format, tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'X':	/* Localized time format */
				i = strftime(cp, maxsize - size,
				    dtcp->time_format, tm);
				if (i == 0)
					return (0);
				cp += i;
				size += i;
				break;

			case 'y':	/* Year in the form yy */
				if ((size += 2) >= maxsize)
					return (0);
				cp = itoa((tm->tm_year% 100), cp, 2);
				break;

			case 'Y':	/* Year in the form ccyy */
				if ((size += 4) >= maxsize)
					return (0);
				cp = itoa(1900 + tm->tm_year, cp, 4);
				break;

			case 'Z':	/* Timezone */
				for(p = tm->tm_zone; *p != '\0'; p++) {
					if (++size >= maxsize)
						return (0);
					*cp++ = *p;
				}
				break;

			default:
				if ((size += 2) >= maxsize)
					return (0);
				*cp++ = c;
				*cp++ = *(format - 1);
				break;
			}
		} else {
			if (++size >= maxsize)
				return (0);
		 	*cp++ = c;
		}
	}
	*cp = '\0';
	return(size);
}

static char *
itoa(i, ptr, dig)
register int	i;
register char	*ptr;
register int	dig;
{
	switch(dig) {
	case 4:
		*ptr++ = i / 1000 + '0';
		i = i - i / 1000 * 1000;
	case 3:
		*ptr++ = i / 100 + '0';
		i = i - i / 100 * 100;
	case 2:
		*ptr++ = i / 10 + '0';
	case 1:
		*ptr++ = i % 10 + '0';
	}

	return(ptr);
}

char *
getlocale_time()
{
	register int fd;
	struct stat buf;
	char *str;
	register char *p;
	register int i;
	struct dtconv dtconvp;
	char temp[MAXLOCALENAME + 1];
	
	if (_locales[0][0] == '\0') 
		init_statics();

	/* Here we use the string newlocales to set time constants 
	 * which should have been saved 
	 * from a previous call to setlocale. We deferred the read until now
	 */

	if (strcmp(_my_time, _locales[LC_TIME -1]) == 0) {
		if (dtconv_str == NULL) {
                        /*
                         *  Below is executed if getlocale_time()
                         * is called when LC_TIME locale is initial
                         * C locale.
                         */
                        strcpy(temp, "C");
                        /*
                         * Just to make openlocale() to read LC_TIME file.
                         */
                        strcat(_locales[LC_TIME-1], temp);
                        goto initial;
                }
		return dtconv_str;
	}
	strcpy(temp, _locales[LC_TIME - 1]);
	strcpy(_locales[LC_TIME - 1], _my_time);
initial:
	if ((fd = openlocale("LC_TIME", LC_TIME, temp, _locales[LC_TIME - 1])) < 0)
		return (NULL);
	strcpy(_my_time, _locales[LC_TIME - 1]);
	if (fd == 0)
		return dtconv_str;
	if ((fstat(fd, &buf)) != 0)
		return (NULL);
	if ((str = malloc((unsigned)buf.st_size + 2)) == NULL) {
		close(fd);
		return (NULL);
	}

	if ((read(fd, str, (int)buf.st_size)) != buf.st_size) {
		close(fd);
		free(str);
		return (NULL);
	}

	/* Set last character of str to '\0' */
	p = &str[buf.st_size];
	*p++ = '\n';
	*p = '\0';

	/* p will "walk thru" str */
	p = str;  	

	for (i = 0; i < 12; i++) {
		p = getstr(p, &dtconvp.abbrev_month_names[i]);
		if (p == NULL)
			goto fail;
	}
	for (i = 0; i < 12; i++) {
		p = getstr(p, &dtconvp.month_names[i]);
		if (p == NULL)
			goto fail;
	}
	for (i = 0; i < 7; i++) {
		p = getstr(p, &dtconvp.abbrev_weekday_names[i]);
		if (p == NULL)
			goto fail;
	}
	for (i = 0; i < 7; i++) {
		p = getstr(p, &dtconvp.weekday_names[i]);
		if (p == NULL)
			goto fail;
	}
	p = getstr(p, &dtconvp.time_format);
	if (p == NULL)
		goto fail;
	p = getstr(p, &dtconvp.sdate_format);
	if (p == NULL)
		goto fail;
	p = getstr(p, &dtconvp.dtime_format);
	if (p == NULL)
		goto fail;
	p = getstr(p, &dtconvp.am_string);
	if (p == NULL)
		goto fail;
	p = getstr(p, &dtconvp.pm_string);
	if (p == NULL)
		goto fail;
	p = getstr(p, &dtconvp.ldate_format);
	if (p == NULL)
		goto fail;
	(void) close(fd);

	/*
	 * set info.
	 */
	if (dtconv_str != NULL)
		free(dtconv_str);

	dtconv_str = str;

	/* The following is to get space malloc'd for _dtconv */

	if (_dtconv == 0)
		(void) localdtconv();
	memcpy(_dtconv, &dtconvp, sizeof(struct dtconv));
	return (dtconv_str);

fail:
	(void) close(fd);
	free(str);
	return (NULL);
}


static char *
getstr(p, strp)
        register char *p;
        char **strp;
{
        *strp = p;
        p = strchr(p, '\n');
        if (p == NULL)
                return (NULL);  /* no end-of-line */
        *p++ = '\0';
        return (p);
}