Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.31.2.2
      1  1.31.2.2  christos /*	$NetBSD: strptime.c,v 1.31.2.2 2008/11/04 21:08:34 christos Exp $	*/
      2  1.31.2.2  christos 
      3  1.31.2.2  christos /*-
      4  1.31.2.2  christos  * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
      5  1.31.2.2  christos  * All rights reserved.
      6  1.31.2.2  christos  *
      7  1.31.2.2  christos  * This code was contributed to The NetBSD Foundation by Klaus Klein.
      8  1.31.2.2  christos  * Heavily optimised by David Laight
      9  1.31.2.2  christos  *
     10  1.31.2.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.31.2.2  christos  * modification, are permitted provided that the following conditions
     12  1.31.2.2  christos  * are met:
     13  1.31.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.31.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.31.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.31.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.31.2.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.31.2.2  christos  *
     19  1.31.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.31.2.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.31.2.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.31.2.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.31.2.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.31.2.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.31.2.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.31.2.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.31.2.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.31.2.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.31.2.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.31.2.2  christos  */
     31  1.31.2.2  christos 
     32  1.31.2.2  christos #include <sys/cdefs.h>
     33  1.31.2.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.31.2.2  christos __RCSID("$NetBSD: strptime.c,v 1.31.2.2 2008/11/04 21:08:34 christos Exp $");
     35  1.31.2.2  christos #endif
     36  1.31.2.2  christos 
     37  1.31.2.2  christos #include "namespace.h"
     38  1.31.2.2  christos #include <sys/localedef.h>
     39  1.31.2.2  christos #include <ctype.h>
     40  1.31.2.2  christos #include <locale.h>
     41  1.31.2.2  christos #include <string.h>
     42  1.31.2.2  christos #include <time.h>
     43  1.31.2.2  christos #include <tzfile.h>
     44  1.31.2.2  christos #include "private.h"
     45  1.31.2.2  christos 
     46  1.31.2.2  christos #ifdef __weak_alias
     47  1.31.2.2  christos __weak_alias(strptime,_strptime)
     48  1.31.2.2  christos #endif
     49  1.31.2.2  christos 
     50  1.31.2.2  christos #define	_ctloc(x)		(_CurrentTimeLocale->x)
     51  1.31.2.2  christos 
     52  1.31.2.2  christos /*
     53  1.31.2.2  christos  * We do not implement alternate representations. However, we always
     54  1.31.2.2  christos  * check whether a given modifier is allowed for a certain conversion.
     55  1.31.2.2  christos  */
     56  1.31.2.2  christos #define ALT_E			0x01
     57  1.31.2.2  christos #define ALT_O			0x02
     58  1.31.2.2  christos #define	LEGAL_ALT(x)		{ if (alt_format & ~(x)) return NULL; }
     59  1.31.2.2  christos 
     60  1.31.2.2  christos static char gmt[] = { "GMT" };
     61  1.31.2.2  christos static char utc[] = { "UTC" };
     62  1.31.2.2  christos 
     63  1.31.2.2  christos static const u_char *conv_num(const unsigned char *, int *, uint, uint);
     64  1.31.2.2  christos static const u_char *find_string(const u_char *, int *, const char * const *,
     65  1.31.2.2  christos 	const char * const *, int);
     66  1.31.2.2  christos 
     67  1.31.2.2  christos 
     68  1.31.2.2  christos char *
     69  1.31.2.2  christos strptime(const char *buf, const char *fmt, struct tm *tm)
     70  1.31.2.2  christos {
     71  1.31.2.2  christos 	unsigned char c;
     72  1.31.2.2  christos 	const unsigned char *bp;
     73  1.31.2.2  christos 	int alt_format, i, split_year = 0, neg, offs;
     74  1.31.2.2  christos 	const char *new_fmt;
     75  1.31.2.2  christos 
     76  1.31.2.2  christos 	bp = (const u_char *)buf;
     77  1.31.2.2  christos 
     78  1.31.2.2  christos 	while (bp != NULL && (c = *fmt++) != '\0') {
     79  1.31.2.2  christos 		/* Clear `alternate' modifier prior to new conversion. */
     80  1.31.2.2  christos 		alt_format = 0;
     81  1.31.2.2  christos 		i = 0;
     82  1.31.2.2  christos 
     83  1.31.2.2  christos 		/* Eat up white-space. */
     84  1.31.2.2  christos 		if (isspace(c)) {
     85  1.31.2.2  christos 			while (isspace(*bp))
     86  1.31.2.2  christos 				bp++;
     87  1.31.2.2  christos 			continue;
     88  1.31.2.2  christos 		}
     89  1.31.2.2  christos 
     90  1.31.2.2  christos 		if (c != '%')
     91  1.31.2.2  christos 			goto literal;
     92  1.31.2.2  christos 
     93  1.31.2.2  christos 
     94  1.31.2.2  christos again:		switch (c = *fmt++) {
     95  1.31.2.2  christos 		case '%':	/* "%%" is converted to "%". */
     96  1.31.2.2  christos literal:
     97  1.31.2.2  christos 			if (c != *bp++)
     98  1.31.2.2  christos 				return NULL;
     99  1.31.2.2  christos 			LEGAL_ALT(0);
    100  1.31.2.2  christos 			continue;
    101  1.31.2.2  christos 
    102  1.31.2.2  christos 		/*
    103  1.31.2.2  christos 		 * "Alternative" modifiers. Just set the appropriate flag
    104  1.31.2.2  christos 		 * and start over again.
    105  1.31.2.2  christos 		 */
    106  1.31.2.2  christos 		case 'E':	/* "%E?" alternative conversion modifier. */
    107  1.31.2.2  christos 			LEGAL_ALT(0);
    108  1.31.2.2  christos 			alt_format |= ALT_E;
    109  1.31.2.2  christos 			goto again;
    110  1.31.2.2  christos 
    111  1.31.2.2  christos 		case 'O':	/* "%O?" alternative conversion modifier. */
    112  1.31.2.2  christos 			LEGAL_ALT(0);
    113  1.31.2.2  christos 			alt_format |= ALT_O;
    114  1.31.2.2  christos 			goto again;
    115  1.31.2.2  christos 
    116  1.31.2.2  christos 		/*
    117  1.31.2.2  christos 		 * "Complex" conversion rules, implemented through recursion.
    118  1.31.2.2  christos 		 */
    119  1.31.2.2  christos 		case 'c':	/* Date and time, using the locale's format. */
    120  1.31.2.2  christos 			new_fmt = _ctloc(d_t_fmt);
    121  1.31.2.2  christos 			goto recurse;
    122  1.31.2.2  christos 
    123  1.31.2.2  christos 		case 'D':	/* The date as "%m/%d/%y". */
    124  1.31.2.2  christos 			new_fmt = "%m/%d/%y";
    125  1.31.2.2  christos 			LEGAL_ALT(0);
    126  1.31.2.2  christos 			goto recurse;
    127  1.31.2.2  christos 
    128  1.31.2.2  christos 		case 'F':	/* The date as "%Y-%m-%d". */
    129  1.31.2.2  christos 			new_fmt = "%Y-%m-%d";
    130  1.31.2.2  christos 			LEGAL_ALT(0);
    131  1.31.2.2  christos 			goto recurse;
    132  1.31.2.2  christos 
    133  1.31.2.2  christos 		case 'R':	/* The time as "%H:%M". */
    134  1.31.2.2  christos 			new_fmt = "%H:%M";
    135  1.31.2.2  christos 			LEGAL_ALT(0);
    136  1.31.2.2  christos 			goto recurse;
    137  1.31.2.2  christos 
    138  1.31.2.2  christos 		case 'r':	/* The time in 12-hour clock representation. */
    139  1.31.2.2  christos 			new_fmt =_ctloc(t_fmt_ampm);
    140  1.31.2.2  christos 			LEGAL_ALT(0);
    141  1.31.2.2  christos 			goto recurse;
    142  1.31.2.2  christos 
    143  1.31.2.2  christos 		case 'T':	/* The time as "%H:%M:%S". */
    144  1.31.2.2  christos 			new_fmt = "%H:%M:%S";
    145  1.31.2.2  christos 			LEGAL_ALT(0);
    146  1.31.2.2  christos 			goto recurse;
    147  1.31.2.2  christos 
    148  1.31.2.2  christos 		case 'X':	/* The time, using the locale's format. */
    149  1.31.2.2  christos 			new_fmt =_ctloc(t_fmt);
    150  1.31.2.2  christos 			goto recurse;
    151  1.31.2.2  christos 
    152  1.31.2.2  christos 		case 'x':	/* The date, using the locale's format. */
    153  1.31.2.2  christos 			new_fmt =_ctloc(d_fmt);
    154  1.31.2.2  christos 		    recurse:
    155  1.31.2.2  christos 			bp = (const u_char *)strptime((const char *)bp,
    156  1.31.2.2  christos 							    new_fmt, tm);
    157  1.31.2.2  christos 			LEGAL_ALT(ALT_E);
    158  1.31.2.2  christos 			continue;
    159  1.31.2.2  christos 
    160  1.31.2.2  christos 		/*
    161  1.31.2.2  christos 		 * "Elementary" conversion rules.
    162  1.31.2.2  christos 		 */
    163  1.31.2.2  christos 		case 'A':	/* The day of week, using the locale's form. */
    164  1.31.2.2  christos 		case 'a':
    165  1.31.2.2  christos 			bp = find_string(bp, &tm->tm_wday, _ctloc(day),
    166  1.31.2.2  christos 					_ctloc(abday), 7);
    167  1.31.2.2  christos 			LEGAL_ALT(0);
    168  1.31.2.2  christos 			continue;
    169  1.31.2.2  christos 
    170  1.31.2.2  christos 		case 'B':	/* The month, using the locale's form. */
    171  1.31.2.2  christos 		case 'b':
    172  1.31.2.2  christos 		case 'h':
    173  1.31.2.2  christos 			bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
    174  1.31.2.2  christos 					_ctloc(abmon), 12);
    175  1.31.2.2  christos 			LEGAL_ALT(0);
    176  1.31.2.2  christos 			continue;
    177  1.31.2.2  christos 
    178  1.31.2.2  christos 		case 'C':	/* The century number. */
    179  1.31.2.2  christos 			i = 20;
    180  1.31.2.2  christos 			bp = conv_num(bp, &i, 0, 99);
    181  1.31.2.2  christos 
    182  1.31.2.2  christos 			i = i * 100 - TM_YEAR_BASE;
    183  1.31.2.2  christos 			if (split_year)
    184  1.31.2.2  christos 				i += tm->tm_year % 100;
    185  1.31.2.2  christos 			split_year = 1;
    186  1.31.2.2  christos 			tm->tm_year = i;
    187  1.31.2.2  christos 			LEGAL_ALT(ALT_E);
    188  1.31.2.2  christos 			continue;
    189  1.31.2.2  christos 
    190  1.31.2.2  christos 		case 'd':	/* The day of month. */
    191  1.31.2.2  christos 		case 'e':
    192  1.31.2.2  christos 			bp = conv_num(bp, &tm->tm_mday, 1, 31);
    193  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    194  1.31.2.2  christos 			continue;
    195  1.31.2.2  christos 
    196  1.31.2.2  christos 		case 'k':	/* The hour (24-hour clock representation). */
    197  1.31.2.2  christos 			LEGAL_ALT(0);
    198  1.31.2.2  christos 			/* FALLTHROUGH */
    199  1.31.2.2  christos 		case 'H':
    200  1.31.2.2  christos 			bp = conv_num(bp, &tm->tm_hour, 0, 23);
    201  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    202  1.31.2.2  christos 			continue;
    203  1.31.2.2  christos 
    204  1.31.2.2  christos 		case 'l':	/* The hour (12-hour clock representation). */
    205  1.31.2.2  christos 			LEGAL_ALT(0);
    206  1.31.2.2  christos 			/* FALLTHROUGH */
    207  1.31.2.2  christos 		case 'I':
    208  1.31.2.2  christos 			bp = conv_num(bp, &tm->tm_hour, 1, 12);
    209  1.31.2.2  christos 			if (tm->tm_hour == 12)
    210  1.31.2.2  christos 				tm->tm_hour = 0;
    211  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    212  1.31.2.2  christos 			continue;
    213  1.31.2.2  christos 
    214  1.31.2.2  christos 		case 'j':	/* The day of year. */
    215  1.31.2.2  christos 			i = 1;
    216  1.31.2.2  christos 			bp = conv_num(bp, &i, 1, 366);
    217  1.31.2.2  christos 			tm->tm_yday = i - 1;
    218  1.31.2.2  christos 			LEGAL_ALT(0);
    219  1.31.2.2  christos 			continue;
    220  1.31.2.2  christos 
    221  1.31.2.2  christos 		case 'M':	/* The minute. */
    222  1.31.2.2  christos 			bp = conv_num(bp, &tm->tm_min, 0, 59);
    223  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    224  1.31.2.2  christos 			continue;
    225  1.31.2.2  christos 
    226  1.31.2.2  christos 		case 'm':	/* The month. */
    227  1.31.2.2  christos 			i = 1;
    228  1.31.2.2  christos 			bp = conv_num(bp, &i, 1, 12);
    229  1.31.2.2  christos 			tm->tm_mon = i - 1;
    230  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    231  1.31.2.2  christos 			continue;
    232  1.31.2.2  christos 
    233  1.31.2.2  christos 		case 'p':	/* The locale's equivalent of AM/PM. */
    234  1.31.2.2  christos 			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
    235  1.31.2.2  christos 			if (tm->tm_hour > 11)
    236  1.31.2.2  christos 				return NULL;
    237  1.31.2.2  christos 			tm->tm_hour += i * 12;
    238  1.31.2.2  christos 			LEGAL_ALT(0);
    239  1.31.2.2  christos 			continue;
    240  1.31.2.2  christos 
    241  1.31.2.2  christos 		case 'S':	/* The seconds. */
    242  1.31.2.2  christos 			bp = conv_num(bp, &tm->tm_sec, 0, 61);
    243  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    244  1.31.2.2  christos 			continue;
    245  1.31.2.2  christos 
    246  1.31.2.2  christos 		case 'U':	/* The week of year, beginning on sunday. */
    247  1.31.2.2  christos 		case 'W':	/* The week of year, beginning on monday. */
    248  1.31.2.2  christos 			/*
    249  1.31.2.2  christos 			 * XXX This is bogus, as we can not assume any valid
    250  1.31.2.2  christos 			 * information present in the tm structure at this
    251  1.31.2.2  christos 			 * point to calculate a real value, so just check the
    252  1.31.2.2  christos 			 * range for now.
    253  1.31.2.2  christos 			 */
    254  1.31.2.2  christos 			 bp = conv_num(bp, &i, 0, 53);
    255  1.31.2.2  christos 			 LEGAL_ALT(ALT_O);
    256  1.31.2.2  christos 			 continue;
    257  1.31.2.2  christos 
    258  1.31.2.2  christos 		case 'w':	/* The day of week, beginning on sunday. */
    259  1.31.2.2  christos 			bp = conv_num(bp, &tm->tm_wday, 0, 6);
    260  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    261  1.31.2.2  christos 			continue;
    262  1.31.2.2  christos 
    263  1.31.2.2  christos 		case 'u':	/* The day of week, monday = 1. */
    264  1.31.2.2  christos 			bp = conv_num(bp, &i, 1, 7);
    265  1.31.2.2  christos 			tm->tm_wday = i % 7;
    266  1.31.2.2  christos 			LEGAL_ALT(ALT_O);
    267  1.31.2.2  christos 			continue;
    268  1.31.2.2  christos 
    269  1.31.2.2  christos 		case 'g':	/* The year corresponding to the ISO week
    270  1.31.2.2  christos 				 * number but without the century.
    271  1.31.2.2  christos 				 */
    272  1.31.2.2  christos 			bp = conv_num(bp, &i, 0, 99);
    273  1.31.2.2  christos 			continue;
    274  1.31.2.2  christos 
    275  1.31.2.2  christos 		case 'G':	/* The year corresponding to the ISO week
    276  1.31.2.2  christos 				 * number with century.
    277  1.31.2.2  christos 				 */
    278  1.31.2.2  christos 			do
    279  1.31.2.2  christos 				bp++;
    280  1.31.2.2  christos 			while (isdigit(*bp));
    281  1.31.2.2  christos 			continue;
    282  1.31.2.2  christos 
    283  1.31.2.2  christos 		case 'V':	/* The ISO 8601:1988 week number as decimal */
    284  1.31.2.2  christos 			bp = conv_num(bp, &i, 0, 53);
    285  1.31.2.2  christos 			continue;
    286  1.31.2.2  christos 
    287  1.31.2.2  christos 		case 'Y':	/* The year. */
    288  1.31.2.2  christos 			i = TM_YEAR_BASE;	/* just for data sanity... */
    289  1.31.2.2  christos 			bp = conv_num(bp, &i, 0, 9999);
    290  1.31.2.2  christos 			tm->tm_year = i - TM_YEAR_BASE;
    291  1.31.2.2  christos 			LEGAL_ALT(ALT_E);
    292  1.31.2.2  christos 			continue;
    293  1.31.2.2  christos 
    294  1.31.2.2  christos 		case 'y':	/* The year within 100 years of the epoch. */
    295  1.31.2.2  christos 			/* LEGAL_ALT(ALT_E | ALT_O); */
    296  1.31.2.2  christos 			bp = conv_num(bp, &i, 0, 99);
    297  1.31.2.2  christos 
    298  1.31.2.2  christos 			if (split_year)
    299  1.31.2.2  christos 				/* preserve century */
    300  1.31.2.2  christos 				i += (tm->tm_year / 100) * 100;
    301  1.31.2.2  christos 			else {
    302  1.31.2.2  christos 				split_year = 1;
    303  1.31.2.2  christos 				if (i <= 68)
    304  1.31.2.2  christos 					i = i + 2000 - TM_YEAR_BASE;
    305  1.31.2.2  christos 				else
    306  1.31.2.2  christos 					i = i + 1900 - TM_YEAR_BASE;
    307  1.31.2.2  christos 			}
    308  1.31.2.2  christos 			tm->tm_year = i;
    309  1.31.2.2  christos 			continue;
    310  1.31.2.2  christos 
    311  1.31.2.2  christos 		case 'Z':
    312  1.31.2.2  christos 			tzset();
    313  1.31.2.2  christos 			if (strncmp((const char *)bp, gmt, 3) == 0) {
    314  1.31.2.2  christos 				tm->tm_isdst = 0;
    315  1.31.2.2  christos #ifdef TM_GMTOFF
    316  1.31.2.2  christos 				tm->TM_GMTOFF = 0;
    317  1.31.2.2  christos #endif
    318  1.31.2.2  christos #ifdef TM_ZONE
    319  1.31.2.2  christos 				tm->TM_ZONE = gmt;
    320  1.31.2.2  christos #endif
    321  1.31.2.2  christos 				bp += 3;
    322  1.31.2.2  christos 			} else {
    323  1.31.2.2  christos 				const unsigned char *ep;
    324  1.31.2.2  christos 
    325  1.31.2.2  christos 				ep = find_string(bp, &i,
    326  1.31.2.2  christos 					       	 (const char * const *)tzname,
    327  1.31.2.2  christos 					       	  NULL, 2);
    328  1.31.2.2  christos 				if (ep != NULL) {
    329  1.31.2.2  christos 					tm->tm_isdst = i;
    330  1.31.2.2  christos #ifdef TM_GMTOFF
    331  1.31.2.2  christos 					tm->TM_GMTOFF = -(timezone);
    332  1.31.2.2  christos #endif
    333  1.31.2.2  christos #ifdef TM_ZONE
    334  1.31.2.2  christos 					tm->TM_ZONE = tzname[i];
    335  1.31.2.2  christos #endif
    336  1.31.2.2  christos 				}
    337  1.31.2.2  christos 				bp = ep;
    338  1.31.2.2  christos 			}
    339  1.31.2.2  christos 			continue;
    340  1.31.2.2  christos 
    341  1.31.2.2  christos 		case 'z':
    342  1.31.2.2  christos 			/*
    343  1.31.2.2  christos 			 * We recognize all ISO 8601 formats:
    344  1.31.2.2  christos 			 * Z	= Zulu time/UTC
    345  1.31.2.2  christos 			 * [+-]hhmm
    346  1.31.2.2  christos 			 * [+-]hh:mm
    347  1.31.2.2  christos 			 * [+-]hh
    348  1.31.2.2  christos 			 */
    349  1.31.2.2  christos 			while (isspace(*bp))
    350  1.31.2.2  christos 				bp++;
    351  1.31.2.2  christos 
    352  1.31.2.2  christos 			switch (*bp++) {
    353  1.31.2.2  christos 			case 'Z':
    354  1.31.2.2  christos 				tm->tm_isdst = 0;
    355  1.31.2.2  christos #ifdef TM_GMTOFF
    356  1.31.2.2  christos 				tm->TM_GMTOFF = 0;
    357  1.31.2.2  christos #endif
    358  1.31.2.2  christos #ifdef TM_ZONE
    359  1.31.2.2  christos 				tm->TM_ZONE = utc;
    360  1.31.2.2  christos #endif
    361  1.31.2.2  christos 				continue;
    362  1.31.2.2  christos 			case '+':
    363  1.31.2.2  christos 				neg = 0;
    364  1.31.2.2  christos 				break;
    365  1.31.2.2  christos 			case '-':
    366  1.31.2.2  christos 				neg = 1;
    367  1.31.2.2  christos 				break;
    368  1.31.2.2  christos 			default:
    369  1.31.2.2  christos 				return NULL;
    370  1.31.2.2  christos 			}
    371  1.31.2.2  christos 			offs = 0;
    372  1.31.2.2  christos 			for (i = 0; i < 4; ) {
    373  1.31.2.2  christos 				if (isdigit(*bp)) {
    374  1.31.2.2  christos 					offs = offs * 10 + (*bp++ - '0');
    375  1.31.2.2  christos 					i++;
    376  1.31.2.2  christos 					continue;
    377  1.31.2.2  christos 				}
    378  1.31.2.2  christos 				if (i == 2 && *bp == ':') {
    379  1.31.2.2  christos 					bp++;
    380  1.31.2.2  christos 					continue;
    381  1.31.2.2  christos 				}
    382  1.31.2.2  christos 				break;
    383  1.31.2.2  christos 			}
    384  1.31.2.2  christos 			switch (i) {
    385  1.31.2.2  christos 			case 2:
    386  1.31.2.2  christos 				offs *= 100;
    387  1.31.2.2  christos 				break;
    388  1.31.2.2  christos 			case 4:
    389  1.31.2.2  christos 				i = offs % 100;
    390  1.31.2.2  christos 				if (i >= 60)
    391  1.31.2.2  christos 					return NULL;
    392  1.31.2.2  christos 				/* Convert minutes into decimal */
    393  1.31.2.2  christos 				offs = (offs / 100) * 100 + (i * 50) / 30;
    394  1.31.2.2  christos 				break;
    395  1.31.2.2  christos 			default:
    396  1.31.2.2  christos 				return NULL;
    397  1.31.2.2  christos 			}
    398  1.31.2.2  christos 			if (neg)
    399  1.31.2.2  christos 				offs = -offs;
    400  1.31.2.2  christos 			tm->tm_isdst = 0;	/* XXX */
    401  1.31.2.2  christos #ifdef TM_GMTOFF
    402  1.31.2.2  christos 			tm->TM_GMTOFF = offs;
    403  1.31.2.2  christos #endif
    404  1.31.2.2  christos #ifdef TM_ZONE
    405  1.31.2.2  christos 			tm->TM_ZONE = NULL;	/* XXX */
    406  1.31.2.2  christos #endif
    407  1.31.2.2  christos 			continue;
    408  1.31.2.2  christos 
    409  1.31.2.2  christos 		/*
    410  1.31.2.2  christos 		 * Miscellaneous conversions.
    411  1.31.2.2  christos 		 */
    412  1.31.2.2  christos 		case 'n':	/* Any kind of white-space. */
    413  1.31.2.2  christos 		case 't':
    414  1.31.2.2  christos 			while (isspace(*bp))
    415  1.31.2.2  christos 				bp++;
    416  1.31.2.2  christos 			LEGAL_ALT(0);
    417  1.31.2.2  christos 			continue;
    418  1.31.2.2  christos 
    419  1.31.2.2  christos 
    420  1.31.2.2  christos 		default:	/* Unknown/unsupported conversion. */
    421  1.31.2.2  christos 			return NULL;
    422  1.31.2.2  christos 		}
    423  1.31.2.2  christos 	}
    424  1.31.2.2  christos 
    425  1.31.2.2  christos 	return __UNCONST(bp);
    426  1.31.2.2  christos }
    427  1.31.2.2  christos 
    428  1.31.2.2  christos 
    429  1.31.2.2  christos static const u_char *
    430  1.31.2.2  christos conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
    431  1.31.2.2  christos {
    432  1.31.2.2  christos 	uint result = 0;
    433  1.31.2.2  christos 	unsigned char ch;
    434  1.31.2.2  christos 
    435  1.31.2.2  christos 	/* The limit also determines the number of valid digits. */
    436  1.31.2.2  christos 	uint rulim = ulim;
    437  1.31.2.2  christos 
    438  1.31.2.2  christos 	ch = *buf;
    439  1.31.2.2  christos 	if (ch < '0' || ch > '9')
    440  1.31.2.2  christos 		return NULL;
    441  1.31.2.2  christos 
    442  1.31.2.2  christos 	do {
    443  1.31.2.2  christos 		result *= 10;
    444  1.31.2.2  christos 		result += ch - '0';
    445  1.31.2.2  christos 		rulim /= 10;
    446  1.31.2.2  christos 		ch = *++buf;
    447  1.31.2.2  christos 	} while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
    448  1.31.2.2  christos 
    449  1.31.2.2  christos 	if (result < llim || result > ulim)
    450  1.31.2.2  christos 		return NULL;
    451  1.31.2.2  christos 
    452  1.31.2.2  christos 	*dest = result;
    453  1.31.2.2  christos 	return buf;
    454  1.31.2.2  christos }
    455  1.31.2.2  christos 
    456  1.31.2.2  christos static const u_char *
    457  1.31.2.2  christos find_string(const u_char *bp, int *tgt, const char * const *n1,
    458  1.31.2.2  christos 		const char * const *n2, int c)
    459  1.31.2.2  christos {
    460  1.31.2.2  christos 	int i;
    461  1.31.2.2  christos 	unsigned int len;
    462  1.31.2.2  christos 
    463  1.31.2.2  christos 	/* check full name - then abbreviated ones */
    464  1.31.2.2  christos 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
    465  1.31.2.2  christos 		for (i = 0; i < c; i++, n1++) {
    466  1.31.2.2  christos 			len = strlen(*n1);
    467  1.31.2.2  christos 			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
    468  1.31.2.2  christos 				*tgt = i;
    469  1.31.2.2  christos 				return bp + len;
    470  1.31.2.2  christos 			}
    471  1.31.2.2  christos 		}
    472  1.31.2.2  christos 	}
    473  1.31.2.2  christos 
    474  1.31.2.2  christos 	/* Nothing matched */
    475  1.31.2.2  christos 	return NULL;
    476  1.31.2.2  christos }
    477