📄 ckclib.c
字号:
p = numbuf + numbp; q = p; s = buf + k; while ((*p++ = *s++)) ; /* Copy */ *p++ = NUL; numbp += len+1; return(q); /* Return pointer */}/* C K U L T O A -- Unsigned long to string */char *#ifdef CK_ANSICckultoa(unsigned long n)#elseckultoa(n) unsigned long n;#endif /* CK_ANSIC *//* ckltoa */ { char buf[32]; /* Internal working buffer */ char * p, * s, * q; int k, x, len = 0; buf[31] = NUL; for (k = 30; k > 0; k--) { /* Convert number to string */ x = n % 10L; buf[k] = x + '0'; n = n / 10L; if (!n) break; } len = 31 - k; if (len + numbp > NUMBUF) numbp = 0; p = numbuf + numbp; q = p; s = buf + k; while ((*p++ = *s++)) ; /* Copy */ numbp += len+1; return(q); /* Return pointer */}char *#ifdef CK_ANSICckltox(long n) /* Long int to "0x.." hex string */#elseckltox(n) long n;#endif /* CK_ANSIC *//* ckltox */ { char buf[32]; /* Internal working buffer */ char *p, *q, *s, *bp = buf + 2; int k; buf[0] = '0'; buf[1] = 'x'; sprintf(bp, "%lx", n); k = strlen(bp); if (k&1) { sprintf(bp, "0%lx", n); k++; } k += 2; /* "0x" */ if (numbp + k >= NUMBUF) numbp = 0; p = numbuf + numbp; q = p; s = buf; while ((*p++ = *s++)) ; /* Copy */ *p++ = NUL; numbp += k+1; return(q); /* Return pointer */}/* C K I T O A -- Int to string -- FOR DISCIPLINED USE ONLY */char *ckitoa(n) int n; { /* See comments with ckltoa(). */ long nn; nn = n; return(ckltoa(nn));}char * /* Unsigned int to string */ckuitoa(n) unsigned int n; { unsigned long nn; nn = n; return(ckultoa(nn));}char *ckitox(n) int n; { /* Int to hex */ long nn; nn = n; return(ckltox(nn));}char *#ifdef CK_ANSICckctoa(char c) /* Char to string */#elseckctoa(c) char c;#endif/* ckctoa */ { static char buf[32]; static int current = 0; if (current >= 30) current = 0; buf[current++] = c; buf[current++] = '\0'; return((char *)(buf + current - 2));}char *#ifdef CK_ANSICckctox(CHAR c, int flag) /* Unsigned char to hex */#elseckctox(c, flag) CHAR c; int flag;#endif/* ckctox */ { static char buf[48]; static int current = 0; int x; char h; if (current > 45) current = 0; x = (c >> 4) & 0x0f; h = rxdigits[x]; if (!flag && isupper(rxdigits[x])) h = tolower(rxdigits[x]); buf[current++] = h; x = c & 0x0f; h = rxdigits[x]; if (!flag && isupper(rxdigits[x])) h = tolower(rxdigits[x]); buf[current++] = h; buf[current++] = '\0'; return((char *)(buf + current - 3));}/* C K I N D E X -- C-Kermit's index function *//* We can't depend on C libraries to have one, so here is our own. Call with: s1 - String to look for. s2 - String to look in. t - Offset from right or left of s2, 0 based; -1 for rightmost char in s2. r - 0 for left-to-right search, non-0 for right-to-left. icase 0 for case independence, non-0 if alphabetic case matters. Returns 0 if string not found, otherwise a 1-based result. Also returns 0 on any kind of error, e.g. junk parameters.*/intckindex(s1,s2,t,r,icase) char *s1, *s2; int t, r, icase; { int len1 = 0, len2 = 0, i, j, x, ot = t; /* ot = original t */ char * s; if (!s1 || !s2) return(0); s = s1; while (*s++) len1++; /* length of string to look for */ s = s2; while (*s++) len2++; /* length of string to look in */ s = s2; if (t < 0) t = len2 - 1; j = len2 - len1; /* length difference */ if (j < 0 || (r == 0 && t > j)) /* search string is longer */ return(0); if (r == 0) { /* Index */ s = s2 + t; /* Point to beginning of target */ for (i = 0; i <= (j - t); i++) { /* Now compare */ x = ckstrcmp(s1,s++,len1,icase); if (!x) return(i+1+t); } } else { /* Reverse Index */ i = len2 - len1; /* Where to start looking */ if (ot > 0) /* Figure in offset if any */ i -= t; for (j = i; j > -1; j--) { if (!ckstrcmp(s1,&s2[j],len1,icase)) return(j+1); } } return(0);}/* C K S T R S T R -- Portable replacement for strstr() *//* Returns pointer to first occurrence of s1 in s2, or NULL */char *ckstrstr(s1, s2) char * s1, * s2; { int k; k = ckindex(s2,s1,0,0,1); return((k < 1) ? NULL : &s1[k-1]);}/* B R S T R I P -- Strip enclosing braces from arg string, in place. *//* Call with: Pointer to string that can be poked. Returns: Pointer to string without enclosing braces. If original string was not braced, this is the arg pointer; otherwise it is 1 + the arg pointer, with the matching closing brace zero'd out. If the string starts with a brace but does not end with a matching brace, the original pointer to the original string is returned. If the arg pointer is NULL, a pointer to an empty string is returned.*/#ifdef COMMENT/* This is the original version, handling only braces */char *brstrip(p) char *p; { if (!p) return(""); if (*p == '{') { int x; x = (int)strlen(p) - 1; if (p[x] == '}') { p[x] = NUL; p++; } } return(p);}#else/* New version handles braces and doublequotes */char *brstrip(p) char *p; { if (!p) return(""); if (*p == '{' || (*p == '"' && dblquo)) { int x; x = (int)strlen(p) - 1; if (x > 0) { if ((*p == '{' && p[x] == '}') || (*p == '"' && p[x] == '"')) { if (x > 0 && p[x-1] != CMDQ) { p[x] = NUL; p++; } } } } return(p);}#endif /* COMMENT */#ifdef COMMENT/* Even newer experimental version -- breaks many things */char *fnstrip(p) char *p; { int i, j, k, n, len; extern int cmd_quoting; /* Bad - no externs allowed! */ if (!p) return(""); if (*p == '{') { len = strlen(p); n = 0; for (j = 0; j < len; j++ ) { if (p[j] == '{' && (!cmd_quoting || j == 0 || p[j-1] != CMDQ)) { for (n = 1, i = j+1; i < len; i++ ) { if (p[i] == '{' && (!cmd_quoting || p[i-1] != CMDQ)) n++; else if (p[i] == '}' && (!cmd_quoting || p[i-1] != CMDQ)) { if (--n == 0) { for (k = j; k < i - 1; k++) p[k] = p[k+1]; for (; i < len; i++ ) p[i-1] = p[i+1]; len -= 2; j = i - 1; } } } } } if (n == 1) { /* Implied right brace at end of field */ for (k = j; k < len; k++) p[k] = p[k+1]; len -= 1; } } else if (*p == '"') { len = strlen(p); n = 0; for (j = 0; j < len; j++) { if (p[j] == '"' && (!cmd_quoting || j == 0 || p[j-1] != CMDQ)) { n++; for (i = j + 1; i < len; i++) { if (p[i] == '"' && (!cmd_quoting || p[i-1] != CMDQ)) { n--; for (k = j; k < i - 1; k++) p[k] = p[k+1]; for (; i < len; i++) p[i-1] = p[i+1]; len -= 2; j = i - 1; } } } } if (n == 1) { /* Implied double quote at end of field */ for (k = j; k < len; k++ ) p[k] = p[k+1]; len -= 1; } } return(p);}#endif /* COMMENT */#ifdef COMMENT/* Not used -- Note: these not only write into their arg, but write past past the end.*/char *brace(fn) char *fn; { int spaces = 0; char * p, ch, ch2; for (p = fn; *p; p++) { if (*p == SP) { spaces = 1; break; } } if (spaces) { p = fn; ch = *p; *p = '{'; p++; while (*p) { ch2 = *p; *p = ch; ch = ch2; p++; } *p = ch; p++; *p = '}'; p++; *p = '\0'; } return(fn);}#endif /* COMMENT *//* d q u o t e -- Puts doublequotes around arg in place. *//* Call with: Pointer to buffer and its total length and flag = 0 to use doublequotes, 1 to use braces. Returns: Number: length of result.*/intdquote(fn, len, flag) char *fn; int len; int flag; { int spaces = 0, k = 0; char * p, ch, ch2; if (!fn) return(0); k = strlen(fn); for (p = fn; *p; p++) { if (*p == SP) { spaces = 1; break; } } if (spaces) { if (k + 2 >= len) return(k); p = fn; ch = *p; *p = flag ? '{' : '"'; p++; while (*p) { ch2 = *p; *p = ch; ch = ch2; p++; } *p = ch; p++; *p = flag ? '}' : '"'; p++; *p = '\0'; } return(k+2);}/* U N T A B I F Y --- Untabify s1 into s2, assuming tabs every 8 space */intuntabify(s1,s2,max) char * s1, * s2; int max; { int i, j, k, x, z; x = strlen(s1); for (i = 0, k = 0; k < x; k++) { if (s1[k] != '\t') { if (i >= max-1) { s2[max-1] = '\0'; return(-1); } s2[i++] = s1[k]; continue; } z = 8 - i%8; if (z == 0) z = 8; for (j = 0; j < z && i < max; j++) s2[i++] = ' '; } s2[i] = '\0'; return(0);}/* M A K E L I S T --- Breaks {{s1}{s2}..{sn}} into an array of strings *//* Call with: s = pointer to string to break up. list = array of string pointers. len = number of elements in array. NOTE: The array must be preinitialized to all NULL pointers. If any array element is not NULL, it is assumed to have been malloc'd and is therefore freed. Do NOT call this function with an uninitialized array, or with an array that has had any static elements assigned to it.*/VOIDmakelist(s,list,len) char * s; char *list[]; int len; { int i, n, q, bc = 0; char *p = NULL, *s2 = NULL; debug(F110,"makelist s",s,0); if (!s) { /* Check for null or empty string */ list[0] = NULL; return; } n = strlen(s); if (n == 0) { list[0] = NULL; return; } if ((s2 = (char *)malloc(n+1))) { /* Safe copy for poking */ strcpy(s2,s); /* (no need for ckstrncpy here) */ s = s2; } s = brstrip(s); /* Strip braces */ n = strlen(s); /* Get length */ if (*s != '{') { /* Outer braces only */ if ((p = (char *)malloc(n+1))) { /* So just one pattern */ strcpy(p,s); /* (no need for ckstrncpy here) */ if (list[0]) free(list[0]); list[0] = p; } if (s2) free(s2); return; } q = 0; /* Inner ones too */ i = 0; /* so a list of patterns. */ n = 0; while (*s && i < len) { if (*s == CMDQ) { /* Quote... */ q = 1; s++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -