This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
remove spurious #endif in last change
[perl5.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a687059c 4 *
d48672a2
LW
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
LW
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
e1dfb34b 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
LW
28# include <unistd.h>
29#endif
30
a687059c
LW
31#ifdef I_VFORK
32# include <vfork.h>
33#endif
34
94b6baf5
AD
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
LW
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
a0d0e21e 56
8c52afec
IZ
57static void xstat _((int));
58long xcount[MAXXCOUNT];
59long lastxcount[MAXXCOUNT];
60long xycount[MAXXCOUNT][MAXYCOUNT];
61long lastxycount[MAXXCOUNT][MAXYCOUNT];
62
a0d0e21e 63#endif
a863c7d1 64
55497cff 65#ifndef MYMALLOC
de3bb511 66
8d063cd8
LW
67/* paranoid version of malloc */
68
a687059c
LW
69/* NOTE: Do not call the next three routines directly. Use the macros
70 * in handy.h, so that we can easily redefine everything to do tracking of
71 * allocated hunks back to the original New to track down any memory leaks.
20cec16a 72 * XXX This advice seems to be widely ignored :-( --AD August 1996.
a687059c
LW
73 */
74
bd4080b3 75Malloc_t
f0f333f4 76safemalloc(MEM_SIZE size)
8d063cd8 77{
bd4080b3 78 Malloc_t ptr;
55497cff 79#ifdef HAS_64K_LIMIT
62b28dd9 80 if (size > 0xffff) {
760ac839 81 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
79072805 82 my_exit(1);
62b28dd9 83 }
55497cff 84#endif /* HAS_64K_LIMIT */
34de22dd
LW
85#ifdef DEBUGGING
86 if ((long)size < 0)
463ee0b2 87 croak("panic: malloc");
34de22dd 88#endif
6ad3d225 89 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
79072805 90#if !(defined(I286) || defined(atarist))
3280af22 91 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
79072805 92#else
6b88bc9c 93 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
8d063cd8
LW
94#endif
95 if (ptr != Nullch)
96 return ptr;
3280af22 97 else if (PL_nomemok)
7c0587c8 98 return Nullch;
8d063cd8 99 else {
22c35a8c 100 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
79072805 101 my_exit(1);
4e35701f 102 return Nullch;
8d063cd8
LW
103 }
104 /*NOTREACHED*/
105}
106
107/* paranoid version of realloc */
108
bd4080b3 109Malloc_t
f0f333f4 110saferealloc(Malloc_t where,MEM_SIZE size)
8d063cd8 111{
bd4080b3 112 Malloc_t ptr;
ecfc5424 113#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
6ad3d225 114 Malloc_t PerlMem_realloc();
ecfc5424 115#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
8d063cd8 116
55497cff 117#ifdef HAS_64K_LIMIT
5f05dabc 118 if (size > 0xffff) {
119 PerlIO_printf(PerlIO_stderr(),
120 "Reallocation too large: %lx\n", size) FLUSH;
121 my_exit(1);
122 }
55497cff 123#endif /* HAS_64K_LIMIT */
7614df0c
JD
124 if (!size) {
125 safefree(where);
126 return NULL;
127 }
128
378cc40b 129 if (!where)
7614df0c 130 return safemalloc(size);
34de22dd
LW
131#ifdef DEBUGGING
132 if ((long)size < 0)
463ee0b2 133 croak("panic: realloc");
34de22dd 134#endif
7614df0c 135 ptr = PerlMem_realloc(where,size);
79072805
LW
136
137#if !(defined(I286) || defined(atarist))
138 DEBUG_m( {
3280af22
NIS
139 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,PL_an++);
140 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
79072805
LW
141 } )
142#else
143 DEBUG_m( {
6b88bc9c
GS
144 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,PL_an++);
145 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
79072805 146 } )
8d063cd8 147#endif
79072805 148
8d063cd8
LW
149 if (ptr != Nullch)
150 return ptr;
3280af22 151 else if (PL_nomemok)
7c0587c8 152 return Nullch;
8d063cd8 153 else {
22c35a8c 154 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
79072805 155 my_exit(1);
4e35701f 156 return Nullch;
8d063cd8
LW
157 }
158 /*NOTREACHED*/
159}
160
161/* safe version of free */
162
54310121 163Free_t
f0f333f4 164safefree(Malloc_t where)
8d063cd8 165{
79072805 166#if !(defined(I286) || defined(atarist))
3280af22 167 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++));
79072805 168#else
6b88bc9c 169 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,PL_an++));
8d063cd8 170#endif
378cc40b 171 if (where) {
de3bb511 172 /*SUPPRESS 701*/
6ad3d225 173 PerlMem_free(where);
378cc40b 174 }
8d063cd8
LW
175}
176
1050c9ca 177/* safe version of calloc */
178
bd4080b3 179Malloc_t
f0f333f4 180safecalloc(MEM_SIZE count, MEM_SIZE size)
1050c9ca 181{
bd4080b3 182 Malloc_t ptr;
1050c9ca 183
55497cff 184#ifdef HAS_64K_LIMIT
5f05dabc 185 if (size * count > 0xffff) {
186 PerlIO_printf(PerlIO_stderr(),
187 "Allocation too large: %lx\n", size * count) FLUSH;
188 my_exit(1);
189 }
55497cff 190#endif /* HAS_64K_LIMIT */
1050c9ca 191#ifdef DEBUGGING
192 if ((long)size < 0 || (long)count < 0)
193 croak("panic: calloc");
194#endif
0b7c1c42 195 size *= count;
6ad3d225 196 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
1050c9ca 197#if !(defined(I286) || defined(atarist))
3280af22 198 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
1050c9ca 199#else
6b88bc9c 200 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
1050c9ca 201#endif
1050c9ca 202 if (ptr != Nullch) {
203 memset((void*)ptr, 0, size);
204 return ptr;
205 }
3280af22 206 else if (PL_nomemok)
1050c9ca 207 return Nullch;
208 else {
22c35a8c 209 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
1050c9ca 210 my_exit(1);
4e35701f 211 return Nullch;
1050c9ca 212 }
213 /*NOTREACHED*/
214}
215
55497cff 216#endif /* !MYMALLOC */
de3bb511 217
a687059c
LW
218#ifdef LEAKTEST
219
8c52afec
IZ
220struct mem_test_strut {
221 union {
222 long type;
223 char c[2];
224 } u;
225 long size;
226};
227
228# define ALIGN sizeof(struct mem_test_strut)
229
230# define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size)
231# define typeof_chunk(ch) \
232 (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100)
233# define set_typeof_chunk(ch,t) \
234 (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100)
235#define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \
236 ? MAXYCOUNT - 1 \
237 : ( (size) > 40 \
238 ? ((size) - 1)/8 + 5 \
239 : ((size) - 1)/4))
8d063cd8 240
bd4080b3 241Malloc_t
f0f333f4 242safexmalloc(I32 x, MEM_SIZE size)
8d063cd8 243{
8c52afec 244 register char* where = (char*)safemalloc(size + ALIGN);
8d063cd8 245
8c52afec
IZ
246 xcount[x] += size;
247 xycount[x][SIZE_TO_Y(size)]++;
248 set_typeof_chunk(where, x);
249 sizeof_chunk(where) = size;
250 return (Malloc_t)(where + ALIGN);
8d063cd8 251}
8d063cd8 252
bd4080b3 253Malloc_t
8c52afec 254safexrealloc(Malloc_t wh, MEM_SIZE size)
a687059c 255{
8c52afec
IZ
256 char *where = (char*)wh;
257
258 if (!wh)
259 return safexmalloc(0,size);
260
261 {
262 MEM_SIZE old = sizeof_chunk(where - ALIGN);
263 int t = typeof_chunk(where - ALIGN);
264 register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN);
265
266 xycount[t][SIZE_TO_Y(old)]--;
267 xycount[t][SIZE_TO_Y(size)]++;
268 xcount[t] += size - old;
269 sizeof_chunk(new) = size;
270 return (Malloc_t)(new + ALIGN);
271 }
a687059c
LW
272}
273
274void
8c52afec 275safexfree(Malloc_t wh)
a687059c 276{
79072805 277 I32 x;
8c52afec
IZ
278 char *where = (char*)wh;
279 MEM_SIZE size;
280
a687059c
LW
281 if (!where)
282 return;
283 where -= ALIGN;
8c52afec 284 size = sizeof_chunk(where);
a687059c 285 x = where[0] + 100 * where[1];
8c52afec
IZ
286 xcount[x] -= size;
287 xycount[x][SIZE_TO_Y(size)]--;
a687059c
LW
288 safefree(where);
289}
290
bd4080b3 291Malloc_t
f0f333f4 292safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
1050c9ca 293{
8c52afec
IZ
294 register char * where = (char*)safexmalloc(x, size * count + ALIGN);
295 xcount[x] += size;
296 xycount[x][SIZE_TO_Y(size)]++;
297 memset((void*)(where + ALIGN), 0, size * count);
298 set_typeof_chunk(where, x);
299 sizeof_chunk(where) = size;
300 return (Malloc_t)(where + ALIGN);
1050c9ca 301}
302
7c0587c8 303static void
8c52afec 304xstat(int flag)
8d063cd8 305{
8c52afec
IZ
306 register I32 i, j, total = 0;
307 I32 subtot[MAXYCOUNT];
8d063cd8 308
8c52afec
IZ
309 for (j = 0; j < MAXYCOUNT; j++) {
310 subtot[j] = 0;
311 }
312
313 PerlIO_printf(PerlIO_stderr(), " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total);
a687059c 314 for (i = 0; i < MAXXCOUNT; i++) {
8c52afec
IZ
315 total += xcount[i];
316 for (j = 0; j < MAXYCOUNT; j++) {
317 subtot[j] += xycount[i][j];
318 }
319 if (flag == 0
320 ? xcount[i] /* Have something */
321 : (flag == 2
322 ? xcount[i] != lastxcount[i] /* Changed */
323 : xcount[i] > lastxcount[i])) { /* Growed */
324 PerlIO_printf(PerlIO_stderr(),"%2d %02d %7ld ", i / 100, i % 100,
325 flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]);
a687059c 326 lastxcount[i] = xcount[i];
8c52afec
IZ
327 for (j = 0; j < MAXYCOUNT; j++) {
328 if ( flag == 0
329 ? xycount[i][j] /* Have something */
330 : (flag == 2
331 ? xycount[i][j] != lastxycount[i][j] /* Changed */
332 : xycount[i][j] > lastxycount[i][j])) { /* Growed */
333 PerlIO_printf(PerlIO_stderr(),"%3ld ",
334 flag == 2
335 ? xycount[i][j] - lastxycount[i][j]
336 : xycount[i][j]);
337 lastxycount[i][j] = xycount[i][j];
338 } else {
339 PerlIO_printf(PerlIO_stderr(), " . ", xycount[i][j]);
340 }
341 }
342 PerlIO_printf(PerlIO_stderr(), "\n");
343 }
344 }
345 if (flag != 2) {
346 PerlIO_printf(PerlIO_stderr(), "Total %7ld ", total);
347 for (j = 0; j < MAXYCOUNT; j++) {
348 if (subtot[j]) {
349 PerlIO_printf(PerlIO_stderr(), "%3ld ", subtot[j]);
350 } else {
351 PerlIO_printf(PerlIO_stderr(), " . ");
352 }
8d063cd8 353 }
8c52afec 354 PerlIO_printf(PerlIO_stderr(), "\n");
8d063cd8 355 }
8d063cd8 356}
a687059c
LW
357
358#endif /* LEAKTEST */
8d063cd8
LW
359
360/* copy a string up to some (non-backslashed) delimiter, if any */
361
362char *
8ac85365 363delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
8d063cd8 364{
fc36a67e 365 register I32 tolen;
366 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b
LW
367 if (*from == '\\') {
368 if (from[1] == delim)
369 from++;
fc36a67e 370 else {
371 if (to < toend)
372 *to++ = *from;
373 tolen++;
374 from++;
375 }
378cc40b 376 }
bedebaa5 377 else if (*from == delim)
8d063cd8 378 break;
fc36a67e 379 if (to < toend)
380 *to++ = *from;
8d063cd8 381 }
bedebaa5
CS
382 if (to < toend)
383 *to = '\0';
fc36a67e 384 *retlen = tolen;
8d063cd8
LW
385 return from;
386}
387
388/* return ptr to little string in big string, NULL if not found */
378cc40b 389/* This routine was donated by Corey Satten. */
8d063cd8
LW
390
391char *
8ac85365 392instr(register char *big, register char *little)
378cc40b
LW
393{
394 register char *s, *x;
79072805 395 register I32 first;
378cc40b 396
a687059c
LW
397 if (!little)
398 return big;
399 first = *little++;
378cc40b
LW
400 if (!first)
401 return big;
402 while (*big) {
403 if (*big++ != first)
404 continue;
405 for (x=big,s=little; *s; /**/ ) {
406 if (!*x)
407 return Nullch;
408 if (*s++ != *x++) {
409 s--;
410 break;
411 }
412 }
413 if (!*s)
414 return big-1;
415 }
416 return Nullch;
417}
8d063cd8 418
a687059c
LW
419/* same as instr but allow embedded nulls */
420
421char *
8ac85365 422ninstr(register char *big, register char *bigend, char *little, char *lend)
8d063cd8 423{
a687059c 424 register char *s, *x;
79072805 425 register I32 first = *little;
a687059c 426 register char *littleend = lend;
378cc40b 427
a0d0e21e 428 if (!first && little >= littleend)
a687059c 429 return big;
de3bb511
LW
430 if (bigend - big < littleend - little)
431 return Nullch;
a687059c
LW
432 bigend -= littleend - little++;
433 while (big <= bigend) {
434 if (*big++ != first)
435 continue;
436 for (x=big,s=little; s < littleend; /**/ ) {
437 if (*s++ != *x++) {
438 s--;
439 break;
440 }
441 }
442 if (s >= littleend)
443 return big-1;
378cc40b 444 }
a687059c
LW
445 return Nullch;
446}
447
448/* reverse of the above--find last substring */
449
450char *
8ac85365 451rninstr(register char *big, char *bigend, char *little, char *lend)
a687059c
LW
452{
453 register char *bigbeg;
454 register char *s, *x;
79072805 455 register I32 first = *little;
a687059c
LW
456 register char *littleend = lend;
457
a0d0e21e 458 if (!first && little >= littleend)
a687059c
LW
459 return bigend;
460 bigbeg = big;
461 big = bigend - (littleend - little++);
462 while (big >= bigbeg) {
463 if (*big-- != first)
464 continue;
465 for (x=big+2,s=little; s < littleend; /**/ ) {
466 if (*s++ != *x++) {
467 s--;
468 break;
469 }
470 }
471 if (s >= littleend)
472 return big+1;
378cc40b 473 }
a687059c 474 return Nullch;
378cc40b 475}
a687059c 476
bbce6d69 477/*
478 * Set up for a new ctype locale.
479 */
55497cff 480void
8ac85365 481perl_new_ctype(char *newctype)
ef7eada9 482{
36477c24 483#ifdef USE_LOCALE_CTYPE
484
bbce6d69 485 int i;
ef7eada9 486
bbce6d69 487 for (i = 0; i < 256; i++) {
488 if (isUPPER_LC(i))
22c35a8c 489 PL_fold_locale[i] = toLOWER_LC(i);
bbce6d69 490 else if (isLOWER_LC(i))
22c35a8c 491 PL_fold_locale[i] = toUPPER_LC(i);
bbce6d69 492 else
22c35a8c 493 PL_fold_locale[i] = i;
bbce6d69 494 }
bbce6d69 495
36477c24 496#endif /* USE_LOCALE_CTYPE */
497}
bbce6d69 498
499/*
500 * Set up for a new collation locale.
501 */
502void
8ac85365 503perl_new_collate(char *newcoll)
bbce6d69 504{
36477c24 505#ifdef USE_LOCALE_COLLATE
506
bbce6d69 507 if (! newcoll) {
3280af22
NIS
508 if (PL_collation_name) {
509 ++PL_collation_ix;
510 Safefree(PL_collation_name);
511 PL_collation_name = NULL;
512 PL_collation_standard = TRUE;
513 PL_collxfrm_base = 0;
514 PL_collxfrm_mult = 2;
bbce6d69 515 }
516 return;
517 }
518
3280af22
NIS
519 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
520 ++PL_collation_ix;
521 Safefree(PL_collation_name);
522 PL_collation_name = savepv(newcoll);
523 PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
bbce6d69 524
bbce6d69 525 {
526 /* 2: at most so many chars ('a', 'b'). */
527 /* 50: surely no system expands a char more. */
528#define XFRMBUFSIZE (2 * 50)
529 char xbuf[XFRMBUFSIZE];
530 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
531 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
532 SSize_t mult = fb - fa;
533 if (mult < 1)
534 croak("strxfrm() gets absurd");
3280af22
NIS
535 PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0;
536 PL_collxfrm_mult = mult;
bbce6d69 537 }
bbce6d69 538 }
bbce6d69 539
36477c24 540#endif /* USE_LOCALE_COLLATE */
541}
bbce6d69 542
543/*
544 * Set up for a new numeric locale.
545 */
546void
8ac85365 547perl_new_numeric(char *newnum)
bbce6d69 548{
36477c24 549#ifdef USE_LOCALE_NUMERIC
550
bbce6d69 551 if (! newnum) {
3280af22
NIS
552 if (PL_numeric_name) {
553 Safefree(PL_numeric_name);
554 PL_numeric_name = NULL;
555 PL_numeric_standard = TRUE;
556 PL_numeric_local = TRUE;
bbce6d69 557 }
558 return;
559 }
560
3280af22
NIS
561 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
562 Safefree(PL_numeric_name);
563 PL_numeric_name = savepv(newnum);
564 PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
565 PL_numeric_local = TRUE;
bbce6d69 566 }
36477c24 567
568#endif /* USE_LOCALE_NUMERIC */
bbce6d69 569}
570
571void
8ac85365 572perl_set_numeric_standard(void)
bbce6d69 573{
5f05dabc 574#ifdef USE_LOCALE_NUMERIC
575
3280af22 576 if (! PL_numeric_standard) {
bbce6d69 577 setlocale(LC_NUMERIC, "C");
3280af22
NIS
578 PL_numeric_standard = TRUE;
579 PL_numeric_local = FALSE;
bbce6d69 580 }
5f05dabc 581
582#endif /* USE_LOCALE_NUMERIC */
bbce6d69 583}
584
585void
8ac85365 586perl_set_numeric_local(void)
bbce6d69 587{
5f05dabc 588#ifdef USE_LOCALE_NUMERIC
589
3280af22
NIS
590 if (! PL_numeric_local) {
591 setlocale(LC_NUMERIC, PL_numeric_name);
592 PL_numeric_standard = FALSE;
593 PL_numeric_local = TRUE;
bbce6d69 594 }
bbce6d69 595
36477c24 596#endif /* USE_LOCALE_NUMERIC */
5f05dabc 597}
36477c24 598
bbce6d69 599
36477c24 600/*
601 * Initialize locale awareness.
602 */
f0c5b223 603int
8ac85365 604perl_init_i18nl10n(int printwarn)
f0c5b223
TB
605{
606 int ok = 1;
607 /* returns
608 * 1 = set ok or not applicable,
609 * 0 = fallback to C locale,
610 * -1 = fallback to C locale failed
611 */
bbce6d69 612
36477c24 613#ifdef USE_LOCALE
bbce6d69 614
36477c24 615#ifdef USE_LOCALE_CTYPE
bbce6d69 616 char *curctype = NULL;
36477c24 617#endif /* USE_LOCALE_CTYPE */
618#ifdef USE_LOCALE_COLLATE
bbce6d69 619 char *curcoll = NULL;
36477c24 620#endif /* USE_LOCALE_COLLATE */
621#ifdef USE_LOCALE_NUMERIC
bbce6d69 622 char *curnum = NULL;
36477c24 623#endif /* USE_LOCALE_NUMERIC */
3aeabbed
JH
624#ifdef __GLIBC__
625 char *language = PerlEnv_getenv("LANGUAGE");
626#endif
76e3520e
GS
627 char *lc_all = PerlEnv_getenv("LC_ALL");
628 char *lang = PerlEnv_getenv("LANG");
bbce6d69 629 bool setlocale_failure = FALSE;
f0c5b223 630
02b32252
CS
631#ifdef LOCALE_ENVIRON_REQUIRED
632
633 /*
634 * Ultrix setlocale(..., "") fails if there are no environment
635 * variables from which to get a locale name.
636 */
637
638 bool done = FALSE;
639
640#ifdef LC_ALL
641 if (lang) {
642 if (setlocale(LC_ALL, ""))
643 done = TRUE;
644 else
645 setlocale_failure = TRUE;
646 }
0644c9cb 647 if (!setlocale_failure) {
02b32252 648#ifdef USE_LOCALE_CTYPE
0644c9cb
IS
649 if (! (curctype =
650 setlocale(LC_CTYPE,
651 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
02b32252
CS
652 ? "" : Nullch)))
653 setlocale_failure = TRUE;
654#endif /* USE_LOCALE_CTYPE */
655#ifdef USE_LOCALE_COLLATE
0644c9cb
IS
656 if (! (curcoll =
657 setlocale(LC_COLLATE,
658 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
02b32252
CS
659 ? "" : Nullch)))
660 setlocale_failure = TRUE;
661#endif /* USE_LOCALE_COLLATE */
662#ifdef USE_LOCALE_NUMERIC
0644c9cb
IS
663 if (! (curnum =
664 setlocale(LC_NUMERIC,
665 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
02b32252
CS
666 ? "" : Nullch)))
667 setlocale_failure = TRUE;
668#endif /* USE_LOCALE_NUMERIC */
669 }
670
0644c9cb 671#endif /* LC_ALL */
02b32252 672
0644c9cb 673#endif /* !LOCALE_ENVIRON_REQUIRED */
5f05dabc 674
0644c9cb 675#ifdef LC_ALL
bbce6d69 676 if (! setlocale(LC_ALL, ""))
677 setlocale_failure = TRUE;
0644c9cb 678#endif /* LC_ALL */
bbce6d69 679
0644c9cb 680 if (!setlocale_failure) {
36477c24 681#ifdef USE_LOCALE_CTYPE
0644c9cb
IS
682 if (! (curctype = setlocale(LC_CTYPE, "")))
683 setlocale_failure = TRUE;
36477c24 684#endif /* USE_LOCALE_CTYPE */
685#ifdef USE_LOCALE_COLLATE
0644c9cb
IS
686 if (! (curcoll = setlocale(LC_COLLATE, "")))
687 setlocale_failure = TRUE;
36477c24 688#endif /* USE_LOCALE_COLLATE */
689#ifdef USE_LOCALE_NUMERIC
0644c9cb
IS
690 if (! (curnum = setlocale(LC_NUMERIC, "")))
691 setlocale_failure = TRUE;
36477c24 692#endif /* USE_LOCALE_NUMERIC */
0644c9cb 693 }
02b32252 694
5f05dabc 695 if (setlocale_failure) {
696 char *p;
697 bool locwarn = (printwarn > 1 ||
698 printwarn &&
76e3520e 699 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)));
20cec16a 700
5f05dabc 701 if (locwarn) {
702#ifdef LC_ALL
703
704 PerlIO_printf(PerlIO_stderr(),
705 "perl: warning: Setting locale failed.\n");
706
707#else /* !LC_ALL */
708
ef7eada9 709 PerlIO_printf(PerlIO_stderr(),
bbce6d69 710 "perl: warning: Setting locale failed for the categories:\n\t");
36477c24 711#ifdef USE_LOCALE_CTYPE
bbce6d69 712 if (! curctype)
5f05dabc 713 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
36477c24 714#endif /* USE_LOCALE_CTYPE */
715#ifdef USE_LOCALE_COLLATE
bbce6d69 716 if (! curcoll)
5f05dabc 717 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
36477c24 718#endif /* USE_LOCALE_COLLATE */
719#ifdef USE_LOCALE_NUMERIC
bbce6d69 720 if (! curnum)
5f05dabc 721 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
36477c24 722#endif /* USE_LOCALE_NUMERIC */
bbce6d69 723 PerlIO_printf(PerlIO_stderr(), "\n");
724
5f05dabc 725#endif /* LC_ALL */
726
760ac839 727 PerlIO_printf(PerlIO_stderr(),
bbce6d69 728 "perl: warning: Please check that your locale settings:\n");
ef7eada9 729
3aeabbed
JH
730#ifdef __GLIBC__
731 PerlIO_printf(PerlIO_stderr(),
732 "\tLANGUAGE = %c%s%c,\n",
733 language ? '"' : '(',
734 language ? language : "unset",
735 language ? '"' : ')');
736#endif
737
ef7eada9 738 PerlIO_printf(PerlIO_stderr(),
bbce6d69 739 "\tLC_ALL = %c%s%c,\n",
740 lc_all ? '"' : '(',
741 lc_all ? lc_all : "unset",
742 lc_all ? '"' : ')');
5f05dabc 743
744 {
745 char **e;
746 for (e = environ; *e; e++) {
747 if (strnEQ(*e, "LC_", 3)
748 && strnNE(*e, "LC_ALL=", 7)
749 && (p = strchr(*e, '=')))
750 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
fb73857a 751 (int)(p - *e), *e, p + 1);
5f05dabc 752 }
753 }
754
ef7eada9 755 PerlIO_printf(PerlIO_stderr(),
bbce6d69 756 "\tLANG = %c%s%c\n",
5f05dabc 757 lang ? '"' : '(',
bbce6d69 758 lang ? lang : "unset",
759 lang ? '"' : ')');
ef7eada9 760
bbce6d69 761 PerlIO_printf(PerlIO_stderr(),
762 " are supported and installed on your system.\n");
5f05dabc 763 }
ef7eada9 764
5f05dabc 765#ifdef LC_ALL
766
767 if (setlocale(LC_ALL, "C")) {
768 if (locwarn)
769 PerlIO_printf(PerlIO_stderr(),
770 "perl: warning: Falling back to the standard locale (\"C\").\n");
bbce6d69 771 ok = 0;
ef7eada9 772 }
5f05dabc 773 else {
774 if (locwarn)
775 PerlIO_printf(PerlIO_stderr(),
776 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
777 ok = -1;
778 }
bbce6d69 779
5f05dabc 780#else /* ! LC_ALL */
781
782 if (0
36477c24 783#ifdef USE_LOCALE_CTYPE
5f05dabc 784 || !(curctype || setlocale(LC_CTYPE, "C"))
36477c24 785#endif /* USE_LOCALE_CTYPE */
786#ifdef USE_LOCALE_COLLATE
5f05dabc 787 || !(curcoll || setlocale(LC_COLLATE, "C"))
36477c24 788#endif /* USE_LOCALE_COLLATE */
789#ifdef USE_LOCALE_NUMERIC
5f05dabc 790 || !(curnum || setlocale(LC_NUMERIC, "C"))
36477c24 791#endif /* USE_LOCALE_NUMERIC */
5f05dabc 792 )
793 {
794 if (locwarn)
bbce6d69 795 PerlIO_printf(PerlIO_stderr(),
5f05dabc 796 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
797 ok = -1;
bbce6d69 798 }
5f05dabc 799
bbce6d69 800#endif /* ! LC_ALL */
5f05dabc 801
802#ifdef USE_LOCALE_CTYPE
803 curctype = setlocale(LC_CTYPE, Nullch);
804#endif /* USE_LOCALE_CTYPE */
805#ifdef USE_LOCALE_COLLATE
806 curcoll = setlocale(LC_COLLATE, Nullch);
807#endif /* USE_LOCALE_COLLATE */
808#ifdef USE_LOCALE_NUMERIC
809 curnum = setlocale(LC_NUMERIC, Nullch);
810#endif /* USE_LOCALE_NUMERIC */
ef7eada9
JH
811 }
812
36477c24 813#ifdef USE_LOCALE_CTYPE
bbce6d69 814 perl_new_ctype(curctype);
36477c24 815#endif /* USE_LOCALE_CTYPE */
bbce6d69 816
36477c24 817#ifdef USE_LOCALE_COLLATE
bbce6d69 818 perl_new_collate(curcoll);
36477c24 819#endif /* USE_LOCALE_COLLATE */
bbce6d69 820
36477c24 821#ifdef USE_LOCALE_NUMERIC
bbce6d69 822 perl_new_numeric(curnum);
36477c24 823#endif /* USE_LOCALE_NUMERIC */
ef7eada9 824
36477c24 825#endif /* USE_LOCALE */
ef7eada9 826
f0c5b223
TB
827 return ok;
828}
829
bbce6d69 830/* Backwards compatibility. */
831int
8ac85365 832perl_init_i18nl14n(int printwarn)
bbce6d69 833{
5f05dabc 834 return perl_init_i18nl10n(printwarn);
bbce6d69 835}
ef7eada9 836
36477c24 837#ifdef USE_LOCALE_COLLATE
ef7eada9 838
bbce6d69 839/*
840 * mem_collxfrm() is a bit like strxfrm() but with two important
841 * differences. First, it handles embedded NULs. Second, it allocates
842 * a bit more memory than needed for the transformed data itself.
843 * The real transformed data begins at offset sizeof(collationix).
844 * Please see sv_collxfrm() to see how this is used.
845 */
846char *
8ac85365 847mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
bbce6d69 848{
849 char *xbuf;
76e3520e 850 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
bbce6d69 851
852 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
853 /* the +1 is for the terminating NUL. */
854
3280af22 855 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
76e3520e 856 New(171, xbuf, xAlloc, char);
bbce6d69 857 if (! xbuf)
858 goto bad;
859
3280af22
NIS
860 *(U32*)xbuf = PL_collation_ix;
861 xout = sizeof(PL_collation_ix);
bbce6d69 862 for (xin = 0; xin < len; ) {
863 SSize_t xused;
864
865 for (;;) {
76e3520e 866 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
bbce6d69 867 if (xused == -1)
868 goto bad;
76e3520e 869 if (xused < xAlloc - xout)
bbce6d69 870 break;
76e3520e
GS
871 xAlloc = (2 * xAlloc) + 1;
872 Renew(xbuf, xAlloc, char);
bbce6d69 873 if (! xbuf)
874 goto bad;
875 }
ef7eada9 876
bbce6d69 877 xin += strlen(s + xin) + 1;
878 xout += xused;
879
880 /* Embedded NULs are understood but silently skipped
881 * because they make no sense in locale collation. */
882 }
ef7eada9 883
bbce6d69 884 xbuf[xout] = '\0';
3280af22 885 *xlen = xout - sizeof(PL_collation_ix);
bbce6d69 886 return xbuf;
887
888 bad:
889 Safefree(xbuf);
890 *xlen = 0;
891 return NULL;
ef7eada9
JH
892}
893
36477c24 894#endif /* USE_LOCALE_COLLATE */
bbce6d69 895
378cc40b 896void
2779dcf1 897fbm_compile(SV *sv, U32 flags /* not used yet */)
378cc40b 898{
942e002e
GS
899 register U8 *s;
900 register U8 *table;
79072805 901 register U32 i;
0b71040e 902 STRLEN len;
79072805
LW
903 I32 rarest = 0;
904 U32 frequency = 256;
905
942e002e 906 s = (U8*)SvPV_force(sv, len);
07f14f54 907 (void)SvUPGRADE(sv, SVt_PVBM);
c277df42 908 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
748a9306 909 return; /* can't have offsets that big */
02128f11
IZ
910 if (len > 2) {
911 Sv_Grow(sv,len + 258);
912 table = (unsigned char*)(SvPVX(sv) + len + 1);
913 s = table - 2;
914 for (i = 0; i < 256; i++) {
915 table[i] = len;
916 }
917 i = 0;
918 while (s >= (unsigned char*)(SvPVX(sv)))
919 {
920 if (table[*s] == len)
921 table[*s] = i;
922 s--,i++;
923 }
378cc40b 924 }
bbce6d69 925 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
79072805 926 SvVALID_on(sv);
378cc40b 927
463ee0b2 928 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
bbce6d69 929 for (i = 0; i < len; i++) {
22c35a8c 930 if (PL_freq[s[i]] < frequency) {
bbce6d69 931 rarest = i;
22c35a8c 932 frequency = PL_freq[s[i]];
378cc40b
LW
933 }
934 }
79072805
LW
935 BmRARE(sv) = s[rarest];
936 BmPREVIOUS(sv) = rarest;
760ac839 937 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
378cc40b
LW
938}
939
378cc40b 940char *
411d5715 941fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
378cc40b 942{
a687059c 943 register unsigned char *s;
79072805
LW
944 register I32 tmp;
945 register I32 littlelen;
a687059c
LW
946 register unsigned char *little;
947 register unsigned char *table;
948 register unsigned char *olds;
949 register unsigned char *oldlittle;
378cc40b 950
79072805 951 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
a0d0e21e
LW
952 STRLEN len;
953 char *l = SvPV(littlestr,len);
c277df42 954 if (!len) {
02128f11
IZ
955 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
956 substr => we can ignore SvVALID */
3280af22 957 if (PL_multiline) {
02128f11 958 char *t = "\n";
3fe6f2dc
MB
959 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
960 t, t + len))) {
961 return (char*)s;
962 }
02128f11 963 }
c277df42 964 if (bigend > big && bigend[-1] == '\n')
161b471a 965 return (char *)(bigend - 1);
c277df42 966 else
161b471a 967 return (char *) bigend;
c277df42 968 }
d48672a2 969 return (char*)big;
c277df42 970 }
a0d0e21e 971 return ninstr((char*)big,(char*)bigend, l, l + len);
d48672a2 972 }
378cc40b 973
79072805 974 littlelen = SvCUR(littlestr);
3280af22 975 if (SvTAIL(littlestr) && !PL_multiline) { /* tail anchored? */
0f85fab0
LW
976 if (littlelen > bigend - big)
977 return Nullch;
463ee0b2 978 little = (unsigned char*)SvPVX(littlestr);
bbce6d69 979 s = bigend - littlelen;
02128f11
IZ
980 if (s > big
981 && bigend[-1] == '\n'
982 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
983 return (char*)s - 1; /* how sweet it is */
984 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 985 return (char*)s; /* how sweet it is */
02128f11
IZ
986 return Nullch;
987 }
988 if (littlelen <= 2) {
989 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
990 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
991 /* This may do extra comparisons if littlelen == 2, but this
992 should be hidden in the noise since we do less indirection. */
993
994 s = big;
995 bigend -= littlelen;
996 while (s <= bigend) {
997 if (s[0] == c1
998 && (littlelen == 1 || s[1] == c2)
999 && (!SvTAIL(littlestr)
1000 || s == bigend
1001 || s[littlelen] == '\n')) /* Automatically multiline */
3fe6f2dc
MB
1002 {
1003 return (char*)s;
1004 }
02128f11 1005 s++;
a687059c 1006 }
bbce6d69 1007 return Nullch;
a687059c 1008 }
463ee0b2 1009 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
62b28dd9
LW
1010 if (--littlelen >= bigend - big)
1011 return Nullch;
1012 s = big + littlelen;
a687059c 1013 oldlittle = little = table - 2;
bbce6d69 1014 if (s < bigend) {
1015 top2:
1016 /*SUPPRESS 560*/
1017 if (tmp = table[*s]) {
62b28dd9 1018#ifdef POINTERRIGOR
bbce6d69 1019 if (bigend - s > tmp) {
1020 s += tmp;
1021 goto top2;
1022 }
62b28dd9 1023#else
bbce6d69 1024 if ((s += tmp) < bigend)
1025 goto top2;
62b28dd9 1026#endif
bbce6d69 1027 return Nullch;
a687059c 1028 }
bbce6d69 1029 else {
1030 tmp = littlelen; /* less expensive than calling strncmp() */
1031 olds = s;
1032 while (tmp--) {
1033 if (*--s == *--little)
1034 continue;
c277df42 1035 differ:
bbce6d69 1036 s = olds + 1; /* here we pay the price for failure */
1037 little = oldlittle;
1038 if (s < bigend) /* fake up continue to outer loop */
62b28dd9 1039 goto top2;
62b28dd9 1040 return Nullch;
a687059c 1041 }
c277df42
IZ
1042 if (SvTAIL(littlestr) /* automatically multiline */
1043 && olds + 1 != bigend
1044 && olds[1] != '\n')
1045 goto differ;
bbce6d69 1046 return (char *)s;
378cc40b
LW
1047 }
1048 }
1049 return Nullch;
1050}
1051
c277df42
IZ
1052/* start_shift, end_shift are positive quantities which give offsets
1053 of ends of some substring of bigstr.
1054 If `last' we want the last occurence.
1055 old_posp is the way of communication between consequent calls if
1056 the next call needs to find the .
1057 The initial *old_posp should be -1.
1058 Note that we do not take into account SvTAIL, so it may give wrong
1059 positives if _ALL flag is set.
1060 */
1061
378cc40b 1062char *
c277df42 1063screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
378cc40b 1064{
5c0ca799 1065 dTHR;
a687059c
LW
1066 register unsigned char *s, *x;
1067 register unsigned char *big;
79072805
LW
1068 register I32 pos;
1069 register I32 previous;
1070 register I32 first;
a687059c 1071 register unsigned char *little;
c277df42 1072 register I32 stop_pos;
a687059c 1073 register unsigned char *littleend;
c277df42 1074 I32 found = 0;
378cc40b 1075
c277df42 1076 if (*old_posp == -1
3280af22
NIS
1077 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
1078 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0))
378cc40b 1079 return Nullch;
463ee0b2 1080 little = (unsigned char *)(SvPVX(littlestr));
79072805 1081 littleend = little + SvCUR(littlestr);
378cc40b 1082 first = *little++;
c277df42 1083 /* The value of pos we can start at: */
79072805 1084 previous = BmPREVIOUS(littlestr);
463ee0b2 1085 big = (unsigned char *)(SvPVX(bigstr));
c277df42
IZ
1086 /* The value of pos we can stop at: */
1087 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1088 if (previous + start_shift > stop_pos) return Nullch;
1089 while (pos < previous + start_shift) {
3280af22 1090 if (!(pos += PL_screamnext[pos]))
378cc40b
LW
1091 return Nullch;
1092 }
de3bb511 1093#ifdef POINTERRIGOR
bbce6d69 1094 do {
ef64f398 1095 if (pos >= stop_pos) break;
bbce6d69 1096 if (big[pos-previous] != first)
1097 continue;
1098 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
bbce6d69 1099 if (*s++ != *x++) {
1100 s--;
1101 break;
de3bb511 1102 }
bbce6d69 1103 }
c277df42
IZ
1104 if (s == littleend) {
1105 *old_posp = pos;
1106 if (!last) return (char *)(big+pos-previous);
1107 found = 1;
1108 }
6b88bc9c 1109 } while ( pos += PL_screamnext[pos] );
c277df42 1110 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
de3bb511
LW
1111#else /* !POINTERRIGOR */
1112 big -= previous;
bbce6d69 1113 do {
ef64f398 1114 if (pos >= stop_pos) break;
bbce6d69 1115 if (big[pos] != first)
1116 continue;
1117 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
bbce6d69 1118 if (*s++ != *x++) {
1119 s--;
1120 break;
378cc40b 1121 }
bbce6d69 1122 }
c277df42
IZ
1123 if (s == littleend) {
1124 *old_posp = pos;
1125 if (!last) return (char *)(big+pos);
1126 found = 1;
1127 }
3280af22 1128 } while ( pos += PL_screamnext[pos] );
c277df42 1129 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
de3bb511 1130#endif /* POINTERRIGOR */
8d063cd8
LW
1131}
1132
79072805 1133I32
8ac85365 1134ibcmp(char *s1, char *s2, register I32 len)
79072805 1135{
bbce6d69 1136 register U8 *a = (U8 *)s1;
1137 register U8 *b = (U8 *)s2;
79072805 1138 while (len--) {
22c35a8c 1139 if (*a != *b && *a != PL_fold[*b])
bbce6d69 1140 return 1;
1141 a++,b++;
1142 }
1143 return 0;
1144}
1145
1146I32
8ac85365 1147ibcmp_locale(char *s1, char *s2, register I32 len)
bbce6d69 1148{
1149 register U8 *a = (U8 *)s1;
1150 register U8 *b = (U8 *)s2;
1151 while (len--) {
22c35a8c 1152 if (*a != *b && *a != PL_fold_locale[*b])
bbce6d69 1153 return 1;
1154 a++,b++;
79072805
LW
1155 }
1156 return 0;
1157}
1158
8d063cd8
LW
1159/* copy a string to a safe spot */
1160
1161char *
8ac85365 1162savepv(char *sv)
8d063cd8 1163{
a687059c 1164 register char *newaddr;
8d063cd8 1165
79072805
LW
1166 New(902,newaddr,strlen(sv)+1,char);
1167 (void)strcpy(newaddr,sv);
8d063cd8
LW
1168 return newaddr;
1169}
1170
a687059c
LW
1171/* same thing but with a known length */
1172
1173char *
8ac85365 1174savepvn(char *sv, register I32 len)
a687059c
LW
1175{
1176 register char *newaddr;
1177
1178 New(903,newaddr,len+1,char);
79072805 1179 Copy(sv,newaddr,len,char); /* might not be null terminated */
a687059c
LW
1180 newaddr[len] = '\0'; /* is now */
1181 return newaddr;
1182}
1183
fc36a67e 1184/* the SV for form() and mess() is not kept in an arena */
1185
76e3520e 1186STATIC SV *
8ac85365 1187mess_alloc(void)
fc36a67e 1188{
e72dc28c 1189 dTHR;
fc36a67e 1190 SV *sv;
1191 XPVMG *any;
1192
e72dc28c
GS
1193 if (!PL_dirty)
1194 return sv_2mortal(newSVpvn("",0));
1195
0372dbb6
GS
1196 if (PL_mess_sv)
1197 return PL_mess_sv;
1198
fc36a67e 1199 /* Create as PVMG now, to avoid any upgrading later */
1200 New(905, sv, 1, SV);
1201 Newz(905, any, 1, XPVMG);
1202 SvFLAGS(sv) = SVt_PVMG;
1203 SvANY(sv) = (void*)any;
1204 SvREFCNT(sv) = 1 << 30; /* practically infinite */
e72dc28c 1205 PL_mess_sv = sv;
fc36a67e 1206 return sv;
1207}
1208
8990e307 1209char *
46fc3d4c 1210form(const char* pat, ...)
8990e307 1211{
e72dc28c 1212 SV *sv = mess_alloc();
46fc3d4c 1213 va_list args;
46fc3d4c 1214 va_start(args, pat);
e72dc28c 1215 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
46fc3d4c 1216 va_end(args);
e72dc28c 1217 return SvPVX(sv);
46fc3d4c 1218}
a687059c 1219
46fc3d4c 1220char *
8ac85365 1221mess(const char *pat, va_list *args)
46fc3d4c 1222{
e72dc28c 1223 SV *sv = mess_alloc();
46fc3d4c 1224 static char dgd[] = " during global destruction.\n";
1225
fc36a67e 1226 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1227 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
e858de61 1228 dTHR;
3280af22 1229 if (PL_dirty)
46fc3d4c 1230 sv_catpv(sv, dgd);
2304df62 1231 else {
3280af22 1232 if (PL_curcop->cop_line)
fc36a67e 1233 sv_catpvf(sv, " at %_ line %ld",
3280af22
NIS
1234 GvSV(PL_curcop->cop_filegv), (long)PL_curcop->cop_line);
1235 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
1236 bool line_mode = (RsSIMPLE(PL_rs) &&
1237 SvLEN(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
46fc3d4c 1238 sv_catpvf(sv, ", <%s> %s %ld",
3280af22 1239 PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
46fc3d4c 1240 line_mode ? "line" : "chunk",
3280af22 1241 (long)IoLINES(GvIOp(PL_last_in_gv)));
2304df62 1242 }
46fc3d4c 1243 sv_catpv(sv, ".\n");
a687059c 1244 }
a687059c 1245 }
46fc3d4c 1246 return SvPVX(sv);
a687059c
LW
1247}
1248
36477c24 1249OP *
71be2cbc 1250die(const char* pat, ...)
36477c24 1251{
5dc0d613 1252 dTHR;
36477c24 1253 va_list args;
1254 char *message;
3280af22 1255 int was_in_eval = PL_in_eval;
36477c24 1256 HV *stash;
1257 GV *gv;
1258 CV *cv;
1259
8b73bbec 1260 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
199100c8 1261 "%p: die: curstack = %p, mainstack = %p\n",
533c011a 1262 thr, PL_curstack, PL_mainstack));
36477c24 1263
36477c24 1264 va_start(args, pat);
4e6ea2c3 1265 message = pat ? mess(pat, &args) : Nullch;
36477c24 1266 va_end(args);
1267
8b73bbec 1268 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
199100c8 1269 "%p: die: message = %s\ndiehook = %p\n",
533c011a 1270 thr, message, PL_diehook));
3280af22 1271 if (PL_diehook) {
1738f5c4 1272 /* sv_2cv might call croak() */
3280af22 1273 SV *olddiehook = PL_diehook;
1738f5c4 1274 ENTER;
3280af22
NIS
1275 SAVESPTR(PL_diehook);
1276 PL_diehook = Nullsv;
1738f5c4
CS
1277 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1278 LEAVE;
1279 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1280 dSP;
774d564b 1281 SV *msg;
1282
1283 ENTER;
4e6ea2c3
GS
1284 if(message) {
1285 msg = newSVpv(message, 0);
1286 SvREADONLY_on(msg);
1287 SAVEFREESV(msg);
1288 }
1289 else {
1290 msg = ERRSV;
1291 }
1738f5c4 1292
e788e7d3 1293 PUSHSTACKi(PERLSI_DIEHOOK);
924508f0 1294 PUSHMARK(SP);
1738f5c4
CS
1295 XPUSHs(msg);
1296 PUTBACK;
1297 perl_call_sv((SV*)cv, G_DISCARD);
d3acc0f7 1298 POPSTACK;
774d564b 1299 LEAVE;
1738f5c4 1300 }
36477c24 1301 }
1302
3280af22 1303 PL_restartop = die_where(message);
8b73bbec 1304 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
7c06b590 1305 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
533c011a 1306 thr, PL_restartop, was_in_eval, PL_top_env));
3280af22 1307 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
6224f72b 1308 JMPENV_JUMP(3);
3280af22 1309 return PL_restartop;
36477c24 1310}
1311
79072805 1312void
71be2cbc 1313croak(const char* pat, ...)
a687059c 1314{
11343788 1315 dTHR;
a687059c 1316 va_list args;
de3bb511 1317 char *message;
748a9306
LW
1318 HV *stash;
1319 GV *gv;
1320 CV *cv;
a687059c 1321
8990e307 1322 va_start(args, pat);
2304df62 1323 message = mess(pat, &args);
a687059c 1324 va_end(args);
8b73bbec 1325 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
3280af22 1326 if (PL_diehook) {
1738f5c4 1327 /* sv_2cv might call croak() */
3280af22 1328 SV *olddiehook = PL_diehook;
1738f5c4 1329 ENTER;
3280af22
NIS
1330 SAVESPTR(PL_diehook);
1331 PL_diehook = Nullsv;
20cec16a 1332 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1738f5c4
CS
1333 LEAVE;
1334 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1335 dSP;
774d564b 1336 SV *msg;
1337
1338 ENTER;
1339 msg = newSVpv(message, 0);
1340 SvREADONLY_on(msg);
1341 SAVEFREESV(msg);
20cec16a 1342
e788e7d3 1343 PUSHSTACKi(PERLSI_DIEHOOK);
924508f0 1344 PUSHMARK(SP);
1738f5c4 1345 XPUSHs(msg);
20cec16a 1346 PUTBACK;
1347 perl_call_sv((SV*)cv, G_DISCARD);
d3acc0f7 1348 POPSTACK;
774d564b 1349 LEAVE;
20cec16a 1350 }
748a9306 1351 }
3280af22
NIS
1352 if (PL_in_eval) {
1353 PL_restartop = die_where(message);
6224f72b 1354 JMPENV_JUMP(3);
a0d0e21e 1355 }
760ac839
LW
1356 PerlIO_puts(PerlIO_stderr(),message);
1357 (void)PerlIO_flush(PerlIO_stderr());
f86702cc 1358 my_failure_exit();
a687059c
LW
1359}
1360
8990e307 1361void
71be2cbc 1362warn(const char* pat,...)
a687059c
LW
1363{
1364 va_list args;
de3bb511 1365 char *message;
748a9306
LW
1366 HV *stash;
1367 GV *gv;
1368 CV *cv;
a687059c 1369
8990e307 1370 va_start(args, pat);
2304df62 1371 message = mess(pat, &args);
a687059c
LW
1372 va_end(args);
1373
3280af22 1374 if (PL_warnhook) {
1738f5c4 1375 /* sv_2cv might call warn() */
11343788 1376 dTHR;
3280af22 1377 SV *oldwarnhook = PL_warnhook;
1738f5c4 1378 ENTER;
3280af22
NIS
1379 SAVESPTR(PL_warnhook);
1380 PL_warnhook = Nullsv;
20cec16a 1381 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1738f5c4
CS
1382 LEAVE;
1383 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1384 dSP;
774d564b 1385 SV *msg;
1386
1387 ENTER;
1388 msg = newSVpv(message, 0);
1389 SvREADONLY_on(msg);
1390 SAVEFREESV(msg);
1391
e788e7d3 1392 PUSHSTACKi(PERLSI_WARNHOOK);
924508f0 1393 PUSHMARK(SP);
774d564b 1394 XPUSHs(msg);
20cec16a 1395 PUTBACK;
1396 perl_call_sv((SV*)cv, G_DISCARD);
d3acc0f7 1397 POPSTACK;
774d564b 1398 LEAVE;
20cec16a 1399 return;
1400 }
748a9306 1401 }
20cec16a 1402 PerlIO_puts(PerlIO_stderr(),message);
a687059c 1403#ifdef LEAKTEST
8c52afec
IZ
1404 DEBUG_L(*message == '!'
1405 ? (xstat(message[1]=='!'
1406 ? (message[2]=='!' ? 2 : 1)
1407 : 0)
1408 , 0)
1409 : 0);
a687059c 1410#endif
20cec16a 1411 (void)PerlIO_flush(PerlIO_stderr());
a687059c 1412}
8d063cd8 1413
599cee73
PM
1414void
1415warner(U32 err, const char* pat,...)
1416{
d008e5eb 1417 dTHR;
599cee73
PM
1418 va_list args;
1419 char *message;
1420 HV *stash;
1421 GV *gv;
1422 CV *cv;
1423
1424 va_start(args, pat);
1425 message = mess(pat, &args);
1426 va_end(args);
1427
1428 if (ckDEAD(err)) {
1429#ifdef USE_THREADS
d008e5eb 1430 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
599cee73
PM
1431#endif /* USE_THREADS */
1432 if (PL_diehook) {
1433 /* sv_2cv might call croak() */
1434 SV *olddiehook = PL_diehook;
1435 ENTER;
1436 SAVESPTR(PL_diehook);
1437 PL_diehook = Nullsv;
1438 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1439 LEAVE;
1440 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1441 dSP;
1442 SV *msg;
1443
1444 ENTER;
1445 msg = newSVpv(message, 0);
1446 SvREADONLY_on(msg);
1447 SAVEFREESV(msg);
1448
1449 PUSHMARK(sp);
1450 XPUSHs(msg);
1451 PUTBACK;
1452 perl_call_sv((SV*)cv, G_DISCARD);
1453
1454 LEAVE;
1455 }
1456 }
1457 if (PL_in_eval) {
1458 PL_restartop = die_where(message);
1459 JMPENV_JUMP(3);
1460 }
1461 PerlIO_puts(PerlIO_stderr(),message);
1462 (void)PerlIO_flush(PerlIO_stderr());
1463 my_failure_exit();
1464
1465 }
1466 else {
1467 if (PL_warnhook) {
1468 /* sv_2cv might call warn() */
1469 dTHR;
1470 SV *oldwarnhook = PL_warnhook;
1471 ENTER;
1472 SAVESPTR(PL_warnhook);
1473 PL_warnhook = Nullsv;
1474 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1475 LEAVE;
1476 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1477 dSP;
1478 SV *msg;
1479
1480 ENTER;
1481 msg = newSVpv(message, 0);
1482 SvREADONLY_on(msg);
1483 SAVEFREESV(msg);
1484
1485 PUSHMARK(sp);
1486 XPUSHs(msg);
1487 PUTBACK;
1488 perl_call_sv((SV*)cv, G_DISCARD);
1489
1490 LEAVE;
1491 return;
1492 }
1493 }
1494 PerlIO_puts(PerlIO_stderr(),message);
1495#ifdef LEAKTEST
1496 DEBUG_L(xstat());
1497#endif
1498 (void)PerlIO_flush(PerlIO_stderr());
1499 }
1500}
1501
a0d0e21e 1502#ifndef VMS /* VMS' my_setenv() is in VMS.c */
3e3baf6d 1503#ifndef WIN32
8d063cd8 1504void
8ac85365 1505my_setenv(char *nam, char *val)
8d063cd8 1506{
79072805 1507 register I32 i=setenv_getix(nam); /* where does it go? */
8d063cd8 1508
3280af22 1509 if (environ == PL_origenviron) { /* need we copy environment? */
79072805
LW
1510 I32 j;
1511 I32 max;
fe14fcc3
LW
1512 char **tmpenv;
1513
de3bb511 1514 /*SUPPRESS 530*/
fe14fcc3
LW
1515 for (max = i; environ[max]; max++) ;
1516 New(901,tmpenv, max+2, char*);
1517 for (j=0; j<max; j++) /* copy environment */
a0d0e21e 1518 tmpenv[j] = savepv(environ[j]);
fe14fcc3
LW
1519 tmpenv[max] = Nullch;
1520 environ = tmpenv; /* tell exec where it is now */
1521 }
a687059c 1522 if (!val) {
e5ebf479 1523 Safefree(environ[i]);
a687059c
LW
1524 while (environ[i]) {
1525 environ[i] = environ[i+1];
1526 i++;
1527 }
1528 return;
1529 }
8d063cd8 1530 if (!environ[i]) { /* does not exist yet */
fe14fcc3 1531 Renew(environ, i+2, char*); /* just expand it a bit */
8d063cd8
LW
1532 environ[i+1] = Nullch; /* make sure it's null terminated */
1533 }
fe14fcc3
LW
1534 else
1535 Safefree(environ[i]);
a687059c 1536 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
62b28dd9 1537#ifndef MSDOS
a687059c 1538 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
62b28dd9
LW
1539#else
1540 /* MS-DOS requires environment variable names to be in uppercase */
fe14fcc3
LW
1541 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1542 * some utilities and applications may break because they only look
1543 * for upper case strings. (Fixed strupr() bug here.)]
1544 */
1545 strcpy(environ[i],nam); strupr(environ[i]);
62b28dd9
LW
1546 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1547#endif /* MSDOS */
8d063cd8
LW
1548}
1549
3e3baf6d 1550#else /* if WIN32 */
68dc0745 1551
1552void
4e35701f 1553my_setenv(char *nam,char *val)
68dc0745 1554{
3e3baf6d
TB
1555
1556#ifdef USE_WIN32_RTL_ENV
1557
68dc0745 1558 register char *envstr;
1559 STRLEN namlen = strlen(nam);
3e3baf6d
TB
1560 STRLEN vallen;
1561 char *oldstr = environ[setenv_getix(nam)];
1562
1563 /* putenv() has totally broken semantics in both the Borland
1564 * and Microsoft CRTLs. They either store the passed pointer in
1565 * the environment without making a copy, or make a copy and don't
1566 * free it. And on top of that, they dont free() old entries that
1567 * are being replaced/deleted. This means the caller must
1568 * free any old entries somehow, or we end up with a memory
1569 * leak every time my_setenv() is called. One might think
1570 * one could directly manipulate environ[], like the UNIX code
1571 * above, but direct changes to environ are not allowed when
1572 * calling putenv(), since the RTLs maintain an internal
1573 * *copy* of environ[]. Bad, bad, *bad* stink.
1574 * GSAR 97-06-07
1575 */
68dc0745 1576
3e3baf6d
TB
1577 if (!val) {
1578 if (!oldstr)
1579 return;
1580 val = "";
1581 vallen = 0;
1582 }
1583 else
1584 vallen = strlen(val);
fc36a67e 1585 New(904, envstr, namlen + vallen + 3, char);
68dc0745 1586 (void)sprintf(envstr,"%s=%s",nam,val);
76e3520e 1587 (void)PerlEnv_putenv(envstr);
3e3baf6d
TB
1588 if (oldstr)
1589 Safefree(oldstr);
1590#ifdef _MSC_VER
1591 Safefree(envstr); /* MSVCRT leaks without this */
1592#endif
1593
1594#else /* !USE_WIN32_RTL_ENV */
1595
1596 /* The sane way to deal with the environment.
1597 * Has these advantages over putenv() & co.:
1598 * * enables us to store a truly empty value in the
1599 * environment (like in UNIX).
1600 * * we don't have to deal with RTL globals, bugs and leaks.
1601 * * Much faster.
1602 * Why you may want to enable USE_WIN32_RTL_ENV:
1603 * * environ[] and RTL functions will not reflect changes,
1604 * which might be an issue if extensions want to access
1605 * the env. via RTL. This cuts both ways, since RTL will
1606 * not see changes made by extensions that call the Win32
1607 * functions directly, either.
1608 * GSAR 97-06-07
1609 */
1610 SetEnvironmentVariable(nam,val);
1611
1612#endif
1613}
1614
1615#endif /* WIN32 */
1616
1617I32
8ac85365 1618setenv_getix(char *nam)
3e3baf6d
TB
1619{
1620 register I32 i, len = strlen(nam);
1621
1622 for (i = 0; environ[i]; i++) {
1623 if (
1624#ifdef WIN32
1625 strnicmp(environ[i],nam,len) == 0
1626#else
1627 strnEQ(environ[i],nam,len)
1628#endif
1629 && environ[i][len] == '=')
1630 break; /* strnEQ must come first to avoid */
1631 } /* potential SEGV's */
1632 return i;
68dc0745 1633}
1634
a0d0e21e 1635#endif /* !VMS */
378cc40b 1636
16d20bd9 1637#ifdef UNLINK_ALL_VERSIONS
79072805 1638I32
378cc40b
LW
1639unlnk(f) /* unlink all versions of a file */
1640char *f;
1641{
79072805 1642 I32 i;
378cc40b 1643
6ad3d225 1644 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
378cc40b
LW
1645 return i ? 0 : -1;
1646}
1647#endif
1648
85e6fe83 1649#if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
378cc40b 1650char *
4e35701f 1651my_bcopy(register char *from,register char *to,register I32 len)
378cc40b
LW
1652{
1653 char *retval = to;
1654
7c0587c8
LW
1655 if (from - to >= 0) {
1656 while (len--)
1657 *to++ = *from++;
1658 }
1659 else {
1660 to += len;
1661 from += len;
1662 while (len--)
faf8582f 1663 *(--to) = *(--from);
7c0587c8 1664 }
378cc40b
LW
1665 return retval;
1666}
ffed7fef 1667#endif
378cc40b 1668
fc36a67e 1669#ifndef HAS_MEMSET
1670void *
1671my_memset(loc,ch,len)
1672register char *loc;
1673register I32 ch;
1674register I32 len;
1675{
1676 char *retval = loc;
1677
1678 while (len--)
1679 *loc++ = ch;
1680 return retval;
1681}
1682#endif
1683
7c0587c8 1684#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1685char *
7c0587c8 1686my_bzero(loc,len)
378cc40b 1687register char *loc;
79072805 1688register I32 len;
378cc40b
LW
1689{
1690 char *retval = loc;
1691
1692 while (len--)
1693 *loc++ = 0;
1694 return retval;
1695}
1696#endif
7c0587c8 1697
36477c24 1698#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1699I32
7c0587c8 1700my_memcmp(s1,s2,len)
36477c24 1701char *s1;
1702char *s2;
79072805 1703register I32 len;
7c0587c8 1704{
36477c24 1705 register U8 *a = (U8 *)s1;
1706 register U8 *b = (U8 *)s2;
79072805 1707 register I32 tmp;
7c0587c8
LW
1708
1709 while (len--) {
36477c24 1710 if (tmp = *a++ - *b++)
7c0587c8
LW
1711 return tmp;
1712 }
1713 return 0;
1714}
36477c24 1715#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1716
fe14fcc3 1717#ifndef HAS_VPRINTF
a687059c 1718
85e6fe83 1719#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1720char *
1721#else
1722int
1723#endif
1724vsprintf(dest, pat, args)
71be2cbc 1725char *dest;
1726const char *pat;
1727char *args;
a687059c
LW
1728{
1729 FILE fakebuf;
1730
1731 fakebuf._ptr = dest;
1732 fakebuf._cnt = 32767;
35c8bce7
LW
1733#ifndef _IOSTRG
1734#define _IOSTRG 0
1735#endif
a687059c
LW
1736 fakebuf._flag = _IOWRT|_IOSTRG;
1737 _doprnt(pat, args, &fakebuf); /* what a kludge */
1738 (void)putc('\0', &fakebuf);
85e6fe83 1739#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1740 return(dest);
1741#else
1742 return 0; /* perl doesn't use return value */
1743#endif
1744}
1745
fe14fcc3 1746#endif /* HAS_VPRINTF */
a687059c
LW
1747
1748#ifdef MYSWAP
ffed7fef 1749#if BYTEORDER != 0x4321
a687059c 1750short
748a9306 1751my_swap(short s)
a687059c
LW
1752{
1753#if (BYTEORDER & 1) == 0
1754 short result;
1755
1756 result = ((s & 255) << 8) + ((s >> 8) & 255);
1757 return result;
1758#else
1759 return s;
1760#endif
1761}
1762
1763long
748a9306 1764my_htonl(long l)
a687059c
LW
1765{
1766 union {
1767 long result;
ffed7fef 1768 char c[sizeof(long)];
a687059c
LW
1769 } u;
1770
ffed7fef 1771#if BYTEORDER == 0x1234
a687059c
LW
1772 u.c[0] = (l >> 24) & 255;
1773 u.c[1] = (l >> 16) & 255;
1774 u.c[2] = (l >> 8) & 255;
1775 u.c[3] = l & 255;
1776 return u.result;
1777#else
ffed7fef 1778#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1779 croak("Unknown BYTEORDER\n");
a687059c 1780#else
79072805
LW
1781 register I32 o;
1782 register I32 s;
a687059c 1783
ffed7fef
LW
1784 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1785 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1786 }
1787 return u.result;
1788#endif
1789#endif
1790}
1791
1792long
748a9306 1793my_ntohl(long l)
a687059c
LW
1794{
1795 union {
1796 long l;
ffed7fef 1797 char c[sizeof(long)];
a687059c
LW
1798 } u;
1799
ffed7fef 1800#if BYTEORDER == 0x1234
a687059c
LW
1801 u.c[0] = (l >> 24) & 255;
1802 u.c[1] = (l >> 16) & 255;
1803 u.c[2] = (l >> 8) & 255;
1804 u.c[3] = l & 255;
1805 return u.l;
1806#else
ffed7fef 1807#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1808 croak("Unknown BYTEORDER\n");
a687059c 1809#else
79072805
LW
1810 register I32 o;
1811 register I32 s;
a687059c
LW
1812
1813 u.l = l;
1814 l = 0;
ffed7fef
LW
1815 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1816 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1817 }
1818 return l;
1819#endif
1820#endif
1821}
1822
ffed7fef 1823#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1824#endif /* MYSWAP */
1825
1826/*
1827 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1828 * If these functions are defined,
1829 * the BYTEORDER is neither 0x1234 nor 0x4321.
1830 * However, this is not assumed.
1831 * -DWS
1832 */
1833
1834#define HTOV(name,type) \
1835 type \
1836 name (n) \
1837 register type n; \
1838 { \
1839 union { \
1840 type value; \
1841 char c[sizeof(type)]; \
1842 } u; \
79072805
LW
1843 register I32 i; \
1844 register I32 s; \
988174c1
LW
1845 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1846 u.c[i] = (n >> s) & 0xFF; \
1847 } \
1848 return u.value; \
1849 }
1850
1851#define VTOH(name,type) \
1852 type \
1853 name (n) \
1854 register type n; \
1855 { \
1856 union { \
1857 type value; \
1858 char c[sizeof(type)]; \
1859 } u; \
79072805
LW
1860 register I32 i; \
1861 register I32 s; \
988174c1
LW
1862 u.value = n; \
1863 n = 0; \
1864 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1865 n += (u.c[i] & 0xFF) << s; \
1866 } \
1867 return n; \
1868 }
1869
1870#if defined(HAS_HTOVS) && !defined(htovs)
1871HTOV(htovs,short)
1872#endif
1873#if defined(HAS_HTOVL) && !defined(htovl)
1874HTOV(htovl,long)
1875#endif
1876#if defined(HAS_VTOHS) && !defined(vtohs)
1877VTOH(vtohs,short)
1878#endif
1879#if defined(HAS_VTOHL) && !defined(vtohl)
1880VTOH(vtohl,long)
1881#endif
a687059c 1882
5f05dabc 1883 /* VMS' my_popen() is in VMS.c, same with OS/2. */
092bebab 1884#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
760ac839 1885PerlIO *
8ac85365 1886my_popen(char *cmd, char *mode)
a687059c
LW
1887{
1888 int p[2];
8ac85365 1889 register I32 This, that;
79072805
LW
1890 register I32 pid;
1891 SV *sv;
1738f5c4 1892 I32 doexec = strNE(cmd,"-");
a687059c 1893
ddcf38b7
IZ
1894#ifdef OS2
1895 if (doexec) {
1896 return my_syspopen(cmd,mode);
1897 }
1898#endif
8ac85365
NIS
1899 This = (*mode == 'w');
1900 that = !This;
3280af22 1901 if (doexec && PL_tainting) {
bbce6d69 1902 taint_env();
1903 taint_proper("Insecure %s%s", "EXEC");
d48672a2 1904 }
c2267164
IZ
1905 if (PerlProc_pipe(p) < 0)
1906 return Nullfp;
a687059c
LW
1907 while ((pid = (doexec?vfork():fork())) < 0) {
1908 if (errno != EAGAIN) {
6ad3d225 1909 PerlLIO_close(p[This]);
a687059c 1910 if (!doexec)
463ee0b2 1911 croak("Can't fork");
a687059c
LW
1912 return Nullfp;
1913 }
1914 sleep(5);
1915 }
1916 if (pid == 0) {
79072805
LW
1917 GV* tmpgv;
1918
30ac6d9b
GS
1919#undef THIS
1920#undef THAT
a687059c 1921#define THIS that
8ac85365 1922#define THAT This
6ad3d225 1923 PerlLIO_close(p[THAT]);
a687059c 1924 if (p[THIS] != (*mode == 'r')) {
6ad3d225
GS
1925 PerlLIO_dup2(p[THIS], *mode == 'r');
1926 PerlLIO_close(p[THIS]);
a687059c
LW
1927 }
1928 if (doexec) {
a0d0e21e 1929#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
1930 int fd;
1931
1932#ifndef NOFILE
1933#define NOFILE 20
1934#endif
6b88bc9c 1935 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
6ad3d225 1936 PerlLIO_close(fd);
ae986130 1937#endif
a687059c 1938 do_exec(cmd); /* may or may not use the shell */
6ad3d225 1939 PerlProc__exit(1);
a687059c 1940 }
de3bb511 1941 /*SUPPRESS 560*/
85e6fe83 1942 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 1943 sv_setiv(GvSV(tmpgv), (IV)getpid());
3280af22
NIS
1944 PL_forkprocess = 0;
1945 hv_clear(PL_pidstatus); /* we have no children */
a687059c
LW
1946 return Nullfp;
1947#undef THIS
1948#undef THAT
1949 }
62b28dd9 1950 do_execfree(); /* free any memory malloced by child on vfork */
6ad3d225 1951 PerlLIO_close(p[that]);
8ac85365 1952 if (p[that] < p[This]) {
6ad3d225
GS
1953 PerlLIO_dup2(p[This], p[that]);
1954 PerlLIO_close(p[This]);
8ac85365 1955 p[This] = p[that];
62b28dd9 1956 }
3280af22 1957 sv = *av_fetch(PL_fdpid,p[This],TRUE);
a0d0e21e 1958 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 1959 SvIVX(sv) = pid;
3280af22 1960 PL_forkprocess = pid;
8ac85365 1961 return PerlIO_fdopen(p[This], mode);
a687059c 1962}
7c0587c8 1963#else
55497cff 1964#if defined(atarist) || defined(DJGPP)
7c0587c8 1965FILE *popen();
760ac839 1966PerlIO *
79072805 1967my_popen(cmd,mode)
7c0587c8
LW
1968char *cmd;
1969char *mode;
1970{
760ac839 1971 /* Needs work for PerlIO ! */
55497cff 1972 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1973 return popen(PerlIO_exportFILE(cmd, 0), mode);
7c0587c8
LW
1974}
1975#endif
1976
1977#endif /* !DOSISH */
a687059c 1978
748a9306 1979#ifdef DUMP_FDS
35ff7856
GS
1980void
1981dump_fds(char *s)
ae986130
LW
1982{
1983 int fd;
1984 struct stat tmpstatbuf;
1985
760ac839 1986 PerlIO_printf(PerlIO_stderr(),"%s", s);
ae986130 1987 for (fd = 0; fd < 32; fd++) {
6ad3d225 1988 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
760ac839 1989 PerlIO_printf(PerlIO_stderr()," %d",fd);
ae986130 1990 }
760ac839 1991 PerlIO_printf(PerlIO_stderr(),"\n");
ae986130 1992}
35ff7856 1993#endif /* DUMP_FDS */
ae986130 1994
fe14fcc3 1995#ifndef HAS_DUP2
fec02dd3 1996int
a687059c
LW
1997dup2(oldfd,newfd)
1998int oldfd;
1999int newfd;
2000{
a0d0e21e 2001#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
2002 if (oldfd == newfd)
2003 return oldfd;
6ad3d225 2004 PerlLIO_close(newfd);
fec02dd3 2005 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 2006#else
fc36a67e 2007#define DUP2_MAX_FDS 256
2008 int fdtmp[DUP2_MAX_FDS];
79072805 2009 I32 fdx = 0;
ae986130
LW
2010 int fd;
2011
fe14fcc3 2012 if (oldfd == newfd)
fec02dd3 2013 return oldfd;
6ad3d225 2014 PerlLIO_close(newfd);
fc36a67e 2015 /* good enough for low fd's... */
6ad3d225 2016 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
fc36a67e 2017 if (fdx >= DUP2_MAX_FDS) {
6ad3d225 2018 PerlLIO_close(fd);
fc36a67e 2019 fd = -1;
2020 break;
2021 }
ae986130 2022 fdtmp[fdx++] = fd;
fc36a67e 2023 }
ae986130 2024 while (fdx > 0)
6ad3d225 2025 PerlLIO_close(fdtmp[--fdx]);
fec02dd3 2026 return fd;
62b28dd9 2027#endif
a687059c
LW
2028}
2029#endif
2030
ff68c719 2031
2032#ifdef HAS_SIGACTION
2033
2034Sighandler_t
8ac85365 2035rsignal(int signo, Sighandler_t handler)
ff68c719 2036{
2037 struct sigaction act, oact;
2038
2039 act.sa_handler = handler;
2040 sigemptyset(&act.sa_mask);
2041 act.sa_flags = 0;
2042#ifdef SA_RESTART
2043 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2044#endif
85264bed
CS
2045#ifdef SA_NOCLDWAIT
2046 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2047 act.sa_flags |= SA_NOCLDWAIT;
2048#endif
ff68c719 2049 if (sigaction(signo, &act, &oact) == -1)
36477c24 2050 return SIG_ERR;
ff68c719 2051 else
36477c24 2052 return oact.sa_handler;
ff68c719 2053}
2054
2055Sighandler_t
8ac85365 2056rsignal_state(int signo)
ff68c719 2057{
2058 struct sigaction oact;
2059
2060 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2061 return SIG_ERR;
2062 else
2063 return oact.sa_handler;
2064}
2065
2066int
8ac85365 2067rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2068{
2069 struct sigaction act;
2070
2071 act.sa_handler = handler;
2072 sigemptyset(&act.sa_mask);
2073 act.sa_flags = 0;
2074#ifdef SA_RESTART
2075 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2076#endif
85264bed
CS
2077#ifdef SA_NOCLDWAIT
2078 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2079 act.sa_flags |= SA_NOCLDWAIT;
2080#endif
ff68c719 2081 return sigaction(signo, &act, save);
2082}
2083
2084int
8ac85365 2085rsignal_restore(int signo, Sigsave_t *save)
ff68c719 2086{
2087 return sigaction(signo, save, (struct sigaction *)NULL);
2088}
2089
2090#else /* !HAS_SIGACTION */
2091
2092Sighandler_t
4e35701f 2093rsignal(int signo, Sighandler_t handler)
ff68c719 2094{
6ad3d225 2095 return PerlProc_signal(signo, handler);
ff68c719 2096}
2097
2098static int sig_trapped;
2099
2100static
2101Signal_t
4e35701f 2102sig_trap(int signo)
ff68c719 2103{
2104 sig_trapped++;
2105}
2106
2107Sighandler_t
4e35701f 2108rsignal_state(int signo)
ff68c719 2109{
2110 Sighandler_t oldsig;
2111
2112 sig_trapped = 0;
6ad3d225
GS
2113 oldsig = PerlProc_signal(signo, sig_trap);
2114 PerlProc_signal(signo, oldsig);
ff68c719 2115 if (sig_trapped)
6ad3d225 2116 PerlProc_kill(getpid(), signo);
ff68c719 2117 return oldsig;
2118}
2119
2120int
4e35701f 2121rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2122{
6ad3d225 2123 *save = PerlProc_signal(signo, handler);
ff68c719 2124 return (*save == SIG_ERR) ? -1 : 0;
2125}
2126
2127int
4e35701f 2128rsignal_restore(int signo, Sigsave_t *save)
ff68c719 2129{
6ad3d225 2130 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
ff68c719 2131}
2132
2133#endif /* !HAS_SIGACTION */
2134
5f05dabc 2135 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
092bebab 2136#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
79072805 2137I32
6acef3b7 2138my_pclose(PerlIO *ptr)
a687059c 2139{
ff68c719 2140 Sigsave_t hstat, istat, qstat;
a687059c 2141 int status;
a0d0e21e 2142 SV **svp;
20188a90 2143 int pid;
1d3434b8 2144 int pid2;
03136e13
CS
2145 bool close_failed;
2146 int saved_errno;
2147#ifdef VMS
2148 int saved_vaxc_errno;
2149#endif
22fae026
TM
2150#ifdef WIN32
2151 int saved_win32_errno;
2152#endif
a687059c 2153
3280af22 2154 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
748a9306 2155 pid = (int)SvIVX(*svp);
a0d0e21e 2156 SvREFCNT_dec(*svp);
3280af22 2157 *svp = &PL_sv_undef;
ddcf38b7
IZ
2158#ifdef OS2
2159 if (pid == -1) { /* Opened by popen. */
2160 return my_syspclose(ptr);
2161 }
2162#endif
03136e13
CS
2163 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2164 saved_errno = errno;
2165#ifdef VMS
2166 saved_vaxc_errno = vaxc$errno;
2167#endif
22fae026
TM
2168#ifdef WIN32
2169 saved_win32_errno = GetLastError();
2170#endif
03136e13 2171 }
7c0587c8 2172#ifdef UTS
6ad3d225 2173 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
7c0587c8 2174#endif
ff68c719 2175 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2176 rsignal_save(SIGINT, SIG_IGN, &istat);
2177 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
748a9306 2178 do {
1d3434b8
GS
2179 pid2 = wait4pid(pid, &status, 0);
2180 } while (pid2 == -1 && errno == EINTR);
ff68c719 2181 rsignal_restore(SIGHUP, &hstat);
2182 rsignal_restore(SIGINT, &istat);
2183 rsignal_restore(SIGQUIT, &qstat);
03136e13
CS
2184 if (close_failed) {
2185 SETERRNO(saved_errno, saved_vaxc_errno);
2186 return -1;
2187 }
1d3434b8 2188 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
20188a90 2189}
4633a7c4
LW
2190#endif /* !DOSISH */
2191
2d7a9237 2192#if !defined(DOSISH) || defined(OS2) || defined(WIN32)
79072805 2193I32
8ac85365 2194wait4pid(int pid, int *statusp, int flags)
20188a90 2195{
79072805
LW
2196 SV *sv;
2197 SV** svp;
fc36a67e 2198 char spid[TYPE_CHARS(int)];
20188a90
LW
2199
2200 if (!pid)
2201 return -1;
20188a90
LW
2202 if (pid > 0) {
2203 sprintf(spid, "%d", pid);
3280af22
NIS
2204 svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE);
2205 if (svp && *svp != &PL_sv_undef) {
463ee0b2 2206 *statusp = SvIVX(*svp);
3280af22 2207 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2208 return pid;
2209 }
2210 }
2211 else {
79072805 2212 HE *entry;
20188a90 2213
3280af22
NIS
2214 hv_iterinit(PL_pidstatus);
2215 if (entry = hv_iternext(PL_pidstatus)) {
a0d0e21e 2216 pid = atoi(hv_iterkey(entry,(I32*)statusp));
3280af22 2217 sv = hv_iterval(PL_pidstatus,entry);
463ee0b2 2218 *statusp = SvIVX(sv);
20188a90 2219 sprintf(spid, "%d", pid);
3280af22 2220 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2221 return pid;
2222 }
2223 }
79072805 2224#ifdef HAS_WAITPID
367f3c24
IZ
2225# ifdef HAS_WAITPID_RUNTIME
2226 if (!HAS_WAITPID_RUNTIME)
2227 goto hard_way;
2228# endif
f55ee38a 2229 return PerlProc_waitpid(pid,statusp,flags);
367f3c24
IZ
2230#endif
2231#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
a0d0e21e 2232 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
367f3c24
IZ
2233#endif
2234#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2235 hard_way:
a0d0e21e
LW
2236 {
2237 I32 result;
2238 if (flags)
2239 croak("Can't do waitpid with flags");
2240 else {
76e3520e 2241 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
a0d0e21e
LW
2242 pidgone(result,*statusp);
2243 if (result < 0)
2244 *statusp = -1;
2245 }
2246 return result;
a687059c
LW
2247 }
2248#endif
a687059c 2249}
2d7a9237 2250#endif /* !DOSISH || OS2 || WIN32 */
a687059c 2251
7c0587c8 2252void
de3bb511 2253/*SUPPRESS 590*/
8ac85365 2254pidgone(int pid, int status)
a687059c 2255{
79072805 2256 register SV *sv;
fc36a67e 2257 char spid[TYPE_CHARS(int)];
a687059c 2258
20188a90 2259 sprintf(spid, "%d", pid);
3280af22 2260 sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
a0d0e21e 2261 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 2262 SvIVX(sv) = status;
20188a90 2263 return;
a687059c
LW
2264}
2265
55497cff 2266#if defined(atarist) || defined(OS2) || defined(DJGPP)
7c0587c8 2267int pclose();
ddcf38b7
IZ
2268#ifdef HAS_FORK
2269int /* Cannot prototype with I32
2270 in os2ish.h. */
2271my_syspclose(ptr)
2272#else
79072805
LW
2273I32
2274my_pclose(ptr)
ddcf38b7 2275#endif
760ac839 2276PerlIO *ptr;
a687059c 2277{
760ac839
LW
2278 /* Needs work for PerlIO ! */
2279 FILE *f = PerlIO_findFILE(ptr);
2280 I32 result = pclose(f);
2281 PerlIO_releaseFILE(ptr,f);
2282 return result;
a687059c 2283}
7c0587c8 2284#endif
9f68db38
LW
2285
2286void
8ac85365 2287repeatcpy(register char *to, register char *from, I32 len, register I32 count)
9f68db38 2288{
79072805 2289 register I32 todo;
9f68db38
LW
2290 register char *frombase = from;
2291
2292 if (len == 1) {
5926133d 2293 register char c = *from;
9f68db38 2294 while (count-- > 0)
5926133d 2295 *to++ = c;
9f68db38
LW
2296 return;
2297 }
2298 while (count-- > 0) {
2299 for (todo = len; todo > 0; todo--) {
2300 *to++ = *from++;
2301 }
2302 from = frombase;
2303 }
2304}
0f85fab0 2305
463ee0b2 2306U32
22c35a8c 2307cast_ulong(double f)
0f85fab0
LW
2308{
2309 long along;
2310
27e2fb84 2311#if CASTFLAGS & 2
34de22dd
LW
2312# define BIGDOUBLE 2147483648.0
2313 if (f >= BIGDOUBLE)
2314 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2315#endif
0f85fab0
LW
2316 if (f >= 0.0)
2317 return (unsigned long)f;
2318 along = (long)f;
2319 return (unsigned long)along;
2320}
ed6116ce 2321# undef BIGDOUBLE
5d94fbed 2322
5d94fbed
AD
2323/* Unfortunately, on some systems the cast_uv() function doesn't
2324 work with the system-supplied definition of ULONG_MAX. The
2325 comparison (f >= ULONG_MAX) always comes out true. It must be a
2326 problem with the compiler constant folding.
2327
2328 In any case, this workaround should be fine on any two's complement
2329 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2330 ccflags.
2331 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2332*/
1eb770ff 2333
2334/* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2335 of LONG_(MIN/MAX).
2336 -- Kenneth Albanowski <kjahds@kjahds.com>
2337*/
2338
20cec16a 2339#ifndef MY_UV_MAX
2340# define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
5d94fbed
AD
2341#endif
2342
ed6116ce 2343I32
22c35a8c 2344cast_i32(double f)
ed6116ce 2345{
20cec16a 2346 if (f >= I32_MAX)
2347 return (I32) I32_MAX;
2348 if (f <= I32_MIN)
2349 return (I32) I32_MIN;
ed6116ce
LW
2350 return (I32) f;
2351}
a0d0e21e
LW
2352
2353IV
22c35a8c 2354cast_iv(double f)
a0d0e21e 2355{
20cec16a 2356 if (f >= IV_MAX)
2357 return (IV) IV_MAX;
2358 if (f <= IV_MIN)
2359 return (IV) IV_MIN;
a0d0e21e
LW
2360 return (IV) f;
2361}
5d94fbed
AD
2362
2363UV
22c35a8c 2364cast_uv(double f)
5d94fbed 2365{
20cec16a 2366 if (f >= MY_UV_MAX)
2367 return (UV) MY_UV_MAX;
5d94fbed
AD
2368 return (UV) f;
2369}
2370
fe14fcc3 2371#ifndef HAS_RENAME
79072805 2372I32
22c35a8c 2373same_dirent(char *a, char *b)
62b28dd9 2374{
93a17b20
LW
2375 char *fa = strrchr(a,'/');
2376 char *fb = strrchr(b,'/');
62b28dd9
LW
2377 struct stat tmpstatbuf1;
2378 struct stat tmpstatbuf2;
46fc3d4c 2379 SV *tmpsv = sv_newmortal();
62b28dd9
LW
2380
2381 if (fa)
2382 fa++;
2383 else
2384 fa = a;
2385 if (fb)
2386 fb++;
2387 else
2388 fb = b;
2389 if (strNE(a,b))
2390 return FALSE;
2391 if (fa == a)
46fc3d4c 2392 sv_setpv(tmpsv, ".");
62b28dd9 2393 else
46fc3d4c 2394 sv_setpvn(tmpsv, a, fa - a);
c6ed36e1 2395 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2396 return FALSE;
2397 if (fb == b)
46fc3d4c 2398 sv_setpv(tmpsv, ".");
62b28dd9 2399 else
46fc3d4c 2400 sv_setpvn(tmpsv, b, fb - b);
c6ed36e1 2401 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2402 return FALSE;
2403 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2404 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2405}
fe14fcc3
LW
2406#endif /* !HAS_RENAME */
2407
55497cff 2408UV
4f19785b
WSI
2409scan_bin(char *start, I32 len, I32 *retlen)
2410{
2411 register char *s = start;
2412 register UV retval = 0;
2413 bool overflowed = FALSE;
2414 while (len && *s >= '0' && *s <= '1') {
2415 register UV n = retval << 1;
2416 if (!overflowed && (n >> 1) != retval) {
2417 warn("Integer overflow in binary number");
2418 overflowed = TRUE;
2419 }
2420 retval = n | (*s++ - '0');
2421 len--;
2422 }
2423 if (len && (*s >= '2' || *s <= '9')) {
2424 dTHR;
2425 if (ckWARN(WARN_UNSAFE))
2426 warner(WARN_UNSAFE, "Illegal binary digit ignored");
2427 }
2428 *retlen = s - start;
2429 return retval;
2430}
2431UV
8ac85365 2432scan_oct(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2433{
2434 register char *s = start;
55497cff 2435 register UV retval = 0;
2436 bool overflowed = FALSE;
fe14fcc3 2437
748a9306 2438 while (len && *s >= '0' && *s <= '7') {
55497cff 2439 register UV n = retval << 3;
2440 if (!overflowed && (n >> 3) != retval) {
2441 warn("Integer overflow in octal number");
2442 overflowed = TRUE;
2443 }
2444 retval = n | (*s++ - '0');
748a9306 2445 len--;
fe14fcc3 2446 }
d008e5eb
GS
2447 if (len && (*s == '8' || *s == '9')) {
2448 dTHR;
2449 if (ckWARN(WARN_OCTAL))
2450 warner(WARN_OCTAL, "Illegal octal digit ignored");
2451 }
fe14fcc3
LW
2452 *retlen = s - start;
2453 return retval;
2454}
2455
71be2cbc 2456UV
8ac85365 2457scan_hex(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2458{
2459 register char *s = start;
55497cff 2460 register UV retval = 0;
2461 bool overflowed = FALSE;
6ff81951 2462 char *tmp = s;
a0ed51b3 2463 register UV n;
fe14fcc3 2464
a0ed51b3
LW
2465 while (len-- && *s) {
2466 tmp = strchr((char *) PL_hexdigit, *s++);
2467 if (!tmp) {
e3fdf988 2468 if (*(s-1) == '_' || (*(s-1) == 'x' && retval == 0))
a0ed51b3
LW
2469 continue;
2470 else {
d008e5eb 2471 dTHR;
a0ed51b3 2472 --s;
599cee73
PM
2473 if (ckWARN(WARN_UNSAFE))
2474 warner(WARN_UNSAFE,"Illegal hex digit ignored");
a0ed51b3
LW
2475 break;
2476 }
2477 }
2478 n = retval << 4;
55497cff 2479 if (!overflowed && (n >> 4) != retval) {
2480 warn("Integer overflow in hex number");
2481 overflowed = TRUE;
2482 }
3280af22 2483 retval = n | ((tmp - PL_hexdigit) & 15);
6ff81951 2484 }
fe14fcc3
LW
2485 *retlen = s - start;
2486 return retval;
2487}
760ac839 2488
491527d0
GS
2489char*
2490find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
2491{
2492 dTHR;
2493 char *xfound = Nullch;
2494 char *xfailed = Nullch;
0f31cffe 2495 char tmpbuf[MAXPATHLEN];
491527d0
GS
2496 register char *s;
2497 I32 len;
2498 int retval;
2499#if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2500# define SEARCH_EXTS ".bat", ".cmd", NULL
2501# define MAX_EXT_LEN 4
2502#endif
2503#ifdef OS2
2504# define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2505# define MAX_EXT_LEN 4
2506#endif
2507#ifdef VMS
2508# define SEARCH_EXTS ".pl", ".com", NULL
2509# define MAX_EXT_LEN 4
2510#endif
2511 /* additional extensions to try in each dir if scriptname not found */
2512#ifdef SEARCH_EXTS
2513 char *exts[] = { SEARCH_EXTS };
2514 char **ext = search_ext ? search_ext : exts;
2515 int extidx = 0, i = 0;
2516 char *curext = Nullch;
2517#else
2518# define MAX_EXT_LEN 0
2519#endif
2520
2521 /*
2522 * If dosearch is true and if scriptname does not contain path
2523 * delimiters, search the PATH for scriptname.
2524 *
2525 * If SEARCH_EXTS is also defined, will look for each
2526 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2527 * while searching the PATH.
2528 *
2529 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2530 * proceeds as follows:
2531 * If DOSISH or VMSISH:
2532 * + look for ./scriptname{,.foo,.bar}
2533 * + search the PATH for scriptname{,.foo,.bar}
2534 *
2535 * If !DOSISH:
2536 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2537 * this will not look in '.' if it's not in the PATH)
2538 */
84486fc6 2539 tmpbuf[0] = '\0';
491527d0
GS
2540
2541#ifdef VMS
2542# ifdef ALWAYS_DEFTYPES
2543 len = strlen(scriptname);
2544 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
2545 int hasdir, idx = 0, deftypes = 1;
2546 bool seen_dot = 1;
2547
2548 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
2549# else
2550 if (dosearch) {
2551 int hasdir, idx = 0, deftypes = 1;
2552 bool seen_dot = 1;
2553
2554 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
2555# endif
2556 /* The first time through, just add SEARCH_EXTS to whatever we
2557 * already have, so we can check for default file types. */
2558 while (deftypes ||
84486fc6 2559 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
491527d0
GS
2560 {
2561 if (deftypes) {
2562 deftypes = 0;
84486fc6 2563 *tmpbuf = '\0';
491527d0 2564 }
84486fc6
GS
2565 if ((strlen(tmpbuf) + strlen(scriptname)
2566 + MAX_EXT_LEN) >= sizeof tmpbuf)
491527d0 2567 continue; /* don't search dir with too-long name */
84486fc6 2568 strcat(tmpbuf, scriptname);
491527d0
GS
2569#else /* !VMS */
2570
2571#ifdef DOSISH
2572 if (strEQ(scriptname, "-"))
2573 dosearch = 0;
2574 if (dosearch) { /* Look in '.' first. */
2575 char *cur = scriptname;
2576#ifdef SEARCH_EXTS
2577 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2578 while (ext[i])
2579 if (strEQ(ext[i++],curext)) {
2580 extidx = -1; /* already has an ext */
2581 break;
2582 }
2583 do {
2584#endif
2585 DEBUG_p(PerlIO_printf(Perl_debug_log,
2586 "Looking for %s\n",cur));
017f25f1
IZ
2587 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
2588 && !S_ISDIR(PL_statbuf.st_mode)) {
491527d0
GS
2589 dosearch = 0;
2590 scriptname = cur;
2591#ifdef SEARCH_EXTS
2592 break;
2593#endif
2594 }
2595#ifdef SEARCH_EXTS
2596 if (cur == scriptname) {
2597 len = strlen(scriptname);
84486fc6 2598 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
491527d0 2599 break;
84486fc6 2600 cur = strcpy(tmpbuf, scriptname);
491527d0
GS
2601 }
2602 } while (extidx >= 0 && ext[extidx] /* try an extension? */
84486fc6 2603 && strcpy(tmpbuf+len, ext[extidx++]));
491527d0
GS
2604#endif
2605 }
2606#endif
2607
2608 if (dosearch && !strchr(scriptname, '/')
2609#ifdef DOSISH
2610 && !strchr(scriptname, '\\')
2611#endif
2612 && (s = PerlEnv_getenv("PATH"))) {
2613 bool seen_dot = 0;
2614
3280af22
NIS
2615 PL_bufend = s + strlen(s);
2616 while (s < PL_bufend) {
491527d0
GS
2617#if defined(atarist) || defined(DOSISH)
2618 for (len = 0; *s
2619# ifdef atarist
2620 && *s != ','
2621# endif
2622 && *s != ';'; len++, s++) {
84486fc6
GS
2623 if (len < sizeof tmpbuf)
2624 tmpbuf[len] = *s;
491527d0 2625 }
84486fc6
GS
2626 if (len < sizeof tmpbuf)
2627 tmpbuf[len] = '\0';
491527d0 2628#else /* ! (atarist || DOSISH) */
3280af22 2629 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
491527d0
GS
2630 ':',
2631 &len);
2632#endif /* ! (atarist || DOSISH) */
3280af22 2633 if (s < PL_bufend)
491527d0 2634 s++;
84486fc6 2635 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
491527d0
GS
2636 continue; /* don't search dir with too-long name */
2637 if (len
61ae2fbf 2638#if defined(atarist) || defined(__MINT__) || defined(DOSISH)
84486fc6
GS
2639 && tmpbuf[len - 1] != '/'
2640 && tmpbuf[len - 1] != '\\'
491527d0
GS
2641#endif
2642 )
84486fc6
GS
2643 tmpbuf[len++] = '/';
2644 if (len == 2 && tmpbuf[0] == '.')
491527d0 2645 seen_dot = 1;
84486fc6 2646 (void)strcpy(tmpbuf + len, scriptname);
491527d0
GS
2647#endif /* !VMS */
2648
2649#ifdef SEARCH_EXTS
84486fc6 2650 len = strlen(tmpbuf);
491527d0
GS
2651 if (extidx > 0) /* reset after previous loop */
2652 extidx = 0;
2653 do {
2654#endif
84486fc6 2655 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3280af22 2656 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
017f25f1
IZ
2657 if (S_ISDIR(PL_statbuf.st_mode)) {
2658 retval = -1;
2659 }
491527d0
GS
2660#ifdef SEARCH_EXTS
2661 } while ( retval < 0 /* not there */
2662 && extidx>=0 && ext[extidx] /* try an extension? */
84486fc6 2663 && strcpy(tmpbuf+len, ext[extidx++])
491527d0
GS
2664 );
2665#endif
2666 if (retval < 0)
2667 continue;
3280af22
NIS
2668 if (S_ISREG(PL_statbuf.st_mode)
2669 && cando(S_IRUSR,TRUE,&PL_statbuf)
491527d0 2670#ifndef DOSISH
3280af22 2671 && cando(S_IXUSR,TRUE,&PL_statbuf)
491527d0
GS
2672#endif
2673 )
2674 {
84486fc6 2675 xfound = tmpbuf; /* bingo! */
491527d0
GS
2676 break;
2677 }
2678 if (!xfailed)
84486fc6 2679 xfailed = savepv(tmpbuf);
491527d0
GS
2680 }
2681#ifndef DOSISH
017f25f1
IZ
2682 if (!xfound && !seen_dot && !xfailed &&
2683 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
2684 || S_ISDIR(PL_statbuf.st_mode)))
491527d0
GS
2685#endif
2686 seen_dot = 1; /* Disable message. */
9ccb31f9
GS
2687 if (!xfound) {
2688 if (flags & 1) { /* do or die? */
2689 croak("Can't %s %s%s%s",
2690 (xfailed ? "execute" : "find"),
2691 (xfailed ? xfailed : scriptname),
2692 (xfailed ? "" : " on PATH"),
2693 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
2694 }
2695 scriptname = Nullch;
2696 }
491527d0
GS
2697 if (xfailed)
2698 Safefree(xfailed);
2699 scriptname = xfound;
2700 }
9ccb31f9 2701 return (scriptname ? savepv(scriptname) : Nullch);
491527d0
GS
2702}
2703
2704
11343788 2705#ifdef USE_THREADS
12ca11f6
MB
2706#ifdef FAKE_THREADS
2707/* Very simplistic scheduler for now */
2708void
2709schedule(void)
2710{
c7848ba1 2711 thr = thr->i.next_run;
12ca11f6
MB
2712}
2713
2714void
22c35a8c 2715perl_cond_init(perl_cond *cp)
12ca11f6
MB
2716{
2717 *cp = 0;
2718}
2719
2720void
22c35a8c 2721perl_cond_signal(perl_cond *cp)
12ca11f6 2722{
51dd5992 2723 perl_os_thread t;
12ca11f6
MB
2724 perl_cond cond = *cp;
2725
2726 if (!cond)
2727 return;
2728 t = cond->thread;
2729 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2730 t->i.next_run = thr->i.next_run;
2731 thr->i.next_run->i.prev_run = t;
2732 t->i.prev_run = thr;
2733 thr->i.next_run = t;
2734 thr->i.wait_queue = 0;
12ca11f6
MB
2735 /* Remove from the wait queue */
2736 *cp = cond->next;
2737 Safefree(cond);
2738}
2739
2740void
22c35a8c 2741perl_cond_broadcast(perl_cond *cp)
12ca11f6 2742{
51dd5992 2743 perl_os_thread t;
12ca11f6
MB
2744 perl_cond cond, cond_next;
2745
2746 for (cond = *cp; cond; cond = cond_next) {
2747 t = cond->thread;
2748 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2749 t->i.next_run = thr->i.next_run;
2750 thr->i.next_run->i.prev_run = t;
2751 t->i.prev_run = thr;
2752 thr->i.next_run = t;
2753 thr->i.wait_queue = 0;
12ca11f6
MB
2754 /* Remove from the wait queue */
2755 cond_next = cond->next;
2756 Safefree(cond);
2757 }
2758 *cp = 0;
2759}
2760
2761void
22c35a8c 2762perl_cond_wait(perl_cond *cp)
12ca11f6
MB
2763{
2764 perl_cond cond;
2765
c7848ba1 2766 if (thr->i.next_run == thr)
12ca11f6
MB
2767 croak("panic: perl_cond_wait called by last runnable thread");
2768
0f15f207 2769 New(666, cond, 1, struct perl_wait_queue);
12ca11f6
MB
2770 cond->thread = thr;
2771 cond->next = *cp;
2772 *cp = cond;
c7848ba1 2773 thr->i.wait_queue = cond;
12ca11f6 2774 /* Remove ourselves from runnable queue */
c7848ba1
MB
2775 thr->i.next_run->i.prev_run = thr->i.prev_run;
2776 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6
MB
2777}
2778#endif /* FAKE_THREADS */
2779
0d85d877 2780#ifdef PTHREAD_GETSPECIFIC_INT
52e1cb5e 2781struct perl_thread *
11343788
MB
2782getTHR _((void))
2783{
2784 pthread_addr_t t;
2785
6b88bc9c 2786 if (pthread_getspecific(PL_thr_key, &t))
11343788 2787 croak("panic: pthread_getspecific");
52e1cb5e 2788 return (struct perl_thread *) t;
11343788 2789}
0d85d877 2790#endif
f93b4edd
MB
2791
2792MAGIC *
8ac85365 2793condpair_magic(SV *sv)
f93b4edd
MB
2794{
2795 MAGIC *mg;
2796
2797 SvUPGRADE(sv, SVt_PVMG);
2798 mg = mg_find(sv, 'm');
2799 if (!mg) {
2800 condpair_t *cp;
2801
2802 New(53, cp, 1, condpair_t);
2803 MUTEX_INIT(&cp->mutex);
2804 COND_INIT(&cp->owner_cond);
2805 COND_INIT(&cp->cond);
2806 cp->owner = 0;
940cb80d 2807 LOCK_SV_MUTEX;
f93b4edd
MB
2808 mg = mg_find(sv, 'm');
2809 if (mg) {
2810 /* someone else beat us to initialising it */
940cb80d 2811 UNLOCK_SV_MUTEX;
f93b4edd
MB
2812 MUTEX_DESTROY(&cp->mutex);
2813 COND_DESTROY(&cp->owner_cond);
2814 COND_DESTROY(&cp->cond);
2815 Safefree(cp);
2816 }
2817 else {
2818 sv_magic(sv, Nullsv, 'm', 0, 0);
2819 mg = SvMAGIC(sv);
2820 mg->mg_ptr = (char *)cp;
565764a8 2821 mg->mg_len = sizeof(cp);
940cb80d 2822 UNLOCK_SV_MUTEX;
8b73bbec 2823 DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
c7848ba1 2824 "%p: condpair_magic %p\n", thr, sv));)
f93b4edd
MB
2825 }
2826 }
2827 return mg;
2828}
a863c7d1
MB
2829
2830/*
199100c8
MB
2831 * Make a new perl thread structure using t as a prototype. Some of the
2832 * fields for the new thread are copied from the prototype thread, t,
2833 * so t should not be running in perl at the time this function is
2834 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2835 * thread calling new_struct_thread) clearly satisfies this constraint.
a863c7d1 2836 */
52e1cb5e
JH
2837struct perl_thread *
2838new_struct_thread(struct perl_thread *t)
a863c7d1 2839{
52e1cb5e 2840 struct perl_thread *thr;
a863c7d1 2841 SV *sv;
199100c8
MB
2842 SV **svp;
2843 I32 i;
2844
2845 sv = newSVpv("", 0);
52e1cb5e
JH
2846 SvGROW(sv, sizeof(struct perl_thread) + 1);
2847 SvCUR_set(sv, sizeof(struct perl_thread));
199100c8 2848 thr = (Thread) SvPVX(sv);
949ced2d 2849#ifdef DEBUGGING
52e1cb5e 2850 memset(thr, 0xab, sizeof(struct perl_thread));
533c011a
NIS
2851 PL_markstack = 0;
2852 PL_scopestack = 0;
2853 PL_savestack = 0;
2854 PL_retstack = 0;
2855 PL_dirty = 0;
2856 PL_localizing = 0;
949ced2d
GS
2857 Zero(&PL_hv_fetch_ent_mh, 1, HE);
2858#else
2859 Zero(thr, 1, struct perl_thread);
2860#endif
199100c8
MB
2861
2862 thr->oursv = sv;
d55594ae 2863 init_stacks(ARGS);
a863c7d1 2864
533c011a 2865 PL_curcop = &PL_compiling;
199100c8 2866 thr->cvcache = newHV();
54b9620d 2867 thr->threadsv = newAV();
a863c7d1 2868 thr->specific = newAV();
38a03e6e
MB
2869 thr->errsv = newSVpv("", 0);
2870 thr->errhv = newHV();
a863c7d1
MB
2871 thr->flags = THRf_R_JOINABLE;
2872 MUTEX_INIT(&thr->mutex);
199100c8 2873
d55594ae
GS
2874 /* top_env needs to be non-zero. It points to an area
2875 in which longjmp() stuff is stored, as C callstack
2876 info there at least is thread specific this has to
2877 be per-thread. Otherwise a 'die' in a thread gives
2878 that thread the C stack of last thread to do an eval {}!
2879 See comments in scope.h
2880 Initialize top entry (as in perl.c for main thread)
2881 */
533c011a
NIS
2882 PL_start_env.je_prev = NULL;
2883 PL_start_env.je_ret = -1;
2884 PL_start_env.je_mustcatch = TRUE;
2885 PL_top_env = &PL_start_env;
2886
2887 PL_in_eval = FALSE;
2888 PL_restartop = 0;
2889
b099ddc0
GS
2890 PL_statname = NEWSV(66,0);
2891 PL_maxscream = -1;
2892 PL_regcompp = FUNC_NAME_TO_PTR(pregcomp);
2893 PL_regexecp = FUNC_NAME_TO_PTR(regexec_flags);
2894 PL_regindent = 0;
2895 PL_reginterp_cnt = 0;
2896 PL_lastscream = Nullsv;
2897 PL_screamfirst = 0;
2898 PL_screamnext = 0;
2899 PL_reg_start_tmp = 0;
2900 PL_reg_start_tmpl = 0;
2901
2902 /* parent thread's data needs to be locked while we make copy */
2903 MUTEX_LOCK(&t->mutex);
2904
2905 PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */
2906 PL_defstash = t->Tdefstash; /* XXX maybe these should */
2907 PL_curstash = t->Tcurstash; /* always be set to main? */
2908
6b88bc9c 2909 PL_tainted = t->Ttainted;
84fee439
NIS
2910 PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */
2911 PL_nrs = newSVsv(t->Tnrs);
2912 PL_rs = SvREFCNT_inc(PL_nrs);
2913 PL_last_in_gv = Nullgv;
2914 PL_ofslen = t->Tofslen;
2915 PL_ofs = savepvn(t->Tofs, PL_ofslen);
2916 PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2917 PL_chopset = t->Tchopset;
2918 PL_formtarget = newSVsv(t->Tformtarget);
2919 PL_bodytarget = newSVsv(t->Tbodytarget);
2920 PL_toptarget = newSVsv(t->Ttoptarget);
533c011a 2921
54b9620d
MB
2922 /* Initialise all per-thread SVs that the template thread used */
2923 svp = AvARRAY(t->threadsv);
93965878 2924 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
533c011a 2925 if (*svp && *svp != &PL_sv_undef) {
199100c8 2926 SV *sv = newSVsv(*svp);
54b9620d 2927 av_store(thr->threadsv, i, sv);
533c011a 2928 sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1);
8b73bbec 2929 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
54b9620d 2930 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
199100c8
MB
2931 }
2932 }
940cb80d 2933 thr->threadsvp = AvARRAY(thr->threadsv);
199100c8 2934
533c011a
NIS
2935 MUTEX_LOCK(&PL_threads_mutex);
2936 PL_nthreads++;
2937 thr->tid = ++PL_threadnum;
199100c8
MB
2938 thr->next = t->next;
2939 thr->prev = t;
2940 t->next = thr;
2941 thr->next->prev = thr;
533c011a 2942 MUTEX_UNLOCK(&PL_threads_mutex);
a863c7d1 2943
b099ddc0
GS
2944 /* done copying parent's state */
2945 MUTEX_UNLOCK(&t->mutex);
2946
a863c7d1
MB
2947#ifdef HAVE_THREAD_INTERN
2948 init_thread_intern(thr);
a863c7d1 2949#endif /* HAVE_THREAD_INTERN */
a863c7d1
MB
2950 return thr;
2951}
11343788 2952#endif /* USE_THREADS */
760ac839
LW
2953
2954#ifdef HUGE_VAL
2955/*
2956 * This hack is to force load of "huge" support from libm.a
2957 * So it is in perl for (say) POSIX to use.
2958 * Needed for SunOS with Sun's 'acc' for example.
2959 */
2960double
8ac85365 2961Perl_huge(void)
760ac839
LW
2962{
2963 return HUGE_VAL;
2964}
2965#endif
4e35701f 2966
22239a37
NIS
2967#ifdef PERL_GLOBAL_STRUCT
2968struct perl_vars *
2969Perl_GetVars(void)
2970{
533c011a 2971 return &PL_Vars;
22239a37 2972}
31fb1209
NIS
2973#endif
2974
2975char **
2976get_op_names(void)
2977{
22c35a8c 2978 return PL_op_name;
31fb1209
NIS
2979}
2980
2981char **
2982get_op_descs(void)
2983{
22c35a8c 2984 return PL_op_desc;
31fb1209 2985}
9e6b2b00
GS
2986
2987char *
2988get_no_modify(void)
2989{
22c35a8c 2990 return (char*)PL_no_modify;
9e6b2b00
GS
2991}
2992
2993U32 *
2994get_opargs(void)
2995{
22c35a8c 2996 return PL_opargs;
9e6b2b00 2997}
51aa15f3 2998
51aa15f3
GS
2999SV **
3000get_specialsv_list(void)
3001{
3280af22 3002 return PL_specialsv_list;
f55ee38a 3003}
dc9e4912
GS
3004
3005
3006MGVTBL*
3007get_vtbl(int vtbl_id)
3008{
3009 MGVTBL* result = Null(MGVTBL*);
3010
3011 switch(vtbl_id) {
3012 case want_vtbl_sv:
3013 result = &PL_vtbl_sv;
3014 break;
3015 case want_vtbl_env:
3016 result = &PL_vtbl_env;
3017 break;
3018 case want_vtbl_envelem:
3019 result = &PL_vtbl_envelem;
3020 break;
3021 case want_vtbl_sig:
3022 result = &PL_vtbl_sig;
3023 break;
3024 case want_vtbl_sigelem:
3025 result = &PL_vtbl_sigelem;
3026 break;
3027 case want_vtbl_pack:
3028 result = &PL_vtbl_pack;
3029 break;
3030 case want_vtbl_packelem:
3031 result = &PL_vtbl_packelem;
3032 break;
3033 case want_vtbl_dbline:
3034 result = &PL_vtbl_dbline;
3035 break;
3036 case want_vtbl_isa:
3037 result = &PL_vtbl_isa;
3038 break;
3039 case want_vtbl_isaelem:
3040 result = &PL_vtbl_isaelem;
3041 break;
3042 case want_vtbl_arylen:
3043 result = &PL_vtbl_arylen;
3044 break;
3045 case want_vtbl_glob:
3046 result = &PL_vtbl_glob;
3047 break;
3048 case want_vtbl_mglob:
3049 result = &PL_vtbl_mglob;
3050 break;
3051 case want_vtbl_nkeys:
3052 result = &PL_vtbl_nkeys;
3053 break;
3054 case want_vtbl_taint:
3055 result = &PL_vtbl_taint;
3056 break;
3057 case want_vtbl_substr:
3058 result = &PL_vtbl_substr;
3059 break;
3060 case want_vtbl_vec:
3061 result = &PL_vtbl_vec;
3062 break;
3063 case want_vtbl_pos:
3064 result = &PL_vtbl_pos;
3065 break;
3066 case want_vtbl_bm:
3067 result = &PL_vtbl_bm;
3068 break;
3069 case want_vtbl_fm:
3070 result = &PL_vtbl_fm;
3071 break;
3072 case want_vtbl_uvar:
3073 result = &PL_vtbl_uvar;
3074 break;
3075#ifdef USE_THREADS
3076 case want_vtbl_mutex:
3077 result = &PL_vtbl_mutex;
3078 break;
3079#endif
3080 case want_vtbl_defelem:
3081 result = &PL_vtbl_defelem;
3082 break;
3083 case want_vtbl_regexp:
3084 result = &PL_vtbl_regexp;
3085 break;
3086 case want_vtbl_regdata:
3087 result = &PL_vtbl_regdata;
3088 break;
3089 case want_vtbl_regdatum:
3090 result = &PL_vtbl_regdatum;
3091 break;
3c90161d 3092#ifdef USE_LOCALE_COLLATE
dc9e4912
GS
3093 case want_vtbl_collxfrm:
3094 result = &PL_vtbl_collxfrm;
3095 break;
3c90161d 3096#endif
dc9e4912
GS
3097 case want_vtbl_amagic:
3098 result = &PL_vtbl_amagic;
3099 break;
3100 case want_vtbl_amagicelem:
3101 result = &PL_vtbl_amagicelem;
3102 break;
3103 }
3104 return result;
3105}
3106