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

📄 ckclib.c

📁 KERMIT工具 这在办公室下载不了,很多人都没有载不到.
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (!s) return;			/* Nothing to sort? */    if (n < 2) return;			/* Not enough elements to sort? */    if (k < 0) k = 0;			/* Key */    m = n;				/* Initial group size is whole array */    while (1) {	m = m / 2;			/* Divide group size in half */	if (m < 1)			/* Small as can be, so done */	  break;	for (j = 0; j < n-m; j++) {	/* Sort each group */	    t = t2 = s[j+m];		/* Compare this one... */	    if (!t)			/* But if it's NULL */	      t2 = "";			/* make it the empty string */	    if (p)			/* Handle parallel array, if any */	      u = p[j+m];	    if (k > 0 && *t2) {		if ((int)strlen(t2) < k) /* If key too big */		  t2 = "";		/* make key the empty string */		else			/* Key is in string */		  t2 = t + k;		/* so point to key position */	    }	    for (i = j; i >= 0; i -= m) { /* Loop thru comparands s[i..]*/		t1 = s[i];		if (!t1)		/* Same deal */		  t1 = "";		if (k > 0 && *t1) {		    if ((int)strlen(t1) < k)		      t1 = "";		    else		      t1 = s[i]+k;		}		if (c == 2) {		/* Numeric comparison */		    x = 0;#ifdef CKFLOAT		    f2 = 0.0;		    f1 = 0.0;		    if (isfloat(t1,1)) {			f1 = floatval;			if (isfloat(t2,1))			  f2 = floatval;			else			  f1 = 0.0;		    }		    if (f2 < f1)		      x = 1;		    else		      x = -1;#else		    n2 = 0L;		    n1 = 0L;		    if (rdigits(t1)) {			n1 = atol(t1);			if (rdigits(t2))			  n2 = atol(t2);			else			  n1 = 0L;		    }		    if (n2 < n1)		      x = 1;		    else		      x = -1;#endif /* CKFLOAT */		} else {		    x = ckstrcmp(t1,t2,-1,c); /* Compare */		}		if (r == 0 && x < 0)		  break;		if (r != 0 && x > 0)		  break;		s[i+m] = s[i];		if (p) p[i+m] = p[i];	    }	    s[i+m] = t;	    if (p) p[i+m] = u;	}    }}/* C K R A D I X  --  Radix converter *//*   Call with:     s:   a number in string format.     in:  int, specifying the radix of s, 2-36.     out: int, specifying the radix to convert to, 2-36.   Returns:     NULL on error (illegal radix, illegal number, etc.).     "-1" on overflow (number too big for unsigned long).     Otherwise: Pointer to result.*/char *ckradix(s,in,out) char * s; int in, out; {    char c, *r = rxresult;    int d, minus = 0;    unsigned long zz = 0L;    long z = 0L;    if (in < 2 || in > 36)		/* Verify legal input radix */      return(NULL);    if (out < 2 || out > 36)		/* and output radix. */      return(NULL);    if (*s == '+') {			/* Get sign if any */	s++;    } else if (*s == '-') {	minus++;	s++;    }    while (*s == SP || *s == '0')	/* Trim leading blanks or 0's */      s++;/*  For detecting overflow, we use a signed copy of the unsigned long  accumulator.  If it goes negative, we know we'll overflow NEXT time  through the loop.*/    for (; *s;  s++) {			/* Convert from input radix to */	c = *s;				/* unsigned long */	if (islower(c)) c = toupper(c);	if (c >= '0' && c <= '9')	  d = c - '0';	else if (c >= 'A' && c <= 'Z')	  d = c - 'A' + 10;	else	  return(NULL);	if (d >= in)			/* Check for illegal digit */	  return(NULL);	zz = zz * in + d;	if (z < 0L)			/* Clever(?) overflow detector */	  return("-1");        z = zz;    }    if (!zz) return("0");    r = &rxresult[RXRESULT];		/* Convert from unsigned long */    *r-- = NUL;				/* to output radix. */    while (zz > 0 && r > rxresult) {	d = zz % (unsigned)out;	*r-- = rxdigits[d];	zz = zz / (unsigned)out;    }    if (minus) *r-- = '-';		/* Replace original sign */    return((char *)(r+1));}#ifndef NOB64/* Base-64 conversion routines */static char b64[] = {			/* Encoding vector */#ifdef pdp11  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="#else  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',  'T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l',  'm','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4',  '5','6','7','8','9','+','/','=','\0'#endif /* pdp11 */};static int b64tbl[] = {			/* Decoding vector */    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,    -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};/*   B 8 T O B 6 4  --  Converts 8-bit data to Base64 encoding.   Call with:     s   = Pointer to 8-bit data;     n   = Number of source bytes to encode (SEE NOTE).           If it's a null-terminated string, you can use -1 here.     out = Address of output buffer.     len = Length of output buffer (should > 4/3 longer than input).   Returns:     >= 0 if OK, number of bytes placed in output buffer,          with the subsequent byte set to NUL if space permits.     -1 on error (output buffer space exhausted).   NOTE:     If this function is to be called repeatedly, e.g. to encode a data     stream a chunk at a time, the source length must be a multiple of 3     in all calls but the final one to avoid the generation of extraneous     pad characters that would throw the decoder out of sync.  When encoding     only a single string, this is not a consideration.  No internal state     is kept, so there is no reset function.*/intb8tob64(s,n,out,len) char * s,* out; int n, len; {    int b3, b4, i, x = 0;    unsigned int t;    if (n < 0) n = strlen(s);    for (i = 0; i < n; i += 3,x += 4) { /* Loop through source bytes */	b3 = b4 = 0;	t = (unsigned)((unsigned)((unsigned int)s[i] & 0xff) << 8);	if (n - 1 > i) {		/* Do we have another after this? */            t |= (unsigned)(s[i+1] & 0xff); /* Yes, OR it in */            b3 = 1;			/* And remember */        }        t <<= 8;			/* Move over */        if (n - 2 > i) {		/* Another one after that? */            t |= (unsigned)(s[i+2] & 0xff); /* Yes, OR it in */            b4 = 1;			/* and remember */        }	if (x + 4 > len)		/* Check output space */	  return(-1);	out[x+3] = b64[b4 ? (t & 0x3f) : 64]; /* 64 = code for '=' */        t >>= 6;        out[x+2] = b64[b3 ? (t & 0x3f) : 64];        t >>= 6;        out[x+1] = b64[t & 0x3f];        t >>= 6;        out[x]   = b64[t & 0x3f];    }    if (x < len) out[x] = NUL;		/* Null-terminate the string */    return(x);}/*   B 6 4 T O B 8  --  Converts Base64 string to 8-bit data.   Call with:     s   = pointer to Base64 string (whitespace ignored).     n   = length of string, or -1 if null terminated, or 0 to reset.     out = address of output buffer.     len = length of output buffer.   Returns:     >= 0 if OK, number of bytes placed in output buffer,          with the subsequent byte set to NUL if space permits.     <  0 on error:       -1 = output buffer too small for input.       -2 = input contains illegal characters.       -3 = internal coding error.   NOTE:     Can be called repeatedly to decode a Base64 stream, one chunk at a     time.  However, if it is to be called for multiple streams in     succession, its internal state must be reset at the beginning of     the new stream.*/intb64tob8(s,n,out,len) char * s,* out; int n, len; { /* Decode */    static int bits = 0;    static unsigned int r = 0;    int i, k = 0, x, t;    unsigned char c;    if (n == 0) {			/* Reset state */	bits = 0;	r = 0;	return(0);    }    x = (n < 0) ? strlen(s) : n;	/* Source length */    n = ((x + 3) / 4) * 3;		/* Compute destination length */    if (x > 0 && s[x-1] == '=') n--;	/* Account for padding */    if (x > 1 && s[x-2] == '=') n--;    if (n > len)			/* Destination not big enough */      return(-1);			/* Fail */    for (i = 0; i < x; i++) {		/* Loop thru source */	c = (CHAR)s[i];			/* Next char */        t = b64tbl[c];			/* Code for this char */	if (t == -2) {			/* Whitespace or Ctrl */	    n--;			/* Ignore */	    continue;	} else if (t == -1) {		/* Illegal code */	    return(-2);			/* Fail. */	} else if (t > 63 || t < 0)	/* Illegal value */	  return(-3);			/* fail. */	bits += 6;			/* Count bits */	r <<= 6;			/* Make space */	r |= (unsigned) t;		/* OR in new code */	if (bits >= 8) {		/* Have a byte yet? */	    bits -= 8;			/* Output it */	    c = (unsigned) ((r >> bits) & 0xff);	    out[k++] = c;	}    }    if (k < len) out[k] = NUL;		/* Null-terminate in case it's */    return(k);				/* a text string */}#endif /* NOB64 *//* C H K N U M  --  See if argument string is an integer  *//* Returns 1 if OK, zero if not OK *//* If OK, string should be acceptable to atoi() *//* Allows leading space, sign */intchknum(s) char *s; {			/* Check Numeric String */    int x = 0;				/* Flag for past leading space */    int y = 0;				/* Flag for digit seen */    char c;    debug(F110,"chknum",s,0);    if (!s) return(0);    if (!*s) return(0);    while ((c = *s++)) {		/* For each character in the string */	switch (c) {	  case SP:			/* Allow leading spaces */	  case HT:	    if (x == 0) continue;	    else return(0);	  case '+':			/* Allow leading sign */	  case '-':	    if (x == 0) x = 1;	    else return(0);	    break;	  default:			/* After that, only decimal digits */	    if (c >= '0' && c <= '9') {		x = y = 1;		continue;	    } else return(0);	}    }    return(y);}/*  R D I G I T S  -- Verify that all characters in arg ARE DIGITS  *//*  Returns 1 if so, 0 if not or if string is empty */intrdigits(s) char *s; {    if (!s) return(0);    do {        if (!isdigit(*s)) return(0);        s++;    } while (*s);    return(1);}/*  P A R N A M  --  Return parity name */char *#ifdef CK_ANSICparnam(char c)#elseparnam(c) char c;#endif /* CK_ANSIC *//* parnam */ {    switch (c) {	case 'e': return("even");	case 'o': return("odd");	case 'm': return("mark");	case 's': return("space");	case 0:   return("none");	default:  return("invalid");    }}char *					/* Convert seconds to hh:mm:ss */#ifdef CK_ANSIChhmmss(long x)#elsehhmmss(x) long x;#endif /* CK_ANSIC *//* hhmmss(x) */ {    static char buf[10];    long s, h, m;    h = x / 3600L;			/* Hours */    x = x % 3600L;    m = x / 60L;			/* Minutes */    s = x % 60L;			/* Seconds */    if (x > -1L)      sprintf(buf,"%02ld:%02ld:%02ld",h,m,s);    else      buf[0] = NUL;    return((char *)buf);}/* L S E T  --  Set s into p, right padding to length n with char c; *//*   s is a NUL-terminated string.   If length(s) > n, only n bytes are moved.   The result is NOT NUL terminated unless c == NUL and length(s) < n.   The intended of this routine is for filling in fixed-length record fields.*/VOIDlset(p,s,n,c) char *s; char *p; int n; int c; {    int x;#ifndef USE_MEMCPY    int i;#endif /* USE_MEMCPY */    if (!s) s = "";    x = strlen(s);    if (x > n) x = n;#ifdef USE_MEMCPY    memcpy(p,s,x);    if (n > x)      memset(p+x,c,n-x);#else    for (i = 0; i < x; i++)      *p++ = *s++;    for (; i < n; i++)      *p++ = c;#endif /* USE_MEMCPY */}/* R S E T  --  Right-adjust s in p, left padding to length n with char c */VOIDrset(p,s,n,c) char *s; char *p; int n; int c; {    int x;#ifndef USE_MEMCPY    int i;#endif /* USE_MEMCPY */    if (!s) s = "";    x = strlen(s);    if (x > n) x = n;#ifdef USE_MEMCPY    memset(p,c,n-x);    memcpy(p+n-x,s,x);#else    for (i = 0; i < (n - x); i++)      *p++ = c;    for (; i < n; i++)      *p++ = *s++;#endif /* USE_MEMCPY */}/*  U L O N G T O H E X  --  Unsigned long to hex  *//*  Converts unsigned long arg to hex and returns string pointer to  rightmost n hex digits left padded with 0's.  Allows for longs  up to 64 bits.  Returns pointer to result.*/char *#ifdef CK_ANSICulongtohex( unsigned long z, int n )#elseulongtohex(z,n) unsigned long z; int n; #endif	/* CK_ANSIC *//* ulongtohex */ {    static char hexbuf[17];    int i = 16, x, k = 0;    hexbuf[16] = '\0';    if (n > 16) n = 16;    k = 2 * (sizeof(long));    for (i = 0; i < n; i++) {	if (i > k || z == 0) {	    hexbuf[15-i] = '0';	} else {	    x = z & 0x0f;	    z = z >> 4;	    hexbuf[15-i] = x + ((x < 10) ? '0' : 0x37);	}    }    return((char *)(&hexbuf[16-i]));}/*  H E X T O U L O N G  --  Hex string to unsigned long  *//*  Converts n chars from s from hex to unsigned long.  Returns:   0L or positive, good result (0L is returned if arg is NULL or empty).  -1L on error: non-hex arg or overflow.*/longhextoulong(s,n) char *s; int n;

⌨️ 快捷键说明

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