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. strspn
  10. strpbrk
  11. strtok
  12. memset
  13. bcopy
  14. memcpy
  15. memmove
  16. memcmp
  17. memscan

   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 
  16 char * ___strtok = NULL;
  17 
  18 char * strcpy(char * dest,const char *src)
     /* [previous][next][first][last][top][bottom][index][help] */
  19 {
  20         char *tmp = dest;
  21 
  22         while ((*dest++ = *src++) != '\0')
  23                 /* nothing */;
  24         return tmp;
  25 }
  26 
  27 char * strncpy(char * dest,const char *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         char *tmp = dest;
  30 
  31         while (count-- && (*dest++ = *src++) != '\0')
  32                 /* nothing */;
  33 
  34         return tmp;
  35 }
  36 
  37 char * strcat(char * dest, const char * src)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39         char *tmp = dest;
  40 
  41         while (*dest)
  42                 dest++;
  43         while ((*dest++ = *src++) != '\0')
  44                 ;
  45 
  46         return tmp;
  47 }
  48 
  49 char * strncat(char *dest, const char *src, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51         char *tmp = dest;
  52 
  53         if (count) {
  54                 while (*dest)
  55                         dest++;
  56                 while ((*dest++ = *src++)) {
  57                         if (--count == 0)
  58                                 break;
  59                 }
  60         }
  61 
  62         return tmp;
  63 }
  64 
  65 int strcmp(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67         register signed char __res;
  68 
  69         while (1) {
  70                 if ((__res = *cs - *ct++) != 0 || !*cs++)
  71                         break;
  72         }
  73 
  74         return __res;
  75 }
  76 
  77 int strncmp(const char * cs,const char * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79         register signed char __res = 0;
  80 
  81         while (count) {
  82                 if ((__res = *cs - *ct++) != 0 || !*cs++)
  83                         break;
  84                 count--;
  85         }
  86 
  87         return __res;
  88 }
  89 
  90 char * strchr(const char * s,char c)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         for(; *s != c; ++s)
  93                 if (*s == '\0')
  94                         return NULL;
  95         return (char *) s;
  96 }
  97 
  98 size_t strlen(const char * s)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         const char *sc;
 101 
 102         for (sc = s; *sc != '\0'; ++sc)
 103                 /* nothing */;
 104         return sc - s;
 105 }
 106 
 107 size_t strspn(const char *s, const char *accept)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         const char *p;
 110         const char *a;
 111         size_t count = 0;
 112 
 113         for (p = s; *p != '\0'; ++p) {
 114                 for (a = accept; *a != '\0'; ++a) {
 115                         if (*p == *a)
 116                                 break;
 117                 }
 118                 if (*a == '\0')
 119                         return count;
 120                 ++count;
 121         }
 122 
 123         return count;
 124 }
 125 
 126 char * strpbrk(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128         const char *sc1,*sc2;
 129 
 130         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
 131                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
 132                         if (*sc1 == *sc2)
 133                                 return (char *) sc1;
 134                 }
 135         }
 136         return NULL;
 137 }
 138 
 139 char * strtok(char * s,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         char *sbegin, *send;
 142 
 143         sbegin  = s ? s : ___strtok;
 144         if (!sbegin) {
 145                 return NULL;
 146         }
 147         sbegin += strspn(sbegin,ct);
 148         if (*sbegin == '\0') {
 149                 ___strtok = NULL;
 150                 return( NULL );
 151         }
 152         send = strpbrk( sbegin, ct);
 153         if (send && *send != '\0')
 154                 *send++ = '\0';
 155         ___strtok = send;
 156         return (sbegin);
 157 }
 158 
 159 void * memset(void * s,char c,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 160 {
 161         char *xs = (char *) s;
 162 
 163         while (count--)
 164                 *xs++ = c;
 165 
 166         return s;
 167 }
 168 
 169 char * bcopy(const char * src, char * dest, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171         char *tmp = dest;
 172 
 173         while (count--)
 174                 *tmp++ = *src++;
 175 
 176         return dest;
 177 }
 178 
 179 void * memcpy(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         char *tmp = (char *) dest, *s = (char *) src;
 182 
 183         while (count--)
 184                 *tmp++ = *s++;
 185 
 186         return dest;
 187 }
 188 
 189 void * memmove(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191         char *tmp, *s;
 192 
 193         if (dest <= src) {
 194                 tmp = (char *) dest;
 195                 s = (char *) src;
 196                 while (count--)
 197                         *tmp++ = *s++;
 198                 }
 199         else {
 200                 tmp = (char *) dest + count;
 201                 s = (char *) src + count;
 202                 while (count--)
 203                         *--tmp = *--s;
 204                 }
 205 
 206         return dest;
 207 }
 208 
 209 int memcmp(const void * cs,const void * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211         const unsigned char *su1, *su2;
 212         signed char res = 0;
 213 
 214         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
 215                 if ((res = *su1 - *su2) != 0)
 216                         break;
 217         return res;
 218 }
 219 
 220 /*
 221  * find the first occurrence of byte 'c', or 1 past the area if none
 222  */
 223 void * memscan(void * addr, unsigned char c, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
 224 {
 225         unsigned char * p = (unsigned char *) addr;
 226 
 227         while (size) {
 228                 if (*p == c)
 229                         return (void *) p;
 230                 p++;
 231                 size--;
 232         }
 233         return (void *) p;
 234 }

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