1N/A/* dirname.c -- return all but the last element in a file name
1N/A
1N/A Copyright (C) 1990, 1998, 2000-2001, 2003-2006, 2009-2010 Free Software
1N/A Foundation, Inc.
1N/A
1N/A This program is free software: you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
1N/A
1N/A#include <config.h>
1N/A
1N/A#include "dirname.h"
1N/A
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A
1N/A/* Return the length of the prefix of FILE that will be used by
1N/A dir_name. If FILE is in the working directory, this returns zero
1N/A even though `dir_name (FILE)' will return ".". Works properly even
1N/A if there are trailing slashes (by effectively ignoring them). */
1N/A
1N/Asize_t
1N/Adir_len (char const *file)
1N/A{
1N/A size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
1N/A size_t length;
1N/A
1N/A /* Advance prefix_length beyond important leading slashes. */
1N/A prefix_length += (prefix_length != 0
1N/A ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
1N/A && ISSLASH (file[prefix_length]))
1N/A : (ISSLASH (file[0])
1N/A ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
1N/A && ISSLASH (file[1]) && ! ISSLASH (file[2])
1N/A ? 2 : 1))
1N/A : 0));
1N/A
1N/A /* Strip the basename and any redundant slashes before it. */
1N/A for (length = last_component (file) - file;
1N/A prefix_length < length; length--)
1N/A if (! ISSLASH (file[length - 1]))
1N/A break;
1N/A return length;
1N/A}
1N/A
1N/A
1N/A/* In general, we can't use the builtin `dirname' function if available,
1N/A since it has different meanings in different environments.
1N/A In some environments the builtin `dirname' modifies its argument.
1N/A
1N/A Return the leading directories part of FILE, allocated with malloc.
1N/A Works properly even if there are trailing slashes (by effectively
1N/A ignoring them). Return NULL on failure.
1N/A
1N/A If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
1N/A lstat (base_name (FILE)); } will access the same file. Likewise,
1N/A if the sequence { chdir (dir_name (FILE));
1N/A rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
1N/A to "foo" in the same directory FILE was in. */
1N/A
1N/Achar *
1N/Amdir_name (char const *file)
1N/A{
1N/A size_t length = dir_len (file);
1N/A bool append_dot = (length == 0
1N/A || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
1N/A && length == FILE_SYSTEM_PREFIX_LEN (file)
1N/A && file[2] != '\0' && ! ISSLASH (file[2])));
1N/A char *dir = malloc (length + append_dot + 1);
1N/A if (!dir)
1N/A return NULL;
1N/A memcpy (dir, file, length);
1N/A if (append_dot)
1N/A dir[length++] = '.';
1N/A dir[length] = '\0';
1N/A return dir;
1N/A}