home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part02 / artio.c next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  5.0 KB  |  188 lines

  1. /* $Id: artio.c,v 4.4 1991/09/09 20:18:23 sob Exp sob $
  2.  *
  3.  * $Log: artio.c,v $
  4.  * Revision 4.4  1991/09/09  20:18:23  sob
  5.  * release 4.4
  6.  *
  7.  *
  8.  * 
  9.  */
  10. /* This software is Copyright 1991 by Stan Barber. 
  11.  *
  12.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  13.  * use this software as long as: there is no monetary profit gained
  14.  * specifically from the use or reproduction of this software, it is not
  15.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  16.  * included prominently in any copy made. 
  17.  *
  18.  * The author make no claims as to the fitness or correctness of this software
  19.  * for any use whatsoever, and it is provided as is. Any use of this software
  20.  * is at the user's own risk. 
  21.  */
  22.  
  23. #include "EXTERN.h"
  24. #include "common.h"
  25. #ifdef SERVER
  26. #include "server.h"
  27. #endif
  28. #include "INTERN.h"
  29. #include "artio.h"
  30.  
  31. void
  32. artio_init()
  33. {
  34.     ;
  35. }
  36.  
  37. /* open an article, unless it's already open */
  38.  
  39. FILE *
  40. artopen(artnum)
  41. ART_NUM artnum;
  42. {
  43. #ifdef SERVER
  44.     return nntpopen(artnum,GET_ARTICLE);
  45. #else
  46.     char artname[MAXFILENAME];        /* filename of current article */
  47.  
  48.     if (artnum < 1)
  49.     return Nullfp;
  50.     if (openart == artnum) {        /* this article is already open? */
  51.     fseek(artfp,0L,0);        /* just get to the beginning */
  52.     return artfp;            /* and say we succeeded */
  53.     }
  54.     if (artfp != Nullfp) {        /* it was somebody else? */
  55.     fclose(artfp);            /* put them out of their misery */
  56.     openart = 0;            /* and remember them no more */
  57.     }
  58.     sprintf(artname,"%ld",(long)artnum);
  59.                     /* produce the name of the article */
  60.     if (artfp = fopen(artname,"r"))    /* if we can open it */
  61.     openart = artnum;        /* remember what we did here */
  62. #ifdef LINKART
  63.     {
  64.     char tmpbuf[256];
  65.     char *s;
  66.  
  67.     if (fstat(fileno(artfp),&filestat))
  68.         return artfp;
  69.     if (filestat.st_size < (sizeof tmpbuf)) {
  70.         fgets(tmpbuf,(sizeof tmpbuf),artfp);
  71.         if (*tmpbuf == '/') {    /* is a "link" to another article */
  72.         fclose(artfp);
  73.         if (s=index(tmpbuf,'\n'))
  74.             *s = '\0';
  75.         if (!(artfp = fopen(tmpbuf,"r")))
  76.             openart = 0;
  77.         else {
  78.             if (*linkartname)
  79.             free(linkartname);
  80.             linkartname = savestr(tmpbuf);
  81.         }
  82.         }
  83.         else
  84.         fseek(artfp,0L,0);        /* get back to the beginning */
  85.     }
  86.     }
  87. #endif
  88.     return artfp;            /* and return either fp or NULL */
  89. #endif /* SERVER */
  90. }
  91.  
  92. #ifdef SERVER
  93. static long our_pid=0;
  94.  
  95. FILE *
  96. nntpopen(artnum,function)
  97. ART_NUM artnum;
  98. ART_PART function;
  99. {
  100.     char artname[MAXFILENAME];        /* filename of current article */
  101.     char intrpwork[MAXFILENAME];    /* filename of current article */
  102.     if (our_pid == 0)
  103.     our_pid = getpid();
  104.     if (artnum < 1)
  105.     return Nullfp;
  106.     if ((openart == artnum) && (openpart >= function))
  107.     {                    /* this article is already open? */
  108.     fseek(artfp,0L,0);        /* just get to the beginning */
  109.     return artfp;            /* and say we succeeded */
  110.     }
  111.     if (artfp != Nullfp) {        /* it was somebody else? */
  112.     fclose(artfp);            /* put them out of their misery */
  113.     nntpclose();
  114.     openart = 0;            /* and remember them no more */
  115.     }
  116.     interp(intrpwork,MAXFILENAME-1,"%P");
  117.     sprintf(artname,"%s/rrn%ld.%ld", intrpwork,(long) artnum, our_pid);
  118.     artfp = fopen(artname, "w+");    /* create the temporary article */
  119.     if (artfp == Nullfp) {
  120.     UNLINK(artname);
  121.     return Nullfp;
  122.     }
  123.     switch (function){
  124.         case GET_STATUS:
  125.         function = GET_HEADER;    /* fall through */
  126.         case GET_HEADER:
  127.         sprintf(ser_line, "HEAD %ld", (long)artnum);
  128.         break;
  129.         case GET_ARTICLE:
  130.         sprintf(ser_line, "ARTICLE %ld", (long)artnum);
  131.         break;
  132.     }        
  133. #ifdef DEBUGGING
  134.     if (debug & DEB_NNTP)
  135.     printf(">%s\n", ser_line) FLUSH;
  136. #endif
  137.     put_server(ser_line);        /* ask the server for the article */
  138.     if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  139.     fprintf(stderr, "\nrrn: Unexpected close of server socket.\n");
  140.     finalize(1);
  141.     }
  142. #ifdef DEBUGGING
  143.     if (debug & DEB_NNTP)
  144.     printf("<%s\n", ser_line) FLUSH;
  145. #endif
  146.     if (*ser_line == CHAR_FATAL) {    /* Fatal error */
  147.         fprintf(stderr,"\nrrn: %s\n",ser_line);
  148.         finalize(1);
  149.     }
  150.     if (*ser_line != CHAR_OK) {        /* and get it's reaction */
  151.     fclose(artfp);
  152.     artfp = Nullfp;
  153.     UNLINK(artname);
  154.      errno = ENOENT;        /* Simulate file-not-found */
  155.         return Nullfp;
  156.     }
  157.  
  158.     for (;;) {
  159.         if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  160.         fprintf(stderr, "\nrrn: Unexpected close of server socket.\n");
  161.         finalize(1);
  162.     }
  163.     if (ser_line[0] == '.' && ser_line[1] == '\0')
  164.         break;
  165.     fputs((ser_line[0] == '.' ? ser_line + 1 : ser_line), artfp);
  166.     putc('\n', artfp);
  167.     }
  168.     openpart = function;
  169.     if (function == GET_HEADER)
  170.      putc('\n', artfp); /* req'd blank line after header */
  171.     fseek(artfp, 0L, 0);        /* Then get back to the start */
  172.     openart = artnum;
  173.     return artfp;            /* and return either fp or NULL */
  174. }
  175.  
  176. void
  177. nntpclose()
  178. {
  179.     char artname[MAXFILENAME];        /* filename of current article */
  180.     char intrpwork[MAXFILENAME];    /* filename of current article */
  181.     if (our_pid == 0)
  182.     our_pid = getpid();
  183.     interp(intrpwork,MAXFILENAME-1,"%P");
  184.     sprintf(artname, "%s/rrn%ld.%ld", intrpwork,(long) openart, our_pid);
  185.     UNLINK(artname);
  186. }
  187. #endif
  188.