-
Notifications
You must be signed in to change notification settings - Fork 16
/
unistd.c
77 lines (69 loc) · 1.16 KB
/
unistd.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
71
72
73
74
75
76
77
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int sleep(int n)
{
struct timespec req = {n, 0};
struct timespec rem;
if (nanosleep(&req, &rem))
return rem.tv_sec;
return 0;
}
#define EXECARGS (1 << 7)
int execle(char *path, ...)
{
va_list ap;
char *argv[EXECARGS];
char **envp;
int argc = 0;
va_start(ap, path);
while (argc + 1 < EXECARGS && (argv[argc] = va_arg(ap, char *)))
argc++;
envp = va_arg(ap, char **);
va_end(ap);
argv[argc] = NULL;
execve(path, argv, envp);
return -1;
}
int execvp(char *cmd, char *argv[])
{
char path[512];
char *p = getenv("PATH");
if (strchr(cmd, '/'))
return execve(cmd, argv, environ);
if (!p)
p = "/bin";
while (*p) {
char *s = path;
while (*p && *p != ':')
*s++ = *p++;
if (s != path)
*s++ = '/';
strcpy(s, cmd);
execve(path, argv, environ);
if (*p == ':')
p++;
}
return -1;
}
int execv(char *path, char *argv[])
{
return execve(path, argv, environ);
}
int wait(int *status)
{
return waitpid(-1, status, 0);
}
int raise(int sig)
{
return kill(getpid(), sig);
}
void abort(void)
{
raise(SIGABRT);
while (1)
;
}