1 /* b-fb.c --- basic file operations
2 
3    Copyright (C) 2010-2020 Thien-Thi Nguyen
4    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Paul Eggert
5    Copyright (C) 1982, 1988, 1989 Walter Tichy
6 
7    This file is part of GNU RCS.
8 
9    GNU RCS is free software: you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by
11    the Free Software Foundation, either version 3 of the License, or
12    (at your option) any later version.
13 
14    GNU RCS is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty
16    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17    See the GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "base.h"
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include "unistd-safer.h"
28 #include "b-complain.h"
29 
30 int
change_mode(int fd,mode_t mode)31 change_mode (int fd, mode_t mode)
32 {
33 #ifndef HAVE_FCHMOD
34   return -1;
35 #else
36   return fchmod (fd, mode);
37 #endif
38 }
39 
40 void
Ierror(void)41 Ierror (void)
42 {
43   fatal_sys ("input error");
44 }
45 
46 void
testIerror(FILE * f)47 testIerror (FILE *f)
48 {
49   if (ferror (f))
50     Ierror ();
51 }
52 
53 void
Oerror(void)54 Oerror (void)
55 {
56   if (BE (Oerrloop))
57     BOW_OUT ();
58   BE (Oerrloop) = true;
59   fatal_sys ("output error");
60 }
61 
62 void
testOerror(FILE * o)63 testOerror (FILE *o)
64 {
65   if (ferror (o))
66     Oerror ();
67 }
68 
69 FILE *
fopen_safer(char const * filename,char const * type)70 fopen_safer (char const *filename, char const *type)
71 /* Like ‘fopen’, except the result is never stdin, stdout, or stderr.  */
72 {
73   FILE *stream = fopen (filename, type);
74 
75   if (stream)
76     {
77       int fd = fileno (stream);
78 
79       if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
80         {
81           int f = dup_safer (fd);
82 
83           if (PROB (f))
84             {
85               int e = errno;
86 
87               fclose (stream);
88               errno = e;
89               return NULL;
90             }
91           if (PROB (fclose (stream)))
92             {
93               int e = errno;
94 
95               close (f);
96               errno = e;
97               return NULL;
98             }
99           stream = fdopen (f, type);
100         }
101     }
102   return stream;
103 }
104 
105 void
Ozclose(FILE ** p)106 Ozclose (FILE **p)
107 {
108   if (*p && EOF == fclose (*p))
109     Oerror ();
110   *p = NULL;
111 }
112 
113 void
aflush(FILE * f)114 aflush (FILE *f)
115 {
116   if (PROB (fflush (f)))
117     Oerror ();
118 }
119 
120 void
oflush(void)121 oflush (void)
122 {
123   FILE *mstdout = MANI (standard_output);
124 
125   if (PROB (fflush (mstdout
126                     ? mstdout
127                     : stdout))
128       && !BE (Oerrloop))
129     Oerror ();
130 }
131 
132 void
afputc(int c,register FILE * f)133 afputc (int c, register FILE *f)
134 /* ‘afputc (c, f)’ acts like ‘aputc (c, f)’ but is smaller and slower.  */
135 {
136   aputc (c, f);
137 }
138 
139 void
newline(FILE * f)140 newline (FILE *f)
141 /* Write a newline character (U+0A) to ‘f’; abort on error.  */
142 {
143   aputc ('\n', f);
144 }
145 
146 void
aputs(char const * s,FILE * iop)147 aputs (char const *s, FILE *iop)
148 /* Put string ‘s’ on file ‘iop’, abort on error.  */
149 {
150   if (PROB (fputs (s, iop)))
151     Oerror ();
152 }
153 
154 void
aprintf(FILE * iop,char const * fmt,...)155 aprintf (FILE * iop, char const *fmt, ...)
156 /* Formatted output.  Same as ‘fprintf’ in <stdio.h>,
157    but abort program on error.  */
158 {
159   va_list ap;
160 
161   va_start (ap, fmt);
162   if (PROB (vfprintf (iop, fmt, ap)))
163     Oerror ();
164   va_end (ap);
165 }
166 
167 /* b-fb.c ends here */
168