2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

relax safe_create().

This commit is contained in:
Mark Andrews
2001-08-04 08:15:02 +00:00
parent ee8a5c01e2
commit d8ec3078d4

View File

@@ -15,7 +15,7 @@
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
/* $Id: os.c,v 1.1 2001/08/03 05:56:22 marka Exp $ */ /* $Id: os.c,v 1.2 2001/08/04 08:15:02 marka Exp $ */
#include <config.h> #include <config.h>
@@ -46,18 +46,22 @@ safe_create(const char *filename) {
int fd; int fd;
FILE *f; FILE *f;
struct stat sb; struct stat sb;
int flags = O_WRONLY;
if (stat(filename, &sb) == -1) { if (stat(filename, &sb) == -1) {
if (errno != ENOENT) if (errno != ENOENT)
return (NULL); return (NULL);
flags = O_WRONLY|O_CREAT|O_EXCL;
} else if ((sb.st_mode & S_IFREG) == 0) { } else if ((sb.st_mode & S_IFREG) == 0) {
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
return (NULL); return (NULL);
} } else
flags = O_WRONLY|O_TRUNC;
fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); fd = open(filename, flags, S_IRUSR|S_IWUSR);
if (fd == -1) if (fd == -1)
return (NULL); return (NULL);
(void)fchmod(fd, S_IRUSR|S_IWUSR);
f = fdopen(fd, "w"); f = fdopen(fd, "w");
if (f == NULL) if (f == NULL)
close(fd); close(fd);