www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2012/03/08/17:10:05

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
X-Recipient: djgpp-workers AT delorie DOT com
X-Authenticated: #27081556
X-Provags-ID: V01U2FsdGVkX192OiRx+xx2ytSZffTrx8lH7+cMMFJYKQY5bRUxVy
gFdOy/qkChhh8b
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
To: djgpp-workers AT delorie DOT com
Subject: Re: errno returned by open()
Date: Thu, 8 Mar 2012 23:06:14 +0100
User-Agent: KMail/1.9.10
References: <201203071845 DOT 09240 DOT juan DOT guerrero AT gmx DOT de> <83obs81df3 DOT fsf AT gnu DOT org>
In-Reply-To: <83obs81df3.fsf@gnu.org>
MIME-Version: 1.0
Message-Id: <201203082306.15018.juan.guerrero@gmx.de>
X-Y-GMX-Trusted: 0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id q28M9rpD006253
Reply-To: djgpp-workers AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019