1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
#include "lint.h"
#include <sys/types.h>
#include <dirent.h>
#include <sys/param.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/*
* Canonicalize the path given in file_name, resolving away all symbolic link
* components. Store the result into the buffer named by resolved_name, which
* must be long enough (PATH_MAX bytes will suffice). Returns NULL
* on failure and resolved_name on success. On failure, to maintain
* compatibility with the past, the contents of file_name will be copied
* into resolved_name.
*/
static char *
realpath_impl(const char *file_name, char *resolved_name)
{
char cwd[PATH_MAX];
int len;
if (file_name == NULL) {
errno = EINVAL;
return (NULL);
}
/*
* Call resolvepath() to resolve all the symlinks in file_name,
* eliminate embedded "." components, and collapse embedded ".."
* components. We may be left with leading ".." components.
*/
if ((len = resolvepath(file_name, resolved_name, PATH_MAX)) < 0) {
(void) strlcpy(resolved_name, file_name, PATH_MAX);
return (NULL); /* errno set by resolvepath() */
}
if (len >= PATH_MAX) /* "can't happen" */
len = PATH_MAX - 1;
resolved_name[len] = '\0';
if (*resolved_name == '/') /* nothing more to do */
return (resolved_name);
/*
* Prepend the current working directory to the relative path.
* If the relative path is not empty (or "."), collapse all of the
* resulting embedded ".." components with trailing cwd components.
* We know that getcwd() returns a path name free of symlinks.
*/
if (getcwd(cwd, sizeof (cwd)) == NULL) {
(void) strlcpy(resolved_name, file_name, PATH_MAX);
return (NULL); /* errno set by getcwd() */
}
if (len != 0 && strcmp(resolved_name, ".") != 0) {
char *relpath = resolved_name;
char *endcwd = cwd + strlen(cwd);
/*
* Eliminate ".." components from the relative path
* left-to-right, components from cwd right-to-left.
*/
relpath[len++] = '/';
while (len >= 3 && strncmp(relpath, "../", 3) == 0) {
relpath += 3;
len -= 3;
while (*--endcwd != '/')
continue;
}
if (len == 0) {
/* the relative path was all ".." components */
*endcwd = '\0';
} else {
/* there are non-null components on both sides */
relpath[--len] = '\0';
*endcwd++ = '/';
if (endcwd + len >= cwd + PATH_MAX) {
(void) strlcpy(resolved_name,
file_name, PATH_MAX);
errno = ENAMETOOLONG;
return (NULL);
}
(void) strcpy(endcwd, relpath);
}
}
(void) strcpy(resolved_name, cwd);
return (resolved_name);
}
/*
* Canonicalize the path given in file_name, resolving away all symbolic link
* components. If resolved_name is a null pointer, return a malloc()d
* buffer containing the result, else store the result into resolved_name
* and return resolved_name. Return NULL on failure.
*/
char *
realpath(const char *file_name, char *resolved_name)
{
char buffer[PATH_MAX];
if (resolved_name != NULL)
return (realpath_impl(file_name, resolved_name));
if (realpath_impl(file_name, buffer) != NULL)
return (strdup(buffer));
return (NULL);
}
/*
* GNU extension.
*/
char *
canonicalize_file_name(const char *path)
{
return (realpath(path, NULL));
}
|