6 * Ideally this should be somewhere down in the includes
7 * but putting it in other places is giving compiler errors.
8 * Also here I am unable to check for HAS_UNAME since it wouldn't have
9 * yet come into the file at this stage - sgp 18th Oct 2000
11 #include <sys/utsname.h>
14 #define PERL_NO_GET_CONTEXT
17 #define PERLIO_NOT_STDIO 1
20 #if defined(PERL_IMPLICIT_SYS)
24 # define open PerlLIO_open3
27 #ifdef I_DIRENT /* XXX maybe better to just rely on perl.h? */
32 #include <sys/errno2.h>
61 # define M_E 2.71828182845904523536028747135266250
64 # define M_LOG2E 1.44269504088896340735992468100189214
67 # define M_LOG10E 0.434294481903251827651128918916605082
70 # define M_LN2 0.693147180559945309417232121458176568
73 # define M_LN10 2.30258509299404568401799145468436421
76 # define M_PI 3.14159265358979323846264338327950288
79 # define M_PI_2 1.57079632679489661923132169163975144
82 # define M_PI_4 0.785398163397448309615660845819875721
85 # define M_1_PI 0.318309886183790671537767526745028724
88 # define M_2_PI 0.636619772367581343075535053490057448
91 # define M_2_SQRTPI 1.12837916709551257389615890312154517
94 # define M_SQRT2 1.41421356237309504880168872420969808
97 # define M_SQRT1_2 0.707106781186547524400844362104849039
100 #if !defined(INFINITY) && defined(NV_INF)
101 # define INFINITY NV_INF
104 #if !defined(NAN) && defined(NV_NAN)
108 #if !defined(Inf) && defined(NV_INF)
112 #if !defined(NaN) && defined(NV_NAN)
116 /* We will have an emulation. */
118 # define FP_INFINITE 0
121 # define FP_SUBNORMAL 3
125 /* We will have an emulation. */
127 # define FE_TONEAREST 0
128 # define FE_TOWARDZERO 1
129 # define FE_DOWNWARD 2
135 acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp
136 log log10 modf pow sin sinh sqrt tan tanh
138 * Implemented in core:
140 atan2 cos exp log pow sin sqrt
144 acosh asinh atanh cbrt copysign erf erfc exp2 expm1 fdim fma fmax
145 fmin fpclassify hypot ilogb isfinite isgreater isgreaterequal isinf
146 isless islessequal islessgreater isnan isnormal isunordered lgamma
147 log1p log2 logb lrint lround nan nearbyint nextafter nexttoward remainder
148 remquo rint round scalbn signbit tgamma trunc
151 http://pubs.opengroup.org/onlinepubs/009695399/basedefs/math.h.html
153 * Berkeley/SVID extensions:
157 * Configure already (5.21.0) scans for:
159 fpclassify isfinite isinf isnan ilogb*l* signbit
161 * For floating-point round mode (which matters for e.g. lrint and rint)
163 fegetround fesetround
167 /* XXX Constant FP_FAST_FMA (if true, FMA is faster) */
169 /* XXX Add ldiv(), lldiv()? It's C99, but from stdlib.h, not math.h */
171 /* XXX Beware old gamma() -- one cannot know whether that is the
172 * gamma or the log of gamma, that's why the new tgamma and lgamma.
173 * Though also remember tgamma_r and lgamma_r. */
175 /* XXX The truthiness of acosh() is the canary for all of the
176 * C99 math. This is very likely wrong, especially in non-UNIX lands
177 * like Win32 and VMS, but also older UNIXes have issues. For Win32,
178 * and other non-fully-C99, we later do some undefines for these interfaces.
180 * But we are very trying very hard to avoid introducing separate Configure
181 * symbols for all the 40-ish new math symbols. Especially since the set
182 * of missing functions doesn't seem to follow any patterns. */
185 # if defined(USE_LONG_DOUBLE) && defined(HAS_ILOGBL)
186 /* There's already a symbol for ilogbl, we will use its truthiness
187 * as the canary for all the *l variants being defined. */
188 # define c99_acosh acoshl
189 # define c99_asinh asinhl
190 # define c99_atanh atanhl
191 # define c99_cbrt cbrtl
192 # define c99_copysign copysignl
193 # define c99_erf erfl
194 # define c99_erfc erfcl
195 # define c99_exp2 exp2l
196 # define c99_expm1 expm1l
197 # define c99_fdim fdiml
198 # define c99_fma fmal
199 # define c99_fmax fmaxl
200 # define c99_fmin fminl
201 # define c99_hypot hypotl
202 # define c99_ilogb ilogbl
203 # define c99_lgamma lgammal
204 # define c99_log1p log1pl
205 # define c99_log2 log2l
206 # define c99_logb logbl
207 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
208 # define c99_lrint llrintl
210 # define c99_lrint lrintl
212 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
213 # define c99_lround llroundl
215 # define c99_lround lroundl
217 # define c99_nan nanl
218 # define c99_nearbyint nearbyintl
219 # define c99_nextafter nextafterl
220 # define c99_nexttoward nexttowardl
221 # define c99_remainder remainderl
222 # define c99_remquo remquol
223 # define c99_rint rintl
224 # define c99_round roundl
225 # define c99_scalbn scalbnl
226 # ifdef HAS_SIGNBIT /* possibly bad assumption */
227 # define c99_signbit signbitl
229 # define c99_tgamma tgammal
230 # define c99_trunc truncl
232 # define c99_acosh acosh
233 # define c99_asinh asinh
234 # define c99_atanh atanh
235 # define c99_cbrt cbrt
236 # define c99_copysign copysign
238 # define c99_erfc erfc
239 # define c99_exp2 exp2
240 # define c99_expm1 expm1
241 # define c99_fdim fdim
243 # define c99_fmax fmax
244 # define c99_fmin fmin
245 # define c99_hypot hypot
246 # define c99_ilogb ilogb
247 # define c99_lgamma lgamma
248 # define c99_log1p log1p
249 # define c99_log2 log2
250 # define c99_logb logb
251 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
252 # define c99_lrint llrint
254 # define c99_lrint lrint
256 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
257 # define c99_lround llround
259 # define c99_lround lround
262 # define c99_nearbyint nearbyint
263 # define c99_nextafter nextafter
264 # define c99_nexttoward nexttoward
265 # define c99_remainder remainder
266 # define c99_remquo remquo
267 # define c99_rint rint
268 # define c99_round round
269 # define c99_scalbn scalbn
270 /* We already define Perl_signbit in perl.h. */
272 # define c99_signbit signbit
274 # define c99_tgamma tgamma
275 # define c99_trunc trunc
280 # define isunordered(x, y) (Perl_isnan(x) || Perl_isnan(y))
281 # elif defined(HAS_UNORDERED)
282 # define isunordered(x, y) unordered(x, y)
286 # if !defined(isgreater) && defined(isunordered)
287 # define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
288 # define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
289 # define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
290 # define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
291 # define islessgreater(x, y) (!isunordered((x), (y)) && \
292 ((x) > (y) || (y) > (x)))
295 /* Check both the Configure symbol and the macro-ness (like C99 promises). */
296 # if defined(HAS_FPCLASSIFY) && defined(fpclassify)
297 # define c99_fpclassify fpclassify
299 /* Like isnormal(), the isfinite(), isinf(), and isnan() are also C99
300 and also (sizeof-arg-aware) macros, but they are already well taken
301 care of by Configure et al, and defined in perl.h as
302 Perl_isfinite(), Perl_isinf(), and Perl_isnan(). */
304 # define c99_isnormal isnormal
306 # ifdef isgreater /* canary for all the C99 is*<cmp>* macros. */
307 # define c99_isgreater isgreater
308 # define c99_isgreaterequal isgreaterequal
309 # define c99_isless isless
310 # define c99_islessequal islessequal
311 # define c99_islessgreater islessgreater
312 # define c99_isunordered isunordered
316 /* If on legacy platforms, and not using gcc, some C99 math interfaces
317 * might be missing, turn them off so that the emulations hopefully
318 * kick in. This is admittedly nasty, and fragile, but the alternative
319 * is to have Configure scans for all the 40+ interfaces.
321 * In other words: if you have an incomplete (or broken) C99 math interface,
322 * #undef the c99_foo here, and let the emulations kick in. */
326 /* HP-UX on PA-RISC is missing certain C99 math functions,
327 * but on IA64 (Integrity) these do exist, and even on
328 * recent enough HP-UX (cc) releases. */
329 # if defined(__hpux) && (defined(__hppa) || defined(_PA_RISC))
330 /* lowest known release, could be lower */
331 # if defined(__HP_cc) && __HP_cc >= 111120
333 # undef c99_nexttoward
341 # undef c99_fpclassify /* hpux 10.20 has fpclassify but different api */
345 # undef c99_nearbyint
346 # undef c99_nexttoward
355 # if defined(__irix__)
360 # if defined(__osf__) /* Tru64 */
365 # undef c99_fpclassify
368 # undef c99_isunordered
371 # undef c99_nearbyint
372 # undef c99_nexttoward
381 /* XXX Regarding C99 math.h, VMS seems to be missing these:
383 lround nan nearbyint round scalbn llrint
389 # undef c99_nearbyint
392 /* Have lrint but not llrint. */
393 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
398 /* XXX Regarding C99 math.h, Win32 seems to be missing these:
400 erf erfc exp2 fdim fma fmax fmin fpclassify ilogb lgamma log1p log2 lrint
401 remquo rint signbit tgamma trunc
403 Win32 does seem to have these:
405 acosh asinh atanh cbrt copysign cosh expm1 hypot log10 nan
406 nearbyint nextafter nexttoward remainder round scalbn
408 And the Bessel functions are defined like _this.
431 /* Some APIs exist under Win32 with "underbar" names. */
434 # undef c99_nextafter
435 # define c99_hypot _hypot
436 # define c99_logb _logb
437 # define c99_nextafter _nextafter
439 # define bessel_j0 _j0
440 # define bessel_j1 _j1
441 # define bessel_jn _jn
442 # define bessel_y0 _y0
443 # define bessel_y1 _y1
444 # define bessel_yn _yn
449 # undef c99_nexttoward
452 /* The Bessel functions: BSD, SVID, XPG4, and POSIX. But not C99. */
454 # if defined(USE_LONG_DOUBLE) && defined(HAS_J0L)
455 # define bessel_j0 j0l
456 # define bessel_j1 j1l
457 # define bessel_jn jnl
458 # define bessel_y0 y0l
459 # define bessel_y1 y1l
460 # define bessel_yn ynl
462 # define bessel_j0 j0
463 # define bessel_j1 j1
464 # define bessel_jn jn
465 # define bessel_y0 y0
466 # define bessel_y1 y1
467 # define bessel_yn yn
471 /* Emulations for missing math APIs.
473 * Keep in mind that the point of many of these functions is that
474 * they, if available, are supposed to give more precise/more
475 * numerically stable results.
477 * See e.g. http://www.johndcook.com/math_h.html
481 static NV my_acosh(NV x)
483 return Perl_log(x + Perl_sqrt(x * x - 1));
485 # define c99_acosh my_acosh
489 static NV my_asinh(NV x)
491 return Perl_log(x + Perl_sqrt(x * x + 1));
493 # define c99_asinh my_asinh
497 static NV my_atanh(NV x)
499 return (Perl_log(1 + x) - Perl_log(1 - x)) / 2;
501 # define c99_atanh my_atanh
505 static NV my_cbrt(NV x)
507 static const NV one_third = (NV)1.0/3;
508 return x >= 0.0 ? Perl_pow(x, one_third) : -Perl_pow(-x, one_third);
510 # define c99_cbrt my_cbrt
514 static NV my_copysign(NV x, NV y)
516 return y >= 0 ? (x < 0 ? -x : x) : (x < 0 ? x : -x);
518 # define c99_copysign my_copysign
521 /* XXX cosh (though c89) */
524 static NV my_erf(NV x)
526 /* http://www.johndcook.com/cpp_erf.html -- public domain */
528 NV a2 = -0.284496736;
530 NV a4 = -1.453152027;
534 int sign = x < 0 ? -1 : 1; /* Save the sign. */
537 /* Abramowitz and Stegun formula 7.1.26 */
538 t = 1.0 / (1.0 + p * x);
539 y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1) * t * Perl_exp(-x*x);
543 # define c99_erf my_erf
547 static NV my_erfc(NV x) {
548 /* This is not necessarily numerically stable, but better than nothing. */
549 return 1.0 - c99_erf(x);
551 # define c99_erfc my_erfc
555 static NV my_exp2(NV x)
557 return Perl_pow((NV)2.0, x);
559 # define c99_exp2 my_exp2
563 static NV my_expm1(NV x)
565 if (PERL_ABS(x) < 1e-5)
566 /* http://www.johndcook.com/cpp_expm1.html -- public domain.
567 * Also including the cubic term. */
568 /* Probably not enough for long doubles. */
569 return x * (1.0 + x * (0.5 + x / 6.0)); /* Taylor series */
571 return Perl_exp(x) - 1;
573 # define c99_expm1 my_expm1
577 static NV my_fdim(NV x, NV y)
579 return x > y ? x - y : 0;
581 # define c99_fdim my_fdim
585 static NV my_fmax(NV x, NV y)
588 return Perl_isnan(y) ? NV_NAN : y;
589 } else if (Perl_isnan(y)) {
592 return x > y ? x : y;
594 # define c99_fmax my_fmax
598 static NV my_fmin(NV x, NV y)
601 return Perl_isnan(y) ? NV_NAN : y;
602 } else if (Perl_isnan(y)) {
605 return x < y ? x : y;
607 # define c99_fmin my_fmin
610 #ifndef c99_fpclassify
612 static IV my_fpclassify(NV x)
614 #ifdef Perl_fp_class_inf
615 if (Perl_fp_class_inf(x)) return FP_INFINITE;
616 if (Perl_fp_class_nan(x)) return FP_NAN;
617 if (Perl_fp_class_norm(x)) return FP_NORMAL;
618 if (Perl_fp_class_denorm(x)) return FP_SUBNORMAL;
619 if (Perl_fp_class_zero(x)) return FP_ZERO;
620 # define c99_fpclassify my_fpclassify
628 static NV my_hypot(NV x, NV y)
630 /* http://en.wikipedia.org/wiki/Hypot */
632 x = PERL_ABS(x); /* Take absolute values. */
638 if (x < y) { /* Swap so that y is less. */
644 return x * Perl_sqrt(1.0 + t * t);
646 # define c99_hypot my_hypot
650 static IV my_ilogb(NV x)
652 return (IV)(Perl_log(x) * M_LOG2E);
654 # define c99_ilogb my_ilogb
657 /* XXX lgamma -- non-trivial */
660 static NV my_log1p(NV x)
662 /* http://www.johndcook.com/cpp_log_one_plus_x.html -- public domain.
663 * Including also quadratic term. */
664 if (PERL_ABS(x) > 1e-4)
665 return Perl_log(1.0 + x);
667 /* Probably not enough for long doubles. */
668 return x * (1.0 - x * (-x / 2.0 + x / 3.0)); /* Taylor series */
670 # define c99_log1p my_log1p
674 static NV my_log2(NV x)
676 return Perl_log(x) * M_LOG2E;
678 # define c99_log2 my_log2
685 static int my_fegetround()
687 #ifdef HAS_FEGETROUND
689 #elif defined(FLT_ROUNDS)
691 #elif defined(HAS_FPGETROUND)
692 switch (fpgetround()) {
694 case FP_RN: return FE_TONEAREST;
695 case FP_RZ: return FE_TOWARDZERO;
696 case FP_RM: return FE_DOWNWARD;
697 case FE_RP: return FE_UPWARD;
704 /* Toward closest integer. */
705 #define MY_ROUND_NEAREST(x) ((NV)((IV)((x) >= 0.0 ? (x) + 0.5 : (x) - 0.5)))
708 #define MY_ROUND_TRUNC(x) ((NV)((IV)(x)))
710 /* Toward minus infinity. */
711 #define MY_ROUND_DOWN(x) ((NV)((IV)((x) >= 0.0 ? (x) : (x) - 0.5)))
713 /* Toward plus infinity. */
714 #define MY_ROUND_UP(x) ((NV)((IV)((x) >= 0.0 ? (x) + 0.5 : (x))))
716 static NV my_rint(NV x)
719 switch (my_fegetround()) {
721 case FE_TONEAREST: return MY_ROUND_NEAREST(x);
722 case FE_TOWARDZERO: return MY_ROUND_TRUNC(x);
723 case FE_DOWNWARD: return MY_ROUND_DOWN(x);
724 case FE_UPWARD: return MY_ROUND_UP(x);
726 #elif defined(HAS_FPGETROUND)
727 switch (fpgetround()) {
729 case FP_RN: return MY_ROUND_NEAREST(x);
730 case FP_RZ: return MY_ROUND_TRUNC(x);
731 case FP_RM: return MY_ROUND_DOWN(x);
732 case FE_RP: return MY_ROUND_UP(x);
739 /* XXX nearbyint() and rint() are not really identical -- but the difference
740 * is messy: nearbyint is defined NOT to raise FE_INEXACT floating point
741 * exceptions, while rint() is defined to MAYBE raise them. At the moment
742 * Perl is blissfully unaware of such fine detail of floating point. */
743 #ifndef c99_nearbyint
745 # define c99_nearbyrint my_rint
751 static IV my_lrint(NV x)
753 return (IV)my_rint(x);
755 # define c99_lrint my_lrint
760 static IV my_lround(NV x)
762 return (IV)return MY_ROUND_NEAREST(x);
764 # define c99_lround my_lround
773 # define c99_rint my_rint
778 static NV my_round(NV x)
780 return MY_ROUND_NEAREST(x);
782 # define c99_round my_round
786 # if defined(Perl_ldexp) && FLT_RADIX == 2
787 static NV my_scalbn(NV x, int y)
789 return Perl_ldexp(x, y);
791 # define c99_scalbn my_scalbn
795 /* XXX sinh (though c89) */
799 static NV my_tgamma(NV x)
801 double l = c99_lgamma(x);
802 return signgam * Perl_exp(l); /* XXX evil global signgam, need lgamma_r */
804 # define c99_tgamma my_tgamma
805 /* XXX tgamma without lgamma -- non-trivial */
809 /* XXX tanh (though c89) */
812 static NV my_trunc(NV x)
814 return MY_ROUND_TRUNC(x);
816 # define c99_trunc my_trunc
819 /* XXX This comment is just to make I_TERMIO and I_SGTTY visible to
820 metaconfig for future extension writers. We don't use them in POSIX.
821 (This is really sneaky :-) --AD
823 #if defined(I_TERMIOS)
832 #include <sys/stat.h>
833 #include <sys/types.h>
841 # if !defined(WIN32) && !defined(__CYGWIN__) && !defined(NETWARE) && !defined(__UWIN__)
842 extern char *tzname[];
845 #if !defined(WIN32) && !defined(__UWIN__) || (defined(__MINGW32__) && !defined(tzname))
846 char *tzname[] = { "" , "" };
850 #if defined(__VMS) && !defined(__POSIX_SOURCE)
852 # include <utsname.h>
855 # define mkfifo(a,b) (not_here("mkfifo"),-1)
857 /* The POSIX notion of ttyname() is better served by getname() under VMS */
858 static char ttnambuf[64];
859 # define ttyname(fd) (isatty(fd) > 0 ? getname(fd,ttnambuf,0) : NULL)
862 #if defined (__CYGWIN__)
863 # define tzname _tzname
865 #if defined (WIN32) || defined (NETWARE)
867 # define mkfifo(a,b) not_here("mkfifo")
868 # define ttyname(a) (char*)not_here("ttyname")
869 # define sigset_t long
872 # define mode_t short
875 # define mode_t short
877 # define tzset() not_here("tzset")
879 # ifndef _POSIX_OPEN_MAX
880 # define _POSIX_OPEN_MAX FOPEN_MAX /* XXX bogus ? */
883 # define sigaction(a,b,c) not_here("sigaction")
884 # define sigpending(a) not_here("sigpending")
885 # define sigprocmask(a,b,c) not_here("sigprocmask")
886 # define sigsuspend(a) not_here("sigsuspend")
887 # define sigemptyset(a) not_here("sigemptyset")
888 # define sigaddset(a,b) not_here("sigaddset")
889 # define sigdelset(a,b) not_here("sigdelset")
890 # define sigfillset(a) not_here("sigfillset")
891 # define sigismember(a,b) not_here("sigismember")
895 # define setuid(a) not_here("setuid")
896 # define setgid(a) not_here("setgid")
898 # define strtold(s1,s2) not_here("strtold")
903 # define mkfifo(a,b) not_here("mkfifo")
904 # else /* !( defined OS2 ) */
906 # define mkfifo(path, mode) (mknod((path), (mode) | S_IFIFO, 0))
909 # endif /* !HAS_MKFIFO */
914 # include <sys/times.h>
916 # include <sys/utsname.h>
918 # include <sys/wait.h>
922 #endif /* WIN32 || NETWARE */
926 typedef long SysRetLong;
927 typedef sigset_t* POSIX__SigSet;
928 typedef HV* POSIX__SigAction;
930 typedef struct termios* POSIX__Termios;
931 #else /* Define termios types to int, and call not_here for the functions.*/
932 #define POSIX__Termios int
936 #define cfgetispeed(x) not_here("cfgetispeed")
937 #define cfgetospeed(x) not_here("cfgetospeed")
938 #define tcdrain(x) not_here("tcdrain")
939 #define tcflush(x,y) not_here("tcflush")
940 #define tcsendbreak(x,y) not_here("tcsendbreak")
941 #define cfsetispeed(x,y) not_here("cfsetispeed")
942 #define cfsetospeed(x,y) not_here("cfsetospeed")
943 #define ctermid(x) (char *) not_here("ctermid")
944 #define tcflow(x,y) not_here("tcflow")
945 #define tcgetattr(x,y) not_here("tcgetattr")
946 #define tcsetattr(x,y,z) not_here("tcsetattr")
949 /* Possibly needed prototypes */
952 double strtod (const char *, char **);
953 long strtol (const char *, char **, int);
954 unsigned long strtoul (const char *, char **, int);
956 long double strtold (const char *, char **);
963 #define difftime(a,b) not_here("difftime")
966 #ifndef HAS_FPATHCONF
967 #define fpathconf(f,n) (SysRetLong) not_here("fpathconf")
970 #define mktime(a) not_here("mktime")
973 #define nice(a) not_here("nice")
976 #define pathconf(f,n) (SysRetLong) not_here("pathconf")
979 #define sysconf(n) (SysRetLong) not_here("sysconf")
982 #define readlink(a,b,c) not_here("readlink")
985 #define setpgid(a,b) not_here("setpgid")
988 #define setsid() not_here("setsid")
991 #define strcoll(s1,s2) not_here("strcoll")
994 #define strtod(s1,s2) not_here("strtod")
997 #define strtold(s1,s2) not_here("strtold")
1000 #define strtol(s1,s2,b) not_here("strtol")
1003 #define strtoul(s1,s2,b) not_here("strtoul")
1006 #define strxfrm(s1,s2,n) not_here("strxfrm")
1008 #ifndef HAS_TCGETPGRP
1009 #define tcgetpgrp(a) not_here("tcgetpgrp")
1011 #ifndef HAS_TCSETPGRP
1012 #define tcsetpgrp(a,b) not_here("tcsetpgrp")
1016 #define times(a) not_here("times")
1017 #endif /* NETWARE */
1020 #define uname(a) not_here("uname")
1023 #define waitpid(a,b,c) not_here("waitpid")
1028 #define mblen(a,b) not_here("mblen")
1031 #ifndef HAS_MBSTOWCS
1032 #define mbstowcs(s, pwcs, n) not_here("mbstowcs")
1035 #define mbtowc(pwc, s, n) not_here("mbtowc")
1037 #ifndef HAS_WCSTOMBS
1038 #define wcstombs(s, pwcs, n) not_here("wcstombs")
1041 #define wctomb(s, wchar) not_here("wcstombs")
1043 #if !defined(HAS_MBLEN) && !defined(HAS_MBSTOWCS) && !defined(HAS_MBTOWC) && !defined(HAS_WCSTOMBS) && !defined(HAS_WCTOMB)
1044 /* If we don't have these functions, then we wouldn't have gotten a typedef
1045 for wchar_t, the wide character type. Defining wchar_t allows the
1046 functions referencing it to compile. Its actual type is then meaningless,
1047 since without the above functions, all sections using it end up calling
1048 not_here() and croak. --Kaveh Ghazi (ghazi@noc.rutgers.edu) 9/18/94. */
1050 #define wchar_t char
1054 #ifndef HAS_LOCALECONV
1055 # define localeconv() not_here("localeconv")
1057 struct lconv_offset {
1062 const struct lconv_offset lconv_strings[] = {
1063 #ifdef USE_LOCALE_NUMERIC
1064 {"decimal_point", STRUCT_OFFSET(struct lconv, decimal_point)},
1065 {"thousands_sep", STRUCT_OFFSET(struct lconv, thousands_sep)},
1066 # ifndef NO_LOCALECONV_GROUPING
1067 {"grouping", STRUCT_OFFSET(struct lconv, grouping)},
1070 #ifdef USE_LOCALE_MONETARY
1071 {"int_curr_symbol", STRUCT_OFFSET(struct lconv, int_curr_symbol)},
1072 {"currency_symbol", STRUCT_OFFSET(struct lconv, currency_symbol)},
1073 {"mon_decimal_point", STRUCT_OFFSET(struct lconv, mon_decimal_point)},
1074 # ifndef NO_LOCALECONV_MON_THOUSANDS_SEP
1075 {"mon_thousands_sep", STRUCT_OFFSET(struct lconv, mon_thousands_sep)},
1077 # ifndef NO_LOCALECONV_MON_GROUPING
1078 {"mon_grouping", STRUCT_OFFSET(struct lconv, mon_grouping)},
1080 {"positive_sign", STRUCT_OFFSET(struct lconv, positive_sign)},
1081 {"negative_sign", STRUCT_OFFSET(struct lconv, negative_sign)},
1086 #ifdef USE_LOCALE_NUMERIC
1088 /* The Linux man pages say these are the field names for the structure
1089 * components that are LC_NUMERIC; the rest being LC_MONETARY */
1090 # define isLC_NUMERIC_STRING(name) (strcmp(name, "decimal_point") \
1091 || strcmp(name, "thousands_sep") \
1093 /* There should be no harm done \
1094 * checking for this, even if \
1095 * NO_LOCALECONV_GROUPING */ \
1096 || strcmp(name, "grouping"))
1098 # define isLC_NUMERIC_STRING(name) (0)
1101 const struct lconv_offset lconv_integers[] = {
1102 #ifdef USE_LOCALE_MONETARY
1103 {"int_frac_digits", STRUCT_OFFSET(struct lconv, int_frac_digits)},
1104 {"frac_digits", STRUCT_OFFSET(struct lconv, frac_digits)},
1105 {"p_cs_precedes", STRUCT_OFFSET(struct lconv, p_cs_precedes)},
1106 {"p_sep_by_space", STRUCT_OFFSET(struct lconv, p_sep_by_space)},
1107 {"n_cs_precedes", STRUCT_OFFSET(struct lconv, n_cs_precedes)},
1108 {"n_sep_by_space", STRUCT_OFFSET(struct lconv, n_sep_by_space)},
1109 {"p_sign_posn", STRUCT_OFFSET(struct lconv, p_sign_posn)},
1110 {"n_sign_posn", STRUCT_OFFSET(struct lconv, n_sign_posn)},
1115 #endif /* HAS_LOCALECONV */
1117 #ifdef HAS_LONG_DOUBLE
1118 # if LONG_DOUBLESIZE > NVSIZE
1119 # undef HAS_LONG_DOUBLE /* XXX until we figure out how to use them */
1123 #ifndef HAS_LONG_DOUBLE
1135 /* Background: in most systems the low byte of the wait status
1136 * is the signal (the lowest 7 bits) and the coredump flag is
1137 * the eight bit, and the second lowest byte is the exit status.
1138 * BeOS bucks the trend and has the bytes in different order.
1139 * See beos/beos.c for how the reality is bent even in BeOS
1140 * to follow the traditional. However, to make the POSIX
1141 * wait W*() macros to work in BeOS, we need to unbend the
1142 * reality back in place. --jhi */
1143 /* In actual fact the code below is to blame here. Perl has an internal
1144 * representation of the exit status ($?), which it re-composes from the
1145 * OS's representation using the W*() POSIX macros. The code below
1146 * incorrectly uses the W*() macros on the internal representation,
1147 * which fails for OSs that have a different representation (namely BeOS
1148 * and Haiku). WMUNGE() is a hack that converts the internal
1149 * representation into the OS specific one, so that the W*() macros work
1150 * as expected. The better solution would be not to use the W*() macros
1151 * in the first place, though. -- Ingo Weinhold
1153 #if defined(__HAIKU__)
1154 # define WMUNGE(x) (((x) & 0xFF00) >> 8 | ((x) & 0x00FF) << 8)
1156 # define WMUNGE(x) (x)
1160 not_here(const char *s)
1162 croak("POSIX::%s not implemented on this architecture", s);
1166 #include "const-c.inc"
1169 restore_sigmask(pTHX_ SV *osset_sv)
1171 /* Fortunately, restoring the signal mask can't fail, because
1172 * there's nothing we can do about it if it does -- we're not
1173 * supposed to return -1 from sigaction unless the disposition
1176 sigset_t *ossetp = (sigset_t *) SvPV_nolen( osset_sv );
1177 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1181 allocate_struct(pTHX_ SV *rv, const STRLEN size, const char *packname) {
1182 SV *const t = newSVrv(rv, packname);
1183 void *const p = sv_grow(t, size + 1);
1193 * (1) The CRT maintains its own copy of the environment, separate from
1194 * the Win32API copy.
1196 * (2) CRT getenv() retrieves from this copy. CRT putenv() updates this
1197 * copy, and then calls SetEnvironmentVariableA() to update the Win32API
1200 * (3) win32_getenv() and win32_putenv() call GetEnvironmentVariableA() and
1201 * SetEnvironmentVariableA() directly, bypassing the CRT copy of the
1204 * (4) The CRT strftime() "%Z" implementation calls __tzset(). That
1205 * calls CRT tzset(), but only the first time it is called, and in turn
1206 * that uses CRT getenv("TZ") to retrieve the timezone info from the CRT
1207 * local copy of the environment and hence gets the original setting as
1208 * perl never updates the CRT copy when assigning to $ENV{TZ}.
1210 * Therefore, we need to retrieve the value of $ENV{TZ} and call CRT
1211 * putenv() to update the CRT copy of the environment (if it is different)
1212 * whenever we're about to call tzset().
1214 * In addition to all that, when perl is built with PERL_IMPLICIT_SYS
1217 * (a) Each interpreter has its own copy of the environment inside the
1218 * perlhost structure. That allows applications that host multiple
1219 * independent Perl interpreters to isolate environment changes from
1220 * each other. (This is similar to how the perlhost mechanism keeps a
1221 * separate working directory for each Perl interpreter, so that calling
1222 * chdir() will not affect other interpreters.)
1224 * (b) Only the first Perl interpreter instantiated within a process will
1225 * "write through" environment changes to the process environment.
1227 * (c) Even the primary Perl interpreter won't update the CRT copy of the
1228 * the environment, only the Win32API copy (it calls win32_putenv()).
1230 * As with CPerlHost::Getenv() and CPerlHost::Putenv() themselves, it makes
1231 * sense to only update the process environment when inside the main
1232 * interpreter, but we don't have access to CPerlHost's m_bTopLevel member
1233 * from here so we'll just have to check PL_curinterp instead.
1235 * Therefore, we can simply #undef getenv() and putenv() so that those names
1236 * always refer to the CRT functions, and explicitly call win32_getenv() to
1237 * access perl's %ENV.
1239 * We also #undef malloc() and free() to be sure we are using the CRT
1240 * functions otherwise under PERL_IMPLICIT_SYS they are redefined to calls
1241 * into VMem::Malloc() and VMem::Free() and all allocations will be freed
1242 * when the Perl interpreter is being destroyed so we'd end up with a pointer
1243 * into deallocated memory in environ[] if a program embedding a Perl
1244 * interpreter continues to operate even after the main Perl interpreter has
1247 * Note that we don't free() the malloc()ed memory unless and until we call
1248 * malloc() again ourselves because the CRT putenv() function simply puts its
1249 * pointer argument into the environ[] array (it doesn't make a copy of it)
1250 * so this memory must otherwise be leaked.
1259 fix_win32_tzenv(void)
1261 static char* oldenv = NULL;
1263 const char* perl_tz_env = win32_getenv("TZ");
1264 const char* crt_tz_env = getenv("TZ");
1265 if (perl_tz_env == NULL)
1267 if (crt_tz_env == NULL)
1269 if (strcmp(perl_tz_env, crt_tz_env) != 0) {
1270 newenv = (char*)malloc((strlen(perl_tz_env) + 4) * sizeof(char));
1271 if (newenv != NULL) {
1272 sprintf(newenv, "TZ=%s", perl_tz_env);
1284 * my_tzset - wrapper to tzset() with a fix to make it work (better) on Win32.
1285 * This code is duplicated in the Time-Piece module, so any changes made here
1286 * should be made there too.
1292 #if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
1293 if (PL_curinterp == aTHX)
1300 typedef int (*isfunc_t)(int);
1301 typedef void (*any_dptr_t)(void *);
1303 /* This needs to be ALIASed in a custom way, hence can't easily be defined as
1305 static XSPROTO(is_common); /* prototype to pass -Wmissing-prototypes */
1306 static XSPROTO(is_common)
1311 croak_xs_usage(cv, "charstring");
1316 /*int RETVAL = 0; YYY means uncomment this to return false on an
1317 * empty string input */
1319 unsigned char *s = (unsigned char *) SvPV(ST(0), len);
1320 unsigned char *e = s + len;
1321 isfunc_t isfunc = (isfunc_t) XSANY.any_dptr;
1323 if (ckWARN_d(WARN_DEPRECATED)) {
1325 /* Warn exactly once for each lexical place this function is
1326 * called. See thread at
1327 * http://markmail.org/thread/jhqcag5njmx7jpyu */
1329 HV *warned = get_hv("POSIX::_warned", GV_ADD | GV_ADDMULTI);
1330 if (! hv_exists(warned, (const char *)&PL_op, sizeof(PL_op))) {
1331 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
1332 "Calling POSIX::%"HEKf"() is deprecated",
1333 HEKfARG(GvNAME_HEK(CvGV(cv))));
1334 hv_store(warned, (const char *)&PL_op, sizeof(PL_op), &PL_sv_yes, 0);
1338 /*if (e > s) { YYY */
1339 for (RETVAL = 1; RETVAL && s < e; s++)
1349 MODULE = POSIX PACKAGE = POSIX
1354 const char *file = __FILE__;
1357 /* silence compiler warning about not_here() defined but not used */
1358 if (0) not_here("");
1360 /* Ensure we get the function, not a macro implementation. Like the C89
1361 standard says we can... */
1363 cv = newXS("POSIX::isalnum", is_common, file);
1364 XSANY.any_dptr = (any_dptr_t) &isalnum;
1366 cv = newXS("POSIX::isalpha", is_common, file);
1367 XSANY.any_dptr = (any_dptr_t) &isalpha;
1369 cv = newXS("POSIX::iscntrl", is_common, file);
1370 XSANY.any_dptr = (any_dptr_t) &iscntrl;
1372 cv = newXS("POSIX::isdigit", is_common, file);
1373 XSANY.any_dptr = (any_dptr_t) &isdigit;
1375 cv = newXS("POSIX::isgraph", is_common, file);
1376 XSANY.any_dptr = (any_dptr_t) &isgraph;
1378 cv = newXS("POSIX::islower", is_common, file);
1379 XSANY.any_dptr = (any_dptr_t) &islower;
1381 cv = newXS("POSIX::isprint", is_common, file);
1382 XSANY.any_dptr = (any_dptr_t) &isprint;
1384 cv = newXS("POSIX::ispunct", is_common, file);
1385 XSANY.any_dptr = (any_dptr_t) &ispunct;
1387 cv = newXS("POSIX::isspace", is_common, file);
1388 XSANY.any_dptr = (any_dptr_t) &isspace;
1390 cv = newXS("POSIX::isupper", is_common, file);
1391 XSANY.any_dptr = (any_dptr_t) &isupper;
1393 cv = newXS("POSIX::isxdigit", is_common, file);
1394 XSANY.any_dptr = (any_dptr_t) &isxdigit;
1397 MODULE = SigSet PACKAGE = POSIX::SigSet PREFIX = sig
1400 new(packname = "POSIX::SigSet", ...)
1401 const char * packname
1406 = (sigset_t *) allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
1410 for (i = 1; i < items; i++)
1411 sigaddset(s, SvIV(ST(i)));
1417 POSIX::SigSet sigset
1422 RETVAL = ix ? sigdelset(sigset, sig) : sigaddset(sigset, sig);
1428 POSIX::SigSet sigset
1432 RETVAL = ix ? sigfillset(sigset) : sigemptyset(sigset);
1437 sigismember(sigset, sig)
1438 POSIX::SigSet sigset
1441 MODULE = Termios PACKAGE = POSIX::Termios PREFIX = cf
1444 new(packname = "POSIX::Termios", ...)
1445 const char * packname
1449 void *const p = allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
1450 sizeof(struct termios), packname);
1451 /* The previous implementation stored a pointer to an uninitialised
1452 struct termios. Seems safer to initialise it, particularly as
1453 this implementation exposes the struct to prying from perl-space.
1455 memset(p, 0, 1 + sizeof(struct termios));
1458 not_here("termios");
1463 getattr(termios_ref, fd = 0)
1464 POSIX::Termios termios_ref
1467 RETVAL = tcgetattr(fd, termios_ref);
1471 # If we define TCSANOW here then both a found and not found constant sub
1472 # are created causing a Constant subroutine TCSANOW redefined warning
1474 # define DEF_SETATTR_ACTION 0
1476 # define DEF_SETATTR_ACTION TCSANOW
1479 setattr(termios_ref, fd = 0, optional_actions = DEF_SETATTR_ACTION)
1480 POSIX::Termios termios_ref
1482 int optional_actions
1484 /* The second argument to the call is mandatory, but we'd like to give
1485 it a useful default. 0 isn't valid on all operating systems - on
1486 Solaris (at least) TCSANOW, TCSADRAIN and TCSAFLUSH have the same
1487 values as the equivalent ioctls, TCSETS, TCSETSW and TCSETSF. */
1488 RETVAL = tcsetattr(fd, optional_actions, termios_ref);
1493 getispeed(termios_ref)
1494 POSIX::Termios termios_ref
1498 RETVAL = ix ? cfgetospeed(termios_ref) : cfgetispeed(termios_ref);
1503 getiflag(termios_ref)
1504 POSIX::Termios termios_ref
1510 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1513 RETVAL = termios_ref->c_iflag;
1516 RETVAL = termios_ref->c_oflag;
1519 RETVAL = termios_ref->c_cflag;
1522 RETVAL = termios_ref->c_lflag;
1525 RETVAL = 0; /* silence compiler warning */
1528 not_here(GvNAME(CvGV(cv)));
1535 getcc(termios_ref, ccix)
1536 POSIX::Termios termios_ref
1539 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1541 croak("Bad getcc subscript");
1542 RETVAL = termios_ref->c_cc[ccix];
1551 setispeed(termios_ref, speed)
1552 POSIX::Termios termios_ref
1558 ? cfsetospeed(termios_ref, speed) : cfsetispeed(termios_ref, speed);
1563 setiflag(termios_ref, flag)
1564 POSIX::Termios termios_ref
1571 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1574 termios_ref->c_iflag = flag;
1577 termios_ref->c_oflag = flag;
1580 termios_ref->c_cflag = flag;
1583 termios_ref->c_lflag = flag;
1587 not_here(GvNAME(CvGV(cv)));
1591 setcc(termios_ref, ccix, cc)
1592 POSIX::Termios termios_ref
1596 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1598 croak("Bad setcc subscript");
1599 termios_ref->c_cc[ccix] = cc;
1605 MODULE = POSIX PACKAGE = POSIX
1607 INCLUDE: const-xs.inc
1613 POSIX::WIFEXITED = 1
1614 POSIX::WIFSIGNALED = 2
1615 POSIX::WIFSTOPPED = 3
1619 #if !defined(WEXITSTATUS) || !defined(WIFEXITED) || !defined(WIFSIGNALED) \
1620 || !defined(WIFSTOPPED) || !defined(WSTOPSIG) || !defined(WTERMSIG)
1621 RETVAL = 0; /* Silence compilers that notice this, but don't realise
1622 that not_here() can't return. */
1627 RETVAL = WEXITSTATUS(WMUNGE(status));
1629 not_here("WEXITSTATUS");
1634 RETVAL = WIFEXITED(WMUNGE(status));
1636 not_here("WIFEXITED");
1641 RETVAL = WIFSIGNALED(WMUNGE(status));
1643 not_here("WIFSIGNALED");
1648 RETVAL = WIFSTOPPED(WMUNGE(status));
1650 not_here("WIFSTOPPED");
1655 RETVAL = WSTOPSIG(WMUNGE(status));
1657 not_here("WSTOPSIG");
1662 RETVAL = WTERMSIG(WMUNGE(status));
1664 not_here("WTERMSIG");
1668 Perl_croak(aTHX_ "Illegal alias %d for POSIX::W*", (int)ix);
1674 open(filename, flags = O_RDONLY, mode = 0666)
1679 if (flags & (O_APPEND|O_CREAT|O_TRUNC|O_RDWR|O_WRONLY|O_EXCL))
1680 TAINT_PROPER("open");
1681 RETVAL = open(filename, flags, mode);
1689 #ifndef HAS_LOCALECONV
1690 localeconv(); /* A stub to call not_here(). */
1692 struct lconv *lcbuf;
1694 /* localeconv() deals with both LC_NUMERIC and LC_MONETARY, but
1695 * LC_MONETARY is already in the correct locale */
1696 STORE_NUMERIC_STANDARD_FORCE_LOCAL();
1699 sv_2mortal((SV*)RETVAL);
1700 if ((lcbuf = localeconv())) {
1701 const struct lconv_offset *strings = lconv_strings;
1702 const struct lconv_offset *integers = lconv_integers;
1703 const char *ptr = (const char *) lcbuf;
1706 /* This string may be controlled by either LC_NUMERIC, or
1709 #if defined(USE_LOCALE_NUMERIC) && defined(USE_LOCALE_MONETARY)
1710 = _is_cur_LC_category_utf8((isLC_NUMERIC_STRING(strings->name))
1713 #elif defined(USE_LOCALE_NUMERIC)
1714 = _is_cur_LC_category_utf8(LC_NUMERIC);
1715 #elif defined(USE_LOCALE_MONETARY)
1716 = _is_cur_LC_category_utf8(LC_MONETARY);
1721 const char *value = *((const char **)(ptr + strings->offset));
1723 if (value && *value) {
1724 (void) hv_store(RETVAL,
1726 strlen(strings->name),
1727 newSVpvn_utf8(value,
1730 /* We mark it as UTF-8 if a utf8 locale
1731 * and is valid, non-ascii UTF-8 */
1733 && ! is_ascii_string((U8 *) value, 0)
1734 && is_utf8_string((U8 *) value, 0)),
1737 } while ((++strings)->name);
1740 const char value = *((const char *)(ptr + integers->offset));
1742 if (value != CHAR_MAX)
1743 (void) hv_store(RETVAL, integers->name,
1744 strlen(integers->name), newSViv(value), 0);
1745 } while ((++integers)->name);
1747 RESTORE_NUMERIC_STANDARD();
1748 #endif /* HAS_LOCALECONV */
1753 setlocale(category, locale = 0)
1759 #ifdef USE_LOCALE_NUMERIC
1760 /* A 0 (or NULL) locale means only query what the current one is. We
1761 * have the LC_NUMERIC name saved, because we are normally switched
1762 * into the C locale for it. Switch back so an LC_ALL query will yield
1763 * the correct results; all other categories don't require special
1766 if (category == LC_NUMERIC) {
1767 XSRETURN_PV(PL_numeric_name);
1770 else if (category == LC_ALL) {
1771 SET_NUMERIC_LOCAL();
1776 #ifdef WIN32 /* Use wrapper on Windows */
1777 retval = Perl_my_setlocale(aTHX_ category, locale);
1779 retval = setlocale(category, locale);
1782 /* Should never happen that a query would return an error, but be
1783 * sure and reset to C locale */
1785 SET_NUMERIC_STANDARD();
1790 /* Save retval since subsequent setlocale() calls may overwrite it. */
1791 retval = savepv(retval);
1793 /* For locale == 0, we may have switched to NUMERIC_LOCAL. Switch back
1796 SET_NUMERIC_STANDARD();
1797 XSRETURN_PV(retval);
1801 #ifdef USE_LOCALE_CTYPE
1802 if (category == LC_CTYPE
1804 || category == LC_ALL
1810 if (category == LC_ALL)
1811 newctype = setlocale(LC_CTYPE, NULL);
1815 new_ctype(newctype);
1817 #endif /* USE_LOCALE_CTYPE */
1818 #ifdef USE_LOCALE_COLLATE
1819 if (category == LC_COLLATE
1821 || category == LC_ALL
1827 if (category == LC_ALL)
1828 newcoll = setlocale(LC_COLLATE, NULL);
1832 new_collate(newcoll);
1834 #endif /* USE_LOCALE_COLLATE */
1835 #ifdef USE_LOCALE_NUMERIC
1836 if (category == LC_NUMERIC
1838 || category == LC_ALL
1844 if (category == LC_ALL)
1845 newnum = setlocale(LC_NUMERIC, NULL);
1849 new_numeric(newnum);
1851 #endif /* USE_LOCALE_NUMERIC */
1896 RETVAL = Perl_acos(x); /* C89 math */
1900 RETVAL = c99_acosh(x);
1906 RETVAL = Perl_asin(x); /* C89 math */
1910 RETVAL = c99_asinh(x);
1916 RETVAL = Perl_atan(x); /* C89 math */
1920 RETVAL = c99_atanh(x);
1927 RETVAL = c99_cbrt(x);
1933 RETVAL = Perl_ceil(x); /* C89 math */
1936 RETVAL = Perl_cosh(x); /* C89 math */
1940 RETVAL = c99_erf(x);
1947 RETVAL = c99_erfc(x);
1954 RETVAL = c99_exp2(x);
1961 RETVAL = c99_expm1(x);
1967 RETVAL = Perl_floor(x); /* C89 math */
1971 RETVAL = bessel_j0(x);
1978 RETVAL = bessel_j1(x);
1984 /* XXX lgamma_r -- the lgamma accesses a global variable (signgam),
1985 * which is evil. Some platforms have lgamma_r, which has
1986 * extra parameter instead of the global variable. */
1988 RETVAL = c99_lgamma(x);
1994 RETVAL = log10(x); /* C89 math */
1998 RETVAL = c99_log1p(x);
2005 RETVAL = c99_log2(x);
2012 RETVAL = c99_logb(x);
2018 #ifdef c99_nearbyint
2019 RETVAL = c99_nearbyint(x);
2021 not_here("nearbyint");
2026 RETVAL = c99_rint(x);
2033 RETVAL = c99_round(x);
2039 RETVAL = Perl_sinh(x); /* C89 math */
2042 RETVAL = Perl_tan(x); /* C89 math */
2045 RETVAL = Perl_tanh(x); /* C89 math */
2048 /* XXX tgamma_r -- the lgamma accesses a global variable (signgam),
2049 * which is evil. Some platforms have tgamma_r, which has
2050 * extra parameter instead of the global variable. */
2052 RETVAL = c99_tgamma(x);
2059 RETVAL = c99_trunc(x);
2066 RETVAL = bessel_y0(x);
2074 RETVAL = bessel_y1(x);
2085 #ifdef HAS_FEGETROUND
2086 RETVAL = my_fegetround();
2089 not_here("fegetround");
2098 #ifdef HAS_FEGETROUND /* canary for fesetround */
2099 RETVAL = fesetround(x);
2100 #elif defined(HAS_FPGETROUND) /* canary for fpsetround */
2103 case FE_TONEAREST: RETVAL = fpsetround(FP_RN); break;
2104 case FE_TOWARDZERO: RETVAL = fpsetround(FP_RZ); break;
2105 case FE_DOWNWARD: RETVAL = fpsetround(FP_RM); break;
2106 case FE_UPWARD: RETVAL = fpsetround(FP_RP); break;
2110 not_here("fesetround");
2131 #ifdef c99_fpclassify
2132 RETVAL = c99_fpclassify(x);
2134 not_here("fpclassify");
2139 RETVAL = c99_ilogb(x);
2145 RETVAL = Perl_isfinite(x);
2148 RETVAL = Perl_isinf(x);
2151 RETVAL = Perl_isnan(x);
2155 RETVAL = c99_isnormal(x);
2157 not_here("isnormal");
2162 RETVAL = c99_lrint(x);
2169 RETVAL = c99_lround(x);
2177 RETVAL = Perl_signbit(x);
2208 RETVAL = c99_copysign(x, y);
2210 not_here("copysign");
2215 RETVAL = c99_fdim(x, y);
2222 RETVAL = c99_fmax(x, y);
2229 RETVAL = c99_fmin(x, y);
2235 RETVAL = Perl_fmod(x, y); /* C89 math */
2239 RETVAL = c99_hypot(x, y);
2245 #ifdef c99_isgreater
2246 RETVAL = c99_isgreater(x, y);
2248 not_here("isgreater");
2252 #ifdef c99_isgreaterequal
2253 RETVAL = c99_isgreaterequal(x, y);
2255 not_here("isgreaterequal");
2260 RETVAL = c99_isless(x, y);
2266 #ifdef c99_islessequal
2267 RETVAL = c99_islessequal(x, y);
2269 not_here("islessequal");
2273 #ifdef c99_islessgreater
2274 RETVAL = c99_islessgreater(x, y);
2276 not_here("islessgreater");
2280 #ifdef c99_isunordered
2281 RETVAL = c99_isunordered(x, y);
2283 not_here("isunordered");
2287 #ifdef c99_nextafter
2288 RETVAL = c99_nextafter(x, y);
2290 not_here("nextafter");
2294 #ifdef c99_nexttoward
2295 RETVAL = c99_nexttoward(x, y);
2297 not_here("nexttoward");
2302 #ifdef c99_remainder
2303 RETVAL = c99_remainder(x, y);
2305 not_here("remainder");
2317 /* (We already know stack is long enough.) */
2318 PUSHs(sv_2mortal(newSVnv(Perl_frexp(x,&expvar)))); /* C89 math */
2319 PUSHs(sv_2mortal(newSViv(expvar)));
2331 /* (We already know stack is long enough.) */
2332 PUSHs(sv_2mortal(newSVnv(Perl_modf(x,&intvar)))); /* C89 math */
2333 PUSHs(sv_2mortal(newSVnv(intvar)));
2342 PUSHs(sv_2mortal(newSVnv(c99_remquo(x,y,&intvar))));
2343 PUSHs(sv_2mortal(newSVnv(intvar)));
2354 RETVAL = c99_scalbn(x, y);
2369 RETVAL = c99_fma(x, y, z);
2382 RETVAL = c99_nan(s ? s : "");
2403 RETVAL = bessel_jn(x, y);
2411 RETVAL = bessel_yn(x, y);
2421 sigaction(sig, optaction, oldaction = 0)
2424 POSIX::SigAction oldaction
2426 #if defined(WIN32) || defined(NETWARE)
2427 RETVAL = not_here("sigaction");
2429 # This code is really grody because we're trying to make the signal
2430 # interface look beautiful, which is hard.
2434 POSIX__SigAction action;
2435 GV *siggv = gv_fetchpvs("SIG", GV_ADD, SVt_PVHV);
2436 struct sigaction act;
2437 struct sigaction oact;
2441 POSIX__SigSet sigset;
2446 croak("Negative signals are not allowed");
2449 if (sig == 0 && SvPOK(ST(0))) {
2450 const char *s = SvPVX_const(ST(0));
2451 int i = whichsig(s);
2453 if (i < 0 && memEQ(s, "SIG", 3))
2454 i = whichsig(s + 3);
2456 if (ckWARN(WARN_SIGNAL))
2457 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
2458 "No such signal: SIG%s", s);
2465 if (sig > NSIG) { /* NSIG - 1 is still okay. */
2466 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
2467 "No such signal: %d", sig);
2471 sigsvp = hv_fetch(GvHVn(siggv),
2473 strlen(PL_sig_name[sig]),
2476 /* Check optaction and set action */
2477 if(SvTRUE(optaction)) {
2478 if(sv_isa(optaction, "POSIX::SigAction"))
2479 action = (HV*)SvRV(optaction);
2481 croak("action is not of type POSIX::SigAction");
2487 /* sigaction() is supposed to look atomic. In particular, any
2488 * signal handler invoked during a sigaction() call should
2489 * see either the old or the new disposition, and not something
2490 * in between. We use sigprocmask() to make it so.
2493 RETVAL=sigprocmask(SIG_BLOCK, &sset, &osset);
2497 /* Restore signal mask no matter how we exit this block. */
2498 osset_sv = newSVpvn((char *)(&osset), sizeof(sigset_t));
2499 SAVEFREESV( osset_sv );
2500 SAVEDESTRUCTOR_X(restore_sigmask, osset_sv);
2502 RETVAL=-1; /* In case both oldaction and action are 0. */
2504 /* Remember old disposition if desired. */
2506 svp = hv_fetchs(oldaction, "HANDLER", TRUE);
2508 croak("Can't supply an oldaction without a HANDLER");
2509 if(SvTRUE(*sigsvp)) { /* TBD: what if "0"? */
2510 sv_setsv(*svp, *sigsvp);
2513 sv_setpvs(*svp, "DEFAULT");
2515 RETVAL = sigaction(sig, (struct sigaction *)0, & oact);
2520 /* Get back the mask. */
2521 svp = hv_fetchs(oldaction, "MASK", TRUE);
2522 if (sv_isa(*svp, "POSIX::SigSet")) {
2523 sigset = (sigset_t *) SvPV_nolen(SvRV(*svp));
2526 sigset = (sigset_t *) allocate_struct(aTHX_ *svp,
2530 *sigset = oact.sa_mask;
2532 /* Get back the flags. */
2533 svp = hv_fetchs(oldaction, "FLAGS", TRUE);
2534 sv_setiv(*svp, oact.sa_flags);
2536 /* Get back whether the old handler used safe signals. */
2537 svp = hv_fetchs(oldaction, "SAFE", TRUE);
2539 /* compare incompatible pointers by casting to integer */
2540 PTR2nat(oact.sa_handler) == PTR2nat(PL_csighandlerp));
2544 /* Safe signals use "csighandler", which vectors through the
2545 PL_sighandlerp pointer when it's safe to do so.
2546 (BTW, "csighandler" is very different from "sighandler".) */
2547 svp = hv_fetchs(action, "SAFE", FALSE);
2551 (*svp && SvTRUE(*svp))
2552 ? PL_csighandlerp : PL_sighandlerp
2555 /* Vector new Perl handler through %SIG.
2556 (The core signal handlers read %SIG to dispatch.) */
2557 svp = hv_fetchs(action, "HANDLER", FALSE);
2559 croak("Can't supply an action without a HANDLER");
2560 sv_setsv(*sigsvp, *svp);
2562 /* This call actually calls sigaction() with almost the
2563 right settings, including appropriate interpretation
2564 of DEFAULT and IGNORE. However, why are we doing
2565 this when we're about to do it again just below? XXX */
2566 SvSETMAGIC(*sigsvp);
2568 /* And here again we duplicate -- DEFAULT/IGNORE checking. */
2570 const char *s=SvPVX_const(*svp);
2571 if(strEQ(s,"IGNORE")) {
2572 act.sa_handler = SIG_IGN;
2574 else if(strEQ(s,"DEFAULT")) {
2575 act.sa_handler = SIG_DFL;
2579 /* Set up any desired mask. */
2580 svp = hv_fetchs(action, "MASK", FALSE);
2581 if (svp && sv_isa(*svp, "POSIX::SigSet")) {
2582 sigset = (sigset_t *) SvPV_nolen(SvRV(*svp));
2583 act.sa_mask = *sigset;
2586 sigemptyset(& act.sa_mask);
2588 /* Set up any desired flags. */
2589 svp = hv_fetchs(action, "FLAGS", FALSE);
2590 act.sa_flags = svp ? SvIV(*svp) : 0;
2592 /* Don't worry about cleaning up *sigsvp if this fails,
2593 * because that means we tried to disposition a
2594 * nonblockable signal, in which case *sigsvp is
2595 * essentially meaningless anyway.
2597 RETVAL = sigaction(sig, & act, (struct sigaction *)0);
2612 POSIX::SigSet sigset
2616 RETVAL = ix ? sigsuspend(sigset) : sigpending(sigset);
2623 sigprocmask(how, sigset, oldsigset = 0)
2625 POSIX::SigSet sigset = NO_INIT
2626 POSIX::SigSet oldsigset = NO_INIT
2628 if (! SvOK(ST(1))) {
2630 } else if (sv_isa(ST(1), "POSIX::SigSet")) {
2631 sigset = (sigset_t *) SvPV_nolen(SvRV(ST(1)));
2633 croak("sigset is not of type POSIX::SigSet");
2636 if (items < 3 || ! SvOK(ST(2))) {
2638 } else if (sv_isa(ST(2), "POSIX::SigSet")) {
2639 oldsigset = (sigset_t *) SvPV_nolen(SvRV(ST(2)));
2641 croak("oldsigset is not of type POSIX::SigSet");
2654 /* RT #98912 - More Microsoft muppetry - failing to actually implemented
2655 the well known documented POSIX behaviour for a POSIX API.
2656 http://msdn.microsoft.com/en-us/library/8syseb29.aspx */
2657 RETVAL = dup2(fd1, fd2) == -1 ? -1 : fd2;
2659 RETVAL = dup2(fd1, fd2);
2665 lseek(fd, offset, whence)
2670 Off_t pos = PerlLIO_lseek(fd, offset, whence);
2671 RETVAL = sizeof(Off_t) > sizeof(IV)
2672 ? newSVnv((NV)pos) : newSViv((IV)pos);
2681 if ((incr = nice(incr)) != -1 || errno == 0) {
2683 XPUSHs(newSVpvs_flags("0 but true", SVs_TEMP));
2685 XPUSHs(sv_2mortal(newSViv(incr)));
2692 if (pipe(fds) != -1) {
2694 PUSHs(sv_2mortal(newSViv(fds[0])));
2695 PUSHs(sv_2mortal(newSViv(fds[1])));
2699 read(fd, buffer, nbytes)
2701 SV *sv_buffer = SvROK(ST(1)) ? SvRV(ST(1)) : ST(1);
2705 char * buffer = sv_grow( sv_buffer, nbytes+1 );
2708 SvCUR_set(sv_buffer, RETVAL);
2709 SvPOK_only(sv_buffer);
2710 *SvEND(sv_buffer) = '\0';
2711 SvTAINTED_on(sv_buffer);
2727 tcsetpgrp(fd, pgrp_id)
2736 if (uname(&buf) >= 0) {
2738 PUSHs(newSVpvn_flags(buf.sysname, strlen(buf.sysname), SVs_TEMP));
2739 PUSHs(newSVpvn_flags(buf.nodename, strlen(buf.nodename), SVs_TEMP));
2740 PUSHs(newSVpvn_flags(buf.release, strlen(buf.release), SVs_TEMP));
2741 PUSHs(newSVpvn_flags(buf.version, strlen(buf.version), SVs_TEMP));
2742 PUSHs(newSVpvn_flags(buf.machine, strlen(buf.machine), SVs_TEMP));
2745 uname((char *) 0); /* A stub to call not_here(). */
2749 write(fd, buffer, nbytes)
2760 RETVAL = newSVpvs("");
2761 SvGROW(RETVAL, L_tmpnam);
2762 /* Yes, we know tmpnam() is bad. So bad that some compilers
2763 * and linkers warn against using it. But it is here for
2764 * completeness. POSIX.pod warns against using it.
2766 * Then again, maybe this should be removed at some point.
2767 * No point in enabling dangerous interfaces. */
2768 len = strlen(tmpnam(SvPV(RETVAL, i)));
2769 SvCUR_set(RETVAL, len);
2782 mbstowcs(s, pwcs, n)
2794 wcstombs(s, pwcs, n)
2816 STORE_NUMERIC_STANDARD_FORCE_LOCAL();
2817 num = strtod(str, &unparsed);
2818 PUSHs(sv_2mortal(newSVnv(num)));
2819 if (GIMME == G_ARRAY) {
2822 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
2824 PUSHs(&PL_sv_undef);
2826 RESTORE_NUMERIC_STANDARD();
2837 STORE_NUMERIC_STANDARD_FORCE_LOCAL();
2838 num = strtold(str, &unparsed);
2839 PUSHs(sv_2mortal(newSVnv(num)));
2840 if (GIMME == G_ARRAY) {
2843 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
2845 PUSHs(&PL_sv_undef);
2847 RESTORE_NUMERIC_STANDARD();
2852 strtol(str, base = 0)
2859 num = strtol(str, &unparsed, base);
2860 #if IVSIZE <= LONGSIZE
2861 if (num < IV_MIN || num > IV_MAX)
2862 PUSHs(sv_2mortal(newSVnv((double)num)));
2865 PUSHs(sv_2mortal(newSViv((IV)num)));
2866 if (GIMME == G_ARRAY) {
2869 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
2871 PUSHs(&PL_sv_undef);
2875 strtoul(str, base = 0)
2882 num = strtoul(str, &unparsed, base);
2883 #if IVSIZE <= LONGSIZE
2885 PUSHs(sv_2mortal(newSVnv((double)num)));
2888 PUSHs(sv_2mortal(newSViv((IV)num)));
2889 if (GIMME == G_ARRAY) {
2892 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
2894 PUSHs(&PL_sv_undef);
2905 char *p = SvPV(src,srclen);
2907 buflen = srclen * 4 + 1;
2908 ST(0) = sv_2mortal(newSV(buflen));
2909 dstlen = strxfrm(SvPVX(ST(0)), p, (size_t)buflen);
2910 if (dstlen >= buflen) {
2912 SvGROW(ST(0), dstlen);
2913 strxfrm(SvPVX(ST(0)), p, (size_t)dstlen);
2916 SvCUR_set(ST(0), dstlen);
2921 mkfifo(filename, mode)
2928 RETVAL = access(filename, mode);
2930 TAINT_PROPER("mkfifo");
2931 RETVAL = mkfifo(filename, mode);
2943 RETVAL = ix == 1 ? close(fd)
2944 : (ix < 1 ? tcdrain(fd) : dup(fd));
2957 RETVAL = ix == 1 ? tcflush(fd, action)
2958 : (ix < 1 ? tcflow(fd, action) : tcsendbreak(fd, action));
2963 asctime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = -1)
2979 init_tm(&mytm); /* XXX workaround - see init_tm() in core util.c */
2982 mytm.tm_hour = hour;
2983 mytm.tm_mday = mday;
2985 mytm.tm_year = year;
2986 mytm.tm_wday = wday;
2987 mytm.tm_yday = yday;
2988 mytm.tm_isdst = isdst;
2990 const time_t result = mktime(&mytm);
2991 if (result == (time_t)-1)
2993 else if (result == 0)
2994 sv_setpvn(TARG, "0 but true", 10);
2996 sv_setiv(TARG, (IV)result);
2998 sv_setpv(TARG, asctime(&mytm));
3016 realtime = times( &tms );
3018 PUSHs( sv_2mortal( newSViv( (IV) realtime ) ) );
3019 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_utime ) ) );
3020 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_stime ) ) );
3021 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cutime ) ) );
3022 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cstime ) ) );
3025 difftime(time1, time2)
3029 #XXX: if $xsubpp::WantOptimize is always the default
3030 # sv_setpv(TARG, ...) could be used rather than
3031 # ST(0) = sv_2mortal(newSVpv(...))
3033 strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
3049 /* allowing user-supplied (rather than literal) formats
3050 * is normally frowned upon as a potential security risk;
3051 * but this is part of the API so we have to allow it */
3052 GCC_DIAG_IGNORE(-Wformat-nonliteral);
3053 buf = my_strftime(SvPV_nolen(fmt), sec, min, hour, mday, mon, year, wday, yday, isdst);
3055 sv = sv_newmortal();
3057 STRLEN len = strlen(buf);
3058 sv_usepvn_flags(sv, buf, len, SV_HAS_TRAILING_NUL);
3060 || (! is_ascii_string((U8*) buf, len)
3061 && is_utf8_string((U8*) buf, len)
3062 #ifdef USE_LOCALE_TIME
3063 && _is_cur_LC_category_utf8(LC_TIME)
3069 else { /* We can't distinguish between errors and just an empty
3070 * return; in all cases just return an empty string */
3071 SvUPGRADE(sv, SVt_PV);
3072 SvPV_set(sv, (char *) "");
3075 SvLEN_set(sv, 0); /* Won't attempt to free the string when sv
3090 PUSHs(newSVpvn_flags(tzname[0], strlen(tzname[0]), SVs_TEMP));
3091 PUSHs(newSVpvn_flags(tzname[1], strlen(tzname[1]), SVs_TEMP));
3097 #ifdef HAS_CTERMID_R
3098 s = (char *) safemalloc((size_t) L_ctermid);
3100 RETVAL = ctermid(s);
3104 #ifdef HAS_CTERMID_R
3113 RETVAL = cuserid(s);
3116 not_here("cuserid");
3127 pathconf(filename, name)
3138 unsigned int seconds
3140 RETVAL = PerlProc_sleep(seconds);
3166 XSprePUSH; PUSHTARG;
3170 lchown(uid, gid, path)
3176 /* yes, the order of arguments is different,
3177 * but consistent with CORE::chown() */
3178 RETVAL = lchown(path, uid, gid);
3180 RETVAL = not_here("lchown");