From dc7f716a656bee42c53722ad6890684a71e098df Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Tue, 6 Mar 2018 15:27:14 -0500 Subject: [PATCH] autoformat to fix indentation issues throughout the code. converted to tabs to make the multiline definition for ARGLEN render nicely --- shared/output.c | 675 +++++++++++++++++++++++++++--------------------- 1 file changed, 375 insertions(+), 300 deletions(-) diff --git a/shared/output.c b/shared/output.c index 776dca54b8..59569de0bf 100644 --- a/shared/output.c +++ b/shared/output.c @@ -71,13 +71,13 @@ pthread_mutex_t mdprintf_mutex = PTHREAD_MUTEX_INITIALIZER; #include #define gettext_noop(s) s -#define _(s) gettext(s) -#define N_(s) gettext_noop(s) +#define _(s) gettext(s) +#define N_(s) gettext_noop(s) #else -#define _(s) s -#define N_(s) s +#define _(s) s +#define N_(s) s #endif @@ -91,73 +91,83 @@ short logg_syslog; #endif short int mprintf_disabled = 0, mprintf_verbose = 0, mprintf_quiet = 0, - mprintf_stdout = 0, mprintf_nowarn = 0, mprintf_send_timeout = 100, mprintf_progress = 0; - -#define ARGLEN(args, str, len) \ -{ \ - size_t arglen = 1, i; \ - char *pt; \ - va_start(args, str); \ - len = strlen(str); \ - for(i = 0; i < len - 1; i++) { \ - if(str[i] == '%') { \ - switch(str[++i]) { \ - case 's': \ - pt = va_arg(args, char *); \ - if(pt) \ - arglen += strlen(pt); \ - break; \ - case 'f': \ - va_arg(args, double); \ - arglen += 25; \ - break; \ - case 'l': \ - va_arg(args, long); \ - arglen += 20; \ - break; \ - default: \ - va_arg(args, int); \ - arglen += 10; \ - break; \ - } \ - } \ - } \ - va_end(args); \ - len += arglen; \ -} + mprintf_stdout = 0, mprintf_nowarn = 0, mprintf_send_timeout = 100, mprintf_progress = 0; + +#define ARGLEN(args, str, len) \ + { \ + size_t arglen = 1, i; \ + char *pt; \ + va_start(args, str); \ + len = strlen(str); \ + for (i = 0; i < len - 1; i++) \ + { \ + if (str[i] == '%') \ + { \ + switch (str[++i]) \ + { \ + case 's': \ + pt = va_arg(args, char *); \ + if (pt) \ + arglen += strlen(pt); \ + break; \ + case 'f': \ + va_arg(args, double); \ + arglen += 25; \ + break; \ + case 'l': \ + va_arg(args, long); \ + arglen += 20; \ + break; \ + default: \ + va_arg(args, int); \ + arglen += 10; \ + break; \ + } \ + } \ + } \ + va_end(args); \ + len += arglen; \ + } int mdprintf(int desc, const char *str, ...) { - va_list args; - char buffer[512], *abuffer = NULL, *buff; - int bytes, todo, ret=0; - size_t len; + va_list args; + char buffer[512], *abuffer = NULL, *buff; + int bytes, todo, ret = 0; + size_t len; ARGLEN(args, str, len); - if(len <= sizeof(buffer)) { - len = sizeof(buffer); - buff = buffer; - } else { - abuffer = malloc(len); - if(!abuffer) { - len = sizeof(buffer); - buff = buffer; - } else { - buff = abuffer; - } + if (len <= sizeof(buffer)) + { + len = sizeof(buffer); + buff = buffer; + } + else + { + abuffer = malloc(len); + if (!abuffer) + { + len = sizeof(buffer); + buff = buffer; + } + else + { + buff = abuffer; + } } va_start(args, str); bytes = vsnprintf(buff, len, str, args); va_end(args); buff[len - 1] = 0; - if(bytes < 0) { - if(len > sizeof(buffer)) - free(abuffer); - return bytes; + if (bytes < 0) + { + if (len > sizeof(buffer)) + free(abuffer); + return bytes; } - if((size_t) bytes >= len) - bytes = len - 1; + if ((size_t)bytes >= len) + bytes = len - 1; todo = bytes; #ifdef CL_THREAD_SAFE @@ -165,43 +175,49 @@ int mdprintf(int desc, const char *str, ...) * important for IDSESSION */ pthread_mutex_lock(&mdprintf_mutex); #endif - while (todo > 0) { - ret = send(desc, buff, bytes, 0); - if (ret < 0) { - struct timeval tv; - if (errno != EWOULDBLOCK) - break; - /* didn't send anything yet */ + while (todo > 0) + { + ret = send(desc, buff, bytes, 0); + if (ret < 0) + { + struct timeval tv; + if (errno != EWOULDBLOCK) + break; + /* didn't send anything yet */ #ifdef CL_THREAD_SAFE - pthread_mutex_unlock(&mdprintf_mutex); -#endif - tv.tv_sec = 0; - tv.tv_usec = mprintf_send_timeout*1000; - do { - fd_set wfds; - FD_ZERO(&wfds); - FD_SET(desc, &wfds); - ret = select(desc+1, NULL, &wfds, NULL, &tv); - } while (ret < 0 && errno == EINTR); + pthread_mutex_unlock(&mdprintf_mutex); +#endif + tv.tv_sec = 0; + tv.tv_usec = mprintf_send_timeout * 1000; + do + { + fd_set wfds; + FD_ZERO(&wfds); + FD_SET(desc, &wfds); + ret = select(desc + 1, NULL, &wfds, NULL, &tv); + } while (ret < 0 && errno == EINTR); #ifdef CL_THREAD_SAFE - pthread_mutex_lock(&mdprintf_mutex); -#endif - if (!ret) { - /* timed out */ - ret = -1; - break; - } - } else { - todo -= ret; - buff += ret; - } + pthread_mutex_lock(&mdprintf_mutex); +#endif + if (!ret) + { + /* timed out */ + ret = -1; + break; + } + } + else + { + todo -= ret; + buff += ret; + } } #ifdef CL_THREAD_SAFE pthread_mutex_unlock(&mdprintf_mutex); #endif - if(len > sizeof(buffer)) - free(abuffer); + if (len > sizeof(buffer)) + free(abuffer); return ret < 0 ? -1 : bytes; } @@ -213,8 +229,10 @@ static int rename_logg(STATBUF *sb) time_t t; struct tm tmp; - if (!logg_rotate) { - if (logg_fp) { + if (!logg_rotate) + { + if (logg_fp) + { fprintf(logg_fp, "Log size = %lld, max = %lld\n", (long long int)sb->st_size, (long long int)logg_size); fprintf(logg_fp, "WARNING: Log size limit met but log file rotation turned off. Forcing log file rotation anyways.\n"); } @@ -224,7 +242,8 @@ static int rename_logg(STATBUF *sb) rotate_file_len = strlen(logg_file) + sizeof("-YYYY-MM-DD_HH:MM:SS"); rotate_file = calloc(1, rotate_file_len + 1); - if (!rotate_file) { + if (!rotate_file) + { if (logg_fp) fprintf(logg_fp, "Need to rotate log file due to size but ran out of memory.\n"); @@ -232,7 +251,8 @@ static int rename_logg(STATBUF *sb) } t = time(NULL); - if (!localtime_r(&t, &tmp)) { + if (!localtime_r(&t, &tmp)) + { if (logg_fp) fprintf(logg_fp, "Need to rotate log file due to size but could not get local time.\n"); @@ -241,14 +261,16 @@ static int rename_logg(STATBUF *sb) } strcpy(rotate_file, logg_file); - strftime(rotate_file+strlen(rotate_file), rotate_file_len-strlen(rotate_file), "-%Y%m%d_%H%M%S", &tmp); + strftime(rotate_file + strlen(rotate_file), rotate_file_len - strlen(rotate_file), "-%Y%m%d_%H%M%S", &tmp); - if (logg_fp) { + if (logg_fp) + { fclose(logg_fp); logg_fp = NULL; } - if (rename(logg_file, rotate_file)) { + if (rename(logg_file, rotate_file)) + { free(rotate_file); return -1; } @@ -261,30 +283,30 @@ static int logg_open(void) { STATBUF sb; - if(logg_file) - if(logg_size > 0) - if(CLAMSTAT(logg_file, &sb) != -1) - if(sb.st_size > logg_size) - if (rename_logg(&sb)) + if (logg_file) + if (logg_size > 0) + if (CLAMSTAT(logg_file, &sb) != -1) + if (sb.st_size > logg_size) + if (rename_logg(&sb)) return -1; - return 0; } void logg_close(void) { #if defined(USE_SYSLOG) && !defined(C_AIX) - if(logg_syslog) - closelog(); + if (logg_syslog) + closelog(); #endif #ifdef CL_THREAD_SAFE pthread_mutex_lock(&logg_mutex); #endif - if(logg_fp) { - fclose(logg_fp); - logg_fp = NULL; + if (logg_fp) + { + fclose(logg_fp); + logg_fp = NULL; } #ifdef CL_THREAD_SAFE pthread_mutex_unlock(&logg_mutex); @@ -312,31 +334,37 @@ void logg_close(void) */ int logg(const char *str, ...) { - va_list args; - char buffer[1025], *abuffer = NULL, *buff; - time_t currtime; - size_t len; - mode_t old_umask; + va_list args; + char buffer[1025], *abuffer = NULL, *buff; + time_t currtime; + size_t len; + mode_t old_umask; #ifdef F_WRLCK - struct flock fl; + struct flock fl; #endif if ((*str == '$' && logg_verbose < 2) || - (*str == '*' && !logg_verbose)) - return 0; + (*str == '*' && !logg_verbose)) + return 0; ARGLEN(args, str, len); - if(len <= sizeof(buffer)) { - len = sizeof(buffer); - buff = buffer; - } else { - abuffer = malloc(len); - if(!abuffer) { - len = sizeof(buffer); - buff = buffer; - } else { - buff = abuffer; - } + if (len <= sizeof(buffer)) + { + len = sizeof(buffer); + buff = buffer; + } + else + { + abuffer = malloc(len); + if (!abuffer) + { + len = sizeof(buffer); + buff = buffer; + } + else + { + buff = abuffer; + } } va_start(args, str); vsnprintf(buff, len, str, args); @@ -349,26 +377,32 @@ int logg(const char *str, ...) logg_open(); - if(!logg_fp && logg_file) { + if (!logg_fp && logg_file) + { old_umask = umask(0037); - if((logg_fp = fopen(logg_file, "at")) == NULL) { + if ((logg_fp = fopen(logg_file, "at")) == NULL) + { umask(old_umask); #ifdef CL_THREAD_SAFE pthread_mutex_unlock(&logg_mutex); #endif printf("ERROR: Can't open %s in append mode (check permissions!).\n", logg_file); - if(len > sizeof(buffer)) + if (len > sizeof(buffer)) free(abuffer); return -1; - } else umask(old_umask); + } + else + umask(old_umask); #ifdef F_WRLCK - if(logg_lock) { + if (logg_lock) + { memset(&fl, 0, sizeof(fl)); fl.l_type = F_WRLCK; - if(fcntl(fileno(logg_fp), F_SETLK, &fl) == -1) { + if (fcntl(fileno(logg_fp), F_SETLK, &fl) == -1) + { #ifdef EOPNOTSUPP - if(errno == EOPNOTSUPP) + if (errno == EOPNOTSUPP) printf("WARNING: File locking not supported (NFS?)\n"); else #endif @@ -377,8 +411,8 @@ int logg(const char *str, ...) pthread_mutex_unlock(&logg_mutex); #endif printf("ERROR: %s is locked by another process\n", logg_file); - if(len > sizeof(buffer)) - free(abuffer); + if (len > sizeof(buffer)) + free(abuffer); return -1; } } @@ -386,73 +420,91 @@ int logg(const char *str, ...) #endif } - if(logg_fp) { - char flush = !logg_noflush; - /* Need to avoid logging time for verbose messages when logverbose + if (logg_fp) + { + char flush = !logg_noflush; + /* Need to avoid logging time for verbose messages when logverbose is not set or we get a bunch of timestamps in the log without newlines... */ - if (logg_time && ((*buff != '*') || logg_verbose)) - { - char timestr[32]; - time(&currtime); - cli_ctime(&currtime, timestr, sizeof(timestr)); - /* cut trailing \n */ - timestr[strlen(timestr) - 1] = '\0'; - fprintf(logg_fp, "%s -> ", timestr); - } - - if(*buff == '!') { - fprintf(logg_fp, "ERROR: %s", buff + 1); - flush = 1; - } else if(*buff == '^') { - if(!logg_nowarn) - fprintf(logg_fp, "WARNING: %s", buff + 1); - flush = 1; - } else if(*buff == '*' || *buff == '$') { - fprintf(logg_fp, "%s", buff + 1); - } else if(*buff == '#' || *buff == '~') { - fprintf(logg_fp, "%s", buff + 1); - } else - fprintf(logg_fp, "%s", buff); - - if (flush) - fflush(logg_fp); - } - - if (logg_foreground) - { - if (buff[0] != '#') - { - if (logg_time) - { - char timestr[32]; - time(&currtime); - cli_ctime(&currtime, timestr, sizeof(timestr)); - /* cut trailing \n */ - timestr[strlen(timestr) - 1] = '\0'; - mprintf("%s -> %s", timestr, buff); - } - else - { - mprintf("%s", buff); - } - } - } + if (logg_time && ((*buff != '*') || logg_verbose)) + { + char timestr[32]; + time(&currtime); + cli_ctime(&currtime, timestr, sizeof(timestr)); + /* cut trailing \n */ + timestr[strlen(timestr) - 1] = '\0'; + fprintf(logg_fp, "%s -> ", timestr); + } -#if defined(USE_SYSLOG) && !defined(C_AIX) - if(logg_syslog) { - cli_chomp(buff); - if(buff[0] == '!') { - syslog(LOG_ERR, "%s", buff + 1); - } else if(buff[0] == '^') { - if(!logg_nowarn) - syslog(LOG_WARNING, "%s", buff + 1); - } else if(buff[0] == '*' || buff[0] == '$') { - syslog(LOG_DEBUG, "%s", buff + 1); - } else if(buff[0] == '#' || buff[0] == '~') { - syslog(LOG_INFO, "%s", buff + 1); - } else syslog(LOG_INFO, "%s", buff); + if (*buff == '!') + { + fprintf(logg_fp, "ERROR: %s", buff + 1); + flush = 1; + } + else if (*buff == '^') + { + if (!logg_nowarn) + fprintf(logg_fp, "WARNING: %s", buff + 1); + flush = 1; + } + else if (*buff == '*' || *buff == '$') + { + fprintf(logg_fp, "%s", buff + 1); + } + else if (*buff == '#' || *buff == '~') + { + fprintf(logg_fp, "%s", buff + 1); + } + else + fprintf(logg_fp, "%s", buff); + if (flush) + fflush(logg_fp); + } + + if (logg_foreground) + { + if (buff[0] != '#') + { + if (logg_time) + { + char timestr[32]; + time(&currtime); + cli_ctime(&currtime, timestr, sizeof(timestr)); + /* cut trailing \n */ + timestr[strlen(timestr) - 1] = '\0'; + mprintf("%s -> %s", timestr, buff); + } + else + { + mprintf("%s", buff); + } + } + } + +#if defined(USE_SYSLOG) && !defined(C_AIX) + if (logg_syslog) + { + cli_chomp(buff); + if (buff[0] == '!') + { + syslog(LOG_ERR, "%s", buff + 1); + } + else if (buff[0] == '^') + { + if (!logg_nowarn) + syslog(LOG_WARNING, "%s", buff + 1); + } + else if (buff[0] == '*' || buff[0] == '$') + { + syslog(LOG_DEBUG, "%s", buff + 1); + } + else if (buff[0] == '#' || buff[0] == '~') + { + syslog(LOG_INFO, "%s", buff + 1); + } + else + syslog(LOG_INFO, "%s", buff); } #endif @@ -460,31 +512,30 @@ int logg(const char *str, ...) pthread_mutex_unlock(&logg_mutex); #endif - if(len > sizeof(buffer)) - free(abuffer); + if (len > sizeof(buffer)) + free(abuffer); return 0; } void mprintf(const char *str, ...) { - va_list args; - FILE *fd; - char buffer[512], *abuffer = NULL, *buff; - size_t len; - + va_list args; + FILE *fd; + char buffer[512], *abuffer = NULL, *buff; + size_t len; - if(mprintf_disabled) - return; + if (mprintf_disabled) + return; fd = stdout; -/* legend: + /* legend: * ! - error * @ - error with logging * ... */ -/* + /* * ERROR WARNING STANDARD * normal stderr stderr stdout * @@ -494,17 +545,23 @@ void mprintf(const char *str, ...) */ ARGLEN(args, str, len); - if(len <= sizeof(buffer)) { - len = sizeof(buffer); - buff = buffer; - } else { - abuffer = malloc(len); - if(!abuffer) { - len = sizeof(buffer); - buff = buffer; - } else { - buff = abuffer; - } + if (len <= sizeof(buffer)) + { + len = sizeof(buffer); + buff = buffer; + } + else + { + abuffer = malloc(len); + if (!abuffer) + { + len = sizeof(buffer); + buff = buffer; + } + else + { + buff = abuffer; + } } va_start(args, str); vsnprintf(buff, len, str, args); @@ -512,64 +569,83 @@ void mprintf(const char *str, ...) buff[len - 1] = 0; #ifdef _WIN32 - do { - int tmplen = len + 1; - wchar_t *tmpw = malloc(tmplen*sizeof(wchar_t)); - char *nubuff; - if(!tmpw) break; - if(!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, buff, -1, tmpw, tmplen)) { - free(tmpw); - break; - } - /* FIXME CHECK IT'S REALLY UTF8 */ - nubuff = (char *)malloc(tmplen); - if(!nubuff) { - free(tmpw); - break; - } - if(!WideCharToMultiByte(CP_OEMCP, 0, tmpw, -1, nubuff, tmplen, NULL, NULL)) { - free(nubuff); - free(tmpw); - break; - } - free(tmpw); - if(len > sizeof(buffer)) - free(abuffer); - abuffer = buff = nubuff; - len = sizeof(buffer) + 1; - } while(0); -#endif - if(buff[0] == '!') { - if(!mprintf_stdout) - fd = stderr; - fprintf(fd, "ERROR: %s", &buff[1]); - } else if(buff[0] == '@') { - if(!mprintf_stdout) - fd = stderr; - fprintf(fd, "ERROR: %s", &buff[1]); - } else if(!mprintf_quiet) { - if(buff[0] == '^') { - if(!mprintf_nowarn) { - if(!mprintf_stdout) - fd = stderr; - fprintf(fd, "WARNING: %s", &buff[1]); - } - } else if(buff[0] == '*') { - if(mprintf_verbose) - fprintf(fd, "%s", &buff[1]); - } else if(buff[0] == '~') { - fprintf(fd, "%s", &buff[1]); - } else fprintf(fd, "%s", buff); + do + { + int tmplen = len + 1; + wchar_t *tmpw = malloc(tmplen * sizeof(wchar_t)); + char *nubuff; + if (!tmpw) + break; + if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, buff, -1, tmpw, tmplen)) + { + free(tmpw); + break; + } + /* FIXME CHECK IT'S REALLY UTF8 */ + nubuff = (char *)malloc(tmplen); + if (!nubuff) + { + free(tmpw); + break; + } + if (!WideCharToMultiByte(CP_OEMCP, 0, tmpw, -1, nubuff, tmplen, NULL, NULL)) + { + free(nubuff); + free(tmpw); + break; + } + free(tmpw); + if (len > sizeof(buffer)) + free(abuffer); + abuffer = buff = nubuff; + len = sizeof(buffer) + 1; + } while (0); +#endif + if (buff[0] == '!') + { + if (!mprintf_stdout) + fd = stderr; + fprintf(fd, "ERROR: %s", &buff[1]); + } + else if (buff[0] == '@') + { + if (!mprintf_stdout) + fd = stderr; + fprintf(fd, "ERROR: %s", &buff[1]); + } + else if (!mprintf_quiet) + { + if (buff[0] == '^') + { + if (!mprintf_nowarn) + { + if (!mprintf_stdout) + fd = stderr; + fprintf(fd, "WARNING: %s", &buff[1]); + } + } + else if (buff[0] == '*') + { + if (mprintf_verbose) + fprintf(fd, "%s", &buff[1]); + } + else if (buff[0] == '~') + { + fprintf(fd, "%s", &buff[1]); + } + else + fprintf(fd, "%s", buff); } - if(fd == stdout) - fflush(stdout); + if (fd == stdout) + fflush(stdout); - if(len > sizeof(buffer)) - free(abuffer); + if (len > sizeof(buffer)) + free(abuffer); } -struct facstruct { +struct facstruct +{ const char *name; int code; }; @@ -577,78 +653,77 @@ struct facstruct { #if defined(USE_SYSLOG) && !defined(C_AIX) static const struct facstruct facilitymap[] = { #ifdef LOG_AUTH - { "LOG_AUTH", LOG_AUTH }, + {"LOG_AUTH", LOG_AUTH}, #endif #ifdef LOG_AUTHPRIV - { "LOG_AUTHPRIV", LOG_AUTHPRIV }, + {"LOG_AUTHPRIV", LOG_AUTHPRIV}, #endif #ifdef LOG_CRON - { "LOG_CRON", LOG_CRON }, + {"LOG_CRON", LOG_CRON}, #endif #ifdef LOG_DAEMON - { "LOG_DAEMON", LOG_DAEMON }, + {"LOG_DAEMON", LOG_DAEMON}, #endif #ifdef LOG_FTP - { "LOG_FTP", LOG_FTP }, + {"LOG_FTP", LOG_FTP}, #endif #ifdef LOG_KERN - { "LOG_KERN", LOG_KERN }, + {"LOG_KERN", LOG_KERN}, #endif #ifdef LOG_LPR - { "LOG_LPR", LOG_LPR }, + {"LOG_LPR", LOG_LPR}, #endif #ifdef LOG_MAIL - { "LOG_MAIL", LOG_MAIL }, + {"LOG_MAIL", LOG_MAIL}, #endif #ifdef LOG_NEWS - { "LOG_NEWS", LOG_NEWS }, + {"LOG_NEWS", LOG_NEWS}, #endif #ifdef LOG_AUTH - { "LOG_AUTH", LOG_AUTH }, + {"LOG_AUTH", LOG_AUTH}, #endif #ifdef LOG_SYSLOG - { "LOG_SYSLOG", LOG_SYSLOG }, + {"LOG_SYSLOG", LOG_SYSLOG}, #endif #ifdef LOG_USER - { "LOG_USER", LOG_USER }, + {"LOG_USER", LOG_USER}, #endif #ifdef LOG_UUCP - { "LOG_UUCP", LOG_UUCP }, + {"LOG_UUCP", LOG_UUCP}, #endif #ifdef LOG_LOCAL0 - { "LOG_LOCAL0", LOG_LOCAL0 }, + {"LOG_LOCAL0", LOG_LOCAL0}, #endif #ifdef LOG_LOCAL1 - { "LOG_LOCAL1", LOG_LOCAL1 }, + {"LOG_LOCAL1", LOG_LOCAL1}, #endif #ifdef LOG_LOCAL2 - { "LOG_LOCAL2", LOG_LOCAL2 }, + {"LOG_LOCAL2", LOG_LOCAL2}, #endif #ifdef LOG_LOCAL3 - { "LOG_LOCAL3", LOG_LOCAL3 }, + {"LOG_LOCAL3", LOG_LOCAL3}, #endif #ifdef LOG_LOCAL4 - { "LOG_LOCAL4", LOG_LOCAL4 }, + {"LOG_LOCAL4", LOG_LOCAL4}, #endif #ifdef LOG_LOCAL5 - { "LOG_LOCAL5", LOG_LOCAL5 }, + {"LOG_LOCAL5", LOG_LOCAL5}, #endif #ifdef LOG_LOCAL6 - { "LOG_LOCAL6", LOG_LOCAL6 }, + {"LOG_LOCAL6", LOG_LOCAL6}, #endif #ifdef LOG_LOCAL7 - { "LOG_LOCAL7", LOG_LOCAL7 }, + {"LOG_LOCAL7", LOG_LOCAL7}, #endif - { NULL, -1 } -}; + {NULL, -1}}; int logg_facility(const char *name) { - int i; + int i; - for(i = 0; facilitymap[i].name; i++) - if(!strcmp(facilitymap[i].name, name)) - return facilitymap[i].code; + for (i = 0; facilitymap[i].name; i++) + if (!strcmp(facilitymap[i].name, name)) + return facilitymap[i].code; return -1; }