home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / vrac_os2 / pmon2.zip / HPPRINT.C < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  114 lines

  1. /* hpprint.c 
  2.  *
  3.  * send stdin to printer connected via HP JetDirect using TCP socket
  4.  *
  5.  * Author:  Kai Uwe Rommel <rommel@ars.muc.de>
  6.  * Created: Sat Jun 04 1994
  7.  */
  8.  
  9. static char *rcsid =
  10. "$Id: hpprint.c,v 1.1 1994/06/04 16:02:50 rommel Exp $";
  11. static char *rcsrev = "$Revision: 1.1 $";
  12.  
  13. /* $Log: hpprint.c,v $
  14. /* Revision 1.1  1994/06/04 16:02:50  rommel
  15. /* Initial revision
  16. /* */
  17.  
  18. #define DEFAULTPORT 9100
  19.  
  20. #include <stdlib.h>
  21. #include <getopt.h>
  22. #include <fcntl.h>
  23. #define ioctl emx_ioctl
  24. #define select emx_select
  25. #include <io.h>
  26. #undef ioctl
  27. #undef select
  28.  
  29. #include <types.h>
  30. #include <netinet/in.h>
  31. #include <sys/socket.h>
  32. #include <netdb.h>
  33.  
  34. void usage(void)
  35. {
  36.   printf("\nUsage: hpprint [-h hostname] [-p port]\n"
  37.      "\nAll standard input is sent to the printer.\n");
  38.   exit(1);
  39. }
  40.  
  41.  
  42. int main(int argc, char **argv)
  43. {
  44.   unsigned long host = 0;
  45.   unsigned short port = 0;
  46.   struct hostent *hostnm;
  47.   struct servent *portnm;
  48.   struct sockaddr_in server;
  49.   struct linger linger;
  50.   int print_socket, opt, bytes, sent;
  51.   char buffer[BUFSIZ], *ptr;
  52.  
  53.   if (argc == 1 && isatty(0))
  54.     usage();
  55.  
  56.   while ((opt = getopt(argc, argv, "h:p:")) != EOF)
  57.     switch (opt)
  58.     {
  59.     case 'h':
  60.       if ((hostnm = gethostbyname(optarg)) != NULL)
  61.     host = *((unsigned long *)hostnm -> h_addr);
  62.       else if ((host = inet_addr(optarg)) == -1)
  63.     usage();
  64.       break;
  65.     case 'p':
  66.       if ((portnm = getservbyname(optarg, "tcp")) != NULL)
  67.     port = portnm -> s_port;
  68.       else if ((port = atoi(optarg)) == 0)
  69.     usage();
  70.       break;
  71.     default:
  72.       usage();
  73.     }
  74.  
  75.   if (host == 0)
  76.     usage();
  77.  
  78.   if (port == 0)
  79.     port = DEFAULTPORT;
  80.  
  81.   sock_init();
  82.  
  83.   if ((print_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  84.     return psock_errno("socket()"), 2;
  85.  
  86.   server.sin_family      = AF_INET;
  87.   server.sin_port        = htons(port);
  88.   server.sin_addr.s_addr = host;
  89.   
  90.   if (connect(print_socket, (struct sockaddr *)&server, sizeof(server)) < 0)
  91.     return psock_errno("connect()"), 2;
  92.  
  93.   linger.l_onoff = 1;
  94.   linger.l_linger = 10;
  95.   
  96.   if (setsockopt(print_socket, SOL_SOCKET, SO_LINGER,
  97.          (char *) &linger, sizeof(linger)) < 0)
  98.     return psock_errno("setsockopt()"), 2;
  99.  
  100.   setmode(0, O_BINARY);
  101.   
  102.   while ((bytes = read(0, buffer, sizeof(buffer))) > 0)
  103.     for (ptr = buffer; bytes > 0; bytes -= sent, ptr += sent)
  104.       if ((sent = send(print_socket, buffer, bytes, 0)) < 0)
  105.     return psock_errno("send()"), 2;
  106.  
  107.   if (soclose(print_socket) < 0)
  108.     return psock_errno("soclose()"), 2;
  109.  
  110.   return 0;
  111. }
  112.  
  113. /* end of hpprint.c */
  114.