home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume23
/
trn
/
part13
/
artio.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-22
|
5KB
|
188 lines
/* $Header: artio.c,v 4.3.3.3 91/01/16 02:20:48 davison Trn $
*
* $Log: artio.c,v $
* Revision 4.3.3.3 91/01/16 02:20:48 davison
* Integrated rn patches 48-54.
*
* Revision 4.3.3.2 90/08/20 16:25:50 davison
* Fixed bug in artopen ==> nntpopen interaction.
*
* Revision 4.3.3.1 90/07/21 20:11:03 davison
* Initial Trn Release
*
* Revision 4.3.2.6 90/11/22 16:06:57 sob
* Changes to make pickly C preprocessors happier.
*
* Revision 4.3.2.5 90/03/22 23:04:04 sob
* Fixes provided by Wayne Davison <drivax!davison>
*
* Revision 4.3.2.4 89/11/27 01:29:57 sob
* Altered NNTP code per ideas suggested by Bela Lubkin
* <filbo@gorn.santa-cruz.ca.us>
*
* Revision 4.3.2.3 89/11/26 22:55:31 sob
* Add nntpopen() and nntpclose() routines to cut down on size of rrn
*
* Revision 4.3.2.2 89/11/08 01:17:12 sob
* Added changes to insure that this will compile for RN or RRN with no
* changes to the source code.
*
* Revision 4.3.2.1 89/11/06 00:07:25 sob
* Added RRN support from NNTP 1.5
*
* Revision 4.3 85/05/01 11:35:39 lwall
* Baseline for release with 4.3bsd.
*
*/
#include "EXTERN.h"
#include "common.h"
#ifdef SERVER
#include "server.h"
#endif
#include "INTERN.h"
#include "artio.h"
void
artio_init()
{
;
}
/* open an article, unless it's already open */
FILE *
artopen(artnum)
ART_NUM artnum;
{
#ifdef SERVER
return nntpopen(artnum,GET_ARTICLE);
#else
char artname[32]; /* filename of current article */
if (artnum < 1)
return Nullfp;
if (openart == artnum) { /* this article is already open? */
fseek(artfp,0L,0); /* just get to the beginning */
return artfp; /* and say we succeeded */
}
if (artfp != Nullfp) { /* it was somebody else? */
fclose(artfp); /* put them out of their misery */
openart = 0; /* and remember them no more */
}
sprintf(artname,"%ld",(long)artnum);
/* produce the name of the article */
if (artfp = fopen(artname,"r")) /* if we can open it */
openart = artnum; /* remember what we did here */
#ifdef LINKART
{
char tmpbuf[256];
char *s;
if (fstat(fileno(artfp),&filestat))
return artfp;
if (filestat.st_size < (sizeof tmpbuf)) {
fgets(tmpbuf,(sizeof tmpbuf),artfp);
if (*tmpbuf == '/') { /* is a "link" to another article */
fclose(artfp);
if (s=index(tmpbuf,'\n'))
*s = '\0';
if (!(artfp = fopen(tmpbuf,"r")))
openart = 0;
else {
if (*linkartname)
free(linkartname);
linkartname = savestr(tmpbuf);
}
}
else
fseek(artfp,0L,0); /* get back to the beginning */
}
}
#endif
return artfp; /* and return either fp or NULL */
#endif /* SERVER */
}
#ifdef SERVER
static long our_pid=0;
FILE *
nntpopen(artnum,function)
ART_NUM artnum;
ART_PART function;
{
char ser_line[256];
char artname[32]; /* filename of current article */
if (our_pid == 0)
our_pid = getpid();
if (artnum < 1)
return Nullfp;
if ((openart == artnum) && (openpart >= function))
{ /* this article is already open? */
fseek(artfp,0L,0); /* just get to the beginning */
return artfp; /* and say we succeeded */
}
if (artfp != Nullfp) { /* it was somebody else? */
fclose(artfp); /* put them out of their misery */
nntpclose();
openart = 0; /* and remember them no more */
}
sprintf(artname,"/tmp/rrn%ld.%ld", (long) artnum, our_pid);
artfp = fopen(artname, "w+"); /* create the temporary article */
if (artfp == Nullfp) {
UNLINK(artname);
return Nullfp;
}
switch (function){
case GET_STATUS:
function = GET_HEADER; /* fall through */
case GET_HEADER:
sprintf(ser_line, "HEAD %ld", (long)artnum);
break;
case GET_ARTICLE:
sprintf(ser_line, "ARTICLE %ld", (long)artnum);
break;
}
put_server(ser_line); /* ask the server for the article */
if (get_server(ser_line, sizeof(ser_line)) < 0) {
fprintf(stderr, "rrn: Unexpected close of server socket.\n");
finalize(1);
}
if (*ser_line != CHAR_OK) { /* and get it's reaction */
fclose(artfp);
artfp = Nullfp;
UNLINK(artname);
errno = ENOENT; /* Simulate file-not-found */
return Nullfp;
}
for (;;) {
if (get_server(ser_line, sizeof(ser_line)) < 0) {
fprintf(stderr, "rrn: Unexpected close of server socket.\n");
finalize(1);
}
if (ser_line[0] == '.' && ser_line[1] == '\0')
break;
fputs((ser_line[0] == '.' ? ser_line + 1 : ser_line), artfp);
putc('\n', artfp);
}
openpart = function;
if (function == GET_HEADER)
putc('\n', artfp); /* req'd blank line after header */
fseek(artfp, 0L, 0); /* Then get back to the start */
openart = artnum;
return artfp; /* and return either fp or NULL */
}
void
nntpclose()
{
char artname[32]; /* filename of current article */
if (our_pid == 0)
our_pid = getpid();
sprintf(artname, "/tmp/rrn%ld.%ld", (long) openart, our_pid);
UNLINK(artname);
}
#endif