summaryrefslogtreecommitdiffstats
path: root/gl/fopen.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/fopen.c')
-rw-r--r--gl/fopen.c47
1 files changed, 15 insertions, 32 deletions
diff --git a/gl/fopen.c b/gl/fopen.c
index 41587d2c..93710ad0 100644
--- a/gl/fopen.c
+++ b/gl/fopen.c
@@ -1,5 +1,5 @@
1/* Open a stream to a file. 1/* Open a stream to a file.
2 Copyright (C) 2007-2025 Free Software Foundation, Inc. 2 Copyright (C) 2007-2026 Free Software Foundation, Inc.
3 3
4 This file is free software: you can redistribute it and/or modify 4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as 5 it under the terms of the GNU Lesser General Public License as
@@ -33,13 +33,7 @@ orig_fopen (const char *filename, const char *mode)
33} 33}
34 34
35/* Specification. */ 35/* Specification. */
36#ifdef __osf__ 36#include <stdio.h>
37/* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates
38 this include because of the preliminary #include <stdio.h> above. */
39# include "stdio.h"
40#else
41# include <stdio.h>
42#endif
43 37
44#include <errno.h> 38#include <errno.h>
45#include <fcntl.h> 39#include <fcntl.h>
@@ -51,24 +45,18 @@ orig_fopen (const char *filename, const char *mode)
51FILE * 45FILE *
52rpl_fopen (const char *filename, const char *mode) 46rpl_fopen (const char *filename, const char *mode)
53{ 47{
54 int open_direction;
55 int open_flags;
56#if GNULIB_FOPEN_GNU
57 bool open_flags_gnu;
58# define BUF_SIZE 80
59 char fdopen_mode_buf[BUF_SIZE + 1];
60#endif
61
62#if defined _WIN32 && ! defined __CYGWIN__ 48#if defined _WIN32 && ! defined __CYGWIN__
63 if (strcmp (filename, "/dev/null") == 0) 49 if (streq (filename, "/dev/null"))
64 filename = "NUL"; 50 filename = "NUL";
65#endif 51#endif
66 52
67 /* Parse the mode. */ 53 /* Parse the mode. */
68 open_direction = 0; 54 int open_direction = 0;
69 open_flags = 0; 55 int open_flags = 0;
70#if GNULIB_FOPEN_GNU 56#if GNULIB_FOPEN_GNU
71 open_flags_gnu = false; 57 bool open_flags_gnu = false;
58# define BUF_SIZE 80
59 char fdopen_mode_buf[BUF_SIZE + 1];
72#endif 60#endif
73 { 61 {
74 const char *p = mode; 62 const char *p = mode;
@@ -169,21 +157,18 @@ rpl_fopen (const char *filename, const char *mode)
169 size_t len = strlen (filename); 157 size_t len = strlen (filename);
170 if (len > 0 && filename[len - 1] == '/') 158 if (len > 0 && filename[len - 1] == '/')
171 { 159 {
172 int fd;
173 struct stat statbuf;
174 FILE *fp;
175
176 if (open_direction != O_RDONLY) 160 if (open_direction != O_RDONLY)
177 { 161 {
178 errno = EISDIR; 162 errno = EISDIR;
179 return NULL; 163 return NULL;
180 } 164 }
181 165
182 fd = open (filename, open_direction | open_flags, 166 int fd = open (filename, open_direction | open_flags,
183 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); 167 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
184 if (fd < 0) 168 if (fd < 0)
185 return NULL; 169 return NULL;
186 170
171 struct stat statbuf;
187 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode)) 172 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
188 { 173 {
189 close (fd); 174 close (fd);
@@ -191,6 +176,7 @@ rpl_fopen (const char *filename, const char *mode)
191 return NULL; 176 return NULL;
192 } 177 }
193 178
179 FILE *fp;
194# if GNULIB_FOPEN_GNU 180# if GNULIB_FOPEN_GNU
195 fp = fdopen (fd, fdopen_mode_buf); 181 fp = fdopen (fd, fdopen_mode_buf);
196# else 182# else
@@ -210,15 +196,12 @@ rpl_fopen (const char *filename, const char *mode)
210#if GNULIB_FOPEN_GNU 196#if GNULIB_FOPEN_GNU
211 if (open_flags_gnu) 197 if (open_flags_gnu)
212 { 198 {
213 int fd; 199 int fd = open (filename, open_direction | open_flags,
214 FILE *fp; 200 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
215
216 fd = open (filename, open_direction | open_flags,
217 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
218 if (fd < 0) 201 if (fd < 0)
219 return NULL; 202 return NULL;
220 203
221 fp = fdopen (fd, fdopen_mode_buf); 204 FILE *fp = fdopen (fd, fdopen_mode_buf);
222 if (fp == NULL) 205 if (fp == NULL)
223 { 206 {
224 int saved_errno = errno; 207 int saved_errno = errno;