-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
168 lines (147 loc) · 3.37 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
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
#include "util.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <pwd.h>
#include <unistd.h>
#include "buf.h"
void debug(const char *format, ...) {
static FILE *debug_fp = NULL;
if (!debug_fp) {
time_t timestamp = time(0);
struct tm *now = localtime(×tamp);
char name[64];
strftime(name, sizeof(name), "/tmp/badavi_log.%Y%m%d-%H%M%S.txt", now);
debug_fp = fopen(name, "w");
assert(debug_fp);
}
va_list args;
va_start(args, format);
vfprintf(debug_fp, format, args);
va_end(args);
fflush(debug_fp);
}
void *xmalloc(size_t size) {
void *mem = malloc(size);
if (!mem) {
fprintf(stderr, "failed to allocate memory (%zu bytes)", size);
abort();
}
return mem;
}
void *xrealloc(void *ptr, size_t size) {
void *mem = realloc(ptr, size);
if (size && !mem) {
fprintf(stderr, "failed to allocate memory (%zu bytes)", size);
abort();
}
return mem;
}
char *xstrdup(const char *s) {
char *copy = strdup(s);
if (!copy) {
fprintf(stderr, "failed to allocate memory (%zu bytes)", strlen(s) + 1);
abort();
}
return copy;
}
bool strtoi(char *s, int *result) {
char *nptr;
long val = strtol(s, &nptr, 10);
if (*nptr == '\0') {
*result = (int) val;
return true;
}
return false;
}
char *strrep(char *s, char *from, char *to) {
struct buf *buf = buf_create(strlen(s));
size_t fromlen = strlen(from);
for (char *p = s; *p; ++p) {
if (!strncmp(p, from, fromlen)) {
buf_append(buf, to);
p += fromlen - 1;
} else {
buf_append_char(buf, *p);
}
}
char *result = buf->buf;
free(buf);
return result;
}
size_t strcnt(char *s, char c) {
size_t cnt = 0;
for (char *p = s; *p; ++p) {
if (*p == c) {
++cnt;
}
}
return cnt;
}
char *abspath(const char *path) {
char buf[PATH_MAX];
if (*path == '~') {
strcpy(buf, homedir());
strcat(buf, path + 1);
} else if (*path != '/') {
getcwd(buf, sizeof(buf));
strcat(buf, "/");
strcat(buf, path);
} else {
strcpy(buf, path);
}
char *result = xmalloc(strlen(buf) + 1);
strcpy(result, "/");
if (!strcmp(path, "/")) {
return result;
}
char *p = result;
char *prevslash = result;
char *component = strtok(buf, "/");
do {
if (!strcmp(component, "") ||
!strcmp(component, ".")) {
continue;
}
if (!strcmp(component, "..")) {
p = prevslash;
prevslash = result;
if (p != result) {
*p = '\0';
prevslash = strrchr(result, '/');
} else {
p[1] = '\0';
}
continue;
}
prevslash = p;
*p = '/';
size_t len = strlen(component);
memcpy(p + 1, component, len);
p += len + 1;
*p = '\0';
} while ((component = strtok(NULL, "/")));
return result;
}
const char *relpath(const char *path, const char *start) {
size_t startlen = strlen(start);
assert(start[startlen - 1] != '/');
if (!strncmp(path, start, startlen)) {
const char *rel = path + startlen;
return *rel == '/' ? rel + 1 : rel;
}
return path;
}
const char *homedir(void) {
const char *home = getenv("HOME");
return home ? home : getpwuid(getuid())->pw_dir;
}
struct region *region_set(struct region *region, size_t start, size_t end) {
region->start = min(start, end);
region->end = max(start, end);
return region;
}