2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
24 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
25 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
26 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
27 /*%% $XConsortium: realpath.c /main/3 1995/10/23 09:49:50 rswiston $ */
30 #include <sys/types.h>
32 #include <sys/param.h>
36 /* This is used when the operating system does not provide us with
37 * a realpath implementation. It's named differently so there is
38 * no name clash between it and the system version of realpath.
42 _tt_internal_realpath(pathname, finalpath)
47 char curpath[MAXPATHLEN],
55 int nlink = 0; /* To keep track of loops in the path. */
57 strcpy(curpath, pathname);
59 if (*pathname != '/') {
60 #if defined(__STDC__) && defined(hpux)
61 if (!getcwd(workpath)) {
63 if (!getwd(workpath)) {
65 strcpy(finalpath, ".");
68 } else *workpath = NULL;
70 /* curpath is the path we're still resolving */
71 /* linkpath is the path a symbolic link points to */
72 /* workpath is the path we've resolved */
76 while (*where != NULL) {
77 if (!strcmp(where, ".")) {
83 if (!strncmp(where, "./", 2)) {
89 if (!strncmp(where, "../", 3)) {
91 ptr = last = workpath;
93 if (*ptr == '/') last = ptr;
100 ptr = strchr(where, '/');
102 ptr = where + strlen(where) - 1;
106 strcpy(namebuf, workpath);
108 for (last = namebuf; *last; last++) continue;
110 /* tack on a trailing, or leading, `/` */
111 if (last == namebuf || *--last != '/') {
112 strcat(namebuf, "/");
115 strcat(namebuf, where);
118 if (lstat(namebuf, &sbuf) == -1) {
119 strcpy(finalpath, namebuf);
123 if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
124 /* Look for loop in path such as link to self. */
126 if (nlink > MAXSYMLINKS) {
130 len = readlink(namebuf, linkpath, MAXPATHLEN);
132 strcpy(finalpath, namebuf);
135 *(linkpath + len) = NULL; /* readlink doesn't null-terminate result */
136 if (*linkpath == '/') *workpath = NULL;
138 strcat(linkpath, "/");
139 strcat(linkpath, where);
141 strcpy(curpath, linkpath);
145 if ((sbuf.st_mode & S_IFDIR) == S_IFDIR) {
146 strcpy(workpath, namebuf);
151 strcpy(finalpath, namebuf);
152 return(NULL); /* path/notadir/morepath */
154 strcpy(workpath, namebuf);
156 strcpy(finalpath, workpath);