Home | History | Annotate | Download | only in core
      1 /*
      2  *  Copyright (C) 1991, 1992  Linus Torvalds
      3  *  Copyright (C) 2004 Tobias Lorenz
      4  *
      5  *  string handling functions
      6  *  based on linux/lib/string.c
      7  *
      8  * This program is free software; you can redistribute it and/or modify
      9  * it under the terms of the GNU General Public License version 2 as
     10  * published by the Free Software Foundation.
     11  */
     12 
     13 FILE_LICENCE ( GPL2_ONLY );
     14 
     15 /*
     16  * stupid library routines.. The optimized versions should generally be found
     17  * as inline code in <asm-xx/string.h>
     18  *
     19  * These are buggy as well..
     20  *
     21  * * Fri Jun 25 1999, Ingo Oeser <ioe (at) informatik.tu-chemnitz.de>
     22  * -  Added strsep() which will replace strtok() soon (because strsep() is
     23  *    reentrant and should be faster). Use only strsep() in new code, please.
     24  */
     25 
     26 #include <stdint.h>
     27 #include <stdlib.h>
     28 #include <string.h>
     29 #include <ctype.h>
     30 
     31 /* *** FROM string.c *** */
     32 
     33 #ifndef __HAVE_ARCH_STRCPY
     34 /**
     35  * strcpy - Copy a %NUL terminated string
     36  * @dest: Where to copy the string to
     37  * @src: Where to copy the string from
     38  */
     39 char * strcpy(char * dest,const char *src)
     40 {
     41 	char *tmp = dest;
     42 
     43 	while ((*dest++ = *src++) != '\0')
     44 		/* nothing */;
     45 	return tmp;
     46 }
     47 #endif
     48 
     49 #ifndef __HAVE_ARCH_STRNCPY
     50 /**
     51  * strncpy - Copy a length-limited, %NUL-terminated string
     52  * @dest: Where to copy the string to
     53  * @src: Where to copy the string from
     54  * @count: The maximum number of bytes to copy
     55  *
     56  * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
     57  * However, the result is not %NUL-terminated if the source exceeds
     58  * @count bytes.
     59  */
     60 char * strncpy(char * dest,const char *src,size_t count)
     61 {
     62 	char *tmp = dest;
     63 
     64 	while (count-- && (*dest++ = *src++) != '\0')
     65 		/* nothing */;
     66 
     67 	return tmp;
     68 }
     69 #endif
     70 
     71 #ifndef __HAVE_ARCH_STRCAT
     72 /**
     73  * strcat - Append one %NUL-terminated string to another
     74  * @dest: The string to be appended to
     75  * @src: The string to append to it
     76  */
     77 char * strcat(char * dest, const char * src)
     78 {
     79 	char *tmp = dest;
     80 
     81 	while (*dest)
     82 		dest++;
     83 	while ((*dest++ = *src++) != '\0')
     84 		;
     85 
     86 	return tmp;
     87 }
     88 #endif
     89 
     90 #ifndef __HAVE_ARCH_STRCMP
     91 /**
     92  * strcmp - Compare two strings
     93  * @cs: One string
     94  * @ct: Another string
     95  */
     96 int strcmp(const char * cs,const char * ct)
     97 {
     98 	register signed char __res;
     99 
    100 	while (1) {
    101 		if ((__res = *cs - *ct++) != 0 || !*cs++)
    102 			break;
    103 	}
    104 
    105 	return __res;
    106 }
    107 #endif
    108 
    109 #ifndef __HAVE_ARCH_STRNCMP
    110 /**
    111  * strncmp - Compare two length-limited strings
    112  * @cs: One string
    113  * @ct: Another string
    114  * @count: The maximum number of bytes to compare
    115  */
    116 int strncmp(const char * cs,const char * ct,size_t count)
    117 {
    118 	register signed char __res = 0;
    119 
    120 	while (count) {
    121 		if ((__res = *cs - *ct++) != 0 || !*cs++)
    122 			break;
    123 		count--;
    124 	}
    125 
    126 	return __res;
    127 }
    128 #endif
    129 
    130 #ifndef __HAVE_ARCH_STRCASECMP
    131 int strcasecmp(const char *a, const char *b)
    132 {
    133 	while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
    134 	return((*a & ~0x20) - (*b & ~0x20));
    135 }
    136 #endif
    137 
    138 #ifndef __HAVE_ARCH_STRCHR
    139 /**
    140  * strchr - Find the first occurrence of a character in a string
    141  * @s: The string to be searched
    142  * @c: The character to search for
    143  */
    144 char * strchr(const char * s, int c)
    145 {
    146 	for(; *s != (char) c; ++s)
    147 		if (*s == '\0')
    148 			return NULL;
    149 	return (char *) s;
    150 }
    151 #endif
    152 
    153 #ifndef __HAVE_ARCH_STRRCHR
    154 /**
    155  * strrchr - Find the last occurrence of a character in a string
    156  * @s: The string to be searched
    157  * @c: The character to search for
    158  */
    159 char * strrchr(const char * s, int c)
    160 {
    161        const char *p = s + strlen(s);
    162        do {
    163            if (*p == (char)c)
    164                return (char *)p;
    165        } while (--p >= s);
    166        return NULL;
    167 }
    168 #endif
    169 
    170 #ifndef __HAVE_ARCH_STRLEN
    171 /**
    172  * strlen - Find the length of a string
    173  * @s: The string to be sized
    174  */
    175 size_t strlen(const char * s)
    176 {
    177 	const char *sc;
    178 
    179 	for (sc = s; *sc != '\0'; ++sc)
    180 		/* nothing */;
    181 	return sc - s;
    182 }
    183 #endif
    184 
    185 #ifndef __HAVE_ARCH_STRNLEN
    186 /**
    187  * strnlen - Find the length of a length-limited string
    188  * @s: The string to be sized
    189  * @count: The maximum number of bytes to search
    190  */
    191 size_t strnlen(const char * s, size_t count)
    192 {
    193 	const char *sc;
    194 
    195 	for (sc = s; count-- && *sc != '\0'; ++sc)
    196 		/* nothing */;
    197 	return sc - s;
    198 }
    199 #endif
    200 
    201 #ifndef __HAVE_ARCH_MEMSET
    202 /**
    203  * memset - Fill a region of memory with the given value
    204  * @s: Pointer to the start of the area.
    205  * @c: The byte to fill the area with
    206  * @count: The size of the area.
    207  *
    208  * Do not use memset() to access IO space, use memset_io() instead.
    209  */
    210 void * memset(void * s,int c,size_t count)
    211 {
    212 	char *xs = (char *) s;
    213 
    214 	while (count--)
    215 		*xs++ = c;
    216 
    217 	return s;
    218 }
    219 #endif
    220 
    221 #ifndef __HAVE_ARCH_MEMCPY
    222 /**
    223  * memcpy - Copy one area of memory to another
    224  * @dest: Where to copy to
    225  * @src: Where to copy from
    226  * @count: The size of the area.
    227  *
    228  * You should not use this function to access IO space, use memcpy_toio()
    229  * or memcpy_fromio() instead.
    230  */
    231 void * memcpy(void * dest,const void *src,size_t count)
    232 {
    233 	char *tmp = (char *) dest, *s = (char *) src;
    234 
    235 	while (count--)
    236 		*tmp++ = *s++;
    237 
    238 	return dest;
    239 }
    240 #endif
    241 
    242 #ifndef __HAVE_ARCH_MEMMOVE
    243 /**
    244  * memmove - Copy one area of memory to another
    245  * @dest: Where to copy to
    246  * @src: Where to copy from
    247  * @count: The size of the area.
    248  *
    249  * Unlike memcpy(), memmove() copes with overlapping areas.
    250  */
    251 void * memmove(void * dest,const void *src,size_t count)
    252 {
    253 	char *tmp, *s;
    254 
    255 	if (dest <= src) {
    256 		tmp = (char *) dest;
    257 		s = (char *) src;
    258 		while (count--)
    259 			*tmp++ = *s++;
    260 		}
    261 	else {
    262 		tmp = (char *) dest + count;
    263 		s = (char *) src + count;
    264 		while (count--)
    265 			*--tmp = *--s;
    266 		}
    267 
    268 	return dest;
    269 }
    270 #endif
    271 
    272 #ifndef __HAVE_ARCH_MEMCMP
    273 /**
    274  * memcmp - Compare two areas of memory
    275  * @cs: One area of memory
    276  * @ct: Another area of memory
    277  * @count: The size of the area.
    278  */
    279 int memcmp(const void * cs,const void * ct,size_t count)
    280 {
    281 	const unsigned char *su1, *su2;
    282 	int res = 0;
    283 
    284 	for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
    285 		if ((res = *su1 - *su2) != 0)
    286 			break;
    287 	return res;
    288 }
    289 #endif
    290 
    291 #ifndef __HAVE_ARCH_STRSTR
    292 /**
    293  * strstr - Find the first substring in a %NUL terminated string
    294  * @s1: The string to be searched
    295  * @s2: The string to search for
    296  */
    297 char * strstr(const char * s1,const char * s2)
    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
    314 
    315 #ifndef __HAVE_ARCH_MEMCHR
    316 /**
    317  * memchr - Find a character in an area of memory.
    318  * @s: The memory area
    319  * @c: The byte to search for
    320  * @n: The size of the area.
    321  *
    322  * returns the address of the first occurrence of @c, or %NULL
    323  * if @c is not found
    324  */
    325 void * memchr(const void *s, int c, size_t n)
    326 {
    327 	const unsigned char *p = s;
    328 	while (n-- != 0) {
    329         	if ((unsigned char)c == *p++) {
    330 			return (void *)(p-1);
    331 		}
    332 	}
    333 	return NULL;
    334 }
    335 
    336 #endif
    337 
    338 char * strndup(const char *s, size_t n)
    339 {
    340         size_t len = strlen(s);
    341         char *new;
    342 
    343         if (len>n)
    344                 len = n;
    345         new = malloc(len+1);
    346         if (new) {
    347                 new[len] = '\0';
    348                 memcpy(new,s,len);
    349         }
    350         return new;
    351 }
    352 
    353 char * strdup(const char *s) {
    354 	return strndup(s, ~((size_t)0));
    355 }
    356