re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / gnu / getdtablesize.c
index b0af77b1c4f8aad7938330556eae0f94afadac47..03eb7ef1bffd1e793a5b802eb6f2ab4d6dc4b9cf 100644 (file)
@@ -1,7 +1,5 @@
-/* -*- buffer-read-only: t -*- vi: set ro: */
-/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
 /* getdtablesize() function for platforms that don't have it.
-   Copyright (C) 2008-2010 Free Software Foundation, Inc.
+   Copyright (C) 2008-2015 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2008.
 
    This program is free software: you can redistribute it and/or modify
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 
-#include <stdio.h>
+# include <stdio.h>
 
-/* Cache for the previous getdtablesize () result.  */
+# include "msvc-inval.h"
+
+# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+static int
+_setmaxstdio_nothrow (int newmax)
+{
+  int result;
+
+  TRY_MSVC_INVAL
+    {
+      result = _setmaxstdio (newmax);
+    }
+  CATCH_MSVC_INVAL
+    {
+      result = -1;
+    }
+  DONE_MSVC_INVAL;
+
+  return result;
+}
+#  define _setmaxstdio _setmaxstdio_nothrow
+# endif
+
+/* Cache for the previous getdtablesize () result.  Safe to cache because
+   Windows also lacks setrlimit.  */
 static int dtablesize;
 
 int
@@ -62,4 +84,38 @@ getdtablesize (void)
   return dtablesize;
 }
 
+#else
+
+# include <limits.h>
+# include <sys/resource.h>
+
+# ifndef RLIM_SAVED_CUR
+#  define RLIM_SAVED_CUR RLIM_INFINITY
+# endif
+# ifndef RLIM_SAVED_MAX
+#  define RLIM_SAVED_MAX RLIM_INFINITY
+# endif
+
+# ifdef __CYGWIN__
+  /* Cygwin 1.7.25 auto-increases the RLIMIT_NOFILE soft limit until it
+     hits the compile-time constant hard limit of 3200.  We might as
+     well just report the hard limit.  */
+#  define rlim_cur rlim_max
+# endif
+
+int
+getdtablesize (void)
+{
+  struct rlimit lim;
+
+  if (getrlimit (RLIMIT_NOFILE, &lim) == 0
+      && 0 <= lim.rlim_cur && lim.rlim_cur <= INT_MAX
+      && lim.rlim_cur != RLIM_INFINITY
+      && lim.rlim_cur != RLIM_SAVED_CUR
+      && lim.rlim_cur != RLIM_SAVED_MAX)
+    return lim.rlim_cur;
+
+  return INT_MAX;
+}
+
 #endif