Initial commit

This commit is contained in:
2025-08-20 19:02:58 +08:00
commit f6b2677c0f
8577 changed files with 1307117 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
LIST=CPU
include recurse.mk

View File

@@ -0,0 +1,32 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <limits.h>
#include <stdlib.h>
char *_fullpath( char *buffer, const char *path, size_t size)
{
char tembuf[_POSIX_PATH_MAX];
realpath(path, tembuf);
if(strlen(tembuf) >size)
return NULL;
else
return realpath(path, buffer);
}

25
lib/compat/linux/atoi.c Normal file
View File

@@ -0,0 +1,25 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
int (atoi)(const char *s) {
return (int)_Stoint(s, 0, 10, 0);
}

27
lib/compat/linux/itoa.c Normal file
View File

@@ -0,0 +1,27 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
extern char *ltoa(long int value, char *buf, int radix);
char *itoa(int value, char *buf, int radix) {
return ltoa(value, buf, radix);
}

31
lib/compat/linux/lltoa.c Normal file
View File

@@ -0,0 +1,31 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
char *lltoa(int64_t value, char *buf, int radix) {
char *save = buf;
if(radix == 10 && value < 0) {
*buf++ = '-';
value = -value;
}
return ulltoa(value, buf, radix) ? save : 0;
}

28
lib/compat/linux/ltoa.c Normal file
View File

@@ -0,0 +1,28 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
extern char *lltoa(int64_t value, char *buf, int radix);
char *ltoa(long int value, char *buf, int radix) {
return lltoa(value, buf, radix);
}

View File

@@ -0,0 +1,46 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <string.h>
int _memicmp( const void *_s1, const void *_s2, size_t len )
{
return( memicmp( _s1, _s2, len ) );
}
int memicmp( const void *_s1, const void *_s2, size_t len )
{
const unsigned char *s1 = _s1;
const unsigned char *s2 = _s2;
unsigned char c1;
unsigned char c2;
for( ;; ) {
if( len == 0 ) return( 0 );
c1 = *s1;
c2 = *s2;
if( c1 >= 'A' && c1 <= 'Z' ) c1 += 'a' - 'A';
if( c2 >= 'A' && c2 <= 'Z' ) c2 += 'a' - 'A';
if( c1 != c2 ) return( *s1 - *s2 );
++s1;
++s2;
--len;
}
}

165
lib/compat/linux/pathfind.c Normal file
View File

@@ -0,0 +1,165 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#define _FILE_OFFSET_BITS 64
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <libgen.h>
#include <sys/stat.h>
#ifndef PATH_MAX
#define PATH_MAX 1024 /* Should use fpathconf(path, _PC_PATH_MAX) */
#endif
#if defined(__WATCOMC__) && __WATCOMC__ <= 1100
#define FIXCONST __based(__segname("CONST2"))
#else
#define FIXCONST
#endif
#define OPTS "r" "w" "x" "f" "b" "c" "d" "p" "u" "g" "k" "s"
#define READABLE 0x00000001 /* r */
#define WRITEABLE 0x00000002 /* w */
#define EXECUTABLE 0x00000004 /* x */
#define FILE 0x00000008 /* f */
#define BLOCKSPEC 0x00000010 /* b */
#define CHARSPEC 0x00000020 /* c */
#define DIR 0x00000040 /* d */
#define FIFO 0x00000080 /* p */
#define SUID 0x00000100 /* u */
#define SGID 0x00000200 /* g */
#define STICKY 0x00000400 /* k */
#define HAS_SIZE 0x00000800 /* s */
static int check(const char *path, unsigned flags) {
struct stat buff;
if(flags & (READABLE | WRITEABLE | EXECUTABLE)) {
int amode = 0;
if(flags & READABLE) {
amode |= R_OK;
}
if(flags & WRITEABLE) {
amode |= W_OK;
}
if(flags & EXECUTABLE) {
amode |= X_OK;
}
if(access(path, amode) == -1) {
return 0;
}
if(!(flags & ~(READABLE | WRITEABLE))) {
return 1;
}
}
if(stat(path, &buff) == -1) {
return 0;
}
if((flags & FILE) && !S_ISREG(buff.st_mode)) {
return 0;
}
if((flags & BLOCKSPEC) && !S_ISBLK(buff.st_mode)) {
return 0;
}
if((flags & CHARSPEC) && !S_ISCHR(buff.st_mode)) {
return 0;
}
if((flags & DIR) && !S_ISDIR(buff.st_mode)) {
return 0;
}
if((flags & (EXECUTABLE | DIR)) == EXECUTABLE && S_ISDIR(buff.st_mode)) {
return 0;
}
if((flags & FIFO) && !S_ISFIFO(buff.st_mode)) {
return 0;
}
if((flags & SUID) && !(buff.st_mode & S_ISUID)) {
return 0;
}
if((flags & SGID) && !(buff.st_mode & S_ISGID)) {
return 0;
}
if((flags & STICKY) && !(buff.st_mode & S_ISVTX)) {
return 0;
}
if((flags & HAS_SIZE) && buff.st_size == 0) {
return 0;
}
return 1;
}
char *pathfind_r(const char *path, const char *name, const char *mode, char *buff, size_t buff_size) {
unsigned flags;
char *p;
int n;
flags = 0;
--mode;
while(*++mode) {
unsigned bit;
const char *ptr;
static const char FIXCONST opts[] = OPTS;
for(ptr = opts, bit = 1; *ptr; ptr++, bit <<= 1) {
if(*mode == *ptr) {
flags |= bit;
break;
}
}
}
n = strlen(name);
if(*name == '/') {
return (n < buff_size && check(name, flags)) ? strcpy(buff, name) : 0;
}
buff_size -= n;
do {
for(n = buff_size, *(p = buff) = 0; path && *path && *path != ':'; n--, path++) {
if(n > 0) {
*p++ = *path;
}
}
if(n > 0) {
if(*buff && p[-1] != '/') {
*p++ = '/';
}
strcpy(p, name);
if(check(buff, flags)) {
return buff;
}
}
if(path && *path == ':') {
path++;
}
} while(path && *path);
*buff = 0;
return 0;
}
char *pathfind(const char *path, const char *name, const char *mode) {
static char buff[PATH_MAX + 1];
return pathfind_r(path, name, mode, buff, sizeof buff);
}

View File

@@ -0,0 +1,87 @@
/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``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 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.
*/
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable
* license fees to QNX Software Systems before you may reproduce,
* modify or distribute this software, or any work that includes
* all or part of this software. Free development licenses are
* available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email
* licensing@qnx.com.
*
* This file may contain contributions from others. Please review
* this entire file for other proprietary rights or license notices,
* as well as the QNX Development Suite License Guide at
* http://licensing.qnx.com/license-guide/ for other information.
* $
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <string.h>
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t strlcpy(dst, src, siz)
char *dst;
const char *src;
size_t siz;
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}

174
lib/compat/linux/strmode.c Normal file
View File

@@ -0,0 +1,174 @@
/*-
* Copyright (c) 1990 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.
*/
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable
* license fees to QNX Software Systems before you may reproduce,
* modify or distribute this software, or any work that includes
* all or part of this software. Free development licenses are
* available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email
* licensing@qnx.com.
*
* This file may contain contributions from others. Please review
* this entire file for other proprietary rights or license notices,
* as well as the QNX Development Suite License Guide at
* http://licensing.qnx.com/license-guide/ for other information.
* $
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strmode.c,v 1.3 1997/06/13 13:57:20 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#ifdef __MINGW32__
#include <lib/compat.h>
#endif
void
strmode(mode, p)
register mode_t mode;
register char *p;
{
/* print type */
switch (mode & S_IFMT) {
case S_IFDIR: /* directory */
*p++ = 'd';
break;
case S_IFCHR: /* character special */
*p++ = 'c';
break;
case S_IFBLK: /* block special */
*p++ = 'b';
break;
case S_IFREG: /* regular */
*p++ = '-';
break;
case S_IFLNK: /* symbolic link */
*p++ = 'l';
break;
case S_IFSOCK: /* socket */
*p++ = 's';
break;
#ifdef S_IFIFO
case S_IFIFO: /* fifo */
*p++ = 'p';
break;
#endif
#ifdef S_IFWHT
case S_IFWHT: /* whiteout */
*p++ = 'w';
break;
#endif
default: /* unknown */
*p++ = '?';
break;
}
/* usr */
if (mode & S_IRUSR)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWUSR)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXUSR | S_ISUID)) {
case 0:
*p++ = '-';
break;
case S_IXUSR:
*p++ = 'x';
break;
case S_ISUID:
*p++ = 'S';
break;
case S_IXUSR | S_ISUID:
*p++ = 's';
break;
}
/* group */
if (mode & S_IRGRP)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWGRP)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXGRP | S_ISGID)) {
case 0:
*p++ = '-';
break;
case S_IXGRP:
*p++ = 'x';
break;
case S_ISGID:
*p++ = 'S';
break;
case S_IXGRP | S_ISGID:
*p++ = 's';
break;
}
/* other */
if (mode & S_IROTH)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWOTH)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXOTH | S_ISVTX)) {
case 0:
*p++ = '-';
break;
case S_IXOTH:
*p++ = 'x';
break;
case S_ISVTX:
*p++ = 'T';
break;
case S_IXOTH | S_ISVTX:
*p++ = 't';
break;
}
*p++ = ' '; /* will be a '+' if ACL's implemented */
*p = '\0';
}

26
lib/compat/linux/tell.c Normal file
View File

@@ -0,0 +1,26 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <sys/types.h>
#include <unistd.h>
off_t tell (int filedes)
{
return(lseek(filedes, 0, SEEK_CUR));
}

48
lib/compat/linux/ulltoa.c Normal file
View File

@@ -0,0 +1,48 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
#include <errno.h>
#if defined(__WATCOMC__) && __WATCOMC__ <= 1100
#define FIXCONST __based(__segname("CONST2"))
#else
#define FIXCONST
#endif
char *ulltoa(u_int64_t value, char *buf, int radix) {
char tmp[64 + 1]; /* Lowest radix is 2, so 64-bits plus a null */
char *p1 = tmp, *p2;
static const char FIXCONST xlat[] = "0123456789abcdefghijklmnopqrstuvwxyz";
if(radix < 2 || radix > 36) {
errno = EINVAL;
return 0;
}
do {
*p1++ = xlat[value % radix];
} while(value /= radix);
for(p2 = buf; p1 != tmp; *p2++ = *--p1);
*p2 = '\0';
return buf;
}

35
lib/compat/linux/ultoa.c Normal file
View File

@@ -0,0 +1,35 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
#include <errno.h>
extern char *ulltoa(u_int64_t value, char *buf, int radix);
#if defined(__WATCOMC__) && __WATCOMC__ <= 1100
#define FIXCONST __based(__segname("CONST2"))
#else
#define FIXCONST
#endif
char *ultoa(unsigned long int value, char *buf, int radix) {
return ulltoa(value, buf, radix);
}

27
lib/compat/linux/utoa.c Normal file
View File

@@ -0,0 +1,27 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
#include <stdlib.h>
extern char *ultoa( unsigned long int __value, char *__buf, int __radix );
char *utoa(unsigned int value, char *buf, int radix) {
return ultoa(value, buf, radix);
}

View File

@@ -0,0 +1,2 @@
LIST=VARIANT
include recurse.mk

View File

@@ -0,0 +1 @@
include ../../../common.mk

164
lib/compat/linux/xstoint.c Normal file
View File

@@ -0,0 +1,164 @@
/*
* $QNXLicenseC:
* Copyright 2007, QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit http://licensing.qnx.com or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at http://licensing.qnx.com/license-guide/
* for other information.
* $
*/
/*
* This convert a string to a constant using the POSIX locale
*/
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#define _STOINT_SIGNED 0x1
#define _STOINT_LLONG 0x2
#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX - 1LL)
#define ULLONG_MAX 18446744073709551615ULL
u_int64_t _Stoint(const char *nptr, char **endptr, int base, int flags) {
int neg, overflow, once;
const char *p;
u_int64_t ll_value;
unsigned long l_value;
int n;
/* Part 1, skip white space characters */
p = nptr;
while(isspace(*p)) {
p++;
}
/* Part 2, Optional sign */
neg = 0;
if(*p == '+') {
p++;
} else if(*p == '-') {
neg = 1;
p++;
}
/* start with 0 for octal, 0x or 0X for hex, rest is decimal */
if(base == 0) {
if(*p == '0') {
if(*(p+1) == 'x' || *(p+1) == 'X') {
p += 2;
base = 16;
} else {
base = 8;
}
} else {
base = 10;
}
} else if(base == 16 && *p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) {
/* Base 16 is allowed to start with 0x or 0X if there */
p += 2;
}
ll_value = 0;
l_value = 0;
once = overflow = 0;
/* Only valid bases are 2 to 36 */
if(base >= 2 && base <= 36) {
while(n = *p) {
u_int64_t ll_save;
unsigned long l_save;
/* Adjust character to from a char to an integer */
if(n >= '0' && n <= '9') {
n -= '0';
} else if(n >= 'a' && n <= 'z') {
n -= 'a' - 10;
} else if(n >= 'A' && n <= 'Z') {
n -= 'A' - 10;
} else {
/* Unknown, done */
break;
}
if(n >= base) {
/* Out of radix range, done */
break;
}
/* Character is valid */
p++;
/* Scale, and add digit, check for overflow */
if (flags & _STOINT_LLONG) {
ll_save = ll_value;
ll_value = ll_value * base + n;
if(ll_value < ll_save) {
overflow = 1;
}
} else {
l_save = l_value;
l_value = l_value * base + n;
if(l_value < l_save) {
overflow = 1;
}
}
once = 1;
}
/* Check for a signed number overflow */
if(flags & _STOINT_SIGNED) {
if(flags & _STOINT_LLONG) {
if(((int64_t)ll_value < 0) && !(neg && (ll_value == -(u_int64_t)LLONG_MIN))) {
overflow = 1;
}
} else {
if(((long)l_value < 0) && !(neg && (l_value == -(unsigned long)LONG_MIN))) {
overflow = 1;
}
}
}
}
/* Store the end of part 2, so caller can get to part 3 */
if(endptr) {
*endptr = (char *)(once ? p : nptr);
}
/* If base not supported or no conversion was done, return 0 setting errno to EINVAL (UNIX98) */
if(!once) {
errno = EINVAL;
return 0;
}
/* If no conversion was done, return respective max setting errno to ERANGE (UNIX98) */
if(overflow) {
errno = ERANGE;
if (flags & _STOINT_LLONG) {
return (flags & _STOINT_SIGNED) ? (neg ? LLONG_MIN : LLONG_MAX) : ULLONG_MAX;
} else {
return (flags & _STOINT_SIGNED) ? (neg ? LONG_MIN : LONG_MAX) : ULONG_MAX;
}
}
/* Everything OK, return the number */
if (flags & _STOINT_LLONG) {
return neg ? -ll_value : ll_value;
} else {
return neg ? -l_value : l_value;
}
}