linux-bk/lib/string.c
<<
>>
Prefs
   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 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
  14 * -  Added strsep() which will replace strtok() soon (because strsep() is
  15 *    reentrant and should be faster). Use only strsep() in new code, please.
  16 *
  17 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  18 *                    Matthew Hawkins <matt@mh.dropbear.id.au>
  19 * -  Kissed strtok() goodbye
  20 */
  21 
  22#include <linux/types.h>
  23#include <linux/string.h>
  24#include <linux/ctype.h>
  25#include <linux/module.h>
  26
  27#ifndef __HAVE_ARCH_STRNICMP
  28/**
  29 * strnicmp - Case insensitive, length-limited string comparison
  30 * @s1: One string
  31 * @s2: The other string
  32 * @len: the maximum number of characters to compare
  33 */
  34int strnicmp(const char *s1, const char *s2, size_t len)
  35{
  36        /* Yes, Virginia, it had better be unsigned */
  37        unsigned char c1, c2;
  38
  39        c1 = 0; c2 = 0;
  40        if (len) {
  41                do {
  42                        c1 = *s1; c2 = *s2;
  43                        s1++; s2++;
  44                        if (!c1)
  45                                break;
  46                        if (!c2)
  47                                break;
  48                        if (c1 == c2)
  49                                continue;
  50                        c1 = tolower(c1);
  51                        c2 = tolower(c2);
  52                        if (c1 != c2)
  53                                break;
  54                } while (--len);
  55        }
  56        return (int)c1 - (int)c2;
  57}
  58
  59EXPORT_SYMBOL(strnicmp);
  60#endif
  61
  62#ifndef __HAVE_ARCH_STRCPY
  63/**
  64 * strcpy - Copy a %NUL terminated string
  65 * @dest: Where to copy the string to
  66 * @src: Where to copy the string from
  67 */
  68char * strcpy(char * dest,const char *src)
  69{
  70        char *tmp = dest;
  71
  72        while ((*dest++ = *src++) != '\0')
  73                /* nothing */;
  74        return tmp;
  75}
  76#endif
  77
  78#ifndef __HAVE_ARCH_STRNCPY
  79/**
  80 * strncpy - Copy a length-limited, %NUL-terminated string
  81 * @dest: Where to copy the string to
  82 * @src: Where to copy the string from
  83 * @count: The maximum number of bytes to copy
  84 *
  85 * The result is not %NUL-terminated if the source exceeds
  86 * @count bytes.
  87 */
  88char * strncpy(char * dest,const char *src,size_t count)
  89{
  90        char *tmp = dest;
  91
  92        while (count) {
  93                if ((*tmp = *src) != 0) src++;
  94                tmp++;
  95                count--;
  96        }
  97        return dest;
  98}
  99#endif
 100
 101#ifndef __HAVE_ARCH_STRLCPY
 102/**
 103 * strlcpy - Copy a %NUL terminated string into a sized buffer
 104 * @dest: Where to copy the string to
 105 * @src: Where to copy the string from
 106 * @size: size of destination buffer
 107 *
 108 * Compatible with *BSD: the result is always a valid
 109 * NUL-terminated string that fits in the buffer (unless,
 110 * of course, the buffer size is zero). It does not pad
 111 * out the result like strncpy() does.
 112 */
 113size_t strlcpy(char *dest, const char *src, size_t size)
 114{
 115        size_t ret = strlen(src);
 116
 117        if (size) {
 118                size_t len = (ret >= size) ? size-1 : ret;
 119                memcpy(dest, src, len);
 120                dest[len] = '\0';
 121        }
 122        return ret;
 123}
 124EXPORT_SYMBOL(strlcpy);
 125#endif
 126
 127#ifndef __HAVE_ARCH_STRCAT
 128/**
 129 * strcat - Append one %NUL-terminated string to another
 130 * @dest: The string to be appended to
 131 * @src: The string to append to it
 132 */
 133char * strcat(char * dest, const char * src)
 134{
 135        char *tmp = dest;
 136
 137        while (*dest)
 138                dest++;
 139        while ((*dest++ = *src++) != '\0')
 140                ;
 141
 142        return tmp;
 143}
 144#endif
 145
 146#ifndef __HAVE_ARCH_STRNCAT
 147/**
 148 * strncat - Append a length-limited, %NUL-terminated string to another
 149 * @dest: The string to be appended to
 150 * @src: The string to append to it
 151 * @count: The maximum numbers of bytes to copy
 152 *
 153 * Note that in contrast to strncpy, strncat ensures the result is
 154 * terminated.
 155 */
 156char * strncat(char *dest, const char *src, size_t count)
 157{
 158        char *tmp = dest;
 159
 160        if (count) {
 161                while (*dest)
 162                        dest++;
 163                while ((*dest++ = *src++)) {
 164                        if (--count == 0) {
 165                                *dest = '\0';
 166                                break;
 167                        }
 168                }
 169        }
 170
 171        return tmp;
 172}
 173#endif
 174
 175#ifndef __HAVE_ARCH_STRLCAT
 176/**
 177 * strlcat - Append a length-limited, %NUL-terminated string to another
 178 * @dest: The string to be appended to
 179 * @src: The string to append to it
 180 * @count: The size of the destination buffer.
 181 */
 182size_t strlcat(char *dest, const char *src, size_t count)
 183{
 184        size_t dsize = strlen(dest);
 185        size_t len = strlen(src);
 186        size_t res = dsize + len;
 187
 188        /* This would be a bug */
 189        BUG_ON(dsize >= count);
 190
 191        dest += dsize;
 192        count -= dsize;
 193        if (len >= count)
 194                len = count-1;
 195        memcpy(dest, src, len);
 196        dest[len] = 0;
 197        return res;
 198}
 199EXPORT_SYMBOL(strlcat);
 200#endif
 201
 202#ifndef __HAVE_ARCH_STRCMP
 203/**
 204 * strcmp - Compare two strings
 205 * @cs: One string
 206 * @ct: Another string
 207 */
 208int strcmp(const char * cs,const char * ct)
 209{
 210        register signed char __res;
 211
 212        while (1) {
 213                if ((__res = *cs - *ct++) != 0 || !*cs++)
 214                        break;
 215        }
 216
 217        return __res;
 218}
 219#endif
 220
 221#ifndef __HAVE_ARCH_STRNCMP
 222/**
 223 * strncmp - Compare two length-limited strings
 224 * @cs: One string
 225 * @ct: Another string
 226 * @count: The maximum number of bytes to compare
 227 */
 228int strncmp(const char * cs,const char * ct,size_t count)
 229{
 230        register signed char __res = 0;
 231
 232        while (count) {
 233                if ((__res = *cs - *ct++) != 0 || !*cs++)
 234                        break;
 235                count--;
 236        }
 237
 238        return __res;
 239}
 240#endif
 241
 242#ifndef __HAVE_ARCH_STRCHR
 243/**
 244 * strchr - Find the first occurrence of a character in a string
 245 * @s: The string to be searched
 246 * @c: The character to search for
 247 */
 248char * strchr(const char * s, int c)
 249{
 250        for(; *s != (char) c; ++s)
 251                if (*s == '\0')
 252                        return NULL;
 253        return (char *) s;
 254}
 255#endif
 256
 257#ifndef __HAVE_ARCH_STRRCHR
 258/**
 259 * strrchr - Find the last occurrence of a character in a string
 260 * @s: The string to be searched
 261 * @c: The character to search for
 262 */
 263char * strrchr(const char * s, int c)
 264{
 265       const char *p = s + strlen(s);
 266       do {
 267           if (*p == (char)c)
 268               return (char *)p;
 269       } while (--p >= s);
 270       return NULL;
 271}
 272#endif
 273
 274#ifndef __HAVE_ARCH_STRLEN
 275/**
 276 * strlen - Find the length of a string
 277 * @s: The string to be sized
 278 */
 279size_t strlen(const char * s)
 280{
 281        const char *sc;
 282
 283        for (sc = s; *sc != '\0'; ++sc)
 284                /* nothing */;
 285        return sc - s;
 286}
 287#endif
 288
 289#ifndef __HAVE_ARCH_STRNLEN
 290/**
 291 * strnlen - Find the length of a length-limited string
 292 * @s: The string to be sized
 293 * @count: The maximum number of bytes to search
 294 */
 295size_t strnlen(const char * s, size_t count)
 296{
 297        const char *sc;
 298
 299        for (sc = s; count-- && *sc != '\0'; ++sc)
 300                /* nothing */;
 301        return sc - s;
 302}
 303#endif
 304
 305#ifndef __HAVE_ARCH_STRSPN
 306/**
 307 * strspn - Calculate the length of the initial substring of @s which only
 308 *      contain letters in @accept
 309 * @s: The string to be searched
 310 * @accept: The string to search for
 311 */
 312size_t strspn(const char *s, const char *accept)
 313{
 314        const char *p;
 315        const char *a;
 316        size_t count = 0;
 317
 318        for (p = s; *p != '\0'; ++p) {
 319                for (a = accept; *a != '\0'; ++a) {
 320                        if (*p == *a)
 321                                break;
 322                }
 323                if (*a == '\0')
 324                        return count;
 325                ++count;
 326        }
 327
 328        return count;
 329}
 330
 331EXPORT_SYMBOL(strspn);
 332#endif
 333
 334/**
 335 * strcspn - Calculate the length of the initial substring of @s which does
 336 *      not contain letters in @reject
 337 * @s: The string to be searched
 338 * @reject: The string to avoid
 339 */
 340size_t strcspn(const char *s, const char *reject)
 341{
 342        const char *p;
 343        const char *r;
 344        size_t count = 0;
 345
 346        for (p = s; *p != '\0'; ++p) {
 347                for (r = reject; *r != '\0'; ++r) {
 348                        if (*p == *r)
 349                                return count;
 350                }
 351                ++count;
 352        }
 353
 354        return count;
 355}       
 356
 357#ifndef __HAVE_ARCH_STRPBRK
 358/**
 359 * strpbrk - Find the first occurrence of a set of characters
 360 * @cs: The string to be searched
 361 * @ct: The characters to search for
 362 */
 363char * strpbrk(const char * cs,const char * ct)
 364{
 365        const char *sc1,*sc2;
 366
 367        for( sc1 = cs; *sc1 != '\0'; ++sc1) {
 368                for( sc2 = ct; *sc2 != '\0'; ++sc2) {
 369                        if (*sc1 == *sc2)
 370                                return (char *) sc1;
 371                }
 372        }
 373        return NULL;
 374}
 375#endif
 376
 377#ifndef __HAVE_ARCH_STRSEP
 378/**
 379 * strsep - Split a string into tokens
 380 * @s: The string to be searched
 381 * @ct: The characters to search for
 382 *
 383 * strsep() updates @s to point after the token, ready for the next call.
 384 *
 385 * It returns empty tokens, too, behaving exactly like the libc function
 386 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
 387 * Same semantics, slimmer shape. ;)
 388 */
 389char * strsep(char **s, const char *ct)
 390{
 391        char *sbegin = *s, *end;
 392
 393        if (sbegin == NULL)
 394                return NULL;
 395
 396        end = strpbrk(sbegin, ct);
 397        if (end)
 398                *end++ = '\0';
 399        *s = end;
 400
 401        return sbegin;
 402}
 403
 404EXPORT_SYMBOL(strsep);
 405#endif
 406
 407#ifndef __HAVE_ARCH_MEMSET
 408/**
 409 * memset - Fill a region of memory with the given value
 410 * @s: Pointer to the start of the area.
 411 * @c: The byte to fill the area with
 412 * @count: The size of the area.
 413 *
 414 * Do not use memset() to access IO space, use memset_io() instead.
 415 */
 416void * memset(void * s,int c,size_t count)
 417{
 418        char *xs = (char *) s;
 419
 420        while (count--)
 421                *xs++ = c;
 422
 423        return s;
 424}
 425#endif
 426
 427#ifndef __HAVE_ARCH_BCOPY
 428/**
 429 * bcopy - Copy one area of memory to another
 430 * @src: Where to copy from
 431 * @dest: Where to copy to
 432 * @count: The size of the area.
 433 *
 434 * Note that this is the same as memcpy(), with the arguments reversed.
 435 * memcpy() is the standard, bcopy() is a legacy BSD function.
 436 *
 437 * You should not use this function to access IO space, use memcpy_toio()
 438 * or memcpy_fromio() instead.
 439 */
 440void bcopy(const char * src, char * dest, int count)
 441{
 442        char *tmp = dest;
 443
 444        while (count--)
 445                *tmp++ = *src++;
 446}
 447#endif
 448
 449#ifndef __HAVE_ARCH_MEMCPY
 450/**
 451 * memcpy - Copy one area of memory to another
 452 * @dest: Where to copy to
 453 * @src: Where to copy from
 454 * @count: The size of the area.
 455 *
 456 * You should not use this function to access IO space, use memcpy_toio()
 457 * or memcpy_fromio() instead.
 458 */
 459void * memcpy(void * dest,const void *src,size_t count)
 460{
 461        char *tmp = (char *) dest, *s = (char *) src;
 462
 463        while (count--)
 464                *tmp++ = *s++;
 465
 466        return dest;
 467}
 468#endif
 469
 470#ifndef __HAVE_ARCH_MEMMOVE
 471/**
 472 * memmove - Copy one area of memory to another
 473 * @dest: Where to copy to
 474 * @src: Where to copy from
 475 * @count: The size of the area.
 476 *
 477 * Unlike memcpy(), memmove() copes with overlapping areas.
 478 */
 479void * memmove(void * dest,const void *src,size_t count)
 480{
 481        char *tmp, *s;
 482
 483        if (dest <= src) {
 484                tmp = (char *) dest;
 485                s = (char *) src;
 486                while (count--)
 487                        *tmp++ = *s++;
 488                }
 489        else {
 490                tmp = (char *) dest + count;
 491                s = (char *) src + count;
 492                while (count--)
 493                        *--tmp = *--s;
 494                }
 495
 496        return dest;
 497}
 498#endif
 499
 500#ifndef __HAVE_ARCH_MEMCMP
 501/**
 502 * memcmp - Compare two areas of memory
 503 * @cs: One area of memory
 504 * @ct: Another area of memory
 505 * @count: The size of the area.
 506 */
 507int memcmp(const void * cs,const void * ct,size_t count)
 508{
 509        const unsigned char *su1, *su2;
 510        int res = 0;
 511
 512        for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
 513                if ((res = *su1 - *su2) != 0)
 514                        break;
 515        return res;
 516}
 517#endif
 518
 519#ifndef __HAVE_ARCH_MEMSCAN
 520/**
 521 * memscan - Find a character in an area of memory.
 522 * @addr: The memory area
 523 * @c: The byte to search for
 524 * @size: The size of the area.
 525 *
 526 * returns the address of the first occurrence of @c, or 1 byte past
 527 * the area if @c is not found
 528 */
 529void * memscan(void * addr, int c, size_t size)
 530{
 531        unsigned char * p = (unsigned char *) addr;
 532
 533        while (size) {
 534                if (*p == c)
 535                        return (void *) p;
 536                p++;
 537                size--;
 538        }
 539        return (void *) p;
 540}
 541#endif
 542
 543#ifndef __HAVE_ARCH_STRSTR
 544/**
 545 * strstr - Find the first substring in a %NUL terminated string
 546 * @s1: The string to be searched
 547 * @s2: The string to search for
 548 */
 549char * strstr(const char * s1,const char * s2)
 550{
 551        int l1, l2;
 552
 553        l2 = strlen(s2);
 554        if (!l2)
 555                return (char *) s1;
 556        l1 = strlen(s1);
 557        while (l1 >= l2) {
 558                l1--;
 559                if (!memcmp(s1,s2,l2))
 560                        return (char *) s1;
 561                s1++;
 562        }
 563        return NULL;
 564}
 565#endif
 566
 567#ifndef __HAVE_ARCH_MEMCHR
 568/**
 569 * memchr - Find a character in an area of memory.
 570 * @s: The memory area
 571 * @c: The byte to search for
 572 * @n: The size of the area.
 573 *
 574 * returns the address of the first occurrence of @c, or %NULL
 575 * if @c is not found
 576 */
 577void *memchr(const void *s, int c, size_t n)
 578{
 579        const unsigned char *p = s;
 580        while (n-- != 0) {
 581                if ((unsigned char)c == *p++) {
 582                        return (void *)(p-1);
 583                }
 584        }
 585        return NULL;
 586}
 587
 588#endif
 589
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.