Blame lib/basename-lgpl.c

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