mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-21 10:05:00 +08:00
1c1d046be6
strcmp() is always expected to return 0 when arguments are equal, negative when its first argument @str1 is less than its second argument @str2 and a positive value otherwise. Previously strcmp("a", "b") returned 1. Now it gives -1, as it is supposed to. Until now this bug never triggered, because all uses for strcmp() in the boot code tested for nonzero: triton:~/tip> git grep strcmp arch/x86/boot/ arch/x86/boot/boot.h:int strcmp(const char *str1, const char *str2); arch/x86/boot/edd.c: if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) { arch/x86/boot/edd.c: else if (!strcmp(eddarg, "off")) arch/x86/boot/edd.c: else if (!strcmp(eddarg, "on")) should in the future strcmp() be used in a comparative way in the boot code, it might have led to (not so subtle) bugs. Signed-off-by: Arjun Sreedharan <arjun024@gmail.com> Signed-off-by: Borislav Petkov <bp@suse.de> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1426520267-1803-1-git-send-email-arjun024@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
158 lines
2.9 KiB
C
158 lines
2.9 KiB
C
/* -*- linux-c -*- ------------------------------------------------------- *
|
|
*
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
* Copyright 2007 rPath, Inc. - All Rights Reserved
|
|
*
|
|
* This file is part of the Linux kernel, and is made available under
|
|
* the terms of the GNU General Public License version 2.
|
|
*
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
/*
|
|
* Very basic string functions
|
|
*/
|
|
|
|
#include <linux/types.h>
|
|
#include "ctype.h"
|
|
|
|
int memcmp(const void *s1, const void *s2, size_t len)
|
|
{
|
|
u8 diff;
|
|
asm("repe; cmpsb; setnz %0"
|
|
: "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
|
|
return diff;
|
|
}
|
|
|
|
int strcmp(const char *str1, const char *str2)
|
|
{
|
|
const unsigned char *s1 = (const unsigned char *)str1;
|
|
const unsigned char *s2 = (const unsigned char *)str2;
|
|
int delta = 0;
|
|
|
|
while (*s1 || *s2) {
|
|
delta = *s1 - *s2;
|
|
if (delta)
|
|
return delta;
|
|
s1++;
|
|
s2++;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int strncmp(const char *cs, const char *ct, size_t count)
|
|
{
|
|
unsigned char c1, c2;
|
|
|
|
while (count) {
|
|
c1 = *cs++;
|
|
c2 = *ct++;
|
|
if (c1 != c2)
|
|
return c1 < c2 ? -1 : 1;
|
|
if (!c1)
|
|
break;
|
|
count--;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
size_t strnlen(const char *s, size_t maxlen)
|
|
{
|
|
const char *es = s;
|
|
while (*es && maxlen) {
|
|
es++;
|
|
maxlen--;
|
|
}
|
|
|
|
return (es - s);
|
|
}
|
|
|
|
unsigned int atou(const char *s)
|
|
{
|
|
unsigned int i = 0;
|
|
while (isdigit(*s))
|
|
i = i * 10 + (*s++ - '0');
|
|
return i;
|
|
}
|
|
|
|
/* Works only for digits and letters, but small and fast */
|
|
#define TOLOWER(x) ((x) | 0x20)
|
|
|
|
static unsigned int simple_guess_base(const char *cp)
|
|
{
|
|
if (cp[0] == '0') {
|
|
if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
|
|
return 16;
|
|
else
|
|
return 8;
|
|
} else {
|
|
return 10;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* simple_strtoull - convert a string to an unsigned long long
|
|
* @cp: The start of the string
|
|
* @endp: A pointer to the end of the parsed string will be placed here
|
|
* @base: The number base to use
|
|
*/
|
|
|
|
unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
|
|
{
|
|
unsigned long long result = 0;
|
|
|
|
if (!base)
|
|
base = simple_guess_base(cp);
|
|
|
|
if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
|
|
cp += 2;
|
|
|
|
while (isxdigit(*cp)) {
|
|
unsigned int value;
|
|
|
|
value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
|
|
if (value >= base)
|
|
break;
|
|
result = result * base + value;
|
|
cp++;
|
|
}
|
|
if (endp)
|
|
*endp = (char *)cp;
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* strlen - Find the length of a string
|
|
* @s: The string to be sized
|
|
*/
|
|
size_t strlen(const char *s)
|
|
{
|
|
const char *sc;
|
|
|
|
for (sc = s; *sc != '\0'; ++sc)
|
|
/* nothing */;
|
|
return sc - s;
|
|
}
|
|
|
|
/**
|
|
* strstr - Find the first substring in a %NUL terminated string
|
|
* @s1: The string to be searched
|
|
* @s2: The string to search for
|
|
*/
|
|
char *strstr(const char *s1, const char *s2)
|
|
{
|
|
size_t l1, l2;
|
|
|
|
l2 = strlen(s2);
|
|
if (!l2)
|
|
return (char *)s1;
|
|
l1 = strlen(s1);
|
|
while (l1 >= l2) {
|
|
l1--;
|
|
if (!memcmp(s1, s2, l2))
|
|
return (char *)s1;
|
|
s1++;
|
|
}
|
|
return NULL;
|
|
}
|