DragonFly BSD
DragonFly submit List (threaded) for 2004-03
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

set errno for fvprintf, fvwrite (wbuf)


From: ibotty <bsd@xxxxxxxxxx>
Date: Wed, 03 Mar 2004 18:44:24 +0100

jordan k. hubbard pointed out, that somewhen errno was not set, when a FILE
* is not writable. [1]

attached patch fixes this for a few more more libc functions.
i added a comment, that indicates, why this errno and not a more logical one
is chosen. (according to followup of [1])

[1] http://docs.freebsd.org/cgi/getmsg.cgi?fetch=178661+0+current
freebsd-hackers

~ibotty

btw: i removed K&R style methods and register keywords, when i came across.
Index: fvwrite.c
===================================================================
RCS file: /home/src/dcvs/src/lib/libc/stdio/fvwrite.c,v
retrieving revision 1.2
diff -u -3 -p -r1.2 fvwrite.c
--- fvwrite.c	17 Jun 2003 04:26:46 -0000	1.2
+++ fvwrite.c	3 Mar 2004 16:48:46 -0000
@@ -41,6 +41,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include "local.h"
 #include "fvwrite.h"
 
@@ -51,22 +52,23 @@
  * to the three different kinds of output buffering is handled here.
  */
 int
-__sfvwrite(fp, uio)
-	register FILE *fp;
-	register struct __suio *uio;
+__sfvwrite(FILE *fp, struct __suio *uio)
 {
-	register size_t len;
-	register char *p;
-	register struct __siov *iov;
-	register int w, s;
+	size_t len;
+	char *p;
+	struct __siov *iov;
+	int w, s;
 	char *nl;
 	int nlknown, nldist;
 
 	if ((len = uio->uio_resid) == 0)
 		return (0);
 	/* make sure we can write */
-	if (cantwrite(fp))
+	if (cantwrite(fp)) {
+		/* use EBADF, not EACCES, because of POSIX */
+		errno = EBADF;
 		return (EOF);
+	}
 
 #define	MIN(a, b) ((a) < (b) ? (a) : (b))
 #define	COPY(n)	  (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
Index: vfprintf.c
===================================================================
RCS file: /home/src/dcvs/src/lib/libc/stdio/vfprintf.c,v
retrieving revision 1.3
diff -u -3 -p -r1.3 vfprintf.c
--- vfprintf.c	12 Nov 2003 20:21:25 -0000	1.3
+++ vfprintf.c	3 Mar 2004 16:48:10 -0000
@@ -50,6 +50,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #if __STDC__
 #include <stdarg.h>
@@ -408,6 +409,8 @@ vfprintf(FILE *fp, const char *fmt0, va_
 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
 	if (cantwrite(fp)) {
 		FUNLOCKFILE(fp);
+		/* EBADF instead of EACCES because of POSIX */
+		errno = EBADF;
 		return (EOF);
 	}
 
Index: wbuf.c
===================================================================
RCS file: /home/src/dcvs/src/lib/libc/stdio/wbuf.c,v
retrieving revision 1.2
diff -u -3 -p -r1.2 wbuf.c
--- wbuf.c	17 Jun 2003 04:26:46 -0000	1.2
+++ wbuf.c	3 Mar 2004 16:55:03 -0000
@@ -47,11 +47,9 @@
  * or if c=='\n' and the file is line buffered.
  */
 int
-__swbuf(c, fp)
-	register int c;
-	register FILE *fp;
+__swbuf(int c, FILE *fp)
 {
-	register int n;
+	int n;
 
 	/*
 	 * In case we cannot write, or longjmp takes us out early,
@@ -61,8 +59,11 @@ __swbuf(c, fp)
 	 * calls might wrap _w from negative to positive.
 	 */
 	fp->_w = fp->_lbfsize;
-	if (cantwrite(fp))
+	if (cantwrite(fp)) {
+		/* EBADF instead of EACCES because of POSIX */
+		errno = EBADF;
 		return (EOF);
+	}
 	c = (unsigned char)c;
 
 	/*


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