| t@@ -0,0 +1,457 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#undef ctime
+#undef wait
+
+/* for Plan 9 */
+#ifdef PLAN9
+#define LP "/bin/lp"
+#define TMPDIR "/var/tmp"
+#define LPDAEMONLOG unsharp("#9/lp/log/lpdaemonl")
+#endif
+/* for Tenth Edition systems */
+#ifdef V10
+#define LP "/usr/bin/lp"
+#define TMPDIR "/tmp"
+#define LPDAEMONLOG "/tmp/lpdaemonl"
+#endif
+/* for System V or BSD systems */
+#if defined(SYSV) || defined(BSD)
+#define LP "/v/bin/lp"
+#define TMPDIR "/tmp"
+#define LPDAEMONLOG "/tmp/lpdaemonl"
+#endif
+
+#define ARGSIZ 4096
+#define NAMELEN 30
+
+unsigned char argvstr[ARGSIZ]; /* arguments after parsing */
+unsigned char *argvals[ARGSIZ/2+1]; /* pointers to arguments after parsing */
+int ascnt = 0, argcnt = 0; /* number of arguments parsed */
+/* for 'stuff' gleened from lpr cntrl file */
+struct jobinfo {
+ char user[NAMELEN+1];
+ char host[NAMELEN+1];
+} *getjobinfo();
+
+#define MIN(a,b) ((a 0; bsize -= rv) {
+ alarm(60);
+ if((rv=read(0, jobbuf, MIN(bsize,RDSIZE))) < 0) {
+ error("error reading input, %d unread\n", bsize);
+ exit(4);
+ } else if (rv == 0) {
+ error("connection closed prematurely\n");
+ exit(4);
+ } else if((write(outfd, jobbuf, rv)) != rv) {
+ error("error writing temp file, %d unread\n", bsize);
+ exit(5);
+ }
+ }
+ dbgstate = 3;
+ alarm(60);
+ if (((rv=read(0, jobbuf, 1))==1) && (*jobbuf=='\0')) {
+ alarm(60);
+ ACK();
+ dbgstate = 4;
+ alarm(0);
+ return(outfd);
+ }
+ alarm(0);
+ error("received bad status <%d> from sender\n", *jobbuf);
+ error("rv=%d\n", rv);
+ NAK();
+ return(-1);
+}
+
+/* reads a line from the input into lnbuf
+ * if there is no error, it returns
+ * the number of characters in the buffer
+ * if there is an error and there where characters
+ * read, it returns the negative value of the
+ * number of characters read
+ * if there is an error and no characters were read,
+ * it returns the negative value of 1 greater than
+ * the size of the line buffer
+ */
+int
+readline(int inpfd)
+{
+ unsigned char *ap;
+ int i, rv;
+
+ ap = lnbuf;
+ lnbuf[0] = '\0';
+ i = 0;
+ alarm(60);
+ do {
+ rv = read(inpfd, ap, 1);
+ } while (rv==1 && ++i && *ap != '\n' && ap++ && (i < LNBFSZ - 2));
+ alarm(0);
+ if (i != 0 && *ap != '\n') {
+ *++ap = '\n';
+ i++;
+ }
+ *++ap = '\0';
+ if (rv < 0) {
+ error("read error; lost connection\n");
+ if (i==0) i = -(LNBFSZ+1);
+ else i = -i;
+ }
+ return(i);
+}
+
+int
+getfiles(void)
+{
+ unsigned char *ap;
+ int filecnt, bsize, rv;
+
+ filecnt = 0;
+ /* get a line, hopefully containing a ctrl char, size, and name */
+ for(;;) {
+ ap = lnbuf;
+ if ((rv=readline(0)) < 0) NAK();
+ if (rv <= 0) {
+ return(filecnt);
+ }
+ switch(*ap++) {
+ case '\1': /* cleanup - data sent was bad (whatever that means) */
+ break;
+ case '\2': /* read control file */
+ bsize = atoi(ap);
+ cntrlfd = tempfile();
+ if (readfile(cntrlfd, bsize) < 0) {
+ close(cntrlfd);
+ NAK();
+ return(0);
+ }
+ break;
+ case '\3': /* read data file */
+ bsize = atoi(ap);
+ datafd[filecnt] = tempfile();
+ if (readfile(datafd[filecnt], bsize) < 0) {
+ close(datafd[filecnt]);
+ NAK();
+ return(0);
+ }
+ filecnt++;
+ break;
+ default:
+ error("protocol error <%d>\n", *(ap-1));
+ NAK();
+ }
+ }
+ return(filecnt);
+}
+
+struct jobinfo *
+getjobinfo(int fd)
+{
+ unsigned char *ap;
+ int rv;
+ static struct jobinfo info;
+
+ if (fd < 0) error("getjobinfo: bad file descriptor\n");
+ if (lseek(fd, 0L, 0) < 0) {
+ error("error seeking in temp file\n");
+ exit(7);
+ }
+ /* the following strings should be < NAMELEN or else they will not
+ * be null terminated.
+ */
+ strncpy(info.user, "daemon", NAMELEN);
+ strncpy(info.host, "nowhere", NAMELEN);
+ /* there may be a space after the name and host. It will be filtered out
+ * by CPYFIELD.
+ */
+ while ((rv=readline(fd)) > 0) {
+ ap = lnbuf;
+ ap[rv-1] = '\0'; /* remove newline from string */
+ switch (*ap) {
+ case 'H':
+ if (ap[1] == '\0')
+ strncpy(info.host, "unknown", NAMELEN);
+ else
+ strncpy(info.host, (const char *)&ap[1], NAMELEN);
+ info.host[strlen(info.host)] = '\0';
+ break;
+ case 'P':
+ if (ap[1] == '\0')
+ strncpy(info.user, "unknown", NAMELEN);
+ else
+ strncpy(info.user, (const char *)&ap[1], NAMELEN);
+ info.user[strlen(info.user)] = '\0';
+ break;
+ }
+ }
+ return(&info);
+}
+
+void
+alarmhandler(int sig) {
+ signal(sig, alarmhandler);
+ error("alarm at %d - %s\n", dbgstate, dbgstrings[dbgstate]);
+}
+
+void
+main(int argc, char **argv)
+{
+ unsigned char *ap, *bp, *cp, *savbufpnt;
+ int i, blen, rv, saveflg, savargcnt;
+ struct jobinfo *jinfop;
+
+ USED(argc);
+ USED(argv);
+
+ signal(SIGHUP, SIG_IGN);
+ signal(SIGALRM, alarmhandler);
+ cp = argvstr;
+ /* setup argv[0] for exec */
+ argvals[argcnt++] = cp;
+ for (bp = (unsigned char *)LP, i = 0; (*bp != '\0') && (i < ARGSIZ-1); *cp++ = *bp++, i++);
+ *cp++ = '\0';
+ /* get the first line sent and parse it as arguments for lp */
+ if ((rv=readline(0)) < 0)
+ exit(1);
+ bp = lnbuf;
+ /* setup the remaining arguments */
+ /* check for BSD style request */
+ /* ^A, ^B, ^C, ^D, ^E (for BSD lpr) */
+ switch (*bp) {
+ case '\001':
+ case '\003':
+ case '\004':
+ bp++; /* drop the ctrl character from the input */
+ argvals[argcnt++] = cp;
+ *cp++ = '-'; *cp++ = 'q'; *cp++ = '\0'; /* -q */
+ argvals[argcnt++] = cp;
+ *cp++ = '-'; *cp++ = 'd'; /* -d */
+ CPYFIELD(bp, cp); /* printer */
+ *cp++ = '\0';
+ break;
+ case '\002':
+ bp++; /* drop the ctrl character from the input */
+ argvals[argcnt++] = cp;
+ *cp++ = '-'; *cp++ = 'd'; /* -d */
+ CPYFIELD(bp, cp); /* printer */
+ *cp++ = '\0';
+ ACK();
+ savargcnt = argcnt;
+ savbufpnt = cp;
+ while ((rv=getfiles())) {
+ jinfop = getjobinfo(cntrlfd);
+ close(cntrlfd);
+ argcnt = savargcnt;
+ cp = savbufpnt;
+ argvals[argcnt++] = cp;
+ *cp++ = '-'; *cp++ = 'M'; /* -M */
+ bp = (unsigned char *)jinfop->host;
+ CPYFIELD(bp, cp); /* host name */
+ *cp++ = '\0';
+ argvals[argcnt++] = cp;
+ *cp++ = '-'; *cp++ = 'u'; /* -u */
+ bp = (unsigned char *)jinfop->user;
+ CPYFIELD(bp, cp); /* user name */
+ *cp++ = '\0';
+ for(i=0;i |
| t@@ -0,0 +1,428 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#define REDIALTIMEOUT 15
+#define TIMEOUT 600
+
+char tmpfilename[L_tmpnam+1];
+int alarmstate = 0;
+int debugflag = 0;
+int killflag = 0;
+int statflag = 0;
+
+void
+cleanup(void) {
+ unlink(tmpfilename);
+}
+
+#define SBSIZE 8192
+unsigned char sendbuf[SBSIZE];
+
+void
+debug(char *str) {
+ if (debugflag)
+ fprintf(stderr, "%s", str);
+}
+
+void
+alarmhandler(int sig) {
+ fprintf(stderr, "timeout occurred, check printer.\n");
+ exit(2);
+}
+
+/* send a message after each WARNPC percent of data sent */
+#define WARNPC 5
+
+int
+copyfile(int in, int out, long tosend) {
+ int n;
+ int sent = 0;
+ int percent = 0;
+
+ if (debugflag)
+ fprintf(stderr, "lpdsend: copyfile(%d,%d,%ld)\n",
+ in, out, tosend);
+ while ((n=read(in, sendbuf, SBSIZE)) > 0) {
+ if (debugflag)
+ fprintf(stderr, "lpdsend: copyfile read %d bytes from %d\n",
+ n, in);
+ alarm(TIMEOUT); alarmstate = 1;
+ if (write(out, sendbuf, n) != n) {
+ alarm(0);
+ fprintf(stderr, "write to fd %d failed\n", out);
+ return(0);
+ }
+ alarm(0);
+ if (debugflag)
+ fprintf(stderr, "lpdsend: copyfile wrote %d bytes to %d\n",
+ n, out);
+ sent += n;
+ if (tosend && ((sent*100/tosend)>=(percent+WARNPC))) {
+ percent += WARNPC;
+ fprintf(stderr, ": %5.2f%% sent\n", sent*100.0/tosend);
+ }
+ }
+ if (debugflag)
+ fprintf(stderr, "lpdsend: copyfile read %d bytes from %d\n",
+ n, in);
+ return(!n);
+}
+
+char strbuf[120];
+char hostname[MAXHOSTNAMELEN], *username, *printername, *killarg;
+char *inputname;
+char filetype = 'o'; /* 'o' is for PostScript */
+int seqno = 0;
+char *seqfilename;
+
+void
+killjob(int printerfd) {
+ int strlength;
+ if (printername==0) {
+ fprintf(stderr, "no printer name\n");
+ exit(1);
+ }
+ if (username==0) {
+ fprintf(stderr, "no user name given\n");
+ exit(1);
+ }
+ if (killarg==0) {
+ fprintf(stderr, "no job to kill\n");
+ exit(1);
+ }
+ sprintf(strbuf, "%c%s %s %s\n", '\5', printername, username, killarg);
+ strlength = strlen(strbuf);
+ if (write(printerfd, strbuf, strlength) != strlength) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ copyfile(printerfd, 2, 0L);
+}
+
+void
+checkqueue(int printerfd) {
+ int strlength;
+
+ sprintf(strbuf, "%c%s\n", '\4', printername);
+ strlength = strlen(strbuf);
+ if (write(printerfd, strbuf, strlength) != strlength) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ copyfile(printerfd, 2, 0L);
+/*
+{ int n;
+ unsigned char sendbuf[1];
+ while ((n=read(printerfd, sendbuf, 1)) > 0) {
+ write(2, sendbuf, n);
+ }
+}
+*/
+}
+
+void
+getack(int printerfd, int as) {
+ char resp;
+ int rv;
+
+ alarm(TIMEOUT); alarmstate = as;
+ if ((rv=read(printerfd, &resp, 1)) != 1 || resp != '\0') {
+ fprintf(stderr, "getack failed: read returned %d, read value (if any) %d, alarmstate=%d\n",
+ rv, resp, alarmstate);
+ exit(1);
+ }
+ alarm(0);
+}
+
+/* send control file */
+void
+sendctrl(int printerfd) {
+ char cntrlstrbuf[256];
+ int strlength, cntrlen;
+
+ sprintf(cntrlstrbuf, "H%s\nP%s\n%cdfA%3.3d%s\n", hostname, username, filetype, seqno, hostname);
+ cntrlen = strlen(cntrlstrbuf);
+ sprintf(strbuf, "%c%d cfA%3.3d%s\n", '\2', cntrlen, seqno, hostname);
+ strlength = strlen(strbuf);
+ if (write(printerfd, strbuf, strlength) != strlength) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ getack(printerfd, 3);
+ if (write(printerfd, cntrlstrbuf, cntrlen) != cntrlen) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ if (write(printerfd, "\0", 1) != 1) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ getack(printerfd, 4);
+}
+
+/* send data file */
+void
+senddata(int inputfd, int printerfd, long size) {
+ int strlength;
+
+ sprintf(strbuf, "%c%ld dfA%3.3d%s\n", '\3', size, seqno, hostname);
+ strlength = strlen(strbuf);
+ if (write(printerfd, strbuf, strlength) != strlength) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ getack(printerfd, 5);
+ if (!copyfile(inputfd, printerfd, size)) {
+ fprintf(stderr, "failed to send file to printer\n");
+ exit(1);
+ }
+ if (write(printerfd, "\0", 1) != 1) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ fprintf(stderr, "%ld bytes sent, status: waiting for end of job\n", size);
+ getack(printerfd, 6);
+}
+
+void
+sendjob(int inputfd, int printerfd) {
+ struct stat statbuf;
+ int strlength;
+
+ if (fstat(inputfd, &statbuf) < 0) {
+ fprintf(stderr, "fstat(%s) failed\n", inputname);
+ exit(1);
+ }
+ sprintf(strbuf, "%c%s\n", '\2', printername);
+ strlength = strlen(strbuf);
+ if (write(printerfd, strbuf, strlength) != strlength) {
+ fprintf(stderr, "write(printer) error\n");
+ exit(1);
+ }
+ getack(printerfd, 2);
+ debug("send data\n");
+ senddata(inputfd, printerfd, statbuf.st_size);
+ debug("send control info\n");
+ sendctrl(printerfd);
+ fprintf(stderr, "%ld bytes sent, status: end of job\n", (long)statbuf.st_size);
+}
+
+/*
+ * make an address, add the defaults
+ */
+char *
+netmkaddr(char *linear, char *defnet, char *defsrv)
+{
+ static char addr[512];
+ char *cp;
+
+ /*
+ * dump network name
+ */
+ cp = strchr(linear, '!');
+ if(cp == 0){
+ if(defnet==0){
+ if(defsrv)
+ sprintf(addr, "net!%s!%s", linear, defsrv);
+ else
+ sprintf(addr, "net!%s", linear);
+ }
+ else {
+ if(defsrv)
+ sprintf(addr, "%s!%s!%s", defnet, linear, defsrv);
+ else
+ sprintf(addr, "%s!%s", defnet, linear);
+ }
+ return addr;
+ }
+
+ /*
+ * if there is already a service, use it
+ */
+ cp = strchr(cp+1, '!');
+ if(cp)
+ return linear;
+
+ /*
+ * add default service
+ */
+ if(defsrv == 0)
+ return linear;
+ sprintf(addr, "%s!%s", linear, defsrv);
+
+ return addr;
+}
+
+void
+main(int argc, char *argv[]) {
+ int c, usgflg = 0;
+ char *desthostname;
+ int printerfd;
+ int inputfd;
+ int sendport;
+ char portstr[4];
+
+ desthostname = nil;
+ if (signal(SIGALRM, alarmhandler) == SIG_ERR) {
+ fprintf(stderr, "failed to set alarm handler\n");
+ exit(1);
+ }
+ while ((c = getopt(argc, argv, "Dd:k:qs:t:H:P:")) != -1)
+ switch (c) {
+ case 'D':
+ debugflag = 1;
+ debug("debugging on\n");
+ break;
+ case 'd':
+ printername = optarg;
+ break;
+ case 'k':
+ if (statflag) {
+ fprintf(stderr, "cannot have both -k and -q flags\n");
+ exit(1);
+ }
+ killflag = 1;
+ killarg = optarg;
+ break;
+ case 'q':
+ if (killflag) {
+ fprintf(stderr, "cannot have both -q and -k flags\n");
+ exit(1);
+ }
+ statflag = 1;
+ break;
+ case 's':
+ seqno = strtol(optarg, NULL, 10);
+ if (seqno < 0 || seqno > 999)
+ seqno = 0;
+ break;
+ case 't':
+ switch (filetype) {
+ case 'c':
+ case 'd':
+ case 'f':
+ case 'g':
+ case 'l':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'r':
+ case 't':
+ case 'v':
+ case 'z':
+ filetype = optarg[0];
+ break;
+ default:
+ usgflg++;
+ break;
+ }
+ break;
+ case 'H':
+ strncpy(hostname, optarg, MAXHOSTNAMELEN);
+ break;
+ case 'P':
+ username = optarg;
+ break;
+ default:
+ case '?':
+ fprintf(stderr, "unknown option %c\n", c);
+ usgflg++;
+ }
+ if (argc < 2) usgflg++;
+ if (optind < argc) {
+ desthostname = argv[optind++];
+ } else
+ usgflg++;
+ if (usgflg) {
+ fprintf(stderr, "usage: to send a job - %s -d printer -H hostname -P username [-s seqno] [-t[cdfgklnoprtvz]] desthost [filename]\n", argv[0]);
+ fprintf(stderr, " to check status - %s -d printer -q desthost\n", argv[0]);
+ fprintf(stderr, " to kill a job - %s -d printer -P username -k jobname desthost\n", argv[0]);
+ exit(1);
+ }
+
+/* make sure the file to send is here and ready
+ * otherwise the TCP connection times out.
+ */
+ inputfd = -1;
+ if (!statflag && !killflag) {
+ if (optind < argc) {
+ inputname = argv[optind++];
+ debug("open("); debug(inputname); debug(")\n");
+ inputfd = open(inputname, O_RDONLY);
+ if (inputfd < 0) {
+ fprintf(stderr, "open(%s) failed\n", inputname);
+ exit(1);
+ }
+ } else {
+ inputname = "stdin";
+ tmpnam(tmpfilename);
+ debug("using stdin\n");
+ if ((inputfd = create(tmpfilename, ORDWR, 0600)) < 0) {
+ fprintf(stderr, "open(%s) failed\n", tmpfilename);
+ exit(1);
+ }
+ atexit(cleanup);
+ debug("copy input to temp file ");
+ debug(tmpfilename);
+ debug("\n");
+ if (!copyfile(0, inputfd, 0L)) {
+ fprintf(stderr, "failed to copy file to temporary file\n");
+ exit(1);
+ }
+ if (lseek(inputfd, 0L, 0) < 0) {
+ fprintf(stderr, "failed to seek back to the beginning of the temporary file\n");
+ exit(1);
+ }
+ }
+ }
+
+ sprintf(strbuf, "%s", netmkaddr(desthostname, "tcp", "printer"));
+ fprintf(stderr, "connecting to %s\n", strbuf);
+ for (sendport=721; sendport<=731; sendport++) {
+ sprintf(portstr, "%3.3d", sendport);
+ fprintf(stderr, " trying from port %s...", portstr);
+ debug(" dial("); debug(strbuf); debug(", "); debug(portstr); debug(", 0, 0) ...");
+ printerfd = dial(strbuf, portstr, 0, 0);
+ if (printerfd >= 0) {
+ fprintf(stderr, "connected\n");
+ break;
+ }
+ fprintf(stderr, "failed\n");
+ sleep(REDIALTIMEOUT);
+ }
+ if (printerfd < 0) {
+ fprintf(stderr, "Cannot open a valid port!\n");
+ fprintf(stderr, "- All source ports [721-731] may be busy.\n");
+ fprintf(stderr, "- Is recipient ready and online?\n");
+ fprintf(stderr, "- If all else fails, cycle the power!\n");
+ exit(1);
+ }
+/* hostname[8] = '\0'; */
+#ifndef PLAN9
+ if (gethostname(hostname, sizeof(hostname)) < 0) {
+ perror("gethostname");
+ exit(1);
+ }
+#endif
+/* char *hnend;
+ if ((hnend = strchr(hostname, '.')) != NULL)
+ *hnend = '\0';
+ */
+ if (statflag) {
+ checkqueue(printerfd);
+ } else if (killflag) {
+ killjob(printerfd);
+ } else {
+ sendjob(inputfd, printerfd);
+ }
+ exit(0);
+} |
| t@@ -0,0 +1,341 @@
+#ifdef plan9
+
+#include
+#include
+#define stderr 2
+
+#define RDNETIMEOUT 60000
+#define WRNETIMEOUT 60000
+
+#else
+
+/* not for plan 9 */
+#include
+#include
+#include
+#include
+#include
+
+#define create creat
+#define seek lseek
+#define fprint fprintf
+#define sprint sprintf
+#define exits exit
+
+#define ORDWR O_RDWR
+#define OTRUNC O_TRUNC
+#define ORCLOSE 0
+
+#define RDNETIMEOUT 60
+#define WRNETIMEOUT 60
+
+#endif
+
+#define MIN(a,b) ((a 0; bcnt -= rv) {
+ alarm(WRNETIMEOUT); /* to break hanging */
+ if((rv=read(inpfd, jobbuf, MIN(bcnt,RDSIZE))) < 0) {
+ error(0, "read error during pass, %d remaining\n", bcnt);
+ break;
+ } else if((write(outfd, jobbuf, rv)) != rv) {
+ error(0, "write error during pass, %d remaining\n", bcnt);
+ break;
+ }
+ }
+ alarm(0);
+ return(bcnt);
+}
+
+/* get whatever stdin has and put it into the temporary file.
+ * return the file size.
+ */
+int
+prereadfile(int inpfd)
+{
+ int rv, bsize;
+
+ bsize = 0;
+ do {
+ if((rv=read(0, jobbuf, RDSIZE))<0) {
+ error(0, "read error while making temp file\n");
+ exits("read error while making temp file");
+ } else if((write(inpfd, jobbuf, rv)) != rv) {
+ error(0, "write error while making temp file\n");
+ exits("write error while making temp file");
+ }
+ bsize += rv;
+ } while (rv!=0);
+ return(bsize);
+}
+
+int
+tempfile(void)
+{
+ static int tindx = 0;
+ char tmpf[20];
+ int tmpfd;
+
+ sprint(tmpf, "/var/tmp/lp%d.%d", getpid(), tindx++);
+ if((tmpfd=create(tmpf,
+
+#ifdef plan9
+
+ ORDWR|OTRUNC,
+
+#endif
+
+ 0666)) < 0) {
+ error(0, "cannot create temp file %s\n", tmpf);
+ exits("cannot create temp file");
+ }
+ close(tmpfd);
+ if((tmpfd=open(tmpf, ORDWR
+
+#ifdef plan9
+
+ |ORCLOSE|OTRUNC
+
+#endif
+
+ )) < 0) {
+ error(0, "cannot open temp file %s\n", tmpf);
+ exits("cannot open temp file");
+ }
+ return(tmpfd);
+}
+
+int
+recvACK(int netfd)
+{
+ int rv;
+
+ *jobbuf = '\0';
+ alarm(RDNETIMEOUT);
+ if (read(netfd, jobbuf, 1)!=1 || *jobbuf!='\0') {
+ error(0, "failed to receive ACK, ");
+ if (*jobbuf == '\0')
+ error(1, "read failed\n");
+ else
+ error(1, "received <0x%x> instead\n", *jobbuf);
+ rv = 0;
+ } else rv = 1;
+ alarm(0);
+ return(rv);
+}
+
+void
+main(int argc, char *argv[])
+{
+ char *devdir;
+ int i, rv, netfd, bsize;
+ int datafd;
+
+#ifndef plan9
+
+ void (*oldhandler)();
+
+#endif
+
+ devdir = nil;
+ /* make connection */
+ if (argc != 2) {
+ fprint(stderr, "usage: %s network!destination!service\n", argv[0]);
+ exits("incorrect number of arguments");
+ }
+
+ /* read options line from stdin into lnbuf */
+ i = readline(0);
+
+ /* read stdin into tempfile to get size */
+ datafd = tempfile();
+ bsize = prereadfile(datafd);
+
+ /* network connection is opened after data is in to avoid timeout */
+ if ((netfd=dial(argv[1], 0, 0, 0)) < 0) {
+ fprint(stderr, "dialing %s\n", devdir);
+ perror("dial");
+ exits("can't dial");
+ }
+
+ /* write out the options we read above */
+ if (write(netfd, lnbuf, i) != i) {
+ error(0, "write error while sending options\n");
+ exits("write error while sending options");
+ }
+
+ /* send the size of the file to be sent */
+ sprint(lnbuf, "%d\n", bsize);
+ i = strlen(lnbuf);
+ if ((rv=write(netfd, lnbuf, i)) != i) {
+ perror("write error while sending size");
+ error(0, "write returned %d\n", rv);
+ exits("write error while sending size");
+ }
+
+ if (seek(datafd, 0L, 0) < 0) {
+ error(0, "error seeking temp file\n");
+ exits("seek error");
+ }
+ /* mirror performance in readfile() in lpdaemon */
+
+#ifdef plan9
+
+ atnotify(alarmhandler, 1);
+
+#else
+
+ oldhandler = signal(SIGALRM, alarmhandler);
+
+#endif
+
+ dbgstate = 1;
+ if(!recvACK(netfd)) {
+ error(0, "failed to receive ACK before sending data\n");
+ exits("recv ack1 failed");
+ }
+ dbgstate = 2;
+ if ((i=pass(datafd, netfd, bsize)) != 0) {
+ NAK(netfd);
+ error(0, "failed to send %d bytes\n", i);
+ exits("send data failed");
+ }
+ ACK(netfd);
+ dbgstate = 3;
+ if(!recvACK(netfd)) {
+ error(0, "failed to receive ACK after sending data\n");
+ exits("recv ack2 failed");
+ }
+
+ /* get response, as from lp -q */
+ dbgstate = 4;
+ while((rv=read(netfd, jobbuf, RDSIZE)) > 0) {
+ if((write(1, jobbuf, rv)) != rv) {
+ error(0, "write error while sending to stdout\n");
+ exits("write error while sending to stdout");
+ }
+ }
+ dbgstate = 5;
+
+#ifdef plan9
+
+ atnotify(alarmhandler, 0);
+ /* close down network connections and go away */
+ exits("");
+
+#else
+
+ signal(SIGALRM, oldhandler);
+ exit(0);
+
+#endif
+
+} |