GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ps/utf8.c Lines: 21 22 95.5 %
Date: 2017-11-13 Branches: 15 18 83.3 %

Line Branch Exec Source
1
/*	$OpenBSD: utf8.c,v 1.1 2016/01/10 14:04:16 schwarze Exp $	*/
2
3
/*
4
 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * witH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <ctype.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <vis.h>
24
#include <wchar.h>
25
26
int
27
mbswprint(const char *mbs, int maxwidth, int trail)
28
{
29
4952
	char	  buf[5];
30
2476
	wchar_t	  wc;
31
	int	  len;  /* length in bytes of UTF-8 encoded string */
32
	int	  width;  /* display width of a single Unicode char */
33
	int	  total_width;  /* display width of what is printed */
34
35
	total_width = 0;
36

51247
	while (*mbs != '\0' && total_width < maxwidth) {
37
15341
		len = mbtowc(&wc, mbs, MB_CUR_MAX);
38
15341
		if (len == -1) {
39
			(void)mbtowc(NULL, NULL, MB_CUR_MAX);
40
			len = 1;
41
		}
42
15341
		if (len == 1)
43
45933
			width = vis(buf, mbs[0],
44
30622
			    VIS_TAB | VIS_NL | VIS_CSTYLE, mbs[1]) - buf;
45
30
		else if ((width = wcwidth(wc)) == -1) {
46
			/* U+FFFD replacement character */
47
5
			memcpy(buf, "\357\277\275\0", 4);
48
			width = 1;
49
5
		} else {
50
25
			memcpy(buf, mbs, len);
51
25
			buf[len] = '\0';
52
		}
53
15341
		if (total_width + width > maxwidth)
54
			break;
55
15341
		fputs(buf, stdout);
56
		total_width += width;
57
15341
		mbs += len;
58
	}
59
2476
	if (trail)
60
29946
		while (total_width++ < maxwidth)
61
27560
			putchar(' ');
62
2476
	return total_width;
63
2476
}