Linux中的字符串库代码之一(Copyright (C) 1991, 1992 Linus Torvalds)

类别:编程语言 点击:0 评论:0 推荐:

/**
 * strnicmp - Case insensitive, length-limited string comparison
 * @s1: One string
 * @s2: The other string
 * @len: the maximum number of characters to compare
 */
int strnicmp(const char *s1, const char *s2, size_t len)
{
 /* Yes, Virginia, it had better be unsigned */
 unsigned char c1, c2;

 c1 = 0; c2 = 0;
 if (len) {
  do {
   c1 = *s1; c2 = *s2;
   s1++; s2++;
   if (!c1)
    break;
   if (!c2)
    break;
   if (c1 == c2)
    continue;
   c1 = tolower(c1);
   c2 = tolower(c2);
   if (c1 != c2)
    break;
  } while (--len);
 }
 return (int)c1 - (int)c2;
}

/**
 * strcpy - Copy a %NUL terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 */
char * strcpy(char * dest,const char *src)
{
 char *tmp = dest;

 while ((*dest++ = *src++) != '\0')
  /* nothing */;
 return tmp;
}

/**
 * strncpy - Copy a length-limited, %NUL-terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @count: The maximum number of bytes to copy
 *
 * The result is not %NUL-terminated if the source exceeds
 * @count bytes.
 */
char * strncpy(char * dest,const char *src,size_t count)
{
 char *tmp = dest;

 while (count) {
  if ((*tmp = *src) != 0) src++;
  tmp++;
  count--;
 }

/**
 * strcat - Append one %NUL-terminated string to another
 * @dest: The string to be appended to
 * @src: The string to append to it
 */
char * strcat(char * dest, const char * src)
{
 char *tmp = dest;

 while (*dest)
  dest++;
 while ((*dest++ = *src++) != '\0')
  ;

 return tmp;
}

/**
 * strncat - Append a length-limited, %NUL-terminated string to another
 * @dest: The string to be appended to
 * @src: The string to append to it
 * @count: The maximum numbers of bytes to copy
 *
 * Note that in contrast to strncpy, strncat ensures the result is
 * terminated.
 */
char * strncat(char *dest, const char *src, size_t count)
{
 char *tmp = dest;

 if (count) {
  while (*dest)
   dest++;
  while ((*dest++ = *src++) != 0) {
   if (--count == 0) {
    *dest = '\0';
    break;
   }
  }
 }

 return tmp;
}

/**
 * strlcat - Append a length-limited, %NUL-terminated string to another
 * @dest: The string to be appended to
 * @src: The string to append to it
 * @count: The size of the destination buffer.
 */
size_t strlcat(char *dest, const char *src, size_t count)
{
 size_t dsize = strlen(dest);
 size_t len = strlen(src);
 size_t res = dsize + len;

 /* This would be a bug */
 BUG_ON(dsize >= count);

 dest += dsize;
 count -= dsize;
 if (len >= count)
  len = count-1;
 memcpy(dest, src, len);
 dest[len] = 0;
 return res;
}
 return dest;
}

/**
 * strlcpy - Copy a %NUL terminated string into a sized buffer
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @size: size of destination buffer
 *
 * Compatible with *BSD: the result is always a valid
 * NUL-terminated string that fits in the buffer (unless,
 * of course, the buffer size is zero). It does not pad
 * out the result like strncpy() does.
 */
size_t strlcpy(char *dest, const char *src, size_t size)
{
 size_t ret = strlen(src);

 if (size) {
  size_t len = (ret >= size) ? size-1 : ret;
  memcpy(dest, src, len);
  dest[len] = '\0';
 }
 return ret;
}

本文地址:http://com.8s8s.com/it/it24532.htm