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
|
/******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation
* All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the BSD License
* which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/bsd-license.php
*
* Contributors:
* IBM Corporation - initial implementation
*****************************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
static void
_scanf(const char **buffer, const char *fmt, va_list *ap)
{
int i;
int length = 0;
fmt++;
while(*fmt != '\0') {
char tbuf[256];
switch(*fmt) {
case 'd':
case 'i':
if(length == 0) length = 256;
for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
tbuf[i] = **buffer;
*buffer += 1;
}
tbuf[i] = '\0';
*(va_arg(*ap, int *)) = strtol(tbuf, NULL, 10);
break;
case 'X':
case 'x':
if(length == 0) length = 256;
for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
tbuf[i] = **buffer;
*buffer += 1;
}
tbuf[i] = '\0';
*(va_arg(*ap, int *)) = strtol(tbuf, NULL, 16);
break;
case 'O':
case 'o':
if(length == 0) length = 256;
for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
tbuf[i] = **buffer;
*buffer += 1;
}
tbuf[i] = '\0';
*(va_arg(*ap, int *)) = strtol(tbuf, NULL, 8);
break;
case 'c':
*(va_arg(*ap, char *)) = **buffer;
*buffer += 1;
if(length > 1)
for(i = 1; i < length; i++)
*buffer += 1;
break;
case 's':
if(length == 0) length = 256;
for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
tbuf[i] = **buffer;
*buffer += 1;
}
tbuf[i] = '\0';
strcpy(va_arg(*ap, char *), tbuf);
break;
default:
if(*fmt >= '0' && *fmt <= '9')
length += *fmt - '0';
break;
}
fmt++;
}
}
int
vsscanf(const char *buffer, const char *fmt, va_list ap)
{
while(*fmt != '\0') {
if(*fmt == '%') {
char formstr[20];
int i=0;
do {
formstr[i] = *fmt;
fmt++;
i++;
} while(!(*fmt == 'd' || *fmt == 'i' || *fmt == 'x' || *fmt == 'X'
|| *fmt == 'p' || *fmt == 'c' || *fmt == 's' || *fmt == '%'
|| *fmt == 'O' || *fmt == 'o' ));
formstr[i++] = *fmt;
formstr[i] = '\0';
if(*fmt != '%') {
while(*buffer == ' ' || *buffer == '\t' || *buffer == '\n')
buffer++;
_scanf(&buffer, formstr, &ap);
}
}
fmt++;
}
return 0;
}
|