1 /* Support for Non-ASCII Path Name
2 Copyright (C) 1985, 1986, 1992, 1993, 1995 Free Software Foundation, Inc.
3 Copyright (C) 1995 Sun Microsystems, Inc.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Mule 2.3. Not in FSF. */
24 /* mcpath.h should be included in config.h */
32 Lisp_Object Qpathname_coding_system = 0;
35 mcpath_encode_code (struct Lisp_Coding_System *cp)
37 Lisp_Object coding_system;
39 coding_system = Fsymbol_value (Qpathname_coding_system);
41 mule_encode_code (coding_system, cp);
42 CODE_CNTL (cp) |= CC_END;
46 mule_encode_path_1 (unsigned char *src, unsigned int srcsize,
47 unsigned char *dst, unsigned int dstsize)
49 struct Lisp_Coding_System code;
51 mcpath_encode_code (&code);
52 if (CODE_TYPE (&code) > MULE_AUTOCONV)
56 /* get_conversion_buffer () is not */
58 buf = (unsigned char *) alloca (MULE_ENCODE_BUF_SIZE (srcsize, &code));
62 Lisp_Object dummy = Qnil;
64 len = mule_encode (&code, src, buf, srcsize, &dummy);
65 if (!CODE_CHAR (&code) && len <= dstsize)
67 memcpy (dst, buf, len);
72 return -1; /* use original */
75 static unsigned char *
76 mule_decode_path_1 (unsigned char *src, unsigned char *dst,
79 struct Lisp_Coding_System code;
81 mcpath_encode_code (&code);
82 if (CODE_TYPE (&code) > MULE_AUTOCONV)
87 len = strlen (src) + 1; /* + 1 for '\0' */
89 /* get_conversion_buffer () is not */
91 buf = (unsigned char *) alloca (MULE_DECODE_BUF_SIZE (len, &code));
94 CODE_CNTL (&code) |= CC_END;
95 len = mule_decode (&code, src, buf, len);
96 if (!CODE_CHAR (&code) && len <= dstsize)
98 memcpy (dst, buf, len); /* len should include '\0' */
106 static unsigned char *
107 mule_decode_path (unsigned char *path, unsigned char ext_path[MC_MAXPATHLEN])
110 (Qpathname_coding_system
111 ? mule_decode_path_1 (path, ext_path, MC_MAXPATHLEN)
112 : path); /* in case of before initialization */
115 static unsigned char *
116 mule_encode_path (unsigned char *path, unsigned char *encode_buffer,
121 len = mule_encode_path_1 (path, strlen (path), encode_buffer, size);
123 path = encode_buffer;
125 /* convert the MSDOS style path delimiter to the UNIX style. Note
126 that now the code is *internal*, so we can simply compare each
127 character with '\\'. And this operation will alter the contents
128 of Lisp Object, PATH. */
130 unsigned char *p = path;
143 #if 0 /* example of how they do it (similar junk deleted) ... */
146 mc_creat (unsigned char *path, int mode)
148 unsigned char buffer[MC_MAXPATHLEN];
149 return creat (mule_decode_path (path, buffer), mode);
153 mc_readlink (unsigned char *path, unsigned char *buf, int size)
155 unsigned char buffer[MC_MAXPATHLEN], buffer2[MAXPATHLEN];
158 nread = readlink (mule_decode_path (path, buffer), buffer2, MAXPATHLEN);
164 len = mule_encode_path_1 (buffer2, nread, buffer, sizeof (buffer));
165 if (0 <= len && len <= size)
167 memcpy (buf, buffer, len);
175 mc_chdir (unsigned char *path)
177 unsigned char buffer[MC_MAXPATHLEN];
179 path = mule_decode_path (path, buffer);
182 if ((path[0] != 0) && (path[1] == ':'))
184 int drive = (tolower (path[0]) - 'a');
185 if (getdisk () != drive)
189 /* If path != "/" and path != "a:/" and path ends with slash, remove
192 int len = strlen (path);
194 if (strcmp (path + 1, ":/") && (len > 1) && (path[len - 1] == '/'))
196 if (path != buffer) /* It is not good to modify original path. */
198 memcpy (buffer, path, len - 1); /* no need to copy last /. */
212 mc_getcwd (unsigned char *null, size_t size)
214 unsigned char buffer[MAXPATHLEN];
217 path = (unsigned char *) getcwd ((char *)buffer, MAXPATHLEN);
220 /* here, should be (path == buffer). */
221 path = (unsigned char *) xmalloc (MC_MAXPATHLEN); /* MSDOS */
225 int buffer_length = strlen (buffer) + 1;
227 len = mule_encode_path_1 (buffer, buffer_length, path, MC_MAXPATHLEN);
230 /* conversion failed. use value that is returned from system. */
231 memcpy (path, buffer, buffer_length);
237 #else /* HAVE_GETWD */
239 mc_getwd (unsigned char path[])
246 unsigned char buffer[MC_MAXPATHLEN];
249 len = mule_encode_path_1 (path, strlen (path) + 1, buffer, sizeof buffer);
252 memcpy (path, buffer, len);
257 #endif /* HAVE_GETWD */
260 /* In callproc.c, execvp() is called like this:
261 * execvp (new_argv[0], new_argv);
262 * following implement depends this.
266 mc_execvp (unsigned char *path, unsigned char *argv[])
268 unsigned char buffer[MC_MAXPATHLEN];
269 argv[0] = path = mule_decode_path (path, buffer);
272 #endif /* !NO_MC_EXECVP */
274 static DIRENTRY mcpath_directory_entry;
279 DIRENTRY *dp = &mcpath_directory_entry;
285 dp->d_ino = sp->d_ino;
287 { /* copy d_name with conversion. */
290 len = mule_encode_path_1 (sp->d_name, NAMLEN (sp),
291 dp->d_name, sizeof (dp->d_name) - 1);
296 assert (len < sizeof (dp->d_name));
298 memcpy (dp->d_name, sp->d_name, len);