home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / xthread.c < prev   
Encoding:
C/C++ Source or Header  |  1994-01-31  |  3.4 KB  |  153 lines

  1. /* This file (and only this file - not the entire nntp distribution) is
  2.  * hereby placed in the public domain.  Use it as you see fit, but if you
  3.  * manage to find some wonderful use for this code elsewhere, it would be
  4.  * nice to receive mention for it.
  5.  *
  6.  * - Tim Iverson
  7.  *   iverson@xstor.com -/- uunet!xstor!iverson
  8.  *   3/28/91
  9.  *   modified by Wayne Davison (davison@borland.com) to work with trn 2.0
  10.  *   10/6/91
  11.  */
  12.  
  13. #include "common.h"
  14.  
  15. #ifdef XTHREAD
  16.  
  17. # ifdef __GNUC__
  18. #  define alloca __builtin_alloca
  19. # endif
  20.  
  21.  
  22. /* Usage: XTHREAD [DBINIT|THREAD]
  23.  *
  24.  * DBINIT    dump the contents of the db.init file to stdout
  25.  * THREAD    dump the contents of the thread file for the current
  26.  *        newsgroup to stdout (default if no argument).
  27.  *
  28.  * N.B. These two files are not ascii and no attempt is made at converting
  29.  *    native byte size to any type of standard whatsoever.  This'll have
  30.  *    to be fixed if this command is to be integrated into the protocol.
  31.  *
  32.  * This command is not documented in rfc977.
  33.  */
  34.  
  35. void
  36. xthread(argc, argv)
  37. int    argc;
  38. char    *argv[];
  39. {
  40.     register FILE    *fp;
  41.     struct stat    s;
  42.     char        *buf, *file, *what;
  43.  
  44.     /* can't transfer threads, only read 'em */
  45.     if (!canread)
  46.     {
  47.         printf("%d You only have permission to transfer, sorry.\r\n",
  48.             ERR_ACCESS);
  49.         (void) fflush(stdout);
  50.         return;
  51.     }
  52.  
  53.     /* "parse" the argument */
  54.     if (argc == 2 && !strcasecmp(argv[1], "dbinit"))
  55.     {
  56.         char *cp, *thread_name();
  57.  
  58.         file = thread_name("*******");
  59.         what = "db.init";
  60.         for (cp = file; *cp != '*'; cp++)
  61.             ;
  62.         strcpy(cp, what);
  63.     }
  64.     else if (argc == 1 || argc == 2 && !strcasecmp(argv[1], "thread"))
  65.     {
  66.         if (!threadfile)
  67.         {
  68.             printf("%d You are not currently in a newsgroup.\r\n",
  69.                 ERR_NCING);
  70.             (void) fflush(stdout);
  71.             return;
  72.         }
  73.         file = threadfile;
  74.         what = "thread";
  75.     }
  76.     else
  77.     {
  78.         printf("%d Usage: XTHREAD [DBINIT|THREAD]\r\n", ERR_CMDSYN);
  79.         (void) fflush(stdout);
  80.         return;
  81.     }
  82.  
  83.     /* try to open the file to be transfered */
  84.     if (!(fp = fopen(file, "r")))
  85.     {
  86.         printf("%d %s file is not available.\r\n", ERR_FAULT, what);
  87.         (void) fflush(stdout);
  88. #ifdef SYSLOG
  89.         if (!strcmp(what, "db.init"))
  90.             syslog(LOG_ERR, "xthread: fopen %s: %m", file);
  91. #endif
  92.         return;
  93.     }
  94.  
  95.     /* tell 'em how much binary data is coming down the pike */
  96.     fstat(fileno(fp), &s);
  97.     printf("%d %u bytes of %s file follows verbatim (binary!)\r\n",
  98.         OK_BIN, s.st_size, what);
  99.  
  100.     /* copy the file verbatim to stdout */
  101. #ifdef __GNUC__
  102.     if (buf = alloca(s.st_size))
  103.     {
  104.         /* ah-so! got lotsa memoree */
  105.         read(fileno(fp), buf, s.st_size);
  106.         fwrite(buf, s.st_size, 1, stdout);
  107.     }
  108.     else
  109. #endif
  110.     {
  111.         int        bytes;
  112.         char        buf[BUFSIZ];
  113.  
  114.         while (bytes = fread(buf, 1, sizeof buf, fp))
  115.             fwrite(buf, bytes, 1, stdout);
  116.     }
  117.  
  118.     fputs("\r\n.\r\n", stdout);
  119.     fflush(stdout);
  120.     fclose(fp);
  121. }
  122.  
  123. /* Change a newsgroup name into the name of the thread data file.  We
  124. ** subsitute any '.'s in the group name into '/'s (unless LONG_THREAD_NAMES
  125. ** is defined), prepend the path, and append the '/.thread' (or '.th') on to
  126. ** the end.
  127. */
  128. char *
  129. thread_name(group)
  130. char *group;
  131. {
  132.     static char name_buff[MAXPATHLEN];
  133. #ifndef LONG_THREAD_NAMES
  134.     char group_buff[512];
  135.     register char *ptr;
  136.  
  137.     strcpy(group_buff, group);
  138.     ptr = group = group_buff;
  139.     while ((ptr = index(ptr, '.'))) {
  140.         *ptr = '/';
  141.     }
  142. #endif
  143. #ifdef SUFFIX
  144.     sprintf(name_buff, "%s/%s%s", threaddir, group, SUFFIX);
  145. #else
  146.     sprintf(name_buff, "%s/%s", threaddir, group);
  147. #endif
  148.  
  149.     return name_buff;
  150. }
  151.  
  152. #endif /* not XTHREAD */
  153.