home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rn_4_3_blars.lzh / artio.c < prev    next >
Text File  |  1990-08-22  |  5KB  |  176 lines

  1. /* $Header: artio.c,v 4.3.2.5 90/03/22 23:04:04 sob Exp $
  2.  *
  3.  * $Log:    artio.c,v $
  4.  * Revision 4.3.2.5  90/03/22  23:04:04  sob
  5.  * Fixes provided by Wayne Davison <drivax!davison>
  6.  * 
  7.  * Revision 4.3.2.4  89/11/27  01:29:57  sob
  8.  * Altered NNTP code per ideas suggested by Bela Lubkin
  9.  * <filbo@gorn.santa-cruz.ca.us>
  10.  * 
  11.  * Revision 4.3.2.3  89/11/26  22:55:31  sob
  12.  * Add nntpopen() and nntpclose() routines to cut down on size of rrn
  13.  * 
  14.  * Revision 4.3.2.2  89/11/08  01:17:12  sob
  15.  * Added changes to insure that this will compile for RN or RRN with no
  16.  * changes to the source code.
  17.  * 
  18.  * Revision 4.3.2.1  89/11/06  00:07:25  sob
  19.  * Added RRN support from NNTP 1.5
  20.  * 
  21.  * Revision 4.3  85/05/01  11:35:39  lwall
  22.  * Baseline for release with 4.3bsd.
  23.  * 
  24.  */
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #ifdef SERVER
  29. #include "server.h"
  30. #endif
  31. #include "INTERN.h"
  32. #include "artio.h"
  33.  
  34. void
  35. artio_init()
  36. {
  37.     ;
  38. }
  39.  
  40. /* open an article, unless it's already open */
  41.  
  42. FILE *
  43. artopen(artnum)
  44. ART_NUM artnum;
  45. {
  46. #ifdef SERVER
  47.     nntpopen(artnum,ARTICLE);
  48. #else
  49.     char artname[32];            /* filename of current article */
  50.  
  51.     if (artnum < 1)
  52.     return Nullfp;
  53.     if (openart == artnum) {        /* this article is already open? */
  54.     fseek(artfp,0L,0);        /* just get to the beginning */
  55.     return artfp;            /* and say we succeeded */
  56.     }
  57.     if (artfp != Nullfp) {        /* it was somebody else? */
  58.     fclose(artfp);            /* put them out of their misery */
  59.     openart = 0;            /* and remember them no more */
  60.     }
  61.     sprintf(artname,"%ld",(long)artnum);
  62.                     /* produce the name of the article */
  63.     if (artfp = fopen(artname,"r"))    /* if we can open it */
  64.     openart = artnum;        /* remember what we did here */
  65. #endif SERVER
  66. #ifdef LINKART
  67.     {
  68.     char tmpbuf[256];
  69.     char *s;
  70.  
  71.     if (fstat(artfp->_file,&filestat))
  72.         return artfp;
  73.     if (filestat.st_size < (sizeof tmpbuf)) {
  74.         fgets(tmpbuf,(sizeof tmpbuf),artfp);
  75.         if (*tmpbuf == '/') {    /* is a "link" to another article */
  76.         fclose(artfp);
  77.         if (s=index(tmpbuf,'\n'))
  78.             *s = '\0';
  79.         if (!(artfp = fopen(tmpbuf,"r")))
  80.             openart = 0;
  81.         else {
  82.             if (*linkartname)
  83.             free(linkartname);
  84.             linkartname = savestr(tmpbuf);
  85.         }
  86.         }
  87.         else
  88.         fseek(artfp,0L,0);        /* get back to the beginning */
  89.     }
  90.     }
  91. #endif
  92.     return artfp;            /* and return either fp or NULL */
  93. }
  94.  
  95. #ifdef SERVER
  96. static long our_pid;
  97.  
  98. FILE *
  99. nntpopen(artnum,function)
  100. ART_NUM artnum;
  101. ART_PART function;
  102. {
  103.     char ser_line[256];
  104.     char artname[32];            /* filename of current article */
  105.     if (our_pid == 0)
  106.     our_pid = getpid();
  107.     if (artnum < 1)
  108.     return Nullfp;
  109.     if ((openart == artnum) && (openpart >= function))
  110.     {                    /* this article is already open? */
  111.     fseek(artfp,0L,0);        /* just get to the beginning */
  112.     return artfp;            /* and say we succeeded */
  113.     }
  114.     if (artfp != Nullfp) {        /* it was somebody else? */
  115.     fclose(artfp);            /* put them out of their misery */
  116.     nntpclose();
  117.     openart = 0;            /* and remember them no more */
  118.     }
  119.     sprintf(artname,"/tmp/rrn%ld.%ld", (long) artnum, our_pid);
  120.     artfp = fopen(artname, "w+");    /* create the temporary article */
  121.     if (artfp == Nullfp) {
  122.     UNLINK(artname);
  123.     return Nullfp;
  124.     }
  125.     switch (function){
  126.         case STAT:
  127.         function = HEAD;    /* fall through */
  128.         case HEAD:
  129.         sprintf(ser_line, "HEAD %ld", (long)artnum);
  130.         break;
  131.         case ARTICLE:
  132.         sprintf(ser_line, "ARTICLE %ld", (long)artnum);
  133.         break;
  134.     }        
  135.     put_server(ser_line);        /* ask the server for the article */
  136.     if (get_server(ser_line, sizeof(ser_line)) < 0) {
  137.     fprintf(stderr, "rrn: Unexpected close of server socket.\n");
  138.     finalize(1);
  139.     }
  140.     if (*ser_line != CHAR_OK) {        /* and get it's reaction */
  141.     fclose(artfp);
  142.     artfp = Nullfp;
  143.     UNLINK(artname);
  144.      errno = ENOENT;        /* Simulate file-not-found */
  145.         return Nullfp;
  146.     }
  147.  
  148.     for (;;) {
  149.         if (get_server(ser_line, sizeof(ser_line)) < 0) {
  150.         fprintf(stderr, "rrn: Unexpected close of server socket.\n");
  151.         finalize(1);
  152.     }
  153.     if (ser_line[0] == '.' && ser_line[1] == '\0')
  154.         break;
  155.     fputs((ser_line[0] == '.' ? ser_line + 1 : ser_line), artfp);
  156.     putc('\n', artfp);
  157.     }
  158.     openpart = function;
  159.     if (function == HEAD)
  160.      putc('\n', artfp); /* req'd blank line after header */
  161.     fseek(artfp, 0L, 0);        /* Then get back to the start */
  162.     openart = artnum;
  163.     return artfp;            /* and return either fp or NULL */
  164. }
  165.  
  166. void
  167. nntpclose()
  168. {
  169.     char artname[32];            /* filename of current article */
  170.     if (our_pid == 0)
  171.     our_pid = getpid();
  172.     sprintf(artname, "/tmp/rrn%ld.%ld", (long) openart, our_pid);
  173.     UNLINK(artname);
  174. }
  175. #endif
  176.