-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylib.c
85 lines (79 loc) · 1.87 KB
/
mylib.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
78
79
80
81
82
83
84
85
/**
* A series of wrapper functions for syscalls needed by the shell.
*/
#include "mylib.h"
#include "syscall.h"
/**
* access:
* @brief wrapper function for access syscall.
*
* @param pathname - the path to check permissions
* @param mode - accessibility checks to conduct.
* @return 0 on success; -1 on error.
*/
int access(const char *pathname, int mode)
{
return syscall2(SYSACCESS, pathname, mode);
}
/**
* read:
* @brief wrapper function for read syscall.
*
* @param fd - file descriptor to read from.
* @param buf - the buffer to write read bytes to.
* @param count - the number of bytes to read.
*/
ssize_t read(int fd, void *buf, size_t count)
{
return (ssize_t) syscall3(SYSREAD, fd, buf, count);
}
/**
* fork:
* @brief wrapper function for fork syscall.
*
* @return pid of child process in parent; 0 in child; -1 on error.
*/
int fork()
{
return syscall0(SYSFORK);
}
/**
* waitpid:
* @brief wrapper function for wait4 syscall.
*
* @param pid - pid to wait for
* @param wstatus - pointer to wait status
* @param options - wait options
*
* @return pid of child on success; -1 on error.
*/
int waitpid(int pid, int *wstatus, int options)
{
return syscall4(SYSWAIT4, pid, wstatus, options, NULL);
}
/**
* execve:
* @brief wrapper function for execve syscall.
*
* @param filename - the executable to execute.
* @param argv - an array of arguments.
* @param envp - an array of environment variables.
* @return does not return on success; -1 on error.
*/
int execve(const char *filename, const char *const argv[], const char *const envp[])
{
return syscall3(SYSEXECVE, filename, argv, envp);
}
/**
* exit:
* @brief wrapper function for exit syscall.
*
* @param status - value to return on exit.
* @return does not return.
*/
void exit(int status)
{
syscall1(SYSEXIT, status);
/* does not return. */
__builtin_unreachable();
}