]> git.draconx.ca Git - rarpd-dx.git/commitdiff
Punt iputils_common helpers.
authorNick Bowler <nbowler@draconx.ca>
Thu, 27 Jul 2023 03:29:57 +0000 (23:29 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 27 Jul 2023 03:37:48 +0000 (23:37 -0400)
Only two functions from this common library are used by rarpd.

The error fallback depends on program_invocation_short_name, which is a
GNU C library feature, and if you have the GNU C library, you have error,
so I'm really not sure what systems this works on.

And for rarpd, there seems to be no point in explicitly closing the
standard I/O streams since it doesn't print anything normally.

Makefile.am
src/iputils_common.c [deleted file]
src/iputils_common.h [deleted file]
src/rarpd.c

index 2909c0728771b298b619da1cef1190cd35812065..9ea63081931a0709422bb2a5c64c40b46ac498f5 100644 (file)
@@ -35,7 +35,7 @@ MAINTAINERCLEANFILES = $(dist_man_MANS)
 EXTRA_DIST += $(dist_man_MANS:.8=.xml) $(srcdir)/doc/custom-man.xsl
 
 sbin_PROGRAMS = rarpd
-rarpd_SOURCES = src/rarpd.c src/iputils_common.c src/iputils_common.h
+rarpd_SOURCES = src/rarpd.c
 
 # When running "make dist" in a VPATH build with a read-only srcdir, Automake
 # will produce a distribution with all files read-only.  Moreover, the files
diff --git a/src/iputils_common.c b/src/iputils_common.c
deleted file mode 100644 (file)
index c41f201..0000000
+++ /dev/null
@@ -1,140 +0,0 @@
-#include <errno.h>
-#include <stdarg.h>
-#include <stdio_ext.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-
-#if HAVE_GETRANDOM
-# include <sys/random.h>
-#endif
-
-#ifdef HAVE_ERROR_H
-# include <error.h>
-#else
-void error(int status, int errnum, const char *format, ...)
-{
-       va_list ap;
-
-       fprintf(stderr, "%s: ", program_invocation_short_name);
-       va_start(ap, format);
-       vfprintf(stderr, format, ap);
-       va_end(ap);
-       if (errnum)
-               fprintf(stderr, ": %s\n", strerror(errnum));
-       else
-               fprintf(stderr, "\n");
-       if (status)
-               exit(status);
-}
-#endif
-
-int close_stream(FILE *stream)
-{
-       const int flush_status = fflush(stream);
-#ifdef HAVE___FPENDING
-       const int some_pending = (__fpending(stream) != 0);
-#endif
-       const int prev_fail = (ferror(stream) != 0);
-       const int fclose_fail = (fclose(stream) != 0);
-
-       if (flush_status ||
-           prev_fail || (fclose_fail && (
-#ifdef HAVE___FPENDING
-                                         some_pending ||
-#endif
-                                         errno != EBADF))) {
-               if (!fclose_fail && !(errno == EPIPE))
-                       errno = 0;
-               return EOF;
-       }
-       return 0;
-}
-
-void close_stdout(void)
-{
-       if (close_stream(stdout) != 0 && !(errno == EPIPE)) {
-               if (errno)
-                       error(0, errno, "write error");
-               else
-                       error(0, 0, "write error");
-               _exit(EXIT_FAILURE);
-       }
-       if (close_stream(stderr) != 0)
-               _exit(EXIT_FAILURE);
-}
-
-long strtol_or_err(char const *const str, char const *const errmesg,
-                  const long min, const long max)
-{
-       long num;
-       char *end = NULL;
-
-       errno = 0;
-       if (str == NULL || *str == '\0')
-               goto err;
-       num = strtol(str, &end, 10);
-       if (errno || str == end || (end && *end))
-               goto err;
-       if (num < min || max < num)
-               error(EXIT_FAILURE, 0, "%s: '%s': out of range: %lu <= value <= %lu",
-                     errmesg, str,  min, max);
-       return num;
- err:
-       error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
-       abort();
-}
-
-static unsigned int iputil_srand_fallback(void)
-{
-       struct timespec ts;
-
-       clock_gettime(CLOCK_REALTIME, &ts);
-       return ((getpid() << 16) ^ getuid() ^ ts.tv_sec ^ ts.tv_nsec);
-}
-
-void iputils_srand(void)
-{
-       unsigned int i;
-
-#if HAVE_GETRANDOM
-       ssize_t ret;
-
-       do {
-               errno = 0;
-               ret = getrandom(&i, sizeof(i), GRND_NONBLOCK);
-               switch (errno) {
-               case 0:
-                       break;
-               case EINTR:
-                       continue;
-               default:
-                       i = iputil_srand_fallback();
-                       goto done;
-               }
-       } while (ret != sizeof(i));
- done:
-#else
-       i = iputil_srand_fallback();
-#endif
-       srand(i);
-       /* Consume up to 31 random numbers */
-       i = rand() & 0x1F;
-       while (0 < i) {
-               rand();
-               i--;
-       }
-}
-
-void timespecsub(struct timespec *a, struct timespec *b, struct timespec *res)
-{
-       res->tv_sec = a->tv_sec - b->tv_sec;
-       res->tv_nsec = a->tv_nsec - b->tv_nsec;
-
-       if (res->tv_nsec < 0) {
-               res->tv_sec--;
-               res->tv_nsec += 1000000000L;
-       }
-}
diff --git a/src/iputils_common.h b/src/iputils_common.h
deleted file mode 100644 (file)
index 26e8f7c..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-#ifndef IPUTILS_COMMON_H
-#define IPUTILS_COMMON_H
-
-#include <stdio.h>
-#include <sys/time.h>
-
-#define ARRAY_SIZE(arr) \
-  (sizeof(arr) / sizeof((arr)[0]) + \
-   sizeof(__typeof__(int[1 - 2 * \
-         !!__builtin_types_compatible_p(__typeof__(arr), \
-                                        __typeof__(&arr[0]))])) * 0)
-
-#ifdef __GNUC__
-# define iputils_attribute_format(t, n, m) __attribute__((__format__ (t, n, m)))
-#else
-# define iputils_attribute_format(t, n, m)
-#endif
-
-#if defined(USE_IDN) || defined(ENABLE_NLS)
-# include <locale.h>
-#endif
-
-#ifdef ENABLE_NLS
-# include <libintl.h>
-# define _(Text) gettext (Text)
-#else
-# undef bindtextdomain
-# define bindtextdomain(Domain, Directory) /* empty */
-# undef textdomain
-# define textdomain(Domain) /* empty */
-# define _(Text) Text
-#endif
-
-#ifdef USE_IDN
-# include <idn2.h>
-
-# include <netdb.h>
-# ifndef AI_IDN
-#  define AI_IDN               0x0040
-# endif
-# ifndef AI_CANONIDN
-#  define AI_CANONIDN          0x0080
-# endif
-# ifndef NI_IDN
-#  define NI_IDN 32
-# endif
-#endif /* #ifdef USE_IDN */
-
-#ifndef SOL_IPV6
-# define SOL_IPV6 IPPROTO_IPV6
-#endif
-#ifndef IP_PMTUDISC_DO
-# define IP_PMTUDISC_DO                2
-#endif
-#ifndef IPV6_PMTUDISC_DO
-# define IPV6_PMTUDISC_DO      2
-#endif
-
-#ifdef HAVE_ERROR_H
-# include <error.h>
-#else
-extern void error(int status, int errnum, const char *format, ...);
-#endif
-
-extern int close_stream(FILE *stream);
-extern void close_stdout(void);
-extern long strtol_or_err(char const *const str, char const *const errmesg,
-                         const long min, const long max);
-extern void iputils_srand(void);
-extern void timespecsub(struct timespec *a, struct timespec *b,
-                       struct timespec *res);
-
-#endif /* IPUTILS_COMMON_H */
index 2943942b7fa69cc32525ff7ba0e44bcb70786f39..76b92e5585ad326e65309caf1ad29c5933d2857d 100644 (file)
@@ -32,7 +32,9 @@
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 
-#include "iputils_common.h"
+#if HAVE_ERROR_H
+#include <error.h>
+#endif
 
 int do_reload = 1;
 
@@ -586,7 +588,6 @@ int main(int argc, char **argv)
        int psize;
        int opt;
 
-       atexit(close_stdout);
        opterr = 0;
        while ((opt = getopt(argc, argv, "aAb:dvoeV")) != EOF) {
                switch (opt) {