FreeBSD/Linux Kernel Cross Reference
sys/kern/sscanf.c
1 /*
2 * Mach Operating System
3 * Copyright (c) 1993 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * HISTORY
28 * $Log: sscanf.c,v $
29 * Revision 2.4 93/11/17 17:24:17 dbg
30 * Converted to stdarg.
31 * [93/10/28 dbg]
32 *
33 * Revision 2.3 93/03/26 17:54:53 mrt
34 * Lint.
35 * [93/03/23 af]
36 *
37 * Revision 2.2 93/03/09 10:55:32 danner
38 * Created.
39 * [93/03/06 14:35:41 af]
40 *
41 */
42 /*
43 * File: sscanf.c
44 * Author: Alessandro Forin, Carnegie Mellon University
45 * Date: 3/93
46 *
47 * Parse trivially simple strings
48 */
49
50 #include <mach/std_types.h>
51 #include <sys/stdarg.h>
52 #include <kern/kern_io.h>
53
54 /*
55 * All I need is a miracle ...
56 * to keep this from growing like all the other sscanf!
57 */
58 int
59 sscanf(
60 const char *input,
61 const char *fmt,
62 ...)
63 {
64 va_list vp;
65 register int c;
66 long n;
67 boolean_t neg;
68 const unsigned char *start = input;
69
70 va_start(vp, fmt);
71
72 while ((c = *fmt++) != '\0') {
73
74 if (c != '%' && c == *input) {
75 input++;
76 continue;
77 }
78
79 if (c != '%')
80 break; /* mismatch */
81
82 c = *fmt++;
83
84 switch (c) {
85
86 case 'd':
87 n = 0;
88 c = *input++;
89
90 neg = c == '-';
91 if (neg) c = *input++;
92
93 while (c >= '' && c <= '9') {
94 n = n * 10 + (c - '');
95 c = *input++;
96 }
97 input--; /* retract lookahead */
98
99 if (neg) n = -n;
100
101 /* done, store it away */
102 {
103 int *p = va_arg(vp, int *);
104 *p = n;
105 }
106
107 break;
108
109 default:
110 break;
111 }
112 }
113
114 va_end(vp);
115
116 return (vm_offset_t)input - (vm_offset_t)start;
117 }
Cache object: 9eefc592b7d5df4cf4bd8cf25670bb75
|