Mail Archives: djgpp-workers/2012/03/08/17:10:05
Am Mittwoch, 7. März 2012 schrieb Eli Zaretskii:
> > From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
> > Date: Wed, 7 Mar 2012 18:45:09 +0100
> >
> > if (fd == -1)
> > return fd; /* errno already set by _open or _creat */
> > + else
> > + errno = 0; /* at this stage reset errno set by
> > + previous calls to _open or _creat */
>
> This isn't right. If a function succeeds, it should leave errno at
> the same value as it was when the function was called. Forcing it to
> zero is not TRT when some previous code set errno to something
> non-zero.
>
> What you need is to save the value of errno upon entry to `open', and
> then restore it on successful exit.
>
> Thanks.
>
I have adjusted the fix according to your suggestion. I restore the original
errno no matter if the previous call to llseek() or _write() still set it. At
this stage the file descriptor is greater than -1 and thus valid. AFAIK if fd
is valid no errno should be generated by the open() code itself and then be
passed to the calling function.
Regards,
Juan M. Guerrero
Index: djgpp/src/libc/posix/fcntl/open.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/posix/fcntl/open.c,v
retrieving revision 1.14
diff -U 5 -r1.14 open.c
--- djgpp/src/libc/posix/fcntl/open.c 7 Mar 2012 18:02:38 -0000 1.14
+++ djgpp/src/libc/posix/fcntl/open.c 8 Mar 2012 22:02:07 -0000
@@ -1,5 +1,6 @@
+/* Copyright (C) 2012 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
@@ -107,10 +108,11 @@
}
int
open(const char* filename, int oflag, ...)
{
+ const int original_errno = errno;
const int original_oflag = oflag;
int fd, dmode, bintext, dont_have_share;
char real_name[FILENAME_MAX + 1];
int should_create = (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
int dirs_solved = 0; /* Only directories resolved in real_name? */
@@ -296,8 +298,11 @@
if ( oflag & O_APPEND )
{
llseek(fd, 0, SEEK_END);
}
+
+ errno = original_errno; /* At this stage fd is valid. Restore original errno. */
+
return fd;
}
- Raw text -