+++ /dev/null
---- logrotate.c
-+++ logrotate.c
-@@ -260,6 +260,111 @@
- return 0;
- }
-
-+int mail_old_log(logInfo * log, int logNum, char * firstRotated, char * disposeName, FILE * errorFile, char * compext) {
-+ int hasErrors = 0;
-+ char * command;
-+ char * mailFilename;
-+ int mailfile_is_compressed;
-+
-+ if (log->flags & LOG_FLAG_MAILFIRST)
-+ mailFilename = firstRotated;
-+ else
-+ mailFilename = disposeName;
-+
-+ mailfile_is_compressed = (log->flags & LOG_FLAG_COMPRESS)
-+ && ((log->flags & (LOG_FLAG_DELAYCOMPRESS|LOG_FLAG_MAILFIRST))
-+ != (LOG_FLAG_DELAYCOMPRESS|LOG_FLAG_MAILFIRST));
-+
-+ if (!mailfile_is_compressed
-+ && !strcmp(compext, mailFilename+strlen(mailFilename)-strlen(compext)))
-+ {
-+ char *ctmp;
-+ ctmp = alloca(strlen(mailFilename));
-+ strcpy(ctmp, mailFilename);
-+ ctmp[strlen(ctmp)-strlen(compext)] = '\0';
-+ mailFilename = ctmp;
-+ }
-+
-+ if (mailFilename) {
-+ char * escapedMailFilename = escapeSingleQuotes(mailFilename);
-+ if (mailfile_is_compressed) {
-+ char * escapedFN = escapeSingleQuotes(log->files[logNum]);
-+ command = alloca(strlen(log->uncompress_prog) +
-+ strlen(escapedMailFilename) +
-+ strlen(mailCommand) +
-+ strlen(escapedFN) +
-+ strlen(log->logAddress) + 20);
-+ sprintf(command, "%s < '%s' | %s '%s' %s",
-+ log->uncompress_prog, escapedMailFilename,
-+ mailCommand, escapedFN, log->logAddress);
-+ free(escapedFN);
-+ } else {
-+ command = alloca(strlen(mailCommand) +
-+ strlen(escapedMailFilename) +
-+ strlen(log->logAddress) +
-+ strlen(escapedMailFilename) + 20);
-+ sprintf(command, "%s '%s' %s < '%s'", mailCommand,
-+ escapedMailFilename, log->logAddress, escapedMailFilename);
-+ }
-+ free(escapedMailFilename);
-+
-+ message(MESS_DEBUG, "executing: \"%s\"\n", command);
-+
-+ if (!debug && system(command)) {
-+ /* sprintf(newName, "%s.%d", log->files[logNum], (unsigned int)getpid()); */
-+ fprintf(errorFile, "Failed to mail %s to %s!\n",
-+ mailFilename, log->logAddress);
-+
-+ hasErrors = 1;
-+ }
-+ }
-+ return hasErrors;
-+}
-+
-+int remove_old_log(logInfo * log, char * disposeName, FILE * errorFile)
-+{
-+ int hasErrors = 0;
-+ message(MESS_DEBUG, "removing old log %s\n", disposeName);
-+
-+ if (!debug && unlink(disposeName)) {
-+ fprintf(errorFile, "Failed to remove old log %s: %s\n",
-+ disposeName, strerror(errno));
-+ hasErrors = 1;
-+ }
-+ return hasErrors;
-+}
-+
-+int compress_log(logInfo * log, char * fileName, const char * action, FILE * errorFile)
-+{
-+ int hasErrors = 0;
-+ struct stat sbprev;
-+
-+ if (stat(fileName, &sbprev)) {
-+ message(MESS_DEBUG, "%s log %s does not exist\n",
-+ action, fileName);
-+ } else {
-+ char * escapedName;
-+ char * command;
-+
-+ escapedName = escapeSingleQuotes(fileName);
-+ command = alloca(strlen(log->compress_prog) +
-+ strlen(log->compress_options) +
-+ strlen(escapedName) + 21);
-+ sprintf(command, "%s %s '%s'", log->compress_prog,
-+ log->compress_options, escapedName);
-+ free(escapedName);
-+ message(MESS_DEBUG, "compressing previous log with: %s\n",
-+ command);
-+ if (!debug && system(command)) {
-+ fprintf(errorFile,
-+ "failed to compress %s log %s\n", action, fileName);
-+ hasErrors = 1;
-+ }
-+ }
-+
-+ return hasErrors;
-+}
-+
- int findNeedRotating(logInfo * log, int logNum, logState ** statesPtr,
- int * numStatesPtr) {
- struct stat sb;
-@@ -418,31 +523,8 @@
- /* First compress the previous log when necessary */
- if (log->flags & LOG_FLAG_COMPRESS &&
- log->flags & LOG_FLAG_DELAYCOMPRESS) {
-- struct stat sbprev;
--
-- sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
-- if (stat(oldName, &sbprev)) {
-- message(MESS_DEBUG, "previous log %s does not exist\n",
-- oldName);
-- } else {
-- char * escapedName;
-- char * command;
--
-- escapedName = escapeSingleQuotes(oldName);
-- command = alloca(strlen(log->compress_prog) +
-- strlen(log->compress_options) +
-- strlen(escapedName) + 10);
-- sprintf(command, "%s %s '%s'", log->compress_prog,
-- log->compress_options, escapedName);
-- free(escapedName);
-- message(MESS_DEBUG, "compressing previous log with: %s\n",
-- command);
-- if (!debug && system(command)) {
-- fprintf(errorFile,
-- "failed to compress previous log %s\n", oldName);
-- hasErrors = 1;
-- }
-- }
-+ sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
-+ hasErrors = compress_log(log, oldName, "previous", errorFile);
- }
-
- sprintf(oldName, "%s/%s.%d%s%s", dirName, baseName,
-@@ -613,92 +695,15 @@
- if (!hasErrors &&
- (log->flags & LOG_FLAG_COMPRESS) &&
- !(log->flags & LOG_FLAG_DELAYCOMPRESS)) {
-- char * escapedName;
-- char * command;
--
-- escapedName = escapeSingleQuotes(finalName);
-- command = alloca(strlen(log->compress_prog) +
-- strlen(log->compress_options) +
-- strlen(escapedName) + 10);
-- sprintf(command, "%s %s '%s'", log->compress_prog,
-- log->compress_options, escapedName);
-- free(escapedName);
-- message(MESS_DEBUG, "compressing new log with: %s\n", command);
-- if (!debug && system(command)) {
-- fprintf(errorFile, "failed to compress log %s\n",
-- finalName);
-- hasErrors = 1;
-- }
-+ hasErrors = compress_log(log, finalName, "new", errorFile);
- }
-
- if (!hasErrors && log->logAddress) {
-- char * command;
-- char * mailFilename;
-- int mailfile_is_compressed;
--
-- if (log->flags & LOG_FLAG_MAILFIRST)
-- mailFilename = firstRotated;
-- else
-- mailFilename = disposeName;
--
-- mailfile_is_compressed = (log->flags & LOG_FLAG_COMPRESS)
-- && ((log->flags & (LOG_FLAG_DELAYCOMPRESS|LOG_FLAG_MAILFIRST))
-- != (LOG_FLAG_DELAYCOMPRESS|LOG_FLAG_MAILFIRST));
--
-- if (!mailfile_is_compressed
-- && !strcmp(compext,
-- mailFilename+strlen(mailFilename)-strlen(compext)))
-- {
-- char *ctmp;
-- ctmp = alloca(strlen(mailFilename));
-- strcpy(ctmp, mailFilename);
-- ctmp[strlen(ctmp)-strlen(compext)] = '\0';
-- mailFilename = ctmp;
-- }
--
-- if (mailFilename) {
-- char * escapedMailFilename = escapeSingleQuotes(mailFilename);
-- if (mailfile_is_compressed) {
-- char * escapedFN = escapeSingleQuotes(log->files[logNum]);
-- command = alloca(strlen(log->uncompress_prog) +
-- strlen(escapedMailFilename) +
-- strlen(mailCommand) +
-- strlen(escapedFN) +
-- strlen(log->logAddress) + 20);
-- sprintf(command, "%s < '%s' | %s '%s' %s",
-- log->uncompress_prog, escapedMailFilename,
-- mailCommand, escapedFN, log->logAddress);
-- free(escapedFN);
-- } else {
-- command = alloca(strlen(mailCommand) +
-- strlen(escapedMailFilename) +
-- strlen(log->logAddress) +
-- strlen(escapedMailFilename) + 20);
-- sprintf(command, "%s '%s' %s < '%s'", mailCommand,
-- escapedMailFilename, log->logAddress, escapedMailFilename);
-- }
-- free(escapedMailFilename);
--
-- message(MESS_DEBUG, "executing: \"%s\"\n", command);
--
-- if (!debug && system(command)) {
-- sprintf(newName, "%s.%d", log->files[logNum], (unsigned int)getpid());
-- fprintf(errorFile, "Failed to mail %s to %s!\n",
-- mailFilename, log->logAddress);
--
-- hasErrors = 1;
-- }
-- }
-+ hasErrors = mail_old_log(log, logNum, firstRotated, disposeName, errorFile, compext);
- }
-
- if (!hasErrors && disposeName) {
-- message(MESS_DEBUG, "removing old log %s\n", disposeName);
--
-- if (!debug && unlink(disposeName)) {
-- fprintf(errorFile, "Failed to remove old log %s: %s\n",
-- disposeName, strerror(errno));
-- hasErrors = 1;
-- }
-+ hasErrors = remove_old_log(log, disposeName, errorFile);
- }
- }
-