Blame lib/basename-lgpl.c

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