home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / gopher / Unix / gopher-gateways / techinfo / techinpher / v1.0 / techinpher-helper.c.Z / techinpher-helper.c
Encoding:
C/C++ Source or Header  |  1993-01-24  |  2.3 KB  |  91 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3.  
  4. /* Techinpher-Helper program
  5.  
  6.   Copyright 1993 by the University of Pennsylvania.
  7.  
  8.   Export of this software from the United States of America is assumed
  9.   to require a specific license from the United States Government.  It
  10.   is the responsibility of any person or organization contemplating
  11.   export to obtain such a license before exporting.  Within that
  12.   constraint, permission to use, distribute, or copy this software is
  13.   granted, provided that this copyright notice appear on all copies.
  14.   This software is provided as is, with no explicit nor implicit
  15.   warranty.
  16.  
  17. */
  18.  
  19. /* HISTORY:
  20.  
  21. Jan 1993: version 0.1 created --lam
  22.  
  23. */
  24.  
  25. #define END_LINE           "\r\n"
  26. #define HELPER_ERROR       "3: techinpher-helper error"
  27. FILE *outfp;
  28.  
  29.  
  30. errexit (int exitstat, char *msg)
  31. {
  32.   fprintf (outfp, "%s%s.%s", msg, END_LINE, END_LINE);
  33.   fclose (outfp);
  34.   exit (exitstat);
  35. }
  36.  
  37. main (int argc, char *argv[])
  38. {
  39.   char *host, *port, *path, *outfile;
  40.   int gophsock;
  41.   char buf[32000];
  42.   int cc;
  43.  
  44.   outfp = stdout;
  45.  
  46.   if (argc < 4) {
  47.     fprintf (stderr, "Usage: %s GopherHost GopherPort \"GopherPath\" [Output File]%s",
  48.          argv[0], END_LINE);
  49.     sprintf (buf, "%s (usage)", HELPER_ERROR);
  50.     errexit (1, buf);
  51.   }
  52.   host = argv[1];
  53.   port = argv[2];
  54.   path = (char *) malloc (strlen(argv[3]) + 10);
  55.   strcpy (path, argv[3]);
  56.   strcat (path, "\r\n");
  57.   outfile = argv[4];
  58.  
  59.   if (argc > 4) {
  60.     outfp = fopen (outfile, "w");
  61.     if (outfp == NULL) {
  62.       outfp = stdout;
  63.       fprintf (stderr, "Unable to write %s%s", outfile, END_LINE);
  64.       perror (outfile);
  65.       sprintf (buf,
  66.            "%s (can't write %s, errno %d)", HELPER_ERROR, outfile, errno);
  67.       errexit (2, buf);
  68.     }
  69.   }
  70.  
  71.   gophsock = inet_establish_connection (host, port, 1);
  72.   if (gophsock < 0) {
  73.     fprintf (stderr,"Unable to connect to %s at %s%s", host,port,END_LINE);
  74.     sprintf (buf, "3:Unable to connect to Gopher %s", host);
  75.     errexit (3, buf);
  76.   }
  77.   else {
  78.     /* Send path to gopher */
  79.     if (write (gophsock, path, strlen(path)) != strlen(path)) { 
  80.       sprintf (buf, "3:Unable to send path \"%s\" (errno %d)", argv[3], errno);
  81.       errexit (4, buf);
  82.     }
  83.     
  84.     while ((cc = read (gophsock, buf, sizeof(buf))) > 0) {
  85.       write (fileno(outfp), buf, cc);
  86.     }
  87.   }
  88.   exit (0);
  89. }
  90.  
  91.