1 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
2 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
3 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
4 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
5 /*%% $XConsortium: realpath.hp.c /main/3 1995/10/23 09:49:57 rswiston $ */
6 /* @(#)realpath.hp.c 1.3 93/07/30
7 * Version of realpath for HP/UX systems */
10 #include <sys/types.h>
12 #include <sys/param.h>
16 realpath(pathname, finalpath)
21 char curpath[MAXPATHLEN],
30 strcpy(curpath, pathname);
32 if (*pathname != '/') {
34 if (!getcwd(workpath)) {
36 if (!getwd(workpath)) {
38 strcpy(finalpath, ".");
41 } else *workpath = NULL;
43 /* curpath is the path we're still resolving */
44 /* linkpath is the path a symbolic link points to */
45 /* workpath is the path we've resolved */
49 while (*where != NULL) {
50 if (!strcmp(where, ".")) {
56 if (!strncmp(where, "./", 2)) {
62 if (!strncmp(where, "../", 3)) {
64 ptr = last = workpath;
66 if (*ptr == '/') last = ptr;
73 ptr = strchr(where, '/');
75 ptr = where + strlen(where) - 1;
79 strcpy(namebuf, workpath);
80 for (last = namebuf; *last; last++) continue;
81 /* tack on a trailing, or leading, `/` */
82 if (last == namebuf || *--last != '/') {
85 strcat(namebuf, where);
88 if (lstat(namebuf, &sbuf) == -1) {
89 strcpy(finalpath, namebuf);
93 if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
94 len = readlink(namebuf, linkpath, MAXPATHLEN);
96 strcpy(finalpath, namebuf);
99 *(linkpath + len) = NULL; /* readlink doesn't null-terminate result */
100 if (*linkpath == '/') *workpath = NULL;
102 strcat(linkpath, "/");
103 strcat(linkpath, where);
105 strcpy(curpath, linkpath);
109 if ((sbuf.st_mode & S_IFDIR) == S_IFDIR) {
110 strcpy(workpath, namebuf);
115 strcpy(finalpath, namebuf);
116 return(NULL); /* path/notadir/morepath */
118 strcpy(workpath, namebuf);
120 strcpy(finalpath, workpath);