landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4 :*/ |
Charlie Shepherd | 54524c9 | 2008-01-25 12:36:24 +0000 | [diff] [blame] | 2 | /* lib.c - reusable stuff. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 3 | * |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 4 | * Functions with the x prefix are wrappers for library functions. They either |
| 5 | * succeed or kill the program with an error message, but never return failure. |
| 6 | * They usually have the same arguments and return value as the function they |
| 7 | * wrap. |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright 2006 Rob Landley <rob@landley.net> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "toys.h" |
| 13 | |
Rob Landley | e15850a | 2007-11-19 01:51:00 -0600 | [diff] [blame] | 14 | // Strcpy with size checking: exit if there's not enough space for the string. |
| 15 | void xstrcpy(char *dest, char *src, size_t size) |
Rob Landley | 18d43ff | 2007-06-07 15:19:44 -0400 | [diff] [blame] | 16 | { |
Rob Landley | e15850a | 2007-11-19 01:51:00 -0600 | [diff] [blame] | 17 | if (strlen(src)+1 > size) error_exit("xstrcpy"); |
| 18 | strcpy(dest, src); |
Rob Landley | 18d43ff | 2007-06-07 15:19:44 -0400 | [diff] [blame] | 19 | } |
Rob Landley | 18d43ff | 2007-06-07 15:19:44 -0400 | [diff] [blame] | 20 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 21 | void verror_msg(char *msg, int err, va_list va) |
| 22 | { |
Rob Landley | 12138e4 | 2008-01-27 15:26:08 -0600 | [diff] [blame] | 23 | char *s = ": %s"; |
| 24 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 25 | fprintf(stderr, "%s: ", toys.which->name); |
Rob Landley | 12138e4 | 2008-01-27 15:26:08 -0600 | [diff] [blame] | 26 | if (msg) vfprintf(stderr, msg, va); |
| 27 | else s+=2; |
| 28 | if (err) fprintf(stderr, s, strerror(err)); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 29 | putc('\n', stderr); |
| 30 | } |
| 31 | |
| 32 | void error_msg(char *msg, ...) |
| 33 | { |
| 34 | va_list va; |
| 35 | |
| 36 | va_start(va, msg); |
| 37 | verror_msg(msg, 0, va); |
| 38 | va_end(va); |
| 39 | } |
| 40 | |
| 41 | void perror_msg(char *msg, ...) |
| 42 | { |
| 43 | va_list va; |
| 44 | |
| 45 | va_start(va, msg); |
| 46 | verror_msg(msg, errno, va); |
| 47 | va_end(va); |
| 48 | } |
| 49 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 50 | // Die with an error message. |
| 51 | void error_exit(char *msg, ...) |
| 52 | { |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 53 | va_list va; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 54 | |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -0500 | [diff] [blame] | 55 | if (CFG_HELP && toys.exithelp) { |
| 56 | *toys.optargs=*toys.argv; |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 57 | USE_HELP(help_main();) // dear gcc: shut up. |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -0500 | [diff] [blame] | 58 | fprintf(stderr,"\n"); |
| 59 | } |
| 60 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 61 | va_start(va, msg); |
| 62 | verror_msg(msg, 0, va); |
| 63 | va_end(va); |
| 64 | |
Rob Landley | aaffc07 | 2007-12-09 15:35:42 -0600 | [diff] [blame] | 65 | exit(!toys.exitval ? 1 : toys.exitval); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 68 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 69 | // Die with an error message and strerror(errno) |
| 70 | void perror_exit(char *msg, ...) |
| 71 | { |
| 72 | va_list va; |
| 73 | |
| 74 | va_start(va, msg); |
| 75 | verror_msg(msg, errno, va); |
| 76 | va_end(va); |
| 77 | |
Rob Landley | aaffc07 | 2007-12-09 15:35:42 -0600 | [diff] [blame] | 78 | exit(!toys.exitval ? 1 : toys.exitval); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 79 | } |
| 80 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 81 | // Die unless we can allocate memory. |
| 82 | void *xmalloc(size_t size) |
| 83 | { |
| 84 | void *ret = malloc(size); |
| 85 | if (!ret) error_exit("xmalloc"); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 86 | |
| 87 | return ret; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 88 | } |
| 89 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 90 | // Die unless we can allocate prezeroed memory. |
| 91 | void *xzalloc(size_t size) |
| 92 | { |
| 93 | void *ret = xmalloc(size); |
Rob Landley | f641854 | 2008-01-27 16:22:41 -0600 | [diff] [blame] | 94 | bzero(ret, size); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | // Die unless we can change the size of an existing allocation, possibly |
| 99 | // moving it. (Notice different arguments from libc function.) |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 100 | void *xrealloc(void *ptr, size_t size) |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 101 | { |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 102 | ptr = realloc(ptr, size); |
| 103 | if (!ptr) error_exit("xrealloc"); |
| 104 | |
| 105 | return ptr; |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 108 | // Die unless we can allocate a copy of this many bytes of string. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 109 | void *xstrndup(char *s, size_t n) |
| 110 | { |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 111 | char *ret = xmalloc(++n); |
| 112 | strncpy(ret, s, n); |
| 113 | ret[--n]=0; |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 114 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 115 | return ret; |
| 116 | } |
| 117 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 118 | // Die unless we can allocate a copy of this string. |
| 119 | void *xstrdup(char *s) |
| 120 | { |
Rob Landley | f641854 | 2008-01-27 16:22:41 -0600 | [diff] [blame] | 121 | return xstrndup(s, strlen(s)); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 122 | } |
| 123 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 124 | // Die unless we can allocate enough space to sprintf() into. |
| 125 | char *xmsprintf(char *format, ...) |
| 126 | { |
Rob Landley | 0d8dfb2 | 2007-06-15 15:16:46 -0400 | [diff] [blame] | 127 | va_list va, va2; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 128 | int len; |
| 129 | char *ret; |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 130 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 131 | va_start(va, format); |
Rob Landley | 0d8dfb2 | 2007-06-15 15:16:46 -0400 | [diff] [blame] | 132 | va_copy(va2, va); |
| 133 | |
| 134 | // How long is it? |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 135 | len = vsnprintf(0, 0, format, va); |
| 136 | len++; |
| 137 | va_end(va); |
| 138 | |
| 139 | // Allocate and do the sprintf() |
| 140 | ret = xmalloc(len); |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 141 | vsnprintf(ret, len, format, va2); |
Rob Landley | 0d8dfb2 | 2007-06-15 15:16:46 -0400 | [diff] [blame] | 142 | va_end(va2); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 147 | void xprintf(char *format, ...) |
| 148 | { |
| 149 | va_list va; |
| 150 | va_start(va, format); |
| 151 | |
| 152 | vprintf(format, va); |
| 153 | if (ferror(stdout)) perror_exit("write"); |
| 154 | } |
| 155 | |
Rob Landley | 5084fea | 2007-06-18 00:14:03 -0400 | [diff] [blame] | 156 | void xputs(char *s) |
| 157 | { |
| 158 | if (EOF == puts(s)) perror_exit("write"); |
| 159 | } |
| 160 | |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 161 | void xputc(char c) |
| 162 | { |
| 163 | if (EOF == fputc(c, stdout)) perror_exit("write"); |
| 164 | } |
| 165 | |
| 166 | void xflush(void) |
| 167 | { |
| 168 | if (fflush(stdout)) perror_exit("write");; |
| 169 | } |
| 170 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 171 | // Die unless we can exec argv[] (or run builtin command). Note that anything |
| 172 | // with a path isn't a builtin, so /bin/sh won't match the builtin sh. |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 173 | void xexec(char **argv) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 174 | { |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 175 | toy_exec(argv); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 176 | execvp(argv[0], argv); |
| 177 | error_exit("No %s", argv[0]); |
| 178 | } |
| 179 | |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 180 | void xaccess(char *path, int flags) |
| 181 | { |
Charlie Shepherd | 94dd3e7 | 2008-01-25 12:54:31 +0000 | [diff] [blame] | 182 | if (access(path, flags)) perror_exit("Can't access '%s'", path); |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Rob Landley | e745d8e | 2007-12-20 06:30:19 -0600 | [diff] [blame] | 185 | // Die unless we can delete a file. (File must exist to be deleted.) |
| 186 | void xunlink(char *path) |
| 187 | { |
| 188 | if (unlink(path)) perror_exit("unlink '%s'", path); |
| 189 | } |
| 190 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 191 | // Die unless we can open/create a file, returning file descriptor. |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 192 | int xcreate(char *path, int flags, int mode) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 193 | { |
| 194 | int fd = open(path, flags, mode); |
Rob Landley | 961e171 | 2007-11-04 15:31:06 -0600 | [diff] [blame] | 195 | if (fd == -1) perror_exit("%s", path); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 196 | return fd; |
| 197 | } |
| 198 | |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 199 | // Die unless we can open a file, returning file descriptor. |
| 200 | int xopen(char *path, int flags) |
| 201 | { |
| 202 | return xcreate(path, flags, 0); |
| 203 | } |
| 204 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 205 | void xclose(int fd) |
| 206 | { |
| 207 | if (close(fd)) perror_exit("xclose"); |
| 208 | } |
| 209 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 210 | // Die unless we can open/create a file, returning FILE *. |
| 211 | FILE *xfopen(char *path, char *mode) |
| 212 | { |
| 213 | FILE *f = fopen(path, mode); |
Charlie Shepherd | 94dd3e7 | 2008-01-25 12:54:31 +0000 | [diff] [blame] | 214 | if (!f) perror_exit("No file %s", path); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 215 | return f; |
| 216 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 217 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 218 | // Keep reading until full or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 219 | ssize_t readall(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 220 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 221 | size_t count = 0; |
Rob Landley | 15b2315 | 2008-07-18 04:15:59 -0500 | [diff] [blame] | 222 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 223 | while (count<len) { |
| 224 | int i = read(fd, buf+count, len-count); |
Rob Landley | 15b2315 | 2008-07-18 04:15:59 -0500 | [diff] [blame] | 225 | if (!i) break; |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 226 | if (i<0) return i; |
| 227 | count += i; |
| 228 | } |
| 229 | |
| 230 | return count; |
| 231 | } |
| 232 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 233 | // Keep writing until done or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 234 | ssize_t writeall(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 235 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 236 | size_t count = 0; |
| 237 | while (count<len) { |
| 238 | int i = write(fd, buf+count, len-count); |
| 239 | if (i<1) return i; |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 240 | count += i; |
| 241 | } |
| 242 | |
| 243 | return count; |
| 244 | } |
| 245 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 246 | // Die if there's an error other than EOF. |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 247 | size_t xread(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 248 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 249 | len = read(fd, buf, len); |
| 250 | if (len < 0) perror_exit("xread"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 251 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 252 | return len; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 255 | void xreadall(int fd, void *buf, size_t len) |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 256 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 257 | if (len != readall(fd, buf, len)) perror_exit("xreadall"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 258 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 259 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 260 | // There's no xwriteall(), just xwrite(). When we read, there may or may not |
| 261 | // be more data waiting. When we write, there is data and it had better go |
| 262 | // somewhere. |
| 263 | |
| 264 | void xwrite(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 265 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 266 | if (len != writeall(fd, buf, len)) perror_exit("xwrite"); |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 267 | } |
| 268 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 269 | char *xgetcwd(void) |
| 270 | { |
| 271 | char *buf = getcwd(NULL, 0); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 272 | if (!buf) perror_exit("xgetcwd"); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 273 | |
| 274 | return buf; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 277 | void xstat(char *path, struct stat *st) |
| 278 | { |
Rob Landley | f641854 | 2008-01-27 16:22:41 -0600 | [diff] [blame] | 279 | if(stat(path, st)) perror_exit("Can't stat %s", path); |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 282 | // Cannonicalizes path by removing ".", "..", and "//" elements. This is not |
Rob Landley | c6f481c | 2006-12-30 22:01:47 -0500 | [diff] [blame] | 283 | // the same as realpath(), where "dir/.." could wind up somewhere else by |
| 284 | // following symlinks. |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 285 | char *xabspath(char *path) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 286 | { |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 287 | char *from, *to; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 288 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 289 | // If this isn't an absolute path, make it one with cwd. |
| 290 | if (path[0]!='/') { |
| 291 | char *cwd=xgetcwd(); |
Rob Landley | f641854 | 2008-01-27 16:22:41 -0600 | [diff] [blame] | 292 | path = xmsprintf("%s/%s", cwd, path); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 293 | free(cwd); |
| 294 | } else path = xstrdup(path); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 295 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 296 | // Loop through path elements |
| 297 | from = to = path; |
| 298 | while (*from) { |
| 299 | |
| 300 | // Continue any current path component. |
| 301 | if (*from!='/') { |
| 302 | *(to++) = *(from++); |
| 303 | continue; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 304 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 305 | |
| 306 | // Skip duplicate slashes. |
| 307 | while (*from=='/') from++; |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 308 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 309 | // Start of a new filename. Handle . and .. |
| 310 | while (*from=='.') { |
| 311 | // Skip . |
| 312 | if (from[1]=='/') from += 2; |
| 313 | else if (!from[1]) from++; |
| 314 | // Back up for .. |
| 315 | else if (from[1]=='.') { |
| 316 | if (from[2]=='/') from +=3; |
| 317 | else if(!from[2]) from+=2; |
| 318 | else break; |
| 319 | while (to>path && *(--to)!='/'); |
| 320 | } else break; |
| 321 | } |
| 322 | // Add directory separator slash. |
| 323 | *(to++) = '/'; |
| 324 | } |
| 325 | *to = 0; |
| 326 | |
| 327 | return path; |
| 328 | } |
| 329 | |
Rob Landley | 988abb3 | 2008-05-12 00:52:27 -0500 | [diff] [blame] | 330 | void xchdir(char *path) |
| 331 | { |
| 332 | if (chdir(path)) error_exit("chdir '%s'"); |
| 333 | } |
| 334 | |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 335 | // Ensure entire path exists. |
| 336 | // If mode != -1 set permissions on newly created dirs. |
| 337 | // Requires that path string be writable (for temporary null terminators). |
| 338 | void xmkpath(char *path, int mode) |
| 339 | { |
| 340 | char *p, old; |
| 341 | mode_t mask; |
| 342 | int rc; |
| 343 | struct stat st; |
| 344 | |
| 345 | for (p = path; ; p++) { |
| 346 | if (!*p || *p == '/') { |
| 347 | old = *p; |
| 348 | *p = rc = 0; |
| 349 | if (stat(path, &st) || !S_ISDIR(st.st_mode)) { |
| 350 | if (mode != -1) { |
| 351 | mask=umask(0); |
| 352 | rc = mkdir(path, mode); |
| 353 | umask(mask); |
| 354 | } else rc = mkdir(path, 0777); |
| 355 | } |
| 356 | *p = old; |
Rob Landley | f641854 | 2008-01-27 16:22:41 -0600 | [diff] [blame] | 357 | if(rc) perror_exit("mkpath '%s'", path); |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 358 | } |
| 359 | if (!*p) break; |
| 360 | } |
| 361 | } |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 362 | // Find all file in a colon-separated path with access type "type" (generally |
| 363 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 364 | // order. |
| 365 | |
| 366 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 367 | { |
Rob Landley | 59f490c | 2008-05-17 17:52:51 -0500 | [diff] [blame] | 368 | struct string_list *rlist = NULL, **prlist=&rlist; |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 369 | char *cwd = xgetcwd(); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 370 | |
| 371 | for (;;) { |
| 372 | char *next = path ? index(path, ':') : NULL; |
| 373 | int len = next ? next-path : strlen(path); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 374 | struct string_list *rnext; |
| 375 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 376 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 377 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 378 | + (len ? len : strlen(cwd)) + 2); |
| 379 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 380 | else { |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 381 | char *res = rnext->str; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 382 | strncpy(res, path, len); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 383 | res += len; |
| 384 | *(res++) = '/'; |
| 385 | strcpy(res, filename); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 386 | } |
| 387 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 388 | // Confirm it's not a directory. |
| 389 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
Rob Landley | 59f490c | 2008-05-17 17:52:51 -0500 | [diff] [blame] | 390 | *prlist = rnext; |
| 391 | rnext->next = NULL; |
| 392 | prlist = &(rnext->next); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 393 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 394 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 395 | if (!next) break; |
| 396 | path += len; |
| 397 | path++; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 398 | } |
| 399 | free(cwd); |
| 400 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 401 | return rlist; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 402 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 403 | |
| 404 | // Convert unsigned int to ascii, writing into supplied buffer. A truncated |
| 405 | // result contains the first few digits of the result ala strncpy, and is |
| 406 | // always null terminated (unless buflen is 0). |
| 407 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
| 408 | { |
| 409 | int i, out = 0; |
| 410 | |
| 411 | if (buflen) { |
| 412 | for (i=1000000000; i; i/=10) { |
| 413 | int res = n/i; |
| 414 | |
| 415 | if ((res || out || i == 1) && --buflen>0) { |
| 416 | out++; |
| 417 | n -= res*i; |
| 418 | *buf++ = '0' + res; |
| 419 | } |
| 420 | } |
| 421 | *buf = 0; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Convert signed integer to ascii, using utoa_to_buf() |
| 426 | void itoa_to_buf(int n, char *buf, unsigned buflen) |
| 427 | { |
| 428 | if (buflen && n<0) { |
| 429 | n = -n; |
| 430 | *buf++ = '-'; |
| 431 | buflen--; |
| 432 | } |
| 433 | utoa_to_buf((unsigned)n, buf, buflen); |
| 434 | } |
| 435 | |
| 436 | // This static buffer is used by both utoa() and itoa(), calling either one a |
| 437 | // second time will overwrite the previous results. |
| 438 | // |
| 439 | // The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes. |
| 440 | // Note that int is always 32 bits on any remotely unix-like system, see |
| 441 | // http://www.unix.org/whitepapers/64bit.html for details. |
| 442 | |
| 443 | static char itoa_buf[12]; |
| 444 | |
| 445 | // Convert unsigned integer to ascii, returning a static buffer. |
| 446 | char *utoa(unsigned n) |
| 447 | { |
| 448 | utoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
| 449 | |
| 450 | return itoa_buf; |
| 451 | } |
| 452 | |
| 453 | char *itoa(int n) |
| 454 | { |
| 455 | itoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
| 456 | |
| 457 | return itoa_buf; |
| 458 | } |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 459 | |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 460 | // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
| 461 | // (zetta and yotta don't fit in 64 bits.) |
| 462 | long atolx(char *c) |
| 463 | { |
| 464 | char *suffixes="kmgtpe", *end; |
| 465 | long val = strtol(c, &c, 0); |
| 466 | |
Rob Landley | 6a6dee3 | 2007-11-04 15:32:59 -0600 | [diff] [blame] | 467 | if (*c) { |
| 468 | end = strchr(suffixes, tolower(*c)); |
| 469 | if (end) val *= 1024L<<((end-suffixes)*10); |
| 470 | } |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 471 | return val; |
| 472 | } |
| 473 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 474 | // Return how long the file at fd is, if there's any way to determine it. |
| 475 | off_t fdlength(int fd) |
| 476 | { |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 477 | off_t bottom = 0, top = 0, pos, old; |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 478 | int size; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 479 | |
| 480 | // If the ioctl works for this, return it. |
| 481 | |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 482 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 483 | |
| 484 | // If not, do a binary search for the last location we can read. (Some |
| 485 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 486 | // a CONFIG option... |
| 487 | |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 488 | old = lseek(fd, 0, SEEK_CUR); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 489 | do { |
| 490 | char temp; |
| 491 | |
| 492 | pos = bottom + (top - bottom) / 2; |
| 493 | |
| 494 | // If we can read from the current location, it's bigger. |
| 495 | |
Rob Landley | b3a3382 | 2007-01-25 16:10:37 -0500 | [diff] [blame] | 496 | if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 497 | if (bottom == top) bottom = top = (top+1) * 2; |
| 498 | else bottom = pos; |
| 499 | |
| 500 | // If we can't, it's smaller. |
| 501 | |
| 502 | } else { |
| 503 | if (bottom == top) { |
| 504 | if (!top) return 0; |
| 505 | bottom = top/2; |
| 506 | } else top = pos; |
| 507 | } |
| 508 | } while (bottom + 1 != top); |
| 509 | |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 510 | lseek(fd, old, SEEK_SET); |
| 511 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 512 | return pos + 1; |
| 513 | } |
| 514 | |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 515 | // This can return null (meaning file not found). It just won't return null |
| 516 | // for memory allocation reasons. |
| 517 | char *xreadlink(char *name) |
| 518 | { |
| 519 | int len, size = 0; |
| 520 | char *buf = 0; |
| 521 | |
| 522 | // Grow by 64 byte chunks until it's big enough. |
| 523 | for(;;) { |
| 524 | size +=64; |
| 525 | buf = xrealloc(buf, size); |
| 526 | len = readlink(name, buf, size); |
| 527 | |
| 528 | if (len<0) { |
| 529 | free(buf); |
| 530 | return 0; |
| 531 | } |
| 532 | if (len<size) { |
| 533 | buf[len]=0; |
| 534 | return buf; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
Rob Landley | b3a3382 | 2007-01-25 16:10:37 -0500 | [diff] [blame] | 539 | /* |
| 540 | This might be of use or might not. Unknown yet... |
| 541 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 542 | // Read contents of file as a single freshly allocated nul-terminated string. |
| 543 | char *readfile(char *name) |
| 544 | { |
| 545 | off_t len; |
| 546 | int fd; |
| 547 | char *buf; |
| 548 | |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 549 | fd = open(name, O_RDONLY); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 550 | if (fd == -1) return 0; |
| 551 | len = fdlength(fd); |
| 552 | buf = xmalloc(len+1); |
Rob Landley | e824ed1 | 2008-07-18 08:43:18 -0500 | [diff] [blame] | 553 | buf[xreadall(fd, buf, len)] = 0; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 554 | |
| 555 | return buf; |
| 556 | } |
| 557 | |
| 558 | char *xreadfile(char *name) |
| 559 | { |
| 560 | char *buf = readfile(name); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 561 | if (!buf) perror_exit("xreadfile %s", name); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 562 | return buf; |
| 563 | } |
| 564 | |
| 565 | */ |
| 566 | |
| 567 | // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently |
| 568 | // exists and is this executable. |
| 569 | void xpidfile(char *name) |
| 570 | { |
| 571 | char pidfile[256], spid[32]; |
| 572 | int i, fd; |
| 573 | pid_t pid; |
| 574 | |
| 575 | sprintf(pidfile, "/var/run/%s.pid", name); |
| 576 | // Try three times to open the sucker. |
| 577 | for (i=0; i<3; i++) { |
| 578 | fd = open(pidfile, O_CREAT|O_EXCL, 0644); |
| 579 | if (fd != -1) break; |
| 580 | |
| 581 | // If it already existed, read it. Loop for race condition. |
| 582 | fd = open(pidfile, O_RDONLY); |
| 583 | if (fd == -1) continue; |
| 584 | |
| 585 | // Is the old program still there? |
| 586 | spid[xread(fd, spid, sizeof(spid)-1)] = 0; |
| 587 | close(fd); |
| 588 | pid = atoi(spid); |
| 589 | if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); |
| 590 | |
| 591 | // An else with more sanity checking might be nice here. |
| 592 | } |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 593 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 594 | if (i == 3) error_exit("xpidfile %s", name); |
| 595 | |
| 596 | xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
| 597 | close(fd); |
| 598 | } |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 599 | |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 600 | // Iterate through an array of files, opening each one and calling a function |
| 601 | // on that filehandle and name. The special filename "-" means stdin if |
| 602 | // flags is O_RDONLY, stdout otherwise. An empty argument list calls |
| 603 | // function() on just stdin/stdout. |
| 604 | // |
| 605 | // Note: read only filehandles are automatically closed when function() |
| 606 | // returns, but writeable filehandles must be close by function() |
| 607 | void loopfiles_rw(char **argv, int flags, void (*function)(int fd, char *name)) |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 608 | { |
| 609 | int fd; |
| 610 | |
| 611 | // If no arguments, read from stdin. |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 612 | if (!*argv) function(flags ? 1 : 0, "-"); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 613 | else do { |
| 614 | // Filename "-" means read from stdin. |
| 615 | // Inability to open a file prints a warning, but doesn't exit. |
| 616 | |
| 617 | if (!strcmp(*argv,"-")) fd=0; |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 618 | else if (0>(fd = open(*argv, flags, 0666))) { |
Rob Landley | f641854 | 2008-01-27 16:22:41 -0600 | [diff] [blame] | 619 | perror_msg("%s", *argv); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 620 | toys.exitval = 1; |
Rob Landley | 3632d5d | 2008-01-01 02:39:29 -0600 | [diff] [blame] | 621 | continue; |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 622 | } |
| 623 | function(fd, *argv); |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 624 | if (!flags) close(fd); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 625 | } while (*++argv); |
| 626 | } |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 627 | |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 628 | // Call loopfiles_rw with O_RDONLY (common case). |
| 629 | void loopfiles(char **argv, void (*function)(int fd, char *name)) |
| 630 | { |
| 631 | loopfiles_rw(argv, O_RDONLY, function); |
| 632 | } |
| 633 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 634 | // Slow, but small. |
| 635 | |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 636 | char *get_rawline(int fd, long *plen, char end) |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 637 | { |
| 638 | char c, *buf = NULL; |
| 639 | long len = 0; |
| 640 | |
| 641 | for (;;) { |
| 642 | if (1>read(fd, &c, 1)) break; |
| 643 | if (!(len & 63)) buf=xrealloc(buf, len+64); |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 644 | if ((buf[len++]=c) == end) break; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 645 | } |
| 646 | if (buf) buf[len]=0; |
| 647 | if (plen) *plen = len; |
| 648 | |
| 649 | return buf; |
| 650 | } |
| 651 | |
| 652 | char *get_line(int fd) |
| 653 | { |
| 654 | long len; |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 655 | char *buf = get_rawline(fd, &len, '\n'); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 656 | |
| 657 | if (buf && buf[--len]=='\n') buf[len]=0; |
| 658 | |
| 659 | return buf; |
| 660 | } |
| 661 | |
| 662 | // Copy the rest of in to out and close both files. |
| 663 | |
| 664 | void xsendfile(int in, int out) |
| 665 | { |
| 666 | long len; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 667 | char buf[4096]; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 668 | |
| 669 | if (in<0) return; |
| 670 | for (;;) { |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 671 | len = xread(in, buf, 4096); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 672 | if (len<1) break; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 673 | xwrite(out, buf, len); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 674 | } |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Open a temporary file to copy an existing file into. |
| 678 | int copy_tempfile(int fdin, char *name, char **tempname) |
| 679 | { |
| 680 | struct stat statbuf; |
| 681 | int fd; |
| 682 | |
| 683 | *tempname = xstrndup(name, strlen(name)+6); |
| 684 | strcat(*tempname,"XXXXXX"); |
| 685 | if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); |
| 686 | |
| 687 | // Set permissions of output file |
| 688 | |
| 689 | fstat(fdin, &statbuf); |
| 690 | fchmod(fd, statbuf.st_mode); |
| 691 | |
| 692 | return fd; |
| 693 | } |
| 694 | |
| 695 | // Abort the copy and delete the temporary file. |
| 696 | void delete_tempfile(int fdin, int fdout, char **tempname) |
| 697 | { |
| 698 | close(fdin); |
| 699 | close(fdout); |
| 700 | unlink(*tempname); |
| 701 | free(*tempname); |
| 702 | *tempname = NULL; |
| 703 | } |
| 704 | |
| 705 | // Copy the rest of the data and replace the original with the copy. |
| 706 | void replace_tempfile(int fdin, int fdout, char **tempname) |
| 707 | { |
| 708 | char *temp = xstrdup(*tempname); |
| 709 | |
| 710 | temp[strlen(temp)-6]=0; |
| 711 | if (fdin != -1) { |
| 712 | xsendfile(fdin, fdout); |
| 713 | xclose(fdin); |
| 714 | } |
| 715 | xclose(fdout); |
| 716 | rename(*tempname, temp); |
| 717 | free(*tempname); |
| 718 | free(temp); |
| 719 | *tempname = NULL; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 720 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame^] | 721 | |
| 722 | // Create a 256 entry CRC32 lookup table. |
| 723 | |
| 724 | void crc_init(unsigned int *crc_table) |
| 725 | { |
| 726 | unsigned int i; |
| 727 | |
| 728 | // Init the CRC32 table (big endian) |
| 729 | for (i=0; i<256; i++) { |
| 730 | unsigned int j, c = i<<24; |
| 731 | for (j=8; j; j--) |
| 732 | c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
| 733 | crc_table[i] = c; |
| 734 | } |
| 735 | } |