Blame SOURCES/bsd-finger-wide-char-support5.patch

c7b137
--- bsd-finger-0.17/finger/finger.c.widechar	2005-12-15 09:14:18.000000000 +0100
c7b137
+++ bsd-finger-0.17/finger/finger.c	2005-12-15 09:14:18.000000000 +0100
c7b137
@@ -77,6 +77,7 @@
c7b137
 #include <getopt.h>
c7b137
 #include <signal.h>
c7b137
 #include <errno.h>
c7b137
+#include <locale.h>
c7b137
 #include "finger.h"
c7b137
 #ifdef _USAGI
c7b137
 #include "version.h"
c7b137
@@ -211,6 +212,9 @@
c7b137
	struct utmp *uptr;
c7b137
	char name[UT_NAMESIZE + 1];
c7b137
c7b137
+	if (setlocale (LC_ALL, "") != NULL)
c7b137
+		set_haslocale();
c7b137
+
c7b137
	name[UT_NAMESIZE] = '\0';
c7b137
c7b137
	/*
c7b137
@@ -286,6 +287,9 @@
c7b137
 	struct utmp *uptr;
c7b137
 	int dolocal, *used;
c7b137
 
c7b137
+	if (setlocale (LC_ALL, "") != NULL)
c7b137
+		set_haslocale();
c7b137
+
c7b137
 	used = calloc(argc, sizeof(int));
c7b137
 	if (!used) {
c7b137
 		eprintf("finger: out of space.\n");
c7b137
--- bsd-finger-0.17/finger/finger.h.widechar	2005-12-15 09:14:17.000000000 +0100
c7b137
+++ bsd-finger-0.17/finger/finger.h	2005-12-15 09:14:18.000000000 +0100
c7b137
@@ -117,3 +117,7 @@
c7b137
 /* terminal inquiries */
c7b137
 int is8bit(void);
c7b137
 int getscreenwidth(void);
c7b137
+
c7b137
+/* locale support */
c7b137
+void set_haslocale(void);
c7b137
+
c7b137
--- bsd-finger-0.17/finger/display.c.widechar	1999-09-29 00:53:58.000000000 +0200
c7b137
+++ bsd-finger-0.17/finger/display.c	2005-12-15 10:05:40.000000000 +0100
c7b137
@@ -40,8 +40,19 @@
c7b137
 #include <stdlib.h>
c7b137
 #include <string.h>
c7b137
 #include <stdarg.h>
c7b137
+#include <inttypes.h>
c7b137
 #include "finger.h"
c7b137
 
c7b137
+#define HAVE_WCHAR_H 1
c7b137
+#define HAVE_MBRTOWC 1
c7b137
+#define HAVE_WCWIDTH 1
c7b137
+
c7b137
+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
c7b137
+#include <wchar.h>
c7b137
+#include <wctype.h>
c7b137
+#include <assert.h>
c7b137
+#endif
c7b137
+
c7b137
 int
c7b137
 getscreenwidth(void)
c7b137
 {
c7b137
@@ -147,9 +158,105 @@
c7b137
 	fxputc(stdout, ch);
c7b137
 }
c7b137
 
c7b137
+static int has_locale = 0;
c7b137
+
c7b137
+void
c7b137
+set_haslocale (void)
c7b137
+{
c7b137
+	has_locale = 1;
c7b137
+}
c7b137
+
c7b137
+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
c7b137
+static int verifymultibyte(const char *buf) {
c7b137
+	mbstate_t state;
c7b137
+	wchar_t nextchar;
c7b137
+	size_t bytesconsumed;
c7b137
+	char *eop, *op;
c7b137
+	(void)memset(&state, 0, sizeof(mbstate_t));
c7b137
+
c7b137
+	eop = (char *) (buf + strlen(buf));
c7b137
+	op = (char *) buf;
c7b137
+	while (op < eop) {
c7b137
+		bytesconsumed = mbrtowc(&nextchar, op, eop - op, &state);
c7b137
+		if (bytesconsumed == (size_t)(-1) ||
c7b137
+		    bytesconsumed == (size_t)(-2)) {
c7b137
+			return 0;
c7b137
+		}
c7b137
+		op += bytesconsumed;
c7b137
+	}
c7b137
+
c7b137
+	return 1;
c7b137
+}
c7b137
+
c7b137
+#define OCTALIFY(n, o) \
c7b137
+	*(n)++ = '\\', \
c7b137
+	*(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
c7b137
+	*(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
c7b137
+	*(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
c7b137
+	(o)++
c7b137
+
c7b137
+#endif
c7b137
+
c7b137
 static void fxputs(FILE *f, const char *buf) {
c7b137
-	int i;
c7b137
-	for (i=0; buf[i]; i++) fxputc(f, buf[i]);
c7b137
+	int widechars;
c7b137
+
c7b137
+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
c7b137
+	if (has_locale)
c7b137
+		widechars = verifymultibyte (buf);
c7b137
+	else
c7b137
+		widechars = 0;
c7b137
+#else
c7b137
+	widechars = 0;
c7b137
+#endif
c7b137
+
c7b137
+	/* on 7-bit terminals, without wide-chars support, or string
c7b137
+	 * isn't parseable, print char * by char */
c7b137
+	if (!is8bit() || !widechars) {
c7b137
+		unsigned int i;
c7b137
+		char ch;
c7b137
+		for (i = 0; i < strlen (buf); i++) {
c7b137
+			ch = buf[i];
c7b137
+			fxputc(f, ch);
c7b137
+		}
c7b137
+		return;
c7b137
+	}
c7b137
+
c7b137
+#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
c7b137
+	{
c7b137
+		mbstate_t state;
c7b137
+		wchar_t nextchar;
c7b137
+		size_t bytesconsumed;
c7b137
+		char *eop, *op, buffer[256];
c7b137
+		(void)memset(&state, 0, sizeof(mbstate_t));
c7b137
+		char* op1;
c7b137
+		eop = (char *) (buf + strlen(buf));
c7b137
+		op = (char *) buf;
c7b137
+		op1 = op;
c7b137
+		while (op < eop) {
c7b137
+			bytesconsumed = mbrtowc(&nextchar, op,
c7b137
+					eop - op, &state);
c7b137
+			/* This isn't supposed to happen as we verified the
c7b137
+			 * string before hand */
c7b137
+			assert(bytesconsumed != (size_t)(-1) && bytesconsumed != (size_t)(-2));
c7b137
+
c7b137
+			if (iswprint(nextchar)) {
c7b137
+				(void)memcpy(buffer, op, bytesconsumed);
c7b137
+				buffer[bytesconsumed] = '\0';
c7b137
+				op += bytesconsumed;
c7b137
+			} else if (bytesconsumed == 1) {
c7b137
+				op++;
c7b137
+			} else {
c7b137
+				char *tmp;
c7b137
+				tmp = buffer;
c7b137
+				buffer[bytesconsumed] = '\0';
c7b137
+				while (bytesconsumed-- > 0) {
c7b137
+					OCTALIFY(tmp, op);
c7b137
+				}
c7b137
+			}
c7b137
+		}
c7b137
+		fprintf(f,"%s",op1);
c7b137
+	}
c7b137
+#endif
c7b137
 }
c7b137
 
c7b137
 int xprintf(const char *fmt, ...) {