gotadmin sits in section 1, not 8
[got-portable.git] / lib / utf8.c
blob6d5ff023fd59a9646377f95da58b639f453d169c
1 /*
2 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "got_compat.h"
20 #include <sys/types.h>
22 #include <err.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
26 #include <langinfo.h>
28 #include "got_error.h"
29 #include "got_utf8.h"
31 const struct got_error *
32 got_mbsavis(char** outp, int *widthp, const char *mbs)
34 const char *src; /* Iterate mbs. */
35 char *dst; /* Iterate *outp. */
36 wchar_t wc;
37 int total_width; /* Display width of the whole string. */
38 int width; /* Display width of a single Unicode char. */
39 int len; /* Length in bytes of UTF-8 encoded string. */
41 len = strlen(mbs);
42 if ((*outp = malloc(len + 1)) == NULL)
43 return got_error_from_errno("malloc");
45 if (MB_CUR_MAX == 1) {
46 memcpy(*outp, mbs, len + 1);
47 *widthp = len;
48 return NULL;
51 src = mbs;
52 dst = *outp;
53 total_width = 0;
54 while (*src != '\0') {
55 if ((len = mbtowc(&wc, src, MB_CUR_MAX)) == -1) {
56 total_width++;
57 *dst++ = '?';
58 src++;
59 } else if ((width = wcwidth(wc)) == -1) {
60 total_width++;
61 *dst++ = '?';
62 src += len;
63 } else {
64 total_width += width;
65 while (len-- > 0)
66 *dst++ = *src++;
69 *dst = '\0';
70 *widthp = total_width;
71 return NULL;
74 int
75 got_locale_is_utf8(void)
77 char *codeset = nl_langinfo(CODESET);
78 return (strcmp(codeset, "UTF-8") == 0);