home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / NCSATELN / TEL23SRC.ZIP / LPR / LPRM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-30  |  6.1 KB  |  197 lines

  1. /*  -------------------------------------------------------------------
  2.     lprm - remove job from line printer queue
  3.  
  4.     lprm removes a job or jobs from a print spooling queue managed by
  5.     an LPD daemon running on a remote machine.
  6.     Built on top of the NCSA TCP/IP package (version 2.2tn for MS-DOS).
  7.  
  8.     Paul Hilchey   May 1989
  9.  
  10.     Copyright (C) 1989  The University of British Columbia
  11.     All rights reserved.
  12.  
  13.      history
  14.      -------
  15.      1/6/89        Microsoft C port by Heeren Pathak (NCSA)
  16.     -------------------------------------------------------------------
  17. */
  18.  
  19. #define LPR
  20.  
  21. #include <stdio.h>
  22. #include <dos.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #ifdef MSC
  28. #include <signal.h>
  29. #include <time.h>
  30. #endif
  31.  
  32. #define WINMASTER
  33. #ifdef MEMORY_DEBUG
  34. #include "memdebug.h"
  35. #endif
  36. #include "whatami.h"
  37. #include "hostform.h"
  38. #include "windat.h"
  39. #include "lp.h"
  40. #include "externs.h"
  41.  
  42. int debug = 0;          /* 1 = print debugging info; set with -D option */
  43. int ftppassword,        /* not used; just to avoid unresolved external */
  44.     bypass_passwd=0;    /* whether to bypass the password check */
  45.  
  46. unsigned char path_name[_MAX_DRIVE+_MAX_DIR],        /* character storage for the path name */
  47.     temp_str[20],s[_MAX_DIR],temp_data[30];
  48.  
  49.  
  50. #define JOB_AND_USER_BUFF_SIZE  150     /* size of buffer for job numbers and user name specified on the command line */
  51.  
  52. /* Function Protoypes */
  53. void main(int argc,char *argv[]);
  54. static void randomize(void );
  55.  
  56. /****************************************************************
  57.  *  Main program.                                               *
  58.  *     lprm [ -Pprinter ] [ -Sserver ] [ - ] [job# ...]         *
  59.  ****************************************************************/
  60. void main(int argc,char *argv[])
  61. {
  62.     int i;
  63.     char *ptr;
  64.     int  len;
  65.  
  66.     char username[9];       /* name of user (max 8 chars) */
  67.     struct config *cp;      /* configuration information */
  68.  
  69.     static char buff[JOB_AND_USER_BUFF_SIZE] = ""; /* string of jobs to be removed */
  70.     int something = 0;      /* 1 = we got something back from the server */
  71.     struct machinfo *server_info_record;
  72.     int server_connection_id;
  73.  
  74.     char *remote_name,      /* printer name on remote system */
  75.          *remote_host;      /* address of remote host        */
  76.  
  77.     _splitpath(argv[0],path_name,s,temp_str,temp_data);    /* split the full path name of telbin.exe into it's components */
  78.     strcat(path_name,s);    /* append the real path name to the drive specifier */
  79.  
  80. #ifdef MSC
  81.     signal(SIGINT,breakstop);        /* Microsoft intercept of break */
  82. #else
  83.     ctrlbrk(breakstop);     /* set up ctrl-c handler */
  84. #endif
  85.  
  86.     /* Do session initialization.  Snetinit reads config file. */
  87.     ptr = getenv("CONFIG.TEL");
  88.     if (ptr != NULL) Shostfile(ptr);
  89.     if(i=Snetinit()) {
  90.         if(i==-2)        /* check for BOOTP server not responding */
  91.             netshut();    /* release network */
  92.         crash("network initialization failed.\n");
  93.       }    /* end if */
  94.  
  95.     /* select default printer and server */
  96.     remote_name = getenv("PRINTER");
  97.     if (remote_name == NULL) remote_name = DEFAULT_PRINTER;
  98.     remote_host = getenv("SERVER");
  99.  
  100.     /* get info from the configuration file */
  101.     cp = (struct config *)malloc(sizeof(struct config));
  102.     Sgetconfig(cp);
  103.  
  104.     /* check that the machine name was set in the configuration file */
  105.     if (0 == strlen(cp->me)) crash("`myname' not set in config file.");
  106.  
  107.     /* set user name.  use first part of machine name if nothing else */
  108.     ptr = getenv("USER");
  109.     if (NULL != ptr) {
  110.         strncpy(username,ptr,8);
  111.         username[8]='\0';
  112.     }
  113.     else {
  114.         i = min(strcspn(cp->me,"."),sizeof(username)-1);
  115.         strncpy(username,cp->me,i);
  116.         username[i]='\0';
  117.     }
  118.  
  119.     /* Loop through command line arguments */
  120.     for (i=1; i<argc; ++i)
  121.  
  122.         if (0 == strncmp(argv[i],"-P",2))       /* select printer */
  123.             if (argv[i][2])
  124.                 remote_name = &argv[i][2];
  125.             else if (i+1 < argc)
  126.                 remote_name = argv[++i];
  127.             else;
  128.  
  129.         else if (0 == strncmp(argv[i],"-S",2))  /* select server */
  130.             if (argv[i][2])
  131.                 remote_host = &argv[i][2];
  132.             else if (i+1 < argc)
  133.                 remote_host = argv[++i];
  134.             else;
  135.  
  136.         else if (0 == strcmp(argv[i],"-")) {    /* delete all my jobs */
  137.             strcat(buff," ");
  138.             strcat(buff,username);
  139.         }
  140.  
  141.         else if (0 == strncmp(argv[i],"-D",2))  /* turn on debug */
  142.             debug = 1;
  143.  
  144.         else {                                  /* job number, add to list */
  145.             strcat(buff," ");
  146.             strcat(buff,argv[i]);
  147.         };
  148.  
  149.     if (remote_host == NULL) crash("server not specified.");
  150.  
  151.     server_info_record = lookup(remote_host);
  152.     if (server_info_record == 0)
  153.         crash("domain lookup failed for %s.",remote_host);
  154.  
  155.  
  156.     /* pick a source port at random from the set of privileged ports, */
  157.     /* and open the connection                                        */
  158.     randomize();
  159.     server_connection_id = open_connection(server_info_record, rand() % MAX_PRIV_PORT, PRINTER_PORT);
  160.     if (server_connection_id < 0) crash ("unable to open connection.");
  161.  
  162.     /* send the request */
  163.     nprintf (server_connection_id, "%c%s %s%s\n", LPD_REMOVE_JOB,
  164.              remote_name, username, buff);
  165.  
  166.     /* wait for replies from the server and print them */
  167.     while(1) {
  168.         len = nread(server_connection_id, buff, 132);
  169.         if (len <= 0) break;
  170.         if (buff[0] != LPD_ERROR) {
  171.             printf("%.*s",len,buff);
  172.             something = 1;
  173.         }
  174.     }
  175.  
  176.     netclose(server_connection_id);
  177.     netshut();
  178.     if (!something) puts("No applicable jobs.");
  179. }
  180.  
  181. #ifdef MSC
  182.  
  183. /******************************************************************
  184. *
  185. * randomize()
  186. *
  187. * replicates the randomize function of Turbo C
  188. * MSC 5.1 does not contain it so we have to write it ourselves.
  189. *
  190. */
  191.  
  192. static void randomize(void )
  193. {
  194.     srand((unsigned)time(NULL));
  195. }
  196. #endif
  197.