forked from fabrizzio-gz/basic-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers.c
95 lines (74 loc) · 1.97 KB
/
wrappers.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
86
87
88
89
90
91
92
93
94
95
#include "wrappers.h"
void unix_error(char *msg) {
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}
void Sigemptyset(sigset_t *set) {
if (sigemptyset(set) < 0)
unix_error("Sigemptyset error");
return;
}
void Sigfillset(sigset_t *set) {
if (sigfillset(set) < 0)
unix_error("Sigfillset error");
return;
}
void Sigaddset(sigset_t *set, int signum) {
if (sigaddset(set, signum) < 0)
unix_error("Sigaddset error");
return;
}
void Sigdelset(sigset_t *set, int signum) {
if (sigdelset(set, signum) < 0)
unix_error("Sigdelset error");
return;
}
void Sigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
if (sigprocmask(how, set, oldset) < 0)
unix_error("Sigprocmask error");
return;
}
void Write(int fd, const void *buf, size_t count) {
if (write(fd, buf, count) < 0) {
write(STDOUT_FILENO, "write error", 12);
_exit(1);
}
}
char *Fgets(char *ptr, int n, FILE *stream) {
char *rptr;
if (((rptr = fgets(ptr, n, stream)) == NULL) && ferror(stream))
unix_error("Fgets error");
return rptr;
}
handler_t *Signal(int signum, handler_t *handler) {
struct sigaction action, old_action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask); /* Block sigs of type being handled */
action.sa_flags = SA_RESTART; /* Restart syscalls if possible */
if (sigaction(signum, &action, &old_action) < 0)
unix_error("Signal error");
return (old_action.sa_handler);
}
void Setpgid(pid_t pid, pid_t pgid) {
int rc;
if ((rc = setpgid(pid, pgid)) < 0)
unix_error("Setpgid error");
return;
}
void Kill(pid_t pid, int signum) {
int rc;
if ((rc = kill(pid, signum)) < 0)
unix_error("Kill error");
}
pid_t Fork(void) {
pid_t pid;
if ((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}
pid_t Waitpid(pid_t pid, int *iptr, int options) {
pid_t retpid;
if ((retpid = waitpid(pid, iptr, options)) < 0)
unix_error("Waitpid error");
return(retpid);
}