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)
894 #ifdef USE_CHAR_VSPRINTF
902 usermess = strEQ(pat, "%s");
904 tmpstr = sv_newmortal();
905 sv_setpv(tmpstr, va_arg(*args, char *));
906 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
909 (void) vsprintf(s,pat,*args);
916 strcpy(s, " during global destruction.\n");
918 if (curcop->cop_line) {
919 (void)sprintf(s," at %s line %ld",
920 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
923 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
924 bool line_mode = (RsSIMPLE(rs) &&
925 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
926 (void)sprintf(s,", <%s> %s %ld",
927 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
928 line_mode ? "line" : "chunk",
929 (long)IoLINES(GvIOp(last_in_gv)));
932 (void)strcpy(s,".\n");
936 sv_catpv(tmpstr,buf+1);
939 if (s - s_start >= sizeof(buf)) { /* Ooops! */
941 fputs(SvPVX(tmpstr), stderr);
944 fputs("panic: message overflow - memory corrupted!\n",stderr);
948 return SvPVX(tmpstr);
955 croak(char* pat, ...)
976 message = mess(pat, &args);
979 DEBUG_L(fprintf(stderr, "croak: 0x%lx %s", (unsigned long) thr, message));
980 #endif /* USE_THREADS */
981 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
986 PUSHs(sv_2mortal(newSVpv(message,0)));
988 perl_call_sv((SV*)cv, G_DISCARD);
991 restartop = die_where(message);
992 Siglongjmp(top_env, 3);
994 fputs(message,stderr);
995 (void)Fflush(stderr);
1001 (void)UNLINK(e_tmpname);
1002 Safefree(e_tmpname);
1005 statusvalue = SHIFTSTATUS(statusvalue);
1007 my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44)));
1009 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1030 va_start(args, pat);
1034 message = mess(pat, &args);
1037 if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
1043 PUSHs(sv_2mortal(newSVpv(message,0)));
1045 perl_call_sv((SV*)cv, G_DISCARD);
1048 fputs(message,stderr);
1052 (void)Fflush(stderr);
1055 #endif /* !defined(I_STDARG) && !defined(I_VARARGS) */
1057 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1062 register I32 i=setenv_getix(nam); /* where does it go? */
1064 if (environ == origenviron) { /* need we copy environment? */
1070 for (max = i; environ[max]; max++) ;
1071 New(901,tmpenv, max+2, char*);
1072 for (j=0; j<max; j++) /* copy environment */
1073 tmpenv[j] = savepv(environ[j]);
1074 tmpenv[max] = Nullch;
1075 environ = tmpenv; /* tell exec where it is now */
1078 while (environ[i]) {
1079 environ[i] = environ[i+1];
1084 if (!environ[i]) { /* does not exist yet */
1085 Renew(environ, i+2, char*); /* just expand it a bit */
1086 environ[i+1] = Nullch; /* make sure it's null terminated */
1089 Safefree(environ[i]);
1090 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1092 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1094 /* MS-DOS requires environment variable names to be in uppercase */
1095 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1096 * some utilities and applications may break because they only look
1097 * for upper case strings. (Fixed strupr() bug here.)]
1099 strcpy(environ[i],nam); strupr(environ[i]);
1100 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1108 register I32 i, len = strlen(nam);
1110 for (i = 0; environ[i]; i++) {
1111 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1112 break; /* strnEQ must come first to avoid */
1113 } /* potential SEGV's */
1118 #ifdef UNLINK_ALL_VERSIONS
1120 unlnk(f) /* unlink all versions of a file */
1125 for (i = 0; unlink(f) >= 0; i++) ;
1130 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1132 my_bcopy(from,to,len)
1133 register char *from;
1139 if (from - to >= 0) {
1147 *(--to) = *(--from);
1153 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1169 my_memcmp(s1,s2,len)
1170 register unsigned char *s1;
1171 register unsigned char *s2;
1177 if (tmp = *s1++ - *s2++)
1182 #endif /* HAS_MEMCMP */
1184 #if defined(I_STDARG) || defined(I_VARARGS)
1187 #ifdef USE_CHAR_VSPRINTF
1192 vsprintf(dest, pat, args)
1193 char *dest, *pat, *args;
1197 fakebuf._ptr = dest;
1198 fakebuf._cnt = 32767;
1202 fakebuf._flag = _IOWRT|_IOSTRG;
1203 _doprnt(pat, args, &fakebuf); /* what a kludge */
1204 (void)putc('\0', &fakebuf);
1205 #ifdef USE_CHAR_VSPRINTF
1208 return 0; /* perl doesn't use return value */
1213 vfprintf(fd, pat, args)
1217 _doprnt(pat, args, fd);
1218 return 0; /* wrong, but perl doesn't use the return value */
1220 #endif /* HAS_VPRINTF */
1221 #endif /* I_VARARGS || I_STDARGS */
1224 #if BYTEORDER != 0x4321
1226 #ifndef CAN_PROTOTYPE
1233 #if (BYTEORDER & 1) == 0
1236 result = ((s & 255) << 8) + ((s >> 8) & 255);
1244 #ifndef CAN_PROTOTYPE
1253 char c[sizeof(long)];
1256 #if BYTEORDER == 0x1234
1257 u.c[0] = (l >> 24) & 255;
1258 u.c[1] = (l >> 16) & 255;
1259 u.c[2] = (l >> 8) & 255;
1263 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1264 croak("Unknown BYTEORDER\n");
1269 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1270 u.c[o & 0xf] = (l >> s) & 255;
1278 #ifndef CAN_PROTOTYPE
1287 char c[sizeof(long)];
1290 #if BYTEORDER == 0x1234
1291 u.c[0] = (l >> 24) & 255;
1292 u.c[1] = (l >> 16) & 255;
1293 u.c[2] = (l >> 8) & 255;
1297 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1298 croak("Unknown BYTEORDER\n");
1305 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1306 l |= (u.c[o & 0xf] & 255) << s;
1313 #endif /* BYTEORDER != 0x4321 */
1317 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1318 * If these functions are defined,
1319 * the BYTEORDER is neither 0x1234 nor 0x4321.
1320 * However, this is not assumed.
1324 #define HTOV(name,type) \
1331 char c[sizeof(type)]; \
1335 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1336 u.c[i] = (n >> s) & 0xFF; \
1341 #define VTOH(name,type) \
1348 char c[sizeof(type)]; \
1354 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1355 n += (u.c[i] & 0xFF) << s; \
1360 #if defined(HAS_HTOVS) && !defined(htovs)
1363 #if defined(HAS_HTOVL) && !defined(htovl)
1366 #if defined(HAS_VTOHS) && !defined(vtohs)
1369 #if defined(HAS_VTOHL) && !defined(vtohl)
1373 #if !defined(DOSISH) && !defined(VMS) /* VMS' my_popen() is in
1374 VMS.c, same with OS/2. */
1381 register I32 this, that;
1384 I32 doexec = strNE(cmd,"-");
1388 this = (*mode == 'w');
1393 taint_proper("Insecure %s%s", "EXEC");
1396 while ((pid = (doexec?vfork():fork())) < 0) {
1397 if (errno != EAGAIN) {
1400 croak("Can't fork");
1411 if (p[THIS] != (*mode == 'r')) {
1412 dup2(p[THIS], *mode == 'r');
1416 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1422 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1425 do_exec(cmd); /* may or may not use the shell */
1429 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1430 sv_setiv(GvSV(tmpgv),(I32)getpid());
1432 hv_clear(pidstatus); /* we have no children */
1437 do_execfree(); /* free any memory malloced by child on vfork */
1439 if (p[that] < p[this]) {
1440 dup2(p[this], p[that]);
1444 sv = *av_fetch(fdpid,p[this],TRUE);
1445 (void)SvUPGRADE(sv,SVt_IV);
1448 return fdopen(p[this], mode);
1451 #if defined(atarist)
1458 return popen(cmd, mode);
1462 #endif /* !DOSISH */
1469 struct stat tmpstatbuf;
1471 fprintf(stderr,"%s", s);
1472 for (fd = 0; fd < 32; fd++) {
1473 if (Fstat(fd,&tmpstatbuf) >= 0)
1474 fprintf(stderr," %d",fd);
1476 fprintf(stderr,"\n");
1486 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1490 return fcntl(oldfd, F_DUPFD, newfd);
1499 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1502 close(fdtmp[--fdx]);
1508 #if !defined(DOSISH) && !defined(VMS) /* VMS' my_popen() is in VMS.c */
1513 Signal_t (*hstat)(), (*istat)(), (*qstat)();
1518 svp = av_fetch(fdpid,fileno(ptr),TRUE);
1519 pid = (int)SvIVX(*svp);
1524 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1526 hstat = signal(SIGHUP, SIG_IGN);
1527 istat = signal(SIGINT, SIG_IGN);
1528 qstat = signal(SIGQUIT, SIG_IGN);
1530 pid = wait4pid(pid, &status, 0);
1531 } while (pid == -1 && errno == EINTR);
1532 signal(SIGHUP, hstat);
1533 signal(SIGINT, istat);
1534 signal(SIGQUIT, qstat);
1535 return(pid < 0 ? pid : status);
1537 #endif /* !DOSISH */
1539 #if !defined(DOSISH) || defined(OS2)
1541 wait4pid(pid,statusp,flags)
1553 sprintf(spid, "%d", pid);
1554 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1555 if (svp && *svp != &sv_undef) {
1556 *statusp = SvIVX(*svp);
1557 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1564 hv_iterinit(pidstatus);
1565 if (entry = hv_iternext(pidstatus)) {
1566 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1567 sv = hv_iterval(pidstatus,entry);
1568 *statusp = SvIVX(sv);
1569 sprintf(spid, "%d", pid);
1570 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1575 return waitpid(pid,statusp,flags);
1578 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
1583 croak("Can't do waitpid with flags");
1585 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
1586 pidgone(result,*statusp);
1595 #endif /* !DOSISH */
1606 sprintf(spid, "%d", pid);
1607 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
1608 (void)SvUPGRADE(sv,SVt_IV);
1613 #if defined(atarist) || defined(OS2)
1624 repeatcpy(to,from,len,count)
1626 register char *from;
1631 register char *frombase = from;
1639 while (count-- > 0) {
1640 for (todo = len; todo > 0; todo--) {
1647 #ifndef CASTNEGFLOAT
1655 # define BIGDOUBLE 2147483648.0
1657 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
1660 return (unsigned long)f;
1662 return (unsigned long)along;
1669 /* Look for MAX and MIN integral values. If we can't find them,
1670 we'll use 32-bit two's complement defaults.
1673 # ifdef MAXLONG /* Often used in <values.h> */
1674 # define LONG_MAX MAXLONG
1676 # define LONG_MAX 2147483647L
1681 # define LONG_MIN (-LONG_MAX - 1)
1686 # define LONG_MAX MAXULONG
1688 # define ULONG_MAX 4294967295L
1692 /* Unfortunately, on some systems the cast_uv() function doesn't
1693 work with the system-supplied definition of ULONG_MAX. The
1694 comparison (f >= ULONG_MAX) always comes out true. It must be a
1695 problem with the compiler constant folding.
1697 In any case, this workaround should be fine on any two's complement
1698 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
1700 --Andy Dougherty <doughera@lafcol.lafayette.edu>
1702 #ifndef MY_ULONG_MAX
1703 # define MY_ULONG_MAX ((UV)LONG_MAX * (UV)2 + (UV)1)
1711 return (I32) LONG_MAX;
1713 return (I32) LONG_MIN;
1722 return (IV) LONG_MAX;
1724 return (IV) LONG_MIN;
1732 if (f >= MY_ULONG_MAX)
1733 return (UV) MY_ULONG_MAX;
1745 char *fa = strrchr(a,'/');
1746 char *fb = strrchr(b,'/');
1747 struct stat tmpstatbuf1;
1748 struct stat tmpstatbuf2;
1750 #define MAXPATHLEN 1024
1752 char tmpbuf[MAXPATHLEN+1];
1767 strncpy(tmpbuf, a, fa - a);
1768 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
1773 strncpy(tmpbuf, b, fb - b);
1774 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
1776 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
1777 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
1779 #endif /* !HAS_RENAME */
1782 scan_oct(start, len, retlen)
1787 register char *s = start;
1788 register unsigned long retval = 0;
1790 while (len && *s >= '0' && *s <= '7') {
1792 retval |= *s++ - '0';
1795 if (dowarn && len && (*s == '8' || *s == '9'))
1796 warn("Illegal octal digit ignored");
1797 *retlen = s - start;
1802 scan_hex(start, len, retlen)
1807 register char *s = start;
1808 register unsigned long retval = 0;
1811 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
1813 retval |= (tmp - hexdigit) & 15;
1816 *retlen = s - start;
1821 #ifdef OLD_PTHREADS_API
1827 if (pthread_getspecific(thr_key, &t))
1828 croak("panic: pthread_getspecific");
1829 return (struct thread *) t;
1831 #endif /* OLD_PTHREADS_API */
1832 #endif /* USE_THREADS */