home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / msvp98b1.lzh / MSNLIB.C < prev    next >
Text File  |  1993-05-14  |  7KB  |  318 lines

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