head	1.3;
access;
symbols;
locks; strict;
comment	@ * @;


1.3
date	92.06.30.21.18.46;	author mao;	state Exp;
branches;
next	1.2;

1.2
date	92.06.15.08.36.12;	author dpassage;	state Exp;
branches;
next	1.1;

1.1
date	92.01.17.05.22.58;	author clarsen;	state Exp;
branches;
next	;


desc
@baseline
@


1.3
log
@get database name for operation from the environment var DATABASE
@
text
@#include <stdio.h>
#include <sys/file.h>

#include "tmp/libpq-fs.h"

int errs;
void copy();
extern char	*getenv();
extern char	*PQexec();

void main(argc,argv)
     int argc;       
     char *argv[];
{
    int infd;
    int cnt, total = 0;
    int blen;
    int i;
    char *res;
    char *dbname;

    if ((dbname = getenv("DATABASE")) == (char *) NULL) {
	fprintf(stderr, "no database specified in env var DATABASE\n");
	fflush(stderr);
	exit (1);
    }

    PQsetdb(dbname);

    if (argc == 1)	{
	copy(0,1);
	exit(0);
    }

    res = PQexec("begin");
    for (i=1;i<argc;i++) {
         if (!strcmp(argv[i],"-")) {
	    copy(0,1);
	    continue;
	 }
         infd = p_open(argv[i],O_RDONLY);
	 if (infd < 0) {
		errs = 1;
		continue;
	 }
	 if (pgcopy(infd,1) < 0) errs = 1;
         p_close(infd);
    }

    res = PQexec("end");
    PQfinish();
}

void copy(i,o)
{
    char buf[2048];
    int n;
    while ((n = read(i,buf,sizeof(buf))) > 0) {
	write(o,buf,n);
    }
}

pgcopy(i,o)
{
    char buf[2048];
    int n;
    while ((n = p_read(i,buf,sizeof(buf))) > 0) {
	write(o,buf,n);
    }
    if (n < 0)
      return -1;
    else 
      return 0;
}
@


1.2
log
@wrap with begin and end
@
text
@d1 3
a4 1
#include  <sys/file.h>
d8 2
d20 7
d28 2
a29 1
    PQsetdb(getenv("USER"));
d49 1
@


1.1
log
@Initial revision
@
text
@d15 1
d23 1
d37 1
@
