home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / missing_functions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-03  |  5.1 KB  |  252 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file contains excerpts of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "sysincludes.h"
  20.  
  21. #ifndef HAVE_STRDUP
  22.  
  23.  
  24. char *strdup(__const char *str)
  25. {
  26.     char *nstr;
  27.  
  28.     if (str == (char*)0)
  29.         return str;
  30.  
  31.     nstr = (char*)malloc((u_int)(strlen(str) + 1));
  32.  
  33.     if (nstr == (char*)0)
  34.     {
  35.         (void)fprintf(stderr, "strdup(): not enough memory to duplicate `%s'\n",
  36.               str);
  37.     exit(1);
  38.     }
  39.  
  40.     (void)strcpy(nstr, str);
  41.  
  42.     return nstr;
  43. }
  44. #endif /* HAVE_STRDUP */
  45.  
  46.  
  47. #ifndef HAVE_MEMCPY
  48. /*
  49.  * Copy contents of memory (with possible overlapping).
  50.  */
  51. char *memcpy(char *s1, __const char *s2, size_t n)
  52. {
  53.     bcopy(s2, s1, n);
  54.     return(s1);
  55. }
  56. #endif
  57.  
  58. #ifndef HAVE_MEMSET
  59. /*
  60.  * Copies the character c, n times to string s
  61.  */
  62. char *memset(char *s, char c, size_t n)
  63. {
  64.     char *s1 = s;
  65.  
  66.     while (n > 0) {
  67.         --n;
  68.         *s++ = c;
  69.     }
  70.     return(s1);
  71. }
  72. #endif /* HAVE_MEMSET */
  73.  
  74.  
  75. #ifndef HAVE_STRPBRK
  76. /*
  77.  * Return ptr to first occurrence of any character from `brkset'
  78.  * in the character string `string'; NULL if none exists.
  79.  */
  80. char *strpbrk(__const char *string, __const char *brkset)
  81. {
  82.     register char *p;
  83.  
  84.     if (!string || !brkset)
  85.         return(0);
  86.     do {
  87.         for (p = brkset; *p != '\0' && *p != *string; ++p)
  88.             ;
  89.         if (*p != '\0')
  90.             return(string);
  91.     }
  92.     while (*string++);
  93.     return(0);
  94. }
  95. #endif /* HAVE_STRPBRK */
  96.  
  97.  
  98. #ifndef HAVE_STRTOUL
  99. static int getdigit(char a, int max)
  100. {
  101.     int dig;
  102.     
  103.     if(a < '0')
  104.         return -1;
  105.     if(a <= '9') {
  106.         dig = a - '0';
  107.     } else if(a >= 'a')
  108.         dig = a - 'a' + 10;
  109.     else if(a >= 'A')
  110.         dig = a - 'A' + 10;
  111.     if(dig >= max)
  112.         return -1;
  113.     else
  114.         return dig;
  115. }
  116.  
  117. unsigned long strtoul(const char *string, char **eptr, int base)
  118. {
  119.     int accu, dig;
  120.  
  121.     if(base < 1 || base > 36) {
  122.         if(string[0] == '0') {
  123.             switch(string[1]) {
  124.                        case 'x':
  125.                 case 'X':
  126.                     return strtoul(string+2, eptr, 16);
  127.                 case 'b':
  128.                        case 'B':
  129.                     return strtoul(string+2, eptr, 2);
  130.                 default:
  131.                     return strtoul(string, eptr, 8);
  132.             }
  133.         }
  134.                return strtoul(string, eptr, 10);
  135.     }
  136.     if(base == 16 && string[0] == '0' &&
  137.        (string[1] == 'x' || string[1] == 'X'))
  138.         string += 2;
  139.  
  140.     if(base == 2 && string[0] == '0' &&
  141.        (string[1] == 'b' || string[1] == 'B'))
  142.         string += 2;
  143.     accu = 0;
  144.     while( (dig = getdigit(*string, base)) != -1 ) {
  145.         accu = accu * base + dig;
  146.         string++;
  147.     }
  148.     if(eptr)
  149.         *eptr = (char *) string;
  150.     return accu;
  151. }
  152. #endif /* HAVE_STRTOUL */
  153.  
  154. #ifndef HAVE_STRSPN
  155. /* Return the length of the maximum initial segment
  156.    of S which contains only characters in ACCEPT.  */
  157. size_t strspn(__const char *s, __const char *accept)
  158. {
  159.   register CONST char *p;
  160.   register CONST char *a;
  161.   register size_t count = 0;
  162.  
  163.   for (p = s; *p != '\0'; ++p)
  164.     {
  165.       for (a = accept; *a != '\0'; ++a)
  166.     if (*p == *a)
  167.       break;
  168.       if (*a == '\0')
  169.     return count;
  170.       else
  171.     ++count;
  172.     }
  173.  
  174.   return count;
  175. }
  176. #endif /* HAVE_STRSPN */
  177.  
  178. #ifndef HAVE_STRCSPN
  179. /* Return the length of the maximum inital segment of S
  180.    which contains no characters from REJECT.  */
  181. size_t strcspn (__const char *s, __const char *reject)
  182. {
  183.   register size_t count = 0;
  184.  
  185.   while (*s != '\0')
  186.     if (strchr (reject, *s++) == NULL)
  187.       ++count;
  188.     else
  189.       return count;
  190.  
  191.   return count;
  192. }
  193.  
  194. #endif /* HAVE_STRCSPN */
  195.  
  196. #ifndef HAVE_STRERROR
  197. char *strerror(int errno)
  198. {
  199.   return sys_errlist[errno];
  200. }
  201. #endif
  202.  
  203. #ifndef HAVE_STRCASECMP
  204. /* Compare S1 and S2, ignoring case, returning less than, equal to or
  205.    greater than zero if S1 is lexiographically less than,
  206.    equal to or greater than S2.  */
  207. int strcasecmp(const char *s1, const char *s2)
  208. {
  209.   register const unsigned char *p1 = (const unsigned char *) s1;
  210.   register const unsigned char *p2 = (const unsigned char *) s2;
  211.   unsigned char c1, c2;
  212.  
  213.   if (p1 == p2)
  214.     return 0;
  215.  
  216.   do
  217.     {
  218.       c1 = tolower (*p1++);
  219.       c2 = tolower (*p2++);
  220.       if (c1 == '\0')
  221.     break;
  222.     }
  223.   while (c1 == c2);
  224.  
  225.   return c1 - c2;
  226. }
  227. #endif
  228.  
  229. #ifndef HAVE_STRCASECMP
  230. /* Compare S1 and S2, ignoring case, returning less than, equal to or
  231.    greater than zero if S1 is lexiographically less than,
  232.    equal to or greater than S2.  */
  233. int strncasecmp(const char *s1, const char *s2, int n)
  234. {
  235.   register const unsigned char *p1 = (const unsigned char *) s1;
  236.   register const unsigned char *p2 = (const unsigned char *) s2;
  237.   unsigned char c1, c2;
  238.  
  239.   if (p1 == p2)
  240.     return 0;
  241.  
  242.   c1 = c2 = 1;
  243.   while (c1 && c1 == c2 && n-- > 0)
  244.     {
  245.       c1 = tolower (*p1++);
  246.       c2 = tolower (*p2++);
  247.     }
  248.  
  249.   return c1 - c2;
  250. }
  251. #endif
  252.