Test changes
[p5sagit/p5-mst-13.2.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a687059c 4 *
d48672a2 5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8 7 *
8d063cd8 8 */
a0d0e21e 9
10/*
11 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
12 * not content." --Gandalf
13 */
8d063cd8 14
8d063cd8 15#include "EXTERN.h"
8d063cd8 16#include "perl.h"
62b28dd9 17
6eb13c3b 18#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
a687059c 19#include <signal.h>
62b28dd9 20#endif
a687059c 21
36477c24 22#ifndef SIG_ERR
23# define SIG_ERR ((Sighandler_t) -1)
24#endif
25
bd4080b3 26/* XXX If this causes problems, set i_unistd=undef in the hint file. */
85e6fe83 27#ifdef I_UNISTD
8990e307 28# include <unistd.h>
29#endif
30
a687059c 31#ifdef I_VFORK
32# include <vfork.h>
33#endif
34
94b6baf5 35/* Put this after #includes because fork and vfork prototypes may
36 conflict.
37*/
38#ifndef HAS_VFORK
39# define vfork fork
40#endif
41
fe14fcc3 42#ifdef I_FCNTL
43# include <fcntl.h>
44#endif
45#ifdef I_SYS_FILE
46# include <sys/file.h>
47#endif
48
ff68c719 49#ifdef I_SYS_WAIT
50# include <sys/wait.h>
51#endif
52
8d063cd8 53#define FLUSH
8d063cd8 54
a0d0e21e 55#ifdef LEAKTEST
56static void xstat _((void));
57#endif
58
55497cff 59#ifndef MYMALLOC
de3bb511 60
8d063cd8 61/* paranoid version of malloc */
62
a687059c 63/* NOTE: Do not call the next three routines directly. Use the macros
64 * in handy.h, so that we can easily redefine everything to do tracking of
65 * allocated hunks back to the original New to track down any memory leaks.
20cec16a 66 * XXX This advice seems to be widely ignored :-( --AD August 1996.
a687059c 67 */
68
bd4080b3 69Malloc_t
f0f333f4 70safemalloc(MEM_SIZE size)
8d063cd8 71{
bd4080b3 72 Malloc_t ptr;
55497cff 73#ifdef HAS_64K_LIMIT
62b28dd9 74 if (size > 0xffff) {
760ac839 75 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
79072805 76 my_exit(1);
62b28dd9 77 }
55497cff 78#endif /* HAS_64K_LIMIT */
34de22dd 79#ifdef DEBUGGING
80 if ((long)size < 0)
463ee0b2 81 croak("panic: malloc");
34de22dd 82#endif
8d063cd8 83 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
79072805 84#if !(defined(I286) || defined(atarist))
760ac839 85 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
79072805 86#else
760ac839 87 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
8d063cd8 88#endif
89 if (ptr != Nullch)
90 return ptr;
7c0587c8 91 else if (nomemok)
92 return Nullch;
8d063cd8 93 else {
760ac839 94 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
79072805 95 my_exit(1);
8d063cd8 96 }
97 /*NOTREACHED*/
98}
99
100/* paranoid version of realloc */
101
bd4080b3 102Malloc_t
f0f333f4 103saferealloc(Malloc_t where,MEM_SIZE size)
8d063cd8 104{
bd4080b3 105 Malloc_t ptr;
ecfc5424 106#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
bd4080b3 107 Malloc_t realloc();
ecfc5424 108#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
8d063cd8 109
55497cff 110#ifdef HAS_64K_LIMIT
5f05dabc 111 if (size > 0xffff) {
112 PerlIO_printf(PerlIO_stderr(),
113 "Reallocation too large: %lx\n", size) FLUSH;
114 my_exit(1);
115 }
55497cff 116#endif /* HAS_64K_LIMIT */
378cc40b 117 if (!where)
463ee0b2 118 croak("Null realloc");
34de22dd 119#ifdef DEBUGGING
120 if ((long)size < 0)
463ee0b2 121 croak("panic: realloc");
34de22dd 122#endif
8d063cd8 123 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
79072805 124
125#if !(defined(I286) || defined(atarist))
126 DEBUG_m( {
760ac839 127 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
128 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
79072805 129 } )
130#else
131 DEBUG_m( {
760ac839 132 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
133 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
79072805 134 } )
8d063cd8 135#endif
79072805 136
8d063cd8 137 if (ptr != Nullch)
138 return ptr;
7c0587c8 139 else if (nomemok)
140 return Nullch;
8d063cd8 141 else {
760ac839 142 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
79072805 143 my_exit(1);
8d063cd8 144 }
145 /*NOTREACHED*/
146}
147
148/* safe version of free */
149
54310121 150Free_t
f0f333f4 151safefree(Malloc_t where)
8d063cd8 152{
79072805 153#if !(defined(I286) || defined(atarist))
f0f333f4 154 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
79072805 155#else
f0f333f4 156 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
8d063cd8 157#endif
378cc40b 158 if (where) {
de3bb511 159 /*SUPPRESS 701*/
378cc40b 160 free(where);
161 }
8d063cd8 162}
163
1050c9ca 164/* safe version of calloc */
165
bd4080b3 166Malloc_t
f0f333f4 167safecalloc(MEM_SIZE count, MEM_SIZE size)
1050c9ca 168{
bd4080b3 169 Malloc_t ptr;
1050c9ca 170
55497cff 171#ifdef HAS_64K_LIMIT
5f05dabc 172 if (size * count > 0xffff) {
173 PerlIO_printf(PerlIO_stderr(),
174 "Allocation too large: %lx\n", size * count) FLUSH;
175 my_exit(1);
176 }
55497cff 177#endif /* HAS_64K_LIMIT */
1050c9ca 178#ifdef DEBUGGING
179 if ((long)size < 0 || (long)count < 0)
180 croak("panic: calloc");
181#endif
0b7c1c42 182 size *= count;
183 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
1050c9ca 184#if !(defined(I286) || defined(atarist))
fb73857a 185 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
1050c9ca 186#else
fb73857a 187 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
1050c9ca 188#endif
1050c9ca 189 if (ptr != Nullch) {
190 memset((void*)ptr, 0, size);
191 return ptr;
192 }
193 else if (nomemok)
194 return Nullch;
195 else {
760ac839 196 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
1050c9ca 197 my_exit(1);
198 }
199 /*NOTREACHED*/
200}
201
55497cff 202#endif /* !MYMALLOC */
de3bb511 203
a687059c 204#ifdef LEAKTEST
205
206#define ALIGN sizeof(long)
8d063cd8 207
bd4080b3 208Malloc_t
f0f333f4 209safexmalloc(I32 x, MEM_SIZE size)
8d063cd8 210{
bd4080b3 211 register Malloc_t where;
8d063cd8 212
a687059c 213 where = safemalloc(size + ALIGN);
214 xcount[x]++;
215 where[0] = x % 100;
216 where[1] = x / 100;
217 return where + ALIGN;
8d063cd8 218}
8d063cd8 219
bd4080b3 220Malloc_t
f0f333f4 221safexrealloc(Malloc_t where, MEM_SIZE size)
a687059c 222{
bd4080b3 223 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
a0d0e21e 224 return new + ALIGN;
a687059c 225}
226
227void
f0f333f4 228safexfree(Malloc_t where)
a687059c 229{
79072805 230 I32 x;
a687059c 231
232 if (!where)
233 return;
234 where -= ALIGN;
235 x = where[0] + 100 * where[1];
236 xcount[x]--;
237 safefree(where);
238}
239
bd4080b3 240Malloc_t
f0f333f4 241safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
1050c9ca 242{
bd4080b3 243 register Malloc_t where;
1050c9ca 244
245 where = safexmalloc(x, size * count + ALIGN);
246 xcount[x]++;
247 memset((void*)where + ALIGN, 0, size * count);
248 where[0] = x % 100;
249 where[1] = x / 100;
250 return where + ALIGN;
251}
252
7c0587c8 253static void
f0f333f4 254xstat(void)
8d063cd8 255{
79072805 256 register I32 i;
8d063cd8 257
a687059c 258 for (i = 0; i < MAXXCOUNT; i++) {
de3bb511 259 if (xcount[i] > lastxcount[i]) {
760ac839 260 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
a687059c 261 lastxcount[i] = xcount[i];
8d063cd8 262 }
263 }
8d063cd8 264}
a687059c 265
266#endif /* LEAKTEST */
8d063cd8 267
268/* copy a string up to some (non-backslashed) delimiter, if any */
269
270char *
8ac85365 271delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
8d063cd8 272{
fc36a67e 273 register I32 tolen;
274 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b 275 if (*from == '\\') {
276 if (from[1] == delim)
277 from++;
fc36a67e 278 else {
279 if (to < toend)
280 *to++ = *from;
281 tolen++;
282 from++;
283 }
378cc40b 284 }
bedebaa5 285 else if (*from == delim)
8d063cd8 286 break;
fc36a67e 287 if (to < toend)
288 *to++ = *from;
8d063cd8 289 }
bedebaa5 290 if (to < toend)
291 *to = '\0';
fc36a67e 292 *retlen = tolen;
8d063cd8 293 return from;
294}
295
296/* return ptr to little string in big string, NULL if not found */
378cc40b 297/* This routine was donated by Corey Satten. */
8d063cd8 298
299char *
8ac85365 300instr(register char *big, register char *little)
378cc40b 301{
302 register char *s, *x;
79072805 303 register I32 first;
378cc40b 304
a687059c 305 if (!little)
306 return big;
307 first = *little++;
378cc40b 308 if (!first)
309 return big;
310 while (*big) {
311 if (*big++ != first)
312 continue;
313 for (x=big,s=little; *s; /**/ ) {
314 if (!*x)
315 return Nullch;
316 if (*s++ != *x++) {
317 s--;
318 break;
319 }
320 }
321 if (!*s)
322 return big-1;
323 }
324 return Nullch;
325}
8d063cd8 326
a687059c 327/* same as instr but allow embedded nulls */
328
329char *
8ac85365 330ninstr(register char *big, register char *bigend, char *little, char *lend)
8d063cd8 331{
a687059c 332 register char *s, *x;
79072805 333 register I32 first = *little;
a687059c 334 register char *littleend = lend;
378cc40b 335
a0d0e21e 336 if (!first && little >= littleend)
a687059c 337 return big;
de3bb511 338 if (bigend - big < littleend - little)
339 return Nullch;
a687059c 340 bigend -= littleend - little++;
341 while (big <= bigend) {
342 if (*big++ != first)
343 continue;
344 for (x=big,s=little; s < littleend; /**/ ) {
345 if (*s++ != *x++) {
346 s--;
347 break;
348 }
349 }
350 if (s >= littleend)
351 return big-1;
378cc40b 352 }
a687059c 353 return Nullch;
354}
355
356/* reverse of the above--find last substring */
357
358char *
8ac85365 359rninstr(register char *big, char *bigend, char *little, char *lend)
a687059c 360{
361 register char *bigbeg;
362 register char *s, *x;
79072805 363 register I32 first = *little;
a687059c 364 register char *littleend = lend;
365
a0d0e21e 366 if (!first && little >= littleend)
a687059c 367 return bigend;
368 bigbeg = big;
369 big = bigend - (littleend - little++);
370 while (big >= bigbeg) {
371 if (*big-- != first)
372 continue;
373 for (x=big+2,s=little; s < littleend; /**/ ) {
374 if (*s++ != *x++) {
375 s--;
376 break;
377 }
378 }
379 if (s >= littleend)
380 return big+1;
378cc40b 381 }
a687059c 382 return Nullch;
378cc40b 383}
a687059c 384
bbce6d69 385/*
386 * Set up for a new ctype locale.
387 */
55497cff 388void
8ac85365 389perl_new_ctype(char *newctype)
ef7eada9 390{
36477c24 391#ifdef USE_LOCALE_CTYPE
392
bbce6d69 393 int i;
ef7eada9 394
bbce6d69 395 for (i = 0; i < 256; i++) {
396 if (isUPPER_LC(i))
397 fold_locale[i] = toLOWER_LC(i);
398 else if (isLOWER_LC(i))
399 fold_locale[i] = toUPPER_LC(i);
400 else
401 fold_locale[i] = i;
402 }
bbce6d69 403
36477c24 404#endif /* USE_LOCALE_CTYPE */
405}
bbce6d69 406
407/*
408 * Set up for a new collation locale.
409 */
410void
8ac85365 411perl_new_collate(char *newcoll)
bbce6d69 412{
36477c24 413#ifdef USE_LOCALE_COLLATE
414
bbce6d69 415 if (! newcoll) {
416 if (collation_name) {
417 ++collation_ix;
418 Safefree(collation_name);
419 collation_name = NULL;
420 collation_standard = TRUE;
bbce6d69 421 collxfrm_base = 0;
422 collxfrm_mult = 2;
bbce6d69 423 }
424 return;
425 }
426
427 if (! collation_name || strNE(collation_name, newcoll)) {
428 ++collation_ix;
429 Safefree(collation_name);
430 collation_name = savepv(newcoll);
ff68c719 431 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
bbce6d69 432
bbce6d69 433 {
434 /* 2: at most so many chars ('a', 'b'). */
435 /* 50: surely no system expands a char more. */
436#define XFRMBUFSIZE (2 * 50)
437 char xbuf[XFRMBUFSIZE];
438 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
439 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
440 SSize_t mult = fb - fa;
441 if (mult < 1)
442 croak("strxfrm() gets absurd");
443 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
444 collxfrm_mult = mult;
445 }
bbce6d69 446 }
bbce6d69 447
36477c24 448#endif /* USE_LOCALE_COLLATE */
449}
bbce6d69 450
451/*
452 * Set up for a new numeric locale.
453 */
454void
8ac85365 455perl_new_numeric(char *newnum)
bbce6d69 456{
36477c24 457#ifdef USE_LOCALE_NUMERIC
458
bbce6d69 459 if (! newnum) {
460 if (numeric_name) {
461 Safefree(numeric_name);
462 numeric_name = NULL;
463 numeric_standard = TRUE;
464 numeric_local = TRUE;
465 }
466 return;
467 }
468
469 if (! numeric_name || strNE(numeric_name, newnum)) {
470 Safefree(numeric_name);
471 numeric_name = savepv(newnum);
ff68c719 472 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
bbce6d69 473 numeric_local = TRUE;
474 }
36477c24 475
476#endif /* USE_LOCALE_NUMERIC */
bbce6d69 477}
478
479void
8ac85365 480perl_set_numeric_standard(void)
bbce6d69 481{
5f05dabc 482#ifdef USE_LOCALE_NUMERIC
483
bbce6d69 484 if (! numeric_standard) {
485 setlocale(LC_NUMERIC, "C");
486 numeric_standard = TRUE;
487 numeric_local = FALSE;
488 }
5f05dabc 489
490#endif /* USE_LOCALE_NUMERIC */
bbce6d69 491}
492
493void
8ac85365 494perl_set_numeric_local(void)
bbce6d69 495{
5f05dabc 496#ifdef USE_LOCALE_NUMERIC
497
bbce6d69 498 if (! numeric_local) {
499 setlocale(LC_NUMERIC, numeric_name);
500 numeric_standard = FALSE;
501 numeric_local = TRUE;
502 }
bbce6d69 503
36477c24 504#endif /* USE_LOCALE_NUMERIC */
5f05dabc 505}
36477c24 506
bbce6d69 507
36477c24 508/*
509 * Initialize locale awareness.
510 */
f0c5b223 511int
8ac85365 512perl_init_i18nl10n(int printwarn)
f0c5b223 513{
514 int ok = 1;
515 /* returns
516 * 1 = set ok or not applicable,
517 * 0 = fallback to C locale,
518 * -1 = fallback to C locale failed
519 */
bbce6d69 520
36477c24 521#ifdef USE_LOCALE
bbce6d69 522
36477c24 523#ifdef USE_LOCALE_CTYPE
bbce6d69 524 char *curctype = NULL;
36477c24 525#endif /* USE_LOCALE_CTYPE */
526#ifdef USE_LOCALE_COLLATE
bbce6d69 527 char *curcoll = NULL;
36477c24 528#endif /* USE_LOCALE_COLLATE */
529#ifdef USE_LOCALE_NUMERIC
bbce6d69 530 char *curnum = NULL;
36477c24 531#endif /* USE_LOCALE_NUMERIC */
02b32252 532 char *lc_all = getenv("LC_ALL");
5f05dabc 533 char *lang = getenv("LANG");
bbce6d69 534 bool setlocale_failure = FALSE;
f0c5b223 535
02b32252 536#ifdef LOCALE_ENVIRON_REQUIRED
537
538 /*
539 * Ultrix setlocale(..., "") fails if there are no environment
540 * variables from which to get a locale name.
541 */
542
543 bool done = FALSE;
544
545#ifdef LC_ALL
546 if (lang) {
547 if (setlocale(LC_ALL, ""))
548 done = TRUE;
549 else
550 setlocale_failure = TRUE;
551 }
552 if (!setlocale_failure)
553#endif /* LC_ALL */
554 {
555#ifdef USE_LOCALE_CTYPE
556 if (! (curctype = setlocale(LC_CTYPE,
557 (!done && (lang || getenv("LC_CTYPE")))
558 ? "" : Nullch)))
559 setlocale_failure = TRUE;
560#endif /* USE_LOCALE_CTYPE */
561#ifdef USE_LOCALE_COLLATE
562 if (! (curcoll = setlocale(LC_COLLATE,
563 (!done && (lang || getenv("LC_COLLATE")))
564 ? "" : Nullch)))
565 setlocale_failure = TRUE;
566#endif /* USE_LOCALE_COLLATE */
567#ifdef USE_LOCALE_NUMERIC
568 if (! (curnum = setlocale(LC_NUMERIC,
569 (!done && (lang || getenv("LC_NUMERIC")))
570 ? "" : Nullch)))
571 setlocale_failure = TRUE;
572#endif /* USE_LOCALE_NUMERIC */
573 }
574
575#else /* !LOCALE_ENVIRON_REQUIRED */
576
bbce6d69 577#ifdef LC_ALL
5f05dabc 578
bbce6d69 579 if (! setlocale(LC_ALL, ""))
580 setlocale_failure = TRUE;
5f05dabc 581 else {
582#ifdef USE_LOCALE_CTYPE
583 curctype = setlocale(LC_CTYPE, Nullch);
584#endif /* USE_LOCALE_CTYPE */
585#ifdef USE_LOCALE_COLLATE
586 curcoll = setlocale(LC_COLLATE, Nullch);
587#endif /* USE_LOCALE_COLLATE */
588#ifdef USE_LOCALE_NUMERIC
589 curnum = setlocale(LC_NUMERIC, Nullch);
590#endif /* USE_LOCALE_NUMERIC */
591 }
592
593#else /* !LC_ALL */
bbce6d69 594
36477c24 595#ifdef USE_LOCALE_CTYPE
5f05dabc 596 if (! (curctype = setlocale(LC_CTYPE, "")))
bbce6d69 597 setlocale_failure = TRUE;
36477c24 598#endif /* USE_LOCALE_CTYPE */
599#ifdef USE_LOCALE_COLLATE
5f05dabc 600 if (! (curcoll = setlocale(LC_COLLATE, "")))
bbce6d69 601 setlocale_failure = TRUE;
36477c24 602#endif /* USE_LOCALE_COLLATE */
603#ifdef USE_LOCALE_NUMERIC
5f05dabc 604 if (! (curnum = setlocale(LC_NUMERIC, "")))
bbce6d69 605 setlocale_failure = TRUE;
36477c24 606#endif /* USE_LOCALE_NUMERIC */
bbce6d69 607
5f05dabc 608#endif /* LC_ALL */
609
02b32252 610#endif /* !LOCALE_ENVIRON_REQUIRED */
611
5f05dabc 612 if (setlocale_failure) {
613 char *p;
614 bool locwarn = (printwarn > 1 ||
615 printwarn &&
616 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
20cec16a 617
5f05dabc 618 if (locwarn) {
619#ifdef LC_ALL
620
621 PerlIO_printf(PerlIO_stderr(),
622 "perl: warning: Setting locale failed.\n");
623
624#else /* !LC_ALL */
625
ef7eada9 626 PerlIO_printf(PerlIO_stderr(),
bbce6d69 627 "perl: warning: Setting locale failed for the categories:\n\t");
36477c24 628#ifdef USE_LOCALE_CTYPE
bbce6d69 629 if (! curctype)
5f05dabc 630 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
36477c24 631#endif /* USE_LOCALE_CTYPE */
632#ifdef USE_LOCALE_COLLATE
bbce6d69 633 if (! curcoll)
5f05dabc 634 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
36477c24 635#endif /* USE_LOCALE_COLLATE */
636#ifdef USE_LOCALE_NUMERIC
bbce6d69 637 if (! curnum)
5f05dabc 638 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
36477c24 639#endif /* USE_LOCALE_NUMERIC */
bbce6d69 640 PerlIO_printf(PerlIO_stderr(), "\n");
641
5f05dabc 642#endif /* LC_ALL */
643
760ac839 644 PerlIO_printf(PerlIO_stderr(),
bbce6d69 645 "perl: warning: Please check that your locale settings:\n");
ef7eada9 646
647 PerlIO_printf(PerlIO_stderr(),
bbce6d69 648 "\tLC_ALL = %c%s%c,\n",
649 lc_all ? '"' : '(',
650 lc_all ? lc_all : "unset",
651 lc_all ? '"' : ')');
5f05dabc 652
653 {
654 char **e;
655 for (e = environ; *e; e++) {
656 if (strnEQ(*e, "LC_", 3)
657 && strnNE(*e, "LC_ALL=", 7)
658 && (p = strchr(*e, '=')))
659 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
fb73857a 660 (int)(p - *e), *e, p + 1);
5f05dabc 661 }
662 }
663
ef7eada9 664 PerlIO_printf(PerlIO_stderr(),
bbce6d69 665 "\tLANG = %c%s%c\n",
5f05dabc 666 lang ? '"' : '(',
bbce6d69 667 lang ? lang : "unset",
668 lang ? '"' : ')');
ef7eada9 669
bbce6d69 670 PerlIO_printf(PerlIO_stderr(),
671 " are supported and installed on your system.\n");
5f05dabc 672 }
ef7eada9 673
5f05dabc 674#ifdef LC_ALL
675
676 if (setlocale(LC_ALL, "C")) {
677 if (locwarn)
678 PerlIO_printf(PerlIO_stderr(),
679 "perl: warning: Falling back to the standard locale (\"C\").\n");
bbce6d69 680 ok = 0;
ef7eada9 681 }
5f05dabc 682 else {
683 if (locwarn)
684 PerlIO_printf(PerlIO_stderr(),
685 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
686 ok = -1;
687 }
bbce6d69 688
5f05dabc 689#else /* ! LC_ALL */
690
691 if (0
36477c24 692#ifdef USE_LOCALE_CTYPE
5f05dabc 693 || !(curctype || setlocale(LC_CTYPE, "C"))
36477c24 694#endif /* USE_LOCALE_CTYPE */
695#ifdef USE_LOCALE_COLLATE
5f05dabc 696 || !(curcoll || setlocale(LC_COLLATE, "C"))
36477c24 697#endif /* USE_LOCALE_COLLATE */
698#ifdef USE_LOCALE_NUMERIC
5f05dabc 699 || !(curnum || setlocale(LC_NUMERIC, "C"))
36477c24 700#endif /* USE_LOCALE_NUMERIC */
5f05dabc 701 )
702 {
703 if (locwarn)
bbce6d69 704 PerlIO_printf(PerlIO_stderr(),
5f05dabc 705 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
706 ok = -1;
bbce6d69 707 }
5f05dabc 708
bbce6d69 709#endif /* ! LC_ALL */
5f05dabc 710
711#ifdef USE_LOCALE_CTYPE
712 curctype = setlocale(LC_CTYPE, Nullch);
713#endif /* USE_LOCALE_CTYPE */
714#ifdef USE_LOCALE_COLLATE
715 curcoll = setlocale(LC_COLLATE, Nullch);
716#endif /* USE_LOCALE_COLLATE */
717#ifdef USE_LOCALE_NUMERIC
718 curnum = setlocale(LC_NUMERIC, Nullch);
719#endif /* USE_LOCALE_NUMERIC */
ef7eada9 720 }
721
36477c24 722#ifdef USE_LOCALE_CTYPE
bbce6d69 723 perl_new_ctype(curctype);
36477c24 724#endif /* USE_LOCALE_CTYPE */
bbce6d69 725
36477c24 726#ifdef USE_LOCALE_COLLATE
bbce6d69 727 perl_new_collate(curcoll);
36477c24 728#endif /* USE_LOCALE_COLLATE */
bbce6d69 729
36477c24 730#ifdef USE_LOCALE_NUMERIC
bbce6d69 731 perl_new_numeric(curnum);
36477c24 732#endif /* USE_LOCALE_NUMERIC */
ef7eada9 733
36477c24 734#endif /* USE_LOCALE */
ef7eada9 735
f0c5b223 736 return ok;
737}
738
bbce6d69 739/* Backwards compatibility. */
740int
8ac85365 741perl_init_i18nl14n(int printwarn)
bbce6d69 742{
5f05dabc 743 return perl_init_i18nl10n(printwarn);
bbce6d69 744}
ef7eada9 745
36477c24 746#ifdef USE_LOCALE_COLLATE
ef7eada9 747
bbce6d69 748/*
749 * mem_collxfrm() is a bit like strxfrm() but with two important
750 * differences. First, it handles embedded NULs. Second, it allocates
751 * a bit more memory than needed for the transformed data itself.
752 * The real transformed data begins at offset sizeof(collationix).
753 * Please see sv_collxfrm() to see how this is used.
754 */
755char *
8ac85365 756mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
bbce6d69 757{
758 char *xbuf;
759 STRLEN xalloc, xin, xout;
760
761 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
762 /* the +1 is for the terminating NUL. */
763
764 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
765 New(171, xbuf, xalloc, char);
766 if (! xbuf)
767 goto bad;
768
769 *(U32*)xbuf = collation_ix;
770 xout = sizeof(collation_ix);
771 for (xin = 0; xin < len; ) {
772 SSize_t xused;
773
774 for (;;) {
775 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
776 if (xused == -1)
777 goto bad;
778 if (xused < xalloc - xout)
779 break;
780 xalloc = (2 * xalloc) + 1;
781 Renew(xbuf, xalloc, char);
782 if (! xbuf)
783 goto bad;
784 }
ef7eada9 785
bbce6d69 786 xin += strlen(s + xin) + 1;
787 xout += xused;
788
789 /* Embedded NULs are understood but silently skipped
790 * because they make no sense in locale collation. */
791 }
ef7eada9 792
bbce6d69 793 xbuf[xout] = '\0';
794 *xlen = xout - sizeof(collation_ix);
795 return xbuf;
796
797 bad:
798 Safefree(xbuf);
799 *xlen = 0;
800 return NULL;
ef7eada9 801}
802
36477c24 803#endif /* USE_LOCALE_COLLATE */
bbce6d69 804
378cc40b 805void
8ac85365 806fbm_compile(SV *sv)
378cc40b 807{
a687059c 808 register unsigned char *s;
809 register unsigned char *table;
79072805 810 register U32 i;
811 register U32 len = SvCUR(sv);
812 I32 rarest = 0;
813 U32 frequency = 256;
814
748a9306 815 if (len > 255)
816 return; /* can't have offsets that big */
79072805 817 Sv_Grow(sv,len+258);
463ee0b2 818 table = (unsigned char*)(SvPVX(sv) + len + 1);
a687059c 819 s = table - 2;
820 for (i = 0; i < 256; i++) {
378cc40b 821 table[i] = len;
822 }
823 i = 0;
463ee0b2 824 while (s >= (unsigned char*)(SvPVX(sv)))
a687059c 825 {
bbce6d69 826 if (table[*s] == len)
827 table[*s] = i;
378cc40b 828 s--,i++;
829 }
79072805 830 sv_upgrade(sv, SVt_PVBM);
bbce6d69 831 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
79072805 832 SvVALID_on(sv);
378cc40b 833
463ee0b2 834 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
bbce6d69 835 for (i = 0; i < len; i++) {
836 if (freq[s[i]] < frequency) {
837 rarest = i;
838 frequency = freq[s[i]];
378cc40b 839 }
840 }
79072805 841 BmRARE(sv) = s[rarest];
842 BmPREVIOUS(sv) = rarest;
760ac839 843 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
378cc40b 844}
845
378cc40b 846char *
8ac85365 847fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
378cc40b 848{
a687059c 849 register unsigned char *s;
79072805 850 register I32 tmp;
851 register I32 littlelen;
a687059c 852 register unsigned char *little;
853 register unsigned char *table;
854 register unsigned char *olds;
855 register unsigned char *oldlittle;
378cc40b 856
79072805 857 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
a0d0e21e 858 STRLEN len;
859 char *l = SvPV(littlestr,len);
860 if (!len)
d48672a2 861 return (char*)big;
a0d0e21e 862 return ninstr((char*)big,(char*)bigend, l, l + len);
d48672a2 863 }
378cc40b 864
79072805 865 littlelen = SvCUR(littlestr);
866 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
0f85fab0 867 if (littlelen > bigend - big)
868 return Nullch;
463ee0b2 869 little = (unsigned char*)SvPVX(littlestr);
bbce6d69 870 s = bigend - littlelen;
36477c24 871 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 872 return (char*)s; /* how sweet it is */
873 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
874 && s > big) {
875 s--;
36477c24 876 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 877 return (char*)s;
a687059c 878 }
bbce6d69 879 return Nullch;
a687059c 880 }
463ee0b2 881 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
62b28dd9 882 if (--littlelen >= bigend - big)
883 return Nullch;
884 s = big + littlelen;
a687059c 885 oldlittle = little = table - 2;
bbce6d69 886 if (s < bigend) {
887 top2:
888 /*SUPPRESS 560*/
889 if (tmp = table[*s]) {
62b28dd9 890#ifdef POINTERRIGOR
bbce6d69 891 if (bigend - s > tmp) {
892 s += tmp;
893 goto top2;
894 }
62b28dd9 895#else
bbce6d69 896 if ((s += tmp) < bigend)
897 goto top2;
62b28dd9 898#endif
bbce6d69 899 return Nullch;
a687059c 900 }
bbce6d69 901 else {
902 tmp = littlelen; /* less expensive than calling strncmp() */
903 olds = s;
904 while (tmp--) {
905 if (*--s == *--little)
906 continue;
907 s = olds + 1; /* here we pay the price for failure */
908 little = oldlittle;
909 if (s < bigend) /* fake up continue to outer loop */
62b28dd9 910 goto top2;
62b28dd9 911 return Nullch;
a687059c 912 }
bbce6d69 913 return (char *)s;
378cc40b 914 }
915 }
916 return Nullch;
917}
918
919char *
8ac85365 920screaminstr(SV *bigstr, SV *littlestr)
378cc40b 921{
a687059c 922 register unsigned char *s, *x;
923 register unsigned char *big;
79072805 924 register I32 pos;
925 register I32 previous;
926 register I32 first;
a687059c 927 register unsigned char *little;
928 register unsigned char *bigend;
929 register unsigned char *littleend;
378cc40b 930
79072805 931 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
378cc40b 932 return Nullch;
463ee0b2 933 little = (unsigned char *)(SvPVX(littlestr));
79072805 934 littleend = little + SvCUR(littlestr);
378cc40b 935 first = *little++;
79072805 936 previous = BmPREVIOUS(littlestr);
463ee0b2 937 big = (unsigned char *)(SvPVX(bigstr));
79072805 938 bigend = big + SvCUR(bigstr);
378cc40b 939 while (pos < previous) {
940 if (!(pos += screamnext[pos]))
941 return Nullch;
942 }
de3bb511 943#ifdef POINTERRIGOR
bbce6d69 944 do {
945 if (big[pos-previous] != first)
946 continue;
947 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
948 if (x >= bigend)
949 return Nullch;
950 if (*s++ != *x++) {
951 s--;
952 break;
de3bb511 953 }
bbce6d69 954 }
955 if (s == littleend)
956 return (char *)(big+pos-previous);
957 } while ( pos += screamnext[pos] );
de3bb511 958#else /* !POINTERRIGOR */
959 big -= previous;
bbce6d69 960 do {
961 if (big[pos] != first)
962 continue;
963 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
964 if (x >= bigend)
965 return Nullch;
966 if (*s++ != *x++) {
967 s--;
968 break;
378cc40b 969 }
bbce6d69 970 }
971 if (s == littleend)
972 return (char *)(big+pos);
973 } while ( pos += screamnext[pos] );
de3bb511 974#endif /* POINTERRIGOR */
8d063cd8 975 return Nullch;
976}
977
79072805 978I32
8ac85365 979ibcmp(char *s1, char *s2, register I32 len)
79072805 980{
bbce6d69 981 register U8 *a = (U8 *)s1;
982 register U8 *b = (U8 *)s2;
79072805 983 while (len--) {
bbce6d69 984 if (*a != *b && *a != fold[*b])
985 return 1;
986 a++,b++;
987 }
988 return 0;
989}
990
991I32
8ac85365 992ibcmp_locale(char *s1, char *s2, register I32 len)
bbce6d69 993{
994 register U8 *a = (U8 *)s1;
995 register U8 *b = (U8 *)s2;
996 while (len--) {
997 if (*a != *b && *a != fold_locale[*b])
998 return 1;
999 a++,b++;
79072805 1000 }
1001 return 0;
1002}
1003
8d063cd8 1004/* copy a string to a safe spot */
1005
1006char *
8ac85365 1007savepv(char *sv)
8d063cd8 1008{
a687059c 1009 register char *newaddr;
8d063cd8 1010
79072805 1011 New(902,newaddr,strlen(sv)+1,char);
1012 (void)strcpy(newaddr,sv);
8d063cd8 1013 return newaddr;
1014}
1015
a687059c 1016/* same thing but with a known length */
1017
1018char *
8ac85365 1019savepvn(char *sv, register I32 len)
a687059c 1020{
1021 register char *newaddr;
1022
1023 New(903,newaddr,len+1,char);
79072805 1024 Copy(sv,newaddr,len,char); /* might not be null terminated */
a687059c 1025 newaddr[len] = '\0'; /* is now */
1026 return newaddr;
1027}
1028
fc36a67e 1029/* the SV for form() and mess() is not kept in an arena */
1030
1031static SV *
8ac85365 1032mess_alloc(void)
fc36a67e 1033{
1034 SV *sv;
1035 XPVMG *any;
1036
1037 /* Create as PVMG now, to avoid any upgrading later */
1038 New(905, sv, 1, SV);
1039 Newz(905, any, 1, XPVMG);
1040 SvFLAGS(sv) = SVt_PVMG;
1041 SvANY(sv) = (void*)any;
1042 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1043 return sv;
1044}
1045
a0d0e21e 1046#ifdef I_STDARG
8990e307 1047char *
46fc3d4c 1048form(const char* pat, ...)
a687059c 1049#else
1050/*VARARGS0*/
de3bb511 1051char *
46fc3d4c 1052form(pat, va_alist)
71be2cbc 1053 const char *pat;
46fc3d4c 1054 va_dcl
8990e307 1055#endif
1056{
46fc3d4c 1057 va_list args;
1058#ifdef I_STDARG
1059 va_start(args, pat);
a687059c 1060#else
46fc3d4c 1061 va_start(args);
d48672a2 1062#endif
fc36a67e 1063 if (!mess_sv)
1064 mess_sv = mess_alloc();
1065 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
46fc3d4c 1066 va_end(args);
fc36a67e 1067 return SvPVX(mess_sv);
46fc3d4c 1068}
a687059c 1069
46fc3d4c 1070char *
8ac85365 1071mess(const char *pat, va_list *args)
46fc3d4c 1072{
1073 SV *sv;
1074 static char dgd[] = " during global destruction.\n";
1075
46fc3d4c 1076 if (!mess_sv)
fc36a67e 1077 mess_sv = mess_alloc();
46fc3d4c 1078 sv = mess_sv;
fc36a67e 1079 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1080 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
e858de61 1081 dTHR;
2304df62 1082 if (dirty)
46fc3d4c 1083 sv_catpv(sv, dgd);
2304df62 1084 else {
46fc3d4c 1085 if (curcop->cop_line)
fc36a67e 1086 sv_catpvf(sv, " at %_ line %ld",
46fc3d4c 1087 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
c07a80fd 1088 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1089 bool line_mode = (RsSIMPLE(rs) &&
1090 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
46fc3d4c 1091 sv_catpvf(sv, ", <%s> %s %ld",
1092 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1093 line_mode ? "line" : "chunk",
1094 (long)IoLINES(GvIOp(last_in_gv)));
2304df62 1095 }
46fc3d4c 1096 sv_catpv(sv, ".\n");
a687059c 1097 }
a687059c 1098 }
46fc3d4c 1099 return SvPVX(sv);
a687059c 1100}
1101
ecfc5424 1102#ifdef I_STDARG
36477c24 1103OP *
71be2cbc 1104die(const char* pat, ...)
36477c24 1105#else
1106/*VARARGS0*/
1107OP *
1108die(pat, va_alist)
71be2cbc 1109 const char *pat;
36477c24 1110 va_dcl
1111#endif
1112{
5dc0d613 1113 dTHR;
36477c24 1114 va_list args;
1115 char *message;
035902c7 1116 I32 oldrunlevel = runlevel;
36477c24 1117 int was_in_eval = in_eval;
1118 HV *stash;
1119 GV *gv;
1120 CV *cv;
1121
d9f997d7 1122 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: curstack = %p, mainstack= %p\n",
0f15f207 1123 curstack, mainstack));/*debug*/
36477c24 1124 /* We have to switch back to mainstack or die_where may try to pop
1125 * the eval block from the wrong stack if die is being called from a
1126 * signal handler. - dkindred@cs.cmu.edu */
1127 if (curstack != mainstack) {
1128 dSP;
1129 SWITCHSTACK(curstack, mainstack);
1130 }
1131
1132#ifdef I_STDARG
1133 va_start(args, pat);
1134#else
1135 va_start(args);
1136#endif
1137 message = mess(pat, &args);
1138 va_end(args);
1139
d9f997d7 1140 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: message = %s\ndiehook = %p\n",
0f15f207 1141 message, diehook));/*debug*/
1738f5c4 1142 if (diehook) {
1143 /* sv_2cv might call croak() */
1144 SV *olddiehook = diehook;
1145 ENTER;
1146 SAVESPTR(diehook);
1147 diehook = Nullsv;
1148 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1149 LEAVE;
1150 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1151 dSP;
774d564b 1152 SV *msg;
1153
1154 ENTER;
1155 msg = newSVpv(message, 0);
1156 SvREADONLY_on(msg);
1157 SAVEFREESV(msg);
1738f5c4 1158
1159 PUSHMARK(sp);
1160 XPUSHs(msg);
1161 PUTBACK;
1162 perl_call_sv((SV*)cv, G_DISCARD);
1163
774d564b 1164 LEAVE;
1738f5c4 1165 }
36477c24 1166 }
1167
1168 restartop = die_where(message);
d9f997d7 1169 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
0f15f207 1170 "die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n",
1171 restartop, was_in_eval, oldrunlevel));/*debug*/
36477c24 1172 if ((!restartop && was_in_eval) || oldrunlevel > 1)
54310121 1173 JMPENV_JUMP(3);
36477c24 1174 return restartop;
1175}
1176
1177#ifdef I_STDARG
79072805 1178void
71be2cbc 1179croak(const char* pat, ...)
463ee0b2 1180#else
8990e307 1181/*VARARGS0*/
1182void
1183croak(pat, va_alist)
1184 char *pat;
1185 va_dcl
463ee0b2 1186#endif
a687059c 1187{
11343788 1188 dTHR;
a687059c 1189 va_list args;
de3bb511 1190 char *message;
748a9306 1191 HV *stash;
1192 GV *gv;
1193 CV *cv;
a687059c 1194
a0d0e21e 1195#ifdef I_STDARG
8990e307 1196 va_start(args, pat);
1197#else
a687059c 1198 va_start(args);
8990e307 1199#endif
2304df62 1200 message = mess(pat, &args);
a687059c 1201 va_end(args);
11343788 1202#ifdef USE_THREADS
d9f997d7 1203 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
11343788 1204#endif /* USE_THREADS */
20cec16a 1205 if (diehook) {
1738f5c4 1206 /* sv_2cv might call croak() */
20cec16a 1207 SV *olddiehook = diehook;
1738f5c4 1208 ENTER;
1209 SAVESPTR(diehook);
1210 diehook = Nullsv;
20cec16a 1211 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1738f5c4 1212 LEAVE;
1213 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1214 dSP;
774d564b 1215 SV *msg;
1216
1217 ENTER;
1218 msg = newSVpv(message, 0);
1219 SvREADONLY_on(msg);
1220 SAVEFREESV(msg);
20cec16a 1221
1222 PUSHMARK(sp);
1738f5c4 1223 XPUSHs(msg);
20cec16a 1224 PUTBACK;
1225 perl_call_sv((SV*)cv, G_DISCARD);
36477c24 1226
774d564b 1227 LEAVE;
20cec16a 1228 }
748a9306 1229 }
a0d0e21e 1230 if (in_eval) {
1231 restartop = die_where(message);
54310121 1232 JMPENV_JUMP(3);
a0d0e21e 1233 }
760ac839 1234 PerlIO_puts(PerlIO_stderr(),message);
1235 (void)PerlIO_flush(PerlIO_stderr());
f86702cc 1236 my_failure_exit();
a687059c 1237}
1238
8990e307 1239void
ecfc5424 1240#ifdef I_STDARG
71be2cbc 1241warn(const char* pat,...)
463ee0b2 1242#else
8990e307 1243/*VARARGS0*/
1244warn(pat,va_alist)
71be2cbc 1245 const char *pat;
8990e307 1246 va_dcl
463ee0b2 1247#endif
a687059c 1248{
1249 va_list args;
de3bb511 1250 char *message;
748a9306 1251 HV *stash;
1252 GV *gv;
1253 CV *cv;
a687059c 1254
a0d0e21e 1255#ifdef I_STDARG
8990e307 1256 va_start(args, pat);
1257#else
a687059c 1258 va_start(args);
8990e307 1259#endif
2304df62 1260 message = mess(pat, &args);
a687059c 1261 va_end(args);
1262
20cec16a 1263 if (warnhook) {
1738f5c4 1264 /* sv_2cv might call warn() */
11343788 1265 dTHR;
20cec16a 1266 SV *oldwarnhook = warnhook;
1738f5c4 1267 ENTER;
1268 SAVESPTR(warnhook);
1269 warnhook = Nullsv;
20cec16a 1270 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1738f5c4 1271 LEAVE;
1272 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1273 dSP;
774d564b 1274 SV *msg;
1275
1276 ENTER;
1277 msg = newSVpv(message, 0);
1278 SvREADONLY_on(msg);
1279 SAVEFREESV(msg);
1280
20cec16a 1281 PUSHMARK(sp);
774d564b 1282 XPUSHs(msg);
20cec16a 1283 PUTBACK;
1284 perl_call_sv((SV*)cv, G_DISCARD);
774d564b 1285
1286 LEAVE;
20cec16a 1287 return;
1288 }
748a9306 1289 }
20cec16a 1290 PerlIO_puts(PerlIO_stderr(),message);
a687059c 1291#ifdef LEAKTEST
20cec16a 1292 DEBUG_L(xstat());
a687059c 1293#endif
20cec16a 1294 (void)PerlIO_flush(PerlIO_stderr());
a687059c 1295}
8d063cd8 1296
a0d0e21e 1297#ifndef VMS /* VMS' my_setenv() is in VMS.c */
3e3baf6d 1298#ifndef WIN32
8d063cd8 1299void
8ac85365 1300my_setenv(char *nam, char *val)
8d063cd8 1301{
79072805 1302 register I32 i=setenv_getix(nam); /* where does it go? */
8d063cd8 1303
fe14fcc3 1304 if (environ == origenviron) { /* need we copy environment? */
79072805 1305 I32 j;
1306 I32 max;
fe14fcc3 1307 char **tmpenv;
1308
de3bb511 1309 /*SUPPRESS 530*/
fe14fcc3 1310 for (max = i; environ[max]; max++) ;
1311 New(901,tmpenv, max+2, char*);
1312 for (j=0; j<max; j++) /* copy environment */
a0d0e21e 1313 tmpenv[j] = savepv(environ[j]);
fe14fcc3 1314 tmpenv[max] = Nullch;
1315 environ = tmpenv; /* tell exec where it is now */
1316 }
a687059c 1317 if (!val) {
e5ebf479 1318 Safefree(environ[i]);
a687059c 1319 while (environ[i]) {
1320 environ[i] = environ[i+1];
1321 i++;
1322 }
1323 return;
1324 }
8d063cd8 1325 if (!environ[i]) { /* does not exist yet */
fe14fcc3 1326 Renew(environ, i+2, char*); /* just expand it a bit */
8d063cd8 1327 environ[i+1] = Nullch; /* make sure it's null terminated */
1328 }
fe14fcc3 1329 else
1330 Safefree(environ[i]);
a687059c 1331 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
62b28dd9 1332#ifndef MSDOS
a687059c 1333 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
62b28dd9 1334#else
1335 /* MS-DOS requires environment variable names to be in uppercase */
fe14fcc3 1336 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1337 * some utilities and applications may break because they only look
1338 * for upper case strings. (Fixed strupr() bug here.)]
1339 */
1340 strcpy(environ[i],nam); strupr(environ[i]);
62b28dd9 1341 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1342#endif /* MSDOS */
8d063cd8 1343}
1344
3e3baf6d 1345#else /* if WIN32 */
68dc0745 1346
1347void
1348my_setenv(nam,val)
1349char *nam, *val;
1350{
3e3baf6d 1351
1352#ifdef USE_WIN32_RTL_ENV
1353
68dc0745 1354 register char *envstr;
1355 STRLEN namlen = strlen(nam);
3e3baf6d 1356 STRLEN vallen;
1357 char *oldstr = environ[setenv_getix(nam)];
1358
1359 /* putenv() has totally broken semantics in both the Borland
1360 * and Microsoft CRTLs. They either store the passed pointer in
1361 * the environment without making a copy, or make a copy and don't
1362 * free it. And on top of that, they dont free() old entries that
1363 * are being replaced/deleted. This means the caller must
1364 * free any old entries somehow, or we end up with a memory
1365 * leak every time my_setenv() is called. One might think
1366 * one could directly manipulate environ[], like the UNIX code
1367 * above, but direct changes to environ are not allowed when
1368 * calling putenv(), since the RTLs maintain an internal
1369 * *copy* of environ[]. Bad, bad, *bad* stink.
1370 * GSAR 97-06-07
1371 */
68dc0745 1372
3e3baf6d 1373 if (!val) {
1374 if (!oldstr)
1375 return;
1376 val = "";
1377 vallen = 0;
1378 }
1379 else
1380 vallen = strlen(val);
fc36a67e 1381 New(904, envstr, namlen + vallen + 3, char);
68dc0745 1382 (void)sprintf(envstr,"%s=%s",nam,val);
3e3baf6d 1383 (void)putenv(envstr);
1384 if (oldstr)
1385 Safefree(oldstr);
1386#ifdef _MSC_VER
1387 Safefree(envstr); /* MSVCRT leaks without this */
1388#endif
1389
1390#else /* !USE_WIN32_RTL_ENV */
1391
1392 /* The sane way to deal with the environment.
1393 * Has these advantages over putenv() & co.:
1394 * * enables us to store a truly empty value in the
1395 * environment (like in UNIX).
1396 * * we don't have to deal with RTL globals, bugs and leaks.
1397 * * Much faster.
1398 * Why you may want to enable USE_WIN32_RTL_ENV:
1399 * * environ[] and RTL functions will not reflect changes,
1400 * which might be an issue if extensions want to access
1401 * the env. via RTL. This cuts both ways, since RTL will
1402 * not see changes made by extensions that call the Win32
1403 * functions directly, either.
1404 * GSAR 97-06-07
1405 */
1406 SetEnvironmentVariable(nam,val);
1407
1408#endif
1409}
1410
1411#endif /* WIN32 */
1412
1413I32
8ac85365 1414setenv_getix(char *nam)
3e3baf6d 1415{
1416 register I32 i, len = strlen(nam);
1417
1418 for (i = 0; environ[i]; i++) {
1419 if (
1420#ifdef WIN32
1421 strnicmp(environ[i],nam,len) == 0
1422#else
1423 strnEQ(environ[i],nam,len)
1424#endif
1425 && environ[i][len] == '=')
1426 break; /* strnEQ must come first to avoid */
1427 } /* potential SEGV's */
1428 return i;
68dc0745 1429}
1430
a0d0e21e 1431#endif /* !VMS */
378cc40b 1432
16d20bd9 1433#ifdef UNLINK_ALL_VERSIONS
79072805 1434I32
378cc40b 1435unlnk(f) /* unlink all versions of a file */
1436char *f;
1437{
79072805 1438 I32 i;
378cc40b 1439
1440 for (i = 0; unlink(f) >= 0; i++) ;
1441 return i ? 0 : -1;
1442}
1443#endif
1444
85e6fe83 1445#if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
378cc40b 1446char *
7c0587c8 1447my_bcopy(from,to,len)
378cc40b 1448register char *from;
1449register char *to;
79072805 1450register I32 len;
378cc40b 1451{
1452 char *retval = to;
1453
7c0587c8 1454 if (from - to >= 0) {
1455 while (len--)
1456 *to++ = *from++;
1457 }
1458 else {
1459 to += len;
1460 from += len;
1461 while (len--)
faf8582f 1462 *(--to) = *(--from);
7c0587c8 1463 }
378cc40b 1464 return retval;
1465}
ffed7fef 1466#endif
378cc40b 1467
fc36a67e 1468#ifndef HAS_MEMSET
1469void *
1470my_memset(loc,ch,len)
1471register char *loc;
1472register I32 ch;
1473register I32 len;
1474{
1475 char *retval = loc;
1476
1477 while (len--)
1478 *loc++ = ch;
1479 return retval;
1480}
1481#endif
1482
7c0587c8 1483#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1484char *
7c0587c8 1485my_bzero(loc,len)
378cc40b 1486register char *loc;
79072805 1487register I32 len;
378cc40b 1488{
1489 char *retval = loc;
1490
1491 while (len--)
1492 *loc++ = 0;
1493 return retval;
1494}
1495#endif
7c0587c8 1496
36477c24 1497#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1498I32
7c0587c8 1499my_memcmp(s1,s2,len)
36477c24 1500char *s1;
1501char *s2;
79072805 1502register I32 len;
7c0587c8 1503{
36477c24 1504 register U8 *a = (U8 *)s1;
1505 register U8 *b = (U8 *)s2;
79072805 1506 register I32 tmp;
7c0587c8 1507
1508 while (len--) {
36477c24 1509 if (tmp = *a++ - *b++)
7c0587c8 1510 return tmp;
1511 }
1512 return 0;
1513}
36477c24 1514#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1515
4633a7c4 1516#if defined(I_STDARG) || defined(I_VARARGS)
fe14fcc3 1517#ifndef HAS_VPRINTF
a687059c 1518
85e6fe83 1519#ifdef USE_CHAR_VSPRINTF
a687059c 1520char *
1521#else
1522int
1523#endif
1524vsprintf(dest, pat, args)
71be2cbc 1525char *dest;
1526const char *pat;
1527char *args;
a687059c 1528{
1529 FILE fakebuf;
1530
1531 fakebuf._ptr = dest;
1532 fakebuf._cnt = 32767;
35c8bce7 1533#ifndef _IOSTRG
1534#define _IOSTRG 0
1535#endif
a687059c 1536 fakebuf._flag = _IOWRT|_IOSTRG;
1537 _doprnt(pat, args, &fakebuf); /* what a kludge */
1538 (void)putc('\0', &fakebuf);
85e6fe83 1539#ifdef USE_CHAR_VSPRINTF
a687059c 1540 return(dest);
1541#else
1542 return 0; /* perl doesn't use return value */
1543#endif
1544}
1545
fe14fcc3 1546#endif /* HAS_VPRINTF */
4633a7c4 1547#endif /* I_VARARGS || I_STDARGS */
a687059c 1548
1549#ifdef MYSWAP
ffed7fef 1550#if BYTEORDER != 0x4321
a687059c 1551short
748a9306 1552#ifndef CAN_PROTOTYPE
a687059c 1553my_swap(s)
1554short s;
748a9306 1555#else
1556my_swap(short s)
1557#endif
a687059c 1558{
1559#if (BYTEORDER & 1) == 0
1560 short result;
1561
1562 result = ((s & 255) << 8) + ((s >> 8) & 255);
1563 return result;
1564#else
1565 return s;
1566#endif
1567}
1568
1569long
748a9306 1570#ifndef CAN_PROTOTYPE
1571my_htonl(l)
a687059c 1572register long l;
748a9306 1573#else
1574my_htonl(long l)
1575#endif
a687059c 1576{
1577 union {
1578 long result;
ffed7fef 1579 char c[sizeof(long)];
a687059c 1580 } u;
1581
ffed7fef 1582#if BYTEORDER == 0x1234
a687059c 1583 u.c[0] = (l >> 24) & 255;
1584 u.c[1] = (l >> 16) & 255;
1585 u.c[2] = (l >> 8) & 255;
1586 u.c[3] = l & 255;
1587 return u.result;
1588#else
ffed7fef 1589#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1590 croak("Unknown BYTEORDER\n");
a687059c 1591#else
79072805 1592 register I32 o;
1593 register I32 s;
a687059c 1594
ffed7fef 1595 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1596 u.c[o & 0xf] = (l >> s) & 255;
a687059c 1597 }
1598 return u.result;
1599#endif
1600#endif
1601}
1602
1603long
748a9306 1604#ifndef CAN_PROTOTYPE
1605my_ntohl(l)
a687059c 1606register long l;
748a9306 1607#else
1608my_ntohl(long l)
1609#endif
a687059c 1610{
1611 union {
1612 long l;
ffed7fef 1613 char c[sizeof(long)];
a687059c 1614 } u;
1615
ffed7fef 1616#if BYTEORDER == 0x1234
a687059c 1617 u.c[0] = (l >> 24) & 255;
1618 u.c[1] = (l >> 16) & 255;
1619 u.c[2] = (l >> 8) & 255;
1620 u.c[3] = l & 255;
1621 return u.l;
1622#else
ffed7fef 1623#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1624 croak("Unknown BYTEORDER\n");
a687059c 1625#else
79072805 1626 register I32 o;
1627 register I32 s;
a687059c 1628
1629 u.l = l;
1630 l = 0;
ffed7fef 1631 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1632 l |= (u.c[o & 0xf] & 255) << s;
a687059c 1633 }
1634 return l;
1635#endif
1636#endif
1637}
1638
ffed7fef 1639#endif /* BYTEORDER != 0x4321 */
988174c1 1640#endif /* MYSWAP */
1641
1642/*
1643 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1644 * If these functions are defined,
1645 * the BYTEORDER is neither 0x1234 nor 0x4321.
1646 * However, this is not assumed.
1647 * -DWS
1648 */
1649
1650#define HTOV(name,type) \
1651 type \
1652 name (n) \
1653 register type n; \
1654 { \
1655 union { \
1656 type value; \
1657 char c[sizeof(type)]; \
1658 } u; \
79072805 1659 register I32 i; \
1660 register I32 s; \
988174c1 1661 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1662 u.c[i] = (n >> s) & 0xFF; \
1663 } \
1664 return u.value; \
1665 }
1666
1667#define VTOH(name,type) \
1668 type \
1669 name (n) \
1670 register type n; \
1671 { \
1672 union { \
1673 type value; \
1674 char c[sizeof(type)]; \
1675 } u; \
79072805 1676 register I32 i; \
1677 register I32 s; \
988174c1 1678 u.value = n; \
1679 n = 0; \
1680 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1681 n += (u.c[i] & 0xFF) << s; \
1682 } \
1683 return n; \
1684 }
1685
1686#if defined(HAS_HTOVS) && !defined(htovs)
1687HTOV(htovs,short)
1688#endif
1689#if defined(HAS_HTOVL) && !defined(htovl)
1690HTOV(htovl,long)
1691#endif
1692#if defined(HAS_VTOHS) && !defined(vtohs)
1693VTOH(vtohs,short)
1694#endif
1695#if defined(HAS_VTOHL) && !defined(vtohl)
1696VTOH(vtohl,long)
1697#endif
a687059c 1698
5f05dabc 1699 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1700#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
760ac839 1701PerlIO *
8ac85365 1702my_popen(char *cmd, char *mode)
a687059c 1703{
1704 int p[2];
8ac85365 1705 register I32 This, that;
79072805 1706 register I32 pid;
1707 SV *sv;
1738f5c4 1708 I32 doexec = strNE(cmd,"-");
a687059c 1709
ddcf38b7 1710#ifdef OS2
1711 if (doexec) {
1712 return my_syspopen(cmd,mode);
1713 }
1714#endif
a687059c 1715 if (pipe(p) < 0)
1716 return Nullfp;
8ac85365 1717 This = (*mode == 'w');
1718 that = !This;
bbce6d69 1719 if (doexec && tainting) {
1720 taint_env();
1721 taint_proper("Insecure %s%s", "EXEC");
d48672a2 1722 }
a687059c 1723 while ((pid = (doexec?vfork():fork())) < 0) {
1724 if (errno != EAGAIN) {
8ac85365 1725 close(p[This]);
a687059c 1726 if (!doexec)
463ee0b2 1727 croak("Can't fork");
a687059c 1728 return Nullfp;
1729 }
1730 sleep(5);
1731 }
1732 if (pid == 0) {
79072805 1733 GV* tmpgv;
1734
a687059c 1735#define THIS that
8ac85365 1736#define THAT This
a687059c 1737 close(p[THAT]);
1738 if (p[THIS] != (*mode == 'r')) {
1739 dup2(p[THIS], *mode == 'r');
1740 close(p[THIS]);
1741 }
1742 if (doexec) {
a0d0e21e 1743#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130 1744 int fd;
1745
1746#ifndef NOFILE
1747#define NOFILE 20
1748#endif
d48672a2 1749 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
ae986130 1750 close(fd);
1751#endif
a687059c 1752 do_exec(cmd); /* may or may not use the shell */
1753 _exit(1);
1754 }
de3bb511 1755 /*SUPPRESS 560*/
85e6fe83 1756 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 1757 sv_setiv(GvSV(tmpgv), (IV)getpid());
9f68db38 1758 forkprocess = 0;
463ee0b2 1759 hv_clear(pidstatus); /* we have no children */
a687059c 1760 return Nullfp;
1761#undef THIS
1762#undef THAT
1763 }
62b28dd9 1764 do_execfree(); /* free any memory malloced by child on vfork */
a687059c 1765 close(p[that]);
8ac85365 1766 if (p[that] < p[This]) {
1767 dup2(p[This], p[that]);
1768 close(p[This]);
1769 p[This] = p[that];
62b28dd9 1770 }
8ac85365 1771 sv = *av_fetch(fdpid,p[This],TRUE);
a0d0e21e 1772 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 1773 SvIVX(sv) = pid;
a687059c 1774 forkprocess = pid;
8ac85365 1775 return PerlIO_fdopen(p[This], mode);
a687059c 1776}
7c0587c8 1777#else
55497cff 1778#if defined(atarist) || defined(DJGPP)
7c0587c8 1779FILE *popen();
760ac839 1780PerlIO *
79072805 1781my_popen(cmd,mode)
7c0587c8 1782char *cmd;
1783char *mode;
1784{
760ac839 1785 /* Needs work for PerlIO ! */
55497cff 1786 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1787 return popen(PerlIO_exportFILE(cmd, 0), mode);
7c0587c8 1788}
1789#endif
1790
1791#endif /* !DOSISH */
a687059c 1792
748a9306 1793#ifdef DUMP_FDS
79072805 1794dump_fds(s)
ae986130 1795char *s;
1796{
1797 int fd;
1798 struct stat tmpstatbuf;
1799
760ac839 1800 PerlIO_printf(PerlIO_stderr(),"%s", s);
ae986130 1801 for (fd = 0; fd < 32; fd++) {
a0d0e21e 1802 if (Fstat(fd,&tmpstatbuf) >= 0)
760ac839 1803 PerlIO_printf(PerlIO_stderr()," %d",fd);
ae986130 1804 }
760ac839 1805 PerlIO_printf(PerlIO_stderr(),"\n");
ae986130 1806}
1807#endif
1808
fe14fcc3 1809#ifndef HAS_DUP2
fec02dd3 1810int
a687059c 1811dup2(oldfd,newfd)
1812int oldfd;
1813int newfd;
1814{
a0d0e21e 1815#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3 1816 if (oldfd == newfd)
1817 return oldfd;
62b28dd9 1818 close(newfd);
fec02dd3 1819 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 1820#else
fc36a67e 1821#define DUP2_MAX_FDS 256
1822 int fdtmp[DUP2_MAX_FDS];
79072805 1823 I32 fdx = 0;
ae986130 1824 int fd;
1825
fe14fcc3 1826 if (oldfd == newfd)
fec02dd3 1827 return oldfd;
a687059c 1828 close(newfd);
fc36a67e 1829 /* good enough for low fd's... */
1830 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1831 if (fdx >= DUP2_MAX_FDS) {
1832 close(fd);
1833 fd = -1;
1834 break;
1835 }
ae986130 1836 fdtmp[fdx++] = fd;
fc36a67e 1837 }
ae986130 1838 while (fdx > 0)
1839 close(fdtmp[--fdx]);
fec02dd3 1840 return fd;
62b28dd9 1841#endif
a687059c 1842}
1843#endif
1844
ff68c719 1845
1846#ifdef HAS_SIGACTION
1847
1848Sighandler_t
8ac85365 1849rsignal(int signo, Sighandler_t handler)
ff68c719 1850{
1851 struct sigaction act, oact;
1852
1853 act.sa_handler = handler;
1854 sigemptyset(&act.sa_mask);
1855 act.sa_flags = 0;
1856#ifdef SA_RESTART
1857 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1858#endif
1859 if (sigaction(signo, &act, &oact) == -1)
36477c24 1860 return SIG_ERR;
ff68c719 1861 else
36477c24 1862 return oact.sa_handler;
ff68c719 1863}
1864
1865Sighandler_t
8ac85365 1866rsignal_state(int signo)
ff68c719 1867{
1868 struct sigaction oact;
1869
1870 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1871 return SIG_ERR;
1872 else
1873 return oact.sa_handler;
1874}
1875
1876int
8ac85365 1877rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 1878{
1879 struct sigaction act;
1880
1881 act.sa_handler = handler;
1882 sigemptyset(&act.sa_mask);
1883 act.sa_flags = 0;
1884#ifdef SA_RESTART
1885 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1886#endif
1887 return sigaction(signo, &act, save);
1888}
1889
1890int
8ac85365 1891rsignal_restore(int signo, Sigsave_t *save)
ff68c719 1892{
1893 return sigaction(signo, save, (struct sigaction *)NULL);
1894}
1895
1896#else /* !HAS_SIGACTION */
1897
1898Sighandler_t
1899rsignal(signo, handler)
1900int signo;
1901Sighandler_t handler;
1902{
1903 return signal(signo, handler);
1904}
1905
1906static int sig_trapped;
1907
1908static
1909Signal_t
1910sig_trap(signo)
1911int signo;
1912{
1913 sig_trapped++;
1914}
1915
1916Sighandler_t
1917rsignal_state(signo)
1918int signo;
1919{
1920 Sighandler_t oldsig;
1921
1922 sig_trapped = 0;
1923 oldsig = signal(signo, sig_trap);
1924 signal(signo, oldsig);
1925 if (sig_trapped)
1926 kill(getpid(), signo);
1927 return oldsig;
1928}
1929
1930int
1931rsignal_save(signo, handler, save)
1932int signo;
1933Sighandler_t handler;
1934Sigsave_t *save;
1935{
1936 *save = signal(signo, handler);
1937 return (*save == SIG_ERR) ? -1 : 0;
1938}
1939
1940int
36477c24 1941rsignal_restore(signo, save)
ff68c719 1942int signo;
1943Sigsave_t *save;
1944{
1945 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
1946}
1947
1948#endif /* !HAS_SIGACTION */
1949
5f05dabc 1950 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
1951#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
79072805 1952I32
8ac85365 1953my_pclose(FILE *ptr)
a687059c 1954{
ff68c719 1955 Sigsave_t hstat, istat, qstat;
a687059c 1956 int status;
a0d0e21e 1957 SV **svp;
20188a90 1958 int pid;
03136e13 1959 bool close_failed;
1960 int saved_errno;
1961#ifdef VMS
1962 int saved_vaxc_errno;
1963#endif
a687059c 1964
760ac839 1965 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
748a9306 1966 pid = (int)SvIVX(*svp);
a0d0e21e 1967 SvREFCNT_dec(*svp);
1968 *svp = &sv_undef;
ddcf38b7 1969#ifdef OS2
1970 if (pid == -1) { /* Opened by popen. */
1971 return my_syspclose(ptr);
1972 }
1973#endif
03136e13 1974 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
1975 saved_errno = errno;
1976#ifdef VMS
1977 saved_vaxc_errno = vaxc$errno;
1978#endif
1979 }
7c0587c8 1980#ifdef UTS
1981 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1982#endif
ff68c719 1983 rsignal_save(SIGHUP, SIG_IGN, &hstat);
1984 rsignal_save(SIGINT, SIG_IGN, &istat);
1985 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
748a9306 1986 do {
1987 pid = wait4pid(pid, &status, 0);
1988 } while (pid == -1 && errno == EINTR);
ff68c719 1989 rsignal_restore(SIGHUP, &hstat);
1990 rsignal_restore(SIGINT, &istat);
1991 rsignal_restore(SIGQUIT, &qstat);
03136e13 1992 if (close_failed) {
1993 SETERRNO(saved_errno, saved_vaxc_errno);
1994 return -1;
1995 }
1996 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
20188a90 1997}
4633a7c4 1998#endif /* !DOSISH */
1999
2000#if !defined(DOSISH) || defined(OS2)
79072805 2001I32
8ac85365 2002wait4pid(int pid, int *statusp, int flags)
20188a90 2003{
79072805 2004 SV *sv;
2005 SV** svp;
fc36a67e 2006 char spid[TYPE_CHARS(int)];
20188a90 2007
2008 if (!pid)
2009 return -1;
20188a90 2010 if (pid > 0) {
2011 sprintf(spid, "%d", pid);
79072805 2012 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2013 if (svp && *svp != &sv_undef) {
463ee0b2 2014 *statusp = SvIVX(*svp);
748a9306 2015 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90 2016 return pid;
2017 }
2018 }
2019 else {
79072805 2020 HE *entry;
20188a90 2021
79072805 2022 hv_iterinit(pidstatus);
2023 if (entry = hv_iternext(pidstatus)) {
a0d0e21e 2024 pid = atoi(hv_iterkey(entry,(I32*)statusp));
79072805 2025 sv = hv_iterval(pidstatus,entry);
463ee0b2 2026 *statusp = SvIVX(sv);
20188a90 2027 sprintf(spid, "%d", pid);
748a9306 2028 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90 2029 return pid;
2030 }
2031 }
79072805 2032#ifdef HAS_WAITPID
367f3c24 2033# ifdef HAS_WAITPID_RUNTIME
2034 if (!HAS_WAITPID_RUNTIME)
2035 goto hard_way;
2036# endif
79072805 2037 return waitpid(pid,statusp,flags);
367f3c24 2038#endif
2039#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
a0d0e21e 2040 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
367f3c24 2041#endif
2042#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2043 hard_way:
a0d0e21e 2044 {
2045 I32 result;
2046 if (flags)
2047 croak("Can't do waitpid with flags");
2048 else {
2049 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2050 pidgone(result,*statusp);
2051 if (result < 0)
2052 *statusp = -1;
2053 }
2054 return result;
a687059c 2055 }
2056#endif
a687059c 2057}
7c0587c8 2058#endif /* !DOSISH */
a687059c 2059
7c0587c8 2060void
de3bb511 2061/*SUPPRESS 590*/
8ac85365 2062pidgone(int pid, int status)
a687059c 2063{
79072805 2064 register SV *sv;
fc36a67e 2065 char spid[TYPE_CHARS(int)];
a687059c 2066
20188a90 2067 sprintf(spid, "%d", pid);
79072805 2068 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
a0d0e21e 2069 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 2070 SvIVX(sv) = status;
20188a90 2071 return;
a687059c 2072}
2073
55497cff 2074#if defined(atarist) || defined(OS2) || defined(DJGPP)
7c0587c8 2075int pclose();
ddcf38b7 2076#ifdef HAS_FORK
2077int /* Cannot prototype with I32
2078 in os2ish.h. */
2079my_syspclose(ptr)
2080#else
79072805 2081I32
2082my_pclose(ptr)
ddcf38b7 2083#endif
760ac839 2084PerlIO *ptr;
a687059c 2085{
760ac839 2086 /* Needs work for PerlIO ! */
2087 FILE *f = PerlIO_findFILE(ptr);
2088 I32 result = pclose(f);
2089 PerlIO_releaseFILE(ptr,f);
2090 return result;
a687059c 2091}
7c0587c8 2092#endif
9f68db38 2093
2094void
8ac85365 2095repeatcpy(register char *to, register char *from, I32 len, register I32 count)
9f68db38 2096{
79072805 2097 register I32 todo;
9f68db38 2098 register char *frombase = from;
2099
2100 if (len == 1) {
2101 todo = *from;
2102 while (count-- > 0)
2103 *to++ = todo;
2104 return;
2105 }
2106 while (count-- > 0) {
2107 for (todo = len; todo > 0; todo--) {
2108 *to++ = *from++;
2109 }
2110 from = frombase;
2111 }
2112}
0f85fab0 2113
2114#ifndef CASTNEGFLOAT
463ee0b2 2115U32
79072805 2116cast_ulong(f)
0f85fab0 2117double f;
2118{
2119 long along;
2120
27e2fb84 2121#if CASTFLAGS & 2
34de22dd 2122# define BIGDOUBLE 2147483648.0
2123 if (f >= BIGDOUBLE)
2124 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2125#endif
0f85fab0 2126 if (f >= 0.0)
2127 return (unsigned long)f;
2128 along = (long)f;
2129 return (unsigned long)along;
2130}
ed6116ce 2131# undef BIGDOUBLE
2132#endif
2133
2134#ifndef CASTI32
5d94fbed 2135
5d94fbed 2136/* Unfortunately, on some systems the cast_uv() function doesn't
2137 work with the system-supplied definition of ULONG_MAX. The
2138 comparison (f >= ULONG_MAX) always comes out true. It must be a
2139 problem with the compiler constant folding.
2140
2141 In any case, this workaround should be fine on any two's complement
2142 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2143 ccflags.
2144 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2145*/
1eb770ff 2146
2147/* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2148 of LONG_(MIN/MAX).
2149 -- Kenneth Albanowski <kjahds@kjahds.com>
2150*/
2151
20cec16a 2152#ifndef MY_UV_MAX
2153# define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
5d94fbed 2154#endif
2155
ed6116ce 2156I32
2157cast_i32(f)
2158double f;
2159{
20cec16a 2160 if (f >= I32_MAX)
2161 return (I32) I32_MAX;
2162 if (f <= I32_MIN)
2163 return (I32) I32_MIN;
ed6116ce 2164 return (I32) f;
2165}
a0d0e21e 2166
2167IV
2168cast_iv(f)
2169double f;
2170{
20cec16a 2171 if (f >= IV_MAX)
2172 return (IV) IV_MAX;
2173 if (f <= IV_MIN)
2174 return (IV) IV_MIN;
a0d0e21e 2175 return (IV) f;
2176}
5d94fbed 2177
2178UV
2179cast_uv(f)
2180double f;
2181{
20cec16a 2182 if (f >= MY_UV_MAX)
2183 return (UV) MY_UV_MAX;
5d94fbed 2184 return (UV) f;
2185}
2186
0f85fab0 2187#endif
62b28dd9 2188
fe14fcc3 2189#ifndef HAS_RENAME
79072805 2190I32
62b28dd9 2191same_dirent(a,b)
2192char *a;
2193char *b;
2194{
93a17b20 2195 char *fa = strrchr(a,'/');
2196 char *fb = strrchr(b,'/');
62b28dd9 2197 struct stat tmpstatbuf1;
2198 struct stat tmpstatbuf2;
46fc3d4c 2199 SV *tmpsv = sv_newmortal();
62b28dd9 2200
2201 if (fa)
2202 fa++;
2203 else
2204 fa = a;
2205 if (fb)
2206 fb++;
2207 else
2208 fb = b;
2209 if (strNE(a,b))
2210 return FALSE;
2211 if (fa == a)
46fc3d4c 2212 sv_setpv(tmpsv, ".");
62b28dd9 2213 else
46fc3d4c 2214 sv_setpvn(tmpsv, a, fa - a);
2215 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
62b28dd9 2216 return FALSE;
2217 if (fb == b)
46fc3d4c 2218 sv_setpv(tmpsv, ".");
62b28dd9 2219 else
46fc3d4c 2220 sv_setpvn(tmpsv, b, fb - b);
2221 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
62b28dd9 2222 return FALSE;
2223 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2224 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2225}
fe14fcc3 2226#endif /* !HAS_RENAME */
2227
55497cff 2228UV
8ac85365 2229scan_oct(char *start, I32 len, I32 *retlen)
fe14fcc3 2230{
2231 register char *s = start;
55497cff 2232 register UV retval = 0;
2233 bool overflowed = FALSE;
fe14fcc3 2234
748a9306 2235 while (len && *s >= '0' && *s <= '7') {
55497cff 2236 register UV n = retval << 3;
2237 if (!overflowed && (n >> 3) != retval) {
2238 warn("Integer overflow in octal number");
2239 overflowed = TRUE;
2240 }
2241 retval = n | (*s++ - '0');
748a9306 2242 len--;
fe14fcc3 2243 }
748a9306 2244 if (dowarn && len && (*s == '8' || *s == '9'))
2245 warn("Illegal octal digit ignored");
fe14fcc3 2246 *retlen = s - start;
2247 return retval;
2248}
2249
71be2cbc 2250UV
8ac85365 2251scan_hex(char *start, I32 len, I32 *retlen)
fe14fcc3 2252{
2253 register char *s = start;
55497cff 2254 register UV retval = 0;
2255 bool overflowed = FALSE;
fe14fcc3 2256 char *tmp;
2257
93a17b20 2258 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
55497cff 2259 register UV n = retval << 4;
2260 if (!overflowed && (n >> 4) != retval) {
2261 warn("Integer overflow in hex number");
2262 overflowed = TRUE;
2263 }
2264 retval = n | (tmp - hexdigit) & 15;
fe14fcc3 2265 s++;
2266 }
2267 *retlen = s - start;
2268 return retval;
2269}
760ac839 2270
11343788 2271#ifdef USE_THREADS
12ca11f6 2272#ifdef FAKE_THREADS
2273/* Very simplistic scheduler for now */
2274void
2275schedule(void)
2276{
c7848ba1 2277 thr = thr->i.next_run;
12ca11f6 2278}
2279
2280void
2281perl_cond_init(cp)
2282perl_cond *cp;
2283{
2284 *cp = 0;
2285}
2286
2287void
2288perl_cond_signal(cp)
2289perl_cond *cp;
2290{
2291 perl_thread t;
2292 perl_cond cond = *cp;
2293
2294 if (!cond)
2295 return;
2296 t = cond->thread;
2297 /* Insert t in the runnable queue just ahead of us */
c7848ba1 2298 t->i.next_run = thr->i.next_run;
2299 thr->i.next_run->i.prev_run = t;
2300 t->i.prev_run = thr;
2301 thr->i.next_run = t;
2302 thr->i.wait_queue = 0;
12ca11f6 2303 /* Remove from the wait queue */
2304 *cp = cond->next;
2305 Safefree(cond);
2306}
2307
2308void
2309perl_cond_broadcast(cp)
2310perl_cond *cp;
2311{
2312 perl_thread t;
2313 perl_cond cond, cond_next;
2314
2315 for (cond = *cp; cond; cond = cond_next) {
2316 t = cond->thread;
2317 /* Insert t in the runnable queue just ahead of us */
c7848ba1 2318 t->i.next_run = thr->i.next_run;
2319 thr->i.next_run->i.prev_run = t;
2320 t->i.prev_run = thr;
2321 thr->i.next_run = t;
2322 thr->i.wait_queue = 0;
12ca11f6 2323 /* Remove from the wait queue */
2324 cond_next = cond->next;
2325 Safefree(cond);
2326 }
2327 *cp = 0;
2328}
2329
2330void
2331perl_cond_wait(cp)
2332perl_cond *cp;
2333{
2334 perl_cond cond;
2335
c7848ba1 2336 if (thr->i.next_run == thr)
12ca11f6 2337 croak("panic: perl_cond_wait called by last runnable thread");
2338
0f15f207 2339 New(666, cond, 1, struct perl_wait_queue);
12ca11f6 2340 cond->thread = thr;
2341 cond->next = *cp;
2342 *cp = cond;
c7848ba1 2343 thr->i.wait_queue = cond;
12ca11f6 2344 /* Remove ourselves from runnable queue */
c7848ba1 2345 thr->i.next_run->i.prev_run = thr->i.prev_run;
2346 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6 2347}
2348#endif /* FAKE_THREADS */
2349
11343788 2350#ifdef OLD_PTHREADS_API
2351struct thread *
2352getTHR _((void))
2353{
2354 pthread_addr_t t;
2355
2356 if (pthread_getspecific(thr_key, &t))
2357 croak("panic: pthread_getspecific");
2358 return (struct thread *) t;
2359}
2360#endif /* OLD_PTHREADS_API */
f93b4edd 2361
2362MAGIC *
8ac85365 2363condpair_magic(SV *sv)
f93b4edd 2364{
2365 MAGIC *mg;
2366
2367 SvUPGRADE(sv, SVt_PVMG);
2368 mg = mg_find(sv, 'm');
2369 if (!mg) {
2370 condpair_t *cp;
2371
2372 New(53, cp, 1, condpair_t);
2373 MUTEX_INIT(&cp->mutex);
2374 COND_INIT(&cp->owner_cond);
2375 COND_INIT(&cp->cond);
2376 cp->owner = 0;
2377 MUTEX_LOCK(&sv_mutex);
2378 mg = mg_find(sv, 'm');
2379 if (mg) {
2380 /* someone else beat us to initialising it */
2381 MUTEX_UNLOCK(&sv_mutex);
2382 MUTEX_DESTROY(&cp->mutex);
2383 COND_DESTROY(&cp->owner_cond);
2384 COND_DESTROY(&cp->cond);
2385 Safefree(cp);
2386 }
2387 else {
2388 sv_magic(sv, Nullsv, 'm', 0, 0);
2389 mg = SvMAGIC(sv);
2390 mg->mg_ptr = (char *)cp;
2391 mg->mg_len = sizeof(cp);
2392 MUTEX_UNLOCK(&sv_mutex);
bc1f4c86 2393 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
c7848ba1 2394 "%p: condpair_magic %p\n", thr, sv));)
f93b4edd 2395 }
2396 }
2397 return mg;
2398}
11343788 2399#endif /* USE_THREADS */
760ac839 2400
2401#ifdef HUGE_VAL
2402/*
2403 * This hack is to force load of "huge" support from libm.a
2404 * So it is in perl for (say) POSIX to use.
2405 * Needed for SunOS with Sun's 'acc' for example.
2406 */
2407double
8ac85365 2408Perl_huge(void)
760ac839 2409{
2410 return HUGE_VAL;
2411}
2412#endif