blob: 38d9cc2116566d8af7c0a4873b7810d05d9a7746 [file] [log] [blame]
Rob Landleyd3904932013-07-16 00:04:56 -05001/* lib.c - various reusable stuff.
landley09ea7ac2006-10-30 01:38:00 -05002 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
landley4f344e32006-10-05 16:18:03 -04004 */
5
6#include "toys.h"
7
landley09ea7ac2006-10-30 01:38:00 -05008void verror_msg(char *msg, int err, va_list va)
9{
Rob Landley7aa651a2012-11-13 17:14:08 -060010 char *s = ": %s";
Rob Landley12138e42008-01-27 15:26:08 -060011
Rob Landley7aa651a2012-11-13 17:14:08 -060012 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 Landley662a2672013-01-02 02:00:35 -060017 if (!toys.exitval) toys.exitval++;
landley09ea7ac2006-10-30 01:38:00 -050018}
19
20void error_msg(char *msg, ...)
21{
Rob Landley7aa651a2012-11-13 17:14:08 -060022 va_list va;
landley09ea7ac2006-10-30 01:38:00 -050023
Rob Landley7aa651a2012-11-13 17:14:08 -060024 va_start(va, msg);
25 verror_msg(msg, 0, va);
26 va_end(va);
landley09ea7ac2006-10-30 01:38:00 -050027}
28
29void perror_msg(char *msg, ...)
30{
Rob Landley7aa651a2012-11-13 17:14:08 -060031 va_list va;
landley09ea7ac2006-10-30 01:38:00 -050032
Rob Landley7aa651a2012-11-13 17:14:08 -060033 va_start(va, msg);
34 verror_msg(msg, errno, va);
35 va_end(va);
landley09ea7ac2006-10-30 01:38:00 -050036}
37
landley4f344e32006-10-05 16:18:03 -040038// Die with an error message.
39void error_exit(char *msg, ...)
40{
Rob Landley7aa651a2012-11-13 17:14:08 -060041 va_list va;
landley4f344e32006-10-05 16:18:03 -040042
Rob Landley36ffc5a2013-04-14 21:43:22 -050043 if (CFG_TOYBOX_HELP && toys.exithelp) show_help();
Rob Landleyd06c58d2007-10-11 15:36:36 -050044
Rob Landley7aa651a2012-11-13 17:14:08 -060045 va_start(va, msg);
46 verror_msg(msg, 0, va);
47 va_end(va);
landley09ea7ac2006-10-30 01:38:00 -050048
Rob Landley55830302013-06-16 19:59:51 -050049 xexit();
landley09ea7ac2006-10-30 01:38:00 -050050}
51
52// Die with an error message and strerror(errno)
53void perror_exit(char *msg, ...)
54{
Rob Landley7aa651a2012-11-13 17:14:08 -060055 va_list va;
landley09ea7ac2006-10-30 01:38:00 -050056
Rob Landley7aa651a2012-11-13 17:14:08 -060057 va_start(va, msg);
58 verror_msg(msg, errno, va);
59 va_end(va);
landley09ea7ac2006-10-30 01:38:00 -050060
Rob Landley55830302013-06-16 19:59:51 -050061 xexit();
landley4f344e32006-10-05 16:18:03 -040062}
63
landley64b2e232006-10-30 10:01:19 -050064// Keep reading until full or EOF
Rob Landley90163772007-01-18 21:54:08 -050065ssize_t readall(int fd, void *buf, size_t len)
landley64b2e232006-10-30 10:01:19 -050066{
Rob Landley7aa651a2012-11-13 17:14:08 -060067 size_t count = 0;
Rob Landley15b23152008-07-18 04:15:59 -050068
Rob Landley7aa651a2012-11-13 17:14:08 -060069 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 }
landley64b2e232006-10-30 10:01:19 -050075
Rob Landley7aa651a2012-11-13 17:14:08 -060076 return count;
landley64b2e232006-10-30 10:01:19 -050077}
78
Rob Landleyf3e452a2007-01-08 02:49:39 -050079// Keep writing until done or EOF
Rob Landley90163772007-01-18 21:54:08 -050080ssize_t writeall(int fd, void *buf, size_t len)
Rob Landleyf3e452a2007-01-08 02:49:39 -050081{
Rob Landley7aa651a2012-11-13 17:14:08 -060082 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 Landleyf3e452a2007-01-08 02:49:39 -050088
Rob Landley7aa651a2012-11-13 17:14:08 -060089 return count;
Rob Landleyf3e452a2007-01-08 02:49:39 -050090}
91
Rob Landley2037b832012-07-15 16:56:20 -050092off_t lskip(int fd, off_t offset)
93{
Rob Landley7aa651a2012-11-13 17:14:08 -060094 off_t and = lseek(fd, offset, SEEK_CUR);
Rob Landley2037b832012-07-15 16:56:20 -050095
Rob Landley7aa651a2012-11-13 17:14:08 -060096 if (and != -1 && offset >= lseek(fd, offset, SEEK_END)
97 && offset+and == lseek(fd, offset+and, SEEK_SET)) return 0;
98 else {
Rob Landley7aa651a2012-11-13 17:14:08 -060099 while (offset>0) {
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500100 int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or;
Rob Landley2037b832012-07-15 16:56:20 -0500101
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500102 or = readall(fd, libbuf, try);
Rob Landley7aa651a2012-11-13 17:14:08 -0600103 if (or < 0) perror_msg("lskip to %lld", (long long)offset);
104 else offset -= try;
105 if (or < try) break;
106 }
Rob Landley2037b832012-07-15 16:56:20 -0500107
Rob Landley7aa651a2012-11-13 17:14:08 -0600108 return offset;
109 }
Rob Landley2037b832012-07-15 16:56:20 -0500110}
111
Rob Landleyfe91e682012-11-22 21:18:09 -0600112// Split a path into linked list of components, tracking head and tail of list.
113// Filters out // entries with no contents.
114struct string_list **splitpath(char *path, struct string_list **list)
landley00f87f12006-10-25 18:38:37 -0400115{
Rob Landleyfe91e682012-11-22 21:18:09 -0600116 char *new = path;
landley00f87f12006-10-25 18:38:37 -0400117
Rob Landleyfe91e682012-11-22 21:18:09 -0600118 *list = 0;
119 do {
120 int len;
landley00f87f12006-10-25 18:38:37 -0400121
Rob Landleyfe91e682012-11-22 21:18:09 -0600122 if (*path && *path != '/') continue;
123 len = path-new;
124 if (len > 0) {
125 *list = xmalloc(sizeof(struct string_list) + len + 1);
126 (*list)->next = 0;
127 strncpy((*list)->str, new, len);
128 (*list)->str[len] = 0;
129 list = &(*list)->next;
130 }
131 new = path+1;
132 } while (*path++);
133
134 return list;
135}
136
Rob Landley0a04b3e2006-11-03 00:05:52 -0500137// Find all file in a colon-separated path with access type "type" (generally
138// X_OK or R_OK). Returns a list of absolute paths to each file found, in
139// order.
140
141struct string_list *find_in_path(char *path, char *filename)
Rob Landleyfa98d012006-11-02 02:57:27 -0500142{
Rob Landley7aa651a2012-11-13 17:14:08 -0600143 struct string_list *rlist = NULL, **prlist=&rlist;
144 char *cwd = xgetcwd();
Rob Landleyfa98d012006-11-02 02:57:27 -0500145
Rob Landley7aa651a2012-11-13 17:14:08 -0600146 for (;;) {
147 char *next = path ? strchr(path, ':') : NULL;
148 int len = next ? next-path : strlen(path);
149 struct string_list *rnext;
150 struct stat st;
Rob Landleyfa98d012006-11-02 02:57:27 -0500151
Rob Landley7aa651a2012-11-13 17:14:08 -0600152 rnext = xmalloc(sizeof(void *) + strlen(filename)
153 + (len ? len : strlen(cwd)) + 2);
154 if (!len) sprintf(rnext->str, "%s/%s", cwd, filename);
155 else {
156 char *res = rnext->str;
157 strncpy(res, path, len);
158 res += len;
159 *(res++) = '/';
160 strcpy(res, filename);
161 }
Rob Landleyfa98d012006-11-02 02:57:27 -0500162
Rob Landley7aa651a2012-11-13 17:14:08 -0600163 // Confirm it's not a directory.
164 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) {
165 *prlist = rnext;
166 rnext->next = NULL;
167 prlist = &(rnext->next);
168 } else free(rnext);
Rob Landleyfa98d012006-11-02 02:57:27 -0500169
Rob Landley7aa651a2012-11-13 17:14:08 -0600170 if (!next) break;
171 path += len;
172 path++;
173 }
174 free(cwd);
landley00f87f12006-10-25 18:38:37 -0400175
Rob Landley7aa651a2012-11-13 17:14:08 -0600176 return rlist;
landley00f87f12006-10-25 18:38:37 -0400177}
landley09ea7ac2006-10-30 01:38:00 -0500178
Rob Landleyf5757162007-02-16 21:08:22 -0500179// atol() with the kilo/mega/giga/tera/peta/exa extensions.
180// (zetta and yotta don't fit in 64 bits.)
Rob Landley5e6dca62012-02-09 06:09:27 -0600181long atolx(char *numstr)
Rob Landleyf5757162007-02-16 21:08:22 -0500182{
Rob Landley7aa651a2012-11-13 17:14:08 -0600183 char *c, *suffixes="bkmgtpe", *end;
184 long val = strtol(numstr, &c, 0);
Rob Landleyf5757162007-02-16 21:08:22 -0500185
Rob Landley7aa651a2012-11-13 17:14:08 -0600186 if (*c) {
187 if (c != numstr && (end = strchr(suffixes, tolower(*c)))) {
188 int shift = end-suffixes;
189 if (shift--) val *= 1024L<<(shift*10);
190 } else {
191 while (isspace(*c)) c++;
192 if (*c) error_exit("not integer: %s", numstr);
193 }
194 }
Rob Landleyad63f4b2011-12-12 15:19:52 -0600195
Rob Landley7aa651a2012-11-13 17:14:08 -0600196 return val;
Rob Landleyf5757162007-02-16 21:08:22 -0500197}
198
Rob Landleyeb7ea222012-04-14 22:30:41 -0500199int numlen(long l)
200{
Rob Landley7aa651a2012-11-13 17:14:08 -0600201 int len = 0;
202 while (l) {
203 l /= 10;
204 len++;
205 }
206 return len;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500207}
208
Rob Landley2037b832012-07-15 16:56:20 -0500209int stridx(char *haystack, char needle)
210{
Rob Landley7aa651a2012-11-13 17:14:08 -0600211 char *off;
Rob Landley2037b832012-07-15 16:56:20 -0500212
Rob Landley7aa651a2012-11-13 17:14:08 -0600213 if (!needle) return -1;
214 off = strchr(haystack, needle);
215 if (!off) return -1;
Rob Landley2037b832012-07-15 16:56:20 -0500216
Rob Landley7aa651a2012-11-13 17:14:08 -0600217 return off-haystack;
Rob Landley2037b832012-07-15 16:56:20 -0500218}
219
Rob Landley055cfcb2007-01-14 20:20:06 -0500220// Return how long the file at fd is, if there's any way to determine it.
221off_t fdlength(int fd)
222{
Rob Landley035f27a2013-08-08 02:46:45 -0500223 struct stat st;
224 off_t base = 0, range = 1, expand = 1, old;
225
226 if (!fstat(fd, &st) && S_ISREG(st.st_mode)) return st.st_size;
Rob Landley055cfcb2007-01-14 20:20:06 -0500227
Rob Landley7aa651a2012-11-13 17:14:08 -0600228 // If the ioctl works for this, return it.
Rob Landley035f27a2013-08-08 02:46:45 -0500229 // TODO: is blocksize still always 512, or do we stat for it?
230 // unsigned int size;
231 // if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L;
Rob Landley055cfcb2007-01-14 20:20:06 -0500232
Rob Landley7aa651a2012-11-13 17:14:08 -0600233 // If not, do a binary search for the last location we can read. (Some
234 // block devices don't do BLKGETSIZE right.) This should probably have
235 // a CONFIG option...
Rob Landley055cfcb2007-01-14 20:20:06 -0500236
Rob Landley035f27a2013-08-08 02:46:45 -0500237 // If not, do a binary search for the last location we can read.
238
Rob Landley7aa651a2012-11-13 17:14:08 -0600239 old = lseek(fd, 0, SEEK_CUR);
240 do {
241 char temp;
Rob Landley035f27a2013-08-08 02:46:45 -0500242 off_t pos = base + range / 2;
Rob Landley055cfcb2007-01-14 20:20:06 -0500243
Rob Landley7aa651a2012-11-13 17:14:08 -0600244 if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) {
Rob Landley035f27a2013-08-08 02:46:45 -0500245 off_t delta = (pos + 1) - base;
Rob Landley055cfcb2007-01-14 20:20:06 -0500246
Rob Landley035f27a2013-08-08 02:46:45 -0500247 base += delta;
248 if (expand) range = (expand <<= 1) - base;
249 else range -= delta;
Rob Landley7aa651a2012-11-13 17:14:08 -0600250 } else {
Rob Landley035f27a2013-08-08 02:46:45 -0500251 expand = 0;
252 range = pos - base;
Rob Landley7aa651a2012-11-13 17:14:08 -0600253 }
Rob Landley035f27a2013-08-08 02:46:45 -0500254 } while (range > 0);
Rob Landley055cfcb2007-01-14 20:20:06 -0500255
Rob Landley7aa651a2012-11-13 17:14:08 -0600256 lseek(fd, old, SEEK_SET);
Rob Landleyf15387d2008-07-18 05:43:44 -0500257
Rob Landley035f27a2013-08-08 02:46:45 -0500258 return base;
Rob Landley055cfcb2007-01-14 20:20:06 -0500259}
260
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500261// Read contents of file as a single nul-terminated string.
262// malloc new one if buf=len=0
263char *readfile(char *name, char *buf, off_t len)
Rob Landley055cfcb2007-01-14 20:20:06 -0500264{
Rob Landley7aa651a2012-11-13 17:14:08 -0600265 int fd;
Rob Landley055cfcb2007-01-14 20:20:06 -0500266
Rob Landley7aa651a2012-11-13 17:14:08 -0600267 fd = open(name, O_RDONLY);
268 if (fd == -1) return 0;
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500269
270 if (len<1) {
271 len = fdlength(fd);
272 // proc files don't report a length, so try 1 page minimum.
273 if (len<4096) len = 4096;
274 }
275 if (!buf) buf = xmalloc(len+1);
276
277 len = readall(fd, buf, len-1);
278 close(fd);
279 if (len<0) {
280 free(buf);
281 buf = 0;
282 } else buf[len] = 0;
Rob Landley055cfcb2007-01-14 20:20:06 -0500283
Rob Landley7aa651a2012-11-13 17:14:08 -0600284 return buf;
Rob Landley055cfcb2007-01-14 20:20:06 -0500285}
286
Rob Landleyf0153442013-04-26 02:41:05 -0500287// Sleep for this many thousandths of a second
288void msleep(long miliseconds)
289{
290 struct timespec ts;
291
292 ts.tv_sec = miliseconds/1000;
293 ts.tv_nsec = (miliseconds%1000)*1000000;
294 nanosleep(&ts, &ts);
295}
296
Rob Landley6b283412013-06-01 20:41:35 -0500297int64_t peek(void *ptr, int size)
298{
299 if (size & 8) {
300 int64_t *p = (int64_t *)ptr;
301 return *p;
302 } else if (size & 4) {
303 int *p = (int *)ptr;
304 return *p;
305 } else if (size & 2) {
306 short *p = (short *)ptr;
307 return *p;
308 } else {
309 char *p = (char *)ptr;
310 return *p;
311 }
312}
313
314void poke(void *ptr, uint64_t val, int size)
315{
316 if (size & 8) {
317 uint64_t *p = (uint64_t *)ptr;
318 *p = val;
319 } else if (size & 4) {
320 int *p = (int *)ptr;
321 *p = val;
322 } else if (size & 2) {
323 short *p = (short *)ptr;
324 *p = val;
325 } else {
326 char *p = (char *)ptr;
327 *p = val;
328 }
329}
330
Rob Landley2bfaaf22008-07-03 19:19:00 -0500331// Iterate through an array of files, opening each one and calling a function
332// on that filehandle and name. The special filename "-" means stdin if
333// flags is O_RDONLY, stdout otherwise. An empty argument list calls
334// function() on just stdin/stdout.
335//
336// Note: read only filehandles are automatically closed when function()
337// returns, but writeable filehandles must be close by function()
Rob Landleyad63f4b2011-12-12 15:19:52 -0600338void loopfiles_rw(char **argv, int flags, int permissions, int failok,
Rob Landley7aa651a2012-11-13 17:14:08 -0600339 void (*function)(int fd, char *name))
Rob Landley7634b552007-11-29 17:49:50 -0600340{
Rob Landley7aa651a2012-11-13 17:14:08 -0600341 int fd;
Rob Landley7634b552007-11-29 17:49:50 -0600342
Rob Landley7aa651a2012-11-13 17:14:08 -0600343 // If no arguments, read from stdin.
344 if (!*argv) function(flags ? 1 : 0, "-");
345 else do {
346 // Filename "-" means read from stdin.
347 // Inability to open a file prints a warning, but doesn't exit.
Rob Landley7634b552007-11-29 17:49:50 -0600348
Rob Landley7aa651a2012-11-13 17:14:08 -0600349 if (!strcmp(*argv,"-")) fd=0;
350 else if (0>(fd = open(*argv, flags, permissions)) && !failok) {
351 perror_msg("%s", *argv);
352 toys.exitval = 1;
353 continue;
354 }
355 function(fd, *argv);
356 if (flags == O_RDONLY) close(fd);
357 } while (*++argv);
Rob Landley7634b552007-11-29 17:49:50 -0600358}
Rob Landleybc078652007-12-15 21:47:25 -0600359
Rob Landleyad63f4b2011-12-12 15:19:52 -0600360// Call loopfiles_rw with O_RDONLY and !failok (common case).
Rob Landley2bfaaf22008-07-03 19:19:00 -0500361void loopfiles(char **argv, void (*function)(int fd, char *name))
362{
Rob Landley7aa651a2012-11-13 17:14:08 -0600363 loopfiles_rw(argv, O_RDONLY, 0, 0, function);
Rob Landley2bfaaf22008-07-03 19:19:00 -0500364}
365
Rob Landleybc078652007-12-15 21:47:25 -0600366// Slow, but small.
367
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500368char *get_rawline(int fd, long *plen, char end)
Rob Landleybc078652007-12-15 21:47:25 -0600369{
Rob Landley7aa651a2012-11-13 17:14:08 -0600370 char c, *buf = NULL;
371 long len = 0;
Rob Landleybc078652007-12-15 21:47:25 -0600372
Rob Landley7aa651a2012-11-13 17:14:08 -0600373 for (;;) {
374 if (1>read(fd, &c, 1)) break;
375 if (!(len & 63)) buf=xrealloc(buf, len+65);
376 if ((buf[len++]=c) == end) break;
377 }
378 if (buf) buf[len]=0;
379 if (plen) *plen = len;
Rob Landleybc078652007-12-15 21:47:25 -0600380
Rob Landley7aa651a2012-11-13 17:14:08 -0600381 return buf;
Rob Landleybc078652007-12-15 21:47:25 -0600382}
383
384char *get_line(int fd)
385{
Rob Landley7aa651a2012-11-13 17:14:08 -0600386 long len;
387 char *buf = get_rawline(fd, &len, '\n');
Rob Landleybc078652007-12-15 21:47:25 -0600388
Rob Landley7aa651a2012-11-13 17:14:08 -0600389 if (buf && buf[--len]=='\n') buf[len]=0;
Rob Landleybc078652007-12-15 21:47:25 -0600390
Rob Landley7aa651a2012-11-13 17:14:08 -0600391 return buf;
Rob Landleybc078652007-12-15 21:47:25 -0600392}
393
Rob Landley67a069d2012-06-03 00:32:12 -0500394int wfchmodat(int fd, char *name, mode_t mode)
395{
Rob Landley7aa651a2012-11-13 17:14:08 -0600396 int rc = fchmodat(fd, name, mode, 0);
Rob Landley67a069d2012-06-03 00:32:12 -0500397
Rob Landley7aa651a2012-11-13 17:14:08 -0600398 if (rc) {
399 perror_msg("chmod '%s' to %04o", name, mode);
400 toys.exitval=1;
401 }
402 return rc;
Rob Landley67a069d2012-06-03 00:32:12 -0500403}
404
Rob Landleyc52db602012-07-30 01:01:33 -0500405static char *tempfile2zap;
406static void tempfile_handler(int i)
407{
Rob Landley7aa651a2012-11-13 17:14:08 -0600408 if (1 < (long)tempfile2zap) unlink(tempfile2zap);
409 _exit(1);
Rob Landleyc52db602012-07-30 01:01:33 -0500410}
411
Rob Landley42ecbab2007-12-18 02:02:21 -0600412// Open a temporary file to copy an existing file into.
413int copy_tempfile(int fdin, char *name, char **tempname)
414{
Rob Landley7aa651a2012-11-13 17:14:08 -0600415 struct stat statbuf;
416 int fd;
Rob Landley42ecbab2007-12-18 02:02:21 -0600417
Rob Landley7aa651a2012-11-13 17:14:08 -0600418 *tempname = xstrndup(name, strlen(name)+6);
419 strcat(*tempname,"XXXXXX");
420 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
421 if (!tempfile2zap) sigatexit(tempfile_handler);
422 tempfile2zap = *tempname;
Rob Landley42ecbab2007-12-18 02:02:21 -0600423
Rob Landley7aa651a2012-11-13 17:14:08 -0600424 // Set permissions of output file
Rob Landley42ecbab2007-12-18 02:02:21 -0600425
Rob Landley7aa651a2012-11-13 17:14:08 -0600426 fstat(fdin, &statbuf);
427 fchmod(fd, statbuf.st_mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600428
Rob Landley7aa651a2012-11-13 17:14:08 -0600429 return fd;
Rob Landley42ecbab2007-12-18 02:02:21 -0600430}
431
432// Abort the copy and delete the temporary file.
433void delete_tempfile(int fdin, int fdout, char **tempname)
434{
Rob Landley7aa651a2012-11-13 17:14:08 -0600435 close(fdin);
436 close(fdout);
437 unlink(*tempname);
438 tempfile2zap = (char *)1;
439 free(*tempname);
440 *tempname = NULL;
Rob Landley42ecbab2007-12-18 02:02:21 -0600441}
442
443// Copy the rest of the data and replace the original with the copy.
444void replace_tempfile(int fdin, int fdout, char **tempname)
445{
Rob Landley7aa651a2012-11-13 17:14:08 -0600446 char *temp = xstrdup(*tempname);
Rob Landley42ecbab2007-12-18 02:02:21 -0600447
Rob Landley7aa651a2012-11-13 17:14:08 -0600448 temp[strlen(temp)-6]=0;
449 if (fdin != -1) {
450 xsendfile(fdin, fdout);
451 xclose(fdin);
452 }
453 xclose(fdout);
454 rename(*tempname, temp);
455 tempfile2zap = (char *)1;
456 free(*tempname);
457 free(temp);
458 *tempname = NULL;
Rob Landleybc078652007-12-15 21:47:25 -0600459}
Rob Landley7e849c52009-01-03 18:15:18 -0600460
461// Create a 256 entry CRC32 lookup table.
462
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600463void crc_init(unsigned int *crc_table, int little_endian)
Rob Landley7e849c52009-01-03 18:15:18 -0600464{
Rob Landley7aa651a2012-11-13 17:14:08 -0600465 unsigned int i;
Rob Landley7e849c52009-01-03 18:15:18 -0600466
Rob Landley7aa651a2012-11-13 17:14:08 -0600467 // Init the CRC32 table (big endian)
468 for (i=0; i<256; i++) {
469 unsigned int j, c = little_endian ? i : i<<24;
470 for (j=8; j; j--)
471 if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1;
472 else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
473 crc_table[i] = c;
474 }
Rob Landley7e849c52009-01-03 18:15:18 -0600475}
Rob Landley26e7b5e2012-02-02 07:27:35 -0600476
477// Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
478// set *x=0 and *y=0 before calling to detect failure to set either, or
479// x=80 y=25 to provide defaults
480
481void terminal_size(unsigned *x, unsigned *y)
482{
Rob Landley7aa651a2012-11-13 17:14:08 -0600483 struct winsize ws;
484 int i;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600485
Rob Landley7aa651a2012-11-13 17:14:08 -0600486 //memset(&ws, 0, sizeof(ws));
487 for (i=0; i<3; i++) {
488 if (ioctl(i, TIOCGWINSZ, &ws)) continue;
489 if (x) *x = ws.ws_col;
490 if (y) *y = ws.ws_row;
491 }
492 if (x) {
493 char *s = getenv("COLUMNS");
Rob Landley26e7b5e2012-02-02 07:27:35 -0600494
Rob Landley7aa651a2012-11-13 17:14:08 -0600495 i = s ? atoi(s) : 0;
496 if (i>0) *x = i;
497 }
498 if (y) {
499 char *s = getenv("ROWS");
Rob Landley26e7b5e2012-02-02 07:27:35 -0600500
Rob Landley7aa651a2012-11-13 17:14:08 -0600501 i = s ? atoi(s) : 0;
502 if (i>0) *y = i;
503 }
Rob Landley26e7b5e2012-02-02 07:27:35 -0600504}
505
Rob Landleyf793d532012-02-27 21:56:49 -0600506int yesno(char *prompt, int def)
Rob Landley26e7b5e2012-02-02 07:27:35 -0600507{
Rob Landley7aa651a2012-11-13 17:14:08 -0600508 char buf;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600509
Rob Landleydb8eb322012-12-08 02:25:32 -0600510 fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
511 fflush(stderr);
512 while (fread(&buf, 1, 1, stdin)) {
513 int new;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600514
Rob Landley22791082013-01-31 04:13:07 -0600515 // The letter changes the value, the newline (or space) returns it.
Rob Landleydb8eb322012-12-08 02:25:32 -0600516 if (isspace(buf)) break;
517 if (-1 != (new = stridx("ny", tolower(buf)))) def = new;
Rob Landley7aa651a2012-11-13 17:14:08 -0600518 }
Rob Landleyee00a7f2012-03-19 19:19:21 -0500519
Rob Landley7aa651a2012-11-13 17:14:08 -0600520 return def;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600521}
Rob Landleyff9ee8f2012-02-18 15:12:41 -0600522
Rob Landley2dd50ad2012-02-26 13:48:00 -0600523struct signame {
Rob Landley7aa651a2012-11-13 17:14:08 -0600524 int num;
525 char *name;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600526};
527
528// Signals required by POSIX 2008:
529// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
530
531#define SIGNIFY(x) {SIG##x, #x}
532
533static struct signame signames[] = {
Rob Landley7aa651a2012-11-13 17:14:08 -0600534 SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS),
535 SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
536 SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM),
537 SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP),
538 SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ),
Rob Landleyc52db602012-07-30 01:01:33 -0500539
Rob Landley7aa651a2012-11-13 17:14:08 -0600540 // Start of non-terminal signals
Rob Landleyc52db602012-07-30 01:01:33 -0500541
Rob Landley7aa651a2012-11-13 17:14:08 -0600542 SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP),
543 SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG)
Rob Landley2dd50ad2012-02-26 13:48:00 -0600544};
545
546// not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR)
547// obsolete: SIGNIFY(PROF) SIGNIFY(POLL)
548
Rob Landleyc52db602012-07-30 01:01:33 -0500549// Install the same handler on every signal that defaults to killing the process
550void sigatexit(void *handler)
551{
Rob Landley7aa651a2012-11-13 17:14:08 -0600552 int i;
553 for (i=0; signames[i].num != SIGCHLD; i++) signal(signames[i].num, handler);
Rob Landleyc52db602012-07-30 01:01:33 -0500554}
Rob Landley2dd50ad2012-02-26 13:48:00 -0600555// Convert name to signal number. If name == NULL print names.
556int sig_to_num(char *pidstr)
557{
Rob Landley7aa651a2012-11-13 17:14:08 -0600558 int i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600559
Rob Landley7aa651a2012-11-13 17:14:08 -0600560 if (pidstr) {
561 char *s;
562 i = strtol(pidstr, &s, 10);
563 if (!*s) return i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600564
Rob Landley7aa651a2012-11-13 17:14:08 -0600565 if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3;
566 }
567 for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++)
568 if (!pidstr) xputs(signames[i].name);
569 else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600570
Rob Landley7aa651a2012-11-13 17:14:08 -0600571 return -1;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600572}
573
574char *num_to_sig(int sig)
575{
Rob Landley7aa651a2012-11-13 17:14:08 -0600576 int i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600577
Rob Landley7aa651a2012-11-13 17:14:08 -0600578 for (i=0; i<sizeof(signames)/sizeof(struct signame); i++)
579 if (signames[i].num == sig) return signames[i].name;
580 return NULL;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600581}
Daniel Walter05744b32012-03-19 19:57:56 -0500582
Rob Landleyc52db602012-07-30 01:01:33 -0500583// premute mode bits based on posix mode strings.
Rob Landleyb6f601e2012-05-16 21:11:43 -0500584mode_t string_to_mode(char *modestr, mode_t mode)
Daniel Walter05744b32012-03-19 19:57:56 -0500585{
Rob Landley7aa651a2012-11-13 17:14:08 -0600586 char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu";
587 char *s, *str = modestr;
Rob Landleycf6bcb22012-03-19 20:56:18 -0500588
Rob Landley7aa651a2012-11-13 17:14:08 -0600589 // Handle octal mode
590 if (isdigit(*str)) {
591 mode = strtol(str, &s, 8);
592 if (*s || (mode & ~(07777))) goto barf;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500593
Rob Landley7aa651a2012-11-13 17:14:08 -0600594 return mode;
595 }
Rob Landleycf6bcb22012-03-19 20:56:18 -0500596
Rob Landley7aa651a2012-11-13 17:14:08 -0600597 // Gaze into the bin of permission...
598 for (;;) {
599 int i, j, dowho, dohow, dowhat, amask;
Rob Landleycf6bcb22012-03-19 20:56:18 -0500600
Rob Landley7aa651a2012-11-13 17:14:08 -0600601 dowho = dohow = dowhat = amask = 0;
Daniel Walter05744b32012-03-19 19:57:56 -0500602
Rob Landley7aa651a2012-11-13 17:14:08 -0600603 // Find the who, how, and what stanzas, in that order
604 while (*str && (s = strchr(whos, *str))) {
605 dowho |= 1<<(s-whos);
606 str++;
607 }
608 // If who isn't specified, like "a" but honoring umask.
609 if (!dowho) {
610 dowho = 8;
611 umask(amask=umask(0));
612 }
613 if (!*str || !(s = strchr(hows, *str))) goto barf;
614 dohow = *(str++);
Rob Landley67a069d2012-06-03 00:32:12 -0500615
Rob Landley7aa651a2012-11-13 17:14:08 -0600616 if (!dohow) goto barf;
617 while (*str && (s = strchr(whats, *str))) {
618 dowhat |= 1<<(s-whats);
619 str++;
620 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500621
Rob Landley7aa651a2012-11-13 17:14:08 -0600622 // Convert X to x for directory or if already executable somewhere
623 if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500624
Rob Landley7aa651a2012-11-13 17:14:08 -0600625 // Copy mode from another category?
626 if (!dowhat && *str && (s = strchr(whys, *str))) {
627 dowhat = (mode>>(3*(s-whys)))&7;
628 str++;
629 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500630
Rob Landley7aa651a2012-11-13 17:14:08 -0600631 // Are we ready to do a thing yet?
632 if (*str && *(str++) != ',') goto barf;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500633
Rob Landley7aa651a2012-11-13 17:14:08 -0600634 // Ok, apply the bits to the mode.
635 for (i=0; i<4; i++) {
636 for (j=0; j<3; j++) {
637 mode_t bit = 0;
638 int where = 1<<((3*i)+j);
Rob Landley31f49e72012-07-21 22:45:05 -0500639
Rob Landley7aa651a2012-11-13 17:14:08 -0600640 if (amask & where) continue;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500641
Rob Landley7aa651a2012-11-13 17:14:08 -0600642 // Figure out new value at this location
643 if (i == 3) {
644 // suid/sticky bit.
645 if (j) {
646 if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++;
647 } else if (dowhat & 16) bit++;
648 } else {
649 if (!(dowho&(8|(1<<i)))) continue;
650 if (dowhat&(1<<j)) bit++;
651 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500652
Rob Landley7aa651a2012-11-13 17:14:08 -0600653 // When selection active, modify bit
Rob Landleyb6f601e2012-05-16 21:11:43 -0500654
Rob Landley7aa651a2012-11-13 17:14:08 -0600655 if (dohow == '=' || (bit && dohow == '-')) mode &= ~where;
656 if (bit && dohow != '-') mode |= where;
657 }
658 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500659
Rob Landley7aa651a2012-11-13 17:14:08 -0600660 if (!*str) break;
661 }
662 return mode;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500663barf:
Rob Landley7aa651a2012-11-13 17:14:08 -0600664 error_exit("bad mode '%s'", modestr);
Daniel Walter05744b32012-03-19 19:57:56 -0500665}
Ashwini Kumar1a0eedf2012-08-26 21:17:00 -0500666
Rob Landley5a26a862013-06-02 00:24:24 -0500667// Format access mode into a drwxrwxrwx string
668void mode_to_string(mode_t mode, char *buf)
Felix Janda5a221e62013-04-22 22:29:43 +0200669{
670 char c, d;
671 int i, bit;
672
Rob Landley5a26a862013-06-02 00:24:24 -0500673 buf[10]=0;
Felix Janda5a221e62013-04-22 22:29:43 +0200674 for (i=0; i<9; i++) {
675 bit = mode & (1<<i);
676 c = i%3;
677 if (!c && (mode & (1<<((d=i/3)+9)))) {
678 c = "tss"[d];
679 if (!bit) c &= ~0x20;
680 } else c = bit ? "xwr"[c] : '-';
Rob Landley5a26a862013-06-02 00:24:24 -0500681 buf[9-i] = c;
Felix Janda5a221e62013-04-22 22:29:43 +0200682 }
683
684 if (S_ISDIR(mode)) c = 'd';
685 else if (S_ISBLK(mode)) c = 'b';
686 else if (S_ISCHR(mode)) c = 'c';
687 else if (S_ISLNK(mode)) c = 'l';
688 else if (S_ISFIFO(mode)) c = 'p';
689 else if (S_ISSOCK(mode)) c = 's';
690 else c = '-';
Rob Landley5a26a862013-06-02 00:24:24 -0500691 *buf = c;
Felix Janda5a221e62013-04-22 22:29:43 +0200692}