1170754Sdelphij/* basename.c -- return the last element in a path 2170754Sdelphij 3170754Sdelphij Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003 Free Software 4170754Sdelphij Foundation, Inc. 5170754Sdelphij 6170754Sdelphij This program is free software; you can redistribute it and/or modify 7170754Sdelphij it under the terms of the GNU General Public License as published by 8170754Sdelphij the Free Software Foundation; either version 2, or (at your option) 9170754Sdelphij any later version. 10170754Sdelphij 11170754Sdelphij This program is distributed in the hope that it will be useful, 12170754Sdelphij but WITHOUT ANY WARRANTY; without even the implied warranty of 13170754Sdelphij MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14170754Sdelphij GNU General Public License for more details. 15170754Sdelphij 16170754Sdelphij You should have received a copy of the GNU General Public License 17170754Sdelphij along with this program; if not, write to the Free Software Foundation, 18170754Sdelphij Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19170754Sdelphij 20170754Sdelphij#if HAVE_CONFIG_H 21170754Sdelphij# include <config.h> 22170754Sdelphij#endif 23170754Sdelphij 24170754Sdelphij#include "dirname.h" 25170754Sdelphij#include <string.h> 26170754Sdelphij 27170754Sdelphij/* In general, we can't use the builtin `basename' function if available, 28170754Sdelphij since it has different meanings in different environments. 29170754Sdelphij In some environments the builtin `basename' modifies its argument. 30170754Sdelphij 31170754Sdelphij Return the address of the last file name component of NAME. If 32170754Sdelphij NAME has no file name components because it is all slashes, return 33170754Sdelphij NAME if it is empty, the address of its last slash otherwise. */ 34170754Sdelphij 35170754Sdelphijchar * 36170754Sdelphijbase_name (char const *name) 37170754Sdelphij{ 38170754Sdelphij char const *base = name + FILESYSTEM_PREFIX_LEN (name); 39170754Sdelphij char const *p; 40170754Sdelphij 41170754Sdelphij for (p = base; *p; p++) 42170754Sdelphij { 43170754Sdelphij if (ISSLASH (*p)) 44170754Sdelphij { 45170754Sdelphij /* Treat multiple adjacent slashes like a single slash. */ 46170754Sdelphij do p++; 47170754Sdelphij while (ISSLASH (*p)); 48170754Sdelphij 49170754Sdelphij /* If the file name ends in slash, use the trailing slash as 50170754Sdelphij the basename if no non-slashes have been found. */ 51170754Sdelphij if (! *p) 52170754Sdelphij { 53170754Sdelphij if (ISSLASH (*base)) 54170754Sdelphij base = p - 1; 55170754Sdelphij break; 56170754Sdelphij } 57170754Sdelphij 58170754Sdelphij /* *P is a non-slash preceded by a slash. */ 59170754Sdelphij base = p; 60170754Sdelphij } 61170754Sdelphij } 62170754Sdelphij 63170754Sdelphij return (char *) base; 64170754Sdelphij} 65170754Sdelphij 66170754Sdelphij/* Return the length of of the basename NAME. Typically NAME is the 67170754Sdelphij value returned by base_name. Act like strlen (NAME), except omit 68170754Sdelphij redundant trailing slashes. */ 69170754Sdelphij 70170754Sdelphijsize_t 71170754Sdelphijbase_len (char const *name) 72170754Sdelphij{ 73170754Sdelphij size_t len; 74170754Sdelphij 75170754Sdelphij for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--) 76170754Sdelphij continue; 77170754Sdelphij 78170754Sdelphij return len; 79170754Sdelphij} 80