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. memcpy
  14. memmove
  15. memcmp
  16. 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 ((*dest++ = *src++) != '\0' && --count)
  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 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 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         const char ch = c;
  93 
  94         for(; *s != ch; ++s)
  95                 if (*s == '\0')
  96                         return NULL;
  97         return (char *) s;
  98 }
  99 
 100 size_t strlen(const char * s)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102         const char *sc;
 103 
 104         for (sc = s; *sc != '\0'; ++sc)
 105                 /* nothing */;
 106         return sc - s;
 107 }
 108 
 109 size_t strspn(const char *s, const char *accept)
     /* [previous][next][first][last][top][bottom][index][help] */
 110 {
 111         const char *p;
 112         const char *a;
 113         size_t count = 0;
 114 
 115         for (p = s; *p != '\0'; ++p) {
 116                 for (a = accept; *a != '\0'; ++a) {
 117                         if (*p == *a)
 118                                 break;
 119                 }
 120                 if (*a == '\0')
 121                         return count;
 122                 ++count;
 123         }
 124 
 125         return count;
 126 }
 127 
 128 char * strpbrk(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         const char *sc1,*sc2;
 131 
 132         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
 133                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
 134                         if (*sc1 == *sc2)
 135                                 return (char *) sc1;
 136                 }
 137         }
 138         return NULL;
 139 }
 140 
 141 char * strtok(char * s,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143         char *sbegin, *send;
 144 
 145         sbegin  = s ? s : ___strtok;
 146         if (!sbegin) {
 147                 return NULL;
 148         }
 149         sbegin += strspn(sbegin,ct);
 150         if (*sbegin == '\0') {
 151                 ___strtok = NULL;
 152                 return( NULL );
 153         }
 154         send = strpbrk( sbegin, ct);
 155         if (send && *send != '\0')
 156                 *send++ = '\0';
 157         ___strtok = send;
 158         return (sbegin);
 159 }
 160 
 161 void * memset(void * s,char c,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163         char *xs = (char *) s;
 164 
 165         while (count--)
 166                 *xs++ = c;
 167 
 168         return s;
 169 }
 170 
 171 void * memcpy(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         char *tmp = (char *) dest, *s = (char *) src;
 174 
 175         while (count--)
 176                 *tmp++ = *s++;
 177 
 178         return dest;
 179 }
 180 
 181 void * memmove(void * dest,const void *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 182 {
 183         char *tmp = (char *) dest, *s = (char *) src;
 184 
 185         while (count--)
 186                 *tmp++ = *s++;
 187 
 188         return dest;
 189 }
 190 
 191 int memcmp(const void * cs,const void * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 192 {
 193         const unsigned char *su1, *su2;
 194 
 195         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
 196                 if (*su1 != *su2)
 197                         return((*su1 < *su2) ? -1 : +1);
 198         return(0);
 199 }
 200 
 201 /*
 202  * find the first occurrence of byte 'c', or 1 past the area if none
 203  */
 204 extern inline void * memscan(void * addr, unsigned char c, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         unsigned char * p = (unsigned char *) addr;
 207 
 208         while (size) {
 209                 if (*p == c)
 210                         return (void *) p;
 211                 p++;
 212                 size--;
 213         }
 214         return (void *) p;
 215 }

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