~ubuntu-branches/ubuntu/raring/findutils/raring

« back to all changes in this revision

Viewing changes to gnulib/lib/dirname.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2005-07-04 11:37:37 UTC
  • mto: (11.1.1 lenny) (1.1.10 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050704113737-oxfumqxsqgfz5gay
Tags: upstream-4.2.22
ImportĀ upstreamĀ versionĀ 4.2.22

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* dirname.c -- return all but the last element in a path
2
 
   Copyright 1990, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
 
1
/* dirname.c -- return all but the last element in a file name
 
2
 
 
3
   Copyright (C) 1990, 1998, 2000, 2001, 2003, 2004, 2005 Free Software
 
4
   Foundation, Inc.
3
5
 
4
6
   This program is free software; you can redistribute it and/or modify
5
7
   it under the terms of the GNU General Public License as published by
13
15
 
14
16
   You should have received a copy of the GNU General Public License
15
17
   along with this program; if not, write to the Free Software Foundation,
16
 
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
18
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
19
 
18
20
#if HAVE_CONFIG_H
19
21
# include <config.h>
20
22
#endif
21
23
 
22
 
#if STDC_HEADERS || HAVE_STRING_H
23
 
# include <string.h>
24
 
#endif
25
 
 
26
24
#include "dirname.h"
 
25
 
 
26
#include <string.h>
27
27
#include "xalloc.h"
28
28
 
29
 
/* Return the length of `dirname (PATH)', or zero if PATH is
 
29
/* Return the length of `dirname (FILE)', or zero if FILE is
30
30
   in the working directory.  Works properly even if
31
31
   there are trailing slashes (by effectively ignoring them).  */
32
32
size_t
33
 
dir_len (char const *path)
 
33
dir_len (char const *file)
34
34
{
35
 
  size_t prefix_length = FILESYSTEM_PREFIX_LEN (path);
 
35
  size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
36
36
  size_t length;
37
37
 
38
38
  /* Strip the basename and any redundant slashes before it.  */
39
 
  for (length = base_name (path) - path;  prefix_length < length;  length--)
40
 
    if (! ISSLASH (path[length - 1]))
 
39
  for (length = base_name (file) - file;  prefix_length < length;  length--)
 
40
    if (! ISSLASH (file[length - 1]))
41
41
      return length;
42
42
 
43
43
  /* But don't strip the only slash from "/".  */
44
 
  return prefix_length + ISSLASH (path[prefix_length]);
 
44
  return prefix_length + ISSLASH (file[prefix_length]);
45
45
}
46
46
 
47
 
/* Return the leading directories part of PATH,
 
47
/* Return the leading directories part of FILE,
48
48
   allocated with xmalloc.
49
49
   Works properly even if there are trailing slashes
50
50
   (by effectively ignoring them).  */
51
51
 
52
52
char *
53
 
dir_name (char const *path)
 
53
dir_name (char const *file)
54
54
{
55
 
  size_t length = dir_len (path);
56
 
  int append_dot = (length == FILESYSTEM_PREFIX_LEN (path));
57
 
  char *newpath = xmalloc (length + append_dot + 1);
58
 
  memcpy (newpath, path, length);
 
55
  size_t length = dir_len (file);
 
56
  bool append_dot = (length == FILE_SYSTEM_PREFIX_LEN (file));
 
57
  char *dir = xmalloc (length + append_dot + 1);
 
58
  memcpy (dir, file, length);
59
59
  if (append_dot)
60
 
    newpath[length++] = '.';
61
 
  newpath[length] = 0;
62
 
  return newpath;
 
60
    dir[length++] = '.';
 
61
  dir[length] = 0;
 
62
  return dir;
63
63
}
64
64
 
65
65
#ifdef TEST_DIRNAME
108
108
  buff[MAX_BUFF_LEN] = 0;
109
109
  while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
110
110
    {
111
 
      char path[MAX_BUFF_LEN];
 
111
      char file[MAX_BUFF_LEN];
112
112
      char expected_result[MAX_BUFF_LEN];
113
113
      char const *result;
114
 
      sscanf (buff, "%s %s", path, expected_result);
115
 
      result = dir_name (path);
 
114
      sscanf (buff, "%s %s", file, expected_result);
 
115
      result = dir_name (file);
116
116
      if (strcmp (result, expected_result))
117
 
        printf ("%s: got %s, expected %s\n", path, result, expected_result);
 
117
        printf ("%s: got %s, expected %s\n", file, result, expected_result);
118
118
    }
119
119
  return 0;
120
120
}