/*************************************************************************** * * svr_print.c * * Usage: svr_print [-v] [-n] [-s seconds] host port * -v prints the version number and exits * -n enables conversion of newlines to carriage-return/newline * -s specifies the number of seconds to sleep waiting for output * to drain * * * In the following instructions, replace the text PRINTERNAME with * a unique name that you have assigned to your printer, REMOTEADDR * with the IP address of the server, and REMOTEPORT with the TCP * port number of the telnet listener for your printer. * * * Wyse Unix SVR3: * * Compile: * cc -o svr_print svr_print.c * cp svr_print /usr/lib/svr_print * * Install printer: * /usr/lib/lpadmin -p PRINTERNAME -h -v/dev/null -mstandard * /usr/lib/accept PRINTERNAME * enable PRINTERNAME * cd /usr/spool/lp/admins/lp/interfaces * mv PRINTERNAME PRINTERNAME.if * cp /usr/lib/svr_interface PRINTERNAME * chmod a+x PRINTERNAME * Edit the file PRINTERNAME to specify the correct internet * address and TCP port number of the Telnet Listener * * Print a file: * lp -d PRINTERNAME files... * * * Wyse Unix SVR4: * * Compile: * cc -o svr_print svr_print.c -lsocket -lnsl * cp svr_print /usr/lib/svr_print * * Install printer: * Same as Wyse Unix SVR3 * * Print a file: * Same as Wyse Unix SVR3 * * * SUN OS: * * Compile: * cc -o svr_print svr_print.c * cp svr_print /usr/lib/svr_print * * Install printer filter: * Create a new file /usr/lib/svr_print.PRINTERNAME * #!/bin/sh * /usr/lib/svr_print -n REMOTEADDR REMOTEPORT * chmod a+x /usr/lib/svr_print.PRINTERNAME * * Install printer: * Edit "/etc/printcap" and add a new entry for your printer * PRINTERNAME:lp=/dev/null:of=/usr/lib/svr_print.PRINTERNAME: * * Print a file: * lpr -P PRINTERNAME files... * * **************************************************************************/ #include #include #include #include #include #include #include #include #define Write(fd, buf, count) \ { \ if (write(fd, buf, count) != count) { \ fprintf(stderr, "%s: ", argv[0]); \ perror("write"); \ exit(2); \ } \ } int nl_to_crnl; char buf[1024]; static char *rcsid = "$Revision: 1.6 $ $Date: 1997/05/28 06:23:44 $"; main(argc, argv) int argc; char **argv; { struct sockaddr_in sin; int sock, n, i; struct hostent *host; struct linger linger; int sleeptime = 0; char *whoami; whoami = argv[0]; while (argc > 1 && argv[1][0] == '-') { switch(argv[1][1]) { case 'v': printf("%s\n", rcsid); exit(0); case 'n': nl_to_crnl = 1; break; case 's': if (!isdigit(*argv[2])) goto usage; sleeptime = atoi(argv[2]); argc--; argv++; break; default: goto usage; } argc--; argv++; } if (argc != 3) { usage: fprintf(stderr, "usage: %s [-v] [-n] [-s seconds] host port\n", whoami); exit(1); } sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(argv[1]); if (sin.sin_addr.s_addr == -1) { host = gethostbyname(argv[1]); if (host == 0) { fprintf(stderr, "%s: host not found %s\n", argv[0], argv[1]); exit(1); } memcpy(&sin.sin_addr.s_addr, host->h_addr_list[0], host->h_length); } sin.sin_port = htons(atoi(argv[2])); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "%s: ", argv[0]); perror("socket"); exit(2); } linger.l_onoff = 1; linger.l_linger = 60; if (setsockopt(sock,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)) < 0) { fprintf(stderr, "%s: ", argv[0]); perror("setsockopt"); exit(2); } if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) { fprintf(stderr, "%s: ", argv[0]); perror("connect"); exit(2); } for (;;) { n = read(0, buf, sizeof(buf)); if (n <= 0) { if (n < 0) { fprintf(stderr, "%s: ", argv[0]); perror("read"); exit(2); } break; } if (nl_to_crnl) { int last_nl = 0; for (i=0; i last_nl) Write(sock, buf+last_nl, i-last_nl); Write(sock, "\r\n", 2); last_nl = i + 1; } if (i > last_nl) Write(sock, buf+last_nl, i-last_nl); } else Write(sock, buf, n); } sleep(sleeptime); close(sock); exit(0); }