commit ad97f92d3dc0fb15decca22c6a79f09c9bc6d743
parent 00eff5e9eea8509f70207dca159ab338c12fa362
Author: Naveen Narayanan zerous <zerous@nocebo.space>
Date: Mon, 18 Dec 2017 20:02:38 +0300
Fix bug in parsing directory path. Add log.c.
Diffstat:
Makefile | | | 6 | ++++-- |
log.c | | | 95 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
pass.c | | | 16 | ++++++++++++---- |
pass.h | | | 11 | +++++++++++ |
4 files changed, 122 insertions(+), 6 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,7 +2,9 @@ VERSION = 0.1
include config.mk
-OBJ = pass.o
+OBJ = pass.o \
+ log.o
+
BIN = pass
all: ${BIN}
@@ -10,7 +12,7 @@ all: ${BIN}
${BIN}: ${OBJ}
${CC} ${LDFLAGS} -o $@ ${OBJ} ${LIBS}
-pass.o:
+${OBJ}: pass.h
install: all
mkdir -p ${DESTDIR}${PREFIX}/bin
diff --git a/log.c b/log.c
@@ -0,0 +1,95 @@
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "pass.h"
+
+int debug;
+
+static char *progname;
+
+static void
+vlog(char *msg, va_list ap)
+{
+ if (debug) {
+ fprintf(stderr, "%s: ", progname);
+ vfprintf(stderr, msg, ap);
+ fputc('\n', stderr);
+ }
+}
+
+void
+loginit(char *prog)
+{
+ progname = prog;
+}
+
+void
+logdbg(char *msg, ...)
+{
+ char buf[512];
+ va_list ap;
+
+ va_start(ap, msg);
+ snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
+ vlog(buf, ap);
+ va_end(ap);
+}
+
+void
+logdbgx(char *msg, ...)
+{
+ va_list ap;
+
+ va_start(ap, msg);
+ vlog(msg, ap);
+ va_end(ap);
+}
+
+void
+logwarn(char *msg, ...)
+{
+ char buf[512];
+ va_list ap;
+
+ va_start(ap, msg);
+ snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
+ vlog(buf, ap);
+ va_end(ap);
+}
+
+void
+logwarnx(char *msg, ...)
+{
+ va_list ap;
+
+ va_start(ap, msg);
+ vlog(msg, ap);
+ va_end(ap);
+}
+
+void
+fatal(char *msg, ...)
+{
+ char buf[512];
+ va_list ap;
+
+ va_start(ap, msg);
+ snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
+ vlog(buf, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void
+fatalx(char *msg, ...)
+{
+ va_list ap;
+
+ va_start(ap, msg);
+ vlog(msg, ap);
+ va_end(ap);
+ exit(1);
+}
diff --git a/pass.c b/pass.c
@@ -24,7 +24,6 @@ initgpgme(void)
gpgme_set_locale(NULL, LC_ALL, "en_US.UTF-8");
gpgme_set_global_flag("require-gnupg", reqgpgver);
gpgme_check_version(reqlibver);
-
}
void
@@ -79,7 +78,7 @@ getuserid(char *u)
void
insert(char *item)
{
- char t[256];
+ char t[PATH_MAX];
char *tp, *i, *gfile;
const char *home;
char file[PATH_MAX];
@@ -103,7 +102,6 @@ insert(char *item)
tp = t;
if ((i = strrchr(tp, '/'))) {
- *i = '\0';
gfile = i + 1;
} else
gfile = item;
@@ -159,6 +157,10 @@ insert(char *item)
break;
}
}
+ /* throw an error if the respective key isn't found */
+ if (gpgme_err_code(gpgerr) == GPG_ERR_EOF)
+ err(1, "can not find key: %s\n", gpgme_strerror(gpgerr));
+
keys[0] = key;
keys[1] = NULL;
@@ -166,9 +168,10 @@ insert(char *item)
gpgme_set_armor(ctx, 1);
fin = fopen(file, "r");
+ memcpy(t, file, strlen(file) + 1);
+ printf("t: %s\n", t);
snprintf(file, sizeof(file), "%s.gpg", file);
fout = fopen(file, "w");
-
gpgerr = gpgme_data_new_from_stream(&in, fin);
if (gpgme_err_code(gpgerr) != GPG_ERR_NO_ERROR)
errx(1, "gpgme_data_new_from_file %s", gpgme_strerror(gpgerr));
@@ -189,6 +192,8 @@ insert(char *item)
gpgme_release(ctx);
fclose(fin);
fclose(fout);
+ if (remove(t))
+ fprintf(stderr, "remove failed\n");
}
@@ -256,6 +261,9 @@ initpass(char *pgpid)
int
main(int argc, char** argv)
{
+ debug = 1;
+ if (debug)
+ loginit("pass");
if (argc > 1)
{
argv++;
diff --git a/pass.h b/pass.h
@@ -0,0 +1,11 @@
+/* log.c */
+extern int debug;
+
+/* log.c */
+void loginit(char *);
+void logdbg(char *, ...);
+void logdbgx(char *, ...);
+void logwarn(char *, ...);
+void logwarnx(char *, ...);
+void fatal(char *, ...);
+void fatalx(char *, ...);