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