delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/02/11/13:58:51

From: "Mark E." <snowball3 AT bigfoot DOT com>
To: djgpp-workers AT delorie DOT com
Date: Sun, 11 Feb 2001 13:58:44 -0500
MIME-Version: 1.0
Subject: preserve errno in open patch
Message-ID: <3A869A94.27606.A30709@localhost>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp-workers AT delorie DOT com

Hi,

This program prints out a "file not found" message even when fd != -1.

#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>

int main()
{
  int fd;

  fd = open("file", O_RDWR | O_CREAT | O_TRUNC, S_IWUSR);

  if (errno)
    perror("file");

  close(fd);

  return 0;
}

I fixed this by restoring errno before calling _open and _creat in the 
!should_create block.

Index: open.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/posix/fcntl/open.c,v
retrieving revision 1.7
diff -c -p -r1.7 open.c
*** open.c	2000/08/28 14:22:33	1.7
--- open.c	2001/02/11 18:48:12
*************** open(const char* filename, int oflag, ..
*** 28,33 ****
--- 28,34 ----
    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 e = errno;
  
    /* Solve symlinks and honor O_NOLINK flag  */
    if (oflag & O_NOLINK)
*************** open(const char* filename, int oflag, ..
*** 127,138 ****
  	   DENY-NONE bit set, unless some sharing bits were already
  	   set in the initial call.  */
  	if (dont_have_share)
  	  fd = _open(real_name, oflag | SH_DENYNO);
        }
        /* Don't call _creat on existing files for which _open fails,
           since the file could be truncated as a result.  */
        else if ((oflag & O_CREAT))
! 	fd = _creat(real_name, dmode);
      }
    }
  
--- 128,145 ----
  	   DENY-NONE bit set, unless some sharing bits were already
  	   set in the initial call.  */
  	if (dont_have_share)
+ 	{
+ 	  errno = e;
  	  fd = _open(real_name, oflag | SH_DENYNO);
+ 	}
        }
        /* Don't call _creat on existing files for which _open fails,
           since the file could be truncated as a result.  */
        else if ((oflag & O_CREAT))
!       {
!         errno = e;
!         fd = _creat(real_name, dmode);
!       }
      }
    }
  

- Raw text -


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