home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / artio.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  6KB  |  204 lines

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