aboutsummaryrefslogtreecommitdiffstats
path: root/close_range.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-02-09 10:16:27 +0100
committerMattias Andrée <m@maandree.se>2025-02-09 10:16:27 +0100
commitb88b278394d0d3ac47d04a40876bc322491a4b83 (patch)
tree35d6e415181f7d415c5480579becae70632e1199 /close_range.c
parentUpdate e-mail (diff)
downloadlibsimple-b88b278394d0d3ac47d04a40876bc322491a4b83.tar.gz
libsimple-b88b278394d0d3ac47d04a40876bc322491a4b83.tar.bz2
libsimple-b88b278394d0d3ac47d04a40876bc322491a4b83.tar.xz
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'close_range.c')
-rw-r--r--close_range.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/close_range.c b/close_range.c
index 540eba0..29602c9 100644
--- a/close_range.c
+++ b/close_range.c
@@ -73,7 +73,8 @@ libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next)
{
int saved_errno;
- *next = first;
+ if (next)
+ *next = first;
if (first > last) {
errno = EINVAL;
@@ -103,10 +104,12 @@ libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next)
qsort(fds, n, sizeof(*fds), uintpcmp);
for (i = 0; i < n; i++) {
if (close((int)fds[i]) && errno != EBADF) {
- if (i + 1 < n)
- *next = fds[i + 1];
- else
- *next = fds[i] + (fds[i] < LIBSIMPLE_CLOSE_RANGE_MAX);
+ if (next) {
+ if (i + 1 < n)
+ *next = fds[i + 1];
+ else
+ *next = fds[i] + (fds[i] < LIBSIMPLE_CLOSE_RANGE_MAX);
+ }
free(fds);
return -1;
}
@@ -118,13 +121,15 @@ libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next)
fallback:
do {
if (close((int)first) && errno != EBADF) {
- *next = first + (first < LIBSIMPLE_CLOSE_RANGE_MAX);
+ if (next)
+ *next = first + (first < LIBSIMPLE_CLOSE_RANGE_MAX);
return -1;
}
} while (first++ < last);
out:
- *next = last + (last < LIBSIMPLE_CLOSE_RANGE_MAX);
+ (next)
+ *next = last + (last < LIBSIMPLE_CLOSE_RANGE_MAX);
errno = saved_errno;
return 0;
}