home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / chip_20022115.iso / amiga / chiputil / nulib2.lha / nufxlib-101 / MiscStuff.c < prev    next >
C/C++ Source or Header  |  2001-12-04  |  3KB  |  126 lines

  1. /*
  2.  * Copyright (C) 2000 by Andy McFadden, All Rights Reserved.
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU Library General Public License, see the file COPYING.LIB.
  5.  *
  6.  * Misc stuff (shared between nufxlib and nulib2).  This is a collection
  7.  * of standard functions that aren't available in libc on this system.
  8.  */
  9. #include "SysDefs.h"
  10. #include "MiscStuff.h"
  11. #include <ctype.h>
  12.  
  13.  
  14. #ifndef HAVE_STRERROR
  15. /*
  16.  * Return a pointer to the appropriate string in the system table, or NULL
  17.  * if the value is out of bounds.
  18.  */
  19. const char*
  20. Nu_strerror(int errnum)
  21. {
  22.     extern int sys_nerr;
  23.     extern char *sys_errlist[];
  24.  
  25.     if (errnum < 0 || errnum > sys_nerr)
  26.         return NULL;
  27.  
  28.     return sys_errlist[errnum];
  29. }
  30. #endif
  31.  
  32. #ifndef HAVE_MEMMOVE
  33. /*
  34.  * Move a block of memory.  Unlike memcpy, this is expected to work
  35.  * correctly with overlapping blocks.
  36.  *
  37.  * This is a straightforward implementation.  A much faster implementation,
  38.  * from BSD, is available in the PGP 2.6.2 distribution, but this should
  39.  * suffice for those few systems that don't have memmove.
  40.  */
  41. void*
  42. Nu_memmove(void* dst, const void* src, size_t n)
  43. {
  44.     void* retval = dst;
  45.     char* srcp = (char*)src;
  46.     char* dstp = (char*)dst;
  47.  
  48.     /* you can normally get away with this if n==0 */
  49.     assert(dst != NULL);
  50.     assert(src != NULL);
  51.  
  52.     if (dstp == srcp || !n) {
  53.         /* nothing to do */
  54.     } else if (dstp > srcp) {
  55.         /* start from the end */
  56. #ifndef _AMIGA
  57.         (char*)dstp += n-1;
  58.         (char*)srcp += n-1;
  59. #else
  60.         dstp += n-1;
  61.         srcp += n-1;
  62. #endif
  63.         while (n--)
  64.             *dstp-- = *srcp--;
  65.     } else {
  66.         /* start from the front */
  67.         while (n--)
  68.             *dstp++ = *srcp++;
  69.     }
  70.  
  71.     return retval;
  72. }
  73. #endif
  74.  
  75. #ifndef HAVE_STRTOUL
  76. /*
  77.  * Perform strtol, but on an unsigned long.
  78.  *
  79.  * On systems that have strtol but don't have strtoul, the strtol
  80.  * function doesn't clamp the return value, making it similar in
  81.  * function to strtoul.  The comparison is not exact, however,
  82.  * because strtoul is expected to lots of fancy things (like set
  83.  * errno to ERANGE).
  84.  *
  85.  * For our purposes here, strtol does all we need it to.  Someday
  86.  * we should replace this with a "real" version.
  87.  */
  88. unsigned long
  89. Nu_strtoul(const char *nptr, char **endptr, int base)
  90. {
  91.     return strtol(nptr, endptr, base);
  92. }
  93. #endif
  94.  
  95. #ifndef HAVE_STRCASECMP
  96. /*
  97.  * Compare two strings, case-insensitive.
  98.  */
  99. int
  100. Nu_strcasecmp(const char *str1, const char *str2)
  101. {
  102.     while (*str1 && *str2 && toupper(*str1) == toupper(*str2))
  103.         str1++, str2++;
  104.     return (toupper(*str1) - toupper(*str2));
  105. }
  106.  
  107. #endif
  108.  
  109. #ifndef HAVE_STRNCASECMP
  110. /*
  111.  * Compare two strings, case-insensitive, stopping after "n" chars.
  112.  */
  113. int
  114. Nu_strncasecmp(const char *str1, const char *str2, size_t n)
  115. {
  116.     while (n && *str1 && *str2 && toupper(*str1) == toupper(*str2))
  117.         str1++, str2++, n--;
  118.  
  119.     if (n)
  120.         return (toupper(*str1) - toupper(*str2));
  121.     else
  122.         return 0;    /* no mismatch in first n chars */
  123. }
  124. #endif
  125.  
  126.