xref: /netbsd-src/external/bsd/tmux/dist/compat/getdtablecount.c (revision 8f3b94834e97ac2c2a7fe1b0ae81072bf0844b41)
14e179ddaSchristos /*
24e179ddaSchristos  * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
34e179ddaSchristos  *
44e179ddaSchristos  * Permission to use, copy, modify, and distribute this software for any
54e179ddaSchristos  * purpose with or without fee is hereby granted, provided that the above
64e179ddaSchristos  * copyright notice and this permission notice appear in all copies.
74e179ddaSchristos  *
84e179ddaSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
94e179ddaSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
104e179ddaSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
114e179ddaSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
124e179ddaSchristos  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
134e179ddaSchristos  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
144e179ddaSchristos  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
154e179ddaSchristos  */
164e179ddaSchristos 
174e179ddaSchristos #include <sys/types.h>
184e179ddaSchristos 
194e179ddaSchristos #include <glob.h>
204e179ddaSchristos #include <unistd.h>
214e179ddaSchristos 
224e179ddaSchristos #include "compat.h"
234e179ddaSchristos 
244e179ddaSchristos void fatal(const char *, ...);
254e179ddaSchristos void fatalx(const char *, ...);
264e179ddaSchristos 
274e179ddaSchristos #ifdef HAVE_PROC_PID
284e179ddaSchristos int
getdtablecount(void)294e179ddaSchristos getdtablecount(void)
304e179ddaSchristos {
314e179ddaSchristos 	char	path[PATH_MAX];
324e179ddaSchristos 	glob_t	g;
33*8f3b9483Schristos 	int	n = 0;
344e179ddaSchristos 
354e179ddaSchristos 	if (snprintf(path, sizeof path, "/proc/%ld/fd/*", (long)getpid()) < 0)
364e179ddaSchristos 		fatal("snprintf overflow");
37*8f3b9483Schristos 	if (glob(path, 0, NULL, &g) == 0)
384e179ddaSchristos 		n = g.gl_pathc;
394e179ddaSchristos 	globfree(&g);
404e179ddaSchristos 	return (n);
414e179ddaSchristos }
424e179ddaSchristos #else
434e179ddaSchristos int
getdtablecount(void)444e179ddaSchristos getdtablecount(void)
454e179ddaSchristos {
464e179ddaSchristos 	return (0);
474e179ddaSchristos }
484e179ddaSchristos #endif
49