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