Commit | Line | Data |
---|---|---|
570d7624 | 1 | /* File IO for GNU Emacs. |
418cd8ca | 2 | Copyright (C) 1985,86,87,88,93,94,95,96,97,1998 Free Software Foundation, Inc. |
570d7624 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
4746118a | 8 | the Free Software Foundation; either version 2, or (at your option) |
570d7624 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
570d7624 | 20 | |
18160b98 | 21 | #include <config.h> |
570d7624 | 22 | |
bb369dc6 RS |
23 | #if defined (USG5) || defined (BSD_SYSTEM) || defined (LINUX) |
24 | #include <fcntl.h> | |
25 | #endif | |
26 | ||
1b335d29 | 27 | #include <stdio.h> |
570d7624 JB |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | |
bfb61299 | 30 | |
29beb080 RS |
31 | #ifdef HAVE_UNISTD_H |
32 | #include <unistd.h> | |
33 | #endif | |
34 | ||
92939d31 AS |
35 | #ifdef STDC_HEADERS |
36 | #include <stdlib.h> | |
37 | #endif | |
38 | ||
f73b0ada BF |
39 | #if !defined (S_ISLNK) && defined (S_IFLNK) |
40 | # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) | |
41 | #endif | |
42 | ||
bb369dc6 RS |
43 | #if !defined (S_ISFIFO) && defined (S_IFIFO) |
44 | # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) | |
45 | #endif | |
46 | ||
f73b0ada BF |
47 | #if !defined (S_ISREG) && defined (S_IFREG) |
48 | # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) | |
49 | #endif | |
50 | ||
bfb61299 | 51 | #ifdef VMS |
de5bf5d3 | 52 | #include "vms-pwd.h" |
bfb61299 | 53 | #else |
570d7624 | 54 | #include <pwd.h> |
bfb61299 JB |
55 | #endif |
56 | ||
570d7624 | 57 | #include <ctype.h> |
bfb61299 JB |
58 | |
59 | #ifdef VMS | |
3d9f5ce2 | 60 | #include "vmsdir.h" |
bfb61299 JB |
61 | #include <perror.h> |
62 | #include <stddef.h> | |
63 | #include <string.h> | |
bfb61299 JB |
64 | #endif |
65 | ||
570d7624 JB |
66 | #include <errno.h> |
67 | ||
bfb61299 | 68 | #ifndef vax11c |
570d7624 | 69 | extern int errno; |
570d7624 JB |
70 | #endif |
71 | ||
ce97267f | 72 | extern char *strerror (); |
570d7624 JB |
73 | |
74 | #ifdef APOLLO | |
75 | #include <sys/time.h> | |
76 | #endif | |
77 | ||
6e23c83e JB |
78 | #ifndef USG |
79 | #ifndef VMS | |
80 | #ifndef BSD4_1 | |
5e570b75 | 81 | #ifndef WINDOWSNT |
6e23c83e JB |
82 | #define HAVE_FSYNC |
83 | #endif | |
84 | #endif | |
85 | #endif | |
5e570b75 | 86 | #endif |
6e23c83e | 87 | |
570d7624 | 88 | #include "lisp.h" |
8d4e077b | 89 | #include "intervals.h" |
570d7624 | 90 | #include "buffer.h" |
6fdaa9a0 KH |
91 | #include "charset.h" |
92 | #include "coding.h" | |
570d7624 JB |
93 | #include "window.h" |
94 | ||
5e570b75 RS |
95 | #ifdef WINDOWSNT |
96 | #define NOMINMAX 1 | |
97 | #include <windows.h> | |
98 | #include <stdlib.h> | |
99 | #include <fcntl.h> | |
100 | #endif /* not WINDOWSNT */ | |
101 | ||
7990d02a EZ |
102 | #ifdef MSDOS |
103 | #include "msdos.h" | |
104 | #include <sys/param.h> | |
105 | #if __DJGPP__ >= 2 | |
106 | #include <fcntl.h> | |
107 | #include <string.h> | |
108 | #endif | |
109 | #endif | |
110 | ||
199607e4 RS |
111 | #ifdef DOS_NT |
112 | #define CORRECT_DIR_SEPS(s) \ | |
113 | do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \ | |
114 | else unixtodos_filename (s); \ | |
115 | } while (0) | |
116 | /* On Windows, drive letters must be alphabetic - on DOS, the Netware | |
117 | redirector allows the six letters between 'Z' and 'a' as well. */ | |
118 | #ifdef MSDOS | |
119 | #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z') | |
120 | #endif | |
121 | #ifdef WINDOWSNT | |
122 | #define IS_DRIVE(x) isalpha (x) | |
123 | #endif | |
f54b565c MB |
124 | /* Need to lower-case the drive letter, or else expanded |
125 | filenames will sometimes compare inequal, because | |
126 | `expand-file-name' doesn't always down-case the drive letter. */ | |
127 | #define DRIVE_LETTER(x) (tolower (x)) | |
199607e4 RS |
128 | #endif |
129 | ||
570d7624 | 130 | #ifdef VMS |
570d7624 JB |
131 | #include <file.h> |
132 | #include <rmsdef.h> | |
133 | #include <fab.h> | |
134 | #include <nam.h> | |
135 | #endif | |
136 | ||
de5bf5d3 | 137 | #include "systime.h" |
570d7624 JB |
138 | |
139 | #ifdef HPUX | |
140 | #include <netio.h> | |
9b7828a5 | 141 | #ifndef HPUX8 |
47e7b9e5 | 142 | #ifndef HPUX9 |
570d7624 JB |
143 | #include <errnet.h> |
144 | #endif | |
9b7828a5 | 145 | #endif |
47e7b9e5 | 146 | #endif |
570d7624 | 147 | |
9c856db9 GM |
148 | #include "commands.h" |
149 | extern int use_dialog_box; | |
150 | ||
570d7624 JB |
151 | #ifndef O_WRONLY |
152 | #define O_WRONLY 1 | |
153 | #endif | |
154 | ||
4018b5ef RS |
155 | #ifndef O_RDONLY |
156 | #define O_RDONLY 0 | |
157 | #endif | |
158 | ||
8f6df9b5 EZ |
159 | #ifndef S_ISLNK |
160 | # define lstat stat | |
161 | #endif | |
162 | ||
570d7624 JB |
163 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
164 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
165 | ||
166 | /* Nonzero during writing of auto-save files */ | |
167 | int auto_saving; | |
168 | ||
169 | /* Set by auto_save_1 to mode of original file so Fwrite_region will create | |
170 | a new file with the same mode as the original */ | |
171 | int auto_save_mode_bits; | |
172 | ||
b1d1b865 RS |
173 | /* Coding system for file names, or nil if none. */ |
174 | Lisp_Object Vfile_name_coding_system; | |
175 | ||
cd913586 KH |
176 | /* Coding system for file names used only when |
177 | Vfile_name_coding_system is nil. */ | |
178 | Lisp_Object Vdefault_file_name_coding_system; | |
179 | ||
199607e4 | 180 | /* Alist of elements (REGEXP . HANDLER) for file names |
32f4334d RS |
181 | whose I/O is done with a special handler. */ |
182 | Lisp_Object Vfile_name_handler_alist; | |
183 | ||
0d420e88 BG |
184 | /* Format for auto-save files */ |
185 | Lisp_Object Vauto_save_file_format; | |
186 | ||
187 | /* Lisp functions for translating file formats */ | |
188 | Lisp_Object Qformat_decode, Qformat_annotate_function; | |
189 | ||
c9e82392 | 190 | /* Function to be called to decide a coding system of a reading file. */ |
0414b394 | 191 | Lisp_Object Vset_auto_coding_function; |
c9e82392 | 192 | |
d6a3cc15 RS |
193 | /* Functions to be called to process text properties in inserted file. */ |
194 | Lisp_Object Vafter_insert_file_functions; | |
195 | ||
196 | /* Functions to be called to create text property annotations for file. */ | |
197 | Lisp_Object Vwrite_region_annotate_functions; | |
198 | ||
6fc6f94b RS |
199 | /* During build_annotations, each time an annotation function is called, |
200 | this holds the annotations made by the previous functions. */ | |
201 | Lisp_Object Vwrite_region_annotations_so_far; | |
202 | ||
e54d3b5d RS |
203 | /* File name in which we write a list of all our auto save files. */ |
204 | Lisp_Object Vauto_save_list_file_name; | |
205 | ||
570d7624 JB |
206 | /* Nonzero means, when reading a filename in the minibuffer, |
207 | start out by inserting the default directory into the minibuffer. */ | |
208 | int insert_default_directory; | |
209 | ||
210 | /* On VMS, nonzero means write new files with record format stmlf. | |
211 | Zero means use var format. */ | |
212 | int vms_stmlf_recfm; | |
213 | ||
199607e4 RS |
214 | /* On NT, specifies the directory separator character, used (eg.) when |
215 | expanding file names. This can be bound to / or \. */ | |
216 | Lisp_Object Vdirectory_sep_char; | |
217 | ||
84f6296a RS |
218 | extern Lisp_Object Vuser_login_name; |
219 | ||
c1c4693e RS |
220 | #ifdef WINDOWSNT |
221 | extern Lisp_Object Vw32_get_true_file_attributes; | |
222 | #endif | |
223 | ||
84f6296a RS |
224 | extern int minibuf_level; |
225 | ||
a8c828be RS |
226 | extern int minibuffer_auto_raise; |
227 | ||
a65970a0 RS |
228 | /* These variables describe handlers that have "already" had a chance |
229 | to handle the current operation. | |
230 | ||
231 | Vinhibit_file_name_handlers is a list of file name handlers. | |
232 | Vinhibit_file_name_operation is the operation being handled. | |
233 | If we try to handle that operation, we ignore those handlers. */ | |
234 | ||
82c2d839 | 235 | static Lisp_Object Vinhibit_file_name_handlers; |
a65970a0 | 236 | static Lisp_Object Vinhibit_file_name_operation; |
82c2d839 | 237 | |
c0b7b21c | 238 | Lisp_Object Qfile_error, Qfile_already_exists, Qfile_date_error; |
570d7624 | 239 | |
15c65264 RS |
240 | Lisp_Object Qfile_name_history; |
241 | ||
d6a3cc15 RS |
242 | Lisp_Object Qcar_less_than_car; |
243 | ||
ec7adf26 | 244 | static int a_write P_ ((int, char *, int, int, |
b9013200 | 245 | Lisp_Object *, struct coding_system *)); |
ec7adf26 RS |
246 | static int e_write P_ ((int, char *, int, struct coding_system *)); |
247 | \f | |
5d01d666 | 248 | void |
570d7624 JB |
249 | report_file_error (string, data) |
250 | char *string; | |
251 | Lisp_Object data; | |
252 | { | |
253 | Lisp_Object errstring; | |
254 | ||
a1f17b2d | 255 | errstring = build_string (strerror (errno)); |
570d7624 JB |
256 | |
257 | /* System error messages are capitalized. Downcase the initial | |
258 | unless it is followed by a slash. */ | |
259 | if (XSTRING (errstring)->data[1] != '/') | |
260 | XSTRING (errstring)->data[0] = DOWNCASE (XSTRING (errstring)->data[0]); | |
261 | ||
262 | while (1) | |
263 | Fsignal (Qfile_error, | |
264 | Fcons (build_string (string), Fcons (errstring, data))); | |
265 | } | |
b5148e85 | 266 | |
b27a1703 | 267 | Lisp_Object |
b5148e85 RS |
268 | close_file_unwind (fd) |
269 | Lisp_Object fd; | |
270 | { | |
271 | close (XFASTINT (fd)); | |
b27a1703 | 272 | return Qnil; |
b5148e85 | 273 | } |
a1d2b64a RS |
274 | |
275 | /* Restore point, having saved it as a marker. */ | |
276 | ||
ec7adf26 | 277 | static Lisp_Object |
a1d2b64a | 278 | restore_point_unwind (location) |
199607e4 | 279 | Lisp_Object location; |
a1d2b64a | 280 | { |
ec7adf26 | 281 | Fgoto_char (location); |
a1d2b64a | 282 | Fset_marker (location, Qnil, Qnil); |
b27a1703 | 283 | return Qnil; |
a1d2b64a | 284 | } |
570d7624 | 285 | \f |
0bf2eed2 | 286 | Lisp_Object Qexpand_file_name; |
273e0829 | 287 | Lisp_Object Qsubstitute_in_file_name; |
0bf2eed2 RS |
288 | Lisp_Object Qdirectory_file_name; |
289 | Lisp_Object Qfile_name_directory; | |
290 | Lisp_Object Qfile_name_nondirectory; | |
642ef245 | 291 | Lisp_Object Qunhandled_file_name_directory; |
0bf2eed2 | 292 | Lisp_Object Qfile_name_as_directory; |
32f4334d | 293 | Lisp_Object Qcopy_file; |
a6e6e718 | 294 | Lisp_Object Qmake_directory_internal; |
32f4334d RS |
295 | Lisp_Object Qdelete_directory; |
296 | Lisp_Object Qdelete_file; | |
297 | Lisp_Object Qrename_file; | |
298 | Lisp_Object Qadd_name_to_file; | |
299 | Lisp_Object Qmake_symbolic_link; | |
300 | Lisp_Object Qfile_exists_p; | |
301 | Lisp_Object Qfile_executable_p; | |
302 | Lisp_Object Qfile_readable_p; | |
32f4334d | 303 | Lisp_Object Qfile_writable_p; |
1f8653eb RS |
304 | Lisp_Object Qfile_symlink_p; |
305 | Lisp_Object Qaccess_file; | |
32f4334d | 306 | Lisp_Object Qfile_directory_p; |
adedc71d | 307 | Lisp_Object Qfile_regular_p; |
32f4334d RS |
308 | Lisp_Object Qfile_accessible_directory_p; |
309 | Lisp_Object Qfile_modes; | |
310 | Lisp_Object Qset_file_modes; | |
311 | Lisp_Object Qfile_newer_than_file_p; | |
312 | Lisp_Object Qinsert_file_contents; | |
313 | Lisp_Object Qwrite_region; | |
314 | Lisp_Object Qverify_visited_file_modtime; | |
3ec46acd | 315 | Lisp_Object Qset_visited_file_modtime; |
32f4334d | 316 | |
49307295 KH |
317 | DEFUN ("find-file-name-handler", Ffind_file_name_handler, Sfind_file_name_handler, 2, 2, 0, |
318 | "Return FILENAME's handler function for OPERATION, if it has one.\n\ | |
642ef245 JB |
319 | Otherwise, return nil.\n\ |
320 | A file name is handled if one of the regular expressions in\n\ | |
82c2d839 | 321 | `file-name-handler-alist' matches it.\n\n\ |
a65970a0 RS |
322 | If OPERATION equals `inhibit-file-name-operation', then we ignore\n\ |
323 | any handlers that are members of `inhibit-file-name-handlers',\n\ | |
324 | but we still do run any other handlers. This lets handlers\n\ | |
82c2d839 | 325 | use the standard functions without calling themselves recursively.") |
49307295 KH |
326 | (filename, operation) |
327 | Lisp_Object filename, operation; | |
32f4334d | 328 | { |
642ef245 | 329 | /* This function must not munge the match data. */ |
a65970a0 | 330 | Lisp_Object chain, inhibited_handlers; |
642ef245 | 331 | |
e4432095 JB |
332 | CHECK_STRING (filename, 0); |
333 | ||
a65970a0 RS |
334 | if (EQ (operation, Vinhibit_file_name_operation)) |
335 | inhibited_handlers = Vinhibit_file_name_handlers; | |
336 | else | |
337 | inhibited_handlers = Qnil; | |
82c2d839 | 338 | |
93c30b5f | 339 | for (chain = Vfile_name_handler_alist; CONSP (chain); |
32f4334d RS |
340 | chain = XCONS (chain)->cdr) |
341 | { | |
342 | Lisp_Object elt; | |
343 | elt = XCONS (chain)->car; | |
93c30b5f | 344 | if (CONSP (elt)) |
32f4334d RS |
345 | { |
346 | Lisp_Object string; | |
347 | string = XCONS (elt)->car; | |
93c30b5f | 348 | if (STRINGP (string) && fast_string_match (string, filename) >= 0) |
a65970a0 RS |
349 | { |
350 | Lisp_Object handler, tem; | |
351 | ||
352 | handler = XCONS (elt)->cdr; | |
353 | tem = Fmemq (handler, inhibited_handlers); | |
354 | if (NILP (tem)) | |
355 | return handler; | |
356 | } | |
32f4334d | 357 | } |
642ef245 JB |
358 | |
359 | QUIT; | |
32f4334d RS |
360 | } |
361 | return Qnil; | |
362 | } | |
363 | \f | |
570d7624 JB |
364 | DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, |
365 | 1, 1, 0, | |
3b7f6e60 EN |
366 | "Return the directory component in file name FILENAME.\n\ |
367 | Return nil if FILENAME does not include a directory.\n\ | |
570d7624 JB |
368 | Otherwise return a directory spec.\n\ |
369 | Given a Unix syntax file name, returns a string ending in slash;\n\ | |
370 | on VMS, perhaps instead a string ending in `:', `]' or `>'.") | |
3b7f6e60 EN |
371 | (filename) |
372 | Lisp_Object filename; | |
570d7624 JB |
373 | { |
374 | register unsigned char *beg; | |
375 | register unsigned char *p; | |
0bf2eed2 | 376 | Lisp_Object handler; |
570d7624 | 377 | |
3b7f6e60 | 378 | CHECK_STRING (filename, 0); |
570d7624 | 379 | |
0bf2eed2 RS |
380 | /* If the file name has special constructs in it, |
381 | call the corresponding file handler. */ | |
3b7f6e60 | 382 | handler = Ffind_file_name_handler (filename, Qfile_name_directory); |
0bf2eed2 | 383 | if (!NILP (handler)) |
3b7f6e60 | 384 | return call2 (handler, Qfile_name_directory, filename); |
0bf2eed2 | 385 | |
4c3c22f3 | 386 | #ifdef FILE_SYSTEM_CASE |
3b7f6e60 | 387 | filename = FILE_SYSTEM_CASE (filename); |
4c3c22f3 | 388 | #endif |
3b7f6e60 | 389 | beg = XSTRING (filename)->data; |
199607e4 RS |
390 | #ifdef DOS_NT |
391 | beg = strcpy (alloca (strlen (beg) + 1), beg); | |
392 | #endif | |
fc932ac6 | 393 | p = beg + STRING_BYTES (XSTRING (filename)); |
570d7624 | 394 | |
199607e4 | 395 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
570d7624 JB |
396 | #ifdef VMS |
397 | && p[-1] != ':' && p[-1] != ']' && p[-1] != '>' | |
398 | #endif /* VMS */ | |
199607e4 | 399 | #ifdef DOS_NT |
ba14e174 RS |
400 | /* only recognise drive specifier at the beginning */ |
401 | && !(p[-1] == ':' | |
402 | /* handle the "/:d:foo" and "/:foo" cases correctly */ | |
403 | && ((p == beg + 2 && !IS_DIRECTORY_SEP (*beg)) | |
404 | || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 405 | #endif |
570d7624 JB |
406 | ) p--; |
407 | ||
408 | if (p == beg) | |
409 | return Qnil; | |
5e570b75 | 410 | #ifdef DOS_NT |
4c3c22f3 | 411 | /* Expansion of "c:" to drive and default directory. */ |
ba14e174 | 412 | if (p[-1] == ':') |
4c3c22f3 | 413 | { |
4c3c22f3 | 414 | /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */ |
199607e4 | 415 | unsigned char *res = alloca (MAXPATHLEN + 1); |
ba14e174 RS |
416 | unsigned char *r = res; |
417 | ||
418 | if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':') | |
419 | { | |
420 | strncpy (res, beg, 2); | |
421 | beg += 2; | |
422 | r += 2; | |
423 | } | |
424 | ||
425 | if (getdefdir (toupper (*beg) - 'A' + 1, r)) | |
4c3c22f3 | 426 | { |
199607e4 | 427 | if (!IS_DIRECTORY_SEP (res[strlen (res) - 1])) |
4c3c22f3 RS |
428 | strcat (res, "/"); |
429 | beg = res; | |
430 | p = beg + strlen (beg); | |
431 | } | |
432 | } | |
199607e4 | 433 | CORRECT_DIR_SEPS (beg); |
5e570b75 | 434 | #endif /* DOS_NT */ |
60d67b83 RS |
435 | |
436 | if (STRING_MULTIBYTE (filename)) | |
437 | return make_string (beg, p - beg); | |
438 | return make_unibyte_string (beg, p - beg); | |
570d7624 JB |
439 | } |
440 | ||
60d67b83 RS |
441 | DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, |
442 | Sfile_name_nondirectory, 1, 1, 0, | |
3b7f6e60 | 443 | "Return file name FILENAME sans its directory.\n\ |
570d7624 JB |
444 | For example, in a Unix-syntax file name,\n\ |
445 | this is everything after the last slash,\n\ | |
446 | or the entire name if it contains no slash.") | |
3b7f6e60 EN |
447 | (filename) |
448 | Lisp_Object filename; | |
570d7624 JB |
449 | { |
450 | register unsigned char *beg, *p, *end; | |
0bf2eed2 | 451 | Lisp_Object handler; |
570d7624 | 452 | |
3b7f6e60 | 453 | CHECK_STRING (filename, 0); |
570d7624 | 454 | |
0bf2eed2 RS |
455 | /* If the file name has special constructs in it, |
456 | call the corresponding file handler. */ | |
3b7f6e60 | 457 | handler = Ffind_file_name_handler (filename, Qfile_name_nondirectory); |
0bf2eed2 | 458 | if (!NILP (handler)) |
3b7f6e60 | 459 | return call2 (handler, Qfile_name_nondirectory, filename); |
0bf2eed2 | 460 | |
3b7f6e60 | 461 | beg = XSTRING (filename)->data; |
fc932ac6 | 462 | end = p = beg + STRING_BYTES (XSTRING (filename)); |
570d7624 | 463 | |
199607e4 | 464 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
570d7624 JB |
465 | #ifdef VMS |
466 | && p[-1] != ':' && p[-1] != ']' && p[-1] != '>' | |
467 | #endif /* VMS */ | |
199607e4 RS |
468 | #ifdef DOS_NT |
469 | /* only recognise drive specifier at beginning */ | |
ba14e174 RS |
470 | && !(p[-1] == ':' |
471 | /* handle the "/:d:foo" case correctly */ | |
472 | && (p == beg + 2 || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 473 | #endif |
60d67b83 RS |
474 | ) |
475 | p--; | |
570d7624 | 476 | |
60d67b83 RS |
477 | if (STRING_MULTIBYTE (filename)) |
478 | return make_string (p, end - p); | |
479 | return make_unibyte_string (p, end - p); | |
570d7624 | 480 | } |
642ef245 | 481 | |
60d67b83 RS |
482 | DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, |
483 | Sunhandled_file_name_directory, 1, 1, 0, | |
642ef245 JB |
484 | "Return a directly usable directory name somehow associated with FILENAME.\n\ |
485 | A `directly usable' directory name is one that may be used without the\n\ | |
486 | intervention of any file handler.\n\ | |
487 | If FILENAME is a directly usable file itself, return\n\ | |
ec7adf26 | 488 | \(file-name-directory FILENAME).\n\ |
642ef245 JB |
489 | The `call-process' and `start-process' functions use this function to\n\ |
490 | get a current directory to run processes in.") | |
491 | (filename) | |
492 | Lisp_Object filename; | |
493 | { | |
494 | Lisp_Object handler; | |
495 | ||
496 | /* If the file name has special constructs in it, | |
497 | call the corresponding file handler. */ | |
49307295 | 498 | handler = Ffind_file_name_handler (filename, Qunhandled_file_name_directory); |
642ef245 JB |
499 | if (!NILP (handler)) |
500 | return call2 (handler, Qunhandled_file_name_directory, filename); | |
501 | ||
502 | return Ffile_name_directory (filename); | |
503 | } | |
504 | ||
570d7624 JB |
505 | \f |
506 | char * | |
507 | file_name_as_directory (out, in) | |
508 | char *out, *in; | |
509 | { | |
510 | int size = strlen (in) - 1; | |
511 | ||
512 | strcpy (out, in); | |
513 | ||
8aa3a244 RS |
514 | if (size < 0) |
515 | { | |
154a307d KH |
516 | out[0] = '.'; |
517 | out[1] = '/'; | |
518 | out[2] = 0; | |
8aa3a244 RS |
519 | return out; |
520 | } | |
521 | ||
570d7624 JB |
522 | #ifdef VMS |
523 | /* Is it already a directory string? */ | |
524 | if (in[size] == ':' || in[size] == ']' || in[size] == '>') | |
525 | return out; | |
526 | /* Is it a VMS directory file name? If so, hack VMS syntax. */ | |
527 | else if (! index (in, '/') | |
528 | && ((size > 3 && ! strcmp (&in[size - 3], ".DIR")) | |
529 | || (size > 3 && ! strcmp (&in[size - 3], ".dir")) | |
530 | || (size > 5 && (! strncmp (&in[size - 5], ".DIR", 4) | |
531 | || ! strncmp (&in[size - 5], ".dir", 4)) | |
532 | && (in[size - 1] == '.' || in[size - 1] == ';') | |
533 | && in[size] == '1'))) | |
534 | { | |
535 | register char *p, *dot; | |
536 | char brack; | |
537 | ||
538 | /* x.dir -> [.x] | |
539 | dir:x.dir --> dir:[x] | |
540 | dir:[x]y.dir --> dir:[x.y] */ | |
541 | p = in + size; | |
542 | while (p != in && *p != ':' && *p != '>' && *p != ']') p--; | |
543 | if (p != in) | |
544 | { | |
545 | strncpy (out, in, p - in); | |
546 | out[p - in] = '\0'; | |
547 | if (*p == ':') | |
548 | { | |
549 | brack = ']'; | |
550 | strcat (out, ":["); | |
551 | } | |
552 | else | |
553 | { | |
554 | brack = *p; | |
555 | strcat (out, "."); | |
556 | } | |
557 | p++; | |
558 | } | |
559 | else | |
560 | { | |
561 | brack = ']'; | |
562 | strcpy (out, "[."); | |
563 | } | |
bfb61299 JB |
564 | dot = index (p, '.'); |
565 | if (dot) | |
570d7624 JB |
566 | { |
567 | /* blindly remove any extension */ | |
568 | size = strlen (out) + (dot - p); | |
569 | strncat (out, p, dot - p); | |
570 | } | |
571 | else | |
572 | { | |
573 | strcat (out, p); | |
574 | size = strlen (out); | |
575 | } | |
576 | out[size++] = brack; | |
577 | out[size] = '\0'; | |
578 | } | |
579 | #else /* not VMS */ | |
580 | /* For Unix syntax, Append a slash if necessary */ | |
199607e4 | 581 | if (!IS_DIRECTORY_SEP (out[size])) |
5e570b75 RS |
582 | { |
583 | out[size + 1] = DIRECTORY_SEP; | |
584 | out[size + 2] = '\0'; | |
585 | } | |
199607e4 RS |
586 | #ifdef DOS_NT |
587 | CORRECT_DIR_SEPS (out); | |
588 | #endif | |
570d7624 JB |
589 | #endif /* not VMS */ |
590 | return out; | |
591 | } | |
592 | ||
593 | DEFUN ("file-name-as-directory", Ffile_name_as_directory, | |
594 | Sfile_name_as_directory, 1, 1, 0, | |
595 | "Return a string representing file FILENAME interpreted as a directory.\n\ | |
596 | This operation exists because a directory is also a file, but its name as\n\ | |
597 | a directory is different from its name as a file.\n\ | |
598 | The result can be used as the value of `default-directory'\n\ | |
599 | or passed as second argument to `expand-file-name'.\n\ | |
600 | For a Unix-syntax file name, just appends a slash.\n\ | |
601 | On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc.") | |
602 | (file) | |
603 | Lisp_Object file; | |
604 | { | |
605 | char *buf; | |
0bf2eed2 | 606 | Lisp_Object handler; |
570d7624 JB |
607 | |
608 | CHECK_STRING (file, 0); | |
265a9e55 | 609 | if (NILP (file)) |
570d7624 | 610 | return Qnil; |
0bf2eed2 RS |
611 | |
612 | /* If the file name has special constructs in it, | |
613 | call the corresponding file handler. */ | |
49307295 | 614 | handler = Ffind_file_name_handler (file, Qfile_name_as_directory); |
0bf2eed2 RS |
615 | if (!NILP (handler)) |
616 | return call2 (handler, Qfile_name_as_directory, file); | |
617 | ||
fc932ac6 | 618 | buf = (char *) alloca (STRING_BYTES (XSTRING (file)) + 10); |
570d7624 JB |
619 | return build_string (file_name_as_directory (buf, XSTRING (file)->data)); |
620 | } | |
621 | \f | |
622 | /* | |
623 | * Convert from directory name to filename. | |
624 | * On VMS: | |
625 | * xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1 | |
626 | * xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1 | |
199607e4 | 627 | * On UNIX, it's simple: just make sure there isn't a terminating / |
570d7624 JB |
628 | |
629 | * Value is nonzero if the string output is different from the input. | |
630 | */ | |
631 | ||
dfcf069d | 632 | int |
570d7624 JB |
633 | directory_file_name (src, dst) |
634 | char *src, *dst; | |
635 | { | |
636 | long slen; | |
637 | #ifdef VMS | |
638 | long rlen; | |
639 | char * ptr, * rptr; | |
640 | char bracket; | |
641 | struct FAB fab = cc$rms_fab; | |
642 | struct NAM nam = cc$rms_nam; | |
643 | char esa[NAM$C_MAXRSS]; | |
644 | #endif /* VMS */ | |
645 | ||
646 | slen = strlen (src); | |
647 | #ifdef VMS | |
648 | if (! index (src, '/') | |
649 | && (src[slen - 1] == ']' | |
650 | || src[slen - 1] == ':' | |
651 | || src[slen - 1] == '>')) | |
652 | { | |
653 | /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */ | |
654 | fab.fab$l_fna = src; | |
655 | fab.fab$b_fns = slen; | |
656 | fab.fab$l_nam = &nam; | |
657 | fab.fab$l_fop = FAB$M_NAM; | |
658 | ||
659 | nam.nam$l_esa = esa; | |
660 | nam.nam$b_ess = sizeof esa; | |
661 | nam.nam$b_nop |= NAM$M_SYNCHK; | |
662 | ||
663 | /* We call SYS$PARSE to handle such things as [--] for us. */ | |
199607e4 | 664 | if (SYS$PARSE (&fab, 0, 0) == RMS$_NORMAL) |
570d7624 JB |
665 | { |
666 | slen = nam.nam$b_esl; | |
667 | if (esa[slen - 1] == ';' && esa[slen - 2] == '.') | |
668 | slen -= 2; | |
669 | esa[slen] = '\0'; | |
670 | src = esa; | |
671 | } | |
672 | if (src[slen - 1] != ']' && src[slen - 1] != '>') | |
673 | { | |
674 | /* what about when we have logical_name:???? */ | |
675 | if (src[slen - 1] == ':') | |
5e570b75 | 676 | { /* Xlate logical name and see what we get */ |
570d7624 JB |
677 | ptr = strcpy (dst, src); /* upper case for getenv */ |
678 | while (*ptr) | |
679 | { | |
680 | if ('a' <= *ptr && *ptr <= 'z') | |
681 | *ptr -= 040; | |
682 | ptr++; | |
683 | } | |
5e570b75 | 684 | dst[slen - 1] = 0; /* remove colon */ |
570d7624 JB |
685 | if (!(src = egetenv (dst))) |
686 | return 0; | |
687 | /* should we jump to the beginning of this procedure? | |
688 | Good points: allows us to use logical names that xlate | |
689 | to Unix names, | |
690 | Bad points: can be a problem if we just translated to a device | |
691 | name... | |
692 | For now, I'll punt and always expect VMS names, and hope for | |
693 | the best! */ | |
694 | slen = strlen (src); | |
695 | if (src[slen - 1] != ']' && src[slen - 1] != '>') | |
696 | { /* no recursion here! */ | |
697 | strcpy (dst, src); | |
698 | return 0; | |
699 | } | |
700 | } | |
701 | else | |
5e570b75 | 702 | { /* not a directory spec */ |
570d7624 JB |
703 | strcpy (dst, src); |
704 | return 0; | |
705 | } | |
706 | } | |
707 | bracket = src[slen - 1]; | |
708 | ||
709 | /* If bracket is ']' or '>', bracket - 2 is the corresponding | |
710 | opening bracket. */ | |
bfb61299 JB |
711 | ptr = index (src, bracket - 2); |
712 | if (ptr == 0) | |
570d7624 JB |
713 | { /* no opening bracket */ |
714 | strcpy (dst, src); | |
715 | return 0; | |
716 | } | |
717 | if (!(rptr = rindex (src, '.'))) | |
718 | rptr = ptr; | |
719 | slen = rptr - src; | |
720 | strncpy (dst, src, slen); | |
721 | dst[slen] = '\0'; | |
722 | if (*rptr == '.') | |
723 | { | |
724 | dst[slen++] = bracket; | |
725 | dst[slen] = '\0'; | |
726 | } | |
727 | else | |
728 | { | |
729 | /* If we have the top-level of a rooted directory (i.e. xx:[000000]), | |
730 | then translate the device and recurse. */ | |
731 | if (dst[slen - 1] == ':' | |
5e570b75 | 732 | && dst[slen - 2] != ':' /* skip decnet nodes */ |
199607e4 | 733 | && strcmp (src + slen, "[000000]") == 0) |
570d7624 JB |
734 | { |
735 | dst[slen - 1] = '\0'; | |
736 | if ((ptr = egetenv (dst)) | |
737 | && (rlen = strlen (ptr) - 1) > 0 | |
738 | && (ptr[rlen] == ']' || ptr[rlen] == '>') | |
739 | && ptr[rlen - 1] == '.') | |
740 | { | |
72b21817 RS |
741 | char * buf = (char *) alloca (strlen (ptr) + 1); |
742 | strcpy (buf, ptr); | |
743 | buf[rlen - 1] = ']'; | |
744 | buf[rlen] = '\0'; | |
745 | return directory_file_name (buf, dst); | |
570d7624 JB |
746 | } |
747 | else | |
748 | dst[slen - 1] = ':'; | |
749 | } | |
750 | strcat (dst, "[000000]"); | |
751 | slen += 8; | |
752 | } | |
753 | rptr++; | |
754 | rlen = strlen (rptr) - 1; | |
755 | strncat (dst, rptr, rlen); | |
756 | dst[slen + rlen] = '\0'; | |
757 | strcat (dst, ".DIR.1"); | |
758 | return 1; | |
759 | } | |
760 | #endif /* VMS */ | |
761 | /* Process as Unix format: just remove any final slash. | |
762 | But leave "/" unchanged; do not change it to "". */ | |
763 | strcpy (dst, src); | |
125feee8 RS |
764 | #ifdef APOLLO |
765 | /* Handle // as root for apollo's. */ | |
766 | if ((slen > 2 && dst[slen - 1] == '/') | |
767 | || (slen > 1 && dst[0] != '/' && dst[slen - 1] == '/')) | |
768 | dst[slen - 1] = 0; | |
769 | #else | |
199607e4 | 770 | if (slen > 1 |
5e570b75 | 771 | && IS_DIRECTORY_SEP (dst[slen - 1]) |
4592782e RS |
772 | #ifdef DOS_NT |
773 | && !IS_ANY_SEP (dst[slen - 2]) | |
774 | #endif | |
775 | ) | |
570d7624 | 776 | dst[slen - 1] = 0; |
199607e4 RS |
777 | #endif |
778 | #ifdef DOS_NT | |
779 | CORRECT_DIR_SEPS (dst); | |
125feee8 | 780 | #endif |
570d7624 JB |
781 | return 1; |
782 | } | |
783 | ||
784 | DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name, | |
785 | 1, 1, 0, | |
3b7f6e60 EN |
786 | "Returns the file name of the directory named DIRECTORY.\n\ |
787 | This is the name of the file that holds the data for the directory DIRECTORY.\n\ | |
570d7624 JB |
788 | This operation exists because a directory is also a file, but its name as\n\ |
789 | a directory is different from its name as a file.\n\ | |
790 | In Unix-syntax, this function just removes the final slash.\n\ | |
791 | On VMS, given a VMS-syntax directory name such as \"[X.Y]\",\n\ | |
792 | it returns a file name such as \"[X]Y.DIR.1\".") | |
793 | (directory) | |
794 | Lisp_Object directory; | |
795 | { | |
796 | char *buf; | |
0bf2eed2 | 797 | Lisp_Object handler; |
570d7624 JB |
798 | |
799 | CHECK_STRING (directory, 0); | |
800 | ||
265a9e55 | 801 | if (NILP (directory)) |
570d7624 | 802 | return Qnil; |
0bf2eed2 RS |
803 | |
804 | /* If the file name has special constructs in it, | |
805 | call the corresponding file handler. */ | |
49307295 | 806 | handler = Ffind_file_name_handler (directory, Qdirectory_file_name); |
0bf2eed2 RS |
807 | if (!NILP (handler)) |
808 | return call2 (handler, Qdirectory_file_name, directory); | |
809 | ||
570d7624 JB |
810 | #ifdef VMS |
811 | /* 20 extra chars is insufficient for VMS, since we might perform a | |
812 | logical name translation. an equivalence string can be up to 255 | |
813 | chars long, so grab that much extra space... - sss */ | |
fc932ac6 | 814 | buf = (char *) alloca (STRING_BYTES (XSTRING (directory)) + 20 + 255); |
570d7624 | 815 | #else |
fc932ac6 | 816 | buf = (char *) alloca (STRING_BYTES (XSTRING (directory)) + 20); |
570d7624 JB |
817 | #endif |
818 | directory_file_name (XSTRING (directory)->data, buf); | |
819 | return build_string (buf); | |
820 | } | |
821 | ||
3ce839e4 RS |
822 | static char make_temp_name_tbl[64] = |
823 | { | |
824 | 'A','B','C','D','E','F','G','H', | |
825 | 'I','J','K','L','M','N','O','P', | |
826 | 'Q','R','S','T','U','V','W','X', | |
827 | 'Y','Z','a','b','c','d','e','f', | |
828 | 'g','h','i','j','k','l','m','n', | |
829 | 'o','p','q','r','s','t','u','v', | |
830 | 'w','x','y','z','0','1','2','3', | |
831 | '4','5','6','7','8','9','-','_' | |
832 | }; | |
833 | static unsigned make_temp_name_count, make_temp_name_count_initialized_p; | |
834 | ||
570d7624 JB |
835 | DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0, |
836 | "Generate temporary file name (string) starting with PREFIX (a string).\n\ | |
837 | The Emacs process number forms part of the result,\n\ | |
41886f35 | 838 | so there is no danger of generating a name being used by another process.\n\ |
3ce839e4 | 839 | \n\ |
41886f35 | 840 | In addition, this function makes an attempt to choose a name\n\ |
3ce839e4 RS |
841 | which has no existing file. To make this work,\n\ |
842 | PREFIX should be an absolute file name.") | |
570d7624 JB |
843 | (prefix) |
844 | Lisp_Object prefix; | |
845 | { | |
846 | Lisp_Object val; | |
3ce839e4 RS |
847 | int len; |
848 | int pid; | |
849 | unsigned char *p, *data; | |
850 | char pidbuf[20]; | |
851 | int pidlen; | |
852 | ||
853 | CHECK_STRING (prefix, 0); | |
854 | ||
855 | /* VAL is created by adding 6 characters to PREFIX. The first | |
856 | three are the PID of this process, in base 64, and the second | |
857 | three are incremented if the file already exists. This ensures | |
858 | 262144 unique file names per PID per PREFIX. */ | |
859 | ||
860 | pid = (int) getpid (); | |
861 | ||
862 | #ifdef HAVE_LONG_FILE_NAMES | |
863 | sprintf (pidbuf, "%d", pid); | |
864 | pidlen = strlen (pidbuf); | |
3a3bfb18 | 865 | #else |
3ce839e4 RS |
866 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; |
867 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
868 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
869 | pidlen = 3; | |
870 | #endif | |
871 | ||
872 | len = XSTRING (prefix)->size; | |
873 | val = make_uninit_string (len + 3 + pidlen); | |
874 | data = XSTRING (val)->data; | |
875 | bcopy(XSTRING (prefix)->data, data, len); | |
876 | p = data + len; | |
877 | ||
878 | bcopy (pidbuf, p, pidlen); | |
879 | p += pidlen; | |
880 | ||
881 | /* Here we try to minimize useless stat'ing when this function is | |
882 | invoked many times successively with the same PREFIX. We achieve | |
883 | this by initializing count to a random value, and incrementing it | |
f6a492a9 RS |
884 | afterwards. |
885 | ||
886 | We don't want make-temp-name to be called while dumping, | |
887 | because then make_temp_name_count_initialized_p would get set | |
888 | and then make_temp_name_count would not be set when Emacs starts. */ | |
889 | ||
3ce839e4 RS |
890 | if (!make_temp_name_count_initialized_p) |
891 | { | |
892 | make_temp_name_count = (unsigned) time (NULL); | |
893 | make_temp_name_count_initialized_p = 1; | |
894 | } | |
895 | ||
896 | while (1) | |
897 | { | |
898 | struct stat ignored; | |
8a7777fc | 899 | unsigned num = make_temp_name_count; |
3ce839e4 RS |
900 | |
901 | p[0] = make_temp_name_tbl[num & 63], num >>= 6; | |
902 | p[1] = make_temp_name_tbl[num & 63], num >>= 6; | |
903 | p[2] = make_temp_name_tbl[num & 63], num >>= 6; | |
904 | ||
8a7777fc RS |
905 | /* Poor man's congruential RN generator. Replace with |
906 | ++make_temp_name_count for debugging. */ | |
907 | make_temp_name_count += 25229; | |
908 | make_temp_name_count %= 225307; | |
909 | ||
3ce839e4 RS |
910 | if (stat (data, &ignored) < 0) |
911 | { | |
912 | /* We want to return only if errno is ENOENT. */ | |
913 | if (errno == ENOENT) | |
914 | return val; | |
915 | else | |
916 | /* The error here is dubious, but there is little else we | |
917 | can do. The alternatives are to return nil, which is | |
918 | as bad as (and in many cases worse than) throwing the | |
919 | error, or to ignore the error, which will likely result | |
8a7777fc RS |
920 | in looping through 225307 stat's, which is not only |
921 | dog-slow, but also useless since it will fallback to | |
922 | the errow below, anyway. */ | |
3ce839e4 RS |
923 | report_file_error ("Cannot create temporary name for prefix `%s'", |
924 | Fcons (prefix, Qnil)); | |
925 | /* not reached */ | |
926 | } | |
927 | } | |
928 | ||
929 | error ("Cannot create temporary name for prefix `%s'", | |
930 | XSTRING (prefix)->data); | |
931 | return Qnil; | |
570d7624 | 932 | } |
3ce839e4 | 933 | |
570d7624 JB |
934 | \f |
935 | DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, | |
3b7f6e60 EN |
936 | "Convert filename NAME to absolute, and canonicalize it.\n\ |
937 | Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative\n\ | |
938 | (does not start with slash); if DEFAULT-DIRECTORY is nil or missing,\n\ | |
570d7624 | 939 | the current buffer's value of default-directory is used.\n\ |
199607e4 RS |
940 | File name components that are `.' are removed, and \n\ |
941 | so are file name components followed by `..', along with the `..' itself;\n\ | |
b72dea2a | 942 | note that these simplifications are done without checking the resulting\n\ |
199607e4 | 943 | file names in the file system.\n\ |
b72dea2a JB |
944 | An initial `~/' expands to your home directory.\n\ |
945 | An initial `~USER/' expands to USER's home directory.\n\ | |
570d7624 | 946 | See also the function `substitute-in-file-name'.") |
82330e7f | 947 | (name, default_directory) |
3b7f6e60 | 948 | Lisp_Object name, default_directory; |
570d7624 JB |
949 | { |
950 | unsigned char *nm; | |
199607e4 | 951 | |
570d7624 JB |
952 | register unsigned char *newdir, *p, *o; |
953 | int tlen; | |
954 | unsigned char *target; | |
955 | struct passwd *pw; | |
570d7624 JB |
956 | #ifdef VMS |
957 | unsigned char * colon = 0; | |
958 | unsigned char * close = 0; | |
959 | unsigned char * slash = 0; | |
960 | unsigned char * brack = 0; | |
961 | int lbrack = 0, rbrack = 0; | |
962 | int dots = 0; | |
963 | #endif /* VMS */ | |
5e570b75 | 964 | #ifdef DOS_NT |
199607e4 | 965 | int drive = 0; |
9a1dc3be | 966 | int collapse_newdir = 1; |
f0f95d31 | 967 | int is_escaped = 0; |
5e570b75 | 968 | #endif /* DOS_NT */ |
199607e4 | 969 | int length; |
0bf2eed2 | 970 | Lisp_Object handler; |
199607e4 | 971 | |
570d7624 JB |
972 | CHECK_STRING (name, 0); |
973 | ||
0bf2eed2 RS |
974 | /* If the file name has special constructs in it, |
975 | call the corresponding file handler. */ | |
49307295 | 976 | handler = Ffind_file_name_handler (name, Qexpand_file_name); |
0bf2eed2 | 977 | if (!NILP (handler)) |
3b7f6e60 | 978 | return call3 (handler, Qexpand_file_name, name, default_directory); |
58fc9587 | 979 | |
3b7f6e60 EN |
980 | /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */ |
981 | if (NILP (default_directory)) | |
982 | default_directory = current_buffer->directory; | |
82330e7f RS |
983 | if (! STRINGP (default_directory)) |
984 | default_directory = build_string ("/"); | |
58fc9587 | 985 | |
3b7f6e60 | 986 | if (!NILP (default_directory)) |
273e0829 | 987 | { |
3b7f6e60 | 988 | handler = Ffind_file_name_handler (default_directory, Qexpand_file_name); |
273e0829 | 989 | if (!NILP (handler)) |
3b7f6e60 | 990 | return call3 (handler, Qexpand_file_name, name, default_directory); |
273e0829 | 991 | } |
0bf2eed2 | 992 | |
3b7f6e60 | 993 | o = XSTRING (default_directory)->data; |
5e570b75 | 994 | |
3b7f6e60 | 995 | /* Make sure DEFAULT_DIRECTORY is properly expanded. |
f14b1c68 | 996 | It would be better to do this down below where we actually use |
3b7f6e60 | 997 | default_directory. Unfortunately, calling Fexpand_file_name recursively |
f14b1c68 JB |
998 | could invoke GC, and the strings might be relocated. This would |
999 | be annoying because we have pointers into strings lying around | |
1000 | that would need adjusting, and people would add new pointers to | |
1001 | the code and forget to adjust them, resulting in intermittent bugs. | |
4ad827c5 RS |
1002 | Putting this call here avoids all that crud. |
1003 | ||
1004 | The EQ test avoids infinite recursion. */ | |
3b7f6e60 | 1005 | if (! NILP (default_directory) && !EQ (default_directory, name) |
199607e4 RS |
1006 | /* Save time in some common cases - as long as default_directory |
1007 | is not relative, it can be canonicalized with name below (if it | |
1008 | is needed at all) without requiring it to be expanded now. */ | |
01937013 | 1009 | #ifdef DOS_NT |
199607e4 | 1010 | /* Detect MSDOS file names with drive specifiers. */ |
f0f95d31 | 1011 | && ! (IS_DRIVE (o[0]) && IS_DEVICE_SEP (o[1]) && IS_DIRECTORY_SEP (o[2])) |
199607e4 RS |
1012 | #ifdef WINDOWSNT |
1013 | /* Detect Windows file names in UNC format. */ | |
1014 | && ! (IS_DIRECTORY_SEP (o[0]) && IS_DIRECTORY_SEP (o[1])) | |
01937013 | 1015 | #endif |
199607e4 RS |
1016 | #else /* not DOS_NT */ |
1017 | /* Detect Unix absolute file names (/... alone is not absolute on | |
1018 | DOS or Windows). */ | |
1019 | && ! (IS_DIRECTORY_SEP (o[0])) | |
1020 | #endif /* not DOS_NT */ | |
1021 | ) | |
f14b1c68 JB |
1022 | { |
1023 | struct gcpro gcpro1; | |
1024 | ||
1025 | GCPRO1 (name); | |
3b7f6e60 | 1026 | default_directory = Fexpand_file_name (default_directory, Qnil); |
f14b1c68 JB |
1027 | UNGCPRO; |
1028 | } | |
1029 | ||
570d7624 JB |
1030 | #ifdef VMS |
1031 | /* Filenames on VMS are always upper case. */ | |
1032 | name = Fupcase (name); | |
1033 | #endif | |
4c3c22f3 RS |
1034 | #ifdef FILE_SYSTEM_CASE |
1035 | name = FILE_SYSTEM_CASE (name); | |
1036 | #endif | |
570d7624 JB |
1037 | |
1038 | nm = XSTRING (name)->data; | |
a5a1cc06 | 1039 | |
5e570b75 | 1040 | #ifdef DOS_NT |
199607e4 RS |
1041 | /* We will force directory separators to be either all \ or /, so make |
1042 | a local copy to modify, even if there ends up being no change. */ | |
1043 | nm = strcpy (alloca (strlen (nm) + 1), nm); | |
1044 | ||
f0f95d31 RS |
1045 | /* Note if special escape prefix is present, but remove for now. */ |
1046 | if (nm[0] == '/' && nm[1] == ':') | |
1047 | { | |
1048 | is_escaped = 1; | |
1049 | nm += 2; | |
1050 | } | |
1051 | ||
199607e4 | 1052 | /* Find and remove drive specifier if present; this makes nm absolute |
f0f95d31 RS |
1053 | even if the rest of the name appears to be relative. Only look for |
1054 | drive specifier at the beginning. */ | |
1055 | if (IS_DRIVE (nm[0]) && IS_DEVICE_SEP (nm[1])) | |
1056 | { | |
1057 | drive = nm[0]; | |
1058 | nm += 2; | |
1059 | } | |
bb1ff1f4 GV |
1060 | |
1061 | #ifdef WINDOWSNT | |
1062 | /* If we see "c://somedir", we want to strip the first slash after the | |
1063 | colon when stripping the drive letter. Otherwise, this expands to | |
1064 | "//somedir". */ | |
1065 | if (drive && IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) | |
1066 | nm++; | |
1067 | #endif /* WINDOWSNT */ | |
5e570b75 | 1068 | #endif /* DOS_NT */ |
4c3c22f3 | 1069 | |
199607e4 RS |
1070 | #ifdef WINDOWSNT |
1071 | /* Discard any previous drive specifier if nm is now in UNC format. */ | |
1072 | if (IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) | |
1073 | { | |
1074 | drive = 0; | |
1075 | } | |
1076 | #endif | |
1077 | ||
1078 | /* If nm is absolute, look for /./ or /../ sequences; if none are | |
1079 | found, we can probably return right away. We will avoid allocating | |
1080 | a new string if name is already fully expanded. */ | |
570d7624 | 1081 | if ( |
5e570b75 | 1082 | IS_DIRECTORY_SEP (nm[0]) |
199607e4 | 1083 | #ifdef MSDOS |
f0f95d31 | 1084 | && drive && !is_escaped |
199607e4 RS |
1085 | #endif |
1086 | #ifdef WINDOWSNT | |
f0f95d31 | 1087 | && (drive || IS_DIRECTORY_SEP (nm[1])) && !is_escaped |
199607e4 | 1088 | #endif |
570d7624 JB |
1089 | #ifdef VMS |
1090 | || index (nm, ':') | |
1091 | #endif /* VMS */ | |
1092 | ) | |
1093 | { | |
f14b1c68 JB |
1094 | /* If it turns out that the filename we want to return is just a |
1095 | suffix of FILENAME, we don't need to go through and edit | |
1096 | things; we just need to construct a new string using data | |
1097 | starting at the middle of FILENAME. If we set lose to a | |
1098 | non-zero value, that means we've discovered that we can't do | |
1099 | that cool trick. */ | |
1100 | int lose = 0; | |
1101 | ||
570d7624 | 1102 | p = nm; |
570d7624 JB |
1103 | while (*p) |
1104 | { | |
199607e4 | 1105 | /* Since we know the name is absolute, we can assume that each |
c77d647e JB |
1106 | element starts with a "/". */ |
1107 | ||
c77d647e | 1108 | /* "." and ".." are hairy. */ |
5e570b75 | 1109 | if (IS_DIRECTORY_SEP (p[0]) |
c77d647e | 1110 | && p[1] == '.' |
5e570b75 | 1111 | && (IS_DIRECTORY_SEP (p[2]) |
c77d647e | 1112 | || p[2] == 0 |
5e570b75 | 1113 | || (p[2] == '.' && (IS_DIRECTORY_SEP (p[3]) |
c77d647e | 1114 | || p[3] == 0)))) |
570d7624 JB |
1115 | lose = 1; |
1116 | #ifdef VMS | |
1117 | if (p[0] == '\\') | |
1118 | lose = 1; | |
1119 | if (p[0] == '/') { | |
1120 | /* if dev:[dir]/, move nm to / */ | |
1121 | if (!slash && p > nm && (brack || colon)) { | |
1122 | nm = (brack ? brack + 1 : colon + 1); | |
1123 | lbrack = rbrack = 0; | |
1124 | brack = 0; | |
1125 | colon = 0; | |
1126 | } | |
1127 | slash = p; | |
1128 | } | |
1129 | if (p[0] == '-') | |
1130 | #ifndef VMS4_4 | |
1131 | /* VMS pre V4.4,convert '-'s in filenames. */ | |
1132 | if (lbrack == rbrack) | |
1133 | { | |
5e570b75 | 1134 | if (dots < 2) /* this is to allow negative version numbers */ |
570d7624 JB |
1135 | p[0] = '_'; |
1136 | } | |
1137 | else | |
1138 | #endif /* VMS4_4 */ | |
1139 | if (lbrack > rbrack && | |
1140 | ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') && | |
1141 | (p[1] == '.' || p[1] == ']' || p[1] == '>'))) | |
1142 | lose = 1; | |
1143 | #ifndef VMS4_4 | |
1144 | else | |
1145 | p[0] = '_'; | |
1146 | #endif /* VMS4_4 */ | |
1147 | /* count open brackets, reset close bracket pointer */ | |
1148 | if (p[0] == '[' || p[0] == '<') | |
1149 | lbrack++, brack = 0; | |
1150 | /* count close brackets, set close bracket pointer */ | |
1151 | if (p[0] == ']' || p[0] == '>') | |
1152 | rbrack++, brack = p; | |
1153 | /* detect ][ or >< */ | |
1154 | if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<')) | |
1155 | lose = 1; | |
1156 | if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~') | |
1157 | nm = p + 1, lose = 1; | |
1158 | if (p[0] == ':' && (colon || slash)) | |
1159 | /* if dev1:[dir]dev2:, move nm to dev2: */ | |
1160 | if (brack) | |
1161 | { | |
1162 | nm = brack + 1; | |
1163 | brack = 0; | |
1164 | } | |
199607e4 | 1165 | /* if /name/dev:, move nm to dev: */ |
570d7624 JB |
1166 | else if (slash) |
1167 | nm = slash + 1; | |
1168 | /* if node::dev:, move colon following dev */ | |
1169 | else if (colon && colon[-1] == ':') | |
1170 | colon = p; | |
1171 | /* if dev1:dev2:, move nm to dev2: */ | |
1172 | else if (colon && colon[-1] != ':') | |
1173 | { | |
1174 | nm = colon + 1; | |
1175 | colon = 0; | |
1176 | } | |
1177 | if (p[0] == ':' && !colon) | |
1178 | { | |
1179 | if (p[1] == ':') | |
1180 | p++; | |
1181 | colon = p; | |
1182 | } | |
1183 | if (lbrack == rbrack) | |
1184 | if (p[0] == ';') | |
1185 | dots = 2; | |
1186 | else if (p[0] == '.') | |
1187 | dots++; | |
1188 | #endif /* VMS */ | |
1189 | p++; | |
1190 | } | |
1191 | if (!lose) | |
1192 | { | |
1193 | #ifdef VMS | |
1194 | if (index (nm, '/')) | |
1195 | return build_string (sys_translate_unix (nm)); | |
1196 | #endif /* VMS */ | |
199607e4 RS |
1197 | #ifdef DOS_NT |
1198 | /* Make sure directories are all separated with / or \ as | |
1199 | desired, but avoid allocation of a new string when not | |
1200 | required. */ | |
1201 | CORRECT_DIR_SEPS (nm); | |
1202 | #ifdef WINDOWSNT | |
1203 | if (IS_DIRECTORY_SEP (nm[1])) | |
1204 | { | |
1205 | if (strcmp (nm, XSTRING (name)->data) != 0) | |
1206 | name = build_string (nm); | |
1207 | } | |
1208 | else | |
1209 | #endif | |
1210 | /* drive must be set, so this is okay */ | |
1211 | if (strcmp (nm - 2, XSTRING (name)->data) != 0) | |
1212 | { | |
1213 | name = make_string (nm - 2, p - nm + 2); | |
f94988a7 | 1214 | XSTRING (name)->data[0] = DRIVE_LETTER (drive); |
199607e4 RS |
1215 | XSTRING (name)->data[1] = ':'; |
1216 | } | |
1217 | return name; | |
1218 | #else /* not DOS_NT */ | |
570d7624 JB |
1219 | if (nm == XSTRING (name)->data) |
1220 | return name; | |
1221 | return build_string (nm); | |
5e570b75 | 1222 | #endif /* not DOS_NT */ |
570d7624 JB |
1223 | } |
1224 | } | |
1225 | ||
199607e4 RS |
1226 | /* At this point, nm might or might not be an absolute file name. We |
1227 | need to expand ~ or ~user if present, otherwise prefix nm with | |
1228 | default_directory if nm is not absolute, and finally collapse /./ | |
1229 | and /foo/../ sequences. | |
1230 | ||
1231 | We set newdir to be the appropriate prefix if one is needed: | |
1232 | - the relevant user directory if nm starts with ~ or ~user | |
1233 | - the specified drive's working dir (DOS/NT only) if nm does not | |
1234 | start with / | |
1235 | - the value of default_directory. | |
1236 | ||
1237 | Note that these prefixes are not guaranteed to be absolute (except | |
1238 | for the working dir of a drive). Therefore, to ensure we always | |
1239 | return an absolute name, if the final prefix is not absolute we | |
1240 | append it to the current working directory. */ | |
570d7624 JB |
1241 | |
1242 | newdir = 0; | |
1243 | ||
1244 | if (nm[0] == '~') /* prefix ~ */ | |
c77d647e | 1245 | { |
5e570b75 | 1246 | if (IS_DIRECTORY_SEP (nm[1]) |
570d7624 | 1247 | #ifdef VMS |
c77d647e | 1248 | || nm[1] == ':' |
5e570b75 | 1249 | #endif /* VMS */ |
c77d647e JB |
1250 | || nm[1] == 0) /* ~ by itself */ |
1251 | { | |
1252 | if (!(newdir = (unsigned char *) egetenv ("HOME"))) | |
1253 | newdir = (unsigned char *) ""; | |
199607e4 | 1254 | nm++; |
5e570b75 | 1255 | #ifdef DOS_NT |
9a1dc3be | 1256 | collapse_newdir = 0; |
4c3c22f3 | 1257 | #endif |
570d7624 | 1258 | #ifdef VMS |
c77d647e | 1259 | nm++; /* Don't leave the slash in nm. */ |
5e570b75 | 1260 | #endif /* VMS */ |
c77d647e JB |
1261 | } |
1262 | else /* ~user/filename */ | |
1263 | { | |
5e570b75 | 1264 | for (p = nm; *p && (!IS_DIRECTORY_SEP (*p) |
570d7624 | 1265 | #ifdef VMS |
c77d647e | 1266 | && *p != ':' |
5e570b75 | 1267 | #endif /* VMS */ |
c77d647e JB |
1268 | ); p++); |
1269 | o = (unsigned char *) alloca (p - nm + 1); | |
1270 | bcopy ((char *) nm, o, p - nm); | |
1271 | o [p - nm] = 0; | |
1272 | ||
1273 | pw = (struct passwd *) getpwnam (o + 1); | |
1274 | if (pw) | |
1275 | { | |
1276 | newdir = (unsigned char *) pw -> pw_dir; | |
570d7624 | 1277 | #ifdef VMS |
c77d647e | 1278 | nm = p + 1; /* skip the terminator */ |
570d7624 | 1279 | #else |
c77d647e | 1280 | nm = p; |
199607e4 | 1281 | #ifdef DOS_NT |
9a1dc3be | 1282 | collapse_newdir = 0; |
199607e4 | 1283 | #endif |
5e570b75 | 1284 | #endif /* VMS */ |
c77d647e | 1285 | } |
e5d77022 | 1286 | |
c77d647e JB |
1287 | /* If we don't find a user of that name, leave the name |
1288 | unchanged; don't move nm forward to p. */ | |
1289 | } | |
1290 | } | |
570d7624 | 1291 | |
5e570b75 | 1292 | #ifdef DOS_NT |
199607e4 RS |
1293 | /* On DOS and Windows, nm is absolute if a drive name was specified; |
1294 | use the drive's current directory as the prefix if needed. */ | |
1295 | if (!newdir && drive) | |
1296 | { | |
1297 | /* Get default directory if needed to make nm absolute. */ | |
1298 | if (!IS_DIRECTORY_SEP (nm[0])) | |
1299 | { | |
1300 | newdir = alloca (MAXPATHLEN + 1); | |
1301 | if (!getdefdir (toupper (drive) - 'A' + 1, newdir)) | |
1302 | newdir = NULL; | |
1303 | } | |
1304 | if (!newdir) | |
1305 | { | |
1306 | /* Either nm starts with /, or drive isn't mounted. */ | |
1307 | newdir = alloca (4); | |
f94988a7 | 1308 | newdir[0] = DRIVE_LETTER (drive); |
199607e4 RS |
1309 | newdir[1] = ':'; |
1310 | newdir[2] = '/'; | |
1311 | newdir[3] = 0; | |
1312 | } | |
1313 | } | |
5e570b75 | 1314 | #endif /* DOS_NT */ |
199607e4 RS |
1315 | |
1316 | /* Finally, if no prefix has been specified and nm is not absolute, | |
1317 | then it must be expanded relative to default_directory. */ | |
1318 | ||
34097368 | 1319 | if (1 |
199607e4 RS |
1320 | #ifndef DOS_NT |
1321 | /* /... alone is not absolute on DOS and Windows. */ | |
34097368 | 1322 | && !IS_DIRECTORY_SEP (nm[0]) |
199607e4 RS |
1323 | #endif |
1324 | #ifdef WINDOWSNT | |
34097368 | 1325 | && !(IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) |
199607e4 RS |
1326 | #endif |
1327 | #ifdef VMS | |
1328 | && !index (nm, ':') | |
1329 | #endif | |
570d7624 JB |
1330 | && !newdir) |
1331 | { | |
3b7f6e60 | 1332 | newdir = XSTRING (default_directory)->data; |
f0f95d31 RS |
1333 | #ifdef DOS_NT |
1334 | /* Note if special escape prefix is present, but remove for now. */ | |
1335 | if (newdir[0] == '/' && newdir[1] == ':') | |
1336 | { | |
1337 | is_escaped = 1; | |
1338 | newdir += 2; | |
1339 | } | |
1340 | #endif | |
570d7624 JB |
1341 | } |
1342 | ||
5e570b75 | 1343 | #ifdef DOS_NT |
199607e4 RS |
1344 | if (newdir) |
1345 | { | |
1346 | /* First ensure newdir is an absolute name. */ | |
1347 | if ( | |
1348 | /* Detect MSDOS file names with drive specifiers. */ | |
1349 | ! (IS_DRIVE (newdir[0]) | |
1350 | && IS_DEVICE_SEP (newdir[1]) && IS_DIRECTORY_SEP (newdir[2])) | |
1351 | #ifdef WINDOWSNT | |
1352 | /* Detect Windows file names in UNC format. */ | |
1353 | && ! (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1354 | #endif | |
1355 | ) | |
1356 | { | |
1357 | /* Effectively, let newdir be (expand-file-name newdir cwd). | |
1358 | Because of the admonition against calling expand-file-name | |
1359 | when we have pointers into lisp strings, we accomplish this | |
1360 | indirectly by prepending newdir to nm if necessary, and using | |
1361 | cwd (or the wd of newdir's drive) as the new newdir. */ | |
1362 | ||
1363 | if (IS_DRIVE (newdir[0]) && newdir[1] == ':') | |
1364 | { | |
1365 | drive = newdir[0]; | |
1366 | newdir += 2; | |
1367 | } | |
1368 | if (!IS_DIRECTORY_SEP (nm[0])) | |
1369 | { | |
1370 | char * tmp = alloca (strlen (newdir) + strlen (nm) + 2); | |
1371 | file_name_as_directory (tmp, newdir); | |
1372 | strcat (tmp, nm); | |
1373 | nm = tmp; | |
1374 | } | |
1375 | newdir = alloca (MAXPATHLEN + 1); | |
1376 | if (drive) | |
1377 | { | |
1378 | if (!getdefdir (toupper (drive) - 'A' + 1, newdir)) | |
1379 | newdir = "/"; | |
1380 | } | |
1381 | else | |
1382 | getwd (newdir); | |
1383 | } | |
1384 | ||
1385 | /* Strip off drive name from prefix, if present. */ | |
1386 | if (IS_DRIVE (newdir[0]) && newdir[1] == ':') | |
1387 | { | |
1388 | drive = newdir[0]; | |
1389 | newdir += 2; | |
1390 | } | |
1391 | ||
1392 | /* Keep only a prefix from newdir if nm starts with slash | |
82330e7f | 1393 | (//server/share for UNC, nothing otherwise). */ |
9a1dc3be | 1394 | if (IS_DIRECTORY_SEP (nm[0]) && collapse_newdir) |
199607e4 RS |
1395 | { |
1396 | #ifdef WINDOWSNT | |
1397 | if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1398 | { | |
1399 | newdir = strcpy (alloca (strlen (newdir) + 1), newdir); | |
1400 | p = newdir + 2; | |
1401 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; | |
1402 | p++; | |
1403 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; | |
1404 | *p = 0; | |
1405 | } | |
1406 | else | |
1407 | #endif | |
1408 | newdir = ""; | |
1409 | } | |
1410 | } | |
5e570b75 | 1411 | #endif /* DOS_NT */ |
199607e4 RS |
1412 | |
1413 | if (newdir) | |
bfb61299 | 1414 | { |
57676091 | 1415 | /* Get rid of any slash at the end of newdir, unless newdir is |
f0f95d31 | 1416 | just / or // (an incomplete UNC name). */ |
199607e4 | 1417 | length = strlen (newdir); |
f0f95d31 | 1418 | if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1]) |
57676091 RS |
1419 | #ifdef WINDOWSNT |
1420 | && !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) | |
1421 | #endif | |
1422 | ) | |
bfb61299 JB |
1423 | { |
1424 | unsigned char *temp = (unsigned char *) alloca (length); | |
1425 | bcopy (newdir, temp, length - 1); | |
1426 | temp[length - 1] = 0; | |
1427 | newdir = temp; | |
1428 | } | |
1429 | tlen = length + 1; | |
1430 | } | |
1431 | else | |
1432 | tlen = 0; | |
570d7624 | 1433 | |
bfb61299 JB |
1434 | /* Now concatenate the directory and name to new space in the stack frame */ |
1435 | tlen += strlen (nm) + 1; | |
5e570b75 | 1436 | #ifdef DOS_NT |
f0f95d31 RS |
1437 | /* Reserve space for drive specifier and escape prefix, since either |
1438 | or both may need to be inserted. (The Microsoft x86 compiler | |
5e570b75 | 1439 | produces incorrect code if the following two lines are combined.) */ |
f0f95d31 RS |
1440 | target = (unsigned char *) alloca (tlen + 4); |
1441 | target += 4; | |
5e570b75 | 1442 | #else /* not DOS_NT */ |
570d7624 | 1443 | target = (unsigned char *) alloca (tlen); |
5e570b75 | 1444 | #endif /* not DOS_NT */ |
570d7624 JB |
1445 | *target = 0; |
1446 | ||
1447 | if (newdir) | |
1448 | { | |
1449 | #ifndef VMS | |
5e570b75 | 1450 | if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0])) |
f5321b5c | 1451 | { |
3ea2d8b2 | 1452 | #ifdef DOS_NT |
f5321b5c RS |
1453 | /* If newdir is effectively "C:/", then the drive letter will have |
1454 | been stripped and newdir will be "/". Concatenating with an | |
1455 | absolute directory in nm produces "//", which will then be | |
1456 | incorrectly treated as a network share. Ignore newdir in | |
1457 | this case (keeping the drive letter). */ | |
1458 | if (!(drive && nm[0] && IS_DIRECTORY_SEP (newdir[0]) | |
1459 | && newdir[1] == '\0')) | |
1460 | #endif | |
1461 | strcpy (target, newdir); | |
1462 | } | |
570d7624 JB |
1463 | else |
1464 | #endif | |
c77d647e | 1465 | file_name_as_directory (target, newdir); |
570d7624 JB |
1466 | } |
1467 | ||
1468 | strcat (target, nm); | |
1469 | #ifdef VMS | |
1470 | if (index (target, '/')) | |
1471 | strcpy (target, sys_translate_unix (target)); | |
1472 | #endif /* VMS */ | |
1473 | ||
199607e4 RS |
1474 | /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */ |
1475 | ||
c77d647e | 1476 | /* Now canonicalize by removing /. and /foo/.. if they appear. */ |
570d7624 JB |
1477 | |
1478 | p = target; | |
1479 | o = target; | |
1480 | ||
1481 | while (*p) | |
1482 | { | |
1483 | #ifdef VMS | |
1484 | if (*p != ']' && *p != '>' && *p != '-') | |
1485 | { | |
1486 | if (*p == '\\') | |
1487 | p++; | |
1488 | *o++ = *p++; | |
1489 | } | |
1490 | else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2) | |
1491 | /* brackets are offset from each other by 2 */ | |
1492 | { | |
1493 | p += 2; | |
1494 | if (*p != '.' && *p != '-' && o[-1] != '.') | |
1495 | /* convert [foo][bar] to [bar] */ | |
1496 | while (o[-1] != '[' && o[-1] != '<') | |
1497 | o--; | |
1498 | else if (*p == '-' && *o != '.') | |
1499 | *--p = '.'; | |
1500 | } | |
1501 | else if (p[0] == '-' && o[-1] == '.' && | |
1502 | (p[1] == '.' || p[1] == ']' || p[1] == '>')) | |
1503 | /* flush .foo.- ; leave - if stopped by '[' or '<' */ | |
1504 | { | |
1505 | do | |
1506 | o--; | |
1507 | while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<'); | |
5e570b75 | 1508 | if (p[1] == '.') /* foo.-.bar ==> bar. */ |
570d7624 JB |
1509 | p += 2; |
1510 | else if (o[-1] == '.') /* '.foo.-]' ==> ']' */ | |
1511 | p++, o--; | |
1512 | /* else [foo.-] ==> [-] */ | |
1513 | } | |
1514 | else | |
1515 | { | |
1516 | #ifndef VMS4_4 | |
1517 | if (*p == '-' && | |
1518 | o[-1] != '[' && o[-1] != '<' && o[-1] != '.' && | |
1519 | p[1] != ']' && p[1] != '>' && p[1] != '.') | |
1520 | *p = '_'; | |
1521 | #endif /* VMS4_4 */ | |
1522 | *o++ = *p++; | |
1523 | } | |
1524 | #else /* not VMS */ | |
5e570b75 RS |
1525 | if (!IS_DIRECTORY_SEP (*p)) |
1526 | { | |
570d7624 JB |
1527 | *o++ = *p++; |
1528 | } | |
5e570b75 | 1529 | else if (IS_DIRECTORY_SEP (p[0]) |
c77d647e | 1530 | && p[1] == '.' |
5e570b75 | 1531 | && (IS_DIRECTORY_SEP (p[2]) |
c77d647e JB |
1532 | || p[2] == 0)) |
1533 | { | |
1534 | /* If "/." is the entire filename, keep the "/". Otherwise, | |
1535 | just delete the whole "/.". */ | |
1536 | if (o == target && p[2] == '\0') | |
1537 | *o++ = *p; | |
1538 | p += 2; | |
1539 | } | |
c0fa5a0b | 1540 | else if (IS_DIRECTORY_SEP (p[0]) && p[1] == '.' && p[2] == '.' |
570d7624 JB |
1541 | /* `/../' is the "superroot" on certain file systems. */ |
1542 | && o != target | |
5e570b75 | 1543 | && (IS_DIRECTORY_SEP (p[3]) || p[3] == 0)) |
570d7624 | 1544 | { |
5e570b75 | 1545 | while (o != target && (--o) && !IS_DIRECTORY_SEP (*o)) |
570d7624 | 1546 | ; |
795de720 RS |
1547 | /* Keep initial / only if this is the whole name. */ |
1548 | if (o == target && IS_ANY_SEP (*o) && p[3] == 0) | |
51b1d12e | 1549 | ++o; |
570d7624 JB |
1550 | p += 3; |
1551 | } | |
1552 | else | |
5e570b75 | 1553 | { |
570d7624 JB |
1554 | *o++ = *p++; |
1555 | } | |
1556 | #endif /* not VMS */ | |
1557 | } | |
1558 | ||
5e570b75 | 1559 | #ifdef DOS_NT |
199607e4 | 1560 | /* At last, set drive name. */ |
5e570b75 | 1561 | #ifdef WINDOWSNT |
199607e4 RS |
1562 | /* Except for network file name. */ |
1563 | if (!(IS_DIRECTORY_SEP (target[0]) && IS_DIRECTORY_SEP (target[1]))) | |
5e570b75 | 1564 | #endif /* WINDOWSNT */ |
4c3c22f3 | 1565 | { |
199607e4 | 1566 | if (!drive) abort (); |
4c3c22f3 | 1567 | target -= 2; |
f94988a7 | 1568 | target[0] = DRIVE_LETTER (drive); |
4c3c22f3 RS |
1569 | target[1] = ':'; |
1570 | } | |
f0f95d31 RS |
1571 | /* Reinsert the escape prefix if required. */ |
1572 | if (is_escaped) | |
1573 | { | |
1574 | target -= 2; | |
1575 | target[0] = '/'; | |
1576 | target[1] = ':'; | |
1577 | } | |
199607e4 | 1578 | CORRECT_DIR_SEPS (target); |
5e570b75 | 1579 | #endif /* DOS_NT */ |
4c3c22f3 | 1580 | |
570d7624 JB |
1581 | return make_string (target, o - target); |
1582 | } | |
5e570b75 | 1583 | |
570d7624 | 1584 | #if 0 |
5e570b75 | 1585 | /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'. */ |
e5d77022 | 1586 | DEAFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, |
570d7624 JB |
1587 | "Convert FILENAME to absolute, and canonicalize it.\n\ |
1588 | Second arg DEFAULT is directory to start with if FILENAME is relative\n\ | |
1589 | (does not start with slash); if DEFAULT is nil or missing,\n\ | |
1590 | the current buffer's value of default-directory is used.\n\ | |
1591 | Filenames containing `.' or `..' as components are simplified;\n\ | |
1592 | initial `~/' expands to your home directory.\n\ | |
1593 | See also the function `substitute-in-file-name'.") | |
1594 | (name, defalt) | |
1595 | Lisp_Object name, defalt; | |
1596 | { | |
1597 | unsigned char *nm; | |
199607e4 | 1598 | |
570d7624 JB |
1599 | register unsigned char *newdir, *p, *o; |
1600 | int tlen; | |
1601 | unsigned char *target; | |
1602 | struct passwd *pw; | |
1603 | int lose; | |
1604 | #ifdef VMS | |
1605 | unsigned char * colon = 0; | |
1606 | unsigned char * close = 0; | |
1607 | unsigned char * slash = 0; | |
1608 | unsigned char * brack = 0; | |
1609 | int lbrack = 0, rbrack = 0; | |
1610 | int dots = 0; | |
1611 | #endif /* VMS */ | |
199607e4 | 1612 | |
570d7624 JB |
1613 | CHECK_STRING (name, 0); |
1614 | ||
1615 | #ifdef VMS | |
1616 | /* Filenames on VMS are always upper case. */ | |
1617 | name = Fupcase (name); | |
1618 | #endif | |
1619 | ||
1620 | nm = XSTRING (name)->data; | |
199607e4 | 1621 | |
570d7624 | 1622 | /* If nm is absolute, flush ...// and detect /./ and /../. |
82330e7f | 1623 | If no /./ or /../ we can return right away. */ |
570d7624 JB |
1624 | if ( |
1625 | nm[0] == '/' | |
1626 | #ifdef VMS | |
1627 | || index (nm, ':') | |
1628 | #endif /* VMS */ | |
1629 | ) | |
1630 | { | |
1631 | p = nm; | |
1632 | lose = 0; | |
1633 | while (*p) | |
1634 | { | |
1635 | if (p[0] == '/' && p[1] == '/' | |
1636 | #ifdef APOLLO | |
82330e7f | 1637 | /* // at start of filename is meaningful on Apollo system. */ |
570d7624 JB |
1638 | && nm != p |
1639 | #endif /* APOLLO */ | |
1640 | ) | |
1641 | nm = p + 1; | |
1642 | if (p[0] == '/' && p[1] == '~') | |
1643 | nm = p + 1, lose = 1; | |
1644 | if (p[0] == '/' && p[1] == '.' | |
1645 | && (p[2] == '/' || p[2] == 0 | |
1646 | || (p[2] == '.' && (p[3] == '/' || p[3] == 0)))) | |
1647 | lose = 1; | |
1648 | #ifdef VMS | |
1649 | if (p[0] == '\\') | |
1650 | lose = 1; | |
1651 | if (p[0] == '/') { | |
1652 | /* if dev:[dir]/, move nm to / */ | |
1653 | if (!slash && p > nm && (brack || colon)) { | |
1654 | nm = (brack ? brack + 1 : colon + 1); | |
1655 | lbrack = rbrack = 0; | |
1656 | brack = 0; | |
1657 | colon = 0; | |
1658 | } | |
1659 | slash = p; | |
1660 | } | |
1661 | if (p[0] == '-') | |
1662 | #ifndef VMS4_4 | |
1663 | /* VMS pre V4.4,convert '-'s in filenames. */ | |
1664 | if (lbrack == rbrack) | |
1665 | { | |
5e570b75 | 1666 | if (dots < 2) /* this is to allow negative version numbers */ |
570d7624 JB |
1667 | p[0] = '_'; |
1668 | } | |
1669 | else | |
1670 | #endif /* VMS4_4 */ | |
1671 | if (lbrack > rbrack && | |
1672 | ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') && | |
1673 | (p[1] == '.' || p[1] == ']' || p[1] == '>'))) | |
1674 | lose = 1; | |
1675 | #ifndef VMS4_4 | |
1676 | else | |
1677 | p[0] = '_'; | |
1678 | #endif /* VMS4_4 */ | |
1679 | /* count open brackets, reset close bracket pointer */ | |
1680 | if (p[0] == '[' || p[0] == '<') | |
1681 | lbrack++, brack = 0; | |
1682 | /* count close brackets, set close bracket pointer */ | |
1683 | if (p[0] == ']' || p[0] == '>') | |
1684 | rbrack++, brack = p; | |
1685 | /* detect ][ or >< */ | |
1686 | if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<')) | |
1687 | lose = 1; | |
1688 | if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~') | |
1689 | nm = p + 1, lose = 1; | |
1690 | if (p[0] == ':' && (colon || slash)) | |
1691 | /* if dev1:[dir]dev2:, move nm to dev2: */ | |
1692 | if (brack) | |
1693 | { | |
1694 | nm = brack + 1; | |
1695 | brack = 0; | |
1696 | } | |
199607e4 | 1697 | /* If /name/dev:, move nm to dev: */ |
570d7624 JB |
1698 | else if (slash) |
1699 | nm = slash + 1; | |
199607e4 | 1700 | /* If node::dev:, move colon following dev */ |
570d7624 JB |
1701 | else if (colon && colon[-1] == ':') |
1702 | colon = p; | |
199607e4 | 1703 | /* If dev1:dev2:, move nm to dev2: */ |
570d7624 JB |
1704 | else if (colon && colon[-1] != ':') |
1705 | { | |
1706 | nm = colon + 1; | |
1707 | colon = 0; | |
1708 | } | |
1709 | if (p[0] == ':' && !colon) | |
1710 | { | |
1711 | if (p[1] == ':') | |
1712 | p++; | |
1713 | colon = p; | |
1714 | } | |
1715 | if (lbrack == rbrack) | |
1716 | if (p[0] == ';') | |
1717 | dots = 2; | |
1718 | else if (p[0] == '.') | |
1719 | dots++; | |
1720 | #endif /* VMS */ | |
1721 | p++; | |
1722 | } | |
1723 | if (!lose) | |
1724 | { | |
1725 | #ifdef VMS | |
1726 | if (index (nm, '/')) | |
1727 | return build_string (sys_translate_unix (nm)); | |
1728 | #endif /* VMS */ | |
1729 | if (nm == XSTRING (name)->data) | |
1730 | return name; | |
1731 | return build_string (nm); | |
1732 | } | |
1733 | } | |
1734 | ||
1735 | /* Now determine directory to start with and put it in NEWDIR */ | |
1736 | ||
1737 | newdir = 0; | |
1738 | ||
5e570b75 | 1739 | if (nm[0] == '~') /* prefix ~ */ |
570d7624 JB |
1740 | if (nm[1] == '/' |
1741 | #ifdef VMS | |
1742 | || nm[1] == ':' | |
1743 | #endif /* VMS */ | |
1744 | || nm[1] == 0)/* ~/filename */ | |
1745 | { | |
1746 | if (!(newdir = (unsigned char *) egetenv ("HOME"))) | |
1747 | newdir = (unsigned char *) ""; | |
1748 | nm++; | |
1749 | #ifdef VMS | |
5e570b75 | 1750 | nm++; /* Don't leave the slash in nm. */ |
570d7624 JB |
1751 | #endif /* VMS */ |
1752 | } | |
1753 | else /* ~user/filename */ | |
1754 | { | |
1755 | /* Get past ~ to user */ | |
1756 | unsigned char *user = nm + 1; | |
1757 | /* Find end of name. */ | |
1758 | unsigned char *ptr = (unsigned char *) index (user, '/'); | |
1759 | int len = ptr ? ptr - user : strlen (user); | |
1760 | #ifdef VMS | |
1761 | unsigned char *ptr1 = index (user, ':'); | |
1762 | if (ptr1 != 0 && ptr1 - user < len) | |
1763 | len = ptr1 - user; | |
5e570b75 | 1764 | #endif /* VMS */ |
570d7624 JB |
1765 | /* Copy the user name into temp storage. */ |
1766 | o = (unsigned char *) alloca (len + 1); | |
1767 | bcopy ((char *) user, o, len); | |
1768 | o[len] = 0; | |
1769 | ||
1770 | /* Look up the user name. */ | |
1771 | pw = (struct passwd *) getpwnam (o + 1); | |
1772 | if (!pw) | |
1773 | error ("\"%s\" isn't a registered user", o + 1); | |
1774 | ||
1775 | newdir = (unsigned char *) pw->pw_dir; | |
1776 | ||
1777 | /* Discard the user name from NM. */ | |
1778 | nm += len; | |
1779 | } | |
1780 | ||
1781 | if (nm[0] != '/' | |
1782 | #ifdef VMS | |
1783 | && !index (nm, ':') | |
1784 | #endif /* not VMS */ | |
1785 | && !newdir) | |
1786 | { | |
265a9e55 | 1787 | if (NILP (defalt)) |
570d7624 JB |
1788 | defalt = current_buffer->directory; |
1789 | CHECK_STRING (defalt, 1); | |
1790 | newdir = XSTRING (defalt)->data; | |
1791 | } | |
1792 | ||
1793 | /* Now concatenate the directory and name to new space in the stack frame */ | |
1794 | ||
1795 | tlen = (newdir ? strlen (newdir) + 1 : 0) + strlen (nm) + 1; | |
1796 | target = (unsigned char *) alloca (tlen); | |
1797 | *target = 0; | |
1798 | ||
1799 | if (newdir) | |
1800 | { | |
1801 | #ifndef VMS | |
1802 | if (nm[0] == 0 || nm[0] == '/') | |
1803 | strcpy (target, newdir); | |
1804 | else | |
1805 | #endif | |
1806 | file_name_as_directory (target, newdir); | |
1807 | } | |
1808 | ||
1809 | strcat (target, nm); | |
1810 | #ifdef VMS | |
1811 | if (index (target, '/')) | |
1812 | strcpy (target, sys_translate_unix (target)); | |
1813 | #endif /* VMS */ | |
1814 | ||
1815 | /* Now canonicalize by removing /. and /foo/.. if they appear */ | |
1816 | ||
1817 | p = target; | |
1818 | o = target; | |
1819 | ||
1820 | while (*p) | |
1821 | { | |
1822 | #ifdef VMS | |
1823 | if (*p != ']' && *p != '>' && *p != '-') | |
1824 | { | |
1825 | if (*p == '\\') | |
1826 | p++; | |
1827 | *o++ = *p++; | |
1828 | } | |
1829 | else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2) | |
1830 | /* brackets are offset from each other by 2 */ | |
1831 | { | |
1832 | p += 2; | |
1833 | if (*p != '.' && *p != '-' && o[-1] != '.') | |
1834 | /* convert [foo][bar] to [bar] */ | |
1835 | while (o[-1] != '[' && o[-1] != '<') | |
1836 | o--; | |
1837 | else if (*p == '-' && *o != '.') | |
1838 | *--p = '.'; | |
1839 | } | |
1840 | else if (p[0] == '-' && o[-1] == '.' && | |
1841 | (p[1] == '.' || p[1] == ']' || p[1] == '>')) | |
1842 | /* flush .foo.- ; leave - if stopped by '[' or '<' */ | |
1843 | { | |
1844 | do | |
1845 | o--; | |
1846 | while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<'); | |
5e570b75 | 1847 | if (p[1] == '.') /* foo.-.bar ==> bar. */ |
570d7624 JB |
1848 | p += 2; |
1849 | else if (o[-1] == '.') /* '.foo.-]' ==> ']' */ | |
1850 | p++, o--; | |
1851 | /* else [foo.-] ==> [-] */ | |
1852 | } | |
1853 | else | |
1854 | { | |
1855 | #ifndef VMS4_4 | |
1856 | if (*p == '-' && | |
1857 | o[-1] != '[' && o[-1] != '<' && o[-1] != '.' && | |
1858 | p[1] != ']' && p[1] != '>' && p[1] != '.') | |
1859 | *p = '_'; | |
1860 | #endif /* VMS4_4 */ | |
1861 | *o++ = *p++; | |
1862 | } | |
1863 | #else /* not VMS */ | |
1864 | if (*p != '/') | |
5e570b75 | 1865 | { |
570d7624 JB |
1866 | *o++ = *p++; |
1867 | } | |
1868 | else if (!strncmp (p, "//", 2) | |
1869 | #ifdef APOLLO | |
82330e7f | 1870 | /* // at start of filename is meaningful in Apollo system. */ |
570d7624 JB |
1871 | && o != target |
1872 | #endif /* APOLLO */ | |
1873 | ) | |
1874 | { | |
1875 | o = target; | |
1876 | p++; | |
1877 | } | |
1878 | else if (p[0] == '/' && p[1] == '.' && | |
1879 | (p[2] == '/' || p[2] == 0)) | |
1880 | p += 2; | |
1881 | else if (!strncmp (p, "/..", 3) | |
1882 | /* `/../' is the "superroot" on certain file systems. */ | |
1883 | && o != target | |
1884 | && (p[3] == '/' || p[3] == 0)) | |
1885 | { | |
1886 | while (o != target && *--o != '/') | |
1887 | ; | |
1888 | #ifdef APOLLO | |
1889 | if (o == target + 1 && o[-1] == '/' && o[0] == '/') | |
1890 | ++o; | |
1891 | else | |
1892 | #endif /* APOLLO */ | |
1893 | if (o == target && *o == '/') | |
1894 | ++o; | |
1895 | p += 3; | |
1896 | } | |
1897 | else | |
5e570b75 | 1898 | { |
570d7624 JB |
1899 | *o++ = *p++; |
1900 | } | |
1901 | #endif /* not VMS */ | |
1902 | } | |
1903 | ||
1904 | return make_string (target, o - target); | |
1905 | } | |
1906 | #endif | |
1907 | \f | |
1908 | DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, | |
1909 | Ssubstitute_in_file_name, 1, 1, 0, | |
1910 | "Substitute environment variables referred to in FILENAME.\n\ | |
1911 | `$FOO' where FOO is an environment variable name means to substitute\n\ | |
1912 | the value of that variable. The variable name should be terminated\n\ | |
1913 | with a character not a letter, digit or underscore; otherwise, enclose\n\ | |
1914 | the entire variable name in braces.\n\ | |
1915 | If `/~' appears, all of FILENAME through that `/' is discarded.\n\n\ | |
1916 | On VMS, `$' substitution is not done; this function does little and only\n\ | |
1917 | duplicates what `expand-file-name' does.") | |
3b7f6e60 EN |
1918 | (filename) |
1919 | Lisp_Object filename; | |
570d7624 JB |
1920 | { |
1921 | unsigned char *nm; | |
1922 | ||
1923 | register unsigned char *s, *p, *o, *x, *endp; | |
1924 | unsigned char *target; | |
1925 | int total = 0; | |
1926 | int substituted = 0; | |
1927 | unsigned char *xnm; | |
8ce069f5 | 1928 | Lisp_Object handler; |
570d7624 | 1929 | |
3b7f6e60 | 1930 | CHECK_STRING (filename, 0); |
570d7624 | 1931 | |
8ce069f5 RS |
1932 | /* If the file name has special constructs in it, |
1933 | call the corresponding file handler. */ | |
3b7f6e60 | 1934 | handler = Ffind_file_name_handler (filename, Qsubstitute_in_file_name); |
8ce069f5 | 1935 | if (!NILP (handler)) |
3b7f6e60 | 1936 | return call2 (handler, Qsubstitute_in_file_name, filename); |
8ce069f5 | 1937 | |
3b7f6e60 | 1938 | nm = XSTRING (filename)->data; |
199607e4 RS |
1939 | #ifdef DOS_NT |
1940 | nm = strcpy (alloca (strlen (nm) + 1), nm); | |
1941 | CORRECT_DIR_SEPS (nm); | |
1942 | substituted = (strcmp (nm, XSTRING (filename)->data) != 0); | |
a5a1cc06 | 1943 | #endif |
fc932ac6 | 1944 | endp = nm + STRING_BYTES (XSTRING (filename)); |
570d7624 | 1945 | |
82330e7f | 1946 | /* If /~ or // appears, discard everything through first slash. */ |
570d7624 JB |
1947 | |
1948 | for (p = nm; p != endp; p++) | |
1949 | { | |
199607e4 RS |
1950 | if ((p[0] == '~' |
1951 | #if defined (APOLLO) || defined (WINDOWSNT) | |
1952 | /* // at start of file name is meaningful in Apollo and | |
82330e7f | 1953 | WindowsNT systems. */ |
199607e4 RS |
1954 | || (IS_DIRECTORY_SEP (p[0]) && p - 1 != nm) |
1955 | #else /* not (APOLLO || WINDOWSNT) */ | |
1956 | || IS_DIRECTORY_SEP (p[0]) | |
1957 | #endif /* not (APOLLO || WINDOWSNT) */ | |
570d7624 | 1958 | ) |
5e570b75 RS |
1959 | && p != nm |
1960 | && (0 | |
570d7624 | 1961 | #ifdef VMS |
5e570b75 | 1962 | || p[-1] == ':' || p[-1] == ']' || p[-1] == '>' |
570d7624 | 1963 | #endif /* VMS */ |
5e570b75 | 1964 | || IS_DIRECTORY_SEP (p[-1]))) |
570d7624 JB |
1965 | { |
1966 | nm = p; | |
1967 | substituted = 1; | |
1968 | } | |
5e570b75 | 1969 | #ifdef DOS_NT |
199607e4 RS |
1970 | /* see comment in expand-file-name about drive specifiers */ |
1971 | else if (IS_DRIVE (p[0]) && p[1] == ':' | |
1972 | && p > nm && IS_DIRECTORY_SEP (p[-1])) | |
4c3c22f3 RS |
1973 | { |
1974 | nm = p; | |
1975 | substituted = 1; | |
1976 | } | |
5e570b75 | 1977 | #endif /* DOS_NT */ |
570d7624 JB |
1978 | } |
1979 | ||
1980 | #ifdef VMS | |
1981 | return build_string (nm); | |
1982 | #else | |
1983 | ||
1984 | /* See if any variables are substituted into the string | |
1985 | and find the total length of their values in `total' */ | |
1986 | ||
1987 | for (p = nm; p != endp;) | |
1988 | if (*p != '$') | |
1989 | p++; | |
1990 | else | |
1991 | { | |
1992 | p++; | |
1993 | if (p == endp) | |
1994 | goto badsubst; | |
1995 | else if (*p == '$') | |
1996 | { | |
1997 | /* "$$" means a single "$" */ | |
1998 | p++; | |
1999 | total -= 1; | |
2000 | substituted = 1; | |
2001 | continue; | |
2002 | } | |
2003 | else if (*p == '{') | |
2004 | { | |
2005 | o = ++p; | |
2006 | while (p != endp && *p != '}') p++; | |
2007 | if (*p != '}') goto missingclose; | |
2008 | s = p; | |
2009 | } | |
2010 | else | |
2011 | { | |
2012 | o = p; | |
2013 | while (p != endp && (isalnum (*p) || *p == '_')) p++; | |
2014 | s = p; | |
2015 | } | |
2016 | ||
2017 | /* Copy out the variable name */ | |
2018 | target = (unsigned char *) alloca (s - o + 1); | |
2019 | strncpy (target, o, s - o); | |
2020 | target[s - o] = 0; | |
5e570b75 | 2021 | #ifdef DOS_NT |
4c3c22f3 | 2022 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 2023 | #endif /* DOS_NT */ |
570d7624 JB |
2024 | |
2025 | /* Get variable value */ | |
2026 | o = (unsigned char *) egetenv (target); | |
570d7624 JB |
2027 | if (!o) goto badvar; |
2028 | total += strlen (o); | |
2029 | substituted = 1; | |
2030 | } | |
2031 | ||
2032 | if (!substituted) | |
3b7f6e60 | 2033 | return filename; |
570d7624 JB |
2034 | |
2035 | /* If substitution required, recopy the string and do it */ | |
2036 | /* Make space in stack frame for the new copy */ | |
fc932ac6 | 2037 | xnm = (unsigned char *) alloca (STRING_BYTES (XSTRING (filename)) + total + 1); |
570d7624 JB |
2038 | x = xnm; |
2039 | ||
2040 | /* Copy the rest of the name through, replacing $ constructs with values */ | |
2041 | for (p = nm; *p;) | |
2042 | if (*p != '$') | |
2043 | *x++ = *p++; | |
2044 | else | |
2045 | { | |
2046 | p++; | |
2047 | if (p == endp) | |
2048 | goto badsubst; | |
2049 | else if (*p == '$') | |
2050 | { | |
2051 | *x++ = *p++; | |
2052 | continue; | |
2053 | } | |
2054 | else if (*p == '{') | |
2055 | { | |
2056 | o = ++p; | |
2057 | while (p != endp && *p != '}') p++; | |
2058 | if (*p != '}') goto missingclose; | |
2059 | s = p++; | |
2060 | } | |
2061 | else | |
2062 | { | |
2063 | o = p; | |
2064 | while (p != endp && (isalnum (*p) || *p == '_')) p++; | |
2065 | s = p; | |
2066 | } | |
2067 | ||
2068 | /* Copy out the variable name */ | |
2069 | target = (unsigned char *) alloca (s - o + 1); | |
2070 | strncpy (target, o, s - o); | |
2071 | target[s - o] = 0; | |
5e570b75 | 2072 | #ifdef DOS_NT |
4c3c22f3 | 2073 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 2074 | #endif /* DOS_NT */ |
570d7624 JB |
2075 | |
2076 | /* Get variable value */ | |
2077 | o = (unsigned char *) egetenv (target); | |
570d7624 JB |
2078 | if (!o) |
2079 | goto badvar; | |
2080 | ||
60d67b83 RS |
2081 | if (STRING_MULTIBYTE (filename)) |
2082 | { | |
2083 | /* If the original string is multibyte, | |
2084 | convert what we substitute into multibyte. */ | |
2085 | unsigned char workbuf[4], *str; | |
2086 | int len; | |
60d67b83 RS |
2087 | |
2088 | while (*o) | |
2089 | { | |
2090 | int c = *o++; | |
418cd8ca RS |
2091 | c = unibyte_char_to_multibyte (c); |
2092 | if (! SINGLE_BYTE_CHAR_P (c)) | |
60d67b83 | 2093 | { |
60d67b83 RS |
2094 | len = CHAR_STRING (c, workbuf, str); |
2095 | bcopy (str, x, len); | |
2096 | x += len; | |
2097 | } | |
2098 | else | |
2099 | *x++ = c; | |
2100 | } | |
2101 | } | |
2102 | else | |
2103 | { | |
2104 | strcpy (x, o); | |
2105 | x += strlen (o); | |
2106 | } | |
570d7624 JB |
2107 | } |
2108 | ||
2109 | *x = 0; | |
2110 | ||
82330e7f | 2111 | /* If /~ or // appears, discard everything through first slash. */ |
570d7624 JB |
2112 | |
2113 | for (p = xnm; p != x; p++) | |
5e570b75 | 2114 | if ((p[0] == '~' |
199607e4 | 2115 | #if defined (APOLLO) || defined (WINDOWSNT) |
5e570b75 | 2116 | || (IS_DIRECTORY_SEP (p[0]) && p - 1 != xnm) |
199607e4 RS |
2117 | #else /* not (APOLLO || WINDOWSNT) */ |
2118 | || IS_DIRECTORY_SEP (p[0]) | |
2119 | #endif /* not (APOLLO || WINDOWSNT) */ | |
570d7624 | 2120 | ) |
ac5b7072 | 2121 | && p != xnm && IS_DIRECTORY_SEP (p[-1])) |
570d7624 | 2122 | xnm = p; |
5e570b75 | 2123 | #ifdef DOS_NT |
199607e4 | 2124 | else if (IS_DRIVE (p[0]) && p[1] == ':' |
4488d7e1 | 2125 | && p > xnm && IS_DIRECTORY_SEP (p[-1])) |
199607e4 | 2126 | xnm = p; |
4c3c22f3 | 2127 | #endif |
570d7624 | 2128 | |
60d67b83 RS |
2129 | if (STRING_MULTIBYTE (filename)) |
2130 | return make_string (xnm, x - xnm); | |
2131 | return make_unibyte_string (xnm, x - xnm); | |
570d7624 JB |
2132 | |
2133 | badsubst: | |
2134 | error ("Bad format environment-variable substitution"); | |
2135 | missingclose: | |
2136 | error ("Missing \"}\" in environment-variable substitution"); | |
2137 | badvar: | |
2138 | error ("Substituting nonexistent environment variable \"%s\"", target); | |
2139 | ||
2140 | /* NOTREACHED */ | |
2141 | #endif /* not VMS */ | |
2142 | } | |
2143 | \f | |
067ffa38 | 2144 | /* A slightly faster and more convenient way to get |
298b760e | 2145 | (directory-file-name (expand-file-name FOO)). */ |
067ffa38 | 2146 | |
570d7624 JB |
2147 | Lisp_Object |
2148 | expand_and_dir_to_file (filename, defdir) | |
2149 | Lisp_Object filename, defdir; | |
2150 | { | |
199607e4 | 2151 | register Lisp_Object absname; |
570d7624 | 2152 | |
199607e4 | 2153 | absname = Fexpand_file_name (filename, defdir); |
570d7624 JB |
2154 | #ifdef VMS |
2155 | { | |
fc932ac6 | 2156 | register int c = XSTRING (absname)->data[STRING_BYTES (XSTRING (absname)) - 1]; |
570d7624 | 2157 | if (c == ':' || c == ']' || c == '>') |
199607e4 | 2158 | absname = Fdirectory_file_name (absname); |
570d7624 JB |
2159 | } |
2160 | #else | |
199607e4 | 2161 | /* Remove final slash, if any (unless this is the root dir). |
570d7624 | 2162 | stat behaves differently depending! */ |
199607e4 | 2163 | if (XSTRING (absname)->size > 1 |
fc932ac6 RS |
2164 | && IS_DIRECTORY_SEP (XSTRING (absname)->data[STRING_BYTES (XSTRING (absname)) - 1]) |
2165 | && !IS_DEVICE_SEP (XSTRING (absname)->data[STRING_BYTES (XSTRING (absname))-2])) | |
ddc61f46 | 2166 | /* We cannot take shortcuts; they might be wrong for magic file names. */ |
199607e4 | 2167 | absname = Fdirectory_file_name (absname); |
570d7624 | 2168 | #endif |
199607e4 | 2169 | return absname; |
570d7624 JB |
2170 | } |
2171 | \f | |
3ed15d97 RS |
2172 | /* Signal an error if the file ABSNAME already exists. |
2173 | If INTERACTIVE is nonzero, ask the user whether to proceed, | |
2174 | and bypass the error if the user says to go ahead. | |
2175 | QUERYSTRING is a name for the action that is being considered | |
2176 | to alter the file. | |
de1d0127 | 2177 | |
3ed15d97 | 2178 | *STATPTR is used to store the stat information if the file exists. |
de1d0127 | 2179 | If the file does not exist, STATPTR->st_mode is set to 0. |
b8b29dc9 RS |
2180 | If STATPTR is null, we don't store into it. |
2181 | ||
2182 | If QUICK is nonzero, we ask for y or n, not yes or no. */ | |
3ed15d97 | 2183 | |
c4df73f9 | 2184 | void |
b8b29dc9 | 2185 | barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick) |
570d7624 JB |
2186 | Lisp_Object absname; |
2187 | unsigned char *querystring; | |
2188 | int interactive; | |
3ed15d97 | 2189 | struct stat *statptr; |
b8b29dc9 | 2190 | int quick; |
570d7624 | 2191 | { |
643c73b9 | 2192 | register Lisp_Object tem, encoded_filename; |
4018b5ef | 2193 | struct stat statbuf; |
570d7624 JB |
2194 | struct gcpro gcpro1; |
2195 | ||
643c73b9 RS |
2196 | encoded_filename = ENCODE_FILE (absname); |
2197 | ||
4018b5ef RS |
2198 | /* stat is a good way to tell whether the file exists, |
2199 | regardless of what access permissions it has. */ | |
643c73b9 | 2200 | if (stat (XSTRING (encoded_filename)->data, &statbuf) >= 0) |
570d7624 JB |
2201 | { |
2202 | if (! interactive) | |
2203 | Fsignal (Qfile_already_exists, | |
2204 | Fcons (build_string ("File already exists"), | |
2205 | Fcons (absname, Qnil))); | |
2206 | GCPRO1 (absname); | |
b8b29dc9 RS |
2207 | tem = format1 ("File %s already exists; %s anyway? ", |
2208 | XSTRING (absname)->data, querystring); | |
2209 | if (quick) | |
2210 | tem = Fy_or_n_p (tem); | |
2211 | else | |
2212 | tem = do_yes_or_no_p (tem); | |
570d7624 | 2213 | UNGCPRO; |
265a9e55 | 2214 | if (NILP (tem)) |
570d7624 JB |
2215 | Fsignal (Qfile_already_exists, |
2216 | Fcons (build_string ("File already exists"), | |
2217 | Fcons (absname, Qnil))); | |
3ed15d97 RS |
2218 | if (statptr) |
2219 | *statptr = statbuf; | |
2220 | } | |
2221 | else | |
2222 | { | |
2223 | if (statptr) | |
2224 | statptr->st_mode = 0; | |
570d7624 JB |
2225 | } |
2226 | return; | |
2227 | } | |
2228 | ||
2229 | DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4, | |
349a7710 | 2230 | "fCopy file: \nFCopy %s to file: \np\nP", |
570d7624 JB |
2231 | "Copy FILE to NEWNAME. Both args must be strings.\n\ |
2232 | Signals a `file-already-exists' error if file NEWNAME already exists,\n\ | |
2233 | unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.\n\ | |
2234 | A number as third arg means request confirmation if NEWNAME already exists.\n\ | |
2235 | This is what happens in interactive use with M-x.\n\ | |
349a7710 JB |
2236 | Fourth arg KEEP-TIME non-nil means give the new file the same\n\ |
2237 | last-modified time as the old one. (This works on only some systems.)\n\ | |
2238 | A prefix arg makes KEEP-TIME non-nil.") | |
3b7f6e60 EN |
2239 | (file, newname, ok_if_already_exists, keep_date) |
2240 | Lisp_Object file, newname, ok_if_already_exists, keep_date; | |
570d7624 JB |
2241 | { |
2242 | int ifd, ofd, n; | |
2243 | char buf[16 * 1024]; | |
3ed15d97 | 2244 | struct stat st, out_st; |
32f4334d | 2245 | Lisp_Object handler; |
b1d1b865 | 2246 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
b5148e85 | 2247 | int count = specpdl_ptr - specpdl; |
f73b0ada | 2248 | int input_file_statable_p; |
b1d1b865 | 2249 | Lisp_Object encoded_file, encoded_newname; |
570d7624 | 2250 | |
b1d1b865 RS |
2251 | encoded_file = encoded_newname = Qnil; |
2252 | GCPRO4 (file, newname, encoded_file, encoded_newname); | |
3b7f6e60 | 2253 | CHECK_STRING (file, 0); |
570d7624 | 2254 | CHECK_STRING (newname, 1); |
b1d1b865 | 2255 | |
3b7f6e60 | 2256 | file = Fexpand_file_name (file, Qnil); |
570d7624 | 2257 | newname = Fexpand_file_name (newname, Qnil); |
32f4334d | 2258 | |
0bf2eed2 | 2259 | /* If the input file name has special constructs in it, |
32f4334d | 2260 | call the corresponding file handler. */ |
3b7f6e60 | 2261 | handler = Ffind_file_name_handler (file, Qcopy_file); |
0bf2eed2 | 2262 | /* Likewise for output file name. */ |
51cf6d37 | 2263 | if (NILP (handler)) |
49307295 | 2264 | handler = Ffind_file_name_handler (newname, Qcopy_file); |
32f4334d | 2265 | if (!NILP (handler)) |
3b7f6e60 | 2266 | RETURN_UNGCPRO (call5 (handler, Qcopy_file, file, newname, |
36712b0a | 2267 | ok_if_already_exists, keep_date)); |
32f4334d | 2268 | |
b1d1b865 RS |
2269 | encoded_file = ENCODE_FILE (file); |
2270 | encoded_newname = ENCODE_FILE (newname); | |
2271 | ||
265a9e55 | 2272 | if (NILP (ok_if_already_exists) |
93c30b5f | 2273 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2274 | barf_or_query_if_file_exists (encoded_newname, "copy to it", |
b8b29dc9 | 2275 | INTEGERP (ok_if_already_exists), &out_st, 0); |
b1d1b865 | 2276 | else if (stat (XSTRING (encoded_newname)->data, &out_st) < 0) |
3ed15d97 | 2277 | out_st.st_mode = 0; |
570d7624 | 2278 | |
b1d1b865 | 2279 | ifd = open (XSTRING (encoded_file)->data, O_RDONLY); |
570d7624 | 2280 | if (ifd < 0) |
3b7f6e60 | 2281 | report_file_error ("Opening input file", Fcons (file, Qnil)); |
570d7624 | 2282 | |
b5148e85 RS |
2283 | record_unwind_protect (close_file_unwind, make_number (ifd)); |
2284 | ||
f73b0ada BF |
2285 | /* We can only copy regular files and symbolic links. Other files are not |
2286 | copyable by us. */ | |
2287 | input_file_statable_p = (fstat (ifd, &st) >= 0); | |
2288 | ||
f9ba66ce | 2289 | #if !defined (DOS_NT) || __DJGPP__ > 1 |
3ed15d97 RS |
2290 | if (out_st.st_mode != 0 |
2291 | && st.st_dev == out_st.st_dev && st.st_ino == out_st.st_ino) | |
2292 | { | |
2293 | errno = 0; | |
2294 | report_file_error ("Input and output files are the same", | |
3b7f6e60 | 2295 | Fcons (file, Fcons (newname, Qnil))); |
3ed15d97 RS |
2296 | } |
2297 | #endif | |
2298 | ||
f73b0ada BF |
2299 | #if defined (S_ISREG) && defined (S_ISLNK) |
2300 | if (input_file_statable_p) | |
2301 | { | |
2302 | if (!(S_ISREG (st.st_mode)) && !(S_ISLNK (st.st_mode))) | |
2303 | { | |
2304 | #if defined (EISDIR) | |
2305 | /* Get a better looking error message. */ | |
2306 | errno = EISDIR; | |
2307 | #endif /* EISDIR */ | |
3b7f6e60 | 2308 | report_file_error ("Non-regular file", Fcons (file, Qnil)); |
f73b0ada BF |
2309 | } |
2310 | } | |
2311 | #endif /* S_ISREG && S_ISLNK */ | |
2312 | ||
570d7624 JB |
2313 | #ifdef VMS |
2314 | /* Create the copy file with the same record format as the input file */ | |
b1d1b865 | 2315 | ofd = sys_creat (XSTRING (encoded_newname)->data, 0666, ifd); |
570d7624 | 2316 | #else |
4c3c22f3 RS |
2317 | #ifdef MSDOS |
2318 | /* System's default file type was set to binary by _fmode in emacs.c. */ | |
b1d1b865 | 2319 | ofd = creat (XSTRING (encoded_newname)->data, S_IREAD | S_IWRITE); |
4c3c22f3 | 2320 | #else /* not MSDOS */ |
b1d1b865 | 2321 | ofd = creat (XSTRING (encoded_newname)->data, 0666); |
4c3c22f3 | 2322 | #endif /* not MSDOS */ |
570d7624 JB |
2323 | #endif /* VMS */ |
2324 | if (ofd < 0) | |
3ed15d97 | 2325 | report_file_error ("Opening output file", Fcons (newname, Qnil)); |
b5148e85 RS |
2326 | |
2327 | record_unwind_protect (close_file_unwind, make_number (ofd)); | |
570d7624 | 2328 | |
b5148e85 RS |
2329 | immediate_quit = 1; |
2330 | QUIT; | |
570d7624 JB |
2331 | while ((n = read (ifd, buf, sizeof buf)) > 0) |
2332 | if (write (ofd, buf, n) != n) | |
3ed15d97 | 2333 | report_file_error ("I/O error", Fcons (newname, Qnil)); |
b5148e85 | 2334 | immediate_quit = 0; |
570d7624 | 2335 | |
5acac34e RS |
2336 | /* Closing the output clobbers the file times on some systems. */ |
2337 | if (close (ofd) < 0) | |
2338 | report_file_error ("I/O error", Fcons (newname, Qnil)); | |
2339 | ||
f73b0ada | 2340 | if (input_file_statable_p) |
570d7624 | 2341 | { |
265a9e55 | 2342 | if (!NILP (keep_date)) |
570d7624 | 2343 | { |
de5bf5d3 JB |
2344 | EMACS_TIME atime, mtime; |
2345 | EMACS_SET_SECS_USECS (atime, st.st_atime, 0); | |
2346 | EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0); | |
b1d1b865 RS |
2347 | if (set_file_times (XSTRING (encoded_newname)->data, |
2348 | atime, mtime)) | |
c0b7b21c | 2349 | Fsignal (Qfile_date_error, |
d1b9ed63 | 2350 | Fcons (build_string ("Cannot set file date"), |
3dbcf3f6 | 2351 | Fcons (newname, Qnil))); |
570d7624 | 2352 | } |
2dc3be7e | 2353 | #ifndef MSDOS |
b1d1b865 | 2354 | chmod (XSTRING (encoded_newname)->data, st.st_mode & 07777); |
2dc3be7e RS |
2355 | #else /* MSDOS */ |
2356 | #if defined (__DJGPP__) && __DJGPP__ > 1 | |
2357 | /* In DJGPP v2.0 and later, fstat usually returns true file mode bits, | |
2358 | and if it can't, it tells so. Otherwise, under MSDOS we usually | |
2359 | get only the READ bit, which will make the copied file read-only, | |
2360 | so it's better not to chmod at all. */ | |
2361 | if ((_djstat_flags & _STFAIL_WRITEBIT) == 0) | |
b1d1b865 | 2362 | chmod (XSTRING (encoded_newname)->data, st.st_mode & 07777); |
2dc3be7e RS |
2363 | #endif /* DJGPP version 2 or newer */ |
2364 | #endif /* MSDOS */ | |
570d7624 JB |
2365 | } |
2366 | ||
5acac34e RS |
2367 | close (ifd); |
2368 | ||
b5148e85 RS |
2369 | /* Discard the unwind protects. */ |
2370 | specpdl_ptr = specpdl + count; | |
2371 | ||
570d7624 JB |
2372 | UNGCPRO; |
2373 | return Qnil; | |
2374 | } | |
385b6cc7 | 2375 | \f |
9bbe01fb | 2376 | DEFUN ("make-directory-internal", Fmake_directory_internal, |
353cfc19 | 2377 | Smake_directory_internal, 1, 1, 0, |
3b7f6e60 EN |
2378 | "Create a new directory named DIRECTORY.") |
2379 | (directory) | |
2380 | Lisp_Object directory; | |
570d7624 JB |
2381 | { |
2382 | unsigned char *dir; | |
32f4334d | 2383 | Lisp_Object handler; |
b1d1b865 | 2384 | Lisp_Object encoded_dir; |
570d7624 | 2385 | |
3b7f6e60 EN |
2386 | CHECK_STRING (directory, 0); |
2387 | directory = Fexpand_file_name (directory, Qnil); | |
32f4334d | 2388 | |
3b7f6e60 | 2389 | handler = Ffind_file_name_handler (directory, Qmake_directory_internal); |
32f4334d | 2390 | if (!NILP (handler)) |
3b7f6e60 | 2391 | return call2 (handler, Qmake_directory_internal, directory); |
9bbe01fb | 2392 | |
b1d1b865 RS |
2393 | encoded_dir = ENCODE_FILE (directory); |
2394 | ||
2395 | dir = XSTRING (encoded_dir)->data; | |
570d7624 | 2396 | |
5e570b75 RS |
2397 | #ifdef WINDOWSNT |
2398 | if (mkdir (dir) != 0) | |
2399 | #else | |
570d7624 | 2400 | if (mkdir (dir, 0777) != 0) |
5e570b75 | 2401 | #endif |
3b7f6e60 | 2402 | report_file_error ("Creating directory", Flist (1, &directory)); |
570d7624 | 2403 | |
32f4334d | 2404 | return Qnil; |
570d7624 JB |
2405 | } |
2406 | ||
aa734e17 | 2407 | DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete directory: ", |
3b7f6e60 EN |
2408 | "Delete the directory named DIRECTORY.") |
2409 | (directory) | |
2410 | Lisp_Object directory; | |
570d7624 JB |
2411 | { |
2412 | unsigned char *dir; | |
32f4334d | 2413 | Lisp_Object handler; |
b1d1b865 | 2414 | Lisp_Object encoded_dir; |
570d7624 | 2415 | |
3b7f6e60 EN |
2416 | CHECK_STRING (directory, 0); |
2417 | directory = Fdirectory_file_name (Fexpand_file_name (directory, Qnil)); | |
570d7624 | 2418 | |
3b7f6e60 | 2419 | handler = Ffind_file_name_handler (directory, Qdelete_directory); |
32f4334d | 2420 | if (!NILP (handler)) |
3b7f6e60 | 2421 | return call2 (handler, Qdelete_directory, directory); |
32f4334d | 2422 | |
b1d1b865 RS |
2423 | encoded_dir = ENCODE_FILE (directory); |
2424 | ||
2425 | dir = XSTRING (encoded_dir)->data; | |
2426 | ||
570d7624 | 2427 | if (rmdir (dir) != 0) |
3b7f6e60 | 2428 | report_file_error ("Removing directory", Flist (1, &directory)); |
570d7624 JB |
2429 | |
2430 | return Qnil; | |
2431 | } | |
2432 | ||
2433 | DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: ", | |
3b7f6e60 | 2434 | "Delete file named FILENAME.\n\ |
570d7624 JB |
2435 | If file has multiple names, it continues to exist with the other names.") |
2436 | (filename) | |
2437 | Lisp_Object filename; | |
2438 | { | |
32f4334d | 2439 | Lisp_Object handler; |
b1d1b865 RS |
2440 | Lisp_Object encoded_file; |
2441 | ||
570d7624 JB |
2442 | CHECK_STRING (filename, 0); |
2443 | filename = Fexpand_file_name (filename, Qnil); | |
32f4334d | 2444 | |
49307295 | 2445 | handler = Ffind_file_name_handler (filename, Qdelete_file); |
32f4334d | 2446 | if (!NILP (handler)) |
8a9b0da9 | 2447 | return call2 (handler, Qdelete_file, filename); |
32f4334d | 2448 | |
b1d1b865 RS |
2449 | encoded_file = ENCODE_FILE (filename); |
2450 | ||
2451 | if (0 > unlink (XSTRING (encoded_file)->data)) | |
570d7624 | 2452 | report_file_error ("Removing old name", Flist (1, &filename)); |
8a9b0da9 | 2453 | return Qnil; |
570d7624 JB |
2454 | } |
2455 | ||
385b6cc7 RS |
2456 | static Lisp_Object |
2457 | internal_delete_file_1 (ignore) | |
2458 | Lisp_Object ignore; | |
2459 | { | |
2460 | return Qt; | |
2461 | } | |
2462 | ||
2463 | /* Delete file FILENAME, returning 1 if successful and 0 if failed. */ | |
2464 | ||
2465 | int | |
2466 | internal_delete_file (filename) | |
2467 | Lisp_Object filename; | |
2468 | { | |
2469 | return NILP (internal_condition_case_1 (Fdelete_file, filename, | |
2470 | Qt, internal_delete_file_1)); | |
2471 | } | |
2472 | \f | |
570d7624 JB |
2473 | DEFUN ("rename-file", Frename_file, Srename_file, 2, 3, |
2474 | "fRename file: \nFRename %s to file: \np", | |
2475 | "Rename FILE as NEWNAME. Both args strings.\n\ | |
2476 | If file has names other than FILE, it continues to have those names.\n\ | |
2477 | Signals a `file-already-exists' error if a file NEWNAME already exists\n\ | |
2478 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\ | |
2479 | A number as third arg means request confirmation if NEWNAME already exists.\n\ | |
2480 | This is what happens in interactive use with M-x.") | |
3b7f6e60 EN |
2481 | (file, newname, ok_if_already_exists) |
2482 | Lisp_Object file, newname, ok_if_already_exists; | |
570d7624 JB |
2483 | { |
2484 | #ifdef NO_ARG_ARRAY | |
2485 | Lisp_Object args[2]; | |
2486 | #endif | |
32f4334d | 2487 | Lisp_Object handler; |
b1d1b865 RS |
2488 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
2489 | Lisp_Object encoded_file, encoded_newname; | |
570d7624 | 2490 | |
b1d1b865 RS |
2491 | encoded_file = encoded_newname = Qnil; |
2492 | GCPRO4 (file, newname, encoded_file, encoded_newname); | |
3b7f6e60 | 2493 | CHECK_STRING (file, 0); |
570d7624 | 2494 | CHECK_STRING (newname, 1); |
3b7f6e60 | 2495 | file = Fexpand_file_name (file, Qnil); |
570d7624 | 2496 | newname = Fexpand_file_name (newname, Qnil); |
32f4334d RS |
2497 | |
2498 | /* If the file name has special constructs in it, | |
2499 | call the corresponding file handler. */ | |
3b7f6e60 | 2500 | handler = Ffind_file_name_handler (file, Qrename_file); |
51cf6d37 | 2501 | if (NILP (handler)) |
49307295 | 2502 | handler = Ffind_file_name_handler (newname, Qrename_file); |
32f4334d | 2503 | if (!NILP (handler)) |
36712b0a | 2504 | RETURN_UNGCPRO (call4 (handler, Qrename_file, |
3b7f6e60 | 2505 | file, newname, ok_if_already_exists)); |
32f4334d | 2506 | |
b1d1b865 RS |
2507 | encoded_file = ENCODE_FILE (file); |
2508 | encoded_newname = ENCODE_FILE (newname); | |
2509 | ||
265a9e55 | 2510 | if (NILP (ok_if_already_exists) |
93c30b5f | 2511 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2512 | barf_or_query_if_file_exists (encoded_newname, "rename to it", |
b8b29dc9 | 2513 | INTEGERP (ok_if_already_exists), 0, 0); |
570d7624 | 2514 | #ifndef BSD4_1 |
b1d1b865 | 2515 | if (0 > rename (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data)) |
570d7624 | 2516 | #else |
b1d1b865 RS |
2517 | if (0 > link (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data) |
2518 | || 0 > unlink (XSTRING (encoded_file)->data)) | |
570d7624 JB |
2519 | #endif |
2520 | { | |
2521 | if (errno == EXDEV) | |
2522 | { | |
3b7f6e60 | 2523 | Fcopy_file (file, newname, |
d093c3ac RM |
2524 | /* We have already prompted if it was an integer, |
2525 | so don't have copy-file prompt again. */ | |
2526 | NILP (ok_if_already_exists) ? Qnil : Qt, Qt); | |
3b7f6e60 | 2527 | Fdelete_file (file); |
570d7624 JB |
2528 | } |
2529 | else | |
2530 | #ifdef NO_ARG_ARRAY | |
2531 | { | |
3b7f6e60 | 2532 | args[0] = file; |
570d7624 JB |
2533 | args[1] = newname; |
2534 | report_file_error ("Renaming", Flist (2, args)); | |
2535 | } | |
2536 | #else | |
3b7f6e60 | 2537 | report_file_error ("Renaming", Flist (2, &file)); |
570d7624 JB |
2538 | #endif |
2539 | } | |
2540 | UNGCPRO; | |
2541 | return Qnil; | |
2542 | } | |
2543 | ||
2544 | DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3, | |
2545 | "fAdd name to file: \nFName to add to %s: \np", | |
2546 | "Give FILE additional name NEWNAME. Both args strings.\n\ | |
2547 | Signals a `file-already-exists' error if a file NEWNAME already exists\n\ | |
2548 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\ | |
2549 | A number as third arg means request confirmation if NEWNAME already exists.\n\ | |
2550 | This is what happens in interactive use with M-x.") | |
3b7f6e60 EN |
2551 | (file, newname, ok_if_already_exists) |
2552 | Lisp_Object file, newname, ok_if_already_exists; | |
570d7624 JB |
2553 | { |
2554 | #ifdef NO_ARG_ARRAY | |
2555 | Lisp_Object args[2]; | |
2556 | #endif | |
32f4334d | 2557 | Lisp_Object handler; |
b1d1b865 RS |
2558 | Lisp_Object encoded_file, encoded_newname; |
2559 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2560 | |
b1d1b865 RS |
2561 | GCPRO4 (file, newname, encoded_file, encoded_newname); |
2562 | encoded_file = encoded_newname = Qnil; | |
3b7f6e60 | 2563 | CHECK_STRING (file, 0); |
570d7624 | 2564 | CHECK_STRING (newname, 1); |
3b7f6e60 | 2565 | file = Fexpand_file_name (file, Qnil); |
570d7624 | 2566 | newname = Fexpand_file_name (newname, Qnil); |
32f4334d RS |
2567 | |
2568 | /* If the file name has special constructs in it, | |
2569 | call the corresponding file handler. */ | |
3b7f6e60 | 2570 | handler = Ffind_file_name_handler (file, Qadd_name_to_file); |
32f4334d | 2571 | if (!NILP (handler)) |
3b7f6e60 | 2572 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
36712b0a | 2573 | newname, ok_if_already_exists)); |
32f4334d | 2574 | |
adc6741c RS |
2575 | /* If the new name has special constructs in it, |
2576 | call the corresponding file handler. */ | |
2577 | handler = Ffind_file_name_handler (newname, Qadd_name_to_file); | |
2578 | if (!NILP (handler)) | |
3b7f6e60 | 2579 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
adc6741c RS |
2580 | newname, ok_if_already_exists)); |
2581 | ||
b1d1b865 RS |
2582 | encoded_file = ENCODE_FILE (file); |
2583 | encoded_newname = ENCODE_FILE (newname); | |
2584 | ||
265a9e55 | 2585 | if (NILP (ok_if_already_exists) |
93c30b5f | 2586 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2587 | barf_or_query_if_file_exists (encoded_newname, "make it a new name", |
b8b29dc9 | 2588 | INTEGERP (ok_if_already_exists), 0, 0); |
5e570b75 | 2589 | |
570d7624 | 2590 | unlink (XSTRING (newname)->data); |
b1d1b865 | 2591 | if (0 > link (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data)) |
570d7624 JB |
2592 | { |
2593 | #ifdef NO_ARG_ARRAY | |
3b7f6e60 | 2594 | args[0] = file; |
570d7624 JB |
2595 | args[1] = newname; |
2596 | report_file_error ("Adding new name", Flist (2, args)); | |
2597 | #else | |
3b7f6e60 | 2598 | report_file_error ("Adding new name", Flist (2, &file)); |
570d7624 JB |
2599 | #endif |
2600 | } | |
2601 | ||
2602 | UNGCPRO; | |
2603 | return Qnil; | |
2604 | } | |
2605 | ||
2606 | #ifdef S_IFLNK | |
2607 | DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3, | |
2608 | "FMake symbolic link to file: \nFMake symbolic link to file %s: \np", | |
2609 | "Make a symbolic link to FILENAME, named LINKNAME. Both args strings.\n\ | |
11183104 | 2610 | Signals a `file-already-exists' error if a file LINKNAME already exists\n\ |
570d7624 | 2611 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\ |
11183104 | 2612 | A number as third arg means request confirmation if LINKNAME already exists.\n\ |
570d7624 | 2613 | This happens for interactive use with M-x.") |
e5d77022 JB |
2614 | (filename, linkname, ok_if_already_exists) |
2615 | Lisp_Object filename, linkname, ok_if_already_exists; | |
570d7624 JB |
2616 | { |
2617 | #ifdef NO_ARG_ARRAY | |
2618 | Lisp_Object args[2]; | |
2619 | #endif | |
32f4334d | 2620 | Lisp_Object handler; |
b1d1b865 RS |
2621 | Lisp_Object encoded_filename, encoded_linkname; |
2622 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2623 | |
b1d1b865 RS |
2624 | GCPRO4 (filename, linkname, encoded_filename, encoded_linkname); |
2625 | encoded_filename = encoded_linkname = Qnil; | |
570d7624 | 2626 | CHECK_STRING (filename, 0); |
e5d77022 | 2627 | CHECK_STRING (linkname, 1); |
d9bc1c99 RS |
2628 | /* If the link target has a ~, we must expand it to get |
2629 | a truly valid file name. Otherwise, do not expand; | |
2630 | we want to permit links to relative file names. */ | |
2631 | if (XSTRING (filename)->data[0] == '~') | |
2632 | filename = Fexpand_file_name (filename, Qnil); | |
e5d77022 | 2633 | linkname = Fexpand_file_name (linkname, Qnil); |
32f4334d RS |
2634 | |
2635 | /* If the file name has special constructs in it, | |
2636 | call the corresponding file handler. */ | |
49307295 | 2637 | handler = Ffind_file_name_handler (filename, Qmake_symbolic_link); |
32f4334d | 2638 | if (!NILP (handler)) |
36712b0a KH |
2639 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, |
2640 | linkname, ok_if_already_exists)); | |
32f4334d | 2641 | |
adc6741c RS |
2642 | /* If the new link name has special constructs in it, |
2643 | call the corresponding file handler. */ | |
2644 | handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link); | |
2645 | if (!NILP (handler)) | |
2646 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, | |
2647 | linkname, ok_if_already_exists)); | |
2648 | ||
b1d1b865 RS |
2649 | encoded_filename = ENCODE_FILE (filename); |
2650 | encoded_linkname = ENCODE_FILE (linkname); | |
2651 | ||
265a9e55 | 2652 | if (NILP (ok_if_already_exists) |
93c30b5f | 2653 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2654 | barf_or_query_if_file_exists (encoded_linkname, "make it a link", |
b8b29dc9 | 2655 | INTEGERP (ok_if_already_exists), 0, 0); |
b1d1b865 RS |
2656 | if (0 > symlink (XSTRING (encoded_filename)->data, |
2657 | XSTRING (encoded_linkname)->data)) | |
570d7624 JB |
2658 | { |
2659 | /* If we didn't complain already, silently delete existing file. */ | |
2660 | if (errno == EEXIST) | |
2661 | { | |
b1d1b865 RS |
2662 | unlink (XSTRING (encoded_linkname)->data); |
2663 | if (0 <= symlink (XSTRING (encoded_filename)->data, | |
2664 | XSTRING (encoded_linkname)->data)) | |
1a04498e KH |
2665 | { |
2666 | UNGCPRO; | |
2667 | return Qnil; | |
2668 | } | |
570d7624 JB |
2669 | } |
2670 | ||
2671 | #ifdef NO_ARG_ARRAY | |
2672 | args[0] = filename; | |
e5d77022 | 2673 | args[1] = linkname; |
570d7624 JB |
2674 | report_file_error ("Making symbolic link", Flist (2, args)); |
2675 | #else | |
2676 | report_file_error ("Making symbolic link", Flist (2, &filename)); | |
2677 | #endif | |
2678 | } | |
2679 | UNGCPRO; | |
2680 | return Qnil; | |
2681 | } | |
2682 | #endif /* S_IFLNK */ | |
2683 | ||
2684 | #ifdef VMS | |
2685 | ||
2686 | DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name, | |
2687 | 2, 2, "sDefine logical name: \nsDefine logical name %s as: ", | |
2688 | "Define the job-wide logical name NAME to have the value STRING.\n\ | |
2689 | If STRING is nil or a null string, the logical name NAME is deleted.") | |
3b7f6e60 EN |
2690 | (name, string) |
2691 | Lisp_Object name; | |
570d7624 JB |
2692 | Lisp_Object string; |
2693 | { | |
3b7f6e60 | 2694 | CHECK_STRING (name, 0); |
265a9e55 | 2695 | if (NILP (string)) |
3b7f6e60 | 2696 | delete_logical_name (XSTRING (name)->data); |
570d7624 JB |
2697 | else |
2698 | { | |
2699 | CHECK_STRING (string, 1); | |
2700 | ||
2701 | if (XSTRING (string)->size == 0) | |
3b7f6e60 | 2702 | delete_logical_name (XSTRING (name)->data); |
570d7624 | 2703 | else |
3b7f6e60 | 2704 | define_logical_name (XSTRING (name)->data, XSTRING (string)->data); |
570d7624 JB |
2705 | } |
2706 | ||
2707 | return string; | |
2708 | } | |
2709 | #endif /* VMS */ | |
2710 | ||
2711 | #ifdef HPUX_NET | |
2712 | ||
2713 | DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0, | |
2714 | "Open a network connection to PATH using LOGIN as the login string.") | |
2715 | (path, login) | |
2716 | Lisp_Object path, login; | |
2717 | { | |
2718 | int netresult; | |
199607e4 | 2719 | |
570d7624 | 2720 | CHECK_STRING (path, 0); |
199607e4 RS |
2721 | CHECK_STRING (login, 0); |
2722 | ||
570d7624 JB |
2723 | netresult = netunam (XSTRING (path)->data, XSTRING (login)->data); |
2724 | ||
2725 | if (netresult == -1) | |
2726 | return Qnil; | |
2727 | else | |
2728 | return Qt; | |
2729 | } | |
2730 | #endif /* HPUX_NET */ | |
2731 | \f | |
2732 | DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p, | |
2733 | 1, 1, 0, | |
199607e4 | 2734 | "Return t if file FILENAME specifies an absolute file name.\n\ |
570d7624 JB |
2735 | On Unix, this is a name starting with a `/' or a `~'.") |
2736 | (filename) | |
2737 | Lisp_Object filename; | |
2738 | { | |
2739 | unsigned char *ptr; | |
2740 | ||
2741 | CHECK_STRING (filename, 0); | |
2742 | ptr = XSTRING (filename)->data; | |
5e570b75 | 2743 | if (IS_DIRECTORY_SEP (*ptr) || *ptr == '~' |
570d7624 JB |
2744 | #ifdef VMS |
2745 | /* ??? This criterion is probably wrong for '<'. */ | |
2746 | || index (ptr, ':') || index (ptr, '<') | |
2747 | || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']')) | |
2748 | && ptr[1] != '.') | |
2749 | #endif /* VMS */ | |
5e570b75 | 2750 | #ifdef DOS_NT |
199607e4 | 2751 | || (IS_DRIVE (*ptr) && ptr[1] == ':' && IS_DIRECTORY_SEP (ptr[2])) |
4c3c22f3 | 2752 | #endif |
570d7624 JB |
2753 | ) |
2754 | return Qt; | |
2755 | else | |
2756 | return Qnil; | |
2757 | } | |
3beeedfe RS |
2758 | \f |
2759 | /* Return nonzero if file FILENAME exists and can be executed. */ | |
2760 | ||
2761 | static int | |
2762 | check_executable (filename) | |
2763 | char *filename; | |
2764 | { | |
3be3c08e RS |
2765 | #ifdef DOS_NT |
2766 | int len = strlen (filename); | |
2767 | char *suffix; | |
2768 | struct stat st; | |
2769 | if (stat (filename, &st) < 0) | |
2770 | return 0; | |
34ead71a | 2771 | #if defined (WINDOWSNT) || (defined (MSDOS) && __DJGPP__ > 1) |
199607e4 RS |
2772 | return ((st.st_mode & S_IEXEC) != 0); |
2773 | #else | |
3be3c08e RS |
2774 | return (S_ISREG (st.st_mode) |
2775 | && len >= 5 | |
2776 | && (stricmp ((suffix = filename + len-4), ".com") == 0 | |
2777 | || stricmp (suffix, ".exe") == 0 | |
2dc3be7e RS |
2778 | || stricmp (suffix, ".bat") == 0) |
2779 | || (st.st_mode & S_IFMT) == S_IFDIR); | |
199607e4 | 2780 | #endif /* not WINDOWSNT */ |
3be3c08e | 2781 | #else /* not DOS_NT */ |
de0be7dd RS |
2782 | #ifdef HAVE_EUIDACCESS |
2783 | return (euidaccess (filename, 1) >= 0); | |
3beeedfe RS |
2784 | #else |
2785 | /* Access isn't quite right because it uses the real uid | |
2786 | and we really want to test with the effective uid. | |
2787 | But Unix doesn't give us a right way to do it. */ | |
2788 | return (access (filename, 1) >= 0); | |
2789 | #endif | |
3be3c08e | 2790 | #endif /* not DOS_NT */ |
3beeedfe RS |
2791 | } |
2792 | ||
2793 | /* Return nonzero if file FILENAME exists and can be written. */ | |
2794 | ||
2795 | static int | |
2796 | check_writable (filename) | |
2797 | char *filename; | |
2798 | { | |
3be3c08e RS |
2799 | #ifdef MSDOS |
2800 | struct stat st; | |
2801 | if (stat (filename, &st) < 0) | |
2802 | return 0; | |
2803 | return (st.st_mode & S_IWRITE || (st.st_mode & S_IFMT) == S_IFDIR); | |
2804 | #else /* not MSDOS */ | |
41f3fb38 KH |
2805 | #ifdef HAVE_EUIDACCESS |
2806 | return (euidaccess (filename, 2) >= 0); | |
3beeedfe RS |
2807 | #else |
2808 | /* Access isn't quite right because it uses the real uid | |
2809 | and we really want to test with the effective uid. | |
2810 | But Unix doesn't give us a right way to do it. | |
2811 | Opening with O_WRONLY could work for an ordinary file, | |
2812 | but would lose for directories. */ | |
2813 | return (access (filename, 2) >= 0); | |
2814 | #endif | |
3be3c08e | 2815 | #endif /* not MSDOS */ |
3beeedfe | 2816 | } |
570d7624 JB |
2817 | |
2818 | DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0, | |
2819 | "Return t if file FILENAME exists. (This does not mean you can read it.)\n\ | |
2820 | See also `file-readable-p' and `file-attributes'.") | |
2821 | (filename) | |
2822 | Lisp_Object filename; | |
2823 | { | |
199607e4 | 2824 | Lisp_Object absname; |
32f4334d | 2825 | Lisp_Object handler; |
4018b5ef | 2826 | struct stat statbuf; |
570d7624 JB |
2827 | |
2828 | CHECK_STRING (filename, 0); | |
199607e4 | 2829 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2830 | |
2831 | /* If the file name has special constructs in it, | |
2832 | call the corresponding file handler. */ | |
199607e4 | 2833 | handler = Ffind_file_name_handler (absname, Qfile_exists_p); |
32f4334d | 2834 | if (!NILP (handler)) |
199607e4 | 2835 | return call2 (handler, Qfile_exists_p, absname); |
32f4334d | 2836 | |
b1d1b865 RS |
2837 | absname = ENCODE_FILE (absname); |
2838 | ||
199607e4 | 2839 | return (stat (XSTRING (absname)->data, &statbuf) >= 0) ? Qt : Qnil; |
570d7624 JB |
2840 | } |
2841 | ||
2842 | DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0, | |
2843 | "Return t if FILENAME can be executed by you.\n\ | |
8b235fde | 2844 | For a directory, this means you can access files in that directory.") |
570d7624 JB |
2845 | (filename) |
2846 | Lisp_Object filename; | |
2847 | ||
2848 | { | |
199607e4 | 2849 | Lisp_Object absname; |
32f4334d | 2850 | Lisp_Object handler; |
570d7624 JB |
2851 | |
2852 | CHECK_STRING (filename, 0); | |
199607e4 | 2853 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2854 | |
2855 | /* If the file name has special constructs in it, | |
2856 | call the corresponding file handler. */ | |
199607e4 | 2857 | handler = Ffind_file_name_handler (absname, Qfile_executable_p); |
32f4334d | 2858 | if (!NILP (handler)) |
199607e4 | 2859 | return call2 (handler, Qfile_executable_p, absname); |
32f4334d | 2860 | |
b1d1b865 RS |
2861 | absname = ENCODE_FILE (absname); |
2862 | ||
199607e4 | 2863 | return (check_executable (XSTRING (absname)->data) ? Qt : Qnil); |
570d7624 JB |
2864 | } |
2865 | ||
2866 | DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0, | |
2867 | "Return t if file FILENAME exists and you can read it.\n\ | |
2868 | See also `file-exists-p' and `file-attributes'.") | |
2869 | (filename) | |
2870 | Lisp_Object filename; | |
2871 | { | |
199607e4 | 2872 | Lisp_Object absname; |
32f4334d | 2873 | Lisp_Object handler; |
4018b5ef | 2874 | int desc; |
bb369dc6 RS |
2875 | int flags; |
2876 | struct stat statbuf; | |
570d7624 JB |
2877 | |
2878 | CHECK_STRING (filename, 0); | |
199607e4 | 2879 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2880 | |
2881 | /* If the file name has special constructs in it, | |
2882 | call the corresponding file handler. */ | |
199607e4 | 2883 | handler = Ffind_file_name_handler (absname, Qfile_readable_p); |
32f4334d | 2884 | if (!NILP (handler)) |
199607e4 | 2885 | return call2 (handler, Qfile_readable_p, absname); |
32f4334d | 2886 | |
b1d1b865 RS |
2887 | absname = ENCODE_FILE (absname); |
2888 | ||
199607e4 RS |
2889 | #ifdef DOS_NT |
2890 | /* Under MS-DOS and Windows, open does not work for directories. */ | |
2891 | if (access (XSTRING (absname)->data, 0) == 0) | |
a8a7d065 RS |
2892 | return Qt; |
2893 | return Qnil; | |
199607e4 | 2894 | #else /* not DOS_NT */ |
bb369dc6 RS |
2895 | flags = O_RDONLY; |
2896 | #if defined (S_ISFIFO) && defined (O_NONBLOCK) | |
2897 | /* Opening a fifo without O_NONBLOCK can wait. | |
2898 | We don't want to wait. But we don't want to mess wth O_NONBLOCK | |
2899 | except in the case of a fifo, on a system which handles it. */ | |
2900 | desc = stat (XSTRING (absname)->data, &statbuf); | |
2901 | if (desc < 0) | |
2902 | return Qnil; | |
2903 | if (S_ISFIFO (statbuf.st_mode)) | |
2904 | flags |= O_NONBLOCK; | |
2905 | #endif | |
2906 | desc = open (XSTRING (absname)->data, flags); | |
4018b5ef RS |
2907 | if (desc < 0) |
2908 | return Qnil; | |
2909 | close (desc); | |
2910 | return Qt; | |
199607e4 | 2911 | #endif /* not DOS_NT */ |
570d7624 JB |
2912 | } |
2913 | ||
f793dc6c RS |
2914 | /* Having this before file-symlink-p mysteriously caused it to be forgotten |
2915 | on the RT/PC. */ | |
2916 | DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0, | |
2917 | "Return t if file FILENAME can be written or created by you.") | |
2918 | (filename) | |
2919 | Lisp_Object filename; | |
2920 | { | |
b1d1b865 | 2921 | Lisp_Object absname, dir, encoded; |
f793dc6c RS |
2922 | Lisp_Object handler; |
2923 | struct stat statbuf; | |
2924 | ||
2925 | CHECK_STRING (filename, 0); | |
199607e4 | 2926 | absname = Fexpand_file_name (filename, Qnil); |
f793dc6c RS |
2927 | |
2928 | /* If the file name has special constructs in it, | |
2929 | call the corresponding file handler. */ | |
199607e4 | 2930 | handler = Ffind_file_name_handler (absname, Qfile_writable_p); |
f793dc6c | 2931 | if (!NILP (handler)) |
199607e4 | 2932 | return call2 (handler, Qfile_writable_p, absname); |
f793dc6c | 2933 | |
b1d1b865 RS |
2934 | encoded = ENCODE_FILE (absname); |
2935 | if (stat (XSTRING (encoded)->data, &statbuf) >= 0) | |
2936 | return (check_writable (XSTRING (encoded)->data) | |
f793dc6c | 2937 | ? Qt : Qnil); |
b1d1b865 | 2938 | |
199607e4 | 2939 | dir = Ffile_name_directory (absname); |
f793dc6c RS |
2940 | #ifdef VMS |
2941 | if (!NILP (dir)) | |
2942 | dir = Fdirectory_file_name (dir); | |
2943 | #endif /* VMS */ | |
2944 | #ifdef MSDOS | |
2945 | if (!NILP (dir)) | |
2946 | dir = Fdirectory_file_name (dir); | |
2947 | #endif /* MSDOS */ | |
b1d1b865 RS |
2948 | |
2949 | dir = ENCODE_FILE (dir); | |
f793dc6c RS |
2950 | return (check_writable (!NILP (dir) ? (char *) XSTRING (dir)->data : "") |
2951 | ? Qt : Qnil); | |
2952 | } | |
2953 | \f | |
1f8653eb RS |
2954 | DEFUN ("access-file", Faccess_file, Saccess_file, 2, 2, 0, |
2955 | "Access file FILENAME, and get an error if that does not work.\n\ | |
2956 | The second argument STRING is used in the error message.\n\ | |
2957 | If there is no error, we return nil.") | |
2958 | (filename, string) | |
2959 | Lisp_Object filename, string; | |
2960 | { | |
b1d1b865 | 2961 | Lisp_Object handler, encoded_filename; |
1f8653eb RS |
2962 | int fd; |
2963 | ||
2964 | CHECK_STRING (filename, 0); | |
a79485af | 2965 | CHECK_STRING (string, 1); |
1f8653eb RS |
2966 | |
2967 | /* If the file name has special constructs in it, | |
2968 | call the corresponding file handler. */ | |
2969 | handler = Ffind_file_name_handler (filename, Qaccess_file); | |
2970 | if (!NILP (handler)) | |
2971 | return call3 (handler, Qaccess_file, filename, string); | |
2972 | ||
b1d1b865 RS |
2973 | encoded_filename = ENCODE_FILE (filename); |
2974 | ||
2975 | fd = open (XSTRING (encoded_filename)->data, O_RDONLY); | |
1f8653eb RS |
2976 | if (fd < 0) |
2977 | report_file_error (XSTRING (string)->data, Fcons (filename, Qnil)); | |
2978 | close (fd); | |
2979 | ||
2980 | return Qnil; | |
2981 | } | |
2982 | \f | |
570d7624 | 2983 | DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0, |
89de89c7 RS |
2984 | "Return non-nil if file FILENAME is the name of a symbolic link.\n\ |
2985 | The value is the name of the file to which it is linked.\n\ | |
2986 | Otherwise returns nil.") | |
570d7624 JB |
2987 | (filename) |
2988 | Lisp_Object filename; | |
2989 | { | |
2990 | #ifdef S_IFLNK | |
2991 | char *buf; | |
2992 | int bufsize; | |
2993 | int valsize; | |
2994 | Lisp_Object val; | |
32f4334d | 2995 | Lisp_Object handler; |
570d7624 JB |
2996 | |
2997 | CHECK_STRING (filename, 0); | |
2998 | filename = Fexpand_file_name (filename, Qnil); | |
2999 | ||
32f4334d RS |
3000 | /* If the file name has special constructs in it, |
3001 | call the corresponding file handler. */ | |
49307295 | 3002 | handler = Ffind_file_name_handler (filename, Qfile_symlink_p); |
32f4334d RS |
3003 | if (!NILP (handler)) |
3004 | return call2 (handler, Qfile_symlink_p, filename); | |
3005 | ||
b1d1b865 RS |
3006 | filename = ENCODE_FILE (filename); |
3007 | ||
570d7624 JB |
3008 | bufsize = 100; |
3009 | while (1) | |
3010 | { | |
3011 | buf = (char *) xmalloc (bufsize); | |
3012 | bzero (buf, bufsize); | |
3013 | valsize = readlink (XSTRING (filename)->data, buf, bufsize); | |
3014 | if (valsize < bufsize) break; | |
3015 | /* Buffer was not long enough */ | |
9ac0d9e0 | 3016 | xfree (buf); |
570d7624 JB |
3017 | bufsize *= 2; |
3018 | } | |
3019 | if (valsize == -1) | |
3020 | { | |
9ac0d9e0 | 3021 | xfree (buf); |
570d7624 JB |
3022 | return Qnil; |
3023 | } | |
3024 | val = make_string (buf, valsize); | |
9ac0d9e0 | 3025 | xfree (buf); |
cd913586 KH |
3026 | val = DECODE_FILE (val); |
3027 | return val; | |
570d7624 JB |
3028 | #else /* not S_IFLNK */ |
3029 | return Qnil; | |
3030 | #endif /* not S_IFLNK */ | |
3031 | } | |
3032 | ||
570d7624 | 3033 | DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0, |
3be6c6a0 DL |
3034 | "Return t if FILENAME names an existing directory.\n\ |
3035 | Symbolic links to directories count as directories.\n\ | |
3036 | See `file-symlink-p' to distinguish symlinks.") | |
570d7624 JB |
3037 | (filename) |
3038 | Lisp_Object filename; | |
3039 | { | |
199607e4 | 3040 | register Lisp_Object absname; |
570d7624 | 3041 | struct stat st; |
32f4334d | 3042 | Lisp_Object handler; |
570d7624 | 3043 | |
199607e4 | 3044 | absname = expand_and_dir_to_file (filename, current_buffer->directory); |
570d7624 | 3045 | |
32f4334d RS |
3046 | /* If the file name has special constructs in it, |
3047 | call the corresponding file handler. */ | |
199607e4 | 3048 | handler = Ffind_file_name_handler (absname, Qfile_directory_p); |
32f4334d | 3049 | if (!NILP (handler)) |
199607e4 | 3050 | return call2 (handler, Qfile_directory_p, absname); |
32f4334d | 3051 | |
b1d1b865 RS |
3052 | absname = ENCODE_FILE (absname); |
3053 | ||
199607e4 | 3054 | if (stat (XSTRING (absname)->data, &st) < 0) |
570d7624 JB |
3055 | return Qnil; |
3056 | return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil; | |
3057 | } | |
3058 | ||
b72dea2a JB |
3059 | DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, Sfile_accessible_directory_p, 1, 1, 0, |
3060 | "Return t if file FILENAME is the name of a directory as a file,\n\ | |
3061 | and files in that directory can be opened by you. In order to use a\n\ | |
3062 | directory as a buffer's current directory, this predicate must return true.\n\ | |
3063 | A directory name spec may be given instead; then the value is t\n\ | |
3064 | if the directory so specified exists and really is a readable and\n\ | |
3065 | searchable directory.") | |
3066 | (filename) | |
3067 | Lisp_Object filename; | |
3068 | { | |
32f4334d | 3069 | Lisp_Object handler; |
1a04498e | 3070 | int tem; |
d26859eb | 3071 | struct gcpro gcpro1; |
32f4334d RS |
3072 | |
3073 | /* If the file name has special constructs in it, | |
3074 | call the corresponding file handler. */ | |
49307295 | 3075 | handler = Ffind_file_name_handler (filename, Qfile_accessible_directory_p); |
32f4334d RS |
3076 | if (!NILP (handler)) |
3077 | return call2 (handler, Qfile_accessible_directory_p, filename); | |
3078 | ||
d26859eb KH |
3079 | /* It's an unlikely combination, but yes we really do need to gcpro: |
3080 | Suppose that file-accessible-directory-p has no handler, but | |
3081 | file-directory-p does have a handler; this handler causes a GC which | |
3082 | relocates the string in `filename'; and finally file-directory-p | |
3083 | returns non-nil. Then we would end up passing a garbaged string | |
3084 | to file-executable-p. */ | |
3085 | GCPRO1 (filename); | |
1a04498e KH |
3086 | tem = (NILP (Ffile_directory_p (filename)) |
3087 | || NILP (Ffile_executable_p (filename))); | |
d26859eb | 3088 | UNGCPRO; |
1a04498e | 3089 | return tem ? Qnil : Qt; |
b72dea2a JB |
3090 | } |
3091 | ||
f793dc6c RS |
3092 | DEFUN ("file-regular-p", Ffile_regular_p, Sfile_regular_p, 1, 1, 0, |
3093 | "Return t if file FILENAME is the name of a regular file.\n\ | |
3094 | This is the sort of file that holds an ordinary stream of data bytes.") | |
3095 | (filename) | |
3096 | Lisp_Object filename; | |
3097 | { | |
199607e4 | 3098 | register Lisp_Object absname; |
f793dc6c RS |
3099 | struct stat st; |
3100 | Lisp_Object handler; | |
3101 | ||
199607e4 | 3102 | absname = expand_and_dir_to_file (filename, current_buffer->directory); |
f793dc6c RS |
3103 | |
3104 | /* If the file name has special constructs in it, | |
3105 | call the corresponding file handler. */ | |
199607e4 | 3106 | handler = Ffind_file_name_handler (absname, Qfile_regular_p); |
f793dc6c | 3107 | if (!NILP (handler)) |
199607e4 | 3108 | return call2 (handler, Qfile_regular_p, absname); |
f793dc6c | 3109 | |
b1d1b865 RS |
3110 | absname = ENCODE_FILE (absname); |
3111 | ||
c1c4693e RS |
3112 | #ifdef WINDOWSNT |
3113 | { | |
3114 | int result; | |
3115 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
3116 | ||
3117 | /* Tell stat to use expensive method to get accurate info. */ | |
3118 | Vw32_get_true_file_attributes = Qt; | |
3119 | result = stat (XSTRING (absname)->data, &st); | |
3120 | Vw32_get_true_file_attributes = tem; | |
3121 | ||
3122 | if (result < 0) | |
3123 | return Qnil; | |
3124 | return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil; | |
3125 | } | |
3126 | #else | |
4cd8344a | 3127 | if (stat (XSTRING (absname)->data, &st) < 0) |
f793dc6c RS |
3128 | return Qnil; |
3129 | return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil; | |
c1c4693e | 3130 | #endif |
f793dc6c RS |
3131 | } |
3132 | \f | |
570d7624 | 3133 | DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0, |
3b7f6e60 | 3134 | "Return mode bits of file named FILENAME, as an integer.") |
570d7624 JB |
3135 | (filename) |
3136 | Lisp_Object filename; | |
3137 | { | |
199607e4 | 3138 | Lisp_Object absname; |
570d7624 | 3139 | struct stat st; |
32f4334d | 3140 | Lisp_Object handler; |
570d7624 | 3141 | |
199607e4 | 3142 | absname = expand_and_dir_to_file (filename, current_buffer->directory); |
570d7624 | 3143 | |
32f4334d RS |
3144 | /* If the file name has special constructs in it, |
3145 | call the corresponding file handler. */ | |
199607e4 | 3146 | handler = Ffind_file_name_handler (absname, Qfile_modes); |
32f4334d | 3147 | if (!NILP (handler)) |
199607e4 | 3148 | return call2 (handler, Qfile_modes, absname); |
32f4334d | 3149 | |
b1d1b865 RS |
3150 | absname = ENCODE_FILE (absname); |
3151 | ||
199607e4 | 3152 | if (stat (XSTRING (absname)->data, &st) < 0) |
570d7624 | 3153 | return Qnil; |
34ead71a | 3154 | #if defined (MSDOS) && __DJGPP__ < 2 |
199607e4 | 3155 | if (check_executable (XSTRING (absname)->data)) |
3be3c08e | 3156 | st.st_mode |= S_IEXEC; |
34ead71a | 3157 | #endif /* MSDOS && __DJGPP__ < 2 */ |
3ace87e3 | 3158 | |
570d7624 JB |
3159 | return make_number (st.st_mode & 07777); |
3160 | } | |
3161 | ||
3162 | DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0, | |
3b7f6e60 | 3163 | "Set mode bits of file named FILENAME to MODE (an integer).\n\ |
570d7624 JB |
3164 | Only the 12 low bits of MODE are used.") |
3165 | (filename, mode) | |
3166 | Lisp_Object filename, mode; | |
3167 | { | |
b1d1b865 | 3168 | Lisp_Object absname, encoded_absname; |
32f4334d | 3169 | Lisp_Object handler; |
570d7624 | 3170 | |
199607e4 | 3171 | absname = Fexpand_file_name (filename, current_buffer->directory); |
570d7624 JB |
3172 | CHECK_NUMBER (mode, 1); |
3173 | ||
32f4334d RS |
3174 | /* If the file name has special constructs in it, |
3175 | call the corresponding file handler. */ | |
199607e4 | 3176 | handler = Ffind_file_name_handler (absname, Qset_file_modes); |
32f4334d | 3177 | if (!NILP (handler)) |
199607e4 | 3178 | return call3 (handler, Qset_file_modes, absname, mode); |
32f4334d | 3179 | |
b1d1b865 RS |
3180 | encoded_absname = ENCODE_FILE (absname); |
3181 | ||
3182 | if (chmod (XSTRING (encoded_absname)->data, XINT (mode)) < 0) | |
199607e4 | 3183 | report_file_error ("Doing chmod", Fcons (absname, Qnil)); |
570d7624 JB |
3184 | |
3185 | return Qnil; | |
3186 | } | |
3187 | ||
c24e9a53 | 3188 | DEFUN ("set-default-file-modes", Fset_default_file_modes, Sset_default_file_modes, 1, 1, 0, |
5f85ea58 RS |
3189 | "Set the file permission bits for newly created files.\n\ |
3190 | The argument MODE should be an integer; only the low 9 bits are used.\n\ | |
36a8c287 | 3191 | This setting is inherited by subprocesses.") |
5f85ea58 RS |
3192 | (mode) |
3193 | Lisp_Object mode; | |
36a8c287 | 3194 | { |
5f85ea58 | 3195 | CHECK_NUMBER (mode, 0); |
199607e4 | 3196 | |
5f85ea58 | 3197 | umask ((~ XINT (mode)) & 0777); |
36a8c287 JB |
3198 | |
3199 | return Qnil; | |
3200 | } | |
3201 | ||
c24e9a53 | 3202 | DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0, |
5f85ea58 RS |
3203 | "Return the default file protection for created files.\n\ |
3204 | The value is an integer.") | |
36a8c287 JB |
3205 | () |
3206 | { | |
5f85ea58 RS |
3207 | int realmask; |
3208 | Lisp_Object value; | |
36a8c287 | 3209 | |
5f85ea58 RS |
3210 | realmask = umask (0); |
3211 | umask (realmask); | |
36a8c287 | 3212 | |
46283abe | 3213 | XSETINT (value, (~ realmask) & 0777); |
5f85ea58 | 3214 | return value; |
36a8c287 | 3215 | } |
f793dc6c | 3216 | \f |
85ffea93 RS |
3217 | #ifdef unix |
3218 | ||
3219 | DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "", | |
3220 | "Tell Unix to finish all pending disk updates.") | |
3221 | () | |
3222 | { | |
3223 | sync (); | |
3224 | return Qnil; | |
3225 | } | |
3226 | ||
3227 | #endif /* unix */ | |
3228 | ||
570d7624 JB |
3229 | DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0, |
3230 | "Return t if file FILE1 is newer than file FILE2.\n\ | |
3231 | If FILE1 does not exist, the answer is nil;\n\ | |
3232 | otherwise, if FILE2 does not exist, the answer is t.") | |
3233 | (file1, file2) | |
3234 | Lisp_Object file1, file2; | |
3235 | { | |
199607e4 | 3236 | Lisp_Object absname1, absname2; |
570d7624 JB |
3237 | struct stat st; |
3238 | int mtime1; | |
32f4334d | 3239 | Lisp_Object handler; |
09121adc | 3240 | struct gcpro gcpro1, gcpro2; |
570d7624 JB |
3241 | |
3242 | CHECK_STRING (file1, 0); | |
3243 | CHECK_STRING (file2, 0); | |
3244 | ||
199607e4 RS |
3245 | absname1 = Qnil; |
3246 | GCPRO2 (absname1, file2); | |
3247 | absname1 = expand_and_dir_to_file (file1, current_buffer->directory); | |
3248 | absname2 = expand_and_dir_to_file (file2, current_buffer->directory); | |
09121adc | 3249 | UNGCPRO; |
570d7624 | 3250 | |
32f4334d RS |
3251 | /* If the file name has special constructs in it, |
3252 | call the corresponding file handler. */ | |
199607e4 | 3253 | handler = Ffind_file_name_handler (absname1, Qfile_newer_than_file_p); |
51cf6d37 | 3254 | if (NILP (handler)) |
199607e4 | 3255 | handler = Ffind_file_name_handler (absname2, Qfile_newer_than_file_p); |
32f4334d | 3256 | if (!NILP (handler)) |
199607e4 | 3257 | return call3 (handler, Qfile_newer_than_file_p, absname1, absname2); |
32f4334d | 3258 | |
b1d1b865 RS |
3259 | GCPRO2 (absname1, absname2); |
3260 | absname1 = ENCODE_FILE (absname1); | |
3261 | absname2 = ENCODE_FILE (absname2); | |
3262 | UNGCPRO; | |
3263 | ||
199607e4 | 3264 | if (stat (XSTRING (absname1)->data, &st) < 0) |
570d7624 JB |
3265 | return Qnil; |
3266 | ||
3267 | mtime1 = st.st_mtime; | |
3268 | ||
199607e4 | 3269 | if (stat (XSTRING (absname2)->data, &st) < 0) |
570d7624 JB |
3270 | return Qt; |
3271 | ||
3272 | return (mtime1 > st.st_mtime) ? Qt : Qnil; | |
3273 | } | |
3274 | \f | |
5e570b75 | 3275 | #ifdef DOS_NT |
4c3c22f3 | 3276 | Lisp_Object Qfind_buffer_file_type; |
5e570b75 | 3277 | #endif /* DOS_NT */ |
4c3c22f3 | 3278 | |
6fdaa9a0 KH |
3279 | #ifndef READ_BUF_SIZE |
3280 | #define READ_BUF_SIZE (64 << 10) | |
3281 | #endif | |
3282 | ||
f736ffbf KH |
3283 | /* This function is called when a function bound to |
3284 | Vset_auto_coding_function causes some error. At that time, a text | |
3285 | of a file has already been inserted in the current buffer, but, | |
3286 | markers has not yet been adjusted. Thus we must adjust markers | |
3287 | here. We are sure that the buffer was empty before the text of the | |
3288 | file was inserted. */ | |
3289 | ||
3290 | static Lisp_Object | |
3291 | set_auto_coding_unwind (multibyte) | |
3292 | Lisp_Object multibyte; | |
3293 | { | |
3294 | int inserted = Z_BYTE - BEG_BYTE; | |
3295 | ||
3296 | if (!NILP (multibyte)) | |
3297 | inserted = multibyte_chars_in_text (GPT_ADDR - inserted, inserted); | |
3298 | adjust_after_insert (PT, PT_BYTE, Z, Z_BYTE, inserted); | |
3299 | ||
3300 | return Qnil; | |
3301 | } | |
3302 | ||
570d7624 | 3303 | DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents, |
3d0387c0 | 3304 | 1, 5, 0, |
570d7624 | 3305 | "Insert contents of file FILENAME after point.\n\ |
ec7adf26 | 3306 | Returns list of absolute file name and number of bytes inserted.\n\ |
570d7624 JB |
3307 | If second argument VISIT is non-nil, the buffer's visited filename\n\ |
3308 | and last save file modtime are set, and it is marked unmodified.\n\ | |
3309 | If visiting and the file does not exist, visiting is completed\n\ | |
6fdaa9a0 | 3310 | before the error is signaled.\n\ |
7fded690 JB |
3311 | The optional third and fourth arguments BEG and END\n\ |
3312 | specify what portion of the file to insert.\n\ | |
ec7adf26 | 3313 | These arguments count bytes in the file, not characters in the buffer.\n\ |
3d0387c0 | 3314 | If VISIT is non-nil, BEG and END must be nil.\n\ |
94bec52a | 3315 | \n\ |
3d0387c0 RS |
3316 | If optional fifth argument REPLACE is non-nil,\n\ |
3317 | it means replace the current buffer contents (in the accessible portion)\n\ | |
3318 | with the file contents. This is better than simply deleting and inserting\n\ | |
3319 | the whole thing because (1) it preserves some marker positions\n\ | |
94bec52a RS |
3320 | and (2) it puts less data in the undo list.\n\ |
3321 | When REPLACE is non-nil, the value is the number of characters actually read,\n\ | |
6fdaa9a0 | 3322 | which is often less than the number of characters to be read.\n\ |
6cf71bf1 | 3323 | \n\ |
6fdaa9a0 | 3324 | This does code conversion according to the value of\n\ |
6cf71bf1 KH |
3325 | `coding-system-for-read' or `file-coding-system-alist',\n\ |
3326 | and sets the variable `last-coding-system-used' to the coding system\n\ | |
3327 | actually used.") | |
3d0387c0 RS |
3328 | (filename, visit, beg, end, replace) |
3329 | Lisp_Object filename, visit, beg, end, replace; | |
570d7624 JB |
3330 | { |
3331 | struct stat st; | |
3332 | register int fd; | |
ec7adf26 | 3333 | int inserted = 0; |
570d7624 | 3334 | register int how_much; |
6fdaa9a0 | 3335 | register int unprocessed; |
570d7624 | 3336 | int count = specpdl_ptr - specpdl; |
b1d1b865 RS |
3337 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
3338 | Lisp_Object handler, val, insval, orig_filename; | |
d6a3cc15 | 3339 | Lisp_Object p; |
7fded690 | 3340 | int total; |
53c34c46 | 3341 | int not_regular = 0; |
feb9dc27 | 3342 | unsigned char read_buf[READ_BUF_SIZE]; |
6fdaa9a0 | 3343 | struct coding_system coding; |
3dbcf3f6 | 3344 | unsigned char buffer[1 << 14]; |
727a0b4a | 3345 | int replace_handled = 0; |
ec7adf26 | 3346 | int set_coding_system = 0; |
f736ffbf | 3347 | int coding_system_decided = 0; |
32f4334d | 3348 | |
95385625 RS |
3349 | if (current_buffer->base_buffer && ! NILP (visit)) |
3350 | error ("Cannot do file visiting in an indirect buffer"); | |
3351 | ||
3352 | if (!NILP (current_buffer->read_only)) | |
3353 | Fbarf_if_buffer_read_only (); | |
3354 | ||
32f4334d | 3355 | val = Qnil; |
d6a3cc15 | 3356 | p = Qnil; |
b1d1b865 | 3357 | orig_filename = Qnil; |
32f4334d | 3358 | |
b1d1b865 | 3359 | GCPRO4 (filename, val, p, orig_filename); |
570d7624 JB |
3360 | |
3361 | CHECK_STRING (filename, 0); | |
3362 | filename = Fexpand_file_name (filename, Qnil); | |
3363 | ||
32f4334d RS |
3364 | /* If the file name has special constructs in it, |
3365 | call the corresponding file handler. */ | |
49307295 | 3366 | handler = Ffind_file_name_handler (filename, Qinsert_file_contents); |
32f4334d RS |
3367 | if (!NILP (handler)) |
3368 | { | |
3d0387c0 RS |
3369 | val = call6 (handler, Qinsert_file_contents, filename, |
3370 | visit, beg, end, replace); | |
b69a8fdc | 3371 | if (CONSP (val) && CONSP (XCONS (val)->cdr)) |
26a04715 | 3372 | inserted = XINT (XCONS (XCONS (val)->cdr)->car); |
32f4334d RS |
3373 | goto handled; |
3374 | } | |
3375 | ||
b1d1b865 RS |
3376 | orig_filename = filename; |
3377 | filename = ENCODE_FILE (filename); | |
3378 | ||
570d7624 JB |
3379 | fd = -1; |
3380 | ||
c1c4693e RS |
3381 | #ifdef WINDOWSNT |
3382 | { | |
3383 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
3384 | ||
3385 | /* Tell stat to use expensive method to get accurate info. */ | |
3386 | Vw32_get_true_file_attributes = Qt; | |
3387 | total = stat (XSTRING (filename)->data, &st); | |
3388 | Vw32_get_true_file_attributes = tem; | |
3389 | } | |
3390 | if (total < 0) | |
3391 | #else | |
570d7624 | 3392 | #ifndef APOLLO |
99bc28f4 | 3393 | if (stat (XSTRING (filename)->data, &st) < 0) |
570d7624 | 3394 | #else |
4018b5ef | 3395 | if ((fd = open (XSTRING (filename)->data, O_RDONLY)) < 0 |
570d7624 JB |
3396 | || fstat (fd, &st) < 0) |
3397 | #endif /* not APOLLO */ | |
c1c4693e | 3398 | #endif /* WINDOWSNT */ |
570d7624 JB |
3399 | { |
3400 | if (fd >= 0) close (fd); | |
99bc28f4 | 3401 | badopen: |
265a9e55 | 3402 | if (NILP (visit)) |
b1d1b865 | 3403 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
570d7624 JB |
3404 | st.st_mtime = -1; |
3405 | how_much = 0; | |
0de6b8f4 RS |
3406 | if (!NILP (Vcoding_system_for_read)) |
3407 | current_buffer->buffer_file_coding_system = Vcoding_system_for_read; | |
570d7624 JB |
3408 | goto notfound; |
3409 | } | |
3410 | ||
99bc28f4 | 3411 | #ifdef S_IFREG |
be53b411 JB |
3412 | /* This code will need to be changed in order to work on named |
3413 | pipes, and it's probably just not worth it. So we should at | |
3414 | least signal an error. */ | |
99bc28f4 | 3415 | if (!S_ISREG (st.st_mode)) |
330bfe57 | 3416 | { |
d4b8687b RS |
3417 | not_regular = 1; |
3418 | ||
3419 | if (! NILP (visit)) | |
3420 | goto notfound; | |
3421 | ||
3422 | if (! NILP (replace) || ! NILP (beg) || ! NILP (end)) | |
330bfe57 RS |
3423 | Fsignal (Qfile_error, |
3424 | Fcons (build_string ("not a regular file"), | |
b1d1b865 | 3425 | Fcons (orig_filename, Qnil))); |
330bfe57 | 3426 | } |
be53b411 JB |
3427 | #endif |
3428 | ||
99bc28f4 | 3429 | if (fd < 0) |
4018b5ef | 3430 | if ((fd = open (XSTRING (filename)->data, O_RDONLY)) < 0) |
99bc28f4 KH |
3431 | goto badopen; |
3432 | ||
3433 | /* Replacement should preserve point as it preserves markers. */ | |
3434 | if (!NILP (replace)) | |
3435 | record_unwind_protect (restore_point_unwind, Fpoint_marker ()); | |
3436 | ||
3437 | record_unwind_protect (close_file_unwind, make_number (fd)); | |
3438 | ||
570d7624 | 3439 | /* Supposedly happens on VMS. */ |
d4b8687b | 3440 | if (! not_regular && st.st_size < 0) |
570d7624 | 3441 | error ("File size is negative"); |
be53b411 | 3442 | |
9c856db9 GM |
3443 | /* Prevent redisplay optimizations. */ |
3444 | current_buffer->clip_changed = 1; | |
3445 | ||
7fded690 JB |
3446 | if (!NILP (beg) || !NILP (end)) |
3447 | if (!NILP (visit)) | |
3448 | error ("Attempt to visit less than an entire file"); | |
3449 | ||
3450 | if (!NILP (beg)) | |
3451 | CHECK_NUMBER (beg, 0); | |
3452 | else | |
2acfd7ae | 3453 | XSETFASTINT (beg, 0); |
7fded690 JB |
3454 | |
3455 | if (!NILP (end)) | |
3456 | CHECK_NUMBER (end, 0); | |
3457 | else | |
3458 | { | |
d4b8687b RS |
3459 | if (! not_regular) |
3460 | { | |
3461 | XSETINT (end, st.st_size); | |
3462 | if (XINT (end) != st.st_size) | |
3463 | error ("Maximum buffer size exceeded"); | |
3464 | } | |
7fded690 JB |
3465 | } |
3466 | ||
f736ffbf KH |
3467 | if (BEG < Z) |
3468 | { | |
3469 | /* Decide the coding system to use for reading the file now | |
3470 | because we can't use an optimized method for handling | |
3471 | `coding:' tag if the current buffer is not empty. */ | |
3472 | Lisp_Object val; | |
3473 | val = Qnil; | |
feb9dc27 | 3474 | |
f736ffbf KH |
3475 | if (!NILP (Vcoding_system_for_read)) |
3476 | val = Vcoding_system_for_read; | |
3477 | else if (! NILP (replace)) | |
3478 | /* In REPLACE mode, we can use the same coding system | |
3479 | that was used to visit the file. */ | |
3480 | val = current_buffer->buffer_file_coding_system; | |
3481 | else | |
3482 | { | |
3483 | /* Don't try looking inside a file for a coding system | |
3484 | specification if it is not seekable. */ | |
3485 | if (! not_regular && ! NILP (Vset_auto_coding_function)) | |
3486 | { | |
3487 | /* Find a coding system specified in the heading two | |
3488 | lines or in the tailing several lines of the file. | |
3489 | We assume that the 1K-byte and 3K-byte for heading | |
003a7eaa | 3490 | and tailing respectively are sufficient for this |
f736ffbf KH |
3491 | purpose. */ |
3492 | int how_many, nread; | |
3493 | ||
3494 | if (st.st_size <= (1024 * 4)) | |
3495 | nread = read (fd, read_buf, 1024 * 4); | |
3496 | else | |
3497 | { | |
3498 | nread = read (fd, read_buf, 1024); | |
3499 | if (nread >= 0) | |
3500 | { | |
3501 | if (lseek (fd, st.st_size - (1024 * 3), 0) < 0) | |
3502 | report_file_error ("Setting file position", | |
3503 | Fcons (orig_filename, Qnil)); | |
3504 | nread += read (fd, read_buf + nread, 1024 * 3); | |
3505 | } | |
3506 | } | |
feb9dc27 | 3507 | |
f736ffbf KH |
3508 | if (nread < 0) |
3509 | error ("IO error reading %s: %s", | |
3510 | XSTRING (orig_filename)->data, strerror (errno)); | |
3511 | else if (nread > 0) | |
3512 | { | |
3513 | int count = specpdl_ptr - specpdl; | |
3514 | struct buffer *prev = current_buffer; | |
3515 | ||
3516 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
3517 | temp_output_buffer_setup (" *code-converting-work*"); | |
3518 | set_buffer_internal (XBUFFER (Vstandard_output)); | |
3519 | current_buffer->enable_multibyte_characters = Qnil; | |
3520 | insert_1_both (read_buf, nread, nread, 0, 0, 0); | |
3521 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); | |
1255deb9 KH |
3522 | val = call2 (Vset_auto_coding_function, |
3523 | filename, make_number (nread)); | |
f736ffbf KH |
3524 | set_buffer_internal (prev); |
3525 | /* Discard the unwind protect for recovering the | |
3526 | current buffer. */ | |
3527 | specpdl_ptr--; | |
3528 | ||
3529 | /* Rewind the file for the actual read done later. */ | |
3530 | if (lseek (fd, 0, 0) < 0) | |
3531 | report_file_error ("Setting file position", | |
3532 | Fcons (orig_filename, Qnil)); | |
3533 | } | |
3534 | } | |
feb9dc27 | 3535 | |
f736ffbf KH |
3536 | if (NILP (val)) |
3537 | { | |
3538 | /* If we have not yet decided a coding system, check | |
3539 | file-coding-system-alist. */ | |
3540 | Lisp_Object args[6], coding_systems; | |
3541 | ||
3542 | args[0] = Qinsert_file_contents, args[1] = orig_filename; | |
3543 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace; | |
3544 | coding_systems = Ffind_operation_coding_system (6, args); | |
3545 | if (CONSP (coding_systems)) | |
3546 | val = XCONS (coding_systems)->car; | |
3547 | } | |
3548 | } | |
c9e82392 | 3549 | |
f736ffbf | 3550 | setup_coding_system (Fcheck_coding_system (val), &coding); |
c8a6d68a | 3551 | |
237a6fd2 RS |
3552 | if (NILP (current_buffer->enable_multibyte_characters) |
3553 | && ! NILP (val)) | |
3554 | /* We must suppress all character code conversion except for | |
3555 | end-of-line conversion. */ | |
57515cfe | 3556 | setup_raw_text_coding_system (&coding); |
54369368 | 3557 | |
f736ffbf KH |
3558 | coding_system_decided = 1; |
3559 | } | |
6cf71bf1 | 3560 | |
f736ffbf KH |
3561 | /* Ensure we always set Vlast_coding_system_used. */ |
3562 | set_coding_system = 1; | |
c9e82392 | 3563 | |
3d0387c0 RS |
3564 | /* If requested, replace the accessible part of the buffer |
3565 | with the file contents. Avoid replacing text at the | |
3566 | beginning or end of the buffer that matches the file contents; | |
3dbcf3f6 RS |
3567 | that preserves markers pointing to the unchanged parts. |
3568 | ||
3569 | Here we implement this feature in an optimized way | |
3570 | for the case where code conversion is NOT needed. | |
3571 | The following if-statement handles the case of conversion | |
727a0b4a RS |
3572 | in a less optimal way. |
3573 | ||
3574 | If the code conversion is "automatic" then we try using this | |
3575 | method and hope for the best. | |
3576 | But if we discover the need for conversion, we give up on this method | |
3577 | and let the following if-statement handle the replace job. */ | |
3dbcf3f6 | 3578 | if (!NILP (replace) |
f736ffbf | 3579 | && BEGV < ZV |
70697733 RS |
3580 | && ! CODING_REQUIRE_DECODING (&coding) |
3581 | && (coding.eol_type == CODING_EOL_UNDECIDED | |
3582 | || coding.eol_type == CODING_EOL_LF)) | |
3d0387c0 | 3583 | { |
ec7adf26 RS |
3584 | /* same_at_start and same_at_end count bytes, |
3585 | because file access counts bytes | |
3586 | and BEG and END count bytes. */ | |
3587 | int same_at_start = BEGV_BYTE; | |
3588 | int same_at_end = ZV_BYTE; | |
9c28748f | 3589 | int overlap; |
6fdaa9a0 KH |
3590 | /* There is still a possibility we will find the need to do code |
3591 | conversion. If that happens, we set this variable to 1 to | |
727a0b4a | 3592 | give up on handling REPLACE in the optimized way. */ |
6fdaa9a0 | 3593 | int giveup_match_end = 0; |
9c28748f | 3594 | |
4d2a0879 RS |
3595 | if (XINT (beg) != 0) |
3596 | { | |
3597 | if (lseek (fd, XINT (beg), 0) < 0) | |
3598 | report_file_error ("Setting file position", | |
b1d1b865 | 3599 | Fcons (orig_filename, Qnil)); |
4d2a0879 RS |
3600 | } |
3601 | ||
3d0387c0 RS |
3602 | immediate_quit = 1; |
3603 | QUIT; | |
3604 | /* Count how many chars at the start of the file | |
3605 | match the text at the beginning of the buffer. */ | |
3606 | while (1) | |
3607 | { | |
3608 | int nread, bufpos; | |
3609 | ||
3610 | nread = read (fd, buffer, sizeof buffer); | |
3611 | if (nread < 0) | |
3612 | error ("IO error reading %s: %s", | |
b1d1b865 | 3613 | XSTRING (orig_filename)->data, strerror (errno)); |
3d0387c0 RS |
3614 | else if (nread == 0) |
3615 | break; | |
6fdaa9a0 | 3616 | |
0ef69138 | 3617 | if (coding.type == coding_type_undecided) |
727a0b4a | 3618 | detect_coding (&coding, buffer, nread); |
6ad0beeb | 3619 | if (CODING_REQUIRE_DECODING (&coding)) |
727a0b4a RS |
3620 | /* We found that the file should be decoded somehow. |
3621 | Let's give up here. */ | |
3622 | { | |
3623 | giveup_match_end = 1; | |
3624 | break; | |
3625 | } | |
3626 | ||
0ef69138 | 3627 | if (coding.eol_type == CODING_EOL_UNDECIDED) |
727a0b4a | 3628 | detect_eol (&coding, buffer, nread); |
1b335d29 | 3629 | if (coding.eol_type != CODING_EOL_UNDECIDED |
70ec4328 | 3630 | && coding.eol_type != CODING_EOL_LF) |
727a0b4a RS |
3631 | /* We found that the format of eol should be decoded. |
3632 | Let's give up here. */ | |
3633 | { | |
3634 | giveup_match_end = 1; | |
3635 | break; | |
3636 | } | |
3637 | ||
3d0387c0 | 3638 | bufpos = 0; |
ec7adf26 | 3639 | while (bufpos < nread && same_at_start < ZV_BYTE |
6fdaa9a0 | 3640 | && FETCH_BYTE (same_at_start) == buffer[bufpos]) |
3d0387c0 RS |
3641 | same_at_start++, bufpos++; |
3642 | /* If we found a discrepancy, stop the scan. | |
8e6208c5 | 3643 | Otherwise loop around and scan the next bufferful. */ |
3d0387c0 RS |
3644 | if (bufpos != nread) |
3645 | break; | |
3646 | } | |
3647 | immediate_quit = 0; | |
3648 | /* If the file matches the buffer completely, | |
3649 | there's no need to replace anything. */ | |
ec7adf26 | 3650 | if (same_at_start - BEGV_BYTE == XINT (end)) |
3d0387c0 RS |
3651 | { |
3652 | close (fd); | |
a1d2b64a | 3653 | specpdl_ptr--; |
1051b3b3 RS |
3654 | /* Truncate the buffer to the size of the file. */ |
3655 | del_range_1 (same_at_start, same_at_end, 0); | |
3d0387c0 RS |
3656 | goto handled; |
3657 | } | |
3658 | immediate_quit = 1; | |
3659 | QUIT; | |
3660 | /* Count how many chars at the end of the file | |
6fdaa9a0 KH |
3661 | match the text at the end of the buffer. But, if we have |
3662 | already found that decoding is necessary, don't waste time. */ | |
3663 | while (!giveup_match_end) | |
3d0387c0 RS |
3664 | { |
3665 | int total_read, nread, bufpos, curpos, trial; | |
3666 | ||
3667 | /* At what file position are we now scanning? */ | |
ec7adf26 | 3668 | curpos = XINT (end) - (ZV_BYTE - same_at_end); |
fc81fa9e KH |
3669 | /* If the entire file matches the buffer tail, stop the scan. */ |
3670 | if (curpos == 0) | |
3671 | break; | |
3d0387c0 RS |
3672 | /* How much can we scan in the next step? */ |
3673 | trial = min (curpos, sizeof buffer); | |
3674 | if (lseek (fd, curpos - trial, 0) < 0) | |
3675 | report_file_error ("Setting file position", | |
b1d1b865 | 3676 | Fcons (orig_filename, Qnil)); |
3d0387c0 RS |
3677 | |
3678 | total_read = 0; | |
3679 | while (total_read < trial) | |
3680 | { | |
3681 | nread = read (fd, buffer + total_read, trial - total_read); | |
3682 | if (nread <= 0) | |
3683 | error ("IO error reading %s: %s", | |
b1d1b865 | 3684 | XSTRING (orig_filename)->data, strerror (errno)); |
3d0387c0 RS |
3685 | total_read += nread; |
3686 | } | |
8e6208c5 | 3687 | /* Scan this bufferful from the end, comparing with |
3d0387c0 RS |
3688 | the Emacs buffer. */ |
3689 | bufpos = total_read; | |
3690 | /* Compare with same_at_start to avoid counting some buffer text | |
3691 | as matching both at the file's beginning and at the end. */ | |
3692 | while (bufpos > 0 && same_at_end > same_at_start | |
6fdaa9a0 | 3693 | && FETCH_BYTE (same_at_end - 1) == buffer[bufpos - 1]) |
3d0387c0 | 3694 | same_at_end--, bufpos--; |
727a0b4a | 3695 | |
3d0387c0 | 3696 | /* If we found a discrepancy, stop the scan. |
8e6208c5 | 3697 | Otherwise loop around and scan the preceding bufferful. */ |
3d0387c0 | 3698 | if (bufpos != 0) |
727a0b4a RS |
3699 | { |
3700 | /* If this discrepancy is because of code conversion, | |
3701 | we cannot use this method; giveup and try the other. */ | |
3702 | if (same_at_end > same_at_start | |
3703 | && FETCH_BYTE (same_at_end - 1) >= 0200 | |
71312b68 | 3704 | && ! NILP (current_buffer->enable_multibyte_characters) |
c8a6d68a | 3705 | && (CODING_MAY_REQUIRE_DECODING (&coding))) |
727a0b4a RS |
3706 | giveup_match_end = 1; |
3707 | break; | |
3708 | } | |
3d0387c0 RS |
3709 | } |
3710 | immediate_quit = 0; | |
9c28748f | 3711 | |
727a0b4a RS |
3712 | if (! giveup_match_end) |
3713 | { | |
ec7adf26 RS |
3714 | int temp; |
3715 | ||
727a0b4a | 3716 | /* We win! We can handle REPLACE the optimized way. */ |
9c28748f | 3717 | |
20f6783d RS |
3718 | /* Extend the start of non-matching text area to multibyte |
3719 | character boundary. */ | |
3720 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
3721 | while (same_at_start > BEGV_BYTE | |
3722 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
3723 | same_at_start--; | |
3724 | ||
3725 | /* Extend the end of non-matching text area to multibyte | |
71312b68 RS |
3726 | character boundary. */ |
3727 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
ec7adf26 RS |
3728 | while (same_at_end < ZV_BYTE |
3729 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
71312b68 RS |
3730 | same_at_end++; |
3731 | ||
727a0b4a | 3732 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 RS |
3733 | overlap = (same_at_start - BEGV_BYTE |
3734 | - (same_at_end + st.st_size - ZV)); | |
727a0b4a RS |
3735 | if (overlap > 0) |
3736 | same_at_end += overlap; | |
9c28748f | 3737 | |
727a0b4a | 3738 | /* Arrange to read only the nonmatching middle part of the file. */ |
ec7adf26 RS |
3739 | XSETFASTINT (beg, XINT (beg) + (same_at_start - BEGV_BYTE)); |
3740 | XSETFASTINT (end, XINT (end) - (ZV_BYTE - same_at_end)); | |
3dbcf3f6 | 3741 | |
ec7adf26 | 3742 | del_range_byte (same_at_start, same_at_end, 0); |
727a0b4a | 3743 | /* Insert from the file at the proper position. */ |
ec7adf26 RS |
3744 | temp = BYTE_TO_CHAR (same_at_start); |
3745 | SET_PT_BOTH (temp, same_at_start); | |
727a0b4a RS |
3746 | |
3747 | /* If display currently starts at beginning of line, | |
3748 | keep it that way. */ | |
3749 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) | |
3750 | XWINDOW (selected_window)->start_at_line_beg = Fbolp (); | |
3751 | ||
3752 | replace_handled = 1; | |
3753 | } | |
3dbcf3f6 RS |
3754 | } |
3755 | ||
3756 | /* If requested, replace the accessible part of the buffer | |
3757 | with the file contents. Avoid replacing text at the | |
3758 | beginning or end of the buffer that matches the file contents; | |
3759 | that preserves markers pointing to the unchanged parts. | |
3760 | ||
3761 | Here we implement this feature for the case where code conversion | |
3762 | is needed, in a simple way that needs a lot of memory. | |
3763 | The preceding if-statement handles the case of no conversion | |
3764 | in a more optimized way. */ | |
f736ffbf | 3765 | if (!NILP (replace) && ! replace_handled && BEGV < ZV) |
3dbcf3f6 | 3766 | { |
ec7adf26 RS |
3767 | int same_at_start = BEGV_BYTE; |
3768 | int same_at_end = ZV_BYTE; | |
3dbcf3f6 RS |
3769 | int overlap; |
3770 | int bufpos; | |
3771 | /* Make sure that the gap is large enough. */ | |
3772 | int bufsize = 2 * st.st_size; | |
b00ca0d7 | 3773 | unsigned char *conversion_buffer = (unsigned char *) xmalloc (bufsize); |
ec7adf26 | 3774 | int temp; |
3dbcf3f6 RS |
3775 | |
3776 | /* First read the whole file, performing code conversion into | |
3777 | CONVERSION_BUFFER. */ | |
3778 | ||
727a0b4a RS |
3779 | if (lseek (fd, XINT (beg), 0) < 0) |
3780 | { | |
68cfd853 | 3781 | xfree (conversion_buffer); |
727a0b4a | 3782 | report_file_error ("Setting file position", |
b1d1b865 | 3783 | Fcons (orig_filename, Qnil)); |
727a0b4a RS |
3784 | } |
3785 | ||
3dbcf3f6 RS |
3786 | total = st.st_size; /* Total bytes in the file. */ |
3787 | how_much = 0; /* Bytes read from file so far. */ | |
3788 | inserted = 0; /* Bytes put into CONVERSION_BUFFER so far. */ | |
3789 | unprocessed = 0; /* Bytes not processed in previous loop. */ | |
3790 | ||
3791 | while (how_much < total) | |
3792 | { | |
3793 | /* try is reserved in some compilers (Microsoft C) */ | |
3794 | int trytry = min (total - how_much, READ_BUF_SIZE - unprocessed); | |
cadf50ff | 3795 | unsigned char *destination = read_buf + unprocessed; |
3dbcf3f6 RS |
3796 | int this; |
3797 | ||
3798 | /* Allow quitting out of the actual I/O. */ | |
3799 | immediate_quit = 1; | |
3800 | QUIT; | |
3801 | this = read (fd, destination, trytry); | |
3802 | immediate_quit = 0; | |
3803 | ||
3804 | if (this < 0 || this + unprocessed == 0) | |
3805 | { | |
3806 | how_much = this; | |
3807 | break; | |
3808 | } | |
3809 | ||
3810 | how_much += this; | |
3811 | ||
c8a6d68a | 3812 | if (CODING_MAY_REQUIRE_DECODING (&coding)) |
3dbcf3f6 | 3813 | { |
c8a6d68a | 3814 | int require, result; |
3dbcf3f6 RS |
3815 | |
3816 | this += unprocessed; | |
3817 | ||
3818 | /* If we are using more space than estimated, | |
3819 | make CONVERSION_BUFFER bigger. */ | |
3820 | require = decoding_buffer_size (&coding, this); | |
3821 | if (inserted + require + 2 * (total - how_much) > bufsize) | |
3822 | { | |
3823 | bufsize = inserted + require + 2 * (total - how_much); | |
92cf1086 | 3824 | conversion_buffer = (unsigned char *) xrealloc (conversion_buffer, bufsize); |
3dbcf3f6 RS |
3825 | } |
3826 | ||
3827 | /* Convert this batch with results in CONVERSION_BUFFER. */ | |
3828 | if (how_much >= total) /* This is the last block. */ | |
c8a6d68a KH |
3829 | coding.mode |= CODING_MODE_LAST_BLOCK; |
3830 | result = decode_coding (&coding, read_buf, | |
3831 | conversion_buffer + inserted, | |
3832 | this, bufsize - inserted); | |
3dbcf3f6 RS |
3833 | |
3834 | /* Save for next iteration whatever we didn't convert. */ | |
c8a6d68a KH |
3835 | unprocessed = this - coding.consumed; |
3836 | bcopy (read_buf + coding.consumed, read_buf, unprocessed); | |
3837 | this = coding.produced; | |
3dbcf3f6 RS |
3838 | } |
3839 | ||
3840 | inserted += this; | |
3841 | } | |
3842 | ||
c8a6d68a | 3843 | /* At this point, INSERTED is how many characters (i.e. bytes) |
3dbcf3f6 RS |
3844 | are present in CONVERSION_BUFFER. |
3845 | HOW_MUCH should equal TOTAL, | |
3846 | or should be <= 0 if we couldn't read the file. */ | |
3847 | ||
3848 | if (how_much < 0) | |
3849 | { | |
a36837e4 | 3850 | xfree (conversion_buffer); |
3dbcf3f6 RS |
3851 | |
3852 | if (how_much == -1) | |
3853 | error ("IO error reading %s: %s", | |
b1d1b865 | 3854 | XSTRING (orig_filename)->data, strerror (errno)); |
3dbcf3f6 RS |
3855 | else if (how_much == -2) |
3856 | error ("maximum buffer size exceeded"); | |
3857 | } | |
3858 | ||
3859 | /* Compare the beginning of the converted file | |
3860 | with the buffer text. */ | |
3861 | ||
3862 | bufpos = 0; | |
3863 | while (bufpos < inserted && same_at_start < same_at_end | |
3864 | && FETCH_BYTE (same_at_start) == conversion_buffer[bufpos]) | |
3865 | same_at_start++, bufpos++; | |
3866 | ||
3867 | /* If the file matches the buffer completely, | |
3868 | there's no need to replace anything. */ | |
3869 | ||
3870 | if (bufpos == inserted) | |
3871 | { | |
a36837e4 | 3872 | xfree (conversion_buffer); |
3dbcf3f6 RS |
3873 | close (fd); |
3874 | specpdl_ptr--; | |
3875 | /* Truncate the buffer to the size of the file. */ | |
427f5aab KH |
3876 | del_range_byte (same_at_start, same_at_end, 0); |
3877 | inserted = 0; | |
3dbcf3f6 RS |
3878 | goto handled; |
3879 | } | |
3880 | ||
20f6783d RS |
3881 | /* Extend the start of non-matching text area to multibyte |
3882 | character boundary. */ | |
3883 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
3884 | while (same_at_start > BEGV_BYTE | |
3885 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
3886 | same_at_start--; | |
3887 | ||
3dbcf3f6 RS |
3888 | /* Scan this bufferful from the end, comparing with |
3889 | the Emacs buffer. */ | |
3890 | bufpos = inserted; | |
3891 | ||
3892 | /* Compare with same_at_start to avoid counting some buffer text | |
3893 | as matching both at the file's beginning and at the end. */ | |
3894 | while (bufpos > 0 && same_at_end > same_at_start | |
3895 | && FETCH_BYTE (same_at_end - 1) == conversion_buffer[bufpos - 1]) | |
3896 | same_at_end--, bufpos--; | |
3897 | ||
20f6783d RS |
3898 | /* Extend the end of non-matching text area to multibyte |
3899 | character boundary. */ | |
3900 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
3901 | while (same_at_end < ZV_BYTE | |
3902 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
3903 | same_at_end++; | |
3904 | ||
3dbcf3f6 | 3905 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 | 3906 | overlap = same_at_start - BEGV_BYTE - (same_at_end + inserted - ZV_BYTE); |
3dbcf3f6 RS |
3907 | if (overlap > 0) |
3908 | same_at_end += overlap; | |
3909 | ||
727a0b4a RS |
3910 | /* If display currently starts at beginning of line, |
3911 | keep it that way. */ | |
3912 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) | |
3913 | XWINDOW (selected_window)->start_at_line_beg = Fbolp (); | |
3914 | ||
3dbcf3f6 RS |
3915 | /* Replace the chars that we need to replace, |
3916 | and update INSERTED to equal the number of bytes | |
3917 | we are taking from the file. */ | |
ec7adf26 | 3918 | inserted -= (Z_BYTE - same_at_end) + (same_at_start - BEG_BYTE); |
427f5aab | 3919 | |
643c73b9 | 3920 | if (same_at_end != same_at_start) |
427f5aab KH |
3921 | { |
3922 | del_range_byte (same_at_start, same_at_end, 0); | |
3923 | temp = GPT; | |
3924 | same_at_start = GPT_BYTE; | |
3925 | } | |
643c73b9 RS |
3926 | else |
3927 | { | |
643c73b9 | 3928 | temp = BYTE_TO_CHAR (same_at_start); |
643c73b9 | 3929 | } |
427f5aab KH |
3930 | /* Insert from the file at the proper position. */ |
3931 | SET_PT_BOTH (temp, same_at_start); | |
ec7adf26 RS |
3932 | insert_1 (conversion_buffer + same_at_start - BEG_BYTE, inserted, |
3933 | 0, 0, 0); | |
427f5aab KH |
3934 | /* Set `inserted' to the number of inserted characters. */ |
3935 | inserted = PT - temp; | |
3dbcf3f6 RS |
3936 | |
3937 | free (conversion_buffer); | |
3938 | close (fd); | |
3939 | specpdl_ptr--; | |
3940 | ||
3dbcf3f6 | 3941 | goto handled; |
3d0387c0 RS |
3942 | } |
3943 | ||
d4b8687b RS |
3944 | if (! not_regular) |
3945 | { | |
3946 | register Lisp_Object temp; | |
7fded690 | 3947 | |
d4b8687b | 3948 | total = XINT (end) - XINT (beg); |
570d7624 | 3949 | |
d4b8687b RS |
3950 | /* Make sure point-max won't overflow after this insertion. */ |
3951 | XSETINT (temp, total); | |
3952 | if (total != XINT (temp)) | |
3953 | error ("Maximum buffer size exceeded"); | |
3954 | } | |
3955 | else | |
3956 | /* For a special file, all we can do is guess. */ | |
3957 | total = READ_BUF_SIZE; | |
570d7624 | 3958 | |
57d8d468 | 3959 | if (NILP (visit) && total > 0) |
6c478ee2 | 3960 | prepare_to_modify_buffer (PT, PT, NULL); |
570d7624 | 3961 | |
7fe52289 | 3962 | move_gap (PT); |
7fded690 JB |
3963 | if (GAP_SIZE < total) |
3964 | make_gap (total - GAP_SIZE); | |
3965 | ||
a1d2b64a | 3966 | if (XINT (beg) != 0 || !NILP (replace)) |
7fded690 JB |
3967 | { |
3968 | if (lseek (fd, XINT (beg), 0) < 0) | |
b1d1b865 RS |
3969 | report_file_error ("Setting file position", |
3970 | Fcons (orig_filename, Qnil)); | |
7fded690 JB |
3971 | } |
3972 | ||
6fdaa9a0 | 3973 | /* In the following loop, HOW_MUCH contains the total bytes read so |
c8a6d68a KH |
3974 | far for a regular file, and not changed for a special file. But, |
3975 | before exiting the loop, it is set to a negative value if I/O | |
3976 | error occurs. */ | |
a1d2b64a | 3977 | how_much = 0; |
6fdaa9a0 KH |
3978 | /* Total bytes inserted. */ |
3979 | inserted = 0; | |
c8a6d68a KH |
3980 | /* Here, we don't do code conversion in the loop. It is done by |
3981 | code_convert_region after all data are read into the buffer. */ | |
6fdaa9a0 | 3982 | while (how_much < total) |
570d7624 | 3983 | { |
5e570b75 | 3984 | /* try is reserved in some compilers (Microsoft C) */ |
c8a6d68a KH |
3985 | int trytry = min (total - how_much, READ_BUF_SIZE); |
3986 | int this; | |
3987 | ||
3988 | /* For a special file, GAP_SIZE should be checked every time. */ | |
3989 | if (not_regular && GAP_SIZE < trytry) | |
3990 | make_gap (total - GAP_SIZE); | |
b5148e85 RS |
3991 | |
3992 | /* Allow quitting out of the actual I/O. */ | |
3993 | immediate_quit = 1; | |
3994 | QUIT; | |
64e0ae2a | 3995 | this = read (fd, BYTE_POS_ADDR (PT_BYTE + inserted - 1) + 1, trytry); |
b5148e85 | 3996 | immediate_quit = 0; |
570d7624 | 3997 | |
c8a6d68a | 3998 | if (this <= 0) |
570d7624 JB |
3999 | { |
4000 | how_much = this; | |
4001 | break; | |
4002 | } | |
4003 | ||
c8a6d68a KH |
4004 | GAP_SIZE -= this; |
4005 | GPT_BYTE += this; | |
4006 | ZV_BYTE += this; | |
4007 | Z_BYTE += this; | |
4008 | GPT += this; | |
4009 | ZV += this; | |
4010 | Z += this; | |
4011 | ||
d4b8687b RS |
4012 | /* For a regular file, where TOTAL is the real size, |
4013 | count HOW_MUCH to compare with it. | |
4014 | For a special file, where TOTAL is just a buffer size, | |
4015 | so don't bother counting in HOW_MUCH. | |
4016 | (INSERTED is where we count the number of characters inserted.) */ | |
4017 | if (! not_regular) | |
4018 | how_much += this; | |
c8a6d68a KH |
4019 | inserted += this; |
4020 | } | |
6fdaa9a0 | 4021 | |
c8a6d68a KH |
4022 | if (GAP_SIZE > 0) |
4023 | /* Put an anchor to ensure multi-byte form ends at gap. */ | |
4024 | *GPT_ADDR = 0; | |
d4b8687b | 4025 | |
c8a6d68a | 4026 | close (fd); |
6fdaa9a0 | 4027 | |
c8a6d68a KH |
4028 | /* Discard the unwind protect for closing the file. */ |
4029 | specpdl_ptr--; | |
6fdaa9a0 | 4030 | |
c8a6d68a KH |
4031 | if (how_much < 0) |
4032 | error ("IO error reading %s: %s", | |
4033 | XSTRING (orig_filename)->data, strerror (errno)); | |
ec7adf26 | 4034 | |
2df42e09 | 4035 | if (! coding_system_decided) |
c8a6d68a | 4036 | { |
2df42e09 KH |
4037 | /* The coding system is not yet decided. Decide it by an |
4038 | optimized method for handling `coding:' tag. */ | |
4039 | Lisp_Object val; | |
4040 | val = Qnil; | |
f736ffbf | 4041 | |
2df42e09 KH |
4042 | if (!NILP (Vcoding_system_for_read)) |
4043 | val = Vcoding_system_for_read; | |
4044 | else | |
4045 | { | |
4046 | if (inserted > 0 && ! NILP (Vset_auto_coding_function)) | |
f736ffbf | 4047 | { |
2df42e09 KH |
4048 | /* Since we are sure that the current buffer was |
4049 | empty before the insertion, we can toggle | |
4050 | enable-multibyte-characters directly here without | |
4051 | taking care of marker adjustment and byte | |
4052 | combining problem. */ | |
4053 | Lisp_Object prev_multibyte; | |
4054 | int count = specpdl_ptr - specpdl; | |
4055 | ||
4056 | prev_multibyte = current_buffer->enable_multibyte_characters; | |
4057 | current_buffer->enable_multibyte_characters = Qnil; | |
4058 | record_unwind_protect (set_auto_coding_unwind, | |
4059 | prev_multibyte); | |
1255deb9 KH |
4060 | val = call2 (Vset_auto_coding_function, |
4061 | filename, make_number (inserted)); | |
2df42e09 KH |
4062 | /* Discard the unwind protect for recovering the |
4063 | error of Vset_auto_coding_function. */ | |
4064 | specpdl_ptr--; | |
4065 | current_buffer->enable_multibyte_characters = prev_multibyte; | |
4066 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); | |
4067 | } | |
f736ffbf | 4068 | |
2df42e09 KH |
4069 | if (NILP (val)) |
4070 | { | |
4071 | /* If the coding system is not yet decided, check | |
4072 | file-coding-system-alist. */ | |
4073 | Lisp_Object args[6], coding_systems; | |
f736ffbf | 4074 | |
2df42e09 KH |
4075 | args[0] = Qinsert_file_contents, args[1] = orig_filename; |
4076 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = Qnil; | |
4077 | coding_systems = Ffind_operation_coding_system (6, args); | |
4078 | if (CONSP (coding_systems)) | |
4079 | val = XCONS (coding_systems)->car; | |
f736ffbf | 4080 | } |
2df42e09 | 4081 | } |
f736ffbf | 4082 | |
2df42e09 KH |
4083 | /* The following kludgy code is to avoid some compiler bug. |
4084 | We can't simply do | |
4085 | setup_coding_system (val, &coding); | |
4086 | on some system. */ | |
4087 | { | |
4088 | struct coding_system temp_coding; | |
4089 | setup_coding_system (val, &temp_coding); | |
4090 | bcopy (&temp_coding, &coding, sizeof coding); | |
4091 | } | |
f736ffbf | 4092 | |
237a6fd2 RS |
4093 | if (NILP (current_buffer->enable_multibyte_characters) |
4094 | && ! NILP (val)) | |
4095 | /* We must suppress all character code conversion except for | |
2df42e09 KH |
4096 | end-of-line conversion. */ |
4097 | setup_raw_text_coding_system (&coding); | |
4098 | } | |
f736ffbf | 4099 | |
c91beee2 | 4100 | if (inserted > 0 || coding.type == coding_type_ccl) |
2df42e09 | 4101 | { |
c8a6d68a | 4102 | if (CODING_MAY_REQUIRE_DECODING (&coding)) |
64e0ae2a | 4103 | { |
f4ac86af KH |
4104 | /* Here, we don't have to consider byte combining (see the |
4105 | comment below) because code_convert_region takes care of | |
4106 | it. */ | |
64e0ae2a KH |
4107 | code_convert_region (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted, |
4108 | &coding, 0, 0); | |
4109 | inserted = (NILP (current_buffer->enable_multibyte_characters) | |
4110 | ? coding.produced : coding.produced_char); | |
4111 | } | |
f8198e19 KH |
4112 | else if (!NILP (current_buffer->enable_multibyte_characters)) |
4113 | { | |
4114 | int inserted_byte = inserted; | |
4115 | ||
f4ac86af KH |
4116 | /* There's a possibility that we must combine bytes at the |
4117 | head (resp. the tail) of the just inserted text with the | |
4118 | bytes before (resp. after) the gap to form a single | |
12fccb85 KH |
4119 | character. */ |
4120 | inserted = multibyte_chars_in_text (GPT_ADDR - inserted, inserted); | |
4121 | adjust_after_insert (PT, PT_BYTE, | |
4122 | PT + inserted_byte, PT_BYTE + inserted_byte, | |
4123 | inserted); | |
f8198e19 | 4124 | } |
e9cea947 AS |
4125 | else |
4126 | adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted, | |
4127 | inserted); | |
2df42e09 | 4128 | } |
570d7624 | 4129 | |
04e6f79c | 4130 | #ifdef DOS_NT |
2df42e09 KH |
4131 | /* Use the conversion type to determine buffer-file-type |
4132 | (find-buffer-file-type is now used to help determine the | |
4133 | conversion). */ | |
4134 | if ((coding.eol_type == CODING_EOL_UNDECIDED | |
4135 | || coding.eol_type == CODING_EOL_LF) | |
4136 | && ! CODING_REQUIRE_DECODING (&coding)) | |
4137 | current_buffer->buffer_file_type = Qt; | |
4138 | else | |
4139 | current_buffer->buffer_file_type = Qnil; | |
04e6f79c | 4140 | #endif |
570d7624 JB |
4141 | |
4142 | notfound: | |
32f4334d | 4143 | handled: |
570d7624 | 4144 | |
265a9e55 | 4145 | if (!NILP (visit)) |
570d7624 | 4146 | { |
cfadd376 RS |
4147 | if (!EQ (current_buffer->undo_list, Qt)) |
4148 | current_buffer->undo_list = Qnil; | |
570d7624 JB |
4149 | #ifdef APOLLO |
4150 | stat (XSTRING (filename)->data, &st); | |
4151 | #endif | |
62bcf009 | 4152 | |
a7e82472 RS |
4153 | if (NILP (handler)) |
4154 | { | |
4155 | current_buffer->modtime = st.st_mtime; | |
b1d1b865 | 4156 | current_buffer->filename = orig_filename; |
a7e82472 | 4157 | } |
62bcf009 | 4158 | |
95385625 | 4159 | SAVE_MODIFF = MODIFF; |
570d7624 | 4160 | current_buffer->auto_save_modified = MODIFF; |
2acfd7ae | 4161 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
570d7624 | 4162 | #ifdef CLASH_DETECTION |
32f4334d RS |
4163 | if (NILP (handler)) |
4164 | { | |
f471f4c2 RS |
4165 | if (!NILP (current_buffer->file_truename)) |
4166 | unlock_file (current_buffer->file_truename); | |
32f4334d RS |
4167 | unlock_file (filename); |
4168 | } | |
570d7624 | 4169 | #endif /* CLASH_DETECTION */ |
330bfe57 RS |
4170 | if (not_regular) |
4171 | Fsignal (Qfile_error, | |
4172 | Fcons (build_string ("not a regular file"), | |
b1d1b865 | 4173 | Fcons (orig_filename, Qnil))); |
330bfe57 | 4174 | |
570d7624 | 4175 | /* If visiting nonexistent file, return nil. */ |
32f4334d | 4176 | if (current_buffer->modtime == -1) |
b1d1b865 | 4177 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
570d7624 JB |
4178 | } |
4179 | ||
0d420e88 | 4180 | /* Decode file format */ |
c8a6d68a | 4181 | if (inserted > 0) |
0d420e88 | 4182 | { |
199607e4 | 4183 | insval = call3 (Qformat_decode, |
c8a6d68a | 4184 | Qnil, make_number (inserted), visit); |
0d420e88 | 4185 | CHECK_NUMBER (insval, 0); |
c8a6d68a | 4186 | inserted = XFASTINT (insval); |
0d420e88 BG |
4187 | } |
4188 | ||
0342d8c5 RS |
4189 | /* Call after-change hooks for the inserted text, aside from the case |
4190 | of normal visiting (not with REPLACE), which is done in a new buffer | |
4191 | "before" the buffer is changed. */ | |
c8a6d68a | 4192 | if (inserted > 0 && total > 0 |
0342d8c5 | 4193 | && (NILP (visit) || !NILP (replace))) |
c8a6d68a | 4194 | signal_after_change (PT, 0, inserted); |
199607e4 | 4195 | |
2df42e09 | 4196 | if (set_coding_system) |
ec7adf26 | 4197 | Vlast_coding_system_used = coding.symbol; |
b56567b5 | 4198 | |
d6a3cc15 RS |
4199 | if (inserted > 0) |
4200 | { | |
4201 | p = Vafter_insert_file_functions; | |
4202 | while (!NILP (p)) | |
4203 | { | |
c8a6d68a | 4204 | insval = call1 (Fcar (p), make_number (inserted)); |
d6a3cc15 RS |
4205 | if (!NILP (insval)) |
4206 | { | |
4207 | CHECK_NUMBER (insval, 0); | |
c8a6d68a | 4208 | inserted = XFASTINT (insval); |
d6a3cc15 RS |
4209 | } |
4210 | QUIT; | |
4211 | p = Fcdr (p); | |
4212 | } | |
4213 | } | |
4214 | ||
ec7adf26 | 4215 | /* ??? Retval needs to be dealt with in all cases consistently. */ |
a1d2b64a | 4216 | if (NILP (val)) |
b1d1b865 | 4217 | val = Fcons (orig_filename, |
a1d2b64a RS |
4218 | Fcons (make_number (inserted), |
4219 | Qnil)); | |
4220 | ||
4221 | RETURN_UNGCPRO (unbind_to (count, val)); | |
570d7624 | 4222 | } |
7fded690 | 4223 | \f |
ec7adf26 RS |
4224 | static Lisp_Object build_annotations P_ ((Lisp_Object, Lisp_Object, |
4225 | Lisp_Object)); | |
d6a3cc15 | 4226 | |
6fc6f94b | 4227 | /* If build_annotations switched buffers, switch back to BUF. |
6fdaa9a0 KH |
4228 | Kill the temporary buffer that was selected in the meantime. |
4229 | ||
4230 | Since this kill only the last temporary buffer, some buffers remain | |
4231 | not killed if build_annotations switched buffers more than once. | |
4232 | -- K.Handa */ | |
6fc6f94b | 4233 | |
199607e4 | 4234 | static Lisp_Object |
6fc6f94b RS |
4235 | build_annotations_unwind (buf) |
4236 | Lisp_Object buf; | |
4237 | { | |
4238 | Lisp_Object tembuf; | |
4239 | ||
4240 | if (XBUFFER (buf) == current_buffer) | |
4241 | return Qnil; | |
4242 | tembuf = Fcurrent_buffer (); | |
4243 | Fset_buffer (buf); | |
4244 | Fkill_buffer (tembuf); | |
4245 | return Qnil; | |
4246 | } | |
4247 | ||
de1d0127 RS |
4248 | DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 7, |
4249 | "r\nFWrite region to file: \ni\ni\ni\np", | |
570d7624 JB |
4250 | "Write current region into specified file.\n\ |
4251 | When called from a program, takes three arguments:\n\ | |
4252 | START, END and FILENAME. START and END are buffer positions.\n\ | |
4253 | Optional fourth argument APPEND if non-nil means\n\ | |
4254 | append to existing file contents (if any).\n\ | |
4255 | Optional fifth argument VISIT if t means\n\ | |
4256 | set the last-save-file-modtime of buffer to this file's modtime\n\ | |
4257 | and mark buffer not modified.\n\ | |
3b7792ed RS |
4258 | If VISIT is a string, it is a second file name;\n\ |
4259 | the output goes to FILENAME, but the buffer is marked as visiting VISIT.\n\ | |
4260 | VISIT is also the file name to lock and unlock for clash detection.\n\ | |
1d386d28 RS |
4261 | If VISIT is neither t nor nil nor a string,\n\ |
4262 | that means do not print the \"Wrote file\" message.\n\ | |
7204a979 | 4263 | The optional sixth arg LOCKNAME, if non-nil, specifies the name to\n\ |
8b68aae7 | 4264 | use for locking and unlocking, overriding FILENAME and VISIT.\n\ |
de1d0127 RS |
4265 | The optional seventh arg CONFIRM, if non-nil, says ask for confirmation\n\ |
4266 | before overwriting an existing file.\n\ | |
570d7624 | 4267 | Kludgy feature: if START is a string, then that string is written\n\ |
6cf71bf1 KH |
4268 | to the file, instead of any buffer contents, and END is ignored.\n\ |
4269 | \n\ | |
4270 | This does code conversion according to the value of\n\ | |
4271 | `coding-system-for-write', `buffer-file-coding-system', or\n\ | |
4272 | `file-coding-system-alist', and sets the variable\n\ | |
4273 | `last-coding-system-used' to the coding system actually used.") | |
4274 | ||
de1d0127 RS |
4275 | (start, end, filename, append, visit, lockname, confirm) |
4276 | Lisp_Object start, end, filename, append, visit, lockname, confirm; | |
570d7624 JB |
4277 | { |
4278 | register int desc; | |
4279 | int failure; | |
4280 | int save_errno; | |
4281 | unsigned char *fn; | |
4282 | struct stat st; | |
c975dd7a | 4283 | int tem; |
570d7624 | 4284 | int count = specpdl_ptr - specpdl; |
6fc6f94b | 4285 | int count1; |
570d7624 | 4286 | #ifdef VMS |
5e570b75 | 4287 | unsigned char *fname = 0; /* If non-0, original filename (must rename) */ |
570d7624 | 4288 | #endif /* VMS */ |
3eac9910 | 4289 | Lisp_Object handler; |
4ad827c5 | 4290 | Lisp_Object visit_file; |
d6a3cc15 | 4291 | Lisp_Object annotations; |
b1d1b865 | 4292 | Lisp_Object encoded_filename; |
d6a3cc15 | 4293 | int visiting, quietly; |
7204a979 | 4294 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
6fc6f94b | 4295 | struct buffer *given_buffer; |
5e570b75 | 4296 | #ifdef DOS_NT |
fa228724 | 4297 | int buffer_file_type = O_BINARY; |
5e570b75 | 4298 | #endif /* DOS_NT */ |
6fdaa9a0 | 4299 | struct coding_system coding; |
570d7624 | 4300 | |
95385625 RS |
4301 | if (current_buffer->base_buffer && ! NILP (visit)) |
4302 | error ("Cannot do file visiting in an indirect buffer"); | |
4303 | ||
561cb8e1 | 4304 | if (!NILP (start) && !STRINGP (start)) |
570d7624 JB |
4305 | validate_region (&start, &end); |
4306 | ||
115af127 | 4307 | GCPRO4 (start, filename, visit, lockname); |
cdfb0f1d | 4308 | |
b1d1b865 | 4309 | /* Decide the coding-system to encode the data with. */ |
cdfb0f1d KH |
4310 | { |
4311 | Lisp_Object val; | |
4312 | ||
cbc64b2a | 4313 | if (auto_saving) |
cdfb0f1d | 4314 | val = Qnil; |
cdfb0f1d KH |
4315 | else if (!NILP (Vcoding_system_for_write)) |
4316 | val = Vcoding_system_for_write; | |
1255deb9 | 4317 | else |
450c1a67 KH |
4318 | { |
4319 | /* If the variable `buffer-file-coding-system' is set locally, | |
4320 | it means that the file was read with some kind of code | |
4321 | conversion or the varialbe is explicitely set by users. We | |
4322 | had better write it out with the same coding system even if | |
4323 | `enable-multibyte-characters' is nil. | |
4324 | ||
c8a6d68a | 4325 | If it is not set locally, we anyway have to convert EOL |
450c1a67 KH |
4326 | format if the default value of `buffer-file-coding-system' |
4327 | tells that it is not Unix-like (LF only) format. */ | |
ef38927f KH |
4328 | int using_default_coding = 0; |
4329 | int force_raw_text = 0; | |
4330 | ||
450c1a67 | 4331 | val = current_buffer->buffer_file_coding_system; |
1255deb9 KH |
4332 | if (NILP (val) |
4333 | || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil))) | |
450c1a67 | 4334 | { |
450c1a67 | 4335 | val = Qnil; |
ef38927f KH |
4336 | if (NILP (current_buffer->enable_multibyte_characters)) |
4337 | force_raw_text = 1; | |
450c1a67 | 4338 | } |
ef38927f | 4339 | |
1255deb9 KH |
4340 | if (NILP (val)) |
4341 | { | |
4342 | /* Check file-coding-system-alist. */ | |
4343 | Lisp_Object args[7], coding_systems; | |
4344 | ||
4345 | args[0] = Qwrite_region; args[1] = start; args[2] = end; | |
4346 | args[3] = filename; args[4] = append; args[5] = visit; | |
4347 | args[6] = lockname; | |
4348 | coding_systems = Ffind_operation_coding_system (7, args); | |
4349 | if (CONSP (coding_systems) && !NILP (XCONS (coding_systems)->cdr)) | |
4350 | val = XCONS (coding_systems)->cdr; | |
4351 | } | |
4352 | ||
ef38927f KH |
4353 | if (NILP (val) |
4354 | && !NILP (current_buffer->buffer_file_coding_system)) | |
4355 | { | |
4356 | /* If we still have not decided a coding system, use the | |
4357 | default value of buffer-file-coding-system. */ | |
4358 | val = current_buffer->buffer_file_coding_system; | |
4359 | using_default_coding = 1; | |
4360 | } | |
1255deb9 | 4361 | |
ef38927f | 4362 | if (!force_raw_text |
1255deb9 KH |
4363 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) |
4364 | /* Confirm that VAL can surely encode the current region. */ | |
c8a6d68a | 4365 | val = call3 (Vselect_safe_coding_system_function, start, end, val); |
ef38927f KH |
4366 | |
4367 | setup_coding_system (Fcheck_coding_system (val), &coding); | |
4368 | if (coding.eol_type == CODING_EOL_UNDECIDED | |
4369 | && !using_default_coding) | |
4370 | { | |
4371 | if (! EQ (default_buffer_file_coding.symbol, | |
4372 | buffer_defaults.buffer_file_coding_system)) | |
4373 | setup_coding_system (buffer_defaults.buffer_file_coding_system, | |
4374 | &default_buffer_file_coding); | |
4375 | if (default_buffer_file_coding.eol_type != CODING_EOL_UNDECIDED) | |
4376 | { | |
4377 | Lisp_Object subsidiaries; | |
4378 | ||
4379 | coding.eol_type = default_buffer_file_coding.eol_type; | |
4380 | subsidiaries = Fget (coding.symbol, Qeol_type); | |
4381 | if (VECTORP (subsidiaries) | |
4382 | && XVECTOR (subsidiaries)->size == 3) | |
4383 | coding.symbol | |
4384 | = XVECTOR (subsidiaries)->contents[coding.eol_type]; | |
4385 | } | |
4386 | } | |
4387 | ||
4388 | if (force_raw_text) | |
4389 | setup_raw_text_coding_system (&coding); | |
4390 | goto done_setup_coding; | |
cdfb0f1d | 4391 | } |
ef38927f | 4392 | |
1255deb9 | 4393 | setup_coding_system (Fcheck_coding_system (val), &coding); |
450c1a67 KH |
4394 | |
4395 | done_setup_coding: | |
cdfb0f1d | 4396 | if (!STRINGP (start) && !NILP (current_buffer->selective_display)) |
c8a6d68a | 4397 | coding.mode |= CODING_MODE_SELECTIVE_DISPLAY; |
cdfb0f1d KH |
4398 | } |
4399 | ||
b56567b5 KH |
4400 | Vlast_coding_system_used = coding.symbol; |
4401 | ||
570d7624 | 4402 | filename = Fexpand_file_name (filename, Qnil); |
de1d0127 RS |
4403 | |
4404 | if (! NILP (confirm)) | |
b8b29dc9 | 4405 | barf_or_query_if_file_exists (filename, "overwrite", 1, 0, 1); |
de1d0127 | 4406 | |
561cb8e1 | 4407 | if (STRINGP (visit)) |
e5176bae | 4408 | visit_file = Fexpand_file_name (visit, Qnil); |
4ad827c5 RS |
4409 | else |
4410 | visit_file = filename; | |
1a04498e | 4411 | UNGCPRO; |
4ad827c5 | 4412 | |
561cb8e1 | 4413 | visiting = (EQ (visit, Qt) || STRINGP (visit)); |
d6a3cc15 RS |
4414 | quietly = !NILP (visit); |
4415 | ||
4416 | annotations = Qnil; | |
4417 | ||
7204a979 RS |
4418 | if (NILP (lockname)) |
4419 | lockname = visit_file; | |
4420 | ||
4421 | GCPRO5 (start, filename, annotations, visit_file, lockname); | |
570d7624 | 4422 | |
32f4334d RS |
4423 | /* If the file name has special constructs in it, |
4424 | call the corresponding file handler. */ | |
49307295 | 4425 | handler = Ffind_file_name_handler (filename, Qwrite_region); |
b56ad927 | 4426 | /* If FILENAME has no handler, see if VISIT has one. */ |
93c30b5f | 4427 | if (NILP (handler) && STRINGP (visit)) |
199607e4 | 4428 | handler = Ffind_file_name_handler (visit, Qwrite_region); |
3eac9910 | 4429 | |
32f4334d RS |
4430 | if (!NILP (handler)) |
4431 | { | |
32f4334d | 4432 | Lisp_Object val; |
51cf6d37 RS |
4433 | val = call6 (handler, Qwrite_region, start, end, |
4434 | filename, append, visit); | |
32f4334d | 4435 | |
d6a3cc15 | 4436 | if (visiting) |
32f4334d | 4437 | { |
95385625 | 4438 | SAVE_MODIFF = MODIFF; |
2acfd7ae | 4439 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
3b7792ed | 4440 | current_buffer->filename = visit_file; |
32f4334d | 4441 | } |
09121adc | 4442 | UNGCPRO; |
32f4334d RS |
4443 | return val; |
4444 | } | |
4445 | ||
561cb8e1 RS |
4446 | /* Special kludge to simplify auto-saving. */ |
4447 | if (NILP (start)) | |
4448 | { | |
2acfd7ae KH |
4449 | XSETFASTINT (start, BEG); |
4450 | XSETFASTINT (end, Z); | |
561cb8e1 RS |
4451 | } |
4452 | ||
6fc6f94b RS |
4453 | record_unwind_protect (build_annotations_unwind, Fcurrent_buffer ()); |
4454 | count1 = specpdl_ptr - specpdl; | |
4455 | ||
4456 | given_buffer = current_buffer; | |
6fdaa9a0 | 4457 | annotations = build_annotations (start, end, coding.pre_write_conversion); |
6fc6f94b RS |
4458 | if (current_buffer != given_buffer) |
4459 | { | |
3cf29f61 RS |
4460 | XSETFASTINT (start, BEGV); |
4461 | XSETFASTINT (end, ZV); | |
6fc6f94b | 4462 | } |
d6a3cc15 | 4463 | |
570d7624 JB |
4464 | #ifdef CLASH_DETECTION |
4465 | if (!auto_saving) | |
84f6296a | 4466 | { |
a9171faa | 4467 | #if 0 /* This causes trouble for GNUS. */ |
84f6296a RS |
4468 | /* If we've locked this file for some other buffer, |
4469 | query before proceeding. */ | |
4470 | if (!visiting && EQ (Ffile_locked_p (lockname), Qt)) | |
bffd00b0 | 4471 | call2 (intern ("ask-user-about-lock"), filename, Vuser_login_name); |
a9171faa | 4472 | #endif |
84f6296a RS |
4473 | |
4474 | lock_file (lockname); | |
4475 | } | |
570d7624 JB |
4476 | #endif /* CLASH_DETECTION */ |
4477 | ||
b1d1b865 RS |
4478 | encoded_filename = ENCODE_FILE (filename); |
4479 | ||
4480 | fn = XSTRING (encoded_filename)->data; | |
570d7624 | 4481 | desc = -1; |
265a9e55 | 4482 | if (!NILP (append)) |
5e570b75 | 4483 | #ifdef DOS_NT |
4c3c22f3 | 4484 | desc = open (fn, O_WRONLY | buffer_file_type); |
5e570b75 | 4485 | #else /* not DOS_NT */ |
570d7624 | 4486 | desc = open (fn, O_WRONLY); |
5e570b75 | 4487 | #endif /* not DOS_NT */ |
570d7624 | 4488 | |
b1d1b865 | 4489 | if (desc < 0 && (NILP (append) || errno == ENOENT)) |
570d7624 | 4490 | #ifdef VMS |
5e570b75 | 4491 | if (auto_saving) /* Overwrite any previous version of autosave file */ |
570d7624 | 4492 | { |
5e570b75 | 4493 | vms_truncate (fn); /* if fn exists, truncate to zero length */ |
570d7624 JB |
4494 | desc = open (fn, O_RDWR); |
4495 | if (desc < 0) | |
561cb8e1 | 4496 | desc = creat_copy_attrs (STRINGP (current_buffer->filename) |
b72dea2a JB |
4497 | ? XSTRING (current_buffer->filename)->data : 0, |
4498 | fn); | |
570d7624 | 4499 | } |
5e570b75 | 4500 | else /* Write to temporary name and rename if no errors */ |
570d7624 JB |
4501 | { |
4502 | Lisp_Object temp_name; | |
4503 | temp_name = Ffile_name_directory (filename); | |
4504 | ||
265a9e55 | 4505 | if (!NILP (temp_name)) |
570d7624 JB |
4506 | { |
4507 | temp_name = Fmake_temp_name (concat2 (temp_name, | |
4508 | build_string ("$$SAVE$$"))); | |
4509 | fname = XSTRING (filename)->data; | |
4510 | fn = XSTRING (temp_name)->data; | |
4511 | desc = creat_copy_attrs (fname, fn); | |
4512 | if (desc < 0) | |
4513 | { | |
4514 | /* If we can't open the temporary file, try creating a new | |
4515 | version of the original file. VMS "creat" creates a | |
4516 | new version rather than truncating an existing file. */ | |
4517 | fn = fname; | |
4518 | fname = 0; | |
4519 | desc = creat (fn, 0666); | |
4520 | #if 0 /* This can clobber an existing file and fail to replace it, | |
4521 | if the user runs out of space. */ | |
4522 | if (desc < 0) | |
4523 | { | |
4524 | /* We can't make a new version; | |
4525 | try to truncate and rewrite existing version if any. */ | |
4526 | vms_truncate (fn); | |
4527 | desc = open (fn, O_RDWR); | |
4528 | } | |
4529 | #endif | |
4530 | } | |
4531 | } | |
4532 | else | |
4533 | desc = creat (fn, 0666); | |
4534 | } | |
4535 | #else /* not VMS */ | |
5e570b75 | 4536 | #ifdef DOS_NT |
199607e4 RS |
4537 | desc = open (fn, |
4538 | O_WRONLY | O_TRUNC | O_CREAT | buffer_file_type, | |
4c3c22f3 | 4539 | S_IREAD | S_IWRITE); |
5e570b75 | 4540 | #else /* not DOS_NT */ |
570d7624 | 4541 | desc = creat (fn, auto_saving ? auto_save_mode_bits : 0666); |
5e570b75 | 4542 | #endif /* not DOS_NT */ |
570d7624 JB |
4543 | #endif /* not VMS */ |
4544 | ||
09121adc RS |
4545 | UNGCPRO; |
4546 | ||
570d7624 JB |
4547 | if (desc < 0) |
4548 | { | |
4549 | #ifdef CLASH_DETECTION | |
4550 | save_errno = errno; | |
7204a979 | 4551 | if (!auto_saving) unlock_file (lockname); |
570d7624 JB |
4552 | errno = save_errno; |
4553 | #endif /* CLASH_DETECTION */ | |
4554 | report_file_error ("Opening output file", Fcons (filename, Qnil)); | |
4555 | } | |
4556 | ||
4557 | record_unwind_protect (close_file_unwind, make_number (desc)); | |
4558 | ||
c1c4693e | 4559 | if (!NILP (append) && !NILP (Ffile_regular_p (filename))) |
570d7624 JB |
4560 | if (lseek (desc, 0, 2) < 0) |
4561 | { | |
4562 | #ifdef CLASH_DETECTION | |
7204a979 | 4563 | if (!auto_saving) unlock_file (lockname); |
570d7624 JB |
4564 | #endif /* CLASH_DETECTION */ |
4565 | report_file_error ("Lseek error", Fcons (filename, Qnil)); | |
4566 | } | |
4567 | ||
4568 | #ifdef VMS | |
4569 | /* | |
4570 | * Kludge Warning: The VMS C RTL likes to insert carriage returns | |
4571 | * if we do writes that don't end with a carriage return. Furthermore | |
4572 | * it cannot handle writes of more then 16K. The modified | |
4573 | * version of "sys_write" in SYSDEP.C (see comment there) copes with | |
4574 | * this EXCEPT for the last record (iff it doesn't end with a carriage | |
4575 | * return). This implies that if your buffer doesn't end with a carriage | |
4576 | * return, you get one free... tough. However it also means that if | |
4577 | * we make two calls to sys_write (a la the following code) you can | |
4578 | * get one at the gap as well. The easiest way to fix this (honest) | |
4579 | * is to move the gap to the next newline (or the end of the buffer). | |
4580 | * Thus this change. | |
4581 | * | |
4582 | * Yech! | |
4583 | */ | |
4584 | if (GPT > BEG && GPT_ADDR[-1] != '\n') | |
4585 | move_gap (find_next_newline (GPT, 1)); | |
cdfb0f1d KH |
4586 | #else |
4587 | /* Whether VMS or not, we must move the gap to the next of newline | |
4588 | when we must put designation sequences at beginning of line. */ | |
4589 | if (INTEGERP (start) | |
4590 | && coding.type == coding_type_iso2022 | |
4591 | && coding.flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL | |
4592 | && GPT > BEG && GPT_ADDR[-1] != '\n') | |
ec7adf26 RS |
4593 | { |
4594 | int opoint = PT, opoint_byte = PT_BYTE; | |
4595 | scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, 1, 0); | |
4596 | move_gap_both (PT, PT_BYTE); | |
4597 | SET_PT_BOTH (opoint, opoint_byte); | |
4598 | } | |
570d7624 JB |
4599 | #endif |
4600 | ||
4601 | failure = 0; | |
4602 | immediate_quit = 1; | |
4603 | ||
561cb8e1 | 4604 | if (STRINGP (start)) |
570d7624 | 4605 | { |
d6a3cc15 | 4606 | failure = 0 > a_write (desc, XSTRING (start)->data, |
fc932ac6 | 4607 | STRING_BYTES (XSTRING (start)), 0, &annotations, |
55a7907f | 4608 | &coding); |
570d7624 JB |
4609 | save_errno = errno; |
4610 | } | |
4611 | else if (XINT (start) != XINT (end)) | |
4612 | { | |
ec7adf26 RS |
4613 | register int end1 = CHAR_TO_BYTE (XINT (end)); |
4614 | ||
4615 | tem = CHAR_TO_BYTE (XINT (start)); | |
4616 | ||
570d7624 JB |
4617 | if (XINT (start) < GPT) |
4618 | { | |
ec7adf26 RS |
4619 | failure = 0 > a_write (desc, BYTE_POS_ADDR (tem), |
4620 | min (GPT_BYTE, end1) - tem, tem, &annotations, | |
6fdaa9a0 | 4621 | &coding); |
570d7624 JB |
4622 | save_errno = errno; |
4623 | } | |
4624 | ||
4625 | if (XINT (end) > GPT && !failure) | |
4626 | { | |
ec7adf26 RS |
4627 | tem = max (tem, GPT_BYTE); |
4628 | failure = 0 > a_write (desc, BYTE_POS_ADDR (tem), end1 - tem, | |
6fdaa9a0 | 4629 | tem, &annotations, &coding); |
d6a3cc15 RS |
4630 | save_errno = errno; |
4631 | } | |
69f6e679 RS |
4632 | } |
4633 | else | |
4634 | { | |
4635 | /* If file was empty, still need to write the annotations */ | |
c8a6d68a | 4636 | coding.mode |= CODING_MODE_LAST_BLOCK; |
6fdaa9a0 KH |
4637 | failure = 0 > a_write (desc, "", 0, XINT (start), &annotations, &coding); |
4638 | save_errno = errno; | |
4639 | } | |
4640 | ||
c8a6d68a KH |
4641 | if (CODING_REQUIRE_FLUSHING (&coding) |
4642 | && !(coding.mode & CODING_MODE_LAST_BLOCK) | |
1354debd | 4643 | && ! failure) |
6fdaa9a0 KH |
4644 | { |
4645 | /* We have to flush out a data. */ | |
c8a6d68a | 4646 | coding.mode |= CODING_MODE_LAST_BLOCK; |
6fdaa9a0 | 4647 | failure = 0 > e_write (desc, "", 0, &coding); |
69f6e679 | 4648 | save_errno = errno; |
570d7624 JB |
4649 | } |
4650 | ||
4651 | immediate_quit = 0; | |
4652 | ||
6e23c83e | 4653 | #ifdef HAVE_FSYNC |
570d7624 JB |
4654 | /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun). |
4655 | Disk full in NFS may be reported here. */ | |
1daffa1c RS |
4656 | /* mib says that closing the file will try to write as fast as NFS can do |
4657 | it, and that means the fsync here is not crucial for autosave files. */ | |
4658 | if (!auto_saving && fsync (desc) < 0) | |
cb33c142 KH |
4659 | { |
4660 | /* If fsync fails with EINTR, don't treat that as serious. */ | |
4661 | if (errno != EINTR) | |
4662 | failure = 1, save_errno = errno; | |
4663 | } | |
570d7624 JB |
4664 | #endif |
4665 | ||
199607e4 | 4666 | /* Spurious "file has changed on disk" warnings have been |
570d7624 JB |
4667 | observed on Suns as well. |
4668 | It seems that `close' can change the modtime, under nfs. | |
4669 | ||
4670 | (This has supposedly been fixed in Sunos 4, | |
4671 | but who knows about all the other machines with NFS?) */ | |
4672 | #if 0 | |
4673 | ||
4674 | /* On VMS and APOLLO, must do the stat after the close | |
4675 | since closing changes the modtime. */ | |
4676 | #ifndef VMS | |
4677 | #ifndef APOLLO | |
4678 | /* Recall that #if defined does not work on VMS. */ | |
4679 | #define FOO | |
4680 | fstat (desc, &st); | |
4681 | #endif | |
4682 | #endif | |
4683 | #endif | |
4684 | ||
4685 | /* NFS can report a write failure now. */ | |
4686 | if (close (desc) < 0) | |
4687 | failure = 1, save_errno = errno; | |
4688 | ||
4689 | #ifdef VMS | |
4690 | /* If we wrote to a temporary name and had no errors, rename to real name. */ | |
4691 | if (fname) | |
4692 | { | |
4693 | if (!failure) | |
4694 | failure = (rename (fn, fname) != 0), save_errno = errno; | |
4695 | fn = fname; | |
4696 | } | |
4697 | #endif /* VMS */ | |
4698 | ||
4699 | #ifndef FOO | |
4700 | stat (fn, &st); | |
4701 | #endif | |
6fc6f94b RS |
4702 | /* Discard the unwind protect for close_file_unwind. */ |
4703 | specpdl_ptr = specpdl + count1; | |
4704 | /* Restore the original current buffer. */ | |
98295b48 | 4705 | visit_file = unbind_to (count, visit_file); |
570d7624 JB |
4706 | |
4707 | #ifdef CLASH_DETECTION | |
4708 | if (!auto_saving) | |
7204a979 | 4709 | unlock_file (lockname); |
570d7624 JB |
4710 | #endif /* CLASH_DETECTION */ |
4711 | ||
4712 | /* Do this before reporting IO error | |
4713 | to avoid a "file has changed on disk" warning on | |
4714 | next attempt to save. */ | |
d6a3cc15 | 4715 | if (visiting) |
570d7624 JB |
4716 | current_buffer->modtime = st.st_mtime; |
4717 | ||
4718 | if (failure) | |
b1d1b865 RS |
4719 | error ("IO error writing %s: %s", XSTRING (filename)->data, |
4720 | strerror (save_errno)); | |
570d7624 | 4721 | |
d6a3cc15 | 4722 | if (visiting) |
570d7624 | 4723 | { |
95385625 | 4724 | SAVE_MODIFF = MODIFF; |
2acfd7ae | 4725 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
3b7792ed | 4726 | current_buffer->filename = visit_file; |
f4226e89 | 4727 | update_mode_lines++; |
570d7624 | 4728 | } |
d6a3cc15 | 4729 | else if (quietly) |
570d7624 JB |
4730 | return Qnil; |
4731 | ||
4732 | if (!auto_saving) | |
60d67b83 | 4733 | message_with_string ("Wrote %s", visit_file, 1); |
570d7624 JB |
4734 | |
4735 | return Qnil; | |
4736 | } | |
ec7adf26 | 4737 | \f |
d6a3cc15 RS |
4738 | Lisp_Object merge (); |
4739 | ||
4740 | DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0, | |
2ba0ccff | 4741 | "Return t if (car A) is numerically less than (car B).") |
d6a3cc15 RS |
4742 | (a, b) |
4743 | Lisp_Object a, b; | |
4744 | { | |
4745 | return Flss (Fcar (a), Fcar (b)); | |
4746 | } | |
4747 | ||
4748 | /* Build the complete list of annotations appropriate for writing out | |
4749 | the text between START and END, by calling all the functions in | |
6fc6f94b RS |
4750 | write-region-annotate-functions and merging the lists they return. |
4751 | If one of these functions switches to a different buffer, we assume | |
4752 | that buffer contains altered text. Therefore, the caller must | |
4753 | make sure to restore the current buffer in all cases, | |
4754 | as save-excursion would do. */ | |
d6a3cc15 RS |
4755 | |
4756 | static Lisp_Object | |
6fdaa9a0 KH |
4757 | build_annotations (start, end, pre_write_conversion) |
4758 | Lisp_Object start, end, pre_write_conversion; | |
d6a3cc15 RS |
4759 | { |
4760 | Lisp_Object annotations; | |
4761 | Lisp_Object p, res; | |
4762 | struct gcpro gcpro1, gcpro2; | |
0a20b684 RS |
4763 | Lisp_Object original_buffer; |
4764 | ||
4765 | XSETBUFFER (original_buffer, current_buffer); | |
d6a3cc15 RS |
4766 | |
4767 | annotations = Qnil; | |
4768 | p = Vwrite_region_annotate_functions; | |
4769 | GCPRO2 (annotations, p); | |
4770 | while (!NILP (p)) | |
4771 | { | |
6fc6f94b RS |
4772 | struct buffer *given_buffer = current_buffer; |
4773 | Vwrite_region_annotations_so_far = annotations; | |
d6a3cc15 | 4774 | res = call2 (Fcar (p), start, end); |
6fc6f94b RS |
4775 | /* If the function makes a different buffer current, |
4776 | assume that means this buffer contains altered text to be output. | |
4777 | Reset START and END from the buffer bounds | |
4778 | and discard all previous annotations because they should have | |
4779 | been dealt with by this function. */ | |
4780 | if (current_buffer != given_buffer) | |
4781 | { | |
3cf29f61 RS |
4782 | XSETFASTINT (start, BEGV); |
4783 | XSETFASTINT (end, ZV); | |
6fc6f94b RS |
4784 | annotations = Qnil; |
4785 | } | |
d6a3cc15 RS |
4786 | Flength (res); /* Check basic validity of return value */ |
4787 | annotations = merge (annotations, res, Qcar_less_than_car); | |
4788 | p = Fcdr (p); | |
4789 | } | |
0d420e88 BG |
4790 | |
4791 | /* Now do the same for annotation functions implied by the file-format */ | |
4792 | if (auto_saving && (!EQ (Vauto_save_file_format, Qt))) | |
4793 | p = Vauto_save_file_format; | |
4794 | else | |
4795 | p = current_buffer->file_format; | |
4796 | while (!NILP (p)) | |
4797 | { | |
4798 | struct buffer *given_buffer = current_buffer; | |
4799 | Vwrite_region_annotations_so_far = annotations; | |
0a20b684 RS |
4800 | res = call4 (Qformat_annotate_function, Fcar (p), start, end, |
4801 | original_buffer); | |
0d420e88 BG |
4802 | if (current_buffer != given_buffer) |
4803 | { | |
3cf29f61 RS |
4804 | XSETFASTINT (start, BEGV); |
4805 | XSETFASTINT (end, ZV); | |
0d420e88 BG |
4806 | annotations = Qnil; |
4807 | } | |
4808 | Flength (res); | |
4809 | annotations = merge (annotations, res, Qcar_less_than_car); | |
4810 | p = Fcdr (p); | |
4811 | } | |
6fdaa9a0 KH |
4812 | |
4813 | /* At last, do the same for the function PRE_WRITE_CONVERSION | |
4814 | implied by the current coding-system. */ | |
4815 | if (!NILP (pre_write_conversion)) | |
4816 | { | |
4817 | struct buffer *given_buffer = current_buffer; | |
4818 | Vwrite_region_annotations_so_far = annotations; | |
4819 | res = call2 (pre_write_conversion, start, end); | |
6fdaa9a0 | 4820 | Flength (res); |
cdfb0f1d KH |
4821 | annotations = (current_buffer != given_buffer |
4822 | ? res | |
4823 | : merge (annotations, res, Qcar_less_than_car)); | |
6fdaa9a0 KH |
4824 | } |
4825 | ||
d6a3cc15 RS |
4826 | UNGCPRO; |
4827 | return annotations; | |
4828 | } | |
ec7adf26 RS |
4829 | \f |
4830 | /* Write to descriptor DESC the NBYTES bytes starting at ADDR, | |
4831 | assuming they start at byte position BYTEPOS in the buffer. | |
d6a3cc15 | 4832 | Intersperse with them the annotations from *ANNOT |
ec7adf26 | 4833 | which fall within the range of byte positions BYTEPOS to BYTEPOS + NBYTES, |
d6a3cc15 RS |
4834 | each at its appropriate position. |
4835 | ||
ec7adf26 RS |
4836 | We modify *ANNOT by discarding elements as we use them up. |
4837 | ||
d6a3cc15 RS |
4838 | The return value is negative in case of system call failure. */ |
4839 | ||
ec7adf26 RS |
4840 | static int |
4841 | a_write (desc, addr, nbytes, bytepos, annot, coding) | |
d6a3cc15 RS |
4842 | int desc; |
4843 | register char *addr; | |
ec7adf26 RS |
4844 | register int nbytes; |
4845 | int bytepos; | |
d6a3cc15 | 4846 | Lisp_Object *annot; |
6fdaa9a0 | 4847 | struct coding_system *coding; |
d6a3cc15 RS |
4848 | { |
4849 | Lisp_Object tem; | |
4850 | int nextpos; | |
ec7adf26 | 4851 | int lastpos = bytepos + nbytes; |
d6a3cc15 | 4852 | |
eb15aa18 | 4853 | while (NILP (*annot) || CONSP (*annot)) |
d6a3cc15 RS |
4854 | { |
4855 | tem = Fcar_safe (Fcar (*annot)); | |
55a7907f | 4856 | nextpos = bytepos - 1; |
ec7adf26 RS |
4857 | if (INTEGERP (tem)) |
4858 | nextpos = CHAR_TO_BYTE (XFASTINT (tem)); | |
4859 | ||
4860 | /* If there are no more annotations in this range, | |
4861 | output the rest of the range all at once. */ | |
4862 | if (! (nextpos >= bytepos && nextpos <= lastpos)) | |
4863 | return e_write (desc, addr, lastpos - bytepos, coding); | |
4864 | ||
4865 | /* Output buffer text up to the next annotation's position. */ | |
4866 | if (nextpos > bytepos) | |
d6a3cc15 | 4867 | { |
ec7adf26 | 4868 | if (0 > e_write (desc, addr, nextpos - bytepos, coding)) |
d6a3cc15 | 4869 | return -1; |
ec7adf26 RS |
4870 | addr += nextpos - bytepos; |
4871 | bytepos = nextpos; | |
d6a3cc15 | 4872 | } |
ec7adf26 | 4873 | /* Output the annotation. */ |
d6a3cc15 RS |
4874 | tem = Fcdr (Fcar (*annot)); |
4875 | if (STRINGP (tem)) | |
4876 | { | |
fc932ac6 | 4877 | if (0 > e_write (desc, XSTRING (tem)->data, STRING_BYTES (XSTRING (tem)), |
6fdaa9a0 | 4878 | coding)) |
d6a3cc15 RS |
4879 | return -1; |
4880 | } | |
4881 | *annot = Fcdr (*annot); | |
4882 | } | |
dfcf069d | 4883 | return 0; |
d6a3cc15 RS |
4884 | } |
4885 | ||
6fdaa9a0 KH |
4886 | #ifndef WRITE_BUF_SIZE |
4887 | #define WRITE_BUF_SIZE (16 * 1024) | |
4888 | #endif | |
4889 | ||
ec7adf26 RS |
4890 | /* Write NBYTES bytes starting at ADDR into descriptor DESC, |
4891 | encoding them with coding system CODING. */ | |
4892 | ||
4893 | static int | |
4894 | e_write (desc, addr, nbytes, coding) | |
570d7624 JB |
4895 | int desc; |
4896 | register char *addr; | |
ec7adf26 | 4897 | register int nbytes; |
6fdaa9a0 | 4898 | struct coding_system *coding; |
570d7624 | 4899 | { |
6fdaa9a0 | 4900 | char buf[WRITE_BUF_SIZE]; |
570d7624 | 4901 | |
6fdaa9a0 KH |
4902 | /* We used to have a code for handling selective display here. But, |
4903 | now it is handled within encode_coding. */ | |
4904 | while (1) | |
570d7624 | 4905 | { |
b4132433 KH |
4906 | int result; |
4907 | ||
4908 | result = encode_coding (coding, addr, buf, nbytes, WRITE_BUF_SIZE); | |
c8a6d68a KH |
4909 | nbytes -= coding->consumed, addr += coding->consumed; |
4910 | if (coding->produced > 0) | |
6fdaa9a0 | 4911 | { |
c8a6d68a KH |
4912 | coding->produced -= write (desc, buf, coding->produced); |
4913 | if (coding->produced) return -1; | |
570d7624 | 4914 | } |
b4132433 KH |
4915 | if (result == CODING_FINISH_INSUFFICIENT_SRC) |
4916 | { | |
4917 | /* The source text ends by an incomplete multibyte form. | |
4918 | There's no way other than write it out as is. */ | |
4919 | nbytes -= write (desc, addr, nbytes); | |
4920 | if (nbytes) return -1; | |
4921 | } | |
ec7adf26 | 4922 | if (nbytes <= 0) |
6fdaa9a0 | 4923 | break; |
570d7624 JB |
4924 | } |
4925 | return 0; | |
4926 | } | |
ec7adf26 | 4927 | \f |
570d7624 JB |
4928 | DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime, |
4929 | Sverify_visited_file_modtime, 1, 1, 0, | |
4930 | "Return t if last mod time of BUF's visited file matches what BUF records.\n\ | |
4931 | This means that the file has not been changed since it was visited or saved.") | |
4932 | (buf) | |
4933 | Lisp_Object buf; | |
4934 | { | |
4935 | struct buffer *b; | |
4936 | struct stat st; | |
32f4334d | 4937 | Lisp_Object handler; |
b1d1b865 | 4938 | Lisp_Object filename; |
570d7624 JB |
4939 | |
4940 | CHECK_BUFFER (buf, 0); | |
4941 | b = XBUFFER (buf); | |
4942 | ||
93c30b5f | 4943 | if (!STRINGP (b->filename)) return Qt; |
570d7624 JB |
4944 | if (b->modtime == 0) return Qt; |
4945 | ||
32f4334d RS |
4946 | /* If the file name has special constructs in it, |
4947 | call the corresponding file handler. */ | |
49307295 KH |
4948 | handler = Ffind_file_name_handler (b->filename, |
4949 | Qverify_visited_file_modtime); | |
32f4334d | 4950 | if (!NILP (handler)) |
09121adc | 4951 | return call2 (handler, Qverify_visited_file_modtime, buf); |
32f4334d | 4952 | |
b1d1b865 RS |
4953 | filename = ENCODE_FILE (b->filename); |
4954 | ||
4955 | if (stat (XSTRING (filename)->data, &st) < 0) | |
570d7624 JB |
4956 | { |
4957 | /* If the file doesn't exist now and didn't exist before, | |
4958 | we say that it isn't modified, provided the error is a tame one. */ | |
4959 | if (errno == ENOENT || errno == EACCES || errno == ENOTDIR) | |
4960 | st.st_mtime = -1; | |
4961 | else | |
4962 | st.st_mtime = 0; | |
4963 | } | |
4964 | if (st.st_mtime == b->modtime | |
4965 | /* If both are positive, accept them if they are off by one second. */ | |
4966 | || (st.st_mtime > 0 && b->modtime > 0 | |
4967 | && (st.st_mtime == b->modtime + 1 | |
4968 | || st.st_mtime == b->modtime - 1))) | |
4969 | return Qt; | |
4970 | return Qnil; | |
4971 | } | |
4972 | ||
4973 | DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, | |
4974 | Sclear_visited_file_modtime, 0, 0, 0, | |
4975 | "Clear out records of last mod time of visited file.\n\ | |
4976 | Next attempt to save will certainly not complain of a discrepancy.") | |
4977 | () | |
4978 | { | |
4979 | current_buffer->modtime = 0; | |
4980 | return Qnil; | |
4981 | } | |
4982 | ||
f5d5eccf RS |
4983 | DEFUN ("visited-file-modtime", Fvisited_file_modtime, |
4984 | Svisited_file_modtime, 0, 0, 0, | |
4985 | "Return the current buffer's recorded visited file modification time.\n\ | |
4986 | The value is a list of the form (HIGH . LOW), like the time values\n\ | |
4987 | that `file-attributes' returns.") | |
4988 | () | |
4989 | { | |
b50536bb | 4990 | return long_to_cons ((unsigned long) current_buffer->modtime); |
f5d5eccf RS |
4991 | } |
4992 | ||
570d7624 | 4993 | DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, |
f5d5eccf | 4994 | Sset_visited_file_modtime, 0, 1, 0, |
570d7624 JB |
4995 | "Update buffer's recorded modification time from the visited file's time.\n\ |
4996 | Useful if the buffer was not read from the file normally\n\ | |
f5d5eccf RS |
4997 | or if the file itself has been changed for some known benign reason.\n\ |
4998 | An argument specifies the modification time value to use\n\ | |
4999 | \(instead of that of the visited file), in the form of a list\n\ | |
5000 | \(HIGH . LOW) or (HIGH LOW).") | |
5001 | (time_list) | |
5002 | Lisp_Object time_list; | |
570d7624 | 5003 | { |
f5d5eccf RS |
5004 | if (!NILP (time_list)) |
5005 | current_buffer->modtime = cons_to_long (time_list); | |
5006 | else | |
5007 | { | |
5008 | register Lisp_Object filename; | |
5009 | struct stat st; | |
5010 | Lisp_Object handler; | |
570d7624 | 5011 | |
f5d5eccf | 5012 | filename = Fexpand_file_name (current_buffer->filename, Qnil); |
32f4334d | 5013 | |
f5d5eccf RS |
5014 | /* If the file name has special constructs in it, |
5015 | call the corresponding file handler. */ | |
49307295 | 5016 | handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime); |
f5d5eccf | 5017 | if (!NILP (handler)) |
caf3c431 | 5018 | /* The handler can find the file name the same way we did. */ |
76c881b0 | 5019 | return call2 (handler, Qset_visited_file_modtime, Qnil); |
b1d1b865 RS |
5020 | |
5021 | filename = ENCODE_FILE (filename); | |
5022 | ||
5023 | if (stat (XSTRING (filename)->data, &st) >= 0) | |
f5d5eccf RS |
5024 | current_buffer->modtime = st.st_mtime; |
5025 | } | |
570d7624 JB |
5026 | |
5027 | return Qnil; | |
5028 | } | |
5029 | \f | |
5030 | Lisp_Object | |
5031 | auto_save_error () | |
5032 | { | |
570d7624 | 5033 | ring_bell (); |
60d67b83 | 5034 | message_with_string ("Autosaving...error for %s", current_buffer->name, 1); |
de49a6d3 | 5035 | Fsleep_for (make_number (1), Qnil); |
60d67b83 | 5036 | message_with_string ("Autosaving...error for %s", current_buffer->name, 0); |
de49a6d3 | 5037 | Fsleep_for (make_number (1), Qnil); |
60d67b83 | 5038 | message_with_string ("Autosaving...error for %s", current_buffer->name, 0); |
de49a6d3 | 5039 | Fsleep_for (make_number (1), Qnil); |
570d7624 JB |
5040 | return Qnil; |
5041 | } | |
5042 | ||
5043 | Lisp_Object | |
5044 | auto_save_1 () | |
5045 | { | |
5046 | unsigned char *fn; | |
5047 | struct stat st; | |
5048 | ||
5049 | /* Get visited file's mode to become the auto save file's mode. */ | |
5050 | if (stat (XSTRING (current_buffer->filename)->data, &st) >= 0) | |
5051 | /* But make sure we can overwrite it later! */ | |
5052 | auto_save_mode_bits = st.st_mode | 0600; | |
5053 | else | |
5054 | auto_save_mode_bits = 0666; | |
5055 | ||
5056 | return | |
5057 | Fwrite_region (Qnil, Qnil, | |
5058 | current_buffer->auto_save_file_name, | |
de1d0127 | 5059 | Qnil, Qlambda, Qnil, Qnil); |
570d7624 JB |
5060 | } |
5061 | ||
e54d3b5d | 5062 | static Lisp_Object |
1b335d29 RS |
5063 | do_auto_save_unwind (stream) /* used as unwind-protect function */ |
5064 | Lisp_Object stream; | |
e54d3b5d | 5065 | { |
3be3c08e | 5066 | auto_saving = 0; |
1b335d29 RS |
5067 | if (!NILP (stream)) |
5068 | fclose ((FILE *) (XFASTINT (XCONS (stream)->car) << 16 | |
5069 | | XFASTINT (XCONS (stream)->cdr))); | |
e54d3b5d RS |
5070 | return Qnil; |
5071 | } | |
5072 | ||
a8c828be RS |
5073 | static Lisp_Object |
5074 | do_auto_save_unwind_1 (value) /* used as unwind-protect function */ | |
5075 | Lisp_Object value; | |
5076 | { | |
5077 | minibuffer_auto_raise = XINT (value); | |
5078 | return Qnil; | |
5079 | } | |
5080 | ||
570d7624 JB |
5081 | DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "", |
5082 | "Auto-save all buffers that need it.\n\ | |
5083 | This is all buffers that have auto-saving enabled\n\ | |
5084 | and are changed since last auto-saved.\n\ | |
5085 | Auto-saving writes the buffer into a file\n\ | |
5086 | so that your editing is not lost if the system crashes.\n\ | |
012d4cdc RS |
5087 | This file is not the file you visited; that changes only when you save.\n\ |
5088 | Normally we run the normal hook `auto-save-hook' before saving.\n\n\ | |
3b7f6e60 EN |
5089 | A non-nil NO-MESSAGE argument means do not print any message if successful.\n\ |
5090 | A non-nil CURRENT-ONLY argument means save only current buffer.") | |
17857782 JB |
5091 | (no_message, current_only) |
5092 | Lisp_Object no_message, current_only; | |
570d7624 JB |
5093 | { |
5094 | struct buffer *old = current_buffer, *b; | |
5095 | Lisp_Object tail, buf; | |
5096 | int auto_saved = 0; | |
f14b1c68 | 5097 | int do_handled_files; |
ff4c9993 | 5098 | Lisp_Object oquit; |
1b335d29 RS |
5099 | FILE *stream; |
5100 | Lisp_Object lispstream; | |
e54d3b5d RS |
5101 | int count = specpdl_ptr - specpdl; |
5102 | int *ptr; | |
a8c828be | 5103 | int orig_minibuffer_auto_raise = minibuffer_auto_raise; |
c71106e5 | 5104 | int message_p = push_message (); |
9c856db9 | 5105 | |
ff4c9993 RS |
5106 | /* Ordinarily don't quit within this function, |
5107 | but don't make it impossible to quit (in case we get hung in I/O). */ | |
5108 | oquit = Vquit_flag; | |
5109 | Vquit_flag = Qnil; | |
570d7624 JB |
5110 | |
5111 | /* No GCPRO needed, because (when it matters) all Lisp_Object variables | |
5112 | point to non-strings reached from Vbuffer_alist. */ | |
5113 | ||
570d7624 | 5114 | if (minibuf_level) |
17857782 | 5115 | no_message = Qt; |
570d7624 | 5116 | |
265a9e55 | 5117 | if (!NILP (Vrun_hooks)) |
570d7624 JB |
5118 | call1 (Vrun_hooks, intern ("auto-save-hook")); |
5119 | ||
e54d3b5d RS |
5120 | if (STRINGP (Vauto_save_list_file_name)) |
5121 | { | |
258fd2cb RS |
5122 | Lisp_Object listfile; |
5123 | listfile = Fexpand_file_name (Vauto_save_list_file_name, Qnil); | |
1b335d29 | 5124 | stream = fopen (XSTRING (listfile)->data, "w"); |
0eff1f85 RS |
5125 | if (stream != NULL) |
5126 | { | |
5127 | /* Arrange to close that file whether or not we get an error. | |
5128 | Also reset auto_saving to 0. */ | |
5129 | lispstream = Fcons (Qnil, Qnil); | |
5130 | XSETFASTINT (XCONS (lispstream)->car, (EMACS_UINT)stream >> 16); | |
5131 | XSETFASTINT (XCONS (lispstream)->cdr, (EMACS_UINT)stream & 0xffff); | |
5132 | } | |
5133 | else | |
5134 | lispstream = Qnil; | |
e54d3b5d RS |
5135 | } |
5136 | else | |
1b335d29 RS |
5137 | { |
5138 | stream = NULL; | |
5139 | lispstream = Qnil; | |
5140 | } | |
199607e4 | 5141 | |
1b335d29 | 5142 | record_unwind_protect (do_auto_save_unwind, lispstream); |
a8c828be RS |
5143 | record_unwind_protect (do_auto_save_unwind_1, |
5144 | make_number (minibuffer_auto_raise)); | |
5145 | minibuffer_auto_raise = 0; | |
3be3c08e RS |
5146 | auto_saving = 1; |
5147 | ||
f14b1c68 JB |
5148 | /* First, save all files which don't have handlers. If Emacs is |
5149 | crashing, the handlers may tweak what is causing Emacs to crash | |
5150 | in the first place, and it would be a shame if Emacs failed to | |
5151 | autosave perfectly ordinary files because it couldn't handle some | |
5152 | ange-ftp'd file. */ | |
5153 | for (do_handled_files = 0; do_handled_files < 2; do_handled_files++) | |
41d86b13 | 5154 | for (tail = Vbuffer_alist; GC_CONSP (tail); tail = XCONS (tail)->cdr) |
f14b1c68 JB |
5155 | { |
5156 | buf = XCONS (XCONS (tail)->car)->cdr; | |
5157 | b = XBUFFER (buf); | |
199607e4 | 5158 | |
e54d3b5d | 5159 | /* Record all the buffers that have auto save mode |
258fd2cb RS |
5160 | in the special file that lists them. For each of these buffers, |
5161 | Record visited name (if any) and auto save name. */ | |
93c30b5f | 5162 | if (STRINGP (b->auto_save_file_name) |
1b335d29 | 5163 | && stream != NULL && do_handled_files == 0) |
e54d3b5d | 5164 | { |
258fd2cb RS |
5165 | if (!NILP (b->filename)) |
5166 | { | |
1b335d29 | 5167 | fwrite (XSTRING (b->filename)->data, 1, |
fc932ac6 | 5168 | STRING_BYTES (XSTRING (b->filename)), stream); |
258fd2cb | 5169 | } |
1b335d29 RS |
5170 | putc ('\n', stream); |
5171 | fwrite (XSTRING (b->auto_save_file_name)->data, 1, | |
fc932ac6 | 5172 | STRING_BYTES (XSTRING (b->auto_save_file_name)), stream); |
1b335d29 | 5173 | putc ('\n', stream); |
e54d3b5d | 5174 | } |
17857782 | 5175 | |
f14b1c68 JB |
5176 | if (!NILP (current_only) |
5177 | && b != current_buffer) | |
5178 | continue; | |
e54d3b5d | 5179 | |
95385625 RS |
5180 | /* Don't auto-save indirect buffers. |
5181 | The base buffer takes care of it. */ | |
5182 | if (b->base_buffer) | |
5183 | continue; | |
5184 | ||
f14b1c68 JB |
5185 | /* Check for auto save enabled |
5186 | and file changed since last auto save | |
5187 | and file changed since last real save. */ | |
93c30b5f | 5188 | if (STRINGP (b->auto_save_file_name) |
95385625 | 5189 | && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b) |
f14b1c68 | 5190 | && b->auto_save_modified < BUF_MODIFF (b) |
82c2d839 RS |
5191 | /* -1 means we've turned off autosaving for a while--see below. */ |
5192 | && XINT (b->save_length) >= 0 | |
f14b1c68 | 5193 | && (do_handled_files |
49307295 KH |
5194 | || NILP (Ffind_file_name_handler (b->auto_save_file_name, |
5195 | Qwrite_region)))) | |
f14b1c68 | 5196 | { |
b60247d9 RS |
5197 | EMACS_TIME before_time, after_time; |
5198 | ||
5199 | EMACS_GET_TIME (before_time); | |
5200 | ||
5201 | /* If we had a failure, don't try again for 20 minutes. */ | |
5202 | if (b->auto_save_failure_time >= 0 | |
5203 | && EMACS_SECS (before_time) - b->auto_save_failure_time < 1200) | |
5204 | continue; | |
5205 | ||
f14b1c68 JB |
5206 | if ((XFASTINT (b->save_length) * 10 |
5207 | > (BUF_Z (b) - BUF_BEG (b)) * 13) | |
5208 | /* A short file is likely to change a large fraction; | |
5209 | spare the user annoying messages. */ | |
5210 | && XFASTINT (b->save_length) > 5000 | |
5211 | /* These messages are frequent and annoying for `*mail*'. */ | |
5212 | && !EQ (b->filename, Qnil) | |
5213 | && NILP (no_message)) | |
5214 | { | |
5215 | /* It has shrunk too much; turn off auto-saving here. */ | |
a8c828be | 5216 | minibuffer_auto_raise = orig_minibuffer_auto_raise; |
60d67b83 RS |
5217 | message_with_string ("Buffer %s has shrunk a lot; auto save turned off there", |
5218 | b->name, 1); | |
a8c828be | 5219 | minibuffer_auto_raise = 0; |
82c2d839 RS |
5220 | /* Turn off auto-saving until there's a real save, |
5221 | and prevent any more warnings. */ | |
46283abe | 5222 | XSETINT (b->save_length, -1); |
f14b1c68 JB |
5223 | Fsleep_for (make_number (1), Qnil); |
5224 | continue; | |
5225 | } | |
5226 | set_buffer_internal (b); | |
5227 | if (!auto_saved && NILP (no_message)) | |
5228 | message1 ("Auto-saving..."); | |
5229 | internal_condition_case (auto_save_1, Qt, auto_save_error); | |
5230 | auto_saved++; | |
5231 | b->auto_save_modified = BUF_MODIFF (b); | |
2acfd7ae | 5232 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
f14b1c68 | 5233 | set_buffer_internal (old); |
b60247d9 RS |
5234 | |
5235 | EMACS_GET_TIME (after_time); | |
5236 | ||
5237 | /* If auto-save took more than 60 seconds, | |
5238 | assume it was an NFS failure that got a timeout. */ | |
5239 | if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60) | |
5240 | b->auto_save_failure_time = EMACS_SECS (after_time); | |
f14b1c68 JB |
5241 | } |
5242 | } | |
570d7624 | 5243 | |
b67f2ca5 RS |
5244 | /* Prevent another auto save till enough input events come in. */ |
5245 | record_auto_save (); | |
570d7624 | 5246 | |
17857782 | 5247 | if (auto_saved && NILP (no_message)) |
f05b275b | 5248 | { |
c71106e5 | 5249 | if (message_p) |
31f3d831 | 5250 | { |
22e59fa7 | 5251 | sit_for (1, 0, 0, 0, 0); |
c71106e5 | 5252 | restore_message (); |
31f3d831 | 5253 | } |
f05b275b KH |
5254 | else |
5255 | message1 ("Auto-saving...done"); | |
5256 | } | |
570d7624 | 5257 | |
ff4c9993 RS |
5258 | Vquit_flag = oquit; |
5259 | ||
c71106e5 | 5260 | pop_message (); |
e54d3b5d | 5261 | unbind_to (count, Qnil); |
570d7624 JB |
5262 | return Qnil; |
5263 | } | |
5264 | ||
5265 | DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved, | |
5266 | Sset_buffer_auto_saved, 0, 0, 0, | |
5267 | "Mark current buffer as auto-saved with its current text.\n\ | |
5268 | No auto-save file will be written until the buffer changes again.") | |
5269 | () | |
5270 | { | |
5271 | current_buffer->auto_save_modified = MODIFF; | |
2acfd7ae | 5272 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
b60247d9 RS |
5273 | current_buffer->auto_save_failure_time = -1; |
5274 | return Qnil; | |
5275 | } | |
5276 | ||
5277 | DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, | |
5278 | Sclear_buffer_auto_save_failure, 0, 0, 0, | |
5279 | "Clear any record of a recent auto-save failure in the current buffer.") | |
5280 | () | |
5281 | { | |
5282 | current_buffer->auto_save_failure_time = -1; | |
570d7624 JB |
5283 | return Qnil; |
5284 | } | |
5285 | ||
5286 | DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p, | |
5287 | 0, 0, 0, | |
5288 | "Return t if buffer has been auto-saved since last read in or saved.") | |
5289 | () | |
5290 | { | |
95385625 | 5291 | return (SAVE_MODIFF < current_buffer->auto_save_modified) ? Qt : Qnil; |
570d7624 JB |
5292 | } |
5293 | \f | |
5294 | /* Reading and completing file names */ | |
5295 | extern Lisp_Object Ffile_name_completion (), Ffile_name_all_completions (); | |
5296 | ||
6e710ae5 RS |
5297 | /* In the string VAL, change each $ to $$ and return the result. */ |
5298 | ||
5299 | static Lisp_Object | |
5300 | double_dollars (val) | |
5301 | Lisp_Object val; | |
5302 | { | |
5303 | register unsigned char *old, *new; | |
5304 | register int n; | |
5305 | int osize, count; | |
5306 | ||
fc932ac6 | 5307 | osize = STRING_BYTES (XSTRING (val)); |
60d67b83 RS |
5308 | |
5309 | /* Count the number of $ characters. */ | |
6e710ae5 RS |
5310 | for (n = osize, count = 0, old = XSTRING (val)->data; n > 0; n--) |
5311 | if (*old++ == '$') count++; | |
5312 | if (count > 0) | |
5313 | { | |
5314 | old = XSTRING (val)->data; | |
60d67b83 RS |
5315 | val = make_uninit_multibyte_string (XSTRING (val)->size + count, |
5316 | osize + count); | |
6e710ae5 RS |
5317 | new = XSTRING (val)->data; |
5318 | for (n = osize; n > 0; n--) | |
5319 | if (*old != '$') | |
5320 | *new++ = *old++; | |
5321 | else | |
5322 | { | |
5323 | *new++ = '$'; | |
5324 | *new++ = '$'; | |
5325 | old++; | |
5326 | } | |
5327 | } | |
5328 | return val; | |
5329 | } | |
5330 | ||
570d7624 JB |
5331 | DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal, |
5332 | 3, 3, 0, | |
5333 | "Internal subroutine for read-file-name. Do not call this.") | |
5334 | (string, dir, action) | |
5335 | Lisp_Object string, dir, action; | |
5336 | /* action is nil for complete, t for return list of completions, | |
5337 | lambda for verify final value */ | |
5338 | { | |
5339 | Lisp_Object name, specdir, realdir, val, orig_string; | |
09121adc | 5340 | int changed; |
8ce069f5 | 5341 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
09121adc | 5342 | |
58cc3710 RS |
5343 | CHECK_STRING (string, 0); |
5344 | ||
09121adc RS |
5345 | realdir = dir; |
5346 | name = string; | |
5347 | orig_string = Qnil; | |
5348 | specdir = Qnil; | |
5349 | changed = 0; | |
5350 | /* No need to protect ACTION--we only compare it with t and nil. */ | |
8ce069f5 | 5351 | GCPRO5 (string, realdir, name, specdir, orig_string); |
570d7624 JB |
5352 | |
5353 | if (XSTRING (string)->size == 0) | |
5354 | { | |
570d7624 | 5355 | if (EQ (action, Qlambda)) |
09121adc RS |
5356 | { |
5357 | UNGCPRO; | |
5358 | return Qnil; | |
5359 | } | |
570d7624 JB |
5360 | } |
5361 | else | |
5362 | { | |
5363 | orig_string = string; | |
5364 | string = Fsubstitute_in_file_name (string); | |
09121adc | 5365 | changed = NILP (Fstring_equal (string, orig_string)); |
570d7624 | 5366 | name = Ffile_name_nondirectory (string); |
09121adc RS |
5367 | val = Ffile_name_directory (string); |
5368 | if (! NILP (val)) | |
5369 | realdir = Fexpand_file_name (val, realdir); | |
570d7624 JB |
5370 | } |
5371 | ||
265a9e55 | 5372 | if (NILP (action)) |
570d7624 JB |
5373 | { |
5374 | specdir = Ffile_name_directory (string); | |
5375 | val = Ffile_name_completion (name, realdir); | |
09121adc | 5376 | UNGCPRO; |
93c30b5f | 5377 | if (!STRINGP (val)) |
570d7624 | 5378 | { |
09121adc | 5379 | if (changed) |
dbd04e01 | 5380 | return double_dollars (string); |
09121adc | 5381 | return val; |
570d7624 JB |
5382 | } |
5383 | ||
265a9e55 | 5384 | if (!NILP (specdir)) |
570d7624 JB |
5385 | val = concat2 (specdir, val); |
5386 | #ifndef VMS | |
6e710ae5 RS |
5387 | return double_dollars (val); |
5388 | #else /* not VMS */ | |
09121adc | 5389 | return val; |
6e710ae5 | 5390 | #endif /* not VMS */ |
570d7624 | 5391 | } |
09121adc | 5392 | UNGCPRO; |
570d7624 JB |
5393 | |
5394 | if (EQ (action, Qt)) | |
5395 | return Ffile_name_all_completions (name, realdir); | |
5396 | /* Only other case actually used is ACTION = lambda */ | |
5397 | #ifdef VMS | |
5398 | /* Supposedly this helps commands such as `cd' that read directory names, | |
5399 | but can someone explain how it helps them? -- RMS */ | |
5400 | if (XSTRING (name)->size == 0) | |
5401 | return Qt; | |
5402 | #endif /* VMS */ | |
5403 | return Ffile_exists_p (string); | |
5404 | } | |
5405 | ||
5406 | DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 5, 0, | |
5407 | "Read file name, prompting with PROMPT and completing in directory DIR.\n\ | |
5408 | Value is not expanded---you must call `expand-file-name' yourself.\n\ | |
3b7f6e60 EN |
5409 | Default name to DEFAULT-FILENAME if user enters a null string.\n\ |
5410 | (If DEFAULT-FILENAME is omitted, the visited file name is used,\n\ | |
3beeedfe | 5411 | except that if INITIAL is specified, that combined with DIR is used.)\n\ |
570d7624 JB |
5412 | Fourth arg MUSTMATCH non-nil means require existing file's name.\n\ |
5413 | Non-nil and non-t means also require confirmation after completion.\n\ | |
5414 | Fifth arg INITIAL specifies text to start with.\n\ | |
5415 | DIR defaults to current buffer's directory default.") | |
3b7f6e60 EN |
5416 | (prompt, dir, default_filename, mustmatch, initial) |
5417 | Lisp_Object prompt, dir, default_filename, mustmatch, initial; | |
570d7624 | 5418 | { |
8d6d9fef | 5419 | Lisp_Object val, insdef, tem; |
570d7624 JB |
5420 | struct gcpro gcpro1, gcpro2; |
5421 | register char *homedir; | |
62f555a5 RS |
5422 | int replace_in_history = 0; |
5423 | int add_to_history = 0; | |
570d7624 JB |
5424 | int count; |
5425 | ||
265a9e55 | 5426 | if (NILP (dir)) |
570d7624 | 5427 | dir = current_buffer->directory; |
3b7f6e60 | 5428 | if (NILP (default_filename)) |
3beeedfe RS |
5429 | { |
5430 | if (! NILP (initial)) | |
3b7f6e60 | 5431 | default_filename = Fexpand_file_name (initial, dir); |
3beeedfe | 5432 | else |
3b7f6e60 | 5433 | default_filename = current_buffer->filename; |
3beeedfe | 5434 | } |
570d7624 JB |
5435 | |
5436 | /* If dir starts with user's homedir, change that to ~. */ | |
5437 | homedir = (char *) egetenv ("HOME"); | |
199607e4 RS |
5438 | #ifdef DOS_NT |
5439 | homedir = strcpy (alloca (strlen (homedir) + 1), homedir); | |
5440 | CORRECT_DIR_SEPS (homedir); | |
5441 | #endif | |
570d7624 | 5442 | if (homedir != 0 |
93c30b5f | 5443 | && STRINGP (dir) |
570d7624 | 5444 | && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir)) |
5e570b75 | 5445 | && IS_DIRECTORY_SEP (XSTRING (dir)->data[strlen (homedir)])) |
570d7624 JB |
5446 | { |
5447 | dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1, | |
fc932ac6 | 5448 | STRING_BYTES (XSTRING (dir)) - strlen (homedir) + 1); |
570d7624 JB |
5449 | XSTRING (dir)->data[0] = '~'; |
5450 | } | |
8d6d9fef AS |
5451 | /* Likewise for default_filename. */ |
5452 | if (homedir != 0 | |
5453 | && STRINGP (default_filename) | |
5454 | && !strncmp (homedir, XSTRING (default_filename)->data, strlen (homedir)) | |
5455 | && IS_DIRECTORY_SEP (XSTRING (default_filename)->data[strlen (homedir)])) | |
5456 | { | |
5457 | default_filename | |
5458 | = make_string (XSTRING (default_filename)->data + strlen (homedir) - 1, | |
5459 | STRING_BYTES (XSTRING (default_filename)) - strlen (homedir) + 1); | |
5460 | XSTRING (default_filename)->data[0] = '~'; | |
5461 | } | |
5462 | if (!NILP (default_filename)) | |
b537a6c7 RS |
5463 | { |
5464 | CHECK_STRING (default_filename, 3); | |
5465 | default_filename = double_dollars (default_filename); | |
5466 | } | |
570d7624 | 5467 | |
58cc3710 | 5468 | if (insert_default_directory && STRINGP (dir)) |
570d7624 JB |
5469 | { |
5470 | insdef = dir; | |
265a9e55 | 5471 | if (!NILP (initial)) |
570d7624 | 5472 | { |
15c65264 | 5473 | Lisp_Object args[2], pos; |
570d7624 JB |
5474 | |
5475 | args[0] = insdef; | |
5476 | args[1] = initial; | |
5477 | insdef = Fconcat (2, args); | |
351bd676 | 5478 | pos = make_number (XSTRING (double_dollars (dir))->size); |
8d6d9fef | 5479 | insdef = Fcons (double_dollars (insdef), pos); |
570d7624 | 5480 | } |
6e710ae5 | 5481 | else |
8d6d9fef | 5482 | insdef = double_dollars (insdef); |
570d7624 | 5483 | } |
58cc3710 | 5484 | else if (STRINGP (initial)) |
8d6d9fef | 5485 | insdef = Fcons (double_dollars (initial), make_number (0)); |
570d7624 | 5486 | else |
8d6d9fef | 5487 | insdef = Qnil; |
570d7624 | 5488 | |
570d7624 | 5489 | count = specpdl_ptr - specpdl; |
a79485af | 5490 | #ifdef VMS |
570d7624 JB |
5491 | specbind (intern ("completion-ignore-case"), Qt); |
5492 | #endif | |
5493 | ||
a79485af RS |
5494 | specbind (intern ("minibuffer-completing-file-name"), Qt); |
5495 | ||
3b7f6e60 | 5496 | GCPRO2 (insdef, default_filename); |
9c856db9 GM |
5497 | |
5498 | #ifdef USE_MOTIF | |
5499 | if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) | |
5500 | && use_dialog_box | |
5501 | && have_menus_p ()) | |
5502 | { | |
5503 | val = Fx_file_dialog (prompt, dir, default_filename, mustmatch); | |
5504 | add_to_history = 1; | |
5505 | } | |
5506 | else | |
5507 | #endif | |
5508 | val = Fcompleting_read (prompt, intern ("read-file-name-internal"), | |
5509 | dir, mustmatch, insdef, | |
5510 | Qfile_name_history, default_filename, Qnil); | |
62f555a5 RS |
5511 | |
5512 | tem = Fsymbol_value (Qfile_name_history); | |
5513 | if (CONSP (tem) && EQ (XCONS (tem)->car, val)) | |
5514 | replace_in_history = 1; | |
5515 | ||
5516 | /* If Fcompleting_read returned the inserted default string itself | |
a8c828be RS |
5517 | (rather than a new string with the same contents), |
5518 | it has to mean that the user typed RET with the minibuffer empty. | |
5519 | In that case, we really want to return "" | |
5520 | so that commands such as set-visited-file-name can distinguish. */ | |
5521 | if (EQ (val, default_filename)) | |
62f555a5 RS |
5522 | { |
5523 | /* In this case, Fcompleting_read has not added an element | |
5524 | to the history. Maybe we should. */ | |
5525 | if (! replace_in_history) | |
5526 | add_to_history = 1; | |
5527 | ||
5528 | val = build_string (""); | |
5529 | } | |
570d7624 | 5530 | |
570d7624 | 5531 | unbind_to (count, Qnil); |
570d7624 | 5532 | UNGCPRO; |
265a9e55 | 5533 | if (NILP (val)) |
570d7624 | 5534 | error ("No file name specified"); |
62f555a5 | 5535 | |
8d6d9fef | 5536 | tem = Fstring_equal (val, CONSP (insdef) ? XCAR (insdef) : insdef); |
62f555a5 | 5537 | |
3b7f6e60 | 5538 | if (!NILP (tem) && !NILP (default_filename)) |
62f555a5 RS |
5539 | val = default_filename; |
5540 | else if (XSTRING (val)->size == 0 && NILP (insdef)) | |
d9bc1c99 | 5541 | { |
3b7f6e60 | 5542 | if (!NILP (default_filename)) |
62f555a5 | 5543 | val = default_filename; |
d9bc1c99 RS |
5544 | else |
5545 | error ("No default file name"); | |
5546 | } | |
62f555a5 | 5547 | val = Fsubstitute_in_file_name (val); |
570d7624 | 5548 | |
62f555a5 RS |
5549 | if (replace_in_history) |
5550 | /* Replace what Fcompleting_read added to the history | |
5551 | with what we will actually return. */ | |
8d6d9fef | 5552 | XCONS (Fsymbol_value (Qfile_name_history))->car = double_dollars (val); |
62f555a5 | 5553 | else if (add_to_history) |
570d7624 | 5554 | { |
62f555a5 RS |
5555 | /* Add the value to the history--but not if it matches |
5556 | the last value already there. */ | |
8d6d9fef | 5557 | Lisp_Object val1 = double_dollars (val); |
62f555a5 | 5558 | tem = Fsymbol_value (Qfile_name_history); |
8d6d9fef | 5559 | if (! CONSP (tem) || NILP (Fequal (XCONS (tem)->car, val1))) |
62f555a5 | 5560 | Fset (Qfile_name_history, |
8d6d9fef | 5561 | Fcons (val1, tem)); |
570d7624 | 5562 | } |
9c856db9 | 5563 | |
62f555a5 | 5564 | return val; |
570d7624 | 5565 | } |
9c856db9 | 5566 | |
570d7624 | 5567 | \f |
dbda5089 GV |
5568 | void |
5569 | init_fileio_once () | |
5570 | { | |
5571 | /* Must be set before any path manipulation is performed. */ | |
5572 | XSETFASTINT (Vdirectory_sep_char, '/'); | |
5573 | } | |
5574 | ||
9c856db9 | 5575 | \f |
dfcf069d | 5576 | void |
570d7624 JB |
5577 | syms_of_fileio () |
5578 | { | |
0bf2eed2 | 5579 | Qexpand_file_name = intern ("expand-file-name"); |
273e0829 | 5580 | Qsubstitute_in_file_name = intern ("substitute-in-file-name"); |
0bf2eed2 RS |
5581 | Qdirectory_file_name = intern ("directory-file-name"); |
5582 | Qfile_name_directory = intern ("file-name-directory"); | |
5583 | Qfile_name_nondirectory = intern ("file-name-nondirectory"); | |
642ef245 | 5584 | Qunhandled_file_name_directory = intern ("unhandled-file-name-directory"); |
0bf2eed2 | 5585 | Qfile_name_as_directory = intern ("file-name-as-directory"); |
32f4334d | 5586 | Qcopy_file = intern ("copy-file"); |
a6e6e718 | 5587 | Qmake_directory_internal = intern ("make-directory-internal"); |
32f4334d RS |
5588 | Qdelete_directory = intern ("delete-directory"); |
5589 | Qdelete_file = intern ("delete-file"); | |
5590 | Qrename_file = intern ("rename-file"); | |
5591 | Qadd_name_to_file = intern ("add-name-to-file"); | |
5592 | Qmake_symbolic_link = intern ("make-symbolic-link"); | |
5593 | Qfile_exists_p = intern ("file-exists-p"); | |
5594 | Qfile_executable_p = intern ("file-executable-p"); | |
5595 | Qfile_readable_p = intern ("file-readable-p"); | |
32f4334d | 5596 | Qfile_writable_p = intern ("file-writable-p"); |
1f8653eb RS |
5597 | Qfile_symlink_p = intern ("file-symlink-p"); |
5598 | Qaccess_file = intern ("access-file"); | |
32f4334d | 5599 | Qfile_directory_p = intern ("file-directory-p"); |
adedc71d | 5600 | Qfile_regular_p = intern ("file-regular-p"); |
32f4334d RS |
5601 | Qfile_accessible_directory_p = intern ("file-accessible-directory-p"); |
5602 | Qfile_modes = intern ("file-modes"); | |
5603 | Qset_file_modes = intern ("set-file-modes"); | |
5604 | Qfile_newer_than_file_p = intern ("file-newer-than-file-p"); | |
5605 | Qinsert_file_contents = intern ("insert-file-contents"); | |
5606 | Qwrite_region = intern ("write-region"); | |
5607 | Qverify_visited_file_modtime = intern ("verify-visited-file-modtime"); | |
3ec46acd | 5608 | Qset_visited_file_modtime = intern ("set-visited-file-modtime"); |
32f4334d | 5609 | |
642ef245 | 5610 | staticpro (&Qexpand_file_name); |
273e0829 | 5611 | staticpro (&Qsubstitute_in_file_name); |
642ef245 JB |
5612 | staticpro (&Qdirectory_file_name); |
5613 | staticpro (&Qfile_name_directory); | |
5614 | staticpro (&Qfile_name_nondirectory); | |
5615 | staticpro (&Qunhandled_file_name_directory); | |
5616 | staticpro (&Qfile_name_as_directory); | |
15c65264 | 5617 | staticpro (&Qcopy_file); |
c34b559d | 5618 | staticpro (&Qmake_directory_internal); |
15c65264 RS |
5619 | staticpro (&Qdelete_directory); |
5620 | staticpro (&Qdelete_file); | |
5621 | staticpro (&Qrename_file); | |
5622 | staticpro (&Qadd_name_to_file); | |
5623 | staticpro (&Qmake_symbolic_link); | |
5624 | staticpro (&Qfile_exists_p); | |
5625 | staticpro (&Qfile_executable_p); | |
5626 | staticpro (&Qfile_readable_p); | |
15c65264 | 5627 | staticpro (&Qfile_writable_p); |
1f8653eb RS |
5628 | staticpro (&Qaccess_file); |
5629 | staticpro (&Qfile_symlink_p); | |
15c65264 | 5630 | staticpro (&Qfile_directory_p); |
adedc71d | 5631 | staticpro (&Qfile_regular_p); |
15c65264 RS |
5632 | staticpro (&Qfile_accessible_directory_p); |
5633 | staticpro (&Qfile_modes); | |
5634 | staticpro (&Qset_file_modes); | |
5635 | staticpro (&Qfile_newer_than_file_p); | |
5636 | staticpro (&Qinsert_file_contents); | |
5637 | staticpro (&Qwrite_region); | |
5638 | staticpro (&Qverify_visited_file_modtime); | |
0a61794b | 5639 | staticpro (&Qset_visited_file_modtime); |
642ef245 JB |
5640 | |
5641 | Qfile_name_history = intern ("file-name-history"); | |
5642 | Fset (Qfile_name_history, Qnil); | |
15c65264 RS |
5643 | staticpro (&Qfile_name_history); |
5644 | ||
570d7624 JB |
5645 | Qfile_error = intern ("file-error"); |
5646 | staticpro (&Qfile_error); | |
199607e4 | 5647 | Qfile_already_exists = intern ("file-already-exists"); |
570d7624 | 5648 | staticpro (&Qfile_already_exists); |
c0b7b21c RS |
5649 | Qfile_date_error = intern ("file-date-error"); |
5650 | staticpro (&Qfile_date_error); | |
570d7624 | 5651 | |
5e570b75 | 5652 | #ifdef DOS_NT |
4c3c22f3 RS |
5653 | Qfind_buffer_file_type = intern ("find-buffer-file-type"); |
5654 | staticpro (&Qfind_buffer_file_type); | |
5e570b75 | 5655 | #endif /* DOS_NT */ |
4c3c22f3 | 5656 | |
b1d1b865 | 5657 | DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system, |
cd913586 KH |
5658 | "*Coding system for encoding file names.\n\ |
5659 | If it is nil, default-file-name-coding-system (which see) is used."); | |
b1d1b865 RS |
5660 | Vfile_name_coding_system = Qnil; |
5661 | ||
cd913586 KH |
5662 | DEFVAR_LISP ("default-file-name-coding-system", |
5663 | &Vdefault_file_name_coding_system, | |
5664 | "Default coding system for encoding file names.\n\ | |
5665 | This variable is used only when file-name-coding-system is nil.\n\ | |
5666 | \n\ | |
5667 | This variable is set/changed by the command set-language-environment.\n\ | |
5668 | User should not set this variable manually,\n\ | |
5669 | instead use file-name-coding-system to get a constant encoding\n\ | |
5670 | of file names regardless of the current language environment."); | |
5671 | Vdefault_file_name_coding_system = Qnil; | |
5672 | ||
0d420e88 | 5673 | DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format, |
824a483f | 5674 | "*Format in which to write auto-save files.\n\ |
0d420e88 BG |
5675 | Should be a list of symbols naming formats that are defined in `format-alist'.\n\ |
5676 | If it is t, which is the default, auto-save files are written in the\n\ | |
5677 | same format as a regular save would use."); | |
5678 | Vauto_save_file_format = Qt; | |
5679 | ||
5680 | Qformat_decode = intern ("format-decode"); | |
5681 | staticpro (&Qformat_decode); | |
5682 | Qformat_annotate_function = intern ("format-annotate-function"); | |
5683 | staticpro (&Qformat_annotate_function); | |
5684 | ||
d6a3cc15 RS |
5685 | Qcar_less_than_car = intern ("car-less-than-car"); |
5686 | staticpro (&Qcar_less_than_car); | |
5687 | ||
570d7624 JB |
5688 | Fput (Qfile_error, Qerror_conditions, |
5689 | Fcons (Qfile_error, Fcons (Qerror, Qnil))); | |
5690 | Fput (Qfile_error, Qerror_message, | |
5691 | build_string ("File error")); | |
5692 | ||
5693 | Fput (Qfile_already_exists, Qerror_conditions, | |
5694 | Fcons (Qfile_already_exists, | |
5695 | Fcons (Qfile_error, Fcons (Qerror, Qnil)))); | |
5696 | Fput (Qfile_already_exists, Qerror_message, | |
5697 | build_string ("File already exists")); | |
5698 | ||
c0b7b21c RS |
5699 | Fput (Qfile_date_error, Qerror_conditions, |
5700 | Fcons (Qfile_date_error, | |
5701 | Fcons (Qfile_error, Fcons (Qerror, Qnil)))); | |
5702 | Fput (Qfile_date_error, Qerror_message, | |
5703 | build_string ("Cannot set file date")); | |
5704 | ||
570d7624 JB |
5705 | DEFVAR_BOOL ("insert-default-directory", &insert_default_directory, |
5706 | "*Non-nil means when reading a filename start with default dir in minibuffer."); | |
5707 | insert_default_directory = 1; | |
5708 | ||
5709 | DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm, | |
5710 | "*Non-nil means write new files with record format `stmlf'.\n\ | |
5711 | nil means use format `var'. This variable is meaningful only on VMS."); | |
5712 | vms_stmlf_recfm = 0; | |
5713 | ||
199607e4 RS |
5714 | DEFVAR_LISP ("directory-sep-char", &Vdirectory_sep_char, |
5715 | "Directory separator character for built-in functions that return file names.\n\ | |
5716 | The value should be either ?/ or ?\\ (any other value is treated as ?\\).\n\ | |
5717 | This variable affects the built-in functions only on Windows,\n\ | |
5718 | on other platforms, it is initialized so that Lisp code can find out\n\ | |
5719 | what the normal separator is."); | |
199607e4 | 5720 | |
1d1826db RS |
5721 | DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist, |
5722 | "*Alist of elements (REGEXP . HANDLER) for file names handled specially.\n\ | |
5723 | If a file name matches REGEXP, then all I/O on that file is done by calling\n\ | |
5724 | HANDLER.\n\ | |
5725 | \n\ | |
5726 | The first argument given to HANDLER is the name of the I/O primitive\n\ | |
5727 | to be handled; the remaining arguments are the arguments that were\n\ | |
5728 | passed to that primitive. For example, if you do\n\ | |
5729 | (file-exists-p FILENAME)\n\ | |
5730 | and FILENAME is handled by HANDLER, then HANDLER is called like this:\n\ | |
642ef245 JB |
5731 | (funcall HANDLER 'file-exists-p FILENAME)\n\ |
5732 | The function `find-file-name-handler' checks this list for a handler\n\ | |
5733 | for its argument."); | |
09121adc RS |
5734 | Vfile_name_handler_alist = Qnil; |
5735 | ||
0414b394 KH |
5736 | DEFVAR_LISP ("set-auto-coding-function", |
5737 | &Vset_auto_coding_function, | |
7fc4808e | 5738 | "If non-nil, a function to call to decide a coding system of file.\n\ |
1255deb9 KH |
5739 | Two arguments are passed to this function: the file name\n\ |
5740 | and the length of a file contents following the point.\n\ | |
5741 | This function should return a coding system to decode the file contents.\n\ | |
5742 | It should check the file name against `auto-coding-alist'.\n\ | |
5743 | If no coding system is decided, it should check a coding system\n\ | |
7fc4808e | 5744 | specified in the heading lines with the format:\n\ |
0414b394 KH |
5745 | -*- ... coding: CODING-SYSTEM; ... -*-\n\ |
5746 | or local variable spec of the tailing lines with `coding:' tag."); | |
5747 | Vset_auto_coding_function = Qnil; | |
c9e82392 | 5748 | |
d6a3cc15 | 5749 | DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions, |
246cfea5 RS |
5750 | "A list of functions to be called at the end of `insert-file-contents'.\n\ |
5751 | Each is passed one argument, the number of bytes inserted. It should return\n\ | |
5752 | the new byte count, and leave point the same. If `insert-file-contents' is\n\ | |
5753 | intercepted by a handler from `file-name-handler-alist', that handler is\n\ | |
d6a3cc15 RS |
5754 | responsible for calling the after-insert-file-functions if appropriate."); |
5755 | Vafter_insert_file_functions = Qnil; | |
5756 | ||
5757 | DEFVAR_LISP ("write-region-annotate-functions", &Vwrite_region_annotate_functions, | |
246cfea5 | 5758 | "A list of functions to be called at the start of `write-region'.\n\ |
568aa585 RS |
5759 | Each is passed two arguments, START and END as for `write-region'.\n\ |
5760 | These are usually two numbers but not always; see the documentation\n\ | |
5761 | for `write-region'. The function should return a list of pairs\n\ | |
5762 | of the form (POSITION . STRING), consisting of strings to be effectively\n\ | |
246cfea5 RS |
5763 | inserted at the specified positions of the file being written (1 means to\n\ |
5764 | insert before the first byte written). The POSITIONs must be sorted into\n\ | |
5765 | increasing order. If there are several functions in the list, the several\n\ | |
d6a3cc15 RS |
5766 | lists are merged destructively."); |
5767 | Vwrite_region_annotate_functions = Qnil; | |
5768 | ||
6fc6f94b RS |
5769 | DEFVAR_LISP ("write-region-annotations-so-far", |
5770 | &Vwrite_region_annotations_so_far, | |
5771 | "When an annotation function is called, this holds the previous annotations.\n\ | |
5772 | These are the annotations made by other annotation functions\n\ | |
5773 | that were already called. See also `write-region-annotate-functions'."); | |
5774 | Vwrite_region_annotations_so_far = Qnil; | |
5775 | ||
82c2d839 | 5776 | DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers, |
268466ed | 5777 | "A list of file name handlers that temporarily should not be used.\n\ |
e3e86241 | 5778 | This applies only to the operation `inhibit-file-name-operation'."); |
82c2d839 RS |
5779 | Vinhibit_file_name_handlers = Qnil; |
5780 | ||
a65970a0 RS |
5781 | DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation, |
5782 | "The operation for which `inhibit-file-name-handlers' is applicable."); | |
5783 | Vinhibit_file_name_operation = Qnil; | |
5784 | ||
e54d3b5d | 5785 | DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name, |
51931aca KH |
5786 | "File name in which we write a list of all auto save file names.\n\ |
5787 | This variable is initialized automatically from `auto-save-list-file-prefix'\n\ | |
5788 | shortly after Emacs reads your `.emacs' file, if you have not yet given it\n\ | |
5789 | a non-nil value."); | |
e54d3b5d RS |
5790 | Vauto_save_list_file_name = Qnil; |
5791 | ||
642ef245 | 5792 | defsubr (&Sfind_file_name_handler); |
570d7624 JB |
5793 | defsubr (&Sfile_name_directory); |
5794 | defsubr (&Sfile_name_nondirectory); | |
642ef245 | 5795 | defsubr (&Sunhandled_file_name_directory); |
570d7624 JB |
5796 | defsubr (&Sfile_name_as_directory); |
5797 | defsubr (&Sdirectory_file_name); | |
5798 | defsubr (&Smake_temp_name); | |
5799 | defsubr (&Sexpand_file_name); | |
5800 | defsubr (&Ssubstitute_in_file_name); | |
5801 | defsubr (&Scopy_file); | |
9bbe01fb | 5802 | defsubr (&Smake_directory_internal); |
aa734e17 | 5803 | defsubr (&Sdelete_directory); |
570d7624 JB |
5804 | defsubr (&Sdelete_file); |
5805 | defsubr (&Srename_file); | |
5806 | defsubr (&Sadd_name_to_file); | |
5807 | #ifdef S_IFLNK | |
5808 | defsubr (&Smake_symbolic_link); | |
5809 | #endif /* S_IFLNK */ | |
5810 | #ifdef VMS | |
5811 | defsubr (&Sdefine_logical_name); | |
5812 | #endif /* VMS */ | |
5813 | #ifdef HPUX_NET | |
5814 | defsubr (&Ssysnetunam); | |
5815 | #endif /* HPUX_NET */ | |
5816 | defsubr (&Sfile_name_absolute_p); | |
5817 | defsubr (&Sfile_exists_p); | |
5818 | defsubr (&Sfile_executable_p); | |
5819 | defsubr (&Sfile_readable_p); | |
5820 | defsubr (&Sfile_writable_p); | |
1f8653eb | 5821 | defsubr (&Saccess_file); |
570d7624 JB |
5822 | defsubr (&Sfile_symlink_p); |
5823 | defsubr (&Sfile_directory_p); | |
b72dea2a | 5824 | defsubr (&Sfile_accessible_directory_p); |
f793dc6c | 5825 | defsubr (&Sfile_regular_p); |
570d7624 JB |
5826 | defsubr (&Sfile_modes); |
5827 | defsubr (&Sset_file_modes); | |
c24e9a53 RS |
5828 | defsubr (&Sset_default_file_modes); |
5829 | defsubr (&Sdefault_file_modes); | |
570d7624 JB |
5830 | defsubr (&Sfile_newer_than_file_p); |
5831 | defsubr (&Sinsert_file_contents); | |
5832 | defsubr (&Swrite_region); | |
d6a3cc15 | 5833 | defsubr (&Scar_less_than_car); |
570d7624 JB |
5834 | defsubr (&Sverify_visited_file_modtime); |
5835 | defsubr (&Sclear_visited_file_modtime); | |
f5d5eccf | 5836 | defsubr (&Svisited_file_modtime); |
570d7624 JB |
5837 | defsubr (&Sset_visited_file_modtime); |
5838 | defsubr (&Sdo_auto_save); | |
5839 | defsubr (&Sset_buffer_auto_saved); | |
b60247d9 | 5840 | defsubr (&Sclear_buffer_auto_save_failure); |
570d7624 JB |
5841 | defsubr (&Srecent_auto_save_p); |
5842 | ||
5843 | defsubr (&Sread_file_name_internal); | |
5844 | defsubr (&Sread_file_name); | |
85ffea93 | 5845 | |
483a2e10 | 5846 | #ifdef unix |
85ffea93 | 5847 | defsubr (&Sunix_sync); |
483a2e10 | 5848 | #endif |
570d7624 | 5849 | } |