home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / LPQ.C < prev    next >
Text File  |  1991-07-12  |  4KB  |  179 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 <tcp.h>
  30.  
  31. #define SHORT_LIST 3
  32. #define LONG_LIST  4
  33.  
  34. #define LPQ_PORT 515
  35. #define LOCAL_PORT 722
  36.  
  37. extern int errno;
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char **argv;
  42. {
  43.     char buffer[513];
  44.     char printer[35];
  45.     char host[35];
  46.  
  47.  
  48.     tcp_Socket socketdata, *s;
  49.     longword host_ip;
  50.     int status;
  51.     int connected = 0;
  52.     int localport;
  53.     int len;
  54.     int i;
  55.     int verbose=0;
  56.     /* */
  57.  
  58.     /* Set defaults from the environment */
  59.     strcpy(host,getenv("LPRSRV"));
  60.     strcpy(printer,getenv("PRINTER"));
  61.  
  62.  
  63.     /* Parse the command line arguments */
  64.     for(i=1; i<=argc ; i++)
  65.     {
  66.     if(argv[i][0] == '-')
  67.     {
  68.         switch(argv[i][1])
  69.           {
  70.             case 'P':
  71.                 if(strlen(argv[i])>2)
  72.                 {
  73.                     strcpy(printer,&argv[i][2]);
  74.                 }
  75.                 else
  76.                 {
  77.                     i++;
  78.                     strcpy(printer,argv[i]);
  79.                 }
  80.                 break;
  81.             case 'S':
  82.                 if(strlen(argv[i])>2)
  83.                 {
  84.                     strcpy(host,&argv[i][2]);
  85.                 }
  86.                 else
  87.                 {
  88.                     i++;
  89.                     strcpy(host,argv[i]);
  90.                 }
  91.                 break;
  92.             case 'l':
  93.                 verbose=1;
  94.                 break;
  95.             default:
  96.                 usage();
  97.         }
  98.     }
  99.     }
  100.  
  101.    /* Verify that we have enough data to procede */
  102.    if(strlen(printer)==0)
  103.    {
  104.     printf("A printer must be specified! Use either the command line\n");
  105.     printf("flag, -Pprinter, or the environment variable, PRINTER.\n");
  106.     exit(1);
  107.    }
  108.  
  109.    if(strlen(host)==0)
  110.    {
  111.     printf("A LPR Server must be specified! Use either the command line\n");
  112.     printf("flag, -Sserver, or the environment variable, LPRSRV.\n");
  113.     exit(1);
  114.    }
  115.     
  116.    sock_init();
  117.  
  118.    s = &socketdata;
  119.    if (!(host_ip = resolve( host ))) {
  120.       fprintf(stderr, "lpq: unknown host %s\n\r",host);
  121.       exit(1);
  122.    }
  123.  
  124.    localport = 255 + (MsecClock() & 255);
  125.    localport = LOCAL_PORT;
  126.  
  127.    if ( !tcp_open( s, localport, host_ip, LPQ_PORT, NULL)) {
  128.       fprintf(stderr,"Unable to open socket.");
  129.       exit(1);
  130.    }
  131.  
  132.    sock_wait_established( s, sock_delay , NULL, &status );
  133.    connected = 1;
  134.  
  135.    if (sock_dataready( s )) {
  136.        sock_fastread( s, buffer, sizeof( buffer ));
  137.        buffer[ sizeof( buffer ) - 1] = 0;
  138.        printf("Response: %s\n", buffer );
  139.        sock_tick( s, &status );    /* in case above message closed port */
  140.    }
  141.  
  142.    if (verbose)
  143.    {
  144.     sprintf(buffer,"%c%s\n",LONG_LIST,printer);
  145.    }
  146.    else
  147.    {
  148.     sprintf(buffer,"%c%s\n",SHORT_LIST,printer);
  149.    }
  150.    sock_write(s, buffer, strlen(buffer));
  151.  
  152.    while ( 1 ) {
  153.     sock_wait_input( s, sock_delay, NULL, &status );
  154.     len = sock_read( s, buffer, sizeof( buffer ));
  155.     printf("%*.*s",len,len,buffer);
  156.    }
  157.  
  158.  
  159. sock_err:
  160.    switch ( status) {
  161.     case 1 : status = 0;
  162.          break;
  163.     case -1: fprintf( stderr, "Host closed connection.\n\r");
  164.          status = 3;
  165.          break;
  166.    }
  167.    if (!connected)
  168.        fprintf( stderr , "\n\rCould not get connected.  You may not be in the /etc/hosts.lpd file!\n\r");
  169.  
  170.  
  171.    exit( status );
  172. }
  173.  
  174. usage()
  175. {
  176.     printf("usage: lpq [-Pprinter] [-Sserver] [-l]\n");
  177.     exit(1);
  178. }
  179.