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