From 4b8a0f3f3dcf60dac2ce308ea08d413a535af29f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 21:12:14 +0200 Subject: Adding upstream version 5.4.4. Signed-off-by: Daniel Baumann --- filecntl.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 filecntl.c (limited to 'filecntl.c') diff --git a/filecntl.c b/filecntl.c new file mode 100644 index 0000000..6cb9c9e --- /dev/null +++ b/filecntl.c @@ -0,0 +1,89 @@ +/* written 2007 by Bernhard R. Link + * This file is in the public domain. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "filecntl.h" + +#ifndef HAVE_CLOSEFROM +void closefrom(int lowfd) { + long maxopen; + int fd; + +# ifdef F_CLOSEM + if (fcntl(lowfd, F_CLOSEM, NULL) == 0) + return; +# endif + maxopen = sysconf(_SC_OPEN_MAX); + if (maxopen > INT_MAX) + maxopen = INT_MAX; + if (maxopen < 0) + maxopen = 1024; + for (fd = lowfd ; fd <= maxopen ; fd++) + (void)close(fd); +} +#endif + +void markcloseonexec(int fd) { + long l; + l = fcntl(fd, F_GETFD, 0); + if (l >= 0) { + (void)fcntl(fd, F_SETFD, l|FD_CLOEXEC); + } +} + +int deletefile(const char *fullfilename) { + int ret, e; + + ret = unlink(fullfilename); + if (ret != 0) { + e = errno; + fprintf(stderr, "error %d unlinking %s: %s\n", + e, fullfilename, strerror(e)); + return (e != 0)?e:EINVAL; + } + return 0; +} + +bool isregularfile(const char *fullfilename) { + struct stat s; + int i; + + assert(fullfilename != NULL); + i = stat(fullfilename, &s); + return i == 0 && S_ISREG(s.st_mode); +} + +bool isdirectory(const char *fullfilename) { + struct stat s; + int i; + + assert(fullfilename != NULL); + i = stat(fullfilename, &s); + return i == 0 && S_ISDIR(s.st_mode); +} + +bool isanyfile(const char *fullfilename) { + struct stat s; + int i; + + assert(fullfilename != NULL); + i = lstat(fullfilename, &s); + return i == 0; +} -- cgit v1.2.3