This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move PERL_FBM_TABLE_OFFSET and PERL_FBM_FLAGS_OFFSET_FROM_TABLE to sv.h
[perl5.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
4bb101f2 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
a687059c 5 *
d48672a2
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
8d063cd8 9 */
a0d0e21e
LW
10
11/*
12 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
13 * not content." --Gandalf
14 */
8d063cd8 15
166f8a29
DM
16/* This file contains assorted utility routines.
17 * Which is a polite way of saying any stuff that people couldn't think of
18 * a better place for. Amongst other things, it includes the warning and
19 * dieing stuff, plus wrappers for malloc code.
20 */
21
8d063cd8 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_UTIL_C
8d063cd8 24#include "perl.h"
62b28dd9 25
64ca3a65 26#ifndef PERL_MICRO
a687059c 27#include <signal.h>
36477c24
PP
28#ifndef SIG_ERR
29# define SIG_ERR ((Sighandler_t) -1)
30#endif
64ca3a65 31#endif
36477c24 32
172d2248
OS
33#ifdef __Lynx__
34/* Missing protos on LynxOS */
35int putenv(char *);
36#endif
37
ff68c719
PP
38#ifdef I_SYS_WAIT
39# include <sys/wait.h>
40#endif
41
868439a2
JH
42#ifdef HAS_SELECT
43# ifdef I_SYS_SELECT
44# include <sys/select.h>
45# endif
46#endif
47
8d063cd8 48#define FLUSH
8d063cd8 49
16cebae2
GS
50#if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
51# define FD_CLOEXEC 1 /* NeXT needs this */
52#endif
53
a687059c
LW
54/* NOTE: Do not call the next three routines directly. Use the macros
55 * in handy.h, so that we can easily redefine everything to do tracking of
56 * allocated hunks back to the original New to track down any memory leaks.
20cec16a 57 * XXX This advice seems to be widely ignored :-( --AD August 1996.
a687059c
LW
58 */
59
ca8d8976
NC
60static char *
61S_write_no_mem(pTHX)
62{
97aff369 63 dVAR;
ca8d8976
NC
64 /* Can't use PerlIO to write as it allocates memory */
65 PerlLIO_write(PerlIO_fileno(Perl_error_log),
66 PL_no_mem, strlen(PL_no_mem));
67 my_exit(1);
1f440eb2 68 NORETURN_FUNCTION_END;
ca8d8976
NC
69}
70
26fa51c3
AMS
71/* paranoid version of system's malloc() */
72
bd4080b3 73Malloc_t
4f63d024 74Perl_safesysmalloc(MEM_SIZE size)
8d063cd8 75{
54aff467 76 dTHX;
bd4080b3 77 Malloc_t ptr;
55497cff 78#ifdef HAS_64K_LIMIT
62b28dd9 79 if (size > 0xffff) {
bf49b057 80 PerlIO_printf(Perl_error_log,
16cebae2 81 "Allocation too large: %lx\n", size) FLUSH;
54aff467 82 my_exit(1);
62b28dd9 83 }
55497cff 84#endif /* HAS_64K_LIMIT */
e8dda941
JD
85#ifdef PERL_TRACK_MEMPOOL
86 size += sTHX;
87#endif
34de22dd
LW
88#ifdef DEBUGGING
89 if ((long)size < 0)
4f63d024 90 Perl_croak_nocontext("panic: malloc");
34de22dd 91#endif
12ae5dfc 92 ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
da927450 93 PERL_ALLOC_CHECK(ptr);
97835f67 94 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
bd61b366 95 if (ptr != NULL) {
e8dda941 96#ifdef PERL_TRACK_MEMPOOL
7cb608b5
NC
97 struct perl_memory_debug_header *const header
98 = (struct perl_memory_debug_header *)ptr;
9a083ecf
NC
99#endif
100
101#ifdef PERL_POISON
7e337ee0 102 PoisonNew(((char *)ptr), size, char);
9a083ecf 103#endif
7cb608b5 104
9a083ecf 105#ifdef PERL_TRACK_MEMPOOL
7cb608b5
NC
106 header->interpreter = aTHX;
107 /* Link us into the list. */
108 header->prev = &PL_memory_debug_header;
109 header->next = PL_memory_debug_header.next;
110 PL_memory_debug_header.next = header;
111 header->next->prev = header;
cd1541b2 112# ifdef PERL_POISON
7cb608b5 113 header->size = size;
cd1541b2 114# endif
e8dda941
JD
115 ptr = (Malloc_t)((char*)ptr+sTHX);
116#endif
8d063cd8 117 return ptr;
e8dda941 118}
3280af22 119 else if (PL_nomemok)
bd61b366 120 return NULL;
8d063cd8 121 else {
0bd48802 122 return write_no_mem();
8d063cd8
LW
123 }
124 /*NOTREACHED*/
125}
126
f2517201 127/* paranoid version of system's realloc() */
8d063cd8 128
bd4080b3 129Malloc_t
4f63d024 130Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
8d063cd8 131{
54aff467 132 dTHX;
bd4080b3 133 Malloc_t ptr;
9a34ef1d 134#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO)
6ad3d225 135 Malloc_t PerlMem_realloc();
ecfc5424 136#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
8d063cd8 137
a1d180c4 138#ifdef HAS_64K_LIMIT
5f05dabc 139 if (size > 0xffff) {
bf49b057 140 PerlIO_printf(Perl_error_log,
5f05dabc 141 "Reallocation too large: %lx\n", size) FLUSH;
54aff467 142 my_exit(1);
5f05dabc 143 }
55497cff 144#endif /* HAS_64K_LIMIT */
7614df0c 145 if (!size) {
f2517201 146 safesysfree(where);
7614df0c
JD
147 return NULL;
148 }
149
378cc40b 150 if (!where)
f2517201 151 return safesysmalloc(size);
e8dda941
JD
152#ifdef PERL_TRACK_MEMPOOL
153 where = (Malloc_t)((char*)where-sTHX);
154 size += sTHX;
7cb608b5
NC
155 {
156 struct perl_memory_debug_header *const header
157 = (struct perl_memory_debug_header *)where;
158
159 if (header->interpreter != aTHX) {
160 Perl_croak_nocontext("panic: realloc from wrong pool");
161 }
162 assert(header->next->prev == header);
163 assert(header->prev->next == header);
cd1541b2 164# ifdef PERL_POISON
7cb608b5
NC
165 if (header->size > size) {
166 const MEM_SIZE freed_up = header->size - size;
167 char *start_of_freed = ((char *)where) + size;
7e337ee0 168 PoisonFree(start_of_freed, freed_up, char);
7cb608b5
NC
169 }
170 header->size = size;
cd1541b2 171# endif
7cb608b5 172 }
e8dda941 173#endif
34de22dd
LW
174#ifdef DEBUGGING
175 if ((long)size < 0)
4f63d024 176 Perl_croak_nocontext("panic: realloc");
34de22dd 177#endif
12ae5dfc 178 ptr = (Malloc_t)PerlMem_realloc(where,size);
da927450 179 PERL_ALLOC_CHECK(ptr);
a1d180c4 180
97835f67
JH
181 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++));
182 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
79072805 183
bd61b366 184 if (ptr != NULL) {
e8dda941 185#ifdef PERL_TRACK_MEMPOOL
7cb608b5
NC
186 struct perl_memory_debug_header *const header
187 = (struct perl_memory_debug_header *)ptr;
188
9a083ecf
NC
189# ifdef PERL_POISON
190 if (header->size < size) {
191 const MEM_SIZE fresh = size - header->size;
192 char *start_of_fresh = ((char *)ptr) + size;
7e337ee0 193 PoisonNew(start_of_fresh, fresh, char);
9a083ecf
NC
194 }
195# endif
196
7cb608b5
NC
197 header->next->prev = header;
198 header->prev->next = header;
199
e8dda941
JD
200 ptr = (Malloc_t)((char*)ptr+sTHX);
201#endif
8d063cd8 202 return ptr;
e8dda941 203 }
3280af22 204 else if (PL_nomemok)
bd61b366 205 return NULL;
8d063cd8 206 else {
0bd48802 207 return write_no_mem();
8d063cd8
LW
208 }
209 /*NOTREACHED*/
210}
211
f2517201 212/* safe version of system's free() */
8d063cd8 213
54310121 214Free_t
4f63d024 215Perl_safesysfree(Malloc_t where)
8d063cd8 216{
e8dda941 217#if defined(PERL_IMPLICIT_SYS) || defined(PERL_TRACK_MEMPOOL)
54aff467 218 dTHX;
97aff369
JH
219#else
220 dVAR;
155aba94 221#endif
97835f67 222 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
378cc40b 223 if (where) {
e8dda941
JD
224#ifdef PERL_TRACK_MEMPOOL
225 where = (Malloc_t)((char*)where-sTHX);
cd1541b2 226 {
7cb608b5
NC
227 struct perl_memory_debug_header *const header
228 = (struct perl_memory_debug_header *)where;
229
230 if (header->interpreter != aTHX) {
231 Perl_croak_nocontext("panic: free from wrong pool");
232 }
233 if (!header->prev) {
cd1541b2
NC
234 Perl_croak_nocontext("panic: duplicate free");
235 }
7cb608b5
NC
236 if (!(header->next) || header->next->prev != header
237 || header->prev->next != header) {
238 Perl_croak_nocontext("panic: bad free");
cd1541b2 239 }
7cb608b5
NC
240 /* Unlink us from the chain. */
241 header->next->prev = header->prev;
242 header->prev->next = header->next;
243# ifdef PERL_POISON
7e337ee0 244 PoisonNew(where, header->size, char);
cd1541b2 245# endif
7cb608b5
NC
246 /* Trigger the duplicate free warning. */
247 header->next = NULL;
248 }
e8dda941 249#endif
6ad3d225 250 PerlMem_free(where);
378cc40b 251 }
8d063cd8
LW
252}
253
f2517201 254/* safe version of system's calloc() */
1050c9ca 255
bd4080b3 256Malloc_t
4f63d024 257Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
1050c9ca 258{
54aff467 259 dTHX;
bd4080b3 260 Malloc_t ptr;
1050c9ca 261
55497cff 262#ifdef HAS_64K_LIMIT
5f05dabc 263 if (size * count > 0xffff) {
bf49b057 264 PerlIO_printf(Perl_error_log,
5f05dabc 265 "Allocation too large: %lx\n", size * count) FLUSH;
54aff467 266 my_exit(1);
5f05dabc 267 }
55497cff 268#endif /* HAS_64K_LIMIT */
1050c9ca
PP
269#ifdef DEBUGGING
270 if ((long)size < 0 || (long)count < 0)
4f63d024 271 Perl_croak_nocontext("panic: calloc");
1050c9ca 272#endif
0b7c1c42 273 size *= count;
e8dda941
JD
274#ifdef PERL_TRACK_MEMPOOL
275 size += sTHX;
276#endif
12ae5dfc 277 ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
da927450 278 PERL_ALLOC_CHECK(ptr);
97835f67 279 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)size));
bd61b366 280 if (ptr != NULL) {
1050c9ca 281 memset((void*)ptr, 0, size);
e8dda941 282#ifdef PERL_TRACK_MEMPOOL
7cb608b5
NC
283 {
284 struct perl_memory_debug_header *const header
285 = (struct perl_memory_debug_header *)ptr;
286
287 header->interpreter = aTHX;
288 /* Link us into the list. */
289 header->prev = &PL_memory_debug_header;
290 header->next = PL_memory_debug_header.next;
291 PL_memory_debug_header.next = header;
292 header->next->prev = header;
cd1541b2 293# ifdef PERL_POISON
7cb608b5 294 header->size = size;
cd1541b2 295# endif
7cb608b5
NC
296 ptr = (Malloc_t)((char*)ptr+sTHX);
297 }
e8dda941 298#endif
1050c9ca
PP
299 return ptr;
300 }
3280af22 301 else if (PL_nomemok)
bd61b366 302 return NULL;
0bd48802 303 return write_no_mem();
1050c9ca
PP
304}
305
cae6d0e5
GS
306/* These must be defined when not using Perl's malloc for binary
307 * compatibility */
308
309#ifndef MYMALLOC
310
311Malloc_t Perl_malloc (MEM_SIZE nbytes)
312{
313 dTHXs;
077a72a9 314 return (Malloc_t)PerlMem_malloc(nbytes);
cae6d0e5
GS
315}
316
317Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
318{
319 dTHXs;
077a72a9 320 return (Malloc_t)PerlMem_calloc(elements, size);
cae6d0e5
GS
321}
322
323Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
324{
325 dTHXs;
077a72a9 326 return (Malloc_t)PerlMem_realloc(where, nbytes);
cae6d0e5
GS
327}
328
329Free_t Perl_mfree (Malloc_t where)
330{
331 dTHXs;
332 PerlMem_free(where);
333}
334
335#endif
336
8d063cd8
LW
337/* copy a string up to some (non-backslashed) delimiter, if any */
338
339char *
e1ec3a88 340Perl_delimcpy(pTHX_ register char *to, register const char *toend, register const char *from, register const char *fromend, register int delim, I32 *retlen)
8d063cd8 341{
fc36a67e 342 register I32 tolen;
96a5add6 343 PERL_UNUSED_CONTEXT;
35da51f7 344
fc36a67e 345 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b 346 if (*from == '\\') {
35da51f7 347 if (from[1] != delim) {
fc36a67e
PP
348 if (to < toend)
349 *to++ = *from;
350 tolen++;
fc36a67e 351 }
35da51f7 352 from++;
378cc40b 353 }
bedebaa5 354 else if (*from == delim)
8d063cd8 355 break;
fc36a67e
PP
356 if (to < toend)
357 *to++ = *from;
8d063cd8 358 }
bedebaa5
CS
359 if (to < toend)
360 *to = '\0';
fc36a67e 361 *retlen = tolen;
73d840c0 362 return (char *)from;
8d063cd8
LW
363}
364
365/* return ptr to little string in big string, NULL if not found */
378cc40b 366/* This routine was donated by Corey Satten. */
8d063cd8
LW
367
368char *
864dbfa3 369Perl_instr(pTHX_ register const char *big, register const char *little)
378cc40b 370{
79072805 371 register I32 first;
96a5add6 372 PERL_UNUSED_CONTEXT;
378cc40b 373
a687059c 374 if (!little)
08105a92 375 return (char*)big;
a687059c 376 first = *little++;
378cc40b 377 if (!first)
08105a92 378 return (char*)big;
378cc40b 379 while (*big) {
66a1b24b 380 register const char *s, *x;
378cc40b
LW
381 if (*big++ != first)
382 continue;
383 for (x=big,s=little; *s; /**/ ) {
384 if (!*x)
bd61b366 385 return NULL;
4fc877ac 386 if (*s != *x)
378cc40b 387 break;
4fc877ac
AL
388 else {
389 s++;
390 x++;
378cc40b
LW
391 }
392 }
393 if (!*s)
08105a92 394 return (char*)(big-1);
378cc40b 395 }
bd61b366 396 return NULL;
378cc40b 397}
8d063cd8 398
a687059c
LW
399/* same as instr but allow embedded nulls */
400
401char *
4c8626be 402Perl_ninstr(pTHX_ const char *big, const char *bigend, const char *little, const char *lend)
8d063cd8 403{
96a5add6 404 PERL_UNUSED_CONTEXT;
4c8626be
GA
405 if (little >= lend)
406 return (char*)big;
407 {
408 char first = *little++;
409 const char *s, *x;
410 bigend -= lend - little;
411 OUTER:
412 while (big <= bigend) {
413 if (*big++ != first)
414 goto OUTER;
415 for (x=big,s=little; s < lend; x++,s++) {
416 if (*s != *x)
417 goto OUTER;
418 }
419 return (char*)(big-1);
420 }
378cc40b 421 }
bd61b366 422 return NULL;
a687059c
LW
423}
424
425/* reverse of the above--find last substring */
426
427char *
864dbfa3 428Perl_rninstr(pTHX_ register const char *big, const char *bigend, const char *little, const char *lend)
a687059c 429{
08105a92 430 register const char *bigbeg;
e1ec3a88 431 register const I32 first = *little;
7452cf6a 432 register const char * const littleend = lend;
96a5add6 433 PERL_UNUSED_CONTEXT;
a687059c 434
260d78c9 435 if (little >= littleend)
08105a92 436 return (char*)bigend;
a687059c
LW
437 bigbeg = big;
438 big = bigend - (littleend - little++);
439 while (big >= bigbeg) {
66a1b24b 440 register const char *s, *x;
a687059c
LW
441 if (*big-- != first)
442 continue;
443 for (x=big+2,s=little; s < littleend; /**/ ) {
4fc877ac 444 if (*s != *x)
a687059c 445 break;
4fc877ac
AL
446 else {
447 x++;
448 s++;
a687059c
LW
449 }
450 }
451 if (s >= littleend)
08105a92 452 return (char*)(big+1);
378cc40b 453 }
bd61b366 454 return NULL;
378cc40b 455}
a687059c 456
cf93c79d
IZ
457/* As a space optimization, we do not compile tables for strings of length
458 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are
459 special-cased in fbm_instr().
460
461 If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
462
954c1994 463/*
ccfc67b7
JH
464=head1 Miscellaneous Functions
465
954c1994
GS
466=for apidoc fbm_compile
467
468Analyses the string in order to make fast searches on it using fbm_instr()
469-- the Boyer-Moore algorithm.
470
471=cut
472*/
473
378cc40b 474void
7506f9c3 475Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
378cc40b 476{
97aff369 477 dVAR;
0d46e09a 478 register const U8 *s;
79072805 479 register U32 i;
0b71040e 480 STRLEN len;
79072805
LW
481 I32 rarest = 0;
482 U32 frequency = 256;
483
c517dc2b 484 if (flags & FBMcf_TAIL) {
890ce7af 485 MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
396482e1 486 sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */
c517dc2b
JH
487 if (mg && mg->mg_len >= 0)
488 mg->mg_len++;
489 }
9cbe880b 490 s = (U8*)SvPV_force_mutable(sv, len);
d1be9408 491 if (len == 0) /* TAIL might be on a zero-length string. */
cf93c79d 492 return;
d07077cc 493 SvUPGRADE(sv, SVt_PVBM);
78d0cf80 494 SvIOK_off(sv);
02128f11 495 if (len > 2) {
9cbe880b 496 const unsigned char *sb;
66a1b24b 497 const U8 mlen = (len>255) ? 255 : (U8)len;
890ce7af 498 register U8 *table;
cf93c79d 499
d8419e03
NC
500 Sv_Grow(sv, len + 256 + PERL_FBM_TABLE_OFFSET);
501 table
502 = (unsigned char*)(SvPVX_mutable(sv) + len + PERL_FBM_TABLE_OFFSET);
503 s = table - 1 - PERL_FBM_TABLE_OFFSET; /* last char */
7506f9c3 504 memset((void*)table, mlen, 256);
c8029a41 505 table[PERL_FBM_FLAGS_OFFSET_FROM_TABLE] = (U8)flags;
02128f11 506 i = 0;
7506f9c3 507 sb = s - mlen + 1; /* first char (maybe) */
cf93c79d
IZ
508 while (s >= sb) {
509 if (table[*s] == mlen)
7506f9c3 510 table[*s] = (U8)i;
cf93c79d
IZ
511 s--, i++;
512 }
378cc40b 513 }
a0714e2c 514 sv_magic(sv, NULL, PERL_MAGIC_bm, NULL, 0); /* deep magic */
79072805 515 SvVALID_on(sv);
378cc40b 516
9cbe880b 517 s = (const unsigned char*)(SvPVX_const(sv)); /* deeper magic */
bbce6d69 518 for (i = 0; i < len; i++) {
22c35a8c 519 if (PL_freq[s[i]] < frequency) {
bbce6d69 520 rarest = i;
22c35a8c 521 frequency = PL_freq[s[i]];
378cc40b
LW
522 }
523 }
79072805 524 BmRARE(sv) = s[rarest];
eb160463 525 BmPREVIOUS(sv) = (U16)rarest;
cf93c79d
IZ
526 BmUSEFUL(sv) = 100; /* Initial value */
527 if (flags & FBMcf_TAIL)
528 SvTAIL_on(sv);
7506f9c3
GS
529 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",
530 BmRARE(sv),BmPREVIOUS(sv)));
378cc40b
LW
531}
532
cf93c79d
IZ
533/* If SvTAIL(littlestr), it has a fake '\n' at end. */
534/* If SvTAIL is actually due to \Z or \z, this gives false positives
535 if multiline */
536
954c1994
GS
537/*
538=for apidoc fbm_instr
539
540Returns the location of the SV in the string delimited by C<str> and
bd61b366 541C<strend>. It returns C<NULL> if the string can't be found. The C<sv>
954c1994
GS
542does not have to be fbm_compiled, but the search will not be as fast
543then.
544
545=cut
546*/
547
378cc40b 548char *
864dbfa3 549Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
378cc40b 550{
a687059c 551 register unsigned char *s;
cf93c79d 552 STRLEN l;
9cbe880b
NC
553 register const unsigned char *little
554 = (const unsigned char *)SvPV_const(littlestr,l);
cf93c79d 555 register STRLEN littlelen = l;
e1ec3a88 556 register const I32 multiline = flags & FBMrf_MULTILINE;
cf93c79d 557
eb160463 558 if ((STRLEN)(bigend - big) < littlelen) {
a1d180c4 559 if ( SvTAIL(littlestr)
eb160463 560 && ((STRLEN)(bigend - big) == littlelen - 1)
a1d180c4 561 && (littlelen == 1
12ae5dfc 562 || (*big == *little &&
27da23d5 563 memEQ((char *)big, (char *)little, littlelen - 1))))
cf93c79d 564 return (char*)big;
bd61b366 565 return NULL;
cf93c79d 566 }
378cc40b 567
cf93c79d 568 if (littlelen <= 2) { /* Special-cased */
cf93c79d
IZ
569
570 if (littlelen == 1) {
571 if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */
572 /* Know that bigend != big. */
573 if (bigend[-1] == '\n')
574 return (char *)(bigend - 1);
575 return (char *) bigend;
576 }
577 s = big;
578 while (s < bigend) {
579 if (*s == *little)
580 return (char *)s;
581 s++;
582 }
583 if (SvTAIL(littlestr))
584 return (char *) bigend;
bd61b366 585 return NULL;
cf93c79d
IZ
586 }
587 if (!littlelen)
588 return (char*)big; /* Cannot be SvTAIL! */
589
590 /* littlelen is 2 */
591 if (SvTAIL(littlestr) && !multiline) {
592 if (bigend[-1] == '\n' && bigend[-2] == *little)
593 return (char*)bigend - 2;
594 if (bigend[-1] == *little)
595 return (char*)bigend - 1;
bd61b366 596 return NULL;
cf93c79d
IZ
597 }
598 {
599 /* This should be better than FBM if c1 == c2, and almost
600 as good otherwise: maybe better since we do less indirection.
601 And we save a lot of memory by caching no table. */
66a1b24b
AL
602 const unsigned char c1 = little[0];
603 const unsigned char c2 = little[1];
cf93c79d
IZ
604
605 s = big + 1;
606 bigend--;
607 if (c1 != c2) {
608 while (s <= bigend) {
609 if (s[0] == c2) {
610 if (s[-1] == c1)
611 return (char*)s - 1;
612 s += 2;
613 continue;
3fe6f2dc 614 }
cf93c79d
IZ
615 next_chars:
616 if (s[0] == c1) {
617 if (s == bigend)
618 goto check_1char_anchor;
619 if (s[1] == c2)
620 return (char*)s;
621 else {
622 s++;
623 goto next_chars;
624 }
625 }
626 else
627 s += 2;
628 }
629 goto check_1char_anchor;
630 }
631 /* Now c1 == c2 */
632 while (s <= bigend) {
633 if (s[0] == c1) {
634 if (s[-1] == c1)
635 return (char*)s - 1;
636 if (s == bigend)
637 goto check_1char_anchor;
638 if (s[1] == c1)
639 return (char*)s;
640 s += 3;
02128f11 641 }
c277df42 642 else
cf93c79d 643 s += 2;
c277df42 644 }
c277df42 645 }
cf93c79d
IZ
646 check_1char_anchor: /* One char and anchor! */
647 if (SvTAIL(littlestr) && (*bigend == *little))
648 return (char *)bigend; /* bigend is already decremented. */
bd61b366 649 return NULL;
d48672a2 650 }
cf93c79d 651 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
bbce6d69 652 s = bigend - littlelen;
a1d180c4 653 if (s >= big && bigend[-1] == '\n' && *s == *little
cf93c79d
IZ
654 /* Automatically of length > 2 */
655 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
7506f9c3 656 {
bbce6d69 657 return (char*)s; /* how sweet it is */
7506f9c3
GS
658 }
659 if (s[1] == *little
660 && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
661 {
cf93c79d 662 return (char*)s + 1; /* how sweet it is */
7506f9c3 663 }
bd61b366 664 return NULL;
02128f11 665 }
cf93c79d 666 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
c4420975 667 char * const b = ninstr((char*)big,(char*)bigend,
cf93c79d
IZ
668 (char*)little, (char*)little + littlelen);
669
670 if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */
671 /* Chop \n from littlestr: */
672 s = bigend - littlelen + 1;
7506f9c3
GS
673 if (*s == *little
674 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
675 {
3fe6f2dc 676 return (char*)s;
7506f9c3 677 }
bd61b366 678 return NULL;
a687059c 679 }
cf93c79d 680 return b;
a687059c 681 }
a1d180c4 682
3566a07d
NC
683 /* Do actual FBM. */
684 if (littlelen > (STRLEN)(bigend - big))
685 return NULL;
686
687 {
d8419e03
NC
688 register const unsigned char * const table
689 = little + littlelen + PERL_FBM_TABLE_OFFSET;
0d46e09a 690 register const unsigned char *oldlittle;
cf93c79d 691
cf93c79d
IZ
692 --littlelen; /* Last char found by table lookup */
693
694 s = big + littlelen;
695 little += littlelen; /* last char */
696 oldlittle = little;
697 if (s < bigend) {
698 register I32 tmp;
699
700 top2:
7506f9c3 701 if ((tmp = table[*s])) {
cf93c79d 702 if ((s += tmp) < bigend)
62b28dd9 703 goto top2;
cf93c79d
IZ
704 goto check_end;
705 }
706 else { /* less expensive than calling strncmp() */
66a1b24b 707 register unsigned char * const olds = s;
cf93c79d
IZ
708
709 tmp = littlelen;
710
711 while (tmp--) {
712 if (*--s == *--little)
713 continue;
cf93c79d
IZ
714 s = olds + 1; /* here we pay the price for failure */
715 little = oldlittle;
716 if (s < bigend) /* fake up continue to outer loop */
717 goto top2;
718 goto check_end;
719 }
720 return (char *)s;
a687059c 721 }
378cc40b 722 }
cf93c79d 723 check_end:
c8029a41
NC
724 if ( s == bigend
725 && (table[PERL_FBM_FLAGS_OFFSET_FROM_TABLE] & FBMcf_TAIL)
12ae5dfc
JH
726 && memEQ((char *)(bigend - littlelen),
727 (char *)(oldlittle - littlelen), littlelen) )
cf93c79d 728 return (char*)bigend - littlelen;
bd61b366 729 return NULL;
378cc40b 730 }
378cc40b
LW
731}
732
c277df42
IZ
733/* start_shift, end_shift are positive quantities which give offsets
734 of ends of some substring of bigstr.
a0288114 735 If "last" we want the last occurrence.
c277df42 736 old_posp is the way of communication between consequent calls if
a1d180c4 737 the next call needs to find the .
c277df42 738 The initial *old_posp should be -1.
cf93c79d
IZ
739
740 Note that we take into account SvTAIL, so one can get extra
741 optimizations if _ALL flag is set.
c277df42
IZ
742 */
743
cf93c79d 744/* If SvTAIL is actually due to \Z or \z, this gives false positives
26fa51c3 745 if PL_multiline. In fact if !PL_multiline the authoritative answer
cf93c79d
IZ
746 is not supported yet. */
747
378cc40b 748char *
864dbfa3 749Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
378cc40b 750{
97aff369 751 dVAR;
0d46e09a 752 register const unsigned char *big;
79072805
LW
753 register I32 pos;
754 register I32 previous;
755 register I32 first;
0d46e09a 756 register const unsigned char *little;
c277df42 757 register I32 stop_pos;
0d46e09a 758 register const unsigned char *littleend;
c277df42 759 I32 found = 0;
378cc40b 760
d372c834
NC
761 assert(SvTYPE(littlestr) == SVt_PVBM);
762
c277df42 763 if (*old_posp == -1
3280af22 764 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
cf93c79d
IZ
765 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) {
766 cant_find:
a1d180c4 767 if ( BmRARE(littlestr) == '\n'
cf93c79d 768 && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) {
cfd0369c 769 little = (const unsigned char *)(SvPVX_const(littlestr));
cf93c79d
IZ
770 littleend = little + SvCUR(littlestr);
771 first = *little++;
772 goto check_tail;
773 }
bd61b366 774 return NULL;
cf93c79d
IZ
775 }
776
cfd0369c 777 little = (const unsigned char *)(SvPVX_const(littlestr));
79072805 778 littleend = little + SvCUR(littlestr);
378cc40b 779 first = *little++;
c277df42 780 /* The value of pos we can start at: */
79072805 781 previous = BmPREVIOUS(littlestr);
cfd0369c 782 big = (const unsigned char *)(SvPVX_const(bigstr));
c277df42
IZ
783 /* The value of pos we can stop at: */
784 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
cf93c79d 785 if (previous + start_shift > stop_pos) {
0fe87f7c
HS
786/*
787 stop_pos does not include SvTAIL in the count, so this check is incorrect
788 (I think) - see [ID 20010618.006] and t/op/study.t. HVDS 2001/06/19
789*/
790#if 0
cf93c79d
IZ
791 if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */
792 goto check_tail;
0fe87f7c 793#endif
bd61b366 794 return NULL;
cf93c79d 795 }
c277df42 796 while (pos < previous + start_shift) {
3280af22 797 if (!(pos += PL_screamnext[pos]))
cf93c79d 798 goto cant_find;
378cc40b 799 }
de3bb511 800 big -= previous;
bbce6d69 801 do {
0d46e09a 802 register const unsigned char *s, *x;
ef64f398 803 if (pos >= stop_pos) break;
bbce6d69
PP
804 if (big[pos] != first)
805 continue;
806 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
bbce6d69
PP
807 if (*s++ != *x++) {
808 s--;
809 break;
378cc40b 810 }
bbce6d69 811 }
c277df42
IZ
812 if (s == littleend) {
813 *old_posp = pos;
814 if (!last) return (char *)(big+pos);
815 found = 1;
816 }
3280af22 817 } while ( pos += PL_screamnext[pos] );
a1d180c4 818 if (last && found)
cf93c79d 819 return (char *)(big+(*old_posp));
cf93c79d
IZ
820 check_tail:
821 if (!SvTAIL(littlestr) || (end_shift > 0))
bd61b366 822 return NULL;
cf93c79d 823 /* Ignore the trailing "\n". This code is not microoptimized */
cfd0369c 824 big = (const unsigned char *)(SvPVX_const(bigstr) + SvCUR(bigstr));
cf93c79d
IZ
825 stop_pos = littleend - little; /* Actual littlestr len */
826 if (stop_pos == 0)
827 return (char*)big;
828 big -= stop_pos;
829 if (*big == first
12ae5dfc
JH
830 && ((stop_pos == 1) ||
831 memEQ((char *)(big + 1), (char *)little, stop_pos - 1)))
cf93c79d 832 return (char*)big;
bd61b366 833 return NULL;
8d063cd8
LW
834}
835
79072805 836I32
864dbfa3 837Perl_ibcmp(pTHX_ const char *s1, const char *s2, register I32 len)
79072805 838{
e1ec3a88
AL
839 register const U8 *a = (const U8 *)s1;
840 register const U8 *b = (const U8 *)s2;
96a5add6
AL
841 PERL_UNUSED_CONTEXT;
842
79072805 843 while (len--) {
22c35a8c 844 if (*a != *b && *a != PL_fold[*b])
bbce6d69
PP
845 return 1;
846 a++,b++;
847 }
848 return 0;
849}
850
851I32
864dbfa3 852Perl_ibcmp_locale(pTHX_ const char *s1, const char *s2, register I32 len)
bbce6d69 853{
27da23d5 854 dVAR;
e1ec3a88
AL
855 register const U8 *a = (const U8 *)s1;
856 register const U8 *b = (const U8 *)s2;
96a5add6
AL
857 PERL_UNUSED_CONTEXT;
858
bbce6d69 859 while (len--) {
22c35a8c 860 if (*a != *b && *a != PL_fold_locale[*b])
bbce6d69
PP
861 return 1;
862 a++,b++;
79072805
LW
863 }
864 return 0;
865}
866
8d063cd8
LW
867/* copy a string to a safe spot */
868
954c1994 869/*
ccfc67b7
JH
870=head1 Memory Management
871
954c1994
GS
872=for apidoc savepv
873
61a925ed
AMS
874Perl's version of C<strdup()>. Returns a pointer to a newly allocated
875string which is a duplicate of C<pv>. The size of the string is
876determined by C<strlen()>. The memory allocated for the new string can
877be freed with the C<Safefree()> function.
954c1994
GS
878
879=cut
880*/
881
8d063cd8 882char *
efdfce31 883Perl_savepv(pTHX_ const char *pv)
8d063cd8 884{
96a5add6 885 PERL_UNUSED_CONTEXT;
e90e2364 886 if (!pv)
bd61b366 887 return NULL;
66a1b24b
AL
888 else {
889 char *newaddr;
890 const STRLEN pvlen = strlen(pv)+1;
10edeb5d
JH
891 Newx(newaddr, pvlen, char);
892 return (char*)memcpy(newaddr, pv, pvlen);
66a1b24b 893 }
8d063cd8
LW
894}
895
a687059c
LW
896/* same thing but with a known length */
897
954c1994
GS
898/*
899=for apidoc savepvn
900
61a925ed
AMS
901Perl's version of what C<strndup()> would be if it existed. Returns a
902pointer to a newly allocated string which is a duplicate of the first
cbf82dd0
NC
903C<len> bytes from C<pv>, plus a trailing NUL byte. The memory allocated for
904the new string can be freed with the C<Safefree()> function.
954c1994
GS
905
906=cut
907*/
908
a687059c 909char *
efdfce31 910Perl_savepvn(pTHX_ const char *pv, register I32 len)
a687059c
LW
911{
912 register char *newaddr;
96a5add6 913 PERL_UNUSED_CONTEXT;
a687059c 914
a02a5408 915 Newx(newaddr,len+1,char);
92110913 916 /* Give a meaning to NULL pointer mainly for the use in sv_magic() */
efdfce31 917 if (pv) {
e90e2364
NC
918 /* might not be null terminated */
919 newaddr[len] = '\0';
07409e01 920 return (char *) CopyD(pv,newaddr,len,char);
92110913
NIS
921 }
922 else {
07409e01 923 return (char *) ZeroD(newaddr,len+1,char);
92110913 924 }
a687059c
LW
925}
926
05ec9bb3
NIS
927/*
928=for apidoc savesharedpv
929
61a925ed
AMS
930A version of C<savepv()> which allocates the duplicate string in memory
931which is shared between threads.
05ec9bb3
NIS
932
933=cut
934*/
935char *
efdfce31 936Perl_savesharedpv(pTHX_ const char *pv)
05ec9bb3 937{
e90e2364 938 register char *newaddr;
490a0e98 939 STRLEN pvlen;
e90e2364 940 if (!pv)
bd61b366 941 return NULL;
e90e2364 942
490a0e98
NC
943 pvlen = strlen(pv)+1;
944 newaddr = (char*)PerlMemShared_malloc(pvlen);
e90e2364 945 if (!newaddr) {
0bd48802 946 return write_no_mem();
05ec9bb3 947 }
10edeb5d 948 return (char*)memcpy(newaddr, pv, pvlen);
05ec9bb3
NIS
949}
950
2e0de35c
NC
951/*
952=for apidoc savesvpv
953
6832267f 954A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from
2e0de35c
NC
955the passed in SV using C<SvPV()>
956
957=cut
958*/
959
960char *
961Perl_savesvpv(pTHX_ SV *sv)
962{
963 STRLEN len;
7452cf6a 964 const char * const pv = SvPV_const(sv, len);
2e0de35c
NC
965 register char *newaddr;
966
26866f99 967 ++len;
a02a5408 968 Newx(newaddr,len,char);
07409e01 969 return (char *) CopyD(pv,newaddr,len,char);
2e0de35c 970}
05ec9bb3
NIS
971
972
cea2e8a9 973/* the SV for Perl_form() and mess() is not kept in an arena */
fc36a67e 974
76e3520e 975STATIC SV *
cea2e8a9 976S_mess_alloc(pTHX)
fc36a67e 977{
97aff369 978 dVAR;
fc36a67e
PP
979 SV *sv;
980 XPVMG *any;
981
e72dc28c 982 if (!PL_dirty)
396482e1 983 return sv_2mortal(newSVpvs(""));
e72dc28c 984
0372dbb6
GS
985 if (PL_mess_sv)
986 return PL_mess_sv;
987
fc36a67e 988 /* Create as PVMG now, to avoid any upgrading later */
a02a5408
JC
989 Newx(sv, 1, SV);
990 Newxz(any, 1, XPVMG);
fc36a67e
PP
991 SvFLAGS(sv) = SVt_PVMG;
992 SvANY(sv) = (void*)any;
6136c704 993 SvPV_set(sv, NULL);
fc36a67e 994 SvREFCNT(sv) = 1 << 30; /* practically infinite */
e72dc28c 995 PL_mess_sv = sv;
fc36a67e
PP
996 return sv;
997}
998
c5be433b 999#if defined(PERL_IMPLICIT_CONTEXT)
cea2e8a9
GS
1000char *
1001Perl_form_nocontext(const char* pat, ...)
1002{
1003 dTHX;
c5be433b 1004 char *retval;
cea2e8a9
GS
1005 va_list args;
1006 va_start(args, pat);
c5be433b 1007 retval = vform(pat, &args);
cea2e8a9 1008 va_end(args);
c5be433b 1009 return retval;
cea2e8a9 1010}
c5be433b 1011#endif /* PERL_IMPLICIT_CONTEXT */
cea2e8a9 1012
7c9e965c 1013/*
ccfc67b7 1014=head1 Miscellaneous Functions
7c9e965c
JP
1015=for apidoc form
1016
1017Takes a sprintf-style format pattern and conventional
1018(non-SV) arguments and returns the formatted string.
1019
1020 (char *) Perl_form(pTHX_ const char* pat, ...)
1021
1022can be used any place a string (char *) is required:
1023
1024 char * s = Perl_form("%d.%d",major,minor);
1025
1026Uses a single private buffer so if you want to format several strings you
1027must explicitly copy the earlier strings away (and free the copies when you
1028are done).
1029
1030=cut
1031*/
1032
8990e307 1033char *
864dbfa3 1034Perl_form(pTHX_ const char* pat, ...)
8990e307 1035{
c5be433b 1036 char *retval;
46fc3d4c 1037 va_list args;
46fc3d4c 1038 va_start(args, pat);
c5be433b 1039 retval = vform(pat, &args);
46fc3d4c 1040 va_end(args);
c5be433b
GS
1041 return retval;
1042}
1043
1044char *
1045Perl_vform(pTHX_ const char *pat, va_list *args)
1046{
2d03de9c 1047 SV * const sv = mess_alloc();
4608196e 1048 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
e72dc28c 1049 return SvPVX(sv);
46fc3d4c 1050}
a687059c 1051
5a844595
GS
1052#if defined(PERL_IMPLICIT_CONTEXT)
1053SV *
1054Perl_mess_nocontext(const char *pat, ...)
1055{
1056 dTHX;
1057 SV *retval;
1058 va_list args;
1059 va_start(args, pat);
1060 retval = vmess(pat, &args);
1061 va_end(args);
1062 return retval;
1063}
1064#endif /* PERL_IMPLICIT_CONTEXT */
1065
06bf62c7 1066SV *
5a844595
GS
1067Perl_mess(pTHX_ const char *pat, ...)
1068{
1069 SV *retval;
1070 va_list args;
1071 va_start(args, pat);
1072 retval = vmess(pat, &args);
1073 va_end(args);
1074 return retval;
1075}
1076
5f66b61c
AL
1077STATIC const COP*
1078S_closest_cop(pTHX_ const COP *cop, const OP *o)
ae7d165c 1079{
97aff369 1080 dVAR;
ae7d165c
PJ
1081 /* Look for PL_op starting from o. cop is the last COP we've seen. */
1082
fabdb6c0
AL
1083 if (!o || o == PL_op)
1084 return cop;
ae7d165c
PJ
1085
1086 if (o->op_flags & OPf_KIDS) {
5f66b61c 1087 const OP *kid;
fabdb6c0 1088 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
5f66b61c 1089 const COP *new_cop;
ae7d165c
PJ
1090
1091 /* If the OP_NEXTSTATE has been optimised away we can still use it
1092 * the get the file and line number. */
1093
1094 if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
5f66b61c 1095 cop = (const COP *)kid;
ae7d165c
PJ
1096
1097 /* Keep searching, and return when we've found something. */
1098
1099 new_cop = closest_cop(cop, kid);
fabdb6c0
AL
1100 if (new_cop)
1101 return new_cop;
ae7d165c
PJ
1102 }
1103 }
1104
1105 /* Nothing found. */
1106
5f66b61c 1107 return NULL;
ae7d165c
PJ
1108}
1109
5a844595
GS
1110SV *
1111Perl_vmess(pTHX_ const char *pat, va_list *args)
46fc3d4c 1112{
97aff369 1113 dVAR;
c4420975 1114 SV * const sv = mess_alloc();
46fc3d4c 1115
5f66b61c 1116 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
46fc3d4c 1117 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
ae7d165c
PJ
1118 /*
1119 * Try and find the file and line for PL_op. This will usually be
1120 * PL_curcop, but it might be a cop that has been optimised away. We
1121 * can try to find such a cop by searching through the optree starting
1122 * from the sibling of PL_curcop.
1123 */
1124
e1ec3a88 1125 const COP *cop = closest_cop(PL_curcop, PL_curcop->op_sibling);
5f66b61c
AL
1126 if (!cop)
1127 cop = PL_curcop;
ae7d165c
PJ
1128
1129 if (CopLINE(cop))
ed094faf 1130 Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf,
3aed30dc 1131 OutCopFILE(cop), (IV)CopLINE(cop));
2035c5e8 1132 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
e1ec3a88 1133 const bool line_mode = (RsSIMPLE(PL_rs) &&
95a20fc0 1134 SvCUR(PL_rs) == 1 && *SvPVX_const(PL_rs) == '\n');
57def98f 1135 Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf,
5f66b61c 1136 PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
edc2eac3
JH
1137 line_mode ? "line" : "chunk",
1138 (IV)IoLINES(GvIOp(PL_last_in_gv)));
a687059c 1139 }
5f66b61c
AL
1140 if (PL_dirty)
1141 sv_catpvs(sv, " during global destruction");
1142 sv_catpvs(sv, ".\n");
a687059c 1143 }
06bf62c7 1144 return sv;
a687059c
LW
1145}
1146
7ff03255
SG
1147void
1148Perl_write_to_stderr(pTHX_ const char* message, int msglen)
1149{
27da23d5 1150 dVAR;
7ff03255
SG
1151 IO *io;
1152 MAGIC *mg;
1153
1154 if (PL_stderrgv && SvREFCNT(PL_stderrgv)
1155 && (io = GvIO(PL_stderrgv))
1156 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1157 {
1158 dSP;
1159 ENTER;
1160 SAVETMPS;
1161
1162 save_re_context();
1163 SAVESPTR(PL_stderrgv);
a0714e2c 1164 PL_stderrgv = NULL;
7ff03255
SG
1165
1166 PUSHSTACKi(PERLSI_MAGIC);
1167
1168 PUSHMARK(SP);
1169 EXTEND(SP,2);
1170 PUSHs(SvTIED_obj((SV*)io, mg));
1171 PUSHs(sv_2mortal(newSVpvn(message, msglen)));
1172 PUTBACK;
1173 call_method("PRINT", G_SCALAR);
1174
1175 POPSTACK;
1176 FREETMPS;
1177 LEAVE;
1178 }
1179 else {
1180#ifdef USE_SFIO
1181 /* SFIO can really mess with your errno */
53c1dcc0 1182 const int e = errno;
7ff03255 1183#endif
53c1dcc0 1184 PerlIO * const serr = Perl_error_log;
7ff03255
SG
1185
1186 PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
1187 (void)PerlIO_flush(serr);
1188#ifdef USE_SFIO
1189 errno = e;
1190#endif
1191 }
1192}
1193
46d9c920 1194/* Common code used by vcroak, vdie, vwarn and vwarner */
3ab1ac99 1195
46d9c920
NC
1196STATIC bool
1197S_vdie_common(pTHX_ const char *message, STRLEN msglen, I32 utf8, bool warn)
63315e18 1198{
97aff369 1199 dVAR;
63315e18
NC
1200 HV *stash;
1201 GV *gv;
1202 CV *cv;
46d9c920
NC
1203 SV **const hook = warn ? &PL_warnhook : &PL_diehook;
1204 /* sv_2cv might call Perl_croak() or Perl_warner() */
1205 SV * const oldhook = *hook;
1206
1207 assert(oldhook);
63315e18 1208
63315e18 1209 ENTER;
46d9c920
NC
1210 SAVESPTR(*hook);
1211 *hook = NULL;
1212 cv = sv_2cv(oldhook, &stash, &gv, 0);
63315e18
NC
1213 LEAVE;
1214 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1215 dSP;
1216 SV *msg;
1217
1218 ENTER;
1219 save_re_context();
46d9c920
NC
1220 if (warn) {
1221 SAVESPTR(*hook);
1222 *hook = NULL;
1223 }
1224 if (warn || message) {
63315e18
NC
1225 msg = newSVpvn(message, msglen);
1226 SvFLAGS(msg) |= utf8;
1227 SvREADONLY_on(msg);
1228 SAVEFREESV(msg);
1229 }
1230 else {
1231 msg = ERRSV;
1232 }
1233
46d9c920 1234 PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK);
63315e18
NC
1235 PUSHMARK(SP);
1236 XPUSHs(msg);
1237 PUTBACK;
1238 call_sv((SV*)cv, G_DISCARD);
1239 POPSTACK;
1240 LEAVE;
46d9c920 1241 return TRUE;
63315e18 1242 }
46d9c920 1243 return FALSE;
63315e18
NC
1244}
1245
cfd0369c 1246STATIC const char *
e07360fa
AT
1247S_vdie_croak_common(pTHX_ const char* pat, va_list* args, STRLEN* msglen,
1248 I32* utf8)
1249{
1250 dVAR;
cfd0369c 1251 const char *message;
e07360fa
AT
1252
1253 if (pat) {
890ce7af 1254 SV * const msv = vmess(pat, args);
e07360fa
AT
1255 if (PL_errors && SvCUR(PL_errors)) {
1256 sv_catsv(PL_errors, msv);
cfd0369c 1257 message = SvPV_const(PL_errors, *msglen);
e07360fa
AT
1258 SvCUR_set(PL_errors, 0);
1259 }
1260 else
cfd0369c 1261 message = SvPV_const(msv,*msglen);
e07360fa
AT
1262 *utf8 = SvUTF8(msv);
1263 }
1264 else {
bd61b366 1265 message = NULL;
e07360fa
AT
1266 }
1267
1268 DEBUG_S(PerlIO_printf(Perl_debug_log,
1269 "%p: die/croak: message = %s\ndiehook = %p\n",
1270 thr, message, PL_diehook));
1271 if (PL_diehook) {
46d9c920 1272 S_vdie_common(aTHX_ message, *msglen, *utf8, FALSE);
e07360fa
AT
1273 }
1274 return message;
1275}
1276
c5be433b
GS
1277OP *
1278Perl_vdie(pTHX_ const char* pat, va_list *args)
36477c24 1279{
97aff369 1280 dVAR;
73d840c0 1281 const char *message;
e1ec3a88 1282 const int was_in_eval = PL_in_eval;
06bf62c7 1283 STRLEN msglen;
ff882698 1284 I32 utf8 = 0;
36477c24 1285
bf49b057 1286 DEBUG_S(PerlIO_printf(Perl_debug_log,
199100c8 1287 "%p: die: curstack = %p, mainstack = %p\n",
533c011a 1288 thr, PL_curstack, PL_mainstack));
36477c24 1289
890ce7af 1290 message = vdie_croak_common(pat, args, &msglen, &utf8);
36477c24 1291
06bf62c7 1292 PL_restartop = die_where(message, msglen);
ff882698 1293 SvFLAGS(ERRSV) |= utf8;
bf49b057 1294 DEBUG_S(PerlIO_printf(Perl_debug_log,
7c06b590 1295 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
533c011a 1296 thr, PL_restartop, was_in_eval, PL_top_env));
3280af22 1297 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
6224f72b 1298 JMPENV_JUMP(3);
3280af22 1299 return PL_restartop;
36477c24
PP
1300}
1301
c5be433b 1302#if defined(PERL_IMPLICIT_CONTEXT)
cea2e8a9
GS
1303OP *
1304Perl_die_nocontext(const char* pat, ...)
a687059c 1305{
cea2e8a9
GS
1306 dTHX;
1307 OP *o;
a687059c 1308 va_list args;
cea2e8a9 1309 va_start(args, pat);
c5be433b 1310 o = vdie(pat, &args);
cea2e8a9
GS
1311 va_end(args);
1312 return o;
1313}
c5be433b 1314#endif /* PERL_IMPLICIT_CONTEXT */
cea2e8a9
GS
1315
1316OP *
1317Perl_die(pTHX_ const char* pat, ...)
1318{
1319 OP *o;
1320 va_list args;
1321 va_start(args, pat);
c5be433b 1322 o = vdie(pat, &args);
cea2e8a9
GS
1323 va_end(args);
1324 return o;
1325}
1326
c5be433b
GS
1327void
1328Perl_vcroak(pTHX_ const char* pat, va_list *args)
cea2e8a9 1329{
97aff369 1330 dVAR;
73d840c0 1331 const char *message;
06bf62c7 1332 STRLEN msglen;
ff882698 1333 I32 utf8 = 0;
a687059c 1334
3ab1ac99 1335 message = S_vdie_croak_common(aTHX_ pat, args, &msglen, &utf8);
5a844595 1336
3280af22 1337 if (PL_in_eval) {
06bf62c7 1338 PL_restartop = die_where(message, msglen);
ff882698 1339 SvFLAGS(ERRSV) |= utf8;
6224f72b 1340 JMPENV_JUMP(3);
a0d0e21e 1341 }
84414e3e 1342 else if (!message)
cfd0369c 1343 message = SvPVx_const(ERRSV, msglen);
84414e3e 1344
7ff03255 1345 write_to_stderr(message, msglen);
f86702cc 1346 my_failure_exit();
a687059c
LW
1347}
1348
c5be433b 1349#if defined(PERL_IMPLICIT_CONTEXT)
8990e307 1350void
cea2e8a9 1351Perl_croak_nocontext(const char *pat, ...)
a687059c 1352{
cea2e8a9 1353 dTHX;
a687059c 1354 va_list args;
cea2e8a9 1355 va_start(args, pat);
c5be433b 1356 vcroak(pat, &args);
cea2e8a9
GS
1357 /* NOTREACHED */
1358 va_end(args);
1359}
1360#endif /* PERL_IMPLICIT_CONTEXT */
1361
954c1994 1362/*
ccfc67b7
JH
1363=head1 Warning and Dieing
1364
954c1994
GS
1365=for apidoc croak
1366
9983fa3c 1367This is the XSUB-writer's interface to Perl's C<die> function.
966353fd
MF
1368Normally call this function the same way you call the C C<printf>
1369function. Calling C<croak> returns control directly to Perl,
1370sidestepping the normal C order of execution. See C<warn>.
9983fa3c
GS
1371
1372If you want to throw an exception object, assign the object to
bd61b366 1373C<$@> and then pass C<NULL> to croak():
9983fa3c
GS
1374
1375 errsv = get_sv("@", TRUE);
1376 sv_setsv(errsv, exception_object);
bd61b366 1377 croak(NULL);
954c1994
GS
1378
1379=cut
1380*/
1381
cea2e8a9
GS
1382void
1383Perl_croak(pTHX_ const char *pat, ...)
1384{
1385 va_list args;
1386 va_start(args, pat);
c5be433b 1387 vcroak(pat, &args);
cea2e8a9
GS
1388 /* NOTREACHED */
1389 va_end(args);
1390}
1391
c5be433b
GS
1392void
1393Perl_vwarn(pTHX_ const char* pat, va_list *args)
cea2e8a9 1394{
27da23d5 1395 dVAR;
06bf62c7 1396 STRLEN msglen;
53c1dcc0
AL
1397 SV * const msv = vmess(pat, args);
1398 const I32 utf8 = SvUTF8(msv);
1399 const char * const message = SvPV_const(msv, msglen);
a687059c 1400
3280af22 1401 if (PL_warnhook) {
46d9c920 1402 if (vdie_common(message, msglen, utf8, TRUE))
20cec16a 1403 return;
748a9306 1404 }
87582a92 1405
7ff03255 1406 write_to_stderr(message, msglen);
a687059c 1407}
8d063cd8 1408
c5be433b 1409#if defined(PERL_IMPLICIT_CONTEXT)
cea2e8a9
GS
1410void
1411Perl_warn_nocontext(const char *pat, ...)
1412{
1413 dTHX;
1414 va_list args;
1415 va_start(args, pat);
c5be433b 1416 vwarn(pat, &args);
cea2e8a9
GS
1417 va_end(args);
1418}
1419#endif /* PERL_IMPLICIT_CONTEXT */
1420
954c1994
GS
1421/*
1422=for apidoc warn
1423
966353fd
MF
1424This is the XSUB-writer's interface to Perl's C<warn> function. Call this
1425function the same way you call the C C<printf> function. See C<croak>.
954c1994
GS
1426
1427=cut
1428*/
1429
cea2e8a9
GS
1430void
1431Perl_warn(pTHX_ const char *pat, ...)
1432{
1433 va_list args;
1434 va_start(args, pat);
c5be433b 1435 vwarn(pat, &args);
cea2e8a9
GS
1436 va_end(args);
1437}
1438
c5be433b
GS
1439#if defined(PERL_IMPLICIT_CONTEXT)
1440void
1441Perl_warner_nocontext(U32 err, const char *pat, ...)
1442{
27da23d5 1443 dTHX;
c5be433b
GS
1444 va_list args;
1445 va_start(args, pat);
1446 vwarner(err, pat, &args);
1447 va_end(args);
1448}
1449#endif /* PERL_IMPLICIT_CONTEXT */
1450
599cee73 1451void
864dbfa3 1452Perl_warner(pTHX_ U32 err, const char* pat,...)
599cee73
PM
1453{
1454 va_list args;
c5be433b
GS
1455 va_start(args, pat);
1456 vwarner(err, pat, &args);
1457 va_end(args);
1458}
1459
1460void
1461Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args)
1462{
27da23d5 1463 dVAR;
5f2d9966 1464 if (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) {
a3b680e6 1465 SV * const msv = vmess(pat, args);
d13b0d77 1466 STRLEN msglen;
7452cf6a 1467 const char * const message = SvPV_const(msv, msglen);
a3b680e6 1468 const I32 utf8 = SvUTF8(msv);
599cee73 1469
3aed30dc 1470 if (PL_diehook) {
63315e18 1471 assert(message);
46d9c920 1472 S_vdie_common(aTHX_ message, msglen, utf8, FALSE);
3aed30dc
HS
1473 }
1474 if (PL_in_eval) {
1475 PL_restartop = die_where(message, msglen);
ff882698 1476 SvFLAGS(ERRSV) |= utf8;
3aed30dc
HS
1477 JMPENV_JUMP(3);
1478 }
7ff03255 1479 write_to_stderr(message, msglen);
3aed30dc 1480 my_failure_exit();
599cee73
PM
1481 }
1482 else {
d13b0d77 1483 Perl_vwarn(aTHX_ pat, args);
599cee73
PM
1484 }
1485}
1486
f54ba1c2
DM
1487/* implements the ckWARN? macros */
1488
1489bool
1490Perl_ckwarn(pTHX_ U32 w)
1491{
97aff369 1492 dVAR;
f54ba1c2
DM
1493 return
1494 (
1495 isLEXWARN_on
1496 && PL_curcop->cop_warnings != pWARN_NONE
1497 && (
1498 PL_curcop->cop_warnings == pWARN_ALL
1499 || isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))
1500 || (unpackWARN2(w) &&
1501 isWARN_on(PL_curcop->cop_warnings, unpackWARN2(w)))
1502 || (unpackWARN3(w) &&
1503 isWARN_on(PL_curcop->cop_warnings, unpackWARN3(w)))
1504 || (unpackWARN4(w) &&
1505 isWARN_on(PL_curcop->cop_warnings, unpackWARN4(w)))
1506 )
1507 )
1508 ||
1509 (
1510 isLEXWARN_off && PL_dowarn & G_WARN_ON
1511 )
1512 ;
1513}
1514
1515/* implements the ckWARN?_d macro */
1516
1517bool
1518Perl_ckwarn_d(pTHX_ U32 w)
1519{
97aff369 1520 dVAR;
f54ba1c2
DM
1521 return
1522 isLEXWARN_off
1523 || PL_curcop->cop_warnings == pWARN_ALL
1524 || (
1525 PL_curcop->cop_warnings != pWARN_NONE
1526 && (
1527 isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))
1528 || (unpackWARN2(w) &&
1529 isWARN_on(PL_curcop->cop_warnings, unpackWARN2(w)))
1530 || (unpackWARN3(w) &&
1531 isWARN_on(PL_curcop->cop_warnings, unpackWARN3(w)))
1532 || (unpackWARN4(w) &&
1533 isWARN_on(PL_curcop->cop_warnings, unpackWARN4(w)))
1534 )
1535 )
1536 ;
1537}
1538
72dc9ed5
NC
1539/* Set buffer=NULL to get a new one. */
1540STRLEN *
8ee4cf24 1541Perl_new_warnings_bitfield(pTHX_ STRLEN *buffer, const char *const bits,
72dc9ed5
NC
1542 STRLEN size) {
1543 const MEM_SIZE len_wanted = sizeof(STRLEN) + size;
35da51f7 1544 PERL_UNUSED_CONTEXT;
72dc9ed5 1545
10edeb5d
JH
1546 buffer = (STRLEN*)
1547 (specialWARN(buffer) ?
1548 PerlMemShared_malloc(len_wanted) :
1549 PerlMemShared_realloc(buffer, len_wanted));
72dc9ed5
NC
1550 buffer[0] = size;
1551 Copy(bits, (buffer + 1), size, char);
1552 return buffer;
1553}
f54ba1c2 1554
e6587932
DM
1555/* since we've already done strlen() for both nam and val
1556 * we can use that info to make things faster than
1557 * sprintf(s, "%s=%s", nam, val)
1558 */
1559#define my_setenv_format(s, nam, nlen, val, vlen) \
1560 Copy(nam, s, nlen, char); \
1561 *(s+nlen) = '='; \
1562 Copy(val, s+(nlen+1), vlen, char); \
1563 *(s+(nlen+1+vlen)) = '\0'
1564
c5d12488
JH
1565#ifdef USE_ENVIRON_ARRAY
1566 /* VMS' my_setenv() is in vms.c */
1567#if !defined(WIN32) && !defined(NETWARE)
8d063cd8 1568void
e1ec3a88 1569Perl_my_setenv(pTHX_ const char *nam, const char *val)
8d063cd8 1570{
27da23d5 1571 dVAR;
4efc5df6
GS
1572#ifdef USE_ITHREADS
1573 /* only parent thread can modify process environment */
1574 if (PL_curinterp == aTHX)
1575#endif
1576 {
f2517201 1577#ifndef PERL_USE_SAFE_PUTENV
50acdf95 1578 if (!PL_use_safe_putenv) {
c5d12488
JH
1579 /* most putenv()s leak, so we manipulate environ directly */
1580 register I32 i=setenv_getix(nam); /* where does it go? */
1581 int nlen, vlen;
1582
1583 if (environ == PL_origenviron) { /* need we copy environment? */
1584 I32 j;
1585 I32 max;
1586 char **tmpenv;
1587
1588 max = i;
1589 while (environ[max])
1590 max++;
1591 tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
1592 for (j=0; j<max; j++) { /* copy environment */
1593 const int len = strlen(environ[j]);
1594 tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char));
1595 Copy(environ[j], tmpenv[j], len+1, char);
1596 }
1597 tmpenv[max] = NULL;
1598 environ = tmpenv; /* tell exec where it is now */
1599 }
1600 if (!val) {
1601 safesysfree(environ[i]);
1602 while (environ[i]) {
1603 environ[i] = environ[i+1];
1604 i++;
a687059c 1605 }
c5d12488
JH
1606 return;
1607 }
1608 if (!environ[i]) { /* does not exist yet */
1609 environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
1610 environ[i+1] = NULL; /* make sure it's null terminated */
1611 }
1612 else
1613 safesysfree(environ[i]);
1614 nlen = strlen(nam);
1615 vlen = strlen(val);
1616
1617 environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
1618 /* all that work just for this */
1619 my_setenv_format(environ[i], nam, nlen, val, vlen);
50acdf95 1620 } else {
c5d12488 1621# endif
7ee146b1 1622# if defined(__CYGWIN__) || defined(EPOC) || defined(__SYMBIAN32__) || defined(__riscos__)
88f5bc07
AB
1623# if defined(HAS_UNSETENV)
1624 if (val == NULL) {
1625 (void)unsetenv(nam);
1626 } else {
1627 (void)setenv(nam, val, 1);
1628 }
1629# else /* ! HAS_UNSETENV */
1630 (void)setenv(nam, val, 1);
1631# endif /* HAS_UNSETENV */
47dafe4d 1632# else
88f5bc07
AB
1633# if defined(HAS_UNSETENV)
1634 if (val == NULL) {
1635 (void)unsetenv(nam);
1636 } else {
c4420975
AL
1637 const int nlen = strlen(nam);
1638 const int vlen = strlen(val);
1639 char * const new_env =
88f5bc07
AB
1640 (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
1641 my_setenv_format(new_env, nam, nlen, val, vlen);
1642 (void)putenv(new_env);
1643 }
1644# else /* ! HAS_UNSETENV */
1645 char *new_env;
c4420975
AL
1646 const int nlen = strlen(nam);
1647 int vlen;
88f5bc07
AB
1648 if (!val) {
1649 val = "";
1650 }
1651 vlen = strlen(val);
1652 new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
1653 /* all that work just for this */
1654 my_setenv_format(new_env, nam, nlen, val, vlen);
1655 (void)putenv(new_env);
1656# endif /* HAS_UNSETENV */
47dafe4d 1657# endif /* __CYGWIN__ */
50acdf95
MS
1658#ifndef PERL_USE_SAFE_PUTENV
1659 }
1660#endif
4efc5df6 1661 }
8d063cd8
LW
1662}
1663
c5d12488 1664#else /* WIN32 || NETWARE */
68dc0745
PP
1665
1666void
72229eff 1667Perl_my_setenv(pTHX_ const char *nam, const char *val)
68dc0745 1668{
27da23d5 1669 dVAR;
c5d12488
JH
1670 register char *envstr;
1671 const int nlen = strlen(nam);
1672 int vlen;
e6587932 1673
c5d12488
JH
1674 if (!val) {
1675 val = "";
ac5c734f 1676 }
c5d12488
JH
1677 vlen = strlen(val);
1678 Newx(envstr, nlen+vlen+2, char);
1679 my_setenv_format(envstr, nam, nlen, val, vlen);
1680 (void)PerlEnv_putenv(envstr);
1681 Safefree(envstr);
3e3baf6d
TB
1682}
1683
c5d12488 1684#endif /* WIN32 || NETWARE */
3e3baf6d 1685
c5d12488 1686#ifndef PERL_MICRO
3e3baf6d 1687I32
e1ec3a88 1688Perl_setenv_getix(pTHX_ const char *nam)
3e3baf6d 1689{
c5d12488 1690 register I32 i;
0d46e09a 1691 register const I32 len = strlen(nam);
96a5add6 1692 PERL_UNUSED_CONTEXT;
3e3baf6d
TB
1693
1694 for (i = 0; environ[i]; i++) {
1695 if (
1696#ifdef WIN32
1697 strnicmp(environ[i],nam,len) == 0
1698#else
1699 strnEQ(environ[i],nam,len)
1700#endif
1701 && environ[i][len] == '=')
1702 break; /* strnEQ must come first to avoid */
1703 } /* potential SEGV's */
1704 return i;
68dc0745 1705}
c5d12488 1706#endif /* !PERL_MICRO */
68dc0745 1707
c5d12488 1708#endif /* !VMS && !EPOC*/
378cc40b 1709
16d20bd9 1710#ifdef UNLINK_ALL_VERSIONS
79072805 1711I32
6e732051 1712Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */
378cc40b 1713{
35da51f7 1714 I32 retries = 0;
378cc40b 1715
35da51f7
AL
1716 while (PerlLIO_unlink(f) >= 0)
1717 retries++;
1718 return retries ? 0 : -1;
378cc40b
LW
1719}
1720#endif
1721
7a3f2258 1722/* this is a drop-in replacement for bcopy() */
2253333f 1723#if (!defined(HAS_MEMCPY) && !defined(HAS_BCOPY)) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY) && !defined(HAS_SAFE_BCOPY))
378cc40b 1724char *
7a3f2258 1725Perl_my_bcopy(register const char *from,register char *to,register I32 len)
378cc40b 1726{
2d03de9c 1727 char * const retval = to;
378cc40b 1728
7c0587c8
LW
1729 if (from - to >= 0) {
1730 while (len--)
1731 *to++ = *from++;
1732 }
1733 else {
1734 to += len;
1735 from += len;
1736 while (len--)
faf8582f 1737 *(--to) = *(--from);
7c0587c8 1738 }
378cc40b
LW
1739 return retval;
1740}
ffed7fef 1741#endif
378cc40b 1742
7a3f2258 1743/* this is a drop-in replacement for memset() */
fc36a67e
PP
1744#ifndef HAS_MEMSET
1745void *
7a3f2258 1746Perl_my_memset(register char *loc, register I32 ch, register I32 len)
fc36a67e 1747{
2d03de9c 1748 char * const retval = loc;
fc36a67e
PP
1749
1750 while (len--)
1751 *loc++ = ch;
1752 return retval;
1753}
1754#endif
1755
7a3f2258 1756/* this is a drop-in replacement for bzero() */
7c0587c8 1757#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1758char *
7a3f2258 1759Perl_my_bzero(register char *loc, register I32 len)
378cc40b 1760{
2d03de9c 1761 char * const retval = loc;
378cc40b
LW
1762
1763 while (len--)
1764 *loc++ = 0;
1765 return retval;
1766}
1767#endif
7c0587c8 1768
7a3f2258 1769/* this is a drop-in replacement for memcmp() */
36477c24 1770#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1771I32
7a3f2258 1772Perl_my_memcmp(const char *s1, const char *s2, register I32 len)
7c0587c8 1773{
e1ec3a88
AL
1774 register const U8 *a = (const U8 *)s1;
1775 register const U8 *b = (const U8 *)s2;
79072805 1776 register I32 tmp;
7c0587c8
LW
1777
1778 while (len--) {
27da23d5 1779 if ((tmp = *a++ - *b++))
7c0587c8
LW
1780 return tmp;
1781 }
1782 return 0;
1783}
36477c24 1784#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1785
fe14fcc3 1786#ifndef HAS_VPRINTF
a687059c 1787
85e6fe83 1788#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1789char *
1790#else
1791int
1792#endif
08105a92 1793vsprintf(char *dest, const char *pat, char *args)
a687059c
LW
1794{
1795 FILE fakebuf;
1796
1797 fakebuf._ptr = dest;
1798 fakebuf._cnt = 32767;
35c8bce7
LW
1799#ifndef _IOSTRG
1800#define _IOSTRG 0
1801#endif
a687059c
LW
1802 fakebuf._flag = _IOWRT|_IOSTRG;
1803 _doprnt(pat, args, &fakebuf); /* what a kludge */
1804 (void)putc('\0', &fakebuf);
85e6fe83 1805#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1806 return(dest);
1807#else
1808 return 0; /* perl doesn't use return value */
1809#endif
1810}
1811
fe14fcc3 1812#endif /* HAS_VPRINTF */
a687059c
LW
1813
1814#ifdef MYSWAP
ffed7fef 1815#if BYTEORDER != 0x4321
a687059c 1816short
864dbfa3 1817Perl_my_swap(pTHX_ short s)
a687059c
LW
1818{
1819#if (BYTEORDER & 1) == 0
1820 short result;
1821
1822 result = ((s & 255) << 8) + ((s >> 8) & 255);
1823 return result;
1824#else
1825 return s;
1826#endif
1827}
1828
1829long
864dbfa3 1830Perl_my_htonl(pTHX_ long l)
a687059c
LW
1831{
1832 union {
1833 long result;
ffed7fef 1834 char c[sizeof(long)];
a687059c
LW
1835 } u;
1836
ffed7fef 1837#if BYTEORDER == 0x1234
a687059c
LW
1838 u.c[0] = (l >> 24) & 255;
1839 u.c[1] = (l >> 16) & 255;
1840 u.c[2] = (l >> 8) & 255;
1841 u.c[3] = l & 255;
1842 return u.result;
1843#else
ffed7fef 1844#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
cea2e8a9 1845 Perl_croak(aTHX_ "Unknown BYTEORDER\n");
a687059c 1846#else
79072805
LW
1847 register I32 o;
1848 register I32 s;
a687059c 1849
ffed7fef
LW
1850 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1851 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1852 }
1853 return u.result;
1854#endif
1855#endif
1856}
1857
1858long
864dbfa3 1859Perl_my_ntohl(pTHX_ long l)
a687059c
LW
1860{
1861 union {
1862 long l;
ffed7fef 1863 char c[sizeof(long)];
a687059c
LW
1864 } u;
1865
ffed7fef 1866#if BYTEORDER == 0x1234
a687059c
LW
1867 u.c[0] = (l >> 24) & 255;
1868 u.c[1] = (l >> 16) & 255;
1869 u.c[2] = (l >> 8) & 255;
1870 u.c[3] = l & 255;
1871 return u.l;
1872#else
ffed7fef 1873#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
cea2e8a9 1874 Perl_croak(aTHX_ "Unknown BYTEORDER\n");
a687059c 1875#else
79072805
LW
1876 register I32 o;
1877 register I32 s;
a687059c
LW
1878
1879 u.l = l;
1880 l = 0;
ffed7fef
LW
1881 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1882 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1883 }
1884 return l;
1885#endif
1886#endif
1887}
1888
ffed7fef 1889#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1890#endif /* MYSWAP */
1891
1892/*
1893 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1894 * If these functions are defined,
1895 * the BYTEORDER is neither 0x1234 nor 0x4321.
1896 * However, this is not assumed.
1897 * -DWS
1898 */
1899
1109a392 1900#define HTOLE(name,type) \
988174c1 1901 type \
ba106d47 1902 name (register type n) \
988174c1
LW
1903 { \
1904 union { \
1905 type value; \
1906 char c[sizeof(type)]; \
1907 } u; \
bb7a0f54
MHM
1908 register U32 i; \
1909 register U32 s = 0; \
1109a392 1910 for (i = 0; i < sizeof(u.c); i++, s += 8) { \
988174c1
LW
1911 u.c[i] = (n >> s) & 0xFF; \
1912 } \
1913 return u.value; \
1914 }
1915
1109a392 1916#define LETOH(name,type) \
988174c1 1917 type \
ba106d47 1918 name (register type n) \
988174c1
LW
1919 { \
1920 union { \
1921 type value; \
1922 char c[sizeof(type)]; \
1923 } u; \
bb7a0f54
MHM
1924 register U32 i; \
1925 register U32 s = 0; \
988174c1
LW
1926 u.value = n; \
1927 n = 0; \
1109a392
MHM
1928 for (i = 0; i < sizeof(u.c); i++, s += 8) { \
1929 n |= ((type)(u.c[i] & 0xFF)) << s; \
988174c1
LW
1930 } \
1931 return n; \
1932 }
1933
1109a392
MHM
1934/*
1935 * Big-endian byte order functions.
1936 */
1937
1938#define HTOBE(name,type) \
1939 type \
1940 name (register type n) \
1941 { \
1942 union { \
1943 type value; \
1944 char c[sizeof(type)]; \
1945 } u; \
bb7a0f54
MHM
1946 register U32 i; \
1947 register U32 s = 8*(sizeof(u.c)-1); \
1109a392
MHM
1948 for (i = 0; i < sizeof(u.c); i++, s -= 8) { \
1949 u.c[i] = (n >> s) & 0xFF; \
1950 } \
1951 return u.value; \
1952 }
1953
1954#define BETOH(name,type) \
1955 type \
1956 name (register type n) \
1957 { \
1958 union { \
1959 type value; \
1960 char c[sizeof(type)]; \
1961 } u; \
bb7a0f54
MHM
1962 register U32 i; \
1963 register U32 s = 8*(sizeof(u.c)-1); \
1109a392
MHM
1964 u.value = n; \
1965 n = 0; \
1966 for (i = 0; i < sizeof(u.c); i++, s -= 8) { \
1967 n |= ((type)(u.c[i] & 0xFF)) << s; \
1968 } \
1969 return n; \
1970 }
1971
1972/*
1973 * If we just can't do it...
1974 */
1975
1976#define NOT_AVAIL(name,type) \
1977 type \
1978 name (register type n) \
1979 { \
1980 Perl_croak_nocontext(#name "() not available"); \
1981 return n; /* not reached */ \
1982 }
1983
1984
988174c1 1985#if defined(HAS_HTOVS) && !defined(htovs)
1109a392 1986HTOLE(htovs,short)
988174c1
LW
1987#endif
1988#if defined(HAS_HTOVL) && !defined(htovl)
1109a392 1989HTOLE(htovl,long)
988174c1
LW
1990#endif
1991#if defined(HAS_VTOHS) && !defined(vtohs)
1109a392 1992LETOH(vtohs,short)
988174c1
LW
1993#endif
1994#if defined(HAS_VTOHL) && !defined(vtohl)
1109a392
MHM
1995LETOH(vtohl,long)
1996#endif
1997
1998#ifdef PERL_NEED_MY_HTOLE16
1999# if U16SIZE == 2
2000HTOLE(Perl_my_htole16,U16)
2001# else
2002NOT_AVAIL(Perl_my_htole16,U16)
2003# endif
2004#endif
2005#ifdef PERL_NEED_MY_LETOH16
2006# if U16SIZE == 2
2007LETOH(Perl_my_letoh16,U16)
2008# else
2009NOT_AVAIL(Perl_my_letoh16,U16)
2010# endif
2011#endif
2012#ifdef PERL_NEED_MY_HTOBE16
2013# if U16SIZE == 2
2014HTOBE(Perl_my_htobe16,U16)
2015# else
2016NOT_AVAIL(Perl_my_htobe16,U16)
2017# endif
2018#endif
2019#ifdef PERL_NEED_MY_BETOH16
2020# if U16SIZE == 2
2021BETOH(Perl_my_betoh16,U16)
2022# else
2023NOT_AVAIL(Perl_my_betoh16,U16)
2024# endif
2025#endif
2026
2027#ifdef PERL_NEED_MY_HTOLE32
2028# if U32SIZE == 4
2029HTOLE(Perl_my_htole32,U32)
2030# else
2031NOT_AVAIL(Perl_my_htole32,U32)
2032# endif
2033#endif
2034#ifdef PERL_NEED_MY_LETOH32
2035# if U32SIZE == 4
2036LETOH(Perl_my_letoh32,U32)
2037# else
2038NOT_AVAIL(Perl_my_letoh32,U32)
2039# endif
2040#endif
2041#ifdef PERL_NEED_MY_HTOBE32
2042# if U32SIZE == 4
2043HTOBE(Perl_my_htobe32,U32)
2044# else
2045NOT_AVAIL(Perl_my_htobe32,U32)
2046# endif
2047#endif
2048#ifdef PERL_NEED_MY_BETOH32
2049# if U32SIZE == 4
2050BETOH(Perl_my_betoh32,U32)
2051# else
2052NOT_AVAIL(Perl_my_betoh32,U32)
2053# endif
2054#endif
2055
2056#ifdef PERL_NEED_MY_HTOLE64
2057# if U64SIZE == 8
2058HTOLE(Perl_my_htole64,U64)
2059# else
2060NOT_AVAIL(Perl_my_htole64,U64)
2061# endif
2062#endif
2063#ifdef PERL_NEED_MY_LETOH64
2064# if U64SIZE == 8
2065LETOH(Perl_my_letoh64,U64)
2066# else
2067NOT_AVAIL(Perl_my_letoh64,U64)
2068# endif
2069#endif
2070#ifdef PERL_NEED_MY_HTOBE64
2071# if U64SIZE == 8
2072HTOBE(Perl_my_htobe64,U64)
2073# else
2074NOT_AVAIL(Perl_my_htobe64,U64)
2075# endif
2076#endif
2077#ifdef PERL_NEED_MY_BETOH64
2078# if U64SIZE == 8
2079BETOH(Perl_my_betoh64,U64)
2080# else
2081NOT_AVAIL(Perl_my_betoh64,U64)
2082# endif
988174c1 2083#endif
a687059c 2084
1109a392
MHM
2085#ifdef PERL_NEED_MY_HTOLES
2086HTOLE(Perl_my_htoles,short)
2087#endif
2088#ifdef PERL_NEED_MY_LETOHS
2089LETOH(Perl_my_letohs,short)
2090#endif
2091#ifdef PERL_NEED_MY_HTOBES
2092HTOBE(Perl_my_htobes,short)
2093#endif
2094#ifdef PERL_NEED_MY_BETOHS
2095BETOH(Perl_my_betohs,short)
2096#endif
2097
2098#ifdef PERL_NEED_MY_HTOLEI
2099HTOLE(Perl_my_htolei,int)
2100#endif
2101#ifdef PERL_NEED_MY_LETOHI
2102LETOH(Perl_my_letohi,int)
2103#endif
2104#ifdef PERL_NEED_MY_HTOBEI
2105HTOBE(Perl_my_htobei,int)
2106#endif
2107#ifdef PERL_NEED_MY_BETOHI
2108BETOH(Perl_my_betohi,int)
2109#endif
2110
2111#ifdef PERL_NEED_MY_HTOLEL
2112HTOLE(Perl_my_htolel,long)
2113#endif
2114#ifdef PERL_NEED_MY_LETOHL
2115LETOH(Perl_my_letohl,long)
2116#endif
2117#ifdef PERL_NEED_MY_HTOBEL
2118HTOBE(Perl_my_htobel,long)
2119#endif
2120#ifdef PERL_NEED_MY_BETOHL
2121BETOH(Perl_my_betohl,long)
2122#endif
2123
2124void
2125Perl_my_swabn(void *ptr, int n)
2126{
2127 register char *s = (char *)ptr;
2128 register char *e = s + (n-1);
2129 register char tc;
2130
2131 for (n /= 2; n > 0; s++, e--, n--) {
2132 tc = *s;
2133 *s = *e;
2134 *e = tc;
2135 }
2136}
2137
4a7d1889
NIS
2138PerlIO *
2139Perl_my_popen_list(pTHX_ char *mode, int n, SV **args)
2140{
2986a63f 2141#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(OS2) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) && !defined(NETWARE)
97aff369 2142 dVAR;
1f852d0d
NIS
2143 int p[2];
2144 register I32 This, that;
2145 register Pid_t pid;
2146 SV *sv;
2147 I32 did_pipes = 0;
2148 int pp[2];
2149
2150 PERL_FLUSHALL_FOR_CHILD;
2151 This = (*mode == 'w');
2152 that = !This;
2153 if (PL_tainting) {
2154 taint_env();
2155 taint_proper("Insecure %s%s", "EXEC");
2156 }
2157 if (PerlProc_pipe(p) < 0)
4608196e 2158 return NULL;
1f852d0d
NIS
2159 /* Try for another pipe pair for error return */
2160 if (PerlProc_pipe(pp) >= 0)
2161 did_pipes = 1;
52e18b1f 2162 while ((pid = PerlProc_fork()) < 0) {
1f852d0d
NIS
2163 if (errno != EAGAIN) {
2164 PerlLIO_close(p[This]);
4e6dfe71 2165 PerlLIO_close(p[that]);
1f852d0d
NIS
2166 if (did_pipes) {
2167 PerlLIO_close(pp[0]);
2168 PerlLIO_close(pp[1]);
2169 }
4608196e 2170 return NULL;
1f852d0d
NIS
2171 }
2172 sleep(5);
2173 }
2174 if (pid == 0) {
2175 /* Child */
1f852d0d
NIS
2176#undef THIS
2177#undef THAT
2178#define THIS that
2179#define THAT This
1f852d0d
NIS
2180 /* Close parent's end of error status pipe (if any) */
2181 if (did_pipes) {
2182 PerlLIO_close(pp[0]);
2183#if defined(HAS_FCNTL) && defined(F_SETFD)
2184 /* Close error pipe automatically if exec works */
2185 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
2186#endif
2187 }
2188 /* Now dup our end of _the_ pipe to right position */
2189 if (p[THIS] != (*mode == 'r')) {
2190 PerlLIO_dup2(p[THIS], *mode == 'r');
2191 PerlLIO_close(p[THIS]);
4e6dfe71
GS
2192 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2193 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
1f852d0d 2194 }
4e6dfe71
GS
2195 else
2196 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
1f852d0d
NIS
2197#if !defined(HAS_FCNTL) || !defined(F_SETFD)
2198 /* No automatic close - do it by hand */
b7953727
JH
2199# ifndef NOFILE
2200# define NOFILE 20
2201# endif
a080fe3d
NIS
2202 {
2203 int fd;
2204
2205 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
3aed30dc 2206 if (fd != pp[1])
a080fe3d
NIS
2207 PerlLIO_close(fd);
2208 }
1f852d0d
NIS
2209 }
2210#endif
a0714e2c 2211 do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes);
1f852d0d
NIS
2212 PerlProc__exit(1);
2213#undef THIS
2214#undef THAT
2215 }
2216 /* Parent */
52e18b1f 2217 do_execfree(); /* free any memory malloced by child on fork */
1f852d0d
NIS
2218 if (did_pipes)
2219 PerlLIO_close(pp[1]);
2220 /* Keep the lower of the two fd numbers */
2221 if (p[that] < p[This]) {
2222 PerlLIO_dup2(p[This], p[that]);
2223 PerlLIO_close(p[This]);
2224 p[This] = p[that];
2225 }
4e6dfe71
GS
2226 else
2227 PerlLIO_close(p[that]); /* close child's end of pipe */
2228
1f852d0d
NIS
2229 LOCK_FDPID_MUTEX;
2230 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2231 UNLOCK_FDPID_MUTEX;
862a34c6 2232 SvUPGRADE(sv,SVt_IV);
45977657 2233 SvIV_set(sv, pid);
1f852d0d
NIS
2234 PL_forkprocess = pid;
2235 /* If we managed to get status pipe check for exec fail */
2236 if (did_pipes && pid > 0) {
2237 int errkid;
bb7a0f54
MHM
2238 unsigned n = 0;
2239 SSize_t n1;
1f852d0d
NIS
2240
2241 while (n < sizeof(int)) {
2242 n1 = PerlLIO_read(pp[0],
2243 (void*)(((char*)&errkid)+n),
2244 (sizeof(int)) - n);
2245 if (n1 <= 0)
2246 break;
2247 n += n1;
2248 }
2249 PerlLIO_close(pp[0]);
2250 did_pipes = 0;
2251 if (n) { /* Error */
2252 int pid2, status;
8c51524e 2253 PerlLIO_close(p[This]);
1f852d0d
NIS
2254 if (n != sizeof(int))
2255 Perl_croak(aTHX_ "panic: kid popen errno read");
2256 do {
2257 pid2 = wait4pid(pid, &status, 0);
2258 } while (pid2 == -1 && errno == EINTR);
2259 errno = errkid; /* Propagate errno from kid */
4608196e 2260 return NULL;
1f852d0d
NIS
2261 }
2262 }
2263 if (did_pipes)
2264 PerlLIO_close(pp[0]);
2265 return PerlIO_fdopen(p[This], mode);
2266#else
4a7d1889
NIS
2267 Perl_croak(aTHX_ "List form of piped open not implemented");
2268 return (PerlIO *) NULL;
1f852d0d 2269#endif
4a7d1889
NIS
2270}
2271
5f05dabc 2272 /* VMS' my_popen() is in VMS.c, same with OS/2. */
cd39f2b6 2273#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 2274PerlIO *
3dd43144 2275Perl_my_popen(pTHX_ const char *cmd, const char *mode)
a687059c 2276{
97aff369 2277 dVAR;
a687059c 2278 int p[2];
8ac85365 2279 register I32 This, that;
d8a83dd3 2280 register Pid_t pid;
79072805 2281 SV *sv;
bfce84ec 2282 const I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
e446cec8
IZ
2283 I32 did_pipes = 0;
2284 int pp[2];
a687059c 2285
45bc9206 2286 PERL_FLUSHALL_FOR_CHILD;
ddcf38b7
IZ
2287#ifdef OS2
2288 if (doexec) {
23da6c43 2289 return my_syspopen(aTHX_ cmd,mode);
ddcf38b7 2290 }
a1d180c4 2291#endif
8ac85365
NIS
2292 This = (*mode == 'w');
2293 that = !This;
3280af22 2294 if (doexec && PL_tainting) {
bbce6d69
PP
2295 taint_env();
2296 taint_proper("Insecure %s%s", "EXEC");
d48672a2 2297 }
c2267164 2298 if (PerlProc_pipe(p) < 0)
4608196e 2299 return NULL;
e446cec8
IZ
2300 if (doexec && PerlProc_pipe(pp) >= 0)
2301 did_pipes = 1;
52e18b1f 2302 while ((pid = PerlProc_fork()) < 0) {
a687059c 2303 if (errno != EAGAIN) {
6ad3d225 2304 PerlLIO_close(p[This]);
b5ac89c3 2305 PerlLIO_close(p[that]);
e446cec8
IZ
2306 if (did_pipes) {
2307 PerlLIO_close(pp[0]);
2308 PerlLIO_close(pp[1]);
2309 }
a687059c 2310 if (!doexec)
cea2e8a9 2311 Perl_croak(aTHX_ "Can't fork");
4608196e 2312 return NULL;
a687059c
LW
2313 }
2314 sleep(5);
2315 }
2316 if (pid == 0) {
79072805
LW
2317 GV* tmpgv;
2318
30ac6d9b
GS
2319#undef THIS
2320#undef THAT
a687059c 2321#define THIS that
8ac85365 2322#define THAT This
e446cec8
IZ
2323 if (did_pipes) {
2324 PerlLIO_close(pp[0]);
2325#if defined(HAS_FCNTL) && defined(F_SETFD)
2326 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
2327#endif
2328 }
a687059c 2329 if (p[THIS] != (*mode == 'r')) {
6ad3d225
GS
2330 PerlLIO_dup2(p[THIS], *mode == 'r');
2331 PerlLIO_close(p[THIS]);
b5ac89c3
NIS
2332 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2333 PerlLIO_close(p[THAT]);
a687059c 2334 }
b5ac89c3
NIS
2335 else
2336 PerlLIO_close(p[THAT]);
4435c477 2337#ifndef OS2
a687059c 2338 if (doexec) {
a0d0e21e 2339#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
2340#ifndef NOFILE
2341#define NOFILE 20
2342#endif
a080fe3d 2343 {
3aed30dc 2344 int fd;
a080fe3d
NIS
2345
2346 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2347 if (fd != pp[1])
3aed30dc 2348 PerlLIO_close(fd);
a080fe3d 2349 }
ae986130 2350#endif
a080fe3d
NIS
2351 /* may or may not use the shell */
2352 do_exec3(cmd, pp[1], did_pipes);
6ad3d225 2353 PerlProc__exit(1);
a687059c 2354 }
4435c477 2355#endif /* defined OS2 */
fafc274c 2356 if ((tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV))) {
4d76a344 2357 SvREADONLY_off(GvSV(tmpgv));
7766f137 2358 sv_setiv(GvSV(tmpgv), PerlProc_getpid());
4d76a344
RGS
2359 SvREADONLY_on(GvSV(tmpgv));
2360 }
2361#ifdef THREADS_HAVE_PIDS
2362 PL_ppid = (IV)getppid();
2363#endif
3280af22 2364 PL_forkprocess = 0;
ca0c25f6 2365#ifdef PERL_USES_PL_PIDSTATUS
3280af22 2366 hv_clear(PL_pidstatus); /* we have no children */
ca0c25f6 2367#endif
4608196e 2368 return NULL;
a687059c
LW
2369#undef THIS
2370#undef THAT
2371 }
b5ac89c3 2372 do_execfree(); /* free any memory malloced by child on vfork */
e446cec8
IZ
2373 if (did_pipes)
2374 PerlLIO_close(pp[1]);
8ac85365 2375 if (p[that] < p[This]) {
6ad3d225
GS
2376 PerlLIO_dup2(p[This], p[that]);
2377 PerlLIO_close(p[This]);
8ac85365 2378 p[This] = p[that];
62b28dd9 2379 }
b5ac89c3
NIS
2380 else
2381 PerlLIO_close(p[that]);
2382
4755096e 2383 LOCK_FDPID_MUTEX;
3280af22 2384 sv = *av_fetch(PL_fdpid,p[This],TRUE);
4755096e 2385 UNLOCK_FDPID_MUTEX;
862a34c6 2386 SvUPGRADE(sv,SVt_IV);
45977657 2387 SvIV_set(sv, pid);
3280af22 2388 PL_forkprocess = pid;
e446cec8
IZ
2389 if (did_pipes && pid > 0) {
2390 int errkid;
bb7a0f54
MHM
2391 unsigned n = 0;
2392 SSize_t n1;
e446cec8
IZ
2393
2394 while (n < sizeof(int)) {
2395 n1 = PerlLIO_read(pp[0],
2396 (void*)(((char*)&errkid)+n),
2397 (sizeof(int)) - n);
2398 if (n1 <= 0)
2399 break;
2400 n += n1;
2401 }
2f96c702
IZ
2402 PerlLIO_close(pp[0]);
2403 did_pipes = 0;
e446cec8 2404 if (n) { /* Error */
faa466a7 2405 int pid2, status;
8c51524e 2406 PerlLIO_close(p[This]);
e446cec8 2407 if (n != sizeof(int))
cea2e8a9 2408 Perl_croak(aTHX_ "panic: kid popen errno read");
faa466a7
RG
2409 do {
2410 pid2 = wait4pid(pid, &status, 0);
2411 } while (pid2 == -1 && errno == EINTR);
e446cec8 2412 errno = errkid; /* Propagate errno from kid */
4608196e 2413 return NULL;
e446cec8
IZ
2414 }
2415 }
2416 if (did_pipes)
2417 PerlLIO_close(pp[0]);
8ac85365 2418 return PerlIO_fdopen(p[This], mode);
a687059c 2419}
7c0587c8 2420#else
85ca448a 2421#if defined(atarist) || defined(EPOC)
7c0587c8 2422FILE *popen();
760ac839 2423PerlIO *
7dc3a6bf 2424Perl_my_popen((pTHX_ const char *cmd, const char *mode)
7c0587c8 2425{
45bc9206 2426 PERL_FLUSHALL_FOR_CHILD;
a1d180c4
NIS
2427 /* Call system's popen() to get a FILE *, then import it.
2428 used 0 for 2nd parameter to PerlIO_importFILE;
2429 apparently not used
2430 */
2431 return PerlIO_importFILE(popen(cmd, mode), 0);
7c0587c8 2432}
2b96b0a5
JH
2433#else
2434#if defined(DJGPP)
2435FILE *djgpp_popen();
2436PerlIO *
7dc3a6bf 2437Perl_my_popen((pTHX_ const char *cmd, const char *mode)
2b96b0a5
JH
2438{
2439 PERL_FLUSHALL_FOR_CHILD;
2440 /* Call system's popen() to get a FILE *, then import it.
2441 used 0 for 2nd parameter to PerlIO_importFILE;
2442 apparently not used
2443 */
2444 return PerlIO_importFILE(djgpp_popen(cmd, mode), 0);
2445}
2446#endif
7c0587c8
LW
2447#endif
2448
2449#endif /* !DOSISH */
a687059c 2450
52e18b1f
GS
2451/* this is called in parent before the fork() */
2452void
2453Perl_atfork_lock(void)
2454{
27da23d5 2455 dVAR;
3db8f154 2456#if defined(USE_ITHREADS)
52e18b1f
GS
2457 /* locks must be held in locking order (if any) */
2458# ifdef MYMALLOC
2459 MUTEX_LOCK(&PL_malloc_mutex);
2460# endif
2461 OP_REFCNT_LOCK;
2462#endif
2463}
2464
2465/* this is called in both parent and child after the fork() */
2466void
2467Perl_atfork_unlock(void)
2468{
27da23d5 2469 dVAR;
3db8f154 2470#if defined(USE_ITHREADS)
52e18b1f
GS
2471 /* locks must be released in same order as in atfork_lock() */
2472# ifdef MYMALLOC
2473 MUTEX_UNLOCK(&PL_malloc_mutex);
2474# endif
2475 OP_REFCNT_UNLOCK;
2476#endif
2477}
2478
2479Pid_t
2480Perl_my_fork(void)
2481{
2482#if defined(HAS_FORK)
2483 Pid_t pid;
3db8f154 2484#if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK)
52e18b1f
GS
2485 atfork_lock();
2486 pid = fork();
2487 atfork_unlock();
2488#else
2489 /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
2490 * handlers elsewhere in the code */
2491 pid = fork();
2492#endif
2493 return pid;
2494#else
2495 /* this "canna happen" since nothing should be calling here if !HAS_FORK */
2496 Perl_croak_nocontext("fork() not available");
b961a566 2497 return 0;
52e18b1f
GS
2498#endif /* HAS_FORK */
2499}
2500
748a9306 2501#ifdef DUMP_FDS
35ff7856 2502void
864dbfa3 2503Perl_dump_fds(pTHX_ char *s)
ae986130
LW
2504{
2505 int fd;
c623ac67 2506 Stat_t tmpstatbuf;
ae986130 2507
bf49b057 2508 PerlIO_printf(Perl_debug_log,"%s", s);
ae986130 2509 for (fd = 0; fd < 32; fd++) {
6ad3d225 2510 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
bf49b057 2511 PerlIO_printf(Perl_debug_log," %d",fd);
ae986130 2512 }
bf49b057 2513 PerlIO_printf(Perl_debug_log,"\n");
27da23d5 2514 return;
ae986130 2515}
35ff7856 2516#endif /* DUMP_FDS */
ae986130 2517
fe14fcc3 2518#ifndef HAS_DUP2
fec02dd3 2519int
ba106d47 2520dup2(int oldfd, int newfd)
a687059c 2521{
a0d0e21e 2522#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
2523 if (oldfd == newfd)
2524 return oldfd;
6ad3d225 2525 PerlLIO_close(newfd);
fec02dd3 2526 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 2527#else
fc36a67e
PP
2528#define DUP2_MAX_FDS 256
2529 int fdtmp[DUP2_MAX_FDS];
79072805 2530 I32 fdx = 0;
ae986130
LW
2531 int fd;
2532
fe14fcc3 2533 if (oldfd == newfd)
fec02dd3 2534 return oldfd;
6ad3d225 2535 PerlLIO_close(newfd);
fc36a67e 2536 /* good enough for low fd's... */
6ad3d225 2537 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
fc36a67e 2538 if (fdx >= DUP2_MAX_FDS) {
6ad3d225 2539 PerlLIO_close(fd);
fc36a67e
PP
2540 fd = -1;
2541 break;
2542 }
ae986130 2543 fdtmp[fdx++] = fd;
fc36a67e 2544 }
ae986130 2545 while (fdx > 0)
6ad3d225 2546 PerlLIO_close(fdtmp[--fdx]);
fec02dd3 2547 return fd;
62b28dd9 2548#endif
a687059c
LW
2549}
2550#endif
2551
64ca3a65 2552#ifndef PERL_MICRO
ff68c719
PP
2553#ifdef HAS_SIGACTION
2554
abea2c45
HS
2555#ifdef MACOS_TRADITIONAL
2556/* We don't want restart behavior on MacOS */
2557#undef SA_RESTART
2558#endif
2559
ff68c719 2560Sighandler_t
864dbfa3 2561Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
ff68c719 2562{
27da23d5 2563 dVAR;
ff68c719
PP
2564 struct sigaction act, oact;
2565
a10b1e10
JH
2566#ifdef USE_ITHREADS
2567 /* only "parent" interpreter can diddle signals */
2568 if (PL_curinterp != aTHX)
8aad04aa 2569 return (Sighandler_t) SIG_ERR;
a10b1e10
JH
2570#endif
2571
8aad04aa 2572 act.sa_handler = (void(*)(int))handler;
ff68c719
PP
2573 sigemptyset(&act.sa_mask);
2574 act.sa_flags = 0;
2575#ifdef SA_RESTART
4ffa73a3
JH
2576 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2577 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
0a8e0eff 2578#endif
358837b8 2579#if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
8aad04aa 2580 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
85264bed
CS
2581 act.sa_flags |= SA_NOCLDWAIT;
2582#endif
ff68c719 2583 if (sigaction(signo, &act, &oact) == -1)
8aad04aa 2584 return (Sighandler_t) SIG_ERR;
ff68c719 2585 else
8aad04aa 2586 return (Sighandler_t) oact.sa_handler;
ff68c719
PP
2587}
2588
2589Sighandler_t
864dbfa3 2590Perl_rsignal_state(pTHX_ int signo)
ff68c719
PP
2591{
2592 struct sigaction oact;
96a5add6 2593 PERL_UNUSED_CONTEXT;
ff68c719
PP
2594
2595 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
8aad04aa 2596 return (Sighandler_t) SIG_ERR;
ff68c719 2597 else
8aad04aa 2598 return (Sighandler_t) oact.sa_handler;
ff68c719
PP
2599}
2600
2601int
864dbfa3 2602Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2603{
27da23d5 2604 dVAR;
ff68c719
PP
2605 struct sigaction act;
2606
a10b1e10
JH
2607#ifdef USE_ITHREADS
2608 /* only "parent" interpreter can diddle signals */
2609 if (PL_curinterp != aTHX)
2610 return -1;
2611#endif
2612
8aad04aa 2613 act.sa_handler = (void(*)(int))handler;
ff68c719
PP
2614 sigemptyset(&act.sa_mask);
2615 act.sa_flags = 0;
2616#ifdef SA_RESTART
4ffa73a3
JH
2617 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2618 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
0a8e0eff 2619#endif
36b5d377 2620#if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
8aad04aa 2621 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
85264bed
CS
2622 act.sa_flags |= SA_NOCLDWAIT;
2623#endif
ff68c719
PP
2624 return sigaction(signo, &act, save);
2625}
2626
2627int
864dbfa3 2628Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
ff68c719 2629{
27da23d5 2630 dVAR;
a10b1e10
JH
2631#ifdef USE_ITHREADS
2632 /* only "parent" interpreter can diddle signals */
2633 if (PL_curinterp != aTHX)
2634 return -1;
2635#endif
2636
ff68c719
PP
2637 return sigaction(signo, save, (struct sigaction *)NULL);
2638}
2639
2640#else /* !HAS_SIGACTION */
2641
2642Sighandler_t
864dbfa3 2643Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
ff68c719 2644{
39f1703b 2645#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2646 /* only "parent" interpreter can diddle signals */
2647 if (PL_curinterp != aTHX)
8aad04aa 2648 return (Sighandler_t) SIG_ERR;
a10b1e10
JH
2649#endif
2650
6ad3d225 2651 return PerlProc_signal(signo, handler);
ff68c719
PP
2652}
2653
fabdb6c0 2654static Signal_t
4e35701f 2655sig_trap(int signo)
ff68c719 2656{
27da23d5
JH
2657 dVAR;
2658 PL_sig_trapped++;
ff68c719
PP
2659}
2660
2661Sighandler_t
864dbfa3 2662Perl_rsignal_state(pTHX_ int signo)
ff68c719 2663{
27da23d5 2664 dVAR;
ff68c719
PP
2665 Sighandler_t oldsig;
2666
39f1703b 2667#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2668 /* only "parent" interpreter can diddle signals */
2669 if (PL_curinterp != aTHX)
8aad04aa 2670 return (Sighandler_t) SIG_ERR;
a10b1e10
JH
2671#endif
2672
27da23d5 2673 PL_sig_trapped = 0;
6ad3d225
GS
2674 oldsig = PerlProc_signal(signo, sig_trap);
2675 PerlProc_signal(signo, oldsig);
27da23d5 2676 if (PL_sig_trapped)
3aed30dc 2677 PerlProc_kill(PerlProc_getpid(), signo);
ff68c719
PP
2678 return oldsig;
2679}
2680
2681int
864dbfa3 2682Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2683{
39f1703b 2684#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2685 /* only "parent" interpreter can diddle signals */
2686 if (PL_curinterp != aTHX)
2687 return -1;
2688#endif
6ad3d225 2689 *save = PerlProc_signal(signo, handler);
8aad04aa 2690 return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0;
ff68c719
PP
2691}
2692
2693int
864dbfa3 2694Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
ff68c719 2695{
39f1703b 2696#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2697 /* only "parent" interpreter can diddle signals */
2698 if (PL_curinterp != aTHX)
2699 return -1;
2700#endif
8aad04aa 2701 return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0;
ff68c719
PP
2702}
2703
2704#endif /* !HAS_SIGACTION */
64ca3a65 2705#endif /* !PERL_MICRO */
ff68c719 2706
5f05dabc 2707 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
cd39f2b6 2708#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
79072805 2709I32
864dbfa3 2710Perl_my_pclose(pTHX_ PerlIO *ptr)
a687059c 2711{
97aff369 2712 dVAR;
ff68c719 2713 Sigsave_t hstat, istat, qstat;
a687059c 2714 int status;
a0d0e21e 2715 SV **svp;
d8a83dd3
JH
2716 Pid_t pid;
2717 Pid_t pid2;
03136e13 2718 bool close_failed;
b7953727 2719 int saved_errno = 0;
22fae026
TM
2720#ifdef WIN32
2721 int saved_win32_errno;
2722#endif
a687059c 2723
4755096e 2724 LOCK_FDPID_MUTEX;
3280af22 2725 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
4755096e 2726 UNLOCK_FDPID_MUTEX;
25d92023 2727 pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
a0d0e21e 2728 SvREFCNT_dec(*svp);
3280af22 2729 *svp = &PL_sv_undef;
ddcf38b7
IZ
2730#ifdef OS2
2731 if (pid == -1) { /* Opened by popen. */
2732 return my_syspclose(ptr);
2733 }
a1d180c4 2734#endif
03136e13
CS
2735 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2736 saved_errno = errno;
22fae026
TM
2737#ifdef WIN32
2738 saved_win32_errno = GetLastError();
2739#endif
03136e13 2740 }
7c0587c8 2741#ifdef UTS
6ad3d225 2742 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
7c0587c8 2743#endif
64ca3a65 2744#ifndef PERL_MICRO
8aad04aa
JH
2745 rsignal_save(SIGHUP, (Sighandler_t) SIG_IGN, &hstat);
2746 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &istat);
2747 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qstat);
64ca3a65 2748#endif
748a9306 2749 do {
1d3434b8
GS
2750 pid2 = wait4pid(pid, &status, 0);
2751 } while (pid2 == -1 && errno == EINTR);
64ca3a65 2752#ifndef PERL_MICRO
ff68c719
PP
2753 rsignal_restore(SIGHUP, &hstat);
2754 rsignal_restore(SIGINT, &istat);
2755 rsignal_restore(SIGQUIT, &qstat);
64ca3a65 2756#endif
03136e13 2757 if (close_failed) {
ce6e1103 2758 SETERRNO(saved_errno, 0);
03136e13
CS
2759 return -1;
2760 }
1d3434b8 2761 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
20188a90 2762}
4633a7c4
LW
2763#endif /* !DOSISH */
2764
2986a63f 2765#if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(MACOS_TRADITIONAL)
79072805 2766I32
d8a83dd3 2767Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
20188a90 2768{
97aff369 2769 dVAR;
27da23d5 2770 I32 result = 0;
b7953727
JH
2771 if (!pid)
2772 return -1;
ca0c25f6 2773#ifdef PERL_USES_PL_PIDSTATUS
b7953727 2774 {
3aed30dc 2775 if (pid > 0) {
12072db5
NC
2776 /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the
2777 pid, rather than a string form. */
c4420975 2778 SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE);
3aed30dc
HS
2779 if (svp && *svp != &PL_sv_undef) {
2780 *statusp = SvIVX(*svp);
12072db5
NC
2781 (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),
2782 G_DISCARD);
3aed30dc
HS
2783 return pid;
2784 }
2785 }
2786 else {
2787 HE *entry;
2788
2789 hv_iterinit(PL_pidstatus);
2790 if ((entry = hv_iternext(PL_pidstatus))) {
c4420975 2791 SV * const sv = hv_iterval(PL_pidstatus,entry);
7ea75b61 2792 I32 len;
0bcc34c2 2793 const char * const spid = hv_iterkey(entry,&len);
27da23d5 2794
12072db5
NC
2795 assert (len == sizeof(Pid_t));
2796 memcpy((char *)&pid, spid, len);
3aed30dc 2797 *statusp = SvIVX(sv);
7b9a3241
NC
2798 /* The hash iterator is currently on this entry, so simply
2799 calling hv_delete would trigger the lazy delete, which on
2800 aggregate does more work, beacuse next call to hv_iterinit()
2801 would spot the flag, and have to call the delete routine,
2802 while in the meantime any new entries can't re-use that
2803 memory. */
2804 hv_iterinit(PL_pidstatus);
7ea75b61 2805 (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD);
3aed30dc
HS
2806 return pid;
2807 }
20188a90
LW
2808 }
2809 }
68a29c53 2810#endif
79072805 2811#ifdef HAS_WAITPID
367f3c24
IZ
2812# ifdef HAS_WAITPID_RUNTIME
2813 if (!HAS_WAITPID_RUNTIME)
2814 goto hard_way;
2815# endif
cddd4526 2816 result = PerlProc_waitpid(pid,statusp,flags);
dfcfdb64 2817 goto finish;
367f3c24
IZ
2818#endif
2819#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
4608196e 2820 result = wait4((pid==-1)?0:pid,statusp,flags,NULL);
dfcfdb64 2821 goto finish;
367f3c24 2822#endif
ca0c25f6 2823#ifdef PERL_USES_PL_PIDSTATUS
27da23d5 2824#if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME)
367f3c24 2825 hard_way:
27da23d5 2826#endif
a0d0e21e 2827 {
a0d0e21e 2828 if (flags)
cea2e8a9 2829 Perl_croak(aTHX_ "Can't do waitpid with flags");
a0d0e21e 2830 else {
76e3520e 2831 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
a0d0e21e
LW
2832 pidgone(result,*statusp);
2833 if (result < 0)
2834 *statusp = -1;
2835 }
a687059c
LW
2836 }
2837#endif
27da23d5 2838#if defined(HAS_WAITPID) || defined(HAS_WAIT4)
dfcfdb64 2839 finish:
27da23d5 2840#endif
cddd4526
NIS
2841 if (result < 0 && errno == EINTR) {
2842 PERL_ASYNC_CHECK();
2843 }
2844 return result;
a687059c 2845}
2986a63f 2846#endif /* !DOSISH || OS2 || WIN32 || NETWARE */
a687059c 2847
ca0c25f6 2848#ifdef PERL_USES_PL_PIDSTATUS
7c0587c8 2849void
d8a83dd3 2850Perl_pidgone(pTHX_ Pid_t pid, int status)
a687059c 2851{
79072805 2852 register SV *sv;
a687059c 2853
12072db5 2854 sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE);
862a34c6 2855 SvUPGRADE(sv,SVt_IV);
45977657 2856 SvIV_set(sv, status);
20188a90 2857 return;
a687059c 2858}
ca0c25f6 2859#endif
a687059c 2860
85ca448a 2861#if defined(atarist) || defined(OS2) || defined(EPOC)
7c0587c8 2862int pclose();
ddcf38b7
IZ
2863#ifdef HAS_FORK
2864int /* Cannot prototype with I32
2865 in os2ish.h. */
ba106d47 2866my_syspclose(PerlIO *ptr)
ddcf38b7 2867#else
79072805 2868I32
864dbfa3 2869Perl_my_pclose(pTHX_ PerlIO *ptr)
a1d180c4 2870#endif
a687059c 2871{
760ac839 2872 /* Needs work for PerlIO ! */
c4420975 2873 FILE * const f = PerlIO_findFILE(ptr);
7452cf6a 2874 const I32 result = pclose(f);
2b96b0a5
JH
2875 PerlIO_releaseFILE(ptr,f);
2876 return result;
2877}
2878#endif
2879
933fea7f 2880#if defined(DJGPP)
2b96b0a5
JH
2881int djgpp_pclose();
2882I32
2883Perl_my_pclose(pTHX_ PerlIO *ptr)
2884{
2885 /* Needs work for PerlIO ! */
c4420975 2886 FILE * const f = PerlIO_findFILE(ptr);
2b96b0a5 2887 I32 result = djgpp_pclose(f);
933fea7f 2888 result = (result << 8) & 0xff00;
760ac839
LW
2889 PerlIO_releaseFILE(ptr,f);
2890 return result;
a687059c 2891}
7c0587c8 2892#endif
9f68db38
LW
2893
2894void
864dbfa3 2895Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, register I32 count)
9f68db38 2896{
79072805 2897 register I32 todo;
c4420975 2898 register const char * const frombase = from;
96a5add6 2899 PERL_UNUSED_CONTEXT;
9f68db38
LW
2900
2901 if (len == 1) {
08105a92 2902 register const char c = *from;
9f68db38 2903 while (count-- > 0)
5926133d 2904 *to++ = c;
9f68db38
LW
2905 return;
2906 }
2907 while (count-- > 0) {
2908 for (todo = len; todo > 0; todo--) {
2909 *to++ = *from++;
2910 }
2911 from = frombase;
2912 }
2913}
0f85fab0 2914
fe14fcc3 2915#ifndef HAS_RENAME
79072805 2916I32
4373e329 2917Perl_same_dirent(pTHX_ const char *a, const char *b)
62b28dd9 2918{
93a17b20
LW
2919 char *fa = strrchr(a,'/');
2920 char *fb = strrchr(b,'/');
c623ac67
GS
2921 Stat_t tmpstatbuf1;
2922 Stat_t tmpstatbuf2;
c4420975 2923 SV * const tmpsv = sv_newmortal();
62b28dd9
LW
2924
2925 if (fa)
2926 fa++;
2927 else
2928 fa = a;
2929 if (fb)
2930 fb++;
2931 else
2932 fb = b;
2933 if (strNE(a,b))
2934 return FALSE;
2935 if (fa == a)
616d8c9c 2936 sv_setpvn(tmpsv, ".", 1);
62b28dd9 2937 else
46fc3d4c 2938 sv_setpvn(tmpsv, a, fa - a);
95a20fc0 2939 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2940 return FALSE;
2941 if (fb == b)
616d8c9c 2942 sv_setpvn(tmpsv, ".", 1);
62b28dd9 2943 else
46fc3d4c 2944 sv_setpvn(tmpsv, b, fb - b);
95a20fc0 2945 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2946 return FALSE;
2947 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2948 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2949}
fe14fcc3
LW
2950#endif /* !HAS_RENAME */
2951
491527d0 2952char*
7f315aed
NC
2953Perl_find_script(pTHX_ const char *scriptname, bool dosearch,
2954 const char *const *const search_ext, I32 flags)
491527d0 2955{
97aff369 2956 dVAR;
bd61b366
SS
2957 const char *xfound = NULL;
2958 char *xfailed = NULL;
0f31cffe 2959 char tmpbuf[MAXPATHLEN];
491527d0 2960 register char *s;
5f74f29c 2961 I32 len = 0;
491527d0
GS
2962 int retval;
2963#if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2964# define SEARCH_EXTS ".bat", ".cmd", NULL
2965# define MAX_EXT_LEN 4
2966#endif
2967#ifdef OS2
2968# define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2969# define MAX_EXT_LEN 4
2970#endif
2971#ifdef VMS
2972# define SEARCH_EXTS ".pl", ".com", NULL
2973# define MAX_EXT_LEN 4
2974#endif
2975 /* additional extensions to try in each dir if scriptname not found */
2976#ifdef SEARCH_EXTS
0bcc34c2 2977 static const char *const exts[] = { SEARCH_EXTS };
7f315aed 2978 const char *const *const ext = search_ext ? search_ext : exts;
491527d0 2979 int extidx = 0, i = 0;
bd61b366 2980 const char *curext = NULL;
491527d0 2981#else
53c1dcc0 2982 PERL_UNUSED_ARG(search_ext);
491527d0
GS
2983# define MAX_EXT_LEN 0
2984#endif
2985
2986 /*
2987 * If dosearch is true and if scriptname does not contain path
2988 * delimiters, search the PATH for scriptname.
2989 *
2990 * If SEARCH_EXTS is also defined, will look for each
2991 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2992 * while searching the PATH.
2993 *
2994 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2995 * proceeds as follows:
2996 * If DOSISH or VMSISH:
2997 * + look for ./scriptname{,.foo,.bar}
2998 * + search the PATH for scriptname{,.foo,.bar}
2999 *
3000 * If !DOSISH:
3001 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
3002 * this will not look in '.' if it's not in the PATH)
3003 */
84486fc6 3004 tmpbuf[0] = '\0';
491527d0
GS
3005
3006#ifdef VMS
3007# ifdef ALWAYS_DEFTYPES
3008 len = strlen(scriptname);
3009 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
c4420975 3010 int idx = 0, deftypes = 1;
491527d0
GS
3011 bool seen_dot = 1;
3012
bd61b366 3013 const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL);
491527d0
GS
3014# else
3015 if (dosearch) {
c4420975 3016 int idx = 0, deftypes = 1;
491527d0
GS
3017 bool seen_dot = 1;
3018
bd61b366 3019 const int hasdir = (strpbrk(scriptname,":[</") != NULL);
491527d0
GS
3020# endif
3021 /* The first time through, just add SEARCH_EXTS to whatever we
3022 * already have, so we can check for default file types. */
3023 while (deftypes ||
84486fc6 3024 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
491527d0
GS
3025 {
3026 if (deftypes) {
3027 deftypes = 0;
84486fc6 3028 *tmpbuf = '\0';
491527d0 3029 }
84486fc6
GS
3030 if ((strlen(tmpbuf) + strlen(scriptname)
3031 + MAX_EXT_LEN) >= sizeof tmpbuf)
491527d0 3032 continue; /* don't search dir with too-long name */
6fca0082 3033 my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf));
491527d0
GS
3034#else /* !VMS */
3035
3036#ifdef DOSISH
3037 if (strEQ(scriptname, "-"))
3038 dosearch = 0;
3039 if (dosearch) { /* Look in '.' first. */
fe2774ed 3040 const char *cur = scriptname;
491527d0
GS
3041#ifdef SEARCH_EXTS
3042 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
3043 while (ext[i])
3044 if (strEQ(ext[i++],curext)) {
3045 extidx = -1; /* already has an ext */
3046 break;
3047 }
3048 do {
3049#endif
3050 DEBUG_p(PerlIO_printf(Perl_debug_log,
3051 "Looking for %s\n",cur));
017f25f1
IZ
3052 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
3053 && !S_ISDIR(PL_statbuf.st_mode)) {
491527d0
GS
3054 dosearch = 0;
3055 scriptname = cur;
3056#ifdef SEARCH_EXTS
3057 break;
3058#endif
3059 }
3060#ifdef SEARCH_EXTS
3061 if (cur == scriptname) {
3062 len = strlen(scriptname);
84486fc6 3063 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
491527d0 3064 break;
9e4425f7
SH
3065 my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf));
3066 cur = tmpbuf;
491527d0
GS
3067 }
3068 } while (extidx >= 0 && ext[extidx] /* try an extension? */
6fca0082 3069 && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len));
491527d0
GS
3070#endif
3071 }
3072#endif
3073
cd39f2b6
JH
3074#ifdef MACOS_TRADITIONAL
3075 if (dosearch && !strchr(scriptname, ':') &&
3076 (s = PerlEnv_getenv("Commands")))
3077#else
491527d0
GS
3078 if (dosearch && !strchr(scriptname, '/')
3079#ifdef DOSISH
3080 && !strchr(scriptname, '\\')
3081#endif
cd39f2b6
JH
3082 && (s = PerlEnv_getenv("PATH")))
3083#endif
3084 {
491527d0 3085 bool seen_dot = 0;
92f0c265 3086
3280af22
NIS
3087 PL_bufend = s + strlen(s);
3088 while (s < PL_bufend) {
cd39f2b6
JH
3089#ifdef MACOS_TRADITIONAL
3090 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
3091 ',',
3092 &len);
3093#else
491527d0
GS
3094#if defined(atarist) || defined(DOSISH)
3095 for (len = 0; *s
3096# ifdef atarist
3097 && *s != ','
3098# endif
3099 && *s != ';'; len++, s++) {
84486fc6
GS
3100 if (len < sizeof tmpbuf)
3101 tmpbuf[len] = *s;
491527d0 3102 }
84486fc6
GS
3103 if (len < sizeof tmpbuf)
3104 tmpbuf[len] = '\0';
491527d0 3105#else /* ! (atarist || DOSISH) */
3280af22 3106 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
491527d0
GS
3107 ':',
3108 &len);
3109#endif /* ! (atarist || DOSISH) */
cd39f2b6 3110#endif /* MACOS_TRADITIONAL */
3280af22 3111 if (s < PL_bufend)
491527d0 3112 s++;
84486fc6 3113 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
491527d0 3114 continue; /* don't search dir with too-long name */
cd39f2b6
JH
3115#ifdef MACOS_TRADITIONAL
3116 if (len && tmpbuf[len - 1] != ':')
3117 tmpbuf[len++] = ':';
3118#else
491527d0 3119 if (len
490a0e98 3120# if defined(atarist) || defined(__MINT__) || defined(DOSISH)
84486fc6
GS
3121 && tmpbuf[len - 1] != '/'
3122 && tmpbuf[len - 1] != '\\'
490a0e98 3123# endif
491527d0 3124 )
84486fc6
GS
3125 tmpbuf[len++] = '/';
3126 if (len == 2 && tmpbuf[0] == '.')
491527d0 3127 seen_dot = 1;
cd39f2b6 3128#endif
28f0d0ec 3129 (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
491527d0
GS
3130#endif /* !VMS */
3131
3132#ifdef SEARCH_EXTS
84486fc6 3133 len = strlen(tmpbuf);
491527d0
GS
3134 if (extidx > 0) /* reset after previous loop */
3135 extidx = 0;
3136 do {
3137#endif
84486fc6 3138 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3280af22 3139 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
017f25f1
IZ
3140 if (S_ISDIR(PL_statbuf.st_mode)) {
3141 retval = -1;
3142 }
491527d0
GS
3143#ifdef SEARCH_EXTS
3144 } while ( retval < 0 /* not there */
3145 && extidx>=0 && ext[extidx] /* try an extension? */
6fca0082 3146 && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)
491527d0
GS
3147 );
3148#endif
3149 if (retval < 0)
3150 continue;
3280af22
NIS
3151 if (S_ISREG(PL_statbuf.st_mode)
3152 && cando(S_IRUSR,TRUE,&PL_statbuf)
73811745 3153#if !defined(DOSISH) && !defined(MACOS_TRADITIONAL)
3280af22 3154 && cando(S_IXUSR,TRUE,&PL_statbuf)
491527d0
GS
3155#endif
3156 )
3157 {
3aed30dc 3158 xfound = tmpbuf; /* bingo! */
491527d0
GS
3159 break;
3160 }
3161 if (!xfailed)
84486fc6 3162 xfailed = savepv(tmpbuf);
491527d0
GS
3163 }
3164#ifndef DOSISH
017f25f1 3165 if (!xfound && !seen_dot && !xfailed &&
a1d180c4 3166 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
017f25f1 3167 || S_ISDIR(PL_statbuf.st_mode)))
491527d0
GS
3168#endif
3169 seen_dot = 1; /* Disable message. */
9ccb31f9
GS
3170 if (!xfound) {
3171 if (flags & 1) { /* do or die? */
3aed30dc 3172 Perl_croak(aTHX_ "Can't %s %s%s%s",
9ccb31f9
GS
3173 (xfailed ? "execute" : "find"),
3174 (xfailed ? xfailed : scriptname),
3175 (xfailed ? "" : " on PATH"),
3176 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
3177 }
bd61b366 3178 scriptname = NULL;
9ccb31f9 3179 }
43c5f42d 3180 Safefree(xfailed);
491527d0
GS
3181 scriptname = xfound;
3182 }
bd61b366 3183 return (scriptname ? savepv(scriptname) : NULL);
491527d0
GS
3184}
3185
ba869deb
GS
3186#ifndef PERL_GET_CONTEXT_DEFINED
3187
3188void *
3189Perl_get_context(void)
3190{
27da23d5 3191 dVAR;
3db8f154 3192#if defined(USE_ITHREADS)
ba869deb
GS
3193# ifdef OLD_PTHREADS_API
3194 pthread_addr_t t;
3195 if (pthread_getspecific(PL_thr_key, &t))
3196 Perl_croak_nocontext("panic: pthread_getspecific");
3197 return (void*)t;
3198# else
bce813aa 3199# ifdef I_MACH_CTHREADS
8b8b35ab 3200 return (void*)cthread_data(cthread_self());
bce813aa 3201# else
8b8b35ab
JH
3202 return (void*)PTHREAD_GETSPECIFIC(PL_thr_key);
3203# endif
c44d3fdb 3204# endif
ba869deb
GS
3205#else
3206 return (void*)NULL;
3207#endif
3208}
3209
3210void
3211Perl_set_context(void *t)
3212{
8772537c 3213 dVAR;
3db8f154 3214#if defined(USE_ITHREADS)
c44d3fdb
GS
3215# ifdef I_MACH_CTHREADS
3216 cthread_set_data(cthread_self(), t);
3217# else
ba869deb
GS
3218 if (pthread_setspecific(PL_thr_key, t))
3219 Perl_croak_nocontext("panic: pthread_setspecific");
c44d3fdb 3220# endif
b464bac0 3221#else
8772537c 3222 PERL_UNUSED_ARG(t);
ba869deb
GS
3223#endif
3224}
3225
3226#endif /* !PERL_GET_CONTEXT_DEFINED */
491527d0 3227
27da23d5 3228#if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
22239a37 3229struct perl_vars *
864dbfa3 3230Perl_GetVars(pTHX)
22239a37 3231{
533c011a 3232 return &PL_Vars;
22239a37 3233}
31fb1209
NIS
3234#endif
3235
1cb0ed9b 3236char **
864dbfa3 3237Perl_get_op_names(pTHX)
31fb1209 3238{
96a5add6
AL
3239 PERL_UNUSED_CONTEXT;
3240 return (char **)PL_op_name;
31fb1209
NIS
3241}
3242
1cb0ed9b 3243char **
864dbfa3 3244Perl_get_op_descs(pTHX)
31fb1209 3245{
96a5add6
AL
3246 PERL_UNUSED_CONTEXT;
3247 return (char **)PL_op_desc;
31fb1209 3248}
9e6b2b00 3249
e1ec3a88 3250const char *
864dbfa3 3251Perl_get_no_modify(pTHX)
9e6b2b00 3252{
96a5add6
AL
3253 PERL_UNUSED_CONTEXT;
3254 return PL_no_modify;
9e6b2b00
GS
3255}
3256
3257U32 *
864dbfa3 3258Perl_get_opargs(pTHX)
9e6b2b00 3259{
96a5add6
AL
3260 PERL_UNUSED_CONTEXT;
3261 return (U32 *)PL_opargs;
9e6b2b00 3262}
51aa15f3 3263
0cb96387
GS
3264PPADDR_t*
3265Perl_get_ppaddr(pTHX)
3266{
96a5add6
AL
3267 dVAR;
3268 PERL_UNUSED_CONTEXT;
3269 return (PPADDR_t*)PL_ppaddr;
0cb96387
GS
3270}
3271
a6c40364
GS
3272#ifndef HAS_GETENV_LEN
3273char *
bf4acbe4 3274Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
a6c40364 3275{
8772537c 3276 char * const env_trans = PerlEnv_getenv(env_elem);
96a5add6 3277 PERL_UNUSED_CONTEXT;
a6c40364
GS
3278 if (env_trans)
3279 *len = strlen(env_trans);
3280 return env_trans;
f675dbe5
CB
3281}
3282#endif
3283
dc9e4912
GS
3284
3285MGVTBL*
864dbfa3 3286Perl_get_vtbl(pTHX_ int vtbl_id)
dc9e4912 3287{
7452cf6a 3288 const MGVTBL* result;
96a5add6 3289 PERL_UNUSED_CONTEXT;
dc9e4912
GS
3290
3291 switch(vtbl_id) {
3292 case want_vtbl_sv:
3293 result = &PL_vtbl_sv;
3294 break;
3295 case want_vtbl_env:
3296 result = &PL_vtbl_env;
3297 break;
3298 case want_vtbl_envelem:
3299 result = &PL_vtbl_envelem;
3300 break;
3301 case want_vtbl_sig:
3302 result = &PL_vtbl_sig;
3303 break;
3304 case want_vtbl_sigelem:
3305 result = &PL_vtbl_sigelem;
3306 break;
3307 case want_vtbl_pack:
3308 result = &PL_vtbl_pack;
3309 break;
3310 case want_vtbl_packelem:
3311 result = &PL_vtbl_packelem;
3312 break;
3313 case want_vtbl_dbline:
3314 result = &PL_vtbl_dbline;
3315 break;
3316 case want_vtbl_isa:
3317 result = &PL_vtbl_isa;
3318 break;
3319 case want_vtbl_isaelem:
3320 result = &PL_vtbl_isaelem;
3321 break;
3322 case want_vtbl_arylen:
3323 result = &PL_vtbl_arylen;
3324 break;
dc9e4912
GS
3325 case want_vtbl_mglob:
3326 result = &PL_vtbl_mglob;
3327 break;
3328 case want_vtbl_nkeys:
3329 result = &PL_vtbl_nkeys;
3330 break;
3331 case want_vtbl_taint:
3332 result = &PL_vtbl_taint;
3333 break;
3334 case want_vtbl_substr:
3335 result = &PL_vtbl_substr;
3336 break;
3337 case want_vtbl_vec:
3338 result = &PL_vtbl_vec;
3339 break;
3340 case want_vtbl_pos:
3341 result = &PL_vtbl_pos;
3342 break;
3343 case want_vtbl_bm:
3344 result = &PL_vtbl_bm;
3345 break;
3346 case want_vtbl_fm:
3347 result = &PL_vtbl_fm;
3348 break;
3349 case want_vtbl_uvar:
3350 result = &PL_vtbl_uvar;
3351 break;
dc9e4912
GS
3352 case want_vtbl_defelem:
3353 result = &PL_vtbl_defelem;
3354 break;
3355 case want_vtbl_regexp:
3356 result = &PL_vtbl_regexp;
3357 break;
3358 case want_vtbl_regdata:
3359 result = &PL_vtbl_regdata;
3360 break;
3361 case want_vtbl_regdatum:
3362 result = &PL_vtbl_regdatum;
3363 break;
3c90161d 3364#ifdef USE_LOCALE_COLLATE
dc9e4912
GS
3365 case want_vtbl_collxfrm:
3366 result = &PL_vtbl_collxfrm;
3367 break;
3c90161d 3368#endif
dc9e4912
GS
3369 case want_vtbl_amagic:
3370 result = &PL_vtbl_amagic;
3371 break;
3372 case want_vtbl_amagicelem:
3373 result = &PL_vtbl_amagicelem;
3374 break;
810b8aa5
GS
3375 case want_vtbl_backref:
3376 result = &PL_vtbl_backref;
3377 break;
7e8c5dac
HS
3378 case want_vtbl_utf8:
3379 result = &PL_vtbl_utf8;
3380 break;
7452cf6a 3381 default:
4608196e 3382 result = NULL;
7452cf6a 3383 break;
dc9e4912 3384 }
27da23d5 3385 return (MGVTBL*)result;
dc9e4912
GS
3386}
3387
767df6a1 3388I32
864dbfa3 3389Perl_my_fflush_all(pTHX)
767df6a1 3390{
f800e14d 3391#if defined(USE_PERLIO) || defined(FFLUSH_NULL) || defined(USE_SFIO)
ce720889 3392 return PerlIO_flush(NULL);
767df6a1 3393#else
8fbdfb7c 3394# if defined(HAS__FWALK)
f13a2bc0 3395 extern int fflush(FILE *);
74cac757
JH
3396 /* undocumented, unprototyped, but very useful BSDism */
3397 extern void _fwalk(int (*)(FILE *));
8fbdfb7c 3398 _fwalk(&fflush);
74cac757 3399 return 0;
8fa7f367 3400# else
8fbdfb7c 3401# if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
8fa7f367 3402 long open_max = -1;
8fbdfb7c 3403# ifdef PERL_FFLUSH_ALL_FOPEN_MAX
d2201af2 3404 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
8fbdfb7c 3405# else
8fa7f367 3406# if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
767df6a1 3407 open_max = sysconf(_SC_OPEN_MAX);
8fa7f367
JH
3408# else
3409# ifdef FOPEN_MAX
74cac757 3410 open_max = FOPEN_MAX;
8fa7f367
JH
3411# else
3412# ifdef OPEN_MAX
74cac757 3413 open_max = OPEN_MAX;
8fa7f367
JH
3414# else
3415# ifdef _NFILE
d2201af2 3416 open_max = _NFILE;
8fa7f367
JH
3417# endif
3418# endif
74cac757 3419# endif
767df6a1
JH
3420# endif
3421# endif
767df6a1
JH
3422 if (open_max > 0) {
3423 long i;
3424 for (i = 0; i < open_max; i++)
d2201af2
AD
3425 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3426 STDIO_STREAM_ARRAY[i]._file < open_max &&
3427 STDIO_STREAM_ARRAY[i]._flag)
3428 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
767df6a1
JH
3429 return 0;
3430 }
8fbdfb7c 3431# endif
93189314 3432 SETERRNO(EBADF,RMS_IFI);
767df6a1 3433 return EOF;
74cac757 3434# endif
767df6a1
JH
3435#endif
3436}
097ee67d 3437
69282e91 3438void
e1ec3a88 3439Perl_report_evil_fh(pTHX_ const GV *gv, const IO *io, I32 op)
bc37a18f 3440{
b64e5050 3441 const char * const name = gv && isGV(gv) ? GvENAME(gv) : NULL;
66fc2fa5 3442
4c80c0b2 3443 if (op == OP_phoney_OUTPUT_ONLY || op == OP_phoney_INPUT_ONLY) {
3aed30dc 3444 if (ckWARN(WARN_IO)) {
10edeb5d
JH
3445 const char * const direction =
3446 (const char *)((op == OP_phoney_INPUT_ONLY) ? "in" : "out");
3aed30dc
HS
3447 if (name && *name)
3448 Perl_warner(aTHX_ packWARN(WARN_IO),
3449 "Filehandle %s opened only for %sput",
fd322ea4 3450 name, direction);
3aed30dc
HS
3451 else
3452 Perl_warner(aTHX_ packWARN(WARN_IO),
fd322ea4 3453 "Filehandle opened only for %sput", direction);
3aed30dc 3454 }
2dd78f96
JH
3455 }
3456 else {
e1ec3a88 3457 const char *vile;
3aed30dc
HS
3458 I32 warn_type;
3459
3460 if (gv && io && IoTYPE(io) == IoTYPE_CLOSED) {
3461 vile = "closed";
3462 warn_type = WARN_CLOSED;
3463 }
3464 else {
3465 vile = "unopened";
3466 warn_type = WARN_UNOPENED;
3467 }
3468
3469 if (ckWARN(warn_type)) {
10edeb5d
JH
3470 const char * const pars =
3471 (const char *)(OP_IS_FILETEST(op) ? "" : "()");
f0a09b71 3472 const char * const func =
10edeb5d
JH
3473 (const char *)
3474 (op == OP_READLINE ? "readline" : /* "<HANDLE>" not nice */
3475 op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */
3476 op < 0 ? "" : /* handle phoney cases */
3477 PL_op_desc[op]);
3478 const char * const type =
3479 (const char *)
3480 (OP_IS_SOCKET(op) ||
3481 (gv && io && IoTYPE(io) == IoTYPE_SOCKET) ?
3482 "socket" : "filehandle");
3aed30dc
HS
3483 if (name && *name) {
3484 Perl_warner(aTHX_ packWARN(warn_type),
3485 "%s%s on %s %s %s", func, pars, vile, type, name);
3486 if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3487 Perl_warner(
3488 aTHX_ packWARN(warn_type),
3489 "\t(Are you trying to call %s%s on dirhandle %s?)\n",
3490 func, pars, name
3491 );
3492 }
3493 else {
3494 Perl_warner(aTHX_ packWARN(warn_type),
3495 "%s%s on %s %s", func, pars, vile, type);
3496 if (gv && io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3497 Perl_warner(
3498 aTHX_ packWARN(warn_type),
3499 "\t(Are you trying to call %s%s on dirhandle?)\n",
3500 func, pars
3501 );
3502 }
3503 }
bc37a18f 3504 }
69282e91 3505}
a926ef6b
JH
3506
3507#ifdef EBCDIC
cbebf344
JH
3508/* in ASCII order, not that it matters */
3509static const char controllablechars[] = "?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
3510
a926ef6b
JH
3511int
3512Perl_ebcdic_control(pTHX_ int ch)
3513{
3aed30dc 3514 if (ch > 'a') {
e1ec3a88 3515 const char *ctlp;
3aed30dc
HS
3516
3517 if (islower(ch))
3518 ch = toupper(ch);
3519
3520 if ((ctlp = strchr(controllablechars, ch)) == 0) {
3521 Perl_die(aTHX_ "unrecognised control character '%c'\n", ch);
a926ef6b 3522 }
3aed30dc
HS
3523
3524 if (ctlp == controllablechars)
3525 return('\177'); /* DEL */
3526 else
3527 return((unsigned char)(ctlp - controllablechars - 1));
3528 } else { /* Want uncontrol */
3529 if (ch == '\177' || ch == -1)
3530 return('?');
3531 else if (ch == '\157')
3532 return('\177');
3533 else if (ch == '\174')
3534 return('\000');
3535 else if (ch == '^') /* '\137' in 1047, '\260' in 819 */
3536 return('\036');
3537 else if (ch == '\155')
3538 return('\037');
3539 else if (0 < ch && ch < (sizeof(controllablechars) - 1))
3540 return(controllablechars[ch+1]);
3541 else
3542 Perl_die(aTHX_ "invalid control request: '\\%03o'\n", ch & 0xFF);
3543 }
a926ef6b
JH
3544}
3545#endif
e72cf795 3546
f6adc668 3547/* To workaround core dumps from the uninitialised tm_zone we get the
e72cf795
JH
3548 * system to give us a reasonable struct to copy. This fix means that
3549 * strftime uses the tm_zone and tm_gmtoff values returned by
3550 * localtime(time()). That should give the desired result most of the
3551 * time. But probably not always!
3552 *
f6adc668
JH
3553 * This does not address tzname aspects of NETaa14816.
3554 *
e72cf795 3555 */
f6adc668 3556
e72cf795
JH
3557#ifdef HAS_GNULIBC
3558# ifndef STRUCT_TM_HASZONE
3559# define STRUCT_TM_HASZONE
3560# endif
3561#endif
3562
f6adc668
JH
3563#ifdef STRUCT_TM_HASZONE /* Backward compat */
3564# ifndef HAS_TM_TM_ZONE
3565# define HAS_TM_TM_ZONE
3566# endif
3567#endif
3568
e72cf795 3569void
f1208910 3570Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */
e72cf795 3571{
f6adc668 3572#ifdef HAS_TM_TM_ZONE
e72cf795 3573 Time_t now;
1b6737cc 3574 const struct tm* my_tm;
e72cf795 3575 (void)time(&now);
82c57498 3576 my_tm = localtime(&now);
ca46b8ee
SP
3577 if (my_tm)
3578 Copy(my_tm, ptm, 1, struct tm);
1b6737cc
AL
3579#else
3580 PERL_UNUSED_ARG(ptm);
e72cf795
JH
3581#endif
3582}
3583
3584/*
3585 * mini_mktime - normalise struct tm values without the localtime()
3586 * semantics (and overhead) of mktime().
3587 */
3588void
f1208910 3589Perl_mini_mktime(pTHX_ struct tm *ptm)
e72cf795
JH
3590{
3591 int yearday;
3592 int secs;
3593 int month, mday, year, jday;
3594 int odd_cent, odd_year;
96a5add6 3595 PERL_UNUSED_CONTEXT;
e72cf795
JH
3596
3597#define DAYS_PER_YEAR 365
3598#define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1)
3599#define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1)