diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/.gitignore | 31 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/Makefile.in | 8 | ||||
-rw-r--r-- | src/io.c | 11 | ||||
-rw-r--r-- | src/libHX.map | 11 | ||||
-rw-r--r-- | src/misc.c | 17 | ||||
-rw-r--r-- | src/socket.c | 25 | ||||
-rw-r--r-- | src/string.c | 88 | ||||
-rw-r--r-- | src/tc-cast.c | 4 | ||||
-rw-r--r-- | src/tc-compile.c | 2 | ||||
-rw-r--r-- | src/tc-dir.c | 2 | ||||
-rw-r--r-- | src/tc-io.c | 5 | ||||
-rw-r--r-- | src/tc-list.c | 22 | ||||
-rw-r--r-- | src/tc-map.c | 29 | ||||
-rw-r--r-- | src/tc-memmem.c | 27 | ||||
-rw-r--r-- | src/tc-misc.c | 19 | ||||
-rw-r--r-- | src/tc-netio.c | 25 | ||||
-rw-r--r-- | src/tc-option.c | 2 | ||||
-rw-r--r-- | src/tc-proc.c | 2 | ||||
-rw-r--r-- | src/tc-rand.c | 2 | ||||
-rw-r--r-- | src/tc-shconfig.c | 19 | ||||
-rw-r--r-- | src/tc-string.c | 88 | ||||
-rw-r--r-- | src/tc-switchuser.c | 14 | ||||
-rw-r--r-- | src/tc-time.c | 31 | ||||
-rw-r--r-- | src/tc-xml.c | 29 | ||||
-rw-r--r-- | src/uxcompat.h | 104 |
26 files changed, 318 insertions, 303 deletions
diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index 89edb97..0000000 --- a/src/.gitignore +++ /dev/null @@ -1,31 +0,0 @@ -/hxdirstamp -/t?-cast -/t?-compile -/t?-deque -/t?-dir -/t?-format -/t?-intdiff -/t?-io -/t?-link -/t?-list -/t?-list2 -/t?-map -/t?-memmem -/t?-misc -/t?-netio -/t?-option -/t?-other -/t?-proc -/t?-rand -/t?-realpath -/t?-shconfig -/t?-socket -/t?-strchr2 -/t?-string -/t?-strquote -/t?-switchuser -/t?-time - -# automake tests -*.log -*.trs diff --git a/src/Makefile.am b/src/Makefile.am index f7ff4f6..ac68f74 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,8 +12,8 @@ endif libHX_la_SOURCES = deque.c dl.c format.c io.c map.c \ mc.c misc.c opt.c proc.c \ rand.c socket.c string.c time.c -libHX_la_LIBADD = ${libdl_LIBS} -lm ${libpthread_LIBS} ${librt_LIBS} -libHX_la_LDFLAGS = -no-undefined -version-info 37:0:5 +libHX_la_LIBADD = ${libdl_LIBS} -lm ${libpthread_LIBS} ${librt_LIBS} ${libsocket_LIBS} +libHX_la_LDFLAGS = -no-undefined -version-info 38:0:6 if WITH_GNU_LD libHX_la_LDFLAGS += -Wl,--version-script=${srcdir}/libHX.map endif diff --git a/src/Makefile.in b/src/Makefile.in index 43f7444..d349749 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -165,7 +165,8 @@ am__installdirs = "$(DESTDIR)$(libdir)" LTLIBRARIES = $(lib_LTLIBRARIES) am__DEPENDENCIES_1 = libHX_la_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ - $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) + $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ + $(am__DEPENDENCIES_1) am__libHX_la_SOURCES_DIST = deque.c dl.c format.c io.c map.c mc.c \ misc.c opt.c proc.c rand.c socket.c string.c time.c ux-file.c \ ux-mmap.c @@ -671,7 +672,6 @@ EGREP = @EGREP@ ETAGS = @ETAGS@ EXEEXT = @EXEEXT@ FGREP = @FGREP@ -FILECMD = @FILECMD@ GREP = @GREP@ INSTALL = @INSTALL@ INSTALL_DATA = @INSTALL_DATA@ @@ -779,8 +779,8 @@ lib_LTLIBRARIES = libHX.la $(am__append_1) libHX_la_SOURCES = deque.c dl.c format.c io.c map.c mc.c misc.c opt.c \ proc.c rand.c socket.c string.c time.c $(am__append_3) libHX_la_LIBADD = ${libdl_LIBS} -lm ${libpthread_LIBS} ${librt_LIBS} \ - $(am__append_4) -libHX_la_LDFLAGS = -no-undefined -version-info 37:0:5 $(am__append_2) + ${libsocket_LIBS} $(am__append_4) +libHX_la_LDFLAGS = -no-undefined -version-info 38:0:6 $(am__append_2) EXTRA_libHX_la_DEPENDENCIES = libHX.map libHX_rtcheck_la_SOURCES = rtcheck.c libHX_rtcheck_la_LIBADD = ${libdl_LIBS} @@ -655,7 +655,7 @@ static ssize_t HX_sendfile_rw(int dst, int src, size_t count) { static const size_t bufsize = 0x10000; size_t xferd = 0; - ssize_t ret; + ssize_t ret = 0; void *buf = malloc(bufsize); if (buf == nullptr) return -ENOMEM; @@ -665,16 +665,13 @@ static ssize_t HX_sendfile_rw(int dst, int src, size_t count) size_t readsize = bufsize; if (count < readsize) readsize = count; + /* Return value of fullread/write is same as read/write(2). */ ret = HXio_fullread(src, buf, readsize); - if (ret < 0) { - errno = -ret; + if (ret < 0) break; - } ret = HXio_fullwrite(dst, buf, ret); - if (ret < 0) { - errno = -ret; + if (ret < 0) break; - } xferd += ret; count -= ret; } diff --git a/src/libHX.map b/src/libHX.map index 33415fa..37bfe0f 100644 --- a/src/libHX.map +++ b/src/libHX.map @@ -167,3 +167,14 @@ global: HX_inet_listen; HX_local_listen; } LIBHX_4.9; + +LIBHX_4.15 { +global: + HX_flpr; + HX_flprf; +} LIBHX_4.11; + +LIBHX_4.16 { +global: + HX_strtoull_nsec; +} LIBHX_4.15; @@ -7,6 +7,7 @@ * General Public License as published by the Free Software Foundation; * either version 2.1 or (at your option) any later version. */ +#include <math.h> /* fmod */ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -88,3 +89,19 @@ EXPORT_SYMBOL void HX_zvecfree(char **args) free(*travp); free(args); } + +EXPORT_SYMBOL float HX_flprf(float x, float y) +{ + float r = fmodf(x, y); + if (r < 0) + r += y; + return r; +} + +EXPORT_SYMBOL double HX_flpr(double x, double y) +{ + double r = fmod(x, y); + if (r < 0) + r += y; + return r; +} diff --git a/src/socket.c b/src/socket.c index 0b6f674..e2ad9ed 100644 --- a/src/socket.c +++ b/src/socket.c @@ -41,12 +41,17 @@ #else # define STUPIDWIN(x) (x) #endif -#if defined(__sunos__) && !defined(SO_PROTOCOL) +#if defined(__sun) && !defined(SO_PROTOCOL) # define SO_PROTOCOL SO_PROTOTYPE #endif #ifndef AI_V4MAPPED # define AI_V4MAPPED 0 #endif +#ifdef SOCK_CLOEXEC +# define PLATFORM_SKFLAGS SOCK_CLOEXEC +#else +# define PLATFORM_SKFLAGS 0 +#endif /** * Return the pointer to the singular colon character, any other input @@ -152,7 +157,8 @@ int HX_inet_connect(const char *host, uint16_t port, unsigned int oflags) if (ret != 0) ; for (const struct addrinfo *r = aires; r != nullptr; r = r->ai_next) { - int fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol); + int fd = socket(r->ai_family, r->ai_socktype | PLATFORM_SKFLAGS, + r->ai_protocol); if (fd < 0) { if (saved_errno == 0) saved_errno = errno; @@ -195,7 +201,8 @@ int HX_inet_connect(const char *host, uint16_t port, unsigned int oflags) static int HX_gai_listen(const struct addrinfo *r) { - int fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol); + int fd = socket(r->ai_family, r->ai_socktype | PLATFORM_SKFLAGS, + r->ai_protocol); if (fd < 0) return -2; static const int y = 1; @@ -282,7 +289,7 @@ int HX_local_listen(const char *path) if (!S_ISSOCK(sb.st_mode)) return -ENOTSOCK; - int testfd = socket(AF_LOCAL, SOCK_STREAM, 0); + int testfd = socket(AF_LOCAL, SOCK_STREAM | PLATFORM_SKFLAGS, 0); if (testfd < 0) return -errno; ret = connect(testfd, r.ai_addr, r.ai_addrlen); @@ -388,8 +395,12 @@ EXPORT_SYMBOL int HX_socket_from_env(const struct addrinfo *ai, const char *intf top_fd = x; } for (int fd = 3; fd < top_fd; ++fd) - if (try_sk_from_env(fd, ai, intf) == fd) + if (try_sk_from_env(fd, ai, intf) == fd) { +#ifdef SOCK_CLOEXEC + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); +#endif return fd; + } errno = ENOENT; return -1; } @@ -415,7 +426,7 @@ static int linux_sockaddr_local3(int sk, const void *buf, size_t bufsize) static int linux_sockaddr_local2(const struct sockaddr *sa, socklen_t sl) { - int sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); + int sk = socket(AF_NETLINK, SOCK_DGRAM | PLATFORM_SKFLAGS, NETLINK_ROUTE); if (sk < 0) return -errno; struct { @@ -474,7 +485,7 @@ static int openbsd_sockaddr_local3(int rsk, const void *buf, size_t bufsize) static int openbsd_sockaddr_local2(const struct sockaddr *sa, socklen_t sl) { - int sk = socket(AF_ROUTE, SOCK_RAW, AF_UNSPEC); + int sk = socket(AF_ROUTE, SOCK_RAW | PLATFORM_SKFLAGS, AF_UNSPEC); if (sk < 0) return -errno; struct { diff --git a/src/string.c b/src/string.c index ae19271..4abd694 100644 --- a/src/string.c +++ b/src/string.c @@ -9,7 +9,7 @@ */ #include <errno.h> #include <limits.h> -#include <math.h> +#include <math.h> /* nextafter */ #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -476,7 +476,7 @@ EXPORT_SYMBOL char *HX_strrev(char *s) EXPORT_SYMBOL size_t HX_strrtrim(char *expr) { - int i = strlen(expr), s = 0; + size_t i = strlen(expr), s = 0; while (i-- && HX_isspace(expr[i])) ++s; expr[++i] = '\0'; @@ -1037,55 +1037,63 @@ EXPORT_SYMBOL unsigned long long HX_strtoull_unit(const char *s, #define SECONDS_PER_YEAR 31557600 #define SECONDS_PER_MONTH 2629800 +#define NSEC_PER_SECOND 1000000000ULL static const struct { const char name[8]; unsigned int len; - uint32_t mult; + uint32_t s_mult; + uint64_t ns_mult; } time_multiplier[] = { - {"seconds", 7, 1}, - {"second", 6, 1}, - {"sec", 3, 1}, - {"s", 1, 1}, - {"minutes", 7, 60}, - {"minute", 6, 60}, - {"min", 3, 60}, - {"hours", 5, 3600}, - {"hour", 4, 3600}, - {"h", 1, 3600}, - {"days", 4, 86400}, - {"day", 3, 86400}, - {"d", 1, 86400}, - {"weeks", 5, 604800}, - {"week", 4, 604800}, - {"months", 6, SECONDS_PER_MONTH}, - {"month", 5, SECONDS_PER_MONTH}, - {"years", 5, SECONDS_PER_YEAR}, - {"year", 4, SECONDS_PER_YEAR}, - {"y", 1, SECONDS_PER_YEAR}, + {"seconds", 7, 1, 1 * NSEC_PER_SECOND}, + {"second", 6, 1, 1 * NSEC_PER_SECOND}, + {"sec", 3, 1, 1 * NSEC_PER_SECOND}, + {"s", 1, 1, 1 * NSEC_PER_SECOND}, + {"minutes", 7, 60, 60 * NSEC_PER_SECOND}, + {"minute", 6, 60, 60 * NSEC_PER_SECOND}, + {"min", 3, 60, 60 * NSEC_PER_SECOND}, + {"hours", 5, 3600, 3600 * NSEC_PER_SECOND}, + {"hour", 4, 3600, 3600 * NSEC_PER_SECOND}, + {"h", 1, 3600, 3600 * NSEC_PER_SECOND}, + {"days", 4, 86400, 86400 * NSEC_PER_SECOND}, + {"day", 3, 86400, 86400 * NSEC_PER_SECOND}, + {"d", 1, 86400, 86400 * NSEC_PER_SECOND}, + {"weeks", 5, 604800, 604800 * NSEC_PER_SECOND}, + {"week", 4, 604800, 604800 * NSEC_PER_SECOND}, + {"months", 6, SECONDS_PER_MONTH, SECONDS_PER_MONTH * NSEC_PER_SECOND}, + {"month", 5, SECONDS_PER_MONTH, SECONDS_PER_MONTH * NSEC_PER_SECOND}, + {"years", 5, SECONDS_PER_YEAR, SECONDS_PER_YEAR * NSEC_PER_SECOND}, + {"year", 4, SECONDS_PER_YEAR, SECONDS_PER_YEAR * NSEC_PER_SECOND}, + {"y", 1, SECONDS_PER_YEAR, SECONDS_PER_YEAR * NSEC_PER_SECOND}, + {"msec", 4, 0, 1000000}, + {"ms", 2, 0, 1000000}, + {"µsec", 5, 0, 1000}, + {"µs", 3, 0, 1000}, + {"nsec", 4, 0, 1}, + {"ns", 2, 0, 1}, + {"", 0, 1, NSEC_PER_SECOND}, }; -EXPORT_SYMBOL unsigned long long HX_strtoull_sec(const char *s, char **out_end) +static unsigned long long HX_strtoull_time(const char *s, char **out_end, bool nsec) { - unsigned long long seconds = 0; + unsigned long long quant = 0; while (*s != '\0') { while (HX_isspace(*s)) ++s; - if (*s == '-') { + if (*s == '-') break; - } char *end = nullptr; unsigned long long num = strtoull(s, &end, 10); + double frac = 0; + bool have_frac = *end == '.'; + if (have_frac) + frac = strtod(s, &end); if (end == s) break; s = end; while (HX_isspace(*s)) ++s; - if (!HX_isalpha(*s)) { - seconds += num; - continue; - } unsigned int i; for (i = 0; i < ARRAY_SIZE(time_multiplier); ++i) if (strncmp(s, time_multiplier[i].name, @@ -1094,12 +1102,26 @@ EXPORT_SYMBOL unsigned long long HX_strtoull_sec(const char *s, char **out_end) break; if (i == ARRAY_SIZE(time_multiplier)) break; - seconds += num * time_multiplier[i].mult; + unsigned long long mult = nsec ? time_multiplier[i].ns_mult : time_multiplier[i].s_mult; + if (have_frac) + quant += frac * mult; + else + quant += num * mult; s += time_multiplier[i].len; } if (out_end != nullptr) *out_end = const_cast(char *, s); - return seconds; + return quant; +} + +EXPORT_SYMBOL unsigned long long HX_strtoull_sec(const char *s, char **out_end) +{ + return HX_strtoull_time(s, out_end, false); +} + +EXPORT_SYMBOL unsigned long long HX_strtoull_nsec(const char *s, char **out_end) +{ + return HX_strtoull_time(s, out_end, true); } EXPORT_SYMBOL char *HX_unit_seconds(char *out, size_t outsize, diff --git a/src/tc-cast.c b/src/tc-cast.c index 2c58805..36bf8b0 100644 --- a/src/tc-cast.c +++ b/src/tc-cast.c @@ -77,7 +77,7 @@ static void c_constA(void) int main(void) { if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; c_signed(); c_reinterpret(); c_static(); @@ -86,5 +86,5 @@ int main(void) c_const3(); c_constA(); HX_exit(); - return 0; + return EXIT_SUCCESS; } diff --git a/src/tc-compile.c b/src/tc-compile.c index d2b0d09..d60ff21 100644 --- a/src/tc-compile.c +++ b/src/tc-compile.c @@ -17,7 +17,7 @@ int main(void) unsigned char bmchar[HXbitmap_size(unsigned char, 256)] = {0}; if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; printf("sizeof bmllong:\t%" HX_SIZET_FMT "u, array_size: %" HX_SIZET_FMT "u\n", sizeof(bmllong), ARRAY_SIZE(bmllong)); printf("sizeof bmlong:\t%" HX_SIZET_FMT "u, array_size: %" HX_SIZET_FMT "u\n", diff --git a/src/tc-dir.c b/src/tc-dir.c index 12cbacd..ec44977 100644 --- a/src/tc-dir.c +++ b/src/tc-dir.c @@ -25,7 +25,7 @@ static void lookatdir(const char *dname) int main(int argc, const char **argv) { if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; if (argc == 1) { /* On Windows VCRT, "/" yields nothing, "c:/" is needed */ lookatdir("/"); diff --git a/src/tc-io.c b/src/tc-io.c index 639ebcc..77baf01 100644 --- a/src/tc-io.c +++ b/src/tc-io.c @@ -20,7 +20,10 @@ static void sf(void) return; } ssize_t ret = HX_sendfile(dst, src, SIZE_MAX); - printf("sendfile transferred %zd bytes\n", ret); + if (ret < 0) + printf("sendfile: %s\n", strerror(errno)); + else + printf("sendfile transferred %zd bytes\n", ret); close(dst); close(src); } diff --git a/src/tc-list.c b/src/tc-list.c index 2fd6380..e8a30b6 100644 --- a/src/tc-list.c +++ b/src/tc-list.c @@ -22,7 +22,7 @@ union list_encap { static HXCLIST_HEAD(strings_ct); -static void l_init(unsigned int max, bool unshift) +static int l_init(unsigned int max, bool unshift) { static const char *const msg[] = {"Pushing", "Unshifting"}; struct text_object *obj; @@ -34,7 +34,7 @@ static void l_init(unsigned int max, bool unshift) #else obj = malloc(sizeof(*obj)); if (obj == NULL) - abort(); + return EXIT_FAILURE; #endif HXlist_init(&obj->list); obj->id[0] = HX_irand('a', 'z'+1); @@ -48,6 +48,7 @@ static void l_init(unsigned int max, bool unshift) else HXclist_push(&strings_ct, &obj->list); } + return EXIT_SUCCESS; } static void l_traverse(void) @@ -137,16 +138,17 @@ static void l_shift(void) #pragma GCC diagnostic pop } -int main(int argc, const char **argv) +static int runner(int argc, const char **argv) { unsigned int max = 10; if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; if (argc >= 2) max = strtoul(argv[1], NULL, 0); - - l_init(max, HX_rand() & 1); + int ret = l_init(max, HX_rand() & 1); + if (ret != EXIT_SUCCESS) + return ret; l_traverse(); l_dump(HX_rand() & 1); l_empty(); @@ -154,3 +156,11 @@ int main(int argc, const char **argv) HX_exit(); return EXIT_SUCCESS; } + +int main(int argc, const char **argv) +{ + int ret = runner(argc, argv); + if (ret != EXIT_FAILURE) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-map.c b/src/tc-map.c index 0037c83..e9e896f 100644 --- a/src/tc-map.c +++ b/src/tc-map.c @@ -205,7 +205,7 @@ static void tmap_trav_speed(struct HXmap *map) tmap_ipop(); } -static void tmap_flat(const struct HXmap *map) +static int tmap_flat(const struct HXmap *map) { struct HXmap_node *nodes; unsigned int i; @@ -215,12 +215,13 @@ static void tmap_flat(const struct HXmap *map) nodes = HXmap_keysvalues(map); if (nodes == NULL) { perror("HXmap_keysvalues"); - abort(); + return EXIT_FAILURE; } for (i = 0; i < map->items; ++i) tmap_printf("%u. %s -> %s\n", i, nodes[i].skey, nodes[i].sdata); tmap_ipop(); free(nodes); + return EXIT_SUCCESS; } static void tmap_trav(struct HXmap *map) @@ -251,7 +252,7 @@ static void tmap_trav(struct HXmap *map) HXmap_travfree(iter); } -static void tmap_generic_tests(enum HXmap_type type, +static int tmap_generic_tests(enum HXmap_type type, unsigned long (*hash_fn)(const void *, size_t), const char *hash_name) { struct HXmap_ops ops = {.k_hash = hash_fn}; @@ -265,10 +266,13 @@ static void tmap_generic_tests(enum HXmap_type type, tmap_flush(map, false); tmap_add_rand(map, 2); - tmap_flat(map); + int ret = tmap_flat(map); + if (ret != EXIT_SUCCESS) + return ret; tmap_trav(map); tmap_flush(map, true); HXmap_free(map); + return EXIT_SUCCESS; } static int tmap_strtolcmp(const void *a, const void *b, size_t z) @@ -722,15 +726,16 @@ static void tmap_zero(void) fprintf(stderr, "eek!\n"); } -int main(void) +static int runner(void) { if (HX_init() <= 0) - abort(); - + return EXIT_FAILURE; tmap_zero(); tmap_printf("* HXhashmap\n"); - tmap_generic_tests(HXMAPT_HASH, HXhash_djb2, "DJB2"); + int ret = tmap_generic_tests(HXMAPT_HASH, HXhash_djb2, "DJB2"); + if (ret != EXIT_SUCCESS) + return ret; tmap_generic_tests(HXMAPT_HASH, HXhash_jlookup3s, "JL3"); tmap_hmap_test_1(); @@ -742,3 +747,11 @@ int main(void) HX_exit(); return EXIT_SUCCESS; } + +int main(void) +{ + int ret = runner(); + if (ret != EXIT_SUCCESS) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-memmem.c b/src/tc-memmem.c index a4f272e..1a56f1b 100644 --- a/src/tc-memmem.c +++ b/src/tc-memmem.c @@ -14,14 +14,14 @@ static unsigned int size = 1048576 * 64; static const char filler_text[] = "Slhrdlu cringle tongle flonging blobbity bleep blingmangl"; -static void long_scan(void) +static int long_scan(void) { struct timespec start, stop, delta; char *filler2, *p; filler2 = malloc(size); if (filler2 == NULL) - abort(); + return EXIT_FAILURE; memset(filler2, 'l', size); filler2[size-2] = 'a'; @@ -32,19 +32,20 @@ static void long_scan(void) HX_timespec_sub(&delta, &stop, &start); printf("long_scan: " HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(&delta)); free(filler2); + return EXIT_SUCCESS; } -int main(void) +static int runner(void) { unsigned int i; char *haystack; struct timespec start, stop, delta; if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; haystack = malloc(size); if (haystack == NULL) - abort(); + return EXIT_FAILURE; memset(haystack, 'A', size); haystack[size-1] = haystack[size-2] = 'Z'; printf("Init done\n"); @@ -55,8 +56,11 @@ int main(void) printf("%p\n", HX_memmem(filler_text, strlen(filler_text), "ngl", 3)); printf("%p\n", HX_memmem(filler_text, strlen(filler_text), "ngl", 3)); - long_scan(); - + int ret = long_scan(); + if (ret != EXIT_SUCCESS) { + free(haystack); + return ret; + } for (i = 0; i < 10; ++i) { printf("Search length %u...", i); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start); @@ -66,6 +70,15 @@ int main(void) printf(HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(&delta)); } + free(haystack); HX_exit(); return EXIT_SUCCESS; } + +int main(void) +{ + int ret = runner(); + if (ret != EXIT_FAILURE) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-misc.c b/src/tc-misc.c index 5b043d4..d422f21 100644 --- a/src/tc-misc.c +++ b/src/tc-misc.c @@ -8,13 +8,13 @@ #include <libHX/init.h> #include <libHX/misc.h> -int main(int argc, const char **argv) +static int runner(int argc, const char **argv) { unsigned int n; struct stat sa, sb; if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; printf("%d\n", HX_ffs(0)); for (n = 1; ; n <<= 1) { printf("%08x = %d\n", n, HX_ffs(n)); @@ -30,12 +30,23 @@ int main(int argc, const char **argv) if (argc >= 3) { if (stat(argv[1], &sa) < 0 || - stat(argv[2], &sb) < 0) + stat(argv[2], &sb) < 0) { perror("stat"); - else + return EXIT_FAILURE; + } else { printf("Difference: %ld\n", HX_time_compare(&sa, &sb, 'm')); + return EXIT_FAILURE; + } } HX_exit(); return EXIT_SUCCESS; } + +int main(int argc, const char **argv) +{ + int ret = runner(argc, argv); + if (ret != EXIT_FAILURE) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-netio.c b/src/tc-netio.c index 5d3c22e..4510d0a 100644 --- a/src/tc-netio.c +++ b/src/tc-netio.c @@ -19,34 +19,47 @@ #endif #include <libHX/init.h> #include <libHX/io.h> +#include "internal.h" -int main(void) +static int runner(void) { const char id[] = "SSH-2.0-OpenSSH_9.9"; - struct addrinfo *res; + struct addrinfo *res = nullptr; int fd, ret; if ((ret = HX_init()) <= 0) { fprintf(stderr, "HX_init: %s\n", strerror(-ret)); - abort(); + return EXIT_FAILURE; } fd = socket(AF_INET6, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); - abort(); + return EXIT_FAILURE; } if (getaddrinfo("::1", "22", NULL, &res) < 0) { perror("getaddrinfo"); - abort(); + close(fd); + return EXIT_FAILURE; } if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { perror("connect"); - abort(); + freeaddrinfo(res); + close(fd); + return EXIT_FAILURE; } if (HXio_fullwrite(fd, id, strlen(id)) < 0) perror("write"); + freeaddrinfo(res); close(fd); HX_exit(); return EXIT_SUCCESS; } + +int main(void) +{ + int ret = runner(); + if (ret != EXIT_SUCCESS) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-option.c b/src/tc-option.c index db3d3f4..83271e0 100644 --- a/src/tc-option.c +++ b/src/tc-option.c @@ -99,7 +99,7 @@ static void t_empty_argv(void) int main(int argc, const char **argv) { if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; printf("Return value of HX_getopt: %d\n", HX_getopt(table, &argc, &argv, HXOPT_USAGEONERR)); t_empty_argv(); diff --git a/src/tc-proc.c b/src/tc-proc.c index 19c8b18..c17b232 100644 --- a/src/tc-proc.c +++ b/src/tc-proc.c @@ -51,7 +51,7 @@ static void t_async1(void) int main(void) { if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; printf("top fd: %d\n", HXproc_top_fd()); /* let it fail - test verbosity */ diff --git a/src/tc-rand.c b/src/tc-rand.c index 149f18e..c581372 100644 --- a/src/tc-rand.c +++ b/src/tc-rand.c @@ -12,7 +12,7 @@ int main(void) unsigned int i; if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; for (i = 0; i < 15; ++i) { printf("%d ", HX_irand(i, i)); printf("%.1f ", HX_drand(i, i)); diff --git a/src/tc-shconfig.c b/src/tc-shconfig.c index 556f90f..3717c15 100644 --- a/src/tc-shconfig.c +++ b/src/tc-shconfig.c @@ -26,7 +26,7 @@ static void t_shconfig(const char *file) fprintf(stderr, "Read error %s: %s\n", file, strerror(errno)); } -static void t_shconfig2(const char *file) +static int t_shconfig2(const char *file) { const struct HXmap_node *node; struct HXmap_trav *trav; @@ -35,15 +35,16 @@ static void t_shconfig2(const char *file) map = HX_shconfig_map(file); if (map == NULL) { fprintf(stderr, "HX_shconfig_map: %s\n", strerror(errno)); - abort(); + return EXIT_FAILURE; } trav = HXmap_travinit(map, HXMAP_NOFLAGS); while ((node = HXmap_traverse(trav)) != NULL) printf("\t\"%s\" -> \"%s\"\n", node->skey, node->sdata); HXmap_travfree(trav); + return EXIT_SUCCESS; } -int main(int argc, const char **argv) +static int runner(int argc, const char **argv) { int ret; @@ -53,7 +54,17 @@ int main(int argc, const char **argv) return EXIT_FAILURE; } t_shconfig((argc >= 2) ? argv[1] : "tc-shconf.c"); - t_shconfig2((argc >= 2) ? argv[1] : "tc-shconf.c"); + ret = t_shconfig2((argc >= 2) ? argv[1] : "tc-shconf.c"); + if (ret != EXIT_SUCCESS) + return ret; HX_exit(); return EXIT_SUCCESS; } + +int main(int argc, const char **argv) +{ + int ret = runner(argc, argv); + if (ret != EXIT_SUCCESS) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-string.c b/src/tc-string.c index 9bf668b..1c7ed09 100644 --- a/src/tc-string.c +++ b/src/tc-string.c @@ -21,14 +21,14 @@ #include <libHX/string.h> #include "internal.h" -static void t_mc(void) +static int t_mc(void) { hxmc_t *s, *old_s; s = HXmc_meminit(NULL, 4096); printf("%" HX_SIZET_FMT "u\n", HXmc_length(s)); if (HXmc_length(s) != 0) - abort(); + return EXIT_FAILURE; old_s = s; HXmc_trunc(&s, 8192); if (old_s != s) @@ -37,6 +37,7 @@ static void t_mc(void) HXmc_setlen(&s, 16384); printf("Length is now %" HX_SIZET_FMT "u\n", HXmc_length(s)); HXmc_free(s); + return EXIT_SUCCESS; } static void t_path(void) @@ -60,13 +61,12 @@ static void t_path(void) } } -static void t_strcpy(void) +static int t_strcpy(void) { hxmc_t *vp = NULL; HXmc_strcpy(&vp, NULL); - if (vp != NULL) - abort(); + return vp == nullptr ? EXIT_SUCCESS : EXIT_FAILURE; } static void t_strdup(void) @@ -291,7 +291,7 @@ static void t_strlcpy2(void) assert(a[0] == 49 && a[0] == a[1] && a[1] == a[2]); } -static void t_units(void) +static int t_units(void) { static const struct { unsigned long long num; @@ -320,18 +320,19 @@ static void t_units(void) printf("\t%llu -> %s\n", vt[i].num, buf); if (strcmp(buf, vt[i].exp_1024) != 0) { printf("\texpected %s\n", vt[i].exp_1024); - abort(); + return EXIT_FAILURE; } HX_unit_size(buf, ARRAY_SIZE(buf), vt[i].num, 1000, 9120); printf("\t%llu -> %s\n", vt[i].num, buf); if (strcmp(buf, vt[i].exp_1000) != 0) { printf("\texpected %s\n", vt[i].exp_1000); - abort(); + return EXIT_FAILURE; } } + return EXIT_SUCCESS; } -static void t_units_cu(void) +static int t_units_cu(void) { static const struct { unsigned long long num; @@ -360,18 +361,19 @@ static void t_units_cu(void) printf("\t%llu -> %s\n", vt[i].num, buf); if (strcmp(buf, vt[i].exp_1024) != 0) { printf("\texpected %s\n", vt[i].exp_1024); - abort(); + return EXIT_FAILURE; } HX_unit_size_cu(buf, ARRAY_SIZE(buf), vt[i].num, 1000); printf("\t%llu -> %s\n", vt[i].num, buf); if (strcmp(buf, vt[i].exp_1000) != 0) { printf("\texpected %s\n", vt[i].exp_1000); - abort(); + return EXIT_FAILURE; } } + return EXIT_SUCCESS; } -static void t_units_strto(void) +static int t_units_strto(void) { static const struct { const char input[24]; @@ -416,8 +418,9 @@ static void t_units_strto(void) unsigned long long q = HX_strtoull_unit(vt[i].input, &end, vt[i].exponent); printf("%s -> %llu __ %s\n", vt[i].input, q, end); if (q != vt[i].expect_out || strcmp(end, vt[i].expect_rem) != 0) - printf("BUG\n"); + return EXIT_FAILURE; } + return EXIT_SUCCESS; } static void t_time_units(void) @@ -454,25 +457,30 @@ static void t_time_units(void) static void t_time_strto(void) { + #define NS_PER_S 1000000000ULL static const struct { const char *input; - unsigned long long expect_out; - const char expect_rem[4]; + unsigned long long expect_s, expect_ns; + const char expect_rem[8]; } vt[] = { - {"1y1month1week1d1h1min1s ", 31557600+2629800+86400*8+3600+60+1, ""}, - {" -1d", 0, "-1d"}, - {"1 -", 1, "-"}, - {"1s", 1, ""}, - {"1min", 60, ""}, - {"0", 0, ""}, + {"29µs", 0, 29000, ""}, + {"1y", 31557600, NS_PER_S * 31557600, ""}, + {"1y1month1week1d1h1min1s ", 31557600+2629800+86400*8+3600+60+1, NS_PER_S * (31557600+2629800+86400*8+3600+60+1), ""}, + {" -1d", 0, 0, "-1d"}, + {"1 -", 1, NS_PER_S, "-"}, + {"12.5 hours .5 hours 240 minutes 25200 seconds", 86400, NS_PER_S * 86400, ""}, + {"1s", 1, NS_PER_S, ""}, + {"1min", 60, 60 * NS_PER_S, ""}, + {"0", 0, 0, ""}, }; char *end; printf("===== t_time_strto\n"); for (size_t i = 0; i < ARRAY_SIZE(vt); ++i) { unsigned long long q = HX_strtoull_sec(vt[i].input, &end); - printf("\"%s\" => %llus + \"%s\"\n", vt[i].input, q, end); - if (q != vt[i].expect_out) - printf("\tBUG: expected %llus\n", vt[i].expect_out); + unsigned long long qn = HX_strtoull_nsec(vt[i].input, &end); + printf("\"%s\" => %llus [%lluns] + \"%s\"\n", vt[i].input, q, qn, end); + if (q != vt[i].expect_s || qn != vt[i].expect_ns) + printf("\tBUG: expected %llus [%lluns]\n", vt[i].expect_s, vt[i].expect_ns); if (strcmp(end, vt[i].expect_rem) != 0) printf("\tBUG: expected remainder \"%s\"\n", vt[i].expect_rem); } @@ -507,14 +515,14 @@ static int t_strmid(void) #undef T } -int main(int argc, const char **argv) +static int runner(int argc, const char **argv) { hxmc_t *tx = NULL; const char *file = (argc >= 2) ? argv[1] : "tx-string.cpp"; FILE *fp; if (HX_init() <= 0) - abort(); + return EXIT_FAILURE; int ret = t_strmid(); if (ret != EXIT_SUCCESS) return EXIT_FAILURE; @@ -528,9 +536,13 @@ int main(int argc, const char **argv) fclose(fp); } - t_mc(); + ret = t_mc(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; t_path(); - t_strcpy(); + ret = t_strcpy(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; t_strncat(); t_strnlen(); t_strdup(); @@ -538,9 +550,15 @@ int main(int argc, const char **argv) t_strtrim(); t_split(); t_split2(); - t_units(); - t_units_cu(); - t_units_strto(); + ret = t_units(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; + ret = t_units_cu(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; + ret = t_units_strto(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; t_time_units(); t_time_strto(); t_strlcpy(); @@ -549,3 +567,11 @@ int main(int argc, const char **argv) HX_exit(); return EXIT_SUCCESS; } + +int main(int argc, const char **argv) +{ + int ret = runner(argc, argv); + if (ret != EXIT_FAILURE) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-switchuser.c b/src/tc-switchuser.c index 1763527..4cc5604 100644 --- a/src/tc-switchuser.c +++ b/src/tc-switchuser.c @@ -19,7 +19,7 @@ static const struct HXoption options_table[] = { HXOPT_TABLEEND, }; -int main(int argc, const char **argv) +static int runner(int argc, const char **argv) { HX_getopt(options_table, &argc, &argv, HXOPT_USAGEONERR); const char *user = user_name != NULL ? user_name : "-"; @@ -27,12 +27,12 @@ int main(int argc, const char **argv) switch (HXproc_switch_user(user_name, group_name)) { case HXPROC_USER_NOT_FOUND: if (user_name == NULL) - abort(); /* impossible outcomes */ + return EXIT_FAILURE; /* impossible outcomes */ printf("No such user \"%s\": %s\n", user_name, strerror(errno)); break; case HXPROC_GROUP_NOT_FOUND: if (group_name == NULL || *group_name == '\0') - abort(); /* impossible outcome */ + return EXIT_FAILURE; /* impossible outcome */ printf("No such group \"%s\": %s\n", group_name, strerror(errno)); break; case HXPROC_SETUID_FAILED: @@ -64,6 +64,14 @@ int main(int argc, const char **argv) } return EXIT_SUCCESS; } + +int main(int argc, const char **argv) +{ + int ret = runner(argc, argv); + if (ret != EXIT_SUCCESS) + fprintf(stderr, "FAILED\n"); + return ret; +} #else int main(void) { diff --git a/src/tc-time.c b/src/tc-time.c index 5097cde..e810233 100644 --- a/src/tc-time.c +++ b/src/tc-time.c @@ -207,7 +207,7 @@ static void print_op2(const struct timespec *r, const struct timespec *a, printf(HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(r)); } -static void test_add(void) +static int test_add(void) { const struct timespec *a, *b; struct timespec r, s; @@ -220,13 +220,14 @@ static void test_add(void) HX_timespec_add_DIVQ(&s, a, b); print_op2(&r, a, "+Q", b); if (r.tv_sec != s.tv_sec || r.tv_nsec != s.tv_nsec) - abort(); + return EXIT_FAILURE; HX_timespec_sub(&r, a, b); print_op2(&r, a, "- ", b); printf("----------\n"); } } printf("\n"); + return EXIT_SUCCESS; } static void test_adds_nz(time_t s, add_func_t fn) @@ -303,7 +304,7 @@ static void test_adds(void) printf("\n"); } -static void test_mul(void) +static int test_mul(void) { struct timespec r, s; unsigned int i; @@ -324,7 +325,7 @@ static void test_mul(void) printf(HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(&s)); if (r.tv_sec != s.tv_sec || r.tv_nsec != s.tv_nsec) - abort(); + return EXIT_FAILURE; } for (k = -3; k <= 3; k += 0.1) { @@ -341,6 +342,7 @@ static void test_mul(void) } printf("\n"); + return EXIT_SUCCESS; } static void test_muls_1i(const char *text, mul_func_t fn) @@ -386,18 +388,29 @@ static void test_muls(void) printf("\n"); } -int main(void) +static int runner(void) { if (HX_init() <= 0) - abort(); - + return EXIT_FAILURE; test_basic(); test_same(); test_neg(); - test_add(); - test_mul(); + int ret = test_add(); + if (ret != EXIT_SUCCESS) + return ret; + ret = test_mul(); + if (ret != EXIT_SUCCESS) + return ret; test_adds(); test_muls(); HX_exit(); return EXIT_SUCCESS; } + +int main(void) +{ + int ret = runner(); + if (ret != EXIT_SUCCESS) + fprintf(stderr, "FAILED\n"); + return ret; +} diff --git a/src/tc-xml.c b/src/tc-xml.c deleted file mode 100644 index 49b24ad..0000000 --- a/src/tc-xml.c +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: MIT -#include <stdbool.h> -#include <stdio.h> -#include <libxml/parser.h> -#include <libHX/defs.h> -#include <libHX/libxml_helper.h> - -int main(void) -{ - xmlDoc *doc; - xmlNode *root, *etc, *node; - char *result = NULL; - int size = 0; - - doc = xmlNewDoc(NULL); - root = xmlNewDocNode(doc, NULL, "root", NULL); - xmlDocSetRootElement(doc, root); - xml_newnode(root, "empty", NULL); - etc = xml_newnode(root, "filled", NULL); - xml_newnode(etc, "a", "1234 bytes"); - node = xml_newnode(etc, "b", "0 bytes"); - xml_newnode(node, "extra", NULL); - xmlDocDumpFormatMemory(doc, reinterpret_cast(xmlChar **, &result), - &size, true); - xmlSaveFileEnc("test.xml", doc, "utf-8"); - if (result != NULL) - printf("%.*s\n", size, result); - return 0; -} diff --git a/src/uxcompat.h b/src/uxcompat.h deleted file mode 100644 index e735c50..0000000 --- a/src/uxcompat.h +++ /dev/null @@ -1,104 +0,0 @@ -#ifndef _LIBHX_UXCOMPAT_H -#define _LIBHX_UXCOMPAT_H 1 - -#if defined(__cplusplus) && __cplusplus >= 201100UL -# include <cstddef> -# include <cstdint> -#else -# include <stddef.h> -# include <stdint.h> -#endif -#include <sys/stat.h> - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef ENOSYS -# define ENOSYS 38 /* Function not implemented */ -#endif - -#ifndef S_IFLNK -# define S_IFLNK 0xA000 -#endif -#ifndef S_IFSOCK -# define S_IFSOCK 0xC000 -#endif -#ifndef S_IFBLK -# define S_IFBLK 0x6000 -#endif -#ifndef S_IFCHR -# define S_IFCHR 0x2000 -#endif -#ifndef S_IFIFO -# define S_IFIFO 0x1000 -#endif -#ifndef S_ISBLK -# define S_ISBLK(__mode) (((__mode) & S_IFMT) == S_IFBLK) -#endif -#ifndef S_ISCHR -# define S_ISCHR(__mode) (((__mode) & S_IFMT) == S_IFCHR) -#endif -#ifndef S_ISDIR -# define S_ISDIR(__mode) (((__mode) & S_IFMT) == S_IFDIR) -#endif -#ifndef S_ISREG -# define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG) -#endif -#ifndef S_ISLNK -# define S_ISLNK(__mode) (((__mode) & S_IFMT) == S_IFLNK) -#endif -#ifndef S_ISFIFO -# define S_ISFIFO(__mode) (((__mode) & S_IFMT) == S_IFIFO) -#endif -#ifndef S_ISSOCK -# define S_ISSOCK(__mode) (((__mode) & S_IFMT) == S_IFSOCK) -#endif -#ifndef S_IRGRP -# define S_IRGRP 00040 -#endif -#ifndef S_IWGRP -# define S_IWGRP 00020 -#endif -#ifndef S_IROTH -# define S_IROTH 00004 -#endif -#ifndef S_IWOTH -# define S_IWOTH 00002 -#endif - -struct stat; - -/* - * UX-FILE.C - */ -extern int chown(const char *, long, long); -extern int fchmod(int, long); -extern int fchown(int, long, long); -extern int lchown(const char *, long, long); -extern int lstat(const char *, struct stat *); -extern int mkfifo(const char *, long); -extern int mknod(const char *, long, long); -extern int readlink(const char *, char *, size_t); -extern int symlink(const char *, const char *); - -/* - * UX-MMAP.C - */ -#ifdef _WIN32 -# define MAP_FAILED reinterpret_cast(void *, static_cast(intptr_t, -1)) -# define PROT_NONE 0x0 -# define PROT_READ 0x1 -# define PROT_WRITE 0x2 -# define PROT_EXEC 0x4 -# define MAP_SHARED 0x1 -# define MAP_PRIVATE 0x2 -extern void *mmap(void *, size_t, int, int, int, off_t); -extern int munmap(void *, size_t); -#endif - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* _LIBHX_UXCOMPAT_H */ |