1 /* @(#)fileio.c	1.14 18/08/26 Copyright 1984-2018 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)fileio.c	1.14 18/08/26 Copyright 1984-2018 J. Schilling";
6 #endif
7 /*
8  *	Low level routines for Input/Output from/to files.
9  *
10  *	Copyright (c) 1984-2018 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include "ved.h"
27 #include <schily/fcntl.h>
28 #include <schily/varargs.h>
29 
30 EXPORT	int	stmpfmodes	__PR((Uchar *name));
31 EXPORT	FILE	*openerrmsg	__PR((Uchar *name, char *mode));
32 EXPORT	FILE	*opencomerr	__PR((ewin_t *wp, Uchar *name, char *mode));
33 EXPORT	FILE	*opensyserr	__PR((ewin_t *wp, Uchar *name, char *mode));
34 EXPORT	int	readsyserr	__PR((ewin_t *wp, FILE * f, void *buf, int len,
35 								Uchar *name));
36 EXPORT	int	writesyserr	__PR((ewin_t *wp, FILE * f, void *buf, int len,
37 								Uchar *name));
38 EXPORT	int	writebsyserr	__PR((ewin_t *wp, FILE * f, void *buf, int len,
39 								Uchar *name));
40 EXPORT	int	writeerrmsg	__PR((ewin_t *wp, FILE * f, void *buf, int len,
41 								Uchar *name));
42 EXPORT	void	exitcomerr	__PR((ewin_t *wp, char *fmt, ...));
43 EXPORT	void	excomerrno	__PR((ewin_t *wp, int err, char *fmt, ...));
44 
45 /*
46  * Make temporary files readable only by the user.
47  */
48 EXPORT int
stmpfmodes(name)49 stmpfmodes(name)
50 	Uchar	*name;
51 {
52 	if (chmod(C name, 0600) < 0)
53 		return (-1);
54 	return (0);
55 }
56 
57 /*
58  * Open a file, print error message on failure.
59  * Used only during startup of ved.
60  */
61 EXPORT FILE *
openerrmsg(name,mode)62 openerrmsg(name, mode)
63 	Uchar	*name;
64 	char	*mode;
65 {
66 	FILE	*f;
67 
68 	if ((f = fileopen(C name, mode)) == (FILE *) NULL)
69 		errmsg("Can not open '%s'.\r\n", name);
70 	return (f);
71 }
72 
73 /*
74  * Open a file, print error message and exit on failure.
75  * Used only during startup and exit of ved.
76  */
77 EXPORT FILE *
opencomerr(wp,name,mode)78 opencomerr(wp, name, mode)
79 	ewin_t	*wp;
80 	Uchar	*name;
81 	char	*mode;
82 {
83 	FILE	*f;
84 
85 	if ((f = fileopen(C name, mode)) == (FILE *) NULL)
86 		exitcomerr(wp, "Can not open '%s'.\r\n", name);
87 	return (f);
88 }
89 
90 /*
91  * Open a file, print error message on the system line on failure.
92  * Used only when the terminal is in edit mode.
93  */
94 EXPORT FILE *
opensyserr(wp,name,mode)95 opensyserr(wp, name, mode)
96 	ewin_t	*wp;
97 	Uchar	*name;
98 	char	*mode;
99 {
100 	FILE	*f;
101 
102 	if ((f = fileopen(C name, mode)) == (FILE *) NULL)
103 		write_errno(wp, "CAN'T OPEN %s", name);
104 	return (f);
105 }
106 
107 /*
108  * Read from file, print error message on the system line on failure.
109  * Used only when the terminal is in edit mode.
110  */
111 EXPORT int
readsyserr(wp,f,buf,len,name)112 readsyserr(wp, f, buf, len, name)
113 	ewin_t	*wp;
114 	FILE	*f;
115 	void	*buf;
116 	int	len;
117 	Uchar	*name;
118 {
119 	int	result;
120 
121 	if ((result = ffileread(f, C buf, len)) < 0)	/* read unbuffered */
122 		write_errno(wp, "CAN'T READ %s", name);
123 	return (result);
124 }
125 
126 /*
127  * Write to file, print error message on the system line on failure.
128  * Used only when the terminal is in edit mode.
129  */
130 EXPORT int
writesyserr(wp,f,buf,len,name)131 writesyserr(wp, f, buf, len, name)
132 	ewin_t	*wp;
133 	FILE	*f;
134 	void	*buf;
135 	int	len;
136 	Uchar	*name;
137 {
138 	int	result;
139 
140 	if ((result = ffilewrite(f, C buf, len)) != len)	/* write unbuffered */
141 		write_errno(wp, "CAN'T WRITE %s", name);
142 	return (result);
143 }
144 
145 /*
146  * Write to file, print error message on the system line on failure.
147  * Used only when the terminal is in edit mode.
148  * XXX Buffered version only used in edtmops.c
149  */
150 EXPORT int
writebsyserr(wp,f,buf,len,name)151 writebsyserr(wp, f, buf, len, name)
152 	ewin_t	*wp;
153 	FILE	*f;
154 	void	*buf;
155 	int	len;
156 	Uchar	*name;
157 {
158 	int	result;
159 
160 	if ((result = filewrite(f, C buf, len)) != len)
161 		write_errno(wp, "CAN'T WRITE %s", name);
162 	return (result);
163 }
164 
165 /*
166  * Write to file, print error message on the system line on failure.
167  * Used only when the terminal is in edit mode.
168  * XXX Buffered version only used in edtmops.c
169  */
170 /* ARGSUSED */
171 EXPORT int
writeerrmsg(wp,f,buf,len,name)172 writeerrmsg(wp, f, buf, len, name)
173 	ewin_t	*wp;
174 	FILE	*f;
175 	void	*buf;
176 	int	len;
177 	Uchar	*name;
178 {
179 	int	result;
180 
181 	if ((result = filewrite(f, C buf, len)) != len)
182 		errmsg("Can't write %s", name);
183 	return (result);
184 }
185 
186 /*
187  * Reset terminal, do tmp file cleanup, print error message and exit.
188  * Use system error number.
189  */
190 /* PRINTFLIKE2 */
191 #ifdef	PROTOTYPES
192 EXPORT void
exitcomerr(ewin_t * wp,char * fmt,...)193 exitcomerr(ewin_t *wp, char *fmt, ...)
194 #else
195 EXPORT void
196 exitcomerr(wp, fmt, va_alist)
197 	ewin_t	*wp;
198 	char	*fmt;
199 	va_dcl
200 #endif
201 {
202 	va_list	args;
203 
204 	int err = geterrno();
205 	rsttmodes(wp);
206 	tmpcleanup(wp, FALSE);
207 #ifdef	PROTOTYPES
208 	va_start(args, fmt);
209 #else
210 	va_start(args);
211 #endif
212 	comerrno(err, "%r", fmt, args);
213 	va_end(args);
214 }
215 
216 /*
217  * Reset terminal, do tmp file cleanup, print error message and exit.
218  * Use user privided error number.
219  */
220 /* PRINTFLIKE3 */
221 #ifdef	PROTOTYPES
222 EXPORT void
excomerrno(ewin_t * wp,int err,char * fmt,...)223 excomerrno(ewin_t *wp, int err, char *fmt, ...)
224 #else
225 EXPORT void
226 excomerrno(wp, err, fmt, va_alist)
227 	ewin_t	*wp;
228 	int	err;
229 	char	*fmt;
230 	va_dcl
231 #endif
232 {
233 	va_list	args;
234 
235 	rsttmodes(wp);
236 	tmpcleanup(wp, FALSE);
237 #ifdef	PROTOTYPES
238 	va_start(args, fmt);
239 #else
240 	va_start(args);
241 #endif
242 	comerrno(err, "%r", fmt, args);
243 	va_end(args);
244 }
245