Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* util.c |
a687059c | 2 | * |
9607fc9c | 3 | * Copyright (c) 1991-1997, Larry Wall |
a687059c | 4 | * |
d48672a2 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 | 7 | * |
8d063cd8 | 8 | */ |
a0d0e21e LW |
9 | |
10 | /* | |
11 | * "Very useful, no doubt, that was to Saruman; yet it seems that he was | |
12 | * not content." --Gandalf | |
13 | */ | |
8d063cd8 | 14 | |
8d063cd8 | 15 | #include "EXTERN.h" |
8d063cd8 | 16 | #include "perl.h" |
62b28dd9 | 17 | |
6eb13c3b | 18 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
a687059c | 19 | #include <signal.h> |
62b28dd9 | 20 | #endif |
a687059c | 21 | |
36477c24 | 22 | #ifndef SIG_ERR |
23 | # define SIG_ERR ((Sighandler_t) -1) | |
24 | #endif | |
25 | ||
bd4080b3 | 26 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ |
85e6fe83 | 27 | #ifdef I_UNISTD |
8990e307 LW |
28 | # include <unistd.h> |
29 | #endif | |
30 | ||
a687059c LW |
31 | #ifdef I_VFORK |
32 | # include <vfork.h> | |
33 | #endif | |
34 | ||
94b6baf5 AD |
35 | /* Put this after #includes because fork and vfork prototypes may |
36 | conflict. | |
37 | */ | |
38 | #ifndef HAS_VFORK | |
39 | # define vfork fork | |
40 | #endif | |
41 | ||
fe14fcc3 LW |
42 | #ifdef I_FCNTL |
43 | # include <fcntl.h> | |
44 | #endif | |
45 | #ifdef I_SYS_FILE | |
46 | # include <sys/file.h> | |
47 | #endif | |
48 | ||
ff68c719 | 49 | #ifdef I_SYS_WAIT |
50 | # include <sys/wait.h> | |
51 | #endif | |
52 | ||
8d063cd8 | 53 | #define FLUSH |
8d063cd8 | 54 | |
a0d0e21e LW |
55 | #ifdef LEAKTEST |
56 | static void xstat _((void)); | |
57 | #endif | |
58 | ||
a863c7d1 MB |
59 | #ifdef USE_THREADS |
60 | static U32 threadnum = 0; | |
61 | #endif /* USE_THREADS */ | |
62 | ||
55497cff | 63 | #ifndef MYMALLOC |
de3bb511 | 64 | |
8d063cd8 LW |
65 | /* paranoid version of malloc */ |
66 | ||
a687059c LW |
67 | /* NOTE: Do not call the next three routines directly. Use the macros |
68 | * in handy.h, so that we can easily redefine everything to do tracking of | |
69 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 70 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
71 | */ |
72 | ||
bd4080b3 | 73 | Malloc_t |
f0f333f4 | 74 | safemalloc(MEM_SIZE size) |
8d063cd8 | 75 | { |
bd4080b3 | 76 | Malloc_t ptr; |
55497cff | 77 | #ifdef HAS_64K_LIMIT |
62b28dd9 | 78 | if (size > 0xffff) { |
760ac839 | 79 | PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH; |
79072805 | 80 | my_exit(1); |
62b28dd9 | 81 | } |
55497cff | 82 | #endif /* HAS_64K_LIMIT */ |
34de22dd LW |
83 | #ifdef DEBUGGING |
84 | if ((long)size < 0) | |
463ee0b2 | 85 | croak("panic: malloc"); |
34de22dd | 86 | #endif |
8d063cd8 | 87 | ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
79072805 | 88 | #if !(defined(I286) || defined(atarist)) |
760ac839 | 89 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size)); |
79072805 | 90 | #else |
760ac839 | 91 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size)); |
8d063cd8 LW |
92 | #endif |
93 | if (ptr != Nullch) | |
94 | return ptr; | |
7c0587c8 LW |
95 | else if (nomemok) |
96 | return Nullch; | |
8d063cd8 | 97 | else { |
760ac839 | 98 | PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH; |
79072805 | 99 | my_exit(1); |
8d063cd8 LW |
100 | } |
101 | /*NOTREACHED*/ | |
102 | } | |
103 | ||
104 | /* paranoid version of realloc */ | |
105 | ||
bd4080b3 | 106 | Malloc_t |
f0f333f4 | 107 | saferealloc(Malloc_t where,MEM_SIZE size) |
8d063cd8 | 108 | { |
bd4080b3 | 109 | Malloc_t ptr; |
ecfc5424 | 110 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) |
bd4080b3 | 111 | Malloc_t realloc(); |
ecfc5424 | 112 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 113 | |
55497cff | 114 | #ifdef HAS_64K_LIMIT |
5f05dabc | 115 | if (size > 0xffff) { |
116 | PerlIO_printf(PerlIO_stderr(), | |
117 | "Reallocation too large: %lx\n", size) FLUSH; | |
118 | my_exit(1); | |
119 | } | |
55497cff | 120 | #endif /* HAS_64K_LIMIT */ |
378cc40b | 121 | if (!where) |
463ee0b2 | 122 | croak("Null realloc"); |
34de22dd LW |
123 | #ifdef DEBUGGING |
124 | if ((long)size < 0) | |
463ee0b2 | 125 | croak("panic: realloc"); |
34de22dd | 126 | #endif |
8d063cd8 | 127 | ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ |
79072805 LW |
128 | |
129 | #if !(defined(I286) || defined(atarist)) | |
130 | DEBUG_m( { | |
760ac839 LW |
131 | PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++); |
132 | PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size); | |
79072805 LW |
133 | } ) |
134 | #else | |
135 | DEBUG_m( { | |
760ac839 LW |
136 | PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++); |
137 | PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size); | |
79072805 | 138 | } ) |
8d063cd8 | 139 | #endif |
79072805 | 140 | |
8d063cd8 LW |
141 | if (ptr != Nullch) |
142 | return ptr; | |
7c0587c8 LW |
143 | else if (nomemok) |
144 | return Nullch; | |
8d063cd8 | 145 | else { |
760ac839 | 146 | PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH; |
79072805 | 147 | my_exit(1); |
8d063cd8 LW |
148 | } |
149 | /*NOTREACHED*/ | |
150 | } | |
151 | ||
152 | /* safe version of free */ | |
153 | ||
54310121 | 154 | Free_t |
f0f333f4 | 155 | safefree(Malloc_t where) |
8d063cd8 | 156 | { |
79072805 | 157 | #if !(defined(I286) || defined(atarist)) |
f0f333f4 | 158 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++)); |
79072805 | 159 | #else |
f0f333f4 | 160 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++)); |
8d063cd8 | 161 | #endif |
378cc40b | 162 | if (where) { |
de3bb511 | 163 | /*SUPPRESS 701*/ |
378cc40b LW |
164 | free(where); |
165 | } | |
8d063cd8 LW |
166 | } |
167 | ||
1050c9ca | 168 | /* safe version of calloc */ |
169 | ||
bd4080b3 | 170 | Malloc_t |
f0f333f4 | 171 | safecalloc(MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 172 | { |
bd4080b3 | 173 | Malloc_t ptr; |
1050c9ca | 174 | |
55497cff | 175 | #ifdef HAS_64K_LIMIT |
5f05dabc | 176 | if (size * count > 0xffff) { |
177 | PerlIO_printf(PerlIO_stderr(), | |
178 | "Allocation too large: %lx\n", size * count) FLUSH; | |
179 | my_exit(1); | |
180 | } | |
55497cff | 181 | #endif /* HAS_64K_LIMIT */ |
1050c9ca | 182 | #ifdef DEBUGGING |
183 | if ((long)size < 0 || (long)count < 0) | |
184 | croak("panic: calloc"); | |
185 | #endif | |
0b7c1c42 GS |
186 | size *= count; |
187 | ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ | |
1050c9ca | 188 | #if !(defined(I286) || defined(atarist)) |
fb73857a | 189 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size)); |
1050c9ca | 190 | #else |
fb73857a | 191 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size)); |
1050c9ca | 192 | #endif |
1050c9ca | 193 | if (ptr != Nullch) { |
194 | memset((void*)ptr, 0, size); | |
195 | return ptr; | |
196 | } | |
197 | else if (nomemok) | |
198 | return Nullch; | |
199 | else { | |
760ac839 | 200 | PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH; |
1050c9ca | 201 | my_exit(1); |
202 | } | |
203 | /*NOTREACHED*/ | |
204 | } | |
205 | ||
55497cff | 206 | #endif /* !MYMALLOC */ |
de3bb511 | 207 | |
a687059c LW |
208 | #ifdef LEAKTEST |
209 | ||
210 | #define ALIGN sizeof(long) | |
8d063cd8 | 211 | |
bd4080b3 | 212 | Malloc_t |
f0f333f4 | 213 | safexmalloc(I32 x, MEM_SIZE size) |
8d063cd8 | 214 | { |
bd4080b3 | 215 | register Malloc_t where; |
8d063cd8 | 216 | |
a687059c LW |
217 | where = safemalloc(size + ALIGN); |
218 | xcount[x]++; | |
219 | where[0] = x % 100; | |
220 | where[1] = x / 100; | |
221 | return where + ALIGN; | |
8d063cd8 | 222 | } |
8d063cd8 | 223 | |
bd4080b3 | 224 | Malloc_t |
f0f333f4 | 225 | safexrealloc(Malloc_t where, MEM_SIZE size) |
a687059c | 226 | { |
bd4080b3 | 227 | register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN); |
a0d0e21e | 228 | return new + ALIGN; |
a687059c LW |
229 | } |
230 | ||
231 | void | |
f0f333f4 | 232 | safexfree(Malloc_t where) |
a687059c | 233 | { |
79072805 | 234 | I32 x; |
a687059c LW |
235 | |
236 | if (!where) | |
237 | return; | |
238 | where -= ALIGN; | |
239 | x = where[0] + 100 * where[1]; | |
240 | xcount[x]--; | |
241 | safefree(where); | |
242 | } | |
243 | ||
bd4080b3 | 244 | Malloc_t |
f0f333f4 | 245 | safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 246 | { |
bd4080b3 | 247 | register Malloc_t where; |
1050c9ca | 248 | |
249 | where = safexmalloc(x, size * count + ALIGN); | |
250 | xcount[x]++; | |
251 | memset((void*)where + ALIGN, 0, size * count); | |
252 | where[0] = x % 100; | |
253 | where[1] = x / 100; | |
254 | return where + ALIGN; | |
255 | } | |
256 | ||
7c0587c8 | 257 | static void |
f0f333f4 | 258 | xstat(void) |
8d063cd8 | 259 | { |
79072805 | 260 | register I32 i; |
8d063cd8 | 261 | |
a687059c | 262 | for (i = 0; i < MAXXCOUNT; i++) { |
de3bb511 | 263 | if (xcount[i] > lastxcount[i]) { |
760ac839 | 264 | PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]); |
a687059c | 265 | lastxcount[i] = xcount[i]; |
8d063cd8 LW |
266 | } |
267 | } | |
8d063cd8 | 268 | } |
a687059c LW |
269 | |
270 | #endif /* LEAKTEST */ | |
8d063cd8 LW |
271 | |
272 | /* copy a string up to some (non-backslashed) delimiter, if any */ | |
273 | ||
274 | char * | |
8ac85365 | 275 | delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen) |
8d063cd8 | 276 | { |
fc36a67e | 277 | register I32 tolen; |
278 | for (tolen = 0; from < fromend; from++, tolen++) { | |
378cc40b LW |
279 | if (*from == '\\') { |
280 | if (from[1] == delim) | |
281 | from++; | |
fc36a67e | 282 | else { |
283 | if (to < toend) | |
284 | *to++ = *from; | |
285 | tolen++; | |
286 | from++; | |
287 | } | |
378cc40b | 288 | } |
bedebaa5 | 289 | else if (*from == delim) |
8d063cd8 | 290 | break; |
fc36a67e | 291 | if (to < toend) |
292 | *to++ = *from; | |
8d063cd8 | 293 | } |
bedebaa5 CS |
294 | if (to < toend) |
295 | *to = '\0'; | |
fc36a67e | 296 | *retlen = tolen; |
8d063cd8 LW |
297 | return from; |
298 | } | |
299 | ||
300 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 301 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
302 | |
303 | char * | |
8ac85365 | 304 | instr(register char *big, register char *little) |
378cc40b LW |
305 | { |
306 | register char *s, *x; | |
79072805 | 307 | register I32 first; |
378cc40b | 308 | |
a687059c LW |
309 | if (!little) |
310 | return big; | |
311 | first = *little++; | |
378cc40b LW |
312 | if (!first) |
313 | return big; | |
314 | while (*big) { | |
315 | if (*big++ != first) | |
316 | continue; | |
317 | for (x=big,s=little; *s; /**/ ) { | |
318 | if (!*x) | |
319 | return Nullch; | |
320 | if (*s++ != *x++) { | |
321 | s--; | |
322 | break; | |
323 | } | |
324 | } | |
325 | if (!*s) | |
326 | return big-1; | |
327 | } | |
328 | return Nullch; | |
329 | } | |
8d063cd8 | 330 | |
a687059c LW |
331 | /* same as instr but allow embedded nulls */ |
332 | ||
333 | char * | |
8ac85365 | 334 | ninstr(register char *big, register char *bigend, char *little, char *lend) |
8d063cd8 | 335 | { |
a687059c | 336 | register char *s, *x; |
79072805 | 337 | register I32 first = *little; |
a687059c | 338 | register char *littleend = lend; |
378cc40b | 339 | |
a0d0e21e | 340 | if (!first && little >= littleend) |
a687059c | 341 | return big; |
de3bb511 LW |
342 | if (bigend - big < littleend - little) |
343 | return Nullch; | |
a687059c LW |
344 | bigend -= littleend - little++; |
345 | while (big <= bigend) { | |
346 | if (*big++ != first) | |
347 | continue; | |
348 | for (x=big,s=little; s < littleend; /**/ ) { | |
349 | if (*s++ != *x++) { | |
350 | s--; | |
351 | break; | |
352 | } | |
353 | } | |
354 | if (s >= littleend) | |
355 | return big-1; | |
378cc40b | 356 | } |
a687059c LW |
357 | return Nullch; |
358 | } | |
359 | ||
360 | /* reverse of the above--find last substring */ | |
361 | ||
362 | char * | |
8ac85365 | 363 | rninstr(register char *big, char *bigend, char *little, char *lend) |
a687059c LW |
364 | { |
365 | register char *bigbeg; | |
366 | register char *s, *x; | |
79072805 | 367 | register I32 first = *little; |
a687059c LW |
368 | register char *littleend = lend; |
369 | ||
a0d0e21e | 370 | if (!first && little >= littleend) |
a687059c LW |
371 | return bigend; |
372 | bigbeg = big; | |
373 | big = bigend - (littleend - little++); | |
374 | while (big >= bigbeg) { | |
375 | if (*big-- != first) | |
376 | continue; | |
377 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
378 | if (*s++ != *x++) { | |
379 | s--; | |
380 | break; | |
381 | } | |
382 | } | |
383 | if (s >= littleend) | |
384 | return big+1; | |
378cc40b | 385 | } |
a687059c | 386 | return Nullch; |
378cc40b | 387 | } |
a687059c | 388 | |
bbce6d69 | 389 | /* |
390 | * Set up for a new ctype locale. | |
391 | */ | |
55497cff | 392 | void |
8ac85365 | 393 | perl_new_ctype(char *newctype) |
ef7eada9 | 394 | { |
36477c24 | 395 | #ifdef USE_LOCALE_CTYPE |
396 | ||
bbce6d69 | 397 | int i; |
ef7eada9 | 398 | |
bbce6d69 | 399 | for (i = 0; i < 256; i++) { |
400 | if (isUPPER_LC(i)) | |
401 | fold_locale[i] = toLOWER_LC(i); | |
402 | else if (isLOWER_LC(i)) | |
403 | fold_locale[i] = toUPPER_LC(i); | |
404 | else | |
405 | fold_locale[i] = i; | |
406 | } | |
bbce6d69 | 407 | |
36477c24 | 408 | #endif /* USE_LOCALE_CTYPE */ |
409 | } | |
bbce6d69 | 410 | |
411 | /* | |
412 | * Set up for a new collation locale. | |
413 | */ | |
414 | void | |
8ac85365 | 415 | perl_new_collate(char *newcoll) |
bbce6d69 | 416 | { |
36477c24 | 417 | #ifdef USE_LOCALE_COLLATE |
418 | ||
bbce6d69 | 419 | if (! newcoll) { |
420 | if (collation_name) { | |
421 | ++collation_ix; | |
422 | Safefree(collation_name); | |
423 | collation_name = NULL; | |
424 | collation_standard = TRUE; | |
bbce6d69 | 425 | collxfrm_base = 0; |
426 | collxfrm_mult = 2; | |
bbce6d69 | 427 | } |
428 | return; | |
429 | } | |
430 | ||
431 | if (! collation_name || strNE(collation_name, newcoll)) { | |
432 | ++collation_ix; | |
433 | Safefree(collation_name); | |
434 | collation_name = savepv(newcoll); | |
ff68c719 | 435 | collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX")); |
bbce6d69 | 436 | |
bbce6d69 | 437 | { |
438 | /* 2: at most so many chars ('a', 'b'). */ | |
439 | /* 50: surely no system expands a char more. */ | |
440 | #define XFRMBUFSIZE (2 * 50) | |
441 | char xbuf[XFRMBUFSIZE]; | |
442 | Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); | |
443 | Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
444 | SSize_t mult = fb - fa; | |
445 | if (mult < 1) | |
446 | croak("strxfrm() gets absurd"); | |
447 | collxfrm_base = (fa > mult) ? (fa - mult) : 0; | |
448 | collxfrm_mult = mult; | |
449 | } | |
bbce6d69 | 450 | } |
bbce6d69 | 451 | |
36477c24 | 452 | #endif /* USE_LOCALE_COLLATE */ |
453 | } | |
bbce6d69 | 454 | |
455 | /* | |
456 | * Set up for a new numeric locale. | |
457 | */ | |
458 | void | |
8ac85365 | 459 | perl_new_numeric(char *newnum) |
bbce6d69 | 460 | { |
36477c24 | 461 | #ifdef USE_LOCALE_NUMERIC |
462 | ||
bbce6d69 | 463 | if (! newnum) { |
464 | if (numeric_name) { | |
465 | Safefree(numeric_name); | |
466 | numeric_name = NULL; | |
467 | numeric_standard = TRUE; | |
468 | numeric_local = TRUE; | |
469 | } | |
470 | return; | |
471 | } | |
472 | ||
473 | if (! numeric_name || strNE(numeric_name, newnum)) { | |
474 | Safefree(numeric_name); | |
475 | numeric_name = savepv(newnum); | |
ff68c719 | 476 | numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX")); |
bbce6d69 | 477 | numeric_local = TRUE; |
478 | } | |
36477c24 | 479 | |
480 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 481 | } |
482 | ||
483 | void | |
8ac85365 | 484 | perl_set_numeric_standard(void) |
bbce6d69 | 485 | { |
5f05dabc | 486 | #ifdef USE_LOCALE_NUMERIC |
487 | ||
bbce6d69 | 488 | if (! numeric_standard) { |
489 | setlocale(LC_NUMERIC, "C"); | |
490 | numeric_standard = TRUE; | |
491 | numeric_local = FALSE; | |
492 | } | |
5f05dabc | 493 | |
494 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 495 | } |
496 | ||
497 | void | |
8ac85365 | 498 | perl_set_numeric_local(void) |
bbce6d69 | 499 | { |
5f05dabc | 500 | #ifdef USE_LOCALE_NUMERIC |
501 | ||
bbce6d69 | 502 | if (! numeric_local) { |
503 | setlocale(LC_NUMERIC, numeric_name); | |
504 | numeric_standard = FALSE; | |
505 | numeric_local = TRUE; | |
506 | } | |
bbce6d69 | 507 | |
36477c24 | 508 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 509 | } |
36477c24 | 510 | |
bbce6d69 | 511 | |
36477c24 | 512 | /* |
513 | * Initialize locale awareness. | |
514 | */ | |
f0c5b223 | 515 | int |
8ac85365 | 516 | perl_init_i18nl10n(int printwarn) |
f0c5b223 TB |
517 | { |
518 | int ok = 1; | |
519 | /* returns | |
520 | * 1 = set ok or not applicable, | |
521 | * 0 = fallback to C locale, | |
522 | * -1 = fallback to C locale failed | |
523 | */ | |
bbce6d69 | 524 | |
36477c24 | 525 | #ifdef USE_LOCALE |
bbce6d69 | 526 | |
36477c24 | 527 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 528 | char *curctype = NULL; |
36477c24 | 529 | #endif /* USE_LOCALE_CTYPE */ |
530 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 531 | char *curcoll = NULL; |
36477c24 | 532 | #endif /* USE_LOCALE_COLLATE */ |
533 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 534 | char *curnum = NULL; |
36477c24 | 535 | #endif /* USE_LOCALE_NUMERIC */ |
02b32252 | 536 | char *lc_all = getenv("LC_ALL"); |
5f05dabc | 537 | char *lang = getenv("LANG"); |
bbce6d69 | 538 | bool setlocale_failure = FALSE; |
f0c5b223 | 539 | |
02b32252 CS |
540 | #ifdef LOCALE_ENVIRON_REQUIRED |
541 | ||
542 | /* | |
543 | * Ultrix setlocale(..., "") fails if there are no environment | |
544 | * variables from which to get a locale name. | |
545 | */ | |
546 | ||
547 | bool done = FALSE; | |
548 | ||
549 | #ifdef LC_ALL | |
550 | if (lang) { | |
551 | if (setlocale(LC_ALL, "")) | |
552 | done = TRUE; | |
553 | else | |
554 | setlocale_failure = TRUE; | |
555 | } | |
556 | if (!setlocale_failure) | |
557 | #endif /* LC_ALL */ | |
558 | { | |
559 | #ifdef USE_LOCALE_CTYPE | |
560 | if (! (curctype = setlocale(LC_CTYPE, | |
561 | (!done && (lang || getenv("LC_CTYPE"))) | |
562 | ? "" : Nullch))) | |
563 | setlocale_failure = TRUE; | |
564 | #endif /* USE_LOCALE_CTYPE */ | |
565 | #ifdef USE_LOCALE_COLLATE | |
566 | if (! (curcoll = setlocale(LC_COLLATE, | |
567 | (!done && (lang || getenv("LC_COLLATE"))) | |
568 | ? "" : Nullch))) | |
569 | setlocale_failure = TRUE; | |
570 | #endif /* USE_LOCALE_COLLATE */ | |
571 | #ifdef USE_LOCALE_NUMERIC | |
572 | if (! (curnum = setlocale(LC_NUMERIC, | |
573 | (!done && (lang || getenv("LC_NUMERIC"))) | |
574 | ? "" : Nullch))) | |
575 | setlocale_failure = TRUE; | |
576 | #endif /* USE_LOCALE_NUMERIC */ | |
577 | } | |
578 | ||
579 | #else /* !LOCALE_ENVIRON_REQUIRED */ | |
580 | ||
bbce6d69 | 581 | #ifdef LC_ALL |
5f05dabc | 582 | |
bbce6d69 | 583 | if (! setlocale(LC_ALL, "")) |
584 | setlocale_failure = TRUE; | |
5f05dabc | 585 | else { |
586 | #ifdef USE_LOCALE_CTYPE | |
587 | curctype = setlocale(LC_CTYPE, Nullch); | |
588 | #endif /* USE_LOCALE_CTYPE */ | |
589 | #ifdef USE_LOCALE_COLLATE | |
590 | curcoll = setlocale(LC_COLLATE, Nullch); | |
591 | #endif /* USE_LOCALE_COLLATE */ | |
592 | #ifdef USE_LOCALE_NUMERIC | |
593 | curnum = setlocale(LC_NUMERIC, Nullch); | |
594 | #endif /* USE_LOCALE_NUMERIC */ | |
595 | } | |
596 | ||
597 | #else /* !LC_ALL */ | |
bbce6d69 | 598 | |
36477c24 | 599 | #ifdef USE_LOCALE_CTYPE |
5f05dabc | 600 | if (! (curctype = setlocale(LC_CTYPE, ""))) |
bbce6d69 | 601 | setlocale_failure = TRUE; |
36477c24 | 602 | #endif /* USE_LOCALE_CTYPE */ |
603 | #ifdef USE_LOCALE_COLLATE | |
5f05dabc | 604 | if (! (curcoll = setlocale(LC_COLLATE, ""))) |
bbce6d69 | 605 | setlocale_failure = TRUE; |
36477c24 | 606 | #endif /* USE_LOCALE_COLLATE */ |
607 | #ifdef USE_LOCALE_NUMERIC | |
5f05dabc | 608 | if (! (curnum = setlocale(LC_NUMERIC, ""))) |
bbce6d69 | 609 | setlocale_failure = TRUE; |
36477c24 | 610 | #endif /* USE_LOCALE_NUMERIC */ |
bbce6d69 | 611 | |
5f05dabc | 612 | #endif /* LC_ALL */ |
613 | ||
02b32252 CS |
614 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
615 | ||
5f05dabc | 616 | if (setlocale_failure) { |
617 | char *p; | |
618 | bool locwarn = (printwarn > 1 || | |
619 | printwarn && | |
620 | (!(p = getenv("PERL_BADLANG")) || atoi(p))); | |
20cec16a | 621 | |
5f05dabc | 622 | if (locwarn) { |
623 | #ifdef LC_ALL | |
624 | ||
625 | PerlIO_printf(PerlIO_stderr(), | |
626 | "perl: warning: Setting locale failed.\n"); | |
627 | ||
628 | #else /* !LC_ALL */ | |
629 | ||
ef7eada9 | 630 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 631 | "perl: warning: Setting locale failed for the categories:\n\t"); |
36477c24 | 632 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 633 | if (! curctype) |
5f05dabc | 634 | PerlIO_printf(PerlIO_stderr(), "LC_CTYPE "); |
36477c24 | 635 | #endif /* USE_LOCALE_CTYPE */ |
636 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 637 | if (! curcoll) |
5f05dabc | 638 | PerlIO_printf(PerlIO_stderr(), "LC_COLLATE "); |
36477c24 | 639 | #endif /* USE_LOCALE_COLLATE */ |
640 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 641 | if (! curnum) |
5f05dabc | 642 | PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC "); |
36477c24 | 643 | #endif /* USE_LOCALE_NUMERIC */ |
bbce6d69 | 644 | PerlIO_printf(PerlIO_stderr(), "\n"); |
645 | ||
5f05dabc | 646 | #endif /* LC_ALL */ |
647 | ||
760ac839 | 648 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 649 | "perl: warning: Please check that your locale settings:\n"); |
ef7eada9 JH |
650 | |
651 | PerlIO_printf(PerlIO_stderr(), | |
bbce6d69 | 652 | "\tLC_ALL = %c%s%c,\n", |
653 | lc_all ? '"' : '(', | |
654 | lc_all ? lc_all : "unset", | |
655 | lc_all ? '"' : ')'); | |
5f05dabc | 656 | |
657 | { | |
658 | char **e; | |
659 | for (e = environ; *e; e++) { | |
660 | if (strnEQ(*e, "LC_", 3) | |
661 | && strnNE(*e, "LC_ALL=", 7) | |
662 | && (p = strchr(*e, '='))) | |
663 | PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n", | |
fb73857a | 664 | (int)(p - *e), *e, p + 1); |
5f05dabc | 665 | } |
666 | } | |
667 | ||
ef7eada9 | 668 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 669 | "\tLANG = %c%s%c\n", |
5f05dabc | 670 | lang ? '"' : '(', |
bbce6d69 | 671 | lang ? lang : "unset", |
672 | lang ? '"' : ')'); | |
ef7eada9 | 673 | |
bbce6d69 | 674 | PerlIO_printf(PerlIO_stderr(), |
675 | " are supported and installed on your system.\n"); | |
5f05dabc | 676 | } |
ef7eada9 | 677 | |
5f05dabc | 678 | #ifdef LC_ALL |
679 | ||
680 | if (setlocale(LC_ALL, "C")) { | |
681 | if (locwarn) | |
682 | PerlIO_printf(PerlIO_stderr(), | |
683 | "perl: warning: Falling back to the standard locale (\"C\").\n"); | |
bbce6d69 | 684 | ok = 0; |
ef7eada9 | 685 | } |
5f05dabc | 686 | else { |
687 | if (locwarn) | |
688 | PerlIO_printf(PerlIO_stderr(), | |
689 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); | |
690 | ok = -1; | |
691 | } | |
bbce6d69 | 692 | |
5f05dabc | 693 | #else /* ! LC_ALL */ |
694 | ||
695 | if (0 | |
36477c24 | 696 | #ifdef USE_LOCALE_CTYPE |
5f05dabc | 697 | || !(curctype || setlocale(LC_CTYPE, "C")) |
36477c24 | 698 | #endif /* USE_LOCALE_CTYPE */ |
699 | #ifdef USE_LOCALE_COLLATE | |
5f05dabc | 700 | || !(curcoll || setlocale(LC_COLLATE, "C")) |
36477c24 | 701 | #endif /* USE_LOCALE_COLLATE */ |
702 | #ifdef USE_LOCALE_NUMERIC | |
5f05dabc | 703 | || !(curnum || setlocale(LC_NUMERIC, "C")) |
36477c24 | 704 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 705 | ) |
706 | { | |
707 | if (locwarn) | |
bbce6d69 | 708 | PerlIO_printf(PerlIO_stderr(), |
5f05dabc | 709 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); |
710 | ok = -1; | |
bbce6d69 | 711 | } |
5f05dabc | 712 | |
bbce6d69 | 713 | #endif /* ! LC_ALL */ |
5f05dabc | 714 | |
715 | #ifdef USE_LOCALE_CTYPE | |
716 | curctype = setlocale(LC_CTYPE, Nullch); | |
717 | #endif /* USE_LOCALE_CTYPE */ | |
718 | #ifdef USE_LOCALE_COLLATE | |
719 | curcoll = setlocale(LC_COLLATE, Nullch); | |
720 | #endif /* USE_LOCALE_COLLATE */ | |
721 | #ifdef USE_LOCALE_NUMERIC | |
722 | curnum = setlocale(LC_NUMERIC, Nullch); | |
723 | #endif /* USE_LOCALE_NUMERIC */ | |
ef7eada9 JH |
724 | } |
725 | ||
36477c24 | 726 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 727 | perl_new_ctype(curctype); |
36477c24 | 728 | #endif /* USE_LOCALE_CTYPE */ |
bbce6d69 | 729 | |
36477c24 | 730 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 731 | perl_new_collate(curcoll); |
36477c24 | 732 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 733 | |
36477c24 | 734 | #ifdef USE_LOCALE_NUMERIC |
bbce6d69 | 735 | perl_new_numeric(curnum); |
36477c24 | 736 | #endif /* USE_LOCALE_NUMERIC */ |
ef7eada9 | 737 | |
36477c24 | 738 | #endif /* USE_LOCALE */ |
ef7eada9 | 739 | |
f0c5b223 TB |
740 | return ok; |
741 | } | |
742 | ||
bbce6d69 | 743 | /* Backwards compatibility. */ |
744 | int | |
8ac85365 | 745 | perl_init_i18nl14n(int printwarn) |
bbce6d69 | 746 | { |
5f05dabc | 747 | return perl_init_i18nl10n(printwarn); |
bbce6d69 | 748 | } |
ef7eada9 | 749 | |
36477c24 | 750 | #ifdef USE_LOCALE_COLLATE |
ef7eada9 | 751 | |
bbce6d69 | 752 | /* |
753 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
754 | * differences. First, it handles embedded NULs. Second, it allocates | |
755 | * a bit more memory than needed for the transformed data itself. | |
756 | * The real transformed data begins at offset sizeof(collationix). | |
757 | * Please see sv_collxfrm() to see how this is used. | |
758 | */ | |
759 | char * | |
8ac85365 | 760 | mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen) |
bbce6d69 | 761 | { |
762 | char *xbuf; | |
763 | STRLEN xalloc, xin, xout; | |
764 | ||
765 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ | |
766 | /* the +1 is for the terminating NUL. */ | |
767 | ||
768 | xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1; | |
769 | New(171, xbuf, xalloc, char); | |
770 | if (! xbuf) | |
771 | goto bad; | |
772 | ||
773 | *(U32*)xbuf = collation_ix; | |
774 | xout = sizeof(collation_ix); | |
775 | for (xin = 0; xin < len; ) { | |
776 | SSize_t xused; | |
777 | ||
778 | for (;;) { | |
779 | xused = strxfrm(xbuf + xout, s + xin, xalloc - xout); | |
780 | if (xused == -1) | |
781 | goto bad; | |
782 | if (xused < xalloc - xout) | |
783 | break; | |
784 | xalloc = (2 * xalloc) + 1; | |
785 | Renew(xbuf, xalloc, char); | |
786 | if (! xbuf) | |
787 | goto bad; | |
788 | } | |
ef7eada9 | 789 | |
bbce6d69 | 790 | xin += strlen(s + xin) + 1; |
791 | xout += xused; | |
792 | ||
793 | /* Embedded NULs are understood but silently skipped | |
794 | * because they make no sense in locale collation. */ | |
795 | } | |
ef7eada9 | 796 | |
bbce6d69 | 797 | xbuf[xout] = '\0'; |
798 | *xlen = xout - sizeof(collation_ix); | |
799 | return xbuf; | |
800 | ||
801 | bad: | |
802 | Safefree(xbuf); | |
803 | *xlen = 0; | |
804 | return NULL; | |
ef7eada9 JH |
805 | } |
806 | ||
36477c24 | 807 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 808 | |
378cc40b | 809 | void |
8ac85365 | 810 | fbm_compile(SV *sv) |
378cc40b | 811 | { |
a687059c LW |
812 | register unsigned char *s; |
813 | register unsigned char *table; | |
79072805 LW |
814 | register U32 i; |
815 | register U32 len = SvCUR(sv); | |
816 | I32 rarest = 0; | |
817 | U32 frequency = 256; | |
818 | ||
748a9306 LW |
819 | if (len > 255) |
820 | return; /* can't have offsets that big */ | |
79072805 | 821 | Sv_Grow(sv,len+258); |
463ee0b2 | 822 | table = (unsigned char*)(SvPVX(sv) + len + 1); |
a687059c LW |
823 | s = table - 2; |
824 | for (i = 0; i < 256; i++) { | |
378cc40b LW |
825 | table[i] = len; |
826 | } | |
827 | i = 0; | |
463ee0b2 | 828 | while (s >= (unsigned char*)(SvPVX(sv))) |
a687059c | 829 | { |
bbce6d69 | 830 | if (table[*s] == len) |
831 | table[*s] = i; | |
378cc40b LW |
832 | s--,i++; |
833 | } | |
79072805 | 834 | sv_upgrade(sv, SVt_PVBM); |
bbce6d69 | 835 | sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */ |
79072805 | 836 | SvVALID_on(sv); |
378cc40b | 837 | |
463ee0b2 | 838 | s = (unsigned char*)(SvPVX(sv)); /* deeper magic */ |
bbce6d69 | 839 | for (i = 0; i < len; i++) { |
840 | if (freq[s[i]] < frequency) { | |
841 | rarest = i; | |
842 | frequency = freq[s[i]]; | |
378cc40b LW |
843 | } |
844 | } | |
79072805 LW |
845 | BmRARE(sv) = s[rarest]; |
846 | BmPREVIOUS(sv) = rarest; | |
760ac839 | 847 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv))); |
378cc40b LW |
848 | } |
849 | ||
378cc40b | 850 | char * |
8ac85365 | 851 | fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr) |
378cc40b | 852 | { |
a687059c | 853 | register unsigned char *s; |
79072805 LW |
854 | register I32 tmp; |
855 | register I32 littlelen; | |
a687059c LW |
856 | register unsigned char *little; |
857 | register unsigned char *table; | |
858 | register unsigned char *olds; | |
859 | register unsigned char *oldlittle; | |
378cc40b | 860 | |
79072805 | 861 | if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) { |
a0d0e21e LW |
862 | STRLEN len; |
863 | char *l = SvPV(littlestr,len); | |
864 | if (!len) | |
d48672a2 | 865 | return (char*)big; |
a0d0e21e | 866 | return ninstr((char*)big,(char*)bigend, l, l + len); |
d48672a2 | 867 | } |
378cc40b | 868 | |
79072805 LW |
869 | littlelen = SvCUR(littlestr); |
870 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ | |
0f85fab0 LW |
871 | if (littlelen > bigend - big) |
872 | return Nullch; | |
463ee0b2 | 873 | little = (unsigned char*)SvPVX(littlestr); |
bbce6d69 | 874 | s = bigend - littlelen; |
36477c24 | 875 | if (*s == *little && memEQ((char*)s,(char*)little,littlelen)) |
bbce6d69 | 876 | return (char*)s; /* how sweet it is */ |
877 | else if (bigend[-1] == '\n' && little[littlelen-1] != '\n' | |
878 | && s > big) { | |
879 | s--; | |
36477c24 | 880 | if (*s == *little && memEQ((char*)s,(char*)little,littlelen)) |
bbce6d69 | 881 | return (char*)s; |
a687059c | 882 | } |
bbce6d69 | 883 | return Nullch; |
a687059c | 884 | } |
463ee0b2 | 885 | table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1); |
62b28dd9 LW |
886 | if (--littlelen >= bigend - big) |
887 | return Nullch; | |
888 | s = big + littlelen; | |
a687059c | 889 | oldlittle = little = table - 2; |
bbce6d69 | 890 | if (s < bigend) { |
891 | top2: | |
892 | /*SUPPRESS 560*/ | |
893 | if (tmp = table[*s]) { | |
62b28dd9 | 894 | #ifdef POINTERRIGOR |
bbce6d69 | 895 | if (bigend - s > tmp) { |
896 | s += tmp; | |
897 | goto top2; | |
898 | } | |
62b28dd9 | 899 | #else |
bbce6d69 | 900 | if ((s += tmp) < bigend) |
901 | goto top2; | |
62b28dd9 | 902 | #endif |
bbce6d69 | 903 | return Nullch; |
a687059c | 904 | } |
bbce6d69 | 905 | else { |
906 | tmp = littlelen; /* less expensive than calling strncmp() */ | |
907 | olds = s; | |
908 | while (tmp--) { | |
909 | if (*--s == *--little) | |
910 | continue; | |
911 | s = olds + 1; /* here we pay the price for failure */ | |
912 | little = oldlittle; | |
913 | if (s < bigend) /* fake up continue to outer loop */ | |
62b28dd9 | 914 | goto top2; |
62b28dd9 | 915 | return Nullch; |
a687059c | 916 | } |
bbce6d69 | 917 | return (char *)s; |
378cc40b LW |
918 | } |
919 | } | |
920 | return Nullch; | |
921 | } | |
922 | ||
923 | char * | |
8ac85365 | 924 | screaminstr(SV *bigstr, SV *littlestr) |
378cc40b | 925 | { |
a687059c LW |
926 | register unsigned char *s, *x; |
927 | register unsigned char *big; | |
79072805 LW |
928 | register I32 pos; |
929 | register I32 previous; | |
930 | register I32 first; | |
a687059c LW |
931 | register unsigned char *little; |
932 | register unsigned char *bigend; | |
933 | register unsigned char *littleend; | |
378cc40b | 934 | |
79072805 | 935 | if ((pos = screamfirst[BmRARE(littlestr)]) < 0) |
378cc40b | 936 | return Nullch; |
463ee0b2 | 937 | little = (unsigned char *)(SvPVX(littlestr)); |
79072805 | 938 | littleend = little + SvCUR(littlestr); |
378cc40b | 939 | first = *little++; |
79072805 | 940 | previous = BmPREVIOUS(littlestr); |
463ee0b2 | 941 | big = (unsigned char *)(SvPVX(bigstr)); |
79072805 | 942 | bigend = big + SvCUR(bigstr); |
378cc40b LW |
943 | while (pos < previous) { |
944 | if (!(pos += screamnext[pos])) | |
945 | return Nullch; | |
946 | } | |
de3bb511 | 947 | #ifdef POINTERRIGOR |
bbce6d69 | 948 | do { |
949 | if (big[pos-previous] != first) | |
950 | continue; | |
951 | for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) { | |
952 | if (x >= bigend) | |
953 | return Nullch; | |
954 | if (*s++ != *x++) { | |
955 | s--; | |
956 | break; | |
de3bb511 | 957 | } |
bbce6d69 | 958 | } |
959 | if (s == littleend) | |
960 | return (char *)(big+pos-previous); | |
961 | } while ( pos += screamnext[pos] ); | |
de3bb511 LW |
962 | #else /* !POINTERRIGOR */ |
963 | big -= previous; | |
bbce6d69 | 964 | do { |
965 | if (big[pos] != first) | |
966 | continue; | |
967 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { | |
968 | if (x >= bigend) | |
969 | return Nullch; | |
970 | if (*s++ != *x++) { | |
971 | s--; | |
972 | break; | |
378cc40b | 973 | } |
bbce6d69 | 974 | } |
975 | if (s == littleend) | |
976 | return (char *)(big+pos); | |
977 | } while ( pos += screamnext[pos] ); | |
de3bb511 | 978 | #endif /* POINTERRIGOR */ |
8d063cd8 LW |
979 | return Nullch; |
980 | } | |
981 | ||
79072805 | 982 | I32 |
8ac85365 | 983 | ibcmp(char *s1, char *s2, register I32 len) |
79072805 | 984 | { |
bbce6d69 | 985 | register U8 *a = (U8 *)s1; |
986 | register U8 *b = (U8 *)s2; | |
79072805 | 987 | while (len--) { |
bbce6d69 | 988 | if (*a != *b && *a != fold[*b]) |
989 | return 1; | |
990 | a++,b++; | |
991 | } | |
992 | return 0; | |
993 | } | |
994 | ||
995 | I32 | |
8ac85365 | 996 | ibcmp_locale(char *s1, char *s2, register I32 len) |
bbce6d69 | 997 | { |
998 | register U8 *a = (U8 *)s1; | |
999 | register U8 *b = (U8 *)s2; | |
1000 | while (len--) { | |
1001 | if (*a != *b && *a != fold_locale[*b]) | |
1002 | return 1; | |
1003 | a++,b++; | |
79072805 LW |
1004 | } |
1005 | return 0; | |
1006 | } | |
1007 | ||
8d063cd8 LW |
1008 | /* copy a string to a safe spot */ |
1009 | ||
1010 | char * | |
8ac85365 | 1011 | savepv(char *sv) |
8d063cd8 | 1012 | { |
a687059c | 1013 | register char *newaddr; |
8d063cd8 | 1014 | |
79072805 LW |
1015 | New(902,newaddr,strlen(sv)+1,char); |
1016 | (void)strcpy(newaddr,sv); | |
8d063cd8 LW |
1017 | return newaddr; |
1018 | } | |
1019 | ||
a687059c LW |
1020 | /* same thing but with a known length */ |
1021 | ||
1022 | char * | |
8ac85365 | 1023 | savepvn(char *sv, register I32 len) |
a687059c LW |
1024 | { |
1025 | register char *newaddr; | |
1026 | ||
1027 | New(903,newaddr,len+1,char); | |
79072805 | 1028 | Copy(sv,newaddr,len,char); /* might not be null terminated */ |
a687059c LW |
1029 | newaddr[len] = '\0'; /* is now */ |
1030 | return newaddr; | |
1031 | } | |
1032 | ||
fc36a67e | 1033 | /* the SV for form() and mess() is not kept in an arena */ |
1034 | ||
1035 | static SV * | |
8ac85365 | 1036 | mess_alloc(void) |
fc36a67e | 1037 | { |
1038 | SV *sv; | |
1039 | XPVMG *any; | |
1040 | ||
1041 | /* Create as PVMG now, to avoid any upgrading later */ | |
1042 | New(905, sv, 1, SV); | |
1043 | Newz(905, any, 1, XPVMG); | |
1044 | SvFLAGS(sv) = SVt_PVMG; | |
1045 | SvANY(sv) = (void*)any; | |
1046 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ | |
1047 | return sv; | |
1048 | } | |
1049 | ||
a0d0e21e | 1050 | #ifdef I_STDARG |
8990e307 | 1051 | char * |
46fc3d4c | 1052 | form(const char* pat, ...) |
a687059c LW |
1053 | #else |
1054 | /*VARARGS0*/ | |
de3bb511 | 1055 | char * |
46fc3d4c | 1056 | form(pat, va_alist) |
71be2cbc | 1057 | const char *pat; |
46fc3d4c | 1058 | va_dcl |
8990e307 LW |
1059 | #endif |
1060 | { | |
46fc3d4c | 1061 | va_list args; |
1062 | #ifdef I_STDARG | |
1063 | va_start(args, pat); | |
a687059c | 1064 | #else |
46fc3d4c | 1065 | va_start(args); |
d48672a2 | 1066 | #endif |
fc36a67e | 1067 | if (!mess_sv) |
1068 | mess_sv = mess_alloc(); | |
1069 | sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*)); | |
46fc3d4c | 1070 | va_end(args); |
fc36a67e | 1071 | return SvPVX(mess_sv); |
46fc3d4c | 1072 | } |
a687059c | 1073 | |
46fc3d4c | 1074 | char * |
8ac85365 | 1075 | mess(const char *pat, va_list *args) |
46fc3d4c | 1076 | { |
1077 | SV *sv; | |
1078 | static char dgd[] = " during global destruction.\n"; | |
1079 | ||
46fc3d4c | 1080 | if (!mess_sv) |
fc36a67e | 1081 | mess_sv = mess_alloc(); |
46fc3d4c | 1082 | sv = mess_sv; |
fc36a67e | 1083 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); |
46fc3d4c | 1084 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
e858de61 | 1085 | dTHR; |
2304df62 | 1086 | if (dirty) |
46fc3d4c | 1087 | sv_catpv(sv, dgd); |
2304df62 | 1088 | else { |
46fc3d4c | 1089 | if (curcop->cop_line) |
fc36a67e | 1090 | sv_catpvf(sv, " at %_ line %ld", |
46fc3d4c | 1091 | GvSV(curcop->cop_filegv), (long)curcop->cop_line); |
c07a80fd | 1092 | if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) { |
1093 | bool line_mode = (RsSIMPLE(rs) && | |
1094 | SvLEN(rs) == 1 && *SvPVX(rs) == '\n'); | |
46fc3d4c | 1095 | sv_catpvf(sv, ", <%s> %s %ld", |
1096 | last_in_gv == argvgv ? "" : GvNAME(last_in_gv), | |
1097 | line_mode ? "line" : "chunk", | |
1098 | (long)IoLINES(GvIOp(last_in_gv))); | |
2304df62 | 1099 | } |
46fc3d4c | 1100 | sv_catpv(sv, ".\n"); |
a687059c | 1101 | } |
a687059c | 1102 | } |
46fc3d4c | 1103 | return SvPVX(sv); |
a687059c LW |
1104 | } |
1105 | ||
ecfc5424 | 1106 | #ifdef I_STDARG |
36477c24 | 1107 | OP * |
71be2cbc | 1108 | die(const char* pat, ...) |
36477c24 | 1109 | #else |
1110 | /*VARARGS0*/ | |
1111 | OP * | |
1112 | die(pat, va_alist) | |
71be2cbc | 1113 | const char *pat; |
36477c24 | 1114 | va_dcl |
1115 | #endif | |
1116 | { | |
5dc0d613 | 1117 | dTHR; |
36477c24 | 1118 | va_list args; |
1119 | char *message; | |
035902c7 | 1120 | I32 oldrunlevel = runlevel; |
36477c24 | 1121 | int was_in_eval = in_eval; |
1122 | HV *stash; | |
1123 | GV *gv; | |
1124 | CV *cv; | |
1125 | ||
d9f997d7 | 1126 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: curstack = %p, mainstack= %p\n", |
0f15f207 | 1127 | curstack, mainstack));/*debug*/ |
36477c24 | 1128 | /* We have to switch back to mainstack or die_where may try to pop |
1129 | * the eval block from the wrong stack if die is being called from a | |
1130 | * signal handler. - dkindred@cs.cmu.edu */ | |
1131 | if (curstack != mainstack) { | |
1132 | dSP; | |
1133 | SWITCHSTACK(curstack, mainstack); | |
1134 | } | |
1135 | ||
1136 | #ifdef I_STDARG | |
1137 | va_start(args, pat); | |
1138 | #else | |
1139 | va_start(args); | |
1140 | #endif | |
1141 | message = mess(pat, &args); | |
1142 | va_end(args); | |
1143 | ||
d9f997d7 | 1144 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: message = %s\ndiehook = %p\n", |
0f15f207 | 1145 | message, diehook));/*debug*/ |
1738f5c4 CS |
1146 | if (diehook) { |
1147 | /* sv_2cv might call croak() */ | |
1148 | SV *olddiehook = diehook; | |
1149 | ENTER; | |
1150 | SAVESPTR(diehook); | |
1151 | diehook = Nullsv; | |
1152 | cv = sv_2cv(olddiehook, &stash, &gv, 0); | |
1153 | LEAVE; | |
1154 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1155 | dSP; | |
774d564b | 1156 | SV *msg; |
1157 | ||
1158 | ENTER; | |
1159 | msg = newSVpv(message, 0); | |
1160 | SvREADONLY_on(msg); | |
1161 | SAVEFREESV(msg); | |
1738f5c4 CS |
1162 | |
1163 | PUSHMARK(sp); | |
1164 | XPUSHs(msg); | |
1165 | PUTBACK; | |
1166 | perl_call_sv((SV*)cv, G_DISCARD); | |
1167 | ||
774d564b | 1168 | LEAVE; |
1738f5c4 | 1169 | } |
36477c24 | 1170 | } |
1171 | ||
1172 | restartop = die_where(message); | |
d9f997d7 | 1173 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), |
0f15f207 MB |
1174 | "die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n", |
1175 | restartop, was_in_eval, oldrunlevel));/*debug*/ | |
36477c24 | 1176 | if ((!restartop && was_in_eval) || oldrunlevel > 1) |
54310121 | 1177 | JMPENV_JUMP(3); |
36477c24 | 1178 | return restartop; |
1179 | } | |
1180 | ||
1181 | #ifdef I_STDARG | |
79072805 | 1182 | void |
71be2cbc | 1183 | croak(const char* pat, ...) |
463ee0b2 | 1184 | #else |
8990e307 LW |
1185 | /*VARARGS0*/ |
1186 | void | |
1187 | croak(pat, va_alist) | |
1188 | char *pat; | |
1189 | va_dcl | |
463ee0b2 | 1190 | #endif |
a687059c | 1191 | { |
11343788 | 1192 | dTHR; |
a687059c | 1193 | va_list args; |
de3bb511 | 1194 | char *message; |
748a9306 LW |
1195 | HV *stash; |
1196 | GV *gv; | |
1197 | CV *cv; | |
a687059c | 1198 | |
a0d0e21e | 1199 | #ifdef I_STDARG |
8990e307 LW |
1200 | va_start(args, pat); |
1201 | #else | |
a687059c | 1202 | va_start(args); |
8990e307 | 1203 | #endif |
2304df62 | 1204 | message = mess(pat, &args); |
a687059c | 1205 | va_end(args); |
11343788 | 1206 | #ifdef USE_THREADS |
d9f997d7 | 1207 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message)); |
11343788 | 1208 | #endif /* USE_THREADS */ |
20cec16a | 1209 | if (diehook) { |
1738f5c4 | 1210 | /* sv_2cv might call croak() */ |
20cec16a | 1211 | SV *olddiehook = diehook; |
1738f5c4 CS |
1212 | ENTER; |
1213 | SAVESPTR(diehook); | |
1214 | diehook = Nullsv; | |
20cec16a | 1215 | cv = sv_2cv(olddiehook, &stash, &gv, 0); |
1738f5c4 CS |
1216 | LEAVE; |
1217 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
20cec16a | 1218 | dSP; |
774d564b | 1219 | SV *msg; |
1220 | ||
1221 | ENTER; | |
1222 | msg = newSVpv(message, 0); | |
1223 | SvREADONLY_on(msg); | |
1224 | SAVEFREESV(msg); | |
20cec16a | 1225 | |
1226 | PUSHMARK(sp); | |
1738f5c4 | 1227 | XPUSHs(msg); |
20cec16a | 1228 | PUTBACK; |
1229 | perl_call_sv((SV*)cv, G_DISCARD); | |
36477c24 | 1230 | |
774d564b | 1231 | LEAVE; |
20cec16a | 1232 | } |
748a9306 | 1233 | } |
a0d0e21e LW |
1234 | if (in_eval) { |
1235 | restartop = die_where(message); | |
54310121 | 1236 | JMPENV_JUMP(3); |
a0d0e21e | 1237 | } |
760ac839 LW |
1238 | PerlIO_puts(PerlIO_stderr(),message); |
1239 | (void)PerlIO_flush(PerlIO_stderr()); | |
f86702cc | 1240 | my_failure_exit(); |
a687059c LW |
1241 | } |
1242 | ||
8990e307 | 1243 | void |
ecfc5424 | 1244 | #ifdef I_STDARG |
71be2cbc | 1245 | warn(const char* pat,...) |
463ee0b2 | 1246 | #else |
8990e307 LW |
1247 | /*VARARGS0*/ |
1248 | warn(pat,va_alist) | |
71be2cbc | 1249 | const char *pat; |
8990e307 | 1250 | va_dcl |
463ee0b2 | 1251 | #endif |
a687059c LW |
1252 | { |
1253 | va_list args; | |
de3bb511 | 1254 | char *message; |
748a9306 LW |
1255 | HV *stash; |
1256 | GV *gv; | |
1257 | CV *cv; | |
a687059c | 1258 | |
a0d0e21e | 1259 | #ifdef I_STDARG |
8990e307 LW |
1260 | va_start(args, pat); |
1261 | #else | |
a687059c | 1262 | va_start(args); |
8990e307 | 1263 | #endif |
2304df62 | 1264 | message = mess(pat, &args); |
a687059c LW |
1265 | va_end(args); |
1266 | ||
20cec16a | 1267 | if (warnhook) { |
1738f5c4 | 1268 | /* sv_2cv might call warn() */ |
11343788 | 1269 | dTHR; |
20cec16a | 1270 | SV *oldwarnhook = warnhook; |
1738f5c4 CS |
1271 | ENTER; |
1272 | SAVESPTR(warnhook); | |
1273 | warnhook = Nullsv; | |
20cec16a | 1274 | cv = sv_2cv(oldwarnhook, &stash, &gv, 0); |
1738f5c4 CS |
1275 | LEAVE; |
1276 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
20cec16a | 1277 | dSP; |
774d564b | 1278 | SV *msg; |
1279 | ||
1280 | ENTER; | |
1281 | msg = newSVpv(message, 0); | |
1282 | SvREADONLY_on(msg); | |
1283 | SAVEFREESV(msg); | |
1284 | ||
20cec16a | 1285 | PUSHMARK(sp); |
774d564b | 1286 | XPUSHs(msg); |
20cec16a | 1287 | PUTBACK; |
1288 | perl_call_sv((SV*)cv, G_DISCARD); | |
774d564b | 1289 | |
1290 | LEAVE; | |
20cec16a | 1291 | return; |
1292 | } | |
748a9306 | 1293 | } |
20cec16a | 1294 | PerlIO_puts(PerlIO_stderr(),message); |
a687059c | 1295 | #ifdef LEAKTEST |
20cec16a | 1296 | DEBUG_L(xstat()); |
a687059c | 1297 | #endif |
20cec16a | 1298 | (void)PerlIO_flush(PerlIO_stderr()); |
a687059c | 1299 | } |
8d063cd8 | 1300 | |
a0d0e21e | 1301 | #ifndef VMS /* VMS' my_setenv() is in VMS.c */ |
3e3baf6d | 1302 | #ifndef WIN32 |
8d063cd8 | 1303 | void |
8ac85365 | 1304 | my_setenv(char *nam, char *val) |
8d063cd8 | 1305 | { |
79072805 | 1306 | register I32 i=setenv_getix(nam); /* where does it go? */ |
8d063cd8 | 1307 | |
fe14fcc3 | 1308 | if (environ == origenviron) { /* need we copy environment? */ |
79072805 LW |
1309 | I32 j; |
1310 | I32 max; | |
fe14fcc3 LW |
1311 | char **tmpenv; |
1312 | ||
de3bb511 | 1313 | /*SUPPRESS 530*/ |
fe14fcc3 LW |
1314 | for (max = i; environ[max]; max++) ; |
1315 | New(901,tmpenv, max+2, char*); | |
1316 | for (j=0; j<max; j++) /* copy environment */ | |
a0d0e21e | 1317 | tmpenv[j] = savepv(environ[j]); |
fe14fcc3 LW |
1318 | tmpenv[max] = Nullch; |
1319 | environ = tmpenv; /* tell exec where it is now */ | |
1320 | } | |
a687059c | 1321 | if (!val) { |
e5ebf479 | 1322 | Safefree(environ[i]); |
a687059c LW |
1323 | while (environ[i]) { |
1324 | environ[i] = environ[i+1]; | |
1325 | i++; | |
1326 | } | |
1327 | return; | |
1328 | } | |
8d063cd8 | 1329 | if (!environ[i]) { /* does not exist yet */ |
fe14fcc3 | 1330 | Renew(environ, i+2, char*); /* just expand it a bit */ |
8d063cd8 LW |
1331 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
1332 | } | |
fe14fcc3 LW |
1333 | else |
1334 | Safefree(environ[i]); | |
a687059c | 1335 | New(904, environ[i], strlen(nam) + strlen(val) + 2, char); |
62b28dd9 | 1336 | #ifndef MSDOS |
a687059c | 1337 | (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
62b28dd9 LW |
1338 | #else |
1339 | /* MS-DOS requires environment variable names to be in uppercase */ | |
fe14fcc3 LW |
1340 | /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but |
1341 | * some utilities and applications may break because they only look | |
1342 | * for upper case strings. (Fixed strupr() bug here.)] | |
1343 | */ | |
1344 | strcpy(environ[i],nam); strupr(environ[i]); | |
62b28dd9 LW |
1345 | (void)sprintf(environ[i] + strlen(nam),"=%s",val); |
1346 | #endif /* MSDOS */ | |
8d063cd8 LW |
1347 | } |
1348 | ||
3e3baf6d | 1349 | #else /* if WIN32 */ |
68dc0745 | 1350 | |
1351 | void | |
1352 | my_setenv(nam,val) | |
1353 | char *nam, *val; | |
1354 | { | |
3e3baf6d TB |
1355 | |
1356 | #ifdef USE_WIN32_RTL_ENV | |
1357 | ||
68dc0745 | 1358 | register char *envstr; |
1359 | STRLEN namlen = strlen(nam); | |
3e3baf6d TB |
1360 | STRLEN vallen; |
1361 | char *oldstr = environ[setenv_getix(nam)]; | |
1362 | ||
1363 | /* putenv() has totally broken semantics in both the Borland | |
1364 | * and Microsoft CRTLs. They either store the passed pointer in | |
1365 | * the environment without making a copy, or make a copy and don't | |
1366 | * free it. And on top of that, they dont free() old entries that | |
1367 | * are being replaced/deleted. This means the caller must | |
1368 | * free any old entries somehow, or we end up with a memory | |
1369 | * leak every time my_setenv() is called. One might think | |
1370 | * one could directly manipulate environ[], like the UNIX code | |
1371 | * above, but direct changes to environ are not allowed when | |
1372 | * calling putenv(), since the RTLs maintain an internal | |
1373 | * *copy* of environ[]. Bad, bad, *bad* stink. | |
1374 | * GSAR 97-06-07 | |
1375 | */ | |
68dc0745 | 1376 | |
3e3baf6d TB |
1377 | if (!val) { |
1378 | if (!oldstr) | |
1379 | return; | |
1380 | val = ""; | |
1381 | vallen = 0; | |
1382 | } | |
1383 | else | |
1384 | vallen = strlen(val); | |
fc36a67e | 1385 | New(904, envstr, namlen + vallen + 3, char); |
68dc0745 | 1386 | (void)sprintf(envstr,"%s=%s",nam,val); |
3e3baf6d TB |
1387 | (void)putenv(envstr); |
1388 | if (oldstr) | |
1389 | Safefree(oldstr); | |
1390 | #ifdef _MSC_VER | |
1391 | Safefree(envstr); /* MSVCRT leaks without this */ | |
1392 | #endif | |
1393 | ||
1394 | #else /* !USE_WIN32_RTL_ENV */ | |
1395 | ||
1396 | /* The sane way to deal with the environment. | |
1397 | * Has these advantages over putenv() & co.: | |
1398 | * * enables us to store a truly empty value in the | |
1399 | * environment (like in UNIX). | |
1400 | * * we don't have to deal with RTL globals, bugs and leaks. | |
1401 | * * Much faster. | |
1402 | * Why you may want to enable USE_WIN32_RTL_ENV: | |
1403 | * * environ[] and RTL functions will not reflect changes, | |
1404 | * which might be an issue if extensions want to access | |
1405 | * the env. via RTL. This cuts both ways, since RTL will | |
1406 | * not see changes made by extensions that call the Win32 | |
1407 | * functions directly, either. | |
1408 | * GSAR 97-06-07 | |
1409 | */ | |
1410 | SetEnvironmentVariable(nam,val); | |
1411 | ||
1412 | #endif | |
1413 | } | |
1414 | ||
1415 | #endif /* WIN32 */ | |
1416 | ||
1417 | I32 | |
8ac85365 | 1418 | setenv_getix(char *nam) |
3e3baf6d TB |
1419 | { |
1420 | register I32 i, len = strlen(nam); | |
1421 | ||
1422 | for (i = 0; environ[i]; i++) { | |
1423 | if ( | |
1424 | #ifdef WIN32 | |
1425 | strnicmp(environ[i],nam,len) == 0 | |
1426 | #else | |
1427 | strnEQ(environ[i],nam,len) | |
1428 | #endif | |
1429 | && environ[i][len] == '=') | |
1430 | break; /* strnEQ must come first to avoid */ | |
1431 | } /* potential SEGV's */ | |
1432 | return i; | |
68dc0745 | 1433 | } |
1434 | ||
a0d0e21e | 1435 | #endif /* !VMS */ |
378cc40b | 1436 | |
16d20bd9 | 1437 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 1438 | I32 |
378cc40b LW |
1439 | unlnk(f) /* unlink all versions of a file */ |
1440 | char *f; | |
1441 | { | |
79072805 | 1442 | I32 i; |
378cc40b LW |
1443 | |
1444 | for (i = 0; unlink(f) >= 0; i++) ; | |
1445 | return i ? 0 : -1; | |
1446 | } | |
1447 | #endif | |
1448 | ||
85e6fe83 | 1449 | #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY) |
378cc40b | 1450 | char * |
7c0587c8 | 1451 | my_bcopy(from,to,len) |
378cc40b LW |
1452 | register char *from; |
1453 | register char *to; | |
79072805 | 1454 | register I32 len; |
378cc40b LW |
1455 | { |
1456 | char *retval = to; | |
1457 | ||
7c0587c8 LW |
1458 | if (from - to >= 0) { |
1459 | while (len--) | |
1460 | *to++ = *from++; | |
1461 | } | |
1462 | else { | |
1463 | to += len; | |
1464 | from += len; | |
1465 | while (len--) | |
faf8582f | 1466 | *(--to) = *(--from); |
7c0587c8 | 1467 | } |
378cc40b LW |
1468 | return retval; |
1469 | } | |
ffed7fef | 1470 | #endif |
378cc40b | 1471 | |
fc36a67e | 1472 | #ifndef HAS_MEMSET |
1473 | void * | |
1474 | my_memset(loc,ch,len) | |
1475 | register char *loc; | |
1476 | register I32 ch; | |
1477 | register I32 len; | |
1478 | { | |
1479 | char *retval = loc; | |
1480 | ||
1481 | while (len--) | |
1482 | *loc++ = ch; | |
1483 | return retval; | |
1484 | } | |
1485 | #endif | |
1486 | ||
7c0587c8 | 1487 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b | 1488 | char * |
7c0587c8 | 1489 | my_bzero(loc,len) |
378cc40b | 1490 | register char *loc; |
79072805 | 1491 | register I32 len; |
378cc40b LW |
1492 | { |
1493 | char *retval = loc; | |
1494 | ||
1495 | while (len--) | |
1496 | *loc++ = 0; | |
1497 | return retval; | |
1498 | } | |
1499 | #endif | |
7c0587c8 | 1500 | |
36477c24 | 1501 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
79072805 | 1502 | I32 |
7c0587c8 | 1503 | my_memcmp(s1,s2,len) |
36477c24 | 1504 | char *s1; |
1505 | char *s2; | |
79072805 | 1506 | register I32 len; |
7c0587c8 | 1507 | { |
36477c24 | 1508 | register U8 *a = (U8 *)s1; |
1509 | register U8 *b = (U8 *)s2; | |
79072805 | 1510 | register I32 tmp; |
7c0587c8 LW |
1511 | |
1512 | while (len--) { | |
36477c24 | 1513 | if (tmp = *a++ - *b++) |
7c0587c8 LW |
1514 | return tmp; |
1515 | } | |
1516 | return 0; | |
1517 | } | |
36477c24 | 1518 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 1519 | |
4633a7c4 | 1520 | #if defined(I_STDARG) || defined(I_VARARGS) |
fe14fcc3 | 1521 | #ifndef HAS_VPRINTF |
a687059c | 1522 | |
85e6fe83 | 1523 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1524 | char * |
1525 | #else | |
1526 | int | |
1527 | #endif | |
1528 | vsprintf(dest, pat, args) | |
71be2cbc | 1529 | char *dest; |
1530 | const char *pat; | |
1531 | char *args; | |
a687059c LW |
1532 | { |
1533 | FILE fakebuf; | |
1534 | ||
1535 | fakebuf._ptr = dest; | |
1536 | fakebuf._cnt = 32767; | |
35c8bce7 LW |
1537 | #ifndef _IOSTRG |
1538 | #define _IOSTRG 0 | |
1539 | #endif | |
a687059c LW |
1540 | fakebuf._flag = _IOWRT|_IOSTRG; |
1541 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
1542 | (void)putc('\0', &fakebuf); | |
85e6fe83 | 1543 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1544 | return(dest); |
1545 | #else | |
1546 | return 0; /* perl doesn't use return value */ | |
1547 | #endif | |
1548 | } | |
1549 | ||
fe14fcc3 | 1550 | #endif /* HAS_VPRINTF */ |
4633a7c4 | 1551 | #endif /* I_VARARGS || I_STDARGS */ |
a687059c LW |
1552 | |
1553 | #ifdef MYSWAP | |
ffed7fef | 1554 | #if BYTEORDER != 0x4321 |
a687059c | 1555 | short |
748a9306 | 1556 | #ifndef CAN_PROTOTYPE |
a687059c LW |
1557 | my_swap(s) |
1558 | short s; | |
748a9306 LW |
1559 | #else |
1560 | my_swap(short s) | |
1561 | #endif | |
a687059c LW |
1562 | { |
1563 | #if (BYTEORDER & 1) == 0 | |
1564 | short result; | |
1565 | ||
1566 | result = ((s & 255) << 8) + ((s >> 8) & 255); | |
1567 | return result; | |
1568 | #else | |
1569 | return s; | |
1570 | #endif | |
1571 | } | |
1572 | ||
1573 | long | |
748a9306 LW |
1574 | #ifndef CAN_PROTOTYPE |
1575 | my_htonl(l) | |
a687059c | 1576 | register long l; |
748a9306 LW |
1577 | #else |
1578 | my_htonl(long l) | |
1579 | #endif | |
a687059c LW |
1580 | { |
1581 | union { | |
1582 | long result; | |
ffed7fef | 1583 | char c[sizeof(long)]; |
a687059c LW |
1584 | } u; |
1585 | ||
ffed7fef | 1586 | #if BYTEORDER == 0x1234 |
a687059c LW |
1587 | u.c[0] = (l >> 24) & 255; |
1588 | u.c[1] = (l >> 16) & 255; | |
1589 | u.c[2] = (l >> 8) & 255; | |
1590 | u.c[3] = l & 255; | |
1591 | return u.result; | |
1592 | #else | |
ffed7fef | 1593 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 | 1594 | croak("Unknown BYTEORDER\n"); |
a687059c | 1595 | #else |
79072805 LW |
1596 | register I32 o; |
1597 | register I32 s; | |
a687059c | 1598 | |
ffed7fef LW |
1599 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1600 | u.c[o & 0xf] = (l >> s) & 255; | |
a687059c LW |
1601 | } |
1602 | return u.result; | |
1603 | #endif | |
1604 | #endif | |
1605 | } | |
1606 | ||
1607 | long | |
748a9306 LW |
1608 | #ifndef CAN_PROTOTYPE |
1609 | my_ntohl(l) | |
a687059c | 1610 | register long l; |
748a9306 LW |
1611 | #else |
1612 | my_ntohl(long l) | |
1613 | #endif | |
a687059c LW |
1614 | { |
1615 | union { | |
1616 | long l; | |
ffed7fef | 1617 | char c[sizeof(long)]; |
a687059c LW |
1618 | } u; |
1619 | ||
ffed7fef | 1620 | #if BYTEORDER == 0x1234 |
a687059c LW |
1621 | u.c[0] = (l >> 24) & 255; |
1622 | u.c[1] = (l >> 16) & 255; | |
1623 | u.c[2] = (l >> 8) & 255; | |
1624 | u.c[3] = l & 255; | |
1625 | return u.l; | |
1626 | #else | |
ffed7fef | 1627 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 | 1628 | croak("Unknown BYTEORDER\n"); |
a687059c | 1629 | #else |
79072805 LW |
1630 | register I32 o; |
1631 | register I32 s; | |
a687059c LW |
1632 | |
1633 | u.l = l; | |
1634 | l = 0; | |
ffed7fef LW |
1635 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1636 | l |= (u.c[o & 0xf] & 255) << s; | |
a687059c LW |
1637 | } |
1638 | return l; | |
1639 | #endif | |
1640 | #endif | |
1641 | } | |
1642 | ||
ffed7fef | 1643 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 LW |
1644 | #endif /* MYSWAP */ |
1645 | ||
1646 | /* | |
1647 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. | |
1648 | * If these functions are defined, | |
1649 | * the BYTEORDER is neither 0x1234 nor 0x4321. | |
1650 | * However, this is not assumed. | |
1651 | * -DWS | |
1652 | */ | |
1653 | ||
1654 | #define HTOV(name,type) \ | |
1655 | type \ | |
1656 | name (n) \ | |
1657 | register type n; \ | |
1658 | { \ | |
1659 | union { \ | |
1660 | type value; \ | |
1661 | char c[sizeof(type)]; \ | |
1662 | } u; \ | |
79072805 LW |
1663 | register I32 i; \ |
1664 | register I32 s; \ | |
988174c1 LW |
1665 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ |
1666 | u.c[i] = (n >> s) & 0xFF; \ | |
1667 | } \ | |
1668 | return u.value; \ | |
1669 | } | |
1670 | ||
1671 | #define VTOH(name,type) \ | |
1672 | type \ | |
1673 | name (n) \ | |
1674 | register type n; \ | |
1675 | { \ | |
1676 | union { \ | |
1677 | type value; \ | |
1678 | char c[sizeof(type)]; \ | |
1679 | } u; \ | |
79072805 LW |
1680 | register I32 i; \ |
1681 | register I32 s; \ | |
988174c1 LW |
1682 | u.value = n; \ |
1683 | n = 0; \ | |
1684 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ | |
1685 | n += (u.c[i] & 0xFF) << s; \ | |
1686 | } \ | |
1687 | return n; \ | |
1688 | } | |
1689 | ||
1690 | #if defined(HAS_HTOVS) && !defined(htovs) | |
1691 | HTOV(htovs,short) | |
1692 | #endif | |
1693 | #if defined(HAS_HTOVL) && !defined(htovl) | |
1694 | HTOV(htovl,long) | |
1695 | #endif | |
1696 | #if defined(HAS_VTOHS) && !defined(vtohs) | |
1697 | VTOH(vtohs,short) | |
1698 | #endif | |
1699 | #if defined(HAS_VTOHL) && !defined(vtohl) | |
1700 | VTOH(vtohl,long) | |
1701 | #endif | |
a687059c | 1702 | |
5f05dabc | 1703 | /* VMS' my_popen() is in VMS.c, same with OS/2. */ |
1704 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) | |
760ac839 | 1705 | PerlIO * |
8ac85365 | 1706 | my_popen(char *cmd, char *mode) |
a687059c LW |
1707 | { |
1708 | int p[2]; | |
8ac85365 | 1709 | register I32 This, that; |
79072805 LW |
1710 | register I32 pid; |
1711 | SV *sv; | |
1738f5c4 | 1712 | I32 doexec = strNE(cmd,"-"); |
a687059c | 1713 | |
ddcf38b7 IZ |
1714 | #ifdef OS2 |
1715 | if (doexec) { | |
1716 | return my_syspopen(cmd,mode); | |
1717 | } | |
1718 | #endif | |
a687059c LW |
1719 | if (pipe(p) < 0) |
1720 | return Nullfp; | |
8ac85365 NIS |
1721 | This = (*mode == 'w'); |
1722 | that = !This; | |
bbce6d69 | 1723 | if (doexec && tainting) { |
1724 | taint_env(); | |
1725 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 1726 | } |
a687059c LW |
1727 | while ((pid = (doexec?vfork():fork())) < 0) { |
1728 | if (errno != EAGAIN) { | |
8ac85365 | 1729 | close(p[This]); |
a687059c | 1730 | if (!doexec) |
463ee0b2 | 1731 | croak("Can't fork"); |
a687059c LW |
1732 | return Nullfp; |
1733 | } | |
1734 | sleep(5); | |
1735 | } | |
1736 | if (pid == 0) { | |
79072805 LW |
1737 | GV* tmpgv; |
1738 | ||
a687059c | 1739 | #define THIS that |
8ac85365 | 1740 | #define THAT This |
a687059c LW |
1741 | close(p[THAT]); |
1742 | if (p[THIS] != (*mode == 'r')) { | |
1743 | dup2(p[THIS], *mode == 'r'); | |
1744 | close(p[THIS]); | |
1745 | } | |
1746 | if (doexec) { | |
a0d0e21e | 1747 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
1748 | int fd; |
1749 | ||
1750 | #ifndef NOFILE | |
1751 | #define NOFILE 20 | |
1752 | #endif | |
d48672a2 | 1753 | for (fd = maxsysfd + 1; fd < NOFILE; fd++) |
ae986130 LW |
1754 | close(fd); |
1755 | #endif | |
a687059c LW |
1756 | do_exec(cmd); /* may or may not use the shell */ |
1757 | _exit(1); | |
1758 | } | |
de3bb511 | 1759 | /*SUPPRESS 560*/ |
85e6fe83 | 1760 | if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV)) |
1e422769 | 1761 | sv_setiv(GvSV(tmpgv), (IV)getpid()); |
9f68db38 | 1762 | forkprocess = 0; |
463ee0b2 | 1763 | hv_clear(pidstatus); /* we have no children */ |
a687059c LW |
1764 | return Nullfp; |
1765 | #undef THIS | |
1766 | #undef THAT | |
1767 | } | |
62b28dd9 | 1768 | do_execfree(); /* free any memory malloced by child on vfork */ |
a687059c | 1769 | close(p[that]); |
8ac85365 NIS |
1770 | if (p[that] < p[This]) { |
1771 | dup2(p[This], p[that]); | |
1772 | close(p[This]); | |
1773 | p[This] = p[that]; | |
62b28dd9 | 1774 | } |
8ac85365 | 1775 | sv = *av_fetch(fdpid,p[This],TRUE); |
a0d0e21e | 1776 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 1777 | SvIVX(sv) = pid; |
a687059c | 1778 | forkprocess = pid; |
8ac85365 | 1779 | return PerlIO_fdopen(p[This], mode); |
a687059c | 1780 | } |
7c0587c8 | 1781 | #else |
55497cff | 1782 | #if defined(atarist) || defined(DJGPP) |
7c0587c8 | 1783 | FILE *popen(); |
760ac839 | 1784 | PerlIO * |
79072805 | 1785 | my_popen(cmd,mode) |
7c0587c8 LW |
1786 | char *cmd; |
1787 | char *mode; | |
1788 | { | |
760ac839 | 1789 | /* Needs work for PerlIO ! */ |
55497cff | 1790 | /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */ |
1791 | return popen(PerlIO_exportFILE(cmd, 0), mode); | |
7c0587c8 LW |
1792 | } |
1793 | #endif | |
1794 | ||
1795 | #endif /* !DOSISH */ | |
a687059c | 1796 | |
748a9306 | 1797 | #ifdef DUMP_FDS |
79072805 | 1798 | dump_fds(s) |
ae986130 LW |
1799 | char *s; |
1800 | { | |
1801 | int fd; | |
1802 | struct stat tmpstatbuf; | |
1803 | ||
760ac839 | 1804 | PerlIO_printf(PerlIO_stderr(),"%s", s); |
ae986130 | 1805 | for (fd = 0; fd < 32; fd++) { |
a0d0e21e | 1806 | if (Fstat(fd,&tmpstatbuf) >= 0) |
760ac839 | 1807 | PerlIO_printf(PerlIO_stderr()," %d",fd); |
ae986130 | 1808 | } |
760ac839 | 1809 | PerlIO_printf(PerlIO_stderr(),"\n"); |
ae986130 LW |
1810 | } |
1811 | #endif | |
1812 | ||
fe14fcc3 | 1813 | #ifndef HAS_DUP2 |
fec02dd3 | 1814 | int |
a687059c LW |
1815 | dup2(oldfd,newfd) |
1816 | int oldfd; | |
1817 | int newfd; | |
1818 | { | |
a0d0e21e | 1819 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
1820 | if (oldfd == newfd) |
1821 | return oldfd; | |
62b28dd9 | 1822 | close(newfd); |
fec02dd3 | 1823 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 1824 | #else |
fc36a67e | 1825 | #define DUP2_MAX_FDS 256 |
1826 | int fdtmp[DUP2_MAX_FDS]; | |
79072805 | 1827 | I32 fdx = 0; |
ae986130 LW |
1828 | int fd; |
1829 | ||
fe14fcc3 | 1830 | if (oldfd == newfd) |
fec02dd3 | 1831 | return oldfd; |
a687059c | 1832 | close(newfd); |
fc36a67e | 1833 | /* good enough for low fd's... */ |
1834 | while ((fd = dup(oldfd)) != newfd && fd >= 0) { | |
1835 | if (fdx >= DUP2_MAX_FDS) { | |
1836 | close(fd); | |
1837 | fd = -1; | |
1838 | break; | |
1839 | } | |
ae986130 | 1840 | fdtmp[fdx++] = fd; |
fc36a67e | 1841 | } |
ae986130 LW |
1842 | while (fdx > 0) |
1843 | close(fdtmp[--fdx]); | |
fec02dd3 | 1844 | return fd; |
62b28dd9 | 1845 | #endif |
a687059c LW |
1846 | } |
1847 | #endif | |
1848 | ||
ff68c719 | 1849 | |
1850 | #ifdef HAS_SIGACTION | |
1851 | ||
1852 | Sighandler_t | |
8ac85365 | 1853 | rsignal(int signo, Sighandler_t handler) |
ff68c719 | 1854 | { |
1855 | struct sigaction act, oact; | |
1856 | ||
1857 | act.sa_handler = handler; | |
1858 | sigemptyset(&act.sa_mask); | |
1859 | act.sa_flags = 0; | |
1860 | #ifdef SA_RESTART | |
1861 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
1862 | #endif | |
1863 | if (sigaction(signo, &act, &oact) == -1) | |
36477c24 | 1864 | return SIG_ERR; |
ff68c719 | 1865 | else |
36477c24 | 1866 | return oact.sa_handler; |
ff68c719 | 1867 | } |
1868 | ||
1869 | Sighandler_t | |
8ac85365 | 1870 | rsignal_state(int signo) |
ff68c719 | 1871 | { |
1872 | struct sigaction oact; | |
1873 | ||
1874 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
1875 | return SIG_ERR; | |
1876 | else | |
1877 | return oact.sa_handler; | |
1878 | } | |
1879 | ||
1880 | int | |
8ac85365 | 1881 | rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 1882 | { |
1883 | struct sigaction act; | |
1884 | ||
1885 | act.sa_handler = handler; | |
1886 | sigemptyset(&act.sa_mask); | |
1887 | act.sa_flags = 0; | |
1888 | #ifdef SA_RESTART | |
1889 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
1890 | #endif | |
1891 | return sigaction(signo, &act, save); | |
1892 | } | |
1893 | ||
1894 | int | |
8ac85365 | 1895 | rsignal_restore(int signo, Sigsave_t *save) |
ff68c719 | 1896 | { |
1897 | return sigaction(signo, save, (struct sigaction *)NULL); | |
1898 | } | |
1899 | ||
1900 | #else /* !HAS_SIGACTION */ | |
1901 | ||
1902 | Sighandler_t | |
1903 | rsignal(signo, handler) | |
1904 | int signo; | |
1905 | Sighandler_t handler; | |
1906 | { | |
1907 | return signal(signo, handler); | |
1908 | } | |
1909 | ||
1910 | static int sig_trapped; | |
1911 | ||
1912 | static | |
1913 | Signal_t | |
1914 | sig_trap(signo) | |
1915 | int signo; | |
1916 | { | |
1917 | sig_trapped++; | |
1918 | } | |
1919 | ||
1920 | Sighandler_t | |
1921 | rsignal_state(signo) | |
1922 | int signo; | |
1923 | { | |
1924 | Sighandler_t oldsig; | |
1925 | ||
1926 | sig_trapped = 0; | |
1927 | oldsig = signal(signo, sig_trap); | |
1928 | signal(signo, oldsig); | |
1929 | if (sig_trapped) | |
1930 | kill(getpid(), signo); | |
1931 | return oldsig; | |
1932 | } | |
1933 | ||
1934 | int | |
1935 | rsignal_save(signo, handler, save) | |
1936 | int signo; | |
1937 | Sighandler_t handler; | |
1938 | Sigsave_t *save; | |
1939 | { | |
1940 | *save = signal(signo, handler); | |
1941 | return (*save == SIG_ERR) ? -1 : 0; | |
1942 | } | |
1943 | ||
1944 | int | |
36477c24 | 1945 | rsignal_restore(signo, save) |
ff68c719 | 1946 | int signo; |
1947 | Sigsave_t *save; | |
1948 | { | |
1949 | return (signal(signo, *save) == SIG_ERR) ? -1 : 0; | |
1950 | } | |
1951 | ||
1952 | #endif /* !HAS_SIGACTION */ | |
1953 | ||
5f05dabc | 1954 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
1955 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) | |
79072805 | 1956 | I32 |
8ac85365 | 1957 | my_pclose(FILE *ptr) |
a687059c | 1958 | { |
ff68c719 | 1959 | Sigsave_t hstat, istat, qstat; |
a687059c | 1960 | int status; |
a0d0e21e | 1961 | SV **svp; |
20188a90 | 1962 | int pid; |
03136e13 CS |
1963 | bool close_failed; |
1964 | int saved_errno; | |
1965 | #ifdef VMS | |
1966 | int saved_vaxc_errno; | |
1967 | #endif | |
a687059c | 1968 | |
760ac839 | 1969 | svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE); |
748a9306 | 1970 | pid = (int)SvIVX(*svp); |
a0d0e21e LW |
1971 | SvREFCNT_dec(*svp); |
1972 | *svp = &sv_undef; | |
ddcf38b7 IZ |
1973 | #ifdef OS2 |
1974 | if (pid == -1) { /* Opened by popen. */ | |
1975 | return my_syspclose(ptr); | |
1976 | } | |
1977 | #endif | |
03136e13 CS |
1978 | if ((close_failed = (PerlIO_close(ptr) == EOF))) { |
1979 | saved_errno = errno; | |
1980 | #ifdef VMS | |
1981 | saved_vaxc_errno = vaxc$errno; | |
1982 | #endif | |
1983 | } | |
7c0587c8 LW |
1984 | #ifdef UTS |
1985 | if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ | |
1986 | #endif | |
ff68c719 | 1987 | rsignal_save(SIGHUP, SIG_IGN, &hstat); |
1988 | rsignal_save(SIGINT, SIG_IGN, &istat); | |
1989 | rsignal_save(SIGQUIT, SIG_IGN, &qstat); | |
748a9306 LW |
1990 | do { |
1991 | pid = wait4pid(pid, &status, 0); | |
1992 | } while (pid == -1 && errno == EINTR); | |
ff68c719 | 1993 | rsignal_restore(SIGHUP, &hstat); |
1994 | rsignal_restore(SIGINT, &istat); | |
1995 | rsignal_restore(SIGQUIT, &qstat); | |
03136e13 CS |
1996 | if (close_failed) { |
1997 | SETERRNO(saved_errno, saved_vaxc_errno); | |
1998 | return -1; | |
1999 | } | |
2000 | return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status)); | |
20188a90 | 2001 | } |
4633a7c4 LW |
2002 | #endif /* !DOSISH */ |
2003 | ||
2004 | #if !defined(DOSISH) || defined(OS2) | |
79072805 | 2005 | I32 |
8ac85365 | 2006 | wait4pid(int pid, int *statusp, int flags) |
20188a90 | 2007 | { |
79072805 LW |
2008 | SV *sv; |
2009 | SV** svp; | |
fc36a67e | 2010 | char spid[TYPE_CHARS(int)]; |
20188a90 LW |
2011 | |
2012 | if (!pid) | |
2013 | return -1; | |
20188a90 LW |
2014 | if (pid > 0) { |
2015 | sprintf(spid, "%d", pid); | |
79072805 LW |
2016 | svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE); |
2017 | if (svp && *svp != &sv_undef) { | |
463ee0b2 | 2018 | *statusp = SvIVX(*svp); |
748a9306 | 2019 | (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
2020 | return pid; |
2021 | } | |
2022 | } | |
2023 | else { | |
79072805 | 2024 | HE *entry; |
20188a90 | 2025 | |
79072805 LW |
2026 | hv_iterinit(pidstatus); |
2027 | if (entry = hv_iternext(pidstatus)) { | |
a0d0e21e | 2028 | pid = atoi(hv_iterkey(entry,(I32*)statusp)); |
79072805 | 2029 | sv = hv_iterval(pidstatus,entry); |
463ee0b2 | 2030 | *statusp = SvIVX(sv); |
20188a90 | 2031 | sprintf(spid, "%d", pid); |
748a9306 | 2032 | (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
2033 | return pid; |
2034 | } | |
2035 | } | |
79072805 | 2036 | #ifdef HAS_WAITPID |
367f3c24 IZ |
2037 | # ifdef HAS_WAITPID_RUNTIME |
2038 | if (!HAS_WAITPID_RUNTIME) | |
2039 | goto hard_way; | |
2040 | # endif | |
79072805 | 2041 | return waitpid(pid,statusp,flags); |
367f3c24 IZ |
2042 | #endif |
2043 | #if !defined(HAS_WAITPID) && defined(HAS_WAIT4) | |
a0d0e21e | 2044 | return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *)); |
367f3c24 IZ |
2045 | #endif |
2046 | #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME) | |
2047 | hard_way: | |
a0d0e21e LW |
2048 | { |
2049 | I32 result; | |
2050 | if (flags) | |
2051 | croak("Can't do waitpid with flags"); | |
2052 | else { | |
2053 | while ((result = wait(statusp)) != pid && pid > 0 && result >= 0) | |
2054 | pidgone(result,*statusp); | |
2055 | if (result < 0) | |
2056 | *statusp = -1; | |
2057 | } | |
2058 | return result; | |
a687059c LW |
2059 | } |
2060 | #endif | |
a687059c | 2061 | } |
7c0587c8 | 2062 | #endif /* !DOSISH */ |
a687059c | 2063 | |
7c0587c8 | 2064 | void |
de3bb511 | 2065 | /*SUPPRESS 590*/ |
8ac85365 | 2066 | pidgone(int pid, int status) |
a687059c | 2067 | { |
79072805 | 2068 | register SV *sv; |
fc36a67e | 2069 | char spid[TYPE_CHARS(int)]; |
a687059c | 2070 | |
20188a90 | 2071 | sprintf(spid, "%d", pid); |
79072805 | 2072 | sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE); |
a0d0e21e | 2073 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 2074 | SvIVX(sv) = status; |
20188a90 | 2075 | return; |
a687059c LW |
2076 | } |
2077 | ||
55497cff | 2078 | #if defined(atarist) || defined(OS2) || defined(DJGPP) |
7c0587c8 | 2079 | int pclose(); |
ddcf38b7 IZ |
2080 | #ifdef HAS_FORK |
2081 | int /* Cannot prototype with I32 | |
2082 | in os2ish.h. */ | |
2083 | my_syspclose(ptr) | |
2084 | #else | |
79072805 LW |
2085 | I32 |
2086 | my_pclose(ptr) | |
ddcf38b7 | 2087 | #endif |
760ac839 | 2088 | PerlIO *ptr; |
a687059c | 2089 | { |
760ac839 LW |
2090 | /* Needs work for PerlIO ! */ |
2091 | FILE *f = PerlIO_findFILE(ptr); | |
2092 | I32 result = pclose(f); | |
2093 | PerlIO_releaseFILE(ptr,f); | |
2094 | return result; | |
a687059c | 2095 | } |
7c0587c8 | 2096 | #endif |
9f68db38 LW |
2097 | |
2098 | void | |
8ac85365 | 2099 | repeatcpy(register char *to, register char *from, I32 len, register I32 count) |
9f68db38 | 2100 | { |
79072805 | 2101 | register I32 todo; |
9f68db38 LW |
2102 | register char *frombase = from; |
2103 | ||
2104 | if (len == 1) { | |
2105 | todo = *from; | |
2106 | while (count-- > 0) | |
2107 | *to++ = todo; | |
2108 | return; | |
2109 | } | |
2110 | while (count-- > 0) { | |
2111 | for (todo = len; todo > 0; todo--) { | |
2112 | *to++ = *from++; | |
2113 | } | |
2114 | from = frombase; | |
2115 | } | |
2116 | } | |
0f85fab0 LW |
2117 | |
2118 | #ifndef CASTNEGFLOAT | |
463ee0b2 | 2119 | U32 |
79072805 | 2120 | cast_ulong(f) |
0f85fab0 LW |
2121 | double f; |
2122 | { | |
2123 | long along; | |
2124 | ||
27e2fb84 | 2125 | #if CASTFLAGS & 2 |
34de22dd LW |
2126 | # define BIGDOUBLE 2147483648.0 |
2127 | if (f >= BIGDOUBLE) | |
2128 | return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000; | |
2129 | #endif | |
0f85fab0 LW |
2130 | if (f >= 0.0) |
2131 | return (unsigned long)f; | |
2132 | along = (long)f; | |
2133 | return (unsigned long)along; | |
2134 | } | |
ed6116ce LW |
2135 | # undef BIGDOUBLE |
2136 | #endif | |
2137 | ||
2138 | #ifndef CASTI32 | |
5d94fbed | 2139 | |
5d94fbed AD |
2140 | /* Unfortunately, on some systems the cast_uv() function doesn't |
2141 | work with the system-supplied definition of ULONG_MAX. The | |
2142 | comparison (f >= ULONG_MAX) always comes out true. It must be a | |
2143 | problem with the compiler constant folding. | |
2144 | ||
2145 | In any case, this workaround should be fine on any two's complement | |
2146 | system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your | |
2147 | ccflags. | |
2148 | --Andy Dougherty <doughera@lafcol.lafayette.edu> | |
2149 | */ | |
1eb770ff | 2150 | |
2151 | /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead | |
2152 | of LONG_(MIN/MAX). | |
2153 | -- Kenneth Albanowski <kjahds@kjahds.com> | |
2154 | */ | |
2155 | ||
20cec16a | 2156 | #ifndef MY_UV_MAX |
2157 | # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1) | |
5d94fbed AD |
2158 | #endif |
2159 | ||
ed6116ce LW |
2160 | I32 |
2161 | cast_i32(f) | |
2162 | double f; | |
2163 | { | |
20cec16a | 2164 | if (f >= I32_MAX) |
2165 | return (I32) I32_MAX; | |
2166 | if (f <= I32_MIN) | |
2167 | return (I32) I32_MIN; | |
ed6116ce LW |
2168 | return (I32) f; |
2169 | } | |
a0d0e21e LW |
2170 | |
2171 | IV | |
2172 | cast_iv(f) | |
2173 | double f; | |
2174 | { | |
20cec16a | 2175 | if (f >= IV_MAX) |
2176 | return (IV) IV_MAX; | |
2177 | if (f <= IV_MIN) | |
2178 | return (IV) IV_MIN; | |
a0d0e21e LW |
2179 | return (IV) f; |
2180 | } | |
5d94fbed AD |
2181 | |
2182 | UV | |
2183 | cast_uv(f) | |
2184 | double f; | |
2185 | { | |
20cec16a | 2186 | if (f >= MY_UV_MAX) |
2187 | return (UV) MY_UV_MAX; | |
5d94fbed AD |
2188 | return (UV) f; |
2189 | } | |
2190 | ||
0f85fab0 | 2191 | #endif |
62b28dd9 | 2192 | |
fe14fcc3 | 2193 | #ifndef HAS_RENAME |
79072805 | 2194 | I32 |
62b28dd9 LW |
2195 | same_dirent(a,b) |
2196 | char *a; | |
2197 | char *b; | |
2198 | { | |
93a17b20 LW |
2199 | char *fa = strrchr(a,'/'); |
2200 | char *fb = strrchr(b,'/'); | |
62b28dd9 LW |
2201 | struct stat tmpstatbuf1; |
2202 | struct stat tmpstatbuf2; | |
46fc3d4c | 2203 | SV *tmpsv = sv_newmortal(); |
62b28dd9 LW |
2204 | |
2205 | if (fa) | |
2206 | fa++; | |
2207 | else | |
2208 | fa = a; | |
2209 | if (fb) | |
2210 | fb++; | |
2211 | else | |
2212 | fb = b; | |
2213 | if (strNE(a,b)) | |
2214 | return FALSE; | |
2215 | if (fa == a) | |
46fc3d4c | 2216 | sv_setpv(tmpsv, "."); |
62b28dd9 | 2217 | else |
46fc3d4c | 2218 | sv_setpvn(tmpsv, a, fa - a); |
2219 | if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0) | |
62b28dd9 LW |
2220 | return FALSE; |
2221 | if (fb == b) | |
46fc3d4c | 2222 | sv_setpv(tmpsv, "."); |
62b28dd9 | 2223 | else |
46fc3d4c | 2224 | sv_setpvn(tmpsv, b, fb - b); |
2225 | if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0) | |
62b28dd9 LW |
2226 | return FALSE; |
2227 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
2228 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
2229 | } | |
fe14fcc3 LW |
2230 | #endif /* !HAS_RENAME */ |
2231 | ||
55497cff | 2232 | UV |
8ac85365 | 2233 | scan_oct(char *start, I32 len, I32 *retlen) |
fe14fcc3 LW |
2234 | { |
2235 | register char *s = start; | |
55497cff | 2236 | register UV retval = 0; |
2237 | bool overflowed = FALSE; | |
fe14fcc3 | 2238 | |
748a9306 | 2239 | while (len && *s >= '0' && *s <= '7') { |
55497cff | 2240 | register UV n = retval << 3; |
2241 | if (!overflowed && (n >> 3) != retval) { | |
2242 | warn("Integer overflow in octal number"); | |
2243 | overflowed = TRUE; | |
2244 | } | |
2245 | retval = n | (*s++ - '0'); | |
748a9306 | 2246 | len--; |
fe14fcc3 | 2247 | } |
748a9306 LW |
2248 | if (dowarn && len && (*s == '8' || *s == '9')) |
2249 | warn("Illegal octal digit ignored"); | |
fe14fcc3 LW |
2250 | *retlen = s - start; |
2251 | return retval; | |
2252 | } | |
2253 | ||
71be2cbc | 2254 | UV |
8ac85365 | 2255 | scan_hex(char *start, I32 len, I32 *retlen) |
fe14fcc3 LW |
2256 | { |
2257 | register char *s = start; | |
55497cff | 2258 | register UV retval = 0; |
2259 | bool overflowed = FALSE; | |
fe14fcc3 LW |
2260 | char *tmp; |
2261 | ||
93a17b20 | 2262 | while (len-- && *s && (tmp = strchr(hexdigit, *s))) { |
55497cff | 2263 | register UV n = retval << 4; |
2264 | if (!overflowed && (n >> 4) != retval) { | |
2265 | warn("Integer overflow in hex number"); | |
2266 | overflowed = TRUE; | |
2267 | } | |
2268 | retval = n | (tmp - hexdigit) & 15; | |
fe14fcc3 LW |
2269 | s++; |
2270 | } | |
2271 | *retlen = s - start; | |
2272 | return retval; | |
2273 | } | |
760ac839 | 2274 | |
11343788 | 2275 | #ifdef USE_THREADS |
12ca11f6 MB |
2276 | #ifdef FAKE_THREADS |
2277 | /* Very simplistic scheduler for now */ | |
2278 | void | |
2279 | schedule(void) | |
2280 | { | |
c7848ba1 | 2281 | thr = thr->i.next_run; |
12ca11f6 MB |
2282 | } |
2283 | ||
2284 | void | |
2285 | perl_cond_init(cp) | |
2286 | perl_cond *cp; | |
2287 | { | |
2288 | *cp = 0; | |
2289 | } | |
2290 | ||
2291 | void | |
2292 | perl_cond_signal(cp) | |
2293 | perl_cond *cp; | |
2294 | { | |
2295 | perl_thread t; | |
2296 | perl_cond cond = *cp; | |
2297 | ||
2298 | if (!cond) | |
2299 | return; | |
2300 | t = cond->thread; | |
2301 | /* Insert t in the runnable queue just ahead of us */ | |
c7848ba1 MB |
2302 | t->i.next_run = thr->i.next_run; |
2303 | thr->i.next_run->i.prev_run = t; | |
2304 | t->i.prev_run = thr; | |
2305 | thr->i.next_run = t; | |
2306 | thr->i.wait_queue = 0; | |
12ca11f6 MB |
2307 | /* Remove from the wait queue */ |
2308 | *cp = cond->next; | |
2309 | Safefree(cond); | |
2310 | } | |
2311 | ||
2312 | void | |
2313 | perl_cond_broadcast(cp) | |
2314 | perl_cond *cp; | |
2315 | { | |
2316 | perl_thread t; | |
2317 | perl_cond cond, cond_next; | |
2318 | ||
2319 | for (cond = *cp; cond; cond = cond_next) { | |
2320 | t = cond->thread; | |
2321 | /* Insert t in the runnable queue just ahead of us */ | |
c7848ba1 MB |
2322 | t->i.next_run = thr->i.next_run; |
2323 | thr->i.next_run->i.prev_run = t; | |
2324 | t->i.prev_run = thr; | |
2325 | thr->i.next_run = t; | |
2326 | thr->i.wait_queue = 0; | |
12ca11f6 MB |
2327 | /* Remove from the wait queue */ |
2328 | cond_next = cond->next; | |
2329 | Safefree(cond); | |
2330 | } | |
2331 | *cp = 0; | |
2332 | } | |
2333 | ||
2334 | void | |
2335 | perl_cond_wait(cp) | |
2336 | perl_cond *cp; | |
2337 | { | |
2338 | perl_cond cond; | |
2339 | ||
c7848ba1 | 2340 | if (thr->i.next_run == thr) |
12ca11f6 MB |
2341 | croak("panic: perl_cond_wait called by last runnable thread"); |
2342 | ||
0f15f207 | 2343 | New(666, cond, 1, struct perl_wait_queue); |
12ca11f6 MB |
2344 | cond->thread = thr; |
2345 | cond->next = *cp; | |
2346 | *cp = cond; | |
c7848ba1 | 2347 | thr->i.wait_queue = cond; |
12ca11f6 | 2348 | /* Remove ourselves from runnable queue */ |
c7848ba1 MB |
2349 | thr->i.next_run->i.prev_run = thr->i.prev_run; |
2350 | thr->i.prev_run->i.next_run = thr->i.next_run; | |
12ca11f6 MB |
2351 | } |
2352 | #endif /* FAKE_THREADS */ | |
2353 | ||
11343788 MB |
2354 | #ifdef OLD_PTHREADS_API |
2355 | struct thread * | |
2356 | getTHR _((void)) | |
2357 | { | |
2358 | pthread_addr_t t; | |
2359 | ||
2360 | if (pthread_getspecific(thr_key, &t)) | |
2361 | croak("panic: pthread_getspecific"); | |
2362 | return (struct thread *) t; | |
2363 | } | |
2364 | #endif /* OLD_PTHREADS_API */ | |
f93b4edd MB |
2365 | |
2366 | MAGIC * | |
8ac85365 | 2367 | condpair_magic(SV *sv) |
f93b4edd MB |
2368 | { |
2369 | MAGIC *mg; | |
2370 | ||
2371 | SvUPGRADE(sv, SVt_PVMG); | |
2372 | mg = mg_find(sv, 'm'); | |
2373 | if (!mg) { | |
2374 | condpair_t *cp; | |
2375 | ||
2376 | New(53, cp, 1, condpair_t); | |
2377 | MUTEX_INIT(&cp->mutex); | |
2378 | COND_INIT(&cp->owner_cond); | |
2379 | COND_INIT(&cp->cond); | |
2380 | cp->owner = 0; | |
2381 | MUTEX_LOCK(&sv_mutex); | |
2382 | mg = mg_find(sv, 'm'); | |
2383 | if (mg) { | |
2384 | /* someone else beat us to initialising it */ | |
2385 | MUTEX_UNLOCK(&sv_mutex); | |
2386 | MUTEX_DESTROY(&cp->mutex); | |
2387 | COND_DESTROY(&cp->owner_cond); | |
2388 | COND_DESTROY(&cp->cond); | |
2389 | Safefree(cp); | |
2390 | } | |
2391 | else { | |
2392 | sv_magic(sv, Nullsv, 'm', 0, 0); | |
2393 | mg = SvMAGIC(sv); | |
2394 | mg->mg_ptr = (char *)cp; | |
2395 | mg->mg_len = sizeof(cp); | |
2396 | MUTEX_UNLOCK(&sv_mutex); | |
bc1f4c86 | 2397 | DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(), |
c7848ba1 | 2398 | "%p: condpair_magic %p\n", thr, sv));) |
f93b4edd MB |
2399 | } |
2400 | } | |
2401 | return mg; | |
2402 | } | |
a863c7d1 MB |
2403 | |
2404 | /* | |
2405 | * Make a new perl thread structure using t as a prototype. If t is NULL | |
2406 | * then this is the initial main thread and we have to bootstrap carefully. | |
2407 | * Some of the fields for the new thread are copied from the prototype | |
2408 | * thread, t, so t should not be running in perl at the time this function | |
2409 | * is called. The usual case, where t is the thread calling new_struct_thread, | |
2410 | * clearly satisfies this constraint. | |
2411 | */ | |
2412 | struct thread * | |
2413 | new_struct_thread(t) | |
2414 | struct thread *t; | |
2415 | { | |
2416 | struct thread *thr; | |
2417 | XPV *xpv; | |
2418 | SV *sv; | |
2419 | ||
2420 | Newz(53, thr, 1, struct thread); | |
2421 | cvcache = newHV(); | |
2422 | curcop = &compiling; | |
2423 | thr->specific = newAV(); | |
2424 | thr->flags = THRf_R_JOINABLE; | |
2425 | MUTEX_INIT(&thr->mutex); | |
2426 | if (t) { | |
2427 | oursv = newSVpv("", 0); | |
2428 | SvGROW(oursv, sizeof(struct thread) + 1); | |
2429 | SvCUR_set(oursv, sizeof(struct thread)); | |
2430 | thr = (struct thread *) SvPVX(sv); | |
2431 | } else { | |
2432 | /* Handcraft thrsv similarly to mess_sv */ | |
2433 | New(53, thrsv, 1, SV); | |
2434 | Newz(53, xpv, 1, XPV); | |
2435 | SvFLAGS(thrsv) = SVt_PV; | |
2436 | SvANY(thrsv) = (void*)xpv; | |
2437 | SvREFCNT(thrsv) = 1 << 30; /* practically infinite */ | |
2438 | SvPVX(thrsv) = (char*)thr; | |
2439 | SvCUR_set(thrsv, sizeof(thr)); | |
2440 | SvLEN_set(thrsv, sizeof(thr)); | |
2441 | *SvEND(thrsv) = '\0'; /* in the trailing_nul field */ | |
2442 | oursv = thrsv; | |
2443 | } | |
2444 | if (t) { | |
2445 | curcop = t->Tcurcop; /* XXX As good a guess as any? */ | |
2446 | defstash = t->Tdefstash; /* XXX maybe these should */ | |
2447 | curstash = t->Tcurstash; /* always be set to main? */ | |
2448 | /* top_env? */ | |
2449 | /* runlevel */ | |
2450 | tainted = t->Ttainted; | |
2451 | curpm = t->Tcurpm; /* XXX No PMOP ref count */ | |
2452 | nrs = newSVsv(t->Tnrs); | |
2453 | rs = newSVsv(t->Trs); | |
2454 | last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv); | |
2455 | ofslen = t->Tofslen; | |
2456 | ofs = savepvn(t->Tofs, ofslen); | |
2457 | defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv); | |
2458 | chopset = t->Tchopset; | |
2459 | formtarget = newSVsv(t->Tformtarget); | |
2460 | bodytarget = newSVsv(t->Tbodytarget); | |
2461 | toptarget = newSVsv(t->Ttoptarget); | |
2462 | keys = newSVpv("", 0); | |
2463 | } else { | |
2464 | curcop = &compiling; | |
2465 | chopset = " \n-"; | |
2466 | } | |
2467 | MUTEX_LOCK(&threads_mutex); | |
2468 | nthreads++; | |
2469 | thr->tid = threadnum++; | |
2470 | if (t) { | |
2471 | thr->next = t->next; | |
2472 | thr->prev = t; | |
2473 | t->next = thr; | |
2474 | thr->next->prev = thr; | |
2475 | } else { | |
2476 | thr->next = thr; | |
2477 | thr->prev = thr; | |
2478 | } | |
2479 | MUTEX_UNLOCK(&threads_mutex); | |
2480 | ||
2481 | #ifdef HAVE_THREAD_INTERN | |
2482 | init_thread_intern(thr); | |
2483 | #else | |
2484 | thr->self = pthread_self(); | |
2485 | #endif /* HAVE_THREAD_INTERN */ | |
2486 | SET_THR(thr); | |
2487 | if (!t) { | |
2488 | /* | |
2489 | * These must come after the SET_THR because sv_setpvn does | |
2490 | * SvTAINT and the taint fields require dTHR. | |
2491 | */ | |
2492 | toptarget = NEWSV(0,0); | |
2493 | sv_upgrade(toptarget, SVt_PVFM); | |
2494 | sv_setpvn(toptarget, "", 0); | |
2495 | bodytarget = NEWSV(0,0); | |
2496 | sv_upgrade(bodytarget, SVt_PVFM); | |
2497 | sv_setpvn(bodytarget, "", 0); | |
2498 | formtarget = bodytarget; | |
2499 | } | |
2500 | return thr; | |
2501 | } | |
2502 | ||
2503 | PADOFFSET | |
2504 | key_create() | |
2505 | { | |
2506 | char *s; | |
2507 | STRLEN len; | |
2508 | PADOFFSET i; | |
2509 | MUTEX_LOCK(&keys_mutex); | |
2510 | s = SvPV(keys, len); | |
2511 | for (i = 0; i < len; i++) { | |
2512 | if (!s[i]) { | |
2513 | s[i] = 1; | |
2514 | break; | |
2515 | } | |
2516 | } | |
2517 | if (i == len) | |
2518 | sv_catpvn(keys, "\1", 1); | |
2519 | MUTEX_UNLOCK(&keys_mutex); | |
2520 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), "key_create: %d\n", (int)i)); | |
2521 | return i; | |
2522 | } | |
2523 | ||
2524 | void | |
2525 | key_destroy(key) | |
2526 | PADOFFSET key; | |
2527 | { | |
2528 | char *s; | |
2529 | MUTEX_LOCK(&keys_mutex); | |
2530 | s = SvPVX(keys); | |
2531 | s[key] = 0; | |
2532 | MUTEX_UNLOCK(&keys_mutex); | |
2533 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), "key_destroy: %d\n", (int)key)); | |
2534 | } | |
11343788 | 2535 | #endif /* USE_THREADS */ |
760ac839 LW |
2536 | |
2537 | #ifdef HUGE_VAL | |
2538 | /* | |
2539 | * This hack is to force load of "huge" support from libm.a | |
2540 | * So it is in perl for (say) POSIX to use. | |
2541 | * Needed for SunOS with Sun's 'acc' for example. | |
2542 | */ | |
2543 | double | |
8ac85365 | 2544 | Perl_huge(void) |
760ac839 LW |
2545 | { |
2546 | return HUGE_VAL; | |
2547 | } | |
2548 | #endif |