gnulib: update to 776af40e0
[deliverable/binutils-gdb.git] / gnulib / import / basename-lgpl.c
CommitLineData
6a29c58e
YQ
1/* basename.c -- return the last element in a file name
2
9c9d63b1 3 Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2021 Free Software
6a29c58e
YQ
4 Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
c0c3707f 17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
6a29c58e
YQ
18
19#include <config.h>
20
9c9d63b1
PM
21/* Specification. */
22#include "basename-lgpl.h"
6a29c58e 23
9c9d63b1 24#include <stdbool.h>
6a29c58e
YQ
25#include <string.h>
26
9c9d63b1 27#include "filename.h"
6a29c58e
YQ
28
29char *
30last_component (char const *name)
31{
32 char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
33 char const *p;
9c9d63b1 34 bool last_was_slash = false;
6a29c58e
YQ
35
36 while (ISSLASH (*base))
37 base++;
38
39 for (p = base; *p; p++)
40 {
41 if (ISSLASH (*p))
9c9d63b1
PM
42 last_was_slash = true;
43 else if (last_was_slash)
6a29c58e
YQ
44 {
45 base = p;
9c9d63b1 46 last_was_slash = false;
6a29c58e
YQ
47 }
48 }
49
50 return (char *) base;
51}
52
6a29c58e
YQ
53size_t
54base_len (char const *name)
55{
56 size_t len;
57 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
58
59 for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)
60 continue;
61
62 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
63 && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
64 return 2;
65
66 if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
67 && len == prefix_len && ISSLASH (name[prefix_len]))
68 return prefix_len + 1;
69
70 return len;
71}
This page took 0.462738 seconds and 4 git commands to generate.