Skip to content

Commit 765ad26

Browse files
committed
Fix pg_current_logfile() to not emit a carriage return on Windows.
Due to not having our signals straight about CRLF vs. LF line termination, the output of pg_current_logfile() included a trailing \r on Windows. To fix, force the file descriptor it uses into text mode. While here, move a couple of local variable declarations to make the function's logic clearer. In v12 and v13, also back-patch the test added by 1c4e88e so that this function has some test coverage. However, the 004_logrotate.pl test script doesn't exist before v12, and it didn't seem worth adding to older branches just for this. Per report from Thomas Kellerer. Back-patch to v10 where this function was added. Discussion: https://postgr.es/m/412ae8da-76bb-640f-039a-f3513499e53d@gmx.net
1 parent f03a34a commit 765ad26

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/backend/utils/adt/misc.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <sys/file.h>
1818
#include <signal.h>
1919
#include <dirent.h>
20+
#include <fcntl.h>
2021
#include <math.h>
2122
#include <unistd.h>
2223

@@ -910,9 +911,6 @@ pg_current_logfile(PG_FUNCTION_ARGS)
910911
FILE *fd;
911912
char lbuffer[MAXPGPATH];
912913
char *logfmt;
913-
char *log_filepath;
914-
char *log_format = lbuffer;
915-
char *nlpos;
916914

917915
/* The log format parameter is optional */
918916
if (PG_NARGS() == 0 || PG_ARGISNULL(0))
@@ -939,16 +937,23 @@ pg_current_logfile(PG_FUNCTION_ARGS)
939937
PG_RETURN_NULL();
940938
}
941939

940+
#ifdef WIN32
941+
/* syslogger.c writes CRLF line endings on Windows */
942+
_setmode(_fileno(fd), _O_TEXT);
943+
#endif
944+
942945
/*
943946
* Read the file to gather current log filename(s) registered by the
944947
* syslogger.
945948
*/
946949
while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
947950
{
948-
/*
949-
* Extract log format and log file path from the line; lbuffer ==
950-
* log_format, they share storage.
951-
*/
951+
char *log_format;
952+
char *log_filepath;
953+
char *nlpos;
954+
955+
/* Extract log format and log file path from the line. */
956+
log_format = lbuffer;
952957
log_filepath = strchr(lbuffer, ' ');
953958
if (log_filepath == NULL)
954959
{

0 commit comments

Comments
 (0)