diff pwm.c @ 20:efef93e54c5f

Automatically save the database when receiving a fatal signal
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed, 06 Sep 2017 13:56:11 +0200
parents 5c6155c8e9b6
children ee4d36c85287
line wrap: on
line diff
--- a/pwm.c	Fri Sep 01 22:33:41 2017 +0200
+++ b/pwm.c	Wed Sep 06 13:56:11 2017 +0200
@@ -142,6 +142,9 @@
 			goto out;
 		case IO_EOF:	/* FALLTHROUGH */
 		case IO_SIGNAL:
+			if (ctx->unsaved_changes) {
+				pwfile_write_autosave_file(ctx);
+			}
 			goto quit;
 		default:
 			fprintf(stderr, "unknown error\n");
@@ -306,7 +309,6 @@
 	const char	*master_password_filename = NULL;
 	struct pwm_ctx	ctx = { 0 };
 	struct passwd	*passwd;
-	char		*pwm_dirname = NULL;
 	FILE		*fp = NULL;
 
 	setprogname(argv[0]);
@@ -355,19 +357,20 @@
 		goto out;
 	}
 
+	passwd = getpwuid(getuid());
+	if (passwd == NULL) {
+		err(1, "getpwuid");
+	}
+	xasprintf(&ctx.dirname, "%s/.pwm", passwd->pw_dir);
+
+	/* create ~/.pwm directory if necessary */
+	if ((mkdir(ctx.dirname, S_IRWXU) != 0) && (errno != EEXIST)) {
+		warn("failed to create directory \"%s\"", ctx.dirname);
+		goto out;
+	}
+
 	if (optind == argc) {
-		passwd = getpwuid(getuid());
-		if (passwd == NULL) {
-			err(1, "getpwuid");
-		}
-		xasprintf(&pwm_dirname, "%s/.pwm", passwd->pw_dir);
-		xasprintf(&ctx.filename, "%s/pwm.psafe3", pwm_dirname);
-
-		/* create ~/.pwm directory if necessary */
-		if ((mkdir(pwm_dirname, S_IRWXU) != 0) && (errno != EEXIST)) {
-			warn("failed to create directory \"%s\"", pwm_dirname);
-			goto out;
-		}
+		xasprintf(&ctx.filename, "%s/pwm.psafe3", ctx.dirname);
 	} else if (optind + 1 == argc) {
 		ctx.filename = xstrdup(argv[optind++]);
 	} else {
@@ -417,8 +420,8 @@
 		fclose(fp);
 	}
 	free(ctx.filename);
+	free(ctx.dirname);
 	free(ctx.errmsg);
-	free(pwm_dirname);
 
 	exit(status);
 }