| 1 |
root |
1.1 |
/* util.c |
| 2 |
|
|
* |
| 3 |
|
|
* Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
| 4 |
|
|
* 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others |
| 5 |
|
|
* |
| 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. |
| 8 |
|
|
* |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
/* |
| 12 |
|
|
* "Very useful, no doubt, that was to Saruman; yet it seems that he was |
| 13 |
|
|
* not content." --Gandalf |
| 14 |
|
|
*/ |
| 15 |
|
|
|
| 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 |
|
|
|
| 22 |
|
|
#include "EXTERN.h" |
| 23 |
|
|
#define PERL_IN_UTIL_C |
| 24 |
|
|
#include "perl.h" |
| 25 |
|
|
|
| 26 |
|
|
#ifndef PERL_MICRO |
| 27 |
|
|
#include <signal.h> |
| 28 |
|
|
#ifndef SIG_ERR |
| 29 |
|
|
# define SIG_ERR ((Sighandler_t) -1) |
| 30 |
|
|
#endif |
| 31 |
|
|
#endif |
| 32 |
|
|
|
| 33 |
|
|
#ifdef __Lynx__ |
| 34 |
|
|
/* Missing protos on LynxOS */ |
| 35 |
|
|
int putenv(char *); |
| 36 |
|
|
#endif |
| 37 |
|
|
|
| 38 |
|
|
#ifdef I_SYS_WAIT |
| 39 |
|
|
# include <sys/wait.h> |
| 40 |
|
|
#endif |
| 41 |
|
|
|
| 42 |
|
|
#ifdef HAS_SELECT |
| 43 |
|
|
# ifdef I_SYS_SELECT |
| 44 |
|
|
# include <sys/select.h> |
| 45 |
|
|
# endif |
| 46 |
|
|
#endif |
| 47 |
|
|
|
| 48 |
|
|
#define FLUSH |
| 49 |
|
|
|
| 50 |
|
|
#if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC) |
| 51 |
|
|
# define FD_CLOEXEC 1 /* NeXT needs this */ |
| 52 |
|
|
#endif |
| 53 |
|
|
|
| 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. |
| 57 |
|
|
* XXX This advice seems to be widely ignored :-( --AD August 1996. |
| 58 |
|
|
*/ |
| 59 |
|
|
|
| 60 |
|
|
/* paranoid version of system's malloc() */ |
| 61 |
|
|
|
| 62 |
|
|
Malloc_t |
| 63 |
|
|
Perl_safesysmalloc(MEM_SIZE size) |
| 64 |
|
|
{ |
| 65 |
|
|
dTHX; |
| 66 |
|
|
Malloc_t ptr; |
| 67 |
|
|
#ifdef HAS_64K_LIMIT |
| 68 |
|
|
if (size > 0xffff) { |
| 69 |
|
|
PerlIO_printf(Perl_error_log, |
| 70 |
|
|
"Allocation too large: %lx\n", size) FLUSH; |
| 71 |
|
|
my_exit(1); |
| 72 |
|
|
} |
| 73 |
|
|
#endif /* HAS_64K_LIMIT */ |
| 74 |
|
|
#ifdef DEBUGGING |
| 75 |
|
|
if ((long)size < 0) |
| 76 |
|
|
Perl_croak_nocontext("panic: malloc"); |
| 77 |
|
|
#endif |
| 78 |
|
|
ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
| 79 |
|
|
PERL_ALLOC_CHECK(ptr); |
| 80 |
|
|
DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); |
| 81 |
|
|
if (ptr != Nullch) |
| 82 |
|
|
return ptr; |
| 83 |
|
|
else if (PL_nomemok) |
| 84 |
|
|
return Nullch; |
| 85 |
|
|
else { |
| 86 |
|
|
/* Can't use PerlIO to write as it allocates memory */ |
| 87 |
|
|
PerlLIO_write(PerlIO_fileno(Perl_error_log), |
| 88 |
|
|
PL_no_mem, strlen(PL_no_mem)); |
| 89 |
|
|
my_exit(1); |
| 90 |
|
|
return Nullch; |
| 91 |
|
|
} |
| 92 |
|
|
/*NOTREACHED*/ |
| 93 |
|
|
} |
| 94 |
|
|
|
| 95 |
|
|
/* paranoid version of system's realloc() */ |
| 96 |
|
|
|
| 97 |
|
|
Malloc_t |
| 98 |
|
|
Perl_safesysrealloc(Malloc_t where,MEM_SIZE size) |
| 99 |
|
|
{ |
| 100 |
|
|
dTHX; |
| 101 |
|
|
Malloc_t ptr; |
| 102 |
|
|
#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO) |
| 103 |
|
|
Malloc_t PerlMem_realloc(); |
| 104 |
|
|
#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
| 105 |
|
|
|
| 106 |
|
|
#ifdef HAS_64K_LIMIT |
| 107 |
|
|
if (size > 0xffff) { |
| 108 |
|
|
PerlIO_printf(Perl_error_log, |
| 109 |
|
|
"Reallocation too large: %lx\n", size) FLUSH; |
| 110 |
|
|
my_exit(1); |
| 111 |
|
|
} |
| 112 |
|
|
#endif /* HAS_64K_LIMIT */ |
| 113 |
|
|
if (!size) { |
| 114 |
|
|
safesysfree(where); |
| 115 |
|
|
return NULL; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
|
if (!where) |
| 119 |
|
|
return safesysmalloc(size); |
| 120 |
|
|
#ifdef DEBUGGING |
| 121 |
|
|
if ((long)size < 0) |
| 122 |
|
|
Perl_croak_nocontext("panic: realloc"); |
| 123 |
|
|
#endif |
| 124 |
|
|
ptr = (Malloc_t)PerlMem_realloc(where,size); |
| 125 |
|
|
PERL_ALLOC_CHECK(ptr); |
| 126 |
|
|
|
| 127 |
|
|
DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++)); |
| 128 |
|
|
DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); |
| 129 |
|
|
|
| 130 |
|
|
if (ptr != Nullch) |
| 131 |
|
|
return ptr; |
| 132 |
|
|
else if (PL_nomemok) |
| 133 |
|
|
return Nullch; |
| 134 |
|
|
else { |
| 135 |
|
|
/* Can't use PerlIO to write as it allocates memory */ |
| 136 |
|
|
PerlLIO_write(PerlIO_fileno(Perl_error_log), |
| 137 |
|
|
PL_no_mem, strlen(PL_no_mem)); |
| 138 |
|
|
my_exit(1); |
| 139 |
|
|
return Nullch; |
| 140 |
|
|
} |
| 141 |
|
|
/*NOTREACHED*/ |
| 142 |
|
|
} |
| 143 |
|
|
|
| 144 |
|
|
/* safe version of system's free() */ |
| 145 |
|
|
|
| 146 |
|
|
Free_t |
| 147 |
|
|
Perl_safesysfree(Malloc_t where) |
| 148 |
|
|
{ |
| 149 |
|
|
#ifdef PERL_IMPLICIT_SYS |
| 150 |
|
|
dTHX; |
| 151 |
|
|
#endif |
| 152 |
|
|
DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++)); |
| 153 |
|
|
if (where) { |
| 154 |
|
|
/*SUPPRESS 701*/ |
| 155 |
|
|
PerlMem_free(where); |
| 156 |
|
|
} |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
|
|
/* safe version of system's calloc() */ |
| 160 |
|
|
|
| 161 |
|
|
Malloc_t |
| 162 |
|
|
Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size) |
| 163 |
|
|
{ |
| 164 |
|
|
dTHX; |
| 165 |
|
|
Malloc_t ptr; |
| 166 |
|
|
|
| 167 |
|
|
#ifdef HAS_64K_LIMIT |
| 168 |
|
|
if (size * count > 0xffff) { |
| 169 |
|
|
PerlIO_printf(Perl_error_log, |
| 170 |
|
|
"Allocation too large: %lx\n", size * count) FLUSH; |
| 171 |
|
|
my_exit(1); |
| 172 |
|
|
} |
| 173 |
|
|
#endif /* HAS_64K_LIMIT */ |
| 174 |
|
|
#ifdef DEBUGGING |
| 175 |
|
|
if ((long)size < 0 || (long)count < 0) |
| 176 |
|
|
Perl_croak_nocontext("panic: calloc"); |
| 177 |
|
|
#endif |
| 178 |
|
|
size *= count; |
| 179 |
|
|
ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
| 180 |
|
|
PERL_ALLOC_CHECK(ptr); |
| 181 |
|
|
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)); |
| 182 |
|
|
if (ptr != Nullch) { |
| 183 |
|
|
memset((void*)ptr, 0, size); |
| 184 |
|
|
return ptr; |
| 185 |
|
|
} |
| 186 |
|
|
else if (PL_nomemok) |
| 187 |
|
|
return Nullch; |
| 188 |
|
|
else { |
| 189 |
|
|
/* Can't use PerlIO to write as it allocates memory */ |
| 190 |
|
|
PerlLIO_write(PerlIO_fileno(Perl_error_log), |
| 191 |
|
|
PL_no_mem, strlen(PL_no_mem)); |
| 192 |
|
|
my_exit(1); |
| 193 |
|
|
return Nullch; |
| 194 |
|
|
} |
| 195 |
|
|
/*NOTREACHED*/ |
| 196 |
|
|
} |
| 197 |
|
|
|
| 198 |
|
|
/* These must be defined when not using Perl's malloc for binary |
| 199 |
|
|
* compatibility */ |
| 200 |
|
|
|
| 201 |
|
|
#ifndef MYMALLOC |
| 202 |
|
|
|
| 203 |
|
|
Malloc_t Perl_malloc (MEM_SIZE nbytes) |
| 204 |
|
|
{ |
| 205 |
|
|
dTHXs; |
| 206 |
|
|
return (Malloc_t)PerlMem_malloc(nbytes); |
| 207 |
|
|
} |
| 208 |
|
|
|
| 209 |
|
|
Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size) |
| 210 |
|
|
{ |
| 211 |
|
|
dTHXs; |
| 212 |
|
|
return (Malloc_t)PerlMem_calloc(elements, size); |
| 213 |
|
|
} |
| 214 |
|
|
|
| 215 |
|
|
Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes) |
| 216 |
|
|
{ |
| 217 |
|
|
dTHXs; |
| 218 |
|
|
return (Malloc_t)PerlMem_realloc(where, nbytes); |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
Free_t Perl_mfree (Malloc_t where) |
| 222 |
|
|
{ |
| 223 |
|
|
dTHXs; |
| 224 |
|
|
PerlMem_free(where); |
| 225 |
|
|
} |
| 226 |
|
|
|
| 227 |
|
|
#endif |
| 228 |
|
|
|
| 229 |
|
|
/* copy a string up to some (non-backslashed) delimiter, if any */ |
| 230 |
|
|
|
| 231 |
|
|
char * |
| 232 |
|
|
Perl_delimcpy(pTHX_ register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen) |
| 233 |
|
|
{ |
| 234 |
|
|
register I32 tolen; |
| 235 |
|
|
for (tolen = 0; from < fromend; from++, tolen++) { |
| 236 |
|
|
if (*from == '\\') { |
| 237 |
|
|
if (from[1] == delim) |
| 238 |
|
|
from++; |
| 239 |
|
|
else { |
| 240 |
|
|
if (to < toend) |
| 241 |
|
|
*to++ = *from; |
| 242 |
|
|
tolen++; |
| 243 |
|
|
from++; |
| 244 |
|
|
} |
| 245 |
|
|
} |
| 246 |
|
|
else if (*from == delim) |
| 247 |
|
|
break; |
| 248 |
|
|
if (to < toend) |
| 249 |
|
|
*to++ = *from; |
| 250 |
|
|
} |
| 251 |
|
|
if (to < toend) |
| 252 |
|
|
*to = '\0'; |
| 253 |
|
|
*retlen = tolen; |
| 254 |
|
|
return from; |
| 255 |
|
|
} |
| 256 |
|
|
|
| 257 |
|
|
/* return ptr to little string in big string, NULL if not found */ |
| 258 |
|
|
/* This routine was donated by Corey Satten. */ |
| 259 |
|
|
|
| 260 |
|
|
char * |
| 261 |
|
|
Perl_instr(pTHX_ register const char *big, register const char *little) |
| 262 |
|
|
{ |
| 263 |
|
|
register const char *s, *x; |
| 264 |
|
|
register I32 first; |
| 265 |
|
|
|
| 266 |
|
|
if (!little) |
| 267 |
|
|
return (char*)big; |
| 268 |
|
|
first = *little++; |
| 269 |
|
|
if (!first) |
| 270 |
|
|
return (char*)big; |
| 271 |
|
|
while (*big) { |
| 272 |
|
|
if (*big++ != first) |
| 273 |
|
|
continue; |
| 274 |
|
|
for (x=big,s=little; *s; /**/ ) { |
| 275 |
|
|
if (!*x) |
| 276 |
|
|
return Nullch; |
| 277 |
|
|
if (*s++ != *x++) { |
| 278 |
|
|
s--; |
| 279 |
|
|
break; |
| 280 |
|
|
} |
| 281 |
|
|
} |
| 282 |
|
|
if (!*s) |
| 283 |
|
|
return (char*)(big-1); |
| 284 |
|
|
} |
| 285 |
|
|
return Nullch; |
| 286 |
|
|
} |
| 287 |
|
|
|
| 288 |
|
|
/* same as instr but allow embedded nulls */ |
| 289 |
|
|
|
| 290 |
|
|
char * |
| 291 |
|
|
Perl_ninstr(pTHX_ register const char *big, register const char *bigend, const char *little, const char *lend) |
| 292 |
|
|
{ |
| 293 |
|
|
register const char *s, *x; |
| 294 |
|
|
register I32 first = *little; |
| 295 |
|
|
register const char *littleend = lend; |
| 296 |
|
|
|
| 297 |
|
|
if (!first && little >= littleend) |
| 298 |
|
|
return (char*)big; |
| 299 |
|
|
if (bigend - big < littleend - little) |
| 300 |
|
|
return Nullch; |
| 301 |
|
|
bigend -= littleend - little++; |
| 302 |
|
|
while (big <= bigend) { |
| 303 |
|
|
if (*big++ != first) |
| 304 |
|
|
continue; |
| 305 |
|
|
for (x=big,s=little; s < littleend; /**/ ) { |
| 306 |
|
|
if (*s++ != *x++) { |
| 307 |
|
|
s--; |
| 308 |
|
|
break; |
| 309 |
|
|
} |
| 310 |
|
|
} |
| 311 |
|
|
if (s >= littleend) |
| 312 |
|
|
return (char*)(big-1); |
| 313 |
|
|
} |
| 314 |
|
|
return Nullch; |
| 315 |
|
|
} |
| 316 |
|
|
|
| 317 |
|
|
/* reverse of the above--find last substring */ |
| 318 |
|
|
|
| 319 |
|
|
char * |
| 320 |
|
|
Perl_rninstr(pTHX_ register const char *big, const char *bigend, const char *little, const char *lend) |
| 321 |
|
|
{ |
| 322 |
|
|
register const char *bigbeg; |
| 323 |
|
|
register const char *s, *x; |
| 324 |
|
|
register I32 first = *little; |
| 325 |
|
|
register const char *littleend = lend; |
| 326 |
|
|
|
| 327 |
|
|
if (!first && little >= littleend) |
| 328 |
|
|
return (char*)bigend; |
| 329 |
|
|
bigbeg = big; |
| 330 |
|
|
big = bigend - (littleend - little++); |
| 331 |
|
|
while (big >= bigbeg) { |
| 332 |
|
|
if (*big-- != first) |
| 333 |
|
|
continue; |
| 334 |
|
|
for (x=big+2,s=little; s < littleend; /**/ ) { |
| 335 |
|
|
if (*s++ != *x++) { |
| 336 |
|
|
s--; |
| 337 |
|
|
break; |
| 338 |
|
|
} |
| 339 |
|
|
} |
| 340 |
|
|
if (s >= littleend) |
| 341 |
|
|
return (char*)(big+1); |
| 342 |
|
|
} |
| 343 |
|
|
return Nullch; |
| 344 |
|
|
} |
| 345 |
|
|
|
| 346 |
|
|
#define FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table*/ |
| 347 |
|
|
|
| 348 |
|
|
/* As a space optimization, we do not compile tables for strings of length |
| 349 |
|
|
0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are |
| 350 |
|
|
special-cased in fbm_instr(). |
| 351 |
|
|
|
| 352 |
|
|
If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ |
| 353 |
|
|
|
| 354 |
|
|
/* |
| 355 |
|
|
=head1 Miscellaneous Functions |
| 356 |
|
|
|
| 357 |
|
|
=for apidoc fbm_compile |
| 358 |
|
|
|
| 359 |
|
|
Analyses the string in order to make fast searches on it using fbm_instr() |
| 360 |
|
|
-- the Boyer-Moore algorithm. |
| 361 |
|
|
|
| 362 |
|
|
=cut |
| 363 |
|
|
*/ |
| 364 |
|
|
|
| 365 |
|
|
void |
| 366 |
|
|
Perl_fbm_compile(pTHX_ SV *sv, U32 flags) |
| 367 |
|
|
{ |
| 368 |
|
|
register U8 *s; |
| 369 |
|
|
register U8 *table; |
| 370 |
|
|
register U32 i; |
| 371 |
|
|
STRLEN len; |
| 372 |
|
|
I32 rarest = 0; |
| 373 |
|
|
U32 frequency = 256; |
| 374 |
|
|
|
| 375 |
|
|
if (flags & FBMcf_TAIL) { |
| 376 |
|
|
MAGIC *mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL; |
| 377 |
|
|
sv_catpvn(sv, "\n", 1); /* Taken into account in fbm_instr() */ |
| 378 |
|
|
if (mg && mg->mg_len >= 0) |
| 379 |
|
|
mg->mg_len++; |
| 380 |
|
|
} |
| 381 |
|
|
s = (U8*)SvPV_force(sv, len); |
| 382 |
|
|
(void)SvUPGRADE(sv, SVt_PVBM); |
| 383 |
|
|
if (len == 0) /* TAIL might be on a zero-length string. */ |
| 384 |
|
|
return; |
| 385 |
|
|
if (len > 2) { |
| 386 |
|
|
U8 mlen; |
| 387 |
|
|
unsigned char *sb; |
| 388 |
|
|
|
| 389 |
|
|
if (len > 255) |
| 390 |
|
|
mlen = 255; |
| 391 |
|
|
else |
| 392 |
|
|
mlen = (U8)len; |
| 393 |
|
|
Sv_Grow(sv, len + 256 + FBM_TABLE_OFFSET); |
| 394 |
|
|
table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET); |
| 395 |
|
|
s = table - 1 - FBM_TABLE_OFFSET; /* last char */ |
| 396 |
|
|
memset((void*)table, mlen, 256); |
| 397 |
|
|
table[-1] = (U8)flags; |
| 398 |
|
|
i = 0; |
| 399 |
|
|
sb = s - mlen + 1; /* first char (maybe) */ |
| 400 |
|
|
while (s >= sb) { |
| 401 |
|
|
if (table[*s] == mlen) |
| 402 |
|
|
table[*s] = (U8)i; |
| 403 |
|
|
s--, i++; |
| 404 |
|
|
} |
| 405 |
|
|
} |
| 406 |
|
|
sv_magic(sv, Nullsv, PERL_MAGIC_bm, Nullch, 0); /* deep magic */ |
| 407 |
|
|
SvVALID_on(sv); |
| 408 |
|
|
|
| 409 |
|
|
s = (unsigned char*)(SvPVX(sv)); /* deeper magic */ |
| 410 |
|
|
for (i = 0; i < len; i++) { |
| 411 |
|
|
if (PL_freq[s[i]] < frequency) { |
| 412 |
|
|
rarest = i; |
| 413 |
|
|
frequency = PL_freq[s[i]]; |
| 414 |
|
|
} |
| 415 |
|
|
} |
| 416 |
|
|
BmRARE(sv) = s[rarest]; |
| 417 |
|
|
BmPREVIOUS(sv) = (U16)rarest; |
| 418 |
|
|
BmUSEFUL(sv) = 100; /* Initial value */ |
| 419 |
|
|
if (flags & FBMcf_TAIL) |
| 420 |
|
|
SvTAIL_on(sv); |
| 421 |
|
|
DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n", |
| 422 |
|
|
BmRARE(sv),BmPREVIOUS(sv))); |
| 423 |
|
|
} |
| 424 |
|
|
|
| 425 |
|
|
/* If SvTAIL(littlestr), it has a fake '\n' at end. */ |
| 426 |
|
|
/* If SvTAIL is actually due to \Z or \z, this gives false positives |
| 427 |
|
|
if multiline */ |
| 428 |
|
|
|
| 429 |
|
|
/* |
| 430 |
|
|
=for apidoc fbm_instr |
| 431 |
|
|
|
| 432 |
|
|
Returns the location of the SV in the string delimited by C<str> and |
| 433 |
|
|
C<strend>. It returns C<Nullch> if the string can't be found. The C<sv> |
| 434 |
|
|
does not have to be fbm_compiled, but the search will not be as fast |
| 435 |
|
|
then. |
| 436 |
|
|
|
| 437 |
|
|
=cut |
| 438 |
|
|
*/ |
| 439 |
|
|
|
| 440 |
|
|
char * |
| 441 |
|
|
Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags) |
| 442 |
|
|
{ |
| 443 |
|
|
register unsigned char *s; |
| 444 |
|
|
STRLEN l; |
| 445 |
|
|
register unsigned char *little = (unsigned char *)SvPV(littlestr,l); |
| 446 |
|
|
register STRLEN littlelen = l; |
| 447 |
|
|
register I32 multiline = flags & FBMrf_MULTILINE; |
| 448 |
|
|
|
| 449 |
|
|
if ((STRLEN)(bigend - big) < littlelen) { |
| 450 |
|
|
if ( SvTAIL(littlestr) |
| 451 |
|
|
&& ((STRLEN)(bigend - big) == littlelen - 1) |
| 452 |
|
|
&& (littlelen == 1 |
| 453 |
|
|
|| (*big == *little && |
| 454 |
|
|
memEQ((char *)big, (char *)little, littlelen - 1)))) |
| 455 |
|
|
return (char*)big; |
| 456 |
|
|
return Nullch; |
| 457 |
|
|
} |
| 458 |
|
|
|
| 459 |
|
|
if (littlelen <= 2) { /* Special-cased */ |
| 460 |
|
|
|
| 461 |
|
|
if (littlelen == 1) { |
| 462 |
|
|
if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */ |
| 463 |
|
|
/* Know that bigend != big. */ |
| 464 |
|
|
if (bigend[-1] == '\n') |
| 465 |
|
|
return (char *)(bigend - 1); |
| 466 |
|
|
return (char *) bigend; |
| 467 |
|
|
} |
| 468 |
|
|
s = big; |
| 469 |
|
|
while (s < bigend) { |
| 470 |
|
|
if (*s == *little) |
| 471 |
|
|
return (char *)s; |
| 472 |
|
|
s++; |
| 473 |
|
|
} |
| 474 |
|
|
if (SvTAIL(littlestr)) |
| 475 |
|
|
return (char *) bigend; |
| 476 |
|
|
return Nullch; |
| 477 |
|
|
} |
| 478 |
|
|
if (!littlelen) |
| 479 |
|
|
return (char*)big; /* Cannot be SvTAIL! */ |
| 480 |
|
|
|
| 481 |
|
|
/* littlelen is 2 */ |
| 482 |
|
|
if (SvTAIL(littlestr) && !multiline) { |
| 483 |
|
|
if (bigend[-1] == '\n' && bigend[-2] == *little) |
| 484 |
|
|
return (char*)bigend - 2; |
| 485 |
|
|
if (bigend[-1] == *little) |
| 486 |
|
|
return (char*)bigend - 1; |
| 487 |
|
|
return Nullch; |
| 488 |
|
|
} |
| 489 |
|
|
{ |
| 490 |
|
|
/* This should be better than FBM if c1 == c2, and almost |
| 491 |
|
|
as good otherwise: maybe better since we do less indirection. |
| 492 |
|
|
And we save a lot of memory by caching no table. */ |
| 493 |
|
|
register unsigned char c1 = little[0]; |
| 494 |
|
|
register unsigned char c2 = little[1]; |
| 495 |
|
|
|
| 496 |
|
|
s = big + 1; |
| 497 |
|
|
bigend--; |
| 498 |
|
|
if (c1 != c2) { |
| 499 |
|
|
while (s <= bigend) { |
| 500 |
|
|
if (s[0] == c2) { |
| 501 |
|
|
if (s[-1] == c1) |
| 502 |
|
|
return (char*)s - 1; |
| 503 |
|
|
s += 2; |
| 504 |
|
|
continue; |
| 505 |
|
|
} |
| 506 |
|
|
next_chars: |
| 507 |
|
|
if (s[0] == c1) { |
| 508 |
|
|
if (s == bigend) |
| 509 |
|
|
goto check_1char_anchor; |
| 510 |
|
|
if (s[1] == c2) |
| 511 |
|
|
return (char*)s; |
| 512 |
|
|
else { |
| 513 |
|
|
s++; |
| 514 |
|
|
goto next_chars; |
| 515 |
|
|
} |
| 516 |
|
|
} |
| 517 |
|
|
else |
| 518 |
|
|
s += 2; |
| 519 |
|
|
} |
| 520 |
|
|
goto check_1char_anchor; |
| 521 |
|
|
} |
| 522 |
|
|
/* Now c1 == c2 */ |
| 523 |
|
|
while (s <= bigend) { |
| 524 |
|
|
if (s[0] == c1) { |
| 525 |
|
|
if (s[-1] == c1) |
| 526 |
|
|
return (char*)s - 1; |
| 527 |
|
|
if (s == bigend) |
| 528 |
|
|
goto check_1char_anchor; |
| 529 |
|
|
if (s[1] == c1) |
| 530 |
|
|
return (char*)s; |
| 531 |
|
|
s += 3; |
| 532 |
|
|
} |
| 533 |
|
|
else |
| 534 |
|
|
s += 2; |
| 535 |
|
|
} |
| 536 |
|
|
} |
| 537 |
|
|
check_1char_anchor: /* One char and anchor! */ |
| 538 |
|
|
if (SvTAIL(littlestr) && (*bigend == *little)) |
| 539 |
|
|
return (char *)bigend; /* bigend is already decremented. */ |
| 540 |
|
|
return Nullch; |
| 541 |
|
|
} |
| 542 |
|
|
if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
| 543 |
|
|
s = bigend - littlelen; |
| 544 |
|
|
if (s >= big && bigend[-1] == '\n' && *s == *little |
| 545 |
|
|
/* Automatically of length > 2 */ |
| 546 |
|
|
&& memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) |
| 547 |
|
|
{ |
| 548 |
|
|
return (char*)s; /* how sweet it is */ |
| 549 |
|
|
} |
| 550 |
|
|
if (s[1] == *little |
| 551 |
|
|
&& memEQ((char*)s + 2, (char*)little + 1, littlelen - 2)) |
| 552 |
|
|
{ |
| 553 |
|
|
return (char*)s + 1; /* how sweet it is */ |
| 554 |
|
|
} |
| 555 |
|
|
return Nullch; |
| 556 |
|
|
} |
| 557 |
|
|
if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) { |
| 558 |
|
|
char *b = ninstr((char*)big,(char*)bigend, |
| 559 |
|
|
(char*)little, (char*)little + littlelen); |
| 560 |
|
|
|
| 561 |
|
|
if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ |
| 562 |
|
|
/* Chop \n from littlestr: */ |
| 563 |
|
|
s = bigend - littlelen + 1; |
| 564 |
|
|
if (*s == *little |
| 565 |
|
|
&& memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) |
| 566 |
|
|
{ |
| 567 |
|
|
return (char*)s; |
| 568 |
|
|
} |
| 569 |
|
|
return Nullch; |
| 570 |
|
|
} |
| 571 |
|
|
return b; |
| 572 |
|
|
} |
| 573 |
|
|
|
| 574 |
|
|
{ /* Do actual FBM. */ |
| 575 |
|
|
register unsigned char *table = little + littlelen + FBM_TABLE_OFFSET; |
| 576 |
|
|
register unsigned char *oldlittle; |
| 577 |
|
|
|
| 578 |
|
|
if (littlelen > (STRLEN)(bigend - big)) |
| 579 |
|
|
return Nullch; |
| 580 |
|
|
--littlelen; /* Last char found by table lookup */ |
| 581 |
|
|
|
| 582 |
|
|
s = big + littlelen; |
| 583 |
|
|
little += littlelen; /* last char */ |
| 584 |
|
|
oldlittle = little; |
| 585 |
|
|
if (s < bigend) { |
| 586 |
|
|
register I32 tmp; |
| 587 |
|
|
|
| 588 |
|
|
top2: |
| 589 |
|
|
/*SUPPRESS 560*/ |
| 590 |
|
|
if ((tmp = table[*s])) { |
| 591 |
|
|
if ((s += tmp) < bigend) |
| 592 |
|
|
goto top2; |
| 593 |
|
|
goto check_end; |
| 594 |
|
|
} |
| 595 |
|
|
else { /* less expensive than calling strncmp() */ |
| 596 |
|
|
register unsigned char *olds = s; |
| 597 |
|
|
|
| 598 |
|
|
tmp = littlelen; |
| 599 |
|
|
|
| 600 |
|
|
while (tmp--) { |
| 601 |
|
|
if (*--s == *--little) |
| 602 |
|
|
continue; |
| 603 |
|
|
s = olds + 1; /* here we pay the price for failure */ |
| 604 |
|
|
little = oldlittle; |
| 605 |
|
|
if (s < bigend) /* fake up continue to outer loop */ |
| 606 |
|
|
goto top2; |
| 607 |
|
|
goto check_end; |
| 608 |
|
|
} |
| 609 |
|
|
return (char *)s; |
| 610 |
|
|
} |
| 611 |
|
|
} |
| 612 |
|
|
check_end: |
| 613 |
|
|
if ( s == bigend && (table[-1] & FBMcf_TAIL) |
| 614 |
|
|
&& memEQ((char *)(bigend - littlelen), |
| 615 |
|
|
(char *)(oldlittle - littlelen), littlelen) ) |
| 616 |
|
|
return (char*)bigend - littlelen; |
| 617 |
|
|
return Nullch; |
| 618 |
|
|
} |
| 619 |
|
|
} |
| 620 |
|
|
|
| 621 |
|
|
/* start_shift, end_shift are positive quantities which give offsets |
| 622 |
|
|
of ends of some substring of bigstr. |
| 623 |
|
|
If `last' we want the last occurrence. |
| 624 |
|
|
old_posp is the way of communication between consequent calls if |
| 625 |
|
|
the next call needs to find the . |
| 626 |
|
|
The initial *old_posp should be -1. |
| 627 |
|
|
|
| 628 |
|
|
Note that we take into account SvTAIL, so one can get extra |
| 629 |
|
|
optimizations if _ALL flag is set. |
| 630 |
|
|
*/ |
| 631 |
|
|
|
| 632 |
|
|
/* If SvTAIL is actually due to \Z or \z, this gives false positives |
| 633 |
|
|
if PL_multiline. In fact if !PL_multiline the authoritative answer |
| 634 |
|
|
is not supported yet. */ |
| 635 |
|
|
|
| 636 |
|
|
char * |
| 637 |
|
|
Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last) |
| 638 |
|
|
{ |
| 639 |
|
|
register unsigned char *s, *x; |
| 640 |
|
|
register unsigned char *big; |
| 641 |
|
|
register I32 pos; |
| 642 |
|
|
register I32 previous; |
| 643 |
|
|
register I32 first; |
| 644 |
|
|
register unsigned char *little; |
| 645 |
|
|
register I32 stop_pos; |
| 646 |
|
|
register unsigned char *littleend; |
| 647 |
|
|
I32 found = 0; |
| 648 |
|
|
|
| 649 |
|
|
if (*old_posp == -1 |
| 650 |
|
|
? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0 |
| 651 |
|
|
: (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) { |
| 652 |
|
|
cant_find: |
| 653 |
|
|
if ( BmRARE(littlestr) == '\n' |
| 654 |
|
|
&& BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) { |
| 655 |
|
|
little = (unsigned char *)(SvPVX(littlestr)); |
| 656 |
|
|
littleend = little + SvCUR(littlestr); |
| 657 |
|
|
first = *little++; |
| 658 |
|
|
goto check_tail; |
| 659 |
|
|
} |
| 660 |
|
|
return Nullch; |
| 661 |
|
|
} |
| 662 |
|
|
|
| 663 |
|
|
little = (unsigned char *)(SvPVX(littlestr)); |
| 664 |
|
|
littleend = little + SvCUR(littlestr); |
| 665 |
|
|
first = *little++; |
| 666 |
|
|
/* The value of pos we can start at: */ |
| 667 |
|
|
previous = BmPREVIOUS(littlestr); |
| 668 |
|
|
big = (unsigned char *)(SvPVX(bigstr)); |
| 669 |
|
|
/* The value of pos we can stop at: */ |
| 670 |
|
|
stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous); |
| 671 |
|
|
if (previous + start_shift > stop_pos) { |
| 672 |
|
|
/* |
| 673 |
|
|
stop_pos does not include SvTAIL in the count, so this check is incorrect |
| 674 |
|
|
(I think) - see [ID 20010618.006] and t/op/study.t. HVDS 2001/06/19 |
| 675 |
|
|
*/ |
| 676 |
|
|
#if 0 |
| 677 |
|
|
if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */ |
| 678 |
|
|
goto check_tail; |
| 679 |
|
|
#endif |
| 680 |
|
|
return Nullch; |
| 681 |
|
|
} |
| 682 |
|
|
while (pos < previous + start_shift) { |
| 683 |
|
|
if (!(pos += PL_screamnext[pos])) |
| 684 |
|
|
goto cant_find; |
| 685 |
|
|
} |
| 686 |
|
|
big -= previous; |
| 687 |
|
|
do { |
| 688 |
|
|
if (pos >= stop_pos) break; |
| 689 |
|
|
if (big[pos] != first) |
| 690 |
|
|
continue; |
| 691 |
|
|
for (x=big+pos+1,s=little; s < littleend; /**/ ) { |
| 692 |
|
|
if (*s++ != *x++) { |
| 693 |
|
|
s--; |
| 694 |
|
|
break; |
| 695 |
|
|
} |
| 696 |
|
|
} |
| 697 |
|
|
if (s == littleend) { |
| 698 |
|
|
*old_posp = pos; |
| 699 |
|
|
if (!last) return (char *)(big+pos); |
| 700 |
|
|
found = 1; |
| 701 |
|
|
} |
| 702 |
|
|
} while ( pos += PL_screamnext[pos] ); |
| 703 |
|
|
if (last && found) |
| 704 |
|
|
return (char *)(big+(*old_posp)); |
| 705 |
|
|
check_tail: |
| 706 |
|
|
if (!SvTAIL(littlestr) || (end_shift > 0)) |
| 707 |
|
|
return Nullch; |
| 708 |
|
|
/* Ignore the trailing "\n". This code is not microoptimized */ |
| 709 |
|
|
big = (unsigned char *)(SvPVX(bigstr) + SvCUR(bigstr)); |
| 710 |
|
|
stop_pos = littleend - little; /* Actual littlestr len */ |
| 711 |
|
|
if (stop_pos == 0) |
| 712 |
|
|
return (char*)big; |
| 713 |
|
|
big -= stop_pos; |
| 714 |
|
|
if (*big == first |
| 715 |
|
|
&& ((stop_pos == 1) || |
| 716 |
|
|
memEQ((char *)(big + 1), (char *)little, stop_pos - 1))) |
| 717 |
|
|
return (char*)big; |
| 718 |
|
|
return Nullch; |
| 719 |
|
|
} |
| 720 |
|
|
|
| 721 |
|
|
I32 |
| 722 |
|
|
Perl_ibcmp(pTHX_ const char *s1, const char *s2, register I32 len) |
| 723 |
|
|
{ |
| 724 |
|
|
register U8 *a = (U8 *)s1; |
| 725 |
|
|
register U8 *b = (U8 *)s2; |
| 726 |
|
|
while (len--) { |
| 727 |
|
|
if (*a != *b && *a != PL_fold[*b]) |
| 728 |
|
|
return 1; |
| 729 |
|
|
a++,b++; |
| 730 |
|
|
} |
| 731 |
|
|
return 0; |
| 732 |
|
|
} |
| 733 |
|
|
|
| 734 |
|
|
I32 |
| 735 |
|
|
Perl_ibcmp_locale(pTHX_ const char *s1, const char *s2, register I32 len) |
| 736 |
|
|
{ |
| 737 |
|
|
register U8 *a = (U8 *)s1; |
| 738 |
|
|
register U8 *b = (U8 *)s2; |
| 739 |
|
|
while (len--) { |
| 740 |
|
|
if (*a != *b && *a != PL_fold_locale[*b]) |
| 741 |
|
|
return 1; |
| 742 |
|
|
a++,b++; |
| 743 |
|
|
} |
| 744 |
|
|
return 0; |
| 745 |
|
|
} |
| 746 |
|
|
|
| 747 |
|
|
/* copy a string to a safe spot */ |
| 748 |
|
|
|
| 749 |
|
|
/* |
| 750 |
|
|
=head1 Memory Management |
| 751 |
|
|
|
| 752 |
|
|
=for apidoc savepv |
| 753 |
|
|
|
| 754 |
|
|
Perl's version of C<strdup()>. Returns a pointer to a newly allocated |
| 755 |
|
|
string which is a duplicate of C<pv>. The size of the string is |
| 756 |
|
|
determined by C<strlen()>. The memory allocated for the new string can |
| 757 |
|
|
be freed with the C<Safefree()> function. |
| 758 |
|
|
|
| 759 |
|
|
=cut |
| 760 |
|
|
*/ |
| 761 |
|
|
|
| 762 |
|
|
char * |
| 763 |
|
|
Perl_savepv(pTHX_ const char *pv) |
| 764 |
|
|
{ |
| 765 |
|
|
register char *newaddr; |
| 766 |
|
|
#ifdef PERL_MALLOC_WRAP |
| 767 |
|
|
STRLEN pvlen; |
| 768 |
|
|
#endif |
| 769 |
|
|
if (!pv) |
| 770 |
|
|
return Nullch; |
| 771 |
|
|
|
| 772 |
|
|
#ifdef PERL_MALLOC_WRAP |
| 773 |
|
|
pvlen = strlen(pv)+1; |
| 774 |
|
|
New(902,newaddr,pvlen,char); |
| 775 |
|
|
#else |
| 776 |
|
|
New(902,newaddr,strlen(pv)+1,char); |
| 777 |
|
|
#endif |
| 778 |
|
|
return strcpy(newaddr,pv); |
| 779 |
|
|
} |
| 780 |
|
|
|
| 781 |
|
|
/* same thing but with a known length */ |
| 782 |
|
|
|
| 783 |
|
|
/* |
| 784 |
|
|
=for apidoc savepvn |
| 785 |
|
|
|
| 786 |
|
|
Perl's version of what C<strndup()> would be if it existed. Returns a |
| 787 |
|
|
pointer to a newly allocated string which is a duplicate of the first |
| 788 |
|
|
C<len> bytes from C<pv>. The memory allocated for the new string can be |
| 789 |
|
|
freed with the C<Safefree()> function. |
| 790 |
|
|
|
| 791 |
|
|
=cut |
| 792 |
|
|
*/ |
| 793 |
|
|
|
| 794 |
|
|
char * |
| 795 |
|
|
Perl_savepvn(pTHX_ const char *pv, register I32 len) |
| 796 |
|
|
{ |
| 797 |
|
|
register char *newaddr; |
| 798 |
|
|
|
| 799 |
|
|
New(903,newaddr,len+1,char); |
| 800 |
|
|
/* Give a meaning to NULL pointer mainly for the use in sv_magic() */ |
| 801 |
|
|
if (pv) { |
| 802 |
|
|
/* might not be null terminated */ |
| 803 |
|
|
newaddr[len] = '\0'; |
| 804 |
|
|
return CopyD(pv,newaddr,len,char); |
| 805 |
|
|
} |
| 806 |
|
|
else { |
| 807 |
|
|
return ZeroD(newaddr,len+1,char); |
| 808 |
|
|
} |
| 809 |
|
|
} |
| 810 |
|
|
|
| 811 |
|
|
/* |
| 812 |
|
|
=for apidoc savesharedpv |
| 813 |
|
|
|
| 814 |
|
|
A version of C<savepv()> which allocates the duplicate string in memory |
| 815 |
|
|
which is shared between threads. |
| 816 |
|
|
|
| 817 |
|
|
=cut |
| 818 |
|
|
*/ |
| 819 |
|
|
char * |
| 820 |
|
|
Perl_savesharedpv(pTHX_ const char *pv) |
| 821 |
|
|
{ |
| 822 |
|
|
register char *newaddr; |
| 823 |
|
|
if (!pv) |
| 824 |
|
|
return Nullch; |
| 825 |
|
|
|
| 826 |
|
|
newaddr = (char*)PerlMemShared_malloc(strlen(pv)+1); |
| 827 |
|
|
if (!newaddr) { |
| 828 |
|
|
PerlLIO_write(PerlIO_fileno(Perl_error_log), |
| 829 |
|
|
PL_no_mem, strlen(PL_no_mem)); |
| 830 |
|
|
my_exit(1); |
| 831 |
|
|
} |
| 832 |
|
|
return strcpy(newaddr,pv); |
| 833 |
|
|
} |
| 834 |
|
|
|
| 835 |
|
|
/* |
| 836 |
|
|
=for apidoc savesvpv |
| 837 |
|
|
|
| 838 |
|
|
A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from |
| 839 |
|
|
the passed in SV using C<SvPV()> |
| 840 |
|
|
|
| 841 |
|
|
=cut |
| 842 |
|
|
*/ |
| 843 |
|
|
|
| 844 |
|
|
char * |
| 845 |
|
|
Perl_savesvpv(pTHX_ SV *sv) |
| 846 |
|
|
{ |
| 847 |
|
|
STRLEN len; |
| 848 |
|
|
const char *pv = SvPV(sv, len); |
| 849 |
|
|
register char *newaddr; |
| 850 |
|
|
|
| 851 |
|
|
++len; |
| 852 |
|
|
New(903,newaddr,len,char); |
| 853 |
|
|
return CopyD(pv,newaddr,len,char); |
| 854 |
|
|
} |
| 855 |
|
|
|
| 856 |
|
|
|
| 857 |
|
|
/* the SV for Perl_form() and mess() is not kept in an arena */ |
| 858 |
|
|
|
| 859 |
|
|
STATIC SV * |
| 860 |
|
|
S_mess_alloc(pTHX) |
| 861 |
|
|
{ |
| 862 |
|
|
SV *sv; |
| 863 |
|
|
XPVMG *any; |
| 864 |
|
|
|
| 865 |
|
|
if (!PL_dirty) |
| 866 |
|
|
return sv_2mortal(newSVpvn("",0)); |
| 867 |
|
|
|
| 868 |
|
|
if (PL_mess_sv) |
| 869 |
|
|
return PL_mess_sv; |
| 870 |
|
|
|
| 871 |
|
|
/* Create as PVMG now, to avoid any upgrading later */ |
| 872 |
|
|
New(905, sv, 1, SV); |
| 873 |
|
|
Newz(905, any, 1, XPVMG); |
| 874 |
|
|
SvFLAGS(sv) = SVt_PVMG; |
| 875 |
|
|
SvANY(sv) = (void*)any; |
| 876 |
|
|
SvREFCNT(sv) = 1 << 30; /* practically infinite */ |
| 877 |
|
|
PL_mess_sv = sv; |
| 878 |
|
|
return sv; |
| 879 |
|
|
} |
| 880 |
|
|
|
| 881 |
|
|
#if defined(PERL_IMPLICIT_CONTEXT) |
| 882 |
|
|
char * |
| 883 |
|
|
Perl_form_nocontext(const char* pat, ...) |
| 884 |
|
|
{ |
| 885 |
|
|
dTHX; |
| 886 |
|
|
char *retval; |
| 887 |
|
|
va_list args; |
| 888 |
|
|
va_start(args, pat); |
| 889 |
|
|
retval = vform(pat, &args); |
| 890 |
|
|
va_end(args); |
| 891 |
|
|
return retval; |
| 892 |
|
|
} |
| 893 |
|
|
#endif /* PERL_IMPLICIT_CONTEXT */ |
| 894 |
|
|
|
| 895 |
|
|
/* |
| 896 |
|
|
=head1 Miscellaneous Functions |
| 897 |
|
|
=for apidoc form |
| 898 |
|
|
|
| 899 |
|
|
Takes a sprintf-style format pattern and conventional |
| 900 |
|
|
(non-SV) arguments and returns the formatted string. |
| 901 |
|
|
|
| 902 |
|
|
(char *) Perl_form(pTHX_ const char* pat, ...) |
| 903 |
|
|
|
| 904 |
|
|
can be used any place a string (char *) is required: |
| 905 |
|
|
|
| 906 |
|
|
char * s = Perl_form("%d.%d",major,minor); |
| 907 |
|
|
|
| 908 |
|
|
Uses a single private buffer so if you want to format several strings you |
| 909 |
|
|
must explicitly copy the earlier strings away (and free the copies when you |
| 910 |
|
|
are done). |
| 911 |
|
|
|
| 912 |
|
|
=cut |
| 913 |
|
|
*/ |
| 914 |
|
|
|
| 915 |
|
|
char * |
| 916 |
|
|
Perl_form(pTHX_ const char* pat, ...) |
| 917 |
|
|
{ |
| 918 |
|
|
char *retval; |
| 919 |
|
|
va_list args; |
| 920 |
|
|
va_start(args, pat); |
| 921 |
|
|
retval = vform(pat, &args); |
| 922 |
|
|
va_end(args); |
| 923 |
|
|
return retval; |
| 924 |
|
|
} |
| 925 |
|
|
|
| 926 |
|
|
char * |
| 927 |
|
|
Perl_vform(pTHX_ const char *pat, va_list *args) |
| 928 |
|
|
{ |
| 929 |
|
|
SV *sv = mess_alloc(); |
| 930 |
|
|
sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); |
| 931 |
|
|
return SvPVX(sv); |
| 932 |
|
|
} |
| 933 |
|
|
|
| 934 |
|
|
#if defined(PERL_IMPLICIT_CONTEXT) |
| 935 |
|
|
SV * |
| 936 |
|
|
Perl_mess_nocontext(const char *pat, ...) |
| 937 |
|
|
{ |
| 938 |
|
|
dTHX; |
| 939 |
|
|
SV *retval; |
| 940 |
|
|
va_list args; |
| 941 |
|
|
va_start(args, pat); |
| 942 |
|
|
retval = vmess(pat, &args); |
| 943 |
|
|
va_end(args); |
| 944 |
|
|
return retval; |
| 945 |
|
|
} |
| 946 |
|
|
#endif /* PERL_IMPLICIT_CONTEXT */ |
| 947 |
|
|
|
| 948 |
|
|
SV * |
| 949 |
|
|
Perl_mess(pTHX_ const char *pat, ...) |
| 950 |
|
|
{ |
| 951 |
|
|
SV *retval; |
| 952 |
|
|
va_list args; |
| 953 |
|
|
va_start(args, pat); |
| 954 |
|
|
retval = vmess(pat, &args); |
| 955 |
|
|
va_end(args); |
| 956 |
|
|
return retval; |
| 957 |
|
|
} |
| 958 |
|
|
|
| 959 |
|
|
STATIC COP* |
| 960 |
|
|
S_closest_cop(pTHX_ COP *cop, OP *o) |
| 961 |
|
|
{ |
| 962 |
|
|
/* Look for PL_op starting from o. cop is the last COP we've seen. */ |
| 963 |
|
|
|
| 964 |
|
|
if (!o || o == PL_op) return cop; |
| 965 |
|
|
|
| 966 |
|
|
if (o->op_flags & OPf_KIDS) { |
| 967 |
|
|
OP *kid; |
| 968 |
|
|
for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) |
| 969 |
|
|
{ |
| 970 |
|
|
COP *new_cop; |
| 971 |
|
|
|
| 972 |
|
|
/* If the OP_NEXTSTATE has been optimised away we can still use it |
| 973 |
|
|
* the get the file and line number. */ |
| 974 |
|
|
|
| 975 |
|
|
if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) |
| 976 |
|
|
cop = (COP *)kid; |
| 977 |
|
|
|
| 978 |
|
|
/* Keep searching, and return when we've found something. */ |
| 979 |
|
|
|
| 980 |
|
|
new_cop = closest_cop(cop, kid); |
| 981 |
|
|
if (new_cop) return new_cop; |
| 982 |
|
|
} |
| 983 |
|
|
} |
| 984 |
|
|
|
| 985 |
|
|
/* Nothing found. */ |
| 986 |
|
|
|
| 987 |
|
|
return 0; |
| 988 |
|
|
} |
| 989 |
|
|
|
| 990 |
|
|
SV * |
| 991 |
|
|
Perl_vmess(pTHX_ const char *pat, va_list *args) |
| 992 |
|
|
{ |
| 993 |
|
|
SV *sv = mess_alloc(); |
| 994 |
|
|
static char dgd[] = " during global destruction.\n"; |
| 995 |
|
|
COP *cop; |
| 996 |
|
|
|
| 997 |
|
|
sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); |
| 998 |
|
|
if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
| 999 |
|
|
|
| 1000 |
|
|
/* |
| 1001 |
|
|
* Try and find the file and line for PL_op. This will usually be |
| 1002 |
|
|
* PL_curcop, but it might be a cop that has been optimised away. We |
| 1003 |
|
|
* can try to find such a cop by searching through the optree starting |
| 1004 |
|
|
* from the sibling of PL_curcop. |
| 1005 |
|
|
*/ |
| 1006 |
|
|
|
| 1007 |
|
|
cop = closest_cop(PL_curcop, PL_curcop->op_sibling); |
| 1008 |
|
|
if (!cop) cop = PL_curcop; |
| 1009 |
|
|
|
| 1010 |
|
|
if (CopLINE(cop)) |
| 1011 |
|
|
Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf, |
| 1012 |
|
|
OutCopFILE(cop), (IV)CopLINE(cop)); |
| 1013 |
|
|
if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) { |
| 1014 |
|
|
bool line_mode = (RsSIMPLE(PL_rs) && |
| 1015 |
|
|
SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n'); |
| 1016 |
|
|
Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf, |
| 1017 |
|
|
PL_last_in_gv == PL_argvgv ? |
| 1018 |
|
|
"" : GvNAME(PL_last_in_gv), |
| 1019 |
|
|
line_mode ? "line" : "chunk", |
| 1020 |
|
|
(IV)IoLINES(GvIOp(PL_last_in_gv))); |
| 1021 |
|
|
} |
| 1022 |
|
|
#ifdef USE_5005THREADS |
| 1023 |
|
|
if (thr->tid) |
| 1024 |
|
|
Perl_sv_catpvf(aTHX_ sv, " thread %ld", thr->tid); |
| 1025 |
|
|
#endif |
| 1026 |
|
|
sv_catpv(sv, PL_dirty ? dgd : ".\n"); |
| 1027 |
|
|
} |
| 1028 |
|
|
return sv; |
| 1029 |
|
|
} |
| 1030 |
|
|
|
| 1031 |
|
|
void |
| 1032 |
|
|
Perl_write_to_stderr(pTHX_ const char* message, int msglen) |
| 1033 |
|
|
{ |
| 1034 |
|
|
IO *io; |
| 1035 |
|
|
MAGIC *mg; |
| 1036 |
|
|
|
| 1037 |
|
|
if (PL_stderrgv && SvREFCNT(PL_stderrgv) |
| 1038 |
|
|
&& (io = GvIO(PL_stderrgv)) |
| 1039 |
|
|
&& (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) |
| 1040 |
|
|
{ |
| 1041 |
|
|
dSP; |
| 1042 |
|
|
ENTER; |
| 1043 |
|
|
SAVETMPS; |
| 1044 |
|
|
|
| 1045 |
|
|
save_re_context(); |
| 1046 |
|
|
SAVESPTR(PL_stderrgv); |
| 1047 |
|
|
PL_stderrgv = Nullgv; |
| 1048 |
|
|
|
| 1049 |
|
|
PUSHSTACKi(PERLSI_MAGIC); |
| 1050 |
|
|
|
| 1051 |
|
|
PUSHMARK(SP); |
| 1052 |
|
|
EXTEND(SP,2); |
| 1053 |
|
|
PUSHs(SvTIED_obj((SV*)io, mg)); |
| 1054 |
|
|
PUSHs(sv_2mortal(newSVpvn(message, msglen))); |
| 1055 |
|
|
PUTBACK; |
| 1056 |
|
|
call_method("PRINT", G_SCALAR); |
| 1057 |
|
|
|
| 1058 |
|
|
POPSTACK; |
| 1059 |
|
|
FREETMPS; |
| 1060 |
|
|
LEAVE; |
| 1061 |
|
|
} |
| 1062 |
|
|
else { |
| 1063 |
|
|
#ifdef USE_SFIO |
| 1064 |
|
|
/* SFIO can really mess with your errno */ |
| 1065 |
|
|
int e = errno; |
| 1066 |
|
|
#endif |
| 1067 |
|
|
PerlIO *serr = Perl_error_log; |
| 1068 |
|
|
|
| 1069 |
|
|
PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen); |
| 1070 |
|
|
(void)PerlIO_flush(serr); |
| 1071 |
|
|
#ifdef USE_SFIO |
| 1072 |
|
|
errno = e; |
| 1073 |
|
|
#endif |
| 1074 |
|
|
} |
| 1075 |
|
|
} |
| 1076 |
|
|
|
| 1077 |
|
|
/* Common code used by vcroak, vdie and vwarner */ |
| 1078 |
|
|
|
| 1079 |
|
|
void S_vdie_common(pTHX_ const char *message, STRLEN msglen, I32 utf8); |
| 1080 |
|
|
|
| 1081 |
|
|
char * |
| 1082 |
|
|
S_vdie_croak_common(pTHX_ const char* pat, va_list* args, STRLEN* msglen, |
| 1083 |
|
|
I32* utf8) |
| 1084 |
|
|
{ |
| 1085 |
|
|
char *message; |
| 1086 |
|
|
|
| 1087 |
|
|
if (pat) { |
| 1088 |
|
|
SV *msv = vmess(pat, args); |
| 1089 |
|
|
if (PL_errors && SvCUR(PL_errors)) { |
| 1090 |
|
|
sv_catsv(PL_errors, msv); |
| 1091 |
|
|
message = SvPV(PL_errors, *msglen); |
| 1092 |
|
|
SvCUR_set(PL_errors, 0); |
| 1093 |
|
|
} |
| 1094 |
|
|
else |
| 1095 |
|
|
message = SvPV(msv,*msglen); |
| 1096 |
|
|
*utf8 = SvUTF8(msv); |
| 1097 |
|
|
} |
| 1098 |
|
|
else { |
| 1099 |
|
|
message = Nullch; |
| 1100 |
|
|
} |
| 1101 |
|
|
|
| 1102 |
|
|
DEBUG_S(PerlIO_printf(Perl_debug_log, |
| 1103 |
|
|
"%p: die/croak: message = %s\ndiehook = %p\n", |
| 1104 |
|
|
thr, message, PL_diehook)); |
| 1105 |
|
|
if (PL_diehook) { |
| 1106 |
|
|
S_vdie_common(aTHX_ message, *msglen, *utf8); |
| 1107 |
|
|
} |
| 1108 |
|
|
return message; |
| 1109 |
|
|
} |
| 1110 |
|
|
|
| 1111 |
|
|
void |
| 1112 |
|
|
S_vdie_common(pTHX_ const char *message, STRLEN msglen, I32 utf8) |
| 1113 |
|
|
{ |
| 1114 |
|
|
HV *stash; |
| 1115 |
|
|
GV *gv; |
| 1116 |
|
|
CV *cv; |
| 1117 |
|
|
/* sv_2cv might call Perl_croak() */ |
| 1118 |
|
|
SV *olddiehook = PL_diehook; |
| 1119 |
|
|
|
| 1120 |
|
|
assert(PL_diehook); |
| 1121 |
|
|
ENTER; |
| 1122 |
|
|
SAVESPTR(PL_diehook); |
| 1123 |
|
|
PL_diehook = Nullsv; |
| 1124 |
|
|
cv = sv_2cv(olddiehook, &stash, &gv, 0); |
| 1125 |
|
|
LEAVE; |
| 1126 |
|
|
if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { |
| 1127 |
|
|
dSP; |
| 1128 |
|
|
SV *msg; |
| 1129 |
|
|
|
| 1130 |
|
|
ENTER; |
| 1131 |
|
|
save_re_context(); |
| 1132 |
|
|
if (message) { |
| 1133 |
|
|
msg = newSVpvn(message, msglen); |
| 1134 |
|
|
SvFLAGS(msg) |= utf8; |
| 1135 |
|
|
SvREADONLY_on(msg); |
| 1136 |
|
|
SAVEFREESV(msg); |
| 1137 |
|
|
} |
| 1138 |
|
|
else { |
| 1139 |
|
|
msg = ERRSV; |
| 1140 |
|
|
} |
| 1141 |
|
|
|
| 1142 |
|
|
PUSHSTACKi(PERLSI_DIEHOOK); |
| 1143 |
|
|
PUSHMARK(SP); |
| 1144 |
|
|
XPUSHs(msg); |
| 1145 |
|
|
PUTBACK; |
| 1146 |
|
|
call_sv((SV*)cv, G_DISCARD); |
| 1147 |
|
|
POPSTACK; |
| 1148 |
|
|
LEAVE; |
| 1149 |
|
|
} |
| 1150 |
|
|
} |
| 1151 |
|
|
|
| 1152 |
|
|
OP * |
| 1153 |
|
|
Perl_vdie(pTHX_ const char* pat, va_list *args) |
| 1154 |
|
|
{ |
| 1155 |
|
|
char *message; |
| 1156 |
|
|
int was_in_eval = PL_in_eval; |
| 1157 |
|
|
STRLEN msglen; |
| 1158 |
|
|
I32 utf8 = 0; |
| 1159 |
|
|
|
| 1160 |
|
|
DEBUG_S(PerlIO_printf(Perl_debug_log, |
| 1161 |
|
|
"%p: die: curstack = %p, mainstack = %p\n", |
| 1162 |
|
|
thr, PL_curstack, PL_mainstack)); |
| 1163 |
|
|
|
| 1164 |
|
|
message = S_vdie_croak_common(aTHX_ pat, args, &msglen, &utf8); |
| 1165 |
|
|
|
| 1166 |
|
|
PL_restartop = die_where(message, msglen); |
| 1167 |
|
|
SvFLAGS(ERRSV) |= utf8; |
| 1168 |
|
|
DEBUG_S(PerlIO_printf(Perl_debug_log, |
| 1169 |
|
|
"%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n", |
| 1170 |
|
|
thr, PL_restartop, was_in_eval, PL_top_env)); |
| 1171 |
|
|
if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev) |
| 1172 |
|
|
JMPENV_JUMP(3); |
| 1173 |
|
|
return PL_restartop; |
| 1174 |
|
|
} |
| 1175 |
|
|
|
| 1176 |
|
|
#if defined(PERL_IMPLICIT_CONTEXT) |
| 1177 |
|
|
OP * |
| 1178 |
|
|
Perl_die_nocontext(const char* pat, ...) |
| 1179 |
|
|
{ |
| 1180 |
|
|
dTHX; |
| 1181 |
|
|
OP *o; |
| 1182 |
|
|
va_list args; |
| 1183 |
|
|
va_start(args, pat); |
| 1184 |
|
|
o = vdie(pat, &args); |
| 1185 |
|
|
va_end(args); |
| 1186 |
|
|
return o; |
| 1187 |
|
|
} |
| 1188 |
|
|
#endif /* PERL_IMPLICIT_CONTEXT */ |
| 1189 |
|
|
|
| 1190 |
|
|
OP * |
| 1191 |
|
|
Perl_die(pTHX_ const char* pat, ...) |
| 1192 |
|
|
{ |
| 1193 |
|
|
OP *o; |
| 1194 |
|
|
va_list args; |
| 1195 |
|
|
va_start(args, pat); |
| 1196 |
|
|
o = vdie(pat, &args); |
| 1197 |
|
|
va_end(args); |
| 1198 |
|
|
return o; |
| 1199 |
|
|
} |
| 1200 |
|
|
|
| 1201 |
|
|
void |
| 1202 |
|
|
Perl_vcroak(pTHX_ const char* pat, va_list *args) |
| 1203 |
|
|
{ |
| 1204 |
|
|
char *message; |
| 1205 |
|
|
STRLEN msglen; |
| 1206 |
|
|
I32 utf8 = 0; |
| 1207 |
|
|
|
| 1208 |
|
|
message = S_vdie_croak_common(aTHX_ pat, args, &msglen, &utf8); |
| 1209 |
|
|
|
| 1210 |
|
|
if (PL_in_eval) { |
| 1211 |
|
|
PL_restartop = die_where(message, msglen); |
| 1212 |
|
|
SvFLAGS(ERRSV) |= utf8; |
| 1213 |
|
|
JMPENV_JUMP(3); |
| 1214 |
|
|
} |
| 1215 |
|
|
else if (!message) |
| 1216 |
|
|
message = SvPVx(ERRSV, msglen); |
| 1217 |
|
|
|
| 1218 |
|
|
write_to_stderr(message, msglen); |
| 1219 |
|
|
my_failure_exit(); |
| 1220 |
|
|
} |
| 1221 |
|
|
|
| 1222 |
|
|
#if defined(PERL_IMPLICIT_CONTEXT) |
| 1223 |
|
|
void |
| 1224 |
|
|
Perl_croak_nocontext(const char *pat, ...) |
| 1225 |
|
|
{ |
| 1226 |
|
|
dTHX; |
| 1227 |
|
|
va_list args; |
| 1228 |
|
|
va_start(args, pat); |
| 1229 |
|
|
vcroak(pat, &args); |
| 1230 |
|
|
/* NOTREACHED */ |
| 1231 |
|
|
va_end(args); |
| 1232 |
|
|
} |
| 1233 |
|
|
#endif /* PERL_IMPLICIT_CONTEXT */ |
| 1234 |
|
|
|
| 1235 |
|
|
/* |
| 1236 |
|
|
=head1 Warning and Dieing |
| 1237 |
|
|
|
| 1238 |
|
|
=for apidoc croak |
| 1239 |
|
|
|
| 1240 |
|
|
This is the XSUB-writer's interface to Perl's C<die> function. |
| 1241 |
|
|
Normally call this function the same way you call the C C<printf> |
| 1242 |
|
|
function. Calling C<croak> returns control directly to Perl, |
| 1243 |
|
|
sidestepping the normal C order of execution. See C<warn>. |
| 1244 |
|
|
|
| 1245 |
|
|
If you want to throw an exception object, assign the object to |
| 1246 |
|
|
C<$@> and then pass C<Nullch> to croak(): |
| 1247 |
|
|
|
| 1248 |
|
|
errsv = get_sv("@", TRUE); |
| 1249 |
|
|
sv_setsv(errsv, exception_object); |
| 1250 |
|
|
croak(Nullch); |
| 1251 |
|
|
|
| 1252 |
|
|
=cut |
| 1253 |
|
|
*/ |
| 1254 |
|
|
|
| 1255 |
|
|
void |
| 1256 |
|
|
Perl_croak(pTHX_ const char *pat, ...) |
| 1257 |
|
|
{ |
| 1258 |
|
|
va_list args; |
| 1259 |
|
|
va_start(args, pat); |
| 1260 |
|
|
vcroak(pat, &args); |
| 1261 |
|
|
/* NOTREACHED */ |
| 1262 |
|
|
va_end(args); |
| 1263 |
|
|
} |
| 1264 |
|
|
|
| 1265 |
|
|
void |
| 1266 |
|
|
Perl_vwarn(pTHX_ const char* pat, va_list *args) |
| 1267 |
|
|
{ |
| 1268 |
|
|
char *message; |
| 1269 |
|
|
HV *stash; |
| 1270 |
|
|
GV *gv; |
| 1271 |
|
|
CV *cv; |
| 1272 |
|
|
SV *msv; |
| 1273 |
|
|
STRLEN msglen; |
| 1274 |
|
|
I32 utf8 = 0; |
| 1275 |
|
|
|
| 1276 |
|
|
msv = vmess(pat, args); |
| 1277 |
|
|
utf8 = SvUTF8(msv); |
| 1278 |
|
|
message = SvPV(msv, msglen); |
| 1279 |
|
|
|
| 1280 |
|
|
if (PL_warnhook) { |
| 1281 |
|
|
/* sv_2cv might call Perl_warn() */ |
| 1282 |
|
|
SV *oldwarnhook = PL_warnhook; |
| 1283 |
|
|
ENTER; |
| 1284 |
|
|
SAVESPTR(PL_warnhook); |
| 1285 |
|
|
PL_warnhook = Nullsv; |
| 1286 |
|
|
cv = sv_2cv(oldwarnhook, &stash, &gv, 0); |
| 1287 |
|
|
LEAVE; |
| 1288 |
|
|
if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { |
| 1289 |
|
|
dSP; |
| 1290 |
|
|
SV *msg; |
| 1291 |
|
|
|
| 1292 |
|
|
ENTER; |
| 1293 |
|
|
save_re_context(); |
| 1294 |
|
|
msg = newSVpvn(message, msglen); |
| 1295 |
|
|
SvFLAGS(msg) |= utf8; |
| 1296 |
|
|
SvREADONLY_on(msg); |
| 1297 |
|
|
SAVEFREESV(msg); |
| 1298 |
|
|
|
| 1299 |
|
|
PUSHSTACKi(PERLSI_WARNHOOK); |
| 1300 |
|
|
PUSHMARK(SP); |
| 1301 |
|
|
XPUSHs(msg); |
| 1302 |
|
|
PUTBACK; |
| 1303 |
|
|
call_sv((SV*)cv, G_DISCARD); |
| 1304 |
|
|
POPSTACK; |
| 1305 |
|
|
LEAVE; |
| 1306 |
|
|
return; |
| 1307 |
|
|
} |
| 1308 |
|
|
} |
| 1309 |
|
|
|
| 1310 |
|
|
write_to_stderr(message, msglen); |
| 1311 |
|
|
} |
| 1312 |
|
|
|
| 1313 |
|
|
#if defined(PERL_IMPLICIT_CONTEXT) |
| 1314 |
|
|
void |
| 1315 |
|
|
Perl_warn_nocontext(const char *pat, ...) |
| 1316 |
|
|
{ |
| 1317 |
|
|
dTHX; |
| 1318 |
|
|
va_list args; |
| 1319 |
|
|
va_start(args, pat); |
| 1320 |
|
|
vwarn(pat, &args); |
| 1321 |
|
|
va_end(args); |
| 1322 |
|
|
} |
| 1323 |
|
|
#endif /* PERL_IMPLICIT_CONTEXT */ |
| 1324 |
|
|
|
| 1325 |
|
|
/* |
| 1326 |
|
|
=for apidoc warn |
| 1327 |
|
|
|
| 1328 |
|
|
This is the XSUB-writer's interface to Perl's C<warn> function. Call this |
| 1329 |
|
|
function the same way you call the C C<printf> function. See C<croak>. |
| 1330 |
|
|
|
| 1331 |
|
|
=cut |
| 1332 |
|
|
*/ |
| 1333 |
|
|
|
| 1334 |
|
|
void |
| 1335 |
|
|
Perl_warn(pTHX_ const char *pat, ...) |
| 1336 |
|
|
{ |
| 1337 |
|
|
va_list args; |
| 1338 |
|
|
va_start(args, pat); |
| 1339 |
|
|
vwarn(pat, &args); |
| 1340 |
|
|
va_end(args); |
| 1341 |
|
|
} |
| 1342 |
|
|
|
| 1343 |
|
|
#if defined(PERL_IMPLICIT_CONTEXT) |
| 1344 |
|
|
void |
| 1345 |
|
|
Perl_warner_nocontext(U32 err, const char *pat, ...) |
| 1346 |
|
|
{ |
| 1347 |
|
|
dTHX; |
| 1348 |
|
|
va_list args; |
| 1349 |
|
|
va_start(args, pat); |
| 1350 |
|
|
vwarner(err, pat, &args); |
| 1351 |
|
|
va_end(args); |
| 1352 |
|
|
} |
| 1353 |
|
|
#endif /* PERL_IMPLICIT_CONTEXT */ |
| 1354 |
|
|
|
| 1355 |
|
|
void |
| 1356 |
|
|
Perl_warner(pTHX_ U32 err, const char* pat,...) |
| 1357 |
|
|
{ |
| 1358 |
|
|
va_list args; |
| 1359 |
|
|
va_start(args, pat); |
| 1360 |
|
|
vwarner(err, pat, &args); |
| 1361 |
|
|
va_end(args); |
| 1362 |
|
|
} |
| 1363 |
|
|
|
| 1364 |
|
|
void |
| 1365 |
|
|
Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) |
| 1366 |
|
|
{ |
| 1367 |
|
|
if (ckDEAD(err)) { |
| 1368 |
|
|
SV *msv = vmess(pat, args); |
| 1369 |
|
|
STRLEN msglen; |
| 1370 |
|
|
char *message = SvPV(msv, msglen); |
| 1371 |
|
|
I32 utf8 = SvUTF8(msv); |
| 1372 |
|
|
|
| 1373 |
|
|
#ifdef USE_5005THREADS |
| 1374 |
|
|
DEBUG_S(PerlIO_printf(Perl_debug_log, "croak: 0x%"UVxf" %s", PTR2UV(thr), message)); |
| 1375 |
|
|
#endif /* USE_5005THREADS */ |
| 1376 |
|
|
if (PL_diehook) { |
| 1377 |
|
|
assert(message); |
| 1378 |
|
|
S_vdie_common(aTHX_ message, msglen, utf8); |
| 1379 |
|
|
} |
| 1380 |
|
|
if (PL_in_eval) { |
| 1381 |
|
|
PL_restartop = die_where(message, msglen); |
| 1382 |
|
|
SvFLAGS(ERRSV) |= utf8; |
| 1383 |
|
|
JMPENV_JUMP(3); |
| 1384 |
|
|
} |
| 1385 |
|
|
write_to_stderr(message, msglen); |
| 1386 |
|
|
my_failure_exit(); |
| 1387 |
|
|
} |
| 1388 |
|
|
else { |
| 1389 |
|
|
Perl_vwarn(aTHX_ pat, args); |
| 1390 |
|
|
} |
| 1391 |
|
|
} |
| 1392 |
|
|
|
| 1393 |
|
|
/* since we've already done strlen() for both nam and val |
| 1394 |
|
|
* we can use that info to make things faster than |
| 1395 |
|
|
* sprintf(s, "%s=%s", nam, val) |
| 1396 |
|
|
*/ |
| 1397 |
|
|
#define my_setenv_format(s, nam, nlen, val, vlen) \ |
| 1398 |
|
|
Copy(nam, s, nlen, char); \ |
| 1399 |
|
|
*(s+nlen) = '='; \ |
| 1400 |
|
|
Copy(val, s+(nlen+1), vlen, char); \ |
| 1401 |
|
|
*(s+(nlen+1+vlen)) = '\0' |
| 1402 |
|
|
|
| 1403 |
|
|
#ifdef USE_ENVIRON_ARRAY |
| 1404 |
|
|
/* VMS' my_setenv() is in vms.c */ |
| 1405 |
|
|
#if !defined(WIN32) && !defined(NETWARE) |
| 1406 |
|
|
void |
| 1407 |
|
|
Perl_my_setenv(pTHX_ char *nam, char *val) |
| 1408 |
|
|
{ |
| 1409 |
|
|
#ifdef USE_ITHREADS |
| 1410 |
|
|
/* only parent thread can modify process environment */ |
| 1411 |
|
|
if (PL_curinterp == aTHX) |
| 1412 |
|
|
#endif |
| 1413 |
|
|
{ |
| 1414 |
|
|
#ifndef PERL_USE_SAFE_PUTENV |
| 1415 |
|
|
if (!PL_use_safe_putenv) { |
| 1416 |
|
|
/* most putenv()s leak, so we manipulate environ directly */ |
| 1417 |
|
|
register I32 i=setenv_getix(nam); /* where does it go? */ |
| 1418 |
|
|
int nlen, vlen; |
| 1419 |
|
|
|
| 1420 |
|
|
if (environ == PL_origenviron) { /* need we copy environment? */ |
| 1421 |
|
|
I32 j; |
| 1422 |
|
|
I32 max; |
| 1423 |
|
|
char **tmpenv; |
| 1424 |
|
|
|
| 1425 |
|
|
/*SUPPRESS 530*/ |
| 1426 |
|
|
for (max = i; environ[max]; max++) ; |
| 1427 |
|
|
tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); |
| 1428 |
|
|
for (j=0; j<max; j++) { /* copy environment */ |
| 1429 |
|
|
int len = strlen(environ[j]); |
| 1430 |
|
|
tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char)); |
| 1431 |
|
|
Copy(environ[j], tmpenv[j], len+1, char); |
| 1432 |
|
|
} |
| 1433 |
|
|
tmpenv[max] = Nullch; |
| 1434 |
|
|
environ = tmpenv; /* tell exec where it is now */ |
| 1435 |
|
|
} |
| 1436 |
|
|
if (!val) { |
| 1437 |
|
|
safesysfree(environ[i]); |
| 1438 |
|
|
while (environ[i]) { |
| 1439 |
|
|
environ[i] = environ[i+1]; |
| 1440 |
|
|
i++; |
| 1441 |
|
|
} |
| 1442 |
|
|
return; |
| 1443 |
|
|
} |
| 1444 |
|
|
if (!environ[i]) { /* does not exist yet */ |
| 1445 |
|
|
environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*)); |
| 1446 |
|
|
environ[i+1] = Nullch; /* make sure it's null terminated */ |
| 1447 |
|
|
} |
| 1448 |
|
|
else |
| 1449 |
|
|
safesysfree(environ[i]); |
| 1450 |
|
|
nlen = strlen(nam); |
| 1451 |
|
|
vlen = strlen(val); |
| 1452 |
|
|
|
| 1453 |
|
|
environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char)); |
| 1454 |
|
|
/* all that work just for this */ |
| 1455 |
|
|
my_setenv_format(environ[i], nam, nlen, val, vlen); |
| 1456 |
|
|
} else { |
| 1457 |
|
|
# endif |
| 1458 |
|
|
# if defined(__CYGWIN__) || defined( EPOC) |
| 1459 |
|
|
setenv(nam, val, 1); |
| 1460 |
|
|
# else |
| 1461 |
|
|
char *new_env; |
| 1462 |
|
|
int nlen = strlen(nam), vlen; |
| 1463 |
|
|
if (!val) { |
| 1464 |
|
|
val = ""; |
| 1465 |
|
|
} |
| 1466 |
|
|
vlen = strlen(val); |
| 1467 |
|
|
new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); |
| 1468 |
|
|
/* all that work just for this */ |
| 1469 |
|
|
my_setenv_format(new_env, nam, nlen, val, vlen); |
| 1470 |
|
|
(void)putenv(new_env); |
| 1471 |
|
|
# endif /* __CYGWIN__ */ |
| 1472 |
|
|
#ifndef PERL_USE_SAFE_PUTENV |
| 1473 |
|
|
} |
| 1474 |
|
|
#endif |
| 1475 |
|
|
} |
| 1476 |
|
|
} |
| 1477 |
|
|
|
| 1478 |
|
|
#else /* WIN32 || NETWARE */ |
| 1479 |
|
|
|
| 1480 |
|
|
void |
| 1481 |
|
|
Perl_my_setenv(pTHX_ char *nam,char *val) |
| 1482 |
|
|
{ |
| 1483 |
|
|
register char *envstr; |
| 1484 |
|
|
int nlen = strlen(nam), vlen; |
| 1485 |
|
|
|
| 1486 |
|
|
if (!val) { |
| 1487 |
|
|
val = ""; |
| 1488 |
|
|
} |
| 1489 |
|
|
vlen = strlen(val); |
| 1490 |
|
|
New(904, envstr, nlen+vlen+2, char); |
| 1491 |
|
|
my_setenv_format(envstr, nam, nlen, val, vlen); |
| 1492 |
|
|
(void)PerlEnv_putenv(envstr); |
| 1493 |
|
|
Safefree(envstr); |
| 1494 |
|
|
} |
| 1495 |
|
|
|
| 1496 |
|
|
#endif /* WIN32 || NETWARE */ |
| 1497 |
|
|
|
| 1498 |
|
|
#ifndef PERL_MICRO |
| 1499 |
|
|
I32 |
| 1500 |
|
|
Perl_setenv_getix(pTHX_ char *nam) |
| 1501 |
|
|
{ |
| 1502 |
|
|
register I32 i, len = strlen(nam); |
| 1503 |
|
|
|
| 1504 |
|
|
for (i = 0; environ[i]; i++) { |
| 1505 |
|
|
if ( |
| 1506 |
|
|
#ifdef WIN32 |
| 1507 |
|
|
strnicmp(environ[i],nam,len) == 0 |
| 1508 |
|
|
#else |
| 1509 |
|
|
strnEQ(environ[i],nam,len) |
| 1510 |
|
|
#endif |
| 1511 |
|
|
&& environ[i][len] == '=') |
| 1512 |
|
|
break; /* strnEQ must come first to avoid */ |
| 1513 |
|
|
} /* potential SEGV's */ |
| 1514 |
|
|
return i; |
| 1515 |
|
|
} |
| 1516 |
|
|
#endif /* !PERL_MICRO */ |
| 1517 |
|
|
|
| 1518 |
|
|
#endif /* !VMS && !EPOC*/ |
| 1519 |
|
|
|
| 1520 |
|
|
#ifdef UNLINK_ALL_VERSIONS |
| 1521 |
|
|
I32 |
| 1522 |
|
|
Perl_unlnk(pTHX_ char *f) /* unlink all versions of a file */ |
| 1523 |
|
|
{ |
| 1524 |
|
|
I32 i; |
| 1525 |
|
|
|
| 1526 |
|
|
for (i = 0; PerlLIO_unlink(f) >= 0; i++) ; |
| 1527 |
|
|
return i ? 0 : -1; |
| 1528 |
|
|
} |
| 1529 |
|
|
#endif |
| 1530 |
|
|
|
| 1531 |
|
|
/* this is a drop-in replacement for bcopy() */ |
| 1532 |
|
|
#if (!defined(HAS_MEMCPY) && !defined(HAS_BCOPY)) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY) && !defined(HAS_SAFE_BCOPY)) |
| 1533 |
|
|
char * |
| 1534 |
|
|
Perl_my_bcopy(register const char *from,register char *to,register I32 len) |
| 1535 |
|
|
{ |
| 1536 |
|
|
char *retval = to; |
| 1537 |
|
|
|
| 1538 |
|
|
if (from - to >= 0) { |
| 1539 |
|
|
while (len--) |
| 1540 |
|
|
*to++ = *from++; |
| 1541 |
|
|
} |
| 1542 |
|
|
else { |
| 1543 |
|
|
to += len; |
| 1544 |
|
|
from += len; |
| 1545 |
|
|
while (len--) |
| 1546 |
|
|
*(--to) = *(--from); |
| 1547 |
|
|
} |
| 1548 |
|
|
return retval; |
| 1549 |
|
|
} |
| 1550 |
|
|
#endif |
| 1551 |
|
|
|
| 1552 |
|
|
/* this is a drop-in replacement for memset() */ |
| 1553 |
|
|
#ifndef HAS_MEMSET |
| 1554 |
|
|
void * |
| 1555 |
|
|
Perl_my_memset(register char *loc, register I32 ch, register I32 len) |
| 1556 |
|
|
{ |
| 1557 |
|
|
char *retval = loc; |
| 1558 |
|
|
|
| 1559 |
|
|
while (len--) |
| 1560 |
|
|
*loc++ = ch; |
| 1561 |
|
|
return retval; |
| 1562 |
|
|
} |
| 1563 |
|
|
#endif |
| 1564 |
|
|
|
| 1565 |
|
|
/* this is a drop-in replacement for bzero() */ |
| 1566 |
|
|
#if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
| 1567 |
|
|
char * |
| 1568 |
|
|
Perl_my_bzero(register char *loc, register I32 len) |
| 1569 |
|
|
{ |
| 1570 |
|
|
char *retval = loc; |
| 1571 |
|
|
|
| 1572 |
|
|
while (len--) |
| 1573 |
|
|
*loc++ = 0; |
| 1574 |
|
|
return retval; |
| 1575 |
|
|
} |
| 1576 |
|
|
#endif |
| 1577 |
|
|
|
| 1578 |
|
|
/* this is a drop-in replacement for memcmp() */ |
| 1579 |
|
|
#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
| 1580 |
|
|
I32 |
| 1581 |
|
|
Perl_my_memcmp(const char *s1, const char *s2, register I32 len) |
| 1582 |
|
|
{ |
| 1583 |
|
|
register U8 *a = (U8 *)s1; |
| 1584 |
|
|
register U8 *b = (U8 *)s2; |
| 1585 |
|
|
register I32 tmp; |
| 1586 |
|
|
|
| 1587 |
|
|
while (len--) { |
| 1588 |
|
|
if (tmp = *a++ - *b++) |
| 1589 |
|
|
return tmp; |
| 1590 |
|
|
} |
| 1591 |
|
|
return 0; |
| 1592 |
|
|
} |
| 1593 |
|
|
#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
| 1594 |
|
|
|
| 1595 |
|
|
#ifndef HAS_VPRINTF |
| 1596 |
|
|
|
| 1597 |
|
|
#ifdef USE_CHAR_VSPRINTF |
| 1598 |
|
|
char * |
| 1599 |
|
|
#else |
| 1600 |
|
|
int |
| 1601 |
|
|
#endif |
| 1602 |
|
|
vsprintf(char *dest, const char *pat, char *args) |
| 1603 |
|
|
{ |
| 1604 |
|
|
FILE fakebuf; |
| 1605 |
|
|
|
| 1606 |
|
|
fakebuf._ptr = dest; |
| 1607 |
|
|
fakebuf._cnt = 32767; |
| 1608 |
|
|
#ifndef _IOSTRG |
| 1609 |
|
|
#define _IOSTRG 0 |
| 1610 |
|
|
#endif |
| 1611 |
|
|
fakebuf._flag = _IOWRT|_IOSTRG; |
| 1612 |
|
|
_doprnt(pat, args, &fakebuf); /* what a kludge */ |
| 1613 |
|
|
(void)putc('\0', &fakebuf); |
| 1614 |
|
|
#ifdef USE_CHAR_VSPRINTF |
| 1615 |
|
|
return(dest); |
| 1616 |
|
|
#else |
| 1617 |
|
|
return 0; /* perl doesn't use return value */ |
| 1618 |
|
|
#endif |
| 1619 |
|
|
} |
| 1620 |
|
|
|
| 1621 |
|
|
#endif /* HAS_VPRINTF */ |
| 1622 |
|
|
|
| 1623 |
|
|
#ifdef MYSWAP |
| 1624 |
|
|
#if BYTEORDER != 0x4321 |
| 1625 |
|
|
short |
| 1626 |
|
|
Perl_my_swap(pTHX_ short s) |
| 1627 |
|
|
{ |
| 1628 |
|
|
#if (BYTEORDER & 1) == 0 |
| 1629 |
|
|
short result; |
| 1630 |
|
|
|
| 1631 |
|
|
result = ((s & 255) << 8) + ((s >> 8) & 255); |
| 1632 |
|
|
return result; |
| 1633 |
|
|
#else |
| 1634 |
|
|
return s; |
| 1635 |
|
|
#endif |
| 1636 |
|
|
} |
| 1637 |
|
|
|
| 1638 |
|
|
long |
| 1639 |
|
|
Perl_my_htonl(pTHX_ long l) |
| 1640 |
|
|
{ |
| 1641 |
|
|
union { |
| 1642 |
|
|
long result; |
| 1643 |
|
|
char c[sizeof(long)]; |
| 1644 |
|
|
} u; |
| 1645 |
|
|
|
| 1646 |
|
|
#if BYTEORDER == 0x1234 |
| 1647 |
|
|
u.c[0] = (l >> 24) & 255; |
| 1648 |
|
|
u.c[1] = (l >> 16) & 255; |
| 1649 |
|
|
u.c[2] = (l >> 8) & 255; |
| 1650 |
|
|
u.c[3] = l & 255; |
| 1651 |
|
|
return u.result; |
| 1652 |
|
|
#else |
| 1653 |
|
|
#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
| 1654 |
|
|
Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
| 1655 |
|
|
#else |
| 1656 |
|
|
register I32 o; |
| 1657 |
|
|
register I32 s; |
| 1658 |
|
|
|
| 1659 |
|
|
for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
| 1660 |
|
|
u.c[o & 0xf] = (l >> s) & 255; |
| 1661 |
|
|
} |
| 1662 |
|
|
return u.result; |
| 1663 |
|
|
#endif |
| 1664 |
|
|
#endif |
| 1665 |
|
|
} |
| 1666 |
|
|
|
| 1667 |
|
|
long |
| 1668 |
|
|
Perl_my_ntohl(pTHX_ long l) |
| 1669 |
|
|
{ |
| 1670 |
|
|
union { |
| 1671 |
|
|
long l; |
| 1672 |
|
|
char c[sizeof(long)]; |
| 1673 |
|
|
} u; |
| 1674 |
|
|
|
| 1675 |
|
|
#if BYTEORDER == 0x1234 |
| 1676 |
|
|
u.c[0] = (l >> 24) & 255; |
| 1677 |
|
|
u.c[1] = (l >> 16) & 255; |
| 1678 |
|
|
u.c[2] = (l >> 8) & 255; |
| 1679 |
|
|
u.c[3] = l & 255; |
| 1680 |
|
|
return u.l; |
| 1681 |
|
|
#else |
| 1682 |
|
|
#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
| 1683 |
|
|
Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
| 1684 |
|
|
#else |
| 1685 |
|
|
register I32 o; |
| 1686 |
|
|
register I32 s; |
| 1687 |
|
|
|
| 1688 |
|
|
u.l = l; |
| 1689 |
|
|
l = 0; |
| 1690 |
|
|
for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
| 1691 |
|
|
l |= (u.c[o & 0xf] & 255) << s; |
| 1692 |
|
|
} |
| 1693 |
|
|
return l; |
| 1694 |
|
|
#endif |
| 1695 |
|
|
#endif |
| 1696 |
|
|
} |
| 1697 |
|
|
|
| 1698 |
|
|
#endif /* BYTEORDER != 0x4321 */ |
| 1699 |
|
|
#endif /* MYSWAP */ |
| 1700 |
|
|
|
| 1701 |
|
|
/* |
| 1702 |
|
|
* Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. |
| 1703 |
|
|
* If these functions are defined, |
| 1704 |
|
|
* the BYTEORDER is neither 0x1234 nor 0x4321. |
| 1705 |
|
|
* However, this is not assumed. |
| 1706 |
|
|
* -DWS |
| 1707 |
|
|
*/ |
| 1708 |
|
|
|
| 1709 |
|
|
#define HTOLE(name,type) \ |
| 1710 |
|
|
type \ |
| 1711 |
|
|
name (register type n) \ |
| 1712 |
|
|
{ \ |
| 1713 |
|
|
union { \ |
| 1714 |
|
|
type value; \ |
| 1715 |
|
|
char c[sizeof(type)]; \ |
| 1716 |
|
|
} u; \ |
| 1717 |
|
|
register I32 i; \ |
| 1718 |
|
|
register I32 s = 0; \ |
| 1719 |
|
|
for (i = 0; i < sizeof(u.c); i++, s += 8) { \ |
| 1720 |
|
|
u.c[i] = (n >> s) & 0xFF; \ |
| 1721 |
|
|
} \ |
| 1722 |
|
|
return u.value; \ |
| 1723 |
|
|
} |
| 1724 |
|
|
|
| 1725 |
|
|
#define LETOH(name,type) \ |
| 1726 |
|
|
type \ |
| 1727 |
|
|
name (register type n) \ |
| 1728 |
|
|
{ \ |
| 1729 |
|
|
union { \ |
| 1730 |
|
|
type value; \ |
| 1731 |
|
|
char c[sizeof(type)]; \ |
| 1732 |
|
|
} u; \ |
| 1733 |
|
|
register I32 i; \ |
| 1734 |
|
|
register I32 s = 0; \ |
| 1735 |
|
|
u.value = n; \ |
| 1736 |
|
|
n = 0; \ |
| 1737 |
|
|
for (i = 0; i < sizeof(u.c); i++, s += 8) { \ |
| 1738 |
|
|
n |= ((type)(u.c[i] & 0xFF)) << s; \ |
| 1739 |
|
|
} \ |
| 1740 |
|
|
return n; \ |
| 1741 |
|
|
} |
| 1742 |
|
|
|
| 1743 |
|
|
/* |
| 1744 |
|
|
* Big-endian byte order functions. |
| 1745 |
|
|
*/ |
| 1746 |
|
|
|
| 1747 |
|
|
#define HTOBE(name,type) \ |
| 1748 |
|
|
type \ |
| 1749 |
|
|
name (register type n) \ |
| 1750 |
|
|
{ \ |
| 1751 |
|
|
union { \ |
| 1752 |
|
|
type value; \ |
| 1753 |
|
|
char c[sizeof(type)]; \ |
| 1754 |
|
|
} u; \ |
| 1755 |
|
|
register I32 i; \ |
| 1756 |
|
|
register I32 s = 8*(sizeof(u.c)-1); \ |
| 1757 |
|
|
for (i = 0; i < sizeof(u.c); i++, s -= 8) { \ |
| 1758 |
|
|
u.c[i] = (n >> s) & 0xFF; \ |
| 1759 |
|
|
} \ |
| 1760 |
|
|
return u.value; \ |
| 1761 |
|
|
} |
| 1762 |
|
|
|
| 1763 |
|
|
#define BETOH(name,type) \ |
| 1764 |
|
|
type \ |
| 1765 |
|
|
name (register type n) \ |
| 1766 |
|
|
{ \ |
| 1767 |
|
|
union { \ |
| 1768 |
|
|
type value; \ |
| 1769 |
|
|
char c[sizeof(type)]; \ |
| 1770 |
|
|
} u; \ |
| 1771 |
|
|
register I32 i; \ |
| 1772 |
|
|
register I32 s = 8*(sizeof(u.c)-1); \ |
| 1773 |
|
|
u.value = n; \ |
| 1774 |
|
|
n = 0; \ |
| 1775 |
|
|
for (i = 0; i < sizeof(u.c); i++, s -= 8) { \ |
| 1776 |
|
|
n |= ((type)(u.c[i] & 0xFF)) << s; \ |
| 1777 |
|
|
} \ |
| 1778 |
|
|
return n; \ |
| 1779 |
|
|
} |
| 1780 |
|
|
|
| 1781 |
|
|
/* |
| 1782 |
|
|
* If we just can't do it... |
| 1783 |
|
|
*/ |
| 1784 |
|
|
|
| 1785 |
|
|
#define NOT_AVAIL(name,type) \ |
| 1786 |
|
|
type \ |
| 1787 |
|
|
name (register type n) \ |
| 1788 |
|
|
{ \ |
| 1789 |
|
|
Perl_croak_nocontext(#name "() not available"); \ |
| 1790 |
|
|
return n; /* not reached */ \ |
| 1791 |
|
|
} |
| 1792 |
|
|
|
| 1793 |
|
|
|
| 1794 |
|
|
#if defined(HAS_HTOVS) && !defined(htovs) |
| 1795 |
|
|
HTOLE(htovs,short) |
| 1796 |
|
|
#endif |
| 1797 |
|
|
#if defined(HAS_HTOVL) && !defined(htovl) |
| 1798 |
|
|
HTOLE(htovl,long) |
| 1799 |
|
|
#endif |
| 1800 |
|
|
#if defined(HAS_VTOHS) && !defined(vtohs) |
| 1801 |
|
|
LETOH(vtohs,short) |
| 1802 |
|
|
#endif |
| 1803 |
|
|
#if defined(HAS_VTOHL) && !defined(vtohl) |
| 1804 |
|
|
LETOH(vtohl,long) |
| 1805 |
|
|
#endif |
| 1806 |
|
|
|
| 1807 |
|
|
#ifdef PERL_NEED_MY_HTOLE16 |
| 1808 |
|
|
# if U16SIZE == 2 |
| 1809 |
|
|
HTOLE(Perl_my_htole16,U16) |
| 1810 |
|
|
# else |
| 1811 |
|
|
NOT_AVAIL(Perl_my_htole16,U16) |
| 1812 |
|
|
# endif |
| 1813 |
|
|
#endif |
| 1814 |
|
|
#ifdef PERL_NEED_MY_LETOH16 |
| 1815 |
|
|
# if U16SIZE == 2 |
| 1816 |
|
|
LETOH(Perl_my_letoh16,U16) |
| 1817 |
|
|
# else |
| 1818 |
|
|
NOT_AVAIL(Perl_my_letoh16,U16) |
| 1819 |
|
|
# endif |
| 1820 |
|
|
#endif |
| 1821 |
|
|
#ifdef PERL_NEED_MY_HTOBE16 |
| 1822 |
|
|
# if U16SIZE == 2 |
| 1823 |
|
|
HTOBE(Perl_my_htobe16,U16) |
| 1824 |
|
|
# else |
| 1825 |
|
|
NOT_AVAIL(Perl_my_htobe16,U16) |
| 1826 |
|
|
# endif |
| 1827 |
|
|
#endif |
| 1828 |
|
|
#ifdef PERL_NEED_MY_BETOH16 |
| 1829 |
|
|
# if U16SIZE == 2 |
| 1830 |
|
|
BETOH(Perl_my_betoh16,U16) |
| 1831 |
|
|
# else |
| 1832 |
|
|
NOT_AVAIL(Perl_my_betoh16,U16) |
| 1833 |
|
|
# endif |
| 1834 |
|
|
#endif |
| 1835 |
|
|
|
| 1836 |
|
|
#ifdef PERL_NEED_MY_HTOLE32 |
| 1837 |
|
|
# if U32SIZE == 4 |
| 1838 |
|
|
HTOLE(Perl_my_htole32,U32) |
| 1839 |
|
|
# else |
| 1840 |
|
|
NOT_AVAIL(Perl_my_htole32,U32) |
| 1841 |
|
|
# endif |
| 1842 |
|
|
#endif |
| 1843 |
|
|
#ifdef PERL_NEED_MY_LETOH32 |
| 1844 |
|
|
# if U32SIZE == 4 |
| 1845 |
|
|
LETOH(Perl_my_letoh32,U32) |
| 1846 |
|
|
# else |
| 1847 |
|
|
NOT_AVAIL(Perl_my_letoh32,U32) |
| 1848 |
|
|
# endif |
| 1849 |
|
|
#endif |
| 1850 |
|
|
#ifdef PERL_NEED_MY_HTOBE32 |
| 1851 |
|
|
# if U32SIZE == 4 |
| 1852 |
|
|
HTOBE(Perl_my_htobe32,U32) |
| 1853 |
|
|
# else |
| 1854 |
|
|
NOT_AVAIL(Perl_my_htobe32,U32) |
| 1855 |
|
|
# endif |
| 1856 |
|
|
#endif |
| 1857 |
|
|
#ifdef PERL_NEED_MY_BETOH32 |
| 1858 |
|
|
# if U32SIZE == 4 |
| 1859 |
|
|
BETOH(Perl_my_betoh32,U32) |
| 1860 |
|
|
# else |
| 1861 |
|
|
NOT_AVAIL(Perl_my_betoh32,U32) |
| 1862 |
|
|
# endif |
| 1863 |
|
|
#endif |
| 1864 |
|
|
|
| 1865 |
|
|
#ifdef PERL_NEED_MY_HTOLE64 |
| 1866 |
|
|
# if U64SIZE == 8 |
| 1867 |
|
|
HTOLE(Perl_my_htole64,U64) |
| 1868 |
|
|
# else |
| 1869 |
|
|
NOT_AVAIL(Perl_my_htole64,U64) |
| 1870 |
|
|
# endif |
| 1871 |
|
|
#endif |
| 1872 |
|
|
#ifdef PERL_NEED_MY_LETOH64 |
| 1873 |
|
|
# if U64SIZE == 8 |
| 1874 |
|
|
LETOH(Perl_my_letoh64,U64) |
| 1875 |
|
|
# else |
| 1876 |
|
|
NOT_AVAIL(Perl_my_letoh64,U64) |
| 1877 |
|
|
# endif |
| 1878 |
|
|
#endif |
| 1879 |
|
|
#ifdef PERL_NEED_MY_HTOBE64 |
| 1880 |
|
|
# if U64SIZE == 8 |
| 1881 |
|
|
HTOBE(Perl_my_htobe64,U64) |
| 1882 |
|
|
# else |
| 1883 |
|
|
NOT_AVAIL(Perl_my_htobe64,U64) |
| 1884 |
|
|
# endif |
| 1885 |
|
|
#endif |
| 1886 |
|
|
#ifdef PERL_NEED_MY_BETOH64 |
| 1887 |
|
|
# if U64SIZE == 8 |
| 1888 |
|
|
BETOH(Perl_my_betoh64,U64) |
| 1889 |
|
|
# else |
| 1890 |
|
|
NOT_AVAIL(Perl_my_betoh64,U64) |
| 1891 |
|
|
# endif |
| 1892 |
|
|
#endif |
| 1893 |
|
|
|
| 1894 |
|
|
#ifdef PERL_NEED_MY_HTOLES |
| 1895 |
|
|
HTOLE(Perl_my_htoles,short) |
| 1896 |
|
|
#endif |
| 1897 |
|
|
#ifdef PERL_NEED_MY_LETOHS |
| 1898 |
|
|
LETOH(Perl_my_letohs,short) |
| 1899 |
|
|
#endif |
| 1900 |
|
|
#ifdef PERL_NEED_MY_HTOBES |
| 1901 |
|
|
HTOBE(Perl_my_htobes,short) |
| 1902 |
|
|
#endif |
| 1903 |
|
|
#ifdef PERL_NEED_MY_BETOHS |
| 1904 |
|
|
BETOH(Perl_my_betohs,short) |
| 1905 |
|
|
#endif |
| 1906 |
|
|
|
| 1907 |
|
|
#ifdef PERL_NEED_MY_HTOLEI |
| 1908 |
|
|
HTOLE(Perl_my_htolei,int) |
| 1909 |
|
|
#endif |
| 1910 |
|
|
#ifdef PERL_NEED_MY_LETOHI |
| 1911 |
|
|
LETOH(Perl_my_letohi,int) |
| 1912 |
|
|
#endif |
| 1913 |
|
|
#ifdef PERL_NEED_MY_HTOBEI |
| 1914 |
|
|
HTOBE(Perl_my_htobei,int) |
| 1915 |
|
|
#endif |
| 1916 |
|
|
#ifdef PERL_NEED_MY_BETOHI |
| 1917 |
|
|
BETOH(Perl_my_betohi,int) |
| 1918 |
|
|
#endif |
| 1919 |
|
|
|
| 1920 |
|
|
#ifdef PERL_NEED_MY_HTOLEL |
| 1921 |
|
|
HTOLE(Perl_my_htolel,long) |
| 1922 |
|
|
#endif |
| 1923 |
|
|
#ifdef PERL_NEED_MY_LETOHL |
| 1924 |
|
|
LETOH(Perl_my_letohl,long) |
| 1925 |
|
|
#endif |
| 1926 |
|
|
#ifdef PERL_NEED_MY_HTOBEL |
| 1927 |
|
|
HTOBE(Perl_my_htobel,long) |
| 1928 |
|
|
#endif |
| 1929 |
|
|
#ifdef PERL_NEED_MY_BETOHL |
| 1930 |
|
|
BETOH(Perl_my_betohl,long) |
| 1931 |
|
|
#endif |
| 1932 |
|
|
|
| 1933 |
|
|
void |
| 1934 |
|
|
Perl_my_swabn(void *ptr, int n) |
| 1935 |
|
|
{ |
| 1936 |
|
|
register char *s = (char *)ptr; |
| 1937 |
|
|
register char *e = s + (n-1); |
| 1938 |
|
|
register char tc; |
| 1939 |
|
|
|
| 1940 |
|
|
for (n /= 2; n > 0; s++, e--, n--) { |
| 1941 |
|
|
tc = *s; |
| 1942 |
|
|
*s = *e; |
| 1943 |
|
|
*e = tc; |
| 1944 |
|
|
} |
| 1945 |
|
|
} |
| 1946 |
|
|
|
| 1947 |
|
|
PerlIO * |
| 1948 |
|
|
Perl_my_popen_list(pTHX_ char *mode, int n, SV **args) |
| 1949 |
|
|
{ |
| 1950 |
|
|
#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(OS2) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) && !defined(NETWARE) |
| 1951 |
|
|
int p[2]; |
| 1952 |
|
|
register I32 This, that; |
| 1953 |
|
|
register Pid_t pid; |
| 1954 |
|
|
SV *sv; |
| 1955 |
|
|
I32 did_pipes = 0; |
| 1956 |
|
|
int pp[2]; |
| 1957 |
|
|
|
| 1958 |
|
|
PERL_FLUSHALL_FOR_CHILD; |
| 1959 |
|
|
This = (*mode == 'w'); |
| 1960 |
|
|
that = !This; |
| 1961 |
|
|
if (PL_tainting) { |
| 1962 |
|
|
taint_env(); |
| 1963 |
|
|
taint_proper("Insecure %s%s", "EXEC"); |
| 1964 |
|
|
} |
| 1965 |
|
|
if (PerlProc_pipe(p) < 0) |
| 1966 |
|
|
return Nullfp; |
| 1967 |
|
|
/* Try for another pipe pair for error return */ |
| 1968 |
|
|
if (PerlProc_pipe(pp) >= 0) |
| 1969 |
|
|
did_pipes = 1; |
| 1970 |
|
|
while ((pid = PerlProc_fork()) < 0) { |
| 1971 |
|
|
if (errno != EAGAIN) { |
| 1972 |
|
|
PerlLIO_close(p[This]); |
| 1973 |
|
|
PerlLIO_close(p[that]); |
| 1974 |
|
|
if (did_pipes) { |
| 1975 |
|
|
PerlLIO_close(pp[0]); |
| 1976 |
|
|
PerlLIO_close(pp[1]); |
| 1977 |
|
|
} |
| 1978 |
|
|
return Nullfp; |
| 1979 |
|
|
} |
| 1980 |
|
|
sleep(5); |
| 1981 |
|
|
} |
| 1982 |
|
|
if (pid == 0) { |
| 1983 |
|
|
/* Child */ |
| 1984 |
|
|
#undef THIS |
| 1985 |
|
|
#undef THAT |
| 1986 |
|
|
#define THIS that |
| 1987 |
|
|
#define THAT This |
| 1988 |
|
|
/* Close parent's end of error status pipe (if any) */ |
| 1989 |
|
|
if (did_pipes) { |
| 1990 |
|
|
PerlLIO_close(pp[0]); |
| 1991 |
|
|
#if defined(HAS_FCNTL) && defined(F_SETFD) |
| 1992 |
|
|
/* Close error pipe automatically if exec works */ |
| 1993 |
|
|
fcntl(pp[1], F_SETFD, FD_CLOEXEC); |
| 1994 |
|
|
#endif |
| 1995 |
|
|
} |
| 1996 |
|
|
/* Now dup our end of _the_ pipe to right position */ |
| 1997 |
|
|
if (p[THIS] != (*mode == 'r')) { |
| 1998 |
|
|
PerlLIO_dup2(p[THIS], *mode == 'r'); |
| 1999 |
|
|
PerlLIO_close(p[THIS]); |
| 2000 |
|
|
if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
| 2001 |
|
|
PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ |
| 2002 |
|
|
} |
| 2003 |
|
|
else |
| 2004 |
|
|
PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ |
| 2005 |
|
|
#if !defined(HAS_FCNTL) || !defined(F_SETFD) |
| 2006 |
|
|
/* No automatic close - do it by hand */ |
| 2007 |
|
|
# ifndef NOFILE |
| 2008 |
|
|
# define NOFILE 20 |
| 2009 |
|
|
# endif |
| 2010 |
|
|
{ |
| 2011 |
|
|
int fd; |
| 2012 |
|
|
|
| 2013 |
|
|
for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) { |
| 2014 |
|
|
if (fd != pp[1]) |
| 2015 |
|
|
PerlLIO_close(fd); |
| 2016 |
|
|
} |
| 2017 |
|
|
} |
| 2018 |
|
|
#endif |
| 2019 |
|
|
do_aexec5(Nullsv, args-1, args-1+n, pp[1], did_pipes); |
| 2020 |
|
|
PerlProc__exit(1); |
| 2021 |
|
|
#undef THIS |
| 2022 |
|
|
#undef THAT |
| 2023 |
|
|
} |
| 2024 |
|
|
/* Parent */ |
| 2025 |
|
|
do_execfree(); /* free any memory malloced by child on fork */ |
| 2026 |
|
|
if (did_pipes) |
| 2027 |
|
|
PerlLIO_close(pp[1]); |
| 2028 |
|
|
/* Keep the lower of the two fd numbers */ |
| 2029 |
|
|
if (p[that] < p[This]) { |
| 2030 |
|
|
PerlLIO_dup2(p[This], p[that]); |
| 2031 |
|
|
PerlLIO_close(p[This]); |
| 2032 |
|
|
p[This] = p[that]; |
| 2033 |
|
|
} |
| 2034 |
|
|
else |
| 2035 |
|
|
PerlLIO_close(p[that]); /* close child's end of pipe */ |
| 2036 |
|
|
|
| 2037 |
|
|
LOCK_FDPID_MUTEX; |
| 2038 |
|
|
sv = *av_fetch(PL_fdpid,p[This],TRUE); |
| 2039 |
|
|
UNLOCK_FDPID_MUTEX; |
| 2040 |
|
|
(void)SvUPGRADE(sv,SVt_IV); |
| 2041 |
|
|
SvIVX(sv) = pid; |
| 2042 |
|
|
PL_forkprocess = pid; |
| 2043 |
|
|
/* If we managed to get status pipe check for exec fail */ |
| 2044 |
|
|
if (did_pipes && pid > 0) { |
| 2045 |
|
|
int errkid; |
| 2046 |
|
|
int n = 0, n1; |
| 2047 |
|
|
|
| 2048 |
|
|
while (n < sizeof(int)) { |
| 2049 |
|
|
n1 = PerlLIO_read(pp[0], |
| 2050 |
|
|
(void*)(((char*)&errkid)+n), |
| 2051 |
|
|
(sizeof(int)) - n); |
| 2052 |
|
|
if (n1 <= 0) |
| 2053 |
|
|
break; |
| 2054 |
|
|
n += n1; |
| 2055 |
|
|
} |
| 2056 |
|
|
PerlLIO_close(pp[0]); |
| 2057 |
|
|
did_pipes = 0; |
| 2058 |
|
|
if (n) { /* Error */ |
| 2059 |
|
|
int pid2, status; |
| 2060 |
|
|
PerlLIO_close(p[This]); |
| 2061 |
|
|
if (n != sizeof(int)) |
| 2062 |
|
|
Perl_croak(aTHX_ "panic: kid popen errno read"); |
| 2063 |
|
|
do { |
| 2064 |
|
|
pid2 = wait4pid(pid, &status, 0); |
| 2065 |
|
|
} while (pid2 == -1 && errno == EINTR); |
| 2066 |
|
|
errno = errkid; /* Propagate errno from kid */ |
| 2067 |
|
|
return Nullfp; |
| 2068 |
|
|
} |
| 2069 |
|
|
} |
| 2070 |
|
|
if (did_pipes) |
| 2071 |
|
|
PerlLIO_close(pp[0]); |
| 2072 |
|
|
return PerlIO_fdopen(p[This], mode); |
| 2073 |
|
|
#else |
| 2074 |
|
|
Perl_croak(aTHX_ "List form of piped open not implemented"); |
| 2075 |
|
|
return (PerlIO *) NULL; |
| 2076 |
|
|
#endif |
| 2077 |
|
|
} |
| 2078 |
|
|
|
| 2079 |
|
|
/* VMS' my_popen() is in VMS.c, same with OS/2. */ |
| 2080 |
|
|
#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) |
| 2081 |
|
|
PerlIO * |
| 2082 |
|
|
Perl_my_popen(pTHX_ char *cmd, char *mode) |
| 2083 |
|
|
{ |
| 2084 |
|
|
int p[2]; |
| 2085 |
|
|
register I32 This, that; |
| 2086 |
|
|
register Pid_t pid; |
| 2087 |
|
|
SV *sv; |
| 2088 |
|
|
I32 doexec = !(*cmd == '-' && cmd[1] == '\0'); |
| 2089 |
|
|
I32 did_pipes = 0; |
| 2090 |
|
|
int pp[2]; |
| 2091 |
|
|
|
| 2092 |
|
|
PERL_FLUSHALL_FOR_CHILD; |
| 2093 |
|
|
#ifdef OS2 |
| 2094 |
|
|
if (doexec) { |
| 2095 |
|
|
return my_syspopen(aTHX_ cmd,mode); |
| 2096 |
|
|
} |
| 2097 |
|
|
#endif |
| 2098 |
|
|
This = (*mode == 'w'); |
| 2099 |
|
|
that = !This; |
| 2100 |
|
|
if (doexec && PL_tainting) { |
| 2101 |
|
|
taint_env(); |
| 2102 |
|
|
taint_proper("Insecure %s%s", "EXEC"); |
| 2103 |
|
|
} |
| 2104 |
|
|
if (PerlProc_pipe(p) < 0) |
| 2105 |
|
|
return Nullfp; |
| 2106 |
|
|
if (doexec && PerlProc_pipe(pp) >= 0) |
| 2107 |
|
|
did_pipes = 1; |
| 2108 |
|
|
while ((pid = PerlProc_fork()) < 0) { |
| 2109 |
|
|
if (errno != EAGAIN) { |
| 2110 |
|
|
PerlLIO_close(p[This]); |
| 2111 |
|
|
PerlLIO_close(p[that]); |
| 2112 |
|
|
if (did_pipes) { |
| 2113 |
|
|
PerlLIO_close(pp[0]); |
| 2114 |
|
|
PerlLIO_close(pp[1]); |
| 2115 |
|
|
} |
| 2116 |
|
|
if (!doexec) |
| 2117 |
|
|
Perl_croak(aTHX_ "Can't fork"); |
| 2118 |
|
|
return Nullfp; |
| 2119 |
|
|
} |
| 2120 |
|
|
sleep(5); |
| 2121 |
|
|
} |
| 2122 |
|
|
if (pid == 0) { |
| 2123 |
|
|
GV* tmpgv; |
| 2124 |
|
|
|
| 2125 |
|
|
#undef THIS |
| 2126 |
|
|
#undef THAT |
| 2127 |
|
|
#define THIS that |
| 2128 |
|
|
#define THAT This |
| 2129 |
|
|
if (did_pipes) { |
| 2130 |
|
|
PerlLIO_close(pp[0]); |
| 2131 |
|
|
#if defined(HAS_FCNTL) && defined(F_SETFD) |
| 2132 |
|
|
fcntl(pp[1], F_SETFD, FD_CLOEXEC); |
| 2133 |
|
|
#endif |
| 2134 |
|
|
} |
| 2135 |
|
|
if (p[THIS] != (*mode == 'r')) { |
| 2136 |
|
|
PerlLIO_dup2(p[THIS], *mode == 'r'); |
| 2137 |
|
|
PerlLIO_close(p[THIS]); |
| 2138 |
|
|
if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
| 2139 |
|
|
PerlLIO_close(p[THAT]); |
| 2140 |
|
|
} |
| 2141 |
|
|
else |
| 2142 |
|
|
PerlLIO_close(p[THAT]); |
| 2143 |
|
|
#ifndef OS2 |
| 2144 |
|
|
if (doexec) { |
| 2145 |
|
|
#if !defined(HAS_FCNTL) || !defined(F_SETFD) |
| 2146 |
|
|
int fd; |
| 2147 |
|
|
|
| 2148 |
|
|
#ifndef NOFILE |
| 2149 |
|
|
#define NOFILE 20 |
| 2150 |
|
|
#endif |
| 2151 |
|
|
{ |
| 2152 |
|
|
int fd; |
| 2153 |
|
|
|
| 2154 |
|
|
for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) |
| 2155 |
|
|
if (fd != pp[1]) |
| 2156 |
|
|
PerlLIO_close(fd); |
| 2157 |
|
|
} |
| 2158 |
|
|
#endif |
| 2159 |
|
|
/* may or may not use the shell */ |
| 2160 |
|
|
do_exec3(cmd, pp[1], did_pipes); |
| 2161 |
|
|
PerlProc__exit(1); |
| 2162 |
|
|
} |
| 2163 |
|
|
#endif /* defined OS2 */ |
| 2164 |
|
|
/*SUPPRESS 560*/ |
| 2165 |
|
|
if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) { |
| 2166 |
|
|
SvREADONLY_off(GvSV(tmpgv)); |
| 2167 |
|
|
sv_setiv(GvSV(tmpgv), PerlProc_getpid()); |
| 2168 |
|
|
SvREADONLY_on(GvSV(tmpgv)); |
| 2169 |
|
|
} |
| 2170 |
|
|
#ifdef THREADS_HAVE_PIDS |
| 2171 |
|
|
PL_ppid = (IV)getppid(); |
| 2172 |
|
|
#endif |
| 2173 |
|
|
PL_forkprocess = 0; |
| 2174 |
|
|
hv_clear(PL_pidstatus); /* we have no children */ |
| 2175 |
|
|
return Nullfp; |
| 2176 |
|
|
#undef THIS |
| 2177 |
|
|
#undef THAT |
| 2178 |
|
|
} |
| 2179 |
|
|
do_execfree(); /* free any memory malloced by child on vfork */ |
| 2180 |
|
|
if (did_pipes) |
| 2181 |
|
|
PerlLIO_close(pp[1]); |
| 2182 |
|
|
if (p[that] < p[This]) { |
| 2183 |
|
|
PerlLIO_dup2(p[This], p[that]); |
| 2184 |
|
|
PerlLIO_close(p[This]); |
| 2185 |
|
|
p[This] = p[that]; |
| 2186 |
|
|
} |
| 2187 |
|
|
else |
| 2188 |
|
|
PerlLIO_close(p[that]); |
| 2189 |
|
|
|
| 2190 |
|
|
LOCK_FDPID_MUTEX; |
| 2191 |
|
|
sv = *av_fetch(PL_fdpid,p[This],TRUE); |
| 2192 |
|
|
UNLOCK_FDPID_MUTEX; |
| 2193 |
|
|
(void)SvUPGRADE(sv,SVt_IV); |
| 2194 |
|
|
SvIVX(sv) = pid; |
| 2195 |
|
|
PL_forkprocess = pid; |
| 2196 |
|
|
if (did_pipes && pid > 0) { |
| 2197 |
|
|
int errkid; |
| 2198 |
|
|
int n = 0, n1; |
| 2199 |
|
|
|
| 2200 |
|
|
while (n < sizeof(int)) { |
| 2201 |
|
|
n1 = PerlLIO_read(pp[0], |
| 2202 |
|
|
(void*)(((char*)&errkid)+n), |
| 2203 |
|
|
(sizeof(int)) - n); |
| 2204 |
|
|
if (n1 <= 0) |
| 2205 |
|
|
break; |
| 2206 |
|
|
n += n1; |
| 2207 |
|
|
} |
| 2208 |
|
|
PerlLIO_close(pp[0]); |
| 2209 |
|
|
did_pipes = 0; |
| 2210 |
|
|
if (n) { /* Error */ |
| 2211 |
|
|
int pid2, status; |
| 2212 |
|
|
PerlLIO_close(p[This]); |
| 2213 |
|
|
if (n != sizeof(int)) |
| 2214 |
|
|
Perl_croak(aTHX_ "panic: kid popen errno read"); |
| 2215 |
|
|
do { |
| 2216 |
|
|
pid2 = wait4pid(pid, &status, 0); |
| 2217 |
|
|
} while (pid2 == -1 && errno == EINTR); |
| 2218 |
|
|
errno = errkid; /* Propagate errno from kid */ |
| 2219 |
|
|
return Nullfp; |
| 2220 |
|
|
} |
| 2221 |
|
|
} |
| 2222 |
|
|
if (did_pipes) |
| 2223 |
|
|
PerlLIO_close(pp[0]); |
| 2224 |
|
|
return PerlIO_fdopen(p[This], mode); |
| 2225 |
|
|
} |
| 2226 |
|
|
#else |
| 2227 |
|
|
#if defined(atarist) || defined(EPOC) |
| 2228 |
|
|
FILE *popen(); |
| 2229 |
|
|
PerlIO * |
| 2230 |
|
|
Perl_my_popen(pTHX_ char *cmd, char *mode) |
| 2231 |
|
|
{ |
| 2232 |
|
|
PERL_FLUSHALL_FOR_CHILD; |
| 2233 |
|
|
/* Call system's popen() to get a FILE *, then import it. |
| 2234 |
|
|
used 0 for 2nd parameter to PerlIO_importFILE; |
| 2235 |
|
|
apparently not used |
| 2236 |
|
|
*/ |
| 2237 |
|
|
return PerlIO_importFILE(popen(cmd, mode), 0); |
| 2238 |
|
|
} |
| 2239 |
|
|
#else |
| 2240 |
|
|
#if defined(DJGPP) |
| 2241 |
|
|
FILE *djgpp_popen(); |
| 2242 |
|
|
PerlIO * |
| 2243 |
|
|
Perl_my_popen(pTHX_ char *cmd, char *mode) |
| 2244 |
|
|
{ |
| 2245 |
|
|
PERL_FLUSHALL_FOR_CHILD; |
| 2246 |
|
|
/* Call system's popen() to get a FILE *, then import it. |
| 2247 |
|
|
used 0 for 2nd parameter to PerlIO_importFILE; |
| 2248 |
|
|
apparently not used |
| 2249 |
|
|
*/ |
| 2250 |
|
|
return PerlIO_importFILE(djgpp_popen(cmd, mode), 0); |
| 2251 |
|
|
} |
| 2252 |
|
|
#endif |
| 2253 |
|
|
#endif |
| 2254 |
|
|
|
| 2255 |
|
|
#endif /* !DOSISH */ |
| 2256 |
|
|
|
| 2257 |
|
|
/* this is called in parent before the fork() */ |
| 2258 |
|
|
void |
| 2259 |
|
|
Perl_atfork_lock(void) |
| 2260 |
|
|
{ |
| 2261 |
|
|
#if defined(USE_5005THREADS) || defined(USE_ITHREADS) |
| 2262 |
|
|
/* locks must be held in locking order (if any) */ |
| 2263 |
|
|
# ifdef MYMALLOC |
| 2264 |
|
|
MUTEX_LOCK(&PL_malloc_mutex); |
| 2265 |
|
|
# endif |
| 2266 |
|
|
OP_REFCNT_LOCK; |
| 2267 |
|
|
#endif |
| 2268 |
|
|
} |
| 2269 |
|
|
|
| 2270 |
|
|
/* this is called in both parent and child after the fork() */ |
| 2271 |
|
|
void |
| 2272 |
|
|
Perl_atfork_unlock(void) |
| 2273 |
|
|
{ |
| 2274 |
|
|
#if defined(USE_5005THREADS) || defined(USE_ITHREADS) |
| 2275 |
|
|
/* locks must be released in same order as in atfork_lock() */ |
| 2276 |
|
|
# ifdef MYMALLOC |
| 2277 |
|
|
MUTEX_UNLOCK(&PL_malloc_mutex); |
| 2278 |
|
|
# endif |
| 2279 |
|
|
OP_REFCNT_UNLOCK; |
| 2280 |
|
|
#endif |
| 2281 |
|
|
} |
| 2282 |
|
|
|
| 2283 |
|
|
Pid_t |
| 2284 |
|
|
Perl_my_fork(void) |
| 2285 |
|
|
{ |
| 2286 |
|
|
#if defined(HAS_FORK) |
| 2287 |
|
|
Pid_t pid; |
| 2288 |
|
|
#if (defined(USE_5005THREADS) || defined(USE_ITHREADS)) && !defined(HAS_PTHREAD_ATFORK) |
| 2289 |
|
|
atfork_lock(); |
| 2290 |
|
|
pid = fork(); |
| 2291 |
|
|
atfork_unlock(); |
| 2292 |
|
|
#else |
| 2293 |
|
|
/* atfork_lock() and atfork_unlock() are installed as pthread_atfork() |
| 2294 |
|
|
* handlers elsewhere in the code */ |
| 2295 |
|
|
pid = fork(); |
| 2296 |
|
|
#endif |
| 2297 |
|
|
return pid; |
| 2298 |
|
|
#else |
| 2299 |
|
|
/* this "canna happen" since nothing should be calling here if !HAS_FORK */ |
| 2300 |
|
|
Perl_croak_nocontext("fork() not available"); |
| 2301 |
|
|
return 0; |
| 2302 |
|
|
#endif /* HAS_FORK */ |
| 2303 |
|
|
} |
| 2304 |
|
|
|
| 2305 |
|
|
#ifdef DUMP_FDS |
| 2306 |
|
|
void |
| 2307 |
|
|
Perl_dump_fds(pTHX_ char *s) |
| 2308 |
|
|
{ |
| 2309 |
|
|
int fd; |
| 2310 |
|
|
Stat_t tmpstatbuf; |
| 2311 |
|
|
|
| 2312 |
|
|
PerlIO_printf(Perl_debug_log,"%s", s); |
| 2313 |
|
|
for (fd = 0; fd < 32; fd++) { |
| 2314 |
|
|
if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0) |
| 2315 |
|
|
PerlIO_printf(Perl_debug_log," %d",fd); |
| 2316 |
|
|
} |
| 2317 |
|
|
PerlIO_printf(Perl_debug_log,"\n"); |
| 2318 |
|
|
} |
| 2319 |
|
|
#endif /* DUMP_FDS */ |
| 2320 |
|
|
|
| 2321 |
|
|
#ifndef HAS_DUP2 |
| 2322 |
|
|
int |
| 2323 |
|
|
dup2(int oldfd, int newfd) |
| 2324 |
|
|
{ |
| 2325 |
|
|
#if defined(HAS_FCNTL) && defined(F_DUPFD) |
| 2326 |
|
|
if (oldfd == newfd) |
| 2327 |
|
|
return oldfd; |
| 2328 |
|
|
PerlLIO_close(newfd); |
| 2329 |
|
|
return fcntl(oldfd, F_DUPFD, newfd); |
| 2330 |
|
|
#else |
| 2331 |
|
|
#define DUP2_MAX_FDS 256 |
| 2332 |
|
|
int fdtmp[DUP2_MAX_FDS]; |
| 2333 |
|
|
I32 fdx = 0; |
| 2334 |
|
|
int fd; |
| 2335 |
|
|
|
| 2336 |
|
|
if (oldfd == newfd) |
| 2337 |
|
|
return oldfd; |
| 2338 |
|
|
PerlLIO_close(newfd); |
| 2339 |
|
|
/* good enough for low fd's... */ |
| 2340 |
|
|
while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) { |
| 2341 |
|
|
if (fdx >= DUP2_MAX_FDS) { |
| 2342 |
|
|
PerlLIO_close(fd); |
| 2343 |
|
|
fd = -1; |
| 2344 |
|
|
break; |
| 2345 |
|
|
} |
| 2346 |
|
|
fdtmp[fdx++] = fd; |
| 2347 |
|
|
} |
| 2348 |
|
|
while (fdx > 0) |
| 2349 |
|
|
PerlLIO_close(fdtmp[--fdx]); |
| 2350 |
|
|
return fd; |
| 2351 |
|
|
#endif |
| 2352 |
|
|
} |
| 2353 |
|
|
#endif |
| 2354 |
|
|
|
| 2355 |
|
|
#ifndef PERL_MICRO |
| 2356 |
|
|
#ifdef HAS_SIGACTION |
| 2357 |
|
|
|
| 2358 |
|
|
#ifdef MACOS_TRADITIONAL |
| 2359 |
|
|
/* We don't want restart behavior on MacOS */ |
| 2360 |
|
|
#undef SA_RESTART |
| 2361 |
|
|
#endif |
| 2362 |
|
|
|
| 2363 |
|
|
Sighandler_t |
| 2364 |
|
|
Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
| 2365 |
|
|
{ |
| 2366 |
|
|
struct sigaction act, oact; |
| 2367 |
|
|
|
| 2368 |
|
|
#ifdef USE_ITHREADS |
| 2369 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2370 |
|
|
if (PL_curinterp != aTHX) |
| 2371 |
|
|
return SIG_ERR; |
| 2372 |
|
|
#endif |
| 2373 |
|
|
|
| 2374 |
|
|
act.sa_handler = handler; |
| 2375 |
|
|
sigemptyset(&act.sa_mask); |
| 2376 |
|
|
act.sa_flags = 0; |
| 2377 |
|
|
#ifdef SA_RESTART |
| 2378 |
|
|
if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
| 2379 |
|
|
act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ |
| 2380 |
|
|
#endif |
| 2381 |
|
|
#if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
| 2382 |
|
|
if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN) |
| 2383 |
|
|
act.sa_flags |= SA_NOCLDWAIT; |
| 2384 |
|
|
#endif |
| 2385 |
|
|
if (sigaction(signo, &act, &oact) == -1) |
| 2386 |
|
|
return SIG_ERR; |
| 2387 |
|
|
else |
| 2388 |
|
|
return oact.sa_handler; |
| 2389 |
|
|
} |
| 2390 |
|
|
|
| 2391 |
|
|
Sighandler_t |
| 2392 |
|
|
Perl_rsignal_state(pTHX_ int signo) |
| 2393 |
|
|
{ |
| 2394 |
|
|
struct sigaction oact; |
| 2395 |
|
|
|
| 2396 |
|
|
if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) |
| 2397 |
|
|
return SIG_ERR; |
| 2398 |
|
|
else |
| 2399 |
|
|
return oact.sa_handler; |
| 2400 |
|
|
} |
| 2401 |
|
|
|
| 2402 |
|
|
int |
| 2403 |
|
|
Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
| 2404 |
|
|
{ |
| 2405 |
|
|
struct sigaction act; |
| 2406 |
|
|
|
| 2407 |
|
|
#ifdef USE_ITHREADS |
| 2408 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2409 |
|
|
if (PL_curinterp != aTHX) |
| 2410 |
|
|
return -1; |
| 2411 |
|
|
#endif |
| 2412 |
|
|
|
| 2413 |
|
|
act.sa_handler = handler; |
| 2414 |
|
|
sigemptyset(&act.sa_mask); |
| 2415 |
|
|
act.sa_flags = 0; |
| 2416 |
|
|
#ifdef SA_RESTART |
| 2417 |
|
|
if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
| 2418 |
|
|
act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ |
| 2419 |
|
|
#endif |
| 2420 |
|
|
#if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
| 2421 |
|
|
if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN) |
| 2422 |
|
|
act.sa_flags |= SA_NOCLDWAIT; |
| 2423 |
|
|
#endif |
| 2424 |
|
|
return sigaction(signo, &act, save); |
| 2425 |
|
|
} |
| 2426 |
|
|
|
| 2427 |
|
|
int |
| 2428 |
|
|
Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
| 2429 |
|
|
{ |
| 2430 |
|
|
#ifdef USE_ITHREADS |
| 2431 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2432 |
|
|
if (PL_curinterp != aTHX) |
| 2433 |
|
|
return -1; |
| 2434 |
|
|
#endif |
| 2435 |
|
|
|
| 2436 |
|
|
return sigaction(signo, save, (struct sigaction *)NULL); |
| 2437 |
|
|
} |
| 2438 |
|
|
|
| 2439 |
|
|
#else /* !HAS_SIGACTION */ |
| 2440 |
|
|
|
| 2441 |
|
|
Sighandler_t |
| 2442 |
|
|
Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
| 2443 |
|
|
{ |
| 2444 |
|
|
#if defined(USE_ITHREADS) && !defined(WIN32) |
| 2445 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2446 |
|
|
if (PL_curinterp != aTHX) |
| 2447 |
|
|
return SIG_ERR; |
| 2448 |
|
|
#endif |
| 2449 |
|
|
|
| 2450 |
|
|
return PerlProc_signal(signo, handler); |
| 2451 |
|
|
} |
| 2452 |
|
|
|
| 2453 |
|
|
static int sig_trapped; /* XXX signals are process-wide anyway, so we |
| 2454 |
|
|
ignore the implications of this for threading */ |
| 2455 |
|
|
|
| 2456 |
|
|
static |
| 2457 |
|
|
Signal_t |
| 2458 |
|
|
sig_trap(int signo) |
| 2459 |
|
|
{ |
| 2460 |
|
|
sig_trapped++; |
| 2461 |
|
|
} |
| 2462 |
|
|
|
| 2463 |
|
|
Sighandler_t |
| 2464 |
|
|
Perl_rsignal_state(pTHX_ int signo) |
| 2465 |
|
|
{ |
| 2466 |
|
|
Sighandler_t oldsig; |
| 2467 |
|
|
|
| 2468 |
|
|
#if defined(USE_ITHREADS) && !defined(WIN32) |
| 2469 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2470 |
|
|
if (PL_curinterp != aTHX) |
| 2471 |
|
|
return SIG_ERR; |
| 2472 |
|
|
#endif |
| 2473 |
|
|
|
| 2474 |
|
|
sig_trapped = 0; |
| 2475 |
|
|
oldsig = PerlProc_signal(signo, sig_trap); |
| 2476 |
|
|
PerlProc_signal(signo, oldsig); |
| 2477 |
|
|
if (sig_trapped) |
| 2478 |
|
|
PerlProc_kill(PerlProc_getpid(), signo); |
| 2479 |
|
|
return oldsig; |
| 2480 |
|
|
} |
| 2481 |
|
|
|
| 2482 |
|
|
int |
| 2483 |
|
|
Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
| 2484 |
|
|
{ |
| 2485 |
|
|
#if defined(USE_ITHREADS) && !defined(WIN32) |
| 2486 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2487 |
|
|
if (PL_curinterp != aTHX) |
| 2488 |
|
|
return -1; |
| 2489 |
|
|
#endif |
| 2490 |
|
|
*save = PerlProc_signal(signo, handler); |
| 2491 |
|
|
return (*save == SIG_ERR) ? -1 : 0; |
| 2492 |
|
|
} |
| 2493 |
|
|
|
| 2494 |
|
|
int |
| 2495 |
|
|
Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
| 2496 |
|
|
{ |
| 2497 |
|
|
#if defined(USE_ITHREADS) && !defined(WIN32) |
| 2498 |
|
|
/* only "parent" interpreter can diddle signals */ |
| 2499 |
|
|
if (PL_curinterp != aTHX) |
| 2500 |
|
|
return -1; |
| 2501 |
|
|
#endif |
| 2502 |
|
|
return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0; |
| 2503 |
|
|
} |
| 2504 |
|
|
|
| 2505 |
|
|
#endif /* !HAS_SIGACTION */ |
| 2506 |
|
|
#endif /* !PERL_MICRO */ |
| 2507 |
|
|
|
| 2508 |
|
|
/* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
| 2509 |
|
|
#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) |
| 2510 |
|
|
I32 |
| 2511 |
|
|
Perl_my_pclose(pTHX_ PerlIO *ptr) |
| 2512 |
|
|
{ |
| 2513 |
|
|
Sigsave_t hstat, istat, qstat; |
| 2514 |
|
|
int status; |
| 2515 |
|
|
SV **svp; |
| 2516 |
|
|
Pid_t pid; |
| 2517 |
|
|
Pid_t pid2; |
| 2518 |
|
|
bool close_failed; |
| 2519 |
|
|
int saved_errno = 0; |
| 2520 |
|
|
#ifdef VMS |
| 2521 |
|
|
int saved_vaxc_errno; |
| 2522 |
|
|
#endif |
| 2523 |
|
|
#ifdef WIN32 |
| 2524 |
|
|
int saved_win32_errno; |
| 2525 |
|
|
#endif |
| 2526 |
|
|
|
| 2527 |
|
|
LOCK_FDPID_MUTEX; |
| 2528 |
|
|
svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE); |
| 2529 |
|
|
UNLOCK_FDPID_MUTEX; |
| 2530 |
|
|
pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1; |
| 2531 |
|
|
SvREFCNT_dec(*svp); |
| 2532 |
|
|
*svp = &PL_sv_undef; |
| 2533 |
|
|
#ifdef OS2 |
| 2534 |
|
|
if (pid == -1) { /* Opened by popen. */ |
| 2535 |
|
|
return my_syspclose(ptr); |
| 2536 |
|
|
} |
| 2537 |
|
|
#endif |
| 2538 |
|
|
if ((close_failed = (PerlIO_close(ptr) == EOF))) { |
| 2539 |
|
|
saved_errno = errno; |
| 2540 |
|
|
#ifdef VMS |
| 2541 |
|
|
saved_vaxc_errno = vaxc$errno; |
| 2542 |
|
|
#endif |
| 2543 |
|
|
#ifdef WIN32 |
| 2544 |
|
|
saved_win32_errno = GetLastError(); |
| 2545 |
|
|
#endif |
| 2546 |
|
|
} |
| 2547 |
|
|
#ifdef UTS |
| 2548 |
|
|
if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ |
| 2549 |
|
|
#endif |
| 2550 |
|
|
#ifndef PERL_MICRO |
| 2551 |
|
|
rsignal_save(SIGHUP, SIG_IGN, &hstat); |
| 2552 |
|
|
rsignal_save(SIGINT, SIG_IGN, &istat); |
| 2553 |
|
|
rsignal_save(SIGQUIT, SIG_IGN, &qstat); |
| 2554 |
|
|
#endif |
| 2555 |
|
|
do { |
| 2556 |
|
|
pid2 = wait4pid(pid, &status, 0); |
| 2557 |
|
|
} while (pid2 == -1 && errno == EINTR); |
| 2558 |
|
|
#ifndef PERL_MICRO |
| 2559 |
|
|
rsignal_restore(SIGHUP, &hstat); |
| 2560 |
|
|
rsignal_restore(SIGINT, &istat); |
| 2561 |
|
|
rsignal_restore(SIGQUIT, &qstat); |
| 2562 |
|
|
#endif |
| 2563 |
|
|
if (close_failed) { |
| 2564 |
|
|
SETERRNO(saved_errno, saved_vaxc_errno); |
| 2565 |
|
|
return -1; |
| 2566 |
|
|
} |
| 2567 |
|
|
return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)); |
| 2568 |
|
|
} |
| 2569 |
|
|
#endif /* !DOSISH */ |
| 2570 |
|
|
|
| 2571 |
|
|
#if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(MACOS_TRADITIONAL) |
| 2572 |
|
|
I32 |
| 2573 |
|
|
Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags) |
| 2574 |
|
|
{ |
| 2575 |
|
|
I32 result; |
| 2576 |
|
|
if (!pid) |
| 2577 |
|
|
return -1; |
| 2578 |
|
|
#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME) |
| 2579 |
|
|
{ |
| 2580 |
|
|
SV *sv; |
| 2581 |
|
|
SV** svp; |
| 2582 |
|
|
char spid[TYPE_CHARS(IV)]; |
| 2583 |
|
|
|
| 2584 |
|
|
if (pid > 0) { |
| 2585 |
|
|
sprintf(spid, "%"IVdf, (IV)pid); |
| 2586 |
|
|
svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE); |
| 2587 |
|
|
if (svp && *svp != &PL_sv_undef) { |
| 2588 |
|
|
*statusp = SvIVX(*svp); |
| 2589 |
|
|
(void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD); |
| 2590 |
|
|
return pid; |
| 2591 |
|
|
} |
| 2592 |
|
|
} |
| 2593 |
|
|
else { |
| 2594 |
|
|
HE *entry; |
| 2595 |
|
|
|
| 2596 |
|
|
hv_iterinit(PL_pidstatus); |
| 2597 |
|
|
if ((entry = hv_iternext(PL_pidstatus))) { |
| 2598 |
|
|
pid = atoi(hv_iterkey(entry,(I32*)statusp)); |
| 2599 |
|
|
sv = hv_iterval(PL_pidstatus,entry); |
| 2600 |
|
|
*statusp = SvIVX(sv); |
| 2601 |
|
|
sprintf(spid, "%"IVdf, (IV)pid); |
| 2602 |
|
|
(void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD); |
| 2603 |
|
|
return pid; |
| 2604 |
|
|
} |
| 2605 |
|
|
} |
| 2606 |
|
|
} |
| 2607 |
|
|
#endif |
| 2608 |
|
|
#ifdef HAS_WAITPID |
| 2609 |
|
|
# ifdef HAS_WAITPID_RUNTIME |
| 2610 |
|
|
if (!HAS_WAITPID_RUNTIME) |
| 2611 |
|
|
goto hard_way; |
| 2612 |
|
|
# endif |
| 2613 |
|
|
result = PerlProc_waitpid(pid,statusp,flags); |
| 2614 |
|
|
goto finish; |
| 2615 |
|
|
#endif |
| 2616 |
|
|
#if !defined(HAS_WAITPID) && defined(HAS_WAIT4) |
| 2617 |
|
|
result = wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *)); |
| 2618 |
|
|
goto finish; |
| 2619 |
|
|
#endif |
| 2620 |
|
|
#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME) |
| 2621 |
|
|
hard_way: |
| 2622 |
|
|
{ |
| 2623 |
|
|
if (flags) |
| 2624 |
|
|
Perl_croak(aTHX_ "Can't do waitpid with flags"); |
| 2625 |
|
|
else { |
| 2626 |
|
|
while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0) |
| 2627 |
|
|
pidgone(result,*statusp); |
| 2628 |
|
|
if (result < 0) |
| 2629 |
|
|
*statusp = -1; |
| 2630 |
|
|
} |
| 2631 |
|
|
} |
| 2632 |
|
|
#endif |
| 2633 |
|
|
finish: |
| 2634 |
|
|
if (result < 0 && errno == EINTR) { |
| 2635 |
|
|
PERL_ASYNC_CHECK(); |
| 2636 |
|
|
} |
| 2637 |
|
|
return result; |
| 2638 |
|
|
} |
| 2639 |
|
|
#endif /* !DOSISH || OS2 || WIN32 || NETWARE */ |
| 2640 |
|
|
|
| 2641 |
|
|
void |
| 2642 |
|
|
/*SUPPRESS 590*/ |
| 2643 |
|
|
Perl_pidgone(pTHX_ Pid_t pid, int status) |
| 2644 |
|
|
{ |
| 2645 |
|
|
register SV *sv; |
| 2646 |
|
|
char spid[TYPE_CHARS(IV)]; |
| 2647 |
|
|
|
| 2648 |
|
|
sprintf(spid, "%"IVdf, (IV)pid); |
| 2649 |
|
|
sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE); |
| 2650 |
|
|
(void)SvUPGRADE(sv,SVt_IV); |
| 2651 |
|
|
SvIVX(sv) = status; |
| 2652 |
|
|
return; |
| 2653 |
|
|
} |
| 2654 |
|
|
|
| 2655 |
|
|
#if defined(atarist) || defined(OS2) || defined(EPOC) |
| 2656 |
|
|
int pclose(); |
| 2657 |
|
|
#ifdef HAS_FORK |
| 2658 |
|
|
int /* Cannot prototype with I32 |
| 2659 |
|
|
in os2ish.h. */ |
| 2660 |
|
|
my_syspclose(PerlIO *ptr) |
| 2661 |
|
|
#else |
| 2662 |
|
|
I32 |
| 2663 |
|
|
Perl_my_pclose(pTHX_ PerlIO *ptr) |
| 2664 |
|
|
#endif |
| 2665 |
|
|
{ |
| 2666 |
|
|
/* Needs work for PerlIO ! */ |
| 2667 |
|
|
FILE *f = PerlIO_findFILE(ptr); |
| 2668 |
|
|
I32 result = pclose(f); |
| 2669 |
|
|
PerlIO_releaseFILE(ptr,f); |
| 2670 |
|
|
return result; |
| 2671 |
|
|
} |
| 2672 |
|
|
#endif |
| 2673 |
|
|
|
| 2674 |
|
|
#if defined(DJGPP) |
| 2675 |
|
|
int djgpp_pclose(); |
| 2676 |
|
|
I32 |
| 2677 |
|
|
Perl_my_pclose(pTHX_ PerlIO *ptr) |
| 2678 |
|
|
{ |
| 2679 |
|
|
/* Needs work for PerlIO ! */ |
| 2680 |
|
|
FILE *f = PerlIO_findFILE(ptr); |
| 2681 |
|
|
I32 result = djgpp_pclose(f); |
| 2682 |
|
|
result = (result << 8) & 0xff00; |
| 2683 |
|
|
PerlIO_releaseFILE(ptr,f); |
| 2684 |
|
|
return result; |
| 2685 |
|
|
} |
| 2686 |
|
|
#endif |
| 2687 |
|
|
|
| 2688 |
|
|
void |
| 2689 |
|
|
Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, register I32 count) |
| 2690 |
|
|
{ |
| 2691 |
|
|
register I32 todo; |
| 2692 |
|
|
register const char *frombase = from; |
| 2693 |
|
|
|
| 2694 |
|
|
if (len == 1) { |
| 2695 |
|
|
register const char c = *from; |
| 2696 |
|
|
while (count-- > 0) |
| 2697 |
|
|
*to++ = c; |
| 2698 |
|
|
return; |
| 2699 |
|
|
} |
| 2700 |
|
|
while (count-- > 0) { |
| 2701 |
|
|
for (todo = len; todo > 0; todo--) { |
| 2702 |
|
|
*to++ = *from++; |
| 2703 |
|
|
} |
| 2704 |
|
|
from = frombase; |
| 2705 |
|
|
} |
| 2706 |
|
|
} |
| 2707 |
|
|
|
| 2708 |
|
|
#ifndef HAS_RENAME |
| 2709 |
|
|
I32 |
| 2710 |
|
|
Perl_same_dirent(pTHX_ char *a, char *b) |
| 2711 |
|
|
{ |
| 2712 |
|
|
char *fa = strrchr(a,'/'); |
| 2713 |
|
|
char *fb = strrchr(b,'/'); |
| 2714 |
|
|
Stat_t tmpstatbuf1; |
| 2715 |
|
|
Stat_t tmpstatbuf2; |
| 2716 |
|
|
SV *tmpsv = sv_newmortal(); |
| 2717 |
|
|
|
| 2718 |
|
|
if (fa) |
| 2719 |
|
|
fa++; |
| 2720 |
|
|
else |
| 2721 |
|
|
fa = a; |
| 2722 |
|
|
if (fb) |
| 2723 |
|
|
fb++; |
| 2724 |
|
|
else |
| 2725 |
|
|
fb = b; |
| 2726 |
|
|
if (strNE(a,b)) |
| 2727 |
|
|
return FALSE; |
| 2728 |
|
|
if (fa == a) |
| 2729 |
|
|
sv_setpv(tmpsv, "."); |
| 2730 |
|
|
else |
| 2731 |
|
|
sv_setpvn(tmpsv, a, fa - a); |
| 2732 |
|
|
if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0) |
| 2733 |
|
|
return FALSE; |
| 2734 |
|
|
if (fb == b) |
| 2735 |
|
|
sv_setpv(tmpsv, "."); |
| 2736 |
|
|
else |
| 2737 |
|
|
sv_setpvn(tmpsv, b, fb - b); |
| 2738 |
|
|
if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0) |
| 2739 |
|
|
return FALSE; |
| 2740 |
|
|
return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && |
| 2741 |
|
|
tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; |
| 2742 |
|
|
} |
| 2743 |
|
|
#endif /* !HAS_RENAME */ |
| 2744 |
|
|
|
| 2745 |
|
|
char* |
| 2746 |
|
|
Perl_find_script(pTHX_ char *scriptname, bool dosearch, char **search_ext, I32 flags) |
| 2747 |
|
|
{ |
| 2748 |
|
|
char *xfound = Nullch; |
| 2749 |
|
|
char *xfailed = Nullch; |
| 2750 |
|
|
char tmpbuf[MAXPATHLEN]; |
| 2751 |
|
|
register char *s; |
| 2752 |
|
|
I32 len = 0; |
| 2753 |
|
|
int retval; |
| 2754 |
|
|
#if defined(DOSISH) && !defined(OS2) && !defined(atarist) |
| 2755 |
|
|
# define SEARCH_EXTS ".bat", ".cmd", NULL |
| 2756 |
|
|
# define MAX_EXT_LEN 4 |
| 2757 |
|
|
#endif |
| 2758 |
|
|
#ifdef OS2 |
| 2759 |
|
|
# define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL |
| 2760 |
|
|
# define MAX_EXT_LEN 4 |
| 2761 |
|
|
#endif |
| 2762 |
|
|
#ifdef VMS |
| 2763 |
|
|
# define SEARCH_EXTS ".pl", ".com", NULL |
| 2764 |
|
|
# define MAX_EXT_LEN 4 |
| 2765 |
|
|
#endif |
| 2766 |
|
|
/* additional extensions to try in each dir if scriptname not found */ |
| 2767 |
|
|
#ifdef SEARCH_EXTS |
| 2768 |
|
|
char *exts[] = { SEARCH_EXTS }; |
| 2769 |
|
|
char **ext = search_ext ? search_ext : exts; |
| 2770 |
|
|
int extidx = 0, i = 0; |
| 2771 |
|
|
char *curext = Nullch; |
| 2772 |
|
|
#else |
| 2773 |
|
|
# define MAX_EXT_LEN 0 |
| 2774 |
|
|
#endif |
| 2775 |
|
|
|
| 2776 |
|
|
/* |
| 2777 |
|
|
* If dosearch is true and if scriptname does not contain path |
| 2778 |
|
|
* delimiters, search the PATH for scriptname. |
| 2779 |
|
|
* |
| 2780 |
|
|
* If SEARCH_EXTS is also defined, will look for each |
| 2781 |
|
|
* scriptname{SEARCH_EXTS} whenever scriptname is not found |
| 2782 |
|
|
* while searching the PATH. |
| 2783 |
|
|
* |
| 2784 |
|
|
* Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search |
| 2785 |
|
|
* proceeds as follows: |
| 2786 |
|
|
* If DOSISH or VMSISH: |
| 2787 |
|
|
* + look for ./scriptname{,.foo,.bar} |
| 2788 |
|
|
* + search the PATH for scriptname{,.foo,.bar} |
| 2789 |
|
|
* |
| 2790 |
|
|
* If !DOSISH: |
| 2791 |
|
|
* + look *only* in the PATH for scriptname{,.foo,.bar} (note |
| 2792 |
|
|
* this will not look in '.' if it's not in the PATH) |
| 2793 |
|
|
*/ |
| 2794 |
|
|
tmpbuf[0] = '\0'; |
| 2795 |
|
|
|
| 2796 |
|
|
#ifdef VMS |
| 2797 |
|
|
# ifdef ALWAYS_DEFTYPES |
| 2798 |
|
|
len = strlen(scriptname); |
| 2799 |
|
|
if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') { |
| 2800 |
|
|
int hasdir, idx = 0, deftypes = 1; |
| 2801 |
|
|
bool seen_dot = 1; |
| 2802 |
|
|
|
| 2803 |
|
|
hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ; |
| 2804 |
|
|
# else |
| 2805 |
|
|
if (dosearch) { |
| 2806 |
|
|
int hasdir, idx = 0, deftypes = 1; |
| 2807 |
|
|
bool seen_dot = 1; |
| 2808 |
|
|
|
| 2809 |
|
|
hasdir = (strpbrk(scriptname,":[</") != Nullch) ; |
| 2810 |
|
|
# endif |
| 2811 |
|
|
/* The first time through, just add SEARCH_EXTS to whatever we |
| 2812 |
|
|
* already have, so we can check for default file types. */ |
| 2813 |
|
|
while (deftypes || |
| 2814 |
|
|
(!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) ) |
| 2815 |
|
|
{ |
| 2816 |
|
|
if (deftypes) { |
| 2817 |
|
|
deftypes = 0; |
| 2818 |
|
|
*tmpbuf = '\0'; |
| 2819 |
|
|
} |
| 2820 |
|
|
if ((strlen(tmpbuf) + strlen(scriptname) |
| 2821 |
|
|
+ MAX_EXT_LEN) >= sizeof tmpbuf) |
| 2822 |
|
|
continue; /* don't search dir with too-long name */ |
| 2823 |
|
|
strcat(tmpbuf, scriptname); |
| 2824 |
|
|
#else /* !VMS */ |
| 2825 |
|
|
|
| 2826 |
|
|
#ifdef DOSISH |
| 2827 |
|
|
if (strEQ(scriptname, "-")) |
| 2828 |
|
|
dosearch = 0; |
| 2829 |
|
|
if (dosearch) { /* Look in '.' first. */ |
| 2830 |
|
|
char *cur = scriptname; |
| 2831 |
|
|
#ifdef SEARCH_EXTS |
| 2832 |
|
|
if ((curext = strrchr(scriptname,'.'))) /* possible current ext */ |
| 2833 |
|
|
while (ext[i]) |
| 2834 |
|
|
if (strEQ(ext[i++],curext)) { |
| 2835 |
|
|
extidx = -1; /* already has an ext */ |
| 2836 |
|
|
break; |
| 2837 |
|
|
} |
| 2838 |
|
|
do { |
| 2839 |
|
|
#endif |
| 2840 |
|
|
DEBUG_p(PerlIO_printf(Perl_debug_log, |
| 2841 |
|
|
"Looking for %s\n",cur)); |
| 2842 |
|
|
if (PerlLIO_stat(cur,&PL_statbuf) >= 0 |
| 2843 |
|
|
&& !S_ISDIR(PL_statbuf.st_mode)) { |
| 2844 |
|
|
dosearch = 0; |
| 2845 |
|
|
scriptname = cur; |
| 2846 |
|
|
#ifdef SEARCH_EXTS |
| 2847 |
|
|
break; |
| 2848 |
|
|
#endif |
| 2849 |
|
|
} |
| 2850 |
|
|
#ifdef SEARCH_EXTS |
| 2851 |
|
|
if (cur == scriptname) { |
| 2852 |
|
|
len = strlen(scriptname); |
| 2853 |
|
|
if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf)) |
| 2854 |
|
|
break; |
| 2855 |
|
|
cur = strcpy(tmpbuf, scriptname); |
| 2856 |
|
|
} |
| 2857 |
|
|
} while (extidx >= 0 && ext[extidx] /* try an extension? */ |
| 2858 |
|
|
&& strcpy(tmpbuf+len, ext[extidx++])); |
| 2859 |
|
|
#endif |
| 2860 |
|
|
} |
| 2861 |
|
|
#endif |
| 2862 |
|
|
|
| 2863 |
|
|
#ifdef MACOS_TRADITIONAL |
| 2864 |
|
|
if (dosearch && !strchr(scriptname, ':') && |
| 2865 |
|
|
(s = PerlEnv_getenv("Commands"))) |
| 2866 |
|
|
#else |
| 2867 |
|
|
if (dosearch && !strchr(scriptname, '/') |
| 2868 |
|
|
#ifdef DOSISH |
| 2869 |
|
|
&& !strchr(scriptname, '\\') |
| 2870 |
|
|
#endif |
| 2871 |
|
|
&& (s = PerlEnv_getenv("PATH"))) |
| 2872 |
|
|
#endif |
| 2873 |
|
|
{ |
| 2874 |
|
|
bool seen_dot = 0; |
| 2875 |
|
|
|
| 2876 |
|
|
PL_bufend = s + strlen(s); |
| 2877 |
|
|
while (s < PL_bufend) { |
| 2878 |
|
|
#ifdef MACOS_TRADITIONAL |
| 2879 |
|
|
s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend, |
| 2880 |
|
|
',', |
| 2881 |
|
|
&len); |
| 2882 |
|
|
#else |
| 2883 |
|
|
#if defined(atarist) || defined(DOSISH) |
| 2884 |
|
|
for (len = 0; *s |
| 2885 |
|
|
# ifdef atarist |
| 2886 |
|
|
&& *s != ',' |
| 2887 |
|
|
# endif |
| 2888 |
|
|
&& *s != ';'; len++, s++) { |
| 2889 |
|
|
if (len < sizeof tmpbuf) |
| 2890 |
|
|
tmpbuf[len] = *s; |
| 2891 |
|
|
} |
| 2892 |
|
|
if (len < sizeof tmpbuf) |
| 2893 |
|
|
tmpbuf[len] = '\0'; |
| 2894 |
|
|
#else /* ! (atarist || DOSISH) */ |
| 2895 |
|
|
s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend, |
| 2896 |
|
|
':', |
| 2897 |
|
|
&len); |
| 2898 |
|
|
#endif /* ! (atarist || DOSISH) */ |
| 2899 |
|
|
#endif /* MACOS_TRADITIONAL */ |
| 2900 |
|
|
if (s < PL_bufend) |
| 2901 |
|
|
s++; |
| 2902 |
|
|
if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf) |
| 2903 |
|
|
continue; /* don't search dir with too-long name */ |
| 2904 |
|
|
#ifdef MACOS_TRADITIONAL |
| 2905 |
|
|
if (len && tmpbuf[len - 1] != ':') |
| 2906 |
|
|
tmpbuf[len++] = ':'; |
| 2907 |
|
|
#else |
| 2908 |
|
|
if (len |
| 2909 |
|
|
#if defined(atarist) || defined(__MINT__) || defined(DOSISH) |
| 2910 |
|
|
&& tmpbuf[len - 1] != '/' |
| 2911 |
|
|
&& tmpbuf[len - 1] != '\\' |
| 2912 |
|
|
#endif |
| 2913 |
|
|
) |
| 2914 |
|
|
tmpbuf[len++] = '/'; |
| 2915 |
|
|
if (len == 2 && tmpbuf[0] == '.') |
| 2916 |
|
|
seen_dot = 1; |
| 2917 |
|
|
#endif |
| 2918 |
|
|
(void)strcpy(tmpbuf + len, scriptname); |
| 2919 |
|
|
#endif /* !VMS */ |
| 2920 |
|
|
|
| 2921 |
|
|
#ifdef SEARCH_EXTS |
| 2922 |
|
|
len = strlen(tmpbuf); |
| 2923 |
|
|
if (extidx > 0) /* reset after previous loop */ |
| 2924 |
|
|
extidx = 0; |
| 2925 |
|
|
do { |
| 2926 |
|
|
#endif |
| 2927 |
|
|
DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf)); |
| 2928 |
|
|
retval = PerlLIO_stat(tmpbuf,&PL_statbuf); |
| 2929 |
|
|
if (S_ISDIR(PL_statbuf.st_mode)) { |
| 2930 |
|
|
retval = -1; |
| 2931 |
|
|
} |
| 2932 |
|
|
#ifdef SEARCH_EXTS |
| 2933 |
|
|
} while ( retval < 0 /* not there */ |
| 2934 |
|
|
&& extidx>=0 && ext[extidx] /* try an extension? */ |
| 2935 |
|
|
&& strcpy(tmpbuf+len, ext[extidx++]) |
| 2936 |
|
|
); |
| 2937 |
|
|
#endif |
| 2938 |
|
|
if (retval < 0) |
| 2939 |
|
|
continue; |
| 2940 |
|
|
if (S_ISREG(PL_statbuf.st_mode) |
| 2941 |
|
|
&& cando(S_IRUSR,TRUE,&PL_statbuf) |
| 2942 |
|
|
#if !defined(DOSISH) && !defined(MACOS_TRADITIONAL) |
| 2943 |
|
|
&& cando(S_IXUSR,TRUE,&PL_statbuf) |
| 2944 |
|
|
#endif |
| 2945 |
|
|
) |
| 2946 |
|
|
{ |
| 2947 |
|
|
xfound = tmpbuf; /* bingo! */ |
| 2948 |
|
|
break; |
| 2949 |
|
|
} |
| 2950 |
|
|
if (!xfailed) |
| 2951 |
|
|
xfailed = savepv(tmpbuf); |
| 2952 |
|
|
} |
| 2953 |
|
|
#ifndef DOSISH |
| 2954 |
|
|
if (!xfound && !seen_dot && !xfailed && |
| 2955 |
|
|
(PerlLIO_stat(scriptname,&PL_statbuf) < 0 |
| 2956 |
|
|
|| S_ISDIR(PL_statbuf.st_mode))) |
| 2957 |
|
|
#endif |
| 2958 |
|
|
seen_dot = 1; /* Disable message. */ |
| 2959 |
|
|
if (!xfound) { |
| 2960 |
|
|
if (flags & 1) { /* do or die? */ |
| 2961 |
|
|
Perl_croak(aTHX_ "Can't %s %s%s%s", |
| 2962 |
|
|
(xfailed ? "execute" : "find"), |
| 2963 |
|
|
(xfailed ? xfailed : scriptname), |
| 2964 |
|
|
(xfailed ? "" : " on PATH"), |
| 2965 |
|
|
(xfailed || seen_dot) ? "" : ", '.' not in PATH"); |
| 2966 |
|
|
} |
| 2967 |
|
|
scriptname = Nullch; |
| 2968 |
|
|
} |
| 2969 |
|
|
if (xfailed) |
| 2970 |
|
|
Safefree(xfailed); |
| 2971 |
|
|
scriptname = xfound; |
| 2972 |
|
|
} |
| 2973 |
|
|
return (scriptname ? savepv(scriptname) : Nullch); |
| 2974 |
|
|
} |
| 2975 |
|
|
|
| 2976 |
|
|
#ifndef PERL_GET_CONTEXT_DEFINED |
| 2977 |
|
|
|
| 2978 |
|
|
void * |
| 2979 |
|
|
Perl_get_context(void) |
| 2980 |
|
|
{ |
| 2981 |
|
|
#if defined(USE_5005THREADS) || defined(USE_ITHREADS) |
| 2982 |
|
|
# ifdef OLD_PTHREADS_API |
| 2983 |
|
|
pthread_addr_t t; |
| 2984 |
|
|
if (pthread_getspecific(PL_thr_key, &t)) |
| 2985 |
|
|
Perl_croak_nocontext("panic: pthread_getspecific"); |
| 2986 |
|
|
return (void*)t; |
| 2987 |
|
|
# else |
| 2988 |
|
|
# ifdef I_MACH_CTHREADS |
| 2989 |
|
|
return (void*)cthread_data(cthread_self()); |
| 2990 |
|
|
# else |
| 2991 |
|
|
return (void*)PTHREAD_GETSPECIFIC(PL_thr_key); |
| 2992 |
|
|
# endif |
| 2993 |
|
|
# endif |
| 2994 |
|
|
#else |
| 2995 |
|
|
return (void*)NULL; |
| 2996 |
|
|
#endif |
| 2997 |
|
|
} |
| 2998 |
|
|
|
| 2999 |
|
|
void |
| 3000 |
|
|
Perl_set_context(void *t) |
| 3001 |
|
|
{ |
| 3002 |
|
|
#if defined(USE_5005THREADS) || defined(USE_ITHREADS) |
| 3003 |
|
|
# ifdef I_MACH_CTHREADS |
| 3004 |
|
|
cthread_set_data(cthread_self(), t); |
| 3005 |
|
|
# else |
| 3006 |
|
|
if (pthread_setspecific(PL_thr_key, t)) |
| 3007 |
|
|
Perl_croak_nocontext("panic: pthread_setspecific"); |
| 3008 |
|
|
# endif |
| 3009 |
|
|
#endif |
| 3010 |
|
|
} |
| 3011 |
|
|
|
| 3012 |
|
|
#endif /* !PERL_GET_CONTEXT_DEFINED */ |
| 3013 |
|
|
|
| 3014 |
|
|
#ifdef USE_5005THREADS |
| 3015 |
|
|
|
| 3016 |
|
|
#ifdef FAKE_THREADS |
| 3017 |
|
|
/* Very simplistic scheduler for now */ |
| 3018 |
|
|
void |
| 3019 |
|
|
schedule(void) |
| 3020 |
|
|
{ |
| 3021 |
|
|
thr = thr->i.next_run; |
| 3022 |
|
|
} |
| 3023 |
|
|
|
| 3024 |
|
|
void |
| 3025 |
|
|
Perl_cond_init(pTHX_ perl_cond *cp) |
| 3026 |
|
|
{ |
| 3027 |
|
|
*cp = 0; |
| 3028 |
|
|
} |
| 3029 |
|
|
|
| 3030 |
|
|
void |
| 3031 |
|
|
Perl_cond_signal(pTHX_ perl_cond *cp) |
| 3032 |
|
|
{ |
| 3033 |
|
|
perl_os_thread t; |
| 3034 |
|
|
perl_cond cond = *cp; |
| 3035 |
|
|
|
| 3036 |
|
|
if (!cond) |
| 3037 |
|
|
return; |
| 3038 |
|
|
t = cond->thread; |
| 3039 |
|
|
/* Insert t in the runnable queue just ahead of us */ |
| 3040 |
|
|
t->i.next_run = thr->i.next_run; |
| 3041 |
|
|
thr->i.next_run->i.prev_run = t; |
| 3042 |
|
|
t->i.prev_run = thr; |
| 3043 |
|
|
thr->i.next_run = t; |
| 3044 |
|
|
thr->i.wait_queue = 0; |
| 3045 |
|
|
/* Remove from the wait queue */ |
| 3046 |
|
|
*cp = cond->next; |
| 3047 |
|
|
Safefree(cond); |
| 3048 |
|
|
} |
| 3049 |
|
|
|
| 3050 |
|
|
void |
| 3051 |
|
|
Perl_cond_broadcast(pTHX_ perl_cond *cp) |
| 3052 |
|
|
{ |
| 3053 |
|
|
perl_os_thread t; |
| 3054 |
|
|
perl_cond cond, cond_next; |
| 3055 |
|
|
|
| 3056 |
|
|
for (cond = *cp; cond; cond = cond_next) { |
| 3057 |
|
|
t = cond->thread; |
| 3058 |
|
|
/* Insert t in the runnable queue just ahead of us */ |
| 3059 |
|
|
t->i.next_run = thr->i.next_run; |
| 3060 |
|
|
thr->i.next_run->i.prev_run = t; |
| 3061 |
|
|
t->i.prev_run = thr; |
| 3062 |
|
|
thr->i.next_run = t; |
| 3063 |
|
|
thr->i.wait_queue = 0; |
| 3064 |
|
|
/* Remove from the wait queue */ |
| 3065 |
|
|
cond_next = cond->next; |
| 3066 |
|
|
Safefree(cond); |
| 3067 |
|
|
} |
| 3068 |
|
|
*cp = 0; |
| 3069 |
|
|
} |
| 3070 |
|
|
|
| 3071 |
|
|
void |
| 3072 |
|
|
Perl_cond_wait(pTHX_ perl_cond *cp) |
| 3073 |
|
|
{ |
| 3074 |
|
|
perl_cond cond; |
| 3075 |
|
|
|
| 3076 |
|
|
if (thr->i.next_run == thr) |
| 3077 |
|
|
Perl_croak(aTHX_ "panic: perl_cond_wait called by last runnable thread"); |
| 3078 |
|
|
|
| 3079 |
|
|
New(666, cond, 1, struct perl_wait_queue); |
| 3080 |
|
|
cond->thread = thr; |
| 3081 |
|
|
cond->next = *cp; |
| 3082 |
|
|
*cp = cond; |
| 3083 |
|
|
thr->i.wait_queue = cond; |
| 3084 |
|
|
/* Remove ourselves from runnable queue */ |
| 3085 |
|
|
thr->i.next_run->i.prev_run = thr->i.prev_run; |
| 3086 |
|
|
thr->i.prev_run->i.next_run = thr->i.next_run; |
| 3087 |
|
|
} |
| 3088 |
|
|
#endif /* FAKE_THREADS */ |
| 3089 |
|
|
|
| 3090 |
|
|
MAGIC * |
| 3091 |
|
|
Perl_condpair_magic(pTHX_ SV *sv) |
| 3092 |
|
|
{ |
| 3093 |
|
|
MAGIC *mg; |
| 3094 |
|
|
|
| 3095 |
|
|
(void)SvUPGRADE(sv, SVt_PVMG); |
| 3096 |
|
|
mg = mg_find(sv, PERL_MAGIC_mutex); |
| 3097 |
|
|
if (!mg) { |
| 3098 |
|
|
condpair_t *cp; |
| 3099 |
|
|
|
| 3100 |
|
|
New(53, cp, 1, condpair_t); |
| 3101 |
|
|
MUTEX_INIT(&cp->mutex); |
| 3102 |
|
|
COND_INIT(&cp->owner_cond); |
| 3103 |
|
|
COND_INIT(&cp->cond); |
| 3104 |
|
|
cp->owner = 0; |
| 3105 |
|
|
LOCK_CRED_MUTEX; /* XXX need separate mutex? */ |
| 3106 |
|
|
mg = mg_find(sv, PERL_MAGIC_mutex); |
| 3107 |
|
|
if (mg) { |
| 3108 |
|
|
/* someone else beat us to initialising it */ |
| 3109 |
|
|
UNLOCK_CRED_MUTEX; /* XXX need separate mutex? */ |
| 3110 |
|
|
MUTEX_DESTROY(&cp->mutex); |
| 3111 |
|
|
COND_DESTROY(&cp->owner_cond); |
| 3112 |
|
|
COND_DESTROY(&cp->cond); |
| 3113 |
|
|
Safefree(cp); |
| 3114 |
|
|
} |
| 3115 |
|
|
else { |
| 3116 |
|
|
sv_magic(sv, Nullsv, PERL_MAGIC_mutex, 0, 0); |
| 3117 |
|
|
mg = SvMAGIC(sv); |
| 3118 |
|
|
mg->mg_ptr = (char *)cp; |
| 3119 |
|
|
mg->mg_len = sizeof(cp); |
| 3120 |
|
|
UNLOCK_CRED_MUTEX; /* XXX need separate mutex? */ |
| 3121 |
|
|
DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log, |
| 3122 |
|
|
"%p: condpair_magic %p\n", thr, sv))); |
| 3123 |
|
|
} |
| 3124 |
|
|
} |
| 3125 |
|
|
return mg; |
| 3126 |
|
|
} |
| 3127 |
|
|
|
| 3128 |
|
|
SV * |
| 3129 |
|
|
Perl_sv_lock(pTHX_ SV *osv) |
| 3130 |
|
|
{ |
| 3131 |
|
|
MAGIC *mg; |
| 3132 |
|
|
SV *sv = osv; |
| 3133 |
|
|
|
| 3134 |
|
|
LOCK_SV_LOCK_MUTEX; |
| 3135 |
|
|
if (SvROK(sv)) { |
| 3136 |
|
|
sv = SvRV(sv); |
| 3137 |
|
|
} |
| 3138 |
|
|
|
| 3139 |
|
|
mg = condpair_magic(sv); |
| 3140 |
|
|
MUTEX_LOCK(MgMUTEXP(mg)); |
| 3141 |
|
|
if (MgOWNER(mg) == thr) |
| 3142 |
|
|
MUTEX_UNLOCK(MgMUTEXP(mg)); |
| 3143 |
|
|
else { |
| 3144 |
|
|
while (MgOWNER(mg)) |
| 3145 |
|
|
COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg)); |
| 3146 |
|
|
MgOWNER(mg) = thr; |
| 3147 |
|
|
DEBUG_S(PerlIO_printf(Perl_debug_log, |
| 3148 |
|
|
"0x%"UVxf": Perl_lock lock 0x%"UVxf"\n", |
| 3149 |
|
|
PTR2UV(thr), PTR2UV(sv))); |
| 3150 |
|
|
MUTEX_UNLOCK(MgMUTEXP(mg)); |
| 3151 |
|
|
SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv); |
| 3152 |
|
|
} |
| 3153 |
|
|
UNLOCK_SV_LOCK_MUTEX; |
| 3154 |
|
|
return sv; |
| 3155 |
|
|
} |
| 3156 |
|
|
|
| 3157 |
|
|
/* |
| 3158 |
|
|
* Make a new perl thread structure using t as a prototype. Some of the |
| 3159 |
|
|
* fields for the new thread are copied from the prototype thread, t, |
| 3160 |
|
|
* so t should not be running in perl at the time this function is |
| 3161 |
|
|
* called. The use by ext/Thread/Thread.xs in core perl (where t is the |
| 3162 |
|
|
* thread calling new_struct_thread) clearly satisfies this constraint. |
| 3163 |
|
|
*/ |
| 3164 |
|
|
struct perl_thread * |
| 3165 |
|
|
Perl_new_struct_thread(pTHX_ struct perl_thread *t) |
| 3166 |
|
|
{ |
| 3167 |
|
|
#if !defined(PERL_IMPLICIT_CONTEXT) |
| 3168 |
|
|
struct perl_thread *thr; |
| 3169 |
|
|
#endif |
| 3170 |
|
|
SV *sv; |
| 3171 |
|
|
SV **svp; |
| 3172 |
|
|
I32 i; |
| 3173 |
|
|
|
| 3174 |
|
|
sv = newSVpvn("", 0); |
| 3175 |
|
|
SvGROW(sv, sizeof(struct perl_thread) + 1); |
| 3176 |
|
|
SvCUR_set(sv, sizeof(struct perl_thread)); |
| 3177 |
|
|
thr = (Thread) SvPVX(sv); |
| 3178 |
|
|
#ifdef DEBUGGING |
| 3179 |
|
|
Poison(thr, 1, struct perl_thread); |
| 3180 |
|
|
PL_markstack = 0; |
| 3181 |
|
|
PL_scopestack = 0; |
| 3182 |
|
|
PL_savestack = 0; |
| 3183 |
|
|
PL_retstack = 0; |
| 3184 |
|
|
PL_dirty = 0; |
| 3185 |
|
|
PL_localizing = 0; |
| 3186 |
|
|
Zero(&PL_hv_fetch_ent_mh, 1, HE); |
| 3187 |
|
|
PL_efloatbuf = (char*)NULL; |
| 3188 |
|
|
PL_efloatsize = 0; |
| 3189 |
|
|
#else |
| 3190 |
|
|
Zero(thr, 1, struct perl_thread); |
| 3191 |
|
|
#endif |
| 3192 |
|
|
|
| 3193 |
|
|
thr->oursv = sv; |
| 3194 |
|
|
init_stacks(); |
| 3195 |
|
|
|
| 3196 |
|
|
PL_curcop = &PL_compiling; |
| 3197 |
|
|
thr->interp = t->interp; |
| 3198 |
|
|
thr->cvcache = newHV(); |
| 3199 |
|
|
thr->threadsv = newAV(); |
| 3200 |
|
|
thr->specific = newAV(); |
| 3201 |
|
|
thr->errsv = newSVpvn("", 0); |
| 3202 |
|
|
thr->flags = THRf_R_JOINABLE; |
| 3203 |
|
|
thr->thr_done = 0; |
| 3204 |
|
|
MUTEX_INIT(&thr->mutex); |
| 3205 |
|
|
|
| 3206 |
|
|
JMPENV_BOOTSTRAP; |
| 3207 |
|
|
|
| 3208 |
|
|
PL_in_eval = EVAL_NULL; /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR|EVAL_INREQUIRE) */ |
| 3209 |
|
|
PL_restartop = 0; |
| 3210 |
|
|
|
| 3211 |
|
|
PL_statname = NEWSV(66,0); |
| 3212 |
|
|
PL_errors = newSVpvn("", 0); |
| 3213 |
|
|
PL_maxscream = -1; |
| 3214 |
|
|
PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp); |
| 3215 |
|
|
PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags); |
| 3216 |
|
|
PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start); |
| 3217 |
|
|
PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string); |
| 3218 |
|
|
PL_regfree = MEMBER_TO_FPTR(Perl_pregfree); |
| 3219 |
|
|
PL_regindent = 0; |
| 3220 |
|
|
PL_reginterp_cnt = 0; |
| 3221 |
|
|
PL_lastscream = Nullsv; |
| 3222 |
|
|
PL_screamfirst = 0; |
| 3223 |
|
|
PL_screamnext = 0; |
| 3224 |
|
|
PL_reg_start_tmp = 0; |
| 3225 |
|
|
PL_reg_start_tmpl = 0; |
| 3226 |
|
|
PL_reg_poscache = Nullch; |
| 3227 |
|
|
|
| 3228 |
|
|
PL_peepp = MEMBER_TO_FPTR(Perl_peep); |
| 3229 |
|
|
|
| 3230 |
|
|
/* parent thread's data needs to be locked while we make copy */ |
| 3231 |
|
|
MUTEX_LOCK(&t->mutex); |
| 3232 |
|
|
|
| 3233 |
|
|
#ifdef PERL_FLEXIBLE_EXCEPTIONS |
| 3234 |
|
|
PL_protect = t->Tprotect; |
| 3235 |
|
|
#endif |
| 3236 |
|
|
|
| 3237 |
|
|
PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */ |
| 3238 |
|
|
PL_defstash = t->Tdefstash; /* XXX maybe these should */ |
| 3239 |
|
|
PL_curstash = t->Tcurstash; /* always be set to main? */ |
| 3240 |
|
|
|
| 3241 |
|
|
PL_tainted = t->Ttainted; |
| 3242 |
|
|
PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */ |
| 3243 |
|
|
PL_rs = newSVsv(t->Trs); |
| 3244 |
|
|
PL_last_in_gv = Nullgv; |
| 3245 |
|
|
PL_ofs_sv = t->Tofs_sv ? SvREFCNT_inc(PL_ofs_sv) : Nullsv; |
| 3246 |
|
|
PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv); |
| 3247 |
|
|
PL_chopset = t->Tchopset; |
| 3248 |
|
|
PL_bodytarget = newSVsv(t->Tbodytarget); |
| 3249 |
|
|
PL_toptarget = newSVsv(t->Ttoptarget); |
| 3250 |
|
|
if (t->Tformtarget == t->Ttoptarget) |
| 3251 |
|
|
PL_formtarget = PL_toptarget; |
| 3252 |
|
|
else |
| 3253 |
|
|
PL_formtarget = PL_bodytarget; |
| 3254 |
|
|
PL_watchaddr = 0; /* XXX */ |
| 3255 |
|
|
PL_watchok = 0; /* XXX */ |
| 3256 |
|
|
PL_comppad = 0; |
| 3257 |
|
|
PL_curpad = 0; |
| 3258 |
|
|
|
| 3259 |
|
|
/* Initialise all per-thread SVs that the template thread used */ |
| 3260 |
|
|
svp = AvARRAY(t->threadsv); |
| 3261 |
|
|
for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) { |
| 3262 |
|
|
if (*svp && *svp != &PL_sv_undef) { |
| 3263 |
|
|
SV *sv = newSVsv(*svp); |
| 3264 |
|
|
av_store(thr->threadsv, i, sv); |
| 3265 |
|
|
sv_magic(sv, 0, PERL_MAGIC_sv, &PL_threadsv_names[i], 1); |
| 3266 |
|
|
DEBUG_S(PerlIO_printf(Perl_debug_log, |
| 3267 |
|
|
"new_struct_thread: copied threadsv %"IVdf" %p->%p\n", |
| 3268 |
|
|
(IV)i, t, thr)); |
| 3269 |
|
|
} |
| 3270 |
|
|
} |
| 3271 |
|
|
thr->threadsvp = AvARRAY(thr->threadsv); |
| 3272 |
|
|
|
| 3273 |
|
|
MUTEX_LOCK(&PL_threads_mutex); |
| 3274 |
|
|
PL_nthreads++; |
| 3275 |
|
|
thr->tid = ++PL_threadnum; |
| 3276 |
|
|
thr->next = t->next; |
| 3277 |
|
|
thr->prev = t; |
| 3278 |
|
|
t->next = thr; |
| 3279 |
|
|
thr->next->prev = thr; |
| 3280 |
|
|
MUTEX_UNLOCK(&PL_threads_mutex); |
| 3281 |
|
|
|
| 3282 |
|
|
/* done copying parent's state */ |
| 3283 |
|
|
MUTEX_UNLOCK(&t->mutex); |
| 3284 |
|
|
|
| 3285 |
|
|
#ifdef HAVE_THREAD_INTERN |
| 3286 |
|
|
Perl_init_thread_intern(thr); |
| 3287 |
|
|
#endif /* HAVE_THREAD_INTERN */ |
| 3288 |
|
|
return thr; |
| 3289 |
|
|
} |
| 3290 |
|
|
#endif /* USE_5005THREADS */ |
| 3291 |
|
|
|
| 3292 |
|
|
#ifdef PERL_GLOBAL_STRUCT |
| 3293 |
|
|
struct perl_vars * |
| 3294 |
|
|
Perl_GetVars(pTHX) |
| 3295 |
|
|
{ |
| 3296 |
|
|
return &PL_Vars; |
| 3297 |
|
|
} |
| 3298 |
|
|
#endif |
| 3299 |
|
|
|
| 3300 |
|
|
char ** |
| 3301 |
|
|
Perl_get_op_names(pTHX) |
| 3302 |
|
|
{ |
| 3303 |
|
|
return PL_op_name; |
| 3304 |
|
|
} |
| 3305 |
|
|
|
| 3306 |
|
|
char ** |
| 3307 |
|
|
Perl_get_op_descs(pTHX) |
| 3308 |
|
|
{ |
| 3309 |
|
|
return PL_op_desc; |
| 3310 |
|
|
} |
| 3311 |
|
|
|
| 3312 |
|
|
char * |
| 3313 |
|
|
Perl_get_no_modify(pTHX) |
| 3314 |
|
|
{ |
| 3315 |
|
|
return (char*)PL_no_modify; |
| 3316 |
|
|
} |
| 3317 |
|
|
|
| 3318 |
|
|
U32 * |
| 3319 |
|
|
Perl_get_opargs(pTHX) |
| 3320 |
|
|
{ |
| 3321 |
|
|
return PL_opargs; |
| 3322 |
|
|
} |
| 3323 |
|
|
|
| 3324 |
|
|
PPADDR_t* |
| 3325 |
|
|
Perl_get_ppaddr(pTHX) |
| 3326 |
|
|
{ |
| 3327 |
|
|
return (PPADDR_t*)PL_ppaddr; |
| 3328 |
|
|
} |
| 3329 |
|
|
|
| 3330 |
|
|
#ifndef HAS_GETENV_LEN |
| 3331 |
|
|
char * |
| 3332 |
|
|
Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len) |
| 3333 |
|
|
{ |
| 3334 |
|
|
char *env_trans = PerlEnv_getenv(env_elem); |
| 3335 |
|
|
if (env_trans) |
| 3336 |
|
|
*len = strlen(env_trans); |
| 3337 |
|
|
return env_trans; |
| 3338 |
|
|
} |
| 3339 |
|
|
#endif |
| 3340 |
|
|
|
| 3341 |
|
|
|
| 3342 |
|
|
MGVTBL* |
| 3343 |
|
|
Perl_get_vtbl(pTHX_ int vtbl_id) |
| 3344 |
|
|
{ |
| 3345 |
|
|
MGVTBL* result = Null(MGVTBL*); |
| 3346 |
|
|
|
| 3347 |
|
|
switch(vtbl_id) { |
| 3348 |
|
|
case want_vtbl_sv: |
| 3349 |
|
|
result = &PL_vtbl_sv; |
| 3350 |
|
|
break; |
| 3351 |
|
|
case want_vtbl_env: |
| 3352 |
|
|
result = &PL_vtbl_env; |
| 3353 |
|
|
break; |
| 3354 |
|
|
case want_vtbl_envelem: |
| 3355 |
|
|
result = &PL_vtbl_envelem; |
| 3356 |
|
|
break; |
| 3357 |
|
|
case want_vtbl_sig: |
| 3358 |
|
|
result = &PL_vtbl_sig; |
| 3359 |
|
|
break; |
| 3360 |
|
|
case want_vtbl_sigelem: |
| 3361 |
|
|
result = &PL_vtbl_sigelem; |
| 3362 |
|
|
break; |
| 3363 |
|
|
case want_vtbl_pack: |
| 3364 |
|
|
result = &PL_vtbl_pack; |
| 3365 |
|
|
break; |
| 3366 |
|
|
case want_vtbl_packelem: |
| 3367 |
|
|
result = &PL_vtbl_packelem; |
| 3368 |
|
|
break; |
| 3369 |
|
|
case want_vtbl_dbline: |
| 3370 |
|
|
result = &PL_vtbl_dbline; |
| 3371 |
|
|
break; |
| 3372 |
|
|
case want_vtbl_isa: |
| 3373 |
|
|
result = &PL_vtbl_isa; |
| 3374 |
|
|
break; |
| 3375 |
|
|
case want_vtbl_isaelem: |
| 3376 |
|
|
result = &PL_vtbl_isaelem; |
| 3377 |
|
|
break; |
| 3378 |
|
|
case want_vtbl_arylen: |
| 3379 |
|
|
result = &PL_vtbl_arylen; |
| 3380 |
|
|
break; |
| 3381 |
|
|
case want_vtbl_glob: |
| 3382 |
|
|
result = &PL_vtbl_glob; |
| 3383 |
|
|
break; |
| 3384 |
|
|
case want_vtbl_mglob: |
| 3385 |
|
|
result = &PL_vtbl_mglob; |
| 3386 |
|
|
break; |
| 3387 |
|
|
case want_vtbl_nkeys: |
| 3388 |
|
|
result = &PL_vtbl_nkeys; |
| 3389 |
|
|
break; |
| 3390 |
|
|
case want_vtbl_taint: |
| 3391 |
|
|
result = &PL_vtbl_taint; |
| 3392 |
|
|
break; |
| 3393 |
|
|
case want_vtbl_substr: |
| 3394 |
|
|
result = &PL_vtbl_substr; |
| 3395 |
|
|
break; |
| 3396 |
|
|
case want_vtbl_vec: |
| 3397 |
|
|
result = &PL_vtbl_vec; |
| 3398 |
|
|
break; |
| 3399 |
|
|
case want_vtbl_pos: |
| 3400 |
|
|
result = &PL_vtbl_pos; |
| 3401 |
|
|
break; |
| 3402 |
|
|
case want_vtbl_bm: |
| 3403 |
|
|
result = &PL_vtbl_bm; |
| 3404 |
|
|
break; |
| 3405 |
|
|
case want_vtbl_fm: |
| 3406 |
|
|
result = &PL_vtbl_fm; |
| 3407 |
|
|
break; |
| 3408 |
|
|
case want_vtbl_uvar: |
| 3409 |
|
|
result = &PL_vtbl_uvar; |
| 3410 |
|
|
break; |
| 3411 |
|
|
#ifdef USE_5005THREADS |
| 3412 |
|
|
case want_vtbl_mutex: |
| 3413 |
|
|
result = &PL_vtbl_mutex; |
| 3414 |
|
|
break; |
| 3415 |
|
|
#endif |
| 3416 |
|
|
case want_vtbl_defelem: |
| 3417 |
|
|
result = &PL_vtbl_defelem; |
| 3418 |
|
|
break; |
| 3419 |
|
|
case want_vtbl_regexp: |
| 3420 |
|
|
result = &PL_vtbl_regexp; |
| 3421 |
|
|
break; |
| 3422 |
|
|
case want_vtbl_regdata: |
| 3423 |
|
|
result = &PL_vtbl_regdata; |
| 3424 |
|
|
break; |
| 3425 |
|
|
case want_vtbl_regdatum: |
| 3426 |
|
|
result = &PL_vtbl_regdatum; |
| 3427 |
|
|
break; |
| 3428 |
|
|
#ifdef USE_LOCALE_COLLATE |
| 3429 |
|
|
case want_vtbl_collxfrm: |
| 3430 |
|
|
result = &PL_vtbl_collxfrm; |
| 3431 |
|
|
break; |
| 3432 |
|
|
#endif |
| 3433 |
|
|
case want_vtbl_amagic: |
| 3434 |
|
|
result = &PL_vtbl_amagic; |
| 3435 |
|
|
break; |
| 3436 |
|
|
case want_vtbl_amagicelem: |
| 3437 |
|
|
result = &PL_vtbl_amagicelem; |
| 3438 |
|
|
break; |
| 3439 |
|
|
case want_vtbl_backref: |
| 3440 |
|
|
result = &PL_vtbl_backref; |
| 3441 |
|
|
break; |
| 3442 |
|
|
case want_vtbl_utf8: |
| 3443 |
|
|
result = &PL_vtbl_utf8; |
| 3444 |
|
|
break; |
| 3445 |
|
|
} |
| 3446 |
|
|
return result; |
| 3447 |
|
|
} |
| 3448 |
|
|
|
| 3449 |
|
|
I32 |
| 3450 |
|
|
Perl_my_fflush_all(pTHX) |
| 3451 |
|
|
{ |
| 3452 |
|
|
#if defined(USE_PERLIO) || defined(FFLUSH_NULL) || defined(USE_SFIO) |
| 3453 |
|
|
return PerlIO_flush(NULL); |
| 3454 |
|
|
#else |
| 3455 |
|
|
# if defined(HAS__FWALK) |
| 3456 |
|
|
extern int fflush(FILE *); |
| 3457 |
|
|
/* undocumented, unprototyped, but very useful BSDism */ |
| 3458 |
|
|
extern void _fwalk(int (*)(FILE *)); |
| 3459 |
|
|
_fwalk(&fflush); |
| 3460 |
|
|
return 0; |
| 3461 |
|
|
# else |
| 3462 |
|
|
# if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY) |
| 3463 |
|
|
long open_max = -1; |
| 3464 |
|
|
# ifdef PERL_FFLUSH_ALL_FOPEN_MAX |
| 3465 |
|
|
open_max = PERL_FFLUSH_ALL_FOPEN_MAX; |
| 3466 |
|
|
# else |
| 3467 |
|
|
# if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX) |
| 3468 |
|
|
open_max = sysconf(_SC_OPEN_MAX); |
| 3469 |
|
|
# else |
| 3470 |
|
|
# ifdef FOPEN_MAX |
| 3471 |
|
|
open_max = FOPEN_MAX; |
| 3472 |
|
|
# else |
| 3473 |
|
|
# ifdef OPEN_MAX |
| 3474 |
|
|
open_max = OPEN_MAX; |
| 3475 |
|
|
# else |
| 3476 |
|
|
# ifdef _NFILE |
| 3477 |
|
|
open_max = _NFILE; |
| 3478 |
|
|
# endif |
| 3479 |
|
|
# endif |
| 3480 |
|
|
# endif |
| 3481 |
|
|
# endif |
| 3482 |
|
|
# endif |
| 3483 |
|
|
if (open_max > 0) { |
| 3484 |
|
|
long i; |
| 3485 |
|
|
for (i = 0; i < open_max; i++) |
| 3486 |
|
|
if (STDIO_STREAM_ARRAY[i]._file >= 0 && |
| 3487 |
|
|
STDIO_STREAM_ARRAY[i]._file < open_max && |
| 3488 |
|
|
STDIO_STREAM_ARRAY[i]._flag) |
| 3489 |
|
|
PerlIO_flush(&STDIO_STREAM_ARRAY[i]); |
| 3490 |
|
|
return 0; |
| 3491 |
|
|
} |
| 3492 |
|
|
# endif |
| 3493 |
|
|
SETERRNO(EBADF,RMS_IFI); |
| 3494 |
|
|
return EOF; |
| 3495 |
|
|
# endif |
| 3496 |
|
|
#endif |
| 3497 |
|
|
} |
| 3498 |
|
|
|
| 3499 |
|
|
void |
| 3500 |
|
|
Perl_report_evil_fh(pTHX_ GV *gv, IO *io, I32 op) |
| 3501 |
|
|
{ |
| 3502 |
|
|
char *func = |
| 3503 |
|
|
op == OP_READLINE ? "readline" : /* "<HANDLE>" not nice */ |
| 3504 |
|
|
op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */ |
| 3505 |
|
|
PL_op_desc[op]; |
| 3506 |
|
|
char *pars = OP_IS_FILETEST(op) ? "" : "()"; |
| 3507 |
|
|
char *type = OP_IS_SOCKET(op) |
| 3508 |
|
|
|| (gv && io && IoTYPE(io) == IoTYPE_SOCKET) |
| 3509 |
|
|
? "socket" : "filehandle"; |
| 3510 |
|
|
char *name = NULL; |
| 3511 |
|
|
|
| 3512 |
|
|
if (gv && isGV(gv)) { |
| 3513 |
|
|
name = GvENAME(gv); |
| 3514 |
|
|
} |
| 3515 |
|
|
|
| 3516 |
|
|
if (op == OP_phoney_OUTPUT_ONLY || op == OP_phoney_INPUT_ONLY) { |
| 3517 |
|
|
if (ckWARN(WARN_IO)) { |
| 3518 |
|
|
const char *direction = (op == OP_phoney_INPUT_ONLY) ? "in" : "out"; |
| 3519 |
|
|
if (name && *name) |
| 3520 |
|
|
Perl_warner(aTHX_ packWARN(WARN_IO), |
| 3521 |
|
|
"Filehandle %s opened only for %sput", |
| 3522 |
|
|
name, direction); |
| 3523 |
|
|
else |
| 3524 |
|
|
Perl_warner(aTHX_ packWARN(WARN_IO), |
| 3525 |
|
|
"Filehandle opened only for %sput", direction); |
| 3526 |
|
|
} |
| 3527 |
|
|
} |
| 3528 |
|
|
else { |
| 3529 |
|
|
char *vile; |
| 3530 |
|
|
I32 warn_type; |
| 3531 |
|
|
|
| 3532 |
|
|
if (gv && io && IoTYPE(io) == IoTYPE_CLOSED) { |
| 3533 |
|
|
vile = "closed"; |
| 3534 |
|
|
warn_type = WARN_CLOSED; |
| 3535 |
|
|
} |
| 3536 |
|
|
else { |
| 3537 |
|
|
vile = "unopened"; |
| 3538 |
|
|
warn_type = WARN_UNOPENED; |
| 3539 |
|
|
} |
| 3540 |
|
|
|
| 3541 |
|
|
if (ckWARN(warn_type)) { |
| 3542 |
|
|
if (name && *name) { |
| 3543 |
|
|
Perl_warner(aTHX_ packWARN(warn_type), |
| 3544 |
|
|
"%s%s on %s %s %s", func, pars, vile, type, name); |
| 3545 |
|
|
if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP)) |
| 3546 |
|
|
Perl_warner( |
| 3547 |
|
|
aTHX_ packWARN(warn_type), |
| 3548 |
|
|
"\t(Are you trying to call %s%s on dirhandle %s?)\n", |
| 3549 |
|
|
func, pars, name |
| 3550 |
|
|
); |
| 3551 |
|
|
} |
| 3552 |
|
|
else { |
| 3553 |
|
|
Perl_warner(aTHX_ packWARN(warn_type), |
| 3554 |
|
|
"%s%s on %s %s", func, pars, vile, type); |
| 3555 |
|
|
if (gv && io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP)) |
| 3556 |
|
|
Perl_warner( |
| 3557 |
|
|
aTHX_ packWARN(warn_type), |
| 3558 |
|
|
"\t(Are you trying to call %s%s on dirhandle?)\n", |
| 3559 |
|
|
func, pars |
| 3560 |
|
|
); |
| 3561 |
|
|
} |
| 3562 |
|
|
} |
| 3563 |
|
|
} |
| 3564 |
|
|
} |
| 3565 |
|
|
|
| 3566 |
|
|
#ifdef EBCDIC |
| 3567 |
|
|
/* in ASCII order, not that it matters */ |
| 3568 |
|
|
static const char controllablechars[] = "?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"; |
| 3569 |
|
|
|
| 3570 |
|
|
int |
| 3571 |
|
|
Perl_ebcdic_control(pTHX_ int ch) |
| 3572 |
|
|
{ |
| 3573 |
|
|
if (ch > 'a') { |
| 3574 |
|
|
char *ctlp; |
| 3575 |
|
|
|
| 3576 |
|
|
if (islower(ch)) |
| 3577 |
|
|
ch = toupper(ch); |
| 3578 |
|
|
|
| 3579 |
|
|
if ((ctlp = strchr(controllablechars, ch)) == 0) { |
| 3580 |
|
|
Perl_die(aTHX_ "unrecognised control character '%c'\n", ch); |
| 3581 |
|
|
} |
| 3582 |
|
|
|
| 3583 |
|
|
if (ctlp == controllablechars) |
| 3584 |
|
|
return('\177'); /* DEL */ |
| 3585 |
|
|
else |
| 3586 |
|
|
return((unsigned char)(ctlp - controllablechars - 1)); |
| 3587 |
|
|
} else { /* Want uncontrol */ |
| 3588 |
|
|
if (ch == '\177' || ch == -1) |
| 3589 |
|
|
return('?'); |
| 3590 |
|
|
else if (ch == '\157') |
| 3591 |
|
|
return('\177'); |
| 3592 |
|
|
else if (ch == '\174') |
| 3593 |
|
|
return('\000'); |
| 3594 |
|
|
else if (ch == '^') /* '\137' in 1047, '\260' in 819 */ |
| 3595 |
|
|
return('\036'); |
| 3596 |
|
|
else if (ch == '\155') |
| 3597 |
|
|
return('\037'); |
| 3598 |
|
|
else if (0 < ch && ch < (sizeof(controllablechars) - 1)) |
| 3599 |
|
|
return(controllablechars[ch+1]); |
| 3600 |
|
|
else |
| 3601 |
|
|
Perl_die(aTHX_ "invalid control request: '\\%03o'\n", ch & 0xFF); |
| 3602 |
|
|
} |
| 3603 |
|
|
} |
| 3604 |
|
|
#endif |
| 3605 |
|
|
|
| 3606 |
|
|
/* To workaround core dumps from the uninitialised tm_zone we get the |
| 3607 |
|
|
* system to give us a reasonable struct to copy. This fix means that |
| 3608 |
|
|
* strftime uses the tm_zone and tm_gmtoff values returned by |
| 3609 |
|
|
* localtime(time()). That should give the desired result most of the |
| 3610 |
|
|
* time. But probably not always! |
| 3611 |
|
|
* |
| 3612 |
|
|
* This does not address tzname aspects of NETaa14816. |
| 3613 |
|
|
* |
| 3614 |
|
|
*/ |
| 3615 |
|
|
|
| 3616 |
|
|
#ifdef HAS_GNULIBC |
| 3617 |
|
|
# ifndef STRUCT_TM_HASZONE |
| 3618 |
|
|
# define STRUCT_TM_HASZONE |
| 3619 |
|
|
# endif |
| 3620 |
|
|
#endif |
| 3621 |
|
|
|
| 3622 |
|
|
#ifdef STRUCT_TM_HASZONE /* Backward compat */ |
| 3623 |
|
|
# ifndef HAS_TM_TM_ZONE |
| 3624 |
|
|
# define HAS_TM_TM_ZONE |
| 3625 |
|
|
# endif |
| 3626 |
|
|
#endif |
| 3627 |
|
|
|
| 3628 |
|
|
void |
| 3629 |
|
|
Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */ |
| 3630 |
|
|
{ |
| 3631 |
|
|
#ifdef HAS_TM_TM_ZONE |
| 3632 |
|
|
Time_t now; |
| 3633 |
|
|
struct tm* my_tm; |
| 3634 |
|
|
(void)time(&now); |
| 3635 |
|
|
my_tm = localtime(&now); |
| 3636 |
|
|
if (my_tm) |
| 3637 |
|
|
Copy(my_tm, ptm, 1, struct tm); |
| 3638 |
|
|
#endif |
| 3639 |
|
|
} |
| 3640 |
|
|
|
| 3641 |
|
|
/* |
| 3642 |
|
|
* mini_mktime - normalise struct tm values without the localtime() |
| 3643 |
|
|
* semantics (and overhead) of mktime(). |
| 3644 |
|
|
*/ |
| 3645 |
|
|
void |
| 3646 |
|
|
Perl_mini_mktime(pTHX_ struct tm *ptm) |
| 3647 |
|
|
{ |
| 3648 |
|
|
int yearday; |
| 3649 |
|
|
int secs; |
| 3650 |
|
|
int month, mday, year, jday; |
| 3651 |
|
|
int odd_cent, odd_year; |
| 3652 |
|
|
|
| 3653 |
|
|
#define DAYS_PER_YEAR 365 |
| 3654 |
|
|
#define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1) |
| 3655 |
|
|
#define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1) |
| 3656 |
|
|
#define DAYS_PER_QCENT (4*DAYS_PER_CENT+1) |
| 3657 |
|
|
#define SECS_PER_HOUR (60*60) |
| 3658 |
|
|
#define SECS_PER_DAY (24*SECS_PER_HOUR) |
| 3659 |
|
|
/* parentheses deliberately absent on these two, otherwise they don't work */ |
| 3660 |
|
|
#define MONTH_TO_DAYS 153/5 |
| 3661 |
|
|
#define DAYS_TO_MONTH 5/153 |
| 3662 |
|
|
/* offset to bias by March (month 4) 1st between month/mday & year finding */ |
| 3663 |
|
|
#define YEAR_ADJUST (4*MONTH_TO_DAYS+1) |
| 3664 |
|
|
/* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */ |
| 3665 |
|
|
#define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again */ |
| 3666 |
|
|
|
| 3667 |
|
|
/* |
| 3668 |
|
|
* Year/day algorithm notes: |
| 3669 |
|
|
* |
| 3670 |
|
|
* With a suitable offset for numeric value of the month, one can find |
| 3671 |
|
|
* an offset into the year by considering months to have 30.6 (153/5) days, |
| 3672 |
|
|
* using integer arithmetic (i.e., with truncation). To avoid too much |
| 3673 |
|
|
* messing about with leap days, we consider January and February to be |
| 3674 |
|
|
* the 13th and 14th month of the previous year. After that transformation, |
| 3675 |
|
|
* we need the month index we use to be high by 1 from 'normal human' usage, |
| 3676 |
|
|
* so the month index values we use run from 4 through 15. |
| 3677 |
|
|
* |
| 3678 |
|
|
* Given that, and the rules for the Gregorian calendar (leap years are those |
| 3679 |
|
|
* divisible by 4 unless also divisible by 100, when they must be divisible |
| 3680 |
|
|
* by 400 instead), we can simply calculate the number of days since some |
| 3681 |
|
|
* arbitrary 'beginning of time' by futzing with the (adjusted) year number, |
| 3682 |
|
|
* the days we derive from our month index, and adding in the day of the |
| 3683 |
|
|
* month. The value used here is not adjusted for the actual origin which |
| 3684 |
|
|
* it normally would use (1 January A.D. 1), since we're not exposing it. |
| 3685 |
|
|
* We're only building the value so we can turn around and get the |
| 3686 |
|
|
* normalised values for the year, month, day-of-month, and day-of-year. |
| 3687 |
|
|
* |
| 3688 |
|
|
* For going backward, we need to bias the value we're using so that we find |
| 3689 |
|
|
* the right year value. (Basically, we don't want the contribution of |
| 3690 |
|
|
* March 1st to the number to apply while deriving the year). Having done |
| 3691 |
|
|
* that, we 'count up' the contribution to the year number by accounting for |
| 3692 |
|
|
* full quadracenturies (400-year periods) with their extra leap days, plus |
| 3693 |
|
|
* the contribution from full centuries (to avoid counting in the lost leap |
| 3694 |
|
|
* days), plus the contribution from full quad-years (to count in the normal |
| 3695 |
|
|
* leap days), plus the leftover contribution from any non-leap years. |
| 3696 |
|
|
* At this point, if we were working with an actual leap day, we'll have 0 |
| 3697 |
|
|
* days left over. This is also true for March 1st, however. So, we have |
| 3698 |
|
|
* to special-case that result, and (earlier) keep track of the 'odd' |
| 3699 |
|
|
* century and year contributions. If we got 4 extra centuries in a qcent, |
| 3700 |
|
|
* or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb. |
| 3701 |
|
|
* Otherwise, we add back in the earlier bias we removed (the 123 from |
| 3702 |
|
|
* figuring in March 1st), find the month index (integer division by 30.6), |
| 3703 |
|
|
* and the remainder is the day-of-month. We then have to convert back to |
| 3704 |
|
|
* 'real' months (including fixing January and February from being 14/15 in |
| 3705 |
|
|
* the previous year to being in the proper year). After that, to get |
| 3706 |
|
|
* tm_yday, we work with the normalised year and get a new yearday value for |
| 3707 |
|
|
* January 1st, which we subtract from the yearday value we had earlier, |
| 3708 |
|
|
* representing the date we've re-built. This is done from January 1 |
| 3709 |
|
|
* because tm_yday is 0-origin. |
| 3710 |
|
|
* |
| 3711 |
|
|
* Since POSIX time routines are only guaranteed to work for times since the |
| 3712 |
|
|
* UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm |
| 3713 |
|
|
* applies Gregorian calendar rules even to dates before the 16th century |
| 3714 |
|
|
* doesn't bother me. Besides, you'd need cultural context for a given |
| 3715 |
|
|
* date to know whether it was Julian or Gregorian calendar, and that's |
| 3716 |
|
|
* outside the scope for this routine. Since we convert back based on the |
| 3717 |
|
|
* same rules we used to build the yearday, you'll only get strange results |
| 3718 |
|
|
* for input which needed normalising, or for the 'odd' century years which |
| 3719 |
|
|
* were leap years in the Julian calander but not in the Gregorian one. |
| 3720 |
|
|
* I can live with that. |
| 3721 |
|
|
* |
| 3722 |
|
|
* This algorithm also fails to handle years before A.D. 1 gracefully, but |
| 3723 |
|
|
* that's still outside the scope for POSIX time manipulation, so I don't |
| 3724 |
|
|
* care. |
| 3725 |
|
|
*/ |
| 3726 |
|
|
|
| 3727 |
|
|
year = 1900 + ptm->tm_year; |
| 3728 |
|
|
month = ptm->tm_mon; |
| 3729 |
|
|
mday = ptm->tm_mday; |
| 3730 |
|
|
/* allow given yday with no month & mday to dominate the result */ |
| 3731 |
|
|
if (ptm->tm_yday >= 0 && mday <= 0 && month <= 0) { |
| 3732 |
|
|
month = 0; |
| 3733 |
|
|
mday = 0; |
| 3734 |
|
|
jday = 1 + ptm->tm_yday; |
| 3735 |
|
|
} |
| 3736 |
|
|
else { |
| 3737 |
|
|
jday = 0; |
| 3738 |
|
|
} |
| 3739 |
|
|
if (month >= 2) |
| 3740 |
|
|
month+=2; |
| 3741 |
|
|
else |
| 3742 |
|
|
month+=14, year--; |
| 3743 |
|
|
yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400; |
| 3744 |
|
|
yearday += month*MONTH_TO_DAYS + mday + jday; |
| 3745 |
|
|
/* |
| 3746 |
|
|
* Note that we don't know when leap-seconds were or will be, |
| 3747 |
|
|
* so we have to trust the user if we get something which looks |
| 3748 |
|
|
* like a sensible leap-second. Wild values for seconds will |
| 3749 |
|
|
* be rationalised, however. |
| 3750 |
|
|
*/ |
| 3751 |
|
|
if ((unsigned) ptm->tm_sec <= 60) { |
| 3752 |
|
|
secs = 0; |
| 3753 |
|
|
} |
| 3754 |
|
|
else { |
| 3755 |
|
|
secs = ptm->tm_sec; |
| 3756 |
|
|
ptm->tm_sec = 0; |
| 3757 |
|
|
} |
| 3758 |
|
|
secs += 60 * ptm->tm_min; |
| 3759 |
|
|
secs += SECS_PER_HOUR * ptm->tm_hour; |
| 3760 |
|
|
if (secs < 0) { |
| 3761 |
|
|
if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) { |
| 3762 |
|
|
/* got negative remainder, but need positive time */ |
| 3763 |
|
|
/* back off an extra day to compensate */ |
| 3764 |
|
|
yearday += (secs/SECS_PER_DAY)-1; |
| 3765 |
|
|
secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1); |
| 3766 |
|
|
} |
| 3767 |
|
|
else { |
| 3768 |
|
|
yearday += (secs/SECS_PER_DAY); |
| 3769 |
|
|
secs -= SECS_PER_DAY * (secs/SECS_PER_DAY); |
| 3770 |
|
|
} |
| 3771 |
|
|
} |
| 3772 |
|
|
else if (secs >= SECS_PER_DAY) { |
| 3773 |
|
|
yearday += (secs/SECS_PER_DAY); |
| 3774 |
|
|
secs %= SECS_PER_DAY; |
| 3775 |
|
|
} |
| 3776 |
|
|
ptm->tm_hour = secs/SECS_PER_HOUR; |
| 3777 |
|
|
secs %= SECS_PER_HOUR; |
| 3778 |
|
|
ptm->tm_min = secs/60; |
| 3779 |
|
|
secs %= 60; |
| 3780 |
|
|
ptm->tm_sec += secs; |
| 3781 |
|
|
/* done with time of day effects */ |
| 3782 |
|
|
/* |
| 3783 |
|
|
* The algorithm for yearday has (so far) left it high by 428. |
| 3784 |
|
|
* To avoid mistaking a legitimate Feb 29 as Mar 1, we need to |
| 3785 |
|
|
* bias it by 123 while trying to figure out what year it |
| 3786 |
|
|
* really represents. Even with this tweak, the reverse |
| 3787 |
|
|
* translation fails for years before A.D. 0001. |
| 3788 |
|
|
* It would still fail for Feb 29, but we catch that one below. |
| 3789 |
|
|
*/ |
| 3790 |
|
|
jday = yearday; /* save for later fixup vis-a-vis Jan 1 */ |
| 3791 |
|
|
yearday -= YEAR_ADJUST; |
| 3792 |
|
|
year = (yearday / DAYS_PER_QCENT) * 400; |
| 3793 |
|
|
yearday %= DAYS_PER_QCENT; |
| 3794 |
|
|
odd_cent = yearday / DAYS_PER_CENT; |
| 3795 |
|
|
year += odd_cent * 100; |
| 3796 |
|
|
yearday %= DAYS_PER_CENT; |
| 3797 |
|
|
year += (yearday / DAYS_PER_QYEAR) * 4; |
| 3798 |
|
|
yearday %= DAYS_PER_QYEAR; |
| 3799 |
|
|
odd_year = yearday / DAYS_PER_YEAR; |
| 3800 |
|
|
year += odd_year; |
| 3801 |
|
|
yearday %= DAYS_PER_YEAR; |
| 3802 |
|
|
if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */ |
| 3803 |
|
|
month = 1; |
| 3804 |
|
|
yearday = 29; |
| 3805 |
|
|
} |
| 3806 |
|
|
else { |
| 3807 |
|
|
yearday += YEAR_ADJUST; /* recover March 1st crock */ |
| 3808 |
|
|
month = yearday*DAYS_TO_MONTH; |
| 3809 |
|
|
yearday -= month*MONTH_TO_DAYS; |
| 3810 |
|
|
/* recover other leap-year adjustment */ |
| 3811 |
|
|
if (month > 13) { |
| 3812 |
|
|
month-=14; |
| 3813 |
|
|
year++; |
| 3814 |
|
|
} |
| 3815 |
|
|
else { |
| 3816 |
|
|
month-=2; |
| 3817 |
|
|
} |
| 3818 |
|
|
} |
| 3819 |
|
|
ptm->tm_year = year - 1900; |
| 3820 |
|
|
if (yearday) { |
| 3821 |
|
|
ptm->tm_mday = yearday; |
| 3822 |
|
|
ptm->tm_mon = month; |
| 3823 |
|
|
} |
| 3824 |
|
|
else { |
| 3825 |
|
|
ptm->tm_mday = 31; |
| 3826 |
|
|
ptm->tm_mon = month - 1; |
| 3827 |
|
|
} |
| 3828 |
|
|
/* re-build yearday based on Jan 1 to get tm_yday */ |
| 3829 |
|
|
year--; |
| 3830 |
|
|
yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400; |
| 3831 |
|
|
yearday += 14*MONTH_TO_DAYS + 1; |
| 3832 |
|
|
ptm->tm_yday = jday - yearday; |
| 3833 |
|
|
/* fix tm_wday if not overridden by caller */ |
| 3834 |
|
|
if ((unsigned)ptm->tm_wday > 6) |
| 3835 |
|
|
ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7; |
| 3836 |
|
|
} |
| 3837 |
|
|
|
| 3838 |
|
|
char * |
| 3839 |
|
|
Perl_my_strftime(pTHX_ char *fmt, int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst) |
| 3840 |
|
|
{ |
| 3841 |
|
|
#ifdef HAS_STRFTIME |
| 3842 |
|
|
char *buf; |
| 3843 |
|
|
int buflen; |
| 3844 |
|
|
struct tm mytm; |
| 3845 |
|
|
int len; |
| 3846 |
|
|
|
| 3847 |
|
|
init_tm(&mytm); /* XXX workaround - see init_tm() above */ |
| 3848 |
|
|
mytm.tm_sec = sec; |
| 3849 |
|
|
mytm.tm_min = min; |
| 3850 |
|
|
mytm.tm_hour = hour; |
| 3851 |
|
|
mytm.tm_mday = mday; |
| 3852 |
|
|
mytm.tm_mon = mon; |
| 3853 |
|
|
mytm.tm_year = year; |
| 3854 |
|
|
mytm.tm_wday = wday; |
| 3855 |
|
|
mytm.tm_yday = yday; |
| 3856 |
|
|
mytm.tm_isdst = isdst; |
| 3857 |
|
|
mini_mktime(&mytm); |
| 3858 |
|
|
/* use libc to get the values for tm_gmtoff and tm_zone [perl #18238] */ |
| 3859 |
|
|
#if defined(HAS_MKTIME) && (defined(HAS_TM_TM_GMTOFF) || defined(HAS_TM_TM_ZONE)) |
| 3860 |
|
|
STMT_START { |
| 3861 |
|
|
struct tm mytm2; |
| 3862 |
|
|
mytm2 = mytm; |
| 3863 |
|
|
mktime(&mytm2); |
| 3864 |
|
|
#ifdef HAS_TM_TM_GMTOFF |
| 3865 |
|
|
mytm.tm_gmtoff = mytm2.tm_gmtoff; |
| 3866 |
|
|
#endif |
| 3867 |
|
|
#ifdef HAS_TM_TM_ZONE |
| 3868 |
|
|
mytm.tm_zone = mytm2.tm_zone; |
| 3869 |
|
|
#endif |
| 3870 |
|
|
} STMT_END; |
| 3871 |
|
|
#endif |
| 3872 |
|
|
buflen = 64; |
| 3873 |
|
|
New(0, buf, buflen, char); |
| 3874 |
|
|
len = strftime(buf, buflen, fmt, &mytm); |
| 3875 |
|
|
/* |
| 3876 |
|
|
** The following is needed to handle to the situation where |
| 3877 |
|
|
** tmpbuf overflows. Basically we want to allocate a buffer |
| 3878 |
|
|
** and try repeatedly. The reason why it is so complicated |
| 3879 |
|
|
** is that getting a return value of 0 from strftime can indicate |
| 3880 |
|
|
** one of the following: |
| 3881 |
|
|
** 1. buffer overflowed, |
| 3882 |
|
|
** 2. illegal conversion specifier, or |
| 3883 |
|
|
** 3. the format string specifies nothing to be returned(not |
| 3884 |
|
|
** an error). This could be because format is an empty string |
| 3885 |
|
|
** or it specifies %p that yields an empty string in some locale. |
| 3886 |
|
|
** If there is a better way to make it portable, go ahead by |
| 3887 |
|
|
** all means. |
| 3888 |
|
|
*/ |
| 3889 |
|
|
if ((len > 0 && len < buflen) || (len == 0 && *fmt == '\0')) |
| 3890 |
|
|
return buf; |
| 3891 |
|
|
else { |
| 3892 |
|
|
/* Possibly buf overflowed - try again with a bigger buf */ |
| 3893 |
|
|
int fmtlen = strlen(fmt); |
| 3894 |
|
|
int bufsize = fmtlen + buflen; |
| 3895 |
|
|
|
| 3896 |
|
|
New(0, buf, bufsize, char); |
| 3897 |
|
|
while (buf) { |
| 3898 |
|
|
buflen = strftime(buf, bufsize, fmt, &mytm); |
| 3899 |
|
|
if (buflen > 0 && buflen < bufsize) |
| 3900 |
|
|
break; |
| 3901 |
|
|
/* heuristic to prevent out-of-memory errors */ |
| 3902 |
|
|
if (bufsize > 100*fmtlen) { |
| 3903 |
|
|
Safefree(buf); |
| 3904 |
|
|
buf = NULL; |
| 3905 |
|
|
break; |
| 3906 |
|
|
} |
| 3907 |
|
|
bufsize *= 2; |
| 3908 |
|
|
Renew(buf, bufsize, char); |
| 3909 |
|
|
} |
| 3910 |
|
|
return buf; |
| 3911 |
|
|
} |
| 3912 |
|
|
#else |
| 3913 |
|
|
Perl_croak(aTHX_ "panic: no strftime"); |
| 3914 |
|
|
#endif |
| 3915 |
|
|
} |
| 3916 |
|
|
|
| 3917 |
|
|
|
| 3918 |
|
|
#define SV_CWD_RETURN_UNDEF \ |
| 3919 |
|
|
sv_setsv(sv, &PL_sv_undef); \ |
| 3920 |
|
|
return FALSE |
| 3921 |
|
|
|
| 3922 |
|
|
#define SV_CWD_ISDOT(dp) \ |
| 3923 |
|
|
(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ |
| 3924 |
|
|
(dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) |
| 3925 |
|
|
|
| 3926 |
|
|
/* |
| 3927 |
|
|
=head1 Miscellaneous Functions |
| 3928 |
|
|
|
| 3929 |
|
|
=for apidoc getcwd_sv |
| 3930 |
|
|
|
| 3931 |
|
|
Fill the sv with current working directory |
| 3932 |
|
|
|
| 3933 |
|
|
=cut |
| 3934 |
|
|
*/ |
| 3935 |
|
|
|
| 3936 |
|
|
/* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars. |
| 3937 |
|
|
* rewritten again by dougm, optimized for use with xs TARG, and to prefer |
| 3938 |
|
|
* getcwd(3) if available |
| 3939 |
|
|
* Comments from the orignal: |
| 3940 |
|
|
* This is a faster version of getcwd. It's also more dangerous |
| 3941 |
|
|
* because you might chdir out of a directory that you can't chdir |
| 3942 |
|
|
* back into. */ |
| 3943 |
|
|
|
| 3944 |
|
|
int |
| 3945 |
|
|
Perl_getcwd_sv(pTHX_ register SV *sv) |
| 3946 |
|
|
{ |
| 3947 |
|
|
#ifndef PERL_MICRO |
| 3948 |
|
|
|
| 3949 |
|
|
#ifndef INCOMPLETE_TAINTS |
| 3950 |
|
|
SvTAINTED_on(sv); |
| 3951 |
|
|
#endif |
| 3952 |
|
|
|
| 3953 |
|
|
#ifdef HAS_GETCWD |
| 3954 |
|
|
{ |
| 3955 |
|
|
char buf[MAXPATHLEN]; |
| 3956 |
|
|
|
| 3957 |
|
|
/* Some getcwd()s automatically allocate a buffer of the given |
| 3958 |
|
|
* size from the heap if they are given a NULL buffer pointer. |
| 3959 |
|
|
* The problem is that this behaviour is not portable. */ |
| 3960 |
|
|
if (getcwd(buf, sizeof(buf) - 1)) { |
| 3961 |
|
|
STRLEN len = strlen(buf); |
| 3962 |
|
|
sv_setpvn(sv, buf, len); |
| 3963 |
|
|
return TRUE; |
| 3964 |
|
|
} |
| 3965 |
|
|
else { |
| 3966 |
|
|
sv_setsv(sv, &PL_sv_undef); |
| 3967 |
|
|
return FALSE; |
| 3968 |
|
|
} |
| 3969 |
|
|
} |
| 3970 |
|
|
|
| 3971 |
|
|
#else |
| 3972 |
|
|
|
| 3973 |
|
|
Stat_t statbuf; |
| 3974 |
|
|
int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino; |
| 3975 |
|
|
int namelen, pathlen=0; |
| 3976 |
|
|
DIR *dir; |
| 3977 |
|
|
Direntry_t *dp; |
| 3978 |
|
|
|
| 3979 |
|
|
(void)SvUPGRADE(sv, SVt_PV); |
| 3980 |
|
|
|
| 3981 |
|
|
if (PerlLIO_lstat(".", &statbuf) < 0) { |
| 3982 |
|
|
SV_CWD_RETURN_UNDEF; |
| 3983 |
|
|
} |
| 3984 |
|
|
|
| 3985 |
|
|
orig_cdev = statbuf.st_dev; |
| 3986 |
|
|
orig_cino = statbuf.st_ino; |
| 3987 |
|
|
cdev = orig_cdev; |
| 3988 |
|
|
cino = orig_cino; |
| 3989 |
|
|
|
| 3990 |
|
|
for (;;) { |
| 3991 |
|
|
odev = cdev; |
| 3992 |
|
|
oino = cino; |
| 3993 |
|
|
|
| 3994 |
|
|
if (PerlDir_chdir("..") < 0) { |
| 3995 |
|
|
SV_CWD_RETURN_UNDEF; |
| 3996 |
|
|
} |
| 3997 |
|
|
if (PerlLIO_stat(".", &statbuf) < 0) { |
| 3998 |
|
|
SV_CWD_RETURN_UNDEF; |
| 3999 |
|
|
} |
| 4000 |
|
|
|
| 4001 |
|
|
cdev = statbuf.st_dev; |
| 4002 |
|
|
cino = statbuf.st_ino; |
| 4003 |
|
|
|
| 4004 |
|
|
if (odev == cdev && oino == cino) { |
| 4005 |
|
|
break; |
| 4006 |
|
|
} |
| 4007 |
|
|
if (!(dir = PerlDir_open("."))) { |
| 4008 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4009 |
|
|
} |
| 4010 |
|
|
|
| 4011 |
|
|
while ((dp = PerlDir_read(dir)) != NULL) { |
| 4012 |
|
|
#ifdef DIRNAMLEN |
| 4013 |
|
|
namelen = dp->d_namlen; |
| 4014 |
|
|
#else |
| 4015 |
|
|
namelen = strlen(dp->d_name); |
| 4016 |
|
|
#endif |
| 4017 |
|
|
/* skip . and .. */ |
| 4018 |
|
|
if (SV_CWD_ISDOT(dp)) { |
| 4019 |
|
|
continue; |
| 4020 |
|
|
} |
| 4021 |
|
|
|
| 4022 |
|
|
if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) { |
| 4023 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4024 |
|
|
} |
| 4025 |
|
|
|
| 4026 |
|
|
tdev = statbuf.st_dev; |
| 4027 |
|
|
tino = statbuf.st_ino; |
| 4028 |
|
|
if (tino == oino && tdev == odev) { |
| 4029 |
|
|
break; |
| 4030 |
|
|
} |
| 4031 |
|
|
} |
| 4032 |
|
|
|
| 4033 |
|
|
if (!dp) { |
| 4034 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4035 |
|
|
} |
| 4036 |
|
|
|
| 4037 |
|
|
if (pathlen + namelen + 1 >= MAXPATHLEN) { |
| 4038 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4039 |
|
|
} |
| 4040 |
|
|
|
| 4041 |
|
|
SvGROW(sv, pathlen + namelen + 1); |
| 4042 |
|
|
|
| 4043 |
|
|
if (pathlen) { |
| 4044 |
|
|
/* shift down */ |
| 4045 |
|
|
Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char); |
| 4046 |
|
|
} |
| 4047 |
|
|
|
| 4048 |
|
|
/* prepend current directory to the front */ |
| 4049 |
|
|
*SvPVX(sv) = '/'; |
| 4050 |
|
|
Move(dp->d_name, SvPVX(sv)+1, namelen, char); |
| 4051 |
|
|
pathlen += (namelen + 1); |
| 4052 |
|
|
|
| 4053 |
|
|
#ifdef VOID_CLOSEDIR |
| 4054 |
|
|
PerlDir_close(dir); |
| 4055 |
|
|
#else |
| 4056 |
|
|
if (PerlDir_close(dir) < 0) { |
| 4057 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4058 |
|
|
} |
| 4059 |
|
|
#endif |
| 4060 |
|
|
} |
| 4061 |
|
|
|
| 4062 |
|
|
if (pathlen) { |
| 4063 |
|
|
SvCUR_set(sv, pathlen); |
| 4064 |
|
|
*SvEND(sv) = '\0'; |
| 4065 |
|
|
SvPOK_only(sv); |
| 4066 |
|
|
|
| 4067 |
|
|
if (PerlDir_chdir(SvPVX(sv)) < 0) { |
| 4068 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4069 |
|
|
} |
| 4070 |
|
|
} |
| 4071 |
|
|
if (PerlLIO_stat(".", &statbuf) < 0) { |
| 4072 |
|
|
SV_CWD_RETURN_UNDEF; |
| 4073 |
|
|
} |
| 4074 |
|
|
|
| 4075 |
|
|
cdev = statbuf.st_dev; |
| 4076 |
|
|
cino = statbuf.st_ino; |
| 4077 |
|
|
|
| 4078 |
|
|
if (cdev != orig_cdev || cino != orig_cino) { |
| 4079 |
|
|
Perl_croak(aTHX_ "Unstable directory path, " |
| 4080 |
|
|
"current directory changed unexpectedly"); |
| 4081 |
|
|
} |
| 4082 |
|
|
|
| 4083 |
|
|
return TRUE; |
| 4084 |
|
|
#endif |
| 4085 |
|
|
|
| 4086 |
|
|
#else |
| 4087 |
|
|
return FALSE; |
| 4088 |
|
|
#endif |
| 4089 |
|
|
} |
| 4090 |
|
|
|
| 4091 |
|
|
#if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT) |
| 4092 |
|
|
# define EMULATE_SOCKETPAIR_UDP |
| 4093 |
|
|
#endif |
| 4094 |
|
|
|
| 4095 |
|
|
#ifdef EMULATE_SOCKETPAIR_UDP |
| 4096 |
|
|
static int |
| 4097 |
|
|
S_socketpair_udp (int fd[2]) { |
| 4098 |
|
|
dTHX; |
| 4099 |
|
|
/* Fake a datagram socketpair using UDP to localhost. */ |
| 4100 |
|
|
int sockets[2] = {-1, -1}; |
| 4101 |
|
|
struct sockaddr_in addresses[2]; |
| 4102 |
|
|
int i; |
| 4103 |
|
|
Sock_size_t size = sizeof(struct sockaddr_in); |
| 4104 |
|
|
unsigned short port; |
| 4105 |
|
|
int got; |
| 4106 |
|
|
|
| 4107 |
|
|
memset(&addresses, 0, sizeof(addresses)); |
| 4108 |
|
|
i = 1; |
| 4109 |
|
|
do { |
| 4110 |
|
|
sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET); |
| 4111 |
|
|
if (sockets[i] == -1) |
| 4112 |
|
|
goto tidy_up_and_fail; |
| 4113 |
|
|
|
| 4114 |
|
|
addresses[i].sin_family = AF_INET; |
| 4115 |
|
|
addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 4116 |
|
|
addresses[i].sin_port = 0; /* kernel choses port. */ |
| 4117 |
|
|
if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i], |
| 4118 |
|
|
sizeof(struct sockaddr_in)) == -1) |
| 4119 |
|
|
goto tidy_up_and_fail; |
| 4120 |
|
|
} while (i--); |
| 4121 |
|
|
|
| 4122 |
|
|
/* Now have 2 UDP sockets. Find out which port each is connected to, and |
| 4123 |
|
|
for each connect the other socket to it. */ |
| 4124 |
|
|
i = 1; |
| 4125 |
|
|
do { |
| 4126 |
|
|
if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i], |
| 4127 |
|
|
&size) == -1) |
| 4128 |
|
|
goto tidy_up_and_fail; |
| 4129 |
|
|
if (size != sizeof(struct sockaddr_in)) |
| 4130 |
|
|
goto abort_tidy_up_and_fail; |
| 4131 |
|
|
/* !1 is 0, !0 is 1 */ |
| 4132 |
|
|
if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i], |
| 4133 |
|
|
sizeof(struct sockaddr_in)) == -1) |
| 4134 |
|
|
goto tidy_up_and_fail; |
| 4135 |
|
|
} while (i--); |
| 4136 |
|
|
|
| 4137 |
|
|
/* Now we have 2 sockets connected to each other. I don't trust some other |
| 4138 |
|
|
process not to have already sent a packet to us (by random) so send |
| 4139 |
|
|
a packet from each to the other. */ |
| 4140 |
|
|
i = 1; |
| 4141 |
|
|
do { |
| 4142 |
|
|
/* I'm going to send my own port number. As a short. |
| 4143 |
|
|
(Who knows if someone somewhere has sin_port as a bitfield and needs |
| 4144 |
|
|
this routine. (I'm assuming crays have socketpair)) */ |
| 4145 |
|
|
port = addresses[i].sin_port; |
| 4146 |
|
|
got = PerlLIO_write(sockets[i], &port, sizeof(port)); |
| 4147 |
|
|
if (got != sizeof(port)) { |
| 4148 |
|
|
if (got == -1) |
| 4149 |
|
|
goto tidy_up_and_fail; |
| 4150 |
|
|
goto abort_tidy_up_and_fail; |
| 4151 |
|
|
} |
| 4152 |
|
|
} while (i--); |
| 4153 |
|
|
|
| 4154 |
|
|
/* Packets sent. I don't trust them to have arrived though. |
| 4155 |
|
|
(As I understand it Solaris TCP stack is multithreaded. Non-blocking |
| 4156 |
|
|
connect to localhost will use a second kernel thread. In 2.6 the |
| 4157 |
|
|
first thread running the connect() returns before the second completes, |
| 4158 |
|
|
so EINPROGRESS> In 2.7 the improved stack is faster and connect() |
| 4159 |
|
|
returns 0. Poor programs have tripped up. One poor program's authors' |
| 4160 |
|
|
had a 50-1 reverse stock split. Not sure how connected these were.) |
| 4161 |
|
|
So I don't trust someone not to have an unpredictable UDP stack. |
| 4162 |
|
|
*/ |
| 4163 |
|
|
|
| 4164 |
|
|
{ |
| 4165 |
|
|
struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */ |
| 4166 |
|
|
int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0]; |
| 4167 |
|
|
fd_set rset; |
| 4168 |
|
|
|
| 4169 |
|
|
FD_ZERO(&rset); |
| 4170 |
|
|
FD_SET(sockets[0], &rset); |
| 4171 |
|
|
FD_SET(sockets[1], &rset); |
| 4172 |
|
|
|
| 4173 |
|
|
got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor); |
| 4174 |
|
|
if (got != 2 || !FD_ISSET(sockets[0], &rset) |
| 4175 |
|
|
|| !FD_ISSET(sockets[1], &rset)) { |
| 4176 |
|
|
/* I hope this is portable and appropriate. */ |
| 4177 |
|
|
if (got == -1) |
| 4178 |
|
|
goto tidy_up_and_fail; |
| 4179 |
|
|
goto abort_tidy_up_and_fail; |
| 4180 |
|
|
} |
| 4181 |
|
|
} |
| 4182 |
|
|
|
| 4183 |
|
|
/* And the paranoia department even now doesn't trust it to have arrive |
| 4184 |
|
|
(hence MSG_DONTWAIT). Or that what arrives was sent by us. */ |
| 4185 |
|
|
{ |
| 4186 |
|
|
struct sockaddr_in readfrom; |
| 4187 |
|
|
unsigned short buffer[2]; |
| 4188 |
|
|
|
| 4189 |
|
|
i = 1; |
| 4190 |
|
|
do { |
| 4191 |
|
|
#ifdef MSG_DONTWAIT |
| 4192 |
|
|
got = PerlSock_recvfrom(sockets[i], (char *) &buffer, |
| 4193 |
|
|
sizeof(buffer), MSG_DONTWAIT, |
| 4194 |
|
|
(struct sockaddr *) &readfrom, &size); |
| 4195 |
|
|
#else |
| 4196 |
|
|
got = PerlSock_recvfrom(sockets[i], (char *) &buffer, |
| 4197 |
|
|
sizeof(buffer), 0, |
| 4198 |
|
|
(struct sockaddr *) &readfrom, &size); |
| 4199 |
|
|
#endif |
| 4200 |
|
|
|
| 4201 |
|
|
if (got == -1) |
| 4202 |
|
|
goto tidy_up_and_fail; |
| 4203 |
|
|
if (got != sizeof(port) |
| 4204 |
|
|
|| size != sizeof(struct sockaddr_in) |
| 4205 |
|
|
/* Check other socket sent us its port. */ |
| 4206 |
|
|
|| buffer[0] != (unsigned short) addresses[!i].sin_port |
| 4207 |
|
|
/* Check kernel says we got the datagram from that socket */ |
| 4208 |
|
|
|| readfrom.sin_family != addresses[!i].sin_family |
| 4209 |
|
|
|| readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr |
| 4210 |
|
|
|| readfrom.sin_port != addresses[!i].sin_port) |
| 4211 |
|
|
goto abort_tidy_up_and_fail; |
| 4212 |
|
|
} while (i--); |
| 4213 |
|
|
} |
| 4214 |
|
|
/* My caller (my_socketpair) has validated that this is non-NULL */ |
| 4215 |
|
|
fd[0] = sockets[0]; |
| 4216 |
|
|
fd[1] = sockets[1]; |
| 4217 |
|
|
/* I hereby declare this connection open. May God bless all who cross |
| 4218 |
|
|
her. */ |
| 4219 |
|
|
return 0; |
| 4220 |
|
|
|
| 4221 |
|
|
abort_tidy_up_and_fail: |
| 4222 |
|
|
errno = ECONNABORTED; |
| 4223 |
|
|
tidy_up_and_fail: |
| 4224 |
|
|
{ |
| 4225 |
|
|
int save_errno = errno; |
| 4226 |
|
|
if (sockets[0] != -1) |
| 4227 |
|
|
PerlLIO_close(sockets[0]); |
| 4228 |
|
|
if (sockets[1] != -1) |
| 4229 |
|
|
PerlLIO_close(sockets[1]); |
| 4230 |
|
|
errno = save_errno; |
| 4231 |
|
|
return -1; |
| 4232 |
|
|
} |
| 4233 |
|
|
} |
| 4234 |
|
|
#endif /* EMULATE_SOCKETPAIR_UDP */ |
| 4235 |
|
|
|
| 4236 |
|
|
#if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) |
| 4237 |
|
|
int |
| 4238 |
|
|
Perl_my_socketpair (int family, int type, int protocol, int fd[2]) { |
| 4239 |
|
|
/* Stevens says that family must be AF_LOCAL, protocol 0. |
| 4240 |
|
|
I'm going to enforce that, then ignore it, and use TCP (or UDP). */ |
| 4241 |
|
|
dTHX; |
| 4242 |
|
|
int listener = -1; |
| 4243 |
|
|
int connector = -1; |
| 4244 |
|
|
int acceptor = -1; |
| 4245 |
|
|
struct sockaddr_in listen_addr; |
| 4246 |
|
|
struct sockaddr_in connect_addr; |
| 4247 |
|
|
Sock_size_t size; |
| 4248 |
|
|
|
| 4249 |
|
|
if (protocol |
| 4250 |
|
|
#ifdef AF_UNIX |
| 4251 |
|
|
|| family != AF_UNIX |
| 4252 |
|
|
#endif |
| 4253 |
|
|
) { |
| 4254 |
|
|
errno = EAFNOSUPPORT; |
| 4255 |
|
|
return -1; |
| 4256 |
|
|
} |
| 4257 |
|
|
if (!fd) { |
| 4258 |
|
|
errno = EINVAL; |
| 4259 |
|
|
return -1; |
| 4260 |
|
|
} |
| 4261 |
|
|
|
| 4262 |
|
|
#ifdef EMULATE_SOCKETPAIR_UDP |
| 4263 |
|
|
if (type == SOCK_DGRAM) |
| 4264 |
|
|
return S_socketpair_udp(fd); |
| 4265 |
|
|
#endif |
| 4266 |
|
|
|
| 4267 |
|
|
listener = PerlSock_socket(AF_INET, type, 0); |
| 4268 |
|
|
if (listener == -1) |
| 4269 |
|
|
return -1; |
| 4270 |
|
|
memset(&listen_addr, 0, sizeof(listen_addr)); |
| 4271 |
|
|
listen_addr.sin_family = AF_INET; |
| 4272 |
|
|
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 4273 |
|
|
listen_addr.sin_port = 0; /* kernel choses port. */ |
| 4274 |
|
|
if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr, |
| 4275 |
|
|
sizeof(listen_addr)) == -1) |
| 4276 |
|
|
goto tidy_up_and_fail; |
| 4277 |
|
|
if (PerlSock_listen(listener, 1) == -1) |
| 4278 |
|
|
goto tidy_up_and_fail; |
| 4279 |
|
|
|
| 4280 |
|
|
connector = PerlSock_socket(AF_INET, type, 0); |
| 4281 |
|
|
if (connector == -1) |
| 4282 |
|
|
goto tidy_up_and_fail; |
| 4283 |
|
|
/* We want to find out the port number to connect to. */ |
| 4284 |
|
|
size = sizeof(connect_addr); |
| 4285 |
|
|
if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr, |
| 4286 |
|
|
&size) == -1) |
| 4287 |
|
|
goto tidy_up_and_fail; |
| 4288 |
|
|
if (size != sizeof(connect_addr)) |
| 4289 |
|
|
goto abort_tidy_up_and_fail; |
| 4290 |
|
|
if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr, |
| 4291 |
|
|
sizeof(connect_addr)) == -1) |
| 4292 |
|
|
goto tidy_up_and_fail; |
| 4293 |
|
|
|
| 4294 |
|
|
size = sizeof(listen_addr); |
| 4295 |
|
|
acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr, |
| 4296 |
|
|
&size); |
| 4297 |
|
|
if (acceptor == -1) |
| 4298 |
|
|
goto tidy_up_and_fail; |
| 4299 |
|
|
if (size != sizeof(listen_addr)) |
| 4300 |
|
|
goto abort_tidy_up_and_fail; |
| 4301 |
|
|
PerlLIO_close(listener); |
| 4302 |
|
|
/* Now check we are talking to ourself by matching port and host on the |
| 4303 |
|
|
two sockets. */ |
| 4304 |
|
|
if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr, |
| 4305 |
|
|
&size) == -1) |
| 4306 |
|
|
goto tidy_up_and_fail; |
| 4307 |
|
|
if (size != sizeof(connect_addr) |
| 4308 |
|
|
|| listen_addr.sin_family != connect_addr.sin_family |
| 4309 |
|
|
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr |
| 4310 |
|
|
|| listen_addr.sin_port != connect_addr.sin_port) { |
| 4311 |
|
|
goto abort_tidy_up_and_fail; |
| 4312 |
|
|
} |
| 4313 |
|
|
fd[0] = connector; |
| 4314 |
|
|
fd[1] = acceptor; |
| 4315 |
|
|
return 0; |
| 4316 |
|
|
|
| 4317 |
|
|
abort_tidy_up_and_fail: |
| 4318 |
|
|
errno = ECONNABORTED; /* I hope this is portable and appropriate. */ |
| 4319 |
|
|
tidy_up_and_fail: |
| 4320 |
|
|
{ |
| 4321 |
|
|
int save_errno = errno; |
| 4322 |
|
|
if (listener != -1) |
| 4323 |
|
|
PerlLIO_close(listener); |
| 4324 |
|
|
if (connector != -1) |
| 4325 |
|
|
PerlLIO_close(connector); |
| 4326 |
|
|
if (acceptor != -1) |
| 4327 |
|
|
PerlLIO_close(acceptor); |
| 4328 |
|
|
errno = save_errno; |
| 4329 |
|
|
return -1; |
| 4330 |
|
|
} |
| 4331 |
|
|
} |
| 4332 |
|
|
#else |
| 4333 |
|
|
/* In any case have a stub so that there's code corresponding |
| 4334 |
|
|
* to the my_socketpair in global.sym. */ |
| 4335 |
|
|
int |
| 4336 |
|
|
Perl_my_socketpair (int family, int type, int protocol, int fd[2]) { |
| 4337 |
|
|
#ifdef HAS_SOCKETPAIR |
| 4338 |
|
|
return socketpair(family, type, protocol, fd); |
| 4339 |
|
|
#else |
| 4340 |
|
|
return -1; |
| 4341 |
|
|
#endif |
| 4342 |
|
|
} |
| 4343 |
|
|
#endif |
| 4344 |
|
|
|
| 4345 |
|
|
/* |
| 4346 |
|
|
|
| 4347 |
|
|
=for apidoc sv_nosharing |
| 4348 |
|
|
|
| 4349 |
|
|
Dummy routine which "shares" an SV when there is no sharing module present. |
| 4350 |
|
|
Exists to avoid test for a NULL function pointer and because it could potentially warn under |
| 4351 |
|
|
some level of strict-ness. |
| 4352 |
|
|
|
| 4353 |
|
|
=cut |
| 4354 |
|
|
*/ |
| 4355 |
|
|
|
| 4356 |
|
|
void |
| 4357 |
|
|
Perl_sv_nosharing(pTHX_ SV *sv) |
| 4358 |
|
|
{ |
| 4359 |
|
|
} |
| 4360 |
|
|
|
| 4361 |
|
|
/* |
| 4362 |
|
|
=for apidoc sv_nolocking |
| 4363 |
|
|
|
| 4364 |
|
|
Dummy routine which "locks" an SV when there is no locking module present. |
| 4365 |
|
|
Exists to avoid test for a NULL function pointer and because it could potentially warn under |
| 4366 |
|
|
some level of strict-ness. |
| 4367 |
|
|
|
| 4368 |
|
|
=cut |
| 4369 |
|
|
*/ |
| 4370 |
|
|
|
| 4371 |
|
|
void |
| 4372 |
|
|
Perl_sv_nolocking(pTHX_ SV *sv) |
| 4373 |
|
|
{ |
| 4374 |
|
|
} |
| 4375 |
|
|
|
| 4376 |
|
|
|
| 4377 |
|
|
/* |
| 4378 |
|
|
=for apidoc sv_nounlocking |
| 4379 |
|
|
|
| 4380 |
|
|
Dummy routine which "unlocks" an SV when there is no locking module present. |
| 4381 |
|
|
Exists to avoid test for a NULL function pointer and because it could potentially warn under |
| 4382 |
|
|
some level of strict-ness. |
| 4383 |
|
|
|
| 4384 |
|
|
=cut |
| 4385 |
|
|
*/ |
| 4386 |
|
|
|
| 4387 |
|
|
void |
| 4388 |
|
|
Perl_sv_nounlocking(pTHX_ SV *sv) |
| 4389 |
|
|
{ |
| 4390 |
|
|
} |
| 4391 |
|
|
|
| 4392 |
|
|
U32 |
| 4393 |
|
|
Perl_parse_unicode_opts(pTHX_ char **popt) |
| 4394 |
|
|
{ |
| 4395 |
|
|
char *p = *popt; |
| 4396 |
|
|
U32 opt = 0; |
| 4397 |
|
|
|
| 4398 |
|
|
if (*p) { |
| 4399 |
|
|
if (isDIGIT(*p)) { |
| 4400 |
|
|
opt = (U32) atoi(p); |
| 4401 |
|
|
while (isDIGIT(*p)) p++; |
| 4402 |
|
|
if (*p && *p != '\n' && *p != '\r') |
| 4403 |
|
|
Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p); |
| 4404 |
|
|
} |
| 4405 |
|
|
else { |
| 4406 |
|
|
for (; *p; p++) { |
| 4407 |
|
|
switch (*p) { |
| 4408 |
|
|
case PERL_UNICODE_STDIN: |
| 4409 |
|
|
opt |= PERL_UNICODE_STDIN_FLAG; break; |
| 4410 |
|
|
case PERL_UNICODE_STDOUT: |
| 4411 |
|
|
opt |= PERL_UNICODE_STDOUT_FLAG; break; |
| 4412 |
|
|
case PERL_UNICODE_STDERR: |
| 4413 |
|
|
opt |= PERL_UNICODE_STDERR_FLAG; break; |
| 4414 |
|
|
case PERL_UNICODE_STD: |
| 4415 |
|
|
opt |= PERL_UNICODE_STD_FLAG; break; |
| 4416 |
|
|
case PERL_UNICODE_IN: |
| 4417 |
|
|
opt |= PERL_UNICODE_IN_FLAG; break; |
| 4418 |
|
|
case PERL_UNICODE_OUT: |
| 4419 |
|
|
opt |= PERL_UNICODE_OUT_FLAG; break; |
| 4420 |
|
|
case PERL_UNICODE_INOUT: |
| 4421 |
|
|
opt |= PERL_UNICODE_INOUT_FLAG; break; |
| 4422 |
|
|
case PERL_UNICODE_LOCALE: |
| 4423 |
|
|
opt |= PERL_UNICODE_LOCALE_FLAG; break; |
| 4424 |
|
|
case PERL_UNICODE_ARGV: |
| 4425 |
|
|
opt |= PERL_UNICODE_ARGV_FLAG; break; |
| 4426 |
|
|
default: |
| 4427 |
|
|
if (*p != '\n' && *p != '\r') |
| 4428 |
|
|
Perl_croak(aTHX_ |
| 4429 |
|
|
"Unknown Unicode option letter '%c'", *p); |
| 4430 |
|
|
} |
| 4431 |
|
|
} |
| 4432 |
|
|
} |
| 4433 |
|
|
} |
| 4434 |
|
|
else |
| 4435 |
|
|
opt = PERL_UNICODE_DEFAULT_FLAGS; |
| 4436 |
|
|
|
| 4437 |
|
|
if (opt & ~PERL_UNICODE_ALL_FLAGS) |
| 4438 |
|
|
Perl_croak(aTHX_ "Unknown Unicode option value %"UVuf, |
| 4439 |
|
|
(UV) (opt & ~PERL_UNICODE_ALL_FLAGS)); |
| 4440 |
|
|
|
| 4441 |
|
|
*popt = p; |
| 4442 |
|
|
|
| 4443 |
|
|
return opt; |
| 4444 |
|
|
} |
| 4445 |
|
|
|
| 4446 |
|
|
U32 |
| 4447 |
|
|
Perl_seed(pTHX) |
| 4448 |
|
|
{ |
| 4449 |
|
|
/* |
| 4450 |
|
|
* This is really just a quick hack which grabs various garbage |
| 4451 |
|
|
* values. It really should be a real hash algorithm which |
| 4452 |
|
|
* spreads the effect of every input bit onto every output bit, |
| 4453 |
|
|
* if someone who knows about such things would bother to write it. |
| 4454 |
|
|
* Might be a good idea to add that function to CORE as well. |
| 4455 |
|
|
* No numbers below come from careful analysis or anything here, |
| 4456 |
|
|
* except they are primes and SEED_C1 > 1E6 to get a full-width |
| 4457 |
|
|
* value from (tv_sec * SEED_C1 + tv_usec). The multipliers should |
| 4458 |
|
|
* probably be bigger too. |
| 4459 |
|
|
*/ |
| 4460 |
|
|
#if RANDBITS > 16 |
| 4461 |
|
|
# define SEED_C1 1000003 |
| 4462 |
|
|
#define SEED_C4 73819 |
| 4463 |
|
|
#else |
| 4464 |
|
|
# define SEED_C1 25747 |
| 4465 |
|
|
#define SEED_C4 20639 |
| 4466 |
|
|
#endif |
| 4467 |
|
|
#define SEED_C2 3 |
| 4468 |
|
|
#define SEED_C3 269 |
| 4469 |
|
|
#define SEED_C5 26107 |
| 4470 |
|
|
|
| 4471 |
|
|
#ifndef PERL_NO_DEV_RANDOM |
| 4472 |
|
|
int fd; |
| 4473 |
|
|
#endif |
| 4474 |
|
|
U32 u; |
| 4475 |
|
|
#ifdef VMS |
| 4476 |
|
|
# include <starlet.h> |
| 4477 |
|
|
/* when[] = (low 32 bits, high 32 bits) of time since epoch |
| 4478 |
|
|
* in 100-ns units, typically incremented ever 10 ms. */ |
| 4479 |
|
|
unsigned int when[2]; |
| 4480 |
|
|
#else |
| 4481 |
|
|
# ifdef HAS_GETTIMEOFDAY |
| 4482 |
|
|
struct timeval when; |
| 4483 |
|
|
# else |
| 4484 |
|
|
Time_t when; |
| 4485 |
|
|
# endif |
| 4486 |
|
|
#endif |
| 4487 |
|
|
|
| 4488 |
|
|
/* This test is an escape hatch, this symbol isn't set by Configure. */ |
| 4489 |
|
|
#ifndef PERL_NO_DEV_RANDOM |
| 4490 |
|
|
#ifndef PERL_RANDOM_DEVICE |
| 4491 |
|
|
/* /dev/random isn't used by default because reads from it will block |
| 4492 |
|
|
* if there isn't enough entropy available. You can compile with |
| 4493 |
|
|
* PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there |
| 4494 |
|
|
* is enough real entropy to fill the seed. */ |
| 4495 |
|
|
# define PERL_RANDOM_DEVICE "/dev/urandom" |
| 4496 |
|
|
#endif |
| 4497 |
|
|
fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0); |
| 4498 |
|
|
if (fd != -1) { |
| 4499 |
|
|
if (PerlLIO_read(fd, &u, sizeof u) != sizeof u) |
| 4500 |
|
|
u = 0; |
| 4501 |
|
|
PerlLIO_close(fd); |
| 4502 |
|
|
if (u) |
| 4503 |
|
|
return u; |
| 4504 |
|
|
} |
| 4505 |
|
|
#endif |
| 4506 |
|
|
|
| 4507 |
|
|
#ifdef VMS |
| 4508 |
|
|
_ckvmssts(sys$gettim(when)); |
| 4509 |
|
|
u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1]; |
| 4510 |
|
|
#else |
| 4511 |
|
|
# ifdef HAS_GETTIMEOFDAY |
| 4512 |
|
|
PerlProc_gettimeofday(&when,NULL); |
| 4513 |
|
|
u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec; |
| 4514 |
|
|
# else |
| 4515 |
|
|
(void)time(&when); |
| 4516 |
|
|
u = (U32)SEED_C1 * when; |
| 4517 |
|
|
# endif |
| 4518 |
|
|
#endif |
| 4519 |
|
|
u += SEED_C3 * (U32)PerlProc_getpid(); |
| 4520 |
|
|
u += SEED_C4 * (U32)PTR2UV(PL_stack_sp); |
| 4521 |
|
|
#ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */ |
| 4522 |
|
|
u += SEED_C5 * (U32)PTR2UV(&when); |
| 4523 |
|
|
#endif |
| 4524 |
|
|
return u; |
| 4525 |
|
|
} |
| 4526 |
|
|
|
| 4527 |
|
|
UV |
| 4528 |
|
|
Perl_get_hash_seed(pTHX) |
| 4529 |
|
|
{ |
| 4530 |
|
|
char *s = PerlEnv_getenv("PERL_HASH_SEED"); |
| 4531 |
|
|
UV myseed = 0; |
| 4532 |
|
|
|
| 4533 |
|
|
if (s) |
| 4534 |
|
|
while (isSPACE(*s)) s++; |
| 4535 |
|
|
if (s && isDIGIT(*s)) |
| 4536 |
|
|
myseed = (UV)Atoul(s); |
| 4537 |
|
|
else |
| 4538 |
|
|
#ifdef USE_HASH_SEED_EXPLICIT |
| 4539 |
|
|
if (s) |
| 4540 |
|
|
#endif |
| 4541 |
|
|
{ |
| 4542 |
|
|
/* Compute a random seed */ |
| 4543 |
|
|
(void)seedDrand01((Rand_seed_t)seed()); |
| 4544 |
|
|
myseed = (UV)(Drand01() * (NV)UV_MAX); |
| 4545 |
|
|
#if RANDBITS < (UVSIZE * 8) |
| 4546 |
|
|
/* Since there are not enough randbits to to reach all |
| 4547 |
|
|
* the bits of a UV, the low bits might need extra |
| 4548 |
|
|
* help. Sum in another random number that will |
| 4549 |
|
|
* fill in the low bits. */ |
| 4550 |
|
|
myseed += |
| 4551 |
|
|
(UV)(Drand01() * (NV)((1 << ((UVSIZE * 8 - RANDBITS))) - 1)); |
| 4552 |
|
|
#endif /* RANDBITS < (UVSIZE * 8) */ |
| 4553 |
|
|
if (myseed == 0) { /* Superparanoia. */ |
| 4554 |
|
|
myseed = (UV)(Drand01() * (NV)UV_MAX); /* One more chance. */ |
| 4555 |
|
|
if (myseed == 0) |
| 4556 |
|
|
Perl_croak(aTHX_ "Your random numbers are not that random"); |
| 4557 |
|
|
} |
| 4558 |
|
|
} |
| 4559 |
|
|
PL_rehash_seed_set = TRUE; |
| 4560 |
|
|
|
| 4561 |
|
|
return myseed; |
| 4562 |
|
|
} |