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