6811333 Remove prom_printf() message in emlxs driver
[opensolaris.git] / usr / src / lib / libc / port / gen / realpath.c
blob08223f8f5cfa238174dc973b2a7c115e49f87158
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
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>
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.
50 char *
51 realpath(const char *file_name, char *resolved_name)
53 char cwd[PATH_MAX];
54 int len;
56 if (file_name == NULL || resolved_name == NULL) {
57 errno = EINVAL;
58 return (NULL);
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.
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() */
71 if (len >= PATH_MAX) /* "can't happen" */
72 len = PATH_MAX - 1;
73 resolved_name[len] = '\0';
75 if (*resolved_name == '/') /* nothing more to do */
76 return (resolved_name);
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.
84 if (getcwd(cwd, sizeof (cwd)) == NULL) {
85 (void) strlcpy(resolved_name, file_name, PATH_MAX);
86 return (NULL); /* errno set by getcwd() */
89 if (len != 0 && strcmp(resolved_name, ".") != 0) {
90 char *relpath = resolved_name;
91 char *endcwd = cwd + strlen(cwd);
94 * Eliminate ".." components from the relative path
95 * left-to-right, components from cwd right-to-left.
97 relpath[len++] = '/';
98 while (len >= 3 && strncmp(relpath, "../", 3) == 0) {
99 relpath += 3;
100 len -= 3;
101 while (*--endcwd != '/')
102 continue;
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);
117 (void) strcpy(endcwd, relpath);
121 (void) strcpy(resolved_name, cwd);
122 return (resolved_name);