annotate src/realpath.c @ 531:0493e9f3c27f

[xemacs-hg @ 2001-05-12 11:16:12 by ben] event-msw.c: eliminate cygwin warnings. dired.c, syswindows.h, win32.c: find the Net* functions the hard way to avoid errors on win 9x. find-paths.el: fix error with null EXCLUDE-REGEXP. font-lock.el: fix problem reported by hrvoje with buffers starting with a space. lib-complete.el: add a variable to control where `find-library' looks, analogous to `find-function-source-path'. etags.c: new version from Francesco. Makefile.in.in: i'm getting real tired of incomplete commits. is this getting worse or something?
author ben
date Sat, 12 May 2001 11:16:25 +0000
parents 0784d089fdc9
children ed498ef2108b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 * realpath.c -- canonicalize pathname by removing symlinks
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 #include <config.h>
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
26 #include "lisp.h"
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 #include <errno.h>
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
28
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 #ifdef HAVE_UNISTD_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 #endif
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
32
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
33 #if defined (HAVE_SYS_PARAM_H) && !defined (WIN32_NATIVE)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
34 #include <sys/param.h>
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
37 #ifdef WIN32_NATIVE
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 #include <direct.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 #include <sys/stat.h> /* for S_IFLNK */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
43 /* First char after start of absolute filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
44 #define ABS_START(name) (name + ABS_LENGTH (name))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
45
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
46 #if defined (WIN32_NATIVE)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
47 /* Length of start of absolute filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
48 # define ABS_LENGTH(name) (win32_abs_start (name))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
49 static int win32_abs_start (const char * name);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
50 /* System dependent version of readlink. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
51 # define system_readlink win32_readlink
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
52 #else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
53 # ifdef CYGWIN
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
54 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? \
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
55 (IS_DIRECTORY_SEP (name[1]) ? 2 : 1) : 0)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
56 # define system_readlink cygwin_readlink
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
57 # else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
58 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? 1 : 0)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
59 # define system_readlink readlink
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
60 # endif /* CYGWIN */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
61 #endif /* WIN32_NATIVE */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
62
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
63 #if defined (WIN32_NATIVE) || defined (CYGWIN)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
64 #include "syswindows.h"
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
65 /* Emulate readlink on win32 - finds real name (i.e. correct case) of
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
66 a file. UNC servers and shares are lower-cased. Directories must be
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
67 given without trailing '/'. One day, this could read Win2K's
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
68 reparse points. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
69 static int
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
70 win32_readlink (const char * name, char * buf, int size)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
71 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
72 WIN32_FIND_DATA find_data;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
73 HANDLE dir_handle = NULL;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
74 int len = 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
75 int err = 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
76 const char* lastname;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
77 int count = 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
78 const char* tmp;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
79 char* res = NULL;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
80
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
81 assert (*name);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
82
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
83 /* Sort of check we have a valid filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
84 if (strpbrk (name, "*?|<>\"") || strlen (name) >= MAX_PATH)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
85 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
86 errno = EIO;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
87 return -1;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
88 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
89
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
90 /* Find start of filename */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
91 lastname = name + strlen (name);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
92 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
93 --lastname;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
94
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
95 /* Count slashes in unc path */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
96 if (ABS_LENGTH (name) == 2)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
97 for (tmp = name; *tmp; tmp++)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
98 if (IS_DIRECTORY_SEP (*tmp))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
99 count++;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
100
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
101 if (count >= 2 && count < 4)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
102 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
103 /* UNC server or share name: just copy lowercased name. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
104 res = find_data.cFileName;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
105 for (tmp = lastname; *tmp; tmp++)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
106 *res++ = tolower (*tmp);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
107 *res = '\0';
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
108 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
109 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
110 dir_handle = FindFirstFile (name, &find_data);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
111
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
112 if (res || dir_handle != INVALID_HANDLE_VALUE)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
113 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
114 if ((len = strlen (find_data.cFileName)) < size)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
115 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
116 if (strcmp (lastname, find_data.cFileName) == 0)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
117 /* Signal that the name is already OK. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
118 err = EINVAL;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
119 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
120 memcpy (buf, find_data.cFileName, len + 1);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
121 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
122 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
123 err = ENAMETOOLONG;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
124 if (!res) FindClose (dir_handle);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
125 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
126 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
127 err = ENOENT;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
128
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
129 errno = err;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
130 return err ? -1 : len;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
131 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
132 #endif /* WIN32_NATIVE || CYGWIN */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
133
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
134 #ifdef CYGWIN
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
135 /* Call readlink and try to find out the correct case for the file. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
136 static int
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
137 cygwin_readlink (const char * name, char * buf, int size)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
138 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
139 int n = readlink (name, buf, size);
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
140 if (n < 0 && errno == EINVAL)
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
141 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
142 /* The file may exist, but isn't a symlink. Try to find the
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
143 right name. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
144 char* tmp = alloca (cygwin_posix_to_win32_path_list_buf_size (name));
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
145 cygwin_posix_to_win32_path_list (name, tmp);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
146 n = win32_readlink (tmp, buf, size);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
147 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
148 return n;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
149 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
150 #endif /* CYGWIN */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
151
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
152 #ifdef WIN32_NATIVE
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
153 #ifndef ELOOP
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
154 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
155 #endif
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
156 /* Length of start of absolute filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
157 static int
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
158 win32_abs_start (const char * name)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
159 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
160 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
161 && IS_DIRECTORY_SEP (name[2]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
162 return 3;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
163 else if (IS_DIRECTORY_SEP (*name))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
164 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
165 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
166 return 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
167 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
168 #endif /* WIN32_NATIVE */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
169
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
170 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
171 #undef getcwd
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
172 #define getcwd(buffer, len) getwd (buffer)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
173 #endif
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
174
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 #ifndef PATH_MAX
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
176 # if defined (_POSIX_PATH_MAX)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
177 # define PATH_MAX _POSIX_PATH_MAX
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
178 # elif defined (MAXPATHLEN)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
179 # define PATH_MAX MAXPATHLEN
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
180 # else
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
181 # define PATH_MAX 1024
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
182 # endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 #define MAX_READLINKS 32
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
187 char * xrealpath (const char *path, char resolved_path []);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 char *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 xrealpath (const char *path, char resolved_path [])
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 char copy_path[PATH_MAX];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 char *new_path = resolved_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 char *max_path;
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
194 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 int readlinks = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 char link_path[PATH_MAX];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 int n;
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
198 int abslen = ABS_LENGTH (path);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 /* Make a copy of the source path since we may need to modify it. */
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
202 strcpy (copy_path, path);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 path = copy_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 max_path = copy_path + PATH_MAX - 2;
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
205
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
206 #ifdef WIN32_NATIVE
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
207 /* Check for c:/... or //server/... */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
208 if (abslen == 2 || abslen == 3)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 {
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
210 strncpy (new_path, path, abslen);
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
211 /* Make sure drive letter is lowercased. */
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
212 if (abslen == 3)
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
213 *new_path = tolower (*new_path);
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
214 new_path += abslen;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
215 path += abslen;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 }
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
217 /* No drive letter, but a beginning slash? Prepend drive letter. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
218 else if (abslen == 1)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
220 getcwd (new_path, PATH_MAX - 1);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 new_path += 3;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 }
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
224 /* Just a path name, prepend the current directory */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
227 getcwd (new_path, PATH_MAX - 1);
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
228 new_path += strlen (new_path);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
229 if (!IS_DIRECTORY_SEP (new_path[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
230 *new_path++ = DIRECTORY_SEP;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 #else
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
233 /* If it's a relative pathname use getcwd for starters. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
234 if (abslen == 0)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
236 getcwd (new_path, PATH_MAX - 1);
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
237 new_path += strlen (new_path);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
238 if (!IS_DIRECTORY_SEP (new_path[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
239 *new_path++ = DIRECTORY_SEP;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 {
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
243 /* Copy first directory sep. May have two on cygwin. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
244 strncpy (new_path, path, abslen);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
245 new_path += abslen;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
246 path += abslen;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 /* Expand each slash-separated pathname component. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 while (*path != '\0')
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 /* Ignore stray "/". */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
253 if (IS_DIRECTORY_SEP (*path))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 if (*path == '.')
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 /* Ignore ".". */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
262 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
268 /* Handle ".." */
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
269 if (path[1] == '.' &&
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
270 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 {
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
272 path += 2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
274 /* Ignore ".." at root. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
275 if (new_path == ABS_START (resolved_path))
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
276 continue;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
278 /* Handle ".." by backing up. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
279 --new_path;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
280 while (!IS_DIRECTORY_SEP (new_path[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
281 --new_path;
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
282 continue;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 /* Safely copy the next pathname component. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
287 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 if (path > max_path)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 errno = ENAMETOOLONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 *new_path++ = *path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
297 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 /* See if latest pathname component is a symlink. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 *new_path = '\0';
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
300 n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 if (n < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 /* EINVAL means the file exists but isn't a symlink. */
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
305 #ifdef CYGWIN
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
306 if (errno != EINVAL && errno != ENOENT)
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
307 #else
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
308 if (errno != EINVAL)
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
309 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 /* Protect against infinite loops. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 if (readlinks++ > MAX_READLINKS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 errno = ELOOP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 /* Note: readlink doesn't add the null byte. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 link_path[n] = '\0';
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
323
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
324 if (ABS_LENGTH (link_path) > 0)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 /* Start over for an absolute symlink. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
326 new_path = resolved_path + ABS_LENGTH (link_path) - 1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 /* Otherwise back up over this component. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
329 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
330 assert (new_path > resolved_path);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 /* Safe sex check. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 if (strlen(path) + n >= PATH_MAX)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 errno = ENAMETOOLONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 /* Insert symlink contents into path. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 strcat(link_path, path);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 strcpy(copy_path, link_path);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 path = copy_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 }
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
344 #endif /* S_IFLNK || WIN32_NATIVE */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
345 *new_path++ = DIRECTORY_SEP;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 /* Delete trailing slash but don't whomp a lone slash. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
349 if (new_path != ABS_START (resolved_path) && IS_DIRECTORY_SEP (new_path[-1]))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 new_path--;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 /* Make sure it's null terminated. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 *new_path = '\0';
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
354
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 return resolved_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 }