blob: dc85cb25ebc6818e77331cec4b1f8d584146e3ca [file] [log] [blame]
Rob Landleyc1d69902006-01-20 18:28:50 +00001/*
2 * Small lzma deflate implementation.
3 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
4 *
5 * Based on bunzip.c from busybox
6 *
7 * Licensed under GPL v2, see file LICENSE in this tarball for details.
8 */
9
10#include <fcntl.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15
16#include "busybox.h"
17#include "unarchive.h"
18
19#define UNLZMA_OPT_STDOUT 1
20
21int unlzma_main(int argc, char **argv)
22{
23 char *filename;
24 unsigned long opt;
25 int status, src_fd, dst_fd;
26
27 opt = bb_getopt_ulflags(argc, argv, "c");
28
29 /* Set input filename and number */
30 filename = argv[optind];
31 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
32 /* Open input file */
33 src_fd = bb_xopen(filename, O_RDONLY);
34 } else {
35 src_fd = STDIN_FILENO;
36 filename = 0;
37 }
38
39 /* if called as lzmacat force the stdout flag */
40 if ((opt & UNLZMA_OPT_STDOUT) || bb_applet_name[4] == 'c')
41 filename = 0;
42
43 if (filename) {
44 char *extension = filename + strlen(filename) - 5;
45
46 if (strcmp(extension, ".lzma") != 0) {
47 bb_error_msg_and_die("Invalid extension");
48 }
49 *extension = 0;
50 dst_fd = bb_xopen(filename, O_WRONLY | O_CREAT);
51 } else
52 dst_fd = STDOUT_FILENO;
53 status = unlzma(src_fd, dst_fd);
54 if (filename) {
55 if (!status)
56 filename[strlen(filename)] = '.';
57 if (unlink(filename) < 0) {
58 bb_error_msg_and_die("Couldn't remove %s", filename);
59 }
60 }
61
62 return status;
63}
64
65/* vi:set ts=4: */