home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / missing_functions.c < prev    next >
Text File  |  1997-11-12  |  6KB  |  328 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. #include "mtools.h"
  21.  
  22. #ifdef _OSK
  23. #include <types.h>
  24. #define CONST
  25. #endif
  26.  
  27. #ifndef HAVE_STRDUP
  28.  
  29.  
  30. char *strdup(const char *str)
  31. {
  32.     char *nstr;
  33.  
  34.     if (str == (char*)0)
  35.         return 0;
  36.  
  37.     nstr = (char*)malloc((strlen(str) + 1));
  38.  
  39.     if (nstr == (char*)0)
  40.     {
  41.         (void)fprintf(stderr, "strdup(): not enough memory to duplicate `%s'\n",
  42.               str);
  43.     exit(1);
  44.     }
  45.  
  46.     (void)strcpy(nstr, str);
  47.  
  48.     return nstr;
  49. }
  50. #endif /* HAVE_STRDUP */
  51.  
  52.  
  53. #ifndef HAVE_MEMCPY
  54. /*
  55.  * Copy contents of memory (with possible overlapping).
  56.  */
  57. char *memcpy(char *s1, const char *s2, size_t n)
  58. {
  59.     bcopy(s2, s1, n);
  60.     return(s1);
  61. }
  62. #endif
  63.  
  64. #ifndef HAVE_MEMSET
  65. /*
  66.  * Copies the character c, n times to string s
  67.  */
  68. char *memset(char *s, char c, size_t n)
  69. {
  70.     char *s1 = s;
  71.  
  72.     while (n > 0) {
  73.         --n;
  74.         *s++ = c;
  75.     }
  76.     return(s1);
  77. }
  78. #endif /* HAVE_MEMSET */
  79.  
  80.  
  81. #ifndef HAVE_STRPBRK
  82. /*
  83.  * Return ptr to first occurrence of any character from `brkset'
  84.  * in the character string `string'; NULL if none exists.
  85.  */
  86. char *strpbrk(const char *string, const char *brkset)
  87. {
  88.     register char *p;
  89.  
  90.     if (!string || !brkset)
  91.         return(0);
  92.     do {
  93.         for (p = brkset; *p != '\0' && *p != *string; ++p)
  94.             ;
  95.         if (*p != '\0')
  96.             return(string);
  97.     }
  98.     while (*string++);
  99.     return(0);
  100. }
  101. #endif /* HAVE_STRPBRK */
  102.  
  103.  
  104. #ifndef HAVE_STRTOUL
  105. static int getdigit(char a, int max)
  106. {
  107.     int dig;
  108.     
  109.     if(a < '0')
  110.         return -1;
  111.     if(a <= '9') {
  112.         dig = a - '0';
  113.     } else if(a >= 'a')
  114.         dig = a - 'a' + 10;
  115.     else if(a >= 'A')
  116.         dig = a - 'A' + 10;
  117.     if(dig >= max)
  118.         return -1;
  119.     else
  120.         return dig;
  121. }
  122.  
  123. unsigned long strtoul(const char *string, char **eptr, int base)
  124. {
  125.     int accu, dig;
  126.  
  127.     if(base < 1 || base > 36) {
  128.         if(string[0] == '0') {
  129.             switch(string[1]) {
  130.                        case 'x':
  131.                 case 'X':
  132.                     return strtoul(string+2, eptr, 16);
  133.                 case 'b':
  134.                        case 'B':
  135.                     return strtoul(string+2, eptr, 2);
  136.                 default:
  137.                     return strtoul(string, eptr, 8);
  138.             }
  139.         }
  140.                return strtoul(string, eptr, 10);
  141.     }
  142.     if(base == 16 && string[0] == '0' &&
  143.        (string[1] == 'x' || string[1] == 'X'))
  144.         string += 2;
  145.  
  146.     if(base == 2 && string[0] == '0' &&
  147.        (string[1] == 'b' || string[1] == 'B'))
  148.         string += 2;
  149.     accu = 0;
  150.     while( (dig = getdigit(*string, base)) != -1 ) {
  151.         accu = accu * base + dig;
  152.         string++;
  153.     }
  154.     if(eptr)
  155.         *eptr = (char *) string;
  156.     return accu;
  157. }
  158. #endif /* HAVE_STRTOUL */
  159.  
  160. #ifndef HAVE_STRSPN
  161. /* Return the length of the maximum initial segment
  162.    of S which contains only characters in ACCEPT.  */
  163. size_t strspn(const char *s, const char *accept)
  164. {
  165.   register CONST char *p;
  166.   register CONST char *a;
  167.   register size_t count = 0;
  168.  
  169.   for (p = s; *p != '\0'; ++p)
  170.     {
  171.       for (a = accept; *a != '\0'; ++a)
  172.     if (*p == *a)
  173.       break;
  174.       if (*a == '\0')
  175.     return count;
  176.       else
  177.     ++count;
  178.     }
  179.  
  180.   return count;
  181. }
  182. #endif /* HAVE_STRSPN */
  183.  
  184. #ifndef HAVE_STRCSPN
  185. /* Return the length of the maximum inital segment of S
  186.    which contains no characters from REJECT.  */
  187. size_t strcspn (const char *s, const char *reject)
  188. {
  189.   register size_t count = 0;
  190.  
  191.   while (*s != '\0')
  192.     if (strchr (reject, *s++) == NULL)
  193.       ++count;
  194.     else
  195.       return count;
  196.  
  197.   return count;
  198. }
  199.  
  200. #endif /* HAVE_STRCSPN */
  201.  
  202. #ifndef HAVE_STRERROR
  203. char *strerror(int errno)
  204. {
  205.   return sys_errlist[errno];
  206. }
  207. #endif
  208.  
  209. #ifndef HAVE_STRCASECMP
  210. /* Compare S1 and S2, ignoring case, returning less than, equal to or
  211.    greater than zero if S1 is lexiographically less than,
  212.    equal to or greater than S2.  */
  213. int strcasecmp(const char *s1, const char *s2)
  214. {
  215.   register const unsigned char *p1 = (const unsigned char *) s1;
  216.   register const unsigned char *p2 = (const unsigned char *) s2;
  217.   unsigned char c1, c2;
  218.  
  219.   if (p1 == p2)
  220.     return 0;
  221.  
  222.   do
  223.     {
  224.       c1 = tolower (*p1++);
  225.       c2 = tolower (*p2++);
  226.       if (c1 == '\0')
  227.     break;
  228.     }
  229.   while (c1 == c2);
  230.  
  231.   return c1 - c2;
  232. }
  233. #endif
  234.  
  235.  
  236.  
  237. #ifndef HAVE_STRCASECMP
  238. /* Compare S1 and S2, ignoring case, returning less than, equal to or
  239.    greater than zero if S1 is lexiographically less than,
  240.    equal to or greater than S2.  */
  241. #ifdef __BEOS__
  242. int strncasecmp(const char *s1, const char *s2, unsigned int n)
  243. #else
  244. int strncasecmp(const char *s1, const char *s2, size_t n)
  245. #endif
  246. {
  247.   register const unsigned char *p1 = (const unsigned char *) s1;
  248.   register const unsigned char *p2 = (const unsigned char *) s2;
  249.   unsigned char c1, c2;
  250.  
  251.   if (p1 == p2)
  252.     return 0;
  253.  
  254.   c1 = c2 = 1;
  255.   while (c1 && c1 == c2 && n-- > 0)
  256.     {
  257.       c1 = tolower (*p1++);
  258.       c2 = tolower (*p2++);
  259.     }
  260.  
  261.   return c1 - c2;
  262. }
  263. #endif
  264.  
  265. #ifndef HAVE_GETPASS
  266. char *getpass(const char *prompt)
  267. {
  268.     static char password[129];
  269.     int l;
  270.  
  271.     fprintf(stderr,"%s",prompt);
  272.     fgets(password, 128, stdin);
  273.     l = strlen(password);
  274.     if(l && password[l-1] == '\n')
  275.         password[l-1] = '\0';
  276.     return password;
  277.  
  278. }
  279. #endif
  280.  
  281. #ifndef HAVE_ATEXIT
  282.  
  283. #ifdef HAVE_ON_EXIT
  284. int atexit(void (*function)(void))
  285. {
  286.     return on_exit( (void(*)(int,void*)) function, 0);
  287. }
  288. #else
  289.  
  290. typedef struct exitCallback {
  291.     void (*function) (void);
  292.     struct exitCallback *next;
  293. } exitCallback_t;
  294.  
  295. static exitCallback_t *callback = 0;
  296.  
  297. int atexit(void (*function) (void))
  298. {
  299.     exitCallback_t *newCallback;
  300.         
  301.     newCallback = New(exitCallback_t);
  302.     if(!newCallback) {
  303.         fprintf(stderr,"Out of memory error\n");
  304.         exit(1);
  305.     }
  306.     newCallback->function = function;
  307.     newCallback->next = callback;
  308.     callback = newCallback;
  309.     return 0;
  310. }
  311. #undef exit
  312.  
  313. void myexit(int code)
  314. {
  315.   void (*function)(void);
  316.  
  317.   while(callback) {
  318.     function = callback->function;
  319.     callback = callback->next;
  320.     function();
  321.   }
  322.   exit(code);
  323. }
  324.  
  325. #endif
  326.  
  327. #endif
  328.