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
25static int
26test (const char *input, const char *result)
27{
28 int retval;
29 char *cp;
30 cp = strdupa (input);
31 cp = dirname (path: cp);
32 retval = strcmp (cp, result);
33 if (retval)
34 printf (format: "dirname(\"%s\") should be \"%s\", but is \"%s\"\n",
35 input, result, cp);
36 return retval;
37}
38
39static int
40do_test (void)
41{
42 int result = 0;
43
44 /* These are the examples given in XPG4.2. */
45 result |= test (input: "/usr/lib", result: "/usr");
46 result |= test (input: "/usr/", result: "/");
47 result |= test (input: "usr", result: ".");
48 result |= test (input: "/", result: "/");
49 result |= test (input: ".", result: ".");
50 result |= test (input: "..", result: ".");
51
52 /* Some more tests. */
53 result |= test (input: "/usr/lib/", result: "/usr");
54 result |= test (input: "/usr", result: "/");
55 result |= test (input: "a//", result: ".");
56 result |= test (input: "a////", result: ".");
57 result |= test (input: "////usr", result: "/");
58 result |= test (input: "////usr//", result: "/");
59 result |= test (input: "//usr", result: "//");
60 result |= test (input: "//usr//", result: "//");
61 result |= test (input: "//", result: "//");
62
63 /* Other Unix implementations behave like this. */
64 result |= test (input: "x///y", result: "x");
65 result |= test (input: "x/////y", result: "x");
66
67 return result != 0;
68}
69
70#define TEST_FUNCTION do_test ()
71#include "../test-skeleton.c"
72

source code of glibc/misc/tst-dirname.c