annotate src/realpath.c @ 569:9cdcb214753f

[xemacs-hg @ 2001-05-24 12:20:33 by yoshiki] Fix broken build. * gui.c (gui_error_2): New function. * lisp.h (gui_error_2): Declare it.
author yoshiki
date Thu, 24 May 2001 12:20:35 +0000
parents ed498ef2108b
children 0f910da8ca73
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"
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
27
558
ed498ef2108b [xemacs-hg @ 2001-05-23 09:59:33 by ben]
ben
parents: 462
diff changeset
28 #include "sysfile.h"
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
30 /* First char after start of absolute filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
31 #define ABS_START(name) (name + ABS_LENGTH (name))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
32
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
33 #if defined (WIN32_NATIVE)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
34 /* Length of start of absolute filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
35 # define ABS_LENGTH(name) (win32_abs_start (name))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
36 static int win32_abs_start (const char * name);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
37 /* System dependent version of readlink. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
38 # define system_readlink win32_readlink
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
39 #else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
40 # ifdef CYGWIN
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
41 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? \
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
42 (IS_DIRECTORY_SEP (name[1]) ? 2 : 1) : 0)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
43 # define system_readlink cygwin_readlink
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
44 # else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
45 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? 1 : 0)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
46 # define system_readlink readlink
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
47 # endif /* CYGWIN */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
48 #endif /* WIN32_NATIVE */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
49
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
50 #if defined (WIN32_NATIVE) || defined (CYGWIN)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
51 #include "syswindows.h"
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
52 /* 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
53 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
54 given without trailing '/'. One day, this could read Win2K's
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
55 reparse points. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
56 static int
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
57 win32_readlink (const char * name, char * buf, int size)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
58 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
59 WIN32_FIND_DATA find_data;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
60 HANDLE dir_handle = NULL;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
61 int len = 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
62 int err = 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
63 const char* lastname;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
64 int count = 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
65 const char* tmp;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
66 char* res = NULL;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
67
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
68 assert (*name);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
69
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
70 /* Sort of check we have a valid filename. */
558
ed498ef2108b [xemacs-hg @ 2001-05-23 09:59:33 by ben]
ben
parents: 462
diff changeset
71 if (strpbrk (name, "*?|<>\"") || strlen (name) >= PATH_MAX)
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
72 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
73 errno = EIO;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
74 return -1;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
75 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
76
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
77 /* Find start of filename */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
78 lastname = name + strlen (name);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
79 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
80 --lastname;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
81
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
82 /* Count slashes in unc path */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
83 if (ABS_LENGTH (name) == 2)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
84 for (tmp = name; *tmp; tmp++)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
85 if (IS_DIRECTORY_SEP (*tmp))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
86 count++;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
87
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
88 if (count >= 2 && count < 4)
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 /* UNC server or share name: just copy lowercased name. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
91 res = find_data.cFileName;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
92 for (tmp = lastname; *tmp; tmp++)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
93 *res++ = tolower (*tmp);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
94 *res = '\0';
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
95 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
96 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
97 dir_handle = FindFirstFile (name, &find_data);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
98
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
99 if (res || dir_handle != INVALID_HANDLE_VALUE)
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 ((len = strlen (find_data.cFileName)) < size)
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 if (strcmp (lastname, find_data.cFileName) == 0)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
104 /* Signal that the name is already OK. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
105 err = EINVAL;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
106 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
107 memcpy (buf, find_data.cFileName, len + 1);
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 err = ENAMETOOLONG;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
111 if (!res) FindClose (dir_handle);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
112 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
113 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
114 err = ENOENT;
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 errno = err;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
117 return err ? -1 : len;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
118 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
119 #endif /* WIN32_NATIVE || CYGWIN */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
120
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
121 #ifdef CYGWIN
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
122 /* 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
123 static int
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
124 cygwin_readlink (const char * name, char * buf, int size)
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 int n = readlink (name, buf, size);
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
127 if (n < 0 && errno == EINVAL)
446
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 /* 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
130 right name. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
131 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
132 cygwin_posix_to_win32_path_list (name, tmp);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
133 n = win32_readlink (tmp, buf, size);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
134 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
135 return n;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
136 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
137 #endif /* CYGWIN */
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 #ifdef WIN32_NATIVE
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
140 #ifndef ELOOP
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
141 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
142 #endif
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
143 /* Length of start of absolute filename. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
144 static int
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
145 win32_abs_start (const char * name)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
146 {
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
147 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
148 && IS_DIRECTORY_SEP (name[2]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
149 return 3;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
150 else if (IS_DIRECTORY_SEP (*name))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
151 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
152 else
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
153 return 0;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
154 }
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
155 #endif /* WIN32_NATIVE */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
156
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
157 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
158 #undef getcwd
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
159 #define getcwd(buffer, len) getwd (buffer)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
160 #endif
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
161
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 #ifndef PATH_MAX
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
163 # if defined (_POSIX_PATH_MAX)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
164 # define PATH_MAX _POSIX_PATH_MAX
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
165 # elif defined (MAXPATHLEN)
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
166 # define PATH_MAX MAXPATHLEN
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
167 # else
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
168 # define PATH_MAX 1024
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
169 # endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 #define MAX_READLINKS 32
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
174 char * xrealpath (const char *path, char resolved_path []);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 char *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 xrealpath (const char *path, char resolved_path [])
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 char copy_path[PATH_MAX];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 char *new_path = resolved_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 char *max_path;
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
181 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 int readlinks = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 char link_path[PATH_MAX];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 int n;
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
185 int abslen = ABS_LENGTH (path);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 /* 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
189 strcpy (copy_path, path);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 path = copy_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 max_path = copy_path + PATH_MAX - 2;
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
192
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
193 #ifdef WIN32_NATIVE
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
194 /* Check for c:/... or //server/... */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
195 if (abslen == 2 || abslen == 3)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 {
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
197 strncpy (new_path, path, abslen);
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
198 /* Make sure drive letter is lowercased. */
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
199 if (abslen == 3)
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
200 *new_path = tolower (*new_path);
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
201 new_path += abslen;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
202 path += abslen;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 }
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
204 /* No drive letter, but a beginning slash? Prepend drive letter. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
205 else if (abslen == 1)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
207 getcwd (new_path, PATH_MAX - 1);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 new_path += 3;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 }
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
211 /* Just a path name, prepend the current directory */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
214 getcwd (new_path, PATH_MAX - 1);
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
215 new_path += strlen (new_path);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
216 if (!IS_DIRECTORY_SEP (new_path[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
217 *new_path++ = DIRECTORY_SEP;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 #else
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
220 /* If it's a relative pathname use getcwd for starters. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
221 if (abslen == 0)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
223 getcwd (new_path, PATH_MAX - 1);
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
224 new_path += strlen (new_path);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
225 if (!IS_DIRECTORY_SEP (new_path[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
226 *new_path++ = DIRECTORY_SEP;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 {
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
230 /* Copy first directory sep. May have two on cygwin. */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
231 strncpy (new_path, path, abslen);
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
232 new_path += abslen;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
233 path += abslen;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 /* Expand each slash-separated pathname component. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 while (*path != '\0')
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 /* Ignore stray "/". */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
240 if (IS_DIRECTORY_SEP (*path))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 if (*path == '.')
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 /* Ignore ".". */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
249 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
255 /* Handle ".." */
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
256 if (path[1] == '.' &&
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
257 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 {
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
259 path += 2;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
261 /* Ignore ".." at root. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
262 if (new_path == ABS_START (resolved_path))
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
263 continue;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
265 /* Handle ".." by backing up. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
266 --new_path;
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
267 while (!IS_DIRECTORY_SEP (new_path[-1]))
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
268 --new_path;
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
269 continue;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 /* Safely copy the next pathname component. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
274 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 if (path > max_path)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 errno = ENAMETOOLONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 *new_path++ = *path++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
284 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 /* See if latest pathname component is a symlink. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 *new_path = '\0';
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
287 n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
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 (n < 0)
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 /* EINVAL means the file exists but isn't a symlink. */
462
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
292 #ifdef CYGWIN
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
293 if (errno != EINVAL && errno != ENOENT)
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
294 #else
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
295 if (errno != EINVAL)
0784d089fdc9 Import from CVS: tag r21-2-46
cvs
parents: 446
diff changeset
296 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 /* Protect against infinite loops. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 if (readlinks++ > MAX_READLINKS)
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 errno = ELOOP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 /* Note: readlink doesn't add the null byte. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 link_path[n] = '\0';
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
310
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
311 if (ABS_LENGTH (link_path) > 0)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 /* Start over for an absolute symlink. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
313 new_path = resolved_path + ABS_LENGTH (link_path) - 1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 /* Otherwise back up over this component. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
316 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
317 assert (new_path > resolved_path);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 /* Safe sex check. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 if (strlen(path) + n >= PATH_MAX)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 errno = ENAMETOOLONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 /* Insert symlink contents into path. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 strcat(link_path, path);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 strcpy(copy_path, link_path);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 path = copy_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 }
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
331 #endif /* S_IFLNK || WIN32_NATIVE */
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
332 *new_path++ = DIRECTORY_SEP;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 }
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 /* Delete trailing slash but don't whomp a lone slash. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
336 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
337 new_path--;
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 /* Make sure it's null terminated. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 *new_path = '\0';
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
341
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 return resolved_path;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 }