-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstdlib.c
70 lines (60 loc) · 1022 Bytes
/
stdlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define ATEXIT_MAX 32
char **environ;
int abs(int n)
{
return n >= 0 ? n : -n;
}
long labs(long n)
{
return n >= 0 ? n : -n;
}
char *getenv(char *name)
{
char **p = environ;
int len = strlen(name);
for (; *p; p++)
if (!memcmp(name, *p, len) && (*p)[len] == '=')
return *p + len + 1;
return NULL;
}
int system(char *cmd)
{
char *argv[] = {"/bin/sh", "-c", cmd, NULL};
pid_t pid;
int ret;
pid = fork();
if (pid < 0)
return -1;
if (!pid) {
execv(argv[0], argv);
exit(1);
}
if (waitpid(pid, &ret, 0) != pid)
return -1;
return ret;
}
static void (*atexit_func[ATEXIT_MAX])(void);
static int atexit_cnt;
int atexit(void (*func)(void))
{
if (atexit_cnt >= ATEXIT_MAX)
return -1;
atexit_func[atexit_cnt++] = func;
return 0;
}
void __neatlibc_exit(void)
{
int i;
for (i = 0; i < atexit_cnt; i++)
atexit_func[i]();
}
void exit(int status)
{
__neatlibc_exit();
_exit(status);
}