opensc-explorer: re-factor do_ls()
* replace magic magic number used as potentially too small buffer size by SC_MAX_EXT_APDU_RESP_SIZE * replace magic number for filename by SC_MAX_PATH_STRING_SIZE * start error messages with upper-case letters * use braces after sizeof, i.e. sizeof(X) instead of sizeof X * fix indentation
This commit is contained in:
parent
f1d46401cb
commit
b77e0faadb
|
@ -488,24 +488,22 @@ static int pattern_match(const char *pattern, const char *string)
|
||||||
|
|
||||||
static int do_ls(int argc, char **argv)
|
static int do_ls(int argc, char **argv)
|
||||||
{
|
{
|
||||||
u8 buf[256], *cur = buf;
|
u8 buf[SC_MAX_EXT_APDU_RESP_SIZE], *cur = buf;
|
||||||
int r, count;
|
int r, count;
|
||||||
|
|
||||||
memset(buf, 0, sizeof buf);
|
memset(buf, 0, sizeof(buf));
|
||||||
r = sc_lock(card);
|
r = sc_lock(card);
|
||||||
if (r == SC_SUCCESS)
|
if (r == SC_SUCCESS)
|
||||||
r = sc_list_files(card, buf, sizeof(buf));
|
r = sc_list_files(card, buf, sizeof(buf));
|
||||||
sc_unlock(card);
|
sc_unlock(card);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
check_ret(r, SC_AC_OP_LIST_FILES, "unable to receive file listing", current_file);
|
check_ret(r, SC_AC_OP_LIST_FILES, "Unable to receive file listing", current_file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
count = r;
|
count = r;
|
||||||
printf("FileID\tType Size\n");
|
printf("FileID\tType Size\n");
|
||||||
while (count >= 2) {
|
while (count >= 2) {
|
||||||
sc_path_t path;
|
char filename[SC_MAX_PATH_STRING_SIZE];
|
||||||
sc_file_t *file = NULL;
|
|
||||||
char filename[10];
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int matches = 0;
|
int matches = 0;
|
||||||
|
|
||||||
|
@ -522,12 +520,15 @@ static int do_ls(int argc, char **argv)
|
||||||
|
|
||||||
/* if any filename pattern were given, filter only matching file names */
|
/* if any filename pattern were given, filter only matching file names */
|
||||||
if (argc == 0 || matches) {
|
if (argc == 0 || matches) {
|
||||||
|
sc_path_t path;
|
||||||
|
sc_file_t *file = NULL;
|
||||||
|
|
||||||
if (current_path.type != SC_PATH_TYPE_DF_NAME) {
|
if (current_path.type != SC_PATH_TYPE_DF_NAME) {
|
||||||
path = current_path;
|
path = current_path;
|
||||||
sc_append_path_id(&path, cur, 2);
|
sc_append_path_id(&path, cur, 2);
|
||||||
} else {
|
} else {
|
||||||
if (sc_path_set(&path, SC_PATH_TYPE_FILE_ID, cur, 2, 0, 0) != SC_SUCCESS) {
|
if (sc_path_set(&path, SC_PATH_TYPE_FILE_ID, cur, 2, 0, 0) != SC_SUCCESS) {
|
||||||
fprintf(stderr, "unable to set path.\n");
|
fprintf(stderr, "Unable to set path.\n");
|
||||||
die(1);
|
die(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -537,7 +538,8 @@ static int do_ls(int argc, char **argv)
|
||||||
r = sc_select_file(card, &path, &file);
|
r = sc_select_file(card, &path, &file);
|
||||||
sc_unlock(card);
|
sc_unlock(card);
|
||||||
if (r) {
|
if (r) {
|
||||||
fprintf(stderr, " %02X%02X unable to select file, %s\n", cur[0], cur[1], sc_strerror(r));
|
fprintf(stderr, "Unable to select file %02X%02X: %s\n",
|
||||||
|
cur[0], cur[1], sc_strerror(r));
|
||||||
} else {
|
} else {
|
||||||
file->id = (cur[0] << 8) | cur[1];
|
file->id = (cur[0] << 8) | cur[1];
|
||||||
print_file(file);
|
print_file(file);
|
||||||
|
|
Loading…
Reference in New Issue