forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
104 lines (86 loc) · 2.15 KB
/
util.c
File metadata and controls
104 lines (86 loc) · 2.15 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
/* 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");
esexit(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");
esexit(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) {
if ((i = write(fd, bufp, remain)) < 0 && errno != EINTR)
break; /* abort silently on errors in write() */
}
}