From 98e9775a3ad56754de1caa2558c66a0040933460 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sun, 11 Jul 2004 13:29:16 +0000
Subject: [PATCH] Use standard macro for psql binary file open.  Add comment
 explaining control-z requirement.

---
 src/bin/psql/command.c  |  6 +++---
 src/bin/psql/common.h   | 14 +-------------
 src/bin/psql/copy.c     |  4 ++--
 src/bin/psql/psqlscan.l |  4 ++--
 src/include/c.h         |  9 ++++++++-
 5 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index b9ee1944367..9b45b96bbf4 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.118 2004/07/11 00:54:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.119 2004/07/11 13:29:15 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -1197,7 +1197,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
 	if (!error)
 	{
 #endif
-		stream = fopen(fname, R_TEXTFILE);
+		stream = fopen(fname, PG_BINARY_R);
 		if (!stream)
 		{
 			psql_error("%s: %s\n", fname, strerror(errno));
@@ -1262,7 +1262,7 @@ process_file(char *filename)
 	if (!filename)
 		return false;
 
-	fd = fopen(filename, R_TEXTFILE);
+	fd = fopen(filename, PG_BINARY_R);
 
 	if (!fd)
 	{
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index b2e6236855f..35d67828015 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.36 2004/07/11 00:54:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.37 2004/07/11 13:29:15 momjian Exp $
  */
 #ifndef COMMON_H
 #define COMMON_H
@@ -62,16 +62,4 @@ extern char parse_char(char **buf);
 
 extern char *expand_tilde(char **filename);
 
-/*
- *	WIN32 treats Control-Z as EOF in files opened in text mode.
- *	Therefore, we open files in binary mode on Win32 so we can read
- *	literal control-Z.  The other affect is that we see CRLF, but
- *	that is OK because we can already handle those cleanly.
- */
-#ifndef WIN32
-#define R_TEXTFILE	"r"
-#else
-#define R_TEXTFILE	"rb"
-#endif
-
 #endif   /* COMMON_H */
diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c
index cc899bf8796..54bc7aea684 100644
--- a/src/bin/psql/copy.c
+++ b/src/bin/psql/copy.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.48 2004/07/11 00:54:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.49 2004/07/11 13:29:15 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "copy.h"
@@ -516,7 +516,7 @@ do_copy(const char *args)
 	if (options->from)
 	{
 		if (options->file)
-			copystream = fopen(options->file, R_TEXTFILE);
+			copystream = fopen(options->file, PG_BINARY_R);
 		else if (!options->psql_inout)
  			copystream = pset.cur_cmd_source;
 		else
diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l
index c6d93daba8d..d0cad5f0bb6 100644
--- a/src/bin/psql/psqlscan.l
+++ b/src/bin/psql/psqlscan.l
@@ -31,7 +31,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.4 2004/07/11 00:54:55 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.5 2004/07/11 13:29:15 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1284,7 +1284,7 @@ psql_scan_slash_option(PsqlScanState state,
 				char		buf[512];
 				size_t		result;
 
-				fd = popen(cmd, R_TEXTFILE);
+				fd = popen(cmd, PG_BINARY_R);
 				if (!fd)
 				{
 					psql_error("%s: %s\n", cmd, strerror(errno));
diff --git a/src/include/c.h b/src/include/c.h
index 17ec1855e18..0d191c36178 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -12,7 +12,7 @@
  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/c.h,v 1.165 2004/05/21 05:08:03 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/c.h,v 1.166 2004/07/11 13:29:16 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -671,6 +671,13 @@ typedef NameData *Name;
  * ----------------------------------------------------------------
  */
 
+/*
+ *	NOTE:  this is also used for opening text files.
+ *  WIN32 treats Control-Z as EOF in files opened in text mode.
+ *  Therefore, we open files in binary mode on Win32 so we can read
+ *  literal control-Z.  The other affect is that we see CRLF, but
+ *  that is OK because we can already handle those cleanly.
+ */
 #if defined(__CYGWIN__) || defined(WIN32)
 #define PG_BINARY	O_BINARY
 #define PG_BINARY_R "rb"
-- 
GitLab