This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

fdopen and errno



Ben mentioned a problem with fdopen on bug-glibc.  I'm redirecting
this to libc-alpha and ask for your opinions.  Shouldn't fdopen set 
errno if the mode to fdopen is incorrect?  I'm appending a simple
program which open a file write only and the fdopen is called with
read, write and append.  The behaviour is correct but shouldn't the
call with read set errno?

The output is:
$ ./fdopen-test 
Open for writing:
fdopen was - as expected - not successful
  errno is: 0 which means: `Success' which looks buggy.
fdopen was successful
  errno is: 0 which means: `Success' this is ok.
fdopen was successful
  errno is: 0 which means: `Success' this is ok.

Andreas

P.S. Here's the test program which can easily get extended for all
cases (I'll do so if you think this would be usefull to add to glibc's 
testsuite):

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

static void
check_errno (int errno_save, int should_be_set)
{
  printf ("  errno is: %d which means: `%m' ", errno_save, errno_save);
  if ((errno_save && should_be_set) || (!errno_save && !should_be_set))
    printf ("this is ok.\n");
  else
    printf ("which looks buggy.\n");
}


static void
check_result (FILE *f, int ok)
{
  int save = errno;
  
  if (f && ok)
    printf ("fdopen was successful\n");
  else if (f && !ok)
    printf ("fdopen was successful but shouldn't\n");
  else if (f == NULL && !ok)
    printf ("fdopen was - as expected - not successful\n");
  else /* f == NULL && ok */
    printf ("fdopen was not successful\n");
  check_errno (save, !ok);
}

static void
test_fdopen (int fd, int read_ok, int write_ok)
{
  FILE *f;

  errno = 0;
  f = fdopen (fd, "r");
  check_result (f, read_ok);

  errno = 0;
  f = fdopen (fd, "w");
  check_result (f, write_ok);

  errno = 0;
  f = fdopen (fd, "a");
  check_result (f, write_ok);

}

int
main (void)
{
  int fd;

  printf ("Open for writing:\n");
  fd = open ("/tmp/testfd", O_WRONLY|O_CREAT);
  test_fdopen (fd, 0, 1);
  close (fd);
  return 0;
}

-- 
 Andreas Jaeger   aj@arthur.rhein-neckar.de    jaeger@informatik.uni-kl.de
  for pgp-key finger ajaeger@aixd1.rhrk.uni-kl.de

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]