3 * Copyright (c) 1991-1997, Larry Wall
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.
11 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
12 * not content." --Gandalf
18 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
23 # define SIG_ERR ((Sighandler_t) -1)
26 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
35 /* Put this after #includes because fork and vfork prototypes may
46 # include <sys/file.h>
50 # include <sys/wait.h>
57 static void xstat _((int));
58 long xcount[MAXXCOUNT];
59 long lastxcount[MAXXCOUNT];
60 long xycount[MAXXCOUNT][MAXYCOUNT];
61 long lastxycount[MAXXCOUNT][MAXYCOUNT];
67 /* paranoid version of malloc */
69 /* NOTE: Do not call the next three routines directly. Use the macros
70 * in handy.h, so that we can easily redefine everything to do tracking of
71 * allocated hunks back to the original New to track down any memory leaks.
72 * XXX This advice seems to be widely ignored :-( --AD August 1996.
76 safemalloc(MEM_SIZE size)
81 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
84 #endif /* HAS_64K_LIMIT */
87 croak("panic: malloc");
89 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
90 #if !(defined(I286) || defined(atarist))
91 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
93 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
100 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
107 /* paranoid version of realloc */
110 saferealloc(Malloc_t where,MEM_SIZE size)
113 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
114 Malloc_t PerlMem_realloc();
115 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
119 PerlIO_printf(PerlIO_stderr(),
120 "Reallocation too large: %lx\n", size) FLUSH;
123 #endif /* HAS_64K_LIMIT */
130 return safemalloc(size);
133 croak("panic: realloc");
135 ptr = PerlMem_realloc(where,size);
137 #if !(defined(I286) || defined(atarist))
139 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
140 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
144 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
145 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
154 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
161 /* safe version of free */
164 safefree(Malloc_t where)
166 #if !(defined(I286) || defined(atarist))
167 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
169 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
177 /* safe version of calloc */
180 safecalloc(MEM_SIZE count, MEM_SIZE size)
185 if (size * count > 0xffff) {
186 PerlIO_printf(PerlIO_stderr(),
187 "Allocation too large: %lx\n", size * count) FLUSH;
190 #endif /* HAS_64K_LIMIT */
192 if ((long)size < 0 || (long)count < 0)
193 croak("panic: calloc");
196 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
197 #if !(defined(I286) || defined(atarist))
198 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
200 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
203 memset((void*)ptr, 0, size);
209 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
216 #endif /* !MYMALLOC */
220 struct mem_test_strut {
228 # define ALIGN sizeof(struct mem_test_strut)
230 # define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size)
231 # define typeof_chunk(ch) \
232 (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100)
233 # define set_typeof_chunk(ch,t) \
234 (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100)
235 #define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \
238 ? ((size) - 1)/8 + 5 \
242 safexmalloc(I32 x, MEM_SIZE size)
244 register char* where = (char*)safemalloc(size + ALIGN);
247 xycount[x][SIZE_TO_Y(size)]++;
248 set_typeof_chunk(where, x);
249 sizeof_chunk(where) = size;
250 return (Malloc_t)(where + ALIGN);
254 safexrealloc(Malloc_t wh, MEM_SIZE size)
256 char *where = (char*)wh;
259 return safexmalloc(0,size);
262 MEM_SIZE old = sizeof_chunk(where - ALIGN);
263 int t = typeof_chunk(where - ALIGN);
264 register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN);
266 xycount[t][SIZE_TO_Y(old)]--;
267 xycount[t][SIZE_TO_Y(size)]++;
268 xcount[t] += size - old;
269 sizeof_chunk(new) = size;
270 return (Malloc_t)(new + ALIGN);
275 safexfree(Malloc_t wh)
278 char *where = (char*)wh;
284 size = sizeof_chunk(where);
285 x = where[0] + 100 * where[1];
287 xycount[x][SIZE_TO_Y(size)]--;
292 safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
294 register char * where = (char*)safexmalloc(x, size * count + ALIGN);
296 xycount[x][SIZE_TO_Y(size)]++;
297 memset((void*)(where + ALIGN), 0, size * count);
298 set_typeof_chunk(where, x);
299 sizeof_chunk(where) = size;
300 return (Malloc_t)(where + ALIGN);
306 register I32 i, j, total = 0;
307 I32 subtot[MAXYCOUNT];
309 for (j = 0; j < MAXYCOUNT; j++) {
313 PerlIO_printf(PerlIO_stderr(), " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total);
314 for (i = 0; i < MAXXCOUNT; i++) {
316 for (j = 0; j < MAXYCOUNT; j++) {
317 subtot[j] += xycount[i][j];
320 ? xcount[i] /* Have something */
322 ? xcount[i] != lastxcount[i] /* Changed */
323 : xcount[i] > lastxcount[i])) { /* Growed */
324 PerlIO_printf(PerlIO_stderr(),"%2d %02d %7ld ", i / 100, i % 100,
325 flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]);
326 lastxcount[i] = xcount[i];
327 for (j = 0; j < MAXYCOUNT; j++) {
329 ? xycount[i][j] /* Have something */
331 ? xycount[i][j] != lastxycount[i][j] /* Changed */
332 : xycount[i][j] > lastxycount[i][j])) { /* Growed */
333 PerlIO_printf(PerlIO_stderr(),"%3ld ",
335 ? xycount[i][j] - lastxycount[i][j]
337 lastxycount[i][j] = xycount[i][j];
339 PerlIO_printf(PerlIO_stderr(), " . ", xycount[i][j]);
342 PerlIO_printf(PerlIO_stderr(), "\n");
346 PerlIO_printf(PerlIO_stderr(), "Total %7ld ", total);
347 for (j = 0; j < MAXYCOUNT; j++) {
349 PerlIO_printf(PerlIO_stderr(), "%3ld ", subtot[j]);
351 PerlIO_printf(PerlIO_stderr(), " . ");
354 PerlIO_printf(PerlIO_stderr(), "\n");
358 #endif /* LEAKTEST */
360 /* copy a string up to some (non-backslashed) delimiter, if any */
363 delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
366 for (tolen = 0; from < fromend; from++, tolen++) {
368 if (from[1] == delim)
377 else if (*from == delim)
388 /* return ptr to little string in big string, NULL if not found */
389 /* This routine was donated by Corey Satten. */
392 instr(register char *big, register char *little)
394 register char *s, *x;
405 for (x=big,s=little; *s; /**/ ) {
419 /* same as instr but allow embedded nulls */
422 ninstr(register char *big, register char *bigend, char *little, char *lend)
424 register char *s, *x;
425 register I32 first = *little;
426 register char *littleend = lend;
428 if (!first && little >= littleend)
430 if (bigend - big < littleend - little)
432 bigend -= littleend - little++;
433 while (big <= bigend) {
436 for (x=big,s=little; s < littleend; /**/ ) {
448 /* reverse of the above--find last substring */
451 rninstr(register char *big, char *bigend, char *little, char *lend)
453 register char *bigbeg;
454 register char *s, *x;
455 register I32 first = *little;
456 register char *littleend = lend;
458 if (!first && little >= littleend)
461 big = bigend - (littleend - little++);
462 while (big >= bigbeg) {
465 for (x=big+2,s=little; s < littleend; /**/ ) {
478 * Set up for a new ctype locale.
481 perl_new_ctype(char *newctype)
483 #ifdef USE_LOCALE_CTYPE
487 for (i = 0; i < 256; i++) {
489 fold_locale[i] = toLOWER_LC(i);
490 else if (isLOWER_LC(i))
491 fold_locale[i] = toUPPER_LC(i);
496 #endif /* USE_LOCALE_CTYPE */
500 * Set up for a new collation locale.
503 perl_new_collate(char *newcoll)
505 #ifdef USE_LOCALE_COLLATE
508 if (collation_name) {
510 Safefree(collation_name);
511 collation_name = NULL;
512 collation_standard = TRUE;
519 if (! collation_name || strNE(collation_name, newcoll)) {
521 Safefree(collation_name);
522 collation_name = savepv(newcoll);
523 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
526 /* 2: at most so many chars ('a', 'b'). */
527 /* 50: surely no system expands a char more. */
528 #define XFRMBUFSIZE (2 * 50)
529 char xbuf[XFRMBUFSIZE];
530 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
531 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
532 SSize_t mult = fb - fa;
534 croak("strxfrm() gets absurd");
535 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
536 collxfrm_mult = mult;
540 #endif /* USE_LOCALE_COLLATE */
544 * Set up for a new numeric locale.
547 perl_new_numeric(char *newnum)
549 #ifdef USE_LOCALE_NUMERIC
553 Safefree(numeric_name);
555 numeric_standard = TRUE;
556 numeric_local = TRUE;
561 if (! numeric_name || strNE(numeric_name, newnum)) {
562 Safefree(numeric_name);
563 numeric_name = savepv(newnum);
564 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
565 numeric_local = TRUE;
568 #endif /* USE_LOCALE_NUMERIC */
572 perl_set_numeric_standard(void)
574 #ifdef USE_LOCALE_NUMERIC
576 if (! numeric_standard) {
577 setlocale(LC_NUMERIC, "C");
578 numeric_standard = TRUE;
579 numeric_local = FALSE;
582 #endif /* USE_LOCALE_NUMERIC */
586 perl_set_numeric_local(void)
588 #ifdef USE_LOCALE_NUMERIC
590 if (! numeric_local) {
591 setlocale(LC_NUMERIC, numeric_name);
592 numeric_standard = FALSE;
593 numeric_local = TRUE;
596 #endif /* USE_LOCALE_NUMERIC */
601 * Initialize locale awareness.
604 perl_init_i18nl10n(int printwarn)
608 * 1 = set ok or not applicable,
609 * 0 = fallback to C locale,
610 * -1 = fallback to C locale failed
615 #ifdef USE_LOCALE_CTYPE
616 char *curctype = NULL;
617 #endif /* USE_LOCALE_CTYPE */
618 #ifdef USE_LOCALE_COLLATE
619 char *curcoll = NULL;
620 #endif /* USE_LOCALE_COLLATE */
621 #ifdef USE_LOCALE_NUMERIC
623 #endif /* USE_LOCALE_NUMERIC */
624 char *lc_all = PerlEnv_getenv("LC_ALL");
625 char *lang = PerlEnv_getenv("LANG");
626 bool setlocale_failure = FALSE;
628 #ifdef LOCALE_ENVIRON_REQUIRED
631 * Ultrix setlocale(..., "") fails if there are no environment
632 * variables from which to get a locale name.
639 if (setlocale(LC_ALL, ""))
642 setlocale_failure = TRUE;
644 if (!setlocale_failure)
647 #ifdef USE_LOCALE_CTYPE
648 if (! (curctype = setlocale(LC_CTYPE,
649 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
651 setlocale_failure = TRUE;
652 #endif /* USE_LOCALE_CTYPE */
653 #ifdef USE_LOCALE_COLLATE
654 if (! (curcoll = setlocale(LC_COLLATE,
655 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
657 setlocale_failure = TRUE;
658 #endif /* USE_LOCALE_COLLATE */
659 #ifdef USE_LOCALE_NUMERIC
660 if (! (curnum = setlocale(LC_NUMERIC,
661 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
663 setlocale_failure = TRUE;
664 #endif /* USE_LOCALE_NUMERIC */
667 #else /* !LOCALE_ENVIRON_REQUIRED */
671 if (! setlocale(LC_ALL, ""))
672 setlocale_failure = TRUE;
674 #ifdef USE_LOCALE_CTYPE
675 curctype = setlocale(LC_CTYPE, Nullch);
676 #endif /* USE_LOCALE_CTYPE */
677 #ifdef USE_LOCALE_COLLATE
678 curcoll = setlocale(LC_COLLATE, Nullch);
679 #endif /* USE_LOCALE_COLLATE */
680 #ifdef USE_LOCALE_NUMERIC
681 curnum = setlocale(LC_NUMERIC, Nullch);
682 #endif /* USE_LOCALE_NUMERIC */
687 #ifdef USE_LOCALE_CTYPE
688 if (! (curctype = setlocale(LC_CTYPE, "")))
689 setlocale_failure = TRUE;
690 #endif /* USE_LOCALE_CTYPE */
691 #ifdef USE_LOCALE_COLLATE
692 if (! (curcoll = setlocale(LC_COLLATE, "")))
693 setlocale_failure = TRUE;
694 #endif /* USE_LOCALE_COLLATE */
695 #ifdef USE_LOCALE_NUMERIC
696 if (! (curnum = setlocale(LC_NUMERIC, "")))
697 setlocale_failure = TRUE;
698 #endif /* USE_LOCALE_NUMERIC */
702 #endif /* !LOCALE_ENVIRON_REQUIRED */
704 if (setlocale_failure) {
706 bool locwarn = (printwarn > 1 ||
708 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)));
713 PerlIO_printf(PerlIO_stderr(),
714 "perl: warning: Setting locale failed.\n");
718 PerlIO_printf(PerlIO_stderr(),
719 "perl: warning: Setting locale failed for the categories:\n\t");
720 #ifdef USE_LOCALE_CTYPE
722 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
723 #endif /* USE_LOCALE_CTYPE */
724 #ifdef USE_LOCALE_COLLATE
726 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
727 #endif /* USE_LOCALE_COLLATE */
728 #ifdef USE_LOCALE_NUMERIC
730 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
731 #endif /* USE_LOCALE_NUMERIC */
732 PerlIO_printf(PerlIO_stderr(), "\n");
736 PerlIO_printf(PerlIO_stderr(),
737 "perl: warning: Please check that your locale settings:\n");
739 PerlIO_printf(PerlIO_stderr(),
740 "\tLC_ALL = %c%s%c,\n",
742 lc_all ? lc_all : "unset",
747 for (e = environ; *e; e++) {
748 if (strnEQ(*e, "LC_", 3)
749 && strnNE(*e, "LC_ALL=", 7)
750 && (p = strchr(*e, '=')))
751 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
752 (int)(p - *e), *e, p + 1);
756 PerlIO_printf(PerlIO_stderr(),
759 lang ? lang : "unset",
762 PerlIO_printf(PerlIO_stderr(),
763 " are supported and installed on your system.\n");
768 if (setlocale(LC_ALL, "C")) {
770 PerlIO_printf(PerlIO_stderr(),
771 "perl: warning: Falling back to the standard locale (\"C\").\n");
776 PerlIO_printf(PerlIO_stderr(),
777 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
784 #ifdef USE_LOCALE_CTYPE
785 || !(curctype || setlocale(LC_CTYPE, "C"))
786 #endif /* USE_LOCALE_CTYPE */
787 #ifdef USE_LOCALE_COLLATE
788 || !(curcoll || setlocale(LC_COLLATE, "C"))
789 #endif /* USE_LOCALE_COLLATE */
790 #ifdef USE_LOCALE_NUMERIC
791 || !(curnum || setlocale(LC_NUMERIC, "C"))
792 #endif /* USE_LOCALE_NUMERIC */
796 PerlIO_printf(PerlIO_stderr(),
797 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
801 #endif /* ! LC_ALL */
803 #ifdef USE_LOCALE_CTYPE
804 curctype = setlocale(LC_CTYPE, Nullch);
805 #endif /* USE_LOCALE_CTYPE */
806 #ifdef USE_LOCALE_COLLATE
807 curcoll = setlocale(LC_COLLATE, Nullch);
808 #endif /* USE_LOCALE_COLLATE */
809 #ifdef USE_LOCALE_NUMERIC
810 curnum = setlocale(LC_NUMERIC, Nullch);
811 #endif /* USE_LOCALE_NUMERIC */
814 #ifdef USE_LOCALE_CTYPE
815 perl_new_ctype(curctype);
816 #endif /* USE_LOCALE_CTYPE */
818 #ifdef USE_LOCALE_COLLATE
819 perl_new_collate(curcoll);
820 #endif /* USE_LOCALE_COLLATE */
822 #ifdef USE_LOCALE_NUMERIC
823 perl_new_numeric(curnum);
824 #endif /* USE_LOCALE_NUMERIC */
826 #endif /* USE_LOCALE */
831 /* Backwards compatibility. */
833 perl_init_i18nl14n(int printwarn)
835 return perl_init_i18nl10n(printwarn);
838 #ifdef USE_LOCALE_COLLATE
841 * mem_collxfrm() is a bit like strxfrm() but with two important
842 * differences. First, it handles embedded NULs. Second, it allocates
843 * a bit more memory than needed for the transformed data itself.
844 * The real transformed data begins at offset sizeof(collationix).
845 * Please see sv_collxfrm() to see how this is used.
848 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
851 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
853 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
854 /* the +1 is for the terminating NUL. */
856 xAlloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
857 New(171, xbuf, xAlloc, char);
861 *(U32*)xbuf = collation_ix;
862 xout = sizeof(collation_ix);
863 for (xin = 0; xin < len; ) {
867 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
870 if (xused < xAlloc - xout)
872 xAlloc = (2 * xAlloc) + 1;
873 Renew(xbuf, xAlloc, char);
878 xin += strlen(s + xin) + 1;
881 /* Embedded NULs are understood but silently skipped
882 * because they make no sense in locale collation. */
886 *xlen = xout - sizeof(collation_ix);
895 #endif /* USE_LOCALE_COLLATE */
898 fbm_compile(SV *sv, U32 flags /* not used yet */)
900 register unsigned char *s;
901 register unsigned char *table;
903 register U32 len = SvCUR(sv);
907 sv_upgrade(sv, SVt_PVBM);
908 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
909 return; /* can't have offsets that big */
911 Sv_Grow(sv,len + 258);
912 table = (unsigned char*)(SvPVX(sv) + len + 1);
914 for (i = 0; i < 256; i++) {
918 while (s >= (unsigned char*)(SvPVX(sv)))
920 if (table[*s] == len)
925 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
928 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
929 for (i = 0; i < len; i++) {
930 if (freq[s[i]] < frequency) {
932 frequency = freq[s[i]];
935 BmRARE(sv) = s[rarest];
936 BmPREVIOUS(sv) = rarest;
937 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
941 fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
943 register unsigned char *s;
945 register I32 littlelen;
946 register unsigned char *little;
947 register unsigned char *table;
948 register unsigned char *olds;
949 register unsigned char *oldlittle;
951 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
953 char *l = SvPV(littlestr,len);
955 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
956 substr => we can ignore SvVALID */
959 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
964 if (bigend > big && bigend[-1] == '\n')
965 return (char *)(bigend - 1);
967 return (char *) bigend;
971 return ninstr((char*)big,(char*)bigend, l, l + len);
974 littlelen = SvCUR(littlestr);
975 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
976 if (littlelen > bigend - big)
978 little = (unsigned char*)SvPVX(littlestr);
979 s = bigend - littlelen;
981 && bigend[-1] == '\n'
982 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
983 return (char*)s - 1; /* how sweet it is */
984 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
985 return (char*)s; /* how sweet it is */
988 if (littlelen <= 2) {
989 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
990 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
991 /* This may do extra comparisons if littlelen == 2, but this
992 should be hidden in the noise since we do less indirection. */
996 while (s <= bigend) {
998 && (littlelen == 1 || s[1] == c2)
999 && (!SvTAIL(littlestr)
1001 || s[littlelen] == '\n')) /* Automatically multiline */
1009 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
1010 if (--littlelen >= bigend - big)
1012 s = big + littlelen;
1013 oldlittle = little = table - 2;
1017 if (tmp = table[*s]) {
1019 if (bigend - s > tmp) {
1024 if ((s += tmp) < bigend)
1030 tmp = littlelen; /* less expensive than calling strncmp() */
1033 if (*--s == *--little)
1036 s = olds + 1; /* here we pay the price for failure */
1038 if (s < bigend) /* fake up continue to outer loop */
1042 if (SvTAIL(littlestr) /* automatically multiline */
1043 && olds + 1 != bigend
1052 /* start_shift, end_shift are positive quantities which give offsets
1053 of ends of some substring of bigstr.
1054 If `last' we want the last occurence.
1055 old_posp is the way of communication between consequent calls if
1056 the next call needs to find the .
1057 The initial *old_posp should be -1.
1058 Note that we do not take into account SvTAIL, so it may give wrong
1059 positives if _ALL flag is set.
1063 screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
1065 register unsigned char *s, *x;
1066 register unsigned char *big;
1068 register I32 previous;
1070 register unsigned char *little;
1071 register I32 stop_pos;
1072 register unsigned char *littleend;
1076 ? (pos = screamfirst[BmRARE(littlestr)]) < 0
1077 : (((pos = *old_posp), pos += screamnext[pos]) == 0))
1079 little = (unsigned char *)(SvPVX(littlestr));
1080 littleend = little + SvCUR(littlestr);
1082 /* The value of pos we can start at: */
1083 previous = BmPREVIOUS(littlestr);
1084 big = (unsigned char *)(SvPVX(bigstr));
1085 /* The value of pos we can stop at: */
1086 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1087 if (previous + start_shift > stop_pos) return Nullch;
1088 while (pos < previous + start_shift) {
1089 if (!(pos += screamnext[pos]))
1094 if (pos >= stop_pos) return Nullch;
1095 if (big[pos-previous] != first)
1097 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1103 if (s == littleend) {
1105 if (!last) return (char *)(big+pos-previous);
1108 } while ( pos += screamnext[pos] );
1109 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
1110 #else /* !POINTERRIGOR */
1113 if (pos >= stop_pos) return Nullch;
1114 if (big[pos] != first)
1116 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1122 if (s == littleend) {
1124 if (!last) return (char *)(big+pos);
1127 } while ( pos += screamnext[pos] );
1128 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
1129 #endif /* POINTERRIGOR */
1133 ibcmp(char *s1, char *s2, register I32 len)
1135 register U8 *a = (U8 *)s1;
1136 register U8 *b = (U8 *)s2;
1138 if (*a != *b && *a != fold[*b])
1146 ibcmp_locale(char *s1, char *s2, register I32 len)
1148 register U8 *a = (U8 *)s1;
1149 register U8 *b = (U8 *)s2;
1151 if (*a != *b && *a != fold_locale[*b])
1158 /* copy a string to a safe spot */
1163 register char *newaddr;
1165 New(902,newaddr,strlen(sv)+1,char);
1166 (void)strcpy(newaddr,sv);
1170 /* same thing but with a known length */
1173 savepvn(char *sv, register I32 len)
1175 register char *newaddr;
1177 New(903,newaddr,len+1,char);
1178 Copy(sv,newaddr,len,char); /* might not be null terminated */
1179 newaddr[len] = '\0'; /* is now */
1183 /* the SV for form() and mess() is not kept in an arena */
1191 /* Create as PVMG now, to avoid any upgrading later */
1192 New(905, sv, 1, SV);
1193 Newz(905, any, 1, XPVMG);
1194 SvFLAGS(sv) = SVt_PVMG;
1195 SvANY(sv) = (void*)any;
1196 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1201 form(const char* pat, ...)
1204 va_start(args, pat);
1206 mess_sv = mess_alloc();
1207 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1209 return SvPVX(mess_sv);
1213 mess(const char *pat, va_list *args)
1216 static char dgd[] = " during global destruction.\n";
1219 mess_sv = mess_alloc();
1221 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1222 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1227 if (curcop->cop_line)
1228 sv_catpvf(sv, " at %_ line %ld",
1229 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1230 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1231 bool line_mode = (RsSIMPLE(rs) &&
1232 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1233 sv_catpvf(sv, ", <%s> %s %ld",
1234 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1235 line_mode ? "line" : "chunk",
1236 (long)IoLINES(GvIOp(last_in_gv)));
1238 sv_catpv(sv, ".\n");
1245 die(const char* pat, ...)
1250 int was_in_eval = in_eval;
1256 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1257 "%p: die: curstack = %p, mainstack = %p\n",
1258 thr, curstack, mainstack));
1259 #endif /* USE_THREADS */
1261 va_start(args, pat);
1262 message = pat ? mess(pat, &args) : Nullch;
1266 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1267 "%p: die: message = %s\ndiehook = %p\n",
1268 thr, message, diehook));
1269 #endif /* USE_THREADS */
1271 /* sv_2cv might call croak() */
1272 SV *olddiehook = diehook;
1276 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1278 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1284 msg = newSVpv(message, 0);
1292 PUSHSTACK(SI_DIEHOOK);
1296 perl_call_sv((SV*)cv, G_DISCARD);
1302 restartop = die_where(message);
1304 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1305 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1306 thr, restartop, was_in_eval, top_env));
1307 #endif /* USE_THREADS */
1308 if ((!restartop && was_in_eval) || top_env->je_prev)
1314 croak(const char* pat, ...)
1323 va_start(args, pat);
1324 message = mess(pat, &args);
1327 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1328 #endif /* USE_THREADS */
1330 /* sv_2cv might call croak() */
1331 SV *olddiehook = diehook;
1335 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1337 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1342 msg = newSVpv(message, 0);
1346 PUSHSTACK(SI_DIEHOOK);
1350 perl_call_sv((SV*)cv, G_DISCARD);
1356 restartop = die_where(message);
1359 PerlIO_puts(PerlIO_stderr(),message);
1360 (void)PerlIO_flush(PerlIO_stderr());
1365 warn(const char* pat,...)
1373 va_start(args, pat);
1374 message = mess(pat, &args);
1378 /* sv_2cv might call warn() */
1380 SV *oldwarnhook = warnhook;
1384 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1386 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1391 msg = newSVpv(message, 0);
1395 PUSHSTACK(SI_WARNHOOK);
1399 perl_call_sv((SV*)cv, G_DISCARD);
1405 PerlIO_puts(PerlIO_stderr(),message);
1407 DEBUG_L(*message == '!'
1408 ? (xstat(message[1]=='!'
1409 ? (message[2]=='!' ? 2 : 1)
1414 (void)PerlIO_flush(PerlIO_stderr());
1417 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1420 my_setenv(char *nam, char *val)
1422 register I32 i=setenv_getix(nam); /* where does it go? */
1424 if (environ == origenviron) { /* need we copy environment? */
1430 for (max = i; environ[max]; max++) ;
1431 New(901,tmpenv, max+2, char*);
1432 for (j=0; j<max; j++) /* copy environment */
1433 tmpenv[j] = savepv(environ[j]);
1434 tmpenv[max] = Nullch;
1435 environ = tmpenv; /* tell exec where it is now */
1438 Safefree(environ[i]);
1439 while (environ[i]) {
1440 environ[i] = environ[i+1];
1445 if (!environ[i]) { /* does not exist yet */
1446 Renew(environ, i+2, char*); /* just expand it a bit */
1447 environ[i+1] = Nullch; /* make sure it's null terminated */
1450 Safefree(environ[i]);
1451 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1453 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1455 /* MS-DOS requires environment variable names to be in uppercase */
1456 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1457 * some utilities and applications may break because they only look
1458 * for upper case strings. (Fixed strupr() bug here.)]
1460 strcpy(environ[i],nam); strupr(environ[i]);
1461 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1465 #else /* if WIN32 */
1468 my_setenv(char *nam,char *val)
1471 #ifdef USE_WIN32_RTL_ENV
1473 register char *envstr;
1474 STRLEN namlen = strlen(nam);
1476 char *oldstr = environ[setenv_getix(nam)];
1478 /* putenv() has totally broken semantics in both the Borland
1479 * and Microsoft CRTLs. They either store the passed pointer in
1480 * the environment without making a copy, or make a copy and don't
1481 * free it. And on top of that, they dont free() old entries that
1482 * are being replaced/deleted. This means the caller must
1483 * free any old entries somehow, or we end up with a memory
1484 * leak every time my_setenv() is called. One might think
1485 * one could directly manipulate environ[], like the UNIX code
1486 * above, but direct changes to environ are not allowed when
1487 * calling putenv(), since the RTLs maintain an internal
1488 * *copy* of environ[]. Bad, bad, *bad* stink.
1499 vallen = strlen(val);
1500 New(904, envstr, namlen + vallen + 3, char);
1501 (void)sprintf(envstr,"%s=%s",nam,val);
1502 (void)PerlEnv_putenv(envstr);
1506 Safefree(envstr); /* MSVCRT leaks without this */
1509 #else /* !USE_WIN32_RTL_ENV */
1511 /* The sane way to deal with the environment.
1512 * Has these advantages over putenv() & co.:
1513 * * enables us to store a truly empty value in the
1514 * environment (like in UNIX).
1515 * * we don't have to deal with RTL globals, bugs and leaks.
1517 * Why you may want to enable USE_WIN32_RTL_ENV:
1518 * * environ[] and RTL functions will not reflect changes,
1519 * which might be an issue if extensions want to access
1520 * the env. via RTL. This cuts both ways, since RTL will
1521 * not see changes made by extensions that call the Win32
1522 * functions directly, either.
1525 SetEnvironmentVariable(nam,val);
1533 setenv_getix(char *nam)
1535 register I32 i, len = strlen(nam);
1537 for (i = 0; environ[i]; i++) {
1540 strnicmp(environ[i],nam,len) == 0
1542 strnEQ(environ[i],nam,len)
1544 && environ[i][len] == '=')
1545 break; /* strnEQ must come first to avoid */
1546 } /* potential SEGV's */
1552 #ifdef UNLINK_ALL_VERSIONS
1554 unlnk(f) /* unlink all versions of a file */
1559 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
1564 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1566 my_bcopy(register char *from,register char *to,register I32 len)
1570 if (from - to >= 0) {
1578 *(--to) = *(--from);
1586 my_memset(loc,ch,len)
1599 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1613 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1615 my_memcmp(s1,s2,len)
1620 register U8 *a = (U8 *)s1;
1621 register U8 *b = (U8 *)s2;
1625 if (tmp = *a++ - *b++)
1630 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1634 #ifdef USE_CHAR_VSPRINTF
1639 vsprintf(dest, pat, args)
1646 fakebuf._ptr = dest;
1647 fakebuf._cnt = 32767;
1651 fakebuf._flag = _IOWRT|_IOSTRG;
1652 _doprnt(pat, args, &fakebuf); /* what a kludge */
1653 (void)putc('\0', &fakebuf);
1654 #ifdef USE_CHAR_VSPRINTF
1657 return 0; /* perl doesn't use return value */
1661 #endif /* HAS_VPRINTF */
1664 #if BYTEORDER != 0x4321
1668 #if (BYTEORDER & 1) == 0
1671 result = ((s & 255) << 8) + ((s >> 8) & 255);
1683 char c[sizeof(long)];
1686 #if BYTEORDER == 0x1234
1687 u.c[0] = (l >> 24) & 255;
1688 u.c[1] = (l >> 16) & 255;
1689 u.c[2] = (l >> 8) & 255;
1693 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1694 croak("Unknown BYTEORDER\n");
1699 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1700 u.c[o & 0xf] = (l >> s) & 255;
1712 char c[sizeof(long)];
1715 #if BYTEORDER == 0x1234
1716 u.c[0] = (l >> 24) & 255;
1717 u.c[1] = (l >> 16) & 255;
1718 u.c[2] = (l >> 8) & 255;
1722 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1723 croak("Unknown BYTEORDER\n");
1730 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1731 l |= (u.c[o & 0xf] & 255) << s;
1738 #endif /* BYTEORDER != 0x4321 */
1742 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1743 * If these functions are defined,
1744 * the BYTEORDER is neither 0x1234 nor 0x4321.
1745 * However, this is not assumed.
1749 #define HTOV(name,type) \
1756 char c[sizeof(type)]; \
1760 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1761 u.c[i] = (n >> s) & 0xFF; \
1766 #define VTOH(name,type) \
1773 char c[sizeof(type)]; \
1779 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1780 n += (u.c[i] & 0xFF) << s; \
1785 #if defined(HAS_HTOVS) && !defined(htovs)
1788 #if defined(HAS_HTOVL) && !defined(htovl)
1791 #if defined(HAS_VTOHS) && !defined(vtohs)
1794 #if defined(HAS_VTOHL) && !defined(vtohl)
1798 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1799 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1801 my_popen(char *cmd, char *mode)
1804 register I32 This, that;
1807 I32 doexec = strNE(cmd,"-");
1811 return my_syspopen(cmd,mode);
1814 This = (*mode == 'w');
1816 if (doexec && tainting) {
1818 taint_proper("Insecure %s%s", "EXEC");
1820 if (PerlProc_pipe(p) < 0)
1822 while ((pid = (doexec?vfork():fork())) < 0) {
1823 if (errno != EAGAIN) {
1824 PerlLIO_close(p[This]);
1826 croak("Can't fork");
1838 PerlLIO_close(p[THAT]);
1839 if (p[THIS] != (*mode == 'r')) {
1840 PerlLIO_dup2(p[THIS], *mode == 'r');
1841 PerlLIO_close(p[THIS]);
1844 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1850 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1853 do_exec(cmd); /* may or may not use the shell */
1857 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1858 sv_setiv(GvSV(tmpgv), (IV)getpid());
1860 hv_clear(pidstatus); /* we have no children */
1865 do_execfree(); /* free any memory malloced by child on vfork */
1866 PerlLIO_close(p[that]);
1867 if (p[that] < p[This]) {
1868 PerlLIO_dup2(p[This], p[that]);
1869 PerlLIO_close(p[This]);
1872 sv = *av_fetch(fdpid,p[This],TRUE);
1873 (void)SvUPGRADE(sv,SVt_IV);
1876 return PerlIO_fdopen(p[This], mode);
1879 #if defined(atarist) || defined(DJGPP)
1886 /* Needs work for PerlIO ! */
1887 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1888 return popen(PerlIO_exportFILE(cmd, 0), mode);
1892 #endif /* !DOSISH */
1899 struct stat tmpstatbuf;
1901 PerlIO_printf(PerlIO_stderr(),"%s", s);
1902 for (fd = 0; fd < 32; fd++) {
1903 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
1904 PerlIO_printf(PerlIO_stderr()," %d",fd);
1906 PerlIO_printf(PerlIO_stderr(),"\n");
1908 #endif /* DUMP_FDS */
1916 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1919 PerlLIO_close(newfd);
1920 return fcntl(oldfd, F_DUPFD, newfd);
1922 #define DUP2_MAX_FDS 256
1923 int fdtmp[DUP2_MAX_FDS];
1929 PerlLIO_close(newfd);
1930 /* good enough for low fd's... */
1931 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
1932 if (fdx >= DUP2_MAX_FDS) {
1940 PerlLIO_close(fdtmp[--fdx]);
1947 #ifdef HAS_SIGACTION
1950 rsignal(int signo, Sighandler_t handler)
1952 struct sigaction act, oact;
1954 act.sa_handler = handler;
1955 sigemptyset(&act.sa_mask);
1958 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1960 if (sigaction(signo, &act, &oact) == -1)
1963 return oact.sa_handler;
1967 rsignal_state(int signo)
1969 struct sigaction oact;
1971 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1974 return oact.sa_handler;
1978 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
1980 struct sigaction act;
1982 act.sa_handler = handler;
1983 sigemptyset(&act.sa_mask);
1986 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1988 return sigaction(signo, &act, save);
1992 rsignal_restore(int signo, Sigsave_t *save)
1994 return sigaction(signo, save, (struct sigaction *)NULL);
1997 #else /* !HAS_SIGACTION */
2000 rsignal(int signo, Sighandler_t handler)
2002 return PerlProc_signal(signo, handler);
2005 static int sig_trapped;
2015 rsignal_state(int signo)
2017 Sighandler_t oldsig;
2020 oldsig = PerlProc_signal(signo, sig_trap);
2021 PerlProc_signal(signo, oldsig);
2023 PerlProc_kill(getpid(), signo);
2028 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
2030 *save = PerlProc_signal(signo, handler);
2031 return (*save == SIG_ERR) ? -1 : 0;
2035 rsignal_restore(int signo, Sigsave_t *save)
2037 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
2040 #endif /* !HAS_SIGACTION */
2042 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2043 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
2045 my_pclose(PerlIO *ptr)
2047 Sigsave_t hstat, istat, qstat;
2055 int saved_vaxc_errno;
2058 int saved_win32_errno;
2061 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
2062 pid = (int)SvIVX(*svp);
2066 if (pid == -1) { /* Opened by popen. */
2067 return my_syspclose(ptr);
2070 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2071 saved_errno = errno;
2073 saved_vaxc_errno = vaxc$errno;
2076 saved_win32_errno = GetLastError();
2080 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2082 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2083 rsignal_save(SIGINT, SIG_IGN, &istat);
2084 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2086 pid2 = wait4pid(pid, &status, 0);
2087 } while (pid2 == -1 && errno == EINTR);
2088 rsignal_restore(SIGHUP, &hstat);
2089 rsignal_restore(SIGINT, &istat);
2090 rsignal_restore(SIGQUIT, &qstat);
2092 SETERRNO(saved_errno, saved_vaxc_errno);
2095 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
2097 #endif /* !DOSISH */
2099 #if !defined(DOSISH) || defined(OS2) || defined(WIN32)
2101 wait4pid(int pid, int *statusp, int flags)
2105 char spid[TYPE_CHARS(int)];
2110 sprintf(spid, "%d", pid);
2111 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2112 if (svp && *svp != &sv_undef) {
2113 *statusp = SvIVX(*svp);
2114 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2121 hv_iterinit(pidstatus);
2122 if (entry = hv_iternext(pidstatus)) {
2123 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2124 sv = hv_iterval(pidstatus,entry);
2125 *statusp = SvIVX(sv);
2126 sprintf(spid, "%d", pid);
2127 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2132 # ifdef HAS_WAITPID_RUNTIME
2133 if (!HAS_WAITPID_RUNTIME)
2136 return PerlProc_waitpid(pid,statusp,flags);
2138 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2139 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2141 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2146 croak("Can't do waitpid with flags");
2148 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
2149 pidgone(result,*statusp);
2157 #endif /* !DOSISH || OS2 || WIN32 */
2161 pidgone(int pid, int status)
2164 char spid[TYPE_CHARS(int)];
2166 sprintf(spid, "%d", pid);
2167 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2168 (void)SvUPGRADE(sv,SVt_IV);
2173 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2176 int /* Cannot prototype with I32
2185 /* Needs work for PerlIO ! */
2186 FILE *f = PerlIO_findFILE(ptr);
2187 I32 result = pclose(f);
2188 PerlIO_releaseFILE(ptr,f);
2194 repeatcpy(register char *to, register char *from, I32 len, register I32 count)
2197 register char *frombase = from;
2205 while (count-- > 0) {
2206 for (todo = len; todo > 0; todo--) {
2213 #ifndef CASTNEGFLOAT
2221 # define BIGDOUBLE 2147483648.0
2223 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2226 return (unsigned long)f;
2228 return (unsigned long)along;
2235 /* Unfortunately, on some systems the cast_uv() function doesn't
2236 work with the system-supplied definition of ULONG_MAX. The
2237 comparison (f >= ULONG_MAX) always comes out true. It must be a
2238 problem with the compiler constant folding.
2240 In any case, this workaround should be fine on any two's complement
2241 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2243 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2246 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2248 -- Kenneth Albanowski <kjahds@kjahds.com>
2252 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2260 return (I32) I32_MAX;
2262 return (I32) I32_MIN;
2282 return (UV) MY_UV_MAX;
2294 char *fa = strrchr(a,'/');
2295 char *fb = strrchr(b,'/');
2296 struct stat tmpstatbuf1;
2297 struct stat tmpstatbuf2;
2298 SV *tmpsv = sv_newmortal();
2311 sv_setpv(tmpsv, ".");
2313 sv_setpvn(tmpsv, a, fa - a);
2314 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2317 sv_setpv(tmpsv, ".");
2319 sv_setpvn(tmpsv, b, fb - b);
2320 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2322 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2323 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2325 #endif /* !HAS_RENAME */
2328 scan_oct(char *start, I32 len, I32 *retlen)
2330 register char *s = start;
2331 register UV retval = 0;
2332 bool overflowed = FALSE;
2334 while (len && *s >= '0' && *s <= '7') {
2335 register UV n = retval << 3;
2336 if (!overflowed && (n >> 3) != retval) {
2337 warn("Integer overflow in octal number");
2340 retval = n | (*s++ - '0');
2343 if (dowarn && len && (*s == '8' || *s == '9'))
2344 warn("Illegal octal digit ignored");
2345 *retlen = s - start;
2350 scan_hex(char *start, I32 len, I32 *retlen)
2352 register char *s = start;
2353 register UV retval = 0;
2354 bool overflowed = FALSE;
2357 while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
2358 register UV n = retval << 4;
2359 if (!overflowed && (n >> 4) != retval) {
2360 warn("Integer overflow in hex number");
2363 retval = n | ((tmp - hexdigit) & 15);
2366 if (dowarn && !tmp) {
2367 warn("Illegal hex digit ignored");
2369 *retlen = s - start;
2374 find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
2377 char *xfound = Nullch;
2378 char *xfailed = Nullch;
2383 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2384 # define SEARCH_EXTS ".bat", ".cmd", NULL
2385 # define MAX_EXT_LEN 4
2388 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2389 # define MAX_EXT_LEN 4
2392 # define SEARCH_EXTS ".pl", ".com", NULL
2393 # define MAX_EXT_LEN 4
2395 /* additional extensions to try in each dir if scriptname not found */
2397 char *exts[] = { SEARCH_EXTS };
2398 char **ext = search_ext ? search_ext : exts;
2399 int extidx = 0, i = 0;
2400 char *curext = Nullch;
2402 # define MAX_EXT_LEN 0
2406 * If dosearch is true and if scriptname does not contain path
2407 * delimiters, search the PATH for scriptname.
2409 * If SEARCH_EXTS is also defined, will look for each
2410 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2411 * while searching the PATH.
2413 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2414 * proceeds as follows:
2415 * If DOSISH or VMSISH:
2416 * + look for ./scriptname{,.foo,.bar}
2417 * + search the PATH for scriptname{,.foo,.bar}
2420 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2421 * this will not look in '.' if it's not in the PATH)
2426 # ifdef ALWAYS_DEFTYPES
2427 len = strlen(scriptname);
2428 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
2429 int hasdir, idx = 0, deftypes = 1;
2432 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
2435 int hasdir, idx = 0, deftypes = 1;
2438 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
2440 /* The first time through, just add SEARCH_EXTS to whatever we
2441 * already have, so we can check for default file types. */
2443 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
2449 if ((strlen(tmpbuf) + strlen(scriptname)
2450 + MAX_EXT_LEN) >= sizeof tmpbuf)
2451 continue; /* don't search dir with too-long name */
2452 strcat(tmpbuf, scriptname);
2456 if (strEQ(scriptname, "-"))
2458 if (dosearch) { /* Look in '.' first. */
2459 char *cur = scriptname;
2461 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2463 if (strEQ(ext[i++],curext)) {
2464 extidx = -1; /* already has an ext */
2469 DEBUG_p(PerlIO_printf(Perl_debug_log,
2470 "Looking for %s\n",cur));
2471 if (PerlLIO_stat(cur,&statbuf) >= 0) {
2479 if (cur == scriptname) {
2480 len = strlen(scriptname);
2481 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
2483 cur = strcpy(tmpbuf, scriptname);
2485 } while (extidx >= 0 && ext[extidx] /* try an extension? */
2486 && strcpy(tmpbuf+len, ext[extidx++]));
2491 if (dosearch && !strchr(scriptname, '/')
2493 && !strchr(scriptname, '\\')
2495 && (s = PerlEnv_getenv("PATH"))) {
2498 bufend = s + strlen(s);
2499 while (s < bufend) {
2500 #if defined(atarist) || defined(DOSISH)
2505 && *s != ';'; len++, s++) {
2506 if (len < sizeof tmpbuf)
2509 if (len < sizeof tmpbuf)
2511 #else /* ! (atarist || DOSISH) */
2512 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend,
2515 #endif /* ! (atarist || DOSISH) */
2518 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
2519 continue; /* don't search dir with too-long name */
2521 #if defined(atarist) || defined(DOSISH)
2522 && tmpbuf[len - 1] != '/'
2523 && tmpbuf[len - 1] != '\\'
2526 tmpbuf[len++] = '/';
2527 if (len == 2 && tmpbuf[0] == '.')
2529 (void)strcpy(tmpbuf + len, scriptname);
2533 len = strlen(tmpbuf);
2534 if (extidx > 0) /* reset after previous loop */
2538 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
2539 retval = PerlLIO_stat(tmpbuf,&statbuf);
2541 } while ( retval < 0 /* not there */
2542 && extidx>=0 && ext[extidx] /* try an extension? */
2543 && strcpy(tmpbuf+len, ext[extidx++])
2548 if (S_ISREG(statbuf.st_mode)
2549 && cando(S_IRUSR,TRUE,&statbuf)
2551 && cando(S_IXUSR,TRUE,&statbuf)
2555 xfound = tmpbuf; /* bingo! */
2559 xfailed = savepv(tmpbuf);
2562 if (!xfound && !seen_dot && !xfailed && (PerlLIO_stat(scriptname,&statbuf) < 0))
2564 seen_dot = 1; /* Disable message. */
2566 if (flags & 1) { /* do or die? */
2567 croak("Can't %s %s%s%s",
2568 (xfailed ? "execute" : "find"),
2569 (xfailed ? xfailed : scriptname),
2570 (xfailed ? "" : " on PATH"),
2571 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
2573 scriptname = Nullch;
2577 scriptname = xfound;
2579 return (scriptname ? savepv(scriptname) : Nullch);
2585 /* Very simplistic scheduler for now */
2589 thr = thr->i.next_run;
2600 perl_cond_signal(cp)
2604 perl_cond cond = *cp;
2609 /* Insert t in the runnable queue just ahead of us */
2610 t->i.next_run = thr->i.next_run;
2611 thr->i.next_run->i.prev_run = t;
2612 t->i.prev_run = thr;
2613 thr->i.next_run = t;
2614 thr->i.wait_queue = 0;
2615 /* Remove from the wait queue */
2621 perl_cond_broadcast(cp)
2625 perl_cond cond, cond_next;
2627 for (cond = *cp; cond; cond = cond_next) {
2629 /* Insert t in the runnable queue just ahead of us */
2630 t->i.next_run = thr->i.next_run;
2631 thr->i.next_run->i.prev_run = t;
2632 t->i.prev_run = thr;
2633 thr->i.next_run = t;
2634 thr->i.wait_queue = 0;
2635 /* Remove from the wait queue */
2636 cond_next = cond->next;
2648 if (thr->i.next_run == thr)
2649 croak("panic: perl_cond_wait called by last runnable thread");
2651 New(666, cond, 1, struct perl_wait_queue);
2655 thr->i.wait_queue = cond;
2656 /* Remove ourselves from runnable queue */
2657 thr->i.next_run->i.prev_run = thr->i.prev_run;
2658 thr->i.prev_run->i.next_run = thr->i.next_run;
2660 #endif /* FAKE_THREADS */
2662 #ifdef OLD_PTHREADS_API
2663 struct perl_thread *
2668 if (pthread_getspecific(thr_key, &t))
2669 croak("panic: pthread_getspecific");
2670 return (struct perl_thread *) t;
2672 #endif /* OLD_PTHREADS_API */
2675 condpair_magic(SV *sv)
2679 SvUPGRADE(sv, SVt_PVMG);
2680 mg = mg_find(sv, 'm');
2684 New(53, cp, 1, condpair_t);
2685 MUTEX_INIT(&cp->mutex);
2686 COND_INIT(&cp->owner_cond);
2687 COND_INIT(&cp->cond);
2690 mg = mg_find(sv, 'm');
2692 /* someone else beat us to initialising it */
2694 MUTEX_DESTROY(&cp->mutex);
2695 COND_DESTROY(&cp->owner_cond);
2696 COND_DESTROY(&cp->cond);
2700 sv_magic(sv, Nullsv, 'm', 0, 0);
2702 mg->mg_ptr = (char *)cp;
2703 mg->mg_len = sizeof(cp);
2705 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2706 "%p: condpair_magic %p\n", thr, sv));)
2713 * Make a new perl thread structure using t as a prototype. Some of the
2714 * fields for the new thread are copied from the prototype thread, t,
2715 * so t should not be running in perl at the time this function is
2716 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2717 * thread calling new_struct_thread) clearly satisfies this constraint.
2719 struct perl_thread *
2720 new_struct_thread(struct perl_thread *t)
2722 struct perl_thread *thr;
2727 sv = newSVpv("", 0);
2728 SvGROW(sv, sizeof(struct perl_thread) + 1);
2729 SvCUR_set(sv, sizeof(struct perl_thread));
2730 thr = (Thread) SvPVX(sv);
2732 memset(thr, 0xab, sizeof(struct perl_thread));
2744 curcop = &compiling;
2745 thr->cvcache = newHV();
2746 thr->threadsv = newAV();
2747 thr->specific = newAV();
2748 thr->errsv = newSVpv("", 0);
2749 thr->errhv = newHV();
2750 thr->flags = THRf_R_JOINABLE;
2751 MUTEX_INIT(&thr->mutex);
2753 curcop = t->Tcurcop; /* XXX As good a guess as any? */
2754 defstash = t->Tdefstash; /* XXX maybe these should */
2755 curstash = t->Tcurstash; /* always be set to main? */
2758 /* top_env needs to be non-zero. It points to an area
2759 in which longjmp() stuff is stored, as C callstack
2760 info there at least is thread specific this has to
2761 be per-thread. Otherwise a 'die' in a thread gives
2762 that thread the C stack of last thread to do an eval {}!
2763 See comments in scope.h
2764 Initialize top entry (as in perl.c for main thread)
2766 start_env.je_prev = NULL;
2767 start_env.je_ret = -1;
2768 start_env.je_mustcatch = TRUE;
2769 top_env = &start_env;
2774 tainted = t->Ttainted;
2775 curpm = t->Tcurpm; /* XXX No PMOP ref count */
2776 nrs = newSVsv(t->Tnrs);
2777 rs = newSVsv(t->Trs);
2778 last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
2779 ofslen = t->Tofslen;
2780 ofs = savepvn(t->Tofs, ofslen);
2781 defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2782 chopset = t->Tchopset;
2783 formtarget = newSVsv(t->Tformtarget);
2784 bodytarget = newSVsv(t->Tbodytarget);
2785 toptarget = newSVsv(t->Ttoptarget);
2787 /* Initialise all per-thread SVs that the template thread used */
2788 svp = AvARRAY(t->threadsv);
2789 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
2790 if (*svp && *svp != &sv_undef) {
2791 SV *sv = newSVsv(*svp);
2792 av_store(thr->threadsv, i, sv);
2793 sv_magic(sv, 0, 0, &threadsv_names[i], 1);
2794 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2795 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
2798 thr->threadsvp = AvARRAY(thr->threadsv);
2800 MUTEX_LOCK(&threads_mutex);
2802 thr->tid = ++threadnum;
2803 thr->next = t->next;
2806 thr->next->prev = thr;
2807 MUTEX_UNLOCK(&threads_mutex);
2809 #ifdef HAVE_THREAD_INTERN
2810 init_thread_intern(thr);
2811 #endif /* HAVE_THREAD_INTERN */
2814 #endif /* USE_THREADS */
2818 * This hack is to force load of "huge" support from libm.a
2819 * So it is in perl for (say) POSIX to use.
2820 * Needed for SunOS with Sun's 'acc' for example.
2829 #ifdef PERL_GLOBAL_STRUCT
2852 return (char*)no_modify;
2863 get_specialsv_list(void)
2865 return specialsv_list;