changeset 427:45df37e81741

Add option to ignore paths when extracting or adding files with lwar Add option to lwar to strip path names from filenames when objects are added to an archive. Also strip path names from objects when extracting them, if present.
author David Flamand <dlflamand@gmail.com>
date Tue, 15 Nov 2016 21:43:33 -0700
parents b4825b42c151
children c5841e2c745d
files lwar/add.c lwar/extract.c lwar/lwar.c lwar/lwar.h lwar/main.c lwar/replace.c
diffstat 6 files changed, 42 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/lwar/add.c	Thu Nov 03 21:44:32 2016 -0600
+++ b/lwar/add.c	Tue Nov 15 21:43:33 2016 -0700
@@ -175,7 +175,7 @@
 		fseek(f2, 0, SEEK_END);
 		l = ftell(f2);
 		fseek(f2, 0, SEEK_SET);
-		fputs(files[i], f);
+		fputs(get_file_name(files[i]), f);
 		fputc(0, f);
 		fputc(l >> 24, f);
 		fputc((l >> 16) & 0xff, f);
--- a/lwar/extract.c	Thu Nov 03 21:44:32 2016 -0600
+++ b/lwar/extract.c	Tue Nov 15 21:43:33 2016 -0700
@@ -30,6 +30,7 @@
 {
 	FILE *f;
 	char buf[8];
+	char *filename;
 	long l;
 	int c;
 	char fnbuf[1024];
@@ -78,6 +79,7 @@
 			}
 		}
 		fnbuf[i] = 0;
+		filename = get_file_name(fnbuf);
 		
 		// get length of archive member
 		l = 0;
@@ -92,16 +94,16 @@
 		
 		for (i = 0; i < nfiles; i++)
 		{
-			if (!strcmp(files[i], fnbuf))
+			if (!strcmp(get_file_name(files[i]), filename))
 				break;
 		}
 		if (i < nfiles || nfiles == 0)
 		{
 			// extract the file
-			nf = fopen(fnbuf, "wb");
+			nf = fopen(filename, "wb");
 			if (!nf)
 			{
-				fprintf(stderr, "Cannot extract '%s': %s\n", fnbuf, strerror(errno));
+				fprintf(stderr, "Cannot extract '%s': %s\n", filename, strerror(errno));
 				exit(1);
 			}
 			while (l)
--- a/lwar/lwar.c	Thu Nov 03 21:44:32 2016 -0600
+++ b/lwar/lwar.c	Tue Nov 15 21:43:33 2016 -0700
@@ -24,6 +24,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -42,6 +43,7 @@
 int nfiles = 0;
 char *archive_file = NULL;
 int mergeflag = 0;
+int filename_flag = 0;
 
 char **files = NULL;
 
@@ -51,3 +53,19 @@
 	files[nfiles] = fn;
 	nfiles++;
 }
+
+char *get_file_name(char *fn)
+{
+	char *filename;
+	if (filename_flag != 0)
+	{
+#ifdef _MSC_VER
+		filename = strrchr(fn, '\\');
+#else
+		filename = strrchr(fn, '/');
+#endif
+		if (filename != NULL)
+			return filename + 1;
+	}
+	return fn;
+}
--- a/lwar/lwar.h	Thu Nov 03 21:44:32 2016 -0600
+++ b/lwar/lwar.h	Tue Nov 15 21:43:33 2016 -0700
@@ -39,6 +39,7 @@
 extern int nfiles;
 extern char **files;
 extern int mergeflag;
+extern int filename_flag;
 
 //typedef void * ARHANDLE;
 
@@ -55,6 +56,8 @@
 
 __lwar_E__ void add_file_name(char *fn);
 
+__lwar_E__ char *get_file_name(char *fn);
+
 //__lwar_E__ ARHANDLE open_archive(char *fn, int mode);
 
 #undef __lwar_E__
--- a/lwar/main.c	Thu Nov 03 21:44:32 2016 -0600
+++ b/lwar/main.c	Tue Nov 15 21:43:33 2016 -0700
@@ -52,6 +52,11 @@
 		debug_level++;
 		break;
 	
+	case 'f':
+		// filename only, no path
+		filename_flag++;
+		break;
+	
 	case 'a':
 		// add members
 		operation = LWAR_OP_ADD;
@@ -111,6 +116,8 @@
 				"Create new archive (or truncate existing one)" },
 	{ "merge",		'm',	0,		0,
 				"Add the contents of archive arguments instead of the archives themselves" },
+	{ "filename",	'f',	0,		0,
+				"Prevent the path from being archived" },
 	{ "debug",		'd',	0,		0,
 				"Set debug mode"},
 	{ 0 }
--- a/lwar/replace.c	Thu Nov 03 21:44:32 2016 -0600
+++ b/lwar/replace.c	Tue Nov 15 21:43:33 2016 -0700
@@ -37,6 +37,7 @@
 	FILE *f;
 	FILE *nf;
 	unsigned char buf[8];
+	char *filename;
 	long l;
 	int c;
 	FILE *f2;
@@ -106,6 +107,7 @@
 			}
 		}
 		fnbuf2[i] = 0;
+		filename = get_file_name(fnbuf2);
 		
 		// get length of archive member
 		l = 0;
@@ -121,7 +123,7 @@
 		// is it a file we are replacing? if so, do not copy it
 		for (i = 0; i < nfiles; i++)
 		{
-			if (!strcmp(files[i], fnbuf2))
+			if (!strcmp(get_file_name(files[i]), filename))
 				break;
 		}
 		if (i < nfiles)
@@ -131,7 +133,7 @@
 		else
 		{
 			// otherwise, copy it
-			fprintf(nf, "%s", fnbuf2);
+			fprintf(nf, "%s", filename);
 			fputc(0, nf);
 			fputc(l >> 24, nf);
 			fputc((l >> 16) & 0xff, nf);
@@ -151,10 +153,11 @@
 doadd:
 	for (i = 0; i < nfiles; i++)
 	{
-		f2 = fopen(files[i], "rb");
+		filename = get_file_name(files[i]);
+		f2 = fopen(filename, "rb");
 		if (!f2)
 		{
-			fprintf(stderr, "Cannot open file %s:", files[i]);
+			fprintf(stderr, "Cannot open file %s:", filename);
 			perror("");
 			exit(1);
 		}
@@ -220,7 +223,7 @@
 		fseek(f2, 0, SEEK_END);
 		l = ftell(f2);
 		fseek(f2, 0, SEEK_SET);
-		fputs(files[i], nf);
+		fputs(filename, nf);
 		fputc(0, nf);
 		fputc(l >> 24, nf);
 		fputc((l >> 16) & 0xff, nf);