]> git.cworth.org Git - tar/blob - gnu/basename.c
upstream: Fix extraction of device nodes.
[tar] / gnu / basename.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* basename.c -- return the last element in a file name
4
5    Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2010 Free Software
6    Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 #include "dirname.h"
24
25 #include <string.h>
26 #include "xalloc.h"
27 #include "xstrndup.h"
28
29 char *
30 base_name (char const *name)
31 {
32   char const *base = last_component (name);
33   size_t length;
34
35   /* If there is no last component, then name is a file system root or the
36      empty string.  */
37   if (! *base)
38     return xstrndup (name, base_len (name));
39
40   /* Collapse a sequence of trailing slashes into one.  */
41   length = base_len (base);
42   if (ISSLASH (base[length]))
43     length++;
44
45   /* On systems with drive letters, `a/b:c' must return `./b:c' rather
46      than `b:c' to avoid confusion with a drive letter.  On systems
47      with pure POSIX semantics, this is not an issue.  */
48   if (FILE_SYSTEM_PREFIX_LEN (base))
49     {
50       char *p = xmalloc (length + 3);
51       p[0] = '.';
52       p[1] = '/';
53       memcpy (p + 2, base, length);
54       p[length + 2] = '\0';
55       return p;
56     }
57
58   /* Finally, copy the basename.  */
59   return xstrndup (base, length);
60 }