3 * Copyright (c) 1991-1994, 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)
22 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
31 /* Put this after #includes because fork and vfork prototypes may
42 # include <sys/file.h>
48 static void xstat _((void));
53 /* paranoid version of malloc */
55 /* NOTE: Do not call the next three routines directly. Use the macros
56 * in handy.h, so that we can easily redefine everything to do tracking of
57 * allocated hunks back to the original New to track down any memory leaks.
58 * XXX This advice seems to be widely ignored :-( --AD August 1996.
68 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
71 #endif /* HAS_64K_LIMIT */
74 croak("panic: malloc");
76 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
77 #if !(defined(I286) || defined(atarist))
78 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
80 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
87 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
93 /* paranoid version of realloc */
96 saferealloc(where,size)
101 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
103 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
107 PerlIO_printf(PerlIO_stderr(), "Reallocation too large: %lx\n", size) FLUSH;
110 #endif /* HAS_64K_LIMIT */
112 croak("Null realloc");
115 croak("panic: realloc");
117 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
119 #if !(defined(I286) || defined(atarist))
121 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
122 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
126 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
127 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
136 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
142 /* safe version of free */
148 #if !(defined(I286) || defined(atarist))
149 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",where,an++));
151 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",where,an++));
159 /* safe version of calloc */
162 safecalloc(count, size)
169 if (size * count > 0xffff) {
170 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size * count) FLUSH;
173 #endif /* HAS_64K_LIMIT */
175 if ((long)size < 0 || (long)count < 0)
176 croak("panic: calloc");
178 #if !(defined(I286) || defined(atarist))
179 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
181 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
184 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
186 memset((void*)ptr, 0, size);
192 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
198 #endif /* !MYMALLOC */
202 #define ALIGN sizeof(long)
209 register Malloc_t where;
211 where = safemalloc(size + ALIGN);
215 return where + ALIGN;
219 safexrealloc(where,size)
223 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
236 x = where[0] + 100 * where[1];
242 safexcalloc(x,count,size)
247 register Malloc_t where;
249 where = safexmalloc(x, size * count + ALIGN);
251 memset((void*)where + ALIGN, 0, size * count);
254 return where + ALIGN;
262 for (i = 0; i < MAXXCOUNT; i++) {
263 if (xcount[i] > lastxcount[i]) {
264 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
265 lastxcount[i] = xcount[i];
270 #endif /* LEAKTEST */
272 /* copy a string up to some (non-backslashed) delimiter, if any */
275 cpytill(to,from,fromend,delim,retlen)
278 register char *fromend;
284 for (; from < fromend; from++,to++) {
286 if (from[1] == delim)
288 else if (from[1] == '\\')
291 else if (*from == delim)
296 *retlen = to - origto;
300 /* return ptr to little string in big string, NULL if not found */
301 /* This routine was donated by Corey Satten. */
306 register char *little;
308 register char *s, *x;
319 for (x=big,s=little; *s; /**/ ) {
333 /* same as instr but allow embedded nulls */
336 ninstr(big, bigend, little, lend)
338 register char *bigend;
342 register char *s, *x;
343 register I32 first = *little;
344 register char *littleend = lend;
346 if (!first && little >= littleend)
348 if (bigend - big < littleend - little)
350 bigend -= littleend - little++;
351 while (big <= bigend) {
354 for (x=big,s=little; s < littleend; /**/ ) {
366 /* reverse of the above--find last substring */
369 rninstr(big, bigend, little, lend)
375 register char *bigbeg;
376 register char *s, *x;
377 register I32 first = *little;
378 register char *littleend = lend;
380 if (!first && little >= littleend)
383 big = bigend - (littleend - little++);
384 while (big >= bigbeg) {
387 for (x=big+2,s=little; s < littleend; /**/ ) {
399 /* Initialize the fold[] array. */
405 for (i = 0; i < 256; i++) {
406 if (isUPPER(i)) fold[i] = toLOWER(i);
407 else if (isLOWER(i)) fold[i] = toUPPER(i);
412 /* Initialize locale (and the fold[] array).*/
414 perl_init_i18nl10n(printwarn)
419 * 1 = set ok or not applicable,
420 * 0 = fallback to C locale,
421 * -1 = fallback to C locale failed
423 #if defined(HAS_SETLOCALE)
424 char * lc_all = getenv("LC_ALL");
425 char * lc_ctype = getenv("LC_CTYPE");
426 char * lc_collate = getenv("LC_COLLATE");
427 char * lang = getenv("LANG");
428 int setlocale_failure = 0;
430 #define SETLOCALE_LC_CTYPE 0x01
431 #define SETLOCALE_LC_COLLATE 0x02
434 if (setlocale(LC_CTYPE, "") == 0)
435 setlocale_failure |= SETLOCALE_LC_CTYPE;
439 if (setlocale(LC_COLLATE, "") == 0)
440 setlocale_failure |= SETLOCALE_LC_COLLATE;
442 lc_collate_active = 1;
445 if (setlocale_failure && (lc_all || lang)) {
450 (!(perl_badlang = getenv("PERL_BADLANG")) || atoi(perl_badlang))) {
452 PerlIO_printf(PerlIO_stderr(),
453 "perl: warning: Setting locale failed for the categories:\n\t");
455 if (setlocale_failure & SETLOCALE_LC_CTYPE)
456 PerlIO_printf(PerlIO_stderr(),
460 if (setlocale_failure & SETLOCALE_LC_COLLATE)
461 PerlIO_printf(PerlIO_stderr(),
464 PerlIO_printf(PerlIO_stderr(),
467 PerlIO_printf(PerlIO_stderr(),
468 "perl: warning: Please check that your locale settings:\n");
470 PerlIO_printf(PerlIO_stderr(),
471 "\tLC_ALL = %c%s%c,\n",
473 lc_all ? lc_all : "unset",
477 if (setlocale_failure & SETLOCALE_LC_CTYPE)
478 PerlIO_printf(PerlIO_stderr(),
479 "\tLC_CTYPE = %c%s%c,\n",
480 lc_ctype ? '"' : '(',
481 lc_ctype ? lc_ctype : "unset",
486 if (setlocale_failure & SETLOCALE_LC_COLLATE)
487 PerlIO_printf(PerlIO_stderr(),
488 "\tLC_COLLATE = %c%s%c,\n",
489 lc_collate ? '"' : '(',
490 lc_collate ? lc_collate : "unset",
491 lc_collate ? '"' : ')'
494 PerlIO_printf(PerlIO_stderr(),
497 lang ? lang : "unset",
501 PerlIO_printf(PerlIO_stderr(),
502 " are supported and installed on your system.\n");
508 if (setlocale_failure) {
509 PerlIO_printf(PerlIO_stderr(),
510 "perl: warning: Falling back to the \"C\" locale.\n");
511 if (setlocale(LC_ALL, "C") == NULL) {
513 PerlIO_printf(PerlIO_stderr(),
514 "perl: warning: Failed to fall back to the \"C\" locale.\n");
518 PerlIO_printf(PerlIO_stderr(),
519 "perl: warning: Cannot fall back to the \"C\" locale.\n");
523 if (setlocale_failure & SETLOCALE_LC_CTYPE == 0)
526 #endif /* #if defined(HAS_SETLOCALE) */
532 mem_collxfrm(m, n, nx) /* mem_collxfrm() does strxfrm() for (data,size) */
533 const char *m; /* "strings", that is, transforms normal eight-bit */
534 const Size_t n; /* data into a format that can be memcmp()ed to get */
535 Size_t * nx; /* 'the right' result for each locale. */
536 { /* Uses strxfrm() but handles embedded NULs. */
542 /* the expansion factor of 16 has been seen with strxfrm() */
543 ma = (lc_collate_active ? 16 : 1) * n + 1;
548 Renew(mx, ma, char); \
553 New(171, mx, ma, char);
559 for (*nx = 0, xc = 0; xc < n; ) {
565 } while (xc < n && m[xc] == 0);
568 dx = strxfrm(mx + *nx, m + xc, ma - *nx);
575 xc += strlen(mx + *nx);
583 #endif /* HAS_STRXFRM */
589 fbm_compile(sv, iflag)
593 register unsigned char *s;
594 register unsigned char *table;
596 register U32 len = SvCUR(sv);
601 return; /* can't have offsets that big */
603 table = (unsigned char*)(SvPVX(sv) + len + 1);
605 for (i = 0; i < 256; i++) {
609 while (s >= (unsigned char*)(SvPVX(sv)))
611 if (table[*s] == len) {
614 table[*s] = table[fold[*s]] = i;
628 sv_upgrade(sv, SVt_PVBM);
629 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
632 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
634 register U32 tmp, foldtmp;
636 for (i = 0; i < len; i++) {
638 foldtmp=freq[fold[s[i]]];
639 if (tmp < frequency && foldtmp < frequency) {
641 /* choose most frequent among the two */
642 frequency = (tmp > foldtmp) ? tmp : foldtmp;
647 for (i = 0; i < len; i++) {
648 if (freq[s[i]] < frequency) {
650 frequency = freq[s[i]];
654 BmRARE(sv) = s[rarest];
655 BmPREVIOUS(sv) = rarest;
656 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
660 fbm_instr(big, bigend, littlestr)
662 register unsigned char *bigend;
665 register unsigned char *s;
667 register I32 littlelen;
668 register unsigned char *little;
669 register unsigned char *table;
670 register unsigned char *olds;
671 register unsigned char *oldlittle;
673 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
675 char *l = SvPV(littlestr,len);
678 return ninstr((char*)big,(char*)bigend, l, l + len);
681 littlelen = SvCUR(littlestr);
682 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
683 if (littlelen > bigend - big)
685 little = (unsigned char*)SvPVX(littlestr);
686 if (SvCASEFOLD(littlestr)) { /* oops, fake it */
687 big = bigend - littlelen; /* just start near end */
688 if (bigend[-1] == '\n' && little[littlelen-1] != '\n')
692 s = bigend - littlelen;
693 if (*s == *little && memcmp((char*)s,(char*)little,littlelen)==0)
694 return (char*)s; /* how sweet it is */
695 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
698 if (*s == *little && memcmp((char*)s,(char*)little,littlelen)==0)
704 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
705 if (--littlelen >= bigend - big)
708 oldlittle = little = table - 2;
709 if (SvCASEFOLD(littlestr)) { /* case insensitive? */
713 if (tmp = table[*s]) {
715 if (bigend - s > tmp) {
720 if ((s += tmp) < bigend)
726 tmp = littlelen; /* less expensive than calling strncmp() */
729 if (*--s == *--little || fold[*s] == *little)
731 s = olds + 1; /* here we pay the price for failure */
733 if (s < bigend) /* fake up continue to outer loop */
745 if (tmp = table[*s]) {
747 if (bigend - s > tmp) {
752 if ((s += tmp) < bigend)
758 tmp = littlelen; /* less expensive than calling strncmp() */
761 if (*--s == *--little)
763 s = olds + 1; /* here we pay the price for failure */
765 if (s < bigend) /* fake up continue to outer loop */
777 screaminstr(bigstr, littlestr)
781 register unsigned char *s, *x;
782 register unsigned char *big;
784 register I32 previous;
786 register unsigned char *little;
787 register unsigned char *bigend;
788 register unsigned char *littleend;
790 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
792 little = (unsigned char *)(SvPVX(littlestr));
793 littleend = little + SvCUR(littlestr);
795 previous = BmPREVIOUS(littlestr);
796 big = (unsigned char *)(SvPVX(bigstr));
797 bigend = big + SvCUR(bigstr);
798 while (pos < previous) {
799 if (!(pos += screamnext[pos]))
803 if (SvCASEFOLD(littlestr)) { /* case insignificant? */
805 if (big[pos-previous] != first && big[pos-previous] != fold[first])
807 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
810 if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
816 return (char *)(big+pos-previous);
818 pos += screamnext[pos] /* does this goof up anywhere? */
823 if (big[pos-previous] != first)
825 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
834 return (char *)(big+pos-previous);
835 } while ( pos += screamnext[pos] );
837 #else /* !POINTERRIGOR */
839 if (SvCASEFOLD(littlestr)) { /* case insignificant? */
841 if (big[pos] != first && big[pos] != fold[first])
843 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
846 if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
852 return (char *)(big+pos);
854 pos += screamnext[pos] /* does this goof up anywhere? */
859 if (big[pos] != first)
861 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
870 return (char *)(big+pos);
872 pos += screamnext[pos]
875 #endif /* POINTERRIGOR */
890 if (fold[*a++] == *b++)
897 /* copy a string to a safe spot */
903 register char *newaddr;
905 New(902,newaddr,strlen(sv)+1,char);
906 (void)strcpy(newaddr,sv);
910 /* same thing but with a known length */
917 register char *newaddr;
919 New(903,newaddr,len+1,char);
920 Copy(sv,newaddr,len,char); /* might not be null terminated */
921 newaddr[len] = '\0'; /* is now */
925 #if !defined(I_STDARG) && !defined(I_VARARGS)
928 * Fallback on the old hackers way of doing varargs
933 mess(pat,a1,a2,a3,a4)
939 I32 usermess = strEQ(pat,"%s");
944 tmpstr = sv_newmortal();
945 sv_setpv(tmpstr, (char*)a1);
946 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
949 (void)sprintf(s,pat,a1,a2,a3,a4);
955 strcpy(s, " during global destruction.\n");
957 if (curcop->cop_line) {
958 (void)sprintf(s," at %s line %ld",
959 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
962 if (GvIO(last_in_gv) &&
963 IoLINES(GvIOp(last_in_gv)) ) {
964 (void)sprintf(s,", <%s> %s %ld",
965 last_in_gv == argvgv ? "" : GvENAME(last_in_gv),
966 strEQ(rs,"\n") ? "line" : "chunk",
967 (long)IoLINES(GvIOp(last_in_gv)));
970 (void)strcpy(s,".\n");
974 sv_catpv(tmpstr,buf+1);
977 if (s - s_start >= sizeof(buf)) { /* Ooops! */
979 PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr));
981 PerlIO_puts(PerlIO_stderr(), buf);
982 PerlIO_puts(PerlIO_stderr(),"panic: message overflow - memory corrupted!\n");
986 return SvPVX(tmpstr);
992 void croak(pat,a1,a2,a3,a4)
1002 message = mess(pat,a1,a2,a3,a4);
1004 SV *olddiehook = diehook;
1005 diehook = Nullsv; /* sv_2cv might call croak() */
1006 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1007 diehook = olddiehook;
1008 if (cv && !CvDEPTH(cv)) {
1013 PUSHs(sv_2mortal(newSVpv(message,0)));
1015 perl_call_sv((SV*)cv, G_DISCARD);
1019 restartop = die_where(message);
1020 Siglongjmp(top_env, 3);
1022 PerlIO_puts(PerlIO_stderr(),message);
1023 (void)PerlIO_flush(PerlIO_stderr());
1029 (void)UNLINK(e_tmpname);
1030 Safefree(e_tmpname);
1033 statusvalue = SHIFTSTATUS(statusvalue);
1035 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
1037 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1042 void warn(pat,a1,a2,a3,a4)
1044 long a1, a2, a3, a4;
1052 message = mess(pat,a1,a2,a3,a4);
1054 SV *oldwarnhook = warnhook;
1055 warnhook = Nullsv; /* sv_2cv might end up calling warn() */
1056 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1057 warnhook = oldwarnhook;
1058 if (cv && !CvDEPTH(cv)) {
1063 PUSHs(sv_2mortal(newSVpv(message,0)));
1065 perl_call_sv((SV*)cv, G_DISCARD);
1069 PerlIO_puts(PerlIO_stderr(),message);
1073 (void)PerlIO_flush(PerlIO_stderr());
1076 #else /* !defined(I_STDARG) && !defined(I_VARARGS) */
1080 mess(char *pat, va_list *args)
1094 #ifdef USE_CHAR_VSPRINTF
1102 usermess = strEQ(pat, "%s");
1104 tmpstr = sv_newmortal();
1105 sv_setpv(tmpstr, va_arg(*args, char *));
1106 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
1109 (void) vsprintf(s,pat,*args);
1114 if (s[-1] != '\n') {
1116 strcpy(s, " during global destruction.\n");
1118 if (curcop->cop_line) {
1119 (void)sprintf(s," at %s line %ld",
1120 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
1123 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1124 bool line_mode = (RsSIMPLE(rs) &&
1125 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1126 (void)sprintf(s,", <%s> %s %ld",
1127 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1128 line_mode ? "line" : "chunk",
1129 (long)IoLINES(GvIOp(last_in_gv)));
1132 (void)strcpy(s,".\n");
1136 sv_catpv(tmpstr,buf+1);
1139 if (s - s_start >= sizeof(buf)) { /* Ooops! */
1141 PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr));
1143 PerlIO_puts(PerlIO_stderr(), buf);
1144 PerlIO_puts(PerlIO_stderr(), "panic: message overflow - memory corrupted!\n");
1148 return SvPVX(tmpstr);
1155 croak(char* pat, ...)
1159 croak(pat, va_alist)
1171 va_start(args, pat);
1175 message = mess(pat, &args);
1178 SV *olddiehook = diehook;
1179 diehook = Nullsv; /* sv_2cv might call croak() */
1180 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1181 diehook = olddiehook;
1182 if (cv && !CvDEPTH(cv)) {
1187 PUSHs(sv_2mortal(newSVpv(message,0)));
1189 perl_call_sv((SV*)cv, G_DISCARD);
1193 restartop = die_where(message);
1194 Siglongjmp(top_env, 3);
1196 PerlIO_puts(PerlIO_stderr(),message);
1197 (void)PerlIO_flush(PerlIO_stderr());
1203 (void)UNLINK(e_tmpname);
1204 Safefree(e_tmpname);
1207 statusvalue = SHIFTSTATUS(statusvalue);
1209 my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44)));
1211 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1232 va_start(args, pat);
1236 message = mess(pat, &args);
1240 SV *oldwarnhook = warnhook;
1241 warnhook = Nullsv; /* sv_2cv might end up calling warn() */
1242 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1243 warnhook = oldwarnhook;
1244 if (cv && !CvDEPTH(cv)) {
1249 PUSHs(sv_2mortal(newSVpv(message,0)));
1251 perl_call_sv((SV*)cv, G_DISCARD);
1255 PerlIO_puts(PerlIO_stderr(),message);
1259 (void)PerlIO_flush(PerlIO_stderr());
1261 #endif /* !defined(I_STDARG) && !defined(I_VARARGS) */
1263 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1268 register I32 i=setenv_getix(nam); /* where does it go? */
1270 if (environ == origenviron) { /* need we copy environment? */
1276 for (max = i; environ[max]; max++) ;
1277 New(901,tmpenv, max+2, char*);
1278 for (j=0; j<max; j++) /* copy environment */
1279 tmpenv[j] = savepv(environ[j]);
1280 tmpenv[max] = Nullch;
1281 environ = tmpenv; /* tell exec where it is now */
1284 while (environ[i]) {
1285 environ[i] = environ[i+1];
1290 if (!environ[i]) { /* does not exist yet */
1291 Renew(environ, i+2, char*); /* just expand it a bit */
1292 environ[i+1] = Nullch; /* make sure it's null terminated */
1295 Safefree(environ[i]);
1296 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1298 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1300 /* MS-DOS requires environment variable names to be in uppercase */
1301 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1302 * some utilities and applications may break because they only look
1303 * for upper case strings. (Fixed strupr() bug here.)]
1305 strcpy(environ[i],nam); strupr(environ[i]);
1306 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1314 register I32 i, len = strlen(nam);
1316 for (i = 0; environ[i]; i++) {
1317 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1318 break; /* strnEQ must come first to avoid */
1319 } /* potential SEGV's */
1324 #ifdef UNLINK_ALL_VERSIONS
1326 unlnk(f) /* unlink all versions of a file */
1331 for (i = 0; unlink(f) >= 0; i++) ;
1336 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1338 my_bcopy(from,to,len)
1339 register char *from;
1345 if (from - to >= 0) {
1353 *(--to) = *(--from);
1359 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1375 my_memcmp(s1,s2,len)
1376 register unsigned char *s1;
1377 register unsigned char *s2;
1383 if (tmp = *s1++ - *s2++)
1388 #endif /* HAS_MEMCMP */
1390 #if defined(I_STDARG) || defined(I_VARARGS)
1393 #ifdef USE_CHAR_VSPRINTF
1398 vsprintf(dest, pat, args)
1399 char *dest, *pat, *args;
1403 fakebuf._ptr = dest;
1404 fakebuf._cnt = 32767;
1408 fakebuf._flag = _IOWRT|_IOSTRG;
1409 _doprnt(pat, args, &fakebuf); /* what a kludge */
1410 (void)putc('\0', &fakebuf);
1411 #ifdef USE_CHAR_VSPRINTF
1414 return 0; /* perl doesn't use return value */
1418 #endif /* HAS_VPRINTF */
1419 #endif /* I_VARARGS || I_STDARGS */
1422 #if BYTEORDER != 0x4321
1424 #ifndef CAN_PROTOTYPE
1431 #if (BYTEORDER & 1) == 0
1434 result = ((s & 255) << 8) + ((s >> 8) & 255);
1442 #ifndef CAN_PROTOTYPE
1451 char c[sizeof(long)];
1454 #if BYTEORDER == 0x1234
1455 u.c[0] = (l >> 24) & 255;
1456 u.c[1] = (l >> 16) & 255;
1457 u.c[2] = (l >> 8) & 255;
1461 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1462 croak("Unknown BYTEORDER\n");
1467 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1468 u.c[o & 0xf] = (l >> s) & 255;
1476 #ifndef CAN_PROTOTYPE
1485 char c[sizeof(long)];
1488 #if BYTEORDER == 0x1234
1489 u.c[0] = (l >> 24) & 255;
1490 u.c[1] = (l >> 16) & 255;
1491 u.c[2] = (l >> 8) & 255;
1495 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1496 croak("Unknown BYTEORDER\n");
1503 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1504 l |= (u.c[o & 0xf] & 255) << s;
1511 #endif /* BYTEORDER != 0x4321 */
1515 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1516 * If these functions are defined,
1517 * the BYTEORDER is neither 0x1234 nor 0x4321.
1518 * However, this is not assumed.
1522 #define HTOV(name,type) \
1529 char c[sizeof(type)]; \
1533 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1534 u.c[i] = (n >> s) & 0xFF; \
1539 #define VTOH(name,type) \
1546 char c[sizeof(type)]; \
1552 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1553 n += (u.c[i] & 0xFF) << s; \
1558 #if defined(HAS_HTOVS) && !defined(htovs)
1561 #if defined(HAS_HTOVL) && !defined(htovl)
1564 #if defined(HAS_VTOHS) && !defined(vtohs)
1567 #if defined(HAS_VTOHL) && !defined(vtohl)
1571 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) \
1572 && !defined(VMS) /* VMS' my_popen() is in VMS.c, same with OS/2. */
1579 register I32 this, that;
1591 return my_syspopen(cmd,mode);
1596 this = (*mode == 'w');
1601 taint_proper("Insecure %s%s", "EXEC");
1604 while ((pid = (doexec?vfork():fork())) < 0) {
1605 if (errno != EAGAIN) {
1608 croak("Can't fork");
1619 if (p[THIS] != (*mode == 'r')) {
1620 dup2(p[THIS], *mode == 'r');
1624 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1630 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1633 do_exec(cmd); /* may or may not use the shell */
1637 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1638 sv_setiv(GvSV(tmpgv),(I32)getpid());
1640 hv_clear(pidstatus); /* we have no children */
1645 do_execfree(); /* free any memory malloced by child on vfork */
1647 if (p[that] < p[this]) {
1648 dup2(p[this], p[that]);
1652 sv = *av_fetch(fdpid,p[this],TRUE);
1653 (void)SvUPGRADE(sv,SVt_IV);
1656 return PerlIO_fdopen(p[this], mode);
1659 #if defined(atarist) || defined(DJGPP)
1666 /* Needs work for PerlIO ! */
1667 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1668 return popen(PerlIO_exportFILE(cmd, 0), mode);
1672 #endif /* !DOSISH */
1679 struct stat tmpstatbuf;
1681 PerlIO_printf(PerlIO_stderr(),"%s", s);
1682 for (fd = 0; fd < 32; fd++) {
1683 if (Fstat(fd,&tmpstatbuf) >= 0)
1684 PerlIO_printf(PerlIO_stderr()," %d",fd);
1686 PerlIO_printf(PerlIO_stderr(),"\n");
1696 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1700 return fcntl(oldfd, F_DUPFD, newfd);
1709 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1712 close(fdtmp[--fdx]);
1718 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) \
1719 && !defined(VMS) /* VMS' my_popen() is in VMS.c */
1724 Signal_t (*hstat)(), (*istat)(), (*qstat)();
1729 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
1730 pid = (int)SvIVX(*svp);
1734 if (pid == -1) { /* Opened by popen. */
1735 return my_syspclose(ptr);
1740 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1742 hstat = signal(SIGHUP, SIG_IGN);
1743 istat = signal(SIGINT, SIG_IGN);
1744 qstat = signal(SIGQUIT, SIG_IGN);
1746 pid = wait4pid(pid, &status, 0);
1747 } while (pid == -1 && errno == EINTR);
1748 signal(SIGHUP, hstat);
1749 signal(SIGINT, istat);
1750 signal(SIGQUIT, qstat);
1751 return(pid < 0 ? pid : status);
1753 #endif /* !DOSISH */
1755 #if !defined(DOSISH) || defined(OS2)
1757 wait4pid(pid,statusp,flags)
1769 sprintf(spid, "%d", pid);
1770 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1771 if (svp && *svp != &sv_undef) {
1772 *statusp = SvIVX(*svp);
1773 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1780 hv_iterinit(pidstatus);
1781 if (entry = hv_iternext(pidstatus)) {
1782 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1783 sv = hv_iterval(pidstatus,entry);
1784 *statusp = SvIVX(sv);
1785 sprintf(spid, "%d", pid);
1786 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1791 return waitpid(pid,statusp,flags);
1794 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
1799 croak("Can't do waitpid with flags");
1801 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
1802 pidgone(result,*statusp);
1811 #endif /* !DOSISH */
1822 sprintf(spid, "%d", pid);
1823 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
1824 (void)SvUPGRADE(sv,SVt_IV);
1829 #if defined(atarist) || defined(OS2) || defined(DJGPP)
1832 int /* Cannot prototype with I32
1841 /* Needs work for PerlIO ! */
1842 FILE *f = PerlIO_findFILE(ptr);
1843 I32 result = pclose(f);
1844 PerlIO_releaseFILE(ptr,f);
1850 repeatcpy(to,from,len,count)
1852 register char *from;
1857 register char *frombase = from;
1865 while (count-- > 0) {
1866 for (todo = len; todo > 0; todo--) {
1873 #ifndef CASTNEGFLOAT
1881 # define BIGDOUBLE 2147483648.0
1883 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
1886 return (unsigned long)f;
1888 return (unsigned long)along;
1895 /* Unfortunately, on some systems the cast_uv() function doesn't
1896 work with the system-supplied definition of ULONG_MAX. The
1897 comparison (f >= ULONG_MAX) always comes out true. It must be a
1898 problem with the compiler constant folding.
1900 In any case, this workaround should be fine on any two's complement
1901 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
1903 --Andy Dougherty <doughera@lafcol.lafayette.edu>
1906 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
1908 -- Kenneth Albanowski <kjahds@kjahds.com>
1912 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
1920 return (I32) I32_MAX;
1922 return (I32) I32_MIN;
1942 return (UV) MY_UV_MAX;
1954 char *fa = strrchr(a,'/');
1955 char *fb = strrchr(b,'/');
1956 struct stat tmpstatbuf1;
1957 struct stat tmpstatbuf2;
1959 #define MAXPATHLEN 1024
1961 char tmpbuf[MAXPATHLEN+1];
1976 strncpy(tmpbuf, a, fa - a);
1977 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
1982 strncpy(tmpbuf, b, fb - b);
1983 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
1985 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
1986 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
1988 #endif /* !HAS_RENAME */
1991 scan_oct(start, len, retlen)
1996 register char *s = start;
1997 register UV retval = 0;
1998 bool overflowed = FALSE;
2000 while (len && *s >= '0' && *s <= '7') {
2001 register UV n = retval << 3;
2002 if (!overflowed && (n >> 3) != retval) {
2003 warn("Integer overflow in octal number");
2006 retval = n | (*s++ - '0');
2009 if (dowarn && len && (*s == '8' || *s == '9'))
2010 warn("Illegal octal digit ignored");
2011 *retlen = s - start;
2016 scan_hex(start, len, retlen)
2021 register char *s = start;
2022 register UV retval = 0;
2023 bool overflowed = FALSE;
2026 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2027 register UV n = retval << 4;
2028 if (!overflowed && (n >> 4) != retval) {
2029 warn("Integer overflow in hex number");
2032 retval = n | (tmp - hexdigit) & 15;
2035 *retlen = s - start;
2042 * This hack is to force load of "huge" support from libm.a
2043 * So it is in perl for (say) POSIX to use.
2044 * Needed for SunOS with Sun's 'acc' for example.