blob: b4881af7537b163ce33623eddc07d1a4a2a10706 [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) {
Rob Landley9a202c92006-06-13 14:54:42 +000044 struct stat stat_buf;
Rob Landleyc1d69902006-01-20 18:28:50 +000045 char *extension = filename + strlen(filename) - 5;
46
47 if (strcmp(extension, ".lzma") != 0) {
48 bb_error_msg_and_die("Invalid extension");
49 }
Rob Landleyc4b67392006-06-13 16:09:16 +000050 xstat(filename, &stat_buf);
Rob Landleyc1d69902006-01-20 18:28:50 +000051 *extension = 0;
Rob Landley9a202c92006-06-13 14:54:42 +000052 dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
Rob Landleyc1d69902006-01-20 18:28:50 +000053 } else
54 dst_fd = STDOUT_FILENO;
55 status = unlzma(src_fd, dst_fd);
56 if (filename) {
57 if (!status)
58 filename[strlen(filename)] = '.';
59 if (unlink(filename) < 0) {
60 bb_error_msg_and_die("Couldn't remove %s", filename);
61 }
62 }
63
64 return status;
65}
66
67/* vi:set ts=4: */