[comp.protocols.nfs] lpq source code for PCNFS

paul@sdgsun.uucp (Paul Emerson) (01/15/91)

Ok, for Karl Buck and the rest here it is:

----------------------- Cut Here -------------------------------
#!/bin/sh
# This is a shell archive (shar 3.32)
# made 01/14/1991 20:14 UTC by paul@sdgsun
# Source directory /home/sdgsun/paul/tmp/post
#
# existing files WILL be overwritten
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   4200 -rwxrwxrwx lpq.c
#    159 -rwxrwxrwx makefile
#   1246 -rw-r--r-- readme
#
if touch 2>&1 | fgrep 'amc' > /dev/null
 then TOUCH=touch
 else TOUCH=true
fi
# ============= lpq.c ==============
echo "x - extracting lpq.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > lpq.c &&
X/*
X**------------------------------------------------------------------
X**  lpq for PCNFS.
X**
X**  This is a quick hack at lpq for the PC.  To compile it you must
X**  have SUN's NFS Toolkit.  To run it you must have PCNFS.  I am
X**  sure with a few hacks, the FTP Software Toolkit could be used,
X**  I don't have it so I can't say for sure.  This is one of my
X**  first attempts a writing some network code.  There is a problem
X**  with issuing two lpq command quickly, but a hack has fixed this
X**  problem. 
X**  I dropped this project because it really didn't make much sense 
X**  to write lpq when, on my PC I can issue: "rsh my_host lpq" and 
X**  I have a BSD lpq.  If it makes you feel better, just put it in 
X**  a bat named lpq.bat file. 
X** 
X**  If you want to know all about lpq and his friends lpd, lpr then
X**  I suggest Unix Network Programming by Stevens.  I didn't have
X**  this book when I wrote this code orginally, it sure would have
X**  been helpful. 
X** 
X**  sdgsun!paul@bikini.cis.ufl.edu
X**            or
X**  uunet!tarpit!sdgsun!paul 
X**
X**------------------------------------------------------------------
X**  Copyright Paul J. Emerson
X**------------------------------------------------------------------
X*/
X#include <sys/types.h>
X#include <sys/socket.h>
X#include <netinet/in.h>
X#include <netdb.h>
X#define _SIZE_T_DEFINED
X#include <stdio.h>
X
X#define SHORT_LIST 3
X#define LONG_LIST  4
X
Xextern int errno;
X
Xmain(argc, argv)
Xint argc;
Xchar **argv;
X{
Xstruct sockaddr_in server;
Xint s, i, p, q;
Xstruct servent *sp;
Xstruct protoent *pp;
Xstruct hostent *hp;
Xint num;
Xchar buffer[256];
Xchar printer[35];
Xchar host[35];
X
X	/* Quick and dirty arg stuff.  This needs to be done right */
X
X	switch (argc)
X	       {
X	       case 1:
X	            strcpy(host,getenv("LPRHOST"));
X	            strcpy(printer,getenv("LPRDEFAULT"));
X	            break;
X	       case 2:
X	            /* Just Hostname */
X	            strcpy(host,argv[1]);
X	            strcpy(printer,"lp");
X	            break;
X	       case 3:
X	           /* Hostname and printer */
X	           strcpy(host,argv[1]);
X	           strcpy(printer,argv[2]);
X	            break;
X	       default:
X	            printf("Usage: lpq host [printer]\n");
X	            exit(1);
X	       }
X
X	if ((hp = gethostbyname(host)) == NULL) 
X	   {
X	   fprintf(stderr, "lpq: unknown host %s\n",argv[1]);
X	   exit(1);
X	   }
X
X	if ((sp = getservbyname("printer", "tcp")) != NULL) 
X	   p = sp->s_port;
X	else
X	   p = 515; 
X
X	if ((pp = getprotobyname("tcp")) != NULL) 
X	   q = pp->p_proto;
X	else
X	   q = 6; 
X
X	if ((s = socket(AF_INET, SOCK_STREAM, q)) == 0) 
X	   {
X	   perror("lpq: socket");
X	   exit(1);
X	   }
X
X	bzero((char *)&server, sizeof (server));
X	bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
X
X	server.sin_family = AF_INET;
X	server.sin_port = p;
X
X	if (bind(s,(char *)&server,sizeof(server)) < 0)
X	   {
X	   perror("lpq: bind");
X	   exit(1);
X	   }
X
X	if (connect(s, &server, sizeof (server)) < 0) 
X	   {
X	   printf("Error: %d\n",errno);
X	   perror("lpq: connect");
X	   exit(1);
X	   }
X
X	sprintf(buffer,"%c%s\n",SHORT_LIST,printer);
X	write (s, buffer, strlen(buffer));
X
X	while(num = read(s, buffer, sizeof(buffer))) 
X	     {
X	     printf("%*.*s",num,num,buffer);
X	     }
X
X	if (shutdown(s,2) < 0)
X	   {
X	   printf("Error: Shutdown fails: %d\n",errno);
X	   exit(1);
X	   }
X
X	if (close(s) < 0)
X	   {
X	   printf("Error: Close fails: %d\n",errno);
X	   exit(1);
X	   }
X
X	/* 
X	**---------------------------------------------------
X	**  The following code fixes a problem of issuing two
X	**  lpq request quickly.  I am not sure how to correctly      
X	**  fix the problem, but re-connecting to the port
X	**  fixes it.  This is dirty but it works.
X	**  If you want to see the error just comment out 
X        **  the rest of the code, up to the exit(0).   
X	**---------------------------------------------------
X	*/
X
X	if ((s = socket(AF_INET, SOCK_STREAM, q)) == 0) 
X	   {
X	   perror("lpq: socket");
X	   exit(1);
X	   }
X
X	if (bind(s,(char *)&server,sizeof(server)) < 0)
X	   {
X	   perror("lpq: bind");
X	   exit(1);
X	   }
X
X	connect(s, &server, sizeof (server));
X
X	if (close(s) < 0)
X	   {
X	   printf("Error: Close fails: %d\n",errno);
X	   exit(1);
X	   }
X
X	exit(0);
X}
SHAR_EOF
$TOUCH -am 0114144791 lpq.c &&
chmod 0777 lpq.c ||
echo "restore of lpq.c failed"
set `wc -c lpq.c`;Wc_c=$1
if test "$Wc_c" != "4200"; then
	echo original size 4200, current size $Wc_c
fi
# ============= makefile ==============
echo "x - extracting makefile (Text)"
sed 's/^X//' << 'SHAR_EOF' > makefile &&
X#
X# nmake makefile
X#
Xlpq.exe: lpq.obj
X	link /ST:16384 lpq c:\toolkit\myp_rtns.obj c:\toolkit\mprotrtn.obj,,,c:\toolkit\mlibtk
X
Xlpq.obj: lpq.c
X	cl -c /AM lpq.c
SHAR_EOF
$TOUCH -am 0114144791 makefile &&
chmod 0777 makefile ||
echo "restore of makefile failed"
set `wc -c makefile`;Wc_c=$1
if test "$Wc_c" != "159"; then
	echo original size 159, current size $Wc_c
fi
# ============= readme ==============
echo "x - extracting readme (Text)"
sed 's/^X//' << 'SHAR_EOF' > readme &&
X
XThis is one of my first attempts at using Sun's PCNFS Toolkit and
Xnetwork programming.  I hacked this code together around December 1989.
XSince then, "Unix Network Programming" by Stevens has appeared.  The
Xlpr and lpq protocols are describe in this book.  This code works but
Xit isn't pretty.  It could use some work in processing and handling 
Xvarious features of lpq like: -l for long listing.  At present only the
Xsort listing is generated, but just change the SHORT_LIST define for
XLONG_LIST and there you go.  I don't have time to play with this code 
Xbut it should be difficult modify.  
X
XThere is a problem with two lpq attempts issued quickly one after
Xthe other.  Netstat shows the connection's state as: TIME_WAIT,
Xeventually the connection is closed.  I just re-opend the connection
Xand then closed it and the problem goes away.  I bet it is due to
Xsomething simple I am missing.
X
XThis lpq code can be a good exercise, but it doesn't make alot of
Xsense when you can issue a real BSD lpq request with rsh.  Just try:
X
XC:\> rsh host_name lpr  < your_file
X
XC:\> rsh host_name lpq  
X
XJust stick the command line in a bat file and call it lpq.bat!
X
X Paul Emerson
X
X sdgsun!paul@bikini.cis.ufl.edu
X            or
X uunet!tarpit!sdgsun!paul 
SHAR_EOF
$TOUCH -am 0114151391 readme &&
chmod 0644 readme ||
echo "restore of readme failed"
set `wc -c readme`;Wc_c=$1
if test "$Wc_c" != "1246"; then
	echo original size 1246, current size $Wc_c
fi
exit 0
-- 
Paul J. Emerson                           SDG Division of SAIC 
Senior Technical Manager                  450 Lakemont Ave.
UUCP:     uunet|tarpit!sdgsun!paul        Winter Park, FL 32792
Internet: sdgsun!paul@bikini.cis.ufl.edu  (407) 657-1300

rhoward@msd.gatech.edu (Robert L. Howard) (01/15/91)

Does anyone want to comlile this and post the binary (EXE)?

I think a lot of us don't have the toolkit....

Robert
--
| Robert L. Howard             |    Georgia Tech Research Institute     |
| rhoward@msd.gatech.edu       |    MATD Laboratory                     |
| (404) 528-7165               |    Atlanta, Georgia  30332             |
|     UUCP:   ...!{allegra,amd,hplabs,ut-ngp}!gatech!msd!rhoward        |
-------------------------------------------------------------------------
|       "They [La Prensa] accused us of suppressing freedom of          |
|        expression.  This was a lie and we could not let them          |
|        publish it." -- Nelba Blandon, Director of Censorship          |