13 #define REDIALTIMEOUT 15
16 char tmpfilename[L_tmpnam+1];
28 unsigned char sendbuf[SBSIZE];
33 fprintf(stderr, "%s", str);
37 alarmhandler(int sig) {
38 fprintf(stderr, "timeout occurred, check printer.\n");
42 /* send a message after each WARNPC percent of data sent */
46 copyfile(int in, int out, long tosend) {
52 fprintf(stderr, "lpdsend: copyfile(%d,%d,%ld)\n",
54 while ((n=read(in, sendbuf, SBSIZE)) > 0) {
56 fprintf(stderr, "lpdsend: copyfile read %d bytes from %d\n",
58 alarm(TIMEOUT); alarmstate = 1;
59 if (write(out, sendbuf, n) != n) {
61 fprintf(stderr, "write to fd %d failed\n", out);
66 fprintf(stderr, "lpdsend: copyfile wrote %d bytes to %d\n",
69 if (tosend && ((sent*100/tosend)>=(percent+WARNPC))) {
71 fprintf(stderr, ": %5.2f%% sent\n", sent*100.0/tosend);
75 fprintf(stderr, "lpdsend: copyfile read %d bytes from %d\n",
81 char hostname[MAXHOSTNAMELEN], *username, *printername, *killarg;
83 char filetype = 'o'; /* 'o' is for PostScript */
88 killjob(int printerfd) {
91 fprintf(stderr, "no printer name\n");
95 fprintf(stderr, "no user name given\n");
99 fprintf(stderr, "no job to kill\n");
102 sprintf(strbuf, "%c%s %s %s\n", '\5', printername, username, killarg);
103 strlength = strlen(strbuf);
104 if (write(printerfd, strbuf, strlength) != strlength) {
105 fprintf(stderr, "write(printer) error\n");
108 copyfile(printerfd, 2, 0L);
112 checkqueue(int printerfd) {
115 sprintf(strbuf, "%c%s\n", '\4', printername);
116 strlength = strlen(strbuf);
117 if (write(printerfd, strbuf, strlength) != strlength) {
118 fprintf(stderr, "write(printer) error\n");
121 copyfile(printerfd, 2, 0L);
124 unsigned char sendbuf[1];
125 while ((n=read(printerfd, sendbuf, 1)) > 0) {
126 write(2, sendbuf, n);
133 getack(int printerfd, int as) {
137 alarm(TIMEOUT); alarmstate = as;
138 if ((rv=read(printerfd, &resp, 1)) != 1 || resp != '\0') {
139 fprintf(stderr, "getack failed: read returned %d, read value (if any) %d, alarmstate=%d\n",
140 rv, resp, alarmstate);
146 /* send control file */
148 sendctrl(int printerfd) {
149 char cntrlstrbuf[256];
150 int strlength, cntrlen;
152 sprintf(cntrlstrbuf, "H%s\nP%s\n%cdfA%3.3d%s\n", hostname, username, filetype, seqno, hostname);
153 cntrlen = strlen(cntrlstrbuf);
154 sprintf(strbuf, "%c%d cfA%3.3d%s\n", '\2', cntrlen, seqno, hostname);
155 strlength = strlen(strbuf);
156 if (write(printerfd, strbuf, strlength) != strlength) {
157 fprintf(stderr, "write(printer) error\n");
160 getack(printerfd, 3);
161 if (write(printerfd, cntrlstrbuf, cntrlen) != cntrlen) {
162 fprintf(stderr, "write(printer) error\n");
165 if (write(printerfd, "\0", 1) != 1) {
166 fprintf(stderr, "write(printer) error\n");
169 getack(printerfd, 4);
174 senddata(int inputfd, int printerfd, long size) {
177 sprintf(strbuf, "%c%ld dfA%3.3d%s\n", '\3', size, seqno, hostname);
178 strlength = strlen(strbuf);
179 if (write(printerfd, strbuf, strlength) != strlength) {
180 fprintf(stderr, "write(printer) error\n");
183 getack(printerfd, 5);
184 if (!copyfile(inputfd, printerfd, size)) {
185 fprintf(stderr, "failed to send file to printer\n");
188 if (write(printerfd, "\0", 1) != 1) {
189 fprintf(stderr, "write(printer) error\n");
192 fprintf(stderr, "%ld bytes sent, status: waiting for end of job\n", size);
193 getack(printerfd, 6);
197 sendjob(int inputfd, int printerfd) {
201 if (fstat(inputfd, &statbuf) < 0) {
202 fprintf(stderr, "fstat(%s) failed\n", inputname);
205 sprintf(strbuf, "%c%s\n", '\2', printername);
206 strlength = strlen(strbuf);
207 if (write(printerfd, strbuf, strlength) != strlength) {
208 fprintf(stderr, "write(printer) error\n");
211 getack(printerfd, 2);
212 debug("send data\n");
213 senddata(inputfd, printerfd, statbuf.st_size);
214 debug("send control info\n");
216 fprintf(stderr, "%ld bytes sent, status: end of job\n", (long)statbuf.st_size);
220 * make an address, add the defaults
223 netmkaddr(char *linear, char *defnet, char *defsrv)
225 static char addr[512];
231 cp = strchr(linear, '!');
235 sprintf(addr, "net!%s!%s", linear, defsrv);
237 sprintf(addr, "net!%s", linear);
241 sprintf(addr, "%s!%s!%s", defnet, linear, defsrv);
243 sprintf(addr, "%s!%s", defnet, linear);
249 * if there is already a service, use it
251 cp = strchr(cp+1, '!');
256 * add default service
260 sprintf(addr, "%s!%s", linear, defsrv);
266 main(int argc, char *argv[]) {
275 if (signal(SIGALRM, alarmhandler) == SIG_ERR) {
276 fprintf(stderr, "failed to set alarm handler\n");
279 while ((c = getopt(argc, argv, "Dd:k:qs:t:H:P:")) != -1)
283 debug("debugging on\n");
286 printername = optarg;
290 fprintf(stderr, "cannot have both -k and -q flags\n");
298 fprintf(stderr, "cannot have both -q and -k flags\n");
304 seqno = strtol(optarg, NULL, 10);
305 if (seqno < 0 || seqno > 999)
322 filetype = optarg[0];
330 strncpy(hostname, optarg, MAXHOSTNAMELEN);
337 fprintf(stderr, "unknown option %c\n", c);
340 if (argc < 2) usgflg++;
342 desthostname = argv[optind++];
346 fprintf(stderr, "usage: to send a job - %s -d printer -H hostname -P username [-s seqno] [-t[cdfgklnoprtvz]] desthost [filename]\n", argv[0]);
347 fprintf(stderr, " to check status - %s -d printer -q desthost\n", argv[0]);
348 fprintf(stderr, " to kill a job - %s -d printer -P username -k jobname desthost\n", argv[0]);
352 /* make sure the file to send is here and ready
353 * otherwise the TCP connection times out.
356 if (!statflag && !killflag) {
358 inputname = argv[optind++];
359 debug("open("); debug(inputname); debug(")\n");
360 inputfd = open(inputname, O_RDONLY);
362 fprintf(stderr, "open(%s) failed\n", inputname);
368 debug("using stdin\n");
369 if ((inputfd = create(tmpfilename, ORDWR, 0600)) < 0) {
370 fprintf(stderr, "open(%s) failed\n", tmpfilename);
374 debug("copy input to temp file ");
377 if (!copyfile(0, inputfd, 0L)) {
378 fprintf(stderr, "failed to copy file to temporary file\n");
381 if (lseek(inputfd, 0L, 0) < 0) {
382 fprintf(stderr, "failed to seek back to the beginning of the temporary file\n");
388 sprintf(strbuf, "%s", netmkaddr(desthostname, "tcp", "printer"));
389 fprintf(stderr, "connecting to %s\n", strbuf);
390 for (sendport=721; sendport<=731; sendport++) {
391 sprintf(portstr, "%3.3d", sendport);
392 fprintf(stderr, " trying from port %s...", portstr);
393 debug(" dial("); debug(strbuf); debug(", "); debug(portstr); debug(", 0, 0) ...");
394 printerfd = dial(strbuf, portstr, 0, 0);
395 if (printerfd >= 0) {
396 fprintf(stderr, "connected\n");
399 fprintf(stderr, "failed\n");
400 sleep(REDIALTIMEOUT);
403 fprintf(stderr, "Cannot open a valid port!\n");
404 fprintf(stderr, "- All source ports [721-731] may be busy.\n");
405 fprintf(stderr, "- Is recipient ready and online?\n");
406 fprintf(stderr, "- If all else fails, cycle the power!\n");
409 /* hostname[8] = '\0'; */
411 if (gethostname(hostname, sizeof(hostname)) < 0) {
412 perror("gethostname");
417 if ((hnend = strchr(hostname, '.')) != NULL)
421 checkqueue(printerfd);
422 } else if (killflag) {
425 sendjob(inputfd, printerfd);