2008-03-20 00:56:58 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the libpayload project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
|
|
|
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
* Copyright (C) 2010 coresystems GmbH
|
2008-03-20 00:56:58 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libpayload.h>
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2011-07-26 12:51:59 +02:00
|
|
|
#include <inttypes.h>
|
2019-03-27 22:47:09 +01:00
|
|
|
#include <limits.h>
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
#include <errno.h>
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the length of a fixed-size string.
|
|
|
|
*
|
|
|
|
* @param str The input string.
|
|
|
|
* @param maxlen Return at most maxlen characters as length of the string.
|
|
|
|
* @return The length of the string, not including the final NUL character.
|
|
|
|
* The maximum length returned is maxlen.
|
|
|
|
*/
|
|
|
|
size_t strnlen(const char *str, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/* NULL and empty strings have length 0. */
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Loop until we find a NUL character, or maxlen is reached. */
|
|
|
|
while ((*str++ != '\0') && (len < maxlen))
|
|
|
|
len++;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the length of a string.
|
|
|
|
*
|
|
|
|
* @param str The input string.
|
|
|
|
* @return The length of the string, not including the final NUL character.
|
|
|
|
*/
|
|
|
|
size_t strlen(const char *str)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/* NULL and empty strings have length 0. */
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Loop until we find a NUL character. */
|
|
|
|
while (*str++ != '\0')
|
|
|
|
len++;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
/**
|
|
|
|
* Compare two strings.
|
|
|
|
*
|
|
|
|
* @param s1 The first string.
|
|
|
|
* @param s2 The second string.
|
|
|
|
* @return Returns a value less than zero, if s1 is shorter than s2. Returns
|
|
|
|
* zero, if s1 equals s2. Returns a value greater than zero, if
|
|
|
|
* s1 is longer than s2.
|
|
|
|
*/
|
|
|
|
int strcasecmp(const char *s1, const char *s2)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
int res;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; 1; i++) {
|
2010-09-08 12:27:13 +02:00
|
|
|
res = tolower(s1[i]) - tolower(s2[i]);
|
|
|
|
if (res || (s1[i] == '\0'))
|
|
|
|
break;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
}
|
|
|
|
|
2010-09-08 12:27:13 +02:00
|
|
|
return res;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two strings with fixed length.
|
|
|
|
*
|
|
|
|
* @param s1 The first string.
|
|
|
|
* @param s2 The second string.
|
|
|
|
* @param maxlen Return at most maxlen characters as length of the string.
|
|
|
|
* @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
|
|
|
|
*/
|
|
|
|
int strncasecmp(const char *s1, const char *s2, size_t maxlen)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
int res = 0;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i < maxlen; i++) {
|
2010-09-08 12:27:13 +02:00
|
|
|
res = tolower(s1[i]) - tolower(s2[i]);
|
|
|
|
if (res || (s1[i] == '\0'))
|
|
|
|
break;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
}
|
|
|
|
|
2010-09-08 12:27:13 +02:00
|
|
|
return res;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
}
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
/**
|
|
|
|
* Compare two strings.
|
|
|
|
*
|
|
|
|
* @param s1 The first string.
|
|
|
|
* @param s2 The second string.
|
|
|
|
* @return Returns a value less than zero, if s1 is shorter than s2. Returns
|
|
|
|
* zero, if s1 equals s2. Returns a value greater than zero, if
|
|
|
|
* s1 is longer than s2.
|
|
|
|
*/
|
|
|
|
int strcmp(const char *s1, const char *s2)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
int res;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; 1; i++) {
|
2010-09-08 12:27:13 +02:00
|
|
|
res = s1[i] - s2[i];
|
|
|
|
if (res || (s1[i] == '\0'))
|
|
|
|
break;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
2010-09-08 12:27:13 +02:00
|
|
|
return res;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two strings with fixed length.
|
|
|
|
*
|
|
|
|
* @param s1 The first string.
|
|
|
|
* @param s2 The second string.
|
|
|
|
* @param maxlen Return at most maxlen characters as length of the string.
|
|
|
|
* @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
|
|
|
|
*/
|
2008-08-26 23:51:04 +02:00
|
|
|
int strncmp(const char *s1, const char *s2, size_t maxlen)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
int res = 0;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i < maxlen; i++) {
|
2010-09-08 12:27:13 +02:00
|
|
|
res = s1[i] - s2[i];
|
|
|
|
if (res || (s1[i] == '\0'))
|
|
|
|
break;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
2010-09-08 12:27:13 +02:00
|
|
|
return res;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
2008-09-26 20:42:40 +02:00
|
|
|
/**
|
|
|
|
* Copy a string with a maximum length.
|
|
|
|
*
|
|
|
|
* @param d The destination memory.
|
|
|
|
* @param s The source string.
|
|
|
|
* @param n Copy at most n characters as length of the string.
|
|
|
|
* @return A pointer to the destination memory.
|
|
|
|
*/
|
2008-08-26 23:51:04 +02:00
|
|
|
char *strncpy(char *d, const char *s, size_t n)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
2008-03-21 16:47:38 +01:00
|
|
|
/* Use +1 to get the NUL terminator. */
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t max = n > strlen(s) + 1 ? strlen(s) + 1 : n;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i < max; i++)
|
2008-03-20 20:54:59 +01:00
|
|
|
d[i] = (char)s[i];
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2008-09-26 20:42:40 +02:00
|
|
|
/**
|
|
|
|
* Copy a string.
|
|
|
|
*
|
|
|
|
* @param d The destination memory.
|
|
|
|
* @param s The source string.
|
|
|
|
* @return A pointer to the destination memory.
|
|
|
|
*/
|
2008-03-20 02:13:28 +01:00
|
|
|
char *strcpy(char *d, const char *s)
|
|
|
|
{
|
2008-03-21 16:47:38 +01:00
|
|
|
return strncpy(d, s, strlen(s) + 1);
|
2008-03-20 02:13:28 +01:00
|
|
|
}
|
|
|
|
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
/**
|
|
|
|
* Concatenates two strings
|
|
|
|
*
|
|
|
|
* @param d The destination string.
|
|
|
|
* @param s The source string.
|
|
|
|
* @return A pointer to the destination string.
|
|
|
|
*/
|
|
|
|
char *strcat(char *d, const char *s)
|
|
|
|
{
|
|
|
|
char *p = d + strlen(d);
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t sl = strlen(s);
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i < sl; i++)
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
p[i] = s[i];
|
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
p[sl] = '\0';
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2008-09-26 20:42:40 +02:00
|
|
|
/**
|
|
|
|
* Concatenates two strings with a maximum length.
|
|
|
|
*
|
|
|
|
* @param d The destination string.
|
|
|
|
* @param s The source string.
|
2009-08-05 15:10:38 +02:00
|
|
|
* @param n Not more than n characters from s will be appended to d.
|
2008-09-26 20:42:40 +02:00
|
|
|
* @return A pointer to the destination string.
|
|
|
|
*/
|
2008-08-26 23:51:04 +02:00
|
|
|
char *strncat(char *d, const char *s, size_t n)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
|
|
|
char *p = d + strlen(d);
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t sl = strlen(s);
|
|
|
|
size_t max = n > sl ? sl : n;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i < max; i++)
|
2008-03-20 00:56:58 +01:00
|
|
|
p[i] = s[i];
|
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
p[max] = '\0';
|
2008-03-20 00:56:58 +01:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
/**
|
|
|
|
* Concatenates two strings with a maximum length.
|
|
|
|
*
|
|
|
|
* @param d The destination string.
|
|
|
|
* @param s The source string.
|
2011-03-22 14:40:09 +01:00
|
|
|
* @param n d will have at most n-1 characters (plus NUL) after invocation.
|
2019-06-25 19:59:59 +02:00
|
|
|
* @return The total length of the concatenated string.
|
2010-03-25 23:15:19 +01:00
|
|
|
*/
|
|
|
|
size_t strlcat(char *d, const char *s, size_t n)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t sl = strlen(s);
|
|
|
|
size_t dl = strlen(d);
|
|
|
|
|
|
|
|
if (n <= dl + 1)
|
|
|
|
return sl + dl;
|
2010-03-25 23:15:19 +01:00
|
|
|
|
|
|
|
char *p = d + dl;
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t max = n > (sl + dl) ? sl : (n - dl - 1);
|
2010-03-25 23:15:19 +01:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i < max; i++)
|
2010-03-25 23:15:19 +01:00
|
|
|
p[i] = s[i];
|
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
p[max] = '\0';
|
2019-06-25 19:59:59 +02:00
|
|
|
return sl + dl;
|
2010-03-25 23:15:19 +01:00
|
|
|
}
|
|
|
|
|
2008-09-26 20:42:40 +02:00
|
|
|
/**
|
|
|
|
* Find a character in a string.
|
|
|
|
*
|
|
|
|
* @param s The string.
|
|
|
|
* @param c The character.
|
2020-02-15 09:27:11 +01:00
|
|
|
* @return A pointer to the first occurrence of the character in the
|
2008-09-26 20:42:40 +02:00
|
|
|
* string, or NULL if the character was not encountered within the string.
|
|
|
|
*/
|
2008-03-20 20:54:59 +01:00
|
|
|
char *strchr(const char *s, int c)
|
2008-03-20 00:56:58 +01:00
|
|
|
{
|
2008-03-20 20:54:59 +01:00
|
|
|
char *p = (char *)s;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
for (; *p != 0; p++) {
|
2008-03-20 00:56:58 +01:00
|
|
|
if (*p == c)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-25 23:15:19 +01:00
|
|
|
/**
|
|
|
|
* Find a character in a string.
|
|
|
|
*
|
|
|
|
* @param s The string.
|
|
|
|
* @param c The character.
|
2020-02-15 09:27:11 +01:00
|
|
|
* @return A pointer to the last occurrence of the character in the
|
2010-03-25 23:15:19 +01:00
|
|
|
* string, or NULL if the character was not encountered within the string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *strrchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
char *p = (char *)s + strlen(s);
|
|
|
|
|
|
|
|
for (; p >= s; p--) {
|
|
|
|
if (*p == c)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-26 20:42:40 +02:00
|
|
|
/**
|
|
|
|
* Duplicate a string.
|
|
|
|
*
|
|
|
|
* @param s The string to duplicate.
|
|
|
|
* @return A pointer to the copy of the original string.
|
|
|
|
*/
|
2008-03-20 00:56:58 +01:00
|
|
|
char *strdup(const char *s)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t n = strlen(s);
|
2008-04-26 01:07:39 +02:00
|
|
|
char *p = malloc(n + 1);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2009-01-26 01:57:54 +01:00
|
|
|
if (p != NULL) {
|
2008-03-20 00:56:58 +01:00
|
|
|
strncpy(p, s, n);
|
2009-01-26 01:57:54 +01:00
|
|
|
p[n] = 0;
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-09-26 20:42:40 +02:00
|
|
|
/**
|
|
|
|
* Find a substring within a string.
|
|
|
|
*
|
|
|
|
* @param h The haystack string.
|
|
|
|
* @param n The needle string (substring).
|
2020-02-15 09:27:11 +01:00
|
|
|
* @return A pointer to the first occurrence of the substring in
|
2008-09-26 20:42:40 +02:00
|
|
|
* the string, or NULL if the substring was not encountered within the string.
|
|
|
|
*/
|
2008-03-20 00:56:58 +01:00
|
|
|
char *strstr(const char *h, const char *n)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t hn = strlen(h);
|
|
|
|
size_t nn = strlen(n);
|
|
|
|
|
|
|
|
if (hn < nn)
|
|
|
|
return NULL;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t i = 0; i <= hn - nn; i++)
|
2009-03-06 18:43:20 +01:00
|
|
|
if (!memcmp(&h[i], n, nn))
|
2008-03-20 20:54:59 +01:00
|
|
|
return (char *)&h[i];
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-26 20:42:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Separate strings.
|
|
|
|
*
|
|
|
|
* @param stringp reference of the string to separate.
|
|
|
|
* @param delim string containing all delimiters.
|
|
|
|
* @return Token string.
|
|
|
|
*/
|
|
|
|
char *strsep(char **stringp, const char *delim)
|
|
|
|
{
|
|
|
|
char *walk, *token;
|
|
|
|
|
|
|
|
if (!stringp || !*stringp || !**stringp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
token = walk = *stringp;
|
|
|
|
|
|
|
|
/* Walk, search for delimiters */
|
|
|
|
while(*walk && !strchr(delim, *walk))
|
|
|
|
walk++;
|
|
|
|
|
|
|
|
if (*walk) {
|
|
|
|
/* NUL terminate */
|
|
|
|
*walk = '\0';
|
|
|
|
walk++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*stringp = walk;
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2008-10-20 19:07:47 +02:00
|
|
|
/* Check that a character is in the valid range for the
|
|
|
|
given base
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int _valid(char ch, int base)
|
|
|
|
{
|
2013-06-25 19:25:46 +02:00
|
|
|
char end = (base > 9) ? '9' : '0' + (base - 1);
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
/* all bases will be some subset of the 0-9 range */
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
if (ch >= '0' && ch <= end)
|
|
|
|
return 1;
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
/* Bases > 11 will also have to match in the a-z range */
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
if (base > 11) {
|
|
|
|
if (tolower(ch) >= 'a' &&
|
|
|
|
tolower(ch) <= 'a' + (base - 11))
|
|
|
|
return 1;
|
|
|
|
}
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
return 0;
|
2008-10-20 19:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the "value" of the character in the given base */
|
|
|
|
|
|
|
|
static int _offset(char ch, int base)
|
|
|
|
{
|
2013-06-25 19:25:46 +02:00
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
return ch - '0';
|
|
|
|
else
|
|
|
|
return 10 + tolower(ch) - 'a';
|
2008-10-20 19:07:47 +02:00
|
|
|
}
|
|
|
|
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
/**
|
|
|
|
* Convert the initial portion of a string into a signed int
|
|
|
|
* @param ptr A pointer to the string to convert
|
|
|
|
* @param endptr A pointer to the unconverted part of the string
|
|
|
|
* @param base The base of the number to convert, or 0 for auto
|
|
|
|
* @return A signed integer representation of the string
|
|
|
|
*/
|
|
|
|
|
2019-03-27 22:47:09 +01:00
|
|
|
long long int strtoll(const char *orig_ptr, char **endptr, int base)
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
{
|
2019-03-27 22:47:09 +01:00
|
|
|
const char *ptr = orig_ptr;
|
|
|
|
int is_negative = 0;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
/* Purge whitespace */
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
for( ; *ptr && isspace(*ptr); ptr++);
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
|
|
|
if (ptr[0] == '-') {
|
2019-03-27 22:47:09 +01:00
|
|
|
is_negative = 1;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2019-03-27 22:47:09 +01:00
|
|
|
unsigned long long uval = strtoull(ptr, endptr, base);
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-03-27 22:47:09 +01:00
|
|
|
/* If the whole string is unparseable, endptr should point to start. */
|
|
|
|
if (endptr && *endptr == ptr)
|
|
|
|
*endptr = (char *)orig_ptr;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-03-27 22:47:09 +01:00
|
|
|
if (uval > (unsigned long long)LLONG_MAX + !!is_negative)
|
|
|
|
uval = (unsigned long long)LLONG_MAX + !!is_negative;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-03-27 22:47:09 +01:00
|
|
|
if (is_negative)
|
|
|
|
return -uval;
|
|
|
|
else
|
|
|
|
return uval;
|
|
|
|
}
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
2019-03-27 22:47:09 +01:00
|
|
|
long int strtol(const char *ptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
long long int val = strtoll(ptr, endptr, base);
|
|
|
|
if (val > LONG_MAX)
|
|
|
|
return LONG_MAX;
|
|
|
|
if (val < LONG_MIN)
|
|
|
|
return LONG_MIN;
|
|
|
|
return val;
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 15:31:52 +02:00
|
|
|
long atol(const char *nptr)
|
|
|
|
{
|
|
|
|
return strtol(nptr, NULL, 10);
|
|
|
|
}
|
|
|
|
|
2008-10-20 19:07:47 +02:00
|
|
|
/**
|
|
|
|
* Convert the initial portion of a string into an unsigned int
|
|
|
|
* @param ptr A pointer to the string to convert
|
|
|
|
* @param endptr A pointer to the unconverted part of the string
|
|
|
|
* @param base The base of the number to convert, or 0 for auto
|
|
|
|
* @return An unsigned integer representation of the string
|
|
|
|
*/
|
|
|
|
|
2011-07-26 12:51:59 +02:00
|
|
|
unsigned long long int strtoull(const char *ptr, char **endptr, int base)
|
2008-10-20 19:07:47 +02:00
|
|
|
{
|
2013-06-25 19:25:46 +02:00
|
|
|
unsigned long long int ret = 0;
|
2008-10-20 19:07:47 +02:00
|
|
|
|
|
|
|
if (endptr != NULL)
|
|
|
|
*endptr = (char *) ptr;
|
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
/* Purge whitespace */
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
for( ; *ptr && isspace(*ptr); ptr++);
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
if (!*ptr)
|
|
|
|
return 0;
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
/* Determine the base */
|
2008-10-20 19:07:47 +02:00
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
if (base == 0) {
|
2008-10-20 19:07:47 +02:00
|
|
|
if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
|
|
|
|
base = 16;
|
|
|
|
else if (ptr[0] == '0') {
|
|
|
|
base = 8;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
base = 10;
|
2013-06-25 19:25:46 +02:00
|
|
|
}
|
2008-10-20 19:07:47 +02:00
|
|
|
|
|
|
|
/* Base 16 allows the 0x on front - so skip over it */
|
|
|
|
|
|
|
|
if (base == 16) {
|
libpayload: strtoull: Fix edge case bug with *endptr
strtoull() can optionally take a second pointer as an out-parameter that
will be adjusted to point to the end of the parsed string. This works
almost right, but misses two important edge cases: firstly,when the
parsed string is "0", the function will interpret the leading '0' as an
octal prefix, so that the first actually parsed digit is already the
terminating '\0' byte. This will cause the function to early abort,
which still (correctly) returns 0 but doesn't adjust *endptr.
The early abort is pointless anyway -- the only other thing the function
does is run a for-loop whose condition is the exact inverse (so it's
guaranteed to run zero iterations in this case) and then adjust *endptr
(which we want). So just take it out. This also technically corrects the
behavior of *endptr for a completely invalid string, since the strtoull
man page says
> If there were no digits at all, strtoul() stores the original value of
> nptr in *endptr (and returns 0).
The second issue occurs when the parsed string is "0x" without another
valid digit behind it. In this case, we will still jump over the 0x
prefix so that *endptr is set to the first byte after that. The correct
interpretation in this case is that there is no 0x prefix, and instead a
valid 0 digit with the 'x' being invalid garbage at the end. By not
skipping the prefix unless there's at least one valid digit after it, we
get the correct behavior of *endptr pointing to the 'x'.
Change-Id: Idddd74e18e410a9d0b6dce9512ca0412b9e2333c
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/32029
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
2019-03-22 21:03:41 +01:00
|
|
|
if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X') &&
|
|
|
|
_valid(ptr[2], base))
|
2008-10-20 19:07:47 +02:00
|
|
|
ptr += 2;
|
|
|
|
}
|
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
for( ; *ptr && _valid(*ptr, base); ptr++)
|
|
|
|
ret = (ret * base) + _offset(*ptr, base);
|
2008-10-20 19:07:47 +02:00
|
|
|
|
|
|
|
if (endptr != NULL)
|
|
|
|
*endptr = (char *) ptr;
|
|
|
|
|
2013-06-25 19:25:46 +02:00
|
|
|
return ret;
|
2008-10-20 19:07:47 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 12:51:59 +02:00
|
|
|
unsigned long int strtoul(const char *ptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
unsigned long long val = strtoull(ptr, endptr, base);
|
2019-03-27 22:47:09 +01:00
|
|
|
if (val > ULONG_MAX) return ULONG_MAX;
|
2011-07-26 12:51:59 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
/**
|
|
|
|
* Determine the number of leading characters in s that match characters in a
|
|
|
|
* @param s A pointer to the string to analyse
|
|
|
|
* @param a A pointer to an array of characters that match the prefix
|
|
|
|
* @return The number of matching characters
|
|
|
|
*/
|
|
|
|
size_t strspn(const char *s, const char *a)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t i;
|
|
|
|
size_t al = strlen(a);
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
for (i = 0; s[i] != 0; i++) {
|
|
|
|
int found = 0;
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t j = 0; j < al; j++) {
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
if (s[i] == a[j]) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2008-10-20 19:07:47 +02:00
|
|
|
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
/**
|
|
|
|
* Determine the number of leading characters in s that do not match characters in a
|
|
|
|
* @param s A pointer to the string to analyse
|
|
|
|
* @param a A pointer to an array of characters that do not match the prefix
|
|
|
|
* @return The number of not matching characters
|
|
|
|
*/
|
|
|
|
size_t strcspn(const char *s, const char *a)
|
|
|
|
{
|
2019-06-26 00:57:07 +02:00
|
|
|
size_t i;
|
|
|
|
size_t al = strlen(a);
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
for (i = 0; s[i] != 0; i++) {
|
|
|
|
int found = 0;
|
2019-06-26 00:57:07 +02:00
|
|
|
for (size_t j = 0; j < al; j++) {
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
if (s[i] == a[j]) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract first token in string str that is delimited by a character in tokens.
|
|
|
|
* Destroys str and eliminates the token delimiter.
|
|
|
|
* @param str A pointer to the string to tokenize.
|
|
|
|
* @param delim A pointer to an array of characters that delimit the token
|
|
|
|
* @param ptr A pointer to a string pointer to keep state of the tokenizer
|
|
|
|
* @return Pointer to token
|
|
|
|
*/
|
|
|
|
char* strtok_r(char *str, const char *delim, char **ptr)
|
|
|
|
{
|
|
|
|
/* start new tokenizing job or continue existing one? */
|
|
|
|
if (str == NULL)
|
|
|
|
str = *ptr;
|
|
|
|
|
|
|
|
/* skip over prefix delimiters */
|
|
|
|
char *start = str + strspn(str, delim);
|
|
|
|
|
2016-09-07 14:09:34 +02:00
|
|
|
if (start[0] == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
/* find first delimiter character */
|
|
|
|
char *end = start + strcspn(start, delim);
|
2016-09-07 14:09:34 +02:00
|
|
|
*ptr = end;
|
|
|
|
if (end[0] != '\0')
|
|
|
|
*(*ptr)++ = '\0';
|
Attached patch moves functions out of the huge libpayload.h into headers
according to libc/posix traditions, to simplify porting applications to
payloads.
It also adds a couple of functions:
strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r,
strtok, perror, exit, getpagesize
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
2010-06-24 13:16:10 +02:00
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **strtok_global;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract first token in string str that is delimited by a character in tokens.
|
|
|
|
* Destroys str, eliminates the token delimiter and uses global state.
|
|
|
|
* @param str A pointer to the string to tokenize.
|
|
|
|
* @param delim A pointer to an array of characters that delimit the token
|
|
|
|
* @return Pointer to token
|
|
|
|
*/
|
|
|
|
char* strtok(char *str, const char *delim)
|
|
|
|
{
|
|
|
|
return strtok_r(str, delim, strtok_global);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print error message and error number
|
|
|
|
* @param s Error message to print
|
|
|
|
*/
|
|
|
|
void perror(const char *s)
|
|
|
|
{
|
|
|
|
printf("%s: %d\n", s?s:"(none)", errno);
|
|
|
|
}
|
2013-06-27 17:06:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a message string describing the given error number.
|
|
|
|
*
|
|
|
|
* @param errnum The error number to be interpreted
|
|
|
|
* @return A pointer to a string describing the given error number
|
|
|
|
*/
|
|
|
|
char *strerror(int errnum)
|
|
|
|
{
|
|
|
|
/* Reserve enough space for the string below + INT64_MIN in decimal + \0 */
|
|
|
|
static char errstr[35];
|
|
|
|
snprintf(errstr, sizeof(errstr), "Unknown error %d", errnum);
|
|
|
|
return errstr;
|
|
|
|
}
|
2014-11-22 00:50:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple routine to convert UTF-16 to ASCII, giving up with ? if too high.
|
|
|
|
* A single code point may convert to ?? if not in the BMP.
|
|
|
|
* @param utf16_string A string encoded in UTF-16LE
|
|
|
|
* @param maxlen Maximum possible length of the string in code points
|
|
|
|
* @return Newly allocated ASCII string
|
|
|
|
*/
|
2019-06-26 00:55:52 +02:00
|
|
|
char *utf16le_to_ascii(const uint16_t *utf16_string, size_t maxlen)
|
2014-11-22 00:50:27 +01:00
|
|
|
{
|
|
|
|
char *ascii_string = xmalloc(maxlen + 1); /* +1 for trailing \0 */
|
|
|
|
ascii_string[maxlen] = '\0';
|
2019-06-26 00:55:52 +02:00
|
|
|
for (size_t i = 0; i < maxlen; i++) {
|
2014-11-22 00:50:27 +01:00
|
|
|
uint16_t wchar = utf16_string[i];
|
|
|
|
ascii_string[i] = wchar > 0x7f ? '?' : (char)wchar;
|
|
|
|
}
|
|
|
|
return ascii_string;
|
|
|
|
}
|