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