Commit | Line | Data |
---|---|---|
570d7624 | 1 | /* File IO for GNU Emacs. |
e9bffc61 | 2 | |
ab422c4d | 3 | Copyright (C) 1985-1988, 1993-2013 Free Software Foundation, Inc. |
570d7624 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
570d7624 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
570d7624 JB |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
570d7624 | 19 | |
18160b98 | 20 | #include <config.h> |
77a28bbf | 21 | #include <limits.h> |
bb369dc6 | 22 | #include <fcntl.h> |
1b335d29 | 23 | #include <stdio.h> |
570d7624 JB |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | |
29beb080 | 26 | #include <unistd.h> |
29beb080 | 27 | |
5b9c0a1d | 28 | #ifdef HAVE_PWD_H |
570d7624 | 29 | #include <pwd.h> |
bfb61299 JB |
30 | #endif |
31 | ||
570d7624 JB |
32 | #include <errno.h> |
33 | ||
574c05e2 KK |
34 | #ifdef HAVE_LIBSELINUX |
35 | #include <selinux/selinux.h> | |
36 | #include <selinux/context.h> | |
37 | #endif | |
38 | ||
7c3d167f RF |
39 | #ifdef HAVE_POSIX_ACL |
40 | #include <sys/acl.h> | |
41 | #endif | |
42 | ||
620f13b0 PE |
43 | #include <c-ctype.h> |
44 | ||
570d7624 | 45 | #include "lisp.h" |
8d4e077b | 46 | #include "intervals.h" |
db327c7e | 47 | #include "character.h" |
e5560ff7 | 48 | #include "buffer.h" |
6fdaa9a0 | 49 | #include "coding.h" |
570d7624 | 50 | #include "window.h" |
67c08d6c | 51 | #include "blockinput.h" |
385ed61f KL |
52 | #include "frame.h" |
53 | #include "dispextern.h" | |
570d7624 | 54 | |
5e570b75 RS |
55 | #ifdef WINDOWSNT |
56 | #define NOMINMAX 1 | |
57 | #include <windows.h> | |
5e570b75 | 58 | #include <fcntl.h> |
bb385a92 | 59 | #include <sys/file.h> |
a68089e4 | 60 | #include "w32.h" |
5e570b75 RS |
61 | #endif /* not WINDOWSNT */ |
62 | ||
7990d02a EZ |
63 | #ifdef MSDOS |
64 | #include "msdos.h" | |
65 | #include <sys/param.h> | |
7990d02a | 66 | #include <fcntl.h> |
7990d02a | 67 | #endif |
7990d02a | 68 | |
199607e4 | 69 | #ifdef DOS_NT |
199607e4 | 70 | /* On Windows, drive letters must be alphabetic - on DOS, the Netware |
a5bb9bd3 | 71 | redirector allows the six letters between 'Z' and 'a' as well. */ |
199607e4 RS |
72 | #ifdef MSDOS |
73 | #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z') | |
74 | #endif | |
75 | #ifdef WINDOWSNT | |
620f13b0 | 76 | #define IS_DRIVE(x) c_isalpha (x) |
199607e4 | 77 | #endif |
f54b565c | 78 | /* Need to lower-case the drive letter, or else expanded |
53964682 | 79 | filenames will sometimes compare unequal, because |
f54b565c | 80 | `expand-file-name' doesn't always down-case the drive letter. */ |
620f13b0 | 81 | #define DRIVE_LETTER(x) c_tolower (x) |
199607e4 RS |
82 | #endif |
83 | ||
de5bf5d3 | 84 | #include "systime.h" |
d35af63c | 85 | #include <stat-time.h> |
570d7624 JB |
86 | |
87 | #ifdef HPUX | |
88 | #include <netio.h> | |
47e7b9e5 | 89 | #endif |
570d7624 | 90 | |
9c856db9 | 91 | #include "commands.h" |
9c856db9 | 92 | |
f75d7a91 PE |
93 | /* True during writing of auto-save files. */ |
94 | static bool auto_saving; | |
570d7624 | 95 | |
71873e2b | 96 | /* Nonzero umask during creation of auto-save directories. */ |
f75d7a91 | 97 | static mode_t auto_saving_dir_umask; |
09450bae | 98 | |
570d7624 | 99 | /* Set by auto_save_1 to mode of original file so Fwrite_region will create |
71873e2b | 100 | a new file with the same mode as the original. */ |
f75d7a91 | 101 | static mode_t auto_save_mode_bits; |
570d7624 | 102 | |
71873e2b | 103 | /* Set by auto_save_1 if an error occurred during the last auto-save. */ |
f75d7a91 | 104 | static bool auto_save_error_occurred; |
ca730bf0 | 105 | |
9fe43ff6 PE |
106 | /* If VALID_TIMESTAMP_FILE_SYSTEM, then TIMESTAMP_FILE_SYSTEM is the device |
107 | number of a file system where time stamps were observed to to work. */ | |
108 | static bool valid_timestamp_file_system; | |
109 | static dev_t timestamp_file_system; | |
110 | ||
356a6224 KH |
111 | /* The symbol bound to coding-system-for-read when |
112 | insert-file-contents is called for recovering a file. This is not | |
113 | an actual coding system name, but just an indicator to tell | |
114 | insert-file-contents to use `emacs-mule' with a special flag for | |
115 | auto saving and recovering a file. */ | |
955cbe7b | 116 | static Lisp_Object Qauto_save_coding; |
356a6224 | 117 | |
f6c9b683 RS |
118 | /* Property name of a file name handler, |
119 | which gives a list of operations it handles.. */ | |
955cbe7b | 120 | static Lisp_Object Qoperations; |
f6c9b683 | 121 | |
71873e2b | 122 | /* Lisp functions for translating file formats. */ |
955cbe7b | 123 | static Lisp_Object Qformat_decode, Qformat_annotate_function; |
0d420e88 | 124 | |
2080470e | 125 | /* Lisp function for setting buffer-file-coding-system and the |
b6426b03 | 126 | multibyteness of the current buffer after inserting a file. */ |
955cbe7b | 127 | static Lisp_Object Qafter_insert_file_set_coding; |
b6426b03 | 128 | |
955cbe7b | 129 | static Lisp_Object Qwrite_region_annotate_functions; |
67fbc0cb CY |
130 | /* Each time an annotation function changes the buffer, the new buffer |
131 | is added here. */ | |
5045092b | 132 | static Lisp_Object Vwrite_region_annotation_buffers; |
67fbc0cb | 133 | |
ccf61795 | 134 | #ifdef HAVE_FSYNC |
ccf61795 RF |
135 | #endif |
136 | ||
955cbe7b | 137 | static Lisp_Object Qdelete_by_moving_to_trash; |
d2b66acf | 138 | |
6cf29fe8 | 139 | /* Lisp function for moving files to trash. */ |
955cbe7b | 140 | static Lisp_Object Qmove_file_to_trash; |
6cf29fe8 | 141 | |
8719abec | 142 | /* Lisp function for recursively copying directories. */ |
955cbe7b | 143 | static Lisp_Object Qcopy_directory; |
8719abec CY |
144 | |
145 | /* Lisp function for recursively deleting directories. */ | |
955cbe7b | 146 | static Lisp_Object Qdelete_directory; |
8719abec | 147 | |
c1c4693e | 148 | #ifdef WINDOWSNT |
c1c4693e RS |
149 | #endif |
150 | ||
955cbe7b PE |
151 | Lisp_Object Qfile_error; |
152 | static Lisp_Object Qfile_already_exists, Qfile_date_error; | |
153 | static Lisp_Object Qexcl; | |
15c65264 RS |
154 | Lisp_Object Qfile_name_history; |
155 | ||
955cbe7b | 156 | static Lisp_Object Qcar_less_than_car; |
d6a3cc15 | 157 | |
f75d7a91 PE |
158 | static bool a_write (int, Lisp_Object, ptrdiff_t, ptrdiff_t, |
159 | Lisp_Object *, struct coding_system *); | |
160 | static bool e_write (int, Lisp_Object, ptrdiff_t, ptrdiff_t, | |
161 | struct coding_system *); | |
ce51c54c | 162 | |
ec7adf26 | 163 | \f |
5d01d666 | 164 | void |
971de7fb | 165 | report_file_error (const char *string, Lisp_Object data) |
570d7624 JB |
166 | { |
167 | Lisp_Object errstring; | |
505ab9bc | 168 | int errorno = errno; |
d7259fdb | 169 | char *str; |
570d7624 | 170 | |
ca9c0567 | 171 | synchronize_system_messages_locale (); |
d7259fdb | 172 | str = strerror (errorno); |
d7ea76b4 | 173 | errstring = code_convert_string_norecord (build_unibyte_string (str), |
68c45bf0 PE |
174 | Vlocale_coding_system, 0); |
175 | ||
570d7624 | 176 | while (1) |
505ab9bc RS |
177 | switch (errorno) |
178 | { | |
179 | case EEXIST: | |
24b1ddad | 180 | xsignal (Qfile_already_exists, Fcons (errstring, data)); |
505ab9bc RS |
181 | break; |
182 | default: | |
183 | /* System error messages are capitalized. Downcase the initial | |
39824137 EZ |
184 | unless it is followed by a slash. (The slash case caters to |
185 | error messages that begin with "I/O" or, in German, "E/A".) */ | |
d5443ffd KH |
186 | if (STRING_MULTIBYTE (errstring) |
187 | && ! EQ (Faref (errstring, make_number (1)), make_number ('/'))) | |
6c0969ca KH |
188 | { |
189 | int c; | |
190 | ||
51b59d79 | 191 | str = SSDATA (errstring); |
5976c3fe | 192 | c = STRING_CHAR ((unsigned char *) str); |
5da9919f | 193 | Faset (errstring, make_number (0), make_number (downcase (c))); |
6c0969ca | 194 | } |
505ab9bc | 195 | |
24b1ddad | 196 | xsignal (Qfile_error, |
505ab9bc RS |
197 | Fcons (build_string (string), Fcons (errstring, data))); |
198 | } | |
570d7624 | 199 | } |
b5148e85 | 200 | |
b27a1703 | 201 | Lisp_Object |
971de7fb | 202 | close_file_unwind (Lisp_Object fd) |
b5148e85 | 203 | { |
68c45bf0 | 204 | emacs_close (XFASTINT (fd)); |
b27a1703 | 205 | return Qnil; |
b5148e85 | 206 | } |
a1d2b64a RS |
207 | |
208 | /* Restore point, having saved it as a marker. */ | |
209 | ||
5dcde606 | 210 | Lisp_Object |
971de7fb | 211 | restore_point_unwind (Lisp_Object location) |
a1d2b64a | 212 | { |
ec7adf26 | 213 | Fgoto_char (location); |
a1d2b64a | 214 | Fset_marker (location, Qnil, Qnil); |
b27a1703 | 215 | return Qnil; |
a1d2b64a | 216 | } |
59c94f03 | 217 | |
570d7624 | 218 | \f |
955cbe7b PE |
219 | static Lisp_Object Qexpand_file_name; |
220 | static Lisp_Object Qsubstitute_in_file_name; | |
221 | static Lisp_Object Qdirectory_file_name; | |
222 | static Lisp_Object Qfile_name_directory; | |
223 | static Lisp_Object Qfile_name_nondirectory; | |
224 | static Lisp_Object Qunhandled_file_name_directory; | |
225 | static Lisp_Object Qfile_name_as_directory; | |
226 | static Lisp_Object Qcopy_file; | |
227 | static Lisp_Object Qmake_directory_internal; | |
228 | static Lisp_Object Qmake_directory; | |
229 | static Lisp_Object Qdelete_directory_internal; | |
32f4334d | 230 | Lisp_Object Qdelete_file; |
955cbe7b PE |
231 | static Lisp_Object Qrename_file; |
232 | static Lisp_Object Qadd_name_to_file; | |
233 | static Lisp_Object Qmake_symbolic_link; | |
32f4334d | 234 | Lisp_Object Qfile_exists_p; |
955cbe7b PE |
235 | static Lisp_Object Qfile_executable_p; |
236 | static Lisp_Object Qfile_readable_p; | |
237 | static Lisp_Object Qfile_writable_p; | |
238 | static Lisp_Object Qfile_symlink_p; | |
239 | static Lisp_Object Qaccess_file; | |
32f4334d | 240 | Lisp_Object Qfile_directory_p; |
955cbe7b PE |
241 | static Lisp_Object Qfile_regular_p; |
242 | static Lisp_Object Qfile_accessible_directory_p; | |
243 | static Lisp_Object Qfile_modes; | |
244 | static Lisp_Object Qset_file_modes; | |
245 | static Lisp_Object Qset_file_times; | |
246 | static Lisp_Object Qfile_selinux_context; | |
247 | static Lisp_Object Qset_file_selinux_context; | |
7c3d167f RF |
248 | static Lisp_Object Qfile_acl; |
249 | static Lisp_Object Qset_file_acl; | |
955cbe7b | 250 | static Lisp_Object Qfile_newer_than_file_p; |
32f4334d | 251 | Lisp_Object Qinsert_file_contents; |
9dbda100 | 252 | Lisp_Object Qchoose_write_coding_system; |
32f4334d | 253 | Lisp_Object Qwrite_region; |
955cbe7b PE |
254 | static Lisp_Object Qverify_visited_file_modtime; |
255 | static Lisp_Object Qset_visited_file_modtime; | |
32f4334d | 256 | |
a7ca3326 | 257 | DEFUN ("find-file-name-handler", Ffind_file_name_handler, |
16a97296 | 258 | Sfind_file_name_handler, 2, 2, 0, |
8c1a1077 PJ |
259 | doc: /* Return FILENAME's handler function for OPERATION, if it has one. |
260 | Otherwise, return nil. | |
261 | A file name is handled if one of the regular expressions in | |
262 | `file-name-handler-alist' matches it. | |
263 | ||
264 | If OPERATION equals `inhibit-file-name-operation', then we ignore | |
265 | any handlers that are members of `inhibit-file-name-handlers', | |
266 | but we still do run any other handlers. This lets handlers | |
267 | use the standard functions without calling themselves recursively. */) | |
5842a27b | 268 | (Lisp_Object filename, Lisp_Object operation) |
32f4334d | 269 | { |
642ef245 | 270 | /* This function must not munge the match data. */ |
204ee271 | 271 | Lisp_Object chain, inhibited_handlers, result; |
d311d28c | 272 | ptrdiff_t pos = -1; |
642ef245 | 273 | |
204ee271 | 274 | result = Qnil; |
b7826503 | 275 | CHECK_STRING (filename); |
e4432095 | 276 | |
a65970a0 RS |
277 | if (EQ (operation, Vinhibit_file_name_operation)) |
278 | inhibited_handlers = Vinhibit_file_name_handlers; | |
279 | else | |
280 | inhibited_handlers = Qnil; | |
82c2d839 | 281 | |
93c30b5f | 282 | for (chain = Vfile_name_handler_alist; CONSP (chain); |
03699b14 | 283 | chain = XCDR (chain)) |
32f4334d RS |
284 | { |
285 | Lisp_Object elt; | |
03699b14 | 286 | elt = XCAR (chain); |
93c30b5f | 287 | if (CONSP (elt)) |
32f4334d | 288 | { |
f6c9b683 | 289 | Lisp_Object string = XCAR (elt); |
d311d28c | 290 | ptrdiff_t match_pos; |
f6c9b683 | 291 | Lisp_Object handler = XCDR (elt); |
68780e2a RS |
292 | Lisp_Object operations = Qnil; |
293 | ||
294 | if (SYMBOLP (handler)) | |
295 | operations = Fget (handler, Qoperations); | |
f6c9b683 | 296 | |
8d2ced53 | 297 | if (STRINGP (string) |
f6c9b683 RS |
298 | && (match_pos = fast_string_match (string, filename)) > pos |
299 | && (NILP (operations) || ! NILP (Fmemq (operation, operations)))) | |
a65970a0 | 300 | { |
f6c9b683 | 301 | Lisp_Object tem; |
a65970a0 | 302 | |
03699b14 | 303 | handler = XCDR (elt); |
a65970a0 RS |
304 | tem = Fmemq (handler, inhibited_handlers); |
305 | if (NILP (tem)) | |
8d2ced53 SM |
306 | { |
307 | result = handler; | |
308 | pos = match_pos; | |
309 | } | |
a65970a0 | 310 | } |
32f4334d | 311 | } |
642ef245 JB |
312 | |
313 | QUIT; | |
32f4334d | 314 | } |
8d2ced53 | 315 | return result; |
32f4334d RS |
316 | } |
317 | \f | |
a7ca3326 | 318 | DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, |
8c1a1077 PJ |
319 | 1, 1, 0, |
320 | doc: /* Return the directory component in file name FILENAME. | |
321 | Return nil if FILENAME does not include a directory. | |
78379264 | 322 | Otherwise return a directory name. |
7c2fb837 | 323 | Given a Unix syntax file name, returns a string ending in slash. */) |
5842a27b | 324 | (Lisp_Object filename) |
570d7624 | 325 | { |
100c44b7 | 326 | #ifndef DOS_NT |
5976c3fe | 327 | register const char *beg; |
100c44b7 | 328 | #else |
5976c3fe | 329 | register char *beg; |
273ac8d1 | 330 | Lisp_Object tem_fn; |
100c44b7 | 331 | #endif |
5976c3fe | 332 | register const char *p; |
0bf2eed2 | 333 | Lisp_Object handler; |
570d7624 | 334 | |
b7826503 | 335 | CHECK_STRING (filename); |
570d7624 | 336 | |
0bf2eed2 RS |
337 | /* If the file name has special constructs in it, |
338 | call the corresponding file handler. */ | |
3b7f6e60 | 339 | handler = Ffind_file_name_handler (filename, Qfile_name_directory); |
0bf2eed2 | 340 | if (!NILP (handler)) |
2375c96a CY |
341 | { |
342 | Lisp_Object handled_name = call2 (handler, Qfile_name_directory, | |
343 | filename); | |
344 | return STRINGP (handled_name) ? handled_name : Qnil; | |
345 | } | |
0bf2eed2 | 346 | |
199607e4 | 347 | #ifdef DOS_NT |
38182d90 | 348 | beg = alloca (SBYTES (filename) + 1); |
5976c3fe | 349 | memcpy (beg, SSDATA (filename), SBYTES (filename) + 1); |
0f3f018c | 350 | #else |
5976c3fe | 351 | beg = SSDATA (filename); |
199607e4 | 352 | #endif |
d5db4077 | 353 | p = beg + SBYTES (filename); |
570d7624 | 354 | |
199607e4 | 355 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
199607e4 | 356 | #ifdef DOS_NT |
e1dbe924 | 357 | /* only recognize drive specifier at the beginning */ |
ba14e174 RS |
358 | && !(p[-1] == ':' |
359 | /* handle the "/:d:foo" and "/:foo" cases correctly */ | |
360 | && ((p == beg + 2 && !IS_DIRECTORY_SEP (*beg)) | |
361 | || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 362 | #endif |
570d7624 JB |
363 | ) p--; |
364 | ||
365 | if (p == beg) | |
366 | return Qnil; | |
5e570b75 | 367 | #ifdef DOS_NT |
4c3c22f3 | 368 | /* Expansion of "c:" to drive and default directory. */ |
ba14e174 | 369 | if (p[-1] == ':') |
4c3c22f3 | 370 | { |
4c3c22f3 | 371 | /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */ |
5976c3fe PE |
372 | char *res = alloca (MAXPATHLEN + 1); |
373 | char *r = res; | |
ba14e174 RS |
374 | |
375 | if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':') | |
376 | { | |
e99a530f | 377 | memcpy (res, beg, 2); |
ba14e174 RS |
378 | beg += 2; |
379 | r += 2; | |
380 | } | |
381 | ||
620f13b0 | 382 | if (getdefdir (c_toupper (*beg) - 'A' + 1, r)) |
4c3c22f3 | 383 | { |
199607e4 | 384 | if (!IS_DIRECTORY_SEP (res[strlen (res) - 1])) |
4c3c22f3 RS |
385 | strcat (res, "/"); |
386 | beg = res; | |
387 | p = beg + strlen (beg); | |
f5c81c80 EZ |
388 | dostounix_filename (beg); |
389 | tem_fn = make_specified_string (beg, -1, p - beg, | |
390 | STRING_MULTIBYTE (filename)); | |
4c3c22f3 | 391 | } |
f5c81c80 EZ |
392 | else |
393 | tem_fn = make_specified_string (beg - 2, -1, p - beg + 2, | |
394 | STRING_MULTIBYTE (filename)); | |
395 | } | |
396 | else if (STRING_MULTIBYTE (filename)) | |
397 | { | |
398 | tem_fn = ENCODE_FILE (make_specified_string (beg, -1, p - beg, 1)); | |
399 | dostounix_filename (SSDATA (tem_fn)); | |
400 | tem_fn = DECODE_FILE (tem_fn); | |
401 | } | |
402 | else | |
403 | { | |
404 | dostounix_filename (beg); | |
405 | tem_fn = make_specified_string (beg, -1, p - beg, 0); | |
4c3c22f3 | 406 | } |
f5c81c80 | 407 | return tem_fn; |
273ac8d1 | 408 | #else /* DOS_NT */ |
d7231f93 | 409 | return make_specified_string (beg, -1, p - beg, STRING_MULTIBYTE (filename)); |
273ac8d1 | 410 | #endif /* DOS_NT */ |
570d7624 JB |
411 | } |
412 | ||
a7ca3326 | 413 | DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, |
60d67b83 | 414 | Sfile_name_nondirectory, 1, 1, 0, |
8c1a1077 PJ |
415 | doc: /* Return file name FILENAME sans its directory. |
416 | For example, in a Unix-syntax file name, | |
417 | this is everything after the last slash, | |
418 | or the entire name if it contains no slash. */) | |
5842a27b | 419 | (Lisp_Object filename) |
570d7624 | 420 | { |
5976c3fe | 421 | register const char *beg, *p, *end; |
0bf2eed2 | 422 | Lisp_Object handler; |
570d7624 | 423 | |
b7826503 | 424 | CHECK_STRING (filename); |
570d7624 | 425 | |
0bf2eed2 RS |
426 | /* If the file name has special constructs in it, |
427 | call the corresponding file handler. */ | |
3b7f6e60 | 428 | handler = Ffind_file_name_handler (filename, Qfile_name_nondirectory); |
0bf2eed2 | 429 | if (!NILP (handler)) |
2375c96a CY |
430 | { |
431 | Lisp_Object handled_name = call2 (handler, Qfile_name_nondirectory, | |
432 | filename); | |
433 | if (STRINGP (handled_name)) | |
434 | return handled_name; | |
435 | error ("Invalid handler in `file-name-handler-alist'"); | |
436 | } | |
0bf2eed2 | 437 | |
5976c3fe | 438 | beg = SSDATA (filename); |
d5db4077 | 439 | end = p = beg + SBYTES (filename); |
570d7624 | 440 | |
199607e4 | 441 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
199607e4 | 442 | #ifdef DOS_NT |
e1dbe924 | 443 | /* only recognize drive specifier at beginning */ |
ba14e174 RS |
444 | && !(p[-1] == ':' |
445 | /* handle the "/:d:foo" case correctly */ | |
446 | && (p == beg + 2 || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 447 | #endif |
60d67b83 RS |
448 | ) |
449 | p--; | |
570d7624 | 450 | |
d7231f93 | 451 | return make_specified_string (p, -1, end - p, STRING_MULTIBYTE (filename)); |
570d7624 | 452 | } |
642ef245 | 453 | |
a7ca3326 | 454 | DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, |
60d67b83 | 455 | Sunhandled_file_name_directory, 1, 1, 0, |
8c1a1077 PJ |
456 | doc: /* Return a directly usable directory name somehow associated with FILENAME. |
457 | A `directly usable' directory name is one that may be used without the | |
458 | intervention of any file handler. | |
459 | If FILENAME is a directly usable file itself, return | |
460 | \(file-name-directory FILENAME). | |
ca319910 SM |
461 | If FILENAME refers to a file which is not accessible from a local process, |
462 | then this should return nil. | |
8c1a1077 PJ |
463 | The `call-process' and `start-process' functions use this function to |
464 | get a current directory to run processes in. */) | |
5842a27b | 465 | (Lisp_Object filename) |
642ef245 JB |
466 | { |
467 | Lisp_Object handler; | |
468 | ||
469 | /* If the file name has special constructs in it, | |
470 | call the corresponding file handler. */ | |
49307295 | 471 | handler = Ffind_file_name_handler (filename, Qunhandled_file_name_directory); |
642ef245 | 472 | if (!NILP (handler)) |
2375c96a CY |
473 | { |
474 | Lisp_Object handled_name = call2 (handler, Qunhandled_file_name_directory, | |
475 | filename); | |
476 | return STRINGP (handled_name) ? handled_name : Qnil; | |
477 | } | |
642ef245 JB |
478 | |
479 | return Ffile_name_directory (filename); | |
480 | } | |
481 | ||
f5c81c80 EZ |
482 | /* Convert from file name SRC of length SRCLEN to directory name in |
483 | DST. MULTIBYTE non-zero means the file name in SRC is a multibyte | |
484 | string. On UNIX, just make sure there is a terminating /. Return | |
485 | the length of DST in bytes. */ | |
570d7624 | 486 | |
c293e30c | 487 | static ptrdiff_t |
f5c81c80 EZ |
488 | file_name_as_directory (char *dst, const char *src, ptrdiff_t srclen, |
489 | bool multibyte) | |
c293e30c DA |
490 | { |
491 | if (srclen == 0) | |
8aa3a244 | 492 | { |
c293e30c DA |
493 | dst[0] = '.'; |
494 | dst[1] = '/'; | |
495 | dst[2] = '\0'; | |
496 | return 2; | |
8aa3a244 RS |
497 | } |
498 | ||
c293e30c | 499 | strcpy (dst, src); |
93f4cf88 | 500 | |
c293e30c | 501 | if (!IS_DIRECTORY_SEP (dst[srclen - 1])) |
5e570b75 | 502 | { |
c293e30c DA |
503 | dst[srclen] = DIRECTORY_SEP; |
504 | dst[srclen + 1] = '\0'; | |
505 | srclen++; | |
5e570b75 | 506 | } |
199607e4 | 507 | #ifdef DOS_NT |
f5c81c80 EZ |
508 | if (multibyte) |
509 | { | |
510 | Lisp_Object tem_fn = make_specified_string (dst, -1, srclen, 1); | |
2e7cddd3 | 511 | |
f5c81c80 EZ |
512 | tem_fn = ENCODE_FILE (tem_fn); |
513 | dostounix_filename (SSDATA (tem_fn)); | |
514 | tem_fn = DECODE_FILE (tem_fn); | |
515 | memcpy (dst, SSDATA (tem_fn), (srclen = SBYTES (tem_fn)) + 1); | |
516 | } | |
517 | else | |
518 | dostounix_filename (dst); | |
199607e4 | 519 | #endif |
c293e30c | 520 | return srclen; |
570d7624 JB |
521 | } |
522 | ||
a7ca3326 | 523 | DEFUN ("file-name-as-directory", Ffile_name_as_directory, |
570d7624 | 524 | Sfile_name_as_directory, 1, 1, 0, |
0dac4f85 | 525 | doc: /* Return a string representing the file name FILE interpreted as a directory. |
8c1a1077 PJ |
526 | This operation exists because a directory is also a file, but its name as |
527 | a directory is different from its name as a file. | |
528 | The result can be used as the value of `default-directory' | |
529 | or passed as second argument to `expand-file-name'. | |
7c2fb837 | 530 | For a Unix-syntax file name, just appends a slash. */) |
5842a27b | 531 | (Lisp_Object file) |
570d7624 JB |
532 | { |
533 | char *buf; | |
c293e30c | 534 | ptrdiff_t length; |
0bf2eed2 | 535 | Lisp_Object handler; |
570d7624 | 536 | |
b7826503 | 537 | CHECK_STRING (file); |
265a9e55 | 538 | if (NILP (file)) |
570d7624 | 539 | return Qnil; |
0bf2eed2 RS |
540 | |
541 | /* If the file name has special constructs in it, | |
542 | call the corresponding file handler. */ | |
49307295 | 543 | handler = Ffind_file_name_handler (file, Qfile_name_as_directory); |
0bf2eed2 | 544 | if (!NILP (handler)) |
2375c96a CY |
545 | { |
546 | Lisp_Object handled_name = call2 (handler, Qfile_name_as_directory, | |
547 | file); | |
548 | if (STRINGP (handled_name)) | |
549 | return handled_name; | |
550 | error ("Invalid handler in `file-name-handler-alist'"); | |
551 | } | |
0bf2eed2 | 552 | |
38182d90 | 553 | buf = alloca (SBYTES (file) + 10); |
f5c81c80 EZ |
554 | length = file_name_as_directory (buf, SSDATA (file), SBYTES (file), |
555 | STRING_MULTIBYTE (file)); | |
c293e30c | 556 | return make_specified_string (buf, -1, length, STRING_MULTIBYTE (file)); |
570d7624 JB |
557 | } |
558 | \f | |
f5c81c80 EZ |
559 | /* Convert from directory name SRC of length SRCLEN to file name in |
560 | DST. MULTIBYTE non-zero means the file name in SRC is a multibyte | |
561 | string. On UNIX, just make sure there isn't a terminating /. | |
562 | Return the length of DST in bytes. */ | |
570d7624 | 563 | |
c293e30c | 564 | static ptrdiff_t |
f5c81c80 | 565 | directory_file_name (char *dst, char *src, ptrdiff_t srclen, bool multibyte) |
570d7624 | 566 | { |
570d7624 JB |
567 | /* Process as Unix format: just remove any final slash. |
568 | But leave "/" unchanged; do not change it to "". */ | |
569 | strcpy (dst, src); | |
c293e30c DA |
570 | if (srclen > 1 |
571 | && IS_DIRECTORY_SEP (dst[srclen - 1]) | |
4592782e | 572 | #ifdef DOS_NT |
c293e30c | 573 | && !IS_ANY_SEP (dst[srclen - 2]) |
4592782e RS |
574 | #endif |
575 | ) | |
c293e30c DA |
576 | { |
577 | dst[srclen - 1] = 0; | |
578 | srclen--; | |
579 | } | |
199607e4 | 580 | #ifdef DOS_NT |
f5c81c80 EZ |
581 | if (multibyte) |
582 | { | |
583 | Lisp_Object tem_fn = make_specified_string (dst, -1, srclen, 1); | |
2e7cddd3 | 584 | |
f5c81c80 EZ |
585 | tem_fn = ENCODE_FILE (tem_fn); |
586 | dostounix_filename (SSDATA (tem_fn)); | |
587 | tem_fn = DECODE_FILE (tem_fn); | |
588 | memcpy (dst, SSDATA (tem_fn), (srclen = SBYTES (tem_fn)) + 1); | |
589 | } | |
590 | else | |
591 | dostounix_filename (dst); | |
125feee8 | 592 | #endif |
c293e30c | 593 | return srclen; |
570d7624 JB |
594 | } |
595 | ||
a7ca3326 | 596 | DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name, |
8c1a1077 PJ |
597 | 1, 1, 0, |
598 | doc: /* Returns the file name of the directory named DIRECTORY. | |
599 | This is the name of the file that holds the data for the directory DIRECTORY. | |
600 | This operation exists because a directory is also a file, but its name as | |
601 | a directory is different from its name as a file. | |
7c2fb837 | 602 | In Unix-syntax, this function just removes the final slash. */) |
5842a27b | 603 | (Lisp_Object directory) |
570d7624 JB |
604 | { |
605 | char *buf; | |
c293e30c | 606 | ptrdiff_t length; |
0bf2eed2 | 607 | Lisp_Object handler; |
570d7624 | 608 | |
b7826503 | 609 | CHECK_STRING (directory); |
570d7624 | 610 | |
265a9e55 | 611 | if (NILP (directory)) |
570d7624 | 612 | return Qnil; |
0bf2eed2 RS |
613 | |
614 | /* If the file name has special constructs in it, | |
615 | call the corresponding file handler. */ | |
49307295 | 616 | handler = Ffind_file_name_handler (directory, Qdirectory_file_name); |
0bf2eed2 | 617 | if (!NILP (handler)) |
2375c96a CY |
618 | { |
619 | Lisp_Object handled_name = call2 (handler, Qdirectory_file_name, | |
620 | directory); | |
621 | if (STRINGP (handled_name)) | |
622 | return handled_name; | |
623 | error ("Invalid handler in `file-name-handler-alist'"); | |
624 | } | |
0bf2eed2 | 625 | |
38182d90 | 626 | buf = alloca (SBYTES (directory) + 20); |
f5c81c80 EZ |
627 | length = directory_file_name (buf, SSDATA (directory), SBYTES (directory), |
628 | STRING_MULTIBYTE (directory)); | |
c293e30c | 629 | return make_specified_string (buf, -1, length, STRING_MULTIBYTE (directory)); |
570d7624 JB |
630 | } |
631 | ||
91433552 | 632 | static const char make_temp_name_tbl[64] = |
3ce839e4 RS |
633 | { |
634 | 'A','B','C','D','E','F','G','H', | |
635 | 'I','J','K','L','M','N','O','P', | |
636 | 'Q','R','S','T','U','V','W','X', | |
637 | 'Y','Z','a','b','c','d','e','f', | |
638 | 'g','h','i','j','k','l','m','n', | |
639 | 'o','p','q','r','s','t','u','v', | |
640 | 'w','x','y','z','0','1','2','3', | |
641 | '4','5','6','7','8','9','-','_' | |
642 | }; | |
cb613bb8 | 643 | |
3ce839e4 RS |
644 | static unsigned make_temp_name_count, make_temp_name_count_initialized_p; |
645 | ||
cb613bb8 | 646 | /* Value is a temporary file name starting with PREFIX, a string. |
efdc16c9 | 647 | |
cb613bb8 GM |
648 | The Emacs process number forms part of the result, so there is |
649 | no danger of generating a name being used by another process. | |
650 | In addition, this function makes an attempt to choose a name | |
651 | which has no existing file. To make this work, PREFIX should be | |
652 | an absolute file name. | |
efdc16c9 | 653 | |
f75d7a91 | 654 | BASE64_P means add the pid as 3 characters in base64 |
cb613bb8 GM |
655 | encoding. In this case, 6 characters will be added to PREFIX to |
656 | form the file name. Otherwise, if Emacs is running on a system | |
657 | with long file names, add the pid as a decimal number. | |
658 | ||
659 | This function signals an error if no unique file name could be | |
660 | generated. */ | |
661 | ||
662 | Lisp_Object | |
f75d7a91 | 663 | make_temp_name (Lisp_Object prefix, bool base64_p) |
570d7624 JB |
664 | { |
665 | Lisp_Object val; | |
0cedd530 | 666 | int len, clen; |
a81d11a3 | 667 | printmax_t pid; |
5976c3fe | 668 | char *p, *data; |
a81d11a3 | 669 | char pidbuf[INT_BUFSIZE_BOUND (printmax_t)]; |
3ce839e4 | 670 | int pidlen; |
efdc16c9 | 671 | |
b7826503 | 672 | CHECK_STRING (prefix); |
3ce839e4 RS |
673 | |
674 | /* VAL is created by adding 6 characters to PREFIX. The first | |
675 | three are the PID of this process, in base 64, and the second | |
676 | three are incremented if the file already exists. This ensures | |
677 | 262144 unique file names per PID per PREFIX. */ | |
678 | ||
93f4cf88 | 679 | pid = getpid (); |
3ce839e4 | 680 | |
cb613bb8 GM |
681 | if (base64_p) |
682 | { | |
683 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
684 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
685 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
686 | pidlen = 3; | |
687 | } | |
688 | else | |
689 | { | |
3ce839e4 | 690 | #ifdef HAVE_LONG_FILE_NAMES |
a81d11a3 | 691 | pidlen = sprintf (pidbuf, "%"pMd, pid); |
3a3bfb18 | 692 | #else |
cb613bb8 GM |
693 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; |
694 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
695 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
696 | pidlen = 3; | |
3ce839e4 | 697 | #endif |
cb613bb8 | 698 | } |
efdc16c9 | 699 | |
0cedd530 SM |
700 | len = SBYTES (prefix); clen = SCHARS (prefix); |
701 | val = make_uninit_multibyte_string (clen + 3 + pidlen, len + 3 + pidlen); | |
702 | if (!STRING_MULTIBYTE (prefix)) | |
703 | STRING_SET_UNIBYTE (val); | |
5976c3fe PE |
704 | data = SSDATA (val); |
705 | memcpy (data, SSDATA (prefix), len); | |
3ce839e4 RS |
706 | p = data + len; |
707 | ||
72af86bd | 708 | memcpy (p, pidbuf, pidlen); |
3ce839e4 RS |
709 | p += pidlen; |
710 | ||
711 | /* Here we try to minimize useless stat'ing when this function is | |
712 | invoked many times successively with the same PREFIX. We achieve | |
713 | this by initializing count to a random value, and incrementing it | |
f6a492a9 RS |
714 | afterwards. |
715 | ||
716 | We don't want make-temp-name to be called while dumping, | |
717 | because then make_temp_name_count_initialized_p would get set | |
718 | and then make_temp_name_count would not be set when Emacs starts. */ | |
719 | ||
3ce839e4 RS |
720 | if (!make_temp_name_count_initialized_p) |
721 | { | |
327eeec8 | 722 | make_temp_name_count = time (NULL); |
3ce839e4 RS |
723 | make_temp_name_count_initialized_p = 1; |
724 | } | |
725 | ||
726 | while (1) | |
727 | { | |
8a7777fc | 728 | unsigned num = make_temp_name_count; |
3ce839e4 RS |
729 | |
730 | p[0] = make_temp_name_tbl[num & 63], num >>= 6; | |
731 | p[1] = make_temp_name_tbl[num & 63], num >>= 6; | |
732 | p[2] = make_temp_name_tbl[num & 63], num >>= 6; | |
733 | ||
8a7777fc RS |
734 | /* Poor man's congruential RN generator. Replace with |
735 | ++make_temp_name_count for debugging. */ | |
736 | make_temp_name_count += 25229; | |
737 | make_temp_name_count %= 225307; | |
738 | ||
bb385a92 | 739 | if (!check_existing (data)) |
3ce839e4 RS |
740 | { |
741 | /* We want to return only if errno is ENOENT. */ | |
742 | if (errno == ENOENT) | |
743 | return val; | |
744 | else | |
745 | /* The error here is dubious, but there is little else we | |
746 | can do. The alternatives are to return nil, which is | |
747 | as bad as (and in many cases worse than) throwing the | |
748 | error, or to ignore the error, which will likely result | |
8a7777fc | 749 | in looping through 225307 stat's, which is not only |
410ed5c3 PE |
750 | dog-slow, but also useless since eventually nil would |
751 | have to be returned anyway. */ | |
9869bb0b | 752 | report_file_error ("Cannot create temporary name for prefix", |
3ce839e4 RS |
753 | Fcons (prefix, Qnil)); |
754 | /* not reached */ | |
755 | } | |
756 | } | |
570d7624 | 757 | } |
3ce839e4 | 758 | |
cb613bb8 GM |
759 | |
760 | DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0, | |
8c1a1077 PJ |
761 | doc: /* Generate temporary file name (string) starting with PREFIX (a string). |
762 | The Emacs process number forms part of the result, | |
763 | so there is no danger of generating a name being used by another process. | |
764 | ||
765 | In addition, this function makes an attempt to choose a name | |
766 | which has no existing file. To make this work, | |
767 | PREFIX should be an absolute file name. | |
768 | ||
769 | There is a race condition between calling `make-temp-name' and creating the | |
770 | file which opens all kinds of security holes. For that reason, you should | |
f9e6f049 RS |
771 | probably use `make-temp-file' instead, except in three circumstances: |
772 | ||
773 | * If you are creating the file in the user's home directory. | |
774 | * If you are creating a directory rather than an ordinary file. | |
775 | * If you are taking special precautions as `make-temp-file' does. */) | |
5842a27b | 776 | (Lisp_Object prefix) |
cb613bb8 GM |
777 | { |
778 | return make_temp_name (prefix, 0); | |
779 | } | |
780 | ||
781 | ||
570d7624 | 782 | \f |
a7ca3326 | 783 | DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, |
8c1a1077 PJ |
784 | doc: /* Convert filename NAME to absolute, and canonicalize it. |
785 | Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative | |
78379264 | 786 | \(does not start with slash or tilde); if DEFAULT-DIRECTORY is nil or missing, |
a4e03fe5 | 787 | the current buffer's value of `default-directory' is used. |
1b2a627f EZ |
788 | NAME should be a string that is a valid file name for the underlying |
789 | filesystem. | |
8c1a1077 PJ |
790 | File name components that are `.' are removed, and |
791 | so are file name components followed by `..', along with the `..' itself; | |
792 | note that these simplifications are done without checking the resulting | |
793 | file names in the file system. | |
15579471 EZ |
794 | Multiple consecutive slashes are collapsed into a single slash, |
795 | except at the beginning of the file name when they are significant (e.g., | |
796 | UNC file names on MS-Windows.) | |
8c1a1077 PJ |
797 | An initial `~/' expands to your home directory. |
798 | An initial `~USER/' expands to USER's home directory. | |
07114296 CY |
799 | See also the function `substitute-in-file-name'. |
800 | ||
801 | For technical reasons, this function can return correct but | |
802 | non-intuitive results for the root directory; for instance, | |
803 | \(expand-file-name ".." "/") returns "/..". For this reason, use | |
15579471 | 804 | \(directory-file-name (file-name-directory dirname)) to traverse a |
07114296 | 805 | filesystem tree, not (expand-file-name ".." dirname). */) |
5842a27b | 806 | (Lisp_Object name, Lisp_Object default_directory) |
570d7624 | 807 | { |
5b5a2ea1 SM |
808 | /* These point to SDATA and need to be careful with string-relocation |
809 | during GC (via DECODE_FILE). */ | |
2893f146 PE |
810 | char *nm; |
811 | const char *newdir; | |
5b5a2ea1 | 812 | /* This should only point to alloca'd data. */ |
5976c3fe | 813 | char *target; |
199607e4 | 814 | |
93f4cf88 | 815 | ptrdiff_t tlen; |
570d7624 | 816 | struct passwd *pw; |
5e570b75 | 817 | #ifdef DOS_NT |
199607e4 | 818 | int drive = 0; |
f75d7a91 PE |
819 | bool collapse_newdir = 1; |
820 | bool is_escaped = 0; | |
5e570b75 | 821 | #endif /* DOS_NT */ |
93f4cf88 | 822 | ptrdiff_t length; |
2375c96a | 823 | Lisp_Object handler, result, handled_name; |
fce31d69 | 824 | bool multibyte; |
9c06a1f3 | 825 | Lisp_Object hdir; |
199607e4 | 826 | |
b7826503 | 827 | CHECK_STRING (name); |
570d7624 | 828 | |
0bf2eed2 RS |
829 | /* If the file name has special constructs in it, |
830 | call the corresponding file handler. */ | |
49307295 | 831 | handler = Ffind_file_name_handler (name, Qexpand_file_name); |
0bf2eed2 | 832 | if (!NILP (handler)) |
2375c96a CY |
833 | { |
834 | handled_name = call3 (handler, Qexpand_file_name, | |
835 | name, default_directory); | |
836 | if (STRINGP (handled_name)) | |
837 | return handled_name; | |
838 | error ("Invalid handler in `file-name-handler-alist'"); | |
839 | } | |
840 | ||
58fc9587 | 841 | |
3b7f6e60 EN |
842 | /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */ |
843 | if (NILP (default_directory)) | |
4b4deea2 | 844 | default_directory = BVAR (current_buffer, directory); |
82330e7f | 845 | if (! STRINGP (default_directory)) |
dd693537 EZ |
846 | { |
847 | #ifdef DOS_NT | |
848 | /* "/" is not considered a root directory on DOS_NT, so using "/" | |
849 | here causes an infinite recursion in, e.g., the following: | |
850 | ||
851 | (let (default-directory) | |
852 | (expand-file-name "a")) | |
853 | ||
854 | To avoid this, we set default_directory to the root of the | |
855 | current drive. */ | |
dd693537 EZ |
856 | default_directory = build_string (emacs_root_dir ()); |
857 | #else | |
858 | default_directory = build_string ("/"); | |
859 | #endif | |
860 | } | |
58fc9587 | 861 | |
3b7f6e60 | 862 | if (!NILP (default_directory)) |
273e0829 | 863 | { |
3b7f6e60 | 864 | handler = Ffind_file_name_handler (default_directory, Qexpand_file_name); |
273e0829 | 865 | if (!NILP (handler)) |
2375c96a CY |
866 | { |
867 | handled_name = call3 (handler, Qexpand_file_name, | |
868 | name, default_directory); | |
869 | if (STRINGP (handled_name)) | |
870 | return handled_name; | |
871 | error ("Invalid handler in `file-name-handler-alist'"); | |
872 | } | |
273e0829 | 873 | } |
0bf2eed2 | 874 | |
5b5a2ea1 | 875 | { |
5976c3fe | 876 | char *o = SSDATA (default_directory); |
5b5a2ea1 SM |
877 | |
878 | /* Make sure DEFAULT_DIRECTORY is properly expanded. | |
879 | It would be better to do this down below where we actually use | |
880 | default_directory. Unfortunately, calling Fexpand_file_name recursively | |
881 | could invoke GC, and the strings might be relocated. This would | |
882 | be annoying because we have pointers into strings lying around | |
883 | that would need adjusting, and people would add new pointers to | |
884 | the code and forget to adjust them, resulting in intermittent bugs. | |
885 | Putting this call here avoids all that crud. | |
886 | ||
887 | The EQ test avoids infinite recursion. */ | |
888 | if (! NILP (default_directory) && !EQ (default_directory, name) | |
889 | /* Save time in some common cases - as long as default_directory | |
890 | is not relative, it can be canonicalized with name below (if it | |
891 | is needed at all) without requiring it to be expanded now. */ | |
01937013 | 892 | #ifdef DOS_NT |
5b5a2ea1 SM |
893 | /* Detect MSDOS file names with drive specifiers. */ |
894 | && ! (IS_DRIVE (o[0]) && IS_DEVICE_SEP (o[1]) | |
895 | && IS_DIRECTORY_SEP (o[2])) | |
199607e4 | 896 | #ifdef WINDOWSNT |
5b5a2ea1 SM |
897 | /* Detect Windows file names in UNC format. */ |
898 | && ! (IS_DIRECTORY_SEP (o[0]) && IS_DIRECTORY_SEP (o[1])) | |
01937013 | 899 | #endif |
199607e4 RS |
900 | #else /* not DOS_NT */ |
901 | /* Detect Unix absolute file names (/... alone is not absolute on | |
902 | DOS or Windows). */ | |
5b5a2ea1 | 903 | && ! (IS_DIRECTORY_SEP (o[0])) |
199607e4 | 904 | #endif /* not DOS_NT */ |
5b5a2ea1 SM |
905 | ) |
906 | { | |
907 | struct gcpro gcpro1; | |
f14b1c68 | 908 | |
5b5a2ea1 SM |
909 | GCPRO1 (name); |
910 | default_directory = Fexpand_file_name (default_directory, Qnil); | |
911 | UNGCPRO; | |
912 | } | |
913 | } | |
2b046a72 | 914 | multibyte = STRING_MULTIBYTE (name); |
6d060996 KH |
915 | if (multibyte != STRING_MULTIBYTE (default_directory)) |
916 | { | |
917 | if (multibyte) | |
918 | default_directory = string_to_multibyte (default_directory); | |
919 | else | |
920 | { | |
921 | name = string_to_multibyte (name); | |
922 | multibyte = 1; | |
923 | } | |
924 | } | |
925 | ||
e8d32c7e | 926 | /* Make a local copy of nm[] to protect it from GC in DECODE_FILE below. */ |
38182d90 | 927 | nm = alloca (SBYTES (name) + 1); |
5976c3fe | 928 | memcpy (nm, SSDATA (name), SBYTES (name) + 1); |
199607e4 | 929 | |
565f0b98 | 930 | #ifdef DOS_NT |
f0f95d31 RS |
931 | /* Note if special escape prefix is present, but remove for now. */ |
932 | if (nm[0] == '/' && nm[1] == ':') | |
933 | { | |
934 | is_escaped = 1; | |
935 | nm += 2; | |
936 | } | |
937 | ||
199607e4 | 938 | /* Find and remove drive specifier if present; this makes nm absolute |
f0f95d31 RS |
939 | even if the rest of the name appears to be relative. Only look for |
940 | drive specifier at the beginning. */ | |
941 | if (IS_DRIVE (nm[0]) && IS_DEVICE_SEP (nm[1])) | |
942 | { | |
5976c3fe | 943 | drive = (unsigned char) nm[0]; |
f0f95d31 RS |
944 | nm += 2; |
945 | } | |
bb1ff1f4 GV |
946 | |
947 | #ifdef WINDOWSNT | |
948 | /* If we see "c://somedir", we want to strip the first slash after the | |
949 | colon when stripping the drive letter. Otherwise, this expands to | |
950 | "//somedir". */ | |
951 | if (drive && IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) | |
952 | nm++; | |
4c3c22f3 | 953 | |
e8d32c7e | 954 | /* Discard any previous drive specifier if nm is now in UNC format. */ |
199607e4 RS |
955 | if (IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) |
956 | { | |
957 | drive = 0; | |
958 | } | |
5b5a2ea1 SM |
959 | #endif /* WINDOWSNT */ |
960 | #endif /* DOS_NT */ | |
199607e4 | 961 | |
214378ec GM |
962 | /* If nm is absolute, look for `/./' or `/../' or `//''sequences; if |
963 | none are found, we can probably return right away. We will avoid | |
964 | allocating a new string if name is already fully expanded. */ | |
570d7624 | 965 | if ( |
5e570b75 | 966 | IS_DIRECTORY_SEP (nm[0]) |
199607e4 | 967 | #ifdef MSDOS |
f0f95d31 | 968 | && drive && !is_escaped |
199607e4 RS |
969 | #endif |
970 | #ifdef WINDOWSNT | |
f0f95d31 | 971 | && (drive || IS_DIRECTORY_SEP (nm[1])) && !is_escaped |
199607e4 | 972 | #endif |
570d7624 JB |
973 | ) |
974 | { | |
f14b1c68 JB |
975 | /* If it turns out that the filename we want to return is just a |
976 | suffix of FILENAME, we don't need to go through and edit | |
977 | things; we just need to construct a new string using data | |
f75d7a91 PE |
978 | starting at the middle of FILENAME. If we set LOSE, that |
979 | means we've discovered that we can't do that cool trick. */ | |
980 | bool lose = 0; | |
5976c3fe | 981 | char *p = nm; |
f14b1c68 | 982 | |
570d7624 JB |
983 | while (*p) |
984 | { | |
199607e4 | 985 | /* Since we know the name is absolute, we can assume that each |
c77d647e JB |
986 | element starts with a "/". */ |
987 | ||
c77d647e | 988 | /* "." and ".." are hairy. */ |
5e570b75 | 989 | if (IS_DIRECTORY_SEP (p[0]) |
c77d647e | 990 | && p[1] == '.' |
5e570b75 | 991 | && (IS_DIRECTORY_SEP (p[2]) |
c77d647e | 992 | || p[2] == 0 |
5e570b75 | 993 | || (p[2] == '.' && (IS_DIRECTORY_SEP (p[3]) |
c77d647e | 994 | || p[3] == 0)))) |
570d7624 | 995 | lose = 1; |
214378ec GM |
996 | /* We want to replace multiple `/' in a row with a single |
997 | slash. */ | |
998 | else if (p > nm | |
999 | && IS_DIRECTORY_SEP (p[0]) | |
1000 | && IS_DIRECTORY_SEP (p[1])) | |
1001 | lose = 1; | |
570d7624 JB |
1002 | p++; |
1003 | } | |
1004 | if (!lose) | |
1005 | { | |
199607e4 | 1006 | #ifdef DOS_NT |
087fc47a JB |
1007 | /* Make sure directories are all separated with /, but |
1008 | avoid allocation of a new string when not required. */ | |
273ac8d1 EZ |
1009 | if (multibyte) |
1010 | { | |
1011 | Lisp_Object tem_name = make_specified_string (nm, -1, strlen (nm), | |
1012 | multibyte); | |
1013 | ||
1014 | tem_name = ENCODE_FILE (tem_name); | |
1015 | dostounix_filename (SSDATA (tem_name)); | |
1016 | tem_name = DECODE_FILE (tem_name); | |
1017 | memcpy (nm, SSDATA (tem_name), SBYTES (tem_name) + 1); | |
1018 | } | |
1019 | else | |
1020 | dostounix_filename (nm); | |
199607e4 RS |
1021 | #ifdef WINDOWSNT |
1022 | if (IS_DIRECTORY_SEP (nm[1])) | |
1023 | { | |
42a5b22f | 1024 | if (strcmp (nm, SSDATA (name)) != 0) |
2b046a72 | 1025 | name = make_specified_string (nm, -1, strlen (nm), multibyte); |
199607e4 RS |
1026 | } |
1027 | else | |
1028 | #endif | |
e8d32c7e | 1029 | /* Drive must be set, so this is okay. */ |
5976c3fe | 1030 | if (strcmp (nm - 2, SSDATA (name)) != 0) |
199607e4 | 1031 | { |
3f817c73 KH |
1032 | char temp[] = " :"; |
1033 | ||
2b046a72 | 1034 | name = make_specified_string (nm, -1, p - nm, multibyte); |
3f817c73 KH |
1035 | temp[0] = DRIVE_LETTER (drive); |
1036 | name = concat2 (build_string (temp), name); | |
199607e4 RS |
1037 | } |
1038 | return name; | |
1039 | #else /* not DOS_NT */ | |
42a5b22f | 1040 | if (strcmp (nm, SSDATA (name)) == 0) |
570d7624 | 1041 | return name; |
2b046a72 | 1042 | return make_specified_string (nm, -1, strlen (nm), multibyte); |
5e570b75 | 1043 | #endif /* not DOS_NT */ |
570d7624 JB |
1044 | } |
1045 | } | |
1046 | ||
199607e4 RS |
1047 | /* At this point, nm might or might not be an absolute file name. We |
1048 | need to expand ~ or ~user if present, otherwise prefix nm with | |
1049 | default_directory if nm is not absolute, and finally collapse /./ | |
1050 | and /foo/../ sequences. | |
1051 | ||
1052 | We set newdir to be the appropriate prefix if one is needed: | |
1053 | - the relevant user directory if nm starts with ~ or ~user | |
1054 | - the specified drive's working dir (DOS/NT only) if nm does not | |
1055 | start with / | |
1056 | - the value of default_directory. | |
1057 | ||
1058 | Note that these prefixes are not guaranteed to be absolute (except | |
1059 | for the working dir of a drive). Therefore, to ensure we always | |
1060 | return an absolute name, if the final prefix is not absolute we | |
1061 | append it to the current working directory. */ | |
570d7624 JB |
1062 | |
1063 | newdir = 0; | |
1064 | ||
1065 | if (nm[0] == '~') /* prefix ~ */ | |
c77d647e | 1066 | { |
5e570b75 | 1067 | if (IS_DIRECTORY_SEP (nm[1]) |
c77d647e JB |
1068 | || nm[1] == 0) /* ~ by itself */ |
1069 | { | |
6d557778 EZ |
1070 | Lisp_Object tem; |
1071 | ||
5976c3fe PE |
1072 | if (!(newdir = egetenv ("HOME"))) |
1073 | newdir = ""; | |
199607e4 | 1074 | nm++; |
e8d32c7e | 1075 | /* `egetenv' may return a unibyte string, which will bite us since |
6d557778 | 1076 | we expect the directory to be multibyte. */ |
9c06a1f3 | 1077 | tem = build_string (newdir); |
f5c81c80 | 1078 | if (multibyte && !STRING_MULTIBYTE (tem)) |
9c06a1f3 EZ |
1079 | { |
1080 | hdir = DECODE_FILE (tem); | |
5976c3fe | 1081 | newdir = SSDATA (hdir); |
9c06a1f3 | 1082 | } |
5e570b75 | 1083 | #ifdef DOS_NT |
9a1dc3be | 1084 | collapse_newdir = 0; |
4c3c22f3 | 1085 | #endif |
c77d647e JB |
1086 | } |
1087 | else /* ~user/filename */ | |
1088 | { | |
5976c3fe | 1089 | char *o, *p; |
7c2fb837 | 1090 | for (p = nm; *p && (!IS_DIRECTORY_SEP (*p)); p++); |
5b5a2ea1 | 1091 | o = alloca (p - nm + 1); |
72af86bd | 1092 | memcpy (o, nm, p - nm); |
c77d647e JB |
1093 | o [p - nm] = 0; |
1094 | ||
4d7e6e51 | 1095 | block_input (); |
350e0088 | 1096 | pw = getpwnam (o + 1); |
4d7e6e51 | 1097 | unblock_input (); |
c77d647e JB |
1098 | if (pw) |
1099 | { | |
f5c81c80 EZ |
1100 | Lisp_Object tem; |
1101 | ||
5976c3fe | 1102 | newdir = pw->pw_dir; |
f5c81c80 EZ |
1103 | /* `getpwnam' may return a unibyte string, which will |
1104 | bite us since we expect the directory to be | |
1105 | multibyte. */ | |
1106 | tem = build_string (newdir); | |
1107 | if (multibyte && !STRING_MULTIBYTE (tem)) | |
1108 | { | |
1109 | hdir = DECODE_FILE (tem); | |
1110 | newdir = SSDATA (hdir); | |
1111 | } | |
c77d647e | 1112 | nm = p; |
199607e4 | 1113 | #ifdef DOS_NT |
9a1dc3be | 1114 | collapse_newdir = 0; |
199607e4 | 1115 | #endif |
c77d647e | 1116 | } |
e5d77022 | 1117 | |
c77d647e JB |
1118 | /* If we don't find a user of that name, leave the name |
1119 | unchanged; don't move nm forward to p. */ | |
1120 | } | |
1121 | } | |
570d7624 | 1122 | |
5e570b75 | 1123 | #ifdef DOS_NT |
199607e4 RS |
1124 | /* On DOS and Windows, nm is absolute if a drive name was specified; |
1125 | use the drive's current directory as the prefix if needed. */ | |
1126 | if (!newdir && drive) | |
1127 | { | |
e8d32c7e | 1128 | /* Get default directory if needed to make nm absolute. */ |
2893f146 | 1129 | char *adir = NULL; |
199607e4 RS |
1130 | if (!IS_DIRECTORY_SEP (nm[0])) |
1131 | { | |
2893f146 | 1132 | adir = alloca (MAXPATHLEN + 1); |
620f13b0 | 1133 | if (!getdefdir (c_toupper (drive) - 'A' + 1, adir)) |
2893f146 | 1134 | adir = NULL; |
f5c81c80 EZ |
1135 | else if (multibyte) |
1136 | { | |
1137 | Lisp_Object tem = build_string (adir); | |
1138 | ||
1139 | tem = DECODE_FILE (tem); | |
1140 | memcpy (adir, SSDATA (tem), SBYTES (tem) + 1); | |
1141 | } | |
199607e4 | 1142 | } |
2893f146 | 1143 | if (!adir) |
199607e4 | 1144 | { |
e8d32c7e | 1145 | /* Either nm starts with /, or drive isn't mounted. */ |
2893f146 PE |
1146 | adir = alloca (4); |
1147 | adir[0] = DRIVE_LETTER (drive); | |
1148 | adir[1] = ':'; | |
1149 | adir[2] = '/'; | |
1150 | adir[3] = 0; | |
199607e4 | 1151 | } |
2893f146 | 1152 | newdir = adir; |
199607e4 | 1153 | } |
5e570b75 | 1154 | #endif /* DOS_NT */ |
199607e4 RS |
1155 | |
1156 | /* Finally, if no prefix has been specified and nm is not absolute, | |
e8d32c7e | 1157 | then it must be expanded relative to default_directory. */ |
199607e4 | 1158 | |
34097368 | 1159 | if (1 |
199607e4 | 1160 | #ifndef DOS_NT |
e8d32c7e | 1161 | /* /... alone is not absolute on DOS and Windows. */ |
34097368 | 1162 | && !IS_DIRECTORY_SEP (nm[0]) |
199607e4 RS |
1163 | #endif |
1164 | #ifdef WINDOWSNT | |
34097368 | 1165 | && !(IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) |
199607e4 | 1166 | #endif |
570d7624 JB |
1167 | && !newdir) |
1168 | { | |
5976c3fe | 1169 | newdir = SSDATA (default_directory); |
f0f95d31 RS |
1170 | #ifdef DOS_NT |
1171 | /* Note if special escape prefix is present, but remove for now. */ | |
1172 | if (newdir[0] == '/' && newdir[1] == ':') | |
1173 | { | |
1174 | is_escaped = 1; | |
1175 | newdir += 2; | |
1176 | } | |
1177 | #endif | |
570d7624 JB |
1178 | } |
1179 | ||
5e570b75 | 1180 | #ifdef DOS_NT |
199607e4 RS |
1181 | if (newdir) |
1182 | { | |
e8d32c7e | 1183 | /* First ensure newdir is an absolute name. */ |
199607e4 RS |
1184 | if ( |
1185 | /* Detect MSDOS file names with drive specifiers. */ | |
1186 | ! (IS_DRIVE (newdir[0]) | |
1187 | && IS_DEVICE_SEP (newdir[1]) && IS_DIRECTORY_SEP (newdir[2])) | |
1188 | #ifdef WINDOWSNT | |
1189 | /* Detect Windows file names in UNC format. */ | |
1190 | && ! (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1191 | #endif | |
1192 | ) | |
1193 | { | |
1194 | /* Effectively, let newdir be (expand-file-name newdir cwd). | |
1195 | Because of the admonition against calling expand-file-name | |
1196 | when we have pointers into lisp strings, we accomplish this | |
1197 | indirectly by prepending newdir to nm if necessary, and using | |
e8d32c7e | 1198 | cwd (or the wd of newdir's drive) as the new newdir. */ |
2893f146 | 1199 | char *adir; |
f5c81c80 | 1200 | |
c70a4df6 | 1201 | if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1])) |
199607e4 | 1202 | { |
5976c3fe | 1203 | drive = (unsigned char) newdir[0]; |
199607e4 RS |
1204 | newdir += 2; |
1205 | } | |
1206 | if (!IS_DIRECTORY_SEP (nm[0])) | |
1207 | { | |
c293e30c DA |
1208 | ptrdiff_t newlen = strlen (newdir); |
1209 | char *tmp = alloca (newlen + strlen (nm) + 2); | |
f5c81c80 | 1210 | file_name_as_directory (tmp, newdir, newlen, multibyte); |
199607e4 RS |
1211 | strcat (tmp, nm); |
1212 | nm = tmp; | |
1213 | } | |
2893f146 | 1214 | adir = alloca (MAXPATHLEN + 1); |
199607e4 RS |
1215 | if (drive) |
1216 | { | |
620f13b0 | 1217 | if (!getdefdir (c_toupper (drive) - 'A' + 1, adir)) |
f5c81c80 | 1218 | strcpy (adir, "/"); |
199607e4 RS |
1219 | } |
1220 | else | |
9239d970 | 1221 | getcwd (adir, MAXPATHLEN + 1); |
f5c81c80 EZ |
1222 | if (multibyte) |
1223 | { | |
1224 | Lisp_Object tem = build_string (adir); | |
1225 | ||
1226 | tem = DECODE_FILE (tem); | |
1227 | memcpy (adir, SSDATA (tem), SBYTES (tem) + 1); | |
1228 | } | |
2893f146 | 1229 | newdir = adir; |
199607e4 RS |
1230 | } |
1231 | ||
e8d32c7e | 1232 | /* Strip off drive name from prefix, if present. */ |
c70a4df6 | 1233 | if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1])) |
199607e4 RS |
1234 | { |
1235 | drive = newdir[0]; | |
1236 | newdir += 2; | |
1237 | } | |
1238 | ||
1239 | /* Keep only a prefix from newdir if nm starts with slash | |
82330e7f | 1240 | (//server/share for UNC, nothing otherwise). */ |
9a1dc3be | 1241 | if (IS_DIRECTORY_SEP (nm[0]) && collapse_newdir) |
199607e4 RS |
1242 | { |
1243 | #ifdef WINDOWSNT | |
1244 | if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1245 | { | |
2893f146 PE |
1246 | char *adir = strcpy (alloca (strlen (newdir) + 1), newdir); |
1247 | char *p = adir + 2; | |
199607e4 RS |
1248 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; |
1249 | p++; | |
1250 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; | |
1251 | *p = 0; | |
2893f146 | 1252 | newdir = adir; |
199607e4 RS |
1253 | } |
1254 | else | |
1255 | #endif | |
1256 | newdir = ""; | |
1257 | } | |
1258 | } | |
5e570b75 | 1259 | #endif /* DOS_NT */ |
199607e4 RS |
1260 | |
1261 | if (newdir) | |
bfb61299 | 1262 | { |
57676091 | 1263 | /* Get rid of any slash at the end of newdir, unless newdir is |
f0f95d31 | 1264 | just / or // (an incomplete UNC name). */ |
199607e4 | 1265 | length = strlen (newdir); |
c293e30c | 1266 | tlen = length + 1; |
f0f95d31 | 1267 | if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1]) |
57676091 RS |
1268 | #ifdef WINDOWSNT |
1269 | && !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) | |
1270 | #endif | |
1271 | ) | |
bfb61299 | 1272 | { |
38182d90 | 1273 | char *temp = alloca (length); |
72af86bd | 1274 | memcpy (temp, newdir, length - 1); |
bfb61299 | 1275 | temp[length - 1] = 0; |
c293e30c | 1276 | length--; |
bfb61299 JB |
1277 | newdir = temp; |
1278 | } | |
bfb61299 JB |
1279 | } |
1280 | else | |
c293e30c DA |
1281 | { |
1282 | length = 0; | |
1283 | tlen = 0; | |
1284 | } | |
570d7624 | 1285 | |
e8d32c7e | 1286 | /* Now concatenate the directory and name to new space in the stack frame. */ |
bfb61299 | 1287 | tlen += strlen (nm) + 1; |
5e570b75 | 1288 | #ifdef DOS_NT |
f0f95d31 RS |
1289 | /* Reserve space for drive specifier and escape prefix, since either |
1290 | or both may need to be inserted. (The Microsoft x86 compiler | |
5e570b75 | 1291 | produces incorrect code if the following two lines are combined.) */ |
38182d90 | 1292 | target = alloca (tlen + 4); |
f0f95d31 | 1293 | target += 4; |
5e570b75 | 1294 | #else /* not DOS_NT */ |
38182d90 | 1295 | target = alloca (tlen); |
5e570b75 | 1296 | #endif /* not DOS_NT */ |
570d7624 JB |
1297 | *target = 0; |
1298 | ||
1299 | if (newdir) | |
1300 | { | |
5e570b75 | 1301 | if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0])) |
f5321b5c | 1302 | { |
3ea2d8b2 | 1303 | #ifdef DOS_NT |
f5321b5c RS |
1304 | /* If newdir is effectively "C:/", then the drive letter will have |
1305 | been stripped and newdir will be "/". Concatenating with an | |
1306 | absolute directory in nm produces "//", which will then be | |
1307 | incorrectly treated as a network share. Ignore newdir in | |
1308 | this case (keeping the drive letter). */ | |
efdc16c9 | 1309 | if (!(drive && nm[0] && IS_DIRECTORY_SEP (newdir[0]) |
f5321b5c RS |
1310 | && newdir[1] == '\0')) |
1311 | #endif | |
1312 | strcpy (target, newdir); | |
1313 | } | |
570d7624 | 1314 | else |
f5c81c80 | 1315 | file_name_as_directory (target, newdir, length, multibyte); |
570d7624 JB |
1316 | } |
1317 | ||
1318 | strcat (target, nm); | |
199607e4 | 1319 | |
214378ec GM |
1320 | /* Now canonicalize by removing `//', `/.' and `/foo/..' if they |
1321 | appear. */ | |
5b5a2ea1 | 1322 | { |
5976c3fe PE |
1323 | char *p = target; |
1324 | char *o = target; | |
570d7624 | 1325 | |
5b5a2ea1 SM |
1326 | while (*p) |
1327 | { | |
5b5a2ea1 SM |
1328 | if (!IS_DIRECTORY_SEP (*p)) |
1329 | { | |
1330 | *o++ = *p++; | |
1331 | } | |
1332 | else if (p[1] == '.' | |
1333 | && (IS_DIRECTORY_SEP (p[2]) | |
1334 | || p[2] == 0)) | |
1335 | { | |
1336 | /* If "/." is the entire filename, keep the "/". Otherwise, | |
1337 | just delete the whole "/.". */ | |
1338 | if (o == target && p[2] == '\0') | |
1339 | *o++ = *p; | |
1340 | p += 2; | |
1341 | } | |
1342 | else if (p[1] == '.' && p[2] == '.' | |
1343 | /* `/../' is the "superroot" on certain file systems. | |
1344 | Turned off on DOS_NT systems because they have no | |
1345 | "superroot" and because this causes us to produce | |
1346 | file names like "d:/../foo" which fail file-related | |
1347 | functions of the underlying OS. (To reproduce, try a | |
1348 | long series of "../../" in default_directory, longer | |
1349 | than the number of levels from the root.) */ | |
aa4060b9 | 1350 | #ifndef DOS_NT |
5b5a2ea1 | 1351 | && o != target |
aa4060b9 | 1352 | #endif |
5b5a2ea1 SM |
1353 | && (IS_DIRECTORY_SEP (p[3]) || p[3] == 0)) |
1354 | { | |
972ed246 | 1355 | #ifdef WINDOWSNT |
5976c3fe | 1356 | char *prev_o = o; |
972ed246 | 1357 | #endif |
5b5a2ea1 SM |
1358 | while (o != target && (--o) && !IS_DIRECTORY_SEP (*o)) |
1359 | ; | |
972ed246 JR |
1360 | #ifdef WINDOWSNT |
1361 | /* Don't go below server level in UNC filenames. */ | |
1362 | if (o == target + 1 && IS_DIRECTORY_SEP (*o) | |
1363 | && IS_DIRECTORY_SEP (*target)) | |
1364 | o = prev_o; | |
1365 | else | |
1366 | #endif | |
5b5a2ea1 SM |
1367 | /* Keep initial / only if this is the whole name. */ |
1368 | if (o == target && IS_ANY_SEP (*o) && p[3] == 0) | |
1369 | ++o; | |
1370 | p += 3; | |
1371 | } | |
1372 | else if (p > target && IS_DIRECTORY_SEP (p[1])) | |
1373 | /* Collapse multiple `/' in a row. */ | |
1374 | p++; | |
1375 | else | |
1376 | { | |
1377 | *o++ = *p++; | |
1378 | } | |
5b5a2ea1 | 1379 | } |
570d7624 | 1380 | |
5e570b75 | 1381 | #ifdef DOS_NT |
e8d32c7e | 1382 | /* At last, set drive name. */ |
5e570b75 | 1383 | #ifdef WINDOWSNT |
5b5a2ea1 SM |
1384 | /* Except for network file name. */ |
1385 | if (!(IS_DIRECTORY_SEP (target[0]) && IS_DIRECTORY_SEP (target[1]))) | |
5e570b75 | 1386 | #endif /* WINDOWSNT */ |
5b5a2ea1 | 1387 | { |
1088b922 | 1388 | if (!drive) emacs_abort (); |
5b5a2ea1 SM |
1389 | target -= 2; |
1390 | target[0] = DRIVE_LETTER (drive); | |
1391 | target[1] = ':'; | |
1392 | } | |
1393 | /* Reinsert the escape prefix if required. */ | |
1394 | if (is_escaped) | |
1395 | { | |
1396 | target -= 2; | |
1397 | target[0] = '/'; | |
1398 | target[1] = ':'; | |
1399 | } | |
5b5a2ea1 | 1400 | result = make_specified_string (target, -1, o - target, multibyte); |
f5c81c80 EZ |
1401 | if (multibyte) |
1402 | { | |
1403 | result = ENCODE_FILE (result); | |
1404 | dostounix_filename (SSDATA (result)); | |
1405 | result = DECODE_FILE (result); | |
1406 | } | |
1407 | else | |
1408 | dostounix_filename (SSDATA (result)); | |
273ac8d1 EZ |
1409 | #else /* !DOS_NT */ |
1410 | result = make_specified_string (target, -1, o - target, multibyte); | |
1411 | #endif /* !DOS_NT */ | |
5b5a2ea1 | 1412 | } |
beb402de KG |
1413 | |
1414 | /* Again look to see if the file name has special constructs in it | |
1415 | and perhaps call the corresponding file handler. This is needed | |
1416 | for filenames such as "/foo/../user@host:/bar/../baz". Expanding | |
1417 | the ".." component gives us "/user@host:/bar/../baz" which needs | |
e8d32c7e | 1418 | to be expanded again. */ |
beb402de KG |
1419 | handler = Ffind_file_name_handler (result, Qexpand_file_name); |
1420 | if (!NILP (handler)) | |
2375c96a CY |
1421 | { |
1422 | handled_name = call3 (handler, Qexpand_file_name, | |
1423 | result, default_directory); | |
1424 | if (STRINGP (handled_name)) | |
1425 | return handled_name; | |
1426 | error ("Invalid handler in `file-name-handler-alist'"); | |
1427 | } | |
beb402de KG |
1428 | |
1429 | return result; | |
570d7624 | 1430 | } |
5e570b75 | 1431 | |
4887597a EZ |
1432 | #if 0 |
1433 | /* PLEASE DO NOT DELETE THIS COMMENTED-OUT VERSION! | |
1434 | This is the old version of expand-file-name, before it was thoroughly | |
1435 | rewritten for Emacs 10.31. We leave this version here commented-out, | |
1436 | because the code is very complex and likely to have subtle bugs. If | |
1437 | bugs _are_ found, it might be of interest to look at the old code and | |
1438 | see what did it do in the relevant situation. | |
1439 | ||
30f50381 GM |
1440 | Don't remove this code: it's true that it will be accessible |
1441 | from the repository, but a few years from deletion, people will | |
1442 | forget it is there. */ | |
4887597a EZ |
1443 | |
1444 | /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'. */ | |
1445 | DEAFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, | |
1446 | "Convert FILENAME to absolute, and canonicalize it.\n\ | |
1447 | Second arg DEFAULT is directory to start with if FILENAME is relative\n\ | |
c70a4df6 | 1448 | \(does not start with slash); if DEFAULT is nil or missing,\n\ |
4887597a EZ |
1449 | the current buffer's value of default-directory is used.\n\ |
1450 | Filenames containing `.' or `..' as components are simplified;\n\ | |
1451 | initial `~/' expands to your home directory.\n\ | |
1452 | See also the function `substitute-in-file-name'.") | |
1453 | (name, defalt) | |
1454 | Lisp_Object name, defalt; | |
1455 | { | |
1456 | unsigned char *nm; | |
1457 | ||
1458 | register unsigned char *newdir, *p, *o; | |
93f4cf88 | 1459 | ptrdiff_t tlen; |
4887597a EZ |
1460 | unsigned char *target; |
1461 | struct passwd *pw; | |
4887597a | 1462 | |
b7826503 | 1463 | CHECK_STRING (name); |
d5db4077 | 1464 | nm = SDATA (name); |
4887597a EZ |
1465 | |
1466 | /* If nm is absolute, flush ...// and detect /./ and /../. | |
1467 | If no /./ or /../ we can return right away. */ | |
7c2fb837 | 1468 | if (nm[0] == '/') |
4887597a | 1469 | { |
f75d7a91 | 1470 | bool lose = 0; |
4887597a | 1471 | p = nm; |
4887597a EZ |
1472 | while (*p) |
1473 | { | |
ce2fe65a | 1474 | if (p[0] == '/' && p[1] == '/') |
4887597a EZ |
1475 | nm = p + 1; |
1476 | if (p[0] == '/' && p[1] == '~') | |
1477 | nm = p + 1, lose = 1; | |
1478 | if (p[0] == '/' && p[1] == '.' | |
1479 | && (p[2] == '/' || p[2] == 0 | |
1480 | || (p[2] == '.' && (p[3] == '/' || p[3] == 0)))) | |
1481 | lose = 1; | |
4887597a EZ |
1482 | p++; |
1483 | } | |
1484 | if (!lose) | |
1485 | { | |
d5db4077 | 1486 | if (nm == SDATA (name)) |
4887597a EZ |
1487 | return name; |
1488 | return build_string (nm); | |
1489 | } | |
1490 | } | |
1491 | ||
e8d32c7e | 1492 | /* Now determine directory to start with and put it in NEWDIR. */ |
4887597a EZ |
1493 | |
1494 | newdir = 0; | |
1495 | ||
1496 | if (nm[0] == '~') /* prefix ~ */ | |
7c2fb837 | 1497 | if (nm[1] == '/' || nm[1] == 0)/* ~/filename */ |
4887597a EZ |
1498 | { |
1499 | if (!(newdir = (unsigned char *) egetenv ("HOME"))) | |
1500 | newdir = (unsigned char *) ""; | |
1501 | nm++; | |
4887597a EZ |
1502 | } |
1503 | else /* ~user/filename */ | |
1504 | { | |
e8d32c7e | 1505 | /* Get past ~ to user. */ |
4887597a | 1506 | unsigned char *user = nm + 1; |
e8d32c7e | 1507 | /* Find end of name. */ |
8966b757 | 1508 | unsigned char *ptr = (unsigned char *) strchr (user, '/'); |
93f4cf88 | 1509 | ptrdiff_t len = ptr ? ptr - user : strlen (user); |
e8d32c7e | 1510 | /* Copy the user name into temp storage. */ |
38182d90 | 1511 | o = alloca (len + 1); |
72af86bd | 1512 | memcpy (o, user, len); |
4887597a EZ |
1513 | o[len] = 0; |
1514 | ||
e8d32c7e | 1515 | /* Look up the user name. */ |
4d7e6e51 | 1516 | block_input (); |
4887597a | 1517 | pw = (struct passwd *) getpwnam (o + 1); |
4d7e6e51 | 1518 | unblock_input (); |
4887597a EZ |
1519 | if (!pw) |
1520 | error ("\"%s\" isn't a registered user", o + 1); | |
1521 | ||
1522 | newdir = (unsigned char *) pw->pw_dir; | |
1523 | ||
1524 | /* Discard the user name from NM. */ | |
1525 | nm += len; | |
1526 | } | |
1527 | ||
7c2fb837 | 1528 | if (nm[0] != '/' && !newdir) |
4887597a EZ |
1529 | { |
1530 | if (NILP (defalt)) | |
1531 | defalt = current_buffer->directory; | |
b7826503 | 1532 | CHECK_STRING (defalt); |
d5db4077 | 1533 | newdir = SDATA (defalt); |
4887597a EZ |
1534 | } |
1535 | ||
e8d32c7e | 1536 | /* Now concatenate the directory and name to new space in the stack frame. */ |
4887597a EZ |
1537 | |
1538 | tlen = (newdir ? strlen (newdir) + 1 : 0) + strlen (nm) + 1; | |
38182d90 | 1539 | target = alloca (tlen); |
4887597a EZ |
1540 | *target = 0; |
1541 | ||
1542 | if (newdir) | |
1543 | { | |
4887597a EZ |
1544 | if (nm[0] == 0 || nm[0] == '/') |
1545 | strcpy (target, newdir); | |
1546 | else | |
4887597a EZ |
1547 | file_name_as_directory (target, newdir); |
1548 | } | |
1549 | ||
1550 | strcat (target, nm); | |
4887597a | 1551 | |
e8d32c7e | 1552 | /* Now canonicalize by removing /. and /foo/.. if they appear. */ |
4887597a EZ |
1553 | |
1554 | p = target; | |
1555 | o = target; | |
1556 | ||
1557 | while (*p) | |
1558 | { | |
4887597a EZ |
1559 | if (*p != '/') |
1560 | { | |
1561 | *o++ = *p++; | |
1562 | } | |
1563 | else if (!strncmp (p, "//", 2) | |
4887597a EZ |
1564 | ) |
1565 | { | |
1566 | o = target; | |
1567 | p++; | |
1568 | } | |
994a7262 RS |
1569 | else if (p[0] == '/' && p[1] == '.' |
1570 | && (p[2] == '/' || p[2] == 0)) | |
4887597a EZ |
1571 | p += 2; |
1572 | else if (!strncmp (p, "/..", 3) | |
1573 | /* `/../' is the "superroot" on certain file systems. */ | |
1574 | && o != target | |
1575 | && (p[3] == '/' || p[3] == 0)) | |
1576 | { | |
1577 | while (o != target && *--o != '/') | |
1578 | ; | |
4887597a EZ |
1579 | if (o == target && *o == '/') |
1580 | ++o; | |
1581 | p += 3; | |
1582 | } | |
1583 | else | |
1584 | { | |
1585 | *o++ = *p++; | |
1586 | } | |
4887597a EZ |
1587 | } |
1588 | ||
1589 | return make_string (target, o - target); | |
1590 | } | |
1591 | #endif | |
570d7624 | 1592 | \f |
c70a4df6 | 1593 | /* If /~ or // appears, discard everything through first slash. */ |
f75d7a91 | 1594 | static bool |
5976c3fe | 1595 | file_name_absolute_p (const char *filename) |
c70a4df6 SM |
1596 | { |
1597 | return | |
1598 | (IS_DIRECTORY_SEP (*filename) || *filename == '~' | |
c70a4df6 SM |
1599 | #ifdef DOS_NT |
1600 | || (IS_DRIVE (*filename) && IS_DEVICE_SEP (filename[1]) | |
1601 | && IS_DIRECTORY_SEP (filename[2])) | |
1602 | #endif | |
1603 | ); | |
1604 | } | |
1605 | ||
5976c3fe PE |
1606 | static char * |
1607 | search_embedded_absfilename (char *nm, char *endp) | |
c70a4df6 | 1608 | { |
5976c3fe | 1609 | char *p, *s; |
c70a4df6 SM |
1610 | |
1611 | for (p = nm + 1; p < endp; p++) | |
1612 | { | |
ce2fe65a | 1613 | if (IS_DIRECTORY_SEP (p[-1]) |
c70a4df6 | 1614 | && file_name_absolute_p (p) |
5e617bc2 | 1615 | #if defined (WINDOWSNT) || defined (CYGWIN) |
c70a4df6 SM |
1616 | /* // at start of file name is meaningful in Apollo, |
1617 | WindowsNT and Cygwin systems. */ | |
f34574c6 | 1618 | && !(IS_DIRECTORY_SEP (p[0]) && p - 1 == nm) |
e39a993c | 1619 | #endif /* not (WINDOWSNT || CYGWIN) */ |
ce2fe65a | 1620 | ) |
c70a4df6 | 1621 | { |
ce2fe65a | 1622 | for (s = p; *s && !IS_DIRECTORY_SEP (*s); s++); |
e8d32c7e | 1623 | if (p[0] == '~' && s > p + 1) /* We've got "/~something/". */ |
c70a4df6 | 1624 | { |
5976c3fe | 1625 | char *o = alloca (s - p + 1); |
c70a4df6 | 1626 | struct passwd *pw; |
72af86bd | 1627 | memcpy (o, p, s - p); |
c70a4df6 SM |
1628 | o [s - p] = 0; |
1629 | ||
1630 | /* If we have ~user and `user' exists, discard | |
1631 | everything up to ~. But if `user' does not exist, leave | |
1632 | ~user alone, it might be a literal file name. */ | |
4d7e6e51 | 1633 | block_input (); |
67c08d6c | 1634 | pw = getpwnam (o + 1); |
4d7e6e51 | 1635 | unblock_input (); |
67c08d6c | 1636 | if (pw) |
c70a4df6 | 1637 | return p; |
c70a4df6 SM |
1638 | } |
1639 | else | |
1640 | return p; | |
1641 | } | |
1642 | } | |
1643 | return NULL; | |
1644 | } | |
1645 | ||
a7ca3326 | 1646 | DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, |
8c1a1077 PJ |
1647 | Ssubstitute_in_file_name, 1, 1, 0, |
1648 | doc: /* Substitute environment variables referred to in FILENAME. | |
1649 | `$FOO' where FOO is an environment variable name means to substitute | |
1650 | the value of that variable. The variable name should be terminated | |
1651 | with a character not a letter, digit or underscore; otherwise, enclose | |
1652 | the entire variable name in braces. | |
c68845e0 GM |
1653 | |
1654 | If `/~' appears, all of FILENAME through that `/' is discarded. | |
1655 | If `//' appears, everything up to and including the first of | |
1656 | those `/' is discarded. */) | |
5842a27b | 1657 | (Lisp_Object filename) |
570d7624 | 1658 | { |
f75d7a91 | 1659 | char *nm, *s, *p, *o, *x, *endp; |
5976c3fe | 1660 | char *target = NULL; |
5f460827 | 1661 | ptrdiff_t total = 0; |
f75d7a91 | 1662 | bool substituted = 0; |
fce31d69 | 1663 | bool multibyte; |
5976c3fe | 1664 | char *xnm; |
8ce069f5 | 1665 | Lisp_Object handler; |
570d7624 | 1666 | |
b7826503 | 1667 | CHECK_STRING (filename); |
570d7624 | 1668 | |
58aec0d6 JR |
1669 | multibyte = STRING_MULTIBYTE (filename); |
1670 | ||
8ce069f5 RS |
1671 | /* If the file name has special constructs in it, |
1672 | call the corresponding file handler. */ | |
3b7f6e60 | 1673 | handler = Ffind_file_name_handler (filename, Qsubstitute_in_file_name); |
8ce069f5 | 1674 | if (!NILP (handler)) |
2375c96a CY |
1675 | { |
1676 | Lisp_Object handled_name = call2 (handler, Qsubstitute_in_file_name, | |
1677 | filename); | |
1678 | if (STRINGP (handled_name)) | |
1679 | return handled_name; | |
1680 | error ("Invalid handler in `file-name-handler-alist'"); | |
1681 | } | |
8ce069f5 | 1682 | |
58aec0d6 JR |
1683 | /* Always work on a copy of the string, in case GC happens during |
1684 | decode of environment variables, causing the original Lisp_String | |
1685 | data to be relocated. */ | |
38182d90 | 1686 | nm = alloca (SBYTES (filename) + 1); |
72af86bd | 1687 | memcpy (nm, SDATA (filename), SBYTES (filename) + 1); |
0f3f018c | 1688 | |
58aec0d6 | 1689 | #ifdef DOS_NT |
f5c81c80 EZ |
1690 | if (multibyte) |
1691 | { | |
1692 | Lisp_Object encoded_filename = ENCODE_FILE (filename); | |
1693 | Lisp_Object tem_fn; | |
1694 | ||
1695 | dostounix_filename (SDATA (encoded_filename)); | |
1696 | tem_fn = DECODE_FILE (encoded_filename); | |
1697 | nm = alloca (SBYTES (tem_fn) + 1); | |
1698 | memcpy (nm, SDATA (tem_fn), SBYTES (tem_fn) + 1); | |
1699 | substituted = (memcmp (nm, SDATA (filename), SBYTES (filename)) != 0); | |
1700 | if (substituted) | |
1701 | filename = tem_fn; | |
1702 | } | |
1703 | else | |
1704 | { | |
1705 | dostounix_filename (nm); | |
1706 | substituted = (memcmp (nm, SDATA (filename), SBYTES (filename)) != 0); | |
1707 | } | |
a5a1cc06 | 1708 | #endif |
d5db4077 | 1709 | endp = nm + SBYTES (filename); |
570d7624 | 1710 | |
82330e7f | 1711 | /* If /~ or // appears, discard everything through first slash. */ |
c70a4df6 SM |
1712 | p = search_embedded_absfilename (nm, endp); |
1713 | if (p) | |
1714 | /* Start over with the new string, so we check the file-name-handler | |
1715 | again. Important with filenames like "/home/foo//:/hello///there" | |
ee7683eb | 1716 | which would substitute to "/:/hello///there" rather than "/there". */ |
c70a4df6 | 1717 | return Fsubstitute_in_file_name |
58aec0d6 | 1718 | (make_specified_string (p, -1, endp - p, multibyte)); |
570d7624 JB |
1719 | |
1720 | /* See if any variables are substituted into the string | |
e8d32c7e | 1721 | and find the total length of their values in `total'. */ |
570d7624 JB |
1722 | |
1723 | for (p = nm; p != endp;) | |
1724 | if (*p != '$') | |
1725 | p++; | |
1726 | else | |
1727 | { | |
1728 | p++; | |
1729 | if (p == endp) | |
1730 | goto badsubst; | |
1731 | else if (*p == '$') | |
1732 | { | |
e8d32c7e | 1733 | /* "$$" means a single "$". */ |
570d7624 JB |
1734 | p++; |
1735 | total -= 1; | |
1736 | substituted = 1; | |
1737 | continue; | |
1738 | } | |
1739 | else if (*p == '{') | |
1740 | { | |
1741 | o = ++p; | |
1742 | while (p != endp && *p != '}') p++; | |
1743 | if (*p != '}') goto missingclose; | |
1744 | s = p; | |
1745 | } | |
1746 | else | |
1747 | { | |
1748 | o = p; | |
620f13b0 | 1749 | while (p != endp && (c_isalnum (*p) || *p == '_')) p++; |
570d7624 JB |
1750 | s = p; |
1751 | } | |
1752 | ||
e8d32c7e | 1753 | /* Copy out the variable name. */ |
38182d90 | 1754 | target = alloca (s - o + 1); |
e99a530f | 1755 | memcpy (target, o, s - o); |
570d7624 | 1756 | target[s - o] = 0; |
5e570b75 | 1757 | #ifdef DOS_NT |
4c3c22f3 | 1758 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 1759 | #endif /* DOS_NT */ |
570d7624 | 1760 | |
e8d32c7e | 1761 | /* Get variable value. */ |
5976c3fe | 1762 | o = egetenv (target); |
8d2ced53 | 1763 | if (o) |
58aec0d6 JR |
1764 | { |
1765 | /* Don't try to guess a maximum length - UTF8 can use up to | |
1766 | four bytes per character. This code is unlikely to run | |
1767 | in a situation that requires performance, so decoding the | |
1768 | env variables twice should be acceptable. Note that | |
1769 | decoding may cause a garbage collect. */ | |
1770 | Lisp_Object orig, decoded; | |
d7ea76b4 | 1771 | orig = build_unibyte_string (o); |
58aec0d6 JR |
1772 | decoded = DECODE_FILE (orig); |
1773 | total += SBYTES (decoded); | |
8d2ced53 SM |
1774 | substituted = 1; |
1775 | } | |
1776 | else if (*p == '}') | |
1777 | goto badvar; | |
570d7624 JB |
1778 | } |
1779 | ||
1780 | if (!substituted) | |
3b7f6e60 | 1781 | return filename; |
570d7624 | 1782 | |
e8d32c7e SM |
1783 | /* If substitution required, recopy the string and do it. */ |
1784 | /* Make space in stack frame for the new copy. */ | |
38182d90 | 1785 | xnm = alloca (SBYTES (filename) + total + 1); |
570d7624 JB |
1786 | x = xnm; |
1787 | ||
e8d32c7e | 1788 | /* Copy the rest of the name through, replacing $ constructs with values. */ |
570d7624 JB |
1789 | for (p = nm; *p;) |
1790 | if (*p != '$') | |
1791 | *x++ = *p++; | |
1792 | else | |
1793 | { | |
1794 | p++; | |
1795 | if (p == endp) | |
1796 | goto badsubst; | |
1797 | else if (*p == '$') | |
1798 | { | |
1799 | *x++ = *p++; | |
1800 | continue; | |
1801 | } | |
1802 | else if (*p == '{') | |
1803 | { | |
1804 | o = ++p; | |
1805 | while (p != endp && *p != '}') p++; | |
1806 | if (*p != '}') goto missingclose; | |
1807 | s = p++; | |
1808 | } | |
1809 | else | |
1810 | { | |
1811 | o = p; | |
620f13b0 | 1812 | while (p != endp && (c_isalnum (*p) || *p == '_')) p++; |
570d7624 JB |
1813 | s = p; |
1814 | } | |
1815 | ||
e8d32c7e | 1816 | /* Copy out the variable name. */ |
38182d90 | 1817 | target = alloca (s - o + 1); |
e99a530f | 1818 | memcpy (target, o, s - o); |
570d7624 | 1819 | target[s - o] = 0; |
5e570b75 | 1820 | #ifdef DOS_NT |
4c3c22f3 | 1821 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 1822 | #endif /* DOS_NT */ |
570d7624 | 1823 | |
e8d32c7e | 1824 | /* Get variable value. */ |
5976c3fe | 1825 | o = egetenv (target); |
570d7624 | 1826 | if (!o) |
8d2ced53 SM |
1827 | { |
1828 | *x++ = '$'; | |
1829 | strcpy (x, target); x+= strlen (target); | |
1830 | } | |
60d67b83 RS |
1831 | else |
1832 | { | |
58aec0d6 | 1833 | Lisp_Object orig, decoded; |
93f4cf88 | 1834 | ptrdiff_t orig_length, decoded_length; |
58aec0d6 JR |
1835 | orig_length = strlen (o); |
1836 | orig = make_unibyte_string (o, orig_length); | |
1837 | decoded = DECODE_FILE (orig); | |
1838 | decoded_length = SBYTES (decoded); | |
e99a530f | 1839 | memcpy (x, SDATA (decoded), decoded_length); |
58aec0d6 JR |
1840 | x += decoded_length; |
1841 | ||
1842 | /* If environment variable needed decoding, return value | |
1843 | needs to be multibyte. */ | |
1844 | if (decoded_length != orig_length | |
e99a530f | 1845 | || memcmp (SDATA (decoded), o, orig_length)) |
58aec0d6 | 1846 | multibyte = 1; |
60d67b83 | 1847 | } |
570d7624 JB |
1848 | } |
1849 | ||
1850 | *x = 0; | |
1851 | ||
82330e7f | 1852 | /* If /~ or // appears, discard everything through first slash. */ |
ce2fe65a | 1853 | while ((p = search_embedded_absfilename (xnm, x)) != NULL) |
c70a4df6 SM |
1854 | /* This time we do not start over because we've already expanded envvars |
1855 | and replaced $$ with $. Maybe we should start over as well, but we'd | |
1856 | need to quote some $ to $$ first. */ | |
1857 | xnm = p; | |
570d7624 | 1858 | |
58aec0d6 | 1859 | return make_specified_string (xnm, -1, x - xnm, multibyte); |
570d7624 JB |
1860 | |
1861 | badsubst: | |
1862 | error ("Bad format environment-variable substitution"); | |
1863 | missingclose: | |
1864 | error ("Missing \"}\" in environment-variable substitution"); | |
1865 | badvar: | |
1866 | error ("Substituting nonexistent environment variable \"%s\"", target); | |
1867 | ||
1868 | /* NOTREACHED */ | |
6bbd7a29 | 1869 | return Qnil; |
570d7624 JB |
1870 | } |
1871 | \f | |
067ffa38 | 1872 | /* A slightly faster and more convenient way to get |
298b760e | 1873 | (directory-file-name (expand-file-name FOO)). */ |
067ffa38 | 1874 | |
570d7624 | 1875 | Lisp_Object |
971de7fb | 1876 | expand_and_dir_to_file (Lisp_Object filename, Lisp_Object defdir) |
570d7624 | 1877 | { |
199607e4 | 1878 | register Lisp_Object absname; |
570d7624 | 1879 | |
199607e4 | 1880 | absname = Fexpand_file_name (filename, defdir); |
7c2fb837 | 1881 | |
199607e4 | 1882 | /* Remove final slash, if any (unless this is the root dir). |
570d7624 | 1883 | stat behaves differently depending! */ |
d5db4077 KR |
1884 | if (SCHARS (absname) > 1 |
1885 | && IS_DIRECTORY_SEP (SREF (absname, SBYTES (absname) - 1)) | |
e8d32c7e | 1886 | && !IS_DEVICE_SEP (SREF (absname, SBYTES (absname) - 2))) |
ddc61f46 | 1887 | /* We cannot take shortcuts; they might be wrong for magic file names. */ |
199607e4 | 1888 | absname = Fdirectory_file_name (absname); |
199607e4 | 1889 | return absname; |
570d7624 JB |
1890 | } |
1891 | \f | |
3ed15d97 | 1892 | /* Signal an error if the file ABSNAME already exists. |
f75d7a91 | 1893 | If INTERACTIVE, ask the user whether to proceed, |
3ed15d97 RS |
1894 | and bypass the error if the user says to go ahead. |
1895 | QUERYSTRING is a name for the action that is being considered | |
1896 | to alter the file. | |
de1d0127 | 1897 | |
3ed15d97 | 1898 | *STATPTR is used to store the stat information if the file exists. |
de1d0127 | 1899 | If the file does not exist, STATPTR->st_mode is set to 0. |
b8b29dc9 RS |
1900 | If STATPTR is null, we don't store into it. |
1901 | ||
f75d7a91 | 1902 | If QUICK, ask for y or n, not yes or no. */ |
3ed15d97 | 1903 | |
f14b7e14 | 1904 | static void |
5976c3fe | 1905 | barf_or_query_if_file_exists (Lisp_Object absname, const char *querystring, |
f75d7a91 PE |
1906 | bool interactive, struct stat *statptr, |
1907 | bool quick) | |
570d7624 | 1908 | { |
f75d7a91 | 1909 | Lisp_Object tem, encoded_filename; |
4018b5ef | 1910 | struct stat statbuf; |
570d7624 JB |
1911 | struct gcpro gcpro1; |
1912 | ||
643c73b9 RS |
1913 | encoded_filename = ENCODE_FILE (absname); |
1914 | ||
e8d32c7e | 1915 | /* `stat' is a good way to tell whether the file exists, |
4018b5ef | 1916 | regardless of what access permissions it has. */ |
42a5b22f | 1917 | if (lstat (SSDATA (encoded_filename), &statbuf) >= 0) |
570d7624 | 1918 | { |
6e9b2be9 | 1919 | if (S_ISDIR (statbuf.st_mode)) |
c4f2d8d4 | 1920 | xsignal2 (Qfile_error, |
ad637907 | 1921 | build_string ("File is a directory"), absname); |
c4f2d8d4 | 1922 | |
570d7624 | 1923 | if (! interactive) |
24b1ddad KS |
1924 | xsignal2 (Qfile_already_exists, |
1925 | build_string ("File already exists"), absname); | |
570d7624 | 1926 | GCPRO1 (absname); |
67e8e2b8 RS |
1927 | tem = format2 ("File %s already exists; %s anyway? ", |
1928 | absname, build_string (querystring)); | |
b8b29dc9 | 1929 | if (quick) |
5616cc54 | 1930 | tem = call1 (intern ("y-or-n-p"), tem); |
b8b29dc9 RS |
1931 | else |
1932 | tem = do_yes_or_no_p (tem); | |
570d7624 | 1933 | UNGCPRO; |
265a9e55 | 1934 | if (NILP (tem)) |
24b1ddad KS |
1935 | xsignal2 (Qfile_already_exists, |
1936 | build_string ("File already exists"), absname); | |
3ed15d97 RS |
1937 | if (statptr) |
1938 | *statptr = statbuf; | |
1939 | } | |
1940 | else | |
1941 | { | |
1942 | if (statptr) | |
1943 | statptr->st_mode = 0; | |
570d7624 JB |
1944 | } |
1945 | return; | |
1946 | } | |
1947 | ||
574c05e2 | 1948 | DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 6, |
7c0f6118 | 1949 | "fCopy file: \nGCopy %s to file: \np\nP", |
8c1a1077 PJ |
1950 | doc: /* Copy FILE to NEWNAME. Both args must be strings. |
1951 | If NEWNAME names a directory, copy FILE there. | |
795c20df CY |
1952 | |
1953 | This function always sets the file modes of the output file to match | |
1954 | the input file. | |
1955 | ||
1956 | The optional third argument OK-IF-ALREADY-EXISTS specifies what to do | |
1957 | if file NEWNAME already exists. If OK-IF-ALREADY-EXISTS is nil, we | |
1958 | signal a `file-already-exists' error without overwriting. If | |
1959 | OK-IF-ALREADY-EXISTS is a number, we request confirmation from the user | |
1960 | about overwriting; this is what happens in interactive use with M-x. | |
1961 | Any other value for OK-IF-ALREADY-EXISTS means to overwrite the | |
1962 | existing file. | |
c50f15d0 | 1963 | |
7f08b80e | 1964 | Fourth arg KEEP-TIME non-nil means give the output file the same |
8c1a1077 | 1965 | last-modified time as the old one. (This works on only some systems.) |
c50f15d0 | 1966 | |
7fce7dfe EZ |
1967 | A prefix arg makes KEEP-TIME non-nil. |
1968 | ||
586702ce | 1969 | If PRESERVE-UID-GID is non-nil, we try to transfer the |
574c05e2 KK |
1970 | uid and gid of FILE to NEWNAME. |
1971 | ||
7c3d167f RF |
1972 | If PRESERVE-EXTENDED-ATTRIBUTES is non-nil, we try to copy additional |
1973 | attributes of FILE to NEWNAME, such as its SELinux context and ACL | |
1974 | entries (depending on how Emacs was built). */) | |
1975 | (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists, Lisp_Object keep_time, Lisp_Object preserve_uid_gid, Lisp_Object preserve_extended_attributes) | |
570d7624 | 1976 | { |
8a2cbd72 | 1977 | int ifd, ofd; |
d311d28c | 1978 | int n; |
570d7624 | 1979 | char buf[16 * 1024]; |
3ed15d97 | 1980 | struct stat st, out_st; |
32f4334d | 1981 | Lisp_Object handler; |
b1d1b865 | 1982 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
d311d28c | 1983 | ptrdiff_t count = SPECPDL_INDEX (); |
b1d1b865 | 1984 | Lisp_Object encoded_file, encoded_newname; |
574c05e2 KK |
1985 | #if HAVE_LIBSELINUX |
1986 | security_context_t con; | |
f75d7a91 | 1987 | int conlength = 0; |
574c05e2 | 1988 | #endif |
7c3d167f RF |
1989 | #ifdef HAVE_POSIX_ACL |
1990 | acl_t acl = NULL; | |
1991 | #endif | |
570d7624 | 1992 | |
b1d1b865 RS |
1993 | encoded_file = encoded_newname = Qnil; |
1994 | GCPRO4 (file, newname, encoded_file, encoded_newname); | |
b7826503 PJ |
1995 | CHECK_STRING (file); |
1996 | CHECK_STRING (newname); | |
b1d1b865 | 1997 | |
a9d14e54 | 1998 | if (!NILP (Ffile_directory_p (newname))) |
6b61353c | 1999 | newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname); |
a9d14e54 GM |
2000 | else |
2001 | newname = Fexpand_file_name (newname, Qnil); | |
2002 | ||
3b7f6e60 | 2003 | file = Fexpand_file_name (file, Qnil); |
32f4334d | 2004 | |
0bf2eed2 | 2005 | /* If the input file name has special constructs in it, |
32f4334d | 2006 | call the corresponding file handler. */ |
3b7f6e60 | 2007 | handler = Ffind_file_name_handler (file, Qcopy_file); |
0bf2eed2 | 2008 | /* Likewise for output file name. */ |
51cf6d37 | 2009 | if (NILP (handler)) |
49307295 | 2010 | handler = Ffind_file_name_handler (newname, Qcopy_file); |
32f4334d | 2011 | if (!NILP (handler)) |
574c05e2 KK |
2012 | RETURN_UNGCPRO (call7 (handler, Qcopy_file, file, newname, |
2013 | ok_if_already_exists, keep_time, preserve_uid_gid, | |
7c3d167f | 2014 | preserve_extended_attributes)); |
32f4334d | 2015 | |
b1d1b865 RS |
2016 | encoded_file = ENCODE_FILE (file); |
2017 | encoded_newname = ENCODE_FILE (newname); | |
2018 | ||
265a9e55 | 2019 | if (NILP (ok_if_already_exists) |
93c30b5f | 2020 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2021 | barf_or_query_if_file_exists (newname, "copy to it", |
b8b29dc9 | 2022 | INTEGERP (ok_if_already_exists), &out_st, 0); |
42a5b22f | 2023 | else if (stat (SSDATA (encoded_newname), &out_st) < 0) |
3ed15d97 | 2024 | out_st.st_mode = 0; |
570d7624 | 2025 | |
e8691c59 | 2026 | #ifdef WINDOWSNT |
66447e07 EZ |
2027 | if (!NILP (preserve_extended_attributes)) |
2028 | { | |
2029 | #ifdef HAVE_POSIX_ACL | |
2030 | acl = acl_get_file (SDATA (encoded_file), ACL_TYPE_ACCESS); | |
2031 | if (acl == NULL && errno != ENOTSUP) | |
2032 | report_file_error ("Getting ACL", Fcons (file, Qnil)); | |
2033 | #endif | |
2034 | } | |
d5db4077 | 2035 | if (!CopyFile (SDATA (encoded_file), |
efdc16c9 | 2036 | SDATA (encoded_newname), |
e8691c59 | 2037 | FALSE)) |
8d23a331 EZ |
2038 | { |
2039 | /* CopyFile doesn't set errno when it fails. By far the most | |
2040 | "popular" reason is that the target is read-only. */ | |
2041 | if (GetLastError () == 5) | |
2042 | errno = EACCES; | |
2043 | else | |
2044 | errno = EPERM; | |
2045 | report_file_error ("Copying file", Fcons (file, Fcons (newname, Qnil))); | |
2046 | } | |
8b53dc06 JR |
2047 | /* CopyFile retains the timestamp by default. */ |
2048 | else if (NILP (keep_time)) | |
e8691c59 GM |
2049 | { |
2050 | EMACS_TIME now; | |
ad497129 JR |
2051 | DWORD attributes; |
2052 | char * filename; | |
2053 | ||
d5db4077 | 2054 | filename = SDATA (encoded_newname); |
ad497129 JR |
2055 | |
2056 | /* Ensure file is writable while its modified time is set. */ | |
2057 | attributes = GetFileAttributes (filename); | |
02cca86b | 2058 | SetFileAttributes (filename, attributes & ~FILE_ATTRIBUTE_READONLY); |
e9a9ae03 | 2059 | now = current_emacs_time (); |
d35af63c | 2060 | if (set_file_times (-1, filename, now, now)) |
ad497129 JR |
2061 | { |
2062 | /* Restore original attributes. */ | |
2063 | SetFileAttributes (filename, attributes); | |
24b1ddad KS |
2064 | xsignal2 (Qfile_date_error, |
2065 | build_string ("Cannot set file date"), newname); | |
ad497129 JR |
2066 | } |
2067 | /* Restore original attributes. */ | |
2068 | SetFileAttributes (filename, attributes); | |
e8691c59 | 2069 | } |
66447e07 EZ |
2070 | #ifdef HAVE_POSIX_ACL |
2071 | if (acl != NULL) | |
2072 | { | |
2073 | bool fail = | |
2074 | acl_set_file (SDATA (encoded_newname), ACL_TYPE_ACCESS, acl) != 0; | |
2075 | if (fail && errno != ENOTSUP) | |
2076 | report_file_error ("Setting ACL", Fcons (newname, Qnil)); | |
2077 | ||
2078 | acl_free (acl); | |
2079 | } | |
2080 | #endif | |
e8691c59 | 2081 | #else /* not WINDOWSNT */ |
13336908 | 2082 | immediate_quit = 1; |
42a5b22f | 2083 | ifd = emacs_open (SSDATA (encoded_file), O_RDONLY, 0); |
13336908 RS |
2084 | immediate_quit = 0; |
2085 | ||
570d7624 | 2086 | if (ifd < 0) |
3b7f6e60 | 2087 | report_file_error ("Opening input file", Fcons (file, Qnil)); |
570d7624 | 2088 | |
b5148e85 RS |
2089 | record_unwind_protect (close_file_unwind, make_number (ifd)); |
2090 | ||
d20704ef PE |
2091 | if (fstat (ifd, &st) != 0) |
2092 | report_file_error ("Input file status", Fcons (file, Qnil)); | |
f73b0ada | 2093 | |
7c3d167f | 2094 | if (!NILP (preserve_extended_attributes)) |
574c05e2 | 2095 | { |
7c3d167f RF |
2096 | #if HAVE_LIBSELINUX |
2097 | if (is_selinux_enabled ()) | |
2098 | { | |
2099 | conlength = fgetfilecon (ifd, &con); | |
2100 | if (conlength == -1) | |
2101 | report_file_error ("Doing fgetfilecon", Fcons (file, Qnil)); | |
2102 | } | |
2103 | #endif | |
2104 | ||
2105 | #ifdef HAVE_POSIX_ACL | |
2106 | acl = acl_get_fd (ifd); | |
2107 | if (acl == NULL && errno != ENOTSUP) | |
2108 | report_file_error ("Getting ACL", Fcons (file, Qnil)); | |
574c05e2 | 2109 | #endif |
7c3d167f | 2110 | } |
574c05e2 | 2111 | |
3ed15d97 RS |
2112 | if (out_st.st_mode != 0 |
2113 | && st.st_dev == out_st.st_dev && st.st_ino == out_st.st_ino) | |
2114 | { | |
2115 | errno = 0; | |
2116 | report_file_error ("Input and output files are the same", | |
3b7f6e60 | 2117 | Fcons (file, Fcons (newname, Qnil))); |
3ed15d97 | 2118 | } |
3ed15d97 | 2119 | |
d20704ef PE |
2120 | /* We can copy only regular files. */ |
2121 | if (!S_ISREG (st.st_mode)) | |
f73b0ada | 2122 | { |
d20704ef PE |
2123 | /* Get a better looking error message. */ |
2124 | errno = S_ISDIR (st.st_mode) ? EISDIR : EINVAL; | |
2125 | report_file_error ("Non-regular file", Fcons (file, Qnil)); | |
f73b0ada | 2126 | } |
f73b0ada | 2127 | |
4c3c22f3 RS |
2128 | #ifdef MSDOS |
2129 | /* System's default file type was set to binary by _fmode in emacs.c. */ | |
c50f15d0 | 2130 | ofd = emacs_open (SDATA (encoded_newname), |
7fce7dfe | 2131 | O_WRONLY | O_TRUNC | O_CREAT |
795c20df | 2132 | | (NILP (ok_if_already_exists) ? O_EXCL : 0), |
c50f15d0 RS |
2133 | S_IREAD | S_IWRITE); |
2134 | #else /* not MSDOS */ | |
15e3a074 | 2135 | { |
d20704ef PE |
2136 | mode_t new_mask = !NILP (preserve_uid_gid) ? 0600 : 0666; |
2137 | new_mask &= st.st_mode; | |
15e3a074 PE |
2138 | ofd = emacs_open (SSDATA (encoded_newname), |
2139 | (O_WRONLY | O_TRUNC | O_CREAT | |
2140 | | (NILP (ok_if_already_exists) ? O_EXCL : 0)), | |
2141 | new_mask); | |
2142 | } | |
4c3c22f3 | 2143 | #endif /* not MSDOS */ |
570d7624 | 2144 | if (ofd < 0) |
3ed15d97 | 2145 | report_file_error ("Opening output file", Fcons (newname, Qnil)); |
b5148e85 RS |
2146 | |
2147 | record_unwind_protect (close_file_unwind, make_number (ofd)); | |
570d7624 | 2148 | |
b5148e85 RS |
2149 | immediate_quit = 1; |
2150 | QUIT; | |
68c45bf0 PE |
2151 | while ((n = emacs_read (ifd, buf, sizeof buf)) > 0) |
2152 | if (emacs_write (ofd, buf, n) != n) | |
3ed15d97 | 2153 | report_file_error ("I/O error", Fcons (newname, Qnil)); |
b5148e85 | 2154 | immediate_quit = 0; |
570d7624 | 2155 | |
b016179b EZ |
2156 | #ifndef MSDOS |
2157 | /* Preserve the original file modes, and if requested, also its | |
2158 | owner and group. */ | |
d20704ef PE |
2159 | { |
2160 | mode_t mode_mask = 07777; | |
2161 | if (!NILP (preserve_uid_gid)) | |
2162 | { | |
2163 | /* Attempt to change owner and group. If that doesn't work | |
2164 | attempt to change just the group, as that is sometimes allowed. | |
2165 | Adjust the mode mask to eliminate setuid or setgid bits | |
2166 | that are inappropriate if the owner and group are wrong. */ | |
2167 | if (fchown (ofd, st.st_uid, st.st_gid) != 0) | |
2168 | { | |
2169 | mode_mask &= ~06000; | |
2170 | if (fchown (ofd, -1, st.st_gid) == 0) | |
2171 | mode_mask |= 02000; | |
2172 | } | |
2173 | } | |
2174 | if (fchmod (ofd, st.st_mode & mode_mask) != 0) | |
2175 | report_file_error ("Doing chmod", Fcons (newname, Qnil)); | |
2176 | } | |
b016179b | 2177 | #endif /* not MSDOS */ |
586702ce | 2178 | |
574c05e2 KK |
2179 | #if HAVE_LIBSELINUX |
2180 | if (conlength > 0) | |
2181 | { | |
24c51a09 | 2182 | /* Set the modified context back to the file. */ |
f75d7a91 | 2183 | bool fail = fsetfilecon (ofd, con) != 0; |
24c51a09 GM |
2184 | /* See http://debbugs.gnu.org/11245 for ENOTSUP. */ |
2185 | if (fail && errno != ENOTSUP) | |
574c05e2 KK |
2186 | report_file_error ("Doing fsetfilecon", Fcons (newname, Qnil)); |
2187 | ||
2188 | freecon (con); | |
2189 | } | |
2190 | #endif | |
2191 | ||
7c3d167f RF |
2192 | #ifdef HAVE_POSIX_ACL |
2193 | if (acl != NULL) | |
2194 | { | |
2195 | bool fail = acl_set_fd (ofd, acl) != 0; | |
2196 | if (fail && errno != ENOTSUP) | |
2197 | report_file_error ("Setting ACL", Fcons (newname, Qnil)); | |
2198 | ||
2199 | acl_free (acl); | |
2200 | } | |
2201 | #endif | |
2202 | ||
d20704ef | 2203 | if (!NILP (keep_time)) |
570d7624 | 2204 | { |
d20704ef PE |
2205 | EMACS_TIME atime = get_stat_atime (&st); |
2206 | EMACS_TIME mtime = get_stat_mtime (&st); | |
2207 | if (set_file_times (ofd, SSDATA (encoded_newname), atime, mtime)) | |
2208 | xsignal2 (Qfile_date_error, | |
2209 | build_string ("Cannot set file date"), newname); | |
570d7624 JB |
2210 | } |
2211 | ||
d35af63c PE |
2212 | if (emacs_close (ofd) < 0) |
2213 | report_file_error ("I/O error", Fcons (newname, Qnil)); | |
2214 | ||
68c45bf0 | 2215 | emacs_close (ifd); |
b016179b | 2216 | |
ed68db4d | 2217 | #ifdef MSDOS |
d20704ef PE |
2218 | /* In DJGPP v2.0 and later, fstat usually returns true file mode bits, |
2219 | and if it can't, it tells so. Otherwise, under MSDOS we usually | |
2220 | get only the READ bit, which will make the copied file read-only, | |
2221 | so it's better not to chmod at all. */ | |
2222 | if ((_djstat_flags & _STFAIL_WRITEBIT) == 0) | |
2223 | chmod (SDATA (encoded_newname), st.st_mode & 07777); | |
ed68db4d | 2224 | #endif /* MSDOS */ |
b016179b | 2225 | #endif /* not WINDOWSNT */ |
5acac34e | 2226 | |
b5148e85 RS |
2227 | /* Discard the unwind protects. */ |
2228 | specpdl_ptr = specpdl + count; | |
2229 | ||
570d7624 JB |
2230 | UNGCPRO; |
2231 | return Qnil; | |
2232 | } | |
385b6cc7 | 2233 | \f |
9bbe01fb | 2234 | DEFUN ("make-directory-internal", Fmake_directory_internal, |
353cfc19 | 2235 | Smake_directory_internal, 1, 1, 0, |
8c1a1077 | 2236 | doc: /* Create a new directory named DIRECTORY. */) |
5842a27b | 2237 | (Lisp_Object directory) |
570d7624 | 2238 | { |
5976c3fe | 2239 | const char *dir; |
32f4334d | 2240 | Lisp_Object handler; |
b1d1b865 | 2241 | Lisp_Object encoded_dir; |
570d7624 | 2242 | |
b7826503 | 2243 | CHECK_STRING (directory); |
3b7f6e60 | 2244 | directory = Fexpand_file_name (directory, Qnil); |
32f4334d | 2245 | |
3b7f6e60 | 2246 | handler = Ffind_file_name_handler (directory, Qmake_directory_internal); |
32f4334d | 2247 | if (!NILP (handler)) |
3b7f6e60 | 2248 | return call2 (handler, Qmake_directory_internal, directory); |
9bbe01fb | 2249 | |
b1d1b865 RS |
2250 | encoded_dir = ENCODE_FILE (directory); |
2251 | ||
5976c3fe | 2252 | dir = SSDATA (encoded_dir); |
570d7624 | 2253 | |
5e570b75 RS |
2254 | #ifdef WINDOWSNT |
2255 | if (mkdir (dir) != 0) | |
2256 | #else | |
09450bae | 2257 | if (mkdir (dir, 0777 & ~auto_saving_dir_umask) != 0) |
5e570b75 | 2258 | #endif |
a9f2aeae | 2259 | report_file_error ("Creating directory", list1 (directory)); |
570d7624 | 2260 | |
32f4334d | 2261 | return Qnil; |
570d7624 JB |
2262 | } |
2263 | ||
9d8f3bd9 MA |
2264 | DEFUN ("delete-directory-internal", Fdelete_directory_internal, |
2265 | Sdelete_directory_internal, 1, 1, 0, | |
efdc16c9 | 2266 | doc: /* Delete the directory named DIRECTORY. Does not follow symlinks. */) |
5842a27b | 2267 | (Lisp_Object directory) |
570d7624 | 2268 | { |
5976c3fe | 2269 | const char *dir; |
b1d1b865 | 2270 | Lisp_Object encoded_dir; |
570d7624 | 2271 | |
b7826503 | 2272 | CHECK_STRING (directory); |
3b7f6e60 | 2273 | directory = Fdirectory_file_name (Fexpand_file_name (directory, Qnil)); |
b1d1b865 | 2274 | encoded_dir = ENCODE_FILE (directory); |
5976c3fe | 2275 | dir = SSDATA (encoded_dir); |
b1d1b865 | 2276 | |
570d7624 | 2277 | if (rmdir (dir) != 0) |
a9f2aeae | 2278 | report_file_error ("Removing directory", list1 (directory)); |
570d7624 JB |
2279 | |
2280 | return Qnil; | |
2281 | } | |
2282 | ||
2e2bbddb | 2283 | DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 2, |
f1a5d776 CY |
2284 | "(list (read-file-name \ |
2285 | (if (and delete-by-moving-to-trash (null current-prefix-arg)) \ | |
2286 | \"Move file to trash: \" \"Delete file: \") \ | |
2287 | nil default-directory (confirm-nonexistent-file-or-buffer)) \ | |
2288 | (null current-prefix-arg))", | |
efdc16c9 | 2289 | doc: /* Delete file named FILENAME. If it is a symlink, remove the symlink. |
53967e09 | 2290 | If file has multiple names, it continues to exist with the other names. |
f1a5d776 CY |
2291 | TRASH non-nil means to trash the file instead of deleting, provided |
2292 | `delete-by-moving-to-trash' is non-nil. | |
53967e09 | 2293 | |
f1a5d776 CY |
2294 | When called interactively, TRASH is t if no prefix argument is given. |
2295 | With a prefix argument, TRASH is nil. */) | |
5842a27b | 2296 | (Lisp_Object filename, Lisp_Object trash) |
570d7624 | 2297 | { |
32f4334d | 2298 | Lisp_Object handler; |
b1d1b865 | 2299 | Lisp_Object encoded_file; |
efdc16c9 | 2300 | struct gcpro gcpro1; |
b1d1b865 | 2301 | |
efdc16c9 | 2302 | GCPRO1 (filename); |
b4bd27c5 RS |
2303 | if (!NILP (Ffile_directory_p (filename)) |
2304 | && NILP (Ffile_symlink_p (filename))) | |
24b1ddad KS |
2305 | xsignal2 (Qfile_error, |
2306 | build_string ("Removing old name: is a directory"), | |
2307 | filename); | |
efdc16c9 | 2308 | UNGCPRO; |
570d7624 | 2309 | filename = Fexpand_file_name (filename, Qnil); |
32f4334d | 2310 | |
49307295 | 2311 | handler = Ffind_file_name_handler (filename, Qdelete_file); |
32f4334d | 2312 | if (!NILP (handler)) |
f5783df3 | 2313 | return call3 (handler, Qdelete_file, filename, trash); |
32f4334d | 2314 | |
f1a5d776 | 2315 | if (delete_by_moving_to_trash && !NILP (trash)) |
6cf29fe8 JR |
2316 | return call1 (Qmove_file_to_trash, filename); |
2317 | ||
b1d1b865 RS |
2318 | encoded_file = ENCODE_FILE (filename); |
2319 | ||
ce2fe65a | 2320 | if (unlink (SSDATA (encoded_file)) < 0) |
a9f2aeae | 2321 | report_file_error ("Removing old name", list1 (filename)); |
8a9b0da9 | 2322 | return Qnil; |
570d7624 JB |
2323 | } |
2324 | ||
385b6cc7 | 2325 | static Lisp_Object |
971de7fb | 2326 | internal_delete_file_1 (Lisp_Object ignore) |
385b6cc7 RS |
2327 | { |
2328 | return Qt; | |
2329 | } | |
2330 | ||
7d80ea23 | 2331 | /* Delete file FILENAME, returning true if successful. |
f1a5d776 | 2332 | This ignores `delete-by-moving-to-trash'. */ |
385b6cc7 | 2333 | |
7d80ea23 | 2334 | bool |
f1a5d776 | 2335 | internal_delete_file (Lisp_Object filename) |
385b6cc7 | 2336 | { |
b0728617 EZ |
2337 | Lisp_Object tem; |
2338 | ||
2339 | tem = internal_condition_case_2 (Fdelete_file, filename, Qnil, | |
2340 | Qt, internal_delete_file_1); | |
2341 | return NILP (tem); | |
385b6cc7 RS |
2342 | } |
2343 | \f | |
570d7624 | 2344 | DEFUN ("rename-file", Frename_file, Srename_file, 2, 3, |
7c0f6118 | 2345 | "fRename file: \nGRename %s to file: \np", |
a4e03fe5 | 2346 | doc: /* Rename FILE as NEWNAME. Both args must be strings. |
8c1a1077 PJ |
2347 | If file has names other than FILE, it continues to have those names. |
2348 | Signals a `file-already-exists' error if a file NEWNAME already exists | |
2349 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2350 | A number as third arg means request confirmation if NEWNAME already exists. | |
2351 | This is what happens in interactive use with M-x. */) | |
5842a27b | 2352 | (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists) |
570d7624 | 2353 | { |
32f4334d | 2354 | Lisp_Object handler; |
f72b5416 JD |
2355 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
2356 | Lisp_Object encoded_file, encoded_newname, symlink_target; | |
570d7624 | 2357 | |
f72b5416 JD |
2358 | symlink_target = encoded_file = encoded_newname = Qnil; |
2359 | GCPRO5 (file, newname, encoded_file, encoded_newname, symlink_target); | |
b7826503 PJ |
2360 | CHECK_STRING (file); |
2361 | CHECK_STRING (newname); | |
3b7f6e60 | 2362 | file = Fexpand_file_name (file, Qnil); |
1ffc5c90 | 2363 | |
f83caf70 EZ |
2364 | if ((!NILP (Ffile_directory_p (newname))) |
2365 | #ifdef DOS_NT | |
2366 | /* If the file names are identical but for the case, | |
2367 | don't attempt to move directory to itself. */ | |
2368 | && (NILP (Fstring_equal (Fdowncase (file), Fdowncase (newname)))) | |
2369 | #endif | |
2370 | ) | |
8719abec | 2371 | { |
ce2fe65a AS |
2372 | Lisp_Object fname = (NILP (Ffile_directory_p (file)) |
2373 | ? file : Fdirectory_file_name (file)); | |
8719abec CY |
2374 | newname = Fexpand_file_name (Ffile_name_nondirectory (fname), newname); |
2375 | } | |
1ffc5c90 RS |
2376 | else |
2377 | newname = Fexpand_file_name (newname, Qnil); | |
32f4334d RS |
2378 | |
2379 | /* If the file name has special constructs in it, | |
2380 | call the corresponding file handler. */ | |
3b7f6e60 | 2381 | handler = Ffind_file_name_handler (file, Qrename_file); |
51cf6d37 | 2382 | if (NILP (handler)) |
49307295 | 2383 | handler = Ffind_file_name_handler (newname, Qrename_file); |
32f4334d | 2384 | if (!NILP (handler)) |
36712b0a | 2385 | RETURN_UNGCPRO (call4 (handler, Qrename_file, |
3b7f6e60 | 2386 | file, newname, ok_if_already_exists)); |
32f4334d | 2387 | |
b1d1b865 RS |
2388 | encoded_file = ENCODE_FILE (file); |
2389 | encoded_newname = ENCODE_FILE (newname); | |
2390 | ||
bc77278f EZ |
2391 | #ifdef DOS_NT |
2392 | /* If the file names are identical but for the case, don't ask for | |
2393 | confirmation: they simply want to change the letter-case of the | |
2394 | file name. */ | |
2395 | if (NILP (Fstring_equal (Fdowncase (file), Fdowncase (newname)))) | |
2396 | #endif | |
265a9e55 | 2397 | if (NILP (ok_if_already_exists) |
93c30b5f | 2398 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2399 | barf_or_query_if_file_exists (newname, "rename to it", |
b8b29dc9 | 2400 | INTEGERP (ok_if_already_exists), 0, 0); |
ce2fe65a | 2401 | if (rename (SSDATA (encoded_file), SSDATA (encoded_newname)) < 0) |
570d7624 JB |
2402 | { |
2403 | if (errno == EXDEV) | |
2404 | { | |
d311d28c | 2405 | ptrdiff_t count; |
f72b5416 | 2406 | symlink_target = Ffile_symlink_p (file); |
440c7d00 JD |
2407 | if (! NILP (symlink_target)) |
2408 | Fmake_symbolic_link (symlink_target, newname, | |
f59abab9 | 2409 | NILP (ok_if_already_exists) ? Qnil : Qt); |
ae0d7250 | 2410 | else if (!NILP (Ffile_directory_p (file))) |
8719abec CY |
2411 | call4 (Qcopy_directory, file, newname, Qt, Qnil); |
2412 | else | |
2413 | /* We have already prompted if it was an integer, so don't | |
2414 | have copy-file prompt again. */ | |
586702ce | 2415 | Fcopy_file (file, newname, |
586702ce | 2416 | NILP (ok_if_already_exists) ? Qnil : Qt, |
574c05e2 | 2417 | Qt, Qt, Qt); |
d550c425 | 2418 | |
6bddfc97 | 2419 | count = SPECPDL_INDEX (); |
d2b66acf | 2420 | specbind (Qdelete_by_moving_to_trash, Qnil); |
8fab2362 | 2421 | |
ae0d7250 | 2422 | if (!NILP (Ffile_directory_p (file)) && NILP (symlink_target)) |
8719abec CY |
2423 | call2 (Qdelete_directory, file, Qt); |
2424 | else | |
f1a5d776 | 2425 | Fdelete_file (file, Qnil); |
6bddfc97 | 2426 | unbind_to (count, Qnil); |
570d7624 JB |
2427 | } |
2428 | else | |
a9f2aeae | 2429 | report_file_error ("Renaming", list2 (file, newname)); |
570d7624 JB |
2430 | } |
2431 | UNGCPRO; | |
2432 | return Qnil; | |
2433 | } | |
2434 | ||
2435 | DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3, | |
7c0f6118 | 2436 | "fAdd name to file: \nGName to add to %s: \np", |
a4e03fe5 | 2437 | doc: /* Give FILE additional name NEWNAME. Both args must be strings. |
8c1a1077 PJ |
2438 | Signals a `file-already-exists' error if a file NEWNAME already exists |
2439 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2440 | A number as third arg means request confirmation if NEWNAME already exists. | |
2441 | This is what happens in interactive use with M-x. */) | |
5842a27b | 2442 | (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists) |
570d7624 | 2443 | { |
32f4334d | 2444 | Lisp_Object handler; |
b1d1b865 RS |
2445 | Lisp_Object encoded_file, encoded_newname; |
2446 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2447 | |
b1d1b865 RS |
2448 | GCPRO4 (file, newname, encoded_file, encoded_newname); |
2449 | encoded_file = encoded_newname = Qnil; | |
b7826503 PJ |
2450 | CHECK_STRING (file); |
2451 | CHECK_STRING (newname); | |
3b7f6e60 | 2452 | file = Fexpand_file_name (file, Qnil); |
1ffc5c90 RS |
2453 | |
2454 | if (!NILP (Ffile_directory_p (newname))) | |
2455 | newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname); | |
2456 | else | |
2457 | newname = Fexpand_file_name (newname, Qnil); | |
32f4334d RS |
2458 | |
2459 | /* If the file name has special constructs in it, | |
2460 | call the corresponding file handler. */ | |
3b7f6e60 | 2461 | handler = Ffind_file_name_handler (file, Qadd_name_to_file); |
32f4334d | 2462 | if (!NILP (handler)) |
3b7f6e60 | 2463 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
36712b0a | 2464 | newname, ok_if_already_exists)); |
32f4334d | 2465 | |
adc6741c RS |
2466 | /* If the new name has special constructs in it, |
2467 | call the corresponding file handler. */ | |
2468 | handler = Ffind_file_name_handler (newname, Qadd_name_to_file); | |
2469 | if (!NILP (handler)) | |
3b7f6e60 | 2470 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
adc6741c RS |
2471 | newname, ok_if_already_exists)); |
2472 | ||
b1d1b865 RS |
2473 | encoded_file = ENCODE_FILE (file); |
2474 | encoded_newname = ENCODE_FILE (newname); | |
2475 | ||
265a9e55 | 2476 | if (NILP (ok_if_already_exists) |
93c30b5f | 2477 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2478 | barf_or_query_if_file_exists (newname, "make it a new name", |
b8b29dc9 | 2479 | INTEGERP (ok_if_already_exists), 0, 0); |
5e570b75 | 2480 | |
42a5b22f | 2481 | unlink (SSDATA (newname)); |
ce2fe65a | 2482 | if (link (SSDATA (encoded_file), SSDATA (encoded_newname)) < 0) |
a9f2aeae | 2483 | report_file_error ("Adding new name", list2 (file, newname)); |
570d7624 JB |
2484 | |
2485 | UNGCPRO; | |
2486 | return Qnil; | |
2487 | } | |
2488 | ||
570d7624 | 2489 | DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3, |
7c0f6118 | 2490 | "FMake symbolic link to file: \nGMake symbolic link to file %s: \np", |
68780e2a RS |
2491 | doc: /* Make a symbolic link to FILENAME, named LINKNAME. |
2492 | Both args must be strings. | |
8c1a1077 PJ |
2493 | Signals a `file-already-exists' error if a file LINKNAME already exists |
2494 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2495 | A number as third arg means request confirmation if LINKNAME already exists. | |
2496 | This happens for interactive use with M-x. */) | |
5842a27b | 2497 | (Lisp_Object filename, Lisp_Object linkname, Lisp_Object ok_if_already_exists) |
570d7624 | 2498 | { |
32f4334d | 2499 | Lisp_Object handler; |
b1d1b865 RS |
2500 | Lisp_Object encoded_filename, encoded_linkname; |
2501 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2502 | |
b1d1b865 RS |
2503 | GCPRO4 (filename, linkname, encoded_filename, encoded_linkname); |
2504 | encoded_filename = encoded_linkname = Qnil; | |
b7826503 PJ |
2505 | CHECK_STRING (filename); |
2506 | CHECK_STRING (linkname); | |
d9bc1c99 RS |
2507 | /* If the link target has a ~, we must expand it to get |
2508 | a truly valid file name. Otherwise, do not expand; | |
2509 | we want to permit links to relative file names. */ | |
d5db4077 | 2510 | if (SREF (filename, 0) == '~') |
d9bc1c99 | 2511 | filename = Fexpand_file_name (filename, Qnil); |
1ffc5c90 RS |
2512 | |
2513 | if (!NILP (Ffile_directory_p (linkname))) | |
dac24db4 | 2514 | linkname = Fexpand_file_name (Ffile_name_nondirectory (filename), linkname); |
1ffc5c90 RS |
2515 | else |
2516 | linkname = Fexpand_file_name (linkname, Qnil); | |
32f4334d RS |
2517 | |
2518 | /* If the file name has special constructs in it, | |
2519 | call the corresponding file handler. */ | |
49307295 | 2520 | handler = Ffind_file_name_handler (filename, Qmake_symbolic_link); |
32f4334d | 2521 | if (!NILP (handler)) |
36712b0a KH |
2522 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, |
2523 | linkname, ok_if_already_exists)); | |
32f4334d | 2524 | |
adc6741c RS |
2525 | /* If the new link name has special constructs in it, |
2526 | call the corresponding file handler. */ | |
2527 | handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link); | |
2528 | if (!NILP (handler)) | |
2529 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, | |
2530 | linkname, ok_if_already_exists)); | |
2531 | ||
b1d1b865 RS |
2532 | encoded_filename = ENCODE_FILE (filename); |
2533 | encoded_linkname = ENCODE_FILE (linkname); | |
2534 | ||
265a9e55 | 2535 | if (NILP (ok_if_already_exists) |
93c30b5f | 2536 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2537 | barf_or_query_if_file_exists (linkname, "make it a link", |
b8b29dc9 | 2538 | INTEGERP (ok_if_already_exists), 0, 0); |
ce2fe65a | 2539 | if (symlink (SSDATA (encoded_filename), SSDATA (encoded_linkname)) < 0) |
570d7624 JB |
2540 | { |
2541 | /* If we didn't complain already, silently delete existing file. */ | |
2542 | if (errno == EEXIST) | |
2543 | { | |
42a5b22f | 2544 | unlink (SSDATA (encoded_linkname)); |
ce2fe65a AS |
2545 | if (symlink (SSDATA (encoded_filename), SSDATA (encoded_linkname)) |
2546 | >= 0) | |
1a04498e KH |
2547 | { |
2548 | UNGCPRO; | |
2549 | return Qnil; | |
2550 | } | |
570d7624 | 2551 | } |
d9d0d182 PE |
2552 | if (errno == ENOSYS) |
2553 | { | |
2554 | UNGCPRO; | |
2555 | xsignal1 (Qfile_error, | |
2556 | build_string ("Symbolic links are not supported")); | |
2557 | } | |
570d7624 | 2558 | |
a9f2aeae | 2559 | report_file_error ("Making symbolic link", list2 (filename, linkname)); |
570d7624 JB |
2560 | } |
2561 | UNGCPRO; | |
2562 | return Qnil; | |
e89b536d | 2563 | } |
570d7624 | 2564 | |
570d7624 | 2565 | \f |
a7ca3326 | 2566 | DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p, |
570d7624 | 2567 | 1, 1, 0, |
8c1a1077 PJ |
2568 | doc: /* Return t if file FILENAME specifies an absolute file name. |
2569 | On Unix, this is a name starting with a `/' or a `~'. */) | |
5842a27b | 2570 | (Lisp_Object filename) |
570d7624 | 2571 | { |
b7826503 | 2572 | CHECK_STRING (filename); |
5976c3fe | 2573 | return file_name_absolute_p (SSDATA (filename)) ? Qt : Qnil; |
570d7624 | 2574 | } |
3beeedfe | 2575 | \f |
bb385a92 EZ |
2576 | /* Return true if FILENAME exists. */ |
2577 | bool | |
2578 | check_existing (const char *filename) | |
2579 | { | |
73dcdb9f | 2580 | return faccessat (AT_FDCWD, filename, F_OK, AT_EACCESS) == 0; |
bb385a92 EZ |
2581 | } |
2582 | ||
f75d7a91 | 2583 | /* Return true if file FILENAME exists and can be executed. */ |
3beeedfe | 2584 | |
f75d7a91 | 2585 | static bool |
971de7fb | 2586 | check_executable (char *filename) |
3beeedfe | 2587 | { |
73dcdb9f | 2588 | return faccessat (AT_FDCWD, filename, X_OK, AT_EACCESS) == 0; |
3beeedfe RS |
2589 | } |
2590 | ||
73dcdb9f PE |
2591 | /* Return true if file FILENAME exists and can be accessed |
2592 | according to AMODE, which should include W_OK. | |
2593 | On failure, return false and set errno. */ | |
3beeedfe | 2594 | |
f75d7a91 | 2595 | static bool |
73dcdb9f | 2596 | check_writable (const char *filename, int amode) |
3beeedfe | 2597 | { |
3be3c08e | 2598 | #ifdef MSDOS |
73dcdb9f PE |
2599 | /* FIXME: an faccessat implementation should be added to the |
2600 | DOS/Windows ports and this #ifdef branch should be removed. */ | |
3be3c08e RS |
2601 | struct stat st; |
2602 | if (stat (filename, &st) < 0) | |
2603 | return 0; | |
73dcdb9f | 2604 | errno = EPERM; |
f68c809d | 2605 | return (st.st_mode & S_IWRITE || S_ISDIR (st.st_mode)); |
3be3c08e | 2606 | #else /* not MSDOS */ |
73dcdb9f | 2607 | bool res = faccessat (AT_FDCWD, filename, amode, AT_EACCESS) == 0; |
e1b01a3a | 2608 | #ifdef CYGWIN |
73dcdb9f | 2609 | /* faccessat may have returned failure because Cygwin couldn't |
e1b01a3a KB |
2610 | determine the file's UID or GID; if so, we return success. */ |
2611 | if (!res) | |
2612 | { | |
73dcdb9f | 2613 | int faccessat_errno = errno; |
e1b01a3a KB |
2614 | struct stat st; |
2615 | if (stat (filename, &st) < 0) | |
2616 | return 0; | |
2617 | res = (st.st_uid == -1 || st.st_gid == -1); | |
73dcdb9f | 2618 | errno = faccessat_errno; |
e1b01a3a KB |
2619 | } |
2620 | #endif /* CYGWIN */ | |
2621 | return res; | |
3be3c08e | 2622 | #endif /* not MSDOS */ |
3beeedfe | 2623 | } |
570d7624 | 2624 | |
a7ca3326 | 2625 | DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0, |
68780e2a RS |
2626 | doc: /* Return t if file FILENAME exists (whether or not you can read it.) |
2627 | See also `file-readable-p' and `file-attributes'. | |
2628 | This returns nil for a symlink to a nonexistent file. | |
2629 | Use `file-symlink-p' to test for such links. */) | |
5842a27b | 2630 | (Lisp_Object filename) |
570d7624 | 2631 | { |
199607e4 | 2632 | Lisp_Object absname; |
32f4334d | 2633 | Lisp_Object handler; |
570d7624 | 2634 | |
b7826503 | 2635 | CHECK_STRING (filename); |
199607e4 | 2636 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2637 | |
2638 | /* If the file name has special constructs in it, | |
2639 | call the corresponding file handler. */ | |
199607e4 | 2640 | handler = Ffind_file_name_handler (absname, Qfile_exists_p); |
32f4334d | 2641 | if (!NILP (handler)) |
199607e4 | 2642 | return call2 (handler, Qfile_exists_p, absname); |
32f4334d | 2643 | |
b1d1b865 RS |
2644 | absname = ENCODE_FILE (absname); |
2645 | ||
bb385a92 | 2646 | return (check_existing (SSDATA (absname))) ? Qt : Qnil; |
570d7624 JB |
2647 | } |
2648 | ||
2649 | DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0, | |
8c1a1077 PJ |
2650 | doc: /* Return t if FILENAME can be executed by you. |
2651 | For a directory, this means you can access files in that directory. */) | |
5842a27b | 2652 | (Lisp_Object filename) |
570d7624 | 2653 | { |
199607e4 | 2654 | Lisp_Object absname; |
32f4334d | 2655 | Lisp_Object handler; |
570d7624 | 2656 | |
b7826503 | 2657 | CHECK_STRING (filename); |
199607e4 | 2658 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2659 | |
2660 | /* If the file name has special constructs in it, | |
2661 | call the corresponding file handler. */ | |
199607e4 | 2662 | handler = Ffind_file_name_handler (absname, Qfile_executable_p); |
32f4334d | 2663 | if (!NILP (handler)) |
199607e4 | 2664 | return call2 (handler, Qfile_executable_p, absname); |
32f4334d | 2665 | |
b1d1b865 RS |
2666 | absname = ENCODE_FILE (absname); |
2667 | ||
42a5b22f | 2668 | return (check_executable (SSDATA (absname)) ? Qt : Qnil); |
570d7624 JB |
2669 | } |
2670 | ||
a7ca3326 | 2671 | DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0, |
8c1a1077 PJ |
2672 | doc: /* Return t if file FILENAME exists and you can read it. |
2673 | See also `file-exists-p' and `file-attributes'. */) | |
5842a27b | 2674 | (Lisp_Object filename) |
570d7624 | 2675 | { |
199607e4 | 2676 | Lisp_Object absname; |
32f4334d | 2677 | Lisp_Object handler; |
570d7624 | 2678 | |
b7826503 | 2679 | CHECK_STRING (filename); |
199607e4 | 2680 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2681 | |
2682 | /* If the file name has special constructs in it, | |
2683 | call the corresponding file handler. */ | |
199607e4 | 2684 | handler = Ffind_file_name_handler (absname, Qfile_readable_p); |
32f4334d | 2685 | if (!NILP (handler)) |
199607e4 | 2686 | return call2 (handler, Qfile_readable_p, absname); |
32f4334d | 2687 | |
b1d1b865 | 2688 | absname = ENCODE_FILE (absname); |
73dcdb9f PE |
2689 | return (faccessat (AT_FDCWD, SSDATA (absname), R_OK, AT_EACCESS) == 0 |
2690 | ? Qt : Qnil); | |
570d7624 JB |
2691 | } |
2692 | ||
f793dc6c | 2693 | DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0, |
8c1a1077 | 2694 | doc: /* Return t if file FILENAME can be written or created by you. */) |
5842a27b | 2695 | (Lisp_Object filename) |
f793dc6c | 2696 | { |
b1d1b865 | 2697 | Lisp_Object absname, dir, encoded; |
f793dc6c | 2698 | Lisp_Object handler; |
f793dc6c | 2699 | |
b7826503 | 2700 | CHECK_STRING (filename); |
199607e4 | 2701 | absname = Fexpand_file_name (filename, Qnil); |
f793dc6c RS |
2702 | |
2703 | /* If the file name has special constructs in it, | |
2704 | call the corresponding file handler. */ | |
199607e4 | 2705 | handler = Ffind_file_name_handler (absname, Qfile_writable_p); |
f793dc6c | 2706 | if (!NILP (handler)) |
199607e4 | 2707 | return call2 (handler, Qfile_writable_p, absname); |
f793dc6c | 2708 | |
b1d1b865 | 2709 | encoded = ENCODE_FILE (absname); |
73dcdb9f PE |
2710 | if (check_writable (SSDATA (encoded), W_OK)) |
2711 | return Qt; | |
2712 | if (errno != ENOENT) | |
2713 | return Qnil; | |
b1d1b865 | 2714 | |
199607e4 | 2715 | dir = Ffile_name_directory (absname); |
73dcdb9f | 2716 | eassert (!NILP (dir)); |
f793dc6c | 2717 | #ifdef MSDOS |
73dcdb9f | 2718 | dir = Fdirectory_file_name (dir); |
f793dc6c | 2719 | #endif /* MSDOS */ |
b1d1b865 RS |
2720 | |
2721 | dir = ENCODE_FILE (dir); | |
e3e8a75a GM |
2722 | #ifdef WINDOWSNT |
2723 | /* The read-only attribute of the parent directory doesn't affect | |
2724 | whether a file or directory can be created within it. Some day we | |
2725 | should check ACLs though, which do affect this. */ | |
73dcdb9f | 2726 | return file_directory_p (SDATA (dir)) ? Qt : Qnil; |
e3e8a75a | 2727 | #else |
73dcdb9f | 2728 | return check_writable (SSDATA (dir), W_OK | X_OK) ? Qt : Qnil; |
e3e8a75a | 2729 | #endif |
f793dc6c RS |
2730 | } |
2731 | \f | |
1f8653eb | 2732 | DEFUN ("access-file", Faccess_file, Saccess_file, 2, 2, 0, |
8c1a1077 PJ |
2733 | doc: /* Access file FILENAME, and get an error if that does not work. |
2734 | The second argument STRING is used in the error message. | |
a4e03fe5 | 2735 | If there is no error, returns nil. */) |
5842a27b | 2736 | (Lisp_Object filename, Lisp_Object string) |
1f8653eb | 2737 | { |
49475635 | 2738 | Lisp_Object handler, encoded_filename, absname; |
1f8653eb RS |
2739 | int fd; |
2740 | ||
b7826503 | 2741 | CHECK_STRING (filename); |
49475635 EZ |
2742 | absname = Fexpand_file_name (filename, Qnil); |
2743 | ||
b7826503 | 2744 | CHECK_STRING (string); |
1f8653eb RS |
2745 | |
2746 | /* If the file name has special constructs in it, | |
2747 | call the corresponding file handler. */ | |
49475635 | 2748 | handler = Ffind_file_name_handler (absname, Qaccess_file); |
1f8653eb | 2749 | if (!NILP (handler)) |
49475635 | 2750 | return call3 (handler, Qaccess_file, absname, string); |
1f8653eb | 2751 | |
49475635 | 2752 | encoded_filename = ENCODE_FILE (absname); |
b1d1b865 | 2753 | |
42a5b22f | 2754 | fd = emacs_open (SSDATA (encoded_filename), O_RDONLY, 0); |
1f8653eb | 2755 | if (fd < 0) |
42a5b22f | 2756 | report_file_error (SSDATA (string), Fcons (filename, Qnil)); |
68c45bf0 | 2757 | emacs_close (fd); |
1f8653eb RS |
2758 | |
2759 | return Qnil; | |
2760 | } | |
2761 | \f | |
a7ca3326 | 2762 | DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0, |
8c1a1077 | 2763 | doc: /* Return non-nil if file FILENAME is the name of a symbolic link. |
1c353c74 | 2764 | The value is the link target, as a string. |
68780e2a RS |
2765 | Otherwise it returns nil. |
2766 | ||
2767 | This function returns t when given the name of a symlink that | |
2768 | points to a nonexistent file. */) | |
5842a27b | 2769 | (Lisp_Object filename) |
570d7624 | 2770 | { |
32f4334d | 2771 | Lisp_Object handler; |
ae0d7250 | 2772 | char *buf; |
ae0d7250 | 2773 | Lisp_Object val; |
d1fdcab7 | 2774 | char readlink_buf[READLINK_BUFSIZE]; |
570d7624 | 2775 | |
b7826503 | 2776 | CHECK_STRING (filename); |
570d7624 JB |
2777 | filename = Fexpand_file_name (filename, Qnil); |
2778 | ||
32f4334d RS |
2779 | /* If the file name has special constructs in it, |
2780 | call the corresponding file handler. */ | |
49307295 | 2781 | handler = Ffind_file_name_handler (filename, Qfile_symlink_p); |
32f4334d RS |
2782 | if (!NILP (handler)) |
2783 | return call2 (handler, Qfile_symlink_p, filename); | |
2784 | ||
b1d1b865 RS |
2785 | filename = ENCODE_FILE (filename); |
2786 | ||
d1fdcab7 PE |
2787 | buf = emacs_readlink (SSDATA (filename), readlink_buf); |
2788 | if (! buf) | |
2789 | return Qnil; | |
efdc16c9 | 2790 | |
d1fdcab7 | 2791 | val = build_string (buf); |
8966b757 | 2792 | if (buf[0] == '/' && strchr (buf, ':')) |
69ac1891 | 2793 | val = concat2 (build_string ("/:"), val); |
d1fdcab7 PE |
2794 | if (buf != readlink_buf) |
2795 | xfree (buf); | |
cd913586 KH |
2796 | val = DECODE_FILE (val); |
2797 | return val; | |
570d7624 JB |
2798 | } |
2799 | ||
a7ca3326 | 2800 | DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0, |
8c1a1077 PJ |
2801 | doc: /* Return t if FILENAME names an existing directory. |
2802 | Symbolic links to directories count as directories. | |
2803 | See `file-symlink-p' to distinguish symlinks. */) | |
5842a27b | 2804 | (Lisp_Object filename) |
570d7624 | 2805 | { |
73dcdb9f | 2806 | Lisp_Object absname; |
32f4334d | 2807 | Lisp_Object handler; |
570d7624 | 2808 | |
4b4deea2 | 2809 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
570d7624 | 2810 | |
32f4334d RS |
2811 | /* If the file name has special constructs in it, |
2812 | call the corresponding file handler. */ | |
199607e4 | 2813 | handler = Ffind_file_name_handler (absname, Qfile_directory_p); |
32f4334d | 2814 | if (!NILP (handler)) |
199607e4 | 2815 | return call2 (handler, Qfile_directory_p, absname); |
32f4334d | 2816 | |
b1d1b865 RS |
2817 | absname = ENCODE_FILE (absname); |
2818 | ||
73dcdb9f PE |
2819 | return file_directory_p (SSDATA (absname)) ? Qt : Qnil; |
2820 | } | |
2821 | ||
2822 | /* Return true if FILE is a directory or a symlink to a directory. */ | |
2823 | bool | |
2824 | file_directory_p (char const *file) | |
2825 | { | |
2826 | #ifdef WINDOWSNT | |
2827 | /* This is cheaper than 'stat'. */ | |
2828 | return faccessat (AT_FDCWD, file, D_OK, AT_EACCESS) == 0; | |
2829 | #else | |
2830 | struct stat st; | |
2831 | return stat (file, &st) == 0 && S_ISDIR (st.st_mode); | |
2832 | #endif | |
570d7624 JB |
2833 | } |
2834 | ||
a7ca3326 | 2835 | DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, |
16a97296 | 2836 | Sfile_accessible_directory_p, 1, 1, 0, |
e385ec41 RS |
2837 | doc: /* Return t if file FILENAME names a directory you can open. |
2838 | For the value to be t, FILENAME must specify the name of a directory as a file, | |
2839 | and the directory must allow you to open files in it. In order to use a | |
8c1a1077 PJ |
2840 | directory as a buffer's current directory, this predicate must return true. |
2841 | A directory name spec may be given instead; then the value is t | |
2842 | if the directory so specified exists and really is a readable and | |
2843 | searchable directory. */) | |
5842a27b | 2844 | (Lisp_Object filename) |
b72dea2a | 2845 | { |
73dcdb9f | 2846 | Lisp_Object absname; |
32f4334d | 2847 | Lisp_Object handler; |
73dcdb9f PE |
2848 | |
2849 | CHECK_STRING (filename); | |
2850 | absname = Fexpand_file_name (filename, Qnil); | |
32f4334d RS |
2851 | |
2852 | /* If the file name has special constructs in it, | |
2853 | call the corresponding file handler. */ | |
73dcdb9f | 2854 | handler = Ffind_file_name_handler (absname, Qfile_accessible_directory_p); |
32f4334d | 2855 | if (!NILP (handler)) |
73dcdb9f | 2856 | return call2 (handler, Qfile_accessible_directory_p, absname); |
32f4334d | 2857 | |
73dcdb9f PE |
2858 | absname = ENCODE_FILE (absname); |
2859 | return file_accessible_directory_p (SSDATA (absname)) ? Qt : Qnil; | |
2860 | } | |
2861 | ||
2862 | /* If FILE is a searchable directory or a symlink to a | |
2863 | searchable directory, return true. Otherwise return | |
2864 | false and set errno to an error number. */ | |
2865 | bool | |
2866 | file_accessible_directory_p (char const *file) | |
2867 | { | |
2868 | #ifdef DOS_NT | |
2869 | /* There's no need to test whether FILE is searchable, as the | |
2870 | searchable/executable bit is invented on DOS_NT platforms. */ | |
2871 | return file_directory_p (file); | |
2872 | #else | |
2873 | /* On POSIXish platforms, use just one system call; this avoids a | |
2874 | race and is typically faster. */ | |
2875 | ptrdiff_t len = strlen (file); | |
2876 | char const *dir; | |
2877 | bool ok; | |
2878 | int saved_errno; | |
2879 | USE_SAFE_ALLOCA; | |
2880 | ||
2881 | /* Normally a file "FOO" is an accessible directory if "FOO/." exists. | |
2882 | There are three exceptions: "", "/", and "//". Leave "" alone, | |
2883 | as it's invalid. Append only "." to the other two exceptions as | |
2884 | "/" and "//" are distinct on some platforms, whereas "/", "///", | |
2885 | "////", etc. are all equivalent. */ | |
2886 | if (! len) | |
2887 | dir = file; | |
2888 | else | |
2889 | { | |
2890 | /* Just check for trailing '/' when deciding whether to append '/'. | |
2891 | That's simpler than testing the two special cases "/" and "//", | |
2892 | and it's a safe optimization here. */ | |
2893 | char *buf = SAFE_ALLOCA (len + 3); | |
2894 | memcpy (buf, file, len); | |
2895 | strcpy (buf + len, "/." + (file[len - 1] == '/')); | |
2896 | dir = buf; | |
2897 | } | |
2898 | ||
2899 | ok = check_existing (dir); | |
2900 | saved_errno = errno; | |
2901 | SAFE_FREE (); | |
2902 | errno = saved_errno; | |
2903 | return ok; | |
2904 | #endif | |
b72dea2a JB |
2905 | } |
2906 | ||
f793dc6c | 2907 | DEFUN ("file-regular-p", Ffile_regular_p, Sfile_regular_p, 1, 1, 0, |
19a9c3b7 LH |
2908 | doc: /* Return t if FILENAME names a regular file. |
2909 | This is the sort of file that holds an ordinary stream of data bytes. | |
2910 | Symbolic links to regular files count as regular files. | |
2911 | See `file-symlink-p' to distinguish symlinks. */) | |
5842a27b | 2912 | (Lisp_Object filename) |
f793dc6c | 2913 | { |
199607e4 | 2914 | register Lisp_Object absname; |
f793dc6c RS |
2915 | struct stat st; |
2916 | Lisp_Object handler; | |
2917 | ||
4b4deea2 | 2918 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
f793dc6c RS |
2919 | |
2920 | /* If the file name has special constructs in it, | |
2921 | call the corresponding file handler. */ | |
199607e4 | 2922 | handler = Ffind_file_name_handler (absname, Qfile_regular_p); |
f793dc6c | 2923 | if (!NILP (handler)) |
199607e4 | 2924 | return call2 (handler, Qfile_regular_p, absname); |
f793dc6c | 2925 | |
b1d1b865 RS |
2926 | absname = ENCODE_FILE (absname); |
2927 | ||
c1c4693e RS |
2928 | #ifdef WINDOWSNT |
2929 | { | |
2930 | int result; | |
2931 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
2932 | ||
2933 | /* Tell stat to use expensive method to get accurate info. */ | |
2934 | Vw32_get_true_file_attributes = Qt; | |
d5db4077 | 2935 | result = stat (SDATA (absname), &st); |
c1c4693e RS |
2936 | Vw32_get_true_file_attributes = tem; |
2937 | ||
2938 | if (result < 0) | |
2939 | return Qnil; | |
f68c809d | 2940 | return S_ISREG (st.st_mode) ? Qt : Qnil; |
c1c4693e RS |
2941 | } |
2942 | #else | |
42a5b22f | 2943 | if (stat (SSDATA (absname), &st) < 0) |
f793dc6c | 2944 | return Qnil; |
f68c809d | 2945 | return S_ISREG (st.st_mode) ? Qt : Qnil; |
c1c4693e | 2946 | #endif |
f793dc6c RS |
2947 | } |
2948 | \f | |
574c05e2 KK |
2949 | DEFUN ("file-selinux-context", Ffile_selinux_context, |
2950 | Sfile_selinux_context, 1, 1, 0, | |
fa74b241 CY |
2951 | doc: /* Return SELinux context of file named FILENAME. |
2952 | The return value is a list (USER ROLE TYPE RANGE), where the list | |
2953 | elements are strings naming the user, role, type, and range of the | |
2954 | file's SELinux security context. | |
2955 | ||
2956 | Return (nil nil nil nil) if the file is nonexistent or inaccessible, | |
2957 | or if SELinux is disabled, or if Emacs lacks SELinux support. */) | |
5842a27b | 2958 | (Lisp_Object filename) |
574c05e2 KK |
2959 | { |
2960 | Lisp_Object absname; | |
2961 | Lisp_Object values[4]; | |
2962 | Lisp_Object handler; | |
2963 | #if HAVE_LIBSELINUX | |
2964 | security_context_t con; | |
2965 | int conlength; | |
2966 | context_t context; | |
2967 | #endif | |
2968 | ||
4b4deea2 | 2969 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
574c05e2 KK |
2970 | |
2971 | /* If the file name has special constructs in it, | |
2972 | call the corresponding file handler. */ | |
2973 | handler = Ffind_file_name_handler (absname, Qfile_selinux_context); | |
2974 | if (!NILP (handler)) | |
2975 | return call2 (handler, Qfile_selinux_context, absname); | |
2976 | ||
2977 | absname = ENCODE_FILE (absname); | |
2978 | ||
2979 | values[0] = Qnil; | |
2980 | values[1] = Qnil; | |
2981 | values[2] = Qnil; | |
2982 | values[3] = Qnil; | |
2983 | #if HAVE_LIBSELINUX | |
2984 | if (is_selinux_enabled ()) | |
2985 | { | |
b14aac08 | 2986 | conlength = lgetfilecon (SSDATA (absname), &con); |
574c05e2 KK |
2987 | if (conlength > 0) |
2988 | { | |
2989 | context = context_new (con); | |
45841e65 KK |
2990 | if (context_user_get (context)) |
2991 | values[0] = build_string (context_user_get (context)); | |
2992 | if (context_role_get (context)) | |
2993 | values[1] = build_string (context_role_get (context)); | |
2994 | if (context_type_get (context)) | |
2995 | values[2] = build_string (context_type_get (context)); | |
2996 | if (context_range_get (context)) | |
2997 | values[3] = build_string (context_range_get (context)); | |
574c05e2 | 2998 | context_free (context); |
c6ba4138 | 2999 | freecon (con); |
574c05e2 | 3000 | } |
574c05e2 KK |
3001 | } |
3002 | #endif | |
3003 | ||
5e617bc2 | 3004 | return Flist (sizeof (values) / sizeof (values[0]), values); |
574c05e2 KK |
3005 | } |
3006 | \f | |
3007 | DEFUN ("set-file-selinux-context", Fset_file_selinux_context, | |
3008 | Sset_file_selinux_context, 2, 2, 0, | |
fa74b241 CY |
3009 | doc: /* Set SELinux context of file named FILENAME to CONTEXT. |
3010 | CONTEXT should be a list (USER ROLE TYPE RANGE), where the list | |
3011 | elements are strings naming the components of a SELinux context. | |
3012 | ||
ccad023b EZ |
3013 | Value is t if setting of SELinux context was successful, nil otherwise. |
3014 | ||
3015 | This function does nothing and returns nil if SELinux is disabled, | |
3016 | or if Emacs was not compiled with SELinux support. */) | |
5842a27b | 3017 | (Lisp_Object filename, Lisp_Object context) |
574c05e2 | 3018 | { |
0f4a96b5 | 3019 | Lisp_Object absname; |
574c05e2 | 3020 | Lisp_Object handler; |
0f4a96b5 JB |
3021 | #if HAVE_LIBSELINUX |
3022 | Lisp_Object encoded_absname; | |
574c05e2 KK |
3023 | Lisp_Object user = CAR_SAFE (context); |
3024 | Lisp_Object role = CAR_SAFE (CDR_SAFE (context)); | |
3025 | Lisp_Object type = CAR_SAFE (CDR_SAFE (CDR_SAFE (context))); | |
3026 | Lisp_Object range = CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (context)))); | |
574c05e2 | 3027 | security_context_t con; |
f75d7a91 PE |
3028 | bool fail; |
3029 | int conlength; | |
574c05e2 KK |
3030 | context_t parsed_con; |
3031 | #endif | |
3032 | ||
4b4deea2 | 3033 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); |
574c05e2 KK |
3034 | |
3035 | /* If the file name has special constructs in it, | |
3036 | call the corresponding file handler. */ | |
3037 | handler = Ffind_file_name_handler (absname, Qset_file_selinux_context); | |
3038 | if (!NILP (handler)) | |
3039 | return call3 (handler, Qset_file_selinux_context, absname, context); | |
3040 | ||
0f4a96b5 | 3041 | #if HAVE_LIBSELINUX |
574c05e2 KK |
3042 | if (is_selinux_enabled ()) |
3043 | { | |
3044 | /* Get current file context. */ | |
a9b53ad3 | 3045 | encoded_absname = ENCODE_FILE (absname); |
b14aac08 | 3046 | conlength = lgetfilecon (SSDATA (encoded_absname), &con); |
574c05e2 KK |
3047 | if (conlength > 0) |
3048 | { | |
3049 | parsed_con = context_new (con); | |
3050 | /* Change the parts defined in the parameter.*/ | |
3051 | if (STRINGP (user)) | |
3052 | { | |
b14aac08 | 3053 | if (context_user_set (parsed_con, SSDATA (user))) |
574c05e2 KK |
3054 | error ("Doing context_user_set"); |
3055 | } | |
3056 | if (STRINGP (role)) | |
3057 | { | |
b14aac08 | 3058 | if (context_role_set (parsed_con, SSDATA (role))) |
574c05e2 KK |
3059 | error ("Doing context_role_set"); |
3060 | } | |
3061 | if (STRINGP (type)) | |
3062 | { | |
b14aac08 | 3063 | if (context_type_set (parsed_con, SSDATA (type))) |
574c05e2 KK |
3064 | error ("Doing context_type_set"); |
3065 | } | |
3066 | if (STRINGP (range)) | |
3067 | { | |
b14aac08 | 3068 | if (context_range_set (parsed_con, SSDATA (range))) |
574c05e2 KK |
3069 | error ("Doing context_range_set"); |
3070 | } | |
3071 | ||
24c51a09 | 3072 | /* Set the modified context back to the file. */ |
f75d7a91 PE |
3073 | fail = (lsetfilecon (SSDATA (encoded_absname), |
3074 | context_str (parsed_con)) | |
3075 | != 0); | |
24c51a09 GM |
3076 | /* See http://debbugs.gnu.org/11245 for ENOTSUP. */ |
3077 | if (fail && errno != ENOTSUP) | |
574c05e2 KK |
3078 | report_file_error ("Doing lsetfilecon", Fcons (absname, Qnil)); |
3079 | ||
3080 | context_free (parsed_con); | |
c6ba4138 | 3081 | freecon (con); |
c1fea2c0 | 3082 | return fail ? Qnil : Qt; |
574c05e2 KK |
3083 | } |
3084 | else | |
5e617bc2 | 3085 | report_file_error ("Doing lgetfilecon", Fcons (absname, Qnil)); |
574c05e2 KK |
3086 | } |
3087 | #endif | |
3088 | ||
3089 | return Qnil; | |
3090 | } | |
3091 | \f | |
7c3d167f | 3092 | DEFUN ("file-acl", Ffile_acl, Sfile_acl, 1, 1, 0, |
553081a5 SM |
3093 | doc: /* Return ACL entries of file named FILENAME. |
3094 | The entries are returned in a format suitable for use in `set-file-acl' | |
3095 | but is otherwise undocumented and subject to change. | |
7c3d167f | 3096 | Return nil if file does not exist or is not accessible, or if Emacs |
553081a5 | 3097 | was unable to determine the ACL entries. */) |
7c3d167f RF |
3098 | (Lisp_Object filename) |
3099 | { | |
3100 | Lisp_Object absname; | |
3101 | Lisp_Object handler; | |
3102 | #ifdef HAVE_POSIX_ACL | |
3103 | acl_t acl; | |
3104 | Lisp_Object acl_string; | |
3105 | char *str; | |
3106 | #endif | |
3107 | ||
3108 | absname = expand_and_dir_to_file (filename, | |
3109 | BVAR (current_buffer, directory)); | |
3110 | ||
3111 | /* If the file name has special constructs in it, | |
3112 | call the corresponding file handler. */ | |
3113 | handler = Ffind_file_name_handler (absname, Qfile_acl); | |
3114 | if (!NILP (handler)) | |
3115 | return call2 (handler, Qfile_acl, absname); | |
3116 | ||
3117 | #ifdef HAVE_POSIX_ACL | |
3118 | absname = ENCODE_FILE (absname); | |
3119 | ||
3120 | acl = acl_get_file (SSDATA (absname), ACL_TYPE_ACCESS); | |
3121 | if (acl == NULL) | |
3122 | return Qnil; | |
3123 | ||
3124 | str = acl_to_text (acl, NULL); | |
3125 | if (str == NULL) | |
3126 | { | |
3127 | acl_free (acl); | |
3128 | return Qnil; | |
3129 | } | |
3130 | ||
3131 | acl_string = build_string (str); | |
3132 | acl_free (str); | |
3133 | acl_free (acl); | |
3134 | ||
3135 | return acl_string; | |
3136 | #endif | |
3137 | ||
3138 | return Qnil; | |
3139 | } | |
3140 | ||
3141 | DEFUN ("set-file-acl", Fset_file_acl, Sset_file_acl, | |
3142 | 2, 2, 0, | |
3143 | doc: /* Set ACL of file named FILENAME to ACL-STRING. | |
3144 | ACL-STRING should contain the textual representation of the ACL | |
3145 | entries in a format suitable for the platform. | |
3146 | ||
ccad023b EZ |
3147 | Value is t if setting of ACL was successful, nil otherwise. |
3148 | ||
7c3d167f RF |
3149 | Setting ACL for local files requires Emacs to be built with ACL |
3150 | support. */) | |
3151 | (Lisp_Object filename, Lisp_Object acl_string) | |
3152 | { | |
3153 | Lisp_Object absname; | |
3154 | Lisp_Object handler; | |
3155 | #ifdef HAVE_POSIX_ACL | |
3156 | Lisp_Object encoded_absname; | |
3157 | acl_t acl; | |
3158 | bool fail; | |
3159 | #endif | |
3160 | ||
3161 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); | |
3162 | ||
3163 | /* If the file name has special constructs in it, | |
3164 | call the corresponding file handler. */ | |
3165 | handler = Ffind_file_name_handler (absname, Qset_file_acl); | |
3166 | if (!NILP (handler)) | |
3167 | return call3 (handler, Qset_file_acl, absname, acl_string); | |
3168 | ||
3169 | #ifdef HAVE_POSIX_ACL | |
3170 | if (STRINGP (acl_string)) | |
3171 | { | |
3172 | acl = acl_from_text (SSDATA (acl_string)); | |
3173 | if (acl == NULL) | |
3174 | { | |
3175 | report_file_error ("Converting ACL", Fcons (absname, Qnil)); | |
3176 | return Qnil; | |
3177 | } | |
3178 | ||
3179 | encoded_absname = ENCODE_FILE (absname); | |
3180 | ||
3181 | fail = (acl_set_file (SSDATA (encoded_absname), ACL_TYPE_ACCESS, | |
3182 | acl) | |
3183 | != 0); | |
3184 | if (fail && errno != ENOTSUP) | |
3185 | report_file_error ("Setting ACL", Fcons (absname, Qnil)); | |
3186 | ||
3187 | acl_free (acl); | |
c1fea2c0 | 3188 | return fail ? Qnil : Qt; |
7c3d167f RF |
3189 | } |
3190 | #endif | |
3191 | ||
3192 | return Qnil; | |
3193 | } | |
3194 | \f | |
570d7624 | 3195 | DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0, |
d4a42098 KS |
3196 | doc: /* Return mode bits of file named FILENAME, as an integer. |
3197 | Return nil, if file does not exist or is not accessible. */) | |
5842a27b | 3198 | (Lisp_Object filename) |
570d7624 | 3199 | { |
199607e4 | 3200 | Lisp_Object absname; |
570d7624 | 3201 | struct stat st; |
32f4334d | 3202 | Lisp_Object handler; |
570d7624 | 3203 | |
4b4deea2 | 3204 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
570d7624 | 3205 | |
32f4334d RS |
3206 | /* If the file name has special constructs in it, |
3207 | call the corresponding file handler. */ | |
199607e4 | 3208 | handler = Ffind_file_name_handler (absname, Qfile_modes); |
32f4334d | 3209 | if (!NILP (handler)) |
199607e4 | 3210 | return call2 (handler, Qfile_modes, absname); |
32f4334d | 3211 | |
b1d1b865 RS |
3212 | absname = ENCODE_FILE (absname); |
3213 | ||
42a5b22f | 3214 | if (stat (SSDATA (absname), &st) < 0) |
570d7624 | 3215 | return Qnil; |
3ace87e3 | 3216 | |
570d7624 JB |
3217 | return make_number (st.st_mode & 07777); |
3218 | } | |
3219 | ||
09fbdf6c MC |
3220 | DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, |
3221 | "(let ((file (read-file-name \"File: \"))) \ | |
3222 | (list file (read-file-modes nil file)))", | |
8c1a1077 | 3223 | doc: /* Set mode bits of file named FILENAME to MODE (an integer). |
ea429250 EZ |
3224 | Only the 12 low bits of MODE are used. |
3225 | ||
3226 | Interactively, mode bits are read by `read-file-modes', which accepts | |
712adc82 | 3227 | symbolic notation, like the `chmod' command from GNU Coreutils. */) |
5842a27b | 3228 | (Lisp_Object filename, Lisp_Object mode) |
570d7624 | 3229 | { |
b1d1b865 | 3230 | Lisp_Object absname, encoded_absname; |
32f4334d | 3231 | Lisp_Object handler; |
570d7624 | 3232 | |
4b4deea2 | 3233 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); |
b7826503 | 3234 | CHECK_NUMBER (mode); |
570d7624 | 3235 | |
32f4334d RS |
3236 | /* If the file name has special constructs in it, |
3237 | call the corresponding file handler. */ | |
199607e4 | 3238 | handler = Ffind_file_name_handler (absname, Qset_file_modes); |
32f4334d | 3239 | if (!NILP (handler)) |
199607e4 | 3240 | return call3 (handler, Qset_file_modes, absname, mode); |
32f4334d | 3241 | |
b1d1b865 RS |
3242 | encoded_absname = ENCODE_FILE (absname); |
3243 | ||
18c52557 | 3244 | if (chmod (SSDATA (encoded_absname), XINT (mode) & 07777) < 0) |
199607e4 | 3245 | report_file_error ("Doing chmod", Fcons (absname, Qnil)); |
570d7624 JB |
3246 | |
3247 | return Qnil; | |
3248 | } | |
3249 | ||
c24e9a53 | 3250 | DEFUN ("set-default-file-modes", Fset_default_file_modes, Sset_default_file_modes, 1, 1, 0, |
8c1a1077 PJ |
3251 | doc: /* Set the file permission bits for newly created files. |
3252 | The argument MODE should be an integer; only the low 9 bits are used. | |
3253 | This setting is inherited by subprocesses. */) | |
5842a27b | 3254 | (Lisp_Object mode) |
36a8c287 | 3255 | { |
b7826503 | 3256 | CHECK_NUMBER (mode); |
199607e4 | 3257 | |
5f85ea58 | 3258 | umask ((~ XINT (mode)) & 0777); |
36a8c287 JB |
3259 | |
3260 | return Qnil; | |
3261 | } | |
3262 | ||
c24e9a53 | 3263 | DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0, |
8c1a1077 PJ |
3264 | doc: /* Return the default file protection for created files. |
3265 | The value is an integer. */) | |
5842a27b | 3266 | (void) |
36a8c287 | 3267 | { |
f75d7a91 | 3268 | mode_t realmask; |
5f85ea58 | 3269 | Lisp_Object value; |
36a8c287 | 3270 | |
4d7e6e51 | 3271 | block_input (); |
5f85ea58 RS |
3272 | realmask = umask (0); |
3273 | umask (realmask); | |
4d7e6e51 | 3274 | unblock_input (); |
36a8c287 | 3275 | |
46283abe | 3276 | XSETINT (value, (~ realmask) & 0777); |
5f85ea58 | 3277 | return value; |
36a8c287 | 3278 | } |
819da85b | 3279 | \f |
819da85b EZ |
3280 | |
3281 | DEFUN ("set-file-times", Fset_file_times, Sset_file_times, 1, 2, 0, | |
f839df0c | 3282 | doc: /* Set times of file FILENAME to TIMESTAMP. |
819da85b EZ |
3283 | Set both access and modification times. |
3284 | Return t on success, else nil. | |
f839df0c | 3285 | Use the current time if TIMESTAMP is nil. TIMESTAMP is in the format of |
819da85b | 3286 | `current-time'. */) |
f839df0c | 3287 | (Lisp_Object filename, Lisp_Object timestamp) |
819da85b EZ |
3288 | { |
3289 | Lisp_Object absname, encoded_absname; | |
3290 | Lisp_Object handler; | |
31571fd7 | 3291 | EMACS_TIME t = lisp_time_argument (timestamp); |
819da85b | 3292 | |
4b4deea2 | 3293 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); |
819da85b EZ |
3294 | |
3295 | /* If the file name has special constructs in it, | |
3296 | call the corresponding file handler. */ | |
3297 | handler = Ffind_file_name_handler (absname, Qset_file_times); | |
3298 | if (!NILP (handler)) | |
f839df0c | 3299 | return call3 (handler, Qset_file_times, absname, timestamp); |
819da85b EZ |
3300 | |
3301 | encoded_absname = ENCODE_FILE (absname); | |
5df5e07c | 3302 | |
819da85b | 3303 | { |
d35af63c | 3304 | if (set_file_times (-1, SSDATA (encoded_absname), t, t)) |
819da85b | 3305 | { |
0d9f584b | 3306 | #ifdef MSDOS |
819da85b | 3307 | /* Setting times on a directory always fails. */ |
73dcdb9f | 3308 | if (file_directory_p (SSDATA (encoded_absname))) |
819da85b EZ |
3309 | return Qnil; |
3310 | #endif | |
3311 | report_file_error ("Setting file times", Fcons (absname, Qnil)); | |
3312 | return Qnil; | |
3313 | } | |
3314 | } | |
5df5e07c | 3315 | |
819da85b EZ |
3316 | return Qt; |
3317 | } | |
f793dc6c | 3318 | \f |
697c17a2 | 3319 | #ifdef HAVE_SYNC |
85ffea93 | 3320 | DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "", |
8c1a1077 | 3321 | doc: /* Tell Unix to finish all pending disk updates. */) |
5842a27b | 3322 | (void) |
85ffea93 RS |
3323 | { |
3324 | sync (); | |
3325 | return Qnil; | |
3326 | } | |
3327 | ||
697c17a2 | 3328 | #endif /* HAVE_SYNC */ |
85ffea93 | 3329 | |
570d7624 | 3330 | DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0, |
8c1a1077 PJ |
3331 | doc: /* Return t if file FILE1 is newer than file FILE2. |
3332 | If FILE1 does not exist, the answer is nil; | |
3333 | otherwise, if FILE2 does not exist, the answer is t. */) | |
5842a27b | 3334 | (Lisp_Object file1, Lisp_Object file2) |
570d7624 | 3335 | { |
199607e4 | 3336 | Lisp_Object absname1, absname2; |
39adff0d | 3337 | struct stat st1, st2; |
32f4334d | 3338 | Lisp_Object handler; |
09121adc | 3339 | struct gcpro gcpro1, gcpro2; |
570d7624 | 3340 | |
b7826503 PJ |
3341 | CHECK_STRING (file1); |
3342 | CHECK_STRING (file2); | |
570d7624 | 3343 | |
199607e4 RS |
3344 | absname1 = Qnil; |
3345 | GCPRO2 (absname1, file2); | |
4b4deea2 TT |
3346 | absname1 = expand_and_dir_to_file (file1, BVAR (current_buffer, directory)); |
3347 | absname2 = expand_and_dir_to_file (file2, BVAR (current_buffer, directory)); | |
09121adc | 3348 | UNGCPRO; |
570d7624 | 3349 | |
32f4334d RS |
3350 | /* If the file name has special constructs in it, |
3351 | call the corresponding file handler. */ | |
199607e4 | 3352 | handler = Ffind_file_name_handler (absname1, Qfile_newer_than_file_p); |
51cf6d37 | 3353 | if (NILP (handler)) |
199607e4 | 3354 | handler = Ffind_file_name_handler (absname2, Qfile_newer_than_file_p); |
32f4334d | 3355 | if (!NILP (handler)) |
199607e4 | 3356 | return call3 (handler, Qfile_newer_than_file_p, absname1, absname2); |
32f4334d | 3357 | |
b1d1b865 RS |
3358 | GCPRO2 (absname1, absname2); |
3359 | absname1 = ENCODE_FILE (absname1); | |
3360 | absname2 = ENCODE_FILE (absname2); | |
3361 | UNGCPRO; | |
3362 | ||
39adff0d | 3363 | if (stat (SSDATA (absname1), &st1) < 0) |
570d7624 JB |
3364 | return Qnil; |
3365 | ||
39adff0d | 3366 | if (stat (SSDATA (absname2), &st2) < 0) |
570d7624 JB |
3367 | return Qt; |
3368 | ||
39adff0d PE |
3369 | return (EMACS_TIME_GT (get_stat_mtime (&st1), get_stat_mtime (&st2)) |
3370 | ? Qt : Qnil); | |
570d7624 JB |
3371 | } |
3372 | \f | |
6fdaa9a0 KH |
3373 | #ifndef READ_BUF_SIZE |
3374 | #define READ_BUF_SIZE (64 << 10) | |
3375 | #endif | |
d311d28c PE |
3376 | /* Some buffer offsets are stored in 'int' variables. */ |
3377 | verify (READ_BUF_SIZE <= INT_MAX); | |
6fdaa9a0 | 3378 | |
98a7d268 KH |
3379 | /* This function is called after Lisp functions to decide a coding |
3380 | system are called, or when they cause an error. Before they are | |
3381 | called, the current buffer is set unibyte and it contains only a | |
3382 | newly inserted text (thus the buffer was empty before the | |
3383 | insertion). | |
3384 | ||
3385 | The functions may set markers, overlays, text properties, or even | |
3386 | alter the buffer contents, change the current buffer. | |
3387 | ||
3388 | Here, we reset all those changes by: | |
3389 | o set back the current buffer. | |
3390 | o move all markers and overlays to BEG. | |
3391 | o remove all text properties. | |
3392 | o set back the buffer multibyteness. */ | |
f736ffbf KH |
3393 | |
3394 | static Lisp_Object | |
971de7fb | 3395 | decide_coding_unwind (Lisp_Object unwind_data) |
f736ffbf | 3396 | { |
98a7d268 | 3397 | Lisp_Object multibyte, undo_list, buffer; |
f736ffbf | 3398 | |
98a7d268 KH |
3399 | multibyte = XCAR (unwind_data); |
3400 | unwind_data = XCDR (unwind_data); | |
3401 | undo_list = XCAR (unwind_data); | |
3402 | buffer = XCDR (unwind_data); | |
3403 | ||
a3d794a1 | 3404 | set_buffer_internal (XBUFFER (buffer)); |
98a7d268 KH |
3405 | adjust_markers_for_delete (BEG, BEG_BYTE, Z, Z_BYTE); |
3406 | adjust_overlays_for_delete (BEG, Z - BEG); | |
0c94c8d6 | 3407 | set_buffer_intervals (current_buffer, NULL); |
98a7d268 KH |
3408 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); |
3409 | ||
3410 | /* Now we are safe to change the buffer's multibyteness directly. */ | |
39eb03f1 PE |
3411 | bset_enable_multibyte_characters (current_buffer, multibyte); |
3412 | bset_undo_list (current_buffer, undo_list); | |
f736ffbf KH |
3413 | |
3414 | return Qnil; | |
3415 | } | |
3416 | ||
2ef88fd4 | 3417 | /* Read from a non-regular file. STATE is a Lisp_Save_Value |
3346c1d0 DA |
3418 | object where slot 0 is the file descriptor, slot 1 specifies |
3419 | an offset to put the read bytes, and slot 2 is the maximum | |
3420 | amount of bytes to read. Value is the number of bytes read. */ | |
55587f8a GM |
3421 | |
3422 | static Lisp_Object | |
2ef88fd4 | 3423 | read_non_regular (Lisp_Object state) |
55587f8a | 3424 | { |
d311d28c | 3425 | int nbytes; |
efdc16c9 | 3426 | |
1b978129 GM |
3427 | immediate_quit = 1; |
3428 | QUIT; | |
3346c1d0 | 3429 | nbytes = emacs_read (XSAVE_INTEGER (state, 0), |
5976c3fe | 3430 | ((char *) BEG_ADDR + PT_BYTE - BEG_BYTE |
3346c1d0 DA |
3431 | + XSAVE_INTEGER (state, 1)), |
3432 | XSAVE_INTEGER (state, 2)); | |
1b978129 | 3433 | immediate_quit = 0; |
e07469fa DA |
3434 | /* Fast recycle this object for the likely next call. */ |
3435 | free_misc (state); | |
1b978129 GM |
3436 | return make_number (nbytes); |
3437 | } | |
55587f8a | 3438 | |
2ef88fd4 DA |
3439 | |
3440 | /* Condition-case handler used when reading from non-regular files | |
3441 | in insert-file-contents. */ | |
1b978129 GM |
3442 | |
3443 | static Lisp_Object | |
2ef88fd4 | 3444 | read_non_regular_quit (Lisp_Object ignore) |
1b978129 | 3445 | { |
55587f8a GM |
3446 | return Qnil; |
3447 | } | |
3448 | ||
b3fbb395 PE |
3449 | /* Return the file offset that VAL represents, checking for type |
3450 | errors and overflow. */ | |
e69dafad | 3451 | static off_t |
b3fbb395 | 3452 | file_offset (Lisp_Object val) |
e69dafad | 3453 | { |
b3fbb395 PE |
3454 | if (RANGED_INTEGERP (0, val, TYPE_MAXIMUM (off_t))) |
3455 | return XINT (val); | |
3456 | ||
3457 | if (FLOATP (val)) | |
e69dafad | 3458 | { |
b3fbb395 PE |
3459 | double v = XFLOAT_DATA (val); |
3460 | if (0 <= v | |
3461 | && (sizeof (off_t) < sizeof v | |
3462 | ? v <= TYPE_MAXIMUM (off_t) | |
3463 | : v < TYPE_MAXIMUM (off_t))) | |
3464 | return v; | |
e69dafad | 3465 | } |
b3fbb395 PE |
3466 | |
3467 | wrong_type_argument (intern ("file-offset"), val); | |
e69dafad PE |
3468 | } |
3469 | ||
f0941253 | 3470 | /* Return a special time value indicating the error number ERRNUM. */ |
dbeed9a6 | 3471 | static EMACS_TIME |
f0941253 | 3472 | time_error_value (int errnum) |
dbeed9a6 | 3473 | { |
0497dc44 | 3474 | int ns = (errnum == ENOENT || errnum == EACCES || errnum == ENOTDIR |
dbeed9a6 PE |
3475 | ? NONEXISTENT_MODTIME_NSECS |
3476 | : UNKNOWN_MODTIME_NSECS); | |
e9a9ae03 | 3477 | return make_emacs_time (0, ns); |
dbeed9a6 | 3478 | } |
55587f8a | 3479 | |
570d7624 | 3480 | DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents, |
8c1a1077 PJ |
3481 | 1, 5, 0, |
3482 | doc: /* Insert contents of file FILENAME after point. | |
cf6d2357 | 3483 | Returns list of absolute file name and number of characters inserted. |
6f2528d8 MR |
3484 | If second argument VISIT is non-nil, the buffer's visited filename and |
3485 | last save file modtime are set, and it is marked unmodified. If | |
3486 | visiting and the file does not exist, visiting is completed before the | |
3487 | error is signaled. | |
3488 | ||
3489 | The optional third and fourth arguments BEG and END specify what portion | |
3490 | of the file to insert. These arguments count bytes in the file, not | |
3491 | characters in the buffer. If VISIT is non-nil, BEG and END must be nil. | |
3492 | ||
3493 | If optional fifth argument REPLACE is non-nil, replace the current | |
3494 | buffer contents (in the accessible portion) with the file contents. | |
3495 | This is better than simply deleting and inserting the whole thing | |
3496 | because (1) it preserves some marker positions and (2) it puts less data | |
3497 | in the undo list. When REPLACE is non-nil, the second return value is | |
3498 | the number of characters that replace previous buffer contents. | |
3499 | ||
3500 | This function does code conversion according to the value of | |
3501 | `coding-system-for-read' or `file-coding-system-alist', and sets the | |
bb085aed EZ |
3502 | variable `last-coding-system-used' to the coding system actually used. |
3503 | ||
3504 | In addition, this function decodes the inserted text from known formats | |
3505 | by calling `format-decode', which see. */) | |
5842a27b | 3506 | (Lisp_Object filename, Lisp_Object visit, Lisp_Object beg, Lisp_Object end, Lisp_Object replace) |
570d7624 JB |
3507 | { |
3508 | struct stat st; | |
dbeed9a6 | 3509 | EMACS_TIME mtime; |
f75d7a91 | 3510 | int fd; |
d311d28c | 3511 | ptrdiff_t inserted = 0; |
f75d7a91 PE |
3512 | bool nochange = 0; |
3513 | ptrdiff_t how_much; | |
728f8f0a | 3514 | off_t beg_offset, end_offset; |
f75d7a91 | 3515 | int unprocessed; |
d311d28c | 3516 | ptrdiff_t count = SPECPDL_INDEX (); |
6f2528d8 MR |
3517 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
3518 | Lisp_Object handler, val, insval, orig_filename, old_undo; | |
d6a3cc15 | 3519 | Lisp_Object p; |
d311d28c | 3520 | ptrdiff_t total = 0; |
f75d7a91 | 3521 | bool not_regular = 0; |
2ef88fd4 | 3522 | int save_errno = 0; |
5976c3fe | 3523 | char read_buf[READ_BUF_SIZE]; |
6fdaa9a0 | 3524 | struct coding_system coding; |
f75d7a91 PE |
3525 | bool replace_handled = 0; |
3526 | bool set_coding_system = 0; | |
db327c7e | 3527 | Lisp_Object coding_system; |
f75d7a91 | 3528 | bool read_quit = 0; |
490ee853 | 3529 | Lisp_Object old_Vdeactivate_mark = Vdeactivate_mark; |
f75d7a91 PE |
3530 | bool we_locked_file = 0; |
3531 | bool deferred_remove_unwind_protect = 0; | |
32f4334d | 3532 | |
95385625 RS |
3533 | if (current_buffer->base_buffer && ! NILP (visit)) |
3534 | error ("Cannot do file visiting in an indirect buffer"); | |
3535 | ||
4b4deea2 | 3536 | if (!NILP (BVAR (current_buffer, read_only))) |
95385625 RS |
3537 | Fbarf_if_buffer_read_only (); |
3538 | ||
32f4334d | 3539 | val = Qnil; |
d6a3cc15 | 3540 | p = Qnil; |
b1d1b865 | 3541 | orig_filename = Qnil; |
6f2528d8 | 3542 | old_undo = Qnil; |
32f4334d | 3543 | |
6f2528d8 | 3544 | GCPRO5 (filename, val, p, orig_filename, old_undo); |
570d7624 | 3545 | |
b7826503 | 3546 | CHECK_STRING (filename); |
570d7624 JB |
3547 | filename = Fexpand_file_name (filename, Qnil); |
3548 | ||
1c157f8d KH |
3549 | /* The value Qnil means that the coding system is not yet |
3550 | decided. */ | |
3551 | coding_system = Qnil; | |
3552 | ||
32f4334d RS |
3553 | /* If the file name has special constructs in it, |
3554 | call the corresponding file handler. */ | |
49307295 | 3555 | handler = Ffind_file_name_handler (filename, Qinsert_file_contents); |
32f4334d RS |
3556 | if (!NILP (handler)) |
3557 | { | |
3d0387c0 RS |
3558 | val = call6 (handler, Qinsert_file_contents, filename, |
3559 | visit, beg, end, replace); | |
d311d28c PE |
3560 | if (CONSP (val) && CONSP (XCDR (val)) |
3561 | && RANGED_INTEGERP (0, XCAR (XCDR (val)), ZV - PT)) | |
03699b14 | 3562 | inserted = XINT (XCAR (XCDR (val))); |
32f4334d RS |
3563 | goto handled; |
3564 | } | |
3565 | ||
b1d1b865 RS |
3566 | orig_filename = filename; |
3567 | filename = ENCODE_FILE (filename); | |
3568 | ||
67722112 PE |
3569 | fd = emacs_open (SSDATA (filename), O_RDONLY, 0); |
3570 | if (fd < 0) | |
570d7624 | 3571 | { |
e5e9d610 | 3572 | save_errno = errno; |
265a9e55 | 3573 | if (NILP (visit)) |
b1d1b865 | 3574 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
f0941253 | 3575 | mtime = time_error_value (save_errno); |
b885bf36 | 3576 | st.st_size = -1; |
0de6b8f4 | 3577 | if (!NILP (Vcoding_system_for_read)) |
22d92d6b | 3578 | Fset (Qbuffer_file_coding_system, Vcoding_system_for_read); |
570d7624 JB |
3579 | goto notfound; |
3580 | } | |
3581 | ||
67722112 PE |
3582 | /* Replacement should preserve point as it preserves markers. */ |
3583 | if (!NILP (replace)) | |
3584 | record_unwind_protect (restore_point_unwind, Fpoint_marker ()); | |
3585 | ||
3586 | record_unwind_protect (close_file_unwind, make_number (fd)); | |
3587 | ||
3588 | if (fstat (fd, &st) != 0) | |
fa705c99 | 3589 | report_file_error ("Input file status", Fcons (orig_filename, Qnil)); |
67722112 PE |
3590 | mtime = get_stat_mtime (&st); |
3591 | ||
be53b411 JB |
3592 | /* This code will need to be changed in order to work on named |
3593 | pipes, and it's probably just not worth it. So we should at | |
3594 | least signal an error. */ | |
99bc28f4 | 3595 | if (!S_ISREG (st.st_mode)) |
330bfe57 | 3596 | { |
d4b8687b RS |
3597 | not_regular = 1; |
3598 | ||
3599 | if (! NILP (visit)) | |
3600 | goto notfound; | |
3601 | ||
3602 | if (! NILP (replace) || ! NILP (beg) || ! NILP (end)) | |
24b1ddad KS |
3603 | xsignal2 (Qfile_error, |
3604 | build_string ("not a regular file"), orig_filename); | |
330bfe57 | 3605 | } |
be53b411 | 3606 | |
9f57b6b4 KH |
3607 | if (!NILP (visit)) |
3608 | { | |
3609 | if (!NILP (beg) || !NILP (end)) | |
3610 | error ("Attempt to visit less than an entire file"); | |
3611 | if (BEG < Z && NILP (replace)) | |
3612 | error ("Cannot do file visiting in a non-empty buffer"); | |
3613 | } | |
7fded690 JB |
3614 | |
3615 | if (!NILP (beg)) | |
b3fbb395 | 3616 | beg_offset = file_offset (beg); |
7fded690 | 3617 | else |
728f8f0a | 3618 | beg_offset = 0; |
7fded690 JB |
3619 | |
3620 | if (!NILP (end)) | |
b3fbb395 | 3621 | end_offset = file_offset (end); |
7fded690 JB |
3622 | else |
3623 | { | |
728f8f0a PE |
3624 | if (not_regular) |
3625 | end_offset = TYPE_MAXIMUM (off_t); | |
3626 | else | |
d4b8687b | 3627 | { |
728f8f0a PE |
3628 | end_offset = st.st_size; |
3629 | ||
3630 | /* A negative size can happen on a platform that allows file | |
3631 | sizes greater than the maximum off_t value. */ | |
3632 | if (end_offset < 0) | |
3633 | buffer_overflow (); | |
68c45bf0 | 3634 | |
d21dd12d GM |
3635 | /* The file size returned from stat may be zero, but data |
3636 | may be readable nonetheless, for example when this is a | |
3637 | file in the /proc filesystem. */ | |
728f8f0a PE |
3638 | if (end_offset == 0) |
3639 | end_offset = READ_BUF_SIZE; | |
d4b8687b | 3640 | } |
7fded690 JB |
3641 | } |
3642 | ||
728f8f0a PE |
3643 | /* Check now whether the buffer will become too large, |
3644 | in the likely case where the file's length is not changing. | |
3645 | This saves a lot of needless work before a buffer overflow. */ | |
3646 | if (! not_regular) | |
3647 | { | |
3648 | /* The likely offset where we will stop reading. We could read | |
3649 | more (or less), if the file grows (or shrinks) as we read it. */ | |
3650 | off_t likely_end = min (end_offset, st.st_size); | |
3651 | ||
3652 | if (beg_offset < likely_end) | |
3653 | { | |
ce2fe65a AS |
3654 | ptrdiff_t buf_bytes |
3655 | = Z_BYTE - (!NILP (replace) ? ZV_BYTE - BEGV_BYTE : 0); | |
728f8f0a PE |
3656 | ptrdiff_t buf_growth_max = BUF_BYTES_MAX - buf_bytes; |
3657 | off_t likely_growth = likely_end - beg_offset; | |
3658 | if (buf_growth_max < likely_growth) | |
3659 | buffer_overflow (); | |
3660 | } | |
3661 | } | |
3662 | ||
3663 | /* Prevent redisplay optimizations. */ | |
3664 | current_buffer->clip_changed = 1; | |
3665 | ||
356a6224 | 3666 | if (EQ (Vcoding_system_for_read, Qauto_save_coding)) |
5560aecd | 3667 | { |
75421805 | 3668 | coding_system = coding_inherit_eol_type (Qutf_8_emacs, Qunix); |
5560aecd KH |
3669 | setup_coding_system (coding_system, &coding); |
3670 | /* Ensure we set Vlast_coding_system_used. */ | |
3671 | set_coding_system = 1; | |
3672 | } | |
356a6224 | 3673 | else if (BEG < Z) |
f736ffbf KH |
3674 | { |
3675 | /* Decide the coding system to use for reading the file now | |
3676 | because we can't use an optimized method for handling | |
3677 | `coding:' tag if the current buffer is not empty. */ | |
f736ffbf | 3678 | if (!NILP (Vcoding_system_for_read)) |
db327c7e | 3679 | coding_system = Vcoding_system_for_read; |
f736ffbf KH |
3680 | else |
3681 | { | |
3682 | /* Don't try looking inside a file for a coding system | |
3683 | specification if it is not seekable. */ | |
3684 | if (! not_regular && ! NILP (Vset_auto_coding_function)) | |
3685 | { | |
3686 | /* Find a coding system specified in the heading two | |
3687 | lines or in the tailing several lines of the file. | |
3688 | We assume that the 1K-byte and 3K-byte for heading | |
003a7eaa | 3689 | and tailing respectively are sufficient for this |
f736ffbf | 3690 | purpose. */ |
d311d28c | 3691 | int nread; |
f736ffbf KH |
3692 | |
3693 | if (st.st_size <= (1024 * 4)) | |
68c45bf0 | 3694 | nread = emacs_read (fd, read_buf, 1024 * 4); |
f736ffbf KH |
3695 | else |
3696 | { | |
68c45bf0 | 3697 | nread = emacs_read (fd, read_buf, 1024); |
4c95c9a5 | 3698 | if (nread == 1024) |
f736ffbf | 3699 | { |
4c95c9a5 PE |
3700 | int ntail; |
3701 | if (lseek (fd, - (1024 * 3), SEEK_END) < 0) | |
f736ffbf KH |
3702 | report_file_error ("Setting file position", |
3703 | Fcons (orig_filename, Qnil)); | |
4c95c9a5 PE |
3704 | ntail = emacs_read (fd, read_buf + nread, 1024 * 3); |
3705 | nread = ntail < 0 ? ntail : nread + ntail; | |
f736ffbf KH |
3706 | } |
3707 | } | |
feb9dc27 | 3708 | |
f736ffbf KH |
3709 | if (nread < 0) |
3710 | error ("IO error reading %s: %s", | |
d5db4077 | 3711 | SDATA (orig_filename), emacs_strerror (errno)); |
f736ffbf KH |
3712 | else if (nread > 0) |
3713 | { | |
f736ffbf | 3714 | struct buffer *prev = current_buffer; |
f839df0c | 3715 | Lisp_Object workbuf; |
685fc579 | 3716 | struct buffer *buf; |
f736ffbf | 3717 | |
66322887 | 3718 | record_unwind_current_buffer (); |
1d92afcd | 3719 | |
f839df0c PE |
3720 | workbuf = Fget_buffer_create (build_string (" *code-converting-work*")); |
3721 | buf = XBUFFER (workbuf); | |
685fc579 | 3722 | |
29ea8ae9 | 3723 | delete_all_overlays (buf); |
39eb03f1 PE |
3724 | bset_directory (buf, BVAR (current_buffer, directory)); |
3725 | bset_read_only (buf, Qnil); | |
3726 | bset_filename (buf, Qnil); | |
3727 | bset_undo_list (buf, Qt); | |
fd318b54 DA |
3728 | eassert (buf->overlays_before == NULL); |
3729 | eassert (buf->overlays_after == NULL); | |
efdc16c9 | 3730 | |
685fc579 RS |
3731 | set_buffer_internal (buf); |
3732 | Ferase_buffer (); | |
39eb03f1 | 3733 | bset_enable_multibyte_characters (buf, Qnil); |
685fc579 | 3734 | |
b68864e5 | 3735 | insert_1_both ((char *) read_buf, nread, nread, 0, 0, 0); |
f736ffbf | 3736 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); |
db327c7e | 3737 | coding_system = call2 (Vset_auto_coding_function, |
8f924df7 | 3738 | filename, make_number (nread)); |
f736ffbf | 3739 | set_buffer_internal (prev); |
efdc16c9 | 3740 | |
f736ffbf KH |
3741 | /* Discard the unwind protect for recovering the |
3742 | current buffer. */ | |
3743 | specpdl_ptr--; | |
3744 | ||
3745 | /* Rewind the file for the actual read done later. */ | |
e69dafad | 3746 | if (lseek (fd, 0, SEEK_SET) < 0) |
f736ffbf KH |
3747 | report_file_error ("Setting file position", |
3748 | Fcons (orig_filename, Qnil)); | |
3749 | } | |
3750 | } | |
feb9dc27 | 3751 | |
db327c7e | 3752 | if (NILP (coding_system)) |
f736ffbf KH |
3753 | { |
3754 | /* If we have not yet decided a coding system, check | |
3755 | file-coding-system-alist. */ | |
8f924df7 | 3756 | Lisp_Object args[6]; |
f736ffbf KH |
3757 | |
3758 | args[0] = Qinsert_file_contents, args[1] = orig_filename; | |
3759 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace; | |
8f924df7 KH |
3760 | coding_system = Ffind_operation_coding_system (6, args); |
3761 | if (CONSP (coding_system)) | |
3762 | coding_system = XCAR (coding_system); | |
f736ffbf KH |
3763 | } |
3764 | } | |
c9e82392 | 3765 | |
db327c7e KH |
3766 | if (NILP (coding_system)) |
3767 | coding_system = Qundecided; | |
3768 | else | |
3769 | CHECK_CODING_SYSTEM (coding_system); | |
c8a6d68a | 3770 | |
4b4deea2 | 3771 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
237a6fd2 RS |
3772 | /* We must suppress all character code conversion except for |
3773 | end-of-line conversion. */ | |
db327c7e | 3774 | coding_system = raw_text_coding_system (coding_system); |
54369368 | 3775 | |
db327c7e KH |
3776 | setup_coding_system (coding_system, &coding); |
3777 | /* Ensure we set Vlast_coding_system_used. */ | |
3778 | set_coding_system = 1; | |
f736ffbf | 3779 | } |
6cf71bf1 | 3780 | |
3d0387c0 RS |
3781 | /* If requested, replace the accessible part of the buffer |
3782 | with the file contents. Avoid replacing text at the | |
3783 | beginning or end of the buffer that matches the file contents; | |
3dbcf3f6 RS |
3784 | that preserves markers pointing to the unchanged parts. |
3785 | ||
3786 | Here we implement this feature in an optimized way | |
3787 | for the case where code conversion is NOT needed. | |
3788 | The following if-statement handles the case of conversion | |
727a0b4a RS |
3789 | in a less optimal way. |
3790 | ||
3791 | If the code conversion is "automatic" then we try using this | |
3792 | method and hope for the best. | |
3793 | But if we discover the need for conversion, we give up on this method | |
3794 | and let the following if-statement handle the replace job. */ | |
3dbcf3f6 | 3795 | if (!NILP (replace) |
f736ffbf | 3796 | && BEGV < ZV |
db327c7e KH |
3797 | && (NILP (coding_system) |
3798 | || ! CODING_REQUIRE_DECODING (&coding))) | |
3d0387c0 | 3799 | { |
ec7adf26 RS |
3800 | /* same_at_start and same_at_end count bytes, |
3801 | because file access counts bytes | |
3802 | and BEG and END count bytes. */ | |
d311d28c PE |
3803 | ptrdiff_t same_at_start = BEGV_BYTE; |
3804 | ptrdiff_t same_at_end = ZV_BYTE; | |
3805 | ptrdiff_t overlap; | |
6fdaa9a0 | 3806 | /* There is still a possibility we will find the need to do code |
f75d7a91 | 3807 | conversion. If that happens, set this variable to |
727a0b4a | 3808 | give up on handling REPLACE in the optimized way. */ |
f75d7a91 | 3809 | bool giveup_match_end = 0; |
9c28748f | 3810 | |
728f8f0a | 3811 | if (beg_offset != 0) |
4d2a0879 | 3812 | { |
728f8f0a | 3813 | if (lseek (fd, beg_offset, SEEK_SET) < 0) |
4d2a0879 | 3814 | report_file_error ("Setting file position", |
b1d1b865 | 3815 | Fcons (orig_filename, Qnil)); |
4d2a0879 RS |
3816 | } |
3817 | ||
3d0387c0 RS |
3818 | immediate_quit = 1; |
3819 | QUIT; | |
3820 | /* Count how many chars at the start of the file | |
3821 | match the text at the beginning of the buffer. */ | |
3822 | while (1) | |
3823 | { | |
d311d28c | 3824 | int nread, bufpos; |
3d0387c0 | 3825 | |
2ef88fd4 | 3826 | nread = emacs_read (fd, read_buf, sizeof read_buf); |
3d0387c0 RS |
3827 | if (nread < 0) |
3828 | error ("IO error reading %s: %s", | |
5976c3fe | 3829 | SSDATA (orig_filename), emacs_strerror (errno)); |
3d0387c0 RS |
3830 | else if (nread == 0) |
3831 | break; | |
6fdaa9a0 | 3832 | |
db327c7e | 3833 | if (CODING_REQUIRE_DETECTION (&coding)) |
727a0b4a | 3834 | { |
2ef88fd4 | 3835 | coding_system = detect_coding_system ((unsigned char *) read_buf, |
5976c3fe | 3836 | nread, nread, 1, 0, |
db327c7e KH |
3837 | coding_system); |
3838 | setup_coding_system (coding_system, &coding); | |
727a0b4a RS |
3839 | } |
3840 | ||
db327c7e KH |
3841 | if (CODING_REQUIRE_DECODING (&coding)) |
3842 | /* We found that the file should be decoded somehow. | |
727a0b4a RS |
3843 | Let's give up here. */ |
3844 | { | |
3845 | giveup_match_end = 1; | |
3846 | break; | |
3847 | } | |
3848 | ||
3d0387c0 | 3849 | bufpos = 0; |
ec7adf26 | 3850 | while (bufpos < nread && same_at_start < ZV_BYTE |
2ef88fd4 | 3851 | && FETCH_BYTE (same_at_start) == read_buf[bufpos]) |
3d0387c0 RS |
3852 | same_at_start++, bufpos++; |
3853 | /* If we found a discrepancy, stop the scan. | |
8e6208c5 | 3854 | Otherwise loop around and scan the next bufferful. */ |
3d0387c0 RS |
3855 | if (bufpos != nread) |
3856 | break; | |
3857 | } | |
3858 | immediate_quit = 0; | |
3859 | /* If the file matches the buffer completely, | |
3860 | there's no need to replace anything. */ | |
2ba8e008 | 3861 | if (same_at_start - BEGV_BYTE == end_offset - beg_offset) |
3d0387c0 | 3862 | { |
68c45bf0 | 3863 | emacs_close (fd); |
a1d2b64a | 3864 | specpdl_ptr--; |
1051b3b3 | 3865 | /* Truncate the buffer to the size of the file. */ |
7dae4502 | 3866 | del_range_1 (same_at_start, same_at_end, 0, 0); |
3d0387c0 RS |
3867 | goto handled; |
3868 | } | |
3869 | immediate_quit = 1; | |
3870 | QUIT; | |
3871 | /* Count how many chars at the end of the file | |
6fdaa9a0 KH |
3872 | match the text at the end of the buffer. But, if we have |
3873 | already found that decoding is necessary, don't waste time. */ | |
3874 | while (!giveup_match_end) | |
3d0387c0 | 3875 | { |
728f8f0a PE |
3876 | int total_read, nread, bufpos, trial; |
3877 | off_t curpos; | |
3d0387c0 RS |
3878 | |
3879 | /* At what file position are we now scanning? */ | |
728f8f0a | 3880 | curpos = end_offset - (ZV_BYTE - same_at_end); |
fc81fa9e KH |
3881 | /* If the entire file matches the buffer tail, stop the scan. */ |
3882 | if (curpos == 0) | |
3883 | break; | |
3d0387c0 | 3884 | /* How much can we scan in the next step? */ |
2ef88fd4 | 3885 | trial = min (curpos, sizeof read_buf); |
728f8f0a | 3886 | if (lseek (fd, curpos - trial, SEEK_SET) < 0) |
3d0387c0 | 3887 | report_file_error ("Setting file position", |
b1d1b865 | 3888 | Fcons (orig_filename, Qnil)); |
3d0387c0 | 3889 | |
b02439c8 | 3890 | total_read = nread = 0; |
3d0387c0 RS |
3891 | while (total_read < trial) |
3892 | { | |
2ef88fd4 | 3893 | nread = emacs_read (fd, read_buf + total_read, trial - total_read); |
2bd2273e | 3894 | if (nread < 0) |
3d0387c0 | 3895 | error ("IO error reading %s: %s", |
d5db4077 | 3896 | SDATA (orig_filename), emacs_strerror (errno)); |
2bd2273e GM |
3897 | else if (nread == 0) |
3898 | break; | |
3d0387c0 RS |
3899 | total_read += nread; |
3900 | } | |
efdc16c9 | 3901 | |
8e6208c5 | 3902 | /* Scan this bufferful from the end, comparing with |
3d0387c0 RS |
3903 | the Emacs buffer. */ |
3904 | bufpos = total_read; | |
efdc16c9 | 3905 | |
3d0387c0 RS |
3906 | /* Compare with same_at_start to avoid counting some buffer text |
3907 | as matching both at the file's beginning and at the end. */ | |
3908 | while (bufpos > 0 && same_at_end > same_at_start | |
2ef88fd4 | 3909 | && FETCH_BYTE (same_at_end - 1) == read_buf[bufpos - 1]) |
3d0387c0 | 3910 | same_at_end--, bufpos--; |
727a0b4a | 3911 | |
3d0387c0 | 3912 | /* If we found a discrepancy, stop the scan. |
8e6208c5 | 3913 | Otherwise loop around and scan the preceding bufferful. */ |
3d0387c0 | 3914 | if (bufpos != 0) |
727a0b4a RS |
3915 | { |
3916 | /* If this discrepancy is because of code conversion, | |
3917 | we cannot use this method; giveup and try the other. */ | |
3918 | if (same_at_end > same_at_start | |
3919 | && FETCH_BYTE (same_at_end - 1) >= 0200 | |
4b4deea2 | 3920 | && ! NILP (BVAR (current_buffer, enable_multibyte_characters)) |
c8a6d68a | 3921 | && (CODING_MAY_REQUIRE_DECODING (&coding))) |
727a0b4a RS |
3922 | giveup_match_end = 1; |
3923 | break; | |
3924 | } | |
b02439c8 GM |
3925 | |
3926 | if (nread == 0) | |
3927 | break; | |
3d0387c0 RS |
3928 | } |
3929 | immediate_quit = 0; | |
9c28748f | 3930 | |
727a0b4a RS |
3931 | if (! giveup_match_end) |
3932 | { | |
d311d28c | 3933 | ptrdiff_t temp; |
ec7adf26 | 3934 | |
727a0b4a | 3935 | /* We win! We can handle REPLACE the optimized way. */ |
9c28748f | 3936 | |
20f6783d RS |
3937 | /* Extend the start of non-matching text area to multibyte |
3938 | character boundary. */ | |
4b4deea2 | 3939 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
20f6783d RS |
3940 | while (same_at_start > BEGV_BYTE |
3941 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
3942 | same_at_start--; | |
3943 | ||
3944 | /* Extend the end of non-matching text area to multibyte | |
71312b68 | 3945 | character boundary. */ |
4b4deea2 | 3946 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
ec7adf26 RS |
3947 | while (same_at_end < ZV_BYTE |
3948 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
71312b68 RS |
3949 | same_at_end++; |
3950 | ||
727a0b4a | 3951 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 | 3952 | overlap = (same_at_start - BEGV_BYTE |
728f8f0a PE |
3953 | - (same_at_end |
3954 | + (! NILP (end) ? end_offset : st.st_size) - ZV_BYTE)); | |
727a0b4a RS |
3955 | if (overlap > 0) |
3956 | same_at_end += overlap; | |
9c28748f | 3957 | |
727a0b4a | 3958 | /* Arrange to read only the nonmatching middle part of the file. */ |
728f8f0a PE |
3959 | beg_offset += same_at_start - BEGV_BYTE; |
3960 | end_offset -= ZV_BYTE - same_at_end; | |
3dbcf3f6 | 3961 | |
ec7adf26 | 3962 | del_range_byte (same_at_start, same_at_end, 0); |
727a0b4a | 3963 | /* Insert from the file at the proper position. */ |
ec7adf26 RS |
3964 | temp = BYTE_TO_CHAR (same_at_start); |
3965 | SET_PT_BOTH (temp, same_at_start); | |
727a0b4a RS |
3966 | |
3967 | /* If display currently starts at beginning of line, | |
3968 | keep it that way. */ | |
d3d50620 | 3969 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) |
c98ff5dd | 3970 | XWINDOW (selected_window)->start_at_line_beg = !NILP (Fbolp ()); |
727a0b4a RS |
3971 | |
3972 | replace_handled = 1; | |
3973 | } | |
3dbcf3f6 RS |
3974 | } |
3975 | ||
3976 | /* If requested, replace the accessible part of the buffer | |
3977 | with the file contents. Avoid replacing text at the | |
3978 | beginning or end of the buffer that matches the file contents; | |
3979 | that preserves markers pointing to the unchanged parts. | |
3980 | ||
3981 | Here we implement this feature for the case where code conversion | |
3982 | is needed, in a simple way that needs a lot of memory. | |
3983 | The preceding if-statement handles the case of no conversion | |
3984 | in a more optimized way. */ | |
f736ffbf | 3985 | if (!NILP (replace) && ! replace_handled && BEGV < ZV) |
3dbcf3f6 | 3986 | { |
d311d28c PE |
3987 | ptrdiff_t same_at_start = BEGV_BYTE; |
3988 | ptrdiff_t same_at_end = ZV_BYTE; | |
3989 | ptrdiff_t same_at_start_charpos; | |
3990 | ptrdiff_t inserted_chars; | |
3991 | ptrdiff_t overlap; | |
3992 | ptrdiff_t bufpos; | |
db327c7e | 3993 | unsigned char *decoded; |
d311d28c PE |
3994 | ptrdiff_t temp; |
3995 | ptrdiff_t this = 0; | |
3996 | ptrdiff_t this_count = SPECPDL_INDEX (); | |
f10fe38f PE |
3997 | bool multibyte |
3998 | = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); | |
5b359650 | 3999 | Lisp_Object conversion_buffer; |
83aca1cb | 4000 | struct gcpro gcpro1; |
db327c7e | 4001 | |
5b359650 | 4002 | conversion_buffer = code_conversion_save (1, multibyte); |
3dbcf3f6 RS |
4003 | |
4004 | /* First read the whole file, performing code conversion into | |
4005 | CONVERSION_BUFFER. */ | |
4006 | ||
728f8f0a | 4007 | if (lseek (fd, beg_offset, SEEK_SET) < 0) |
8f924df7 KH |
4008 | report_file_error ("Setting file position", |
4009 | Fcons (orig_filename, Qnil)); | |
727a0b4a | 4010 | |
3dbcf3f6 RS |
4011 | inserted = 0; /* Bytes put into CONVERSION_BUFFER so far. */ |
4012 | unprocessed = 0; /* Bytes not processed in previous loop. */ | |
4013 | ||
2ba48777 | 4014 | GCPRO1 (conversion_buffer); |
b41b8a7e | 4015 | while (1) |
3dbcf3f6 | 4016 | { |
b41b8a7e PE |
4017 | /* Read at most READ_BUF_SIZE bytes at a time, to allow |
4018 | quitting while reading a huge file. */ | |
3dbcf3f6 RS |
4019 | |
4020 | /* Allow quitting out of the actual I/O. */ | |
4021 | immediate_quit = 1; | |
4022 | QUIT; | |
b41b8a7e PE |
4023 | this = emacs_read (fd, read_buf + unprocessed, |
4024 | READ_BUF_SIZE - unprocessed); | |
3dbcf3f6 RS |
4025 | immediate_quit = 0; |
4026 | ||
db327c7e | 4027 | if (this <= 0) |
43aae36e | 4028 | break; |
3dbcf3f6 | 4029 | |
bf1c0f27 SM |
4030 | BUF_TEMP_SET_PT (XBUFFER (conversion_buffer), |
4031 | BUF_Z (XBUFFER (conversion_buffer))); | |
5976c3fe PE |
4032 | decode_coding_c_string (&coding, (unsigned char *) read_buf, |
4033 | unprocessed + this, conversion_buffer); | |
db327c7e KH |
4034 | unprocessed = coding.carryover_bytes; |
4035 | if (coding.carryover_bytes > 0) | |
72af86bd | 4036 | memcpy (read_buf, coding.carryover, unprocessed); |
3dbcf3f6 | 4037 | } |
2ba48777 | 4038 | UNGCPRO; |
db327c7e | 4039 | emacs_close (fd); |
3dbcf3f6 | 4040 | |
db65a627 CY |
4041 | /* We should remove the unwind_protect calling |
4042 | close_file_unwind, but other stuff has been added the stack, | |
4043 | so defer the removal till we reach the `handled' label. */ | |
4044 | deferred_remove_unwind_protect = 1; | |
4045 | ||
43aae36e | 4046 | if (this < 0) |
4ed925c6 MB |
4047 | error ("IO error reading %s: %s", |
4048 | SDATA (orig_filename), emacs_strerror (errno)); | |
3dbcf3f6 | 4049 | |
db327c7e KH |
4050 | if (unprocessed > 0) |
4051 | { | |
4052 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
5976c3fe PE |
4053 | decode_coding_c_string (&coding, (unsigned char *) read_buf, |
4054 | unprocessed, conversion_buffer); | |
db327c7e KH |
4055 | coding.mode &= ~CODING_MODE_LAST_BLOCK; |
4056 | } | |
4057 | ||
50b06221 | 4058 | coding_system = CODING_ID_NAME (coding.id); |
f6a07420 | 4059 | set_coding_system = 1; |
db327c7e | 4060 | decoded = BUF_BEG_ADDR (XBUFFER (conversion_buffer)); |
50342b35 KH |
4061 | inserted = (BUF_Z_BYTE (XBUFFER (conversion_buffer)) |
4062 | - BUF_BEG_BYTE (XBUFFER (conversion_buffer))); | |
db327c7e KH |
4063 | |
4064 | /* Compare the beginning of the converted string with the buffer | |
4065 | text. */ | |
3dbcf3f6 RS |
4066 | |
4067 | bufpos = 0; | |
4068 | while (bufpos < inserted && same_at_start < same_at_end | |
db327c7e | 4069 | && FETCH_BYTE (same_at_start) == decoded[bufpos]) |
3dbcf3f6 RS |
4070 | same_at_start++, bufpos++; |
4071 | ||
db327c7e | 4072 | /* If the file matches the head of buffer completely, |
3dbcf3f6 RS |
4073 | there's no need to replace anything. */ |
4074 | ||
4075 | if (bufpos == inserted) | |
4076 | { | |
3dbcf3f6 | 4077 | /* Truncate the buffer to the size of the file. */ |
18a9f8d9 SM |
4078 | if (same_at_start == same_at_end) |
4079 | nochange = 1; | |
4080 | else | |
4081 | del_range_byte (same_at_start, same_at_end, 0); | |
427f5aab | 4082 | inserted = 0; |
e8553dd1 KH |
4083 | |
4084 | unbind_to (this_count, Qnil); | |
3dbcf3f6 RS |
4085 | goto handled; |
4086 | } | |
4087 | ||
db327c7e KH |
4088 | /* Extend the start of non-matching text area to the previous |
4089 | multibyte character boundary. */ | |
4b4deea2 | 4090 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
20f6783d RS |
4091 | while (same_at_start > BEGV_BYTE |
4092 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
4093 | same_at_start--; | |
4094 | ||
3dbcf3f6 RS |
4095 | /* Scan this bufferful from the end, comparing with |
4096 | the Emacs buffer. */ | |
4097 | bufpos = inserted; | |
4098 | ||
4099 | /* Compare with same_at_start to avoid counting some buffer text | |
4100 | as matching both at the file's beginning and at the end. */ | |
4101 | while (bufpos > 0 && same_at_end > same_at_start | |
db327c7e | 4102 | && FETCH_BYTE (same_at_end - 1) == decoded[bufpos - 1]) |
3dbcf3f6 RS |
4103 | same_at_end--, bufpos--; |
4104 | ||
db327c7e KH |
4105 | /* Extend the end of non-matching text area to the next |
4106 | multibyte character boundary. */ | |
4b4deea2 | 4107 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
20f6783d RS |
4108 | while (same_at_end < ZV_BYTE |
4109 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
4110 | same_at_end++; | |
4111 | ||
3dbcf3f6 | 4112 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 | 4113 | overlap = same_at_start - BEGV_BYTE - (same_at_end + inserted - ZV_BYTE); |
3dbcf3f6 RS |
4114 | if (overlap > 0) |
4115 | same_at_end += overlap; | |
4116 | ||
727a0b4a RS |
4117 | /* If display currently starts at beginning of line, |
4118 | keep it that way. */ | |
d3d50620 | 4119 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) |
c98ff5dd | 4120 | XWINDOW (selected_window)->start_at_line_beg = !NILP (Fbolp ()); |
727a0b4a | 4121 | |
3dbcf3f6 RS |
4122 | /* Replace the chars that we need to replace, |
4123 | and update INSERTED to equal the number of bytes | |
db327c7e | 4124 | we are taking from the decoded string. */ |
4b70e2c9 | 4125 | inserted -= (ZV_BYTE - same_at_end) + (same_at_start - BEGV_BYTE); |
427f5aab | 4126 | |
643c73b9 | 4127 | if (same_at_end != same_at_start) |
427f5aab KH |
4128 | { |
4129 | del_range_byte (same_at_start, same_at_end, 0); | |
4130 | temp = GPT; | |
4131 | same_at_start = GPT_BYTE; | |
4132 | } | |
643c73b9 RS |
4133 | else |
4134 | { | |
643c73b9 | 4135 | temp = BYTE_TO_CHAR (same_at_start); |
643c73b9 | 4136 | } |
427f5aab KH |
4137 | /* Insert from the file at the proper position. */ |
4138 | SET_PT_BOTH (temp, same_at_start); | |
50342b35 KH |
4139 | same_at_start_charpos |
4140 | = buf_bytepos_to_charpos (XBUFFER (conversion_buffer), | |
7f5d2c72 SM |
4141 | same_at_start - BEGV_BYTE |
4142 | + BUF_BEG_BYTE (XBUFFER (conversion_buffer))); | |
50342b35 KH |
4143 | inserted_chars |
4144 | = (buf_bytepos_to_charpos (XBUFFER (conversion_buffer), | |
7f5d2c72 SM |
4145 | same_at_start + inserted - BEGV_BYTE |
4146 | + BUF_BEG_BYTE (XBUFFER (conversion_buffer))) | |
50342b35 | 4147 | - same_at_start_charpos); |
d07af40d KH |
4148 | /* This binding is to avoid ask-user-about-supersession-threat |
4149 | being called in insert_from_buffer (via in | |
4150 | prepare_to_modify_buffer). */ | |
4151 | specbind (intern ("buffer-file-name"), Qnil); | |
db327c7e | 4152 | insert_from_buffer (XBUFFER (conversion_buffer), |
50342b35 | 4153 | same_at_start_charpos, inserted_chars, 0); |
427f5aab KH |
4154 | /* Set `inserted' to the number of inserted characters. */ |
4155 | inserted = PT - temp; | |
77343e1d KH |
4156 | /* Set point before the inserted characters. */ |
4157 | SET_PT_BOTH (temp, same_at_start); | |
3dbcf3f6 | 4158 | |
db327c7e | 4159 | unbind_to (this_count, Qnil); |
3dbcf3f6 | 4160 | |
3dbcf3f6 | 4161 | goto handled; |
3d0387c0 RS |
4162 | } |
4163 | ||
d4b8687b | 4164 | if (! not_regular) |
728f8f0a | 4165 | total = end_offset - beg_offset; |
d4b8687b RS |
4166 | else |
4167 | /* For a special file, all we can do is guess. */ | |
4168 | total = READ_BUF_SIZE; | |
570d7624 | 4169 | |
8b913b57 | 4170 | if (NILP (visit) && total > 0) |
68780e2a RS |
4171 | { |
4172 | #ifdef CLASH_DETECTION | |
4b4deea2 | 4173 | if (!NILP (BVAR (current_buffer, file_truename)) |
68780e2a | 4174 | /* Make binding buffer-file-name to nil effective. */ |
4b4deea2 | 4175 | && !NILP (BVAR (current_buffer, filename)) |
68780e2a RS |
4176 | && SAVE_MODIFF >= MODIFF) |
4177 | we_locked_file = 1; | |
4178 | #endif /* CLASH_DETECTION */ | |
4179 | prepare_to_modify_buffer (GPT, GPT, NULL); | |
4180 | } | |
570d7624 | 4181 | |
13002885 | 4182 | move_gap_both (PT, PT_BYTE); |
7fded690 JB |
4183 | if (GAP_SIZE < total) |
4184 | make_gap (total - GAP_SIZE); | |
4185 | ||
728f8f0a | 4186 | if (beg_offset != 0 || !NILP (replace)) |
7fded690 | 4187 | { |
728f8f0a | 4188 | if (lseek (fd, beg_offset, SEEK_SET) < 0) |
b1d1b865 RS |
4189 | report_file_error ("Setting file position", |
4190 | Fcons (orig_filename, Qnil)); | |
7fded690 JB |
4191 | } |
4192 | ||
2ef88fd4 DA |
4193 | /* In the following loop, HOW_MUCH contains the total bytes read so |
4194 | far for a regular file, and not changed for a special file. But, | |
4195 | before exiting the loop, it is set to a negative value if I/O | |
4196 | error occurs. */ | |
a1d2b64a | 4197 | how_much = 0; |
efdc16c9 | 4198 | |
6fdaa9a0 KH |
4199 | /* Total bytes inserted. */ |
4200 | inserted = 0; | |
efdc16c9 | 4201 | |
2ef88fd4 DA |
4202 | /* Here, we don't do code conversion in the loop. It is done by |
4203 | decode_coding_gap after all data are read into the buffer. */ | |
4204 | { | |
4205 | ptrdiff_t gap_size = GAP_SIZE; | |
1b978129 | 4206 | |
2ef88fd4 DA |
4207 | while (how_much < total) |
4208 | { | |
4209 | /* try is reserved in some compilers (Microsoft C) */ | |
4210 | ptrdiff_t trytry = min (total - how_much, READ_BUF_SIZE); | |
4211 | ptrdiff_t this; | |
1b978129 | 4212 | |
2ef88fd4 DA |
4213 | if (not_regular) |
4214 | { | |
4215 | Lisp_Object nbytes; | |
4216 | ||
4217 | /* Maybe make more room. */ | |
4218 | if (gap_size < trytry) | |
4219 | { | |
4220 | make_gap (trytry - gap_size); | |
4221 | gap_size = GAP_SIZE - inserted; | |
4222 | } | |
4223 | ||
4224 | /* Read from the file, capturing `quit'. When an | |
4225 | error occurs, end the loop, and arrange for a quit | |
4226 | to be signaled after decoding the text we read. */ | |
4227 | nbytes = internal_condition_case_1 | |
4228 | (read_non_regular, | |
4229 | make_save_value ("iii", (ptrdiff_t) fd, inserted, trytry), | |
4230 | Qerror, read_non_regular_quit); | |
4231 | ||
4232 | if (NILP (nbytes)) | |
4233 | { | |
4234 | read_quit = 1; | |
4235 | break; | |
4236 | } | |
4237 | ||
4238 | this = XINT (nbytes); | |
4239 | } | |
4240 | else | |
4241 | { | |
4242 | /* Allow quitting out of the actual I/O. We don't make text | |
4243 | part of the buffer until all the reading is done, so a C-g | |
4244 | here doesn't do any harm. */ | |
4245 | immediate_quit = 1; | |
4246 | QUIT; | |
4247 | this = emacs_read (fd, | |
4248 | ((char *) BEG_ADDR + PT_BYTE - BEG_BYTE | |
4249 | + inserted), | |
4250 | trytry); | |
4251 | immediate_quit = 0; | |
4252 | } | |
4253 | ||
4254 | if (this <= 0) | |
4255 | { | |
4256 | how_much = this; | |
4257 | break; | |
4258 | } | |
4259 | ||
4260 | gap_size -= this; | |
4261 | ||
4262 | /* For a regular file, where TOTAL is the real size, | |
4263 | count HOW_MUCH to compare with it. | |
4264 | For a special file, where TOTAL is just a buffer size, | |
4265 | so don't bother counting in HOW_MUCH. | |
4266 | (INSERTED is where we count the number of characters inserted.) */ | |
4267 | if (! not_regular) | |
4268 | how_much += this; | |
4269 | inserted += this; | |
4270 | } | |
4271 | } | |
1b978129 | 4272 | |
e07469fa DA |
4273 | /* Now we have either read all the file data into the gap, |
4274 | or stop reading on I/O error or quit. If nothing was | |
4275 | read, undo marking the buffer modified. */ | |
68780e2a RS |
4276 | |
4277 | if (inserted == 0) | |
4278 | { | |
6840d350 | 4279 | #ifdef CLASH_DETECTION |
68780e2a | 4280 | if (we_locked_file) |
4b4deea2 | 4281 | unlock_file (BVAR (current_buffer, file_truename)); |
6840d350 | 4282 | #endif |
68780e2a RS |
4283 | Vdeactivate_mark = old_Vdeactivate_mark; |
4284 | } | |
83c1cf6d RS |
4285 | else |
4286 | Vdeactivate_mark = Qt; | |
68780e2a | 4287 | |
68c45bf0 | 4288 | emacs_close (fd); |
6fdaa9a0 | 4289 | |
c8a6d68a KH |
4290 | /* Discard the unwind protect for closing the file. */ |
4291 | specpdl_ptr--; | |
6fdaa9a0 | 4292 | |
2ef88fd4 DA |
4293 | if (how_much < 0) |
4294 | error ("IO error reading %s: %s", | |
4295 | SDATA (orig_filename), emacs_strerror (errno)); | |
4296 | ||
4297 | /* Make the text read part of the buffer. */ | |
4298 | GAP_SIZE -= inserted; | |
4299 | GPT += inserted; | |
4300 | GPT_BYTE += inserted; | |
4301 | ZV += inserted; | |
4302 | ZV_BYTE += inserted; | |
4303 | Z += inserted; | |
4304 | Z_BYTE += inserted; | |
4305 | ||
4306 | if (GAP_SIZE > 0) | |
4307 | /* Put an anchor to ensure multi-byte form ends at gap. */ | |
4308 | *GPT_ADDR = 0; | |
4309 | ||
f8569325 DL |
4310 | notfound: |
4311 | ||
db327c7e | 4312 | if (NILP (coding_system)) |
c8a6d68a | 4313 | { |
2df42e09 | 4314 | /* The coding system is not yet decided. Decide it by an |
dfe35e7b RS |
4315 | optimized method for handling `coding:' tag. |
4316 | ||
4317 | Note that we can get here only if the buffer was empty | |
4318 | before the insertion. */ | |
f736ffbf | 4319 | |
2df42e09 | 4320 | if (!NILP (Vcoding_system_for_read)) |
db327c7e | 4321 | coding_system = Vcoding_system_for_read; |
2df42e09 KH |
4322 | else |
4323 | { | |
98a7d268 KH |
4324 | /* Since we are sure that the current buffer was empty |
4325 | before the insertion, we can toggle | |
4326 | enable-multibyte-characters directly here without taking | |
9a7f80aa KH |
4327 | care of marker adjustment. By this way, we can run Lisp |
4328 | program safely before decoding the inserted text. */ | |
98a7d268 | 4329 | Lisp_Object unwind_data; |
d311d28c | 4330 | ptrdiff_t count1 = SPECPDL_INDEX (); |
2df42e09 | 4331 | |
4b4deea2 TT |
4332 | unwind_data = Fcons (BVAR (current_buffer, enable_multibyte_characters), |
4333 | Fcons (BVAR (current_buffer, undo_list), | |
98a7d268 | 4334 | Fcurrent_buffer ())); |
39eb03f1 PE |
4335 | bset_enable_multibyte_characters (current_buffer, Qnil); |
4336 | bset_undo_list (current_buffer, Qt); | |
98a7d268 KH |
4337 | record_unwind_protect (decide_coding_unwind, unwind_data); |
4338 | ||
4339 | if (inserted > 0 && ! NILP (Vset_auto_coding_function)) | |
4340 | { | |
db327c7e | 4341 | coding_system = call2 (Vset_auto_coding_function, |
8f924df7 | 4342 | filename, make_number (inserted)); |
2df42e09 | 4343 | } |
f736ffbf | 4344 | |
db327c7e | 4345 | if (NILP (coding_system)) |
2df42e09 KH |
4346 | { |
4347 | /* If the coding system is not yet decided, check | |
4348 | file-coding-system-alist. */ | |
8f924df7 | 4349 | Lisp_Object args[6]; |
f736ffbf | 4350 | |
2df42e09 KH |
4351 | args[0] = Qinsert_file_contents, args[1] = orig_filename; |
4352 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = Qnil; | |
8f924df7 KH |
4353 | coding_system = Ffind_operation_coding_system (6, args); |
4354 | if (CONSP (coding_system)) | |
4355 | coding_system = XCAR (coding_system); | |
f736ffbf | 4356 | } |
f839df0c | 4357 | unbind_to (count1, Qnil); |
98a7d268 | 4358 | inserted = Z_BYTE - BEG_BYTE; |
2df42e09 | 4359 | } |
f736ffbf | 4360 | |
db327c7e KH |
4361 | if (NILP (coding_system)) |
4362 | coding_system = Qundecided; | |
4363 | else | |
4364 | CHECK_CODING_SYSTEM (coding_system); | |
f736ffbf | 4365 | |
4b4deea2 | 4366 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
237a6fd2 | 4367 | /* We must suppress all character code conversion except for |
2df42e09 | 4368 | end-of-line conversion. */ |
db327c7e | 4369 | coding_system = raw_text_coding_system (coding_system); |
db327c7e KH |
4370 | setup_coding_system (coding_system, &coding); |
4371 | /* Ensure we set Vlast_coding_system_used. */ | |
4372 | set_coding_system = 1; | |
2df42e09 | 4373 | } |
f736ffbf | 4374 | |
db327c7e | 4375 | if (!NILP (visit)) |
8c3b9441 | 4376 | { |
db327c7e | 4377 | /* When we visit a file by raw-text, we change the buffer to |
9a7f80aa | 4378 | unibyte. */ |
db327c7e KH |
4379 | if (CODING_FOR_UNIBYTE (&coding) |
4380 | /* Can't do this if part of the buffer might be preserved. */ | |
4381 | && NILP (replace)) | |
4382 | /* Visiting a file with these coding system makes the buffer | |
4383 | unibyte. */ | |
39eb03f1 | 4384 | bset_enable_multibyte_characters (current_buffer, Qnil); |
8c3b9441 KH |
4385 | } |
4386 | ||
4b4deea2 | 4387 | coding.dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
5b359650 | 4388 | if (CODING_MAY_REQUIRE_DECODING (&coding) |
1c157f8d | 4389 | && (inserted > 0 || CODING_REQUIRE_FLUSHING (&coding))) |
2df42e09 | 4390 | { |
db327c7e KH |
4391 | move_gap_both (PT, PT_BYTE); |
4392 | GAP_SIZE += inserted; | |
4393 | ZV_BYTE -= inserted; | |
4394 | Z_BYTE -= inserted; | |
4395 | ZV -= inserted; | |
4396 | Z -= inserted; | |
4397 | decode_coding_gap (&coding, inserted, inserted); | |
4398 | inserted = coding.produced_char; | |
5b359650 | 4399 | coding_system = CODING_ID_NAME (coding.id); |
2df42e09 | 4400 | } |
db327c7e KH |
4401 | else if (inserted > 0) |
4402 | adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted, | |
4403 | inserted); | |
570d7624 | 4404 | |
0068070e SM |
4405 | /* Call after-change hooks for the inserted text, aside from the case |
4406 | of normal visiting (not with REPLACE), which is done in a new buffer | |
4407 | "before" the buffer is changed. */ | |
4408 | if (inserted > 0 && total > 0 | |
4409 | && (NILP (visit) || !NILP (replace))) | |
4410 | { | |
4411 | signal_after_change (PT, 0, inserted); | |
4412 | update_compositions (PT, PT, CHECK_BORDER); | |
4413 | } | |
4414 | ||
cf6d2357 RS |
4415 | /* Now INSERTED is measured in characters. */ |
4416 | ||
32f4334d | 4417 | handled: |
570d7624 | 4418 | |
db65a627 CY |
4419 | if (deferred_remove_unwind_protect) |
4420 | /* If requested above, discard the unwind protect for closing the | |
4421 | file. */ | |
4422 | specpdl_ptr--; | |
4423 | ||
265a9e55 | 4424 | if (!NILP (visit)) |
570d7624 | 4425 | { |
4b4deea2 | 4426 | if (!EQ (BVAR (current_buffer, undo_list), Qt) && !nochange) |
39eb03f1 | 4427 | bset_undo_list (current_buffer, Qnil); |
62bcf009 | 4428 | |
a7e82472 RS |
4429 | if (NILP (handler)) |
4430 | { | |
dbeed9a6 | 4431 | current_buffer->modtime = mtime; |
58b963f7 | 4432 | current_buffer->modtime_size = st.st_size; |
39eb03f1 | 4433 | bset_filename (current_buffer, orig_filename); |
a7e82472 | 4434 | } |
62bcf009 | 4435 | |
95385625 | 4436 | SAVE_MODIFF = MODIFF; |
0b5397c2 | 4437 | BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF; |
4b4deea2 | 4438 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
570d7624 | 4439 | #ifdef CLASH_DETECTION |
32f4334d RS |
4440 | if (NILP (handler)) |
4441 | { | |
4b4deea2 TT |
4442 | if (!NILP (BVAR (current_buffer, file_truename))) |
4443 | unlock_file (BVAR (current_buffer, file_truename)); | |
32f4334d RS |
4444 | unlock_file (filename); |
4445 | } | |
570d7624 | 4446 | #endif /* CLASH_DETECTION */ |
330bfe57 | 4447 | if (not_regular) |
24b1ddad KS |
4448 | xsignal2 (Qfile_error, |
4449 | build_string ("not a regular file"), orig_filename); | |
570d7624 JB |
4450 | } |
4451 | ||
b6426b03 | 4452 | if (set_coding_system) |
8f924df7 | 4453 | Vlast_coding_system_used = coding_system; |
b6426b03 | 4454 | |
2080470e | 4455 | if (! NILP (Ffboundp (Qafter_insert_file_set_coding))) |
b6426b03 | 4456 | { |
37a3c774 KH |
4457 | insval = call2 (Qafter_insert_file_set_coding, make_number (inserted), |
4458 | visit); | |
b6426b03 KH |
4459 | if (! NILP (insval)) |
4460 | { | |
d311d28c PE |
4461 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4462 | wrong_type_argument (intern ("inserted-chars"), insval); | |
b6426b03 KH |
4463 | inserted = XFASTINT (insval); |
4464 | } | |
4465 | } | |
4466 | ||
6420e80c | 4467 | /* Decode file format. */ |
c8a6d68a | 4468 | if (inserted > 0) |
0d420e88 | 4469 | { |
6420e80c | 4470 | /* Don't run point motion or modification hooks when decoding. */ |
d311d28c PE |
4471 | ptrdiff_t count1 = SPECPDL_INDEX (); |
4472 | ptrdiff_t old_inserted = inserted; | |
6f2528d8 MR |
4473 | specbind (Qinhibit_point_motion_hooks, Qt); |
4474 | specbind (Qinhibit_modification_hooks, Qt); | |
4475 | ||
6420e80c | 4476 | /* Save old undo list and don't record undo for decoding. */ |
4b4deea2 | 4477 | old_undo = BVAR (current_buffer, undo_list); |
39eb03f1 | 4478 | bset_undo_list (current_buffer, Qt); |
efdc16c9 | 4479 | |
6f2528d8 | 4480 | if (NILP (replace)) |
ed8e506f | 4481 | { |
6f2528d8 MR |
4482 | insval = call3 (Qformat_decode, |
4483 | Qnil, make_number (inserted), visit); | |
d311d28c PE |
4484 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4485 | wrong_type_argument (intern ("inserted-chars"), insval); | |
6f2528d8 MR |
4486 | inserted = XFASTINT (insval); |
4487 | } | |
4488 | else | |
4489 | { | |
4490 | /* If REPLACE is non-nil and we succeeded in not replacing the | |
6420e80c AS |
4491 | beginning or end of the buffer text with the file's contents, |
4492 | call format-decode with `point' positioned at the beginning | |
fa463103 | 4493 | of the buffer and `inserted' equaling the number of |
6420e80c AS |
4494 | characters in the buffer. Otherwise, format-decode might |
4495 | fail to correctly analyze the beginning or end of the buffer. | |
4496 | Hence we temporarily save `point' and `inserted' here and | |
4497 | restore `point' iff format-decode did not insert or delete | |
4498 | any text. Otherwise we leave `point' at point-min. */ | |
d311d28c PE |
4499 | ptrdiff_t opoint = PT; |
4500 | ptrdiff_t opoint_byte = PT_BYTE; | |
4501 | ptrdiff_t oinserted = ZV - BEGV; | |
4502 | EMACS_INT ochars_modiff = CHARS_MODIFF; | |
1f163f28 MA |
4503 | |
4504 | TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE); | |
6f2528d8 MR |
4505 | insval = call3 (Qformat_decode, |
4506 | Qnil, make_number (oinserted), visit); | |
d311d28c PE |
4507 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4508 | wrong_type_argument (intern ("inserted-chars"), insval); | |
cac4219c MR |
4509 | if (ochars_modiff == CHARS_MODIFF) |
4510 | /* format_decode didn't modify buffer's characters => move | |
4511 | point back to position before inserted text and leave | |
6420e80c | 4512 | value of inserted alone. */ |
6f2528d8 | 4513 | SET_PT_BOTH (opoint, opoint_byte); |
cac4219c MR |
4514 | else |
4515 | /* format_decode modified buffer's characters => consider | |
6420e80c | 4516 | entire buffer changed and leave point at point-min. */ |
cac4219c | 4517 | inserted = XFASTINT (insval); |
ed8e506f | 4518 | } |
efdc16c9 | 4519 | |
6f2528d8 | 4520 | /* For consistency with format-decode call these now iff inserted > 0 |
6420e80c | 4521 | (martin 2007-06-28). */ |
6f2528d8 MR |
4522 | p = Vafter_insert_file_functions; |
4523 | while (CONSP (p)) | |
4524 | { | |
4525 | if (NILP (replace)) | |
4526 | { | |
4527 | insval = call1 (XCAR (p), make_number (inserted)); | |
4528 | if (!NILP (insval)) | |
4529 | { | |
d311d28c PE |
4530 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4531 | wrong_type_argument (intern ("inserted-chars"), insval); | |
6f2528d8 MR |
4532 | inserted = XFASTINT (insval); |
4533 | } | |
4534 | } | |
4535 | else | |
4536 | { | |
6420e80c AS |
4537 | /* For the rationale of this see the comment on |
4538 | format-decode above. */ | |
d311d28c PE |
4539 | ptrdiff_t opoint = PT; |
4540 | ptrdiff_t opoint_byte = PT_BYTE; | |
4541 | ptrdiff_t oinserted = ZV - BEGV; | |
4542 | EMACS_INT ochars_modiff = CHARS_MODIFF; | |
1f163f28 | 4543 | |
6f2528d8 MR |
4544 | TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE); |
4545 | insval = call1 (XCAR (p), make_number (oinserted)); | |
4546 | if (!NILP (insval)) | |
4547 | { | |
d311d28c PE |
4548 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4549 | wrong_type_argument (intern ("inserted-chars"), insval); | |
cac4219c MR |
4550 | if (ochars_modiff == CHARS_MODIFF) |
4551 | /* after_insert_file_functions didn't modify | |
4552 | buffer's characters => move point back to | |
4553 | position before inserted text and leave value of | |
6420e80c | 4554 | inserted alone. */ |
6f2528d8 | 4555 | SET_PT_BOTH (opoint, opoint_byte); |
cac4219c MR |
4556 | else |
4557 | /* after_insert_file_functions did modify buffer's | |
4558 | characters => consider entire buffer changed and | |
6420e80c | 4559 | leave point at point-min. */ |
cac4219c | 4560 | inserted = XFASTINT (insval); |
6f2528d8 MR |
4561 | } |
4562 | } | |
4563 | ||
4564 | QUIT; | |
4565 | p = XCDR (p); | |
ed8e506f | 4566 | } |
efdc16c9 | 4567 | |
6f2528d8 MR |
4568 | if (NILP (visit)) |
4569 | { | |
39eb03f1 | 4570 | bset_undo_list (current_buffer, old_undo); |
6420e80c | 4571 | if (CONSP (old_undo) && inserted != old_inserted) |
6f2528d8 | 4572 | { |
6420e80c AS |
4573 | /* Adjust the last undo record for the size change during |
4574 | the format conversion. */ | |
6f2528d8 | 4575 | Lisp_Object tem = XCAR (old_undo); |
6420e80c AS |
4576 | if (CONSP (tem) && INTEGERP (XCAR (tem)) |
4577 | && INTEGERP (XCDR (tem)) | |
4578 | && XFASTINT (XCDR (tem)) == PT + old_inserted) | |
4579 | XSETCDR (tem, make_number (PT + inserted)); | |
6f2528d8 MR |
4580 | } |
4581 | } | |
6f2528d8 | 4582 | else |
1bc99c9c | 4583 | /* If undo_list was Qt before, keep it that way. |
6420e80c | 4584 | Otherwise start with an empty undo_list. */ |
39eb03f1 | 4585 | bset_undo_list (current_buffer, EQ (old_undo, Qt) ? Qt : Qnil); |
efdc16c9 | 4586 | |
f839df0c | 4587 | unbind_to (count1, Qnil); |
0d420e88 BG |
4588 | } |
4589 | ||
f8569325 | 4590 | if (!NILP (visit) |
d35af63c | 4591 | && EMACS_NSECS (current_buffer->modtime) == NONEXISTENT_MODTIME_NSECS) |
f8569325 DL |
4592 | { |
4593 | /* If visiting nonexistent file, return nil. */ | |
e5e9d610 | 4594 | errno = save_errno; |
f8569325 | 4595 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
d6a3cc15 RS |
4596 | } |
4597 | ||
1b978129 GM |
4598 | if (read_quit) |
4599 | Fsignal (Qquit, Qnil); | |
4600 | ||
2ef88fd4 | 4601 | /* Retval needs to be dealt with in all cases consistently. */ |
a1d2b64a | 4602 | if (NILP (val)) |
e07469fa | 4603 | val = list2 (orig_filename, make_number (inserted)); |
a1d2b64a RS |
4604 | |
4605 | RETURN_UNGCPRO (unbind_to (count, val)); | |
570d7624 | 4606 | } |
7fded690 | 4607 | \f |
f57e2426 | 4608 | static Lisp_Object build_annotations (Lisp_Object, Lisp_Object); |
d6a3cc15 | 4609 | |
199607e4 | 4610 | static Lisp_Object |
971de7fb | 4611 | build_annotations_unwind (Lisp_Object arg) |
6fc6f94b | 4612 | { |
67fbc0cb | 4613 | Vwrite_region_annotation_buffers = arg; |
6fc6f94b RS |
4614 | return Qnil; |
4615 | } | |
4616 | ||
7c82a4a9 SM |
4617 | /* Decide the coding-system to encode the data with. */ |
4618 | ||
9dbda100 GM |
4619 | DEFUN ("choose-write-coding-system", Fchoose_write_coding_system, |
4620 | Schoose_write_coding_system, 3, 6, 0, | |
4621 | doc: /* Choose the coding system for writing a file. | |
4622 | Arguments are as for `write-region'. | |
4623 | This function is for internal use only. It may prompt the user. */ ) | |
4624 | (Lisp_Object start, Lisp_Object end, Lisp_Object filename, | |
4625 | Lisp_Object append, Lisp_Object visit, Lisp_Object lockname) | |
7c82a4a9 SM |
4626 | { |
4627 | Lisp_Object val; | |
75421805 | 4628 | Lisp_Object eol_parent = Qnil; |
7c82a4a9 | 4629 | |
9dbda100 GM |
4630 | /* Mimic write-region behavior. */ |
4631 | if (NILP (start)) | |
4632 | { | |
4633 | XSETFASTINT (start, BEGV); | |
4634 | XSETFASTINT (end, ZV); | |
4635 | } | |
4636 | ||
6b61353c | 4637 | if (auto_saving |
4b4deea2 TT |
4638 | && NILP (Fstring_equal (BVAR (current_buffer, filename), |
4639 | BVAR (current_buffer, auto_save_file_name)))) | |
75421805 KH |
4640 | { |
4641 | val = Qutf_8_emacs; | |
4642 | eol_parent = Qunix; | |
4643 | } | |
7c82a4a9 | 4644 | else if (!NILP (Vcoding_system_for_write)) |
42b01e1e KH |
4645 | { |
4646 | val = Vcoding_system_for_write; | |
4647 | if (coding_system_require_warning | |
4648 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4649 | /* Confirm that VAL can surely encode the current region. */ | |
4650 | val = call5 (Vselect_safe_coding_system_function, | |
4651 | start, end, Fcons (Qt, Fcons (val, Qnil)), | |
4652 | Qnil, filename); | |
4653 | } | |
7c82a4a9 SM |
4654 | else |
4655 | { | |
4656 | /* If the variable `buffer-file-coding-system' is set locally, | |
4657 | it means that the file was read with some kind of code | |
4658 | conversion or the variable is explicitly set by users. We | |
4659 | had better write it out with the same coding system even if | |
4660 | `enable-multibyte-characters' is nil. | |
4661 | ||
4662 | If it is not set locally, we anyway have to convert EOL | |
4663 | format if the default value of `buffer-file-coding-system' | |
4664 | tells that it is not Unix-like (LF only) format. */ | |
f75d7a91 PE |
4665 | bool using_default_coding = 0; |
4666 | bool force_raw_text = 0; | |
7c82a4a9 | 4667 | |
4b4deea2 | 4668 | val = BVAR (current_buffer, buffer_file_coding_system); |
7c82a4a9 SM |
4669 | if (NILP (val) |
4670 | || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil))) | |
4671 | { | |
4672 | val = Qnil; | |
4b4deea2 | 4673 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
7c82a4a9 SM |
4674 | force_raw_text = 1; |
4675 | } | |
efdc16c9 | 4676 | |
7c82a4a9 SM |
4677 | if (NILP (val)) |
4678 | { | |
4679 | /* Check file-coding-system-alist. */ | |
4680 | Lisp_Object args[7], coding_systems; | |
4681 | ||
4682 | args[0] = Qwrite_region; args[1] = start; args[2] = end; | |
4683 | args[3] = filename; args[4] = append; args[5] = visit; | |
4684 | args[6] = lockname; | |
4685 | coding_systems = Ffind_operation_coding_system (7, args); | |
4686 | if (CONSP (coding_systems) && !NILP (XCDR (coding_systems))) | |
4687 | val = XCDR (coding_systems); | |
4688 | } | |
4689 | ||
c934586d | 4690 | if (NILP (val)) |
7c82a4a9 SM |
4691 | { |
4692 | /* If we still have not decided a coding system, use the | |
4693 | default value of buffer-file-coding-system. */ | |
4b4deea2 | 4694 | val = BVAR (current_buffer, buffer_file_coding_system); |
7c82a4a9 SM |
4695 | using_default_coding = 1; |
4696 | } | |
efdc16c9 | 4697 | |
db327c7e KH |
4698 | if (! NILP (val) && ! force_raw_text) |
4699 | { | |
4700 | Lisp_Object spec, attrs; | |
4701 | ||
4702 | CHECK_CODING_SYSTEM_GET_SPEC (val, spec); | |
4703 | attrs = AREF (spec, 0); | |
4704 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
4705 | force_raw_text = 1; | |
4706 | } | |
4707 | ||
7c82a4a9 SM |
4708 | if (!force_raw_text |
4709 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4710 | /* Confirm that VAL can surely encode the current region. */ | |
905a4276 PJ |
4711 | val = call5 (Vselect_safe_coding_system_function, |
4712 | start, end, val, Qnil, filename); | |
7c82a4a9 | 4713 | |
db327c7e KH |
4714 | /* If the decided coding-system doesn't specify end-of-line |
4715 | format, we use that of | |
4716 | `default-buffer-file-coding-system'. */ | |
c934586d | 4717 | if (! using_default_coding |
4b4deea2 | 4718 | && ! NILP (BVAR (&buffer_defaults, buffer_file_coding_system))) |
db327c7e | 4719 | val = (coding_inherit_eol_type |
4b4deea2 | 4720 | (val, BVAR (&buffer_defaults, buffer_file_coding_system))); |
7c82a4a9 | 4721 | |
db327c7e KH |
4722 | /* If we decide not to encode text, use `raw-text' or one of its |
4723 | subsidiaries. */ | |
7c82a4a9 | 4724 | if (force_raw_text) |
db327c7e | 4725 | val = raw_text_coding_system (val); |
7c82a4a9 SM |
4726 | } |
4727 | ||
75421805 | 4728 | val = coding_inherit_eol_type (val, eol_parent); |
c934586d | 4729 | return val; |
7c82a4a9 SM |
4730 | } |
4731 | ||
a7ca3326 | 4732 | DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 7, |
8c1a1077 PJ |
4733 | "r\nFWrite region to file: \ni\ni\ni\np", |
4734 | doc: /* Write current region into specified file. | |
c2efea25 RS |
4735 | When called from a program, requires three arguments: |
4736 | START, END and FILENAME. START and END are normally buffer positions | |
4737 | specifying the part of the buffer to write. | |
4738 | If START is nil, that means to use the entire buffer contents. | |
4739 | If START is a string, then output that string to the file | |
4740 | instead of any buffer contents; END is ignored. | |
4741 | ||
8c1a1077 | 4742 | Optional fourth argument APPEND if non-nil means |
b3fbb395 | 4743 | append to existing file contents (if any). If it is a number, |
8c1a1077 | 4744 | seek to that offset in the file before writing. |
36e50520 | 4745 | Optional fifth argument VISIT, if t or a string, means |
8c1a1077 PJ |
4746 | set the last-save-file-modtime of buffer to this file's modtime |
4747 | and mark buffer not modified. | |
4748 | If VISIT is a string, it is a second file name; | |
4749 | the output goes to FILENAME, but the buffer is marked as visiting VISIT. | |
4750 | VISIT is also the file name to lock and unlock for clash detection. | |
4751 | If VISIT is neither t nor nil nor a string, | |
5f4e6aa9 | 4752 | that means do not display the \"Wrote file\" message. |
8c1a1077 PJ |
4753 | The optional sixth arg LOCKNAME, if non-nil, specifies the name to |
4754 | use for locking and unlocking, overriding FILENAME and VISIT. | |
4755 | The optional seventh arg MUSTBENEW, if non-nil, insists on a check | |
4756 | for an existing file with the same name. If MUSTBENEW is `excl', | |
4757 | that means to get an error if the file already exists; never overwrite. | |
4758 | If MUSTBENEW is neither nil nor `excl', that means ask for | |
4759 | confirmation before overwriting, but do go ahead and overwrite the file | |
4760 | if the user confirms. | |
8c1a1077 PJ |
4761 | |
4762 | This does code conversion according to the value of | |
4763 | `coding-system-for-write', `buffer-file-coding-system', or | |
4764 | `file-coding-system-alist', and sets the variable | |
aacd8ba1 GM |
4765 | `last-coding-system-used' to the coding system actually used. |
4766 | ||
4767 | This calls `write-region-annotate-functions' at the start, and | |
4768 | `write-region-post-annotation-function' at the end. */) | |
5842a27b | 4769 | (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew) |
570d7624 | 4770 | { |
f75d7a91 | 4771 | int desc; |
3a955a1f PE |
4772 | int open_flags; |
4773 | int mode; | |
4774 | off_t offset IF_LINT (= 0); | |
f75d7a91 | 4775 | bool ok; |
6bbd7a29 | 4776 | int save_errno = 0; |
5976c3fe | 4777 | const char *fn; |
570d7624 | 4778 | struct stat st; |
d6453ce4 | 4779 | EMACS_TIME modtime; |
d311d28c | 4780 | ptrdiff_t count = SPECPDL_INDEX (); |
6fc6f94b | 4781 | int count1; |
3eac9910 | 4782 | Lisp_Object handler; |
4ad827c5 | 4783 | Lisp_Object visit_file; |
65b7d3e7 | 4784 | Lisp_Object annotations; |
b1d1b865 | 4785 | Lisp_Object encoded_filename; |
f75d7a91 PE |
4786 | bool visiting = (EQ (visit, Qt) || STRINGP (visit)); |
4787 | bool quietly = !NILP (visit); | |
7204a979 | 4788 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
6fc6f94b | 4789 | struct buffer *given_buffer; |
6fdaa9a0 | 4790 | struct coding_system coding; |
570d7624 | 4791 | |
d3a67486 | 4792 | if (current_buffer->base_buffer && visiting) |
95385625 RS |
4793 | error ("Cannot do file visiting in an indirect buffer"); |
4794 | ||
561cb8e1 | 4795 | if (!NILP (start) && !STRINGP (start)) |
570d7624 JB |
4796 | validate_region (&start, &end); |
4797 | ||
95c1c901 | 4798 | visit_file = Qnil; |
59fac292 | 4799 | GCPRO5 (start, filename, visit, visit_file, lockname); |
b56567b5 | 4800 | |
570d7624 | 4801 | filename = Fexpand_file_name (filename, Qnil); |
de1d0127 | 4802 | |
236a12f2 | 4803 | if (!NILP (mustbenew) && !EQ (mustbenew, Qexcl)) |
b8b29dc9 | 4804 | barf_or_query_if_file_exists (filename, "overwrite", 1, 0, 1); |
de1d0127 | 4805 | |
561cb8e1 | 4806 | if (STRINGP (visit)) |
e5176bae | 4807 | visit_file = Fexpand_file_name (visit, Qnil); |
4ad827c5 RS |
4808 | else |
4809 | visit_file = filename; | |
4810 | ||
7204a979 RS |
4811 | if (NILP (lockname)) |
4812 | lockname = visit_file; | |
4813 | ||
65b7d3e7 RS |
4814 | annotations = Qnil; |
4815 | ||
32f4334d RS |
4816 | /* If the file name has special constructs in it, |
4817 | call the corresponding file handler. */ | |
49307295 | 4818 | handler = Ffind_file_name_handler (filename, Qwrite_region); |
b56ad927 | 4819 | /* If FILENAME has no handler, see if VISIT has one. */ |
93c30b5f | 4820 | if (NILP (handler) && STRINGP (visit)) |
199607e4 | 4821 | handler = Ffind_file_name_handler (visit, Qwrite_region); |
3eac9910 | 4822 | |
32f4334d RS |
4823 | if (!NILP (handler)) |
4824 | { | |
32f4334d | 4825 | Lisp_Object val; |
51cf6d37 RS |
4826 | val = call6 (handler, Qwrite_region, start, end, |
4827 | filename, append, visit); | |
32f4334d | 4828 | |
d6a3cc15 | 4829 | if (visiting) |
32f4334d | 4830 | { |
95385625 | 4831 | SAVE_MODIFF = MODIFF; |
4b4deea2 | 4832 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
39eb03f1 | 4833 | bset_filename (current_buffer, visit_file); |
32f4334d | 4834 | } |
09121adc | 4835 | UNGCPRO; |
32f4334d RS |
4836 | return val; |
4837 | } | |
4838 | ||
4a38de71 KH |
4839 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); |
4840 | ||
561cb8e1 RS |
4841 | /* Special kludge to simplify auto-saving. */ |
4842 | if (NILP (start)) | |
4843 | { | |
6b3d752c SM |
4844 | /* Do it later, so write-region-annotate-function can work differently |
4845 | if we save "the buffer" vs "a region". | |
4846 | This is useful in tar-mode. --Stef | |
2acfd7ae | 4847 | XSETFASTINT (start, BEG); |
6b3d752c | 4848 | XSETFASTINT (end, Z); */ |
4a38de71 | 4849 | Fwiden (); |
561cb8e1 RS |
4850 | } |
4851 | ||
67fbc0cb CY |
4852 | record_unwind_protect (build_annotations_unwind, |
4853 | Vwrite_region_annotation_buffers); | |
4854 | Vwrite_region_annotation_buffers = Fcons (Fcurrent_buffer (), Qnil); | |
aed13378 | 4855 | count1 = SPECPDL_INDEX (); |
6fc6f94b RS |
4856 | |
4857 | given_buffer = current_buffer; | |
bf3428a1 RS |
4858 | |
4859 | if (!STRINGP (start)) | |
236a12f2 | 4860 | { |
bf3428a1 RS |
4861 | annotations = build_annotations (start, end); |
4862 | ||
4863 | if (current_buffer != given_buffer) | |
4864 | { | |
4865 | XSETFASTINT (start, BEGV); | |
4866 | XSETFASTINT (end, ZV); | |
4867 | } | |
236a12f2 SM |
4868 | } |
4869 | ||
6b3d752c SM |
4870 | if (NILP (start)) |
4871 | { | |
4872 | XSETFASTINT (start, BEGV); | |
4873 | XSETFASTINT (end, ZV); | |
4874 | } | |
4875 | ||
236a12f2 SM |
4876 | UNGCPRO; |
4877 | ||
4878 | GCPRO5 (start, filename, annotations, visit_file, lockname); | |
4879 | ||
59fac292 SM |
4880 | /* Decide the coding-system to encode the data with. |
4881 | We used to make this choice before calling build_annotations, but that | |
4882 | leads to problems when a write-annotate-function takes care of | |
4883 | unsavable chars (as was the case with X-Symbol). */ | |
9dbda100 GM |
4884 | Vlast_coding_system_used = NILP (Vcoding_system_for_write) ? |
4885 | Fchoose_write_coding_system (start, end, filename, | |
4886 | append, visit, lockname) : | |
4887 | Vcoding_system_for_write; | |
4888 | ||
4889 | setup_coding_system (Vlast_coding_system_used, &coding); | |
4890 | ||
4891 | if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display))) | |
4892 | coding.mode |= CODING_MODE_SELECTIVE_DISPLAY; | |
d6a3cc15 | 4893 | |
570d7624 JB |
4894 | #ifdef CLASH_DETECTION |
4895 | if (!auto_saving) | |
67fbc0cb | 4896 | lock_file (lockname); |
570d7624 JB |
4897 | #endif /* CLASH_DETECTION */ |
4898 | ||
b1d1b865 | 4899 | encoded_filename = ENCODE_FILE (filename); |
5976c3fe | 4900 | fn = SSDATA (encoded_filename); |
3a955a1f PE |
4901 | open_flags = O_WRONLY | O_BINARY | O_CREAT; |
4902 | open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : !NILP (append) ? 0 : O_TRUNC; | |
4903 | if (NUMBERP (append)) | |
4904 | offset = file_offset (append); | |
4905 | else if (!NILP (append)) | |
4906 | open_flags |= O_APPEND; | |
5e570b75 | 4907 | #ifdef DOS_NT |
3a955a1f PE |
4908 | mode = S_IREAD | S_IWRITE; |
4909 | #else | |
4910 | mode = auto_saving ? auto_save_mode_bits : 0666; | |
4911 | #endif | |
4912 | ||
4913 | desc = emacs_open (fn, open_flags, mode); | |
570d7624 JB |
4914 | |
4915 | if (desc < 0) | |
4916 | { | |
4917 | #ifdef CLASH_DETECTION | |
4918 | save_errno = errno; | |
7204a979 | 4919 | if (!auto_saving) unlock_file (lockname); |
570d7624 JB |
4920 | errno = save_errno; |
4921 | #endif /* CLASH_DETECTION */ | |
43fb7d9a | 4922 | UNGCPRO; |
570d7624 JB |
4923 | report_file_error ("Opening output file", Fcons (filename, Qnil)); |
4924 | } | |
4925 | ||
4926 | record_unwind_protect (close_file_unwind, make_number (desc)); | |
4927 | ||
3a955a1f | 4928 | if (NUMBERP (append)) |
43fb7d9a | 4929 | { |
3a955a1f | 4930 | off_t ret = lseek (desc, offset, SEEK_SET); |
43fb7d9a DL |
4931 | if (ret < 0) |
4932 | { | |
570d7624 | 4933 | #ifdef CLASH_DETECTION |
e5e9d610 | 4934 | save_errno = errno; |
43fb7d9a | 4935 | if (!auto_saving) unlock_file (lockname); |
e5e9d610 | 4936 | errno = save_errno; |
570d7624 | 4937 | #endif /* CLASH_DETECTION */ |
43fb7d9a DL |
4938 | UNGCPRO; |
4939 | report_file_error ("Lseek error", Fcons (filename, Qnil)); | |
4940 | } | |
4941 | } | |
efdc16c9 | 4942 | |
43fb7d9a | 4943 | UNGCPRO; |
570d7624 | 4944 | |
570d7624 JB |
4945 | immediate_quit = 1; |
4946 | ||
561cb8e1 | 4947 | if (STRINGP (start)) |
f75d7a91 | 4948 | ok = a_write (desc, start, 0, SCHARS (start), &annotations, &coding); |
570d7624 | 4949 | else if (XINT (start) != XINT (end)) |
f75d7a91 PE |
4950 | ok = a_write (desc, Qnil, XINT (start), XINT (end) - XINT (start), |
4951 | &annotations, &coding); | |
69f6e679 RS |
4952 | else |
4953 | { | |
f75d7a91 | 4954 | /* If file was empty, still need to write the annotations. */ |
c8a6d68a | 4955 | coding.mode |= CODING_MODE_LAST_BLOCK; |
f75d7a91 | 4956 | ok = a_write (desc, Qnil, XINT (end), 0, &annotations, &coding); |
6fdaa9a0 | 4957 | } |
f75d7a91 | 4958 | save_errno = errno; |
6fdaa9a0 | 4959 | |
f75d7a91 PE |
4960 | if (ok && CODING_REQUIRE_FLUSHING (&coding) |
4961 | && !(coding.mode & CODING_MODE_LAST_BLOCK)) | |
6fdaa9a0 KH |
4962 | { |
4963 | /* We have to flush out a data. */ | |
c8a6d68a | 4964 | coding.mode |= CODING_MODE_LAST_BLOCK; |
f75d7a91 | 4965 | ok = e_write (desc, Qnil, 1, 1, &coding); |
69f6e679 | 4966 | save_errno = errno; |
570d7624 JB |
4967 | } |
4968 | ||
4969 | immediate_quit = 0; | |
4970 | ||
6e23c83e | 4971 | #ifdef HAVE_FSYNC |
570d7624 JB |
4972 | /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun). |
4973 | Disk full in NFS may be reported here. */ | |
1daffa1c RS |
4974 | /* mib says that closing the file will try to write as fast as NFS can do |
4975 | it, and that means the fsync here is not crucial for autosave files. */ | |
ccf61795 | 4976 | if (!auto_saving && !write_region_inhibit_fsync && fsync (desc) < 0) |
cb33c142 | 4977 | { |
6cff77fd AS |
4978 | /* If fsync fails with EINTR, don't treat that as serious. Also |
4979 | ignore EINVAL which happens when fsync is not supported on this | |
4980 | file. */ | |
4981 | if (errno != EINTR && errno != EINVAL) | |
f75d7a91 | 4982 | ok = 0, save_errno = errno; |
cb33c142 | 4983 | } |
570d7624 JB |
4984 | #endif |
4985 | ||
d6453ce4 PE |
4986 | modtime = invalid_emacs_time (); |
4987 | if (visiting) | |
4988 | { | |
4989 | if (fstat (desc, &st) == 0) | |
4990 | modtime = get_stat_mtime (&st); | |
4991 | else | |
4992 | ok = 0, save_errno = errno; | |
4993 | } | |
4994 | ||
570d7624 | 4995 | /* NFS can report a write failure now. */ |
68c45bf0 | 4996 | if (emacs_close (desc) < 0) |
f75d7a91 | 4997 | ok = 0, save_errno = errno; |
570d7624 | 4998 | |
6fc6f94b RS |
4999 | /* Discard the unwind protect for close_file_unwind. */ |
5000 | specpdl_ptr = specpdl + count1; | |
67fbc0cb | 5001 | |
9fe43ff6 PE |
5002 | /* Some file systems have a bug where st_mtime is not updated |
5003 | properly after a write. For example, CIFS might not see the | |
5004 | st_mtime change until after the file is opened again. | |
5005 | ||
5006 | Attempt to detect this file system bug, and update MODTIME to the | |
5007 | newer st_mtime if the bug appears to be present. This introduces | |
5008 | a race condition, so to avoid most instances of the race condition | |
5009 | on non-buggy file systems, skip this check if the most recently | |
5010 | encountered non-buggy file system was the current file system. | |
5011 | ||
5012 | A race condition can occur if some other process modifies the | |
5013 | file between the fstat above and the fstat below, but the race is | |
5014 | unlikely and a similar race between the last write and the fstat | |
5015 | above cannot possibly be closed anyway. */ | |
5016 | ||
5017 | if (EMACS_TIME_VALID_P (modtime) | |
5018 | && ! (valid_timestamp_file_system && st.st_dev == timestamp_file_system)) | |
5019 | { | |
21cd50b8 | 5020 | int desc1 = emacs_open (fn, O_WRONLY | O_BINARY, 0); |
9fe43ff6 PE |
5021 | if (0 <= desc1) |
5022 | { | |
5023 | struct stat st1; | |
5024 | if (fstat (desc1, &st1) == 0 | |
5025 | && st.st_dev == st1.st_dev && st.st_ino == st1.st_ino) | |
5026 | { | |
5027 | EMACS_TIME modtime1 = get_stat_mtime (&st1); | |
5028 | if (EMACS_TIME_EQ (modtime, modtime1) | |
5029 | && st.st_size == st1.st_size) | |
5030 | { | |
5031 | timestamp_file_system = st.st_dev; | |
5032 | valid_timestamp_file_system = 1; | |
5033 | } | |
5034 | else | |
5035 | { | |
5036 | st.st_size = st1.st_size; | |
5037 | modtime = modtime1; | |
5038 | } | |
5039 | } | |
5040 | emacs_close (desc1); | |
5041 | } | |
5042 | } | |
5043 | ||
67fbc0cb | 5044 | /* Call write-region-post-annotation-function. */ |
294fa707 | 5045 | while (CONSP (Vwrite_region_annotation_buffers)) |
67fbc0cb CY |
5046 | { |
5047 | Lisp_Object buf = XCAR (Vwrite_region_annotation_buffers); | |
5048 | if (!NILP (Fbuffer_live_p (buf))) | |
5049 | { | |
5050 | Fset_buffer (buf); | |
5051 | if (FUNCTIONP (Vwrite_region_post_annotation_function)) | |
5052 | call0 (Vwrite_region_post_annotation_function); | |
5053 | } | |
5054 | Vwrite_region_annotation_buffers | |
5055 | = XCDR (Vwrite_region_annotation_buffers); | |
5056 | } | |
5057 | ||
5058 | unbind_to (count, Qnil); | |
570d7624 JB |
5059 | |
5060 | #ifdef CLASH_DETECTION | |
5061 | if (!auto_saving) | |
7204a979 | 5062 | unlock_file (lockname); |
570d7624 JB |
5063 | #endif /* CLASH_DETECTION */ |
5064 | ||
5065 | /* Do this before reporting IO error | |
5066 | to avoid a "file has changed on disk" warning on | |
5067 | next attempt to save. */ | |
d6453ce4 | 5068 | if (EMACS_TIME_VALID_P (modtime)) |
58b963f7 | 5069 | { |
d6453ce4 | 5070 | current_buffer->modtime = modtime; |
58b963f7 SM |
5071 | current_buffer->modtime_size = st.st_size; |
5072 | } | |
570d7624 | 5073 | |
f75d7a91 | 5074 | if (! ok) |
d5db4077 | 5075 | error ("IO error writing %s: %s", SDATA (filename), |
68c45bf0 | 5076 | emacs_strerror (save_errno)); |
570d7624 | 5077 | |
d6a3cc15 | 5078 | if (visiting) |
570d7624 | 5079 | { |
95385625 | 5080 | SAVE_MODIFF = MODIFF; |
4b4deea2 | 5081 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
39eb03f1 | 5082 | bset_filename (current_buffer, visit_file); |
f4226e89 | 5083 | update_mode_lines++; |
570d7624 | 5084 | } |
d6a3cc15 | 5085 | else if (quietly) |
6b61353c KH |
5086 | { |
5087 | if (auto_saving | |
4b4deea2 TT |
5088 | && ! NILP (Fstring_equal (BVAR (current_buffer, filename), |
5089 | BVAR (current_buffer, auto_save_file_name)))) | |
6b61353c KH |
5090 | SAVE_MODIFF = MODIFF; |
5091 | ||
5092 | return Qnil; | |
5093 | } | |
570d7624 JB |
5094 | |
5095 | if (!auto_saving) | |
3908c576 | 5096 | message_with_string ((NUMBERP (append) |
0c328a0e RS |
5097 | ? "Updated %s" |
5098 | : ! NILP (append) | |
5099 | ? "Added to %s" | |
5100 | : "Wrote %s"), | |
5101 | visit_file, 1); | |
570d7624 JB |
5102 | |
5103 | return Qnil; | |
5104 | } | |
ec7adf26 | 5105 | \f |
dd4c5104 | 5106 | Lisp_Object merge (Lisp_Object, Lisp_Object, Lisp_Object); |
d6a3cc15 RS |
5107 | |
5108 | DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0, | |
8c1a1077 | 5109 | doc: /* Return t if (car A) is numerically less than (car B). */) |
5842a27b | 5110 | (Lisp_Object a, Lisp_Object b) |
d6a3cc15 RS |
5111 | { |
5112 | return Flss (Fcar (a), Fcar (b)); | |
5113 | } | |
5114 | ||
5115 | /* Build the complete list of annotations appropriate for writing out | |
5116 | the text between START and END, by calling all the functions in | |
6fc6f94b RS |
5117 | write-region-annotate-functions and merging the lists they return. |
5118 | If one of these functions switches to a different buffer, we assume | |
5119 | that buffer contains altered text. Therefore, the caller must | |
5120 | make sure to restore the current buffer in all cases, | |
5121 | as save-excursion would do. */ | |
d6a3cc15 RS |
5122 | |
5123 | static Lisp_Object | |
971de7fb | 5124 | build_annotations (Lisp_Object start, Lisp_Object end) |
d6a3cc15 RS |
5125 | { |
5126 | Lisp_Object annotations; | |
5127 | Lisp_Object p, res; | |
5128 | struct gcpro gcpro1, gcpro2; | |
0a20b684 | 5129 | Lisp_Object original_buffer; |
f75d7a91 PE |
5130 | int i; |
5131 | bool used_global = 0; | |
0a20b684 RS |
5132 | |
5133 | XSETBUFFER (original_buffer, current_buffer); | |
d6a3cc15 RS |
5134 | |
5135 | annotations = Qnil; | |
5136 | p = Vwrite_region_annotate_functions; | |
5137 | GCPRO2 (annotations, p); | |
28c3eb5a | 5138 | while (CONSP (p)) |
d6a3cc15 | 5139 | { |
6fc6f94b | 5140 | struct buffer *given_buffer = current_buffer; |
bd235610 SM |
5141 | if (EQ (Qt, XCAR (p)) && !used_global) |
5142 | { /* Use the global value of the hook. */ | |
5143 | Lisp_Object arg[2]; | |
5144 | used_global = 1; | |
5145 | arg[0] = Fdefault_value (Qwrite_region_annotate_functions); | |
5146 | arg[1] = XCDR (p); | |
5147 | p = Fappend (2, arg); | |
5148 | continue; | |
5149 | } | |
6fc6f94b | 5150 | Vwrite_region_annotations_so_far = annotations; |
28c3eb5a | 5151 | res = call2 (XCAR (p), start, end); |
6fc6f94b RS |
5152 | /* If the function makes a different buffer current, |
5153 | assume that means this buffer contains altered text to be output. | |
5154 | Reset START and END from the buffer bounds | |
5155 | and discard all previous annotations because they should have | |
5156 | been dealt with by this function. */ | |
5157 | if (current_buffer != given_buffer) | |
5158 | { | |
67fbc0cb CY |
5159 | Vwrite_region_annotation_buffers |
5160 | = Fcons (Fcurrent_buffer (), | |
5161 | Vwrite_region_annotation_buffers); | |
3cf29f61 RS |
5162 | XSETFASTINT (start, BEGV); |
5163 | XSETFASTINT (end, ZV); | |
6fc6f94b RS |
5164 | annotations = Qnil; |
5165 | } | |
d6a3cc15 RS |
5166 | Flength (res); /* Check basic validity of return value */ |
5167 | annotations = merge (annotations, res, Qcar_less_than_car); | |
28c3eb5a | 5168 | p = XCDR (p); |
d6a3cc15 | 5169 | } |
0d420e88 BG |
5170 | |
5171 | /* Now do the same for annotation functions implied by the file-format */ | |
4b4deea2 TT |
5172 | if (auto_saving && (!EQ (BVAR (current_buffer, auto_save_file_format), Qt))) |
5173 | p = BVAR (current_buffer, auto_save_file_format); | |
0d420e88 | 5174 | else |
4b4deea2 | 5175 | p = BVAR (current_buffer, file_format); |
28c3eb5a | 5176 | for (i = 0; CONSP (p); p = XCDR (p), ++i) |
0d420e88 BG |
5177 | { |
5178 | struct buffer *given_buffer = current_buffer; | |
efdc16c9 | 5179 | |
0d420e88 | 5180 | Vwrite_region_annotations_so_far = annotations; |
532ed661 GM |
5181 | |
5182 | /* Value is either a list of annotations or nil if the function | |
5183 | has written annotations to a temporary buffer, which is now | |
5184 | current. */ | |
28c3eb5a | 5185 | res = call5 (Qformat_annotate_function, XCAR (p), start, end, |
532ed661 | 5186 | original_buffer, make_number (i)); |
0d420e88 BG |
5187 | if (current_buffer != given_buffer) |
5188 | { | |
3cf29f61 RS |
5189 | XSETFASTINT (start, BEGV); |
5190 | XSETFASTINT (end, ZV); | |
0d420e88 BG |
5191 | annotations = Qnil; |
5192 | } | |
efdc16c9 | 5193 | |
532ed661 GM |
5194 | if (CONSP (res)) |
5195 | annotations = merge (annotations, res, Qcar_less_than_car); | |
0d420e88 | 5196 | } |
6fdaa9a0 | 5197 | |
236a12f2 SM |
5198 | UNGCPRO; |
5199 | return annotations; | |
5200 | } | |
5201 | ||
ec7adf26 | 5202 | \f |
ce51c54c KH |
5203 | /* Write to descriptor DESC the NCHARS chars starting at POS of STRING. |
5204 | If STRING is nil, POS is the character position in the current buffer. | |
d6a3cc15 | 5205 | Intersperse with them the annotations from *ANNOT |
ce51c54c | 5206 | which fall within the range of POS to POS + NCHARS, |
d6a3cc15 RS |
5207 | each at its appropriate position. |
5208 | ||
ec7adf26 RS |
5209 | We modify *ANNOT by discarding elements as we use them up. |
5210 | ||
f75d7a91 | 5211 | Return true if successful. */ |
d6a3cc15 | 5212 | |
f75d7a91 | 5213 | static bool |
d311d28c | 5214 | a_write (int desc, Lisp_Object string, ptrdiff_t pos, |
f75d7a91 | 5215 | ptrdiff_t nchars, Lisp_Object *annot, |
8a2cbd72 | 5216 | struct coding_system *coding) |
d6a3cc15 RS |
5217 | { |
5218 | Lisp_Object tem; | |
d311d28c PE |
5219 | ptrdiff_t nextpos; |
5220 | ptrdiff_t lastpos = pos + nchars; | |
d6a3cc15 | 5221 | |
eb15aa18 | 5222 | while (NILP (*annot) || CONSP (*annot)) |
d6a3cc15 RS |
5223 | { |
5224 | tem = Fcar_safe (Fcar (*annot)); | |
ce51c54c | 5225 | nextpos = pos - 1; |
ec7adf26 | 5226 | if (INTEGERP (tem)) |
ce51c54c | 5227 | nextpos = XFASTINT (tem); |
ec7adf26 RS |
5228 | |
5229 | /* If there are no more annotations in this range, | |
5230 | output the rest of the range all at once. */ | |
ce51c54c KH |
5231 | if (! (nextpos >= pos && nextpos <= lastpos)) |
5232 | return e_write (desc, string, pos, lastpos, coding); | |
ec7adf26 RS |
5233 | |
5234 | /* Output buffer text up to the next annotation's position. */ | |
ce51c54c | 5235 | if (nextpos > pos) |
d6a3cc15 | 5236 | { |
f75d7a91 PE |
5237 | if (!e_write (desc, string, pos, nextpos, coding)) |
5238 | return 0; | |
ce51c54c | 5239 | pos = nextpos; |
d6a3cc15 | 5240 | } |
ec7adf26 | 5241 | /* Output the annotation. */ |
d6a3cc15 RS |
5242 | tem = Fcdr (Fcar (*annot)); |
5243 | if (STRINGP (tem)) | |
5244 | { | |
f75d7a91 PE |
5245 | if (!e_write (desc, tem, 0, SCHARS (tem), coding)) |
5246 | return 0; | |
d6a3cc15 RS |
5247 | } |
5248 | *annot = Fcdr (*annot); | |
5249 | } | |
f75d7a91 | 5250 | return 1; |
d6a3cc15 RS |
5251 | } |
5252 | ||
6fdaa9a0 | 5253 | |
ce51c54c KH |
5254 | /* Write text in the range START and END into descriptor DESC, |
5255 | encoding them with coding system CODING. If STRING is nil, START | |
5256 | and END are character positions of the current buffer, else they | |
f75d7a91 | 5257 | are indexes to the string STRING. Return true if successful. */ |
ec7adf26 | 5258 | |
f75d7a91 | 5259 | static bool |
d311d28c | 5260 | e_write (int desc, Lisp_Object string, ptrdiff_t start, ptrdiff_t end, |
8a2cbd72 | 5261 | struct coding_system *coding) |
570d7624 | 5262 | { |
ce51c54c KH |
5263 | if (STRINGP (string)) |
5264 | { | |
db327c7e | 5265 | start = 0; |
8f924df7 | 5266 | end = SCHARS (string); |
ce51c54c | 5267 | } |
570d7624 | 5268 | |
6fdaa9a0 KH |
5269 | /* We used to have a code for handling selective display here. But, |
5270 | now it is handled within encode_coding. */ | |
01ca97a2 KH |
5271 | |
5272 | while (start < end) | |
570d7624 | 5273 | { |
01ca97a2 | 5274 | if (STRINGP (string)) |
6ad568dd | 5275 | { |
01ca97a2 KH |
5276 | coding->src_multibyte = SCHARS (string) < SBYTES (string); |
5277 | if (CODING_REQUIRE_ENCODING (coding)) | |
5278 | { | |
5279 | encode_coding_object (coding, string, | |
5280 | start, string_char_to_byte (string, start), | |
5281 | end, string_char_to_byte (string, end), Qt); | |
5282 | } | |
5283 | else | |
5284 | { | |
5285 | coding->dst_object = string; | |
5286 | coding->consumed_char = SCHARS (string); | |
5287 | coding->produced = SBYTES (string); | |
5288 | } | |
6ad568dd | 5289 | } |
db327c7e | 5290 | else |
6ad568dd | 5291 | { |
d311d28c PE |
5292 | ptrdiff_t start_byte = CHAR_TO_BYTE (start); |
5293 | ptrdiff_t end_byte = CHAR_TO_BYTE (end); | |
b4132433 | 5294 | |
01ca97a2 KH |
5295 | coding->src_multibyte = (end - start) < (end_byte - start_byte); |
5296 | if (CODING_REQUIRE_ENCODING (coding)) | |
5297 | { | |
5298 | encode_coding_object (coding, Fcurrent_buffer (), | |
5299 | start, start_byte, end, end_byte, Qt); | |
5300 | } | |
5301 | else | |
5302 | { | |
5303 | coding->dst_object = Qnil; | |
5304 | coding->dst_pos_byte = start_byte; | |
5305 | if (start >= GPT || end <= GPT) | |
5306 | { | |
5307 | coding->consumed_char = end - start; | |
5308 | coding->produced = end_byte - start_byte; | |
5309 | } | |
5310 | else | |
5311 | { | |
5312 | coding->consumed_char = GPT - start; | |
5313 | coding->produced = GPT_BYTE - start_byte; | |
5314 | } | |
5315 | } | |
c185d744 | 5316 | } |
01ca97a2 KH |
5317 | |
5318 | if (coding->produced > 0) | |
c185d744 | 5319 | { |
ce2fe65a AS |
5320 | coding->produced |
5321 | -= emacs_write (desc, | |
5322 | STRINGP (coding->dst_object) | |
5323 | ? SSDATA (coding->dst_object) | |
5324 | : (char *) BYTE_POS_ADDR (coding->dst_pos_byte), | |
5325 | coding->produced); | |
01ca97a2 KH |
5326 | |
5327 | if (coding->produced) | |
f75d7a91 | 5328 | return 0; |
570d7624 | 5329 | } |
01ca97a2 | 5330 | start += coding->consumed_char; |
c185d744 KH |
5331 | } |
5332 | ||
f75d7a91 | 5333 | return 1; |
570d7624 | 5334 | } |
ec7adf26 | 5335 | \f |
a7ca3326 | 5336 | DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime, |
ec1b9b17 | 5337 | Sverify_visited_file_modtime, 0, 1, 0, |
8c1a1077 | 5338 | doc: /* Return t if last mod time of BUF's visited file matches what BUF records. |
6b61353c | 5339 | This means that the file has not been changed since it was visited or saved. |
ec1b9b17 | 5340 | If BUF is omitted or nil, it defaults to the current buffer. |
6b61353c | 5341 | See Info node `(elisp)Modification Time' for more details. */) |
5842a27b | 5342 | (Lisp_Object buf) |
570d7624 JB |
5343 | { |
5344 | struct buffer *b; | |
5345 | struct stat st; | |
32f4334d | 5346 | Lisp_Object handler; |
b1d1b865 | 5347 | Lisp_Object filename; |
b95a9c0c | 5348 | EMACS_TIME mtime; |
570d7624 | 5349 | |
ec1b9b17 GM |
5350 | if (NILP (buf)) |
5351 | b = current_buffer; | |
5352 | else | |
5353 | { | |
5354 | CHECK_BUFFER (buf); | |
5355 | b = XBUFFER (buf); | |
5356 | } | |
570d7624 | 5357 | |
4b4deea2 | 5358 | if (!STRINGP (BVAR (b, filename))) return Qt; |
d35af63c | 5359 | if (EMACS_NSECS (b->modtime) == UNKNOWN_MODTIME_NSECS) return Qt; |
570d7624 | 5360 | |
32f4334d RS |
5361 | /* If the file name has special constructs in it, |
5362 | call the corresponding file handler. */ | |
4b4deea2 | 5363 | handler = Ffind_file_name_handler (BVAR (b, filename), |
49307295 | 5364 | Qverify_visited_file_modtime); |
32f4334d | 5365 | if (!NILP (handler)) |
09121adc | 5366 | return call2 (handler, Qverify_visited_file_modtime, buf); |
32f4334d | 5367 | |
4b4deea2 | 5368 | filename = ENCODE_FILE (BVAR (b, filename)); |
b1d1b865 | 5369 | |
dbeed9a6 PE |
5370 | mtime = (stat (SSDATA (filename), &st) == 0 |
5371 | ? get_stat_mtime (&st) | |
f0941253 | 5372 | : time_error_value (errno)); |
b95a9c0c | 5373 | if (EMACS_TIME_EQ (mtime, b->modtime) |
645c6a30 PE |
5374 | && (b->modtime_size < 0 |
5375 | || st.st_size == b->modtime_size)) | |
570d7624 JB |
5376 | return Qt; |
5377 | return Qnil; | |
5378 | } | |
5379 | ||
5380 | DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, | |
8c1a1077 PJ |
5381 | Sclear_visited_file_modtime, 0, 0, 0, |
5382 | doc: /* Clear out records of last mod time of visited file. | |
5383 | Next attempt to save will certainly not complain of a discrepancy. */) | |
5842a27b | 5384 | (void) |
570d7624 | 5385 | { |
e9a9ae03 | 5386 | current_buffer->modtime = make_emacs_time (0, UNKNOWN_MODTIME_NSECS); |
58b963f7 | 5387 | current_buffer->modtime_size = -1; |
570d7624 JB |
5388 | return Qnil; |
5389 | } | |
5390 | ||
f5d5eccf | 5391 | DEFUN ("visited-file-modtime", Fvisited_file_modtime, |
8c1a1077 PJ |
5392 | Svisited_file_modtime, 0, 0, 0, |
5393 | doc: /* Return the current buffer's recorded visited file modification time. | |
d35af63c | 5394 | The value is a list of the form (HIGH LOW USEC PSEC), like the time values that |
1c4d7f3d LMI |
5395 | `file-attributes' returns. If the current buffer has no recorded file |
5396 | modification time, this function returns 0. If the visited file | |
9d1d6e68 | 5397 | doesn't exist, HIGH will be -1. |
6b61353c | 5398 | See Info node `(elisp)Modification Time' for more details. */) |
5842a27b | 5399 | (void) |
f5d5eccf | 5400 | { |
d35af63c | 5401 | if (EMACS_NSECS (current_buffer->modtime) < 0) |
184246df PE |
5402 | { |
5403 | if (EMACS_NSECS (current_buffer->modtime) == NONEXISTENT_MODTIME_NSECS) | |
5404 | { | |
5405 | /* make_lisp_time won't work here if time_t is unsigned. */ | |
5406 | return list4 (make_number (-1), make_number (65535), | |
5407 | make_number (0), make_number (0)); | |
5408 | } | |
5409 | return make_number (0); | |
5410 | } | |
d35af63c | 5411 | return make_lisp_time (current_buffer->modtime); |
f5d5eccf RS |
5412 | } |
5413 | ||
570d7624 | 5414 | DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, |
8c1a1077 PJ |
5415 | Sset_visited_file_modtime, 0, 1, 0, |
5416 | doc: /* Update buffer's recorded modification time from the visited file's time. | |
5417 | Useful if the buffer was not read from the file normally | |
5418 | or if the file itself has been changed for some known benign reason. | |
5419 | An argument specifies the modification time value to use | |
5420 | \(instead of that of the visited file), in the form of a list | |
d35af63c | 5421 | \(HIGH LOW USEC PSEC) as returned by `current-time'. */) |
5842a27b | 5422 | (Lisp_Object time_list) |
570d7624 | 5423 | { |
f5d5eccf | 5424 | if (!NILP (time_list)) |
58b963f7 | 5425 | { |
31571fd7 | 5426 | current_buffer->modtime = lisp_time_argument (time_list); |
58b963f7 SM |
5427 | current_buffer->modtime_size = -1; |
5428 | } | |
f5d5eccf RS |
5429 | else |
5430 | { | |
5431 | register Lisp_Object filename; | |
5432 | struct stat st; | |
5433 | Lisp_Object handler; | |
570d7624 | 5434 | |
4b4deea2 | 5435 | filename = Fexpand_file_name (BVAR (current_buffer, filename), Qnil); |
32f4334d | 5436 | |
f5d5eccf RS |
5437 | /* If the file name has special constructs in it, |
5438 | call the corresponding file handler. */ | |
49307295 | 5439 | handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime); |
f5d5eccf | 5440 | if (!NILP (handler)) |
caf3c431 | 5441 | /* The handler can find the file name the same way we did. */ |
76c881b0 | 5442 | return call2 (handler, Qset_visited_file_modtime, Qnil); |
b1d1b865 RS |
5443 | |
5444 | filename = ENCODE_FILE (filename); | |
5445 | ||
42a5b22f | 5446 | if (stat (SSDATA (filename), &st) >= 0) |
58b963f7 | 5447 | { |
d35af63c | 5448 | current_buffer->modtime = get_stat_mtime (&st); |
58b963f7 SM |
5449 | current_buffer->modtime_size = st.st_size; |
5450 | } | |
f5d5eccf | 5451 | } |
570d7624 JB |
5452 | |
5453 | return Qnil; | |
5454 | } | |
5455 | \f | |
f14b7e14 | 5456 | static Lisp_Object |
f839df0c | 5457 | auto_save_error (Lisp_Object error_val) |
570d7624 | 5458 | { |
d7f31e22 | 5459 | Lisp_Object args[3], msg; |
b09cca6a | 5460 | int i; |
d7f31e22 | 5461 | struct gcpro gcpro1; |
efdc16c9 | 5462 | |
ca730bf0 CY |
5463 | auto_save_error_occurred = 1; |
5464 | ||
385ed61f | 5465 | ring_bell (XFRAME (selected_frame)); |
efdc16c9 | 5466 | |
d7f31e22 | 5467 | args[0] = build_string ("Auto-saving %s: %s"); |
4b4deea2 | 5468 | args[1] = BVAR (current_buffer, name); |
f839df0c | 5469 | args[2] = Ferror_message_string (error_val); |
d7f31e22 GM |
5470 | msg = Fformat (3, args); |
5471 | GCPRO1 (msg); | |
d7f31e22 GM |
5472 | |
5473 | for (i = 0; i < 3; ++i) | |
5474 | { | |
5475 | if (i == 0) | |
b09cca6a | 5476 | message3 (msg); |
d7f31e22 | 5477 | else |
b09cca6a | 5478 | message3_nolog (msg); |
d7f31e22 GM |
5479 | Fsleep_for (make_number (1), Qnil); |
5480 | } | |
5481 | ||
5482 | UNGCPRO; | |
570d7624 JB |
5483 | return Qnil; |
5484 | } | |
5485 | ||
f14b7e14 | 5486 | static Lisp_Object |
971de7fb | 5487 | auto_save_1 (void) |
570d7624 | 5488 | { |
570d7624 | 5489 | struct stat st; |
d4a42098 KS |
5490 | Lisp_Object modes; |
5491 | ||
5492 | auto_save_mode_bits = 0666; | |
570d7624 JB |
5493 | |
5494 | /* Get visited file's mode to become the auto save file's mode. */ | |
4b4deea2 | 5495 | if (! NILP (BVAR (current_buffer, filename))) |
d4a42098 | 5496 | { |
4b4deea2 | 5497 | if (stat (SSDATA (BVAR (current_buffer, filename)), &st) >= 0) |
d4a42098 | 5498 | /* But make sure we can overwrite it later! */ |
18c52557 | 5499 | auto_save_mode_bits = (st.st_mode | 0600) & 0777; |
ce2fe65a AS |
5500 | else if (modes = Ffile_modes (BVAR (current_buffer, filename)), |
5501 | INTEGERP (modes)) | |
d4a42098 | 5502 | /* Remote files don't cooperate with stat. */ |
18c52557 | 5503 | auto_save_mode_bits = (XINT (modes) | 0600) & 0777; |
d4a42098 | 5504 | } |
570d7624 JB |
5505 | |
5506 | return | |
4b4deea2 | 5507 | Fwrite_region (Qnil, Qnil, BVAR (current_buffer, auto_save_file_name), Qnil, |
699b53bc CY |
5508 | NILP (Vauto_save_visited_file_name) ? Qlambda : Qt, |
5509 | Qnil, Qnil); | |
570d7624 JB |
5510 | } |
5511 | ||
e54d3b5d | 5512 | static Lisp_Object |
971de7fb | 5513 | do_auto_save_unwind (Lisp_Object arg) /* used as unwind-protect function */ |
410ed5c3 | 5514 | |
e54d3b5d | 5515 | { |
2b30549c | 5516 | FILE *stream = XSAVE_POINTER (arg, 0); |
3be3c08e | 5517 | auto_saving = 0; |
fff7e982 | 5518 | if (stream != NULL) |
aab12958 | 5519 | { |
4d7e6e51 | 5520 | block_input (); |
aab12958 | 5521 | fclose (stream); |
4d7e6e51 | 5522 | unblock_input (); |
aab12958 | 5523 | } |
e54d3b5d RS |
5524 | return Qnil; |
5525 | } | |
5526 | ||
a8c828be | 5527 | static Lisp_Object |
971de7fb | 5528 | do_auto_save_unwind_1 (Lisp_Object value) /* used as unwind-protect function */ |
410ed5c3 | 5529 | |
a8c828be RS |
5530 | { |
5531 | minibuffer_auto_raise = XINT (value); | |
5532 | return Qnil; | |
5533 | } | |
5534 | ||
5794dd61 | 5535 | static Lisp_Object |
971de7fb | 5536 | do_auto_save_make_dir (Lisp_Object dir) |
5794dd61 | 5537 | { |
09450bae | 5538 | Lisp_Object result; |
26816cbf | 5539 | |
09450bae PE |
5540 | auto_saving_dir_umask = 077; |
5541 | result = call2 (Qmake_directory, dir, Qt); | |
5542 | auto_saving_dir_umask = 0; | |
5543 | return result; | |
5794dd61 RS |
5544 | } |
5545 | ||
5546 | static Lisp_Object | |
971de7fb | 5547 | do_auto_save_eh (Lisp_Object ignore) |
5794dd61 | 5548 | { |
09450bae | 5549 | auto_saving_dir_umask = 0; |
5794dd61 RS |
5550 | return Qnil; |
5551 | } | |
5552 | ||
a7ca3326 | 5553 | DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "", |
8c1a1077 PJ |
5554 | doc: /* Auto-save all buffers that need it. |
5555 | This is all buffers that have auto-saving enabled | |
5556 | and are changed since last auto-saved. | |
5557 | Auto-saving writes the buffer into a file | |
5558 | so that your editing is not lost if the system crashes. | |
5559 | This file is not the file you visited; that changes only when you save. | |
5560 | Normally we run the normal hook `auto-save-hook' before saving. | |
5561 | ||
5562 | A non-nil NO-MESSAGE argument means do not print any message if successful. | |
5563 | A non-nil CURRENT-ONLY argument means save only current buffer. */) | |
5842a27b | 5564 | (Lisp_Object no_message, Lisp_Object current_only) |
570d7624 JB |
5565 | { |
5566 | struct buffer *old = current_buffer, *b; | |
dee091a3 | 5567 | Lisp_Object tail, buf, hook; |
f75d7a91 | 5568 | bool auto_saved = 0; |
f14b1c68 | 5569 | int do_handled_files; |
ff4c9993 | 5570 | Lisp_Object oquit; |
fff7e982 | 5571 | FILE *stream = NULL; |
d311d28c | 5572 | ptrdiff_t count = SPECPDL_INDEX (); |
f75d7a91 | 5573 | bool orig_minibuffer_auto_raise = minibuffer_auto_raise; |
fce31d69 | 5574 | bool old_message_p = 0; |
d57563b6 | 5575 | struct gcpro gcpro1, gcpro2; |
38da540d RS |
5576 | |
5577 | if (max_specpdl_size < specpdl_size + 40) | |
5578 | max_specpdl_size = specpdl_size + 40; | |
5579 | ||
5580 | if (minibuf_level) | |
5581 | no_message = Qt; | |
5582 | ||
5794dd61 RS |
5583 | if (NILP (no_message)) |
5584 | { | |
5585 | old_message_p = push_message (); | |
5586 | record_unwind_protect (pop_message_unwind, Qnil); | |
5587 | } | |
efdc16c9 | 5588 | |
ff4c9993 RS |
5589 | /* Ordinarily don't quit within this function, |
5590 | but don't make it impossible to quit (in case we get hung in I/O). */ | |
5591 | oquit = Vquit_flag; | |
5592 | Vquit_flag = Qnil; | |
570d7624 JB |
5593 | |
5594 | /* No GCPRO needed, because (when it matters) all Lisp_Object variables | |
5595 | point to non-strings reached from Vbuffer_alist. */ | |
5596 | ||
dee091a3 JD |
5597 | hook = intern ("auto-save-hook"); |
5598 | Frun_hooks (1, &hook); | |
570d7624 | 5599 | |
e54d3b5d RS |
5600 | if (STRINGP (Vauto_save_list_file_name)) |
5601 | { | |
0894672f | 5602 | Lisp_Object listfile; |
efdc16c9 | 5603 | |
258fd2cb | 5604 | listfile = Fexpand_file_name (Vauto_save_list_file_name, Qnil); |
0894672f GM |
5605 | |
5606 | /* Don't try to create the directory when shutting down Emacs, | |
5607 | because creating the directory might signal an error, and | |
5608 | that would leave Emacs in a strange state. */ | |
5609 | if (!NILP (Vrun_hooks)) | |
5610 | { | |
5611 | Lisp_Object dir; | |
d57563b6 RS |
5612 | dir = Qnil; |
5613 | GCPRO2 (dir, listfile); | |
0894672f GM |
5614 | dir = Ffile_name_directory (listfile); |
5615 | if (NILP (Ffile_directory_p (dir))) | |
5794dd61 | 5616 | internal_condition_case_1 (do_auto_save_make_dir, |
09450bae | 5617 | dir, Qt, |
5794dd61 | 5618 | do_auto_save_eh); |
d57563b6 | 5619 | UNGCPRO; |
0894672f | 5620 | } |
efdc16c9 | 5621 | |
42a5b22f | 5622 | stream = fopen (SSDATA (listfile), "w"); |
1b335d29 | 5623 | } |
199607e4 | 5624 | |
fff7e982 | 5625 | record_unwind_protect (do_auto_save_unwind, |
468afbac | 5626 | make_save_pointer (stream)); |
a8c828be RS |
5627 | record_unwind_protect (do_auto_save_unwind_1, |
5628 | make_number (minibuffer_auto_raise)); | |
5629 | minibuffer_auto_raise = 0; | |
3be3c08e | 5630 | auto_saving = 1; |
ca730bf0 | 5631 | auto_save_error_occurred = 0; |
3be3c08e | 5632 | |
6b61353c KH |
5633 | /* On first pass, save all files that don't have handlers. |
5634 | On second pass, save all files that do have handlers. | |
5635 | ||
5636 | If Emacs is crashing, the handlers may tweak what is causing | |
5637 | Emacs to crash in the first place, and it would be a shame if | |
5638 | Emacs failed to autosave perfectly ordinary files because it | |
5639 | couldn't handle some ange-ftp'd file. */ | |
5640 | ||
f14b1c68 | 5641 | for (do_handled_files = 0; do_handled_files < 2; do_handled_files++) |
8e50cc2d | 5642 | for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail)) |
f14b1c68 | 5643 | { |
03699b14 | 5644 | buf = XCDR (XCAR (tail)); |
f14b1c68 | 5645 | b = XBUFFER (buf); |
199607e4 | 5646 | |
e54d3b5d | 5647 | /* Record all the buffers that have auto save mode |
258fd2cb RS |
5648 | in the special file that lists them. For each of these buffers, |
5649 | Record visited name (if any) and auto save name. */ | |
4b4deea2 | 5650 | if (STRINGP (BVAR (b, auto_save_file_name)) |
1b335d29 | 5651 | && stream != NULL && do_handled_files == 0) |
e54d3b5d | 5652 | { |
4d7e6e51 | 5653 | block_input (); |
4b4deea2 | 5654 | if (!NILP (BVAR (b, filename))) |
258fd2cb | 5655 | { |
4b4deea2 TT |
5656 | fwrite (SDATA (BVAR (b, filename)), 1, |
5657 | SBYTES (BVAR (b, filename)), stream); | |
258fd2cb | 5658 | } |
1b335d29 | 5659 | putc ('\n', stream); |
4b4deea2 TT |
5660 | fwrite (SDATA (BVAR (b, auto_save_file_name)), 1, |
5661 | SBYTES (BVAR (b, auto_save_file_name)), stream); | |
1b335d29 | 5662 | putc ('\n', stream); |
4d7e6e51 | 5663 | unblock_input (); |
e54d3b5d | 5664 | } |
17857782 | 5665 | |
f14b1c68 JB |
5666 | if (!NILP (current_only) |
5667 | && b != current_buffer) | |
5668 | continue; | |
e54d3b5d | 5669 | |
95385625 RS |
5670 | /* Don't auto-save indirect buffers. |
5671 | The base buffer takes care of it. */ | |
5672 | if (b->base_buffer) | |
5673 | continue; | |
5674 | ||
f14b1c68 JB |
5675 | /* Check for auto save enabled |
5676 | and file changed since last auto save | |
5677 | and file changed since last real save. */ | |
4b4deea2 | 5678 | if (STRINGP (BVAR (b, auto_save_file_name)) |
95385625 | 5679 | && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b) |
0b5397c2 | 5680 | && BUF_AUTOSAVE_MODIFF (b) < BUF_MODIFF (b) |
82c2d839 | 5681 | /* -1 means we've turned off autosaving for a while--see below. */ |
4b4deea2 | 5682 | && XINT (BVAR (b, save_length)) >= 0 |
f14b1c68 | 5683 | && (do_handled_files |
4b4deea2 | 5684 | || NILP (Ffind_file_name_handler (BVAR (b, auto_save_file_name), |
49307295 | 5685 | Qwrite_region)))) |
f14b1c68 | 5686 | { |
e9a9ae03 PE |
5687 | EMACS_TIME before_time = current_emacs_time (); |
5688 | EMACS_TIME after_time; | |
b60247d9 RS |
5689 | |
5690 | /* If we had a failure, don't try again for 20 minutes. */ | |
d311d28c | 5691 | if (b->auto_save_failure_time > 0 |
b60247d9 RS |
5692 | && EMACS_SECS (before_time) - b->auto_save_failure_time < 1200) |
5693 | continue; | |
5694 | ||
090101cf CY |
5695 | set_buffer_internal (b); |
5696 | if (NILP (Vauto_save_include_big_deletions) | |
4b4deea2 | 5697 | && (XFASTINT (BVAR (b, save_length)) * 10 |
4be941e3 | 5698 | > (BUF_Z (b) - BUF_BEG (b)) * 13) |
f14b1c68 JB |
5699 | /* A short file is likely to change a large fraction; |
5700 | spare the user annoying messages. */ | |
4b4deea2 | 5701 | && XFASTINT (BVAR (b, save_length)) > 5000 |
f14b1c68 | 5702 | /* These messages are frequent and annoying for `*mail*'. */ |
4b4deea2 | 5703 | && !EQ (BVAR (b, filename), Qnil) |
f14b1c68 JB |
5704 | && NILP (no_message)) |
5705 | { | |
5706 | /* It has shrunk too much; turn off auto-saving here. */ | |
a8c828be | 5707 | minibuffer_auto_raise = orig_minibuffer_auto_raise; |
fd91d0d4 | 5708 | message_with_string ("Buffer %s has shrunk a lot; auto save disabled in that buffer until next real save", |
4b4deea2 | 5709 | BVAR (b, name), 1); |
a8c828be | 5710 | minibuffer_auto_raise = 0; |
82c2d839 RS |
5711 | /* Turn off auto-saving until there's a real save, |
5712 | and prevent any more warnings. */ | |
4b4deea2 | 5713 | XSETINT (BVAR (b, save_length), -1); |
f14b1c68 JB |
5714 | Fsleep_for (make_number (1), Qnil); |
5715 | continue; | |
5716 | } | |
f14b1c68 JB |
5717 | if (!auto_saved && NILP (no_message)) |
5718 | message1 ("Auto-saving..."); | |
5719 | internal_condition_case (auto_save_1, Qt, auto_save_error); | |
f75d7a91 | 5720 | auto_saved = 1; |
0b5397c2 | 5721 | BUF_AUTOSAVE_MODIFF (b) = BUF_MODIFF (b); |
4b4deea2 | 5722 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
f14b1c68 | 5723 | set_buffer_internal (old); |
b60247d9 | 5724 | |
e9a9ae03 | 5725 | after_time = current_emacs_time (); |
b60247d9 RS |
5726 | |
5727 | /* If auto-save took more than 60 seconds, | |
5728 | assume it was an NFS failure that got a timeout. */ | |
5729 | if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60) | |
5730 | b->auto_save_failure_time = EMACS_SECS (after_time); | |
f14b1c68 JB |
5731 | } |
5732 | } | |
570d7624 | 5733 | |
b67f2ca5 RS |
5734 | /* Prevent another auto save till enough input events come in. */ |
5735 | record_auto_save (); | |
570d7624 | 5736 | |
17857782 | 5737 | if (auto_saved && NILP (no_message)) |
f05b275b | 5738 | { |
5794dd61 | 5739 | if (old_message_p) |
31f3d831 | 5740 | { |
5794dd61 RS |
5741 | /* If we are going to restore an old message, |
5742 | give time to read ours. */ | |
83f8d903 | 5743 | sit_for (make_number (1), 0, 0); |
c71106e5 | 5744 | restore_message (); |
31f3d831 | 5745 | } |
ca730bf0 | 5746 | else if (!auto_save_error_occurred) |
31e31a15 CY |
5747 | /* Don't overwrite the error message if an error occurred. |
5748 | If we displayed a message and then restored a state | |
5794dd61 | 5749 | with no message, leave a "done" message on the screen. */ |
f05b275b KH |
5750 | message1 ("Auto-saving...done"); |
5751 | } | |
570d7624 | 5752 | |
ff4c9993 RS |
5753 | Vquit_flag = oquit; |
5754 | ||
5794dd61 | 5755 | /* This restores the message-stack status. */ |
e54d3b5d | 5756 | unbind_to (count, Qnil); |
570d7624 JB |
5757 | return Qnil; |
5758 | } | |
5759 | ||
5760 | DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved, | |
8c1a1077 PJ |
5761 | Sset_buffer_auto_saved, 0, 0, 0, |
5762 | doc: /* Mark current buffer as auto-saved with its current text. | |
5763 | No auto-save file will be written until the buffer changes again. */) | |
5842a27b | 5764 | (void) |
570d7624 | 5765 | { |
0b5397c2 SM |
5766 | /* FIXME: This should not be called in indirect buffers, since |
5767 | they're not autosaved. */ | |
5768 | BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF; | |
4b4deea2 | 5769 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
d311d28c | 5770 | current_buffer->auto_save_failure_time = 0; |
b60247d9 RS |
5771 | return Qnil; |
5772 | } | |
5773 | ||
5774 | DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, | |
8c1a1077 PJ |
5775 | Sclear_buffer_auto_save_failure, 0, 0, 0, |
5776 | doc: /* Clear any record of a recent auto-save failure in the current buffer. */) | |
5842a27b | 5777 | (void) |
b60247d9 | 5778 | { |
d311d28c | 5779 | current_buffer->auto_save_failure_time = 0; |
570d7624 JB |
5780 | return Qnil; |
5781 | } | |
5782 | ||
5783 | DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p, | |
8c1a1077 | 5784 | 0, 0, 0, |
68780e2a RS |
5785 | doc: /* Return t if current buffer has been auto-saved recently. |
5786 | More precisely, if it has been auto-saved since last read from or saved | |
5787 | in the visited file. If the buffer has no visited file, | |
5788 | then any auto-save counts as "recent". */) | |
5842a27b | 5789 | (void) |
570d7624 | 5790 | { |
0b5397c2 SM |
5791 | /* FIXME: maybe we should return nil for indirect buffers since |
5792 | they're never autosaved. */ | |
5793 | return (SAVE_MODIFF < BUF_AUTOSAVE_MODIFF (current_buffer) ? Qt : Qnil); | |
570d7624 JB |
5794 | } |
5795 | \f | |
5796 | /* Reading and completing file names */ | |
6e710ae5 | 5797 | |
88208bb8 JD |
5798 | DEFUN ("next-read-file-uses-dialog-p", Fnext_read_file_uses_dialog_p, |
5799 | Snext_read_file_uses_dialog_p, 0, 0, 0, | |
5800 | doc: /* Return t if a call to `read-file-name' will use a dialog. | |
5801 | The return value is only relevant for a call to `read-file-name' that happens | |
1a0de25c | 5802 | before any other event (mouse or keypress) is handled. */) |
5842a27b | 5803 | (void) |
88208bb8 | 5804 | { |
d7e642cc JD |
5805 | #if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) \ |
5806 | || defined (HAVE_NS) | |
88208bb8 JD |
5807 | if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) |
5808 | && use_dialog_box | |
5809 | && use_file_dialog | |
5810 | && have_menus_p ()) | |
5811 | return Qt; | |
5812 | #endif | |
5813 | return Qnil; | |
5814 | } | |
d4a42098 | 5815 | |
dbd50d4b | 5816 | Lisp_Object |
971de7fb | 5817 | Fread_file_name (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object initial, Lisp_Object predicate) |
570d7624 | 5818 | { |
fd4ead52 | 5819 | struct gcpro gcpro1; |
dbd50d4b | 5820 | Lisp_Object args[7]; |
a79485af | 5821 | |
71e1f69d | 5822 | GCPRO1 (default_filename); |
dbd50d4b SM |
5823 | args[0] = intern ("read-file-name"); |
5824 | args[1] = prompt; | |
5825 | args[2] = dir; | |
5826 | args[3] = default_filename; | |
5827 | args[4] = mustmatch; | |
5828 | args[5] = initial; | |
5829 | args[6] = predicate; | |
5830 | RETURN_UNGCPRO (Ffuncall (7, args)); | |
570d7624 | 5831 | } |
9c856db9 | 5832 | |
570d7624 | 5833 | \f |
9fe43ff6 PE |
5834 | void |
5835 | init_fileio (void) | |
5836 | { | |
5837 | valid_timestamp_file_system = 0; | |
5838 | } | |
5839 | ||
dfcf069d | 5840 | void |
971de7fb | 5841 | syms_of_fileio (void) |
570d7624 | 5842 | { |
cd3520a4 JB |
5843 | DEFSYM (Qoperations, "operations"); |
5844 | DEFSYM (Qexpand_file_name, "expand-file-name"); | |
5845 | DEFSYM (Qsubstitute_in_file_name, "substitute-in-file-name"); | |
5846 | DEFSYM (Qdirectory_file_name, "directory-file-name"); | |
5847 | DEFSYM (Qfile_name_directory, "file-name-directory"); | |
5848 | DEFSYM (Qfile_name_nondirectory, "file-name-nondirectory"); | |
5849 | DEFSYM (Qunhandled_file_name_directory, "unhandled-file-name-directory"); | |
5850 | DEFSYM (Qfile_name_as_directory, "file-name-as-directory"); | |
5851 | DEFSYM (Qcopy_file, "copy-file"); | |
5852 | DEFSYM (Qmake_directory_internal, "make-directory-internal"); | |
5853 | DEFSYM (Qmake_directory, "make-directory"); | |
5854 | DEFSYM (Qdelete_directory_internal, "delete-directory-internal"); | |
5855 | DEFSYM (Qdelete_file, "delete-file"); | |
5856 | DEFSYM (Qrename_file, "rename-file"); | |
5857 | DEFSYM (Qadd_name_to_file, "add-name-to-file"); | |
5858 | DEFSYM (Qmake_symbolic_link, "make-symbolic-link"); | |
5859 | DEFSYM (Qfile_exists_p, "file-exists-p"); | |
5860 | DEFSYM (Qfile_executable_p, "file-executable-p"); | |
5861 | DEFSYM (Qfile_readable_p, "file-readable-p"); | |
5862 | DEFSYM (Qfile_writable_p, "file-writable-p"); | |
5863 | DEFSYM (Qfile_symlink_p, "file-symlink-p"); | |
5864 | DEFSYM (Qaccess_file, "access-file"); | |
5865 | DEFSYM (Qfile_directory_p, "file-directory-p"); | |
5866 | DEFSYM (Qfile_regular_p, "file-regular-p"); | |
5867 | DEFSYM (Qfile_accessible_directory_p, "file-accessible-directory-p"); | |
5868 | DEFSYM (Qfile_modes, "file-modes"); | |
5869 | DEFSYM (Qset_file_modes, "set-file-modes"); | |
5870 | DEFSYM (Qset_file_times, "set-file-times"); | |
5871 | DEFSYM (Qfile_selinux_context, "file-selinux-context"); | |
5872 | DEFSYM (Qset_file_selinux_context, "set-file-selinux-context"); | |
7c3d167f RF |
5873 | DEFSYM (Qfile_acl, "file-acl"); |
5874 | DEFSYM (Qset_file_acl, "set-file-acl"); | |
cd3520a4 JB |
5875 | DEFSYM (Qfile_newer_than_file_p, "file-newer-than-file-p"); |
5876 | DEFSYM (Qinsert_file_contents, "insert-file-contents"); | |
9dbda100 | 5877 | DEFSYM (Qchoose_write_coding_system, "choose-write-coding-system"); |
cd3520a4 JB |
5878 | DEFSYM (Qwrite_region, "write-region"); |
5879 | DEFSYM (Qverify_visited_file_modtime, "verify-visited-file-modtime"); | |
5880 | DEFSYM (Qset_visited_file_modtime, "set-visited-file-modtime"); | |
5881 | DEFSYM (Qauto_save_coding, "auto-save-coding"); | |
5882 | ||
5883 | DEFSYM (Qfile_name_history, "file-name-history"); | |
642ef245 | 5884 | Fset (Qfile_name_history, Qnil); |
15c65264 | 5885 | |
cd3520a4 JB |
5886 | DEFSYM (Qfile_error, "file-error"); |
5887 | DEFSYM (Qfile_already_exists, "file-already-exists"); | |
5888 | DEFSYM (Qfile_date_error, "file-date-error"); | |
5889 | DEFSYM (Qexcl, "excl"); | |
570d7624 | 5890 | |
29208e82 | 5891 | DEFVAR_LISP ("file-name-coding-system", Vfile_name_coding_system, |
fb7ada5f | 5892 | doc: /* Coding system for encoding file names. |
346ebf53 | 5893 | If it is nil, `default-file-name-coding-system' (which see) is used. */); |
b1d1b865 RS |
5894 | Vfile_name_coding_system = Qnil; |
5895 | ||
cd913586 | 5896 | DEFVAR_LISP ("default-file-name-coding-system", |
29208e82 | 5897 | Vdefault_file_name_coding_system, |
8c1a1077 | 5898 | doc: /* Default coding system for encoding file names. |
346ebf53 | 5899 | This variable is used only when `file-name-coding-system' is nil. |
8c1a1077 | 5900 | |
346ebf53 | 5901 | This variable is set/changed by the command `set-language-environment'. |
8c1a1077 | 5902 | User should not set this variable manually, |
346ebf53 | 5903 | instead use `file-name-coding-system' to get a constant encoding |
8c1a1077 | 5904 | of file names regardless of the current language environment. */); |
cd913586 KH |
5905 | Vdefault_file_name_coding_system = Qnil; |
5906 | ||
cd3520a4 JB |
5907 | DEFSYM (Qformat_decode, "format-decode"); |
5908 | DEFSYM (Qformat_annotate_function, "format-annotate-function"); | |
5909 | DEFSYM (Qafter_insert_file_set_coding, "after-insert-file-set-coding"); | |
5910 | DEFSYM (Qcar_less_than_car, "car-less-than-car"); | |
d6a3cc15 | 5911 | |
570d7624 | 5912 | Fput (Qfile_error, Qerror_conditions, |
d67b4f80 | 5913 | Fpurecopy (list2 (Qfile_error, Qerror))); |
570d7624 | 5914 | Fput (Qfile_error, Qerror_message, |
2a0213a6 | 5915 | build_pure_c_string ("File error")); |
570d7624 JB |
5916 | |
5917 | Fput (Qfile_already_exists, Qerror_conditions, | |
d67b4f80 | 5918 | Fpurecopy (list3 (Qfile_already_exists, Qfile_error, Qerror))); |
570d7624 | 5919 | Fput (Qfile_already_exists, Qerror_message, |
2a0213a6 | 5920 | build_pure_c_string ("File already exists")); |
570d7624 | 5921 | |
c0b7b21c | 5922 | Fput (Qfile_date_error, Qerror_conditions, |
d67b4f80 | 5923 | Fpurecopy (list3 (Qfile_date_error, Qfile_error, Qerror))); |
c0b7b21c | 5924 | Fput (Qfile_date_error, Qerror_message, |
2a0213a6 | 5925 | build_pure_c_string ("Cannot set file date")); |
c0b7b21c | 5926 | |
29208e82 | 5927 | DEFVAR_LISP ("file-name-handler-alist", Vfile_name_handler_alist, |
a59225b1 CY |
5928 | doc: /* Alist of elements (REGEXP . HANDLER) for file names handled specially. |
5929 | If a file name matches REGEXP, all I/O on that file is done by calling | |
5930 | HANDLER. If a file name matches more than one handler, the handler | |
5931 | whose match starts last in the file name gets precedence. The | |
5932 | function `find-file-name-handler' checks this list for a handler for | |
5933 | its argument. | |
5934 | ||
5935 | HANDLER should be a function. The first argument given to it is the | |
5936 | name of the I/O primitive to be handled; the remaining arguments are | |
5937 | the arguments that were passed to that primitive. For example, if you | |
5938 | do (file-exists-p FILENAME) and FILENAME is handled by HANDLER, then | |
5939 | HANDLER is called like this: | |
5940 | ||
5941 | (funcall HANDLER 'file-exists-p FILENAME) | |
5942 | ||
5943 | Note that HANDLER must be able to handle all I/O primitives; if it has | |
5944 | nothing special to do for a primitive, it should reinvoke the | |
5945 | primitive to handle the operation \"the usual way\". | |
5946 | See Info node `(elisp)Magic File Names' for more details. */); | |
09121adc RS |
5947 | Vfile_name_handler_alist = Qnil; |
5948 | ||
0414b394 | 5949 | DEFVAR_LISP ("set-auto-coding-function", |
29208e82 | 5950 | Vset_auto_coding_function, |
8c1a1077 PJ |
5951 | doc: /* If non-nil, a function to call to decide a coding system of file. |
5952 | Two arguments are passed to this function: the file name | |
5953 | and the length of a file contents following the point. | |
5954 | This function should return a coding system to decode the file contents. | |
5955 | It should check the file name against `auto-coding-alist'. | |
5956 | If no coding system is decided, it should check a coding system | |
5957 | specified in the heading lines with the format: | |
5958 | -*- ... coding: CODING-SYSTEM; ... -*- | |
5959 | or local variable spec of the tailing lines with `coding:' tag. */); | |
0414b394 | 5960 | Vset_auto_coding_function = Qnil; |
c9e82392 | 5961 | |
29208e82 | 5962 | DEFVAR_LISP ("after-insert-file-functions", Vafter_insert_file_functions, |
8c1a1077 | 5963 | doc: /* A list of functions to be called at the end of `insert-file-contents'. |
0cf9f5b5 RS |
5964 | Each is passed one argument, the number of characters inserted, |
5965 | with point at the start of the inserted text. Each function | |
5966 | should leave point the same, and return the new character count. | |
cf6d2357 RS |
5967 | If `insert-file-contents' is intercepted by a handler from |
5968 | `file-name-handler-alist', that handler is responsible for calling the | |
5969 | functions in `after-insert-file-functions' if appropriate. */); | |
d6a3cc15 RS |
5970 | Vafter_insert_file_functions = Qnil; |
5971 | ||
29208e82 | 5972 | DEFVAR_LISP ("write-region-annotate-functions", Vwrite_region_annotate_functions, |
8c1a1077 PJ |
5973 | doc: /* A list of functions to be called at the start of `write-region'. |
5974 | Each is passed two arguments, START and END as for `write-region'. | |
5975 | These are usually two numbers but not always; see the documentation | |
5976 | for `write-region'. The function should return a list of pairs | |
5977 | of the form (POSITION . STRING), consisting of strings to be effectively | |
5978 | inserted at the specified positions of the file being written (1 means to | |
5979 | insert before the first byte written). The POSITIONs must be sorted into | |
67fbc0cb CY |
5980 | increasing order. |
5981 | ||
5982 | If there are several annotation functions, the lists returned by these | |
5983 | functions are merged destructively. As each annotation function runs, | |
5984 | the variable `write-region-annotations-so-far' contains a list of all | |
5985 | annotations returned by previous annotation functions. | |
5986 | ||
5987 | An annotation function can return with a different buffer current. | |
5988 | Doing so removes the annotations returned by previous functions, and | |
5989 | resets START and END to `point-min' and `point-max' of the new buffer. | |
5990 | ||
5991 | After `write-region' completes, Emacs calls the function stored in | |
5992 | `write-region-post-annotation-function', once for each buffer that was | |
5993 | current when building the annotations (i.e., at least once), with that | |
5994 | buffer current. */); | |
d6a3cc15 | 5995 | Vwrite_region_annotate_functions = Qnil; |
cd3520a4 | 5996 | DEFSYM (Qwrite_region_annotate_functions, "write-region-annotate-functions"); |
d6a3cc15 | 5997 | |
67fbc0cb | 5998 | DEFVAR_LISP ("write-region-post-annotation-function", |
29208e82 | 5999 | Vwrite_region_post_annotation_function, |
67fbc0cb CY |
6000 | doc: /* Function to call after `write-region' completes. |
6001 | The function is called with no arguments. If one or more of the | |
6002 | annotation functions in `write-region-annotate-functions' changed the | |
6003 | current buffer, the function stored in this variable is called for | |
6004 | each of those additional buffers as well, in addition to the original | |
6005 | buffer. The relevant buffer is current during each function call. */); | |
6006 | Vwrite_region_post_annotation_function = Qnil; | |
6007 | staticpro (&Vwrite_region_annotation_buffers); | |
6008 | ||
6fc6f94b | 6009 | DEFVAR_LISP ("write-region-annotations-so-far", |
29208e82 | 6010 | Vwrite_region_annotations_so_far, |
8c1a1077 PJ |
6011 | doc: /* When an annotation function is called, this holds the previous annotations. |
6012 | These are the annotations made by other annotation functions | |
6013 | that were already called. See also `write-region-annotate-functions'. */); | |
6fc6f94b RS |
6014 | Vwrite_region_annotations_so_far = Qnil; |
6015 | ||
29208e82 | 6016 | DEFVAR_LISP ("inhibit-file-name-handlers", Vinhibit_file_name_handlers, |
8c1a1077 PJ |
6017 | doc: /* A list of file name handlers that temporarily should not be used. |
6018 | This applies only to the operation `inhibit-file-name-operation'. */); | |
82c2d839 RS |
6019 | Vinhibit_file_name_handlers = Qnil; |
6020 | ||
29208e82 | 6021 | DEFVAR_LISP ("inhibit-file-name-operation", Vinhibit_file_name_operation, |
8c1a1077 | 6022 | doc: /* The operation for which `inhibit-file-name-handlers' is applicable. */); |
a65970a0 RS |
6023 | Vinhibit_file_name_operation = Qnil; |
6024 | ||
29208e82 | 6025 | DEFVAR_LISP ("auto-save-list-file-name", Vauto_save_list_file_name, |
8c1a1077 PJ |
6026 | doc: /* File name in which we write a list of all auto save file names. |
6027 | This variable is initialized automatically from `auto-save-list-file-prefix' | |
a2458031 | 6028 | shortly after Emacs reads your init file, if you have not yet given it |
8c1a1077 | 6029 | a non-nil value. */); |
e54d3b5d RS |
6030 | Vauto_save_list_file_name = Qnil; |
6031 | ||
29208e82 | 6032 | DEFVAR_LISP ("auto-save-visited-file-name", Vauto_save_visited_file_name, |
699b53bc CY |
6033 | doc: /* Non-nil says auto-save a buffer in the file it is visiting, when practical. |
6034 | Normally auto-save files are written under other names. */); | |
6035 | Vauto_save_visited_file_name = Qnil; | |
6036 | ||
29208e82 | 6037 | DEFVAR_LISP ("auto-save-include-big-deletions", Vauto_save_include_big_deletions, |
090101cf CY |
6038 | doc: /* If non-nil, auto-save even if a large part of the text is deleted. |
6039 | If nil, deleting a substantial portion of the text disables auto-save | |
6040 | in the buffer; this is the default behavior, because the auto-save | |
6041 | file is usually more useful if it contains the deleted text. */); | |
6042 | Vauto_save_include_big_deletions = Qnil; | |
6043 | ||
ccf61795 | 6044 | #ifdef HAVE_FSYNC |
29208e82 | 6045 | DEFVAR_BOOL ("write-region-inhibit-fsync", write_region_inhibit_fsync, |
fb7ada5f | 6046 | doc: /* Non-nil means don't call fsync in `write-region'. |
e3f509dd RF |
6047 | This variable affects calls to `write-region' as well as save commands. |
6048 | A non-nil value may result in data loss! */); | |
ccf61795 RF |
6049 | write_region_inhibit_fsync = 0; |
6050 | #endif | |
6051 | ||
29208e82 | 6052 | DEFVAR_BOOL ("delete-by-moving-to-trash", delete_by_moving_to_trash, |
6cf29fe8 | 6053 | doc: /* Specifies whether to use the system's trash can. |
f1a5d776 CY |
6054 | When non-nil, certain file deletion commands use the function |
6055 | `move-file-to-trash' instead of deleting files outright. | |
6056 | This includes interactive calls to `delete-file' and | |
6057 | `delete-directory' and the Dired deletion commands. */); | |
6cf29fe8 | 6058 | delete_by_moving_to_trash = 0; |
d67b4f80 | 6059 | Qdelete_by_moving_to_trash = intern_c_string ("delete-by-moving-to-trash"); |
cd3520a4 JB |
6060 | |
6061 | DEFSYM (Qmove_file_to_trash, "move-file-to-trash"); | |
6062 | DEFSYM (Qcopy_directory, "copy-directory"); | |
6063 | DEFSYM (Qdelete_directory, "delete-directory"); | |
6cf29fe8 | 6064 | |
642ef245 | 6065 | defsubr (&Sfind_file_name_handler); |
570d7624 JB |
6066 | defsubr (&Sfile_name_directory); |
6067 | defsubr (&Sfile_name_nondirectory); | |
642ef245 | 6068 | defsubr (&Sunhandled_file_name_directory); |
570d7624 JB |
6069 | defsubr (&Sfile_name_as_directory); |
6070 | defsubr (&Sdirectory_file_name); | |
6071 | defsubr (&Smake_temp_name); | |
6072 | defsubr (&Sexpand_file_name); | |
6073 | defsubr (&Ssubstitute_in_file_name); | |
6074 | defsubr (&Scopy_file); | |
9bbe01fb | 6075 | defsubr (&Smake_directory_internal); |
9d8f3bd9 | 6076 | defsubr (&Sdelete_directory_internal); |
570d7624 JB |
6077 | defsubr (&Sdelete_file); |
6078 | defsubr (&Srename_file); | |
6079 | defsubr (&Sadd_name_to_file); | |
570d7624 | 6080 | defsubr (&Smake_symbolic_link); |
570d7624 JB |
6081 | defsubr (&Sfile_name_absolute_p); |
6082 | defsubr (&Sfile_exists_p); | |
6083 | defsubr (&Sfile_executable_p); | |
6084 | defsubr (&Sfile_readable_p); | |
6085 | defsubr (&Sfile_writable_p); | |
1f8653eb | 6086 | defsubr (&Saccess_file); |
570d7624 JB |
6087 | defsubr (&Sfile_symlink_p); |
6088 | defsubr (&Sfile_directory_p); | |
b72dea2a | 6089 | defsubr (&Sfile_accessible_directory_p); |
f793dc6c | 6090 | defsubr (&Sfile_regular_p); |
570d7624 JB |
6091 | defsubr (&Sfile_modes); |
6092 | defsubr (&Sset_file_modes); | |
819da85b | 6093 | defsubr (&Sset_file_times); |
574c05e2 | 6094 | defsubr (&Sfile_selinux_context); |
7c3d167f RF |
6095 | defsubr (&Sfile_acl); |
6096 | defsubr (&Sset_file_acl); | |
574c05e2 | 6097 | defsubr (&Sset_file_selinux_context); |
c24e9a53 RS |
6098 | defsubr (&Sset_default_file_modes); |
6099 | defsubr (&Sdefault_file_modes); | |
570d7624 JB |
6100 | defsubr (&Sfile_newer_than_file_p); |
6101 | defsubr (&Sinsert_file_contents); | |
9dbda100 | 6102 | defsubr (&Schoose_write_coding_system); |
570d7624 | 6103 | defsubr (&Swrite_region); |
d6a3cc15 | 6104 | defsubr (&Scar_less_than_car); |
570d7624 JB |
6105 | defsubr (&Sverify_visited_file_modtime); |
6106 | defsubr (&Sclear_visited_file_modtime); | |
f5d5eccf | 6107 | defsubr (&Svisited_file_modtime); |
570d7624 JB |
6108 | defsubr (&Sset_visited_file_modtime); |
6109 | defsubr (&Sdo_auto_save); | |
6110 | defsubr (&Sset_buffer_auto_saved); | |
b60247d9 | 6111 | defsubr (&Sclear_buffer_auto_save_failure); |
570d7624 JB |
6112 | defsubr (&Srecent_auto_save_p); |
6113 | ||
88208bb8 | 6114 | defsubr (&Snext_read_file_uses_dialog_p); |
85ffea93 | 6115 | |
697c17a2 | 6116 | #ifdef HAVE_SYNC |
85ffea93 | 6117 | defsubr (&Sunix_sync); |
483a2e10 | 6118 | #endif |
570d7624 | 6119 | } |