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)
387387bool linedone ;
388388char copybuf [COPYBUFSIZ ];
389389char * s ;
390- int buflen ;
390+ int bufleft ;
391391int c = 0 ;
392392
393+ if (prompt )/* disable prompt if not interactive */
394+ {
395+ if (!isatty (fileno (copystream )))
396+ prompt = NULL ;
397+ }
398+
393399while (!copydone )
394400{/* for each input line ... */
395- if (prompt && isatty ( fileno ( copystream )) )
401+ if (prompt )
396402{
397403fputs (prompt ,stdout );
398404fflush (stdout );
399405}
400406firstload = true;
401407linedone = false;
402408while (!linedone )
403- {/* for eachbuffer ... */
409+ {/* for eachbufferload in line ... */
404410s = copybuf ;
405- for (buflen = COPYBUFSIZ ; buflen > 1 ; buflen -- )
411+ for (bufleft = COPYBUFSIZ - 1 ; bufleft > 0 ; bufleft -- )
406412{
407413c = getc (copystream );
408414if (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{
418424PQputline (conn ,"\\." );
419425copydone = true;
@@ -423,10 +429,10 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
423429if (firstload )
424430{
425431if (!strcmp (copybuf ,"\\." ))
426- {
432+ {
427433copydone = true;
428- break ;
429- }
434+ break ;
435+ }
430436firstload = false;
431437}
432438}