string.c

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 1991, 1992  Linus Torvalds
00003  *  Copyright (C) 2004 Tobias Lorenz
00004  *
00005  *  string handling functions
00006  *  based on linux/lib/string.c
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License version 2 as
00010  * published by the Free Software Foundation.
00011  */
00012 
00013 FILE_LICENCE ( GPL2_ONLY );
00014 
00015 /*
00016  * stupid library routines.. The optimized versions should generally be found
00017  * as inline code in <asm-xx/string.h>
00018  *
00019  * These are buggy as well..
00020  *
00021  * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
00022  * -  Added strsep() which will replace strtok() soon (because strsep() is
00023  *    reentrant and should be faster). Use only strsep() in new code, please.
00024  */
00025  
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <ctype.h>
00030 
00031 /* *** FROM string.c *** */
00032 
00033 #ifndef __HAVE_ARCH_STRCPY
00034 /**
00035  * strcpy - Copy a %NUL terminated string
00036  * @dest: Where to copy the string to
00037  * @src: Where to copy the string from
00038  */
00039 char * strcpy(char * dest,const char *src)
00040 {
00041         char *tmp = dest;
00042 
00043         while ((*dest++ = *src++) != '\0')
00044                 /* nothing */;
00045         return tmp;
00046 }
00047 #endif
00048 
00049 #ifndef __HAVE_ARCH_STRNCPY
00050 /**
00051  * strncpy - Copy a length-limited, %NUL-terminated string
00052  * @dest: Where to copy the string to
00053  * @src: Where to copy the string from
00054  * @count: The maximum number of bytes to copy
00055  *
00056  * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
00057  * However, the result is not %NUL-terminated if the source exceeds
00058  * @count bytes.
00059  */
00060 char * strncpy(char * dest,const char *src,size_t count)
00061 {
00062         char *tmp = dest;
00063 
00064         while (count-- && (*dest++ = *src++) != '\0')
00065                 /* nothing */;
00066 
00067         return tmp;
00068 }
00069 #endif
00070 
00071 #ifndef __HAVE_ARCH_STRCAT
00072 /**
00073  * strcat - Append one %NUL-terminated string to another
00074  * @dest: The string to be appended to
00075  * @src: The string to append to it
00076  */
00077 char * strcat(char * dest, const char * src)
00078 {
00079         char *tmp = dest;
00080 
00081         while (*dest)
00082                 dest++;
00083         while ((*dest++ = *src++) != '\0')
00084                 ;
00085 
00086         return tmp;
00087 }
00088 #endif
00089 
00090 #ifndef __HAVE_ARCH_STRCMP
00091 /**
00092  * strcmp - Compare two strings
00093  * @cs: One string
00094  * @ct: Another string
00095  */
00096 int strcmp(const char * cs,const char * ct)
00097 {
00098         register signed char __res;
00099 
00100         while (1) {
00101                 if ((__res = *cs - *ct++) != 0 || !*cs++)
00102                         break;
00103         }
00104 
00105         return __res;
00106 }
00107 #endif
00108 
00109 #ifndef __HAVE_ARCH_STRNCMP
00110 /**
00111  * strncmp - Compare two length-limited strings
00112  * @cs: One string
00113  * @ct: Another string
00114  * @count: The maximum number of bytes to compare
00115  */
00116 int strncmp(const char * cs,const char * ct,size_t count)
00117 {
00118         register signed char __res = 0;
00119 
00120         while (count) {
00121                 if ((__res = *cs - *ct++) != 0 || !*cs++)
00122                         break;
00123                 count--;
00124         }
00125 
00126         return __res;
00127 }
00128 #endif
00129 
00130 #ifndef __HAVE_ARCH_STRCASECMP
00131 int strcasecmp(const char *a, const char *b)
00132 {
00133         while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
00134         return((*a & ~0x20) - (*b & ~0x20));
00135 }
00136 #endif
00137 
00138 #ifndef __HAVE_ARCH_STRCHR
00139 /**
00140  * strchr - Find the first occurrence of a character in a string
00141  * @s: The string to be searched
00142  * @c: The character to search for
00143  */
00144 char * strchr(const char * s, int c)
00145 {
00146         for(; *s != (char) c; ++s)
00147                 if (*s == '\0')
00148                         return NULL;
00149         return (char *) s;
00150 }
00151 #endif
00152 
00153 #ifndef __HAVE_ARCH_STRRCHR
00154 /**
00155  * strrchr - Find the last occurrence of a character in a string
00156  * @s: The string to be searched
00157  * @c: The character to search for
00158  */
00159 char * strrchr(const char * s, int c)
00160 {
00161        const char *p = s + strlen(s);
00162        do {
00163            if (*p == (char)c)
00164                return (char *)p;
00165        } while (--p >= s);
00166        return NULL;
00167 }
00168 #endif
00169 
00170 #ifndef __HAVE_ARCH_STRLEN
00171 /**
00172  * strlen - Find the length of a string
00173  * @s: The string to be sized
00174  */
00175 size_t strlen(const char * s)
00176 {
00177         const char *sc;
00178 
00179         for (sc = s; *sc != '\0'; ++sc)
00180                 /* nothing */;
00181         return sc - s;
00182 }
00183 #endif
00184 
00185 #ifndef __HAVE_ARCH_STRNLEN
00186 /**
00187  * strnlen - Find the length of a length-limited string
00188  * @s: The string to be sized
00189  * @count: The maximum number of bytes to search
00190  */
00191 size_t strnlen(const char * s, size_t count)
00192 {
00193         const char *sc;
00194 
00195         for (sc = s; count-- && *sc != '\0'; ++sc)
00196                 /* nothing */;
00197         return sc - s;
00198 }
00199 #endif
00200 
00201 #ifndef __HAVE_ARCH_MEMSET
00202 /**
00203  * memset - Fill a region of memory with the given value
00204  * @s: Pointer to the start of the area.
00205  * @c: The byte to fill the area with
00206  * @count: The size of the area.
00207  *
00208  * Do not use memset() to access IO space, use memset_io() instead.
00209  */
00210 void * memset(void * s,int c,size_t count)
00211 {
00212         char *xs = (char *) s;
00213 
00214         while (count--)
00215                 *xs++ = c;
00216 
00217         return s;
00218 }
00219 #endif
00220 
00221 #ifndef __HAVE_ARCH_MEMCPY
00222 /**
00223  * memcpy - Copy one area of memory to another
00224  * @dest: Where to copy to
00225  * @src: Where to copy from
00226  * @count: The size of the area.
00227  *
00228  * You should not use this function to access IO space, use memcpy_toio()
00229  * or memcpy_fromio() instead.
00230  */
00231 void * memcpy(void * dest,const void *src,size_t count)
00232 {
00233         char *tmp = (char *) dest, *s = (char *) src;
00234 
00235         while (count--)
00236                 *tmp++ = *s++;
00237 
00238         return dest;
00239 }
00240 #endif
00241 
00242 #ifndef __HAVE_ARCH_MEMMOVE
00243 /**
00244  * memmove - Copy one area of memory to another
00245  * @dest: Where to copy to
00246  * @src: Where to copy from
00247  * @count: The size of the area.
00248  *
00249  * Unlike memcpy(), memmove() copes with overlapping areas.
00250  */
00251 void * memmove(void * dest,const void *src,size_t count)
00252 {
00253         char *tmp, *s;
00254 
00255         if (dest <= src) {
00256                 tmp = (char *) dest;
00257                 s = (char *) src;
00258                 while (count--)
00259                         *tmp++ = *s++;
00260                 }
00261         else {
00262                 tmp = (char *) dest + count;
00263                 s = (char *) src + count;
00264                 while (count--)
00265                         *--tmp = *--s;
00266                 }
00267 
00268         return dest;
00269 }
00270 #endif
00271 
00272 #ifndef __HAVE_ARCH_MEMCMP
00273 /**
00274  * memcmp - Compare two areas of memory
00275  * @cs: One area of memory
00276  * @ct: Another area of memory
00277  * @count: The size of the area.
00278  */
00279 int memcmp(const void * cs,const void * ct,size_t count)
00280 {
00281         const unsigned char *su1, *su2;
00282         int res = 0;
00283 
00284         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
00285                 if ((res = *su1 - *su2) != 0)
00286                         break;
00287         return res;
00288 }
00289 #endif
00290 
00291 #ifndef __HAVE_ARCH_STRSTR
00292 /**
00293  * strstr - Find the first substring in a %NUL terminated string
00294  * @s1: The string to be searched
00295  * @s2: The string to search for
00296  */
00297 char * strstr(const char * s1,const char * s2)
00298 {
00299         int l1, l2;
00300 
00301         l2 = strlen(s2);
00302         if (!l2)
00303                 return (char *) s1;
00304         l1 = strlen(s1);
00305         while (l1 >= l2) {
00306                 l1--;
00307                 if (!memcmp(s1,s2,l2))
00308                         return (char *) s1;
00309                 s1++;
00310         }
00311         return NULL;
00312 }
00313 #endif
00314 
00315 #ifndef __HAVE_ARCH_MEMCHR
00316 /**
00317  * memchr - Find a character in an area of memory.
00318  * @s: The memory area
00319  * @c: The byte to search for
00320  * @n: The size of the area.
00321  *
00322  * returns the address of the first occurrence of @c, or %NULL
00323  * if @c is not found
00324  */
00325 void * memchr(const void *s, int c, size_t n)
00326 {
00327         const unsigned char *p = s;
00328         while (n-- != 0) {
00329                 if ((unsigned char)c == *p++) {
00330                         return (void *)(p-1);
00331                 }
00332         }
00333         return NULL;
00334 }
00335 
00336 #endif
00337 
00338 char * strndup(const char *s, size_t n)
00339 {
00340         size_t len = strlen(s);
00341         char *new;
00342 
00343         if (len>n)
00344                 len = n;
00345         new = malloc(len+1);
00346         if (new) {
00347                 new[len] = '\0';
00348                 memcpy(new,s,len);
00349         }
00350         return new;
00351 }
00352 
00353 char * strdup(const char *s) {
00354         return strndup(s, ~((size_t)0));
00355 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1