home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msnlib.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  8KB  |  330 lines

  1. /* File MSNLIB.C
  2.  * Replacement for C library for use with MS-DOS Kermit.
  3.  *
  4.  *    Copyright (C) 1982, 1997, Trustees of Columbia University in the 
  5.  *    City of New York.  The MS-DOS Kermit software may not be, in whole 
  6.  *    or in part, licensed or sold for profit as a software product itself,
  7.  *    nor may it be included in or distributed with commercial products
  8.  *    or otherwise distributed by commercial concerns to their clients 
  9.  *    or customers without written permission of the Office of Kermit 
  10.  *    Development and Distribution, Columbia University.  This copyright 
  11.  *    notice must not be removed, altered, or obscured.
  12.  *
  13.  * Last edit
  14.  * 12 Jan 1995 v3.14
  15.  *  Authors: J.R.Doupnik, USU, Frank da Cruz, Columbia Univ.
  16.  * Contains:
  17.  * strchr, strcat, strncat, strcpy, strncpy, strlen, strcmp, stricmp, strncmp
  18.  * atoi, itoa, ltoa, isdigit, ntoa.
  19. */
  20. #include "msntcp.h"
  21. #include "msnlib.h"
  22.  
  23. #ifndef MSDOS
  24. /*
  25.  In MS-DOS Kermit, these are assembler routines to avoid math library.
  26. */
  27. #define ourmod(a,b)   (a % b)
  28. #define ourdiv(a, b)  (a / b)
  29. #define ourlmod(a,b)  (a % b)
  30. #define ourldiv(a, b) (a / b)
  31. #endif /* MSDOS */
  32.  
  33. #ifndef NULL
  34. #define NULL 0
  35. #endif /* NULL */
  36.  
  37. /*
  38.  By the way, there is probably no point in the next #ifndef,
  39.  because size_t is either built into to the compiler or typedef'd,
  40.  rather than defined.  So the #define below will always happen.
  41. */
  42. #ifndef size_t
  43. #define size_t int
  44. #endif /* size_t */
  45.  
  46. int _acrtused;            /* MS C compiler startup file quantity */
  47.  
  48. /*
  49.   _strchr
  50.   Finds first occurence of character c in string s.
  51.   Returns pointer to it if found, NULL if not found.
  52. */
  53. byte *
  54. strchr(byte *s, const byte c) {
  55.     while ((*s != (byte)'\0') && (*s != (byte)(c & 0xff))) s++;
  56.     if (*s == '\0') return(NULL);
  57.     else return(s);
  58. }
  59.  
  60. byte FAR *
  61. strchrf(byte FAR *s, const byte c) {
  62.     while ((*s != (byte)'\0') && (*s != (byte)(c & 0xff))) s++;
  63.     if (*s == '\0') return(NULL);
  64.     else return(s);
  65. }
  66.  
  67. /*
  68.   _strcat
  69.   Appends entire string s2 to string s1.
  70.   Assumes there is room for s2 after end of s1.
  71.   Returns pointer to s1 or NULL if s1 is a null pointer.
  72. */
  73. byte *
  74. strcat(byte *s1, byte *s2) {
  75.     register byte *p;
  76.  
  77.     if (s1 == NULL) return(NULL);
  78.     if (s2 == NULL || *s2 == '\0') return(s1);
  79.     p = s1;                /* Make copy */
  80.     while (*p) p++;            /* Find end */
  81.     while (*p++ = *s2++);        /* Copy thru terminating NUL */
  82.     return(s1);                /* Return original */
  83. }
  84.  
  85. /*
  86.   _strncat
  87.   Appends up to n chars of string s2 to string s1.
  88.   Returns pointer to string1 or NULL if s1 is a null pointer.
  89. */
  90. byte *
  91. strncat(byte *s1, byte *s2, size_t n) {
  92.     register byte * p;
  93.  
  94.     if (s1 == NULL) return(NULL);
  95.     if (s2 == NULL || *s2 == '\0') return(s1);
  96.     p = s1;                /* Copy pointer */
  97.     while (*p) p++;            /* Step to end of s1 */
  98.     while ((*p++ = *s2++) && (--n > 0)); /* Copy up to n bytes of s2 */
  99.     return(s1);                /* Return original pointer */
  100. }
  101.  
  102. /*
  103.   _strcpy
  104.   Copies s2 to s1, returns pointer to s1 or NULL if s1 was NULL.
  105. */
  106. byte *
  107. strcpy(byte *s1, byte *s2) {
  108.     register byte *p;
  109.  
  110.     if (s1 == NULL) return(NULL);
  111.     if (s2 == NULL) s2 = "";
  112.     p = s1;                /* Copy pointer */
  113.     while (*p++ = *s2++);        /* Copy thru terminating NUL */
  114.     return(s1);                /* Return original pointer */
  115. }
  116.  
  117. /*
  118.   _strncpy
  119.   Copies at most n characters from s2 to to s1, returns pointer to s1.
  120.   Returns s1 or NULL if s1 was NULL.
  121. */
  122. byte *
  123. strncpy(byte *s1, byte *s2, size_t n) {
  124.     register int s2len;
  125.     register byte *p1;
  126.  
  127.     if (s1 == NULL) return(NULL);
  128.     if (s2 == NULL) s2 = "";
  129.     if ((s2len = strlen(s2)) > n) s2len = n;
  130.     p1 = s1;
  131.  
  132.     while (s2len-- > 0)            /* Copy */
  133.       *p1++ = *s2++;
  134.     *p1 = '\0';                /* Terminate */
  135.     return(s1);                /* No need to pad out, one's enuf */
  136. }
  137.  
  138. /*
  139.   _strlen
  140.   Returns length of null-terminated string not including '\0'
  141. */
  142. size_t
  143. strlen(byte *s) {
  144.     register int i = 0;
  145.  
  146.     if (s == NULL) return(0);
  147.     while (*s++) i++;
  148.     return(i);
  149. }
  150.  
  151. /*
  152.   _strcmp
  153.   Compare null-terminated strings using ASCII values.
  154.   Case matters.  Returns:
  155.   < 0 if s1 < s2,
  156.   = 0 if s1 = s2,
  157.   > 0 if s1 > s2
  158. */
  159. int
  160. strcmp(byte *s1, byte *s2) {
  161.     if (s1 == NULL) s1 = "";
  162.     if (s2 == NULL) s2 = "";
  163.     do {
  164.     if (*s1 < *s2) return(-1);
  165.     if (*s1 > *s2) return(1);
  166.     if (*s2 == '\0') return(0);
  167.     s2++;
  168.     } while (*s1++);
  169.     return(0);
  170. }
  171.  
  172. /*
  173.   _stricmp
  174.   Like strcmp but case insenstive
  175. */
  176. int
  177. stricmp(byte *s1, byte *s2) {
  178.     register byte c1, c2;
  179.  
  180.     if (s1 == NULL) s1 = "";
  181.     if (s2 == NULL) s2 = "";
  182.     do {
  183.     c1 = *s1; c2 = *s2;
  184.     if ('a' <= c1 && c1 <= 'z') c1 = c1 - (byte)('a' - 'A');
  185.     if ('a' <= c2 && c2 <= 'z') c2 = c2 - (byte)('a' - 'A');
  186.     if (c1 < c2) return(-1);
  187.     if (c1 > c2) return(1);
  188.     if (c2 == '\0') return(0);
  189.     s1++; s2++;
  190.     } while (c1 != '\0');
  191.     return(0);
  192. }
  193.  
  194. /*
  195.   _strncmp
  196.   Compares at most n characters of strings s1 and s2.
  197. */
  198. int
  199. strncmp(byte *s1, byte *s2, size_t n) {    
  200.  
  201.     if (s1 == NULL) s1 = "";
  202.     if (s2 == NULL) s2 = "";
  203.     while (n-- > 0 && *s1) {
  204.     if (*s1 < *s2) return(-1);
  205.     if (*s1 > *s2) return(1);
  206.     s1++; s2++;
  207.     }
  208.     return(0);
  209. }
  210.  
  211. /*
  212.   _atoi
  213.   Converts decimal numeric string to integer.
  214.   Breaks on first non-digit or end of string.
  215.   Returns integer.
  216. */
  217. int
  218. atoi(byte *s) {
  219.     register int i, count;
  220.     count = 0;
  221.     for (i = 0; i < 18; i++) {
  222.     if (*s < '0' || *s > '9') break;
  223.     count *= 10;
  224.     count += *s - '0';        /* ascii to binary */
  225.     s++;
  226.     }
  227.     return(count);
  228. }
  229.  
  230. /*
  231.   _itoa
  232.   Converts integer value to ASCII digits (up to 18 characters long),
  233.   stores in string, null terminated.  Returns NULL on failure,
  234.   pointer to result on success.
  235. */
  236. byte *
  237. itoa(int value, byte *string, int radix) { /* From K & R */
  238.     int c, j, sign;
  239.     register int i;
  240.     register byte *s;
  241.  
  242.     if (string == NULL) return(NULL);
  243.     
  244.     s = string;
  245.  
  246.     if ((sign = value) < 0)        /* Save sign */
  247.       value = - value;            /* Force value positive */
  248.     i = 0;
  249.     do {
  250.     s[i++] = (byte)(ourmod(value, radix) + '0');
  251.     } while ((value = ourdiv(value, radix)) > 0);
  252.  
  253.     if (sign < 0)
  254.       s[i++] = '-';
  255.     s[i] = '\0';
  256.     j = strlen(s) -1;
  257.     for (i = 0; i < j; i++, j--) {
  258.     c = s[i];
  259.     if (c > '9') c = c - '9' + 'A' -1;
  260.     s[i] = s[j];
  261.     s[j] = (byte)(c & 0xff);
  262.     }
  263.     return(string);
  264. }
  265.  
  266. /*
  267.   _ltoa
  268.   Like itoa() but using long value ( < 34 ).
  269. */
  270. byte *
  271. ltoa(long value, byte *string, int radix) { /* K & R */
  272.     int c, j;
  273.     register int i;
  274.     long sign;
  275.     register byte * s;
  276.  
  277.     if (string == NULL) return(NULL);
  278.     s = string;
  279.  
  280.     if ((sign = value) < 0) value = - value; /* value to positive*/
  281.     i = 0;
  282.     do {
  283.     s[i++] = (byte)(ourlmod(value, radix) + '0');
  284.     } while ((value = ourldiv(value, radix)) > 0);
  285.  
  286.     if (sign < 0)
  287.       s[i++] = '-';
  288.     s[i] = '\0';
  289.  
  290.     j = strlen(s) - 1;
  291.     for (i = 0; i < j; i++, j--) {
  292.     c = s[i];
  293.     if (c > '9') c = c - '9' + 'A' -1;
  294.     s[i] = s[j];
  295.     s[j] = (byte)(c & 0xff);
  296.     }
  297.     return(string);
  298. }
  299.  
  300. /*
  301.   _isdigit
  302.   Returns 1 if argument is a decimal digit, 0 otherwise.
  303. */
  304. int
  305. isdigit(const byte c) {
  306.     if ((c & 0xff) < '0' || (c & 0xff) > '9')
  307.       return(0);            /* say is not a digit */
  308.     return(1);
  309. }
  310.  
  311. /* 
  312.    Convert long val to dotted decimal string at pointer p, does high order
  313.    byte first. Intended to yield dotted decimal IP addresses from longs.
  314. */
  315. void
  316. ntoa(byte *p, unsigned long val)
  317. {
  318.     register byte *ptr;
  319.     register int i;
  320.     
  321.     ptr = p;
  322.     for (i = 24; i >= 0; i -= 8)
  323.         {
  324.         itoa((int)((val >> i) & 0xff), ptr, 10); /* convert a byte */
  325.         strcat(ptr, ".");            /* dot separator */
  326.         ptr = p + strlen(p);
  327.         }
  328.     *(--ptr) = '\0';            /* remove trailing dot */
  329. }
  330.