Blame lib/dirname-lgpl.c

Packit Service fdd496
/* dirname.c -- return all but the last element in a file name
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 1990, 1998, 2000-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 <stdlib.h>
Packit Service fdd496
#include <string.h>
Packit Service fdd496
Packit Service fdd496
/* Return the length of the prefix of FILE that will be used by
Packit Service fdd496
   dir_name.  If FILE is in the working directory, this returns zero
Packit Service fdd496
   even though 'dir_name (FILE)' will return ".".  Works properly even
Packit Service fdd496
   if there are trailing slashes (by effectively ignoring them).  */
Packit Service fdd496
Packit Service fdd496
size_t
Packit Service fdd496
dir_len (char const *file)
Packit Service fdd496
{
Packit Service fdd496
  size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
Packit Service fdd496
  size_t length;
Packit Service fdd496
Packit Service fdd496
  /* Advance prefix_length beyond important leading slashes.  */
Packit Service fdd496
  prefix_length += (prefix_length != 0
Packit Service fdd496
                    ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
Packit Service fdd496
                       && ISSLASH (file[prefix_length]))
Packit Service fdd496
                    : (ISSLASH (file[0])
Packit Service fdd496
                       ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
Packit Service fdd496
                           && ISSLASH (file[1]) && ! ISSLASH (file[2])
Packit Service fdd496
                           ? 2 : 1))
Packit Service fdd496
                       : 0));
Packit Service fdd496
Packit Service fdd496
  /* Strip the basename and any redundant slashes before it.  */
Packit Service fdd496
  for (length = last_component (file) - file;
Packit Service fdd496
       prefix_length < length; length--)
Packit Service fdd496
    if (! ISSLASH (file[length - 1]))
Packit Service fdd496
      break;
Packit Service fdd496
  return length;
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
Packit Service fdd496
/* In general, we can't use the builtin 'dirname' function if available,
Packit Service fdd496
   since it has different meanings in different environments.
Packit Service fdd496
   In some environments the builtin 'dirname' modifies its argument.
Packit Service fdd496
Packit Service fdd496
   Return the leading directories part of FILE, allocated with malloc.
Packit Service fdd496
   Works properly even if there are trailing slashes (by effectively
Packit Service fdd496
   ignoring them).  Return NULL on failure.
Packit Service fdd496
Packit Service fdd496
   If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
Packit Service fdd496
   lstat (base_name (FILE)); } will access the same file.  Likewise,
Packit Service fdd496
   if the sequence { chdir (dir_name (FILE));
Packit Service fdd496
   rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
Packit Service fdd496
   to "foo" in the same directory FILE was in.  */
Packit Service fdd496
Packit Service fdd496
char *
Packit Service fdd496
mdir_name (char const *file)
Packit Service fdd496
{
Packit Service fdd496
  size_t length = dir_len (file);
Packit Service fdd496
  bool append_dot = (length == 0
Packit Service fdd496
                     || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
Packit Service fdd496
                         && length == FILE_SYSTEM_PREFIX_LEN (file)
Packit Service fdd496
                         && file[2] != '\0' && ! ISSLASH (file[2])));
Packit Service fdd496
  char *dir = malloc (length + append_dot + 1);
Packit Service fdd496
  if (!dir)
Packit Service fdd496
    return NULL;
Packit Service fdd496
  memcpy (dir, file, length);
Packit Service fdd496
  if (append_dot)
Packit Service fdd496
    dir[length++] = '.';
Packit Service fdd496
  dir[length] = '\0';
Packit Service fdd496
  return dir;
Packit Service fdd496
}