Browse Source

improved file creation on f[re]open()

eck 34 years ago
parent
commit
35886fdf28
2 changed files with 13 additions and 5 deletions
  1. 7 3
      lang/cem/libcc.ansi/stdio/fopen.c
  2. 6 2
      lang/cem/libcc.ansi/stdio/freopen.c

+ 7 - 3
lang/cem/libcc.ansi/stdio/fopen.c

@@ -37,7 +37,6 @@
 
 int _open(const char *path, int flags);
 int _creat(const char *path, int mode);
-
 int _close(int d);
 
 FILE *
@@ -89,8 +88,13 @@ fopen(const char *name, const char *mode)
 	 */
 	if ((rwflags & O_TRUNC)
 	    || (((fd = _open(name, rwmode)) < 0)
-		    && (flags & _IOWRITE)))
-		fd = _creat(name, PMODE);
+		    && (flags & _IOWRITE))) {
+		if (((fd = _creat(name, PMODE)) > 0) && flags  | _IOREAD) {
+			(void) _close(fd);
+			fd = _open(name, rwmode);
+		}
+			
+	}
 
 	if (fd < 0) return (FILE *)NULL;
 

+ 6 - 2
lang/cem/libcc.ansi/stdio/freopen.c

@@ -68,8 +68,12 @@ freopen(const char *name, const char *mode, FILE *stream)
 
 	if ((rwflags & O_TRUNC)
 	    || (((fd = _open(name, rwmode)) < 0)
-		    && (flags & _IOWRITE)))
-		fd = _creat(name, PMODE);
+		    && (flags & _IOWRITE))) {
+		if (((fd = _creat(name, PMODE)) < 0) && flags | _IOREAD) {
+			(void) _close(fd);
+			fd = _open(name, rwmode);
+		}
+	}
 
 	if (fd < 0) {
 		for( i = 0; i < FOPEN_MAX; i++) {