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