Merge branch 'stable'
[gnulib.git] / tests / test-fcntl.c
index c5a931c..23a0dba 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of fcntl(2).
-   Copyright (C) 2009 Free Software Foundation, Inc.
+   Copyright (C) 2009-2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 /* Specification.  */
 #include <fcntl.h>
 
+#include "signature.h"
+SIGNATURE_CHECK (fcntl, int, (int, int, ...));
+
 /* Helpers.  */
 #include <errno.h>
 #include <stdarg.h>
 #include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <unistd.h>
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 #endif
 
 #include "binary-io.h"
-
-/* Use O_CLOEXEC if available, but test works without it.  */
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
+#include "macros.h"
 
 #if !O_BINARY
 # define setmode(f,m) zero ()
 static int zero (void) { return 0; }
 #endif
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-        {                                                                    \
-          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
-          fflush (stderr);                                                   \
-          abort ();                                                          \
-        }                                                                    \
-    }                                                                        \
-  while (0)
-
 /* Return true if FD is open.  */
 static bool
 is_open (int fd)
@@ -155,22 +140,16 @@ check_flags (void)
 {
   switch (0)
     {
-#ifdef F_DUPFD
     case F_DUPFD:
-# if F_DUPFD
-# endif
+#if F_DUPFD
 #endif
 
-#ifdef F_DUPFD_CLOEXEC
     case F_DUPFD_CLOEXEC:
-# if F_DUPFD_CLOEXEC
-# endif
+#if F_DUPFD_CLOEXEC
 #endif
 
-#ifdef F_GETFD
     case F_GETFD:
-# if F_GETFD
-# endif
+#if F_GETFD
 #endif
 
 #ifdef F_SETFD
@@ -226,7 +205,7 @@ check_flags (void)
 }
 
 int
-main (int argc, char **argv)
+main (void)
 {
   const char *file = "test-fcntl.tmp";
   int fd;
@@ -241,8 +220,6 @@ main (int argc, char **argv)
   }
   check_flags ();
 
-#if HAVE_FCNTL
-
   /* Assume std descriptors were provided by invoker, and ignore fds
      that might have been inherited.  */
   fd = creat (file, 0600);
@@ -335,11 +312,30 @@ main (int argc, char **argv)
   ASSERT (is_mode (fd + 2, O_TEXT));
   ASSERT (close (fd + 2) == 0);
 
+  /* Test F_GETFD.  */
+  errno = 0;
+  ASSERT (fcntl (-1, F_GETFD) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (fd + 1, F_GETFD) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (10000000, F_GETFD) == -1);
+  ASSERT (errno == EBADF);
+  {
+    int result = fcntl (fd, F_GETFD);
+    ASSERT (0 <= result);
+    ASSERT ((result & FD_CLOEXEC) == FD_CLOEXEC);
+    ASSERT (dup (fd) == fd + 1);
+    result = fcntl (fd + 1, F_GETFD);
+    ASSERT (0 <= result);
+    ASSERT ((result & FD_CLOEXEC) == 0);
+    ASSERT (close (fd + 1) == 0);
+  }
+
   /* Cleanup.  */
   ASSERT (close (fd) == 0);
   ASSERT (unlink (file) == 0);
 
-#endif /* HAVE_FCNTL */
-
   return 0;
 }