home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / contrib / nntpget / badnntpget.c next >
C/C++ Source or Header  |  1992-06-07  |  3KB  |  133 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. char *progname;
  24. unsigned int debug;
  25. FILE *dfp;
  26.  
  27. /* forwards */
  28. static void fopensock(/* char *hname, char *service, FILE *fp[2] */);
  29.  
  30. /*
  31.  * main - parse arguments and handle options
  32.  */
  33. int
  34. main(argc, argv)
  35. int argc;
  36. char **argv;
  37. {
  38.     FILE *fp[2];
  39.     char *line, *port = "nntp";
  40.     int len, c, tmpbatches = 0, errflg = 0, cmdline = 0;
  41.     struct netdata *ndp;
  42.  
  43.     progname = argv[0];
  44.     dfp = stderr;
  45.     while ((c = getopt(argc, argv, "dD:p:t")) != EOF) {
  46.         switch (c) {
  47.         case 'd':
  48.             debug++;
  49.             break;
  50.         case 'D':
  51.             dfp = efopen(optarg, "a+");
  52.             break;
  53.         case 'p':
  54.             port = optarg;
  55.             break;
  56.         case 't':
  57.             tmpbatches++;
  58.             break;
  59.         default:
  60.             errflg++;
  61.             break;
  62.         }
  63.     }
  64.     if (optind >= argc) {
  65.         fprintf(stderr, "Usage: nntpget [-d] [-D] [-p port] [-t] hostname [msgid ...]\n");
  66.         exit(1);
  67.     }
  68.     if (tmpbatches)
  69.         cd(artfile(NULLSTR));
  70.     else
  71.         batchinit(stdout);
  72.     fopensock(argv[optind], port, fp);
  73.     optind++;
  74.     cmdline = optind < argc;
  75.     ndp = net_new();
  76.     for (;;) {
  77.         if (cmdline) {
  78.             line = argv[optind++];
  79.         } else {
  80.             line = fgetline(stdin, &len);
  81.             if (line && line[len - 1] == '\n')
  82.                 line[--len] = '\0';
  83.         }
  84.         if (line == NULL)
  85.             break;
  86.         (void) rewind(fp[0]);
  87.         if (net_ackwrite("ARTICLE ", 8, line, fp[0]) != 0)
  88.             error("net_ackwrite(%s) failed", line);
  89.         (void) rewind(fp[0]);
  90.         /* get reply, check it's a 200 code */
  91.         if ((line = net_cmdread(fp[0], &len)) == NULL || *line != '2')
  92.             continue;
  93.         if (net_getdata(fp[0], ndp) != 0) {
  94.             warning("error fetching article", "");
  95.             continue;
  96.         }
  97.         if (batchadd(ndp->nd_buf, ndp->nd_bufcnt, ndp->nd_bytes,
  98.                  ndp->nd_fp, ndp->nd_spilled) != 0)
  99.             warning("error batching article", "");
  100.     }
  101.     (void) rewind(fp[0]);
  102.     (void) net_ackwrite("QUIT ", 5, line, fp[0]);
  103.     batchend();
  104. }
  105.  
  106. static void
  107. fopensock(hname, service, fp)
  108. char *hname, *service;
  109. FILE *fp[2];
  110. {
  111.     int fd, len;
  112.     char *line;
  113.  
  114.     fd = tcpopen(hname, service);
  115.     if (fd < 0)
  116.         error("tcpopen failed in %s", tcperror());
  117.     fp[0] = fdopen(fd, "r+");
  118.     if (fp[0] == NULL)
  119.         error("fdopen(socket, \"r+\") failed", "");
  120. #if 0
  121.     fp[1] = fdopen(fd, "w");
  122.     if (fp[1] == NULL)
  123.         error("fdopen(socket, \"w\") failed", "");
  124. #endif
  125.     line = net_cmdread(fp[0], &len);
  126.     if (*line != '2') {
  127.         errno = 0;
  128.         error("remote server responded with %s", line);
  129.     }
  130.     if (fflush(fp[0]) != 0)
  131.         error("fflush on socket failed", "");
  132. }
  133.