]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include: add functions to implement --hyperlink
authorKarel Zak <kzak@redhat.com>
Thu, 28 Nov 2024 12:39:10 +0000 (13:39 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 28 Nov 2024 12:39:10 +0000 (13:39 +0100)
* add xgethosturi() to allocate file://hostname
* add hyperlinkwanted_or_err() to parse --hyperlink={never,always,auto}

Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
include/xalloc.h
lib/strutils.c

index e5ddbcf1bf83f36efd6f67be8ad45098b985b6cd..8ef9c880ea19cab1ae2a8634f52464efe381e16f 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <time.h>
+#include <stdbool.h>
 
 #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)
 
index 1ece82d56458031432bdbc27c28fac20d8e94cb1..e8e427c862720fe10f9f80f2316557301499bc15 100644 (file)
@@ -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
index e3a059145a72d55f1c33964f7e1547f5efe28541..2b54bcce5104a568c513a4a8f189aa9c89a61768 100644 (file)
@@ -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.