stringextra.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 /*
00014  * stupid library routines.. The optimized versions should generally be found
00015  * as inline code in <asm-xx/string.h>
00016  *
00017  * These are buggy as well..
00018  *
00019  * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
00020  * -  Added strsep() which will replace strtok() soon (because strsep() is
00021  *    reentrant and should be faster). Use only strsep() in new code, please.
00022  */
00023 
00024 /*
00025  * these are the standard string functions that are currently not used by
00026  * any code in etherboot. put into a separate file to avoid linking them in
00027  * with the rest of string.o
00028  * if anything ever does want to use a function of these, consider moving
00029  * the function in question back into string.c
00030  */
00031  
00032 #include <stdint.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <ctype.h>
00036 
00037 /* *** FROM string.c *** */
00038 
00039 #ifndef __HAVE_ARCH_STRNICMP
00040 /**
00041  * strnicmp - Case insensitive, length-limited string comparison
00042  * @s1: One string
00043  * @s2: The other string
00044  * @len: the maximum number of characters to compare
00045  */
00046 int strnicmp(const char *s1, const char *s2, size_t len)
00047 {
00048         /* Yes, Virginia, it had better be unsigned */
00049         unsigned char c1, c2;
00050 
00051         c1 = 0; c2 = 0;
00052         if (len) {
00053                 do {
00054                         c1 = *s1; c2 = *s2;
00055                         s1++; s2++;
00056                         if (!c1)
00057                                 break;
00058                         if (!c2)
00059                                 break;
00060                         if (c1 == c2)
00061                                 continue;
00062                         c1 = tolower(c1);
00063                         c2 = tolower(c2);
00064                         if (c1 != c2)
00065                                 break;
00066                 } while (--len);
00067         }
00068         return (int)c1 - (int)c2;
00069 }
00070 #endif
00071 
00072 char * ___strtok;
00073 
00074 #ifndef __HAVE_ARCH_STRNCAT
00075 /**
00076  * strncat - Append a length-limited, %NUL-terminated string to another
00077  * @dest: The string to be appended to
00078  * @src: The string to append to it
00079  * @count: The maximum numbers of bytes to copy
00080  *
00081  * Note that in contrast to strncpy, strncat ensures the result is
00082  * terminated.
00083  */
00084 char * strncat(char *dest, const char *src, size_t count)
00085 {
00086         char *tmp = dest;
00087 
00088         if (count) {
00089                 while (*dest)
00090                         dest++;
00091                 while ((*dest++ = *src++)) {
00092                         if (--count == 0) {
00093                                 *dest = '\0';
00094                                 break;
00095                         }
00096                 }
00097         }
00098 
00099         return tmp;
00100 }
00101 #endif
00102 
00103 #ifndef __HAVE_ARCH_STRSPN
00104 /**
00105  * strspn - Calculate the length of the initial substring of @s which only
00106  *      contain letters in @accept
00107  * @s: The string to be searched
00108  * @accept: The string to search for
00109  */
00110 size_t strspn(const char *s, const char *accept)
00111 {
00112         const char *p;
00113         const char *a;
00114         size_t count = 0;
00115 
00116         for (p = s; *p != '\0'; ++p) {
00117                 for (a = accept; *a != '\0'; ++a) {
00118                         if (*p == *a)
00119                                 break;
00120                 }
00121                 if (*a == '\0')
00122                         return count;
00123                 ++count;
00124         }
00125 
00126         return count;
00127 }
00128 #endif
00129 
00130 #ifndef __HAVE_ARCH_STRCSPN
00131 /**
00132  * strcspn - Calculate the length of the initial substring of @s which only
00133  *      contain letters not in @reject
00134  * @s: The string to be searched
00135  * @accept: The string to search for
00136  */
00137 size_t strcspn(const char *s, const char *reject)
00138 {
00139         const char *p;
00140         const char *r;
00141         size_t count = 0;
00142 
00143         for (p = s; *p != '\0'; ++p) {
00144                 for (r = reject; *r != '\0'; ++r) {
00145                         if (*p == *r)
00146                                 return count;
00147                 }
00148                 ++count;
00149         }
00150 
00151         return count;
00152 }
00153 #endif
00154 
00155 #ifndef __HAVE_ARCH_STRPBRK
00156 /**
00157  * strpbrk - Find the first occurrence of a set of characters
00158  * @cs: The string to be searched
00159  * @ct: The characters to search for
00160  */
00161 char * strpbrk(const char * cs,const char * ct)
00162 {
00163         const char *sc1,*sc2;
00164 
00165         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
00166                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
00167                         if (*sc1 == *sc2)
00168                                 return (char *) sc1;
00169                 }
00170         }
00171         return NULL;
00172 }
00173 #endif
00174 
00175 #ifndef __HAVE_ARCH_STRTOK
00176 /**
00177  * strtok - Split a string into tokens
00178  * @s: The string to be searched
00179  * @ct: The characters to search for
00180  *
00181  * WARNING: strtok is deprecated, use strsep instead.
00182  */
00183 char * strtok(char * s,const char * ct)
00184 {
00185         char *sbegin, *send;
00186 
00187         sbegin  = s ? s : ___strtok;
00188         if (!sbegin) {
00189                 return NULL;
00190         }
00191         sbegin += strspn(sbegin,ct);
00192         if (*sbegin == '\0') {
00193                 ___strtok = NULL;
00194                 return( NULL );
00195         }
00196         send = strpbrk( sbegin, ct);
00197         if (send && *send != '\0')
00198                 *send++ = '\0';
00199         ___strtok = send;
00200         return (sbegin);
00201 }
00202 #endif
00203 
00204 #ifndef __HAVE_ARCH_STRSEP
00205 /**
00206  * strsep - Split a string into tokens
00207  * @s: The string to be searched
00208  * @ct: The characters to search for
00209  *
00210  * strsep() updates @s to point after the token, ready for the next call.
00211  *
00212  * It returns empty tokens, too, behaving exactly like the libc function
00213  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
00214  * Same semantics, slimmer shape. ;)
00215  */
00216 char * strsep(char **s, const char *ct)
00217 {
00218         char *sbegin = *s, *end;
00219 
00220         if (sbegin == NULL)
00221                 return NULL;
00222 
00223         end = strpbrk(sbegin, ct);
00224         if (end)
00225                 *end++ = '\0';
00226         *s = end;
00227 
00228         return sbegin;
00229 }
00230 #endif
00231 
00232 #ifndef __HAVE_ARCH_BCOPY
00233 /**
00234  * bcopy - Copy one area of memory to another
00235  * @src: Where to copy from
00236  * @dest: Where to copy to
00237  * @count: The size of the area.
00238  *
00239  * Note that this is the same as memcpy(), with the arguments reversed.
00240  * memcpy() is the standard, bcopy() is a legacy BSD function.
00241  *
00242  * You should not use this function to access IO space, use memcpy_toio()
00243  * or memcpy_fromio() instead.
00244  */
00245 char * bcopy(const char * src, char * dest, int count)
00246 {
00247         return memmove(dest,src,count);
00248 }
00249 #endif
00250 
00251 #ifndef __HAVE_ARCH_MEMSCAN
00252 /**
00253  * memscan - Find a character in an area of memory.
00254  * @addr: The memory area
00255  * @c: The byte to search for
00256  * @size: The size of the area.
00257  *
00258  * returns the address of the first occurrence of @c, or 1 byte past
00259  * the area if @c is not found
00260  */
00261 void * memscan(const void * addr, int c, size_t size)
00262 {
00263         unsigned char * p = (unsigned char *) addr;
00264 
00265         while (size) {
00266                 if (*p == c)
00267                         return (void *) p;
00268                 p++;
00269                 size--;
00270         }
00271         return (void *) p;
00272 }
00273 #endif

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