7 * Ideally this should be somewhere down in the includes
8 * but putting it in other places is giving compiler errors.
9 * Also here I am unable to check for HAS_UNAME since it wouldn't have
10 * yet come into the file at this stage - sgp 18th Oct 2000
12 #include <sys/utsname.h>
15 #define PERL_NO_GET_CONTEXT
18 #define PERLIO_NOT_STDIO 1
22 static int not_here(const char *s);
24 #if defined(PERL_IMPLICIT_SYS)
28 # define open PerlLIO_open3
31 #ifdef I_DIRENT /* XXX maybe better to just rely on perl.h? */
36 #include <sys/errno2.h>
40 #if !(defined(__vax__) && defined(__NetBSD__))
60 # include <sys/time.h>
64 # include <sys/resource.h>
67 /* Cygwin's stdio.h doesn't make cuserid() visible with -D_GNU_SOURCE,
74 #if defined(USE_QUADMATH) && defined(I_QUADMATH)
91 # define M_LOG2E M_LOG2Eq
92 # define M_LOG10E M_LOG10Eq
94 # define M_LN10 M_LN10q
96 # define M_PI_2 M_PI_2q
97 # define M_PI_4 M_PI_4q
98 # define M_1_PI M_1_PIq
99 # define M_2_PI M_2_PIq
100 # define M_2_SQRTPI M_2_SQRTPIq
101 # define M_SQRT2 M_SQRT2q
102 # define M_SQRT1_2 M_SQRT1_2q
106 # ifdef USE_LONG_DOUBLE
120 # define FLOAT_C(c) CAT2(c,L)
122 # define FLOAT_C(c) (c)
126 # define M_E FLOAT_C(2.71828182845904523536028747135266250)
129 # define M_LOG2E FLOAT_C(1.44269504088896340735992468100189214)
132 # define M_LOG10E FLOAT_C(0.434294481903251827651128918916605082)
135 # define M_LN2 FLOAT_C(0.693147180559945309417232121458176568)
138 # define M_LN10 FLOAT_C(2.30258509299404568401799145468436421)
141 # define M_PI FLOAT_C(3.14159265358979323846264338327950288)
144 # define M_PI_2 FLOAT_C(1.57079632679489661923132169163975144)
147 # define M_PI_4 FLOAT_C(0.785398163397448309615660845819875721)
150 # define M_1_PI FLOAT_C(0.318309886183790671537767526745028724)
153 # define M_2_PI FLOAT_C(0.636619772367581343075535053490057448)
156 # define M_2_SQRTPI FLOAT_C(1.12837916709551257389615890312154517)
159 # define M_SQRT2 FLOAT_C(1.41421356237309504880168872420969808)
162 # define M_SQRT1_2 FLOAT_C(0.707106781186547524400844362104849039)
167 #if !defined(INFINITY) && defined(NV_INF)
168 # define INFINITY NV_INF
171 #if !defined(NAN) && defined(NV_NAN)
175 #if !defined(Inf) && defined(NV_INF)
179 #if !defined(NaN) && defined(NV_NAN)
183 /* We will have an emulation. */
185 # define FP_INFINITE 0
188 # define FP_SUBNORMAL 3
192 /* We will have an emulation. */
194 # define FE_TOWARDZERO 0
195 # define FE_TONEAREST 1
197 # define FE_DOWNWARD 3
202 acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp
203 log log10 modf pow sin sinh sqrt tan tanh
205 * Implemented in core:
207 atan2 cos exp log pow sin sqrt
211 acosh asinh atanh cbrt copysign erf erfc exp2 expm1 fdim fma fmax
212 fmin fpclassify hypot ilogb isfinite isgreater isgreaterequal isinf
213 isless islessequal islessgreater isnan isnormal isunordered lgamma
214 log1p log2 logb lrint lround nan nearbyint nextafter nexttoward remainder
215 remquo rint round scalbn signbit tgamma trunc
218 http://pubs.opengroup.org/onlinepubs/009695399/basedefs/math.h.html
220 * Berkeley/SVID extensions:
224 * Configure already (5.21.5) scans for:
226 copysign*l* fpclassify isfinite isinf isnan isnan*l* ilogb*l* signbit scalbn*l*
228 * For floating-point round mode (which matters for e.g. lrint and rint)
230 fegetround fesetround
234 /* XXX Constant FP_FAST_FMA (if true, FMA is faster) */
236 /* XXX Add ldiv(), lldiv()? It's C99, but from stdlib.h, not math.h */
238 /* XXX Beware old gamma() -- one cannot know whether that is the
239 * gamma or the log of gamma, that's why the new tgamma and lgamma.
240 * Though also remember lgamma_r. */
242 /* Certain AIX releases have the C99 math, but not in long double.
243 * The <math.h> has them, e.g. __expl128, but no library has them!
245 * Also see the comments in hints/aix.sh about long doubles. */
247 #if defined(USE_QUADMATH) && defined(I_QUADMATH)
248 # define c99_acosh acoshq
249 # define c99_asinh asinhq
250 # define c99_atanh atanhq
251 # define c99_cbrt cbrtq
252 # define c99_copysign copysignq
253 # define c99_erf erfq
254 # define c99_erfc erfcq
256 # define c99_expm1 expm1q
257 # define c99_fdim fdimq
258 # define c99_fma fmaq
259 # define c99_fmax fmaxq
260 # define c99_fmin fminq
261 # define c99_hypot hypotq
262 # define c99_ilogb ilogbq
263 # define c99_lgamma lgammaq
264 # define c99_log1p log1pq
265 # define c99_log2 log2q
267 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
268 # define c99_lrint llrintq
269 # define c99_lround llroundq
271 # define c99_lrint lrintq
272 # define c99_lround lroundq
274 # define c99_nan nanq
275 # define c99_nearbyint nearbyintq
276 # define c99_nextafter nextafterq
278 # define c99_remainder remainderq
279 # define c99_remquo remquoq
280 # define c99_rint rintq
281 # define c99_round roundq
282 # define c99_scalbn scalbnq
283 # define c99_signbit signbitq
284 # define c99_tgamma tgammaq
285 # define c99_trunc truncq
286 # define bessel_j0 j0q
287 # define bessel_j1 j1q
288 # define bessel_jn jnq
289 # define bessel_y0 y0q
290 # define bessel_y1 y1q
291 # define bessel_yn ynq
292 #elif defined(USE_LONG_DOUBLE) && \
293 (defined(HAS_FREXPL) || defined(HAS_ILOGBL)) && defined(HAS_SQRTL)
294 /* Use some of the Configure scans for long double math functions
295 * as the canary for all the C99 *l variants being defined. */
296 # define c99_acosh acoshl
297 # define c99_asinh asinhl
298 # define c99_atanh atanhl
299 # define c99_cbrt cbrtl
300 # define c99_copysign copysignl
301 # define c99_erf erfl
302 # define c99_erfc erfcl
303 # define c99_exp2 exp2l
304 # define c99_expm1 expm1l
305 # define c99_fdim fdiml
306 # define c99_fma fmal
307 # define c99_fmax fmaxl
308 # define c99_fmin fminl
309 # define c99_hypot hypotl
310 # define c99_ilogb ilogbl
311 # define c99_lgamma lgammal
312 # define c99_log1p log1pl
313 # define c99_log2 log2l
314 # define c99_logb logbl
315 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLRINTL)
316 # define c99_lrint llrintl
317 # elif defined(HAS_LRINTL)
318 # define c99_lrint lrintl
320 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLROUNDL)
321 # define c99_lround llroundl
322 # elif defined(HAS_LROUNDL)
323 # define c99_lround lroundl
325 # define c99_nan nanl
326 # define c99_nearbyint nearbyintl
327 # define c99_nextafter nextafterl
328 # define c99_nexttoward nexttowardl
329 # define c99_remainder remainderl
330 # define c99_remquo remquol
331 # define c99_rint rintl
332 # define c99_round roundl
333 # define c99_scalbn scalbnl
334 # ifdef HAS_SIGNBIT /* possibly bad assumption */
335 # define c99_signbit signbitl
337 # define c99_tgamma tgammal
338 # define c99_trunc truncl
340 # define c99_acosh acosh
341 # define c99_asinh asinh
342 # define c99_atanh atanh
343 # define c99_cbrt cbrt
344 # define c99_copysign copysign
346 # define c99_erfc erfc
347 # define c99_exp2 exp2
348 # define c99_expm1 expm1
349 # define c99_fdim fdim
351 # define c99_fmax fmax
352 # define c99_fmin fmin
353 # define c99_hypot hypot
354 # define c99_ilogb ilogb
355 # define c99_lgamma lgamma
356 # define c99_log1p log1p
357 # define c99_log2 log2
358 # define c99_logb logb
359 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLRINT)
360 # define c99_lrint llrint
362 # define c99_lrint lrint
364 # if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLROUND)
365 # define c99_lround llround
367 # define c99_lround lround
370 # define c99_nearbyint nearbyint
371 # define c99_nextafter nextafter
372 # define c99_nexttoward nexttoward
373 # define c99_remainder remainder
374 # define c99_remquo remquo
375 # define c99_rint rint
376 # define c99_round round
377 # define c99_scalbn scalbn
378 /* We already define Perl_signbit in perl.h. */
380 # define c99_signbit signbit
382 # define c99_tgamma tgamma
383 # define c99_trunc trunc
386 /* AIX xlc (__IBMC__) really doesn't have the following long double
387 * math interfaces (no __acoshl128 aka acoshl, etc.), see
388 * hints/aix.sh. These are in the -lc128 but fail to be found
389 * during dynamic linking/loading.
391 * XXX1 Better Configure scans
392 * XXX2 Is this xlc version dependent? */
393 #if defined(USE_LONG_DOUBLE) && defined(__IBMC__)
413 # undef c99_nearbyint
414 # undef c99_nextafter
415 # undef c99_nexttoward
416 # undef c99_remainder
427 # define isunordered(x, y) (Perl_isnan(x) || Perl_isnan(y))
428 # elif defined(HAS_UNORDERED)
429 # define isunordered(x, y) unordered(x, y)
433 /* XXX these isgreater/isnormal/isunordered macros definitions should
434 * be moved further in the file to be part of the emulations, so that
435 * platforms can e.g. #undef c99_isunordered and have it work like
436 * it does for the other interfaces. */
438 #if !defined(isgreater) && defined(isunordered)
439 # define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
440 # define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
441 # define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
442 # define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
443 # define islessgreater(x, y) (!isunordered((x), (y)) && \
444 ((x) > (y) || (y) > (x)))
447 /* Check both the Configure symbol and the macro-ness (like C99 promises). */
448 #if defined(HAS_FPCLASSIFY) && defined(fpclassify)
449 # define c99_fpclassify fpclassify
451 /* Like isnormal(), the isfinite(), isinf(), and isnan() are also C99
452 and also (sizeof-arg-aware) macros, but they are already well taken
453 care of by Configure et al, and defined in perl.h as
454 Perl_isfinite(), Perl_isinf(), and Perl_isnan(). */
456 # define c99_isnormal isnormal
458 #ifdef isgreater /* canary for all the C99 is*<cmp>* macros. */
459 # define c99_isgreater isgreater
460 # define c99_isgreaterequal isgreaterequal
461 # define c99_isless isless
462 # define c99_islessequal islessequal
463 # define c99_islessgreater islessgreater
464 # define c99_isunordered isunordered
467 /* The Great Wall of Undef where according to the definedness of HAS_FOO symbols
468 * the corresponding c99_foo wrappers are undefined. This list doesn't include
469 * the isfoo() interfaces because they are either type-aware macros, or dealt
470 * separately, already in perl.h */
511 #ifndef HAS_FPCLASSIFY
512 # undef c99_fpclassify
541 #ifndef HAS_NEARBYINT
542 # undef c99_nearbyint
544 #ifndef HAS_NEXTAFTER
545 # undef c99_nextafter
547 #ifndef HAS_NEXTTOWARD
548 # undef c99_nexttoward
550 #ifndef HAS_REMAINDER
551 # undef c99_remainder
577 /* Some APIs exist under Win32 with "underbar" names. */
580 # undef c99_nextafter
581 # define c99_hypot _hypot
582 # define c99_logb _logb
583 # define c99_nextafter _nextafter
585 # define bessel_j0 _j0
586 # define bessel_j1 _j1
587 # define bessel_jn _jn
588 # define bessel_y0 _y0
589 # define bessel_y1 _y1
590 # define bessel_yn _yn
594 /* The Bessel functions: BSD, SVID, XPG4, and POSIX. But not C99. */
595 #if defined(HAS_J0) && !defined(bessel_j0)
596 # if defined(USE_LONG_DOUBLE) && defined(HAS_J0L)
597 # define bessel_j0 j0l
598 # define bessel_j1 j1l
599 # define bessel_jn jnl
600 # define bessel_y0 y0l
601 # define bessel_y1 y1l
602 # define bessel_yn ynl
604 # define bessel_j0 j0
605 # define bessel_j1 j1
606 # define bessel_jn jn
607 # define bessel_y0 y0
608 # define bessel_y1 y1
609 # define bessel_yn yn
613 /* Emulations for missing math APIs.
615 * Keep in mind that the point of many of these functions is that
616 * they, if available, are supposed to give more precise/more
617 * numerically stable results.
619 * See e.g. http://www.johndcook.com/math_h.html
623 static NV my_acosh(NV x)
625 return Perl_log(x + Perl_sqrt(x * x - 1));
627 # define c99_acosh my_acosh
631 static NV my_asinh(NV x)
633 return Perl_log(x + Perl_sqrt(x * x + 1));
635 # define c99_asinh my_asinh
639 static NV my_atanh(NV x)
641 return (Perl_log(1 + x) - Perl_log(1 - x)) / 2;
643 # define c99_atanh my_atanh
647 static NV my_cbrt(NV x)
649 static const NV one_third = (NV)1.0/3;
650 return x >= 0.0 ? Perl_pow(x, one_third) : -Perl_pow(-x, one_third);
652 # define c99_cbrt my_cbrt
656 static NV my_copysign(NV x, NV y)
658 return y >= 0 ? (x < 0 ? -x : x) : (x < 0 ? x : -x);
660 # define c99_copysign my_copysign
663 /* XXX cosh (though c89) */
666 static NV my_erf(NV x)
668 /* http://www.johndcook.com/cpp_erf.html -- public domain */
670 NV a2 = -0.284496736;
672 NV a4 = -1.453152027;
676 int sign = x < 0 ? -1 : 1; /* Save the sign. */
679 /* Abramowitz and Stegun formula 7.1.26 */
680 t = 1.0 / (1.0 + p * x);
681 y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1) * t * Perl_exp(-x*x);
685 # define c99_erf my_erf
689 static NV my_erfc(NV x) {
690 /* This is not necessarily numerically stable, but better than nothing. */
691 return 1.0 - c99_erf(x);
693 # define c99_erfc my_erfc
697 static NV my_exp2(NV x)
699 return Perl_pow((NV)2.0, x);
701 # define c99_exp2 my_exp2
705 static NV my_expm1(NV x)
707 if (PERL_ABS(x) < 1e-5)
708 /* http://www.johndcook.com/cpp_expm1.html -- public domain.
709 * Taylor series, the first four terms (the last term quartic). */
710 /* Probably not enough for long doubles. */
711 return x * (1.0 + x * (1/2.0 + x * (1/6.0 + x/24.0)));
713 return Perl_exp(x) - 1;
715 # define c99_expm1 my_expm1
719 static NV my_fdim(NV x, NV y)
722 return (Perl_isnan(x) || Perl_isnan(y)) ? NV_NAN : (x > y ? x - y : 0);
724 return (x > y ? x - y : 0);
727 # define c99_fdim my_fdim
731 static NV my_fma(NV x, NV y, NV z)
735 # define c99_fma my_fma
739 static NV my_fmax(NV x, NV y)
743 return Perl_isnan(y) ? NV_NAN : y;
744 } else if (Perl_isnan(y)) {
748 return x > y ? x : y;
750 # define c99_fmax my_fmax
754 static NV my_fmin(NV x, NV y)
758 return Perl_isnan(y) ? NV_NAN : y;
759 } else if (Perl_isnan(y)) {
763 return x < y ? x : y;
765 # define c99_fmin my_fmin
768 #ifndef c99_fpclassify
770 static IV my_fpclassify(NV x)
772 #ifdef Perl_fp_class_inf
773 if (Perl_fp_class_inf(x)) return FP_INFINITE;
774 if (Perl_fp_class_nan(x)) return FP_NAN;
775 if (Perl_fp_class_norm(x)) return FP_NORMAL;
776 if (Perl_fp_class_denorm(x)) return FP_SUBNORMAL;
777 if (Perl_fp_class_zero(x)) return FP_ZERO;
778 # define c99_fpclassify my_fpclassify
786 static NV my_hypot(NV x, NV y)
788 /* http://en.wikipedia.org/wiki/Hypot */
790 x = PERL_ABS(x); /* Take absolute values. */
798 if (x < y) { /* Swap so that y is less. */
804 return x * Perl_sqrt(1.0 + t * t);
806 # define c99_hypot my_hypot
810 static IV my_ilogb(NV x)
812 return (IV)(Perl_log(x) * M_LOG2E);
814 # define c99_ilogb my_ilogb
817 /* tgamma and lgamma emulations based on
818 * http://www.johndcook.com/cpp_gamma.html,
819 * code placed in public domain.
821 * Note that these implementations (neither the johndcook originals
822 * nor these) do NOT set the global signgam variable. This is not
823 * necessarily a bad thing. */
825 /* Note that the tgamma() and lgamma() implementations
826 * here depend on each other. */
828 #if !defined(HAS_TGAMMA) || !defined(c99_tgamma)
829 static NV my_tgamma(NV x);
830 # define c99_tgamma my_tgamma
831 # define USE_MY_TGAMMA
833 #if !defined(HAS_LGAMMA) || !defined(c99_lgamma)
834 static NV my_lgamma(NV x);
835 # define c99_lgamma my_lgamma
836 # define USE_MY_LGAMMA
840 static NV my_tgamma(NV x)
842 const NV gamma = 0.577215664901532860606512090; /* Euler's gamma constant. */
844 if (Perl_isnan(x) || x < 0.0)
848 if (x == 0.0 || x == NV_INF)
849 #ifdef DOUBLE_IS_IEEE_FORMAT
850 return x == -0.0 ? -NV_INF : NV_INF;
856 /* The function domain is split into three intervals:
857 * (0, 0.001), [0.001, 12), and (12, infinity) */
859 /* First interval: (0, 0.001)
860 * For small values, 1/tgamma(x) has power series x + gamma x^2,
861 * so in this range, 1/tgamma(x) = x + gamma x^2 with error on the order of x^3.
862 * The relative error over this interval is less than 6e-7. */
864 return 1.0 / (x * (1.0 + gamma * x));
866 /* Second interval: [0.001, 12) */
868 double y = x; /* Working copy. */
870 /* Numerator coefficients for approximation over the interval (1,2) */
871 static const NV p[] = {
872 -1.71618513886549492533811E+0,
873 2.47656508055759199108314E+1,
874 -3.79804256470945635097577E+2,
875 6.29331155312818442661052E+2,
876 8.66966202790413211295064E+2,
877 -3.14512729688483675254357E+4,
878 -3.61444134186911729807069E+4,
879 6.64561438202405440627855E+4
881 /* Denominator coefficients for approximation over the interval (1, 2) */
882 static const NV q[] = {
883 -3.08402300119738975254353E+1,
884 3.15350626979604161529144E+2,
885 -1.01515636749021914166146E+3,
886 -3.10777167157231109440444E+3,
887 2.25381184209801510330112E+4,
888 4.75584627752788110767815E+3,
889 -1.34659959864969306392456E+5,
890 -1.15132259675553483497211E+5
901 n = (int)Perl_floor(y) - 1;
905 for (i = 0; i < 8; i++) {
906 num = (num + p[i]) * z;
907 den = den * z + q[i];
909 result = num / den + 1.0;
912 /* Use the identity tgamma(z) = tgamma(z+1)/z
913 * The variable "result" now holds tgamma of the original y + 1
914 * Thus we use y - 1 to get back the original y. */
918 /* Use the identity tgamma(z+n) = z*(z+1)* ... *(z+n-1)*tgamma(z) */
919 for (i = 0; i < n; i++)
927 /* Third interval: [12, +Inf) */
928 #if LDBL_MANT_DIG == 113 /* IEEE quad prec */
939 return Perl_exp(c99_lgamma(x));
944 static NV my_lgamma(NV x)
951 if (x <= 0 || x == NV_INF)
954 if (x == 1.0 || x == 2.0)
957 return Perl_log(PERL_ABS(c99_tgamma(x)));
958 /* Abramowitz and Stegun 6.1.41
959 * Asymptotic series should be good to at least 11 or 12 figures
960 * For error analysis, see Whittiker and Watson
961 * A Course in Modern Analysis (1927), page 252 */
963 static const NV c[8] = {
973 NV z = 1.0 / (x * x);
975 static const NV half_log_of_two_pi =
976 0.91893853320467274178032973640562;
979 for (i = 6; i >= 0; i--) {
984 return (x - 0.5) * Perl_log(x) - x + half_log_of_two_pi + series;
990 static NV my_log1p(NV x)
992 /* http://www.johndcook.com/cpp_log_one_plus_x.html -- public domain.
993 * Taylor series, the first four terms (the last term quartic). */
1002 if (PERL_ABS(x) > 1e-4)
1003 return Perl_log(1.0 + x);
1005 /* Probably not enough for long doubles. */
1006 return x * (1.0 + x * (-1/2.0 + x * (1/3.0 - x/4.0)));
1008 # define c99_log1p my_log1p
1012 static NV my_log2(NV x)
1014 return Perl_log(x) * M_LOG2E;
1016 # define c99_log2 my_log2
1021 /* XXX nexttoward */
1023 static int my_fegetround()
1025 #ifdef HAS_FEGETROUND
1026 return fegetround();
1027 #elif defined(HAS_FPGETROUND)
1028 switch (fpgetround()) {
1029 case FP_RN: return FE_TONEAREST;
1030 case FP_RZ: return FE_TOWARDZERO;
1031 case FP_RM: return FE_DOWNWARD;
1032 case FP_RP: return FE_UPWARD;
1035 #elif defined(FLT_ROUNDS)
1036 switch (FLT_ROUNDS) {
1037 case 0: return FE_TOWARDZERO;
1038 case 1: return FE_TONEAREST;
1039 case 2: return FE_UPWARD;
1040 case 3: return FE_DOWNWARD;
1043 #elif defined(__osf__) /* Tru64 */
1044 switch (read_rnd()) {
1045 case FP_RND_RN: return FE_TONEAREST;
1046 case FP_RND_RZ: return FE_TOWARDZERO;
1047 case FP_RND_RM: return FE_DOWNWARD;
1048 case FP_RND_RP: return FE_UPWARD;
1056 /* Toward closest integer. */
1057 #define MY_ROUND_NEAREST(x) ((NV)((IV)((x) >= 0.0 ? (x) + 0.5 : (x) - 0.5)))
1060 #define MY_ROUND_TRUNC(x) ((NV)((IV)(x)))
1062 /* Toward minus infinity. */
1063 #define MY_ROUND_DOWN(x) ((NV)((IV)((x) >= 0.0 ? (x) : (x) - 0.5)))
1065 /* Toward plus infinity. */
1066 #define MY_ROUND_UP(x) ((NV)((IV)((x) >= 0.0 ? (x) + 0.5 : (x))))
1068 #if (!defined(c99_nearbyint) || !defined(c99_lrint)) && defined(FE_TONEAREST)
1069 static NV my_rint(NV x)
1072 switch (my_fegetround()) {
1073 case FE_TONEAREST: return MY_ROUND_NEAREST(x);
1074 case FE_TOWARDZERO: return MY_ROUND_TRUNC(x);
1075 case FE_DOWNWARD: return MY_ROUND_DOWN(x);
1076 case FE_UPWARD: return MY_ROUND_UP(x);
1079 #elif defined(HAS_FPGETROUND)
1080 switch (fpgetround()) {
1081 case FP_RN: return MY_ROUND_NEAREST(x);
1082 case FP_RZ: return MY_ROUND_TRUNC(x);
1083 case FP_RM: return MY_ROUND_DOWN(x);
1084 case FE_RP: return MY_ROUND_UP(x);
1092 /* XXX nearbyint() and rint() are not really identical -- but the difference
1093 * is messy: nearbyint is defined NOT to raise FE_INEXACT floating point
1094 * exceptions, while rint() is defined to MAYBE raise them. At the moment
1095 * Perl is blissfully unaware of such fine detail of floating point. */
1096 #ifndef c99_nearbyint
1097 # ifdef FE_TONEAREST
1098 # define c99_nearbyrint my_rint
1103 # ifdef FE_TONEAREST
1104 static IV my_lrint(NV x)
1106 return (IV)my_rint(x);
1108 # define c99_lrint my_lrint
1113 static IV my_lround(NV x)
1115 return (IV)MY_ROUND_NEAREST(x);
1117 # define c99_lround my_lround
1125 # ifdef FE_TONEAREST
1126 # define c99_rint my_rint
1131 static NV my_round(NV x)
1133 return MY_ROUND_NEAREST(x);
1135 # define c99_round my_round
1139 # if defined(Perl_ldexp) && FLT_RADIX == 2
1140 static NV my_scalbn(NV x, int y)
1142 return Perl_ldexp(x, y);
1144 # define c99_scalbn my_scalbn
1148 /* XXX sinh (though c89) */
1150 /* tgamma -- see lgamma */
1152 /* XXX tanh (though c89) */
1155 static NV my_trunc(NV x)
1157 return MY_ROUND_TRUNC(x);
1159 # define c99_trunc my_trunc
1164 #undef NV_PAYLOAD_DEBUG
1166 /* NOTE: the NaN payload API implementation is hand-rolled, since the
1167 * APIs are only proposed ones as of June 2015, so very few, if any,
1168 * platforms have implementations yet, so HAS_SETPAYLOAD and such are
1169 * unlikely to be helpful.
1171 * XXX - if the core numification wants to actually generate
1172 * the nan payload in "nan(123)", and maybe "nans(456)", for
1173 * signaling payload", this needs to be moved to e.g. numeric.c
1174 * (look for grok_infnan)
1176 * Conversely, if the core stringification wants the nan payload
1177 * and/or the nan quiet/signaling distinction, S_getpayload()
1178 * from this file needs to be moved, to e.g. sv.c (look for S_infnan_2pv),
1179 * and the (trivial) functionality of issignaling() copied
1180 * (for generating "NaNS", or maybe even "NaNQ") -- or maybe there
1181 * are too many formatting parameters for simple stringification?
1184 /* While it might make sense for the payload to be UV or IV,
1185 * to avoid conversion loss, the proposed ISO interfaces use
1186 * a floating point input, which is then truncated to integer,
1187 * and only the integer part being used. This is workable,
1188 * except for: (1) the conversion loss (2) suboptimal for
1189 * 32-bit integer platforms. A workaround API for (2) and
1190 * in general for bit-honesty would be an array of integers
1191 * as the payload... but the proposed C API does nothing of
1193 #if NVSIZE == UVSIZE
1194 # define NV_PAYLOAD_TYPE UV
1196 # define NV_PAYLOAD_TYPE NV
1199 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
1200 # define NV_PAYLOAD_SIZEOF_ASSERT(a) \
1201 STATIC_ASSERT_STMT(sizeof(a) == NVSIZE / 2)
1203 # define NV_PAYLOAD_SIZEOF_ASSERT(a) \
1204 STATIC_ASSERT_STMT(sizeof(a) == NVSIZE)
1207 static void S_setpayload(NV* nvp, NV_PAYLOAD_TYPE payload, bool signaling)
1210 static const U8 m[] = { NV_NAN_PAYLOAD_MASK };
1211 static const U8 p[] = { NV_NAN_PAYLOAD_PERM };
1212 UV a[(NVSIZE + UVSIZE - 1) / UVSIZE] = { 0 };
1214 NV_PAYLOAD_SIZEOF_ASSERT(m);
1215 NV_PAYLOAD_SIZEOF_ASSERT(p);
1217 /* Divide the input into the array in "base unsigned integer" in
1218 * little-endian order. Note that the integer might be smaller than
1219 * an NV (if UV is U32, for example). */
1220 #if NVSIZE == UVSIZE
1221 a[0] = payload; /* The trivial case. */
1224 NV t1 = c99_trunc(payload); /* towards zero (drop fractional) */
1225 #ifdef NV_PAYLOAD_DEBUG
1226 Perl_warn(aTHX_ "t1 = %" NVgf " (payload %" NVgf ")\n", t1, payload);
1229 a[0] = (UV)t1; /* Fast path, also avoids rounding errors (right?) */
1231 /* UVSIZE < NVSIZE or payload > UV_MAX.
1233 * This may happen for example if:
1234 * (1) UVSIZE == 32 and common 64-bit double NV
1235 * (32-bit system not using -Duse64bitint)
1236 * (2) UVSIZE == 64 and the x86-style 80-bit long double NV
1237 * (note that here the room for payload is actually the 64 bits)
1238 * (3) UVSIZE == 64 and the 128-bit IEEE 764 quadruple NV
1239 * (112 bits in mantissa, 111 bits room for payload)
1241 * NOTE: this is very sensitive to correctly functioning
1242 * fmod()/fmodl(), and correct casting of big-unsigned-integer to NV.
1243 * If these don't work right, especially the low order bits
1244 * are in danger. For example Solaris and AIX seem to have issues
1245 * here, especially if using 32-bit UVs. */
1247 for (i = 0, t2 = t1; i < (int)C_ARRAY_LENGTH(a); i++) {
1248 a[i] = (UV)Perl_fmod(t2, (NV)UV_MAX);
1249 t2 = Perl_floor(t2 / (NV)UV_MAX);
1254 #ifdef NV_PAYLOAD_DEBUG
1255 for (i = 0; i < (int)C_ARRAY_LENGTH(a); i++) {
1256 Perl_warn(aTHX_ "a[%d] = 0x%" UVxf "\n", i, a[i]);
1259 for (i = 0; i < (int)sizeof(p); i++) {
1260 if (m[i] && p[i] < sizeof(p)) {
1261 U8 s = (p[i] % UVSIZE) << 3;
1262 UV u = a[p[i] / UVSIZE] & ((UV)0xFF << s);
1263 U8 b = (U8)((u >> s) & m[i]);
1264 ((U8 *)(nvp))[i] &= ~m[i]; /* For NaNs with non-zero payload bits. */
1265 ((U8 *)(nvp))[i] |= b;
1266 #ifdef NV_PAYLOAD_DEBUG
1268 "set p[%2d] = %02x (i = %d, m = %02x, s = %2d, b = %02x, u = %08"
1269 UVxf ")\n", i, ((U8 *)(nvp))[i], i, m[i], s, b, u);
1271 a[p[i] / UVSIZE] &= ~u;
1275 NV_NAN_SET_SIGNALING(nvp);
1277 #ifdef USE_LONG_DOUBLE
1278 # if LONG_DOUBLEKIND == 3 || LONG_DOUBLEKIND == 4
1279 # if LONG_DOUBLESIZE > 10
1280 memset((char *)nvp + 10, '\0', LONG_DOUBLESIZE - 10); /* x86 long double */
1284 for (i = 0; i < (int)C_ARRAY_LENGTH(a); i++) {
1286 Perl_warn(aTHX_ "payload lost bits (%" UVxf ")", a[i]);
1290 #ifdef NV_PAYLOAD_DEBUG
1291 for (i = 0; i < NVSIZE; i++) {
1292 PerlIO_printf(Perl_debug_log, "%02x ", ((U8 *)(nvp))[i]);
1294 PerlIO_printf(Perl_debug_log, "\n");
1298 static NV_PAYLOAD_TYPE S_getpayload(NV nv)
1301 static const U8 m[] = { NV_NAN_PAYLOAD_MASK };
1302 static const U8 p[] = { NV_NAN_PAYLOAD_PERM };
1303 UV a[(NVSIZE + UVSIZE - 1) / UVSIZE] = { 0 };
1306 NV_PAYLOAD_SIZEOF_ASSERT(m);
1307 NV_PAYLOAD_SIZEOF_ASSERT(p);
1309 for (i = 0; i < (int)sizeof(p); i++) {
1310 if (m[i] && p[i] < NVSIZE) {
1311 U8 s = (p[i] % UVSIZE) << 3;
1312 a[p[i] / UVSIZE] |= (UV)(((U8 *)(&nv))[i] & m[i]) << s;
1315 for (i = (int)C_ARRAY_LENGTH(a) - 1; i >= 0; i--) {
1316 #ifdef NV_PAYLOAD_DEBUG
1317 Perl_warn(aTHX_ "a[%d] = %" UVxf "\n", i, a[i]);
1322 #ifdef NV_PAYLOAD_DEBUG
1323 for (i = 0; i < NVSIZE; i++) {
1324 PerlIO_printf(Perl_debug_log, "%02x ", ((U8 *)(&nv))[i]);
1326 PerlIO_printf(Perl_debug_log, "\n");
1331 #endif /* #ifdef NV_NAN */
1333 /* XXX This comment is just to make I_TERMIO and I_SGTTY visible to
1334 metaconfig for future extension writers. We don't use them in POSIX.
1335 (This is really sneaky :-) --AD
1337 #if defined(I_TERMIOS)
1338 #include <termios.h>
1344 #include <sys/stat.h>
1345 #include <sys/types.h>
1353 # if !defined(WIN32) && !defined(__CYGWIN__) && !defined(NETWARE) && !defined(__UWIN__)
1354 extern char *tzname[];
1357 #if !defined(WIN32) && !defined(__UWIN__) || (defined(__MINGW32__) && !defined(tzname))
1358 char *tzname[] = { "" , "" };
1362 #if defined(__VMS) && !defined(__POSIX_SOURCE)
1364 # include <utsname.h>
1367 # define mkfifo(a,b) (not_here("mkfifo"),-1)
1369 /* The POSIX notion of ttyname() is better served by getname() under VMS */
1370 static char ttnambuf[64];
1371 # define ttyname(fd) (isatty(fd) > 0 ? getname(fd,ttnambuf,0) : NULL)
1374 #if defined (__CYGWIN__)
1375 # define tzname _tzname
1377 #if defined (WIN32) || defined (NETWARE)
1379 # define mkfifo(a,b) not_here("mkfifo")
1380 # define ttyname(a) (char*)not_here("ttyname")
1381 # define sigset_t long
1384 # define mode_t short
1387 # define mode_t short
1389 # define tzset() not_here("tzset")
1391 # ifndef _POSIX_OPEN_MAX
1392 # define _POSIX_OPEN_MAX FOPEN_MAX /* XXX bogus ? */
1395 # define sigaction(a,b,c) not_here("sigaction")
1396 # define sigpending(a) not_here("sigpending")
1397 # define sigprocmask(a,b,c) not_here("sigprocmask")
1398 # define sigsuspend(a) not_here("sigsuspend")
1399 # define sigemptyset(a) not_here("sigemptyset")
1400 # define sigaddset(a,b) not_here("sigaddset")
1401 # define sigdelset(a,b) not_here("sigdelset")
1402 # define sigfillset(a) not_here("sigfillset")
1403 # define sigismember(a,b) not_here("sigismember")
1407 # define setuid(a) not_here("setuid")
1408 # define setgid(a) not_here("setgid")
1409 #endif /* NETWARE */
1410 #ifndef USE_LONG_DOUBLE
1411 # define strtold(s1,s2) not_here("strtold")
1412 #endif /* USE_LONG_DOUBLE */
1416 # if defined(OS2) || defined(__amigaos4__)
1417 # define mkfifo(a,b) not_here("mkfifo")
1418 # else /* !( defined OS2 ) */
1420 # define mkfifo(path, mode) (mknod((path), (mode) | S_IFIFO, 0))
1423 # endif /* !HAS_MKFIFO */
1428 # include <sys/times.h>
1430 # include <sys/utsname.h>
1432 # ifndef __amigaos4__
1433 # include <sys/wait.h>
1438 #endif /* WIN32 || NETWARE */
1442 typedef long SysRetLong;
1443 typedef sigset_t* POSIX__SigSet;
1444 typedef HV* POSIX__SigAction;
1445 typedef int POSIX__SigNo;
1446 typedef int POSIX__Fd;
1448 typedef struct termios* POSIX__Termios;
1449 #else /* Define termios types to int, and call not_here for the functions.*/
1450 #define POSIX__Termios int
1452 #define tcflag_t int
1454 #define cfgetispeed(x) not_here("cfgetispeed")
1455 #define cfgetospeed(x) not_here("cfgetospeed")
1456 #define tcdrain(x) not_here("tcdrain")
1457 #define tcflush(x,y) not_here("tcflush")
1458 #define tcsendbreak(x,y) not_here("tcsendbreak")
1459 #define cfsetispeed(x,y) not_here("cfsetispeed")
1460 #define cfsetospeed(x,y) not_here("cfsetospeed")
1461 #define ctermid(x) (char *) not_here("ctermid")
1462 #define tcflow(x,y) not_here("tcflow")
1463 #define tcgetattr(x,y) not_here("tcgetattr")
1464 #define tcsetattr(x,y,z) not_here("tcsetattr")
1467 /* Possibly needed prototypes */
1470 double strtod (const char *, char **);
1471 long strtol (const char *, char **, int);
1472 unsigned long strtoul (const char *, char **, int);
1474 long double strtold (const char *, char **);
1479 #ifndef HAS_DIFFTIME
1481 #define difftime(a,b) not_here("difftime")
1484 #ifndef HAS_FPATHCONF
1485 #define fpathconf(f,n) (SysRetLong) not_here("fpathconf")
1488 #define mktime(a) not_here("mktime")
1491 #define nice(a) not_here("nice")
1493 #ifndef HAS_PATHCONF
1494 #define pathconf(f,n) (SysRetLong) not_here("pathconf")
1497 #define sysconf(n) (SysRetLong) not_here("sysconf")
1499 #ifndef HAS_READLINK
1500 #define readlink(a,b,c) not_here("readlink")
1503 #define setpgid(a,b) not_here("setpgid")
1506 #define setsid() not_here("setsid")
1509 #define strcoll(s1,s2) not_here("strcoll")
1512 #define strtod(s1,s2) not_here("strtod")
1515 #define strtold(s1,s2) not_here("strtold")
1518 #define strtol(s1,s2,b) not_here("strtol")
1521 #define strtoul(s1,s2,b) not_here("strtoul")
1524 #define strxfrm(s1,s2,n) not_here("strxfrm")
1526 #ifndef HAS_TCGETPGRP
1527 #define tcgetpgrp(a) not_here("tcgetpgrp")
1529 #ifndef HAS_TCSETPGRP
1530 #define tcsetpgrp(a,b) not_here("tcsetpgrp")
1534 #define times(a) not_here("times")
1535 #endif /* NETWARE */
1538 #define uname(a) not_here("uname")
1541 #define waitpid(a,b,c) not_here("waitpid")
1546 #define mblen(a,b) not_here("mblen")
1549 #ifndef HAS_MBSTOWCS
1550 #define mbstowcs(s, pwcs, n) not_here("mbstowcs")
1553 #define mbtowc(pwc, s, n) not_here("mbtowc")
1555 #ifndef HAS_WCSTOMBS
1556 #define wcstombs(s, pwcs, n) not_here("wcstombs")
1559 #define wctomb(s, wchar) not_here("wcstombs")
1561 #if !defined(HAS_MBLEN) && !defined(HAS_MBSTOWCS) && !defined(HAS_MBTOWC) && !defined(HAS_WCSTOMBS) && !defined(HAS_WCTOMB)
1562 /* If we don't have these functions, then we wouldn't have gotten a typedef
1563 for wchar_t, the wide character type. Defining wchar_t allows the
1564 functions referencing it to compile. Its actual type is then meaningless,
1565 since without the above functions, all sections using it end up calling
1566 not_here() and croak. --Kaveh Ghazi (ghazi@noc.rutgers.edu) 9/18/94. */
1568 #define wchar_t char
1572 #ifndef HAS_LOCALECONV
1573 # define localeconv() not_here("localeconv")
1575 struct lconv_offset {
1580 static const struct lconv_offset lconv_strings[] = {
1581 #ifdef USE_LOCALE_NUMERIC
1582 {"decimal_point", STRUCT_OFFSET(struct lconv, decimal_point)},
1583 {"thousands_sep", STRUCT_OFFSET(struct lconv, thousands_sep)},
1584 # ifndef NO_LOCALECONV_GROUPING
1585 {"grouping", STRUCT_OFFSET(struct lconv, grouping)},
1588 #ifdef USE_LOCALE_MONETARY
1589 {"int_curr_symbol", STRUCT_OFFSET(struct lconv, int_curr_symbol)},
1590 {"currency_symbol", STRUCT_OFFSET(struct lconv, currency_symbol)},
1591 {"mon_decimal_point", STRUCT_OFFSET(struct lconv, mon_decimal_point)},
1592 # ifndef NO_LOCALECONV_MON_THOUSANDS_SEP
1593 {"mon_thousands_sep", STRUCT_OFFSET(struct lconv, mon_thousands_sep)},
1595 # ifndef NO_LOCALECONV_MON_GROUPING
1596 {"mon_grouping", STRUCT_OFFSET(struct lconv, mon_grouping)},
1598 {"positive_sign", STRUCT_OFFSET(struct lconv, positive_sign)},
1599 {"negative_sign", STRUCT_OFFSET(struct lconv, negative_sign)},
1604 #ifdef USE_LOCALE_NUMERIC
1606 /* The Linux man pages say these are the field names for the structure
1607 * components that are LC_NUMERIC; the rest being LC_MONETARY */
1608 # define isLC_NUMERIC_STRING(name) ( strEQ(name, "decimal_point") \
1609 || strEQ(name, "thousands_sep") \
1611 /* There should be no harm done \
1612 * checking for this, even if \
1613 * NO_LOCALECONV_GROUPING */ \
1614 || strEQ(name, "grouping"))
1616 # define isLC_NUMERIC_STRING(name) (0)
1619 static const struct lconv_offset lconv_integers[] = {
1620 #ifdef USE_LOCALE_MONETARY
1621 {"int_frac_digits", STRUCT_OFFSET(struct lconv, int_frac_digits)},
1622 {"frac_digits", STRUCT_OFFSET(struct lconv, frac_digits)},
1623 {"p_cs_precedes", STRUCT_OFFSET(struct lconv, p_cs_precedes)},
1624 {"p_sep_by_space", STRUCT_OFFSET(struct lconv, p_sep_by_space)},
1625 {"n_cs_precedes", STRUCT_OFFSET(struct lconv, n_cs_precedes)},
1626 {"n_sep_by_space", STRUCT_OFFSET(struct lconv, n_sep_by_space)},
1627 {"p_sign_posn", STRUCT_OFFSET(struct lconv, p_sign_posn)},
1628 {"n_sign_posn", STRUCT_OFFSET(struct lconv, n_sign_posn)},
1629 #ifdef HAS_LC_MONETARY_2008
1630 {"int_p_cs_precedes", STRUCT_OFFSET(struct lconv, int_p_cs_precedes)},
1631 {"int_p_sep_by_space", STRUCT_OFFSET(struct lconv, int_p_sep_by_space)},
1632 {"int_n_cs_precedes", STRUCT_OFFSET(struct lconv, int_n_cs_precedes)},
1633 {"int_n_sep_by_space", STRUCT_OFFSET(struct lconv, int_n_sep_by_space)},
1634 {"int_p_sign_posn", STRUCT_OFFSET(struct lconv, int_p_sign_posn)},
1635 {"int_n_sign_posn", STRUCT_OFFSET(struct lconv, int_n_sign_posn)},
1641 #endif /* HAS_LOCALECONV */
1643 #ifdef HAS_LONG_DOUBLE
1644 # if LONG_DOUBLESIZE > NVSIZE
1645 # undef HAS_LONG_DOUBLE /* XXX until we figure out how to use them */
1649 #ifndef HAS_LONG_DOUBLE
1661 /* Background: in most systems the low byte of the wait status
1662 * is the signal (the lowest 7 bits) and the coredump flag is
1663 * the eight bit, and the second lowest byte is the exit status.
1664 * BeOS bucks the trend and has the bytes in different order.
1665 * See beos/beos.c for how the reality is bent even in BeOS
1666 * to follow the traditional. However, to make the POSIX
1667 * wait W*() macros to work in BeOS, we need to unbend the
1668 * reality back in place. --jhi */
1669 /* In actual fact the code below is to blame here. Perl has an internal
1670 * representation of the exit status ($?), which it re-composes from the
1671 * OS's representation using the W*() POSIX macros. The code below
1672 * incorrectly uses the W*() macros on the internal representation,
1673 * which fails for OSs that have a different representation (namely BeOS
1674 * and Haiku). WMUNGE() is a hack that converts the internal
1675 * representation into the OS specific one, so that the W*() macros work
1676 * as expected. The better solution would be not to use the W*() macros
1677 * in the first place, though. -- Ingo Weinhold
1679 #if defined(__HAIKU__)
1680 # define WMUNGE(x) (((x) & 0xFF00) >> 8 | ((x) & 0x00FF) << 8)
1682 # define WMUNGE(x) (x)
1686 not_here(const char *s)
1688 croak("POSIX::%s not implemented on this architecture", s);
1692 #include "const-c.inc"
1695 restore_sigmask(pTHX_ SV *osset_sv)
1697 /* Fortunately, restoring the signal mask can't fail, because
1698 * there's nothing we can do about it if it does -- we're not
1699 * supposed to return -1 from sigaction unless the disposition
1702 #if !(defined(__amigaos4__) && defined(__NEWLIB__))
1703 sigset_t *ossetp = (sigset_t *) SvPV_nolen( osset_sv );
1704 (void)sigprocmask(SIG_SETMASK, ossetp, (sigset_t *)0);
1709 allocate_struct(pTHX_ SV *rv, const STRLEN size, const char *packname) {
1710 SV *const t = newSVrv(rv, packname);
1711 void *const p = sv_grow(t, size + 1);
1713 /* Ensure at least one use of not_here() to avoid "defined but not
1714 * used" warning. This is not at all related to allocate_struct(); I
1715 * just needed somewhere to dump it - DAPM */
1716 if (0) { not_here(""); }
1726 * (1) The CRT maintains its own copy of the environment, separate from
1727 * the Win32API copy.
1729 * (2) CRT getenv() retrieves from this copy. CRT putenv() updates this
1730 * copy, and then calls SetEnvironmentVariableA() to update the Win32API
1733 * (3) win32_getenv() and win32_putenv() call GetEnvironmentVariableA() and
1734 * SetEnvironmentVariableA() directly, bypassing the CRT copy of the
1737 * (4) The CRT strftime() "%Z" implementation calls __tzset(). That
1738 * calls CRT tzset(), but only the first time it is called, and in turn
1739 * that uses CRT getenv("TZ") to retrieve the timezone info from the CRT
1740 * local copy of the environment and hence gets the original setting as
1741 * perl never updates the CRT copy when assigning to $ENV{TZ}.
1743 * Therefore, we need to retrieve the value of $ENV{TZ} and call CRT
1744 * putenv() to update the CRT copy of the environment (if it is different)
1745 * whenever we're about to call tzset().
1747 * In addition to all that, when perl is built with PERL_IMPLICIT_SYS
1750 * (a) Each interpreter has its own copy of the environment inside the
1751 * perlhost structure. That allows applications that host multiple
1752 * independent Perl interpreters to isolate environment changes from
1753 * each other. (This is similar to how the perlhost mechanism keeps a
1754 * separate working directory for each Perl interpreter, so that calling
1755 * chdir() will not affect other interpreters.)
1757 * (b) Only the first Perl interpreter instantiated within a process will
1758 * "write through" environment changes to the process environment.
1760 * (c) Even the primary Perl interpreter won't update the CRT copy of the
1761 * the environment, only the Win32API copy (it calls win32_putenv()).
1763 * As with CPerlHost::Getenv() and CPerlHost::Putenv() themselves, it makes
1764 * sense to only update the process environment when inside the main
1765 * interpreter, but we don't have access to CPerlHost's m_bTopLevel member
1766 * from here so we'll just have to check PL_curinterp instead.
1768 * Therefore, we can simply #undef getenv() and putenv() so that those names
1769 * always refer to the CRT functions, and explicitly call win32_getenv() to
1770 * access perl's %ENV.
1772 * We also #undef malloc() and free() to be sure we are using the CRT
1773 * functions otherwise under PERL_IMPLICIT_SYS they are redefined to calls
1774 * into VMem::Malloc() and VMem::Free() and all allocations will be freed
1775 * when the Perl interpreter is being destroyed so we'd end up with a pointer
1776 * into deallocated memory in environ[] if a program embedding a Perl
1777 * interpreter continues to operate even after the main Perl interpreter has
1780 * Note that we don't free() the malloc()ed memory unless and until we call
1781 * malloc() again ourselves because the CRT putenv() function simply puts its
1782 * pointer argument into the environ[] array (it doesn't make a copy of it)
1783 * so this memory must otherwise be leaked.
1792 fix_win32_tzenv(void)
1794 static char* oldenv = NULL;
1796 const char* perl_tz_env = win32_getenv("TZ");
1797 const char* crt_tz_env = getenv("TZ");
1798 if (perl_tz_env == NULL)
1800 if (crt_tz_env == NULL)
1802 if (strNE(perl_tz_env, crt_tz_env)) {
1803 newenv = (char*)malloc((strlen(perl_tz_env) + 4) * sizeof(char));
1804 if (newenv != NULL) {
1805 sprintf(newenv, "TZ=%s", perl_tz_env);
1817 * my_tzset - wrapper to tzset() with a fix to make it work (better) on Win32.
1818 * This code is duplicated in the Time-Piece module, so any changes made here
1819 * should be made there too.
1825 #if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
1826 if (PL_curinterp == aTHX)
1833 MODULE = SigSet PACKAGE = POSIX::SigSet PREFIX = sig
1836 new(packname = "POSIX::SigSet", ...)
1837 const char * packname
1842 = (sigset_t *) allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
1846 for (i = 1; i < items; i++)
1847 sigaddset(s, SvIV(ST(i)));
1853 POSIX::SigSet sigset
1858 RETVAL = ix ? sigdelset(sigset, sig) : sigaddset(sigset, sig);
1864 POSIX::SigSet sigset
1868 RETVAL = ix ? sigfillset(sigset) : sigemptyset(sigset);
1873 sigismember(sigset, sig)
1874 POSIX::SigSet sigset
1877 MODULE = Termios PACKAGE = POSIX::Termios PREFIX = cf
1880 new(packname = "POSIX::Termios", ...)
1881 const char * packname
1885 void *const p = allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
1886 sizeof(struct termios), packname);
1887 /* The previous implementation stored a pointer to an uninitialised
1888 struct termios. Seems safer to initialise it, particularly as
1889 this implementation exposes the struct to prying from perl-space.
1891 memset(p, 0, 1 + sizeof(struct termios));
1894 not_here("termios");
1899 getattr(termios_ref, fd = 0)
1900 POSIX::Termios termios_ref
1903 RETVAL = tcgetattr(fd, termios_ref);
1907 # If we define TCSANOW here then both a found and not found constant sub
1908 # are created causing a Constant subroutine TCSANOW redefined warning
1911 # define DEF_SETATTR_ACTION 0
1913 # define DEF_SETATTR_ACTION TCSANOW
1916 setattr(termios_ref, fd = 0, optional_actions = DEF_SETATTR_ACTION)
1917 POSIX::Termios termios_ref
1919 int optional_actions
1921 /* The second argument to the call is mandatory, but we'd like to give
1922 it a useful default. 0 isn't valid on all operating systems - on
1923 Solaris (at least) TCSANOW, TCSADRAIN and TCSAFLUSH have the same
1924 values as the equivalent ioctls, TCSETS, TCSETSW and TCSETSF. */
1925 if (optional_actions < 0) {
1926 SETERRNO(EINVAL, LIB_INVARG);
1929 RETVAL = tcsetattr(fd, optional_actions, termios_ref);
1935 getispeed(termios_ref)
1936 POSIX::Termios termios_ref
1940 RETVAL = ix ? cfgetospeed(termios_ref) : cfgetispeed(termios_ref);
1945 getiflag(termios_ref)
1946 POSIX::Termios termios_ref
1952 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1955 RETVAL = termios_ref->c_iflag;
1958 RETVAL = termios_ref->c_oflag;
1961 RETVAL = termios_ref->c_cflag;
1964 RETVAL = termios_ref->c_lflag;
1967 RETVAL = 0; /* silence compiler warning */
1970 not_here(GvNAME(CvGV(cv)));
1977 getcc(termios_ref, ccix)
1978 POSIX::Termios termios_ref
1981 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
1983 croak("Bad getcc subscript");
1984 RETVAL = termios_ref->c_cc[ccix];
1993 setispeed(termios_ref, speed)
1994 POSIX::Termios termios_ref
2000 ? cfsetospeed(termios_ref, speed) : cfsetispeed(termios_ref, speed);
2005 setiflag(termios_ref, flag)
2006 POSIX::Termios termios_ref
2013 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2016 termios_ref->c_iflag = flag;
2019 termios_ref->c_oflag = flag;
2022 termios_ref->c_cflag = flag;
2025 termios_ref->c_lflag = flag;
2029 not_here(GvNAME(CvGV(cv)));
2033 setcc(termios_ref, ccix, cc)
2034 POSIX::Termios termios_ref
2038 #ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
2040 croak("Bad setcc subscript");
2041 termios_ref->c_cc[ccix] = cc;
2047 MODULE = POSIX PACKAGE = POSIX
2049 INCLUDE: const-xs.inc
2055 POSIX::WIFEXITED = 1
2056 POSIX::WIFSIGNALED = 2
2057 POSIX::WIFSTOPPED = 3
2061 #if !defined(WEXITSTATUS) || !defined(WIFEXITED) || !defined(WIFSIGNALED) \
2062 || !defined(WIFSTOPPED) || !defined(WSTOPSIG) || !defined(WTERMSIG)
2063 RETVAL = 0; /* Silence compilers that notice this, but don't realise
2064 that not_here() can't return. */
2069 RETVAL = WEXITSTATUS(WMUNGE(status));
2071 not_here("WEXITSTATUS");
2076 RETVAL = WIFEXITED(WMUNGE(status));
2078 not_here("WIFEXITED");
2083 RETVAL = WIFSIGNALED(WMUNGE(status));
2085 not_here("WIFSIGNALED");
2090 RETVAL = WIFSTOPPED(WMUNGE(status));
2092 not_here("WIFSTOPPED");
2097 RETVAL = WSTOPSIG(WMUNGE(status));
2099 not_here("WSTOPSIG");
2104 RETVAL = WTERMSIG(WMUNGE(status));
2106 not_here("WTERMSIG");
2110 croak("Illegal alias %d for POSIX::W*", (int)ix);
2116 open(filename, flags = O_RDONLY, mode = 0666)
2121 if (flags & (O_APPEND|O_CREAT|O_TRUNC|O_RDWR|O_WRONLY|O_EXCL))
2122 TAINT_PROPER("open");
2123 RETVAL = open(filename, flags, mode);
2131 #ifndef HAS_LOCALECONV
2132 localeconv(); /* A stub to call not_here(). */
2134 struct lconv *lcbuf;
2135 # if defined(USE_ITHREADS) \
2136 && defined(HAS_POSIX_2008_LOCALE) \
2137 && defined(HAS_LOCALECONV_L) /* Prefer this thread-safe version */
2138 bool do_free = FALSE;
2139 locale_t cur = NULL;
2140 # elif defined(TS_W32_BROKEN_LOCALECONV)
2141 const char * save_global;
2142 const char * save_thread;
2144 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2146 /* localeconv() deals with both LC_NUMERIC and LC_MONETARY, but
2147 * LC_MONETARY is already in the correct locale */
2148 # ifdef USE_LOCALE_MONETARY
2150 const bool is_monetary_utf8 = _is_cur_LC_category_utf8(LC_MONETARY);
2152 # ifdef USE_LOCALE_NUMERIC
2154 bool is_numeric_utf8;
2156 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
2158 is_numeric_utf8 = _is_cur_LC_category_utf8(LC_NUMERIC);
2162 sv_2mortal((SV*)RETVAL);
2163 # if defined(USE_ITHREADS) \
2164 && defined(HAS_POSIX_2008_LOCALE) \
2165 && defined(HAS_LOCALECONV_L) \
2166 && defined(HAS_DUPLOCALE)
2168 cur = uselocale((locale_t) 0);
2169 if (cur == LC_GLOBAL_LOCALE) {
2170 cur = duplocale(LC_GLOBAL_LOCALE);
2174 lcbuf = localeconv_l(cur);
2176 LOCALE_LOCK_V; /* Prevent interference with other threads using
2178 # ifdef TS_W32_BROKEN_LOCALECONV
2179 /* This is a workaround for a Windows bug prior to VS 15, in which
2180 * localeconv only looks at the global locale. We toggle to the global
2181 * locale; populate the return; then toggle back. We have to use
2182 * LC_ALL instead of the individual ones because of another bug in
2185 save_thread = savepv(Perl_setlocale(LC_NUMERIC, NULL));
2187 _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
2189 save_global = savepv(Perl_setlocale(LC_ALL, NULL));
2191 Perl_setlocale(LC_ALL, save_thread);
2193 lcbuf = localeconv();
2196 const struct lconv_offset *strings = lconv_strings;
2197 const struct lconv_offset *integers = lconv_integers;
2198 const char *ptr = (const char *) lcbuf;
2200 while (strings->name) {
2201 /* This string may be controlled by either LC_NUMERIC, or
2203 const bool is_utf8_locale =
2204 # if defined(USE_LOCALE_NUMERIC) && defined(USE_LOCALE_MONETARY)
2205 (isLC_NUMERIC_STRING(strings->name))
2208 # elif defined(USE_LOCALE_NUMERIC)
2210 # elif defined(USE_LOCALE_MONETARY)
2216 const char *value = *((const char **)(ptr + strings->offset));
2218 if (value && *value) {
2219 const STRLEN value_len = strlen(value);
2221 /* We mark it as UTF-8 if a utf8 locale and is valid and
2222 * variant under UTF-8 */
2223 const bool is_utf8 = is_utf8_locale
2224 && is_utf8_non_invariant_string(
2227 (void) hv_store(RETVAL,
2229 strlen(strings->name),
2230 newSVpvn_utf8(value, value_len, is_utf8),
2236 while (integers->name) {
2237 const char value = *((const char *)(ptr + integers->offset));
2239 if (value != CHAR_MAX)
2240 (void) hv_store(RETVAL, integers->name,
2241 strlen(integers->name), newSViv(value), 0);
2245 # if defined(USE_ITHREADS) \
2246 && defined(HAS_POSIX_2008_LOCALE) \
2247 && defined(HAS_LOCALECONV_L)
2252 # ifdef TS_W32_BROKEN_LOCALECONV
2253 Perl_setlocale(LC_ALL, save_global);
2255 _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
2257 Perl_setlocale(LC_ALL, save_thread);
2259 Safefree(save_global);
2260 Safefree(save_thread);
2264 RESTORE_LC_NUMERIC();
2265 #endif /* HAS_LOCALECONV */
2270 setlocale(category, locale = 0)
2276 retval = (char *) Perl_setlocale(category, locale);
2328 RETVAL = Perl_acos(x); /* C89 math */
2332 RETVAL = c99_acosh(x);
2338 RETVAL = Perl_asin(x); /* C89 math */
2342 RETVAL = c99_asinh(x);
2348 RETVAL = Perl_atan(x); /* C89 math */
2352 RETVAL = c99_atanh(x);
2359 RETVAL = c99_cbrt(x);
2365 RETVAL = Perl_ceil(x); /* C89 math */
2368 RETVAL = Perl_cosh(x); /* C89 math */
2372 RETVAL = c99_erf(x);
2379 RETVAL = c99_erfc(x);
2386 RETVAL = c99_exp2(x);
2393 RETVAL = c99_expm1(x);
2399 RETVAL = Perl_floor(x); /* C89 math */
2403 RETVAL = bessel_j0(x);
2410 RETVAL = bessel_j1(x);
2416 /* XXX Note: the lgamma modifies a global variable (signgam),
2417 * which is evil. Some platforms have lgamma_r, which has
2418 * extra output parameter instead of the global variable. */
2420 RETVAL = c99_lgamma(x);
2426 RETVAL = Perl_log10(x); /* C89 math */
2430 RETVAL = c99_log1p(x);
2437 RETVAL = c99_log2(x);
2444 RETVAL = c99_logb(x);
2445 #elif defined(c99_log2) && FLT_RADIX == 2
2446 RETVAL = Perl_floor(c99_log2(PERL_ABS(x)));
2452 #ifdef c99_nearbyint
2453 RETVAL = c99_nearbyint(x);
2455 not_here("nearbyint");
2460 RETVAL = c99_rint(x);
2467 RETVAL = c99_round(x);
2473 RETVAL = Perl_sinh(x); /* C89 math */
2476 RETVAL = Perl_tan(x); /* C89 math */
2479 RETVAL = Perl_tanh(x); /* C89 math */
2483 RETVAL = c99_tgamma(x);
2490 RETVAL = c99_trunc(x);
2497 RETVAL = bessel_y0(x);
2505 RETVAL = bessel_y1(x);
2516 #ifdef HAS_FEGETROUND
2517 RETVAL = my_fegetround();
2520 not_here("fegetround");
2529 #ifdef HAS_FEGETROUND /* canary for fesetround */
2530 RETVAL = fesetround(x);
2531 #elif defined(HAS_FPGETROUND) /* canary for fpsetround */
2533 case FE_TONEAREST: RETVAL = fpsetround(FP_RN); break;
2534 case FE_TOWARDZERO: RETVAL = fpsetround(FP_RZ); break;
2535 case FE_DOWNWARD: RETVAL = fpsetround(FP_RM); break;
2536 case FE_UPWARD: RETVAL = fpsetround(FP_RP); break;
2537 default: RETVAL = -1; break;
2539 #elif defined(__osf__) /* Tru64 */
2541 case FE_TONEAREST: RETVAL = write_rnd(FP_RND_RN); break;
2542 case FE_TOWARDZERO: RETVAL = write_rnd(FP_RND_RZ); break;
2543 case FE_DOWNWARD: RETVAL = write_rnd(FP_RND_RM); break;
2544 case FE_UPWARD: RETVAL = write_rnd(FP_RND_RP); break;
2545 default: RETVAL = -1; break;
2550 not_here("fesetround");
2572 #ifdef c99_fpclassify
2573 RETVAL = c99_fpclassify(x);
2575 not_here("fpclassify");
2580 RETVAL = c99_ilogb(x);
2586 RETVAL = Perl_isfinite(x);
2589 RETVAL = Perl_isinf(x);
2592 RETVAL = Perl_isnan(x);
2596 RETVAL = c99_isnormal(x);
2598 not_here("isnormal");
2603 RETVAL = c99_lrint(x);
2610 RETVAL = c99_lround(x);
2618 RETVAL = Perl_signbit(x);
2621 #ifdef DOUBLE_IS_IEEE_FORMAT
2636 #ifdef DOUBLE_HAS_NAN
2637 RETVAL = S_getpayload(nv);
2639 PERL_UNUSED_VAR(nv);
2641 not_here("getpayload");
2647 setpayload(nv, payload)
2651 #ifdef DOUBLE_HAS_NAN
2652 S_setpayload(&nv, payload, FALSE);
2654 PERL_UNUSED_VAR(nv);
2655 PERL_UNUSED_VAR(payload);
2656 not_here("setpayload");
2662 setpayloadsig(nv, payload)
2666 #ifdef DOUBLE_HAS_NAN
2668 S_setpayload(&nv, payload, TRUE);
2670 PERL_UNUSED_VAR(nv);
2671 PERL_UNUSED_VAR(payload);
2672 not_here("setpayloadsig");
2681 #ifdef DOUBLE_HAS_NAN
2682 RETVAL = Perl_isnan(nv) && NV_NAN_IS_SIGNALING(&nv);
2684 PERL_UNUSED_VAR(nv);
2686 not_here("issignaling");
2721 RETVAL = c99_copysign(x, y);
2723 not_here("copysign");
2728 RETVAL = c99_fdim(x, y);
2735 RETVAL = c99_fmax(x, y);
2742 RETVAL = c99_fmin(x, y);
2748 RETVAL = Perl_fmod(x, y); /* C89 math */
2752 RETVAL = c99_hypot(x, y);
2758 #ifdef c99_isgreater
2759 RETVAL = c99_isgreater(x, y);
2761 not_here("isgreater");
2765 #ifdef c99_isgreaterequal
2766 RETVAL = c99_isgreaterequal(x, y);
2768 not_here("isgreaterequal");
2773 RETVAL = c99_isless(x, y);
2779 #ifdef c99_islessequal
2780 RETVAL = c99_islessequal(x, y);
2782 not_here("islessequal");
2786 #ifdef c99_islessgreater
2787 RETVAL = c99_islessgreater(x, y);
2789 not_here("islessgreater");
2793 #ifdef c99_isunordered
2794 RETVAL = c99_isunordered(x, y);
2796 not_here("isunordered");
2800 #ifdef c99_nextafter
2801 RETVAL = c99_nextafter(x, y);
2803 not_here("nextafter");
2807 #ifdef c99_nexttoward
2808 RETVAL = c99_nexttoward(x, y);
2810 not_here("nexttoward");
2815 #ifdef c99_remainder
2816 RETVAL = c99_remainder(x, y);
2818 not_here("remainder");
2830 /* (We already know stack is long enough.) */
2831 PUSHs(sv_2mortal(newSVnv(Perl_frexp(x,&expvar)))); /* C89 math */
2832 PUSHs(sv_2mortal(newSViv(expvar)));
2839 RETVAL = Perl_ldexp(x, exp);
2848 /* (We already know stack is long enough.) */
2849 PUSHs(sv_2mortal(newSVnv(Perl_modf(x,&intvar)))); /* C89 math */
2850 PUSHs(sv_2mortal(newSVnv(intvar)));
2859 PUSHs(sv_2mortal(newSVnv(c99_remquo(x,y,&intvar))));
2860 PUSHs(sv_2mortal(newSVnv(intvar)));
2873 RETVAL = c99_scalbn(x, y);
2890 RETVAL = c99_fma(x, y, z);
2905 /* If no payload given, just return the default NaN.
2906 * This makes a difference in platforms where the default
2907 * NaN is not all zeros. */
2911 S_setpayload(&RETVAL, payload, FALSE);
2913 #elif defined(c99_nan)
2915 STRLEN elen = my_snprintf(PL_efloatbuf, PL_efloatsize, "%g", payload);
2916 if ((IV)elen == -1) {
2924 RETVAL = c99_nan(PL_efloatbuf);
2948 RETVAL = bessel_jn(x, y);
2958 RETVAL = bessel_yn(x, y);
2970 sigaction(sig, optaction, oldaction = 0)
2973 POSIX::SigAction oldaction
2975 #if defined(WIN32) || defined(NETWARE) || (defined(__amigaos4__) && defined(__NEWLIB__))
2976 RETVAL = not_here("sigaction");
2978 # This code is really grody because we are trying to make the signal
2979 # interface look beautiful, which is hard.
2983 POSIX__SigAction action;
2984 GV *siggv = gv_fetchpvs("SIG", GV_ADD, SVt_PVHV);
2985 struct sigaction act;
2986 struct sigaction oact;
2990 POSIX__SigSet sigset;
2995 croak("Negative signals are not allowed");
2998 if (sig == 0 && SvPOK(ST(0))) {
2999 const char *s = SvPVX_const(ST(0));
3000 int i = whichsig(s);
3002 if (i < 0 && memBEGINs(s, SvCUR(ST(0)), "SIG"))
3003 i = whichsig(s + 3);
3005 if (ckWARN(WARN_SIGNAL))
3006 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
3007 "No such signal: SIG%s", s);
3014 if (sig > NSIG) { /* NSIG - 1 is still okay. */
3015 Perl_warner(aTHX_ packWARN(WARN_SIGNAL),
3016 "No such signal: %d", sig);
3020 sigsvp = hv_fetch(GvHVn(siggv),
3022 strlen(PL_sig_name[sig]),
3025 /* Check optaction and set action */
3026 if(SvTRUE(optaction)) {
3027 if(sv_isa(optaction, "POSIX::SigAction"))
3028 action = (HV*)SvRV(optaction);
3030 croak("action is not of type POSIX::SigAction");
3036 /* sigaction() is supposed to look atomic. In particular, any
3037 * signal handler invoked during a sigaction() call should
3038 * see either the old or the new disposition, and not something
3039 * in between. We use sigprocmask() to make it so.
3042 RETVAL=sigprocmask(SIG_BLOCK, &sset, &osset);
3046 /* Restore signal mask no matter how we exit this block. */
3047 osset_sv = newSVpvn((char *)(&osset), sizeof(sigset_t));
3048 SAVEFREESV( osset_sv );
3049 SAVEDESTRUCTOR_X(restore_sigmask, osset_sv);
3051 RETVAL=-1; /* In case both oldaction and action are 0. */
3053 /* Remember old disposition if desired. */
3055 svp = hv_fetchs(oldaction, "HANDLER", TRUE);
3057 croak("Can't supply an oldaction without a HANDLER");
3058 if(SvTRUE(*sigsvp)) { /* TBD: what if "0"? */
3059 sv_setsv(*svp, *sigsvp);
3062 sv_setpvs(*svp, "DEFAULT");
3064 RETVAL = sigaction(sig, (struct sigaction *)0, & oact);
3069 /* Get back the mask. */
3070 svp = hv_fetchs(oldaction, "MASK", TRUE);
3071 if (sv_isa(*svp, "POSIX::SigSet")) {
3072 sigset = (sigset_t *) SvPV_nolen(SvRV(*svp));
3075 sigset = (sigset_t *) allocate_struct(aTHX_ *svp,
3079 *sigset = oact.sa_mask;
3081 /* Get back the flags. */
3082 svp = hv_fetchs(oldaction, "FLAGS", TRUE);
3083 sv_setiv(*svp, oact.sa_flags);
3085 /* Get back whether the old handler used safe signals. */
3086 svp = hv_fetchs(oldaction, "SAFE", TRUE);
3088 /* compare incompatible pointers by casting to integer */
3089 PTR2nat(oact.sa_handler) == PTR2nat(PL_csighandlerp));
3093 /* Safe signals use "csighandler", which vectors through the
3094 PL_sighandlerp pointer when it's safe to do so.
3095 (BTW, "csighandler" is very different from "sighandler".) */
3096 svp = hv_fetchs(action, "SAFE", FALSE);
3100 (*svp && SvTRUE(*svp))
3101 ? PL_csighandlerp : PL_sighandlerp
3104 /* Vector new Perl handler through %SIG.
3105 (The core signal handlers read %SIG to dispatch.) */
3106 svp = hv_fetchs(action, "HANDLER", FALSE);
3108 croak("Can't supply an action without a HANDLER");
3109 sv_setsv(*sigsvp, *svp);
3111 /* This call actually calls sigaction() with almost the
3112 right settings, including appropriate interpretation
3113 of DEFAULT and IGNORE. However, why are we doing
3114 this when we're about to do it again just below? XXX */
3115 SvSETMAGIC(*sigsvp);
3117 /* And here again we duplicate -- DEFAULT/IGNORE checking. */
3119 const char *s=SvPVX_const(*svp);
3120 if(strEQ(s,"IGNORE")) {
3121 act.sa_handler = SIG_IGN;
3123 else if(strEQ(s,"DEFAULT")) {
3124 act.sa_handler = SIG_DFL;
3128 /* Set up any desired mask. */
3129 svp = hv_fetchs(action, "MASK", FALSE);
3130 if (svp && sv_isa(*svp, "POSIX::SigSet")) {
3131 sigset = (sigset_t *) SvPV_nolen(SvRV(*svp));
3132 act.sa_mask = *sigset;
3135 sigemptyset(& act.sa_mask);
3137 /* Set up any desired flags. */
3138 svp = hv_fetchs(action, "FLAGS", FALSE);
3139 act.sa_flags = svp ? SvIV(*svp) : 0;
3141 /* Don't worry about cleaning up *sigsvp if this fails,
3142 * because that means we tried to disposition a
3143 * nonblockable signal, in which case *sigsvp is
3144 * essentially meaningless anyway.
3146 RETVAL = sigaction(sig, & act, (struct sigaction *)0);
3161 POSIX::SigSet sigset
3166 RETVAL = not_here("sigpending");
3168 RETVAL = ix ? sigsuspend(sigset) : sigpending(sigset);
3176 sigprocmask(how, sigset, oldsigset = 0)
3178 POSIX::SigSet sigset = NO_INIT
3179 POSIX::SigSet oldsigset = NO_INIT
3181 if (! SvOK(ST(1))) {
3183 } else if (sv_isa(ST(1), "POSIX::SigSet")) {
3184 sigset = (sigset_t *) SvPV_nolen(SvRV(ST(1)));
3186 croak("sigset is not of type POSIX::SigSet");
3189 if (items < 3 || ! SvOK(ST(2))) {
3191 } else if (sv_isa(ST(2), "POSIX::SigSet")) {
3192 oldsigset = (sigset_t *) SvPV_nolen(SvRV(ST(2)));
3194 croak("oldsigset is not of type POSIX::SigSet");
3206 if (fd1 >= 0 && fd2 >= 0) {
3208 /* RT #98912 - More Microsoft muppetry - failing to
3209 actually implemented the well known documented POSIX
3210 behaviour for a POSIX API.
3211 http://msdn.microsoft.com/en-us/library/8syseb29.aspx */
3212 RETVAL = dup2(fd1, fd2) == -1 ? -1 : fd2;
3214 RETVAL = dup2(fd1, fd2);
3217 SETERRNO(EBADF,RMS_IFI);
3224 lseek(fd, offset, whence)
3230 Off_t pos = PerlLIO_lseek(fd, offset, whence);
3231 RETVAL = sizeof(Off_t) > sizeof(IV)
3232 ? newSVnv((NV)pos) : newSViv((IV)pos);
3242 if ((incr = nice(incr)) != -1 || errno == 0) {
3244 XPUSHs(newSVpvs_flags("0 but true", SVs_TEMP));
3246 XPUSHs(sv_2mortal(newSViv(incr)));
3253 if (pipe(fds) != -1) {
3255 PUSHs(sv_2mortal(newSViv(fds[0])));
3256 PUSHs(sv_2mortal(newSViv(fds[1])));
3260 read(fd, buffer, nbytes)
3262 SV *sv_buffer = SvROK(ST(1)) ? SvRV(ST(1)) : ST(1);
3266 char * buffer = sv_grow( sv_buffer, nbytes+1 );
3269 SvCUR_set(sv_buffer, RETVAL);
3270 SvPOK_only(sv_buffer);
3271 *SvEND(sv_buffer) = '\0';
3272 SvTAINTED_on(sv_buffer);
3288 tcsetpgrp(fd, pgrp_id)
3297 if (uname(&buf) >= 0) {
3299 PUSHs(newSVpvn_flags(buf.sysname, strlen(buf.sysname), SVs_TEMP));
3300 PUSHs(newSVpvn_flags(buf.nodename, strlen(buf.nodename), SVs_TEMP));
3301 PUSHs(newSVpvn_flags(buf.release, strlen(buf.release), SVs_TEMP));
3302 PUSHs(newSVpvn_flags(buf.version, strlen(buf.version), SVs_TEMP));
3303 PUSHs(newSVpvn_flags(buf.machine, strlen(buf.machine), SVs_TEMP));
3306 uname((char *) 0); /* A stub to call not_here(). */
3310 write(fd, buffer, nbytes)
3327 #if defined(USE_ITHREADS) && defined(HAS_MBRLEN)
3331 #if defined(USE_ITHREADS) && defined(HAS_MBRLEN)
3332 PERL_UNUSED_RESULT(mbrlen(NULL, 0, &ps)); /* Initialize state */
3333 RETVAL = mbrlen(s, n, &ps); /* Prefer reentrant version */
3335 RETVAL = mblen(s, n);
3341 mbstowcs(s, pwcs, n)
3352 #if defined(USE_ITHREADS) && defined(HAS_MBRTOWC)
3356 #if defined(USE_ITHREADS) && defined(HAS_MBRTOWC)
3357 memset(&ps, 0, sizeof(ps));;
3358 PERL_UNUSED_RESULT(mbrtowc(pwc, NULL, 0, &ps));/* Reset any shift state */
3360 RETVAL = mbrtowc(pwc, s, n, &ps); /* Prefer reentrant version */
3362 RETVAL = mbtowc(pwc, s, n);
3368 wcstombs(s, pwcs, n)
3390 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3391 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
3392 num = strtod(str, &unparsed);
3393 RESTORE_LC_NUMERIC();
3394 PUSHs(sv_2mortal(newSVnv(num)));
3395 if (GIMME_V == G_ARRAY) {
3398 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3400 PUSHs(&PL_sv_undef);
3412 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3413 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
3414 num = strtold(str, &unparsed);
3415 RESTORE_LC_NUMERIC();
3416 PUSHs(sv_2mortal(newSVnv(num)));
3417 if (GIMME_V == G_ARRAY) {
3420 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3422 PUSHs(&PL_sv_undef);
3428 strtol(str, base = 0)
3435 if (base == 0 || (base >= 2 && base <= 36)) {
3436 num = strtol(str, &unparsed, base);
3437 #if IVSIZE < LONGSIZE
3438 if (num < IV_MIN || num > IV_MAX)
3439 PUSHs(sv_2mortal(newSVnv((double)num)));
3442 PUSHs(sv_2mortal(newSViv((IV)num)));
3443 if (GIMME_V == G_ARRAY) {
3446 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3448 PUSHs(&PL_sv_undef);
3451 SETERRNO(EINVAL, LIB_INVARG);
3452 PUSHs(&PL_sv_undef);
3453 if (GIMME_V == G_ARRAY) {
3455 PUSHs(&PL_sv_undef);
3460 strtoul(str, base = 0)
3465 char *unparsed = NULL;
3467 PERL_UNUSED_VAR(str);
3468 PERL_UNUSED_VAR(base);
3469 if (base == 0 || (base >= 2 && base <= 36)) {
3470 num = strtoul(str, &unparsed, base);
3471 #if IVSIZE <= LONGSIZE
3473 PUSHs(sv_2mortal(newSVnv((double)num)));
3476 PUSHs(sv_2mortal(newSViv((IV)num)));
3477 if (GIMME_V == G_ARRAY) {
3480 PUSHs(sv_2mortal(newSViv(strlen(unparsed))));
3482 PUSHs(&PL_sv_undef);
3485 SETERRNO(EINVAL, LIB_INVARG);
3486 PUSHs(&PL_sv_undef);
3487 if (GIMME_V == G_ARRAY) {
3489 PUSHs(&PL_sv_undef);
3501 char *p = SvPV(src,srclen);
3503 buflen = srclen * 4 + 1;
3504 ST(0) = sv_2mortal(newSV(buflen));
3505 dstlen = strxfrm(SvPVX(ST(0)), p, (size_t)buflen);
3506 if (dstlen >= buflen) {
3508 SvGROW(ST(0), dstlen);
3509 strxfrm(SvPVX(ST(0)), p, (size_t)dstlen);
3512 SvCUR_set(ST(0), dstlen);
3517 mkfifo(filename, mode)
3524 RETVAL = access(filename, mode);
3526 TAINT_PROPER("mkfifo");
3527 RETVAL = mkfifo(filename, mode);
3540 RETVAL = ix == 1 ? close(fd)
3541 : (ix < 1 ? tcdrain(fd) : dup(fd));
3543 SETERRNO(EBADF,RMS_IFI);
3559 RETVAL = ix == 1 ? tcflush(fd, action)
3560 : (ix < 1 ? tcflow(fd, action) : tcsendbreak(fd, action));
3562 SETERRNO(EINVAL,LIB_INVARG);
3569 asctime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = -1)
3585 init_tm(&mytm); /* XXX workaround - see init_tm() in core util.c */
3588 mytm.tm_hour = hour;
3589 mytm.tm_mday = mday;
3591 mytm.tm_year = year;
3592 mytm.tm_wday = wday;
3593 mytm.tm_yday = yday;
3594 mytm.tm_isdst = isdst;
3596 const time_t result = mktime(&mytm);
3597 if (result == (time_t)-1)
3599 else if (result == 0)
3600 sv_setpvs(TARG, "0 but true");
3602 sv_setiv(TARG, (IV)result);
3604 sv_setpv(TARG, asctime(&mytm));
3622 realtime = times( &tms );
3624 PUSHs( sv_2mortal( newSViv( (IV) realtime ) ) );
3625 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_utime ) ) );
3626 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_stime ) ) );
3627 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cutime ) ) );
3628 PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cstime ) ) );
3631 difftime(time1, time2)
3635 #XXX: if $xsubpp::WantOptimize is always the default
3636 # sv_setpv(TARG, ...) could be used rather than
3637 # ST(0) = sv_2mortal(newSVpv(...))
3639 strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
3655 /* allowing user-supplied (rather than literal) formats
3656 * is normally frowned upon as a potential security risk;
3657 * but this is part of the API so we have to allow it */
3658 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
3659 buf = my_strftime(SvPV_nolen(fmt), sec, min, hour, mday, mon, year, wday, yday, isdst);
3660 GCC_DIAG_RESTORE_STMT;
3661 sv = sv_newmortal();
3663 STRLEN len = strlen(buf);
3664 sv_usepvn_flags(sv, buf, len, SV_HAS_TRAILING_NUL);
3666 || ( is_utf8_non_invariant_string((U8*) buf, len)
3667 #ifdef USE_LOCALE_TIME
3668 && _is_cur_LC_category_utf8(LC_TIME)
3669 #else /* If can't check directly, at least can see if script is consistent,
3670 under UTF-8, which gives us an extra measure of confidence. */
3672 && isSCRIPT_RUN((const U8 *) buf,
3673 (const U8 *) buf + len,
3674 TRUE) /* Means assume UTF-8 */
3680 else { /* We can't distinguish between errors and just an empty
3681 * return; in all cases just return an empty string */
3682 SvUPGRADE(sv, SVt_PV);
3683 SvPV_set(sv, (char *) "");
3686 SvLEN_set(sv, 0); /* Won't attempt to free the string when sv
3701 PUSHs(newSVpvn_flags(tzname[0], strlen(tzname[0]), SVs_TEMP));
3702 PUSHs(newSVpvn_flags(tzname[1], strlen(tzname[1]), SVs_TEMP));
3708 #ifdef HAS_CTERMID_R
3709 s = (char *) safemalloc((size_t) L_ctermid);
3711 RETVAL = ctermid(s);
3715 #ifdef HAS_CTERMID_R
3724 RETVAL = cuserid(s);
3728 not_here("cuserid");
3739 pathconf(filename, name)
3750 unsigned int seconds
3752 RETVAL = PerlProc_sleep(seconds);
3778 XSprePUSH; PUSHTARG;
3782 lchown(uid, gid, path)
3788 /* yes, the order of arguments is different,
3789 * but consistent with CORE::chown() */
3790 RETVAL = lchown(path, uid, gid);
3792 PERL_UNUSED_VAR(uid);
3793 PERL_UNUSED_VAR(gid);
3794 PERL_UNUSED_VAR(path);
3795 RETVAL = not_here("lchown");