~ubuntu-branches/ubuntu/trusty/librep/trusty

« back to all changes in this revision

Viewing changes to src/realpath.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2001-11-13 15:06:22 UTC
  • Revision ID: james.westby@ubuntu.com-20011113150622-vgmgmk6srj3kldr3
Tags: upstream-0.15.2
ImportĀ upstreamĀ versionĀ 0.15.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Return the canonical absolute name of a given file.
 
2
   Copyright (C) 1996 Free Software Foundation, Inc.
 
3
   This file is part of the GNU C Library.
 
4
 
 
5
   The GNU C Library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public License as
 
7
   published by the Free Software Foundation; either version 2 of the
 
8
   License, or (at your option) any later version.
 
9
 
 
10
   The GNU C Library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public
 
16
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
 
17
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.  */
 
19
 
 
20
/* I've hacked this file to compile with Jade --jsh */
 
21
 
 
22
#include <config.h>
 
23
 
 
24
#ifndef HAVE_REALPATH
 
25
 
 
26
/* AIX requires this to be the first thing in the file.  */
 
27
#ifdef __GNUC__
 
28
# define alloca __builtin_alloca
 
29
#else
 
30
# if HAVE_ALLOCA_H
 
31
#  include <alloca.h>
 
32
# else
 
33
#  ifdef _AIX
 
34
 #pragma alloca
 
35
#  else
 
36
#   ifndef alloca /* predefined by HP cc +Olibcalls */
 
37
char *alloca ();
 
38
#   endif
 
39
#  endif
 
40
# endif
 
41
#endif
 
42
 
 
43
#include <stdlib.h>
 
44
#include <string.h>
 
45
#ifdef NEED_MEMORY_H
 
46
# include <memory.h>
 
47
#endif
 
48
#ifdef HAVE_UNISTD_H
 
49
# include <unistd.h>
 
50
#endif
 
51
#include <limits.h>
 
52
#include <sys/param.h>
 
53
#include <sys/stat.h>
 
54
 
 
55
#ifndef S_ISLNK
 
56
#define S_ISLNK(mode)  (((mode) & S_IFMT) == S_IFLNK)
 
57
#endif
 
58
 
 
59
#ifndef S_ISSOCK
 
60
#define S_ISSOCK(mode)  (((mode) & S_IFMT) == S_IFSOCK)
 
61
#endif
 
62
 
 
63
/* Return the canonical absolute name of file NAME.  A canonical name
 
64
   does not contain any `.', `..' components nor any repeated path
 
65
   separators ('/') or symlinks.  All path components must exist.  If
 
66
   RESOLVED is null, the result is malloc'd; otherwise, if the
 
67
   canonical name is PATH_MAX chars or more, returns null with `errno'
 
68
   set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
 
69
   returns the name in RESOLVED.  If the name cannot be resolved and
 
70
   RESOLVED is non-NULL, it contains the path of the first component
 
71
   that cannot be resolved.  If the path can be resolved, RESOLVED
 
72
   holds the same value as the value returned.  */
 
73
 
 
74
/* I'll never test errno, so ignore all attempts to set it, in the
 
75
   interests of portability.. */
 
76
#define __set_errno(err)
 
77
 
 
78
char *
 
79
realpath (const char *name, char *resolved)
 
80
{
 
81
  char *rpath, *dest, *extra_buf = NULL;
 
82
  const char *start, *end, *rpath_limit;
 
83
  long int path_max;
 
84
  int num_links = 0;
 
85
 
 
86
#ifdef PATH_MAX
 
87
  path_max = PATH_MAX;
 
88
#else
 
89
  path_max = pathconf (name, _PC_PATH_MAX);
 
90
  if (path_max <= 0)
 
91
    path_max = 1024;
 
92
#endif
 
93
 
 
94
  rpath = resolved ? alloca (path_max) : malloc (path_max);
 
95
  rpath_limit = rpath + path_max;
 
96
 
 
97
  if (name[0] != '/')
 
98
    {
 
99
#ifdef HAVE_GETCWD
 
100
      if (!getcwd (rpath, path_max))
 
101
#else
 
102
      if (!getwd (rpath))
 
103
#endif
 
104
        goto error;
 
105
      dest = strchr (rpath, '\0');
 
106
    }
 
107
  else
 
108
    {
 
109
      rpath[0] = '/';
 
110
      dest = rpath + 1;
 
111
    }
 
112
 
 
113
  for (start = end = name; *start; start = end)
 
114
    {
 
115
      struct stat st;
 
116
      int n;
 
117
 
 
118
      /* skip sequence of multiple path-separators: */
 
119
      while (*start == '/') ++start;
 
120
 
 
121
      /* find end of path component: */
 
122
      for (end = start; *end && *end != '/'; ++end);
 
123
 
 
124
      if (end - start == 0)
 
125
        break;
 
126
      else if (strncmp (start, ".", end - start) == 0)
 
127
        /* nothing */;
 
128
      else if (strncmp (start, "..", end - start) == 0) {
 
129
        /* back up to previous component, ignore if at root already: */
 
130
        if (dest > rpath + 1)
 
131
          while ((--dest)[-1] != '/');
 
132
      } else
 
133
        {
 
134
          size_t new_size;
 
135
 
 
136
          if (dest[-1] != '/')
 
137
            *dest++ = '/';
 
138
 
 
139
          if (dest + (end - start) >= rpath_limit)
 
140
            {
 
141
              if (resolved)
 
142
                {
 
143
                  __set_errno (ENAMETOOLONG);
 
144
                  goto error;
 
145
                }
 
146
              new_size = rpath_limit - rpath;
 
147
              if (end - start + 1 > path_max)
 
148
                new_size += end - start + 1;
 
149
              else
 
150
                new_size += path_max;
 
151
              rpath = realloc (rpath, new_size);
 
152
              rpath_limit = rpath + new_size;
 
153
              if (!rpath)
 
154
                return NULL;
 
155
            }
 
156
 
 
157
          memcpy (dest, start, end - start);
 
158
          dest += end - start;
 
159
          *dest = '\0';
 
160
 
 
161
          if (lstat (rpath, &st) < 0)
 
162
            goto error;
 
163
 
 
164
          if (S_ISLNK (st.st_mode))
 
165
            {
 
166
              char *buf = alloca (path_max);
 
167
 
 
168
              if (++num_links > MAXSYMLINKS)
 
169
                {
 
170
                  __set_errno (ELOOP);
 
171
                  goto error;
 
172
                }
 
173
 
 
174
              n = readlink (rpath, buf, path_max);
 
175
              if (n < 0)
 
176
                goto error;
 
177
              buf[n] = '\0';
 
178
 
 
179
              if (!extra_buf)
 
180
                extra_buf = alloca (path_max);
 
181
 
 
182
              if ((long int) (n + strlen (end)) >= path_max)
 
183
                {
 
184
                  __set_errno (ENAMETOOLONG);
 
185
                  goto error;
 
186
                }
 
187
 
 
188
              /* careful here, end may be a pointer into extra_buf... */
 
189
              strcat (buf, end);
 
190
              strcpy (extra_buf, buf);
 
191
              name = end = extra_buf;
 
192
 
 
193
              if (buf[0] == '/')
 
194
                dest = rpath + 1;       /* it's an absolute symlink */
 
195
              else
 
196
                /* back up to previous component, ignore if at root already: */
 
197
                if (dest > rpath + 1)
 
198
                  while ((--dest)[-1] != '/');
 
199
            }
 
200
          else
 
201
            num_links = 0;
 
202
        }
 
203
    }
 
204
  if (dest > rpath + 1 && dest[-1] == '/')
 
205
    --dest;
 
206
  *dest = '\0';
 
207
 
 
208
  return resolved ? strcpy (resolved, rpath) : rpath;
 
209
 
 
210
error:
 
211
  if (resolved)
 
212
    strcpy (resolved, rpath);
 
213
  else
 
214
    free (rpath);
 
215
  return NULL;
 
216
}
 
217
 
 
218
#endif /* !HAVE_REALPATH */