Charlie Shepherd | 54524c9 | 2008-01-25 12:36:24 +0000 | [diff] [blame] | 1 | /* lib.c - reusable stuff. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 2 | * |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 3 | * Functions with the x prefix are wrappers for library functions. They either |
| 4 | * succeed or kill the program with an error message, but never return failure. |
| 5 | * They usually have the same arguments and return value as the function they |
| 6 | * wrap. |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 7 | * |
| 8 | * Copyright 2006 Rob Landley <rob@landley.net> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "toys.h" |
| 12 | |
Rob Landley | e15850a | 2007-11-19 01:51:00 -0600 | [diff] [blame] | 13 | // Strcpy with size checking: exit if there's not enough space for the string. |
| 14 | void xstrcpy(char *dest, char *src, size_t size) |
Rob Landley | 18d43ff | 2007-06-07 15:19:44 -0400 | [diff] [blame] | 15 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 16 | if (strlen(src)+1 > size) error_exit("xstrcpy"); |
| 17 | strcpy(dest, src); |
Rob Landley | 18d43ff | 2007-06-07 15:19:44 -0400 | [diff] [blame] | 18 | } |
Rob Landley | 18d43ff | 2007-06-07 15:19:44 -0400 | [diff] [blame] | 19 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 20 | void verror_msg(char *msg, int err, va_list va) |
| 21 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 22 | char *s = ": %s"; |
Rob Landley | 12138e4 | 2008-01-27 15:26:08 -0600 | [diff] [blame] | 23 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 24 | fprintf(stderr, "%s: ", toys.which->name); |
| 25 | if (msg) vfprintf(stderr, msg, va); |
| 26 | else s+=2; |
| 27 | if (err) fprintf(stderr, s, strerror(err)); |
| 28 | putc('\n', stderr); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void error_msg(char *msg, ...) |
| 32 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 33 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 34 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 35 | va_start(va, msg); |
| 36 | verror_msg(msg, 0, va); |
| 37 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void perror_msg(char *msg, ...) |
| 41 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 42 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 43 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 44 | va_start(va, msg); |
| 45 | verror_msg(msg, errno, va); |
| 46 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 47 | } |
| 48 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 49 | // Die with an error message. |
| 50 | void error_exit(char *msg, ...) |
| 51 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 52 | va_list va; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 53 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 54 | if (CFG_HELP && toys.exithelp) { |
| 55 | *toys.optargs=*toys.argv; |
| 56 | USE_HELP(help_main();) // dear gcc: shut up. |
| 57 | fprintf(stderr,"\n"); |
| 58 | } |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -0500 | [diff] [blame] | 59 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 60 | va_start(va, msg); |
| 61 | verror_msg(msg, 0, va); |
| 62 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 63 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 64 | exit(!toys.exitval ? 1 : toys.exitval); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 67 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 68 | // Die with an error message and strerror(errno) |
| 69 | void perror_exit(char *msg, ...) |
| 70 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 71 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 72 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 73 | va_start(va, msg); |
| 74 | verror_msg(msg, errno, va); |
| 75 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 76 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 77 | exit(!toys.exitval ? 1 : toys.exitval); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 78 | } |
| 79 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 80 | // Die unless we can allocate memory. |
| 81 | void *xmalloc(size_t size) |
| 82 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 83 | void *ret = malloc(size); |
| 84 | if (!ret) error_exit("xmalloc"); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 85 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 86 | return ret; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 87 | } |
| 88 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 89 | // Die unless we can allocate prezeroed memory. |
| 90 | void *xzalloc(size_t size) |
| 91 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 92 | void *ret = xmalloc(size); |
| 93 | memset(ret, 0, size); |
| 94 | return ret; |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Die unless we can change the size of an existing allocation, possibly |
| 98 | // moving it. (Notice different arguments from libc function.) |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 99 | void *xrealloc(void *ptr, size_t size) |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 100 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 101 | ptr = realloc(ptr, size); |
| 102 | if (!ptr) error_exit("xrealloc"); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 103 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 104 | return ptr; |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 107 | // Die unless we can allocate a copy of this many bytes of string. |
Rob Landley | 1e01cd1 | 2010-01-05 10:48:32 -0600 | [diff] [blame] | 108 | char *xstrndup(char *s, size_t n) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 109 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 110 | char *ret = xmalloc(++n); |
| 111 | strncpy(ret, s, n); |
| 112 | ret[--n]=0; |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 113 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 114 | return ret; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 117 | // Die unless we can allocate a copy of this string. |
Rob Landley | 1e01cd1 | 2010-01-05 10:48:32 -0600 | [diff] [blame] | 118 | char *xstrdup(char *s) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 119 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 120 | return xstrndup(s, strlen(s)); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 121 | } |
| 122 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 123 | // Die unless we can allocate enough space to sprintf() into. |
| 124 | char *xmsprintf(char *format, ...) |
| 125 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 126 | va_list va, va2; |
| 127 | int len; |
| 128 | char *ret; |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 129 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 130 | va_start(va, format); |
| 131 | va_copy(va2, va); |
Rob Landley | 0d8dfb2 | 2007-06-15 15:16:46 -0400 | [diff] [blame] | 132 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 133 | // How long is it? |
| 134 | len = vsnprintf(0, 0, format, va); |
| 135 | len++; |
| 136 | va_end(va); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 137 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 138 | // Allocate and do the sprintf() |
| 139 | ret = xmalloc(len); |
| 140 | vsnprintf(ret, len, format, va2); |
| 141 | va_end(va2); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 142 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 143 | return ret; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 146 | void xprintf(char *format, ...) |
| 147 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 148 | va_list va; |
| 149 | va_start(va, format); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 150 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 151 | vprintf(format, va); |
| 152 | if (ferror(stdout)) perror_exit("write"); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Rob Landley | 5084fea | 2007-06-18 00:14:03 -0400 | [diff] [blame] | 155 | void xputs(char *s) |
| 156 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 157 | if (EOF == puts(s)) perror_exit("write"); |
Rob Landley | 5084fea | 2007-06-18 00:14:03 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 160 | void xputc(char c) |
| 161 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 162 | if (EOF == fputc(c, stdout) || fflush(stdout)) perror_exit("write"); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void xflush(void) |
| 166 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 167 | if (fflush(stdout)) perror_exit("write");; |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 168 | } |
| 169 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 170 | // Die unless we can exec argv[] (or run builtin command). Note that anything |
| 171 | // 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] | 172 | void xexec(char **argv) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 173 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 174 | toy_exec(argv); |
| 175 | execvp(argv[0], argv); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 176 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 177 | perror_exit("exec %s", argv[0]); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 180 | void xaccess(char *path, int flags) |
| 181 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [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 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 188 | if (unlink(path)) perror_exit("unlink '%s'", path); |
Rob Landley | e745d8e | 2007-12-20 06:30:19 -0600 | [diff] [blame] | 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 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 194 | int fd = open(path, flags, mode); |
| 195 | if (fd == -1) perror_exit("%s", path); |
| 196 | return fd; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 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 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 202 | return xcreate(path, flags, 0); |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 205 | void xclose(int fd) |
| 206 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 207 | if (close(fd)) perror_exit("xclose"); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 208 | } |
| 209 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 210 | int xdup(int fd) |
| 211 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 212 | if (fd != -1) { |
| 213 | fd = dup(fd); |
| 214 | if (fd == -1) perror_exit("xdup"); |
| 215 | } |
| 216 | return fd; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 217 | } |
| 218 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 219 | // Die unless we can open/create a file, returning FILE *. |
| 220 | FILE *xfopen(char *path, char *mode) |
| 221 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 222 | FILE *f = fopen(path, mode); |
| 223 | if (!f) perror_exit("No file %s", path); |
| 224 | return f; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 225 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 226 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 227 | // Keep reading until full or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 228 | ssize_t readall(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 229 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 230 | size_t count = 0; |
Rob Landley | 15b2315 | 2008-07-18 04:15:59 -0500 | [diff] [blame] | 231 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 232 | while (count<len) { |
| 233 | int i = read(fd, buf+count, len-count); |
| 234 | if (!i) break; |
| 235 | if (i<0) return i; |
| 236 | count += i; |
| 237 | } |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 238 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 239 | return count; |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 242 | // Keep writing until done or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 243 | ssize_t writeall(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 244 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 245 | size_t count = 0; |
| 246 | while (count<len) { |
| 247 | int i = write(fd, buf+count, len-count); |
| 248 | if (i<1) return i; |
| 249 | count += i; |
| 250 | } |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 251 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 252 | return count; |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 255 | // Die if there's an error other than EOF. |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 256 | size_t xread(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 257 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 258 | ssize_t ret = read(fd, buf, len); |
| 259 | if (ret < 0) perror_exit("xread"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 260 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 261 | return ret; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 264 | void xreadall(int fd, void *buf, size_t len) |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 265 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 266 | if (len != readall(fd, buf, len)) perror_exit("xreadall"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 267 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 268 | |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 269 | // There's no xwriteall(), just xwrite(). When we read, there may or may not |
| 270 | // be more data waiting. When we write, there is data and it had better go |
| 271 | // somewhere. |
| 272 | |
| 273 | void xwrite(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 274 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 275 | if (len != writeall(fd, buf, len)) perror_exit("xwrite"); |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Rob Landley | 5247671 | 2009-01-18 16:19:25 -0600 | [diff] [blame] | 278 | // Die if lseek fails, probably due to being called on a pipe. |
| 279 | |
| 280 | off_t xlseek(int fd, off_t offset, int whence) |
| 281 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 282 | offset = lseek(fd, offset, whence); |
| 283 | if (offset<0) perror_exit("lseek"); |
Rob Landley | 5247671 | 2009-01-18 16:19:25 -0600 | [diff] [blame] | 284 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 285 | return offset; |
Rob Landley | 5247671 | 2009-01-18 16:19:25 -0600 | [diff] [blame] | 286 | } |
| 287 | |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 288 | off_t lskip(int fd, off_t offset) |
| 289 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 290 | off_t and = lseek(fd, offset, SEEK_CUR); |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 291 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 292 | if (and != -1 && offset >= lseek(fd, offset, SEEK_END) |
| 293 | && offset+and == lseek(fd, offset+and, SEEK_SET)) return 0; |
| 294 | else { |
| 295 | char buf[4096]; |
| 296 | while (offset>0) { |
| 297 | int try = offset>sizeof(buf) ? sizeof(buf) : offset, or; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 298 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 299 | or = readall(fd, buf, try); |
| 300 | if (or < 0) perror_msg("lskip to %lld", (long long)offset); |
| 301 | else offset -= try; |
| 302 | if (or < try) break; |
| 303 | } |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 304 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 305 | return offset; |
| 306 | } |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 307 | } |
| 308 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 309 | char *xgetcwd(void) |
| 310 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 311 | char *buf = getcwd(NULL, 0); |
| 312 | if (!buf) perror_exit("xgetcwd"); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 313 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 314 | return buf; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 317 | void xstat(char *path, struct stat *st) |
| 318 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 319 | if(stat(path, st)) perror_exit("Can't stat %s", path); |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 322 | // Cannonicalizes path by removing ".", "..", and "//" elements. This is not |
Rob Landley | c6f481c | 2006-12-30 22:01:47 -0500 | [diff] [blame] | 323 | // the same as realpath(), where "dir/.." could wind up somewhere else by |
| 324 | // following symlinks. |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 325 | char *xabspath(char *path) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 326 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 327 | char *from, *to; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 328 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 329 | // If this isn't an absolute path, make it one with cwd. |
| 330 | if (path[0]!='/') { |
| 331 | char *cwd=xgetcwd(); |
| 332 | path = xmsprintf("%s/%s", cwd, path); |
| 333 | free(cwd); |
| 334 | } else path = xstrdup(path); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 335 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 336 | // Loop through path elements |
| 337 | from = to = path; |
| 338 | while (*from) { |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 339 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 340 | // Continue any current path component. |
| 341 | if (*from!='/') { |
| 342 | *(to++) = *(from++); |
| 343 | continue; |
| 344 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 345 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 346 | // Skip duplicate slashes. |
| 347 | while (*from=='/') from++; |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 348 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 349 | // Start of a new filename. Handle . and .. |
| 350 | while (*from=='.') { |
| 351 | // Skip . |
| 352 | if (from[1]=='/') from += 2; |
| 353 | else if (!from[1]) from++; |
| 354 | // Back up for .. |
| 355 | else if (from[1]=='.') { |
| 356 | if (from[2]=='/') from +=3; |
| 357 | else if(!from[2]) from+=2; |
| 358 | else break; |
| 359 | while (to>path && *(--to)!='/'); |
| 360 | } else break; |
| 361 | } |
| 362 | // Add directory separator slash. |
| 363 | *(to++) = '/'; |
| 364 | } |
| 365 | *to = 0; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 366 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 367 | return path; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Rob Landley | eec4637 | 2012-06-01 13:50:41 -0500 | [diff] [blame] | 370 | // Resolve all symlinks, returning malloc() memory. |
| 371 | char *xrealpath(char *path) |
| 372 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 373 | char *new = realpath(path, NULL); |
| 374 | if (!new) perror_exit("realpath '%s'", path); |
| 375 | return new; |
Rob Landley | eec4637 | 2012-06-01 13:50:41 -0500 | [diff] [blame] | 376 | } |
| 377 | |
Rob Landley | 988abb3 | 2008-05-12 00:52:27 -0500 | [diff] [blame] | 378 | void xchdir(char *path) |
| 379 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 380 | if (chdir(path)) error_exit("chdir '%s'", path); |
Rob Landley | 988abb3 | 2008-05-12 00:52:27 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 383 | // Ensure entire path exists. |
| 384 | // If mode != -1 set permissions on newly created dirs. |
| 385 | // Requires that path string be writable (for temporary null terminators). |
| 386 | void xmkpath(char *path, int mode) |
| 387 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 388 | char *p, old; |
| 389 | mode_t mask; |
| 390 | int rc; |
| 391 | struct stat st; |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 392 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 393 | for (p = path; ; p++) { |
| 394 | if (!*p || *p == '/') { |
| 395 | old = *p; |
| 396 | *p = rc = 0; |
| 397 | if (stat(path, &st) || !S_ISDIR(st.st_mode)) { |
| 398 | if (mode != -1) { |
| 399 | mask=umask(0); |
| 400 | rc = mkdir(path, mode); |
| 401 | umask(mask); |
| 402 | } else rc = mkdir(path, 0777); |
| 403 | } |
| 404 | *p = old; |
| 405 | if(rc) perror_exit("mkpath '%s'", path); |
| 406 | } |
| 407 | if (!*p) break; |
| 408 | } |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 409 | } |
Rob Landley | e0377fb | 2010-01-05 12:17:05 -0600 | [diff] [blame] | 410 | |
| 411 | // setuid() can fail (for example, too many processes belonging to that user), |
| 412 | // which opens a security hole if the process continues as the original user. |
| 413 | |
| 414 | void xsetuid(uid_t uid) |
| 415 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 416 | if (setuid(uid)) perror_exit("xsetuid"); |
Rob Landley | e0377fb | 2010-01-05 12:17:05 -0600 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 420 | // Find all file in a colon-separated path with access type "type" (generally |
| 421 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 422 | // order. |
| 423 | |
| 424 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 425 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 426 | struct string_list *rlist = NULL, **prlist=&rlist; |
| 427 | char *cwd = xgetcwd(); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 428 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 429 | for (;;) { |
| 430 | char *next = path ? strchr(path, ':') : NULL; |
| 431 | int len = next ? next-path : strlen(path); |
| 432 | struct string_list *rnext; |
| 433 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 434 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 435 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 436 | + (len ? len : strlen(cwd)) + 2); |
| 437 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
| 438 | else { |
| 439 | char *res = rnext->str; |
| 440 | strncpy(res, path, len); |
| 441 | res += len; |
| 442 | *(res++) = '/'; |
| 443 | strcpy(res, filename); |
| 444 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 445 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 446 | // Confirm it's not a directory. |
| 447 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
| 448 | *prlist = rnext; |
| 449 | rnext->next = NULL; |
| 450 | prlist = &(rnext->next); |
| 451 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 452 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 453 | if (!next) break; |
| 454 | path += len; |
| 455 | path++; |
| 456 | } |
| 457 | free(cwd); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 458 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 459 | return rlist; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 460 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 461 | |
| 462 | // Convert unsigned int to ascii, writing into supplied buffer. A truncated |
| 463 | // result contains the first few digits of the result ala strncpy, and is |
| 464 | // always null terminated (unless buflen is 0). |
| 465 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
| 466 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 467 | int i, out = 0; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 468 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 469 | if (buflen) { |
| 470 | for (i=1000000000; i; i/=10) { |
| 471 | int res = n/i; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 472 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 473 | if ((res || out || i == 1) && --buflen>0) { |
| 474 | out++; |
| 475 | n -= res*i; |
| 476 | *buf++ = '0' + res; |
| 477 | } |
| 478 | } |
| 479 | *buf = 0; |
| 480 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Convert signed integer to ascii, using utoa_to_buf() |
| 484 | void itoa_to_buf(int n, char *buf, unsigned buflen) |
| 485 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 486 | if (buflen && n<0) { |
| 487 | n = -n; |
| 488 | *buf++ = '-'; |
| 489 | buflen--; |
| 490 | } |
| 491 | utoa_to_buf((unsigned)n, buf, buflen); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // This static buffer is used by both utoa() and itoa(), calling either one a |
| 495 | // second time will overwrite the previous results. |
| 496 | // |
| 497 | // The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes. |
| 498 | // Note that int is always 32 bits on any remotely unix-like system, see |
| 499 | // http://www.unix.org/whitepapers/64bit.html for details. |
| 500 | |
| 501 | static char itoa_buf[12]; |
| 502 | |
| 503 | // Convert unsigned integer to ascii, returning a static buffer. |
| 504 | char *utoa(unsigned n) |
| 505 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 506 | utoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 507 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 508 | return itoa_buf; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | char *itoa(int n) |
| 512 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 513 | itoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 514 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 515 | return itoa_buf; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 516 | } |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 517 | |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 518 | // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
| 519 | // (zetta and yotta don't fit in 64 bits.) |
Rob Landley | 5e6dca6 | 2012-02-09 06:09:27 -0600 | [diff] [blame] | 520 | long atolx(char *numstr) |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 521 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 522 | char *c, *suffixes="bkmgtpe", *end; |
| 523 | long val = strtol(numstr, &c, 0); |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 524 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 525 | if (*c) { |
| 526 | if (c != numstr && (end = strchr(suffixes, tolower(*c)))) { |
| 527 | int shift = end-suffixes; |
| 528 | if (shift--) val *= 1024L<<(shift*10); |
| 529 | } else { |
| 530 | while (isspace(*c)) c++; |
| 531 | if (*c) error_exit("not integer: %s", numstr); |
| 532 | } |
| 533 | } |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 534 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 535 | return val; |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 536 | } |
| 537 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 538 | int numlen(long l) |
| 539 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 540 | int len = 0; |
| 541 | while (l) { |
| 542 | l /= 10; |
| 543 | len++; |
| 544 | } |
| 545 | return len; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 548 | int stridx(char *haystack, char needle) |
| 549 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 550 | char *off; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 551 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 552 | if (!needle) return -1; |
| 553 | off = strchr(haystack, needle); |
| 554 | if (!off) return -1; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 555 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 556 | return off-haystack; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 557 | } |
| 558 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 559 | // Return how long the file at fd is, if there's any way to determine it. |
| 560 | off_t fdlength(int fd) |
| 561 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 562 | off_t bottom = 0, top = 0, pos, old; |
| 563 | int size; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 564 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 565 | // If the ioctl works for this, return it. |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 566 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 567 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 568 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 569 | // If not, do a binary search for the last location we can read. (Some |
| 570 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 571 | // a CONFIG option... |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 572 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 573 | old = lseek(fd, 0, SEEK_CUR); |
| 574 | do { |
| 575 | char temp; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 576 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 577 | pos = bottom + (top - bottom) / 2; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 578 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 579 | // If we can read from the current location, it's bigger. |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 580 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 581 | if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
| 582 | if (bottom == top) bottom = top = (top+1) * 2; |
| 583 | else bottom = pos; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 584 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 585 | // If we can't, it's smaller. |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 586 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 587 | } else { |
| 588 | if (bottom == top) { |
| 589 | if (!top) return 0; |
| 590 | bottom = top/2; |
| 591 | } else top = pos; |
| 592 | } |
| 593 | } while (bottom + 1 != top); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 594 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 595 | lseek(fd, old, SEEK_SET); |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 596 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 597 | return pos + 1; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 598 | } |
| 599 | |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 600 | // This can return null (meaning file not found). It just won't return null |
| 601 | // for memory allocation reasons. |
| 602 | char *xreadlink(char *name) |
| 603 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 604 | int len, size = 0; |
| 605 | char *buf = 0; |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 606 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 607 | // Grow by 64 byte chunks until it's big enough. |
| 608 | for(;;) { |
| 609 | size +=64; |
| 610 | buf = xrealloc(buf, size); |
| 611 | len = readlink(name, buf, size); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 612 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 613 | if (len<0) { |
| 614 | free(buf); |
| 615 | return 0; |
| 616 | } |
| 617 | if (len<size) { |
| 618 | buf[len]=0; |
| 619 | return buf; |
| 620 | } |
| 621 | } |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Rob Landley | b3a3382 | 2007-01-25 16:10:37 -0500 | [diff] [blame] | 624 | /* |
| 625 | This might be of use or might not. Unknown yet... |
| 626 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 627 | // Read contents of file as a single freshly allocated nul-terminated string. |
| 628 | char *readfile(char *name) |
| 629 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 630 | off_t len; |
| 631 | int fd; |
| 632 | char *buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 633 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 634 | fd = open(name, O_RDONLY); |
| 635 | if (fd == -1) return 0; |
| 636 | len = fdlength(fd); |
| 637 | buf = xmalloc(len+1); |
| 638 | buf[readall(fd, buf, len)] = 0; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 639 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 640 | return buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | char *xreadfile(char *name) |
| 644 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 645 | char *buf = readfile(name); |
| 646 | if (!buf) perror_exit("xreadfile %s", name); |
| 647 | return buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | */ |
| 651 | |
| 652 | // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently |
| 653 | // exists and is this executable. |
| 654 | void xpidfile(char *name) |
| 655 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 656 | char pidfile[256], spid[32]; |
| 657 | int i, fd; |
| 658 | pid_t pid; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 659 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 660 | sprintf(pidfile, "/var/run/%s.pid", name); |
| 661 | // Try three times to open the sucker. |
| 662 | for (i=0; i<3; i++) { |
| 663 | fd = open(pidfile, O_CREAT|O_EXCL, 0644); |
| 664 | if (fd != -1) break; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 665 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 666 | // If it already existed, read it. Loop for race condition. |
| 667 | fd = open(pidfile, O_RDONLY); |
| 668 | if (fd == -1) continue; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 669 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 670 | // Is the old program still there? |
| 671 | spid[xread(fd, spid, sizeof(spid)-1)] = 0; |
| 672 | close(fd); |
| 673 | pid = atoi(spid); |
| 674 | if (pid < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 675 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 676 | // An else with more sanity checking might be nice here. |
| 677 | } |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 678 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 679 | if (i == 3) error_exit("xpidfile %s", name); |
| 680 | |
| 681 | xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
| 682 | close(fd); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 683 | } |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 684 | |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 685 | // Iterate through an array of files, opening each one and calling a function |
| 686 | // on that filehandle and name. The special filename "-" means stdin if |
| 687 | // flags is O_RDONLY, stdout otherwise. An empty argument list calls |
| 688 | // function() on just stdin/stdout. |
| 689 | // |
| 690 | // Note: read only filehandles are automatically closed when function() |
| 691 | // returns, but writeable filehandles must be close by function() |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 692 | void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 693 | void (*function)(int fd, char *name)) |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 694 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 695 | int fd; |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 696 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 697 | // If no arguments, read from stdin. |
| 698 | if (!*argv) function(flags ? 1 : 0, "-"); |
| 699 | else do { |
| 700 | // Filename "-" means read from stdin. |
| 701 | // Inability to open a file prints a warning, but doesn't exit. |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 702 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 703 | if (!strcmp(*argv,"-")) fd=0; |
| 704 | else if (0>(fd = open(*argv, flags, permissions)) && !failok) { |
| 705 | perror_msg("%s", *argv); |
| 706 | toys.exitval = 1; |
| 707 | continue; |
| 708 | } |
| 709 | function(fd, *argv); |
| 710 | if (flags == O_RDONLY) close(fd); |
| 711 | } while (*++argv); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 712 | } |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 713 | |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 714 | // Call loopfiles_rw with O_RDONLY and !failok (common case). |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 715 | void loopfiles(char **argv, void (*function)(int fd, char *name)) |
| 716 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 717 | loopfiles_rw(argv, O_RDONLY, 0, 0, function); |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 718 | } |
| 719 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 720 | // Slow, but small. |
| 721 | |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 722 | char *get_rawline(int fd, long *plen, char end) |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 723 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 724 | char c, *buf = NULL; |
| 725 | long len = 0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 726 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 727 | for (;;) { |
| 728 | if (1>read(fd, &c, 1)) break; |
| 729 | if (!(len & 63)) buf=xrealloc(buf, len+65); |
| 730 | if ((buf[len++]=c) == end) break; |
| 731 | } |
| 732 | if (buf) buf[len]=0; |
| 733 | if (plen) *plen = len; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 734 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 735 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | char *get_line(int fd) |
| 739 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 740 | long len; |
| 741 | char *buf = get_rawline(fd, &len, '\n'); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 742 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 743 | if (buf && buf[--len]=='\n') buf[len]=0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 744 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 745 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // Copy the rest of in to out and close both files. |
| 749 | |
| 750 | void xsendfile(int in, int out) |
| 751 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 752 | long len; |
| 753 | char buf[4096]; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 754 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 755 | if (in<0) return; |
| 756 | for (;;) { |
| 757 | len = xread(in, buf, 4096); |
| 758 | if (len<1) break; |
| 759 | xwrite(out, buf, len); |
| 760 | } |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 761 | } |
| 762 | |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 763 | int wfchmodat(int fd, char *name, mode_t mode) |
| 764 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 765 | int rc = fchmodat(fd, name, mode, 0); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 766 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 767 | if (rc) { |
| 768 | perror_msg("chmod '%s' to %04o", name, mode); |
| 769 | toys.exitval=1; |
| 770 | } |
| 771 | return rc; |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 772 | } |
| 773 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 774 | static char *tempfile2zap; |
| 775 | static void tempfile_handler(int i) |
| 776 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 777 | if (1 < (long)tempfile2zap) unlink(tempfile2zap); |
| 778 | _exit(1); |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 781 | // Open a temporary file to copy an existing file into. |
| 782 | int copy_tempfile(int fdin, char *name, char **tempname) |
| 783 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 784 | struct stat statbuf; |
| 785 | int fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 786 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 787 | *tempname = xstrndup(name, strlen(name)+6); |
| 788 | strcat(*tempname,"XXXXXX"); |
| 789 | if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); |
| 790 | if (!tempfile2zap) sigatexit(tempfile_handler); |
| 791 | tempfile2zap = *tempname; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 792 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 793 | // Set permissions of output file |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 794 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 795 | fstat(fdin, &statbuf); |
| 796 | fchmod(fd, statbuf.st_mode); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 797 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 798 | return fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | // Abort the copy and delete the temporary file. |
| 802 | void delete_tempfile(int fdin, int fdout, char **tempname) |
| 803 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 804 | close(fdin); |
| 805 | close(fdout); |
| 806 | unlink(*tempname); |
| 807 | tempfile2zap = (char *)1; |
| 808 | free(*tempname); |
| 809 | *tempname = NULL; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Copy the rest of the data and replace the original with the copy. |
| 813 | void replace_tempfile(int fdin, int fdout, char **tempname) |
| 814 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 815 | char *temp = xstrdup(*tempname); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 816 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 817 | temp[strlen(temp)-6]=0; |
| 818 | if (fdin != -1) { |
| 819 | xsendfile(fdin, fdout); |
| 820 | xclose(fdin); |
| 821 | } |
| 822 | xclose(fdout); |
| 823 | rename(*tempname, temp); |
| 824 | tempfile2zap = (char *)1; |
| 825 | free(*tempname); |
| 826 | free(temp); |
| 827 | *tempname = NULL; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 828 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 829 | |
| 830 | // Create a 256 entry CRC32 lookup table. |
| 831 | |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 832 | void crc_init(unsigned int *crc_table, int little_endian) |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 833 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 834 | unsigned int i; |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 835 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 836 | // Init the CRC32 table (big endian) |
| 837 | for (i=0; i<256; i++) { |
| 838 | unsigned int j, c = little_endian ? i : i<<24; |
| 839 | for (j=8; j; j--) |
| 840 | if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1; |
| 841 | else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
| 842 | crc_table[i] = c; |
| 843 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 844 | } |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 845 | |
| 846 | // Quick and dirty query size of terminal, doesn't do ANSI probe fallback. |
| 847 | // set *x=0 and *y=0 before calling to detect failure to set either, or |
| 848 | // x=80 y=25 to provide defaults |
| 849 | |
| 850 | void terminal_size(unsigned *x, unsigned *y) |
| 851 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 852 | struct winsize ws; |
| 853 | int i; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 854 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 855 | //memset(&ws, 0, sizeof(ws)); |
| 856 | for (i=0; i<3; i++) { |
| 857 | if (ioctl(i, TIOCGWINSZ, &ws)) continue; |
| 858 | if (x) *x = ws.ws_col; |
| 859 | if (y) *y = ws.ws_row; |
| 860 | } |
| 861 | if (x) { |
| 862 | char *s = getenv("COLUMNS"); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 863 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 864 | i = s ? atoi(s) : 0; |
| 865 | if (i>0) *x = i; |
| 866 | } |
| 867 | if (y) { |
| 868 | char *s = getenv("ROWS"); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 869 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 870 | i = s ? atoi(s) : 0; |
| 871 | if (i>0) *y = i; |
| 872 | } |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | // This should use a raw tty, fixit later. |
Rob Landley | f793d53 | 2012-02-27 21:56:49 -0600 | [diff] [blame] | 876 | int yesno(char *prompt, int def) |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 877 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 878 | FILE *fps[] = {stdin, stdout, stderr}; |
| 879 | int i; |
| 880 | char buf; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 881 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 882 | for (i=0; i<3; i++) if (isatty(i)) break; |
| 883 | if (i == 3) return 1; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 884 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 885 | fprintf(fps[i], "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); |
| 886 | fflush(fps[i]); |
| 887 | while (fread(&buf, 1, 1, fps[i])) { |
| 888 | if (tolower(buf) == 'y') def = 1; |
| 889 | if (tolower(buf) == 'n') def = 0; |
| 890 | else if (!isspace(buf)) continue; |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 891 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 892 | break; |
| 893 | } |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 894 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 895 | return def; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 896 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 897 | |
| 898 | // Execute a callback for each PID that matches a process name from a list. |
Rob Landley | f42e11b | 2012-02-18 18:09:14 -0600 | [diff] [blame] | 899 | void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid)) |
| 900 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 901 | DIR *dp; |
| 902 | struct dirent *entry; |
| 903 | char cmd[sizeof(toybuf)], path[64]; |
| 904 | char **curname; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 905 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 906 | if (!(dp = opendir("/proc"))) perror_exit("opendir"); |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 907 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 908 | while ((entry = readdir(dp))) { |
| 909 | int fd, n; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 910 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 911 | if (!isdigit(*entry->d_name)) continue; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 912 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 913 | if (sizeof(path) <= snprintf(path, sizeof(path), "/proc/%s/cmdline", |
| 914 | entry->d_name)) continue; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 915 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 916 | if (-1 == (fd=open(path, O_RDONLY))) continue; |
| 917 | n = read(fd, cmd, sizeof(cmd)); |
| 918 | close(fd); |
| 919 | if (n<1) continue; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 920 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 921 | for (curname = names; *curname; curname++) |
| 922 | if (!strcmp(basename(cmd), *curname)) callback(atol(entry->d_name)); |
| 923 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 924 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 925 | closedir(dp); |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 926 | } |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 927 | |
| 928 | struct signame { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 929 | int num; |
| 930 | char *name; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | // Signals required by POSIX 2008: |
| 934 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html |
| 935 | |
| 936 | #define SIGNIFY(x) {SIG##x, #x} |
| 937 | |
| 938 | static struct signame signames[] = { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 939 | SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), |
| 940 | SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), |
| 941 | SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), |
| 942 | SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), |
| 943 | SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 944 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 945 | // Start of non-terminal signals |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 946 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 947 | SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), |
| 948 | SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG) |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 949 | }; |
| 950 | |
| 951 | // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR) |
| 952 | // obsolete: SIGNIFY(PROF) SIGNIFY(POLL) |
| 953 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 954 | // Install the same handler on every signal that defaults to killing the process |
| 955 | void sigatexit(void *handler) |
| 956 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 957 | int i; |
| 958 | for (i=0; signames[i].num != SIGCHLD; i++) signal(signames[i].num, handler); |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 959 | } |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 960 | // Convert name to signal number. If name == NULL print names. |
| 961 | int sig_to_num(char *pidstr) |
| 962 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 963 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 964 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 965 | if (pidstr) { |
| 966 | char *s; |
| 967 | i = strtol(pidstr, &s, 10); |
| 968 | if (!*s) return i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 969 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 970 | if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; |
| 971 | } |
| 972 | for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++) |
| 973 | if (!pidstr) xputs(signames[i].name); |
| 974 | else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 975 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 976 | return -1; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | char *num_to_sig(int sig) |
| 980 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 981 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 982 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 983 | for (i=0; i<sizeof(signames)/sizeof(struct signame); i++) |
| 984 | if (signames[i].num == sig) return signames[i].name; |
| 985 | return NULL; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 986 | } |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 987 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 988 | // premute mode bits based on posix mode strings. |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 989 | mode_t string_to_mode(char *modestr, mode_t mode) |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 990 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 991 | char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu"; |
| 992 | char *s, *str = modestr; |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 993 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 994 | // Handle octal mode |
| 995 | if (isdigit(*str)) { |
| 996 | mode = strtol(str, &s, 8); |
| 997 | if (*s || (mode & ~(07777))) goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 998 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 999 | return mode; |
| 1000 | } |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 1001 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1002 | // Gaze into the bin of permission... |
| 1003 | for (;;) { |
| 1004 | int i, j, dowho, dohow, dowhat, amask; |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 1005 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1006 | dowho = dohow = dowhat = amask = 0; |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 1007 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1008 | // Find the who, how, and what stanzas, in that order |
| 1009 | while (*str && (s = strchr(whos, *str))) { |
| 1010 | dowho |= 1<<(s-whos); |
| 1011 | str++; |
| 1012 | } |
| 1013 | // If who isn't specified, like "a" but honoring umask. |
| 1014 | if (!dowho) { |
| 1015 | dowho = 8; |
| 1016 | umask(amask=umask(0)); |
| 1017 | } |
| 1018 | if (!*str || !(s = strchr(hows, *str))) goto barf; |
| 1019 | dohow = *(str++); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 1020 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1021 | if (!dohow) goto barf; |
| 1022 | while (*str && (s = strchr(whats, *str))) { |
| 1023 | dowhat |= 1<<(s-whats); |
| 1024 | str++; |
| 1025 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1026 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1027 | // Convert X to x for directory or if already executable somewhere |
| 1028 | if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1029 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1030 | // Copy mode from another category? |
| 1031 | if (!dowhat && *str && (s = strchr(whys, *str))) { |
| 1032 | dowhat = (mode>>(3*(s-whys)))&7; |
| 1033 | str++; |
| 1034 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1035 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1036 | // Are we ready to do a thing yet? |
| 1037 | if (*str && *(str++) != ',') goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1038 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1039 | // Ok, apply the bits to the mode. |
| 1040 | for (i=0; i<4; i++) { |
| 1041 | for (j=0; j<3; j++) { |
| 1042 | mode_t bit = 0; |
| 1043 | int where = 1<<((3*i)+j); |
Rob Landley | 31f49e7 | 2012-07-21 22:45:05 -0500 | [diff] [blame] | 1044 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1045 | if (amask & where) continue; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1046 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1047 | // Figure out new value at this location |
| 1048 | if (i == 3) { |
| 1049 | // suid/sticky bit. |
| 1050 | if (j) { |
| 1051 | if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++; |
| 1052 | } else if (dowhat & 16) bit++; |
| 1053 | } else { |
| 1054 | if (!(dowho&(8|(1<<i)))) continue; |
| 1055 | if (dowhat&(1<<j)) bit++; |
| 1056 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1057 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1058 | // When selection active, modify bit |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1059 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1060 | if (dohow == '=' || (bit && dohow == '-')) mode &= ~where; |
| 1061 | if (bit && dohow != '-') mode |= where; |
| 1062 | } |
| 1063 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1064 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1065 | if (!*str) break; |
| 1066 | } |
| 1067 | return mode; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 1068 | barf: |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1069 | error_exit("bad mode '%s'", modestr); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 1070 | } |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 1071 | |
| 1072 | |
| 1073 | char* make_human_readable(unsigned long long size, unsigned long unit) |
| 1074 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1075 | unsigned int frac = 0; |
| 1076 | if(unit) { |
| 1077 | size = (size/(unit)) + (size%(unit)?1:0); |
| 1078 | return xmsprintf("%llu", size); |
| 1079 | } |
| 1080 | else { |
| 1081 | static char units[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'}; |
| 1082 | int index = 0; |
| 1083 | while(size >= 1024) { |
| 1084 | frac = size%1024; |
| 1085 | size /= 1024; |
| 1086 | index++; |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 1087 | } |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1088 | frac = (frac/102) + ((frac%102)?1:0); |
| 1089 | if(frac >= 10) { |
| 1090 | size += 1; |
| 1091 | frac = 0; |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 1092 | } |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame^] | 1093 | if(frac) return xmsprintf("%llu.%u%c", size, frac, units[index]); |
| 1094 | else return xmsprintf("%llu%c", size, units[index]); |
| 1095 | } |
| 1096 | return NULL; //not reached |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 1097 | } |