diff cmd.c @ 10:17fb30016e64

Enable access to record and file metadata Add info command to show file metadata. Enable display of creation and modification dates of records.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Fri, 28 Jul 2017 15:53:57 +0200
parents b5c4267a7182
children 85bce13237cf
line wrap: on
line diff
--- a/cmd.c	Fri Jul 28 09:53:46 2017 +0200
+++ b/cmd.c	Fri Jul 28 15:53:57 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Guido Berhoerster <guido+pwm@berhoerster.name>
+ * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -33,12 +33,16 @@
 #endif /* READPASSPHRASE_H */
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "cmd.h"
 #include "pwfile.h"
 #include "util.h"
 
+#define	TIME_FORMAT	"%Y-%m-%dT%TZ"
+#define	TIME_SIZE	(4 + 1 + 2 + 1 + 2 + 1 + 8 + 1 + 1)
+
 enum field_type {
 	FIELD_UNKNOWN = -1,
 	FIELD_GROUP,
@@ -46,9 +50,12 @@
 	FIELD_USERNAME,
 	FIELD_PASSWORD,
 	FIELD_NOTES,
-	FIELD_URL
+	FIELD_URL,
+	FIELD_MTIME,
+	FIELD_CTIME
 };
 
+static enum cmd_return	cmd_info(struct pwm_ctx *, int, char *[]);
 static enum cmd_return	cmd_list(struct pwm_ctx *, int, char *[]);
 static enum cmd_return	cmd_create(struct pwm_ctx *, int, char *[]);
 static enum cmd_return	cmd_modify(struct pwm_ctx *, int, char *[]);
@@ -68,7 +75,9 @@
     "username",
     "password",
     "notes",
-    "url"
+    "url",
+    "ctime",
+    "mtime"
 };
 
 static const char *field_labels[] = {
@@ -77,10 +86,14 @@
     "Username: ",
     "Password: ",
     "Notes:    ",
-    "URL:      "
+    "URL:      ",
+    "Created:  ",
+    "Modified: "
 };
 
 static struct cmd cmds[] = {
+    { "i", "info", "info", "Show metadata information about the current file",
+    cmd_info },
     { "ls", "list", "list", "List entries", cmd_list },
     { "c", "create", "create field=value ...", "Create entry", cmd_create },
     { "m", "modify", "modify id field=value ...", "Modify entry", cmd_modify },
@@ -150,7 +163,7 @@
 				record->url = value;
 				break;
 			default:
-				break;
+				return (FIELD_UNKNOWN);
 			}
 			return (i);
 		}
@@ -177,6 +190,37 @@
 }
 
 static enum cmd_return
+cmd_info(struct pwm_ctx *ctx, int argc, char *argv[])
+{
+	struct metadata	*metadata;
+	struct tm	*tm;
+	char		timebuf[TIME_SIZE];
+
+	if (argc != 1) {
+		return (CMD_USAGE);
+	}
+
+	metadata = pwfile_get_metadata(ctx);
+	printf("Format:      0x%04x\n", metadata->version);
+	if (metadata->user != NULL) {
+		printf("User:        %s\n", metadata->user);
+	}
+	if (metadata->user != NULL) {
+		printf("Host:        %s\n", metadata->host);
+	}
+	if (metadata->user != NULL) {
+		printf("Application: %s\n", metadata->application);
+	}
+	tm = gmtime(&metadata->timestamp);
+	strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
+	printf("Last Saved:  %s\n", timebuf);
+
+	pwfile_destroy_metadata(metadata);
+
+	return (CMD_OK);
+}
+
+static enum cmd_return
 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[])
 {
 	union list_item	**list;
@@ -290,6 +334,9 @@
 static void
 print_record(struct record *record, int fields[], int show_labels, FILE *fp)
 {
+	struct tm	*tm;
+	char		timebuf[TIME_SIZE];
+
 	if (fields[FIELD_TITLE]) {
 		if (print_field(field_labels[FIELD_TITLE], record->title,
 		    show_labels, fp) != 0) {
@@ -326,6 +373,22 @@
 			return;
 		}
 	}
+	if (fields[FIELD_CTIME]) {
+		tm = gmtime(&record->ctime);
+		strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
+		if (print_field(field_labels[FIELD_CTIME], timebuf,
+		    show_labels, fp)) {
+			return;
+		}
+	}
+	if (fields[FIELD_MTIME]) {
+		tm = gmtime(&record->mtime);
+		strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
+		if (print_field(field_labels[FIELD_MTIME], timebuf,
+		    show_labels, fp)) {
+			return;
+		}
+	}
 }
 
 static enum cmd_return