]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sscanf.c
xnu-123.5.tar.gz
[apple/xnu.git] / osfmk / kern / sscanf.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_FREE_COPYRIGHT@
24 */
25/*
26 * HISTORY
27 *
28 * Revision 1.1.1.1 1998/09/22 21:05:32 wsanchez
29 * Import of Mac OS X kernel (~semeria)
30 *
31 * Revision 1.1.1.1 1998/03/07 02:25:56 wsanchez
32 * Import of OSF Mach kernel (~mburg)
33 *
34 * Revision 1.1.8.2 1997/02/12 12:52:33 stephen
35 * New file, reimplemented to be sure of copyright status
36 * Initially only supports character matching and '%d'
37 * [1997/02/12 12:43:09 stephen]
38 *
39 * $EndLog$
40 */
41
42#include <stdarg.h>
43#include <kern/misc_protos.h>
44
45#define isdigit(c) ((unsigned) ((c) - '0') < 10U)
46
47/*
48 * Scan items from a string in accordance with a format. This is much
49 * simpler than the C standard function: it only recognises %d without a
50 * field width, and does not treat space in the format string or the
51 * input any differently from other characters. The return value is the
52 * number of characters from the input string that were successfully
53 * scanned, not the number of format items matched as in standard sscanf.
54 * e.mcmanus@opengroup.org, 12 Feb 97
55 */
56int
57sscanf(const char *str, const char *format, ...)
58{
59 const char *start = str;
60 va_list args;
61
62 va_start(args, format);
63 for ( ; *format != '\0'; format++) {
64 if (*format == '%' && format[1] == 'd') {
65 int positive;
66 int value;
67 int *valp;
68
69 if (*str == '-') {
70 positive = 0;
71 str++;
72 } else
73 positive = 1;
74 if (!isdigit(*str))
75 break;
76 value = 0;
77 do {
78 value = (value * 10) - (*str - '0');
79 str++;
80 } while (isdigit(*str));
81 if (positive)
82 value = -value;
83 valp = va_arg(args, int *);
84 *valp = value;
85 format++;
86 } else if (*format == *str) {
87 str++;
88 } else
89 break;
90 }
91 va_end(args);
92 return str - start;
93}