newsrc.c   newsrc.c 
/* ========================================================================
* Copyright 1988-2006 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* ========================================================================
*/
/* /*
* Program: Newsrc manipulation routines * Program: Newsrc manipulation routines
* *
* Author: Mark Crispin * Author: Mark Crispin
* Networks and Distributed Computing * Networks and Distributed Computing
* Computing & Communications * Computing & Communications
* University of Washington * University of Washington
* Administration Building, AG-44 * Administration Building, AG-44
* Seattle, WA 98195 * Seattle, WA 98195
* Internet: MRC@CAC.Washington.EDU * Internet: MRC@CAC.Washington.EDU
* *
* Date: 12 September 1994 * Date: 12 September 1994
* Last Edited: 27 April 2004 * Last Edited: 30 August 2006
*
* The IMAP toolkit provided in this Distribution is
* Copyright 1988-2004 University of Washington.
* The full text of our legal notices is contained in the file called
* CPYRIGHT, included with this Distribution.
*/ */
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include "mail.h" #include "mail.h"
#include "osdep.h" #include "osdep.h"
#include "misc.h" #include "misc.h"
#include "newsrc.h" #include "newsrc.h"
#ifndef OLDFILESUFFIX #ifndef OLDFILESUFFIX
skipping to change at line 55 skipping to change at line 63
/* Write error message /* Write error message
* Accepts: newsrc name * Accepts: newsrc name
* file designator * file designator
* file designator * file designator
* Returns: NIL, always * Returns: NIL, always
*/ */
long newsrc_write_error (char *name,FILE *f1,FILE *f2) long newsrc_write_error (char *name,FILE *f1,FILE *f2)
{ {
fclose (f1); /* close file designators */ if (f1) fclose (f1); /* close file designators */
fclose (f2); if (f2) fclose (f2);
return newsrc_error ("Error writing to %.80s",name,ERROR); return newsrc_error ("Error writing to %.80s",name,ERROR);
} }
/* Create newsrc file in local form /* Create newsrc file in local form
* Accepts: MAIL stream * Accepts: MAIL stream
* notification flag * notification flag
* Returns: file designator of newsrc * Returns: file designator of newsrc
*/ */
FILE *newsrc_create (MAILSTREAM *stream,int notify) FILE *newsrc_create (MAILSTREAM *stream,int notify)
skipping to change at line 84 skipping to change at line 92
/* Write new state in newsrc /* Write new state in newsrc
* Accepts: file designator of newsrc * Accepts: file designator of newsrc
* group * group
* new subscription status character * new subscription status character
* newline convention * newline convention
* Returns: T if successful, NIL otherwise * Returns: T if successful, NIL otherwise
*/ */
long newsrc_newstate (FILE *f,char *group,char state,char *nl) long newsrc_newstate (FILE *f,char *group,char state,char *nl)
{ {
return (f && (fputs (group,f) != EOF) && ((putc (state,f)) != EOF) && long ret = (f && (fputs (group,f) != EOF) && ((putc (state,f)) != EOF) &&
((putc (' ',f)) != EOF) && (fputs (nl,f) != EOF) && ((putc (' ',f)) != EOF) && (fputs (nl,f) != EOF)) ? LONGT : NI
(fclose (f) != EOF)) ? LONGT : NIL; L;
if (fclose (f) == EOF) ret = NIL;
return ret;
} }
/* Write messages in newsrc /* Write messages in newsrc
* Accepts: file designator of newsrc * Accepts: file designator of newsrc
* MAIL stream * MAIL stream
* message number/newsgroup message map * message number/newsgroup message map
* newline convention * newline convention
* Returns: T if successful, NIL otherwise * Returns: T if successful, NIL otherwise
*/ */
skipping to change at line 218 skipping to change at line 227
} while (c != EOF); } while (c != EOF);
if (f) { /* still haven't written it yet? */ if (f) { /* still haven't written it yet? */
if (nl[0]) { /* know its newline convention? */ if (nl[0]) { /* know its newline convention? */
fseek (f,0L,2); /* yes, seek to end of file */ fseek (f,0L,2); /* yes, seek to end of file */
ret = newsrc_newstate (f,group,state,nl); ret = newsrc_newstate (f,group,state,nl);
} }
else { /* can't find a newline convention */ else { /* can't find a newline convention */
fclose (f); /* punt the file */ fclose (f); /* punt the file */
/* can't win if read something */ /* can't win if read something */
if (pos) newsrc_error ("Unknown newline convention in %.80s", if (pos) newsrc_error ("Unknown newline convention in %.80s",
newsrc,ERROR); newsrc,ERROR);
/* file must have been empty, rewrite it */ /* file must have been empty, rewrite it */
else ret = newsrc_newstate(newsrc_create(stream,NIL),group,state,"\n "); else ret = newsrc_newstate(newsrc_create(stream,NIL),group,state,"\n ");
} }
} }
} }
/* create new file */ /* create new file */
else ret = newsrc_newstate (newsrc_create (stream,T),group,state,"\n"); else ret = newsrc_newstate (newsrc_create (stream,T),group,state,"\n");
return ret; /* return with update status */ return ret; /* return with update status */
} }
/* Update newsgroup status in stream /* Update newsgroup status in stream
skipping to change at line 318 skipping to change at line 327
return recent; return recent;
} }
/* Update newsgroup entry in newsrc /* Update newsgroup entry in newsrc
* Accepts: newsgroup name * Accepts: newsgroup name
* MAIL stream * MAIL stream
* Returns: T if successful, NIL otherwise * Returns: T if successful, NIL otherwise
*/ */
long newsrc_write (char *group,MAILSTREAM *stream) long newsrc_write (char *group,MAILSTREAM *stream)
{ {
long ret = NIL;
int c = 0,d = EOF; int c = 0,d = EOF;
char *newsrc = (char *) mail_parameters (stream,GET_NEWSRC,stream); char *newsrc = (char *) mail_parameters (stream,GET_NEWSRC,stream);
char *s,tmp[MAILTMPLEN],backup[MAILTMPLEN],nl[3]; char *s,tmp[MAILTMPLEN],backup[MAILTMPLEN],nl[3];
FILE *f,*bf; FILE *f,*bf;
nl[0] = nl[1] = nl[2] = '\0'; /* no newline known yet */ nl[0] = nl[1] = nl[2] = '\0'; /* no newline known yet */
if (f = fopen (newsrc,"rb")) {/* have existing newsrc file? */ if (f = fopen (newsrc,"rb")) {/* have existing newsrc file? */
if (!(bf = fopen ((strcat (strcpy (backup,newsrc),OLDFILESUFFIX)),"wb") )) { if (!(bf = fopen ((strcat (strcpy (backup,newsrc),OLDFILESUFFIX)),"wb") )) {
fclose (f); /* punt input file */ fclose (f); /* punt input file */
return newsrc_error("Can't create backup news state %.80s",backup,ERR OR); return newsrc_error("Can't create backup news state %.80s",backup,ERR OR);
} }
skipping to change at line 360 skipping to change at line 370
} }
else if (!nl[0]) /* make sure newlines valid */ else if (!nl[0]) /* make sure newlines valid */
return newsrc_error ("Unknown newline convention in %.80s",newsrc,ERR OR); return newsrc_error ("Unknown newline convention in %.80s",newsrc,ERR OR);
/* now read backup file */ /* now read backup file */
else if (!(bf = fopen (backup,"rb"))) else if (!(bf = fopen (backup,"rb")))
return newsrc_error ("Error reading backup news state %.80s", return newsrc_error ("Error reading backup news state %.80s",
backup,ERROR); backup,ERROR);
/* open newsrc for writing */ /* open newsrc for writing */
else if (!(f = fopen (newsrc,"wb"))) { else if (!(f = fopen (newsrc,"wb"))) {
fclose (bf); /* punt backup */ fclose (bf); /* punt backup */
return newsrc_error ("Can't rewrite news state %.80s", return newsrc_error ("Can't rewrite news state %.80s",newsrc,ERROR);
newsrc,ERROR);
} }
} }
else { /* create new newsrc file */ else { /* create new newsrc file */
if (f = newsrc_create (stream,T)) bf = NIL; if (f = newsrc_create (stream,T)) bf = NIL;
else return NIL; /* can't create newsrc */ else return NIL; /* can't create newsrc */
} }
while (bf) { /* read newsrc */ while (bf) { /* read newsrc */
for (s = tmp; (s < (tmp + MAILTMPLEN - 1)) && ((c = getc (bf)) != EOF) && for (s = tmp; (s < (tmp + MAILTMPLEN - 1)) && ((c = getc (bf)) != EOF) &&
(c != ':') && (c != '!') && (c != '\015') && (c != '\012'); *s++ = c); (c != ':') && (c != '!') && (c != '\015') && (c != '\012'); *s++ = c);
*s = '\0'; /* tie off name */ *s = '\0'; /* tie off name */
skipping to change at line 390 skipping to change at line 399
return newsrc_write_error (newsrc,bf,f); return newsrc_write_error (newsrc,bf,f);
/* skip past old data */ /* skip past old data */
while (((c = getc (bf)) != EOF) && (c != '\015') && (c != '\012')); while (((c = getc (bf)) != EOF) && (c != '\015') && (c != '\012'));
/* skip past newline */ /* skip past newline */
while ((c == '\015') || (c == '\012')) c = getc (bf); while ((c == '\015') || (c == '\012')) c = getc (bf);
while (c != EOF) { /* copy remainder of file */ while (c != EOF) { /* copy remainder of file */
if (putc (c,f) == EOF) return newsrc_write_error (newsrc,bf,f); if (putc (c,f) == EOF) return newsrc_write_error (newsrc,bf,f);
c = getc (bf); /* get next character */ c = getc (bf); /* get next character */
} }
/* done with file */ /* done with file */
if (fclose (f) == EOF) return newsrc_write_error (newsrc,bf,f); if (fclose (f) == EOF) return newsrc_write_error (newsrc,bf,NIL);
f = NIL; f = NIL;
} }
/* copy remainder of line */ /* copy remainder of line */
else while (((c = getc (bf)) != EOF) && (c != '\015') && (c != '\012' )) else while (((c = getc (bf)) != EOF) && (c != '\015') && (c != '\012' ))
if (putc (c,f) == EOF) return newsrc_write_error (newsrc,bf,f); if (putc (c,f) == EOF) return newsrc_write_error (newsrc,bf,f);
if (c == '\015') { /* write CR if seen */ if (c == '\015') { /* write CR if seen */
if (putc (c,f) == EOF) return newsrc_write_error (newsrc,bf,f); if (putc (c,f) == EOF) return newsrc_write_error (newsrc,bf,f);
/* sniff to see if LF */ /* sniff to see if LF */
if (((c = getc (bf)) != EOF) && (c != '\012')) ungetc (c,bf); if (((c = getc (bf)) != EOF) && (c != '\012')) ungetc (c,bf);
} }
/* write LF if seen */ /* write LF if seen */
if ((c == '\012') && (putc (c,f) == EOF)) if ((c == '\012') && (putc (c,f) == EOF))
return newsrc_write_error (newsrc,bf,f); return newsrc_write_error (newsrc,bf,f);
} }
if (c == EOF) { /* hit end of file? */ if (c == EOF) { /* hit end of file? */
fclose (bf); /* yup, close the file */ fclose (bf); /* yup, close the file */
bf = NIL; bf = NIL;
} }
} }
if (f) { /* still have newsrc file open? */ if (f) { /* still have newsrc file open? */
if ((fputs (group,f) == EOF) || ((putc (':',f)) == EOF) || ret = ((fputs (group,f) != EOF) && ((putc (':',f)) != EOF) &&
(!newsrc_newmessages (f,stream,nl[0] ? nl : "\n")) || newsrc_newmessages (f,stream,nl[0] ? nl : "\n")) ? LONGT : NIL;
(fclose (f) == EOF)) return newsrc_write_error (newsrc,bf,f); if (fclose (f) == EOF) ret = newsrc_write_error (newsrc,NIL,NIL);
} }
return LONGT; else ret = LONGT;
return ret;
} }
/* Get newsgroup state as text stream /* Get newsgroup state as text stream
* Accepts: MAIL stream * Accepts: MAIL stream
* newsgroup name * newsgroup name
* Returns: string containing newsgroup state, or NIL if not found * Returns: string containing newsgroup state, or NIL if not found
*/ */
char *newsrc_state (MAILSTREAM *stream,char *group) char *newsrc_state (MAILSTREAM *stream,char *group)
{ {
int c = 0; int c = 0;
 End of changes. 10 change blocks. 
19 lines changed or deleted 30 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/