Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdio add and "f" flag to fopen that makes sure we ...



details:   https://anonhg.NetBSD.org/src/rev/389a517c9db9
branches:  trunk
changeset: 480538:389a517c9db9
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jan 15 01:11:45 2000 +0000

description:
add and "f" flag to fopen that makes sure we are opening a plain file,
so that there is no chance to block.

diffstat:

 lib/libc/stdio/flags.c |  28 +++++++++++++++++++++-------
 lib/libc/stdio/fopen.3 |  13 +++++++++++--
 lib/libc/stdio/fopen.c |  24 +++++++++++++++++++-----
 3 files changed, 51 insertions(+), 14 deletions(-)

diffs (145 lines):

diff -r 8a0c398ba45b -r 389a517c9db9 lib/libc/stdio/flags.c
--- a/lib/libc/stdio/flags.c    Sat Jan 15 01:10:12 2000 +0000
+++ b/lib/libc/stdio/flags.c    Sat Jan 15 01:11:45 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: flags.c,v 1.11 1999/09/20 04:39:27 lukem Exp $ */
+/*     $NetBSD: flags.c,v 1.12 2000/01/15 01:11:45 christos Exp $      */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)flags.c    8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: flags.c,v 1.11 1999/09/20 04:39:27 lukem Exp $");
+__RCSID("$NetBSD: flags.c,v 1.12 2000/01/15 01:11:45 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -91,11 +91,25 @@
                return (0);
        }
 
-       /* [rwa]\+ or [rwa]b\+ means read and write */
-       if (*mode == '+' || (*mode == 'b' && mode[1] == '+')) {
-               ret = __SRW;
-               m = O_RDWR;
-       }
+       /*
+        * [rwa]\+ or [rwa]b\+ means read and write 
+        * f means open only plain files.
+        */
+       for (; *mode; mode++)
+               switch (*mode) {
+               case '+':
+                       ret = __SRW;
+                       m = O_RDWR;
+                       break;
+               case 'f':
+                       o |= O_NONBLOCK;
+                       break;
+               case 'b':
+                       break;
+               default:        /* We could produce a warning here */
+                       break;
+               }
+
        *optr = m | o;
        return (ret);
 }
diff -r 8a0c398ba45b -r 389a517c9db9 lib/libc/stdio/fopen.3
--- a/lib/libc/stdio/fopen.3    Sat Jan 15 01:10:12 2000 +0000
+++ b/lib/libc/stdio/fopen.3    Sat Jan 15 01:11:45 2000 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: fopen.3,v 1.9 1999/01/12 15:27:28 kleink Exp $
+.\"    $NetBSD: fopen.3,v 1.10 2000/01/15 01:11:45 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -87,12 +87,18 @@
 .Pp
 The
 .Fa mode
-string can also include the letter ``b'' either as a third character or
+string can also include the letter ``b'' either as a last character or
 as a character between the characters in any of the two-character strings
 described above.
 This is strictly for compatibility with
 .St -ansiC
 and has no effect; the ``b'' is ignored.
+In addition the letter ``f'' in the mode string restricts fopen to plain
+files; if the file opened is not a plain file,
+.Fn fopen
+will fail. This is a non
+.St -ansiC
+extension.
 .Pp
 Any created files will have mode
 .Pf \\*q Dv S_IRUSR
@@ -189,6 +195,9 @@
 or
 .Fn freopen
 was invalid.
+.It Bq Er EFTYPE
+The file is not a plain file and the character ``f'' is specified
+in the mode.
 .El
 .Pp
 The
diff -r 8a0c398ba45b -r 389a517c9db9 lib/libc/stdio/fopen.c
--- a/lib/libc/stdio/fopen.c    Sat Jan 15 01:10:12 2000 +0000
+++ b/lib/libc/stdio/fopen.c    Sat Jan 15 01:11:45 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fopen.c,v 1.8 1999/09/20 04:39:27 lukem Exp $  */
+/*     $NetBSD: fopen.c,v 1.9 2000/01/15 01:11:45 christos Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)fopen.c    8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: fopen.c,v 1.8 1999/09/20 04:39:27 lukem Exp $");
+__RCSID("$NetBSD: fopen.c,v 1.9 2000/01/15 01:11:45 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -49,6 +49,7 @@
 #include <sys/stat.h>
 #include <assert.h>
 #include <fcntl.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <errno.h>
 #include "local.h"
@@ -67,9 +68,19 @@
                return (NULL);
        if ((fp = __sfp()) == NULL)
                return (NULL);
-       if ((f = open(file, oflags, DEFFILEMODE)) < 0) {
-               fp->_flags = 0;                 /* release */
-               return (NULL);
+       if ((f = open(file, oflags, DEFFILEMODE)) < 0)
+               goto release;
+       if (oflags & O_NONBLOCK) {
+               struct stat st;
+               if (fstat(f, &st) == -1) {
+                       close(f);
+                       goto release;
+               }
+               if (!S_ISREG(st.st_mode)) {
+                       errno = EFTYPE;
+                       close(f);
+                       goto release;
+               }
        }
        fp->_file = f;
        fp->_flags = flags;
@@ -90,4 +101,7 @@
        if (oflags & O_APPEND)
                (void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
        return (fp);
+release:
+       fp->_flags = 0;                 /* release */
+       return (NULL);
 }



Home | Main Index | Thread Index | Old Index