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(stderr,"0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
88 DEBUG_m(fprintf(stderr,"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(stderr,"0x%x: (%05d) rfree\n",where,an++);
134 fprintf(stderr,"0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
138 fprintf(stderr,"0x%lx: (%05d) rfree\n",where,an++);
139 fprintf(stderr,"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(stderr,"0x%x: (%05d) free\n",where,an++));
163 DEBUG_m( fprintf(stderr,"0x%lx: (%05d) free\n",where,an++));
171 #endif /* !safemalloc */
175 #define ALIGN sizeof(long)
182 register char *where;
184 where = safemalloc(size + ALIGN);
188 return where + ALIGN;
192 safexrealloc(where,size)
196 register char *new = saferealloc(where - ALIGN, size + ALIGN);
209 x = where[0] + 100 * where[1];
219 for (i = 0; i < MAXXCOUNT; i++) {
220 if (xcount[i] > lastxcount[i]) {
221 fprintf(stderr,"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
222 lastxcount[i] = xcount[i];
227 #endif /* LEAKTEST */
229 /* copy a string up to some (non-backslashed) delimiter, if any */
232 cpytill(to,from,fromend,delim,retlen)
235 register char *fromend;
241 for (; from < fromend; from++,to++) {
243 if (from[1] == delim)
245 else if (from[1] == '\\')
248 else if (*from == delim)
253 *retlen = to - origto;
257 /* return ptr to little string in big string, NULL if not found */
258 /* This routine was donated by Corey Satten. */
263 register char *little;
265 register char *s, *x;
276 for (x=big,s=little; *s; /**/ ) {
290 /* same as instr but allow embedded nulls */
293 ninstr(big, bigend, little, lend)
295 register char *bigend;
299 register char *s, *x;
300 register I32 first = *little;
301 register char *littleend = lend;
303 if (!first && little >= littleend)
305 if (bigend - big < littleend - little)
307 bigend -= littleend - little++;
308 while (big <= bigend) {
311 for (x=big,s=little; s < littleend; /**/ ) {
323 /* reverse of the above--find last substring */
326 rninstr(big, bigend, little, lend)
332 register char *bigbeg;
333 register char *s, *x;
334 register I32 first = *little;
335 register char *littleend = lend;
337 if (!first && little >= littleend)
340 big = bigend - (littleend - little++);
341 while (big >= bigbeg) {
344 for (x=big+2,s=little; s < littleend; /**/ ) {
356 /* Initialize locale (and the fold[] array).*/
358 perl_init_i18nl14n(printwarn)
363 * 1 = set ok or not applicable,
364 * 0 = fallback to C locale,
365 * -1 = fallback to C locale failed
367 #if defined(HAS_SETLOCALE) && defined(LC_CTYPE)
368 char * lang = getenv("LANG");
369 char * lc_all = getenv("LC_ALL");
370 char * lc_ctype = getenv("LC_CTYPE");
373 if (setlocale(LC_CTYPE, "") == NULL && (lc_all || lc_ctype || lang)) {
375 fprintf(stderr, "warning: setlocale(LC_CTYPE, \"\") failed.\n");
377 "warning: LC_ALL = \"%s\", LC_CTYPE = \"%s\", LANG = \"%s\",\n",
378 lc_all ? lc_all : "(null)",
379 lc_ctype ? lc_ctype : "(null)",
380 lang ? lang : "(null)"
382 fprintf(stderr, "warning: falling back to the \"C\" locale.\n");
385 if (setlocale(LC_CTYPE, "C") == NULL)
389 for (i = 0; i < 256; i++) {
390 if (isUPPER(i)) fold[i] = toLOWER(i);
391 else if (isLOWER(i)) fold[i] = toUPPER(i);
399 fbm_compile(sv, iflag)
403 register unsigned char *s;
404 register unsigned char *table;
406 register U32 len = SvCUR(sv);
411 return; /* can't have offsets that big */
413 table = (unsigned char*)(SvPVX(sv) + len + 1);
415 for (i = 0; i < 256; i++) {
419 while (s >= (unsigned char*)(SvPVX(sv)))
421 if (table[*s] == len) {
424 table[*s] = table[fold[*s]] = i;
438 sv_upgrade(sv, SVt_PVBM);
439 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
442 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
444 register U32 tmp, foldtmp;
446 for (i = 0; i < len; i++) {
448 foldtmp=freq[fold[s[i]]];
449 if (tmp < frequency && foldtmp < frequency) {
451 /* choose most frequent among the two */
452 frequency = (tmp > foldtmp) ? tmp : foldtmp;
457 for (i = 0; i < len; i++) {
458 if (freq[s[i]] < frequency) {
460 frequency = freq[s[i]];
464 BmRARE(sv) = s[rarest];
465 BmPREVIOUS(sv) = rarest;
466 DEBUG_r(fprintf(stderr,"rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
470 fbm_instr(big, bigend, littlestr)
472 register unsigned char *bigend;
475 register unsigned char *s;
477 register I32 littlelen;
478 register unsigned char *little;
479 register unsigned char *table;
480 register unsigned char *olds;
481 register unsigned char *oldlittle;
483 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
485 char *l = SvPV(littlestr,len);
488 return ninstr((char*)big,(char*)bigend, l, l + len);
491 littlelen = SvCUR(littlestr);
492 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
493 if (littlelen > bigend - big)
495 little = (unsigned char*)SvPVX(littlestr);
496 if (SvCASEFOLD(littlestr)) { /* oops, fake it */
497 big = bigend - littlelen; /* just start near end */
498 if (bigend[-1] == '\n' && little[littlelen-1] != '\n')
502 s = bigend - littlelen;
503 if (*s == *little && bcmp((char*)s,(char*)little,littlelen)==0)
504 return (char*)s; /* how sweet it is */
505 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
508 if (*s == *little && bcmp((char*)s,(char*)little,littlelen)==0)
514 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
515 if (--littlelen >= bigend - big)
518 oldlittle = little = table - 2;
519 if (SvCASEFOLD(littlestr)) { /* case insensitive? */
523 if (tmp = table[*s]) {
525 if (bigend - s > tmp) {
530 if ((s += tmp) < bigend)
536 tmp = littlelen; /* less expensive than calling strncmp() */
539 if (*--s == *--little || fold[*s] == *little)
541 s = olds + 1; /* here we pay the price for failure */
543 if (s < bigend) /* fake up continue to outer loop */
555 if (tmp = table[*s]) {
557 if (bigend - s > tmp) {
562 if ((s += tmp) < bigend)
568 tmp = littlelen; /* less expensive than calling strncmp() */
571 if (*--s == *--little)
573 s = olds + 1; /* here we pay the price for failure */
575 if (s < bigend) /* fake up continue to outer loop */
587 screaminstr(bigstr, littlestr)
591 register unsigned char *s, *x;
592 register unsigned char *big;
594 register I32 previous;
596 register unsigned char *little;
597 register unsigned char *bigend;
598 register unsigned char *littleend;
600 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
602 little = (unsigned char *)(SvPVX(littlestr));
603 littleend = little + SvCUR(littlestr);
605 previous = BmPREVIOUS(littlestr);
606 big = (unsigned char *)(SvPVX(bigstr));
607 bigend = big + SvCUR(bigstr);
608 while (pos < previous) {
609 if (!(pos += screamnext[pos]))
613 if (SvCASEFOLD(littlestr)) { /* case insignificant? */
615 if (big[pos-previous] != first && big[pos-previous] != fold[first])
617 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
620 if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
626 return (char *)(big+pos-previous);
628 pos += screamnext[pos] /* does this goof up anywhere? */
633 if (big[pos-previous] != first)
635 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
644 return (char *)(big+pos-previous);
645 } while ( pos += screamnext[pos] );
647 #else /* !POINTERRIGOR */
649 if (SvCASEFOLD(littlestr)) { /* case insignificant? */
651 if (big[pos] != first && big[pos] != fold[first])
653 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
656 if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
662 return (char *)(big+pos);
664 pos += screamnext[pos] /* does this goof up anywhere? */
669 if (big[pos] != first)
671 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
680 return (char *)(big+pos);
682 pos += screamnext[pos]
685 #endif /* POINTERRIGOR */
700 if (fold[*a++] == *b++)
707 /* copy a string to a safe spot */
713 register char *newaddr;
715 New(902,newaddr,strlen(sv)+1,char);
716 (void)strcpy(newaddr,sv);
720 /* same thing but with a known length */
727 register char *newaddr;
729 New(903,newaddr,len+1,char);
730 Copy(sv,newaddr,len,char); /* might not be null terminated */
731 newaddr[len] = '\0'; /* is now */
735 #if !defined(I_STDARG) && !defined(I_VARARGS)
738 * Fallback on the old hackers way of doing varargs
743 mess(pat,a1,a2,a3,a4)
749 I32 usermess = strEQ(pat,"%s");
754 tmpstr = sv_newmortal();
755 sv_setpv(tmpstr, (char*)a1);
756 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
759 (void)sprintf(s,pat,a1,a2,a3,a4);
765 strcpy(s, " during global destruction.\n");
767 if (curcop->cop_line) {
768 (void)sprintf(s," at %s line %ld",
769 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
772 if (GvIO(last_in_gv) &&
773 IoLINES(GvIOp(last_in_gv)) ) {
774 (void)sprintf(s,", <%s> %s %ld",
775 last_in_gv == argvgv ? "" : GvENAME(last_in_gv),
776 strEQ(rs,"\n") ? "line" : "chunk",
777 (long)IoLINES(GvIOp(last_in_gv)));
780 (void)strcpy(s,".\n");
784 sv_catpv(tmpstr,buf+1);
787 if (s - s_start >= sizeof(buf)) { /* Ooops! */
789 fputs(SvPVX(tmpstr), stderr);
792 fputs("panic: message overflow - memory corrupted!\n",stderr);
796 return SvPVX(tmpstr);
802 void croak(pat,a1,a2,a3,a4)
812 message = mess(pat,a1,a2,a3,a4);
813 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
818 PUSHs(sv_2mortal(newSVpv(message,0)));
820 perl_call_sv((SV*)cv, G_DISCARD);
823 restartop = die_where(message);
824 Siglongjmp(top_env, 3);
826 fputs(message,stderr);
827 (void)Fflush(stderr);
833 (void)UNLINK(e_tmpname);
837 statusvalue = SHIFTSTATUS(statusvalue);
839 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
841 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
846 void warn(pat,a1,a2,a3,a4)
856 message = mess(pat,a1,a2,a3,a4);
857 if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
862 PUSHs(sv_2mortal(newSVpv(message,0)));
864 perl_call_sv((SV*)cv, G_DISCARD);
867 fputs(message,stderr);
871 (void)Fflush(stderr);
875 #else /* !defined(I_STDARG) && !defined(I_VARARGS) */
879 mess(char *pat, va_list *args)
893 #ifdef USE_CHAR_VSPRINTF
901 usermess = strEQ(pat, "%s");
903 tmpstr = sv_newmortal();
904 sv_setpv(tmpstr, va_arg(*args, char *));
905 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
908 (void) vsprintf(s,pat,*args);
915 strcpy(s, " during global destruction.\n");
917 if (curcop->cop_line) {
918 (void)sprintf(s," at %s line %ld",
919 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
922 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
923 bool line_mode = (RsSIMPLE(rs) &&
924 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
925 (void)sprintf(s,", <%s> %s %ld",
926 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
927 line_mode ? "line" : "chunk",
928 (long)IoLINES(GvIOp(last_in_gv)));
931 (void)strcpy(s,".\n");
935 sv_catpv(tmpstr,buf+1);
938 if (s - s_start >= sizeof(buf)) { /* Ooops! */
940 fputs(SvPVX(tmpstr), stderr);
943 fputs("panic: message overflow - memory corrupted!\n",stderr);
947 return SvPVX(tmpstr);
954 croak(char* pat, ...)
974 message = mess(pat, &args);
976 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
981 PUSHs(sv_2mortal(newSVpv(message,0)));
983 perl_call_sv((SV*)cv, G_DISCARD);
986 restartop = die_where(message);
987 Siglongjmp(top_env, 3);
989 fputs(message,stderr);
990 (void)Fflush(stderr);
996 (void)UNLINK(e_tmpname);
1000 statusvalue = SHIFTSTATUS(statusvalue);
1002 my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44)));
1004 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1025 va_start(args, pat);
1029 message = mess(pat, &args);
1032 if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
1037 PUSHs(sv_2mortal(newSVpv(message,0)));
1039 perl_call_sv((SV*)cv, G_DISCARD);
1042 fputs(message,stderr);
1046 (void)Fflush(stderr);
1049 #endif /* !defined(I_STDARG) && !defined(I_VARARGS) */
1051 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1056 register I32 i=setenv_getix(nam); /* where does it go? */
1058 if (environ == origenviron) { /* need we copy environment? */
1064 for (max = i; environ[max]; max++) ;
1065 New(901,tmpenv, max+2, char*);
1066 for (j=0; j<max; j++) /* copy environment */
1067 tmpenv[j] = savepv(environ[j]);
1068 tmpenv[max] = Nullch;
1069 environ = tmpenv; /* tell exec where it is now */
1072 while (environ[i]) {
1073 environ[i] = environ[i+1];
1078 if (!environ[i]) { /* does not exist yet */
1079 Renew(environ, i+2, char*); /* just expand it a bit */
1080 environ[i+1] = Nullch; /* make sure it's null terminated */
1083 Safefree(environ[i]);
1084 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1086 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1088 /* MS-DOS requires environment variable names to be in uppercase */
1089 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1090 * some utilities and applications may break because they only look
1091 * for upper case strings. (Fixed strupr() bug here.)]
1093 strcpy(environ[i],nam); strupr(environ[i]);
1094 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1102 register I32 i, len = strlen(nam);
1104 for (i = 0; environ[i]; i++) {
1105 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1106 break; /* strnEQ must come first to avoid */
1107 } /* potential SEGV's */
1112 #ifdef UNLINK_ALL_VERSIONS
1114 unlnk(f) /* unlink all versions of a file */
1119 for (i = 0; unlink(f) >= 0; i++) ;
1124 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1126 my_bcopy(from,to,len)
1127 register char *from;
1133 if (from - to >= 0) {
1141 *(--to) = *(--from);
1147 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1163 my_memcmp(s1,s2,len)
1164 register unsigned char *s1;
1165 register unsigned char *s2;
1171 if (tmp = *s1++ - *s2++)
1176 #endif /* HAS_MEMCMP */
1178 #if defined(I_STDARG) || defined(I_VARARGS)
1181 #ifdef USE_CHAR_VSPRINTF
1186 vsprintf(dest, pat, args)
1187 char *dest, *pat, *args;
1191 fakebuf._ptr = dest;
1192 fakebuf._cnt = 32767;
1196 fakebuf._flag = _IOWRT|_IOSTRG;
1197 _doprnt(pat, args, &fakebuf); /* what a kludge */
1198 (void)putc('\0', &fakebuf);
1199 #ifdef USE_CHAR_VSPRINTF
1202 return 0; /* perl doesn't use return value */
1207 vfprintf(fd, pat, args)
1211 _doprnt(pat, args, fd);
1212 return 0; /* wrong, but perl doesn't use the return value */
1214 #endif /* HAS_VPRINTF */
1215 #endif /* I_VARARGS || I_STDARGS */
1218 #if BYTEORDER != 0x4321
1220 #ifndef CAN_PROTOTYPE
1227 #if (BYTEORDER & 1) == 0
1230 result = ((s & 255) << 8) + ((s >> 8) & 255);
1238 #ifndef CAN_PROTOTYPE
1247 char c[sizeof(long)];
1250 #if BYTEORDER == 0x1234
1251 u.c[0] = (l >> 24) & 255;
1252 u.c[1] = (l >> 16) & 255;
1253 u.c[2] = (l >> 8) & 255;
1257 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1258 croak("Unknown BYTEORDER\n");
1263 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1264 u.c[o & 0xf] = (l >> s) & 255;
1272 #ifndef CAN_PROTOTYPE
1281 char c[sizeof(long)];
1284 #if BYTEORDER == 0x1234
1285 u.c[0] = (l >> 24) & 255;
1286 u.c[1] = (l >> 16) & 255;
1287 u.c[2] = (l >> 8) & 255;
1291 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1292 croak("Unknown BYTEORDER\n");
1299 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1300 l |= (u.c[o & 0xf] & 255) << s;
1307 #endif /* BYTEORDER != 0x4321 */
1311 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1312 * If these functions are defined,
1313 * the BYTEORDER is neither 0x1234 nor 0x4321.
1314 * However, this is not assumed.
1318 #define HTOV(name,type) \
1325 char c[sizeof(type)]; \
1329 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1330 u.c[i] = (n >> s) & 0xFF; \
1335 #define VTOH(name,type) \
1342 char c[sizeof(type)]; \
1348 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1349 n += (u.c[i] & 0xFF) << s; \
1354 #if defined(HAS_HTOVS) && !defined(htovs)
1357 #if defined(HAS_HTOVL) && !defined(htovl)
1360 #if defined(HAS_VTOHS) && !defined(vtohs)
1363 #if defined(HAS_VTOHL) && !defined(vtohl)
1367 #if !defined(DOSISH) && !defined(VMS) /* VMS' my_popen() is in
1368 VMS.c, same with OS/2. */
1375 register I32 this, that;
1378 I32 doexec = strNE(cmd,"-");
1382 this = (*mode == 'w');
1387 taint_proper("Insecure %s%s", "EXEC");
1390 while ((pid = (doexec?vfork():fork())) < 0) {
1391 if (errno != EAGAIN) {
1394 croak("Can't fork");
1405 if (p[THIS] != (*mode == 'r')) {
1406 dup2(p[THIS], *mode == 'r');
1410 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1416 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1419 do_exec(cmd); /* may or may not use the shell */
1423 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1424 sv_setiv(GvSV(tmpgv),(I32)getpid());
1426 hv_clear(pidstatus); /* we have no children */
1431 do_execfree(); /* free any memory malloced by child on vfork */
1433 if (p[that] < p[this]) {
1434 dup2(p[this], p[that]);
1438 sv = *av_fetch(fdpid,p[this],TRUE);
1439 (void)SvUPGRADE(sv,SVt_IV);
1442 return fdopen(p[this], mode);
1445 #if defined(atarist)
1452 return popen(cmd, mode);
1456 #endif /* !DOSISH */
1463 struct stat tmpstatbuf;
1465 fprintf(stderr,"%s", s);
1466 for (fd = 0; fd < 32; fd++) {
1467 if (Fstat(fd,&tmpstatbuf) >= 0)
1468 fprintf(stderr," %d",fd);
1470 fprintf(stderr,"\n");
1480 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1484 return fcntl(oldfd, F_DUPFD, newfd);
1493 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1496 close(fdtmp[--fdx]);
1502 #if !defined(DOSISH) && !defined(VMS) /* VMS' my_popen() is in VMS.c */
1507 Signal_t (*hstat)(), (*istat)(), (*qstat)();
1512 svp = av_fetch(fdpid,fileno(ptr),TRUE);
1513 pid = (int)SvIVX(*svp);
1518 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1520 hstat = signal(SIGHUP, SIG_IGN);
1521 istat = signal(SIGINT, SIG_IGN);
1522 qstat = signal(SIGQUIT, SIG_IGN);
1524 pid = wait4pid(pid, &status, 0);
1525 } while (pid == -1 && errno == EINTR);
1526 signal(SIGHUP, hstat);
1527 signal(SIGINT, istat);
1528 signal(SIGQUIT, qstat);
1529 return(pid < 0 ? pid : status);
1531 #endif /* !DOSISH */
1533 #if !defined(DOSISH) || defined(OS2)
1535 wait4pid(pid,statusp,flags)
1547 sprintf(spid, "%d", pid);
1548 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1549 if (svp && *svp != &sv_undef) {
1550 *statusp = SvIVX(*svp);
1551 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1558 hv_iterinit(pidstatus);
1559 if (entry = hv_iternext(pidstatus)) {
1560 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1561 sv = hv_iterval(pidstatus,entry);
1562 *statusp = SvIVX(sv);
1563 sprintf(spid, "%d", pid);
1564 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1569 return waitpid(pid,statusp,flags);
1572 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
1577 croak("Can't do waitpid with flags");
1579 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
1580 pidgone(result,*statusp);
1589 #endif /* !DOSISH */
1600 sprintf(spid, "%d", pid);
1601 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
1602 (void)SvUPGRADE(sv,SVt_IV);
1607 #if defined(atarist) || defined(OS2)
1618 repeatcpy(to,from,len,count)
1620 register char *from;
1625 register char *frombase = from;
1633 while (count-- > 0) {
1634 for (todo = len; todo > 0; todo--) {
1641 #ifndef CASTNEGFLOAT
1649 # define BIGDOUBLE 2147483648.0
1651 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
1654 return (unsigned long)f;
1656 return (unsigned long)along;
1663 /* Look for MAX and MIN integral values. If we can't find them,
1664 we'll use 32-bit two's complement defaults.
1667 # ifdef MAXLONG /* Often used in <values.h> */
1668 # define LONG_MAX MAXLONG
1670 # define LONG_MAX 2147483647L
1675 # define LONG_MIN (-LONG_MAX - 1)
1680 # define LONG_MAX MAXULONG
1682 # define ULONG_MAX 4294967295L
1686 /* Unfortunately, on some systems the cast_uv() function doesn't
1687 work with the system-supplied definition of ULONG_MAX. The
1688 comparison (f >= ULONG_MAX) always comes out true. It must be a
1689 problem with the compiler constant folding.
1691 In any case, this workaround should be fine on any two's complement
1692 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
1694 --Andy Dougherty <doughera@lafcol.lafayette.edu>
1696 #ifndef MY_ULONG_MAX
1697 # define MY_ULONG_MAX ((UV)LONG_MAX * (UV)2 + (UV)1)
1705 return (I32) LONG_MAX;
1707 return (I32) LONG_MIN;
1716 return (IV) LONG_MAX;
1718 return (IV) LONG_MIN;
1726 if (f >= MY_ULONG_MAX)
1727 return (UV) MY_ULONG_MAX;
1739 char *fa = strrchr(a,'/');
1740 char *fb = strrchr(b,'/');
1741 struct stat tmpstatbuf1;
1742 struct stat tmpstatbuf2;
1744 #define MAXPATHLEN 1024
1746 char tmpbuf[MAXPATHLEN+1];
1761 strncpy(tmpbuf, a, fa - a);
1762 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
1767 strncpy(tmpbuf, b, fb - b);
1768 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
1770 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
1771 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
1773 #endif /* !HAS_RENAME */
1776 scan_oct(start, len, retlen)
1781 register char *s = start;
1782 register unsigned long retval = 0;
1784 while (len && *s >= '0' && *s <= '7') {
1786 retval |= *s++ - '0';
1789 if (dowarn && len && (*s == '8' || *s == '9'))
1790 warn("Illegal octal digit ignored");
1791 *retlen = s - start;
1796 scan_hex(start, len, retlen)
1801 register char *s = start;
1802 register unsigned long retval = 0;
1805 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
1807 retval |= (tmp - hexdigit) & 15;
1810 *retlen = s - start;