Hi,

please find attached a few patches for opensc-explorer.c

* 0001-opensc-explorer-support-writing-to-stdout-in-get.patch
  new feature: use filename '-' as an indicator tow write to stdout in 'get'

* 0002-opensc-explorer-re-factor-print_file.patch
  re-factor: simplify code

* 0003-opensc-explorer-re-factor-do_cat.patch
  re-factor: bring logocal parts closer to each other

* 0004-opensc-explorer-use-relative-addressing-in-do_cat.patch
  bug fix (at least I think it is one):
  according to the usage message 'cat' uses file ids, just as e.g. 'get'
  IMHO it should then use relative addressing instead of absolute
  addressing.
  This change made 'cat' useful with my OpenPGP cards
  (before it baled out crying "unable to select file: Invalid arguments")

It would be cool if the changes made it into OpenSC mainline

Best
Peter
-- 
Peter Marschall
pe...@adpm.de
From abe0fd986c9e3c47c05464c9453ca229cab74c4a Mon Sep 17 00:00:00 2001
From: Peter Marschall <pe...@adpm.de>
Date: Sat, 19 Mar 2011 14:56:44 +0100
Subject: [PATCH 1/4] opensc-explorer: support writing to stdout in 'get'

Treat the value '-' of the second parameter to get special.
If it is given, use stdout as the file to write to.

Signed-off-by: Peter Marschall <pe...@adpm.de>
---
 src/tools/opensc-explorer.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c
index d8b2beb..7cc7d9a 100644
--- a/src/tools/opensc-explorer.c
+++ b/src/tools/opensc-explorer.c
@@ -903,7 +903,9 @@ static int do_get(int argc, char **argv)
 		fbuf[5*i-1] = 0;
 		filename = fbuf;
 	}
-	outf = fopen(filename, "wb");
+	outf = (strcmp(filename, "-") == 0)
+		? stdout
+		: fopen(filename, "wb");
 	if (outf == NULL) {
 		perror(filename);
 		goto err;
@@ -936,14 +938,19 @@ static int do_get(int argc, char **argv)
 		idx += r;
 		count -= r;
 	}
-	printf("Total of %d bytes read from %s and saved to %s.\n",
-	       idx, argv[0], filename);
+	if (outf == stdout) {
+		fwrite("\n", 1, 1, outf);
+	}
+	else {
+		printf("Total of %d bytes read from %s and saved to %s.\n",
+		       idx, argv[0], filename);
+	}
 	
 	err = 0;
 err:
 	if (file)
 		sc_file_free(file);
-	if (outf)
+	if (outf != NULL && outf != stdout)
 		fclose(outf);
 	select_current_path_or_die();
 	return -err;
-- 
1.7.4.1

From a2446ed4ce6d302466375b7618f30c80d0342ba5 Mon Sep 17 00:00:00 2001
From: Peter Marschall <pe...@adpm.de>
Date: Sat, 19 Mar 2011 16:13:47 +0100
Subject: [PATCH 2/4] opensc-explorer: re-factor print_file()


Signed-off-by: Peter Marschall <pe...@adpm.de>
---
 src/tools/opensc-explorer.c |   19 +++++--------------
 1 files changed, 5 insertions(+), 14 deletions(-)

diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c
index 7cc7d9a..2c6fb4e 100644
--- a/src/tools/opensc-explorer.c
+++ b/src/tools/opensc-explorer.c
@@ -164,17 +164,9 @@ static int arg_to_path(const char *arg, sc_path_t *path, int is_id)
 
 static void print_file(const sc_file_t *file)
 {
-	const char *st;
+	const char *format = " %02X%02X ";
+	const char *st = "???";
 
-	if (file->type == SC_FILE_TYPE_DF)
-		printf("[");
-	else
-		printf(" ");
-	printf("%02X%02X", file->id >> 8, file->id & 0xFF);
-	if (file->type == SC_FILE_TYPE_DF)
-		printf("]");
-	else
-		printf(" ");
 	switch (file->type) {
 	case SC_FILE_TYPE_WORKING_EF:
 		st = "wEF";
@@ -183,12 +175,11 @@ static void print_file(const sc_file_t *file)
 		st = "iEF";
 		break;
 	case SC_FILE_TYPE_DF:
+		format = "[%02X%02X]";
 		st = "DF";
 		break;
-	default:
-		st = "???";
-		break;
 	}
+	printf(format, file->id >> 8, file->id & 0xFF);
 	printf("\t%4s", st);
 	printf(" %5lu", (unsigned long)file->size);
 	if (file->namelen) {
-- 
1.7.4.1

From 61dbed8084fe3aa920e31da33a2b3e919507fd1c Mon Sep 17 00:00:00 2001
From: Peter Marschall <pe...@adpm.de>
Date: Sat, 19 Mar 2011 17:51:20 +0100
Subject: [PATCH 3/4] opensc-explorer: re-factor do_cat()

* use strlen(..) instead of sizeof(..)-1
  - easier to read
  - avoids errors if string is defined with explicit size
* bring sfi:-related code closer together

Signed-off-by: Peter Marschall <pe...@adpm.de>
---
 src/tools/opensc-explorer.c |   25 +++++++++++++------------
 1 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c
index 2c6fb4e..636856d 100644
--- a/src/tools/opensc-explorer.c
+++ b/src/tools/opensc-explorer.c
@@ -342,7 +342,6 @@ static int do_cat(int argc, char **argv)
 	sc_file_t *file = NULL;
 	int not_current = 1;
 	int sfi = 0;
-	const char sfi_prefix[] = "sfi:";
 
 	if (argc > 1)
 		goto usage;
@@ -351,17 +350,10 @@ static int do_cat(int argc, char **argv)
 		file = current_file;
 		not_current = 0;
 	} else {
-		if (strncmp(argv[0], sfi_prefix, sizeof(sfi_prefix)-1)) {
-			if (arg_to_path(argv[0], &path, 1) != 0)
-				goto usage;
-			r = sc_select_file(card, &path, &file);
-			if (r) {
-				check_ret(r, SC_AC_OP_SELECT, "unable to select file",
-					current_file);
-				goto err;
-			}
-		} else {
-			const char *sfi_n = &argv[0][sizeof(sfi_prefix)-1];
+		const char sfi_prefix[] = "sfi:";
+
+		if (strncasecmp(argv[0], sfi_prefix, strlen(sfi_prefix)) == 0) {
+			const char *sfi_n = argv[0] + strlen(sfi_prefix);
 
 			if(!current_file) {
 				printf("A DF must be selected to read by SFI\n");
@@ -375,6 +367,15 @@ static int do_cat(int argc, char **argv)
 				printf("Invalid SFI: %s\n", sfi_n);
 				goto usage;
 			}
+		} else {
+			if (arg_to_path(argv[0], &path, 1) != 0)
+				goto usage;
+			r = sc_select_file(card, &path, &file);
+			if (r) {
+				check_ret(r, SC_AC_OP_SELECT, "unable to select file",
+					current_file);
+				goto err;
+			}
 		}
 	}
 	if (file->type != SC_FILE_TYPE_WORKING_EF &&
-- 
1.7.4.1

From 2bea34f41554a3fc4223371de3f479138f015f6f Mon Sep 17 00:00:00 2001
From: Peter Marschall <pe...@adpm.de>
Date: Sat, 19 Mar 2011 17:59:37 +0100
Subject: [PATCH 4/4] opensc-explorer: use relative addressing in do_cat

'cat' takes a file_id as argument. This should be selected relative
to the currently selected DF instead of being treated as an ID.
(compare with 'get' that also takes a file_id argument)

Signed-off-by: Peter Marschall <pe...@adpm.de>
---
 src/tools/opensc-explorer.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c
index 636856d..b5ab2b5 100644
--- a/src/tools/opensc-explorer.c
+++ b/src/tools/opensc-explorer.c
@@ -368,7 +368,7 @@ static int do_cat(int argc, char **argv)
 				goto usage;
 			}
 		} else {
-			if (arg_to_path(argv[0], &path, 1) != 0)
+			if (arg_to_path(argv[0], &path, 0) != 0)
 				goto usage;
 			r = sc_select_file(card, &path, &file);
 			if (r) {
-- 
1.7.4.1

_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to