exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / basename-lgpl.c
blob256f84609ce7f0ecd6d45e96f45aaa2c46c75eda
1 /* basename.c -- return the last element in a file name
3 Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2024 Free Software
4 Foundation, Inc.
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include <config.h>
21 /* Specification. */
22 #include "basename-lgpl.h"
24 #include <string.h>
26 #include "filename.h"
28 char *
29 last_component (char const *name)
31 char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
32 char const *p;
33 bool last_was_slash = false;
35 while (ISSLASH (*base))
36 base++;
38 for (p = base; *p; p++)
40 if (ISSLASH (*p))
41 last_was_slash = true;
42 else if (last_was_slash)
44 base = p;
45 last_was_slash = false;
49 return (char *) base;
52 size_t
53 base_len (char const *name)
55 size_t len;
56 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
58 for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)
59 continue;
61 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
62 && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
63 return 2;
65 if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
66 && len == prefix_len && ISSLASH (name[prefix_len]))
67 return prefix_len + 1;
69 return len;