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
This commit is contained in:
parent
815c47f7b4
commit
980a69b8c2
@ -30,3 +30,8 @@
|
|||||||
|
|
||||||
unsigned long virtual_offset = 0;
|
unsigned long virtual_offset = 0;
|
||||||
|
|
||||||
|
|
||||||
|
int getpagesize(void)
|
||||||
|
{
|
||||||
|
return 4096;
|
||||||
|
}
|
||||||
|
@ -30,3 +30,7 @@
|
|||||||
|
|
||||||
unsigned long virtual_offset = 0;
|
unsigned long virtual_offset = 0;
|
||||||
|
|
||||||
|
int getpagesize(void)
|
||||||
|
{
|
||||||
|
return 4096;
|
||||||
|
}
|
||||||
|
55
payloads/libpayload/include/ctype.h
Normal file
55
payloads/libpayload/include/ctype.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CTYPE_H
|
||||||
|
#define _CTYPE_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup ctype Character type functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
int isalnum(int c);
|
||||||
|
int isalpha(int c);
|
||||||
|
int isascii(int c);
|
||||||
|
int isblank(int c);
|
||||||
|
int iscntrl(int c);
|
||||||
|
int isdigit(int c);
|
||||||
|
int isgraph(int c);
|
||||||
|
int islower(int c);
|
||||||
|
int isprint(int c);
|
||||||
|
int ispunct(int c);
|
||||||
|
int isspace(int c);
|
||||||
|
int isupper(int c);
|
||||||
|
int isxdigit(int c);
|
||||||
|
int tolower(int c);
|
||||||
|
int toupper(int c);
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
35
payloads/libpayload/include/errno.h
Normal file
35
payloads/libpayload/include/errno.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 coresystems GmbH
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ERRNO_H
|
||||||
|
#define _ERRNO_H
|
||||||
|
|
||||||
|
extern int errno;
|
||||||
|
|
||||||
|
#endif
|
@ -44,12 +44,16 @@
|
|||||||
#define _LIBPAYLOAD_H
|
#define _LIBPAYLOAD_H
|
||||||
|
|
||||||
#include <libpayload-config.h>
|
#include <libpayload-config.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <arch/types.h>
|
#include <arch/types.h>
|
||||||
#include <arch/io.h>
|
#include <arch/io.h>
|
||||||
#include <arch/virtual.h>
|
#include <arch/virtual.h>
|
||||||
#include <sysinfo.h>
|
#include <sysinfo.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <pci.h>
|
#include <pci.h>
|
||||||
#ifdef CONFIG_LAR
|
#ifdef CONFIG_LAR
|
||||||
#include <lar.h>
|
#include <lar.h>
|
||||||
@ -224,27 +228,6 @@ void console_add_input_driver(struct console_input_driver *in);
|
|||||||
#define havechar havekey
|
#define havechar havekey
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup ctype Character type functions
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
int isalnum(int c);
|
|
||||||
int isalpha(int c);
|
|
||||||
int isascii(int c);
|
|
||||||
int isblank(int c);
|
|
||||||
int iscntrl(int c);
|
|
||||||
int isdigit(int c);
|
|
||||||
int isgraph(int c);
|
|
||||||
int islower(int c);
|
|
||||||
int isprint(int c);
|
|
||||||
int ispunct(int c);
|
|
||||||
int isspace(int c);
|
|
||||||
int isupper(int c);
|
|
||||||
int isxdigit(int c);
|
|
||||||
int tolower(int c);
|
|
||||||
int toupper(int c);
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup ipchecksum IP checksum functions
|
* @defgroup ipchecksum IP checksum functions
|
||||||
* @{
|
* @{
|
||||||
@ -252,87 +235,6 @@ int toupper(int c);
|
|||||||
unsigned short ipchksum(const void *ptr, unsigned long nbytes);
|
unsigned short ipchksum(const void *ptr, unsigned long nbytes);
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup malloc Memory allocation functions
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
#if defined(CONFIG_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
|
|
||||||
#define free(p) \
|
|
||||||
({ \
|
|
||||||
extern void print_malloc_map(void); \
|
|
||||||
extern void free(void *); \
|
|
||||||
printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \
|
|
||||||
__LINE__);\
|
|
||||||
printf("PRE free()\n"); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
free(p); \
|
|
||||||
printf("POST free()\n"); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
})
|
|
||||||
#define malloc(s) \
|
|
||||||
({ \
|
|
||||||
extern void print_malloc_map(void); \
|
|
||||||
extern void *malloc(size_t); \
|
|
||||||
void *ptr; \
|
|
||||||
printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
|
|
||||||
__LINE__);\
|
|
||||||
printf("PRE malloc\n"); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr = malloc(s); \
|
|
||||||
printf("POST malloc (ptr = %p)\n", ptr); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr; \
|
|
||||||
})
|
|
||||||
#define calloc(n,s) \
|
|
||||||
({ \
|
|
||||||
extern void print_malloc_map(void); \
|
|
||||||
extern void *calloc(size_t,size_t); \
|
|
||||||
void *ptr; \
|
|
||||||
printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \
|
|
||||||
__func__, __LINE__);\
|
|
||||||
printf("PRE calloc\n"); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr = calloc(n,s); \
|
|
||||||
printf("POST calloc (ptr = %p)\n", ptr); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr; \
|
|
||||||
})
|
|
||||||
#define realloc(p,s) \
|
|
||||||
({ \
|
|
||||||
extern void print_malloc_map(void); \
|
|
||||||
extern void *realloc(void*,size_t); \
|
|
||||||
void *ptr; \
|
|
||||||
printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \
|
|
||||||
__func__, __LINE__);\
|
|
||||||
printf("PRE realloc\n"); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr = realloc(p,s); \
|
|
||||||
printf("POST realloc (ptr = %p)\n", ptr); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr; \
|
|
||||||
})
|
|
||||||
#define memalign(a,s) \
|
|
||||||
({ \
|
|
||||||
extern void print_malloc_map(void); \
|
|
||||||
extern void *memalign(size_t, size_t); \
|
|
||||||
void *ptr; \
|
|
||||||
printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \
|
|
||||||
__func__, __LINE__);\
|
|
||||||
printf("PRE memalign\n"); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr = memalign(a,s); \
|
|
||||||
printf("POST realloc (ptr = %p)\n", ptr); \
|
|
||||||
print_malloc_map(); \
|
|
||||||
ptr; \
|
|
||||||
})
|
|
||||||
#else
|
|
||||||
void free(void *ptr);
|
|
||||||
void *malloc(size_t size);
|
|
||||||
void *calloc(size_t nmemb, size_t size);
|
|
||||||
void *realloc(void *ptr, size_t size);
|
|
||||||
void *memalign(size_t align, size_t size);
|
|
||||||
#endif
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup exec Execution functions
|
* @defgroup exec Execution functions
|
||||||
@ -355,36 +257,6 @@ u8 hex2bin(u8 h);
|
|||||||
void fatal(const char *msg) __attribute__ ((noreturn));
|
void fatal(const char *msg) __attribute__ ((noreturn));
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup memory Memory manipulation functions
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
void *memset(void *s, int c, size_t n);
|
|
||||||
void *memcpy(void *dst, const void *src, size_t n);
|
|
||||||
void *memmove(void *dst, const void *src, size_t n);
|
|
||||||
int memcmp(const void *s1, const void *s2, size_t len);
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup printf Print functions
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
int snprintf(char *str, size_t size, const char *fmt, ...);
|
|
||||||
int sprintf(char *str, const char *fmt, ...);
|
|
||||||
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
|
|
||||||
int vsprintf(char *str, const char *fmt, va_list ap);
|
|
||||||
int printf(const char *fmt, ...);
|
|
||||||
int vprintf(const char *fmt, va_list ap);
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup rand Random number generator functions
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
int rand_r(unsigned int *seed);
|
|
||||||
int rand(void);
|
|
||||||
void srand(unsigned int seed);
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup hash Hashing functions
|
* @defgroup hash Hashing functions
|
||||||
@ -404,27 +276,6 @@ void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
|
|||||||
u8 *sha1(const u8 *data, size_t len, u8 *buf);
|
u8 *sha1(const u8 *data, size_t len, u8 *buf);
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup string String functions
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
size_t strnlen(const char *str, size_t maxlen);
|
|
||||||
size_t strlen(const char *str);
|
|
||||||
int strcmp(const char *s1, const char *s2);
|
|
||||||
int strncmp(const char *s1, const char *s2, size_t maxlen);
|
|
||||||
char *strncpy(char *d, const char *s, size_t n);
|
|
||||||
char *strcpy(char *d, const char *s);
|
|
||||||
char *strncat(char *d, const char *s, size_t n);
|
|
||||||
size_t strlcat(char *d, const char *s, size_t n);
|
|
||||||
char *strchr(const char *s, int c);
|
|
||||||
char *strrchr(const char *s, int c);
|
|
||||||
char *strdup(const char *s);
|
|
||||||
char *strstr(const char *h, const char *n);
|
|
||||||
char *strsep(char **stringp, const char *delim);
|
|
||||||
unsigned int strtoul(const char *s, char **nptr, int base);
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup time Time functions
|
* @defgroup time Time functions
|
||||||
* @{
|
* @{
|
||||||
@ -527,14 +378,6 @@ void udelay(unsigned int n);
|
|||||||
void mdelay(unsigned int n);
|
void mdelay(unsigned int n);
|
||||||
void delay(unsigned int n);
|
void delay(unsigned int n);
|
||||||
|
|
||||||
#define abort() halt() /**< Alias for the halt() function */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop execution and halt the processor (this function does not return).
|
|
||||||
*/
|
|
||||||
void halt(void) __attribute__ ((noreturn));
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup readline Readline functions
|
* @defgroup readline Readline functions
|
||||||
* This interface provides a simple implementation of the standard readline()
|
* This interface provides a simple implementation of the standard readline()
|
||||||
|
@ -36,8 +36,11 @@ typedef u32 pcidev_t;
|
|||||||
|
|
||||||
#define REG_VENDOR_ID 0x00
|
#define REG_VENDOR_ID 0x00
|
||||||
#define REG_COMMAND 0x04
|
#define REG_COMMAND 0x04
|
||||||
|
#define REG_CLASS_DEV 0x0A
|
||||||
#define REG_HEADER_TYPE 0x0E
|
#define REG_HEADER_TYPE 0x0E
|
||||||
#define REG_PRIMARY_BUS 0x18
|
#define REG_PRIMARY_BUS 0x18
|
||||||
|
#define REG_SUBSYS_VENDOR_ID 0x2C
|
||||||
|
#define REG_SUBSYS_ID 0x2E
|
||||||
|
|
||||||
#define REG_COMMAND_BM (1 << 2)
|
#define REG_COMMAND_BM (1 << 2)
|
||||||
|
|
||||||
|
44
payloads/libpayload/include/stdarg.h
Normal file
44
payloads/libpayload/include/stdarg.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBPAYLOAD_STDARG_H
|
||||||
|
#define _LIBPAYLOAD_STDARG_H
|
||||||
|
|
||||||
|
#include_next <stdarg.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup vprintf Varargs print functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
|
||||||
|
int vsprintf(char *str, const char *fmt, va_list ap);
|
||||||
|
int vprintf(const char *fmt, va_list ap);
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
#endif
|
30
payloads/libpayload/include/stdint.h
Normal file
30
payloads/libpayload/include/stdint.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 coresystems GmbH
|
||||||
|
*
|
||||||
|
* 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 <arch/types.h>
|
46
payloads/libpayload/include/stdio.h
Normal file
46
payloads/libpayload/include/stdio.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _STDIO_H
|
||||||
|
#define _STDIO_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup printf Print functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
int snprintf(char *str, size_t size, const char *fmt, ...);
|
||||||
|
int sprintf(char *str, const char *fmt, ...);
|
||||||
|
int printf(const char *fmt, ...);
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
void perror(const char *s);
|
||||||
|
|
||||||
|
#endif
|
144
payloads/libpayload/include/stdlib.h
Normal file
144
payloads/libpayload/include/stdlib.h
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _STDLIB_H
|
||||||
|
#define _STDLIB_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup malloc Memory allocation functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
#if defined(CONFIG_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
|
||||||
|
#define free(p) \
|
||||||
|
({ \
|
||||||
|
extern void print_malloc_map(void); \
|
||||||
|
extern void free(void *); \
|
||||||
|
printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \
|
||||||
|
__LINE__);\
|
||||||
|
printf("PRE free()\n"); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
free(p); \
|
||||||
|
printf("POST free()\n"); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
})
|
||||||
|
#define malloc(s) \
|
||||||
|
({ \
|
||||||
|
extern void print_malloc_map(void); \
|
||||||
|
extern void *malloc(size_t); \
|
||||||
|
void *ptr; \
|
||||||
|
printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
|
||||||
|
__LINE__);\
|
||||||
|
printf("PRE malloc\n"); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr = malloc(s); \
|
||||||
|
printf("POST malloc (ptr = %p)\n", ptr); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr; \
|
||||||
|
})
|
||||||
|
#define calloc(n,s) \
|
||||||
|
({ \
|
||||||
|
extern void print_malloc_map(void); \
|
||||||
|
extern void *calloc(size_t,size_t); \
|
||||||
|
void *ptr; \
|
||||||
|
printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \
|
||||||
|
__func__, __LINE__);\
|
||||||
|
printf("PRE calloc\n"); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr = calloc(n,s); \
|
||||||
|
printf("POST calloc (ptr = %p)\n", ptr); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr; \
|
||||||
|
})
|
||||||
|
#define realloc(p,s) \
|
||||||
|
({ \
|
||||||
|
extern void print_malloc_map(void); \
|
||||||
|
extern void *realloc(void*,size_t); \
|
||||||
|
void *ptr; \
|
||||||
|
printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \
|
||||||
|
__func__, __LINE__);\
|
||||||
|
printf("PRE realloc\n"); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr = realloc(p,s); \
|
||||||
|
printf("POST realloc (ptr = %p)\n", ptr); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr; \
|
||||||
|
})
|
||||||
|
#define memalign(a,s) \
|
||||||
|
({ \
|
||||||
|
extern void print_malloc_map(void); \
|
||||||
|
extern void *memalign(size_t, size_t); \
|
||||||
|
void *ptr; \
|
||||||
|
printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \
|
||||||
|
__func__, __LINE__);\
|
||||||
|
printf("PRE memalign\n"); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr = memalign(a,s); \
|
||||||
|
printf("POST realloc (ptr = %p)\n", ptr); \
|
||||||
|
print_malloc_map(); \
|
||||||
|
ptr; \
|
||||||
|
})
|
||||||
|
#else
|
||||||
|
void free(void *ptr);
|
||||||
|
void *malloc(size_t size);
|
||||||
|
void *calloc(size_t nmemb, size_t size);
|
||||||
|
void *realloc(void *ptr, size_t size);
|
||||||
|
void *memalign(size_t align, size_t size);
|
||||||
|
#endif
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup stdlib String conversion functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
long int strtol(const char *s, char **nptr, int base);
|
||||||
|
unsigned long int strtoul(const char *s, char **nptr, int base);
|
||||||
|
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup rand Random number generator functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
int rand_r(unsigned int *seed);
|
||||||
|
int rand(void);
|
||||||
|
void srand(unsigned int seed);
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop execution and halt the processor (this function does not return).
|
||||||
|
*/
|
||||||
|
void halt(void) __attribute__ ((noreturn));
|
||||||
|
void exit(int status) __attribute__ ((noreturn));
|
||||||
|
#define abort() halt() /**< Alias for the halt() function */
|
||||||
|
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
#endif
|
71
payloads/libpayload/include/string.h
Normal file
71
payloads/libpayload/include/string.h
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _STRING_H
|
||||||
|
#define _STRING_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup memory Memory manipulation functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
void *memset(void *s, int c, size_t n);
|
||||||
|
void *memcpy(void *dst, const void *src, size_t n);
|
||||||
|
void *memmove(void *dst, const void *src, size_t n);
|
||||||
|
int memcmp(const void *s1, const void *s2, size_t len);
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup string String functions
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
size_t strnlen(const char *str, size_t maxlen);
|
||||||
|
size_t strlen(const char *str);
|
||||||
|
int strcmp(const char *s1, const char *s2);
|
||||||
|
int strncmp(const char *s1, const char *s2, size_t maxlen);
|
||||||
|
int strcasecmp(const char *s1, const char *s2);
|
||||||
|
int strncasecmp(const char *s1, const char *s2, size_t maxlen);
|
||||||
|
char *strncpy(char *d, const char *s, size_t n);
|
||||||
|
char *strcpy(char *d, const char *s);
|
||||||
|
char *strncat(char *d, const char *s, size_t n);
|
||||||
|
size_t strlcat(char *d, const char *s, size_t n);
|
||||||
|
char *strcat(char *d, const char *s);
|
||||||
|
char *strchr(const char *s, int c);
|
||||||
|
char *strrchr(const char *s, int c);
|
||||||
|
char *strdup(const char *s);
|
||||||
|
char *strstr(const char *h, const char *n);
|
||||||
|
char *strsep(char **stringp, const char *delim);
|
||||||
|
size_t strspn(const char *s, const char *a);
|
||||||
|
size_t strcspn(const char *s, const char *a);
|
||||||
|
char* strtok(char *str, const char *delim);
|
||||||
|
char* strtok_r(char *str, const char *delim, char **ptr);
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
#endif
|
30
payloads/libpayload/include/sys/types.h
Normal file
30
payloads/libpayload/include/sys/types.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 coresystems GmbH
|
||||||
|
*
|
||||||
|
* 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 <arch/types.h>
|
30
payloads/libpayload/include/unistd.h
Normal file
30
payloads/libpayload/include/unistd.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libpayload project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 coresystems GmbH
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
int getpagesize(void);
|
@ -113,3 +113,10 @@ void fatal(const char *msg)
|
|||||||
halt();
|
halt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void exit(int status)
|
||||||
|
{
|
||||||
|
printf("exited with status %d\n", status);
|
||||||
|
halt();
|
||||||
|
}
|
||||||
|
|
||||||
|
int errno;
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <libpayload.h>
|
#include <libpayload.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
/** Structure for specifying output methods for different printf clones. */
|
/** Structure for specifying output methods for different printf clones. */
|
||||||
struct printf_spec {
|
struct printf_spec {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
|
* Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
|
||||||
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||||
|
* Copyright (C) 2010 coresystems GmbH
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@ -29,6 +30,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <libpayload.h>
|
#include <libpayload.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the length of a fixed-size string.
|
* Calculate the length of a fixed-size string.
|
||||||
@ -83,29 +87,57 @@ size_t strlen(const char *str)
|
|||||||
* zero, if s1 equals s2. Returns a value greater than zero, if
|
* zero, if s1 equals s2. Returns a value greater than zero, if
|
||||||
* s1 is longer than s2.
|
* s1 is longer than s2.
|
||||||
*/
|
*/
|
||||||
int strcmp(const char *s1, const char *s2)
|
int strcasecmp(const char *s1, const char *s2)
|
||||||
{
|
{
|
||||||
char c1, c2;
|
int i;
|
||||||
|
|
||||||
/* Set c1 == c2, so that we can enter the while loop. */
|
for (i = 0; 1; i++) {
|
||||||
c1 = 0;
|
if (tolower(s1[i]) != tolower(s2[i]))
|
||||||
c2 = 0;
|
return s1[i] - s2[i];
|
||||||
|
|
||||||
/* Compare characters until they differ, or one of the strings ends. */
|
|
||||||
while (c1 == c2) {
|
|
||||||
/* Read the next character from each string. */
|
|
||||||
c1 = *s1++;
|
|
||||||
c2 = *s2++;
|
|
||||||
|
|
||||||
/* Return something negative (if s1 is shorter than s2), or
|
|
||||||
zero (if s1 equals s2). */
|
|
||||||
if (c1 == '\0')
|
|
||||||
return c1 - c2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return something positive (if s1 is longer than s2), or zero (if s1
|
return 0;
|
||||||
and s2 are equal). */
|
}
|
||||||
return c1 - c2;
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < maxlen; i++) {
|
||||||
|
if (tolower(s1[i]) != tolower(s2[i]))
|
||||||
|
return s1[i] - s2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; 1; i++) {
|
||||||
|
if (s1[i] != s2[i])
|
||||||
|
return s1[i] - s2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,6 +192,26 @@ char *strcpy(char *d, const char *s)
|
|||||||
return strncpy(d, s, strlen(s) + 1);
|
return strncpy(d, s, strlen(s) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
int sl = strlen(s);
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < sl; i++)
|
||||||
|
p[i] = s[i];
|
||||||
|
|
||||||
|
p[i] = '\0';
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Concatenates two strings with a maximum length.
|
* Concatenates two strings with a maximum length.
|
||||||
*
|
*
|
||||||
@ -352,6 +404,69 @@ static int _offset(char ch, int base)
|
|||||||
return tolower(ch) - 'a';
|
return tolower(ch) - 'a';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
long int strtol(const char *ptr, char **endptr, int base)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
int negative = 1;
|
||||||
|
|
||||||
|
if (endptr != NULL)
|
||||||
|
*endptr = (char *) ptr;
|
||||||
|
|
||||||
|
/* Purge whitespace */
|
||||||
|
|
||||||
|
for( ; *ptr && isspace(*ptr); ptr++);
|
||||||
|
|
||||||
|
if (ptr[0] == '-') {
|
||||||
|
negative = -1;
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!*ptr)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Determine the base */
|
||||||
|
|
||||||
|
if (base == 0) {
|
||||||
|
if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
|
||||||
|
base = 16;
|
||||||
|
else if (ptr[0] == '0') {
|
||||||
|
base = 8;
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
base = 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Base 16 allows the 0x on front - so skip over it */
|
||||||
|
|
||||||
|
if (base == 16) {
|
||||||
|
if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
|
||||||
|
ptr += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If the first character isn't valid, then don't
|
||||||
|
* bother */
|
||||||
|
|
||||||
|
if (!*ptr || !_valid(*ptr, base))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for( ; *ptr && _valid(*ptr, base); ptr++)
|
||||||
|
ret = (ret * base) + _offset(*ptr, base);
|
||||||
|
|
||||||
|
if (endptr != NULL)
|
||||||
|
*endptr = (char *) ptr;
|
||||||
|
|
||||||
|
return ret * negative;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert the initial portion of a string into an unsigned int
|
* Convert the initial portion of a string into an unsigned int
|
||||||
* @param ptr A pointer to the string to convert
|
* @param ptr A pointer to the string to convert
|
||||||
@ -360,7 +475,7 @@ static int _offset(char ch, int base)
|
|||||||
* @return An unsigned integer representation of the string
|
* @return An unsigned integer representation of the string
|
||||||
*/
|
*/
|
||||||
|
|
||||||
unsigned int strtoul(const char *ptr, char **endptr, int base)
|
unsigned long int strtoul(const char *ptr, char **endptr, int base)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
@ -409,4 +524,102 @@ unsigned int strtoul(const char *ptr, char **endptr, int base)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
int al = strlen(a);
|
||||||
|
for (i = 0; s[i] != 0; i++) {
|
||||||
|
int found = 0;
|
||||||
|
for (j = 0; j < al; j++) {
|
||||||
|
if (s[i] == a[j]) {
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
int al = strlen(a);
|
||||||
|
for (i = 0; s[i] != 0; i++) {
|
||||||
|
int found = 0;
|
||||||
|
for (j = 0; j < al; j++) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
/* find first delimiter character */
|
||||||
|
char *end = start + strcspn(start, delim);
|
||||||
|
end[0] = '\0';
|
||||||
|
|
||||||
|
*ptr = end+1;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user