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) { |
| 70 | int i = read(fd, buf+count, len-count); |
| 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) { |
| 84 | int i = write(fd, buf+count, len-count); |
| 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); |
| 112 | else offset -= try; |
| 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 | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 119 | // Split a path into linked list of components, tracking head and tail of list. |
| 120 | // Filters out // entries with no contents. |
| 121 | struct string_list **splitpath(char *path, struct string_list **list) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 122 | { |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 123 | char *new = path; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 124 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 125 | *list = 0; |
| 126 | do { |
| 127 | int len; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 128 | |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 129 | if (*path && *path != '/') continue; |
| 130 | len = path-new; |
| 131 | if (len > 0) { |
| 132 | *list = xmalloc(sizeof(struct string_list) + len + 1); |
| 133 | (*list)->next = 0; |
| 134 | strncpy((*list)->str, new, len); |
| 135 | (*list)->str[len] = 0; |
| 136 | list = &(*list)->next; |
| 137 | } |
| 138 | new = path+1; |
| 139 | } while (*path++); |
| 140 | |
| 141 | return list; |
| 142 | } |
| 143 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 144 | // Find all file in a colon-separated path with access type "type" (generally |
| 145 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 146 | // order. |
| 147 | |
| 148 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 149 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 150 | struct string_list *rlist = NULL, **prlist=&rlist; |
| 151 | char *cwd = xgetcwd(); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 152 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 153 | for (;;) { |
| 154 | char *next = path ? strchr(path, ':') : NULL; |
| 155 | int len = next ? next-path : strlen(path); |
| 156 | struct string_list *rnext; |
| 157 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 158 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 159 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 160 | + (len ? len : strlen(cwd)) + 2); |
| 161 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
| 162 | else { |
| 163 | char *res = rnext->str; |
| 164 | strncpy(res, path, len); |
| 165 | res += len; |
| 166 | *(res++) = '/'; |
| 167 | strcpy(res, filename); |
| 168 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 169 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 170 | // Confirm it's not a directory. |
| 171 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
| 172 | *prlist = rnext; |
| 173 | rnext->next = NULL; |
| 174 | prlist = &(rnext->next); |
| 175 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 176 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 177 | if (!next) break; |
| 178 | path += len; |
| 179 | path++; |
| 180 | } |
| 181 | free(cwd); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 182 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 183 | return rlist; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 184 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 185 | |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 186 | // atol() with the kilo/mega/giga/tera/peta/exa extensions. |
| 187 | // (zetta and yotta don't fit in 64 bits.) |
Rob Landley | 5e6dca6 | 2012-02-09 06:09:27 -0600 | [diff] [blame] | 188 | long atolx(char *numstr) |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 189 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 190 | char *c, *suffixes="bkmgtpe", *end; |
| 191 | long val = strtol(numstr, &c, 0); |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 192 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 193 | if (*c) { |
| 194 | if (c != numstr && (end = strchr(suffixes, tolower(*c)))) { |
| 195 | int shift = end-suffixes; |
| 196 | if (shift--) val *= 1024L<<(shift*10); |
| 197 | } else { |
| 198 | while (isspace(*c)) c++; |
| 199 | if (*c) error_exit("not integer: %s", numstr); |
| 200 | } |
| 201 | } |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 202 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 203 | return val; |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 206 | int numlen(long l) |
| 207 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 208 | int len = 0; |
| 209 | while (l) { |
| 210 | l /= 10; |
| 211 | len++; |
| 212 | } |
| 213 | return len; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 216 | int stridx(char *haystack, char needle) |
| 217 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 218 | char *off; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 219 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 220 | if (!needle) return -1; |
| 221 | off = strchr(haystack, needle); |
| 222 | if (!off) return -1; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 223 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 224 | return off-haystack; |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 227 | // Return how long the file at fd is, if there's any way to determine it. |
| 228 | off_t fdlength(int fd) |
| 229 | { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 230 | struct stat st; |
| 231 | off_t base = 0, range = 1, expand = 1, old; |
| 232 | |
| 233 | 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] | 234 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 235 | // If the ioctl works for this, return it. |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 236 | // TODO: is blocksize still always 512, or do we stat for it? |
| 237 | // unsigned int size; |
| 238 | // if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 239 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 240 | // If not, do a binary search for the last location we can read. (Some |
| 241 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 242 | // a CONFIG option... |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 243 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 244 | // If not, do a binary search for the last location we can read. |
| 245 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 246 | old = lseek(fd, 0, SEEK_CUR); |
| 247 | do { |
| 248 | char temp; |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 249 | off_t pos = base + range / 2; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 250 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 251 | if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 252 | off_t delta = (pos + 1) - base; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 253 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 254 | base += delta; |
| 255 | if (expand) range = (expand <<= 1) - base; |
| 256 | else range -= delta; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 257 | } else { |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 258 | expand = 0; |
| 259 | range = pos - base; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 260 | } |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 261 | } while (range > 0); |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 262 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 263 | lseek(fd, old, SEEK_SET); |
Rob Landley | f15387d | 2008-07-18 05:43:44 -0500 | [diff] [blame] | 264 | |
Rob Landley | 035f27a | 2013-08-08 02:46:45 -0500 | [diff] [blame] | 265 | return base; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 268 | // Read contents of file as a single nul-terminated string. |
| 269 | // malloc new one if buf=len=0 |
| 270 | char *readfile(char *name, char *buf, off_t len) |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 271 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 272 | int fd; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 273 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 274 | fd = open(name, O_RDONLY); |
| 275 | if (fd == -1) return 0; |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 276 | |
| 277 | if (len<1) { |
| 278 | len = fdlength(fd); |
| 279 | // proc files don't report a length, so try 1 page minimum. |
| 280 | if (len<4096) len = 4096; |
| 281 | } |
| 282 | if (!buf) buf = xmalloc(len+1); |
| 283 | |
| 284 | len = readall(fd, buf, len-1); |
| 285 | close(fd); |
| 286 | if (len<0) { |
| 287 | free(buf); |
| 288 | buf = 0; |
| 289 | } else buf[len] = 0; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 290 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 291 | return buf; |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 294 | // Sleep for this many thousandths of a second |
| 295 | void msleep(long miliseconds) |
| 296 | { |
| 297 | struct timespec ts; |
| 298 | |
| 299 | ts.tv_sec = miliseconds/1000; |
| 300 | ts.tv_nsec = (miliseconds%1000)*1000000; |
| 301 | nanosleep(&ts, &ts); |
| 302 | } |
| 303 | |
Rob Landley | 6b28341 | 2013-06-01 20:41:35 -0500 | [diff] [blame] | 304 | int64_t peek(void *ptr, int size) |
| 305 | { |
| 306 | if (size & 8) { |
| 307 | int64_t *p = (int64_t *)ptr; |
| 308 | return *p; |
| 309 | } else if (size & 4) { |
| 310 | int *p = (int *)ptr; |
| 311 | return *p; |
| 312 | } else if (size & 2) { |
| 313 | short *p = (short *)ptr; |
| 314 | return *p; |
| 315 | } else { |
| 316 | char *p = (char *)ptr; |
| 317 | return *p; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void poke(void *ptr, uint64_t val, int size) |
| 322 | { |
| 323 | if (size & 8) { |
| 324 | uint64_t *p = (uint64_t *)ptr; |
| 325 | *p = val; |
| 326 | } else if (size & 4) { |
| 327 | int *p = (int *)ptr; |
| 328 | *p = val; |
| 329 | } else if (size & 2) { |
| 330 | short *p = (short *)ptr; |
| 331 | *p = val; |
| 332 | } else { |
| 333 | char *p = (char *)ptr; |
| 334 | *p = val; |
| 335 | } |
| 336 | } |
| 337 | |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 338 | // Iterate through an array of files, opening each one and calling a function |
| 339 | // on that filehandle and name. The special filename "-" means stdin if |
| 340 | // flags is O_RDONLY, stdout otherwise. An empty argument list calls |
| 341 | // function() on just stdin/stdout. |
| 342 | // |
| 343 | // Note: read only filehandles are automatically closed when function() |
| 344 | // returns, but writeable filehandles must be close by function() |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 345 | void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 346 | void (*function)(int fd, char *name)) |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 347 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 348 | int fd; |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 349 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 350 | // If no arguments, read from stdin. |
| 351 | if (!*argv) function(flags ? 1 : 0, "-"); |
| 352 | else do { |
| 353 | // Filename "-" means read from stdin. |
| 354 | // Inability to open a file prints a warning, but doesn't exit. |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 355 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 356 | if (!strcmp(*argv,"-")) fd=0; |
| 357 | else if (0>(fd = open(*argv, flags, permissions)) && !failok) { |
| 358 | perror_msg("%s", *argv); |
| 359 | toys.exitval = 1; |
| 360 | continue; |
| 361 | } |
| 362 | function(fd, *argv); |
| 363 | if (flags == O_RDONLY) close(fd); |
| 364 | } while (*++argv); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 365 | } |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 366 | |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 367 | // Call loopfiles_rw with O_RDONLY and !failok (common case). |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 368 | void loopfiles(char **argv, void (*function)(int fd, char *name)) |
| 369 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 370 | loopfiles_rw(argv, O_RDONLY, 0, 0, function); |
Rob Landley | 2bfaaf2 | 2008-07-03 19:19:00 -0500 | [diff] [blame] | 371 | } |
| 372 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 373 | // Slow, but small. |
| 374 | |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 375 | char *get_rawline(int fd, long *plen, char end) |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 376 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 377 | char c, *buf = NULL; |
| 378 | long len = 0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 379 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 380 | for (;;) { |
| 381 | if (1>read(fd, &c, 1)) break; |
| 382 | if (!(len & 63)) buf=xrealloc(buf, len+65); |
| 383 | if ((buf[len++]=c) == end) break; |
| 384 | } |
| 385 | if (buf) buf[len]=0; |
| 386 | if (plen) *plen = len; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 387 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 388 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | char *get_line(int fd) |
| 392 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 393 | long len; |
| 394 | char *buf = get_rawline(fd, &len, '\n'); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 395 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 396 | if (buf && buf[--len]=='\n') buf[len]=0; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 397 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 398 | return buf; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 399 | } |
| 400 | |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 401 | int wfchmodat(int fd, char *name, mode_t mode) |
| 402 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 403 | int rc = fchmodat(fd, name, mode, 0); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 404 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 405 | if (rc) { |
| 406 | perror_msg("chmod '%s' to %04o", name, mode); |
| 407 | toys.exitval=1; |
| 408 | } |
| 409 | return rc; |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 412 | static char *tempfile2zap; |
| 413 | static void tempfile_handler(int i) |
| 414 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 415 | if (1 < (long)tempfile2zap) unlink(tempfile2zap); |
| 416 | _exit(1); |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 419 | // Open a temporary file to copy an existing file into. |
| 420 | int copy_tempfile(int fdin, char *name, char **tempname) |
| 421 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 422 | struct stat statbuf; |
| 423 | int fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 424 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 425 | *tempname = xstrndup(name, strlen(name)+6); |
| 426 | strcat(*tempname,"XXXXXX"); |
| 427 | if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); |
| 428 | if (!tempfile2zap) sigatexit(tempfile_handler); |
| 429 | tempfile2zap = *tempname; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 430 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 431 | // Set permissions of output file |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 432 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 433 | fstat(fdin, &statbuf); |
| 434 | fchmod(fd, statbuf.st_mode); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 435 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 436 | return fd; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // Abort the copy and delete the temporary file. |
| 440 | void delete_tempfile(int fdin, int fdout, char **tempname) |
| 441 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 442 | close(fdin); |
| 443 | close(fdout); |
| 444 | unlink(*tempname); |
| 445 | tempfile2zap = (char *)1; |
| 446 | free(*tempname); |
| 447 | *tempname = NULL; |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Copy the rest of the data and replace the original with the copy. |
| 451 | void replace_tempfile(int fdin, int fdout, char **tempname) |
| 452 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 453 | char *temp = xstrdup(*tempname); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 454 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 455 | temp[strlen(temp)-6]=0; |
| 456 | if (fdin != -1) { |
| 457 | xsendfile(fdin, fdout); |
| 458 | xclose(fdin); |
| 459 | } |
| 460 | xclose(fdout); |
| 461 | rename(*tempname, temp); |
| 462 | tempfile2zap = (char *)1; |
| 463 | free(*tempname); |
| 464 | free(temp); |
| 465 | *tempname = NULL; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 466 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 467 | |
| 468 | // Create a 256 entry CRC32 lookup table. |
| 469 | |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 470 | void crc_init(unsigned int *crc_table, int little_endian) |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 471 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 472 | unsigned int i; |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 473 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 474 | // Init the CRC32 table (big endian) |
| 475 | for (i=0; i<256; i++) { |
| 476 | unsigned int j, c = little_endian ? i : i<<24; |
| 477 | for (j=8; j; j--) |
| 478 | if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1; |
| 479 | else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
| 480 | crc_table[i] = c; |
| 481 | } |
Rob Landley | 7e849c5 | 2009-01-03 18:15:18 -0600 | [diff] [blame] | 482 | } |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 483 | |
| 484 | // Quick and dirty query size of terminal, doesn't do ANSI probe fallback. |
| 485 | // set *x=0 and *y=0 before calling to detect failure to set either, or |
| 486 | // x=80 y=25 to provide defaults |
| 487 | |
| 488 | void terminal_size(unsigned *x, unsigned *y) |
| 489 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 490 | struct winsize ws; |
| 491 | int i; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 492 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 493 | //memset(&ws, 0, sizeof(ws)); |
| 494 | for (i=0; i<3; i++) { |
| 495 | if (ioctl(i, TIOCGWINSZ, &ws)) continue; |
| 496 | if (x) *x = ws.ws_col; |
| 497 | if (y) *y = ws.ws_row; |
| 498 | } |
| 499 | if (x) { |
| 500 | char *s = getenv("COLUMNS"); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 501 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 502 | i = s ? atoi(s) : 0; |
| 503 | if (i>0) *x = i; |
| 504 | } |
| 505 | if (y) { |
| 506 | char *s = getenv("ROWS"); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 507 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 508 | i = s ? atoi(s) : 0; |
| 509 | if (i>0) *y = i; |
| 510 | } |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 511 | } |
| 512 | |
Rob Landley | f793d53 | 2012-02-27 21:56:49 -0600 | [diff] [blame] | 513 | int yesno(char *prompt, int def) |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 514 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 515 | char buf; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 516 | |
Rob Landley | db8eb32 | 2012-12-08 02:25:32 -0600 | [diff] [blame] | 517 | fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N'); |
| 518 | fflush(stderr); |
| 519 | while (fread(&buf, 1, 1, stdin)) { |
| 520 | int new; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 521 | |
Rob Landley | 2279108 | 2013-01-31 04:13:07 -0600 | [diff] [blame] | 522 | // The letter changes the value, the newline (or space) returns it. |
Rob Landley | db8eb32 | 2012-12-08 02:25:32 -0600 | [diff] [blame] | 523 | if (isspace(buf)) break; |
| 524 | if (-1 != (new = stridx("ny", tolower(buf)))) def = new; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 525 | } |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 526 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 527 | return def; |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 528 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 529 | |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 530 | struct signame { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 531 | int num; |
| 532 | char *name; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | // Signals required by POSIX 2008: |
| 536 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html |
| 537 | |
| 538 | #define SIGNIFY(x) {SIG##x, #x} |
| 539 | |
| 540 | static struct signame signames[] = { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 541 | SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), |
| 542 | SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), |
| 543 | SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), |
| 544 | SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), |
| 545 | SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 546 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 547 | // Start of non-terminal signals |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 548 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 549 | SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), |
| 550 | SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG) |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 551 | }; |
| 552 | |
| 553 | // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR) |
| 554 | // obsolete: SIGNIFY(PROF) SIGNIFY(POLL) |
| 555 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 556 | // Install the same handler on every signal that defaults to killing the process |
| 557 | void sigatexit(void *handler) |
| 558 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 559 | int i; |
| 560 | 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] | 561 | } |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 562 | // Convert name to signal number. If name == NULL print names. |
| 563 | int sig_to_num(char *pidstr) |
| 564 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 565 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 566 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 567 | if (pidstr) { |
| 568 | char *s; |
| 569 | i = strtol(pidstr, &s, 10); |
| 570 | if (!*s) return i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 571 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 572 | if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; |
| 573 | } |
| 574 | for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++) |
| 575 | if (!pidstr) xputs(signames[i].name); |
| 576 | else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 577 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 578 | return -1; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | char *num_to_sig(int sig) |
| 582 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 583 | int i; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 584 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 585 | for (i=0; i<sizeof(signames)/sizeof(struct signame); i++) |
| 586 | if (signames[i].num == sig) return signames[i].name; |
| 587 | return NULL; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 588 | } |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 589 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 590 | // premute mode bits based on posix mode strings. |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 591 | mode_t string_to_mode(char *modestr, mode_t mode) |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 592 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 593 | char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu"; |
| 594 | char *s, *str = modestr; |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 595 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 596 | // Handle octal mode |
| 597 | if (isdigit(*str)) { |
| 598 | mode = strtol(str, &s, 8); |
| 599 | if (*s || (mode & ~(07777))) goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 600 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 601 | return mode; |
| 602 | } |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 603 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 604 | // Gaze into the bin of permission... |
| 605 | for (;;) { |
| 606 | int i, j, dowho, dohow, dowhat, amask; |
Rob Landley | cf6bcb2 | 2012-03-19 20:56:18 -0500 | [diff] [blame] | 607 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 608 | dowho = dohow = dowhat = amask = 0; |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 609 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 610 | // Find the who, how, and what stanzas, in that order |
| 611 | while (*str && (s = strchr(whos, *str))) { |
| 612 | dowho |= 1<<(s-whos); |
| 613 | str++; |
| 614 | } |
| 615 | // If who isn't specified, like "a" but honoring umask. |
| 616 | if (!dowho) { |
| 617 | dowho = 8; |
| 618 | umask(amask=umask(0)); |
| 619 | } |
| 620 | if (!*str || !(s = strchr(hows, *str))) goto barf; |
| 621 | dohow = *(str++); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 622 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 623 | if (!dohow) goto barf; |
| 624 | while (*str && (s = strchr(whats, *str))) { |
| 625 | dowhat |= 1<<(s-whats); |
| 626 | str++; |
| 627 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 628 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 629 | // Convert X to x for directory or if already executable somewhere |
| 630 | if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 631 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 632 | // Copy mode from another category? |
| 633 | if (!dowhat && *str && (s = strchr(whys, *str))) { |
| 634 | dowhat = (mode>>(3*(s-whys)))&7; |
| 635 | str++; |
| 636 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 637 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 638 | // Are we ready to do a thing yet? |
| 639 | if (*str && *(str++) != ',') goto barf; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 640 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 641 | // Ok, apply the bits to the mode. |
| 642 | for (i=0; i<4; i++) { |
| 643 | for (j=0; j<3; j++) { |
| 644 | mode_t bit = 0; |
| 645 | int where = 1<<((3*i)+j); |
Rob Landley | 31f49e7 | 2012-07-21 22:45:05 -0500 | [diff] [blame] | 646 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 647 | if (amask & where) continue; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 648 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 649 | // Figure out new value at this location |
| 650 | if (i == 3) { |
| 651 | // suid/sticky bit. |
| 652 | if (j) { |
| 653 | if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++; |
| 654 | } else if (dowhat & 16) bit++; |
| 655 | } else { |
| 656 | if (!(dowho&(8|(1<<i)))) continue; |
| 657 | if (dowhat&(1<<j)) bit++; |
| 658 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 659 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 660 | // When selection active, modify bit |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 661 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 662 | if (dohow == '=' || (bit && dohow == '-')) mode &= ~where; |
| 663 | if (bit && dohow != '-') mode |= where; |
| 664 | } |
| 665 | } |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 666 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 667 | if (!*str) break; |
| 668 | } |
| 669 | return mode; |
Rob Landley | b6f601e | 2012-05-16 21:11:43 -0500 | [diff] [blame] | 670 | barf: |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 671 | error_exit("bad mode '%s'", modestr); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 672 | } |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 673 | |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 674 | // Format access mode into a drwxrwxrwx string |
| 675 | void mode_to_string(mode_t mode, char *buf) |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 676 | { |
| 677 | char c, d; |
| 678 | int i, bit; |
| 679 | |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 680 | buf[10]=0; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 681 | for (i=0; i<9; i++) { |
| 682 | bit = mode & (1<<i); |
| 683 | c = i%3; |
| 684 | if (!c && (mode & (1<<((d=i/3)+9)))) { |
| 685 | c = "tss"[d]; |
| 686 | if (!bit) c &= ~0x20; |
| 687 | } else c = bit ? "xwr"[c] : '-'; |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 688 | buf[9-i] = c; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | if (S_ISDIR(mode)) c = 'd'; |
| 692 | else if (S_ISBLK(mode)) c = 'b'; |
| 693 | else if (S_ISCHR(mode)) c = 'c'; |
| 694 | else if (S_ISLNK(mode)) c = 'l'; |
| 695 | else if (S_ISFIFO(mode)) c = 'p'; |
| 696 | else if (S_ISSOCK(mode)) c = 's'; |
| 697 | else c = '-'; |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 698 | *buf = c; |
Felix Janda | 5a221e6 | 2013-04-22 22:29:43 +0200 | [diff] [blame] | 699 | } |