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