17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/*
327c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bdstevel@tonic-gate * The Regents of the University of California
347c478bdstevel@tonic-gate * All Rights Reserved
357c478bdstevel@tonic-gate *
367c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
377c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
387c478bdstevel@tonic-gate * contributors.
397c478bdstevel@tonic-gate */
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate#include "rcv.h"
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#undef	fopen
467c478bdstevel@tonic-gate#undef	fclose
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate/*
497c478bdstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
507c478bdstevel@tonic-gate *	mail program
517c478bdstevel@tonic-gate *
527c478bdstevel@tonic-gate * Local version of fopen() and fclose(). These maintain a list of
537c478bdstevel@tonic-gate * file pointers which can be run down when we need to close
547c478bdstevel@tonic-gate * all files, such as before executing external commands.
557c478bdstevel@tonic-gate */
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gatestatic NODE	*append();
587c478bdstevel@tonic-gatestatic NODE	*del1();
597c478bdstevel@tonic-gatestatic NODE	*getnode();
607c478bdstevel@tonic-gatestatic NODE	*search();
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gatestatic NODE *
637c478bdstevel@tonic-gategetnode(FILE *fp)
647c478bdstevel@tonic-gate{
657c478bdstevel@tonic-gate	NODE *newnode;
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	if ((newnode = (NODE *)malloc(sizeof(NODE))) == (NODE *)NULL) {
687c478bdstevel@tonic-gate		(void) fputs("Cannot allocate node space\n", stderr);
697c478bdstevel@tonic-gate		exit(3);
707c478bdstevel@tonic-gate	}
717c478bdstevel@tonic-gate	newnode->fp = fp;
727c478bdstevel@tonic-gate	newnode->next = (NODE *)NULL;
737c478bdstevel@tonic-gate	return(newnode);
747c478bdstevel@tonic-gate}
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gatestatic NODE *
777c478bdstevel@tonic-gatesearch(FILE *fp)
787c478bdstevel@tonic-gate{
797c478bdstevel@tonic-gate	register NODE *tmp;
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate	for (tmp = fplist; tmp != (NODE *)NULL; tmp = tmp->next)
827c478bdstevel@tonic-gate		if (tmp->fp == fp)
837c478bdstevel@tonic-gate			break;
847c478bdstevel@tonic-gate	return( tmp != (NODE *)NULL ? tmp : NOFP);
857c478bdstevel@tonic-gate}
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gatestatic NODE *
887c478bdstevel@tonic-gateappend(FILE *fp)
897c478bdstevel@tonic-gate{
907c478bdstevel@tonic-gate	register NODE *newnode;
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate	if ((newnode = getnode(fp)) == (NODE *)NULL)
937c478bdstevel@tonic-gate		return(NOFP);
947c478bdstevel@tonic-gate	if (fplist == NOFP) {
957c478bdstevel@tonic-gate		fplist = newnode;
967c478bdstevel@tonic-gate	} else {
977c478bdstevel@tonic-gate		newnode->next = curptr->next;
987c478bdstevel@tonic-gate		curptr->next = newnode;
997c478bdstevel@tonic-gate	}
1007c478bdstevel@tonic-gate	return(newnode);
1017c478bdstevel@tonic-gate}
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gatestatic NODE *
1047c478bdstevel@tonic-gatedel1(NODE *oldcur)
1057c478bdstevel@tonic-gate{
1067c478bdstevel@tonic-gate	register NODE *cur, *prev;
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate	for (prev = cur = fplist; cur != (NODE *)NULL; cur = cur->next) {
1097c478bdstevel@tonic-gate		if (cur == oldcur) {
1107c478bdstevel@tonic-gate			if (cur == fplist) {
1117c478bdstevel@tonic-gate				cur = fplist = cur->next;
1127c478bdstevel@tonic-gate			} else {
1137c478bdstevel@tonic-gate				prev->next = cur->next;
1147c478bdstevel@tonic-gate				cur = prev->next ? prev->next : prev;
1157c478bdstevel@tonic-gate			}
1167c478bdstevel@tonic-gate			if (curptr == oldcur)
1177c478bdstevel@tonic-gate				curptr = prev;
1187c478bdstevel@tonic-gate			free(oldcur);
1197c478bdstevel@tonic-gate			break;
1207c478bdstevel@tonic-gate		}
1217c478bdstevel@tonic-gate		prev = cur;
1227c478bdstevel@tonic-gate	}
1237c478bdstevel@tonic-gate	return(cur);
1247c478bdstevel@tonic-gate}
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gateFILE *
1277c478bdstevel@tonic-gatemy_fopen(char *file, char *mode)
1287c478bdstevel@tonic-gate{
1297c478bdstevel@tonic-gate	FILE *fp;
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gate	if ((fp = fopen(file, mode)) == (FILE *)NULL) {
1327c478bdstevel@tonic-gate		fplist = NOFP;
1337c478bdstevel@tonic-gate		return(fp);
1347c478bdstevel@tonic-gate	} else {
1357c478bdstevel@tonic-gate		curptr = append(fp);
1367c478bdstevel@tonic-gate	}
1377c478bdstevel@tonic-gate	return(fp);
1387c478bdstevel@tonic-gate}
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gateint
1417c478bdstevel@tonic-gatemy_fclose(register FILE *iop)
1427c478bdstevel@tonic-gate{
1437c478bdstevel@tonic-gate	register NODE *cur;
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	int ret = fclose(iop);
1467c478bdstevel@tonic-gate	if (fplist != NOFP) {
1477c478bdstevel@tonic-gate		cur = search(iop);
1487c478bdstevel@tonic-gate		cur = del1(cur);
1497c478bdstevel@tonic-gate	}
1507c478bdstevel@tonic-gate	return ret;
1517c478bdstevel@tonic-gate}
152