xref: /dflybsd-src/contrib/cvs-1.12/lib/basename.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* basename.c -- return the last element in a file name
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino    Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
486d7f5d3SJohn Marino    Software Foundation, Inc.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
786d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
886d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
986d7f5d3SJohn Marino    any later version.
1086d7f5d3SJohn Marino 
1186d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
1286d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1386d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1486d7f5d3SJohn Marino    GNU General Public License for more details.
1586d7f5d3SJohn Marino 
1686d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
1786d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
1886d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
2186d7f5d3SJohn Marino # include <config.h>
2286d7f5d3SJohn Marino #endif
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino #include "dirname.h"
2586d7f5d3SJohn Marino #include <string.h>
2686d7f5d3SJohn Marino 
2786d7f5d3SJohn Marino /* In general, we can't use the builtin `basename' function if available,
2886d7f5d3SJohn Marino    since it has different meanings in different environments.
2986d7f5d3SJohn Marino    In some environments the builtin `basename' modifies its argument.
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino    Return the address of the last file name component of NAME.  If
3286d7f5d3SJohn Marino    NAME has no file name components because it is all slashes, return
3386d7f5d3SJohn Marino    NAME if it is empty, the address of its last slash otherwise.  */
3486d7f5d3SJohn Marino 
3586d7f5d3SJohn Marino char *
base_name(char const * name)3686d7f5d3SJohn Marino base_name (char const *name)
3786d7f5d3SJohn Marino {
3886d7f5d3SJohn Marino   char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
3986d7f5d3SJohn Marino   char const *p;
4086d7f5d3SJohn Marino 
4186d7f5d3SJohn Marino   for (p = base; *p; p++)
4286d7f5d3SJohn Marino     {
4386d7f5d3SJohn Marino       if (ISSLASH (*p))
4486d7f5d3SJohn Marino 	{
4586d7f5d3SJohn Marino 	  /* Treat multiple adjacent slashes like a single slash.  */
4686d7f5d3SJohn Marino 	  do p++;
4786d7f5d3SJohn Marino 	  while (ISSLASH (*p));
4886d7f5d3SJohn Marino 
4986d7f5d3SJohn Marino 	  /* If the file name ends in slash, use the trailing slash as
5086d7f5d3SJohn Marino 	     the basename if no non-slashes have been found.  */
5186d7f5d3SJohn Marino 	  if (! *p)
5286d7f5d3SJohn Marino 	    {
5386d7f5d3SJohn Marino 	      if (ISSLASH (*base))
5486d7f5d3SJohn Marino 		base = p - 1;
5586d7f5d3SJohn Marino 	      break;
5686d7f5d3SJohn Marino 	    }
5786d7f5d3SJohn Marino 
5886d7f5d3SJohn Marino 	  /* *P is a non-slash preceded by a slash.  */
5986d7f5d3SJohn Marino 	  base = p;
6086d7f5d3SJohn Marino 	}
6186d7f5d3SJohn Marino     }
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino   return (char *) base;
6486d7f5d3SJohn Marino }
6586d7f5d3SJohn Marino 
6686d7f5d3SJohn Marino /* Return the length of of the basename NAME.  Typically NAME is the
6786d7f5d3SJohn Marino    value returned by base_name.  Act like strlen (NAME), except omit
6886d7f5d3SJohn Marino    redundant trailing slashes.  */
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino size_t
base_len(char const * name)7186d7f5d3SJohn Marino base_len (char const *name)
7286d7f5d3SJohn Marino {
7386d7f5d3SJohn Marino   size_t len;
7486d7f5d3SJohn Marino 
7586d7f5d3SJohn Marino   for (len = strlen (name);  1 < len && ISSLASH (name[len - 1]);  len--)
7686d7f5d3SJohn Marino     continue;
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino   return len;
7986d7f5d3SJohn Marino }
80