⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckclib.c

📁 KERMIT工具 这在办公室下载不了,很多人都没有载不到.
💻 C
📖 第 1 页 / 共 5 页
字号:
	    n++;	    continue;	}	if (*s == '{' && !q) {		/* Opening brace */	    if (bc++ == 0) {		/* Beginning of a group */		p = ++s;		n = 0;	    } else {			/* It's a brace inside the group */		n++;		s++;	    }	    continue;	} else if (*s == '}' && !q) {	/* Closing brace */	    if (--bc == 0) {		/* End of a group */		*s++ = NUL;		debug(F111,"makelist element",p,i);		if (list[i])		  free(list[i]);		if ((list[i] = (char *)malloc(n+1))) {		    ckstrncpy(list[i],p,n+1); /* Note: n+1 */		    i++;		}		while (*s == SP) s++;		p = s;		n = 0;		continue;	    } else {			/* Within a group */		n++;		s++;	    }	} else {			/* Regular character */	    q = 0;	    s++;	    n++;	}    }    if (*p && i < len) {		/* Last one */	if (list[i])	  free(list[i]);	if ((list[i] = (char *)malloc(n+1))) {	    ckstrncpy(list[i],p,n+1);	    debug(F111,"makelist last element",p,i);	}    }    i++;				/* Clear out the rest of the list */    for ( ; i < len; i++) {	if (list[i])	  free (list[i]);	list[i] = NULL;    }    if (s2) free(s2);}/*   M A K E S T R  --  Creates a dynamically allocated string.   Makes a new copy of string s and sets pointer p to its address.   Handles degenerate cases, like when buffers overlap or are the same,   one or both arguments are NULL, etc.   The source string is assumed to be NUL-terminated.  Therefore it can not   be a UCS-2 string or arbitrary binary data.   The target pointer must be either NULL or else a pointer to a previously   malloc'ed buffer.  If not, expect a core dump or segmentation fault.   Note: The caller can tell whether this routine failed as follows:     malloc(&p,q);     if (q & !p) { makestr() failed };   Really this routine should have returned a length, but since it doesn't   we set the global variable makestrlen to the length of the result string.*/int makestrlen = 0;VOID#ifdef CK_ANSICmakestr(char **p, const char *s)#elsemakestr(p,s) char **p, *s;#endif/* makestr */ {    int x = 0;    char *q = NULL;#ifdef CK_ANSIC    register const char * s2;#else    register char * s2;#endif /* CK_ANSIC */    register char * q2;    if (*p == s)			/* The two pointers are the same. */      return;				/* Don't do anything. */    if (!s) {				/* New definition is null? */	if (*p)				/* Free old storage. */	  free(*p);	*p = NULL;			/* Return null pointer. */	makestrlen = 0;	return;    }    s2 = s;				/* Maybe new string will fit */#ifdef COMMENT/*  This is a fairly big win, allowing us to skip the malloc() and free if the  destination string already exists and is not shorter than the source string.  But it doesn't allow for possible overlap of source and destination.*/    if (*p) {				/* into old storage... */	char * p2 = *p;	char c;	while (c = *p2) {	    if (!(*p2++ = *s2++))	      break;	    x++;	}	makestrlen = x;	if (c) return;    }#endif /* COMMENT *//* Didn't fit */    x = 0;    while (*s2++) x++;			/* Get (rest of) length of s.  */    if (x >= 0) {			/* Get length, even of empty string. */	q = malloc(x+1);		/* Get and point to temp storage. */	if (q) {	    makestrlen = x;		/* Remember length for stats */	    s2 = s;			/* Point back to beginning of source */	    q2 = q;			/* Copy dest pointer to increment... */	    while ((*q2++ = *s2++)) ;	/* Instead of calling strcpy(). *//*  Note: HP flexelint says that the above loop can result in creation (++) and  access (*) of out-of-bounds pointers.  I really don't see it.*/	}#ifdef DEBUG	else {				/* This would be a really bad error */	    char tmp[24];		/* So get a good record of it. */	    if (x > 23) {		ckstrncpy(tmp,s,20);		strcpy(tmp+20,"...");		tmp[23] = NUL;	    } else {		strcpy(tmp,s);		/* We already checked the length */	    }	    debug(F110,"MAKESTR MALLOC FAILURE ",tmp,0);	}#endif /* DEBUG */    } else      q = NULL;				/* Length of string is zero */    if (*p)				/* Now free the original storage. */      free(*p);    *p = q;}/*  X M A K E S T R  --  Non-destructive makestr() if s is NULL.  */VOID#ifdef CK_ANSICxmakestr(char **p, const char *s)#elsexmakestr(p,s) char **p, *s;#endif/* xmakestr */ {    if (s) makestr(p,s);}#ifndef USE_MEMCPY/* C K M E M C P Y  --  Portable (but slow) memcpy() *//* Copies n bytes from s to p, allowing for overlap. *//* For use when real memcpy() not available. */VOIDckmemcpy(p,s,n) char *p, *s; int n; {    char * q = NULL;    register int i;    int x;    if (!s || !p || n <= 0 || p == s)	/* Verify args */      return;    x = p - s;				/* Check for overlap */    if (x < 0)      x = 0 - x;    if (x < n) {			/* They overlap */	q = p;	if (!(p = (char *)malloc(n)))	/* So use a temporary buffer */	  return;    }    for (i = 0; i < n; i++)		/* Copy n bytes */      p[i] = s[i];    if (q) {				/* If we used a temporary buffer */	for (i = 0; i < n; i++)		/* copy from it to destination */	  q[i] = p[i];	if (p) free(p);			/* and free the temporary buffer */    }}#endif /* USE_MEMCPY *//*  C K S T R C M P  --  String comparison with case-matters selection *//*  Call with pointers to the two strings, s1 and s2, a length, n,  and c == 0 for caseless comparison, nonzero for case matters.  Call with n == -1 to compare without a length limit.  Compares up to n characters of the two strings and returns:    1 if s1 > s2    0 if s1 = s2   -1 if s1 < s2  Note: case handling is only as good as isupper() and tolower().*/intckstrcmp(s1,s2,n,c) char *s1, *s2; register int n, c; {    register CHAR t1, t2;    if (n == 0) return(0);    if (!s1) s1 = "";			/* Watch out for null pointers. */    if (!s2) s2 = "";    if (!*s1) return(*s2 ? -1 : 0);    if (!*s2) return(1);    while (n--) {	t1 = (CHAR) *s1++;		/* Get next character from each. */	t2 = (CHAR) *s2++;	if (!t1) return(t2 ? -1 : 0);	if (!t2) return(1);	if (!c) {			/* If case doesn't matter */	    if (isupper(t1)) t1 = tolower(t1); /* Convert case. */	    if (isupper(t2)) t2 = tolower(t2);	}	if (t1 < t2) return(-1);	/* s1 < s2 */	if (t1 > t2) return(1);		/* s1 > s2 */    }    return(0);				/* They're equal */}/*  C K S T R P R E  --  Caseless string prefix comparison  *//* Returns position of the first char in the 2 strings that doesn't match */intckstrpre(s1,s2) char *s1, *s2; {    CHAR t1, t2;    int n = 0;    if (!s1) s1 = "";    if (!s2) s2 = "";    while (1) {	t1 = (CHAR) *s1++;	t2 = (CHAR) *s2++;	if (!t1 || !t2) return(n);	if (isupper(t1)) t1 = tolower(t1);	if (isupper(t2)) t2 = tolower(t2);	if (t1 != t2)	  return(n);	n++;    }}#define GLOBBING/*  C K M A T C H  --  Match a string against a pattern  *//*  Call with:    pattern to be matched.    string to look for the pattern in.    icase is 1 if case-sensitive, 0 otherwise.    opts is a bitmask:      Bit 0 (=1):	1 = Match strings starting with '.'	0 = Don't match them (used with UNIX filenames).      Bit 1 (=2):	1 = File globbing (dirseps are fences);	0 = Dirseps are not fences.      Bit 2 (=4):	1 = Allow ^ and $ anchors at beginning and end of pattern.	0 = Don't allow them (normal case for filename matching).      Bit 3 (and beyond): Undefined.  Works only with NUL-terminated strings.  Pattern may contain any number of ? and/or *.  If CKREGEX is defined, also [abc], [a-z], and/or {string,string,...}.  (Note: REGEX is a misnomer, see below.)  Returns:    0 if string does not match pattern,    >= 1, the 1-based position in the string where the match was found.  To be done:    Find a way to identify the piece of the string that matched the pattern,    as in Snobol "LINE (PAT . RESULT)".  This is now partially done by    setting matchpos and matchend (except matchend needs some tuning).  But    these are useless unless a copy of the string is kept, or a copy of the    matching part is made.  But that would be too costly in performance --    this routine has to be fast because it's used for wildcard expansion.  Note:    Patterns are not the same as regular expressions, in which '*' means    0 or more repetitions of the preceding item.  For example "a*b" as a    pattern matches any string that starts with 'a' and ends with 'b'; as a    regular expression it matches any string of zero or more a's followed by    one b.  Regular expressions are especially useful in matching strings of    (say) digits, or letters, e.g. "[0-9]*" matches any string of digits.*/static char * mypat = NULL;		/* For rewriting pattern */static int matchpos = 0;int matchend = 0;static int matchdepth = 0;static int stringpos = 0;static char * ostring = NULL;#define MATCHRETURN(x,y) { rc=y; where=x; goto xckmatch; }static char * lastpat = NULL;intckmatch(pattern, string, icase, opts) char *pattern,*string; int icase, opts; {    int q = 0, i = 0, k = -1, x, flag = 0;    int rc = 0;				/* Return code */    int where = -1;    CHAR cp;				/* Current character from pattern */    CHAR cs;				/* Current character from string */    int plen, dot, globbing, xstar = 0;    int bronly = 0;			/* Whole pattern is {a,b,c,...} */    debug(F111,"CKMATCH ENTRY pat opt",pattern,opts);    debug(F111,"CKMATCH ENTRY str dep",string,matchdepth);    /* debug(F101,"CKMATCH ENTRY icase","",icase); */    globbing = opts & 2;    if (!string) string = "";    if (!pattern) pattern = "";    if (!*pattern) {			/* Empty pattern matches anything */	matchdepth++;			/* (it wasn't incremented yet) */	MATCHRETURN(0,1);    }    if (matchdepth == 0) {		/* Top-level call? */	stringpos = 0;			/* Reset indices etc. */	matchpos = 0;	matchend = 0;	ostring = string;	lastpat = pattern;	if (*pattern == '{')		/* Entire pattern is {a,b.c} */	  bronly = 1;			/* Maybe */	dot = (opts & 1) ||		/* Match leading dot (if file) */	    (opts & 2 == 0) ||		/* always if not file */	    (pattern[0] == '.');	/* or if pattern starts with '.' */	plen = strlen(pattern);		/* Length of pattern *//* This would be used in calculating length of matching segment */	if (plen > 0)			/* User's pattern ends with '*' */	  if (pattern[plen - 1] == '*')	    xstar = 1;	if (pattern[0] == '*') {	/* User's pattern starts with '*' */	    matchpos = 1;	    debug(F111,"CKMATCH 1",string, matchpos);	}	if (opts & 4) {			/* ^..$ allowed (top level only) */	    /* Rewrite pattern to account for ^..$ anchoring... */	    if (mypat) free(mypat);	/* Get space for "*pattern*" */	    mypat = (char *)malloc(plen + 4);	    if (mypat) {		/* Got space? */		char * s = pattern, * p = mypat; /* Set working pointers */		if (*s == '^') {	/* First source char is ^ */		    s++;		/* so skip past it */		} else if (*s != '*') {	/* otherwise */		    *p++ = '*';		/* prepend '*' to pattern */		}		while (*s) {		/* Copy rest of pattern */		    if (!*(s+1)) {	/* Final pattern character? */			if (*s != '$') { /* If it's not '$' */			    *p++ = *s;	/* Copy it into the pattern */			    if (*s++ != '*') /* And if it's also not '*' */			      *p++ = '*'; /* append '*'. */			}			break;		/* Done */		    } else		/* Not final character */		      *p++ = *s++;	/* Just copy it */		}		*p = NUL;		/* Terminate the new pattern */		pattern = mypat;	/* Make the switch */	    }	    debug(F110,"CKMATCH INIT pat",pattern,0);	}    }    matchdepth++;			/* Now increment call depth */#ifdef UNIX    if (!dot) {				/* For UNIX file globbing */	if (*string == '.' && *pattern != '.' && !matchdot) {	    if (#ifdef CKREGEX		*pattern != '{' && *pattern != '['#else		1#endif /* CKREGEX */		) {		debug(F110,"ckmatch skip",string,0);		MATCHRETURN(1,0);	    }	}    }#endif /* UNIX */    while (1) {	k++;	cp = *pattern;			/* Character from pattern */	cs = *string;			/* Character from string */#ifdef COMMENT	debug(F000,"CKMATCH pat cp",pattern,cp);	debug(F000,"CKMATCH str cs",string,cs);#endif /* COMMENT */	if (!cs) {			/* End of string - done. */	    x = (!cp || (cp == '*' && !*(pattern+1))) ? 1 : 0;	    if (x) {		if (!matchpos) {		    matchpos = stringpos;		    debug(F111,"CKMATCH A",string, matchpos);		}		matchend = stringpos;		MATCHRETURN(2,matchpos);	    }	    debug(F111,"CKMATCH ZERO d",string, matchpos);	    matchpos = 0;	    MATCHRETURN(16,matchpos);	}        if (!icase) {			/* If ignoring case */	    if (isupper(cp))		/* convert both to lowercase. */	      cp = tolower(cp);	    if (isupper(cs))	      cs = tolower(cs);        }	if (q) {			/* This character was quoted */	    debug(F000,"CKMATCH QUOTED",pattern,cp);	    q = 0;			/* Turn off quote flag */	    if (cs == cp) {		/* Compare directly */		if (!matchpos) {	/* Matches */		    matchpos = stringpos;		    debug(F111,"CKMATCH \\ new match",string, matchpos);		}		pattern++;	    } else {			/* Doesn't match */		pattern = lastpat;	/* Back up the pattern */		matchpos = 0;		debug(F111,"CKMATCH \\ no match",pattern, matchpos);	    }	    string++;	    stringpos++;	    continue;	}	if (cp == CMDQ && !q) {		/* Quote in pattern */	    debug(F000,"CKMATCH QUOTE",pattern,cp);	    q = 1;			/* Set flag */	    pattern++;			/* Advance to next pattern character */	    continue;			/* and continue. */	}	if (cs && cp == '?') {		/* '?' matches any char */	    if (!matchpos) {		matchpos = stringpos;		debug(F111,"CKMATCH D",string, matchpos);	    }	    debug(F110,"CKMATCH ? pat",pattern,0);	    debug(F110,"CKMATCH ? str",string,0);	    pattern++, string++;	    stringpos++;	    continue;#ifdef CKREGEX	} else if (cp == '[') {		/* Have bracket */	    int q = 0;			/* My own private q */	    char * psave = NULL;	/* and backup pointer */	    CHAR clist[256];		/* Character list from brackets */	    CHAR c, c1, c2;	    for (i = 0; i < 256; i++)	/* memset() etc not portable */	      clist[i] = NUL;	    psave = ++pattern;		/* Where pattern starts */	    debug(F111,"CKMATCH [] ",pattern-1, matchpos);

⌨️ 快捷键说明

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