|
|
1.1 root 1: /******************************************************************************
2: * Copyright (c) 2004, 2008 IBM Corporation
3: * All rights reserved.
4: * This program and the accompanying materials
5: * are made available under the terms of the BSD License
6: * which accompanies this distribution, and is available at
7: * http://www.opensource.org/licenses/bsd-license.php
8: *
9: * Contributors:
10: * IBM Corporation - initial implementation
11: *****************************************************************************/
12:
13: #include "stdio.h"
14: #include "stdlib.h"
15: #include "string.h"
16:
17:
18: static void
19: _scanf(const char **buffer, const char *fmt, va_list *ap)
20: {
21: int i;
22: int length = 0;
23:
24: fmt++;
25:
26: while(*fmt != '\0') {
27:
28: char tbuf[256];
29:
30: switch(*fmt) {
31: case 'd':
32: case 'i':
33: if(length == 0) length = 256;
34:
35: for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
36: tbuf[i] = **buffer;
37: *buffer += 1;
38: }
39: tbuf[i] = '\0';
40:
41: *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 10);
42: break;
43: case 'X':
44: case 'x':
45: if(length == 0) length = 256;
46:
47: for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
48: tbuf[i] = **buffer;
49: *buffer += 1;
50: }
51: tbuf[i] = '\0';
52:
53: *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 16);
54: break;
55: case 'O':
56: case 'o':
57: if(length == 0) length = 256;
58:
59: for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
60: tbuf[i] = **buffer;
61: *buffer += 1;
62: }
63: tbuf[i] = '\0';
64:
65: *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 8);
66: break;
67: case 'c':
68: *(va_arg(*ap, char *)) = **buffer;
69: *buffer += 1;
70: if(length > 1)
71: for(i = 1; i < length; i++)
72: *buffer += 1;
73: break;
74: case 's':
75: if(length == 0) length = 256;
76:
77: for(i = 0; **buffer != ' ' && **buffer != '\t' && **buffer != '\n' && i < length; i++) {
78: tbuf[i] = **buffer;
79: *buffer += 1;
80: }
81:
82: tbuf[i] = '\0';
83:
84: strcpy(va_arg(*ap, char *), tbuf);
85: break;
86: default:
87: if(*fmt >= '0' && *fmt <= '9')
88: length += *fmt - '0';
89: break;
90: }
91: fmt++;
92: }
93:
94: }
95:
96:
97: int
98: vsscanf(const char *buffer, const char *fmt, va_list ap)
99: {
100:
101: while(*fmt != '\0') {
102:
103: if(*fmt == '%') {
104:
105: char formstr[20];
106: int i=0;
107:
108: do {
109: formstr[i] = *fmt;
110: fmt++;
111: i++;
112: } while(!(*fmt == 'd' || *fmt == 'i' || *fmt == 'x' || *fmt == 'X'
113: || *fmt == 'p' || *fmt == 'c' || *fmt == 's' || *fmt == '%'
114: || *fmt == 'O' || *fmt == 'o' ));
115: formstr[i++] = *fmt;
116: formstr[i] = '\0';
117: if(*fmt != '%') {
118: while(*buffer == ' ' || *buffer == '\t' || *buffer == '\n')
119: buffer++;
120: _scanf(&buffer, formstr, &ap);
121: }
122:
123: }
124:
125: fmt++;
126:
127: }
128:
129: return 0;
130: }
131:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.