home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / contrib / nntpget / nntpget.c < prev   
C/C++ Source or Header  |  1992-06-07  |  3KB  |  132 lines

  1. /*
  2.  * asks a remote server for a list of message-ids (from stdin or command
  3.  * line) and outputs a relaynews batch on stdout
  4.  */
  5. /* Mark Moraes, University of Toronto */
  6.  
  7. #include <stdio.h>
  8. #include "fgetfln.h"
  9. #include "debug.h"
  10. #include "config.h"
  11. #include "libcnews.h"
  12. #include "netdata.h"
  13. #include "batch.h"
  14.  
  15. #define NULLSTR        ((char *) NULL)
  16. #define    STREQN(a, b, n)    (*(a) == *(b) && strncmp((a), (b), (n)) == 0)
  17.  
  18. /* for getopt */
  19. extern int optind;
  20. extern char *optarg;
  21. extern int errno;
  22.  
  23. extern char *tcperror();
  24.  
  25. char *progname;
  26. unsigned int debug;
  27. FILE *dfp;
  28.  
  29. /* forwards */
  30. static void fopensock(/* char *hname, char *service, FILE *fp[2] */);
  31.  
  32. /*
  33.  * main - parse arguments and handle options
  34.  */
  35. int
  36. main(argc, argv)
  37. int argc;
  38. char **argv;
  39. {
  40.     FILE *fp[2];
  41.     char *line, *port = "nntp";
  42.     int len, c, tmpbatches = 0, errflg = 0, cmdline = 0;
  43.     struct netdata *ndp;
  44.  
  45.     progname = argv[0];
  46.     dfp = stderr;
  47.     while ((c = getopt(argc, argv, "dD:p:t")) != EOF) {
  48.         switch (c) {
  49.         case 'd':
  50.             debug++;
  51.             break;
  52.         case 'D':
  53.             dfp = efopen(optarg, "a+");
  54.             break;
  55.         case 'p':
  56.             port = optarg;
  57.             break;
  58.         case 't':
  59.             tmpbatches++;
  60.             break;
  61.         default:
  62.             errflg++;
  63.             break;
  64.         }
  65.     }
  66.     if (optind >= argc || errflg > 0) {
  67.         fprintf(stderr, "Usage: nntpget [-d] [-D] [-p port] [-t] hostname [msgid ...]\n");
  68.         exit(1);
  69.     }
  70.     if (tmpbatches)
  71.         cd(artfile(NULLSTR));
  72.     else
  73.         batchinit(stdout);
  74.     fopensock(argv[optind], port, fp);
  75.     optind++;
  76.     cmdline = optind < argc;
  77.     ndp = net_new();
  78.     for (;;) {
  79.         if (cmdline) {
  80.             line = argv[optind++];
  81.         } else {
  82.             line = fgetline(stdin, &len);
  83.             if (line != NULL)
  84.                 if (line[len - 1] == '\n')
  85.                     line[--len] = '\0';
  86.         }
  87.         if (line == NULL)
  88.             break;
  89.         if (net_ackwrite("ARTICLE", 7, line, fp[1]) != 0)
  90.             error("net_ackwrite(%s) failed", line);
  91.         /* get reply, check it's a 200 code */
  92.         if ((line = net_getreply(fp[0], &len, NULLFUNC, NULLSTR))
  93.             == NULL || *line != '2')
  94.             continue;
  95.         if (net_getdata(fp[0], ndp) != 0) {
  96.             warning("error fetching article", line);
  97.             continue;
  98.         }
  99.         if (batchadd(ndp->nd_buf, ndp->nd_bufcnt, ndp->nd_bytes,
  100.                  ndp->nd_fp, ndp->nd_spilled) != 0)
  101.             warning("error batching article", line);
  102.     }
  103.     (void) net_ackwrite("QUIT", 4, NULLSTR, fp[1]);
  104.     batchend();
  105.     return 0;
  106. }
  107.  
  108. static void
  109. fopensock(hname, service, fp)
  110. char *hname, *service;
  111. FILE *fp[2];
  112. {
  113.     int fd, len;
  114.     char *line;
  115.  
  116.     fd = tcpopen(hname, service);
  117.     if (fd < 0)
  118.         error("tcpopen failed in %s", tcperror());
  119.     fp[0] = fdopen(fd, "r");
  120.     if (fp[0] == NULL)
  121.         error("fdopen(socket, \"r\") failed", "");
  122.     fp[1] = fdopen(fd, "w");
  123.     if (fp[1] == NULL)
  124.         error("fdopen(socket, \"w\") failed", "");
  125.     if ((line = net_getreply(fp[0], &len, NULLFUNC, NULLSTR)) == NULL)
  126.         error("net_getreply failed", "");
  127.     if (*line != '2') {
  128.         errno = 0;
  129.         error("remote server responded with %s", line);
  130.     }
  131. }
  132.