changeset 129:94d87d05f6c5

libcommon: initial support for file search
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Feb 2021 21:00:24 +0000
parents 01aed8d0685a
children 9c10afbb745a
files libcommon/Makefile libcommon/filesearch.c libcommon/hexread.c
diffstat 3 files changed, 33 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/Makefile	Sat Feb 20 20:10:18 2021 +0000
+++ b/libcommon/Makefile	Sat Feb 20 21:00:24 2021 +0000
@@ -2,9 +2,9 @@
 CFLAGS=	-O2 -I/usr/include/PCSC
 OBJS=	alpha_decode.o alpha_fromfile.o alpha_valid.o apdu.o apducmd.o atr.o \
 	cardconnect.o chkblank.o decimal_str.o dumpdirfunc.o exit.o \
-	globalopts.o gsm7_decode.o gsm7_encode.o gsm7_encode_table.o \
-	gsm7_unpack.o hexdump.o hexread.o hexstr.o names.o number_decode.o \
-	number_encode.o pinentry.o plmncodes.o revnibbles.o
+	filesearch.o globalopts.o gsm7_decode.o gsm7_encode.o \
+	gsm7_encode_table.o gsm7_unpack.o hexdump.o hexread.o hexstr.o names.o \
+	number_decode.o number_encode.o pinentry.o plmncodes.o revnibbles.o
 LIB=	libcommon.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/filesearch.c	Sat Feb 20 21:00:24 2021 +0000
@@ -0,0 +1,27 @@
+/*
+ * This module implements the function that searches for files
+ * in a dedicated directory for SIM programming scripts.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+
+static char script_install_dir[] = "/opt/freecalypso/sim-scripts";
+
+FILE *
+open_script_input_file(req_filename)
+	char *req_filename;
+{
+	char pathbuf[256];
+	FILE *f;
+
+	if (!index(req_filename, '/') && strlen(req_filename) < 128) {
+		sprintf(pathbuf, "%s/%s", script_install_dir, req_filename);
+		f = fopen(pathbuf, "r");
+		if (f)
+			return f;
+	}
+	f = fopen(req_filename, "r");
+	return f;
+}
--- a/libcommon/hexread.c	Sat Feb 20 20:10:18 2021 +0000
+++ b/libcommon/hexread.c	Sat Feb 20 21:00:24 2021 +0000
@@ -5,11 +5,11 @@
 
 #include <sys/types.h>
 #include <ctype.h>
-#include <string.h>
-#include <strings.h>
 #include <stdio.h>
 #include <stdlib.h>
 
+extern FILE *open_script_input_file();
+
 read_hex_data_file(filename, databuf)
 	char *filename;
 	u_char *databuf;
@@ -18,7 +18,7 @@
 	unsigned count;
 	int c, c2;
 
-	inf = fopen(filename, "r");
+	inf = open_script_input_file(filename);
 	if (!inf) {
 		perror(filename);
 		return(-1);