]> pd.if.org Git - pdclib/blob - functions/stdio/fscanf.c
Re-import from Subversion.
[pdclib] / functions / stdio / fscanf.c
1 /* ----------------------------------------------------------------------------
2  * $Id$
3  * ----------------------------------------------------------------------------
4  * Public Domain C Library - http://pdclib.sourceforge.net
5  * This code is Public Domain. Use, modify, and redistribute at will.
6  * --------------------------------------------------------------------------*/
7
8 int fscanf( FILE * restrict stream, const char * restrict format, ... ) { /* TODO */ };
9
10 /* PDPC code - unreviewed
11 {
12     va_list arg;
13     int ret;
14
15     va_start(arg, format);
16     ret = vvscanf(format, arg, stream, NULL);
17     va_end(arg);
18     return (ret);
19 }
20
21 static int vvscanf(const char *format, va_list arg, FILE *fp, const char *s)
22 {
23     int ch;
24     int fin = 0;
25     int cnt = 0;
26     char *cptr;
27     int *iptr;
28
29     inch();
30     while (!fin)
31     {
32         if (*format == '\0')
33         {
34             fin = 1;
35         }
36         else if (*format == '%')
37         {
38             format++;
39             if (*format == '%')
40             {
41                 if (ch != '%') return (cnt);
42                 inch();
43             }
44             else if (*format == 's')
45             {
46                 cptr = va_arg(arg, char *);
47                 *cptr++ = (char)ch;
48                 inch();
49                 while ((ch >= 0) && (!isspace(ch)))
50                 {
51                     *cptr++ = (char)ch;
52                     inch();
53                 }
54                 *cptr = '\0';
55                 if (ch < 0)
56                 {
57                     fin = 1;
58                 }
59             }
60             else if (*format == 'd')
61             {
62                 iptr = va_arg(arg, int *);
63                 if (!isdigit(ch)) return (cnt);
64                 *iptr = ch - '0';
65                 inch();
66                 while ((ch >= 0) && (isdigit(ch)))
67                 {
68                     *iptr = *iptr * 10 + (ch - '0');
69                     inch();
70                 }
71                 if (ch < 0)
72                 {
73                     fin = 1;
74                 }
75             }
76         }
77         else
78         {
79             if (ch != *format) return (cnt);
80             inch();
81         }
82     }
83     return (cnt);
84 }
85 */