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