1 /*
2  * Copyright © 2005-2020 Rich Felker
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #include <string.h>
27 #include <libgen.h>
28 #include <stdlib.h>
29 
30 #define TEST(p, b) do {                                                 \
31         tmp = strdup((p));                                              \
32         s = dirname (tmp);                                              \
33         if (strcmp((b),s)) {                                            \
34             printf(__FILE__ ":%d: dirname(\"%s\") returned \"%s\"; expected \"%s\"\n", \
35                    __LINE__, (p), s, (b));                              \
36             err++;                                                      \
37         }                                                               \
38         free(tmp);                                                      \
39     } while (0)
40 
test_dirname(void)41 int test_dirname(void)
42 {
43 	char *tmp, *s;
44 	int err=0;
45 
46 	if (strcmp(dirname(NULL), ".")) {
47 		printf(__FILE__ ":%d: dirname(NULL) returned \"%s\"; "
48 			"expected \".\"\n", __LINE__, dirname(NULL));
49 		err++;
50 	}
51 	TEST("", ".");
52 	TEST("/usr/lib", "/usr");
53 	TEST("/usr/", "/");
54 	TEST("usr", ".");
55 	TEST("/", "/");
56 	TEST("///", "/");
57 	TEST(".", ".");
58 	TEST("..", ".");
59 
60 	return err;
61 }
62 
63 #undef dirname
64 #define TEST_NAME dirname
65 #include "testcase.h"
66