File: sscanf.c

package info (click to toggle)
libamplsolver 0~20190702-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: ansic: 40,472; asm: 76; sh: 72; fortran: 51; makefile: 16
file content (162 lines) | stat: -rw-r--r-- 3,327 bytes parent folder | download | duplicates (4)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/****************************************************************
Copyright (C) 1997 Lucent Technologies
All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/

/* This is for compilers (like MetaWare High C) whose sscanf is broken.
 * It implements only the relevant subset of sscanf.
 * With sensible compilers, you can omit sscanf.o
 * if you add -DSscanf=sscanf to CFLAGS (in the makefile).
 */

#ifdef KR_headers
#include "varargs.h"
#else
#include "stddef.h"
#include "stdarg.h"
#include "stdlib.h"
#endif

#include "arith.h" /* for NO_SSIZE_T */
#include "stdio1.h"
#include "string.h"

#ifndef Stderr
extern FILE *Stderr;
#endif

#ifdef KR_headers
#ifndef size_t__
#define size_t int
#define size_t__
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

 static void
bad
#ifdef KR_headers
(fmt) char *fmt;
#else
(const char *fmt)
#endif
{
	fprintf(Stderr, "bad fmt in Sscanf, starting with \"%s\"\n", fmt);
	exit(1);
	}

 int
Sscanf
#ifdef KR_headers
	(va_alist)
 va_dcl
#else
	(char *s, const char *fmt, ...)
#endif
{
	char *s0;
	va_list ap;
	long L, *Lp;
	int c, i, *ip, rc, sgn;
	ssize_t Ll, *Llp;

#ifdef KR_headers
	char *fmt, *s;
	va_start(ap);
	s = va_arg(ap, char*);
	fmt = va_arg(ap, char*);
#else
	va_start(ap, fmt);
#endif
	rc = 0;
	for(;;) {
		for(;;) {
			switch(i = *(unsigned char *)fmt++) {
				case 0:
					goto done;
				case '%':
					break;
				default:
					if (i <= ' ') {
						while(*s <= ' ')
							if (!*s++)
								return rc;
						}
					else if (*s++ != i)
						return rc;
					continue;
				}
			break;
			}
		switch(*fmt++) {
			case 'l':
				if (*fmt != 'd')
					bad(fmt);
				fmt++;
				Lp = va_arg(ap, long*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*Lp = L;
					continue;
					}
				return rc;
			case 'd':
				ip = va_arg(ap, int*);
				L = strtol(s0 = s, &s, 10);
				if (s > s0) {
					rc++;
					*ip = (int)L;
					continue;
					}
				return rc;
			case 'D':
				Llp = va_arg(ap, ssize_t*);
				sgn = 0;
				c = *s;
				if (c == '-') {
					sgn = 1;
					c = *++s;
					}
				if (c < '0' || c > '9')
					return rc;
				Ll = c - '0';
				while((c = *++s) >= '0' && c <= '9')
					Ll = 10*Ll + c - '0';
				++rc;
				if (sgn)
					Ll = -Ll;
				*Llp = Ll;
				continue;
			default:
				bad(fmt);
			}
		}
 done:
	return rc;
	}
#ifdef __cplusplus
	}
#endif