Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b681bf9

Browse files
committed
Fix psql's COPY support to deal with \r\n line endings.
Andrew Dunstan, some further hacking by Tom Lane.
1 parent 2193121 commit b681bf9

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/bin/psql/copy.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.51 2004/08/13 14:47:23 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.52 2004/08/13 22:59:29 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
@@ -663,6 +663,7 @@ handleCopyIn(PGconn *conn, FILE *copystream)
663663
bool copydone = false;
664664
bool firstload;
665665
bool linedone;
666+
bool saw_cr = false;
666667
char copybuf[COPYBUFSIZ];
667668
char *s;
668669
int bufleft;
@@ -695,38 +696,56 @@ handleCopyIn(PGconn *conn, FILE *copystream)
695696

696697
while (!linedone)
697698
{ /* for each bufferload in line ... */
699+
/* Fetch string until \n, EOF, or buffer full */
698700
s = copybuf;
699701
for (bufleft = COPYBUFSIZ - 1; bufleft > 0; bufleft--)
700702
{
701703
c = getc(copystream);
702-
if (c == '\n' || c == EOF)
704+
if (c == EOF)
703705
{
704706
linedone = true;
705707
break;
706708
}
707709
*s++ = c;
710+
if (c == '\n')
711+
{
712+
linedone = true;
713+
break;
714+
}
715+
if (c == '\r')
716+
saw_cr = true;
708717
}
709718
*s = '\0';
719+
/* EOF with empty line-so-far? */
710720
if (c == EOF && s == copybuf && firstload)
711721
{
712-
PQputline(conn, "\\.");
722+
/*
723+
* We are guessing a little bit as to the right line-ending
724+
* here...
725+
*/
726+
if (saw_cr)
727+
PQputline(conn, "\\.\r\n");
728+
else
729+
PQputline(conn, "\\.\n");
713730
copydone = true;
714731
if (pset.cur_cmd_interactive)
715732
puts("\\.");
716733
break;
717734
}
735+
/* No, so pass the data to the backend */
718736
PQputline(conn, copybuf);
737+
/* Check for line consisting only of \. */
719738
if (firstload)
720739
{
721-
if (!strcmp(copybuf, "\\."))
740+
if (strcmp(copybuf, "\\.\n") == 0 ||
741+
strcmp(copybuf, "\\.\r\n") == 0)
722742
{
723743
copydone = true;
724744
break;
725745
}
726746
firstload = false;
727747
}
728748
}
729-
PQputline(conn, "\n");
730749
linecount++;
731750
}
732751
ret = !PQendcopy(conn);

0 commit comments

Comments
 (0)