blob: 52c95147f84370e45f5b46fcaa1196ca3f46a5cc [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 {
99 char buf[4096];
100 while (offset>0) {
101 int try = offset>sizeof(buf) ? sizeof(buf) : offset, or;
Rob Landley2037b832012-07-15 16:56:20 -0500102
Rob Landley7aa651a2012-11-13 17:14:08 -0600103 or = readall(fd, buf, try);
104 if (or < 0) perror_msg("lskip to %lld", (long long)offset);
105 else offset -= try;
106 if (or < try) break;
107 }
Rob Landley2037b832012-07-15 16:56:20 -0500108
Rob Landley7aa651a2012-11-13 17:14:08 -0600109 return offset;
110 }
Rob Landley2037b832012-07-15 16:56:20 -0500111}
112
Rob Landleyfe91e682012-11-22 21:18:09 -0600113// Split a path into linked list of components, tracking head and tail of list.
114// Filters out // entries with no contents.
115struct string_list **splitpath(char *path, struct string_list **list)
landley00f87f12006-10-25 18:38:37 -0400116{
Rob Landleyfe91e682012-11-22 21:18:09 -0600117 char *new = path;
landley00f87f12006-10-25 18:38:37 -0400118
Rob Landleyfe91e682012-11-22 21:18:09 -0600119 *list = 0;
120 do {
121 int len;
landley00f87f12006-10-25 18:38:37 -0400122
Rob Landleyfe91e682012-11-22 21:18:09 -0600123 if (*path && *path != '/') continue;
124 len = path-new;
125 if (len > 0) {
126 *list = xmalloc(sizeof(struct string_list) + len + 1);
127 (*list)->next = 0;
128 strncpy((*list)->str, new, len);
129 (*list)->str[len] = 0;
130 list = &(*list)->next;
131 }
132 new = path+1;
133 } while (*path++);
134
135 return list;
136}
137
Rob Landley0a04b3e2006-11-03 00:05:52 -0500138// Find all file in a colon-separated path with access type "type" (generally
139// X_OK or R_OK). Returns a list of absolute paths to each file found, in
140// order.
141
142struct string_list *find_in_path(char *path, char *filename)
Rob Landleyfa98d012006-11-02 02:57:27 -0500143{
Rob Landley7aa651a2012-11-13 17:14:08 -0600144 struct string_list *rlist = NULL, **prlist=&rlist;
145 char *cwd = xgetcwd();
Rob Landleyfa98d012006-11-02 02:57:27 -0500146
Rob Landley7aa651a2012-11-13 17:14:08 -0600147 for (;;) {
148 char *next = path ? strchr(path, ':') : NULL;
149 int len = next ? next-path : strlen(path);
150 struct string_list *rnext;
151 struct stat st;
Rob Landleyfa98d012006-11-02 02:57:27 -0500152
Rob Landley7aa651a2012-11-13 17:14:08 -0600153 rnext = xmalloc(sizeof(void *) + strlen(filename)
154 + (len ? len : strlen(cwd)) + 2);
155 if (!len) sprintf(rnext->str, "%s/%s", cwd, filename);
156 else {
157 char *res = rnext->str;
158 strncpy(res, path, len);
159 res += len;
160 *(res++) = '/';
161 strcpy(res, filename);
162 }
Rob Landleyfa98d012006-11-02 02:57:27 -0500163
Rob Landley7aa651a2012-11-13 17:14:08 -0600164 // Confirm it's not a directory.
165 if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) {
166 *prlist = rnext;
167 rnext->next = NULL;
168 prlist = &(rnext->next);
169 } else free(rnext);
Rob Landleyfa98d012006-11-02 02:57:27 -0500170
Rob Landley7aa651a2012-11-13 17:14:08 -0600171 if (!next) break;
172 path += len;
173 path++;
174 }
175 free(cwd);
landley00f87f12006-10-25 18:38:37 -0400176
Rob Landley7aa651a2012-11-13 17:14:08 -0600177 return rlist;
landley00f87f12006-10-25 18:38:37 -0400178}
landley09ea7ac2006-10-30 01:38:00 -0500179
180// Convert unsigned int to ascii, writing into supplied buffer. A truncated
181// result contains the first few digits of the result ala strncpy, and is
182// always null terminated (unless buflen is 0).
183void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
184{
Rob Landley7aa651a2012-11-13 17:14:08 -0600185 int i, out = 0;
landley09ea7ac2006-10-30 01:38:00 -0500186
Rob Landley7aa651a2012-11-13 17:14:08 -0600187 if (buflen) {
188 for (i=1000000000; i; i/=10) {
189 int res = n/i;
landley09ea7ac2006-10-30 01:38:00 -0500190
Rob Landley7aa651a2012-11-13 17:14:08 -0600191 if ((res || out || i == 1) && --buflen>0) {
192 out++;
193 n -= res*i;
194 *buf++ = '0' + res;
195 }
196 }
197 *buf = 0;
198 }
landley09ea7ac2006-10-30 01:38:00 -0500199}
200
201// Convert signed integer to ascii, using utoa_to_buf()
202void itoa_to_buf(int n, char *buf, unsigned buflen)
203{
Rob Landley7aa651a2012-11-13 17:14:08 -0600204 if (buflen && n<0) {
205 n = -n;
206 *buf++ = '-';
207 buflen--;
208 }
209 utoa_to_buf((unsigned)n, buf, buflen);
landley09ea7ac2006-10-30 01:38:00 -0500210}
211
212// This static buffer is used by both utoa() and itoa(), calling either one a
213// second time will overwrite the previous results.
214//
215// The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes.
216// Note that int is always 32 bits on any remotely unix-like system, see
217// http://www.unix.org/whitepapers/64bit.html for details.
218
219static char itoa_buf[12];
220
221// Convert unsigned integer to ascii, returning a static buffer.
222char *utoa(unsigned n)
223{
Rob Landley7aa651a2012-11-13 17:14:08 -0600224 utoa_to_buf(n, itoa_buf, sizeof(itoa_buf));
landley09ea7ac2006-10-30 01:38:00 -0500225
Rob Landley7aa651a2012-11-13 17:14:08 -0600226 return itoa_buf;
landley09ea7ac2006-10-30 01:38:00 -0500227}
228
229char *itoa(int n)
230{
Rob Landley7aa651a2012-11-13 17:14:08 -0600231 itoa_to_buf(n, itoa_buf, sizeof(itoa_buf));
landley09ea7ac2006-10-30 01:38:00 -0500232
Rob Landley7aa651a2012-11-13 17:14:08 -0600233 return itoa_buf;
landley09ea7ac2006-10-30 01:38:00 -0500234}
Rob Landley055cfcb2007-01-14 20:20:06 -0500235
Rob Landleyf5757162007-02-16 21:08:22 -0500236// atol() with the kilo/mega/giga/tera/peta/exa extensions.
237// (zetta and yotta don't fit in 64 bits.)
Rob Landley5e6dca62012-02-09 06:09:27 -0600238long atolx(char *numstr)
Rob Landleyf5757162007-02-16 21:08:22 -0500239{
Rob Landley7aa651a2012-11-13 17:14:08 -0600240 char *c, *suffixes="bkmgtpe", *end;
241 long val = strtol(numstr, &c, 0);
Rob Landleyf5757162007-02-16 21:08:22 -0500242
Rob Landley7aa651a2012-11-13 17:14:08 -0600243 if (*c) {
244 if (c != numstr && (end = strchr(suffixes, tolower(*c)))) {
245 int shift = end-suffixes;
246 if (shift--) val *= 1024L<<(shift*10);
247 } else {
248 while (isspace(*c)) c++;
249 if (*c) error_exit("not integer: %s", numstr);
250 }
251 }
Rob Landleyad63f4b2011-12-12 15:19:52 -0600252
Rob Landley7aa651a2012-11-13 17:14:08 -0600253 return val;
Rob Landleyf5757162007-02-16 21:08:22 -0500254}
255
Rob Landleyeb7ea222012-04-14 22:30:41 -0500256int numlen(long l)
257{
Rob Landley7aa651a2012-11-13 17:14:08 -0600258 int len = 0;
259 while (l) {
260 l /= 10;
261 len++;
262 }
263 return len;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500264}
265
Rob Landley2037b832012-07-15 16:56:20 -0500266int stridx(char *haystack, char needle)
267{
Rob Landley7aa651a2012-11-13 17:14:08 -0600268 char *off;
Rob Landley2037b832012-07-15 16:56:20 -0500269
Rob Landley7aa651a2012-11-13 17:14:08 -0600270 if (!needle) return -1;
271 off = strchr(haystack, needle);
272 if (!off) return -1;
Rob Landley2037b832012-07-15 16:56:20 -0500273
Rob Landley7aa651a2012-11-13 17:14:08 -0600274 return off-haystack;
Rob Landley2037b832012-07-15 16:56:20 -0500275}
276
Rob Landley055cfcb2007-01-14 20:20:06 -0500277// Return how long the file at fd is, if there's any way to determine it.
278off_t fdlength(int fd)
279{
Rob Landley7aa651a2012-11-13 17:14:08 -0600280 off_t bottom = 0, top = 0, pos, old;
281 int size;
Rob Landley055cfcb2007-01-14 20:20:06 -0500282
Rob Landley7aa651a2012-11-13 17:14:08 -0600283 // If the ioctl works for this, return it.
Rob Landley055cfcb2007-01-14 20:20:06 -0500284
Rob Landley7aa651a2012-11-13 17:14:08 -0600285 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L;
Rob Landley055cfcb2007-01-14 20:20:06 -0500286
Rob Landley7aa651a2012-11-13 17:14:08 -0600287 // If not, do a binary search for the last location we can read. (Some
288 // block devices don't do BLKGETSIZE right.) This should probably have
289 // a CONFIG option...
Rob Landley055cfcb2007-01-14 20:20:06 -0500290
Rob Landley7aa651a2012-11-13 17:14:08 -0600291 old = lseek(fd, 0, SEEK_CUR);
292 do {
293 char temp;
Rob Landley055cfcb2007-01-14 20:20:06 -0500294
Rob Landley7aa651a2012-11-13 17:14:08 -0600295 pos = bottom + (top - bottom) / 2;
Rob Landley055cfcb2007-01-14 20:20:06 -0500296
Rob Landley7aa651a2012-11-13 17:14:08 -0600297 // If we can read from the current location, it's bigger.
Rob Landley055cfcb2007-01-14 20:20:06 -0500298
Rob Landley7aa651a2012-11-13 17:14:08 -0600299 if (lseek(fd, pos, 0)>=0 && read(fd, &temp, 1)==1) {
300 if (bottom == top) bottom = top = (top+1) * 2;
301 else bottom = pos;
Rob Landley055cfcb2007-01-14 20:20:06 -0500302
Rob Landley7aa651a2012-11-13 17:14:08 -0600303 // If we can't, it's smaller.
Rob Landley055cfcb2007-01-14 20:20:06 -0500304
Rob Landley7aa651a2012-11-13 17:14:08 -0600305 } else {
306 if (bottom == top) {
307 if (!top) return 0;
308 bottom = top/2;
309 } else top = pos;
310 }
311 } while (bottom + 1 != top);
Rob Landley055cfcb2007-01-14 20:20:06 -0500312
Rob Landley7aa651a2012-11-13 17:14:08 -0600313 lseek(fd, old, SEEK_SET);
Rob Landleyf15387d2008-07-18 05:43:44 -0500314
Rob Landley7aa651a2012-11-13 17:14:08 -0600315 return pos + 1;
Rob Landley055cfcb2007-01-14 20:20:06 -0500316}
317
318// Read contents of file as a single freshly allocated nul-terminated string.
319char *readfile(char *name)
320{
Rob Landley7aa651a2012-11-13 17:14:08 -0600321 off_t len;
322 int fd;
323 char *buf;
Rob Landley055cfcb2007-01-14 20:20:06 -0500324
Rob Landley7aa651a2012-11-13 17:14:08 -0600325 fd = open(name, O_RDONLY);
326 if (fd == -1) return 0;
327 len = fdlength(fd);
328 buf = xmalloc(len+1);
329 buf[readall(fd, buf, len)] = 0;
Rob Landley055cfcb2007-01-14 20:20:06 -0500330
Rob Landley7aa651a2012-11-13 17:14:08 -0600331 return buf;
Rob Landley055cfcb2007-01-14 20:20:06 -0500332}
333
Rob Landleyf0153442013-04-26 02:41:05 -0500334// Sleep for this many thousandths of a second
335void msleep(long miliseconds)
336{
337 struct timespec ts;
338
339 ts.tv_sec = miliseconds/1000;
340 ts.tv_nsec = (miliseconds%1000)*1000000;
341 nanosleep(&ts, &ts);
342}
343
Rob Landley6b283412013-06-01 20:41:35 -0500344int64_t peek(void *ptr, int size)
345{
346 if (size & 8) {
347 int64_t *p = (int64_t *)ptr;
348 return *p;
349 } else if (size & 4) {
350 int *p = (int *)ptr;
351 return *p;
352 } else if (size & 2) {
353 short *p = (short *)ptr;
354 return *p;
355 } else {
356 char *p = (char *)ptr;
357 return *p;
358 }
359}
360
361void poke(void *ptr, uint64_t val, int size)
362{
363 if (size & 8) {
364 uint64_t *p = (uint64_t *)ptr;
365 *p = val;
366 } else if (size & 4) {
367 int *p = (int *)ptr;
368 *p = val;
369 } else if (size & 2) {
370 short *p = (short *)ptr;
371 *p = val;
372 } else {
373 char *p = (char *)ptr;
374 *p = val;
375 }
376}
377
Rob Landley2bfaaf22008-07-03 19:19:00 -0500378// Iterate through an array of files, opening each one and calling a function
379// on that filehandle and name. The special filename "-" means stdin if
380// flags is O_RDONLY, stdout otherwise. An empty argument list calls
381// function() on just stdin/stdout.
382//
383// Note: read only filehandles are automatically closed when function()
384// returns, but writeable filehandles must be close by function()
Rob Landleyad63f4b2011-12-12 15:19:52 -0600385void loopfiles_rw(char **argv, int flags, int permissions, int failok,
Rob Landley7aa651a2012-11-13 17:14:08 -0600386 void (*function)(int fd, char *name))
Rob Landley7634b552007-11-29 17:49:50 -0600387{
Rob Landley7aa651a2012-11-13 17:14:08 -0600388 int fd;
Rob Landley7634b552007-11-29 17:49:50 -0600389
Rob Landley7aa651a2012-11-13 17:14:08 -0600390 // If no arguments, read from stdin.
391 if (!*argv) function(flags ? 1 : 0, "-");
392 else do {
393 // Filename "-" means read from stdin.
394 // Inability to open a file prints a warning, but doesn't exit.
Rob Landley7634b552007-11-29 17:49:50 -0600395
Rob Landley7aa651a2012-11-13 17:14:08 -0600396 if (!strcmp(*argv,"-")) fd=0;
397 else if (0>(fd = open(*argv, flags, permissions)) && !failok) {
398 perror_msg("%s", *argv);
399 toys.exitval = 1;
400 continue;
401 }
402 function(fd, *argv);
403 if (flags == O_RDONLY) close(fd);
404 } while (*++argv);
Rob Landley7634b552007-11-29 17:49:50 -0600405}
Rob Landleybc078652007-12-15 21:47:25 -0600406
Rob Landleyad63f4b2011-12-12 15:19:52 -0600407// Call loopfiles_rw with O_RDONLY and !failok (common case).
Rob Landley2bfaaf22008-07-03 19:19:00 -0500408void loopfiles(char **argv, void (*function)(int fd, char *name))
409{
Rob Landley7aa651a2012-11-13 17:14:08 -0600410 loopfiles_rw(argv, O_RDONLY, 0, 0, function);
Rob Landley2bfaaf22008-07-03 19:19:00 -0500411}
412
Rob Landleybc078652007-12-15 21:47:25 -0600413// Slow, but small.
414
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500415char *get_rawline(int fd, long *plen, char end)
Rob Landleybc078652007-12-15 21:47:25 -0600416{
Rob Landley7aa651a2012-11-13 17:14:08 -0600417 char c, *buf = NULL;
418 long len = 0;
Rob Landleybc078652007-12-15 21:47:25 -0600419
Rob Landley7aa651a2012-11-13 17:14:08 -0600420 for (;;) {
421 if (1>read(fd, &c, 1)) break;
422 if (!(len & 63)) buf=xrealloc(buf, len+65);
423 if ((buf[len++]=c) == end) break;
424 }
425 if (buf) buf[len]=0;
426 if (plen) *plen = len;
Rob Landleybc078652007-12-15 21:47:25 -0600427
Rob Landley7aa651a2012-11-13 17:14:08 -0600428 return buf;
Rob Landleybc078652007-12-15 21:47:25 -0600429}
430
431char *get_line(int fd)
432{
Rob Landley7aa651a2012-11-13 17:14:08 -0600433 long len;
434 char *buf = get_rawline(fd, &len, '\n');
Rob Landleybc078652007-12-15 21:47:25 -0600435
Rob Landley7aa651a2012-11-13 17:14:08 -0600436 if (buf && buf[--len]=='\n') buf[len]=0;
Rob Landleybc078652007-12-15 21:47:25 -0600437
Rob Landley7aa651a2012-11-13 17:14:08 -0600438 return buf;
Rob Landleybc078652007-12-15 21:47:25 -0600439}
440
Rob Landley67a069d2012-06-03 00:32:12 -0500441int wfchmodat(int fd, char *name, mode_t mode)
442{
Rob Landley7aa651a2012-11-13 17:14:08 -0600443 int rc = fchmodat(fd, name, mode, 0);
Rob Landley67a069d2012-06-03 00:32:12 -0500444
Rob Landley7aa651a2012-11-13 17:14:08 -0600445 if (rc) {
446 perror_msg("chmod '%s' to %04o", name, mode);
447 toys.exitval=1;
448 }
449 return rc;
Rob Landley67a069d2012-06-03 00:32:12 -0500450}
451
Rob Landleyc52db602012-07-30 01:01:33 -0500452static char *tempfile2zap;
453static void tempfile_handler(int i)
454{
Rob Landley7aa651a2012-11-13 17:14:08 -0600455 if (1 < (long)tempfile2zap) unlink(tempfile2zap);
456 _exit(1);
Rob Landleyc52db602012-07-30 01:01:33 -0500457}
458
Rob Landley42ecbab2007-12-18 02:02:21 -0600459// Open a temporary file to copy an existing file into.
460int copy_tempfile(int fdin, char *name, char **tempname)
461{
Rob Landley7aa651a2012-11-13 17:14:08 -0600462 struct stat statbuf;
463 int fd;
Rob Landley42ecbab2007-12-18 02:02:21 -0600464
Rob Landley7aa651a2012-11-13 17:14:08 -0600465 *tempname = xstrndup(name, strlen(name)+6);
466 strcat(*tempname,"XXXXXX");
467 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
468 if (!tempfile2zap) sigatexit(tempfile_handler);
469 tempfile2zap = *tempname;
Rob Landley42ecbab2007-12-18 02:02:21 -0600470
Rob Landley7aa651a2012-11-13 17:14:08 -0600471 // Set permissions of output file
Rob Landley42ecbab2007-12-18 02:02:21 -0600472
Rob Landley7aa651a2012-11-13 17:14:08 -0600473 fstat(fdin, &statbuf);
474 fchmod(fd, statbuf.st_mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600475
Rob Landley7aa651a2012-11-13 17:14:08 -0600476 return fd;
Rob Landley42ecbab2007-12-18 02:02:21 -0600477}
478
479// Abort the copy and delete the temporary file.
480void delete_tempfile(int fdin, int fdout, char **tempname)
481{
Rob Landley7aa651a2012-11-13 17:14:08 -0600482 close(fdin);
483 close(fdout);
484 unlink(*tempname);
485 tempfile2zap = (char *)1;
486 free(*tempname);
487 *tempname = NULL;
Rob Landley42ecbab2007-12-18 02:02:21 -0600488}
489
490// Copy the rest of the data and replace the original with the copy.
491void replace_tempfile(int fdin, int fdout, char **tempname)
492{
Rob Landley7aa651a2012-11-13 17:14:08 -0600493 char *temp = xstrdup(*tempname);
Rob Landley42ecbab2007-12-18 02:02:21 -0600494
Rob Landley7aa651a2012-11-13 17:14:08 -0600495 temp[strlen(temp)-6]=0;
496 if (fdin != -1) {
497 xsendfile(fdin, fdout);
498 xclose(fdin);
499 }
500 xclose(fdout);
501 rename(*tempname, temp);
502 tempfile2zap = (char *)1;
503 free(*tempname);
504 free(temp);
505 *tempname = NULL;
Rob Landleybc078652007-12-15 21:47:25 -0600506}
Rob Landley7e849c52009-01-03 18:15:18 -0600507
508// Create a 256 entry CRC32 lookup table.
509
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600510void crc_init(unsigned int *crc_table, int little_endian)
Rob Landley7e849c52009-01-03 18:15:18 -0600511{
Rob Landley7aa651a2012-11-13 17:14:08 -0600512 unsigned int i;
Rob Landley7e849c52009-01-03 18:15:18 -0600513
Rob Landley7aa651a2012-11-13 17:14:08 -0600514 // Init the CRC32 table (big endian)
515 for (i=0; i<256; i++) {
516 unsigned int j, c = little_endian ? i : i<<24;
517 for (j=8; j; j--)
518 if (little_endian) c = (c&1) ? (c>>1)^0xEDB88320 : c>>1;
519 else c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
520 crc_table[i] = c;
521 }
Rob Landley7e849c52009-01-03 18:15:18 -0600522}
Rob Landley26e7b5e2012-02-02 07:27:35 -0600523
524// Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
525// set *x=0 and *y=0 before calling to detect failure to set either, or
526// x=80 y=25 to provide defaults
527
528void terminal_size(unsigned *x, unsigned *y)
529{
Rob Landley7aa651a2012-11-13 17:14:08 -0600530 struct winsize ws;
531 int i;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600532
Rob Landley7aa651a2012-11-13 17:14:08 -0600533 //memset(&ws, 0, sizeof(ws));
534 for (i=0; i<3; i++) {
535 if (ioctl(i, TIOCGWINSZ, &ws)) continue;
536 if (x) *x = ws.ws_col;
537 if (y) *y = ws.ws_row;
538 }
539 if (x) {
540 char *s = getenv("COLUMNS");
Rob Landley26e7b5e2012-02-02 07:27:35 -0600541
Rob Landley7aa651a2012-11-13 17:14:08 -0600542 i = s ? atoi(s) : 0;
543 if (i>0) *x = i;
544 }
545 if (y) {
546 char *s = getenv("ROWS");
Rob Landley26e7b5e2012-02-02 07:27:35 -0600547
Rob Landley7aa651a2012-11-13 17:14:08 -0600548 i = s ? atoi(s) : 0;
549 if (i>0) *y = i;
550 }
Rob Landley26e7b5e2012-02-02 07:27:35 -0600551}
552
Rob Landleyf793d532012-02-27 21:56:49 -0600553int yesno(char *prompt, int def)
Rob Landley26e7b5e2012-02-02 07:27:35 -0600554{
Rob Landley7aa651a2012-11-13 17:14:08 -0600555 char buf;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600556
Rob Landleydb8eb322012-12-08 02:25:32 -0600557 fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
558 fflush(stderr);
559 while (fread(&buf, 1, 1, stdin)) {
560 int new;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600561
Rob Landley22791082013-01-31 04:13:07 -0600562 // The letter changes the value, the newline (or space) returns it.
Rob Landleydb8eb322012-12-08 02:25:32 -0600563 if (isspace(buf)) break;
564 if (-1 != (new = stridx("ny", tolower(buf)))) def = new;
Rob Landley7aa651a2012-11-13 17:14:08 -0600565 }
Rob Landleyee00a7f2012-03-19 19:19:21 -0500566
Rob Landley7aa651a2012-11-13 17:14:08 -0600567 return def;
Rob Landley26e7b5e2012-02-02 07:27:35 -0600568}
Rob Landleyff9ee8f2012-02-18 15:12:41 -0600569
Rob Landley2dd50ad2012-02-26 13:48:00 -0600570struct signame {
Rob Landley7aa651a2012-11-13 17:14:08 -0600571 int num;
572 char *name;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600573};
574
575// Signals required by POSIX 2008:
576// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
577
578#define SIGNIFY(x) {SIG##x, #x}
579
580static struct signame signames[] = {
Rob Landley7aa651a2012-11-13 17:14:08 -0600581 SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS),
582 SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
583 SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM),
584 SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP),
585 SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ),
Rob Landleyc52db602012-07-30 01:01:33 -0500586
Rob Landley7aa651a2012-11-13 17:14:08 -0600587 // Start of non-terminal signals
Rob Landleyc52db602012-07-30 01:01:33 -0500588
Rob Landley7aa651a2012-11-13 17:14:08 -0600589 SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP),
590 SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG)
Rob Landley2dd50ad2012-02-26 13:48:00 -0600591};
592
593// not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR)
594// obsolete: SIGNIFY(PROF) SIGNIFY(POLL)
595
Rob Landleyc52db602012-07-30 01:01:33 -0500596// Install the same handler on every signal that defaults to killing the process
597void sigatexit(void *handler)
598{
Rob Landley7aa651a2012-11-13 17:14:08 -0600599 int i;
600 for (i=0; signames[i].num != SIGCHLD; i++) signal(signames[i].num, handler);
Rob Landleyc52db602012-07-30 01:01:33 -0500601}
Rob Landley2dd50ad2012-02-26 13:48:00 -0600602// Convert name to signal number. If name == NULL print names.
603int sig_to_num(char *pidstr)
604{
Rob Landley7aa651a2012-11-13 17:14:08 -0600605 int i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600606
Rob Landley7aa651a2012-11-13 17:14:08 -0600607 if (pidstr) {
608 char *s;
609 i = strtol(pidstr, &s, 10);
610 if (!*s) return i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600611
Rob Landley7aa651a2012-11-13 17:14:08 -0600612 if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3;
613 }
614 for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++)
615 if (!pidstr) xputs(signames[i].name);
616 else if (!strcasecmp(pidstr, signames[i].name)) return signames[i].num;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600617
Rob Landley7aa651a2012-11-13 17:14:08 -0600618 return -1;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600619}
620
621char *num_to_sig(int sig)
622{
Rob Landley7aa651a2012-11-13 17:14:08 -0600623 int i;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600624
Rob Landley7aa651a2012-11-13 17:14:08 -0600625 for (i=0; i<sizeof(signames)/sizeof(struct signame); i++)
626 if (signames[i].num == sig) return signames[i].name;
627 return NULL;
Rob Landley2dd50ad2012-02-26 13:48:00 -0600628}
Daniel Walter05744b32012-03-19 19:57:56 -0500629
Rob Landleyc52db602012-07-30 01:01:33 -0500630// premute mode bits based on posix mode strings.
Rob Landleyb6f601e2012-05-16 21:11:43 -0500631mode_t string_to_mode(char *modestr, mode_t mode)
Daniel Walter05744b32012-03-19 19:57:56 -0500632{
Rob Landley7aa651a2012-11-13 17:14:08 -0600633 char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu";
634 char *s, *str = modestr;
Rob Landleycf6bcb22012-03-19 20:56:18 -0500635
Rob Landley7aa651a2012-11-13 17:14:08 -0600636 // Handle octal mode
637 if (isdigit(*str)) {
638 mode = strtol(str, &s, 8);
639 if (*s || (mode & ~(07777))) goto barf;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500640
Rob Landley7aa651a2012-11-13 17:14:08 -0600641 return mode;
642 }
Rob Landleycf6bcb22012-03-19 20:56:18 -0500643
Rob Landley7aa651a2012-11-13 17:14:08 -0600644 // Gaze into the bin of permission...
645 for (;;) {
646 int i, j, dowho, dohow, dowhat, amask;
Rob Landleycf6bcb22012-03-19 20:56:18 -0500647
Rob Landley7aa651a2012-11-13 17:14:08 -0600648 dowho = dohow = dowhat = amask = 0;
Daniel Walter05744b32012-03-19 19:57:56 -0500649
Rob Landley7aa651a2012-11-13 17:14:08 -0600650 // Find the who, how, and what stanzas, in that order
651 while (*str && (s = strchr(whos, *str))) {
652 dowho |= 1<<(s-whos);
653 str++;
654 }
655 // If who isn't specified, like "a" but honoring umask.
656 if (!dowho) {
657 dowho = 8;
658 umask(amask=umask(0));
659 }
660 if (!*str || !(s = strchr(hows, *str))) goto barf;
661 dohow = *(str++);
Rob Landley67a069d2012-06-03 00:32:12 -0500662
Rob Landley7aa651a2012-11-13 17:14:08 -0600663 if (!dohow) goto barf;
664 while (*str && (s = strchr(whats, *str))) {
665 dowhat |= 1<<(s-whats);
666 str++;
667 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500668
Rob Landley7aa651a2012-11-13 17:14:08 -0600669 // Convert X to x for directory or if already executable somewhere
670 if ((dowhat&32) && (S_ISDIR(mode) || (mode&0111))) dowhat |= 1;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500671
Rob Landley7aa651a2012-11-13 17:14:08 -0600672 // Copy mode from another category?
673 if (!dowhat && *str && (s = strchr(whys, *str))) {
674 dowhat = (mode>>(3*(s-whys)))&7;
675 str++;
676 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500677
Rob Landley7aa651a2012-11-13 17:14:08 -0600678 // Are we ready to do a thing yet?
679 if (*str && *(str++) != ',') goto barf;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500680
Rob Landley7aa651a2012-11-13 17:14:08 -0600681 // Ok, apply the bits to the mode.
682 for (i=0; i<4; i++) {
683 for (j=0; j<3; j++) {
684 mode_t bit = 0;
685 int where = 1<<((3*i)+j);
Rob Landley31f49e72012-07-21 22:45:05 -0500686
Rob Landley7aa651a2012-11-13 17:14:08 -0600687 if (amask & where) continue;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500688
Rob Landley7aa651a2012-11-13 17:14:08 -0600689 // Figure out new value at this location
690 if (i == 3) {
691 // suid/sticky bit.
692 if (j) {
693 if ((dowhat & 8) && (dowho&(8|(1<<i)))) bit++;
694 } else if (dowhat & 16) bit++;
695 } else {
696 if (!(dowho&(8|(1<<i)))) continue;
697 if (dowhat&(1<<j)) bit++;
698 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500699
Rob Landley7aa651a2012-11-13 17:14:08 -0600700 // When selection active, modify bit
Rob Landleyb6f601e2012-05-16 21:11:43 -0500701
Rob Landley7aa651a2012-11-13 17:14:08 -0600702 if (dohow == '=' || (bit && dohow == '-')) mode &= ~where;
703 if (bit && dohow != '-') mode |= where;
704 }
705 }
Rob Landleyb6f601e2012-05-16 21:11:43 -0500706
Rob Landley7aa651a2012-11-13 17:14:08 -0600707 if (!*str) break;
708 }
709 return mode;
Rob Landleyb6f601e2012-05-16 21:11:43 -0500710barf:
Rob Landley7aa651a2012-11-13 17:14:08 -0600711 error_exit("bad mode '%s'", modestr);
Daniel Walter05744b32012-03-19 19:57:56 -0500712}
Ashwini Kumar1a0eedf2012-08-26 21:17:00 -0500713
Rob Landley5a26a862013-06-02 00:24:24 -0500714// Format access mode into a drwxrwxrwx string
715void mode_to_string(mode_t mode, char *buf)
Felix Janda5a221e62013-04-22 22:29:43 +0200716{
717 char c, d;
718 int i, bit;
719
Rob Landley5a26a862013-06-02 00:24:24 -0500720 buf[10]=0;
Felix Janda5a221e62013-04-22 22:29:43 +0200721 for (i=0; i<9; i++) {
722 bit = mode & (1<<i);
723 c = i%3;
724 if (!c && (mode & (1<<((d=i/3)+9)))) {
725 c = "tss"[d];
726 if (!bit) c &= ~0x20;
727 } else c = bit ? "xwr"[c] : '-';
Rob Landley5a26a862013-06-02 00:24:24 -0500728 buf[9-i] = c;
Felix Janda5a221e62013-04-22 22:29:43 +0200729 }
730
731 if (S_ISDIR(mode)) c = 'd';
732 else if (S_ISBLK(mode)) c = 'b';
733 else if (S_ISCHR(mode)) c = 'c';
734 else if (S_ISLNK(mode)) c = 'l';
735 else if (S_ISFIFO(mode)) c = 'p';
736 else if (S_ISSOCK(mode)) c = 's';
737 else c = '-';
Rob Landley5a26a862013-06-02 00:24:24 -0500738 *buf = c;
Felix Janda5a221e62013-04-22 22:29:43 +0200739}