--- logging.c.orig Fri Dec 22 20:47:18 2006 +++ logging.c Sat Dec 23 23:22:11 2006 @@ -133,44 +133,59 @@ char *msg; { size_t count; + size_t maxlen; char *p; char *tmp; char save; /* + * You should carefully watch that the value of skip_*'s RHS is + * greater than zero to avoid unsigned overflows. + */ + const char fmt_first[] = "%8s : %s"; + const char fmt_contd[] = "%8s : (command continued) %s"; + const unsigned int skip_first = sizeof(fmt_first) + 5 - 3; + const unsigned int skip_contd = sizeof(fmt_contd) + 5 - 3; + const unsigned int max_skip = (skip_first > skip_contd ? + skip_first : skip_contd); + + /* * Log the full line, breaking into multiple syslog(3) calls if necessary */ - for (p = msg, count = 0; *p && count < strlen(msg) / MAXSYSLOGLEN + 1; - count++) { - if (strlen(p) > MAXSYSLOGLEN) { + for (p = msg, count = 0, maxlen = MAXSYSLOGLEN - skip_first; + *p && count < strlen(msg) / (MAXSYSLOGLEN - max_skip) + 1; + count++, maxlen = MAXSYSLOGLEN - skip_contd) { + if (strlen(p) > maxlen) { /* * Break up the line into what will fit on one syslog(3) line - * Try to break on a word boundary if possible. + * Try to avoid breaking words into several lines if possible. */ - for (tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp--) + for (tmp = p + maxlen; tmp > p && *tmp != ' '; tmp--) ; if (tmp <= p) - tmp = p + MAXSYSLOGLEN; + tmp = p + maxlen; /* NULL terminate line, but save the char to restore later */ save = *tmp; *tmp = '\0'; if (count == 0) - mysyslog(pri, "%8s : %s", user_name, p); + mysyslog(pri, fmt_first, user_name, p); else - mysyslog(pri, "%8s : (command continued) %s", user_name, p); + mysyslog(pri, fmt_contd, user_name, p); *tmp = save; /* restore saved character */ /* Eliminate leading whitespace */ - for (p = tmp; *p != ' ' && *p !='\0'; p++) + for (p = tmp; *p == ' '; p++) ; } else { if (count == 0) - mysyslog(pri, "%8s : %s", user_name, p); + mysyslog(pri, fmt_first, user_name, p); else - mysyslog(pri, "%8s : (command continued) %s", user_name, p); + mysyslog(pri, fmt_contd, user_name, p); + /* All done, no more iterations */ + break; } } }