string.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 FILE_LICENCE ( GPL2_ONLY );
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <ctype.h>
00030
00031
00032
00033 #ifndef __HAVE_ARCH_STRCPY
00034
00035
00036
00037
00038
00039 char * strcpy(char * dest,const char *src)
00040 {
00041 char *tmp = dest;
00042
00043 while ((*dest++ = *src++) != '\0')
00044 ;
00045 return tmp;
00046 }
00047 #endif
00048
00049 #ifndef __HAVE_ARCH_STRNCPY
00050
00051
00052
00053
00054
00055
00056
00057
00058
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 ;
00066
00067 return tmp;
00068 }
00069 #endif
00070
00071 #ifndef __HAVE_ARCH_STRCAT
00072
00073
00074
00075
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
00093
00094
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
00112
00113
00114
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
00141
00142
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
00156
00157
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
00173
00174
00175 size_t strlen(const char * s)
00176 {
00177 const char *sc;
00178
00179 for (sc = s; *sc != '\0'; ++sc)
00180 ;
00181 return sc - s;
00182 }
00183 #endif
00184
00185 #ifndef __HAVE_ARCH_STRNLEN
00186
00187
00188
00189
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 ;
00197 return sc - s;
00198 }
00199 #endif
00200
00201 #ifndef __HAVE_ARCH_MEMSET
00202
00203
00204
00205
00206
00207
00208
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
00224
00225
00226
00227
00228
00229
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
00245
00246
00247
00248
00249
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
00275
00276
00277
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
00294
00295
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
00318
00319
00320
00321
00322
00323
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 }