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