Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* util.c |
a687059c | 2 | * |
1129b882 NC |
3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 | * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others | |
a687059c | 5 | * |
d48672a2 LW |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 | 8 | * |
8d063cd8 | 9 | */ |
a0d0e21e LW |
10 | |
11 | /* | |
4ac71550 TC |
12 | * 'Very useful, no doubt, that was to Saruman; yet it seems that he was |
13 | * not content.' --Gandalf to Pippin | |
14 | * | |
15 | * [p.598 of _The Lord of the Rings_, III/xi: "The Palantír"] | |
a0d0e21e | 16 | */ |
8d063cd8 | 17 | |
166f8a29 DM |
18 | /* This file contains assorted utility routines. |
19 | * Which is a polite way of saying any stuff that people couldn't think of | |
20 | * a better place for. Amongst other things, it includes the warning and | |
21 | * dieing stuff, plus wrappers for malloc code. | |
22 | */ | |
23 | ||
8d063cd8 | 24 | #include "EXTERN.h" |
864dbfa3 | 25 | #define PERL_IN_UTIL_C |
8d063cd8 | 26 | #include "perl.h" |
62b28dd9 | 27 | |
64ca3a65 | 28 | #ifndef PERL_MICRO |
a687059c | 29 | #include <signal.h> |
36477c24 | 30 | #ifndef SIG_ERR |
31 | # define SIG_ERR ((Sighandler_t) -1) | |
32 | #endif | |
64ca3a65 | 33 | #endif |
36477c24 | 34 | |
172d2248 OS |
35 | #ifdef __Lynx__ |
36 | /* Missing protos on LynxOS */ | |
37 | int putenv(char *); | |
38 | #endif | |
39 | ||
ff68c719 | 40 | #ifdef I_SYS_WAIT |
41 | # include <sys/wait.h> | |
42 | #endif | |
43 | ||
868439a2 JH |
44 | #ifdef HAS_SELECT |
45 | # ifdef I_SYS_SELECT | |
46 | # include <sys/select.h> | |
47 | # endif | |
48 | #endif | |
49 | ||
8d063cd8 | 50 | #define FLUSH |
8d063cd8 | 51 | |
16cebae2 GS |
52 | #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC) |
53 | # define FD_CLOEXEC 1 /* NeXT needs this */ | |
54 | #endif | |
55 | ||
a687059c LW |
56 | /* NOTE: Do not call the next three routines directly. Use the macros |
57 | * in handy.h, so that we can easily redefine everything to do tracking of | |
58 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 59 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
60 | */ |
61 | ||
ca8d8976 NC |
62 | static char * |
63 | S_write_no_mem(pTHX) | |
64 | { | |
97aff369 | 65 | dVAR; |
ca8d8976 NC |
66 | /* Can't use PerlIO to write as it allocates memory */ |
67 | PerlLIO_write(PerlIO_fileno(Perl_error_log), | |
68 | PL_no_mem, strlen(PL_no_mem)); | |
69 | my_exit(1); | |
1f440eb2 | 70 | NORETURN_FUNCTION_END; |
ca8d8976 NC |
71 | } |
72 | ||
79a92154 | 73 | #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL) |
1f4d2d4e NC |
74 | # define ALWAYS_NEED_THX |
75 | #endif | |
76 | ||
26fa51c3 AMS |
77 | /* paranoid version of system's malloc() */ |
78 | ||
bd4080b3 | 79 | Malloc_t |
4f63d024 | 80 | Perl_safesysmalloc(MEM_SIZE size) |
8d063cd8 | 81 | { |
1f4d2d4e | 82 | #ifdef ALWAYS_NEED_THX |
54aff467 | 83 | dTHX; |
0cb20dae | 84 | #endif |
bd4080b3 | 85 | Malloc_t ptr; |
55497cff | 86 | #ifdef HAS_64K_LIMIT |
62b28dd9 | 87 | if (size > 0xffff) { |
bf49b057 | 88 | PerlIO_printf(Perl_error_log, |
16cebae2 | 89 | "Allocation too large: %lx\n", size) FLUSH; |
54aff467 | 90 | my_exit(1); |
62b28dd9 | 91 | } |
55497cff | 92 | #endif /* HAS_64K_LIMIT */ |
e8dda941 JD |
93 | #ifdef PERL_TRACK_MEMPOOL |
94 | size += sTHX; | |
95 | #endif | |
34de22dd LW |
96 | #ifdef DEBUGGING |
97 | if ((long)size < 0) | |
4f63d024 | 98 | Perl_croak_nocontext("panic: malloc"); |
34de22dd | 99 | #endif |
12ae5dfc | 100 | ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
da927450 | 101 | PERL_ALLOC_CHECK(ptr); |
97835f67 | 102 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); |
bd61b366 | 103 | if (ptr != NULL) { |
e8dda941 | 104 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
105 | struct perl_memory_debug_header *const header |
106 | = (struct perl_memory_debug_header *)ptr; | |
9a083ecf NC |
107 | #endif |
108 | ||
109 | #ifdef PERL_POISON | |
7e337ee0 | 110 | PoisonNew(((char *)ptr), size, char); |
9a083ecf | 111 | #endif |
7cb608b5 | 112 | |
9a083ecf | 113 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
114 | header->interpreter = aTHX; |
115 | /* Link us into the list. */ | |
116 | header->prev = &PL_memory_debug_header; | |
117 | header->next = PL_memory_debug_header.next; | |
118 | PL_memory_debug_header.next = header; | |
119 | header->next->prev = header; | |
cd1541b2 | 120 | # ifdef PERL_POISON |
7cb608b5 | 121 | header->size = size; |
cd1541b2 | 122 | # endif |
e8dda941 JD |
123 | ptr = (Malloc_t)((char*)ptr+sTHX); |
124 | #endif | |
8d063cd8 | 125 | return ptr; |
e8dda941 | 126 | } |
8d063cd8 | 127 | else { |
1f4d2d4e | 128 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
129 | dTHX; |
130 | #endif | |
131 | if (PL_nomemok) | |
132 | return NULL; | |
133 | else { | |
134 | return write_no_mem(); | |
135 | } | |
8d063cd8 LW |
136 | } |
137 | /*NOTREACHED*/ | |
138 | } | |
139 | ||
f2517201 | 140 | /* paranoid version of system's realloc() */ |
8d063cd8 | 141 | |
bd4080b3 | 142 | Malloc_t |
4f63d024 | 143 | Perl_safesysrealloc(Malloc_t where,MEM_SIZE size) |
8d063cd8 | 144 | { |
1f4d2d4e | 145 | #ifdef ALWAYS_NEED_THX |
54aff467 | 146 | dTHX; |
0cb20dae | 147 | #endif |
bd4080b3 | 148 | Malloc_t ptr; |
9a34ef1d | 149 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO) |
6ad3d225 | 150 | Malloc_t PerlMem_realloc(); |
ecfc5424 | 151 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 152 | |
a1d180c4 | 153 | #ifdef HAS_64K_LIMIT |
5f05dabc | 154 | if (size > 0xffff) { |
bf49b057 | 155 | PerlIO_printf(Perl_error_log, |
5f05dabc | 156 | "Reallocation too large: %lx\n", size) FLUSH; |
54aff467 | 157 | my_exit(1); |
5f05dabc | 158 | } |
55497cff | 159 | #endif /* HAS_64K_LIMIT */ |
7614df0c | 160 | if (!size) { |
f2517201 | 161 | safesysfree(where); |
7614df0c JD |
162 | return NULL; |
163 | } | |
164 | ||
378cc40b | 165 | if (!where) |
f2517201 | 166 | return safesysmalloc(size); |
e8dda941 JD |
167 | #ifdef PERL_TRACK_MEMPOOL |
168 | where = (Malloc_t)((char*)where-sTHX); | |
169 | size += sTHX; | |
7cb608b5 NC |
170 | { |
171 | struct perl_memory_debug_header *const header | |
172 | = (struct perl_memory_debug_header *)where; | |
173 | ||
174 | if (header->interpreter != aTHX) { | |
175 | Perl_croak_nocontext("panic: realloc from wrong pool"); | |
176 | } | |
177 | assert(header->next->prev == header); | |
178 | assert(header->prev->next == header); | |
cd1541b2 | 179 | # ifdef PERL_POISON |
7cb608b5 NC |
180 | if (header->size > size) { |
181 | const MEM_SIZE freed_up = header->size - size; | |
182 | char *start_of_freed = ((char *)where) + size; | |
7e337ee0 | 183 | PoisonFree(start_of_freed, freed_up, char); |
7cb608b5 NC |
184 | } |
185 | header->size = size; | |
cd1541b2 | 186 | # endif |
7cb608b5 | 187 | } |
e8dda941 | 188 | #endif |
34de22dd LW |
189 | #ifdef DEBUGGING |
190 | if ((long)size < 0) | |
4f63d024 | 191 | Perl_croak_nocontext("panic: realloc"); |
34de22dd | 192 | #endif |
12ae5dfc | 193 | ptr = (Malloc_t)PerlMem_realloc(where,size); |
da927450 | 194 | PERL_ALLOC_CHECK(ptr); |
a1d180c4 | 195 | |
4fd0a9b8 NC |
196 | /* MUST do this fixup first, before doing ANYTHING else, as anything else |
197 | might allocate memory/free/move memory, and until we do the fixup, it | |
198 | may well be chasing (and writing to) free memory. */ | |
e8dda941 | 199 | #ifdef PERL_TRACK_MEMPOOL |
4fd0a9b8 | 200 | if (ptr != NULL) { |
7cb608b5 NC |
201 | struct perl_memory_debug_header *const header |
202 | = (struct perl_memory_debug_header *)ptr; | |
203 | ||
9a083ecf NC |
204 | # ifdef PERL_POISON |
205 | if (header->size < size) { | |
206 | const MEM_SIZE fresh = size - header->size; | |
207 | char *start_of_fresh = ((char *)ptr) + size; | |
7e337ee0 | 208 | PoisonNew(start_of_fresh, fresh, char); |
9a083ecf NC |
209 | } |
210 | # endif | |
211 | ||
7cb608b5 NC |
212 | header->next->prev = header; |
213 | header->prev->next = header; | |
214 | ||
e8dda941 | 215 | ptr = (Malloc_t)((char*)ptr+sTHX); |
4fd0a9b8 | 216 | } |
e8dda941 | 217 | #endif |
4fd0a9b8 NC |
218 | |
219 | /* In particular, must do that fixup above before logging anything via | |
220 | *printf(), as it can reallocate memory, which can cause SEGVs. */ | |
221 | ||
222 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++)); | |
223 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); | |
224 | ||
225 | ||
226 | if (ptr != NULL) { | |
8d063cd8 | 227 | return ptr; |
e8dda941 | 228 | } |
8d063cd8 | 229 | else { |
1f4d2d4e | 230 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
231 | dTHX; |
232 | #endif | |
233 | if (PL_nomemok) | |
234 | return NULL; | |
235 | else { | |
236 | return write_no_mem(); | |
237 | } | |
8d063cd8 LW |
238 | } |
239 | /*NOTREACHED*/ | |
240 | } | |
241 | ||
f2517201 | 242 | /* safe version of system's free() */ |
8d063cd8 | 243 | |
54310121 | 244 | Free_t |
4f63d024 | 245 | Perl_safesysfree(Malloc_t where) |
8d063cd8 | 246 | { |
79a92154 | 247 | #ifdef ALWAYS_NEED_THX |
54aff467 | 248 | dTHX; |
97aff369 JH |
249 | #else |
250 | dVAR; | |
155aba94 | 251 | #endif |
97835f67 | 252 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++)); |
378cc40b | 253 | if (where) { |
e8dda941 JD |
254 | #ifdef PERL_TRACK_MEMPOOL |
255 | where = (Malloc_t)((char*)where-sTHX); | |
cd1541b2 | 256 | { |
7cb608b5 NC |
257 | struct perl_memory_debug_header *const header |
258 | = (struct perl_memory_debug_header *)where; | |
259 | ||
260 | if (header->interpreter != aTHX) { | |
261 | Perl_croak_nocontext("panic: free from wrong pool"); | |
262 | } | |
263 | if (!header->prev) { | |
cd1541b2 NC |
264 | Perl_croak_nocontext("panic: duplicate free"); |
265 | } | |
7cb608b5 NC |
266 | if (!(header->next) || header->next->prev != header |
267 | || header->prev->next != header) { | |
268 | Perl_croak_nocontext("panic: bad free"); | |
cd1541b2 | 269 | } |
7cb608b5 NC |
270 | /* Unlink us from the chain. */ |
271 | header->next->prev = header->prev; | |
272 | header->prev->next = header->next; | |
273 | # ifdef PERL_POISON | |
7e337ee0 | 274 | PoisonNew(where, header->size, char); |
cd1541b2 | 275 | # endif |
7cb608b5 NC |
276 | /* Trigger the duplicate free warning. */ |
277 | header->next = NULL; | |
278 | } | |
e8dda941 | 279 | #endif |
6ad3d225 | 280 | PerlMem_free(where); |
378cc40b | 281 | } |
8d063cd8 LW |
282 | } |
283 | ||
f2517201 | 284 | /* safe version of system's calloc() */ |
1050c9ca | 285 | |
bd4080b3 | 286 | Malloc_t |
4f63d024 | 287 | Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 288 | { |
1f4d2d4e | 289 | #ifdef ALWAYS_NEED_THX |
54aff467 | 290 | dTHX; |
0cb20dae | 291 | #endif |
bd4080b3 | 292 | Malloc_t ptr; |
ad7244db | 293 | MEM_SIZE total_size = 0; |
1050c9ca | 294 | |
ad7244db | 295 | /* Even though calloc() for zero bytes is strange, be robust. */ |
19a94d75 | 296 | if (size && (count <= MEM_SIZE_MAX / size)) |
ad7244db JH |
297 | total_size = size * count; |
298 | else | |
f1f66076 | 299 | Perl_croak_nocontext("%s", PL_memory_wrap); |
ad7244db | 300 | #ifdef PERL_TRACK_MEMPOOL |
19a94d75 | 301 | if (sTHX <= MEM_SIZE_MAX - (MEM_SIZE)total_size) |
ad7244db JH |
302 | total_size += sTHX; |
303 | else | |
f1f66076 | 304 | Perl_croak_nocontext("%s", PL_memory_wrap); |
ad7244db | 305 | #endif |
55497cff | 306 | #ifdef HAS_64K_LIMIT |
e1a95402 | 307 | if (total_size > 0xffff) { |
bf49b057 | 308 | PerlIO_printf(Perl_error_log, |
e1a95402 | 309 | "Allocation too large: %lx\n", total_size) FLUSH; |
54aff467 | 310 | my_exit(1); |
5f05dabc | 311 | } |
55497cff | 312 | #endif /* HAS_64K_LIMIT */ |
1050c9ca | 313 | #ifdef DEBUGGING |
314 | if ((long)size < 0 || (long)count < 0) | |
4f63d024 | 315 | Perl_croak_nocontext("panic: calloc"); |
1050c9ca | 316 | #endif |
e8dda941 | 317 | #ifdef PERL_TRACK_MEMPOOL |
e1a95402 NC |
318 | /* Have to use malloc() because we've added some space for our tracking |
319 | header. */ | |
ad7244db JH |
320 | /* malloc(0) is non-portable. */ |
321 | ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1); | |
e1a95402 NC |
322 | #else |
323 | /* Use calloc() because it might save a memset() if the memory is fresh | |
324 | and clean from the OS. */ | |
ad7244db JH |
325 | if (count && size) |
326 | ptr = (Malloc_t)PerlMem_calloc(count, size); | |
327 | else /* calloc(0) is non-portable. */ | |
328 | ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1); | |
e8dda941 | 329 | #endif |
da927450 | 330 | PERL_ALLOC_CHECK(ptr); |
e1a95402 | 331 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)total_size)); |
bd61b366 | 332 | if (ptr != NULL) { |
e8dda941 | 333 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
334 | { |
335 | struct perl_memory_debug_header *const header | |
336 | = (struct perl_memory_debug_header *)ptr; | |
337 | ||
e1a95402 | 338 | memset((void*)ptr, 0, total_size); |
7cb608b5 NC |
339 | header->interpreter = aTHX; |
340 | /* Link us into the list. */ | |
341 | header->prev = &PL_memory_debug_header; | |
342 | header->next = PL_memory_debug_header.next; | |
343 | PL_memory_debug_header.next = header; | |
344 | header->next->prev = header; | |
cd1541b2 | 345 | # ifdef PERL_POISON |
e1a95402 | 346 | header->size = total_size; |
cd1541b2 | 347 | # endif |
7cb608b5 NC |
348 | ptr = (Malloc_t)((char*)ptr+sTHX); |
349 | } | |
e8dda941 | 350 | #endif |
1050c9ca | 351 | return ptr; |
352 | } | |
0cb20dae | 353 | else { |
1f4d2d4e | 354 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
355 | dTHX; |
356 | #endif | |
357 | if (PL_nomemok) | |
358 | return NULL; | |
359 | return write_no_mem(); | |
360 | } | |
1050c9ca | 361 | } |
362 | ||
cae6d0e5 GS |
363 | /* These must be defined when not using Perl's malloc for binary |
364 | * compatibility */ | |
365 | ||
366 | #ifndef MYMALLOC | |
367 | ||
368 | Malloc_t Perl_malloc (MEM_SIZE nbytes) | |
369 | { | |
370 | dTHXs; | |
077a72a9 | 371 | return (Malloc_t)PerlMem_malloc(nbytes); |
cae6d0e5 GS |
372 | } |
373 | ||
374 | Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size) | |
375 | { | |
376 | dTHXs; | |
077a72a9 | 377 | return (Malloc_t)PerlMem_calloc(elements, size); |
cae6d0e5 GS |
378 | } |
379 | ||
380 | Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes) | |
381 | { | |
382 | dTHXs; | |
077a72a9 | 383 | return (Malloc_t)PerlMem_realloc(where, nbytes); |
cae6d0e5 GS |
384 | } |
385 | ||
386 | Free_t Perl_mfree (Malloc_t where) | |
387 | { | |
388 | dTHXs; | |
389 | PerlMem_free(where); | |
390 | } | |
391 | ||
392 | #endif | |
393 | ||
8d063cd8 LW |
394 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
395 | ||
396 | char * | |
04c9e624 | 397 | Perl_delimcpy(register char *to, register const char *toend, register const char *from, register const char *fromend, register int delim, I32 *retlen) |
8d063cd8 | 398 | { |
fc36a67e | 399 | register I32 tolen; |
35da51f7 | 400 | |
7918f24d NC |
401 | PERL_ARGS_ASSERT_DELIMCPY; |
402 | ||
fc36a67e | 403 | for (tolen = 0; from < fromend; from++, tolen++) { |
378cc40b | 404 | if (*from == '\\') { |
35da51f7 | 405 | if (from[1] != delim) { |
fc36a67e | 406 | if (to < toend) |
407 | *to++ = *from; | |
408 | tolen++; | |
fc36a67e | 409 | } |
35da51f7 | 410 | from++; |
378cc40b | 411 | } |
bedebaa5 | 412 | else if (*from == delim) |
8d063cd8 | 413 | break; |
fc36a67e | 414 | if (to < toend) |
415 | *to++ = *from; | |
8d063cd8 | 416 | } |
bedebaa5 CS |
417 | if (to < toend) |
418 | *to = '\0'; | |
fc36a67e | 419 | *retlen = tolen; |
73d840c0 | 420 | return (char *)from; |
8d063cd8 LW |
421 | } |
422 | ||
423 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 424 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
425 | |
426 | char * | |
04c9e624 | 427 | Perl_instr(register const char *big, register const char *little) |
378cc40b | 428 | { |
79072805 | 429 | register I32 first; |
378cc40b | 430 | |
7918f24d NC |
431 | PERL_ARGS_ASSERT_INSTR; |
432 | ||
a687059c | 433 | if (!little) |
08105a92 | 434 | return (char*)big; |
a687059c | 435 | first = *little++; |
378cc40b | 436 | if (!first) |
08105a92 | 437 | return (char*)big; |
378cc40b | 438 | while (*big) { |
66a1b24b | 439 | register const char *s, *x; |
378cc40b LW |
440 | if (*big++ != first) |
441 | continue; | |
442 | for (x=big,s=little; *s; /**/ ) { | |
443 | if (!*x) | |
bd61b366 | 444 | return NULL; |
4fc877ac | 445 | if (*s != *x) |
378cc40b | 446 | break; |
4fc877ac AL |
447 | else { |
448 | s++; | |
449 | x++; | |
378cc40b LW |
450 | } |
451 | } | |
452 | if (!*s) | |
08105a92 | 453 | return (char*)(big-1); |
378cc40b | 454 | } |
bd61b366 | 455 | return NULL; |
378cc40b | 456 | } |
8d063cd8 | 457 | |
a687059c LW |
458 | /* same as instr but allow embedded nulls */ |
459 | ||
460 | char * | |
04c9e624 | 461 | Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend) |
8d063cd8 | 462 | { |
7918f24d | 463 | PERL_ARGS_ASSERT_NINSTR; |
4c8626be GA |
464 | if (little >= lend) |
465 | return (char*)big; | |
466 | { | |
8ba22ff4 | 467 | const char first = *little; |
4c8626be | 468 | const char *s, *x; |
8ba22ff4 | 469 | bigend -= lend - little++; |
4c8626be GA |
470 | OUTER: |
471 | while (big <= bigend) { | |
b0ca24ee JH |
472 | if (*big++ == first) { |
473 | for (x=big,s=little; s < lend; x++,s++) { | |
474 | if (*s != *x) | |
475 | goto OUTER; | |
476 | } | |
477 | return (char*)(big-1); | |
4c8626be | 478 | } |
4c8626be | 479 | } |
378cc40b | 480 | } |
bd61b366 | 481 | return NULL; |
a687059c LW |
482 | } |
483 | ||
484 | /* reverse of the above--find last substring */ | |
485 | ||
486 | char * | |
04c9e624 | 487 | Perl_rninstr(register const char *big, const char *bigend, const char *little, const char *lend) |
a687059c | 488 | { |
08105a92 | 489 | register const char *bigbeg; |
e1ec3a88 | 490 | register const I32 first = *little; |
7452cf6a | 491 | register const char * const littleend = lend; |
a687059c | 492 | |
7918f24d NC |
493 | PERL_ARGS_ASSERT_RNINSTR; |
494 | ||
260d78c9 | 495 | if (little >= littleend) |
08105a92 | 496 | return (char*)bigend; |
a687059c LW |
497 | bigbeg = big; |
498 | big = bigend - (littleend - little++); | |
499 | while (big >= bigbeg) { | |
66a1b24b | 500 | register const char *s, *x; |
a687059c LW |
501 | if (*big-- != first) |
502 | continue; | |
503 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
4fc877ac | 504 | if (*s != *x) |
a687059c | 505 | break; |
4fc877ac AL |
506 | else { |
507 | x++; | |
508 | s++; | |
a687059c LW |
509 | } |
510 | } | |
511 | if (s >= littleend) | |
08105a92 | 512 | return (char*)(big+1); |
378cc40b | 513 | } |
bd61b366 | 514 | return NULL; |
378cc40b | 515 | } |
a687059c | 516 | |
cf93c79d IZ |
517 | /* As a space optimization, we do not compile tables for strings of length |
518 | 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are | |
519 | special-cased in fbm_instr(). | |
520 | ||
521 | If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ | |
522 | ||
954c1994 | 523 | /* |
ccfc67b7 JH |
524 | =head1 Miscellaneous Functions |
525 | ||
954c1994 GS |
526 | =for apidoc fbm_compile |
527 | ||
528 | Analyses the string in order to make fast searches on it using fbm_instr() | |
529 | -- the Boyer-Moore algorithm. | |
530 | ||
531 | =cut | |
532 | */ | |
533 | ||
378cc40b | 534 | void |
7506f9c3 | 535 | Perl_fbm_compile(pTHX_ SV *sv, U32 flags) |
378cc40b | 536 | { |
97aff369 | 537 | dVAR; |
0d46e09a | 538 | register const U8 *s; |
79072805 | 539 | register U32 i; |
0b71040e | 540 | STRLEN len; |
cb742848 | 541 | U32 rarest = 0; |
79072805 LW |
542 | U32 frequency = 256; |
543 | ||
7918f24d NC |
544 | PERL_ARGS_ASSERT_FBM_COMPILE; |
545 | ||
c517dc2b | 546 | if (flags & FBMcf_TAIL) { |
890ce7af | 547 | MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL; |
396482e1 | 548 | sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */ |
c517dc2b JH |
549 | if (mg && mg->mg_len >= 0) |
550 | mg->mg_len++; | |
551 | } | |
9cbe880b | 552 | s = (U8*)SvPV_force_mutable(sv, len); |
d1be9408 | 553 | if (len == 0) /* TAIL might be on a zero-length string. */ |
cf93c79d | 554 | return; |
cecf5685 | 555 | SvUPGRADE(sv, SVt_PVGV); |
78d0cf80 | 556 | SvIOK_off(sv); |
8eeaf79a NC |
557 | SvNOK_off(sv); |
558 | SvVALID_on(sv); | |
02128f11 | 559 | if (len > 2) { |
9cbe880b | 560 | const unsigned char *sb; |
66a1b24b | 561 | const U8 mlen = (len>255) ? 255 : (U8)len; |
890ce7af | 562 | register U8 *table; |
cf93c79d | 563 | |
d8419e03 NC |
564 | Sv_Grow(sv, len + 256 + PERL_FBM_TABLE_OFFSET); |
565 | table | |
566 | = (unsigned char*)(SvPVX_mutable(sv) + len + PERL_FBM_TABLE_OFFSET); | |
567 | s = table - 1 - PERL_FBM_TABLE_OFFSET; /* last char */ | |
7506f9c3 | 568 | memset((void*)table, mlen, 256); |
02128f11 | 569 | i = 0; |
7506f9c3 | 570 | sb = s - mlen + 1; /* first char (maybe) */ |
cf93c79d IZ |
571 | while (s >= sb) { |
572 | if (table[*s] == mlen) | |
7506f9c3 | 573 | table[*s] = (U8)i; |
cf93c79d IZ |
574 | s--, i++; |
575 | } | |
d0688fc4 NC |
576 | } else { |
577 | Sv_Grow(sv, len + PERL_FBM_TABLE_OFFSET); | |
378cc40b | 578 | } |
a0714e2c | 579 | sv_magic(sv, NULL, PERL_MAGIC_bm, NULL, 0); /* deep magic */ |
378cc40b | 580 | |
9cbe880b | 581 | s = (const unsigned char*)(SvPVX_const(sv)); /* deeper magic */ |
bbce6d69 | 582 | for (i = 0; i < len; i++) { |
22c35a8c | 583 | if (PL_freq[s[i]] < frequency) { |
bbce6d69 | 584 | rarest = i; |
22c35a8c | 585 | frequency = PL_freq[s[i]]; |
378cc40b LW |
586 | } |
587 | } | |
610460f9 | 588 | BmFLAGS(sv) = (U8)flags; |
79072805 | 589 | BmRARE(sv) = s[rarest]; |
44a10c71 | 590 | BmPREVIOUS(sv) = rarest; |
cf93c79d IZ |
591 | BmUSEFUL(sv) = 100; /* Initial value */ |
592 | if (flags & FBMcf_TAIL) | |
593 | SvTAIL_on(sv); | |
8eeaf79a NC |
594 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %lu\n", |
595 | BmRARE(sv),(unsigned long)BmPREVIOUS(sv))); | |
378cc40b LW |
596 | } |
597 | ||
cf93c79d IZ |
598 | /* If SvTAIL(littlestr), it has a fake '\n' at end. */ |
599 | /* If SvTAIL is actually due to \Z or \z, this gives false positives | |
600 | if multiline */ | |
601 | ||
954c1994 GS |
602 | /* |
603 | =for apidoc fbm_instr | |
604 | ||
605 | Returns the location of the SV in the string delimited by C<str> and | |
bd61b366 | 606 | C<strend>. It returns C<NULL> if the string can't be found. The C<sv> |
954c1994 GS |
607 | does not have to be fbm_compiled, but the search will not be as fast |
608 | then. | |
609 | ||
610 | =cut | |
611 | */ | |
612 | ||
378cc40b | 613 | char * |
864dbfa3 | 614 | Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags) |
378cc40b | 615 | { |
a687059c | 616 | register unsigned char *s; |
cf93c79d | 617 | STRLEN l; |
9cbe880b NC |
618 | register const unsigned char *little |
619 | = (const unsigned char *)SvPV_const(littlestr,l); | |
cf93c79d | 620 | register STRLEN littlelen = l; |
e1ec3a88 | 621 | register const I32 multiline = flags & FBMrf_MULTILINE; |
cf93c79d | 622 | |
7918f24d NC |
623 | PERL_ARGS_ASSERT_FBM_INSTR; |
624 | ||
eb160463 | 625 | if ((STRLEN)(bigend - big) < littlelen) { |
a1d180c4 | 626 | if ( SvTAIL(littlestr) |
eb160463 | 627 | && ((STRLEN)(bigend - big) == littlelen - 1) |
a1d180c4 | 628 | && (littlelen == 1 |
12ae5dfc | 629 | || (*big == *little && |
27da23d5 | 630 | memEQ((char *)big, (char *)little, littlelen - 1)))) |
cf93c79d | 631 | return (char*)big; |
bd61b366 | 632 | return NULL; |
cf93c79d | 633 | } |
378cc40b | 634 | |
cf93c79d | 635 | if (littlelen <= 2) { /* Special-cased */ |
cf93c79d IZ |
636 | |
637 | if (littlelen == 1) { | |
638 | if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */ | |
639 | /* Know that bigend != big. */ | |
640 | if (bigend[-1] == '\n') | |
641 | return (char *)(bigend - 1); | |
642 | return (char *) bigend; | |
643 | } | |
644 | s = big; | |
645 | while (s < bigend) { | |
646 | if (*s == *little) | |
647 | return (char *)s; | |
648 | s++; | |
649 | } | |
650 | if (SvTAIL(littlestr)) | |
651 | return (char *) bigend; | |
bd61b366 | 652 | return NULL; |
cf93c79d IZ |
653 | } |
654 | if (!littlelen) | |
655 | return (char*)big; /* Cannot be SvTAIL! */ | |
656 | ||
657 | /* littlelen is 2 */ | |
658 | if (SvTAIL(littlestr) && !multiline) { | |
659 | if (bigend[-1] == '\n' && bigend[-2] == *little) | |
660 | return (char*)bigend - 2; | |
661 | if (bigend[-1] == *little) | |
662 | return (char*)bigend - 1; | |
bd61b366 | 663 | return NULL; |
cf93c79d IZ |
664 | } |
665 | { | |
666 | /* This should be better than FBM if c1 == c2, and almost | |
667 | as good otherwise: maybe better since we do less indirection. | |
668 | And we save a lot of memory by caching no table. */ | |
66a1b24b AL |
669 | const unsigned char c1 = little[0]; |
670 | const unsigned char c2 = little[1]; | |
cf93c79d IZ |
671 | |
672 | s = big + 1; | |
673 | bigend--; | |
674 | if (c1 != c2) { | |
675 | while (s <= bigend) { | |
676 | if (s[0] == c2) { | |
677 | if (s[-1] == c1) | |
678 | return (char*)s - 1; | |
679 | s += 2; | |
680 | continue; | |
3fe6f2dc | 681 | } |
cf93c79d IZ |
682 | next_chars: |
683 | if (s[0] == c1) { | |
684 | if (s == bigend) | |
685 | goto check_1char_anchor; | |
686 | if (s[1] == c2) | |
687 | return (char*)s; | |
688 | else { | |
689 | s++; | |
690 | goto next_chars; | |
691 | } | |
692 | } | |
693 | else | |
694 | s += 2; | |
695 | } | |
696 | goto check_1char_anchor; | |
697 | } | |
698 | /* Now c1 == c2 */ | |
699 | while (s <= bigend) { | |
700 | if (s[0] == c1) { | |
701 | if (s[-1] == c1) | |
702 | return (char*)s - 1; | |
703 | if (s == bigend) | |
704 | goto check_1char_anchor; | |
705 | if (s[1] == c1) | |
706 | return (char*)s; | |
707 | s += 3; | |
02128f11 | 708 | } |
c277df42 | 709 | else |
cf93c79d | 710 | s += 2; |
c277df42 | 711 | } |
c277df42 | 712 | } |
cf93c79d IZ |
713 | check_1char_anchor: /* One char and anchor! */ |
714 | if (SvTAIL(littlestr) && (*bigend == *little)) | |
715 | return (char *)bigend; /* bigend is already decremented. */ | |
bd61b366 | 716 | return NULL; |
d48672a2 | 717 | } |
cf93c79d | 718 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
bbce6d69 | 719 | s = bigend - littlelen; |
a1d180c4 | 720 | if (s >= big && bigend[-1] == '\n' && *s == *little |
cf93c79d IZ |
721 | /* Automatically of length > 2 */ |
722 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
7506f9c3 | 723 | { |
bbce6d69 | 724 | return (char*)s; /* how sweet it is */ |
7506f9c3 GS |
725 | } |
726 | if (s[1] == *little | |
727 | && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2)) | |
728 | { | |
cf93c79d | 729 | return (char*)s + 1; /* how sweet it is */ |
7506f9c3 | 730 | } |
bd61b366 | 731 | return NULL; |
02128f11 | 732 | } |
cecf5685 | 733 | if (!SvVALID(littlestr)) { |
c4420975 | 734 | char * const b = ninstr((char*)big,(char*)bigend, |
cf93c79d IZ |
735 | (char*)little, (char*)little + littlelen); |
736 | ||
737 | if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ | |
738 | /* Chop \n from littlestr: */ | |
739 | s = bigend - littlelen + 1; | |
7506f9c3 GS |
740 | if (*s == *little |
741 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
742 | { | |
3fe6f2dc | 743 | return (char*)s; |
7506f9c3 | 744 | } |
bd61b366 | 745 | return NULL; |
a687059c | 746 | } |
cf93c79d | 747 | return b; |
a687059c | 748 | } |
a1d180c4 | 749 | |
3566a07d NC |
750 | /* Do actual FBM. */ |
751 | if (littlelen > (STRLEN)(bigend - big)) | |
752 | return NULL; | |
753 | ||
754 | { | |
d8419e03 NC |
755 | register const unsigned char * const table |
756 | = little + littlelen + PERL_FBM_TABLE_OFFSET; | |
0d46e09a | 757 | register const unsigned char *oldlittle; |
cf93c79d | 758 | |
cf93c79d IZ |
759 | --littlelen; /* Last char found by table lookup */ |
760 | ||
761 | s = big + littlelen; | |
762 | little += littlelen; /* last char */ | |
763 | oldlittle = little; | |
764 | if (s < bigend) { | |
765 | register I32 tmp; | |
766 | ||
767 | top2: | |
7506f9c3 | 768 | if ((tmp = table[*s])) { |
cf93c79d | 769 | if ((s += tmp) < bigend) |
62b28dd9 | 770 | goto top2; |
cf93c79d IZ |
771 | goto check_end; |
772 | } | |
773 | else { /* less expensive than calling strncmp() */ | |
66a1b24b | 774 | register unsigned char * const olds = s; |
cf93c79d IZ |
775 | |
776 | tmp = littlelen; | |
777 | ||
778 | while (tmp--) { | |
779 | if (*--s == *--little) | |
780 | continue; | |
cf93c79d IZ |
781 | s = olds + 1; /* here we pay the price for failure */ |
782 | little = oldlittle; | |
783 | if (s < bigend) /* fake up continue to outer loop */ | |
784 | goto top2; | |
785 | goto check_end; | |
786 | } | |
787 | return (char *)s; | |
a687059c | 788 | } |
378cc40b | 789 | } |
cf93c79d | 790 | check_end: |
c8029a41 | 791 | if ( s == bigend |
8eeaf79a | 792 | && (BmFLAGS(littlestr) & FBMcf_TAIL) |
12ae5dfc JH |
793 | && memEQ((char *)(bigend - littlelen), |
794 | (char *)(oldlittle - littlelen), littlelen) ) | |
cf93c79d | 795 | return (char*)bigend - littlelen; |
bd61b366 | 796 | return NULL; |
378cc40b | 797 | } |
378cc40b LW |
798 | } |
799 | ||
c277df42 IZ |
800 | /* start_shift, end_shift are positive quantities which give offsets |
801 | of ends of some substring of bigstr. | |
a0288114 | 802 | If "last" we want the last occurrence. |
c277df42 | 803 | old_posp is the way of communication between consequent calls if |
a1d180c4 | 804 | the next call needs to find the . |
c277df42 | 805 | The initial *old_posp should be -1. |
cf93c79d IZ |
806 | |
807 | Note that we take into account SvTAIL, so one can get extra | |
808 | optimizations if _ALL flag is set. | |
c277df42 IZ |
809 | */ |
810 | ||
cf93c79d | 811 | /* If SvTAIL is actually due to \Z or \z, this gives false positives |
26fa51c3 | 812 | if PL_multiline. In fact if !PL_multiline the authoritative answer |
cf93c79d IZ |
813 | is not supported yet. */ |
814 | ||
378cc40b | 815 | char * |
864dbfa3 | 816 | Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last) |
378cc40b | 817 | { |
97aff369 | 818 | dVAR; |
0d46e09a | 819 | register const unsigned char *big; |
79072805 LW |
820 | register I32 pos; |
821 | register I32 previous; | |
822 | register I32 first; | |
0d46e09a | 823 | register const unsigned char *little; |
c277df42 | 824 | register I32 stop_pos; |
0d46e09a | 825 | register const unsigned char *littleend; |
c277df42 | 826 | I32 found = 0; |
378cc40b | 827 | |
7918f24d NC |
828 | PERL_ARGS_ASSERT_SCREAMINSTR; |
829 | ||
cecf5685 NC |
830 | assert(SvTYPE(littlestr) == SVt_PVGV); |
831 | assert(SvVALID(littlestr)); | |
d372c834 | 832 | |
c277df42 | 833 | if (*old_posp == -1 |
3280af22 | 834 | ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0 |
cf93c79d IZ |
835 | : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) { |
836 | cant_find: | |
a1d180c4 | 837 | if ( BmRARE(littlestr) == '\n' |
85c508c3 | 838 | && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) { |
cfd0369c | 839 | little = (const unsigned char *)(SvPVX_const(littlestr)); |
cf93c79d IZ |
840 | littleend = little + SvCUR(littlestr); |
841 | first = *little++; | |
842 | goto check_tail; | |
843 | } | |
bd61b366 | 844 | return NULL; |
cf93c79d IZ |
845 | } |
846 | ||
cfd0369c | 847 | little = (const unsigned char *)(SvPVX_const(littlestr)); |
79072805 | 848 | littleend = little + SvCUR(littlestr); |
378cc40b | 849 | first = *little++; |
c277df42 | 850 | /* The value of pos we can start at: */ |
79072805 | 851 | previous = BmPREVIOUS(littlestr); |
cfd0369c | 852 | big = (const unsigned char *)(SvPVX_const(bigstr)); |
c277df42 IZ |
853 | /* The value of pos we can stop at: */ |
854 | stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous); | |
cf93c79d | 855 | if (previous + start_shift > stop_pos) { |
0fe87f7c HS |
856 | /* |
857 | stop_pos does not include SvTAIL in the count, so this check is incorrect | |
858 | (I think) - see [ID 20010618.006] and t/op/study.t. HVDS 2001/06/19 | |
859 | */ | |
860 | #if 0 | |
cf93c79d IZ |
861 | if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */ |
862 | goto check_tail; | |
0fe87f7c | 863 | #endif |
bd61b366 | 864 | return NULL; |
cf93c79d | 865 | } |
c277df42 | 866 | while (pos < previous + start_shift) { |
3280af22 | 867 | if (!(pos += PL_screamnext[pos])) |
cf93c79d | 868 | goto cant_find; |
378cc40b | 869 | } |
de3bb511 | 870 | big -= previous; |
bbce6d69 | 871 | do { |
0d46e09a | 872 | register const unsigned char *s, *x; |
ef64f398 | 873 | if (pos >= stop_pos) break; |
bbce6d69 | 874 | if (big[pos] != first) |
875 | continue; | |
876 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { | |
bbce6d69 | 877 | if (*s++ != *x++) { |
878 | s--; | |
879 | break; | |
378cc40b | 880 | } |
bbce6d69 | 881 | } |
c277df42 IZ |
882 | if (s == littleend) { |
883 | *old_posp = pos; | |
884 | if (!last) return (char *)(big+pos); | |
885 | found = 1; | |
886 | } | |
3280af22 | 887 | } while ( pos += PL_screamnext[pos] ); |
a1d180c4 | 888 | if (last && found) |
cf93c79d | 889 | return (char *)(big+(*old_posp)); |
cf93c79d IZ |
890 | check_tail: |
891 | if (!SvTAIL(littlestr) || (end_shift > 0)) | |
bd61b366 | 892 | return NULL; |
cf93c79d | 893 | /* Ignore the trailing "\n". This code is not microoptimized */ |
cfd0369c | 894 | big = (const unsigned char *)(SvPVX_const(bigstr) + SvCUR(bigstr)); |
cf93c79d IZ |
895 | stop_pos = littleend - little; /* Actual littlestr len */ |
896 | if (stop_pos == 0) | |
897 | return (char*)big; | |
898 | big -= stop_pos; | |
899 | if (*big == first | |
12ae5dfc JH |
900 | && ((stop_pos == 1) || |
901 | memEQ((char *)(big + 1), (char *)little, stop_pos - 1))) | |
cf93c79d | 902 | return (char*)big; |
bd61b366 | 903 | return NULL; |
8d063cd8 LW |
904 | } |
905 | ||
e6226b18 KW |
906 | /* |
907 | =for apidoc foldEQ | |
908 | ||
909 | Returns true if the leading len bytes of the strings s1 and s2 are the same | |
910 | case-insensitively; false otherwise. Uppercase and lowercase ASCII range bytes | |
911 | match themselves and their opposite case counterparts. Non-cased and non-ASCII | |
912 | range bytes match only themselves. | |
913 | ||
914 | =cut | |
915 | */ | |
916 | ||
917 | ||
79072805 | 918 | I32 |
e6226b18 | 919 | Perl_foldEQ(const char *s1, const char *s2, register I32 len) |
79072805 | 920 | { |
e1ec3a88 AL |
921 | register const U8 *a = (const U8 *)s1; |
922 | register const U8 *b = (const U8 *)s2; | |
96a5add6 | 923 | |
e6226b18 | 924 | PERL_ARGS_ASSERT_FOLDEQ; |
7918f24d | 925 | |
79072805 | 926 | while (len--) { |
22c35a8c | 927 | if (*a != *b && *a != PL_fold[*b]) |
e6226b18 | 928 | return 0; |
bbce6d69 | 929 | a++,b++; |
930 | } | |
e6226b18 | 931 | return 1; |
bbce6d69 | 932 | } |
933 | ||
e6226b18 KW |
934 | /* |
935 | =for apidoc foldEQ_locale | |
936 | ||
937 | Returns true if the leading len bytes of the strings s1 and s2 are the same | |
938 | case-insensitively in the current locale; false otherwise. | |
939 | ||
940 | =cut | |
941 | */ | |
942 | ||
bbce6d69 | 943 | I32 |
e6226b18 | 944 | Perl_foldEQ_locale(const char *s1, const char *s2, register I32 len) |
bbce6d69 | 945 | { |
27da23d5 | 946 | dVAR; |
e1ec3a88 AL |
947 | register const U8 *a = (const U8 *)s1; |
948 | register const U8 *b = (const U8 *)s2; | |
96a5add6 | 949 | |
e6226b18 | 950 | PERL_ARGS_ASSERT_FOLDEQ_LOCALE; |
7918f24d | 951 | |
bbce6d69 | 952 | while (len--) { |
22c35a8c | 953 | if (*a != *b && *a != PL_fold_locale[*b]) |
e6226b18 | 954 | return 0; |
bbce6d69 | 955 | a++,b++; |
79072805 | 956 | } |
e6226b18 | 957 | return 1; |
79072805 LW |
958 | } |
959 | ||
8d063cd8 LW |
960 | /* copy a string to a safe spot */ |
961 | ||
954c1994 | 962 | /* |
ccfc67b7 JH |
963 | =head1 Memory Management |
964 | ||
954c1994 GS |
965 | =for apidoc savepv |
966 | ||
61a925ed AMS |
967 | Perl's version of C<strdup()>. Returns a pointer to a newly allocated |
968 | string which is a duplicate of C<pv>. The size of the string is | |
969 | determined by C<strlen()>. The memory allocated for the new string can | |
970 | be freed with the C<Safefree()> function. | |
954c1994 GS |
971 | |
972 | =cut | |
973 | */ | |
974 | ||
8d063cd8 | 975 | char * |
efdfce31 | 976 | Perl_savepv(pTHX_ const char *pv) |
8d063cd8 | 977 | { |
96a5add6 | 978 | PERL_UNUSED_CONTEXT; |
e90e2364 | 979 | if (!pv) |
bd61b366 | 980 | return NULL; |
66a1b24b AL |
981 | else { |
982 | char *newaddr; | |
983 | const STRLEN pvlen = strlen(pv)+1; | |
10edeb5d JH |
984 | Newx(newaddr, pvlen, char); |
985 | return (char*)memcpy(newaddr, pv, pvlen); | |
66a1b24b | 986 | } |
8d063cd8 LW |
987 | } |
988 | ||
a687059c LW |
989 | /* same thing but with a known length */ |
990 | ||
954c1994 GS |
991 | /* |
992 | =for apidoc savepvn | |
993 | ||
61a925ed AMS |
994 | Perl's version of what C<strndup()> would be if it existed. Returns a |
995 | pointer to a newly allocated string which is a duplicate of the first | |
cbf82dd0 NC |
996 | C<len> bytes from C<pv>, plus a trailing NUL byte. The memory allocated for |
997 | the new string can be freed with the C<Safefree()> function. | |
954c1994 GS |
998 | |
999 | =cut | |
1000 | */ | |
1001 | ||
a687059c | 1002 | char * |
efdfce31 | 1003 | Perl_savepvn(pTHX_ const char *pv, register I32 len) |
a687059c LW |
1004 | { |
1005 | register char *newaddr; | |
96a5add6 | 1006 | PERL_UNUSED_CONTEXT; |
a687059c | 1007 | |
a02a5408 | 1008 | Newx(newaddr,len+1,char); |
92110913 | 1009 | /* Give a meaning to NULL pointer mainly for the use in sv_magic() */ |
efdfce31 | 1010 | if (pv) { |
e90e2364 NC |
1011 | /* might not be null terminated */ |
1012 | newaddr[len] = '\0'; | |
07409e01 | 1013 | return (char *) CopyD(pv,newaddr,len,char); |
92110913 NIS |
1014 | } |
1015 | else { | |
07409e01 | 1016 | return (char *) ZeroD(newaddr,len+1,char); |
92110913 | 1017 | } |
a687059c LW |
1018 | } |
1019 | ||
05ec9bb3 NIS |
1020 | /* |
1021 | =for apidoc savesharedpv | |
1022 | ||
61a925ed AMS |
1023 | A version of C<savepv()> which allocates the duplicate string in memory |
1024 | which is shared between threads. | |
05ec9bb3 NIS |
1025 | |
1026 | =cut | |
1027 | */ | |
1028 | char * | |
efdfce31 | 1029 | Perl_savesharedpv(pTHX_ const char *pv) |
05ec9bb3 | 1030 | { |
e90e2364 | 1031 | register char *newaddr; |
490a0e98 | 1032 | STRLEN pvlen; |
e90e2364 | 1033 | if (!pv) |
bd61b366 | 1034 | return NULL; |
e90e2364 | 1035 | |
490a0e98 NC |
1036 | pvlen = strlen(pv)+1; |
1037 | newaddr = (char*)PerlMemShared_malloc(pvlen); | |
e90e2364 | 1038 | if (!newaddr) { |
0bd48802 | 1039 | return write_no_mem(); |
05ec9bb3 | 1040 | } |
10edeb5d | 1041 | return (char*)memcpy(newaddr, pv, pvlen); |
05ec9bb3 NIS |
1042 | } |
1043 | ||
2e0de35c | 1044 | /* |
d9095cec NC |
1045 | =for apidoc savesharedpvn |
1046 | ||
1047 | A version of C<savepvn()> which allocates the duplicate string in memory | |
1048 | which is shared between threads. (With the specific difference that a NULL | |
1049 | pointer is not acceptable) | |
1050 | ||
1051 | =cut | |
1052 | */ | |
1053 | char * | |
1054 | Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len) | |
1055 | { | |
1056 | char *const newaddr = (char*)PerlMemShared_malloc(len + 1); | |
7918f24d NC |
1057 | |
1058 | PERL_ARGS_ASSERT_SAVESHAREDPVN; | |
1059 | ||
d9095cec NC |
1060 | if (!newaddr) { |
1061 | return write_no_mem(); | |
1062 | } | |
1063 | newaddr[len] = '\0'; | |
1064 | return (char*)memcpy(newaddr, pv, len); | |
1065 | } | |
1066 | ||
1067 | /* | |
2e0de35c NC |
1068 | =for apidoc savesvpv |
1069 | ||
6832267f | 1070 | A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from |
2e0de35c NC |
1071 | the passed in SV using C<SvPV()> |
1072 | ||
1073 | =cut | |
1074 | */ | |
1075 | ||
1076 | char * | |
1077 | Perl_savesvpv(pTHX_ SV *sv) | |
1078 | { | |
1079 | STRLEN len; | |
7452cf6a | 1080 | const char * const pv = SvPV_const(sv, len); |
2e0de35c NC |
1081 | register char *newaddr; |
1082 | ||
7918f24d NC |
1083 | PERL_ARGS_ASSERT_SAVESVPV; |
1084 | ||
26866f99 | 1085 | ++len; |
a02a5408 | 1086 | Newx(newaddr,len,char); |
07409e01 | 1087 | return (char *) CopyD(pv,newaddr,len,char); |
2e0de35c | 1088 | } |
05ec9bb3 NIS |
1089 | |
1090 | ||
cea2e8a9 | 1091 | /* the SV for Perl_form() and mess() is not kept in an arena */ |
fc36a67e | 1092 | |
76e3520e | 1093 | STATIC SV * |
cea2e8a9 | 1094 | S_mess_alloc(pTHX) |
fc36a67e | 1095 | { |
97aff369 | 1096 | dVAR; |
fc36a67e | 1097 | SV *sv; |
1098 | XPVMG *any; | |
1099 | ||
e72dc28c | 1100 | if (!PL_dirty) |
84bafc02 | 1101 | return newSVpvs_flags("", SVs_TEMP); |
e72dc28c | 1102 | |
0372dbb6 GS |
1103 | if (PL_mess_sv) |
1104 | return PL_mess_sv; | |
1105 | ||
fc36a67e | 1106 | /* Create as PVMG now, to avoid any upgrading later */ |
a02a5408 JC |
1107 | Newx(sv, 1, SV); |
1108 | Newxz(any, 1, XPVMG); | |
fc36a67e | 1109 | SvFLAGS(sv) = SVt_PVMG; |
1110 | SvANY(sv) = (void*)any; | |
6136c704 | 1111 | SvPV_set(sv, NULL); |
fc36a67e | 1112 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ |
e72dc28c | 1113 | PL_mess_sv = sv; |
fc36a67e | 1114 | return sv; |
1115 | } | |
1116 | ||
c5be433b | 1117 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1118 | char * |
1119 | Perl_form_nocontext(const char* pat, ...) | |
1120 | { | |
1121 | dTHX; | |
c5be433b | 1122 | char *retval; |
cea2e8a9 | 1123 | va_list args; |
7918f24d | 1124 | PERL_ARGS_ASSERT_FORM_NOCONTEXT; |
cea2e8a9 | 1125 | va_start(args, pat); |
c5be433b | 1126 | retval = vform(pat, &args); |
cea2e8a9 | 1127 | va_end(args); |
c5be433b | 1128 | return retval; |
cea2e8a9 | 1129 | } |
c5be433b | 1130 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1131 | |
7c9e965c | 1132 | /* |
ccfc67b7 | 1133 | =head1 Miscellaneous Functions |
7c9e965c JP |
1134 | =for apidoc form |
1135 | ||
1136 | Takes a sprintf-style format pattern and conventional | |
1137 | (non-SV) arguments and returns the formatted string. | |
1138 | ||
1139 | (char *) Perl_form(pTHX_ const char* pat, ...) | |
1140 | ||
1141 | can be used any place a string (char *) is required: | |
1142 | ||
1143 | char * s = Perl_form("%d.%d",major,minor); | |
1144 | ||
1145 | Uses a single private buffer so if you want to format several strings you | |
1146 | must explicitly copy the earlier strings away (and free the copies when you | |
1147 | are done). | |
1148 | ||
1149 | =cut | |
1150 | */ | |
1151 | ||
8990e307 | 1152 | char * |
864dbfa3 | 1153 | Perl_form(pTHX_ const char* pat, ...) |
8990e307 | 1154 | { |
c5be433b | 1155 | char *retval; |
46fc3d4c | 1156 | va_list args; |
7918f24d | 1157 | PERL_ARGS_ASSERT_FORM; |
46fc3d4c | 1158 | va_start(args, pat); |
c5be433b | 1159 | retval = vform(pat, &args); |
46fc3d4c | 1160 | va_end(args); |
c5be433b GS |
1161 | return retval; |
1162 | } | |
1163 | ||
1164 | char * | |
1165 | Perl_vform(pTHX_ const char *pat, va_list *args) | |
1166 | { | |
2d03de9c | 1167 | SV * const sv = mess_alloc(); |
7918f24d | 1168 | PERL_ARGS_ASSERT_VFORM; |
4608196e | 1169 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
e72dc28c | 1170 | return SvPVX(sv); |
46fc3d4c | 1171 | } |
a687059c | 1172 | |
c5df3096 Z |
1173 | /* |
1174 | =for apidoc Am|SV *|mess|const char *pat|... | |
1175 | ||
1176 | Take a sprintf-style format pattern and argument list. These are used to | |
1177 | generate a string message. If the message does not end with a newline, | |
1178 | then it will be extended with some indication of the current location | |
1179 | in the code, as described for L</mess_sv>. | |
1180 | ||
1181 | Normally, the resulting message is returned in a new mortal SV. | |
1182 | During global destruction a single SV may be shared between uses of | |
1183 | this function. | |
1184 | ||
1185 | =cut | |
1186 | */ | |
1187 | ||
5a844595 GS |
1188 | #if defined(PERL_IMPLICIT_CONTEXT) |
1189 | SV * | |
1190 | Perl_mess_nocontext(const char *pat, ...) | |
1191 | { | |
1192 | dTHX; | |
1193 | SV *retval; | |
1194 | va_list args; | |
7918f24d | 1195 | PERL_ARGS_ASSERT_MESS_NOCONTEXT; |
5a844595 GS |
1196 | va_start(args, pat); |
1197 | retval = vmess(pat, &args); | |
1198 | va_end(args); | |
1199 | return retval; | |
1200 | } | |
1201 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1202 | ||
06bf62c7 | 1203 | SV * |
5a844595 GS |
1204 | Perl_mess(pTHX_ const char *pat, ...) |
1205 | { | |
1206 | SV *retval; | |
1207 | va_list args; | |
7918f24d | 1208 | PERL_ARGS_ASSERT_MESS; |
5a844595 GS |
1209 | va_start(args, pat); |
1210 | retval = vmess(pat, &args); | |
1211 | va_end(args); | |
1212 | return retval; | |
1213 | } | |
1214 | ||
5f66b61c AL |
1215 | STATIC const COP* |
1216 | S_closest_cop(pTHX_ const COP *cop, const OP *o) | |
ae7d165c | 1217 | { |
97aff369 | 1218 | dVAR; |
ae7d165c PJ |
1219 | /* Look for PL_op starting from o. cop is the last COP we've seen. */ |
1220 | ||
7918f24d NC |
1221 | PERL_ARGS_ASSERT_CLOSEST_COP; |
1222 | ||
fabdb6c0 AL |
1223 | if (!o || o == PL_op) |
1224 | return cop; | |
ae7d165c PJ |
1225 | |
1226 | if (o->op_flags & OPf_KIDS) { | |
5f66b61c | 1227 | const OP *kid; |
fabdb6c0 | 1228 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
5f66b61c | 1229 | const COP *new_cop; |
ae7d165c PJ |
1230 | |
1231 | /* If the OP_NEXTSTATE has been optimised away we can still use it | |
1232 | * the get the file and line number. */ | |
1233 | ||
1234 | if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) | |
5f66b61c | 1235 | cop = (const COP *)kid; |
ae7d165c PJ |
1236 | |
1237 | /* Keep searching, and return when we've found something. */ | |
1238 | ||
1239 | new_cop = closest_cop(cop, kid); | |
fabdb6c0 AL |
1240 | if (new_cop) |
1241 | return new_cop; | |
ae7d165c PJ |
1242 | } |
1243 | } | |
1244 | ||
1245 | /* Nothing found. */ | |
1246 | ||
5f66b61c | 1247 | return NULL; |
ae7d165c PJ |
1248 | } |
1249 | ||
c5df3096 Z |
1250 | /* |
1251 | =for apidoc Am|SV *|mess_sv|SV *basemsg|bool consume | |
1252 | ||
1253 | Expands a message, intended for the user, to include an indication of | |
1254 | the current location in the code, if the message does not already appear | |
1255 | to be complete. | |
1256 | ||
1257 | C<basemsg> is the initial message or object. If it is a reference, it | |
1258 | will be used as-is and will be the result of this function. Otherwise it | |
1259 | is used as a string, and if it already ends with a newline, it is taken | |
1260 | to be complete, and the result of this function will be the same string. | |
1261 | If the message does not end with a newline, then a segment such as C<at | |
1262 | foo.pl line 37> will be appended, and possibly other clauses indicating | |
1263 | the current state of execution. The resulting message will end with a | |
1264 | dot and a newline. | |
1265 | ||
1266 | Normally, the resulting message is returned in a new mortal SV. | |
1267 | During global destruction a single SV may be shared between uses of this | |
1268 | function. If C<consume> is true, then the function is permitted (but not | |
1269 | required) to modify and return C<basemsg> instead of allocating a new SV. | |
1270 | ||
1271 | =cut | |
1272 | */ | |
1273 | ||
5a844595 | 1274 | SV * |
c5df3096 | 1275 | Perl_mess_sv(pTHX_ SV *basemsg, bool consume) |
46fc3d4c | 1276 | { |
97aff369 | 1277 | dVAR; |
c5df3096 | 1278 | SV *sv; |
46fc3d4c | 1279 | |
c5df3096 Z |
1280 | PERL_ARGS_ASSERT_MESS_SV; |
1281 | ||
1282 | if (SvROK(basemsg)) { | |
1283 | if (consume) { | |
1284 | sv = basemsg; | |
1285 | } | |
1286 | else { | |
1287 | sv = mess_alloc(); | |
1288 | sv_setsv(sv, basemsg); | |
1289 | } | |
1290 | return sv; | |
1291 | } | |
1292 | ||
1293 | if (SvPOK(basemsg) && consume) { | |
1294 | sv = basemsg; | |
1295 | } | |
1296 | else { | |
1297 | sv = mess_alloc(); | |
1298 | sv_copypv(sv, basemsg); | |
1299 | } | |
7918f24d | 1300 | |
46fc3d4c | 1301 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
ae7d165c PJ |
1302 | /* |
1303 | * Try and find the file and line for PL_op. This will usually be | |
1304 | * PL_curcop, but it might be a cop that has been optimised away. We | |
1305 | * can try to find such a cop by searching through the optree starting | |
1306 | * from the sibling of PL_curcop. | |
1307 | */ | |
1308 | ||
e1ec3a88 | 1309 | const COP *cop = closest_cop(PL_curcop, PL_curcop->op_sibling); |
5f66b61c AL |
1310 | if (!cop) |
1311 | cop = PL_curcop; | |
ae7d165c PJ |
1312 | |
1313 | if (CopLINE(cop)) | |
ed094faf | 1314 | Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf, |
3aed30dc | 1315 | OutCopFILE(cop), (IV)CopLINE(cop)); |
191f87d5 DH |
1316 | /* Seems that GvIO() can be untrustworthy during global destruction. */ |
1317 | if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO) | |
1318 | && IoLINES(GvIOp(PL_last_in_gv))) | |
1319 | { | |
e1ec3a88 | 1320 | const bool line_mode = (RsSIMPLE(PL_rs) && |
95a20fc0 | 1321 | SvCUR(PL_rs) == 1 && *SvPVX_const(PL_rs) == '\n'); |
57def98f | 1322 | Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf, |
5f66b61c | 1323 | PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv), |
edc2eac3 JH |
1324 | line_mode ? "line" : "chunk", |
1325 | (IV)IoLINES(GvIOp(PL_last_in_gv))); | |
a687059c | 1326 | } |
5f66b61c AL |
1327 | if (PL_dirty) |
1328 | sv_catpvs(sv, " during global destruction"); | |
1329 | sv_catpvs(sv, ".\n"); | |
a687059c | 1330 | } |
06bf62c7 | 1331 | return sv; |
a687059c LW |
1332 | } |
1333 | ||
c5df3096 Z |
1334 | /* |
1335 | =for apidoc Am|SV *|vmess|const char *pat|va_list *args | |
1336 | ||
1337 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1338 | argument list. These are used to generate a string message. If the | |
1339 | message does not end with a newline, then it will be extended with | |
1340 | some indication of the current location in the code, as described for | |
1341 | L</mess_sv>. | |
1342 | ||
1343 | Normally, the resulting message is returned in a new mortal SV. | |
1344 | During global destruction a single SV may be shared between uses of | |
1345 | this function. | |
1346 | ||
1347 | =cut | |
1348 | */ | |
1349 | ||
1350 | SV * | |
1351 | Perl_vmess(pTHX_ const char *pat, va_list *args) | |
1352 | { | |
1353 | dVAR; | |
1354 | SV * const sv = mess_alloc(); | |
1355 | ||
1356 | PERL_ARGS_ASSERT_VMESS; | |
1357 | ||
1358 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); | |
1359 | return mess_sv(sv, 1); | |
1360 | } | |
1361 | ||
7ff03255 | 1362 | void |
7d0994e0 | 1363 | Perl_write_to_stderr(pTHX_ SV* msv) |
7ff03255 | 1364 | { |
27da23d5 | 1365 | dVAR; |
7ff03255 SG |
1366 | IO *io; |
1367 | MAGIC *mg; | |
1368 | ||
7918f24d NC |
1369 | PERL_ARGS_ASSERT_WRITE_TO_STDERR; |
1370 | ||
7ff03255 SG |
1371 | if (PL_stderrgv && SvREFCNT(PL_stderrgv) |
1372 | && (io = GvIO(PL_stderrgv)) | |
daba3364 | 1373 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
7ff03255 SG |
1374 | { |
1375 | dSP; | |
1376 | ENTER; | |
1377 | SAVETMPS; | |
1378 | ||
1379 | save_re_context(); | |
1380 | SAVESPTR(PL_stderrgv); | |
a0714e2c | 1381 | PL_stderrgv = NULL; |
7ff03255 SG |
1382 | |
1383 | PUSHSTACKi(PERLSI_MAGIC); | |
1384 | ||
1385 | PUSHMARK(SP); | |
1386 | EXTEND(SP,2); | |
daba3364 | 1387 | PUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); |
7d0994e0 | 1388 | PUSHs(msv); |
7ff03255 SG |
1389 | PUTBACK; |
1390 | call_method("PRINT", G_SCALAR); | |
1391 | ||
1392 | POPSTACK; | |
1393 | FREETMPS; | |
1394 | LEAVE; | |
1395 | } | |
1396 | else { | |
1397 | #ifdef USE_SFIO | |
1398 | /* SFIO can really mess with your errno */ | |
4ee39169 | 1399 | dSAVED_ERRNO; |
7ff03255 | 1400 | #endif |
53c1dcc0 | 1401 | PerlIO * const serr = Perl_error_log; |
7d0994e0 GG |
1402 | STRLEN msglen; |
1403 | const char* message = SvPVx_const(msv, msglen); | |
7ff03255 SG |
1404 | |
1405 | PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen); | |
1406 | (void)PerlIO_flush(serr); | |
1407 | #ifdef USE_SFIO | |
4ee39169 | 1408 | RESTORE_ERRNO; |
7ff03255 SG |
1409 | #endif |
1410 | } | |
1411 | } | |
1412 | ||
c5df3096 Z |
1413 | /* |
1414 | =head1 Warning and Dieing | |
1415 | */ | |
1416 | ||
1417 | /* Common code used in dieing and warning */ | |
1418 | ||
1419 | STATIC SV * | |
1420 | S_with_queued_errors(pTHX_ SV *ex) | |
1421 | { | |
1422 | PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS; | |
1423 | if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) { | |
1424 | sv_catsv(PL_errors, ex); | |
1425 | ex = sv_mortalcopy(PL_errors); | |
1426 | SvCUR_set(PL_errors, 0); | |
1427 | } | |
1428 | return ex; | |
1429 | } | |
3ab1ac99 | 1430 | |
46d9c920 | 1431 | STATIC bool |
c5df3096 | 1432 | S_invoke_exception_hook(pTHX_ SV *ex, bool warn) |
63315e18 | 1433 | { |
97aff369 | 1434 | dVAR; |
63315e18 NC |
1435 | HV *stash; |
1436 | GV *gv; | |
1437 | CV *cv; | |
46d9c920 NC |
1438 | SV **const hook = warn ? &PL_warnhook : &PL_diehook; |
1439 | /* sv_2cv might call Perl_croak() or Perl_warner() */ | |
1440 | SV * const oldhook = *hook; | |
1441 | ||
c5df3096 Z |
1442 | if (!oldhook) |
1443 | return FALSE; | |
63315e18 | 1444 | |
63315e18 | 1445 | ENTER; |
46d9c920 NC |
1446 | SAVESPTR(*hook); |
1447 | *hook = NULL; | |
1448 | cv = sv_2cv(oldhook, &stash, &gv, 0); | |
63315e18 NC |
1449 | LEAVE; |
1450 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1451 | dSP; | |
c5df3096 | 1452 | SV *exarg; |
63315e18 NC |
1453 | |
1454 | ENTER; | |
1455 | save_re_context(); | |
46d9c920 NC |
1456 | if (warn) { |
1457 | SAVESPTR(*hook); | |
1458 | *hook = NULL; | |
1459 | } | |
c5df3096 Z |
1460 | exarg = newSVsv(ex); |
1461 | SvREADONLY_on(exarg); | |
1462 | SAVEFREESV(exarg); | |
63315e18 | 1463 | |
46d9c920 | 1464 | PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK); |
63315e18 | 1465 | PUSHMARK(SP); |
c5df3096 | 1466 | XPUSHs(exarg); |
63315e18 | 1467 | PUTBACK; |
daba3364 | 1468 | call_sv(MUTABLE_SV(cv), G_DISCARD); |
63315e18 NC |
1469 | POPSTACK; |
1470 | LEAVE; | |
46d9c920 | 1471 | return TRUE; |
63315e18 | 1472 | } |
46d9c920 | 1473 | return FALSE; |
63315e18 NC |
1474 | } |
1475 | ||
c5df3096 Z |
1476 | /* |
1477 | =for apidoc Am|OP *|die_sv|SV *baseex | |
e07360fa | 1478 | |
c5df3096 Z |
1479 | Behaves the same as L</croak_sv>, except for the return type. |
1480 | It should be used only where the C<OP *> return type is required. | |
1481 | The function never actually returns. | |
e07360fa | 1482 | |
c5df3096 Z |
1483 | =cut |
1484 | */ | |
e07360fa | 1485 | |
c5df3096 Z |
1486 | OP * |
1487 | Perl_die_sv(pTHX_ SV *baseex) | |
36477c24 | 1488 | { |
c5df3096 Z |
1489 | PERL_ARGS_ASSERT_DIE_SV; |
1490 | croak_sv(baseex); | |
ad09800f GG |
1491 | /* NOTREACHED */ |
1492 | return NULL; | |
36477c24 | 1493 | } |
1494 | ||
c5df3096 Z |
1495 | /* |
1496 | =for apidoc Am|OP *|die|const char *pat|... | |
1497 | ||
1498 | Behaves the same as L</croak>, except for the return type. | |
1499 | It should be used only where the C<OP *> return type is required. | |
1500 | The function never actually returns. | |
1501 | ||
1502 | =cut | |
1503 | */ | |
1504 | ||
c5be433b | 1505 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1506 | OP * |
1507 | Perl_die_nocontext(const char* pat, ...) | |
a687059c | 1508 | { |
cea2e8a9 | 1509 | dTHX; |
a687059c | 1510 | va_list args; |
cea2e8a9 | 1511 | va_start(args, pat); |
c5df3096 Z |
1512 | vcroak(pat, &args); |
1513 | /* NOTREACHED */ | |
cea2e8a9 | 1514 | va_end(args); |
c5df3096 | 1515 | return NULL; |
cea2e8a9 | 1516 | } |
c5be433b | 1517 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 GS |
1518 | |
1519 | OP * | |
1520 | Perl_die(pTHX_ const char* pat, ...) | |
1521 | { | |
cea2e8a9 GS |
1522 | va_list args; |
1523 | va_start(args, pat); | |
c5df3096 Z |
1524 | vcroak(pat, &args); |
1525 | /* NOTREACHED */ | |
cea2e8a9 | 1526 | va_end(args); |
c5df3096 | 1527 | return NULL; |
cea2e8a9 GS |
1528 | } |
1529 | ||
c5df3096 Z |
1530 | /* |
1531 | =for apidoc Am|void|croak_sv|SV *baseex | |
1532 | ||
1533 | This is an XS interface to Perl's C<die> function. | |
1534 | ||
1535 | C<baseex> is the error message or object. If it is a reference, it | |
1536 | will be used as-is. Otherwise it is used as a string, and if it does | |
1537 | not end with a newline then it will be extended with some indication of | |
1538 | the current location in the code, as described for L</mess_sv>. | |
1539 | ||
1540 | The error message or object will be used as an exception, by default | |
1541 | returning control to the nearest enclosing C<eval>, but subject to | |
1542 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak_sv> | |
1543 | function never returns normally. | |
1544 | ||
1545 | To die with a simple string message, the L</croak> function may be | |
1546 | more convenient. | |
1547 | ||
1548 | =cut | |
1549 | */ | |
1550 | ||
c5be433b | 1551 | void |
c5df3096 | 1552 | Perl_croak_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1553 | { |
c5df3096 Z |
1554 | SV *ex = with_queued_errors(mess_sv(baseex, 0)); |
1555 | PERL_ARGS_ASSERT_CROAK_SV; | |
1556 | invoke_exception_hook(ex, FALSE); | |
1557 | die_unwind(ex); | |
1558 | } | |
1559 | ||
1560 | /* | |
1561 | =for apidoc Am|void|vcroak|const char *pat|va_list *args | |
1562 | ||
1563 | This is an XS interface to Perl's C<die> function. | |
1564 | ||
1565 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1566 | argument list. These are used to generate a string message. If the | |
1567 | message does not end with a newline, then it will be extended with | |
1568 | some indication of the current location in the code, as described for | |
1569 | L</mess_sv>. | |
1570 | ||
1571 | The error message will be used as an exception, by default | |
1572 | returning control to the nearest enclosing C<eval>, but subject to | |
1573 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1574 | function never returns normally. | |
a687059c | 1575 | |
c5df3096 Z |
1576 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> |
1577 | (C<$@>) will be used as an error message or object instead of building an | |
1578 | error message from arguments. If you want to throw a non-string object, | |
1579 | or build an error message in an SV yourself, it is preferable to use | |
1580 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
5a844595 | 1581 | |
c5df3096 Z |
1582 | =cut |
1583 | */ | |
1584 | ||
1585 | void | |
1586 | Perl_vcroak(pTHX_ const char* pat, va_list *args) | |
1587 | { | |
1588 | SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0)); | |
1589 | invoke_exception_hook(ex, FALSE); | |
1590 | die_unwind(ex); | |
a687059c LW |
1591 | } |
1592 | ||
c5df3096 Z |
1593 | /* |
1594 | =for apidoc Am|void|croak|const char *pat|... | |
1595 | ||
1596 | This is an XS interface to Perl's C<die> function. | |
1597 | ||
1598 | Take a sprintf-style format pattern and argument list. These are used to | |
1599 | generate a string message. If the message does not end with a newline, | |
1600 | then it will be extended with some indication of the current location | |
1601 | in the code, as described for L</mess_sv>. | |
1602 | ||
1603 | The error message will be used as an exception, by default | |
1604 | returning control to the nearest enclosing C<eval>, but subject to | |
1605 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1606 | function never returns normally. | |
1607 | ||
1608 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> | |
1609 | (C<$@>) will be used as an error message or object instead of building an | |
1610 | error message from arguments. If you want to throw a non-string object, | |
1611 | or build an error message in an SV yourself, it is preferable to use | |
1612 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
1613 | ||
1614 | =cut | |
1615 | */ | |
1616 | ||
c5be433b | 1617 | #if defined(PERL_IMPLICIT_CONTEXT) |
8990e307 | 1618 | void |
cea2e8a9 | 1619 | Perl_croak_nocontext(const char *pat, ...) |
a687059c | 1620 | { |
cea2e8a9 | 1621 | dTHX; |
a687059c | 1622 | va_list args; |
cea2e8a9 | 1623 | va_start(args, pat); |
c5be433b | 1624 | vcroak(pat, &args); |
cea2e8a9 GS |
1625 | /* NOTREACHED */ |
1626 | va_end(args); | |
1627 | } | |
1628 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1629 | ||
c5df3096 Z |
1630 | void |
1631 | Perl_croak(pTHX_ const char *pat, ...) | |
1632 | { | |
1633 | va_list args; | |
1634 | va_start(args, pat); | |
1635 | vcroak(pat, &args); | |
1636 | /* NOTREACHED */ | |
1637 | va_end(args); | |
1638 | } | |
1639 | ||
954c1994 | 1640 | /* |
6ad8f254 NC |
1641 | =for apidoc Am|void|croak_no_modify |
1642 | ||
1643 | Exactly equivalent to C<Perl_croak(aTHX_ "%s", PL_no_modify)>, but generates | |
1644 | terser object code than using C<Perl_croak>. Less code used on exception code | |
1645 | paths reduces CPU cache pressure. | |
1646 | ||
1647 | */ | |
1648 | ||
1649 | void | |
1650 | Perl_croak_no_modify(pTHX) | |
1651 | { | |
1652 | Perl_croak(aTHX_ "%s", PL_no_modify); | |
1653 | } | |
1654 | ||
1655 | /* | |
c5df3096 | 1656 | =for apidoc Am|void|warn_sv|SV *baseex |
ccfc67b7 | 1657 | |
c5df3096 | 1658 | This is an XS interface to Perl's C<warn> function. |
954c1994 | 1659 | |
c5df3096 Z |
1660 | C<baseex> is the error message or object. If it is a reference, it |
1661 | will be used as-is. Otherwise it is used as a string, and if it does | |
1662 | not end with a newline then it will be extended with some indication of | |
1663 | the current location in the code, as described for L</mess_sv>. | |
9983fa3c | 1664 | |
c5df3096 Z |
1665 | The error message or object will by default be written to standard error, |
1666 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
9983fa3c | 1667 | |
c5df3096 Z |
1668 | To warn with a simple string message, the L</warn> function may be |
1669 | more convenient. | |
954c1994 GS |
1670 | |
1671 | =cut | |
1672 | */ | |
1673 | ||
cea2e8a9 | 1674 | void |
c5df3096 | 1675 | Perl_warn_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1676 | { |
c5df3096 Z |
1677 | SV *ex = mess_sv(baseex, 0); |
1678 | PERL_ARGS_ASSERT_WARN_SV; | |
1679 | if (!invoke_exception_hook(ex, TRUE)) | |
1680 | write_to_stderr(ex); | |
cea2e8a9 GS |
1681 | } |
1682 | ||
c5df3096 Z |
1683 | /* |
1684 | =for apidoc Am|void|vwarn|const char *pat|va_list *args | |
1685 | ||
1686 | This is an XS interface to Perl's C<warn> function. | |
1687 | ||
1688 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1689 | argument list. These are used to generate a string message. If the | |
1690 | message does not end with a newline, then it will be extended with | |
1691 | some indication of the current location in the code, as described for | |
1692 | L</mess_sv>. | |
1693 | ||
1694 | The error message or object will by default be written to standard error, | |
1695 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1696 | ||
1697 | Unlike with L</vcroak>, C<pat> is not permitted to be null. | |
1698 | ||
1699 | =cut | |
1700 | */ | |
1701 | ||
c5be433b GS |
1702 | void |
1703 | Perl_vwarn(pTHX_ const char* pat, va_list *args) | |
cea2e8a9 | 1704 | { |
c5df3096 | 1705 | SV *ex = vmess(pat, args); |
7918f24d | 1706 | PERL_ARGS_ASSERT_VWARN; |
c5df3096 Z |
1707 | if (!invoke_exception_hook(ex, TRUE)) |
1708 | write_to_stderr(ex); | |
1709 | } | |
7918f24d | 1710 | |
c5df3096 Z |
1711 | /* |
1712 | =for apidoc Am|void|warn|const char *pat|... | |
87582a92 | 1713 | |
c5df3096 Z |
1714 | This is an XS interface to Perl's C<warn> function. |
1715 | ||
1716 | Take a sprintf-style format pattern and argument list. These are used to | |
1717 | generate a string message. If the message does not end with a newline, | |
1718 | then it will be extended with some indication of the current location | |
1719 | in the code, as described for L</mess_sv>. | |
1720 | ||
1721 | The error message or object will by default be written to standard error, | |
1722 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1723 | ||
1724 | Unlike with L</croak>, C<pat> is not permitted to be null. | |
1725 | ||
1726 | =cut | |
1727 | */ | |
8d063cd8 | 1728 | |
c5be433b | 1729 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1730 | void |
1731 | Perl_warn_nocontext(const char *pat, ...) | |
1732 | { | |
1733 | dTHX; | |
1734 | va_list args; | |
7918f24d | 1735 | PERL_ARGS_ASSERT_WARN_NOCONTEXT; |
cea2e8a9 | 1736 | va_start(args, pat); |
c5be433b | 1737 | vwarn(pat, &args); |
cea2e8a9 GS |
1738 | va_end(args); |
1739 | } | |
1740 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1741 | ||
1742 | void | |
1743 | Perl_warn(pTHX_ const char *pat, ...) | |
1744 | { | |
1745 | va_list args; | |
7918f24d | 1746 | PERL_ARGS_ASSERT_WARN; |
cea2e8a9 | 1747 | va_start(args, pat); |
c5be433b | 1748 | vwarn(pat, &args); |
cea2e8a9 GS |
1749 | va_end(args); |
1750 | } | |
1751 | ||
c5be433b GS |
1752 | #if defined(PERL_IMPLICIT_CONTEXT) |
1753 | void | |
1754 | Perl_warner_nocontext(U32 err, const char *pat, ...) | |
1755 | { | |
27da23d5 | 1756 | dTHX; |
c5be433b | 1757 | va_list args; |
7918f24d | 1758 | PERL_ARGS_ASSERT_WARNER_NOCONTEXT; |
c5be433b GS |
1759 | va_start(args, pat); |
1760 | vwarner(err, pat, &args); | |
1761 | va_end(args); | |
1762 | } | |
1763 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1764 | ||
599cee73 | 1765 | void |
9b387841 NC |
1766 | Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...) |
1767 | { | |
1768 | PERL_ARGS_ASSERT_CK_WARNER_D; | |
1769 | ||
1770 | if (Perl_ckwarn_d(aTHX_ err)) { | |
1771 | va_list args; | |
1772 | va_start(args, pat); | |
1773 | vwarner(err, pat, &args); | |
1774 | va_end(args); | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | void | |
a2a5de95 NC |
1779 | Perl_ck_warner(pTHX_ U32 err, const char* pat, ...) |
1780 | { | |
1781 | PERL_ARGS_ASSERT_CK_WARNER; | |
1782 | ||
1783 | if (Perl_ckwarn(aTHX_ err)) { | |
1784 | va_list args; | |
1785 | va_start(args, pat); | |
1786 | vwarner(err, pat, &args); | |
1787 | va_end(args); | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | void | |
864dbfa3 | 1792 | Perl_warner(pTHX_ U32 err, const char* pat,...) |
599cee73 PM |
1793 | { |
1794 | va_list args; | |
7918f24d | 1795 | PERL_ARGS_ASSERT_WARNER; |
c5be433b GS |
1796 | va_start(args, pat); |
1797 | vwarner(err, pat, &args); | |
1798 | va_end(args); | |
1799 | } | |
1800 | ||
1801 | void | |
1802 | Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) | |
1803 | { | |
27da23d5 | 1804 | dVAR; |
7918f24d | 1805 | PERL_ARGS_ASSERT_VWARNER; |
5f2d9966 | 1806 | if (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) { |
a3b680e6 | 1807 | SV * const msv = vmess(pat, args); |
599cee73 | 1808 | |
c5df3096 Z |
1809 | invoke_exception_hook(msv, FALSE); |
1810 | die_unwind(msv); | |
599cee73 PM |
1811 | } |
1812 | else { | |
d13b0d77 | 1813 | Perl_vwarn(aTHX_ pat, args); |
599cee73 PM |
1814 | } |
1815 | } | |
1816 | ||
f54ba1c2 DM |
1817 | /* implements the ckWARN? macros */ |
1818 | ||
1819 | bool | |
1820 | Perl_ckwarn(pTHX_ U32 w) | |
1821 | { | |
97aff369 | 1822 | dVAR; |
ad287e37 NC |
1823 | /* If lexical warnings have not been set, use $^W. */ |
1824 | if (isLEXWARN_off) | |
1825 | return PL_dowarn & G_WARN_ON; | |
1826 | ||
26c7b074 | 1827 | return ckwarn_common(w); |
f54ba1c2 DM |
1828 | } |
1829 | ||
1830 | /* implements the ckWARN?_d macro */ | |
1831 | ||
1832 | bool | |
1833 | Perl_ckwarn_d(pTHX_ U32 w) | |
1834 | { | |
97aff369 | 1835 | dVAR; |
ad287e37 NC |
1836 | /* If lexical warnings have not been set then default classes warn. */ |
1837 | if (isLEXWARN_off) | |
1838 | return TRUE; | |
1839 | ||
26c7b074 NC |
1840 | return ckwarn_common(w); |
1841 | } | |
1842 | ||
1843 | static bool | |
1844 | S_ckwarn_common(pTHX_ U32 w) | |
1845 | { | |
ad287e37 NC |
1846 | if (PL_curcop->cop_warnings == pWARN_ALL) |
1847 | return TRUE; | |
1848 | ||
1849 | if (PL_curcop->cop_warnings == pWARN_NONE) | |
1850 | return FALSE; | |
1851 | ||
98fe6610 NC |
1852 | /* Check the assumption that at least the first slot is non-zero. */ |
1853 | assert(unpackWARN1(w)); | |
1854 | ||
1855 | /* Check the assumption that it is valid to stop as soon as a zero slot is | |
1856 | seen. */ | |
1857 | if (!unpackWARN2(w)) { | |
1858 | assert(!unpackWARN3(w)); | |
1859 | assert(!unpackWARN4(w)); | |
1860 | } else if (!unpackWARN3(w)) { | |
1861 | assert(!unpackWARN4(w)); | |
1862 | } | |
1863 | ||
26c7b074 NC |
1864 | /* Right, dealt with all the special cases, which are implemented as non- |
1865 | pointers, so there is a pointer to a real warnings mask. */ | |
98fe6610 NC |
1866 | do { |
1867 | if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))) | |
1868 | return TRUE; | |
1869 | } while (w >>= WARNshift); | |
1870 | ||
1871 | return FALSE; | |
f54ba1c2 DM |
1872 | } |
1873 | ||
72dc9ed5 NC |
1874 | /* Set buffer=NULL to get a new one. */ |
1875 | STRLEN * | |
8ee4cf24 | 1876 | Perl_new_warnings_bitfield(pTHX_ STRLEN *buffer, const char *const bits, |
72dc9ed5 NC |
1877 | STRLEN size) { |
1878 | const MEM_SIZE len_wanted = sizeof(STRLEN) + size; | |
35da51f7 | 1879 | PERL_UNUSED_CONTEXT; |
7918f24d | 1880 | PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD; |
72dc9ed5 | 1881 | |
10edeb5d JH |
1882 | buffer = (STRLEN*) |
1883 | (specialWARN(buffer) ? | |
1884 | PerlMemShared_malloc(len_wanted) : | |
1885 | PerlMemShared_realloc(buffer, len_wanted)); | |
72dc9ed5 NC |
1886 | buffer[0] = size; |
1887 | Copy(bits, (buffer + 1), size, char); | |
1888 | return buffer; | |
1889 | } | |
f54ba1c2 | 1890 | |
e6587932 DM |
1891 | /* since we've already done strlen() for both nam and val |
1892 | * we can use that info to make things faster than | |
1893 | * sprintf(s, "%s=%s", nam, val) | |
1894 | */ | |
1895 | #define my_setenv_format(s, nam, nlen, val, vlen) \ | |
1896 | Copy(nam, s, nlen, char); \ | |
1897 | *(s+nlen) = '='; \ | |
1898 | Copy(val, s+(nlen+1), vlen, char); \ | |
1899 | *(s+(nlen+1+vlen)) = '\0' | |
1900 | ||
c5d12488 JH |
1901 | #ifdef USE_ENVIRON_ARRAY |
1902 | /* VMS' my_setenv() is in vms.c */ | |
1903 | #if !defined(WIN32) && !defined(NETWARE) | |
8d063cd8 | 1904 | void |
e1ec3a88 | 1905 | Perl_my_setenv(pTHX_ const char *nam, const char *val) |
8d063cd8 | 1906 | { |
27da23d5 | 1907 | dVAR; |
4efc5df6 GS |
1908 | #ifdef USE_ITHREADS |
1909 | /* only parent thread can modify process environment */ | |
1910 | if (PL_curinterp == aTHX) | |
1911 | #endif | |
1912 | { | |
f2517201 | 1913 | #ifndef PERL_USE_SAFE_PUTENV |
50acdf95 | 1914 | if (!PL_use_safe_putenv) { |
c5d12488 | 1915 | /* most putenv()s leak, so we manipulate environ directly */ |
3a9222be JH |
1916 | register I32 i; |
1917 | register const I32 len = strlen(nam); | |
c5d12488 JH |
1918 | int nlen, vlen; |
1919 | ||
3a9222be JH |
1920 | /* where does it go? */ |
1921 | for (i = 0; environ[i]; i++) { | |
1922 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') | |
1923 | break; | |
1924 | } | |
1925 | ||
c5d12488 JH |
1926 | if (environ == PL_origenviron) { /* need we copy environment? */ |
1927 | I32 j; | |
1928 | I32 max; | |
1929 | char **tmpenv; | |
1930 | ||
1931 | max = i; | |
1932 | while (environ[max]) | |
1933 | max++; | |
1934 | tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); | |
1935 | for (j=0; j<max; j++) { /* copy environment */ | |
1936 | const int len = strlen(environ[j]); | |
1937 | tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char)); | |
1938 | Copy(environ[j], tmpenv[j], len+1, char); | |
1939 | } | |
1940 | tmpenv[max] = NULL; | |
1941 | environ = tmpenv; /* tell exec where it is now */ | |
1942 | } | |
1943 | if (!val) { | |
1944 | safesysfree(environ[i]); | |
1945 | while (environ[i]) { | |
1946 | environ[i] = environ[i+1]; | |
1947 | i++; | |
a687059c | 1948 | } |
c5d12488 JH |
1949 | return; |
1950 | } | |
1951 | if (!environ[i]) { /* does not exist yet */ | |
1952 | environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*)); | |
1953 | environ[i+1] = NULL; /* make sure it's null terminated */ | |
1954 | } | |
1955 | else | |
1956 | safesysfree(environ[i]); | |
1957 | nlen = strlen(nam); | |
1958 | vlen = strlen(val); | |
1959 | ||
1960 | environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char)); | |
1961 | /* all that work just for this */ | |
1962 | my_setenv_format(environ[i], nam, nlen, val, vlen); | |
50acdf95 | 1963 | } else { |
c5d12488 | 1964 | # endif |
7ee146b1 | 1965 | # if defined(__CYGWIN__) || defined(EPOC) || defined(__SYMBIAN32__) || defined(__riscos__) |
88f5bc07 AB |
1966 | # if defined(HAS_UNSETENV) |
1967 | if (val == NULL) { | |
1968 | (void)unsetenv(nam); | |
1969 | } else { | |
1970 | (void)setenv(nam, val, 1); | |
1971 | } | |
1972 | # else /* ! HAS_UNSETENV */ | |
1973 | (void)setenv(nam, val, 1); | |
1974 | # endif /* HAS_UNSETENV */ | |
47dafe4d | 1975 | # else |
88f5bc07 AB |
1976 | # if defined(HAS_UNSETENV) |
1977 | if (val == NULL) { | |
1978 | (void)unsetenv(nam); | |
1979 | } else { | |
c4420975 AL |
1980 | const int nlen = strlen(nam); |
1981 | const int vlen = strlen(val); | |
1982 | char * const new_env = | |
88f5bc07 AB |
1983 | (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); |
1984 | my_setenv_format(new_env, nam, nlen, val, vlen); | |
1985 | (void)putenv(new_env); | |
1986 | } | |
1987 | # else /* ! HAS_UNSETENV */ | |
1988 | char *new_env; | |
c4420975 AL |
1989 | const int nlen = strlen(nam); |
1990 | int vlen; | |
88f5bc07 AB |
1991 | if (!val) { |
1992 | val = ""; | |
1993 | } | |
1994 | vlen = strlen(val); | |
1995 | new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); | |
1996 | /* all that work just for this */ | |
1997 | my_setenv_format(new_env, nam, nlen, val, vlen); | |
1998 | (void)putenv(new_env); | |
1999 | # endif /* HAS_UNSETENV */ | |
47dafe4d | 2000 | # endif /* __CYGWIN__ */ |
50acdf95 MS |
2001 | #ifndef PERL_USE_SAFE_PUTENV |
2002 | } | |
2003 | #endif | |
4efc5df6 | 2004 | } |
8d063cd8 LW |
2005 | } |
2006 | ||
c5d12488 | 2007 | #else /* WIN32 || NETWARE */ |
68dc0745 | 2008 | |
2009 | void | |
72229eff | 2010 | Perl_my_setenv(pTHX_ const char *nam, const char *val) |
68dc0745 | 2011 | { |
27da23d5 | 2012 | dVAR; |
c5d12488 JH |
2013 | register char *envstr; |
2014 | const int nlen = strlen(nam); | |
2015 | int vlen; | |
e6587932 | 2016 | |
c5d12488 JH |
2017 | if (!val) { |
2018 | val = ""; | |
ac5c734f | 2019 | } |
c5d12488 JH |
2020 | vlen = strlen(val); |
2021 | Newx(envstr, nlen+vlen+2, char); | |
2022 | my_setenv_format(envstr, nam, nlen, val, vlen); | |
2023 | (void)PerlEnv_putenv(envstr); | |
2024 | Safefree(envstr); | |
3e3baf6d TB |
2025 | } |
2026 | ||
c5d12488 | 2027 | #endif /* WIN32 || NETWARE */ |
3e3baf6d | 2028 | |
c5d12488 | 2029 | #endif /* !VMS && !EPOC*/ |
378cc40b | 2030 | |
16d20bd9 | 2031 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 2032 | I32 |
6e732051 | 2033 | Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */ |
378cc40b | 2034 | { |
35da51f7 | 2035 | I32 retries = 0; |
378cc40b | 2036 | |
7918f24d NC |
2037 | PERL_ARGS_ASSERT_UNLNK; |
2038 | ||
35da51f7 AL |
2039 | while (PerlLIO_unlink(f) >= 0) |
2040 | retries++; | |
2041 | return retries ? 0 : -1; | |
378cc40b LW |
2042 | } |
2043 | #endif | |
2044 | ||
7a3f2258 | 2045 | /* this is a drop-in replacement for bcopy() */ |
2253333f | 2046 | #if (!defined(HAS_MEMCPY) && !defined(HAS_BCOPY)) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY) && !defined(HAS_SAFE_BCOPY)) |
378cc40b | 2047 | char * |
7a3f2258 | 2048 | Perl_my_bcopy(register const char *from,register char *to,register I32 len) |
378cc40b | 2049 | { |
2d03de9c | 2050 | char * const retval = to; |
378cc40b | 2051 | |
7918f24d NC |
2052 | PERL_ARGS_ASSERT_MY_BCOPY; |
2053 | ||
7c0587c8 LW |
2054 | if (from - to >= 0) { |
2055 | while (len--) | |
2056 | *to++ = *from++; | |
2057 | } | |
2058 | else { | |
2059 | to += len; | |
2060 | from += len; | |
2061 | while (len--) | |
faf8582f | 2062 | *(--to) = *(--from); |
7c0587c8 | 2063 | } |
378cc40b LW |
2064 | return retval; |
2065 | } | |
ffed7fef | 2066 | #endif |
378cc40b | 2067 | |
7a3f2258 | 2068 | /* this is a drop-in replacement for memset() */ |
fc36a67e | 2069 | #ifndef HAS_MEMSET |
2070 | void * | |
7a3f2258 | 2071 | Perl_my_memset(register char *loc, register I32 ch, register I32 len) |
fc36a67e | 2072 | { |
2d03de9c | 2073 | char * const retval = loc; |
fc36a67e | 2074 | |
7918f24d NC |
2075 | PERL_ARGS_ASSERT_MY_MEMSET; |
2076 | ||
fc36a67e | 2077 | while (len--) |
2078 | *loc++ = ch; | |
2079 | return retval; | |
2080 | } | |
2081 | #endif | |
2082 | ||
7a3f2258 | 2083 | /* this is a drop-in replacement for bzero() */ |
7c0587c8 | 2084 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b | 2085 | char * |
7a3f2258 | 2086 | Perl_my_bzero(register char *loc, register I32 len) |
378cc40b | 2087 | { |
2d03de9c | 2088 | char * const retval = loc; |
378cc40b | 2089 | |
7918f24d NC |
2090 | PERL_ARGS_ASSERT_MY_BZERO; |
2091 | ||
378cc40b LW |
2092 | while (len--) |
2093 | *loc++ = 0; | |
2094 | return retval; | |
2095 | } | |
2096 | #endif | |
7c0587c8 | 2097 | |
7a3f2258 | 2098 | /* this is a drop-in replacement for memcmp() */ |
36477c24 | 2099 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
79072805 | 2100 | I32 |
7a3f2258 | 2101 | Perl_my_memcmp(const char *s1, const char *s2, register I32 len) |
7c0587c8 | 2102 | { |
e1ec3a88 AL |
2103 | register const U8 *a = (const U8 *)s1; |
2104 | register const U8 *b = (const U8 *)s2; | |
79072805 | 2105 | register I32 tmp; |
7c0587c8 | 2106 | |
7918f24d NC |
2107 | PERL_ARGS_ASSERT_MY_MEMCMP; |
2108 | ||
7c0587c8 | 2109 | while (len--) { |
27da23d5 | 2110 | if ((tmp = *a++ - *b++)) |
7c0587c8 LW |
2111 | return tmp; |
2112 | } | |
2113 | return 0; | |
2114 | } | |
36477c24 | 2115 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 2116 | |
fe14fcc3 | 2117 | #ifndef HAS_VPRINTF |
d05d9be5 AD |
2118 | /* This vsprintf replacement should generally never get used, since |
2119 | vsprintf was available in both System V and BSD 2.11. (There may | |
2120 | be some cross-compilation or embedded set-ups where it is needed, | |
2121 | however.) | |
2122 | ||
2123 | If you encounter a problem in this function, it's probably a symptom | |
2124 | that Configure failed to detect your system's vprintf() function. | |
2125 | See the section on "item vsprintf" in the INSTALL file. | |
2126 | ||
2127 | This version may compile on systems with BSD-ish <stdio.h>, | |
2128 | but probably won't on others. | |
2129 | */ | |
a687059c | 2130 | |
85e6fe83 | 2131 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2132 | char * |
2133 | #else | |
2134 | int | |
2135 | #endif | |
d05d9be5 | 2136 | vsprintf(char *dest, const char *pat, void *args) |
a687059c LW |
2137 | { |
2138 | FILE fakebuf; | |
2139 | ||
d05d9be5 AD |
2140 | #if defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE) |
2141 | FILE_ptr(&fakebuf) = (STDCHAR *) dest; | |
2142 | FILE_cnt(&fakebuf) = 32767; | |
2143 | #else | |
2144 | /* These probably won't compile -- If you really need | |
2145 | this, you'll have to figure out some other method. */ | |
a687059c LW |
2146 | fakebuf._ptr = dest; |
2147 | fakebuf._cnt = 32767; | |
d05d9be5 | 2148 | #endif |
35c8bce7 LW |
2149 | #ifndef _IOSTRG |
2150 | #define _IOSTRG 0 | |
2151 | #endif | |
a687059c LW |
2152 | fakebuf._flag = _IOWRT|_IOSTRG; |
2153 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
d05d9be5 AD |
2154 | #if defined(STDIO_PTR_LVALUE) |
2155 | *(FILE_ptr(&fakebuf)++) = '\0'; | |
2156 | #else | |
2157 | /* PerlIO has probably #defined away fputc, but we want it here. */ | |
2158 | # ifdef fputc | |
2159 | # undef fputc /* XXX Should really restore it later */ | |
2160 | # endif | |
2161 | (void)fputc('\0', &fakebuf); | |
2162 | #endif | |
85e6fe83 | 2163 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2164 | return(dest); |
2165 | #else | |
2166 | return 0; /* perl doesn't use return value */ | |
2167 | #endif | |
2168 | } | |
2169 | ||
fe14fcc3 | 2170 | #endif /* HAS_VPRINTF */ |
a687059c LW |
2171 | |
2172 | #ifdef MYSWAP | |
ffed7fef | 2173 | #if BYTEORDER != 0x4321 |
a687059c | 2174 | short |
864dbfa3 | 2175 | Perl_my_swap(pTHX_ short s) |
a687059c LW |
2176 | { |
2177 | #if (BYTEORDER & 1) == 0 | |
2178 | short result; | |
2179 | ||
2180 | result = ((s & 255) << 8) + ((s >> 8) & 255); | |
2181 | return result; | |
2182 | #else | |
2183 | return s; | |
2184 | #endif | |
2185 | } | |
2186 | ||
2187 | long | |
864dbfa3 | 2188 | Perl_my_htonl(pTHX_ long l) |
a687059c LW |
2189 | { |
2190 | union { | |
2191 | long result; | |
ffed7fef | 2192 | char c[sizeof(long)]; |
a687059c LW |
2193 | } u; |
2194 | ||
cef6ea9d JH |
2195 | #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 |
2196 | #if BYTEORDER == 0x12345678 | |
2197 | u.result = 0; | |
2198 | #endif | |
a687059c LW |
2199 | u.c[0] = (l >> 24) & 255; |
2200 | u.c[1] = (l >> 16) & 255; | |
2201 | u.c[2] = (l >> 8) & 255; | |
2202 | u.c[3] = l & 255; | |
2203 | return u.result; | |
2204 | #else | |
ffed7fef | 2205 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
cea2e8a9 | 2206 | Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
a687059c | 2207 | #else |
79072805 LW |
2208 | register I32 o; |
2209 | register I32 s; | |
a687059c | 2210 | |
ffed7fef LW |
2211 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
2212 | u.c[o & 0xf] = (l >> s) & 255; | |
a687059c LW |
2213 | } |
2214 | return u.result; | |
2215 | #endif | |
2216 | #endif | |
2217 | } | |
2218 | ||
2219 | long | |
864dbfa3 | 2220 | Perl_my_ntohl(pTHX_ long l) |
a687059c LW |
2221 | { |
2222 | union { | |
2223 | long l; | |
ffed7fef | 2224 | char c[sizeof(long)]; |
a687059c LW |
2225 | } u; |
2226 | ||
ffed7fef | 2227 | #if BYTEORDER == 0x1234 |
a687059c LW |
2228 | u.c[0] = (l >> 24) & 255; |
2229 | u.c[1] = (l >> 16) & 255; | |
2230 | u.c[2] = (l >> 8) & 255; | |
2231 | u.c[3] = l & 255; | |
2232 | return u.l; | |
2233 | #else | |
ffed7fef | 2234 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
cea2e8a9 | 2235 | Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
a687059c | 2236 | #else |
79072805 LW |
2237 | register I32 o; |
2238 | register I32 s; | |
a687059c LW |
2239 | |
2240 | u.l = l; | |
2241 | l = 0; | |
ffed7fef LW |
2242 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
2243 | l |= (u.c[o & 0xf] & 255) << s; | |
a687059c LW |
2244 | } |
2245 | return l; | |
2246 | #endif | |
2247 | #endif | |
2248 | } | |
2249 | ||
ffed7fef | 2250 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 LW |
2251 | #endif /* MYSWAP */ |
2252 | ||
2253 | /* | |
2254 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. | |
2255 | * If these functions are defined, | |
2256 | * the BYTEORDER is neither 0x1234 nor 0x4321. | |
2257 | * However, this is not assumed. | |
2258 | * -DWS | |
2259 | */ | |
2260 | ||
1109a392 | 2261 | #define HTOLE(name,type) \ |
988174c1 | 2262 | type \ |
ba106d47 | 2263 | name (register type n) \ |
988174c1 LW |
2264 | { \ |
2265 | union { \ | |
2266 | type value; \ | |
2267 | char c[sizeof(type)]; \ | |
2268 | } u; \ | |
bb7a0f54 MHM |
2269 | register U32 i; \ |
2270 | register U32 s = 0; \ | |
1109a392 | 2271 | for (i = 0; i < sizeof(u.c); i++, s += 8) { \ |
988174c1 LW |
2272 | u.c[i] = (n >> s) & 0xFF; \ |
2273 | } \ | |
2274 | return u.value; \ | |
2275 | } | |
2276 | ||
1109a392 | 2277 | #define LETOH(name,type) \ |
988174c1 | 2278 | type \ |
ba106d47 | 2279 | name (register type n) \ |
988174c1 LW |
2280 | { \ |
2281 | union { \ | |
2282 | type value; \ | |
2283 | char c[sizeof(type)]; \ | |
2284 | } u; \ | |
bb7a0f54 MHM |
2285 | register U32 i; \ |
2286 | register U32 s = 0; \ | |
988174c1 LW |
2287 | u.value = n; \ |
2288 | n = 0; \ | |
1109a392 MHM |
2289 | for (i = 0; i < sizeof(u.c); i++, s += 8) { \ |
2290 | n |= ((type)(u.c[i] & 0xFF)) << s; \ | |
988174c1 LW |
2291 | } \ |
2292 | return n; \ | |
2293 | } | |
2294 | ||
1109a392 MHM |
2295 | /* |
2296 | * Big-endian byte order functions. | |
2297 | */ | |
2298 | ||
2299 | #define HTOBE(name,type) \ | |
2300 | type \ | |
2301 | name (register type n) \ | |
2302 | { \ | |
2303 | union { \ | |
2304 | type value; \ | |
2305 | char c[sizeof(type)]; \ | |
2306 | } u; \ | |
bb7a0f54 MHM |
2307 | register U32 i; \ |
2308 | register U32 s = 8*(sizeof(u.c)-1); \ | |
1109a392 MHM |
2309 | for (i = 0; i < sizeof(u.c); i++, s -= 8) { \ |
2310 | u.c[i] = (n >> s) & 0xFF; \ | |
2311 | } \ | |
2312 | return u.value; \ | |
2313 | } | |
2314 | ||
2315 | #define BETOH(name,type) \ | |
2316 | type \ | |
2317 | name (register type n) \ | |
2318 | { \ | |
2319 | union { \ | |
2320 | type value; \ | |
2321 | char c[sizeof(type)]; \ | |
2322 | } u; \ | |
bb7a0f54 MHM |
2323 | register U32 i; \ |
2324 | register U32 s = 8*(sizeof(u.c)-1); \ | |
1109a392 MHM |
2325 | u.value = n; \ |
2326 | n = 0; \ | |
2327 | for (i = 0; i < sizeof(u.c); i++, s -= 8) { \ | |
2328 | n |= ((type)(u.c[i] & 0xFF)) << s; \ | |
2329 | } \ | |
2330 | return n; \ | |
2331 | } | |
2332 | ||
2333 | /* | |
2334 | * If we just can't do it... | |
2335 | */ | |
2336 | ||
2337 | #define NOT_AVAIL(name,type) \ | |
2338 | type \ | |
2339 | name (register type n) \ | |
2340 | { \ | |
2341 | Perl_croak_nocontext(#name "() not available"); \ | |
2342 | return n; /* not reached */ \ | |
2343 | } | |
2344 | ||
2345 | ||
988174c1 | 2346 | #if defined(HAS_HTOVS) && !defined(htovs) |
1109a392 | 2347 | HTOLE(htovs,short) |
988174c1 LW |
2348 | #endif |
2349 | #if defined(HAS_HTOVL) && !defined(htovl) | |
1109a392 | 2350 | HTOLE(htovl,long) |
988174c1 LW |
2351 | #endif |
2352 | #if defined(HAS_VTOHS) && !defined(vtohs) | |
1109a392 | 2353 | LETOH(vtohs,short) |
988174c1 LW |
2354 | #endif |
2355 | #if defined(HAS_VTOHL) && !defined(vtohl) | |
1109a392 MHM |
2356 | LETOH(vtohl,long) |
2357 | #endif | |
2358 | ||
2359 | #ifdef PERL_NEED_MY_HTOLE16 | |
2360 | # if U16SIZE == 2 | |
2361 | HTOLE(Perl_my_htole16,U16) | |
2362 | # else | |
2363 | NOT_AVAIL(Perl_my_htole16,U16) | |
2364 | # endif | |
2365 | #endif | |
2366 | #ifdef PERL_NEED_MY_LETOH16 | |
2367 | # if U16SIZE == 2 | |
2368 | LETOH(Perl_my_letoh16,U16) | |
2369 | # else | |
2370 | NOT_AVAIL(Perl_my_letoh16,U16) | |
2371 | # endif | |
2372 | #endif | |
2373 | #ifdef PERL_NEED_MY_HTOBE16 | |
2374 | # if U16SIZE == 2 | |
2375 | HTOBE(Perl_my_htobe16,U16) | |
2376 | # else | |
2377 | NOT_AVAIL(Perl_my_htobe16,U16) | |
2378 | # endif | |
2379 | #endif | |
2380 | #ifdef PERL_NEED_MY_BETOH16 | |
2381 | # if U16SIZE == 2 | |
2382 | BETOH(Perl_my_betoh16,U16) | |
2383 | # else | |
2384 | NOT_AVAIL(Perl_my_betoh16,U16) | |
2385 | # endif | |
2386 | #endif | |
2387 | ||
2388 | #ifdef PERL_NEED_MY_HTOLE32 | |
2389 | # if U32SIZE == 4 | |
2390 | HTOLE(Perl_my_htole32,U32) | |
2391 | # else | |
2392 | NOT_AVAIL(Perl_my_htole32,U32) | |
2393 | # endif | |
2394 | #endif | |
2395 | #ifdef PERL_NEED_MY_LETOH32 | |
2396 | # if U32SIZE == 4 | |
2397 | LETOH(Perl_my_letoh32,U32) | |
2398 | # else | |
2399 | NOT_AVAIL(Perl_my_letoh32,U32) | |
2400 | # endif | |
2401 | #endif | |
2402 | #ifdef PERL_NEED_MY_HTOBE32 | |
2403 | # if U32SIZE == 4 | |
2404 | HTOBE(Perl_my_htobe32,U32) | |
2405 | # else | |
2406 | NOT_AVAIL(Perl_my_htobe32,U32) | |
2407 | # endif | |
2408 | #endif | |
2409 | #ifdef PERL_NEED_MY_BETOH32 | |
2410 | # if U32SIZE == 4 | |
2411 | BETOH(Perl_my_betoh32,U32) | |
2412 | # else | |
2413 | NOT_AVAIL(Perl_my_betoh32,U32) | |
2414 | # endif | |
2415 | #endif | |
2416 | ||
2417 | #ifdef PERL_NEED_MY_HTOLE64 | |
2418 | # if U64SIZE == 8 | |
2419 | HTOLE(Perl_my_htole64,U64) | |
2420 | # else | |
2421 | NOT_AVAIL(Perl_my_htole64,U64) | |
2422 | # endif | |
2423 | #endif | |
2424 | #ifdef PERL_NEED_MY_LETOH64 | |
2425 | # if U64SIZE == 8 | |
2426 | LETOH(Perl_my_letoh64,U64) | |
2427 | # else | |
2428 | NOT_AVAIL(Perl_my_letoh64,U64) | |
2429 | # endif | |
2430 | #endif | |
2431 | #ifdef PERL_NEED_MY_HTOBE64 | |
2432 | # if U64SIZE == 8 | |
2433 | HTOBE(Perl_my_htobe64,U64) | |
2434 | # else | |
2435 | NOT_AVAIL(Perl_my_htobe64,U64) | |
2436 | # endif | |
2437 | #endif | |
2438 | #ifdef PERL_NEED_MY_BETOH64 | |
2439 | # if U64SIZE == 8 | |
2440 | BETOH(Perl_my_betoh64,U64) | |
2441 | # else | |
2442 | NOT_AVAIL(Perl_my_betoh64,U64) | |
2443 | # endif | |
988174c1 | 2444 | #endif |
a687059c | 2445 | |
1109a392 MHM |
2446 | #ifdef PERL_NEED_MY_HTOLES |
2447 | HTOLE(Perl_my_htoles,short) | |
2448 | #endif | |
2449 | #ifdef PERL_NEED_MY_LETOHS | |
2450 | LETOH(Perl_my_letohs,short) | |
2451 | #endif | |
2452 | #ifdef PERL_NEED_MY_HTOBES | |
2453 | HTOBE(Perl_my_htobes,short) | |
2454 | #endif | |
2455 | #ifdef PERL_NEED_MY_BETOHS | |
2456 | BETOH(Perl_my_betohs,short) | |
2457 | #endif | |
2458 | ||
2459 | #ifdef PERL_NEED_MY_HTOLEI | |
2460 | HTOLE(Perl_my_htolei,int) | |
2461 | #endif | |
2462 | #ifdef PERL_NEED_MY_LETOHI | |
2463 | LETOH(Perl_my_letohi,int) | |
2464 | #endif | |
2465 | #ifdef PERL_NEED_MY_HTOBEI | |
2466 | HTOBE(Perl_my_htobei,int) | |
2467 | #endif | |
2468 | #ifdef PERL_NEED_MY_BETOHI | |
2469 | BETOH(Perl_my_betohi,int) | |
2470 | #endif | |
2471 | ||
2472 | #ifdef PERL_NEED_MY_HTOLEL | |
2473 | HTOLE(Perl_my_htolel,long) | |
2474 | #endif | |
2475 | #ifdef PERL_NEED_MY_LETOHL | |
2476 | LETOH(Perl_my_letohl,long) | |
2477 | #endif | |
2478 | #ifdef PERL_NEED_MY_HTOBEL | |
2479 | HTOBE(Perl_my_htobel,long) | |
2480 | #endif | |
2481 | #ifdef PERL_NEED_MY_BETOHL | |
2482 | BETOH(Perl_my_betohl,long) | |
2483 | #endif | |
2484 | ||
2485 | void | |
2486 | Perl_my_swabn(void *ptr, int n) | |
2487 | { | |
2488 | register char *s = (char *)ptr; | |
2489 | register char *e = s + (n-1); | |
2490 | register char tc; | |
2491 | ||
7918f24d NC |
2492 | PERL_ARGS_ASSERT_MY_SWABN; |
2493 | ||
1109a392 MHM |
2494 | for (n /= 2; n > 0; s++, e--, n--) { |
2495 | tc = *s; | |
2496 | *s = *e; | |
2497 | *e = tc; | |
2498 | } | |
2499 | } | |
2500 | ||
4a7d1889 | 2501 | PerlIO * |
c9289b7b | 2502 | Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args) |
4a7d1889 | 2503 | { |
e37778c2 | 2504 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(OS2) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(NETWARE) && !defined(__LIBCATAMOUNT__) |
97aff369 | 2505 | dVAR; |
1f852d0d NIS |
2506 | int p[2]; |
2507 | register I32 This, that; | |
2508 | register Pid_t pid; | |
2509 | SV *sv; | |
2510 | I32 did_pipes = 0; | |
2511 | int pp[2]; | |
2512 | ||
7918f24d NC |
2513 | PERL_ARGS_ASSERT_MY_POPEN_LIST; |
2514 | ||
1f852d0d NIS |
2515 | PERL_FLUSHALL_FOR_CHILD; |
2516 | This = (*mode == 'w'); | |
2517 | that = !This; | |
2518 | if (PL_tainting) { | |
2519 | taint_env(); | |
2520 | taint_proper("Insecure %s%s", "EXEC"); | |
2521 | } | |
2522 | if (PerlProc_pipe(p) < 0) | |
4608196e | 2523 | return NULL; |
1f852d0d NIS |
2524 | /* Try for another pipe pair for error return */ |
2525 | if (PerlProc_pipe(pp) >= 0) | |
2526 | did_pipes = 1; | |
52e18b1f | 2527 | while ((pid = PerlProc_fork()) < 0) { |
1f852d0d NIS |
2528 | if (errno != EAGAIN) { |
2529 | PerlLIO_close(p[This]); | |
4e6dfe71 | 2530 | PerlLIO_close(p[that]); |
1f852d0d NIS |
2531 | if (did_pipes) { |
2532 | PerlLIO_close(pp[0]); | |
2533 | PerlLIO_close(pp[1]); | |
2534 | } | |
4608196e | 2535 | return NULL; |
1f852d0d | 2536 | } |
a2a5de95 | 2537 | Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds"); |
1f852d0d NIS |
2538 | sleep(5); |
2539 | } | |
2540 | if (pid == 0) { | |
2541 | /* Child */ | |
1f852d0d NIS |
2542 | #undef THIS |
2543 | #undef THAT | |
2544 | #define THIS that | |
2545 | #define THAT This | |
1f852d0d NIS |
2546 | /* Close parent's end of error status pipe (if any) */ |
2547 | if (did_pipes) { | |
2548 | PerlLIO_close(pp[0]); | |
2549 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
2550 | /* Close error pipe automatically if exec works */ | |
2551 | fcntl(pp[1], F_SETFD, FD_CLOEXEC); | |
2552 | #endif | |
2553 | } | |
2554 | /* Now dup our end of _the_ pipe to right position */ | |
2555 | if (p[THIS] != (*mode == 'r')) { | |
2556 | PerlLIO_dup2(p[THIS], *mode == 'r'); | |
2557 | PerlLIO_close(p[THIS]); | |
4e6dfe71 GS |
2558 | if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
2559 | PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ | |
1f852d0d | 2560 | } |
4e6dfe71 GS |
2561 | else |
2562 | PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ | |
1f852d0d NIS |
2563 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
2564 | /* No automatic close - do it by hand */ | |
b7953727 JH |
2565 | # ifndef NOFILE |
2566 | # define NOFILE 20 | |
2567 | # endif | |
a080fe3d NIS |
2568 | { |
2569 | int fd; | |
2570 | ||
2571 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) { | |
3aed30dc | 2572 | if (fd != pp[1]) |
a080fe3d NIS |
2573 | PerlLIO_close(fd); |
2574 | } | |
1f852d0d NIS |
2575 | } |
2576 | #endif | |
a0714e2c | 2577 | do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes); |
1f852d0d NIS |
2578 | PerlProc__exit(1); |
2579 | #undef THIS | |
2580 | #undef THAT | |
2581 | } | |
2582 | /* Parent */ | |
52e18b1f | 2583 | do_execfree(); /* free any memory malloced by child on fork */ |
1f852d0d NIS |
2584 | if (did_pipes) |
2585 | PerlLIO_close(pp[1]); | |
2586 | /* Keep the lower of the two fd numbers */ | |
2587 | if (p[that] < p[This]) { | |
2588 | PerlLIO_dup2(p[This], p[that]); | |
2589 | PerlLIO_close(p[This]); | |
2590 | p[This] = p[that]; | |
2591 | } | |
4e6dfe71 GS |
2592 | else |
2593 | PerlLIO_close(p[that]); /* close child's end of pipe */ | |
2594 | ||
1f852d0d | 2595 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
862a34c6 | 2596 | SvUPGRADE(sv,SVt_IV); |
45977657 | 2597 | SvIV_set(sv, pid); |
1f852d0d NIS |
2598 | PL_forkprocess = pid; |
2599 | /* If we managed to get status pipe check for exec fail */ | |
2600 | if (did_pipes && pid > 0) { | |
2601 | int errkid; | |
bb7a0f54 MHM |
2602 | unsigned n = 0; |
2603 | SSize_t n1; | |
1f852d0d NIS |
2604 | |
2605 | while (n < sizeof(int)) { | |
2606 | n1 = PerlLIO_read(pp[0], | |
2607 | (void*)(((char*)&errkid)+n), | |
2608 | (sizeof(int)) - n); | |
2609 | if (n1 <= 0) | |
2610 | break; | |
2611 | n += n1; | |
2612 | } | |
2613 | PerlLIO_close(pp[0]); | |
2614 | did_pipes = 0; | |
2615 | if (n) { /* Error */ | |
2616 | int pid2, status; | |
8c51524e | 2617 | PerlLIO_close(p[This]); |
1f852d0d NIS |
2618 | if (n != sizeof(int)) |
2619 | Perl_croak(aTHX_ "panic: kid popen errno read"); | |
2620 | do { | |
2621 | pid2 = wait4pid(pid, &status, 0); | |
2622 | } while (pid2 == -1 && errno == EINTR); | |
2623 | errno = errkid; /* Propagate errno from kid */ | |
4608196e | 2624 | return NULL; |
1f852d0d NIS |
2625 | } |
2626 | } | |
2627 | if (did_pipes) | |
2628 | PerlLIO_close(pp[0]); | |
2629 | return PerlIO_fdopen(p[This], mode); | |
2630 | #else | |
9d419b5f | 2631 | # ifdef OS2 /* Same, without fork()ing and all extra overhead... */ |
4e205ed6 | 2632 | return my_syspopen4(aTHX_ NULL, mode, n, args); |
9d419b5f | 2633 | # else |
4a7d1889 NIS |
2634 | Perl_croak(aTHX_ "List form of piped open not implemented"); |
2635 | return (PerlIO *) NULL; | |
9d419b5f | 2636 | # endif |
1f852d0d | 2637 | #endif |
4a7d1889 NIS |
2638 | } |
2639 | ||
5f05dabc | 2640 | /* VMS' my_popen() is in VMS.c, same with OS/2. */ |
e37778c2 | 2641 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(__LIBCATAMOUNT__) |
760ac839 | 2642 | PerlIO * |
3dd43144 | 2643 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
a687059c | 2644 | { |
97aff369 | 2645 | dVAR; |
a687059c | 2646 | int p[2]; |
8ac85365 | 2647 | register I32 This, that; |
d8a83dd3 | 2648 | register Pid_t pid; |
79072805 | 2649 | SV *sv; |
bfce84ec | 2650 | const I32 doexec = !(*cmd == '-' && cmd[1] == '\0'); |
e446cec8 IZ |
2651 | I32 did_pipes = 0; |
2652 | int pp[2]; | |
a687059c | 2653 | |
7918f24d NC |
2654 | PERL_ARGS_ASSERT_MY_POPEN; |
2655 | ||
45bc9206 | 2656 | PERL_FLUSHALL_FOR_CHILD; |
ddcf38b7 IZ |
2657 | #ifdef OS2 |
2658 | if (doexec) { | |
23da6c43 | 2659 | return my_syspopen(aTHX_ cmd,mode); |
ddcf38b7 | 2660 | } |
a1d180c4 | 2661 | #endif |
8ac85365 NIS |
2662 | This = (*mode == 'w'); |
2663 | that = !This; | |
3280af22 | 2664 | if (doexec && PL_tainting) { |
bbce6d69 | 2665 | taint_env(); |
2666 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 2667 | } |
c2267164 | 2668 | if (PerlProc_pipe(p) < 0) |
4608196e | 2669 | return NULL; |
e446cec8 IZ |
2670 | if (doexec && PerlProc_pipe(pp) >= 0) |
2671 | did_pipes = 1; | |
52e18b1f | 2672 | while ((pid = PerlProc_fork()) < 0) { |
a687059c | 2673 | if (errno != EAGAIN) { |
6ad3d225 | 2674 | PerlLIO_close(p[This]); |
b5ac89c3 | 2675 | PerlLIO_close(p[that]); |
e446cec8 IZ |
2676 | if (did_pipes) { |
2677 | PerlLIO_close(pp[0]); | |
2678 | PerlLIO_close(pp[1]); | |
2679 | } | |
a687059c | 2680 | if (!doexec) |
b3647a36 | 2681 | Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno)); |
4608196e | 2682 | return NULL; |
a687059c | 2683 | } |
a2a5de95 | 2684 | Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds"); |
a687059c LW |
2685 | sleep(5); |
2686 | } | |
2687 | if (pid == 0) { | |
79072805 LW |
2688 | GV* tmpgv; |
2689 | ||
30ac6d9b GS |
2690 | #undef THIS |
2691 | #undef THAT | |
a687059c | 2692 | #define THIS that |
8ac85365 | 2693 | #define THAT This |
e446cec8 IZ |
2694 | if (did_pipes) { |
2695 | PerlLIO_close(pp[0]); | |
2696 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
2697 | fcntl(pp[1], F_SETFD, FD_CLOEXEC); | |
2698 | #endif | |
2699 | } | |
a687059c | 2700 | if (p[THIS] != (*mode == 'r')) { |
6ad3d225 GS |
2701 | PerlLIO_dup2(p[THIS], *mode == 'r'); |
2702 | PerlLIO_close(p[THIS]); | |
b5ac89c3 NIS |
2703 | if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
2704 | PerlLIO_close(p[THAT]); | |
a687059c | 2705 | } |
b5ac89c3 NIS |
2706 | else |
2707 | PerlLIO_close(p[THAT]); | |
4435c477 | 2708 | #ifndef OS2 |
a687059c | 2709 | if (doexec) { |
a0d0e21e | 2710 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
2711 | #ifndef NOFILE |
2712 | #define NOFILE 20 | |
2713 | #endif | |
a080fe3d | 2714 | { |
3aed30dc | 2715 | int fd; |
a080fe3d NIS |
2716 | |
2717 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) | |
2718 | if (fd != pp[1]) | |
3aed30dc | 2719 | PerlLIO_close(fd); |
a080fe3d | 2720 | } |
ae986130 | 2721 | #endif |
a080fe3d NIS |
2722 | /* may or may not use the shell */ |
2723 | do_exec3(cmd, pp[1], did_pipes); | |
6ad3d225 | 2724 | PerlProc__exit(1); |
a687059c | 2725 | } |
4435c477 | 2726 | #endif /* defined OS2 */ |
713cef20 IZ |
2727 | |
2728 | #ifdef PERLIO_USING_CRLF | |
2729 | /* Since we circumvent IO layers when we manipulate low-level | |
2730 | filedescriptors directly, need to manually switch to the | |
2731 | default, binary, low-level mode; see PerlIOBuf_open(). */ | |
2732 | PerlLIO_setmode((*mode == 'r'), O_BINARY); | |
2733 | #endif | |
2734 | ||
fafc274c | 2735 | if ((tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV))) { |
4d76a344 | 2736 | SvREADONLY_off(GvSV(tmpgv)); |
7766f137 | 2737 | sv_setiv(GvSV(tmpgv), PerlProc_getpid()); |
4d76a344 RGS |
2738 | SvREADONLY_on(GvSV(tmpgv)); |
2739 | } | |
2740 | #ifdef THREADS_HAVE_PIDS | |
2741 | PL_ppid = (IV)getppid(); | |
2742 | #endif | |
3280af22 | 2743 | PL_forkprocess = 0; |
ca0c25f6 | 2744 | #ifdef PERL_USES_PL_PIDSTATUS |
3280af22 | 2745 | hv_clear(PL_pidstatus); /* we have no children */ |
ca0c25f6 | 2746 | #endif |
4608196e | 2747 | return NULL; |
a687059c LW |
2748 | #undef THIS |
2749 | #undef THAT | |
2750 | } | |
b5ac89c3 | 2751 | do_execfree(); /* free any memory malloced by child on vfork */ |
e446cec8 IZ |
2752 | if (did_pipes) |
2753 | PerlLIO_close(pp[1]); | |
8ac85365 | 2754 | if (p[that] < p[This]) { |
6ad3d225 GS |
2755 | PerlLIO_dup2(p[This], p[that]); |
2756 | PerlLIO_close(p[This]); | |
8ac85365 | 2757 | p[This] = p[that]; |
62b28dd9 | 2758 | } |
b5ac89c3 NIS |
2759 | else |
2760 | PerlLIO_close(p[that]); | |
2761 | ||
3280af22 | 2762 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
862a34c6 | 2763 | SvUPGRADE(sv,SVt_IV); |
45977657 | 2764 | SvIV_set(sv, pid); |
3280af22 | 2765 | PL_forkprocess = pid; |
e446cec8 IZ |
2766 | if (did_pipes && pid > 0) { |
2767 | int errkid; | |
bb7a0f54 MHM |
2768 | unsigned n = 0; |
2769 | SSize_t n1; | |
e446cec8 IZ |
2770 | |
2771 | while (n < sizeof(int)) { | |
2772 | n1 = PerlLIO_read(pp[0], | |
2773 | (void*)(((char*)&errkid)+n), | |
2774 | (sizeof(int)) - n); | |
2775 | if (n1 <= 0) | |
2776 | break; | |
2777 | n += n1; | |
2778 | } | |
2f96c702 IZ |
2779 | PerlLIO_close(pp[0]); |
2780 | did_pipes = 0; | |
e446cec8 | 2781 | if (n) { /* Error */ |
faa466a7 | 2782 | int pid2, status; |
8c51524e | 2783 | PerlLIO_close(p[This]); |
e446cec8 | 2784 | if (n != sizeof(int)) |
cea2e8a9 | 2785 | Perl_croak(aTHX_ "panic: kid popen errno read"); |
faa466a7 RG |
2786 | do { |
2787 | pid2 = wait4pid(pid, &status, 0); | |
2788 | } while (pid2 == -1 && errno == EINTR); | |
e446cec8 | 2789 | errno = errkid; /* Propagate errno from kid */ |
4608196e | 2790 | return NULL; |
e446cec8 IZ |
2791 | } |
2792 | } | |
2793 | if (did_pipes) | |
2794 | PerlLIO_close(pp[0]); | |
8ac85365 | 2795 | return PerlIO_fdopen(p[This], mode); |
a687059c | 2796 | } |
7c0587c8 | 2797 | #else |
85ca448a | 2798 | #if defined(atarist) || defined(EPOC) |
7c0587c8 | 2799 | FILE *popen(); |
760ac839 | 2800 | PerlIO * |
cef6ea9d | 2801 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
7c0587c8 | 2802 | { |
7918f24d | 2803 | PERL_ARGS_ASSERT_MY_POPEN; |
45bc9206 | 2804 | PERL_FLUSHALL_FOR_CHILD; |
a1d180c4 NIS |
2805 | /* Call system's popen() to get a FILE *, then import it. |
2806 | used 0 for 2nd parameter to PerlIO_importFILE; | |
2807 | apparently not used | |
2808 | */ | |
2809 | return PerlIO_importFILE(popen(cmd, mode), 0); | |
7c0587c8 | 2810 | } |
2b96b0a5 JH |
2811 | #else |
2812 | #if defined(DJGPP) | |
2813 | FILE *djgpp_popen(); | |
2814 | PerlIO * | |
cef6ea9d | 2815 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
2b96b0a5 JH |
2816 | { |
2817 | PERL_FLUSHALL_FOR_CHILD; | |
2818 | /* Call system's popen() to get a FILE *, then import it. | |
2819 | used 0 for 2nd parameter to PerlIO_importFILE; | |
2820 | apparently not used | |
2821 | */ | |
2822 | return PerlIO_importFILE(djgpp_popen(cmd, mode), 0); | |
2823 | } | |
9c12f1e5 RGS |
2824 | #else |
2825 | #if defined(__LIBCATAMOUNT__) | |
2826 | PerlIO * | |
2827 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) | |
2828 | { | |
2829 | return NULL; | |
2830 | } | |
2831 | #endif | |
2b96b0a5 | 2832 | #endif |
7c0587c8 LW |
2833 | #endif |
2834 | ||
2835 | #endif /* !DOSISH */ | |
a687059c | 2836 | |
52e18b1f GS |
2837 | /* this is called in parent before the fork() */ |
2838 | void | |
2839 | Perl_atfork_lock(void) | |
2840 | { | |
27da23d5 | 2841 | dVAR; |
3db8f154 | 2842 | #if defined(USE_ITHREADS) |
52e18b1f GS |
2843 | /* locks must be held in locking order (if any) */ |
2844 | # ifdef MYMALLOC | |
2845 | MUTEX_LOCK(&PL_malloc_mutex); | |
2846 | # endif | |
2847 | OP_REFCNT_LOCK; | |
2848 | #endif | |
2849 | } | |
2850 | ||
2851 | /* this is called in both parent and child after the fork() */ | |
2852 | void | |
2853 | Perl_atfork_unlock(void) | |
2854 | { | |
27da23d5 | 2855 | dVAR; |
3db8f154 | 2856 | #if defined(USE_ITHREADS) |
52e18b1f GS |
2857 | /* locks must be released in same order as in atfork_lock() */ |
2858 | # ifdef MYMALLOC | |
2859 | MUTEX_UNLOCK(&PL_malloc_mutex); | |
2860 | # endif | |
2861 | OP_REFCNT_UNLOCK; | |
2862 | #endif | |
2863 | } | |
2864 | ||
2865 | Pid_t | |
2866 | Perl_my_fork(void) | |
2867 | { | |
2868 | #if defined(HAS_FORK) | |
2869 | Pid_t pid; | |
3db8f154 | 2870 | #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK) |
52e18b1f GS |
2871 | atfork_lock(); |
2872 | pid = fork(); | |
2873 | atfork_unlock(); | |
2874 | #else | |
2875 | /* atfork_lock() and atfork_unlock() are installed as pthread_atfork() | |
2876 | * handlers elsewhere in the code */ | |
2877 | pid = fork(); | |
2878 | #endif | |
2879 | return pid; | |
2880 | #else | |
2881 | /* this "canna happen" since nothing should be calling here if !HAS_FORK */ | |
2882 | Perl_croak_nocontext("fork() not available"); | |
b961a566 | 2883 | return 0; |
52e18b1f GS |
2884 | #endif /* HAS_FORK */ |
2885 | } | |
2886 | ||
748a9306 | 2887 | #ifdef DUMP_FDS |
35ff7856 | 2888 | void |
c9289b7b | 2889 | Perl_dump_fds(pTHX_ const char *const s) |
ae986130 LW |
2890 | { |
2891 | int fd; | |
c623ac67 | 2892 | Stat_t tmpstatbuf; |
ae986130 | 2893 | |
7918f24d NC |
2894 | PERL_ARGS_ASSERT_DUMP_FDS; |
2895 | ||
bf49b057 | 2896 | PerlIO_printf(Perl_debug_log,"%s", s); |
ae986130 | 2897 | for (fd = 0; fd < 32; fd++) { |
6ad3d225 | 2898 | if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0) |
bf49b057 | 2899 | PerlIO_printf(Perl_debug_log," %d",fd); |
ae986130 | 2900 | } |
bf49b057 | 2901 | PerlIO_printf(Perl_debug_log,"\n"); |
27da23d5 | 2902 | return; |
ae986130 | 2903 | } |
35ff7856 | 2904 | #endif /* DUMP_FDS */ |
ae986130 | 2905 | |
fe14fcc3 | 2906 | #ifndef HAS_DUP2 |
fec02dd3 | 2907 | int |
ba106d47 | 2908 | dup2(int oldfd, int newfd) |
a687059c | 2909 | { |
a0d0e21e | 2910 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
2911 | if (oldfd == newfd) |
2912 | return oldfd; | |
6ad3d225 | 2913 | PerlLIO_close(newfd); |
fec02dd3 | 2914 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 2915 | #else |
fc36a67e | 2916 | #define DUP2_MAX_FDS 256 |
2917 | int fdtmp[DUP2_MAX_FDS]; | |
79072805 | 2918 | I32 fdx = 0; |
ae986130 LW |
2919 | int fd; |
2920 | ||
fe14fcc3 | 2921 | if (oldfd == newfd) |
fec02dd3 | 2922 | return oldfd; |
6ad3d225 | 2923 | PerlLIO_close(newfd); |
fc36a67e | 2924 | /* good enough for low fd's... */ |
6ad3d225 | 2925 | while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) { |
fc36a67e | 2926 | if (fdx >= DUP2_MAX_FDS) { |
6ad3d225 | 2927 | PerlLIO_close(fd); |
fc36a67e | 2928 | fd = -1; |
2929 | break; | |
2930 | } | |
ae986130 | 2931 | fdtmp[fdx++] = fd; |
fc36a67e | 2932 | } |
ae986130 | 2933 | while (fdx > 0) |
6ad3d225 | 2934 | PerlLIO_close(fdtmp[--fdx]); |
fec02dd3 | 2935 | return fd; |
62b28dd9 | 2936 | #endif |
a687059c LW |
2937 | } |
2938 | #endif | |
2939 | ||
64ca3a65 | 2940 | #ifndef PERL_MICRO |
ff68c719 | 2941 | #ifdef HAS_SIGACTION |
2942 | ||
2943 | Sighandler_t | |
864dbfa3 | 2944 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2945 | { |
27da23d5 | 2946 | dVAR; |
ff68c719 | 2947 | struct sigaction act, oact; |
2948 | ||
a10b1e10 JH |
2949 | #ifdef USE_ITHREADS |
2950 | /* only "parent" interpreter can diddle signals */ | |
2951 | if (PL_curinterp != aTHX) | |
8aad04aa | 2952 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
2953 | #endif |
2954 | ||
8aad04aa | 2955 | act.sa_handler = (void(*)(int))handler; |
ff68c719 | 2956 | sigemptyset(&act.sa_mask); |
2957 | act.sa_flags = 0; | |
2958 | #ifdef SA_RESTART | |
4ffa73a3 JH |
2959 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
2960 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
0a8e0eff | 2961 | #endif |
358837b8 | 2962 | #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
8aad04aa | 2963 | if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN) |
85264bed CS |
2964 | act.sa_flags |= SA_NOCLDWAIT; |
2965 | #endif | |
ff68c719 | 2966 | if (sigaction(signo, &act, &oact) == -1) |
8aad04aa | 2967 | return (Sighandler_t) SIG_ERR; |
ff68c719 | 2968 | else |
8aad04aa | 2969 | return (Sighandler_t) oact.sa_handler; |
ff68c719 | 2970 | } |
2971 | ||
2972 | Sighandler_t | |
864dbfa3 | 2973 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2974 | { |
2975 | struct sigaction oact; | |
96a5add6 | 2976 | PERL_UNUSED_CONTEXT; |
ff68c719 | 2977 | |
2978 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
8aad04aa | 2979 | return (Sighandler_t) SIG_ERR; |
ff68c719 | 2980 | else |
8aad04aa | 2981 | return (Sighandler_t) oact.sa_handler; |
ff68c719 | 2982 | } |
2983 | ||
2984 | int | |
864dbfa3 | 2985 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2986 | { |
27da23d5 | 2987 | dVAR; |
ff68c719 | 2988 | struct sigaction act; |
2989 | ||
7918f24d NC |
2990 | PERL_ARGS_ASSERT_RSIGNAL_SAVE; |
2991 | ||
a10b1e10 JH |
2992 | #ifdef USE_ITHREADS |
2993 | /* only "parent" interpreter can diddle signals */ | |
2994 | if (PL_curinterp != aTHX) | |
2995 | return -1; | |
2996 | #endif | |
2997 | ||
8aad04aa | 2998 | act.sa_handler = (void(*)(int))handler; |
ff68c719 | 2999 | sigemptyset(&act.sa_mask); |
3000 | act.sa_flags = 0; | |
3001 | #ifdef SA_RESTART | |
4ffa73a3 JH |
3002 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
3003 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
0a8e0eff | 3004 | #endif |
36b5d377 | 3005 | #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
8aad04aa | 3006 | if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN) |
85264bed CS |
3007 | act.sa_flags |= SA_NOCLDWAIT; |
3008 | #endif | |
ff68c719 | 3009 | return sigaction(signo, &act, save); |
3010 | } | |
3011 | ||
3012 | int | |
864dbfa3 | 3013 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 3014 | { |
27da23d5 | 3015 | dVAR; |
a10b1e10 JH |
3016 | #ifdef USE_ITHREADS |
3017 | /* only "parent" interpreter can diddle signals */ | |
3018 | if (PL_curinterp != aTHX) | |
3019 | return -1; | |
3020 | #endif | |
3021 | ||
ff68c719 | 3022 | return sigaction(signo, save, (struct sigaction *)NULL); |
3023 | } | |
3024 | ||
3025 | #else /* !HAS_SIGACTION */ | |
3026 | ||
3027 | Sighandler_t | |
864dbfa3 | 3028 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 3029 | { |
39f1703b | 3030 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3031 | /* only "parent" interpreter can diddle signals */ |
3032 | if (PL_curinterp != aTHX) | |
8aad04aa | 3033 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
3034 | #endif |
3035 | ||
6ad3d225 | 3036 | return PerlProc_signal(signo, handler); |
ff68c719 | 3037 | } |
3038 | ||
fabdb6c0 | 3039 | static Signal_t |
4e35701f | 3040 | sig_trap(int signo) |
ff68c719 | 3041 | { |
27da23d5 JH |
3042 | dVAR; |
3043 | PL_sig_trapped++; | |
ff68c719 | 3044 | } |
3045 | ||
3046 | Sighandler_t | |
864dbfa3 | 3047 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 3048 | { |
27da23d5 | 3049 | dVAR; |
ff68c719 | 3050 | Sighandler_t oldsig; |
3051 | ||
39f1703b | 3052 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3053 | /* only "parent" interpreter can diddle signals */ |
3054 | if (PL_curinterp != aTHX) | |
8aad04aa | 3055 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
3056 | #endif |
3057 | ||
27da23d5 | 3058 | PL_sig_trapped = 0; |
6ad3d225 GS |
3059 | oldsig = PerlProc_signal(signo, sig_trap); |
3060 | PerlProc_signal(signo, oldsig); | |
27da23d5 | 3061 | if (PL_sig_trapped) |
3aed30dc | 3062 | PerlProc_kill(PerlProc_getpid(), signo); |
ff68c719 | 3063 | return oldsig; |
3064 | } | |
3065 | ||
3066 | int | |
864dbfa3 | 3067 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 3068 | { |
39f1703b | 3069 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3070 | /* only "parent" interpreter can diddle signals */ |
3071 | if (PL_curinterp != aTHX) | |
3072 | return -1; | |
3073 | #endif | |
6ad3d225 | 3074 | *save = PerlProc_signal(signo, handler); |
8aad04aa | 3075 | return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0; |
ff68c719 | 3076 | } |
3077 | ||
3078 | int | |
864dbfa3 | 3079 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 3080 | { |
39f1703b | 3081 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3082 | /* only "parent" interpreter can diddle signals */ |
3083 | if (PL_curinterp != aTHX) | |
3084 | return -1; | |
3085 | #endif | |
8aad04aa | 3086 | return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0; |
ff68c719 | 3087 | } |
3088 | ||
3089 | #endif /* !HAS_SIGACTION */ | |
64ca3a65 | 3090 | #endif /* !PERL_MICRO */ |
ff68c719 | 3091 | |
5f05dabc | 3092 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
e37778c2 | 3093 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(__LIBCATAMOUNT__) |
79072805 | 3094 | I32 |
864dbfa3 | 3095 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a687059c | 3096 | { |
97aff369 | 3097 | dVAR; |
ff68c719 | 3098 | Sigsave_t hstat, istat, qstat; |
a687059c | 3099 | int status; |
a0d0e21e | 3100 | SV **svp; |
d8a83dd3 JH |
3101 | Pid_t pid; |
3102 | Pid_t pid2; | |
03136e13 | 3103 | bool close_failed; |
4ee39169 | 3104 | dSAVEDERRNO; |
a687059c | 3105 | |
3280af22 | 3106 | svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE); |
25d92023 | 3107 | pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1; |
a0d0e21e | 3108 | SvREFCNT_dec(*svp); |
3280af22 | 3109 | *svp = &PL_sv_undef; |
ddcf38b7 IZ |
3110 | #ifdef OS2 |
3111 | if (pid == -1) { /* Opened by popen. */ | |
3112 | return my_syspclose(ptr); | |
3113 | } | |
a1d180c4 | 3114 | #endif |
f1618b10 CS |
3115 | close_failed = (PerlIO_close(ptr) == EOF); |
3116 | SAVE_ERRNO; | |
7c0587c8 | 3117 | #ifdef UTS |
6ad3d225 | 3118 | if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ |
7c0587c8 | 3119 | #endif |
64ca3a65 | 3120 | #ifndef PERL_MICRO |
8aad04aa JH |
3121 | rsignal_save(SIGHUP, (Sighandler_t) SIG_IGN, &hstat); |
3122 | rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &istat); | |
3123 | rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qstat); | |
64ca3a65 | 3124 | #endif |
748a9306 | 3125 | do { |
1d3434b8 GS |
3126 | pid2 = wait4pid(pid, &status, 0); |
3127 | } while (pid2 == -1 && errno == EINTR); | |
64ca3a65 | 3128 | #ifndef PERL_MICRO |
ff68c719 | 3129 | rsignal_restore(SIGHUP, &hstat); |
3130 | rsignal_restore(SIGINT, &istat); | |
3131 | rsignal_restore(SIGQUIT, &qstat); | |
64ca3a65 | 3132 | #endif |
03136e13 | 3133 | if (close_failed) { |
4ee39169 | 3134 | RESTORE_ERRNO; |
03136e13 CS |
3135 | return -1; |
3136 | } | |
1d3434b8 | 3137 | return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)); |
20188a90 | 3138 | } |
9c12f1e5 RGS |
3139 | #else |
3140 | #if defined(__LIBCATAMOUNT__) | |
3141 | I32 | |
3142 | Perl_my_pclose(pTHX_ PerlIO *ptr) | |
3143 | { | |
3144 | return -1; | |
3145 | } | |
3146 | #endif | |
4633a7c4 LW |
3147 | #endif /* !DOSISH */ |
3148 | ||
e37778c2 | 3149 | #if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(__LIBCATAMOUNT__) |
79072805 | 3150 | I32 |
d8a83dd3 | 3151 | Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags) |
20188a90 | 3152 | { |
97aff369 | 3153 | dVAR; |
27da23d5 | 3154 | I32 result = 0; |
7918f24d | 3155 | PERL_ARGS_ASSERT_WAIT4PID; |
b7953727 JH |
3156 | if (!pid) |
3157 | return -1; | |
ca0c25f6 | 3158 | #ifdef PERL_USES_PL_PIDSTATUS |
b7953727 | 3159 | { |
3aed30dc | 3160 | if (pid > 0) { |
12072db5 NC |
3161 | /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the |
3162 | pid, rather than a string form. */ | |
c4420975 | 3163 | SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE); |
3aed30dc HS |
3164 | if (svp && *svp != &PL_sv_undef) { |
3165 | *statusp = SvIVX(*svp); | |
12072db5 NC |
3166 | (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t), |
3167 | G_DISCARD); | |
3aed30dc HS |
3168 | return pid; |
3169 | } | |
3170 | } | |
3171 | else { | |
3172 | HE *entry; | |
3173 | ||
3174 | hv_iterinit(PL_pidstatus); | |
3175 | if ((entry = hv_iternext(PL_pidstatus))) { | |
c4420975 | 3176 | SV * const sv = hv_iterval(PL_pidstatus,entry); |
7ea75b61 | 3177 | I32 len; |
0bcc34c2 | 3178 | const char * const spid = hv_iterkey(entry,&len); |
27da23d5 | 3179 | |
12072db5 NC |
3180 | assert (len == sizeof(Pid_t)); |
3181 | memcpy((char *)&pid, spid, len); | |
3aed30dc | 3182 | *statusp = SvIVX(sv); |
7b9a3241 NC |
3183 | /* The hash iterator is currently on this entry, so simply |
3184 | calling hv_delete would trigger the lazy delete, which on | |
3185 | aggregate does more work, beacuse next call to hv_iterinit() | |
3186 | would spot the flag, and have to call the delete routine, | |
3187 | while in the meantime any new entries can't re-use that | |
3188 | memory. */ | |
3189 | hv_iterinit(PL_pidstatus); | |
7ea75b61 | 3190 | (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD); |
3aed30dc HS |
3191 | return pid; |
3192 | } | |
20188a90 LW |
3193 | } |
3194 | } | |
68a29c53 | 3195 | #endif |
79072805 | 3196 | #ifdef HAS_WAITPID |
367f3c24 IZ |
3197 | # ifdef HAS_WAITPID_RUNTIME |
3198 | if (!HAS_WAITPID_RUNTIME) | |
3199 | goto hard_way; | |
3200 | # endif | |
cddd4526 | 3201 | result = PerlProc_waitpid(pid,statusp,flags); |
dfcfdb64 | 3202 | goto finish; |
367f3c24 IZ |
3203 | #endif |
3204 | #if !defined(HAS_WAITPID) && defined(HAS_WAIT4) | |
4608196e | 3205 | result = wait4((pid==-1)?0:pid,statusp,flags,NULL); |
dfcfdb64 | 3206 | goto finish; |
367f3c24 | 3207 | #endif |
ca0c25f6 | 3208 | #ifdef PERL_USES_PL_PIDSTATUS |
27da23d5 | 3209 | #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME) |
367f3c24 | 3210 | hard_way: |
27da23d5 | 3211 | #endif |
a0d0e21e | 3212 | { |
a0d0e21e | 3213 | if (flags) |
cea2e8a9 | 3214 | Perl_croak(aTHX_ "Can't do waitpid with flags"); |
a0d0e21e | 3215 | else { |
76e3520e | 3216 | while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0) |
a0d0e21e LW |
3217 | pidgone(result,*statusp); |
3218 | if (result < 0) | |
3219 | *statusp = -1; | |
3220 | } | |
a687059c LW |
3221 | } |
3222 | #endif | |
27da23d5 | 3223 | #if defined(HAS_WAITPID) || defined(HAS_WAIT4) |
dfcfdb64 | 3224 | finish: |
27da23d5 | 3225 | #endif |
cddd4526 NIS |
3226 | if (result < 0 && errno == EINTR) { |
3227 | PERL_ASYNC_CHECK(); | |
48dbb59e | 3228 | errno = EINTR; /* reset in case a signal handler changed $! */ |
cddd4526 NIS |
3229 | } |
3230 | return result; | |
a687059c | 3231 | } |
2986a63f | 3232 | #endif /* !DOSISH || OS2 || WIN32 || NETWARE */ |
a687059c | 3233 | |
ca0c25f6 | 3234 | #ifdef PERL_USES_PL_PIDSTATUS |
7c0587c8 | 3235 | void |
ed4173ef | 3236 | S_pidgone(pTHX_ Pid_t pid, int status) |
a687059c | 3237 | { |
79072805 | 3238 | register SV *sv; |
a687059c | 3239 | |
12072db5 | 3240 | sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE); |
862a34c6 | 3241 | SvUPGRADE(sv,SVt_IV); |
45977657 | 3242 | SvIV_set(sv, status); |
20188a90 | 3243 | return; |
a687059c | 3244 | } |
ca0c25f6 | 3245 | #endif |
a687059c | 3246 | |
85ca448a | 3247 | #if defined(atarist) || defined(OS2) || defined(EPOC) |
7c0587c8 | 3248 | int pclose(); |
ddcf38b7 IZ |
3249 | #ifdef HAS_FORK |
3250 | int /* Cannot prototype with I32 | |
3251 | in os2ish.h. */ | |
ba106d47 | 3252 | my_syspclose(PerlIO *ptr) |
ddcf38b7 | 3253 | #else |
79072805 | 3254 | I32 |
864dbfa3 | 3255 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a1d180c4 | 3256 | #endif |
a687059c | 3257 | { |
760ac839 | 3258 | /* Needs work for PerlIO ! */ |
c4420975 | 3259 | FILE * const f = PerlIO_findFILE(ptr); |
7452cf6a | 3260 | const I32 result = pclose(f); |
2b96b0a5 JH |
3261 | PerlIO_releaseFILE(ptr,f); |
3262 | return result; | |
3263 | } | |
3264 | #endif | |
3265 | ||
933fea7f | 3266 | #if defined(DJGPP) |
2b96b0a5 JH |
3267 | int djgpp_pclose(); |
3268 | I32 | |
3269 | Perl_my_pclose(pTHX_ PerlIO *ptr) | |
3270 | { | |
3271 | /* Needs work for PerlIO ! */ | |
c4420975 | 3272 | FILE * const f = PerlIO_findFILE(ptr); |
2b96b0a5 | 3273 | I32 result = djgpp_pclose(f); |
933fea7f | 3274 | result = (result << 8) & 0xff00; |
760ac839 LW |
3275 | PerlIO_releaseFILE(ptr,f); |
3276 | return result; | |
a687059c | 3277 | } |
7c0587c8 | 3278 | #endif |
9f68db38 | 3279 | |
16fa5c11 | 3280 | #define PERL_REPEATCPY_LINEAR 4 |
9f68db38 | 3281 | void |
16fa5c11 | 3282 | Perl_repeatcpy(register char *to, register const char *from, I32 len, register I32 count) |
9f68db38 | 3283 | { |
7918f24d NC |
3284 | PERL_ARGS_ASSERT_REPEATCPY; |
3285 | ||
16fa5c11 VP |
3286 | if (len == 1) |
3287 | memset(to, *from, count); | |
3288 | else if (count) { | |
3289 | register char *p = to; | |
3290 | I32 items, linear, half; | |
3291 | ||
3292 | linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR; | |
3293 | for (items = 0; items < linear; ++items) { | |
3294 | register const char *q = from; | |
3295 | I32 todo; | |
3296 | for (todo = len; todo > 0; todo--) | |
3297 | *p++ = *q++; | |
3298 | } | |
3299 | ||
3300 | half = count / 2; | |
3301 | while (items <= half) { | |
3302 | I32 size = items * len; | |
3303 | memcpy(p, to, size); | |
3304 | p += size; | |
3305 | items *= 2; | |
9f68db38 | 3306 | } |
16fa5c11 VP |
3307 | |
3308 | if (count > items) | |
3309 | memcpy(p, to, (count - items) * len); | |
9f68db38 LW |
3310 | } |
3311 | } | |
0f85fab0 | 3312 | |
fe14fcc3 | 3313 | #ifndef HAS_RENAME |
79072805 | 3314 | I32 |
4373e329 | 3315 | Perl_same_dirent(pTHX_ const char *a, const char *b) |
62b28dd9 | 3316 | { |
93a17b20 LW |
3317 | char *fa = strrchr(a,'/'); |
3318 | char *fb = strrchr(b,'/'); | |
c623ac67 GS |
3319 | Stat_t tmpstatbuf1; |
3320 | Stat_t tmpstatbuf2; | |
c4420975 | 3321 | SV * const tmpsv = sv_newmortal(); |
62b28dd9 | 3322 | |
7918f24d NC |
3323 | PERL_ARGS_ASSERT_SAME_DIRENT; |
3324 | ||
62b28dd9 LW |
3325 | if (fa) |
3326 | fa++; | |
3327 | else | |
3328 | fa = a; | |
3329 | if (fb) | |
3330 | fb++; | |
3331 | else | |
3332 | fb = b; | |
3333 | if (strNE(a,b)) | |
3334 | return FALSE; | |
3335 | if (fa == a) | |
76f68e9b | 3336 | sv_setpvs(tmpsv, "."); |
62b28dd9 | 3337 | else |
46fc3d4c | 3338 | sv_setpvn(tmpsv, a, fa - a); |
95a20fc0 | 3339 | if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0) |
62b28dd9 LW |
3340 | return FALSE; |
3341 | if (fb == b) | |
76f68e9b | 3342 | sv_setpvs(tmpsv, "."); |
62b28dd9 | 3343 | else |
46fc3d4c | 3344 | sv_setpvn(tmpsv, b, fb - b); |
95a20fc0 | 3345 | if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0) |
62b28dd9 LW |
3346 | return FALSE; |
3347 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
3348 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
3349 | } | |
fe14fcc3 LW |
3350 | #endif /* !HAS_RENAME */ |
3351 | ||
491527d0 | 3352 | char* |
7f315aed NC |
3353 | Perl_find_script(pTHX_ const char *scriptname, bool dosearch, |
3354 | const char *const *const search_ext, I32 flags) | |
491527d0 | 3355 | { |
97aff369 | 3356 | dVAR; |
bd61b366 SS |
3357 | const char *xfound = NULL; |
3358 | char *xfailed = NULL; | |
0f31cffe | 3359 | char tmpbuf[MAXPATHLEN]; |
491527d0 | 3360 | register char *s; |
5f74f29c | 3361 | I32 len = 0; |
491527d0 | 3362 | int retval; |
39a02377 | 3363 | char *bufend; |
491527d0 GS |
3364 | #if defined(DOSISH) && !defined(OS2) && !defined(atarist) |
3365 | # define SEARCH_EXTS ".bat", ".cmd", NULL | |
3366 | # define MAX_EXT_LEN 4 | |
3367 | #endif | |
3368 | #ifdef OS2 | |
3369 | # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL | |
3370 | # define MAX_EXT_LEN 4 | |
3371 | #endif | |
3372 | #ifdef VMS | |
3373 | # define SEARCH_EXTS ".pl", ".com", NULL | |
3374 | # define MAX_EXT_LEN 4 | |
3375 | #endif | |
3376 | /* additional extensions to try in each dir if scriptname not found */ | |
3377 | #ifdef SEARCH_EXTS | |
0bcc34c2 | 3378 | static const char *const exts[] = { SEARCH_EXTS }; |
7f315aed | 3379 | const char *const *const ext = search_ext ? search_ext : exts; |
491527d0 | 3380 | int extidx = 0, i = 0; |
bd61b366 | 3381 | const char *curext = NULL; |
491527d0 | 3382 | #else |
53c1dcc0 | 3383 | PERL_UNUSED_ARG(search_ext); |
491527d0 GS |
3384 | # define MAX_EXT_LEN 0 |
3385 | #endif | |
3386 | ||
7918f24d NC |
3387 | PERL_ARGS_ASSERT_FIND_SCRIPT; |
3388 | ||
491527d0 GS |
3389 | /* |
3390 | * If dosearch is true and if scriptname does not contain path | |
3391 | * delimiters, search the PATH for scriptname. | |
3392 | * | |
3393 | * If SEARCH_EXTS is also defined, will look for each | |
3394 | * scriptname{SEARCH_EXTS} whenever scriptname is not found | |
3395 | * while searching the PATH. | |
3396 | * | |
3397 | * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search | |
3398 | * proceeds as follows: | |
3399 | * If DOSISH or VMSISH: | |
3400 | * + look for ./scriptname{,.foo,.bar} | |
3401 | * + search the PATH for scriptname{,.foo,.bar} | |
3402 | * | |
3403 | * If !DOSISH: | |
3404 | * + look *only* in the PATH for scriptname{,.foo,.bar} (note | |
3405 | * this will not look in '.' if it's not in the PATH) | |
3406 | */ | |
84486fc6 | 3407 | tmpbuf[0] = '\0'; |
491527d0 GS |
3408 | |
3409 | #ifdef VMS | |
3410 | # ifdef ALWAYS_DEFTYPES | |
3411 | len = strlen(scriptname); | |
3412 | if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') { | |
c4420975 | 3413 | int idx = 0, deftypes = 1; |
491527d0 GS |
3414 | bool seen_dot = 1; |
3415 | ||
bd61b366 | 3416 | const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL); |
491527d0 GS |
3417 | # else |
3418 | if (dosearch) { | |
c4420975 | 3419 | int idx = 0, deftypes = 1; |
491527d0 GS |
3420 | bool seen_dot = 1; |
3421 | ||
bd61b366 | 3422 | const int hasdir = (strpbrk(scriptname,":[</") != NULL); |
491527d0 GS |
3423 | # endif |
3424 | /* The first time through, just add SEARCH_EXTS to whatever we | |
3425 | * already have, so we can check for default file types. */ | |
3426 | while (deftypes || | |
84486fc6 | 3427 | (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) ) |
491527d0 GS |
3428 | { |
3429 | if (deftypes) { | |
3430 | deftypes = 0; | |
84486fc6 | 3431 | *tmpbuf = '\0'; |
491527d0 | 3432 | } |
84486fc6 GS |
3433 | if ((strlen(tmpbuf) + strlen(scriptname) |
3434 | + MAX_EXT_LEN) >= sizeof tmpbuf) | |
491527d0 | 3435 | continue; /* don't search dir with too-long name */ |
6fca0082 | 3436 | my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf)); |
491527d0 GS |
3437 | #else /* !VMS */ |
3438 | ||
3439 | #ifdef DOSISH | |
3440 | if (strEQ(scriptname, "-")) | |
3441 | dosearch = 0; | |
3442 | if (dosearch) { /* Look in '.' first. */ | |
fe2774ed | 3443 | const char *cur = scriptname; |
491527d0 GS |
3444 | #ifdef SEARCH_EXTS |
3445 | if ((curext = strrchr(scriptname,'.'))) /* possible current ext */ | |
3446 | while (ext[i]) | |
3447 | if (strEQ(ext[i++],curext)) { | |
3448 | extidx = -1; /* already has an ext */ | |
3449 | break; | |
3450 | } | |
3451 | do { | |
3452 | #endif | |
3453 | DEBUG_p(PerlIO_printf(Perl_debug_log, | |
3454 | "Looking for %s\n",cur)); | |
017f25f1 IZ |
3455 | if (PerlLIO_stat(cur,&PL_statbuf) >= 0 |
3456 | && !S_ISDIR(PL_statbuf.st_mode)) { | |
491527d0 GS |
3457 | dosearch = 0; |
3458 | scriptname = cur; | |
3459 | #ifdef SEARCH_EXTS | |
3460 | break; | |
3461 | #endif | |
3462 | } | |
3463 | #ifdef SEARCH_EXTS | |
3464 | if (cur == scriptname) { | |
3465 | len = strlen(scriptname); | |
84486fc6 | 3466 | if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf)) |
491527d0 | 3467 | break; |
9e4425f7 SH |
3468 | my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf)); |
3469 | cur = tmpbuf; | |
491527d0 GS |
3470 | } |
3471 | } while (extidx >= 0 && ext[extidx] /* try an extension? */ | |
6fca0082 | 3472 | && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)); |
491527d0 GS |
3473 | #endif |
3474 | } | |
3475 | #endif | |
3476 | ||
3477 | if (dosearch && !strchr(scriptname, '/') | |
3478 | #ifdef DOSISH | |
3479 | && !strchr(scriptname, '\\') | |
3480 | #endif | |
cd39f2b6 | 3481 | && (s = PerlEnv_getenv("PATH"))) |
cd39f2b6 | 3482 | { |
491527d0 | 3483 | bool seen_dot = 0; |
92f0c265 | 3484 | |
39a02377 DM |
3485 | bufend = s + strlen(s); |
3486 | while (s < bufend) { | |
491527d0 GS |
3487 | #if defined(atarist) || defined(DOSISH) |
3488 | for (len = 0; *s | |
3489 | # ifdef atarist | |
3490 | && *s != ',' | |
3491 | # endif | |
3492 | && *s != ';'; len++, s++) { | |
84486fc6 GS |
3493 | if (len < sizeof tmpbuf) |
3494 | tmpbuf[len] = *s; | |
491527d0 | 3495 | } |
84486fc6 GS |
3496 | if (len < sizeof tmpbuf) |
3497 | tmpbuf[len] = '\0'; | |
491527d0 | 3498 | #else /* ! (atarist || DOSISH) */ |
39a02377 | 3499 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend, |
491527d0 GS |
3500 | ':', |
3501 | &len); | |
3502 | #endif /* ! (atarist || DOSISH) */ | |
39a02377 | 3503 | if (s < bufend) |
491527d0 | 3504 | s++; |
84486fc6 | 3505 | if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf) |
491527d0 GS |
3506 | continue; /* don't search dir with too-long name */ |
3507 | if (len | |
cd86ed9d | 3508 | # if defined(atarist) || defined(DOSISH) |
84486fc6 GS |
3509 | && tmpbuf[len - 1] != '/' |
3510 | && tmpbuf[len - 1] != '\\' | |
490a0e98 | 3511 | # endif |
491527d0 | 3512 | ) |
84486fc6 GS |
3513 | tmpbuf[len++] = '/'; |
3514 | if (len == 2 && tmpbuf[0] == '.') | |
491527d0 | 3515 | seen_dot = 1; |
28f0d0ec | 3516 | (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len); |
491527d0 GS |
3517 | #endif /* !VMS */ |
3518 | ||
3519 | #ifdef SEARCH_EXTS | |
84486fc6 | 3520 | len = strlen(tmpbuf); |
491527d0 GS |
3521 | if (extidx > 0) /* reset after previous loop */ |
3522 | extidx = 0; | |
3523 | do { | |
3524 | #endif | |
84486fc6 | 3525 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf)); |
3280af22 | 3526 | retval = PerlLIO_stat(tmpbuf,&PL_statbuf); |
017f25f1 IZ |
3527 | if (S_ISDIR(PL_statbuf.st_mode)) { |
3528 | retval = -1; | |
3529 | } | |
491527d0 GS |
3530 | #ifdef SEARCH_EXTS |
3531 | } while ( retval < 0 /* not there */ | |
3532 | && extidx>=0 && ext[extidx] /* try an extension? */ | |
6fca0082 | 3533 | && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len) |
491527d0 GS |
3534 | ); |
3535 | #endif | |
3536 | if (retval < 0) | |
3537 | continue; | |
3280af22 NIS |
3538 | if (S_ISREG(PL_statbuf.st_mode) |
3539 | && cando(S_IRUSR,TRUE,&PL_statbuf) | |
e37778c2 | 3540 | #if !defined(DOSISH) |
3280af22 | 3541 | && cando(S_IXUSR,TRUE,&PL_statbuf) |
491527d0 GS |
3542 | #endif |
3543 | ) | |
3544 | { | |
3aed30dc | 3545 | xfound = tmpbuf; /* bingo! */ |
491527d0 GS |
3546 | break; |
3547 | } | |
3548 | if (!xfailed) | |
84486fc6 | 3549 | xfailed = savepv(tmpbuf); |
491527d0 GS |
3550 | } |
3551 | #ifndef DOSISH | |
017f25f1 | 3552 | if (!xfound && !seen_dot && !xfailed && |
a1d180c4 | 3553 | (PerlLIO_stat(scriptname,&PL_statbuf) < 0 |
017f25f1 | 3554 | || S_ISDIR(PL_statbuf.st_mode))) |
491527d0 GS |
3555 | #endif |
3556 | seen_dot = 1; /* Disable message. */ | |
9ccb31f9 GS |
3557 | if (!xfound) { |
3558 | if (flags & 1) { /* do or die? */ | |
3aed30dc | 3559 | Perl_croak(aTHX_ "Can't %s %s%s%s", |
9ccb31f9 GS |
3560 | (xfailed ? "execute" : "find"), |
3561 | (xfailed ? xfailed : scriptname), | |
3562 | (xfailed ? "" : " on PATH"), | |
3563 | (xfailed || seen_dot) ? "" : ", '.' not in PATH"); | |
3564 | } | |
bd61b366 | 3565 | scriptname = NULL; |
9ccb31f9 | 3566 | } |
43c5f42d | 3567 | Safefree(xfailed); |
491527d0 GS |
3568 | scriptname = xfound; |
3569 | } | |
bd61b366 | 3570 | return (scriptname ? savepv(scriptname) : NULL); |
491527d0 GS |
3571 | } |
3572 | ||
ba869deb GS |
3573 | #ifndef PERL_GET_CONTEXT_DEFINED |
3574 | ||
3575 | void * | |
3576 | Perl_get_context(void) | |
3577 | { | |
27da23d5 | 3578 | dVAR; |
3db8f154 | 3579 | #if defined(USE_ITHREADS) |
ba869deb GS |
3580 | # ifdef OLD_PTHREADS_API |
3581 | pthread_addr_t t; | |
3582 | if (pthread_getspecific(PL_thr_key, &t)) | |
3583 | Perl_croak_nocontext("panic: pthread_getspecific"); | |
3584 | return (void*)t; | |
3585 | # else | |
bce813aa | 3586 | # ifdef I_MACH_CTHREADS |
8b8b35ab | 3587 | return (void*)cthread_data(cthread_self()); |
bce813aa | 3588 | # else |
8b8b35ab JH |
3589 | return (void*)PTHREAD_GETSPECIFIC(PL_thr_key); |
3590 | # endif | |
c44d3fdb | 3591 | # endif |
ba869deb GS |
3592 | #else |
3593 | return (void*)NULL; | |
3594 | #endif | |
3595 | } | |
3596 | ||
3597 | void | |
3598 | Perl_set_context(void *t) | |
3599 | { | |
8772537c | 3600 | dVAR; |
7918f24d | 3601 | PERL_ARGS_ASSERT_SET_CONTEXT; |
3db8f154 | 3602 | #if defined(USE_ITHREADS) |
c44d3fdb GS |
3603 | # ifdef I_MACH_CTHREADS |
3604 | cthread_set_data(cthread_self(), t); | |
3605 | # else | |
ba869deb GS |
3606 | if (pthread_setspecific(PL_thr_key, t)) |
3607 | Perl_croak_nocontext("panic: pthread_setspecific"); | |
c44d3fdb | 3608 | # endif |
b464bac0 | 3609 | #else |
8772537c | 3610 | PERL_UNUSED_ARG(t); |
ba869deb GS |
3611 | #endif |
3612 | } | |
3613 | ||
3614 | #endif /* !PERL_GET_CONTEXT_DEFINED */ | |
491527d0 | 3615 | |
27da23d5 | 3616 | #if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GLOBAL_STRUCT_PRIVATE) |
22239a37 | 3617 | struct perl_vars * |
864dbfa3 | 3618 | Perl_GetVars(pTHX) |
22239a37 | 3619 | { |
533c011a | 3620 | return &PL_Vars; |
22239a37 | 3621 | } |
31fb1209 NIS |
3622 | #endif |
3623 | ||
1cb0ed9b | 3624 | char ** |
864dbfa3 | 3625 | Perl_get_op_names(pTHX) |
31fb1209 | 3626 | { |
96a5add6 AL |
3627 | PERL_UNUSED_CONTEXT; |
3628 | return (char **)PL_op_name; | |
31fb1209 NIS |
3629 | } |
3630 | ||
1cb0ed9b | 3631 | char ** |
864dbfa3 | 3632 | Perl_get_op_descs(pTHX) |
31fb1209 | 3633 | { |
96a5add6 AL |
3634 | PERL_UNUSED_CONTEXT; |
3635 | return (char **)PL_op_desc; | |
31fb1209 | 3636 | } |
9e6b2b00 | 3637 | |
e1ec3a88 | 3638 | const char * |
864dbfa3 | 3639 | Perl_get_no_modify(pTHX) |
9e6b2b00 | 3640 | { |
96a5add6 AL |
3641 | PERL_UNUSED_CONTEXT; |
3642 | return PL_no_modify; | |
9e6b2b00 GS |
3643 | } |
3644 | ||
3645 | U32 * | |
864dbfa3 | 3646 | Perl_get_opargs(pTHX) |
9e6b2b00 | 3647 | { |
96a5add6 AL |
3648 | PERL_UNUSED_CONTEXT; |
3649 | return (U32 *)PL_opargs; | |
9e6b2b00 | 3650 | } |
51aa15f3 | 3651 | |
0cb96387 GS |
3652 | PPADDR_t* |
3653 | Perl_get_ppaddr(pTHX) | |
3654 | { | |
96a5add6 AL |
3655 | dVAR; |
3656 | PERL_UNUSED_CONTEXT; | |
3657 | return (PPADDR_t*)PL_ppaddr; | |
0cb96387 GS |
3658 | } |
3659 | ||
a6c40364 GS |
3660 | #ifndef HAS_GETENV_LEN |
3661 | char * | |
bf4acbe4 | 3662 | Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len) |
a6c40364 | 3663 | { |
8772537c | 3664 | char * const env_trans = PerlEnv_getenv(env_elem); |
96a5add6 | 3665 | PERL_UNUSED_CONTEXT; |
7918f24d | 3666 | PERL_ARGS_ASSERT_GETENV_LEN; |
a6c40364 GS |
3667 | if (env_trans) |
3668 | *len = strlen(env_trans); | |
3669 | return env_trans; | |
f675dbe5 CB |
3670 | } |
3671 | #endif | |
3672 | ||
dc9e4912 GS |
3673 | |
3674 | MGVTBL* | |
864dbfa3 | 3675 | Perl_get_vtbl(pTHX_ int vtbl_id) |
dc9e4912 | 3676 | { |
7452cf6a | 3677 | const MGVTBL* result; |
96a5add6 | 3678 | PERL_UNUSED_CONTEXT; |
dc9e4912 GS |
3679 | |
3680 | switch(vtbl_id) { | |
3681 | case want_vtbl_sv: | |
3682 | result = &PL_vtbl_sv; | |
3683 | break; | |
3684 | case want_vtbl_env: | |
3685 | result = &PL_vtbl_env; | |
3686 | break; | |
3687 | case want_vtbl_envelem: | |
3688 | result = &PL_vtbl_envelem; | |
3689 | break; | |
3690 | case want_vtbl_sig: | |
3691 | result = &PL_vtbl_sig; | |
3692 | break; | |
3693 | case want_vtbl_sigelem: | |
3694 | result = &PL_vtbl_sigelem; | |
3695 | break; | |
3696 | case want_vtbl_pack: | |
3697 | result = &PL_vtbl_pack; | |
3698 | break; | |
3699 | case want_vtbl_packelem: | |
3700 | result = &PL_vtbl_packelem; | |
3701 | break; | |
3702 | case want_vtbl_dbline: | |
3703 | result = &PL_vtbl_dbline; | |
3704 | break; | |
3705 | case want_vtbl_isa: | |
3706 | result = &PL_vtbl_isa; | |
3707 | break; | |
3708 | case want_vtbl_isaelem: | |
3709 | result = &PL_vtbl_isaelem; | |
3710 | break; | |
3711 | case want_vtbl_arylen: | |
3712 | result = &PL_vtbl_arylen; | |
3713 | break; | |
dc9e4912 GS |
3714 | case want_vtbl_mglob: |
3715 | result = &PL_vtbl_mglob; | |
3716 | break; | |
3717 | case want_vtbl_nkeys: | |
3718 | result = &PL_vtbl_nkeys; | |
3719 | break; | |
3720 | case want_vtbl_taint: | |
3721 | result = &PL_vtbl_taint; | |
3722 | break; | |
3723 | case want_vtbl_substr: | |
3724 | result = &PL_vtbl_substr; | |
3725 | break; | |
3726 | case want_vtbl_vec: | |
3727 | result = &PL_vtbl_vec; | |
3728 | break; | |
3729 | case want_vtbl_pos: | |
3730 | result = &PL_vtbl_pos; | |
3731 | break; | |
3732 | case want_vtbl_bm: | |
3733 | result = &PL_vtbl_bm; | |
3734 | break; | |
3735 | case want_vtbl_fm: | |
3736 | result = &PL_vtbl_fm; | |
3737 | break; | |
3738 | case want_vtbl_uvar: | |
3739 | result = &PL_vtbl_uvar; | |
3740 | break; | |
dc9e4912 GS |
3741 | case want_vtbl_defelem: |
3742 | result = &PL_vtbl_defelem; | |
3743 | break; | |
3744 | case want_vtbl_regexp: | |
3745 | result = &PL_vtbl_regexp; | |
3746 | break; | |
3747 | case want_vtbl_regdata: | |
3748 | result = &PL_vtbl_regdata; | |
3749 | break; | |
3750 | case want_vtbl_regdatum: | |
3751 | result = &PL_vtbl_regdatum; | |
3752 | break; | |
3c90161d | 3753 | #ifdef USE_LOCALE_COLLATE |
dc9e4912 GS |
3754 | case want_vtbl_collxfrm: |
3755 | result = &PL_vtbl_collxfrm; | |
3756 | break; | |
3c90161d | 3757 | #endif |
dc9e4912 GS |
3758 | case want_vtbl_amagic: |
3759 | result = &PL_vtbl_amagic; | |
3760 | break; | |
3761 | case want_vtbl_amagicelem: | |
3762 | result = &PL_vtbl_amagicelem; | |
3763 | break; | |
810b8aa5 GS |
3764 | case want_vtbl_backref: |
3765 | result = &PL_vtbl_backref; | |
3766 | break; | |
7e8c5dac HS |
3767 | case want_vtbl_utf8: |
3768 | result = &PL_vtbl_utf8; | |
3769 | break; | |
7452cf6a | 3770 | default: |
4608196e | 3771 | result = NULL; |
7452cf6a | 3772 | break; |
dc9e4912 | 3773 | } |
27da23d5 | 3774 | return (MGVTBL*)result; |
dc9e4912 GS |
3775 | } |
3776 | ||
767df6a1 | 3777 | I32 |
864dbfa3 | 3778 | Perl_my_fflush_all(pTHX) |
767df6a1 | 3779 | { |
f800e14d | 3780 | #if defined(USE_PERLIO) || defined(FFLUSH_NULL) || defined(USE_SFIO) |
ce720889 | 3781 | return PerlIO_flush(NULL); |
767df6a1 | 3782 | #else |
8fbdfb7c | 3783 | # if defined(HAS__FWALK) |
f13a2bc0 | 3784 | extern int fflush(FILE *); |
74cac757 JH |
3785 | /* undocumented, unprototyped, but very useful BSDism */ |
3786 | extern void _fwalk(int (*)(FILE *)); | |
8fbdfb7c | 3787 | _fwalk(&fflush); |
74cac757 | 3788 | return 0; |
8fa7f367 | 3789 | # else |
8fbdfb7c | 3790 | # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY) |
8fa7f367 | 3791 | long open_max = -1; |
8fbdfb7c | 3792 | # ifdef PERL_FFLUSH_ALL_FOPEN_MAX |
d2201af2 | 3793 | open_max = PERL_FFLUSH_ALL_FOPEN_MAX; |
8fbdfb7c | 3794 | # else |
8fa7f367 | 3795 | # if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX) |
767df6a1 | 3796 | open_max = sysconf(_SC_OPEN_MAX); |
8fa7f367 JH |
3797 | # else |
3798 | # ifdef FOPEN_MAX | |
74cac757 | 3799 | open_max = FOPEN_MAX; |
8fa7f367 JH |
3800 | # else |
3801 | # ifdef OPEN_MAX | |
74cac757 | 3802 | open_max = OPEN_MAX; |
8fa7f367 JH |
3803 | # else |
3804 | # ifdef _NFILE | |
d2201af2 | 3805 | open_max = _NFILE; |
8fa7f367 JH |
3806 | # endif |
3807 | # endif | |
74cac757 | 3808 | # endif |
767df6a1 JH |
3809 | # endif |
3810 | # endif | |
767df6a1 JH |
3811 | if (open_max > 0) { |
3812 | long i; | |
3813 | for (i = 0; i < open_max; i++) | |
d2201af2 AD |
3814 | if (STDIO_STREAM_ARRAY[i]._file >= 0 && |
3815 | STDIO_STREAM_ARRAY[i]._file < open_max && | |
3816 | STDIO_STREAM_ARRAY[i]._flag) | |
3817 | PerlIO_flush(&STDIO_STREAM_ARRAY[i]); | |
767df6a1 JH |
3818 | return 0; |
3819 | } | |
8fbdfb7c | 3820 | # endif |
93189314 | 3821 | SETERRNO(EBADF,RMS_IFI); |
767df6a1 | 3822 | return EOF; |
74cac757 | 3823 | # endif |
767df6a1 JH |
3824 | #endif |
3825 | } | |
097ee67d | 3826 | |
69282e91 | 3827 | void |
e1ec3a88 | 3828 | Perl_report_evil_fh(pTHX_ const GV *gv, const IO *io, I32 op) |
bc37a18f | 3829 | { |
b64e5050 | 3830 | const char * const name = gv && isGV(gv) ? GvENAME(gv) : NULL; |
66fc2fa5 | 3831 | |
4c80c0b2 | 3832 | if (op == OP_phoney_OUTPUT_ONLY || op == OP_phoney_INPUT_ONLY) { |
3aed30dc | 3833 | if (ckWARN(WARN_IO)) { |
10edeb5d JH |
3834 | const char * const direction = |
3835 | (const char *)((op == OP_phoney_INPUT_ONLY) ? "in" : "out"); | |
3aed30dc HS |
3836 | if (name && *name) |
3837 | Perl_warner(aTHX_ packWARN(WARN_IO), | |
3838 | "Filehandle %s opened only for %sput", | |
fd322ea4 | 3839 | name, direction); |
3aed30dc HS |
3840 | else |
3841 | Perl_warner(aTHX_ packWARN(WARN_IO), | |
fd322ea4 | 3842 | "Filehandle opened only for %sput", direction); |
3aed30dc | 3843 | } |
2dd78f96 JH |
3844 | } |
3845 | else { | |
e1ec3a88 | 3846 | const char *vile; |
3aed30dc HS |
3847 | I32 warn_type; |
3848 | ||
3849 | if (gv && io && IoTYPE(io) == IoTYPE_CLOSED) { | |
3850 | vile = "closed"; | |
3851 | warn_type = WARN_CLOSED; | |
3852 | } | |
3853 | else { | |
3854 | vile = "unopened"; | |
3855 | warn_type = WARN_UNOPENED; | |
3856 | } | |
3857 | ||
3858 | if (ckWARN(warn_type)) { | |
10edeb5d JH |
3859 | const char * const pars = |
3860 | (const char *)(OP_IS_FILETEST(op) ? "" : "()"); | |
f0a09b71 | 3861 | const char * const func = |
10edeb5d JH |
3862 | (const char *) |
3863 | (op == OP_READLINE ? "readline" : /* "<HANDLE>" not nice */ | |
3864 | op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */ | |
3865 | op < 0 ? "" : /* handle phoney cases */ | |
3866 | PL_op_desc[op]); | |
3867 | const char * const type = | |
3868 | (const char *) | |
3869 | (OP_IS_SOCKET(op) || | |
3870 | (gv && io && IoTYPE(io) == IoTYPE_SOCKET) ? | |
3871 | "socket" : "filehandle"); | |
3aed30dc HS |
3872 | if (name && *name) { |
3873 | Perl_warner(aTHX_ packWARN(warn_type), | |
3874 | "%s%s on %s %s %s", func, pars, vile, type, name); | |
3875 | if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP)) | |
3876 | Perl_warner( | |
3877 | aTHX_ packWARN(warn_type), | |
3878 | "\t(Are you trying to call %s%s on dirhandle %s?)\n", | |
3879 | func, pars, name | |
3880 | ); | |
3881 | } | |
3882 | else { | |
3883 | Perl_warner(aTHX_ packWARN(warn_type), | |
3884 | "%s%s on %s %s", func, pars, vile, type); | |
3885 | if (gv && io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP)) | |
3886 | Perl_warner( | |
3887 | aTHX_ packWARN(warn_type), | |
3888 | "\t(Are you trying to call %s%s on dirhandle?)\n", | |
3889 | func, pars | |
3890 | ); | |
3891 | } | |
3892 | } | |
bc37a18f | 3893 | } |
69282e91 | 3894 | } |
a926ef6b | 3895 | |
f9d13529 KW |
3896 | /* XXX Add documentation after final interface and behavior is decided */ |
3897 | /* May want to show context for error, so would pass Perl_bslash_c(pTHX_ const char* current, const char* start, const bool output_warning) | |
3898 | U8 source = *current; | |
3899 | ||
3900 | May want to add eg, WARN_REGEX | |
3901 | */ | |
3902 | ||
3903 | char | |
3904 | Perl_grok_bslash_c(pTHX_ const char source, const bool output_warning) | |
3905 | { | |
3906 | ||
3907 | U8 result; | |
3908 | ||
3909 | if (! isASCII(source)) { | |
3910 | Perl_croak(aTHX_ "Character following \"\\c\" must be ASCII"); | |
3911 | } | |
3912 | ||
3913 | result = toCTRL(source); | |
3914 | if (! isCNTRL(result)) { | |
3915 | if (source == '{') { | |
3916 | Perl_croak(aTHX_ "It is proposed that \"\\c{\" no longer be valid. It has historically evaluated to\n \";\". If you disagree with this proposal, send email to perl5-porters@perl.org\nOtherwise, or in the meantime, you can work around this failure by changing\n\"\\c{\" to \";\""); | |
3917 | } | |
3918 | else if (output_warning) { | |
1408fb84 KW |
3919 | U8 clearer[3]; |
3920 | U8 i = 0; | |
3921 | if (! isALNUM(result)) { | |
3922 | clearer[i++] = '\\'; | |
3923 | } | |
3924 | clearer[i++] = result; | |
3925 | clearer[i++] = '\0'; | |
3926 | ||
f9d13529 | 3927 | Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), |
1408fb84 | 3928 | "\"\\c%c\" more clearly written simply as \"%s\"", |
f9d13529 | 3929 | source, |
1408fb84 | 3930 | clearer); |
f9d13529 KW |
3931 | } |
3932 | } | |
3933 | ||
3934 | return result; | |
3935 | } | |
3936 | ||
f6adc668 | 3937 | /* To workaround core dumps from the uninitialised tm_zone we get the |
e72cf795 JH |
3938 | * system to give us a reasonable struct to copy. This fix means that |
3939 | * strftime uses the tm_zone and tm_gmtoff values returned by | |
3940 | * localtime(time()). That should give the desired result most of the | |
3941 | * time. But probably not always! | |
3942 | * | |
f6adc668 JH |
3943 | * This does not address tzname aspects of NETaa14816. |
3944 | * | |
e72cf795 | 3945 | */ |
f6adc668 | 3946 | |
e72cf795 JH |
3947 | #ifdef HAS_GNULIBC |
3948 | # ifndef STRUCT_TM_HASZONE | |
3949 | # define STRUCT_TM_HASZONE | |
3950 | # endif | |
3951 | #endif | |
3952 | ||
f6adc668 JH |
3953 | #ifdef STRUCT_TM_HASZONE /* Backward compat */ |
3954 | # ifndef HAS_TM_TM_ZONE | |
3955 | # define HAS_TM_TM_ZONE | |
3956 | # endif | |
3957 | #endif | |
3958 | ||
e72cf795 | 3959 | void |
f1208910 | 3960 | Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */ |
e72cf795 | 3961 | { |
f6adc668 | 3962 | #ifdef HAS_TM_TM_ZONE |
e72cf795 | 3963 | Time_t now; |
1b6737cc | 3964 | const struct tm* my_tm; |
7918f24d | 3965 | PERL_ARGS_ASSERT_INIT_TM; |
e72cf795 | 3966 | (void)time(&now); |
82c57498 | 3967 | my_tm = localtime(&now); |
ca46b8ee SP |
3968 | if (my_tm) |
3969 | Copy(my_tm, ptm, 1, struct tm); | |
1b6737cc | 3970 | #else |
7918f24d | 3971 | PERL_ARGS_ASSERT_INIT_TM; |
1b6737cc | 3972 | PERL_UNUSED_ARG(ptm); |
e72cf795 JH |
3973 | #endif |
3974 | } | |
3975 | ||
3976 | /* | |
3977 | * mini_mktime - normalise struct tm values without the localtime() | |
3978 | * semantics (and overhead) of mktime(). | |
3979 | */ | |
3980 | void | |
f1208910 | 3981 | Perl_mini_mktime(pTHX_ struct tm *ptm) |
e72cf795 JH |
3982 | { |
3983 | int yearday; | |
3984 | int secs; | |
3985 | int month, mday, year, jday; | |
3986 | int odd_cent, odd_year; | |
96a5add6 | 3987 | PERL_UNUSED_CONTEXT; |
e72cf795 | 3988 | |
7918f24d NC |
3989 | PERL_ARGS_ASSERT_MINI_MKTIME; |
3990 | ||
e72cf795 JH |
3991 | #define DAYS_PER_YEAR 365 |
3992 | #define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1) | |
3993 | #define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1) | |
3994 | #define DAYS_PER_QCENT (4*DAYS_PER_CENT+1) | |
3995 | #define SECS_PER_HOUR (60*60) | |
3996 | #define SECS_PER_DAY (24*SECS_PER_HOUR) | |
3997 | /* parentheses deliberately absent on these two, otherwise they don't work */ | |
3998 | #define MONTH_TO_DAYS 153/5 | |
3999 | #define DAYS_TO_MONTH 5/153 | |
4000 | /* offset to bias by March (month 4) 1st between month/mday & year finding */ | |
4001 | #define YEAR_ADJUST (4*MONTH_TO_DAYS+1) | |
4002 | /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */ | |
4003 | #define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again */ | |
4004 | ||
4005 | /* | |
4006 | * Year/day algorithm notes: | |
4007 | * | |
4008 | * With a suitable offset for numeric value of the month, one can find | |
4009 | * an offset into the year by considering months to have 30.6 (153/5) days, | |
4010 | * using integer arithmetic (i.e., with truncation). To avoid too much | |
4011 | * messing about with leap days, we consider January and February to be | |
4012 | * the 13th and 14th month of the previous year. After that transformation, | |
4013 | * we need the month index we use to be high by 1 from 'normal human' usage, | |
4014 | * so the month index values we use run from 4 through 15. | |
4015 | * | |
4016 | * Given that, and the rules for the Gregorian calendar (leap years are those | |
4017 | * divisible by 4 unless also divisible by 100, when they must be divisible | |
4018 | * by 400 instead), we can simply calculate the number of days since some | |
4019 | * arbitrary 'beginning of time' by futzing with the (adjusted) year number, | |
4020 | * the days we derive from our month index, and adding in the day of the | |
4021 | * month. The value used here is not adjusted for the actual origin which | |
4022 | * it normally would use (1 January A.D. 1), since we're not exposing it. | |
4023 | * We're only building the value so we can turn around and get the | |
4024 | * normalised values for the year, month, day-of-month, and day-of-year. | |
4025 | * | |
4026 | * For going backward, we need to bias the value we're using so that we find | |
4027 | * the right year value. (Basically, we don't want the contribution of | |
4028 | * March 1st to the number to apply while deriving the year). Having done | |
4029 | * that, we 'count up' the contribution to the year number by accounting for | |
4030 | * full quadracenturies (400-year periods) with their extra leap days, plus | |
4031 | * the contribution from full centuries (to avoid counting in the lost leap | |
4032 | * days), plus the contribution from full quad-years (to count in the normal | |
4033 | * leap days), plus the leftover contribution from any non-leap years. | |
4034 | * At this point, if we were working with an actual leap day, we'll have 0 | |
4035 | * days left over. This is also true for March 1st, however. So, we have | |
4036 | * to special-case that result, and (earlier) keep track of the 'odd' | |
4037 | * century and year contributions. If we got 4 extra centuries in a qcent, | |
4038 | * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb. | |
4039 | * Otherwise, we add back in the earlier bias we removed (the 123 from | |
4040 | * figuring in March 1st), find the month index (integer division by 30.6), | |
4041 | * and the remainder is the day-of-month. We then have to convert back to | |
4042 | * 'real' months (including fixing January and February from being 14/15 in | |
4043 | * the previous year to being in the proper year). After that, to get | |
4044 | * tm_yday, we work with the normalised year and get a new yearday value for | |
4045 | * January 1st, which we subtract from the yearday value we had earlier, | |
4046 | * representing the date we've re-built. This is done from January 1 | |
4047 | * because tm_yday is 0-origin. | |
4048 | * | |
4049 | * Since POSIX time routines are only guaranteed to work for times since the | |
4050 | * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm | |
4051 | * applies Gregorian calendar rules even to dates before the 16th century | |
4052 | * doesn't bother me. Besides, you'd need cultural context for a given | |
4053 | * date to know whether it was Julian or Gregorian calendar, and that's | |
4054 | * outside the scope for this routine. Since we convert back based on the | |
4055 | * same rules we used to build the yearday, you'll only get strange results | |
4056 | * for input which needed normalising, or for the 'odd' century years which | |
4057 | * were leap years in the Julian calander but not in the Gregorian one. | |
4058 | * I can live with that. | |
4059 | * | |
4060 | * This algorithm also fails to handle years before A.D. 1 gracefully, but | |
4061 | * that's still outside the scope for POSIX time manipulation, so I don't | |
4062 | * care. | |
4063 | */ | |
4064 | ||
4065 | year = 1900 + ptm->tm_year; | |
4066 | month = ptm->tm_mon; | |
4067 | mday = ptm->tm_mday; | |
4068 | /* allow given yday with no month & mday to dominate the result */ | |
4069 | if (ptm->tm_yday >= 0 && mday <= 0 && month <= 0) { | |
4070 | month = 0; | |
4071 | mday = 0; | |
4072 | jday = 1 + ptm->tm_yday; | |
4073 | } | |
4074 | else { | |
4075 | jday = 0; | |
4076 | } | |
4077 | if (month >= 2) | |
4078 | month+=2; | |
4079 | else | |
4080 | month+=14, year--; | |
4081 | yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400; | |
4082 | yearday += month*MONTH_TO_DAYS + mday + jday; | |
4083 | /* | |
4084 | * Note that we don't know when leap-seconds were or will be, | |
4085 | * so we have to trust the user if we get something which looks | |
4086 | * like a sensible leap-second. Wild values for seconds will | |
4087 | * be rationalised, however. | |
4088 | */ | |
4089 | if ((unsigned) ptm->tm_sec <= 60) { | |
4090 | secs = 0; | |
4091 | } | |
4092 | else { | |
4093 | secs = ptm->tm_sec; | |
4094 | ptm->tm_sec = 0; | |
4095 | } | |
4096 | secs += 60 * ptm->tm_min; | |
4097 | secs += SECS_PER_HOUR * ptm->tm_hour; | |
4098 | if (secs < 0) { | |
4099 | if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) { | |
4100 | /* got negative remainder, but need positive time */ | |
4101 | /* back off an extra day to compensate */ | |
4102 | yearday += (secs/SECS_PER_DAY)-1; | |
4103 | secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1); | |
4104 | } | |
4105 | else { | |
4106 | yearday += (secs/SECS_PER_DAY); | |
4107 | secs -= SECS_PER_DAY * (secs/SECS_PER_DAY); | |
4108 | } | |
4109 | } | |
4110 | else if (secs >= SECS_PER_DAY) { | |
4111 | yearday += (secs/SECS_PER_DAY); | |
4112 | secs %= SECS_PER_DAY; | |
4113 | } | |
4114 | ptm->tm_hour = secs/SECS_PER_HOUR; | |
4115 | secs %= SECS_PER_HOUR; | |
4116 | ptm->tm_min = secs/60; | |
4117 | secs %= 60; | |
4118 | ptm->tm_sec += secs; | |
4119 | /* done with time of day effects */ | |
4120 | /* | |
4121 | * The algorithm for yearday has (so far) left it high by 428. | |
4122 | * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to | |
4123 | * bias it by 123 while trying to figure out what year it | |
4124 | * really represents. Even with this tweak, the reverse | |
4125 | * translation fails for years before A.D. 0001. | |
4126 | * It would still fail for Feb 29, but we catch that one below. | |
4127 | */ | |
4128 | jday = yearday; /* save for later fixup vis-a-vis Jan 1 */ | |
4129 | yearday -= YEAR_ADJUST; | |
4130 | year = (yearday / DAYS_PER_QCENT) * 400; | |
4131 | yearday %= DAYS_PER_QCENT; | |
4132 | odd_cent = yearday / DAYS_PER_CENT; | |
4133 | year += odd_cent * 100; | |
4134 | yearday %= DAYS_PER_CENT; | |
4135 | year += (yearday / DAYS_PER_QYEAR) * 4; | |
4136 | yearday %= DAYS_PER_QYEAR; | |
4137 | odd_year = yearday / DAYS_PER_YEAR; | |
4138 | year += odd_year; | |
4139 | yearday %= DAYS_PER_YEAR; | |
4140 | if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */ | |
4141 | month = 1; | |
4142 | yearday = 29; | |
4143 | } | |
4144 | else { | |
4145 | yearday += YEAR_ADJUST; /* recover March 1st crock */ | |
4146 | month = yearday*DAYS_TO_MONTH; | |
4147 | yearday -= month*MONTH_TO_DAYS; | |
4148 | /* recover other leap-year adjustment */ | |
4149 | if (month > 13) { | |
4150 | month-=14; | |
4151 | year++; | |
4152 | } | |
4153 | else { | |
4154 | month-=2; | |
4155 | } | |
4156 | } | |
4157 | ptm->tm_year = year - 1900; | |
4158 | if (yearday) { | |
4159 | ptm->tm_mday = yearday; | |
4160 | ptm->tm_mon = month; | |
4161 | } | |
4162 | else { | |
4163 | ptm->tm_mday = 31; | |
4164 | ptm->tm_mon = month - 1; | |
4165 | } | |
4166 | /* re-build yearday based on Jan 1 to get tm_yday */ | |
4167 | year--; | |
4168 | yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400; | |
4169 | yearday += 14*MONTH_TO_DAYS + 1; | |
4170 | ptm->tm_yday = jday - yearday; | |
4171 | /* fix tm_wday if not overridden by caller */ | |
4172 | if ((unsigned)ptm->tm_wday > 6) | |
4173 | ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7; | |
4174 | } | |
b3c85772 JH |
4175 | |
4176 | char * | |
e1ec3a88 | 4177 | Perl_my_strftime(pTHX_ const char *fmt, int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst) |
b3c85772 JH |
4178 | { |
4179 | #ifdef HAS_STRFTIME | |
4180 | char *buf; | |
4181 | int buflen; | |
4182 | struct tm mytm; | |
4183 | int len; | |
4184 | ||
7918f24d NC |
4185 | PERL_ARGS_ASSERT_MY_STRFTIME; |
4186 | ||
b3c85772 JH |
4187 | init_tm(&mytm); /* XXX workaround - see init_tm() above */ |
4188 | mytm.tm_sec = sec; | |
4189 | mytm.tm_min = min; | |
4190 | mytm.tm_hour = hour; | |
4191 | mytm.tm_mday = mday; | |
4192 | mytm.tm_mon = mon; | |
4193 | mytm.tm_year = year; | |
4194 | mytm.tm_wday = wday; | |
4195 | mytm.tm_yday = yday; | |
4196 | mytm.tm_isdst = isdst; | |
4197 | mini_mktime(&mytm); | |
c473feec SR |
4198 | /* use libc to get the values for tm_gmtoff and tm_zone [perl #18238] */ |
4199 | #if defined(HAS_MKTIME) && (defined(HAS_TM_TM_GMTOFF) || defined(HAS_TM_TM_ZONE)) | |
4200 | STMT_START { | |
4201 | struct tm mytm2; | |
4202 | mytm2 = mytm; | |
4203 | mktime(&mytm2); | |
4204 | #ifdef HAS_TM_TM_GMTOFF | |
4205 | mytm.tm_gmtoff = mytm2.tm_gmtoff; | |
4206 | #endif | |
4207 | #ifdef HAS_TM_TM_ZONE | |
4208 | mytm.tm_zone = mytm2.tm_zone; | |
4209 | #endif | |
4210 | } STMT_END; | |
4211 | #endif | |
b3c85772 | 4212 | buflen = 64; |
a02a5408 | 4213 | Newx(buf, buflen, char); |
b3c85772 JH |
4214 | len = strftime(buf, buflen, fmt, &mytm); |
4215 | /* | |
877f6a72 | 4216 | ** The following is needed to handle to the situation where |
b3c85772 JH |
4217 | ** tmpbuf overflows. Basically we want to allocate a buffer |
4218 | ** and try repeatedly. The reason why it is so complicated | |
4219 | ** is that getting a return value of 0 from strftime can indicate | |
4220 | ** one of the following: | |
4221 | ** 1. buffer overflowed, | |
4222 | ** 2. illegal conversion specifier, or | |
4223 | ** 3. the format string specifies nothing to be returned(not | |
4224 | ** an error). This could be because format is an empty string | |
4225 | ** or it specifies %p that yields an empty string in some locale. | |
4226 | ** If there is a better way to make it portable, go ahead by | |
4227 | ** all means. | |
4228 | */ | |
4229 | if ((len > 0 && len < buflen) || (len == 0 && *fmt == '\0')) | |
4230 | return buf; | |
4231 | else { | |
4232 | /* Possibly buf overflowed - try again with a bigger buf */ | |
e1ec3a88 | 4233 | const int fmtlen = strlen(fmt); |
7743c307 | 4234 | int bufsize = fmtlen + buflen; |
877f6a72 | 4235 | |
a02a5408 | 4236 | Newx(buf, bufsize, char); |
b3c85772 JH |
4237 | while (buf) { |
4238 | buflen = strftime(buf, bufsize, fmt, &mytm); | |
4239 | if (buflen > 0 && buflen < bufsize) | |
4240 | break; | |
4241 | /* heuristic to prevent out-of-memory errors */ | |
4242 | if (bufsize > 100*fmtlen) { | |
4243 | Safefree(buf); | |
4244 | buf = NULL; | |
4245 | break; | |
4246 | } | |
7743c307 SH |
4247 | bufsize *= 2; |
4248 | Renew(buf, bufsize, char); | |
b3c85772 JH |
4249 | } |
4250 | return buf; | |
4251 | } | |
4252 | #else | |
4253 | Perl_croak(aTHX_ "panic: no strftime"); | |
27da23d5 | 4254 | return NULL; |
b3c85772 JH |
4255 | #endif |
4256 | } | |
4257 | ||
877f6a72 NIS |
4258 | |
4259 | #define SV_CWD_RETURN_UNDEF \ | |
4260 | sv_setsv(sv, &PL_sv_undef); \ | |
4261 | return FALSE | |
4262 | ||
4263 | #define SV_CWD_ISDOT(dp) \ | |
4264 | (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ | |
3aed30dc | 4265 | (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) |
877f6a72 NIS |
4266 | |
4267 | /* | |
ccfc67b7 JH |
4268 | =head1 Miscellaneous Functions |
4269 | ||
89423764 | 4270 | =for apidoc getcwd_sv |
877f6a72 NIS |
4271 | |
4272 | Fill the sv with current working directory | |
4273 | ||
4274 | =cut | |
4275 | */ | |
4276 | ||
4277 | /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars. | |
4278 | * rewritten again by dougm, optimized for use with xs TARG, and to prefer | |
4279 | * getcwd(3) if available | |
4280 | * Comments from the orignal: | |
4281 | * This is a faster version of getcwd. It's also more dangerous | |
4282 | * because you might chdir out of a directory that you can't chdir | |
4283 | * back into. */ | |
4284 | ||
877f6a72 | 4285 | int |
89423764 | 4286 | Perl_getcwd_sv(pTHX_ register SV *sv) |
877f6a72 NIS |
4287 | { |
4288 | #ifndef PERL_MICRO | |
97aff369 | 4289 | dVAR; |
ea715489 JH |
4290 | #ifndef INCOMPLETE_TAINTS |
4291 | SvTAINTED_on(sv); | |
4292 | #endif | |
4293 | ||
7918f24d NC |
4294 | PERL_ARGS_ASSERT_GETCWD_SV; |
4295 | ||
8f95b30d JH |
4296 | #ifdef HAS_GETCWD |
4297 | { | |
60e110a8 DM |
4298 | char buf[MAXPATHLEN]; |
4299 | ||
3aed30dc | 4300 | /* Some getcwd()s automatically allocate a buffer of the given |
60e110a8 DM |
4301 | * size from the heap if they are given a NULL buffer pointer. |
4302 | * The problem is that this behaviour is not portable. */ | |
3aed30dc | 4303 | if (getcwd(buf, sizeof(buf) - 1)) { |
42d9b98d | 4304 | sv_setpv(sv, buf); |
3aed30dc HS |
4305 | return TRUE; |
4306 | } | |
4307 | else { | |
4308 | sv_setsv(sv, &PL_sv_undef); | |
4309 | return FALSE; | |
4310 | } | |
8f95b30d JH |
4311 | } |
4312 | ||
4313 | #else | |
4314 | ||
c623ac67 | 4315 | Stat_t statbuf; |
877f6a72 | 4316 | int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino; |
4373e329 | 4317 | int pathlen=0; |
877f6a72 | 4318 | Direntry_t *dp; |
877f6a72 | 4319 | |
862a34c6 | 4320 | SvUPGRADE(sv, SVt_PV); |
877f6a72 | 4321 | |
877f6a72 | 4322 | if (PerlLIO_lstat(".", &statbuf) < 0) { |
3aed30dc | 4323 | SV_CWD_RETURN_UNDEF; |
877f6a72 NIS |
4324 | } |
4325 | ||
4326 | orig_cdev = statbuf.st_dev; | |
4327 | orig_cino = statbuf.st_ino; | |
4328 | cdev = orig_cdev; | |
4329 | cino = orig_cino; | |
4330 | ||
4331 | for (;;) { | |
4373e329 | 4332 | DIR *dir; |
f56ed502 | 4333 | int namelen; |
3aed30dc HS |
4334 | odev = cdev; |
4335 | oino = cino; | |
4336 | ||
4337 | if (PerlDir_chdir("..") < 0) { | |
4338 | SV_CWD_RETURN_UNDEF; | |
4339 | } | |
4340 | if (PerlLIO_stat(".", &statbuf) < 0) { | |
4341 | SV_CWD_RETURN_UNDEF; | |
4342 | } | |
4343 | ||
4344 | cdev = statbuf.st_dev; | |
4345 | cino = statbuf.st_ino; | |
4346 | ||
4347 | if (odev == cdev && oino == cino) { | |
4348 | break; | |
4349 | } | |
4350 | if (!(dir = PerlDir_open("."))) { | |
4351 | SV_CWD_RETURN_UNDEF; | |
4352 | } | |
4353 | ||
4354 | while ((dp = PerlDir_read(dir)) != NULL) { | |
877f6a72 | 4355 | #ifdef DIRNAMLEN |
f56ed502 | 4356 | namelen = dp->d_namlen; |
877f6a72 | 4357 | #else |
f56ed502 | 4358 | namelen = strlen(dp->d_name); |
877f6a72 | 4359 | #endif |
3aed30dc HS |
4360 | /* skip . and .. */ |
4361 | if (SV_CWD_ISDOT(dp)) { | |
4362 | continue; | |
4363 | } | |
4364 | ||
4365 | if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) { | |
4366 | SV_CWD_RETURN_UNDEF; | |
4367 | } | |
4368 | ||
4369 | tdev = statbuf.st_dev; | |
4370 | tino = statbuf.st_ino; | |
4371 | if (tino == oino && tdev == odev) { | |
4372 | break; | |
4373 | } | |
cb5953d6 JH |
4374 | } |
4375 | ||
3aed30dc HS |
4376 | if (!dp) { |
4377 | SV_CWD_RETURN_UNDEF; | |
4378 | } | |
4379 | ||
4380 | if (pathlen + namelen + 1 >= MAXPATHLEN) { | |
4381 | SV_CWD_RETURN_UNDEF; | |
4382 | } | |
877f6a72 | 4383 | |
3aed30dc HS |
4384 | SvGROW(sv, pathlen + namelen + 1); |
4385 | ||
4386 | if (pathlen) { | |
4387 | /* shift down */ | |
95a20fc0 | 4388 | Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char); |
3aed30dc | 4389 | } |
877f6a72 | 4390 | |
3aed30dc HS |
4391 | /* prepend current directory to the front */ |
4392 | *SvPVX(sv) = '/'; | |
4393 | Move(dp->d_name, SvPVX(sv)+1, namelen, char); | |
4394 | pathlen += (namelen + 1); | |
877f6a72 NIS |
4395 | |
4396 | #ifdef VOID_CLOSEDIR | |
3aed30dc | 4397 | PerlDir_close(dir); |
877f6a72 | 4398 | #else |
3aed30dc HS |
4399 | if (PerlDir_close(dir) < 0) { |
4400 | SV_CWD_RETURN_UNDEF; | |
4401 | } | |
877f6a72 NIS |
4402 | #endif |
4403 | } | |
4404 | ||
60e110a8 | 4405 | if (pathlen) { |
3aed30dc HS |
4406 | SvCUR_set(sv, pathlen); |
4407 | *SvEND(sv) = '\0'; | |
4408 | SvPOK_only(sv); | |
877f6a72 | 4409 | |
95a20fc0 | 4410 | if (PerlDir_chdir(SvPVX_const(sv)) < 0) { |
3aed30dc HS |
4411 | SV_CWD_RETURN_UNDEF; |
4412 | } | |
877f6a72 NIS |
4413 | } |
4414 | if (PerlLIO_stat(".", &statbuf) < 0) { | |
3aed30dc | 4415 | SV_CWD_RETURN_UNDEF; |
877f6a72 NIS |
4416 | } |
4417 | ||
4418 | cdev = statbuf.st_dev; | |
4419 | cino = statbuf.st_ino; | |
4420 | ||
4421 | if (cdev != orig_cdev || cino != orig_cino) { | |
3aed30dc HS |
4422 | Perl_croak(aTHX_ "Unstable directory path, " |
4423 | "current directory changed unexpectedly"); | |
877f6a72 | 4424 | } |
877f6a72 NIS |
4425 | |
4426 | return TRUE; | |
793b8d8e JH |
4427 | #endif |
4428 | ||
877f6a72 NIS |
4429 | #else |
4430 | return FALSE; | |
4431 | #endif | |
4432 | } | |
4433 | ||
c812d146 | 4434 | #define VERSION_MAX 0x7FFFFFFF |
91152fc1 | 4435 | |
22f16304 RU |
4436 | /* |
4437 | =for apidoc prescan_version | |
4438 | ||
4439 | =cut | |
4440 | */ | |
91152fc1 DG |
4441 | const char * |
4442 | Perl_prescan_version(pTHX_ const char *s, bool strict, | |
4443 | const char **errstr, | |
4444 | bool *sqv, int *ssaw_decimal, int *swidth, bool *salpha) { | |
4445 | bool qv = (sqv ? *sqv : FALSE); | |
4446 | int width = 3; | |
4447 | int saw_decimal = 0; | |
4448 | bool alpha = FALSE; | |
4449 | const char *d = s; | |
4450 | ||
4451 | PERL_ARGS_ASSERT_PRESCAN_VERSION; | |
4452 | ||
4453 | if (qv && isDIGIT(*d)) | |
4454 | goto dotted_decimal_version; | |
4455 | ||
4456 | if (*d == 'v') { /* explicit v-string */ | |
4457 | d++; | |
4458 | if (isDIGIT(*d)) { | |
4459 | qv = TRUE; | |
4460 | } | |
4461 | else { /* degenerate v-string */ | |
4462 | /* requires v1.2.3 */ | |
4463 | BADVERSION(s,errstr,"Invalid version format (dotted-decimal versions require at least three parts)"); | |
4464 | } | |
4465 | ||
4466 | dotted_decimal_version: | |
4467 | if (strict && d[0] == '0' && isDIGIT(d[1])) { | |
4468 | /* no leading zeros allowed */ | |
4469 | BADVERSION(s,errstr,"Invalid version format (no leading zeros)"); | |
4470 | } | |
4471 | ||
4472 | while (isDIGIT(*d)) /* integer part */ | |
4473 | d++; | |
4474 | ||
4475 | if (*d == '.') | |
4476 | { | |
4477 | saw_decimal++; | |
4478 | d++; /* decimal point */ | |
4479 | } | |
4480 | else | |
4481 | { | |
4482 | if (strict) { | |
4483 | /* require v1.2.3 */ | |
4484 | BADVERSION(s,errstr,"Invalid version format (dotted-decimal versions require at least three parts)"); | |
4485 | } | |
4486 | else { | |
4487 | goto version_prescan_finish; | |
4488 | } | |
4489 | } | |
4490 | ||
4491 | { | |
4492 | int i = 0; | |
4493 | int j = 0; | |
4494 | while (isDIGIT(*d)) { /* just keep reading */ | |
4495 | i++; | |
4496 | while (isDIGIT(*d)) { | |
4497 | d++; j++; | |
4498 | /* maximum 3 digits between decimal */ | |
4499 | if (strict && j > 3) { | |
4500 | BADVERSION(s,errstr,"Invalid version format (maximum 3 digits between decimals)"); | |
4501 | } | |
4502 | } | |
4503 | if (*d == '_') { | |
4504 | if (strict) { | |
4505 | BADVERSION(s,errstr,"Invalid version format (no underscores)"); | |
4506 | } | |
4507 | if ( alpha ) { | |
4508 | BADVERSION(s,errstr,"Invalid version format (multiple underscores)"); | |
4509 | } | |
4510 | d++; | |
4511 | alpha = TRUE; | |
4512 | } | |
4513 | else if (*d == '.') { | |
4514 | if (alpha) { | |
4515 | BADVERSION(s,errstr,"Invalid version format (underscores before decimal)"); | |
4516 | } | |
4517 | saw_decimal++; | |
4518 | d++; | |
4519 | } | |
4520 | else if (!isDIGIT(*d)) { | |
4521 | break; | |
4522 | } | |
4523 | j = 0; | |
4524 | } | |
4525 | ||
4526 | if (strict && i < 2) { | |
4527 | /* requires v1.2.3 */ | |
4528 | BADVERSION(s,errstr,"Invalid version format (dotted-decimal versions require at least three parts)"); | |
4529 | } | |
4530 | } | |
4531 | } /* end if dotted-decimal */ | |
4532 | else | |
4533 | { /* decimal versions */ | |
4534 | /* special strict case for leading '.' or '0' */ | |
4535 | if (strict) { | |
4536 | if (*d == '.') { | |
4537 | BADVERSION(s,errstr,"Invalid version format (0 before decimal required)"); | |
4538 | } | |
4539 | if (*d == '0' && isDIGIT(d[1])) { | |
4540 | BADVERSION(s,errstr,"Invalid version format (no leading zeros)"); | |
4541 | } | |
4542 | } | |
4543 | ||
4544 | /* consume all of the integer part */ | |
4545 | while (isDIGIT(*d)) | |
4546 | d++; | |
4547 | ||
4548 | /* look for a fractional part */ | |
4549 | if (*d == '.') { | |
4550 | /* we found it, so consume it */ | |
4551 | saw_decimal++; | |
4552 | d++; | |
4553 | } | |
4e4da3ac | 4554 | else if (!*d || *d == ';' || isSPACE(*d) || *d == '{' || *d == '}') { |
91152fc1 DG |
4555 | if ( d == s ) { |
4556 | /* found nothing */ | |
4557 | BADVERSION(s,errstr,"Invalid version format (version required)"); | |
4558 | } | |
4559 | /* found just an integer */ | |
4560 | goto version_prescan_finish; | |
4561 | } | |
4562 | else if ( d == s ) { | |
4563 | /* didn't find either integer or period */ | |
4564 | BADVERSION(s,errstr,"Invalid version format (non-numeric data)"); | |
4565 | } | |
4566 | else if (*d == '_') { | |
4567 | /* underscore can't come after integer part */ | |
4568 | if (strict) { | |
4569 | BADVERSION(s,errstr,"Invalid version format (no underscores)"); | |
4570 | } | |
4571 | else if (isDIGIT(d[1])) { | |
4572 | BADVERSION(s,errstr,"Invalid version format (alpha without decimal)"); | |
4573 | } | |
4574 | else { | |
4575 | BADVERSION(s,errstr,"Invalid version format (misplaced underscore)"); | |
4576 | } | |
4577 | } | |
4578 | else { | |
4579 | /* anything else after integer part is just invalid data */ | |
4580 | BADVERSION(s,errstr,"Invalid version format (non-numeric data)"); | |
4581 | } | |
4582 | ||
4583 | /* scan the fractional part after the decimal point*/ | |
4584 | ||
4e4da3ac | 4585 | if (!isDIGIT(*d) && (strict || ! (!*d || *d == ';' || isSPACE(*d) || *d == '{' || *d == '}') )) { |
91152fc1 DG |
4586 | /* strict or lax-but-not-the-end */ |
4587 | BADVERSION(s,errstr,"Invalid version format (fractional part required)"); | |
4588 | } | |
4589 | ||
4590 | while (isDIGIT(*d)) { | |
4591 | d++; | |
4592 | if (*d == '.' && isDIGIT(d[-1])) { | |
4593 | if (alpha) { | |
4594 | BADVERSION(s,errstr,"Invalid version format (underscores before decimal)"); | |
4595 | } | |
4596 | if (strict) { | |
4597 | BADVERSION(s,errstr,"Invalid version format (dotted-decimal versions must begin with 'v')"); | |
4598 | } | |
4599 | d = (char *)s; /* start all over again */ | |
4600 | qv = TRUE; | |
4601 | goto dotted_decimal_version; | |
4602 | } | |
4603 | if (*d == '_') { | |
4604 | if (strict) { | |
4605 | BADVERSION(s,errstr,"Invalid version format (no underscores)"); | |
4606 | } | |
4607 | if ( alpha ) { | |
4608 | BADVERSION(s,errstr,"Invalid version format (multiple underscores)"); | |
4609 | } | |
4610 | if ( ! isDIGIT(d[1]) ) { | |
4611 | BADVERSION(s,errstr,"Invalid version format (misplaced underscore)"); | |
4612 | } | |
4613 | d++; | |
4614 | alpha = TRUE; | |
4615 | } | |
4616 | } | |
4617 | } | |
4618 | ||
4619 | version_prescan_finish: | |
4620 | while (isSPACE(*d)) | |
4621 | d++; | |
4622 | ||
4e4da3ac | 4623 | if (!isDIGIT(*d) && (! (!*d || *d == ';' || *d == '{' || *d == '}') )) { |
91152fc1 DG |
4624 | /* trailing non-numeric data */ |
4625 | BADVERSION(s,errstr,"Invalid version format (non-numeric data)"); | |
4626 | } | |
4627 | ||
4628 | if (sqv) | |
4629 | *sqv = qv; | |
4630 | if (swidth) | |
4631 | *swidth = width; | |
4632 | if (ssaw_decimal) | |
4633 | *ssaw_decimal = saw_decimal; | |
4634 | if (salpha) | |
4635 | *salpha = alpha; | |
4636 | return d; | |
4637 | } | |
4638 | ||
f4758303 | 4639 | /* |
b0f01acb JP |
4640 | =for apidoc scan_version |
4641 | ||
4642 | Returns a pointer to the next character after the parsed | |
4643 | version string, as well as upgrading the passed in SV to | |
4644 | an RV. | |
4645 | ||
4646 | Function must be called with an already existing SV like | |
4647 | ||
137d6fc0 | 4648 | sv = newSV(0); |
abc25d8c | 4649 | s = scan_version(s, SV *sv, bool qv); |
b0f01acb JP |
4650 | |
4651 | Performs some preprocessing to the string to ensure that | |
4652 | it has the correct characteristics of a version. Flags the | |
4653 | object if it contains an underscore (which denotes this | |
abc25d8c | 4654 | is an alpha version). The boolean qv denotes that the version |
137d6fc0 JP |
4655 | should be interpreted as if it had multiple decimals, even if |
4656 | it doesn't. | |
b0f01acb JP |
4657 | |
4658 | =cut | |
4659 | */ | |
4660 | ||
9137345a | 4661 | const char * |
e1ec3a88 | 4662 | Perl_scan_version(pTHX_ const char *s, SV *rv, bool qv) |
b0f01acb | 4663 | { |
e0218a61 | 4664 | const char *start; |
9137345a JP |
4665 | const char *pos; |
4666 | const char *last; | |
91152fc1 DG |
4667 | const char *errstr = NULL; |
4668 | int saw_decimal = 0; | |
9137345a | 4669 | int width = 3; |
91152fc1 | 4670 | bool alpha = FALSE; |
c812d146 | 4671 | bool vinf = FALSE; |
7452cf6a AL |
4672 | AV * const av = newAV(); |
4673 | SV * const hv = newSVrv(rv, "version"); /* create an SV and upgrade the RV */ | |
7918f24d NC |
4674 | |
4675 | PERL_ARGS_ASSERT_SCAN_VERSION; | |
4676 | ||
9137345a | 4677 | (void)sv_upgrade(hv, SVt_PVHV); /* needs to be an HV type */ |
cb5772bb | 4678 | |
91152fc1 DG |
4679 | #ifndef NODEFAULT_SHAREKEYS |
4680 | HvSHAREKEYS_on(hv); /* key-sharing on by default */ | |
4681 | #endif | |
4682 | ||
e0218a61 JP |
4683 | while (isSPACE(*s)) /* leading whitespace is OK */ |
4684 | s++; | |
4685 | ||
91152fc1 DG |
4686 | last = prescan_version(s, FALSE, &errstr, &qv, &saw_decimal, &width, &alpha); |
4687 | if (errstr) { | |
4688 | /* "undef" is a special case and not an error */ | |
4689 | if ( ! ( *s == 'u' && strEQ(s,"undef")) ) { | |
4690 | Perl_croak(aTHX_ "%s", errstr); | |
46314c13 | 4691 | } |
ad63d80f | 4692 | } |
ad63d80f | 4693 | |
91152fc1 DG |
4694 | start = s; |
4695 | if (*s == 'v') | |
4696 | s++; | |
9137345a JP |
4697 | pos = s; |
4698 | ||
4699 | if ( qv ) | |
ef8f7699 | 4700 | (void)hv_stores(MUTABLE_HV(hv), "qv", newSViv(qv)); |
cb5772bb | 4701 | if ( alpha ) |
ef8f7699 | 4702 | (void)hv_stores(MUTABLE_HV(hv), "alpha", newSViv(alpha)); |
9137345a | 4703 | if ( !qv && width < 3 ) |
ef8f7699 | 4704 | (void)hv_stores(MUTABLE_HV(hv), "width", newSViv(width)); |
9137345a | 4705 | |
ad63d80f | 4706 | while (isDIGIT(*pos)) |
46314c13 | 4707 | pos++; |
ad63d80f JP |
4708 | if (!isALPHA(*pos)) { |
4709 | I32 rev; | |
4710 | ||
ad63d80f JP |
4711 | for (;;) { |
4712 | rev = 0; | |
4713 | { | |
129318bd | 4714 | /* this is atoi() that delimits on underscores */ |
9137345a | 4715 | const char *end = pos; |
129318bd | 4716 | I32 mult = 1; |
c812d146 | 4717 | I32 orev; |
9137345a | 4718 | |
129318bd JP |
4719 | /* the following if() will only be true after the decimal |
4720 | * point of a version originally created with a bare | |
4721 | * floating point number, i.e. not quoted in any way | |
4722 | */ | |
91152fc1 | 4723 | if ( !qv && s > start && saw_decimal == 1 ) { |
c76df65e | 4724 | mult *= 100; |
129318bd | 4725 | while ( s < end ) { |
c812d146 | 4726 | orev = rev; |
129318bd JP |
4727 | rev += (*s - '0') * mult; |
4728 | mult /= 10; | |
c812d146 JP |
4729 | if ( (PERL_ABS(orev) > PERL_ABS(rev)) |
4730 | || (PERL_ABS(rev) > VERSION_MAX )) { | |
a2a5de95 NC |
4731 | Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW), |
4732 | "Integer overflow in version %d",VERSION_MAX); | |
c812d146 JP |
4733 | s = end - 1; |
4734 | rev = VERSION_MAX; | |
4735 | vinf = 1; | |
4736 | } | |
129318bd | 4737 | s++; |
9137345a JP |
4738 | if ( *s == '_' ) |
4739 | s++; | |
129318bd JP |
4740 | } |
4741 | } | |
4742 | else { | |
4743 | while (--end >= s) { | |
c812d146 | 4744 | orev = rev; |
129318bd JP |
4745 | rev += (*end - '0') * mult; |
4746 | mult *= 10; | |
c812d146 JP |
4747 | if ( (PERL_ABS(orev) > PERL_ABS(rev)) |
4748 | || (PERL_ABS(rev) > VERSION_MAX )) { | |
a2a5de95 NC |
4749 | Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW), |
4750 | "Integer overflow in version"); | |
c812d146 JP |
4751 | end = s - 1; |
4752 | rev = VERSION_MAX; | |
4753 | vinf = 1; | |
4754 | } | |
129318bd JP |
4755 | } |
4756 | } | |
4757 | } | |
9137345a | 4758 | |
129318bd | 4759 | /* Append revision */ |
9137345a | 4760 | av_push(av, newSViv(rev)); |
c812d146 JP |
4761 | if ( vinf ) { |
4762 | s = last; | |
4763 | break; | |
4764 | } | |
4765 | else if ( *pos == '.' ) | |
9137345a JP |
4766 | s = ++pos; |
4767 | else if ( *pos == '_' && isDIGIT(pos[1]) ) | |
ad63d80f | 4768 | s = ++pos; |
f941e658 JP |
4769 | else if ( *pos == ',' && isDIGIT(pos[1]) ) |
4770 | s = ++pos; | |
ad63d80f JP |
4771 | else if ( isDIGIT(*pos) ) |
4772 | s = pos; | |
b0f01acb | 4773 | else { |
ad63d80f JP |
4774 | s = pos; |
4775 | break; | |
4776 | } | |
9137345a JP |
4777 | if ( qv ) { |
4778 | while ( isDIGIT(*pos) ) | |
4779 | pos++; | |
4780 | } | |
4781 | else { | |
4782 | int digits = 0; | |
4783 | while ( ( isDIGIT(*pos) || *pos == '_' ) && digits < 3 ) { | |
4784 | if ( *pos != '_' ) | |
4785 | digits++; | |
4786 | pos++; | |
4787 | } | |
b0f01acb JP |
4788 | } |
4789 | } | |
4790 | } | |
9137345a JP |
4791 | if ( qv ) { /* quoted versions always get at least three terms*/ |
4792 | I32 len = av_len(av); | |
4edfc503 NC |
4793 | /* This for loop appears to trigger a compiler bug on OS X, as it |
4794 | loops infinitely. Yes, len is negative. No, it makes no sense. | |
4795 | Compiler in question is: | |
4796 | gcc version 3.3 20030304 (Apple Computer, Inc. build 1640) | |
4797 | for ( len = 2 - len; len > 0; len-- ) | |
502c6561 | 4798 | av_push(MUTABLE_AV(sv), newSViv(0)); |
4edfc503 NC |
4799 | */ |
4800 | len = 2 - len; | |
4801 | while (len-- > 0) | |
9137345a | 4802 | av_push(av, newSViv(0)); |
b9381830 | 4803 | } |
9137345a | 4804 | |
8cb289bd | 4805 | /* need to save off the current version string for later */ |
c812d146 JP |
4806 | if ( vinf ) { |
4807 | SV * orig = newSVpvn("v.Inf", sizeof("v.Inf")-1); | |
ef8f7699 NC |
4808 | (void)hv_stores(MUTABLE_HV(hv), "original", orig); |
4809 | (void)hv_stores(MUTABLE_HV(hv), "vinf", newSViv(1)); | |
c812d146 JP |
4810 | } |
4811 | else if ( s > start ) { | |
8cb289bd | 4812 | SV * orig = newSVpvn(start,s-start); |
91152fc1 | 4813 | if ( qv && saw_decimal == 1 && *start != 'v' ) { |
8cb289bd RGS |
4814 | /* need to insert a v to be consistent */ |
4815 | sv_insert(orig, 0, 0, "v", 1); | |
4816 | } | |
ef8f7699 | 4817 | (void)hv_stores(MUTABLE_HV(hv), "original", orig); |
8cb289bd RGS |
4818 | } |
4819 | else { | |
76f68e9b | 4820 | (void)hv_stores(MUTABLE_HV(hv), "original", newSVpvs("0")); |
9137345a | 4821 | av_push(av, newSViv(0)); |
8cb289bd RGS |
4822 | } |
4823 | ||
4824 | /* And finally, store the AV in the hash */ | |
daba3364 | 4825 | (void)hv_stores(MUTABLE_HV(hv), "version", newRV_noinc(MUTABLE_SV(av))); |
9137345a | 4826 | |
92dcf8ce JP |
4827 | /* fix RT#19517 - special case 'undef' as string */ |
4828 | if ( *s == 'u' && strEQ(s,"undef") ) { | |
4829 | s += 5; | |
4830 | } | |
4831 | ||
9137345a | 4832 | return s; |
b0f01acb JP |
4833 | } |
4834 | ||
4835 | /* | |
4836 | =for apidoc new_version | |
4837 | ||
4838 | Returns a new version object based on the passed in SV: | |
4839 | ||
4840 | SV *sv = new_version(SV *ver); | |
4841 | ||
4842 | Does not alter the passed in ver SV. See "upg_version" if you | |
4843 | want to upgrade the SV. | |
4844 | ||
4845 | =cut | |
4846 | */ | |
4847 | ||
4848 | SV * | |
4849 | Perl_new_version(pTHX_ SV *ver) | |
4850 | { | |
97aff369 | 4851 | dVAR; |
2d03de9c | 4852 | SV * const rv = newSV(0); |
7918f24d | 4853 | PERL_ARGS_ASSERT_NEW_VERSION; |
d7aa5382 JP |
4854 | if ( sv_derived_from(ver,"version") ) /* can just copy directly */ |
4855 | { | |
4856 | I32 key; | |
53c1dcc0 | 4857 | AV * const av = newAV(); |
9137345a JP |
4858 | AV *sav; |
4859 | /* This will get reblessed later if a derived class*/ | |
e0218a61 | 4860 | SV * const hv = newSVrv(rv, "version"); |
9137345a | 4861 | (void)sv_upgrade(hv, SVt_PVHV); /* needs to be an HV type */ |
91152fc1 DG |
4862 | #ifndef NODEFAULT_SHAREKEYS |
4863 | HvSHAREKEYS_on(hv); /* key-sharing on by default */ | |
4864 | #endif | |
9137345a JP |
4865 | |
4866 | if ( SvROK(ver) ) | |
4867 | ver = SvRV(ver); | |
4868 | ||
4869 | /* Begin copying all of the elements */ | |
ef8f7699 NC |
4870 | if ( hv_exists(MUTABLE_HV(ver), "qv", 2) ) |
4871 | (void)hv_stores(MUTABLE_HV(hv), "qv", newSViv(1)); | |
9137345a | 4872 | |
ef8f7699 NC |
4873 | if ( hv_exists(MUTABLE_HV(ver), "alpha", 5) ) |
4874 | (void)hv_stores(MUTABLE_HV(hv), "alpha", newSViv(1)); | |
9137345a | 4875 | |
ef8f7699 | 4876 | if ( hv_exists(MUTABLE_HV(ver), "width", 5 ) ) |
d7aa5382 | 4877 | { |
ef8f7699 NC |
4878 | const I32 width = SvIV(*hv_fetchs(MUTABLE_HV(ver), "width", FALSE)); |
4879 | (void)hv_stores(MUTABLE_HV(hv), "width", newSViv(width)); | |
d7aa5382 | 4880 | } |
9137345a | 4881 | |
ef8f7699 | 4882 | if ( hv_exists(MUTABLE_HV(ver), "original", 8 ) ) |
8cb289bd | 4883 | { |
ef8f7699 NC |
4884 | SV * pv = *hv_fetchs(MUTABLE_HV(ver), "original", FALSE); |
4885 | (void)hv_stores(MUTABLE_HV(hv), "original", newSVsv(pv)); | |
8cb289bd RGS |
4886 | } |
4887 | ||
502c6561 | 4888 | sav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(ver), "version", FALSE))); |
9137345a JP |
4889 | /* This will get reblessed later if a derived class*/ |
4890 | for ( key = 0; key <= av_len(sav); key++ ) | |
4891 | { | |
4892 | const I32 rev = SvIV(*av_fetch(sav, key, FALSE)); | |
4893 | av_push(av, newSViv(rev)); | |
4894 | } | |
4895 | ||
daba3364 | 4896 | (void)hv_stores(MUTABLE_HV(hv), "version", newRV_noinc(MUTABLE_SV(av))); |
d7aa5382 JP |
4897 | return rv; |
4898 | } | |
ad63d80f | 4899 | #ifdef SvVOK |
4f2da183 | 4900 | { |
3c21775b | 4901 | const MAGIC* const mg = SvVSTRING_mg(ver); |
4f2da183 NC |
4902 | if ( mg ) { /* already a v-string */ |
4903 | const STRLEN len = mg->mg_len; | |
4904 | char * const version = savepvn( (const char*)mg->mg_ptr, len); | |
4905 | sv_setpvn(rv,version,len); | |
8cb289bd | 4906 | /* this is for consistency with the pure Perl class */ |
91152fc1 | 4907 | if ( isDIGIT(*version) ) |
8cb289bd | 4908 | sv_insert(rv, 0, 0, "v", 1); |
4f2da183 NC |
4909 | Safefree(version); |
4910 | } | |
4911 | else { | |
ad63d80f | 4912 | #endif |
4f2da183 | 4913 | sv_setsv(rv,ver); /* make a duplicate */ |
137d6fc0 | 4914 | #ifdef SvVOK |
4f2da183 | 4915 | } |
26ec6fc3 | 4916 | } |
137d6fc0 | 4917 | #endif |
ac0e6a2f | 4918 | return upg_version(rv, FALSE); |
b0f01acb JP |
4919 | } |
4920 | ||
4921 | /* | |
4922 | =for apidoc upg_version | |
4923 | ||
4924 | In-place upgrade of the supplied SV to a version object. | |
4925 | ||
ac0e6a2f | 4926 | SV *sv = upg_version(SV *sv, bool qv); |
b0f01acb | 4927 | |
ac0e6a2f RGS |
4928 | Returns a pointer to the upgraded SV. Set the boolean qv if you want |
4929 | to force this SV to be interpreted as an "extended" version. | |
b0f01acb JP |
4930 | |
4931 | =cut | |
4932 | */ | |
4933 | ||
4934 | SV * | |
ac0e6a2f | 4935 | Perl_upg_version(pTHX_ SV *ver, bool qv) |
b0f01acb | 4936 | { |
cd57dc11 | 4937 | const char *version, *s; |
4f2da183 NC |
4938 | #ifdef SvVOK |
4939 | const MAGIC *mg; | |
4940 | #endif | |
137d6fc0 | 4941 | |
7918f24d NC |
4942 | PERL_ARGS_ASSERT_UPG_VERSION; |
4943 | ||
ac0e6a2f | 4944 | if ( SvNOK(ver) && !( SvPOK(ver) && sv_len(ver) == 3 ) ) |
137d6fc0 | 4945 | { |
ac0e6a2f | 4946 | /* may get too much accuracy */ |
137d6fc0 | 4947 | char tbuf[64]; |
b5b5a8f0 RGS |
4948 | #ifdef USE_LOCALE_NUMERIC |
4949 | char *loc = setlocale(LC_NUMERIC, "C"); | |
4950 | #endif | |
63e3af20 | 4951 | STRLEN len = my_snprintf(tbuf, sizeof(tbuf), "%.9"NVff, SvNVX(ver)); |
b5b5a8f0 RGS |
4952 | #ifdef USE_LOCALE_NUMERIC |
4953 | setlocale(LC_NUMERIC, loc); | |
4954 | #endif | |
c8a14fb6 | 4955 | while (tbuf[len-1] == '0' && len > 0) len--; |
8cb289bd | 4956 | if ( tbuf[len-1] == '.' ) len--; /* eat the trailing decimal */ |
86c11942 | 4957 | version = savepvn(tbuf, len); |
137d6fc0 | 4958 | } |
ad63d80f | 4959 | #ifdef SvVOK |
666cce26 | 4960 | else if ( (mg = SvVSTRING_mg(ver)) ) { /* already a v-string */ |
ad63d80f | 4961 | version = savepvn( (const char*)mg->mg_ptr,mg->mg_len ); |
91152fc1 | 4962 | qv = TRUE; |
b0f01acb | 4963 | } |
ad63d80f | 4964 | #endif |
137d6fc0 JP |
4965 | else /* must be a string or something like a string */ |
4966 | { | |
ac0e6a2f RGS |
4967 | STRLEN len; |
4968 | version = savepv(SvPV(ver,len)); | |
4969 | #ifndef SvVOK | |
4970 | # if PERL_VERSION > 5 | |
4971 | /* This will only be executed for 5.6.0 - 5.8.0 inclusive */ | |
91152fc1 DG |
4972 | if ( len >= 3 && !instr(version,".") && !instr(version,"_") |
4973 | && !(*version == 'u' && strEQ(version, "undef")) | |
4974 | && (*version < '0' || *version > '9') ) { | |
ac0e6a2f RGS |
4975 | /* may be a v-string */ |
4976 | SV * const nsv = sv_newmortal(); | |
4977 | const char *nver; | |
4978 | const char *pos; | |
91152fc1 | 4979 | int saw_decimal = 0; |
8cb289bd | 4980 | sv_setpvf(nsv,"v%vd",ver); |
ac0e6a2f RGS |
4981 | pos = nver = savepv(SvPV_nolen(nsv)); |
4982 | ||
4983 | /* scan the resulting formatted string */ | |
8cb289bd | 4984 | pos++; /* skip the leading 'v' */ |
ac0e6a2f RGS |
4985 | while ( *pos == '.' || isDIGIT(*pos) ) { |
4986 | if ( *pos == '.' ) | |
91152fc1 | 4987 | saw_decimal++ ; |
ac0e6a2f RGS |
4988 | pos++; |
4989 | } | |
4990 | ||
4991 | /* is definitely a v-string */ | |
91152fc1 | 4992 | if ( saw_decimal >= 2 ) { |
ac0e6a2f RGS |
4993 | Safefree(version); |
4994 | version = nver; | |
4995 | } | |
4996 | } | |
4997 | # endif | |
4998 | #endif | |
137d6fc0 | 4999 | } |
92dcf8ce | 5000 | |
cd57dc11 | 5001 | s = scan_version(version, ver, qv); |
808ee47e | 5002 | if ( *s != '\0' ) |
a2a5de95 NC |
5003 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), |
5004 | "Version string '%s' contains invalid data; " | |
5005 | "ignoring: '%s'", version, s); | |
137d6fc0 | 5006 | Safefree(version); |
ad63d80f | 5007 | return ver; |
b0f01acb JP |
5008 | } |
5009 | ||
e0218a61 JP |
5010 | /* |
5011 | =for apidoc vverify | |
5012 | ||
5013 | Validates that the SV contains a valid version object. | |
5014 | ||
5015 | bool vverify(SV *vobj); | |
5016 | ||
5017 | Note that it only confirms the bare minimum structure (so as not to get | |
5018 | confused by derived classes which may contain additional hash entries): | |
5019 | ||
5020 | =over 4 | |
5021 | ||
cb5772bb | 5022 | =item * The SV contains a [reference to a] hash |
e0218a61 JP |
5023 | |
5024 | =item * The hash contains a "version" key | |
5025 | ||
cb5772bb | 5026 | =item * The "version" key has [a reference to] an AV as its value |
e0218a61 JP |
5027 | |
5028 | =back | |
5029 | ||
5030 | =cut | |
5031 | */ | |
5032 | ||
5033 | bool | |
5034 | Perl_vverify(pTHX_ SV *vs) | |
5035 | { | |
5036 | SV *sv; | |
7918f24d NC |
5037 | |
5038 | PERL_ARGS_ASSERT_VVERIFY; | |
5039 | ||
e0218a61 JP |
5040 | if ( SvROK(vs) ) |
5041 | vs = SvRV(vs); | |
5042 | ||
5043 | /* see if the appropriate elements exist */ | |
5044 | if ( SvTYPE(vs) == SVt_PVHV | |
ef8f7699 NC |
5045 | && hv_exists(MUTABLE_HV(vs), "version", 7) |
5046 | && (sv = SvRV(*hv_fetchs(MUTABLE_HV(vs), "version", FALSE))) | |
e0218a61 JP |
5047 | && SvTYPE(sv) == SVt_PVAV ) |
5048 | return TRUE; | |
5049 | else | |
5050 | return FALSE; | |
5051 | } | |
b0f01acb JP |
5052 | |
5053 | /* | |
5054 | =for apidoc vnumify | |
5055 | ||
ad63d80f JP |
5056 | Accepts a version object and returns the normalized floating |
5057 | point representation. Call like: | |
b0f01acb | 5058 | |
ad63d80f | 5059 | sv = vnumify(rv); |
b0f01acb | 5060 | |
ad63d80f JP |
5061 | NOTE: you can pass either the object directly or the SV |
5062 | contained within the RV. | |
b0f01acb JP |
5063 | |
5064 | =cut | |
5065 | */ | |
5066 | ||
5067 | SV * | |
ad63d80f | 5068 | Perl_vnumify(pTHX_ SV *vs) |
b0f01acb | 5069 | { |
ad63d80f | 5070 | I32 i, len, digit; |
9137345a JP |
5071 | int width; |
5072 | bool alpha = FALSE; | |
cb4a3036 | 5073 | SV *sv; |
9137345a | 5074 | AV *av; |
7918f24d NC |
5075 | |
5076 | PERL_ARGS_ASSERT_VNUMIFY; | |
5077 | ||
ad63d80f JP |
5078 | if ( SvROK(vs) ) |
5079 | vs = SvRV(vs); | |
9137345a | 5080 | |
e0218a61 JP |
5081 | if ( !vverify(vs) ) |
5082 | Perl_croak(aTHX_ "Invalid version object"); | |
5083 | ||
9137345a | 5084 | /* see if various flags exist */ |
ef8f7699 | 5085 | if ( hv_exists(MUTABLE_HV(vs), "alpha", 5 ) ) |
9137345a | 5086 | alpha = TRUE; |
ef8f7699 NC |
5087 | if ( hv_exists(MUTABLE_HV(vs), "width", 5 ) ) |
5088 | width = SvIV(*hv_fetchs(MUTABLE_HV(vs), "width", FALSE)); | |
9137345a JP |
5089 | else |
5090 | width = 3; | |
5091 | ||
5092 | ||
5093 | /* attempt to retrieve the version array */ | |
502c6561 | 5094 | if ( !(av = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(vs), "version", FALSE))) ) ) { |
cb4a3036 | 5095 | return newSVpvs("0"); |
9137345a JP |
5096 | } |
5097 | ||
5098 | len = av_len(av); | |
46314c13 JP |
5099 | if ( len == -1 ) |
5100 | { | |
cb4a3036 | 5101 | return newSVpvs("0"); |
46314c13 | 5102 | } |
9137345a JP |
5103 | |
5104 | digit = SvIV(*av_fetch(av, 0, 0)); | |
cb4a3036 | 5105 | sv = Perl_newSVpvf(aTHX_ "%d.", (int)PERL_ABS(digit)); |
13f8f398 | 5106 | for ( i = 1 ; i < len ; i++ ) |
b0f01acb | 5107 | { |
9137345a JP |
5108 | digit = SvIV(*av_fetch(av, i, 0)); |
5109 | if ( width < 3 ) { | |
43eaf59d | 5110 | const int denom = (width == 2 ? 10 : 100); |
53c1dcc0 | 5111 | const div_t term = div((int)PERL_ABS(digit),denom); |
261fcdab | 5112 | Perl_sv_catpvf(aTHX_ sv, "%0*d_%d", width, term.quot, term.rem); |
9137345a JP |
5113 | } |
5114 | else { | |
261fcdab | 5115 | Perl_sv_catpvf(aTHX_ sv, "%0*d", width, (int)digit); |
9137345a | 5116 | } |
b0f01acb | 5117 | } |
13f8f398 JP |
5118 | |
5119 | if ( len > 0 ) | |
5120 | { | |
9137345a JP |
5121 | digit = SvIV(*av_fetch(av, len, 0)); |
5122 | if ( alpha && width == 3 ) /* alpha version */ | |
396482e1 | 5123 | sv_catpvs(sv,"_"); |
261fcdab | 5124 | Perl_sv_catpvf(aTHX_ sv, "%0*d", width, (int)digit); |
13f8f398 | 5125 | } |
e0218a61 | 5126 | else /* len == 0 */ |
13f8f398 | 5127 | { |
396482e1 | 5128 | sv_catpvs(sv, "000"); |
13f8f398 | 5129 | } |
b0f01acb JP |
5130 | return sv; |
5131 | } | |
5132 | ||
5133 | /* | |
b9381830 | 5134 | =for apidoc vnormal |
b0f01acb | 5135 | |
ad63d80f JP |
5136 | Accepts a version object and returns the normalized string |
5137 | representation. Call like: | |
b0f01acb | 5138 | |
b9381830 | 5139 | sv = vnormal(rv); |
b0f01acb | 5140 | |
ad63d80f JP |
5141 | NOTE: you can pass either the object directly or the SV |
5142 | contained within the RV. | |
b0f01acb JP |
5143 | |
5144 | =cut | |
5145 | */ | |
5146 | ||
5147 | SV * | |
b9381830 | 5148 | Perl_vnormal(pTHX_ SV *vs) |
b0f01acb | 5149 | { |
ad63d80f | 5150 | I32 i, len, digit; |
9137345a | 5151 | bool alpha = FALSE; |
cb4a3036 | 5152 | SV *sv; |
9137345a | 5153 | AV *av; |
7918f24d NC |
5154 | |
5155 | PERL_ARGS_ASSERT_VNORMAL; | |
5156 | ||
ad63d80f JP |
5157 | if ( SvROK(vs) ) |
5158 | vs = SvRV(vs); | |
9137345a | 5159 | |
e0218a61 JP |
5160 | if ( !vverify(vs) ) |
5161 | Perl_croak(aTHX_ "Invalid version object"); | |
5162 | ||
ef8f7699 | 5163 | if ( hv_exists(MUTABLE_HV(vs), "alpha", 5 ) ) |
9137345a | 5164 | alpha = TRUE; |
502c6561 | 5165 | av = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(vs), "version", FALSE))); |
9137345a JP |
5166 | |
5167 | len = av_len(av); | |
e0218a61 JP |
5168 | if ( len == -1 ) |
5169 | { | |
cb4a3036 | 5170 | return newSVpvs(""); |
46314c13 | 5171 | } |
9137345a | 5172 | digit = SvIV(*av_fetch(av, 0, 0)); |
cb4a3036 | 5173 | sv = Perl_newSVpvf(aTHX_ "v%"IVdf, (IV)digit); |
cb5772bb | 5174 | for ( i = 1 ; i < len ; i++ ) { |
9137345a | 5175 | digit = SvIV(*av_fetch(av, i, 0)); |
261fcdab | 5176 | Perl_sv_catpvf(aTHX_ sv, ".%"IVdf, (IV)digit); |
9137345a JP |
5177 | } |
5178 | ||
e0218a61 JP |
5179 | if ( len > 0 ) |
5180 | { | |
9137345a JP |
5181 | /* handle last digit specially */ |
5182 | digit = SvIV(*av_fetch(av, len, 0)); | |
5183 | if ( alpha ) | |
261fcdab | 5184 | Perl_sv_catpvf(aTHX_ sv, "_%"IVdf, (IV)digit); |
ad63d80f | 5185 | else |
261fcdab | 5186 | Perl_sv_catpvf(aTHX_ sv, ".%"IVdf, (IV)digit); |
b0f01acb | 5187 | } |
9137345a | 5188 | |
137d6fc0 JP |
5189 | if ( len <= 2 ) { /* short version, must be at least three */ |
5190 | for ( len = 2 - len; len != 0; len-- ) | |
396482e1 | 5191 | sv_catpvs(sv,".0"); |
137d6fc0 | 5192 | } |
b0f01acb | 5193 | return sv; |
9137345a | 5194 | } |
b0f01acb | 5195 | |
ad63d80f | 5196 | /* |
b9381830 JP |
5197 | =for apidoc vstringify |
5198 | ||
5199 | In order to maintain maximum compatibility with earlier versions | |
5200 | of Perl, this function will return either the floating point | |
5201 | notation or the multiple dotted notation, depending on whether | |
5202 | the original version contained 1 or more dots, respectively | |
5203 | ||
5204 | =cut | |
5205 | */ | |
5206 | ||
5207 | SV * | |
5208 | Perl_vstringify(pTHX_ SV *vs) | |
5209 | { | |
7918f24d NC |
5210 | PERL_ARGS_ASSERT_VSTRINGIFY; |
5211 | ||
b9381830 JP |
5212 | if ( SvROK(vs) ) |
5213 | vs = SvRV(vs); | |
219bf418 | 5214 | |
e0218a61 JP |
5215 | if ( !vverify(vs) ) |
5216 | Perl_croak(aTHX_ "Invalid version object"); | |
5217 | ||
ef8f7699 | 5218 | if (hv_exists(MUTABLE_HV(vs), "original", sizeof("original") - 1)) { |
219bf418 | 5219 | SV *pv; |
ef8f7699 | 5220 | pv = *hv_fetchs(MUTABLE_HV(vs), "original", FALSE); |
219bf418 RGS |
5221 | if ( SvPOK(pv) ) |
5222 | return newSVsv(pv); | |
5223 | else | |
5224 | return &PL_sv_undef; | |
5225 | } | |
5226 | else { | |
ef8f7699 | 5227 | if ( hv_exists(MUTABLE_HV(vs), "qv", 2) ) |
219bf418 RGS |
5228 | return vnormal(vs); |
5229 | else | |
5230 | return vnumify(vs); | |
5231 | } | |
b9381830 JP |
5232 | } |
5233 | ||
5234 | /* | |
ad63d80f JP |
5235 | =for apidoc vcmp |
5236 | ||
5237 | Version object aware cmp. Both operands must already have been | |
5238 | converted into version objects. | |
5239 | ||
5240 | =cut | |
5241 | */ | |
5242 | ||
5243 | int | |
9137345a | 5244 | Perl_vcmp(pTHX_ SV *lhv, SV *rhv) |
ad63d80f JP |
5245 | { |
5246 | I32 i,l,m,r,retval; | |
9137345a JP |
5247 | bool lalpha = FALSE; |
5248 | bool ralpha = FALSE; | |
5249 | I32 left = 0; | |
5250 | I32 right = 0; | |
5251 | AV *lav, *rav; | |
7918f24d NC |
5252 | |
5253 | PERL_ARGS_ASSERT_VCMP; | |
5254 | ||
9137345a JP |
5255 | if ( SvROK(lhv) ) |
5256 | lhv = SvRV(lhv); | |
5257 | if ( SvROK(rhv) ) | |
5258 | rhv = SvRV(rhv); | |
5259 | ||
e0218a61 JP |
5260 | if ( !vverify(lhv) ) |
5261 | Perl_croak(aTHX_ "Invalid version object"); | |
5262 | ||
5263 | if ( !vverify(rhv) ) | |
5264 | Perl_croak(aTHX_ "Invalid version object"); | |
5265 | ||
9137345a | 5266 | /* get the left hand term */ |
502c6561 | 5267 | lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(lhv), "version", FALSE))); |
ef8f7699 | 5268 | if ( hv_exists(MUTABLE_HV(lhv), "alpha", 5 ) ) |
9137345a JP |
5269 | lalpha = TRUE; |
5270 | ||
5271 | /* and the right hand term */ | |
502c6561 | 5272 | rav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(rhv), "version", FALSE))); |
ef8f7699 | 5273 | if ( hv_exists(MUTABLE_HV(rhv), "alpha", 5 ) ) |
9137345a JP |
5274 | ralpha = TRUE; |
5275 | ||
5276 | l = av_len(lav); | |
5277 | r = av_len(rav); | |
ad63d80f JP |
5278 | m = l < r ? l : r; |
5279 | retval = 0; | |
5280 | i = 0; | |
5281 | while ( i <= m && retval == 0 ) | |
5282 | { | |
9137345a JP |
5283 | left = SvIV(*av_fetch(lav,i,0)); |
5284 | right = SvIV(*av_fetch(rav,i,0)); | |
5285 | if ( left < right ) | |
ad63d80f | 5286 | retval = -1; |
9137345a | 5287 | if ( left > right ) |
ad63d80f JP |
5288 | retval = +1; |
5289 | i++; | |
5290 | } | |
5291 | ||
9137345a JP |
5292 | /* tiebreaker for alpha with identical terms */ |
5293 | if ( retval == 0 && l == r && left == right && ( lalpha || ralpha ) ) | |
5294 | { | |
5295 | if ( lalpha && !ralpha ) | |
5296 | { | |
5297 | retval = -1; | |
5298 | } | |
5299 | else if ( ralpha && !lalpha) | |
5300 | { | |
5301 | retval = +1; | |
5302 | } | |
5303 | } | |
5304 | ||
137d6fc0 | 5305 | if ( l != r && retval == 0 ) /* possible match except for trailing 0's */ |
129318bd | 5306 | { |
137d6fc0 | 5307 | if ( l < r ) |
129318bd | 5308 | { |
137d6fc0 JP |
5309 | while ( i <= r && retval == 0 ) |
5310 | { | |
9137345a | 5311 | if ( SvIV(*av_fetch(rav,i,0)) != 0 ) |
137d6fc0 JP |
5312 | retval = -1; /* not a match after all */ |
5313 | i++; | |
5314 | } | |
5315 | } | |
5316 | else | |
5317 | { | |
5318 | while ( i <= l && retval == 0 ) | |
5319 | { | |
9137345a | 5320 | if ( SvIV(*av_fetch(lav,i,0)) != 0 ) |
137d6fc0 JP |
5321 | retval = +1; /* not a match after all */ |
5322 | i++; | |
5323 | } | |
129318bd JP |
5324 | } |
5325 | } | |
ad63d80f JP |
5326 | return retval; |
5327 | } | |
5328 | ||
c95c94b1 | 5329 | #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT) |
2bc69dc4 NIS |
5330 | # define EMULATE_SOCKETPAIR_UDP |
5331 | #endif | |
5332 | ||
5333 | #ifdef EMULATE_SOCKETPAIR_UDP | |
02fc2eee NC |
5334 | static int |
5335 | S_socketpair_udp (int fd[2]) { | |
e10bb1e9 | 5336 | dTHX; |
02fc2eee NC |
5337 | /* Fake a datagram socketpair using UDP to localhost. */ |
5338 | int sockets[2] = {-1, -1}; | |
5339 | struct sockaddr_in addresses[2]; | |
5340 | int i; | |
3aed30dc | 5341 | Sock_size_t size = sizeof(struct sockaddr_in); |
ae92b34e | 5342 | unsigned short port; |
02fc2eee NC |
5343 | int got; |
5344 | ||
3aed30dc | 5345 | memset(&addresses, 0, sizeof(addresses)); |
02fc2eee NC |
5346 | i = 1; |
5347 | do { | |
3aed30dc HS |
5348 | sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET); |
5349 | if (sockets[i] == -1) | |
5350 | goto tidy_up_and_fail; | |
5351 | ||
5352 | addresses[i].sin_family = AF_INET; | |
5353 | addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
5354 | addresses[i].sin_port = 0; /* kernel choses port. */ | |
5355 | if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i], | |
5356 | sizeof(struct sockaddr_in)) == -1) | |
5357 | goto tidy_up_and_fail; | |
02fc2eee NC |
5358 | } while (i--); |
5359 | ||
5360 | /* Now have 2 UDP sockets. Find out which port each is connected to, and | |
5361 | for each connect the other socket to it. */ | |
5362 | i = 1; | |
5363 | do { | |
3aed30dc HS |
5364 | if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i], |
5365 | &size) == -1) | |
5366 | goto tidy_up_and_fail; | |
5367 | if (size != sizeof(struct sockaddr_in)) | |
5368 | goto abort_tidy_up_and_fail; | |
5369 | /* !1 is 0, !0 is 1 */ | |
5370 | if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i], | |
5371 | sizeof(struct sockaddr_in)) == -1) | |
5372 | goto tidy_up_and_fail; | |
02fc2eee NC |
5373 | } while (i--); |
5374 | ||
5375 | /* Now we have 2 sockets connected to each other. I don't trust some other | |
5376 | process not to have already sent a packet to us (by random) so send | |
5377 | a packet from each to the other. */ | |
5378 | i = 1; | |
5379 | do { | |
3aed30dc HS |
5380 | /* I'm going to send my own port number. As a short. |
5381 | (Who knows if someone somewhere has sin_port as a bitfield and needs | |
5382 | this routine. (I'm assuming crays have socketpair)) */ | |
5383 | port = addresses[i].sin_port; | |
5384 | got = PerlLIO_write(sockets[i], &port, sizeof(port)); | |
5385 | if (got != sizeof(port)) { | |
5386 | if (got == -1) | |
5387 | goto tidy_up_and_fail; | |
5388 | goto abort_tidy_up_and_fail; | |
5389 | } | |
02fc2eee NC |
5390 | } while (i--); |
5391 | ||
5392 | /* Packets sent. I don't trust them to have arrived though. | |
5393 | (As I understand it Solaris TCP stack is multithreaded. Non-blocking | |
5394 | connect to localhost will use a second kernel thread. In 2.6 the | |
5395 | first thread running the connect() returns before the second completes, | |
5396 | so EINPROGRESS> In 2.7 the improved stack is faster and connect() | |
5397 | returns 0. Poor programs have tripped up. One poor program's authors' | |
5398 | had a 50-1 reverse stock split. Not sure how connected these were.) | |
5399 | So I don't trust someone not to have an unpredictable UDP stack. | |
5400 | */ | |
5401 | ||
5402 | { | |
3aed30dc HS |
5403 | struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */ |
5404 | int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0]; | |
5405 | fd_set rset; | |
5406 | ||
5407 | FD_ZERO(&rset); | |
ea407a0c NC |
5408 | FD_SET((unsigned int)sockets[0], &rset); |
5409 | FD_SET((unsigned int)sockets[1], &rset); | |
3aed30dc HS |
5410 | |
5411 | got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor); | |
5412 | if (got != 2 || !FD_ISSET(sockets[0], &rset) | |
5413 | || !FD_ISSET(sockets[1], &rset)) { | |
5414 | /* I hope this is portable and appropriate. */ | |
5415 | if (got == -1) | |
5416 | goto tidy_up_and_fail; | |
5417 | goto abort_tidy_up_and_fail; | |
5418 | } | |
02fc2eee | 5419 | } |
f4758303 | 5420 | |
02fc2eee NC |
5421 | /* And the paranoia department even now doesn't trust it to have arrive |
5422 | (hence MSG_DONTWAIT). Or that what arrives was sent by us. */ | |
5423 | { | |
3aed30dc HS |
5424 | struct sockaddr_in readfrom; |
5425 | unsigned short buffer[2]; | |
02fc2eee | 5426 | |
3aed30dc HS |
5427 | i = 1; |
5428 | do { | |
02fc2eee | 5429 | #ifdef MSG_DONTWAIT |
3aed30dc HS |
5430 | got = PerlSock_recvfrom(sockets[i], (char *) &buffer, |
5431 | sizeof(buffer), MSG_DONTWAIT, | |
5432 | (struct sockaddr *) &readfrom, &size); | |
02fc2eee | 5433 | #else |
3aed30dc HS |
5434 | got = PerlSock_recvfrom(sockets[i], (char *) &buffer, |
5435 | sizeof(buffer), 0, | |
5436 | (struct sockaddr *) &readfrom, &size); | |
e10bb1e9 | 5437 | #endif |
02fc2eee | 5438 | |
3aed30dc HS |
5439 | if (got == -1) |
5440 | goto tidy_up_and_fail; | |
5441 | if (got != sizeof(port) | |
5442 | || size != sizeof(struct sockaddr_in) | |
5443 | /* Check other socket sent us its port. */ | |
5444 | || buffer[0] != (unsigned short) addresses[!i].sin_port | |
5445 | /* Check kernel says we got the datagram from that socket */ | |
5446 | || readfrom.sin_family != addresses[!i].sin_family | |
5447 | || readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr | |
5448 | || readfrom.sin_port != addresses[!i].sin_port) | |
5449 | goto abort_tidy_up_and_fail; | |
5450 | } while (i--); | |
02fc2eee NC |
5451 | } |
5452 | /* My caller (my_socketpair) has validated that this is non-NULL */ | |
5453 | fd[0] = sockets[0]; | |
5454 | fd[1] = sockets[1]; | |
5455 | /* I hereby declare this connection open. May God bless all who cross | |
5456 | her. */ | |
5457 | return 0; | |
5458 | ||
5459 | abort_tidy_up_and_fail: | |
5460 | errno = ECONNABORTED; | |
5461 | tidy_up_and_fail: | |
5462 | { | |
4ee39169 | 5463 | dSAVE_ERRNO; |
3aed30dc HS |
5464 | if (sockets[0] != -1) |
5465 | PerlLIO_close(sockets[0]); | |
5466 | if (sockets[1] != -1) | |
5467 | PerlLIO_close(sockets[1]); | |
4ee39169 | 5468 | RESTORE_ERRNO; |
3aed30dc | 5469 | return -1; |
02fc2eee NC |
5470 | } |
5471 | } | |
85ca448a | 5472 | #endif /* EMULATE_SOCKETPAIR_UDP */ |
02fc2eee | 5473 | |
b5ac89c3 | 5474 | #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) |
02fc2eee NC |
5475 | int |
5476 | Perl_my_socketpair (int family, int type, int protocol, int fd[2]) { | |
5477 | /* Stevens says that family must be AF_LOCAL, protocol 0. | |
2948e0bd | 5478 | I'm going to enforce that, then ignore it, and use TCP (or UDP). */ |
e10bb1e9 | 5479 | dTHX; |
02fc2eee NC |
5480 | int listener = -1; |
5481 | int connector = -1; | |
5482 | int acceptor = -1; | |
5483 | struct sockaddr_in listen_addr; | |
5484 | struct sockaddr_in connect_addr; | |
5485 | Sock_size_t size; | |
5486 | ||
50458334 JH |
5487 | if (protocol |
5488 | #ifdef AF_UNIX | |
5489 | || family != AF_UNIX | |
5490 | #endif | |
3aed30dc HS |
5491 | ) { |
5492 | errno = EAFNOSUPPORT; | |
5493 | return -1; | |
02fc2eee | 5494 | } |
2948e0bd | 5495 | if (!fd) { |
3aed30dc HS |
5496 | errno = EINVAL; |
5497 | return -1; | |
2948e0bd | 5498 | } |
02fc2eee | 5499 | |
2bc69dc4 | 5500 | #ifdef EMULATE_SOCKETPAIR_UDP |
02fc2eee | 5501 | if (type == SOCK_DGRAM) |
3aed30dc | 5502 | return S_socketpair_udp(fd); |
2bc69dc4 | 5503 | #endif |
02fc2eee | 5504 | |
3aed30dc | 5505 | listener = PerlSock_socket(AF_INET, type, 0); |
02fc2eee | 5506 | if (listener == -1) |
3aed30dc HS |
5507 | return -1; |
5508 | memset(&listen_addr, 0, sizeof(listen_addr)); | |
02fc2eee | 5509 | listen_addr.sin_family = AF_INET; |
3aed30dc | 5510 | listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
02fc2eee | 5511 | listen_addr.sin_port = 0; /* kernel choses port. */ |
3aed30dc HS |
5512 | if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr, |
5513 | sizeof(listen_addr)) == -1) | |
5514 | goto tidy_up_and_fail; | |
e10bb1e9 | 5515 | if (PerlSock_listen(listener, 1) == -1) |
3aed30dc | 5516 | goto tidy_up_and_fail; |
02fc2eee | 5517 | |
3aed30dc | 5518 | connector = PerlSock_socket(AF_INET, type, 0); |
02fc2eee | 5519 | if (connector == -1) |
3aed30dc | 5520 | goto tidy_up_and_fail; |
02fc2eee | 5521 | /* We want to find out the port number to connect to. */ |
3aed30dc HS |
5522 | size = sizeof(connect_addr); |
5523 | if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr, | |
5524 | &size) == -1) | |
5525 | goto tidy_up_and_fail; | |
5526 | if (size != sizeof(connect_addr)) | |
5527 | goto abort_tidy_up_and_fail; | |
e10bb1e9 | 5528 | if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr, |
3aed30dc HS |
5529 | sizeof(connect_addr)) == -1) |
5530 | goto tidy_up_and_fail; | |
02fc2eee | 5531 | |
3aed30dc HS |
5532 | size = sizeof(listen_addr); |
5533 | acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr, | |
5534 | &size); | |
02fc2eee | 5535 | if (acceptor == -1) |
3aed30dc HS |
5536 | goto tidy_up_and_fail; |
5537 | if (size != sizeof(listen_addr)) | |
5538 | goto abort_tidy_up_and_fail; | |
5539 | PerlLIO_close(listener); | |
02fc2eee NC |
5540 | /* Now check we are talking to ourself by matching port and host on the |
5541 | two sockets. */ | |
3aed30dc HS |
5542 | if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr, |
5543 | &size) == -1) | |
5544 | goto tidy_up_and_fail; | |
5545 | if (size != sizeof(connect_addr) | |
5546 | || listen_addr.sin_family != connect_addr.sin_family | |
5547 | || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr | |
5548 | || listen_addr.sin_port != connect_addr.sin_port) { | |
5549 | goto abort_tidy_up_and_fail; | |
02fc2eee NC |
5550 | } |
5551 | fd[0] = connector; | |
5552 | fd[1] = acceptor; | |
5553 | return 0; | |
5554 | ||
5555 | abort_tidy_up_and_fail: | |
27da23d5 JH |
5556 | #ifdef ECONNABORTED |
5557 | errno = ECONNABORTED; /* This would be the standard thing to do. */ | |
5558 | #else | |
5559 | # ifdef ECONNREFUSED | |
5560 | errno = ECONNREFUSED; /* E.g. Symbian does not have ECONNABORTED. */ | |
5561 | # else | |
5562 | errno = ETIMEDOUT; /* Desperation time. */ | |
5563 | # endif | |
5564 | #endif | |
02fc2eee NC |
5565 | tidy_up_and_fail: |
5566 | { | |
4ee39169 | 5567 | dSAVE_ERRNO; |
3aed30dc HS |
5568 | if (listener != -1) |
5569 | PerlLIO_close(listener); | |
5570 | if (connector != -1) | |
5571 | PerlLIO_close(connector); | |
5572 | if (acceptor != -1) | |
5573 | PerlLIO_close(acceptor); | |
4ee39169 | 5574 | RESTORE_ERRNO; |
3aed30dc | 5575 | return -1; |
02fc2eee NC |
5576 | } |
5577 | } | |
85ca448a | 5578 | #else |
48ea76d1 JH |
5579 | /* In any case have a stub so that there's code corresponding |
5580 | * to the my_socketpair in global.sym. */ | |
5581 | int | |
5582 | Perl_my_socketpair (int family, int type, int protocol, int fd[2]) { | |
daf16542 | 5583 | #ifdef HAS_SOCKETPAIR |
48ea76d1 | 5584 | return socketpair(family, type, protocol, fd); |
daf16542 JH |
5585 | #else |
5586 | return -1; | |
5587 | #endif | |
48ea76d1 JH |
5588 | } |
5589 | #endif | |
5590 | ||
68795e93 NIS |
5591 | /* |
5592 | ||
5593 | =for apidoc sv_nosharing | |
5594 | ||
5595 | Dummy routine which "shares" an SV when there is no sharing module present. | |
d5b2b27b NC |
5596 | Or "locks" it. Or "unlocks" it. In other words, ignores its single SV argument. |
5597 | Exists to avoid test for a NULL function pointer and because it could | |
5598 | potentially warn under some level of strict-ness. | |
68795e93 NIS |
5599 | |
5600 | =cut | |
5601 | */ | |
5602 | ||
5603 | void | |
5604 | Perl_sv_nosharing(pTHX_ SV *sv) | |
5605 | { | |
96a5add6 | 5606 | PERL_UNUSED_CONTEXT; |
53c1dcc0 | 5607 | PERL_UNUSED_ARG(sv); |
68795e93 NIS |
5608 | } |
5609 | ||
eba16661 JH |
5610 | /* |
5611 | ||
5612 | =for apidoc sv_destroyable | |
5613 | ||
5614 | Dummy routine which reports that object can be destroyed when there is no | |
5615 | sharing module present. It ignores its single SV argument, and returns | |
5616 | 'true'. Exists to avoid test for a NULL function pointer and because it | |
5617 | could potentially warn under some level of strict-ness. | |
5618 | ||
5619 | =cut | |
5620 | */ | |
5621 | ||
5622 | bool | |
5623 | Perl_sv_destroyable(pTHX_ SV *sv) | |
5624 | { | |
5625 | PERL_UNUSED_CONTEXT; | |
5626 | PERL_UNUSED_ARG(sv); | |
5627 | return TRUE; | |
5628 | } | |
5629 | ||
a05d7ebb | 5630 | U32 |
e1ec3a88 | 5631 | Perl_parse_unicode_opts(pTHX_ const char **popt) |
a05d7ebb | 5632 | { |
e1ec3a88 | 5633 | const char *p = *popt; |
a05d7ebb JH |
5634 | U32 opt = 0; |
5635 | ||
7918f24d NC |
5636 | PERL_ARGS_ASSERT_PARSE_UNICODE_OPTS; |
5637 | ||
a05d7ebb JH |
5638 | if (*p) { |
5639 | if (isDIGIT(*p)) { | |
5640 | opt = (U32) atoi(p); | |
35da51f7 AL |
5641 | while (isDIGIT(*p)) |
5642 | p++; | |
7c91f477 | 5643 | if (*p && *p != '\n' && *p != '\r') |
a05d7ebb JH |
5644 | Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p); |
5645 | } | |
5646 | else { | |
5647 | for (; *p; p++) { | |
5648 | switch (*p) { | |
5649 | case PERL_UNICODE_STDIN: | |
5650 | opt |= PERL_UNICODE_STDIN_FLAG; break; | |
5651 | case PERL_UNICODE_STDOUT: | |
5652 | opt |= PERL_UNICODE_STDOUT_FLAG; break; | |
5653 | case PERL_UNICODE_STDERR: | |
5654 | opt |= PERL_UNICODE_STDERR_FLAG; break; | |
5655 | case PERL_UNICODE_STD: | |
5656 | opt |= PERL_UNICODE_STD_FLAG; break; | |
5657 | case PERL_UNICODE_IN: | |
5658 | opt |= PERL_UNICODE_IN_FLAG; break; | |
5659 | case PERL_UNICODE_OUT: | |
5660 | opt |= PERL_UNICODE_OUT_FLAG; break; | |
5661 | case PERL_UNICODE_INOUT: | |
5662 | opt |= PERL_UNICODE_INOUT_FLAG; break; | |
5663 | case PERL_UNICODE_LOCALE: | |
5664 | opt |= PERL_UNICODE_LOCALE_FLAG; break; | |
5665 | case PERL_UNICODE_ARGV: | |
5666 | opt |= PERL_UNICODE_ARGV_FLAG; break; | |
5a22a2bb NC |
5667 | case PERL_UNICODE_UTF8CACHEASSERT: |
5668 | opt |= PERL_UNICODE_UTF8CACHEASSERT_FLAG; break; | |
a05d7ebb | 5669 | default: |
7c91f477 JH |
5670 | if (*p != '\n' && *p != '\r') |
5671 | Perl_croak(aTHX_ | |
5672 | "Unknown Unicode option letter '%c'", *p); | |
a05d7ebb JH |
5673 | } |
5674 | } | |
5675 | } | |
5676 | } | |
5677 | else | |
5678 | opt = PERL_UNICODE_DEFAULT_FLAGS; | |
5679 | ||
5680 | if (opt & ~PERL_UNICODE_ALL_FLAGS) | |
06e66572 | 5681 | Perl_croak(aTHX_ "Unknown Unicode option value %"UVuf, |
a05d7ebb JH |
5682 | (UV) (opt & ~PERL_UNICODE_ALL_FLAGS)); |
5683 | ||
5684 | *popt = p; | |
5685 | ||
5686 | return opt; | |
5687 | } | |
5688 | ||
132efe8b JH |
5689 | U32 |
5690 | Perl_seed(pTHX) | |
5691 | { | |
97aff369 | 5692 | dVAR; |
132efe8b JH |
5693 | /* |
5694 | * This is really just a quick hack which grabs various garbage | |
5695 | * values. It really should be a real hash algorithm which | |
5696 | * spreads the effect of every input bit onto every output bit, | |
5697 | * if someone who knows about such things would bother to write it. | |
5698 | * Might be a good idea to add that function to CORE as well. | |
5699 | * No numbers below come from careful analysis or anything here, | |
5700 | * except they are primes and SEED_C1 > 1E6 to get a full-width | |
5701 | * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should | |
5702 | * probably be bigger too. | |
5703 | */ | |
5704 | #if RANDBITS > 16 | |
5705 | # define SEED_C1 1000003 | |
5706 | #define SEED_C4 73819 | |
5707 | #else | |
5708 | # define SEED_C1 25747 | |
5709 | #define SEED_C4 20639 | |
5710 | #endif | |
5711 | #define SEED_C2 3 | |
5712 | #define SEED_C3 269 | |
5713 | #define SEED_C5 26107 | |
5714 | ||
5715 | #ifndef PERL_NO_DEV_RANDOM | |
5716 | int fd; | |
5717 | #endif | |
5718 | U32 u; | |
5719 | #ifdef VMS | |
5720 | # include <starlet.h> | |
5721 | /* when[] = (low 32 bits, high 32 bits) of time since epoch | |
5722 | * in 100-ns units, typically incremented ever 10 ms. */ | |
5723 | unsigned int when[2]; | |
5724 | #else | |
5725 | # ifdef HAS_GETTIMEOFDAY | |
5726 | struct timeval when; | |
5727 | # else | |
5728 | Time_t when; | |
5729 | # endif | |
5730 | #endif | |
5731 | ||
5732 | /* This test is an escape hatch, this symbol isn't set by Configure. */ | |
5733 | #ifndef PERL_NO_DEV_RANDOM | |
5734 | #ifndef PERL_RANDOM_DEVICE | |
5735 | /* /dev/random isn't used by default because reads from it will block | |
5736 | * if there isn't enough entropy available. You can compile with | |
5737 | * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there | |
5738 | * is enough real entropy to fill the seed. */ | |
5739 | # define PERL_RANDOM_DEVICE "/dev/urandom" | |
5740 | #endif | |
5741 | fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0); | |
5742 | if (fd != -1) { | |
27da23d5 | 5743 | if (PerlLIO_read(fd, (void*)&u, sizeof u) != sizeof u) |
132efe8b JH |
5744 | u = 0; |
5745 | PerlLIO_close(fd); | |
5746 | if (u) | |
5747 | return u; | |
5748 | } | |
5749 | #endif | |
5750 | ||
5751 | #ifdef VMS | |
5752 | _ckvmssts(sys$gettim(when)); | |
5753 | u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1]; | |
5754 | #else | |
5755 | # ifdef HAS_GETTIMEOFDAY | |
5756 | PerlProc_gettimeofday(&when,NULL); | |
5757 | u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec; | |
5758 | # else | |
5759 | (void)time(&when); | |
5760 | u = (U32)SEED_C1 * when; | |
5761 | # endif | |
5762 | #endif | |
5763 | u += SEED_C3 * (U32)PerlProc_getpid(); | |
5764 | u += SEED_C4 * (U32)PTR2UV(PL_stack_sp); | |
5765 | #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */ | |
5766 | u += SEED_C5 * (U32)PTR2UV(&when); | |
5767 | #endif | |
5768 | return u; | |
5769 | } | |
5770 | ||
bed60192 | 5771 | UV |
a783c5f4 | 5772 | Perl_get_hash_seed(pTHX) |
bed60192 | 5773 | { |
97aff369 | 5774 | dVAR; |
e1ec3a88 | 5775 | const char *s = PerlEnv_getenv("PERL_HASH_SEED"); |
bed60192 JH |
5776 | UV myseed = 0; |
5777 | ||
5778 | if (s) | |
35da51f7 AL |
5779 | while (isSPACE(*s)) |
5780 | s++; | |
bed60192 JH |
5781 | if (s && isDIGIT(*s)) |
5782 | myseed = (UV)Atoul(s); | |
5783 | else | |
5784 | #ifdef USE_HASH_SEED_EXPLICIT | |
5785 | if (s) | |
5786 | #endif | |
5787 | { | |
5788 | /* Compute a random seed */ | |
5789 | (void)seedDrand01((Rand_seed_t)seed()); | |
bed60192 JH |
5790 | myseed = (UV)(Drand01() * (NV)UV_MAX); |
5791 | #if RANDBITS < (UVSIZE * 8) | |
5792 | /* Since there are not enough randbits to to reach all | |
5793 | * the bits of a UV, the low bits might need extra | |
5794 | * help. Sum in another random number that will | |
5795 | * fill in the low bits. */ | |
5796 | myseed += | |
fa58a56f | 5797 | (UV)(Drand01() * (NV)((((UV)1) << ((UVSIZE * 8 - RANDBITS))) - 1)); |
bed60192 | 5798 | #endif /* RANDBITS < (UVSIZE * 8) */ |
6cfd5ea7 JH |
5799 | if (myseed == 0) { /* Superparanoia. */ |
5800 | myseed = (UV)(Drand01() * (NV)UV_MAX); /* One more chance. */ | |
5801 | if (myseed == 0) | |
5802 | Perl_croak(aTHX_ "Your random numbers are not that random"); | |
5803 | } | |
bed60192 | 5804 | } |
008fb0c0 | 5805 | PL_rehash_seed_set = TRUE; |
bed60192 JH |
5806 | |
5807 | return myseed; | |
5808 | } | |
27da23d5 | 5809 | |
ed221c57 AL |
5810 | #ifdef USE_ITHREADS |
5811 | bool | |
5812 | Perl_stashpv_hvname_match(pTHX_ const COP *c, const HV *hv) | |
5813 | { | |
5814 | const char * const stashpv = CopSTASHPV(c); | |
5815 | const char * const name = HvNAME_get(hv); | |
96a5add6 | 5816 | PERL_UNUSED_CONTEXT; |
7918f24d | 5817 | PERL_ARGS_ASSERT_STASHPV_HVNAME_MATCH; |
ed221c57 AL |
5818 | |
5819 | if (stashpv == name) | |
5820 | return TRUE; | |
5821 | if (stashpv && name) | |
5822 | if (strEQ(stashpv, name)) | |
5823 | return TRUE; | |
5824 | return FALSE; | |
5825 | } | |
5826 | #endif | |
5827 | ||
5828 | ||
27da23d5 JH |
5829 | #ifdef PERL_GLOBAL_STRUCT |
5830 | ||
bae1192d JH |
5831 | #define PERL_GLOBAL_STRUCT_INIT |
5832 | #include "opcode.h" /* the ppaddr and check */ | |
5833 | ||
27da23d5 JH |
5834 | struct perl_vars * |
5835 | Perl_init_global_struct(pTHX) | |
5836 | { | |
5837 | struct perl_vars *plvarsp = NULL; | |
bae1192d | 5838 | # ifdef PERL_GLOBAL_STRUCT |
7452cf6a AL |
5839 | const IV nppaddr = sizeof(Gppaddr)/sizeof(Perl_ppaddr_t); |
5840 | const IV ncheck = sizeof(Gcheck) /sizeof(Perl_check_t); | |
27da23d5 JH |
5841 | # ifdef PERL_GLOBAL_STRUCT_PRIVATE |
5842 | /* PerlMem_malloc() because can't use even safesysmalloc() this early. */ | |
5843 | plvarsp = (struct perl_vars*)PerlMem_malloc(sizeof(struct perl_vars)); | |
5844 | if (!plvarsp) | |
5845 | exit(1); | |
5846 | # else | |
5847 | plvarsp = PL_VarsPtr; | |
5848 | # endif /* PERL_GLOBAL_STRUCT_PRIVATE */ | |
aadb217d JH |
5849 | # undef PERLVAR |
5850 | # undef PERLVARA | |
5851 | # undef PERLVARI | |
5852 | # undef PERLVARIC | |
5853 | # undef PERLVARISC | |
27da23d5 JH |
5854 | # define PERLVAR(var,type) /**/ |
5855 | # define PERLVARA(var,n,type) /**/ | |
5856 | # define PERLVARI(var,type,init) plvarsp->var = init; | |
5857 | # define PERLVARIC(var,type,init) plvarsp->var = init; | |
5858 | # define PERLVARISC(var,init) Copy(init, plvarsp->var, sizeof(init), char); | |
5859 | # include "perlvars.h" | |
5860 | # undef PERLVAR | |
5861 | # undef PERLVARA | |
5862 | # undef PERLVARI | |
5863 | # undef PERLVARIC | |
5864 | # undef PERLVARISC | |
5865 | # ifdef PERL_GLOBAL_STRUCT | |
bae1192d JH |
5866 | plvarsp->Gppaddr = |
5867 | (Perl_ppaddr_t*) | |
5868 | PerlMem_malloc(nppaddr * sizeof(Perl_ppaddr_t)); | |
27da23d5 JH |
5869 | if (!plvarsp->Gppaddr) |
5870 | exit(1); | |
bae1192d JH |
5871 | plvarsp->Gcheck = |
5872 | (Perl_check_t*) | |
5873 | PerlMem_malloc(ncheck * sizeof(Perl_check_t)); | |
27da23d5 JH |
5874 | if (!plvarsp->Gcheck) |
5875 | exit(1); | |
5876 | Copy(Gppaddr, plvarsp->Gppaddr, nppaddr, Perl_ppaddr_t); | |
5877 | Copy(Gcheck, plvarsp->Gcheck, ncheck, Perl_check_t); | |
5878 | # endif | |
5879 | # ifdef PERL_SET_VARS | |
5880 | PERL_SET_VARS(plvarsp); | |
5881 | # endif | |
bae1192d JH |
5882 | # undef PERL_GLOBAL_STRUCT_INIT |
5883 | # endif | |
27da23d5 JH |
5884 | return plvarsp; |
5885 | } | |
5886 | ||
5887 | #endif /* PERL_GLOBAL_STRUCT */ | |
5888 | ||
5889 | #ifdef PERL_GLOBAL_STRUCT | |
5890 | ||
5891 | void | |
5892 | Perl_free_global_struct(pTHX_ struct perl_vars *plvarsp) | |
5893 | { | |
7918f24d | 5894 | PERL_ARGS_ASSERT_FREE_GLOBAL_STRUCT; |
bae1192d | 5895 | # ifdef PERL_GLOBAL_STRUCT |
27da23d5 JH |
5896 | # ifdef PERL_UNSET_VARS |
5897 | PERL_UNSET_VARS(plvarsp); | |
5898 | # endif | |
5899 | free(plvarsp->Gppaddr); | |
5900 | free(plvarsp->Gcheck); | |
bae1192d | 5901 | # ifdef PERL_GLOBAL_STRUCT_PRIVATE |
27da23d5 | 5902 | free(plvarsp); |
bae1192d JH |
5903 | # endif |
5904 | # endif | |
27da23d5 JH |
5905 | } |
5906 | ||
5907 | #endif /* PERL_GLOBAL_STRUCT */ | |
5908 | ||
fe4f188c JH |
5909 | #ifdef PERL_MEM_LOG |
5910 | ||
1cd8acb5 | 5911 | /* -DPERL_MEM_LOG: the Perl_mem_log_..() is compiled, including the |
73d1d973 JC |
5912 | * the default implementation, unless -DPERL_MEM_LOG_NOIMPL is also |
5913 | * given, and you supply your own implementation. | |
65ceff02 | 5914 | * |
2e5b5004 | 5915 | * The default implementation reads a single env var, PERL_MEM_LOG, |
1cd8acb5 JC |
5916 | * expecting one or more of the following: |
5917 | * | |
5918 | * \d+ - fd fd to write to : must be 1st (atoi) | |
2e5b5004 | 5919 | * 'm' - memlog was PERL_MEM_LOG=1 |
1cd8acb5 JC |
5920 | * 's' - svlog was PERL_SV_LOG=1 |
5921 | * 't' - timestamp was PERL_MEM_LOG_TIMESTAMP=1 | |
0b0ab801 | 5922 | * |
1cd8acb5 JC |
5923 | * This makes the logger controllable enough that it can reasonably be |
5924 | * added to the system perl. | |
65ceff02 JH |
5925 | */ |
5926 | ||
1cd8acb5 | 5927 | /* -DPERL_MEM_LOG_SPRINTF_BUF_SIZE=X: size of a (stack-allocated) buffer |
65ceff02 JH |
5928 | * the Perl_mem_log_...() will use (either via sprintf or snprintf). |
5929 | */ | |
e352bcff JH |
5930 | #define PERL_MEM_LOG_SPRINTF_BUF_SIZE 128 |
5931 | ||
1cd8acb5 JC |
5932 | /* -DPERL_MEM_LOG_FD=N: the file descriptor the Perl_mem_log_...() |
5933 | * writes to. In the default logger, this is settable at runtime. | |
65ceff02 JH |
5934 | */ |
5935 | #ifndef PERL_MEM_LOG_FD | |
5936 | # define PERL_MEM_LOG_FD 2 /* If STDERR is too boring for you. */ | |
5937 | #endif | |
5938 | ||
73d1d973 | 5939 | #ifndef PERL_MEM_LOG_NOIMPL |
d7a2c63c MHM |
5940 | |
5941 | # ifdef DEBUG_LEAKING_SCALARS | |
5942 | # define SV_LOG_SERIAL_FMT " [%lu]" | |
5943 | # define _SV_LOG_SERIAL_ARG(sv) , (unsigned long) (sv)->sv_debug_serial | |
5944 | # else | |
5945 | # define SV_LOG_SERIAL_FMT | |
5946 | # define _SV_LOG_SERIAL_ARG(sv) | |
5947 | # endif | |
5948 | ||
0b0ab801 | 5949 | static void |
73d1d973 JC |
5950 | S_mem_log_common(enum mem_log_type mlt, const UV n, |
5951 | const UV typesize, const char *type_name, const SV *sv, | |
5952 | Malloc_t oldalloc, Malloc_t newalloc, | |
5953 | const char *filename, const int linenumber, | |
5954 | const char *funcname) | |
0b0ab801 | 5955 | { |
1cd8acb5 | 5956 | const char *pmlenv; |
4ca7bcef | 5957 | |
1cd8acb5 | 5958 | PERL_ARGS_ASSERT_MEM_LOG_COMMON; |
4ca7bcef | 5959 | |
1cd8acb5 JC |
5960 | pmlenv = PerlEnv_getenv("PERL_MEM_LOG"); |
5961 | if (!pmlenv) | |
5962 | return; | |
5963 | if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s')) | |
65ceff02 JH |
5964 | { |
5965 | /* We can't use SVs or PerlIO for obvious reasons, | |
5966 | * so we'll use stdio and low-level IO instead. */ | |
5967 | char buf[PERL_MEM_LOG_SPRINTF_BUF_SIZE]; | |
1cd8acb5 | 5968 | |
5b692037 | 5969 | # ifdef HAS_GETTIMEOFDAY |
0b0ab801 MHM |
5970 | # define MEM_LOG_TIME_FMT "%10d.%06d: " |
5971 | # define MEM_LOG_TIME_ARG (int)tv.tv_sec, (int)tv.tv_usec | |
5972 | struct timeval tv; | |
65ceff02 | 5973 | gettimeofday(&tv, 0); |
0b0ab801 MHM |
5974 | # else |
5975 | # define MEM_LOG_TIME_FMT "%10d: " | |
5976 | # define MEM_LOG_TIME_ARG (int)when | |
5977 | Time_t when; | |
5978 | (void)time(&when); | |
5b692037 JH |
5979 | # endif |
5980 | /* If there are other OS specific ways of hires time than | |
40d04ec4 | 5981 | * gettimeofday() (see ext/Time-HiRes), the easiest way is |
5b692037 JH |
5982 | * probably that they would be used to fill in the struct |
5983 | * timeval. */ | |
65ceff02 | 5984 | { |
0b0ab801 | 5985 | STRLEN len; |
1cd8acb5 JC |
5986 | int fd = atoi(pmlenv); |
5987 | if (!fd) | |
5988 | fd = PERL_MEM_LOG_FD; | |
0b0ab801 | 5989 | |
1cd8acb5 | 5990 | if (strchr(pmlenv, 't')) { |
0b0ab801 MHM |
5991 | len = my_snprintf(buf, sizeof(buf), |
5992 | MEM_LOG_TIME_FMT, MEM_LOG_TIME_ARG); | |
5993 | PerlLIO_write(fd, buf, len); | |
5994 | } | |
0b0ab801 MHM |
5995 | switch (mlt) { |
5996 | case MLT_ALLOC: | |
5997 | len = my_snprintf(buf, sizeof(buf), | |
5998 | "alloc: %s:%d:%s: %"IVdf" %"UVuf | |
5999 | " %s = %"IVdf": %"UVxf"\n", | |
6000 | filename, linenumber, funcname, n, typesize, | |
bef8a128 | 6001 | type_name, n * typesize, PTR2UV(newalloc)); |
0b0ab801 MHM |
6002 | break; |
6003 | case MLT_REALLOC: | |
6004 | len = my_snprintf(buf, sizeof(buf), | |
6005 | "realloc: %s:%d:%s: %"IVdf" %"UVuf | |
6006 | " %s = %"IVdf": %"UVxf" -> %"UVxf"\n", | |
6007 | filename, linenumber, funcname, n, typesize, | |
bef8a128 | 6008 | type_name, n * typesize, PTR2UV(oldalloc), |
0b0ab801 MHM |
6009 | PTR2UV(newalloc)); |
6010 | break; | |
6011 | case MLT_FREE: | |
6012 | len = my_snprintf(buf, sizeof(buf), | |
6013 | "free: %s:%d:%s: %"UVxf"\n", | |
6014 | filename, linenumber, funcname, | |
6015 | PTR2UV(oldalloc)); | |
6016 | break; | |
d7a2c63c MHM |
6017 | case MLT_NEW_SV: |
6018 | case MLT_DEL_SV: | |
6019 | len = my_snprintf(buf, sizeof(buf), | |
6020 | "%s_SV: %s:%d:%s: %"UVxf SV_LOG_SERIAL_FMT "\n", | |
6021 | mlt == MLT_NEW_SV ? "new" : "del", | |
6022 | filename, linenumber, funcname, | |
6023 | PTR2UV(sv) _SV_LOG_SERIAL_ARG(sv)); | |
6024 | break; | |
73d1d973 JC |
6025 | default: |
6026 | len = 0; | |
0b0ab801 MHM |
6027 | } |
6028 | PerlLIO_write(fd, buf, len); | |
65ceff02 JH |
6029 | } |
6030 | } | |
0b0ab801 | 6031 | } |
73d1d973 JC |
6032 | #endif /* !PERL_MEM_LOG_NOIMPL */ |
6033 | ||
6034 | #ifndef PERL_MEM_LOG_NOIMPL | |
6035 | # define \ | |
6036 | mem_log_common_if(alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) \ | |
6037 | mem_log_common (alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) | |
6038 | #else | |
6039 | /* this is suboptimal, but bug compatible. User is providing their | |
6040 | own implemenation, but is getting these functions anyway, and they | |
6041 | do nothing. But _NOIMPL users should be able to cope or fix */ | |
6042 | # define \ | |
6043 | mem_log_common_if(alty, num, tysz, tynm, u, oal, nal, flnm, ln, fnnm) \ | |
6044 | /* mem_log_common_if_PERL_MEM_LOG_NOIMPL */ | |
0b0ab801 MHM |
6045 | #endif |
6046 | ||
6047 | Malloc_t | |
73d1d973 JC |
6048 | Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name, |
6049 | Malloc_t newalloc, | |
6050 | const char *filename, const int linenumber, | |
6051 | const char *funcname) | |
6052 | { | |
6053 | mem_log_common_if(MLT_ALLOC, n, typesize, type_name, | |
6054 | NULL, NULL, newalloc, | |
6055 | filename, linenumber, funcname); | |
fe4f188c JH |
6056 | return newalloc; |
6057 | } | |
6058 | ||
6059 | Malloc_t | |
73d1d973 JC |
6060 | Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name, |
6061 | Malloc_t oldalloc, Malloc_t newalloc, | |
6062 | const char *filename, const int linenumber, | |
6063 | const char *funcname) | |
6064 | { | |
6065 | mem_log_common_if(MLT_REALLOC, n, typesize, type_name, | |
6066 | NULL, oldalloc, newalloc, | |
6067 | filename, linenumber, funcname); | |
fe4f188c JH |
6068 | return newalloc; |
6069 | } | |
6070 | ||
6071 | Malloc_t | |
73d1d973 JC |
6072 | Perl_mem_log_free(Malloc_t oldalloc, |
6073 | const char *filename, const int linenumber, | |
6074 | const char *funcname) | |
fe4f188c | 6075 | { |
73d1d973 JC |
6076 | mem_log_common_if(MLT_FREE, 0, 0, "", NULL, oldalloc, NULL, |
6077 | filename, linenumber, funcname); | |
fe4f188c JH |
6078 | return oldalloc; |
6079 | } | |
6080 | ||
d7a2c63c | 6081 | void |
73d1d973 JC |
6082 | Perl_mem_log_new_sv(const SV *sv, |
6083 | const char *filename, const int linenumber, | |
6084 | const char *funcname) | |
d7a2c63c | 6085 | { |
73d1d973 JC |
6086 | mem_log_common_if(MLT_NEW_SV, 0, 0, "", sv, NULL, NULL, |
6087 | filename, linenumber, funcname); | |
d7a2c63c MHM |
6088 | } |
6089 | ||
6090 | void | |
73d1d973 JC |
6091 | Perl_mem_log_del_sv(const SV *sv, |
6092 | const char *filename, const int linenumber, | |
6093 | const char *funcname) | |
d7a2c63c | 6094 | { |
73d1d973 JC |
6095 | mem_log_common_if(MLT_DEL_SV, 0, 0, "", sv, NULL, NULL, |
6096 | filename, linenumber, funcname); | |
d7a2c63c MHM |
6097 | } |
6098 | ||
fe4f188c JH |
6099 | #endif /* PERL_MEM_LOG */ |
6100 | ||
66610fdd | 6101 | /* |
ce582cee NC |
6102 | =for apidoc my_sprintf |
6103 | ||
6104 | The C library C<sprintf>, wrapped if necessary, to ensure that it will return | |
6105 | the length of the string written to the buffer. Only rare pre-ANSI systems | |
6106 | need the wrapper function - usually this is a direct call to C<sprintf>. | |
6107 | ||
6108 | =cut | |
6109 | */ | |
6110 | #ifndef SPRINTF_RETURNS_STRLEN | |
6111 | int | |
6112 | Perl_my_sprintf(char *buffer, const char* pat, ...) | |
6113 | { | |
6114 | va_list args; | |
7918f24d | 6115 | PERL_ARGS_ASSERT_MY_SPRINTF; |
ce582cee NC |
6116 | va_start(args, pat); |
6117 | vsprintf(buffer, pat, args); | |
6118 | va_end(args); | |
6119 | return strlen(buffer); | |
6120 | } | |
6121 | #endif | |
6122 | ||
d9fad198 JH |
6123 | /* |
6124 | =for apidoc my_snprintf | |
6125 | ||
6126 | The C library C<snprintf> functionality, if available and | |
5b692037 | 6127 | standards-compliant (uses C<vsnprintf>, actually). However, if the |
d9fad198 | 6128 | C<vsnprintf> is not available, will unfortunately use the unsafe |
5b692037 JH |
6129 | C<vsprintf> which can overrun the buffer (there is an overrun check, |
6130 | but that may be too late). Consider using C<sv_vcatpvf> instead, or | |
6131 | getting C<vsnprintf>. | |
d9fad198 JH |
6132 | |
6133 | =cut | |
6134 | */ | |
6135 | int | |
6136 | Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...) | |
d9fad198 JH |
6137 | { |
6138 | dTHX; | |
6139 | int retval; | |
6140 | va_list ap; | |
7918f24d | 6141 | PERL_ARGS_ASSERT_MY_SNPRINTF; |
d9fad198 | 6142 | va_start(ap, format); |
5b692037 | 6143 | #ifdef HAS_VSNPRINTF |
d9fad198 JH |
6144 | retval = vsnprintf(buffer, len, format, ap); |
6145 | #else | |
6146 | retval = vsprintf(buffer, format, ap); | |
6147 | #endif | |
6148 | va_end(ap); | |
1208b3dd | 6149 | /* vsnprintf() shows failure with >= len, vsprintf() with < 0 */ |
625dac9d | 6150 | if (retval < 0 || (len > 0 && (Size_t)retval >= len)) |
5b692037 | 6151 | Perl_croak(aTHX_ "panic: my_snprintf buffer overflow"); |
d9fad198 JH |
6152 | return retval; |
6153 | } | |
6154 | ||
6155 | /* | |
6156 | =for apidoc my_vsnprintf | |
6157 | ||
5b692037 JH |
6158 | The C library C<vsnprintf> if available and standards-compliant. |
6159 | However, if if the C<vsnprintf> is not available, will unfortunately | |
6160 | use the unsafe C<vsprintf> which can overrun the buffer (there is an | |
6161 | overrun check, but that may be too late). Consider using | |
6162 | C<sv_vcatpvf> instead, or getting C<vsnprintf>. | |
d9fad198 JH |
6163 | |
6164 | =cut | |
6165 | */ | |
6166 | int | |
6167 | Perl_my_vsnprintf(char *buffer, const Size_t len, const char *format, va_list ap) | |
d9fad198 JH |
6168 | { |
6169 | dTHX; | |
6170 | int retval; | |
d9fad198 JH |
6171 | #ifdef NEED_VA_COPY |
6172 | va_list apc; | |
7918f24d NC |
6173 | |
6174 | PERL_ARGS_ASSERT_MY_VSNPRINTF; | |
6175 | ||
239fec62 | 6176 | Perl_va_copy(ap, apc); |
5b692037 | 6177 | # ifdef HAS_VSNPRINTF |
d9fad198 JH |
6178 | retval = vsnprintf(buffer, len, format, apc); |
6179 | # else | |
6180 | retval = vsprintf(buffer, format, apc); | |
6181 | # endif | |
6182 | #else | |
5b692037 | 6183 | # ifdef HAS_VSNPRINTF |
d9fad198 JH |
6184 | retval = vsnprintf(buffer, len, format, ap); |
6185 | # else | |
6186 | retval = vsprintf(buffer, format, ap); | |
6187 | # endif | |
5b692037 | 6188 | #endif /* #ifdef NEED_VA_COPY */ |
1208b3dd | 6189 | /* vsnprintf() shows failure with >= len, vsprintf() with < 0 */ |
625dac9d | 6190 | if (retval < 0 || (len > 0 && (Size_t)retval >= len)) |
5b692037 | 6191 | Perl_croak(aTHX_ "panic: my_vsnprintf buffer overflow"); |
d9fad198 JH |
6192 | return retval; |
6193 | } | |
6194 | ||
b0269e46 AB |
6195 | void |
6196 | Perl_my_clearenv(pTHX) | |
6197 | { | |
6198 | dVAR; | |
6199 | #if ! defined(PERL_MICRO) | |
6200 | # if defined(PERL_IMPLICIT_SYS) || defined(WIN32) | |
6201 | PerlEnv_clearenv(); | |
6202 | # else /* ! (PERL_IMPLICIT_SYS || WIN32) */ | |
6203 | # if defined(USE_ENVIRON_ARRAY) | |
6204 | # if defined(USE_ITHREADS) | |
6205 | /* only the parent thread can clobber the process environment */ | |
6206 | if (PL_curinterp == aTHX) | |
6207 | # endif /* USE_ITHREADS */ | |
6208 | { | |
6209 | # if ! defined(PERL_USE_SAFE_PUTENV) | |
6210 | if ( !PL_use_safe_putenv) { | |
6211 | I32 i; | |
6212 | if (environ == PL_origenviron) | |
6213 | environ = (char**)safesysmalloc(sizeof(char*)); | |
6214 | else | |
6215 | for (i = 0; environ[i]; i++) | |
6216 | (void)safesysfree(environ[i]); | |
6217 | } | |
6218 | environ[0] = NULL; | |
6219 | # else /* PERL_USE_SAFE_PUTENV */ | |
6220 | # if defined(HAS_CLEARENV) | |
6221 | (void)clearenv(); | |
6222 | # elif defined(HAS_UNSETENV) | |
6223 | int bsiz = 80; /* Most envvar names will be shorter than this. */ | |
d1307786 JH |
6224 | int bufsiz = bsiz * sizeof(char); /* sizeof(char) paranoid? */ |
6225 | char *buf = (char*)safesysmalloc(bufsiz); | |
b0269e46 AB |
6226 | while (*environ != NULL) { |
6227 | char *e = strchr(*environ, '='); | |
b57a0404 | 6228 | int l = e ? e - *environ : (int)strlen(*environ); |
b0269e46 AB |
6229 | if (bsiz < l + 1) { |
6230 | (void)safesysfree(buf); | |
1bdfa2de | 6231 | bsiz = l + 1; /* + 1 for the \0. */ |
d1307786 | 6232 | buf = (char*)safesysmalloc(bufsiz); |
b0269e46 | 6233 | } |
82d8bb49 NC |
6234 | memcpy(buf, *environ, l); |
6235 | buf[l] = '\0'; | |
b0269e46 AB |
6236 | (void)unsetenv(buf); |
6237 | } | |
6238 | (void)safesysfree(buf); | |
6239 | # else /* ! HAS_CLEARENV && ! HAS_UNSETENV */ | |
6240 | /* Just null environ and accept the leakage. */ | |
6241 | *environ = NULL; | |
6242 | # endif /* HAS_CLEARENV || HAS_UNSETENV */ | |
6243 | # endif /* ! PERL_USE_SAFE_PUTENV */ | |
6244 | } | |
6245 | # endif /* USE_ENVIRON_ARRAY */ | |
6246 | # endif /* PERL_IMPLICIT_SYS || WIN32 */ | |
6247 | #endif /* PERL_MICRO */ | |
6248 | } | |
6249 | ||
f16dd614 DM |
6250 | #ifdef PERL_IMPLICIT_CONTEXT |
6251 | ||
53d44271 | 6252 | /* Implements the MY_CXT_INIT macro. The first time a module is loaded, |
f16dd614 DM |
6253 | the global PL_my_cxt_index is incremented, and that value is assigned to |
6254 | that module's static my_cxt_index (who's address is passed as an arg). | |
6255 | Then, for each interpreter this function is called for, it makes sure a | |
6256 | void* slot is available to hang the static data off, by allocating or | |
6257 | extending the interpreter's PL_my_cxt_list array */ | |
6258 | ||
53d44271 | 6259 | #ifndef PERL_GLOBAL_STRUCT_PRIVATE |
f16dd614 DM |
6260 | void * |
6261 | Perl_my_cxt_init(pTHX_ int *index, size_t size) | |
6262 | { | |
97aff369 | 6263 | dVAR; |
f16dd614 | 6264 | void *p; |
7918f24d | 6265 | PERL_ARGS_ASSERT_MY_CXT_INIT; |
f16dd614 DM |
6266 | if (*index == -1) { |
6267 | /* this module hasn't been allocated an index yet */ | |
8703a9a4 | 6268 | #if defined(USE_ITHREADS) |
f16dd614 | 6269 | MUTEX_LOCK(&PL_my_ctx_mutex); |
8703a9a4 | 6270 | #endif |
f16dd614 | 6271 | *index = PL_my_cxt_index++; |
8703a9a4 | 6272 | #if defined(USE_ITHREADS) |
f16dd614 | 6273 | MUTEX_UNLOCK(&PL_my_ctx_mutex); |
8703a9a4 | 6274 | #endif |
f16dd614 DM |
6275 | } |
6276 | ||
6277 | /* make sure the array is big enough */ | |
4c901e72 DM |
6278 | if (PL_my_cxt_size <= *index) { |
6279 | if (PL_my_cxt_size) { | |
6280 | while (PL_my_cxt_size <= *index) | |
f16dd614 DM |
6281 | PL_my_cxt_size *= 2; |
6282 | Renew(PL_my_cxt_list, PL_my_cxt_size, void *); | |
6283 | } | |
6284 | else { | |
6285 | PL_my_cxt_size = 16; | |
6286 | Newx(PL_my_cxt_list, PL_my_cxt_size, void *); | |
6287 | } | |
6288 | } | |
6289 | /* newSV() allocates one more than needed */ | |
6290 | p = (void*)SvPVX(newSV(size-1)); | |
6291 | PL_my_cxt_list[*index] = p; | |
6292 | Zero(p, size, char); | |
6293 | return p; | |
6294 | } | |
53d44271 JH |
6295 | |
6296 | #else /* #ifndef PERL_GLOBAL_STRUCT_PRIVATE */ | |
6297 | ||
6298 | int | |
6299 | Perl_my_cxt_index(pTHX_ const char *my_cxt_key) | |
6300 | { | |
6301 | dVAR; | |
6302 | int index; | |
6303 | ||
7918f24d NC |
6304 | PERL_ARGS_ASSERT_MY_CXT_INDEX; |
6305 | ||
53d44271 JH |
6306 | for (index = 0; index < PL_my_cxt_index; index++) { |
6307 | const char *key = PL_my_cxt_keys[index]; | |
6308 | /* try direct pointer compare first - there are chances to success, | |
6309 | * and it's much faster. | |
6310 | */ | |
6311 | if ((key == my_cxt_key) || strEQ(key, my_cxt_key)) | |
6312 | return index; | |
6313 | } | |
6314 | return -1; | |
6315 | } | |
6316 | ||
6317 | void * | |
6318 | Perl_my_cxt_init(pTHX_ const char *my_cxt_key, size_t size) | |
6319 | { | |
6320 | dVAR; | |
6321 | void *p; | |
6322 | int index; | |
6323 | ||
7918f24d NC |
6324 | PERL_ARGS_ASSERT_MY_CXT_INIT; |
6325 | ||
53d44271 JH |
6326 | index = Perl_my_cxt_index(aTHX_ my_cxt_key); |
6327 | if (index == -1) { | |
6328 | /* this module hasn't been allocated an index yet */ | |
8703a9a4 | 6329 | #if defined(USE_ITHREADS) |
53d44271 | 6330 | MUTEX_LOCK(&PL_my_ctx_mutex); |
8703a9a4 | 6331 | #endif |
53d44271 | 6332 | index = PL_my_cxt_index++; |
8703a9a4 | 6333 | #if defined(USE_ITHREADS) |
53d44271 | 6334 | MUTEX_UNLOCK(&PL_my_ctx_mutex); |
8703a9a4 | 6335 | #endif |
53d44271 JH |
6336 | } |
6337 | ||
6338 | /* make sure the array is big enough */ | |
6339 | if (PL_my_cxt_size <= index) { | |
6340 | int old_size = PL_my_cxt_size; | |
6341 | int i; | |
6342 | if (PL_my_cxt_size) { | |
6343 | while (PL_my_cxt_size <= index) | |
6344 | PL_my_cxt_size *= 2; | |
6345 | Renew(PL_my_cxt_list, PL_my_cxt_size, void *); | |
6346 | Renew(PL_my_cxt_keys, PL_my_cxt_size, const char *); | |
6347 | } | |
6348 | else { | |
6349 | PL_my_cxt_size = 16; | |
6350 | Newx(PL_my_cxt_list, PL_my_cxt_size, void *); | |
6351 | Newx(PL_my_cxt_keys, PL_my_cxt_size, const char *); | |
6352 | } | |
6353 | for (i = old_size; i < PL_my_cxt_size; i++) { | |
6354 | PL_my_cxt_keys[i] = 0; | |
6355 | PL_my_cxt_list[i] = 0; | |
6356 | } | |
6357 | } | |
6358 | PL_my_cxt_keys[index] = my_cxt_key; | |
6359 | /* newSV() allocates one more than needed */ | |
6360 | p = (void*)SvPVX(newSV(size-1)); | |
6361 | PL_my_cxt_list[index] = p; | |
6362 | Zero(p, size, char); | |
6363 | return p; | |
6364 | } | |
6365 | #endif /* #ifndef PERL_GLOBAL_STRUCT_PRIVATE */ | |
6366 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
f16dd614 | 6367 | |
a6cc4119 SP |
6368 | #ifndef HAS_STRLCAT |
6369 | Size_t | |
6370 | Perl_my_strlcat(char *dst, const char *src, Size_t size) | |
6371 | { | |
6372 | Size_t used, length, copy; | |
6373 | ||
6374 | used = strlen(dst); | |
6375 | length = strlen(src); | |
6376 | if (size > 0 && used < size - 1) { | |
6377 | copy = (length >= size - used) ? size - used - 1 : length; | |
6378 | memcpy(dst + used, src, copy); | |
6379 | dst[used + copy] = '\0'; | |
6380 | } | |
6381 | return used + length; | |
6382 | } | |
6383 | #endif | |
6384 | ||
6385 | #ifndef HAS_STRLCPY | |
6386 | Size_t | |
6387 | Perl_my_strlcpy(char *dst, const char *src, Size_t size) | |
6388 | { | |
6389 | Size_t length, copy; | |
6390 | ||
6391 | length = strlen(src); | |
6392 | if (size > 0) { | |
6393 | copy = (length >= size) ? size - 1 : length; | |
6394 | memcpy(dst, src, copy); | |
6395 | dst[copy] = '\0'; | |
6396 | } | |
6397 | return length; | |
6398 | } | |
6399 | #endif | |
6400 | ||
17dd9954 JH |
6401 | #if defined(_MSC_VER) && (_MSC_VER >= 1300) && (_MSC_VER < 1400) && (WINVER < 0x0500) |
6402 | /* VC7 or 7.1, building with pre-VC7 runtime libraries. */ | |
6403 | long _ftol( double ); /* Defined by VC6 C libs. */ | |
6404 | long _ftol2( double dblSource ) { return _ftol( dblSource ); } | |
6405 | #endif | |
6406 | ||
c51f309c NC |
6407 | void |
6408 | Perl_get_db_sub(pTHX_ SV **svp, CV *cv) | |
6409 | { | |
6410 | dVAR; | |
6411 | SV * const dbsv = GvSVn(PL_DBsub); | |
6412 | /* We do not care about using sv to call CV; | |
6413 | * it's for informational purposes only. | |
6414 | */ | |
6415 | ||
7918f24d NC |
6416 | PERL_ARGS_ASSERT_GET_DB_SUB; |
6417 | ||
c51f309c NC |
6418 | save_item(dbsv); |
6419 | if (!PERLDB_SUB_NN) { | |
6420 | GV * const gv = CvGV(cv); | |
6421 | ||
6422 | if ( svp && ((CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) | |
6423 | || strEQ(GvNAME(gv), "END") | |
6424 | || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */ | |
159b6efe NC |
6425 | !( (SvTYPE(*svp) == SVt_PVGV) |
6426 | && (GvCV((const GV *)*svp) == cv) )))) { | |
c51f309c NC |
6427 | /* Use GV from the stack as a fallback. */ |
6428 | /* GV is potentially non-unique, or contain different CV. */ | |
daba3364 | 6429 | SV * const tmp = newRV(MUTABLE_SV(cv)); |
c51f309c NC |
6430 | sv_setsv(dbsv, tmp); |
6431 | SvREFCNT_dec(tmp); | |
6432 | } | |
6433 | else { | |
6434 | gv_efullname3(dbsv, gv, NULL); | |
6435 | } | |
6436 | } | |
6437 | else { | |
6438 | const int type = SvTYPE(dbsv); | |
6439 | if (type < SVt_PVIV && type != SVt_IV) | |
6440 | sv_upgrade(dbsv, SVt_PVIV); | |
6441 | (void)SvIOK_on(dbsv); | |
6442 | SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */ | |
6443 | } | |
6444 | } | |
6445 | ||
3497a01f | 6446 | int |
08ea85eb | 6447 | Perl_my_dirfd(pTHX_ DIR * dir) { |
3497a01f SP |
6448 | |
6449 | /* Most dirfd implementations have problems when passed NULL. */ | |
6450 | if(!dir) | |
6451 | return -1; | |
6452 | #ifdef HAS_DIRFD | |
6453 | return dirfd(dir); | |
6454 | #elif defined(HAS_DIR_DD_FD) | |
6455 | return dir->dd_fd; | |
6456 | #else | |
6457 | Perl_die(aTHX_ PL_no_func, "dirfd"); | |
6458 | /* NOT REACHED */ | |
6459 | return 0; | |
6460 | #endif | |
6461 | } | |
6462 | ||
f7e71195 AB |
6463 | REGEXP * |
6464 | Perl_get_re_arg(pTHX_ SV *sv) { | |
f7e71195 AB |
6465 | |
6466 | if (sv) { | |
6467 | if (SvMAGICAL(sv)) | |
6468 | mg_get(sv); | |
df052ff8 BM |
6469 | if (SvROK(sv)) |
6470 | sv = MUTABLE_SV(SvRV(sv)); | |
6471 | if (SvTYPE(sv) == SVt_REGEXP) | |
6472 | return (REGEXP*) sv; | |
f7e71195 AB |
6473 | } |
6474 | ||
6475 | return NULL; | |
6476 | } | |
6477 | ||
ce582cee | 6478 | /* |
66610fdd RGS |
6479 | * Local variables: |
6480 | * c-indentation-style: bsd | |
6481 | * c-basic-offset: 4 | |
6482 | * indent-tabs-mode: t | |
6483 | * End: | |
6484 | * | |
37442d52 RGS |
6485 | * ex: set ts=8 sts=4 sw=4 noet: |
6486 | */ |