diff options
author | Sami Kerola | 2019-07-21 22:36:28 +0200 |
---|---|---|
committer | Karel Zak | 2019-07-24 11:11:46 +0200 |
commit | 2a952555d2b56ffaf461465139255c94bf6a49a5 (patch) | |
tree | f6c0841fe09634b8ebe78fd122daa59ee3413fbb | |
parent | login: simplify string handling (diff) | |
download | kernel-qcow2-util-linux-2a952555d2b56ffaf461465139255c94bf6a49a5.tar.gz kernel-qcow2-util-linux-2a952555d2b56ffaf461465139255c94bf6a49a5.tar.xz kernel-qcow2-util-linux-2a952555d2b56ffaf461465139255c94bf6a49a5.zip |
agetty: simplify code in dolog() preprocessor blocks
Aim is to make dolog() a lot more readable and understandable, with downside
of when (rarely?) USE_SYSLOG it not defined the function will use a bit more
space from stack. I think that is price well worth paying.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
-rw-r--r-- | term-utils/agetty.c | 46 |
1 files changed, 20 insertions, 26 deletions
diff --git a/term-utils/agetty.c b/term-utils/agetty.c index 96bf41b36..703fb9fd7 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -2391,46 +2391,40 @@ static void list_speeds(void) * Will be used by log_err() and log_warn() therefore * it takes a format as well as va_list. */ -#define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2) - -static void dolog(int priority, const char *fmt, va_list ap) -{ -#ifndef USE_SYSLOG - int fd; +static void dolog(int priority +#ifndef USE_SYSLOG + __attribute__((__unused__)) #endif - char buf[BUFSIZ]; - char *bp; - + , const char *fmt, va_list ap) +{ +#ifdef USE_SYSLOG /* * If the diagnostic is reported via syslog(3), the process name is * automatically prepended to the message. If we write directly to * /dev/console, we must prepend the process name ourselves. */ -#ifdef USE_SYSLOG - buf[0] = '\0'; - bp = buf; -#else - str2cpy(buf, program_invocation_short_name, ": "); - bp = buf + strlen(buf); -#endif /* USE_SYSLOG */ - vsnprintf(bp, sizeof(buf)-strlen(buf), fmt, ap); - - /* - * Write the diagnostic directly to /dev/console if we do not use the - * syslog(3) facility. - */ -#ifdef USE_SYSLOG openlog(program_invocation_short_name, LOG_PID, LOG_AUTHPRIV); - syslog(priority, "%s", buf); + vsyslog(priority, fmt, ap); closelog(); #else + /* + * Write the diagnostic directly to /dev/console if we do not use + * the syslog(3) facility. + */ + char buf[BUFSIZ]; + char new_fmt[BUFSIZ]; + int fd; + + snprintf(new_fmt, sizeof(new_fmt), "%s: %s\r\n", + program_invocation_short_name, fmt); /* Terminate with CR-LF since the console mode is unknown. */ - strcat(bp, "\r\n"); + vsnprintf(buf, sizeof(buf), new_fmt, ap); + if ((fd = open("/dev/console", 1)) >= 0) { write_all(fd, buf, strlen(buf)); close(fd); } -#endif /* USE_SYSLOG */ +#endif /* USE_SYSLOG */ } static void exit_slowly(int code) |