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
21 static int not_here(const char *s);
23 #if defined(PERL_IMPLICIT_SYS)
27 # define open PerlLIO_open3
30 #ifdef I_DIRENT /* XXX maybe better to just rely on perl.h? */
35 #include <sys/errno2.h>
41 #if !(defined(__vax__) && defined(__NetBSD__))
65 #if defined(USE_QUADMATH) && defined(I_QUADMATH)
82 # define M_LOG2E M_LOG2Eq
83 # define M_LOG10E M_LOG10Eq
85 # define M_LN10 M_LN10q
87 # define M_PI_2 M_PI_2q
88 # define M_PI_4 M_PI_4q
89 # define M_1_PI M_1_PIq
90 # define M_2_PI M_2_PIq
91 # define M_2_SQRTPI M_2_SQRTPIq
92 # define M_SQRT2 M_SQRT2q
93 # define M_SQRT1_2 M_SQRT1_2q
97 # ifdef USE_LONG_DOUBLE
111 # define FLOAT_C(c) CAT2(c,L)
113 # define FLOAT_C(c) (c)
117 # define M_E FLOAT_C(2.71828182845904523536028747135266250)
120 # define M_LOG2E FLOAT_C(1.44269504088896340735992468100189214)
123 # define M_LOG10E FLOAT_C(0.434294481903251827651128918916605082)
126 # define M_LN2 FLOAT_C(0.693147180559945309417232121458176568)
129 # define M_LN10 FLOAT_C(2.30258509299404568401799145468436421)
132 # define M_PI FLOAT_C(3.14159265358979323846264338327950288)
135 # define M_PI_2 FLOAT_C(1.57079632679489661923132169163975144)
138 # define M_PI_4 FLOAT_C(0.785398163397448309615660845819875721)
141 # define M_1_PI FLOAT_C(0.318309886183790671537767526745028724)
144 # define M_2_PI FLOAT_C(0.636619772367581343075535053490057448)
147 # define M_2_SQRTPI FLOAT_C(1.12837916709551257389615890312154517)
150 # define M_SQRT2 FLOAT_C(1.41421356237309504880168872420969808)
153 # define M_SQRT1_2 FLOAT_C(0.707106781186547524400844362104849039)
158 #if !defined(INFINITY) && defined(NV_INF)
159 # define INFINITY NV_INF
162 #if !defined(NAN) && defined(NV_NAN)
166 #if !defined(Inf) && defined(NV_INF)
170 #if !defined(NaN) && defined(NV_NAN)
174 /* We will have an emulation. */
176 # define FP_INFINITE 0
179 # define FP_SUBNORMAL 3
183 /* We will have an emulation. */
185 # define FE_TOWARDZERO 0
186 # define FE_TONEAREST 1
188 # define FE_DOWNWARD 3
193 acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp
194 log log10 modf pow sin sinh sqrt tan tanh
196 * Implemented in core:
198 atan2 cos exp log pow sin sqrt
202 acosh asinh atanh cbrt copysign erf erfc exp2 expm1 fdim fma fmax
203 fmin fpclassify hypot ilogb isfinite isgreater isgreaterequal isinf
204 isless islessequal islessgreater isnan isnormal isunordered lgamma
205 log1p log2 logb lrint lround nan nearbyint nextafter nexttoward remainder
206 remquo rint round scalbn signbit tgamma trunc
209 http://pubs.opengroup.org/onlinepubs/009695399/basedefs/math.h.html
211 * Berkeley/SVID extensions:
215 * Configure already (5.21.5) scans for:
217 copysign*l* fpclassify isfinite isinf isnan isnan*l* ilogb*l* signbit scalbn*l*
219 * For floating-point round mode (which matters for e.g. lrint and rint)
221 fegetround fesetround
225 /* XXX Constant FP_FAST_FMA (if true, FMA is faster) */
227 /* XXX Add ldiv(), lldiv()? It's C99, but from stdlib.h, not math.h */
229 /* XXX Beware old gamma() -- one cannot know whether that is the
230 * gamma or the log of gamma, that's why the new tgamma and lgamma.
231 * Though also remember lgamma_r. */
233 /* Certain AIX releases have the C99 math, but not in long double.
234 * The <math.h> has them, e.g. __expl128, but no library has them!
236 * Also see the comments in hints/aix.sh about long doubles. */
238 #if defined(USE_QUADMATH) && defined(I_QUADMATH)
239 # define c99_acosh acoshq
240 # define c99_asinh asinhq
241 # define c99_atanh atanhq
242 # define c99_cbrt cbrtq
243 # define c99_copysign copysignq
244 # define c99_erf erfq
245 # define c99_erfc erfcq
247 # define c99_expm1 expm1q
248 # define c99_fdim fdimq
249 # define c99_fma fmaq
250 # define c99_fmax fmaxq
251 # define c99_fmin fminq
252 # define c99_hypot hypotq
253 # define c99_ilogb ilogbq
254 # define c99_lgamma lgammaq
255 # define c99_log1p log1pq
256 # define c99_log2 log2q
258 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
259 # define c99_lrint llrintq
260 # define c99_lround llroundq
262 # define c99_lrint lrintq
263 # define c99_lround lroundq
265 # define c99_nan nanq
266 # define c99_nearbyint nearbyintq
267 # define c99_nextafter nextafterq
269 # define c99_remainder remainderq
270 # define c99_remquo remquoq
271 # define c99_rint rintq
272 # define c99_round roundq
273 # define c99_scalbn scalbnq
274 # define c99_signbit signbitq
275 # define c99_tgamma tgammaq
276 # define c99_trunc truncq
277 # define bessel_j0 j0q
278 # define bessel_j1 j1q
279 # define bessel_jn jnq
280 # define bessel_y0 y0q
281 # define bessel_y1 y1q
282 # define bessel_yn ynq
283 #elif defined(USE_LONG_DOUBLE) && \
284 (defined(HAS_FREXPL) || defined(HAS_ILOGBL)) && defined(HAS_SQRTL)
285 /* Use some of the Configure scans for long double math functions
286 * as the canary for all the C99 *l variants being defined. */
287 # define c99_acosh acoshl
288 # define c99_asinh asinhl
289 # define c99_atanh atanhl
290 # define c99_cbrt cbrtl
291 # define c99_copysign copysignl
292 # define c99_erf erfl
293 # define c99_erfc erfcl
294 # define c99_exp2 exp2l
295 # define c99_expm1 expm1l
296 # define c99_fdim fdiml
297 # define c99_fma fmal
298 # define c99_fmax fmaxl
299 # define c99_fmin fminl
300 # define c99_hypot hypotl
301 # define c99_ilogb ilogbl
302 # define c99_lgamma lgammal
303 # define c99_log1p log1pl
304 # define c99_log2 log2l
305 # define c99_logb logbl
306 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLRINTL)
307 # define c99_lrint llrintl
308 # elif defined(HAS_LRINTL)
309 # define c99_lrint lrintl
311 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLROUNDL)
312 # define c99_lround llroundl
313 # elif defined(HAS_LROUNDL)
314 # define c99_lround lroundl
316 # define c99_nan nanl
317 # define c99_nearbyint nearbyintl
318 # define c99_nextafter nextafterl
319 # define c99_nexttoward nexttowardl
320 # define c99_remainder remainderl
321 # define c99_remquo remquol
322 # define c99_rint rintl
323 # define c99_round roundl
324 # define c99_scalbn scalbnl
325 # ifdef HAS_SIGNBIT /* possibly bad assumption */
326 # define c99_signbit signbitl
328 # define c99_tgamma tgammal
329 # define c99_trunc truncl
331 # define c99_acosh acosh
332 # define c99_asinh asinh
333 # define c99_atanh atanh
334 # define c99_cbrt cbrt
335 # define c99_copysign copysign
337 # define c99_erfc erfc
338 # define c99_exp2 exp2
339 # define c99_expm1 expm1
340 # define c99_fdim fdim
342 # define c99_fmax fmax
343 # define c99_fmin fmin
344 # define c99_hypot hypot
345 # define c99_ilogb ilogb
346 # define c99_lgamma lgamma
347 # define c99_log1p log1p
348 # define c99_log2 log2
349 # define c99_logb logb
350 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLRINT)
351 # define c99_lrint llrint
353 # define c99_lrint lrint
355 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLROUND)
356 # define c99_lround llround
358 # define c99_lround lround
361 # define c99_nearbyint nearbyint
362 # define c99_nextafter nextafter
363 # define c99_nexttoward nexttoward
364 # define c99_remainder remainder
365 # define c99_remquo remquo
366 # define c99_rint rint
367 # define c99_round round
368 # define c99_scalbn scalbn
369 /* We already define Perl_signbit in perl.h. */
371 # define c99_signbit signbit
373 # define c99_tgamma tgamma
374 # define c99_trunc trunc
377 /* AIX xlc (__IBMC__) really doesn't have the following long double
378 * math interfaces (no __acoshl128 aka acoshl, etc.), see
379 * hints/aix.sh. These are in the -lc128 but fail to be found
380 * during dynamic linking/loading.
382 * XXX1 Better Configure scans
383 * XXX2 Is this xlc version dependent? */
384 #if defined(USE_LONG_DOUBLE) && defined(__IBMC__)
404 # undef c99_nearbyint
405 # undef c99_nextafter
406 # undef c99_nexttoward
407 # undef c99_remainder
418 # define isunordered(x, y) (Perl_isnan(x) || Perl_isnan(y))
419 # elif defined(HAS_UNORDERED)
420 # define isunordered(x, y) unordered(x, y)
424 /* XXX these isgreater/isnormal/isunordered macros definitions should
425 * be moved further in the file to be part of the emulations, so that
426 * platforms can e.g. #undef c99_isunordered and have it work like
427 * it does for the other interfaces. */
429 #if !defined(isgreater) && defined(isunordered)
430 # define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
431 # define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
432 # define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
433 # define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
434 # define islessgreater(x, y) (!isunordered((x), (y)) && \
435 ((x) > (y) || (y) > (x)))
438 /* Check both the Configure symbol and the macro-ness (like C99 promises). */
439 #if defined(HAS_FPCLASSIFY) && defined(fpclassify)
440 # define c99_fpclassify fpclassify
442 /* Like isnormal(), the isfinite(), isinf(), and isnan() are also C99
443 and also (sizeof-arg-aware) macros, but they are already well taken
444 care of by Configure et al, and defined in perl.h as
445 Perl_isfinite(), Perl_isinf(), and Perl_isnan(). */
447 # define c99_isnormal isnormal
449 #ifdef isgreater /* canary for all the C99 is*<cmp>* macros. */
450 # define c99_isgreater isgreater
451 # define c99_isgreaterequal isgreaterequal
452 # define c99_isless isless
453 # define c99_islessequal islessequal
454 # define c99_islessgreater islessgreater
455 # define c99_isunordered isunordered
458 /* The Great Wall of Undef where according to the definedness of HAS_FOO symbols
459 * the corresponding c99_foo wrappers are undefined. This list doesn't include
460 * the isfoo() interfaces because they are either type-aware macros, or dealt
461 * separately, already in perl.h */
502 #ifndef HAS_FPCLASSIFY
503 # undef c99_fpclassify
532 #ifndef HAS_NEARBYINT
533 # undef c99_nearbyint
535 #ifndef HAS_NEXTAFTER
536 # undef c99_nextafter
538 #ifndef HAS_NEXTTOWARD
539 # undef c99_nexttoward
541 #ifndef HAS_REMAINDER
542 # undef c99_remainder
568 /* Some APIs exist under Win32 with "underbar" names. */
571 # undef c99_nextafter
572 # define c99_hypot _hypot
573 # define c99_logb _logb
574 # define c99_nextafter _nextafter
576 # define bessel_j0 _j0
577 # define bessel_j1 _j1
578 # define bessel_jn _jn
579 # define bessel_y0 _y0
580 # define bessel_y1 _y1
581 # define bessel_yn _yn
585 /* The Bessel functions: BSD, SVID, XPG4, and POSIX. But not C99. */
586 #if defined(HAS_J0) && !defined(bessel_j0)
587 # if defined(USE_LONG_DOUBLE) && defined(HAS_J0L)
588 # define bessel_j0 j0l
589 # define bessel_j1 j1l
590 # define bessel_jn jnl
591 # define bessel_y0 y0l
592 # define bessel_y1 y1l
593 # define bessel_yn ynl
595 # define bessel_j0 j0
596 # define bessel_j1 j1
597 # define bessel_jn jn
598 # define bessel_y0 y0
599 # define bessel_y1 y1
600 # define bessel_yn yn
604 /* Emulations for missing math APIs.
606 * Keep in mind that the point of many of these functions is that
607 * they, if available, are supposed to give more precise/more
608 * numerically stable results.
610 * See e.g. http://www.johndcook.com/math_h.html
614 static NV my_acosh(NV x)
616 return Perl_log(x + Perl_sqrt(x * x - 1));
618 # define c99_acosh my_acosh
622 static NV my_asinh(NV x)
624 return Perl_log(x + Perl_sqrt(x * x + 1));
626 # define c99_asinh my_asinh
630 static NV my_atanh(NV x)
632 return (Perl_log(1 + x) - Perl_log(1 - x)) / 2;
634 # define c99_atanh my_atanh
638 static NV my_cbrt(NV x)
640 static const NV one_third = (NV)1.0/3;
641 return x >= 0.0 ? Perl_pow(x, one_third) : -Perl_pow(-x, one_third);
643 # define c99_cbrt my_cbrt
647 static NV my_copysign(NV x, NV y)
649 return y >= 0 ? (x < 0 ? -x : x) : (x < 0 ? x : -x);
651 # define c99_copysign my_copysign
654 /* XXX cosh (though c89) */
657 static NV my_erf(NV x)
659 /* http://www.johndcook.com/cpp_erf.html -- public domain */
661 NV a2 = -0.284496736;
663 NV a4 = -1.453152027;
667 int sign = x < 0 ? -1 : 1; /* Save the sign. */
670 /* Abramowitz and Stegun formula 7.1.26 */
671 t = 1.0 / (1.0 + p * x);
672 y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1) * t * Perl_exp(-x*x);
676 # define c99_erf my_erf
680 static NV my_erfc(NV x) {
681 /* This is not necessarily numerically stable, but better than nothing. */
682 return 1.0 - c99_erf(x);
684 # define c99_erfc my_erfc
688 static NV my_exp2(NV x)
690 return Perl_pow((NV)2.0, x);
692 # define c99_exp2 my_exp2
696 static NV my_expm1(NV x)
698 if (PERL_ABS(x) < 1e-5)
699 /* http://www.johndcook.com/cpp_expm1.html -- public domain.
700 * Taylor series, the first four terms (the last term quartic). */
701 /* Probably not enough for long doubles. */
702 return x * (1.0 + x * (1/2.0 + x * (1/6.0 + x/24.0)));
704 return Perl_exp(x) - 1;
706 # define c99_expm1 my_expm1
710 static NV my_fdim(NV x, NV y)
713 return (Perl_isnan(x) || Perl_isnan(y)) ? NV_NAN : (x > y ? x - y : 0);
715 return (x > y ? x - y : 0);
718 # define c99_fdim my_fdim
722 static NV my_fma(NV x, NV y, NV z)
726 # define c99_fma my_fma
730 static NV my_fmax(NV x, NV y)
734 return Perl_isnan(y) ? NV_NAN : y;
735 } else if (Perl_isnan(y)) {
739 return x > y ? x : y;
741 # define c99_fmax my_fmax
745 static NV my_fmin(NV x, NV y)
749 return Perl_isnan(y) ? NV_NAN : y;
750 } else if (Perl_isnan(y)) {
754 return x < y ? x : y;
756 # define c99_fmin my_fmin
759 #ifndef c99_fpclassify
761 static IV my_fpclassify(NV x)
763 #ifdef Perl_fp_class_inf
764 if (Perl_fp_class_inf(x)) return FP_INFINITE;
765 if (Perl_fp_class_nan(x)) return FP_NAN;
766 if (Perl_fp_class_norm(x)) return FP_NORMAL;
767 if (Perl_fp_class_denorm(x)) return FP_SUBNORMAL;
768 if (Perl_fp_class_zero(x)) return FP_ZERO;
769 # define c99_fpclassify my_fpclassify
777 static NV my_hypot(NV x, NV y)
779 /* http://en.wikipedia.org/wiki/Hypot */
781 x = PERL_ABS(x); /* Take absolute values. */
789 if (x < y) { /* Swap so that y is less. */
795 return x * Perl_sqrt(1.0 + t * t);
797 # define c99_hypot my_hypot
801 static IV my_ilogb(NV x)
803 return (IV)(Perl_log(x) * M_LOG2E);
805 # define c99_ilogb my_ilogb
808 /* tgamma and lgamma emulations based on
809 * http://www.johndcook.com/cpp_gamma.html,
810 * code placed in public domain.
812 * Note that these implementations (neither the johndcook originals
813 * nor these) do NOT set the global signgam variable. This is not
814 * necessarily a bad thing. */
816 /* Note that the tgamma() and lgamma() implementations
817 * here depend on each other. */
819 #if !defined(HAS_TGAMMA) || !defined(c99_tgamma)
820 static NV my_tgamma(NV x);
821 # define c99_tgamma my_tgamma
822 # define USE_MY_TGAMMA
824 #if !defined(HAS_LGAMMA) || !defined(c99_lgamma)
825 static NV my_lgamma(NV x);
826 # define c99_lgamma my_lgamma
827 # define USE_MY_LGAMMA
831 static NV my_tgamma(NV x)
833 const NV gamma = 0.577215664901532860606512090; /* Euler's gamma constant. */
835 if (Perl_isnan(x) || x < 0.0)
839 if (x == 0.0 || x == NV_INF)
840 #ifdef DOUBLE_IS_IEEE_FORMAT
841 return x == -0.0 ? -NV_INF : NV_INF;
847 /* The function domain is split into three intervals:
848 * (0, 0.001), [0.001, 12), and (12, infinity) */
850 /* First interval: (0, 0.001)
851 * For small values, 1/tgamma(x) has power series x + gamma x^2,
852 * so in this range, 1/tgamma(x) = x + gamma x^2 with error on the order of x^3.
853 * The relative error over this interval is less than 6e-7. */
855 return 1.0 / (x * (1.0 + gamma * x));
857 /* Second interval: [0.001, 12) */
859 double y = x; /* Working copy. */
861 /* Numerator coefficients for approximation over the interval (1,2) */
862 static const NV p[] = {
863 -1.71618513886549492533811E+0,
864 2.47656508055759199108314E+1,
865 -3.79804256470945635097577E+2,
866 6.29331155312818442661052E+2,
867 8.66966202790413211295064E+2,
868 -3.14512729688483675254357E+4,
869 -3.61444134186911729807069E+4,
870 6.64561438202405440627855E+4
872 /* Denominator coefficients for approximation over the interval (1, 2) */
873 static const NV q[] = {
874 -3.08402300119738975254353E+1,
875 3.15350626979604161529144E+2,
876 -1.01515636749021914166146E+3,
877 -3.10777167157231109440444E+3,
878 2.25381184209801510330112E+4,
879 4.75584627752788110767815E+3,
880 -1.34659959864969306392456E+5,
881 -1.15132259675553483497211E+5
892 n = (int)Perl_floor(y) - 1;
896 for (i = 0; i < 8; i++) {
897 num = (num + p[i]) * z;
898 den = den * z + q[i];
900 result = num / den + 1.0;
903 /* Use the identity tgamma(z) = tgamma(z+1)/z
904 * The variable "result" now holds tgamma of the original y + 1
905 * Thus we use y - 1 to get back the original y. */
909 /* Use the identity tgamma(z+n) = z*(z+1)* ... *(z+n-1)*tgamma(z) */
910 for (i = 0; i < n; i++)
918 /* Third interval: [12, +Inf) */
919 #if LDBL_MANT_DIG == 113 /* IEEE quad prec */
930 return Perl_exp(c99_lgamma(x));
935 static NV my_lgamma(NV x)
942 if (x <= 0 || x == NV_INF)
945 if (x == 1.0 || x == 2.0)
948 return Perl_log(PERL_ABS(c99_tgamma(x)));
949 /* Abramowitz and Stegun 6.1.41
950 * Asymptotic series should be good to at least 11 or 12 figures
951 * For error analysis, see Whittiker and Watson
952 * A Course in Modern Analysis (1927), page 252 */
954 static const NV c[8] = {
964 NV z = 1.0 / (x * x);
966 static const NV half_log_of_two_pi =
967 0.91893853320467274178032973640562;
970 for (i = 6; i >= 0; i--) {
975 return (x - 0.5) * Perl_log(x) - x + half_log_of_two_pi + series;
981 static NV my_log1p(NV x)
983 /* http://www.johndcook.com/cpp_log_one_plus_x.html -- public domain.
984 * Taylor series, the first four terms (the last term quartic). */
993 if (PERL_ABS(x) > 1e-4)
994 return Perl_log(1.0 + x);
996 /* Probably not enough for long doubles. */
997 return x * (1.0 + x * (-1/2.0 + x * (1/3.0 - x/4.0)));
999 # define c99_log1p my_log1p
1003 static NV my_log2(NV x)
1005 return Perl_log(x) * M_LOG2E;
1007 # define c99_log2 my_log2
1012 /* XXX nexttoward */
1014 static int my_fegetround()
1016 #ifdef HAS_FEGETROUND
1017 return fegetround();
1018 #elif defined(HAS_FPGETROUND)
1019 switch (fpgetround()) {
1020 case FP_RN: return FE_TONEAREST;
1021 case FP_RZ: return FE_TOWARDZERO;
1022 case FP_RM: return FE_DOWNWARD;
1023 case FP_RP: return FE_UPWARD;
1026 #elif defined(FLT_ROUNDS)
1027 switch (FLT_ROUNDS) {
1028 case 0: return FE_TOWARDZERO;
1029 case 1: return FE_TONEAREST;
1030 case 2: return FE_UPWARD;
1031 case 3: return FE_DOWNWARD;
1034 #elif defined(__osf__) /* Tru64 */
1035 switch (read_rnd()) {
1036 case FP_RND_RN: return FE_TONEAREST;
1037 case FP_RND_RZ: return FE_TOWARDZERO;
1038 case FP_RND_RM: return FE_DOWNWARD;
1039 case FP_RND_RP: return FE_UPWARD;
1047 /* Toward closest integer. */
1048 #define MY_ROUND_NEAREST(x) ((NV)((IV)((x) >= 0.0 ? (x) + 0.5 : (x) - 0.5)))
1051 #define MY_ROUND_TRUNC(x) ((NV)((IV)(x)))
1053 /* Toward minus infinity. */
1054 #define MY_ROUND_DOWN(x) ((NV)((IV)((x) >= 0.0 ? (x) : (x) - 0.5)))
1056 /* Toward plus infinity. */
1057 #define MY_ROUND_UP(x) ((NV)((IV)((x) >= 0.0 ? (x) + 0.5 : (x))))
1059 #if (!defined(c99_nearbyint) || !defined(c99_lrint)) && defined(FE_TONEAREST)
1060 static NV my_rint(NV x)
1063 switch (my_fegetround()) {
1064 case FE_TONEAREST: return MY_ROUND_NEAREST(x);
1065 case FE_TOWARDZERO: return MY_ROUND_TRUNC(x);
1066 case FE_DOWNWARD: return MY_ROUND_DOWN(x);
1067 case FE_UPWARD: return MY_ROUND_UP(x);
1070 #elif defined(HAS_FPGETROUND)
1071 switch (fpgetround()) {
1072 case FP_RN: return MY_ROUND_NEAREST(x);
1073 case FP_RZ: return MY_ROUND_TRUNC(x);
1074 case FP_RM: return MY_ROUND_DOWN(x);
1075 case FE_RP: return MY_ROUND_UP(x);
1083 /* XXX nearbyint() and rint() are not really identical -- but the difference
1084 * is messy: nearbyint is defined NOT to raise FE_INEXACT floating point
1085 * exceptions, while rint() is defined to MAYBE raise them. At the moment
1086 * Perl is blissfully unaware of such fine detail of floating point. */
1087 #ifndef c99_nearbyint
1088 # ifdef FE_TONEAREST
1089 # define c99_nearbyrint my_rint
1094 # ifdef FE_TONEAREST
1095 static IV my_lrint(NV x)
1097 return (IV)my_rint(x);
1099 # define c99_lrint my_lrint
1104 static IV my_lround(NV x)
1106 return (IV)MY_ROUND_NEAREST(x);
1108 # define c99_lround my_lround
1116 # ifdef FE_TONEAREST
1117 # define c99_rint my_rint
1122 static NV my_round(NV x)
1124 return MY_ROUND_NEAREST(x);
1126 # define c99_round my_round
1130 # if defined(Perl_ldexp) && FLT_RADIX == 2
1131 static NV my_scalbn(NV x, int y)
1133 return Perl_ldexp(x, y);
1135 # define c99_scalbn my_scalbn
1139 /* XXX sinh (though c89) */
1141 /* tgamma -- see lgamma */
1143 /* XXX tanh (though c89) */
1146 static NV my_trunc(NV x)
1148 return MY_ROUND_TRUNC(x);
1150 # define c99_trunc my_trunc
1155 #undef NV_PAYLOAD_DEBUG
1157 /* NOTE: the NaN payload API implementation is hand-rolled, since the
1158 * APIs are only proposed ones as of June 2015, so very few, if any,
1159 * platforms have implementations yet, so HAS_SETPAYLOAD and such are
1160 * unlikely to be helpful.
1162 * XXX - if the core numification wants to actually generate
1163 * the nan payload in "nan(123)", and maybe "nans(456)", for
1164 * signaling payload", this needs to be moved to e.g. numeric.c
1165 * (look for grok_infnan)
1167 * Conversely, if the core stringification wants the nan payload
1168 * and/or the nan quiet/signaling distinction, S_getpayload()
1169 * from this file needs to be moved, to e.g. sv.c (look for S_infnan_2pv),
1170 * and the (trivial) functionality of issignaling() copied
1171 * (for generating "NaNS", or maybe even "NaNQ") -- or maybe there
1172 * are too many formatting parameters for simple stringification?
1175 /* While it might make sense for the payload to be UV or IV,
1176 * to avoid conversion loss, the proposed ISO interfaces use
1177 * a floating point input, which is then truncated to integer,
1178 * and only the integer part being used. This is workable,
1179 * except for: (1) the conversion loss (2) suboptimal for
1180 * 32-bit integer platforms. A workaround API for (2) and
1181 * in general for bit-honesty would be an array of integers
1182 * as the payload... but the proposed C API does nothing of
1184 #if NVSIZE == UVSIZE
1185 # define NV_PAYLOAD_TYPE UV
1187 # define NV_PAYLOAD_TYPE NV
1190 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
1191 # define NV_PAYLOAD_SIZEOF_ASSERT(a) \
1192 STATIC_ASSERT_STMT(sizeof(a) == NVSIZE / 2)
1194 # define NV_PAYLOAD_SIZEOF_ASSERT(a) \
1195 STATIC_ASSERT_STMT(sizeof(a) == NVSIZE)
1198 static void S_setpayload(NV* nvp, NV_PAYLOAD_TYPE payload, bool signaling)
1201 static const U8 m[] = { NV_NAN_PAYLOAD_MASK };
1202 static const U8 p[] = { NV_NAN_PAYLOAD_PERM };
1203 UV a[(NVSIZE + UVSIZE - 1) / UVSIZE] = { 0 };
1205 NV_PAYLOAD_SIZEOF_ASSERT(m);
1206 NV_PAYLOAD_SIZEOF_ASSERT(p);
1208 /* Divide the input into the array in "base unsigned integer" in
1209 * little-endian order. Note that the integer might be smaller than
1210 * an NV (if UV is U32, for example). */
1211 #if NVSIZE == UVSIZE
1212 a[0] = payload; /* The trivial case. */
1215 NV t1 = c99_trunc(payload); /* towards zero (drop fractional) */
1216 #ifdef NV_PAYLOAD_DEBUG
1217 Perl_warn(aTHX_ "t1 = %" NVgf " (payload %" NVgf ")\n", t1, payload);
1220 a[0] = (UV)t1; /* Fast path, also avoids rounding errors (right?) */
1222 /* UVSIZE < NVSIZE or payload > UV_MAX.
1224 * This may happen for example if:
1225 * (1) UVSIZE == 32 and common 64-bit double NV
1226 * (32-bit system not using -Duse64bitint)
1227 * (2) UVSIZE == 64 and the x86-style 80-bit long double NV
1228 * (note that here the room for payload is actually the 64 bits)
1229 * (3) UVSIZE == 64 and the 128-bit IEEE 764 quadruple NV
1230 * (112 bits in mantissa, 111 bits room for payload)
1232 * NOTE: this is very sensitive to correctly functioning
1233 * fmod()/fmodl(), and correct casting of big-unsigned-integer to NV.
1234 * If these don't work right, especially the low order bits
1235 * are in danger. For example Solaris and AIX seem to have issues
1236 * here, especially if using 32-bit UVs. */
1238 for (i = 0, t2 = t1; i < (int)C_ARRAY_LENGTH(a); i++) {
1239 a[i] = (UV)Perl_fmod(t2, (NV)UV_MAX);
1240 t2 = Perl_floor(t2 / (NV)UV_MAX);
1245 #ifdef NV_PAYLOAD_DEBUG
1246 for (i = 0; i < (int)C_ARRAY_LENGTH(a); i++) {
1247 Perl_warn(aTHX_ "a[%d] = 0x%" UVxf "\n", i, a[i]);
1250 for (i = 0; i < (int)sizeof(p); i++) {
1251 if (m[i] && p[i] < sizeof(p)) {
1252 U8 s = (p[i] % UVSIZE) << 3;
1253 UV u = a[p[i] / UVSIZE] & ((UV)0xFF << s);
1254 U8 b = (U8)((u >> s) & m[i]);
1255 ((U8 *)(nvp))[i] &= ~m[i]; /* For NaNs with non-zero payload bits. */
1256 ((U8 *)(nvp))[i] |= b;
1257 #ifdef NV_PAYLOAD_DEBUG
1259 "set p[%2d] = %02x (i = %d, m = %02x, s = %2d, b = %02x, u = %08"
1260 UVxf ")\n", i, ((U8 *)(nvp))[i], i, m[i], s, b, u);
1262 a[p[i] / UVSIZE] &= ~u;
1266 NV_NAN_SET_SIGNALING(nvp);
1268 #ifdef USE_LONG_DOUBLE
1269 # if LONG_DOUBLEKIND == 3 || LONG_DOUBLEKIND == 4
1270 # if LONG_DOUBLESIZE > 10
1271 memset((char *)nvp + 10, '\0', LONG_DOUBLESIZE - 10); /* x86 long double */
1275 for (i = 0; i < (int)C_ARRAY_LENGTH(a); i++) {
1277 Perl_warn(aTHX_ "payload lost bits (%" UVxf ")", a[i]);
1281 #ifdef NV_PAYLOAD_DEBUG
1282 for (i = 0; i < NVSIZE; i++) {
1283 PerlIO_printf(Perl_debug_log, "%02x ", ((U8 *)(nvp))[i]);
1285 PerlIO_printf(Perl_debug_log, "\n");
1289 static NV_PAYLOAD_TYPE S_getpayload(NV nv)
1292 static const U8 m[] = { NV_NAN_PAYLOAD_MASK };
1293 static const U8 p[] = { NV_NAN_PAYLOAD_PERM };
1294 UV a[(NVSIZE + UVSIZE - 1) / UVSIZE] = { 0 };
1297 NV_PAYLOAD_SIZEOF_ASSERT(m);
1298 NV_PAYLOAD_SIZEOF_ASSERT(p);
1300 for (i = 0; i < (int)sizeof(p); i++) {
1301 if (m[i] && p[i] < NVSIZE) {
1302 U8 s = (p[i] % UVSIZE) << 3;
1303 a[p[i] / UVSIZE] |= (UV)(((U8 *)(&nv))[i] & m[i]) << s;
1306 for (i = (int)C_ARRAY_LENGTH(a) - 1; i >= 0; i--) {
1307 #ifdef NV_PAYLOAD_DEBUG
1308 Perl_warn(aTHX_ "a[%d] = %" UVxf "\n", i, a[i]);
1313 #ifdef NV_PAYLOAD_DEBUG
1314 for (i = 0; i < NVSIZE; i++) {
1315 PerlIO_printf(Perl_debug_log, "%02x ", ((U8 *)(&nv))[i]);
1317 PerlIO_printf(Perl_debug_log, "\n");
1322 #endif /* #ifdef NV_NAN */
1324 /* XXX This comment is just to make I_TERMIO and I_SGTTY visible to
1325 metaconfig for future extension writers. We don't use them in POSIX.
1326 (This is really sneaky :-) --AD
1328 #if defined(I_TERMIOS)
1329 #include <termios.h>
1337 #include <sys/stat.h>
1338 #include <sys/types.h>
1346 # if !defined(WIN32) && !defined(__CYGWIN__) && !defined(NETWARE) && !defined(__UWIN__)
1347 extern char *tzname[];
1350 #if !defined(WIN32) && !defined(__UWIN__) || (defined(__MINGW32__) && !defined(tzname))
1351 char *tzname[] = { "" , "" };
1355 #if defined(__VMS) && !defined(__POSIX_SOURCE)
1357 # include <utsname.h>
1360 # define mkfifo(a,b) (not_here("mkfifo"),-1)
1362 /* The POSIX notion of ttyname() is better served by getname() under VMS */
1363 static char ttnambuf[64];
1364 # define ttyname(fd) (isatty(fd) > 0 ? getname(fd,ttnambuf,0) : NULL)
1367 #if defined (__CYGWIN__)
1368 # define tzname _tzname
1370 #if defined (WIN32) || defined (NETWARE)
1372 # define mkfifo(a,b) not_here("mkfifo")
1373 # define ttyname(a) (char*)not_here("ttyname")
1374 # define sigset_t long
1377 # define mode_t short
1380 # define mode_t short
1382 # define tzset() not_here("tzset")
1384 # ifndef _POSIX_OPEN_MAX
1385 # define _POSIX_OPEN_MAX FOPEN_MAX /* XXX bogus ? */
1388 # define sigaction(a,b,c) not_here("sigaction")
1389 # define sigpending(a) not_here("sigpending")
1390 # define sigprocmask(a,b,c) not_here("sigprocmask")
1391 # define sigsuspend(a) not_here("sigsuspend")
1392 # define sigemptyset(a) not_here("sigemptyset")
1393 # define sigaddset(a,b) not_here("sigaddset")
1394 # define sigdelset(a,b) not_here("sigdelset")
1395 # define sigfillset(a) not_here("sigfillset")
1396 # define sigismember(a,b) not_here("sigismember")
1400 # define setuid(a) not_here("setuid")
1401 # define setgid(a) not_here("setgid")
1402 #endif /* NETWARE */
1403 #ifndef USE_LONG_DOUBLE
1404 # define strtold(s1,s2) not_here("strtold")
1405 #endif /* USE_LONG_DOUBLE */
1409 # if defined(OS2) || defined(__amigaos4__)
1410 # define mkfifo(a,b) not_here("mkfifo")
1411 # else /* !( defined OS2 ) */
1413 # define mkfifo(path, mode) (mknod((path), (mode) | S_IFIFO, 0))
1416 # endif /* !HAS_MKFIFO */
1421 # include <sys/times.h>
1423 # include <sys/utsname.h>
1425 # ifndef __amigaos4__
1426 # include <sys/wait.h>
1431 #endif /* WIN32 || NETWARE */
1435 typedef long SysRetLong;
1436 typedef sigset_t* POSIX__SigSet;
1437 typedef HV* POSIX__SigAction;
1438 typedef int POSIX__SigNo;
1439 typedef int POSIX__Fd;
1441 typedef struct termios* POSIX__Termios;
1442 #else /* Define termios types to int, and call not_here for the functions.*/
1443 #define POSIX__Termios int
1445 #define tcflag_t int
1447 #define cfgetispeed(x) not_here("cfgetispeed")
1448 #define cfgetospeed(x) not_here("cfgetospeed")
1449 #define tcdrain(x) not_here("tcdrain")
1450 #define tcflush(x,y) not_here("tcflush")
1451 #define tcsendbreak(x,y) not_here("tcsendbreak")
1452 #define cfsetispeed(x,y) not_here("cfsetispeed")
1453 #define cfsetospeed(x,y) not_here("cfsetospeed")
1454 #define ctermid(x) (char *) not_here("ctermid")
1455 #define tcflow(x,y) not_here("tcflow")
1456 #define tcgetattr(x,y) not_here("tcgetattr")
1457 #define tcsetattr(x,y,z) not_here("tcsetattr")
1460 /* Possibly needed prototypes */
1463 double strtod (const char *, char **);
1464 long strtol (const char *, char **, int);
1465 unsigned long strtoul (const char *, char **, int);
1467 long double strtold (const char *, char **);
1472 #ifndef HAS_DIFFTIME
1474 #define difftime(a,b) not_here("difftime")
1477 #ifndef HAS_FPATHCONF
1478 #define fpathconf(f,n) (SysRetLong) not_here("fpathconf")
1481 #define mktime(a) not_here("mktime")
1484 #define nice(a) not_here("nice")
1486 #ifndef HAS_PATHCONF
1487 #define pathconf(f,n) (SysRetLong) not_here("pathconf")
1490 #define sysconf(n) (SysRetLong) not_here("sysconf")
1492 #ifndef HAS_READLINK
1493 #define readlink(a,b,c) not_here("readlink")
1496 #define setpgid(a,b) not_here("setpgid")
1499 #define setsid() not_here("setsid")
1502 #define strcoll(s1,s2) not_here("strcoll")
1505 #define strtod(s1,s2) not_here("strtod")
1508 #define strtold(s1,s2) not_here("strtold")
1511 #define strtol(s1,s2,b) not_here("strtol")
1514 #define strtoul(s1,s2,b) not_here("strtoul")
1517 #define strxfrm(s1,s2,n) not_here("strxfrm")
1519 #ifndef HAS_TCGETPGRP
1520 #define tcgetpgrp(a) not_here("tcgetpgrp")
1522 #ifndef HAS_TCSETPGRP
1523 #define tcsetpgrp(a,b) not_here("tcsetpgrp")
1527 #define times(a) not_here("times")
1528 #endif /* NETWARE */
1531 #define uname(a) not_here("uname")
1534 #define waitpid(a,b,c) not_here("waitpid")
1539 #define mblen(a,b) not_here("mblen")
1542 #ifndef HAS_MBSTOWCS
1543 #define mbstowcs(s, pwcs, n) not_here("mbstowcs")
1546 #define mbtowc(pwc, s, n) not_here("mbtowc")
1548 #ifndef HAS_WCSTOMBS
1549 #define wcstombs(s, pwcs, n) not_here("wcstombs")
1552 #define wctomb(s, wchar) not_here("wcstombs")
1554 #if !defined(HAS_MBLEN) && !defined(HAS_MBSTOWCS) && !defined(HAS_MBTOWC) && !defined(HAS_WCSTOMBS) && !defined(HAS_WCTOMB)
1555 /* If we don't have these functions, then we wouldn't have gotten a typedef
1556 for wchar_t, the wide character type. Defining wchar_t allows the
1557 functions referencing it to compile. Its actual type is then meaningless,
1558 since without the above functions, all sections using it end up calling
1559 not_here() and croak. --Kaveh Ghazi (ghazi@noc.rutgers.edu) 9/18/94. */
1561 #define wchar_t char
1565 #ifndef HAS_LOCALECONV
1566 # define localeconv() not_here("localeconv")
1568 struct lconv_offset {
1573 static const struct lconv_offset lconv_strings[] = {
1574 #ifdef USE_LOCALE_NUMERIC
1575 {"decimal_point", STRUCT_OFFSET(struct lconv, decimal_point)},
1576 {"thousands_sep", STRUCT_OFFSET(struct lconv, thousands_sep)},
1577 # ifndef NO_LOCALECONV_GROUPING
1578 {"grouping", STRUCT_OFFSET(struct lconv, grouping)},
1581 #ifdef USE_LOCALE_MONETARY
1582 {"int_curr_symbol", STRUCT_OFFSET(struct lconv, int_curr_symbol)},
1583 {"currency_symbol", STRUCT_OFFSET(struct lconv, currency_symbol)},
1584 {"mon_decimal_point", STRUCT_OFFSET(struct lconv, mon_decimal_point)},
1585 # ifndef NO_LOCALECONV_MON_THOUSANDS_SEP
1586 {"mon_thousands_sep", STRUCT_OFFSET(struct lconv, mon_thousands_sep)},
1588 # ifndef NO_LOCALECONV_MON_GROUPING
1589 {"mon_grouping", STRUCT_OFFSET(struct lconv, mon_grouping)},
1591 {"positive_sign", STRUCT_OFFSET(struct lconv, positive_sign)},
1592 {"negative_sign", STRUCT_OFFSET(struct lconv, negative_sign)},
1597 #ifdef USE_LOCALE_NUMERIC
1599 /* The Linux man pages say these are the field names for the structure
1600 * components that are LC_NUMERIC; the rest being LC_MONETARY */
1601 # define isLC_NUMERIC_STRING(name) (strEQ(name, "decimal_point") \
1602 || strEQ(name, "thousands_sep") \
1604 /* There should be no harm done \
1605 * checking for this, even if \
1606 * NO_LOCALECONV_GROUPING */ \
1607 || strEQ(name, "grouping"))
1609 # define isLC_NUMERIC_STRING(name) (0)
1612 static const struct lconv_offset lconv_integers[] = {
1613 #ifdef USE_LOCALE_MONETARY
1614 {"int_frac_digits", STRUCT_OFFSET(struct lconv, int_frac_digits)},
1615 {"frac_digits", STRUCT_OFFSET(struct lconv, frac_digits)},
1616 {"p_cs_precedes", STRUCT_OFFSET(struct lconv, p_cs_precedes)},
1617 {"p_sep_by_space", STRUCT_OFFSET(struct lconv, p_sep_by_space)},
1618 {"n_cs_precedes", STRUCT_OFFSET(struct lconv, n_cs_precedes)},
1619 {"n_sep_by_space", STRUCT_OFFSET(struct lconv, n_sep_by_space)},
1620 {"p_sign_posn", STRUCT_OFFSET(struct lconv, p_sign_posn)},
1621 {"n_sign_posn", STRUCT_OFFSET(struct lconv, n_sign_posn)},
1622 #ifdef HAS_LC_MONETARY_2008
1623 {"int_p_cs_precedes", STRUCT_OFFSET(struct lconv, int_p_cs_precedes)},
1624 {"int_p_sep_by_space", STRUCT_OFFSET(struct lconv, int_p_sep_by_space)},
1625 {"int_n_cs_precedes", STRUCT_OFFSET(struct lconv, int_n_cs_precedes)},
1626 {"int_n_sep_by_space", STRUCT_OFFSET(struct lconv, int_n_sep_by_space)},
1627 {"int_p_sign_posn", STRUCT_OFFSET(struct lconv, int_p_sign_posn)},
1628 {"int_n_sign_posn", STRUCT_OFFSET(struct lconv, int_n_sign_posn)},
1634 #endif /* HAS_LOCALECONV */
1636 #ifdef HAS_LONG_DOUBLE
1637 # if LONG_DOUBLESIZE > NVSIZE
1638 # undef HAS_LONG_DOUBLE /* XXX until we figure out how to use them */
1642 #ifndef HAS_LONG_DOUBLE
1654 /* Background: in most systems the low byte of the wait status
1655 * is the signal (the lowest 7 bits) and the coredump flag is
1656 * the eight bit, and the second lowest byte is the exit status.
1657 * BeOS bucks the trend and has the bytes in different order.
1658 * See beos/beos.c for how the reality is bent even in BeOS
1659 * to follow the traditional. However, to make the POSIX
1660 * wait W*() macros to work in BeOS, we need to unbend the
1661 * reality back in place. --jhi */
1662 /* In actual fact the code below is to blame here. Perl has an internal
1663 * representation of the exit status ($?), which it re-composes from the
1664 * OS's representation using the W*() POSIX macros. The code below
1665 * incorrectly uses the W*() macros on the internal representation,
1666 * which fails for OSs that have a different representation (namely BeOS
1667 * and Haiku). WMUNGE() is a hack that converts the internal
1668 * representation into the OS specific one, so that the W*() macros work
1669 * as expected. The better solution would be not to use the W*() macros
1670 * in the first place, though. -- Ingo Weinhold
1672 #if defined(__HAIKU__)
1673 # define WMUNGE(x) (((x) & 0xFF00) >> 8 | ((x) & 0x00FF) << 8)
1675 # define WMUNGE(x) (x)
1679 not_here(const char *s)
1681 croak("POSIX::%s not implemented on this architecture", s);
1685 #include "const-c.inc"
1688 restore_sigmask(pTHX_ SV *osset_sv)
1690 /* Fortunately, restoring the signal mask can't fail, because
1691 * there's nothing we can do about it if it does -- we're not
1692 * supposed to return -1 from sigaction unless the disposition
1695 #if !(defined(__amigaos4__) && defined(__NEWLIB__))
1696 sigset_t *ossetp = (sigset_t *) SvPV_nolen( osset_sv );
1697 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1702 allocate_struct(pTHX_ SV *rv, const STRLEN size, const char *packname) {
1703 SV *const t = newSVrv(rv, packname);
1704 void *const p = sv_grow(t, size + 1);
1706 /* Ensure at least one use of not_here() to avoid "defined but not
1707 * used" warning. This is not at all related to allocate_struct(); I
1708 * just needed somewhere to dump it - DAPM */
1709 if (0) { not_here(""); }
1719 * (1) The CRT maintains its own copy of the environment, separate from
1720 * the Win32API copy.
1722 * (2) CRT getenv() retrieves from this copy. CRT putenv() updates this
1723 * copy, and then calls SetEnvironmentVariableA() to update the Win32API
1726 * (3) win32_getenv() and win32_putenv() call GetEnvironmentVariableA() and
1727 * SetEnvironmentVariableA() directly, bypassing the CRT copy of the
1730 * (4) The CRT strftime() "%Z" implementation calls __tzset(). That
1731 * calls CRT tzset(), but only the first time it is called, and in turn
1732 * that uses CRT getenv("TZ") to retrieve the timezone info from the CRT
1733 * local copy of the environment and hence gets the original setting as
1734 * perl never updates the CRT copy when assigning to $ENV{TZ}.
1736 * Therefore, we need to retrieve the value of $ENV{TZ} and call CRT
1737 * putenv() to update the CRT copy of the environment (if it is different)
1738 * whenever we're about to call tzset().
1740 * In addition to all that, when perl is built with PERL_IMPLICIT_SYS
1743 * (a) Each interpreter has its own copy of the environment inside the
1744 * perlhost structure. That allows applications that host multiple
1745 * independent Perl interpreters to isolate environment changes from
1746 * each other. (This is similar to how the perlhost mechanism keeps a
1747 * separate working directory for each Perl interpreter, so that calling
1748 * chdir() will not affect other interpreters.)
1750 * (b) Only the first Perl interpreter instantiated within a process will
1751 * "write through" environment changes to the process environment.
1753 * (c) Even the primary Perl interpreter won't update the CRT copy of the
1754 * the environment, only the Win32API copy (it calls win32_putenv()).
1756 * As with CPerlHost::Getenv() and CPerlHost::Putenv() themselves, it makes
1757 * sense to only update the process environment when inside the main
1758 * interpreter, but we don't have access to CPerlHost's m_bTopLevel member
1759 * from here so we'll just have to check PL_curinterp instead.
1761 * Therefore, we can simply #undef getenv() and putenv() so that those names
1762 * always refer to the CRT functions, and explicitly call win32_getenv() to
1763 * access perl's %ENV.
1765 * We also #undef malloc() and free() to be sure we are using the CRT
1766 * functions otherwise under PERL_IMPLICIT_SYS they are redefined to calls
1767 * into VMem::Malloc() and VMem::Free() and all allocations will be freed
1768 * when the Perl interpreter is being destroyed so we'd end up with a pointer
1769 * into deallocated memory in environ[] if a program embedding a Perl
1770 * interpreter continues to operate even after the main Perl interpreter has
1773 * Note that we don't free() the malloc()ed memory unless and until we call
1774 * malloc() again ourselves because the CRT putenv() function simply puts its
1775 * pointer argument into the environ[] array (it doesn't make a copy of it)
1776 * so this memory must otherwise be leaked.
1785 fix_win32_tzenv(void)
1787 static char* oldenv = NULL;
1789 const char* perl_tz_env = win32_getenv("TZ");
1790 const char* crt_tz_env = getenv("TZ");
1791 if (perl_tz_env == NULL)
1793 if (crt_tz_env == NULL)
1795 if (strcmp(perl_tz_env, crt_tz_env) != 0) {
1796 newenv = (char*)malloc((strlen(perl_tz_env) + 4) * sizeof(char));
1797 if (newenv != NULL) {
1798 sprintf(newenv, "TZ=%s", perl_tz_env);
1810 * my_tzset - wrapper to tzset() with a fix to make it work (better) on Win32.
1811 * This code is duplicated in the Time-Piece module, so any changes made here
1812 * should be made there too.
1818 #if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
1819 if (PL_curinterp == aTHX)
1826 MODULE = SigSet PACKAGE = POSIX::SigSet PREFIX = sig
1829 new(packname = "POSIX::SigSet", ...)
1830 const char * packname
1835 = (sigset_t *) allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
1839 for (i = 1; i < items; i++)
1840 sigaddset(s, SvIV(ST(i)));
1846 POSIX::SigSet sigset
1851 RETVAL = ix ? sigdelset(sigset, sig) : sigaddset(sigset, sig);
1857 POSIX::SigSet sigset
1861 RETVAL = ix ? sigfillset(sigset) : sigemptyset(sigset);
1866 sigismember(sigset, sig)
1867 POSIX::SigSet sigset
1870 MODULE = Termios PACKAGE = POSIX::Termios PREFIX = cf
1873 new(packname = "POSIX::Termios", ...)
1874 const char * packname
1878 void *const p = allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
1879 sizeof(struct termios), packname);
1880 /* The previous implementation stored a pointer to an uninitialised
1881 struct termios. Seems safer to initialise it, particularly as
1882 this implementation exposes the struct to prying from perl-space.
1884 memset(p, 0, 1 + sizeof(struct termios));
1887 not_here("termios");
1892 getattr(termios_ref, fd = 0)
1893 POSIX::Termios termios_ref
1896 RETVAL = tcgetattr(fd, termios_ref);
1900 # If we define TCSANOW here then both a found and not found constant sub
1901 # are created causing a Constant subroutine TCSANOW redefined warning
1903 # define DEF_SETATTR_ACTION 0
1905 # define DEF_SETATTR_ACTION TCSANOW
1908 setattr(termios_ref, fd = 0, optional_actions = DEF_SETATTR_ACTION)
1909 POSIX::Termios termios_ref
1911 int optional_actions
1913 /* The second argument to the call is mandatory, but we'd like to give
1914 it a useful default. 0 isn't valid on all operating systems - on
1915 Solaris (at least) TCSANOW, TCSADRAIN and TCSAFLUSH have the same
1916 values as the equivalent ioctls, TCSETS, TCSETSW and TCSETSF. */
1917 if (optional_actions < 0) {
1918 SETERRNO(EINVAL, LIB_INVARG);
1921 RETVAL = tcsetattr(fd, optional_actions, termios_ref);
1927 getispeed(termios_ref)
1928 POSIX::Termios termios_ref
1932 RETVAL = ix ? cfgetospeed(termios_ref) : cfgetispeed(termios_ref);
1937 getiflag(termios_ref)
1938 POSIX::Termios termios_ref
1944 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1947 RETVAL = termios_ref->c_iflag;
1950 RETVAL = termios_ref->c_oflag;
1953 RETVAL = termios_ref->c_cflag;
1956 RETVAL = termios_ref->c_lflag;
1959 RETVAL = 0; /* silence compiler warning */
1962 not_here(GvNAME(CvGV(cv)));
1969 getcc(termios_ref, ccix)
1970 POSIX::Termios termios_ref
1973 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1975 croak("Bad getcc subscript");
1976 RETVAL = termios_ref->c_cc[ccix];
1985 setispeed(termios_ref, speed)
1986 POSIX::Termios termios_ref
1992 ? cfsetospeed(termios_ref, speed) : cfsetispeed(termios_ref, speed);
1997 setiflag(termios_ref, flag)
1998 POSIX::Termios termios_ref
2005 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2008 termios_ref->c_iflag = flag;
2011 termios_ref->c_oflag = flag;
2014 termios_ref->c_cflag = flag;
2017 termios_ref->c_lflag = flag;
2021 not_here(GvNAME(CvGV(cv)));
2025 setcc(termios_ref, ccix, cc)
2026 POSIX::Termios termios_ref
2030 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2032 croak("Bad setcc subscript");
2033 termios_ref->c_cc[ccix] = cc;
2039 MODULE = POSIX PACKAGE = POSIX
2041 INCLUDE: const-xs.inc
2047 POSIX::WIFEXITED = 1
2048 POSIX::WIFSIGNALED = 2
2049 POSIX::WIFSTOPPED = 3
2053 #if !defined(WEXITSTATUS) || !defined(WIFEXITED) || !defined(WIFSIGNALED) \
2054 || !defined(WIFSTOPPED) || !defined(WSTOPSIG) || !defined(WTERMSIG)
2055 RETVAL = 0; /* Silence compilers that notice this, but don't realise
2056 that not_here() can't return. */
2061 RETVAL = WEXITSTATUS(WMUNGE(status));
2063 not_here("WEXITSTATUS");
2068 RETVAL = WIFEXITED(WMUNGE(status));
2070 not_here("WIFEXITED");
2075 RETVAL = WIFSIGNALED(WMUNGE(status));
2077 not_here("WIFSIGNALED");
2082 RETVAL = WIFSTOPPED(WMUNGE(status));
2084 not_here("WIFSTOPPED");
2089 RETVAL = WSTOPSIG(WMUNGE(status));
2091 not_here("WSTOPSIG");
2096 RETVAL = WTERMSIG(WMUNGE(status));
2098 not_here("WTERMSIG");
2102 croak("Illegal alias %d for POSIX::W*", (int)ix);
2108 open(filename, flags = O_RDONLY, mode = 0666)
2113 if (flags & (O_APPEND|O_CREAT|O_TRUNC|O_RDWR|O_WRONLY|O_EXCL))
2114 TAINT_PROPER("open");
2115 RETVAL = open(filename, flags, mode);
2123 #ifndef HAS_LOCALECONV
2124 localeconv(); /* A stub to call not_here(). */
2126 struct lconv *lcbuf;
2128 /* localeconv() deals with both LC_NUMERIC and LC_MONETARY, but
2129 * LC_MONETARY is already in the correct locale */
2130 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2131 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
2134 sv_2mortal((SV*)RETVAL);
2135 if ((lcbuf = localeconv())) {
2136 const struct lconv_offset *strings = lconv_strings;
2137 const struct lconv_offset *integers = lconv_integers;
2138 const char *ptr = (const char *) lcbuf;
2140 while (strings->name) {
2141 /* This string may be controlled by either LC_NUMERIC, or
2144 #if defined(USE_LOCALE_NUMERIC) && defined(USE_LOCALE_MONETARY)
2145 = _is_cur_LC_category_utf8((isLC_NUMERIC_STRING(strings->name))
2148 #elif defined(USE_LOCALE_NUMERIC)
2149 = _is_cur_LC_category_utf8(LC_NUMERIC);
2150 #elif defined(USE_LOCALE_MONETARY)
2151 = _is_cur_LC_category_utf8(LC_MONETARY);
2156 const char *value = *((const char **)(ptr + strings->offset));
2158 if (value && *value) {
2159 (void) hv_store(RETVAL,
2161 strlen(strings->name),
2166 /* We mark it as UTF-8 if a utf8 locale and is
2167 * valid and variant under UTF-8 */
2169 && ! is_utf8_invariant_string((U8 *) value, 0)
2170 && is_utf8_string((U8 *) value, 0)),
2176 while (integers->name) {
2177 const char value = *((const char *)(ptr + integers->offset));
2179 if (value != CHAR_MAX)
2180 (void) hv_store(RETVAL, integers->name,
2181 strlen(integers->name), newSViv(value), 0);
2185 RESTORE_LC_NUMERIC_STANDARD();
2186 #endif /* HAS_LOCALECONV */
2191 setlocale(category, locale = 0)
2197 #ifdef USE_LOCALE_NUMERIC
2198 /* A 0 (or NULL) locale means only query what the current one is. We
2199 * have the LC_NUMERIC name saved, because we are normally switched
2200 * into the C locale for it. Switch back so an LC_ALL query will yield
2201 * the correct results; all other categories don't require special
2204 if (category == LC_NUMERIC) {
2205 XSRETURN_PV(PL_numeric_name);
2208 else if (category == LC_ALL) {
2209 SET_NUMERIC_UNDERLYING();
2214 #ifdef WIN32 /* Use wrapper on Windows */
2215 retval = Perl_my_setlocale(aTHX_ category, locale);
2217 retval = setlocale(category, locale);
2219 DEBUG_L(PerlIO_printf(Perl_debug_log,
2220 "%s:%d: %s\n", __FILE__, __LINE__,
2221 _setlocale_debug_string(category, locale, retval)));
2223 /* Should never happen that a query would return an error, but be
2224 * sure and reset to C locale */
2226 SET_NUMERIC_STANDARD();
2231 /* Save retval since subsequent setlocale() calls may overwrite it. */
2232 retval = savepv(retval);
2235 /* For locale == 0, we may have switched to NUMERIC_UNDERLYING. Switch
2238 SET_NUMERIC_STANDARD();
2239 XSRETURN_PV(retval);
2243 #ifdef USE_LOCALE_CTYPE
2244 if (category == LC_CTYPE
2246 || category == LC_ALL
2252 if (category == LC_ALL) {
2253 newctype = setlocale(LC_CTYPE, NULL);
2254 DEBUG_Lv(PerlIO_printf(Perl_debug_log,
2255 "%s:%d: %s\n", __FILE__, __LINE__,
2256 _setlocale_debug_string(LC_CTYPE, NULL, newctype)));
2261 new_ctype(newctype);
2263 #endif /* USE_LOCALE_CTYPE */
2264 #ifdef USE_LOCALE_COLLATE
2265 if (category == LC_COLLATE
2267 || category == LC_ALL
2273 if (category == LC_ALL) {
2274 newcoll = setlocale(LC_COLLATE, NULL);
2275 DEBUG_Lv(PerlIO_printf(Perl_debug_log,
2276 "%s:%d: %s\n", __FILE__, __LINE__,
2277 _setlocale_debug_string(LC_COLLATE, NULL, newcoll)));
2282 new_collate(newcoll);
2284 #endif /* USE_LOCALE_COLLATE */
2285 #ifdef USE_LOCALE_NUMERIC
2286 if (category == LC_NUMERIC
2288 || category == LC_ALL
2294 if (category == LC_ALL) {
2295 newnum = setlocale(LC_NUMERIC, NULL);
2296 DEBUG_Lv(PerlIO_printf(Perl_debug_log,
2297 "%s:%d: %s\n", __FILE__, __LINE__,
2298 _setlocale_debug_string(LC_NUMERIC, NULL, newnum)));
2303 new_numeric(newnum);
2305 #endif /* USE_LOCALE_NUMERIC */
2353 RETVAL = Perl_acos(x); /* C89 math */
2357 RETVAL = c99_acosh(x);
2363 RETVAL = Perl_asin(x); /* C89 math */
2367 RETVAL = c99_asinh(x);
2373 RETVAL = Perl_atan(x); /* C89 math */
2377 RETVAL = c99_atanh(x);
2384 RETVAL = c99_cbrt(x);
2390 RETVAL = Perl_ceil(x); /* C89 math */
2393 RETVAL = Perl_cosh(x); /* C89 math */
2397 RETVAL = c99_erf(x);
2404 RETVAL = c99_erfc(x);
2411 RETVAL = c99_exp2(x);
2418 RETVAL = c99_expm1(x);
2424 RETVAL = Perl_floor(x); /* C89 math */
2428 RETVAL = bessel_j0(x);
2435 RETVAL = bessel_j1(x);
2441 /* XXX Note: the lgamma modifies a global variable (signgam),
2442 * which is evil. Some platforms have lgamma_r, which has
2443 * extra output parameter instead of the global variable. */
2445 RETVAL = c99_lgamma(x);
2451 RETVAL = log10(x); /* C89 math */
2455 RETVAL = c99_log1p(x);
2462 RETVAL = c99_log2(x);
2469 RETVAL = c99_logb(x);
2470 #elif defined(c99_log2) && FLT_RADIX == 2
2471 RETVAL = Perl_floor(c99_log2(PERL_ABS(x)));
2477 #ifdef c99_nearbyint
2478 RETVAL = c99_nearbyint(x);
2480 not_here("nearbyint");
2485 RETVAL = c99_rint(x);
2492 RETVAL = c99_round(x);
2498 RETVAL = Perl_sinh(x); /* C89 math */
2501 RETVAL = Perl_tan(x); /* C89 math */
2504 RETVAL = Perl_tanh(x); /* C89 math */
2508 RETVAL = c99_tgamma(x);
2515 RETVAL = c99_trunc(x);
2522 RETVAL = bessel_y0(x);
2530 RETVAL = bessel_y1(x);
2541 #ifdef HAS_FEGETROUND
2542 RETVAL = my_fegetround();
2545 not_here("fegetround");
2554 #ifdef HAS_FEGETROUND /* canary for fesetround */
2555 RETVAL = fesetround(x);
2556 #elif defined(HAS_FPGETROUND) /* canary for fpsetround */
2558 case FE_TONEAREST: RETVAL = fpsetround(FP_RN); break;
2559 case FE_TOWARDZERO: RETVAL = fpsetround(FP_RZ); break;
2560 case FE_DOWNWARD: RETVAL = fpsetround(FP_RM); break;
2561 case FE_UPWARD: RETVAL = fpsetround(FP_RP); break;
2562 default: RETVAL = -1; break;
2564 #elif defined(__osf__) /* Tru64 */
2566 case FE_TONEAREST: RETVAL = write_rnd(FP_RND_RN); break;
2567 case FE_TOWARDZERO: RETVAL = write_rnd(FP_RND_RZ); break;
2568 case FE_DOWNWARD: RETVAL = write_rnd(FP_RND_RM); break;
2569 case FE_UPWARD: RETVAL = write_rnd(FP_RND_RP); break;
2570 default: RETVAL = -1; break;
2575 not_here("fesetround");
2597 #ifdef c99_fpclassify
2598 RETVAL = c99_fpclassify(x);
2600 not_here("fpclassify");
2605 RETVAL = c99_ilogb(x);
2611 RETVAL = Perl_isfinite(x);
2614 RETVAL = Perl_isinf(x);
2617 RETVAL = Perl_isnan(x);
2621 RETVAL = c99_isnormal(x);
2623 not_here("isnormal");
2628 RETVAL = c99_lrint(x);
2635 RETVAL = c99_lround(x);
2643 RETVAL = Perl_signbit(x);
2646 #ifdef DOUBLE_IS_IEEE_FORMAT
2661 #ifdef DOUBLE_HAS_NAN
2662 RETVAL = S_getpayload(nv);
2664 PERL_UNUSED_VAR(nv);
2666 not_here("getpayload");
2672 setpayload(nv, payload)
2676 #ifdef DOUBLE_HAS_NAN
2677 S_setpayload(&nv, payload, FALSE);
2679 PERL_UNUSED_VAR(nv);
2680 PERL_UNUSED_VAR(payload);
2681 not_here("setpayload");
2687 setpayloadsig(nv, payload)
2691 #ifdef DOUBLE_HAS_NAN
2693 S_setpayload(&nv, payload, TRUE);
2695 PERL_UNUSED_VAR(nv);
2696 PERL_UNUSED_VAR(payload);
2697 not_here("setpayloadsig");
2706 #ifdef DOUBLE_HAS_NAN
2707 RETVAL = Perl_isnan(nv) && NV_NAN_IS_SIGNALING(&nv);
2709 PERL_UNUSED_VAR(nv);
2711 not_here("issignaling");
2746 RETVAL = c99_copysign(x, y);
2748 not_here("copysign");
2753 RETVAL = c99_fdim(x, y);
2760 RETVAL = c99_fmax(x, y);
2767 RETVAL = c99_fmin(x, y);
2773 RETVAL = Perl_fmod(x, y); /* C89 math */
2777 RETVAL = c99_hypot(x, y);
2783 #ifdef c99_isgreater
2784 RETVAL = c99_isgreater(x, y);
2786 not_here("isgreater");
2790 #ifdef c99_isgreaterequal
2791 RETVAL = c99_isgreaterequal(x, y);
2793 not_here("isgreaterequal");
2798 RETVAL = c99_isless(x, y);
2804 #ifdef c99_islessequal
2805 RETVAL = c99_islessequal(x, y);
2807 not_here("islessequal");
2811 #ifdef c99_islessgreater
2812 RETVAL = c99_islessgreater(x, y);
2814 not_here("islessgreater");
2818 #ifdef c99_isunordered
2819 RETVAL = c99_isunordered(x, y);
2821 not_here("isunordered");
2825 #ifdef c99_nextafter
2826 RETVAL = c99_nextafter(x, y);
2828 not_here("nextafter");
2832 #ifdef c99_nexttoward
2833 RETVAL = c99_nexttoward(x, y);
2835 not_here("nexttoward");
2840 #ifdef c99_remainder
2841 RETVAL = c99_remainder(x, y);
2843 not_here("remainder");
2855 /* (We already know stack is long enough.) */
2856 PUSHs(sv_2mortal(newSVnv(Perl_frexp(x,&expvar)))); /* C89 math */
2857 PUSHs(sv_2mortal(newSViv(expvar)));
2869 /* (We already know stack is long enough.) */
2870 PUSHs(sv_2mortal(newSVnv(Perl_modf(x,&intvar)))); /* C89 math */
2871 PUSHs(sv_2mortal(newSVnv(intvar)));
2880 PUSHs(sv_2mortal(newSVnv(c99_remquo(x,y,&intvar))));
2881 PUSHs(sv_2mortal(newSVnv(intvar)));
2894 RETVAL = c99_scalbn(x, y);
2911 RETVAL = c99_fma(x, y, z);
2926 /* If no payload given, just return the default NaN.
2927 * This makes a difference in platforms where the default
2928 * NaN is not all zeros. */
2932 S_setpayload(&RETVAL, payload, FALSE);
2934 #elif defined(c99_nan)
2936 STRLEN elen = my_snprintf(PL_efloatbuf, PL_efloatsize, "%g", payload);
2937 if ((IV)elen == -1) {
2945 RETVAL = c99_nan(PL_efloatbuf);
2969 RETVAL = bessel_jn(x, y);
2979 RETVAL = bessel_yn(x, y);
2991 sigaction(sig, optaction, oldaction = 0)
2994 POSIX::SigAction oldaction
2996 #if defined(WIN32) || defined(NETWARE) || (defined(__amigaos4__) && defined(__NEWLIB__))
2997 RETVAL = not_here("sigaction");
2999 # This code is really grody because we are trying to make the signal
3000 # interface look beautiful, which is hard.
3004 POSIX__SigAction action;
3005 GV *siggv = gv_fetchpvs("SIG", GV_ADD, SVt_PVHV);
3006 struct sigaction act;
3007 struct sigaction oact;
3011 POSIX__SigSet sigset;
3016 croak("Negative signals are not allowed");
3019 if (sig == 0 && SvPOK(ST(0))) {
3020 const char *s = SvPVX_const(ST(0));
3021 int i = whichsig(s);
3023 if (i < 0 && _memEQs(s, "SIG"))
3024 i = whichsig(s + 3);
3026 if (ckWARN(WARN_SIGNAL))
3027 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
3028 "No such signal: SIG%s", s);
3035 if (sig > NSIG) { /* NSIG - 1 is still okay. */
3036 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
3037 "No such signal: %d", sig);
3041 sigsvp = hv_fetch(GvHVn(siggv),
3043 strlen(PL_sig_name[sig]),
3046 /* Check optaction and set action */
3047 if(SvTRUE(optaction)) {
3048 if(sv_isa(optaction, "POSIX::SigAction"))
3049 action = (HV*)SvRV(optaction);
3051 croak("action is not of type POSIX::SigAction");
3057 /* sigaction() is supposed to look atomic. In particular, any
3058 * signal handler invoked during a sigaction() call should
3059 * see either the old or the new disposition, and not something
3060 * in between. We use sigprocmask() to make it so.
3063 RETVAL=sigprocmask(SIG_BLOCK, &sset, &osset);
3067 /* Restore signal mask no matter how we exit this block. */
3068 osset_sv = newSVpvn((char *)(&osset), sizeof(sigset_t));
3069 SAVEFREESV( osset_sv );
3070 SAVEDESTRUCTOR_X(restore_sigmask, osset_sv);
3072 RETVAL=-1; /* In case both oldaction and action are 0. */
3074 /* Remember old disposition if desired. */
3076 svp = hv_fetchs(oldaction, "HANDLER", TRUE);
3078 croak("Can't supply an oldaction without a HANDLER");
3079 if(SvTRUE(*sigsvp)) { /* TBD: what if "0"? */
3080 sv_setsv(*svp, *sigsvp);
3083 sv_setpvs(*svp, "DEFAULT");
3085 RETVAL = sigaction(sig, (struct sigaction *)0, & oact);
3090 /* Get back the mask. */
3091 svp = hv_fetchs(oldaction, "MASK", TRUE);
3092 if (sv_isa(*svp, "POSIX::SigSet")) {
3093 sigset = (sigset_t *) SvPV_nolen(SvRV(*svp));
3096 sigset = (sigset_t *) allocate_struct(aTHX_ *svp,
3100 *sigset = oact.sa_mask;
3102 /* Get back the flags. */
3103 svp = hv_fetchs(oldaction, "FLAGS", TRUE);
3104 sv_setiv(*svp, oact.sa_flags);
3106 /* Get back whether the old handler used safe signals. */
3107 svp = hv_fetchs(oldaction, "SAFE", TRUE);
3109 /* compare incompatible pointers by casting to integer */
3110 PTR2nat(oact.sa_handler) == PTR2nat(PL_csighandlerp));
3114 /* Safe signals use "csighandler", which vectors through the
3115 PL_sighandlerp pointer when it's safe to do so.
3116 (BTW, "csighandler" is very different from "sighandler".) */
3117 svp = hv_fetchs(action, "SAFE", FALSE);
3121 (*svp && SvTRUE(*svp))
3122 ? PL_csighandlerp : PL_sighandlerp
3125 /* Vector new Perl handler through %SIG.
3126 (The core signal handlers read %SIG to dispatch.) */
3127 svp = hv_fetchs(action, "HANDLER", FALSE);
3129 croak("Can't supply an action without a HANDLER");
3130 sv_setsv(*sigsvp, *svp);
3132 /* This call actually calls sigaction() with almost the
3133 right settings, including appropriate interpretation
3134 of DEFAULT and IGNORE. However, why are we doing
3135 this when we're about to do it again just below? XXX */
3136 SvSETMAGIC(*sigsvp);
3138 /* And here again we duplicate -- DEFAULT/IGNORE checking. */
3140 const char *s=SvPVX_const(*svp);
3141 if(strEQ(s,"IGNORE")) {
3142 act.sa_handler = SIG_IGN;
3144 else if(strEQ(s,"DEFAULT")) {
3145 act.sa_handler = SIG_DFL;
3149 /* Set up any desired mask. */
3150 svp = hv_fetchs(action, "MASK", FALSE);
3151 if (svp && sv_isa(*svp, "POSIX::SigSet")) {
3152 sigset = (sigset_t *) SvPV_nolen(SvRV(*svp));
3153 act.sa_mask = *sigset;
3156 sigemptyset(& act.sa_mask);
3158 /* Set up any desired flags. */
3159 svp = hv_fetchs(action, "FLAGS", FALSE);
3160 act.sa_flags = svp ? SvIV(*svp) : 0;
3162 /* Don't worry about cleaning up *sigsvp if this fails,
3163 * because that means we tried to disposition a
3164 * nonblockable signal, in which case *sigsvp is
3165 * essentially meaningless anyway.
3167 RETVAL = sigaction(sig, & act, (struct sigaction *)0);
3182 POSIX::SigSet sigset
3187 RETVAL = not_here("sigpending");
3189 RETVAL = ix ? sigsuspend(sigset) : sigpending(sigset);
3197 sigprocmask(how, sigset, oldsigset = 0)
3199 POSIX::SigSet sigset = NO_INIT
3200 POSIX::SigSet oldsigset = NO_INIT
3202 if (! SvOK(ST(1))) {
3204 } else if (sv_isa(ST(1), "POSIX::SigSet")) {
3205 sigset = (sigset_t *) SvPV_nolen(SvRV(ST(1)));
3207 croak("sigset is not of type POSIX::SigSet");
3210 if (items < 3 || ! SvOK(ST(2))) {
3212 } else if (sv_isa(ST(2), "POSIX::SigSet")) {
3213 oldsigset = (sigset_t *) SvPV_nolen(SvRV(ST(2)));
3215 croak("oldsigset is not of type POSIX::SigSet");
3227 if (fd1 >= 0 && fd2 >= 0) {
3229 /* RT #98912 - More Microsoft muppetry - failing to
3230 actually implemented the well known documented POSIX
3231 behaviour for a POSIX API.
3232 http://msdn.microsoft.com/en-us/library/8syseb29.aspx */
3233 RETVAL = dup2(fd1, fd2) == -1 ? -1 : fd2;
3235 RETVAL = dup2(fd1, fd2);
3238 SETERRNO(EBADF,RMS_IFI);
3245 lseek(fd, offset, whence)
3251 Off_t pos = PerlLIO_lseek(fd, offset, whence);
3252 RETVAL = sizeof(Off_t) > sizeof(IV)
3253 ? newSVnv((NV)pos) : newSViv((IV)pos);
3263 if ((incr = nice(incr)) != -1 || errno == 0) {
3265 XPUSHs(newSVpvs_flags("0 but true", SVs_TEMP));
3267 XPUSHs(sv_2mortal(newSViv(incr)));
3274 if (pipe(fds) != -1) {
3276 PUSHs(sv_2mortal(newSViv(fds[0])));
3277 PUSHs(sv_2mortal(newSViv(fds[1])));
3281 read(fd, buffer, nbytes)
3283 SV *sv_buffer = SvROK(ST(1)) ? SvRV(ST(1)) : ST(1);
3287 char * buffer = sv_grow( sv_buffer, nbytes+1 );
3290 SvCUR_set(sv_buffer, RETVAL);
3291 SvPOK_only(sv_buffer);
3292 *SvEND(sv_buffer) = '\0';
3293 SvTAINTED_on(sv_buffer);
3309 tcsetpgrp(fd, pgrp_id)
3318 if (uname(&buf) >= 0) {
3320 PUSHs(newSVpvn_flags(buf.sysname, strlen(buf.sysname), SVs_TEMP));
3321 PUSHs(newSVpvn_flags(buf.nodename, strlen(buf.nodename), SVs_TEMP));
3322 PUSHs(newSVpvn_flags(buf.release, strlen(buf.release), SVs_TEMP));
3323 PUSHs(newSVpvn_flags(buf.version, strlen(buf.version), SVs_TEMP));
3324 PUSHs(newSVpvn_flags(buf.machine, strlen(buf.machine), SVs_TEMP));
3327 uname((char *) 0); /* A stub to call not_here(). */
3331 write(fd, buffer, nbytes)
3345 mbstowcs(s, pwcs, n)
3357 wcstombs(s, pwcs, n)
3379 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3380 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
3381 num = strtod(str, &unparsed);
3382 PUSHs(sv_2mortal(newSVnv(num)));
3383 if (GIMME_V == G_ARRAY) {
3386 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3388 PUSHs(&PL_sv_undef);
3390 RESTORE_LC_NUMERIC_STANDARD();
3401 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3402 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
3403 num = strtold(str, &unparsed);
3404 PUSHs(sv_2mortal(newSVnv(num)));
3405 if (GIMME_V == G_ARRAY) {
3408 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3410 PUSHs(&PL_sv_undef);
3412 RESTORE_LC_NUMERIC_STANDARD();
3417 strtol(str, base = 0)
3424 if (base == 0 || (base >= 2 && base <= 36)) {
3425 num = strtol(str, &unparsed, base);
3426 #if IVSIZE < LONGSIZE
3427 if (num < IV_MIN || num > IV_MAX)
3428 PUSHs(sv_2mortal(newSVnv((double)num)));
3431 PUSHs(sv_2mortal(newSViv((IV)num)));
3432 if (GIMME_V == G_ARRAY) {
3435 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3437 PUSHs(&PL_sv_undef);
3440 SETERRNO(EINVAL, LIB_INVARG);
3441 PUSHs(&PL_sv_undef);
3442 if (GIMME_V == G_ARRAY) {
3444 PUSHs(&PL_sv_undef);
3449 strtoul(str, base = 0)
3456 PERL_UNUSED_VAR(str);
3457 PERL_UNUSED_VAR(base);
3458 if (base == 0 || (base >= 2 && base <= 36)) {
3459 num = strtoul(str, &unparsed, base);
3460 #if IVSIZE <= LONGSIZE
3462 PUSHs(sv_2mortal(newSVnv((double)num)));
3465 PUSHs(sv_2mortal(newSViv((IV)num)));
3466 if (GIMME_V == G_ARRAY) {
3469 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3471 PUSHs(&PL_sv_undef);
3474 SETERRNO(EINVAL, LIB_INVARG);
3475 PUSHs(&PL_sv_undef);
3476 if (GIMME_V == G_ARRAY) {
3478 PUSHs(&PL_sv_undef);
3490 char *p = SvPV(src,srclen);
3492 buflen = srclen * 4 + 1;
3493 ST(0) = sv_2mortal(newSV(buflen));
3494 dstlen = strxfrm(SvPVX(ST(0)), p, (size_t)buflen);
3495 if (dstlen >= buflen) {
3497 SvGROW(ST(0), dstlen);
3498 strxfrm(SvPVX(ST(0)), p, (size_t)dstlen);
3501 SvCUR_set(ST(0), dstlen);
3506 mkfifo(filename, mode)
3513 RETVAL = access(filename, mode);
3515 TAINT_PROPER("mkfifo");
3516 RETVAL = mkfifo(filename, mode);
3529 RETVAL = ix == 1 ? close(fd)
3530 : (ix < 1 ? tcdrain(fd) : dup(fd));
3532 SETERRNO(EBADF,RMS_IFI);
3548 RETVAL = ix == 1 ? tcflush(fd, action)
3549 : (ix < 1 ? tcflow(fd, action) : tcsendbreak(fd, action));
3551 SETERRNO(EINVAL,LIB_INVARG);
3558 asctime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = -1)
3574 init_tm(&mytm); /* XXX workaround - see init_tm() in core util.c */
3577 mytm.tm_hour = hour;
3578 mytm.tm_mday = mday;
3580 mytm.tm_year = year;
3581 mytm.tm_wday = wday;
3582 mytm.tm_yday = yday;
3583 mytm.tm_isdst = isdst;
3585 const time_t result = mktime(&mytm);
3586 if (result == (time_t)-1)
3588 else if (result == 0)
3589 sv_setpvs(TARG, "0 but true");
3591 sv_setiv(TARG, (IV)result);
3593 sv_setpv(TARG, asctime(&mytm));
3611 realtime = times( &tms );
3613 PUSHs( sv_2mortal( newSViv( (IV) realtime ) ) );
3614 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_utime ) ) );
3615 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_stime ) ) );
3616 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cutime ) ) );
3617 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cstime ) ) );
3620 difftime(time1, time2)
3624 #XXX: if $xsubpp::WantOptimize is always the default
3625 # sv_setpv(TARG, ...) could be used rather than
3626 # ST(0) = sv_2mortal(newSVpv(...))
3628 strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
3644 /* allowing user-supplied (rather than literal) formats
3645 * is normally frowned upon as a potential security risk;
3646 * but this is part of the API so we have to allow it */
3647 GCC_DIAG_IGNORE(-Wformat-nonliteral);
3648 buf = my_strftime(SvPV_nolen(fmt), sec, min, hour, mday, mon, year, wday, yday, isdst);
3650 sv = sv_newmortal();
3652 STRLEN len = strlen(buf);
3653 sv_usepvn_flags(sv, buf, len, SV_HAS_TRAILING_NUL);
3655 || (! is_utf8_invariant_string((U8*) buf, len)
3656 && is_utf8_string((U8*) buf, len)
3657 #ifdef USE_LOCALE_TIME
3658 && _is_cur_LC_category_utf8(LC_TIME)
3664 else { /* We can't distinguish between errors and just an empty
3665 * return; in all cases just return an empty string */
3666 SvUPGRADE(sv, SVt_PV);
3667 SvPV_set(sv, (char *) "");
3670 SvLEN_set(sv, 0); /* Won't attempt to free the string when sv
3685 PUSHs(newSVpvn_flags(tzname[0], strlen(tzname[0]), SVs_TEMP));
3686 PUSHs(newSVpvn_flags(tzname[1], strlen(tzname[1]), SVs_TEMP));
3692 #ifdef HAS_CTERMID_R
3693 s = (char *) safemalloc((size_t) L_ctermid);
3695 RETVAL = ctermid(s);
3699 #ifdef HAS_CTERMID_R
3708 RETVAL = cuserid(s);
3712 not_here("cuserid");
3723 pathconf(filename, name)
3734 unsigned int seconds
3736 RETVAL = PerlProc_sleep(seconds);
3762 XSprePUSH; PUSHTARG;
3766 lchown(uid, gid, path)
3772 /* yes, the order of arguments is different,
3773 * but consistent with CORE::chown() */
3774 RETVAL = lchown(path, uid, gid);
3776 PERL_UNUSED_VAR(uid);
3777 PERL_UNUSED_VAR(gid);
3778 PERL_UNUSED_VAR(path);
3779 RETVAL = not_here("lchown");