Git init
[framework/uifw/xorg/app/x11-apps.git] / xedit / realpath.c
1 /*
2  * Copyright (c) 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 /* $XFree86: xc/programs/xedit/realpath.c,v 1.4 2000/02/12 20:45:45 dawes Exp $ */
37
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)realpath.c  8.1 (Berkeley) 2/16/94";
40 #endif /* LIBC_SCCS and not lint */
41
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #ifndef LISP
53 #include "xedit.h"
54 #endif
55
56 #if defined(__Lynx__)
57 #define NO_FCHDIR
58 #endif
59
60 #if defined(ISC)
61 #ifndef MAXPATHLEN
62 #define MAXPATHLEN      1024
63 #endif
64 #endif
65 #ifndef MAXSYMLINKS
66 #define MAXSYMLINKS     256
67 #endif
68
69 /*
70  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
71  *
72  * Find the real name of path, by removing all ".", ".." and symlink
73  * components.  Returns (resolved) on success, or (NULL) on failure,
74  * in which case the path which caused trouble is left in (resolved).
75  */
76 char *
77 realpath(path, resolved)
78         const char *path;
79         char *resolved;
80 {
81         struct stat sb;
82 #ifdef NO_FCHDIR
83         char start[MAXPATHLEN];
84 #else
85         int fd;
86 #endif
87         int n, rootd, serrno;
88         char *p, *q, wbuf[MAXPATHLEN];
89         int symlinks = 0;
90
91         /* Save the starting point. */
92 #ifdef NO_FCHDIR
93         if (getcwd(start, MAXPATHLEN) == NULL) {
94                 (void)strcpy(resolved, ".");
95                 return (NULL);
96         }
97 #else
98         if ((fd = open(".", O_RDONLY)) < 0) {
99                 (void)strcpy(resolved, ".");
100                 return (NULL);
101         }
102 #endif
103
104         /*
105          * Find the dirname and basename from the path to be resolved.
106          * Change directory to the dirname component.
107          * lstat the basename part.
108          *     if it is a symlink, read in the value and loop.
109          *     if it is a directory, then change to that directory.
110          * get the current directory name and append the basename.
111          */
112         (void)strncpy(resolved, path, MAXPATHLEN - 1);
113         resolved[MAXPATHLEN - 1] = '\0';
114 loop:
115         q = strrchr(resolved, '/');
116         if (q != NULL) {
117                 p = q + 1;
118                 if (q == resolved)
119                         q = "/";
120                 else {
121                         do {
122                                 --q;
123                         } while (q > resolved && *q == '/');
124                         q[1] = '\0';
125                         q = resolved;
126                 }
127                 if (chdir(q) < 0)
128                         goto err1;
129         } else
130                 p = resolved;
131
132         /* Deal with the last component. */
133         if (*p != '\0' && lstat(p, &sb) == 0) {
134                 if (S_ISLNK(sb.st_mode)) {
135                       if (++symlinks > MAXSYMLINKS) {
136                               errno = ELOOP;
137                               goto err1;
138                       }
139                         n = readlink(p, resolved, MAXPATHLEN);
140                         if (n < 0)
141                                 goto err1;
142                         resolved[n] = '\0';
143                         goto loop;
144                 }
145                 if (S_ISDIR(sb.st_mode)) {
146                         if (chdir(p) < 0)
147                                 goto err1;
148                         p = "";
149                 }
150         }
151
152         /*
153          * Save the last component name and get the full pathname of
154          * the current directory.
155          */
156         (void)strcpy(wbuf, p);
157         if (getcwd(resolved, MAXPATHLEN) == 0)
158                 goto err1;
159
160         /*
161          * Join the two strings together, ensuring that the right thing
162          * happens if the last component is empty, or the dirname is root.
163          */
164         if (resolved[0] == '/' && resolved[1] == '\0')
165                 rootd = 1;
166         else
167                 rootd = 0;
168
169         if (*wbuf) {
170                 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
171                         errno = ENAMETOOLONG;
172                         goto err1;
173                 }
174                 if (rootd == 0)
175                         (void)strcat(resolved, "/");
176                 (void)strcat(resolved, wbuf);
177         }
178
179         /* Go back to where we came from. */
180 #ifdef NO_FCHDIR
181         if (chdir(start) < 0)
182 #else
183         if (fchdir(fd) < 0)
184 #endif
185         {
186                 serrno = errno;
187                 goto err2;
188         }
189
190         /* It's okay if the close fails, what's an fd more or less? */
191 #ifndef NO_FCHDIR
192         (void)close(fd);
193 #endif
194         return (resolved);
195
196 err1:   serrno = errno;
197 #ifdef NO_FCHDIR
198         (void)chdir(start);
199 #else
200         (void)fchdir(fd);
201 #endif
202 err2:
203 #ifndef NO_FCHDIR
204         (void)close(fd);
205 #endif
206         errno = serrno;
207         return (NULL);
208 }