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 librararies 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.ultrix.c /main/3 1995/10/23 09:50:04 rswiston $ */
30 #include <sys/types.h>
32 #include <sys/param.h>
36 realpath(pathname, finalpath)
41 char curpath[MAXPATHLEN],
50 strcpy(curpath, pathname);
52 if (*pathname != '/') {
53 #if defined(__STDC__) && defined(hp)
54 if (!getcwd(workpath)) {
56 if (!getwd(workpath)) {
58 strcpy(finalpath, ".");
61 } else *workpath = NULL;
63 /* curpath is the path we're still resolving */
64 /* linkpath is the path a symbolic link points to */
65 /* workpath is the path we've resolved */
69 while (*where != NULL) {
70 if (!strcmp(where, ".")) {
76 if (!strncmp(where, "./", 2)) {
82 if (!strncmp(where, "../", 3)) {
84 ptr = last = workpath;
86 if (*ptr == '/') last = ptr;
93 ptr = strchr(where, '/');
95 ptr = where + strlen(where) - 1;
99 strcpy(namebuf, workpath);
100 for (last = namebuf; *last; last++) continue;
101 /* tack on a trailing, or leading, `/` */
102 if (last == namebuf || *--last != '/') {
103 strcat(namebuf, "/");
105 strcat(namebuf, where);
108 if (lstat(namebuf, &sbuf) == -1) {
109 strcpy(finalpath, namebuf);
113 if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
114 len = readlink(namebuf, linkpath, MAXPATHLEN);
116 strcpy(finalpath, namebuf);
119 *(linkpath + len) = NULL; /* readlink doesn't null-terminate result */
120 if (*linkpath == '/') *workpath = NULL;
122 strcat(linkpath, "/");
123 strcat(linkpath, where);
125 strcpy(curpath, linkpath);
129 if ((sbuf.st_mode & S_IFDIR) == S_IFDIR) {
130 strcpy(workpath, namebuf);
135 strcpy(finalpath, namebuf);
136 return(NULL); /* path/notadir/morepath */
138 strcpy(workpath, namebuf);
140 strcpy(finalpath, workpath);