-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmsstdlb.c
516 lines (454 loc) · 18.6 KB
/
cmsstdlb.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**************************************************************************************************/
/* CMSSTDLB.C: Native CMS implementation of STDLIB.H. */
/* */
/* */
/* Robert O'Hara, Redmond Washington, July 2010. */
/* */
/* Based code written by Paul Edwards and Dave Wade. */
/* Released to the public domain. */
/**************************************************************************************************/
#include <cmsruntm.h>
#include <cmssys.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
static unsigned long myseed = 1; /* seed for random number generator */
void
__abort(void)
/**************************************************************************************************/
/* void __abort(void) */
/**************************************************************************************************/
{
raise(SIGABRT);
}
/* end of abort */
#ifdef abs
#undef abs
#endif
int
abs(int j)
/**************************************************************************************************/
/* int abs(int j) */
/**************************************************************************************************/
{
if (j < 0) j = -j;
return j;
} /* end of abs */
int atexit(void (*func)(void))
/**************************************************************************************************/
/* int atexit(void (* func)(void)) */
/**************************************************************************************************/
{
int x;
GCCCRAB *theCRAB = GETGCCCRAB();
for (x = 0; x < __NATEXIT; x++) {
if (theCRAB->userexits[x] == 0) {
theCRAB->userexits[x] = func;
return 0;
}
}
return -1;
}
double
atof(const char *nptr)
/**************************************************************************************************/
/* double atof(const char *nptr) */
/**************************************************************************************************/
{
return (strtod(nptr, (char **) NULL));
}
int
atoi(const char *nptr)
/**************************************************************************************************/
/* int atoi(const char * nptr) */
/**************************************************************************************************/
{
return ((int) strtol(nptr, (char **) NULL, 10));
}
long int
atol(const char *nptr)
/**************************************************************************************************/
/* long int atol(const char * nptr) */
/**************************************************************************************************/
{
return (strtol(nptr, (char **) NULL, 10));
}
void *
bsearch(const void *key, const void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
/**************************************************************************************************/
/* void * bsearch(const void * key, const void * base, size_t nmemb, size_t size, */
/* int (* compar) (const void *, const void *)) */
/**************************************************************************************************/
{
size_t try;
int res;
const void *ptr;
while (nmemb > 0) {
try = nmemb / 2;
ptr = (void *) ((char *) base + try * size);
res = compar(ptr, key);
if (res == 0) return ((void *) ptr);
else if (res < 0) {
nmemb = nmemb - try - 1;
base = (const void *) ((const char *) ptr + size);
} else nmemb = try;
}
return NULL;
}
div_t
div(int numer, int denom)
/**************************************************************************************************/
/* div_t div(int numer, int denom) */
/**************************************************************************************************/
{
div_t x;
x.quot = numer / denom;
x.rem = numer % denom;
return x;
}
/**************************************************************************************************/
/* void exit_stage2(int status) */
/* This is called under the auxstack so that the memory system can be shutdown */
/**************************************************************************************************/
static void exit_stage2(int status) {
char messagebuffer[100];
/* Deallocate Stack */
lessstak(GETGCCCRAB()->dynamicstack);
free(GETGCCCRAB()->dynamicstack);
/* Shutdown dlmalloc */
struct mallinfo memoryinfo;
memoryinfo = mallinfo();
dest_msp();
size_t leaked = memoryinfo.uordblks - GETGCCCRAB()->startmemoryusage;
if (leaked) {
if (GETGCCCRAB()->debug) {
sprintf(messagebuffer, "WARNING: MEMORY FREED (%d BYTES LEAKED)",
leaked);
CMSconsoleWrite(messagebuffer, CMS_EDIT);
}
}
GETGCCCRAB()->exitfunc(status);
}
void
/**************************************************************************************************/
/* void exit(int status) */
/**************************************************************************************************/
exit(int status) {
int x;
GCCCRAB *theCRAB = GETGCCCRAB();
/* Call Exit functions */
for (x = __NATEXIT - 1; x >= 0; x--) {
if (theCRAB->userexits[x]) {
(theCRAB->userexits[x])();
}
}
/* Close files */
_clfiles();
/* Free Process Global Memory */
if (theCRAB->process_global) free(theCRAB->process_global);
/* Caltype 5 - int return to string assist */
if ((theCRAB->calltype == 5) && !theCRAB->evalblok) {
CMSreturnvalint(status);
status = 0;
}
/* Free argument structures */
if (theCRAB->argv) free(theCRAB->argv);
if (theCRAB->argbuffer) free(theCRAB->argbuffer);
/* Call exit_stage2 under the aux stack */
__CLVSTK(theCRAB->auxstack, exit_stage2, status);
}
char *getenv(const char *name)
/**************************************************************************************************/
/* char * getenv(const char *name) */
/**************************************************************************************************/
{
return NULL;
}
#ifdef labs
#undef labs
#endif
long int labs(long int j)
/**************************************************************************************************/
/* long int labs(long int j) */
/**************************************************************************************************/
{
if (j < 0) j = -j;
return j;
}
ldiv_t ldiv(long int numer, long int denom)
/**************************************************************************************************/
/* ldiv_t ldiv(long int numer, long int denom) */
/**************************************************************************************************/
{
ldiv_t x;
x.quot = numer / denom;
x.rem = numer % denom;
return x;
}
int
mblen(const char *s, size_t n)
/**************************************************************************************************/
/* int mblen(const char * s, size_t n) */
/**************************************************************************************************/
{
if (s == NULL) return 0;
if (n == 1) return 1;
else return -1;
}
size_t
mbstowcs(wchar_t *pwcs, const char *s, size_t n)
/**************************************************************************************************/
/* size_t mbstowcs(wchar_t * pwcs, const char * s, size_t n) */
/**************************************************************************************************/
{
strncpy((char *) pwcs, s, n);
if (strlen(s) >= n) return n;
return strlen((char *) pwcs);
}
int
mbtowc(wchar_t *pwc, const char *s, size_t n)
/**************************************************************************************************/
/* int mbtowc(wchar_t * pwc, const char * s, size_t n) */
/**************************************************************************************************/
{
if (s == NULL) return 0;
if (n == 1) {
if (pwc != NULL) *pwc = *s;
return 1;
} else
return -1;
}
void
qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
/**************************************************************************************************/
/* void qsort(void * base, size_t nmemb, size_t size, int (* compar)(const void *, const void *)) */
/* */
/* This qsort function does a little trick: to reduce stackspace it iterates the larger interval */
/* instead of doing the recursion on both intervals. So stackspace is limited to */
/* 32 * stack_for_1_iteration = */
/* 32 * 4 * (4 arguments + 1 returnaddress + 11 stored registers) = 2048 bytes. */
/* This should be small enough to keep everyone happy. */
/**************************************************************************************************/
{
char *base2 = (char *) base;
size_t i, a, b, c;
while (nmemb > 1) {
a = 0;
b = nmemb - 1;
c = (a + b) /
2; /* middle element */
for (;;) {
while ((*compar)(&base2[size * c], &base2[size * a]) > 0)
a++; /* look for one >= middle */
while ((*compar)(&base2[size * c], &base2[size * b]) < 0)
b--; /* look for one <= middle */
if (a >= b)
break; /* we found no pair */
for (i = 0; i <
size; i++) { /* swap them */
char tmp = base2[size * a + i];
base2[size * a + i] = base2[size * b + i];
base2[size * b + i] = tmp;
}
if (c == a)
c = b; /* keep track of middle element */
else if (c == b) c = a;
a++; /* these two are already sorted */
b--;
} /* a points to first element of right interval now (b to last of left) */
b++;
if (b < nmemb -
b) { /* do recursion on smaller interval and iteration on larger one */
qsort(base2, b, size, compar);
base2 = &base2[size * b];
nmemb = nmemb - b;
} else {
qsort(&base2[size * b], nmemb - b, size, compar);
nmemb = b;
}
}
}
int
rand(void)
/**************************************************************************************************/
/* int rand(void) */
/**************************************************************************************************/
{
int ret;
#if defined(__MVS__) || defined(__CMS__)
/* This hack should be removed. It is to get around a bug in the GCC 3.2.3 MVS 3.0 optimizer. */
myseed = myseed * 1103515245UL;
ret = (int) (((myseed + 12345) >> 16) & 0x8fff);
#else
myseed = myseed * 1103515245UL + 12345;
ret = (int) ((myseed >> 16) & 0x8fff);
#endif
return ret;
}
void
srand(unsigned int seed)
/**************************************************************************************************/
/* void srand(unsigned int seed) */
/**************************************************************************************************/
{
myseed = seed;
return;
}
double
strtod(const char *nptr, char **endptr)
/**************************************************************************************************/
/* double strtod(const char * nptr, char ** endptr) */
/**************************************************************************************************/
{
double x = 0.0;
double xs = 1.0;
double es = 1.0;
double xf = 0.0;
double xd = 1.0;
while (isspace((unsigned char) *nptr)) ++nptr;
if (*nptr == '-') {
xs = -1;
nptr++;
} else if (*nptr == '+') nptr++;
while (1) {
if (isdigit((unsigned char) *nptr)) {
x = x * 10 + (*nptr - '0');
nptr++;
} else {
x = x * xs;
break;
}
}
if (*nptr == '.') {
nptr++;
while (1) {
if (isdigit((unsigned char) *nptr)) {
xf = xf * 10 + (*nptr - '0');
xd = xd * 10;
} else {
x = x + xs * (xf / xd);
break;
}
nptr++;
}
}
if ((*nptr == 'e') || (*nptr == 'E')) {
nptr++;
if (*nptr == '-') {
es = -1;
nptr++;
}
xd = 1;
xf = 0;
while (1) {
if (isdigit((unsigned char) *nptr)) {
xf = xf * 10 + (*nptr - '0');
nptr++;
} else {
while (xf > 0) {
xd *= 10;
xf--;
}
if (es < 0.0) x = x / xd;
else x = x * xd;
break;
}
}
}
if (endptr != NULL) *endptr = (char *) nptr;
return (x);
}
long int
strtol(const char *nptr, char **endptr, int base)
/**************************************************************************************************/
/* long int strtol(const char * nptr, char ** endptr, int base) */
/**************************************************************************************************/
{
unsigned long y;
long x;
int neg = 0;
while (isspace((unsigned char) *nptr)) ++nptr;
if (*nptr == '-') {
neg = 1;
nptr++;
} else if (*nptr == '+') nptr++;
y = strtoul(nptr, endptr, base);
if (neg) x = (long) -y;
else x = (long) y;
return x;
}
unsigned long int
strtoul(const char *nptr, char **endptr, int base)
/**************************************************************************************************/
/* unsigned long int strtoul(const char * nptr, char ** endptr, int base) */
/* */
/* This logic is also in vvscanf - if you update this, update that one too. */
/**************************************************************************************************/
{
unsigned long x = 0;
int undecided = 0;
if (base == 0) undecided = 1;
while (isspace((unsigned char) *nptr)) ++nptr;
while (1) {
if (isdigit((unsigned char) *nptr)) {
if (base == 0) {
if (*nptr == '0') base = 8;
else {
base = 10;
undecided = 0;
}
}
x = x * base + (*nptr - '0');
nptr++;
} else if (isalpha((unsigned char) *nptr)) {
if ((*nptr == 'X') || (*nptr == 'x')) {
if ((base == 0) || ((base == 8) && undecided)) {
base = 16;
undecided = 0;
nptr++;
} else if (base == 16)
nptr++; /* hex values are allowed to have an optional 0x */
else break;
} else if (base <= 10) break;
else {
x = x * base + (toupper((unsigned char) *nptr) - 'A') + 10;
nptr++;
}
} else break;
}
if (endptr != NULL) *endptr = (char *) nptr;
return (x);
}
int
system(const char *s)
/**************************************************************************************************/
/* int system(const char * s) */
/**************************************************************************************************/
{
return CMScommand(s, CMS_COMMAND);
}
size_t
wcstombs(char *s, const wchar_t *pwcs, size_t n)
/**************************************************************************************************/
/* size_t wcstombs(char * s, const wchar_t * pwcs, size_t n) */
/**************************************************************************************************/
{
strncpy(s, (const char *) pwcs, n);
if (strlen((const char *) pwcs) >= n) return n;
return strlen(s);
}
int
wctomb(char *s, wchar_t wchar)
/**************************************************************************************************/
/* int wctomb(char * s, wchar_t wchar) */
/**************************************************************************************************/
{
if (s != NULL) {
*s = wchar;
return 1;
} else return 0;
}