landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4 :*/ |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 2 | /* functions.c - reusable stuff. |
| 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 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 14 | void verror_msg(char *msg, int err, va_list va) |
| 15 | { |
| 16 | fprintf(stderr, "%s: ", toys.which->name); |
| 17 | vfprintf(stderr, msg, va); |
| 18 | if (err) fprintf(stderr, ": %s", strerror(err)); |
| 19 | putc('\n', stderr); |
| 20 | } |
| 21 | |
| 22 | void error_msg(char *msg, ...) |
| 23 | { |
| 24 | va_list va; |
| 25 | |
| 26 | va_start(va, msg); |
| 27 | verror_msg(msg, 0, va); |
| 28 | va_end(va); |
| 29 | } |
| 30 | |
| 31 | void perror_msg(char *msg, ...) |
| 32 | { |
| 33 | va_list va; |
| 34 | |
| 35 | va_start(va, msg); |
| 36 | verror_msg(msg, errno, va); |
| 37 | va_end(va); |
| 38 | } |
| 39 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 40 | // Die with an error message. |
| 41 | void error_exit(char *msg, ...) |
| 42 | { |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 43 | va_list va; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 44 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 45 | va_start(va, msg); |
| 46 | verror_msg(msg, 0, va); |
| 47 | va_end(va); |
| 48 | |
| 49 | exit(toys.exitval); |
| 50 | } |
| 51 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 52 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 53 | // Die with an error message and strerror(errno) |
| 54 | void perror_exit(char *msg, ...) |
| 55 | { |
| 56 | va_list va; |
| 57 | |
| 58 | va_start(va, msg); |
| 59 | verror_msg(msg, errno, va); |
| 60 | va_end(va); |
| 61 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 62 | exit(toys.exitval); |
| 63 | } |
| 64 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 65 | // Stub until the online help system goes in. |
| 66 | void usage_exit(void) |
| 67 | { |
| 68 | exit(1); |
| 69 | } |
| 70 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 71 | // Like strncpy but always null terminated. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 72 | void strlcpy(char *dest, char *src, size_t size) |
| 73 | { |
| 74 | strncpy(dest,src,size); |
| 75 | dest[size-1] = 0; |
| 76 | } |
| 77 | |
| 78 | // Die unless we can allocate memory. |
| 79 | void *xmalloc(size_t size) |
| 80 | { |
| 81 | void *ret = malloc(size); |
| 82 | if (!ret) error_exit("xmalloc"); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 83 | |
| 84 | return ret; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 85 | } |
| 86 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 87 | // Die unless we can allocate prezeroed memory. |
| 88 | void *xzalloc(size_t size) |
| 89 | { |
| 90 | void *ret = xmalloc(size); |
| 91 | bzero(ret,size); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | // Die unless we can change the size of an existing allocation, possibly |
| 96 | // moving it. (Notice different arguments from libc function.) |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame^] | 97 | void *xrealloc(void *ptr, size_t size) |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 98 | { |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame^] | 99 | ptr = realloc(ptr, size); |
| 100 | if (!ptr) error_exit("xrealloc"); |
| 101 | |
| 102 | return ptr; |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 105 | // Die unless we can allocate a copy of this many bytes of string. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 106 | void *xstrndup(char *s, size_t n) |
| 107 | { |
| 108 | void *ret = xmalloc(++n); |
| 109 | strlcpy(ret, s, n); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 114 | // Die unless we can allocate a copy of this string. |
| 115 | void *xstrdup(char *s) |
| 116 | { |
| 117 | return xstrndup(s,strlen(s)); |
| 118 | } |
| 119 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 120 | // Die unless we can allocate enough space to sprintf() into. |
| 121 | char *xmsprintf(char *format, ...) |
| 122 | { |
| 123 | va_list va; |
| 124 | int len; |
| 125 | char *ret; |
| 126 | |
| 127 | // How long is it? |
| 128 | |
| 129 | va_start(va, format); |
| 130 | len = vsnprintf(0, 0, format, va); |
| 131 | len++; |
| 132 | va_end(va); |
| 133 | |
| 134 | // Allocate and do the sprintf() |
| 135 | ret = xmalloc(len); |
| 136 | va_start(va, format); |
| 137 | vsnprintf(ret, len, format, va); |
| 138 | va_end(va); |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 143 | void xprintf(char *format, ...) |
| 144 | { |
| 145 | va_list va; |
| 146 | va_start(va, format); |
| 147 | |
| 148 | vprintf(format, va); |
| 149 | if (ferror(stdout)) perror_exit("write"); |
| 150 | } |
| 151 | |
| 152 | void xputc(char c) |
| 153 | { |
| 154 | if (EOF == fputc(c, stdout)) perror_exit("write"); |
| 155 | } |
| 156 | |
| 157 | void xflush(void) |
| 158 | { |
| 159 | if (fflush(stdout)) perror_exit("write");; |
| 160 | } |
| 161 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 162 | // Die unless we can exec argv[] (or run builtin command). Note that anything |
| 163 | // 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] | 164 | void xexec(char **argv) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 165 | { |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 166 | toy_exec(argv); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 167 | execvp(argv[0], argv); |
| 168 | error_exit("No %s", argv[0]); |
| 169 | } |
| 170 | |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 171 | void xaccess(char *path, int flags) |
| 172 | { |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 173 | if (access(path, flags)) perror_exit("Can't access '%s'\n", path); |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 174 | } |
| 175 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 176 | // Die unless we can open/create a file, returning file descriptor. |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 177 | int xcreate(char *path, int flags, int mode) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 178 | { |
| 179 | int fd = open(path, flags, mode); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 180 | if (fd == -1) perror_exit("No file %s\n", path); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 181 | return fd; |
| 182 | } |
| 183 | |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 184 | // Die unless we can open a file, returning file descriptor. |
| 185 | int xopen(char *path, int flags) |
| 186 | { |
| 187 | return xcreate(path, flags, 0); |
| 188 | } |
| 189 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 190 | // Die unless we can open/create a file, returning FILE *. |
| 191 | FILE *xfopen(char *path, char *mode) |
| 192 | { |
| 193 | FILE *f = fopen(path, mode); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 194 | if (!f) perror_exit("No file %s\n", path); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 195 | return f; |
| 196 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 197 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 198 | // Keep reading until full or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 199 | ssize_t readall(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 200 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 201 | size_t count = 0; |
| 202 | while (count<len) { |
| 203 | int i = read(fd, buf+count, len-count); |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 204 | if (!i) return len; |
| 205 | if (i<0) return i; |
| 206 | count += i; |
| 207 | } |
| 208 | |
| 209 | return count; |
| 210 | } |
| 211 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 212 | // Keep writing until done or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 213 | ssize_t writeall(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 214 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 215 | size_t count = 0; |
| 216 | while (count<len) { |
| 217 | int i = write(fd, buf+count, len-count); |
| 218 | if (i<1) return i; |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 219 | count += i; |
| 220 | } |
| 221 | |
| 222 | return count; |
| 223 | } |
| 224 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 225 | // Die if there's an error other than EOF. |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 226 | size_t xread(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 227 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 228 | len = read(fd, buf, len); |
| 229 | if (len < 0) perror_exit("xread"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 230 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 231 | return len; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 234 | void xreadall(int fd, void *buf, size_t len) |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 235 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 236 | if (len != readall(fd, buf, len)) perror_exit("xreadall"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 237 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 238 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 239 | // There's no xwriteall(), just xwrite(). When we read, there may or may not |
| 240 | // be more data waiting. When we write, there is data and it had better go |
| 241 | // somewhere. |
| 242 | |
| 243 | void xwrite(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 244 | { |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 245 | if (len != writeall(fd, buf, len)) perror_exit("xwrite"); |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 246 | } |
| 247 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 248 | char *xgetcwd(void) |
| 249 | { |
| 250 | char *buf = getcwd(NULL, 0); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 251 | if (!buf) perror_exit("xgetcwd"); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 252 | |
| 253 | return buf; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 254 | } |
| 255 | |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 256 | void xstat(char *path, struct stat *st) |
| 257 | { |
| 258 | if(stat(path, st)) perror_exit("Can't stat %s\n",path); |
| 259 | } |
| 260 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 261 | // Cannonicalizes path by removing ".", "..", and "//" elements. This is not |
Rob Landley | c6f481c | 2006-12-30 22:01:47 -0500 | [diff] [blame] | 262 | // the same as realpath(), where "dir/.." could wind up somewhere else by |
| 263 | // following symlinks. |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 264 | char *xabspath(char *path) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 265 | { |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 266 | char *from, *to; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 267 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 268 | // If this isn't an absolute path, make it one with cwd. |
| 269 | if (path[0]!='/') { |
| 270 | char *cwd=xgetcwd(); |
| 271 | path = xmsprintf("%s/%s",cwd,path); |
| 272 | free(cwd); |
| 273 | } else path = xstrdup(path); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 274 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 275 | // Loop through path elements |
| 276 | from = to = path; |
| 277 | while (*from) { |
| 278 | |
| 279 | // Continue any current path component. |
| 280 | if (*from!='/') { |
| 281 | *(to++) = *(from++); |
| 282 | continue; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 283 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 284 | |
| 285 | // Skip duplicate slashes. |
| 286 | while (*from=='/') from++; |
| 287 | |
| 288 | // Start of a new filename. Handle . and .. |
| 289 | while (*from=='.') { |
| 290 | // Skip . |
| 291 | if (from[1]=='/') from += 2; |
| 292 | else if (!from[1]) from++; |
| 293 | // Back up for .. |
| 294 | else if (from[1]=='.') { |
| 295 | if (from[2]=='/') from +=3; |
| 296 | else if(!from[2]) from+=2; |
| 297 | else break; |
| 298 | while (to>path && *(--to)!='/'); |
| 299 | } else break; |
| 300 | } |
| 301 | // Add directory separator slash. |
| 302 | *(to++) = '/'; |
| 303 | } |
| 304 | *to = 0; |
| 305 | |
| 306 | return path; |
| 307 | } |
| 308 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 309 | // Find all file in a colon-separated path with access type "type" (generally |
| 310 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 311 | // order. |
| 312 | |
| 313 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 314 | { |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 315 | struct string_list *rlist = NULL; |
| 316 | char *cwd = xgetcwd(); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 317 | |
| 318 | for (;;) { |
| 319 | char *next = path ? index(path, ':') : NULL; |
| 320 | int len = next ? next-path : strlen(path); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 321 | struct string_list *rnext; |
| 322 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 323 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 324 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 325 | + (len ? len : strlen(cwd)) + 2); |
| 326 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 327 | else { |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 328 | char *res = rnext->str; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 329 | strncpy(res, path, len); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 330 | res += len; |
| 331 | *(res++) = '/'; |
| 332 | strcpy(res, filename); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 335 | // Confirm it's not a directory. |
| 336 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
| 337 | rnext->next = rlist; |
| 338 | rlist = rnext; |
| 339 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 340 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 341 | if (!next) break; |
| 342 | path += len; |
| 343 | path++; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 344 | } |
| 345 | free(cwd); |
| 346 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 347 | return rlist; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 348 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 349 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 350 | |
| 351 | // Convert unsigned int to ascii, writing into supplied buffer. A truncated |
| 352 | // result contains the first few digits of the result ala strncpy, and is |
| 353 | // always null terminated (unless buflen is 0). |
| 354 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
| 355 | { |
| 356 | int i, out = 0; |
| 357 | |
| 358 | if (buflen) { |
| 359 | for (i=1000000000; i; i/=10) { |
| 360 | int res = n/i; |
| 361 | |
| 362 | if ((res || out || i == 1) && --buflen>0) { |
| 363 | out++; |
| 364 | n -= res*i; |
| 365 | *buf++ = '0' + res; |
| 366 | } |
| 367 | } |
| 368 | *buf = 0; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Convert signed integer to ascii, using utoa_to_buf() |
| 373 | void itoa_to_buf(int n, char *buf, unsigned buflen) |
| 374 | { |
| 375 | if (buflen && n<0) { |
| 376 | n = -n; |
| 377 | *buf++ = '-'; |
| 378 | buflen--; |
| 379 | } |
| 380 | utoa_to_buf((unsigned)n, buf, buflen); |
| 381 | } |
| 382 | |
| 383 | // This static buffer is used by both utoa() and itoa(), calling either one a |
| 384 | // second time will overwrite the previous results. |
| 385 | // |
| 386 | // The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes. |
| 387 | // Note that int is always 32 bits on any remotely unix-like system, see |
| 388 | // http://www.unix.org/whitepapers/64bit.html for details. |
| 389 | |
| 390 | static char itoa_buf[12]; |
| 391 | |
| 392 | // Convert unsigned integer to ascii, returning a static buffer. |
| 393 | char *utoa(unsigned n) |
| 394 | { |
| 395 | utoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
| 396 | |
| 397 | return itoa_buf; |
| 398 | } |
| 399 | |
| 400 | char *itoa(int n) |
| 401 | { |
| 402 | itoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
| 403 | |
| 404 | return itoa_buf; |
| 405 | } |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 406 | |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 407 | // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
| 408 | // (zetta and yotta don't fit in 64 bits.) |
| 409 | long atolx(char *c) |
| 410 | { |
| 411 | char *suffixes="kmgtpe", *end; |
| 412 | long val = strtol(c, &c, 0); |
| 413 | |
| 414 | end = strchr(suffixes, tolower(*c)); |
| 415 | if (end) val *= 1024<<((end-suffixes)*10); |
| 416 | return val; |
| 417 | } |
| 418 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 419 | // Return how long the file at fd is, if there's any way to determine it. |
| 420 | off_t fdlength(int fd) |
| 421 | { |
| 422 | off_t bottom = 0, top = 0, pos; |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 423 | int size; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 424 | |
| 425 | // If the ioctl works for this, return it. |
| 426 | |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 427 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 428 | |
| 429 | // If not, do a binary search for the last location we can read. (Some |
| 430 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 431 | // a CONFIG option... |
| 432 | |
| 433 | do { |
| 434 | char temp; |
| 435 | |
| 436 | pos = bottom + (top - bottom) / 2; |
| 437 | |
| 438 | // If we can read from the current location, it's bigger. |
| 439 | |
Rob Landley | b3a3382 | 2007-01-25 16:10:37 -0500 | [diff] [blame] | 440 | if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 441 | if (bottom == top) bottom = top = (top+1) * 2; |
| 442 | else bottom = pos; |
| 443 | |
| 444 | // If we can't, it's smaller. |
| 445 | |
| 446 | } else { |
| 447 | if (bottom == top) { |
| 448 | if (!top) return 0; |
| 449 | bottom = top/2; |
| 450 | } else top = pos; |
| 451 | } |
| 452 | } while (bottom + 1 != top); |
| 453 | |
| 454 | return pos + 1; |
| 455 | } |
| 456 | |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame^] | 457 | // This can return null (meaning file not found). It just won't return null |
| 458 | // for memory allocation reasons. |
| 459 | char *xreadlink(char *name) |
| 460 | { |
| 461 | int len, size = 0; |
| 462 | char *buf = 0; |
| 463 | |
| 464 | // Grow by 64 byte chunks until it's big enough. |
| 465 | for(;;) { |
| 466 | size +=64; |
| 467 | buf = xrealloc(buf, size); |
| 468 | len = readlink(name, buf, size); |
| 469 | |
| 470 | if (len<0) { |
| 471 | free(buf); |
| 472 | return 0; |
| 473 | } |
| 474 | if (len<size) { |
| 475 | buf[len]=0; |
| 476 | return buf; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Rob Landley | b3a3382 | 2007-01-25 16:10:37 -0500 | [diff] [blame] | 481 | /* |
| 482 | This might be of use or might not. Unknown yet... |
| 483 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 484 | // Read contents of file as a single freshly allocated nul-terminated string. |
| 485 | char *readfile(char *name) |
| 486 | { |
| 487 | off_t len; |
| 488 | int fd; |
| 489 | char *buf; |
| 490 | |
| 491 | fd = open(pidfile, O_RDONLY); |
| 492 | if (fd == -1) return 0; |
| 493 | len = fdlength(fd); |
| 494 | buf = xmalloc(len+1); |
| 495 | buf[xread(fd, buf, len)] = 0; |
| 496 | |
| 497 | return buf; |
| 498 | } |
| 499 | |
| 500 | char *xreadfile(char *name) |
| 501 | { |
| 502 | char *buf = readfile(name); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 503 | if (!buf) perror_exit("xreadfile %s", name); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 504 | return buf; |
| 505 | } |
| 506 | |
| 507 | */ |
| 508 | |
| 509 | // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently |
| 510 | // exists and is this executable. |
| 511 | void xpidfile(char *name) |
| 512 | { |
| 513 | char pidfile[256], spid[32]; |
| 514 | int i, fd; |
| 515 | pid_t pid; |
| 516 | |
| 517 | sprintf(pidfile, "/var/run/%s.pid", name); |
| 518 | // Try three times to open the sucker. |
| 519 | for (i=0; i<3; i++) { |
| 520 | fd = open(pidfile, O_CREAT|O_EXCL, 0644); |
| 521 | if (fd != -1) break; |
| 522 | |
| 523 | // If it already existed, read it. Loop for race condition. |
| 524 | fd = open(pidfile, O_RDONLY); |
| 525 | if (fd == -1) continue; |
| 526 | |
| 527 | // Is the old program still there? |
| 528 | spid[xread(fd, spid, sizeof(spid)-1)] = 0; |
| 529 | close(fd); |
| 530 | pid = atoi(spid); |
| 531 | if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); |
| 532 | |
| 533 | // An else with more sanity checking might be nice here. |
| 534 | } |
| 535 | |
| 536 | if (i == 3) error_exit("xpidfile %s", name); |
| 537 | |
| 538 | xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
| 539 | close(fd); |
| 540 | } |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 541 | |
| 542 | // Create a dirtree node from a path. |
| 543 | |
| 544 | struct dirtree *read_dirtree_node(char *path) |
| 545 | { |
| 546 | struct dirtree *dt; |
| 547 | char *name; |
| 548 | |
| 549 | // Find last chunk of name. |
| 550 | |
| 551 | for (;;) { |
| 552 | name = strrchr(path, '/'); |
| 553 | |
| 554 | if (!name) name = path; |
| 555 | else { |
| 556 | if (*(name+1)) name++; |
| 557 | else { |
| 558 | *name=0; |
| 559 | continue; |
| 560 | } |
| 561 | } |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | dt = xzalloc(sizeof(struct dirtree)+strlen(name)+1); |
| 566 | xstat(path, &(dt->st)); |
| 567 | strcpy(dt->name, name); |
| 568 | |
| 569 | return dt; |
| 570 | } |
| 571 | |
| 572 | // Given a directory (in a writeable PATH_MAX buffer), recursively read in a |
| 573 | // directory tree. |
| 574 | |
Rob Landley | 720fc26 | 2007-02-04 19:14:58 -0500 | [diff] [blame] | 575 | struct dirtree *read_dirtree(char *path, struct dirtree *parent) |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 576 | { |
| 577 | struct dirtree *dt = NULL, **ddt = &dt; |
| 578 | DIR *dir; |
| 579 | int len = strlen(path); |
| 580 | |
| 581 | if (!(dir = opendir(path))) perror_msg("No %s", path); |
| 582 | |
| 583 | for (;;) { |
| 584 | struct dirent *entry = readdir(dir); |
| 585 | if (!entry) break; |
| 586 | |
| 587 | // Skip "." and ".." |
| 588 | if (entry->d_name[0]=='.') { |
| 589 | if (!entry->d_name[1]) continue; |
| 590 | if (entry->d_name[1]=='.' && !entry->d_name[2]) continue; |
| 591 | } |
| 592 | |
| 593 | snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name); |
| 594 | *ddt = read_dirtree_node(path); |
Rob Landley | 720fc26 | 2007-02-04 19:14:58 -0500 | [diff] [blame] | 595 | (*ddt)->parent = parent; |
| 596 | if (entry->d_type == DT_DIR) (*ddt)->child = read_dirtree(path, *ddt); |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 597 | ddt = &((*ddt)->next); |
| 598 | path[len]=0; |
| 599 | } |
| 600 | |
| 601 | return dt; |
| 602 | } |