Fix for a segfault, from Marc Lehmann.
[p5sagit/p5-mst-13.2.git] / ext / POSIX / POSIX.xs
1 #ifdef WIN32
2 #define _POSIX_
3 #endif
4
5 #define PERL_NO_GET_CONTEXT
6
7 #include "EXTERN.h"
8 #define PERLIO_NOT_STDIO 1
9 #include "perl.h"
10 #include "XSUB.h"
11 #if defined(PERL_OBJECT) || defined(PERL_CAPI) || defined(PERL_IMPLICIT_SYS)
12 #  undef signal
13 #  undef open
14 #  undef setmode
15 #  define open PerlLIO_open3
16 #endif
17 #include <ctype.h>
18 #ifdef I_DIRENT    /* XXX maybe better to just rely on perl.h? */
19 #include <dirent.h>
20 #endif
21 #include <errno.h>
22 #ifdef I_FLOAT
23 #include <float.h>
24 #endif
25 #ifdef I_LIMITS
26 #include <limits.h>
27 #endif
28 #include <locale.h>
29 #include <math.h>
30 #ifdef I_PWD
31 #include <pwd.h>
32 #endif
33 #include <setjmp.h>
34 #include <signal.h>
35 #include <stdarg.h>
36
37 #ifdef I_STDDEF
38 #include <stddef.h>
39 #endif
40
41 #ifdef I_UNISTD
42 #include <unistd.h>
43 #endif
44
45 /* XXX This comment is just to make I_TERMIO and I_SGTTY visible to 
46    metaconfig for future extension writers.  We don't use them in POSIX.
47    (This is really sneaky :-)  --AD
48 */
49 #if defined(I_TERMIOS)
50 #include <termios.h>
51 #endif
52 #ifdef I_STDLIB
53 #include <stdlib.h>
54 #endif
55 #include <string.h>
56 #include <sys/stat.h>
57 #include <sys/types.h>
58 #include <time.h>
59 #ifdef I_UNISTD
60 #include <unistd.h>
61 #endif
62 #ifdef MACOS_TRADITIONAL
63 #undef fdopen
64 #endif
65 #include <fcntl.h>
66
67 #ifdef HAS_TZNAME
68 #  if !defined(WIN32) && !defined(__CYGWIN__)
69 extern char *tzname[];
70 #  endif
71 #else
72 #if !defined(WIN32) || (defined(__MINGW32__) && !defined(tzname))
73 char *tzname[] = { "" , "" };
74 #endif
75 #endif
76
77 #if defined(__VMS) && !defined(__POSIX_SOURCE)
78 #  include <libdef.h>       /* LIB$_INVARG constant */
79 #  include <lib$routines.h> /* prototype for lib$ediv() */
80 #  include <starlet.h>      /* prototype for sys$gettim() */
81 #  if DECC_VERSION < 50000000
82 #    define pid_t int       /* old versions of DECC miss this in types.h */
83 #  endif
84
85 #  undef mkfifo
86 #  define mkfifo(a,b) (not_here("mkfifo"),-1)
87 #  define tzset() not_here("tzset")
88
89 #if ((__VMS_VER >= 70000000) && (__DECC_VER >= 50200000)) || (__CRTL_VER >= 70000000)
90 #    define HAS_TZNAME  /* shows up in VMS 7.0 or Dec C 5.6 */
91 #    include <utsname.h>
92 #  endif /* __VMS_VER >= 70000000 or Dec C 5.6 */
93
94    /* The POSIX notion of ttyname() is better served by getname() under VMS */
95    static char ttnambuf[64];
96 #  define ttyname(fd) (isatty(fd) > 0 ? getname(fd,ttnambuf,0) : NULL)
97
98    /* The non-POSIX CRTL times() has void return type, so we just get the
99       current time directly */
100    clock_t vms_times(struct tms *bufptr) {
101         dTHX;
102         clock_t retval;
103         /* Get wall time and convert to 10 ms intervals to
104          * produce the return value that the POSIX standard expects */
105 #  if defined(__DECC) && defined (__ALPHA)
106 #    include <ints.h>
107         uint64 vmstime;
108         _ckvmssts(sys$gettim(&vmstime));
109         vmstime /= 100000;
110         retval = vmstime & 0x7fffffff;
111 #  else
112         /* (Older hw or ccs don't have an atomic 64-bit type, so we
113          * juggle 32-bit ints (and a float) to produce a time_t result
114          * with minimal loss of information.) */
115         long int vmstime[2],remainder,divisor = 100000;
116         _ckvmssts(sys$gettim((unsigned long int *)vmstime));
117         vmstime[1] &= 0x7fff;  /* prevent overflow in EDIV */
118         _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
119 #  endif
120         /* Fill in the struct tms using the CRTL routine . . .*/
121         times((tbuffer_t *)bufptr);
122         return (clock_t) retval;
123    }
124 #  define times(t) vms_times(t)
125 #else
126 #if defined (__CYGWIN__)
127 #    define tzname _tzname
128 #endif
129 #if defined (WIN32)
130 #  undef mkfifo
131 #  define mkfifo(a,b) not_here("mkfifo")
132 #  define ttyname(a) (char*)not_here("ttyname")
133 #  define sigset_t long
134 #  define pid_t long
135 #  ifdef __BORLANDC__
136 #    define tzname _tzname
137 #  endif
138 #  ifdef _MSC_VER
139 #    define mode_t short
140 #  endif
141 #  ifdef __MINGW32__
142 #    define mode_t short
143 #    ifndef tzset
144 #      define tzset()           not_here("tzset")
145 #    endif
146 #    ifndef _POSIX_OPEN_MAX
147 #      define _POSIX_OPEN_MAX   FOPEN_MAX       /* XXX bogus ? */
148 #    endif
149 #  endif
150 #  define sigaction(a,b,c)      not_here("sigaction")
151 #  define sigpending(a)         not_here("sigpending")
152 #  define sigprocmask(a,b,c)    not_here("sigprocmask")
153 #  define sigsuspend(a)         not_here("sigsuspend")
154 #  define sigemptyset(a)        not_here("sigemptyset")
155 #  define sigaddset(a,b)        not_here("sigaddset")
156 #  define sigdelset(a,b)        not_here("sigdelset")
157 #  define sigfillset(a)         not_here("sigfillset")
158 #  define sigismember(a,b)      not_here("sigismember")
159 #else
160
161 #  ifndef HAS_MKFIFO
162 #    if defined(OS2) || defined(MACOS_TRADITIONAL)
163 #      define mkfifo(a,b) not_here("mkfifo")
164 #    else       /* !( defined OS2 ) */ 
165 #      ifndef mkfifo
166 #        define mkfifo(path, mode) (mknod((path), (mode) | S_IFIFO, 0))
167 #      endif
168 #    endif
169 #  endif /* !HAS_MKFIFO */
170
171 #  ifdef MACOS_TRADITIONAL
172 #    define ttyname(a) (char*)not_here("ttyname")
173 #    define tzset() not_here("tzset")
174 #  else
175 #    include <grp.h>
176 #    include <sys/times.h>
177 #    ifdef HAS_UNAME
178 #      include <sys/utsname.h>
179 #    endif
180 #    include <sys/wait.h>
181 #  endif
182 #  ifdef I_UTIME
183 #    include <utime.h>
184 #  endif
185 #endif /* WIN32 */
186 #endif /* __VMS */
187
188 typedef int SysRet;
189 typedef long SysRetLong;
190 typedef sigset_t* POSIX__SigSet;
191 typedef HV* POSIX__SigAction;
192 #ifdef I_TERMIOS
193 typedef struct termios* POSIX__Termios;
194 #else /* Define termios types to int, and call not_here for the functions.*/
195 #define POSIX__Termios int
196 #define speed_t int
197 #define tcflag_t int
198 #define cc_t int
199 #define cfgetispeed(x) not_here("cfgetispeed")
200 #define cfgetospeed(x) not_here("cfgetospeed")
201 #define tcdrain(x) not_here("tcdrain")
202 #define tcflush(x,y) not_here("tcflush")
203 #define tcsendbreak(x,y) not_here("tcsendbreak")
204 #define cfsetispeed(x,y) not_here("cfsetispeed")
205 #define cfsetospeed(x,y) not_here("cfsetospeed")
206 #define ctermid(x) (char *) not_here("ctermid")
207 #define tcflow(x,y) not_here("tcflow")
208 #define tcgetattr(x,y) not_here("tcgetattr")
209 #define tcsetattr(x,y,z) not_here("tcsetattr")
210 #endif
211
212 /* Possibly needed prototypes */
213 char *cuserid (char *);
214 double strtod (const char *, char **);
215 long strtol (const char *, char **, int);
216 unsigned long strtoul (const char *, char **, int);
217
218 #ifndef HAS_CUSERID
219 #define cuserid(a) (char *) not_here("cuserid")
220 #endif
221 #ifndef HAS_DIFFTIME
222 #ifndef difftime
223 #define difftime(a,b) not_here("difftime")
224 #endif
225 #endif
226 #ifndef HAS_FPATHCONF
227 #define fpathconf(f,n)  (SysRetLong) not_here("fpathconf")
228 #endif
229 #ifndef HAS_MKTIME
230 #define mktime(a) not_here("mktime")
231 #endif
232 #ifndef HAS_NICE
233 #define nice(a) not_here("nice")
234 #endif
235 #ifndef HAS_PATHCONF
236 #define pathconf(f,n)   (SysRetLong) not_here("pathconf")
237 #endif
238 #ifndef HAS_SYSCONF
239 #define sysconf(n)      (SysRetLong) not_here("sysconf")
240 #endif
241 #ifndef HAS_READLINK
242 #define readlink(a,b,c) not_here("readlink")
243 #endif
244 #ifndef HAS_SETPGID
245 #define setpgid(a,b) not_here("setpgid")
246 #endif
247 #ifndef HAS_SETSID
248 #define setsid() not_here("setsid")
249 #endif
250 #ifndef HAS_STRCOLL
251 #define strcoll(s1,s2) not_here("strcoll")
252 #endif
253 #ifndef HAS_STRTOD
254 #define strtod(s1,s2) not_here("strtod")
255 #endif
256 #ifndef HAS_STRTOL
257 #define strtol(s1,s2,b) not_here("strtol")
258 #endif
259 #ifndef HAS_STRTOUL
260 #define strtoul(s1,s2,b) not_here("strtoul")
261 #endif
262 #ifndef HAS_STRXFRM
263 #define strxfrm(s1,s2,n) not_here("strxfrm")
264 #endif
265 #ifndef HAS_TCGETPGRP
266 #define tcgetpgrp(a) not_here("tcgetpgrp")
267 #endif
268 #ifndef HAS_TCSETPGRP
269 #define tcsetpgrp(a,b) not_here("tcsetpgrp")
270 #endif
271 #ifndef HAS_TIMES
272 #define times(a) not_here("times")
273 #endif
274 #ifndef HAS_UNAME
275 #define uname(a) not_here("uname")
276 #endif
277 #ifndef HAS_WAITPID
278 #define waitpid(a,b,c) not_here("waitpid")
279 #endif
280
281 #ifndef HAS_MBLEN
282 #ifndef mblen
283 #define mblen(a,b) not_here("mblen")
284 #endif
285 #endif
286 #ifndef HAS_MBSTOWCS
287 #define mbstowcs(s, pwcs, n) not_here("mbstowcs")
288 #endif
289 #ifndef HAS_MBTOWC
290 #define mbtowc(pwc, s, n) not_here("mbtowc")
291 #endif
292 #ifndef HAS_WCSTOMBS
293 #define wcstombs(s, pwcs, n) not_here("wcstombs")
294 #endif
295 #ifndef HAS_WCTOMB
296 #define wctomb(s, wchar) not_here("wcstombs")
297 #endif
298 #if !defined(HAS_MBLEN) && !defined(HAS_MBSTOWCS) && !defined(HAS_MBTOWC) && !defined(HAS_WCSTOMBS) && !defined(HAS_WCTOMB)
299 /* If we don't have these functions, then we wouldn't have gotten a typedef
300    for wchar_t, the wide character type.  Defining wchar_t allows the
301    functions referencing it to compile.  Its actual type is then meaningless,
302    since without the above functions, all sections using it end up calling
303    not_here() and croak.  --Kaveh Ghazi (ghazi@noc.rutgers.edu) 9/18/94. */
304 #ifndef wchar_t
305 #define wchar_t char
306 #endif
307 #endif
308
309 #ifndef HAS_LOCALECONV
310 #define localeconv() not_here("localeconv")
311 #endif
312
313 #ifdef HAS_LONG_DOUBLE
314 #  if LONG_DOUBLESIZE > NVSIZE
315 #    undef HAS_LONG_DOUBLE  /* XXX until we figure out how to use them */
316 #  endif
317 #endif
318
319 #ifndef HAS_LONG_DOUBLE
320 #ifdef LDBL_MAX
321 #undef LDBL_MAX
322 #endif
323 #ifdef LDBL_MIN
324 #undef LDBL_MIN
325 #endif
326 #ifdef LDBL_EPSILON
327 #undef LDBL_EPSILON
328 #endif
329 #endif
330
331 static int
332 not_here(char *s)
333 {
334     croak("POSIX::%s not implemented on this architecture", s);
335     return -1;
336 }
337
338 static
339 NV
340 constant(char *name, int arg)
341 {
342     errno = 0;
343     switch (*name) {
344     case 'A':
345         if (strEQ(name, "ARG_MAX"))
346 #ifdef ARG_MAX
347             return ARG_MAX;
348 #else
349             goto not_there;
350 #endif
351         break;
352     case 'B':
353         if (strEQ(name, "BUFSIZ"))
354 #ifdef BUFSIZ
355             return BUFSIZ;
356 #else
357             goto not_there;
358 #endif
359         if (strEQ(name, "BRKINT"))
360 #ifdef BRKINT
361             return BRKINT;
362 #else
363             goto not_there;
364 #endif
365         if (strEQ(name, "B9600"))
366 #ifdef B9600
367             return B9600;
368 #else
369             goto not_there;
370 #endif
371         if (strEQ(name, "B19200"))
372 #ifdef B19200
373             return B19200;
374 #else
375             goto not_there;
376 #endif
377         if (strEQ(name, "B38400"))
378 #ifdef B38400
379             return B38400;
380 #else
381             goto not_there;
382 #endif
383         if (strEQ(name, "B0"))
384 #ifdef B0
385             return B0;
386 #else
387             goto not_there;
388 #endif
389         if (strEQ(name, "B110"))
390 #ifdef B110
391             return B110;
392 #else
393             goto not_there;
394 #endif
395         if (strEQ(name, "B1200"))
396 #ifdef B1200
397             return B1200;
398 #else
399             goto not_there;
400 #endif
401         if (strEQ(name, "B134"))
402 #ifdef B134
403             return B134;
404 #else
405             goto not_there;
406 #endif
407         if (strEQ(name, "B150"))
408 #ifdef B150
409             return B150;
410 #else
411             goto not_there;
412 #endif
413         if (strEQ(name, "B1800"))
414 #ifdef B1800
415             return B1800;
416 #else
417             goto not_there;
418 #endif
419         if (strEQ(name, "B200"))
420 #ifdef B200
421             return B200;
422 #else
423             goto not_there;
424 #endif
425         if (strEQ(name, "B2400"))
426 #ifdef B2400
427             return B2400;
428 #else
429             goto not_there;
430 #endif
431         if (strEQ(name, "B300"))
432 #ifdef B300
433             return B300;
434 #else
435             goto not_there;
436 #endif
437         if (strEQ(name, "B4800"))
438 #ifdef B4800
439             return B4800;
440 #else
441             goto not_there;
442 #endif
443         if (strEQ(name, "B50"))
444 #ifdef B50
445             return B50;
446 #else
447             goto not_there;
448 #endif
449         if (strEQ(name, "B600"))
450 #ifdef B600
451             return B600;
452 #else
453             goto not_there;
454 #endif
455         if (strEQ(name, "B75"))
456 #ifdef B75
457             return B75;
458 #else
459             goto not_there;
460 #endif
461         break;
462     case 'C':
463         if (strEQ(name, "CHAR_BIT"))
464 #ifdef CHAR_BIT
465             return CHAR_BIT;
466 #else
467             goto not_there;
468 #endif
469         if (strEQ(name, "CHAR_MAX"))
470 #ifdef CHAR_MAX
471             return CHAR_MAX;
472 #else
473             goto not_there;
474 #endif
475         if (strEQ(name, "CHAR_MIN"))
476 #ifdef CHAR_MIN
477             return CHAR_MIN;
478 #else
479             goto not_there;
480 #endif
481         if (strEQ(name, "CHILD_MAX"))
482 #ifdef CHILD_MAX
483             return CHILD_MAX;
484 #else
485             goto not_there;
486 #endif
487         if (strEQ(name, "CLK_TCK"))
488 #ifdef CLK_TCK
489             return CLK_TCK;
490 #else
491             goto not_there;
492 #endif
493         if (strEQ(name, "CLOCAL"))
494 #ifdef CLOCAL
495             return CLOCAL;
496 #else
497             goto not_there;
498 #endif
499         if (strEQ(name, "CLOCKS_PER_SEC"))
500 #ifdef CLOCKS_PER_SEC
501             return CLOCKS_PER_SEC;
502 #else
503             goto not_there;
504 #endif
505         if (strEQ(name, "CREAD"))
506 #ifdef CREAD
507             return CREAD;
508 #else
509             goto not_there;
510 #endif
511         if (strEQ(name, "CS5"))
512 #ifdef CS5
513             return CS5;
514 #else
515             goto not_there;
516 #endif
517         if (strEQ(name, "CS6"))
518 #ifdef CS6
519             return CS6;
520 #else
521             goto not_there;
522 #endif
523         if (strEQ(name, "CS7"))
524 #ifdef CS7
525             return CS7;
526 #else
527             goto not_there;
528 #endif
529         if (strEQ(name, "CS8"))
530 #ifdef CS8
531             return CS8;
532 #else
533             goto not_there;
534 #endif
535         if (strEQ(name, "CSIZE"))
536 #ifdef CSIZE
537             return CSIZE;
538 #else
539             goto not_there;
540 #endif
541         if (strEQ(name, "CSTOPB"))
542 #ifdef CSTOPB
543             return CSTOPB;
544 #else
545             goto not_there;
546 #endif
547         break;
548     case 'D':
549         if (strEQ(name, "DBL_MAX"))
550 #ifdef DBL_MAX
551             return DBL_MAX;
552 #else
553             goto not_there;
554 #endif
555         if (strEQ(name, "DBL_MIN"))
556 #ifdef DBL_MIN
557             return DBL_MIN;
558 #else
559             goto not_there;
560 #endif
561         if (strEQ(name, "DBL_DIG"))
562 #ifdef DBL_DIG
563             return DBL_DIG;
564 #else
565             goto not_there;
566 #endif
567         if (strEQ(name, "DBL_EPSILON"))
568 #ifdef DBL_EPSILON
569             return DBL_EPSILON;
570 #else
571             goto not_there;
572 #endif
573         if (strEQ(name, "DBL_MANT_DIG"))
574 #ifdef DBL_MANT_DIG
575             return DBL_MANT_DIG;
576 #else
577             goto not_there;
578 #endif
579         if (strEQ(name, "DBL_MAX_10_EXP"))
580 #ifdef DBL_MAX_10_EXP
581             return DBL_MAX_10_EXP;
582 #else
583             goto not_there;
584 #endif
585         if (strEQ(name, "DBL_MAX_EXP"))
586 #ifdef DBL_MAX_EXP
587             return DBL_MAX_EXP;
588 #else
589             goto not_there;
590 #endif
591         if (strEQ(name, "DBL_MIN_10_EXP"))
592 #ifdef DBL_MIN_10_EXP
593             return DBL_MIN_10_EXP;
594 #else
595             goto not_there;
596 #endif
597         if (strEQ(name, "DBL_MIN_EXP"))
598 #ifdef DBL_MIN_EXP
599             return DBL_MIN_EXP;
600 #else
601             goto not_there;
602 #endif
603         break;
604     case 'E':
605         switch (name[1]) {
606         case 'A':
607             if (strEQ(name, "EACCES"))
608 #ifdef EACCES
609                 return EACCES;
610 #else
611                 goto not_there;
612 #endif
613             if (strEQ(name, "EADDRINUSE"))
614 #ifdef EADDRINUSE
615                 return EADDRINUSE;
616 #else
617                 goto not_there;
618 #endif
619             if (strEQ(name, "EADDRNOTAVAIL"))
620 #ifdef EADDRNOTAVAIL
621                 return EADDRNOTAVAIL;
622 #else
623                 goto not_there;
624 #endif
625             if (strEQ(name, "EAFNOSUPPORT"))
626 #ifdef EAFNOSUPPORT
627                 return EAFNOSUPPORT;
628 #else
629                 goto not_there;
630 #endif
631             if (strEQ(name, "EAGAIN"))
632 #ifdef EAGAIN
633                 return EAGAIN;
634 #else
635                 goto not_there;
636 #endif
637             if (strEQ(name, "EALREADY"))
638 #ifdef EALREADY
639                 return EALREADY;
640 #else
641                 goto not_there;
642 #endif
643             break;
644         case 'B':
645             if (strEQ(name, "EBADF"))
646 #ifdef EBADF
647                 return EBADF;
648 #else
649                 goto not_there;
650 #endif
651             if (strEQ(name, "EBUSY"))
652 #ifdef EBUSY
653                 return EBUSY;
654 #else
655                 goto not_there;
656 #endif
657             break;
658         case 'C':
659             if (strEQ(name, "ECHILD"))
660 #ifdef ECHILD
661                 return ECHILD;
662 #else
663                 goto not_there;
664 #endif
665             if (strEQ(name, "ECHO"))
666 #ifdef ECHO
667                 return ECHO;
668 #else
669                 goto not_there;
670 #endif
671             if (strEQ(name, "ECHOE"))
672 #ifdef ECHOE
673                 return ECHOE;
674 #else
675                 goto not_there;
676 #endif
677             if (strEQ(name, "ECHOK"))
678 #ifdef ECHOK
679                 return ECHOK;
680 #else
681                 goto not_there;
682 #endif
683             if (strEQ(name, "ECHONL"))
684 #ifdef ECHONL
685                 return ECHONL;
686 #else
687                 goto not_there;
688 #endif
689             if (strEQ(name, "ECONNABORTED"))
690 #ifdef ECONNABORTED
691                 return ECONNABORTED;
692 #else
693                 goto not_there;
694 #endif
695             if (strEQ(name, "ECONNREFUSED"))
696 #ifdef ECONNREFUSED
697                 return ECONNREFUSED;
698 #else
699                 goto not_there;
700 #endif
701             if (strEQ(name, "ECONNRESET"))
702 #ifdef ECONNRESET
703                 return ECONNRESET;
704 #else
705                 goto not_there;
706 #endif
707             break;
708         case 'D':
709             if (strEQ(name, "EDEADLK"))
710 #ifdef EDEADLK
711                 return EDEADLK;
712 #else
713                 goto not_there;
714 #endif
715             if (strEQ(name, "EDESTADDRREQ"))
716 #ifdef EDESTADDRREQ
717                 return EDESTADDRREQ;
718 #else
719                 goto not_there;
720 #endif
721             if (strEQ(name, "EDOM"))
722 #ifdef EDOM
723                 return EDOM;
724 #else
725                 goto not_there;
726 #endif
727             if (strEQ(name, "EDQUOT"))
728 #ifdef EDQUOT
729                 return EDQUOT;
730 #else
731                 goto not_there;
732 #endif
733             break;
734         case 'E':
735             if (strEQ(name, "EEXIST"))
736 #ifdef EEXIST
737                 return EEXIST;
738 #else
739                 goto not_there;
740 #endif
741             break;
742         case 'F':
743             if (strEQ(name, "EFAULT"))
744 #ifdef EFAULT
745                 return EFAULT;
746 #else
747                 goto not_there;
748 #endif
749             if (strEQ(name, "EFBIG"))
750 #ifdef EFBIG
751                 return EFBIG;
752 #else
753                 goto not_there;
754 #endif
755             break;
756         case 'H':
757             if (strEQ(name, "EHOSTDOWN"))
758 #ifdef EHOSTDOWN
759                 return EHOSTDOWN;
760 #else
761                 goto not_there;
762 #endif
763             if (strEQ(name, "EHOSTUNREACH"))
764 #ifdef EHOSTUNREACH
765                 return EHOSTUNREACH;
766 #else
767                 goto not_there;
768 #endif
769             break;
770         case 'I':
771             if (strEQ(name, "EINPROGRESS"))
772 #ifdef EINPROGRESS
773                 return EINPROGRESS;
774 #else
775                 goto not_there;
776 #endif
777             if (strEQ(name, "EINTR"))
778 #ifdef EINTR
779                 return EINTR;
780 #else
781                 goto not_there;
782 #endif
783             if (strEQ(name, "EINVAL"))
784 #ifdef EINVAL
785                 return EINVAL;
786 #else
787                 goto not_there;
788 #endif
789             if (strEQ(name, "EIO"))
790 #ifdef EIO
791                 return EIO;
792 #else
793                 goto not_there;
794 #endif
795             if (strEQ(name, "EISCONN"))
796 #ifdef EISCONN
797                 return EISCONN;
798 #else
799                 goto not_there;
800 #endif
801             if (strEQ(name, "EISDIR"))
802 #ifdef EISDIR
803                 return EISDIR;
804 #else
805                 goto not_there;
806 #endif
807             break;
808         case 'L':
809             if (strEQ(name, "ELOOP"))
810 #ifdef ELOOP
811                 return ELOOP;
812 #else
813                 goto not_there;
814 #endif
815             break;
816         case 'M':
817             if (strEQ(name, "EMFILE"))
818 #ifdef EMFILE
819                 return EMFILE;
820 #else
821                 goto not_there;
822 #endif
823             if (strEQ(name, "EMLINK"))
824 #ifdef EMLINK
825                 return EMLINK;
826 #else
827                 goto not_there;
828 #endif
829             if (strEQ(name, "EMSGSIZE"))
830 #ifdef EMSGSIZE
831                 return EMSGSIZE;
832 #else
833                 goto not_there;
834 #endif
835             break;
836         case 'N':
837             if (strEQ(name, "ENETDOWN"))
838 #ifdef ENETDOWN
839                 return ENETDOWN;
840 #else
841                 goto not_there;
842 #endif
843             if (strEQ(name, "ENETRESET"))
844 #ifdef ENETRESET
845                 return ENETRESET;
846 #else
847                 goto not_there;
848 #endif
849             if (strEQ(name, "ENETUNREACH"))
850 #ifdef ENETUNREACH
851                 return ENETUNREACH;
852 #else
853                 goto not_there;
854 #endif
855             if (strEQ(name, "ENOBUFS"))
856 #ifdef ENOBUFS
857                 return ENOBUFS;
858 #else
859                 goto not_there;
860 #endif
861             if (strEQ(name, "ENOEXEC"))
862 #ifdef ENOEXEC
863                 return ENOEXEC;
864 #else
865                 goto not_there;
866 #endif
867             if (strEQ(name, "ENOMEM"))
868 #ifdef ENOMEM
869                 return ENOMEM;
870 #else
871                 goto not_there;
872 #endif
873             if (strEQ(name, "ENOPROTOOPT"))
874 #ifdef ENOPROTOOPT
875                 return ENOPROTOOPT;
876 #else
877                 goto not_there;
878 #endif
879             if (strEQ(name, "ENOSPC"))
880 #ifdef ENOSPC
881                 return ENOSPC;
882 #else
883                 goto not_there;
884 #endif
885             if (strEQ(name, "ENOTBLK"))
886 #ifdef ENOTBLK
887                 return ENOTBLK;
888 #else
889                 goto not_there;
890 #endif
891             if (strEQ(name, "ENOTCONN"))
892 #ifdef ENOTCONN
893                 return ENOTCONN;
894 #else
895                 goto not_there;
896 #endif
897             if (strEQ(name, "ENOTDIR"))
898 #ifdef ENOTDIR
899                 return ENOTDIR;
900 #else
901                 goto not_there;
902 #endif
903             if (strEQ(name, "ENOTEMPTY"))
904 #ifdef ENOTEMPTY
905                 return ENOTEMPTY;
906 #else
907                 goto not_there;
908 #endif
909             if (strEQ(name, "ENOTSOCK"))
910 #ifdef ENOTSOCK
911                 return ENOTSOCK;
912 #else
913                 goto not_there;
914 #endif
915             if (strEQ(name, "ENOTTY"))
916 #ifdef ENOTTY
917                 return ENOTTY;
918 #else
919                 goto not_there;
920 #endif
921             if (strEQ(name, "ENFILE"))
922 #ifdef ENFILE
923                 return ENFILE;
924 #else
925                 goto not_there;
926 #endif
927             if (strEQ(name, "ENODEV"))
928 #ifdef ENODEV
929                 return ENODEV;
930 #else
931                 goto not_there;
932 #endif
933             if (strEQ(name, "ENOENT"))
934 #ifdef ENOENT
935                 return ENOENT;
936 #else
937                 goto not_there;
938 #endif
939             if (strEQ(name, "ENOLCK"))
940 #ifdef ENOLCK
941                 return ENOLCK;
942 #else
943                 goto not_there;
944 #endif
945             if (strEQ(name, "ENOSYS"))
946 #ifdef ENOSYS
947                 return ENOSYS;
948 #else
949                 goto not_there;
950 #endif
951             if (strEQ(name, "ENXIO"))
952 #ifdef ENXIO
953                 return ENXIO;
954 #else
955                 goto not_there;
956 #endif
957             if (strEQ(name, "ENAMETOOLONG"))
958 #ifdef ENAMETOOLONG
959                 return ENAMETOOLONG;
960 #else
961                 goto not_there;
962 #endif
963             break;
964         case 'O':
965             if (strEQ(name, "EOF"))
966 #ifdef EOF
967                 return EOF;
968 #else
969                 goto not_there;
970 #endif
971             if (strEQ(name, "EOPNOTSUPP"))
972 #ifdef EOPNOTSUPP
973                 return EOPNOTSUPP;
974 #else
975                 goto not_there;
976 #endif
977             break;
978         case 'P':
979             if (strEQ(name, "EPERM"))
980 #ifdef EPERM
981                 return EPERM;
982 #else
983                 goto not_there;
984 #endif
985             if (strEQ(name, "EPFNOSUPPORT"))
986 #ifdef EPFNOSUPPORT
987                 return EPFNOSUPPORT;
988 #else
989                 goto not_there;
990 #endif
991             if (strEQ(name, "EPIPE"))
992 #ifdef EPIPE
993                 return EPIPE;
994 #else
995                 goto not_there;
996 #endif
997             if (strEQ(name, "EPROCLIM"))
998 #ifdef EPROCLIM
999                 return EPROCLIM;
1000 #else
1001                 goto not_there;
1002 #endif
1003             if (strEQ(name, "EPROTONOSUPPORT"))
1004 #ifdef EPROTONOSUPPORT
1005                 return EPROTONOSUPPORT;
1006 #else
1007                 goto not_there;
1008 #endif
1009             if (strEQ(name, "EPROTOTYPE"))
1010 #ifdef EPROTOTYPE
1011                 return EPROTOTYPE;
1012 #else
1013                 goto not_there;
1014 #endif
1015             break;
1016         case 'R':
1017             if (strEQ(name, "ERANGE"))
1018 #ifdef ERANGE
1019                 return ERANGE;
1020 #else
1021                 goto not_there;
1022 #endif
1023             if (strEQ(name, "EREMOTE"))
1024 #ifdef EREMOTE
1025                 return EREMOTE;
1026 #else
1027                 goto not_there;
1028 #endif
1029             if (strEQ(name, "ERESTART"))
1030 #ifdef ERESTART
1031                 return ERESTART;
1032 #else
1033                 goto not_there;
1034 #endif
1035             if (strEQ(name, "EROFS"))
1036 #ifdef EROFS
1037                 return EROFS;
1038 #else
1039                 goto not_there;
1040 #endif
1041             break;
1042         case 'S':
1043             if (strEQ(name, "ESHUTDOWN"))
1044 #ifdef ESHUTDOWN
1045                 return ESHUTDOWN;
1046 #else
1047                 goto not_there;
1048 #endif
1049             if (strEQ(name, "ESOCKTNOSUPPORT"))
1050 #ifdef ESOCKTNOSUPPORT
1051                 return ESOCKTNOSUPPORT;
1052 #else
1053                 goto not_there;
1054 #endif
1055             if (strEQ(name, "ESPIPE"))
1056 #ifdef ESPIPE
1057                 return ESPIPE;
1058 #else
1059                 goto not_there;
1060 #endif
1061             if (strEQ(name, "ESRCH"))
1062 #ifdef ESRCH
1063                 return ESRCH;
1064 #else
1065                 goto not_there;
1066 #endif
1067             if (strEQ(name, "ESTALE"))
1068 #ifdef ESTALE
1069                 return ESTALE;
1070 #else
1071                 goto not_there;
1072 #endif
1073             break;
1074         case 'T':
1075             if (strEQ(name, "ETIMEDOUT"))
1076 #ifdef ETIMEDOUT
1077                 return ETIMEDOUT;
1078 #else
1079                 goto not_there;
1080 #endif
1081             if (strEQ(name, "ETOOMANYREFS"))
1082 #ifdef ETOOMANYREFS
1083                 return ETOOMANYREFS;
1084 #else
1085                 goto not_there;
1086 #endif
1087             if (strEQ(name, "ETXTBSY"))
1088 #ifdef ETXTBSY
1089                 return ETXTBSY;
1090 #else
1091                 goto not_there;
1092 #endif
1093             break;
1094         case 'U':
1095             if (strEQ(name, "EUSERS"))
1096 #ifdef EUSERS
1097                 return EUSERS;
1098 #else
1099                 goto not_there;
1100 #endif
1101             break;
1102         case 'W':
1103             if (strEQ(name, "EWOULDBLOCK"))
1104 #ifdef EWOULDBLOCK
1105                 return EWOULDBLOCK;
1106 #else
1107                 goto not_there;
1108 #endif
1109             break;
1110         case 'X':
1111             if (strEQ(name, "EXIT_FAILURE"))
1112 #ifdef EXIT_FAILURE
1113                 return EXIT_FAILURE;
1114 #else
1115                 return 1;
1116 #endif
1117             if (strEQ(name, "EXIT_SUCCESS"))
1118 #ifdef EXIT_SUCCESS
1119                 return EXIT_SUCCESS;
1120 #else
1121                 return 0;
1122 #endif
1123             if (strEQ(name, "EXDEV"))
1124 #ifdef EXDEV
1125                 return EXDEV;
1126 #else
1127                 goto not_there;
1128 #endif
1129             break;
1130         }
1131         if (strEQ(name, "E2BIG"))
1132 #ifdef E2BIG
1133             return E2BIG;
1134 #else
1135             goto not_there;
1136 #endif
1137         break;
1138     case 'F':
1139         if (strnEQ(name, "FLT_", 4)) {
1140             if (strEQ(name, "FLT_MAX"))
1141 #ifdef FLT_MAX
1142                 return FLT_MAX;
1143 #else
1144                 goto not_there;
1145 #endif
1146             if (strEQ(name, "FLT_MIN"))
1147 #ifdef FLT_MIN
1148                 return FLT_MIN;
1149 #else
1150                 goto not_there;
1151 #endif
1152             if (strEQ(name, "FLT_ROUNDS"))
1153 #ifdef FLT_ROUNDS
1154                 return FLT_ROUNDS;
1155 #else
1156                 goto not_there;
1157 #endif
1158             if (strEQ(name, "FLT_DIG"))
1159 #ifdef FLT_DIG
1160                 return FLT_DIG;
1161 #else
1162                 goto not_there;
1163 #endif
1164             if (strEQ(name, "FLT_EPSILON"))
1165 #ifdef FLT_EPSILON
1166                 return FLT_EPSILON;
1167 #else
1168                 goto not_there;
1169 #endif
1170             if (strEQ(name, "FLT_MANT_DIG"))
1171 #ifdef FLT_MANT_DIG
1172                 return FLT_MANT_DIG;
1173 #else
1174                 goto not_there;
1175 #endif
1176             if (strEQ(name, "FLT_MAX_10_EXP"))
1177 #ifdef FLT_MAX_10_EXP
1178                 return FLT_MAX_10_EXP;
1179 #else
1180                 goto not_there;
1181 #endif
1182             if (strEQ(name, "FLT_MAX_EXP"))
1183 #ifdef FLT_MAX_EXP
1184                 return FLT_MAX_EXP;
1185 #else
1186                 goto not_there;
1187 #endif
1188             if (strEQ(name, "FLT_MIN_10_EXP"))
1189 #ifdef FLT_MIN_10_EXP
1190                 return FLT_MIN_10_EXP;
1191 #else
1192                 goto not_there;
1193 #endif
1194             if (strEQ(name, "FLT_MIN_EXP"))
1195 #ifdef FLT_MIN_EXP
1196                 return FLT_MIN_EXP;
1197 #else
1198                 goto not_there;
1199 #endif
1200             if (strEQ(name, "FLT_RADIX"))
1201 #ifdef FLT_RADIX
1202                 return FLT_RADIX;
1203 #else
1204                 goto not_there;
1205 #endif
1206             break;
1207         }
1208         if (strnEQ(name, "F_", 2)) {
1209             if (strEQ(name, "F_DUPFD"))
1210 #ifdef F_DUPFD
1211                 return F_DUPFD;
1212 #else
1213                 goto not_there;
1214 #endif
1215             if (strEQ(name, "F_GETFD"))
1216 #ifdef F_GETFD
1217                 return F_GETFD;
1218 #else
1219                 goto not_there;
1220 #endif
1221             if (strEQ(name, "F_GETFL"))
1222 #ifdef F_GETFL
1223                 return F_GETFL;
1224 #else
1225                 goto not_there;
1226 #endif
1227             if (strEQ(name, "F_GETLK"))
1228 #ifdef F_GETLK
1229                 return F_GETLK;
1230 #else
1231                 goto not_there;
1232 #endif
1233             if (strEQ(name, "F_OK"))
1234 #ifdef F_OK
1235                 return F_OK;
1236 #else
1237                 goto not_there;
1238 #endif
1239             if (strEQ(name, "F_RDLCK"))
1240 #ifdef F_RDLCK
1241                 return F_RDLCK;
1242 #else
1243                 goto not_there;
1244 #endif
1245             if (strEQ(name, "F_SETFD"))
1246 #ifdef F_SETFD
1247                 return F_SETFD;
1248 #else
1249                 goto not_there;
1250 #endif
1251             if (strEQ(name, "F_SETFL"))
1252 #ifdef F_SETFL
1253                 return F_SETFL;
1254 #else
1255                 goto not_there;
1256 #endif
1257             if (strEQ(name, "F_SETLK"))
1258 #ifdef F_SETLK
1259                 return F_SETLK;
1260 #else
1261                 goto not_there;
1262 #endif
1263             if (strEQ(name, "F_SETLKW"))
1264 #ifdef F_SETLKW
1265                 return F_SETLKW;
1266 #else
1267                 goto not_there;
1268 #endif
1269             if (strEQ(name, "F_UNLCK"))
1270 #ifdef F_UNLCK
1271                 return F_UNLCK;
1272 #else
1273                 goto not_there;
1274 #endif
1275             if (strEQ(name, "F_WRLCK"))
1276 #ifdef F_WRLCK
1277                 return F_WRLCK;
1278 #else
1279                 goto not_there;
1280 #endif
1281             break;
1282         }
1283         if (strEQ(name, "FD_CLOEXEC"))
1284 #ifdef FD_CLOEXEC
1285             return FD_CLOEXEC;
1286 #else
1287             goto not_there;
1288 #endif
1289         if (strEQ(name, "FILENAME_MAX"))
1290 #ifdef FILENAME_MAX
1291             return FILENAME_MAX;
1292 #else
1293             goto not_there;
1294 #endif
1295         break;
1296     case 'H':
1297         if (strEQ(name, "HUGE_VAL"))
1298 #if defined(USE_LONG_DOUBLE) && defined(HUGE_VALL)
1299           /* HUGE_VALL is admittedly non-POSIX but if we are using long doubles
1300            * we might as well use long doubles. --jhi */
1301             return HUGE_VALL;
1302 #endif
1303 #ifdef HUGE_VAL
1304             return HUGE_VAL;
1305 #else
1306             goto not_there;
1307 #endif
1308         if (strEQ(name, "HUPCL"))
1309 #ifdef HUPCL
1310             return HUPCL;
1311 #else
1312             goto not_there;
1313 #endif
1314         break;
1315     case 'I':
1316         if (strEQ(name, "INT_MAX"))
1317 #ifdef INT_MAX
1318             return INT_MAX;
1319 #else
1320             goto not_there;
1321 #endif
1322         if (strEQ(name, "INT_MIN"))
1323 #ifdef INT_MIN
1324             return INT_MIN;
1325 #else
1326             goto not_there;
1327 #endif
1328         if (strEQ(name, "ICANON"))
1329 #ifdef ICANON
1330             return ICANON;
1331 #else
1332             goto not_there;
1333 #endif
1334         if (strEQ(name, "ICRNL"))
1335 #ifdef ICRNL
1336             return ICRNL;
1337 #else
1338             goto not_there;
1339 #endif
1340         if (strEQ(name, "IEXTEN"))
1341 #ifdef IEXTEN
1342             return IEXTEN;
1343 #else
1344             goto not_there;
1345 #endif
1346         if (strEQ(name, "IGNBRK"))
1347 #ifdef IGNBRK
1348             return IGNBRK;
1349 #else
1350             goto not_there;
1351 #endif
1352         if (strEQ(name, "IGNCR"))
1353 #ifdef IGNCR
1354             return IGNCR;
1355 #else
1356             goto not_there;
1357 #endif
1358         if (strEQ(name, "IGNPAR"))
1359 #ifdef IGNPAR
1360             return IGNPAR;
1361 #else
1362             goto not_there;
1363 #endif
1364         if (strEQ(name, "INLCR"))
1365 #ifdef INLCR
1366             return INLCR;
1367 #else
1368             goto not_there;
1369 #endif
1370         if (strEQ(name, "INPCK"))
1371 #ifdef INPCK
1372             return INPCK;
1373 #else
1374             goto not_there;
1375 #endif
1376         if (strEQ(name, "ISIG"))
1377 #ifdef ISIG
1378             return ISIG;
1379 #else
1380             goto not_there;
1381 #endif
1382         if (strEQ(name, "ISTRIP"))
1383 #ifdef ISTRIP
1384             return ISTRIP;
1385 #else
1386             goto not_there;
1387 #endif
1388         if (strEQ(name, "IXOFF"))
1389 #ifdef IXOFF
1390             return IXOFF;
1391 #else
1392             goto not_there;
1393 #endif
1394         if (strEQ(name, "IXON"))
1395 #ifdef IXON
1396             return IXON;
1397 #else
1398             goto not_there;
1399 #endif
1400         break;
1401     case 'L':
1402         if (strnEQ(name, "LC_", 3)) {
1403             if (strEQ(name, "LC_ALL"))
1404 #ifdef LC_ALL
1405                 return LC_ALL;
1406 #else
1407                 goto not_there;
1408 #endif
1409             if (strEQ(name, "LC_COLLATE"))
1410 #ifdef LC_COLLATE
1411                 return LC_COLLATE;
1412 #else
1413                 goto not_there;
1414 #endif
1415             if (strEQ(name, "LC_CTYPE"))
1416 #ifdef LC_CTYPE
1417                 return LC_CTYPE;
1418 #else
1419                 goto not_there;
1420 #endif
1421             if (strEQ(name, "LC_MONETARY"))
1422 #ifdef LC_MONETARY
1423                 return LC_MONETARY;
1424 #else
1425                 goto not_there;
1426 #endif
1427             if (strEQ(name, "LC_NUMERIC"))
1428 #ifdef LC_NUMERIC
1429                 return LC_NUMERIC;
1430 #else
1431                 goto not_there;
1432 #endif
1433             if (strEQ(name, "LC_TIME"))
1434 #ifdef LC_TIME
1435                 return LC_TIME;
1436 #else
1437                 goto not_there;
1438 #endif
1439             break;
1440         }
1441         if (strnEQ(name, "LDBL_", 5)) {
1442             if (strEQ(name, "LDBL_MAX"))
1443 #ifdef LDBL_MAX
1444                 return LDBL_MAX;
1445 #else
1446                 goto not_there;
1447 #endif
1448             if (strEQ(name, "LDBL_MIN"))
1449 #ifdef LDBL_MIN
1450                 return LDBL_MIN;
1451 #else
1452                 goto not_there;
1453 #endif
1454             if (strEQ(name, "LDBL_DIG"))
1455 #ifdef LDBL_DIG
1456                 return LDBL_DIG;
1457 #else
1458                 goto not_there;
1459 #endif
1460             if (strEQ(name, "LDBL_EPSILON"))
1461 #ifdef LDBL_EPSILON
1462                 return LDBL_EPSILON;
1463 #else
1464                 goto not_there;
1465 #endif
1466             if (strEQ(name, "LDBL_MANT_DIG"))
1467 #ifdef LDBL_MANT_DIG
1468                 return LDBL_MANT_DIG;
1469 #else
1470                 goto not_there;
1471 #endif
1472             if (strEQ(name, "LDBL_MAX_10_EXP"))
1473 #ifdef LDBL_MAX_10_EXP
1474                 return LDBL_MAX_10_EXP;
1475 #else
1476                 goto not_there;
1477 #endif
1478             if (strEQ(name, "LDBL_MAX_EXP"))
1479 #ifdef LDBL_MAX_EXP
1480                 return LDBL_MAX_EXP;
1481 #else
1482                 goto not_there;
1483 #endif
1484             if (strEQ(name, "LDBL_MIN_10_EXP"))
1485 #ifdef LDBL_MIN_10_EXP
1486                 return LDBL_MIN_10_EXP;
1487 #else
1488                 goto not_there;
1489 #endif
1490             if (strEQ(name, "LDBL_MIN_EXP"))
1491 #ifdef LDBL_MIN_EXP
1492                 return LDBL_MIN_EXP;
1493 #else
1494                 goto not_there;
1495 #endif
1496             break;
1497         }
1498         if (strnEQ(name, "L_", 2)) {
1499             if (strEQ(name, "L_ctermid"))
1500 #ifdef L_ctermid
1501                 return L_ctermid;
1502 #else
1503                 goto not_there;
1504 #endif
1505             if (strEQ(name, "L_cuserid"))
1506 #ifdef L_cuserid
1507                 return L_cuserid;
1508 #else
1509                 goto not_there;
1510 #endif
1511             /* L_tmpnam[e] was a typo--retained for compatibility */
1512             if (strEQ(name, "L_tmpname") || strEQ(name, "L_tmpnam"))
1513 #ifdef L_tmpnam
1514                 return L_tmpnam;
1515 #else
1516                 goto not_there;
1517 #endif
1518             break;
1519         }
1520         if (strEQ(name, "LONG_MAX"))
1521 #ifdef LONG_MAX
1522             return LONG_MAX;
1523 #else
1524             goto not_there;
1525 #endif
1526         if (strEQ(name, "LONG_MIN"))
1527 #ifdef LONG_MIN
1528             return LONG_MIN;
1529 #else
1530             goto not_there;
1531 #endif
1532         if (strEQ(name, "LINK_MAX"))
1533 #ifdef LINK_MAX
1534             return LINK_MAX;
1535 #else
1536             goto not_there;
1537 #endif
1538         break;
1539     case 'M':
1540         if (strEQ(name, "MAX_CANON"))
1541 #ifdef MAX_CANON
1542             return MAX_CANON;
1543 #else
1544             goto not_there;
1545 #endif
1546         if (strEQ(name, "MAX_INPUT"))
1547 #ifdef MAX_INPUT
1548             return MAX_INPUT;
1549 #else
1550             goto not_there;
1551 #endif
1552         if (strEQ(name, "MB_CUR_MAX"))
1553 #ifdef MB_CUR_MAX
1554             return MB_CUR_MAX;
1555 #else
1556             goto not_there;
1557 #endif
1558         if (strEQ(name, "MB_LEN_MAX"))
1559 #ifdef MB_LEN_MAX
1560             return MB_LEN_MAX;
1561 #else
1562             goto not_there;
1563 #endif
1564         break;
1565     case 'N':
1566         if (strEQ(name, "NULL")) return 0;
1567         if (strEQ(name, "NAME_MAX"))
1568 #ifdef NAME_MAX
1569             return NAME_MAX;
1570 #else
1571             goto not_there;
1572 #endif
1573         if (strEQ(name, "NCCS"))
1574 #ifdef NCCS
1575             return NCCS;
1576 #else
1577             goto not_there;
1578 #endif
1579         if (strEQ(name, "NGROUPS_MAX"))
1580 #ifdef NGROUPS_MAX
1581             return NGROUPS_MAX;
1582 #else
1583             goto not_there;
1584 #endif
1585         if (strEQ(name, "NOFLSH"))
1586 #ifdef NOFLSH
1587             return NOFLSH;
1588 #else
1589             goto not_there;
1590 #endif
1591         break;
1592     case 'O':
1593         if (strnEQ(name, "O_", 2)) {
1594             if (strEQ(name, "O_APPEND"))
1595 #ifdef O_APPEND
1596                 return O_APPEND;
1597 #else
1598                 goto not_there;
1599 #endif
1600             if (strEQ(name, "O_CREAT"))
1601 #ifdef O_CREAT
1602                 return O_CREAT;
1603 #else
1604                 goto not_there;
1605 #endif
1606             if (strEQ(name, "O_TRUNC"))
1607 #ifdef O_TRUNC
1608                 return O_TRUNC;
1609 #else
1610                 goto not_there;
1611 #endif
1612             if (strEQ(name, "O_RDONLY"))
1613 #ifdef O_RDONLY
1614                 return O_RDONLY;
1615 #else
1616                 goto not_there;
1617 #endif
1618             if (strEQ(name, "O_RDWR"))
1619 #ifdef O_RDWR
1620                 return O_RDWR;
1621 #else
1622                 goto not_there;
1623 #endif
1624             if (strEQ(name, "O_WRONLY"))
1625 #ifdef O_WRONLY
1626                 return O_WRONLY;
1627 #else
1628                 goto not_there;
1629 #endif
1630             if (strEQ(name, "O_EXCL"))
1631 #ifdef O_EXCL
1632                 return O_EXCL;
1633 #else
1634                 goto not_there;
1635 #endif
1636             if (strEQ(name, "O_NOCTTY"))
1637 #ifdef O_NOCTTY
1638                 return O_NOCTTY;
1639 #else
1640                 goto not_there;
1641 #endif
1642             if (strEQ(name, "O_NONBLOCK"))
1643 #ifdef O_NONBLOCK
1644                 return O_NONBLOCK;
1645 #else
1646                 goto not_there;
1647 #endif
1648             if (strEQ(name, "O_ACCMODE"))
1649 #ifdef O_ACCMODE
1650                 return O_ACCMODE;
1651 #else
1652                 goto not_there;
1653 #endif
1654             break;
1655         }
1656         if (strEQ(name, "OPEN_MAX"))
1657 #ifdef OPEN_MAX
1658             return OPEN_MAX;
1659 #else
1660             goto not_there;
1661 #endif
1662         if (strEQ(name, "OPOST"))
1663 #ifdef OPOST
1664             return OPOST;
1665 #else
1666             goto not_there;
1667 #endif
1668         break;
1669     case 'P':
1670         if (strEQ(name, "PATH_MAX"))
1671 #ifdef PATH_MAX
1672             return PATH_MAX;
1673 #else
1674             goto not_there;
1675 #endif
1676         if (strEQ(name, "PARENB"))
1677 #ifdef PARENB
1678             return PARENB;
1679 #else
1680             goto not_there;
1681 #endif
1682         if (strEQ(name, "PARMRK"))
1683 #ifdef PARMRK
1684             return PARMRK;
1685 #else
1686             goto not_there;
1687 #endif
1688         if (strEQ(name, "PARODD"))
1689 #ifdef PARODD
1690             return PARODD;
1691 #else
1692             goto not_there;
1693 #endif
1694         if (strEQ(name, "PIPE_BUF"))
1695 #ifdef PIPE_BUF
1696             return PIPE_BUF;
1697 #else
1698             goto not_there;
1699 #endif
1700         break;
1701     case 'R':
1702         if (strEQ(name, "RAND_MAX"))
1703 #ifdef RAND_MAX
1704             return RAND_MAX;
1705 #else
1706             goto not_there;
1707 #endif
1708         if (strEQ(name, "R_OK"))
1709 #ifdef R_OK
1710             return R_OK;
1711 #else
1712             goto not_there;
1713 #endif
1714         break;
1715     case 'S':
1716         if (strnEQ(name, "SIG", 3)) {
1717             if (name[3] == '_') {
1718                 if (strEQ(name, "SIG_BLOCK"))
1719 #ifdef SIG_BLOCK
1720                     return SIG_BLOCK;
1721 #else
1722                     goto not_there;
1723 #endif
1724 #ifdef SIG_DFL
1725                 if (strEQ(name, "SIG_DFL")) return (IV)SIG_DFL;
1726 #endif
1727 #ifdef SIG_ERR
1728                 if (strEQ(name, "SIG_ERR")) return (IV)SIG_ERR;
1729 #endif
1730 #ifdef SIG_IGN
1731                 if (strEQ(name, "SIG_IGN")) return (IV)SIG_IGN;
1732 #endif
1733                 if (strEQ(name, "SIG_SETMASK"))
1734 #ifdef SIG_SETMASK
1735                     return SIG_SETMASK;
1736 #else
1737                     goto not_there;
1738 #endif
1739                 if (strEQ(name, "SIG_UNBLOCK"))
1740 #ifdef SIG_UNBLOCK
1741                     return SIG_UNBLOCK;
1742 #else
1743                     goto not_there;
1744 #endif
1745                 break;
1746             }
1747             if (strEQ(name, "SIGABRT"))
1748 #ifdef SIGABRT
1749                 return SIGABRT;
1750 #else
1751                 goto not_there;
1752 #endif
1753             if (strEQ(name, "SIGALRM"))
1754 #ifdef SIGALRM
1755                 return SIGALRM;
1756 #else
1757                 goto not_there;
1758 #endif
1759             if (strEQ(name, "SIGCHLD"))
1760 #ifdef SIGCHLD
1761                 return SIGCHLD;
1762 #else
1763                 goto not_there;
1764 #endif
1765             if (strEQ(name, "SIGCONT"))
1766 #ifdef SIGCONT
1767                 return SIGCONT;
1768 #else
1769                 goto not_there;
1770 #endif
1771             if (strEQ(name, "SIGFPE"))
1772 #ifdef SIGFPE
1773                 return SIGFPE;
1774 #else
1775                 goto not_there;
1776 #endif
1777             if (strEQ(name, "SIGHUP"))
1778 #ifdef SIGHUP
1779                 return SIGHUP;
1780 #else
1781                 goto not_there;
1782 #endif
1783             if (strEQ(name, "SIGILL"))
1784 #ifdef SIGILL
1785                 return SIGILL;
1786 #else
1787                 goto not_there;
1788 #endif
1789             if (strEQ(name, "SIGINT"))
1790 #ifdef SIGINT
1791                 return SIGINT;
1792 #else
1793                 goto not_there;
1794 #endif
1795             if (strEQ(name, "SIGKILL"))
1796 #ifdef SIGKILL
1797                 return SIGKILL;
1798 #else
1799                 goto not_there;
1800 #endif
1801             if (strEQ(name, "SIGPIPE"))
1802 #ifdef SIGPIPE
1803                 return SIGPIPE;
1804 #else
1805                 goto not_there;
1806 #endif
1807             if (strEQ(name, "SIGQUIT"))
1808 #ifdef SIGQUIT
1809                 return SIGQUIT;
1810 #else
1811                 goto not_there;
1812 #endif
1813             if (strEQ(name, "SIGSEGV"))
1814 #ifdef SIGSEGV
1815                 return SIGSEGV;
1816 #else
1817                 goto not_there;
1818 #endif
1819             if (strEQ(name, "SIGSTOP"))
1820 #ifdef SIGSTOP
1821                 return SIGSTOP;
1822 #else
1823                 goto not_there;
1824 #endif
1825             if (strEQ(name, "SIGTERM"))
1826 #ifdef SIGTERM
1827                 return SIGTERM;
1828 #else
1829                 goto not_there;
1830 #endif
1831             if (strEQ(name, "SIGTSTP"))
1832 #ifdef SIGTSTP
1833                 return SIGTSTP;
1834 #else
1835                 goto not_there;
1836 #endif
1837             if (strEQ(name, "SIGTTIN"))
1838 #ifdef SIGTTIN
1839                 return SIGTTIN;
1840 #else
1841                 goto not_there;
1842 #endif
1843             if (strEQ(name, "SIGTTOU"))
1844 #ifdef SIGTTOU
1845                 return SIGTTOU;
1846 #else
1847                 goto not_there;
1848 #endif
1849             if (strEQ(name, "SIGUSR1"))
1850 #ifdef SIGUSR1
1851                 return SIGUSR1;
1852 #else
1853                 goto not_there;
1854 #endif
1855             if (strEQ(name, "SIGUSR2"))
1856 #ifdef SIGUSR2
1857                 return SIGUSR2;
1858 #else
1859                 goto not_there;
1860 #endif
1861             break;
1862         }
1863         if (name[1] == '_') {
1864             if (strEQ(name, "S_ISGID"))
1865 #ifdef S_ISGID
1866                 return S_ISGID;
1867 #else
1868                 goto not_there;
1869 #endif
1870             if (strEQ(name, "S_ISUID"))
1871 #ifdef S_ISUID
1872                 return S_ISUID;
1873 #else
1874                 goto not_there;
1875 #endif
1876             if (strEQ(name, "S_IRGRP"))
1877 #ifdef S_IRGRP
1878                 return S_IRGRP;
1879 #else
1880                 goto not_there;
1881 #endif
1882             if (strEQ(name, "S_IROTH"))
1883 #ifdef S_IROTH
1884                 return S_IROTH;
1885 #else
1886                 goto not_there;
1887 #endif
1888             if (strEQ(name, "S_IRUSR"))
1889 #ifdef S_IRUSR
1890                 return S_IRUSR;
1891 #else
1892                 goto not_there;
1893 #endif
1894             if (strEQ(name, "S_IRWXG"))
1895 #ifdef S_IRWXG
1896                 return S_IRWXG;
1897 #else
1898                 goto not_there;
1899 #endif
1900             if (strEQ(name, "S_IRWXO"))
1901 #ifdef S_IRWXO
1902                 return S_IRWXO;
1903 #else
1904                 goto not_there;
1905 #endif
1906             if (strEQ(name, "S_IRWXU"))
1907 #ifdef S_IRWXU
1908                 return S_IRWXU;
1909 #else
1910                 goto not_there;
1911 #endif
1912             if (strEQ(name, "S_IWGRP"))
1913 #ifdef S_IWGRP
1914                 return S_IWGRP;
1915 #else
1916                 goto not_there;
1917 #endif
1918             if (strEQ(name, "S_IWOTH"))
1919 #ifdef S_IWOTH
1920                 return S_IWOTH;
1921 #else
1922                 goto not_there;
1923 #endif
1924             if (strEQ(name, "S_IWUSR"))
1925 #ifdef S_IWUSR
1926                 return S_IWUSR;
1927 #else
1928                 goto not_there;
1929 #endif
1930             if (strEQ(name, "S_IXGRP"))
1931 #ifdef S_IXGRP
1932                 return S_IXGRP;
1933 #else
1934                 goto not_there;
1935 #endif
1936             if (strEQ(name, "S_IXOTH"))
1937 #ifdef S_IXOTH
1938                 return S_IXOTH;
1939 #else
1940                 goto not_there;
1941 #endif
1942             if (strEQ(name, "S_IXUSR"))
1943 #ifdef S_IXUSR
1944                 return S_IXUSR;
1945 #else
1946                 goto not_there;
1947 #endif
1948             errno = EAGAIN;             /* the following aren't constants */
1949 #ifdef S_ISBLK
1950             if (strEQ(name, "S_ISBLK")) return S_ISBLK(arg);
1951 #endif
1952 #ifdef S_ISCHR
1953             if (strEQ(name, "S_ISCHR")) return S_ISCHR(arg);
1954 #endif
1955 #ifdef S_ISDIR
1956             if (strEQ(name, "S_ISDIR")) return S_ISDIR(arg);
1957 #endif
1958 #ifdef S_ISFIFO
1959             if (strEQ(name, "S_ISFIFO")) return S_ISFIFO(arg);
1960 #endif
1961 #ifdef S_ISREG
1962             if (strEQ(name, "S_ISREG")) return S_ISREG(arg);
1963 #endif
1964             break;
1965         }
1966         if (strEQ(name, "SEEK_CUR"))
1967 #ifdef SEEK_CUR
1968             return SEEK_CUR;
1969 #else
1970             goto not_there;
1971 #endif
1972         if (strEQ(name, "SEEK_END"))
1973 #ifdef SEEK_END
1974             return SEEK_END;
1975 #else
1976             goto not_there;
1977 #endif
1978         if (strEQ(name, "SEEK_SET"))
1979 #ifdef SEEK_SET
1980             return SEEK_SET;
1981 #else
1982             goto not_there;
1983 #endif
1984         if (strEQ(name, "STREAM_MAX"))
1985 #ifdef STREAM_MAX
1986             return STREAM_MAX;
1987 #else
1988             goto not_there;
1989 #endif
1990         if (strEQ(name, "SHRT_MAX"))
1991 #ifdef SHRT_MAX
1992             return SHRT_MAX;
1993 #else
1994             goto not_there;
1995 #endif
1996         if (strEQ(name, "SHRT_MIN"))
1997 #ifdef SHRT_MIN
1998             return SHRT_MIN;
1999 #else
2000             goto not_there;
2001 #endif
2002         if (strnEQ(name, "SA_", 3)) {
2003             if (strEQ(name, "SA_NOCLDSTOP"))
2004 #ifdef SA_NOCLDSTOP
2005                 return SA_NOCLDSTOP;
2006 #else
2007                 goto not_there;
2008 #endif
2009             if (strEQ(name, "SA_NOCLDWAIT"))
2010 #ifdef SA_NOCLDWAIT
2011                 return SA_NOCLDWAIT;
2012 #else
2013                 goto not_there;
2014 #endif
2015             if (strEQ(name, "SA_NODEFER"))
2016 #ifdef SA_NODEFER
2017                 return SA_NODEFER;
2018 #else
2019                 goto not_there;
2020 #endif
2021             if (strEQ(name, "SA_ONSTACK"))
2022 #ifdef SA_ONSTACK
2023                 return SA_ONSTACK;
2024 #else
2025                 goto not_there;
2026 #endif
2027             if (strEQ(name, "SA_RESETHAND"))
2028 #ifdef SA_RESETHAND
2029                 return SA_RESETHAND;
2030 #else
2031                 goto not_there;
2032 #endif
2033             if (strEQ(name, "SA_RESTART"))
2034 #ifdef SA_RESTART
2035                 return SA_RESTART;
2036 #else
2037                 goto not_there;
2038 #endif
2039             if (strEQ(name, "SA_SIGINFO"))
2040 #ifdef SA_SIGINFO
2041                 return SA_SIGINFO;
2042 #else
2043                 goto not_there;
2044 #endif
2045             break;
2046         }
2047         if (strEQ(name, "SCHAR_MAX"))
2048 #ifdef SCHAR_MAX
2049             return SCHAR_MAX;
2050 #else
2051             goto not_there;
2052 #endif
2053         if (strEQ(name, "SCHAR_MIN"))
2054 #ifdef SCHAR_MIN
2055             return SCHAR_MIN;
2056 #else
2057             goto not_there;
2058 #endif
2059         if (strEQ(name, "SSIZE_MAX"))
2060 #ifdef SSIZE_MAX
2061             return SSIZE_MAX;
2062 #else
2063             goto not_there;
2064 #endif
2065         if (strEQ(name, "STDIN_FILENO"))
2066 #ifdef STDIN_FILENO
2067             return STDIN_FILENO;
2068 #else
2069             goto not_there;
2070 #endif
2071         if (strEQ(name, "STDOUT_FILENO"))
2072 #ifdef STDOUT_FILENO
2073             return STDOUT_FILENO;
2074 #else
2075             goto not_there;
2076 #endif
2077         if (strEQ(name, "STDERR_FILENO"))
2078 #ifdef STDERR_FILENO
2079             return STDERR_FILENO;
2080 #else
2081             goto not_there;
2082 #endif
2083         break;
2084     case 'T':
2085         if (strEQ(name, "TCIFLUSH"))
2086 #ifdef TCIFLUSH
2087             return TCIFLUSH;
2088 #else
2089             goto not_there;
2090 #endif
2091         if (strEQ(name, "TCIOFF"))
2092 #ifdef TCIOFF
2093             return TCIOFF;
2094 #else
2095             goto not_there;
2096 #endif
2097         if (strEQ(name, "TCIOFLUSH"))
2098 #ifdef TCIOFLUSH
2099             return TCIOFLUSH;
2100 #else
2101             goto not_there;
2102 #endif
2103         if (strEQ(name, "TCION"))
2104 #ifdef TCION
2105             return TCION;
2106 #else
2107             goto not_there;
2108 #endif
2109         if (strEQ(name, "TCOFLUSH"))
2110 #ifdef TCOFLUSH
2111             return TCOFLUSH;
2112 #else
2113             goto not_there;
2114 #endif
2115         if (strEQ(name, "TCOOFF"))
2116 #ifdef TCOOFF
2117             return TCOOFF;
2118 #else
2119             goto not_there;
2120 #endif
2121         if (strEQ(name, "TCOON"))
2122 #ifdef TCOON
2123             return TCOON;
2124 #else
2125             goto not_there;
2126 #endif
2127         if (strEQ(name, "TCSADRAIN"))
2128 #ifdef TCSADRAIN
2129             return TCSADRAIN;
2130 #else
2131             goto not_there;
2132 #endif
2133         if (strEQ(name, "TCSAFLUSH"))
2134 #ifdef TCSAFLUSH
2135             return TCSAFLUSH;
2136 #else
2137             goto not_there;
2138 #endif
2139         if (strEQ(name, "TCSANOW"))
2140 #ifdef TCSANOW
2141             return TCSANOW;
2142 #else
2143             goto not_there;
2144 #endif
2145         if (strEQ(name, "TMP_MAX"))
2146 #ifdef TMP_MAX
2147             return TMP_MAX;
2148 #else
2149             goto not_there;
2150 #endif
2151         if (strEQ(name, "TOSTOP"))
2152 #ifdef TOSTOP
2153             return TOSTOP;
2154 #else
2155             goto not_there;
2156 #endif
2157         if (strEQ(name, "TZNAME_MAX"))
2158 #ifdef TZNAME_MAX
2159             return TZNAME_MAX;
2160 #else
2161             goto not_there;
2162 #endif
2163         break;
2164     case 'U':
2165         if (strEQ(name, "UCHAR_MAX"))
2166 #ifdef UCHAR_MAX
2167             return UCHAR_MAX;
2168 #else
2169             goto not_there;
2170 #endif
2171         if (strEQ(name, "UINT_MAX"))
2172 #ifdef UINT_MAX
2173             return UINT_MAX;
2174 #else
2175             goto not_there;
2176 #endif
2177         if (strEQ(name, "ULONG_MAX"))
2178 #ifdef ULONG_MAX
2179             return ULONG_MAX;
2180 #else
2181             goto not_there;
2182 #endif
2183         if (strEQ(name, "USHRT_MAX"))
2184 #ifdef USHRT_MAX
2185             return USHRT_MAX;
2186 #else
2187             goto not_there;
2188 #endif
2189         break;
2190     case 'V':
2191         if (strEQ(name, "VEOF"))
2192 #ifdef VEOF
2193             return VEOF;
2194 #else
2195             goto not_there;
2196 #endif
2197         if (strEQ(name, "VEOL"))
2198 #ifdef VEOL
2199             return VEOL;
2200 #else
2201             goto not_there;
2202 #endif
2203         if (strEQ(name, "VERASE"))
2204 #ifdef VERASE
2205             return VERASE;
2206 #else
2207             goto not_there;
2208 #endif
2209         if (strEQ(name, "VINTR"))
2210 #ifdef VINTR
2211             return VINTR;
2212 #else
2213             goto not_there;
2214 #endif
2215         if (strEQ(name, "VKILL"))
2216 #ifdef VKILL
2217             return VKILL;
2218 #else
2219             goto not_there;
2220 #endif
2221         if (strEQ(name, "VMIN"))
2222 #ifdef VMIN
2223             return VMIN;
2224 #else
2225             goto not_there;
2226 #endif
2227         if (strEQ(name, "VQUIT"))
2228 #ifdef VQUIT
2229             return VQUIT;
2230 #else
2231             goto not_there;
2232 #endif
2233         if (strEQ(name, "VSTART"))
2234 #ifdef VSTART
2235             return VSTART;
2236 #else
2237             goto not_there;
2238 #endif
2239         if (strEQ(name, "VSTOP"))
2240 #ifdef VSTOP
2241             return VSTOP;
2242 #else
2243             goto not_there;
2244 #endif
2245         if (strEQ(name, "VSUSP"))
2246 #ifdef VSUSP
2247             return VSUSP;
2248 #else
2249             goto not_there;
2250 #endif
2251         if (strEQ(name, "VTIME"))
2252 #ifdef VTIME
2253             return VTIME;
2254 #else
2255             goto not_there;
2256 #endif
2257         break;
2258     case 'W':
2259         if (strEQ(name, "W_OK"))
2260 #ifdef W_OK
2261             return W_OK;
2262 #else
2263             goto not_there;
2264 #endif
2265         if (strEQ(name, "WNOHANG"))
2266 #ifdef WNOHANG
2267             return WNOHANG;
2268 #else
2269             goto not_there;
2270 #endif
2271         if (strEQ(name, "WUNTRACED"))
2272 #ifdef WUNTRACED
2273             return WUNTRACED;
2274 #else
2275             goto not_there;
2276 #endif
2277         errno = EAGAIN;         /* the following aren't constants */
2278 #ifdef WEXITSTATUS
2279         if (strEQ(name, "WEXITSTATUS")) return WEXITSTATUS(arg);
2280 #endif
2281 #ifdef WIFEXITED
2282         if (strEQ(name, "WIFEXITED")) return WIFEXITED(arg);
2283 #endif
2284 #ifdef WIFSIGNALED
2285         if (strEQ(name, "WIFSIGNALED")) return WIFSIGNALED(arg);
2286 #endif
2287 #ifdef WIFSTOPPED
2288         if (strEQ(name, "WIFSTOPPED")) return WIFSTOPPED(arg);
2289 #endif
2290 #ifdef WSTOPSIG
2291         if (strEQ(name, "WSTOPSIG")) return WSTOPSIG(arg);
2292 #endif
2293 #ifdef WTERMSIG
2294         if (strEQ(name, "WTERMSIG")) return WTERMSIG(arg);
2295 #endif
2296         break;
2297     case 'X':
2298         if (strEQ(name, "X_OK"))
2299 #ifdef X_OK
2300             return X_OK;
2301 #else
2302             goto not_there;
2303 #endif
2304         break;
2305     case '_':
2306         if (strnEQ(name, "_PC_", 4)) {
2307             if (strEQ(name, "_PC_CHOWN_RESTRICTED"))
2308 #if defined(_PC_CHOWN_RESTRICTED) || HINT_SC_EXIST
2309                 return _PC_CHOWN_RESTRICTED;
2310 #else
2311                 goto not_there;
2312 #endif
2313             if (strEQ(name, "_PC_LINK_MAX"))
2314 #if defined(_PC_LINK_MAX) || HINT_SC_EXIST
2315                 return _PC_LINK_MAX;
2316 #else
2317                 goto not_there;
2318 #endif
2319             if (strEQ(name, "_PC_MAX_CANON"))
2320 #if defined(_PC_MAX_CANON) || HINT_SC_EXIST
2321                 return _PC_MAX_CANON;
2322 #else
2323                 goto not_there;
2324 #endif
2325             if (strEQ(name, "_PC_MAX_INPUT"))
2326 #if defined(_PC_MAX_INPUT) || HINT_SC_EXIST
2327                 return _PC_MAX_INPUT;
2328 #else
2329                 goto not_there;
2330 #endif
2331             if (strEQ(name, "_PC_NAME_MAX"))
2332 #if defined(_PC_NAME_MAX) || HINT_SC_EXIST
2333                 return _PC_NAME_MAX;
2334 #else
2335                 goto not_there;
2336 #endif
2337             if (strEQ(name, "_PC_NO_TRUNC"))
2338 #if defined(_PC_NO_TRUNC) || HINT_SC_EXIST
2339                 return _PC_NO_TRUNC;
2340 #else
2341                 goto not_there;
2342 #endif
2343             if (strEQ(name, "_PC_PATH_MAX"))
2344 #if defined(_PC_PATH_MAX) || HINT_SC_EXIST
2345                 return _PC_PATH_MAX;
2346 #else
2347                 goto not_there;
2348 #endif
2349             if (strEQ(name, "_PC_PIPE_BUF"))
2350 #if defined(_PC_PIPE_BUF) || HINT_SC_EXIST
2351                 return _PC_PIPE_BUF;
2352 #else
2353                 goto not_there;
2354 #endif
2355             if (strEQ(name, "_PC_VDISABLE"))
2356 #if defined(_PC_VDISABLE) || HINT_SC_EXIST
2357                 return _PC_VDISABLE;
2358 #else
2359                 goto not_there;
2360 #endif
2361             break;
2362         }
2363         if (strnEQ(name, "_POSIX_", 7)) {
2364             if (strEQ(name, "_POSIX_ARG_MAX"))
2365 #ifdef _POSIX_ARG_MAX
2366                 return _POSIX_ARG_MAX;
2367 #else
2368                 return 0;
2369 #endif
2370             if (strEQ(name, "_POSIX_CHILD_MAX"))
2371 #ifdef _POSIX_CHILD_MAX
2372                 return _POSIX_CHILD_MAX;
2373 #else
2374                 return 0;
2375 #endif
2376             if (strEQ(name, "_POSIX_CHOWN_RESTRICTED"))
2377 #ifdef _POSIX_CHOWN_RESTRICTED
2378                 return _POSIX_CHOWN_RESTRICTED;
2379 #else
2380                 return 0;
2381 #endif
2382             if (strEQ(name, "_POSIX_JOB_CONTROL"))
2383 #ifdef _POSIX_JOB_CONTROL
2384                 return _POSIX_JOB_CONTROL;
2385 #else
2386                 return 0;
2387 #endif
2388             if (strEQ(name, "_POSIX_LINK_MAX"))
2389 #ifdef _POSIX_LINK_MAX
2390                 return _POSIX_LINK_MAX;
2391 #else
2392                 return 0;
2393 #endif
2394             if (strEQ(name, "_POSIX_MAX_CANON"))
2395 #ifdef _POSIX_MAX_CANON
2396                 return _POSIX_MAX_CANON;
2397 #else
2398                 return 0;
2399 #endif
2400             if (strEQ(name, "_POSIX_MAX_INPUT"))
2401 #ifdef _POSIX_MAX_INPUT
2402                 return _POSIX_MAX_INPUT;
2403 #else
2404                 return 0;
2405 #endif
2406             if (strEQ(name, "_POSIX_NAME_MAX"))
2407 #ifdef _POSIX_NAME_MAX
2408                 return _POSIX_NAME_MAX;
2409 #else
2410                 return 0;
2411 #endif
2412             if (strEQ(name, "_POSIX_NGROUPS_MAX"))
2413 #ifdef _POSIX_NGROUPS_MAX
2414                 return _POSIX_NGROUPS_MAX;
2415 #else
2416                 return 0;
2417 #endif
2418             if (strEQ(name, "_POSIX_NO_TRUNC"))
2419 #ifdef _POSIX_NO_TRUNC
2420                 return _POSIX_NO_TRUNC;
2421 #else
2422                 return 0;
2423 #endif
2424             if (strEQ(name, "_POSIX_OPEN_MAX"))
2425 #ifdef _POSIX_OPEN_MAX
2426                 return _POSIX_OPEN_MAX;
2427 #else
2428                 return 0;
2429 #endif
2430             if (strEQ(name, "_POSIX_PATH_MAX"))
2431 #ifdef _POSIX_PATH_MAX
2432                 return _POSIX_PATH_MAX;
2433 #else
2434                 return 0;
2435 #endif
2436             if (strEQ(name, "_POSIX_PIPE_BUF"))
2437 #ifdef _POSIX_PIPE_BUF
2438                 return _POSIX_PIPE_BUF;
2439 #else
2440                 return 0;
2441 #endif
2442             if (strEQ(name, "_POSIX_SAVED_IDS"))
2443 #ifdef _POSIX_SAVED_IDS
2444                 return _POSIX_SAVED_IDS;
2445 #else
2446                 return 0;
2447 #endif
2448             if (strEQ(name, "_POSIX_SSIZE_MAX"))
2449 #ifdef _POSIX_SSIZE_MAX
2450                 return _POSIX_SSIZE_MAX;
2451 #else
2452                 return 0;
2453 #endif
2454             if (strEQ(name, "_POSIX_STREAM_MAX"))
2455 #ifdef _POSIX_STREAM_MAX
2456                 return _POSIX_STREAM_MAX;
2457 #else
2458                 return 0;
2459 #endif
2460             if (strEQ(name, "_POSIX_TZNAME_MAX"))
2461 #ifdef _POSIX_TZNAME_MAX
2462                 return _POSIX_TZNAME_MAX;
2463 #else
2464                 return 0;
2465 #endif
2466             if (strEQ(name, "_POSIX_VDISABLE"))
2467 #ifdef _POSIX_VDISABLE
2468                 return _POSIX_VDISABLE;
2469 #else
2470                 return 0;
2471 #endif
2472             if (strEQ(name, "_POSIX_VERSION"))
2473 #ifdef _POSIX_VERSION
2474                 return _POSIX_VERSION;
2475 #else
2476                 return 0;
2477 #endif
2478             break;
2479         }
2480         if (strnEQ(name, "_SC_", 4)) {
2481             if (strEQ(name, "_SC_ARG_MAX"))
2482 #if defined(_SC_ARG_MAX) || HINT_SC_EXIST
2483                 return _SC_ARG_MAX;
2484 #else
2485                 goto not_there;
2486 #endif
2487             if (strEQ(name, "_SC_CHILD_MAX"))
2488 #if defined(_SC_CHILD_MAX) || HINT_SC_EXIST
2489                 return _SC_CHILD_MAX;
2490 #else
2491                 goto not_there;
2492 #endif
2493             if (strEQ(name, "_SC_CLK_TCK"))
2494 #if defined(_SC_CLK_TCK) || HINT_SC_EXIST
2495                 return _SC_CLK_TCK;
2496 #else
2497                 goto not_there;
2498 #endif
2499             if (strEQ(name, "_SC_JOB_CONTROL"))
2500 #if defined(_SC_JOB_CONTROL) || HINT_SC_EXIST
2501                 return _SC_JOB_CONTROL;
2502 #else
2503                 goto not_there;
2504 #endif
2505             if (strEQ(name, "_SC_NGROUPS_MAX"))
2506 #if defined(_SC_NGROUPS_MAX) || HINT_SC_EXIST
2507                 return _SC_NGROUPS_MAX;
2508 #else
2509                 goto not_there;
2510 #endif
2511             if (strEQ(name, "_SC_OPEN_MAX"))
2512 #if defined(_SC_OPEN_MAX) || HINT_SC_EXIST
2513                 return _SC_OPEN_MAX;
2514 #else
2515                 goto not_there;
2516 #endif
2517             if (strEQ(name, "_SC_SAVED_IDS"))
2518 #if defined(_SC_SAVED_IDS) || HINT_SC_EXIST
2519                 return _SC_SAVED_IDS;
2520 #else
2521                 goto not_there;
2522 #endif
2523             if (strEQ(name, "_SC_STREAM_MAX"))
2524 #if defined(_SC_STREAM_MAX) || HINT_SC_EXIST
2525                 return _SC_STREAM_MAX;
2526 #else
2527                 goto not_there;
2528 #endif
2529             if (strEQ(name, "_SC_TZNAME_MAX"))
2530 #if defined(_SC_TZNAME_MAX) || HINT_SC_EXIST
2531                 return _SC_TZNAME_MAX;
2532 #else
2533                 goto not_there;
2534 #endif
2535             if (strEQ(name, "_SC_VERSION"))
2536 #if defined(_SC_VERSION) || HINT_SC_EXIST
2537                 return _SC_VERSION;
2538 #else
2539                 goto not_there;
2540 #endif
2541             break;
2542         }
2543     }
2544     errno = EINVAL;
2545     return 0;
2546
2547 not_there:
2548     errno = ENOENT;
2549     return 0;
2550 }
2551
2552 static void
2553 restore_sigmask(sigset_t *ossetp)
2554 {
2555             /* Fortunately, restoring the signal mask can't fail, because
2556              * there's nothing we can do about it if it does -- we're not
2557              * supposed to return -1 from sigaction unless the disposition
2558              * was unaffected.
2559              */
2560             (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
2561 }
2562
2563 MODULE = SigSet         PACKAGE = POSIX::SigSet         PREFIX = sig
2564
2565 POSIX::SigSet
2566 new(packname = "POSIX::SigSet", ...)
2567     char *              packname
2568     CODE:
2569         {
2570             int i;
2571             New(0, RETVAL, 1, sigset_t);
2572             sigemptyset(RETVAL);
2573             for (i = 1; i < items; i++)
2574                 sigaddset(RETVAL, SvIV(ST(i)));
2575         }
2576     OUTPUT:
2577         RETVAL
2578
2579 void
2580 DESTROY(sigset)
2581         POSIX::SigSet   sigset
2582     CODE:
2583         Safefree(sigset);
2584
2585 SysRet
2586 sigaddset(sigset, sig)
2587         POSIX::SigSet   sigset
2588         int             sig
2589
2590 SysRet
2591 sigdelset(sigset, sig)
2592         POSIX::SigSet   sigset
2593         int             sig
2594
2595 SysRet
2596 sigemptyset(sigset)
2597         POSIX::SigSet   sigset
2598
2599 SysRet
2600 sigfillset(sigset)
2601         POSIX::SigSet   sigset
2602
2603 int
2604 sigismember(sigset, sig)
2605         POSIX::SigSet   sigset
2606         int             sig
2607
2608
2609 MODULE = Termios        PACKAGE = POSIX::Termios        PREFIX = cf
2610
2611 POSIX::Termios
2612 new(packname = "POSIX::Termios", ...)
2613     char *              packname
2614     CODE:
2615         {
2616 #ifdef I_TERMIOS
2617             New(0, RETVAL, 1, struct termios);
2618 #else
2619             not_here("termios");
2620         RETVAL = 0;
2621 #endif
2622         }
2623     OUTPUT:
2624         RETVAL
2625
2626 void
2627 DESTROY(termios_ref)
2628         POSIX::Termios  termios_ref
2629     CODE:
2630 #ifdef I_TERMIOS
2631         Safefree(termios_ref);
2632 #else
2633             not_here("termios");
2634 #endif
2635
2636 SysRet
2637 getattr(termios_ref, fd = 0)
2638         POSIX::Termios  termios_ref
2639         int             fd
2640     CODE:
2641         RETVAL = tcgetattr(fd, termios_ref);
2642     OUTPUT:
2643         RETVAL
2644
2645 SysRet
2646 setattr(termios_ref, fd = 0, optional_actions = 0)
2647         POSIX::Termios  termios_ref
2648         int             fd
2649         int             optional_actions
2650     CODE:
2651         RETVAL = tcsetattr(fd, optional_actions, termios_ref);
2652     OUTPUT:
2653         RETVAL
2654
2655 speed_t
2656 cfgetispeed(termios_ref)
2657         POSIX::Termios  termios_ref
2658
2659 speed_t
2660 cfgetospeed(termios_ref)
2661         POSIX::Termios  termios_ref
2662
2663 tcflag_t
2664 getiflag(termios_ref)
2665         POSIX::Termios  termios_ref
2666     CODE:
2667 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2668         RETVAL = termios_ref->c_iflag;
2669 #else
2670      not_here("getiflag");
2671      RETVAL = 0;
2672 #endif
2673     OUTPUT:
2674         RETVAL
2675
2676 tcflag_t
2677 getoflag(termios_ref)
2678         POSIX::Termios  termios_ref
2679     CODE:
2680 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2681         RETVAL = termios_ref->c_oflag;
2682 #else
2683      not_here("getoflag");
2684      RETVAL = 0;
2685 #endif
2686     OUTPUT:
2687         RETVAL
2688
2689 tcflag_t
2690 getcflag(termios_ref)
2691         POSIX::Termios  termios_ref
2692     CODE:
2693 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2694         RETVAL = termios_ref->c_cflag;
2695 #else
2696      not_here("getcflag");
2697      RETVAL = 0;
2698 #endif
2699     OUTPUT:
2700         RETVAL
2701
2702 tcflag_t
2703 getlflag(termios_ref)
2704         POSIX::Termios  termios_ref
2705     CODE:
2706 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2707         RETVAL = termios_ref->c_lflag;
2708 #else
2709      not_here("getlflag");
2710      RETVAL = 0;
2711 #endif
2712     OUTPUT:
2713         RETVAL
2714
2715 cc_t
2716 getcc(termios_ref, ccix)
2717         POSIX::Termios  termios_ref
2718         int             ccix
2719     CODE:
2720 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2721         if (ccix >= NCCS)
2722             croak("Bad getcc subscript");
2723         RETVAL = termios_ref->c_cc[ccix];
2724 #else
2725      not_here("getcc");
2726      RETVAL = 0;
2727 #endif
2728     OUTPUT:
2729         RETVAL
2730
2731 SysRet
2732 cfsetispeed(termios_ref, speed)
2733         POSIX::Termios  termios_ref
2734         speed_t         speed
2735
2736 SysRet
2737 cfsetospeed(termios_ref, speed)
2738         POSIX::Termios  termios_ref
2739         speed_t         speed
2740
2741 void
2742 setiflag(termios_ref, iflag)
2743         POSIX::Termios  termios_ref
2744         tcflag_t        iflag
2745     CODE:
2746 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2747         termios_ref->c_iflag = iflag;
2748 #else
2749             not_here("setiflag");
2750 #endif
2751
2752 void
2753 setoflag(termios_ref, oflag)
2754         POSIX::Termios  termios_ref
2755         tcflag_t        oflag
2756     CODE:
2757 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2758         termios_ref->c_oflag = oflag;
2759 #else
2760             not_here("setoflag");
2761 #endif
2762
2763 void
2764 setcflag(termios_ref, cflag)
2765         POSIX::Termios  termios_ref
2766         tcflag_t        cflag
2767     CODE:
2768 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2769         termios_ref->c_cflag = cflag;
2770 #else
2771             not_here("setcflag");
2772 #endif
2773
2774 void
2775 setlflag(termios_ref, lflag)
2776         POSIX::Termios  termios_ref
2777         tcflag_t        lflag
2778     CODE:
2779 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2780         termios_ref->c_lflag = lflag;
2781 #else
2782             not_here("setlflag");
2783 #endif
2784
2785 void
2786 setcc(termios_ref, ccix, cc)
2787         POSIX::Termios  termios_ref
2788         int             ccix
2789         cc_t            cc
2790     CODE:
2791 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2792         if (ccix >= NCCS)
2793             croak("Bad setcc subscript");
2794         termios_ref->c_cc[ccix] = cc;
2795 #else
2796             not_here("setcc");
2797 #endif
2798
2799
2800 MODULE = POSIX          PACKAGE = POSIX
2801
2802 NV
2803 constant(name,arg)
2804         char *          name
2805         int             arg
2806
2807 int
2808 isalnum(charstring)
2809         unsigned char * charstring
2810     CODE:
2811         unsigned char *s = charstring;
2812         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2813         for (RETVAL = 1; RETVAL && s < e; s++)
2814             if (!isalnum(*s))
2815                 RETVAL = 0;
2816     OUTPUT:
2817         RETVAL
2818
2819 int
2820 isalpha(charstring)
2821         unsigned char * charstring
2822     CODE:
2823         unsigned char *s = charstring;
2824         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2825         for (RETVAL = 1; RETVAL && s < e; s++)
2826             if (!isalpha(*s))
2827                 RETVAL = 0;
2828     OUTPUT:
2829         RETVAL
2830
2831 int
2832 iscntrl(charstring)
2833         unsigned char * charstring
2834     CODE:
2835         unsigned char *s = charstring;
2836         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2837         for (RETVAL = 1; RETVAL && s < e; s++)
2838             if (!iscntrl(*s))
2839                 RETVAL = 0;
2840     OUTPUT:
2841         RETVAL
2842
2843 int
2844 isdigit(charstring)
2845         unsigned char * charstring
2846     CODE:
2847         unsigned char *s = charstring;
2848         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2849         for (RETVAL = 1; RETVAL && s < e; s++)
2850             if (!isdigit(*s))
2851                 RETVAL = 0;
2852     OUTPUT:
2853         RETVAL
2854
2855 int
2856 isgraph(charstring)
2857         unsigned char * charstring
2858     CODE:
2859         unsigned char *s = charstring;
2860         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2861         for (RETVAL = 1; RETVAL && s < e; s++)
2862             if (!isgraph(*s))
2863                 RETVAL = 0;
2864     OUTPUT:
2865         RETVAL
2866
2867 int
2868 islower(charstring)
2869         unsigned char * charstring
2870     CODE:
2871         unsigned char *s = charstring;
2872         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2873         for (RETVAL = 1; RETVAL && s < e; s++)
2874             if (!islower(*s))
2875                 RETVAL = 0;
2876     OUTPUT:
2877         RETVAL
2878
2879 int
2880 isprint(charstring)
2881         unsigned char * charstring
2882     CODE:
2883         unsigned char *s = charstring;
2884         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2885         for (RETVAL = 1; RETVAL && s < e; s++)
2886             if (!isprint(*s))
2887                 RETVAL = 0;
2888     OUTPUT:
2889         RETVAL
2890
2891 int
2892 ispunct(charstring)
2893         unsigned char * charstring
2894     CODE:
2895         unsigned char *s = charstring;
2896         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2897         for (RETVAL = 1; RETVAL && s < e; s++)
2898             if (!ispunct(*s))
2899                 RETVAL = 0;
2900     OUTPUT:
2901         RETVAL
2902
2903 int
2904 isspace(charstring)
2905         unsigned char * charstring
2906     CODE:
2907         unsigned char *s = charstring;
2908         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2909         for (RETVAL = 1; RETVAL && s < e; s++)
2910             if (!isspace(*s))
2911                 RETVAL = 0;
2912     OUTPUT:
2913         RETVAL
2914
2915 int
2916 isupper(charstring)
2917         unsigned char * charstring
2918     CODE:
2919         unsigned char *s = charstring;
2920         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2921         for (RETVAL = 1; RETVAL && s < e; s++)
2922             if (!isupper(*s))
2923                 RETVAL = 0;
2924     OUTPUT:
2925         RETVAL
2926
2927 int
2928 isxdigit(charstring)
2929         unsigned char * charstring
2930     CODE:
2931         unsigned char *s = charstring;
2932         unsigned char *e = s + PL_na;   /* "PL_na" set by typemap side effect */
2933         for (RETVAL = 1; RETVAL && s < e; s++)
2934             if (!isxdigit(*s))
2935                 RETVAL = 0;
2936     OUTPUT:
2937         RETVAL
2938
2939 SysRet
2940 open(filename, flags = O_RDONLY, mode = 0666)
2941         char *          filename
2942         int             flags
2943         Mode_t          mode
2944     CODE:
2945         if (flags & (O_APPEND|O_CREAT|O_TRUNC|O_RDWR|O_WRONLY|O_EXCL))
2946             TAINT_PROPER("open");
2947         RETVAL = open(filename, flags, mode);
2948     OUTPUT:
2949         RETVAL
2950
2951
2952 HV *
2953 localeconv()
2954     CODE:
2955 #ifdef HAS_LOCALECONV
2956         struct lconv *lcbuf;
2957         RETVAL = newHV();
2958         if ((lcbuf = localeconv())) {
2959             /* the strings */
2960             if (lcbuf->decimal_point && *lcbuf->decimal_point)
2961                 hv_store(RETVAL, "decimal_point", 13,
2962                     newSVpv(lcbuf->decimal_point, 0), 0);
2963             if (lcbuf->thousands_sep && *lcbuf->thousands_sep)
2964                 hv_store(RETVAL, "thousands_sep", 13,
2965                     newSVpv(lcbuf->thousands_sep, 0), 0);
2966 #ifndef NO_LOCALECONV_GROUPING
2967             if (lcbuf->grouping && *lcbuf->grouping)
2968                 hv_store(RETVAL, "grouping", 8,
2969                     newSVpv(lcbuf->grouping, 0), 0);
2970 #endif
2971             if (lcbuf->int_curr_symbol && *lcbuf->int_curr_symbol)
2972                 hv_store(RETVAL, "int_curr_symbol", 15,
2973                     newSVpv(lcbuf->int_curr_symbol, 0), 0);
2974             if (lcbuf->currency_symbol && *lcbuf->currency_symbol)
2975                 hv_store(RETVAL, "currency_symbol", 15,
2976                     newSVpv(lcbuf->currency_symbol, 0), 0);
2977             if (lcbuf->mon_decimal_point && *lcbuf->mon_decimal_point)
2978                 hv_store(RETVAL, "mon_decimal_point", 17,
2979                     newSVpv(lcbuf->mon_decimal_point, 0), 0);
2980 #ifndef NO_LOCALECONV_MON_THOUSANDS_SEP
2981             if (lcbuf->mon_thousands_sep && *lcbuf->mon_thousands_sep)
2982                 hv_store(RETVAL, "mon_thousands_sep", 17,
2983                     newSVpv(lcbuf->mon_thousands_sep, 0), 0);
2984 #endif                    
2985 #ifndef NO_LOCALECONV_MON_GROUPING
2986             if (lcbuf->mon_grouping && *lcbuf->mon_grouping)
2987                 hv_store(RETVAL, "mon_grouping", 12,
2988                     newSVpv(lcbuf->mon_grouping, 0), 0);
2989 #endif
2990             if (lcbuf->positive_sign && *lcbuf->positive_sign)
2991                 hv_store(RETVAL, "positive_sign", 13,
2992                     newSVpv(lcbuf->positive_sign, 0), 0);
2993             if (lcbuf->negative_sign && *lcbuf->negative_sign)
2994                 hv_store(RETVAL, "negative_sign", 13,
2995                     newSVpv(lcbuf->negative_sign, 0), 0);
2996             /* the integers */
2997             if (lcbuf->int_frac_digits != CHAR_MAX)
2998                 hv_store(RETVAL, "int_frac_digits", 15,
2999                     newSViv(lcbuf->int_frac_digits), 0);
3000             if (lcbuf->frac_digits != CHAR_MAX)
3001                 hv_store(RETVAL, "frac_digits", 11,
3002                     newSViv(lcbuf->frac_digits), 0);
3003             if (lcbuf->p_cs_precedes != CHAR_MAX)
3004                 hv_store(RETVAL, "p_cs_precedes", 13,
3005                     newSViv(lcbuf->p_cs_precedes), 0);
3006             if (lcbuf->p_sep_by_space != CHAR_MAX)
3007                 hv_store(RETVAL, "p_sep_by_space", 14,
3008                     newSViv(lcbuf->p_sep_by_space), 0);
3009             if (lcbuf->n_cs_precedes != CHAR_MAX)
3010                 hv_store(RETVAL, "n_cs_precedes", 13,
3011                     newSViv(lcbuf->n_cs_precedes), 0);
3012             if (lcbuf->n_sep_by_space != CHAR_MAX)
3013                 hv_store(RETVAL, "n_sep_by_space", 14,
3014                     newSViv(lcbuf->n_sep_by_space), 0);
3015             if (lcbuf->p_sign_posn != CHAR_MAX)
3016                 hv_store(RETVAL, "p_sign_posn", 11,
3017                     newSViv(lcbuf->p_sign_posn), 0);
3018             if (lcbuf->n_sign_posn != CHAR_MAX)
3019                 hv_store(RETVAL, "n_sign_posn", 11,
3020                     newSViv(lcbuf->n_sign_posn), 0);
3021         }
3022 #else
3023         localeconv(); /* A stub to call not_here(). */
3024 #endif
3025     OUTPUT:
3026         RETVAL
3027
3028 char *
3029 setlocale(category, locale = 0)
3030         int             category
3031         char *          locale
3032     CODE:
3033         RETVAL = setlocale(category, locale);
3034         if (RETVAL) {
3035 #ifdef USE_LOCALE_CTYPE
3036             if (category == LC_CTYPE
3037 #ifdef LC_ALL
3038                 || category == LC_ALL
3039 #endif
3040                 )
3041             {
3042                 char *newctype;
3043 #ifdef LC_ALL
3044                 if (category == LC_ALL)
3045                     newctype = setlocale(LC_CTYPE, NULL);
3046                 else
3047 #endif
3048                     newctype = RETVAL;
3049                 new_ctype(newctype);
3050             }
3051 #endif /* USE_LOCALE_CTYPE */
3052 #ifdef USE_LOCALE_COLLATE
3053             if (category == LC_COLLATE
3054 #ifdef LC_ALL
3055                 || category == LC_ALL
3056 #endif
3057                 )
3058             {
3059                 char *newcoll;
3060 #ifdef LC_ALL
3061                 if (category == LC_ALL)
3062                     newcoll = setlocale(LC_COLLATE, NULL);
3063                 else
3064 #endif
3065                     newcoll = RETVAL;
3066                 new_collate(newcoll);
3067             }
3068 #endif /* USE_LOCALE_COLLATE */
3069 #ifdef USE_LOCALE_NUMERIC
3070             if (category == LC_NUMERIC
3071 #ifdef LC_ALL
3072                 || category == LC_ALL
3073 #endif
3074                 )
3075             {
3076                 char *newnum;
3077 #ifdef LC_ALL
3078                 if (category == LC_ALL)
3079                     newnum = setlocale(LC_NUMERIC, NULL);
3080                 else
3081 #endif
3082                     newnum = RETVAL;
3083                 new_numeric(newnum);
3084             }
3085 #endif /* USE_LOCALE_NUMERIC */
3086         }
3087     OUTPUT:
3088         RETVAL
3089
3090
3091 NV
3092 acos(x)
3093         NV              x
3094
3095 NV
3096 asin(x)
3097         NV              x
3098
3099 NV
3100 atan(x)
3101         NV              x
3102
3103 NV
3104 ceil(x)
3105         NV              x
3106
3107 NV
3108 cosh(x)
3109         NV              x
3110
3111 NV
3112 floor(x)
3113         NV              x
3114
3115 NV
3116 fmod(x,y)
3117         NV              x
3118         NV              y
3119
3120 void
3121 frexp(x)
3122         NV              x
3123     PPCODE:
3124         int expvar;
3125         /* (We already know stack is long enough.) */
3126         PUSHs(sv_2mortal(newSVnv(frexp(x,&expvar))));
3127         PUSHs(sv_2mortal(newSViv(expvar)));
3128
3129 NV
3130 ldexp(x,exp)
3131         NV              x
3132         int             exp
3133
3134 NV
3135 log10(x)
3136         NV              x
3137
3138 void
3139 modf(x)
3140         NV              x
3141     PPCODE:
3142         NV intvar;
3143         /* (We already know stack is long enough.) */
3144         PUSHs(sv_2mortal(newSVnv(Perl_modf(x,&intvar))));
3145         PUSHs(sv_2mortal(newSVnv(intvar)));
3146
3147 NV
3148 sinh(x)
3149         NV              x
3150
3151 NV
3152 tan(x)
3153         NV              x
3154
3155 NV
3156 tanh(x)
3157         NV              x
3158
3159 SysRet
3160 sigaction(sig, optaction, oldaction = 0)
3161         int                     sig
3162         SV *                    optaction
3163         POSIX::SigAction        oldaction
3164     CODE:
3165 #ifdef WIN32
3166         RETVAL = not_here("sigaction");
3167 #else
3168 # This code is really grody because we're trying to make the signal
3169 # interface look beautiful, which is hard.
3170
3171         {
3172             POSIX__SigAction action;
3173             GV *siggv = gv_fetchpv("SIG", TRUE, SVt_PVHV);
3174             struct sigaction act;
3175             struct sigaction oact;
3176             sigset_t sset;
3177             sigset_t osset;
3178             POSIX__SigSet sigset;
3179             SV** svp;
3180             SV** sigsvp = hv_fetch(GvHVn(siggv),
3181                                  PL_sig_name[sig],
3182                                  strlen(PL_sig_name[sig]),
3183                                  TRUE);
3184             STRLEN n_a;
3185
3186             /* Check optaction and set action */
3187             if(SvTRUE(optaction)) {
3188                 if(sv_isa(optaction, "POSIX::SigAction"))
3189                         action = (HV*)SvRV(optaction);
3190                 else
3191                         croak("action is not of type POSIX::SigAction");
3192             }
3193             else {
3194                 action=0;
3195             }
3196
3197             /* sigaction() is supposed to look atomic. In particular, any
3198              * signal handler invoked during a sigaction() call should
3199              * see either the old or the new disposition, and not something
3200              * in between. We use sigprocmask() to make it so.
3201              */
3202             sigfillset(&sset);
3203             RETVAL=sigprocmask(SIG_BLOCK, &sset, &osset);
3204             if(RETVAL == -1)
3205                 XSRETURN(1);
3206             ENTER;
3207             /* Restore signal mask no matter how we exit this block. */
3208             SAVEDESTRUCTOR(restore_sigmask, &osset);
3209
3210             RETVAL=-1; /* In case both oldaction and action are 0. */
3211
3212             /* Remember old disposition if desired. */
3213             if (oldaction) {
3214                 svp = hv_fetch(oldaction, "HANDLER", 7, TRUE);
3215                 if(!svp)
3216                     croak("Can't supply an oldaction without a HANDLER");
3217                 if(SvTRUE(*sigsvp)) { /* TBD: what if "0"? */
3218                         sv_setsv(*svp, *sigsvp);
3219                 }
3220                 else {
3221                         sv_setpv(*svp, "DEFAULT");
3222                 }
3223                 RETVAL = sigaction(sig, (struct sigaction *)0, & oact);
3224                 if(RETVAL == -1)
3225                     XSRETURN(1);
3226                 /* Get back the mask. */
3227                 svp = hv_fetch(oldaction, "MASK", 4, TRUE);
3228                 if (sv_isa(*svp, "POSIX::SigSet")) {
3229                     IV tmp = SvIV((SV*)SvRV(*svp));
3230                     sigset = INT2PTR(sigset_t*, tmp);
3231                 }
3232                 else {
3233                     New(0, sigset, 1, sigset_t);
3234                     sv_setptrobj(*svp, sigset, "POSIX::SigSet");
3235                 }
3236                 *sigset = oact.sa_mask;
3237
3238                 /* Get back the flags. */
3239                 svp = hv_fetch(oldaction, "FLAGS", 5, TRUE);
3240                 sv_setiv(*svp, oact.sa_flags);
3241             }
3242
3243             if (action) {
3244                 /* Vector new handler through %SIG.  (We always use sighandler
3245                    for the C signal handler, which reads %SIG to dispatch.) */
3246                 svp = hv_fetch(action, "HANDLER", 7, FALSE);
3247                 if (!svp)
3248                     croak("Can't supply an action without a HANDLER");
3249                 sv_setsv(*sigsvp, *svp);
3250                 mg_set(*sigsvp);        /* handles DEFAULT and IGNORE */
3251                 if(SvPOK(*svp)) {
3252                         char *s=SvPVX(*svp);
3253                         if(strEQ(s,"IGNORE")) {
3254                                 act.sa_handler = SIG_IGN;
3255                         }
3256                         else if(strEQ(s,"DEFAULT")) {
3257                                 act.sa_handler = SIG_DFL;
3258                         }
3259                         else {
3260                                 act.sa_handler = PL_sighandlerp;
3261                         }
3262                 }
3263                 else {
3264                         act.sa_handler = PL_sighandlerp;
3265                 }
3266
3267                 /* Set up any desired mask. */
3268                 svp = hv_fetch(action, "MASK", 4, FALSE);
3269                 if (svp && sv_isa(*svp, "POSIX::SigSet")) {
3270                     IV tmp = SvIV((SV*)SvRV(*svp));
3271                     sigset = INT2PTR(sigset_t*, tmp);
3272                     act.sa_mask = *sigset;
3273                 }
3274                 else
3275                     sigemptyset(& act.sa_mask);
3276
3277                 /* Set up any desired flags. */
3278                 svp = hv_fetch(action, "FLAGS", 5, FALSE);
3279                 act.sa_flags = svp ? SvIV(*svp) : 0;
3280
3281                 /* Don't worry about cleaning up *sigsvp if this fails,
3282                  * because that means we tried to disposition a
3283                  * nonblockable signal, in which case *sigsvp is
3284                  * essentially meaningless anyway.
3285                  */
3286                 RETVAL = sigaction(sig, & act, (struct sigaction *)0);
3287             }
3288
3289             LEAVE;
3290         }
3291 #endif
3292     OUTPUT:
3293         RETVAL
3294
3295 SysRet
3296 sigpending(sigset)
3297         POSIX::SigSet           sigset
3298
3299 SysRet
3300 sigprocmask(how, sigset, oldsigset = 0)
3301         int                     how
3302         POSIX::SigSet           sigset
3303         POSIX::SigSet           oldsigset = NO_INIT
3304 INIT:
3305         if ( items < 3 ) {
3306             oldsigset = 0;
3307         }
3308         else if (sv_derived_from(ST(2), "POSIX::SigSet")) {
3309             IV tmp = SvIV((SV*)SvRV(ST(2)));
3310             oldsigset = INT2PTR(POSIX__SigSet,tmp);
3311         }
3312         else {
3313             New(0, oldsigset, 1, sigset_t);
3314             sigemptyset(oldsigset);
3315             sv_setref_pv(ST(2), "POSIX::SigSet", (void*)oldsigset);
3316         }
3317
3318 SysRet
3319 sigsuspend(signal_mask)
3320         POSIX::SigSet           signal_mask
3321
3322 void
3323 _exit(status)
3324         int             status
3325
3326 SysRet
3327 close(fd)
3328         int             fd
3329
3330 SysRet
3331 dup(fd)
3332         int             fd
3333
3334 SysRet
3335 dup2(fd1, fd2)
3336         int             fd1
3337         int             fd2
3338
3339 SysRetLong
3340 lseek(fd, offset, whence)
3341         int             fd
3342         Off_t           offset
3343         int             whence
3344
3345 SysRet
3346 nice(incr)
3347         int             incr
3348
3349 void
3350 pipe()
3351     PPCODE:
3352         int fds[2];
3353         if (pipe(fds) != -1) {
3354             EXTEND(SP,2);
3355             PUSHs(sv_2mortal(newSViv(fds[0])));
3356             PUSHs(sv_2mortal(newSViv(fds[1])));
3357         }
3358
3359 SysRet
3360 read(fd, buffer, nbytes)
3361     PREINIT:
3362         SV *sv_buffer = SvROK(ST(1)) ? SvRV(ST(1)) : ST(1);
3363     INPUT:
3364         int             fd
3365         size_t          nbytes
3366         char *          buffer = sv_grow( sv_buffer, nbytes+1 );
3367     CLEANUP:
3368         if (RETVAL >= 0) {
3369             SvCUR(sv_buffer) = RETVAL;
3370             SvPOK_only(sv_buffer);
3371             *SvEND(sv_buffer) = '\0';
3372             SvTAINTED_on(sv_buffer);
3373         }
3374
3375 SysRet
3376 setpgid(pid, pgid)
3377         pid_t           pid
3378         pid_t           pgid
3379
3380 pid_t
3381 setsid()
3382
3383 pid_t
3384 tcgetpgrp(fd)
3385         int             fd
3386
3387 SysRet
3388 tcsetpgrp(fd, pgrp_id)
3389         int             fd
3390         pid_t           pgrp_id
3391
3392 void
3393 uname()
3394     PPCODE:
3395 #ifdef HAS_UNAME
3396         struct utsname buf;
3397         if (uname(&buf) >= 0) {
3398             EXTEND(SP, 5);
3399             PUSHs(sv_2mortal(newSVpv(buf.sysname, 0)));
3400             PUSHs(sv_2mortal(newSVpv(buf.nodename, 0)));
3401             PUSHs(sv_2mortal(newSVpv(buf.release, 0)));
3402             PUSHs(sv_2mortal(newSVpv(buf.version, 0)));
3403             PUSHs(sv_2mortal(newSVpv(buf.machine, 0)));
3404         }
3405 #else
3406         uname((char *) 0); /* A stub to call not_here(). */
3407 #endif
3408
3409 SysRet
3410 write(fd, buffer, nbytes)
3411         int             fd
3412         char *          buffer
3413         size_t          nbytes
3414
3415 SV *
3416 tmpnam()
3417     PREINIT:
3418         STRLEN i;
3419         int len;
3420     CODE:
3421         RETVAL = newSVpvn("", 0);
3422         SvGROW(RETVAL, L_tmpnam);
3423         len = strlen(tmpnam(SvPV(RETVAL, i)));
3424         SvCUR_set(RETVAL, len);
3425     OUTPUT:
3426         RETVAL
3427
3428 void
3429 abort()
3430
3431 int
3432 mblen(s, n)
3433         char *          s
3434         size_t          n
3435
3436 size_t
3437 mbstowcs(s, pwcs, n)
3438         wchar_t *       s
3439         char *          pwcs
3440         size_t          n
3441
3442 int
3443 mbtowc(pwc, s, n)
3444         wchar_t *       pwc
3445         char *          s
3446         size_t          n
3447
3448 int
3449 wcstombs(s, pwcs, n)
3450         char *          s
3451         wchar_t *       pwcs
3452         size_t          n
3453
3454 int
3455 wctomb(s, wchar)
3456         char *          s
3457         wchar_t         wchar
3458
3459 int
3460 strcoll(s1, s2)
3461         char *          s1
3462         char *          s2
3463
3464 void
3465 strtod(str)
3466         char *          str
3467     PREINIT:
3468         double num;
3469         char *unparsed;
3470     PPCODE:
3471         SET_NUMERIC_LOCAL();
3472         num = strtod(str, &unparsed);
3473         PUSHs(sv_2mortal(newSVnv(num)));
3474         if (GIMME == G_ARRAY) {
3475             EXTEND(SP, 1);
3476             if (unparsed)
3477                 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3478             else
3479                 PUSHs(&PL_sv_undef);
3480         }
3481
3482 void
3483 strtol(str, base = 0)
3484         char *          str
3485         int             base
3486     PREINIT:
3487         long num;
3488         char *unparsed;
3489     PPCODE:
3490         num = strtol(str, &unparsed, base);
3491 #if IVSIZE <= LONGSIZE
3492         if (num < IV_MIN || num > IV_MAX)
3493             PUSHs(sv_2mortal(newSVnv((double)num)));
3494         else
3495 #endif
3496             PUSHs(sv_2mortal(newSViv((IV)num)));
3497         if (GIMME == G_ARRAY) {
3498             EXTEND(SP, 1);
3499             if (unparsed)
3500                 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3501             else
3502                 PUSHs(&PL_sv_undef);
3503         }
3504
3505 void
3506 strtoul(str, base = 0)
3507         char *          str
3508         int             base
3509     PREINIT:
3510         unsigned long num;
3511         char *unparsed;
3512     PPCODE:
3513         num = strtoul(str, &unparsed, base);
3514         if (num <= IV_MAX)
3515             PUSHs(sv_2mortal(newSViv((IV)num)));
3516         else
3517             PUSHs(sv_2mortal(newSVnv((double)num)));
3518         if (GIMME == G_ARRAY) {
3519             EXTEND(SP, 1);
3520             if (unparsed)
3521                 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3522             else
3523                 PUSHs(&PL_sv_undef);
3524         }
3525
3526 void
3527 strxfrm(src)
3528         SV *            src
3529     CODE:
3530         {
3531           STRLEN srclen;
3532           STRLEN dstlen;
3533           char *p = SvPV(src,srclen);
3534           srclen++;
3535           ST(0) = sv_2mortal(NEWSV(800,srclen));
3536           dstlen = strxfrm(SvPVX(ST(0)), p, (size_t)srclen);
3537           if (dstlen > srclen) {
3538               dstlen++;
3539               SvGROW(ST(0), dstlen);
3540               strxfrm(SvPVX(ST(0)), p, (size_t)dstlen);
3541               dstlen--;
3542           }
3543           SvCUR(ST(0)) = dstlen;
3544             SvPOK_only(ST(0));
3545         }
3546
3547 SysRet
3548 mkfifo(filename, mode)
3549         char *          filename
3550         Mode_t          mode
3551     CODE:
3552         TAINT_PROPER("mkfifo");
3553         RETVAL = mkfifo(filename, mode);
3554     OUTPUT:
3555         RETVAL
3556
3557 SysRet
3558 tcdrain(fd)
3559         int             fd
3560
3561
3562 SysRet
3563 tcflow(fd, action)
3564         int             fd
3565         int             action
3566
3567
3568 SysRet
3569 tcflush(fd, queue_selector)
3570         int             fd
3571         int             queue_selector
3572
3573 SysRet
3574 tcsendbreak(fd, duration)
3575         int             fd
3576         int             duration
3577
3578 char *
3579 asctime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
3580         int             sec
3581         int             min
3582         int             hour
3583         int             mday
3584         int             mon
3585         int             year
3586         int             wday
3587         int             yday
3588         int             isdst
3589     CODE:
3590         {
3591             struct tm mytm;
3592             init_tm(&mytm);     /* XXX workaround - see init_tm() above */
3593             mytm.tm_sec = sec;
3594             mytm.tm_min = min;
3595             mytm.tm_hour = hour;
3596             mytm.tm_mday = mday;
3597             mytm.tm_mon = mon;
3598             mytm.tm_year = year;
3599             mytm.tm_wday = wday;
3600             mytm.tm_yday = yday;
3601             mytm.tm_isdst = isdst;
3602             RETVAL = asctime(&mytm);
3603         }
3604     OUTPUT:
3605         RETVAL
3606
3607 long
3608 clock()
3609
3610 char *
3611 ctime(time)
3612         Time_t          &time
3613
3614 void
3615 times()
3616         PPCODE:
3617         struct tms tms;
3618         clock_t realtime;
3619         realtime = times( &tms );
3620         EXTEND(SP,5);
3621         PUSHs( sv_2mortal( newSViv( (IV) realtime ) ) );
3622         PUSHs( sv_2mortal( newSViv( (IV) tms.tms_utime ) ) );
3623         PUSHs( sv_2mortal( newSViv( (IV) tms.tms_stime ) ) );
3624         PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cutime ) ) );
3625         PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cstime ) ) );
3626
3627 double
3628 difftime(time1, time2)
3629         Time_t          time1
3630         Time_t          time2
3631
3632 SysRetLong
3633 mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
3634         int             sec
3635         int             min
3636         int             hour
3637         int             mday
3638         int             mon
3639         int             year
3640         int             wday
3641         int             yday
3642         int             isdst
3643     CODE:
3644         {
3645             struct tm mytm;
3646             init_tm(&mytm);     /* XXX workaround - see init_tm() above */
3647             mytm.tm_sec = sec;
3648             mytm.tm_min = min;
3649             mytm.tm_hour = hour;
3650             mytm.tm_mday = mday;
3651             mytm.tm_mon = mon;
3652             mytm.tm_year = year;
3653             mytm.tm_wday = wday;
3654             mytm.tm_yday = yday;
3655             mytm.tm_isdst = isdst;
3656             RETVAL = mktime(&mytm);
3657         }
3658     OUTPUT:
3659         RETVAL
3660
3661 #XXX: if $xsubpp::WantOptimize is always the default
3662 #     sv_setpv(TARG, ...) could be used rather than
3663 #     ST(0) = sv_2mortal(newSVpv(...))
3664 void
3665 strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
3666         char *          fmt
3667         int             sec
3668         int             min
3669         int             hour
3670         int             mday
3671         int             mon
3672         int             year
3673         int             wday
3674         int             yday
3675         int             isdst
3676     CODE:
3677         {
3678             char *buf = my_strftime(fmt, sec, min, hour, mday, mon, year, wday, yday, isdst);
3679             if (buf) {
3680                 ST(0) = sv_2mortal(newSVpv(buf, 0));
3681                 Safefree(buf);
3682             }
3683         }
3684
3685 void
3686 tzset()
3687
3688 void
3689 tzname()
3690     PPCODE:
3691         EXTEND(SP,2);
3692         PUSHs(sv_2mortal(newSVpvn(tzname[0],strlen(tzname[0]))));
3693         PUSHs(sv_2mortal(newSVpvn(tzname[1],strlen(tzname[1]))));
3694
3695 SysRet
3696 access(filename, mode)
3697         char *          filename
3698         Mode_t          mode
3699
3700 char *
3701 ctermid(s = 0)
3702         char *          s = 0;
3703
3704 char *
3705 cuserid(s = 0)
3706         char *          s = 0;
3707
3708 SysRetLong
3709 fpathconf(fd, name)
3710         int             fd
3711         int             name
3712
3713 SysRetLong
3714 pathconf(filename, name)
3715         char *          filename
3716         int             name
3717
3718 SysRet
3719 pause()
3720
3721 SysRet
3722 setgid(gid)
3723         Gid_t           gid
3724
3725 SysRet
3726 setuid(uid)
3727         Uid_t           uid
3728
3729 SysRetLong
3730 sysconf(name)
3731         int             name
3732
3733 char *
3734 ttyname(fd)
3735         int             fd
3736
3737 char *
3738 getcwd()
3739         PPCODE:
3740 #ifdef HAS_GETCWD
3741         char *          buf;
3742         int             buflen = 128;
3743         int             i;
3744
3745         New(0, buf, buflen, char);
3746         /* Many getcwd()s know how to automatically allocate memory
3747          * for the directory if the buffer argument is NULL but...
3748          * (1) we cannot assume all getcwd()s do that
3749          * (2) this may interfere with Perl's malloc
3750          * So let's not.  --jhi */
3751         while ((getcwd(buf, buflen) == NULL) && errno == ERANGE) {
3752             buflen += 128;
3753             if (buflen > MAXPATHLEN) {
3754                 Safefree(buf);
3755                 buf = NULL;
3756                 break;
3757             }
3758             Renew(buf, buflen, char);
3759         }
3760         if (buf) {
3761             PUSHs(sv_2mortal(newSVpv(buf, 0)));
3762             Safefree(buf);
3763         }
3764         else
3765             PUSHs(&PL_sv_undef);
3766 #else
3767         require_pv("Cwd.pm");
3768         /* Module require may have grown the stack */
3769         SPAGAIN;
3770         PUSHMARK(sp);
3771         PUTBACK;
3772         XSRETURN(call_pv("Cwd::cwd", GIMME_V));
3773 #endif