delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2016/04/07/17:12:26

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Recipient: djgpp AT delorie DOT com
Message-ID: <5706CAFA.1060201@gmx.de>
Date: Thu, 07 Apr 2016 23:02:50 +0200
From: "Juan Manuel Guerrero (juan DOT guerrero AT gmx DOT de) [via djgpp AT delorie DOT com]" <djgpp AT delorie DOT com>
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.13) Gecko/20101206 SUSE/3.1.7 Thunderbird/3.1.7
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Different dup2 behaviour between DOS/DJGPP and posix.
X-Provags-ID: V03:K0:DwZvrD7zrp/Qeoxc4joqabLoll51V+vhSdnYNr0rsg4a0/NzX6g
fscUa8AgjkgEiaLyZ/RoadVcxE375qrdduZhDM1q9yejUFI9vADj4Ypdc20zKh0XWQxt/Zw
//j9BfLkgE16eKdbJoz8vxxChfzZRTpvWNG5JejoIFgUGzfXxt1SykXDf5nJEsGF2VxzR5f
0OLR48HAsNA6KSFDpDKjQ==
X-UI-Out-Filterresults: notjunk:1;V01:K0:jUVIXbJRSjU=:vo/ZwV4czENzuRGjerZEpq
KOgBjPwxdoGip2IqGQ5KzwWA6FzUtuGlD++r/BXTuq6D3N6PCPrHN9YSLVrSmrvvRATmu1uxX
SecMdaGE2Iqb8fYJBcWsUrq+pTmvB1SWBvaT9/oQxoH6ZIN9KVI7YOBNIxg6dHr4pbSRAa2jw
91+XVZE8jg96q+ANW2Z/LDweSjZIcvEi7yb9UlHTjvz6Q5xfmhVYobxkPl8KyvBskfioBTsUd
U1WR4QjSsnDfLCMA+SPy92ZaYQPLvmrjAV/xRSbKv4Xfo1qD0Z0IynRhOd9oEs2k4kBwaZFPC
MuArlRGYnph+2l6D/DRqv4T7hxnvW4AGCXuLwTuJtDHjuutWbgdv+HLvdDI0f9r/BRusbugzE
2e4yGvWFsHWGYne7zAG+mNosr4IRzLYXSZrygPxaXdXIr54+3WMXEDmjTsSKW8NuAiUtF3kN6
NgWHnEpwsGKbFVEOTqZMyfxd+c4Y4ZP73El7aP2k8Z05aUdvt+9R72P6r3XBvzOkrEzjj+UVt
E0tchOdUTavTTBBhWIcr9j1zXoBAumO+MzZ5wcnSzjUlQo+a385K6mJ9y3+b3KoxACRIuvTSF
1bcMXFOZUd2TQ5/rZDkBaFqYGEMtOByGNoWHhODMm83MCGKGKEzxEVuGysWlsWBcrzugM8qWF
XqT9l8wWsUvUoMllaM2yBj0j2WOcE/8CIXk0Lsbj1rsApLOena0hB4lshVhGoe7gxOe1LlBA5
vYVVkSP+XiO+dfz7cpKR7XwjEc6TwcmNgdr7woir+UbTFF3mcGKmxE8D7Z/O+AECqb0DPdoY6
8nvDqvW
Reply-To: djgpp AT delorie DOT com

Please note the following code snippet:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int
main (void)
{
int result = 0;
int fd;
FILE *f = fopen("./dup2.txt", "w");
errno = 0;
              if ((fd = dup2 (1, 1)) != 1)
                result |= 2;
fprintf(f, "%d:  result = %d fd = %d  errno = %s\n", __LINE__, result, fd, sys_errlist[errno]);


              close (0);
errno = 0;
              if ((fd = dup2 (0, 0)) != -1)
                result |= 8;
fprintf(f, "%d:  result = %d fd = %d  errno = %s\n", __LINE__, result, fd, sys_errlist[errno]);

              return result;
}

It depicts an autoconf check for dup2.  If I compile this with DJGPP I get
the following output:
14:  result = 0 fd = 1  errno = No Error
19:  result = 8 fd = 0  errno = No Error

If I do the same on linux I get the following output:
19:  result = 0 fd = 1  errno = Success
24:  result = 0 fd = -1  errno = Bad file descriptor

The question is why DOS/DJGPP allows to close STDIN and reopen it with FD 0
meanwhile posix seems not to allow this.  Is this behaviour expected?
I have inspected the code of dup2 and the implementation seems OK.

The consequence of this check is that Autoconf assumes that DJGPP's dup2
implementaion does not work.


Regards,
Juan M. Guerrero

- Raw text -


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