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