blob: 46fbefdc0884748ef28747b6dde821fb6389938f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landleyc1d69902006-01-20 18:28:50 +00002/*
3 * Small lzma deflate implementation.
4 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 *
6 * Based on bunzip.c from busybox
7 *
8 * Licensed under GPL v2, see file LICENSE in this tarball for details.
9 */
10
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000011/* Why our g[un]zip/bunzip2 are so ugly compared to this beauty? */
12
Rob Landleyc1d69902006-01-20 18:28:50 +000013#include "busybox.h"
14#include "unarchive.h"
15
16#define UNLZMA_OPT_STDOUT 1
17
18int unlzma_main(int argc, char **argv)
19{
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000020 USE_DESKTOP(long long) int status;
Rob Landleyc1d69902006-01-20 18:28:50 +000021 char *filename;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000022 unsigned opt;
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000023 int src_fd, dst_fd;
Rob Landleyc1d69902006-01-20 18:28:50 +000024
Denis Vlasenko67b23e62006-10-03 21:00:06 +000025 opt = getopt32(argc, argv, "c");
Rob Landleyc1d69902006-01-20 18:28:50 +000026
27 /* Set input filename and number */
28 filename = argv[optind];
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000029 if (filename && (filename[0] != '-') && (filename[1] != '\0')) {
Rob Landleyc1d69902006-01-20 18:28:50 +000030 /* Open input file */
Rob Landleyd921b2e2006-08-03 15:41:12 +000031 src_fd = xopen(filename, O_RDONLY);
Rob Landleyc1d69902006-01-20 18:28:50 +000032 } else {
33 src_fd = STDIN_FILENO;
34 filename = 0;
35 }
36
37 /* if called as lzmacat force the stdout flag */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000038 if ((opt & UNLZMA_OPT_STDOUT) || applet_name[4] == 'c')
Rob Landleyc1d69902006-01-20 18:28:50 +000039 filename = 0;
40
41 if (filename) {
Rob Landley9a202c92006-06-13 14:54:42 +000042 struct stat stat_buf;
Denis Vlasenko22dca232006-09-03 14:23:29 +000043 /* bug: char *extension = filename + strlen(filename) - 5; */
44 char *extension = strrchr(filename, '.');
45 if (!extension || strcmp(extension, ".lzma") != 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +000046 bb_error_msg_and_die("invalid extension");
Rob Landleyc1d69902006-01-20 18:28:50 +000047 }
Rob Landleyc4b67392006-06-13 16:09:16 +000048 xstat(filename, &stat_buf);
Denis Vlasenko22dca232006-09-03 14:23:29 +000049 *extension = '\0';
50 dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
51 stat_buf.st_mode);
Rob Landleyc1d69902006-01-20 18:28:50 +000052 } else
53 dst_fd = STDOUT_FILENO;
54 status = unlzma(src_fd, dst_fd);
55 if (filename) {
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000056 if (status >= 0) /* if success delete src, else delete dst */
Rob Landleyc1d69902006-01-20 18:28:50 +000057 filename[strlen(filename)] = '.';
58 if (unlink(filename) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +000059 bb_error_msg_and_die("cannot remove %s", filename);
Rob Landleyc1d69902006-01-20 18:28:50 +000060 }
61 }
62
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000063 return (status < 0);
Rob Landleyc1d69902006-01-20 18:28:50 +000064}