blob: fe18b3e182e43be7a0c45257c0629202c27c905f [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 Landleybc382be2013-09-16 23:41:51 -050092// skip this many bytes of input. Return 0 for success, >0 means this much
93// left after input skipped.
Rob Landley2037b832012-07-15 16:56:20 -050094off_t lskip(int fd, off_t offset)
95{
Rob Landleybc382be2013-09-16 23:41:51 -050096 off_t cur = lseek(fd, 0, SEEK_CUR);
Rob Landley2037b832012-07-15 16:56:20 -050097
Rob Landleybc382be2013-09-16 23:41:51 -050098 if (cur != -1) {
99 off_t end = lseek(fd, 0, SEEK_END) - cur;
Rob Landley2037b832012-07-15 16:56:20 -0500100
Rob Landleybc382be2013-09-16 23:41:51 -0500101 if (end > 0 && end < offset) return offset - end;
102 end = offset+cur;
103 if (end == lseek(fd, end, SEEK_SET)) return 0;
104 perror_exit("lseek");
Rob Landley7aa651a2012-11-13 17:14:08 -0600105 }
Rob Landleybc382be2013-09-16 23:41:51 -0500106
107 while (offset>0) {
108 int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or;
109
110 or = readall(fd, libbuf, try);
111 if (or < 0) perror_exit("lskip to %lld", (long long)offset);
112 else offset -= try;
113 if (or < try) break;
114 }
115
116 return offset;
Rob Landley2037b832012-07-15 16:56:20 -0500117}
118
Rob Landleyfe91e682012-11-22 21:18:09 -0600119// Split a path into linked list of components, tracking head and tail of list.
120// Filters out // entries with no contents.
121struct string_list **splitpath(char *path, struct string_list **list)
landley00f87f12006-10-25 18:38:37 -0400122{
Rob Landleyfe91e682012-11-22 21:18:09 -0600123 char *new = path;
landley00f87f12006-10-25 18:38:37 -0400124
Rob Landleyfe91e682012-11-22 21:18:09 -0600125 *list = 0;
126 do {
127 int len;
landley00f87f12006-10-25 18:38:37 -0400128
Rob Landleyfe91e682012-11-22 21:18:09 -0600129 if (*path && *path != '/') continue;
130 len = path-new;
131 if (len > 0) {
132 *list = xmalloc(sizeof(struct string_list) + len + 1);
133 (*list)->next = 0;
134 strncpy((*list)->str, new, len);
135 (*list)->str[len] = 0;
136 list = &(*list)->next;
137 }
138 new = path+1;
139 } while (*path++);
140
141 return list;
142}
143
Rob Landley0a04b3e2006-11-03 00:05:52 -0500144// Find all file in a colon-separated path with access type "type" (generally
145// X_OK or R_OK). Returns a list of absolute paths to each file found, in
146// order.
147
148struct string_list *find_in_path(char *path, char *filename)
Rob Landleyfa98d012006-11-02 02:57:27 -0500149{
Rob Landley7aa651a2012-11-13 17:14:08 -0600150 struct string_list *rlist = NULL, **prlist=&rlist;
151 char *cwd = xgetcwd();
Rob Landleyfa98d012006-11-02 02:57:27 -0500152
Rob Landley7aa651a2012-11-13 17:14:08 -0600153 for (;;) {
154 char *next = path ? strchr(path, ':') : NULL;
155 int len = next ? next-path : strlen(path);
156 struct string_list *rnext;
157 struct stat st;
Rob Landleyfa98d012006-11-02 02:57:27 -0500158
Rob Landley7aa651a2012-11-13 17:14:08 -0600159 rnext = xmalloc(sizeof(void *) + strlen(filename)
160 + (len ? len : strlen(cwd)) + 2);
161 if (!len) sprintf(rnext->str, "%s/%s", cwd, filename);
162 else {
163 char *res = rnext->str;
164 strncpy(res, path, len);
165 res += len;
166 *(res++) = '/';
167 strcpy(res, filename);
168 }
Rob Landleyfa98d012006-11-02 02:57:27 -0500169
Rob Landley7aa651a2012-11-13 17:14:08 -0600170 // Confirm it's not a directory.
171 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) {
172 *prlist = rnext;
173 rnext->next = NULL;
174 prlist = &(rnext->next);
175 } else free(rnext);
Rob Landleyfa98d012006-11-02 02:57:27 -0500176
Rob Landley7aa651a2012-11-13 17:14:08 -0600177 if (!next) break;
178 path += len;
179 path++;
180 }
181 free(cwd);
landley00f87f12006-10-25 18:38:37 -0400182
Rob Landley7aa651a2012-11-13 17:14:08 -0600183 return rlist;
landley00f87f12006-10-25 18:38:37 -0400184}
landley09ea7ac2006-10-30 01:38:00 -0500185
Rob Landleyf5757162007-02-16 21:08:22 -0500186// atol() with the kilo/mega/giga/tera/peta/exa extensions.
187// (zetta and yotta don't fit in 64 bits.)
Rob Landley5e6dca62012-02-09 06:09:27 -0600188long atolx(char *numstr)
Rob Landleyf5757162007-02-16 21:08:22 -0500189{
Rob Landley7aa651a2012-11-13 17:14:08 -0600190 char *c, *suffixes="bkmgtpe", *end;
191 long val = strtol(numstr, &c, 0);
Rob Landleyf5757162007-02-16 21:08:22 -0500192
Rob Landley7aa651a2012-11-13 17:14:08 -0600193 if (*c) {
194 if (c != numstr && (end = strchr(suffixes, tolower(*c)))) {
195 int shift = end-suffixes;
196 if (shift--) val *= 1024L<<(shift*10);
197 } else {
198 while (isspace(*c)) c++;
199 if (*c) error_exit("not integer: %s", numstr);
200 }
201 }
Rob Landleyad63f4b2011-12-12 15:19:52 -0600202
Rob Landley7aa651a2012-11-13 17:14:08 -0600203 return val;
Rob Landleyf5757162007-02-16 21:08:22 -0500204}
205
Rob Landleyeb7ea222012-04-14 22:30:41 -0500206int numlen(long l)
207{
Rob Landley7aa651a2012-11-13 17:14:08 -0600208 int len = 0;
209 while (l) {
210 l /= 10;
211 len++;
212 }
213 return len;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500214}
215
Rob Landley2037b832012-07-15 16:56:20 -0500216int stridx(char *haystack, char needle)
217{
Rob Landley7aa651a2012-11-13 17:14:08 -0600218 char *off;
Rob Landley2037b832012-07-15 16:56:20 -0500219
Rob Landley7aa651a2012-11-13 17:14:08 -0600220 if (!needle) return -1;
221 off = strchr(haystack, needle);
222 if (!off) return -1;
Rob Landley2037b832012-07-15 16:56:20 -0500223
Rob Landley7aa651a2012-11-13 17:14:08 -0600224 return off-haystack;
Rob Landley2037b832012-07-15 16:56:20 -0500225}
226
Rob Landley055cfcb2007-01-14 20:20:06 -0500227// Return how long the file at fd is, if there's any way to determine it.
228off_t fdlength(int fd)
229{
Rob Landley035f27a2013-08-08 02:46:45 -0500230 struct stat st;
231 off_t base = 0, range = 1, expand = 1, old;
232
233 if (!fstat(fd, &st) && S_ISREG(st.st_mode)) return st.st_size;
Rob Landley055cfcb2007-01-14 20:20:06 -0500234
Rob Landley7aa651a2012-11-13 17:14:08 -0600235 // If the ioctl works for this, return it.
Rob Landley035f27a2013-08-08 02:46:45 -0500236 // TODO: is blocksize still always 512, or do we stat for it?
237 // unsigned int size;
238 // if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L;
Rob Landley055cfcb2007-01-14 20:20:06 -0500239
Rob Landley7aa651a2012-11-13 17:14:08 -0600240 // If not, do a binary search for the last location we can read. (Some
241 // block devices don't do BLKGETSIZE right.) This should probably have
242 // a CONFIG option...
Rob Landley055cfcb2007-01-14 20:20:06 -0500243
Rob Landley035f27a2013-08-08 02:46:45 -0500244 // If not, do a binary search for the last location we can read.
245
Rob Landley7aa651a2012-11-13 17:14:08 -0600246 old = lseek(fd, 0, SEEK_CUR);
247 do {
248 char temp;
Rob Landley035f27a2013-08-08 02:46:45 -0500249 off_t pos = base + range / 2;
Rob Landley055cfcb2007-01-14 20:20:06 -0500250
Rob Landley7aa651a2012-11-13 17:14:08 -0600251 if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) {
Rob Landley035f27a2013-08-08 02:46:45 -0500252 off_t delta = (pos + 1) - base;
Rob Landley055cfcb2007-01-14 20:20:06 -0500253
Rob Landley035f27a2013-08-08 02:46:45 -0500254 base += delta;
255 if (expand) range = (expand <<= 1) - base;
256 else range -= delta;
Rob Landley7aa651a2012-11-13 17:14:08 -0600257 } else {
Rob Landley035f27a2013-08-08 02:46:45 -0500258 expand = 0;
259 range = pos - base;
Rob Landley7aa651a2012-11-13 17:14:08 -0600260 }
Rob Landley035f27a2013-08-08 02:46:45 -0500261 } while (range > 0);
Rob Landley055cfcb2007-01-14 20:20:06 -0500262
Rob Landley7aa651a2012-11-13 17:14:08 -0600263 lseek(fd, old, SEEK_SET);
Rob Landleyf15387d2008-07-18 05:43:44 -0500264
Rob Landley035f27a2013-08-08 02:46:45 -0500265 return base;
Rob Landley055cfcb2007-01-14 20:20:06 -0500266}
267
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500268// Read contents of file as a single nul-terminated string.
269// malloc new one if buf=len=0
270char *readfile(char *name, char *buf, off_t len)
Rob Landley055cfcb2007-01-14 20:20:06 -0500271{
Rob Landley7aa651a2012-11-13 17:14:08 -0600272 int fd;
Rob Landley055cfcb2007-01-14 20:20:06 -0500273
Rob Landley7aa651a2012-11-13 17:14:08 -0600274 fd = open(name, O_RDONLY);
275 if (fd == -1) return 0;
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500276
277 if (len<1) {
278 len = fdlength(fd);
279 // proc files don't report a length, so try 1 page minimum.
280 if (len<4096) len = 4096;
281 }
282 if (!buf) buf = xmalloc(len+1);
283
284 len = readall(fd, buf, len-1);
285 close(fd);
286 if (len<0) {
287 free(buf);
288 buf = 0;
289 } else buf[len] = 0;
Rob Landley055cfcb2007-01-14 20:20:06 -0500290
Rob Landley7aa651a2012-11-13 17:14:08 -0600291 return buf;
Rob Landley055cfcb2007-01-14 20:20:06 -0500292}
293
Rob Landleyf0153442013-04-26 02:41:05 -0500294// Sleep for this many thousandths of a second
295void msleep(long miliseconds)
296{
297 struct timespec ts;
298
299 ts.tv_sec = miliseconds/1000;
300 ts.tv_nsec = (miliseconds%1000)*1000000;
301 nanosleep(&ts, &ts);
302}
303
Rob Landley6b283412013-06-01 20:41:35 -0500304int64_t peek(void *ptr, int size)
305{
306 if (size & 8) {
307 int64_t *p = (int64_t *)ptr;
308 return *p;
309 } else if (size & 4) {
310 int *p = (int *)ptr;
311 return *p;
312 } else if (size & 2) {
313 short *p = (short *)ptr;
314 return *p;
315 } else {
316 char *p = (char *)ptr;
317 return *p;
318 }
319}
320
321void poke(void *ptr, uint64_t val, int size)
322{
323 if (size & 8) {
324 uint64_t *p = (uint64_t *)ptr;
325 *p = val;
326 } else if (size & 4) {
327 int *p = (int *)ptr;
328 *p = val;
329 } else if (size & 2) {
330 short *p = (short *)ptr;
331 *p = val;
332 } else {
333 char *p = (char *)ptr;
334 *p = val;
335 }
336}
337
Rob Landley2bfaaf22008-07-03 19:19:00 -0500338// Iterate through an array of files, opening each one and calling a function
339// on that filehandle and name. The special filename "-" means stdin if
340// flags is O_RDONLY, stdout otherwise. An empty argument list calls
341// function() on just stdin/stdout.
342//
343// Note: read only filehandles are automatically closed when function()
344// returns, but writeable filehandles must be close by function()
Rob Landleyad63f4b2011-12-12 15:19:52 -0600345void loopfiles_rw(char **argv, int flags, int permissions, int failok,
Rob Landley7aa651a2012-11-13 17:14:08 -0600346 void (*function)(int fd, char *name))
Rob Landley7634b552007-11-29 17:49:50 -0600347{
Rob Landley7aa651a2012-11-13 17:14:08 -0600348 int fd;
Rob Landley7634b552007-11-29 17:49:50 -0600349
Rob Landley7aa651a2012-11-13 17:14:08 -0600350 // If no arguments, read from stdin.
351 if (!*argv) function(flags ? 1 : 0, "-");
352 else do {
353 // Filename "-" means read from stdin.
354 // Inability to open a file prints a warning, but doesn't exit.
Rob Landley7634b552007-11-29 17:49:50 -0600355
Rob Landley7aa651a2012-11-13 17:14:08 -0600356 if (!strcmp(*argv,"-")) fd=0;
357 else if (0>(fd = open(*argv, flags, permissions)) && !failok) {
358 perror_msg("%s", *argv);
359 toys.exitval = 1;
360 continue;
361 }
362 function(fd, *argv);
363 if (flags == O_RDONLY) close(fd);
364 } while (*++argv);
Rob Landley7634b552007-11-29 17:49:50 -0600365}
Rob Landleybc078652007-12-15 21:47:25 -0600366
Rob Landleyad63f4b2011-12-12 15:19:52 -0600367// Call loopfiles_rw with O_RDONLY and !failok (common case).
Rob Landley2bfaaf22008-07-03 19:19:00 -0500368void loopfiles(char **argv, void (*function)(int fd, char *name))
369{
Rob Landley7aa651a2012-11-13 17:14:08 -0600370 loopfiles_rw(argv, O_RDONLY, 0, 0, function);
Rob Landley2bfaaf22008-07-03 19:19:00 -0500371}
372
Rob Landleybc078652007-12-15 21:47:25 -0600373// Slow, but small.
374
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500375char *get_rawline(int fd, long *plen, char end)
Rob Landleybc078652007-12-15 21:47:25 -0600376{
Rob Landley7aa651a2012-11-13 17:14:08 -0600377 char c, *buf = NULL;
378 long len = 0;
Rob Landleybc078652007-12-15 21:47:25 -0600379
Rob Landley7aa651a2012-11-13 17:14:08 -0600380 for (;;) {
381 if (1>read(fd, &c, 1)) break;
382 if (!(len & 63)) buf=xrealloc(buf, len+65);
383 if ((buf[len++]=c) == end) break;
384 }
385 if (buf) buf[len]=0;
386 if (plen) *plen = len;
Rob Landleybc078652007-12-15 21:47:25 -0600387
Rob Landley7aa651a2012-11-13 17:14:08 -0600388 return buf;
Rob Landleybc078652007-12-15 21:47:25 -0600389}
390
391char *get_line(int fd)
392{
Rob Landley7aa651a2012-11-13 17:14:08 -0600393 long len;
394 char *buf = get_rawline(fd, &len, '\n');
Rob Landleybc078652007-12-15 21:47:25 -0600395
Rob Landley7aa651a2012-11-13 17:14:08 -0600396 if (buf && buf[--len]=='\n') buf[len]=0;
Rob Landleybc078652007-12-15 21:47:25 -0600397
Rob Landley7aa651a2012-11-13 17:14:08 -0600398 return buf;
Rob Landleybc078652007-12-15 21:47:25 -0600399}
400
Rob Landley67a069d2012-06-03 00:32:12 -0500401int wfchmodat(int fd, char *name, mode_t mode)
402{
Rob Landley7aa651a2012-11-13 17:14:08 -0600403 int rc = fchmodat(fd, name, mode, 0);
Rob Landley67a069d2012-06-03 00:32:12 -0500404
Rob Landley7aa651a2012-11-13 17:14:08 -0600405 if (rc) {
406 perror_msg("chmod '%s' to %04o", name, mode);
407 toys.exitval=1;
408 }
409 return rc;
Rob Landley67a069d2012-06-03 00:32:12 -0500410}
411
Rob Landleyc52db602012-07-30 01:01:33 -0500412static char *tempfile2zap;
413static void tempfile_handler(int i)
414{
Rob Landley7aa651a2012-11-13 17:14:08 -0600415 if (1 < (long)tempfile2zap) unlink(tempfile2zap);
416 _exit(1);
Rob Landleyc52db602012-07-30 01:01:33 -0500417}
418
Rob Landley42ecbab2007-12-18 02:02:21 -0600419// Open a temporary file to copy an existing file into.
420int copy_tempfile(int fdin, char *name, char **tempname)
421{
Rob Landley7aa651a2012-11-13 17:14:08 -0600422 struct stat statbuf;
423 int fd;
Rob Landley42ecbab2007-12-18 02:02:21 -0600424
Rob Landley7aa651a2012-11-13 17:14:08 -0600425 *tempname = xstrndup(name, strlen(name)+6);
426 strcat(*tempname,"XXXXXX");
427 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
428 if (!tempfile2zap) sigatexit(tempfile_handler);
429 tempfile2zap = *tempname;
Rob Landley42ecbab2007-12-18 02:02:21 -0600430
Rob Landley7aa651a2012-11-13 17:14:08 -0600431 // Set permissions of output file
Rob Landley42ecbab2007-12-18 02:02:21 -0600432
Rob Landley7aa651a2012-11-13 17:14:08 -0600433 fstat(fdin, &statbuf);
434 fchmod(fd, statbuf.st_mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600435
Rob Landley7aa651a2012-11-13 17:14:08 -0600436 return fd;
Rob Landley42ecbab2007-12-18 02:02:21 -0600437}
438
439// Abort the copy and delete the temporary file.
440void delete_tempfile(int fdin, int fdout, char **tempname)
441{
Rob Landley7aa651a2012-11-13 17:14:08 -0600442 close(fdin);
443 close(fdout);
444 unlink(*tempname);
445 tempfile2zap = (char *)1;
446 free(*tempname);
447 *tempname = NULL;
Rob Landley42ecbab2007-12-18 02:02:21 -0600448}
449
450// Copy the rest of the data and replace the original with the copy.
451void replace_tempfile(int fdin, int fdout, char **tempname)
452{
Rob Landley7aa651a2012-11-13 17:14:08 -0600453 char *temp = xstrdup(*tempname);
Rob Landley42ecbab2007-12-18 02:02:21 -0600454
Rob Landley7aa651a2012-11-13 17:14:08 -0600455 temp[strlen(temp)-6]=0;
456 if (fdin != -1) {
457 xsendfile(fdin, fdout);
458 xclose(fdin);
459 }
460 xclose(fdout);
461 rename(*tempname, temp);
462 tempfile2zap = (char *)1;
463 free(*tempname);
464 free(temp);
465 *tempname = NULL;
Rob Landleybc078652007-12-15 21:47:25 -0600466}
Rob Landley7e849c52009-01-03 18:15:18 -0600467
468// Create a 256 entry CRC32 lookup table.
469
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600470void crc_init(unsigned int *crc_table, int little_endian)
Rob Landley7e849c52009-01-03 18:15:18 -0600471{
Rob Landley7aa651a2012-11-13 17:14:08 -0600472 unsigned int i;
Rob Landley7e849c52009-01-03 18:15:18 -0600473
Rob Landley7aa651a2012-11-13 17:14:08 -0600474 // Init the CRC32 table (big endian)
475 for (i=0; i<256; i++) {
476 unsigned int j, c = little_endian ? i : i<<24;
477 for (j=8; j; j--)
478 if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1;
479 else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
480 crc_table[i] = c;
481 }
Rob Landley7e849c52009-01-03 18:15:18 -0600482}
Rob Landley26e7b5e2012-02-02 07:27:35 -0600483
484// Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
485// set *x=0 and *y=0 before calling to detect failure to set either, or
486// x=80 y=25 to provide defaults
487
Rob Landleyc9cc5302013-10-27 00:02:56 -0500488void terminal_size(unsigned *xx, unsigned *yy)
Rob Landley26e7b5e2012-02-02 07:27:35 -0600489{
Rob Landley7aa651a2012-11-13 17:14:08 -0600490 struct winsize ws;
Rob Landleyc9cc5302013-10-27 00:02:56 -0500491 unsigned i, x = xx ? *xx : 0, y = yy ? *yy : 0;
492 char *s;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600493
Rob Landley7aa651a2012-11-13 17:14:08 -0600494 for (i=0; i<3; i++) {
Rob Landleyc9cc5302013-10-27 00:02:56 -0500495 memset(&ws, 0, sizeof(ws));
496 if (!ioctl(i, TIOCGWINSZ, &ws)) {
497 if (ws.ws_col) x = ws.ws_col;
498 if (ws.ws_row) y = ws.ws_row;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600499
Rob Landleyc9cc5302013-10-27 00:02:56 -0500500 break;
501 }
Rob Landley7aa651a2012-11-13 17:14:08 -0600502 }
Rob Landleyc9cc5302013-10-27 00:02:56 -0500503 s = getenv("COLUMNS");
504 if (s) sscanf(s, "%u", &x);
505 s = getenv("ROWS");
506 if (s) sscanf(s, "%u", &y);
Rob Landley26e7b5e2012-02-02 07:27:35 -0600507
Rob Landleyc9cc5302013-10-27 00:02:56 -0500508 if (xx) *xx = x;
509 if (yy) *yy = y;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600510}
511
Rob Landleyf793d532012-02-27 21:56:49 -0600512int yesno(char *prompt, int def)
Rob Landley26e7b5e2012-02-02 07:27:35 -0600513{
Rob Landley7aa651a2012-11-13 17:14:08 -0600514 char buf;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600515
Rob Landleydb8eb322012-12-08 02:25:32 -0600516 fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
517 fflush(stderr);
518 while (fread(&buf, 1, 1, stdin)) {
519 int new;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600520
Rob Landley22791082013-01-31 04:13:07 -0600521 // The letter changes the value, the newline (or space) returns it.
Rob Landleydb8eb322012-12-08 02:25:32 -0600522 if (isspace(buf)) break;
523 if (-1 != (new = stridx("ny", tolower(buf)))) def = new;
Rob Landley7aa651a2012-11-13 17:14:08 -0600524 }
Rob Landleyee00a7f2012-03-19 19:19:21 -0500525
Rob Landley7aa651a2012-11-13 17:14:08 -0600526 return def;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600527}
Rob Landleyff9ee8f2012-02-18 15:12:41 -0600528
Rob Landley2dd50ad2012-02-26 13:48:00 -0600529struct signame {
Rob Landley7aa651a2012-11-13 17:14:08 -0600530 int num;
531 char *name;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600532};
533
534// Signals required by POSIX 2008:
535// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
536
537#define SIGNIFY(x) {SIG##x, #x}
538
539static struct signame signames[] = {
Rob Landley7aa651a2012-11-13 17:14:08 -0600540 SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS),
541 SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
542 SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM),
543 SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP),
544 SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ),
Rob Landleyc52db602012-07-30 01:01:33 -0500545
Rob Landley7aa651a2012-11-13 17:14:08 -0600546 // Start of non-terminal signals
Rob Landleyc52db602012-07-30 01:01:33 -0500547
Rob Landley7aa651a2012-11-13 17:14:08 -0600548 SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP),
549 SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG)
Rob Landley2dd50ad2012-02-26 13:48:00 -0600550};
551
552// not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR)
553// obsolete: SIGNIFY(PROF) SIGNIFY(POLL)
554
Rob Landleyc52db602012-07-30 01:01:33 -0500555// Install the same handler on every signal that defaults to killing the process
556void sigatexit(void *handler)
557{
Rob Landley7aa651a2012-11-13 17:14:08 -0600558 int i;
559 for (i=0; signames[i].num != SIGCHLD; i++) signal(signames[i].num, handler);
Rob Landleyc52db602012-07-30 01:01:33 -0500560}
Rob Landley2dd50ad2012-02-26 13:48:00 -0600561// Convert name to signal number. If name == NULL print names.
562int sig_to_num(char *pidstr)
563{
Rob Landley7aa651a2012-11-13 17:14:08 -0600564 int i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600565
Rob Landley7aa651a2012-11-13 17:14:08 -0600566 if (pidstr) {
567 char *s;
568 i = strtol(pidstr, &s, 10);
569 if (!*s) return i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600570
Rob Landley7aa651a2012-11-13 17:14:08 -0600571 if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3;
572 }
573 for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++)
574 if (!pidstr) xputs(signames[i].name);
575 else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600576
Rob Landley7aa651a2012-11-13 17:14:08 -0600577 return -1;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600578}
579
580char *num_to_sig(int sig)
581{
Rob Landley7aa651a2012-11-13 17:14:08 -0600582 int i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600583
Rob Landley7aa651a2012-11-13 17:14:08 -0600584 for (i=0; i<sizeof(signames)/sizeof(struct signame); i++)
585 if (signames[i].num == sig) return signames[i].name;
586 return NULL;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600587}
Daniel Walter05744b32012-03-19 19:57:56 -0500588
Rob Landleyc52db602012-07-30 01:01:33 -0500589// premute mode bits based on posix mode strings.
Rob Landleyb6f601e2012-05-16 21:11:43 -0500590mode_t string_to_mode(char *modestr, mode_t mode)
Daniel Walter05744b32012-03-19 19:57:56 -0500591{
Rob Landley7aa651a2012-11-13 17:14:08 -0600592 char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu";
593 char *s, *str = modestr;
Rob Landleycf6bcb22012-03-19 20:56:18 -0500594
Rob Landley7aa651a2012-11-13 17:14:08 -0600595 // Handle octal mode
596 if (isdigit(*str)) {
597 mode = strtol(str, &s, 8);
598 if (*s || (mode & ~(07777))) goto barf;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500599
Rob Landley7aa651a2012-11-13 17:14:08 -0600600 return mode;
601 }
Rob Landleycf6bcb22012-03-19 20:56:18 -0500602
Rob Landley7aa651a2012-11-13 17:14:08 -0600603 // Gaze into the bin of permission...
604 for (;;) {
605 int i, j, dowho, dohow, dowhat, amask;
Rob Landleycf6bcb22012-03-19 20:56:18 -0500606
Rob Landley7aa651a2012-11-13 17:14:08 -0600607 dowho = dohow = dowhat = amask = 0;
Daniel Walter05744b32012-03-19 19:57:56 -0500608
Rob Landley7aa651a2012-11-13 17:14:08 -0600609 // Find the who, how, and what stanzas, in that order
610 while (*str && (s = strchr(whos, *str))) {
611 dowho |= 1<<(s-whos);
612 str++;
613 }
614 // If who isn't specified, like "a" but honoring umask.
615 if (!dowho) {
616 dowho = 8;
617 umask(amask=umask(0));
618 }
619 if (!*str || !(s = strchr(hows, *str))) goto barf;
620 dohow = *(str++);
Rob Landley67a069d2012-06-03 00:32:12 -0500621
Rob Landley7aa651a2012-11-13 17:14:08 -0600622 if (!dohow) goto barf;
623 while (*str && (s = strchr(whats, *str))) {
624 dowhat |= 1<<(s-whats);
625 str++;
626 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500627
Rob Landley7aa651a2012-11-13 17:14:08 -0600628 // Convert X to x for directory or if already executable somewhere
629 if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500630
Rob Landley7aa651a2012-11-13 17:14:08 -0600631 // Copy mode from another category?
632 if (!dowhat && *str && (s = strchr(whys, *str))) {
633 dowhat = (mode>>(3*(s-whys)))&7;
634 str++;
635 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500636
Rob Landley7aa651a2012-11-13 17:14:08 -0600637 // Are we ready to do a thing yet?
638 if (*str && *(str++) != ',') goto barf;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500639
Rob Landley7aa651a2012-11-13 17:14:08 -0600640 // Ok, apply the bits to the mode.
641 for (i=0; i<4; i++) {
642 for (j=0; j<3; j++) {
643 mode_t bit = 0;
644 int where = 1<<((3*i)+j);
Rob Landley31f49e72012-07-21 22:45:05 -0500645
Rob Landley7aa651a2012-11-13 17:14:08 -0600646 if (amask & where) continue;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500647
Rob Landley7aa651a2012-11-13 17:14:08 -0600648 // Figure out new value at this location
649 if (i == 3) {
650 // suid/sticky bit.
651 if (j) {
652 if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++;
653 } else if (dowhat & 16) bit++;
654 } else {
655 if (!(dowho&(8|(1<<i)))) continue;
656 if (dowhat&(1<<j)) bit++;
657 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500658
Rob Landley7aa651a2012-11-13 17:14:08 -0600659 // When selection active, modify bit
Rob Landleyb6f601e2012-05-16 21:11:43 -0500660
Rob Landley7aa651a2012-11-13 17:14:08 -0600661 if (dohow == '=' || (bit && dohow == '-')) mode &= ~where;
662 if (bit && dohow != '-') mode |= where;
663 }
664 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500665
Rob Landley7aa651a2012-11-13 17:14:08 -0600666 if (!*str) break;
667 }
668 return mode;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500669barf:
Rob Landley7aa651a2012-11-13 17:14:08 -0600670 error_exit("bad mode '%s'", modestr);
Daniel Walter05744b32012-03-19 19:57:56 -0500671}
Ashwini Kumar1a0eedf2012-08-26 21:17:00 -0500672
Rob Landley5a26a862013-06-02 00:24:24 -0500673// Format access mode into a drwxrwxrwx string
674void mode_to_string(mode_t mode, char *buf)
Felix Janda5a221e62013-04-22 22:29:43 +0200675{
676 char c, d;
677 int i, bit;
678
Rob Landley5a26a862013-06-02 00:24:24 -0500679 buf[10]=0;
Felix Janda5a221e62013-04-22 22:29:43 +0200680 for (i=0; i<9; i++) {
681 bit = mode & (1<<i);
682 c = i%3;
683 if (!c && (mode & (1<<((d=i/3)+9)))) {
684 c = "tss"[d];
685 if (!bit) c &= ~0x20;
686 } else c = bit ? "xwr"[c] : '-';
Rob Landley5a26a862013-06-02 00:24:24 -0500687 buf[9-i] = c;
Felix Janda5a221e62013-04-22 22:29:43 +0200688 }
689
690 if (S_ISDIR(mode)) c = 'd';
691 else if (S_ISBLK(mode)) c = 'b';
692 else if (S_ISCHR(mode)) c = 'c';
693 else if (S_ISLNK(mode)) c = 'l';
694 else if (S_ISFIFO(mode)) c = 'p';
695 else if (S_ISSOCK(mode)) c = 's';
696 else c = '-';
Rob Landley5a26a862013-06-02 00:24:24 -0500697 *buf = c;
Felix Janda5a221e62013-04-22 22:29:43 +0200698}