10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
296812Sraf #include "lint.h"
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <dirent.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * Canonicalize the path given in file_name, resolving away all symbolic link
410Sstevel@tonic-gate * components. Store the result into the buffer named by resolved_name, which
42*13093SRoger.Faulkner@Oracle.COM * must be long enough (PATH_MAX bytes will suffice). Returns NULL
430Sstevel@tonic-gate * on failure and resolved_name on success. On failure, to maintain
440Sstevel@tonic-gate * compatibility with the past, the contents of file_name will be copied
450Sstevel@tonic-gate * into resolved_name.
460Sstevel@tonic-gate */
47*13093SRoger.Faulkner@Oracle.COM static char *
realpath_impl(const char * file_name,char * resolved_name)48*13093SRoger.Faulkner@Oracle.COM realpath_impl(const char *file_name, char *resolved_name)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate char cwd[PATH_MAX];
510Sstevel@tonic-gate int len;
520Sstevel@tonic-gate
53*13093SRoger.Faulkner@Oracle.COM if (file_name == NULL) {
540Sstevel@tonic-gate errno = EINVAL;
550Sstevel@tonic-gate return (NULL);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate * Call resolvepath() to resolve all the symlinks in file_name,
600Sstevel@tonic-gate * eliminate embedded "." components, and collapse embedded ".."
610Sstevel@tonic-gate * components. We may be left with leading ".." components.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate if ((len = resolvepath(file_name, resolved_name, PATH_MAX)) < 0) {
640Sstevel@tonic-gate (void) strlcpy(resolved_name, file_name, PATH_MAX);
650Sstevel@tonic-gate return (NULL); /* errno set by resolvepath() */
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate if (len >= PATH_MAX) /* "can't happen" */
690Sstevel@tonic-gate len = PATH_MAX - 1;
700Sstevel@tonic-gate resolved_name[len] = '\0';
710Sstevel@tonic-gate
720Sstevel@tonic-gate if (*resolved_name == '/') /* nothing more to do */
730Sstevel@tonic-gate return (resolved_name);
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Prepend the current working directory to the relative path.
770Sstevel@tonic-gate * If the relative path is not empty (or "."), collapse all of the
780Sstevel@tonic-gate * resulting embedded ".." components with trailing cwd components.
790Sstevel@tonic-gate * We know that getcwd() returns a path name free of symlinks.
800Sstevel@tonic-gate */
810Sstevel@tonic-gate if (getcwd(cwd, sizeof (cwd)) == NULL) {
820Sstevel@tonic-gate (void) strlcpy(resolved_name, file_name, PATH_MAX);
830Sstevel@tonic-gate return (NULL); /* errno set by getcwd() */
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (len != 0 && strcmp(resolved_name, ".") != 0) {
870Sstevel@tonic-gate char *relpath = resolved_name;
880Sstevel@tonic-gate char *endcwd = cwd + strlen(cwd);
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * Eliminate ".." components from the relative path
920Sstevel@tonic-gate * left-to-right, components from cwd right-to-left.
930Sstevel@tonic-gate */
940Sstevel@tonic-gate relpath[len++] = '/';
950Sstevel@tonic-gate while (len >= 3 && strncmp(relpath, "../", 3) == 0) {
960Sstevel@tonic-gate relpath += 3;
970Sstevel@tonic-gate len -= 3;
980Sstevel@tonic-gate while (*--endcwd != '/')
990Sstevel@tonic-gate continue;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate if (len == 0) {
1020Sstevel@tonic-gate /* the relative path was all ".." components */
1030Sstevel@tonic-gate *endcwd = '\0';
1040Sstevel@tonic-gate } else {
1050Sstevel@tonic-gate /* there are non-null components on both sides */
1060Sstevel@tonic-gate relpath[--len] = '\0';
1070Sstevel@tonic-gate *endcwd++ = '/';
1080Sstevel@tonic-gate if (endcwd + len >= cwd + PATH_MAX) {
1090Sstevel@tonic-gate (void) strlcpy(resolved_name,
1100Sstevel@tonic-gate file_name, PATH_MAX);
1110Sstevel@tonic-gate errno = ENAMETOOLONG;
1120Sstevel@tonic-gate return (NULL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate (void) strcpy(endcwd, relpath);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate (void) strcpy(resolved_name, cwd);
1190Sstevel@tonic-gate return (resolved_name);
1200Sstevel@tonic-gate }
121*13093SRoger.Faulkner@Oracle.COM
122*13093SRoger.Faulkner@Oracle.COM /*
123*13093SRoger.Faulkner@Oracle.COM * Canonicalize the path given in file_name, resolving away all symbolic link
124*13093SRoger.Faulkner@Oracle.COM * components. If resolved_name is a null pointer, return a malloc()d
125*13093SRoger.Faulkner@Oracle.COM * buffer containing the result, else store the result into resolved_name
126*13093SRoger.Faulkner@Oracle.COM * and return resolved_name. Return NULL on failure.
127*13093SRoger.Faulkner@Oracle.COM */
128*13093SRoger.Faulkner@Oracle.COM char *
realpath(const char * file_name,char * resolved_name)129*13093SRoger.Faulkner@Oracle.COM realpath(const char *file_name, char *resolved_name)
130*13093SRoger.Faulkner@Oracle.COM {
131*13093SRoger.Faulkner@Oracle.COM char buffer[PATH_MAX];
132*13093SRoger.Faulkner@Oracle.COM
133*13093SRoger.Faulkner@Oracle.COM if (resolved_name != NULL)
134*13093SRoger.Faulkner@Oracle.COM return (realpath_impl(file_name, resolved_name));
135*13093SRoger.Faulkner@Oracle.COM
136*13093SRoger.Faulkner@Oracle.COM if (realpath_impl(file_name, buffer) != NULL)
137*13093SRoger.Faulkner@Oracle.COM return (strdup(buffer));
138*13093SRoger.Faulkner@Oracle.COM
139*13093SRoger.Faulkner@Oracle.COM return (NULL);
140*13093SRoger.Faulkner@Oracle.COM }
141*13093SRoger.Faulkner@Oracle.COM
142*13093SRoger.Faulkner@Oracle.COM /*
143*13093SRoger.Faulkner@Oracle.COM * GNU extension.
144*13093SRoger.Faulkner@Oracle.COM */
145*13093SRoger.Faulkner@Oracle.COM char *
canonicalize_file_name(const char * path)146*13093SRoger.Faulkner@Oracle.COM canonicalize_file_name(const char *path)
147*13093SRoger.Faulkner@Oracle.COM {
148*13093SRoger.Faulkner@Oracle.COM return (realpath(path, NULL));
149*13093SRoger.Faulkner@Oracle.COM }
150