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