home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / APPS / LPQ.C < prev    next >
Text File  |  1994-12-01  |  4KB  |  183 lines

  1. /*
  2.  * Lpq - query printer
  3.  *
  4.  *   Copyright (C) 1991, University of Waterloo
  5.  *
  6.  *   Portions Copyright (C) 1990, National Center for Supercomputer Applications
  7.  *   and portions copyright (c) 1990, Clarkson University
  8.  *
  9.  *   This program is free software; you can redistribute it and/or modify
  10.  *   it, but you may not sell it.
  11.  *
  12.  *   This program is distributed in the hope that it will be useful,
  13.  *   but without any warranty; without even the implied warranty of
  14.  *   merchantability or fitness for a particular purpose.
  15.  *
  16.  *       Erick Engelke                   or via E-Mail
  17.  *       Faculty of Engineering
  18.  *       University of Waterloo          Erick@development.watstar.uwaterloo.ca
  19.  *       200 University Ave.,
  20.  *       Waterloo, Ont., Canada
  21.  *       N2L 3G1
  22.  *
  23.  *  If you want to use this, make sure you are in /etc/host.lpr or better.
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <tcp.h>
  31.  
  32. #define SHORT_LIST 3
  33. #define LONG_LIST  4
  34.  
  35. #define LPQ_PORT 515
  36. #define LOCAL_PORT 722
  37.  
  38. extern int errno;
  39.  
  40. void usage( void )
  41. {
  42.     printf("usage: lpq [-Pprinter] [-Sserver] [-l]\n");
  43.     exit(1);
  44. }
  45.  
  46. int main(int argc, char **argv)
  47. {
  48.     char buffer[513];
  49.     char printer[35];
  50.     char host[35];
  51.     char *cptr;
  52.  
  53.     static tcp_Socket socketdata;
  54.     tcp_Socket *s;
  55.     longword host_ip;
  56.     int status;
  57.     int connected = 0;
  58.     int localport;
  59.     int len;
  60.     int i;
  61.     int verbose=0;
  62.     /* */
  63.  
  64.     /* Set defaults from the environment */
  65.     *host = *printer = '\0';
  66.     if( (cptr = getenv("LPRSRV")) != NULL )
  67.         strncpy(host,cptr,35);
  68.     if( (cptr = getenv("PRINTER")) != NULL )
  69.         strncpy(printer,cptr,35);
  70.  
  71.  
  72.     /* Parse the command line arguments */
  73.     for(i=1; i<=argc ; i++)
  74.     {
  75.     if(argv[i][0] == '-')
  76.     {
  77.         switch(argv[i][1])
  78.           {
  79.             case 'P':
  80.                 if(strlen(argv[i])>2)
  81.                 {
  82.                     strcpy(printer,&argv[i][2]);
  83.                 }
  84.                 else
  85.                 {
  86.                     i++;
  87.                     strcpy(printer,argv[i]);
  88.                 }
  89.                 break;
  90.             case 'S':
  91.                 if(strlen(argv[i])>2)
  92.                 {
  93.                     strcpy(host,&argv[i][2]);
  94.                 }
  95.                 else
  96.                 {
  97.                     i++;
  98.                     strcpy(host,argv[i]);
  99.                 }
  100.                 break;
  101.             case 'l':
  102.                 verbose=1;
  103.                 break;
  104.             default:
  105.                 usage();
  106.         }
  107.     }
  108.     }
  109.  
  110.    /* Verify that we have enough data to procede */
  111.    if(strlen(printer)==0)
  112.    {
  113.     printf("A printer must be specified! Use either the command line\n");
  114.     printf("flag, -Pprinter, or the environment variable, PRINTER.\n");
  115.     exit(1);
  116.    }
  117.  
  118.    if(strlen(host)==0)
  119.    {
  120.     printf("A LPR Server must be specified! Use either the command line\n");
  121.     printf("flag, -Sserver, or the environment variable, LPRSRV.\n");
  122.     exit(1);
  123.    }
  124.     
  125.    sock_init();
  126.  
  127.    s = &socketdata;
  128.    if (!(host_ip = resolve( host ))) {
  129.       fprintf(stderr, "lpq: unknown host %s\n\r",host);
  130.       exit(1);
  131.    }
  132.  
  133.    localport = 255 + (MsecClock() & 255);
  134.    localport = LOCAL_PORT;
  135.  
  136.    if ( !tcp_open( s, localport, host_ip, LPQ_PORT, NULL)) {
  137.       fprintf(stderr,"Unable to open socket.");
  138.       exit(1);
  139.    }
  140.  
  141.    sock_wait_established( s, sock_delay , NULL, &status );
  142.    connected = 1;
  143.  
  144.    if (sock_dataready( s )) {
  145.        sock_fastread( s, buffer, sizeof( buffer ));
  146.        buffer[ sizeof( buffer ) - 1] = 0;
  147.        printf("Response: %s\n", buffer );
  148.        sock_tick( s, &status );    /* in case above message closed port */
  149.    }
  150.  
  151.    if (verbose)
  152.    {
  153.     sprintf(buffer,"%c%s\n",LONG_LIST,printer);
  154.    }
  155.    else
  156.    {
  157.     sprintf(buffer,"%c%s\n",SHORT_LIST,printer);
  158.    }
  159.    sock_write(s, buffer, strlen(buffer));
  160.  
  161.    while ( 1 ) {
  162.     sock_wait_input( s, sock_delay, NULL, &status );
  163.     len = sock_read( s, buffer, sizeof( buffer ));
  164.     printf("%*.*s",len,len,buffer);
  165.    }
  166.  
  167.  
  168. sock_err:
  169.    switch ( status) {
  170.     case 1 : status = 0;
  171.          break;
  172.     case -1: fprintf( stderr, "Host closed connection.\n\r");
  173.          status = 3;
  174.          break;
  175.    }
  176.    if (!connected)
  177.        fprintf( stderr , "\n\rCould not get connected.  You may not be in the /etc/hosts.lpd file!\n\r");
  178.  
  179.  
  180.    exit( status );
  181.    return (0);   /* not reached */
  182. }
  183.