From d8e113174e0b2ceff6426ba27104343c8f41d447 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 13 Apr 2008 16:02:41 +0100 Subject: [PATCH] Improve formatting before we get too stuck in. --- checkpath.c | 139 +++++++++++++++++++++++++++++++----------------------------- checkpath.h | 2 - chkpath.c | 16 +++---- tmpdir.c | 34 ++++++--------- 4 files changed, 89 insertions(+), 102 deletions(-) diff --git a/checkpath.c b/checkpath.c index b6ee94e..85c4b09 100644 --- a/checkpath.c +++ b/checkpath.c @@ -1,7 +1,5 @@ /* -*-c-*- * - * $Id: checkpath.c,v 1.6 2004/04/08 01:36:22 mdw Exp $ - * * Check a path for safety * * (c) 1999 Mark Wooding @@ -152,10 +150,7 @@ static void pop(void) */ static void popall(void) -{ - while (sp->e_link) - pop(); -} + { while (sp->e_link) pop(); } /* --- @push@ --- * * @@ -191,75 +186,82 @@ static void push(struct elt *e) static void report(const struct checkpath *cp, unsigned what, int verbose, const char *p, const char *msg, ...) { + dstr d = DSTR_INIT; + va_list ap; + const char *q = msg; + const char *s; + size_t n; + int e = errno; + uid_t u; + struct passwd *pw; + gid_t g; + struct group *gr; + /* --- Decide whether to bin this message --- */ if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what)) return; + /* --- If no reporting, do the easy thing --- */ + + if (!(cp->cp_what & CP_REPORT)) { + cp->cp_report(what, verbose, p, 0, cp->cp_arg); + return; + } + /* --- Format the message nicely --- */ - if (cp->cp_what & CP_REPORT) { - dstr d = DSTR_INIT; - va_list ap; - const char *q = msg; - size_t n; - int e = errno; - - va_start(ap, msg); - if (verbose > 1) - dstr_puts(&d, "[ "); - if (p) - dstr_putf(&d, "Path: %s: ", p); - while (*q) { - if (*q == '%') { - q++; - switch (*q) { - case 'e': - dstr_puts(&d, strerror(e)); - break; - case 'u': { - uid_t u = (uid_t)va_arg(ap, int); - struct passwd *pw = getpwuid(u); - if (pw) - dstr_putf(&d, "`%s'", pw->pw_name); - else - dstr_putf(&d, "%i", (int)u); - } break; - case 'g': { - gid_t g = (gid_t)va_arg(ap, int); - struct group *gr = getgrgid(g); - if (gr) - dstr_putf(&d, "`%s'", gr->gr_name); - else - dstr_putf(&d, "%i", (int)g); - } break; - case 's': { - const char *s = va_arg(ap, const char *); - dstr_puts(&d, s); - } break; - case '%': - dstr_putc(&d, '%'); - break; - default: - dstr_putc(&d, '%'); - dstr_putc(&d, *q); - break; - } - q++; - } else { - n = strcspn(q, "%"); - DPUTM(&d, q, n); - q += n; + va_start(ap, msg); + if (verbose > 1) + dstr_puts(&d, "[ "); + if (p) + dstr_putf(&d, "Path: %s: ", p); + while (*q) { + if (*q == '%') { + q++; + switch (*q) { + case 'e': + dstr_puts(&d, strerror(e)); + break; + case 'u': + u = (uid_t)va_arg(ap, int); + if ((pw = getpwuid(u)) != 0) + dstr_putf(&d, "`%s'", pw->pw_name); + else + dstr_putf(&d, "%i", (int)u); + break; + case 'g': + g = (gid_t)va_arg(ap, int); + if ((gr = getgrgid(g)) != 0) + dstr_putf(&d, "`%s'", gr->gr_name); + else + dstr_putf(&d, "%i", (int)g); + break; + case 's': + s = va_arg(ap, const char *); + dstr_puts(&d, s); + break; + case '%': + dstr_putc(&d, '%'); + break; + default: + dstr_putc(&d, '%'); + dstr_putc(&d, *q); + break; } + q++; + } else { + n = strcspn(q, "%"); + DPUTM(&d, q, n); + q += n; } - if (verbose > 1) - dstr_puts(&d, " ]"); - DPUTZ(&d); - cp->cp_report(what, verbose, p, d.buf, cp->cp_arg); - dstr_destroy(&d); - va_end(ap); - } else - cp->cp_report(what, verbose, p, 0, cp->cp_arg); + } + if (verbose > 1) + dstr_puts(&d, " ]"); + DPUTZ(&d); + cp->cp_report(what, verbose, p, d.buf, cp->cp_arg); + dstr_destroy(&d); + va_end(ap); } /* --- @sanity@ --- * @@ -279,6 +281,8 @@ static unsigned sanity(const char *p, struct stat *st, { unsigned bad = 0; int stickyok = 0; + int i; + unsigned b; if (S_ISDIR(st->st_mode) && (!(f & f_last) || (cp->cp_what & CP_STICKYOK))) @@ -296,8 +300,7 @@ static unsigned sanity(const char *p, struct stat *st, if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) && (st->st_mode & (0020 | stickyok)) == 0020) { - int i; - unsigned b = CP_WRGRP; + b = CP_WRGRP; if (cp->cp_what & CP_WROTHGRP) { b = CP_WROTHGRP; diff --git a/checkpath.h b/checkpath.h index 5a7e35e..e2f6385 100644 --- a/checkpath.h +++ b/checkpath.h @@ -1,7 +1,5 @@ /* -*-c-*- * - * $Id: checkpath.h,v 1.4 2004/04/08 01:36:22 mdw Exp $ - * * Check a path for safety * * (c) 1999 Mark Wooding diff --git a/chkpath.c b/chkpath.c index 5d7e01d..4230637 100644 --- a/chkpath.c +++ b/chkpath.c @@ -1,7 +1,5 @@ /* -*-c-*- * - * $Id: chkpath.c,v 1.4 2004/04/08 01:36:22 mdw Exp $ - * * Check a user's file search path * * (c) 1999 Mark Wooding @@ -43,26 +41,22 @@ /*----- Main code ---------------------------------------------------------*/ +/* --- @report@ --- */ + static void report(unsigned what, int verbose, const char *p, const char *msg, void *arg) -{ - moan("%s", msg); -} + { moan("%s", msg); } /* --- @usage@ --- */ static void usage(FILE *fp) -{ - fprintf(fp, "Usage: %s [-vqstp] [PATH...]\n", QUIS); -} + { fprintf(fp, "Usage: %s [-vqstp] [PATH...]\n", QUIS); } /* --- @version@ --- */ static void version(FILE *fp) -{ - fprintf(fp, "%s version %s\n", QUIS, VERSION); -} + { fprintf(fp, "%s version %s\n", QUIS, VERSION); } /* --- @help@ --- */ diff --git a/tmpdir.c b/tmpdir.c index 0920341..137127a 100644 --- a/tmpdir.c +++ b/tmpdir.c @@ -1,7 +1,5 @@ /* -*-c-*- * - * $Id: tmpdir.c,v 1.5 2004/04/08 01:36:22 mdw Exp $ - * * Choose and check temporary directories * * (c) 1999 Mark Wooding @@ -40,6 +38,7 @@ #include #include +#include #include #include #include @@ -229,16 +228,12 @@ static char *goodtmp(void) /* --- @usage@ --- */ static void usage(FILE *fp) -{ - fprintf(fp, "Usage: %s [-bc] [-v PATH]\n", QUIS); -} + { fprintf(fp, "Usage: %s [-bc] [-v PATH]\n", QUIS); } /* --- @version@ --- */ static void version(FILE *fp) -{ - fprintf(fp, "%s version %s\n", QUIS, VERSION); -} + { fprintf(fp, "%s version %s\n", QUIS, VERSION); } /* --- @help@ --- */ @@ -284,6 +279,7 @@ int main(int argc, char *argv[]) { int shell = 0; int duff = 0; + char *p; enum { sh_unknown, @@ -352,7 +348,6 @@ int main(int argc, char *argv[]) /* --- Choose a shell --- */ if (!shell) { - char *p; if (!(p = getenv("SHELL"))) p = pw->pw_shell; if (strstr(p, "csh")) @@ -363,19 +358,16 @@ int main(int argc, char *argv[]) /* --- Start the checking --- */ - { - char *p = goodtmp(); - if (!p) - die(1, "no good tmp directory"); - switch (shell) { - case sh_bourne: - printf("TMPDIR=\"%s\"; export TMPDIR\n", p); - break; - case sh_csh: - printf("setenv TMPDIR \"%s\"\n", p); + if ((p = goodtmp()) == 0) + die(1, "no good tmp directory"); + switch (shell) { + case sh_bourne: + printf("TMPDIR=\"%s\"; export TMPDIR\n", p); + break; + case sh_csh: + printf("setenv TMPDIR \"%s\"\n", p); break; - } - } + } return (0); } -- 2.11.0