Implemented getenv(), atexit(), number parsing, and more
This commit is contained in:
@@ -45,7 +45,4 @@ typedef __uint_fast64_t uint_fast64_t;
|
|||||||
typedef __intmax_t intmax_t;
|
typedef __intmax_t intmax_t;
|
||||||
typedef __uintmax_t uintmax_t;
|
typedef __uintmax_t uintmax_t;
|
||||||
|
|
||||||
typedef __intptr_t intptr_t;
|
|
||||||
typedef __uintptr_t uintptr_t;
|
|
||||||
|
|
||||||
#endif /* STDINT_H_ */
|
#endif /* STDINT_H_ */
|
||||||
|
|||||||
@@ -19,9 +19,13 @@ extern void srand(unsigned int seed);
|
|||||||
|
|
||||||
extern int atoi(const char *str);
|
extern int atoi(const char *str);
|
||||||
extern double atof(const char *str);
|
extern double atof(const char *str);
|
||||||
extern long int strtol(const char *str, char **endptr, int base);
|
extern long strtol(const char *str, char **endptr, int base);
|
||||||
|
extern unsigned long strtoul(const char *str, char **endptr, int base);
|
||||||
|
extern float strtof(const char *str, char **endptr);
|
||||||
extern double strtod(const char *str, char **endptr);
|
extern double strtod(const char *str, char **endptr);
|
||||||
|
|
||||||
|
extern char *getenv(const char *name);
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
} /* extern "C" */
|
} /* extern "C" */
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
21
photon/libc/stdlib/atexit.c
Normal file
21
photon/libc/stdlib/atexit.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#define MAX_FN 32
|
||||||
|
|
||||||
|
static void(*atexit_fn[MAX_FN])(void);
|
||||||
|
static int atexit_fn_count = 0;
|
||||||
|
|
||||||
|
int atexit(void(*fn)(void))
|
||||||
|
{
|
||||||
|
if (atexit_fn_count == MAX_FN) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
atexit_fn[atexit_fn_count++] = fn;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void __crt_run_atexit()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < atexit_fn_count; i++) {
|
||||||
|
atexit_fn[i]();
|
||||||
|
}
|
||||||
|
}
|
||||||
6
photon/libc/stdlib/getenv.c
Normal file
6
photon/libc/stdlib/getenv.c
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
extern char *__crt_sys_getenv(const char *name);
|
||||||
|
|
||||||
|
char *getenv(const char *name)
|
||||||
|
{
|
||||||
|
return __crt_sys_getenv(name);
|
||||||
|
}
|
||||||
33
photon/libc/stdlib/strtof.c
Normal file
33
photon/libc/stdlib/strtof.c
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
float strtof(const char *str, char **endptr)
|
||||||
|
{
|
||||||
|
float res = 0.0F;
|
||||||
|
char *ptr = (char*)str;
|
||||||
|
int after_dot = 0;
|
||||||
|
float div = 1;
|
||||||
|
|
||||||
|
while (*ptr != '\0') {
|
||||||
|
if (isdigit(*ptr)) {
|
||||||
|
if (!after_dot) {
|
||||||
|
res *= 10;
|
||||||
|
res += *ptr - '0';
|
||||||
|
} else {
|
||||||
|
div *= 10;
|
||||||
|
res += (float)(*ptr - '0') / div;
|
||||||
|
}
|
||||||
|
} else if (*ptr == '.') {
|
||||||
|
after_dot = 1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (endptr) {
|
||||||
|
*endptr = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
@@ -10,11 +10,6 @@ long strtol(const char *nptr, char **endptr, int base)
|
|||||||
unsigned long cutoff;
|
unsigned long cutoff;
|
||||||
int neg = 0, any, cutlim;
|
int neg = 0, any, cutlim;
|
||||||
|
|
||||||
/*
|
|
||||||
* Skip white space and pick up leading +/- sign if any.
|
|
||||||
* If base is 0, allow 0x for hex and 0 for octal, else
|
|
||||||
* assume decimal; if base is already 16, allow 0x.
|
|
||||||
*/
|
|
||||||
do {
|
do {
|
||||||
c = *s++;
|
c = *s++;
|
||||||
} while (isspace(c));
|
} while (isspace(c));
|
||||||
@@ -22,9 +17,9 @@ long strtol(const char *nptr, char **endptr, int base)
|
|||||||
if (c == '-') {
|
if (c == '-') {
|
||||||
neg = 1;
|
neg = 1;
|
||||||
c = *s++;
|
c = *s++;
|
||||||
} else if (c == '+')
|
} else if (c == '+') {
|
||||||
c = *s++;
|
c = *s++;
|
||||||
if ((base == 0 || base == 16) &&
|
} if ((base == 0 || base == 16) &&
|
||||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||||
c = s[1];
|
c = s[1];
|
||||||
s += 2;
|
s += 2;
|
||||||
@@ -35,51 +30,44 @@ long strtol(const char *nptr, char **endptr, int base)
|
|||||||
s += 2;
|
s += 2;
|
||||||
base = 2;
|
base = 2;
|
||||||
}
|
}
|
||||||
if (base == 0)
|
|
||||||
base = c == '0' ? 8 : 10;
|
|
||||||
|
|
||||||
/*
|
if (base == 0) {
|
||||||
* Compute the cutoff value between legal numbers and illegal
|
base = c == '0' ? 8 : 10;
|
||||||
* numbers. That is the largest legal value, divided by the
|
}
|
||||||
* base. An input number that is greater than this value, if
|
|
||||||
* followed by a legal input character, is too big. One that
|
|
||||||
* is equal to this value may be valid or not; the limit
|
|
||||||
* between valid and invalid numbers is then based on the last
|
|
||||||
* digit. For instance, if the range for longs is
|
|
||||||
* [-2147483648..2147483647] and the input base is 10,
|
|
||||||
* cutoff will be set to 214748364 and cutlim to either
|
|
||||||
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
|
|
||||||
* a value > 214748364, or equal but the next digit is > 7 (or 8),
|
|
||||||
* the number is too big, and we will return a range error.
|
|
||||||
*
|
|
||||||
* Set any if any `digits' consumed; make it negative to indicate
|
|
||||||
* overflow.
|
|
||||||
*/
|
|
||||||
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
|
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
|
||||||
cutlim = cutoff % (unsigned long)base;
|
cutlim = cutoff % (unsigned long)base;
|
||||||
cutoff /= (unsigned long)base;
|
cutoff /= (unsigned long)base;
|
||||||
|
|
||||||
for (acc = 0, any = 0;; c = *s++) {
|
for (acc = 0, any = 0;; c = *s++) {
|
||||||
if (isdigit(c))
|
if (isdigit(c)) {
|
||||||
c -= '0';
|
c -= '0';
|
||||||
else if (isalpha(c))
|
} else if (isalpha(c)) {
|
||||||
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
||||||
else
|
} else {
|
||||||
break;
|
break;
|
||||||
if (c >= base)
|
}
|
||||||
|
|
||||||
|
if (c >= base) {
|
||||||
break;
|
break;
|
||||||
if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
|
} if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) {
|
||||||
any = -1;
|
any = -1;
|
||||||
else {
|
} else {
|
||||||
any = 1;
|
any = 1;
|
||||||
acc *= base;
|
acc *= base;
|
||||||
acc += c;
|
acc += c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (any < 0) {
|
if (any < 0) {
|
||||||
acc = neg ? LONG_MIN : LONG_MAX;
|
acc = neg ? LONG_MIN : LONG_MAX;
|
||||||
} else if (neg)
|
} else if (neg) {
|
||||||
acc = -acc;
|
acc = -acc;
|
||||||
if (endptr != 0)
|
}
|
||||||
*endptr = (char *)(any ? s - 1 : nptr);
|
|
||||||
return (acc);
|
if (endptr != 0) {
|
||||||
|
*endptr = (char *)(any ? s - 1 : nptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
}
|
}
|
||||||
|
|||||||
9
photon/libc/stdlib/strtoul.c
Normal file
9
photon/libc/stdlib/strtoul.c
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
unsigned long strtoul(const char *nptr, char **endptr, int base)
|
||||||
|
{
|
||||||
|
return (unsigned long)strtol(nptr, endptr, base);
|
||||||
|
}
|
||||||
14
photon/libc/string/memcmp.c
Normal file
14
photon/libc/string/memcmp.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
int memcmp(const void *a, const void *b, size_t n)
|
||||||
|
{
|
||||||
|
const unsigned char *s1 = a, *s2 = b;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < n; i++) {
|
||||||
|
if (s1[i] != s2[i]) {
|
||||||
|
return ((s1[i] < s2[i]) ? -1 : 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
27
photon/libc/string/memmove.c
Normal file
27
photon/libc/string/memmove.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define ALIGNED(p) (!((long)p & (sizeof(long) - 1)))
|
||||||
|
#define QUADBLOCKSIZE (sizeof(long) << 2)
|
||||||
|
#define BLOCKSIZE (sizeof(long))
|
||||||
|
|
||||||
|
static void *memcpy_r(void *dest, const void *src, size_t sz)
|
||||||
|
{
|
||||||
|
unsigned char *d = dest;
|
||||||
|
const unsigned char *s = src;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < sz; i++) {
|
||||||
|
size_t b = sz - i - 1;
|
||||||
|
d[b] = s[b];
|
||||||
|
}
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *memmove(void *dest, const void *src, size_t n)
|
||||||
|
{
|
||||||
|
if (dest > src) {
|
||||||
|
return memcpy(dest, src, n);
|
||||||
|
} else {
|
||||||
|
return memcpy_r(dest, src, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,25 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
int strcmp(const char *s1, const char *s2)
|
int strcmp(const char *s1, const char *s2)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; s1[i] == s2[i]; i++)
|
for (i = 0; s1[i] == s2[i]; i++) {
|
||||||
if (s1[i] == '\0')
|
if (s1[i] == '\0') {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return s1[i] - s2[i];
|
return s1[i] - s2[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
int strncmp(const char *s1, const char *s2, unsigned int n)
|
int strncmp(const char *s1, const char *s2, size_t n)
|
||||||
{
|
{
|
||||||
for (; n > 0; s1++, s2++, --n)
|
for (; n > 0; s1++, s2++, --n) {
|
||||||
if (*s1 != *s2)
|
if (*s1 != *s2) {
|
||||||
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : 1);
|
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : 1);
|
||||||
else if (*s1 == '\0')
|
} else if (*s1 == '\0') {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
35
photon/libc/sys/magenta/environ.c
Normal file
35
photon/libc/sys/magenta/environ.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
extern const char **__crt_environ();
|
||||||
|
|
||||||
|
char *__crt_sys_getenv(const char *name)
|
||||||
|
{
|
||||||
|
const char **envp = __crt_environ();
|
||||||
|
|
||||||
|
for (int i = 0; envp[i] != NULL; i++) {
|
||||||
|
int equal = -1;
|
||||||
|
const char *env = envp[i];
|
||||||
|
|
||||||
|
for (int ii = 0; env[ii] != '\0'; ii++) {
|
||||||
|
if (env[ii] == '=') {
|
||||||
|
equal = ii;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (equal == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
char env_name[equal];
|
||||||
|
memcpy(env_name, env, equal - 1);
|
||||||
|
env_name[equal - 1] = '\0';
|
||||||
|
|
||||||
|
if (!strcmp(env_name, name)) {
|
||||||
|
return (char *)(env + equal + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
@@ -22,7 +22,15 @@ struct handles {
|
|||||||
|
|
||||||
static struct handles rt_handles;
|
static struct handles rt_handles;
|
||||||
|
|
||||||
|
static const char **environ = NULL;
|
||||||
|
|
||||||
|
const char **__crt_environ()
|
||||||
|
{
|
||||||
|
return environ;
|
||||||
|
}
|
||||||
|
|
||||||
extern int main(int, const char **);
|
extern int main(int, const char **);
|
||||||
|
extern void __crt_run_atexit();
|
||||||
|
|
||||||
static void parse_args(
|
static void parse_args(
|
||||||
mx_bootstrap_msg_t *args, mx_handle_t *handles,
|
mx_bootstrap_msg_t *args, mx_handle_t *handles,
|
||||||
@@ -124,13 +132,18 @@ int __crt_init(mx_handle_t bootstrap)
|
|||||||
|
|
||||||
mx_bootstrap_msg_t *msg = (mx_bootstrap_msg_t *)msg_buf;
|
mx_bootstrap_msg_t *msg = (mx_bootstrap_msg_t *)msg_buf;
|
||||||
|
|
||||||
const char *argv[msg->args_num];
|
const char *argv[msg->args_num + 1];
|
||||||
const char *envp[msg->environ_num];
|
const char *envp[msg->environ_num + 1];
|
||||||
|
argv[msg->args_num] = NULL;
|
||||||
|
envp[msg->environ_num] = NULL;
|
||||||
|
environ = envp;
|
||||||
|
|
||||||
parse_args(msg, handles, argv, envp, nr_handles, &rt_handles);
|
parse_args(msg, handles, argv, envp, nr_handles, &rt_handles);
|
||||||
rt_handles.bootstrap = bootstrap;
|
rt_handles.bootstrap = bootstrap;
|
||||||
|
|
||||||
int ret = main(msg->args_num, argv);
|
int ret = main(msg->args_num, argv);
|
||||||
|
|
||||||
|
__crt_run_atexit();
|
||||||
mx_task_kill(rt_handles.task_self, ret);
|
mx_task_kill(rt_handles.task_self, ret);
|
||||||
|
|
||||||
/* unreachable */
|
/* unreachable */
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ typedef unsigned int uid_t;
|
|||||||
typedef unsigned long useconds_t;
|
typedef unsigned long useconds_t;
|
||||||
typedef unsigned long long dev_t;
|
typedef unsigned long long dev_t;
|
||||||
|
|
||||||
#ifndef __size_t_DEFINED
|
#ifndef __size_t_defined
|
||||||
|
#define __size_t_defined
|
||||||
typedef __uintptr_t size_t;
|
typedef __uintptr_t size_t;
|
||||||
#define __size_t_DEFINED
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef __ssize_t_defined
|
#ifndef __ssize_t_defined
|
||||||
|
|||||||
Reference in New Issue
Block a user