xref: /freebsd/share/examples/printing/netprint (revision 63a938566d524836885917d95bd491aa4400b181)
1#!/usr/bin/perl
2#
3#  netprint - Text filter for printer attached to network
4#  Installed in /usr/local/libexec/netprint
5#
6
7$#ARGV eq 1 || die "Usage: $0 <printer-hostname> <port-number>";
8
9$printer_host = $ARGV[0];
10$printer_port = $ARGV[1];
11
12require 'sys/socket.ph';
13
14($ignore, $ignore, $protocol) = getprotobyname('tcp');
15($ignore, $ignore, $ignore, $ignore, $address)
16    = gethostbyname($printer_host);
17
18$sockaddr = pack('S n a4 x8', &AF_INET, $printer_port, $address);
19
20socket(PRINTER, &PF_INET, &SOCK_STREAM, $protocol)
21    || die "Can't create TCP/IP stream socket: $!";
22connect(PRINTER, $sockaddr) || die "Can't contact $printer_host: $!";
23while (<STDIN>) { print PRINTER; }
24exit 0;
25