X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=lib%2Fgetdtablesize.c;fp=lib%2Fgetdtablesize.c;h=d23405571b074baf5b5c69722651c5db79c48d83;hb=20fcfc81ece044b8b0a6768ec6cf47be4e22a2e6;hp=9947405af61b9773e5fc06d96d25ca66990ab76e;hpb=d57728a6ca2413a7c564d8b7bb13d9e5a5a180f3;p=debian%2Fgzip diff --git a/lib/getdtablesize.c b/lib/getdtablesize.c index 9947405..d234055 100644 --- a/lib/getdtablesize.c +++ b/lib/getdtablesize.c @@ -1,5 +1,5 @@ /* getdtablesize() function for platforms that don't have it. - Copyright (C) 2008-2013 Free Software Foundation, Inc. + Copyright (C) 2008-2016 Free Software Foundation, Inc. Written by Bruno Haible , 2008. This program is free software: you can redistribute it and/or modify @@ -22,11 +22,11 @@ #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ -#include +# include -#include "msvc-inval.h" +# include "msvc-inval.h" -#if HAVE_MSVC_INVALID_PARAMETER_HANDLER +# if HAVE_MSVC_INVALID_PARAMETER_HANDLER static int _setmaxstdio_nothrow (int newmax) { @@ -44,10 +44,11 @@ _setmaxstdio_nothrow (int newmax) return result; } -# define _setmaxstdio _setmaxstdio_nothrow -#endif +# define _setmaxstdio _setmaxstdio_nothrow +# endif -/* Cache for the previous getdtablesize () result. */ +/* Cache for the previous getdtablesize () result. Safe to cache because + Windows also lacks setrlimit. */ static int dtablesize; int @@ -83,4 +84,38 @@ getdtablesize (void) return dtablesize; } +#else + +# include +# include + +# 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