From: Karel Zak Date: Thu, 28 Nov 2024 12:39:10 +0000 (+0100) Subject: include: add functions to implement --hyperlink X-Git-Tag: v2.42-start~120^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=28453bcca770f897a6c36706148e105fb7e476ca;p=thirdparty%2Futil-linux.git include: add functions to implement --hyperlink * add xgethosturi() to allocate file://hostname * add hyperlinkwanted_or_err() to parse --hyperlink={never,always,auto} Signed-off-by: Karel Zak --- diff --git a/include/strutils.h b/include/strutils.h index e5ddbcf1b..8ef9c880e 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -14,6 +14,7 @@ #include #include #include +#include #include "c.h" @@ -58,6 +59,8 @@ extern void strtotimespec_or_err(const char *str, struct timespec *ts, const char *errmesg); extern time_t strtotime_or_err(const char *str, const char *errmesg); +extern bool hyperlinkwanted_or_err(const char *mode, const char *errmesg); + extern int isdigit_strend(const char *str, const char **end); #define isdigit_string(_s) isdigit_strend(_s, NULL) diff --git a/include/xalloc.h b/include/xalloc.h index 1ece82d56..e8e427c86 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -193,4 +193,21 @@ char *xgethostname(void) return name; } +static inline +__attribute__((warn_unused_result)) +char *xgethosturi(const char *proto) +{ + char *n = xgethostname(); + char *uri = NULL; + + if (!proto) + proto = "file://"; + if (!n) + return xstrdup(proto); + + xasprintf(&uri, "%s%s", proto, n); + free(n); + return uri; +} + #endif diff --git a/lib/strutils.c b/lib/strutils.c index e3a059145..2b54bcce5 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -524,6 +524,20 @@ time_t strtotime_or_err(const char *str, const char *errmesg) return (time_t) user_input; } +bool hyperlinkwanted_or_err(const char *mode, const char *errmesg) +{ + if (mode && strcmp(mode, "never") == 0) + return false; + + if (mode && strcmp(mode, "always") == 0) + return true; + + if (!mode || strcmp(mode, "auto") == 0) + return isatty(STDOUT_FILENO) ? true : false; + + errx(EXIT_FAILURE, "%s: '%s'", errmesg, mode); +} + /* * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must * be 11 bytes.