PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/screensavers/rsxs-0.9/lib/dirname.c

http://github.com/xbmc/xbmc
C | 87 lines | 40 code | 10 blank | 37 comment | 14 complexity | f3c88cc2a2ee479a34e02d0ad846dcaa MD5 | raw file
 1/* dirname.c -- return all but the last element in a file name
 2
 3   Copyright (C) 1990, 1998, 2000, 2001, 2003, 2004, 2005, 2006 Free Software
 4   Foundation, Inc.
 5
 6   This program is free software; you can redistribute it and/or modify
 7   it under the terms of the GNU General Public License as published by
 8   the Free Software Foundation; either version 2, or (at your option)
 9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include "dirname.h"
25
26#include <string.h>
27#include "xalloc.h"
28
29/* Return the length of the prefix of FILE that will be used by
30   dir_name.  If FILE is in the working directory, this returns zero
31   even though `dir_name (FILE)' will return ".".  Works properly even
32   if there are trailing slashes (by effectively ignoring them).  */
33
34size_t
35dir_len (char const *file)
36{
37  size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
38  size_t length;
39
40  /* Advance prefix_length beyond important leading slashes.  */
41  prefix_length += (prefix_length != 0
42		    ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
43		       && ISSLASH (file[prefix_length]))
44		    : (ISSLASH (file[0])
45		       ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
46			   && ISSLASH (file[1]) && ! ISSLASH (file[2])
47			   ? 2 : 1))
48		       : 0));
49
50  /* Strip the basename and any redundant slashes before it.  */
51  for (length = last_component (file) - file;
52       prefix_length < length; length--)
53    if (! ISSLASH (file[length - 1]))
54      break;
55  return length;
56}
57
58
59/* In general, we can't use the builtin `dirname' function if available,
60   since it has different meanings in different environments.
61   In some environments the builtin `dirname' modifies its argument.
62
63   Return the leading directories part of FILE, allocated with xmalloc.
64   Works properly even if there are trailing slashes (by effectively
65   ignoring them).  Unlike POSIX dirname(), FILE cannot be NULL.
66
67   If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
68   lstat (base_name (FILE)); } will access the same file.  Likewise,
69   if the sequence { chdir (dir_name (FILE));
70   rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
71   to "foo" in the same directory FILE was in.  */
72
73char *
74dir_name (char const *file)
75{
76  size_t length = dir_len (file);
77  bool append_dot = (length == 0
78		     || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
79			 && length == FILE_SYSTEM_PREFIX_LEN (file)
80			 && file[2] != '\0' && ! ISSLASH (file[2])));
81  char *dir = xmalloc (length + append_dot + 1);
82  memcpy (dir, file, length);
83  if (append_dot)
84    dir[length++] = '.';
85  dir[length] = '\0';
86  return dir;
87}