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