forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapunix.c
264 lines (220 loc) · 4.46 KB
/
wrapunix.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Socket wrapper functions.
* These could all go into separate files, so only the ones needed cause
* the corresponding function to be added to the executable. If sockets
* are a library (SVR4) this might make a difference (?), but if sockets
* are in the kernel (BSD) it doesn't matter.
*
* These wrapper functions also use the same prototypes as POSIX.1g,
* which might differ from many implementations (i.e., POSIX.1g specifies
* the fourth argument to getsockopt() as "void *", not "char *").
*
* If your system's headers are not correct [i.e., the Solaris 2.5
* <sys/socket.h> omits the "const" from the second argument to both
* bind() and connect()], you'll get warnings of the form:
*warning: passing arg 2 of `bind' discards `const' from pointer target type
*warning: passing arg 2 of `connect' discards `const' from pointer target type
*/
#include "unp.h"
void *
Calloc(size_t n, size_t size)
{
void *ptr;
if ( (ptr = calloc(n, size)) == NULL)
err_sys("calloc error");
return(ptr);
}
void
Close(int fd)
{
if (close(fd) == -1)
err_sys("close error");
}
void
Dup2(int fd1, int fd2)
{
if (dup2(fd1, fd2) == -1)
err_sys("dup2 error");
}
int
Fcntl(int fd, int cmd, int arg)
{
int n;
if ( (n = fcntl(fd, cmd, arg)) == -1)
err_sys("fcntl error");
return(n);
}
void
Gettimeofday(struct timeval *tv, void *foo)
{
if (gettimeofday(tv, foo) == -1)
err_sys("gettimeofday error");
return;
}
int
Ioctl(int fd, int request, void *arg)
{
int n;
if ( (n = ioctl(fd, request, arg)) == -1)
err_sys("ioctl error");
return(n); /* streamio of I_LIST returns value */
}
pid_t
Fork(void)
{
pid_t pid;
if ( (pid = fork()) == -1)
err_sys("fork error");
return(pid);
}
void *
Malloc(size_t size)
{
void *ptr;
if ( (ptr = malloc(size)) == NULL)
err_sys("malloc error");
return(ptr);
}
int
Mkstemp(char *template)
{
int i;
#ifdef HAVE_MKSTEMP
if ((i = mkstemp(template)) < 0)
err_quit("mkstemp error");
#else
if (mktemp(template) == NULL || template[0] == 0)
err_quit("mktemp error");
i = Open(template, O_CREAT | O_WRONLY, FILE_MODE);
#endif
return i;
}
#include <sys/mman.h>
void *
Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
void *ptr;
if ( (ptr = mmap(addr, len, prot, flags, fd, offset)) == ((void *) -1))
err_sys("mmap error");
return(ptr);
}
int
Open(const char *pathname, int oflag, mode_t mode)
{
int fd;
if ( (fd = open(pathname, oflag, mode)) == -1)
err_sys("open error for %s", pathname);
return(fd);
}
void
Pipe(int *fds)
{
if (pipe(fds) < 0)
err_sys("pipe error");
}
ssize_t
Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n;
if ( (n = read(fd, ptr, nbytes)) == -1)
err_sys("read error");
return(n);
}
void
Sigaddset(sigset_t *set, int signo)
{
if (sigaddset(set, signo) == -1)
err_sys("sigaddset error");
}
void
Sigdelset(sigset_t *set, int signo)
{
if (sigdelset(set, signo) == -1)
err_sys("sigdelset error");
}
void
Sigemptyset(sigset_t *set)
{
if (sigemptyset(set) == -1)
err_sys("sigemptyset error");
}
void
Sigfillset(sigset_t *set)
{
if (sigfillset(set) == -1)
err_sys("sigfillset error");
}
int
Sigismember(const sigset_t *set, int signo)
{
int n;
if ( (n = sigismember(set, signo)) == -1)
err_sys("sigismember error");
return(n);
}
void
Sigpending(sigset_t *set)
{
if (sigpending(set) == -1)
err_sys("sigpending error");
}
void
Sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
if (sigprocmask(how, set, oset) == -1)
err_sys("sigprocmask error");
}
char *
Strdup(const char *str)
{
char *ptr;
if ( (ptr = strdup(str)) == NULL)
err_sys("strdup error");
return(ptr);
}
long
Sysconf(int name)
{
long val;
errno = 0; /* in case sysconf() does not change this */
if ( (val = sysconf(name)) == -1)
err_sys("sysconf error");
return(val);
}
#ifdef HAVE_SYS_SYSCTL_H
void
Sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen)
{
if (sysctl(name, namelen, oldp, oldlenp, newp, newlen) == -1)
err_sys("sysctl error");
}
#endif
void
Unlink(const char *pathname)
{
if (unlink(pathname) == -1)
err_sys("unlink error for %s", pathname);
}
pid_t
Wait(int *iptr)
{
pid_t pid;
if ( (pid = wait(iptr)) == -1)
err_sys("wait error");
return(pid);
}
pid_t
Waitpid(pid_t pid, int *iptr, int options)
{
pid_t retpid;
if ( (retpid = waitpid(pid, iptr, options)) == -1)
err_sys("waitpid error");
return(retpid);
}
void
Write(int fd, void *ptr, size_t nbytes)
{
if (write(fd, ptr, nbytes) != nbytes)
err_sys("write error");
}