cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0152-libsmartcols-add-basic-tools-necessary-for-new-versi.patch

05ad79
From c465ce9765273e8e1227b192e1917826ac4eaaf7 Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Thu, 31 May 2018 11:13:31 +0200
05ad79
Subject: [PATCH 152/173] libsmartcols: add basic tools necessary for new
05ad79
 version
05ad79
05ad79
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1561350
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 configure.ac          |   3 +
05ad79
 include/carefulputc.h |  96 +++++++++++++++++++++++++++-
05ad79
 include/colors.h      |   5 ++
05ad79
 include/mbsalign.h    |   9 ++-
05ad79
 include/strutils.h    |  13 ++--
05ad79
 include/ttyutils.h    |   1 +
05ad79
 lib/Makemodule.am     |   1 +
05ad79
 lib/color-names.c     |  57 +++++++++++++++++
05ad79
 lib/mbsalign.c        | 172 +++++++++++++++++++++++++++++++++++++++++---------
05ad79
 lib/ttyutils.c        |  52 +++++++++++++++
05ad79
 libfdisk/src/ask.c    |   4 +-
05ad79
 11 files changed, 373 insertions(+), 40 deletions(-)
05ad79
 create mode 100644 lib/color-names.c
05ad79
05ad79
diff --git a/configure.ac b/configure.ac
05ad79
index d561e01d0..8cf317dc0 100644
05ad79
--- a/configure.ac
05ad79
+++ b/configure.ac
05ad79
@@ -133,6 +133,9 @@ AC_SUBST([BSD_WARN_CFLAGS])
05ad79
 dnl libtool-2
05ad79
 LT_INIT
05ad79
 
05ad79
+dnl check supported linker flags
05ad79
+AX_CHECK_VSCRIPT
05ad79
+
05ad79
 m4_ifndef([PKG_PROG_PKG_CONFIG],
05ad79
   [m4_fatal([Could not locate the pkg-config autoconf
05ad79
     macros. These are usually located in /usr/share/aclocal/pkg.m4.
05ad79
diff --git a/include/carefulputc.h b/include/carefulputc.h
05ad79
index a54498cfd..613d94c1e 100644
05ad79
--- a/include/carefulputc.h
05ad79
+++ b/include/carefulputc.h
05ad79
@@ -26,7 +26,87 @@ static inline int carefulputc(int c, FILE *fp) {
05ad79
 	return (ret < 0) ? EOF : 0;
05ad79
 }
05ad79
 
05ad79
-static inline void fputs_quoted(const char *data, FILE *out)
05ad79
+/*
05ad79
+ * Backported for RHEL7.6 libsmartcols
05ad79
+ */
05ad79
+
05ad79
+/*
05ad79
+ * Requirements enumerated via testing (V8, Firefox, IE11):
05ad79
+ *
05ad79
+ * var charsToEscape = [];
05ad79
+ * for (var i = 0; i < 65535; i += 1) {
05ad79
+ *	try {
05ad79
+ *		JSON.parse('{"sample": "' + String.fromCodePoint(i) + '"}');
05ad79
+ *	} catch (e) {
05ad79
+ *		charsToEscape.push(i);
05ad79
+ *	}
05ad79
+ * }
05ad79
+ */
05ad79
+static inline void fputs_quoted_case_json(const char *data, FILE *out, int dir)
05ad79
+{
05ad79
+	const char *p;
05ad79
+
05ad79
+	fputc('"', out);
05ad79
+	for (p = data; p && *p; p++) {
05ad79
+
05ad79
+		const unsigned char c = (unsigned char) *p;
05ad79
+
05ad79
+		/* From http://www.json.org
05ad79
+		 *
05ad79
+		 * The double-quote and backslashes would break out a string or
05ad79
+		 * init an escape sequence if not escaped.
05ad79
+		 *
05ad79
+		 * Note that single-quotes and forward slashes, while they're
05ad79
+		 * in the JSON spec, don't break double-quoted strings.
05ad79
+		 */
05ad79
+		if (c == '"' || c == '\\') {
05ad79
+			fputc('\\', out);
05ad79
+			fputc(c, out);
05ad79
+			continue;
05ad79
+		}
05ad79
+
05ad79
+		/* All non-control characters OK; do the case swap as required. */
05ad79
+		if (c >= 0x20) {
05ad79
+			fputc(dir ==  1 ? toupper(c) :
05ad79
+			      dir == -1 ? tolower(c) : *p, out);
05ad79
+			continue;
05ad79
+		}
05ad79
+
05ad79
+		/* In addition, all chars under ' ' break Node's/V8/Chrome's, and
05ad79
+		 * Firefox's JSON.parse function
05ad79
+		 */
05ad79
+		switch (c) {
05ad79
+			/* Handle short-hand cases to reduce output size.  C
05ad79
+			 * has most of the same stuff here, so if there's an
05ad79
+			 * "Escape for C" function somewhere in the STL, we
05ad79
+			 * should probably be using it.
05ad79
+			 */
05ad79
+			case '\b':
05ad79
+				fputs("\\b", out);
05ad79
+				break;
05ad79
+			case '\t':
05ad79
+				fputs("\\t", out);
05ad79
+				break;
05ad79
+			case '\n':
05ad79
+				fputs("\\n", out);
05ad79
+				break;
05ad79
+			case '\f':
05ad79
+				fputs("\\f", out);
05ad79
+				break;
05ad79
+			case '\r':
05ad79
+				fputs("\\r", out);
05ad79
+				break;
05ad79
+			default:
05ad79
+				/* Other assorted control characters */
05ad79
+				fprintf(out, "\\u00%02x", c);
05ad79
+				break;
05ad79
+		}
05ad79
+	}
05ad79
+	fputc('"', out);
05ad79
+}
05ad79
+
05ad79
+
05ad79
+static inline void fputs_quoted_case(const char *data, FILE *out, int dir)
05ad79
 {
05ad79
 	const char *p;
05ad79
 
05ad79
@@ -34,16 +114,28 @@ static inline void fputs_quoted(const char *data, FILE *out)
05ad79
 	for (p = data; p && *p; p++) {
05ad79
 		if ((unsigned char) *p == 0x22 ||		/* " */
05ad79
 		    (unsigned char) *p == 0x5c ||		/* \ */
05ad79
+		    (unsigned char) *p == 0x60 ||		/* ` */
05ad79
+		    (unsigned char) *p == 0x24 ||		/* $ */
05ad79
 		    !isprint((unsigned char) *p) ||
05ad79
 		    iscntrl((unsigned char) *p)) {
05ad79
 
05ad79
 			fprintf(out, "\\x%02x", (unsigned char) *p);
05ad79
 		} else
05ad79
-			fputc(*p, out);
05ad79
+			fputc(dir ==  1 ? toupper(*p) :
05ad79
+			      dir == -1 ? tolower(*p) :
05ad79
+			      *p, out);
05ad79
 	}
05ad79
 	fputc('"', out);
05ad79
 }
05ad79
 
05ad79
+#define fputs_quoted(_d, _o)		fputs_quoted_case(_d, _o, 0)
05ad79
+#define fputs_quoted_upper(_d, _o)	fputs_quoted_case(_d, _o, 1)
05ad79
+#define fputs_quoted_lower(_d, _o)	fputs_quoted_case(_d, _o, -1)
05ad79
+
05ad79
+#define fputs_quoted_json(_d, _o)       fputs_quoted_case_json(_d, _o, 0)
05ad79
+#define fputs_quoted_json_upper(_d, _o) fputs_quoted_case_json(_d, _o, 1)
05ad79
+#define fputs_quoted_json_lower(_d, _o) fputs_quoted_case_json(_d, _o, -1)
05ad79
+
05ad79
 static inline void fputs_nonblank(const char *data, FILE *out)
05ad79
 {
05ad79
 	const char *p;
05ad79
diff --git a/include/colors.h b/include/colors.h
05ad79
index dd77bf6df..39c0edf46 100644
05ad79
--- a/include/colors.h
05ad79
+++ b/include/colors.h
05ad79
@@ -38,6 +38,11 @@
05ad79
 
05ad79
 #define UL_COLOR_WHITE		"\033[1;37m"
05ad79
 
05ad79
+/* maximal length of human readable name of ESC seq. */
05ad79
+#define UL_COLORNAME_MAXSZ      32
05ad79
+
05ad79
+extern const char *color_sequence_from_colorname(const char *str);
05ad79
+
05ad79
 /* Initialize the global variable OUT_IS_TERM */
05ad79
 extern int colors_init(void);
05ad79
 
05ad79
diff --git a/include/mbsalign.h b/include/mbsalign.h
05ad79
index 5eaf606e5..0c28e6f69 100644
05ad79
--- a/include/mbsalign.h
05ad79
+++ b/include/mbsalign.h
05ad79
@@ -46,11 +46,18 @@ extern size_t mbsalign (const char *src, char *dest,
05ad79
 			size_t dest_size,  size_t *width,
05ad79
 			mbs_align_t align, int flags);
05ad79
 
05ad79
+extern size_t mbsalign_with_padding (const char *src, char *dest, size_t dest_size,
05ad79
+	               size_t *width, mbs_align_t align, int flags,
05ad79
+		       int padchar);
05ad79
+
05ad79
 extern size_t mbs_safe_nwidth(const char *buf, size_t bufsz, size_t *sz);
05ad79
 extern size_t mbs_safe_width(const char *s);
05ad79
 
05ad79
 extern char *mbs_safe_encode(const char *s, size_t *width);
05ad79
-extern char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf);
05ad79
+extern char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf, const char *safechars);
05ad79
 extern size_t mbs_safe_encode_size(size_t bytes);
05ad79
 
05ad79
+extern char *mbs_invalid_encode(const char *s, size_t *width);
05ad79
+extern char *mbs_invalid_encode_to_buffer(const char *s, size_t *width, char *buf);
05ad79
+
05ad79
 #endif /* UTIL_LINUX_MBSALIGN_H */
05ad79
diff --git a/include/strutils.h b/include/strutils.h
05ad79
index 1f028e4ed..822fb7d49 100644
05ad79
--- a/include/strutils.h
05ad79
+++ b/include/strutils.h
05ad79
@@ -6,6 +6,7 @@
05ad79
 #include <string.h>
05ad79
 #include <sys/types.h>
05ad79
 #include <stdio.h>
05ad79
+#include <errno.h>
05ad79
 
05ad79
 /* default strtoxx_or_err() exit code */
05ad79
 #ifndef STRTOXX_EXIT_CODE
05ad79
@@ -57,20 +58,24 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
05ad79
 	dest[n-1] = 0;
05ad79
 }
05ad79
 
05ad79
-static inline char *strdup_to_offset(void *stru, size_t offset, const char *str)
05ad79
+static inline int strdup_to_offset(void *stru, size_t offset, const char *str)
05ad79
 {
05ad79
 	char *n = NULL;
05ad79
-	char **o = (char **) ((char *) stru + offset);
05ad79
+	char **o;
05ad79
 
05ad79
+	if (!stru)
05ad79
+		return -EINVAL;
05ad79
+
05ad79
+	o = (char **) ((char *) stru + offset);
05ad79
 	if (str) {
05ad79
 		n = strdup(str);
05ad79
 		if (!n)
05ad79
-			return NULL;
05ad79
+			return -ENOMEM;
05ad79
 	}
05ad79
 
05ad79
 	free(*o);
05ad79
 	*o = n;
05ad79
-	return n;
05ad79
+	return 0;
05ad79
 }
05ad79
 
05ad79
 #define strdup_to_struct_member(_s, _m, _str) \
05ad79
diff --git a/include/ttyutils.h b/include/ttyutils.h
05ad79
index 13495ba96..47fe34472 100644
05ad79
--- a/include/ttyutils.h
05ad79
+++ b/include/ttyutils.h
05ad79
@@ -47,6 +47,7 @@ struct chardata {
05ad79
 	        (ptr)->capslock = 0;         \
05ad79
 	} while (0)
05ad79
 
05ad79
+extern int get_terminal_dimension(int *cols, int *lines);
05ad79
 extern int get_terminal_width(void);
05ad79
 extern int get_terminal_name(int fd, const char **path, const char **name,
05ad79
 			     const char **number);
05ad79
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
05ad79
index acae27afb..714233c40 100644
05ad79
--- a/lib/Makemodule.am
05ad79
+++ b/lib/Makemodule.am
05ad79
@@ -6,6 +6,7 @@ libcommon_la_SOURCES = \
05ad79
 	lib/blkdev.c \
05ad79
 	lib/canonicalize.c \
05ad79
 	lib/colors.c \
05ad79
+	lib/color-names.c \
05ad79
 	lib/crc32.c \
05ad79
 	lib/env.c \
05ad79
 	lib/idcache.c \
05ad79
diff --git a/lib/color-names.c b/lib/color-names.c
05ad79
new file mode 100644
05ad79
index 000000000..cf37670a9
05ad79
--- /dev/null
05ad79
+++ b/lib/color-names.c
05ad79
@@ -0,0 +1,57 @@
05ad79
+
05ad79
+#include "c.h"
05ad79
+#include "colors.h"
05ad79
+
05ad79
+struct ul_color_name {
05ad79
+	const char *name;
05ad79
+	const char *seq;
05ad79
+};
05ad79
+
05ad79
+/*
05ad79
+ * qsort/bsearch buddy
05ad79
+ */
05ad79
+static int cmp_color_name(const void *a0, const void *b0)
05ad79
+{
05ad79
+	struct ul_color_name	*a = (struct ul_color_name *) a0,
05ad79
+				*b = (struct ul_color_name *) b0;
05ad79
+	return strcmp(a->name, b->name);
05ad79
+}
05ad79
+
05ad79
+/*
05ad79
+ * Maintains human readable color names
05ad79
+ */
05ad79
+const char *color_sequence_from_colorname(const char *str)
05ad79
+{
05ad79
+	static const struct ul_color_name basic_schemes[] = {
05ad79
+		{ "black",	UL_COLOR_BLACK           },
05ad79
+		{ "blink",      UL_COLOR_BLINK           },
05ad79
+		{ "blue",	UL_COLOR_BLUE            },
05ad79
+		{ "bold",       UL_COLOR_BOLD		 },
05ad79
+		{ "brown",	UL_COLOR_BROWN           },
05ad79
+		{ "cyan",	UL_COLOR_CYAN            },
05ad79
+		{ "darkgray",	UL_COLOR_DARK_GRAY       },
05ad79
+		{ "gray",	UL_COLOR_GRAY            },
05ad79
+		{ "green",	UL_COLOR_GREEN           },
05ad79
+		{ "halfbright", UL_COLOR_HALFBRIGHT	 },
05ad79
+		{ "lightblue",	UL_COLOR_BOLD_BLUE       },
05ad79
+		{ "lightcyan",	UL_COLOR_BOLD_CYAN       },
05ad79
+		{ "lightgray,",	UL_COLOR_GRAY            },
05ad79
+		{ "lightgreen", UL_COLOR_BOLD_GREEN      },
05ad79
+		{ "lightmagenta", UL_COLOR_BOLD_MAGENTA  },
05ad79
+		{ "lightred",	UL_COLOR_BOLD_RED        },
05ad79
+		{ "magenta",	UL_COLOR_MAGENTA         },
05ad79
+		{ "red",	UL_COLOR_RED             },
05ad79
+		{ "reset",      UL_COLOR_RESET,          },
05ad79
+		{ "reverse",    UL_COLOR_REVERSE         },
05ad79
+		{ "yellow",	UL_COLOR_BOLD_YELLOW     },
05ad79
+	};
05ad79
+	struct ul_color_name key = { .name = (char *) str }, *res;
05ad79
+
05ad79
+	if (!str)
05ad79
+		return NULL;
05ad79
+
05ad79
+	res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
05ad79
+				sizeof(struct ul_color_name),
05ad79
+				cmp_color_name);
05ad79
+	return res ? res->seq : NULL;
05ad79
+}
05ad79
diff --git a/lib/mbsalign.c b/lib/mbsalign.c
05ad79
index b307d19f7..8fdab9ee9 100644
05ad79
--- a/lib/mbsalign.c
05ad79
+++ b/lib/mbsalign.c
05ad79
@@ -27,9 +27,9 @@
05ad79
 
05ad79
 #include "c.h"
05ad79
 #include "mbsalign.h"
05ad79
+#include "strutils.h"
05ad79
 #include "widechar.h"
05ad79
 
05ad79
-#ifdef HAVE_WIDECHAR
05ad79
 /* Replace non printable chars.
05ad79
    Note \t and \n etc. are non printable.
05ad79
    Return 1 if replacement made, 0 otherwise.  */
05ad79
@@ -43,17 +43,19 @@
05ad79
  */
05ad79
 size_t mbs_safe_nwidth(const char *buf, size_t bufsz, size_t *sz)
05ad79
 {
05ad79
-	mbstate_t st;
05ad79
 	const char *p = buf, *last = buf;
05ad79
 	size_t width = 0, bytes = 0;
05ad79
 
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
+	mbstate_t st;
05ad79
 	memset(&st, 0, sizeof(st));
05ad79
-
05ad79
+#endif
05ad79
 	if (p && *p && bufsz)
05ad79
 		last = p + (bufsz - 1);
05ad79
 
05ad79
 	while (p && *p && p <= last) {
05ad79
-		if (iscntrl((unsigned char) *p)) {
05ad79
+		if ((p < last && *p == '\\' && *(p + 1) == 'x')
05ad79
+		    || iscntrl((unsigned char) *p)) {
05ad79
 			width += 4, bytes += 4;		/* *p encoded to \x?? */
05ad79
 			p++;
05ad79
 		}
05ad79
@@ -106,28 +108,36 @@ size_t mbs_safe_width(const char *s)
05ad79
 
05ad79
 /*
05ad79
  * Copy @s to @buf and replace control and non-printable chars with
05ad79
- * \x?? hex sequence. The @width returns number of cells.
05ad79
+ * \x?? hex sequence. The @width returns number of cells. The @safechars
05ad79
+ * are not encoded.
05ad79
  *
05ad79
  * The @buf has to be big enough to store mbs_safe_encode_size(strlen(s)))
05ad79
  * bytes.
05ad79
  */
05ad79
-char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf)
05ad79
+char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf, const char *safechars)
05ad79
 {
05ad79
-	mbstate_t st;
05ad79
 	const char *p = s;
05ad79
 	char *r;
05ad79
 	size_t sz = s ? strlen(s) : 0;
05ad79
 
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
+	mbstate_t st;
05ad79
+	memset(&st, 0, sizeof(st));
05ad79
+#endif
05ad79
 	if (!sz || !buf)
05ad79
 		return NULL;
05ad79
 
05ad79
-	memset(&st, 0, sizeof(st));
05ad79
-
05ad79
 	r = buf;
05ad79
 	*width = 0;
05ad79
 
05ad79
 	while (p && *p) {
05ad79
-		if (iscntrl((unsigned char) *p)) {
05ad79
+		if (safechars && strchr(safechars, *p)) {
05ad79
+			*r++ = *p++;
05ad79
+			continue;
05ad79
+		}
05ad79
+
05ad79
+		if ((*p == '\\' && *(p + 1) == 'x')
05ad79
+		    || iscntrl((unsigned char) *p)) {
05ad79
 			sprintf(r, "\\x%02x", (unsigned char) *p);
05ad79
 			r += 4;
05ad79
 			*width += 4;
05ad79
@@ -152,13 +162,13 @@ char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf)
05ad79
 					r += 4;
05ad79
 					*width += 4;
05ad79
 				} else {
05ad79
-					width++;
05ad79
+					(*width)++;
05ad79
 					*r++ = *p;
05ad79
 				}
05ad79
 			} else if (!iswprint(wc)) {
05ad79
 				size_t i;
05ad79
 				for (i = 0; i < len; i++) {
05ad79
-					sprintf(r, "\\x%02x", (unsigned char) *p);
05ad79
+					sprintf(r, "\\x%02x", (unsigned char) p[i]);
05ad79
 					r += 4;
05ad79
 					*width += 4;
05ad79
 				}
05ad79
@@ -177,13 +187,76 @@ char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf)
05ad79
 			*width += 4;
05ad79
 		} else {
05ad79
 			*r++ = *p++;
05ad79
-			*width++;
05ad79
+			(*width)++;
05ad79
 		}
05ad79
 #endif
05ad79
 	}
05ad79
 
05ad79
 	*r = '\0';
05ad79
+	return buf;
05ad79
+}
05ad79
 
05ad79
+/*
05ad79
+ * Copy @s to @buf and replace broken sequences to \x?? hex sequence. The
05ad79
+ * @width returns number of cells. The @safechars are not encoded.
05ad79
+ *
05ad79
+ * The @buf has to be big enough to store mbs_safe_encode_size(strlen(s)))
05ad79
+ * bytes.
05ad79
+ */
05ad79
+char *mbs_invalid_encode_to_buffer(const char *s, size_t *width, char *buf)
05ad79
+{
05ad79
+	const char *p = s;
05ad79
+	char *r;
05ad79
+	size_t sz = s ? strlen(s) : 0;
05ad79
+
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
+	mbstate_t st;
05ad79
+	memset(&st, 0, sizeof(st));
05ad79
+#endif
05ad79
+	if (!sz || !buf)
05ad79
+		return NULL;
05ad79
+
05ad79
+	r = buf;
05ad79
+	*width = 0;
05ad79
+
05ad79
+	while (p && *p) {
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
+		wchar_t wc;
05ad79
+		size_t len = mbrtowc(&wc, p, MB_CUR_MAX, &st);
05ad79
+#else
05ad79
+		size_t len = 1;
05ad79
+#endif
05ad79
+
05ad79
+		if (len == 0)
05ad79
+			break;		/* end of string */
05ad79
+
05ad79
+		if (len == (size_t) -1 || len == (size_t) -2) {
05ad79
+			len = 1;
05ad79
+			/*
05ad79
+			 * Not valid multibyte sequence -- maybe it's
05ad79
+			 * printable char according to the current locales.
05ad79
+			 */
05ad79
+			if (!isprint((unsigned char) *p)) {
05ad79
+				sprintf(r, "\\x%02x", (unsigned char) *p);
05ad79
+				r += 4;
05ad79
+				*width += 4;
05ad79
+			} else {
05ad79
+				(*width)++;
05ad79
+				*r++ = *p;
05ad79
+			}
05ad79
+		} else if (*p == '\\' && *(p + 1) == 'x') {
05ad79
+			sprintf(r, "\\x%02x", (unsigned char) *p);
05ad79
+			r += 4;
05ad79
+			*width += 4;
05ad79
+		} else {
05ad79
+			memcpy(r, p, len);
05ad79
+			r += len;
05ad79
+			*width += wcwidth(wc);
05ad79
+		}
05ad79
+		p += len;
05ad79
+	}
05ad79
+
05ad79
+	*r = '\0';
05ad79
 	return buf;
05ad79
 }
05ad79
 
05ad79
@@ -199,17 +272,39 @@ size_t mbs_safe_encode_size(size_t bytes)
05ad79
 char *mbs_safe_encode(const char *s, size_t *width)
05ad79
 {
05ad79
 	size_t sz = s ? strlen(s) : 0;
05ad79
-	char *buf;
05ad79
+	char *buf, *ret = NULL;
05ad79
 
05ad79
 	if (!sz)
05ad79
 		return NULL;
05ad79
 	buf = malloc(mbs_safe_encode_size(sz));
05ad79
-	if (!buf)
05ad79
-		return NULL;
05ad79
+	if (buf)
05ad79
+		ret = mbs_safe_encode_to_buffer(s, width, buf, NULL);
05ad79
+	if (!ret)
05ad79
+		free(buf);
05ad79
+	return ret;
05ad79
+}
05ad79
 
05ad79
-	return mbs_safe_encode_to_buffer(s, width, buf);
05ad79
+/*
05ad79
+ * Returns allocated string where all broken widechars chars are
05ad79
+ * replaced with \x?? hex sequence.
05ad79
+ */
05ad79
+char *mbs_invalid_encode(const char *s, size_t *width)
05ad79
+{
05ad79
+	size_t sz = s ? strlen(s) : 0;
05ad79
+	char *buf, *ret = NULL;
05ad79
+
05ad79
+	if (!sz)
05ad79
+		return NULL;
05ad79
+	buf = malloc(mbs_safe_encode_size(sz));
05ad79
+	if (buf)
05ad79
+		ret = mbs_invalid_encode_to_buffer(s, width, buf);
05ad79
+	if (!ret)
05ad79
+		free(buf);
05ad79
+	return ret;
05ad79
 }
05ad79
 
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
+
05ad79
 static bool
05ad79
 wc_ensure_printable (wchar_t *wchars)
05ad79
 {
05ad79
@@ -246,6 +341,7 @@ wc_truncate (wchar_t *wc, size_t width)
05ad79
         }
05ad79
       if (cells + next_cells > width)
05ad79
         break;
05ad79
+
05ad79
       cells += next_cells;
05ad79
       wc++;
05ad79
     }
05ad79
@@ -273,7 +369,7 @@ rpl_wcswidth (const wchar_t *s, size_t n)
05ad79
 
05ad79
   return ret;
05ad79
 }
05ad79
-#endif
05ad79
+#endif /* HAVE_WIDECHAR */
05ad79
 
05ad79
 /* Truncate multi-byte string to @width and returns number of
05ad79
  * bytes of the new string @str, and in @width returns number
05ad79
@@ -290,7 +386,7 @@ mbs_truncate(char *str, size_t *width)
05ad79
 	if (sz == (ssize_t) -1)
05ad79
 		goto done;
05ad79
 
05ad79
-	wcs = malloc((sz + 1) * sizeof(wchar_t));
05ad79
+	wcs = calloc(1, (sz + 1) * sizeof(wchar_t));
05ad79
 	if (!wcs)
05ad79
 		goto done;
05ad79
 
05ad79
@@ -301,7 +397,7 @@ mbs_truncate(char *str, size_t *width)
05ad79
 done:
05ad79
 	free(wcs);
05ad79
 #else
05ad79
-	if (*width < bytes)
05ad79
+	if (bytes >= 0 && *width < (size_t) bytes)
05ad79
 		bytes = *width;
05ad79
 #endif
05ad79
 	if (bytes >= 0)
05ad79
@@ -315,16 +411,23 @@ done:
05ad79
    A pointer to the terminating NUL is returned.  */
05ad79
 
05ad79
 static char*
05ad79
-mbs_align_pad (char *dest, const char* dest_end, size_t n_spaces)
05ad79
+mbs_align_pad (char *dest, const char* dest_end, size_t n_spaces, int padchar)
05ad79
 {
05ad79
   /* FIXME: Should we pad with "figure space" (\u2007)
05ad79
      if non ascii data present?  */
05ad79
-  while (n_spaces-- && (dest < dest_end))
05ad79
-    *dest++ = ' ';
05ad79
+  for (/* nothing */; n_spaces && (dest < dest_end); n_spaces--)
05ad79
+    *dest++ = padchar;
05ad79
   *dest = '\0';
05ad79
   return dest;
05ad79
 }
05ad79
 
05ad79
+size_t
05ad79
+mbsalign (const char *src, char *dest, size_t dest_size,
05ad79
+          size_t *width, mbs_align_t align, int flags)
05ad79
+{
05ad79
+	return mbsalign_with_padding(src, dest, dest_size, width, align, flags, ' ');
05ad79
+}
05ad79
+
05ad79
 /* Align a string, SRC, in a field of *WIDTH columns, handling multi-byte
05ad79
    characters; write the result into the DEST_SIZE-byte buffer, DEST.
05ad79
    ALIGNMENT specifies whether to left- or right-justify or to center.
05ad79
@@ -339,8 +442,14 @@ mbs_align_pad (char *dest, const char* dest_end, size_t n_spaces)
05ad79
    Update *WIDTH to indicate how many columns were used before padding.  */
05ad79
 
05ad79
 size_t
05ad79
-mbsalign (const char *src, char *dest, size_t dest_size,
05ad79
-          size_t *width, mbs_align_t align, int flags)
05ad79
+mbsalign_with_padding (const char *src, char *dest, size_t dest_size,
05ad79
+	               size_t *width, mbs_align_t align,
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
+		       int flags,
05ad79
+#else
05ad79
+		       int flags __attribute__((__unused__)),
05ad79
+#endif
05ad79
+		       int padchar)
05ad79
 {
05ad79
   size_t ret = -1;
05ad79
   size_t src_size = strlen (src) + 1;
05ad79
@@ -350,10 +459,11 @@ mbsalign (const char *src, char *dest, size_t dest_size,
05ad79
   size_t n_cols = src_size - 1;
05ad79
   size_t n_used_bytes = n_cols; /* Not including NUL */
05ad79
   size_t n_spaces = 0, space_left;
05ad79
+
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
   bool conversion = false;
05ad79
   bool wc_enabled = false;
05ad79
 
05ad79
-#ifdef HAVE_WIDECHAR
05ad79
   /* In multi-byte locales convert to wide characters
05ad79
      to allow easy truncation. Also determine number
05ad79
      of screen columns used.  */
05ad79
@@ -407,9 +517,9 @@ mbsalign (const char *src, char *dest, size_t dest_size,
05ad79
         n_cols = wc_truncate (str_wc, *width);
05ad79
         n_used_bytes = wcstombs (newstr, str_wc, src_size);
05ad79
     }
05ad79
-#endif
05ad79
 
05ad79
 mbsalign_unibyte:
05ad79
+#endif
05ad79
 
05ad79
   if (n_cols > *width) /* Unibyte truncation required.  */
05ad79
     {
05ad79
@@ -451,14 +561,14 @@ mbsalign_unibyte:
05ad79
 	  abort();
05ad79
         }
05ad79
 
05ad79
-      dest = mbs_align_pad (dest, dest_end, start_spaces);
05ad79
+      dest = mbs_align_pad (dest, dest_end, start_spaces, padchar);
05ad79
       space_left = dest_end - dest;
05ad79
       dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));
05ad79
-      mbs_align_pad (dest, dest_end, end_spaces);
05ad79
+      mbs_align_pad (dest, dest_end, end_spaces, padchar);
05ad79
     }
05ad79
-
05ad79
+#ifdef HAVE_WIDECHAR
05ad79
 mbsalign_cleanup:
05ad79
-
05ad79
+#endif
05ad79
   free (str_wc);
05ad79
   free (newstr);
05ad79
 
05ad79
diff --git a/lib/ttyutils.c b/lib/ttyutils.c
05ad79
index ea551e26c..91497e763 100644
05ad79
--- a/lib/ttyutils.c
05ad79
+++ b/lib/ttyutils.c
05ad79
@@ -9,6 +9,58 @@
05ad79
 #include "c.h"
05ad79
 #include "ttyutils.h"
05ad79
 
05ad79
+/*
05ad79
+ * Backported for RHEL7.6 libsmartcols
05ad79
+ */
05ad79
+static int get_env_int(const char *name)
05ad79
+{
05ad79
+	const char *cp = getenv(name);
05ad79
+
05ad79
+	if (cp) {
05ad79
+		char *end = NULL;
05ad79
+		long x;
05ad79
+
05ad79
+		errno = 0;
05ad79
+		x = strtol(cp, &end, 10);
05ad79
+
05ad79
+		if (errno == 0 && end && *end == '\0' && end > cp &&
05ad79
+		    x > 0 && x <= INT_MAX)
05ad79
+			return x;
05ad79
+	}
05ad79
+
05ad79
+	return -1;
05ad79
+}
05ad79
+
05ad79
+int get_terminal_dimension(int *cols, int *lines)
05ad79
+{
05ad79
+	int c = 0, l = 0;
05ad79
+
05ad79
+#if defined(TIOCGWINSZ)
05ad79
+	struct winsize	w_win;
05ad79
+	if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &w_win) == 0) {
05ad79
+		c = w_win.ws_col;
05ad79
+		l = w_win.ws_row;
05ad79
+	}
05ad79
+#elif defined(TIOCGSIZE)
05ad79
+	struct ttysize	t_win;
05ad79
+	if (ioctl (STDOUT_FILENO, TIOCGSIZE, &t_win) == 0) {
05ad79
+		c = t_win.ts_cols;
05ad79
+		l = t_win.ts_lines;
05ad79
+	}
05ad79
+#endif
05ad79
+
05ad79
+	if (cols && c <= 0)
05ad79
+		c = get_env_int("COLUMNS");
05ad79
+	if (lines && l <= 0)
05ad79
+		l = get_env_int("LINES");
05ad79
+
05ad79
+	if (cols)
05ad79
+		*cols = c;
05ad79
+	if (lines)
05ad79
+		*lines = l;
05ad79
+	return 0;
05ad79
+}
05ad79
+
05ad79
 int get_terminal_width(void)
05ad79
 {
05ad79
 #ifdef TIOCGSIZE
05ad79
diff --git a/libfdisk/src/ask.c b/libfdisk/src/ask.c
05ad79
index cdb4d0124..a10f3dc82 100644
05ad79
--- a/libfdisk/src/ask.c
05ad79
+++ b/libfdisk/src/ask.c
05ad79
@@ -42,7 +42,7 @@ const char *fdisk_ask_get_query(struct fdisk_ask *ask)
05ad79
 int fdisk_ask_set_query(struct fdisk_ask *ask, const char *str)
05ad79
 {
05ad79
 	assert(ask);
05ad79
-	return !strdup_to_struct_member(ask, query, str) ? -ENOMEM : 0;
05ad79
+	return strdup_to_struct_member(ask, query, str);
05ad79
 }
05ad79
 
05ad79
 int fdisk_ask_get_type(struct fdisk_ask *ask)
05ad79
@@ -90,7 +90,7 @@ const char *fdisk_ask_number_get_range(struct fdisk_ask *ask)
05ad79
 int fdisk_ask_number_set_range(struct fdisk_ask *ask, const char *range)
05ad79
 {
05ad79
 	assert(ask);
05ad79
-	return !strdup_to_struct_member(ask, data.num.range, range) ? -ENOMEM : 0;
05ad79
+	return strdup_to_struct_member(ask, data.num.range, range);
05ad79
 }
05ad79
 
05ad79
 uint64_t fdisk_ask_number_get_default(struct fdisk_ask *ask)
05ad79
-- 
05ad79
2.14.4
05ad79