xref: /illumos-gate/usr/src/cmd/mailx/myfopen.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 /*
32  * University Copyright- Copyright (c) 1982, 1986, 1988
33  * The Regents of the University of California
34  * All Rights Reserved
35  *
36  * University Acknowledgment- Portions of this document are derived from
37  * software developed by the University of California, Berkeley, and its
38  * contributors.
39  */
40 
41 #include "rcv.h"
42 
43 #undef	fopen
44 #undef	fclose
45 
46 /*
47  * mailx -- a modified version of a University of California at Berkeley
48  *	mail program
49  *
50  * Local version of fopen() and fclose(). These maintain a list of
51  * file pointers which can be run down when we need to close
52  * all files, such as before executing external commands.
53  */
54 
55 static NODE	*append();
56 static NODE	*del1();
57 static NODE	*getnode();
58 static NODE	*search();
59 
60 static NODE *
61 getnode(FILE *fp)
62 {
63 	NODE *newnode;
64 
65 	if ((newnode = (NODE *)malloc(sizeof(NODE))) == (NODE *)NULL) {
66 		(void) fputs("Cannot allocate node space\n", stderr);
67 		exit(3);
68 	}
69 	newnode->fp = fp;
70 	newnode->next = (NODE *)NULL;
71 	return(newnode);
72 }
73 
74 static NODE *
75 search(FILE *fp)
76 {
77 	register NODE *tmp;
78 
79 	for (tmp = fplist; tmp != (NODE *)NULL; tmp = tmp->next)
80 		if (tmp->fp == fp)
81 			break;
82 	return( tmp != (NODE *)NULL ? tmp : NOFP);
83 }
84 
85 static NODE *
86 append(FILE *fp)
87 {
88 	register NODE *newnode;
89 
90 	if ((newnode = getnode(fp)) == (NODE *)NULL)
91 		return(NOFP);
92 	if (fplist == NOFP) {
93 		fplist = newnode;
94 	} else {
95 		newnode->next = curptr->next;
96 		curptr->next = newnode;
97 	}
98 	return(newnode);
99 }
100 
101 static NODE *
102 del1(NODE *oldcur)
103 {
104 	register NODE *cur, *prev;
105 
106 	for (prev = cur = fplist; cur != (NODE *)NULL; cur = cur->next) {
107 		if (cur == oldcur) {
108 			if (cur == fplist) {
109 				cur = fplist = cur->next;
110 			} else {
111 				prev->next = cur->next;
112 				cur = prev->next ? prev->next : prev;
113 			}
114 			if (curptr == oldcur)
115 				curptr = prev;
116 			free(oldcur);
117 			break;
118 		}
119 		prev = cur;
120 	}
121 	return(cur);
122 }
123 
124 FILE *
125 my_fopen(char *file, char *mode)
126 {
127 	FILE *fp;
128 
129 	if ((fp = fopen(file, mode)) == (FILE *)NULL) {
130 		fplist = NOFP;
131 		return(fp);
132 	} else {
133 		curptr = append(fp);
134 	}
135 	return(fp);
136 }
137 
138 int
139 my_fclose(register FILE *iop)
140 {
141 	register NODE *cur;
142 
143 	int ret = fclose(iop);
144 	if (fplist != NOFP) {
145 		cur = search(iop);
146 		cur = del1(cur);
147 	}
148 	return ret;
149 }
150