1 /* dirname - return directory part of PATH.
2 Copyright (C) 1996-2024 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/>. */
26 static const char dot
[] = ".";
30 last_slash
= path
!= NULL
? strrchr (path
, '/') : NULL
;
32 if (last_slash
!= NULL
&& last_slash
!= path
&& last_slash
[1] == '\0')
34 /* Determine whether all remaining characters are slashes. */
37 for (runp
= last_slash
; runp
!= path
; --runp
)
41 /* The '/' is the last character, we have to look further. */
43 last_slash
= __memrchr (path
, '/', runp
- path
);
46 if (last_slash
!= NULL
)
48 /* Determine whether all remaining characters are slashes. */
51 for (runp
= last_slash
; runp
!= path
; --runp
)
55 /* Terminate the path. */
58 /* The last slash is the first character in the string. We have to
59 return "/". As a special case we have to return "//" if there
60 are exactly two slashes at the beginning of the string. See
61 XBD 4.10 Path Name Resolution for more information. */
62 if (last_slash
== path
+ 1)
65 last_slash
= path
+ 1;
73 /* This assignment is ill-designed but the XPG specs require to
74 return a string containing "." in any case no directory part is
75 found and so a static and constant string is required. */