Backed out changeset e05f5705283d (wrong name)
[unleashed.git] / usr / src / cmd / mailx / myfopen.c
blob86b25f9c0a634870e5f24eaaef58f07be010f7c4
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
34 * All Rights Reserved
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
38 * contributors.
41 #pragma ident "%Z%%M% %I% %E% SMI"
43 #include "rcv.h"
45 #undef fopen
46 #undef fclose
49 * mailx -- a modified version of a University of California at Berkeley
50 * mail program
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.
57 static NODE *append();
58 static NODE *del1();
59 static NODE *getnode();
60 static NODE *search();
62 static NODE *
63 getnode(FILE *fp)
65 NODE *newnode;
67 if ((newnode = (NODE *)malloc(sizeof(NODE))) == (NODE *)NULL) {
68 (void) fputs("Cannot allocate node space\n", stderr);
69 exit(3);
71 newnode->fp = fp;
72 newnode->next = (NODE *)NULL;
73 return(newnode);
76 static NODE *
77 search(FILE *fp)
79 register NODE *tmp;
81 for (tmp = fplist; tmp != (NODE *)NULL; tmp = tmp->next)
82 if (tmp->fp == fp)
83 break;
84 return( tmp != (NODE *)NULL ? tmp : NOFP);
87 static NODE *
88 append(FILE *fp)
90 register NODE *newnode;
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;
100 return(newnode);
103 static NODE *
104 del1(NODE *oldcur)
106 register NODE *cur, *prev;
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;
116 if (curptr == oldcur)
117 curptr = prev;
118 free(oldcur);
119 break;
121 prev = cur;
123 return(cur);
126 FILE *
127 my_fopen(char *file, char *mode)
129 FILE *fp;
131 if ((fp = fopen(file, mode)) == (FILE *)NULL) {
132 fplist = NOFP;
133 return(fp);
134 } else {
135 curptr = append(fp);
137 return(fp);
141 my_fclose(register FILE *iop)
143 register NODE *cur;
145 int ret = fclose(iop);
146 if (fplist != NOFP) {
147 cur = search(iop);
148 cur = del1(cur);
150 return ret;