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. strlen
  9. strnlen
  10. strspn
  11. strpbrk
  12. strtok
  13. memset
  14. bcopy
  15. memcpy
  16. memmove
  17. memcmp
  18. memscan
  19. 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_STRLEN
 116 size_t strlen(const char * s)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         const char *sc;
 119 
 120         for (sc = s; *sc != '\0'; ++sc)
 121                 /* nothing */;
 122         return sc - s;
 123 }
 124 #endif
 125 
 126 #ifndef __HAVE_ARCH_STRNLEN
 127 size_t strnlen(const char * s, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {
 129         const char *sc;
 130 
 131         for (sc = s; count-- && *sc != '\0'; ++sc)
 132                 /* nothing */;
 133         return sc - s;
 134 }
 135 #endif
 136 
 137 #ifndef __HAVE_ARCH_STRSPN
 138 size_t strspn(const char *s, const char *accept)
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140         const char *p;
 141         const char *a;
 142         size_t count = 0;
 143 
 144         for (p = s; *p != '\0'; ++p) {
 145                 for (a = accept; *a != '\0'; ++a) {
 146                         if (*p == *a)
 147                                 break;
 148                 }
 149                 if (*a == '\0')
 150                         return count;
 151                 ++count;
 152         }
 153 
 154         return count;
 155 }
 156 #endif
 157 
 158 #ifndef __HAVE_ARCH_STRPBRK
 159 char * strpbrk(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 160 {
 161         const char *sc1,*sc2;
 162 
 163         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
 164                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
 165                         if (*sc1 == *sc2)
 166                                 return (char *) sc1;
 167                 }
 168         }
 169         return NULL;
 170 }
 171 #endif
 172 
 173 #ifndef __HAVE_ARCH_STRTOK
 174 char * strtok(char * s,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176         char *sbegin, *send;
 177 
 178         sbegin  = s ? s : ___strtok;
 179         if (!sbegin) {
 180                 return NULL;
 181         }
 182         sbegin += strspn(sbegin,ct);
 183         if (*sbegin == '\0') {
 184                 ___strtok = NULL;
 185                 return( NULL );
 186         }
 187         send = strpbrk( sbegin, ct);
 188         if (send && *send != '\0')
 189                 *send++ = '\0';
 190         ___strtok = send;
 191         return (sbegin);
 192 }
 193 #endif
 194 
 195 #ifndef __HAVE_ARCH_MEMSET
 196 void * memset(void * s,char c,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198         char *xs = (char *) s;
 199 
 200         while (count--)
 201                 *xs++ = c;
 202 
 203         return s;
 204 }
 205 #endif
 206 
 207 #ifndef __HAVE_ARCH_BCOPY
 208 char * bcopy(const char * src, char * dest, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210         char *tmp = dest;
 211 
 212         while (count--)
 213                 *tmp++ = *src++;
 214 
 215         return dest;
 216 }
 217 #endif
 218 
 219 #ifndef __HAVE_ARCH_MEMCPY
 220 void * memcpy(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         char *tmp = (char *) dest, *s = (char *) src;
 223 
 224         while (count--)
 225                 *tmp++ = *s++;
 226 
 227         return dest;
 228 }
 229 #endif
 230 
 231 #ifndef __HAVE_ARCH_MEMMOVE
 232 void * memmove(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234         char *tmp, *s;
 235 
 236         if (dest <= src) {
 237                 tmp = (char *) dest;
 238                 s = (char *) src;
 239                 while (count--)
 240                         *tmp++ = *s++;
 241                 }
 242         else {
 243                 tmp = (char *) dest + count;
 244                 s = (char *) src + count;
 245                 while (count--)
 246                         *--tmp = *--s;
 247                 }
 248 
 249         return dest;
 250 }
 251 #endif
 252 
 253 #ifndef __HAVE_ARCH_MEMCMP
 254 int memcmp(const void * cs,const void * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256         const unsigned char *su1, *su2;
 257         signed char res = 0;
 258 
 259         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
 260                 if ((res = *su1 - *su2) != 0)
 261                         break;
 262         return res;
 263 }
 264 #endif
 265 
 266 /*
 267  * find the first occurrence of byte 'c', or 1 past the area if none
 268  */
 269 #ifndef __HAVE_ARCH_MEMSCAN
 270 void * memscan(void * addr, int c, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272         unsigned char * p = (unsigned char *) addr;
 273 
 274         while (size) {
 275                 if (*p == c)
 276                         return (void *) p;
 277                 p++;
 278                 size--;
 279         }
 280         return (void *) p;
 281 }
 282 #endif
 283 
 284 #ifndef __HAVE_ARCH_STRSTR
 285 char * strstr(const char * s1,const char * s2)
     /* [previous][next][first][last][top][bottom][index][help] */
 286 {
 287         int l1, l2;
 288 
 289         l2 = strlen(s2);
 290         if (!l2)
 291                 return (char *) s1;
 292         l1 = strlen(s1);
 293         while (l1 >= l2) {
 294                 l1--;
 295                 if (!memcmp(s1,s2,l2))
 296                         return (char *) s1;
 297                 s1++;
 298         }
 299         return NULL;
 300 }
 301 #endif

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