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