blob: 5f18ffdace65cd82b9c543b37bed2dd8566c106a [file] [log] [blame]
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08001/* basename.c -- return the last element in a file name
2
Ying Wang05436632013-04-05 16:01:00 -07003 Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2012 Free Software
4 Foundation, Inc.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08005
Ying Wang05436632013-04-05 16:01:00 -07006 This program is free software: you can redistribute it and/or modify
The Android Open Source Projectcea198a2009-03-03 19:29:17 -08007 it under the terms of the GNU General Public License as published by
Ying Wang05436632013-04-05 16:01:00 -07008 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080010
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
Ying Wang05436632013-04-05 16:01:00 -070017 along with this program. If not, see <http://www.gnu.org/licenses/>. */
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080018
Ying Wang05436632013-04-05 16:01:00 -070019#include <config.h>
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080020
21#include "dirname.h"
Ying Wang05436632013-04-05 16:01:00 -070022
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080023#include <string.h>
Ying Wang05436632013-04-05 16:01:00 -070024#include "xalloc.h"
25#include "xstrndup.h"
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080026
27char *
28base_name (char const *name)
29{
Ying Wang05436632013-04-05 16:01:00 -070030 char const *base = last_component (name);
31 size_t length;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080032
Ying Wang05436632013-04-05 16:01:00 -070033 /* If there is no last component, then name is a file system root or the
34 empty string. */
35 if (! *base)
36 return xstrndup (name, base_len (name));
37
38 /* Collapse a sequence of trailing slashes into one. */
39 length = base_len (base);
40 if (ISSLASH (base[length]))
41 length++;
42
43 /* On systems with drive letters, "a/b:c" must return "./b:c" rather
44 than "b:c" to avoid confusion with a drive letter. On systems
45 with pure POSIX semantics, this is not an issue. */
46 if (FILE_SYSTEM_PREFIX_LEN (base))
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080047 {
Ying Wang05436632013-04-05 16:01:00 -070048 char *p = xmalloc (length + 3);
49 p[0] = '.';
50 p[1] = '/';
51 memcpy (p + 2, base, length);
52 p[length + 2] = '\0';
53 return p;
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080054 }
55
Ying Wang05436632013-04-05 16:01:00 -070056 /* Finally, copy the basename. */
57 return xstrndup (base, length);
The Android Open Source Projectcea198a2009-03-03 19:29:17 -080058}