usr/src/lib/libc/port/gen/realpath.c
changeset 13105 48f2dbca79a2
parent 6812 febeba71273d
equal deleted inserted replaced
13104:213128c64663 13105:48f2dbca79a2
    18  *
    18  *
    19  * CDDL HEADER END
    19  * CDDL HEADER END
    20  */
    20  */
    21 
    21 
    22 /*
    22 /*
    23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
    23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
    24  * Use is subject to license terms.
       
    25  */
    24  */
    26 
    25 
    27 /*	Copyright (c) 1988 AT&T	*/
    26 /*	Copyright (c) 1988 AT&T	*/
    28 /*	  All Rights Reserved  	*/
    27 /*	  All Rights Reserved  	*/
    29 
       
    30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
       
    31 
    28 
    32 #include "lint.h"
    29 #include "lint.h"
    33 #include <sys/types.h>
    30 #include <sys/types.h>
    34 #include <dirent.h>
    31 #include <dirent.h>
    35 #include <sys/param.h>
    32 #include <sys/param.h>
    40 #include <string.h>
    37 #include <string.h>
    41 
    38 
    42 /*
    39 /*
    43  * Canonicalize the path given in file_name, resolving away all symbolic link
    40  * 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
    41  * components.  Store the result into the buffer named by resolved_name, which
    45  * must be long enough (MAXPATHLEN bytes will suffice).  Returns NULL
    42  * must be long enough (PATH_MAX bytes will suffice).  Returns NULL
    46  * on failure and resolved_name on success.  On failure, to maintain
    43  * on failure and resolved_name on success.  On failure, to maintain
    47  * compatibility with the past, the contents of file_name will be copied
    44  * compatibility with the past, the contents of file_name will be copied
    48  * into resolved_name.
    45  * into resolved_name.
    49  */
    46  */
    50 char *
    47 static char *
    51 realpath(const char *file_name, char *resolved_name)
    48 realpath_impl(const char *file_name, char *resolved_name)
    52 {
    49 {
    53 	char cwd[PATH_MAX];
    50 	char cwd[PATH_MAX];
    54 	int len;
    51 	int len;
    55 
    52 
    56 	if (file_name == NULL || resolved_name == NULL) {
    53 	if (file_name == NULL) {
    57 		errno = EINVAL;
    54 		errno = EINVAL;
    58 		return (NULL);
    55 		return (NULL);
    59 	}
    56 	}
    60 
    57 
    61 	/*
    58 	/*
   119 	}
   116 	}
   120 
   117 
   121 	(void) strcpy(resolved_name, cwd);
   118 	(void) strcpy(resolved_name, cwd);
   122 	return (resolved_name);
   119 	return (resolved_name);
   123 }
   120 }
       
   121 
       
   122 /*
       
   123  * Canonicalize the path given in file_name, resolving away all symbolic link
       
   124  * components.  If resolved_name is a null pointer, return a malloc()d
       
   125  * buffer containing the result, else store the result into resolved_name
       
   126  * and return resolved_name.  Return NULL on failure.
       
   127  */
       
   128 char *
       
   129 realpath(const char *file_name, char *resolved_name)
       
   130 {
       
   131 	char buffer[PATH_MAX];
       
   132 
       
   133 	if (resolved_name != NULL)
       
   134 		return (realpath_impl(file_name, resolved_name));
       
   135 
       
   136 	if (realpath_impl(file_name, buffer) != NULL)
       
   137 		return (strdup(buffer));
       
   138 
       
   139 	return (NULL);
       
   140 }
       
   141 
       
   142 /*
       
   143  * GNU extension.
       
   144  */
       
   145 char *
       
   146 canonicalize_file_name(const char *path)
       
   147 {
       
   148 	return (realpath(path, NULL));
       
   149 }