1
2
3
4
5
6
7
8
9
10#include "toys.h"
11
12
13
14
15
16void xstrncpy(char *dest, char *src, size_t size)
17{
18 if (strlen(src)+1 > size) error_exit("'%s' > %ld bytes", src, (long)size);
19 strcpy(dest, src);
20}
21
22void xstrncat(char *dest, char *src, size_t size)
23{
24 long len = strlen(dest);
25
26 if (len+strlen(src)+1 > size)
27 error_exit("'%s%s' > %ld bytes", dest, src, (long)size);
28 strcpy(dest+len, src);
29}
30
31
32
33
34
35
36
37
38void _xexit(void)
39{
40 if (toys.rebound) siglongjmp(*toys.rebound, 1);
41
42 _exit(toys.exitval);
43}
44
45void xexit(void)
46{
47
48 while (toys.xexit) {
49 struct arg_list *al = llist_pop(&toys.xexit);
50
51
52
53 ((void (*)(int))(al->arg))(0);
54
55 free(al);
56 }
57 xflush(1);
58 _xexit();
59}
60
61void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off)
62{
63 void *ret = mmap(addr, length, prot, flags, fd, off);
64 if (ret == MAP_FAILED) perror_exit("mmap");
65 return ret;
66}
67
68
69void *xmalloc(size_t size)
70{
71 void *ret = malloc(size);
72 if (!ret) error_exit("xmalloc(%ld)", (long)size);
73
74 return ret;
75}
76
77
78void *xzalloc(size_t size)
79{
80 void *ret = xmalloc(size);
81 memset(ret, 0, size);
82 return ret;
83}
84
85
86
87void *xrealloc(void *ptr, size_t size)
88{
89 ptr = realloc(ptr, size);
90 if (!ptr) error_exit("xrealloc");
91
92 return ptr;
93}
94
95
96char *xstrndup(char *s, size_t n)
97{
98 char *ret = strndup(s, n);
99
100 if (!ret) error_exit("xstrndup");
101
102 return ret;
103}
104
105
106char *xstrdup(char *s)
107{
108 return xstrndup(s, strlen(s));
109}
110
111void *xmemdup(void *s, long len)
112{
113 void *ret = xmalloc(len);
114 memcpy(ret, s, len);
115
116 return ret;
117}
118
119
120char *xmprintf(char *format, ...)
121{
122 va_list va, va2;
123 int len;
124 char *ret;
125
126 va_start(va, format);
127 va_copy(va2, va);
128
129
130 len = vsnprintf(0, 0, format, va);
131 len++;
132 va_end(va);
133
134
135 ret = xmalloc(len);
136 vsnprintf(ret, len, format, va2);
137 va_end(va2);
138
139 return ret;
140}
141
142
143void xflush(int flush)
144{
145 if ((flush && fflush(0)) || ferror(stdout))
146 if (!toys.exitval) perror_msg("write");
147}
148
149void xprintf(char *format, ...)
150{
151 va_list va;
152 va_start(va, format);
153
154 vprintf(format, va);
155 va_end(va);
156 xflush(0);
157}
158
159
160void xputsl(char *s, int len)
161{
162 int out;
163
164 while (len != (out = fwrite(s, 1, len, stdout))) {
165 if (out<1) perror_exit("write");
166 len -= out;
167 s += out;
168 }
169 xflush(0);
170}
171
172
173void xputsn(char *s)
174{
175 xputsl(s, strlen(s));
176}
177
178
179void xputs(char *s)
180{
181 puts(s);
182 xflush(0);
183}
184
185void xputc(char c)
186{
187 if (EOF == fputc(c, stdout)) perror_exit("write");
188 xflush(0);
189}
190
191
192
193
194
195pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid)
196{
197 if (pid == -1) perror_exit("vfork");
198
199
200 if (!pid) toys.stacktop = 0;
201
202 return pid;
203}
204
205
206
207void xexec(char **argv)
208{
209
210 if (CFG_TOYBOX && !CFG_TOYBOX_NORECURSE && toys.stacktop && **argv != '/')
211 toy_exec(argv);
212 execvp(argv[0], argv);
213
214 toys.exitval = 126+(errno == ENOENT);
215 perror_msg("exec %s", argv[0]);
216 if (!toys.stacktop) _exit(toys.exitval);
217 xexit();
218}
219
220
221
222
223
224
225
226
227pid_t xpopen_setup(char **argv, int *pipes, void (*callback)(char **argv))
228{
229 int cestnepasun[4], pid;
230
231
232 memset(cestnepasun, 0, sizeof(cestnepasun));
233 if (pipes) for (pid = 0; pid < 2; pid++) {
234 if (pipes[pid] != -1) continue;
235 if (pipe(cestnepasun+(2*pid))) perror_exit("pipe");
236 }
237
238 if (!(pid = CFG_TOYBOX_FORK ? xfork() : XVFORK())) {
239
240
241 if (pipes) {
242
243
244
245
246 if (cestnepasun[2]) {
247 close(cestnepasun[2]);
248 pipes[1] = cestnepasun[3];
249 }
250
251
252 if (cestnepasun[1]) {
253 close(cestnepasun[1]);
254 pipes[0] = cestnepasun[0];
255 }
256
257
258 if (!pipes[1]) pipes[1] = dup(0);
259
260
261 if (pipes[0]) {
262 dup2(pipes[0], 0);
263 close(pipes[0]);
264 }
265
266
267 if (pipes[1] != 1) {
268 dup2(pipes[1], 1);
269 close(pipes[1]);
270 }
271 }
272 if (callback) callback(argv);
273 if (argv) xexec(argv);
274
275
276 if (CFG_TOYBOX_FORK) {
277 toy_init(toys.which, toys.argv);
278 toys.stacktop = 0;
279 toys.which->toy_main();
280 xexit();
281
282
283 } else {
284 char *s = "/proc/self/exe";
285
286
287
288 **toys.argv |= 0x80;
289 execv(s, toys.argv);
290 if ((s = getenv("_"))) execv(s, toys.argv);
291 perror_msg_raw(s);
292
293 _exit(127);
294 }
295 }
296
297
298 if (!CFG_TOYBOX_FORK) **toys.argv &= 0x7f;
299
300 if (pipes) {
301 if (cestnepasun[1]) {
302 pipes[0] = cestnepasun[1];
303 close(cestnepasun[0]);
304 }
305 if (cestnepasun[2]) {
306 pipes[1] = cestnepasun[2];
307 close(cestnepasun[3]);
308 }
309 }
310
311 return pid;
312}
313
314pid_t xpopen_both(char **argv, int *pipes)
315{
316 return xpopen_setup(argv, pipes, 0);
317}
318
319
320
321int xwaitpid(pid_t pid)
322{
323 int status;
324
325 while (-1 == waitpid(pid, &status, 0) && errno == EINTR);
326
327 return WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+128;
328}
329
330int xpclose_both(pid_t pid, int *pipes)
331{
332 if (pipes) {
333 close(pipes[0]);
334 close(pipes[1]);
335 }
336
337 return xwaitpid(pid);
338}
339
340
341pid_t xpopen(char **argv, int *pipe, int isstdout)
342{
343 int pipes[2], pid;
344
345 pipes[0] = isstdout ? 0 : -1;
346 pipes[1] = isstdout ? -1 : 1;
347 pid = xpopen_both(argv, pipes);
348 *pipe = pid ? pipes[!!isstdout] : -1;
349
350 return pid;
351}
352
353int xpclose(pid_t pid, int pipe)
354{
355 close(pipe);
356
357 return xpclose_both(pid, 0);
358}
359
360
361int xrun(char **argv)
362{
363 return xpclose_both(xpopen_both(argv, 0), 0);
364}
365
366void xaccess(char *path, int flags)
367{
368 if (access(path, flags)) perror_exit("Can't access '%s'", path);
369}
370
371
372void xunlink(char *path)
373{
374 if (unlink(path)) perror_exit("unlink '%s'", path);
375}
376
377
378
379
380int xcreate_stdio(char *path, int flags, int mode)
381{
382 int fd = open(path, (flags^O_CLOEXEC)&~WARN_ONLY, mode);
383
384 if (fd == -1) ((flags&WARN_ONLY) ? perror_msg_raw : perror_exit_raw)(path);
385 return fd;
386}
387
388
389int xopen_stdio(char *path, int flags)
390{
391 return xcreate_stdio(path, flags, 0);
392}
393
394void xpipe(int *pp)
395{
396 if (pipe(pp)) perror_exit("xpipe");
397}
398
399void xclose(int fd)
400{
401 if (close(fd)) perror_exit("xclose");
402}
403
404int xdup(int fd)
405{
406 if (fd != -1) {
407 fd = dup(fd);
408 if (fd == -1) perror_exit("xdup");
409 }
410 return fd;
411}
412
413
414
415int notstdio(int fd)
416{
417 if (fd<0) return fd;
418
419 while (fd<3) {
420 int fd2 = xdup(fd);
421
422 close(fd);
423 xopen_stdio("/dev/null", O_RDWR);
424 fd = fd2;
425 }
426
427 return fd;
428}
429
430void xrename(char *from, char *to)
431{
432 if (rename(from, to)) perror_exit("rename %s -> %s", from, to);
433}
434
435int xtempfile(char *name, char **tempname)
436{
437 int fd;
438
439 *tempname = xmprintf("%s%s", name, "XXXXXX");
440 if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
441
442 return fd;
443}
444
445
446int xcreate(char *path, int flags, int mode)
447{
448 return notstdio(xcreate_stdio(path, flags, mode));
449}
450
451
452int xopen(char *path, int flags)
453{
454 return notstdio(xopen_stdio(path, flags));
455}
456
457
458int openro(char *path, int flags)
459{
460 if (!strcmp(path, "-")) return 0;
461
462 return xopen(path, flags^WARN_ONLY);
463}
464
465
466int xopenro(char *path)
467{
468 return openro(path, O_RDONLY|WARN_ONLY);
469}
470
471FILE *xfdopen(int fd, char *mode)
472{
473 FILE *f = fdopen(fd, mode);
474
475 if (!f) perror_exit("xfdopen");
476
477 return f;
478}
479
480
481FILE *xfopen(char *path, char *mode)
482{
483 FILE *f = fopen(path, mode);
484 if (!f) perror_exit("No file %s", path);
485 return f;
486}
487
488
489size_t xread(int fd, void *buf, size_t len)
490{
491 ssize_t ret = read(fd, buf, len);
492 if (ret < 0) perror_exit("xread");
493
494 return ret;
495}
496
497void xreadall(int fd, void *buf, size_t len)
498{
499 if (len != readall(fd, buf, len)) perror_exit("xreadall");
500}
501
502
503
504
505
506void xwrite(int fd, void *buf, size_t len)
507{
508 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
509}
510
511
512
513off_t xlseek(int fd, off_t offset, int whence)
514{
515 offset = lseek(fd, offset, whence);
516 if (offset<0) perror_exit("lseek");
517
518 return offset;
519}
520
521char *xgetcwd(void)
522{
523 char *buf = getcwd(NULL, 0);
524 if (!buf) perror_exit("xgetcwd");
525
526 return buf;
527}
528
529void xstat(char *path, struct stat *st)
530{
531 if(stat(path, st)) perror_exit("Can't stat %s", path);
532}
533
534
535
536
537
538char *xabspath(char *path, int exact)
539{
540 struct string_list *todo, *done = 0;
541 int try = 9999, dirfd = open("/", O_PATH), missing = 0;
542 char *ret;
543
544
545 if (*path != '/') {
546 char *temp = xgetcwd();
547
548 splitpath(path, splitpath(temp, &todo));
549 free(temp);
550 } else splitpath(path, &todo);
551
552
553 while (todo) {
554 struct string_list *new = llist_pop(&todo), **tail;
555 ssize_t len;
556
557
558 if (!try--) {
559 errno = ELOOP;
560 goto error;
561 }
562
563
564 if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) {
565 int x = new->str[1];
566
567 free(new);
568 if (!x) continue;
569 if (done) free(llist_pop(&done));
570 len = 0;
571
572 if (missing) missing--;
573 else {
574 if (-1 == (x = openat(dirfd, "..", O_PATH))) goto error;
575 close(dirfd);
576 dirfd = x;
577 }
578 continue;
579 }
580
581
582 if (exact == -2 && !todo) len = 0;
583 else len = readlinkat(dirfd, new->str, libbuf, sizeof(libbuf));
584 if (len>4095) goto error;
585
586
587 if (len<1) {
588 int fd;
589
590 new->next = done;
591 done = new;
592 if (errno == EINVAL && !todo) break;
593 if (errno == ENOENT && exact<0) {
594 missing++;
595 continue;
596 }
597 if (errno != EINVAL && (exact || todo)) goto error;
598
599 fd = openat(dirfd, new->str, O_PATH);
600 if (fd == -1 && (exact || todo || errno != ENOENT)) goto error;
601 close(dirfd);
602 dirfd = fd;
603 continue;
604 }
605
606
607 libbuf[len] = 0;
608 if (*libbuf == '/') {
609 llist_traverse(done, free);
610 done=0;
611 close(dirfd);
612 dirfd = open("/", O_PATH);
613 }
614 free(new);
615
616
617 tail = splitpath(libbuf, &new);
618
619
620 if (new) {
621 *tail = todo;
622 todo = new;
623 }
624 }
625 close(dirfd);
626
627
628
629
630 try = 2;
631 while (done) {
632 struct string_list *temp = llist_pop(&done);
633
634 if (todo) try++;
635 try += strlen(temp->str);
636 temp->next = todo;
637 todo = temp;
638 }
639
640
641
642 ret = xmalloc(try);
643 *ret = '/';
644 ret [try = 1] = 0;
645 while (todo) {
646 if (try>1) ret[try++] = '/';
647 try = stpcpy(ret+try, todo->str) - ret;
648 free(llist_pop(&todo));
649 }
650
651 return ret;
652
653error:
654 close(dirfd);
655 llist_traverse(todo, free);
656 llist_traverse(done, free);
657
658 return 0;
659}
660
661void xchdir(char *path)
662{
663 if (chdir(path)) perror_exit("chdir '%s'", path);
664}
665
666void xchroot(char *path)
667{
668 if (chroot(path)) error_exit("chroot '%s'", path);
669 xchdir("/");
670}
671
672struct passwd *xgetpwuid(uid_t uid)
673{
674 struct passwd *pwd = getpwuid(uid);
675 if (!pwd) error_exit("bad uid %ld", (long)uid);
676 return pwd;
677}
678
679struct group *xgetgrgid(gid_t gid)
680{
681 struct group *group = getgrgid(gid);
682
683 if (!group) perror_exit("gid %ld", (long)gid);
684 return group;
685}
686
687unsigned xgetuid(char *name)
688{
689 struct passwd *up = getpwnam(name);
690 char *s = 0;
691 long uid;
692
693 if (up) return up->pw_uid;
694
695 uid = estrtol(name, &s, 10);
696 if (!errno && s && !*s && uid>=0 && uid<=UINT_MAX) return uid;
697
698 error_exit("bad user '%s'", name);
699}
700
701unsigned xgetgid(char *name)
702{
703 struct group *gr = getgrnam(name);
704 char *s = 0;
705 long gid;
706
707 if (gr) return gr->gr_gid;
708
709 gid = estrtol(name, &s, 10);
710 if (!errno && s && !*s && gid>=0 && gid<=UINT_MAX) return gid;
711
712 error_exit("bad group '%s'", name);
713}
714
715struct passwd *xgetpwnam(char *name)
716{
717 struct passwd *up = getpwnam(name);
718
719 if (!up) perror_exit("user '%s'", name);
720 return up;
721}
722
723struct group *xgetgrnam(char *name)
724{
725 struct group *gr = getgrnam(name);
726
727 if (!gr) perror_exit("group '%s'", name);
728 return gr;
729}
730
731
732
733
734void xsetuser(struct passwd *pwd)
735{
736 if (initgroups(pwd->pw_name, pwd->pw_gid) || setgid(pwd->pw_uid)
737 || setuid(pwd->pw_uid)) perror_exit("xsetuser '%s'", pwd->pw_name);
738}
739
740
741
742char *xreadlink(char *name)
743{
744 int len, size = 0;
745 char *buf = 0;
746
747
748 for(;;) {
749 size +=64;
750 buf = xrealloc(buf, size);
751 len = readlink(name, buf, size);
752
753 if (len<0) {
754 free(buf);
755 return 0;
756 }
757 if (len<size) {
758 buf[len]=0;
759 return buf;
760 }
761 }
762}
763
764char *xreadfile(char *name, char *buf, off_t len)
765{
766 if (!(buf = readfile(name, buf, len))) perror_exit("Bad '%s'", name);
767
768 return buf;
769}
770
771
772
773int xioctl(int fd, int request, void *data)
774{
775 int rc;
776
777 errno = 0;
778 rc = ioctl(fd, request, data);
779 if (rc == -1 && errno) perror_exit("ioctl %x", request);
780
781 return rc;
782}
783
784
785
786void xpidfile(char *name)
787{
788 char pidfile[256], spid[32];
789 int i, fd;
790 pid_t pid;
791
792 sprintf(pidfile, "/var/run/%s.pid", name);
793
794 for (i=0; i<3; i++) {
795 fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
796 if (fd != -1) break;
797
798
799 fd = open(pidfile, O_RDONLY);
800 if (fd == -1) continue;
801
802
803 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
804 close(fd);
805 pid = atoi(spid);
806 if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile);
807
808
809 }
810
811 if (i == 3) error_exit("xpidfile %s", name);
812
813 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
814 close(fd);
815}
816
817
818
819long long sendfile_len(int in, int out, long long bytes, long long *consumed)
820{
821 long long total = 0, len;
822
823 if (consumed) *consumed = 0;
824 if (in<0) return 0;
825 while (bytes != total) {
826 len = bytes-total;
827 if (bytes<0 || len>sizeof(libbuf)) len = sizeof(libbuf);
828
829 len = read(in, libbuf, len);
830 if (!len && errno==EAGAIN) continue;
831 if (len<1) break;
832 if (consumed) *consumed += len;
833 if (writeall(out, libbuf, len) != len) return -1;
834 total += len;
835 }
836
837 return total;
838}
839
840
841long long xsendfile_len(int in, int out, long long bytes)
842{
843 long long len = sendfile_len(in, out, bytes, 0);
844
845 if (bytes != -1 && bytes != len) {
846 if (out == 1 && len<0) xexit();
847 error_exit("short %s", (len<0) ? "write" : "read");
848 }
849
850 return len;
851}
852
853
854void xsendfile_pad(int in, int out, long long len)
855{
856 len -= xsendfile_len(in, out, len);
857 if (len) {
858 perror_msg("short read");
859 memset(libbuf, 0, sizeof(libbuf));
860 while (len) {
861 int i = len>sizeof(libbuf) ? sizeof(libbuf) : len;
862
863 xwrite(out, libbuf, i);
864 len -= i;
865 }
866 }
867}
868
869
870long long xsendfile(int in, int out)
871{
872 return xsendfile_len(in, out, -1);
873}
874
875double xstrtod(char *s)
876{
877 char *end;
878 double d;
879
880 errno = 0;
881 d = strtod(s, &end);
882 if (!errno && *end) errno = E2BIG;
883 if (errno) perror_exit("strtod %s", s);
884
885 return d;
886}
887
888
889long xparsetime(char *arg, long zeroes, long *fraction)
890{
891 long l, fr = 0, mask = 1;
892 char *end;
893
894 if (*arg != '.' && !isdigit(*arg)) error_exit("Not a number '%s'", arg);
895 l = strtoul(arg, &end, 10);
896 if (*end == '.') {
897 end++;
898 while (zeroes--) {
899 fr *= 10;
900 mask *= 10;
901 if (isdigit(*end)) fr += *end++-'0';
902 }
903 while (isdigit(*end)) end++;
904 }
905
906
907 if (*end) {
908 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *end);
909
910 if (i == -1 || *(end+1)) error_exit("Unknown suffix '%s'", end);
911 l *= ismhd[i];
912 fr *= ismhd[i];
913 l += fr/mask;
914 fr %= mask;
915 }
916 if (fraction) *fraction = fr;
917
918 return l;
919}
920
921long long xparsemillitime(char *arg)
922{
923 long l, ll;
924
925 l = xparsetime(arg, 3, &ll);
926
927 return (l*1000LL)+ll;
928}
929
930
931
932
933void xregcomp(regex_t *preg, char *regex, int cflags)
934{
935 int rc;
936
937
938
939 if (!*regex) {
940 regex = "()";
941 cflags |= REG_EXTENDED;
942 }
943
944 if ((rc = regcomp(preg, regex, cflags))) {
945 regerror(rc, preg, libbuf, sizeof(libbuf));
946 error_exit("bad regex: %s", libbuf);
947 }
948}
949
950char *xtzset(char *new)
951{
952 char *old = getenv("TZ");
953
954 if (old) old = xstrdup(old);
955 if (new ? setenv("TZ", new, 1) : unsetenv("TZ")) perror_exit("setenv");
956 tzset();
957
958 return old;
959}
960
961
962void xsignal_flags(int signal, void *handler, int flags)
963{
964 struct sigaction *sa = (void *)libbuf;
965
966 memset(sa, 0, sizeof(struct sigaction));
967 sa->sa_handler = handler;
968 sa->sa_flags = flags;
969
970 if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal);
971}
972
973void xsignal(int signal, void *handler)
974{
975 xsignal_flags(signal, handler, 0);
976}
977
978
979time_t xvali_date(struct tm *tm, char *str)
980{
981 time_t t;
982
983 if (tm && (unsigned)tm->tm_sec<=60 && (unsigned)tm->tm_min<=59
984 && (unsigned)tm->tm_hour<=23 && tm->tm_mday && (unsigned)tm->tm_mday<=31
985 && (unsigned)tm->tm_mon<=11 && (t = mktime(tm)) != -1) return t;
986
987 error_exit("bad date %s", str);
988}
989
990
991void xparsedate(char *str, time_t *t, unsigned *nano, int endian)
992{
993 struct tm tm;
994 time_t now = *t;
995 int len = 0, i = 0;
996
997
998 char *s = str, *p, *oldtz = 0, *formats[] = {"%Y-%m-%d %T", "%Y-%m-%dT%T",
999 "%H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d", "%H:%M", "%m%d%H%M",
1000 endian ? "%m%d%H%M%y" : "%y%m%d%H%M",
1001 endian ? "%m%d%H%M%C%y" : "%C%y%m%d%H%M"};
1002
1003 *nano = 0;
1004
1005
1006 if (*str == '@') {
1007 long long ll;
1008
1009
1010
1011 sscanf(s, "@%lld%n", &ll, &len);
1012 if (s[len]=='.') {
1013 s += len+1;
1014 for (len = 0; len<9; len++) {
1015 *nano *= 10;
1016 if (isdigit(*s)) *nano += *s++-'0';
1017 }
1018 }
1019 *t = ll;
1020 if (!s[len]) return;
1021 xvali_date(0, str);
1022 }
1023
1024
1025
1026 if ((i = strlen(str)) && toupper(str[i-1])=='Z') {
1027 str[--i] = 0;
1028 oldtz = getenv("TZ");
1029 if (oldtz) oldtz = xstrdup(oldtz);
1030 setenv("TZ", "UTC0", 1);
1031 }
1032
1033
1034 for (i = 0; i<ARRAY_LEN(formats); i++) {
1035 localtime_r(&now, &tm);
1036 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1037 tm.tm_isdst = -endian;
1038
1039 if ((p = strptime(s, formats[i], &tm))) {
1040 if (*p == '.') {
1041 p++;
1042
1043 if (i>2) {
1044 len = 0;
1045 sscanf(p, "%2u%n", &tm.tm_sec, &len);
1046 p += len;
1047 }
1048
1049 for (len = 0; len<9; len++) {
1050 *nano *= 10;
1051 if (isdigit(*p)) *nano += *p++-'0';
1052 }
1053 }
1054
1055 if (!*p) break;
1056 }
1057 }
1058
1059
1060 *t = xvali_date((i!=ARRAY_LEN(formats)) ? &tm : 0, str);
1061
1062 if (oldtz) setenv("TZ", oldtz, 1);
1063 free(oldtz);
1064}
1065
1066char *xgetline(FILE *fp, int *len)
1067{
1068 char *new = 0;
1069 size_t linelen = 0;
1070 long ll;
1071
1072 errno = 0;
1073 if (1>(ll = getline(&new, &linelen, fp))) {
1074 if (errno) perror_msg("getline");
1075 new = 0;
1076 } else if (new[linelen-1] == '\n') new[--linelen] = 0;
1077 if (len) *len = ll;
1078
1079 return new;
1080}
1081
1082time_t xmktime(struct tm *tm, int utc)
1083{
1084 char *old_tz = utc ? xtzset("UTC0") : 0;
1085 time_t result;
1086
1087 if ((result = mktime(tm)) < 0) error_exit("mktime");
1088 if (utc) {
1089 free(xtzset(old_tz));
1090 free(old_tz);
1091 }
1092 return result;
1093}
1094