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