From 5c1f83f7c05d3d871ab8f74652ae435a79711366 Mon Sep 17 00:00:00 2001 From: Lefteris Chatzimparmpas Date: Sat, 6 Aug 2011 14:48:01 +0200 Subject: [PATCH] Change debug option to take filename as argument Don't create a debug file in ~/.imapfilter, but instead create the specified file. --- src/imap.c | 5 ++--- src/imapfilter.c | 11 +++++------ src/imapfilter.h | 2 +- src/log.c | 27 ++++++--------------------- src/request.c | 2 +- src/response.c | 2 +- 6 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/imap.c b/src/imap.c index 0de81de..d05646e 100644 --- a/src/imap.c +++ b/src/imap.c @@ -32,8 +32,7 @@ send_command(session *ssn, char *cmd, char *alt) if (ssn->socket == -1) return -1; - debug("sending command (%d):\n\n%s\n", ssn->socket, - (opts.debug == 1 && alt ? alt : cmd)); + debug("sending command (%d):\n\n%s\n", ssn->socket, alt ? alt : cmd); verbose("C (%d): %s", ssn->socket, (alt ? alt : cmd)); @@ -85,7 +84,7 @@ imap_continuation(session *ssn, const char *cont, size_t len) socket_write(ssn, "\r\n", strlen("\r\n")) == -1) return -1; - if (opts.debug > 0) { + if (opts.debug) { unsigned int i; debug("sending continuation data (%d):\n\n", ssn->socket); diff --git a/src/imapfilter.c b/src/imapfilter.c index 5182e99..4520152 100644 --- a/src/imapfilter.c +++ b/src/imapfilter.c @@ -43,17 +43,17 @@ main(int argc, char *argv[]) setlocale(LC_CTYPE, ""); - opts.debug = 0; opts.verbose = 0; opts.interactive = 0; opts.log = NULL; opts.config = NULL; opts.oneline = NULL; + opts.debug = NULL; env.home = getenv("HOME"); env.pathmax = -1; - while ((c = getopt(argc, argv, "Vc:de:il:v?")) != -1) { + while ((c = getopt(argc, argv, "Vc:d:e:il:v?")) != -1) { switch (c) { case 'V': version(); @@ -63,8 +63,7 @@ main(int argc, char *argv[]) opts.config = optarg; break; case 'd': - if (opts.debug < 2) - opts.debug++; + opts.debug = optarg; break; case 'e': opts.oneline = optarg; @@ -166,8 +165,8 @@ void usage(void) { - fprintf(stderr, "usage: imapfilter [-diVv] [-c configfile] " - "[-e 'command'] [-l logfile]\n"); + fprintf(stderr, "usage: imapfilter [-iVv] [-c configfile] " + "[-e 'command'] [-l logfile] [-d debugfile]\n"); exit(0); } diff --git a/src/imapfilter.h b/src/imapfilter.h index d6cbe64..590aabb 100644 --- a/src/imapfilter.h +++ b/src/imapfilter.h @@ -63,12 +63,12 @@ /* Program's options. */ typedef struct options { - int debug; /* Debugging level (0..2). */ int verbose; /* Verbose mode. */ int interactive; /* Act as an interpreter. */ char *log; /* Log file for error messages. */ char *config; /* Configuration file. */ char *oneline; /* One line of program/configuration. */ + char *debug; /* Debug file. */ } options; /* Environment variables. */ diff --git a/src/log.c b/src/log.c index 30ec284..293c8fe 100644 --- a/src/log.c +++ b/src/log.c @@ -139,32 +139,17 @@ fatal(unsigned int errnum, const char *fmt,...) int open_debug(void) { - int n; - char b; - char *dt; - int fd; if (!opts.debug) return 0; - n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_DEBUG); + if (create_file(opts.debug, S_IRUSR | S_IWUSR)) + return 1; - if (env.pathmax != -1 && n > env.pathmax) - fatal(ERROR_PATHNAME, - "pathname limit %ld exceeded: %d\n", env.pathmax, n); - - dt = (char *)xmalloc((n + 1) * sizeof(char)); - snprintf(dt, n + 1, "%s/%s", env.home, PATHNAME_DEBUG); - - fd = mkstemp(dt); - - if (fd != -1) { - debugfp = fdopen(fd, "w"); - if (debugfp == NULL) { - error("opening debug file %s: %s\n", dt, - strerror(errno)); - return -1; - } + debugfp = fopen(opts.debug, "w"); + if (logfp == NULL) { + error("opening debug file %s: %s\n", opts.debug, strerror(errno)); + return 1; } return 0; } diff --git a/src/request.c b/src/request.c index f6c9691..caa0fe7 100644 --- a/src/request.c +++ b/src/request.c @@ -67,7 +67,7 @@ request_login(const char *server, const char *port, const char *ssl, if ((rg = response_greeting(s)) == -1) goto fail; - if (opts.debug > 0) + if (opts.debug) if (response_generic(s, imap_noop(s)) == -1) goto fail; diff --git a/src/response.c b/src/response.c index b522751..ea557a7 100644 --- a/src/response.c +++ b/src/response.c @@ -87,7 +87,7 @@ receive_response(session *ssn, char *buf, long timeout, int timeoutfail) return -1; - if (opts.debug > 0) { + if (opts.debug) { int i; debug("getting response (%d):\n\n", ssn->socket);