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