Technical article 127616

How to print to a network printer other than HP

OpenServer 6.0.0OpenServer 5.0.7UnixWare 7.1.4

Written · Last revised

Problem

I have a network printer but I can't use the hpnpcfg utility

Solution

To add a network printer to your system perform the following steps (we will assume that the new printer is called "netprn".)

1. Add a new local printer with "scoadmin printer", defining the device as /dev/null instead of a serial or parallel port, and the model as "standard". You may set it as the default printer.

NOTE:        For UnixWare, add a local file printer as described in TA 126173 -
             "How can I configure UnixWare 7 to print to a file?"
2. Replace the interface script file:
/usr/spool/lp/admins/lp/interfaces/netprn

with a new interface file as follows:

-----
                #!/bin/sh
                # netprint model interface script
trap '' 15
printer=`basename $0`
# set variables for various options passed
# from spool system
user=$2
options=$5
copies=$4
shift; shift; shift; shift; shift
files="$*"
netprint_binary=/usr/spool/lp/bin/netprint
LOG=/tmp/$printer.$$.log
port=9100
#print required number of copies of file(s):-
i=1
while [ $i -le $copies ]
do
     for file in $files
     do
          $netprint_binary "$file" "$printer" $port >> \
              $LOG 2>&1  && rm $LOG
     done
         i=`expr $i + 1`
done
exit 0
-----

3. Add the printer-name to /etc/hosts with a corresponding IP address, eg:

192.168.0.123   netprn
4. # cc netprn.c -lsocket -s -o /usr/spool/lp/bin/netprint
     /* Source for netprn.c begins here */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
/*
print input file to network port, optionally:
changing any single lf to cr/lf - any cr/lf sequences found are left
as is, or outputting 'as is' ('r')
suppressing any initial form-feed in character position 1 (by replacing
with a carriage-return ('s')
outputting a final form-feed ('f')
NOTE: the required destination MUST be either defined in /etc/hosts
or available from a nameserver, in order to be identified, even if
specified as an IP-address
*/
static void catch_alarm();
static void open_socket();
int wrtfile;
main(argc, argv)
int argc;
char *argv[];
{
int rdfile, rdsize, wrtsize,wrterr,netdest,timer, raw=0;
int ffeed=0, nfeed=0;
char *mems, *memd, *memdi, *host;
register char *memss, *memend;
register char a;
register char b;
if (argc < 4)
 {
 fprintf(stderr,
 "usage: netprint   file   printer   port  timeout  [frs]\n");
 fprintf(stderr,
 "  'file' may be '-' for stdin\n");
 fprintf(stderr,
 "  'printer' must be a host-name or IP address \
available either in /etc/hosts or    from a nameserver\n");
 fprintf(stderr,
 "  'port' is typically 9100 (see printer documentation)\n");
 fprintf(stderr,
 "  timeout in seconds (0 defaults to 20 seconds)\n");
 fprintf(stderr,
 "  optional 'f' = send form-feed at end of print\n");
 fprintf(stderr,
 "  optional 's' = suppress any form-feed at start of file\n");
 fprintf(stderr,
 "  optional 'r' = raw mode - do not convert lf to cr/lf\n");
 fprintf(stderr,
 "  timeout and options may be omitted\n");
 exit(1);
 }
if (*(*++argv) == '-')
rdfile=fileno(stdin);
else
rdfile=open(*argv, O_RDONLY);
if (rdfile == -1)
 {
 fprintf(stderr,"unable to open input file  %s\n",argv[0]);
 exit(2);
 }
sigset(SIGALRM,catch_alarm);

host=*++argv; netdest=atoi(*++argv); if (argc > 4) { timer=atoi(*++argv); if (timer == 0) timer=20; if (argc > 5) { mems=*++argv; while (*mems) { if (*mems == 'r') raw=1; else if (*mems == 'f') ffeed=1; else if (*mems == 's') nfeed=1; mems++; } } } else timer=20;

open_socket(host,netdest,timer);
mems=malloc(1024);
memdi=malloc(2048);
b=0;
while ((rdsize=read(rdfile,mems,1024)) > 0)
{
if (nfeed && *mems == '\f')
 {
 *mems='\r';
 nfeed=0;
 }
if (raw)
 {
     wrterr=write(wrtfile,mems,rdsize);
     if (wrterr < 0)
       {
       fprintf(stderr,"write failure to %s\n",host);
       exit(5);
       }
 }
 else
 {
memss=mems;
memd=memdi;
memend=mems + rdsize;
while (memss < memend)
   {
 a=*memss++;
 if (a == 0x0d)
  {
  *memd++=a;
  if (memss < memend)
   {
   *memd++=*memss++;
   a=*memd;
   }
  }
 else
 {
 if (a == 0x0a && b != 0x0d)
        {
         *memd++=0x0d;
         *memd++=0x0a;
        }
 else
        {
         *memd++ = a;
        }
   }
 b=a;  /* save last char processed in case 0x0d end on a block!*/
 }
wrtsize=memd-memdi;
if (wrtsize > 0)
    {
     wrterr=write(wrtfile,memdi,wrtsize);
       if (wrterr < 0)
       {
       fprintf(stderr,"write failure to %s\n",host);
       exit(5);
       }
    }
   }
}
if (ffeed) write(wrtfile,"\f",1);
close(rdfile);
close(wrtfile);
exit(0);
}

void open_socket(char *hostname, int port, int alarm_time) { int a; struct sockaddr_in sock; struct hostent *hp;

hp = gethostbyname(hostname); if (hp == NULL) { fprintf(stderr,"unable to identify network printer %s\n",hostname); fprintf(stderr,"- check if %s is included in /etc/hosts\n",hostname); fprintf(stderr,"if %s is not resolvable from a nameserver\n",hostname); exit(3); }

if ( ( wrtfile=socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
    {
    fprintf(stderr,"unable to create network socket\n");
    exit(4);
    }

memset(&sock, 0, sizeof(struct sockaddr_in)); sock.sin_family=AF_INET; sock.sin_port=htons(port); a=sizeof(struct sockaddr_in); memcpy(&sock.sin_addr, hp->h_addr_list[0], hp->h_length);

alarm(alarm_time); if (connect(wrtfile, (struct sockaddr *) &sock, a) < 0) { printf("unable to connect to Network Printer %s\n",hostname); exit(5); } alarm(0); }

void catch_alarm() { return; } /* Source for netprn.c ends here */

You can download a pre-compiled version from:

ftp://ftpput.sco.com/tmp/support/tools/netprint

Save this to /usr/spool/lp/bin/netprint and set the permissions:

# chmod 111 /usr/spool/lp/bin/netprint
# chown lp:lp /usr/spool/lp/bin/netprint

Now you should be ready to print. Try it with:

# netprint  <file-name>   ip-address-of-printer  9100  5

netprint has to be in your $PATH. The 5 is optional - it defines timeout in seconds for the connection to the printer.

There is a 'r' option to permit 'raw' mode which does not convert line-feeds (this, with the pipe facility, allows, for example, postcript files to be piped through a suitable filter to add the necessary commands to the printer).

So for example:
# cat ps_start FILE ps_end | netprint 192.168.0.4 9100 0 r

ps_start and ps_end are little files containing the strings required to put the printer in postscript mode and revert to normal mode afterwards.

NOTES:    This only works using the standard 'lp' system rather than cups

From the Xinuos technical article database, record 127616. Reproduced with permission. The original is at www.sco.com/ta/127616, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.