forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
134 lines (115 loc) · 2.58 KB
/
util.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
/* util.c -- the kitchen sink ($Revision: 1.2 $) */
#include "es.h"
#if !HAVE_STRERROR
/* strerror -- turn an error code into a string */
static char *strerror(int n) {
extern int sys_nerr;
extern char *sys_errlist[];
if (n > sys_nerr)
return NULL;
return sys_errlist[n];
}
#endif
/* esstrerror -- a wrapper around sterror(3) */
extern char *esstrerror(int n) {
char *error = strerror(n);
if (error == NULL)
return "unknown error";
return error;
}
/* uerror -- print a unix error, our version of perror */
extern void uerror(char *s) {
if (s != NULL)
eprint("%s: %s\n", s, esstrerror(errno));
else
eprint("%s\n", esstrerror(errno));
}
/* isabsolute -- test to see if pathname begins with "/", "./", or "../" */
extern Boolean isabsolute(char *path) {
return path[0] == '/'
|| (path[0] == '.' && (path[1] == '/'
|| (path[1] == '.' && path[2] == '/')));
}
/* streq2 -- is a string equal to the concatenation of two strings? */
extern Boolean streq2(const char *s, const char *t1, const char *t2) {
int c;
assert(s != NULL && t1 != NULL && t2 != NULL);
while ((c = *t1++) != '\0')
if (c != *s++)
return FALSE;
while ((c = *t2++) != '\0')
if (c != *s++)
return FALSE;
return *s == '\0';
}
/*
* safe interface to malloc and friends
*/
/* ealloc -- error checked malloc */
extern void *ealloc(size_t n) {
extern void *malloc(size_t n);
void *p = malloc(n);
if (p == NULL) {
uerror("malloc");
exit(1);
}
return p;
}
/* erealloc -- error checked realloc */
extern void *erealloc(void *p, size_t n) {
extern void *realloc(void *, size_t);
if (p == NULL)
return ealloc(n);
p = realloc(p, n);
if (p == NULL) {
uerror("realloc");
exit(1);
}
return p;
}
/* efree -- error checked free */
extern void efree(void *p) {
extern void free(void *);
assert(p != NULL);
free(p);
}
/*
* private interfaces to system calls
*/
extern void ewrite(int fd, const char *buf, size_t n) {
volatile long i, remain;
const char *volatile bufp = buf;
for (i = 0, remain = n; remain > 0; bufp += i, remain -= i) {
interrupted = FALSE;
if (!setjmp(slowlabel)) {
slow = TRUE;
if (interrupted)
break;
else if ((i = write(fd, bufp, remain)) <= 0)
break; /* abort silently on errors in write() */
} else
break;
slow = FALSE;
}
slow = FALSE;
SIGCHK();
}
extern long eread(int fd, char *buf, size_t n) {
long r;
interrupted = FALSE;
if (!setjmp(slowlabel)) {
slow = TRUE;
if (!interrupted)
r = read(fd, buf, n);
else
r = -2;
} else
r = -2;
slow = FALSE;
if (r == -2) {
errno = EINTR;
r = -1;
}
SIGCHK();
return r;
}