archival: added O_TRUNC so that when we overwrite files on unpack,
          we truncate them. Also spotted & fixed hard to trigger bug
          with extension handling.

diff --git a/archival/bunzip2.c b/archival/bunzip2.c
index a970aeb..9810e02 100644
--- a/archival/bunzip2.c
+++ b/archival/bunzip2.c
@@ -41,17 +41,20 @@
 
 	if (filename) {
 		struct stat stat_buf;
-		char *extension=filename+strlen(filename)-4;
-		if (strcmp(extension, ".bz2") != 0) {
+		/* extension = filename+strlen(filename)-4 is buggy:
+		 * strlen may be less than 4 */
+		char *extension = strrchr(filename, '.');
+		if (!extension || strcmp(extension, ".bz2") != 0) {
 			bb_error_msg_and_die("Invalid extension");
 		}
 		xstat(filename, &stat_buf);
-		*extension=0;
-		dst_fd = xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
+		*extension = '\0';
+		dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
+				stat_buf.st_mode);
 	} else dst_fd = STDOUT_FILENO;
 	status = uncompressStream(src_fd, dst_fd);
 	if(filename) {
-		if (!status) filename[strlen(filename)]='.';
+		if (!status) filename[strlen(filename)] = '.';
 		if (unlink(filename) < 0) {
 			bb_error_msg_and_die("Couldn't remove %s", filename);
 		}
diff --git a/archival/gunzip.c b/archival/gunzip.c
index 3a1d1cb..7be94e1 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -98,7 +98,8 @@
 			}
 
 			/* Open output file (with correct permissions) */
-			dst_fd = xopen3(new_path, O_WRONLY | O_CREAT, stat_buf.st_mode);
+			dst_fd = xopen3(new_path, O_WRONLY | O_CREAT | O_TRUNC,
+					stat_buf.st_mode);
 
 			/* If unzip succeeds remove the old file */
 			delete_path = old_path;
diff --git a/archival/uncompress.c b/archival/uncompress.c
index ca775c7..8c466eb 100644
--- a/archival/uncompress.c
+++ b/archival/uncompress.c
@@ -55,7 +55,8 @@
 
 			/* Open output file */
 			xstat(compressed_file, &stat_buf);
-			dst_fd = xopen3(uncompressed_file, O_WRONLY | O_CREAT,
+			dst_fd = xopen3(uncompressed_file,
+					O_WRONLY | O_CREAT | O_TRUNC,
 					stat_buf.st_mode);
 
 			/* If unzip succeeds remove the old file */
diff --git a/archival/unlzma.c b/archival/unlzma.c
index fbd207c..00acea6 100644
--- a/archival/unlzma.c
+++ b/archival/unlzma.c
@@ -37,14 +37,15 @@
 
 	if (filename) {
 		struct stat stat_buf;
-		char *extension = filename + strlen(filename) - 5;
-
-		if (strcmp(extension, ".lzma") != 0) {
+		/* bug: char *extension = filename + strlen(filename) - 5; */
+		char *extension = strrchr(filename, '.');
+		if (!extension || strcmp(extension, ".lzma") != 0) {
 			bb_error_msg_and_die("Invalid extension");
 		}
 		xstat(filename, &stat_buf);
-		*extension = 0;
-		dst_fd = xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
+		*extension = '\0';
+		dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
+				stat_buf.st_mode);
 	} else
 		dst_fd = STDOUT_FILENO;
 	status = unlzma(src_fd, dst_fd);
diff --git a/archival/unzip.c b/archival/unzip.c
index 6dd1d35..709ebf8 100644
--- a/archival/unzip.c
+++ b/archival/unzip.c
@@ -333,7 +333,7 @@
 			overwrite = o_always;
 		case 'y': /* Open file and fall into unzip */
 			unzip_create_leading_dirs(dst_fn);
-			dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT, 0777);
+			dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0777);
 		case -1: /* Unzip */
 			if (verbosity == v_normal) {
 				printf("  inflating: %s\n", dst_fn);