2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-20 23:58:50 +05:30
|
|
|
/*
|
|
|
|
* Small lzma deflate implementation.
|
|
|
|
* Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
|
|
|
|
*
|
|
|
|
* Based on bunzip.c from busybox
|
|
|
|
*
|
|
|
|
* Licensed under GPL v2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2006-10-01 21:25:11 +05:30
|
|
|
/* Why our g[un]zip/bunzip2 are so ugly compared to this beauty? */
|
|
|
|
|
2006-01-20 23:58:50 +05:30
|
|
|
#include "busybox.h"
|
|
|
|
#include "unarchive.h"
|
|
|
|
|
|
|
|
#define UNLZMA_OPT_STDOUT 1
|
|
|
|
|
|
|
|
int unlzma_main(int argc, char **argv)
|
|
|
|
{
|
2006-10-01 21:25:11 +05:30
|
|
|
USE_DESKTOP(long long) int status;
|
2006-01-20 23:58:50 +05:30
|
|
|
char *filename;
|
2006-10-04 02:30:06 +05:30
|
|
|
unsigned opt;
|
2006-10-01 21:25:11 +05:30
|
|
|
int src_fd, dst_fd;
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2006-10-04 02:30:06 +05:30
|
|
|
opt = getopt32(argc, argv, "c");
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
/* Set input filename and number */
|
|
|
|
filename = argv[optind];
|
2006-10-01 21:25:11 +05:30
|
|
|
if (filename && (filename[0] != '-') && (filename[1] != '\0')) {
|
2006-01-20 23:58:50 +05:30
|
|
|
/* Open input file */
|
2006-08-03 21:11:12 +05:30
|
|
|
src_fd = xopen(filename, O_RDONLY);
|
2006-01-20 23:58:50 +05:30
|
|
|
} else {
|
|
|
|
src_fd = STDIN_FILENO;
|
|
|
|
filename = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if called as lzmacat force the stdout flag */
|
2006-10-04 02:30:43 +05:30
|
|
|
if ((opt & UNLZMA_OPT_STDOUT) || applet_name[4] == 'c')
|
2006-01-20 23:58:50 +05:30
|
|
|
filename = 0;
|
|
|
|
|
|
|
|
if (filename) {
|
2006-06-13 20:24:42 +05:30
|
|
|
struct stat stat_buf;
|
2006-09-03 19:53:29 +05:30
|
|
|
/* bug: char *extension = filename + strlen(filename) - 5; */
|
|
|
|
char *extension = strrchr(filename, '.');
|
|
|
|
if (!extension || strcmp(extension, ".lzma") != 0) {
|
2006-09-30 03:00:43 +05:30
|
|
|
bb_error_msg_and_die("invalid extension");
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
2006-06-13 21:39:16 +05:30
|
|
|
xstat(filename, &stat_buf);
|
2006-09-03 19:53:29 +05:30
|
|
|
*extension = '\0';
|
|
|
|
dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
stat_buf.st_mode);
|
2006-01-20 23:58:50 +05:30
|
|
|
} else
|
|
|
|
dst_fd = STDOUT_FILENO;
|
|
|
|
status = unlzma(src_fd, dst_fd);
|
|
|
|
if (filename) {
|
2006-10-01 21:25:11 +05:30
|
|
|
if (status >= 0) /* if success delete src, else delete dst */
|
2006-01-20 23:58:50 +05:30
|
|
|
filename[strlen(filename)] = '.';
|
|
|
|
if (unlink(filename) < 0) {
|
2006-09-30 03:00:43 +05:30
|
|
|
bb_error_msg_and_die("cannot remove %s", filename);
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-01 21:25:11 +05:30
|
|
|
return (status < 0);
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|