mirror of
https://github.com/Fishwaldo/bl_mcu_sdk.git
synced 2025-07-12 07:48:42 +00:00
[refactor][components] Restructure the catalogue, sync from internal repo
This commit is contained in:
parent
681e8c744a
commit
89592fc9a3
3357 changed files with 616226 additions and 159127 deletions
714
components/libc/nuttx/compiler.h
Normal file
714
components/libc/nuttx/compiler.h
Normal file
|
@ -0,0 +1,714 @@
|
|||
/****************************************************************************
|
||||
* include/nuttx/compiler.h
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __INCLUDE_NUTTX_COMPILER_H
|
||||
#define __INCLUDE_NUTTX_COMPILER_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* GCC-specific definitions *************************************************/
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
||||
/* Pre-processor */
|
||||
|
||||
# define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
|
||||
# define CONFIG_CPP_HAVE_WARNING 1 /* Supports #warning */
|
||||
|
||||
/* Intriniscs. GCC supports __func__ but provides __FUNCTION__ for backward
|
||||
* compatibility with older versions of GCC.
|
||||
*/
|
||||
|
||||
# define CONFIG_HAVE_FUNCTIONNAME 1 /* Has __FUNCTION__ */
|
||||
# define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
|
||||
|
||||
/* Indicate that a local variable is not used */
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
/* Built-in functions */
|
||||
|
||||
/* GCC 4.x have __builtin_ctz(|l|ll) and __builtin_clz(|l|ll). These count
|
||||
* trailing/leading zeros of input number and typically will generate few
|
||||
* fast bit-counting instructions. Inputting zero to these functions is
|
||||
* undefined and needs to be taken care of by the caller.
|
||||
*/
|
||||
|
||||
#if __GNUC__ >= 4
|
||||
# define CONFIG_HAVE_BUILTIN_CTZ 1
|
||||
# define CONFIG_HAVE_BUILTIN_CLZ 1
|
||||
# define CONFIG_HAVE_BUILTIN_POPCOUNT 1
|
||||
#endif
|
||||
|
||||
/* C++ support */
|
||||
|
||||
#if defined(__cplusplus) && __cplusplus >= 201402L
|
||||
# define CONFIG_HAVE_CXX14 1
|
||||
#else
|
||||
# undef CONFIG_HAVE_CXX14
|
||||
#endif
|
||||
|
||||
/* Attributes
|
||||
*
|
||||
* GCC supports weak symbols which can be used to reduce code size because
|
||||
* unnecessary "weak" functions can be excluded from the link.
|
||||
*/
|
||||
|
||||
# if !defined(__CYGWIN__) && !defined(CONFIG_ARCH_GNU_NO_WEAKFUNCTIONS)
|
||||
# define CONFIG_HAVE_WEAKFUNCTIONS 1
|
||||
# define weak_alias(name, aliasname) \
|
||||
extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
|
||||
# define weak_data __attribute__ ((weak))
|
||||
# define weak_function __attribute__ ((weak))
|
||||
# define weak_const_function __attribute__ ((weak, __const__))
|
||||
# else
|
||||
# undef CONFIG_HAVE_WEAKFUNCTIONS
|
||||
# define weak_alias(name, aliasname)
|
||||
# define weak_data
|
||||
# define weak_function
|
||||
# define weak_const_function
|
||||
# endif
|
||||
|
||||
/* The noreturn attribute informs GCC that the function will not return.
|
||||
* C11 adds _Noreturn keyword (see stdnoreturn.h)
|
||||
*/
|
||||
|
||||
# define noreturn_function __attribute__ ((noreturn))
|
||||
|
||||
/* The farcall_function attribute informs GCC that is should use long calls
|
||||
* (even though -mlong-calls does not appear in the compilation options)
|
||||
*/
|
||||
|
||||
# define farcall_function __attribute__ ((long_call))
|
||||
|
||||
/* Code locate */
|
||||
|
||||
# define locate_code(n) __attribute__ ((section(n)))
|
||||
|
||||
/* Data alignment */
|
||||
|
||||
# define aligned_data(n) __attribute__ ((aligned(n)))
|
||||
|
||||
/* Data location */
|
||||
|
||||
# define locate_data(n) __attribute__ ((section(n)))
|
||||
|
||||
/* The packed attribute informs GCC that the structure elements are packed,
|
||||
* ignoring other alignment rules.
|
||||
*/
|
||||
|
||||
# define begin_packed_struct
|
||||
# define end_packed_struct __attribute__ ((packed))
|
||||
|
||||
/* GCC does not support the reentrant attribute */
|
||||
|
||||
# define reentrant_function
|
||||
|
||||
/* The naked attribute informs GCC that the programmer will take care of
|
||||
* the function prolog and epilog.
|
||||
*/
|
||||
|
||||
# define naked_function __attribute__ ((naked,no_instrument_function))
|
||||
|
||||
/* The inline_function attribute informs GCC that the function should always
|
||||
* be inlined, regardless of the level of optimization. The
|
||||
* noinline_function indicates that the function should never be inlined.
|
||||
*/
|
||||
|
||||
# define inline_function __attribute__ ((always_inline,no_instrument_function))
|
||||
# define noinline_function __attribute__ ((noinline))
|
||||
|
||||
/* Some versions of GCC have a separate __syslog__ format.
|
||||
* http://mail-index.netbsd.org/source-changes/2015/10/14/msg069435.html
|
||||
* Use it if available. Otherwise, assume __printf__ accepts %m.
|
||||
*/
|
||||
|
||||
# if !defined(__syslog_attribute__)
|
||||
# define __syslog__ __printf__
|
||||
# endif
|
||||
|
||||
# define printflike(a, b) __attribute__((__format__ (__printf__, a, b)))
|
||||
# define sysloglike(a, b) __attribute__((__format__ (__syslog__, a, b)))
|
||||
# define scanflike(a, b) __attribute__((__format__ (__scanf__, a, b)))
|
||||
# define strftimelike(a) __attribute__((__format__ (__strftime__, a, 0)))
|
||||
|
||||
/* GCC does not use storage classes to qualify addressing */
|
||||
|
||||
# define FAR
|
||||
# define NEAR
|
||||
# define DSEG
|
||||
# define CODE
|
||||
|
||||
/* Handle cases where sizeof(int) is 16-bits, sizeof(long) is 32-bits, and
|
||||
* pointers are 16-bits.
|
||||
*/
|
||||
|
||||
#if defined(__m32c__)
|
||||
/* No I-space access qualifiers */
|
||||
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
|
||||
/* Select the small, 16-bit addressing model */
|
||||
|
||||
# define CONFIG_SMALL_MEMORY 1
|
||||
|
||||
/* Long and int are not the same size */
|
||||
|
||||
# define CONFIG_LONG_IS_NOT_INT 1
|
||||
|
||||
/* Pointers and int are the same size */
|
||||
|
||||
# undef CONFIG_PTR_IS_NOT_INT
|
||||
|
||||
#elif defined(__AVR__)
|
||||
# if defined(CONFIG_AVR_HAS_MEMX_PTR)
|
||||
/* I-space access qualifiers needed by Harvard architecture */
|
||||
|
||||
# define IOBJ __flash
|
||||
# define IPTR __memx
|
||||
|
||||
# else
|
||||
/* No I-space access qualifiers */
|
||||
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
# endif
|
||||
|
||||
/* Select the small, 16-bit addressing model (for D-Space) */
|
||||
|
||||
# define CONFIG_SMALL_MEMORY 1
|
||||
|
||||
/* Long and int are not the same size */
|
||||
|
||||
# define CONFIG_LONG_IS_NOT_INT 1
|
||||
|
||||
/* Pointers and int are the same size */
|
||||
|
||||
# undef CONFIG_PTR_IS_NOT_INT
|
||||
|
||||
/* Uses a 32-bit FAR pointer only from accessing data outside of the 16-bit
|
||||
* data space.
|
||||
*/
|
||||
|
||||
# define CONFIG_HAVE_FARPOINTER 1
|
||||
|
||||
#elif defined(__mc68hc1x__)
|
||||
|
||||
/* No I-space access qualifiers */
|
||||
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
|
||||
/* Select the small, 16-bit addressing model */
|
||||
|
||||
# define CONFIG_SMALL_MEMORY 1
|
||||
|
||||
/* Normally, mc68hc1x code is compiled with the -mshort option
|
||||
* which results in a 16-bit integer. If -mnoshort is defined
|
||||
* then an integer is 32-bits. GCC will defined __INT__ accordingly:
|
||||
*/
|
||||
|
||||
# if __INT__ == 16
|
||||
/* int is 16-bits, long is 32-bits */
|
||||
|
||||
# define CONFIG_LONG_IS_NOT_INT 1
|
||||
|
||||
/* Pointers and int are the same size (16-bits) */
|
||||
|
||||
# undef CONFIG_PTR_IS_NOT_INT
|
||||
# else
|
||||
/* int and long are both 32-bits */
|
||||
|
||||
# undef CONFIG_LONG_IS_NOT_INT
|
||||
|
||||
/* Pointers and int are NOT the same size */
|
||||
|
||||
# define CONFIG_PTR_IS_NOT_INT 1
|
||||
# endif
|
||||
|
||||
#else
|
||||
|
||||
/* No I-space access qualifiers */
|
||||
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
|
||||
/* Select the large, 32-bit addressing model */
|
||||
|
||||
# undef CONFIG_SMALL_MEMORY
|
||||
|
||||
/* Long and int are (probably) the same size (32-bits) */
|
||||
|
||||
# undef CONFIG_LONG_IS_NOT_INT
|
||||
|
||||
/* Pointers and int are the same size (32-bits) */
|
||||
|
||||
# undef CONFIG_PTR_IS_NOT_INT
|
||||
#endif
|
||||
|
||||
/* ISO C11 supports anonymous (unnamed) structures and unions, added in
|
||||
* GCC 4.6 (but might be suppressed with -std= option). ISO C++11 also
|
||||
* adds un-named unions, but NOT unnamed structures (although compilers
|
||||
* may support them).
|
||||
*
|
||||
* CAREFUL: This can cause issues for shared data structures shared between
|
||||
* C and C++ if the two versions do not support the same features.
|
||||
* Structures and unions can lose binary compatibility!
|
||||
*
|
||||
* NOTE: The NuttX coding standard forbids the use of unnamed structures and
|
||||
* unions within the OS.
|
||||
*/
|
||||
|
||||
# undef CONFIG_HAVE_ANONYMOUS_STRUCT
|
||||
# undef CONFIG_HAVE_ANONYMOUS_UNION
|
||||
|
||||
# if (defined(__cplusplus) && __cplusplus >= 201103L) || \
|
||||
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
|
||||
# define CONFIG_HAVE_ANONYMOUS_STRUCT 1
|
||||
# define CONFIG_HAVE_ANONYMOUS_UNION 1
|
||||
# endif
|
||||
|
||||
// # define CONFIG_HAVE_LONG_LONG 1
|
||||
// # define CONFIG_HAVE_FLOAT 1
|
||||
// # define CONFIG_HAVE_DOUBLE 1
|
||||
// # define CONFIG_HAVE_LONG_DOUBLE 1
|
||||
|
||||
/* Indicate that a local variable is not used */
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
/* SDCC-specific definitions ************************************************/
|
||||
|
||||
#elif defined(SDCC) || defined(__SDCC)
|
||||
|
||||
/* No I-space access qualifiers */
|
||||
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
|
||||
/* Pre-processor */
|
||||
|
||||
# define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
|
||||
# define CONFIG_CPP_HAVE_WARNING 1 /* Supports #warning */
|
||||
|
||||
/* Intriniscs */
|
||||
|
||||
# define CONFIG_HAVE_FUNCTIONNAME 1 /* Has __FUNCTION__ */
|
||||
# define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
|
||||
# define __FUNCTION__ __func__ /* SDCC supports on __func__ */
|
||||
|
||||
/* Pragmas
|
||||
*
|
||||
* Disable warnings for unused function arguments
|
||||
*/
|
||||
|
||||
# pragma disable_warning 85
|
||||
|
||||
/* C++ support */
|
||||
|
||||
# undef CONFIG_HAVE_CXX14
|
||||
|
||||
/* Attributes
|
||||
*
|
||||
* SDCC does not support weak symbols
|
||||
*/
|
||||
|
||||
# undef CONFIG_HAVE_WEAKFUNCTIONS
|
||||
# define weak_alias(name, aliasname)
|
||||
# define weak_data
|
||||
# define weak_function
|
||||
# define weak_const_function
|
||||
# define restrict /* REVISIT */
|
||||
|
||||
/* SDCC does not support the noreturn or packed attributes */
|
||||
|
||||
/* Current SDCC supports noreturn via C11 _Noreturn keyword (see
|
||||
* stdnoreturn.h).
|
||||
*/
|
||||
|
||||
# define noreturn_function
|
||||
# define locate_code(n)
|
||||
# define aligned_data(n)
|
||||
# define locate_data(n)
|
||||
# define begin_packed_struct
|
||||
# define end_packed_struct
|
||||
|
||||
/* REVISIT: */
|
||||
|
||||
# define farcall_function
|
||||
|
||||
/* SDCC does support "naked" functions */
|
||||
|
||||
# define naked_function __naked
|
||||
|
||||
/* SDCC does not support forced inlining. */
|
||||
|
||||
# define inline_function
|
||||
# define noinline_function
|
||||
|
||||
# define printflike(a, b)
|
||||
# define sysloglike(a, b)
|
||||
# define scanflike(a, b)
|
||||
# define strftimelike(a)
|
||||
|
||||
/* The reentrant attribute informs SDCC that the function
|
||||
* must be reentrant. In this case, SDCC will store input
|
||||
* arguments on the stack to support reentrancy.
|
||||
*
|
||||
* SDCC functions are always reentrant (except for the mcs51,
|
||||
* ds390, hc08 and s08 backends)
|
||||
*/
|
||||
|
||||
# define reentrant_function __reentrant
|
||||
|
||||
/* ISO C11 supports anonymous (unnamed) structures and unions. Does SDCC? */
|
||||
|
||||
# undef CONFIG_HAVE_ANONYMOUS_STRUCT
|
||||
# undef CONFIG_HAVE_ANONYMOUS_UNION
|
||||
|
||||
/* Indicate that a local variable is not used */
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
/* It is assumed that the system is build using the small
|
||||
* data model with storage defaulting to internal RAM.
|
||||
* The NEAR storage class can also be used to address data
|
||||
* in internal RAM; FAR can be used to address data in
|
||||
* external RAM.
|
||||
*/
|
||||
|
||||
#if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_gbz80)
|
||||
# define FAR
|
||||
# define NEAR
|
||||
# define CODE
|
||||
# define DSEG
|
||||
#else
|
||||
# define FAR __xdata
|
||||
# define NEAR __data
|
||||
# define CODE __code
|
||||
# if defined(SDCC_MODEL_SMALL)
|
||||
# define DSEG __data
|
||||
# else
|
||||
# define DSEG __xdata
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* Select small, 16-bit address model */
|
||||
|
||||
# define CONFIG_SMALL_MEMORY 1
|
||||
|
||||
/* Long and int are not the same size */
|
||||
|
||||
# define CONFIG_LONG_IS_NOT_INT 1
|
||||
|
||||
/* The generic pointer and int are not the same size (for some SDCC
|
||||
* architectures). REVISIT: SDCC now has more backends where pointers are
|
||||
* the same size as int than just z80 and z180.
|
||||
*/
|
||||
|
||||
#if !defined(__z80) && !defined(__gbz80)
|
||||
# define CONFIG_PTR_IS_NOT_INT 1
|
||||
#endif
|
||||
|
||||
/* SDCC does types long long and float, but not types double and long
|
||||
* double.
|
||||
*/
|
||||
|
||||
# define CONFIG_HAVE_LONG_LONG 1
|
||||
# define CONFIG_HAVE_FLOAT 1
|
||||
# undef CONFIG_HAVE_DOUBLE
|
||||
# undef CONFIG_HAVE_LONG_DOUBLE
|
||||
|
||||
/* Indicate that a local variable is not used */
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
/* Zilog-specific definitions ***********************************************/
|
||||
|
||||
#elif defined(__ZILOG__)
|
||||
|
||||
/* At present, only the following Zilog compilers are recognized */
|
||||
|
||||
# if !defined(__ZNEO__) && !defined(__EZ8__) && !defined(__EZ80__)
|
||||
# warning "Unrecognized Zilog compiler"
|
||||
# endif
|
||||
|
||||
/* Pre-processor */
|
||||
|
||||
# undef CONFIG_CPP_HAVE_VARARGS /* No variable argument macros */
|
||||
# undef CONFIG_CPP_HAVE_WARNING /* Does not support #warning */
|
||||
|
||||
/* Intrinsics */
|
||||
|
||||
# define CONFIG_HAVE_FUNCTIONNAME 1 /* Has __FUNCTION__ */
|
||||
# define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
|
||||
|
||||
/* No I-space access qualifiers */
|
||||
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
|
||||
/* C++ support */
|
||||
|
||||
# undef CONFIG_HAVE_CXX14
|
||||
|
||||
/* Attributes
|
||||
*
|
||||
* The Zilog compiler does not support weak symbols
|
||||
*/
|
||||
|
||||
# undef CONFIG_HAVE_WEAKFUNCTIONS
|
||||
# define weak_alias(name, aliasname)
|
||||
# define weak_data
|
||||
# define weak_function
|
||||
# define weak_const_function
|
||||
# define restrict
|
||||
|
||||
/* The Zilog compiler does not support the noreturn, packed, naked
|
||||
* attributes.
|
||||
*/
|
||||
|
||||
# define noreturn_function
|
||||
# define aligned_data(n)
|
||||
# define locate_code(n)
|
||||
# define locate_data(n)
|
||||
# define begin_packed_struct
|
||||
# define end_packed_struct
|
||||
# define naked_function
|
||||
# define inline_function
|
||||
# define noinline_function
|
||||
# define printflike(a, b)
|
||||
# define sysloglike(a, b)
|
||||
# define scanflike(a, b)
|
||||
# define strftimelike(a)
|
||||
|
||||
/* REVISIT: */
|
||||
|
||||
# define farcall_function
|
||||
|
||||
/* The Zilog compiler does not support the reentrant attribute */
|
||||
|
||||
# define reentrant_function
|
||||
|
||||
/* Addressing.
|
||||
*
|
||||
* Z16F ZNEO: Far is 24-bits; near is 16-bits of address.
|
||||
* The supported model is (1) all code on ROM, and (2) all data
|
||||
* and stacks in external (far) RAM.
|
||||
* Z8Encore!: Far is 16-bits; near is 8-bits of address.
|
||||
* The supported model is (1) all code on ROM, and (2) all data
|
||||
* and stacks in internal (far) RAM.
|
||||
* Z8Acclaim: In Z80 mode, all pointers are 16-bits. In ADL mode, all
|
||||
* pointers are 24 bits.
|
||||
*/
|
||||
|
||||
# if defined(__ZNEO__)
|
||||
# define FAR _Far
|
||||
# define NEAR _Near
|
||||
# define DSEG _Far
|
||||
# define CODE _Erom
|
||||
# undef CONFIG_SMALL_MEMORY /* Select the large, 32-bit addressing model */
|
||||
# undef CONFIG_LONG_IS_NOT_INT /* Long and int are the same size */
|
||||
# undef CONFIG_PTR_IS_NOT_INT /* FAR pointers and int are the same size */
|
||||
# elif defined(__EZ8__)
|
||||
# define FAR far
|
||||
# define NEAR near
|
||||
# define DSEG far
|
||||
# define CODE rom
|
||||
# define CONFIG_SMALL_MEMORY 1 /* Select small, 16-bit address model */
|
||||
# define CONFIG_LONG_IS_NOT_INT 1 /* Long and int are not the same size */
|
||||
# undef CONFIG_PTR_IS_NOT_INT /* FAR pointers and int are the same size */
|
||||
# elif defined(__EZ80__)
|
||||
# define FAR
|
||||
# define NEAR
|
||||
# define DSEG
|
||||
# define CODE
|
||||
# undef CONFIG_SMALL_MEMORY /* Select the large, 32-bit addressing model */
|
||||
# define CONFIG_LONG_IS_NOT_INT 1 /* Long and int are not the same size */
|
||||
# ifdef CONFIG_EZ80_Z80MODE
|
||||
# define CONFIG_PTR_IS_NOT_INT 1 /* Pointers and int are not the same size */
|
||||
# else
|
||||
# undef CONFIG_PTR_IS_NOT_INT /* Pointers and int are the same size */
|
||||
# endif
|
||||
# endif
|
||||
|
||||
/* ISO C11 supports anonymous (unnamed) structures and unions. Zilog does
|
||||
* not support C11
|
||||
*/
|
||||
|
||||
# undef CONFIG_HAVE_ANONYMOUS_STRUCT
|
||||
# undef CONFIG_HAVE_ANONYMOUS_UNION
|
||||
|
||||
/* Older Zilog compilers support both types double and long long, but the
|
||||
* size is 32-bits (same as long and single precision) so it is safer to say
|
||||
* that they are not supported. Later versions are more ANSII compliant and
|
||||
* simply do not support long long or double.
|
||||
*/
|
||||
|
||||
# undef CONFIG_HAVE_LONG_LONG
|
||||
# define CONFIG_HAVE_FLOAT 1
|
||||
# undef CONFIG_HAVE_DOUBLE
|
||||
# undef CONFIG_HAVE_LONG_DOUBLE
|
||||
|
||||
/* Indicate that a local variable is not used */
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
/* ICCARM-specific definitions **********************************************/
|
||||
|
||||
#elif defined(__ICCARM__)
|
||||
|
||||
# define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
|
||||
# define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
|
||||
# define CONFIG_HAVE_FLOAT 1
|
||||
|
||||
/* Indicate that a local variable is not used */
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
# define weak_alias(name, aliasname)
|
||||
# define weak_data __weak
|
||||
# define weak_function __weak
|
||||
# define weak_const_function
|
||||
# define noreturn_function
|
||||
# define farcall_function
|
||||
# define locate_code(n)
|
||||
# define aligned_data(n)
|
||||
# define locate_data(n)
|
||||
# define begin_packed_struct __packed
|
||||
# define end_packed_struct
|
||||
# define reentrant_function
|
||||
# define naked_function
|
||||
# define inline_function
|
||||
# define noinline_function
|
||||
# define printflike(a, b)
|
||||
# define sysloglike(a, b)
|
||||
# define scanflike(a, b)
|
||||
# define strftimelike(a)
|
||||
|
||||
# define FAR
|
||||
# define NEAR
|
||||
# define DSEG
|
||||
# define CODE
|
||||
# define IOBJ
|
||||
# define IPTR
|
||||
|
||||
# define __asm__ asm
|
||||
# define __volatile__ volatile
|
||||
|
||||
/* For operatots __sfb() and __sfe() */
|
||||
|
||||
# pragma section = ".bss"
|
||||
# pragma section = ".data"
|
||||
# pragma section = ".data_init"
|
||||
# pragma section = ".text"
|
||||
|
||||
/* C++ support */
|
||||
|
||||
# undef CONFIG_HAVE_CXX14
|
||||
|
||||
/* ISO C11 supports anonymous (unnamed) structures and unions. Does
|
||||
* ICCARM?
|
||||
*/
|
||||
|
||||
# undef CONFIG_HAVE_ANONYMOUS_STRUCT
|
||||
# undef CONFIG_HAVE_ANONYMOUS_UNION
|
||||
|
||||
/* Unknown compiler *********************************************************/
|
||||
|
||||
#else
|
||||
|
||||
# undef CONFIG_CPP_HAVE_VARARGS
|
||||
# undef CONFIG_CPP_HAVE_WARNING
|
||||
# undef CONFIG_HAVE_FUNCTIONNAME
|
||||
# undef CONFIG_HAVE_FILENAME
|
||||
# undef CONFIG_HAVE_WEAKFUNCTIONS
|
||||
# undef CONFIG_HAVE_CXX14
|
||||
# define weak_alias(name, aliasname)
|
||||
# define weak_data
|
||||
# define weak_function
|
||||
# define weak_const_function
|
||||
# define restrict
|
||||
# define noreturn_function
|
||||
# define farcall_function
|
||||
# define aligned_data(n)
|
||||
# define locate_code(n)
|
||||
# define locate_data(n)
|
||||
# define begin_packed_struct
|
||||
# define end_packed_struct
|
||||
# define reentrant_function
|
||||
# define naked_function
|
||||
# define inline_function
|
||||
# define noinline_function
|
||||
# define printflike(a, b)
|
||||
# define sysloglike(a, b)
|
||||
# define scanflike(a, b)
|
||||
# define strftimelike(a)
|
||||
|
||||
# define FAR
|
||||
# define NEAR
|
||||
# define DSEG
|
||||
# define CODE
|
||||
|
||||
# undef CONFIG_SMALL_MEMORY
|
||||
# undef CONFIG_LONG_IS_NOT_INT
|
||||
# undef CONFIG_PTR_IS_NOT_INT
|
||||
# undef CONFIG_HAVE_LONG_LONG
|
||||
# define CONFIG_HAVE_FLOAT 1
|
||||
# undef CONFIG_HAVE_DOUBLE
|
||||
# undef CONFIG_HAVE_LONG_DOUBLE
|
||||
# undef CONFIG_HAVE_ANONYMOUS_STRUCT
|
||||
# undef CONFIG_HAVE_ANONYMOUS_UNION
|
||||
|
||||
# define UNUSED(a) ((void)(1 || (a)))
|
||||
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __INCLUDE_NUTTX_COMPILER_H */
|
42
components/libc/nuttx/config.h
Normal file
42
components/libc/nuttx/config.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef __INCLUDE_NUTTX_CONFIG_H
|
||||
#define __INCLUDE_NUTTX_CONFIG_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define FAR
|
||||
#define CODE
|
||||
#define set_errno(e)
|
||||
|
||||
/* Defined in lib_skipspace.c */
|
||||
|
||||
void lib_skipspace(FAR const char **pptr);
|
||||
|
||||
/* Defined in lib_isbasedigit.c */
|
||||
|
||||
bool lib_isbasedigit(int ch, int base, FAR int *value);
|
||||
|
||||
/* Defined in lib_checkbase.c */
|
||||
|
||||
int lib_checkbase(int base, FAR const char **pptr);
|
||||
|
||||
#if defined(CPU_D0)
|
||||
#define CONFIG_HAVE_LONG_LONG
|
||||
#else
|
||||
#define CONFIG_ARCH_RV32IM
|
||||
#endif
|
||||
|
||||
// # define CONFIG_HAVE_FLOAT 1
|
||||
// # define CONFIG_HAVE_DOUBLE 1
|
||||
// # define CONFIG_HAVE_LONG_DOUBLE 1
|
||||
|
||||
#define lib_malloc(s) malloc(s)
|
||||
#define lib_zalloc(s) zalloc(s)
|
||||
#define lib_realloc(p, s) realloc(p, s)
|
||||
#define lib_memalign(p, s) memalign(p, s)
|
||||
#define lib_free(p) free(p)
|
||||
|
||||
#include "nuttx_limits.h"
|
||||
|
||||
#endif
|
42
components/libc/nuttx/libc/stdlib/Kconfig
Normal file
42
components/libc/nuttx/libc/stdlib/Kconfig
Normal file
|
@ -0,0 +1,42 @@
|
|||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see the file kconfig-language.txt in the NuttX tools repository.
|
||||
#
|
||||
|
||||
menu "stdlib Options"
|
||||
|
||||
config LIB_RAND_ORDER
|
||||
int "Order of the random number generate"
|
||||
default 1
|
||||
range 1 3
|
||||
---help---
|
||||
The order of the random number generator. 1=fast but very bad random
|
||||
numbers, 3=slow but very good random numbers.
|
||||
|
||||
config LIB_HOMEDIR
|
||||
string "Home directory"
|
||||
default "/"
|
||||
depends on !DISABLE_ENVIRON
|
||||
---help---
|
||||
The home directory to use with operations like such as 'cd ~'
|
||||
|
||||
config LIBC_TMPDIR
|
||||
string "Temporary file directory"
|
||||
default "/tmp"
|
||||
---help---
|
||||
If a write-able file system is selected, this string will be
|
||||
provided to specify the full path to a directory where temporary
|
||||
files can be created. This would be a good application of RAM disk:
|
||||
To provide temporary storage for application data.
|
||||
|
||||
config LIBC_MAX_TMPFILE
|
||||
int "Maximum size of a temporary file path"
|
||||
default 32
|
||||
---help---
|
||||
If a write-able file system is selected, then temporary file may be
|
||||
supported at the path provided by LIBC_TMPDIR. The tmpnam() interface
|
||||
keeps a static copy of this last filename produced; this value is the
|
||||
maximum size of that last filename. This size is the size of the full
|
||||
file path.
|
||||
|
||||
endmenu # stdlib Options
|
46
components/libc/nuttx/libc/stdlib/Make.defs
Normal file
46
components/libc/nuttx/libc/stdlib/Make.defs
Normal file
|
@ -0,0 +1,46 @@
|
|||
############################################################################
|
||||
# libs/libc/stdlib/Make.defs
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership. The
|
||||
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance with the
|
||||
# License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
# Add the stdlib C files to the build
|
||||
|
||||
CSRCS += lib_abs.c lib_abort.c lib_atof.c lib_atoi.c
|
||||
CSRCS += lib_atol.c lib_atoll.c lib_div.c lib_ldiv.c lib_lldiv.c lib_Exit.c
|
||||
CSRCS += lib_itoa.c lib_labs.c lib_llabs.c lib_realpath.c lib_bsearch.c
|
||||
CSRCS += lib_rand.c lib_qsort.c lib_srand.c lib_strtol.c
|
||||
CSRCS += lib_strtoll.c lib_strtoul.c lib_strtoull.c lib_strtod.c lib_strtof.c
|
||||
CSRCS += lib_strtold.c lib_checkbase.c lib_mktemp.c lib_mkstemp.c lib_mkdtemp.c
|
||||
|
||||
ifeq ($(CONFIG_LIBC_WCHAR),y)
|
||||
CSRCS += lib_mblen.c lib_mbtowc.c lib_wctomb.c
|
||||
CSRCS += lib_mbstowcs.c lib_wcstombs.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_PSEUDOTERM_SUSV1),y)
|
||||
CSRCS += lib_ptsname.c lib_ptsnamer.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_PSEUDOTERM),y)
|
||||
CSRCS += lib_unlockpt.c
|
||||
endif
|
||||
|
||||
# Add the stdlib directory to the build
|
||||
|
||||
DEPPATH += --dep-path stdlib
|
||||
VPATH += :stdlib
|
35
components/libc/nuttx/libc/stdlib/lib_Exit.c
Normal file
35
components/libc/nuttx/libc/stdlib/lib_Exit.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_Exit.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void _Exit(int status)
|
||||
{
|
||||
_exit(status);
|
||||
}
|
83
components/libc/nuttx/libc/stdlib/lib_abort.c
Normal file
83
components/libc/nuttx/libc/stdlib/lib_abort.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_abort.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: Abort
|
||||
*
|
||||
* Description:
|
||||
* The abort() first unblocks the SIGABRT signal, and then raises that
|
||||
* signal for the calling process. This results in the abnormal
|
||||
* termination of the process unless the SIGABRT signal is caught and
|
||||
* the signal handler does not return.
|
||||
*
|
||||
* If the abort() function causes process termination, all open
|
||||
* streams are closed and flushed.
|
||||
*
|
||||
* If the SIGABRT signal is ignored, or caught by a handler that
|
||||
* returns, the abort() function will still terminate the process.
|
||||
* It does this by restoring the default disposition for SIGABRT and
|
||||
* then raising the signal for a second time.
|
||||
*
|
||||
* Input Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* This function does not return,
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void abort(void)
|
||||
{
|
||||
/* NuttX does not support standard signal functionality (like the
|
||||
* behavior of the SIGABRT signal). So no attempt is made to provide
|
||||
* a conformant version of abort() at this time. This version does not
|
||||
* signal the calling thread all.
|
||||
*
|
||||
* Note that pthread_exit() is called instead of exit(). That is because
|
||||
* we do no know if abort was called from a pthread or a normal thread
|
||||
* (we could find out, of course). If abort() is called from a
|
||||
* non-pthread, then pthread_exit() should fail and fall back to call
|
||||
* exit() anyway.
|
||||
*
|
||||
* If exit() is called (either below or via pthread_exit()), then exit()
|
||||
* will flush and close all open files and terminate the thread. If this
|
||||
* function was called from a pthread, then pthread_exit() will complete
|
||||
* any joins, but will not flush or close any streams.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_DISABLE_PTHREAD
|
||||
exit(EXIT_FAILURE);
|
||||
#else
|
||||
pthread_exit(NULL);
|
||||
#endif
|
||||
}
|
40
components/libc/nuttx/libc/stdlib/lib_abs.c
Normal file
40
components/libc/nuttx/libc/stdlib/lib_abs.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_abs.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int abs(int j)
|
||||
{
|
||||
if (j < 0)
|
||||
{
|
||||
j = -j;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
36
components/libc/nuttx/libc/stdlib/lib_atof.c
Normal file
36
components/libc/nuttx/libc/stdlib/lib_atof.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_atof.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_HAVE_DOUBLE
|
||||
double atof(FAR const char *nptr)
|
||||
{
|
||||
return strtod(nptr, NULL);
|
||||
}
|
||||
#endif
|
35
components/libc/nuttx/libc/stdlib/lib_atoi.c
Normal file
35
components/libc/nuttx/libc/stdlib/lib_atoi.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_atoi.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int atoi(FAR const char *nptr)
|
||||
{
|
||||
return strtol(nptr, NULL, 10);
|
||||
}
|
36
components/libc/nuttx/libc/stdlib/lib_atol.c
Normal file
36
components/libc/nuttx/libc/stdlib/lib_atol.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_atol.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
long atol(FAR const char *nptr)
|
||||
{
|
||||
return strtol(nptr, NULL, 10);
|
||||
}
|
36
components/libc/nuttx/libc/stdlib/lib_atoll.c
Normal file
36
components/libc/nuttx/libc/stdlib/lib_atoll.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_atoll.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_LONG
|
||||
long long atoll(FAR const char *nptr)
|
||||
{
|
||||
return strtoll(nptr, NULL, 10);
|
||||
}
|
||||
#endif
|
141
components/libc/nuttx/libc/stdlib/lib_bsearch.c
Normal file
141
components/libc/nuttx/libc/stdlib/lib_bsearch.c
Normal file
|
@ -0,0 +1,141 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_bsearch.c
|
||||
*
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: bsearch
|
||||
*
|
||||
* Description:
|
||||
* The bsearch() function will search an array of nel objects, the initial
|
||||
* element of which is pointed to by 'base', for an element that matches
|
||||
* the object pointed to by 'key'. The size of each element in the array
|
||||
* is specified by 'width'. If the nel argument has the value zero, the
|
||||
* comparison function pointed to by 'compar' will not be called and no
|
||||
* match will be found.
|
||||
*
|
||||
* The comparison function pointed to by 'compar' will be called with two
|
||||
* arguments that point to the 'key' object and to an array element, in
|
||||
* that order.
|
||||
*
|
||||
* The application will ensure that the comparison function pointed to by
|
||||
* 'compar 'does not alter the contents of the array. The implementation
|
||||
* may reorder elements of the array between calls to the comparison
|
||||
* function, but will not alter the contents of any individual element.
|
||||
*
|
||||
* The implementation will ensure that the first argument is always a
|
||||
* pointer to the 'key'.
|
||||
*
|
||||
* When the same objects (consisting of width bytes, irrespective of their
|
||||
* current positions in the array) are passed more than once to the
|
||||
* comparison function, the results will be consistent with one another.
|
||||
* That is, the same object will always compare the same way with the key.
|
||||
*
|
||||
* The application will ensure that the function returns an integer less
|
||||
* than, equal to, or greater than 0 if the key object is considered,
|
||||
* respectively, to be less than, to match, or to be greater than the
|
||||
* array element. The application will ensure that the array consists of
|
||||
* all the elements that compare less than, all the elements that compare
|
||||
* equal to, and all the elements that compare greater than the key
|
||||
* object, in that order.
|
||||
*
|
||||
* (Based on description from OpenGroup.org).
|
||||
*
|
||||
* Returned Value:
|
||||
* The bsearch() function will return a pointer to a matching member of
|
||||
* the array, or a null pointer if no match is found. If two or more
|
||||
* members compare equal, which member is returned is unspecified.
|
||||
*
|
||||
* Notes from the NetBSD version:
|
||||
* The code below is a bit sneaky. After a comparison fails, we divide
|
||||
* the work in half by moving either left or right. If 'lim' is odd,
|
||||
* moving left simply involves halving 'lim': e.g., when 'lim' is 5 we
|
||||
* look at item 2, so we change 'lim' to 2 so that we will look at items
|
||||
* 0 & 1. If 'lim' is even, the same applies. If 'lim' is odd, moving
|
||||
* right again involes halving 'lim', this time moving the base up one
|
||||
* item past 'middle': e.g., when 'lim' is 5 we change base to item 3 and
|
||||
* make 'lim' 2 so that we will look at items 3 and 4. If 'lim' is
|
||||
* even, however, we have to shrink it by one before halving: e.g.,
|
||||
* when 'lim' is 4, we still looked at item 2, so we have to make 'lim'
|
||||
* 3, then halve, obtaining 1, so that we will only look at item 3.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *bsearch(FAR const void *key, FAR const void *base, size_t nel,
|
||||
size_t width, CODE int (*compar)(FAR const void *,
|
||||
FAR const void *))
|
||||
{
|
||||
FAR const void *middle; /* Current entry being tested */
|
||||
FAR const char *lower; /* The lower limit of the search region */
|
||||
size_t lim; /* The number of elements in the region */
|
||||
int cmp; /* Boolean comparison result */
|
||||
|
||||
// DEBUGASSERT(key != NULL);
|
||||
// DEBUGASSERT(base != NULL || nel == 0);
|
||||
// DEBUGASSERT(compar != NULL);
|
||||
|
||||
for (lim = nel, lower = (const char *)base; lim != 0; lim >>= 1)
|
||||
{
|
||||
middle = lower + (lim >> 1) * width;
|
||||
cmp = (*compar)(key, middle);
|
||||
|
||||
if (cmp == 0)
|
||||
{
|
||||
return (FAR void *)middle;
|
||||
}
|
||||
|
||||
if (cmp > 0)
|
||||
{
|
||||
/* key > middle: move right (else move left) */
|
||||
|
||||
lower = (FAR const char *)middle + width;
|
||||
lim--;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
109
components/libc/nuttx/libc/stdlib/lib_checkbase.c
Normal file
109
components/libc/nuttx/libc/stdlib/lib_checkbase.c
Normal file
|
@ -0,0 +1,109 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_checkbase.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_checkbase
|
||||
*
|
||||
* Description:
|
||||
* This is part of the strol() family implementation. This function checks
|
||||
* the initial part of a string to see if it can determine the numeric
|
||||
* base that is represented.
|
||||
*
|
||||
* Assumptions:
|
||||
* *ptr points to the first, non-whitespace character in the string.
|
||||
*
|
||||
* Returned Value:
|
||||
* - if base is valid, the actual base to use, and pptr is updated to point
|
||||
* at the first digit.
|
||||
* - if base is invalid (<2 or >36), return -1.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lib_checkbase(int base, FAR const char **pptr)
|
||||
{
|
||||
FAR const char *ptr = *pptr;
|
||||
|
||||
/* Check for unspecified base */
|
||||
|
||||
if (!base)
|
||||
{
|
||||
/* Assume base 10 */
|
||||
|
||||
base = 10;
|
||||
|
||||
/* Check for leading '0' - that would signify octal
|
||||
* or hex (or binary)
|
||||
*/
|
||||
|
||||
if (*ptr == '0')
|
||||
{
|
||||
/* Assume octal */
|
||||
|
||||
base = 8;
|
||||
ptr++;
|
||||
|
||||
/* Check for hexadecimal */
|
||||
|
||||
if ((*ptr == 'X' || *ptr == 'x') &&
|
||||
lib_isbasedigit(ptr[1], 16, NULL))
|
||||
{
|
||||
base = 16;
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If it a hexadecimal representation,
|
||||
* than discard any leading "0X" or "0x"
|
||||
*/
|
||||
|
||||
else if (base == 16)
|
||||
{
|
||||
if (ptr[0] == '0' && (ptr[1] == 'X' || ptr[1] == 'x'))
|
||||
{
|
||||
ptr += 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for incorrect bases. */
|
||||
|
||||
else if (base < 2 || base > 26)
|
||||
{
|
||||
return -1; /* Means incorrect base */
|
||||
}
|
||||
|
||||
/* Return the updated pointer and base */
|
||||
|
||||
*pptr = ptr;
|
||||
return base;
|
||||
}
|
76
components/libc/nuttx/libc/stdlib/lib_div.c
Normal file
76
components/libc/nuttx/libc/stdlib/lib_div.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_div.c
|
||||
*
|
||||
* Copyright (C) 2015 Stavros Polymenis. All rights reserved.
|
||||
* Author: Stavros Polymenis <sp@orbitalfox.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: div
|
||||
*
|
||||
* Description:
|
||||
* The div() function computes the quotient and remainder of the division
|
||||
* of the numerator 'numer' by the denominator 'denom". If the division is
|
||||
* inexact, the resulting quotient is the integer of lesser magnitude that
|
||||
* is the nearest to the algebraic quotient. If the result cannot be
|
||||
* represented, the behavior is undefined; otherwise, quot * denom + rem
|
||||
* will equal 'numer'.
|
||||
*
|
||||
* Input Parameters:
|
||||
* numer - Numerator of the Division
|
||||
* denom - Denominator of the division
|
||||
*
|
||||
* Returned Value:
|
||||
* The result of the devision represent as values of type div_t
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
div_t div(int numer, int denom)
|
||||
{
|
||||
div_t f;
|
||||
|
||||
f.quot = numer / denom;
|
||||
f.rem = numer % denom;
|
||||
return f;
|
||||
}
|
88
components/libc/nuttx/libc/stdlib/lib_itoa.c
Normal file
88
components/libc/nuttx/libc/stdlib/lib_itoa.c
Normal file
|
@ -0,0 +1,88 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_itoa.c
|
||||
*
|
||||
* Copyright (C) 2013 Brooks Automation, Inc. All rights reserved.
|
||||
* Author: Ryan Sundberg <ryan.sundberg@brooks.com>
|
||||
*
|
||||
* This file is a part of NuttX:
|
||||
*
|
||||
* Copyright (C) 2010-2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *itoa(int val, FAR char *str, int base)
|
||||
{
|
||||
static FAR const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
int intval = abs(val);
|
||||
int digit;
|
||||
int pos;
|
||||
int len;
|
||||
FAR char *buf = str;
|
||||
char swap;
|
||||
|
||||
if (base >= 2 && base <= 36)
|
||||
{
|
||||
do
|
||||
{
|
||||
digit = intval % base;
|
||||
intval = intval / base;
|
||||
*buf++ = digits[digit];
|
||||
}
|
||||
while (intval > 0);
|
||||
|
||||
if (val < 0)
|
||||
{
|
||||
*buf++ = '-';
|
||||
}
|
||||
|
||||
for (pos = 0, len = buf - str; pos < len / 2; pos++)
|
||||
{
|
||||
swap = str[len - pos - 1];
|
||||
str[len - pos - 1] = str[pos];
|
||||
str[pos] = swap;
|
||||
}
|
||||
}
|
||||
|
||||
*buf = '\0';
|
||||
|
||||
return str;
|
||||
}
|
40
components/libc/nuttx/libc/stdlib/lib_labs.c
Normal file
40
components/libc/nuttx/libc/stdlib/lib_labs.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_labs.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
long int labs(long int j)
|
||||
{
|
||||
if (j < 0)
|
||||
{
|
||||
j = -j;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
81
components/libc/nuttx/libc/stdlib/lib_ldiv.c
Normal file
81
components/libc/nuttx/libc/stdlib/lib_ldiv.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_ldiv.c
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* A direct leverage of the div() inplement by:
|
||||
*
|
||||
* Copyright (C) 2015 Stavros Polymenis. All rights reserved.
|
||||
* Author: Stavros Polymenis <sp@orbitalfox.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ldiv
|
||||
*
|
||||
* Description:
|
||||
* The ldiv() function computes the quotient and remainder of the division
|
||||
* of the numerator 'numer' by the denominator 'denom". If the division is
|
||||
* inexact, the resulting quotient is the integer of lesser magnitude that
|
||||
* is the nearest to the algebraic quotient. If the result cannot be
|
||||
* represented, the behavior is undefined; otherwise, quot * denom + rem
|
||||
* will equal 'numer'.
|
||||
*
|
||||
* Input Parameters:
|
||||
* numer - Numerator of the Division
|
||||
* denom - Denominator of the division
|
||||
*
|
||||
* Returned Value:
|
||||
* The result of the devision represent as values of type ldiv_t
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ldiv_t ldiv(long numer, long denom)
|
||||
{
|
||||
ldiv_t f;
|
||||
|
||||
f.quot = numer / denom;
|
||||
f.rem = numer % denom;
|
||||
return f;
|
||||
}
|
43
components/libc/nuttx/libc/stdlib/lib_llabs.c
Normal file
43
components/libc/nuttx/libc/stdlib/lib_llabs.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_llabs.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_LONG
|
||||
long long int llabs(long long int j)
|
||||
{
|
||||
if (j < 0)
|
||||
{
|
||||
j = -j;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
#endif
|
85
components/libc/nuttx/libc/stdlib/lib_lldiv.c
Normal file
85
components/libc/nuttx/libc/stdlib/lib_lldiv.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_lldiv.c
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* A direct leverage of the div() inplement by:
|
||||
*
|
||||
* Copyright (C) 2015 Stavros Polymenis. All rights reserved.
|
||||
* Author: Stavros Polymenis <sp@orbitalfox.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(CONFIG_HAVE_LONG_LONG)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lldiv
|
||||
*
|
||||
* Description:
|
||||
* The lldiv() function computes the quotient and remainder of the division
|
||||
* of the numerator 'numer' by the denominator 'denom". If the division is
|
||||
* inexact, the resulting quotient is the integer of lesser magnitude that
|
||||
* is the nearest to the algebraic quotient. If the result cannot be
|
||||
* represented, the behavior is undefined; otherwise, quot * denom + rem
|
||||
* will equal 'numer'.
|
||||
*
|
||||
* Input Parameters:
|
||||
* numer - Numerator of the Division
|
||||
* denom - Denominator of the division
|
||||
*
|
||||
* Returned Value:
|
||||
* The result of the devision represent as values of type lldiv_t
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
lldiv_t lldiv(long long numer, long long denom)
|
||||
{
|
||||
lldiv_t f;
|
||||
|
||||
f.quot = numer / denom;
|
||||
f.rem = numer % denom;
|
||||
return f;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_HAVE_LONG_LONG */
|
47
components/libc/nuttx/libc/stdlib/lib_mblen.c
Normal file
47
components/libc/nuttx/libc/stdlib/lib_mblen.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_mblen.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef CONFIG_LIBC_WCHAR
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mblen
|
||||
*
|
||||
* Description:
|
||||
* Determine number of bytes in next multibyte character
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int mblen(FAR const char *s, size_t n)
|
||||
{
|
||||
return mbtowc(NULL, s, n);
|
||||
}
|
||||
|
||||
#endif
|
47
components/libc/nuttx/libc/stdlib/lib_mbstowcs.c
Normal file
47
components/libc/nuttx/libc/stdlib/lib_mbstowcs.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_mbstowcs.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef CONFIG_LIBC_WCHAR
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mbsrtowcs
|
||||
*
|
||||
* Description:
|
||||
* Convert a multibyte string to a wide-character string
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
size_t mbstowcs(FAR wchar_t *dst, FAR const char *src, size_t len)
|
||||
{
|
||||
return mbsrtowcs(dst, &src, len, NULL);
|
||||
}
|
||||
|
||||
#endif
|
72
components/libc/nuttx/libc/stdlib/lib_mbtowc.c
Normal file
72
components/libc/nuttx/libc/stdlib/lib_mbtowc.c
Normal file
|
@ -0,0 +1,72 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_mbtowc.c
|
||||
*
|
||||
* Copyright (c)1999 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef CONFIG_LIBC_WCHAR
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mbtowc.c
|
||||
*
|
||||
* Description:
|
||||
* Minimal multibyte to wide char converter
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int mbtowc(FAR wchar_t *pwc, FAR const char *s, size_t n)
|
||||
{
|
||||
if (s == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pwc)
|
||||
{
|
||||
*pwc = (wchar_t)*s;
|
||||
}
|
||||
|
||||
return (*s != '\0');
|
||||
}
|
||||
#endif
|
87
components/libc/nuttx/libc/stdlib/lib_mkdtemp.c
Normal file
87
components/libc/nuttx/libc/stdlib/lib_mkdtemp.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_mkdtemp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mkdtemp
|
||||
*
|
||||
* Description:
|
||||
* The mkdtemp() function shall create a directory with a unique name
|
||||
* derived from template. The application shall ensure that the string
|
||||
* provided in template is a pathname ending with at least six trailing
|
||||
* 'X' characters. The mkdtemp() function shall modify the contents of
|
||||
* template by replacing six or more 'X' characters at the end of the
|
||||
* pathname with the same number of characters from the portable filename
|
||||
* character set. The characters shall be chosen such that the resulting
|
||||
* pathname does not duplicate the name of an existing file at the time
|
||||
* of the call to mkdtemp(). The mkdtemp() function shall use the
|
||||
* resulting pathname to create the new directory as if by a call to:
|
||||
*
|
||||
* Input Parameters:
|
||||
* template - The base directory name that will be modified to produce
|
||||
* the unique name. This must be a full path beginning with /tmp.
|
||||
* This function will modify only the first XXXXXX characters within
|
||||
* that full path.
|
||||
*
|
||||
* Returned Value:
|
||||
* Upon successful completion, the mkdtemp() function shall return the
|
||||
* value of template. Otherwise, it shall return a null pointer and
|
||||
* shall set errno to indicate the error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *mkdtemp(FAR char *path_template)
|
||||
{
|
||||
FAR char *path = mktemp(path_template);
|
||||
|
||||
if (path)
|
||||
{
|
||||
if (mkdir(path, S_IRWXU) < 0)
|
||||
{
|
||||
path = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
73
components/libc/nuttx/libc/stdlib/lib_mkstemp.c
Normal file
73
components/libc/nuttx/libc/stdlib/lib_mkstemp.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_mkstemp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mkstemp
|
||||
*
|
||||
* Description:
|
||||
* The mkstemp() function replaces the contents of the string pointed to
|
||||
* by path_template by a unique filename, and returns a file descriptor
|
||||
* for the file open for reading and writing. The function thus prevents
|
||||
* any possible race condition between testing whether the file exists and
|
||||
* opening it for use. The string in path_template should look like a
|
||||
* filename with six trailing 'X' s; mkstemp() replaces each 'X' with a
|
||||
* character from the portable filename character set. The characters are
|
||||
* chosen such that the resulting name does not duplicate the name of an
|
||||
* existing file at the time of a call to mkstemp().
|
||||
*
|
||||
* Input Parameters:
|
||||
* path_template - The base file name that will be modified to produce
|
||||
* the unique file name. This must be a full path beginning with /tmp.
|
||||
* This function will modify only the first XXXXXX characters within
|
||||
* that full path.
|
||||
*
|
||||
* Returned Value:
|
||||
* Upon successful completion, mkstemp() returns an open file descriptor.
|
||||
* Otherwise, -1 is returned if no suitable file could be created.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int mkstemp(FAR char *path_template)
|
||||
{
|
||||
FAR char *path = mktemp(path_template);
|
||||
int ret = ERROR;
|
||||
|
||||
if (path)
|
||||
{
|
||||
ret = open(path, O_RDWR | O_CREAT | O_EXCL, 0666);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
259
components/libc/nuttx/libc/stdlib/lib_mktemp.c
Normal file
259
components/libc/nuttx/libc/stdlib/lib_mktemp.c
Normal file
|
@ -0,0 +1,259 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_mktemp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <nuttx/semaphore.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define MAX_XS 6
|
||||
#define MIN_NUMERIC 0 /* 0-9: Numeric */
|
||||
#define MAX_NUMERIC 9
|
||||
#define MIN_UPPERCASE 10 /* 10-35: Upper case */
|
||||
#define MAX_UPPERCASE 35
|
||||
#define MIN_LOWERCASE 36 /* 36-61: Lower case */
|
||||
#define MAX_LOWERCASE 61
|
||||
#define MAX_BASE62 MAX_LOWERCASE
|
||||
|
||||
/* 62**1 = 62
|
||||
* 62**2 = 3844
|
||||
* 62**3 = 238328
|
||||
* 62**4 = 14776336
|
||||
* 62**5 = 916132832
|
||||
* 62**6 = 56800235584 > UINT32_MAX
|
||||
*/
|
||||
|
||||
#define BIG_XS 5
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static uint8_t g_base62[MAX_XS];
|
||||
static sem_t g_b62sem = SEM_INITIALIZER(1);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base62_to_char
|
||||
*
|
||||
* Description:
|
||||
* Convert a base62 value to a printable character.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static char base62_to_char(uint8_t base62)
|
||||
{
|
||||
if (base62 <= MAX_NUMERIC)
|
||||
{
|
||||
return '0' + base62;
|
||||
}
|
||||
else if (base62 <= MAX_UPPERCASE)
|
||||
{
|
||||
return 'A' + base62 - MIN_UPPERCASE;
|
||||
}
|
||||
else /* if (base62 <= MAX_LOWERCASE) */
|
||||
{
|
||||
DEBUGASSERT(base62 <= MAX_LOWERCASE);
|
||||
return 'a' + base62 - MIN_LOWERCASE;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: incr_base62
|
||||
*
|
||||
* Description:
|
||||
* increment the base62 value array.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void incr_base62(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = MAX_XS - 1; i >= 0; i--)
|
||||
{
|
||||
if (g_base62[i] < MAX_LOWERCASE)
|
||||
{
|
||||
g_base62[i]++;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_base62[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: get_base62
|
||||
*
|
||||
* Description:
|
||||
* Atomically copy and increment the base62 array.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void get_base62(FAR uint8_t *ptr)
|
||||
{
|
||||
int ret;
|
||||
|
||||
while ((ret = _SEM_WAIT(&g_b62sem)) < 0)
|
||||
{
|
||||
DEBUGASSERT(_SEM_ERRNO(ret) == EINTR || _SEM_ERRNO(ret) == ECANCELED);
|
||||
}
|
||||
|
||||
memcpy(ptr, g_base62, MAX_XS);
|
||||
incr_base62();
|
||||
_SEM_POST(&g_b62sem);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: copy_base62
|
||||
*
|
||||
* Description:
|
||||
* Copy the base62 array into the template filename, converting each
|
||||
* base62 value to a printable character.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void copy_base62(FAR const uint8_t *src, FAR char *dest, int len)
|
||||
{
|
||||
if (len < MAX_XS)
|
||||
{
|
||||
src += MAX_XS - len;
|
||||
}
|
||||
|
||||
for (; len > 0; len--)
|
||||
{
|
||||
*dest++ = base62_to_char(*src++);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mktemp
|
||||
*
|
||||
* Description:
|
||||
* The mktemp() function generates a unique temporary filename from
|
||||
* template. The last six characters of template must be XXXXXX and these
|
||||
* are replaced with a string that makes the filename unique. Since it
|
||||
* will be modified, template must not be a string constant, but should be
|
||||
* declared as a character array.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *mktemp(FAR char *path_template)
|
||||
{
|
||||
uint8_t base62[MAX_XS];
|
||||
uint32_t retries;
|
||||
struct stat buf;
|
||||
FAR char *xptr;
|
||||
int xlen;
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
/* Count the number of X's at the end of the template */
|
||||
|
||||
xptr = &path_template[strlen(path_template)];
|
||||
for (xlen = 0; xlen < MAX_XS && path_template < xptr && *(xptr - 1) == 'X';
|
||||
xlen++, xptr--);
|
||||
|
||||
if (xlen == 0)
|
||||
{
|
||||
/* No Xs? There should always really be 6 */
|
||||
|
||||
return path_template;
|
||||
}
|
||||
|
||||
/* Ignore any X's after the sixth */
|
||||
|
||||
if (xlen > MAX_XS)
|
||||
{
|
||||
xptr += xlen - MAX_XS;
|
||||
xlen = MAX_XS;
|
||||
}
|
||||
|
||||
/* If xlen is small, then we need to determine the maximum number of
|
||||
* retries before the values will repeat.
|
||||
*/
|
||||
|
||||
if (xlen >= BIG_XS)
|
||||
{
|
||||
retries = UINT32_MAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 1, retries = 62; i < xlen; i++, retries *= 62);
|
||||
}
|
||||
|
||||
/* Then loop until we find a unique file name */
|
||||
|
||||
while (retries > 0)
|
||||
{
|
||||
/* Sample and increment the base62 counter */
|
||||
|
||||
get_base62(base62);
|
||||
|
||||
/* Form the candidate file name */
|
||||
|
||||
copy_base62(base62, xptr, xlen);
|
||||
|
||||
/* Attempt to stat the candidate file */
|
||||
|
||||
ret = stat(path_template, &buf);
|
||||
if (ret < 0 && get_errno() == ENOENT)
|
||||
{
|
||||
/* We have it... Clear the errno and return the template */
|
||||
|
||||
set_errno(0);
|
||||
return path_template;
|
||||
}
|
||||
|
||||
retries--;
|
||||
}
|
||||
|
||||
/* We could not find an unique filename */
|
||||
|
||||
set_errno(EINVAL);
|
||||
|
||||
return NULL;
|
||||
}
|
58
components/libc/nuttx/libc/stdlib/lib_ptsname.c
Normal file
58
components/libc/nuttx/libc/stdlib/lib_ptsname.c
Normal file
|
@ -0,0 +1,58 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_ptsname.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef CONFIG_PSEUDOTERM_SUSV1
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ptsname
|
||||
*
|
||||
* Description:
|
||||
* The ptsname() function returns the name of the slave pseudoterminal
|
||||
* device corresponding to the master referred to by fd.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, ptsname() returns a pointer to a string in static storage
|
||||
* which will be overwritten by subsequent calls. This pointer must not
|
||||
* be freed. On failure, NULL is returned.
|
||||
*
|
||||
* ENOTTY fd does not refer to a pseudoterminal master device.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *ptsname(int fd)
|
||||
{
|
||||
static char devname[16];
|
||||
int ret = ptsname_r(fd, devname, 16);
|
||||
return ret < 0 ? NULL : devname;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_PSEUDOTERM_SUSV1 */
|
80
components/libc/nuttx/libc/stdlib/lib_ptsnamer.c
Normal file
80
components/libc/nuttx/libc/stdlib/lib_ptsnamer.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_ptsnamer.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef CONFIG_PSEUDOTERM_SUSV1
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ptsname_r
|
||||
*
|
||||
* Description:
|
||||
* The ptsname_r() function is the reentrant equivalent of ptsname().
|
||||
* It returns the name of the slave pseudoterminal device as a null-
|
||||
* terminated string in the buffer pointed to by buf. The buflen
|
||||
* argument specifies the number of bytes available in buf.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, ptsname_r() returns 0. On failure, a nonzero value is
|
||||
* returned and errno is set to indicate the error.
|
||||
*
|
||||
* EINVAL (ptsname_r() only) buf is NULL.
|
||||
* ENOTTY fd does not refer to a pseudoterminal master device.
|
||||
* ERANGE (ptsname_r() only) buf is too small.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ptsname_r(int fd, FAR char *buf, size_t buflen)
|
||||
{
|
||||
int ptyno;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(buf != NULL);
|
||||
|
||||
/* Get the slave PTY number */
|
||||
|
||||
ret = ioctl(fd, TIOCGPTN, (unsigned long)((uintptr_t)&ptyno));
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Create the device name. This current does not handler EINVAL or ERANGE
|
||||
* error detection.
|
||||
*/
|
||||
|
||||
snprintf(buf, buflen, "/dev/pts/%d", ptyno);
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_PSEUDOTERM_SUSV1 */
|
297
components/libc/nuttx/libc/stdlib/lib_qsort.c
Normal file
297
components/libc/nuttx/libc/stdlib/lib_qsort.c
Normal file
|
@ -0,0 +1,297 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_qsort.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Leveraged from:
|
||||
*
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define min(a, b) (a) < (b) ? a : b
|
||||
|
||||
#define swapcode(TYPE, parmi, parmj, n) \
|
||||
{ \
|
||||
long i = (n) / sizeof (TYPE); \
|
||||
register TYPE *pi = (TYPE *)(parmi); \
|
||||
register TYPE *pj = (TYPE *)(parmj); \
|
||||
do { \
|
||||
register TYPE t = *pi; \
|
||||
*pi++ = *pj; \
|
||||
*pj++ = t; \
|
||||
} while (--i > 0); \
|
||||
}
|
||||
|
||||
#define SWAPINIT(a, width) \
|
||||
swaptype = ((FAR char *)a - (FAR char *)0) % sizeof(long) || \
|
||||
width % sizeof(long) ? 2 : width == sizeof(long)? 0 : 1;
|
||||
|
||||
#define swap(a, b) \
|
||||
if (swaptype == 0) \
|
||||
{ \
|
||||
long t = *(long *)(a); \
|
||||
*(long *)(a) = *(long *)(b); \
|
||||
*(long *)(b) = t; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
swapfunc(a, b, width, swaptype); \
|
||||
}
|
||||
|
||||
#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static inline void swapfunc(FAR char *a, FAR char *b, int n, int swaptype);
|
||||
static inline FAR char *med3(FAR char *a, FAR char *b, FAR char *c,
|
||||
CODE int (*compar)(FAR const void *,
|
||||
FAR const void *));
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline void swapfunc(FAR char *a, FAR char *b, int n, int swaptype)
|
||||
{
|
||||
if (swaptype <= 1)
|
||||
{
|
||||
swapcode(long, a, b, n)
|
||||
}
|
||||
else
|
||||
{
|
||||
swapcode(char, a, b, n)
|
||||
}
|
||||
}
|
||||
|
||||
static inline FAR char *med3(FAR char *a, FAR char *b, FAR char *c,
|
||||
CODE int (*compar)(FAR const void *,
|
||||
FAR const void *))
|
||||
{
|
||||
return compar(a, b) < 0 ?
|
||||
(compar(b, c) < 0 ? b : (compar(a, c) < 0 ? c : a)) :
|
||||
(compar(b, c) > 0 ? b : (compar(a, c) < 0 ? a : c));
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: qsort
|
||||
*
|
||||
* Description:
|
||||
* The qsort() function will sort an array of 'nel' objects, the initial
|
||||
* element of which is pointed to by 'base'. The size of each object, in
|
||||
* bytes, is specified by the 'width" argument. If the 'nel' argument has
|
||||
* the value zero, the comparison function pointed to by 'compar' will not
|
||||
* be called and no rearrangement will take place.
|
||||
*
|
||||
* The application will ensure that the comparison function pointed to by
|
||||
* 'compar' does not alter the contents of the array. The implementation
|
||||
* may reorder elements of the array between calls to the comparison
|
||||
* function, but will not alter the contents of any individual element.
|
||||
*
|
||||
* When the same objects (consisting of 'width" bytes, irrespective of
|
||||
* their current positions in the array) are passed more than once to
|
||||
* the comparison function, the results will be consistent with one
|
||||
* another. That is, they will define a total ordering on the array.
|
||||
*
|
||||
* The contents of the array will be sorted in ascending order according
|
||||
* to a comparison function. The 'compar' argument is a pointer to the
|
||||
* comparison function, which is called with two arguments that point to
|
||||
* the elements being compared. The application will ensure that the
|
||||
* function returns an integer less than, equal to, or greater than 0,
|
||||
* if the first argument is considered respectively less than, equal to,
|
||||
* or greater than the second. If two members compare as equal, their
|
||||
* order in the sorted array is unspecified.
|
||||
*
|
||||
* (Based on description from OpenGroup.org).
|
||||
*
|
||||
* Returned Value:
|
||||
* The qsort() function will not return a value.
|
||||
*
|
||||
* Notes from the original BSD version:
|
||||
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void qsort(FAR void *base, size_t nel, size_t width,
|
||||
CODE int(*compar)(FAR const void *, FAR const void *))
|
||||
{
|
||||
FAR char *pa;
|
||||
FAR char *pb;
|
||||
FAR char *pc;
|
||||
FAR char *pd;
|
||||
FAR char *pl;
|
||||
FAR char *pm;
|
||||
FAR char *pn;
|
||||
int swaptype;
|
||||
int swap_cnt;
|
||||
int d;
|
||||
int r;
|
||||
|
||||
loop:
|
||||
SWAPINIT(base, width);
|
||||
swap_cnt = 0;
|
||||
|
||||
if (nel < 7)
|
||||
{
|
||||
for (pm = (FAR char *)base + width;
|
||||
pm < (FAR char *)base + nel * width;
|
||||
pm += width)
|
||||
{
|
||||
for (pl = pm;
|
||||
pl > (FAR char *)base && compar(pl - width, pl) > 0;
|
||||
pl -= width)
|
||||
{
|
||||
swap(pl, pl - width);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
pm = (FAR char *)base + (nel / 2) * width;
|
||||
if (nel > 7)
|
||||
{
|
||||
pl = base;
|
||||
pn = (FAR char *)base + (nel - 1) * width;
|
||||
if (nel > 40)
|
||||
{
|
||||
d = (nel / 8) * width;
|
||||
pl = med3(pl, pl + d, pl + 2 * d, compar);
|
||||
pm = med3(pm - d, pm, pm + d, compar);
|
||||
pn = med3(pn - 2 * d, pn - d, pn, compar);
|
||||
}
|
||||
|
||||
pm = med3(pl, pm, pn, compar);
|
||||
}
|
||||
|
||||
swap(base, pm);
|
||||
pa = pb = (FAR char *)base + width;
|
||||
|
||||
pc = pd = (FAR char *)base + (nel - 1) * width;
|
||||
for (; ; )
|
||||
{
|
||||
while (pb <= pc && (r = compar(pb, base)) <= 0)
|
||||
{
|
||||
if (r == 0)
|
||||
{
|
||||
swap_cnt = 1;
|
||||
swap(pa, pb);
|
||||
pa += width;
|
||||
}
|
||||
|
||||
pb += width;
|
||||
}
|
||||
|
||||
while (pb <= pc && (r = compar(pc, base)) >= 0)
|
||||
{
|
||||
if (r == 0)
|
||||
{
|
||||
swap_cnt = 1;
|
||||
swap(pc, pd);
|
||||
pd -= width;
|
||||
}
|
||||
|
||||
pc -= width;
|
||||
}
|
||||
|
||||
if (pb > pc)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
swap(pb, pc);
|
||||
swap_cnt = 1;
|
||||
pb += width;
|
||||
pc -= width;
|
||||
}
|
||||
|
||||
if (swap_cnt == 0)
|
||||
{
|
||||
/* Switch to insertion sort */
|
||||
|
||||
for (pm = (FAR char *)base + width;
|
||||
pm < (FAR char *)base + nel * width;
|
||||
pm += width)
|
||||
{
|
||||
for (pl = pm;
|
||||
pl > (FAR char *)base && compar(pl - width, pl) > 0;
|
||||
pl -= width)
|
||||
{
|
||||
swap(pl, pl - width);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
pn = (FAR char *)base + nel * width;
|
||||
r = min(pa - (FAR char *)base, pb - pa);
|
||||
vecswap(base, pb - r, r);
|
||||
|
||||
r = min(pd - pc, pn - pd - width);
|
||||
vecswap(pb, pn - r, r);
|
||||
|
||||
if ((r = pb - pa) > width)
|
||||
{
|
||||
qsort(base, r / width, width, compar);
|
||||
}
|
||||
|
||||
if ((r = pd - pc) > width)
|
||||
{
|
||||
/* Iterate rather than recurse to save stack space */
|
||||
|
||||
base = pn - r;
|
||||
nel = r / width;
|
||||
goto loop;
|
||||
}
|
||||
}
|
63
components/libc/nuttx/libc/stdlib/lib_rand.c
Normal file
63
components/libc/nuttx/libc/stdlib/lib_rand.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_rand.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <nuttx/lib/lib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: rand
|
||||
*
|
||||
* Description:
|
||||
* Generate a non-negative, integer random number in the range of 0 through
|
||||
* (RAND_MAX - 1)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int rand(void)
|
||||
{
|
||||
return (int)nrand(INT_MAX);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: random
|
||||
*
|
||||
* Description:
|
||||
* Generate a non-negative, integer random number in the range of 0 through
|
||||
* (LONG_MAX - 1)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
long random(void)
|
||||
{
|
||||
return (long)nrand(LONG_MAX);
|
||||
}
|
237
components/libc/nuttx/libc/stdlib/lib_realpath.c
Normal file
237
components/libc/nuttx/libc/stdlib/lib_realpath.c
Normal file
|
@ -0,0 +1,237 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_realpath.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "libc.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *realpath(FAR const char *path, FAR char *resolved)
|
||||
{
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
char wbuf[2][PATH_MAX];
|
||||
int nlnk = 0;
|
||||
int idx = 0;
|
||||
ssize_t n;
|
||||
#endif
|
||||
FAR const char *q;
|
||||
FAR char *fres = NULL;
|
||||
FAR char *p;
|
||||
struct stat sb;
|
||||
size_t len;
|
||||
|
||||
if (path == NULL)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (*path == '\0')
|
||||
{
|
||||
set_errno(ENOENT);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (resolved == NULL)
|
||||
{
|
||||
fres = resolved = lib_malloc(PATH_MAX);
|
||||
if (resolved == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Build real path one by one with paying an attention to .,
|
||||
* .. and symbolic link.
|
||||
*/
|
||||
|
||||
/* `p' is where we'll put a new component with prepending
|
||||
* a delimiter.
|
||||
*/
|
||||
|
||||
p = resolved;
|
||||
|
||||
/* If relative path, start from current working directory. */
|
||||
|
||||
if (*path != '/')
|
||||
{
|
||||
/* check for resolved pointer to appease coverity */
|
||||
|
||||
if (getcwd(resolved, PATH_MAX) == NULL)
|
||||
{
|
||||
goto out;
|
||||
}
|
||||
|
||||
len = strlen(resolved);
|
||||
if (len > 1)
|
||||
{
|
||||
p += len;
|
||||
}
|
||||
}
|
||||
|
||||
loop:
|
||||
|
||||
/* Skip any slash. */
|
||||
|
||||
while (*path == '/')
|
||||
{
|
||||
path++;
|
||||
}
|
||||
|
||||
if (*path == '\0')
|
||||
{
|
||||
if (p == resolved)
|
||||
{
|
||||
*p++ = '/';
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
return resolved;
|
||||
}
|
||||
|
||||
/* Find the end of this component. */
|
||||
|
||||
q = path;
|
||||
do
|
||||
{
|
||||
q++;
|
||||
}
|
||||
while (*q != '/' && *q != '\0');
|
||||
|
||||
/* Test . or .. */
|
||||
|
||||
if (path[0] == '.')
|
||||
{
|
||||
if (q - path == 1)
|
||||
{
|
||||
path = q;
|
||||
goto loop;
|
||||
}
|
||||
|
||||
if (path[1] == '.' && q - path == 2)
|
||||
{
|
||||
/* Trim the last component. */
|
||||
|
||||
if (p != resolved)
|
||||
{
|
||||
while (*--p != '/')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
path = q;
|
||||
goto loop;
|
||||
}
|
||||
}
|
||||
|
||||
/* Append this component. */
|
||||
|
||||
if (p - resolved + 1 + q - path + 1 > PATH_MAX)
|
||||
{
|
||||
set_errno(ENAMETOOLONG);
|
||||
goto out;
|
||||
}
|
||||
|
||||
p[0] = '/';
|
||||
memcpy(&p[1], path, q - path);
|
||||
p[1 + q - path] = '\0';
|
||||
|
||||
/* If this component is a symlink, toss it and prepend link
|
||||
* target to unresolved path.
|
||||
*/
|
||||
|
||||
if (lstat(resolved, &sb) == -1)
|
||||
{
|
||||
goto out;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
if (S_ISLNK(sb.st_mode))
|
||||
{
|
||||
if (nlnk++ >= SYMLOOP_MAX)
|
||||
{
|
||||
set_errno(ELOOP);
|
||||
goto out;
|
||||
}
|
||||
|
||||
n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
|
||||
if (n <= 0)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
set_errno(ENOENT);
|
||||
}
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Append unresolved path to link target and switch to it. */
|
||||
|
||||
if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0]))
|
||||
{
|
||||
set_errno(ENAMETOOLONG);
|
||||
goto out;
|
||||
}
|
||||
|
||||
memcpy(&wbuf[idx][n], q, len + 1);
|
||||
path = wbuf[idx];
|
||||
idx ^= 1;
|
||||
|
||||
/* If absolute symlink, start from root. */
|
||||
|
||||
if (*path == '/')
|
||||
{
|
||||
p = resolved;
|
||||
}
|
||||
|
||||
goto loop;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (*q == '/' && !S_ISDIR(sb.st_mode))
|
||||
{
|
||||
set_errno(ENOTDIR);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Advance both resolved and unresolved path. */
|
||||
|
||||
p += 1 + q - path;
|
||||
path = q;
|
||||
goto loop;
|
||||
|
||||
out:
|
||||
lib_free(fres);
|
||||
return NULL;
|
||||
}
|
281
components/libc/nuttx/libc/stdlib/lib_srand.c
Normal file
281
components/libc/nuttx/libc/stdlib/lib_srand.c
Normal file
|
@ -0,0 +1,281 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_srand.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <nuttx/lib/lib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* First, second, and thired order congruential generators are supported */
|
||||
|
||||
#ifndef CONFIG_LIB_RAND_ORDER
|
||||
# define CONFIG_LIB_RAND_ORDER 1
|
||||
#endif
|
||||
|
||||
#if CONFIG_LIB_RAND_ORDER > 3
|
||||
# undef CONFIG_LIB_RAND_ORDER
|
||||
# define CONFIG_LIB_RAND_ORDER 3
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_HAVE_DOUBLE
|
||||
typedef float float_t;
|
||||
#else
|
||||
typedef double float_t;
|
||||
#endif
|
||||
|
||||
/* Values needed by the random number generator */
|
||||
|
||||
#define RND1_CONSTK 470001
|
||||
#define RND1_CONSTP 999563
|
||||
#define RND2_CONSTK1 366528
|
||||
#define RND2_CONSTK2 508531
|
||||
#define RND2_CONSTP 998917
|
||||
#define RND3_CONSTK1 360137
|
||||
#define RND3_CONSTK2 519815
|
||||
#define RND3_CONSTK3 616087
|
||||
#define RND3_CONSTP 997783
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/* First order congruential generators */
|
||||
|
||||
static inline unsigned long fgenerate1(void);
|
||||
#if (CONFIG_LIB_RAND_ORDER == 1)
|
||||
static float_t frand1(void);
|
||||
#endif
|
||||
|
||||
/* Second order congruential generators */
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER > 1)
|
||||
static inline unsigned long fgenerate2(void);
|
||||
#if (CONFIG_LIB_RAND_ORDER == 2)
|
||||
static float_t frand2(void);
|
||||
#endif
|
||||
|
||||
/* Third order congruential generators */
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER > 2)
|
||||
static inline unsigned long fgenerate3(void);
|
||||
static float_t frand3(void);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static unsigned long g_randint1;
|
||||
#if (CONFIG_LIB_RAND_ORDER > 1)
|
||||
static unsigned long g_randint2;
|
||||
#if (CONFIG_LIB_RAND_ORDER > 2)
|
||||
static unsigned long g_randint3;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/* First order congruential generators */
|
||||
|
||||
static inline unsigned long fgenerate1(void)
|
||||
{
|
||||
unsigned long randint;
|
||||
|
||||
/* First order congruential generator. One may be added to the result of
|
||||
* the generated value to avoid the value zero. This would be fatal for
|
||||
* the first order random number generator.
|
||||
*/
|
||||
|
||||
randint = (RND1_CONSTK * g_randint1) % RND1_CONSTP;
|
||||
g_randint1 = (randint == 0 ? 1 : randint);
|
||||
return randint;
|
||||
}
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER == 1)
|
||||
static float_t frand1(void)
|
||||
{
|
||||
/* First order congruential generator. */
|
||||
|
||||
unsigned long randint = fgenerate1();
|
||||
|
||||
/* Construct an floating point value in the range from 0.0 up to 1.0 */
|
||||
|
||||
return ((float_t)randint) / ((float_t)RND1_CONSTP);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Second order congruential generators */
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER > 1)
|
||||
static inline unsigned long fgenerate2(void)
|
||||
{
|
||||
unsigned long randint;
|
||||
|
||||
/* Second order congruential generator. */
|
||||
|
||||
randint = (RND2_CONSTK1 * g_randint1 +
|
||||
RND2_CONSTK2 * g_randint2) % RND2_CONSTP;
|
||||
|
||||
g_randint2 = g_randint1;
|
||||
g_randint1 = randint;
|
||||
|
||||
/* We cannot permit both values to become zero. That would be fatal for
|
||||
* the second order random number generator.
|
||||
*/
|
||||
|
||||
if (g_randint2 == 0 && g_randint1 == 0)
|
||||
{
|
||||
g_randint2 = 1;
|
||||
}
|
||||
|
||||
return randint;
|
||||
}
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER == 2)
|
||||
static float_t frand2(void)
|
||||
{
|
||||
/* Second order congruential generator */
|
||||
|
||||
unsigned long randint = fgenerate2();
|
||||
|
||||
/* Construct an floating point value in the range from 0.0 up to 1.0 */
|
||||
|
||||
return ((float_t)randint) / ((float_t)RND2_CONSTP);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Third order congruential generators */
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER > 2)
|
||||
static inline unsigned long fgenerate3(void)
|
||||
{
|
||||
unsigned long randint;
|
||||
|
||||
/* Third order congruential generator. */
|
||||
|
||||
randint = (RND3_CONSTK1 * g_randint1 +
|
||||
RND3_CONSTK2 * g_randint2 +
|
||||
RND3_CONSTK2 * g_randint3) % RND3_CONSTP;
|
||||
|
||||
g_randint3 = g_randint2;
|
||||
g_randint2 = g_randint1;
|
||||
g_randint1 = randint;
|
||||
|
||||
/* We cannot permit all three values to become zero. That would be fatal
|
||||
* for the third order random number generator.
|
||||
*/
|
||||
|
||||
if (g_randint3 == 0 && g_randint2 == 0 && g_randint1 == 0)
|
||||
{
|
||||
g_randint3 = 1;
|
||||
}
|
||||
|
||||
return randint;
|
||||
}
|
||||
|
||||
static float_t frand3(void)
|
||||
{
|
||||
/* Third order congruential generator */
|
||||
|
||||
unsigned long randint = fgenerate3();
|
||||
|
||||
/* Construct an floating point value in the range from 0.0 up to 1.0 */
|
||||
|
||||
return ((float_t)randint) / ((float_t)RND3_CONSTP);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: srand
|
||||
*
|
||||
* Description:
|
||||
* Seed the congruential random number generator.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void srand(unsigned int seed)
|
||||
{
|
||||
g_randint1 = seed;
|
||||
#if (CONFIG_LIB_RAND_ORDER > 1)
|
||||
g_randint2 = seed;
|
||||
fgenerate1();
|
||||
#if (CONFIG_LIB_RAND_ORDER > 2)
|
||||
g_randint3 = seed;
|
||||
fgenerate2();
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nrand
|
||||
*
|
||||
* Description:
|
||||
* Return a random, unsigned long value in the range of 0 to (limit - 1)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
unsigned long nrand(unsigned long limit)
|
||||
{
|
||||
unsigned long result;
|
||||
float_t ratio;
|
||||
|
||||
/* Loop to be sure a legal random number is generated */
|
||||
|
||||
do
|
||||
{
|
||||
/* Get a random integer in the range 0.0 - 1.0 */
|
||||
|
||||
#if (CONFIG_LIB_RAND_ORDER == 1)
|
||||
ratio = frand1();
|
||||
#elif (CONFIG_LIB_RAND_ORDER == 2)
|
||||
ratio = frand2();
|
||||
#else /* if (CONFIG_LIB_RAND_ORDER > 2) */
|
||||
ratio = frand3();
|
||||
#endif
|
||||
|
||||
/* Then, produce the return-able value in the requested range */
|
||||
|
||||
result = (unsigned long)(((float_t)limit) * ratio);
|
||||
|
||||
/* Loop because there is an (unlikely) possibility that rounding
|
||||
* could increase the result at the limit value about the limit.
|
||||
*/
|
||||
}
|
||||
while (result >= limit);
|
||||
|
||||
return result;
|
||||
}
|
264
components/libc/nuttx/libc/stdlib/lib_strtod.c
Normal file
264
components/libc/nuttx/libc/stdlib/lib_strtod.c
Normal file
|
@ -0,0 +1,264 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtod.c
|
||||
* Convert string to double
|
||||
*
|
||||
* Copyright (C) 2002 Michael Ringgaard. All rights reserved.
|
||||
* Copyright (C) 2006-2007 H. Peter Anvin.
|
||||
*
|
||||
* 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. Neither the name of the project nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef CONFIG_HAVE_DOUBLE
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* These are predefined with GCC, but could be issues for other compilers. If
|
||||
* not defined, an arbitrary big number is put in for now. These should be
|
||||
* added to nuttx/compiler for your compiler.
|
||||
*/
|
||||
|
||||
#if !defined(__DBL_MIN_EXP__) || !defined(__DBL_MAX_EXP__)
|
||||
# ifdef CONFIG_CPP_HAVE_WARNING
|
||||
# warning "Size of exponent is unknown"
|
||||
# endif
|
||||
# undef __DBL_MIN_EXP__
|
||||
# define __DBL_MIN_EXP__ (-1021)
|
||||
# undef __DBL_MAX_EXP__
|
||||
# define __DBL_MAX_EXP__ (1024)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int is_real(double x)
|
||||
{
|
||||
const double infinite = 1.0 / 0.0;
|
||||
return (x < infinite) && (x >= -infinite);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/***************************************************(************************
|
||||
* Name: strtod
|
||||
*
|
||||
* Description:
|
||||
* Convert a string to a double value
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
double strtod(FAR const char *str, FAR char **endptr)
|
||||
{
|
||||
double number;
|
||||
int exponent;
|
||||
int negative;
|
||||
FAR char *p = (FAR char *) str;
|
||||
double p10;
|
||||
int n;
|
||||
int num_digits;
|
||||
int num_decimals;
|
||||
const double infinite = 1.0 / 0.0;
|
||||
|
||||
/* Skip leading whitespace */
|
||||
|
||||
while (isspace(*p))
|
||||
{
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Handle optional sign */
|
||||
|
||||
negative = 0;
|
||||
switch (*p)
|
||||
{
|
||||
case '-':
|
||||
negative = 1; /* Fall through to increment position */
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case '+':
|
||||
p++;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
number = 0.;
|
||||
exponent = 0;
|
||||
num_digits = 0;
|
||||
num_decimals = 0;
|
||||
|
||||
/* Process string of digits */
|
||||
|
||||
while (isdigit(*p))
|
||||
{
|
||||
number = number * 10. + (*p - '0');
|
||||
p++;
|
||||
num_digits++;
|
||||
}
|
||||
|
||||
/* Process decimal part */
|
||||
|
||||
if (*p == '.')
|
||||
{
|
||||
p++;
|
||||
|
||||
while (isdigit(*p))
|
||||
{
|
||||
number = number * 10. + (*p - '0');
|
||||
p++;
|
||||
num_digits++;
|
||||
num_decimals++;
|
||||
}
|
||||
|
||||
exponent -= num_decimals;
|
||||
}
|
||||
|
||||
if (num_digits == 0)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
number = 0.0;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Correct for sign */
|
||||
|
||||
if (negative)
|
||||
{
|
||||
number = -number;
|
||||
}
|
||||
|
||||
/* Process an exponent string */
|
||||
|
||||
if (*p == 'e' || *p == 'E')
|
||||
{
|
||||
/* Handle optional sign */
|
||||
|
||||
negative = 0;
|
||||
switch (*++p)
|
||||
{
|
||||
case '-':
|
||||
negative = 1; /* Fall through to increment pos */
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case '+':
|
||||
p++;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Process string of digits */
|
||||
|
||||
n = 0;
|
||||
while (isdigit(*p))
|
||||
{
|
||||
n = n * 10 + (*p - '0');
|
||||
p++;
|
||||
}
|
||||
|
||||
if (negative)
|
||||
{
|
||||
exponent -= n;
|
||||
}
|
||||
else
|
||||
{
|
||||
exponent += n;
|
||||
}
|
||||
}
|
||||
|
||||
if (exponent < __DBL_MIN_EXP__ ||
|
||||
exponent > __DBL_MAX_EXP__)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
number = infinite;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Scale the result */
|
||||
|
||||
p10 = 10.;
|
||||
n = exponent;
|
||||
if (n < 0)
|
||||
{
|
||||
n = -n;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
if (n & 1)
|
||||
{
|
||||
if (exponent < 0)
|
||||
{
|
||||
number /= p10;
|
||||
}
|
||||
else
|
||||
{
|
||||
number *= p10;
|
||||
}
|
||||
}
|
||||
|
||||
n >>= 1;
|
||||
p10 *= p10;
|
||||
}
|
||||
|
||||
if (!is_real(number))
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
}
|
||||
|
||||
errout:
|
||||
if (endptr)
|
||||
{
|
||||
*endptr = p;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_HAVE_DOUBLE */
|
264
components/libc/nuttx/libc/stdlib/lib_strtof.c
Normal file
264
components/libc/nuttx/libc/stdlib/lib_strtof.c
Normal file
|
@ -0,0 +1,264 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtof.c
|
||||
* Convert string to float
|
||||
*
|
||||
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||
*
|
||||
* A pretty straight forward conversion fo strtod():
|
||||
*
|
||||
* Copyright (C) 2002 Michael Ringgaard. All rights reserved.
|
||||
* Copyright (C) 2006-2007 H. Peter Anvin.
|
||||
*
|
||||
* 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. Neither the name of the project nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* These are predefined with GCC, but could be issues for other compilers. If
|
||||
* not defined, an arbitrary big number is put in for now. These should be
|
||||
* added to nuttx/compiler for your compiler.
|
||||
*/
|
||||
|
||||
#if !defined(__FLT_MIN_EXP__) || !defined(__FLT_MAX_EXP__)
|
||||
# ifdef CONFIG_CPP_HAVE_WARNING
|
||||
# warning "Size of exponent is unknown"
|
||||
# endif
|
||||
# undef __FLT_MIN_EXP__
|
||||
# define __FLT_MIN_EXP__ (-125)
|
||||
# undef __FLT_MAX_EXP__
|
||||
# define __FLT_MAX_EXP__ (128)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int is_real(float x)
|
||||
{
|
||||
const float infinite = 1.0F / 0.0F;
|
||||
return (x < infinite) && (x >= -infinite);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/***************************************************(************************
|
||||
* Name: strtof
|
||||
*
|
||||
* Description:
|
||||
* Convert a string to a float value
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
float strtof(FAR const char *str, FAR char **endptr)
|
||||
{
|
||||
float number;
|
||||
int exponent;
|
||||
int negative;
|
||||
FAR char *p = (FAR char *) str;
|
||||
float p10;
|
||||
int n;
|
||||
int num_digits;
|
||||
int num_decimals;
|
||||
const float infinite = 1.0F / 0.0F;
|
||||
|
||||
/* Skip leading whitespace */
|
||||
|
||||
while (isspace(*p))
|
||||
{
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Handle optional sign */
|
||||
|
||||
negative = 0;
|
||||
switch (*p)
|
||||
{
|
||||
case '-':
|
||||
negative = 1; /* Fall through to increment position */
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case '+':
|
||||
p++;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
number = 0.0F;
|
||||
exponent = 0;
|
||||
num_digits = 0;
|
||||
num_decimals = 0;
|
||||
|
||||
/* Process string of digits */
|
||||
|
||||
while (isdigit(*p))
|
||||
{
|
||||
number = number * 10.0F + (float)(*p - '0');
|
||||
p++;
|
||||
num_digits++;
|
||||
}
|
||||
|
||||
/* Process decimal part */
|
||||
|
||||
if (*p == '.')
|
||||
{
|
||||
p++;
|
||||
|
||||
while (isdigit(*p))
|
||||
{
|
||||
number = number * 10.0F + (float)(*p - '0');
|
||||
p++;
|
||||
num_digits++;
|
||||
num_decimals++;
|
||||
}
|
||||
|
||||
exponent -= num_decimals;
|
||||
}
|
||||
|
||||
if (num_digits == 0)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
number = 0.0F;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Correct for sign */
|
||||
|
||||
if (negative)
|
||||
{
|
||||
number = -number;
|
||||
}
|
||||
|
||||
/* Process an exponent string */
|
||||
|
||||
if (*p == 'e' || *p == 'E')
|
||||
{
|
||||
/* Handle optional sign */
|
||||
|
||||
negative = 0;
|
||||
switch (*++p)
|
||||
{
|
||||
case '-':
|
||||
negative = 1; /* Fall through to increment pos */
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case '+':
|
||||
p++;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Process string of digits */
|
||||
|
||||
n = 0;
|
||||
while (isdigit(*p))
|
||||
{
|
||||
n = n * 10 + (*p - '0');
|
||||
p++;
|
||||
}
|
||||
|
||||
if (negative)
|
||||
{
|
||||
exponent -= n;
|
||||
}
|
||||
else
|
||||
{
|
||||
exponent += n;
|
||||
}
|
||||
}
|
||||
|
||||
if (exponent < __FLT_MIN_EXP__ ||
|
||||
exponent > __FLT_MAX_EXP__)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
number = infinite;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Scale the result */
|
||||
|
||||
p10 = 10.0F;
|
||||
n = exponent;
|
||||
if (n < 0)
|
||||
{
|
||||
n = -n;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
if (n & 1)
|
||||
{
|
||||
if (exponent < 0)
|
||||
{
|
||||
number /= p10;
|
||||
}
|
||||
else
|
||||
{
|
||||
number *= p10;
|
||||
}
|
||||
}
|
||||
|
||||
n >>= 1;
|
||||
p10 *= p10;
|
||||
}
|
||||
|
||||
if (!is_real(number))
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
}
|
||||
|
||||
errout:
|
||||
if (endptr)
|
||||
{
|
||||
*endptr = p;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
121
components/libc/nuttx/libc/stdlib/lib_strtol.c
Normal file
121
components/libc/nuttx/libc/stdlib/lib_strtol.c
Normal file
|
@ -0,0 +1,121 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtol.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strtol
|
||||
*
|
||||
* Description:
|
||||
* The strtol() function converts the initial part of the string in
|
||||
* nptr to a long integer value according to the given base, which must be
|
||||
* between 2 and 36 inclusive, or be the special value 0.
|
||||
*
|
||||
* Returned Value:
|
||||
* - The converted value, if the base and number are valid
|
||||
* - 0 if an error occurs, and set errno to:
|
||||
* * EINVAL if base < 2 or base > 36
|
||||
* - LONG_MIN or LONG_MAX, of correct sign, if an overflow occurs,
|
||||
* and set errno to:
|
||||
* * ERANGE if the number cannot be represented using long
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
long strtol(FAR const char *nptr, FAR char **endptr, int base)
|
||||
{
|
||||
unsigned long accum = 0;
|
||||
long retval = 0;
|
||||
char sign = 0;
|
||||
|
||||
if (nptr)
|
||||
{
|
||||
/* Skip leading spaces */
|
||||
|
||||
lib_skipspace(&nptr);
|
||||
|
||||
/* Check for leading + or - */
|
||||
|
||||
if (*nptr == '-' || *nptr == '+')
|
||||
{
|
||||
sign = *nptr;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
/* Get the unsigned value */
|
||||
|
||||
accum = strtoul(nptr, endptr, base);
|
||||
|
||||
/* Correct the sign of the result and check for overflow */
|
||||
|
||||
if (sign == '-')
|
||||
{
|
||||
const unsigned long limit = ((unsigned long)-(LONG_MIN + 1)) + 1;
|
||||
|
||||
if (accum > limit)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
retval = LONG_MIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = (accum == limit) ? LONG_MIN : -(long)accum;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (accum > LONG_MAX)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
retval = LONG_MAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = accum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the final pointer to the unused value */
|
||||
|
||||
if (endptr)
|
||||
{
|
||||
if (sign)
|
||||
{
|
||||
if (*((*endptr) - 1) == sign)
|
||||
{
|
||||
(*endptr)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
265
components/libc/nuttx/libc/stdlib/lib_strtold.c
Normal file
265
components/libc/nuttx/libc/stdlib/lib_strtold.c
Normal file
|
@ -0,0 +1,265 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtold.c
|
||||
* Convert string to long double
|
||||
*
|
||||
* Copyright (C) 2002 Michael Ringgaard. All rights reserved.
|
||||
* Copyright (C) 2006-2007 H. Peter Anvin.
|
||||
*
|
||||
* 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. Neither the name of the project nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_DOUBLE
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* These are predefined with GCC, but could be issues for other compilers. If
|
||||
* not defined, an arbitrary big number is put in for now. These should be
|
||||
* added to nuttx/compiler for your compiler.
|
||||
*/
|
||||
|
||||
#if !defined(__LDBL_MIN_EXP__) || !defined(__LDBL_MAX_EXP__)
|
||||
# ifdef CONFIG_CPP_HAVE_WARNING
|
||||
# warning "Size of exponent is unknown"
|
||||
# endif
|
||||
# undef __LDBL_MIN_EXP__
|
||||
# define __LDBL_MIN_EXP__ (-1021)
|
||||
# undef __LDBL_MAX_EXP__
|
||||
# define __LDBL_MAX_EXP__ (1024)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int is_real(long double x)
|
||||
{
|
||||
const long double infinite = 1.0L / 0.0L;
|
||||
return (x < infinite) && (x >= -infinite);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/***************************************************(************************
|
||||
* Name: strtold
|
||||
*
|
||||
* Description:
|
||||
* Convert a string to a long double value
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
long double strtold(FAR const char *str, FAR char **endptr)
|
||||
{
|
||||
long double number;
|
||||
int exponent;
|
||||
int negative;
|
||||
FAR char *p = (FAR char *) str;
|
||||
long double p10;
|
||||
int n;
|
||||
int num_digits;
|
||||
int num_decimals;
|
||||
const long double infinite = 1.0L / 0.0L;
|
||||
|
||||
/* Skip leading whitespace */
|
||||
|
||||
while (isspace(*p))
|
||||
{
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Handle optional sign */
|
||||
|
||||
negative = 0;
|
||||
switch (*p)
|
||||
{
|
||||
case '-':
|
||||
negative = 1; /* Fall through to increment position */
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case '+':
|
||||
p++;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
number = 0.0L;
|
||||
exponent = 0;
|
||||
num_digits = 0;
|
||||
num_decimals = 0;
|
||||
|
||||
/* Process string of digits */
|
||||
|
||||
while (isdigit(*p))
|
||||
{
|
||||
number = number * 10.0L + (long double)(*p - '0');
|
||||
p++;
|
||||
num_digits++;
|
||||
}
|
||||
|
||||
/* Process decimal part */
|
||||
|
||||
if (*p == '.')
|
||||
{
|
||||
p++;
|
||||
|
||||
while (isdigit(*p))
|
||||
{
|
||||
number = number * 10.0L + (long double)(*p - '0');
|
||||
p++;
|
||||
num_digits++;
|
||||
num_decimals++;
|
||||
}
|
||||
|
||||
exponent -= num_decimals;
|
||||
}
|
||||
|
||||
if (num_digits == 0)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
number = 0.0L;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Correct for sign */
|
||||
|
||||
if (negative)
|
||||
{
|
||||
number = -number;
|
||||
}
|
||||
|
||||
/* Process an exponent string */
|
||||
|
||||
if (*p == 'e' || *p == 'E')
|
||||
{
|
||||
/* Handle optional sign */
|
||||
|
||||
negative = 0;
|
||||
switch (*++p)
|
||||
{
|
||||
case '-':
|
||||
negative = 1; /* Fall through to increment pos */
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case '+':
|
||||
p++;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Process string of digits */
|
||||
|
||||
n = 0;
|
||||
while (isdigit(*p))
|
||||
{
|
||||
n = n * 10 + (*p - '0');
|
||||
p++;
|
||||
}
|
||||
|
||||
if (negative)
|
||||
{
|
||||
exponent -= n;
|
||||
}
|
||||
else
|
||||
{
|
||||
exponent += n;
|
||||
}
|
||||
}
|
||||
|
||||
if (exponent < __LDBL_MIN_EXP__ ||
|
||||
exponent > __LDBL_MAX_EXP__)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
number = infinite;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Scale the result */
|
||||
|
||||
p10 = 10.0L;
|
||||
n = exponent;
|
||||
if (n < 0)
|
||||
{
|
||||
n = -n;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
if (n & 1)
|
||||
{
|
||||
if (exponent < 0)
|
||||
{
|
||||
number /= p10;
|
||||
}
|
||||
else
|
||||
{
|
||||
number *= p10;
|
||||
}
|
||||
}
|
||||
|
||||
n >>= 1;
|
||||
p10 *= p10;
|
||||
}
|
||||
|
||||
if (!is_real(number))
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
}
|
||||
|
||||
errout:
|
||||
if (endptr)
|
||||
{
|
||||
*endptr = p;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_HAVE_LONG_DOUBLE */
|
126
components/libc/nuttx/libc/stdlib/lib_strtoll.c
Normal file
126
components/libc/nuttx/libc/stdlib/lib_strtoll.c
Normal file
|
@ -0,0 +1,126 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtoll.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_LONG
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strtoll
|
||||
*
|
||||
* Description:
|
||||
* The strtoll() function converts the initial part of the string in
|
||||
* nptr to a long long integer value according to the given base, which
|
||||
* must be between 2 and 36 inclusive, or be the special value 0.
|
||||
*
|
||||
* Returned Value:
|
||||
* - The converted value, if the base and number are valid
|
||||
* - 0 if an error occurs, and set errno to:
|
||||
* * EINVAL if base < 2 or base > 36
|
||||
* - LLONG_MIN or LLONG_MAX, of correct sign, if an overflow occurs,
|
||||
* and set errno to:
|
||||
* * ERANGE if the number cannot be represented using long long
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
long long strtoll(FAR const char *nptr, FAR char **endptr, int base)
|
||||
{
|
||||
unsigned long long accum = 0;
|
||||
long long retval = 0;
|
||||
char sign = 0;
|
||||
|
||||
if (nptr)
|
||||
{
|
||||
/* Skip leading spaces */
|
||||
|
||||
lib_skipspace(&nptr);
|
||||
|
||||
/* Check for leading + or - */
|
||||
|
||||
if (*nptr == '-' || *nptr == '+')
|
||||
{
|
||||
sign = *nptr;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
/* Get the unsigned value */
|
||||
|
||||
accum = strtoull(nptr, endptr, base);
|
||||
|
||||
/* Correct the sign of the result and check for overflow */
|
||||
|
||||
if (sign == '-')
|
||||
{
|
||||
const unsigned long long limit =
|
||||
((unsigned long long)-(LLONG_MIN + 1)) + 1;
|
||||
|
||||
if (accum > limit)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
retval = LLONG_MIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = (accum == limit) ? LLONG_MIN : -(long long)accum;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (accum > LLONG_MAX)
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
return LLONG_MAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = accum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the final pointer to the unused value */
|
||||
|
||||
if (endptr)
|
||||
{
|
||||
if (sign)
|
||||
{
|
||||
if (*((*endptr) - 1) == sign)
|
||||
{
|
||||
(*endptr)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
#endif
|
127
components/libc/nuttx/libc/stdlib/lib_strtoul.c
Normal file
127
components/libc/nuttx/libc/stdlib/lib_strtoul.c
Normal file
|
@ -0,0 +1,127 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtoul.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strtoul
|
||||
*
|
||||
* Description:
|
||||
* The strtoul() function converts the initial part of the string in
|
||||
* nptr to a long unsigned integer value according to the given base, which
|
||||
* must be between 2 and 36 inclusive, or be the special value 0.
|
||||
*
|
||||
* Returned Value:
|
||||
* - The converted value, if the base and number are valid
|
||||
* - 0 if an error occurs, and set errno to:
|
||||
* * EINVAL if base < 2 or base > 36
|
||||
* - ULONG_MAX if an overflow occurs, and set errno to:
|
||||
* * ERANGE if the number cannot be represented using unsigned long
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base)
|
||||
{
|
||||
unsigned long accum = 0;
|
||||
unsigned long limit;
|
||||
int value;
|
||||
int last_digit;
|
||||
char sign = 0;
|
||||
|
||||
if (nptr)
|
||||
{
|
||||
/* Skip leading spaces */
|
||||
|
||||
lib_skipspace(&nptr);
|
||||
|
||||
/* Check for leading + or - already done for strtol */
|
||||
|
||||
if (*nptr == '-' || *nptr == '+')
|
||||
{
|
||||
sign = *nptr;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
/* Check for unspecified or incorrect base */
|
||||
|
||||
base = lib_checkbase(base, &nptr);
|
||||
|
||||
if (base < 0)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
accum = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
limit = ULONG_MAX / base;
|
||||
last_digit = ULONG_MAX % base;
|
||||
|
||||
/* Accumulate each "digit" */
|
||||
|
||||
while (lib_isbasedigit(*nptr, base, &value))
|
||||
{
|
||||
/* Check for overflow */
|
||||
|
||||
if (accum > limit || (accum == limit && value > last_digit))
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
accum = ULONG_MAX;
|
||||
break;
|
||||
}
|
||||
|
||||
accum = accum * base + value;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
if (sign == '-')
|
||||
{
|
||||
accum = (~accum) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the final pointer to the unused value */
|
||||
|
||||
if (endptr)
|
||||
{
|
||||
if (sign)
|
||||
{
|
||||
if (*(nptr - 1) == sign)
|
||||
{
|
||||
nptr--;
|
||||
}
|
||||
}
|
||||
|
||||
*endptr = (FAR char *)nptr;
|
||||
}
|
||||
|
||||
return accum;
|
||||
}
|
133
components/libc/nuttx/libc/stdlib/lib_strtoull.c
Normal file
133
components/libc/nuttx/libc/stdlib/lib_strtoull.c
Normal file
|
@ -0,0 +1,133 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_strtoull.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_LONG
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strtoull
|
||||
*
|
||||
* Description:
|
||||
* The strtoull() function converts the initial part of the string in
|
||||
* nptr to a long unsigned integer value according to the given base, which
|
||||
* must be between 2 and 36 inclusive, or be the special value 0.
|
||||
*
|
||||
* Returned Value:
|
||||
* - The converted value, if the base and number are valid
|
||||
* - 0 if an error occurs, and set errno to:
|
||||
* * EINVAL if base < 2 or base > 36
|
||||
* - ULLONG_MAX if an overflow occurs, and set errno to:
|
||||
* * ERANGE if the number cannot be represented using unsigned long long
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
unsigned long long strtoull(FAR const char *nptr,
|
||||
FAR char **endptr, int base)
|
||||
{
|
||||
unsigned long long accum = 0;
|
||||
unsigned long long limit;
|
||||
int value;
|
||||
int last_digit;
|
||||
char sign = 0;
|
||||
|
||||
if (nptr)
|
||||
{
|
||||
/* Skip leading spaces */
|
||||
|
||||
lib_skipspace(&nptr);
|
||||
|
||||
/* Check for leading + or - already done for strtol */
|
||||
|
||||
if (*nptr == '-' || *nptr == '+')
|
||||
{
|
||||
sign = *nptr;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
/* Check for unspecified or incorrect base */
|
||||
|
||||
base = lib_checkbase(base, &nptr);
|
||||
|
||||
if (base < 0)
|
||||
{
|
||||
set_errno(EINVAL);
|
||||
accum = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
limit = ULLONG_MAX / base;
|
||||
last_digit = ULLONG_MAX % base;
|
||||
|
||||
/* Accumulate each "digit" */
|
||||
|
||||
while (lib_isbasedigit(*nptr, base, &value))
|
||||
{
|
||||
/* Check for overflow */
|
||||
|
||||
if (accum > limit || (accum == limit && value > last_digit))
|
||||
{
|
||||
set_errno(ERANGE);
|
||||
accum = ULLONG_MAX;
|
||||
break;
|
||||
}
|
||||
|
||||
accum = accum * base + value;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
if (sign == '-')
|
||||
{
|
||||
accum = (~accum) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the final pointer to the unused value */
|
||||
|
||||
if (endptr)
|
||||
{
|
||||
if (sign)
|
||||
{
|
||||
if (*(nptr - 1) == sign)
|
||||
{
|
||||
nptr--;
|
||||
}
|
||||
}
|
||||
|
||||
*endptr = (FAR char *)nptr;
|
||||
}
|
||||
|
||||
return accum;
|
||||
}
|
||||
|
||||
#endif
|
60
components/libc/nuttx/libc/stdlib/lib_unlockpt.c
Normal file
60
components/libc/nuttx/libc/stdlib/lib_unlockpt.c
Normal file
|
@ -0,0 +1,60 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_unlockpt.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef CONFIG_PSEUDOTERM
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: unlockpt
|
||||
*
|
||||
* Description:
|
||||
* The unlockpt() function unlocks the slave pseudoterminal device
|
||||
* corresponding to the master pseudoterminal referred to by fd.
|
||||
* unlockpt() must be called before opening the slave side of a
|
||||
* pseudoterminal.
|
||||
*
|
||||
* Returned Value:
|
||||
* When successful, unlockpt() returns 0. Otherwise, it returns -1 and
|
||||
* sets errno appropriately.
|
||||
*
|
||||
* EBADF - The fd argument is not a file descriptor open for writing.
|
||||
* EINVAL - The fd argument is not associated with a master
|
||||
* pseudoterminal
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int unlockpt(int fd)
|
||||
{
|
||||
return ioctl(fd, TIOCSPTLCK, 0);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_PSEUDOTERM */
|
43
components/libc/nuttx/libc/stdlib/lib_wcstombs.c
Normal file
43
components/libc/nuttx/libc/stdlib/lib_wcstombs.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_wcstombs.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef CONFIG_LIBC_WCHAR
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: wcstombs
|
||||
****************************************************************************/
|
||||
|
||||
size_t wcstombs(FAR char *dst, FAR const wchar_t *src, size_t len)
|
||||
{
|
||||
return wcsrtombs(dst, &src, len, NULL);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_LIBC_WCHAR */
|
76
components/libc/nuttx/libc/stdlib/lib_wctomb.c
Normal file
76
components/libc/nuttx/libc/stdlib/lib_wctomb.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/stdlib/lib_wctomb.c
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* 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. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef CONFIG_LIBC_WCHAR
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: wctomb
|
||||
*
|
||||
* Description:
|
||||
* Try to represent a wide character as a multi byte
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int wctomb(FAR char *s, wchar_t wc)
|
||||
{
|
||||
if (s == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Verify that wchar is a valid single-byte character. */
|
||||
|
||||
if ((size_t) wc >= 0x100)
|
||||
{
|
||||
set_errno(EILSEQ);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*s = (char)wc;
|
||||
return 1;
|
||||
}
|
||||
#endif
|
92
components/libc/nuttx/libc/string/Kconfig
Normal file
92
components/libc/nuttx/libc/string/Kconfig
Normal file
|
@ -0,0 +1,92 @@
|
|||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see the file kconfig-language.txt in the NuttX tools repository.
|
||||
#
|
||||
|
||||
menu "errno Decode Support"
|
||||
|
||||
config LIBC_STRERROR
|
||||
bool "Enable strerror"
|
||||
default n
|
||||
---help---
|
||||
strerror() is useful because it decodes 'errno' values into a human readable
|
||||
strings. But it can also require a lot of memory. If this option is not
|
||||
selected, strerror() will still exist in the build but it will not decode error
|
||||
values. This option should be used by other logic to decide if it should use
|
||||
strerror() or not. For example, the NSH application will not use strerror()
|
||||
if this option is not selected; perror() will not use strerror() is this option
|
||||
is not selected (see also NSH_STRERROR).
|
||||
|
||||
config LIBC_STRERROR_SHORT
|
||||
bool "Use short error descriptions in strerror()"
|
||||
default n
|
||||
depends on LIBC_STRERROR
|
||||
---help---
|
||||
If this option is selected, then strerror() will use a shortened string when
|
||||
it decodes the error. Specifically, strerror() is simply use the string that
|
||||
is the common name for the error. For example, the 'errno' value of 2 will
|
||||
produce the string "No such file or directory" is LIBC_STRERROR_SHORT
|
||||
is not defined but the string "ENOENT" is LIBC_STRERROR_SHORT is defined.
|
||||
|
||||
config LIBC_PERROR_STDOUT
|
||||
bool "perror() to stdout"
|
||||
default n
|
||||
---help---
|
||||
POSIX requires that perror() provide its output on stderr. This option may
|
||||
be defined, however, to provide perror() output that is serialized with
|
||||
other stdout messages.
|
||||
|
||||
endmenu # errno Decode Support
|
||||
|
||||
menu "memcpy/memset Options"
|
||||
|
||||
config MEMCPY_VIK
|
||||
bool "Vik memcpy()"
|
||||
default n
|
||||
depends on !LIBC_ARCH_MEMCPY
|
||||
---help---
|
||||
Select this option to use the optimized memcpy() function by Daniel Vik.
|
||||
Select this option for improved performance at the expense of increased
|
||||
size. See licensing information in the top-level LICENSE file.
|
||||
|
||||
if MEMCPY_VIK
|
||||
|
||||
config MEMCPY_PRE_INC_PTRS
|
||||
bool "Pre-increment pointers"
|
||||
default n
|
||||
---help---
|
||||
Use pre-increment of pointers. Default is post increment of pointers.
|
||||
|
||||
config MEMCPY_INDEXED_COPY
|
||||
bool "Array indexing"
|
||||
default y
|
||||
---help---
|
||||
Copying data using array indexing. Using this option, disables the
|
||||
MEMCPY_PRE_INC_PTRS option.
|
||||
|
||||
config MEMCPY_64BIT
|
||||
bool "64-bit memcpy()"
|
||||
default n
|
||||
---help---
|
||||
Compiles memcpy() for architectures that support 64-bit operations
|
||||
efficiently.
|
||||
|
||||
endif # MEMCPY_VIK
|
||||
|
||||
config MEMSET_OPTSPEED
|
||||
bool "Optimize memset() for speed"
|
||||
default n
|
||||
depends on !LIBC_ARCH_MEMSET
|
||||
---help---
|
||||
Select this option to use a version of memcpy() optimized for speed.
|
||||
Default: memcpy() is optimized for size.
|
||||
|
||||
config MEMSET_64BIT
|
||||
bool "64-bit memset()"
|
||||
default n
|
||||
depends on MEMSET_OPTSPEED
|
||||
---help---
|
||||
Compiles memset() for architectures that support 64-bit operations
|
||||
efficiently.
|
||||
|
||||
endmenu # memcpy/memset Options
|
53
components/libc/nuttx/libc/string/Make.defs
Normal file
53
components/libc/nuttx/libc/string/Make.defs
Normal file
|
@ -0,0 +1,53 @@
|
|||
############################################################################
|
||||
# libs/libc/string/Make.defs
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership. The
|
||||
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance with the
|
||||
# License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
# Add the string C files to the build
|
||||
|
||||
CSRCS += lib_ffs.c lib_ffsl.c lib_ffsll.c lib_fls.c lib_flsl.c
|
||||
CSRCS += lib_flsll.c lib_isbasedigit.c lib_memset.c lib_memchr.c
|
||||
CSRCS += lib_memccpy.c lib_memcmp.c lib_memmove.c lib_memrchr.c
|
||||
CSRCS += lib_popcount.c lib_popcountl.c lib_popcountll.c
|
||||
CSRCS += lib_skipspace.c lib_stpcpy.c lib_stpncpy.c lib_strcasecmp.c
|
||||
CSRCS += lib_strcat.c lib_strchr.c lib_strcpy.c lib_strcmp.c lib_strcspn.c
|
||||
CSRCS += lib_strdup.c lib_strerror.c lib_strlen.c lib_strnlen.c
|
||||
CSRCS += lib_strncasecmp.c lib_strncat.c lib_strncmp.c lib_strncpy.c
|
||||
CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.c lib_strrchr.c
|
||||
CSRCS += lib_strspn.c lib_strstr.c lib_strtok.c lib_strtokr.c
|
||||
CSRCS += lib_strsep.c lib_strerrorr.c lib_explicit_bzero.c lib_strsignal.c
|
||||
CSRCS += lib_anbstr2cstr.c lib_ancstr2bstr.c lib_bmem2cmem.c
|
||||
CSRCS += lib_bstrnlen.c lib_cmem2bmem.c lib_nbstr2cstr.c lib_ncstr2bstr.c
|
||||
CSRCS += lib_index.c lib_rindex.c lib_strlcpy.c
|
||||
|
||||
ifneq ($(CONFIG_LIBC_ARCH_MEMCPY),y)
|
||||
ifeq ($(CONFIG_MEMCPY_VIK),y)
|
||||
CSRCS += lib_vikmemcpy.c
|
||||
else
|
||||
CSRCS += lib_memcpy.c
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIBC_LOCALE),y)
|
||||
CSRCS += lib_strcoll.c lib_strxfrm.c
|
||||
endif
|
||||
|
||||
# Add the string directory to the build
|
||||
|
||||
DEPPATH += --dep-path string
|
||||
VPATH += :string
|
54
components/libc/nuttx/libc/string/lib_anbstr2cstr.c
Normal file
54
components/libc/nuttx/libc/string/lib_anbstr2cstr.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_anbstr2cstr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libc.h"
|
||||
|
||||
#if CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *anbstr2cstr(FAR const char *src, size_t maxlen)
|
||||
{
|
||||
FAR char *dst;
|
||||
size_t len;
|
||||
|
||||
len = bstrnlen(src, maxlen);
|
||||
dst = lib_malloc(C2B(len + 1));
|
||||
if (dst)
|
||||
{
|
||||
dst[C2B(len + 1) - 1] = 0;
|
||||
bmem2cmem(dst, src, 0, len);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
#endif
|
54
components/libc/nuttx/libc/string/lib_ancstr2bstr.c
Normal file
54
components/libc/nuttx/libc/string/lib_ancstr2bstr.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_ancstr2bstr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libc.h"
|
||||
|
||||
#if CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *ancstr2bstr(FAR const char *src, size_t maxlen)
|
||||
{
|
||||
FAR char *dst;
|
||||
size_t len;
|
||||
|
||||
len = strnlen(src, maxlen);
|
||||
dst = lib_malloc(B2C(len + 1));
|
||||
if (dst)
|
||||
{
|
||||
dst[B2C(len + 1) - 1] = 0;
|
||||
cmem2bmem(dst, 0, src, len);
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
#endif
|
59
components/libc/nuttx/libc/string/lib_bmem2cmem.c
Normal file
59
components/libc/nuttx/libc/string/lib_bmem2cmem.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_bmem2cmem.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(CONFIG_ENDIAN_BIG) && CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void bmem2cmem(FAR void *dst_, FAR const void *src_, size_t rem, size_t len)
|
||||
{
|
||||
char *dst = dst_;
|
||||
const char *src = src_;
|
||||
|
||||
while (1)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 8 * rem; i < CHAR_BIT; i += 8)
|
||||
{
|
||||
if (len-- == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
*dst++ = (*src >> i) & 0xff;
|
||||
}
|
||||
|
||||
rem = 0;
|
||||
src++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
55
components/libc/nuttx/libc/string/lib_bstrnlen.c
Normal file
55
components/libc/nuttx/libc/string/lib_bstrnlen.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_bstrnlen.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(CONFIG_ENDIAN_BIG) && CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
size_t bstrnlen(FAR const char *src, size_t maxlen)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CHAR_BIT; i += 8, len++)
|
||||
{
|
||||
if (maxlen-- == 0 || ((*src >> i) & 0xff) == 0)
|
||||
{
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
src++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
63
components/libc/nuttx/libc/string/lib_cmem2bmem.c
Normal file
63
components/libc/nuttx/libc/string/lib_cmem2bmem.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_cmem2bmem.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(CONFIG_ENDIAN_BIG) && CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void cmem2bmem(FAR void *dst_, size_t rem, FAR const void *src_, size_t len)
|
||||
{
|
||||
char *dst = dst_;
|
||||
const char *src = src_;
|
||||
|
||||
while (1)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 8 * rem; i < CHAR_BIT; i += 8)
|
||||
{
|
||||
if (len-- == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (i == 8 * rem)
|
||||
{
|
||||
*dst = 0;
|
||||
}
|
||||
|
||||
*dst |= (*src++ & 0xff) << i;
|
||||
}
|
||||
|
||||
rem = 0;
|
||||
dst++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
55
components/libc/nuttx/libc/string/lib_explicit_bzero.c
Normal file
55
components/libc/nuttx/libc/string/lib_explicit_bzero.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_explicit_bzero.c
|
||||
*
|
||||
* Copyright (C) 2015,2017 Haltian Ltd. All rights reserved.
|
||||
* Author: Juha Niskanen <juha.niskanen@haltian.com>
|
||||
* Jussi Kivilinna <jussi.kivilinna@haltian.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/* memset that must not be optimized away by compiler (not even with LTO). */
|
||||
|
||||
void explicit_bzero(FAR void *s, size_t n)
|
||||
{
|
||||
static FAR void *(*FAR const volatile memset_v)(FAR void *, int, size_t) =
|
||||
&memset;
|
||||
|
||||
memset_v(s, 0, n);
|
||||
}
|
78
components/libc/nuttx/libc/string/lib_ffs.c
Normal file
78
components/libc/nuttx/libc/string/lib_ffs.c
Normal file
|
@ -0,0 +1,78 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_ffs.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NBITS (8 * sizeof(unsigned int))
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ffs
|
||||
*
|
||||
* Description:
|
||||
* The ffs() function will find the first bit set (beginning with the least
|
||||
* significant bit) in j, and return the index of that bit. Bits are
|
||||
* numbered starting at one (the least significant bit).
|
||||
*
|
||||
* Returned Value:
|
||||
* The ffs() function will return the index of the first bit set. If j is
|
||||
* 0, then ffs() will return 0.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ffs(int j)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (j != 0)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_CTZ
|
||||
/* Count trailing zeros function can be used to implement ffs. */
|
||||
|
||||
ret = __builtin_ctz(j) + 1;
|
||||
#else
|
||||
unsigned int value = (unsigned int)j;
|
||||
int bitno;
|
||||
|
||||
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
|
||||
{
|
||||
if ((value & 1) != 0)
|
||||
{
|
||||
ret = bitno;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
78
components/libc/nuttx/libc/string/lib_ffsl.c
Normal file
78
components/libc/nuttx/libc/string/lib_ffsl.c
Normal file
|
@ -0,0 +1,78 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_ffsl.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NBITS (8 * sizeof(unsigned long))
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ffsl
|
||||
*
|
||||
* Description:
|
||||
* The ffsl() function will find the first bit set (beginning with the
|
||||
* least significant bit) in j, and return the index of that bit. Bits are
|
||||
* numbered starting at one (the least significant bit).
|
||||
*
|
||||
* Returned Value:
|
||||
* The ffsl() function will return the index of the first bit set. If j is
|
||||
* 0, then ffsl() will return 0.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ffsl(long j)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (j != 0)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_CTZ
|
||||
/* Count trailing zeros function can be used to implement ffs. */
|
||||
|
||||
ret = __builtin_ctzl(j) + 1;
|
||||
#else
|
||||
unsigned long value = (unsigned long)j;
|
||||
int bitno;
|
||||
|
||||
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
|
||||
{
|
||||
if ((value & 1) != 0)
|
||||
{
|
||||
ret = bitno;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
82
components/libc/nuttx/libc/string/lib_ffsll.c
Normal file
82
components/libc/nuttx/libc/string/lib_ffsll.c
Normal file
|
@ -0,0 +1,82 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_ffsll.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NBITS (8 * sizeof(unsigned long long))
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_LONG
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ffsll
|
||||
*
|
||||
* Description:
|
||||
* The ffsll() function will find the first bit set (beginning with the
|
||||
* least significant bit) in i, and return the index of that bit. Bits are
|
||||
* numbered starting at one (the least significant bit).
|
||||
*
|
||||
* Returned Value:
|
||||
* The ffsll() function will return the index of the first bit set. If j is
|
||||
* 0, then ffsll() will return 0.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ffsll(long long j)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (j != 0)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_CTZ
|
||||
/* Count trailing zeros function can be used to implement ffs. */
|
||||
|
||||
ret = __builtin_ctzll(j) + 1;
|
||||
#else
|
||||
unsigned long long value = (unsigned long long)j;
|
||||
int bitno;
|
||||
|
||||
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
|
||||
{
|
||||
if ((value & 1) != 0)
|
||||
{
|
||||
ret = bitno;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
93
components/libc/nuttx/libc/string/lib_fls.c
Normal file
93
components/libc/nuttx/libc/string/lib_fls.c
Normal file
|
@ -0,0 +1,93 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_fls.c
|
||||
*
|
||||
* Copyright (C) 2017 Haltian Ltd. All rights reserved.
|
||||
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NBITS (8 * sizeof(unsigned int))
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: fls
|
||||
*
|
||||
* Description:
|
||||
* The fls() function will find the last bit set in value and return
|
||||
* the index of that bit. Bits are numbered starting at one (the least
|
||||
* significant bit).
|
||||
*
|
||||
* Returned Value:
|
||||
* The fls() function will return the index of the last bit set. If j is
|
||||
* 0, then fls() will return 0.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int fls(int j)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (j != 0)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_CLZ
|
||||
/* Count leading zeros function can be used to implement fls. */
|
||||
|
||||
ret = NBITS - __builtin_clz(j);
|
||||
#else
|
||||
unsigned int value = (unsigned int)j;
|
||||
int bitno;
|
||||
|
||||
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
|
||||
{
|
||||
if (value == 1)
|
||||
{
|
||||
ret = bitno;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
93
components/libc/nuttx/libc/string/lib_flsl.c
Normal file
93
components/libc/nuttx/libc/string/lib_flsl.c
Normal file
|
@ -0,0 +1,93 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_flsl.c
|
||||
*
|
||||
* Copyright (C) 2017 Haltian Ltd. All rights reserved.
|
||||
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NBITS (8 * sizeof(unsigned long))
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: flsl
|
||||
*
|
||||
* Description:
|
||||
* The flsl() function will find the last bit set in value and return
|
||||
* the index of that bit. Bits are numbered starting at one (the least
|
||||
* significant bit).
|
||||
*
|
||||
* Returned Value:
|
||||
* The flsl() function will return the index of the last bit set. If j is
|
||||
* 0, then flsl() will return 0.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int flsl(long j)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (j != 0)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_CLZ
|
||||
/* Count leading zeros function can be used to implement fls. */
|
||||
|
||||
ret = NBITS - __builtin_clzl(j);
|
||||
#else
|
||||
unsigned long value = (unsigned long)j;
|
||||
int bitno;
|
||||
|
||||
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
|
||||
{
|
||||
if (value == 1)
|
||||
{
|
||||
ret = bitno;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
97
components/libc/nuttx/libc/string/lib_flsll.c
Normal file
97
components/libc/nuttx/libc/string/lib_flsll.c
Normal file
|
@ -0,0 +1,97 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_flsll.c
|
||||
*
|
||||
* Copyright (C) 2017 Haltian Ltd. All rights reserved.
|
||||
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NBITS (8 * sizeof(unsigned long long))
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_HAVE_LONG_LONG
|
||||
|
||||
/****************************************************************************
|
||||
* Name: flsll
|
||||
*
|
||||
* Description:
|
||||
* The flsll() function will find the last bit set in value and return
|
||||
* the index of that bit. Bits are numbered starting at one (the least
|
||||
* significant bit).
|
||||
*
|
||||
* Returned Value:
|
||||
* The flsll() function will return the index of the last bit set. If j is
|
||||
* 0, then flsll() will return 0.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int flsll(long long j)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (j != 0)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_CLZ
|
||||
/* Count leading zeros function can be used to implement fls. */
|
||||
|
||||
ret = NBITS - __builtin_clzll(j);
|
||||
#else
|
||||
unsigned long long value = (unsigned long long)j;
|
||||
int bitno;
|
||||
|
||||
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
|
||||
{
|
||||
if (value == 1)
|
||||
{
|
||||
ret = bitno;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
39
components/libc/nuttx/libc/string/lib_index.c
Normal file
39
components/libc/nuttx/libc/string/lib_index.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_index.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: index
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *index(FAR const char *s, int c)
|
||||
{
|
||||
return strchr(s, c);
|
||||
}
|
87
components/libc/nuttx/libc/string/lib_isbasedigit.c
Normal file
87
components/libc/nuttx/libc/string/lib_isbasedigit.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_isbasedigit.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_isbasedigit
|
||||
*
|
||||
* Description:
|
||||
* Given an ASCII character, ch, and a base (1-36) do two
|
||||
* things: 1) Determine if ch is a valid charcter, and 2)
|
||||
* convert ch to its binary value.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
bool lib_isbasedigit(int ch, int base, int *value)
|
||||
{
|
||||
bool ret = false;
|
||||
int tmp = 0;
|
||||
|
||||
if (base <= 10)
|
||||
{
|
||||
if (ch >= '0' && ch <= base + '0' - 1)
|
||||
{
|
||||
tmp = ch - '0';
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
else if (base <= 36)
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
tmp = ch - '0';
|
||||
ret = true;
|
||||
}
|
||||
else if (ch >= 'a' && ch <= 'a' + base - 11)
|
||||
{
|
||||
tmp = ch - 'a' + 10;
|
||||
ret = true;
|
||||
}
|
||||
else if (ch >= 'A' && ch <= 'A' + base - 11)
|
||||
{
|
||||
tmp = ch - 'A' + 10;
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (value)
|
||||
{
|
||||
*value = tmp;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
75
components/libc/nuttx/libc/string/lib_memccpy.c
Normal file
75
components/libc/nuttx/libc/string/lib_memccpy.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memccpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: memccpy
|
||||
*
|
||||
* Description:
|
||||
* The memccpy() function copies bytes from memory area s2 into s1,
|
||||
* stopping after the first occurrence of byte c (converted to an unsigned
|
||||
* char) is copied, or after n bytes are copied, whichever comes first. If
|
||||
* copying takes place between objects that overlap, the behavior is
|
||||
* undefined.
|
||||
*
|
||||
* Returned Value:
|
||||
* The memccpy() function returns a pointer to the byte after the copy of c
|
||||
* in s1, or a null pointer if c was not found in the first n bytes of s2.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, size_t n)
|
||||
{
|
||||
FAR unsigned char *pout = (FAR unsigned char *)s1;
|
||||
FAR unsigned char *pin = (FAR unsigned char *)s2;
|
||||
|
||||
/* Copy at most n bytes */
|
||||
|
||||
while (n-- > 0)
|
||||
{
|
||||
/* Copy one byte */
|
||||
|
||||
*pout = *pin++;
|
||||
|
||||
/* Did we just copy the terminating byte c? */
|
||||
|
||||
if (*pout++ == (unsigned char)c)
|
||||
{
|
||||
/* Yes return a pointer to the byte after the copy of c into s1 */
|
||||
|
||||
return (FAR void *)pout;
|
||||
}
|
||||
}
|
||||
|
||||
/* C was not found in the first n bytes of s2 */
|
||||
|
||||
return NULL;
|
||||
}
|
65
components/libc/nuttx/libc/string/lib_memchr.c
Normal file
65
components/libc/nuttx/libc/string/lib_memchr.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memchr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: memchr
|
||||
*
|
||||
* Description:
|
||||
* The memchr() function locates the first occurrence of 'c' (converted to
|
||||
* an unsigned char) in the initial 'n' bytes (each interpreted as
|
||||
* unsigned char) of the object pointed to by s.
|
||||
*
|
||||
* Returned Value:
|
||||
* The memchr() function returns a pointer to the located byte, or a null
|
||||
* pointer if the byte does not occur in the object.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *memchr(FAR const void *s, int c, size_t n)
|
||||
{
|
||||
FAR const unsigned char *p = (FAR const unsigned char *)s;
|
||||
|
||||
if (s)
|
||||
{
|
||||
while (n--)
|
||||
{
|
||||
if (*p == (unsigned char)c)
|
||||
{
|
||||
return (FAR void *)p;
|
||||
}
|
||||
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
56
components/libc/nuttx/libc/string/lib_memcmp.c
Normal file
56
components/libc/nuttx/libc/string/lib_memcmp.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memcmp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_MEMCMP
|
||||
int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
|
||||
{
|
||||
unsigned char *p1 = (unsigned char *)s1;
|
||||
unsigned char *p2 = (unsigned char *)s2;
|
||||
|
||||
while (n-- > 0)
|
||||
{
|
||||
if (*p1 < *p2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (*p1 > *p2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
p1++;
|
||||
p2++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
45
components/libc/nuttx/libc/string/lib_memcpy.c
Normal file
45
components/libc/nuttx/libc/string/lib_memcpy.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memcpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: memcpy
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_MEMCPY
|
||||
FAR void *memcpy(FAR void *dest, FAR const void *src, size_t n)
|
||||
{
|
||||
FAR unsigned char *pout = (FAR unsigned char *)dest;
|
||||
FAR unsigned char *pin = (FAR unsigned char *)src;
|
||||
while (n-- > 0) *pout++ = *pin++;
|
||||
return dest;
|
||||
}
|
||||
#endif
|
62
components/libc/nuttx/libc/string/lib_memmove.c
Normal file
62
components/libc/nuttx/libc/string/lib_memmove.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memmove.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_MEMMOVE
|
||||
FAR void *memmove(FAR void *dest, FAR const void *src, size_t count)
|
||||
{
|
||||
FAR char *tmp;
|
||||
FAR char *s;
|
||||
|
||||
if (dest <= src)
|
||||
{
|
||||
tmp = (FAR char *) dest;
|
||||
s = (FAR char *) src;
|
||||
|
||||
while (count--)
|
||||
{
|
||||
*tmp++ = *s++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = (FAR char *) dest + count;
|
||||
s = (FAR char *) src + count;
|
||||
|
||||
while (count--)
|
||||
{
|
||||
*--tmp = *--s;
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
#endif
|
60
components/libc/nuttx/libc/string/lib_memrchr.c
Normal file
60
components/libc/nuttx/libc/string/lib_memrchr.c
Normal file
|
@ -0,0 +1,60 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memrchr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: memrchr
|
||||
*
|
||||
* Description:
|
||||
* The memrchr() function locates the last occurrence of 'c' (converted to
|
||||
* an unsigned char) in the initial 'n' bytes (each interpreted as
|
||||
* unsigned char) of the object pointed to by s.
|
||||
*
|
||||
* Returned Value:
|
||||
* The memrchr() function returns a pointer to the located byte, or a null
|
||||
* pointer if the byte does not occur in the object.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *memrchr(FAR const void *s, int c, size_t n)
|
||||
{
|
||||
FAR const unsigned char *p = (FAR const unsigned char *)s + n;
|
||||
|
||||
while (n--)
|
||||
{
|
||||
if (*--p == (unsigned char)c)
|
||||
{
|
||||
return (FAR void *)p;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
171
components/libc/nuttx/libc/string/lib_memset.c
Normal file
171
components/libc/nuttx/libc/string/lib_memset.c
Normal file
|
@ -0,0 +1,171 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_memset.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* Can't support CONFIG_MEMSET_64BIT if the platform does not have 64-bit
|
||||
* integer types.
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_HAVE_LONG_LONG
|
||||
# undef CONFIG_MEMSET_64BIT
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_MEMSET
|
||||
FAR void *memset(FAR void *s, int c, size_t n)
|
||||
{
|
||||
#ifdef CONFIG_MEMSET_OPTSPEED
|
||||
/* This version is optimized for speed (you could do better
|
||||
* still by exploiting processor caching or memory burst
|
||||
* knowledge.)
|
||||
*/
|
||||
|
||||
uintptr_t addr = (uintptr_t)s;
|
||||
uint16_t val16 = ((uint16_t)c << 8) | (uint16_t)c;
|
||||
uint32_t val32 = ((uint32_t)val16 << 16) | (uint32_t)val16;
|
||||
#ifdef CONFIG_MEMSET_64BIT
|
||||
uint64_t val64 = ((uint64_t)val32 << 32) | (uint64_t)val32;
|
||||
#endif
|
||||
|
||||
/* Make sure that there is something to be cleared */
|
||||
|
||||
if (n > 0)
|
||||
{
|
||||
/* Align to a 16-bit boundary */
|
||||
|
||||
if ((addr & 1) != 0)
|
||||
{
|
||||
*(FAR uint8_t *)addr = (uint8_t)c;
|
||||
addr += 1;
|
||||
n -= 1;
|
||||
}
|
||||
|
||||
/* Check if there are at least 16-bits left to be written */
|
||||
|
||||
if (n >= 2)
|
||||
{
|
||||
/* Align to a 32-bit boundary (we know that the destination
|
||||
* address is already aligned to at least a 16-bit boundary).
|
||||
*/
|
||||
|
||||
if ((addr & 3) != 0)
|
||||
{
|
||||
*(FAR uint16_t *)addr = val16;
|
||||
addr += 2;
|
||||
n -= 2;
|
||||
}
|
||||
|
||||
#ifndef CONFIG_MEMSET_64BIT
|
||||
/* Loop while there are at least 32-bits left to be written */
|
||||
|
||||
while (n >= 4)
|
||||
{
|
||||
*(FAR uint32_t *)addr = val32;
|
||||
addr += 4;
|
||||
n -= 4;
|
||||
}
|
||||
#else
|
||||
/* Check if there are at least 32-bits left to be written */
|
||||
|
||||
if (n >= 4)
|
||||
{
|
||||
/* Align to a 64-bit boundary (we know that the destination
|
||||
* address is already aligned to at least a 32-bit boundary).
|
||||
*/
|
||||
|
||||
if ((addr & 7) != 0)
|
||||
{
|
||||
*(FAR uint32_t *)addr = val32;
|
||||
addr += 4;
|
||||
n -= 4;
|
||||
}
|
||||
|
||||
/* Loop while there are at least 64-bits left to be written */
|
||||
|
||||
while (n >= 8)
|
||||
{
|
||||
*(FAR uint64_t *)addr = val64;
|
||||
addr += 8;
|
||||
n -= 8;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef CONFIG_MEMSET_64BIT
|
||||
/* We may get here with n in the range 0..7. If n >= 4, then we should
|
||||
* have 64-bit alignment.
|
||||
*/
|
||||
|
||||
if (n >= 4)
|
||||
{
|
||||
*(FAR uint32_t *)addr = val32;
|
||||
addr += 4;
|
||||
n -= 4;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* We may get here under the following conditions:
|
||||
*
|
||||
* n = 0, addr may or may not be aligned
|
||||
* n = 1, addr is aligned to at least a 16-bit boundary
|
||||
* n = 2, addr is aligned to a 32-bit boundary
|
||||
* n = 3, addr is aligned to a 32-bit boundary
|
||||
*/
|
||||
|
||||
if (n >= 2)
|
||||
{
|
||||
*(FAR uint16_t *)addr = val16;
|
||||
addr += 2;
|
||||
n -= 2;
|
||||
}
|
||||
|
||||
if (n >= 1)
|
||||
{
|
||||
*(FAR uint8_t *)addr = (uint8_t)c;
|
||||
}
|
||||
}
|
||||
#else
|
||||
/* This version is optimized for size */
|
||||
|
||||
FAR unsigned char *p = (FAR unsigned char *)s;
|
||||
while (n-- > 0) *p++ = c;
|
||||
#endif
|
||||
return s;
|
||||
}
|
||||
#endif
|
59
components/libc/nuttx/libc/string/lib_nbstr2cstr.c
Normal file
59
components/libc/nuttx/libc/string/lib_nbstr2cstr.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_nbstr2cstr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(CONFIG_ENDIAN_BIG) && CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void nbstr2cstr(FAR char *dst, FAR const char *src, size_t maxlen)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CHAR_BIT; i += 8)
|
||||
{
|
||||
if (maxlen-- == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
*dst = (*src >> i) & 0xff;
|
||||
if (*dst++ == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
src++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
67
components/libc/nuttx/libc/string/lib_ncstr2bstr.c
Normal file
67
components/libc/nuttx/libc/string/lib_ncstr2bstr.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_ncstr2bstr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(CONFIG_ENDIAN_BIG) && CHAR_BIT != 8
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void ncstr2bstr(FAR char *dst, FAR const char *src, size_t maxlen)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CHAR_BIT; i += 8)
|
||||
{
|
||||
char tmp;
|
||||
|
||||
if (maxlen-- == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (i == 0)
|
||||
{
|
||||
*dst = 0;
|
||||
}
|
||||
|
||||
tmp = *src++ & 0xff;
|
||||
if (tmp == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
*dst |= tmp << i;
|
||||
}
|
||||
|
||||
dst++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
55
components/libc/nuttx/libc/string/lib_popcount.c
Normal file
55
components/libc/nuttx/libc/string/lib_popcount.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_popcount.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: popcount
|
||||
****************************************************************************/
|
||||
|
||||
unsigned int popcount(unsigned int j)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_POPCOUNT
|
||||
return __builtin_popcount(j);
|
||||
#else
|
||||
unsigned int count = 0;
|
||||
|
||||
while (j > 0)
|
||||
{
|
||||
if ((j & 1) == 1)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
j >>= 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
#endif
|
||||
}
|
55
components/libc/nuttx/libc/string/lib_popcountl.c
Normal file
55
components/libc/nuttx/libc/string/lib_popcountl.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_popcountl.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: popcount
|
||||
****************************************************************************/
|
||||
|
||||
unsigned int popcountl(unsigned long j)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_POPCOUNT
|
||||
return __builtin_popcountl(j);
|
||||
#else
|
||||
unsigned int count = 0;
|
||||
|
||||
while (j > 0)
|
||||
{
|
||||
if ((j & 1) == 1)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
j >>= 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
#endif
|
||||
}
|
55
components/libc/nuttx/libc/string/lib_popcountll.c
Normal file
55
components/libc/nuttx/libc/string/lib_popcountll.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_popcountll.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/compiler.h>
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: popcount
|
||||
****************************************************************************/
|
||||
|
||||
unsigned int popcountll(unsigned long long j)
|
||||
{
|
||||
#ifdef CONFIG_HAVE_BUILTIN_POPCOUNT
|
||||
return __builtin_popcountll(j);
|
||||
#else
|
||||
unsigned int count = 0;
|
||||
|
||||
while (j > 0)
|
||||
{
|
||||
if ((j & 1) == 1)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
j >>= 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
#endif
|
||||
}
|
38
components/libc/nuttx/libc/string/lib_rindex.c
Normal file
38
components/libc/nuttx/libc/string/lib_rindex.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_rindex.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <strings.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: rindex
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *rindex(FAR const char *s, int c)
|
||||
{
|
||||
return strrchr(s, c);
|
||||
}
|
51
components/libc/nuttx/libc/string/lib_skipspace.c
Normal file
51
components/libc/nuttx/libc/string/lib_skipspace.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_skipspace.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lib_skipspace
|
||||
*
|
||||
* Description:
|
||||
* Skip over leading whitespace
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void lib_skipspace(const char **pptr)
|
||||
{
|
||||
const char *ptr = *pptr;
|
||||
while (isspace(*ptr)) ptr++;
|
||||
*pptr = ptr;
|
||||
}
|
52
components/libc/nuttx/libc/string/lib_stpcpy.c
Normal file
52
components/libc/nuttx/libc/string/lib_stpcpy.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_stpcpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stpcpy
|
||||
*
|
||||
* Description:
|
||||
* Copies the string pointed to by 'src' (including the terminating NUL
|
||||
* character) into the array pointed to by 'dest'.
|
||||
*
|
||||
* Returned Value:
|
||||
* The stpcpy() function returns a pointer to the terminating NUL
|
||||
* character copied into the 'dest' buffer
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ARCH_STPCPY
|
||||
FAR char *stpcpy(FAR char *dest, FAR const char *src)
|
||||
{
|
||||
while ((*dest++ = *src++) != '\0');
|
||||
return --dest;
|
||||
}
|
||||
#endif
|
92
components/libc/nuttx/libc/string/lib_stpncpy.c
Normal file
92
components/libc/nuttx/libc/string/lib_stpncpy.c
Normal file
|
@ -0,0 +1,92 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_stpncpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stpncpy
|
||||
*
|
||||
* Description:
|
||||
* Copies the string pointed to by 'src' (including the terminating NUL
|
||||
* character) into the array pointed to by 'dest'. strncpy() will not
|
||||
* copy more than 'n' bytes from 'src' to 'dest' array (including the
|
||||
* NUL terminator).
|
||||
*
|
||||
* If the array pointed to by 'src' is a string that is shorter than 'n'
|
||||
* bytes, NUL characters will be appended to the copy in the array
|
||||
* pointed to by 'dest', until 'n' bytes in all are written.
|
||||
*
|
||||
* If copying takes place between objects that overlap, the behavior is
|
||||
* undefined.
|
||||
*
|
||||
* Returned Value:
|
||||
* If a NUL character is written to the destination, the stpncpy()
|
||||
* function will return the address of the first such NUL character.
|
||||
* Otherwise, it will return &dest[n]
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STPNCPY
|
||||
FAR char *stpncpy(FAR char *dest, FAR const char *src, size_t n)
|
||||
{
|
||||
FAR char *end = dest + n; /* End of dest buffer + 1 byte */
|
||||
FAR char *ret; /* Value to be returned */
|
||||
|
||||
/* Copy up n bytes, breaking out of the loop early if a NUL terminator is
|
||||
* encountered.
|
||||
*/
|
||||
|
||||
while ((dest != end) && (*dest = *src++) != '\0')
|
||||
{
|
||||
/* Increment the 'dest' pointer only if it does not refer to the
|
||||
* NUL terminator.
|
||||
*/
|
||||
|
||||
dest++;
|
||||
}
|
||||
|
||||
/* Return the pointer to the NUL terminator (or to the end of the buffer
|
||||
* + 1).
|
||||
*/
|
||||
|
||||
ret = dest;
|
||||
|
||||
/* Pad the remainder of the array pointer to 'dest' with NULs. This
|
||||
* overwrites any previously copied NUL terminator.
|
||||
*/
|
||||
|
||||
while (dest != end)
|
||||
{
|
||||
*dest++ = '\0';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
51
components/libc/nuttx/libc/string/lib_strcasecmp.c
Normal file
51
components/libc/nuttx/libc/string/lib_strcasecmp.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcasecmp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ARCH_STRCASECMP
|
||||
int strcasecmp(FAR const char *cs, FAR const char *ct)
|
||||
{
|
||||
int result;
|
||||
for (; ; )
|
||||
{
|
||||
if ((result = (int)toupper(*cs) - (int)toupper(*ct)) != 0 || !*cs)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
cs++;
|
||||
ct++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
123
components/libc/nuttx/libc/string/lib_strcasestr.c
Normal file
123
components/libc/nuttx/libc/string/lib_strcasestr.c
Normal file
|
@ -0,0 +1,123 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcasestr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static FAR char *strcasechr(FAR const char *s, int uc)
|
||||
{
|
||||
register char ch;
|
||||
|
||||
if (s)
|
||||
{
|
||||
for (; *s; s++)
|
||||
{
|
||||
ch = *s;
|
||||
if (toupper(ch) == uc)
|
||||
{
|
||||
return (FAR char *)s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strcasestr(FAR const char *str, FAR const char *substr)
|
||||
{
|
||||
FAR const char *candidate; /* Candidate in str with matching start character */
|
||||
char ch; /* First character of the substring */
|
||||
size_t len; /* The length of the substring */
|
||||
|
||||
/* Special case the empty substring */
|
||||
|
||||
len = strlen(substr);
|
||||
ch = *substr;
|
||||
|
||||
if (!ch)
|
||||
{
|
||||
/* We'll say that an empty substring matches at the beginning of
|
||||
* the string
|
||||
*/
|
||||
|
||||
return (FAR char *)str;
|
||||
}
|
||||
|
||||
/* Search for the substring */
|
||||
|
||||
candidate = str;
|
||||
ch = toupper(ch);
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
/* strcasechr() will return a pointer to the next occurrence of the
|
||||
* character ch in the string (ignoring case)
|
||||
*/
|
||||
|
||||
candidate = strcasechr(candidate, ch);
|
||||
if (!candidate || strlen(candidate) < len)
|
||||
{
|
||||
/* First character of the substring does not appear in the string
|
||||
* or the remainder of the string is not long enough to contain the
|
||||
* substring.
|
||||
*/
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Check if this is the beginning of a matching substring
|
||||
* (ignoring case)
|
||||
*/
|
||||
|
||||
if (strncasecmp(candidate, substr, len) == 0)
|
||||
{
|
||||
/* Yes.. return the pointer to the first occurrence of the matching
|
||||
* substring.
|
||||
*/
|
||||
|
||||
return (FAR char *)candidate;
|
||||
}
|
||||
|
||||
/* No, find the next candidate after this one */
|
||||
|
||||
candidate++;
|
||||
}
|
||||
|
||||
/* Won't get here, but some compilers might complain. Others might
|
||||
* complain about this code being unreachable too.
|
||||
*/
|
||||
|
||||
return NULL;
|
||||
}
|
48
components/libc/nuttx/libc/string/lib_strcat.c
Normal file
48
components/libc/nuttx/libc/string/lib_strcat.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcat.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ARCH_STRCAT
|
||||
char *strcat(char *dest, const char *src)
|
||||
{
|
||||
char *ret = dest;
|
||||
|
||||
dest += strlen(dest);
|
||||
while (*src != '\0')
|
||||
{
|
||||
*dest++ = *src++;
|
||||
}
|
||||
|
||||
*dest = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
68
components/libc/nuttx/libc/string/lib_strchr.c
Normal file
68
components/libc/nuttx/libc/string/lib_strchr.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strchr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strchr
|
||||
*
|
||||
* Description:
|
||||
* The strchr() function locates the first occurrence of 'c' (converted to
|
||||
* a char) in the string pointed to by 's'. The terminating null byte is
|
||||
* considered to be part of the string.
|
||||
*
|
||||
* Returned Value:
|
||||
* Upon completion, strchr() returns a pointer to the byte, or a null
|
||||
* pointer if the byte was not found.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRCHR
|
||||
FAR char *strchr(FAR const char *s, int c)
|
||||
{
|
||||
if (s)
|
||||
{
|
||||
for (; ; s++)
|
||||
{
|
||||
if (*s == c)
|
||||
{
|
||||
return (FAR char *)s;
|
||||
}
|
||||
|
||||
if (!*s)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
45
components/libc/nuttx/libc/string/lib_strcmp.c
Normal file
45
components/libc/nuttx/libc/string/lib_strcmp.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcmp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRCMP
|
||||
int strcmp(FAR const char *cs, FAR const char *ct)
|
||||
{
|
||||
register signed char result;
|
||||
for (; ; )
|
||||
{
|
||||
if ((result = *cs - *ct++) != 0 || !*cs++)
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
59
components/libc/nuttx/libc/string/lib_strcoll.c
Normal file
59
components/libc/nuttx/libc/string/lib_strcoll.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcoll.c
|
||||
*
|
||||
* Copyright (c)1999 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef CONFIG_LIBC_LOCALE
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strcoll
|
||||
*
|
||||
* Description:
|
||||
* The strcoll() compares the string pointed to by a to the string pointed
|
||||
* to by b, using an interpretation appropriate to the current
|
||||
* LC_COLLATE state. Current implementation doesn't care about locale.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int strcoll(const char *a, const char *b)
|
||||
{
|
||||
return strcmp(a, b);
|
||||
}
|
||||
#endif
|
52
components/libc/nuttx/libc/string/lib_strcpy.c
Normal file
52
components/libc/nuttx/libc/string/lib_strcpy.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strcpy
|
||||
*
|
||||
* Description:
|
||||
* Copies the string pointed to by 'src' (including the terminating NUL
|
||||
* character) into the array pointed to by 'des'.
|
||||
*
|
||||
* Returned Value:
|
||||
* The strcpy() function returns the 'dest' pointer
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRCPY
|
||||
FAR char *strcpy(FAR char *dest, FAR const char *src)
|
||||
{
|
||||
char *tmp = dest;
|
||||
while ((*dest++ = *src++) != '\0');
|
||||
return tmp;
|
||||
}
|
||||
#endif
|
47
components/libc/nuttx/libc/string/lib_strcspn.c
Normal file
47
components/libc/nuttx/libc/string/lib_strcspn.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strcspn.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strcspn
|
||||
*
|
||||
* Description:
|
||||
* strcspn() calculates the length of the initial segment of s which
|
||||
* consists entirely of characters not in reject.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
size_t strcspn(const char *s, const char *reject)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; s[i] && strchr(reject, s[i]) == NULL; i++);
|
||||
return i;
|
||||
}
|
46
components/libc/nuttx/libc/string/lib_strdup.c
Normal file
46
components/libc/nuttx/libc/string/lib_strdup.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strdup.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strdup(FAR const char *s)
|
||||
{
|
||||
FAR char *news = NULL;
|
||||
if (s)
|
||||
{
|
||||
news = (FAR char *)lib_malloc(strlen(s) + 1);
|
||||
if (news)
|
||||
{
|
||||
strcpy(news, s);
|
||||
}
|
||||
}
|
||||
|
||||
return news;
|
||||
}
|
370
components/libc/nuttx/libc/string/lib_strerror.c
Normal file
370
components/libc/nuttx/libc/string/lib_strerror.c
Normal file
|
@ -0,0 +1,370 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strerror.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct errno_strmap_s
|
||||
{
|
||||
uint8_t errnum;
|
||||
const char *str;
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_LIBC_STRERROR
|
||||
|
||||
/* This table maps all error numbers to descriptive strings.
|
||||
* The only assumption that the code makes with regard to this
|
||||
* this table is that it is ordered by error number.
|
||||
*
|
||||
* The size of this table is quite large. Its size can be
|
||||
* reduced by eliminating some of the more obscure error
|
||||
* strings.
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_LIBC_STRERROR_SHORT
|
||||
|
||||
static const struct errno_strmap_s g_errnomap[] =
|
||||
{
|
||||
{ 0, "Success" },
|
||||
{ EPERM, EPERM_STR },
|
||||
{ ENOENT, ENOENT_STR },
|
||||
{ ESRCH, ESRCH_STR },
|
||||
{ EINTR, EINTR_STR },
|
||||
{ EIO, EIO_STR },
|
||||
{ ENXIO, ENXIO_STR },
|
||||
{ E2BIG, E2BIG_STR },
|
||||
{ ENOEXEC, ENOEXEC_STR },
|
||||
{ EBADF, EBADF_STR },
|
||||
{ ECHILD, ECHILD_STR },
|
||||
{ EAGAIN, EAGAIN_STR },
|
||||
{ ENOMEM, ENOMEM_STR },
|
||||
{ EACCES, EACCES_STR },
|
||||
{ EFAULT, EFAULT_STR },
|
||||
{ ENOTBLK, ENOTBLK_STR },
|
||||
{ EBUSY, EBUSY_STR },
|
||||
{ EEXIST, EEXIST_STR },
|
||||
{ EXDEV, EXDEV_STR },
|
||||
{ ENODEV, ENODEV_STR },
|
||||
{ ENOTDIR, ENOTDIR_STR },
|
||||
{ EISDIR, EISDIR_STR },
|
||||
{ EINVAL, EINVAL_STR },
|
||||
{ ENFILE, ENFILE_STR },
|
||||
{ EMFILE, EMFILE_STR },
|
||||
{ ENOTTY, ENOTTY_STR },
|
||||
{ ETXTBSY, ETXTBSY_STR },
|
||||
{ EFBIG, EFBIG_STR },
|
||||
{ ENOSPC, ENOSPC_STR },
|
||||
{ ESPIPE, ESPIPE_STR },
|
||||
{ EROFS, EROFS_STR },
|
||||
{ EMLINK, EMLINK_STR },
|
||||
{ EPIPE, EPIPE_STR },
|
||||
{ EDOM, EDOM_STR },
|
||||
{ ERANGE, ERANGE_STR },
|
||||
{ ENOMSG, ENOMSG_STR },
|
||||
{ EIDRM, EIDRM_STR },
|
||||
{ ECHRNG, ECHRNG_STR },
|
||||
{ EL2NSYNC, EL2NSYNC_STR },
|
||||
{ EL3HLT, EL3HLT_STR },
|
||||
{ EL3RST, EL3RST_STR },
|
||||
{ ELNRNG, ELNRNG_STR },
|
||||
{ EUNATCH, EUNATCH_STR },
|
||||
{ ENOCSI, ENOCSI_STR },
|
||||
{ EL2HLT, EL2HLT_STR },
|
||||
{ EDEADLK, EDEADLK_STR },
|
||||
{ ENOLCK, ENOLCK_STR },
|
||||
{ EBADE, EBADE_STR },
|
||||
{ EBADR, EBADR_STR },
|
||||
{ EXFULL, EXFULL_STR },
|
||||
{ ENOANO, ENOANO_STR },
|
||||
{ EBADRQC, EBADRQC_STR },
|
||||
{ EBADSLT, EBADSLT_STR },
|
||||
{ EDEADLOCK, EDEADLOCK_STR },
|
||||
{ EBFONT, EBFONT_STR },
|
||||
{ ENOSTR, ENOSTR_STR },
|
||||
{ ENODATA, ENODATA_STR },
|
||||
{ ETIME, ETIME_STR },
|
||||
{ ENOSR, ENOSR_STR },
|
||||
{ ENONET, ENONET_STR },
|
||||
{ ENOPKG, ENOPKG_STR },
|
||||
{ EREMOTE, EREMOTE_STR },
|
||||
{ ENOLINK, ENOLINK_STR },
|
||||
{ EADV, EADV_STR },
|
||||
{ ESRMNT, ESRMNT_STR },
|
||||
{ ECOMM, ECOMM_STR },
|
||||
{ EPROTO, EPROTO_STR },
|
||||
{ EMULTIHOP, EMULTIHOP_STR },
|
||||
{ ELBIN, ELBIN_STR },
|
||||
{ EDOTDOT, EDOTDOT_STR },
|
||||
{ EBADMSG, EBADMSG_STR },
|
||||
{ EFTYPE, EFTYPE_STR },
|
||||
{ ENOTUNIQ, ENOTUNIQ_STR },
|
||||
{ EBADFD, EBADFD_STR },
|
||||
{ EREMCHG, EREMCHG_STR },
|
||||
{ ELIBACC, ELIBACC_STR },
|
||||
{ ELIBBAD, ELIBBAD_STR },
|
||||
{ ELIBSCN, ELIBSCN_STR },
|
||||
{ ELIBMAX, ELIBMAX_STR },
|
||||
{ ELIBEXEC, ELIBEXEC_STR },
|
||||
{ ENOSYS, ENOSYS_STR },
|
||||
{ ENMFILE, ENMFILE_STR },
|
||||
{ ENOTEMPTY, ENOTEMPTY_STR },
|
||||
{ ENAMETOOLONG, ENAMETOOLONG_STR },
|
||||
{ ELOOP, ELOOP_STR },
|
||||
{ EOPNOTSUPP, EOPNOTSUPP_STR },
|
||||
{ EPFNOSUPPORT, EPFNOSUPPORT_STR },
|
||||
{ ECONNRESET, ECONNRESET_STR },
|
||||
{ ENOBUFS, ENOBUFS_STR },
|
||||
{ EAFNOSUPPORT, EAFNOSUPPORT_STR },
|
||||
{ EPROTOTYPE, EPROTOTYPE_STR },
|
||||
{ ENOTSOCK, ENOTSOCK_STR },
|
||||
{ ENOPROTOOPT, ENOPROTOOPT_STR },
|
||||
{ ESHUTDOWN, ESHUTDOWN_STR },
|
||||
{ ECONNREFUSED, ECONNREFUSED_STR },
|
||||
{ EADDRINUSE, EADDRINUSE_STR },
|
||||
{ ECONNABORTED, ECONNABORTED_STR },
|
||||
{ ENETUNREACH, ENETUNREACH_STR },
|
||||
{ ENETDOWN, ENETDOWN_STR },
|
||||
{ ETIMEDOUT, ETIMEDOUT_STR },
|
||||
{ EHOSTDOWN, EHOSTDOWN_STR },
|
||||
{ EHOSTUNREACH, EHOSTUNREACH_STR },
|
||||
{ EINPROGRESS, EINPROGRESS_STR },
|
||||
{ EALREADY, EALREADY_STR },
|
||||
{ EDESTADDRREQ, EDESTADDRREQ_STR },
|
||||
{ EMSGSIZE, EMSGSIZE_STR },
|
||||
{ EPROTONOSUPPORT, EPROTONOSUPPORT_STR },
|
||||
{ ESOCKTNOSUPPORT, ESOCKTNOSUPPORT_STR },
|
||||
{ EADDRNOTAVAIL, EADDRNOTAVAIL_STR },
|
||||
{ ENETRESET, ENETRESET_STR },
|
||||
{ EISCONN, EISCONN_STR },
|
||||
{ ENOTCONN, ENOTCONN_STR },
|
||||
{ ETOOMANYREFS, ETOOMANYREFS_STR },
|
||||
{ EPROCLIM, EPROCLIM_STR },
|
||||
{ EUSERS, EUSERS_STR },
|
||||
{ EDQUOT, EDQUOT_STR },
|
||||
{ ESTALE, ESTALE_STR },
|
||||
{ ENOTSUP, ENOTSUP_STR },
|
||||
{ ENOMEDIUM, ENOMEDIUM_STR },
|
||||
{ ENOSHARE, ENOSHARE_STR },
|
||||
{ ECASECLASH, ECASECLASH_STR },
|
||||
{ EILSEQ, EILSEQ_STR },
|
||||
{ EOVERFLOW, EOVERFLOW_STR },
|
||||
{ ECANCELED, ECANCELED_STR },
|
||||
{ ENOTRECOVERABLE, ENOTRECOVERABLE_STR },
|
||||
{ EOWNERDEAD, EOWNERDEAD_STR },
|
||||
{ ESTRPIPE, ESTRPIPE_STR }
|
||||
};
|
||||
|
||||
#else /* CONFIG_LIBC_STRERROR_SHORT */
|
||||
|
||||
static const struct errno_strmap_s g_errnomap[] =
|
||||
{
|
||||
{ 0, "OK" },
|
||||
{ EPERM, "EPERM" },
|
||||
{ ENOENT, "ENOENT" },
|
||||
{ ESRCH, "ESRCH" },
|
||||
{ EINTR, "EINTR" },
|
||||
{ EIO, "EIO" },
|
||||
{ ENXIO, "ENXIO" },
|
||||
{ E2BIG, "E2BIG" },
|
||||
{ ENOEXEC, "ENOEXEC" },
|
||||
{ EBADF, "EBADF" },
|
||||
{ ECHILD, "ECHILD" },
|
||||
{ EAGAIN, "EAGAIN" },
|
||||
{ ENOMEM, "ENOMEM" },
|
||||
{ EACCES, "EACCES" },
|
||||
{ EFAULT, "EFAULT" },
|
||||
{ ENOTBLK, "ENOTBLK" },
|
||||
{ EBUSY, "EBUSY" },
|
||||
{ EEXIST, "EEXIST" },
|
||||
{ EXDEV, "EXDEV" },
|
||||
{ ENODEV, "ENODEV" },
|
||||
{ ENOTDIR, "ENOTDIR" },
|
||||
{ EISDIR, "EISDIR" },
|
||||
{ EINVAL, "EINVAL" },
|
||||
{ ENFILE, "ENFILE" },
|
||||
{ EMFILE, "EMFILE" },
|
||||
{ ENOTTY, "ENOTTY" },
|
||||
{ ETXTBSY, "ETXTBSY" },
|
||||
{ EFBIG, "EFBIG" },
|
||||
{ ENOSPC, "ENOSPC" },
|
||||
{ ESPIPE, "ESPIPE" },
|
||||
{ EROFS, "EROFS" },
|
||||
{ EMLINK, "EMLINK" },
|
||||
{ EPIPE, "EPIPE" },
|
||||
{ EDOM, "EDOM" },
|
||||
{ ERANGE, "ERANGE" },
|
||||
{ ENOMSG, "ENOMSG" },
|
||||
{ EIDRM, "EIDRM" },
|
||||
{ ECHRNG, "ECHRNG" },
|
||||
{ EL2NSYNC, "EL2NSYNC" },
|
||||
{ EL3HLT, "EL3HLT" },
|
||||
{ EL3RST, "EL3RST" },
|
||||
{ ELNRNG, "ELNRNG" },
|
||||
{ EUNATCH, "EUNATCH" },
|
||||
{ ENOCSI, "ENOCSI" },
|
||||
{ EL2HLT, "EL2HLT" },
|
||||
{ EDEADLK, "EDEADLK" },
|
||||
{ ENOLCK, "ENOLCK" },
|
||||
{ EBADE, "EBADE" },
|
||||
{ EBADR, "EBADR" },
|
||||
{ EXFULL, "EXFULL" },
|
||||
{ ENOANO, "ENOANO" },
|
||||
{ EBADRQC, "EBADRQC" },
|
||||
{ EBADSLT, "EBADSLT" },
|
||||
{ EDEADLOCK, "EDEADLOCK" },
|
||||
{ EBFONT, "EBFONT" },
|
||||
{ ENOSTR, "ENOSTR" },
|
||||
{ ENODATA, "ENODATA" },
|
||||
{ ETIME, "ETIME" },
|
||||
{ ENOSR, "ENOSR" },
|
||||
{ ENONET, "ENONET" },
|
||||
{ ENOPKG, "ENOPKG" },
|
||||
{ EREMOTE, "EREMOTE" },
|
||||
{ ENOLINK, "ENOLINK" },
|
||||
{ EADV, "EADV" },
|
||||
{ ESRMNT, "ESRMNT" },
|
||||
{ ECOMM, "ECOMM" },
|
||||
{ EPROTO, "EPROTO" },
|
||||
{ EMULTIHOP, "EMULTIHOP" },
|
||||
{ ELBIN, "ELBIN" },
|
||||
{ EDOTDOT, "EDOTDOT" },
|
||||
{ EBADMSG, "EBADMSG" },
|
||||
{ EFTYPE, "EFTYPE" },
|
||||
{ ENOTUNIQ, "ENOTUNIQ" },
|
||||
{ EBADFD, "EBADFD" },
|
||||
{ EREMCHG, "EREMCHG" },
|
||||
{ ELIBACC, "ELIBACC" },
|
||||
{ ELIBBAD, "ELIBBAD" },
|
||||
{ ELIBSCN, "ELIBSCN" },
|
||||
{ ELIBMAX, "ELIBMAX" },
|
||||
{ ELIBEXEC, "ELIBEXEC" },
|
||||
{ ENOSYS, "ENOSYS" },
|
||||
{ ENMFILE, "ENMFILE" },
|
||||
{ ENOTEMPTY, "ENOTEMPTY" },
|
||||
{ ENAMETOOLONG, "ENAMETOOLONG" },
|
||||
{ ELOOP, "ELOOP" },
|
||||
{ EOPNOTSUPP, "EOPNOTSUPP" },
|
||||
{ EPFNOSUPPORT, "EPFNOSUPPORT" },
|
||||
{ ECONNRESET, "ECONNRESET" },
|
||||
{ ENOBUFS, "ENOBUFS" },
|
||||
{ EAFNOSUPPORT, "EAFNOSUPPORT" },
|
||||
{ EPROTOTYPE, "EPROTOTYPE" },
|
||||
{ ENOTSOCK, "ENOTSOCK" },
|
||||
{ ENOPROTOOPT, "ENOPROTOOPT" },
|
||||
{ ESHUTDOWN, "ESHUTDOWN" },
|
||||
{ ECONNREFUSED, "ECONNREFUSED" },
|
||||
{ EADDRINUSE, "EADDRINUSE" },
|
||||
{ ECONNABORTED, "ECONNABORTED" },
|
||||
{ ENETUNREACH, "ENETUNREACH" },
|
||||
{ ENETDOWN, "ENETDOWN" },
|
||||
{ ETIMEDOUT, "ETIMEDOUT" },
|
||||
{ EHOSTDOWN, "EHOSTDOWN" },
|
||||
{ EHOSTUNREACH, "EHOSTUNREACH" },
|
||||
{ EINPROGRESS, "EINPROGRESS" },
|
||||
{ EALREADY, "EALREADY" },
|
||||
{ EDESTADDRREQ, "EDESTADDRREQ" },
|
||||
{ EMSGSIZE, "EMSGSIZE" },
|
||||
{ EPROTONOSUPPORT, "EPROTONOSUPPORT" },
|
||||
{ ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT" },
|
||||
{ EADDRNOTAVAIL, "EADDRNOTAVAIL" },
|
||||
{ ENETRESET, "ENETRESET" },
|
||||
{ EISCONN, "EISCONN" },
|
||||
{ ENOTCONN, "ENOTCONN" },
|
||||
{ ETOOMANYREFS, "ETOOMANYREFS" },
|
||||
{ EPROCLIM, "EPROCLIM" },
|
||||
{ EUSERS, "EUSERS" },
|
||||
{ EDQUOT, "EDQUOT" },
|
||||
{ ESTALE, "ESTALE" },
|
||||
{ ENOTSUP, "ENOTSUP" },
|
||||
{ ENOMEDIUM, "ENOMEDIUM" },
|
||||
{ ENOSHARE, "ENOSHARE" },
|
||||
{ ECASECLASH, "ECASECLASH" },
|
||||
{ EILSEQ, "EILSEQ" },
|
||||
{ EOVERFLOW, "EOVERFLOW" },
|
||||
{ ECANCELED, "ECANCELED" },
|
||||
{ ENOTRECOVERABLE, "ENOTRECOVERABLE" },
|
||||
{ EOWNERDEAD, "EOWNERDEAD" },
|
||||
{ ESTRPIPE, "ESTRPIPE" }
|
||||
};
|
||||
|
||||
#endif /* CONFIG_LIBC_STRERROR_SHORT */
|
||||
|
||||
#define NERRNO_STRS (sizeof(g_errnomap) / sizeof(struct errno_strmap_s))
|
||||
|
||||
#endif /* CONFIG_LIBC_STRERROR */
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strerror
|
||||
****************************************************************************/
|
||||
|
||||
FAR const char *strerror(int errnum)
|
||||
{
|
||||
#ifdef CONFIG_LIBC_STRERROR
|
||||
int ndxlow = 0;
|
||||
int ndxhi = NERRNO_STRS - 1;
|
||||
int ndxmid;
|
||||
|
||||
do
|
||||
{
|
||||
ndxmid = (ndxlow + ndxhi) >> 1;
|
||||
if (errnum > g_errnomap[ndxmid].errnum)
|
||||
{
|
||||
ndxlow = ndxmid + 1;
|
||||
}
|
||||
else if (errnum < g_errnomap[ndxmid].errnum)
|
||||
{
|
||||
ndxhi = ndxmid - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return g_errnomap[ndxmid].str;
|
||||
}
|
||||
}
|
||||
while (ndxlow <= ndxhi);
|
||||
#endif
|
||||
return "Unknown error";
|
||||
}
|
59
components/libc/nuttx/libc/string/lib_strerrorr.c
Normal file
59
components/libc/nuttx/libc/string/lib_strerrorr.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strerrorr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strerror_r
|
||||
*
|
||||
* Description:
|
||||
* The strerror_r() function is similar to strerror(), but is thread safe.
|
||||
* It returns the error string in the user-supplied buffer 'buf' of length
|
||||
* 'buflen'.
|
||||
*
|
||||
* Returned Value:
|
||||
* strerror_r() returns 0 on success. On error, a (positive) error number is
|
||||
* returned.
|
||||
*
|
||||
* Portability:
|
||||
* Specified in POSIX.1-2001
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int strerror_r(int errnum, FAR char *buf, size_t buflen)
|
||||
{
|
||||
FAR const char *errstr = strerror(errnum);
|
||||
|
||||
DEBUGASSERT(buf != NULL);
|
||||
strncpy(buf, errstr, buflen);
|
||||
return OK;
|
||||
}
|
75
components/libc/nuttx/libc/string/lib_strlcpy.c
Normal file
75
components/libc/nuttx/libc/string/lib_strlcpy.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strlcpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strlcpy
|
||||
*
|
||||
* Description:
|
||||
* Copy src to string dst of size dsize. At most dsize-1 characters
|
||||
* will be copied. Always NUL terminates (unless dsize == 0).
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns strlen(src); if retval >= dsize, truncation occurred.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRLCPY
|
||||
size_t strlcpy(FAR char *dst, FAR const char *src, size_t dsize)
|
||||
{
|
||||
FAR const char *osrc = src;
|
||||
size_t nleft = dsize;
|
||||
|
||||
if (nleft != 0)
|
||||
{
|
||||
while (--nleft != 0)
|
||||
{
|
||||
if ((*dst++ = *src++) == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nleft == 0)
|
||||
{
|
||||
if (dsize != 0)
|
||||
{
|
||||
*dst = '\0';
|
||||
}
|
||||
|
||||
while (*src++);
|
||||
}
|
||||
|
||||
return (src - osrc - 1);
|
||||
}
|
||||
#endif
|
40
components/libc/nuttx/libc/string/lib_strlen.c
Normal file
40
components/libc/nuttx/libc/string/lib_strlen.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strlen.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRLEN
|
||||
size_t strlen(const char *s)
|
||||
{
|
||||
const char *sc;
|
||||
for (sc = s; *sc != '\0'; ++sc);
|
||||
return sc - s;
|
||||
}
|
||||
#endif
|
52
components/libc/nuttx/libc/string/lib_strncasecmp.c
Normal file
52
components/libc/nuttx/libc/string/lib_strncasecmp.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strncasecmp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ARCH_STRNCASECMP
|
||||
int strncasecmp(const char *cs, const char *ct, size_t nb)
|
||||
{
|
||||
int result = 0;
|
||||
for (; nb > 0; nb--)
|
||||
{
|
||||
if ((result = (int)toupper(*cs) - (int)toupper(*ct)) != 0 || !*cs)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
cs++;
|
||||
ct++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
48
components/libc/nuttx/libc/string/lib_strncat.c
Normal file
48
components/libc/nuttx/libc/string/lib_strncat.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strncat.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ARCH_STRNCAT
|
||||
char *strncat(char *dest, const char *src, size_t n)
|
||||
{
|
||||
char *ret = dest;
|
||||
|
||||
dest += strlen(dest);
|
||||
for (; n > 0 && *src != '\0' ; n--)
|
||||
{
|
||||
*dest++ = *src++;
|
||||
}
|
||||
|
||||
*dest = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
47
components/libc/nuttx/libc/string/lib_strncmp.c
Normal file
47
components/libc/nuttx/libc/string/lib_strncmp.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strncmp.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ARCH_STRNCMP
|
||||
int strncmp(const char *cs, const char *ct, size_t nb)
|
||||
{
|
||||
int result = 0;
|
||||
for (; nb > 0; nb--)
|
||||
{
|
||||
if ((result = (int)*cs - (int)*ct++) != 0 || !*cs++)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
79
components/libc/nuttx/libc/string/lib_strncpy.c
Normal file
79
components/libc/nuttx/libc/string/lib_strncpy.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strncpy.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strncpy
|
||||
*
|
||||
* Description:
|
||||
* Copies the string pointed to by 'src' (including the terminating NUL
|
||||
* character) into the array pointed to by 'dest'. strncpy() will not
|
||||
* copy more than 'n' bytes from 'src' to 'dest' array (including the
|
||||
* NUL terminator).
|
||||
*
|
||||
* If the array pointed to by 'src' is a string that is shorter than 'n'
|
||||
* bytes, NUL characters will be appended to the copy in the array
|
||||
* pointed to by 'dest', until 'n' bytes in all are written.
|
||||
*
|
||||
* If copying takes place between objects that overlap, the behavior is
|
||||
* undefined.
|
||||
*
|
||||
* Returned Value:
|
||||
* The strncpy() function returns the pointer to 'dest'
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRNCPY
|
||||
FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
|
||||
{
|
||||
FAR char *ret = dest; /* Value to be returned */
|
||||
FAR char *end = dest + n; /* End of dest buffer + 1 byte */
|
||||
|
||||
/* Copy up n bytes, breaking out of the loop early if a NUL terminator is
|
||||
* encountered.
|
||||
*/
|
||||
|
||||
while ((dest != end) && (*dest++ = *src++) != '\0')
|
||||
{
|
||||
}
|
||||
|
||||
/* Note that there may be no NUL terminator in 'dest' */
|
||||
|
||||
/* Pad the remainder of the array pointer to 'dest' with NULs */
|
||||
|
||||
while (dest != end)
|
||||
{
|
||||
*dest++ = '\0';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
76
components/libc/nuttx/libc/string/lib_strndup.c
Normal file
76
components/libc/nuttx/libc/string/lib_strndup.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strndup.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "libc.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strndup
|
||||
*
|
||||
* Description:
|
||||
* The strndup() function is equivalent to the strdup() function,
|
||||
* duplicating the provided 's' in a new block of memory allocated as
|
||||
* if by using malloc(), with the exception being that strndup() copies
|
||||
* at most 'size' plus one bytes into the newly allocated memory,
|
||||
* terminating the new string with a NUL character. If the length of 's'
|
||||
* is larger than 'size', only 'size' bytes will be duplicated. If
|
||||
* 'size' is larger than the length of 's', all bytes in s will be
|
||||
* copied into the new memory buffer, including the terminating NUL
|
||||
* character. The newly created string will always be properly
|
||||
* terminated.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strndup(FAR const char *s, size_t size)
|
||||
{
|
||||
FAR char *news = NULL;
|
||||
if (s)
|
||||
{
|
||||
/* Get the size of the new string (limited to size) */
|
||||
|
||||
size_t allocsize = strnlen(s, size);
|
||||
|
||||
/* Allocate the new string, adding 1 for the NUL terminator */
|
||||
|
||||
news = (FAR char *)lib_malloc(allocsize + 1);
|
||||
if (news)
|
||||
{
|
||||
/* Copy the string into the allocated memory and add a NUL
|
||||
* terminator in any case.
|
||||
*/
|
||||
|
||||
memcpy(news, s, allocsize);
|
||||
news[allocsize] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return news;
|
||||
}
|
62
components/libc/nuttx/libc/string/lib_strnlen.c
Normal file
62
components/libc/nuttx/libc/string/lib_strnlen.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strnlen.c
|
||||
*
|
||||
* This file is part of NuttX, contributed by Michael Hrabanek
|
||||
*
|
||||
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
|
||||
* Author: Michael Hrabanek
|
||||
*
|
||||
* Derives from the file libs/libc/lib_strlen.c:
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2010 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_LIBC_ARCH_STRNLEN
|
||||
size_t strnlen(const char *s, size_t maxlen)
|
||||
{
|
||||
const char *sc;
|
||||
for (sc = s; maxlen != 0 && *sc != '\0'; maxlen--, ++sc);
|
||||
return sc - s;
|
||||
}
|
||||
#endif
|
71
components/libc/nuttx/libc/string/lib_strpbrk.c
Normal file
71
components/libc/nuttx/libc/string/lib_strpbrk.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strpbrk.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strpbrk(FAR const char *str, FAR const char *charset)
|
||||
{
|
||||
/* Sanity checking */
|
||||
|
||||
#ifdef CONFIG_DEBUG_FEATURES
|
||||
if (!str || !charset)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Check each character in the string */
|
||||
|
||||
while (*str)
|
||||
{
|
||||
/* Check if the character from the string matches any character in the
|
||||
* charset
|
||||
*/
|
||||
|
||||
if (strchr(charset, *str) != NULL)
|
||||
{
|
||||
/* Yes, then this position must be the first occurrence in string */
|
||||
|
||||
return (FAR char *)str;
|
||||
}
|
||||
|
||||
/* This character from the strings matches none of those in the
|
||||
* charset. Try the next character from the string.
|
||||
*/
|
||||
|
||||
str++;
|
||||
}
|
||||
|
||||
/* We have looked at every character in the string, and none of them match
|
||||
* any of the characters in charset.
|
||||
*/
|
||||
|
||||
return NULL;
|
||||
}
|
52
components/libc/nuttx/libc/string/lib_strrchr.c
Normal file
52
components/libc/nuttx/libc/string/lib_strrchr.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strrchr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/* The strrchr() function returns a pointer to the last
|
||||
* occurrence of the character c in the string s.
|
||||
*/
|
||||
|
||||
FAR char *strrchr(FAR const char *s, int c)
|
||||
{
|
||||
if (s)
|
||||
{
|
||||
const char *p = &s[strlen(s)];
|
||||
for (; p >= s; p--)
|
||||
{
|
||||
if (*p == c)
|
||||
{
|
||||
return (FAR char *)p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
69
components/libc/nuttx/libc/string/lib_strsep.c
Normal file
69
components/libc/nuttx/libc/string/lib_strsep.c
Normal file
|
@ -0,0 +1,69 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strsep.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strsep
|
||||
*
|
||||
* Description:
|
||||
* If *strp is NULL, the strsep() function returns NULL and does
|
||||
* nothing else. Otherwise, this function finds the first token in the
|
||||
* string *strp, that is delimited by one of the bytes in the string
|
||||
* delim. This token is terminated by overwriting the delimiter with a
|
||||
* null byte ('\0'), and *strp is updated to point past the token.
|
||||
* In case no delimiter was found, the token is taken to be the entire
|
||||
* string *strp, and *strp is made NULL.
|
||||
*
|
||||
* Returned Value:
|
||||
* The strsep() function returns a pointer to the token, that is, it
|
||||
* returns the original value of *strp.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strsep(FAR char **strp, FAR const char *delim)
|
||||
{
|
||||
FAR char *sbegin = *strp;
|
||||
FAR char *end;
|
||||
|
||||
if (sbegin == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
end = strpbrk(sbegin, delim);
|
||||
if (end != NULL)
|
||||
{
|
||||
*end++ = '\0';
|
||||
}
|
||||
|
||||
*strp = end;
|
||||
return sbegin;
|
||||
}
|
180
components/libc/nuttx/libc/string/lib_strsignal.c
Normal file
180
components/libc/nuttx/libc/string/lib_strsignal.c
Normal file
|
@ -0,0 +1,180 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strsignal.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/* We don't know what signals names will be assigned to which signals in
|
||||
* advance and we do not want to return a volatile value. One solution is
|
||||
* this silly array of useless names:
|
||||
*/
|
||||
|
||||
static FAR const char *g_default_sigstr[32] =
|
||||
{
|
||||
"Signal 0",
|
||||
"Signal 1",
|
||||
"Signal 2",
|
||||
"Signal 3",
|
||||
"Signal 4",
|
||||
"Signal 5",
|
||||
"Signal 6",
|
||||
"Signal 7",
|
||||
"Signal 8",
|
||||
"Signal 9",
|
||||
"Signal 10",
|
||||
"Signal 11",
|
||||
"Signal 12",
|
||||
"Signal 13",
|
||||
"Signal 14",
|
||||
"Signal 15",
|
||||
"Signal 16",
|
||||
"Signal 17",
|
||||
"Signal 18",
|
||||
"Signal 19",
|
||||
"Signal 20",
|
||||
"Signal 21",
|
||||
"Signal 22",
|
||||
"Signal 23",
|
||||
"Signal 24",
|
||||
"Signal 25",
|
||||
"Signal 26",
|
||||
"Signal 27",
|
||||
"Signal 28",
|
||||
"Signal 29",
|
||||
"Signal 30",
|
||||
"Signal 31",
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strsignal
|
||||
*
|
||||
* Description:
|
||||
* The strsignal() function will map the signal number in signum to an
|
||||
* implementation-defined string and will return a pointer to it.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strsignal(int signum)
|
||||
{
|
||||
/* Handle invalid signals */
|
||||
|
||||
if (!GOOD_SIGNO(signum))
|
||||
{
|
||||
return (FAR char *)"Invalid Signal";
|
||||
}
|
||||
|
||||
/* Handle named signals */
|
||||
|
||||
switch (signum)
|
||||
{
|
||||
/* Standard signals */
|
||||
|
||||
#ifdef SIGUSR1
|
||||
case SIGUSR1:
|
||||
return (FAR char *)"SIGUSR1";
|
||||
#endif
|
||||
|
||||
#ifdef SIGUSR2
|
||||
case SIGUSR2:
|
||||
return (FAR char *)"SIGUSR2";
|
||||
#endif
|
||||
|
||||
#ifdef SIGALRM
|
||||
case SIGALRM:
|
||||
return (FAR char *)"SIGALRM";
|
||||
#endif
|
||||
|
||||
#ifdef SIGCHLD
|
||||
case SIGCHLD:
|
||||
return (FAR char *)"SIGCHLD";
|
||||
#endif
|
||||
|
||||
#ifdef SIGPOLL
|
||||
case SIGPOLL:
|
||||
return (FAR char *)"SIGPOLL";
|
||||
#endif
|
||||
|
||||
#ifdef SIGSTOP
|
||||
case SIGSTOP:
|
||||
return (FAR char *)"SIGSTOP";
|
||||
#endif
|
||||
|
||||
#ifdef SIGTSTP
|
||||
case SIGTSTP:
|
||||
return (FAR char *)"SIGTSTP";
|
||||
#endif
|
||||
|
||||
#ifdef SIGCONT
|
||||
case SIGCONT:
|
||||
return (FAR char *)"SIGCONT";
|
||||
#endif
|
||||
|
||||
#ifdef SIGKILL
|
||||
case SIGKILL:
|
||||
return (FAR char *)"SIGKILL";
|
||||
#endif
|
||||
|
||||
#ifdef SIGINT
|
||||
case SIGINT:
|
||||
return (FAR char *)"SIGINT";
|
||||
#endif
|
||||
|
||||
#ifdef SIGQUIT
|
||||
case SIGQUIT:
|
||||
return (FAR char *)"SIGQUIT";
|
||||
#endif
|
||||
|
||||
#ifdef SIGTERM
|
||||
case SIGTERM:
|
||||
return (FAR char *)"SIGTERM";
|
||||
#endif
|
||||
|
||||
/* Non-standard signals */
|
||||
|
||||
#ifdef SIGCONDTIMEDOUT
|
||||
case SIGCONDTIMEDOUT:
|
||||
return (FAR char *)"SIGCONDTIMEDOUT";
|
||||
#endif
|
||||
|
||||
#ifdef SIGWORK
|
||||
case SIGWORK:
|
||||
return (FAR char *)"SIGWORK";
|
||||
#endif
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Return a string devoid is meaning */
|
||||
|
||||
return (FAR char *)g_default_sigstr[signum];
|
||||
}
|
47
components/libc/nuttx/libc/string/lib_strspn.c
Normal file
47
components/libc/nuttx/libc/string/lib_strspn.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strspn.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strspn
|
||||
*
|
||||
* Description:
|
||||
* strspn() calculates the length of the initial segment of s which
|
||||
* consists entirely of characters in accept.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
size_t strspn(const char *s, const char *accept)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; s[i] && strchr(accept, s[i]) != NULL; i++);
|
||||
return i;
|
||||
}
|
90
components/libc/nuttx/libc/string/lib_strstr.c
Normal file
90
components/libc/nuttx/libc/string/lib_strstr.c
Normal file
|
@ -0,0 +1,90 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strstr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strstr(FAR const char *str, FAR const char *substr)
|
||||
{
|
||||
FAR const char *candidate; /* Candidate in str with matching start character */
|
||||
char ch; /* First character of the substring */
|
||||
size_t len; /* The length of the substring */
|
||||
|
||||
/* Special case the empty substring */
|
||||
|
||||
len = strlen(substr);
|
||||
ch = *substr;
|
||||
|
||||
if (!ch)
|
||||
{
|
||||
/* We'll say that an empty substring matches at the beginning of
|
||||
* the string
|
||||
*/
|
||||
|
||||
return (FAR char *)str;
|
||||
}
|
||||
|
||||
/* Search for the substring */
|
||||
|
||||
candidate = str;
|
||||
for (; ; )
|
||||
{
|
||||
/* strchr() will return a pointer to the next occurrence of the
|
||||
* character ch in the string
|
||||
*/
|
||||
|
||||
candidate = strchr(candidate, ch);
|
||||
if (!candidate || strlen(candidate) < len)
|
||||
{
|
||||
/* First character of the substring does not appear in the string
|
||||
* or the remainder of the string is not long enough to contain the
|
||||
* substring.
|
||||
*/
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Check if this is the beginning of a matching substring */
|
||||
|
||||
if (strncmp(candidate, substr, len) == 0)
|
||||
{
|
||||
return (FAR char *)candidate;
|
||||
}
|
||||
|
||||
/* No, find the next candidate after this one */
|
||||
|
||||
candidate++;
|
||||
}
|
||||
|
||||
/* Won't get here, but some compilers might complain. Other compilers
|
||||
* might complain about this code being unreachable too.
|
||||
*/
|
||||
|
||||
return NULL;
|
||||
}
|
72
components/libc/nuttx/libc/string/lib_strtok.c
Normal file
72
components/libc/nuttx/libc/string/lib_strtok.c
Normal file
|
@ -0,0 +1,72 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strtok.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static char *g_saveptr = NULL;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strtok
|
||||
*
|
||||
* Description:
|
||||
* The strtok() function parses a string into a
|
||||
* sequence of tokens. On the first call to strtok() the
|
||||
* string to be parsed should be specified in 'str'. In
|
||||
* each subsequent call that should parse the same string,
|
||||
* 'str' should be NULL.
|
||||
*
|
||||
* The 'delim' argument specifies a set of characters that
|
||||
* delimit the tokens in the parsed string. The caller
|
||||
* may specify different strings in delim in successive
|
||||
* calls that parse the same string.
|
||||
*
|
||||
* Each call to strtok() returns a pointer to a null-
|
||||
* terminated string containing the next token. This
|
||||
* string does not include the delimiting character. If
|
||||
* no more tokens are found, strtok() returns NULL.
|
||||
*
|
||||
* A sequence of two or more contiguous delimiter
|
||||
* characters in the parsed string is considered to be a
|
||||
* single delimiter. Delimiter characters at the start or
|
||||
* end of the string are ignored. The tokens returned by
|
||||
* strtok() are always non-empty strings.
|
||||
*
|
||||
* Returned Value:
|
||||
* strtok() returns a pointer to the next token, or NULL
|
||||
* if there are no more tokens.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
char *strtok(char *str, const char *delim)
|
||||
{
|
||||
return strtok_r(str, delim, &g_saveptr);
|
||||
}
|
144
components/libc/nuttx/libc/string/lib_strtokr.c
Normal file
144
components/libc/nuttx/libc/string/lib_strtokr.c
Normal file
|
@ -0,0 +1,144 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/string/lib_strtokr.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: strtok_r
|
||||
*
|
||||
* Description:
|
||||
* The strtok_r() function is a reentrant version strtok().
|
||||
* Like strtok(), it parses a string into a sequence of
|
||||
* tokens. On the first call to strtok() the string to be
|
||||
* parsed should be specified in 'str'. In each subsequent
|
||||
* call that should parse the same string, 'str' should be
|
||||
* NULL.
|
||||
*
|
||||
* The 'saveptr' argument is a pointer to a char *
|
||||
* variable that is used internally by strtok_r() in
|
||||
* order to maintain context between successive calls
|
||||
* that parse the same string.
|
||||
*
|
||||
* On the first call to strtok_r(), 'str' should point to the
|
||||
* string to be parsed, and the value of 'saveptr' is
|
||||
* ignored. In subsequent calls, 'str' should be NULL, and
|
||||
* saveptr should be unchanged since the previous call.
|
||||
*
|
||||
* The 'delim' argument specifies a set of characters that
|
||||
* delimit the tokens in the parsed string. The caller
|
||||
* may specify different strings in delim in successive
|
||||
* calls that parse the same string.
|
||||
*
|
||||
* Each call to strtok_r() returns a pointer to a null-
|
||||
* terminated string containing the next token. This
|
||||
* string does not include the delimiting character. If
|
||||
* no more tokens are found, strtok_r() returns NULL.
|
||||
*
|
||||
* A sequence of two or more contiguous delimiter
|
||||
* characters in the parsed string is considered to be a
|
||||
* single delimiter. Delimiter characters at the start or
|
||||
* end of the string are ignored. The tokens returned by
|
||||
* strtok() are always non-empty strings.
|
||||
*
|
||||
* Returned Value:
|
||||
* strtok_r() returns a pointer to the next token, or NULL
|
||||
* if there are no more tokens.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *strtok_r(FAR char *str, FAR const char *delim, FAR char **saveptr)
|
||||
{
|
||||
char *pbegin;
|
||||
char *pend = NULL;
|
||||
|
||||
/* Decide if we are starting a new string or continuing from
|
||||
* the point we left off.
|
||||
*/
|
||||
|
||||
if (str)
|
||||
{
|
||||
pbegin = str;
|
||||
}
|
||||
else if (saveptr && *saveptr)
|
||||
{
|
||||
pbegin = *saveptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Find the beginning of the next token */
|
||||
|
||||
for (;
|
||||
*pbegin && strchr(delim, *pbegin) != NULL;
|
||||
pbegin++);
|
||||
|
||||
/* If we are at the end of the string with nothing
|
||||
* but delimiters found, then return NULL.
|
||||
*/
|
||||
|
||||
if (!*pbegin)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Find the end of the token */
|
||||
|
||||
for (pend = pbegin + 1;
|
||||
*pend && strchr(delim, *pend) == NULL;
|
||||
pend++);
|
||||
|
||||
/* pend either points to the end of the string or to
|
||||
* the first delimiter after the string.
|
||||
*/
|
||||
|
||||
if (*pend)
|
||||
{
|
||||
/* Turn the delimiter into a null terminator */
|
||||
|
||||
*pend++ = '\0';
|
||||
}
|
||||
|
||||
/* Save the pointer where we left off and return the
|
||||
* beginning of the token.
|
||||
*/
|
||||
|
||||
if (saveptr)
|
||||
{
|
||||
*saveptr = pend;
|
||||
}
|
||||
|
||||
return pbegin;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue