blob: 0567aa4192ee2790078ffd44a13836e6b5e875cc [file] [log] [blame]
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001/* $OpenBSD: strptime.c,v 1.11 2005/08/08 08:05:38 espie Exp $ */
2/* $NetBSD: strptime.c,v 1.12 1998/01/20 21:39:40 mycroft Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003
4/*-
5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code was contributed to The NetBSD Foundation by Klaus Klein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39//#include <sys/localedef.h>
40#include <ctype.h>
41#include <locale.h>
42#include <string.h>
43#include <time.h>
44#include "tzfile.h"
45
46static const struct {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070047 const char *abday[7];
48 const char *day[7];
49 const char *abmon[12];
50 const char *mon[12];
51 const char *am_pm[2];
52 const char *d_t_fmt;
53 const char *d_fmt;
54 const char *t_fmt;
55 const char *t_fmt_ampm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056} _DefaultTimeLocale = {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070057 {
58 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
59 },
60 {
61 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
62 "Friday", "Saturday"
63 },
64 {
65 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
66 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
67 },
68 {
69 "January", "February", "March", "April", "May", "June", "July",
70 "August", "September", "October", "November", "December"
71 },
72 {
73 "AM", "PM"
74 },
75 "%a %b %d %H:%M:%S %Y",
76 "%m/%d/%y",
77 "%H:%M:%S",
78 "%I:%M:%S %p"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079};
80
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070081#define _ctloc(x) (_DefaultTimeLocale.x)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
83/*
84 * We do not implement alternate representations. However, we always
85 * check whether a given modifier is allowed for a certain conversion.
86 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070087#define _ALT_E 0x01
88#define _ALT_O 0x02
89#define _LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090
91
Glenn Kastenb138e4f2011-01-09 10:33:23 -080092struct century_relyear {
93 int century;
94 int relyear;
95};
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070096static int _conv_num(const unsigned char **, int *, int, int);
Glenn Kastenb138e4f2011-01-09 10:33:23 -080097static unsigned char *_strptime(const unsigned char *, const char *, struct tm *,
98 struct century_relyear *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
100
101char *
102strptime(const char *buf, const char *fmt, struct tm *tm)
103{
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800104 struct century_relyear cr;
105 cr.century = TM_YEAR_BASE;
106 cr.relyear = -1;
107 return (char*)(_strptime((const unsigned char*)buf, fmt, tm, &cr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108}
109
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700110static unsigned char *
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800111_strptime(const unsigned char *buf, const char *fmt, struct tm *tm, struct century_relyear *cr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700113 unsigned char c;
114 const unsigned char *bp;
115 size_t len = 0;
116 int alt_format, i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700118 bp = (unsigned char *)buf;
119 while ((c = *fmt) != '\0') {
120 /* Clear `alternate' modifier prior to new conversion. */
121 alt_format = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700123 /* Eat up white-space. */
124 if (isspace(c)) {
125 while (isspace(*bp))
126 bp++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700128 fmt++;
129 continue;
130 }
131
132 if ((c = *fmt++) != '%')
133 goto literal;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134
135
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700136again: switch (c = *fmt++) {
137 case '%': /* "%%" is converted to "%". */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138literal:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700139 if (c != *bp++)
140 return (NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700142 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700144 /*
145 * "Alternative" modifiers. Just set the appropriate flag
146 * and start over again.
147 */
148 case 'E': /* "%E?" alternative conversion modifier. */
149 _LEGAL_ALT(0);
150 alt_format |= _ALT_E;
151 goto again;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700153 case 'O': /* "%O?" alternative conversion modifier. */
154 _LEGAL_ALT(0);
155 alt_format |= _ALT_O;
156 goto again;
157
158 /*
159 * "Complex" conversion rules, implemented through recursion.
160 */
161 case 'c': /* Date and time, using the locale's format. */
162 _LEGAL_ALT(_ALT_E);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800163 if (!(bp = _strptime(bp, _ctloc(d_t_fmt), tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700164 return (NULL);
165 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700167 case 'D': /* The date as "%m/%d/%y". */
168 _LEGAL_ALT(0);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800169 if (!(bp = _strptime(bp, "%m/%d/%y", tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700170 return (NULL);
171 break;
172
173 case 'R': /* The time as "%H:%M". */
174 _LEGAL_ALT(0);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800175 if (!(bp = _strptime(bp, "%H:%M", tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700176 return (NULL);
177 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700179 case 'r': /* The time as "%I:%M:%S %p". */
180 _LEGAL_ALT(0);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800181 if (!(bp = _strptime(bp, "%I:%M:%S %p", tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700182 return (NULL);
183 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700185 case 'T': /* The time as "%H:%M:%S". */
186 _LEGAL_ALT(0);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800187 if (!(bp = _strptime(bp, "%H:%M:%S", tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700188 return (NULL);
189 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700191 case 'X': /* The time, using the locale's format. */
192 _LEGAL_ALT(_ALT_E);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800193 if (!(bp = _strptime(bp, _ctloc(t_fmt), tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700194 return (NULL);
195 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700197 case 'x': /* The date, using the locale's format. */
198 _LEGAL_ALT(_ALT_E);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800199 if (!(bp = _strptime(bp, _ctloc(d_fmt), tm, cr)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700200 return (NULL);
201 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700203 /*
204 * "Elementary" conversion rules.
205 */
206 case 'A': /* The day of week, using the locale's form. */
207 case 'a':
208 _LEGAL_ALT(0);
209 for (i = 0; i < 7; i++) {
210 /* Full name. */
211 len = strlen(_ctloc(day[i]));
212 if (strncasecmp(_ctloc(day[i]), (const char*)bp, len) == 0)
213 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700215 /* Abbreviated name. */
216 len = strlen(_ctloc(abday[i]));
217 if (strncasecmp(_ctloc(abday[i]), (const char*)bp, len) == 0)
218 break;
219 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700221 /* Nothing matched. */
222 if (i == 7)
223 return (NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700225 tm->tm_wday = i;
226 bp += len;
227 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700229 case 'B': /* The month, using the locale's form. */
230 case 'b':
231 case 'h':
232 _LEGAL_ALT(0);
233 for (i = 0; i < 12; i++) {
234 /* Full name. */
235 len = strlen(_ctloc(mon[i]));
236 if (strncasecmp(_ctloc(mon[i]), (const char*)bp, len) == 0)
237 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700239 /* Abbreviated name. */
240 len = strlen(_ctloc(abmon[i]));
241 if (strncasecmp(_ctloc(abmon[i]), (const char*)bp, len) == 0)
242 break;
243 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700245 /* Nothing matched. */
246 if (i == 12)
247 return (NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700249 tm->tm_mon = i;
250 bp += len;
251 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700253 case 'C': /* The century number. */
254 _LEGAL_ALT(_ALT_E);
255 if (!(_conv_num(&bp, &i, 0, 99)))
256 return (NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800258 cr->century = i * 100;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700259 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700261 case 'd': /* The day of month. */
262 case 'e':
263 _LEGAL_ALT(_ALT_O);
264 if (!(_conv_num(&bp, &tm->tm_mday, 1, 31)))
265 return (NULL);
266 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700268 case 'k': /* The hour (24-hour clock representation). */
269 _LEGAL_ALT(0);
270 /* FALLTHROUGH */
271 case 'H':
272 _LEGAL_ALT(_ALT_O);
273 if (!(_conv_num(&bp, &tm->tm_hour, 0, 23)))
274 return (NULL);
275 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700277 case 'l': /* The hour (12-hour clock representation). */
278 _LEGAL_ALT(0);
279 /* FALLTHROUGH */
280 case 'I':
281 _LEGAL_ALT(_ALT_O);
282 if (!(_conv_num(&bp, &tm->tm_hour, 1, 12)))
283 return (NULL);
284 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700286 case 'j': /* The day of year. */
287 _LEGAL_ALT(0);
288 if (!(_conv_num(&bp, &tm->tm_yday, 1, 366)))
289 return (NULL);
290 tm->tm_yday--;
291 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700293 case 'M': /* The minute. */
294 _LEGAL_ALT(_ALT_O);
295 if (!(_conv_num(&bp, &tm->tm_min, 0, 59)))
296 return (NULL);
297 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800298
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700299 case 'm': /* The month. */
300 _LEGAL_ALT(_ALT_O);
301 if (!(_conv_num(&bp, &tm->tm_mon, 1, 12)))
302 return (NULL);
303 tm->tm_mon--;
304 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700306 case 'p': /* The locale's equivalent of AM/PM. */
307 _LEGAL_ALT(0);
308 /* AM? */
309 len = strlen(_ctloc(am_pm[0]));
310 if (strncasecmp(_ctloc(am_pm[0]), (const char*)bp, len) == 0) {
311 if (tm->tm_hour > 12) /* i.e., 13:00 AM ?! */
312 return (NULL);
313 else if (tm->tm_hour == 12)
314 tm->tm_hour = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800315
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700316 bp += len;
317 break;
318 }
319 /* PM? */
320 len = strlen(_ctloc(am_pm[1]));
321 if (strncasecmp(_ctloc(am_pm[1]), (const char*)bp, len) == 0) {
322 if (tm->tm_hour > 12) /* i.e., 13:00 PM ?! */
323 return (NULL);
324 else if (tm->tm_hour < 12)
325 tm->tm_hour += 12;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700327 bp += len;
328 break;
329 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700331 /* Nothing matched. */
332 return (NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700334 case 'S': /* The seconds. */
335 _LEGAL_ALT(_ALT_O);
336 if (!(_conv_num(&bp, &tm->tm_sec, 0, 61)))
337 return (NULL);
338 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700340 case 'U': /* The week of year, beginning on sunday. */
341 case 'W': /* The week of year, beginning on monday. */
342 _LEGAL_ALT(_ALT_O);
343 /*
344 * XXX This is bogus, as we can not assume any valid
345 * information present in the tm structure at this
346 * point to calculate a real value, so just check the
347 * range for now.
348 */
349 if (!(_conv_num(&bp, &i, 0, 53)))
350 return (NULL);
351 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700353 case 'w': /* The day of week, beginning on sunday. */
354 _LEGAL_ALT(_ALT_O);
355 if (!(_conv_num(&bp, &tm->tm_wday, 0, 6)))
356 return (NULL);
357 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700359 case 'Y': /* The year. */
360 _LEGAL_ALT(_ALT_E);
361 if (!(_conv_num(&bp, &i, 0, 9999)))
362 return (NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800364 cr->relyear = -1;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700365 tm->tm_year = i - TM_YEAR_BASE;
366 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700368 case 'y': /* The year within the century (2 digits). */
369 _LEGAL_ALT(_ALT_E | _ALT_O);
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800370 if (!(_conv_num(&bp, &cr->relyear, 0, 99)))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700371 return (NULL);
372 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700374 /*
375 * Miscellaneous conversions.
376 */
377 case 'n': /* Any kind of white-space. */
378 case 't':
379 _LEGAL_ALT(0);
380 while (isspace(*bp))
381 bp++;
382 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800383
384
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700385 default: /* Unknown/unsupported conversion. */
386 return (NULL);
387 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800388
389
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700390 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700392 /*
393 * We need to evaluate the two digit year spec (%y)
394 * last as we can get a century spec (%C) at any time.
395 */
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800396 if (cr->relyear != -1) {
397 if (cr->century == TM_YEAR_BASE) {
398 if (cr->relyear <= 68)
399 tm->tm_year = cr->relyear + 2000 - TM_YEAR_BASE;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700400 else
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800401 tm->tm_year = cr->relyear + 1900 - TM_YEAR_BASE;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700402 } else {
Glenn Kastenb138e4f2011-01-09 10:33:23 -0800403 tm->tm_year = cr->relyear + cr->century - TM_YEAR_BASE;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700404 }
405 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700407 return (unsigned char*)bp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408}
409
410
411static int
412_conv_num(const unsigned char **buf, int *dest, int llim, int ulim)
413{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700414 int result = 0;
415 int rulim = ulim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700417 if (**buf < '0' || **buf > '9')
418 return (0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700420 /* we use rulim to break out of the loop when we run out of digits */
421 do {
422 result *= 10;
423 result += *(*buf)++ - '0';
424 rulim /= 10;
425 } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800426
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700427 if (result < llim || result > ulim)
428 return (0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700430 *dest = result;
431 return (1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800432}