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