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