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