root/lib/string.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. strcpy
  2. strncpy
  3. strcat
  4. strncat
  5. strcmp
  6. strncmp
  7. strchr
  8. strrchr
  9. strlen
  10. strnlen
  11. strspn
  12. strpbrk
  13. strtok
  14. memset
  15. bcopy
  16. memcpy
  17. memmove
  18. memcmp
  19. memscan
  20. strstr

   1 /*
   2  *  linux/lib/string.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * stupid library routines.. The optimized versions should generally be found
   9  * as inline code in <asm-xx/string.h>
  10  *
  11  * These are buggy as well..
  12  */
  13  
  14 #include <linux/types.h>
  15 #include <linux/string.h>
  16 
  17 char * ___strtok = NULL;
  18 
  19 #ifndef __HAVE_ARCH_STRCPY
  20 char * strcpy(char * dest,const char *src)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22         char *tmp = dest;
  23 
  24         while ((*dest++ = *src++) != '\0')
  25                 /* nothing */;
  26         return tmp;
  27 }
  28 #endif
  29 
  30 #ifndef __HAVE_ARCH_STRNCPY
  31 char * strncpy(char * dest,const char *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33         char *tmp = dest;
  34 
  35         while (count-- && (*dest++ = *src++) != '\0')
  36                 /* nothing */;
  37 
  38         return tmp;
  39 }
  40 #endif
  41 
  42 #ifndef __HAVE_ARCH_STRCAT
  43 char * strcat(char * dest, const char * src)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45         char *tmp = dest;
  46 
  47         while (*dest)
  48                 dest++;
  49         while ((*dest++ = *src++) != '\0')
  50                 ;
  51 
  52         return tmp;
  53 }
  54 #endif
  55 
  56 #ifndef __HAVE_ARCH_STRNCAT
  57 char * strncat(char *dest, const char *src, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         char *tmp = dest;
  60 
  61         if (count) {
  62                 while (*dest)
  63                         dest++;
  64                 while ((*dest++ = *src++)) {
  65                         if (--count == 0) {
  66                                 *dest = '\0';
  67                                 break;
  68                         }
  69                 }
  70         }
  71 
  72         return tmp;
  73 }
  74 #endif
  75 
  76 #ifndef __HAVE_ARCH_STRCMP
  77 int strcmp(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79         register signed char __res;
  80 
  81         while (1) {
  82                 if ((__res = *cs - *ct++) != 0 || !*cs++)
  83                         break;
  84         }
  85 
  86         return __res;
  87 }
  88 #endif
  89 
  90 #ifndef __HAVE_ARCH_STRNCMP
  91 int strncmp(const char * cs,const char * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93         register signed char __res = 0;
  94 
  95         while (count) {
  96                 if ((__res = *cs - *ct++) != 0 || !*cs++)
  97                         break;
  98                 count--;
  99         }
 100 
 101         return __res;
 102 }
 103 #endif
 104 
 105 #ifndef __HAVE_ARCH_STRCHR
 106 char * strchr(const char * s, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108         for(; *s != (char) c; ++s)
 109                 if (*s == '\0')
 110                         return NULL;
 111         return (char *) s;
 112 }
 113 #endif
 114 
 115 #ifndef __HAVE_ARCH_STRRCHR
 116 char * strrchr(const char * s, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118        const char *p = s + strlen(s);
 119        do {
 120            if (*p == (char)c)
 121                return (char *)p;
 122        } while (--p >= s);
 123        return NULL;
 124 }
 125 #endif
 126 
 127 #ifndef __HAVE_ARCH_STRLEN
 128 size_t strlen(const char * s)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         const char *sc;
 131 
 132         for (sc = s; *sc != '\0'; ++sc)
 133                 /* nothing */;
 134         return sc - s;
 135 }
 136 #endif
 137 
 138 #ifndef __HAVE_ARCH_STRNLEN
 139 size_t strnlen(const char * s, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         const char *sc;
 142 
 143         for (sc = s; count-- && *sc != '\0'; ++sc)
 144                 /* nothing */;
 145         return sc - s;
 146 }
 147 #endif
 148 
 149 #ifndef __HAVE_ARCH_STRSPN
 150 size_t strspn(const char *s, const char *accept)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152         const char *p;
 153         const char *a;
 154         size_t count = 0;
 155 
 156         for (p = s; *p != '\0'; ++p) {
 157                 for (a = accept; *a != '\0'; ++a) {
 158                         if (*p == *a)
 159                                 break;
 160                 }
 161                 if (*a == '\0')
 162                         return count;
 163                 ++count;
 164         }
 165 
 166         return count;
 167 }
 168 #endif
 169 
 170 #ifndef __HAVE_ARCH_STRPBRK
 171 char * strpbrk(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         const char *sc1,*sc2;
 174 
 175         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
 176                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
 177                         if (*sc1 == *sc2)
 178                                 return (char *) sc1;
 179                 }
 180         }
 181         return NULL;
 182 }
 183 #endif
 184 
 185 #ifndef __HAVE_ARCH_STRTOK
 186 char * strtok(char * s,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188         char *sbegin, *send;
 189 
 190         sbegin  = s ? s : ___strtok;
 191         if (!sbegin) {
 192                 return NULL;
 193         }
 194         sbegin += strspn(sbegin,ct);
 195         if (*sbegin == '\0') {
 196                 ___strtok = NULL;
 197                 return( NULL );
 198         }
 199         send = strpbrk( sbegin, ct);
 200         if (send && *send != '\0')
 201                 *send++ = '\0';
 202         ___strtok = send;
 203         return (sbegin);
 204 }
 205 #endif
 206 
 207 #ifndef __HAVE_ARCH_MEMSET
 208 void * memset(void * s,char c,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210         char *xs = (char *) s;
 211 
 212         while (count--)
 213                 *xs++ = c;
 214 
 215         return s;
 216 }
 217 #endif
 218 
 219 #ifndef __HAVE_ARCH_BCOPY
 220 char * bcopy(const char * src, char * dest, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         char *tmp = dest;
 223 
 224         while (count--)
 225                 *tmp++ = *src++;
 226 
 227         return dest;
 228 }
 229 #endif
 230 
 231 #ifndef __HAVE_ARCH_MEMCPY
 232 void * memcpy(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234         char *tmp = (char *) dest, *s = (char *) src;
 235 
 236         while (count--)
 237                 *tmp++ = *s++;
 238 
 239         return dest;
 240 }
 241 #endif
 242 
 243 #ifndef __HAVE_ARCH_MEMMOVE
 244 void * memmove(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 245 {
 246         char *tmp, *s;
 247 
 248         if (dest <= src) {
 249                 tmp = (char *) dest;
 250                 s = (char *) src;
 251                 while (count--)
 252                         *tmp++ = *s++;
 253                 }
 254         else {
 255                 tmp = (char *) dest + count;
 256                 s = (char *) src + count;
 257                 while (count--)
 258                         *--tmp = *--s;
 259                 }
 260 
 261         return dest;
 262 }
 263 #endif
 264 
 265 #ifndef __HAVE_ARCH_MEMCMP
 266 int memcmp(const void * cs,const void * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 267 {
 268         const unsigned char *su1, *su2;
 269         signed char res = 0;
 270 
 271         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
 272                 if ((res = *su1 - *su2) != 0)
 273                         break;
 274         return res;
 275 }
 276 #endif
 277 
 278 /*
 279  * find the first occurrence of byte 'c', or 1 past the area if none
 280  */
 281 #ifndef __HAVE_ARCH_MEMSCAN
 282 void * memscan(void * addr, int c, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284         unsigned char * p = (unsigned char *) addr;
 285 
 286         while (size) {
 287                 if (*p == c)
 288                         return (void *) p;
 289                 p++;
 290                 size--;
 291         }
 292         return (void *) p;
 293 }
 294 #endif
 295 
 296 #ifndef __HAVE_ARCH_STRSTR
 297 char * strstr(const char * s1,const char * s2)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299         int l1, l2;
 300 
 301         l2 = strlen(s2);
 302         if (!l2)
 303                 return (char *) s1;
 304         l1 = strlen(s1);
 305         while (l1 >= l2) {
 306                 l1--;
 307                 if (!memcmp(s1,s2,l2))
 308                         return (char *) s1;
 309                 s1++;
 310         }
 311         return NULL;
 312 }
 313 #endif

/* [previous][next][first][last][top][bottom][index][help] */