NEWS.stable: log cherry-pick [e446f25]->[c092018] relocatable-shell: Update suggested...
[gnulib.git] / tests / test-posix_spawn_file_actions_adddup2.c
index 18b7ca5..347e783 100644 (file)
@@ -1,5 +1,5 @@
 /* Test posix_spawn_file_actions_adddup2() function.
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011-2014 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
@@ -23,12 +23,29 @@ SIGNATURE_CHECK (posix_spawn_file_actions_adddup2, int,
                  (posix_spawn_file_actions_t *, int, int));
 
 #include <errno.h>
+#include <limits.h>
+#include <unistd.h>
 
 #include "macros.h"
 
+/* Return a file descriptor that is too big to use.
+   Prefer the smallest such fd, except use OPEN_MAX if it is defined
+   and is greater than getdtablesize (), as that's how OS X works.  */
+static int
+big_fd (void)
+{
+  int fd = getdtablesize ();
+#ifdef OPEN_MAX
+  if (fd < OPEN_MAX)
+    fd = OPEN_MAX;
+#endif
+  return fd;
+}
+
 int
 main (void)
 {
+  int bad_fd = big_fd ();
   posix_spawn_file_actions_t actions;
 
   ASSERT (posix_spawn_file_actions_init (&actions) == 0);
@@ -40,7 +57,7 @@ main (void)
   }
   {
     errno = 0;
-    ASSERT (posix_spawn_file_actions_adddup2 (&actions, 10000000, 2) == EBADF);
+    ASSERT (posix_spawn_file_actions_adddup2 (&actions, bad_fd, 2) == EBADF);
   }
   {
     errno = 0;
@@ -48,7 +65,7 @@ main (void)
   }
   {
     errno = 0;
-    ASSERT (posix_spawn_file_actions_adddup2 (&actions, 2, 10000000) == EBADF);
+    ASSERT (posix_spawn_file_actions_adddup2 (&actions, 2, bad_fd) == EBADF);
   }
 
   return 0;