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