xref: /openbmc/linux/tools/include/nolibc/stdlib.h (revision 06fdba53e0a9a897ba00c3602f14b3498b321655)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * stdlib function definitions for NOLIBC
4  * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5  */
6 
7 #ifndef _NOLIBC_STDLIB_H
8 #define _NOLIBC_STDLIB_H
9 
10 #include "std.h"
11 #include "arch.h"
12 #include "types.h"
13 #include "sys.h"
14 
15 /*
16  * As much as possible, please keep functions alphabetically sorted.
17  */
18 
19 static __attribute__((unused))
20 long atol(const char *s)
21 {
22 	unsigned long ret = 0;
23 	unsigned long d;
24 	int neg = 0;
25 
26 	if (*s == '-') {
27 		neg = 1;
28 		s++;
29 	}
30 
31 	while (1) {
32 		d = (*s++) - '0';
33 		if (d > 9)
34 			break;
35 		ret *= 10;
36 		ret += d;
37 	}
38 
39 	return neg ? -ret : ret;
40 }
41 
42 static __attribute__((unused))
43 int atoi(const char *s)
44 {
45 	return atol(s);
46 }
47 
48 static __attribute__((unused))
49 int msleep(unsigned int msecs)
50 {
51 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
52 
53 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
54 		return (my_timeval.tv_sec * 1000) +
55 			(my_timeval.tv_usec / 1000) +
56 			!!(my_timeval.tv_usec % 1000);
57 	else
58 		return 0;
59 }
60 
61 /* This one is not marked static as it's needed by libgcc for divide by zero */
62 __attribute__((weak,unused))
63 int raise(int signal)
64 {
65 	return kill(getpid(), signal);
66 }
67 
68 static __attribute__((unused))
69 unsigned int sleep(unsigned int seconds)
70 {
71 	struct timeval my_timeval = { seconds, 0 };
72 
73 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
74 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
75 	else
76 		return 0;
77 }
78 
79 static __attribute__((unused))
80 int tcsetpgrp(int fd, pid_t pid)
81 {
82 	return ioctl(fd, TIOCSPGRP, &pid);
83 }
84 
85 #endif /* _NOLIBC_STDLIB_H */
86