delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/02/11/15:01:04

From: Martin Str|mberg <ams AT ludd DOT luth DOT se>
Message-Id: <200102112000.VAA19068@father.ludd.luth.se>
Subject: Re: preserve errno in open patch
In-Reply-To: <3A869A94.27606.A30709@localhost> from "Mark E." at "Feb 11, 2001 01:58:44 pm"
To: djgpp-workers AT delorie DOT com
Date: Sun, 11 Feb 2001 21:00:54 +0100 (MET)
X-Mailer: ELM [version 2.4ME+ PL54 (25)]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

According to Mark E.:
> 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;
> }

In general, it's wrong to look at errno unless you've got error
indication from the system call. Hence the program works as expected.



Right,

						MartinS

- Raw text -


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