util.c

来自「早期freebsd实现」· C语言 代码 · 共 1,778 行 · 第 1/3 页

C
1,778 行
字号
	return Nullch;    s = big + littlelen;    oldlittle = little = table - 2;    if (littlestr->str_pok & SP_CASEFOLD) {	/* case insensitive? */	if (s < bigend) {	  top1:	    /*SUPPRESS 560*/	    if (tmp = table[*s]) {#ifdef POINTERRIGOR		if (bigend - s > tmp) {		    s += tmp;		    goto top1;		}#else		if ((s += tmp) < bigend)		    goto top1;#endif		return Nullch;	    }	    else {		tmp = littlelen;	/* less expensive than calling strncmp() */		olds = s;		while (tmp--) {		    if (*--s == *--little || fold[*s] == *little)			continue;		    s = olds + 1;	/* here we pay the price for failure */		    little = oldlittle;		    if (s < bigend)	/* fake up continue to outer loop */			goto top1;		    return Nullch;		}#ifndef lint		return (char *)s;#endif	    }	}    }    else {	if (s < bigend) {	  top2:	    /*SUPPRESS 560*/	    if (tmp = table[*s]) {#ifdef POINTERRIGOR		if (bigend - s > tmp) {		    s += tmp;		    goto top2;		}#else		if ((s += tmp) < bigend)		    goto top2;#endif		return Nullch;	    }	    else {		tmp = littlelen;	/* less expensive than calling strncmp() */		olds = s;		while (tmp--) {		    if (*--s == *--little)			continue;		    s = olds + 1;	/* here we pay the price for failure */		    little = oldlittle;		    if (s < bigend)	/* fake up continue to outer loop */			goto top2;		    return Nullch;		}#ifndef lint		return (char *)s;#endif	    }	}    }    return Nullch;}char *screaminstr(bigstr, littlestr)STR *bigstr;STR *littlestr;{    register unsigned char *s, *x;    register unsigned char *big;    register int pos;    register int previous;    register int first;    register unsigned char *little;    register unsigned char *bigend;    register unsigned char *littleend;    if ((pos = screamfirst[littlestr->str_rare]) < 0) 	return Nullch;#ifndef lint    little = (unsigned char *)(littlestr->str_ptr);#else    little = Null(unsigned char *);#endif    littleend = little + littlestr->str_cur;    first = *little++;    previous = littlestr->str_state;#ifndef lint    big = (unsigned char *)(bigstr->str_ptr);#else    big = Null(unsigned char*);#endif    bigend = big + bigstr->str_cur;    while (pos < previous) {#ifndef lint	if (!(pos += screamnext[pos]))#endif	    return Nullch;    }#ifdef POINTERRIGOR    if (littlestr->str_pok & SP_CASEFOLD) {	/* case insignificant? */	do {	    if (big[pos-previous] != first && big[pos-previous] != fold[first])		continue;	    for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {		if (x >= bigend)		    return Nullch;		if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {		    s--;		    break;		}	    }	    if (s == littleend)#ifndef lint		return (char *)(big+pos-previous);#else		return Nullch;#endif	} while (#ifndef lint		pos += screamnext[pos]	/* does this goof up anywhere? */#else		pos += screamnext[0]#endif	    );    }    else {	do {	    if (big[pos-previous] != first)		continue;	    for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {		if (x >= bigend)		    return Nullch;		if (*s++ != *x++) {		    s--;		    break;		}	    }	    if (s == littleend)#ifndef lint		return (char *)(big+pos-previous);#else		return Nullch;#endif	} while (#ifndef lint		pos += screamnext[pos]#else		pos += screamnext[0]#endif	    );    }#else /* !POINTERRIGOR */    big -= previous;    if (littlestr->str_pok & SP_CASEFOLD) {	/* case insignificant? */	do {	    if (big[pos] != first && big[pos] != fold[first])		continue;	    for (x=big+pos+1,s=little; s < littleend; /**/ ) {		if (x >= bigend)		    return Nullch;		if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {		    s--;		    break;		}	    }	    if (s == littleend)#ifndef lint		return (char *)(big+pos);#else		return Nullch;#endif	} while (#ifndef lint		pos += screamnext[pos]	/* does this goof up anywhere? */#else		pos += screamnext[0]#endif	    );    }    else {	do {	    if (big[pos] != first)		continue;	    for (x=big+pos+1,s=little; s < littleend; /**/ ) {		if (x >= bigend)		    return Nullch;		if (*s++ != *x++) {		    s--;		    break;		}	    }	    if (s == littleend)#ifndef lint		return (char *)(big+pos);#else		return Nullch;#endif	} while (#ifndef lint		pos += screamnext[pos]#else		pos += screamnext[0]#endif	    );    }#endif /* POINTERRIGOR */    return Nullch;}/* copy a string to a safe spot */char *savestr(str)char *str;{    register char *newaddr;    New(902,newaddr,strlen(str)+1,char);    (void)strcpy(newaddr,str);    return newaddr;}/* same thing but with a known length */char *nsavestr(str, len)char *str;register int len;{    register char *newaddr;    New(903,newaddr,len+1,char);    Copy(str,newaddr,len,char);		/* might not be null terminated */    newaddr[len] = '\0';		/* is now */    return newaddr;}/* grow a static string to at least a certain length */voidgrowstr(strptr,curlen,newlen)char **strptr;int *curlen;int newlen;{    if (newlen > *curlen) {		/* need more room? */	if (*curlen)	    Renew(*strptr,newlen,char);	else	    New(905,*strptr,newlen,char);	*curlen = newlen;    }}#ifndef I_VARARGS/*VARARGS1*/char *mess(pat,a1,a2,a3,a4)char *pat;long a1, a2, a3, a4;{    char *s;    int usermess = strEQ(pat,"%s");    STR *tmpstr;    s = buf;    if (usermess) {	tmpstr = str_mortal(&str_undef);	str_set(tmpstr, (char*)a1);	*s++ = tmpstr->str_ptr[tmpstr->str_cur-1];    }    else {	(void)sprintf(s,pat,a1,a2,a3,a4);	s += strlen(s);    }    if (s[-1] != '\n') {	if (curcmd->c_line) {	    (void)sprintf(s," at %s line %ld",	      stab_val(curcmd->c_filestab)->str_ptr, (long)curcmd->c_line);	    s += strlen(s);	}	if (last_in_stab &&	    stab_io(last_in_stab) &&	    stab_io(last_in_stab)->lines ) {	    (void)sprintf(s,", <%s> line %ld",	      last_in_stab == argvstab ? "" : stab_ename(last_in_stab),	      (long)stab_io(last_in_stab)->lines);	    s += strlen(s);	}	(void)strcpy(s,".\n");	if (usermess)	    str_cat(tmpstr,buf+1);    }    if (usermess)	return tmpstr->str_ptr;    else	return buf;}/*VARARGS1*/void fatal(pat,a1,a2,a3,a4)char *pat;long a1, a2, a3, a4;{    extern FILE *e_fp;    extern char *e_tmpname;    char *tmps;    char *message;    message = mess(pat,a1,a2,a3,a4);    if (in_eval) {	str_set(stab_val(stabent("@",TRUE)),message);	tmps = "_EVAL_";	while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||	  strNE(tmps,loop_stack[loop_ptr].loop_label) )) {#ifdef DEBUGGING	    if (debug & 4) {		deb("(Skipping label #%d %s)\n",loop_ptr,		    loop_stack[loop_ptr].loop_label);	    }#endif	    loop_ptr--;	}#ifdef DEBUGGING	if (debug & 4) {	    deb("(Found label #%d %s)\n",loop_ptr,		loop_stack[loop_ptr].loop_label);	}#endif	if (loop_ptr < 0) {	    in_eval = 0;	    fatal("Bad label: %s", tmps);	}	longjmp(loop_stack[loop_ptr].loop_env, 1);    }    fputs(message,stderr);    (void)fflush(stderr);    if (e_fp)	(void)UNLINK(e_tmpname);    statusvalue >>= 8;    exit((int)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));}/*VARARGS1*/void warn(pat,a1,a2,a3,a4)char *pat;long a1, a2, a3, a4;{    char *message;    message = mess(pat,a1,a2,a3,a4);    fputs(message,stderr);#ifdef LEAKTEST#ifdef DEBUGGING    if (debug & 4096)	xstat();#endif#endif    (void)fflush(stderr);}#else/*VARARGS0*/char *mess(args)va_list args;{    char *pat;    char *s;    STR *tmpstr;    int usermess;#ifndef HAS_VPRINTF#ifdef CHARVSPRINTF    char *vsprintf();#else    int vsprintf();#endif#endif#ifdef lint    pat = Nullch;#else    pat = va_arg(args, char *);#endif    s = buf;    usermess = strEQ(pat, "%s");    if (usermess) {	tmpstr = str_mortal(&str_undef);	str_set(tmpstr, va_arg(args, char *));	*s++ = tmpstr->str_ptr[tmpstr->str_cur-1];    }    else {	(void) vsprintf(s,pat,args);	s += strlen(s);    }    if (s[-1] != '\n') {	if (curcmd->c_line) {	    (void)sprintf(s," at %s line %ld",	      stab_val(curcmd->c_filestab)->str_ptr, (long)curcmd->c_line);	    s += strlen(s);	}	if (last_in_stab &&	    stab_io(last_in_stab) &&	    stab_io(last_in_stab)->lines ) {	    (void)sprintf(s,", <%s> line %ld",	      last_in_stab == argvstab ? "" : last_in_stab->str_magic->str_ptr,	      (long)stab_io(last_in_stab)->lines);	    s += strlen(s);	}	(void)strcpy(s,".\n");	if (usermess)	    str_cat(tmpstr,buf+1);    }    if (usermess)	return tmpstr->str_ptr;    else	return buf;}/*VARARGS0*/void fatal(va_alist)va_dcl{    va_list args;    extern FILE *e_fp;    extern char *e_tmpname;    char *tmps;    char *message;#ifndef lint    va_start(args);#else    args = 0;#endif    message = mess(args);    va_end(args);    if (in_eval) {	str_set(stab_val(stabent("@",TRUE)),message);	tmps = "_EVAL_";	while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||	  strNE(tmps,loop_stack[loop_ptr].loop_label) )) {#ifdef DEBUGGING	    if (debug & 4) {		deb("(Skipping label #%d %s)\n",loop_ptr,		    loop_stack[loop_ptr].loop_label);	    }#endif	    loop_ptr--;	}#ifdef DEBUGGING	if (debug & 4) {	    deb("(Found label #%d %s)\n",loop_ptr,		loop_stack[loop_ptr].loop_label);	}#endif	if (loop_ptr < 0) {	    in_eval = 0;	    fatal("Bad label: %s", tmps);	}	longjmp(loop_stack[loop_ptr].loop_env, 1);    }    fputs(message,stderr);    (void)fflush(stderr);    if (e_fp)	(void)UNLINK(e_tmpname);    statusvalue >>= 8;    exit((int)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));}/*VARARGS0*/void warn(va_alist)va_dcl{    va_list args;    char *message;#ifndef lint    va_start(args);#else    args = 0;#endif    message = mess(args);    va_end(args);    fputs(message,stderr);#ifdef LEAKTEST#ifdef DEBUGGING    if (debug & 4096)	xstat();#endif#endif    (void)fflush(stderr);}#endifvoidmy_setenv(nam,val)char *nam, *val;{    register int i=envix(nam);		/* where does it go? */    if (environ == origenviron) {	/* need we copy environment? */	int j;	int max;	char **tmpenv;	/*SUPPRESS 530*/	for (max = i; environ[max]; max++) ;	New(901,tmpenv, max+2, char*);	for (j=0; j<max; j++)		/* copy environment */	    tmpenv[j] = savestr(environ[j]);	tmpenv[max] = Nullch;	environ = tmpenv;		/* tell exec where it is now */    }    if (!val) {	while (environ[i]) {	    environ[i] = environ[i+1];	    i++;	}	return;    }    if (!environ[i]) {			/* does not exist yet */	Renew(environ, i+2, char*);	/* just expand it a bit */	environ[i+1] = Nullch;	/* make sure it's null terminated */    }    else	Safefree(environ[i]);    New(904, environ[i], strlen(nam) + strlen(val) + 2, char);#ifndef MSDOS    (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */#else    /* MS-DOS requires environment variable names to be in uppercase */    /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but     * some utilities and applications may break because they only look     * for upper case strings. (Fixed strupr() bug here.)]     */    strcpy(environ[i],nam); strupr(environ[i]);    (void)sprintf(environ[i] + strlen(nam),"=%s",val);#endif /* MSDOS */}intenvix(nam)char *nam;{    register int i, len = strlen(nam);    for (i = 0; environ[i]; i++) {	if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')	    break;			/* strnEQ must come first to avoid */    }					/* potential SEGV's */    return i;}#ifdef EUNICEintunlnk(f)	/* unlink all versions of a file */char *f;{    int i;    for (i = 0; unlink(f) >= 0; i++) ;    return i ? 0 : -1;}#endif#if !defined(HAS_BCOPY) || !defined(SAFE_BCOPY)char *my_bcopy(from,to,len)register char *from;register char *to;register int len;{    char *retval = to;    if (from - to >= 0) {	while (len--)	    *to++ = *from++;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?