Subversion Repositories Kolibri OS

Rev

Rev 6934 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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/kernel.h>
  26. #include <linux/export.h>
  27. #include <linux/bug.h>
  28. #include <linux/errno.h>
  29.  
  30.  
  31. #ifndef __HAVE_ARCH_STRNCPY
  32. /**
  33.  * strncpy - Copy a length-limited, C-string
  34.  * @dest: Where to copy the string to
  35.  * @src: Where to copy the string from
  36.  * @count: The maximum number of bytes to copy
  37.  *
  38.  * The result is not %NUL-terminated if the source exceeds
  39.  * @count bytes.
  40.  *
  41.  * In the case where the length of @src is less than  that  of
  42.  * count, the remainder of @dest will be padded with %NUL.
  43.  *
  44.  */
  45. char *strncpy(char *dest, const char *src, size_t count)
  46. {
  47.         char *tmp = dest;
  48.  
  49.         while (count) {
  50.                 if ((*tmp = *src) != 0)
  51.                         src++;
  52.                 tmp++;
  53.                 count--;
  54.         }
  55.         return dest;
  56. }
  57. EXPORT_SYMBOL(strncpy);
  58. #endif
  59.  
  60. #ifndef __HAVE_ARCH_STRLCPY
  61. /**
  62.  * strlcpy - Copy a C-string into a sized buffer
  63.  * @dest: Where to copy the string to
  64.  * @src: Where to copy the string from
  65.  * @size: size of destination buffer
  66.  *
  67.  * Compatible with *BSD: the result is always a valid
  68.  * NUL-terminated string that fits in the buffer (unless,
  69.  * of course, the buffer size is zero). It does not pad
  70.  * out the result like strncpy() does.
  71.  */
  72. size_t strlcpy(char *dest, const char *src, size_t size)
  73. {
  74.     size_t ret = strlen(src);
  75.  
  76.     if (size) {
  77.         size_t len = (ret >= size) ? size - 1 : ret;
  78.         memcpy(dest, src, len);
  79.         dest[len] = '\0';
  80.     }
  81.     return ret;
  82. }
  83.  
  84. #ifndef __HAVE_ARCH_STRLCAT
  85. /**
  86.  * strlcat - Append a length-limited, C-string to another
  87.  * @dest: The string to be appended to
  88.  * @src: The string to append to it
  89.  * @count: The size of the destination buffer.
  90.  */
  91. size_t strlcat(char *dest, const char *src, size_t count)
  92. {
  93.         size_t dsize = strlen(dest);
  94.         size_t len = strlen(src);
  95.         size_t res = dsize + len;
  96.  
  97.         /* This would be a bug */
  98.         BUG_ON(dsize >= count);
  99.  
  100.         dest += dsize;
  101.         count -= dsize;
  102.         if (len >= count)
  103.                 len = count-1;
  104.         memcpy(dest, src, len);
  105.         dest[len] = 0;
  106.         return res;
  107. }
  108. #endif
  109.  
  110.  
  111. #endif
  112.  
  113.