GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: libexec/ftpd/../../bin/ls/utf8.c Lines: 0 18 0.0 %
Date: 2017-11-07 Branches: 0 22 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: utf8.c,v 1.2 2016/01/18 19:06:37 schwarze Exp $	*/
2
3
/*
4
 * Copyright (c) 2015, 2016 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
#ifndef SMALL
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <wchar.h>
23
24
extern int f_nonprint;
25
26
int
27
mbsprint(const char *mbs, int print)
28
{
29
	wchar_t	  wc;
30
	int	  len;  /* length in bytes of UTF-8 encoded string */
31
	int	  width;  /* display width of a single Unicode char */
32
	int	  total_width;  /* display width of the whole string */
33
34
	for (total_width = 0; *mbs != '\0'; mbs += len) {
35
		if ((len = mbtowc(&wc, mbs, MB_CUR_MAX)) == -1) {
36
			(void)mbtowc(NULL, NULL, MB_CUR_MAX);
37
			if (print)
38
				putchar(f_nonprint ? '?' : *mbs);
39
			total_width++;
40
			len = 1;
41
		} else if ((width = wcwidth(wc)) == -1) {
42
			if (print) {
43
				if (f_nonprint)
44
					putchar('?');
45
				else
46
					fwrite(mbs, 1, len, stdout);
47
			}
48
			total_width++;
49
		} else {
50
			if (print)
51
				fwrite(mbs, 1, len, stdout);
52
			total_width += width;
53
		}
54
	}
55
	return total_width;
56
}
57
#endif