From e33f550bc06a0410ae5a64ad16cc29f8ed5a882f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 21 Jan 2000 04:21:12 +0000 Subject: [PATCH] Fix handleCopyIn's response to EOF seen mid-line, that is, input file does not end with a newline. I don't think this explains the recent complaints, since this bug existed in 6.5 (and probably long before). But might as well fix it now that I see it. --- src/bin/psql/copy.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 140aa983fa..d3a5f79b5c 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Team * - * $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.7 2000/01/20 21:51:09 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.8 2000/01/21 04:21:12 tgl Exp $ */ #include #include "copy.h" @@ -318,7 +318,7 @@ do_copy(const char *args) /* - * handeCopyOut + * handleCopyOut * receives data as a result of a COPY ... TO stdout command * * If you want to use COPY TO in your application, this is the code to steal :) @@ -367,7 +367,7 @@ handleCopyOut(PGconn *conn, FILE *copystream) /* - * handeCopyOut + * handleCopyIn * receives data as a result of a COPY ... FROM stdin command * * Again, if you want to use COPY FROM in your application, copy this. @@ -387,12 +387,18 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt) bool linedone; char copybuf[COPYBUFSIZ]; char *s; - int buflen; + int bufleft; int c = 0; + if (prompt) /* disable prompt if not interactive */ + { + if (! isatty(fileno(copystream))) + prompt = NULL; + } + while (!copydone) { /* for each input line ... */ - if (prompt && isatty(fileno(copystream))) + if (prompt) { fputs(prompt, stdout); fflush(stdout); @@ -400,9 +406,9 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt) firstload = true; linedone = false; while (!linedone) - { /* for each buffer ... */ + { /* for each bufferload in line ... */ s = copybuf; - for (buflen = COPYBUFSIZ; buflen > 1; buflen--) + for (bufleft = COPYBUFSIZ-1; bufleft > 0; bufleft--) { c = getc(copystream); if (c == '\n' || c == EOF) @@ -413,7 +419,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt) *s++ = c; } *s = '\0'; - if (c == EOF) + if (c == EOF && s == copybuf && firstload) { PQputline(conn, "\\."); copydone = true; @@ -423,10 +429,10 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt) if (firstload) { if (!strcmp(copybuf, "\\.")) - { + { copydone = true; - break; - } + break; + } firstload = false; } } -- 2.11.0