]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: add helper function for --annotation option
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Wed, 24 Sep 2025 14:44:57 +0000 (10:44 -0400)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Wed, 24 Sep 2025 14:58:00 +0000 (10:58 -0400)
This function helps evaluates the --annotation option argument
and determines whether annotations are enabled or not.
It is identical to the hyperlinkwanted() routine, and although
that forms some form of redundancy, it is intentionally kept
seperate for potential future changes.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
include/strutils.h
lib/strutils.c

index e4518cdcfe0e62fa8f8fe7d6df43d6213bb9d48d..d034f6b958f15cfb1dc38e5392194cbf7b668974 100644 (file)
@@ -62,6 +62,7 @@ extern void strtotimespec_or_err(const char *str, struct timespec *ts,
 extern time_t strtotime_or_err(const char *str, const char *errmesg);
 
 extern bool hyperlinkwanted(const char *mode);
+extern bool annotationwanted(const char *mode);
 
 extern int isdigit_strend(const char *str, const char **end);
 #define isdigit_string(_s)     isdigit_strend(_s, NULL)
index 1ecc0c398191bbdc759f68cac0827b4187fc301f..e4dabf21208b348bb53619bdb8d4e030d27fb9c5 100644 (file)
@@ -551,6 +551,20 @@ bool hyperlinkwanted(const char *mode)
        errx(EXIT_FAILURE, _("invalid argument of --hyperlink: %s"), mode);
 }
 
+bool annotationwanted(const char *mode)
+{
+       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, _("invalid argument of --annotation: %s"), mode);
+}
+
 /*
  * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must
  * be 11 bytes.