1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
4 #include "../src/bfstd.h"
5 #include "../src/config.h"
6 #include "../src/diag.h"
15 /** Check the result of xdirname()/xbasename(). */
16 static void check_base_dir(const char *path, const char *dir, const char *base) {
17 char *xdir = xdirname(path);
18 bfs_verify(xdir, "xdirname(): %s", strerror(errno));
19 bfs_verify(strcmp(xdir, dir) == 0, "xdirname('%s') == '%s' (!= '%s')", path, xdir, dir);
22 char *xbase = xbasename(path);
23 bfs_verify(xbase, "xbasename(): %s", strerror(errno));
24 bfs_verify(strcmp(xbase, base) == 0, "xbasename('%s') == '%s' (!= '%s')", path, xbase, base);
28 /** Check the result of wordesc(). */
29 static void check_wordesc(const char *str, const char *exp, enum wesc_flags flags) {
31 char *end = buf + sizeof(buf);
32 char *ret = wordesc(buf, end, str, flags);
33 bfs_verify(ret != end);
34 bfs_verify(strcmp(buf, exp) == 0, "wordesc(%s) == %s (!= %s)", str, buf, exp);
38 // Try to set a UTF-8 locale
39 if (!setlocale(LC_ALL, "C.UTF-8")) {
40 setlocale(LC_ALL, "");
43 // From man 3p basename
44 check_base_dir("usr", ".", "usr");
45 check_base_dir("usr/", ".", "usr");
46 check_base_dir("", ".", ".");
47 check_base_dir("/", "/", "/");
48 // check_base_dir("//", "/" or "//", "/" or "//");
49 check_base_dir("///", "/", "/");
50 check_base_dir("/usr/", "/", "usr");
51 check_base_dir("/usr/lib", "/usr", "lib");
52 check_base_dir("//usr//lib//", "//usr", "lib");
53 check_base_dir("/home//dwc//test", "/home//dwc", "test");
55 check_wordesc("", "\"\"", WESC_SHELL);
56 check_wordesc("word", "word", WESC_SHELL);
57 check_wordesc("two words", "\"two words\"", WESC_SHELL);
58 check_wordesc("word's", "\"word's\"", WESC_SHELL);
59 check_wordesc("\"word\"", "'\"word\"'", WESC_SHELL);
60 check_wordesc("\"word's\"", "'\"word'\\''s\"'", WESC_SHELL);
61 check_wordesc("\033[1mbold's\033[0m", "$'\\e[1mbold\\'s\\e[0m'", WESC_SHELL | WESC_TTY);
62 check_wordesc("\x7F", "$'\\x7F'", WESC_SHELL | WESC_TTY);
64 const char *charmap = nl_langinfo(CODESET);
65 if (strcmp(charmap, "UTF-8") == 0) {
66 check_wordesc("\xF0", "$'\\xF0'", WESC_SHELL | WESC_TTY);
67 check_wordesc("\xF0\x9F", "$'\\xF0\\x9F'", WESC_SHELL | WESC_TTY);
68 check_wordesc("\xF0\x9F\x98", "$'\\xF0\\x9F\\x98'", WESC_SHELL | WESC_TTY);
69 check_wordesc("\xF0\x9F\x98\x80", "\xF0\x9F\x98\x80", WESC_SHELL | WESC_TTY);