19 #include "vis-single-payload.inc"
22 #define VIS_TMP "/tmp/.vis-single-XXXXXX"
26 #define VIS_TERMINFO "/etc/terminfo:/lib/terminfo:/usr/share/terminfo:" \
27 "/usr/lib/terminfo:/usr/local/share/terminfo:/usr/local/lib/terminfo"
30 static lzma_stream strm
= LZMA_STREAM_INIT
;
32 static int libtar_xzopen(const char *pathname
, int flags
, ...) {
33 int ret
= lzma_stream_decoder(&strm
, UINT64_MAX
, LZMA_TELL_UNSUPPORTED_CHECK
| LZMA_CONCATENATED
);
35 fprintf(stderr
, "lzma_stream_decoder error: %d\n", ret
);
39 strm
.next_in
= vis_single_payload
;
40 strm
.avail_in
= sizeof(vis_single_payload
);
45 static int libtar_xzclose(int fd
) {
50 static ssize_t
libtar_xzread(int fd
, void *buf
, size_t count
) {
52 strm
.avail_out
= count
;
54 int ret
= lzma_code(&strm
, LZMA_FINISH
);
55 if (ret
!= LZMA_OK
&& ret
!= LZMA_STREAM_END
) {
56 fprintf(stderr
, "lzma_code error: %d\n", ret
);
60 return count
- strm
.avail_out
;
69 int extract(char *directory
) {
72 if (tar_open(&tar
, NULL
, &xztype
, O_RDONLY
, 0, 0) == -1) {
77 if (tar_extract_all(tar
, directory
) != 0) {
78 perror("tar_extract_all");
82 if (tar_close(tar
) != 0) {
90 static int unlink_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
94 int main(int argc
, char **argv
) {
95 int rc
= EXIT_FAILURE
;
96 char exe
[256], path
[PATH_MAX
];
97 char tmp_dirname
[] = VIS_TMP
;
99 if (!mkdtemp(tmp_dirname
)) {
104 char *old_path
= getenv("PATH");
105 if (snprintf(path
, sizeof(path
), "%s%s%s", tmp_dirname
,
106 old_path
? ":" : "", old_path
? old_path
: "") < 0) {
110 if (setenv("PATH", path
, 1) == -1 ||
111 setenv("TERMINFO_DIRS", VIS_TERMINFO
, 0) == -1) {
116 if (extract(tmp_dirname
) != 0)
119 if (snprintf(exe
, sizeof(exe
), "%s/vis", tmp_dirname
) < 0)
122 int child_pid
= fork();
123 if (child_pid
== -1) {
126 } else if (child_pid
== 0) {
132 signal(SIGINT
, SIG_IGN
);
136 int w
= waitpid(child_pid
, &status
, 0);
141 if (w
== child_pid
) {
142 rc
= WEXITSTATUS(status
);
148 nftw(tmp_dirname
, unlink_cb
, 64, FTW_DEPTH
|FTW_PHYS
|FTW_MOUNT
);