home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / xstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  3.7 KB  |  173 lines

  1. /* xstring.c -- code for needed string functions that might be missing.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  strcasecmp() and strncasecmp()
  20.    have been stolen from the GNU C library.  */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <sys/types.h>
  28. #include <ctype.h>
  29. #include <stddef.h>
  30.  
  31. #include "xmalloc.h"
  32. #include "xstring.h"
  33.  
  34.  
  35. #ifndef HAVE_STRCASECMP
  36.  
  37. int
  38. strcasecmp(s1, s2)
  39.     const char *s1;
  40.     const char *s2;
  41. {
  42.     unsigned char c1, c2;
  43.  
  44.     if (s1 == s2)
  45.         return 0;
  46.  
  47.     do
  48.     {
  49.         c1 = tolower(*s1++);
  50.         c2 = tolower(*s2++);
  51.  
  52.         if (c1 == 0)
  53.             break;
  54.     }
  55.     while (c1 == c2);
  56.  
  57.     return c1 - c2;
  58. }
  59.  
  60. #endif /* !HAVE_STRCASECMP */
  61.  
  62.  
  63. #ifndef HAVE_STRNCASECMP
  64.  
  65. /* Compare no more than N characters of S1 and S2,
  66.    ignoring case, returning less than, equal to or
  67.    greater than zero if S1 is lexicographically less
  68.    than, equal to or greater than S2.  */
  69. int
  70. strncasecmp(s1, s2, n)
  71.     const char *s1;
  72.     const char *s2;
  73.     size_t n;
  74. {
  75.     unsigned char c1, c2;
  76.     register const unsigned char *p1 = (const unsigned char *) s1;
  77.     register const unsigned char *p2 = (const unsigned char *) s2;
  78.  
  79.     if (p1 == p2 || n == 0)
  80.         return 0;
  81.  
  82.     do
  83.     {
  84.         c1 = tolower (*p1++);
  85.         c2 = tolower (*p2++);
  86.         if (c1 == '\0' || c1 != c2)
  87.             return c1 - c2;
  88.     }
  89.     while (--n > 0);
  90.  
  91.     return c1 - c2;
  92. }
  93.  
  94. #endif /* !HAVE_STRNCASECMP */
  95.  
  96.  
  97. #ifndef HAVE_STRSTR
  98.  
  99. /* Return the first ocurrence of NEEDLE in HAYSTACK.  */
  100.  
  101. char *
  102. strstr(haystack, needle)
  103.     const char *haystack;
  104.     const char *needle;
  105. {
  106.     register char *needle_end   = strchr(needle, '\0');
  107.     register char *haystack_end = strchr(haystack, '\0');
  108.     register size_t needle_len  = needle_end - needle;
  109.     register size_t needle_last = needle_len - 1;
  110.     register char *begin;
  111.  
  112.     if (needle_len == 0)
  113.         return (char *) haystack;       /* ANSI 4.11.5.7, line 25.  */
  114.  
  115.     if ((size_t) (haystack_end - haystack) < needle_len)
  116.         return NULL;
  117.  
  118.     for (begin = &haystack[needle_last]; begin < haystack_end; ++begin)
  119.     {
  120.         register char *n = &needle[needle_last];
  121.         register char *h = begin;
  122.  
  123.         do
  124.             if (*h != *n)
  125.                 goto loop;              /* continue for loop */
  126.         while (--n >= needle && --h >= haystack);
  127.  
  128.         return (char *) h;
  129.  
  130.         loop:;
  131.     }
  132.  
  133.     return NULL;
  134. }
  135.  
  136. #endif /* !HAVE_STRSTR */
  137.  
  138.  
  139. /* A strdup() version that calls xmalloc instead of malloc, never returning
  140.    a NULL pointer.  */
  141.  
  142. char *
  143. xstrdup(s)
  144.     const char *s;
  145. {
  146.     size_t len = strlen(s) + 1;
  147.     char *new_s = xmalloc(len);
  148.  
  149.     memcpy(new_s, s, len);
  150.     return new_s;
  151. }
  152.  
  153.  
  154. #ifndef HAVE_MEMMOVE
  155.  
  156. /* A slow but portable memmove function.  For those loosing systems that
  157.    don't have one.  */
  158.  
  159. void *
  160. memmove(dest, src, n)
  161.     void *dest;
  162.     const void *src;
  163.     size_t n;
  164. {
  165.     char *temp = xmalloc(n);
  166.     memcpy(temp, src, n);
  167.     memcpy(dest, src, n);
  168.     xfree(temp);
  169.     return dest;
  170. }
  171.  
  172. #endif /* !HAVE_MEMMOVE */
  173.