usr.bin/mail/quit.c
/*
 * Terminate an editing session by attempting to write out the user's
 * file from the temporary.  Save any new stuff appended to the file.
 */
Terminate an editing session by attempting to write out the user's
file from the temporary.  Save any new stuff appended to the file.
Terminate an editing session by attempting to write out the user ' s file from the temporary . Save any new stuff appended to the file .
void
edstop()
{
	extern char *tmpdir;
	register int gotcha, c;
	register struct message *mp;
	FILE *obuf, *ibuf, *readstat;
	struct stat statb;
	char tempname[30];
	char *mktemp();
	if (readonly)
		return;
	holdsigs();
	if (Tflag != NOSTR) {
		if ((readstat = Fopen(Tflag, "w")) == NULL)
			Tflag = NOSTR;
	}
	for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
		if (mp->m_flag & MNEW) {
			mp->m_flag &= ~MNEW;
			mp->m_flag |= MSTATUS;
		}
		if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
			gotcha++;
		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
			char *id;
			if ((id = hfield("article-id", mp)) != NOSTR)
				fprintf(readstat, "%s\n", id);
		}
	}
	if (Tflag != NOSTR)
		Fclose(readstat);
	if (!gotcha || Tflag != NOSTR)
		goto done;
	ibuf = NULL;
	if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
		strcpy(tempname, tmpdir);
		strcat(tempname, "mboxXXXXXX");
		mktemp(tempname);
		if ((obuf = Fopen(tempname, "w")) == NULL) {
			perror(tempname);
			relsesigs();
			reset(0);
		}
		if ((ibuf = Fopen(mailname, "r")) == NULL) {
			perror(mailname);
			Fclose(obuf);
			rm(tempname);
			relsesigs();
			reset(0);
		}
		fseek(ibuf, (long)mailsize, 0);
		while ((c = getc(ibuf)) != EOF)
			(void) putc(c, obuf);
		Fclose(ibuf);
		Fclose(obuf);
		if ((ibuf = Fopen(tempname, "r")) == NULL) {
			perror(tempname);
			rm(tempname);
			relsesigs();
			reset(0);
		}
		rm(tempname);
	}
	printf("\"%s\" ", mailname);
	fflush(stdout);
	if ((obuf = Fopen(mailname, "r+")) == NULL) {
		perror(mailname);
		relsesigs();
		reset(0);
	}
	trunc(obuf);
	c = 0;
	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
		if ((mp->m_flag & MDELETED) != 0)
			continue;
		c++;
		if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
			perror(mailname);
			relsesigs();
			reset(0);
		}
	}
	gotcha = (c == 0 && ibuf == NULL);
	if (ibuf != NULL) {
		while ((c = getc(ibuf)) != EOF)
			(void) putc(c, obuf);
		Fclose(ibuf);
	}
	fflush(obuf);
	if (ferror(obuf)) {
		perror(mailname);
		relsesigs();
		reset(0);
	}
	Fclose(obuf);
	if (gotcha) {
		rm(mailname);
		printf("removed\n");
	} else
		printf("complete\n");
	fflush(stdout);
done:
	relsesigs();
}
void edstop ( ) { extern char * tmpdir ; register int gotcha , c ; register struct message * mp ; FILE * obuf , * ibuf , * readstat ; struct stat statb ; char tempname [ 30 ] ; char * mktemp ( ) ; if ( readonly ) return ; holdsigs ( ) ; if ( Tflag != NOSTR ) { if ( ( readstat = Fopen ( Tflag , " " ) ) == NULL ) Tflag = NOSTR ; } for ( mp = & message [ 0 ] , gotcha = 0 ; mp < & message [ msgCount ] ; mp ++ ) { if ( mp -> m_flag & MNEW ) { mp -> m_flag &= ~ MNEW ; mp -> m_flag |= MSTATUS ; } if ( mp -> m_flag & ( MODIFY | MDELETED | MSTATUS ) ) gotcha ++ ; if ( Tflag != NOSTR && ( mp -> m_flag & ( MREAD | MDELETED ) ) != 0 ) { char * id ; if ( ( id = hfield ( " " , mp ) ) != NOSTR ) fprintf ( readstat , " \n " , id ) ; } } if ( Tflag != NOSTR ) Fclose ( readstat ) ; if ( ! gotcha || Tflag != NOSTR ) goto done ; ibuf = NULL ; if ( stat ( mailname , & statb ) >= 0 && statb . st_size > mailsize ) { strcpy ( tempname , tmpdir ) ; strcat ( tempname , " " ) ; mktemp ( tempname ) ; if ( ( obuf = Fopen ( tempname , " " ) ) == NULL ) { perror ( tempname ) ; relsesigs ( ) ; reset ( 0 ) ; } if ( ( ibuf = Fopen ( mailname , " " ) ) == NULL ) { perror ( mailname ) ; Fclose ( obuf ) ; rm ( tempname ) ; relsesigs ( ) ; reset ( 0 ) ; } fseek ( ibuf , ( long ) mailsize , 0 ) ; while ( ( c = getc ( ibuf ) ) != EOF ) ( void ) putc ( c , obuf ) ; Fclose ( ibuf ) ; Fclose ( obuf ) ; if ( ( ibuf = Fopen ( tempname , " " ) ) == NULL ) { perror ( tempname ) ; rm ( tempname ) ; relsesigs ( ) ; reset ( 0 ) ; } rm ( tempname ) ; } printf ( " \" \" " , mailname ) ; fflush ( stdout ) ; if ( ( obuf = Fopen ( mailname , " " ) ) == NULL ) { perror ( mailname ) ; relsesigs ( ) ; reset ( 0 ) ; } trunc ( obuf ) ; c = 0 ; for ( mp = & message [ 0 ] ; mp < & message [ msgCount ] ; mp ++ ) { if ( ( mp -> m_flag & MDELETED ) != 0 ) continue ; c ++ ; if ( send ( mp , obuf , ( struct ignoretab * ) NULL , NOSTR ) < 0 ) { perror ( mailname ) ; relsesigs ( ) ; reset ( 0 ) ; } } gotcha = ( c == 0 && ibuf == NULL ) ; if ( ibuf != NULL ) { while ( ( c = getc ( ibuf ) ) != EOF ) ( void ) putc ( c , obuf ) ; Fclose ( ibuf ) ; } fflush ( obuf ) ; if ( ferror ( obuf ) ) { perror ( mailname ) ; relsesigs ( ) ; reset ( 0 ) ; } Fclose ( obuf ) ; if ( gotcha ) { rm ( mailname ) ; printf ( " \n " ) ; } else printf ( " \n " ) ; fflush ( stdout ) ; done : relsesigs ( ) ; }
Terminate an editing session by attempting to write out the user's
 file from the temporary.
Terminate an editing session by attempting to write out the user ' s file from the temporary .