Blame lib/basename-lgpl.c

Packit 33f14e
/* basename.c -- return the last element in a file name
Packit 33f14e
Packit 33f14e
   Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2017 Free Software
Packit 33f14e
   Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
#include "dirname.h"
Packit 33f14e
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
/* Return the address of the last file name component of NAME.  If
Packit 33f14e
   NAME has no relative file name components because it is a file
Packit 33f14e
   system root, return the empty string.  */
Packit 33f14e
Packit 33f14e
char *
Packit 33f14e
last_component (char const *name)
Packit 33f14e
{
Packit 33f14e
  char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
Packit 33f14e
  char const *p;
Packit 33f14e
  bool saw_slash = false;
Packit 33f14e
Packit 33f14e
  while (ISSLASH (*base))
Packit 33f14e
    base++;
Packit 33f14e
Packit 33f14e
  for (p = base; *p; p++)
Packit 33f14e
    {
Packit 33f14e
      if (ISSLASH (*p))
Packit 33f14e
        saw_slash = true;
Packit 33f14e
      else if (saw_slash)
Packit 33f14e
        {
Packit 33f14e
          base = p;
Packit 33f14e
          saw_slash = false;
Packit 33f14e
        }
Packit 33f14e
    }
Packit 33f14e
Packit 33f14e
  return (char *) base;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Return the length of the basename NAME.  Typically NAME is the
Packit 33f14e
   value returned by base_name or last_component.  Act like strlen
Packit 33f14e
   (NAME), except omit all trailing slashes.  */
Packit 33f14e
Packit 33f14e
size_t
Packit 33f14e
base_len (char const *name)
Packit 33f14e
{
Packit 33f14e
  size_t len;
Packit 33f14e
  size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
Packit 33f14e
Packit 33f14e
  for (len = strlen (name);  1 < len && ISSLASH (name[len - 1]);  len--)
Packit 33f14e
    continue;
Packit 33f14e
Packit 33f14e
  if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
Packit 33f14e
      && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
Packit 33f14e
    return 2;
Packit 33f14e
Packit 33f14e
  if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
Packit 33f14e
      && len == prefix_len && ISSLASH (name[prefix_len]))
Packit 33f14e
    return prefix_len + 1;
Packit 33f14e
Packit 33f14e
  return len;
Packit 33f14e
}