/* basename.c -- return the last element in a path12Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003 Free Software3Foundation, Inc.45This program is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation; either version 2, or (at your option)8any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program; if not, write to the Free Software Foundation,17Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */1819#if HAVE_CONFIG_H20# include <config.h>21#endif2223#include "dirname.h"24#include <string.h>2526/* In general, we can't use the builtin `basename' function if available,27since it has different meanings in different environments.28In some environments the builtin `basename' modifies its argument.2930Return the address of the last file name component of NAME. If31NAME has no file name components because it is all slashes, return32NAME if it is empty, the address of its last slash otherwise. */3334char *35base_name (char const *name)36{37char const *base = name + FILESYSTEM_PREFIX_LEN (name);38char const *p;3940for (p = base; *p; p++)41{42if (ISSLASH (*p))43{44/* Treat multiple adjacent slashes like a single slash. */45do p++;46while (ISSLASH (*p));4748/* If the file name ends in slash, use the trailing slash as49the basename if no non-slashes have been found. */50if (! *p)51{52if (ISSLASH (*base))53base = p - 1;54break;55}5657/* *P is a non-slash preceded by a slash. */58base = p;59}60}6162return (char *) base;63}6465/* Return the length of of the basename NAME. Typically NAME is the66value returned by base_name. Act like strlen (NAME), except omit67redundant trailing slashes. */6869size_t70base_len (char const *name)71{72size_t len;7374for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)75continue;7677return len;78}798081