-
Notifications
You must be signed in to change notification settings - Fork 6
/
missing.c
200 lines (170 loc) · 3.07 KB
/
missing.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/********************************************
missing.c
copyright 2009, Thomas E. Dickey
copyright 1995, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*
* $MawkId: missing.c,v 1.3 2009/12/13 19:43:23 Jonathan.Nieder Exp $
* @Log: missing.c,v @
* Revision 1.2 1995/06/03 09:31:11 mike
* handle strchr(s,0) correctly
*
*/
#include "nstd.h"
#include "scancode.h"
#ifdef NO_STRCHR
#undef strchr
char *
strchr(const char *s, int c)
{
if (c == 0)
return (char *) (s + strlen(s));
while (*s) {
if (*s == c)
return (char *) s;
s++;
}
return (char *) 0;
}
char *
strrchr(const char *s, int c)
{
char *ret = (char *) 0;
if (c == 0)
return s + strlen(s);
while (*s) {
if (*s == c)
ret = s;
s++;
}
return ret;
}
#endif /* NO_STRCHR */
#ifdef NO_STRERROR
extern int sys_nerr;
extern char *sys_errlist[];
char *
strerror(int n)
{
return n > 0 & n < sys_nerr ? sys_errlist[n] : "";
}
#endif
#ifdef NO_MEMCPY
PTR
memcpy(void *t, const void *s, size_t n)
{
char *tt = t;
const char *ss = (const char *) s;
while (n > 0) {
n--;
*tt++ = *ss++;
}
return t;
}
int
memcmp(const void *t, const void *s, size_t n)
{
char *tt = t;
char *ss = s;
while (n > 0) {
if (*tt < *ss)
return -1;
if (*tt > *ss)
return 1;
tt++;
ss++;
n--;
}
return 0;
}
PTR
memset(void *t, int c, size_t n)
{
char *tt = (char *) t;
while (n > 0) {
n--;
*tt++ = c;
}
return t;
}
#endif /* NO_MEMCPY */
#ifdef NO_STRTOD
/* don't use this unless you really don't have strtod() because
(1) its probably slower than your real strtod()
(2) atof() may call the real strtod()
*/
double
strtod(const char *s, char **endptr)
{
register unsigned char *p;
int flag;
double atof();
if (endptr) {
p = (unsigned char *) s;
flag = 0;
while (*p == ' ' || *p == '\t')
p++;
if (*p == '-' || *p == '+')
p++;
while (scan_code[*p] == SC_DIGIT) {
flag++;
p++;
}
if (*p == '.') {
p++;
while (scan_code[*p] == SC_DIGIT) {
flag++;
p++;
}
}
/* done with number part */
if (flag == 0) { /* no number part */
*endptr = s;
return 0.0;
} else
*endptr = (char *) p;
/* now look for exponent */
if (*p == 'e' || *p == 'E') {
flag = 0;
p++;
if (*p == '-' || *p == '+')
p++;
while (scan_code[*p] == SC_DIGIT) {
flag++;
p++;
}
if (flag)
*endptr = (char *) p;
}
}
return atof(s);
}
#endif /* no strtod() */
#ifdef NO_FMOD
#ifdef SW_FP_CHECK /* this is V7 and XNX23A specific */
double
fmod(double x, double y)
{
double modf();
double dtmp, ipart;
clrerr();
dtmp = x / y;
fpcheck();
modf(dtmp, &ipart);
return x - ipart * y;
}
#else
double
fmod(double x, double y)
{
double modf();
double ipart;
modf(x / y, &ipart);
return x - ipart * y;
}
#endif
#endif /* NO_FMOD */