xref: /dflybsd-src/contrib/cvs-1.12/lib/basename.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* basename.c -- return the last element in a file name
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino    Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
4*86d7f5d3SJohn Marino    Software Foundation, Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
7*86d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
8*86d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
9*86d7f5d3SJohn Marino    any later version.
10*86d7f5d3SJohn Marino 
11*86d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
12*86d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*86d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*86d7f5d3SJohn Marino    GNU General Public License for more details.
15*86d7f5d3SJohn Marino 
16*86d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
17*86d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
18*86d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19*86d7f5d3SJohn Marino 
20*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
21*86d7f5d3SJohn Marino # include <config.h>
22*86d7f5d3SJohn Marino #endif
23*86d7f5d3SJohn Marino 
24*86d7f5d3SJohn Marino #include "dirname.h"
25*86d7f5d3SJohn Marino #include <string.h>
26*86d7f5d3SJohn Marino 
27*86d7f5d3SJohn Marino /* In general, we can't use the builtin `basename' function if available,
28*86d7f5d3SJohn Marino    since it has different meanings in different environments.
29*86d7f5d3SJohn Marino    In some environments the builtin `basename' modifies its argument.
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino    Return the address of the last file name component of NAME.  If
32*86d7f5d3SJohn Marino    NAME has no file name components because it is all slashes, return
33*86d7f5d3SJohn Marino    NAME if it is empty, the address of its last slash otherwise.  */
34*86d7f5d3SJohn Marino 
35*86d7f5d3SJohn Marino char *
base_name(char const * name)36*86d7f5d3SJohn Marino base_name (char const *name)
37*86d7f5d3SJohn Marino {
38*86d7f5d3SJohn Marino   char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
39*86d7f5d3SJohn Marino   char const *p;
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino   for (p = base; *p; p++)
42*86d7f5d3SJohn Marino     {
43*86d7f5d3SJohn Marino       if (ISSLASH (*p))
44*86d7f5d3SJohn Marino 	{
45*86d7f5d3SJohn Marino 	  /* Treat multiple adjacent slashes like a single slash.  */
46*86d7f5d3SJohn Marino 	  do p++;
47*86d7f5d3SJohn Marino 	  while (ISSLASH (*p));
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino 	  /* If the file name ends in slash, use the trailing slash as
50*86d7f5d3SJohn Marino 	     the basename if no non-slashes have been found.  */
51*86d7f5d3SJohn Marino 	  if (! *p)
52*86d7f5d3SJohn Marino 	    {
53*86d7f5d3SJohn Marino 	      if (ISSLASH (*base))
54*86d7f5d3SJohn Marino 		base = p - 1;
55*86d7f5d3SJohn Marino 	      break;
56*86d7f5d3SJohn Marino 	    }
57*86d7f5d3SJohn Marino 
58*86d7f5d3SJohn Marino 	  /* *P is a non-slash preceded by a slash.  */
59*86d7f5d3SJohn Marino 	  base = p;
60*86d7f5d3SJohn Marino 	}
61*86d7f5d3SJohn Marino     }
62*86d7f5d3SJohn Marino 
63*86d7f5d3SJohn Marino   return (char *) base;
64*86d7f5d3SJohn Marino }
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino /* Return the length of of the basename NAME.  Typically NAME is the
67*86d7f5d3SJohn Marino    value returned by base_name.  Act like strlen (NAME), except omit
68*86d7f5d3SJohn Marino    redundant trailing slashes.  */
69*86d7f5d3SJohn Marino 
70*86d7f5d3SJohn Marino size_t
base_len(char const * name)71*86d7f5d3SJohn Marino base_len (char const *name)
72*86d7f5d3SJohn Marino {
73*86d7f5d3SJohn Marino   size_t len;
74*86d7f5d3SJohn Marino 
75*86d7f5d3SJohn Marino   for (len = strlen (name);  1 < len && ISSLASH (name[len - 1]);  len--)
76*86d7f5d3SJohn Marino     continue;
77*86d7f5d3SJohn Marino 
78*86d7f5d3SJohn Marino   return len;
79*86d7f5d3SJohn Marino }
80