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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "busybox.h"
|
|
|
|
#include "unarchive.h"
|
|
|
|
|
|
|
|
#define UNLZMA_OPT_STDOUT 1
|
|
|
|
|
|
|
|
int unlzma_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
unsigned long opt;
|
|
|
|
int status, src_fd, dst_fd;
|
|
|
|
|
|
|
|
opt = bb_getopt_ulflags(argc, argv, "c");
|
|
|
|
|
|
|
|
/* Set input filename and number */
|
|
|
|
filename = argv[optind];
|
|
|
|
if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
|
|
|
|
/* Open input file */
|
|
|
|
src_fd = bb_xopen(filename, O_RDONLY);
|
|
|
|
} else {
|
|
|
|
src_fd = STDIN_FILENO;
|
|
|
|
filename = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if called as lzmacat force the stdout flag */
|
|
|
|
if ((opt & UNLZMA_OPT_STDOUT) || bb_applet_name[4] == 'c')
|
|
|
|
filename = 0;
|
|
|
|
|
|
|
|
if (filename) {
|
2006-06-13 20:24:42 +05:30
|
|
|
struct stat stat_buf;
|
2006-01-20 23:58:50 +05:30
|
|
|
char *extension = filename + strlen(filename) - 5;
|
|
|
|
|
|
|
|
if (strcmp(extension, ".lzma") != 0) {
|
|
|
|
bb_error_msg_and_die("Invalid extension");
|
|
|
|
}
|
2006-06-13 21:39:16 +05:30
|
|
|
xstat(filename, &stat_buf);
|
2006-01-20 23:58:50 +05:30
|
|
|
*extension = 0;
|
2006-06-13 20:24:42 +05:30
|
|
|
dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, 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) {
|
|
|
|
if (!status)
|
|
|
|
filename[strlen(filename)] = '.';
|
|
|
|
if (unlink(filename) < 0) {
|
|
|
|
bb_error_msg_and_die("Couldn't remove %s", filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|