comparison src/fileio.c @ 19861:163421a61771

(ENCODE_FILE): New macro. (Vfile_name_coding_system): New variable. (syms_of_fileio): Set up Lisp variable. (Fset_visited_file_modtime): Use ENCODE_FILE. (Fcopy_file, Fmake_directory_internal, Fdelete_directory, Fdelete_file) (Frename_file, Fadd_name_to_file, Ffile_exists_p Ffile_executable_p) (Ffile_readable_p, Ffile_writable_p, Faccess_file, Ffile_symlink_p) (Ffile_directory_p, Ffile_accessible_directory_p, Ffile_regular_p) (Ffile_modes, Fset_file_modes, Ffile_newer_than_file_p, Fwrite_region) (Finsert_file_contents, Fverify_visited_file_modtime): Likewise. (Ffile_symlink_p): Decode the file name value.
author Richard M. Stallman <rms@gnu.org>
date Thu, 11 Sep 1997 00:00:01 +0000
parents 380cbf03df01
children 31b8e0e60e2d
comparison
equal deleted inserted replaced
19860:c17fd465ea95 19861:163421a61771
150 #endif 150 #endif
151 151
152 #define min(a, b) ((a) < (b) ? (a) : (b)) 152 #define min(a, b) ((a) < (b) ? (a) : (b))
153 #define max(a, b) ((a) > (b) ? (a) : (b)) 153 #define max(a, b) ((a) > (b) ? (a) : (b))
154 154
155 /* Encode the file name NAME using the specified coding system
156 for file names, if any. */
157 #define ENCODE_FILE(name) \
158 (! NILP (Vfile_name_coding_system) \
159 && XFASTINT (Vfile_name_coding_system) != 0 \
160 ? Fencode_coding_string (name, Vfile_name_coding_system, Qt) \
161 : name)
162
155 /* Nonzero during writing of auto-save files */ 163 /* Nonzero during writing of auto-save files */
156 int auto_saving; 164 int auto_saving;
157 165
158 /* Set by auto_save_1 to mode of original file so Fwrite_region will create 166 /* Set by auto_save_1 to mode of original file so Fwrite_region will create
159 a new file with the same mode as the original */ 167 a new file with the same mode as the original */
160 int auto_save_mode_bits; 168 int auto_save_mode_bits;
169
170 /* Coding system for file names, or nil if none. */
171 Lisp_Object Vfile_name_coding_system;
161 172
162 /* Alist of elements (REGEXP . HANDLER) for file names 173 /* Alist of elements (REGEXP . HANDLER) for file names
163 whose I/O is done with a special handler. */ 174 whose I/O is done with a special handler. */
164 Lisp_Object Vfile_name_handler_alist; 175 Lisp_Object Vfile_name_handler_alist;
165 176
2049 { 2060 {
2050 int ifd, ofd, n; 2061 int ifd, ofd, n;
2051 char buf[16 * 1024]; 2062 char buf[16 * 1024];
2052 struct stat st, out_st; 2063 struct stat st, out_st;
2053 Lisp_Object handler; 2064 Lisp_Object handler;
2054 struct gcpro gcpro1, gcpro2; 2065 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2055 int count = specpdl_ptr - specpdl; 2066 int count = specpdl_ptr - specpdl;
2056 int input_file_statable_p; 2067 int input_file_statable_p;
2057 2068 Lisp_Object encoded_file, encoded_newname;
2058 GCPRO2 (file, newname); 2069
2070 encoded_file = encoded_newname = Qnil;
2071 GCPRO4 (file, newname, encoded_file, encoded_newname);
2059 CHECK_STRING (file, 0); 2072 CHECK_STRING (file, 0);
2060 CHECK_STRING (newname, 1); 2073 CHECK_STRING (newname, 1);
2074
2061 file = Fexpand_file_name (file, Qnil); 2075 file = Fexpand_file_name (file, Qnil);
2062 newname = Fexpand_file_name (newname, Qnil); 2076 newname = Fexpand_file_name (newname, Qnil);
2063 2077
2064 /* If the input file name has special constructs in it, 2078 /* If the input file name has special constructs in it,
2065 call the corresponding file handler. */ 2079 call the corresponding file handler. */
2069 handler = Ffind_file_name_handler (newname, Qcopy_file); 2083 handler = Ffind_file_name_handler (newname, Qcopy_file);
2070 if (!NILP (handler)) 2084 if (!NILP (handler))
2071 RETURN_UNGCPRO (call5 (handler, Qcopy_file, file, newname, 2085 RETURN_UNGCPRO (call5 (handler, Qcopy_file, file, newname,
2072 ok_if_already_exists, keep_date)); 2086 ok_if_already_exists, keep_date));
2073 2087
2088 encoded_file = ENCODE_FILE (file);
2089 encoded_newname = ENCODE_FILE (newname);
2090
2074 if (NILP (ok_if_already_exists) 2091 if (NILP (ok_if_already_exists)
2075 || INTEGERP (ok_if_already_exists)) 2092 || INTEGERP (ok_if_already_exists))
2076 barf_or_query_if_file_exists (newname, "copy to it", 2093 barf_or_query_if_file_exists (encoded_newname, "copy to it",
2077 INTEGERP (ok_if_already_exists), &out_st); 2094 INTEGERP (ok_if_already_exists), &out_st);
2078 else if (stat (XSTRING (newname)->data, &out_st) < 0) 2095 else if (stat (XSTRING (encoded_newname)->data, &out_st) < 0)
2079 out_st.st_mode = 0; 2096 out_st.st_mode = 0;
2080 2097
2081 ifd = open (XSTRING (file)->data, O_RDONLY); 2098 ifd = open (XSTRING (encoded_file)->data, O_RDONLY);
2082 if (ifd < 0) 2099 if (ifd < 0)
2083 report_file_error ("Opening input file", Fcons (file, Qnil)); 2100 report_file_error ("Opening input file", Fcons (file, Qnil));
2084 2101
2085 record_unwind_protect (close_file_unwind, make_number (ifd)); 2102 record_unwind_protect (close_file_unwind, make_number (ifd));
2086 2103
2112 } 2129 }
2113 #endif /* S_ISREG && S_ISLNK */ 2130 #endif /* S_ISREG && S_ISLNK */
2114 2131
2115 #ifdef VMS 2132 #ifdef VMS
2116 /* Create the copy file with the same record format as the input file */ 2133 /* Create the copy file with the same record format as the input file */
2117 ofd = sys_creat (XSTRING (newname)->data, 0666, ifd); 2134 ofd = sys_creat (XSTRING (encoded_newname)->data, 0666, ifd);
2118 #else 2135 #else
2119 #ifdef MSDOS 2136 #ifdef MSDOS
2120 /* System's default file type was set to binary by _fmode in emacs.c. */ 2137 /* System's default file type was set to binary by _fmode in emacs.c. */
2121 ofd = creat (XSTRING (newname)->data, S_IREAD | S_IWRITE); 2138 ofd = creat (XSTRING (encoded_newname)->data, S_IREAD | S_IWRITE);
2122 #else /* not MSDOS */ 2139 #else /* not MSDOS */
2123 ofd = creat (XSTRING (newname)->data, 0666); 2140 ofd = creat (XSTRING (encoded_newname)->data, 0666);
2124 #endif /* not MSDOS */ 2141 #endif /* not MSDOS */
2125 #endif /* VMS */ 2142 #endif /* VMS */
2126 if (ofd < 0) 2143 if (ofd < 0)
2127 report_file_error ("Opening output file", Fcons (newname, Qnil)); 2144 report_file_error ("Opening output file", Fcons (newname, Qnil));
2128 2145
2144 if (!NILP (keep_date)) 2161 if (!NILP (keep_date))
2145 { 2162 {
2146 EMACS_TIME atime, mtime; 2163 EMACS_TIME atime, mtime;
2147 EMACS_SET_SECS_USECS (atime, st.st_atime, 0); 2164 EMACS_SET_SECS_USECS (atime, st.st_atime, 0);
2148 EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0); 2165 EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
2149 if (set_file_times (XSTRING (newname)->data, atime, mtime)) 2166 if (set_file_times (XSTRING (encoded_newname)->data,
2167 atime, mtime))
2150 Fsignal (Qfile_date_error, 2168 Fsignal (Qfile_date_error,
2151 Fcons (build_string ("Cannot set file date"), 2169 Fcons (build_string ("Cannot set file date"),
2152 Fcons (newname, Qnil))); 2170 Fcons (newname, Qnil)));
2153 } 2171 }
2154 #ifndef MSDOS 2172 #ifndef MSDOS
2155 chmod (XSTRING (newname)->data, st.st_mode & 07777); 2173 chmod (XSTRING (encoded_newname)->data, st.st_mode & 07777);
2156 #else /* MSDOS */ 2174 #else /* MSDOS */
2157 #if defined (__DJGPP__) && __DJGPP__ > 1 2175 #if defined (__DJGPP__) && __DJGPP__ > 1
2158 /* In DJGPP v2.0 and later, fstat usually returns true file mode bits, 2176 /* In DJGPP v2.0 and later, fstat usually returns true file mode bits,
2159 and if it can't, it tells so. Otherwise, under MSDOS we usually 2177 and if it can't, it tells so. Otherwise, under MSDOS we usually
2160 get only the READ bit, which will make the copied file read-only, 2178 get only the READ bit, which will make the copied file read-only,
2161 so it's better not to chmod at all. */ 2179 so it's better not to chmod at all. */
2162 if ((_djstat_flags & _STFAIL_WRITEBIT) == 0) 2180 if ((_djstat_flags & _STFAIL_WRITEBIT) == 0)
2163 chmod (XSTRING (newname)->data, st.st_mode & 07777); 2181 chmod (XSTRING (encoded_newname)->data, st.st_mode & 07777);
2164 #endif /* DJGPP version 2 or newer */ 2182 #endif /* DJGPP version 2 or newer */
2165 #endif /* MSDOS */ 2183 #endif /* MSDOS */
2166 } 2184 }
2167 2185
2168 close (ifd); 2186 close (ifd);
2180 (directory) 2198 (directory)
2181 Lisp_Object directory; 2199 Lisp_Object directory;
2182 { 2200 {
2183 unsigned char *dir; 2201 unsigned char *dir;
2184 Lisp_Object handler; 2202 Lisp_Object handler;
2203 Lisp_Object encoded_dir;
2185 2204
2186 CHECK_STRING (directory, 0); 2205 CHECK_STRING (directory, 0);
2187 directory = Fexpand_file_name (directory, Qnil); 2206 directory = Fexpand_file_name (directory, Qnil);
2188 2207
2189 handler = Ffind_file_name_handler (directory, Qmake_directory_internal); 2208 handler = Ffind_file_name_handler (directory, Qmake_directory_internal);
2190 if (!NILP (handler)) 2209 if (!NILP (handler))
2191 return call2 (handler, Qmake_directory_internal, directory); 2210 return call2 (handler, Qmake_directory_internal, directory);
2192 2211
2193 dir = XSTRING (directory)->data; 2212 encoded_dir = ENCODE_FILE (directory);
2213
2214 dir = XSTRING (encoded_dir)->data;
2194 2215
2195 #ifdef WINDOWSNT 2216 #ifdef WINDOWSNT
2196 if (mkdir (dir) != 0) 2217 if (mkdir (dir) != 0)
2197 #else 2218 #else
2198 if (mkdir (dir, 0777) != 0) 2219 if (mkdir (dir, 0777) != 0)
2207 (directory) 2228 (directory)
2208 Lisp_Object directory; 2229 Lisp_Object directory;
2209 { 2230 {
2210 unsigned char *dir; 2231 unsigned char *dir;
2211 Lisp_Object handler; 2232 Lisp_Object handler;
2233 Lisp_Object encoded_dir;
2212 2234
2213 CHECK_STRING (directory, 0); 2235 CHECK_STRING (directory, 0);
2214 directory = Fdirectory_file_name (Fexpand_file_name (directory, Qnil)); 2236 directory = Fdirectory_file_name (Fexpand_file_name (directory, Qnil));
2215 dir = XSTRING (directory)->data;
2216 2237
2217 handler = Ffind_file_name_handler (directory, Qdelete_directory); 2238 handler = Ffind_file_name_handler (directory, Qdelete_directory);
2218 if (!NILP (handler)) 2239 if (!NILP (handler))
2219 return call2 (handler, Qdelete_directory, directory); 2240 return call2 (handler, Qdelete_directory, directory);
2241
2242 encoded_dir = ENCODE_FILE (directory);
2243
2244 dir = XSTRING (encoded_dir)->data;
2220 2245
2221 if (rmdir (dir) != 0) 2246 if (rmdir (dir) != 0)
2222 report_file_error ("Removing directory", Flist (1, &directory)); 2247 report_file_error ("Removing directory", Flist (1, &directory));
2223 2248
2224 return Qnil; 2249 return Qnil;
2229 If file has multiple names, it continues to exist with the other names.") 2254 If file has multiple names, it continues to exist with the other names.")
2230 (filename) 2255 (filename)
2231 Lisp_Object filename; 2256 Lisp_Object filename;
2232 { 2257 {
2233 Lisp_Object handler; 2258 Lisp_Object handler;
2259 Lisp_Object encoded_file;
2260
2234 CHECK_STRING (filename, 0); 2261 CHECK_STRING (filename, 0);
2235 filename = Fexpand_file_name (filename, Qnil); 2262 filename = Fexpand_file_name (filename, Qnil);
2236 2263
2237 handler = Ffind_file_name_handler (filename, Qdelete_file); 2264 handler = Ffind_file_name_handler (filename, Qdelete_file);
2238 if (!NILP (handler)) 2265 if (!NILP (handler))
2239 return call2 (handler, Qdelete_file, filename); 2266 return call2 (handler, Qdelete_file, filename);
2240 2267
2241 if (0 > unlink (XSTRING (filename)->data)) 2268 encoded_file = ENCODE_FILE (filename);
2269
2270 if (0 > unlink (XSTRING (encoded_file)->data))
2242 report_file_error ("Removing old name", Flist (1, &filename)); 2271 report_file_error ("Removing old name", Flist (1, &filename));
2243 return Qnil; 2272 return Qnil;
2244 } 2273 }
2245 2274
2246 static Lisp_Object 2275 static Lisp_Object
2273 { 2302 {
2274 #ifdef NO_ARG_ARRAY 2303 #ifdef NO_ARG_ARRAY
2275 Lisp_Object args[2]; 2304 Lisp_Object args[2];
2276 #endif 2305 #endif
2277 Lisp_Object handler; 2306 Lisp_Object handler;
2278 struct gcpro gcpro1, gcpro2; 2307 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2279 2308 Lisp_Object encoded_file, encoded_newname;
2280 GCPRO2 (file, newname); 2309
2310 encoded_file = encoded_newname = Qnil;
2311 GCPRO4 (file, newname, encoded_file, encoded_newname);
2281 CHECK_STRING (file, 0); 2312 CHECK_STRING (file, 0);
2282 CHECK_STRING (newname, 1); 2313 CHECK_STRING (newname, 1);
2283 file = Fexpand_file_name (file, Qnil); 2314 file = Fexpand_file_name (file, Qnil);
2284 newname = Fexpand_file_name (newname, Qnil); 2315 newname = Fexpand_file_name (newname, Qnil);
2285 2316
2290 handler = Ffind_file_name_handler (newname, Qrename_file); 2321 handler = Ffind_file_name_handler (newname, Qrename_file);
2291 if (!NILP (handler)) 2322 if (!NILP (handler))
2292 RETURN_UNGCPRO (call4 (handler, Qrename_file, 2323 RETURN_UNGCPRO (call4 (handler, Qrename_file,
2293 file, newname, ok_if_already_exists)); 2324 file, newname, ok_if_already_exists));
2294 2325
2326 encoded_file = ENCODE_FILE (file);
2327 encoded_newname = ENCODE_FILE (newname);
2328
2295 if (NILP (ok_if_already_exists) 2329 if (NILP (ok_if_already_exists)
2296 || INTEGERP (ok_if_already_exists)) 2330 || INTEGERP (ok_if_already_exists))
2297 barf_or_query_if_file_exists (newname, "rename to it", 2331 barf_or_query_if_file_exists (encoded_newname, "rename to it",
2298 INTEGERP (ok_if_already_exists), 0); 2332 INTEGERP (ok_if_already_exists), 0);
2299 #ifndef BSD4_1 2333 #ifndef BSD4_1
2300 if (0 > rename (XSTRING (file)->data, XSTRING (newname)->data)) 2334 if (0 > rename (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data))
2301 #else 2335 #else
2302 if (0 > link (XSTRING (file)->data, XSTRING (newname)->data) 2336 if (0 > link (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data)
2303 || 0 > unlink (XSTRING (file)->data)) 2337 || 0 > unlink (XSTRING (encoded_file)->data))
2304 #endif 2338 #endif
2305 { 2339 {
2306 if (errno == EXDEV) 2340 if (errno == EXDEV)
2307 { 2341 {
2308 Fcopy_file (file, newname, 2342 Fcopy_file (file, newname,
2338 { 2372 {
2339 #ifdef NO_ARG_ARRAY 2373 #ifdef NO_ARG_ARRAY
2340 Lisp_Object args[2]; 2374 Lisp_Object args[2];
2341 #endif 2375 #endif
2342 Lisp_Object handler; 2376 Lisp_Object handler;
2343 struct gcpro gcpro1, gcpro2; 2377 Lisp_Object encoded_file, encoded_newname;
2344 2378 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2345 GCPRO2 (file, newname); 2379
2380 GCPRO4 (file, newname, encoded_file, encoded_newname);
2381 encoded_file = encoded_newname = Qnil;
2346 CHECK_STRING (file, 0); 2382 CHECK_STRING (file, 0);
2347 CHECK_STRING (newname, 1); 2383 CHECK_STRING (newname, 1);
2348 file = Fexpand_file_name (file, Qnil); 2384 file = Fexpand_file_name (file, Qnil);
2349 newname = Fexpand_file_name (newname, Qnil); 2385 newname = Fexpand_file_name (newname, Qnil);
2350 2386
2360 handler = Ffind_file_name_handler (newname, Qadd_name_to_file); 2396 handler = Ffind_file_name_handler (newname, Qadd_name_to_file);
2361 if (!NILP (handler)) 2397 if (!NILP (handler))
2362 RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, 2398 RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file,
2363 newname, ok_if_already_exists)); 2399 newname, ok_if_already_exists));
2364 2400
2401 encoded_file = ENCODE_FILE (file);
2402 encoded_newname = ENCODE_FILE (newname);
2403
2365 if (NILP (ok_if_already_exists) 2404 if (NILP (ok_if_already_exists)
2366 || INTEGERP (ok_if_already_exists)) 2405 || INTEGERP (ok_if_already_exists))
2367 barf_or_query_if_file_exists (newname, "make it a new name", 2406 barf_or_query_if_file_exists (encoded_newname, "make it a new name",
2368 INTEGERP (ok_if_already_exists), 0); 2407 INTEGERP (ok_if_already_exists), 0);
2369 2408
2370 unlink (XSTRING (newname)->data); 2409 unlink (XSTRING (newname)->data);
2371 if (0 > link (XSTRING (file)->data, XSTRING (newname)->data)) 2410 if (0 > link (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data))
2372 { 2411 {
2373 #ifdef NO_ARG_ARRAY 2412 #ifdef NO_ARG_ARRAY
2374 args[0] = file; 2413 args[0] = file;
2375 args[1] = newname; 2414 args[1] = newname;
2376 report_file_error ("Adding new name", Flist (2, args)); 2415 report_file_error ("Adding new name", Flist (2, args));
2396 { 2435 {
2397 #ifdef NO_ARG_ARRAY 2436 #ifdef NO_ARG_ARRAY
2398 Lisp_Object args[2]; 2437 Lisp_Object args[2];
2399 #endif 2438 #endif
2400 Lisp_Object handler; 2439 Lisp_Object handler;
2401 struct gcpro gcpro1, gcpro2; 2440 Lisp_Object encoded_filename, encoded_linkname;
2402 2441 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2403 GCPRO2 (filename, linkname); 2442
2443 GCPRO4 (filename, linkname, encoded_filename, encoded_linkname);
2444 encoded_filename = encoded_linkname = Qnil;
2404 CHECK_STRING (filename, 0); 2445 CHECK_STRING (filename, 0);
2405 CHECK_STRING (linkname, 1); 2446 CHECK_STRING (linkname, 1);
2406 /* If the link target has a ~, we must expand it to get 2447 /* If the link target has a ~, we must expand it to get
2407 a truly valid file name. Otherwise, do not expand; 2448 a truly valid file name. Otherwise, do not expand;
2408 we want to permit links to relative file names. */ 2449 we want to permit links to relative file names. */
2422 handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link); 2463 handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link);
2423 if (!NILP (handler)) 2464 if (!NILP (handler))
2424 RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, 2465 RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename,
2425 linkname, ok_if_already_exists)); 2466 linkname, ok_if_already_exists));
2426 2467
2468 encoded_filename = ENCODE_FILE (filename);
2469 encoded_linkname = ENCODE_FILE (linkname);
2470
2427 if (NILP (ok_if_already_exists) 2471 if (NILP (ok_if_already_exists)
2428 || INTEGERP (ok_if_already_exists)) 2472 || INTEGERP (ok_if_already_exists))
2429 barf_or_query_if_file_exists (linkname, "make it a link", 2473 barf_or_query_if_file_exists (encoded_linkname, "make it a link",
2430 INTEGERP (ok_if_already_exists), 0); 2474 INTEGERP (ok_if_already_exists), 0);
2431 if (0 > symlink (XSTRING (filename)->data, XSTRING (linkname)->data)) 2475 if (0 > symlink (XSTRING (encoded_filename)->data,
2476 XSTRING (encoded_linkname)->data))
2432 { 2477 {
2433 /* If we didn't complain already, silently delete existing file. */ 2478 /* If we didn't complain already, silently delete existing file. */
2434 if (errno == EEXIST) 2479 if (errno == EEXIST)
2435 { 2480 {
2436 unlink (XSTRING (linkname)->data); 2481 unlink (XSTRING (encoded_linkname)->data);
2437 if (0 <= symlink (XSTRING (filename)->data, XSTRING (linkname)->data)) 2482 if (0 <= symlink (XSTRING (encoded_filename)->data,
2483 XSTRING (encoded_linkname)->data))
2438 { 2484 {
2439 UNGCPRO; 2485 UNGCPRO;
2440 return Qnil; 2486 return Qnil;
2441 } 2487 }
2442 } 2488 }
2605 call the corresponding file handler. */ 2651 call the corresponding file handler. */
2606 handler = Ffind_file_name_handler (absname, Qfile_exists_p); 2652 handler = Ffind_file_name_handler (absname, Qfile_exists_p);
2607 if (!NILP (handler)) 2653 if (!NILP (handler))
2608 return call2 (handler, Qfile_exists_p, absname); 2654 return call2 (handler, Qfile_exists_p, absname);
2609 2655
2656 absname = ENCODE_FILE (absname);
2657
2610 return (stat (XSTRING (absname)->data, &statbuf) >= 0) ? Qt : Qnil; 2658 return (stat (XSTRING (absname)->data, &statbuf) >= 0) ? Qt : Qnil;
2611 } 2659 }
2612 2660
2613 DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0, 2661 DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0,
2614 "Return t if FILENAME can be executed by you.\n\ 2662 "Return t if FILENAME can be executed by you.\n\
2627 call the corresponding file handler. */ 2675 call the corresponding file handler. */
2628 handler = Ffind_file_name_handler (absname, Qfile_executable_p); 2676 handler = Ffind_file_name_handler (absname, Qfile_executable_p);
2629 if (!NILP (handler)) 2677 if (!NILP (handler))
2630 return call2 (handler, Qfile_executable_p, absname); 2678 return call2 (handler, Qfile_executable_p, absname);
2631 2679
2680 absname = ENCODE_FILE (absname);
2681
2632 return (check_executable (XSTRING (absname)->data) ? Qt : Qnil); 2682 return (check_executable (XSTRING (absname)->data) ? Qt : Qnil);
2633 } 2683 }
2634 2684
2635 DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0, 2685 DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0,
2636 "Return t if file FILENAME exists and you can read it.\n\ 2686 "Return t if file FILENAME exists and you can read it.\n\
2650 /* If the file name has special constructs in it, 2700 /* If the file name has special constructs in it,
2651 call the corresponding file handler. */ 2701 call the corresponding file handler. */
2652 handler = Ffind_file_name_handler (absname, Qfile_readable_p); 2702 handler = Ffind_file_name_handler (absname, Qfile_readable_p);
2653 if (!NILP (handler)) 2703 if (!NILP (handler))
2654 return call2 (handler, Qfile_readable_p, absname); 2704 return call2 (handler, Qfile_readable_p, absname);
2705
2706 absname = ENCODE_FILE (absname);
2655 2707
2656 #ifdef DOS_NT 2708 #ifdef DOS_NT
2657 /* Under MS-DOS and Windows, open does not work for directories. */ 2709 /* Under MS-DOS and Windows, open does not work for directories. */
2658 if (access (XSTRING (absname)->data, 0) == 0) 2710 if (access (XSTRING (absname)->data, 0) == 0)
2659 return Qt; 2711 return Qt;
2683 DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0, 2735 DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
2684 "Return t if file FILENAME can be written or created by you.") 2736 "Return t if file FILENAME can be written or created by you.")
2685 (filename) 2737 (filename)
2686 Lisp_Object filename; 2738 Lisp_Object filename;
2687 { 2739 {
2688 Lisp_Object absname, dir; 2740 Lisp_Object absname, dir, encoded;
2689 Lisp_Object handler; 2741 Lisp_Object handler;
2690 struct stat statbuf; 2742 struct stat statbuf;
2691 2743
2692 CHECK_STRING (filename, 0); 2744 CHECK_STRING (filename, 0);
2693 absname = Fexpand_file_name (filename, Qnil); 2745 absname = Fexpand_file_name (filename, Qnil);
2696 call the corresponding file handler. */ 2748 call the corresponding file handler. */
2697 handler = Ffind_file_name_handler (absname, Qfile_writable_p); 2749 handler = Ffind_file_name_handler (absname, Qfile_writable_p);
2698 if (!NILP (handler)) 2750 if (!NILP (handler))
2699 return call2 (handler, Qfile_writable_p, absname); 2751 return call2 (handler, Qfile_writable_p, absname);
2700 2752
2701 if (stat (XSTRING (absname)->data, &statbuf) >= 0) 2753 encoded = ENCODE_FILE (absname);
2702 return (check_writable (XSTRING (absname)->data) 2754 if (stat (XSTRING (encoded)->data, &statbuf) >= 0)
2755 return (check_writable (XSTRING (encoded)->data)
2703 ? Qt : Qnil); 2756 ? Qt : Qnil);
2757
2704 dir = Ffile_name_directory (absname); 2758 dir = Ffile_name_directory (absname);
2705 #ifdef VMS 2759 #ifdef VMS
2706 if (!NILP (dir)) 2760 if (!NILP (dir))
2707 dir = Fdirectory_file_name (dir); 2761 dir = Fdirectory_file_name (dir);
2708 #endif /* VMS */ 2762 #endif /* VMS */
2709 #ifdef MSDOS 2763 #ifdef MSDOS
2710 if (!NILP (dir)) 2764 if (!NILP (dir))
2711 dir = Fdirectory_file_name (dir); 2765 dir = Fdirectory_file_name (dir);
2712 #endif /* MSDOS */ 2766 #endif /* MSDOS */
2767
2768 dir = ENCODE_FILE (dir);
2713 return (check_writable (!NILP (dir) ? (char *) XSTRING (dir)->data : "") 2769 return (check_writable (!NILP (dir) ? (char *) XSTRING (dir)->data : "")
2714 ? Qt : Qnil); 2770 ? Qt : Qnil);
2715 } 2771 }
2716 2772
2717 DEFUN ("access-file", Faccess_file, Saccess_file, 2, 2, 0, 2773 DEFUN ("access-file", Faccess_file, Saccess_file, 2, 2, 0,
2719 The second argument STRING is used in the error message.\n\ 2775 The second argument STRING is used in the error message.\n\
2720 If there is no error, we return nil.") 2776 If there is no error, we return nil.")
2721 (filename, string) 2777 (filename, string)
2722 Lisp_Object filename, string; 2778 Lisp_Object filename, string;
2723 { 2779 {
2724 Lisp_Object handler; 2780 Lisp_Object handler, encoded_filename;
2725 int fd; 2781 int fd;
2726 2782
2727 CHECK_STRING (filename, 0); 2783 CHECK_STRING (filename, 0);
2728 2784
2729 /* If the file name has special constructs in it, 2785 /* If the file name has special constructs in it,
2730 call the corresponding file handler. */ 2786 call the corresponding file handler. */
2731 handler = Ffind_file_name_handler (filename, Qaccess_file); 2787 handler = Ffind_file_name_handler (filename, Qaccess_file);
2732 if (!NILP (handler)) 2788 if (!NILP (handler))
2733 return call3 (handler, Qaccess_file, filename, string); 2789 return call3 (handler, Qaccess_file, filename, string);
2734 2790
2735 fd = open (XSTRING (filename)->data, O_RDONLY); 2791 encoded_filename = ENCODE_FILE (filename);
2792
2793 fd = open (XSTRING (encoded_filename)->data, O_RDONLY);
2736 if (fd < 0) 2794 if (fd < 0)
2737 report_file_error (XSTRING (string)->data, Fcons (filename, Qnil)); 2795 report_file_error (XSTRING (string)->data, Fcons (filename, Qnil));
2738 close (fd); 2796 close (fd);
2739 2797
2740 return Qnil; 2798 return Qnil;
2761 call the corresponding file handler. */ 2819 call the corresponding file handler. */
2762 handler = Ffind_file_name_handler (filename, Qfile_symlink_p); 2820 handler = Ffind_file_name_handler (filename, Qfile_symlink_p);
2763 if (!NILP (handler)) 2821 if (!NILP (handler))
2764 return call2 (handler, Qfile_symlink_p, filename); 2822 return call2 (handler, Qfile_symlink_p, filename);
2765 2823
2824 filename = ENCODE_FILE (filename);
2825
2766 bufsize = 100; 2826 bufsize = 100;
2767 while (1) 2827 while (1)
2768 { 2828 {
2769 buf = (char *) xmalloc (bufsize); 2829 buf = (char *) xmalloc (bufsize);
2770 bzero (buf, bufsize); 2830 bzero (buf, bufsize);
2779 xfree (buf); 2839 xfree (buf);
2780 return Qnil; 2840 return Qnil;
2781 } 2841 }
2782 val = make_string (buf, valsize); 2842 val = make_string (buf, valsize);
2783 xfree (buf); 2843 xfree (buf);
2784 return val; 2844 return Fdecode_coding_string (val, Vfile_name_coding_system, Qt);
2785 #else /* not S_IFLNK */ 2845 #else /* not S_IFLNK */
2786 return Qnil; 2846 return Qnil;
2787 #endif /* not S_IFLNK */ 2847 #endif /* not S_IFLNK */
2788 } 2848 }
2789 2849
2801 /* If the file name has special constructs in it, 2861 /* If the file name has special constructs in it,
2802 call the corresponding file handler. */ 2862 call the corresponding file handler. */
2803 handler = Ffind_file_name_handler (absname, Qfile_directory_p); 2863 handler = Ffind_file_name_handler (absname, Qfile_directory_p);
2804 if (!NILP (handler)) 2864 if (!NILP (handler))
2805 return call2 (handler, Qfile_directory_p, absname); 2865 return call2 (handler, Qfile_directory_p, absname);
2866
2867 absname = ENCODE_FILE (absname);
2806 2868
2807 if (stat (XSTRING (absname)->data, &st) < 0) 2869 if (stat (XSTRING (absname)->data, &st) < 0)
2808 return Qnil; 2870 return Qnil;
2809 return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil; 2871 return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
2810 } 2872 }
2858 call the corresponding file handler. */ 2920 call the corresponding file handler. */
2859 handler = Ffind_file_name_handler (absname, Qfile_regular_p); 2921 handler = Ffind_file_name_handler (absname, Qfile_regular_p);
2860 if (!NILP (handler)) 2922 if (!NILP (handler))
2861 return call2 (handler, Qfile_regular_p, absname); 2923 return call2 (handler, Qfile_regular_p, absname);
2862 2924
2925 absname = ENCODE_FILE (absname);
2926
2863 if (stat (XSTRING (absname)->data, &st) < 0) 2927 if (stat (XSTRING (absname)->data, &st) < 0)
2864 return Qnil; 2928 return Qnil;
2865 return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil; 2929 return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
2866 } 2930 }
2867 2931
2879 /* If the file name has special constructs in it, 2943 /* If the file name has special constructs in it,
2880 call the corresponding file handler. */ 2944 call the corresponding file handler. */
2881 handler = Ffind_file_name_handler (absname, Qfile_modes); 2945 handler = Ffind_file_name_handler (absname, Qfile_modes);
2882 if (!NILP (handler)) 2946 if (!NILP (handler))
2883 return call2 (handler, Qfile_modes, absname); 2947 return call2 (handler, Qfile_modes, absname);
2948
2949 absname = ENCODE_FILE (absname);
2884 2950
2885 if (stat (XSTRING (absname)->data, &st) < 0) 2951 if (stat (XSTRING (absname)->data, &st) < 0)
2886 return Qnil; 2952 return Qnil;
2887 #if defined (MSDOS) && __DJGPP__ < 2 2953 #if defined (MSDOS) && __DJGPP__ < 2
2888 if (check_executable (XSTRING (absname)->data)) 2954 if (check_executable (XSTRING (absname)->data))
2896 "Set mode bits of file named FILENAME to MODE (an integer).\n\ 2962 "Set mode bits of file named FILENAME to MODE (an integer).\n\
2897 Only the 12 low bits of MODE are used.") 2963 Only the 12 low bits of MODE are used.")
2898 (filename, mode) 2964 (filename, mode)
2899 Lisp_Object filename, mode; 2965 Lisp_Object filename, mode;
2900 { 2966 {
2901 Lisp_Object absname; 2967 Lisp_Object absname, encoded_absname;
2902 Lisp_Object handler; 2968 Lisp_Object handler;
2903 2969
2904 absname = Fexpand_file_name (filename, current_buffer->directory); 2970 absname = Fexpand_file_name (filename, current_buffer->directory);
2905 CHECK_NUMBER (mode, 1); 2971 CHECK_NUMBER (mode, 1);
2906 2972
2908 call the corresponding file handler. */ 2974 call the corresponding file handler. */
2909 handler = Ffind_file_name_handler (absname, Qset_file_modes); 2975 handler = Ffind_file_name_handler (absname, Qset_file_modes);
2910 if (!NILP (handler)) 2976 if (!NILP (handler))
2911 return call3 (handler, Qset_file_modes, absname, mode); 2977 return call3 (handler, Qset_file_modes, absname, mode);
2912 2978
2913 if (chmod (XSTRING (absname)->data, XINT (mode)) < 0) 2979 encoded_absname = ENCODE_FILE (absname);
2980
2981 if (chmod (XSTRING (encoded_absname)->data, XINT (mode)) < 0)
2914 report_file_error ("Doing chmod", Fcons (absname, Qnil)); 2982 report_file_error ("Doing chmod", Fcons (absname, Qnil));
2915 2983
2916 return Qnil; 2984 return Qnil;
2917 } 2985 }
2918 2986
2984 handler = Ffind_file_name_handler (absname1, Qfile_newer_than_file_p); 3052 handler = Ffind_file_name_handler (absname1, Qfile_newer_than_file_p);
2985 if (NILP (handler)) 3053 if (NILP (handler))
2986 handler = Ffind_file_name_handler (absname2, Qfile_newer_than_file_p); 3054 handler = Ffind_file_name_handler (absname2, Qfile_newer_than_file_p);
2987 if (!NILP (handler)) 3055 if (!NILP (handler))
2988 return call3 (handler, Qfile_newer_than_file_p, absname1, absname2); 3056 return call3 (handler, Qfile_newer_than_file_p, absname1, absname2);
3057
3058 GCPRO2 (absname1, absname2);
3059 absname1 = ENCODE_FILE (absname1);
3060 absname2 = ENCODE_FILE (absname2);
3061 UNGCPRO;
2989 3062
2990 if (stat (XSTRING (absname1)->data, &st) < 0) 3063 if (stat (XSTRING (absname1)->data, &st) < 0)
2991 return Qnil; 3064 return Qnil;
2992 3065
2993 mtime1 = st.st_mtime; 3066 mtime1 = st.st_mtime;
3036 register int fd; 3109 register int fd;
3037 register int inserted = 0; 3110 register int inserted = 0;
3038 register int how_much; 3111 register int how_much;
3039 register int unprocessed; 3112 register int unprocessed;
3040 int count = specpdl_ptr - specpdl; 3113 int count = specpdl_ptr - specpdl;
3041 struct gcpro gcpro1, gcpro2, gcpro3; 3114 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3042 Lisp_Object handler, val, insval; 3115 Lisp_Object handler, val, insval, orig_filename;
3043 Lisp_Object p; 3116 Lisp_Object p;
3044 int total; 3117 int total;
3045 int not_regular = 0; 3118 int not_regular = 0;
3046 char read_buf[READ_BUF_SIZE]; 3119 char read_buf[READ_BUF_SIZE];
3047 struct coding_system coding; 3120 struct coding_system coding;
3054 if (!NILP (current_buffer->read_only)) 3127 if (!NILP (current_buffer->read_only))
3055 Fbarf_if_buffer_read_only (); 3128 Fbarf_if_buffer_read_only ();
3056 3129
3057 val = Qnil; 3130 val = Qnil;
3058 p = Qnil; 3131 p = Qnil;
3059 3132 orig_filename = Qnil;
3060 GCPRO3 (filename, val, p); 3133
3134 GCPRO4 (filename, val, p, orig_filename);
3061 3135
3062 CHECK_STRING (filename, 0); 3136 CHECK_STRING (filename, 0);
3063 filename = Fexpand_file_name (filename, Qnil); 3137 filename = Fexpand_file_name (filename, Qnil);
3064 3138
3065 /* If the file name has special constructs in it, 3139 /* If the file name has special constructs in it,
3069 { 3143 {
3070 val = call6 (handler, Qinsert_file_contents, filename, 3144 val = call6 (handler, Qinsert_file_contents, filename,
3071 visit, beg, end, replace); 3145 visit, beg, end, replace);
3072 goto handled; 3146 goto handled;
3073 } 3147 }
3148
3149 orig_filename = filename;
3150 filename = ENCODE_FILE (filename);
3074 3151
3075 fd = -1; 3152 fd = -1;
3076 3153
3077 #ifndef APOLLO 3154 #ifndef APOLLO
3078 if (stat (XSTRING (filename)->data, &st) < 0) 3155 if (stat (XSTRING (filename)->data, &st) < 0)
3082 #endif /* not APOLLO */ 3159 #endif /* not APOLLO */
3083 { 3160 {
3084 if (fd >= 0) close (fd); 3161 if (fd >= 0) close (fd);
3085 badopen: 3162 badopen:
3086 if (NILP (visit)) 3163 if (NILP (visit))
3087 report_file_error ("Opening input file", Fcons (filename, Qnil)); 3164 report_file_error ("Opening input file", Fcons (orig_filename, Qnil));
3088 st.st_mtime = -1; 3165 st.st_mtime = -1;
3089 how_much = 0; 3166 how_much = 0;
3090 goto notfound; 3167 goto notfound;
3091 } 3168 }
3092 3169
3102 goto notfound; 3179 goto notfound;
3103 3180
3104 if (! NILP (replace) || ! NILP (beg) || ! NILP (end)) 3181 if (! NILP (replace) || ! NILP (beg) || ! NILP (end))
3105 Fsignal (Qfile_error, 3182 Fsignal (Qfile_error,
3106 Fcons (build_string ("not a regular file"), 3183 Fcons (build_string ("not a regular file"),
3107 Fcons (filename, Qnil))); 3184 Fcons (orig_filename, Qnil)));
3108 } 3185 }
3109 #endif 3186 #endif
3110 3187
3111 if (fd < 0) 3188 if (fd < 0)
3112 if ((fd = open (XSTRING (filename)->data, O_RDONLY)) < 0) 3189 if ((fd = open (XSTRING (filename)->data, O_RDONLY)) < 0)
3168 nread = read (fd, read_buf, 1024); 3245 nread = read (fd, read_buf, 1024);
3169 if (nread >= 0) 3246 if (nread >= 0)
3170 { 3247 {
3171 if (lseek (fd, st.st_size - (1024 * 3), 0) < 0) 3248 if (lseek (fd, st.st_size - (1024 * 3), 0) < 0)
3172 report_file_error ("Setting file position", 3249 report_file_error ("Setting file position",
3173 Fcons (filename, Qnil)); 3250 Fcons (orig_filename, Qnil));
3174 nread += read (fd, read_buf + nread, 1024 * 3); 3251 nread += read (fd, read_buf + nread, 1024 * 3);
3175 } 3252 }
3176 } 3253 }
3177 3254
3178 if (nread < 0) 3255 if (nread < 0)
3179 error ("IO error reading %s: %s", 3256 error ("IO error reading %s: %s",
3180 XSTRING (filename)->data, strerror (errno)); 3257 XSTRING (orig_filename)->data, strerror (errno));
3181 else if (nread > 0) 3258 else if (nread > 0)
3182 { 3259 {
3183 val = call1 (Vset_auto_coding_function, 3260 val = call1 (Vset_auto_coding_function,
3184 make_string (read_buf, nread)); 3261 make_string (read_buf, nread));
3185 /* Rewind the file for the actual read done later. */ 3262 /* Rewind the file for the actual read done later. */
3186 if (lseek (fd, 0, 0) < 0) 3263 if (lseek (fd, 0, 0) < 0)
3187 report_file_error ("Setting file position", 3264 report_file_error ("Setting file position",
3188 Fcons (filename, Qnil)); 3265 Fcons (orig_filename, Qnil));
3189 } 3266 }
3190 } 3267 }
3191 if (NILP (val)) 3268 if (NILP (val))
3192 { 3269 {
3193 Lisp_Object args[6], coding_systems; 3270 Lisp_Object args[6], coding_systems;
3194 3271
3195 args[0] = Qinsert_file_contents, args[1] = filename, 3272 args[0] = Qinsert_file_contents, args[1] = orig_filename,
3196 args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace; 3273 args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace;
3197 coding_systems = Ffind_operation_coding_system (6, args); 3274 coding_systems = Ffind_operation_coding_system (6, args);
3198 if (CONSP (coding_systems)) val = XCONS (coding_systems)->car; 3275 if (CONSP (coding_systems)) val = XCONS (coding_systems)->car;
3199 } 3276 }
3200 } 3277 }
3228 3305
3229 if (XINT (beg) != 0) 3306 if (XINT (beg) != 0)
3230 { 3307 {
3231 if (lseek (fd, XINT (beg), 0) < 0) 3308 if (lseek (fd, XINT (beg), 0) < 0)
3232 report_file_error ("Setting file position", 3309 report_file_error ("Setting file position",
3233 Fcons (filename, Qnil)); 3310 Fcons (orig_filename, Qnil));
3234 } 3311 }
3235 3312
3236 immediate_quit = 1; 3313 immediate_quit = 1;
3237 QUIT; 3314 QUIT;
3238 /* Count how many chars at the start of the file 3315 /* Count how many chars at the start of the file
3242 int nread, bufpos; 3319 int nread, bufpos;
3243 3320
3244 nread = read (fd, buffer, sizeof buffer); 3321 nread = read (fd, buffer, sizeof buffer);
3245 if (nread < 0) 3322 if (nread < 0)
3246 error ("IO error reading %s: %s", 3323 error ("IO error reading %s: %s",
3247 XSTRING (filename)->data, strerror (errno)); 3324 XSTRING (orig_filename)->data, strerror (errno));
3248 else if (nread == 0) 3325 else if (nread == 0)
3249 break; 3326 break;
3250 3327
3251 if (coding.type == coding_type_undecided) 3328 if (coding.type == coding_type_undecided)
3252 detect_coding (&coding, buffer, nread); 3329 detect_coding (&coding, buffer, nread);
3307 break; 3384 break;
3308 /* How much can we scan in the next step? */ 3385 /* How much can we scan in the next step? */
3309 trial = min (curpos, sizeof buffer); 3386 trial = min (curpos, sizeof buffer);
3310 if (lseek (fd, curpos - trial, 0) < 0) 3387 if (lseek (fd, curpos - trial, 0) < 0)
3311 report_file_error ("Setting file position", 3388 report_file_error ("Setting file position",
3312 Fcons (filename, Qnil)); 3389 Fcons (orig_filename, Qnil));
3313 3390
3314 total_read = 0; 3391 total_read = 0;
3315 while (total_read < trial) 3392 while (total_read < trial)
3316 { 3393 {
3317 nread = read (fd, buffer + total_read, trial - total_read); 3394 nread = read (fd, buffer + total_read, trial - total_read);
3318 if (nread <= 0) 3395 if (nread <= 0)
3319 error ("IO error reading %s: %s", 3396 error ("IO error reading %s: %s",
3320 XSTRING (filename)->data, strerror (errno)); 3397 XSTRING (orig_filename)->data, strerror (errno));
3321 total_read += nread; 3398 total_read += nread;
3322 } 3399 }
3323 /* Scan this bufferful from the end, comparing with 3400 /* Scan this bufferful from the end, comparing with
3324 the Emacs buffer. */ 3401 the Emacs buffer. */
3325 bufpos = total_read; 3402 bufpos = total_read;
3401 3478
3402 if (lseek (fd, XINT (beg), 0) < 0) 3479 if (lseek (fd, XINT (beg), 0) < 0)
3403 { 3480 {
3404 free (conversion_buffer); 3481 free (conversion_buffer);
3405 report_file_error ("Setting file position", 3482 report_file_error ("Setting file position",
3406 Fcons (filename, Qnil)); 3483 Fcons (orig_filename, Qnil));
3407 } 3484 }
3408 3485
3409 total = st.st_size; /* Total bytes in the file. */ 3486 total = st.st_size; /* Total bytes in the file. */
3410 how_much = 0; /* Bytes read from file so far. */ 3487 how_much = 0; /* Bytes read from file so far. */
3411 inserted = 0; /* Bytes put into CONVERSION_BUFFER so far. */ 3488 inserted = 0; /* Bytes put into CONVERSION_BUFFER so far. */
3473 { 3550 {
3474 free (conversion_buffer); 3551 free (conversion_buffer);
3475 3552
3476 if (how_much == -1) 3553 if (how_much == -1)
3477 error ("IO error reading %s: %s", 3554 error ("IO error reading %s: %s",
3478 XSTRING (filename)->data, strerror (errno)); 3555 XSTRING (orig_filename)->data, strerror (errno));
3479 else if (how_much == -2) 3556 else if (how_much == -2)
3480 error ("maximum buffer size exceeded"); 3557 error ("maximum buffer size exceeded");
3481 } 3558 }
3482 3559
3483 /* Compare the beginning of the converted file 3560 /* Compare the beginning of the converted file
3560 make_gap (total - GAP_SIZE); 3637 make_gap (total - GAP_SIZE);
3561 3638
3562 if (XINT (beg) != 0 || !NILP (replace)) 3639 if (XINT (beg) != 0 || !NILP (replace))
3563 { 3640 {
3564 if (lseek (fd, XINT (beg), 0) < 0) 3641 if (lseek (fd, XINT (beg), 0) < 0)
3565 report_file_error ("Setting file position", Fcons (filename, Qnil)); 3642 report_file_error ("Setting file position",
3643 Fcons (orig_filename, Qnil));
3566 } 3644 }
3567 3645
3568 /* In the following loop, HOW_MUCH contains the total bytes read so 3646 /* In the following loop, HOW_MUCH contains the total bytes read so
3569 far. Before exiting the loop, it is set to -1 if I/O error 3647 far. Before exiting the loop, it is set to -1 if I/O error
3570 occurs, set to -2 if the maximum buffer size is exceeded. */ 3648 occurs, set to -2 if the maximum buffer size is exceeded. */
3675 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */ 3753 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
3676 /* Determine file type from name and remove LFs from CR-LFs if the file 3754 /* Determine file type from name and remove LFs from CR-LFs if the file
3677 is deemed to be a text file. */ 3755 is deemed to be a text file. */
3678 { 3756 {
3679 current_buffer->buffer_file_type 3757 current_buffer->buffer_file_type
3680 = call1 (Qfind_buffer_file_type, filename); 3758 = call1 (Qfind_buffer_file_type, orig_filename);
3681 if (NILP (current_buffer->buffer_file_type)) 3759 if (NILP (current_buffer->buffer_file_type))
3682 { 3760 {
3683 int reduced_size 3761 int reduced_size
3684 = inserted - crlf_to_lf (inserted, POS_ADDR (PT - 1) + 1); 3762 = inserted - crlf_to_lf (inserted, POS_ADDR (PT - 1) + 1);
3685 ZV -= reduced_size; 3763 ZV -= reduced_size;
3706 /* Discard the unwind protect for closing the file. */ 3784 /* Discard the unwind protect for closing the file. */
3707 specpdl_ptr--; 3785 specpdl_ptr--;
3708 3786
3709 if (how_much == -1) 3787 if (how_much == -1)
3710 error ("IO error reading %s: %s", 3788 error ("IO error reading %s: %s",
3711 XSTRING (filename)->data, strerror (errno)); 3789 XSTRING (orig_filename)->data, strerror (errno));
3712 else if (how_much == -2) 3790 else if (how_much == -2)
3713 error ("maximum buffer size exceeded"); 3791 error ("maximum buffer size exceeded");
3714 3792
3715 notfound: 3793 notfound:
3716 handled: 3794 handled:
3724 #endif 3802 #endif
3725 3803
3726 if (NILP (handler)) 3804 if (NILP (handler))
3727 { 3805 {
3728 current_buffer->modtime = st.st_mtime; 3806 current_buffer->modtime = st.st_mtime;
3729 current_buffer->filename = filename; 3807 current_buffer->filename = orig_filename;
3730 } 3808 }
3731 3809
3732 SAVE_MODIFF = MODIFF; 3810 SAVE_MODIFF = MODIFF;
3733 current_buffer->auto_save_modified = MODIFF; 3811 current_buffer->auto_save_modified = MODIFF;
3734 XSETFASTINT (current_buffer->save_length, Z - BEG); 3812 XSETFASTINT (current_buffer->save_length, Z - BEG);
3741 } 3819 }
3742 #endif /* CLASH_DETECTION */ 3820 #endif /* CLASH_DETECTION */
3743 if (not_regular) 3821 if (not_regular)
3744 Fsignal (Qfile_error, 3822 Fsignal (Qfile_error,
3745 Fcons (build_string ("not a regular file"), 3823 Fcons (build_string ("not a regular file"),
3746 Fcons (filename, Qnil))); 3824 Fcons (orig_filename, Qnil)));
3747 3825
3748 /* If visiting nonexistent file, return nil. */ 3826 /* If visiting nonexistent file, return nil. */
3749 if (current_buffer->modtime == -1) 3827 if (current_buffer->modtime == -1)
3750 report_file_error ("Opening input file", Fcons (filename, Qnil)); 3828 report_file_error ("Opening input file", Fcons (orig_filename, Qnil));
3751 } 3829 }
3752 3830
3753 /* Decode file format */ 3831 /* Decode file format */
3754 if (inserted > 0) 3832 if (inserted > 0)
3755 { 3833 {
3784 p = Fcdr (p); 3862 p = Fcdr (p);
3785 } 3863 }
3786 } 3864 }
3787 3865
3788 if (NILP (val)) 3866 if (NILP (val))
3789 val = Fcons (filename, 3867 val = Fcons (orig_filename,
3790 Fcons (make_number (inserted), 3868 Fcons (make_number (inserted),
3791 Qnil)); 3869 Qnil));
3792 3870
3793 RETURN_UNGCPRO (unbind_to (count, val)); 3871 RETURN_UNGCPRO (unbind_to (count, val));
3794 } 3872 }
3851 unsigned char *fname = 0; /* If non-0, original filename (must rename) */ 3929 unsigned char *fname = 0; /* If non-0, original filename (must rename) */
3852 #endif /* VMS */ 3930 #endif /* VMS */
3853 Lisp_Object handler; 3931 Lisp_Object handler;
3854 Lisp_Object visit_file; 3932 Lisp_Object visit_file;
3855 Lisp_Object annotations; 3933 Lisp_Object annotations;
3934 Lisp_Object encoded_filename;
3856 int visiting, quietly; 3935 int visiting, quietly;
3857 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; 3936 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
3858 struct buffer *given_buffer; 3937 struct buffer *given_buffer;
3859 #ifdef DOS_NT 3938 #ifdef DOS_NT
3860 int buffer_file_type = O_BINARY; 3939 int buffer_file_type = O_BINARY;
3867 if (!NILP (start) && !STRINGP (start)) 3946 if (!NILP (start) && !STRINGP (start))
3868 validate_region (&start, &end); 3947 validate_region (&start, &end);
3869 3948
3870 GCPRO4 (start, filename, visit, lockname); 3949 GCPRO4 (start, filename, visit, lockname);
3871 3950
3872 /* Decide the coding-system to be encoded to. */ 3951 /* Decide the coding-system to encode the data with. */
3873 { 3952 {
3874 Lisp_Object val; 3953 Lisp_Object val;
3875 3954
3876 if (auto_saving) 3955 if (auto_saving)
3877 val = Qnil; 3956 val = Qnil;
3993 4072
3994 lock_file (lockname); 4073 lock_file (lockname);
3995 } 4074 }
3996 #endif /* CLASH_DETECTION */ 4075 #endif /* CLASH_DETECTION */
3997 4076
3998 fn = XSTRING (filename)->data; 4077 encoded_filename = ENCODE_FILE (filename);
4078
4079 fn = XSTRING (encoded_filename)->data;
3999 desc = -1; 4080 desc = -1;
4000 if (!NILP (append)) 4081 if (!NILP (append))
4001 #ifdef DOS_NT 4082 #ifdef DOS_NT
4002 desc = open (fn, O_WRONLY | buffer_file_type); 4083 desc = open (fn, O_WRONLY | buffer_file_type);
4003 #else /* not DOS_NT */ 4084 #else /* not DOS_NT */
4004 desc = open (fn, O_WRONLY); 4085 desc = open (fn, O_WRONLY);
4005 #endif /* not DOS_NT */ 4086 #endif /* not DOS_NT */
4006 4087
4007 if (desc < 0 && (NILP (append) || errno == ENOENT) ) 4088 if (desc < 0 && (NILP (append) || errno == ENOENT))
4008 #ifdef VMS 4089 #ifdef VMS
4009 if (auto_saving) /* Overwrite any previous version of autosave file */ 4090 if (auto_saving) /* Overwrite any previous version of autosave file */
4010 { 4091 {
4011 vms_truncate (fn); /* if fn exists, truncate to zero length */ 4092 vms_truncate (fn); /* if fn exists, truncate to zero length */
4012 desc = open (fn, O_RDWR); 4093 desc = open (fn, O_RDWR);
4226 next attempt to save. */ 4307 next attempt to save. */
4227 if (visiting) 4308 if (visiting)
4228 current_buffer->modtime = st.st_mtime; 4309 current_buffer->modtime = st.st_mtime;
4229 4310
4230 if (failure) 4311 if (failure)
4231 error ("IO error writing %s: %s", fn, strerror (save_errno)); 4312 error ("IO error writing %s: %s", XSTRING (filename)->data,
4313 strerror (save_errno));
4232 4314
4233 if (visiting) 4315 if (visiting)
4234 { 4316 {
4235 SAVE_MODIFF = MODIFF; 4317 SAVE_MODIFF = MODIFF;
4236 XSETFASTINT (current_buffer->save_length, Z - BEG); 4318 XSETFASTINT (current_buffer->save_length, Z - BEG);
4425 Lisp_Object buf; 4507 Lisp_Object buf;
4426 { 4508 {
4427 struct buffer *b; 4509 struct buffer *b;
4428 struct stat st; 4510 struct stat st;
4429 Lisp_Object handler; 4511 Lisp_Object handler;
4512 Lisp_Object filename;
4430 4513
4431 CHECK_BUFFER (buf, 0); 4514 CHECK_BUFFER (buf, 0);
4432 b = XBUFFER (buf); 4515 b = XBUFFER (buf);
4433 4516
4434 if (!STRINGP (b->filename)) return Qt; 4517 if (!STRINGP (b->filename)) return Qt;
4439 handler = Ffind_file_name_handler (b->filename, 4522 handler = Ffind_file_name_handler (b->filename,
4440 Qverify_visited_file_modtime); 4523 Qverify_visited_file_modtime);
4441 if (!NILP (handler)) 4524 if (!NILP (handler))
4442 return call2 (handler, Qverify_visited_file_modtime, buf); 4525 return call2 (handler, Qverify_visited_file_modtime, buf);
4443 4526
4444 if (stat (XSTRING (b->filename)->data, &st) < 0) 4527 filename = ENCODE_FILE (b->filename);
4528
4529 if (stat (XSTRING (filename)->data, &st) < 0)
4445 { 4530 {
4446 /* If the file doesn't exist now and didn't exist before, 4531 /* If the file doesn't exist now and didn't exist before,
4447 we say that it isn't modified, provided the error is a tame one. */ 4532 we say that it isn't modified, provided the error is a tame one. */
4448 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR) 4533 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
4449 st.st_mtime = -1; 4534 st.st_mtime = -1;
4504 call the corresponding file handler. */ 4589 call the corresponding file handler. */
4505 handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime); 4590 handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime);
4506 if (!NILP (handler)) 4591 if (!NILP (handler))
4507 /* The handler can find the file name the same way we did. */ 4592 /* The handler can find the file name the same way we did. */
4508 return call2 (handler, Qset_visited_file_modtime, Qnil); 4593 return call2 (handler, Qset_visited_file_modtime, Qnil);
4509 else if (stat (XSTRING (filename)->data, &st) >= 0) 4594
4595 filename = ENCODE_FILE (filename);
4596
4597 if (stat (XSTRING (filename)->data, &st) >= 0)
4510 current_buffer->modtime = st.st_mtime; 4598 current_buffer->modtime = st.st_mtime;
4511 } 4599 }
4512 4600
4513 return Qnil; 4601 return Qnil;
4514 } 4602 }
5131 5219
5132 #ifdef DOS_NT 5220 #ifdef DOS_NT
5133 Qfind_buffer_file_type = intern ("find-buffer-file-type"); 5221 Qfind_buffer_file_type = intern ("find-buffer-file-type");
5134 staticpro (&Qfind_buffer_file_type); 5222 staticpro (&Qfind_buffer_file_type);
5135 #endif /* DOS_NT */ 5223 #endif /* DOS_NT */
5224
5225 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system,
5226 "*Coding system for encoding file names.");
5227 Vfile_name_coding_system = Qnil;
5136 5228
5137 DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format, 5229 DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format,
5138 "*Format in which to write auto-save files.\n\ 5230 "*Format in which to write auto-save files.\n\
5139 Should be a list of symbols naming formats that are defined in `format-alist'.\n\ 5231 Should be a list of symbols naming formats that are defined in `format-alist'.\n\
5140 If it is t, which is the default, auto-save files are written in the\n\ 5232 If it is t, which is the default, auto-save files are written in the\n\