1 /* Test program for dirname function a la XPG.
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #define _GNU_SOURCE 1
20 #include <libgen.h>
21 #include <stdio.h>
22 #include <string.h>
23
24
25 static int
test(const char * input,const char * result)26 test (const char *input, const char *result)
27 {
28 int retval;
29 char *cp;
30 cp = strdupa (input);
31 cp = dirname (cp);
32 retval = strcmp (cp, result);
33 if (retval)
34 printf ("dirname(\"%s\") should be \"%s\", but is \"%s\"\n",
35 input, result, cp);
36 return retval;
37 }
38
39 static int
do_test(void)40 do_test (void)
41 {
42 int result = 0;
43
44 /* These are the examples given in XPG4.2. */
45 result |= test ("/usr/lib", "/usr");
46 result |= test ("/usr/", "/");
47 result |= test ("usr", ".");
48 result |= test ("/", "/");
49 result |= test (".", ".");
50 result |= test ("..", ".");
51
52 /* Some more tests. */
53 result |= test ("/usr/lib/", "/usr");
54 result |= test ("/usr", "/");
55 result |= test ("a//", ".");
56 result |= test ("a////", ".");
57 result |= test ("////usr", "/");
58 result |= test ("////usr//", "/");
59 result |= test ("//usr", "//");
60 result |= test ("//usr//", "//");
61 result |= test ("//", "//");
62
63 /* Other Unix implementations behave like this. */
64 result |= test ("x///y", "x");
65 result |= test ("x/////y", "x");
66
67 return result != 0;
68 }
69
70 #define TEST_FUNCTION do_test ()
71 #include "../test-skeleton.c"
72