diff options
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | close_range.c | 19 | ||||
-rw-r--r-- | libsimple.h | 1 | ||||
-rw-r--r-- | libsimple/definitions.h | 104 | ||||
-rw-r--r-- | libsimple/time.h | 39 | ||||
-rw-r--r-- | man3/libsimple_close_range.3 | 5 |
6 files changed, 161 insertions, 9 deletions
@@ -1,6 +1,6 @@ ISC License -© 2017, 2018, 2021, 2022, 2023, 2024 Mattias Andrée <m@maandree.se> +© 2017, 2018, 2021, 2022, 2023, 2024, 2025 Mattias Andrée <m@maandree.se> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above 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; } diff --git a/libsimple.h b/libsimple.h index ba01ab5..6d1867e 100644 --- a/libsimple.h +++ b/libsimple.h @@ -211,6 +211,7 @@ libsimple_close(int *fdp__) * @throws Any error for close(3) except EBADF * * @since 1.6 + * @since 1.8 `next` can be `NULL` */ int libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next); #define LIBSIMPLE_CLOSE_RANGE_MAX (~0U) diff --git a/libsimple/definitions.h b/libsimple/definitions.h index 6fbc17f..98c3a15 100644 --- a/libsimple/definitions.h +++ b/libsimple/definitions.h @@ -229,6 +229,110 @@ /** + * Get the length of a string, stored in as a + * character array, that might not be NUL-termianted + * + * Note that since arrays are passed as pointers + * in function arguments, function parameters + * declared as arrays are actually pointers and + * cannot be used with this macro + * + * Using this macro when `S` is a pointer, + * will cause the returned length to be truncated + * to at most `sizeof(void *)` + * + * @param S:char[] The string to get the legnth of + * @return The length of `S` + * + * @since 1.8 + */ +#ifndef BUFSTRLEN +# define BUFSTRLEN(S) (strnlen((S), sizeof(S))) /* TODO man */ +#endif + + +/** + * This macro doesn't really do anything, but + * it can be used to mark that a poorly named + * constant is the length of a string including + * the NUL byte + * + * @param X:size_t Any value + * @return :size_t `X` as is + * + * @since 1.8 + */ +#ifndef ISSTRSIZE +# define ISSTRSIZE(X) (X) /* TODO man */ +#endif + + +/** + * Macro that can be used to mark that a value + * is the length of a string excluding the NUL + * byte, but adds one to convert it to the + * length of the string inlcuding the NUL byte + * + * @param X:size_t String length excluding NUL byte + * @return :size_t String length including NUL byte + * + * @since 1.8 + */ +#ifndef TOSTRSIZE +# define TOSTRSIZE(X) ((X) + 1U) /* TODO man */ +#endif + + +/** + * This macro doesn't really do anything, but + * it can be used to mark that a poorly named + * constant is the length of a string excluding + * the NUL byte + * + * @param X:size_t Any value + * @return :size_t `X` as is + * + * @since 1.8 + */ +#ifndef ISSTRLEN +# define ISSTRLEN(X) (X) /* TODO man */ +#endif + + +/** + * Macro that can be used to mark that a value + * is the length of a string including the NUL + * byte, but substracts one to convert it to the + * length of the string exlcuding the NUL byte + * + * @param X:size_t String length including NUL byte + * @return :size_t String length excluding NUL byte + * + * @since 1.8 + */ +#ifndef TOSTRLEN +# define TOSTRLEN(X) ((X) - 1U) /* TODO man */ +#endif + + +/** + * Limit a value to an inclusive range + * + * @param MINIMUM The minimum allowed value + * @param X The value to limit + * @param MAXIMUM The maximum allowed value + * @return `X` except no less than `MINIMUM` and no greater than `MAXIMUM` + * + * `MAXIMUM` must be at least `MINIMUM` + * + * @since 1.8 + */ +#ifndef LIMITRANGE +# define LIMITRANGE(MINIMUM, X, MAXIMUM) (MIN(MAX((MINIMUM), (X)), (MAXIMUM))) /* TODO man */ +#endif + + +/** * Get an approximation for the longest string an * integer of the specific integer type can be * converted to assuming it will not having an diff --git a/libsimple/time.h b/libsimple/time.h index b873635..5317cfc 100644 --- a/libsimple/time.h +++ b/libsimple/time.h @@ -1,6 +1,45 @@ /* See LICENSE file for copyright and license details. */ +/* TODO man, doc (since 1.8) */ +#define LIBSIMPLE_NANOSECONDS_PER_NANOSECOND UINT64_C(1) +#define LIBSIMPLE_NANOSECONDS_PER_MICROSECOND UINT64_C(1000) +#define LIBSIMPLE_NANOSECONDS_PER_MILLISECOND (LIBSIMPLE_NANOSECONDS_PER_MICROSECOND * UINT64_C(1000)) +#define LIBSIMPLE_NANOSECONDS_PER_SECOND (LIBSIMPLE_NANOSECONDS_PER_MILLISECOND * UINT64_C(1000)) +#define LIBSIMPLE_NANOSECONDS_PER_MINUTE (LIBSIMPLE_NANOSECONDS_PER_SECOND * UINT64_C(60)) +#define LIBSIMPLE_NANOSECONDS_PER_HOUR (LIBSIMPLE_NANOSECONDS_PER_MINUTE * UINT64_C(60)) +#define LIBSIMPLE_NANOSECONDS_PER_DAY (LIBSIMPLE_NANOSECONDS_PER_HOUR * UINT64_C(24)) +#define LIBSIMPLE_NANOSECONDS_PER_WEEK (LIBSIMPLE_NANOSECONDS_PER_DAY * UINT64_C(7)) +#define LIBSIMPLE_MICROSECONDS_PER_MICROSECOND UINT64_C(1) +#define LIBSIMPLE_MICROSECONDS_PER_MILLISECOND UINT64_C(1000) +#define LIBSIMPLE_MICROSECONDS_PER_SECOND (LIBSIMPLE_MICROSECONDS_PER_MICROSECOND * UINT64_C(1000)) +#define LIBSIMPLE_MICROSECONDS_PER_MINUTE (LIBSIMPLE_MICROSECONDS_PER_SECOND * UINT64_C(60)) +#define LIBSIMPLE_MICROSECONDS_PER_HOUR (LIBSIMPLE_MICROSECONDS_PER_MINUTE * UINT64_C(60)) +#define LIBSIMPLE_MICROSECONDS_PER_DAY (LIBSIMPLE_MICROSECOND_PER_HOUR * UINT64_C(24)) +#define LIBSIMPLE_MICROSECONDS_PER_WEEK (LIBSIMPLE_MICROSECOND_PER_DAY * UINT64_C(7)) +#define LIBSIMPLE_MILLISECONDS_PER_MILLISECONDS UINT64_C(1) +#define LIBSIMPLE_MILLISECONDS_PER_SECOND UINT64_C(1000) +#define LIBSIMPLE_MILLISECONDS_PER_MINUTE (LIBSIMPLE_MILLISECONDS_PER_SECOND * UINT64_C(60)) +#define LIBSIMPLE_MILLISECONDS_PER_HOUR (LIBSIMPLE_MILLISECONDS_PER_MINUTE * UINT64_C(60)) +#define LIBSIMPLE_MILLISECONDS_PER_DAY (LIBSIMPLE_MILLISECONDS_PER_HOUR * UINT64_C(24)) +#define LIBSIMPLE_MILLISECONDS_PER_WEEK (LIBSIMPLE_MILLISECONDS_PER_DAY * UINT64_C(7)) +#define LIBSIMPLE_SECONDS_PER_SECOND UINT64_C(1) +#define LIBSIMPLE_SECONDS_PER_MINUTE UINT64_C(60) +#define LIBSIMPLE_SECONDS_PER_HOUR (LIBSIMPLE_SECONDS_PER_MINUTE * UINT64_C(60)) +#define LIBSIMPLE_SECONDS_PER_DAY (LIBSIMPLE_SECONDS_PER_HOUR * UINT64_C(24)) +#define LIBSIMPLE_SECONDS_PER_WEEK (LIBSIMPLE_SECONDS_PER_DAY * UINT64_C(7)) +#define LIBSIMPLE_MINUTES_PER_MINUTE UINT64_C(1) +#define LIBSIMPLE_MINUTES_PER_HOUR UINT64_C(60) +#define LIBSIMPLE_MINUTES_PER_DAY (LIBSIMPLE_MINUTES_PER_HOUR * UINT64_C(24)) +#define LIBSIMPLE_MINUTES_PER_WEEK (LIBSIMPLE_MINUTES_PER_DAY * UINT64_C(7)) +#define LIBSIMPLE_HOURS_PER_HOUR UINT64_C(1) +#define LIBSIMPLE_HOURS_PER_DAY UINT64_C(24) +#define LIBSIMPLE_HOURS_PER_WEEK (LIBSIMPLE_HOURS_PER_DAY * UINT64_C(7)) +#define LIBSIMPLE_DAYS_PER_DAY UINT64_C(1) +#define LIBSIMPLE_DAYS_PER_WEEK UINT64_C(7) +#define LIBSIMPLE_WEEKS_PER_WEEK UINT64_C(1) + + /** * Calculates the sum of two `struct timespec`s * diff --git a/man3/libsimple_close_range.3 b/man3/libsimple_close_range.3 index 76094d0..a1b172c 100644 --- a/man3/libsimple_close_range.3 +++ b/man3/libsimple_close_range.3 @@ -69,7 +69,10 @@ None. libsimple 1.6 .SH BUGS -None. +.I next +could not be +.I NULL +before version 1.8 of libsimple. .SH SEE ALSO .BR close_range (2), |