|
Packit Bot |
06c835 |
/* Return the name-within-directory of a file name.
|
|
Packit Bot |
06c835 |
Copyright (C) 1996-2002, 2004, 2006, 2010, 2012, 2015 Free Software
|
|
Packit Bot |
06c835 |
Foundation, Inc.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
NOTE: The canonical source of this file is maintained with the GNU C Library.
|
|
Packit Bot |
06c835 |
Bugs can be reported to bug-glibc@gnu.org.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
This program is free software: you can redistribute it and/or modify it
|
|
Packit Bot |
06c835 |
under the terms of the GNU General Public License as published by the
|
|
Packit Bot |
06c835 |
Free Software Foundation; either version 3 of the License, or any
|
|
Packit Bot |
06c835 |
later version.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
This program is distributed in the hope that it will be useful,
|
|
Packit Bot |
06c835 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Bot |
06c835 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Bot |
06c835 |
GNU General Public License for more details.
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
You should have received a copy of the GNU General Public License
|
|
Packit Bot |
06c835 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include <config.h>
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Specification. */
|
|
Packit Bot |
06c835 |
#include "basename.h"
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#if !(__GLIBC__ >= 2 || defined __UCLIBC__)
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#include <stdio.h>
|
|
Packit Bot |
06c835 |
#include <assert.h>
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
|
|
Packit Bot |
06c835 |
/* Win32, Cygwin, OS/2, DOS */
|
|
Packit Bot |
06c835 |
# define HAS_DEVICE(P) \
|
|
Packit Bot |
06c835 |
((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
|
|
Packit Bot |
06c835 |
&& (P)[1] == ':')
|
|
Packit Bot |
06c835 |
# define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
|
|
Packit Bot |
06c835 |
# define ISSLASH(C) ((C) == '/' || (C) == '\\')
|
|
Packit Bot |
06c835 |
#endif
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#ifndef FILE_SYSTEM_PREFIX_LEN
|
|
Packit Bot |
06c835 |
# define FILE_SYSTEM_PREFIX_LEN(Filename) 0
|
|
Packit Bot |
06c835 |
#endif
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#ifndef ISSLASH
|
|
Packit Bot |
06c835 |
# define ISSLASH(C) ((C) == '/')
|
|
Packit Bot |
06c835 |
#endif
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#ifndef _LIBC
|
|
Packit Bot |
06c835 |
/* We cannot generally use the name 'basename' since XPG defines an unusable
|
|
Packit Bot |
06c835 |
variant of the function but we cannot use it. */
|
|
Packit Bot |
06c835 |
# undef basename
|
|
Packit Bot |
06c835 |
# define basename gnu_basename
|
|
Packit Bot |
06c835 |
#endif
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* In general, we can't use the builtin 'basename' function if available,
|
|
Packit Bot |
06c835 |
since it has different meanings in different environments.
|
|
Packit Bot |
06c835 |
In some environments the builtin 'basename' modifies its argument.
|
|
Packit Bot |
06c835 |
If NAME is all slashes, be sure to return '/'. */
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
char *
|
|
Packit Bot |
06c835 |
basename (char const *name)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
char const *base = name += FILE_SYSTEM_PREFIX_LEN (name);
|
|
Packit Bot |
06c835 |
int all_slashes = 1;
|
|
Packit Bot |
06c835 |
char const *p;
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
for (p = name; *p; p++)
|
|
Packit Bot |
06c835 |
{
|
|
Packit Bot |
06c835 |
if (ISSLASH (*p))
|
|
Packit Bot |
06c835 |
base = p + 1;
|
|
Packit Bot |
06c835 |
else
|
|
Packit Bot |
06c835 |
all_slashes = 0;
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* If NAME is all slashes, arrange to return '/'. */
|
|
Packit Bot |
06c835 |
if (*base == '\0' && ISSLASH (*name) && all_slashes)
|
|
Packit Bot |
06c835 |
--base;
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
/* Make sure the last byte is not a slash. */
|
|
Packit Bot |
06c835 |
assert (all_slashes || !ISSLASH (*(p - 1)));
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
return (char *) base;
|
|
Packit Bot |
06c835 |
}
|
|
Packit Bot |
06c835 |
|
|
Packit Bot |
06c835 |
#endif
|