1 /* Return basename of given pathname according to the weird XPG specification.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
24 __xpg_basename (char *filename
)
28 if (filename
== NULL
|| filename
[0] == '\0')
29 /* We return a pointer to a static string containing ".". */
33 p
= strrchr (filename
, '/');
36 /* There is no slash in the filename. Return the whole string. */
42 /* We must remove trailing '/'. */
43 while (p
> filename
&& p
[-1] == '/')
46 /* Now we can be in two situations:
47 a) the string only contains '/' characters, so we return
49 b) p points past the last component, but we have to remove
50 the trailing slash. */
54 while (p
> filename
&& p
[-1] != '/')
58 /* The last slash we already found is the right position
64 /* Go to the first character of the name. */