comparison 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
comparison
equal deleted inserted replaced
19:5c6155c8e9b6 20:efef93e54c5f
140 /* line was truncated in non-interactive mode */ 140 /* line was truncated in non-interactive mode */
141 fprintf(stderr, "line too long\n"); 141 fprintf(stderr, "line too long\n");
142 goto out; 142 goto out;
143 case IO_EOF: /* FALLTHROUGH */ 143 case IO_EOF: /* FALLTHROUGH */
144 case IO_SIGNAL: 144 case IO_SIGNAL:
145 if (ctx->unsaved_changes) {
146 pwfile_write_autosave_file(ctx);
147 }
145 goto quit; 148 goto quit;
146 default: 149 default:
147 fprintf(stderr, "unknown error\n"); 150 fprintf(stderr, "unknown error\n");
148 goto quit; 151 goto quit;
149 } 152 }
304 int errflag = 0; 307 int errflag = 0;
305 int c; 308 int c;
306 const char *master_password_filename = NULL; 309 const char *master_password_filename = NULL;
307 struct pwm_ctx ctx = { 0 }; 310 struct pwm_ctx ctx = { 0 };
308 struct passwd *passwd; 311 struct passwd *passwd;
309 char *pwm_dirname = NULL;
310 FILE *fp = NULL; 312 FILE *fp = NULL;
311 313
312 setprogname(argv[0]); 314 setprogname(argv[0]);
313 315
314 /* set up locale and check for UTF-8 codeset */ 316 /* set up locale and check for UTF-8 codeset */
353 usage(); 355 usage();
354 status = EXIT_USAGE; 356 status = EXIT_USAGE;
355 goto out; 357 goto out;
356 } 358 }
357 359
360 passwd = getpwuid(getuid());
361 if (passwd == NULL) {
362 err(1, "getpwuid");
363 }
364 xasprintf(&ctx.dirname, "%s/.pwm", passwd->pw_dir);
365
366 /* create ~/.pwm directory if necessary */
367 if ((mkdir(ctx.dirname, S_IRWXU) != 0) && (errno != EEXIST)) {
368 warn("failed to create directory \"%s\"", ctx.dirname);
369 goto out;
370 }
371
358 if (optind == argc) { 372 if (optind == argc) {
359 passwd = getpwuid(getuid()); 373 xasprintf(&ctx.filename, "%s/pwm.psafe3", ctx.dirname);
360 if (passwd == NULL) {
361 err(1, "getpwuid");
362 }
363 xasprintf(&pwm_dirname, "%s/.pwm", passwd->pw_dir);
364 xasprintf(&ctx.filename, "%s/pwm.psafe3", pwm_dirname);
365
366 /* create ~/.pwm directory if necessary */
367 if ((mkdir(pwm_dirname, S_IRWXU) != 0) && (errno != EEXIST)) {
368 warn("failed to create directory \"%s\"", pwm_dirname);
369 goto out;
370 }
371 } else if (optind + 1 == argc) { 374 } else if (optind + 1 == argc) {
372 ctx.filename = xstrdup(argv[optind++]); 375 ctx.filename = xstrdup(argv[optind++]);
373 } else { 376 } else {
374 usage(); 377 usage();
375 status = EXIT_USAGE; 378 status = EXIT_USAGE;
415 pwfile_destroy(&ctx); 418 pwfile_destroy(&ctx);
416 if (fp != NULL) { 419 if (fp != NULL) {
417 fclose(fp); 420 fclose(fp);
418 } 421 }
419 free(ctx.filename); 422 free(ctx.filename);
423 free(ctx.dirname);
420 free(ctx.errmsg); 424 free(ctx.errmsg);
421 free(pwm_dirname);
422 425
423 exit(status); 426 exit(status);
424 } 427 }