| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
Ü |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <usual/fileutil.h> |
| 20 |
|
| 21 |
#ifdef HAVE_SYS_MMAN_H |
| 22 |
#include <sys/mman.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <sys/stat.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
void *load_file(const char *fn, size_t *len_p) |
| 34 |
{ |
| 35 |
struct stat st; |
| 36 |
char *buf = NULL; |
| 37 |
int res; |
| 38 |
FILE *f; |
| 39 |
int save_errno; |
| 40 |
|
| 41 |
f = fopen(fn, "r"); |
| 42 |
if (!f) |
| 43 |
return NULL; |
| 44 |
|
| 45 |
res = fstat(fileno(f), &st); |
| 46 |
if (res < 0) { |
| 47 |
save_errno = errno; |
| 48 |
fclose(f); |
| 49 |
errno = save_errno; |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
buf = malloc(st.st_size + 1); |
| 54 |
if (!buf) { |
| 55 |
save_errno = errno; |
| 56 |
fclose(f); |
| 57 |
errno = save_errno; |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
if ((res = fread(buf, 1, st.st_size, f)) < 0) { |
| 62 |
save_errno = errno; |
| 63 |
free(buf); |
| 64 |
fclose(f); |
| 65 |
errno = save_errno; |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
|
| 69 |
fclose(f); |
| 70 |
|
| 71 |
buf[res] = 0; |
| 72 |
if (len_p) |
| 73 |
*len_p = res; |
| 74 |
return buf; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
bool foreach_line(const char *fn, procline_cb proc_line, void *arg) |
| 82 |
{ |
| 83 |
char *ln = NULL; |
| 84 |
size_t len = 0; |
| 85 |
ssize_t res; |
| 86 |
FILE *f = fopen(fn, "rb"); |
| 87 |
bool ok = false; |
| 88 |
if (!f) |
| 89 |
return false; |
| 90 |
while (1) { |
| 91 |
res = getline(&ln, &len, f); |
| 92 |
if (res < 0) { |
| 93 |
if (feof(f)) |
| 94 |
ok = true; |
| 95 |
break; |
| 96 |
} |
| 97 |
if (!proc_line(arg, ln, res)) |
| 98 |
break; |
| 99 |
} |
| 100 |
fclose(f); |
| 101 |
free(ln); |
| 102 |
return ok; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
ssize_t file_size(const char *fn) |
| 110 |
{ |
| 111 |
struct stat st; |
| 112 |
if (stat(fn, &st) < 0) |
| 113 |
return -1; |
| 114 |
return st.st_size; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#ifdef HAVE_MMAP |
| 122 |
|
| 123 |
int map_file(struct MappedFile *m, const char *fname, int rw) |
| 124 |
{ |
| 125 |
struct stat st; |
| 126 |
m->fd = open(fname, rw ? O_RDWR : O_RDONLY); |
| 127 |
if (m->fd < 0) |
| 128 |
return -1; |
| 129 |
if (fstat(m->fd, &st) < 0) { |
| 130 |
close(m->fd); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
m->len = st.st_size; |
| 134 |
m->ptr = mmap(NULL, m->len, PROT_READ | (rw ? PROT_WRITE : 0), |
| 135 |
MAP_SHARED, m->fd, 0); |
| 136 |
if (m->ptr == MAP_FAILED) { |
| 137 |
close(m->fd); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
void unmap_file(struct MappedFile *m) |
| 144 |
{ |
| 145 |
munmap(m->ptr, m->len); |
| 146 |
close(m->fd); |
| 147 |
m->ptr = NULL; |
| 148 |
m->fd = 0; |
| 149 |
} |
| 150 |
|
| 151 |
#endif |
| 152 |
|
| 153 |
|
| 154 |
#ifndef HAVE_GETLINE |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
int getline(char **line_p, size_t *size_p, void *_f) |
| 159 |
{ |
| 160 |
FILE *f = _f; |
| 161 |
char *p; |
| 162 |
int len = 0; |
| 163 |
|
| 164 |
if (!*line_p || *size_p < 128) { |
| 165 |
p = realloc(*line_p, 512); |
| 166 |
if (!p) return -1; |
| 167 |
*line_p = p; |
| 168 |
*size_p = 512; |
| 169 |
} |
| 170 |
|
| 171 |
while (1) { |
| 172 |
p = fgets(*line_p + len, *size_p - len, f); |
| 173 |
if (!p) |
| 174 |
return len ? len : -1; |
| 175 |
len += strlen(p); |
| 176 |
if ((*line_p)[len - 1] == '\n') |
| 177 |
return len; |
| 178 |
p = realloc(*line_p, *size_p * 2); |
| 179 |
if (!p) |
| 180 |
return -1; |
| 181 |
*line_p = p; |
| 182 |
*size_p *= 2; |
| 183 |
} |
| 184 |
} |
| 185 |
#endif |