pass

simple password manager
git clone git://git.2f30.org/pass
Log | Files | Refs | README | LICENSE

log.c (2241B)


      1 #include <errno.h>
      2 #include <gpgme.h>
      3 #include <limits.h>
      4 #include <sys/stat.h>
      5 #include <stdarg.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 
     10 #include "pass.h"
     11 
     12 int debug;
     13 
     14 static char *progname;
     15 
     16 static void
     17 vlog(char *msg, va_list ap)
     18 {
     19 	char file[PATH_MAX];
     20 	const char *home;
     21 	FILE *fp;
     22 	struct stat sb;
     23 
     24 	if (debug) {
     25 		if (!(home = getenv("HOME"))) {
     26 			fprintf(stderr, "$HOME not set, cannot determine password-store location");
     27 			exit(1);
     28 		}
     29 		snprintf(file, sizeof(file), "%s/.pass.log", home);
     30 		
     31 		if (!stat(file,&sb))
     32 			remove(file);
     33 		if((fp = fopen(file, "ab"))) {
     34 			fprintf(fp, "%s: ", progname);
     35 			vfprintf(fp, msg, ap);
     36 			fputc('\n', fp);
     37 		} else {
     38 			fprintf(stderr, "fopen %s: %s", file, strerror(errno));
     39 		}
     40 	} else {
     41 		fprintf(stderr, "%s: ", progname);
     42 		vfprintf(stderr, msg, ap);
     43 		fputc('\n', stderr);
     44 	}
     45 }
     46 
     47 void
     48 loginit(char *prog)
     49 {
     50 	progname = prog;
     51 }
     52 
     53 void
     54 logdbg(char *msg, ...)
     55 {
     56 	char buf[512];
     57 	va_list ap;
     58 
     59 	va_start(ap, msg);
     60 	snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
     61 	vlog(buf, ap);
     62 	va_end(ap);
     63 }
     64 
     65 void
     66 logdbgx(char *msg, ...)
     67 {
     68 	va_list ap;
     69 
     70 	va_start(ap, msg);
     71 	vlog(msg, ap);
     72 	va_end(ap);
     73 }
     74 
     75 void
     76 logwarn(char *msg, ...)
     77 {
     78 	char buf[512];
     79 	va_list ap;
     80 
     81 	va_start(ap, msg);
     82 	snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
     83 	vlog(buf, ap);
     84 	va_end(ap);
     85 }
     86 
     87 void
     88 logwarnx(char *msg, ...)
     89 {
     90 	va_list ap;
     91 
     92 	va_start(ap, msg);
     93 	vlog(msg, ap);
     94 	va_end(ap);
     95 }
     96 
     97 void
     98 loggpg(gpgme_error_t gerr, char *msg, ...)
     99 {
    100 	char buf[512];
    101 	va_list ap;
    102 
    103 	va_start(ap, msg);
    104 	snprintf(buf, sizeof(buf), "%s: %s", msg, gpgme_strerror(gerr));
    105 	vlog(msg, ap);
    106 	va_end(ap);
    107 }
    108 
    109 void
    110 loggpgx(char *msg, ...)
    111 {
    112 	va_list ap;
    113 
    114 	va_start(ap, msg);
    115 	vlog(msg, ap);
    116 	va_end(ap);
    117 }
    118 
    119 void
    120 fatalgpg(gpgme_error_t gerr, char *msg, ...)
    121 {
    122 	char buf[512];
    123 	va_list ap;
    124 
    125 	va_start(ap, msg);
    126 	snprintf(buf, sizeof(buf), "%s: %s", msg, gpgme_strerror(gerr));
    127 	vlog(buf, ap);
    128 	va_end(ap);
    129 	exit(1);
    130 }
    131 
    132 void
    133 fatal(char *msg, ...)
    134 {
    135 	char buf[512];
    136 	va_list ap;
    137 
    138 	va_start(ap, msg);
    139 	snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
    140 	vlog(buf, ap);
    141 	va_end(ap);
    142 	exit(1);
    143 }
    144 
    145 void
    146 fatalx(char *msg, ...)
    147 {
    148 	va_list ap;
    149 
    150 	va_start(ap, msg);
    151 	vlog(msg, ap);
    152 	va_end(ap);
    153 	exit(1);
    154 }