17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57257d1b4Sraf * Common Development and Distribution License (the "License"). 67257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 23*23a1cceaSRoger A. Faulkner * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297257d1b4Sraf #include "lint.h" 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <dirent.h> 327c478bd9Sstevel@tonic-gate #include <sys/param.h> 337c478bd9Sstevel@tonic-gate #include <limits.h> 347c478bd9Sstevel@tonic-gate #include <errno.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <unistd.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Canonicalize the path given in file_name, resolving away all symbolic link 417c478bd9Sstevel@tonic-gate * components. Store the result into the buffer named by resolved_name, which 42*23a1cceaSRoger A. Faulkner * must be long enough (PATH_MAX bytes will suffice). Returns NULL 437c478bd9Sstevel@tonic-gate * on failure and resolved_name on success. On failure, to maintain 447c478bd9Sstevel@tonic-gate * compatibility with the past, the contents of file_name will be copied 457c478bd9Sstevel@tonic-gate * into resolved_name. 467c478bd9Sstevel@tonic-gate */ 47*23a1cceaSRoger A. Faulkner static char * 48*23a1cceaSRoger A. Faulkner realpath_impl(const char *file_name, char *resolved_name) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate char cwd[PATH_MAX]; 517c478bd9Sstevel@tonic-gate int len; 527c478bd9Sstevel@tonic-gate 53*23a1cceaSRoger A. Faulkner if (file_name == NULL) { 547c478bd9Sstevel@tonic-gate errno = EINVAL; 557c478bd9Sstevel@tonic-gate return (NULL); 567c478bd9Sstevel@tonic-gate } 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate /* 597c478bd9Sstevel@tonic-gate * Call resolvepath() to resolve all the symlinks in file_name, 607c478bd9Sstevel@tonic-gate * eliminate embedded "." components, and collapse embedded ".." 617c478bd9Sstevel@tonic-gate * components. We may be left with leading ".." components. 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate if ((len = resolvepath(file_name, resolved_name, PATH_MAX)) < 0) { 647c478bd9Sstevel@tonic-gate (void) strlcpy(resolved_name, file_name, PATH_MAX); 657c478bd9Sstevel@tonic-gate return (NULL); /* errno set by resolvepath() */ 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate if (len >= PATH_MAX) /* "can't happen" */ 697c478bd9Sstevel@tonic-gate len = PATH_MAX - 1; 707c478bd9Sstevel@tonic-gate resolved_name[len] = '\0'; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate if (*resolved_name == '/') /* nothing more to do */ 737c478bd9Sstevel@tonic-gate return (resolved_name); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * Prepend the current working directory to the relative path. 777c478bd9Sstevel@tonic-gate * If the relative path is not empty (or "."), collapse all of the 787c478bd9Sstevel@tonic-gate * resulting embedded ".." components with trailing cwd components. 797c478bd9Sstevel@tonic-gate * We know that getcwd() returns a path name free of symlinks. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate if (getcwd(cwd, sizeof (cwd)) == NULL) { 827c478bd9Sstevel@tonic-gate (void) strlcpy(resolved_name, file_name, PATH_MAX); 837c478bd9Sstevel@tonic-gate return (NULL); /* errno set by getcwd() */ 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (len != 0 && strcmp(resolved_name, ".") != 0) { 877c478bd9Sstevel@tonic-gate char *relpath = resolved_name; 887c478bd9Sstevel@tonic-gate char *endcwd = cwd + strlen(cwd); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * Eliminate ".." components from the relative path 927c478bd9Sstevel@tonic-gate * left-to-right, components from cwd right-to-left. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate relpath[len++] = '/'; 957c478bd9Sstevel@tonic-gate while (len >= 3 && strncmp(relpath, "../", 3) == 0) { 967c478bd9Sstevel@tonic-gate relpath += 3; 977c478bd9Sstevel@tonic-gate len -= 3; 987c478bd9Sstevel@tonic-gate while (*--endcwd != '/') 997c478bd9Sstevel@tonic-gate continue; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate if (len == 0) { 1027c478bd9Sstevel@tonic-gate /* the relative path was all ".." components */ 1037c478bd9Sstevel@tonic-gate *endcwd = '\0'; 1047c478bd9Sstevel@tonic-gate } else { 1057c478bd9Sstevel@tonic-gate /* there are non-null components on both sides */ 1067c478bd9Sstevel@tonic-gate relpath[--len] = '\0'; 1077c478bd9Sstevel@tonic-gate *endcwd++ = '/'; 1087c478bd9Sstevel@tonic-gate if (endcwd + len >= cwd + PATH_MAX) { 1097c478bd9Sstevel@tonic-gate (void) strlcpy(resolved_name, 1107c478bd9Sstevel@tonic-gate file_name, PATH_MAX); 1117c478bd9Sstevel@tonic-gate errno = ENAMETOOLONG; 1127c478bd9Sstevel@tonic-gate return (NULL); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate (void) strcpy(endcwd, relpath); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate (void) strcpy(resolved_name, cwd); 1197c478bd9Sstevel@tonic-gate return (resolved_name); 1207c478bd9Sstevel@tonic-gate } 121*23a1cceaSRoger A. Faulkner 122*23a1cceaSRoger A. Faulkner /* 123*23a1cceaSRoger A. Faulkner * Canonicalize the path given in file_name, resolving away all symbolic link 124*23a1cceaSRoger A. Faulkner * components. If resolved_name is a null pointer, return a malloc()d 125*23a1cceaSRoger A. Faulkner * buffer containing the result, else store the result into resolved_name 126*23a1cceaSRoger A. Faulkner * and return resolved_name. Return NULL on failure. 127*23a1cceaSRoger A. Faulkner */ 128*23a1cceaSRoger A. Faulkner char * 129*23a1cceaSRoger A. Faulkner realpath(const char *file_name, char *resolved_name) 130*23a1cceaSRoger A. Faulkner { 131*23a1cceaSRoger A. Faulkner char buffer[PATH_MAX]; 132*23a1cceaSRoger A. Faulkner 133*23a1cceaSRoger A. Faulkner if (resolved_name != NULL) 134*23a1cceaSRoger A. Faulkner return (realpath_impl(file_name, resolved_name)); 135*23a1cceaSRoger A. Faulkner 136*23a1cceaSRoger A. Faulkner if (realpath_impl(file_name, buffer) != NULL) 137*23a1cceaSRoger A. Faulkner return (strdup(buffer)); 138*23a1cceaSRoger A. Faulkner 139*23a1cceaSRoger A. Faulkner return (NULL); 140*23a1cceaSRoger A. Faulkner } 141*23a1cceaSRoger A. Faulkner 142*23a1cceaSRoger A. Faulkner /* 143*23a1cceaSRoger A. Faulkner * GNU extension. 144*23a1cceaSRoger A. Faulkner */ 145*23a1cceaSRoger A. Faulkner char * 146*23a1cceaSRoger A. Faulkner canonicalize_file_name(const char *path) 147*23a1cceaSRoger A. Faulkner { 148*23a1cceaSRoger A. Faulkner return (realpath(path, NULL)); 149*23a1cceaSRoger A. Faulkner } 150