-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathxlibc.c
More file actions
120 lines (106 loc) · 2.06 KB
/
Copy pathxlibc.c
File metadata and controls
120 lines (106 loc) · 2.06 KB
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
/*
* xlibc.c
* Standard libc function augmented with behavior expected by rset
*/
#include <err.h>
#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "missing/compat.h"
#include "rutils.h"
#include "xlibc.h"
/*
* Mimic dirname(3) on OpenBSD which does not modify it's input
*/
char *
xdirname(const char *path) {
static char dname[PATH_MAX];
str_cpy(dname, path, sizeof(dname));
return dirname(dname);
}
/*
* Mimic basename(3) on OpenBSD which does not modify it's input
*/
char *
xbasename(const char *path) {
static char dname[PATH_MAX];
str_cpy(dname, path, sizeof(dname));
return basename(dname);
}
/*
* Like stat(2) but retry until expected status
*/
int
xstat(const char *path, struct stat *sb, int expected) {
int i;
int ret;
struct timespec delay = { 0, 1000000 };
/* wait up to 0.5 seconds */
for (i = 0; i < 5; i++) {
ret = stat(path, sb);
if (ret == expected)
break;
nanosleep(&delay, NULL);
}
return ret;
}
/*
* Call strdup(3) and exit on failure
*/
void *
xstrdup(const char *s, const char *name) {
void *p;
p = strdup(s);
if (p == NULL)
err(1, "strdup > %s", name);
return p;
}
/*
* Call calloc(3) and exit on failure
*/
void *
xcalloc(size_t nmemb, size_t size, const char *name) {
void *p;
p = calloc(nmemb, size);
if (p == NULL)
err(1, "calloc > %s", name);
return p;
}
/*
* Call malloc(3) and exit on failure
*/
void *
xmalloc(size_t size, const char *name) {
void *p;
p = malloc(size);
if (p == NULL)
err(1, "malloc > %s", name);
return p;
}
/*
* Call realloc(3) and exit on failure or if request is over a limit
*/
void *
xrealloc(void *ptr, size_t size, const char *name) {
void *p;
if (size > REALLOC_MAX_SIZE)
errx(1, "realloc > %s exceeds %d", name, REALLOC_MAX_SIZE);
p = realloc(ptr, size);
if (p == NULL)
err(1, "realloc > %s", name);
return p;
}
/*
* Call pipe(2) and exit on failure
*/
int
xpipe(int *fildes, const char *name) {
int ret;
ret = pipe(fildes);
if (ret == -1)
err(1, "pipe < %s", name);
return ret;
}