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

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