Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 1 | /* lib.c - various reusable stuff. |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2006 Rob Landley <rob@landley.net> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "toys.h" |
| 7 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 8 | void verror_msg(char *msg, int err, va_list va) |
| 9 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 10 | char *s = ": %s"; |
Rob Landley | 12138e4 | 2008-01-27 15:26:08 -0600 | [diff] [blame] | 11 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 12 | fprintf(stderr, "%s: ", toys.which->name); |
| 13 | if (msg) vfprintf(stderr, msg, va); |
| 14 | else s+=2; |
| 15 | if (err) fprintf(stderr, s, strerror(err)); |
Rob Landley | e5354ca | 2015-09-11 16:35:14 -0500 | [diff] [blame] | 16 | if (msg || err) putc('\n', stderr); |
Rob Landley | 662a267 | 2013-01-02 02:00:35 -0600 | [diff] [blame] | 17 | if (!toys.exitval) toys.exitval++; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 18 | } |
| 19 | |
Rob Landley | e5354ca | 2015-09-11 16:35:14 -0500 | [diff] [blame] | 20 | // These functions don't collapse together because of the va_stuff. |
| 21 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 22 | void error_msg(char *msg, ...) |
| 23 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 24 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 25 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 26 | va_start(va, msg); |
| 27 | verror_msg(msg, 0, va); |
| 28 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void perror_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, errno, va); |
| 37 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 38 | } |
| 39 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 40 | // Die with an error message. |
| 41 | void error_exit(char *msg, ...) |
| 42 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 43 | va_list va; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 44 | |
Rob Landley | e5354ca | 2015-09-11 16:35:14 -0500 | [diff] [blame] | 45 | va_start(va, msg); |
| 46 | verror_msg(msg, 0, va); |
| 47 | va_end(va); |
| 48 | |
| 49 | xexit(); |
| 50 | } |
| 51 | |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 52 | // Die with an error message and strerror(errno) |
| 53 | void perror_exit(char *msg, ...) |
| 54 | { |
| 55 | va_list va; |
| 56 | |
| 57 | va_start(va, msg); |
| 58 | verror_msg(msg, errno, va); |
| 59 | va_end(va); |
| 60 | |
| 61 | xexit(); |
| 62 | } |
| 63 | |
Rob Landley | e5354ca | 2015-09-11 16:35:14 -0500 | [diff] [blame] | 64 | // Exit with an error message after showing help text. |
| 65 | void help_exit(char *msg, ...) |
| 66 | { |
| 67 | va_list va; |
| 68 | |
Rob Landley | ab33097 | 2017-06-05 21:22:02 -0500 | [diff] [blame] | 69 | if (CFG_TOYBOX_HELP) |
| 70 | fprintf(stderr, "See %s --help\n", toys.which->name); |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -0500 | [diff] [blame] | 71 | |
Rob Landley | 8941e5f | 2015-09-29 05:32:57 -0500 | [diff] [blame] | 72 | if (msg) { |
| 73 | va_start(va, msg); |
| 74 | verror_msg(msg, 0, va); |
| 75 | va_end(va); |
| 76 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 77 | |
Rob Landley | 5583030 | 2013-06-16 19:59:51 -0500 | [diff] [blame] | 78 | xexit(); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 81 | // If you want to explicitly disable the printf() behavior (because you're |
| 82 | // printing user-supplied data, or because android's static checker produces |
| 83 | // false positives for 'char *s = x ? "blah1" : "blah2"; printf(s);' and it's |
| 84 | // -Werror there for policy reasons). |
| 85 | void error_msg_raw(char *msg) |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 86 | { |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 87 | error_msg("%s", msg); |
| 88 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 89 | |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 90 | void perror_msg_raw(char *msg) |
| 91 | { |
| 92 | perror_msg("%s", msg); |
| 93 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 94 | |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 95 | void error_exit_raw(char *msg) |
| 96 | { |
| 97 | error_exit("%s", msg); |
| 98 | } |
| 99 | |
| 100 | void perror_exit_raw(char *msg) |
| 101 | { |
Elliott Hughes | e894358 | 2016-01-09 11:47:35 -0800 | [diff] [blame] | 102 | perror_exit("%s", msg); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 103 | } |
| 104 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 105 | // Keep reading until full or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 106 | ssize_t readall(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 107 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 108 | size_t count = 0; |
Rob Landley | 15b2315 | 2008-07-18 04:15:59 -0500 | [diff] [blame] | 109 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 110 | while (count<len) { |
Rob Landley | dbbd3d6 | 2013-12-08 13:26:05 -0600 | [diff] [blame] | 111 | int i = read(fd, (char *)buf+count, len-count); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 112 | if (!i) break; |
| 113 | if (i<0) return i; |
| 114 | count += i; |
| 115 | } |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 116 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 117 | return count; |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 120 | // Keep writing until done or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 121 | ssize_t writeall(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 122 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 123 | size_t count = 0; |
| 124 | while (count<len) { |
Rob Landley | 1fb3ae7 | 2014-02-16 11:09:23 -0600 | [diff] [blame] | 125 | int i = write(fd, count+(char *)buf, len-count); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 126 | if (i<1) return i; |
| 127 | count += i; |
| 128 | } |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 129 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 130 | return count; |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 133 | // skip this many bytes of input. Return 0 for success, >0 means this much |
| 134 | // left after input skipped. |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 135 | off_t lskip(int fd, off_t offset) |
| 136 | { |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 137 | off_t cur = lseek(fd, 0, SEEK_CUR); |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 138 | |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 139 | if (cur != -1) { |
| 140 | off_t end = lseek(fd, 0, SEEK_END) - cur; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 141 | |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 142 | if (end > 0 && end < offset) return offset - end; |
| 143 | end = offset+cur; |
| 144 | if (end == lseek(fd, end, SEEK_SET)) return 0; |
| 145 | perror_exit("lseek"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 146 | } |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 147 | |
| 148 | while (offset>0) { |
| 149 | int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or; |
| 150 | |
| 151 | or = readall(fd, libbuf, try); |
| 152 | if (or < 0) perror_exit("lskip to %lld", (long long)offset); |
Rob Landley | 4dd800c | 2014-02-28 23:10:03 -0600 | [diff] [blame] | 153 | else offset -= or; |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 154 | if (or < try) break; |
| 155 | } |
| 156 | |
| 157 | return offset; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 160 | // flags: 1=make last dir (with mode lastmode, otherwise skips last component) |
| 161 | // 2=make path (already exists is ok) |
| 162 | // 4=verbose |
| 163 | // returns 0 = path ok, 1 = error |
| 164 | int mkpathat(int atfd, char *dir, mode_t lastmode, int flags) |
| 165 | { |
| 166 | struct stat buf; |
| 167 | char *s; |
| 168 | |
| 169 | // mkdir -p one/two/three is not an error if the path already exists, |
| 170 | // but is if "three" is a file. The others we dereference and catch |
| 171 | // not-a-directory along the way, but the last one we must explicitly |
| 172 | // test for. Might as well do it up front. |
| 173 | |
| 174 | if (!fstatat(atfd, dir, &buf, 0) && !S_ISDIR(buf.st_mode)) { |
| 175 | errno = EEXIST; |
| 176 | return 1; |
| 177 | } |
| 178 | |
Rob Landley | 02f5a30 | 2014-03-24 06:26:49 -0500 | [diff] [blame] | 179 | for (s = dir; ;s++) { |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 180 | char save = 0; |
| 181 | mode_t mode = (0777&~toys.old_umask)|0300; |
| 182 | |
Rob Landley | 02f5a30 | 2014-03-24 06:26:49 -0500 | [diff] [blame] | 183 | // find next '/', but don't try to mkdir "" at start of absolute path |
| 184 | if (*s == '/' && (flags&2) && s != dir) { |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 185 | save = *s; |
| 186 | *s = 0; |
| 187 | } else if (*s) continue; |
| 188 | |
| 189 | // Use the mode from the -m option only for the last directory. |
| 190 | if (!save) { |
| 191 | if (flags&1) mode = lastmode; |
| 192 | else break; |
| 193 | } |
| 194 | |
| 195 | if (mkdirat(atfd, dir, mode)) { |
| 196 | if (!(flags&2) || errno != EEXIST) return 1; |
| 197 | } else if (flags&4) |
| 198 | fprintf(stderr, "%s: created directory '%s'\n", toys.which->name, dir); |
Rob Landley | 2c1cf4a | 2015-01-18 14:06:14 -0600 | [diff] [blame] | 199 | |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 200 | if (!(*s = save)) break; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 206 | // Split a path into linked list of components, tracking head and tail of list. |
| 207 | // Filters out // entries with no contents. |
| 208 | struct string_list **splitpath(char *path, struct string_list **list) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 209 | { |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 210 | char *new = path; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 211 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 212 | *list = 0; |
| 213 | do { |
| 214 | int len; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 215 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 216 | if (*path && *path != '/') continue; |
| 217 | len = path-new; |
| 218 | if (len > 0) { |
| 219 | *list = xmalloc(sizeof(struct string_list) + len + 1); |
| 220 | (*list)->next = 0; |
Rob Landley | 87fbe12 | 2014-12-13 11:56:41 -0600 | [diff] [blame] | 221 | memcpy((*list)->str, new, len); |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 222 | (*list)->str[len] = 0; |
| 223 | list = &(*list)->next; |
| 224 | } |
| 225 | new = path+1; |
| 226 | } while (*path++); |
| 227 | |
| 228 | return list; |
| 229 | } |
| 230 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 231 | // Find all file in a colon-separated path with access type "type" (generally |
| 232 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 233 | // order. |
| 234 | |
| 235 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 236 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 237 | struct string_list *rlist = NULL, **prlist=&rlist; |
Rob Landley | 0aad9e4 | 2014-06-24 08:19:24 -0500 | [diff] [blame] | 238 | char *cwd; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 239 | |
Rob Landley | 0aad9e4 | 2014-06-24 08:19:24 -0500 | [diff] [blame] | 240 | if (!path) return 0; |
| 241 | |
| 242 | cwd = xgetcwd(); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 243 | for (;;) { |
Rob Landley | 0aad9e4 | 2014-06-24 08:19:24 -0500 | [diff] [blame] | 244 | char *next = strchr(path, ':'); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 245 | int len = next ? next-path : strlen(path); |
| 246 | struct string_list *rnext; |
| 247 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 248 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 249 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 250 | + (len ? len : strlen(cwd)) + 2); |
| 251 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
| 252 | else { |
| 253 | char *res = rnext->str; |
Rob Landley | 87fbe12 | 2014-12-13 11:56:41 -0600 | [diff] [blame] | 254 | |
| 255 | memcpy(res, path, len); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 256 | res += len; |
| 257 | *(res++) = '/'; |
| 258 | strcpy(res, filename); |
| 259 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 260 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 261 | // Confirm it's not a directory. |
| 262 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
| 263 | *prlist = rnext; |
| 264 | rnext->next = NULL; |
| 265 | prlist = &(rnext->next); |
| 266 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 267 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 268 | if (!next) break; |
| 269 | path += len; |
| 270 | path++; |
| 271 | } |
| 272 | free(cwd); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 273 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 274 | return rlist; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 275 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 276 | |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 277 | long long estrtol(char *str, char **end, int base) |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 278 | { |
| 279 | errno = 0; |
| 280 | |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 281 | return strtoll(str, end, base); |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 282 | } |
| 283 | |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 284 | long long xstrtol(char *str, char **end, int base) |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 285 | { |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 286 | long long l = estrtol(str, end, base); |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 287 | |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 288 | if (errno) perror_exit_raw(str); |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 289 | |
| 290 | return l; |
| 291 | } |
| 292 | |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 293 | // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
| 294 | // (zetta and yotta don't fit in 64 bits.) |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 295 | long long atolx(char *numstr) |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 296 | { |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 297 | char *c = numstr, *suffixes="cbkmgtpe", *end; |
| 298 | long long val; |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 299 | |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 300 | val = xstrtol(numstr, &c, 0); |
Rob Landley | 93e044c | 2016-07-13 13:46:50 -0500 | [diff] [blame] | 301 | if (c != numstr && *c && (end = strchr(suffixes, tolower(*c)))) { |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 302 | int shift = end-suffixes-2; |
| 303 | |
| 304 | if (shift >= 0) { |
| 305 | if (toupper(*++c)=='d') do val *= 1000; while (shift--); |
| 306 | else val *= 1024LL<<(shift*10); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 307 | } |
| 308 | } |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 309 | while (isspace(*c)) c++; |
| 310 | if (c==numstr || *c) error_exit("not integer: %s", numstr); |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 311 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 312 | return val; |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 315 | long long atolx_range(char *numstr, long long low, long long high) |
Rob Landley | b5e7416 | 2013-11-28 21:11:34 -0600 | [diff] [blame] | 316 | { |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 317 | long long val = atolx(numstr); |
Rob Landley | b5e7416 | 2013-11-28 21:11:34 -0600 | [diff] [blame] | 318 | |
Rob Landley | 30454a4 | 2016-07-12 14:51:51 -0500 | [diff] [blame] | 319 | if (val < low) error_exit("%lld < %lld", val, low); |
| 320 | if (val > high) error_exit("%lld > %lld", val, high); |
Rob Landley | b5e7416 | 2013-11-28 21:11:34 -0600 | [diff] [blame] | 321 | |
| 322 | return val; |
| 323 | } |
| 324 | |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 325 | int stridx(char *haystack, char needle) |
| 326 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 327 | char *off; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 328 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 329 | if (!needle) return -1; |
| 330 | off = strchr(haystack, needle); |
| 331 | if (!off) return -1; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 332 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 333 | return off-haystack; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Rob Landley | 7cc95a7 | 2015-08-01 11:48:59 -0500 | [diff] [blame] | 336 | char *strlower(char *s) |
| 337 | { |
| 338 | char *try, *new; |
| 339 | |
| 340 | if (!CFG_TOYBOX_I18N) { |
| 341 | try = new = xstrdup(s); |
| 342 | for (; *s; s++) *(new++) = tolower(*s); |
| 343 | } else { |
| 344 | // I can't guarantee the string _won't_ expand during reencoding, so...? |
| 345 | try = new = xmalloc(strlen(s)*2+1); |
| 346 | |
| 347 | while (*s) { |
| 348 | wchar_t c; |
| 349 | int len = mbrtowc(&c, s, MB_CUR_MAX, 0); |
| 350 | |
| 351 | if (len < 1) *(new++) = *(s++); |
| 352 | else { |
| 353 | s += len; |
| 354 | // squash title case too |
| 355 | c = towlower(c); |
| 356 | |
| 357 | // if we had a valid utf8 sequence, convert it to lower case, and can't |
| 358 | // encode back to utf8, something is wrong with your libc. But just |
| 359 | // in case somebody finds an exploit... |
| 360 | len = wcrtomb(new, c, 0); |
| 361 | if (len < 1) error_exit("bad utf8 %x", (int)c); |
| 362 | new += len; |
| 363 | } |
| 364 | } |
| 365 | *new = 0; |
| 366 | } |
| 367 | |
| 368 | return try; |
| 369 | } |
| 370 | |
Rob Landley | 4d3ad67 | 2015-12-29 11:52:12 -0600 | [diff] [blame] | 371 | // strstr but returns pointer after match |
| 372 | char *strafter(char *haystack, char *needle) |
| 373 | { |
| 374 | char *s = strstr(haystack, needle); |
| 375 | |
| 376 | return s ? s+strlen(needle) : s; |
| 377 | } |
| 378 | |
Rob Landley | 45e0acc | 2015-11-02 01:34:58 -0600 | [diff] [blame] | 379 | // Remove trailing \n |
Elliott Hughes | 11d6079 | 2015-10-31 12:15:25 -0700 | [diff] [blame] | 380 | char *chomp(char *s) |
| 381 | { |
| 382 | char *p = strrchr(s, '\n'); |
| 383 | |
Rob Landley | 45e0acc | 2015-11-02 01:34:58 -0600 | [diff] [blame] | 384 | if (p && !p[1]) *p = 0; |
Elliott Hughes | 11d6079 | 2015-10-31 12:15:25 -0700 | [diff] [blame] | 385 | return s; |
| 386 | } |
| 387 | |
Rob Landley | 5fcc715 | 2014-10-18 17:14:12 -0500 | [diff] [blame] | 388 | int unescape(char c) |
| 389 | { |
| 390 | char *from = "\\abefnrtv", *to = "\\\a\b\033\f\n\r\t\v"; |
| 391 | int idx = stridx(from, c); |
| 392 | |
| 393 | return (idx == -1) ? 0 : to[idx]; |
| 394 | } |
| 395 | |
Rob Landley | e1ee741 | 2017-06-06 13:21:03 -0500 | [diff] [blame^] | 396 | // If string ends with suffix return pointer to start of suffix in string, |
| 397 | // else NULL |
Rob Landley | d74b562 | 2017-05-08 23:01:06 -0500 | [diff] [blame] | 398 | char *strend(char *str, char *suffix) |
| 399 | { |
| 400 | long a = strlen(str), b = strlen(suffix); |
| 401 | |
| 402 | if (a>b && !strcmp(str += a-b, suffix)) return str; |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Rob Landley | 8115fc1 | 2014-06-09 07:12:49 -0500 | [diff] [blame] | 407 | // If *a starts with b, advance *a past it and return 1, else return 0; |
| 408 | int strstart(char **a, char *b) |
| 409 | { |
| 410 | int len = strlen(b), i = !strncmp(*a, b, len); |
| 411 | |
| 412 | if (i) *a += len; |
| 413 | |
| 414 | return i; |
| 415 | } |
| 416 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 417 | // Return how long the file at fd is, if there's any way to determine it. |
| 418 | off_t fdlength(int fd) |
| 419 | { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 420 | struct stat st; |
| 421 | off_t base = 0, range = 1, expand = 1, old; |
| 422 | |
| 423 | if (!fstat(fd, &st) && S_ISREG(st.st_mode)) return st.st_size; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 424 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 425 | // If the ioctl works for this, return it. |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 426 | // TODO: is blocksize still always 512, or do we stat for it? |
| 427 | // unsigned int size; |
| 428 | // if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 429 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 430 | // If not, do a binary search for the last location we can read. (Some |
| 431 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 432 | // a CONFIG option... |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 433 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 434 | // If not, do a binary search for the last location we can read. |
| 435 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 436 | old = lseek(fd, 0, SEEK_CUR); |
| 437 | do { |
| 438 | char temp; |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 439 | off_t pos = base + range / 2; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 440 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 441 | if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 442 | off_t delta = (pos + 1) - base; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 443 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 444 | base += delta; |
| 445 | if (expand) range = (expand <<= 1) - base; |
| 446 | else range -= delta; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 447 | } else { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 448 | expand = 0; |
| 449 | range = pos - base; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 450 | } |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 451 | } while (range > 0); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 452 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 453 | lseek(fd, old, SEEK_SET); |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 454 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 455 | return base; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 458 | // Read contents of file as a single nul-terminated string. |
Rob Landley | 12a487b | 2015-11-26 21:16:12 -0600 | [diff] [blame] | 459 | // measure file size if !len, allocate buffer if !buf |
Rob Landley | 46409d5 | 2016-06-15 15:47:01 -0500 | [diff] [blame] | 460 | // Existing buffers need len in *plen |
| 461 | // Returns amount of data read in *plen |
Rob Landley | 12a487b | 2015-11-26 21:16:12 -0600 | [diff] [blame] | 462 | char *readfileat(int dirfd, char *name, char *ibuf, off_t *plen) |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 463 | { |
Rob Landley | 59781de | 2016-01-20 16:48:01 -0600 | [diff] [blame] | 464 | off_t len, rlen; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 465 | int fd; |
Rob Landley | 59781de | 2016-01-20 16:48:01 -0600 | [diff] [blame] | 466 | char *buf, *rbuf; |
| 467 | |
| 468 | // Unsafe to probe for size with a supplied buffer, don't ever do that. |
| 469 | if (CFG_TOYBOX_DEBUG && (ibuf ? !*plen : *plen)) error_exit("bad readfileat"); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 470 | |
Rob Landley | e10483f | 2015-04-03 11:49:31 -0500 | [diff] [blame] | 471 | if (-1 == (fd = openat(dirfd, name, O_RDONLY))) return 0; |
Rob Landley | 59781de | 2016-01-20 16:48:01 -0600 | [diff] [blame] | 472 | |
| 473 | // If we dunno the length, probe it. If we can't probe, start with 1 page. |
| 474 | if (!*plen) { |
| 475 | if ((len = fdlength(fd))>0) *plen = len; |
| 476 | else len = 4096; |
| 477 | } else len = *plen-1; |
| 478 | |
Ashwini Sharma | 26b2188 | 2014-05-02 06:24:11 -0500 | [diff] [blame] | 479 | if (!ibuf) buf = xmalloc(len+1); |
| 480 | else buf = ibuf; |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 481 | |
Rob Landley | 59781de | 2016-01-20 16:48:01 -0600 | [diff] [blame] | 482 | for (rbuf = buf;;) { |
| 483 | rlen = readall(fd, rbuf, len); |
| 484 | if (*plen || rlen<len) break; |
| 485 | |
| 486 | // If reading unknown size, expand buffer by 1.5 each time we fill it up. |
| 487 | rlen += rbuf-buf; |
| 488 | buf = xrealloc(buf, len = (rlen*3)/2); |
| 489 | rbuf = buf+rlen; |
| 490 | len -= rlen; |
| 491 | } |
Elliott Hughes | 81f31e4 | 2016-02-18 16:24:02 -0800 | [diff] [blame] | 492 | *plen = len = rlen+(rbuf-buf); |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 493 | close(fd); |
Rob Landley | 59781de | 2016-01-20 16:48:01 -0600 | [diff] [blame] | 494 | |
| 495 | if (rlen<0) { |
Ashwini Sharma | 26b2188 | 2014-05-02 06:24:11 -0500 | [diff] [blame] | 496 | if (ibuf != buf) free(buf); |
Elliott Hughes | 81f31e4 | 2016-02-18 16:24:02 -0800 | [diff] [blame] | 497 | buf = 0; |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 498 | } else buf[len] = 0; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 499 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 500 | return buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Rob Landley | e10483f | 2015-04-03 11:49:31 -0500 | [diff] [blame] | 503 | char *readfile(char *name, char *ibuf, off_t len) |
| 504 | { |
Rob Landley | 12a487b | 2015-11-26 21:16:12 -0600 | [diff] [blame] | 505 | return readfileat(AT_FDCWD, name, ibuf, &len); |
Rob Landley | e10483f | 2015-04-03 11:49:31 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 508 | // Sleep for this many thousandths of a second |
| 509 | void msleep(long miliseconds) |
| 510 | { |
| 511 | struct timespec ts; |
| 512 | |
| 513 | ts.tv_sec = miliseconds/1000; |
| 514 | ts.tv_nsec = (miliseconds%1000)*1000000; |
| 515 | nanosleep(&ts, &ts); |
| 516 | } |
| 517 | |
Rob Landley | f86f2f4 | 2017-05-21 13:11:42 -0500 | [diff] [blame] | 518 | // return 1<<x of highest bit set |
| 519 | int highest_bit(unsigned long l) |
| 520 | { |
| 521 | int i; |
| 522 | |
| 523 | for (i = 0; l; i++) l >>= 1; |
| 524 | |
| 525 | return i-1; |
| 526 | } |
| 527 | |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 528 | // Inefficient, but deals with unaligned access |
| 529 | int64_t peek_le(void *ptr, unsigned size) |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 530 | { |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 531 | int64_t ret = 0; |
| 532 | char *c = ptr; |
| 533 | int i; |
| 534 | |
Elliott Hughes | 9502a77 | 2016-02-13 09:50:20 -0800 | [diff] [blame] | 535 | for (i=0; i<size; i++) ret |= ((int64_t)c[i])<<(i*8); |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | int64_t peek_be(void *ptr, unsigned size) |
| 540 | { |
| 541 | int64_t ret = 0; |
| 542 | char *c = ptr; |
Elliott Hughes | 9502a77 | 2016-02-13 09:50:20 -0800 | [diff] [blame] | 543 | int i; |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 544 | |
Elliott Hughes | 9502a77 | 2016-02-13 09:50:20 -0800 | [diff] [blame] | 545 | for (i=0; i<size; i++) ret = (ret<<8)|(c[i]&0xff); |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | int64_t peek(void *ptr, unsigned size) |
| 550 | { |
| 551 | return IS_BIG_ENDIAN ? peek_be(ptr, size) : peek_le(ptr, size); |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void poke(void *ptr, uint64_t val, int size) |
| 555 | { |
| 556 | if (size & 8) { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 557 | volatile uint64_t *p = (uint64_t *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 558 | *p = val; |
| 559 | } else if (size & 4) { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 560 | volatile int *p = (int *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 561 | *p = val; |
| 562 | } else if (size & 2) { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 563 | volatile short *p = (short *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 564 | *p = val; |
| 565 | } else { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 566 | volatile char *p = (char *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 567 | *p = val; |
| 568 | } |
| 569 | } |
| 570 | |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 571 | // Iterate through an array of files, opening each one and calling a function |
Rob Landley | eed9ed4 | 2016-09-05 00:55:24 -0500 | [diff] [blame] | 572 | // on that filehandle and name. The special filename "-" means stdin if |
| 573 | // flags is O_RDONLY, stdout otherwise. An empty argument list calls |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 574 | // function() on just stdin/stdout. |
| 575 | // |
Rob Landley | 784eb9c | 2014-10-14 14:16:34 -0500 | [diff] [blame] | 576 | // Note: pass O_CLOEXEC to automatically close filehandles when function() |
Rob Landley | eed9ed4 | 2016-09-05 00:55:24 -0500 | [diff] [blame] | 577 | // returns, otherwise filehandles must be closed by function(). |
| 578 | // pass WARN_ONLY to produce warning messages about files it couldn't |
| 579 | // open/create, and skip them. Otherwise function is called with fd -1. |
| 580 | void loopfiles_rw(char **argv, int flags, int permissions, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 581 | void (*function)(int fd, char *name)) |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 582 | { |
Rob Landley | eed9ed4 | 2016-09-05 00:55:24 -0500 | [diff] [blame] | 583 | int fd, failok = !(flags&WARN_ONLY); |
| 584 | |
| 585 | flags &= ~WARN_ONLY; |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 586 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 587 | // If no arguments, read from stdin. |
Rob Landley | 45b3882 | 2014-10-27 18:08:59 -0500 | [diff] [blame] | 588 | if (!*argv) function((flags & O_ACCMODE) != O_RDONLY ? 1 : 0, "-"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 589 | else do { |
| 590 | // Filename "-" means read from stdin. |
| 591 | // Inability to open a file prints a warning, but doesn't exit. |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 592 | |
Rob Landley | eed9ed4 | 2016-09-05 00:55:24 -0500 | [diff] [blame] | 593 | if (!strcmp(*argv, "-")) fd = 0; |
| 594 | else if (0>(fd = notstdio(open(*argv, flags, permissions))) && !failok) { |
Rob Landley | d3a435e | 2016-01-05 22:26:58 -0600 | [diff] [blame] | 595 | perror_msg_raw(*argv); |
Elliott Hughes | 8784d8d | 2016-01-08 19:13:38 -0600 | [diff] [blame] | 596 | continue; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 597 | } |
Elliott Hughes | 8784d8d | 2016-01-08 19:13:38 -0600 | [diff] [blame] | 598 | function(fd, *argv); |
Rob Landley | fff20ab | 2016-07-15 04:39:35 -0500 | [diff] [blame] | 599 | if ((flags & O_CLOEXEC) && fd) close(fd); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 600 | } while (*++argv); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 601 | } |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 602 | |
Rob Landley | eed9ed4 | 2016-09-05 00:55:24 -0500 | [diff] [blame] | 603 | // Call loopfiles_rw with O_RDONLY|O_CLOEXEC|WARN_ONLY (common case) |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 604 | void loopfiles(char **argv, void (*function)(int fd, char *name)) |
| 605 | { |
Rob Landley | eed9ed4 | 2016-09-05 00:55:24 -0500 | [diff] [blame] | 606 | loopfiles_rw(argv, O_RDONLY|O_CLOEXEC|WARN_ONLY, 0, function); |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 607 | } |
| 608 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 609 | // Slow, but small. |
| 610 | |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 611 | char *get_rawline(int fd, long *plen, char end) |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 612 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 613 | char c, *buf = NULL; |
| 614 | long len = 0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 615 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 616 | for (;;) { |
| 617 | if (1>read(fd, &c, 1)) break; |
| 618 | if (!(len & 63)) buf=xrealloc(buf, len+65); |
| 619 | if ((buf[len++]=c) == end) break; |
| 620 | } |
| 621 | if (buf) buf[len]=0; |
| 622 | if (plen) *plen = len; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 623 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 624 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | char *get_line(int fd) |
| 628 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 629 | long len; |
| 630 | char *buf = get_rawline(fd, &len, '\n'); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 631 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 632 | if (buf && buf[--len]=='\n') buf[len]=0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 633 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 634 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 635 | } |
| 636 | |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 637 | int wfchmodat(int fd, char *name, mode_t mode) |
| 638 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 639 | int rc = fchmodat(fd, name, mode, 0); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 640 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 641 | if (rc) { |
| 642 | perror_msg("chmod '%s' to %04o", name, mode); |
| 643 | toys.exitval=1; |
| 644 | } |
| 645 | return rc; |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 646 | } |
| 647 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 648 | static char *tempfile2zap; |
Rob Landley | 0e05569 | 2016-03-29 03:59:20 -0500 | [diff] [blame] | 649 | static void tempfile_handler(void) |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 650 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 651 | if (1 < (long)tempfile2zap) unlink(tempfile2zap); |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 652 | } |
| 653 | |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 654 | // Open a temporary file to copy an existing file into. |
| 655 | int copy_tempfile(int fdin, char *name, char **tempname) |
| 656 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 657 | struct stat statbuf; |
| 658 | int fd; |
Elliott Hughes | fd56876 | 2017-02-19 09:22:45 -0800 | [diff] [blame] | 659 | int ignored __attribute__((__unused__)); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 660 | |
Rob Landley | 9b5000c | 2014-12-22 17:04:47 -0600 | [diff] [blame] | 661 | *tempname = xmprintf("%s%s", name, "XXXXXX"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 662 | if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); |
| 663 | if (!tempfile2zap) sigatexit(tempfile_handler); |
| 664 | tempfile2zap = *tempname; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 665 | |
Rob Landley | 3366af7 | 2016-09-30 17:35:34 -0500 | [diff] [blame] | 666 | // Set permissions of output file (ignoring errors, usually due to nonroot) |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 667 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 668 | fstat(fdin, &statbuf); |
| 669 | fchmod(fd, statbuf.st_mode); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 670 | |
Rob Landley | 4a4b3d6 | 2017-02-05 00:51:18 -0600 | [diff] [blame] | 671 | // We chmod before chown, which strips the suid bit. Caller has to explicitly |
| 672 | // switch it back on if they want to keep suid. |
| 673 | |
Elliott Hughes | fd56876 | 2017-02-19 09:22:45 -0800 | [diff] [blame] | 674 | // Suppress warn-unused-result. Both gcc and clang clutch their pearls about |
| 675 | // this but it's _supposed_ to fail when we're not root. |
| 676 | ignored = fchown(fd, statbuf.st_uid, statbuf.st_gid); |
Rob Landley | 3366af7 | 2016-09-30 17:35:34 -0500 | [diff] [blame] | 677 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 678 | return fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // Abort the copy and delete the temporary file. |
| 682 | void delete_tempfile(int fdin, int fdout, char **tempname) |
| 683 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 684 | close(fdin); |
| 685 | close(fdout); |
Rob Landley | 11d2ff5 | 2015-08-08 19:21:42 -0500 | [diff] [blame] | 686 | if (*tempname) unlink(*tempname); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 687 | tempfile2zap = (char *)1; |
| 688 | free(*tempname); |
| 689 | *tempname = NULL; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | // Copy the rest of the data and replace the original with the copy. |
| 693 | void replace_tempfile(int fdin, int fdout, char **tempname) |
| 694 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 695 | char *temp = xstrdup(*tempname); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 696 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 697 | temp[strlen(temp)-6]=0; |
| 698 | if (fdin != -1) { |
| 699 | xsendfile(fdin, fdout); |
| 700 | xclose(fdin); |
| 701 | } |
| 702 | xclose(fdout); |
| 703 | rename(*tempname, temp); |
| 704 | tempfile2zap = (char *)1; |
| 705 | free(*tempname); |
| 706 | free(temp); |
| 707 | *tempname = NULL; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 708 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 709 | |
| 710 | // Create a 256 entry CRC32 lookup table. |
| 711 | |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 712 | void crc_init(unsigned int *crc_table, int little_endian) |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 713 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 714 | unsigned int i; |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 715 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 716 | // Init the CRC32 table (big endian) |
| 717 | for (i=0; i<256; i++) { |
| 718 | unsigned int j, c = little_endian ? i : i<<24; |
| 719 | for (j=8; j; j--) |
| 720 | if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1; |
| 721 | else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
| 722 | crc_table[i] = c; |
| 723 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 724 | } |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 725 | |
Rob Landley | 0517eb7 | 2014-12-13 11:58:08 -0600 | [diff] [blame] | 726 | // Init base64 table |
| 727 | |
| 728 | void base64_init(char *p) |
| 729 | { |
| 730 | int i; |
| 731 | |
| 732 | for (i = 'A'; i != ':'; i++) { |
| 733 | if (i == 'Z'+1) i = 'a'; |
| 734 | if (i == 'z'+1) i = '0'; |
| 735 | *(p++) = i; |
| 736 | } |
| 737 | *(p++) = '+'; |
| 738 | *(p++) = '/'; |
| 739 | } |
| 740 | |
Rob Landley | b1353fb | 2015-09-07 17:12:57 -0500 | [diff] [blame] | 741 | int yesno(int def) |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 742 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 743 | char buf; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 744 | |
Rob Landley | b1353fb | 2015-09-07 17:12:57 -0500 | [diff] [blame] | 745 | fprintf(stderr, " (%c/%c):", def ? 'Y' : 'y', def ? 'n' : 'N'); |
Rob Landley | db8eb32 | 2012-12-08 02:25:32 -0600 | [diff] [blame] | 746 | fflush(stderr); |
| 747 | while (fread(&buf, 1, 1, stdin)) { |
| 748 | int new; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 749 | |
Rob Landley | 2279108 | 2013-01-31 04:13:07 -0600 | [diff] [blame] | 750 | // The letter changes the value, the newline (or space) returns it. |
Rob Landley | db8eb32 | 2012-12-08 02:25:32 -0600 | [diff] [blame] | 751 | if (isspace(buf)) break; |
| 752 | if (-1 != (new = stridx("ny", tolower(buf)))) def = new; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 753 | } |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 754 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 755 | return def; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 756 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 757 | |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 758 | struct signame { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 759 | int num; |
| 760 | char *name; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | // Signals required by POSIX 2008: |
| 764 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html |
| 765 | |
| 766 | #define SIGNIFY(x) {SIG##x, #x} |
| 767 | |
| 768 | static struct signame signames[] = { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 769 | SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), |
| 770 | SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), |
| 771 | SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), |
| 772 | SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), |
| 773 | SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 774 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 775 | // Start of non-terminal signals |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 776 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 777 | SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), |
| 778 | SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG) |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 779 | }; |
| 780 | |
| 781 | // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR) |
| 782 | // obsolete: SIGNIFY(PROF) SIGNIFY(POLL) |
| 783 | |
Rob Landley | 1bc5224 | 2014-05-21 07:24:16 -0500 | [diff] [blame] | 784 | // Handler that sets toys.signal, and writes to toys.signalfd if set |
| 785 | void generic_signal(int sig) |
| 786 | { |
| 787 | if (toys.signalfd) { |
| 788 | char c = sig; |
| 789 | |
| 790 | writeall(toys.signalfd, &c, 1); |
| 791 | } |
| 792 | toys.signal = sig; |
| 793 | } |
| 794 | |
Rob Landley | eb24df9 | 2016-03-13 20:23:41 -0500 | [diff] [blame] | 795 | void exit_signal(int sig) |
| 796 | { |
Rob Landley | 0e05569 | 2016-03-29 03:59:20 -0500 | [diff] [blame] | 797 | if (sig) toys.exitval = sig|128; |
Rob Landley | eb24df9 | 2016-03-13 20:23:41 -0500 | [diff] [blame] | 798 | xexit(); |
| 799 | } |
| 800 | |
| 801 | // Install the same handler on every signal that defaults to killing the |
Rob Landley | 0e05569 | 2016-03-29 03:59:20 -0500 | [diff] [blame] | 802 | // process, calling the handler on the way out. Calling multiple times |
| 803 | // adds the handlers to a list, to be called in order. |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 804 | void sigatexit(void *handler) |
| 805 | { |
Rob Landley | eb24df9 | 2016-03-13 20:23:41 -0500 | [diff] [blame] | 806 | struct arg_list *al = xmalloc(sizeof(struct arg_list)); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 807 | int i; |
Rob Landley | eb24df9 | 2016-03-13 20:23:41 -0500 | [diff] [blame] | 808 | |
| 809 | for (i=0; signames[i].num != SIGCHLD; i++) |
| 810 | signal(signames[i].num, exit_signal); |
| 811 | al->next = toys.xexit; |
| 812 | al->arg = handler; |
| 813 | toys.xexit = al; |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 814 | } |
Rob Landley | 1bc5224 | 2014-05-21 07:24:16 -0500 | [diff] [blame] | 815 | |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 816 | // Convert name to signal number. If name == NULL print names. |
| 817 | int sig_to_num(char *pidstr) |
| 818 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 819 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 820 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 821 | if (pidstr) { |
| 822 | char *s; |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 823 | |
| 824 | i = estrtol(pidstr, &s, 10); |
| 825 | if (!errno && !*s) return i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 826 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 827 | if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; |
| 828 | } |
| 829 | for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++) |
| 830 | if (!pidstr) xputs(signames[i].name); |
| 831 | else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 832 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 833 | return -1; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | char *num_to_sig(int sig) |
| 837 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 838 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 839 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 840 | for (i=0; i<sizeof(signames)/sizeof(struct signame); i++) |
| 841 | if (signames[i].num == sig) return signames[i].name; |
| 842 | return NULL; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 843 | } |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 844 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 845 | // premute mode bits based on posix mode strings. |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 846 | mode_t string_to_mode(char *modestr, mode_t mode) |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 847 | { |
Rob Landley | 3b5b19e | 2014-08-15 10:50:39 -0500 | [diff] [blame] | 848 | char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu", |
| 849 | *s, *str = modestr; |
| 850 | mode_t extrabits = mode & ~(07777); |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 851 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 852 | // Handle octal mode |
| 853 | if (isdigit(*str)) { |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame] | 854 | mode = estrtol(str, &s, 8); |
| 855 | if (errno || *s || (mode & ~(07777))) goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 856 | |
Rob Landley | 3b5b19e | 2014-08-15 10:50:39 -0500 | [diff] [blame] | 857 | return mode | extrabits; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 858 | } |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 859 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 860 | // Gaze into the bin of permission... |
| 861 | for (;;) { |
| 862 | int i, j, dowho, dohow, dowhat, amask; |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 863 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 864 | dowho = dohow = dowhat = amask = 0; |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 865 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 866 | // Find the who, how, and what stanzas, in that order |
| 867 | while (*str && (s = strchr(whos, *str))) { |
| 868 | dowho |= 1<<(s-whos); |
| 869 | str++; |
| 870 | } |
| 871 | // If who isn't specified, like "a" but honoring umask. |
| 872 | if (!dowho) { |
| 873 | dowho = 8; |
| 874 | umask(amask=umask(0)); |
| 875 | } |
| 876 | if (!*str || !(s = strchr(hows, *str))) goto barf; |
| 877 | dohow = *(str++); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 878 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 879 | if (!dohow) goto barf; |
| 880 | while (*str && (s = strchr(whats, *str))) { |
| 881 | dowhat |= 1<<(s-whats); |
| 882 | str++; |
| 883 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 884 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 885 | // Convert X to x for directory or if already executable somewhere |
| 886 | if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 887 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 888 | // Copy mode from another category? |
| 889 | if (!dowhat && *str && (s = strchr(whys, *str))) { |
| 890 | dowhat = (mode>>(3*(s-whys)))&7; |
| 891 | str++; |
| 892 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 893 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 894 | // Are we ready to do a thing yet? |
| 895 | if (*str && *(str++) != ',') goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 896 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 897 | // Ok, apply the bits to the mode. |
| 898 | for (i=0; i<4; i++) { |
| 899 | for (j=0; j<3; j++) { |
| 900 | mode_t bit = 0; |
| 901 | int where = 1<<((3*i)+j); |
Rob Landley | 31f49e7 | 2012-07-21 22:45:05 -0500 | [diff] [blame] | 902 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 903 | if (amask & where) continue; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 904 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 905 | // Figure out new value at this location |
| 906 | if (i == 3) { |
| 907 | // suid/sticky bit. |
| 908 | if (j) { |
| 909 | if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++; |
| 910 | } else if (dowhat & 16) bit++; |
| 911 | } else { |
| 912 | if (!(dowho&(8|(1<<i)))) continue; |
| 913 | if (dowhat&(1<<j)) bit++; |
| 914 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 915 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 916 | // When selection active, modify bit |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 917 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 918 | if (dohow == '=' || (bit && dohow == '-')) mode &= ~where; |
| 919 | if (bit && dohow != '-') mode |= where; |
| 920 | } |
| 921 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 922 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 923 | if (!*str) break; |
| 924 | } |
Rob Landley | 3b5b19e | 2014-08-15 10:50:39 -0500 | [diff] [blame] | 925 | |
| 926 | return mode|extrabits; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 927 | barf: |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 928 | error_exit("bad mode '%s'", modestr); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 929 | } |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 930 | |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 931 | // Format access mode into a drwxrwxrwx string |
| 932 | void mode_to_string(mode_t mode, char *buf) |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 933 | { |
| 934 | char c, d; |
| 935 | int i, bit; |
| 936 | |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 937 | buf[10]=0; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 938 | for (i=0; i<9; i++) { |
| 939 | bit = mode & (1<<i); |
| 940 | c = i%3; |
| 941 | if (!c && (mode & (1<<((d=i/3)+9)))) { |
| 942 | c = "tss"[d]; |
| 943 | if (!bit) c &= ~0x20; |
| 944 | } else c = bit ? "xwr"[c] : '-'; |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 945 | buf[9-i] = c; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | if (S_ISDIR(mode)) c = 'd'; |
| 949 | else if (S_ISBLK(mode)) c = 'b'; |
| 950 | else if (S_ISCHR(mode)) c = 'c'; |
| 951 | else if (S_ISLNK(mode)) c = 'l'; |
| 952 | else if (S_ISFIFO(mode)) c = 'p'; |
| 953 | else if (S_ISSOCK(mode)) c = 's'; |
| 954 | else c = '-'; |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 955 | *buf = c; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 956 | } |
Rob Landley | db1009d | 2013-12-19 09:32:30 -0600 | [diff] [blame] | 957 | |
Rob Landley | 1c028ca | 2016-04-08 18:25:59 -0500 | [diff] [blame] | 958 | // basename() can modify its argument or return a pointer to a constant string |
| 959 | // This just gives after the last '/' or the whole stirng if no / |
| 960 | char *getbasename(char *name) |
Rob Landley | 6292beb | 2015-07-10 14:52:14 -0500 | [diff] [blame] | 961 | { |
| 962 | char *s = strrchr(name, '/'); |
| 963 | |
| 964 | if (s) return s+1; |
Rob Landley | 1c028ca | 2016-04-08 18:25:59 -0500 | [diff] [blame] | 965 | |
Rob Landley | 6292beb | 2015-07-10 14:52:14 -0500 | [diff] [blame] | 966 | return name; |
| 967 | } |
| 968 | |
Rob Landley | db1009d | 2013-12-19 09:32:30 -0600 | [diff] [blame] | 969 | // Execute a callback for each PID that matches a process name from a list. |
| 970 | void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)) |
| 971 | { |
| 972 | DIR *dp; |
| 973 | struct dirent *entry; |
| 974 | |
| 975 | if (!(dp = opendir("/proc"))) perror_exit("opendir"); |
| 976 | |
| 977 | while ((entry = readdir(dp))) { |
| 978 | unsigned u; |
| 979 | char *cmd, **curname; |
| 980 | |
| 981 | if (!(u = atoi(entry->d_name))) continue; |
| 982 | sprintf(libbuf, "/proc/%u/cmdline", u); |
| 983 | if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue; |
| 984 | |
| 985 | for (curname = names; *curname; curname++) |
| 986 | if (**curname == '/' ? !strcmp(cmd, *curname) |
Rob Landley | cb49c30 | 2016-04-10 14:35:51 -0500 | [diff] [blame] | 987 | : !strcmp(getbasename(cmd), getbasename(*curname))) |
Rob Landley | db1009d | 2013-12-19 09:32:30 -0600 | [diff] [blame] | 988 | if (callback(u, *curname)) break; |
| 989 | if (*curname) break; |
| 990 | } |
| 991 | closedir(dp); |
| 992 | } |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 993 | |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 994 | // display first few digits of number with power of two units |
Elliott Hughes | 0fb4651 | 2015-08-08 21:10:44 -0500 | [diff] [blame] | 995 | int human_readable(char *buf, unsigned long long num, int style) |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 996 | { |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 997 | unsigned long long snap = 0; |
Rob Landley | 960100a | 2015-09-06 20:10:04 -0500 | [diff] [blame] | 998 | int len, unit, divisor = (style&HR_1000) ? 1000 : 1024; |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 999 | |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 1000 | // Divide rounding up until we have 3 or fewer digits. Since the part we |
| 1001 | // print is decimal, the test is 999 even when we divide by 1024. |
| 1002 | // We can't run out of units because 2<<64 is 18 exabytes. |
| 1003 | // test 5675 is 5.5k not 5.6k. |
| 1004 | for (unit = 0; num > 999; unit++) num = ((snap = num)+(divisor/2))/divisor; |
| 1005 | len = sprintf(buf, "%llu", num); |
| 1006 | if (unit && len == 1) { |
Rob Landley | 960100a | 2015-09-06 20:10:04 -0500 | [diff] [blame] | 1007 | // Redo rounding for 1.2M case, this works with and without HR_1000. |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 1008 | num = snap/divisor; |
| 1009 | snap -= num*divisor; |
| 1010 | snap = ((snap*100)+50)/divisor; |
| 1011 | snap /= 10; |
| 1012 | len = sprintf(buf, "%llu.%llu", num, snap); |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 1013 | } |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 1014 | if (style & HR_SPACE) buf[len++] = ' '; |
| 1015 | if (unit) { |
| 1016 | unit = " kMGTPE"[unit]; |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 1017 | |
Rob Landley | 960100a | 2015-09-06 20:10:04 -0500 | [diff] [blame] | 1018 | if (!(style&HR_1000)) unit = toupper(unit); |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 1019 | buf[len++] = unit; |
Rob Landley | 960100a | 2015-09-06 20:10:04 -0500 | [diff] [blame] | 1020 | } else if (style & HR_B) buf[len++] = 'B'; |
Rob Landley | d06ea37 | 2015-09-03 20:36:44 -0500 | [diff] [blame] | 1021 | buf[len] = 0; |
| 1022 | |
| 1023 | return len; |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 1024 | } |
Rob Landley | 5b493dc | 2015-04-19 21:50:51 -0500 | [diff] [blame] | 1025 | |
| 1026 | // The qsort man page says you can use alphasort, the posix committee |
| 1027 | // disagreed, and doubled down: http://austingroupbugs.net/view.php?id=142 |
| 1028 | // So just do our own. (The const is entirely to humor the stupid compiler.) |
| 1029 | int qstrcmp(const void *a, const void *b) |
| 1030 | { |
| 1031 | return strcmp(*(char **)a, *(char **)b); |
| 1032 | } |
Rob Landley | 2fd8624 | 2015-04-27 11:13:19 -0500 | [diff] [blame] | 1033 | |
Rob Landley | 5493916 | 2016-01-16 16:59:47 -0600 | [diff] [blame] | 1034 | // According to http://www.opengroup.org/onlinepubs/9629399/apdxa.htm |
| 1035 | // we should generate a uuid structure by reading a clock with 100 nanosecond |
| 1036 | // precision, normalizing it to the start of the gregorian calendar in 1582, |
| 1037 | // and looking up our eth0 mac address. |
| 1038 | // |
| 1039 | // On the other hand, we have 128 bits to come up with a unique identifier, of |
| 1040 | // which 6 have a defined value. /dev/urandom it is. |
Rob Landley | 2fd8624 | 2015-04-27 11:13:19 -0500 | [diff] [blame] | 1041 | |
Rob Landley | 5493916 | 2016-01-16 16:59:47 -0600 | [diff] [blame] | 1042 | void create_uuid(char *uuid) |
| 1043 | { |
| 1044 | // Read 128 random bits |
Rob Landley | 027a73a | 2016-08-04 10:16:59 -0500 | [diff] [blame] | 1045 | int fd = xopenro("/dev/urandom"); |
Rob Landley | 5493916 | 2016-01-16 16:59:47 -0600 | [diff] [blame] | 1046 | xreadall(fd, uuid, 16); |
| 1047 | close(fd); |
| 1048 | |
| 1049 | // Claim to be a DCE format UUID. |
| 1050 | uuid[6] = (uuid[6] & 0x0F) | 0x40; |
| 1051 | uuid[8] = (uuid[8] & 0x3F) | 0x80; |
| 1052 | |
| 1053 | // rfc2518 section 6.4.1 suggests if we're not using a macaddr, we should |
| 1054 | // set bit 1 of the node ID, which is the mac multicast bit. This means we |
| 1055 | // should never collide with anybody actually using a macaddr. |
| 1056 | uuid[11] |= 128; |
Rob Landley | 2fd8624 | 2015-04-27 11:13:19 -0500 | [diff] [blame] | 1057 | } |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 1058 | |
| 1059 | char *show_uuid(char *uuid) |
| 1060 | { |
| 1061 | char *out = libbuf; |
| 1062 | int i; |
| 1063 | |
| 1064 | for (i=0; i<16; i++) out+=sprintf(out, "-%02x"+!(0x550&(1<<i)), uuid[i]); |
| 1065 | *out = 0; |
| 1066 | |
| 1067 | return libbuf; |
| 1068 | } |
Rob Landley | cf0f037 | 2016-01-22 21:35:48 -0600 | [diff] [blame] | 1069 | |
| 1070 | // Returns pointer to letter at end, 0 if none. *start = initial % |
| 1071 | char *next_printf(char *s, char **start) |
| 1072 | { |
| 1073 | for (; *s; s++) { |
| 1074 | if (*s != '%') continue; |
| 1075 | if (*++s == '%') continue; |
| 1076 | if (start) *start = s-1; |
| 1077 | while (0 <= stridx("0'#-+ ", *s)) s++; |
| 1078 | while (isdigit(*s)) s++; |
| 1079 | if (*s == '.') s++; |
| 1080 | while (isdigit(*s)) s++; |
| 1081 | |
| 1082 | return s; |
| 1083 | } |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
Rob Landley | f435f04 | 2016-02-10 21:05:22 -0600 | [diff] [blame] | 1087 | |
| 1088 | // Posix inexplicably hasn't got this, so find str in line. |
| 1089 | char *strnstr(char *line, char *str) |
| 1090 | { |
| 1091 | long len = strlen(str); |
| 1092 | char *s; |
| 1093 | |
| 1094 | for (s = line; *s; s++) if (!strncasecmp(s, str, len)) break; |
| 1095 | |
| 1096 | return *s ? s : 0; |
| 1097 | } |
Rob Landley | 7ca5dc4 | 2016-03-02 11:52:38 -0600 | [diff] [blame] | 1098 | |
| 1099 | int dev_minor(int dev) |
| 1100 | { |
| 1101 | return ((dev&0xfff00000)>>12)|(dev&0xff); |
| 1102 | } |
| 1103 | |
| 1104 | int dev_major(int dev) |
| 1105 | { |
| 1106 | return (dev&0xfff00)>>8; |
| 1107 | } |
| 1108 | |
| 1109 | int dev_makedev(int major, int minor) |
| 1110 | { |
| 1111 | return (minor&0xff)|((major&0xfff)<<8)|((minor&0xfff00)<<12); |
| 1112 | } |
Rob Landley | 6d50d4c | 2016-05-17 12:10:07 -0500 | [diff] [blame] | 1113 | |
| 1114 | // Return cached passwd entries. |
| 1115 | struct passwd *bufgetpwuid(uid_t uid) |
| 1116 | { |
| 1117 | struct pwuidbuf_list { |
| 1118 | struct pwuidbuf_list *next; |
| 1119 | struct passwd pw; |
| 1120 | } *list; |
| 1121 | struct passwd *temp; |
| 1122 | static struct pwuidbuf_list *pwuidbuf; |
| 1123 | |
| 1124 | for (list = pwuidbuf; list; list = list->next) |
| 1125 | if (list->pw.pw_uid == uid) return &(list->pw); |
| 1126 | |
| 1127 | list = xmalloc(512); |
| 1128 | list->next = pwuidbuf; |
| 1129 | |
| 1130 | errno = getpwuid_r(uid, &list->pw, sizeof(*list)+(char *)list, |
| 1131 | 512-sizeof(*list), &temp); |
| 1132 | if (!temp) { |
| 1133 | free(list); |
| 1134 | |
| 1135 | return 0; |
| 1136 | } |
| 1137 | pwuidbuf = list; |
| 1138 | |
| 1139 | return &list->pw; |
| 1140 | } |
Rob Landley | b602f1c | 2016-05-20 14:28:13 -0500 | [diff] [blame] | 1141 | |
| 1142 | // Return cached passwd entries. |
| 1143 | struct group *bufgetgrgid(gid_t gid) |
| 1144 | { |
| 1145 | struct grgidbuf_list { |
| 1146 | struct grgidbuf_list *next; |
| 1147 | struct group gr; |
| 1148 | } *list; |
| 1149 | struct group *temp; |
| 1150 | static struct grgidbuf_list *grgidbuf; |
| 1151 | |
| 1152 | for (list = grgidbuf; list; list = list->next) |
| 1153 | if (list->gr.gr_gid == gid) return &(list->gr); |
| 1154 | |
| 1155 | list = xmalloc(512); |
| 1156 | list->next = grgidbuf; |
| 1157 | |
| 1158 | errno = getgrgid_r(gid, &list->gr, sizeof(*list)+(char *)list, |
| 1159 | 512-sizeof(*list), &temp); |
| 1160 | if (!temp) { |
| 1161 | free(list); |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | grgidbuf = list; |
| 1166 | |
| 1167 | return &list->gr; |
| 1168 | } |
Rob Landley | 46409d5 | 2016-06-15 15:47:01 -0500 | [diff] [blame] | 1169 | |
| 1170 | // Always null terminates, returns 0 for failure, len for success |
| 1171 | int readlinkat0(int dirfd, char *path, char *buf, int len) |
| 1172 | { |
| 1173 | if (!len) return 0; |
| 1174 | |
| 1175 | len = readlinkat(dirfd, path, buf, len-1); |
| 1176 | if (len<1) return 0; |
| 1177 | buf[len] = 0; |
| 1178 | |
| 1179 | return len; |
| 1180 | } |
| 1181 | |
| 1182 | int readlink0(char *path, char *buf, int len) |
| 1183 | { |
| 1184 | return readlinkat0(AT_FDCWD, path, buf, len); |
| 1185 | } |
Rob Landley | f20b10e | 2016-07-26 13:35:56 -0500 | [diff] [blame] | 1186 | |
| 1187 | // Do regex matching handling embedded NUL bytes in string (hence extra len |
| 1188 | // argument). Note that neither the pattern nor the match can currently include |
| 1189 | // NUL bytes (even with wildcards) and string must be null terminated at |
| 1190 | // string[len]. But this can find a match after the first NUL. |
| 1191 | int regexec0(regex_t *preg, char *string, long len, int nmatch, |
| 1192 | regmatch_t pmatch[], int eflags) |
| 1193 | { |
| 1194 | char *s = string; |
| 1195 | |
| 1196 | for (;;) { |
| 1197 | long ll = 0; |
| 1198 | int rc; |
| 1199 | |
| 1200 | while (len && !*s) { |
| 1201 | s++; |
| 1202 | len--; |
| 1203 | } |
| 1204 | while (s[ll] && ll<len) ll++; |
| 1205 | |
| 1206 | rc = regexec(preg, s, nmatch, pmatch, eflags); |
| 1207 | if (!rc) { |
| 1208 | for (rc = 0; rc<nmatch && pmatch[rc].rm_so!=-1; rc++) { |
| 1209 | pmatch[rc].rm_so += s-string; |
| 1210 | pmatch[rc].rm_eo += s-string; |
| 1211 | } |
| 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | if (ll==len) return rc; |
| 1216 | |
| 1217 | s += ll; |
| 1218 | len -= ll; |
| 1219 | } |
| 1220 | } |
Rob Landley | bc1ccac | 2016-08-13 15:19:29 -0500 | [diff] [blame] | 1221 | |
| 1222 | // Return user name or string representation of number, returned buffer |
| 1223 | // lasts until next call. |
| 1224 | char *getusername(uid_t uid) |
| 1225 | { |
| 1226 | struct passwd *pw = bufgetpwuid(uid); |
| 1227 | static char unum[12]; |
| 1228 | |
| 1229 | sprintf(unum, "%u", (unsigned)uid); |
| 1230 | return pw ? pw->pw_name : unum; |
| 1231 | } |
| 1232 | |
| 1233 | // Return group name or string representation of number, returned buffer |
| 1234 | // lasts until next call. |
| 1235 | char *getgroupname(gid_t gid) |
| 1236 | { |
| 1237 | struct group *gr = bufgetgrgid(gid); |
| 1238 | static char gnum[12]; |
| 1239 | |
| 1240 | sprintf(gnum, "%u", (unsigned)gid); |
| 1241 | return gr ? gr->gr_name : gnum; |
| 1242 | } |
| 1243 | |
Rob Landley | 7528a96 | 2016-09-03 15:41:55 -0500 | [diff] [blame] | 1244 | // Iterate over lines in file, calling function. Function can write 0 to |
| 1245 | // the line pointer if they want to keep it, or 1 to terminate processing, |
| 1246 | // otherwise line is freed. Passed file descriptor is closed at the end. |
| 1247 | void do_lines(int fd, void (*call)(char **pline, long len)) |
| 1248 | { |
| 1249 | FILE *fp = fd ? xfdopen(fd, "r") : stdin; |
| 1250 | |
| 1251 | for (;;) { |
| 1252 | char *line = 0; |
| 1253 | ssize_t len; |
| 1254 | |
| 1255 | len = getline(&line, (void *)&len, fp); |
| 1256 | if (len > 0) { |
| 1257 | call(&line, len); |
| 1258 | if (line == (void *)1) break; |
| 1259 | free(line); |
| 1260 | } else break; |
| 1261 | } |
| 1262 | |
| 1263 | if (fd) fclose(fp); |
| 1264 | } |