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