Blame lib/basename-lgpl.c

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