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