Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e33f550

Browse files
committed
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.
1 parent 5b7bc48 commit e33f550

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/bin/psql/copy.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Team
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.7 2000/01/20 21:51:09 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.8 2000/01/21 04:21:12 tgl Exp $
77
*/
88
#include <c.h>
99
#include "copy.h"
@@ -318,7 +318,7 @@ do_copy(const char *args)
318318

319319

320320
/*
321-
* handeCopyOut
321+
* handleCopyOut
322322
* receives data as a result of a COPY ... TO stdout command
323323
*
324324
* 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)
367367

368368

369369
/*
370-
* handeCopyOut
370+
* handleCopyIn
371371
* receives data as a result of a COPY ... FROM stdin command
372372
*
373373
* Again, if you want to use COPY FROM in your application, copy this.
@@ -387,22 +387,28 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
387387
bool linedone;
388388
char copybuf[COPYBUFSIZ];
389389
char *s;
390-
int buflen;
390+
int bufleft;
391391
int c = 0;
392392

393+
if (prompt) /* disable prompt if not interactive */
394+
{
395+
if (! isatty(fileno(copystream)))
396+
prompt = NULL;
397+
}
398+
393399
while (!copydone)
394400
{ /* for each input line ... */
395-
if (prompt && isatty(fileno(copystream)))
401+
if (prompt)
396402
{
397403
fputs(prompt, stdout);
398404
fflush(stdout);
399405
}
400406
firstload = true;
401407
linedone = false;
402408
while (!linedone)
403-
{ /* for each buffer ... */
409+
{ /* for each bufferload in line ... */
404410
s = copybuf;
405-
for (buflen = COPYBUFSIZ; buflen > 1; buflen--)
411+
for (bufleft = COPYBUFSIZ-1; bufleft > 0; bufleft--)
406412
{
407413
c = getc(copystream);
408414
if (c == '\n' || c == EOF)
@@ -413,7 +419,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
413419
*s++ = c;
414420
}
415421
*s = '\0';
416-
if (c == EOF)
422+
if (c == EOF && s == copybuf && firstload)
417423
{
418424
PQputline(conn, "\\.");
419425
copydone = true;
@@ -423,10 +429,10 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
423429
if (firstload)
424430
{
425431
if (!strcmp(copybuf, "\\."))
426-
{
432+
{
427433
copydone = true;
428-
break;
429-
}
434+
break;
435+
}
430436
firstload = false;
431437
}
432438
}

0 commit comments

Comments
 (0)