xref: /titanic_51/usr/src/cmd/mailx/myfopen.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #include "rcv.h"
44 
45 #undef	fopen
46 #undef	fclose
47 
48 /*
49  * mailx -- a modified version of a University of California at Berkeley
50  *	mail program
51  *
52  * Local version of fopen() and fclose(). These maintain a list of
53  * file pointers which can be run down when we need to close
54  * all files, such as before executing external commands.
55  */
56 
57 static NODE	*append();
58 static NODE	*del1();
59 static NODE	*getnode();
60 static NODE	*search();
61 
62 static NODE *
63 getnode(FILE *fp)
64 {
65 	NODE *newnode;
66 
67 	if ((newnode = (NODE *)malloc(sizeof(NODE))) == (NODE *)NULL) {
68 		(void) fputs("Cannot allocate node space\n", stderr);
69 		exit(3);
70 	}
71 	newnode->fp = fp;
72 	newnode->next = (NODE *)NULL;
73 	return(newnode);
74 }
75 
76 static NODE *
77 search(FILE *fp)
78 {
79 	register NODE *tmp;
80 
81 	for (tmp = fplist; tmp != (NODE *)NULL; tmp = tmp->next)
82 		if (tmp->fp == fp)
83 			break;
84 	return( tmp != (NODE *)NULL ? tmp : NOFP);
85 }
86 
87 static NODE *
88 append(FILE *fp)
89 {
90 	register NODE *newnode;
91 
92 	if ((newnode = getnode(fp)) == (NODE *)NULL)
93 		return(NOFP);
94 	if (fplist == NOFP) {
95 		fplist = newnode;
96 	} else {
97 		newnode->next = curptr->next;
98 		curptr->next = newnode;
99 	}
100 	return(newnode);
101 }
102 
103 static NODE *
104 del1(NODE *oldcur)
105 {
106 	register NODE *cur, *prev;
107 
108 	for (prev = cur = fplist; cur != (NODE *)NULL; cur = cur->next) {
109 		if (cur == oldcur) {
110 			if (cur == fplist) {
111 				cur = fplist = cur->next;
112 			} else {
113 				prev->next = cur->next;
114 				cur = prev->next ? prev->next : prev;
115 			}
116 			if (curptr == oldcur)
117 				curptr = prev;
118 			free(oldcur);
119 			break;
120 		}
121 		prev = cur;
122 	}
123 	return(cur);
124 }
125 
126 FILE *
127 my_fopen(char *file, char *mode)
128 {
129 	FILE *fp;
130 
131 	if ((fp = fopen(file, mode)) == (FILE *)NULL) {
132 		fplist = NOFP;
133 		return(fp);
134 	} else {
135 		curptr = append(fp);
136 	}
137 	return(fp);
138 }
139 
140 int
141 my_fclose(register FILE *iop)
142 {
143 	register NODE *cur;
144 
145 	int ret = fclose(iop);
146 	if (fplist != NOFP) {
147 		cur = search(iop);
148 		cur = del1(cur);
149 	}
150 	return ret;
151 }
152