20 #include "vis-single-payload.inc"
23 #define VIS_TMP "/tmp/.vis-single-XXXXXX"
27 #define VIS_TERMINFO "/etc/terminfo:/lib/terminfo:/usr/share/terminfo:" \
28 "/usr/lib/terminfo:/usr/local/share/terminfo:/usr/local/lib/terminfo"
31 static lzma_stream strm
= LZMA_STREAM_INIT
;
33 static int libtar_xzopen(const char *pathname
, int flags
, ...) {
34 int ret
= lzma_stream_decoder(&strm
, UINT64_MAX
, LZMA_TELL_UNSUPPORTED_CHECK
| LZMA_CONCATENATED
);
36 fprintf(stderr
, "lzma_stream_decoder error: %d\n", ret
);
40 strm
.next_in
= vis_single_payload
;
41 strm
.avail_in
= sizeof(vis_single_payload
);
46 static int libtar_xzclose(int fd
) {
51 static ssize_t
libtar_xzread(int fd
, void *buf
, size_t count
) {
53 strm
.avail_out
= count
;
55 int ret
= lzma_code(&strm
, LZMA_FINISH
);
56 if (ret
!= LZMA_OK
&& ret
!= LZMA_STREAM_END
) {
57 fprintf(stderr
, "lzma_code error: %d\n", ret
);
61 return count
- strm
.avail_out
;
70 int extract(char *directory
) {
73 if (tar_open(&tar
, NULL
, &xztype
, O_RDONLY
, 0, 0) == -1) {
78 if (tar_extract_all(tar
, directory
) != 0) {
79 perror("tar_extract_all");
83 if (tar_close(tar
) != 0) {
91 static int unlink_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
95 int main(int argc
, char **argv
) {
96 int rc
= EXIT_FAILURE
;
97 char exe
[256], path
[PATH_MAX
];
98 char tmp_dirname
[] = VIS_TMP
;
100 if (!mkdtemp(tmp_dirname
)) {
105 char *old_path
= getenv("PATH");
106 if (snprintf(path
, sizeof(path
), "%s%s%s", tmp_dirname
,
107 old_path
? ":" : "", old_path
? old_path
: "") < 0) {
111 if (setenv("PATH", path
, 1) == -1 ||
112 setenv("TERMINFO_DIRS", VIS_TERMINFO
, 0) == -1) {
117 if (extract(tmp_dirname
) != 0)
120 if (snprintf(exe
, sizeof(exe
), "%s/vis", tmp_dirname
) < 0)
123 int child_pid
= fork();
124 if (child_pid
== -1) {
127 } else if (child_pid
== 0) {
133 signal(SIGINT
, SIG_IGN
);
137 int w
= waitpid(child_pid
, &status
, 0);
142 if (w
== child_pid
) {
143 rc
= WEXITSTATUS(status
);
149 nftw(tmp_dirname
, unlink_cb
, 64, FTW_DEPTH
|FTW_PHYS
|FTW_MOUNT
);