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 /* Omit this -- it causes too much grief on mixed systems.
32 #ifdef I_LIMITS /* Needed for cast_xxx() functions below. */
36 /* Put this after #includes because fork and vfork prototypes may
47 # include <sys/file.h>
53 static void xstat _((void));
58 /* paranoid version of malloc */
60 /* NOTE: Do not call the next three routines directly. Use the macros
61 * in handy.h, so that we can easily redefine everything to do tracking of
62 * allocated hunks back to the original New to track down any memory leaks.
76 fprintf(stderr, "Allocation too large: %lx\n", size) FLUSH;
82 croak("panic: malloc");
84 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
85 #if !(defined(I286) || defined(atarist))
86 DEBUG_m(fprintf(Perl_debug_log,"0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
88 DEBUG_m(fprintf(Perl_debug_log,"0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
95 fputs(no_mem,stderr) FLUSH;
101 /* paranoid version of realloc */
104 saferealloc(where,size)
113 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
115 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
119 fprintf(stderr, "Reallocation too large: %lx\n", size) FLUSH;
124 croak("Null realloc");
127 croak("panic: realloc");
129 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
131 #if !(defined(I286) || defined(atarist))
133 fprintf(Perl_debug_log,"0x%x: (%05d) rfree\n",where,an++);
134 fprintf(Perl_debug_log,"0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
138 fprintf(Perl_debug_log,"0x%lx: (%05d) rfree\n",where,an++);
139 fprintf(Perl_debug_log,"0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
148 fputs(no_mem,stderr) FLUSH;
154 /* safe version of free */
160 #if !(defined(I286) || defined(atarist))
161 DEBUG_m( fprintf(Perl_debug_log,"0x%x: (%05d) free\n",where,an++));
163 DEBUG_m( fprintf(Perl_debug_log,"0x%lx: (%05d) free\n",where,an++));
171 /* safe version of calloc */
174 safecalloc(count, size)
181 if (size * count > 0xffff) {
182 fprintf(stderr, "Allocation too large: %lx\n", size * count) FLUSH;
187 if ((long)size < 0 || (long)count < 0)
188 croak("panic: calloc");
190 #if !(defined(I286) || defined(atarist))
191 DEBUG_m(fprintf(stderr,"0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
193 DEBUG_m(fprintf(stderr,"0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
196 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
198 memset((void*)ptr, 0, size);
204 fputs(no_mem,stderr) FLUSH;
210 #endif /* !safemalloc */
214 #define ALIGN sizeof(long)
221 register char *where;
223 where = safemalloc(size + ALIGN);
227 return where + ALIGN;
231 safexrealloc(where,size)
235 register char *new = saferealloc(where - ALIGN, size + ALIGN);
248 x = where[0] + 100 * where[1];
254 safexcalloc(x,count,size)
259 register char *where;
261 where = safexmalloc(x, size * count + ALIGN);
263 memset((void*)where + ALIGN, 0, size * count);
266 return where + ALIGN;
274 for (i = 0; i < MAXXCOUNT; i++) {
275 if (xcount[i] > lastxcount[i]) {
276 fprintf(stderr,"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
277 lastxcount[i] = xcount[i];
282 #endif /* LEAKTEST */
284 /* copy a string up to some (non-backslashed) delimiter, if any */
287 cpytill(to,from,fromend,delim,retlen)
290 register char *fromend;
296 for (; from < fromend; from++,to++) {
298 if (from[1] == delim)
300 else if (from[1] == '\\')
303 else if (*from == delim)
308 *retlen = to - origto;
312 /* return ptr to little string in big string, NULL if not found */
313 /* This routine was donated by Corey Satten. */
318 register char *little;
320 register char *s, *x;
331 for (x=big,s=little; *s; /**/ ) {
345 /* same as instr but allow embedded nulls */
348 ninstr(big, bigend, little, lend)
350 register char *bigend;
354 register char *s, *x;
355 register I32 first = *little;
356 register char *littleend = lend;
358 if (!first && little >= littleend)
360 if (bigend - big < littleend - little)
362 bigend -= littleend - little++;
363 while (big <= bigend) {
366 for (x=big,s=little; s < littleend; /**/ ) {
378 /* reverse of the above--find last substring */
381 rninstr(big, bigend, little, lend)
387 register char *bigbeg;
388 register char *s, *x;
389 register I32 first = *little;
390 register char *littleend = lend;
392 if (!first && little >= littleend)
395 big = bigend - (littleend - little++);
396 while (big >= bigbeg) {
399 for (x=big+2,s=little; s < littleend; /**/ ) {
411 /* Initialize locale (and the fold[] array).*/
413 perl_init_i18nl10n(printwarn)
418 * 1 = set ok or not applicable,
419 * 0 = fallback to C locale,
420 * -1 = fallback to C locale failed
422 #if defined(HAS_SETLOCALE) && defined(LC_CTYPE)
423 char * lang = getenv("LANG");
424 char * lc_all = getenv("LC_ALL");
425 char * lc_ctype = getenv("LC_CTYPE");
428 if (setlocale(LC_CTYPE, "") == NULL && (lc_all || lc_ctype || lang)) {
430 fprintf(stderr, "warning: setlocale(LC_CTYPE, \"\") failed.\n");
432 "warning: LC_ALL = \"%s\", LC_CTYPE = \"%s\", LANG = \"%s\",\n",
433 lc_all ? lc_all : "(null)",
434 lc_ctype ? lc_ctype : "(null)",
435 lang ? lang : "(null)"
437 fprintf(stderr, "warning: falling back to the \"C\" locale.\n");
440 if (setlocale(LC_CTYPE, "C") == NULL)
444 for (i = 0; i < 256; i++) {
445 if (isUPPER(i)) fold[i] = toLOWER(i);
446 else if (isLOWER(i)) fold[i] = toUPPER(i);
454 fbm_compile(sv, iflag)
458 register unsigned char *s;
459 register unsigned char *table;
461 register U32 len = SvCUR(sv);
466 return; /* can't have offsets that big */
468 table = (unsigned char*)(SvPVX(sv) + len + 1);
470 for (i = 0; i < 256; i++) {
474 while (s >= (unsigned char*)(SvPVX(sv)))
476 if (table[*s] == len) {
479 table[*s] = table[fold[*s]] = i;
493 sv_upgrade(sv, SVt_PVBM);
494 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
497 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
499 register U32 tmp, foldtmp;
501 for (i = 0; i < len; i++) {
503 foldtmp=freq[fold[s[i]]];
504 if (tmp < frequency && foldtmp < frequency) {
506 /* choose most frequent among the two */
507 frequency = (tmp > foldtmp) ? tmp : foldtmp;
512 for (i = 0; i < len; i++) {
513 if (freq[s[i]] < frequency) {
515 frequency = freq[s[i]];
519 BmRARE(sv) = s[rarest];
520 BmPREVIOUS(sv) = rarest;
521 DEBUG_r(fprintf(Perl_debug_log,"rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
525 fbm_instr(big, bigend, littlestr)
527 register unsigned char *bigend;
530 register unsigned char *s;
532 register I32 littlelen;
533 register unsigned char *little;
534 register unsigned char *table;
535 register unsigned char *olds;
536 register unsigned char *oldlittle;
538 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
540 char *l = SvPV(littlestr,len);
543 return ninstr((char*)big,(char*)bigend, l, l + len);
546 littlelen = SvCUR(littlestr);
547 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
548 if (littlelen > bigend - big)
550 little = (unsigned char*)SvPVX(littlestr);
551 if (SvCASEFOLD(littlestr)) { /* oops, fake it */
552 big = bigend - littlelen; /* just start near end */
553 if (bigend[-1] == '\n' && little[littlelen-1] != '\n')
557 s = bigend - littlelen;
558 if (*s == *little && bcmp((char*)s,(char*)little,littlelen)==0)
559 return (char*)s; /* how sweet it is */
560 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
563 if (*s == *little && bcmp((char*)s,(char*)little,littlelen)==0)
569 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
570 if (--littlelen >= bigend - big)
573 oldlittle = little = table - 2;
574 if (SvCASEFOLD(littlestr)) { /* case insensitive? */
578 if (tmp = table[*s]) {
580 if (bigend - s > tmp) {
585 if ((s += tmp) < bigend)
591 tmp = littlelen; /* less expensive than calling strncmp() */
594 if (*--s == *--little || fold[*s] == *little)
596 s = olds + 1; /* here we pay the price for failure */
598 if (s < bigend) /* fake up continue to outer loop */
610 if (tmp = table[*s]) {
612 if (bigend - s > tmp) {
617 if ((s += tmp) < bigend)
623 tmp = littlelen; /* less expensive than calling strncmp() */
626 if (*--s == *--little)
628 s = olds + 1; /* here we pay the price for failure */
630 if (s < bigend) /* fake up continue to outer loop */
642 screaminstr(bigstr, littlestr)
646 register unsigned char *s, *x;
647 register unsigned char *big;
649 register I32 previous;
651 register unsigned char *little;
652 register unsigned char *bigend;
653 register unsigned char *littleend;
655 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
657 little = (unsigned char *)(SvPVX(littlestr));
658 littleend = little + SvCUR(littlestr);
660 previous = BmPREVIOUS(littlestr);
661 big = (unsigned char *)(SvPVX(bigstr));
662 bigend = big + SvCUR(bigstr);
663 while (pos < previous) {
664 if (!(pos += screamnext[pos]))
668 if (SvCASEFOLD(littlestr)) { /* case insignificant? */
670 if (big[pos-previous] != first && big[pos-previous] != fold[first])
672 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
675 if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
681 return (char *)(big+pos-previous);
683 pos += screamnext[pos] /* does this goof up anywhere? */
688 if (big[pos-previous] != first)
690 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
699 return (char *)(big+pos-previous);
700 } while ( pos += screamnext[pos] );
702 #else /* !POINTERRIGOR */
704 if (SvCASEFOLD(littlestr)) { /* case insignificant? */
706 if (big[pos] != first && big[pos] != fold[first])
708 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
711 if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
717 return (char *)(big+pos);
719 pos += screamnext[pos] /* does this goof up anywhere? */
724 if (big[pos] != first)
726 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
735 return (char *)(big+pos);
737 pos += screamnext[pos]
740 #endif /* POINTERRIGOR */
755 if (fold[*a++] == *b++)
762 /* copy a string to a safe spot */
768 register char *newaddr;
770 New(902,newaddr,strlen(sv)+1,char);
771 (void)strcpy(newaddr,sv);
775 /* same thing but with a known length */
782 register char *newaddr;
784 New(903,newaddr,len+1,char);
785 Copy(sv,newaddr,len,char); /* might not be null terminated */
786 newaddr[len] = '\0'; /* is now */
790 #if !defined(I_STDARG) && !defined(I_VARARGS)
793 * Fallback on the old hackers way of doing varargs
798 mess(pat,a1,a2,a3,a4)
804 I32 usermess = strEQ(pat,"%s");
809 tmpstr = sv_newmortal();
810 sv_setpv(tmpstr, (char*)a1);
811 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
814 (void)sprintf(s,pat,a1,a2,a3,a4);
820 strcpy(s, " during global destruction.\n");
822 if (curcop->cop_line) {
823 (void)sprintf(s," at %s line %ld",
824 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
827 if (GvIO(last_in_gv) &&
828 IoLINES(GvIOp(last_in_gv)) ) {
829 (void)sprintf(s,", <%s> %s %ld",
830 last_in_gv == argvgv ? "" : GvENAME(last_in_gv),
831 strEQ(rs,"\n") ? "line" : "chunk",
832 (long)IoLINES(GvIOp(last_in_gv)));
835 (void)strcpy(s,".\n");
839 sv_catpv(tmpstr,buf+1);
842 if (s - s_start >= sizeof(buf)) { /* Ooops! */
844 fputs(SvPVX(tmpstr), stderr);
847 fputs("panic: message overflow - memory corrupted!\n",stderr);
851 return SvPVX(tmpstr);
857 void croak(pat,a1,a2,a3,a4)
867 message = mess(pat,a1,a2,a3,a4);
868 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
873 PUSHs(sv_2mortal(newSVpv(message,0)));
875 perl_call_sv((SV*)cv, G_DISCARD);
878 restartop = die_where(message);
879 Siglongjmp(top_env, 3);
881 fputs(message,stderr);
882 (void)Fflush(stderr);
888 (void)UNLINK(e_tmpname);
892 statusvalue = SHIFTSTATUS(statusvalue);
894 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
896 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
901 void warn(pat,a1,a2,a3,a4)
911 message = mess(pat,a1,a2,a3,a4);
912 if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
917 PUSHs(sv_2mortal(newSVpv(message,0)));
919 perl_call_sv((SV*)cv, G_DISCARD);
922 fputs(message,stderr);
926 (void)Fflush(stderr);
930 #else /* !defined(I_STDARG) && !defined(I_VARARGS) */
934 mess(char *pat, va_list *args)
948 #ifdef USE_CHAR_VSPRINTF
956 usermess = strEQ(pat, "%s");
958 tmpstr = sv_newmortal();
959 sv_setpv(tmpstr, va_arg(*args, char *));
960 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
963 (void) vsprintf(s,pat,*args);
970 strcpy(s, " during global destruction.\n");
972 if (curcop->cop_line) {
973 (void)sprintf(s," at %s line %ld",
974 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
977 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
978 bool line_mode = (RsSIMPLE(rs) &&
979 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
980 (void)sprintf(s,", <%s> %s %ld",
981 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
982 line_mode ? "line" : "chunk",
983 (long)IoLINES(GvIOp(last_in_gv)));
986 (void)strcpy(s,".\n");
990 sv_catpv(tmpstr,buf+1);
993 if (s - s_start >= sizeof(buf)) { /* Ooops! */
995 fputs(SvPVX(tmpstr), stderr);
998 fputs("panic: message overflow - memory corrupted!\n",stderr);
1002 return SvPVX(tmpstr);
1009 croak(char* pat, ...)
1013 croak(pat, va_alist)
1025 va_start(args, pat);
1029 message = mess(pat, &args);
1031 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
1036 PUSHs(sv_2mortal(newSVpv(message,0)));
1038 perl_call_sv((SV*)cv, G_DISCARD);
1041 restartop = die_where(message);
1042 Siglongjmp(top_env, 3);
1044 fputs(message,stderr);
1045 (void)Fflush(stderr);
1051 (void)UNLINK(e_tmpname);
1052 Safefree(e_tmpname);
1055 statusvalue = SHIFTSTATUS(statusvalue);
1057 my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44)));
1059 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1080 va_start(args, pat);
1084 message = mess(pat, &args);
1087 if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
1092 PUSHs(sv_2mortal(newSVpv(message,0)));
1094 perl_call_sv((SV*)cv, G_DISCARD);
1097 fputs(message,stderr);
1101 (void)Fflush(stderr);
1104 #endif /* !defined(I_STDARG) && !defined(I_VARARGS) */
1106 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1111 register I32 i=setenv_getix(nam); /* where does it go? */
1113 if (environ == origenviron) { /* need we copy environment? */
1119 for (max = i; environ[max]; max++) ;
1120 New(901,tmpenv, max+2, char*);
1121 for (j=0; j<max; j++) /* copy environment */
1122 tmpenv[j] = savepv(environ[j]);
1123 tmpenv[max] = Nullch;
1124 environ = tmpenv; /* tell exec where it is now */
1127 while (environ[i]) {
1128 environ[i] = environ[i+1];
1133 if (!environ[i]) { /* does not exist yet */
1134 Renew(environ, i+2, char*); /* just expand it a bit */
1135 environ[i+1] = Nullch; /* make sure it's null terminated */
1138 Safefree(environ[i]);
1139 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1141 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1143 /* MS-DOS requires environment variable names to be in uppercase */
1144 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1145 * some utilities and applications may break because they only look
1146 * for upper case strings. (Fixed strupr() bug here.)]
1148 strcpy(environ[i],nam); strupr(environ[i]);
1149 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1157 register I32 i, len = strlen(nam);
1159 for (i = 0; environ[i]; i++) {
1160 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1161 break; /* strnEQ must come first to avoid */
1162 } /* potential SEGV's */
1167 #ifdef UNLINK_ALL_VERSIONS
1169 unlnk(f) /* unlink all versions of a file */
1174 for (i = 0; unlink(f) >= 0; i++) ;
1179 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1181 my_bcopy(from,to,len)
1182 register char *from;
1188 if (from - to >= 0) {
1196 *(--to) = *(--from);
1202 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1218 my_memcmp(s1,s2,len)
1219 register unsigned char *s1;
1220 register unsigned char *s2;
1226 if (tmp = *s1++ - *s2++)
1231 #endif /* HAS_MEMCMP */
1233 #if defined(I_STDARG) || defined(I_VARARGS)
1236 #ifdef USE_CHAR_VSPRINTF
1241 vsprintf(dest, pat, args)
1242 char *dest, *pat, *args;
1246 fakebuf._ptr = dest;
1247 fakebuf._cnt = 32767;
1251 fakebuf._flag = _IOWRT|_IOSTRG;
1252 _doprnt(pat, args, &fakebuf); /* what a kludge */
1253 (void)putc('\0', &fakebuf);
1254 #ifdef USE_CHAR_VSPRINTF
1257 return 0; /* perl doesn't use return value */
1262 vfprintf(fd, pat, args)
1266 _doprnt(pat, args, fd);
1267 return 0; /* wrong, but perl doesn't use the return value */
1269 #endif /* HAS_VPRINTF */
1270 #endif /* I_VARARGS || I_STDARGS */
1273 #if BYTEORDER != 0x4321
1275 #ifndef CAN_PROTOTYPE
1282 #if (BYTEORDER & 1) == 0
1285 result = ((s & 255) << 8) + ((s >> 8) & 255);
1293 #ifndef CAN_PROTOTYPE
1302 char c[sizeof(long)];
1305 #if BYTEORDER == 0x1234
1306 u.c[0] = (l >> 24) & 255;
1307 u.c[1] = (l >> 16) & 255;
1308 u.c[2] = (l >> 8) & 255;
1312 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1313 croak("Unknown BYTEORDER\n");
1318 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1319 u.c[o & 0xf] = (l >> s) & 255;
1327 #ifndef CAN_PROTOTYPE
1336 char c[sizeof(long)];
1339 #if BYTEORDER == 0x1234
1340 u.c[0] = (l >> 24) & 255;
1341 u.c[1] = (l >> 16) & 255;
1342 u.c[2] = (l >> 8) & 255;
1346 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1347 croak("Unknown BYTEORDER\n");
1354 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1355 l |= (u.c[o & 0xf] & 255) << s;
1362 #endif /* BYTEORDER != 0x4321 */
1366 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1367 * If these functions are defined,
1368 * the BYTEORDER is neither 0x1234 nor 0x4321.
1369 * However, this is not assumed.
1373 #define HTOV(name,type) \
1380 char c[sizeof(type)]; \
1384 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1385 u.c[i] = (n >> s) & 0xFF; \
1390 #define VTOH(name,type) \
1397 char c[sizeof(type)]; \
1403 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1404 n += (u.c[i] & 0xFF) << s; \
1409 #if defined(HAS_HTOVS) && !defined(htovs)
1412 #if defined(HAS_HTOVL) && !defined(htovl)
1415 #if defined(HAS_VTOHS) && !defined(vtohs)
1418 #if defined(HAS_VTOHL) && !defined(vtohl)
1422 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) /* VMS' my_popen() is in
1423 VMS.c, same with OS/2. */
1430 register I32 this, that;
1433 I32 doexec = strNE(cmd,"-");
1437 this = (*mode == 'w');
1442 taint_proper("Insecure %s%s", "EXEC");
1445 while ((pid = (doexec?vfork():fork())) < 0) {
1446 if (errno != EAGAIN) {
1449 croak("Can't fork");
1460 if (p[THIS] != (*mode == 'r')) {
1461 dup2(p[THIS], *mode == 'r');
1465 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1471 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1474 do_exec(cmd); /* may or may not use the shell */
1478 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1479 sv_setiv(GvSV(tmpgv),(I32)getpid());
1481 hv_clear(pidstatus); /* we have no children */
1486 do_execfree(); /* free any memory malloced by child on vfork */
1488 if (p[that] < p[this]) {
1489 dup2(p[this], p[that]);
1493 sv = *av_fetch(fdpid,p[this],TRUE);
1494 (void)SvUPGRADE(sv,SVt_IV);
1497 return fdopen(p[this], mode);
1500 #if defined(atarist)
1507 return popen(cmd, mode);
1511 #endif /* !DOSISH */
1518 struct stat tmpstatbuf;
1520 fprintf(stderr,"%s", s);
1521 for (fd = 0; fd < 32; fd++) {
1522 if (Fstat(fd,&tmpstatbuf) >= 0)
1523 fprintf(stderr," %d",fd);
1525 fprintf(stderr,"\n");
1535 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1539 return fcntl(oldfd, F_DUPFD, newfd);
1548 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1551 close(fdtmp[--fdx]);
1557 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) /* VMS' my_popen() is in VMS.c */
1562 Signal_t (*hstat)(), (*istat)(), (*qstat)();
1567 svp = av_fetch(fdpid,fileno(ptr),TRUE);
1568 pid = (int)SvIVX(*svp);
1573 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1575 hstat = signal(SIGHUP, SIG_IGN);
1576 istat = signal(SIGINT, SIG_IGN);
1577 qstat = signal(SIGQUIT, SIG_IGN);
1579 pid = wait4pid(pid, &status, 0);
1580 } while (pid == -1 && errno == EINTR);
1581 signal(SIGHUP, hstat);
1582 signal(SIGINT, istat);
1583 signal(SIGQUIT, qstat);
1584 return(pid < 0 ? pid : status);
1586 #endif /* !DOSISH */
1588 #if !defined(DOSISH) || defined(OS2)
1590 wait4pid(pid,statusp,flags)
1602 sprintf(spid, "%d", pid);
1603 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1604 if (svp && *svp != &sv_undef) {
1605 *statusp = SvIVX(*svp);
1606 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1613 hv_iterinit(pidstatus);
1614 if (entry = hv_iternext(pidstatus)) {
1615 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1616 sv = hv_iterval(pidstatus,entry);
1617 *statusp = SvIVX(sv);
1618 sprintf(spid, "%d", pid);
1619 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1624 return waitpid(pid,statusp,flags);
1627 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
1632 croak("Can't do waitpid with flags");
1634 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
1635 pidgone(result,*statusp);
1644 #endif /* !DOSISH */
1655 sprintf(spid, "%d", pid);
1656 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
1657 (void)SvUPGRADE(sv,SVt_IV);
1662 #if defined(atarist) || (defined(OS2) && !defined(HAS_FORK))
1673 repeatcpy(to,from,len,count)
1675 register char *from;
1680 register char *frombase = from;
1688 while (count-- > 0) {
1689 for (todo = len; todo > 0; todo--) {
1696 #ifndef CASTNEGFLOAT
1704 # define BIGDOUBLE 2147483648.0
1706 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
1709 return (unsigned long)f;
1711 return (unsigned long)along;
1718 /* Look for MAX and MIN integral values. If we can't find them,
1719 we'll use 32-bit two's complement defaults.
1722 # ifdef MAXLONG /* Often used in <values.h> */
1723 # define LONG_MAX MAXLONG
1725 # define LONG_MAX 2147483647L
1730 # define LONG_MIN (-LONG_MAX - 1)
1735 # define LONG_MAX MAXULONG
1737 # define ULONG_MAX 4294967295L
1741 /* Unfortunately, on some systems the cast_uv() function doesn't
1742 work with the system-supplied definition of ULONG_MAX. The
1743 comparison (f >= ULONG_MAX) always comes out true. It must be a
1744 problem with the compiler constant folding.
1746 In any case, this workaround should be fine on any two's complement
1747 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
1749 --Andy Dougherty <doughera@lafcol.lafayette.edu>
1751 #ifndef MY_ULONG_MAX
1752 # define MY_ULONG_MAX ((UV)LONG_MAX * (UV)2 + (UV)1)
1760 return (I32) LONG_MAX;
1762 return (I32) LONG_MIN;
1771 return (IV) LONG_MAX;
1773 return (IV) LONG_MIN;
1781 if (f >= MY_ULONG_MAX)
1782 return (UV) MY_ULONG_MAX;
1794 char *fa = strrchr(a,'/');
1795 char *fb = strrchr(b,'/');
1796 struct stat tmpstatbuf1;
1797 struct stat tmpstatbuf2;
1799 #define MAXPATHLEN 1024
1801 char tmpbuf[MAXPATHLEN+1];
1816 strncpy(tmpbuf, a, fa - a);
1817 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
1822 strncpy(tmpbuf, b, fb - b);
1823 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
1825 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
1826 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
1828 #endif /* !HAS_RENAME */
1831 scan_oct(start, len, retlen)
1836 register char *s = start;
1837 register unsigned long retval = 0;
1839 while (len && *s >= '0' && *s <= '7') {
1841 retval |= *s++ - '0';
1844 if (dowarn && len && (*s == '8' || *s == '9'))
1845 warn("Illegal octal digit ignored");
1846 *retlen = s - start;
1851 scan_hex(start, len, retlen)
1856 register char *s = start;
1857 register unsigned long retval = 0;
1860 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
1862 retval |= (tmp - hexdigit) & 15;
1865 *retlen = s - start;