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)); |
| 16 | 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 | |
| 20 | void error_msg(char *msg, ...) |
| 21 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 22 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 23 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 24 | va_start(va, msg); |
| 25 | verror_msg(msg, 0, va); |
| 26 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void perror_msg(char *msg, ...) |
| 30 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 31 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 32 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 33 | va_start(va, msg); |
| 34 | verror_msg(msg, errno, va); |
| 35 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 36 | } |
| 37 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 38 | // Die with an error message. |
| 39 | void error_exit(char *msg, ...) |
| 40 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 41 | va_list va; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 42 | |
Rob Landley | 36ffc5a | 2013-04-14 21:43:22 -0500 | [diff] [blame] | 43 | if (CFG_TOYBOX_HELP && toys.exithelp) show_help(); |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -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, 0, va); |
| 47 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 48 | |
Rob Landley | 5583030 | 2013-06-16 19:59:51 -0500 | [diff] [blame] | 49 | xexit(); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Die with an error message and strerror(errno) |
| 53 | void perror_exit(char *msg, ...) |
| 54 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 55 | va_list va; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 56 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 57 | va_start(va, msg); |
| 58 | verror_msg(msg, errno, va); |
| 59 | va_end(va); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 60 | |
Rob Landley | 5583030 | 2013-06-16 19:59:51 -0500 | [diff] [blame] | 61 | xexit(); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 62 | } |
| 63 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 64 | // Keep reading until full or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 65 | ssize_t readall(int fd, void *buf, size_t len) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 66 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 67 | size_t count = 0; |
Rob Landley | 15b2315 | 2008-07-18 04:15:59 -0500 | [diff] [blame] | 68 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 69 | while (count<len) { |
Rob Landley | dbbd3d6 | 2013-12-08 13:26:05 -0600 | [diff] [blame] | 70 | int i = read(fd, (char *)buf+count, len-count); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 71 | if (!i) break; |
| 72 | if (i<0) return i; |
| 73 | count += i; |
| 74 | } |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 75 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 76 | return count; |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 79 | // Keep writing until done or EOF |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 80 | ssize_t writeall(int fd, void *buf, size_t len) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 81 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 82 | size_t count = 0; |
| 83 | while (count<len) { |
Rob Landley | 1fb3ae7 | 2014-02-16 11:09:23 -0600 | [diff] [blame] | 84 | int i = write(fd, count+(char *)buf, len-count); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 85 | if (i<1) return i; |
| 86 | count += i; |
| 87 | } |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 88 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 89 | return count; |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 92 | // skip this many bytes of input. Return 0 for success, >0 means this much |
| 93 | // left after input skipped. |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 94 | off_t lskip(int fd, off_t offset) |
| 95 | { |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 96 | off_t cur = lseek(fd, 0, SEEK_CUR); |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 97 | |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 98 | if (cur != -1) { |
| 99 | off_t end = lseek(fd, 0, SEEK_END) - cur; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 100 | |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 101 | if (end > 0 && end < offset) return offset - end; |
| 102 | end = offset+cur; |
| 103 | if (end == lseek(fd, end, SEEK_SET)) return 0; |
| 104 | perror_exit("lseek"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 105 | } |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 106 | |
| 107 | while (offset>0) { |
| 108 | int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or; |
| 109 | |
| 110 | or = readall(fd, libbuf, try); |
| 111 | if (or < 0) perror_exit("lskip to %lld", (long long)offset); |
Rob Landley | 4dd800c | 2014-02-28 23:10:03 -0600 | [diff] [blame] | 112 | else offset -= or; |
Rob Landley | bc382be | 2013-09-16 23:41:51 -0500 | [diff] [blame] | 113 | if (or < try) break; |
| 114 | } |
| 115 | |
| 116 | return offset; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 119 | // flags: 1=make last dir (with mode lastmode, otherwise skips last component) |
| 120 | // 2=make path (already exists is ok) |
| 121 | // 4=verbose |
| 122 | // returns 0 = path ok, 1 = error |
| 123 | int mkpathat(int atfd, char *dir, mode_t lastmode, int flags) |
| 124 | { |
| 125 | struct stat buf; |
| 126 | char *s; |
| 127 | |
| 128 | // mkdir -p one/two/three is not an error if the path already exists, |
| 129 | // but is if "three" is a file. The others we dereference and catch |
| 130 | // not-a-directory along the way, but the last one we must explicitly |
| 131 | // test for. Might as well do it up front. |
| 132 | |
| 133 | if (!fstatat(atfd, dir, &buf, 0) && !S_ISDIR(buf.st_mode)) { |
| 134 | errno = EEXIST; |
| 135 | return 1; |
| 136 | } |
| 137 | |
Rob Landley | 02f5a30 | 2014-03-24 06:26:49 -0500 | [diff] [blame] | 138 | for (s = dir; ;s++) { |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 139 | char save = 0; |
| 140 | mode_t mode = (0777&~toys.old_umask)|0300; |
| 141 | |
Rob Landley | 02f5a30 | 2014-03-24 06:26:49 -0500 | [diff] [blame] | 142 | // find next '/', but don't try to mkdir "" at start of absolute path |
| 143 | if (*s == '/' && (flags&2) && s != dir) { |
Rob Landley | ca1b60e | 2014-03-11 20:44:55 -0500 | [diff] [blame] | 144 | save = *s; |
| 145 | *s = 0; |
| 146 | } else if (*s) continue; |
| 147 | |
| 148 | // Use the mode from the -m option only for the last directory. |
| 149 | if (!save) { |
| 150 | if (flags&1) mode = lastmode; |
| 151 | else break; |
| 152 | } |
| 153 | |
| 154 | if (mkdirat(atfd, dir, mode)) { |
| 155 | if (!(flags&2) || errno != EEXIST) return 1; |
| 156 | } else if (flags&4) |
| 157 | fprintf(stderr, "%s: created directory '%s'\n", toys.which->name, dir); |
| 158 | |
| 159 | if (!(*s = save)) break; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 165 | // Split a path into linked list of components, tracking head and tail of list. |
| 166 | // Filters out // entries with no contents. |
| 167 | struct string_list **splitpath(char *path, struct string_list **list) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 168 | { |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 169 | char *new = path; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 170 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 171 | *list = 0; |
| 172 | do { |
| 173 | int len; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 174 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 175 | if (*path && *path != '/') continue; |
| 176 | len = path-new; |
| 177 | if (len > 0) { |
| 178 | *list = xmalloc(sizeof(struct string_list) + len + 1); |
| 179 | (*list)->next = 0; |
Rob Landley | 87fbe12 | 2014-12-13 11:56:41 -0600 | [diff] [blame] | 180 | memcpy((*list)->str, new, len); |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 181 | (*list)->str[len] = 0; |
| 182 | list = &(*list)->next; |
| 183 | } |
| 184 | new = path+1; |
| 185 | } while (*path++); |
| 186 | |
| 187 | return list; |
| 188 | } |
| 189 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 190 | // Find all file in a colon-separated path with access type "type" (generally |
| 191 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 192 | // order. |
| 193 | |
| 194 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 195 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 196 | struct string_list *rlist = NULL, **prlist=&rlist; |
Rob Landley | 0aad9e4 | 2014-06-24 08:19:24 -0500 | [diff] [blame] | 197 | char *cwd; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 198 | |
Rob Landley | 0aad9e4 | 2014-06-24 08:19:24 -0500 | [diff] [blame] | 199 | if (!path) return 0; |
| 200 | |
| 201 | cwd = xgetcwd(); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 202 | for (;;) { |
Rob Landley | 0aad9e4 | 2014-06-24 08:19:24 -0500 | [diff] [blame] | 203 | char *next = strchr(path, ':'); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 204 | int len = next ? next-path : strlen(path); |
| 205 | struct string_list *rnext; |
| 206 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 207 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 208 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 209 | + (len ? len : strlen(cwd)) + 2); |
| 210 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
| 211 | else { |
| 212 | char *res = rnext->str; |
Rob Landley | 87fbe12 | 2014-12-13 11:56:41 -0600 | [diff] [blame] | 213 | |
| 214 | memcpy(res, path, len); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 215 | res += len; |
| 216 | *(res++) = '/'; |
| 217 | strcpy(res, filename); |
| 218 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 219 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 220 | // Confirm it's not a directory. |
| 221 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
| 222 | *prlist = rnext; |
| 223 | rnext->next = NULL; |
| 224 | prlist = &(rnext->next); |
| 225 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 226 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 227 | if (!next) break; |
| 228 | path += len; |
| 229 | path++; |
| 230 | } |
| 231 | free(cwd); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 232 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 233 | return rlist; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 234 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 235 | |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame^] | 236 | long estrtol(char *str, char **end, int base) |
| 237 | { |
| 238 | errno = 0; |
| 239 | |
| 240 | return strtol(str, end, base); |
| 241 | } |
| 242 | |
| 243 | long xstrtol(char *str, char **end, int base) |
| 244 | { |
| 245 | long l = estrtol(str, end, base); |
| 246 | |
| 247 | if (errno) perror_exit("%s", str); |
| 248 | |
| 249 | return l; |
| 250 | } |
| 251 | |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 252 | // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
| 253 | // (zetta and yotta don't fit in 64 bits.) |
Rob Landley | 5e6dca6 | 2012-02-09 06:09:27 -0600 | [diff] [blame] | 254 | long atolx(char *numstr) |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 255 | { |
Rob Landley | 87c06e1 | 2014-07-19 20:54:29 -0500 | [diff] [blame] | 256 | char *c, *suffixes="cbkmgtpe", *end; |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame^] | 257 | long val; |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 258 | |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame^] | 259 | val = xstrtol(numstr, &c, 0); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 260 | if (*c) { |
| 261 | if (c != numstr && (end = strchr(suffixes, tolower(*c)))) { |
Rob Landley | 87c06e1 | 2014-07-19 20:54:29 -0500 | [diff] [blame] | 262 | int shift = end-suffixes-2; |
| 263 | if (shift >= 0) val *= 1024L<<(shift*10); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 264 | } else { |
| 265 | while (isspace(*c)) c++; |
| 266 | if (*c) error_exit("not integer: %s", numstr); |
| 267 | } |
| 268 | } |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 269 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 270 | return val; |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Rob Landley | b5e7416 | 2013-11-28 21:11:34 -0600 | [diff] [blame] | 273 | long atolx_range(char *numstr, long low, long high) |
| 274 | { |
| 275 | long val = atolx(numstr); |
| 276 | |
| 277 | if (val < low) error_exit("%ld < %ld", val, low); |
| 278 | if (val > high) error_exit("%ld > %ld", val, high); |
| 279 | |
| 280 | return val; |
| 281 | } |
| 282 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 283 | int numlen(long l) |
| 284 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 285 | int len = 0; |
| 286 | while (l) { |
| 287 | l /= 10; |
| 288 | len++; |
| 289 | } |
| 290 | return len; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 293 | int stridx(char *haystack, char needle) |
| 294 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 295 | char *off; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 296 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 297 | if (!needle) return -1; |
| 298 | off = strchr(haystack, needle); |
| 299 | if (!off) return -1; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 300 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 301 | return off-haystack; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Rob Landley | 5fcc715 | 2014-10-18 17:14:12 -0500 | [diff] [blame] | 304 | int unescape(char c) |
| 305 | { |
| 306 | char *from = "\\abefnrtv", *to = "\\\a\b\033\f\n\r\t\v"; |
| 307 | int idx = stridx(from, c); |
| 308 | |
| 309 | return (idx == -1) ? 0 : to[idx]; |
| 310 | } |
| 311 | |
Rob Landley | 8115fc1 | 2014-06-09 07:12:49 -0500 | [diff] [blame] | 312 | // If *a starts with b, advance *a past it and return 1, else return 0; |
| 313 | int strstart(char **a, char *b) |
| 314 | { |
| 315 | int len = strlen(b), i = !strncmp(*a, b, len); |
| 316 | |
| 317 | if (i) *a += len; |
| 318 | |
| 319 | return i; |
| 320 | } |
| 321 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 322 | // Return how long the file at fd is, if there's any way to determine it. |
| 323 | off_t fdlength(int fd) |
| 324 | { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 325 | struct stat st; |
| 326 | off_t base = 0, range = 1, expand = 1, old; |
| 327 | |
| 328 | 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] | 329 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 330 | // If the ioctl works for this, return it. |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 331 | // TODO: is blocksize still always 512, or do we stat for it? |
| 332 | // unsigned int size; |
| 333 | // if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 334 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 335 | // If not, do a binary search for the last location we can read. (Some |
| 336 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 337 | // a CONFIG option... |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 338 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 339 | // If not, do a binary search for the last location we can read. |
| 340 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 341 | old = lseek(fd, 0, SEEK_CUR); |
| 342 | do { |
| 343 | char temp; |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 344 | off_t pos = base + range / 2; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 345 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 346 | if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 347 | off_t delta = (pos + 1) - base; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 348 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 349 | base += delta; |
| 350 | if (expand) range = (expand <<= 1) - base; |
| 351 | else range -= delta; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 352 | } else { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 353 | expand = 0; |
| 354 | range = pos - base; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 355 | } |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 356 | } while (range > 0); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 357 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 358 | lseek(fd, old, SEEK_SET); |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 359 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 360 | return base; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 361 | } |
| 362 | |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 363 | // Read contents of file as a single nul-terminated string. |
| 364 | // malloc new one if buf=len=0 |
Ashwini Sharma | 26b2188 | 2014-05-02 06:24:11 -0500 | [diff] [blame] | 365 | char *readfile(char *name, char *ibuf, off_t len) |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 366 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 367 | int fd; |
Ashwini Sharma | 26b2188 | 2014-05-02 06:24:11 -0500 | [diff] [blame] | 368 | char *buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 369 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 370 | fd = open(name, O_RDONLY); |
| 371 | if (fd == -1) return 0; |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 372 | |
| 373 | if (len<1) { |
| 374 | len = fdlength(fd); |
| 375 | // proc files don't report a length, so try 1 page minimum. |
| 376 | if (len<4096) len = 4096; |
| 377 | } |
Ashwini Sharma | 26b2188 | 2014-05-02 06:24:11 -0500 | [diff] [blame] | 378 | if (!ibuf) buf = xmalloc(len+1); |
| 379 | else buf = ibuf; |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 380 | |
| 381 | len = readall(fd, buf, len-1); |
| 382 | close(fd); |
| 383 | if (len<0) { |
Ashwini Sharma | 26b2188 | 2014-05-02 06:24:11 -0500 | [diff] [blame] | 384 | if (ibuf != buf) free(buf); |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 385 | buf = 0; |
| 386 | } else buf[len] = 0; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 387 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 388 | return buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 389 | } |
| 390 | |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 391 | // Sleep for this many thousandths of a second |
| 392 | void msleep(long miliseconds) |
| 393 | { |
| 394 | struct timespec ts; |
| 395 | |
| 396 | ts.tv_sec = miliseconds/1000; |
| 397 | ts.tv_nsec = (miliseconds%1000)*1000000; |
| 398 | nanosleep(&ts, &ts); |
| 399 | } |
| 400 | |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 401 | // Inefficient, but deals with unaligned access |
| 402 | int64_t peek_le(void *ptr, unsigned size) |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 403 | { |
Rob Landley | 546b293 | 2014-07-21 19:56:53 -0500 | [diff] [blame] | 404 | int64_t ret = 0; |
| 405 | char *c = ptr; |
| 406 | int i; |
| 407 | |
| 408 | for (i=0; i<size; i++) ret |= ((int64_t)c[i])<<i; |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | int64_t peek_be(void *ptr, unsigned size) |
| 414 | { |
| 415 | int64_t ret = 0; |
| 416 | char *c = ptr; |
| 417 | |
| 418 | while (size--) ret = (ret<<8)|c[size]; |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | int64_t peek(void *ptr, unsigned size) |
| 424 | { |
| 425 | return IS_BIG_ENDIAN ? peek_be(ptr, size) : peek_le(ptr, size); |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void poke(void *ptr, uint64_t val, int size) |
| 429 | { |
| 430 | if (size & 8) { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 431 | volatile uint64_t *p = (uint64_t *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 432 | *p = val; |
| 433 | } else if (size & 4) { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 434 | volatile int *p = (int *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 435 | *p = val; |
| 436 | } else if (size & 2) { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 437 | volatile short *p = (short *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 438 | *p = val; |
| 439 | } else { |
Rob Landley | a8b88fe | 2014-03-11 18:11:12 -0500 | [diff] [blame] | 440 | volatile char *p = (char *)ptr; |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 441 | *p = val; |
| 442 | } |
| 443 | } |
| 444 | |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 445 | // Iterate through an array of files, opening each one and calling a function |
| 446 | // on that filehandle and name. The special filename "-" means stdin if |
| 447 | // flags is O_RDONLY, stdout otherwise. An empty argument list calls |
| 448 | // function() on just stdin/stdout. |
| 449 | // |
Rob Landley | 784eb9c | 2014-10-14 14:16:34 -0500 | [diff] [blame] | 450 | // Note: pass O_CLOEXEC to automatically close filehandles when function() |
| 451 | // returns, otherwise filehandles must be closed by function() |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 452 | void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 453 | void (*function)(int fd, char *name)) |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 454 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 455 | int fd; |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 456 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 457 | // If no arguments, read from stdin. |
Rob Landley | 45b3882 | 2014-10-27 18:08:59 -0500 | [diff] [blame] | 458 | if (!*argv) function((flags & O_ACCMODE) != O_RDONLY ? 1 : 0, "-"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 459 | else do { |
| 460 | // Filename "-" means read from stdin. |
| 461 | // Inability to open a file prints a warning, but doesn't exit. |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 462 | |
Rob Landley | 45b3882 | 2014-10-27 18:08:59 -0500 | [diff] [blame] | 463 | if (!strcmp(*argv, "-")) fd=0; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 464 | else if (0>(fd = open(*argv, flags, permissions)) && !failok) { |
| 465 | perror_msg("%s", *argv); |
| 466 | toys.exitval = 1; |
| 467 | continue; |
| 468 | } |
| 469 | function(fd, *argv); |
Rob Landley | 784eb9c | 2014-10-14 14:16:34 -0500 | [diff] [blame] | 470 | if (flags & O_CLOEXEC) close(fd); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 471 | } while (*++argv); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 472 | } |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 473 | |
Rob Landley | 784eb9c | 2014-10-14 14:16:34 -0500 | [diff] [blame] | 474 | // Call loopfiles_rw with O_RDONLY|O_CLOEXEC and !failok (common case). |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 475 | void loopfiles(char **argv, void (*function)(int fd, char *name)) |
| 476 | { |
Rob Landley | 784eb9c | 2014-10-14 14:16:34 -0500 | [diff] [blame] | 477 | loopfiles_rw(argv, O_RDONLY|O_CLOEXEC, 0, 0, function); |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 478 | } |
| 479 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 480 | // Slow, but small. |
| 481 | |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 482 | char *get_rawline(int fd, long *plen, char end) |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 483 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 484 | char c, *buf = NULL; |
| 485 | long len = 0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 486 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 487 | for (;;) { |
| 488 | if (1>read(fd, &c, 1)) break; |
| 489 | if (!(len & 63)) buf=xrealloc(buf, len+65); |
| 490 | if ((buf[len++]=c) == end) break; |
| 491 | } |
| 492 | if (buf) buf[len]=0; |
| 493 | if (plen) *plen = len; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 494 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 495 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | char *get_line(int fd) |
| 499 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 500 | long len; |
| 501 | char *buf = get_rawline(fd, &len, '\n'); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 502 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 503 | if (buf && buf[--len]=='\n') buf[len]=0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 504 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 505 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 506 | } |
| 507 | |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 508 | int wfchmodat(int fd, char *name, mode_t mode) |
| 509 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 510 | int rc = fchmodat(fd, name, mode, 0); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 511 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 512 | if (rc) { |
| 513 | perror_msg("chmod '%s' to %04o", name, mode); |
| 514 | toys.exitval=1; |
| 515 | } |
| 516 | return rc; |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 517 | } |
| 518 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 519 | static char *tempfile2zap; |
| 520 | static void tempfile_handler(int i) |
| 521 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 522 | if (1 < (long)tempfile2zap) unlink(tempfile2zap); |
| 523 | _exit(1); |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 526 | // Open a temporary file to copy an existing file into. |
| 527 | int copy_tempfile(int fdin, char *name, char **tempname) |
| 528 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 529 | struct stat statbuf; |
| 530 | int fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 531 | |
Rob Landley | 9b5000c | 2014-12-22 17:04:47 -0600 | [diff] [blame] | 532 | *tempname = xmprintf("%s%s", name, "XXXXXX"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 533 | if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); |
| 534 | if (!tempfile2zap) sigatexit(tempfile_handler); |
| 535 | tempfile2zap = *tempname; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 536 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 537 | // Set permissions of output file |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 538 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 539 | fstat(fdin, &statbuf); |
| 540 | fchmod(fd, statbuf.st_mode); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 541 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 542 | return fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // Abort the copy and delete the temporary file. |
| 546 | void delete_tempfile(int fdin, int fdout, char **tempname) |
| 547 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 548 | close(fdin); |
| 549 | close(fdout); |
| 550 | unlink(*tempname); |
| 551 | tempfile2zap = (char *)1; |
| 552 | free(*tempname); |
| 553 | *tempname = NULL; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // Copy the rest of the data and replace the original with the copy. |
| 557 | void replace_tempfile(int fdin, int fdout, char **tempname) |
| 558 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 559 | char *temp = xstrdup(*tempname); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 560 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 561 | temp[strlen(temp)-6]=0; |
| 562 | if (fdin != -1) { |
| 563 | xsendfile(fdin, fdout); |
| 564 | xclose(fdin); |
| 565 | } |
| 566 | xclose(fdout); |
| 567 | rename(*tempname, temp); |
| 568 | tempfile2zap = (char *)1; |
| 569 | free(*tempname); |
| 570 | free(temp); |
| 571 | *tempname = NULL; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 572 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 573 | |
| 574 | // Create a 256 entry CRC32 lookup table. |
| 575 | |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 576 | void crc_init(unsigned int *crc_table, int little_endian) |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 577 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 578 | unsigned int i; |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 579 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 580 | // Init the CRC32 table (big endian) |
| 581 | for (i=0; i<256; i++) { |
| 582 | unsigned int j, c = little_endian ? i : i<<24; |
| 583 | for (j=8; j; j--) |
| 584 | if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1; |
| 585 | else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
| 586 | crc_table[i] = c; |
| 587 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 588 | } |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 589 | |
Rob Landley | 0517eb7 | 2014-12-13 11:58:08 -0600 | [diff] [blame] | 590 | // Init base64 table |
| 591 | |
| 592 | void base64_init(char *p) |
| 593 | { |
| 594 | int i; |
| 595 | |
| 596 | for (i = 'A'; i != ':'; i++) { |
| 597 | if (i == 'Z'+1) i = 'a'; |
| 598 | if (i == 'z'+1) i = '0'; |
| 599 | *(p++) = i; |
| 600 | } |
| 601 | *(p++) = '+'; |
| 602 | *(p++) = '/'; |
| 603 | } |
| 604 | |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 605 | // Quick and dirty query size of terminal, doesn't do ANSI probe fallback. |
Rob Landley | 10bdaa4 | 2013-11-07 09:04:50 -0600 | [diff] [blame] | 606 | // set x=80 y=25 before calling to provide defaults. Returns 0 if couldn't |
| 607 | // determine size. |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 608 | |
Rob Landley | 10bdaa4 | 2013-11-07 09:04:50 -0600 | [diff] [blame] | 609 | int terminal_size(unsigned *xx, unsigned *yy) |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 610 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 611 | struct winsize ws; |
Rob Landley | 10bdaa4 | 2013-11-07 09:04:50 -0600 | [diff] [blame] | 612 | unsigned i, x = 0, y = 0; |
Rob Landley | c9cc530 | 2013-10-27 00:02:56 -0500 | [diff] [blame] | 613 | char *s; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 614 | |
Rob Landley | 10bdaa4 | 2013-11-07 09:04:50 -0600 | [diff] [blame] | 615 | // stdin, stdout, stderr |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 616 | for (i=0; i<3; i++) { |
Rob Landley | c9cc530 | 2013-10-27 00:02:56 -0500 | [diff] [blame] | 617 | memset(&ws, 0, sizeof(ws)); |
| 618 | if (!ioctl(i, TIOCGWINSZ, &ws)) { |
| 619 | if (ws.ws_col) x = ws.ws_col; |
| 620 | if (ws.ws_row) y = ws.ws_row; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 621 | |
Rob Landley | c9cc530 | 2013-10-27 00:02:56 -0500 | [diff] [blame] | 622 | break; |
| 623 | } |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 624 | } |
Rob Landley | c9cc530 | 2013-10-27 00:02:56 -0500 | [diff] [blame] | 625 | s = getenv("COLUMNS"); |
| 626 | if (s) sscanf(s, "%u", &x); |
| 627 | s = getenv("ROWS"); |
| 628 | if (s) sscanf(s, "%u", &y); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 629 | |
Rob Landley | 10bdaa4 | 2013-11-07 09:04:50 -0600 | [diff] [blame] | 630 | // Never return 0 for either value, leave it at default instead. |
| 631 | if (xx && x) *xx = x; |
| 632 | if (yy && y) *yy = y; |
| 633 | |
| 634 | return x || y; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 635 | } |
| 636 | |
Rob Landley | f793d53 | 2012-02-27 21:56:49 -0600 | [diff] [blame] | 637 | int yesno(char *prompt, int def) |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 638 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 639 | char buf; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 640 | |
Rob Landley | db8eb32 | 2012-12-08 02:25:32 -0600 | [diff] [blame] | 641 | fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); |
| 642 | fflush(stderr); |
| 643 | while (fread(&buf, 1, 1, stdin)) { |
| 644 | int new; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 645 | |
Rob Landley | 2279108 | 2013-01-31 04:13:07 -0600 | [diff] [blame] | 646 | // The letter changes the value, the newline (or space) returns it. |
Rob Landley | db8eb32 | 2012-12-08 02:25:32 -0600 | [diff] [blame] | 647 | if (isspace(buf)) break; |
| 648 | if (-1 != (new = stridx("ny", tolower(buf)))) def = new; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 649 | } |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 650 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 651 | return def; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 652 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 653 | |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 654 | struct signame { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 655 | int num; |
| 656 | char *name; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 657 | }; |
| 658 | |
| 659 | // Signals required by POSIX 2008: |
| 660 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html |
| 661 | |
| 662 | #define SIGNIFY(x) {SIG##x, #x} |
| 663 | |
| 664 | static struct signame signames[] = { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 665 | SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), |
| 666 | SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), |
| 667 | SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), |
| 668 | SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), |
| 669 | SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 670 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 671 | // Start of non-terminal signals |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 672 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 673 | SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), |
| 674 | SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG) |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR) |
| 678 | // obsolete: SIGNIFY(PROF) SIGNIFY(POLL) |
| 679 | |
Rob Landley | 1bc5224 | 2014-05-21 07:24:16 -0500 | [diff] [blame] | 680 | // Handler that sets toys.signal, and writes to toys.signalfd if set |
| 681 | void generic_signal(int sig) |
| 682 | { |
| 683 | if (toys.signalfd) { |
| 684 | char c = sig; |
| 685 | |
| 686 | writeall(toys.signalfd, &c, 1); |
| 687 | } |
| 688 | toys.signal = sig; |
| 689 | } |
| 690 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 691 | // Install the same handler on every signal that defaults to killing the process |
| 692 | void sigatexit(void *handler) |
| 693 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 694 | int i; |
| 695 | 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] | 696 | } |
Rob Landley | 1bc5224 | 2014-05-21 07:24:16 -0500 | [diff] [blame] | 697 | |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 698 | // Convert name to signal number. If name == NULL print names. |
| 699 | int sig_to_num(char *pidstr) |
| 700 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 701 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 702 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 703 | if (pidstr) { |
| 704 | char *s; |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame^] | 705 | |
| 706 | i = estrtol(pidstr, &s, 10); |
| 707 | if (!errno && !*s) return i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 708 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 709 | if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; |
| 710 | } |
| 711 | for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++) |
| 712 | if (!pidstr) xputs(signames[i].name); |
| 713 | else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 714 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 715 | return -1; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | char *num_to_sig(int sig) |
| 719 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 720 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 721 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 722 | for (i=0; i<sizeof(signames)/sizeof(struct signame); i++) |
| 723 | if (signames[i].num == sig) return signames[i].name; |
| 724 | return NULL; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 725 | } |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 726 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 727 | // premute mode bits based on posix mode strings. |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 728 | mode_t string_to_mode(char *modestr, mode_t mode) |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 729 | { |
Rob Landley | 3b5b19e | 2014-08-15 10:50:39 -0500 | [diff] [blame] | 730 | char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu", |
| 731 | *s, *str = modestr; |
| 732 | mode_t extrabits = mode & ~(07777); |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 733 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 734 | // Handle octal mode |
| 735 | if (isdigit(*str)) { |
Rob Landley | 86c747a | 2015-01-01 16:28:51 -0600 | [diff] [blame^] | 736 | mode = estrtol(str, &s, 8); |
| 737 | if (errno || *s || (mode & ~(07777))) goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 738 | |
Rob Landley | 3b5b19e | 2014-08-15 10:50:39 -0500 | [diff] [blame] | 739 | return mode | extrabits; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 740 | } |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 741 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 742 | // Gaze into the bin of permission... |
| 743 | for (;;) { |
| 744 | int i, j, dowho, dohow, dowhat, amask; |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 745 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 746 | dowho = dohow = dowhat = amask = 0; |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 747 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 748 | // Find the who, how, and what stanzas, in that order |
| 749 | while (*str && (s = strchr(whos, *str))) { |
| 750 | dowho |= 1<<(s-whos); |
| 751 | str++; |
| 752 | } |
| 753 | // If who isn't specified, like "a" but honoring umask. |
| 754 | if (!dowho) { |
| 755 | dowho = 8; |
| 756 | umask(amask=umask(0)); |
| 757 | } |
| 758 | if (!*str || !(s = strchr(hows, *str))) goto barf; |
| 759 | dohow = *(str++); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 760 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 761 | if (!dohow) goto barf; |
| 762 | while (*str && (s = strchr(whats, *str))) { |
| 763 | dowhat |= 1<<(s-whats); |
| 764 | str++; |
| 765 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 766 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 767 | // Convert X to x for directory or if already executable somewhere |
| 768 | if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 769 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 770 | // Copy mode from another category? |
| 771 | if (!dowhat && *str && (s = strchr(whys, *str))) { |
| 772 | dowhat = (mode>>(3*(s-whys)))&7; |
| 773 | str++; |
| 774 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 775 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 776 | // Are we ready to do a thing yet? |
| 777 | if (*str && *(str++) != ',') goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 778 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 779 | // Ok, apply the bits to the mode. |
| 780 | for (i=0; i<4; i++) { |
| 781 | for (j=0; j<3; j++) { |
| 782 | mode_t bit = 0; |
| 783 | int where = 1<<((3*i)+j); |
Rob Landley | 31f49e7 | 2012-07-21 22:45:05 -0500 | [diff] [blame] | 784 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 785 | if (amask & where) continue; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 786 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 787 | // Figure out new value at this location |
| 788 | if (i == 3) { |
| 789 | // suid/sticky bit. |
| 790 | if (j) { |
| 791 | if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++; |
| 792 | } else if (dowhat & 16) bit++; |
| 793 | } else { |
| 794 | if (!(dowho&(8|(1<<i)))) continue; |
| 795 | if (dowhat&(1<<j)) bit++; |
| 796 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 797 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 798 | // When selection active, modify bit |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 799 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 800 | if (dohow == '=' || (bit && dohow == '-')) mode &= ~where; |
| 801 | if (bit && dohow != '-') mode |= where; |
| 802 | } |
| 803 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 804 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 805 | if (!*str) break; |
| 806 | } |
Rob Landley | 3b5b19e | 2014-08-15 10:50:39 -0500 | [diff] [blame] | 807 | |
| 808 | return mode|extrabits; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 809 | barf: |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 810 | error_exit("bad mode '%s'", modestr); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 811 | } |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 812 | |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 813 | // Format access mode into a drwxrwxrwx string |
| 814 | void mode_to_string(mode_t mode, char *buf) |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 815 | { |
| 816 | char c, d; |
| 817 | int i, bit; |
| 818 | |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 819 | buf[10]=0; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 820 | for (i=0; i<9; i++) { |
| 821 | bit = mode & (1<<i); |
| 822 | c = i%3; |
| 823 | if (!c && (mode & (1<<((d=i/3)+9)))) { |
| 824 | c = "tss"[d]; |
| 825 | if (!bit) c &= ~0x20; |
| 826 | } else c = bit ? "xwr"[c] : '-'; |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 827 | buf[9-i] = c; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (S_ISDIR(mode)) c = 'd'; |
| 831 | else if (S_ISBLK(mode)) c = 'b'; |
| 832 | else if (S_ISCHR(mode)) c = 'c'; |
| 833 | else if (S_ISLNK(mode)) c = 'l'; |
| 834 | else if (S_ISFIFO(mode)) c = 'p'; |
| 835 | else if (S_ISSOCK(mode)) c = 's'; |
| 836 | else c = '-'; |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 837 | *buf = c; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 838 | } |
Rob Landley | db1009d | 2013-12-19 09:32:30 -0600 | [diff] [blame] | 839 | |
| 840 | // Execute a callback for each PID that matches a process name from a list. |
| 841 | void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)) |
| 842 | { |
| 843 | DIR *dp; |
| 844 | struct dirent *entry; |
| 845 | |
| 846 | if (!(dp = opendir("/proc"))) perror_exit("opendir"); |
| 847 | |
| 848 | while ((entry = readdir(dp))) { |
| 849 | unsigned u; |
| 850 | char *cmd, **curname; |
| 851 | |
| 852 | if (!(u = atoi(entry->d_name))) continue; |
| 853 | sprintf(libbuf, "/proc/%u/cmdline", u); |
| 854 | if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue; |
| 855 | |
| 856 | for (curname = names; *curname; curname++) |
| 857 | if (**curname == '/' ? !strcmp(cmd, *curname) |
| 858 | : !strcmp(basename(cmd), basename(*curname))) |
| 859 | if (callback(u, *curname)) break; |
| 860 | if (*curname) break; |
| 861 | } |
| 862 | closedir(dp); |
| 863 | } |
Rob Landley | 48c172b | 2014-05-06 06:31:28 -0500 | [diff] [blame] | 864 | |
| 865 | // display first few digits of number with power of two units, except we're |
| 866 | // actually just counting decimal digits and showing mil/bil/trillions. |
| 867 | int human_readable(char *buf, unsigned long long num) |
| 868 | { |
| 869 | int end, len; |
| 870 | |
| 871 | len = sprintf(buf, "%lld", num); |
| 872 | end = ((len-1)%3)+1; |
| 873 | len /= 3; |
| 874 | |
| 875 | if (len && end == 1) { |
| 876 | buf[2] = buf[1]; |
| 877 | buf[1] = '.'; |
| 878 | end = 3; |
| 879 | } |
| 880 | buf[end++] = ' '; |
| 881 | if (len) buf[end++] = " KMGTPE"[len]; |
| 882 | buf[end++] = 'B'; |
| 883 | buf[end++] = 0; |
| 884 | |
| 885 | return end; |
| 886 | } |