0
|
1 /* File IO for XEmacs.
|
|
2 Copyright (C) 1985-1988, 1992-1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Mule 2.0, FSF 19.30. */
|
211
|
23 /* More syncing: FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
|
0
|
24
|
|
25 #include <config.h>
|
|
26 #include "lisp.h"
|
|
27
|
|
28 #include "buffer.h"
|
|
29 #include "events.h"
|
|
30 #include "frame.h"
|
|
31 #include "insdel.h"
|
|
32 #include "lstream.h"
|
|
33 #include "redisplay.h"
|
|
34 #include "sysdep.h"
|
|
35 #include "window.h" /* minibuf_level */
|
259
|
36 #ifdef FILE_CODING
|
|
37 #include "file-coding.h"
|
70
|
38 #endif
|
0
|
39
|
2
|
40 #ifdef HAVE_LIBGEN_H /* Must come before sysfile.h */
|
|
41 #include <libgen.h>
|
|
42 #endif
|
0
|
43 #include "sysfile.h"
|
|
44 #include "sysproc.h"
|
|
45 #include "syspwd.h"
|
|
46 #include "systime.h"
|
|
47 #include "sysdir.h"
|
|
48
|
|
49 #ifdef HPUX
|
|
50 #include <netio.h>
|
|
51 #ifdef HPUX_PRE_8_0
|
|
52 #include <errnet.h>
|
211
|
53 #endif /* HPUX_PRE_8_0 */
|
|
54 #endif /* HPUX */
|
|
55
|
|
56 #ifdef WINDOWSNT
|
|
57 #define NOMINMAX 1
|
|
58 #include <windows.h>
|
|
59 #include <stdlib.h>
|
|
60 #include <fcntl.h>
|
|
61 #endif /* not WINDOWSNT */
|
|
62
|
|
63 #ifdef DOS_NT
|
|
64 #define CORRECT_DIR_SEPS(s) \
|
|
65 do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
|
|
66 else unixtodos_filename (s); \
|
|
67 } while (0)
|
|
68 /* On Windows, drive letters must be alphabetic - on DOS, the Netware
|
|
69 redirector allows the six letters between 'Z' and 'a' as well. */
|
|
70 #ifdef MSDOS
|
|
71 #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z')
|
0
|
72 #endif
|
211
|
73 #ifdef WINDOWSNT
|
|
74 #define IS_DRIVE(x) isalpha (x)
|
|
75 #endif
|
|
76 /* Need to lower-case the drive letter, or else expanded
|
|
77 filenames will sometimes compare inequal, because
|
|
78 `expand-file-name' doesn't always down-case the drive letter. */
|
|
79 #define DRIVE_LETTER(x) (tolower (x))
|
|
80 #endif /* DOS_NT */
|
0
|
81
|
|
82 /* Nonzero during writing of auto-save files */
|
|
83 static int auto_saving;
|
|
84
|
|
85 /* Set by auto_save_1 to mode of original file so Fwrite_region_internal
|
|
86 will create a new file with the same mode as the original */
|
|
87 static int auto_save_mode_bits;
|
|
88
|
151
|
89 /* Alist of elements (REGEXP . HANDLER) for file names
|
0
|
90 whose I/O is done with a special handler. */
|
|
91 Lisp_Object Vfile_name_handler_alist;
|
|
92
|
|
93 /* Format for auto-save files */
|
|
94 Lisp_Object Vauto_save_file_format;
|
|
95
|
|
96 /* Lisp functions for translating file formats */
|
|
97 Lisp_Object Qformat_decode, Qformat_annotate_function;
|
|
98
|
|
99 /* Functions to be called to process text properties in inserted file. */
|
|
100 Lisp_Object Vafter_insert_file_functions;
|
|
101
|
|
102 /* Functions to be called to create text property annotations for file. */
|
|
103 Lisp_Object Vwrite_region_annotate_functions;
|
|
104
|
|
105 /* During build_annotations, each time an annotation function is called,
|
|
106 this holds the annotations made by the previous functions. */
|
|
107 Lisp_Object Vwrite_region_annotations_so_far;
|
|
108
|
|
109 /* File name in which we write a list of all our auto save files. */
|
|
110 Lisp_Object Vauto_save_list_file_name;
|
|
111
|
|
112 int disable_auto_save_when_buffer_shrinks;
|
|
113
|
|
114 Lisp_Object Qfile_name_handler_alist;
|
|
115
|
211
|
116 /* Syncing with FSF 19.34.6 note: although labelled as NT-specific, these
|
|
117 two lisp variables are compiled in even when not defined(DOS_NT).
|
|
118 Need to check if we should bracket them between #ifdef's.
|
|
119 --marcpa */
|
|
120 /* On NT, specifies the directory separator character, used (eg.) when
|
|
121 expanding file names. This can be bound to / or \.
|
|
122
|
|
123 This needs to be initialized statically, because file name functions
|
|
124 are called during initialization. */
|
235
|
125 Lisp_Object Vdirectory_sep_char;
|
|
126
|
|
127 #ifdef DOS_NT
|
|
128 /* Until we can figure out how to deal with the functions in this file in
|
|
129 a civilized fashion, this will remain #ifdef'ed out. -slb */
|
211
|
130 /* For the benefit of backwards compatability with earlier versions of
|
|
131 Emacs on DOS_NT, provide a way to disable the REPLACE option support
|
|
132 in insert-file-contents. */
|
|
133 Lisp_Object Vinsert_file_contents_allow_replace;
|
|
134 #endif /* DOS_NT */
|
|
135
|
0
|
136 /* These variables describe handlers that have "already" had a chance
|
|
137 to handle the current operation.
|
|
138
|
|
139 Vinhibit_file_name_handlers is a list of file name handlers.
|
|
140 Vinhibit_file_name_operation is the operation being handled.
|
|
141 If we try to handle that operation, we ignore those handlers. */
|
|
142
|
|
143 static Lisp_Object Vinhibit_file_name_handlers;
|
|
144 static Lisp_Object Vinhibit_file_name_operation;
|
|
145
|
|
146 Lisp_Object Qfile_error, Qfile_already_exists;
|
|
147
|
|
148 Lisp_Object Qauto_save_hook;
|
|
149 Lisp_Object Qauto_save_error;
|
|
150 Lisp_Object Qauto_saving;
|
|
151
|
|
152 Lisp_Object Qcar_less_than_car;
|
|
153
|
|
154 Lisp_Object Qcompute_buffer_file_truename;
|
|
155
|
|
156 /* signal a file error when errno contains a meaningful value. */
|
|
157
|
|
158 DOESNT_RETURN
|
|
159 report_file_error (CONST char *string, Lisp_Object data)
|
|
160 {
|
114
|
161 /* #### dmoore - This uses current_buffer, better make sure no one
|
|
162 has GC'd the current buffer. File handlers are giving me a headache
|
|
163 maybe I'll just always protect current_buffer around all of those
|
|
164 calls. */
|
|
165
|
0
|
166 signal_error (Qfile_error,
|
|
167 Fcons (build_translated_string (string),
|
151
|
168 Fcons (lisp_strerror (errno), data)));
|
0
|
169 }
|
|
170
|
|
171 void
|
|
172 maybe_report_file_error (CONST char *string, Lisp_Object data,
|
|
173 Lisp_Object class, Error_behavior errb)
|
|
174 {
|
|
175 /* Optimization: */
|
|
176 if (ERRB_EQ (errb, ERROR_ME_NOT))
|
|
177 return;
|
|
178
|
|
179 maybe_signal_error (Qfile_error,
|
|
180 Fcons (build_translated_string (string),
|
151
|
181 Fcons (lisp_strerror (errno), data)),
|
0
|
182 class, errb);
|
|
183 }
|
|
184
|
|
185 /* signal a file error when errno does not contain a meaningful value. */
|
|
186
|
|
187 DOESNT_RETURN
|
|
188 signal_file_error (CONST char *string, Lisp_Object data)
|
|
189 {
|
|
190 signal_error (Qfile_error,
|
|
191 list2 (build_translated_string (string), data));
|
|
192 }
|
|
193
|
|
194 void
|
|
195 maybe_signal_file_error (CONST char *string, Lisp_Object data,
|
|
196 Lisp_Object class, Error_behavior errb)
|
|
197 {
|
|
198 /* Optimization: */
|
|
199 if (ERRB_EQ (errb, ERROR_ME_NOT))
|
|
200 return;
|
|
201 maybe_signal_error (Qfile_error,
|
|
202 list2 (build_translated_string (string), data),
|
|
203 class, errb);
|
|
204 }
|
|
205
|
|
206 DOESNT_RETURN
|
|
207 signal_double_file_error (CONST char *string1, CONST char *string2,
|
|
208 Lisp_Object data)
|
|
209 {
|
|
210 signal_error (Qfile_error,
|
|
211 list3 (build_translated_string (string1),
|
|
212 build_translated_string (string2),
|
|
213 data));
|
|
214 }
|
|
215
|
|
216 void
|
|
217 maybe_signal_double_file_error (CONST char *string1, CONST char *string2,
|
|
218 Lisp_Object data, Lisp_Object class,
|
|
219 Error_behavior errb)
|
|
220 {
|
|
221 /* Optimization: */
|
|
222 if (ERRB_EQ (errb, ERROR_ME_NOT))
|
|
223 return;
|
|
224 maybe_signal_error (Qfile_error,
|
|
225 list3 (build_translated_string (string1),
|
|
226 build_translated_string (string2),
|
|
227 data),
|
|
228 class, errb);
|
|
229 }
|
|
230
|
|
231 DOESNT_RETURN
|
|
232 signal_double_file_error_2 (CONST char *string1, CONST char *string2,
|
|
233 Lisp_Object data1, Lisp_Object data2)
|
|
234 {
|
|
235 signal_error (Qfile_error,
|
151
|
236 list4 (build_translated_string (string1),
|
0
|
237 build_translated_string (string2),
|
|
238 data1, data2));
|
|
239 }
|
|
240
|
|
241 void
|
|
242 maybe_signal_double_file_error_2 (CONST char *string1, CONST char *string2,
|
|
243 Lisp_Object data1, Lisp_Object data2,
|
|
244 Lisp_Object class, Error_behavior errb)
|
|
245 {
|
|
246 /* Optimization: */
|
|
247 if (ERRB_EQ (errb, ERROR_ME_NOT))
|
|
248 return;
|
|
249 maybe_signal_error (Qfile_error,
|
151
|
250 list4 (build_translated_string (string1),
|
0
|
251 build_translated_string (string2),
|
|
252 data1, data2),
|
|
253 class, errb);
|
|
254 }
|
|
255
|
151
|
256
|
|
257 /* Just like strerror(3), except return a lisp string instead of char *.
|
|
258 The string needs to be converted since it may be localized.
|
|
259 Perhaps this should use strerror-coding-system instead? */
|
|
260 Lisp_Object
|
|
261 lisp_strerror (int errnum)
|
|
262 {
|
|
263 return build_ext_string (strerror (errnum), FORMAT_NATIVE);
|
|
264 }
|
|
265
|
0
|
266 static Lisp_Object
|
|
267 close_file_unwind (Lisp_Object fd)
|
|
268 {
|
|
269 if (CONSP (fd))
|
|
270 {
|
|
271 if (INTP (XCAR (fd)))
|
|
272 close (XINT (XCAR (fd)));
|
|
273
|
|
274 free_cons (XCONS (fd));
|
|
275 }
|
|
276 else
|
|
277 close (XINT (fd));
|
|
278
|
|
279 return Qnil;
|
|
280 }
|
|
281
|
|
282 static Lisp_Object
|
|
283 close_stream_unwind (Lisp_Object stream)
|
|
284 {
|
|
285 Lstream_close (XLSTREAM (stream));
|
|
286 return Qnil;
|
|
287 }
|
|
288
|
|
289 /* Restore point, having saved it as a marker. */
|
|
290
|
|
291 static Lisp_Object
|
|
292 restore_point_unwind (Lisp_Object point_marker)
|
|
293 {
|
|
294 BUF_SET_PT (current_buffer, marker_position (point_marker));
|
173
|
295 return Fset_marker (point_marker, Qnil, Qnil);
|
0
|
296 }
|
|
297
|
|
298 /* Versions of read() and write() that allow quitting out of the actual
|
|
299 I/O. We don't use immediate_quit (i.e. direct longjmp() out of the
|
|
300 signal handler) because that's way too losing.
|
|
301
|
|
302 (#### Actually, longjmp()ing out of the signal handler may not be
|
|
303 as losing as I thought. See sys_do_signal() in sysdep.c.)
|
|
304
|
|
305 Solaris include files declare the return value as ssize_t.
|
|
306 Is that standard? */
|
|
307 int
|
|
308 read_allowing_quit (int fildes, void *buf, unsigned int nbyte)
|
|
309 {
|
|
310 int nread;
|
|
311 QUIT;
|
|
312
|
|
313 nread = sys_read_1 (fildes, buf, nbyte, 1);
|
|
314 return nread;
|
|
315 }
|
|
316
|
|
317 int
|
|
318 write_allowing_quit (int fildes, CONST void *buf, unsigned int nbyte)
|
|
319 {
|
|
320 int nread;
|
|
321
|
|
322 QUIT;
|
|
323 nread = sys_write_1 (fildes, buf, nbyte, 1);
|
|
324 return nread;
|
|
325 }
|
|
326
|
|
327
|
|
328 Lisp_Object Qexpand_file_name;
|
|
329 Lisp_Object Qfile_truename;
|
|
330 Lisp_Object Qsubstitute_in_file_name;
|
|
331 Lisp_Object Qdirectory_file_name;
|
|
332 Lisp_Object Qfile_name_directory;
|
|
333 Lisp_Object Qfile_name_nondirectory;
|
|
334 Lisp_Object Qunhandled_file_name_directory;
|
|
335 Lisp_Object Qfile_name_as_directory;
|
|
336 Lisp_Object Qcopy_file;
|
|
337 Lisp_Object Qmake_directory_internal;
|
|
338 Lisp_Object Qdelete_directory;
|
|
339 Lisp_Object Qdelete_file;
|
|
340 Lisp_Object Qrename_file;
|
|
341 Lisp_Object Qadd_name_to_file;
|
|
342 Lisp_Object Qmake_symbolic_link;
|
|
343 Lisp_Object Qfile_exists_p;
|
|
344 Lisp_Object Qfile_executable_p;
|
|
345 Lisp_Object Qfile_readable_p;
|
|
346 Lisp_Object Qfile_symlink_p;
|
|
347 Lisp_Object Qfile_writable_p;
|
|
348 Lisp_Object Qfile_directory_p;
|
|
349 Lisp_Object Qfile_regular_p;
|
|
350 Lisp_Object Qfile_accessible_directory_p;
|
|
351 Lisp_Object Qfile_modes;
|
|
352 Lisp_Object Qset_file_modes;
|
|
353 Lisp_Object Qfile_newer_than_file_p;
|
|
354 Lisp_Object Qinsert_file_contents;
|
|
355 Lisp_Object Qwrite_region;
|
|
356 Lisp_Object Qverify_visited_file_modtime;
|
|
357 Lisp_Object Qset_visited_file_modtime;
|
|
358
|
|
359 /* If FILENAME is handled specially on account of its syntax,
|
|
360 return its handler function. Otherwise, return nil. */
|
|
361
|
20
|
362 DEFUN ("find-file-name-handler", Ffind_file_name_handler, 1, 2, 0, /*
|
0
|
363 Return FILENAME's handler function for OPERATION, if it has one.
|
|
364 Otherwise, return nil.
|
|
365 A file name is handled if one of the regular expressions in
|
|
366 `file-name-handler-alist' matches it.
|
|
367
|
|
368 If OPERATION equals `inhibit-file-name-operation', then we ignore
|
|
369 any handlers that are members of `inhibit-file-name-handlers',
|
|
370 but we still do run any other handlers. This lets handlers
|
|
371 use the standard functions without calling themselves recursively.
|
20
|
372 */
|
|
373 (filename, operation))
|
0
|
374 {
|
116
|
375 /* This function does not GC */
|
263
|
376 /* This function can be called during GC */
|
0
|
377 /* This function must not munge the match data. */
|
|
378 Lisp_Object chain, inhibited_handlers;
|
|
379
|
|
380 CHECK_STRING (filename);
|
|
381
|
|
382 if (EQ (operation, Vinhibit_file_name_operation))
|
|
383 inhibited_handlers = Vinhibit_file_name_handlers;
|
|
384 else
|
|
385 inhibited_handlers = Qnil;
|
|
386
|
227
|
387 EXTERNAL_LIST_LOOP (chain, Vfile_name_handler_alist)
|
0
|
388 {
|
|
389 Lisp_Object elt = XCAR (chain);
|
|
390 if (CONSP (elt))
|
|
391 {
|
227
|
392 Lisp_Object string = XCAR (elt);
|
0
|
393 if (STRINGP (string)
|
|
394 && (fast_lisp_string_match (string, filename) >= 0))
|
|
395 {
|
|
396 Lisp_Object handler = XCDR (elt);
|
|
397 if (NILP (Fmemq (handler, inhibited_handlers)))
|
173
|
398 return handler;
|
0
|
399 }
|
|
400 }
|
|
401 QUIT;
|
|
402 }
|
|
403 return Qnil;
|
|
404 }
|
|
405
|
|
406 static Lisp_Object
|
|
407 call2_check_string (Lisp_Object fn, Lisp_Object arg0, Lisp_Object arg1)
|
|
408 {
|
116
|
409 /* This function can call lisp */
|
0
|
410 Lisp_Object result = call2 (fn, arg0, arg1);
|
|
411 CHECK_STRING (result);
|
173
|
412 return result;
|
0
|
413 }
|
|
414
|
|
415 static Lisp_Object
|
114
|
416 call2_check_string_or_nil (Lisp_Object fn, Lisp_Object arg0, Lisp_Object arg1)
|
|
417 {
|
116
|
418 /* This function can call lisp */
|
114
|
419 Lisp_Object result = call2 (fn, arg0, arg1);
|
|
420 if (!NILP (result))
|
|
421 CHECK_STRING (result);
|
173
|
422 return result;
|
114
|
423 }
|
|
424
|
|
425 static Lisp_Object
|
151
|
426 call3_check_string (Lisp_Object fn, Lisp_Object arg0,
|
0
|
427 Lisp_Object arg1, Lisp_Object arg2)
|
|
428 {
|
116
|
429 /* This function can call lisp */
|
0
|
430 Lisp_Object result = call3 (fn, arg0, arg1, arg2);
|
|
431 CHECK_STRING (result);
|
173
|
432 return result;
|
0
|
433 }
|
|
434
|
|
435
|
20
|
436 DEFUN ("file-name-directory", Ffile_name_directory, 1, 1, 0, /*
|
0
|
437 Return the directory component in file name NAME.
|
|
438 Return nil if NAME does not include a directory.
|
|
439 Otherwise return a directory spec.
|
209
|
440 Given a Unix syntax file name, returns a string ending in slash.
|
20
|
441 */
|
|
442 (file))
|
0
|
443 {
|
120
|
444 /* This function can GC. GC checked 1997.04.06. */
|
0
|
445 Bufbyte *beg;
|
|
446 Bufbyte *p;
|
|
447 Lisp_Object handler;
|
|
448
|
|
449 CHECK_STRING (file);
|
|
450
|
|
451 /* If the file name has special constructs in it,
|
|
452 call the corresponding file handler. */
|
|
453 handler = Ffind_file_name_handler (file, Qfile_name_directory);
|
|
454 if (!NILP (handler))
|
173
|
455 return call2_check_string_or_nil (handler, Qfile_name_directory, file);
|
0
|
456
|
|
457 #ifdef FILE_SYSTEM_CASE
|
|
458 file = FILE_SYSTEM_CASE (file);
|
|
459 #endif
|
16
|
460 beg = XSTRING_DATA (file);
|
|
461 p = beg + XSTRING_LENGTH (file);
|
0
|
462
|
211
|
463 while (p != beg && !IS_ANY_SEP (p[-1])
|
|
464 #ifdef DOS_NT
|
|
465 /* only recognise drive specifier at beginning */
|
|
466 && !(p[-1] == ':' && p == beg + 2)
|
|
467 #endif
|
|
468 ) p--;
|
0
|
469
|
|
470 if (p == beg)
|
|
471 return Qnil;
|
|
472 #ifdef DOS_NT
|
|
473 /* Expansion of "c:" to drive and default directory. */
|
|
474 /* (NT does the right thing.) */
|
|
475 if (p == beg + 2 && beg[1] == ':')
|
|
476 {
|
|
477 /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */
|
211
|
478 Bufbyte *res = alloca (MAXPATHLEN + 1);
|
|
479 if (getdefdir (toupper (*beg) - 'A' + 1, res))
|
0
|
480 {
|
211
|
481 if (!IS_DIRECTORY_SEP (res[strlen ((char *) res) - 1]))
|
0
|
482 strcat ((char *) res, "/");
|
|
483 beg = res;
|
|
484 p = beg + strlen ((char *) beg);
|
|
485 }
|
|
486 }
|
211
|
487 CORRECT_DIR_SEPS (beg);
|
0
|
488 #endif /* DOS_NT */
|
|
489 return make_string (beg, p - beg);
|
|
490 }
|
|
491
|
20
|
492 DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, 1, 1, 0, /*
|
0
|
493 Return file name NAME sans its directory.
|
|
494 For example, in a Unix-syntax file name,
|
|
495 this is everything after the last slash,
|
|
496 or the entire name if it contains no slash.
|
20
|
497 */
|
|
498 (file))
|
0
|
499 {
|
120
|
500 /* This function can GC. GC checked 1997.04.06. */
|
0
|
501 Bufbyte *beg, *p, *end;
|
|
502 Lisp_Object handler;
|
|
503
|
|
504 CHECK_STRING (file);
|
|
505
|
|
506 /* If the file name has special constructs in it,
|
|
507 call the corresponding file handler. */
|
|
508 handler = Ffind_file_name_handler (file, Qfile_name_nondirectory);
|
|
509 if (!NILP (handler))
|
173
|
510 return call2_check_string (handler, Qfile_name_nondirectory, file);
|
0
|
511
|
16
|
512 beg = XSTRING_DATA (file);
|
|
513 end = p = beg + XSTRING_LENGTH (file);
|
0
|
514
|
211
|
515 while (p != beg && !IS_ANY_SEP (p[-1])
|
|
516 #ifdef DOS_NT
|
|
517 /* only recognise drive specifier at beginning */
|
|
518 && !(p[-1] == ':' && p == beg + 2)
|
|
519 #endif
|
|
520 ) p--;
|
0
|
521
|
|
522 return make_string (p, end - p);
|
|
523 }
|
|
524
|
20
|
525 DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, 1, 1, 0, /*
|
0
|
526 Return a directly usable directory name somehow associated with FILENAME.
|
|
527 A `directly usable' directory name is one that may be used without the
|
|
528 intervention of any file handler.
|
|
529 If FILENAME is a directly usable file itself, return
|
|
530 (file-name-directory FILENAME).
|
|
531 The `call-process' and `start-process' functions use this function to
|
|
532 get a current directory to run processes in.
|
20
|
533 */
|
70
|
534 (filename))
|
0
|
535 {
|
120
|
536 /* This function can GC. GC checked 1997.04.06. */
|
0
|
537 Lisp_Object handler;
|
|
538
|
|
539 /* If the file name has special constructs in it,
|
|
540 call the corresponding file handler. */
|
|
541 handler = Ffind_file_name_handler (filename, Qunhandled_file_name_directory);
|
|
542 if (!NILP (handler))
|
|
543 return call2 (handler, Qunhandled_file_name_directory,
|
|
544 filename);
|
|
545
|
|
546 return Ffile_name_directory (filename);
|
|
547 }
|
|
548
|
|
549
|
|
550 static char *
|
|
551 file_name_as_directory (char *out, char *in)
|
|
552 {
|
|
553 int size = strlen (in) - 1;
|
|
554
|
|
555 strcpy (out, in);
|
|
556
|
|
557 /* For Unix syntax, Append a slash if necessary */
|
|
558 if (!IS_ANY_SEP (out[size]))
|
|
559 {
|
|
560 out[size + 1] = DIRECTORY_SEP;
|
|
561 out[size + 2] = '\0';
|
|
562 }
|
213
|
563 #ifdef DOS_NT
|
|
564 CORRECT_DIR_SEPS (out);
|
|
565 #endif
|
0
|
566 return out;
|
|
567 }
|
|
568
|
20
|
569 DEFUN ("file-name-as-directory", Ffile_name_as_directory, 1, 1, 0, /*
|
0
|
570 Return a string representing file FILENAME interpreted as a directory.
|
|
571 This operation exists because a directory is also a file, but its name as
|
|
572 a directory is different from its name as a file.
|
|
573 The result can be used as the value of `default-directory'
|
|
574 or passed as second argument to `expand-file-name'.
|
|
575 For a Unix-syntax file name, just appends a slash.
|
20
|
576 */
|
|
577 (file))
|
0
|
578 {
|
120
|
579 /* This function can GC. GC checked 1997.04.06. */
|
0
|
580 char *buf;
|
|
581 Lisp_Object handler;
|
|
582
|
|
583 CHECK_STRING (file);
|
|
584
|
|
585 /* If the file name has special constructs in it,
|
|
586 call the corresponding file handler. */
|
|
587 handler = Ffind_file_name_handler (file, Qfile_name_as_directory);
|
|
588 if (!NILP (handler))
|
173
|
589 return call2_check_string (handler, Qfile_name_as_directory, file);
|
0
|
590
|
16
|
591 buf = (char *) alloca (XSTRING_LENGTH (file) + 10);
|
0
|
592 return build_string (file_name_as_directory
|
16
|
593 (buf, (char *) XSTRING_DATA (file)));
|
0
|
594 }
|
|
595
|
|
596 /*
|
|
597 * Convert from directory name to filename.
|
211
|
598 * On UNIX, it's simple: just make sure there isn't a terminating /
|
0
|
599
|
|
600 * Value is nonzero if the string output is different from the input.
|
|
601 */
|
|
602
|
|
603 static int
|
|
604 directory_file_name (CONST char *src, char *dst)
|
|
605 {
|
|
606 long slen;
|
|
607
|
|
608 slen = strlen (src);
|
|
609 /* Process as Unix format: just remove any final slash.
|
|
610 But leave "/" unchanged; do not change it to "". */
|
|
611 strcpy (dst, src);
|
|
612 #ifdef APOLLO
|
|
613 /* Handle // as root for apollo's. */
|
|
614 if ((slen > 2 && dst[slen - 1] == '/')
|
|
615 || (slen > 1 && dst[0] != '/' && dst[slen - 1] == '/'))
|
|
616 dst[slen - 1] = 0;
|
|
617 #else
|
151
|
618 if (slen > 1
|
0
|
619 && IS_DIRECTORY_SEP (dst[slen - 1])
|
|
620 #ifdef DOS_NT
|
|
621 && !IS_ANY_SEP (dst[slen - 2])
|
211
|
622 #endif /* DOS_NT */
|
0
|
623 )
|
|
624 dst[slen - 1] = 0;
|
211
|
625 #endif /* APOLLO */
|
|
626 #ifdef DOS_NT
|
|
627 CORRECT_DIR_SEPS (dst);
|
|
628 #endif /* DOS_NT */
|
0
|
629 return 1;
|
|
630 }
|
|
631
|
20
|
632 DEFUN ("directory-file-name", Fdirectory_file_name, 1, 1, 0, /*
|
0
|
633 Return the file name of the directory named DIR.
|
|
634 This is the name of the file that holds the data for the directory DIR.
|
|
635 This operation exists because a directory is also a file, but its name as
|
|
636 a directory is different from its name as a file.
|
|
637 In Unix-syntax, this function just removes the final slash.
|
20
|
638 */
|
|
639 (directory))
|
0
|
640 {
|
120
|
641 /* This function can GC. GC checked 1997.04.06. */
|
0
|
642 char *buf;
|
|
643 Lisp_Object handler;
|
|
644
|
|
645 CHECK_STRING (directory);
|
|
646
|
|
647 #if 0 /* #### WTF? */
|
|
648 if (NILP (directory))
|
|
649 return Qnil;
|
|
650 #endif
|
|
651
|
|
652 /* If the file name has special constructs in it,
|
|
653 call the corresponding file handler. */
|
|
654 handler = Ffind_file_name_handler (directory, Qdirectory_file_name);
|
|
655 if (!NILP (handler))
|
173
|
656 return call2_check_string (handler, Qdirectory_file_name, directory);
|
16
|
657 buf = (char *) alloca (XSTRING_LENGTH (directory) + 20);
|
|
658 directory_file_name ((char *) XSTRING_DATA (directory), buf);
|
0
|
659 return build_string (buf);
|
|
660 }
|
|
661
|
20
|
662 DEFUN ("make-temp-name", Fmake_temp_name, 1, 1, 0, /*
|
0
|
663 Generate temporary file name (string) starting with PREFIX (a string).
|
|
664 The Emacs process number forms part of the result,
|
|
665 so there is no danger of generating a name being used by another process.
|
20
|
666 */
|
|
667 (prefix))
|
0
|
668 {
|
|
669 CONST char suffix[] = "XXXXXX";
|
|
670 Bufbyte *data;
|
|
671 Bytecount len;
|
|
672 Lisp_Object val;
|
|
673
|
|
674 CHECK_STRING (prefix);
|
16
|
675 len = XSTRING_LENGTH (prefix);
|
0
|
676 val = make_uninit_string (len + countof (suffix) - 1);
|
16
|
677 data = XSTRING_DATA (val);
|
|
678 memcpy (data, XSTRING_DATA (prefix), len);
|
0
|
679 memcpy (data + len, suffix, countof (suffix));
|
|
680 /* !!#### does mktemp() Mule-encapsulate? */
|
|
681 mktemp ((char *) data);
|
|
682
|
211
|
683 #ifdef DOS_NT
|
|
684 CORRECT_DIR_SEPS (XSTRING_DATA (val));
|
|
685 #endif /* DOS_NT */
|
0
|
686 return val;
|
|
687 }
|
|
688
|
20
|
689 DEFUN ("expand-file-name", Fexpand_file_name, 1, 2, 0, /*
|
243
|
690 Convert filename NAME to absolute, and canonicalize it.
|
|
691 Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative
|
|
692 (does not start with slash); if DEFAULT-DIRECTORY is nil or missing,
|
0
|
693 the current buffer's value of default-directory is used.
|
243
|
694 File name components that are `.' are removed, and
|
|
695 so are file name components followed by `..', along with the `..' itself;
|
0
|
696 note that these simplifications are done without checking the resulting
|
243
|
697 file names in the file system.
|
0
|
698 An initial `~/' expands to your home directory.
|
|
699 An initial `~USER/' expands to USER's home directory.
|
|
700 See also the function `substitute-in-file-name'.
|
20
|
701 */
|
243
|
702 (name, default_directory))
|
0
|
703 {
|
243
|
704 /* This function can GC */
|
0
|
705 Bufbyte *nm;
|
151
|
706
|
0
|
707 Bufbyte *newdir, *p, *o;
|
|
708 int tlen;
|
|
709 Bufbyte *target;
|
|
710 struct passwd *pw;
|
|
711 #ifdef DOS_NT
|
211
|
712 int drive = 0;
|
|
713 int collapse_newdir = 1;
|
243
|
714 #endif /* DOS_NT */
|
211
|
715 int length;
|
0
|
716 Lisp_Object handler;
|
151
|
717
|
0
|
718 CHECK_STRING (name);
|
|
719
|
|
720 /* If the file name has special constructs in it,
|
|
721 call the corresponding file handler. */
|
|
722 handler = Ffind_file_name_handler (name, Qexpand_file_name);
|
|
723 if (!NILP (handler))
|
243
|
724 return call3_check_string (handler, Qexpand_file_name, name,
|
|
725 default_directory);
|
|
726
|
|
727 /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */
|
|
728 if (NILP (default_directory))
|
|
729 default_directory = current_buffer->directory;
|
|
730 if (! STRINGP (default_directory))
|
|
731 default_directory = build_string ("/");
|
|
732
|
|
733 if (!NILP (default_directory))
|
0
|
734 {
|
243
|
735 handler = Ffind_file_name_handler (default_directory, Qexpand_file_name);
|
|
736 if (!NILP (handler))
|
|
737 return call3 (handler, Qexpand_file_name, name, default_directory);
|
0
|
738 }
|
243
|
739
|
|
740 o = XSTRING_DATA (default_directory);
|
|
741
|
|
742 /* Make sure DEFAULT_DIRECTORY is properly expanded.
|
0
|
743 It would be better to do this down below where we actually use
|
243
|
744 default_directory. Unfortunately, calling Fexpand_file_name recursively
|
0
|
745 could invoke GC, and the strings might be relocated. This would
|
|
746 be annoying because we have pointers into strings lying around
|
|
747 that would need adjusting, and people would add new pointers to
|
|
748 the code and forget to adjust them, resulting in intermittent bugs.
|
|
749 Putting this call here avoids all that crud.
|
|
750
|
|
751 The EQ test avoids infinite recursion. */
|
243
|
752 if (! NILP (default_directory) && !EQ (default_directory, name)
|
211
|
753 /* Save time in some common cases - as long as default_directory
|
|
754 is not relative, it can be canonicalized with name below (if it
|
|
755 is needed at all) without requiring it to be expanded now. */
|
|
756 #ifdef DOS_NT
|
243
|
757 /* Detect MSDOS file names with drive specifiers. */
|
|
758 && ! (IS_DRIVE (o[0]) && (IS_DEVICE_SEP (o[1]) && IS_DIRECTORY_SEP (o[2])))
|
211
|
759 #ifdef WINDOWSNT
|
243
|
760 /* Detect Windows file names in UNC format. */
|
|
761 && ! (IS_DIRECTORY_SEP (o[0]) && IS_DIRECTORY_SEP (o[1]))
|
211
|
762 #endif
|
|
763 #else /* not DOS_NT */
|
243
|
764 /* Detect Unix absolute file names (/... alone is not absolute on
|
|
765 DOS or Windows). */
|
|
766 && ! (IS_DIRECTORY_SEP (o[0]))
|
211
|
767 #endif /* not DOS_NT */
|
243
|
768 )
|
0
|
769 {
|
|
770 struct gcpro gcpro1;
|
|
771
|
243
|
772 GCPRO1 (name);
|
|
773 default_directory = Fexpand_file_name (default_directory, Qnil);
|
0
|
774 UNGCPRO;
|
|
775 }
|
|
776
|
|
777 #ifdef FILE_SYSTEM_CASE
|
|
778 name = FILE_SYSTEM_CASE (name);
|
|
779 #endif
|
|
780
|
243
|
781 /* #### dmoore - this is ugly, clean this up. Looks like nm pointing
|
|
782 into name should be safe during all of this, though. */
|
16
|
783 nm = XSTRING_DATA (name);
|
151
|
784
|
0
|
785 #ifdef DOS_NT
|
211
|
786 /* We will force directory separators to be either all \ or /, so make
|
|
787 a local copy to modify, even if there ends up being no change. */
|
|
788 nm = strcpy (alloca (strlen (nm) + 1), nm);
|
|
789
|
|
790 /* Find and remove drive specifier if present; this makes nm absolute
|
|
791 even if the rest of the name appears to be relative. */
|
0
|
792 {
|
|
793 Bufbyte *colon = strrchr (nm, ':');
|
243
|
794
|
0
|
795 if (colon)
|
211
|
796 /* Only recognize colon as part of drive specifier if there is a
|
|
797 single alphabetic character preceeding the colon (and if the
|
|
798 character before the drive letter, if present, is a directory
|
|
799 separator); this is to support the remote system syntax used by
|
|
800 ange-ftp, and the "po:username" syntax for POP mailboxes. */
|
243
|
801 look_again:
|
0
|
802 if (nm == colon)
|
|
803 nm++;
|
211
|
804 else if (IS_DRIVE (colon[-1])
|
|
805 && (colon == nm + 1 || IS_DIRECTORY_SEP (colon[-2])))
|
0
|
806 {
|
|
807 drive = colon[-1];
|
|
808 nm = colon + 1;
|
211
|
809 }
|
|
810 else
|
|
811 {
|
|
812 while (--colon >= nm)
|
|
813 if (colon[0] == ':')
|
|
814 goto look_again;
|
151
|
815 }
|
0
|
816 }
|
211
|
817
|
|
818 #ifdef WINDOWSNT
|
|
819 /* If we see "c://somedir", we want to strip the first slash after the
|
|
820 colon when stripping the drive letter. Otherwise, this expands to
|
|
821 "//somedir". */
|
|
822 if (drive && IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1]))
|
|
823 nm++;
|
|
824 #endif /* WINDOWSNT */
|
0
|
825 #endif /* DOS_NT */
|
|
826
|
211
|
827 #ifdef WINDOWSNT
|
|
828 /* Discard any previous drive specifier if nm is now in UNC format. */
|
|
829 if (IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1]))
|
|
830 {
|
|
831 drive = 0;
|
|
832 }
|
|
833 #endif /* WINDOWSNT */
|
|
834
|
|
835 /* If nm is absolute, look for /./ or /../ sequences; if none are
|
|
836 found, we can probably return right away. We will avoid allocating
|
|
837 a new string if name is already fully expanded. */
|
243
|
838 if (
|
|
839 IS_DIRECTORY_SEP (nm[0])
|
|
840 #ifdef MSDOS
|
|
841 && drive
|
|
842 #endif
|
211
|
843 #ifdef WINDOWSNT
|
|
844 && (drive || IS_DIRECTORY_SEP (nm[1]))
|
189
|
845 #endif
|
211
|
846 )
|
0
|
847 {
|
|
848 /* If it turns out that the filename we want to return is just a
|
|
849 suffix of FILENAME, we don't need to go through and edit
|
|
850 things; we just need to construct a new string using data
|
|
851 starting at the middle of FILENAME. If we set lose to a
|
|
852 non-zero value, that means we've discovered that we can't do
|
|
853 that cool trick. */
|
|
854 int lose = 0;
|
|
855
|
|
856 p = nm;
|
|
857 while (*p)
|
|
858 {
|
243
|
859 /* Since we know the name is absolute, we can assume that each
|
0
|
860 element starts with a "/". */
|
|
861
|
|
862 /* "." and ".." are hairy. */
|
|
863 if (IS_DIRECTORY_SEP (p[0])
|
|
864 && p[1] == '.'
|
|
865 && (IS_DIRECTORY_SEP (p[2])
|
|
866 || p[2] == 0
|
|
867 || (p[2] == '.' && (IS_DIRECTORY_SEP (p[3])
|
|
868 || p[3] == 0))))
|
|
869 lose = 1;
|
|
870 p++;
|
|
871 }
|
|
872 if (!lose)
|
|
873 {
|
211
|
874 #ifdef DOS_NT
|
|
875 /* Make sure directories are all separated with / or \ as
|
|
876 desired, but avoid allocation of a new string when not
|
|
877 required. */
|
|
878 CORRECT_DIR_SEPS (nm);
|
|
879 #ifdef WINDOWSNT
|
|
880 if (IS_DIRECTORY_SEP (nm[1]))
|
|
881 {
|
|
882 if (strcmp (nm, XSTRING_DATA (name)) != 0)
|
|
883 name = build_string (nm);
|
|
884 }
|
|
885 else
|
|
886 #endif
|
243
|
887 /* drive must be set, so this is okay */
|
|
888 if (strcmp (nm - 2, XSTRING_DATA (name)) != 0)
|
|
889 {
|
|
890 name = make_string (nm - 2, p - nm + 2);
|
|
891 XSTRING_DATA (name)[0] = DRIVE_LETTER (drive);
|
|
892 XSTRING_DATA (name)[1] = ':';
|
|
893 }
|
211
|
894 return name;
|
|
895 #else /* not DOS_NT */
|
16
|
896 if (nm == XSTRING_DATA (name))
|
0
|
897 return name;
|
243
|
898 return build_string (nm);
|
|
899 #endif /* not DOS_NT */
|
0
|
900 }
|
|
901 }
|
|
902
|
211
|
903 /* At this point, nm might or might not be an absolute file name. We
|
|
904 need to expand ~ or ~user if present, otherwise prefix nm with
|
|
905 default_directory if nm is not absolute, and finally collapse /./
|
|
906 and /foo/../ sequences.
|
|
907
|
|
908 We set newdir to be the appropriate prefix if one is needed:
|
243
|
909 - the relevant user directory if nm starts with ~ or ~user
|
|
910 - the specified drive's working dir (DOS/NT only) if nm does not
|
|
911 start with /
|
|
912 - the value of default_directory.
|
211
|
913
|
|
914 Note that these prefixes are not guaranteed to be absolute (except
|
|
915 for the working dir of a drive). Therefore, to ensure we always
|
|
916 return an absolute name, if the final prefix is not absolute we
|
|
917 append it to the current working directory. */
|
0
|
918
|
|
919 newdir = 0;
|
|
920
|
|
921 if (nm[0] == '~') /* prefix ~ */
|
|
922 {
|
|
923 if (IS_DIRECTORY_SEP (nm[1])
|
243
|
924 || nm[1] == 0) /* ~ by itself */
|
0
|
925 {
|
|
926 if (!(newdir = (Bufbyte *) egetenv ("HOME")))
|
|
927 newdir = (Bufbyte *) "";
|
|
928 nm++;
|
211
|
929 #ifdef DOS_NT
|
|
930 collapse_newdir = 0;
|
243
|
931 #endif
|
0
|
932 }
|
|
933 else /* ~user/filename */
|
|
934 {
|
209
|
935 for (p = nm; *p && (!IS_DIRECTORY_SEP (*p)); p++);
|
0
|
936 o = (Bufbyte *) alloca (p - nm + 1);
|
|
937 memcpy (o, (char *) nm, p - nm);
|
|
938 o [p - nm] = 0;
|
|
939
|
243
|
940 /* #### marcpa's syncing note: FSF uses getpwnam even on NT,
|
|
941 which does not work. The following works only if ~USER
|
|
942 names the user who runs this instance of XEmacs. While
|
|
943 NT is single-user (for the moment) you still can have
|
|
944 multiple user profiles users defined, each with its HOME.
|
|
945 Therefore, the following should be reworked to handle
|
|
946 this case. */
|
0
|
947 #ifdef WINDOWSNT
|
243
|
948 /* Now if the file given is "~foo/file" and HOME="c:/", then
|
|
949 we want the file to be named "c:/file" ("~foo" becomes
|
|
950 "c:/"). The variable o has "~foo", so we can use the
|
|
951 length of that string to offset nm. August Hill, 31 Aug
|
|
952 1998. */
|
211
|
953 newdir = (Bufbyte *) egetenv ("HOME");
|
0
|
954 dostounix_filename (newdir);
|
195
|
955 nm += strlen(o) + 1;
|
0
|
956 #else /* not WINDOWSNT */
|
|
957 /* Jamie reports that getpwnam() can get wedged by SIGIO/SIGALARM
|
|
958 occurring in it. (It can call select()). */
|
|
959 slow_down_interrupts ();
|
|
960 pw = (struct passwd *) getpwnam ((char *) o + 1);
|
|
961 speed_up_interrupts ();
|
|
962 if (pw)
|
|
963 {
|
|
964 newdir = (Bufbyte *) pw -> pw_dir;
|
|
965 nm = p;
|
|
966 }
|
|
967 #endif /* not WINDOWSNT */
|
|
968
|
|
969 /* If we don't find a user of that name, leave the name
|
|
970 unchanged; don't move nm forward to p. */
|
|
971 }
|
|
972 }
|
|
973
|
|
974 #ifdef DOS_NT
|
211
|
975 /* On DOS and Windows, nm is absolute if a drive name was specified;
|
|
976 use the drive's current directory as the prefix if needed. */
|
|
977 if (!newdir && drive)
|
|
978 {
|
|
979 /* Get default directory if needed to make nm absolute. */
|
|
980 if (!IS_DIRECTORY_SEP (nm[0]))
|
|
981 {
|
|
982 newdir = alloca (MAXPATHLEN + 1);
|
|
983 if (!getdefdir (toupper (drive) - 'A' + 1, newdir))
|
|
984 newdir = NULL;
|
|
985 }
|
|
986 if (!newdir)
|
|
987 {
|
|
988 /* Either nm starts with /, or drive isn't mounted. */
|
|
989 newdir = alloca (4);
|
|
990 newdir[0] = DRIVE_LETTER (drive);
|
|
991 newdir[1] = ':';
|
|
992 newdir[2] = '/';
|
|
993 newdir[3] = 0;
|
|
994 }
|
|
995 }
|
0
|
996 #endif /* DOS_NT */
|
211
|
997
|
|
998 /* Finally, if no prefix has been specified and nm is not absolute,
|
|
999 then it must be expanded relative to default_directory. */
|
243
|
1000
|
211
|
1001 if (1
|
|
1002 #ifndef DOS_NT
|
243
|
1003 /* /... alone is not absolute on DOS and Windows. */
|
|
1004 && !IS_DIRECTORY_SEP (nm[0])
|
|
1005 #endif
|
211
|
1006 #ifdef WINDOWSNT
|
|
1007 && !(IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1]))
|
|
1008 #endif
|
243
|
1009 && !newdir)
|
0
|
1010 {
|
243
|
1011 newdir = XSTRING_DATA (default_directory);
|
0
|
1012 }
|
|
1013
|
|
1014 #ifdef DOS_NT
|
211
|
1015 if (newdir)
|
|
1016 {
|
|
1017 /* First ensure newdir is an absolute name. */
|
|
1018 if (
|
|
1019 /* Detect MSDOS file names with drive specifiers. */
|
|
1020 ! (IS_DRIVE (newdir[0])
|
|
1021 && IS_DEVICE_SEP (newdir[1]) && IS_DIRECTORY_SEP (newdir[2]))
|
|
1022 #ifdef WINDOWSNT
|
|
1023 /* Detect Windows file names in UNC format. */
|
|
1024 && ! (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1]))
|
|
1025 #endif
|
|
1026 )
|
|
1027 {
|
|
1028 /* Effectively, let newdir be (expand-file-name newdir cwd).
|
|
1029 Because of the admonition against calling expand-file-name
|
|
1030 when we have pointers into lisp strings, we accomplish this
|
|
1031 indirectly by prepending newdir to nm if necessary, and using
|
|
1032 cwd (or the wd of newdir's drive) as the new newdir. */
|
|
1033
|
|
1034 if (IS_DRIVE (newdir[0]) && newdir[1] == ':')
|
|
1035 {
|
|
1036 drive = newdir[0];
|
|
1037 newdir += 2;
|
|
1038 }
|
|
1039 if (!IS_DIRECTORY_SEP (nm[0]))
|
|
1040 {
|
|
1041 char * tmp = alloca (strlen (newdir) + strlen (nm) + 2);
|
|
1042 file_name_as_directory (tmp, newdir);
|
|
1043 strcat (tmp, nm);
|
|
1044 nm = tmp;
|
|
1045 }
|
|
1046 newdir = alloca (MAXPATHLEN + 1);
|
|
1047 if (drive)
|
|
1048 {
|
|
1049 if (!getdefdir (toupper (drive) - 'A' + 1, newdir))
|
|
1050 newdir = "/";
|
|
1051 }
|
|
1052 else
|
|
1053 getwd (newdir);
|
|
1054 }
|
|
1055
|
|
1056 /* Strip off drive name from prefix, if present. */
|
|
1057 if (IS_DRIVE (newdir[0]) && newdir[1] == ':')
|
|
1058 {
|
|
1059 drive = newdir[0];
|
|
1060 newdir += 2;
|
|
1061 }
|
|
1062
|
|
1063 /* Keep only a prefix from newdir if nm starts with slash
|
225
|
1064 (/ /server/share for UNC, nothing otherwise). */
|
211
|
1065 if (IS_DIRECTORY_SEP (nm[0]) && collapse_newdir)
|
|
1066 {
|
|
1067 #ifdef WINDOWSNT
|
|
1068 if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1]))
|
|
1069 {
|
|
1070 newdir = strcpy (alloca (strlen (newdir) + 1), newdir);
|
|
1071 p = newdir + 2;
|
|
1072 while (*p && !IS_DIRECTORY_SEP (*p)) p++;
|
|
1073 p++;
|
|
1074 while (*p && !IS_DIRECTORY_SEP (*p)) p++;
|
|
1075 *p = 0;
|
|
1076 }
|
|
1077 else
|
|
1078 #endif
|
|
1079 newdir = "";
|
|
1080 }
|
|
1081 }
|
0
|
1082 #endif /* DOS_NT */
|
243
|
1083
|
|
1084 if (newdir)
|
0
|
1085 {
|
211
|
1086 /* Get rid of any slash at the end of newdir, unless newdir is
|
|
1087 just // (an incomplete UNC name). */
|
243
|
1088 length = strlen (newdir);
|
|
1089 if (length > 0 && IS_DIRECTORY_SEP (newdir[length - 1])
|
211
|
1090 #ifdef WINDOWSNT
|
|
1091 && !(length == 2 && IS_DIRECTORY_SEP (newdir[0]))
|
0
|
1092 #endif
|
211
|
1093 )
|
0
|
1094 {
|
|
1095 Bufbyte *temp = (Bufbyte *) alloca (length);
|
|
1096 memcpy (temp, newdir, length - 1);
|
|
1097 temp[length - 1] = 0;
|
|
1098 newdir = temp;
|
|
1099 }
|
|
1100 tlen = length + 1;
|
|
1101 }
|
|
1102 else
|
|
1103 tlen = 0;
|
|
1104
|
|
1105 /* Now concatenate the directory and name to new space in the stack frame */
|
243
|
1106 tlen += strlen (nm) + 1;
|
0
|
1107 #ifdef DOS_NT
|
151
|
1108 /* Add reserved space for drive name. (The Microsoft x86 compiler
|
0
|
1109 produces incorrect code if the following two lines are combined.) */
|
|
1110 target = (Bufbyte *) alloca (tlen + 2);
|
|
1111 target += 2;
|
|
1112 #else /* not DOS_NT */
|
|
1113 target = (Bufbyte *) alloca (tlen);
|
243
|
1114 #endif /* not DOS_NT */
|
0
|
1115 *target = 0;
|
|
1116
|
|
1117 if (newdir)
|
|
1118 {
|
|
1119 if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0]))
|
243
|
1120 strcpy (target, newdir);
|
0
|
1121 else
|
243
|
1122 file_name_as_directory (target, newdir);
|
0
|
1123 }
|
|
1124
|
243
|
1125 strcat (target, nm);
|
|
1126
|
|
1127 /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */
|
0
|
1128
|
|
1129 /* Now canonicalize by removing /. and /foo/.. if they appear. */
|
|
1130
|
|
1131 p = target;
|
|
1132 o = target;
|
|
1133
|
|
1134 while (*p)
|
|
1135 {
|
|
1136 if (!IS_DIRECTORY_SEP (*p))
|
|
1137 {
|
|
1138 *o++ = *p++;
|
|
1139 }
|
|
1140 else if (IS_DIRECTORY_SEP (p[0])
|
243
|
1141 && p[1] == '.'
|
0
|
1142 && (IS_DIRECTORY_SEP (p[2])
|
|
1143 || p[2] == 0))
|
|
1144 {
|
|
1145 /* If "/." is the entire filename, keep the "/". Otherwise,
|
|
1146 just delete the whole "/.". */
|
|
1147 if (o == target && p[2] == '\0')
|
|
1148 *o++ = *p;
|
|
1149 p += 2;
|
|
1150 }
|
|
1151 else if (IS_DIRECTORY_SEP (p[0]) && p[1] == '.' && p[2] == '.'
|
|
1152 /* `/../' is the "superroot" on certain file systems. */
|
|
1153 && o != target
|
|
1154 && (IS_DIRECTORY_SEP (p[3]) || p[3] == 0))
|
|
1155 {
|
|
1156 while (o != target && (--o) && !IS_DIRECTORY_SEP (*o))
|
|
1157 ;
|
243
|
1158 /* Keep initial / only if this is the whole name. */
|
|
1159 if (o == target && IS_ANY_SEP (*o) && p[3] == 0)
|
0
|
1160 ++o;
|
|
1161 p += 3;
|
|
1162 }
|
|
1163 else
|
|
1164 {
|
|
1165 *o++ = *p++;
|
|
1166 }
|
|
1167 }
|
|
1168
|
|
1169 #ifdef DOS_NT
|
211
|
1170 /* At last, set drive name. */
|
0
|
1171 #ifdef WINDOWSNT
|
211
|
1172 /* Except for network file name. */
|
|
1173 if (!(IS_DIRECTORY_SEP (target[0]) && IS_DIRECTORY_SEP (target[1])))
|
0
|
1174 #endif /* WINDOWSNT */
|
|
1175 {
|
211
|
1176 if (!drive) abort ();
|
0
|
1177 target -= 2;
|
211
|
1178 target[0] = DRIVE_LETTER (drive);
|
0
|
1179 target[1] = ':';
|
|
1180 }
|
211
|
1181 CORRECT_DIR_SEPS (target);
|
0
|
1182 #endif /* DOS_NT */
|
|
1183
|
|
1184 return make_string (target, o - target);
|
|
1185 }
|
|
1186
|
|
1187 #if 0 /* FSFmacs */
|
227
|
1188 /* another older version of expand-file-name; */
|
0
|
1189 #endif
|
|
1190
|
20
|
1191 DEFUN ("file-truename", Ffile_truename, 1, 2, 0, /*
|
0
|
1192 Return the canonical name of the given FILE.
|
|
1193 Second arg DEFAULT is directory to start with if FILE is relative
|
|
1194 (does not start with slash); if DEFAULT is nil or missing,
|
|
1195 the current buffer's value of default-directory is used.
|
|
1196 No component of the resulting pathname will be a symbolic link, as
|
|
1197 in the realpath() function.
|
20
|
1198 */
|
173
|
1199 (filename, default_))
|
0
|
1200 {
|
120
|
1201 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1202 Lisp_Object expanded_name;
|
|
1203 Lisp_Object handler;
|
120
|
1204 struct gcpro gcpro1;
|
0
|
1205
|
|
1206 CHECK_STRING (filename);
|
|
1207
|
173
|
1208 expanded_name = Fexpand_file_name (filename, default_);
|
0
|
1209
|
|
1210 if (!STRINGP (expanded_name))
|
|
1211 return Qnil;
|
|
1212
|
|
1213 GCPRO1 (expanded_name);
|
|
1214 handler = Ffind_file_name_handler (expanded_name, Qfile_truename);
|
|
1215 UNGCPRO;
|
|
1216
|
|
1217 if (!NILP (handler))
|
173
|
1218 return call2_check_string (handler, Qfile_truename, expanded_name);
|
0
|
1219
|
|
1220 {
|
|
1221 char resolved_path[MAXPATHLEN];
|
|
1222 char path[MAXPATHLEN];
|
|
1223 char *p = path;
|
16
|
1224 int elen = XSTRING_LENGTH (expanded_name);
|
151
|
1225
|
0
|
1226 if (elen >= countof (path))
|
|
1227 goto toolong;
|
151
|
1228
|
16
|
1229 memcpy (path, XSTRING_DATA (expanded_name), elen + 1);
|
0
|
1230 /* memset (resolved_path, 0, sizeof (resolved_path)); */
|
|
1231
|
|
1232 /* Try doing it all at once. */
|
|
1233 /* !!#### Does realpath() Mule-encapsulate? */
|
|
1234 if (!realpath (path, resolved_path))
|
|
1235 {
|
|
1236 /* Didn't resolve it -- have to do it one component at a time. */
|
|
1237 /* "realpath" is a typically useless, stupid un*x piece of crap.
|
|
1238 It claims to return a useful value in the "error" case, but since
|
|
1239 there is no indication provided of how far along the pathname
|
|
1240 the function went before erring, there is no way to use the
|
|
1241 partial result returned. What a piece of junk. */
|
|
1242 for (;;)
|
|
1243 {
|
|
1244 p = (char *) memchr (p + 1, '/', elen - (p + 1 - path));
|
|
1245 if (p)
|
|
1246 *p = 0;
|
|
1247
|
|
1248 /* memset (resolved_path, 0, sizeof (resolved_path)); */
|
|
1249 if (realpath (path, resolved_path))
|
|
1250 {
|
|
1251 if (p)
|
|
1252 *p = '/';
|
|
1253 else
|
|
1254 break;
|
|
1255
|
|
1256 }
|
209
|
1257 else if (errno == ENOENT || errno == EACCES)
|
0
|
1258 {
|
|
1259 /* Failed on this component. Just tack on the rest of
|
|
1260 the string and we are done. */
|
|
1261 int rlen = strlen (resolved_path);
|
|
1262
|
|
1263 /* "On failure, it returns NULL, sets errno to indicate
|
|
1264 the error, and places in resolved_path the absolute pathname
|
|
1265 of the path component which could not be resolved." */
|
|
1266 if (p)
|
|
1267 {
|
|
1268 int plen = elen - (p - path);
|
151
|
1269
|
0
|
1270 if (rlen > 1 && resolved_path[rlen - 1] == '/')
|
|
1271 rlen = rlen - 1;
|
|
1272
|
|
1273 if (plen + rlen + 1 > countof (resolved_path))
|
|
1274 goto toolong;
|
|
1275
|
|
1276 resolved_path[rlen] = '/';
|
|
1277 memcpy (resolved_path + rlen + 1, p + 1, plen + 1 - 1);
|
|
1278 }
|
|
1279 break;
|
|
1280 }
|
|
1281 else
|
|
1282 goto lose;
|
|
1283 }
|
|
1284 }
|
|
1285
|
|
1286 {
|
|
1287 int rlen = strlen (resolved_path);
|
82
|
1288 if (elen > 0 && XSTRING_BYTE (expanded_name, elen - 1) == '/'
|
0
|
1289 && !(rlen > 0 && resolved_path[rlen - 1] == '/'))
|
|
1290 {
|
|
1291 if (rlen + 1 > countof (resolved_path))
|
|
1292 goto toolong;
|
|
1293 resolved_path[rlen] = '/';
|
|
1294 resolved_path[rlen + 1] = 0;
|
|
1295 rlen = rlen + 1;
|
|
1296 }
|
209
|
1297 return make_ext_string ((Bufbyte *) resolved_path, rlen, FORMAT_BINARY);
|
0
|
1298 }
|
|
1299
|
|
1300 toolong:
|
|
1301 errno = ENAMETOOLONG;
|
|
1302 goto lose;
|
|
1303 lose:
|
|
1304 report_file_error ("Finding truename", list1 (expanded_name));
|
|
1305 }
|
|
1306 return Qnil; /* suppress compiler warning */
|
|
1307 }
|
|
1308
|
|
1309
|
20
|
1310 DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, 1, 1, 0, /*
|
0
|
1311 Substitute environment variables referred to in FILENAME.
|
|
1312 `$FOO' where FOO is an environment variable name means to substitute
|
|
1313 the value of that variable. The variable name should be terminated
|
|
1314 with a character not a letter, digit or underscore; otherwise, enclose
|
|
1315 the entire variable name in braces.
|
|
1316 If `/~' appears, all of FILENAME through that `/' is discarded.
|
|
1317
|
20
|
1318 */
|
|
1319 (string))
|
0
|
1320 {
|
120
|
1321 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1322 Bufbyte *nm;
|
|
1323
|
|
1324 Bufbyte *s, *p, *o, *x, *endp;
|
|
1325 Bufbyte *target = 0;
|
|
1326 int total = 0;
|
|
1327 int substituted = 0;
|
|
1328 Bufbyte *xnm;
|
|
1329 Lisp_Object handler;
|
|
1330
|
|
1331 CHECK_STRING (string);
|
|
1332
|
|
1333 /* If the file name has special constructs in it,
|
|
1334 call the corresponding file handler. */
|
|
1335 handler = Ffind_file_name_handler (string, Qsubstitute_in_file_name);
|
|
1336 if (!NILP (handler))
|
173
|
1337 return call2_check_string_or_nil (handler, Qsubstitute_in_file_name,
|
|
1338 string);
|
0
|
1339
|
16
|
1340 nm = XSTRING_DATA (string);
|
211
|
1341 #ifdef DOS_NT
|
|
1342 nm = strcpy (alloca (strlen (nm) + 1), nm);
|
|
1343 CORRECT_DIR_SEPS (nm);
|
|
1344 substituted = (strcmp (nm, XSTRING_DATA (string)) != 0);
|
0
|
1345 #endif
|
16
|
1346 endp = nm + XSTRING_LENGTH (string);
|
0
|
1347
|
|
1348 /* If /~ or // appears, discard everything through first slash. */
|
|
1349
|
|
1350 for (p = nm; p != endp; p++)
|
|
1351 {
|
211
|
1352 if ((p[0] == '~'
|
|
1353 #if defined (APOLLO) || defined (WINDOWSNT)
|
|
1354 /* // at start of file name is meaningful in Apollo and
|
|
1355 WindowsNT systems */
|
|
1356 || (IS_DIRECTORY_SEP (p[0]) && p - 1 != nm)
|
|
1357 #else /* not (APOLLO || WINDOWSNT) */
|
|
1358 || IS_DIRECTORY_SEP (p[0])
|
|
1359 #endif /* not (APOLLO || WINDOWSNT) */
|
0
|
1360 )
|
|
1361 && p != nm
|
209
|
1362 && (IS_DIRECTORY_SEP (p[-1])))
|
0
|
1363 {
|
|
1364 nm = p;
|
|
1365 substituted = 1;
|
|
1366 }
|
|
1367 #ifdef DOS_NT
|
211
|
1368 /* see comment in expand-file-name about drive specifiers */
|
|
1369 else if (IS_DRIVE (p[0]) && p[1] == ':'
|
|
1370 && p > nm && IS_DIRECTORY_SEP (p[-1]))
|
0
|
1371 {
|
|
1372 nm = p;
|
|
1373 substituted = 1;
|
|
1374 }
|
|
1375 #endif /* DOS_NT */
|
|
1376 }
|
|
1377
|
|
1378 /* See if any variables are substituted into the string
|
|
1379 and find the total length of their values in `total' */
|
|
1380
|
|
1381 for (p = nm; p != endp;)
|
|
1382 if (*p != '$')
|
|
1383 p++;
|
|
1384 else
|
|
1385 {
|
|
1386 p++;
|
|
1387 if (p == endp)
|
|
1388 goto badsubst;
|
|
1389 else if (*p == '$')
|
|
1390 {
|
|
1391 /* "$$" means a single "$" */
|
|
1392 p++;
|
|
1393 total -= 1;
|
|
1394 substituted = 1;
|
|
1395 continue;
|
|
1396 }
|
|
1397 else if (*p == '{')
|
|
1398 {
|
|
1399 o = ++p;
|
|
1400 while (p != endp && *p != '}') p++;
|
|
1401 if (*p != '}') goto missingclose;
|
|
1402 s = p;
|
|
1403 }
|
|
1404 else
|
|
1405 {
|
|
1406 o = p;
|
|
1407 while (p != endp && (isalnum (*p) || *p == '_')) p++;
|
|
1408 s = p;
|
|
1409 }
|
|
1410
|
|
1411 /* Copy out the variable name */
|
|
1412 target = (Bufbyte *) alloca (s - o + 1);
|
|
1413 strncpy ((char *) target, (char *) o, s - o);
|
|
1414 target[s - o] = 0;
|
|
1415 #ifdef DOS_NT
|
|
1416 strupr (target); /* $home == $HOME etc. */
|
|
1417 #endif /* DOS_NT */
|
|
1418
|
|
1419 /* Get variable value */
|
|
1420 o = (Bufbyte *) egetenv ((char *) target);
|
|
1421 if (!o) goto badvar;
|
|
1422 total += strlen ((char *) o);
|
|
1423 substituted = 1;
|
|
1424 }
|
|
1425
|
|
1426 if (!substituted)
|
|
1427 return string;
|
|
1428
|
|
1429 /* If substitution required, recopy the string and do it */
|
|
1430 /* Make space in stack frame for the new copy */
|
16
|
1431 xnm = (Bufbyte *) alloca (XSTRING_LENGTH (string) + total + 1);
|
0
|
1432 x = xnm;
|
|
1433
|
|
1434 /* Copy the rest of the name through, replacing $ constructs with values */
|
|
1435 for (p = nm; *p;)
|
|
1436 if (*p != '$')
|
|
1437 *x++ = *p++;
|
|
1438 else
|
|
1439 {
|
|
1440 p++;
|
|
1441 if (p == endp)
|
|
1442 goto badsubst;
|
|
1443 else if (*p == '$')
|
|
1444 {
|
|
1445 *x++ = *p++;
|
|
1446 continue;
|
|
1447 }
|
|
1448 else if (*p == '{')
|
|
1449 {
|
|
1450 o = ++p;
|
|
1451 while (p != endp && *p != '}') p++;
|
|
1452 if (*p != '}') goto missingclose;
|
|
1453 s = p++;
|
|
1454 }
|
|
1455 else
|
|
1456 {
|
|
1457 o = p;
|
|
1458 while (p != endp && (isalnum (*p) || *p == '_')) p++;
|
|
1459 s = p;
|
|
1460 }
|
|
1461
|
|
1462 /* Copy out the variable name */
|
|
1463 target = (Bufbyte *) alloca (s - o + 1);
|
|
1464 strncpy ((char *) target, (char *) o, s - o);
|
|
1465 target[s - o] = 0;
|
|
1466 #ifdef DOS_NT
|
|
1467 strupr (target); /* $home == $HOME etc. */
|
|
1468 #endif /* DOS_NT */
|
|
1469
|
|
1470 /* Get variable value */
|
|
1471 o = (Bufbyte *) egetenv ((char *) target);
|
|
1472 if (!o)
|
|
1473 goto badvar;
|
|
1474
|
|
1475 strcpy ((char *) x, (char *) o);
|
|
1476 x += strlen ((char *) o);
|
|
1477 }
|
|
1478
|
|
1479 *x = 0;
|
|
1480
|
|
1481 /* If /~ or // appears, discard everything through first slash. */
|
|
1482
|
|
1483 for (p = xnm; p != x; p++)
|
|
1484 if ((p[0] == '~'
|
211
|
1485 #if defined (APOLLO) || defined (WINDOWSNT)
|
0
|
1486 || (IS_DIRECTORY_SEP (p[0]) && p - 1 != xnm)
|
211
|
1487 #else /* not (APOLLO || WINDOWSNT) */
|
|
1488 || IS_DIRECTORY_SEP (p[0])
|
|
1489 #endif /* APOLLO || WINDOWSNT */
|
0
|
1490 )
|
|
1491 /* don't do p[-1] if that would go off the beginning --jwz */
|
|
1492 && p != nm && p > xnm && IS_DIRECTORY_SEP (p[-1]))
|
|
1493 xnm = p;
|
|
1494 #ifdef DOS_NT
|
211
|
1495 else if (IS_DRIVE (p[0]) && p[1] == ':'
|
|
1496 && p > nm && IS_DIRECTORY_SEP (p[-1]))
|
0
|
1497 xnm = p;
|
|
1498 #endif
|
|
1499
|
|
1500 return make_string (xnm, x - xnm);
|
|
1501
|
|
1502 badsubst:
|
|
1503 error ("Bad format environment-variable substitution");
|
|
1504 missingclose:
|
|
1505 error ("Missing \"}\" in environment-variable substitution");
|
|
1506 badvar:
|
|
1507 error ("Substituting nonexistent environment variable \"%s\"",
|
|
1508 target);
|
|
1509
|
|
1510 /* NOTREACHED */
|
|
1511 return Qnil; /* suppress compiler warning */
|
|
1512 }
|
|
1513
|
211
|
1514 /* A slightly faster and more convenient way to get
|
|
1515 (directory-file-name (expand-file-name FOO)). */
|
0
|
1516
|
|
1517 Lisp_Object
|
|
1518 expand_and_dir_to_file (Lisp_Object filename, Lisp_Object defdir)
|
|
1519 {
|
116
|
1520 /* This function can call lisp */
|
0
|
1521 Lisp_Object abspath;
|
|
1522 struct gcpro gcpro1;
|
|
1523
|
|
1524 abspath = Fexpand_file_name (filename, defdir);
|
116
|
1525 GCPRO1 (abspath);
|
0
|
1526 /* Remove final slash, if any (unless path is root).
|
|
1527 stat behaves differently depending! */
|
16
|
1528 if (XSTRING_LENGTH (abspath) > 1
|
82
|
1529 && IS_DIRECTORY_SEP (XSTRING_BYTE (abspath, XSTRING_LENGTH (abspath) - 1))
|
|
1530 && !IS_DEVICE_SEP (XSTRING_BYTE (abspath, XSTRING_LENGTH (abspath) - 2)))
|
0
|
1531 /* We cannot take shortcuts; they might be wrong for magic file names. */
|
|
1532 abspath = Fdirectory_file_name (abspath);
|
|
1533 UNGCPRO;
|
|
1534 return abspath;
|
|
1535 }
|
|
1536
|
|
1537 /* Signal an error if the file ABSNAME already exists.
|
|
1538 If INTERACTIVE is nonzero, ask the user whether to proceed,
|
|
1539 and bypass the error if the user says to go ahead.
|
|
1540 QUERYSTRING is a name for the action that is being considered
|
|
1541 to alter the file.
|
|
1542 *STATPTR is used to store the stat information if the file exists.
|
|
1543 If the file does not exist, STATPTR->st_mode is set to 0. */
|
|
1544
|
|
1545 static void
|
|
1546 barf_or_query_if_file_exists (Lisp_Object absname, CONST char *querystring,
|
|
1547 int interactive, struct stat *statptr)
|
|
1548 {
|
120
|
1549 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1550 struct stat statbuf;
|
|
1551
|
|
1552 /* stat is a good way to tell whether the file exists,
|
|
1553 regardless of what access permissions it has. */
|
16
|
1554 if (stat ((char *) XSTRING_DATA (absname), &statbuf) >= 0)
|
0
|
1555 {
|
|
1556 Lisp_Object tem;
|
116
|
1557
|
0
|
1558 if (interactive)
|
116
|
1559 {
|
|
1560 Lisp_Object prompt;
|
|
1561 struct gcpro gcpro1;
|
|
1562
|
|
1563 prompt = emacs_doprnt_string_c
|
0
|
1564 ((CONST Bufbyte *) GETTEXT ("File %s already exists; %s anyway? "),
|
16
|
1565 Qnil, -1, XSTRING_DATA (absname),
|
116
|
1566 GETTEXT (querystring));
|
151
|
1567
|
116
|
1568 GCPRO1 (prompt);
|
|
1569 tem = call1 (Qyes_or_no_p, prompt);
|
|
1570 UNGCPRO;
|
|
1571 }
|
0
|
1572 else
|
|
1573 tem = Qnil;
|
116
|
1574
|
0
|
1575 if (NILP (tem))
|
|
1576 Fsignal (Qfile_already_exists,
|
|
1577 list2 (build_translated_string ("File already exists"),
|
|
1578 absname));
|
|
1579 if (statptr)
|
|
1580 *statptr = statbuf;
|
|
1581 }
|
|
1582 else
|
|
1583 {
|
|
1584 if (statptr)
|
|
1585 statptr->st_mode = 0;
|
|
1586 }
|
|
1587 return;
|
|
1588 }
|
|
1589
|
20
|
1590 DEFUN ("copy-file", Fcopy_file, 2, 4,
|
|
1591 "fCopy file: \nFCopy %s to file: \np\nP", /*
|
0
|
1592 Copy FILE to NEWNAME. Both args must be strings.
|
|
1593 Signals a `file-already-exists' error if file NEWNAME already exists,
|
|
1594 unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
|
|
1595 A number as third arg means request confirmation if NEWNAME already exists.
|
|
1596 This is what happens in interactive use with M-x.
|
|
1597 Fourth arg KEEP-TIME non-nil means give the new file the same
|
|
1598 last-modified time as the old one. (This works on only some systems.)
|
|
1599 A prefix arg makes KEEP-TIME non-nil.
|
20
|
1600 */
|
74
|
1601 (filename, newname, ok_if_already_exists, keep_time))
|
0
|
1602 {
|
120
|
1603 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1604 int ifd, ofd, n;
|
|
1605 char buf[16 * 1024];
|
|
1606 struct stat st, out_st;
|
|
1607 Lisp_Object handler;
|
|
1608 int speccount = specpdl_depth ();
|
|
1609 struct gcpro gcpro1, gcpro2;
|
|
1610 /* Lisp_Object args[6]; */
|
|
1611 int input_file_statable_p;
|
|
1612
|
|
1613 GCPRO2 (filename, newname);
|
|
1614 CHECK_STRING (filename);
|
|
1615 CHECK_STRING (newname);
|
|
1616 filename = Fexpand_file_name (filename, Qnil);
|
|
1617 newname = Fexpand_file_name (newname, Qnil);
|
|
1618
|
|
1619 /* If the input file name has special constructs in it,
|
|
1620 call the corresponding file handler. */
|
|
1621 handler = Ffind_file_name_handler (filename, Qcopy_file);
|
|
1622 /* Likewise for output file name. */
|
|
1623 if (NILP (handler))
|
|
1624 handler = Ffind_file_name_handler (newname, Qcopy_file);
|
|
1625 if (!NILP (handler))
|
|
1626 {
|
|
1627 UNGCPRO;
|
|
1628 return call5 (handler, Qcopy_file, filename, newname,
|
74
|
1629 ok_if_already_exists, keep_time);
|
0
|
1630 }
|
|
1631
|
|
1632 /* When second argument is a directory, copy the file into it.
|
|
1633 (copy-file "foo" "bar/") == (copy-file "foo" "bar/foo")
|
|
1634 */
|
|
1635 if (!NILP (Ffile_directory_p (newname)))
|
|
1636 {
|
|
1637 Lisp_Object args[3];
|
|
1638 struct gcpro ngcpro1;
|
|
1639 int i = 1;
|
|
1640
|
151
|
1641 args[0] = newname;
|
0
|
1642 args[1] = Qnil; args[2] = Qnil;
|
151
|
1643 NGCPRO1 (*args);
|
0
|
1644 ngcpro1.nvars = 3;
|
82
|
1645 if (XSTRING_BYTE (newname, XSTRING_LENGTH (newname) - 1) != '/')
|
0
|
1646 args[i++] = build_string ("/");
|
|
1647 args[i++] = Ffile_name_nondirectory (filename);
|
|
1648 newname = Fconcat (i, args);
|
|
1649 NUNGCPRO;
|
|
1650 }
|
|
1651
|
|
1652 if (NILP (ok_if_already_exists)
|
|
1653 || INTP (ok_if_already_exists))
|
|
1654 barf_or_query_if_file_exists (newname, "copy to it",
|
|
1655 INTP (ok_if_already_exists), &out_st);
|
16
|
1656 else if (stat ((CONST char *) XSTRING_DATA (newname), &out_st) < 0)
|
0
|
1657 out_st.st_mode = 0;
|
|
1658
|
251
|
1659 ifd = interruptible_open ((char *) XSTRING_DATA (filename), O_RDONLY | OPEN_BINARY, 0);
|
0
|
1660 if (ifd < 0)
|
227
|
1661 report_file_error ("Opening input file", list1 (filename));
|
0
|
1662
|
|
1663 record_unwind_protect (close_file_unwind, make_int (ifd));
|
|
1664
|
|
1665 /* We can only copy regular files and symbolic links. Other files are not
|
|
1666 copyable by us. */
|
|
1667 input_file_statable_p = (fstat (ifd, &st) >= 0);
|
|
1668
|
|
1669 #ifndef DOS_NT
|
|
1670 if (out_st.st_mode != 0
|
|
1671 && st.st_dev == out_st.st_dev && st.st_ino == out_st.st_ino)
|
|
1672 {
|
|
1673 errno = 0;
|
|
1674 report_file_error ("Input and output files are the same",
|
227
|
1675 list2 (filename, newname));
|
0
|
1676 }
|
|
1677 #endif
|
|
1678
|
|
1679 #if defined (S_ISREG) && defined (S_ISLNK)
|
|
1680 if (input_file_statable_p)
|
|
1681 {
|
|
1682 if (!(S_ISREG (st.st_mode))
|
|
1683 /* XEmacs: have to allow S_ISCHR in order to copy /dev/null */
|
|
1684 #ifdef S_ISCHR
|
|
1685 && !(S_ISCHR (st.st_mode))
|
|
1686 #endif
|
|
1687 && !(S_ISLNK (st.st_mode)))
|
|
1688 {
|
|
1689 #if defined (EISDIR)
|
|
1690 /* Get a better looking error message. */
|
|
1691 errno = EISDIR;
|
|
1692 #endif /* EISDIR */
|
227
|
1693 report_file_error ("Non-regular file", list1 (filename));
|
0
|
1694 }
|
|
1695 }
|
|
1696 #endif /* S_ISREG && S_ISLNK */
|
|
1697
|
|
1698 #ifdef MSDOS
|
|
1699 /* System's default file type was set to binary by _fmode in emacs.c. */
|
16
|
1700 ofd = creat ((char *) XSTRING_DATA (newname), S_IREAD | S_IWRITE);
|
0
|
1701 #else /* not MSDOS */
|
251
|
1702 ofd = open( (char *) XSTRING_DATA (newname),
|
|
1703 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, CREAT_MODE);
|
0
|
1704 #endif /* not MSDOS */
|
|
1705 if (ofd < 0)
|
|
1706 report_file_error ("Opening output file", list1 (newname));
|
|
1707
|
|
1708 {
|
|
1709 Lisp_Object ofd_locative = noseeum_cons (make_int (ofd), Qnil);
|
|
1710
|
|
1711 record_unwind_protect (close_file_unwind, ofd_locative);
|
|
1712
|
|
1713 while ((n = read_allowing_quit (ifd, buf, sizeof (buf))) > 0)
|
|
1714 {
|
|
1715 if (write_allowing_quit (ofd, buf, n) != n)
|
|
1716 report_file_error ("I/O error", list1 (newname));
|
|
1717 }
|
|
1718
|
|
1719 /* Closing the output clobbers the file times on some systems. */
|
|
1720 if (close (ofd) < 0)
|
227
|
1721 report_file_error ("I/O error", list1 (newname));
|
0
|
1722
|
|
1723 if (input_file_statable_p)
|
|
1724 {
|
74
|
1725 if (!NILP (keep_time))
|
0
|
1726 {
|
|
1727 EMACS_TIME atime, mtime;
|
|
1728 EMACS_SET_SECS_USECS (atime, st.st_atime, 0);
|
|
1729 EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
|
16
|
1730 if (set_file_times ((char *) XSTRING_DATA (newname), atime,
|
0
|
1731 mtime))
|
227
|
1732 report_file_error ("I/O error", list1 (newname));
|
0
|
1733 }
|
|
1734 #ifndef MSDOS
|
16
|
1735 chmod ((CONST char *) XSTRING_DATA (newname),
|
0
|
1736 st.st_mode & 07777);
|
|
1737 #else /* MSDOS */
|
|
1738 #if defined (__DJGPP__) && __DJGPP__ > 1
|
|
1739 /* In DJGPP v2.0 and later, fstat usually returns true file mode bits,
|
|
1740 and if it can't, it tells so. Otherwise, under MSDOS we usually
|
|
1741 get only the READ bit, which will make the copied file read-only,
|
|
1742 so it's better not to chmod at all. */
|
|
1743 if ((_djstat_flags & _STFAIL_WRITEBIT) == 0)
|
16
|
1744 chmod ((char *) XSTRING_DATA (newname), st.st_mode & 07777);
|
0
|
1745 #endif /* DJGPP version 2 or newer */
|
|
1746 #endif /* MSDOS */
|
|
1747 }
|
|
1748
|
|
1749 /* We'll close it by hand */
|
|
1750 XCAR (ofd_locative) = Qnil;
|
|
1751
|
|
1752 /* Close ifd */
|
|
1753 unbind_to (speccount, Qnil);
|
|
1754 }
|
|
1755
|
|
1756 UNGCPRO;
|
|
1757 return Qnil;
|
|
1758 }
|
|
1759
|
20
|
1760 DEFUN ("make-directory-internal", Fmake_directory_internal, 1, 1, 0, /*
|
0
|
1761 Create a directory. One argument, a file name string.
|
20
|
1762 */
|
173
|
1763 (dirname_))
|
0
|
1764 {
|
120
|
1765 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1766 char dir [MAXPATHLEN];
|
|
1767 Lisp_Object handler;
|
|
1768 struct gcpro gcpro1;
|
151
|
1769
|
173
|
1770 CHECK_STRING (dirname_);
|
|
1771 dirname_ = Fexpand_file_name (dirname_, Qnil);
|
|
1772
|
|
1773 GCPRO1 (dirname_);
|
|
1774 handler = Ffind_file_name_handler (dirname_, Qmake_directory_internal);
|
0
|
1775 UNGCPRO;
|
|
1776 if (!NILP (handler))
|
173
|
1777 return (call2 (handler, Qmake_directory_internal, dirname_));
|
|
1778
|
|
1779 if (XSTRING_LENGTH (dirname_) > (sizeof (dir) - 1))
|
0
|
1780 {
|
|
1781 return Fsignal (Qfile_error,
|
|
1782 list3 (build_translated_string ("Creating directory"),
|
|
1783 build_translated_string ("pathame too long"),
|
173
|
1784 dirname_));
|
0
|
1785 }
|
173
|
1786 strncpy (dir, (char *) XSTRING_DATA (dirname_),
|
|
1787 XSTRING_LENGTH (dirname_) + 1);
|
0
|
1788
|
173
|
1789 if (dir [XSTRING_LENGTH (dirname_) - 1] == '/')
|
|
1790 dir [XSTRING_LENGTH (dirname_) - 1] = 0;
|
0
|
1791
|
|
1792 #ifdef WINDOWSNT
|
|
1793 if (mkdir (dir) != 0)
|
|
1794 #else
|
|
1795 if (mkdir (dir, 0777) != 0)
|
|
1796 #endif
|
173
|
1797 report_file_error ("Creating directory", list1 (dirname_));
|
0
|
1798
|
|
1799 return Qnil;
|
|
1800 }
|
|
1801
|
20
|
1802 DEFUN ("delete-directory", Fdelete_directory, 1, 1, "FDelete directory: ", /*
|
0
|
1803 Delete a directory. One argument, a file name or directory name string.
|
20
|
1804 */
|
173
|
1805 (dirname_))
|
0
|
1806 {
|
120
|
1807 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1808 Lisp_Object handler;
|
|
1809 struct gcpro gcpro1;
|
151
|
1810
|
173
|
1811 CHECK_STRING (dirname_);
|
|
1812
|
|
1813 GCPRO1 (dirname_);
|
|
1814 dirname_ = Fexpand_file_name (dirname_, Qnil);
|
|
1815 dirname_ = Fdirectory_file_name (dirname_);
|
|
1816
|
|
1817 handler = Ffind_file_name_handler (dirname_, Qdelete_directory);
|
0
|
1818 UNGCPRO;
|
|
1819 if (!NILP (handler))
|
173
|
1820 return (call2 (handler, Qdelete_directory, dirname_));
|
|
1821
|
|
1822 if (rmdir ((char *) XSTRING_DATA (dirname_)) != 0)
|
|
1823 report_file_error ("Removing directory", list1 (dirname_));
|
0
|
1824
|
|
1825 return Qnil;
|
|
1826 }
|
|
1827
|
20
|
1828 DEFUN ("delete-file", Fdelete_file, 1, 1, "fDelete file: ", /*
|
0
|
1829 Delete specified file. One argument, a file name string.
|
|
1830 If file has multiple names, it continues to exist with the other names.
|
20
|
1831 */
|
|
1832 (filename))
|
0
|
1833 {
|
120
|
1834 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1835 Lisp_Object handler;
|
|
1836 struct gcpro gcpro1;
|
151
|
1837
|
0
|
1838 CHECK_STRING (filename);
|
|
1839 filename = Fexpand_file_name (filename, Qnil);
|
|
1840
|
114
|
1841 GCPRO1 (filename);
|
0
|
1842 handler = Ffind_file_name_handler (filename, Qdelete_file);
|
|
1843 UNGCPRO;
|
|
1844 if (!NILP (handler))
|
|
1845 return call2 (handler, Qdelete_file, filename);
|
|
1846
|
16
|
1847 if (0 > unlink ((char *) XSTRING_DATA (filename)))
|
0
|
1848 report_file_error ("Removing old name", list1 (filename));
|
|
1849 return Qnil;
|
|
1850 }
|
|
1851
|
|
1852 static Lisp_Object
|
|
1853 internal_delete_file_1 (Lisp_Object ignore, Lisp_Object ignore2)
|
|
1854 {
|
|
1855 return Qt;
|
|
1856 }
|
|
1857
|
|
1858 /* Delete file FILENAME, returning 1 if successful and 0 if failed. */
|
|
1859
|
|
1860 int
|
|
1861 internal_delete_file (Lisp_Object filename)
|
|
1862 {
|
120
|
1863 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1864 return NILP (condition_case_1 (Qt, Fdelete_file, filename,
|
|
1865 internal_delete_file_1, Qnil));
|
|
1866 }
|
|
1867
|
20
|
1868 DEFUN ("rename-file", Frename_file, 2, 3,
|
|
1869 "fRename file: \nFRename %s to file: \np", /*
|
0
|
1870 Rename FILE as NEWNAME. Both args strings.
|
|
1871 If file has names other than FILE, it continues to have those names.
|
|
1872 Signals a `file-already-exists' error if a file NEWNAME already exists
|
|
1873 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
|
|
1874 A number as third arg means request confirmation if NEWNAME already exists.
|
|
1875 This is what happens in interactive use with M-x.
|
20
|
1876 */
|
|
1877 (filename, newname, ok_if_already_exists))
|
0
|
1878 {
|
120
|
1879 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1880 Lisp_Object handler;
|
|
1881 struct gcpro gcpro1, gcpro2;
|
|
1882
|
|
1883 GCPRO2 (filename, newname);
|
|
1884 CHECK_STRING (filename);
|
|
1885 CHECK_STRING (newname);
|
|
1886 filename = Fexpand_file_name (filename, Qnil);
|
|
1887 newname = Fexpand_file_name (newname, Qnil);
|
|
1888
|
|
1889 /* If the file name has special constructs in it,
|
|
1890 call the corresponding file handler. */
|
|
1891 handler = Ffind_file_name_handler (filename, Qrename_file);
|
|
1892 if (NILP (handler))
|
|
1893 handler = Ffind_file_name_handler (newname, Qrename_file);
|
|
1894 if (!NILP (handler))
|
|
1895 {
|
|
1896 UNGCPRO;
|
|
1897 return call4 (handler, Qrename_file,
|
|
1898 filename, newname, ok_if_already_exists);
|
|
1899 }
|
|
1900
|
|
1901 /* When second argument is a directory, rename the file into it.
|
|
1902 (rename-file "foo" "bar/") == (rename-file "foo" "bar/foo")
|
|
1903 */
|
|
1904 if (!NILP (Ffile_directory_p (newname)))
|
|
1905 {
|
|
1906 Lisp_Object args[3];
|
|
1907 struct gcpro ngcpro1;
|
|
1908 int i = 1;
|
|
1909
|
151
|
1910 args[0] = newname;
|
0
|
1911 args[1] = Qnil; args[2] = Qnil;
|
151
|
1912 NGCPRO1 (*args);
|
0
|
1913 ngcpro1.nvars = 3;
|
16
|
1914 if (XSTRING_BYTE (newname, XSTRING_LENGTH (newname) - 1) != '/')
|
0
|
1915 args[i++] = build_string ("/");
|
|
1916 args[i++] = Ffile_name_nondirectory (filename);
|
|
1917 newname = Fconcat (i, args);
|
|
1918 NUNGCPRO;
|
|
1919 }
|
|
1920
|
|
1921 if (NILP (ok_if_already_exists)
|
|
1922 || INTP (ok_if_already_exists))
|
|
1923 barf_or_query_if_file_exists (newname, "rename to it",
|
|
1924 INTP (ok_if_already_exists), 0);
|
|
1925
|
211
|
1926 /* Syncing with FSF 19.34.6 note: FSF does not have conditional code for
|
|
1927 WINDOWSNT here; I've removed it. --marcpa */
|
|
1928
|
0
|
1929 /* FSFmacs only calls rename() here under BSD 4.1, and calls
|
|
1930 link() and unlink() otherwise, but that's bogus. Sometimes
|
|
1931 rename() succeeds where link()/unlink() fail, and we have
|
|
1932 configure check for rename() and emulate using link()/unlink()
|
|
1933 if necessary. */
|
16
|
1934 if (0 > rename ((char *) XSTRING_DATA (filename),
|
|
1935 (char *) XSTRING_DATA (newname)))
|
0
|
1936 {
|
|
1937 if (errno == EXDEV)
|
|
1938 {
|
|
1939 Fcopy_file (filename, newname,
|
|
1940 /* We have already prompted if it was an integer,
|
|
1941 so don't have copy-file prompt again. */
|
|
1942 ((NILP (ok_if_already_exists)) ? Qnil : Qt),
|
|
1943 Qt);
|
|
1944 Fdelete_file (filename);
|
|
1945 }
|
|
1946 else
|
|
1947 {
|
|
1948 report_file_error ("Renaming", list2 (filename, newname));
|
|
1949 }
|
|
1950 }
|
|
1951 UNGCPRO;
|
|
1952 return Qnil;
|
|
1953 }
|
|
1954
|
20
|
1955 DEFUN ("add-name-to-file", Fadd_name_to_file, 2, 3,
|
|
1956 "fAdd name to file: \nFName to add to %s: \np", /*
|
0
|
1957 Give FILE additional name NEWNAME. Both args strings.
|
|
1958 Signals a `file-already-exists' error if a file NEWNAME already exists
|
|
1959 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
|
|
1960 A number as third arg means request confirmation if NEWNAME already exists.
|
|
1961 This is what happens in interactive use with M-x.
|
20
|
1962 */
|
|
1963 (filename, newname, ok_if_already_exists))
|
0
|
1964 {
|
120
|
1965 /* This function can GC. GC checked 1997.04.06. */
|
0
|
1966 Lisp_Object handler;
|
|
1967 struct gcpro gcpro1, gcpro2;
|
|
1968
|
|
1969 GCPRO2 (filename, newname);
|
|
1970 CHECK_STRING (filename);
|
|
1971 CHECK_STRING (newname);
|
|
1972 filename = Fexpand_file_name (filename, Qnil);
|
|
1973 newname = Fexpand_file_name (newname, Qnil);
|
|
1974
|
|
1975 /* If the file name has special constructs in it,
|
|
1976 call the corresponding file handler. */
|
|
1977 handler = Ffind_file_name_handler (filename, Qadd_name_to_file);
|
|
1978 if (!NILP (handler))
|
|
1979 RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, filename,
|
|
1980 newname, ok_if_already_exists));
|
|
1981
|
|
1982 /* If the new name has special constructs in it,
|
|
1983 call the corresponding file handler. */
|
|
1984 handler = Ffind_file_name_handler (newname, Qadd_name_to_file);
|
|
1985 if (!NILP (handler))
|
|
1986 RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, filename,
|
|
1987 newname, ok_if_already_exists));
|
|
1988
|
|
1989 if (NILP (ok_if_already_exists)
|
|
1990 || INTP (ok_if_already_exists))
|
|
1991 barf_or_query_if_file_exists (newname, "make it a new name",
|
|
1992 INTP (ok_if_already_exists), 0);
|
211
|
1993 /* Syncing with FSF 19.34.6 note: FSF does not report a file error
|
|
1994 on NT here. --marcpa */
|
213
|
1995 /* But FSF #defines link as sys_link which is supplied in nt.c. We can't do
|
|
1996 that because sysfile.h defines sys_link depending on ENCAPSULATE_LINK.
|
|
1997 Reverted to previous behaviour pending a working fix. (jhar) */
|
|
1998 #if defined(WINDOWSNT)
|
0
|
1999 /* Windows does not support this operation. */
|
|
2000 report_file_error ("Adding new name", Flist (2, &filename));
|
213
|
2001 #else /* not defined(WINDOWSNT) */
|
0
|
2002
|
16
|
2003 unlink ((char *) XSTRING_DATA (newname));
|
|
2004 if (0 > link ((char *) XSTRING_DATA (filename),
|
|
2005 (char *) XSTRING_DATA (newname)))
|
0
|
2006 {
|
|
2007 report_file_error ("Adding new name",
|
|
2008 list2 (filename, newname));
|
|
2009 }
|
213
|
2010 #endif /* defined(WINDOWSNT) */
|
0
|
2011
|
|
2012 UNGCPRO;
|
|
2013 return Qnil;
|
|
2014 }
|
|
2015
|
|
2016 #ifdef S_IFLNK
|
20
|
2017 DEFUN ("make-symbolic-link", Fmake_symbolic_link, 2, 3,
|
|
2018 "FMake symbolic link to file: \nFMake symbolic link to file %s: \np", /*
|
0
|
2019 Make a symbolic link to FILENAME, named LINKNAME. Both args strings.
|
|
2020 Signals a `file-already-exists' error if a file LINKNAME already exists
|
|
2021 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
|
|
2022 A number as third arg means request confirmation if LINKNAME already exists.
|
|
2023 This happens for interactive use with M-x.
|
20
|
2024 */
|
|
2025 (filename, linkname, ok_if_already_exists))
|
0
|
2026 {
|
120
|
2027 /* This function can GC. GC checked 1997.06.04. */
|
0
|
2028 Lisp_Object handler;
|
|
2029 struct gcpro gcpro1, gcpro2;
|
|
2030
|
|
2031 GCPRO2 (filename, linkname);
|
|
2032 CHECK_STRING (filename);
|
|
2033 CHECK_STRING (linkname);
|
|
2034 /* If the link target has a ~, we must expand it to get
|
|
2035 a truly valid file name. Otherwise, do not expand;
|
|
2036 we want to permit links to relative file names. */
|
211
|
2037 if (XSTRING_BYTE (filename, 0) == '~')
|
0
|
2038 filename = Fexpand_file_name (filename, Qnil);
|
|
2039 linkname = Fexpand_file_name (linkname, Qnil);
|
|
2040
|
|
2041 /* If the file name has special constructs in it,
|
|
2042 call the corresponding file handler. */
|
|
2043 handler = Ffind_file_name_handler (filename, Qmake_symbolic_link);
|
|
2044 if (!NILP (handler))
|
|
2045 RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, linkname,
|
|
2046 ok_if_already_exists));
|
|
2047
|
|
2048 /* If the new link name has special constructs in it,
|
|
2049 call the corresponding file handler. */
|
|
2050 handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link);
|
|
2051 if (!NILP (handler))
|
|
2052 RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename,
|
|
2053 linkname, ok_if_already_exists));
|
|
2054
|
|
2055 if (NILP (ok_if_already_exists)
|
|
2056 || INTP (ok_if_already_exists))
|
|
2057 barf_or_query_if_file_exists (linkname, "make it a link",
|
|
2058 INTP (ok_if_already_exists), 0);
|
|
2059
|
16
|
2060 unlink ((char *) XSTRING_DATA (linkname));
|
|
2061 if (0 > symlink ((char *) XSTRING_DATA (filename),
|
|
2062 (char *) XSTRING_DATA (linkname)))
|
0
|
2063 {
|
|
2064 report_file_error ("Making symbolic link",
|
|
2065 list2 (filename, linkname));
|
|
2066 }
|
|
2067 UNGCPRO;
|
|
2068 return Qnil;
|
|
2069 }
|
|
2070 #endif /* S_IFLNK */
|
|
2071
|
|
2072 #ifdef HPUX_NET
|
|
2073
|
20
|
2074 DEFUN ("sysnetunam", Fsysnetunam, 2, 2, 0, /*
|
0
|
2075 Open a network connection to PATH using LOGIN as the login string.
|
20
|
2076 */
|
|
2077 (path, login))
|
0
|
2078 {
|
|
2079 int netresult;
|
151
|
2080
|
0
|
2081 CHECK_STRING (path);
|
151
|
2082 CHECK_STRING (login);
|
0
|
2083
|
|
2084 /* netunam, being a strange-o system call only used once, is not
|
|
2085 encapsulated. */
|
|
2086 {
|
|
2087 char *path_ext;
|
|
2088 char *login_ext;
|
|
2089
|
|
2090 GET_C_STRING_FILENAME_DATA_ALLOCA (path, path_ext);
|
|
2091 GET_C_STRING_EXT_DATA_ALLOCA (login, FORMAT_OS, login_ext);
|
151
|
2092
|
0
|
2093 netresult = netunam (path_ext, login_ext);
|
|
2094 }
|
|
2095
|
|
2096 if (netresult == -1)
|
|
2097 return Qnil;
|
|
2098 else
|
|
2099 return Qt;
|
|
2100 }
|
|
2101 #endif /* HPUX_NET */
|
|
2102
|
20
|
2103 DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, 1, 1, 0, /*
|
0
|
2104 Return t if file FILENAME specifies an absolute path name.
|
|
2105 On Unix, this is a name starting with a `/' or a `~'.
|
20
|
2106 */
|
|
2107 (filename))
|
0
|
2108 {
|
116
|
2109 /* This function does not GC */
|
0
|
2110 Bufbyte *ptr;
|
|
2111
|
|
2112 CHECK_STRING (filename);
|
16
|
2113 ptr = XSTRING_DATA (filename);
|
0
|
2114 if (IS_DIRECTORY_SEP (*ptr) || *ptr == '~'
|
|
2115 #ifdef DOS_NT
|
211
|
2116 || (IS_DRIVE (*ptr) && ptr[1] == ':' && IS_DIRECTORY_SEP (ptr[2]))
|
0
|
2117 #endif
|
|
2118 )
|
|
2119 return Qt;
|
|
2120 else
|
|
2121 return Qnil;
|
|
2122 }
|
|
2123
|
|
2124 /* Return nonzero if file FILENAME exists and can be executed. */
|
|
2125
|
|
2126 static int
|
|
2127 check_executable (char *filename)
|
|
2128 {
|
|
2129 #ifdef DOS_NT
|
|
2130 int len = strlen (filename);
|
|
2131 char *suffix;
|
|
2132 struct stat st;
|
|
2133 if (stat (filename, &st) < 0)
|
|
2134 return 0;
|
211
|
2135 #if defined (WINDOWSNT)
|
|
2136 return ((st.st_mode & S_IEXEC) != 0);
|
|
2137 #else
|
0
|
2138 return (S_ISREG (st.st_mode)
|
|
2139 && len >= 5
|
|
2140 && (stricmp ((suffix = filename + len-4), ".com") == 0
|
|
2141 || stricmp (suffix, ".exe") == 0
|
|
2142 || stricmp (suffix, ".bat") == 0)
|
|
2143 || (st.st_mode & S_IFMT) == S_IFDIR);
|
211
|
2144 #endif /* not WINDOWSNT */
|
0
|
2145 #else /* not DOS_NT */
|
|
2146 #ifdef HAVE_EACCESS
|
183
|
2147 return eaccess (filename, 1) >= 0;
|
0
|
2148 #else
|
|
2149 /* Access isn't quite right because it uses the real uid
|
|
2150 and we really want to test with the effective uid.
|
|
2151 But Unix doesn't give us a right way to do it. */
|
183
|
2152 return access (filename, 1) >= 0;
|
|
2153 #endif /* HAVE_EACCESS */
|
0
|
2154 #endif /* not DOS_NT */
|
|
2155 }
|
|
2156
|
|
2157 /* Return nonzero if file FILENAME exists and can be written. */
|
|
2158
|
|
2159 static int
|
|
2160 check_writable (CONST char *filename)
|
|
2161 {
|
|
2162 #ifdef MSDOS
|
|
2163 struct stat st;
|
|
2164 if (stat (filename, &st) < 0)
|
|
2165 return 0;
|
|
2166 return (st.st_mode & S_IWRITE || (st.st_mode & S_IFMT) == S_IFDIR);
|
|
2167 #else /* not MSDOS */
|
|
2168 #ifdef HAVE_EACCESS
|
|
2169 return (eaccess (filename, 2) >= 0);
|
|
2170 #else
|
|
2171 /* Access isn't quite right because it uses the real uid
|
|
2172 and we really want to test with the effective uid.
|
|
2173 But Unix doesn't give us a right way to do it.
|
|
2174 Opening with O_WRONLY could work for an ordinary file,
|
|
2175 but would lose for directories. */
|
|
2176 return (access (filename, 2) >= 0);
|
|
2177 #endif
|
|
2178 #endif /* not MSDOS */
|
|
2179 }
|
|
2180
|
20
|
2181 DEFUN ("file-exists-p", Ffile_exists_p, 1, 1, 0, /*
|
0
|
2182 Return t if file FILENAME exists. (This does not mean you can read it.)
|
|
2183 See also `file-readable-p' and `file-attributes'.
|
20
|
2184 */
|
|
2185 (filename))
|
0
|
2186 {
|
116
|
2187 /* This function can call lisp */
|
0
|
2188 Lisp_Object abspath;
|
|
2189 Lisp_Object handler;
|
|
2190 struct stat statbuf;
|
|
2191 struct gcpro gcpro1;
|
151
|
2192
|
0
|
2193 CHECK_STRING (filename);
|
|
2194 abspath = Fexpand_file_name (filename, Qnil);
|
|
2195
|
|
2196 /* If the file name has special constructs in it,
|
|
2197 call the corresponding file handler. */
|
|
2198 GCPRO1 (abspath);
|
|
2199 handler = Ffind_file_name_handler (abspath, Qfile_exists_p);
|
|
2200 UNGCPRO;
|
|
2201 if (!NILP (handler))
|
|
2202 return call2 (handler, Qfile_exists_p, abspath);
|
|
2203
|
16
|
2204 if (stat ((char *) XSTRING_DATA (abspath), &statbuf) >= 0)
|
0
|
2205 return (Qt);
|
|
2206 else
|
|
2207 return (Qnil);
|
|
2208 }
|
|
2209
|
20
|
2210 DEFUN ("file-executable-p", Ffile_executable_p, 1, 1, 0, /*
|
0
|
2211 Return t if FILENAME can be executed by you.
|
|
2212 For a directory, this means you can access files in that directory.
|
20
|
2213 */
|
|
2214 (filename))
|
70
|
2215
|
0
|
2216 {
|
124
|
2217 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2218 Lisp_Object abspath;
|
|
2219 Lisp_Object handler;
|
|
2220 struct gcpro gcpro1;
|
151
|
2221
|
0
|
2222 CHECK_STRING (filename);
|
|
2223 abspath = Fexpand_file_name (filename, Qnil);
|
|
2224
|
|
2225 /* If the file name has special constructs in it,
|
|
2226 call the corresponding file handler. */
|
|
2227 GCPRO1 (abspath);
|
|
2228 handler = Ffind_file_name_handler (abspath, Qfile_executable_p);
|
|
2229 UNGCPRO;
|
|
2230 if (!NILP (handler))
|
|
2231 return call2 (handler, Qfile_executable_p, abspath);
|
|
2232
|
16
|
2233 return (check_executable ((char *) XSTRING_DATA (abspath))
|
0
|
2234 ? Qt : Qnil);
|
|
2235 }
|
|
2236
|
20
|
2237 DEFUN ("file-readable-p", Ffile_readable_p, 1, 1, 0, /*
|
0
|
2238 Return t if file FILENAME exists and you can read it.
|
|
2239 See also `file-exists-p' and `file-attributes'.
|
20
|
2240 */
|
|
2241 (filename))
|
0
|
2242 {
|
225
|
2243 /* This function can GC */
|
|
2244 Lisp_Object abspath = Qnil;
|
0
|
2245 Lisp_Object handler;
|
|
2246 int desc;
|
|
2247 struct gcpro gcpro1;
|
225
|
2248 GCPRO1 (abspath);
|
151
|
2249
|
0
|
2250 CHECK_STRING (filename);
|
|
2251 abspath = Fexpand_file_name (filename, Qnil);
|
|
2252
|
|
2253 /* If the file name has special constructs in it,
|
|
2254 call the corresponding file handler. */
|
|
2255 handler = Ffind_file_name_handler (abspath, Qfile_readable_p);
|
|
2256 if (!NILP (handler))
|
225
|
2257 RETURN_UNGCPRO (call2 (handler, Qfile_readable_p, abspath));
|
0
|
2258
|
211
|
2259 #ifdef DOS_NT
|
|
2260 /* Under MS-DOS and Windows, open does not work for directories. */
|
|
2261 if (access (XSTRING_DATA (abspath), 0) == 0)
|
|
2262 return Qt;
|
|
2263 return Qnil;
|
|
2264 #else /* not DOS_NT */
|
251
|
2265 desc = interruptible_open ((char *) XSTRING_DATA (abspath), O_RDONLY | OPEN_BINARY, 0);
|
225
|
2266 UNGCPRO;
|
0
|
2267 if (desc < 0)
|
|
2268 return Qnil;
|
|
2269 close (desc);
|
|
2270 return Qt;
|
211
|
2271 #endif /* not DOS_NT */
|
0
|
2272 }
|
|
2273
|
|
2274 /* Having this before file-symlink-p mysteriously caused it to be forgotten
|
|
2275 on the RT/PC. */
|
20
|
2276 DEFUN ("file-writable-p", Ffile_writable_p, 1, 1, 0, /*
|
0
|
2277 Return t if file FILENAME can be written or created by you.
|
20
|
2278 */
|
|
2279 (filename))
|
0
|
2280 {
|
124
|
2281 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2282 Lisp_Object abspath, dir;
|
|
2283 Lisp_Object handler;
|
|
2284 struct stat statbuf;
|
|
2285 struct gcpro gcpro1;
|
151
|
2286
|
0
|
2287 CHECK_STRING (filename);
|
|
2288 abspath = Fexpand_file_name (filename, Qnil);
|
|
2289
|
|
2290 /* If the file name has special constructs in it,
|
|
2291 call the corresponding file handler. */
|
|
2292 GCPRO1 (abspath);
|
|
2293 handler = Ffind_file_name_handler (abspath, Qfile_writable_p);
|
|
2294 UNGCPRO;
|
|
2295 if (!NILP (handler))
|
|
2296 return call2 (handler, Qfile_writable_p, abspath);
|
|
2297
|
16
|
2298 if (stat ((char *) XSTRING_DATA (abspath), &statbuf) >= 0)
|
|
2299 return (check_writable ((char *) XSTRING_DATA (abspath))
|
0
|
2300 ? Qt : Qnil);
|
|
2301
|
|
2302
|
114
|
2303 GCPRO1 (abspath);
|
0
|
2304 dir = Ffile_name_directory (abspath);
|
114
|
2305 UNGCPRO;
|
209
|
2306 #ifdef MSDOS
|
0
|
2307 if (!NILP (dir))
|
114
|
2308 {
|
|
2309 GCPRO1(dir);
|
|
2310 dir = Fdirectory_file_name (dir);
|
|
2311 UNGCPRO;
|
|
2312 }
|
209
|
2313 #endif /* MSDOS */
|
16
|
2314 return (check_writable (!NILP (dir) ? (char *) XSTRING_DATA (dir)
|
0
|
2315 : "")
|
|
2316 ? Qt : Qnil);
|
|
2317 }
|
|
2318
|
20
|
2319 DEFUN ("file-symlink-p", Ffile_symlink_p, 1, 1, 0, /*
|
0
|
2320 Return non-nil if file FILENAME is the name of a symbolic link.
|
|
2321 The value is the name of the file to which it is linked.
|
|
2322 Otherwise returns nil.
|
20
|
2323 */
|
|
2324 (filename))
|
0
|
2325 {
|
124
|
2326 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2327 #ifdef S_IFLNK
|
|
2328 char *buf;
|
|
2329 int bufsize;
|
|
2330 int valsize;
|
|
2331 Lisp_Object val;
|
|
2332 Lisp_Object handler;
|
|
2333 struct gcpro gcpro1;
|
151
|
2334
|
0
|
2335 CHECK_STRING (filename);
|
|
2336 filename = Fexpand_file_name (filename, Qnil);
|
|
2337
|
|
2338 /* If the file name has special constructs in it,
|
|
2339 call the corresponding file handler. */
|
116
|
2340 GCPRO1 (filename);
|
0
|
2341 handler = Ffind_file_name_handler (filename, Qfile_symlink_p);
|
116
|
2342 UNGCPRO;
|
0
|
2343 if (!NILP (handler))
|
|
2344 return call2 (handler, Qfile_symlink_p, filename);
|
|
2345
|
|
2346 bufsize = 100;
|
|
2347 while (1)
|
|
2348 {
|
185
|
2349 buf = xnew_array_and_zero (char, bufsize);
|
16
|
2350 valsize = readlink ((char *) XSTRING_DATA (filename),
|
0
|
2351 buf, bufsize);
|
|
2352 if (valsize < bufsize) break;
|
|
2353 /* Buffer was not long enough */
|
|
2354 xfree (buf);
|
|
2355 bufsize *= 2;
|
|
2356 }
|
|
2357 if (valsize == -1)
|
|
2358 {
|
|
2359 xfree (buf);
|
|
2360 return Qnil;
|
|
2361 }
|
|
2362 val = make_string ((Bufbyte *) buf, valsize);
|
|
2363 xfree (buf);
|
|
2364 return val;
|
|
2365 #else /* not S_IFLNK */
|
|
2366 return Qnil;
|
|
2367 #endif /* not S_IFLNK */
|
|
2368 }
|
|
2369
|
20
|
2370 DEFUN ("file-directory-p", Ffile_directory_p, 1, 1, 0, /*
|
0
|
2371 Return t if file FILENAME is the name of a directory as a file.
|
|
2372 A directory name spec may be given instead; then the value is t
|
|
2373 if the directory so specified exists and really is a directory.
|
20
|
2374 */
|
|
2375 (filename))
|
0
|
2376 {
|
124
|
2377 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2378 Lisp_Object abspath;
|
|
2379 struct stat st;
|
|
2380 Lisp_Object handler;
|
|
2381 struct gcpro gcpro1;
|
151
|
2382
|
114
|
2383 GCPRO1 (current_buffer->directory);
|
0
|
2384 abspath = expand_and_dir_to_file (filename,
|
|
2385 current_buffer->directory);
|
|
2386 UNGCPRO;
|
|
2387
|
|
2388 /* If the file name has special constructs in it,
|
|
2389 call the corresponding file handler. */
|
|
2390 GCPRO1 (abspath);
|
|
2391 handler = Ffind_file_name_handler (abspath, Qfile_directory_p);
|
|
2392 UNGCPRO;
|
|
2393 if (!NILP (handler))
|
|
2394 return call2 (handler, Qfile_directory_p, abspath);
|
|
2395
|
16
|
2396 if (stat ((char *) XSTRING_DATA (abspath), &st) < 0)
|
0
|
2397 return Qnil;
|
|
2398 return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
|
|
2399 }
|
|
2400
|
20
|
2401 DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, 1, 1, 0, /*
|
0
|
2402 Return t if file FILENAME is the name of a directory as a file,
|
|
2403 and files in that directory can be opened by you. In order to use a
|
|
2404 directory as a buffer's current directory, this predicate must return true.
|
|
2405 A directory name spec may be given instead; then the value is t
|
|
2406 if the directory so specified exists and really is a readable and
|
|
2407 searchable directory.
|
20
|
2408 */
|
|
2409 (filename))
|
0
|
2410 {
|
124
|
2411 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2412 Lisp_Object handler;
|
|
2413
|
|
2414 /* If the file name has special constructs in it,
|
|
2415 call the corresponding file handler. */
|
|
2416 handler = Ffind_file_name_handler (filename, Qfile_accessible_directory_p);
|
|
2417 if (!NILP (handler))
|
|
2418 return call2 (handler, Qfile_accessible_directory_p,
|
|
2419 filename);
|
|
2420
|
211
|
2421 #if !defined(DOS_NT)
|
0
|
2422 if (NILP (Ffile_directory_p (filename)))
|
|
2423 return (Qnil);
|
116
|
2424 else
|
|
2425 return Ffile_executable_p (filename);
|
211
|
2426 #else
|
|
2427 {
|
|
2428 int tem;
|
|
2429 struct gcpro gcpro1;
|
|
2430 /* It's an unlikely combination, but yes we really do need to gcpro:
|
|
2431 Suppose that file-accessible-directory-p has no handler, but
|
|
2432 file-directory-p does have a handler; this handler causes a GC which
|
|
2433 relocates the string in `filename'; and finally file-directory-p
|
|
2434 returns non-nil. Then we would end up passing a garbaged string
|
|
2435 to file-executable-p. */
|
|
2436 GCPRO1 (filename);
|
|
2437 tem = (NILP (Ffile_directory_p (filename))
|
|
2438 || NILP (Ffile_executable_p (filename)));
|
|
2439 UNGCPRO;
|
|
2440 return tem ? Qnil : Qt;
|
|
2441 }
|
|
2442 #endif /* !defined(DOS_NT) */
|
0
|
2443 }
|
|
2444
|
20
|
2445 DEFUN ("file-regular-p", Ffile_regular_p, 1, 1, 0, /*
|
0
|
2446 "Return t if file FILENAME is the name of a regular file.
|
|
2447 This is the sort of file that holds an ordinary stream of data bytes.
|
20
|
2448 */
|
|
2449 (filename))
|
0
|
2450 {
|
124
|
2451 /* This function can GC. GC checked 1997.04.10. */
|
114
|
2452 Lisp_Object abspath;
|
0
|
2453 struct stat st;
|
|
2454 Lisp_Object handler;
|
114
|
2455 struct gcpro gcpro1;
|
|
2456
|
|
2457 GCPRO1 (current_buffer->directory);
|
0
|
2458 abspath = expand_and_dir_to_file (filename, current_buffer->directory);
|
114
|
2459 UNGCPRO;
|
0
|
2460
|
|
2461 /* If the file name has special constructs in it,
|
|
2462 call the corresponding file handler. */
|
114
|
2463 GCPRO1 (abspath);
|
0
|
2464 handler = Ffind_file_name_handler (abspath, Qfile_regular_p);
|
114
|
2465 UNGCPRO;
|
0
|
2466 if (!NILP (handler))
|
|
2467 return call2 (handler, Qfile_regular_p, abspath);
|
|
2468
|
16
|
2469 if (stat ((char *) XSTRING_DATA (abspath), &st) < 0)
|
0
|
2470 return Qnil;
|
|
2471 return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
|
|
2472 }
|
|
2473
|
20
|
2474 DEFUN ("file-modes", Ffile_modes, 1, 1, 0, /*
|
0
|
2475 Return mode bits of FILE, as an integer.
|
20
|
2476 */
|
|
2477 (filename))
|
0
|
2478 {
|
124
|
2479 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2480 Lisp_Object abspath;
|
|
2481 struct stat st;
|
|
2482 Lisp_Object handler;
|
|
2483 struct gcpro gcpro1;
|
151
|
2484
|
114
|
2485 GCPRO1 (current_buffer->directory);
|
0
|
2486 abspath = expand_and_dir_to_file (filename,
|
|
2487 current_buffer->directory);
|
|
2488 UNGCPRO;
|
|
2489
|
|
2490 /* If the file name has special constructs in it,
|
|
2491 call the corresponding file handler. */
|
|
2492 GCPRO1 (abspath);
|
|
2493 handler = Ffind_file_name_handler (abspath, Qfile_modes);
|
|
2494 UNGCPRO;
|
|
2495 if (!NILP (handler))
|
|
2496 return call2 (handler, Qfile_modes, abspath);
|
|
2497
|
16
|
2498 if (stat ((char *) XSTRING_DATA (abspath), &st) < 0)
|
0
|
2499 return Qnil;
|
211
|
2500 /* Syncing with FSF 19.34.6 note: not in FSF, #if 0'ed out here. */
|
|
2501 #if 0
|
0
|
2502 #ifdef DOS_NT
|
100
|
2503 if (check_executable (XSTRING (abspath)->_data))
|
0
|
2504 st.st_mode |= S_IEXEC;
|
|
2505 #endif /* DOS_NT */
|
211
|
2506 #endif /* 0 */
|
0
|
2507
|
|
2508 return make_int (st.st_mode & 07777);
|
|
2509 }
|
|
2510
|
20
|
2511 DEFUN ("set-file-modes", Fset_file_modes, 2, 2, 0, /*
|
0
|
2512 Set mode bits of FILE to MODE (an integer).
|
|
2513 Only the 12 low bits of MODE are used.
|
20
|
2514 */
|
|
2515 (filename, mode))
|
0
|
2516 {
|
124
|
2517 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2518 Lisp_Object abspath;
|
|
2519 Lisp_Object handler;
|
114
|
2520 struct gcpro gcpro1;
|
151
|
2521
|
114
|
2522 GCPRO1 (current_buffer->directory);
|
0
|
2523 abspath = Fexpand_file_name (filename, current_buffer->directory);
|
116
|
2524 UNGCPRO;
|
|
2525
|
70
|
2526 CHECK_INT (mode);
|
44
|
2527
|
0
|
2528 /* If the file name has special constructs in it,
|
|
2529 call the corresponding file handler. */
|
|
2530 GCPRO1 (abspath);
|
|
2531 handler = Ffind_file_name_handler (abspath, Qset_file_modes);
|
|
2532 UNGCPRO;
|
|
2533 if (!NILP (handler))
|
|
2534 return call3 (handler, Qset_file_modes, abspath, mode);
|
|
2535
|
16
|
2536 if (chmod ((char *) XSTRING_DATA (abspath), XINT (mode)) < 0)
|
227
|
2537 report_file_error ("Doing chmod", list1 (abspath));
|
0
|
2538
|
|
2539 return Qnil;
|
|
2540 }
|
|
2541
|
20
|
2542 DEFUN ("set-default-file-modes", Fset_default_file_modes, 1, 1, 0, /*
|
0
|
2543 Set the file permission bits for newly created files.
|
|
2544 MASK should be an integer; if a permission's bit in MASK is 1,
|
|
2545 subsequently created files will not have that permission enabled.
|
|
2546 Only the low 9 bits are used.
|
|
2547 This setting is inherited by subprocesses.
|
20
|
2548 */
|
|
2549 (mode))
|
0
|
2550 {
|
|
2551 CHECK_INT (mode);
|
151
|
2552
|
0
|
2553 umask ((~ XINT (mode)) & 0777);
|
|
2554
|
|
2555 return Qnil;
|
|
2556 }
|
|
2557
|
20
|
2558 DEFUN ("default-file-modes", Fdefault_file_modes, 0, 0, 0, /*
|
0
|
2559 Return the default file protection for created files.
|
|
2560 The umask value determines which permissions are enabled in newly
|
|
2561 created files. If a permission's bit in the umask is 1, subsequently
|
|
2562 created files will not have that permission enabled.
|
20
|
2563 */
|
|
2564 ())
|
0
|
2565 {
|
|
2566 int mode;
|
|
2567
|
|
2568 mode = umask (0);
|
|
2569 umask (mode);
|
|
2570
|
|
2571 return make_int ((~ mode) & 0777);
|
|
2572 }
|
|
2573
|
20
|
2574 DEFUN ("unix-sync", Funix_sync, 0, 0, "", /*
|
0
|
2575 Tell Unix to finish all pending disk updates.
|
20
|
2576 */
|
|
2577 ())
|
0
|
2578 {
|
100
|
2579 #ifndef WINDOWSNT
|
0
|
2580 sync ();
|
100
|
2581 #endif
|
0
|
2582 return Qnil;
|
|
2583 }
|
|
2584
|
|
2585
|
20
|
2586 DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, 2, 2, 0, /*
|
0
|
2587 Return t if file FILE1 is newer than file FILE2.
|
|
2588 If FILE1 does not exist, the answer is nil;
|
|
2589 otherwise, if FILE2 does not exist, the answer is t.
|
20
|
2590 */
|
|
2591 (file1, file2))
|
0
|
2592 {
|
124
|
2593 /* This function can GC. GC checked 1997.04.10. */
|
0
|
2594 Lisp_Object abspath1, abspath2;
|
|
2595 struct stat st;
|
|
2596 int mtime1;
|
|
2597 Lisp_Object handler;
|
114
|
2598 struct gcpro gcpro1, gcpro2, gcpro3;
|
0
|
2599
|
|
2600 CHECK_STRING (file1);
|
|
2601 CHECK_STRING (file2);
|
|
2602
|
|
2603 abspath1 = Qnil;
|
114
|
2604 abspath2 = Qnil;
|
|
2605
|
|
2606 GCPRO3 (abspath1, abspath2, current_buffer->directory);
|
|
2607 abspath1 = expand_and_dir_to_file (file1, current_buffer->directory);
|
|
2608 abspath2 = expand_and_dir_to_file (file2, current_buffer->directory);
|
0
|
2609
|
|
2610 /* If the file name has special constructs in it,
|
|
2611 call the corresponding file handler. */
|
|
2612 handler = Ffind_file_name_handler (abspath1, Qfile_newer_than_file_p);
|
|
2613 if (NILP (handler))
|
|
2614 handler = Ffind_file_name_handler (abspath2, Qfile_newer_than_file_p);
|
|
2615 UNGCPRO;
|
|
2616 if (!NILP (handler))
|
|
2617 return call3 (handler, Qfile_newer_than_file_p, abspath1,
|
|
2618 abspath2);
|
|
2619
|
16
|
2620 if (stat ((char *) XSTRING_DATA (abspath1), &st) < 0)
|
0
|
2621 return Qnil;
|
|
2622
|
|
2623 mtime1 = st.st_mtime;
|
|
2624
|
16
|
2625 if (stat ((char *) XSTRING_DATA (abspath2), &st) < 0)
|
0
|
2626 return Qt;
|
|
2627
|
|
2628 return (mtime1 > st.st_mtime) ? Qt : Qnil;
|
|
2629 }
|
|
2630
|
|
2631
|
|
2632 #ifdef DOS_NT
|
|
2633 Lisp_Object Qfind_buffer_file_type;
|
211
|
2634
|
|
2635 /* Return 1 if buffer is text, 0 if binary. */
|
|
2636 static int
|
|
2637 decide_buffer_type (unsigned char * buffer, int nbytes)
|
|
2638 {
|
|
2639 /* Buffer is binary if we find any LF chars not preceeded by CR or if
|
|
2640 the buffer doesn't contain at least 1 line. */
|
|
2641 unsigned lines = 0;
|
|
2642 unsigned char *p, *q;
|
|
2643
|
|
2644 for (p = buffer; nbytes > 0 && (q = memchr (p, '\n', nbytes)) != NULL;
|
|
2645 p = q + 1 )
|
|
2646 {
|
|
2647 nbytes -= (q + 1 - p);
|
|
2648 lines++;
|
|
2649 if (q > buffer && q[-1] != '\r')
|
|
2650 return 0;
|
|
2651 }
|
|
2652
|
|
2653 /* If we haven't seen any line endings yet, return -1 (meaning type is
|
|
2654 undecided) so we can examine the next bufferful as well. */
|
|
2655 return (lines > 0) ? 1 : -1;
|
|
2656 }
|
|
2657
|
|
2658 /* XEmacs addition: like decide_buffer_type(), but working on a XEmacs buffer:
|
|
2659 first arg is a byte index position instead of a char pointer;
|
|
2660 we check each char sequentially. --marcpa */
|
|
2661 static int
|
|
2662 buf_decide_buffer_type (struct buffer *buf, Bytind start, int nbytes)
|
|
2663 {
|
|
2664 /* Buffer is binary if we find any LF chars not preceeded by CR or if
|
|
2665 the buffer doesn't contain at least 1 line. */
|
|
2666 unsigned lines = 0;
|
|
2667 Bytind cur = start;
|
|
2668
|
|
2669 while (nbytes)
|
|
2670 {
|
|
2671 if (BI_BUF_FETCH_CHAR(buf, cur) == '\n')
|
|
2672 {
|
|
2673 lines++;
|
|
2674 if (cur != start && BI_BUF_FETCH_CHAR(buf, cur - 1) != '\r')
|
|
2675 return 0;
|
|
2676 }
|
|
2677 nbytes--;
|
|
2678 }
|
|
2679
|
|
2680 /* If we haven't seen any line endings yet, return -1 (meaning type is
|
|
2681 undecided) so we can examine the next bufferful as well. */
|
|
2682 return (lines > 0) ? 1 : -1;
|
|
2683 }
|
0
|
2684 #endif /* DOS_NT */
|
|
2685
|
2
|
2686 /* Stack sizes > 2**16 is a good way to elicit compiler bugs */
|
|
2687 /* #define READ_BUF_SIZE (2 << 16) */
|
|
2688 #define READ_BUF_SIZE (1 << 15)
|
0
|
2689
|
20
|
2690 DEFUN ("insert-file-contents-internal",
|
70
|
2691 Finsert_file_contents_internal, 1, 7, 0, /*
|
|
2692 Insert contents of file FILENAME after point; no coding-system frobbing.
|
|
2693 This function is identical to `insert-file-contents' except for the
|
|
2694 handling of the CODESYS and USED-CODESYS arguments under
|
|
2695 XEmacs/Mule. (When Mule support is not present, both functions are
|
|
2696 identical and ignore the CODESYS and USED-CODESYS arguments.)
|
|
2697
|
|
2698 If support for Mule exists in this Emacs, the file is decoded according
|
|
2699 to CODESYS; if omitted, no conversion happens. If USED-CODESYS is non-nil,
|
|
2700 it should be a symbol, and the actual coding system that was used for the
|
|
2701 decoding is stored into it. It will in general be different from CODESYS
|
|
2702 if CODESYS specifies automatic encoding detection or end-of-line detection.
|
|
2703
|
|
2704 Currently BEG and END refer to byte positions (as opposed to character
|
|
2705 positions), even in Mule. (Fixing this is very difficult.)
|
20
|
2706 */
|
70
|
2707 (filename, visit, beg, end, replace, codesys, used_codesys))
|
0
|
2708 {
|
116
|
2709 /* This function can call lisp */
|
|
2710 /* #### dmoore - this function hasn't been checked for gc recently */
|
0
|
2711 struct stat st;
|
|
2712 int fd;
|
|
2713 int saverrno = 0;
|
|
2714 Charcount inserted = 0;
|
|
2715 int speccount;
|
114
|
2716 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
0
|
2717 Lisp_Object handler = Qnil, val;
|
|
2718 int total;
|
|
2719 Bufbyte read_buf[READ_BUF_SIZE];
|
|
2720 int mc_count;
|
|
2721 struct buffer *buf = current_buffer;
|
114
|
2722 Lisp_Object curbuf;
|
0
|
2723 int not_regular = 0;
|
211
|
2724 #ifdef DOS_NT
|
|
2725 int crlf_conversion_required = 0;
|
|
2726 unsigned crlf_count = 0;
|
|
2727 unsigned lf_count = 0;
|
|
2728 #endif
|
0
|
2729
|
|
2730 if (buf->base_buffer && ! NILP (visit))
|
|
2731 error ("Cannot do file visiting in an indirect buffer");
|
|
2732
|
|
2733 /* No need to call Fbarf_if_buffer_read_only() here.
|
|
2734 That's called in begin_multiple_change() or wherever. */
|
|
2735
|
|
2736 val = Qnil;
|
|
2737
|
114
|
2738 /* #### dmoore - should probably check in various places to see if
|
|
2739 curbuf was killed and if so signal an error? */
|
|
2740
|
|
2741 XSETBUFFER (curbuf, buf);
|
|
2742
|
|
2743 GCPRO5 (filename, val, visit, handler, curbuf);
|
151
|
2744
|
0
|
2745 mc_count = (NILP (replace)) ?
|
|
2746 begin_multiple_change (buf, BUF_PT (buf), BUF_PT (buf)) :
|
|
2747 begin_multiple_change (buf, BUF_BEG (buf), BUF_Z (buf));
|
|
2748
|
|
2749 speccount = specpdl_depth (); /* begin_multiple_change also adds
|
|
2750 an unwind_protect */
|
|
2751
|
|
2752 filename = Fexpand_file_name (filename, Qnil);
|
|
2753
|
|
2754 /* If the file name has special constructs in it,
|
|
2755 call the corresponding file handler. */
|
|
2756 handler = Ffind_file_name_handler (filename, Qinsert_file_contents);
|
|
2757 if (!NILP (handler))
|
|
2758 {
|
74
|
2759 val = call6 (handler, Qinsert_file_contents, filename,
|
|
2760 visit, beg, end, replace);
|
0
|
2761 goto handled;
|
|
2762 }
|
|
2763
|
259
|
2764 #ifdef FILE_CODING
|
70
|
2765 if (!NILP (used_codesys))
|
|
2766 CHECK_SYMBOL (used_codesys);
|
|
2767 #endif
|
|
2768
|
0
|
2769 if ( (!NILP (beg) || !NILP (end)) && !NILP (visit) )
|
|
2770 error ("Attempt to visit less than an entire file");
|
151
|
2771
|
0
|
2772 fd = -1;
|
|
2773
|
225
|
2774 if (
|
0
|
2775 #ifndef APOLLO
|
225
|
2776 (stat ((char *) XSTRING_DATA (filename), &st) < 0)
|
0
|
2777 #else /* APOLLO */
|
225
|
2778 /* Don't even bother with interruptible_open. APOLLO sucks. */
|
251
|
2779 ((fd = open ((char *) XSTRING_DATA (filename), O_RDONLY | OPEN_BINARY, 0)) < 0
|
225
|
2780 || fstat (fd, &st) < 0)
|
0
|
2781 #endif /* APOLLO */
|
225
|
2782 )
|
0
|
2783 {
|
|
2784 if (fd >= 0) close (fd);
|
|
2785 badopen:
|
|
2786 if (NILP (visit))
|
227
|
2787 report_file_error ("Opening input file", list1 (filename));
|
0
|
2788 st.st_mtime = -1;
|
|
2789 goto notfound;
|
|
2790 }
|
|
2791
|
|
2792 #ifdef S_IFREG
|
209
|
2793 /* Signal an error if we are accessing a non-regular file, with
|
|
2794 REPLACE, BEG or END being non-nil. */
|
0
|
2795 if (!S_ISREG (st.st_mode))
|
|
2796 {
|
173
|
2797 not_regular = 1;
|
|
2798
|
|
2799 if (!NILP (visit))
|
|
2800 goto notfound;
|
|
2801
|
|
2802 if (!NILP (replace) || !NILP (beg) || !NILP (end))
|
0
|
2803 {
|
|
2804 end_multiple_change (buf, mc_count);
|
|
2805
|
|
2806 return Fsignal (Qfile_error,
|
|
2807 list2 (build_translated_string("not a regular file"),
|
|
2808 filename));
|
|
2809 }
|
|
2810 }
|
183
|
2811 #endif /* S_IFREG */
|
0
|
2812
|
173
|
2813 if (!NILP (beg))
|
|
2814 CHECK_INT (beg);
|
|
2815 else
|
|
2816 beg = Qzero;
|
|
2817
|
|
2818 if (!NILP (end))
|
|
2819 CHECK_INT (end);
|
|
2820
|
0
|
2821 if (fd < 0)
|
225
|
2822 {
|
|
2823 if ((fd = interruptible_open ((char *) XSTRING_DATA (filename),
|
251
|
2824 O_RDONLY | OPEN_BINARY, 0)) < 0)
|
225
|
2825 goto badopen;
|
|
2826 }
|
0
|
2827
|
|
2828 /* Replacement should preserve point as it preserves markers. */
|
|
2829 if (!NILP (replace))
|
|
2830 record_unwind_protect (restore_point_unwind, Fpoint_marker (Qnil, Qnil));
|
|
2831
|
|
2832 record_unwind_protect (close_file_unwind, make_int (fd));
|
|
2833
|
|
2834 /* Supposedly happens on VMS. */
|
|
2835 if (st.st_size < 0)
|
|
2836 error ("File size is negative");
|
|
2837
|
|
2838 if (NILP (end))
|
|
2839 {
|
173
|
2840 if (!not_regular)
|
|
2841 {
|
|
2842 end = make_int (st.st_size);
|
|
2843 if (XINT (end) != st.st_size)
|
|
2844 error ("Maximum buffer size exceeded");
|
|
2845 }
|
211
|
2846
|
|
2847 #ifdef DOS_NT
|
|
2848 /* Permit old behaviour if desired. */
|
|
2849 if (NILP (Vinsert_file_contents_allow_replace) && !NILP (replace))
|
|
2850 {
|
|
2851 replace = Qnil;
|
|
2852 /* Surely this was never right! */
|
|
2853 /* XSETFASTINT (beg, 0);
|
|
2854 XSETFASTINT (end, st.st_size); */
|
|
2855 buffer_delete_range (buf, BUF_BEGV(buf), BUF_ZV(buf),
|
|
2856 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
2857 }
|
|
2858 #endif /* DOS_NT */
|
0
|
2859 }
|
|
2860
|
|
2861 /* If requested, replace the accessible part of the buffer
|
|
2862 with the file contents. Avoid replacing text at the
|
|
2863 beginning or end of the buffer that matches the file contents;
|
|
2864 that preserves markers pointing to the unchanged parts. */
|
259
|
2865 #if !defined (FILE_CODING)
|
0
|
2866 /* The replace-mode code currently only works when the assumption
|
211
|
2867 'one byte == one char' holds true. This fails Mule because
|
|
2868 files may contain multibyte characters. It holds under Windows NT
|
|
2869 provided we convert CRLF into LF. */
|
0
|
2870 # define FSFMACS_SPEEDY_INSERT
|
|
2871 #endif
|
|
2872 #ifndef FSFMACS_SPEEDY_INSERT
|
|
2873 if (!NILP (replace))
|
|
2874 {
|
|
2875 buffer_delete_range (buf, BUF_BEG (buf), BUF_Z (buf),
|
|
2876 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
2877 }
|
|
2878 #else /* FSFMACS_SPEEDY_INSERT */
|
|
2879 if (!NILP (replace))
|
|
2880 {
|
|
2881 char buffer[1 << 14];
|
|
2882 Bufpos same_at_start = BUF_BEGV (buf);
|
|
2883 Bufpos same_at_end = BUF_ZV (buf);
|
|
2884 int overlap;
|
211
|
2885 #ifdef DOS_NT
|
|
2886 /* Syncing with 19.34.6 note: same_at_start_in_file and
|
|
2887 same_at_end_in_file are not in XEmacs 20.4.
|
|
2888 First try to introduce them as-is and see what happens.
|
|
2889 Might be necessary to use constructs like
|
|
2890 st.st_size - (BUF_ZV (buf) - same_at_end)
|
|
2891 instead.
|
|
2892 --marcpa
|
|
2893 */
|
|
2894 /* Offset into the file where discrepancy begins. */
|
|
2895 int same_at_start_in_file = 0;
|
|
2896 /* Offset into the file where discrepancy ends. */
|
|
2897 int same_at_end_in_file = st.st_size;
|
|
2898 /* DOS_NT only: is there a `\r' character left in the buffer? */
|
|
2899 int cr_left_in_buffer = 0;
|
|
2900 /* DOS_NT only: was `\n' the first character in previous bufferful? */
|
|
2901 int last_was_lf = 0;
|
|
2902
|
|
2903 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
|
|
2904 /* Determine file type (text/binary) from its name.
|
|
2905 Note that the buffer_file_type changes here when the file
|
|
2906 being inserted is not of the same type as the original buffer. */
|
|
2907 current_buffer->buffer_file_type = call1 (Qfind_buffer_file_type, filename);
|
|
2908 if (NILP (current_buffer->buffer_file_type))
|
|
2909 crlf_conversion_required = 1;
|
|
2910 else if (current_buffer->buffer_file_type != Qt)
|
|
2911 /* Use heuristic to decide whether file is text or binary (based
|
|
2912 on the first bufferful) if buffer-file-type is not nil or t.
|
|
2913 If no decision is made (because no line endings were ever
|
|
2914 seen) then let buffer-file-type default to nil. */
|
|
2915 crlf_conversion_required = -1;
|
|
2916 #endif /* DOS_NT */
|
0
|
2917
|
|
2918 /* Count how many chars at the start of the file
|
|
2919 match the text at the beginning of the buffer. */
|
|
2920 while (1)
|
|
2921 {
|
|
2922 int nread;
|
|
2923 Bufpos bufpos;
|
211
|
2924 #ifdef DOS_NT
|
|
2925 if (cr_left_in_buffer)
|
|
2926 {
|
|
2927 nread = read_allowing_quit (fd, buffer + 1, sizeof(buffer) - 1);
|
|
2928 cr_left_in_buffer = 0;
|
|
2929 if (nread >= 0)
|
|
2930 nread++;
|
|
2931 }
|
|
2932 else
|
|
2933 #endif /* DOS_NT */
|
0
|
2934 nread = read_allowing_quit (fd, buffer, sizeof buffer);
|
|
2935 if (nread < 0)
|
|
2936 error ("IO error reading %s: %s",
|
16
|
2937 XSTRING_DATA (filename), strerror (errno));
|
0
|
2938 else if (nread == 0)
|
|
2939 break;
|
|
2940 bufpos = 0;
|
211
|
2941 #ifdef DOS_NT
|
|
2942 /* If requested, we do a simple check on the first bufferful
|
|
2943 to decide whether the file is binary or text. (If text, we
|
|
2944 count LF and CRLF occurences to determine whether the file
|
|
2945 was in Unix or DOS format.) */
|
|
2946 if (crlf_conversion_required < 0)
|
|
2947 {
|
|
2948 crlf_conversion_required = decide_buffer_type (buffer, nread);
|
|
2949 current_buffer->buffer_file_type =
|
|
2950 crlf_conversion_required ? Qnil : Qt;
|
|
2951 }
|
|
2952
|
|
2953 /* DOS_NT text files require that we ignore a `\r' before a `\n'. */
|
|
2954 if (crlf_conversion_required > 0)
|
|
2955 while (bufpos < nread && same_at_start < BUF_ZV (buf))
|
|
2956 {
|
|
2957 int filec = buffer[bufpos];
|
|
2958 int bufc = BUF_FETCH_CHAR (buf, same_at_start);
|
|
2959
|
|
2960 if (filec == '\n')
|
|
2961 lf_count++;
|
|
2962
|
|
2963 if (filec == bufc)
|
|
2964 same_at_start++, bufpos++, same_at_start_in_file++;
|
|
2965 else if (filec == '\r' && bufc == '\n')
|
|
2966 {
|
|
2967 /* If the `\r' is the last character in this buffer,
|
|
2968 it will be examined with the next bufferful. */
|
|
2969 if (bufpos == nread)
|
|
2970 {
|
|
2971 buffer[0] = filec;
|
|
2972 cr_left_in_buffer = 1;
|
|
2973 }
|
|
2974 else if (buffer[bufpos + 1] == bufc)
|
|
2975 {
|
|
2976 bufpos += 2;
|
|
2977 same_at_start_in_file += 2;
|
|
2978 same_at_start++;
|
|
2979 crlf_count++;
|
|
2980 lf_count++;
|
|
2981 }
|
|
2982 else
|
|
2983 break;
|
|
2984 }
|
|
2985 else
|
|
2986 break;
|
|
2987 }
|
|
2988 else
|
|
2989 #endif /* DOS_NT */
|
0
|
2990 while (bufpos < nread && same_at_start < BUF_ZV (buf)
|
|
2991 && BUF_FETCH_CHAR (buf, same_at_start) == buffer[bufpos])
|
211
|
2992 #ifdef DOS_NT
|
|
2993 same_at_start_in_file++,
|
|
2994 #endif
|
0
|
2995 same_at_start++, bufpos++;
|
|
2996 /* If we found a discrepancy, stop the scan.
|
211
|
2997 Otherwise loop around and scan the next bufferful. */
|
0
|
2998 if (bufpos != nread)
|
|
2999 break;
|
|
3000 }
|
|
3001 /* If the file matches the buffer completely,
|
|
3002 there's no need to replace anything. */
|
211
|
3003 #ifdef DOS_NT
|
|
3004 if (same_at_start_in_file == st.st_size)
|
|
3005 #else
|
0
|
3006 if (same_at_start - BUF_BEGV (buf) == st.st_size)
|
211
|
3007 #endif /* DOS_NT */
|
0
|
3008 {
|
|
3009 close (fd);
|
|
3010 unbind_to (speccount, Qnil);
|
|
3011 /* Truncate the buffer to the size of the file. */
|
|
3012 buffer_delete_range (buf, same_at_start, same_at_end,
|
|
3013 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3014 goto handled;
|
|
3015 }
|
|
3016 /* Count how many chars at the end of the file
|
|
3017 match the text at the end of the buffer. */
|
|
3018 while (1)
|
|
3019 {
|
|
3020 int total_read, nread;
|
|
3021 Bufpos bufpos, curpos, trial;
|
|
3022
|
|
3023 /* At what file position are we now scanning? */
|
211
|
3024 #ifdef DOS_NT
|
|
3025 curpos = same_at_end_in_file;
|
|
3026 #else
|
0
|
3027 curpos = st.st_size - (BUF_ZV (buf) - same_at_end);
|
211
|
3028 #endif /* DOS_NT */
|
0
|
3029 /* If the entire file matches the buffer tail, stop the scan. */
|
|
3030 if (curpos == 0)
|
|
3031 break;
|
|
3032 /* How much can we scan in the next step? */
|
|
3033 trial = min (curpos, sizeof buffer);
|
|
3034 if (lseek (fd, curpos - trial, 0) < 0)
|
227
|
3035 report_file_error ("Setting file position", list1 (filename));
|
0
|
3036
|
|
3037 total_read = 0;
|
|
3038 while (total_read < trial)
|
|
3039 {
|
|
3040 nread = read_allowing_quit (fd, buffer + total_read,
|
|
3041 trial - total_read);
|
|
3042 if (nread <= 0)
|
227
|
3043 report_file_error ("IO error reading file", list1 (filename));
|
0
|
3044 total_read += nread;
|
|
3045 }
|
211
|
3046 /* Scan this bufferful from the end, comparing with
|
0
|
3047 the Emacs buffer. */
|
|
3048 bufpos = total_read;
|
211
|
3049 #ifdef DOS_NT
|
|
3050 /* DOS_NT text files require that we ignore a `\r' before a `\n'. */
|
|
3051 if (crlf_conversion_required)
|
|
3052 #endif /* DOS_NT */
|
0
|
3053 /* Compare with same_at_start to avoid counting some buffer text
|
|
3054 as matching both at the file's beginning and at the end. */
|
211
|
3055 #if !defined(DOS_NT)
|
0
|
3056 while (bufpos > 0 && same_at_end > same_at_start
|
|
3057 && BUF_FETCH_CHAR (buf, same_at_end - 1) ==
|
|
3058 buffer[bufpos - 1])
|
|
3059 same_at_end--, bufpos--;
|
211
|
3060 #else /* DOS_NT */
|
|
3061 while (bufpos > 0 && same_at_end > same_at_start
|
|
3062 && same_at_end_in_file > same_at_start_in_file)
|
|
3063 {
|
|
3064 int filec = buffer[bufpos - 1];
|
|
3065 int bufc = BUF_FETCH_CHAR (buf, same_at_end - 1);
|
|
3066
|
|
3067 /* Account for `\n' in previous bufferful. */
|
|
3068 if (last_was_lf && filec == '\r')
|
|
3069 {
|
|
3070 same_at_end_in_file--, bufpos--;
|
|
3071 last_was_lf = 0;
|
|
3072 crlf_count++;
|
|
3073 }
|
|
3074 else if (filec == bufc)
|
|
3075 {
|
|
3076 last_was_lf = 0;
|
|
3077 same_at_end--, same_at_end_in_file--, bufpos--;
|
|
3078 if (bufc == '\n')
|
|
3079 {
|
|
3080 lf_count++;
|
|
3081 if (bufpos <= 0)
|
|
3082 last_was_lf = 1;
|
|
3083 else if (same_at_end_in_file <= same_at_start_in_file)
|
|
3084 break;
|
|
3085 else if (buffer[bufpos - 1] == '\r')
|
|
3086 same_at_end_in_file--, bufpos--, crlf_count++;
|
|
3087 }
|
|
3088 }
|
|
3089 else
|
|
3090 {
|
|
3091 last_was_lf = 0;
|
|
3092 break;
|
|
3093 }
|
|
3094 }
|
|
3095 else
|
|
3096 while (bufpos > 0 && same_at_end > same_at_start
|
|
3097 && same_at_end_in_file > same_at_start_in_file
|
|
3098 && BUF_FETCH_CHAR (buf, same_at_end - 1) ==
|
|
3099 buffer[bufpos - 1])
|
|
3100 same_at_end--, same_at_end_in_file--, bufpos--;
|
|
3101 #endif /* !defined(DOS_NT) */
|
0
|
3102 /* If we found a discrepancy, stop the scan.
|
211
|
3103 Otherwise loop around and scan the preceding bufferful. */
|
0
|
3104 if (bufpos != 0)
|
|
3105 break;
|
|
3106 /* If display current starts at beginning of line,
|
|
3107 keep it that way. */
|
|
3108 if (XBUFFER (XWINDOW (Fselected_window (Qnil))->buffer) == buf)
|
|
3109 XWINDOW (Fselected_window (Qnil))->start_at_line_beg =
|
|
3110 !NILP (Fbolp (make_buffer (buf)));
|
|
3111 }
|
|
3112
|
|
3113 /* Don't try to reuse the same piece of text twice. */
|
|
3114 overlap = same_at_start - BUF_BEGV (buf) -
|
|
3115 (same_at_end + st.st_size - BUF_ZV (buf));
|
|
3116 if (overlap > 0)
|
|
3117 same_at_end += overlap;
|
|
3118
|
|
3119 /* Arrange to read only the nonmatching middle part of the file. */
|
211
|
3120 #ifdef DOS_NT
|
|
3121 beg = make_int (same_at_start_in_file);
|
|
3122 end = make_int (same_at_end_in_file);
|
|
3123 #else
|
0
|
3124 beg = make_int (same_at_start - BUF_BEGV (buf));
|
|
3125 end = make_int (st.st_size - (BUF_ZV (buf) - same_at_end));
|
211
|
3126 #endif /* DOS_NT */
|
0
|
3127
|
|
3128 buffer_delete_range (buf, same_at_start, same_at_end,
|
|
3129 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3130 /* Insert from the file at the proper position. */
|
|
3131 BUF_SET_PT (buf, same_at_start);
|
|
3132 }
|
|
3133 #endif /* FSFMACS_SPEEDY_INSERT */
|
|
3134
|
173
|
3135 if (!not_regular)
|
|
3136 {
|
|
3137 total = XINT (end) - XINT (beg);
|
|
3138
|
|
3139 /* Make sure point-max won't overflow after this insertion. */
|
227
|
3140 if (total != XINT (make_int (total)))
|
173
|
3141 error ("Maximum buffer size exceeded");
|
|
3142 }
|
|
3143 else
|
|
3144 /* For a special file, all we can do is guess. */
|
|
3145 total = READ_BUF_SIZE;
|
0
|
3146
|
|
3147 if (XINT (beg) != 0
|
|
3148 #ifdef FSFMACS_SPEEDY_INSERT
|
|
3149 /* why was this here? asked jwz. The reason is that the replace-mode
|
|
3150 connivings above will normally put the file pointer other than
|
|
3151 where it should be. */
|
|
3152 || !NILP (replace)
|
|
3153 #endif /* !FSFMACS_SPEEDY_INSERT */
|
|
3154 )
|
|
3155 {
|
|
3156 if (lseek (fd, XINT (beg), 0) < 0)
|
227
|
3157 report_file_error ("Setting file position", list1 (filename));
|
0
|
3158 }
|
|
3159
|
|
3160 {
|
|
3161 Bufpos cur_point = BUF_PT (buf);
|
|
3162 struct gcpro ngcpro1;
|
|
3163 Lisp_Object stream = make_filedesc_input_stream (fd, 0, total,
|
|
3164 LSTR_ALLOW_QUIT);
|
|
3165
|
|
3166 NGCPRO1 (stream);
|
|
3167 Lstream_set_buffering (XLSTREAM (stream), LSTREAM_BLOCKN_BUFFERED, 65536);
|
259
|
3168 #ifdef FILE_CODING
|
70
|
3169 stream = make_decoding_input_stream
|
|
3170 (XLSTREAM (stream), Fget_coding_system (codesys));
|
|
3171 Lstream_set_character_mode (XLSTREAM (stream));
|
|
3172 Lstream_set_buffering (XLSTREAM (stream), LSTREAM_BLOCKN_BUFFERED, 65536);
|
183
|
3173 #endif /* MULE */
|
0
|
3174
|
|
3175 record_unwind_protect (close_stream_unwind, stream);
|
|
3176
|
16
|
3177 /* No need to limit the amount of stuff we attempt to read. (It would
|
|
3178 be incorrect, anyway, when Mule is enabled.) Instead, the limiting
|
|
3179 occurs inside of the filedesc stream. */
|
0
|
3180 while (1)
|
|
3181 {
|
|
3182 Bytecount this_len;
|
|
3183 Charcount cc_inserted;
|
|
3184
|
|
3185 QUIT;
|
|
3186 this_len = Lstream_read (XLSTREAM (stream), read_buf,
|
|
3187 sizeof (read_buf));
|
151
|
3188
|
0
|
3189 if (this_len <= 0)
|
|
3190 {
|
|
3191 if (this_len < 0)
|
|
3192 saverrno = errno;
|
|
3193 break;
|
|
3194 }
|
211
|
3195 #ifdef DOS_NT
|
|
3196 /* XEmacs (--marcpa) change: FSF does buffer_insert_raw_string_1() first
|
|
3197 then checks if conversion is needed, calling lisp
|
|
3198 (find-buffer-file-type) which can call a user-function that
|
|
3199 might look at the unconverted buffer to decide if
|
|
3200 conversion is needed.
|
|
3201 I removed the possibility for lisp functions called from
|
|
3202 find-buffer-file-type to look at the buffer's content, for
|
|
3203 simplicity reasons: it is easier to do the CRLF -> LF
|
|
3204 conversion on read_buf than on buffer contents because
|
|
3205 BUF_FETCH_CHAR does not return a pointer to an unsigned
|
|
3206 char memory location, and because we must cope with bytind
|
|
3207 VS bufpos in XEmacs, thus complicating crlf_to_lf().
|
|
3208 This decision (of doing Lstream_read(), crlf_to_lf() then
|
|
3209 buffer_insert_raw_string_1()) is debatable.
|
|
3210 --marcpa
|
|
3211 */
|
|
3212 /* Following FSF note no longer apply now. See comment above.
|
|
3213 --marcpa*/
|
|
3214 /* For compatability with earlier versions that did not support the
|
|
3215 REPLACE funtionality, we call find-buffer-file-type after inserting
|
|
3216 the contents to allow it to inspect the inserted data. (This was
|
|
3217 not intentional usage, but proved to be quite useful.) */
|
|
3218 if (NILP (replace))
|
|
3219 {
|
|
3220 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
|
|
3221 /* Determine file type (text/binary) from its name.
|
|
3222 Note that the buffer_file_type changes here when the file
|
|
3223 being inserted is not of the same type as the original buffer. */
|
|
3224 current_buffer->buffer_file_type = call1 (Qfind_buffer_file_type, filename);
|
|
3225 if (NILP (current_buffer->buffer_file_type))
|
|
3226 crlf_conversion_required = 1;
|
|
3227 else if (current_buffer->buffer_file_type != Qt)
|
|
3228 /* Use heuristic to decide whether file is text or binary (based
|
|
3229 on the first bufferful) if buffer-file-type is not nil or t.
|
|
3230 If no decision is made (because no line endings were ever
|
|
3231 seen) then let buffer-file-type default to nil. */
|
|
3232 crlf_conversion_required = -1;
|
|
3233 }
|
|
3234
|
|
3235 /* If requested, we check the inserted data to decide whether the file
|
|
3236 is binary or text. (If text, we count LF and CRLF occurences to
|
|
3237 determine whether the file was in Unix or DOS format.) */
|
|
3238 if (crlf_conversion_required < 0)
|
|
3239 {
|
|
3240 crlf_conversion_required =
|
|
3241 decide_buffer_type (read_buf, this_len);
|
|
3242 current_buffer->buffer_file_type =
|
|
3243 crlf_conversion_required ? Qnil : Qt;
|
|
3244 }
|
|
3245
|
|
3246 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
|
|
3247 /* Remove CRs from CR-LFs if the file is deemed to be a text file. */
|
|
3248 if (crlf_conversion_required)
|
|
3249 {
|
|
3250 int reduced_size
|
|
3251 = this_len - crlf_to_lf (this_len, read_buf,
|
|
3252 &lf_count);
|
|
3253 crlf_count += reduced_size;
|
|
3254 /* XEmacs (--marcpa) change: No need for this since we havent
|
|
3255 inserted in buffer yet. */
|
|
3256 #if 0
|
|
3257 ZV -= reduced_size;
|
|
3258 Z -= reduced_size;
|
|
3259 GPT -= reduced_size;
|
|
3260 GAP_SIZE += reduced_size;
|
|
3261 inserted -= reduced_size;
|
|
3262 #endif
|
|
3263 this_len -= reduced_size;
|
|
3264
|
|
3265 /* Change buffer_file_type back to binary if Unix eol format. */
|
|
3266 if (crlf_count == 0 && lf_count > 0)
|
|
3267 current_buffer->buffer_file_type = Qt;
|
|
3268 }
|
|
3269
|
|
3270 /* Make crlf_count and lf_count available for inspection. */
|
|
3271 Fset (intern ("buffer-file-lines"), make_int (lf_count));
|
|
3272 Fset (intern ("buffer-file-dos-lines"), make_int (crlf_count));
|
|
3273 #endif /* DOS_NT */
|
0
|
3274
|
|
3275 cc_inserted = buffer_insert_raw_string_1 (buf, cur_point, read_buf,
|
|
3276 this_len,
|
|
3277 !NILP (visit)
|
|
3278 ? INSDEL_NO_LOCKING : 0);
|
|
3279 inserted += cc_inserted;
|
|
3280 cur_point += cc_inserted;
|
|
3281 }
|
259
|
3282 #ifdef FILE_CODING
|
70
|
3283 if (!NILP (used_codesys))
|
|
3284 {
|
|
3285 Fset (used_codesys,
|
|
3286 XCODING_SYSTEM_NAME (decoding_stream_coding_system (XLSTREAM (stream))));
|
|
3287 }
|
183
|
3288 #endif /* MULE */
|
0
|
3289 NUNGCPRO;
|
|
3290 }
|
|
3291
|
|
3292 /* Close the file/stream */
|
|
3293 unbind_to (speccount, Qnil);
|
|
3294
|
|
3295 if (saverrno != 0)
|
|
3296 {
|
|
3297 error ("IO error reading %s: %s",
|
16
|
3298 XSTRING_DATA (filename), strerror (saverrno));
|
0
|
3299 }
|
|
3300
|
|
3301 notfound:
|
|
3302 handled:
|
|
3303
|
|
3304 end_multiple_change (buf, mc_count);
|
|
3305
|
|
3306 if (!NILP (visit))
|
|
3307 {
|
|
3308 if (!EQ (buf->undo_list, Qt))
|
|
3309 buf->undo_list = Qnil;
|
|
3310 #ifdef APOLLO
|
16
|
3311 stat ((char *) XSTRING_DATA (filename), &st);
|
0
|
3312 #endif
|
|
3313 if (NILP (handler))
|
|
3314 {
|
|
3315 buf->modtime = st.st_mtime;
|
|
3316 buf->filename = filename;
|
|
3317 /* XEmacs addition: */
|
|
3318 /* This function used to be in C, ostensibly so that
|
|
3319 it could be called here. But that's just silly.
|
|
3320 There's no reason C code can't call out to Lisp
|
|
3321 code, and it's a lot cleaner this way. */
|
|
3322 if (!NILP (Ffboundp (Qcompute_buffer_file_truename)))
|
|
3323 call1 (Qcompute_buffer_file_truename, make_buffer (buf));
|
|
3324 }
|
|
3325 BUF_SAVE_MODIFF (buf) = BUF_MODIFF (buf);
|
|
3326 buf->auto_save_modified = BUF_MODIFF (buf);
|
|
3327 buf->save_length = make_int (BUF_SIZE (buf));
|
|
3328 #ifdef CLASH_DETECTION
|
|
3329 if (NILP (handler))
|
|
3330 {
|
|
3331 if (!NILP (buf->file_truename))
|
|
3332 unlock_file (buf->file_truename);
|
|
3333 unlock_file (filename);
|
|
3334 }
|
|
3335 #endif /* CLASH_DETECTION */
|
|
3336 if (not_regular)
|
|
3337 RETURN_UNGCPRO (Fsignal (Qfile_error,
|
|
3338 list2 (build_string ("not a regular file"),
|
|
3339 filename)));
|
|
3340
|
|
3341 /* If visiting nonexistent file, return nil. */
|
|
3342 if (buf->modtime == -1)
|
|
3343 report_file_error ("Opening input file",
|
|
3344 list1 (filename));
|
|
3345 }
|
|
3346
|
|
3347 /* Decode file format */
|
|
3348 if (inserted > 0)
|
|
3349 {
|
151
|
3350 Lisp_Object insval = call3 (Qformat_decode,
|
0
|
3351 Qnil, make_int (inserted), visit);
|
|
3352 CHECK_INT (insval);
|
|
3353 inserted = XINT (insval);
|
|
3354 }
|
|
3355
|
|
3356 if (inserted > 0)
|
|
3357 {
|
227
|
3358 Lisp_Object p;
|
0
|
3359 struct gcpro ngcpro1;
|
|
3360
|
|
3361 NGCPRO1 (p);
|
227
|
3362 EXTERNAL_LIST_LOOP (p, Vafter_insert_file_functions)
|
0
|
3363 {
|
|
3364 Lisp_Object insval =
|
227
|
3365 call1 (XCAR (p), make_int (inserted));
|
0
|
3366 if (!NILP (insval))
|
|
3367 {
|
|
3368 CHECK_NATNUM (insval);
|
|
3369 inserted = XINT (insval);
|
|
3370 }
|
|
3371 QUIT;
|
|
3372 }
|
|
3373 NUNGCPRO;
|
|
3374 }
|
|
3375
|
|
3376 UNGCPRO;
|
|
3377
|
|
3378 if (!NILP (val))
|
|
3379 return (val);
|
|
3380 else
|
|
3381 return (list2 (filename, make_int (inserted)));
|
|
3382 }
|
|
3383
|
|
3384
|
|
3385 static int a_write (Lisp_Object outstream, Lisp_Object instream, int pos,
|
|
3386 Lisp_Object *annot);
|
|
3387 static Lisp_Object build_annotations (Lisp_Object start, Lisp_Object end);
|
|
3388
|
|
3389 /* If build_annotations switched buffers, switch back to BUF.
|
|
3390 Kill the temporary buffer that was selected in the meantime. */
|
|
3391
|
151
|
3392 static Lisp_Object
|
0
|
3393 build_annotations_unwind (Lisp_Object buf)
|
|
3394 {
|
|
3395 Lisp_Object tembuf;
|
|
3396
|
|
3397 if (XBUFFER (buf) == current_buffer)
|
|
3398 return Qnil;
|
|
3399 tembuf = Fcurrent_buffer ();
|
|
3400 Fset_buffer (buf);
|
|
3401 Fkill_buffer (tembuf);
|
|
3402 return Qnil;
|
|
3403 }
|
|
3404
|
70
|
3405 DEFUN ("write-region-internal", Fwrite_region_internal, 3, 7,
|
20
|
3406 "r\nFWrite region to file: ", /*
|
70
|
3407 Write current region into specified file; no coding-system frobbing.
|
|
3408 This function is identical to `write-region' except for the handling
|
|
3409 of the CODESYS argument under XEmacs/Mule. (When Mule support is not
|
|
3410 present, both functions are identical and ignore the CODESYS argument.)
|
|
3411 If support for Mule exists in this Emacs, the file is encoded according
|
|
3412 to the value of CODESYS. If this is nil, no code conversion occurs.
|
20
|
3413 */
|
70
|
3414 (start, end, filename, append, visit, lockname, codesys))
|
0
|
3415 {
|
116
|
3416 /* This function can call lisp */
|
0
|
3417 int desc;
|
|
3418 int failure;
|
|
3419 int save_errno = 0;
|
|
3420 struct stat st;
|
|
3421 Lisp_Object fn;
|
|
3422 int speccount = specpdl_depth ();
|
|
3423 int visiting_other = STRINGP (visit);
|
|
3424 int visiting = (EQ (visit, Qt) || visiting_other);
|
|
3425 int quietly = (!visiting && !NILP (visit));
|
|
3426 Lisp_Object visit_file = Qnil;
|
|
3427 Lisp_Object annotations = Qnil;
|
|
3428 struct buffer *given_buffer;
|
|
3429 Bufpos start1, end1;
|
|
3430
|
114
|
3431 /* #### dmoore - if Fexpand_file_name or handlers kill the buffer,
|
|
3432 we should signal an error rather than blissfully continuing
|
|
3433 along. ARGH, this function is going to lose lose lose. We need
|
|
3434 to protect the current_buffer from being destroyed, but the
|
|
3435 multiple return points make this a pain in the butt. */
|
|
3436
|
0
|
3437 #ifdef DOS_NT
|
|
3438 int buffer_file_type
|
|
3439 = NILP (current_buffer->buffer_file_type) ? O_TEXT : O_BINARY;
|
|
3440 #endif /* DOS_NT */
|
|
3441
|
259
|
3442 #ifdef FILE_CODING
|
70
|
3443 codesys = Fget_coding_system (codesys);
|
|
3444 #endif /* MULE */
|
|
3445
|
0
|
3446 if (current_buffer->base_buffer && ! NILP (visit))
|
|
3447 error ("Cannot do file visiting in an indirect buffer");
|
|
3448
|
|
3449 if (!NILP (start) && !STRINGP (start))
|
|
3450 get_buffer_range_char (current_buffer, start, end, &start1, &end1, 0);
|
|
3451
|
|
3452 {
|
|
3453 Lisp_Object handler;
|
|
3454 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
114
|
3455
|
0
|
3456 GCPRO5 (start, filename, visit, visit_file, lockname);
|
|
3457
|
|
3458 if (visiting_other)
|
|
3459 visit_file = Fexpand_file_name (visit, Qnil);
|
|
3460 else
|
|
3461 visit_file = filename;
|
|
3462 filename = Fexpand_file_name (filename, Qnil);
|
|
3463
|
|
3464 UNGCPRO;
|
|
3465
|
|
3466 if (NILP (lockname))
|
|
3467 lockname = visit_file;
|
|
3468
|
|
3469 /* If the file name has special constructs in it,
|
|
3470 call the corresponding file handler. */
|
|
3471 handler = Ffind_file_name_handler (filename, Qwrite_region);
|
|
3472 /* If FILENAME has no handler, see if VISIT has one. */
|
|
3473 if (NILP (handler) && STRINGP (visit))
|
|
3474 handler = Ffind_file_name_handler (visit, Qwrite_region);
|
|
3475
|
|
3476 if (!NILP (handler))
|
|
3477 {
|
151
|
3478 Lisp_Object val = call8 (handler, Qwrite_region, start, end,
|
86
|
3479 filename, append, visit, lockname, codesys);
|
0
|
3480 if (visiting)
|
|
3481 {
|
|
3482 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
|
|
3483 current_buffer->save_length =
|
|
3484 make_int (BUF_SIZE (current_buffer));
|
|
3485 current_buffer->filename = visit_file;
|
|
3486 MARK_MODELINE_CHANGED;
|
|
3487 }
|
|
3488 return val;
|
|
3489 }
|
|
3490 }
|
|
3491
|
|
3492 #ifdef CLASH_DETECTION
|
|
3493 if (!auto_saving)
|
|
3494 {
|
114
|
3495 Lisp_Object curbuf;
|
|
3496 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
3497
|
|
3498 XSETBUFFER (curbuf, current_buffer);
|
|
3499 GCPRO5 (start, filename, visit_file, lockname, curbuf);
|
0
|
3500 lock_file (lockname);
|
|
3501 UNGCPRO;
|
|
3502 }
|
|
3503 #endif /* CLASH_DETECTION */
|
|
3504
|
|
3505 /* Special kludge to simplify auto-saving. */
|
|
3506 if (NILP (start))
|
|
3507 {
|
|
3508 start1 = BUF_BEG (current_buffer);
|
|
3509 end1 = BUF_Z (current_buffer);
|
|
3510 }
|
|
3511
|
|
3512 record_unwind_protect (build_annotations_unwind, Fcurrent_buffer ());
|
|
3513
|
|
3514 given_buffer = current_buffer;
|
|
3515 annotations = build_annotations (start, end);
|
|
3516 if (current_buffer != given_buffer)
|
|
3517 {
|
|
3518 start1 = BUF_BEGV (current_buffer);
|
|
3519 end1 = BUF_ZV (current_buffer);
|
|
3520 }
|
|
3521
|
|
3522 fn = filename;
|
|
3523 desc = -1;
|
|
3524 if (!NILP (append))
|
|
3525 #ifdef DOS_NT
|
16
|
3526 desc = open ((char *) XSTRING_DATA (fn),
|
211
|
3527 (O_WRONLY | buffer_file_type), 0);
|
0
|
3528 #else /* not DOS_NT */
|
251
|
3529 desc = open ((char *) XSTRING_DATA (fn), O_WRONLY | OPEN_BINARY, 0);
|
0
|
3530 #endif /* not DOS_NT */
|
|
3531
|
|
3532 if (desc < 0)
|
|
3533 {
|
|
3534 #ifdef DOS_NT
|
151
|
3535 desc = open ((char *) XSTRING_DATA (fn),
|
211
|
3536 (O_WRONLY | O_TRUNC | O_CREAT | buffer_file_type),
|
0
|
3537 (S_IREAD | S_IWRITE));
|
|
3538 #else /* not DOS_NT */
|
251
|
3539 desc = open ((char *) XSTRING_DATA (fn),
|
|
3540 (O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY),
|
|
3541 ((auto_saving) ? auto_save_mode_bits : CREAT_MODE));
|
0
|
3542 #endif /* DOS_NT */
|
|
3543 }
|
|
3544
|
|
3545 if (desc < 0)
|
|
3546 {
|
|
3547 #ifdef CLASH_DETECTION
|
|
3548 save_errno = errno;
|
|
3549 if (!auto_saving) unlock_file (lockname);
|
|
3550 errno = save_errno;
|
|
3551 #endif /* CLASH_DETECTION */
|
227
|
3552 report_file_error ("Opening output file", list1 (filename));
|
0
|
3553 }
|
|
3554
|
|
3555 {
|
|
3556 Lisp_Object desc_locative = Fcons (make_int (desc), Qnil);
|
|
3557 Lisp_Object instream = Qnil, outstream = Qnil;
|
|
3558 struct gcpro gcpro1, gcpro2;
|
|
3559 /* need to gcpro; QUIT could happen out of call to write() */
|
|
3560 GCPRO2 (instream, outstream);
|
|
3561
|
|
3562 record_unwind_protect (close_file_unwind, desc_locative);
|
|
3563
|
|
3564 if (!NILP (append))
|
|
3565 {
|
|
3566 if (lseek (desc, 0, 2) < 0)
|
|
3567 {
|
|
3568 #ifdef CLASH_DETECTION
|
|
3569 if (!auto_saving) unlock_file (lockname);
|
|
3570 #endif /* CLASH_DETECTION */
|
|
3571 report_file_error ("Lseek error",
|
|
3572 list1 (filename));
|
|
3573 }
|
|
3574 }
|
|
3575
|
|
3576 failure = 0;
|
|
3577
|
|
3578 /* Note: I tried increasing the buffering size, along with
|
|
3579 various other tricks, but nothing seemed to make much of
|
|
3580 a difference in the time it took to save a large file.
|
|
3581 (Actually that's not true. With a local disk, changing
|
|
3582 the buffer size doesn't seem to make much difference.
|
|
3583 With an NFS-mounted disk, it could make a lot of difference
|
|
3584 because you're affecting the number of network requests
|
|
3585 that need to be made, and there could be a large latency
|
|
3586 for each request. So I've increased the buffer size
|
|
3587 to 64K.) */
|
|
3588 outstream = make_filedesc_output_stream (desc, 0, -1, 0);
|
|
3589 Lstream_set_buffering (XLSTREAM (outstream),
|
|
3590 LSTREAM_BLOCKN_BUFFERED, 65536);
|
259
|
3591 #ifdef FILE_CODING
|
70
|
3592 outstream =
|
185
|
3593 make_encoding_output_stream (XLSTREAM (outstream), codesys);
|
70
|
3594 Lstream_set_buffering (XLSTREAM (outstream),
|
|
3595 LSTREAM_BLOCKN_BUFFERED, 65536);
|
183
|
3596 #endif /* MULE */
|
0
|
3597 if (STRINGP (start))
|
|
3598 {
|
|
3599 instream = make_lisp_string_input_stream (start, 0, -1);
|
|
3600 start1 = 0;
|
|
3601 }
|
|
3602 else
|
|
3603 instream = make_lisp_buffer_input_stream (current_buffer, start1, end1,
|
|
3604 LSTR_SELECTIVE |
|
|
3605 LSTR_IGNORE_ACCESSIBLE);
|
|
3606 failure = (0 > (a_write (outstream, instream, start1,
|
|
3607 &annotations)));
|
|
3608 save_errno = errno;
|
|
3609 /* Note that this doesn't close the desc since we created the
|
|
3610 stream without the LSTR_CLOSING flag, but it does
|
|
3611 flush out any buffered data. */
|
|
3612 if (Lstream_close (XLSTREAM (outstream)) < 0)
|
|
3613 {
|
|
3614 failure = 1;
|
|
3615 save_errno = errno;
|
|
3616 }
|
|
3617 Lstream_close (XLSTREAM (instream));
|
|
3618 UNGCPRO;
|
|
3619
|
|
3620 #ifdef HAVE_FSYNC
|
|
3621 /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
|
|
3622 Disk full in NFS may be reported here. */
|
|
3623 /* mib says that closing the file will try to write as fast as NFS can do
|
|
3624 it, and that means the fsync here is not crucial for autosave files. */
|
|
3625 if (!auto_saving && fsync (desc) < 0
|
|
3626 /* If fsync fails with EINTR, don't treat that as serious. */
|
|
3627 && errno != EINTR)
|
|
3628 {
|
|
3629 failure = 1;
|
|
3630 save_errno = errno;
|
|
3631 }
|
183
|
3632 #endif /* HAVE_FSYNC */
|
0
|
3633
|
151
|
3634 /* Spurious "file has changed on disk" warnings have been
|
0
|
3635 observed on Suns as well.
|
|
3636 It seems that `close' can change the modtime, under nfs.
|
|
3637
|
|
3638 (This has supposedly been fixed in Sunos 4,
|
|
3639 but who knows about all the other machines with NFS?) */
|
|
3640 /* On VMS and APOLLO, must do the stat after the close
|
|
3641 since closing changes the modtime. */
|
|
3642 #if 0 /* !defined (VMS) && !defined (APOLLO) */
|
|
3643 fstat (desc, &st);
|
|
3644 #endif
|
|
3645
|
|
3646 /* NFS can report a write failure now. */
|
|
3647 if (close (desc) < 0)
|
|
3648 {
|
|
3649 failure = 1;
|
|
3650 save_errno = errno;
|
|
3651 }
|
|
3652
|
|
3653 /* Discard the close unwind-protect. Execute the one for
|
|
3654 build_annotations (switches back to the original current buffer
|
|
3655 as necessary). */
|
|
3656 XCAR (desc_locative) = Qnil;
|
|
3657 unbind_to (speccount, Qnil);
|
|
3658 }
|
151
|
3659
|
0
|
3660
|
|
3661 #if 1 /* defined (VMS) || defined (APOLLO) */
|
16
|
3662 stat ((char *) XSTRING_DATA (fn), &st);
|
0
|
3663 #endif
|
|
3664
|
|
3665 #ifdef CLASH_DETECTION
|
|
3666 if (!auto_saving)
|
|
3667 unlock_file (lockname);
|
|
3668 #endif /* CLASH_DETECTION */
|
|
3669
|
|
3670 /* Do this before reporting IO error
|
|
3671 to avoid a "file has changed on disk" warning on
|
|
3672 next attempt to save. */
|
|
3673 if (visiting)
|
|
3674 current_buffer->modtime = st.st_mtime;
|
|
3675
|
|
3676 if (failure)
|
151
|
3677 error ("IO error writing %s: %s",
|
|
3678 XSTRING_DATA (fn),
|
0
|
3679 strerror (save_errno));
|
|
3680
|
|
3681 if (visiting)
|
|
3682 {
|
|
3683 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
|
|
3684 current_buffer->save_length = make_int (BUF_SIZE (current_buffer));
|
|
3685 current_buffer->filename = visit_file;
|
|
3686 MARK_MODELINE_CHANGED;
|
|
3687 }
|
|
3688 else if (quietly)
|
|
3689 {
|
|
3690 return Qnil;
|
|
3691 }
|
|
3692
|
|
3693 if (!auto_saving)
|
|
3694 {
|
|
3695 if (visiting_other)
|
16
|
3696 message ("Wrote %s", XSTRING_DATA (visit_file));
|
0
|
3697 else
|
|
3698 {
|
|
3699 struct gcpro gcpro1;
|
|
3700 Lisp_Object fsp;
|
|
3701 GCPRO1 (fn);
|
|
3702
|
|
3703 fsp = Ffile_symlink_p (fn);
|
|
3704 if (NILP (fsp))
|
16
|
3705 message ("Wrote %s", XSTRING_DATA (fn));
|
0
|
3706 else
|
151
|
3707 message ("Wrote %s (symlink to %s)",
|
16
|
3708 XSTRING_DATA (fn), XSTRING_DATA (fsp));
|
0
|
3709 UNGCPRO;
|
|
3710 }
|
|
3711 }
|
|
3712 return Qnil;
|
|
3713 }
|
|
3714
|
|
3715 /* #### This is such a load of shit!!!! There is no way we should define
|
|
3716 something so stupid as a subr, just sort the fucking list more
|
|
3717 intelligently. */
|
20
|
3718 DEFUN ("car-less-than-car", Fcar_less_than_car, 2, 2, 0, /*
|
0
|
3719 Return t if (car A) is numerically less than (car B).
|
20
|
3720 */
|
|
3721 (a, b))
|
0
|
3722 {
|
|
3723 return Flss (Fcar (a), Fcar (b));
|
|
3724 }
|
|
3725
|
|
3726 /* Heh heh heh, let's define this too, just to aggravate the person who
|
|
3727 wrote the above comment. */
|
20
|
3728 DEFUN ("cdr-less-than-cdr", Fcdr_less_than_cdr, 2, 2, 0, /*
|
0
|
3729 Return t if (cdr A) is numerically less than (cdr B).
|
20
|
3730 */
|
|
3731 (a, b))
|
0
|
3732 {
|
|
3733 return Flss (Fcdr (a), Fcdr (b));
|
|
3734 }
|
|
3735
|
|
3736 /* Build the complete list of annotations appropriate for writing out
|
|
3737 the text between START and END, by calling all the functions in
|
|
3738 write-region-annotate-functions and merging the lists they return.
|
|
3739 If one of these functions switches to a different buffer, we assume
|
|
3740 that buffer contains altered text. Therefore, the caller must
|
|
3741 make sure to restore the current buffer in all cases,
|
|
3742 as save-excursion would do. */
|
|
3743
|
|
3744 static Lisp_Object
|
|
3745 build_annotations (Lisp_Object start, Lisp_Object end)
|
|
3746 {
|
|
3747 /* This function can GC */
|
|
3748 Lisp_Object annotations;
|
|
3749 Lisp_Object p, res;
|
|
3750 struct gcpro gcpro1, gcpro2;
|
215
|
3751 Lisp_Object original_buffer;
|
|
3752
|
|
3753 XSETBUFFER (original_buffer, current_buffer);
|
0
|
3754
|
|
3755 annotations = Qnil;
|
|
3756 p = Vwrite_region_annotate_functions;
|
|
3757 GCPRO2 (annotations, p);
|
|
3758 while (!NILP (p))
|
|
3759 {
|
|
3760 struct buffer *given_buffer = current_buffer;
|
|
3761 Vwrite_region_annotations_so_far = annotations;
|
|
3762 res = call2 (Fcar (p), start, end);
|
|
3763 /* If the function makes a different buffer current,
|
|
3764 assume that means this buffer contains altered text to be output.
|
|
3765 Reset START and END from the buffer bounds
|
|
3766 and discard all previous annotations because they should have
|
|
3767 been dealt with by this function. */
|
|
3768 if (current_buffer != given_buffer)
|
|
3769 {
|
|
3770 start = make_int (BUF_BEGV (current_buffer));
|
|
3771 end = make_int (BUF_ZV (current_buffer));
|
|
3772 annotations = Qnil;
|
|
3773 }
|
183
|
3774 Flength (res); /* Check basic validity of return value */
|
0
|
3775 annotations = merge (annotations, res, Qcar_less_than_car);
|
|
3776 p = Fcdr (p);
|
|
3777 }
|
|
3778
|
|
3779 /* Now do the same for annotation functions implied by the file-format */
|
|
3780 if (auto_saving && (!EQ (Vauto_save_file_format, Qt)))
|
|
3781 p = Vauto_save_file_format;
|
|
3782 else
|
|
3783 p = current_buffer->file_format;
|
|
3784 while (!NILP (p))
|
|
3785 {
|
|
3786 struct buffer *given_buffer = current_buffer;
|
|
3787 Vwrite_region_annotations_so_far = annotations;
|
215
|
3788 res = call4 (Qformat_annotate_function, Fcar (p), start, end,
|
|
3789 original_buffer);
|
0
|
3790 if (current_buffer != given_buffer)
|
|
3791 {
|
|
3792 start = make_int (BUF_BEGV (current_buffer));
|
|
3793 end = make_int (BUF_ZV (current_buffer));
|
|
3794 annotations = Qnil;
|
|
3795 }
|
183
|
3796 Flength (res);
|
0
|
3797 annotations = merge (annotations, res, Qcar_less_than_car);
|
|
3798 p = Fcdr (p);
|
|
3799 }
|
|
3800 UNGCPRO;
|
|
3801 return annotations;
|
|
3802 }
|
|
3803
|
|
3804 /* Write to stream OUTSTREAM the characters from INSTREAM (it is read until
|
|
3805 EOF is encountered), assuming they start at position POS in the buffer
|
|
3806 of string that STREAM refers to. Intersperse with them the annotations
|
|
3807 from *ANNOT that fall into the range of positions we are reading from,
|
|
3808 each at its appropriate position.
|
|
3809
|
|
3810 Modify *ANNOT by discarding elements as we output them.
|
|
3811 The return value is negative in case of system call failure. */
|
|
3812
|
|
3813 /* 4K should probably be fine. We just need to reduce the number of
|
|
3814 function calls to reasonable level. The Lstream stuff itself will
|
|
3815 batch to 64K to reduce the number of system calls. */
|
|
3816
|
|
3817 #define A_WRITE_BATCH_SIZE 4096
|
|
3818
|
|
3819 static int
|
|
3820 a_write (Lisp_Object outstream, Lisp_Object instream, int pos,
|
|
3821 Lisp_Object *annot)
|
|
3822 {
|
|
3823 Lisp_Object tem;
|
|
3824 int nextpos;
|
|
3825 unsigned char largebuf[A_WRITE_BATCH_SIZE];
|
|
3826 Lstream *instr = XLSTREAM (instream);
|
|
3827 Lstream *outstr = XLSTREAM (outstream);
|
|
3828
|
|
3829 while (NILP (*annot) || CONSP (*annot))
|
|
3830 {
|
|
3831 tem = Fcar_safe (Fcar (*annot));
|
|
3832 if (INTP (tem))
|
|
3833 nextpos = XINT (tem);
|
|
3834 else
|
|
3835 nextpos = INT_MAX;
|
70
|
3836 #ifdef MULE
|
|
3837 /* If there are annotations left and we have Mule, then we
|
|
3838 have to do the I/O one emchar at a time so we can
|
|
3839 determine when to insert the annotation. */
|
|
3840 if (!NILP (*annot))
|
|
3841 {
|
|
3842 Emchar ch;
|
|
3843 while (pos != nextpos && (ch = Lstream_get_emchar (instr)) != EOF)
|
|
3844 {
|
|
3845 if (Lstream_put_emchar (outstr, ch) < 0)
|
|
3846 return -1;
|
|
3847 pos++;
|
|
3848 }
|
|
3849 }
|
|
3850 else
|
183
|
3851 #endif /* MULE */
|
0
|
3852 {
|
|
3853 while (pos != nextpos)
|
|
3854 {
|
|
3855 /* Otherwise there is no point to that. Just go in batches. */
|
|
3856 int chunk = min (nextpos - pos, A_WRITE_BATCH_SIZE);
|
151
|
3857
|
0
|
3858 chunk = Lstream_read (instr, largebuf, chunk);
|
|
3859 if (chunk < 0)
|
|
3860 return -1;
|
|
3861 if (chunk == 0) /* EOF */
|
|
3862 break;
|
|
3863 if (Lstream_write (outstr, largebuf, chunk) < chunk)
|
|
3864 return -1;
|
|
3865 pos += chunk;
|
|
3866 }
|
|
3867 }
|
|
3868 if (pos == nextpos)
|
|
3869 {
|
|
3870 tem = Fcdr (Fcar (*annot));
|
|
3871 if (STRINGP (tem))
|
|
3872 {
|
16
|
3873 if (Lstream_write (outstr, XSTRING_DATA (tem),
|
|
3874 XSTRING_LENGTH (tem)) < 0)
|
0
|
3875 return -1;
|
|
3876 }
|
|
3877 *annot = Fcdr (*annot);
|
|
3878 }
|
|
3879 else
|
|
3880 return 0;
|
|
3881 }
|
|
3882 return -1;
|
|
3883 }
|
|
3884
|
|
3885
|
|
3886
|
|
3887 #if 0
|
|
3888 #include <des_crypt.h>
|
|
3889
|
|
3890 #define CRYPT_BLOCK_SIZE 8 /* bytes */
|
|
3891 #define CRYPT_KEY_SIZE 8 /* bytes */
|
|
3892
|
20
|
3893 DEFUN ("encrypt-string", Fencrypt_string, 2, 2, 0, /*
|
0
|
3894 Encrypt STRING using KEY.
|
20
|
3895 */
|
|
3896 (string, key))
|
0
|
3897 {
|
|
3898 char *encrypted_string, *raw_key;
|
|
3899 int rounded_size, extra, key_size;
|
|
3900
|
|
3901 /* !!#### May produce bogus data under Mule. */
|
|
3902 CHECK_STRING (string);
|
|
3903 CHECK_STRING (key);
|
|
3904
|
16
|
3905 extra = XSTRING_LENGTH (string) % CRYPT_BLOCK_SIZE;
|
|
3906 rounded_size = XSTRING_LENGTH (string) + extra;
|
0
|
3907 encrypted_string = alloca (rounded_size + 1);
|
16
|
3908 memcpy (encrypted_string, XSTRING_DATA (string), XSTRING_LENGTH (string));
|
0
|
3909 memset (encrypted_string + rounded_size - extra, 0, extra + 1);
|
|
3910
|
183
|
3911 key_size = min (CRYPT_KEY_SIZE, XSTRING_LENGTH (key))
|
0
|
3912
|
|
3913 raw_key = alloca (CRYPT_KEY_SIZE + 1);
|
16
|
3914 memcpy (raw_key, XSTRING_DATA (key), key_size);
|
0
|
3915 memset (raw_key + key_size, 0, (CRYPT_KEY_SIZE + 1) - key_size);
|
|
3916
|
183
|
3917 ecb_crypt (raw_key, encrypted_string, rounded_size,
|
|
3918 DES_ENCRYPT | DES_SW);
|
0
|
3919 return make_string (encrypted_string, rounded_size);
|
|
3920 }
|
|
3921
|
20
|
3922 DEFUN ("decrypt-string", Fdecrypt_string, 2, 2, 0, /*
|
0
|
3923 Decrypt STRING using KEY.
|
20
|
3924 */
|
|
3925 (string, key))
|
0
|
3926 {
|
|
3927 char *decrypted_string, *raw_key;
|
|
3928 int string_size, key_size;
|
|
3929
|
|
3930 CHECK_STRING (string);
|
|
3931 CHECK_STRING (key);
|
|
3932
|
16
|
3933 string_size = XSTRING_LENGTH (string) + 1;
|
0
|
3934 decrypted_string = alloca (string_size);
|
16
|
3935 memcpy (decrypted_string, XSTRING_DATA (string), string_size);
|
0
|
3936 decrypted_string[string_size - 1] = '\0';
|
|
3937
|
183
|
3938 key_size = min (CRYPT_KEY_SIZE, XSTRING_LENGTH (key))
|
0
|
3939
|
|
3940 raw_key = alloca (CRYPT_KEY_SIZE + 1);
|
16
|
3941 memcpy (raw_key, XSTRING_DATA (key), key_size);
|
0
|
3942 memset (raw_key + key_size, 0, (CRYPT_KEY_SIZE + 1) - key_size);
|
|
3943
|
|
3944
|
183
|
3945 ecb_crypt (raw_key, decrypted_string, string_size, D | DES_SW);
|
0
|
3946 return make_string (decrypted_string, string_size - 1);
|
|
3947 }
|
183
|
3948 #endif /* 0 */
|
0
|
3949
|
|
3950
|
20
|
3951 DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime, 1, 1, 0, /*
|
0
|
3952 Return t if last mod time of BUF's visited file matches what BUF records.
|
|
3953 This means that the file has not been changed since it was visited or saved.
|
20
|
3954 */
|
|
3955 (buf))
|
0
|
3956 {
|
116
|
3957 /* This function can call lisp */
|
0
|
3958 struct buffer *b;
|
|
3959 struct stat st;
|
|
3960 Lisp_Object handler;
|
|
3961
|
|
3962 CHECK_BUFFER (buf);
|
|
3963 b = XBUFFER (buf);
|
|
3964
|
|
3965 if (!STRINGP (b->filename)) return Qt;
|
|
3966 if (b->modtime == 0) return Qt;
|
|
3967
|
|
3968 /* If the file name has special constructs in it,
|
|
3969 call the corresponding file handler. */
|
|
3970 handler = Ffind_file_name_handler (b->filename,
|
|
3971 Qverify_visited_file_modtime);
|
|
3972 if (!NILP (handler))
|
|
3973 return call2 (handler, Qverify_visited_file_modtime, buf);
|
|
3974
|
16
|
3975 if (stat ((char *) XSTRING_DATA (b->filename), &st) < 0)
|
0
|
3976 {
|
|
3977 /* If the file doesn't exist now and didn't exist before,
|
|
3978 we say that it isn't modified, provided the error is a tame one. */
|
|
3979 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
|
|
3980 st.st_mtime = -1;
|
|
3981 else
|
|
3982 st.st_mtime = 0;
|
|
3983 }
|
|
3984 if (st.st_mtime == b->modtime
|
|
3985 /* If both are positive, accept them if they are off by one second. */
|
|
3986 || (st.st_mtime > 0 && b->modtime > 0
|
|
3987 && (st.st_mtime == b->modtime + 1
|
|
3988 || st.st_mtime == b->modtime - 1)))
|
|
3989 return Qt;
|
|
3990 return Qnil;
|
|
3991 }
|
|
3992
|
20
|
3993 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, 0, 0, 0, /*
|
0
|
3994 Clear out records of last mod time of visited file.
|
|
3995 Next attempt to save will certainly not complain of a discrepancy.
|
20
|
3996 */
|
|
3997 ())
|
0
|
3998 {
|
|
3999 current_buffer->modtime = 0;
|
|
4000 return Qnil;
|
|
4001 }
|
|
4002
|
20
|
4003 DEFUN ("visited-file-modtime", Fvisited_file_modtime, 0, 0, 0, /*
|
0
|
4004 Return the current buffer's recorded visited file modification time.
|
|
4005 The value is a list of the form (HIGH . LOW), like the time values
|
|
4006 that `file-attributes' returns.
|
20
|
4007 */
|
|
4008 ())
|
0
|
4009 {
|
|
4010 return time_to_lisp ((time_t) current_buffer->modtime);
|
|
4011 }
|
|
4012
|
20
|
4013 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, 0, 1, 0, /*
|
0
|
4014 Update buffer's recorded modification time from the visited file's time.
|
|
4015 Useful if the buffer was not read from the file normally
|
|
4016 or if the file itself has been changed for some known benign reason.
|
|
4017 An argument specifies the modification time value to use
|
|
4018 (instead of that of the visited file), in the form of a list
|
|
4019 (HIGH . LOW) or (HIGH LOW).
|
20
|
4020 */
|
|
4021 (time_list))
|
0
|
4022 {
|
116
|
4023 /* This function can call lisp */
|
0
|
4024 if (!NILP (time_list))
|
|
4025 {
|
|
4026 time_t the_time;
|
|
4027 lisp_to_time (time_list, &the_time);
|
|
4028 current_buffer->modtime = (int) the_time;
|
|
4029 }
|
|
4030 else
|
|
4031 {
|
|
4032 Lisp_Object filename;
|
|
4033 struct stat st;
|
|
4034 Lisp_Object handler;
|
114
|
4035 struct gcpro gcpro1, gcpro2, gcpro3;
|
151
|
4036
|
114
|
4037 GCPRO3 (filename, time_list, current_buffer->filename);
|
|
4038 filename = Fexpand_file_name (current_buffer->filename, Qnil);
|
0
|
4039
|
|
4040 /* If the file name has special constructs in it,
|
|
4041 call the corresponding file handler. */
|
|
4042 handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime);
|
|
4043 UNGCPRO;
|
|
4044 if (!NILP (handler))
|
|
4045 /* The handler can find the file name the same way we did. */
|
|
4046 return call2 (handler, Qset_visited_file_modtime, Qnil);
|
16
|
4047 else if (stat ((char *) XSTRING_DATA (filename), &st) >= 0)
|
0
|
4048 current_buffer->modtime = st.st_mtime;
|
|
4049 }
|
|
4050
|
|
4051 return Qnil;
|
|
4052 }
|
|
4053
|
|
4054 static Lisp_Object
|
|
4055 auto_save_error (Lisp_Object condition_object, Lisp_Object ignored)
|
|
4056 {
|
116
|
4057 /* This function can call lisp */
|
0
|
4058 if (gc_in_progress)
|
|
4059 return Qnil;
|
110
|
4060 /* Don't try printing an error message after everything is gone! */
|
|
4061 if (preparing_for_armageddon)
|
|
4062 return Qnil;
|
0
|
4063 clear_echo_area (selected_frame (), Qauto_saving, 1);
|
|
4064 Fding (Qt, Qauto_save_error, Qnil);
|
82
|
4065 message ("Auto-saving...error for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4066 Fsleep_for (make_int (1));
|
82
|
4067 message ("Auto-saving...error!for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4068 Fsleep_for (make_int (1));
|
82
|
4069 message ("Auto-saving...error for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4070 Fsleep_for (make_int (1));
|
|
4071 return Qnil;
|
|
4072 }
|
|
4073
|
|
4074 static Lisp_Object
|
|
4075 auto_save_1 (Lisp_Object ignored)
|
|
4076 {
|
116
|
4077 /* This function can call lisp */
|
|
4078 /* #### I think caller is protecting current_buffer? */
|
0
|
4079 struct stat st;
|
|
4080 Lisp_Object fn = current_buffer->filename;
|
|
4081 Lisp_Object a = current_buffer->auto_save_file_name;
|
|
4082
|
|
4083 if (!STRINGP (a))
|
|
4084 return (Qnil);
|
|
4085
|
|
4086 /* Get visited file's mode to become the auto save file's mode. */
|
|
4087 if (STRINGP (fn) &&
|
16
|
4088 stat ((char *) XSTRING_DATA (fn), &st) >= 0)
|
0
|
4089 /* But make sure we can overwrite it later! */
|
|
4090 auto_save_mode_bits = st.st_mode | 0600;
|
|
4091 else
|
|
4092 /* default mode for auto-save files of buffers with no file is
|
|
4093 readable by owner only. This may annoy some small number of
|
|
4094 people, but the alternative removes all privacy from email. */
|
|
4095 auto_save_mode_bits = 0600;
|
|
4096
|
|
4097 return
|
70
|
4098 /* !!#### need to deal with this 'escape-quoted everywhere */
|
|
4099 Fwrite_region_internal (Qnil, Qnil, a, Qnil, Qlambda, Qnil,
|
|
4100 #ifdef MULE
|
|
4101 Qescape_quoted
|
|
4102 #else
|
|
4103 Qnil
|
|
4104 #endif
|
|
4105 );
|
44
|
4106 }
|
|
4107
|
116
|
4108 static Lisp_Object
|
|
4109 auto_save_expand_name_error (Lisp_Object condition_object, Lisp_Object ignored)
|
|
4110 {
|
|
4111 /* #### this function should spew an error message about not being
|
|
4112 able to open the .saves file. */
|
|
4113 return Qnil;
|
|
4114 }
|
|
4115
|
|
4116 static Lisp_Object
|
|
4117 auto_save_expand_name (Lisp_Object name)
|
|
4118 {
|
|
4119 struct gcpro gcpro1;
|
|
4120
|
|
4121 /* note that caller did NOT gc protect name, so we do it. */
|
|
4122 /* #### dmoore - this might not be neccessary, if condition_case_1
|
|
4123 protects it. but I don't think it does. */
|
|
4124 GCPRO1 (name);
|
|
4125 RETURN_UNGCPRO (Fexpand_file_name (name, Qnil));
|
|
4126 }
|
|
4127
|
0
|
4128
|
|
4129 static Lisp_Object
|
|
4130 do_auto_save_unwind (Lisp_Object fd)
|
|
4131 {
|
|
4132 close (XINT (fd));
|
|
4133 return (fd);
|
|
4134 }
|
|
4135
|
|
4136 static Lisp_Object
|
|
4137 do_auto_save_unwind_2 (Lisp_Object old_auto_saving)
|
|
4138 {
|
|
4139 auto_saving = XINT (old_auto_saving);
|
|
4140 return Qnil;
|
|
4141 }
|
|
4142
|
|
4143 /* Fdo_auto_save() checks whether a GC is in progress when it is called,
|
|
4144 and if so, tries to avoid touching lisp objects.
|
|
4145
|
|
4146 The only time that Fdo_auto_save() is called while GC is in progress
|
|
4147 is if we're going down, as a result of an abort() or a kill signal.
|
|
4148 It's fairly important that we generate autosave files in that case!
|
|
4149 */
|
|
4150
|
20
|
4151 DEFUN ("do-auto-save", Fdo_auto_save, 0, 2, "", /*
|
0
|
4152 Auto-save all buffers that need it.
|
|
4153 This is all buffers that have auto-saving enabled
|
|
4154 and are changed since last auto-saved.
|
|
4155 Auto-saving writes the buffer into a file
|
|
4156 so that your editing is not lost if the system crashes.
|
|
4157 This file is not the file you visited; that changes only when you save.
|
|
4158 Normally we run the normal hook `auto-save-hook' before saving.
|
|
4159
|
|
4160 Non-nil first argument means do not print any message if successful.
|
|
4161 Non-nil second argument means save only current buffer.
|
20
|
4162 */
|
|
4163 (no_message, current_only))
|
0
|
4164 {
|
116
|
4165 /* This function can call lisp */
|
110
|
4166 struct buffer *b;
|
0
|
4167 Lisp_Object tail, buf;
|
|
4168 int auto_saved = 0;
|
|
4169 int do_handled_files;
|
|
4170 Lisp_Object oquit = Qnil;
|
|
4171 Lisp_Object listfile = Qnil;
|
110
|
4172 Lisp_Object old;
|
0
|
4173 int listdesc = -1;
|
|
4174 int speccount = specpdl_depth ();
|
110
|
4175 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
4176
|
|
4177 XSETBUFFER (old, current_buffer);
|
|
4178 GCPRO3 (oquit, listfile, old);
|
0
|
4179 check_quit (); /* make Vquit_flag accurate */
|
|
4180 /* Ordinarily don't quit within this function,
|
|
4181 but don't make it impossible to quit (in case we get hung in I/O). */
|
|
4182 oquit = Vquit_flag;
|
|
4183 Vquit_flag = Qnil;
|
|
4184
|
|
4185 /* No further GCPRO needed, because (when it matters) all Lisp_Object
|
|
4186 variables point to non-strings reached from Vbuffer_alist. */
|
|
4187
|
|
4188 if (minibuf_level != 0 || preparing_for_armageddon)
|
|
4189 no_message = Qt;
|
|
4190
|
|
4191 run_hook (Qauto_save_hook);
|
|
4192
|
|
4193 if (GC_STRINGP (Vauto_save_list_file_name))
|
116
|
4194 listfile = condition_case_1 (Qt,
|
|
4195 auto_save_expand_name,
|
|
4196 Vauto_save_list_file_name,
|
|
4197 auto_save_expand_name_error, Qnil);
|
0
|
4198
|
|
4199 /* Make sure auto_saving is reset. */
|
|
4200 record_unwind_protect (do_auto_save_unwind_2, make_int (auto_saving));
|
|
4201
|
|
4202 auto_saving = 1;
|
|
4203
|
|
4204 /* First, save all files which don't have handlers. If Emacs is
|
|
4205 crashing, the handlers may tweak what is causing Emacs to crash
|
|
4206 in the first place, and it would be a shame if Emacs failed to
|
|
4207 autosave perfectly ordinary files because it couldn't handle some
|
|
4208 ange-ftp'd file. */
|
|
4209 for (do_handled_files = 0; do_handled_files < 2; do_handled_files++)
|
|
4210 {
|
|
4211 for (tail = Vbuffer_alist;
|
|
4212 GC_CONSP (tail);
|
|
4213 tail = XCDR (tail))
|
|
4214 {
|
|
4215 buf = XCDR (XCAR (tail));
|
|
4216 b = XBUFFER (buf);
|
|
4217
|
|
4218 if (!GC_NILP (current_only)
|
|
4219 && b != current_buffer)
|
|
4220 continue;
|
151
|
4221
|
0
|
4222 /* Don't auto-save indirect buffers.
|
|
4223 The base buffer takes care of it. */
|
|
4224 if (b->base_buffer)
|
|
4225 continue;
|
|
4226
|
|
4227 /* Check for auto save enabled
|
|
4228 and file changed since last auto save
|
|
4229 and file changed since last real save. */
|
|
4230 if (GC_STRINGP (b->auto_save_file_name)
|
|
4231 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
|
|
4232 && b->auto_save_modified < BUF_MODIFF (b)
|
|
4233 /* -1 means we've turned off autosaving for a while--see below. */
|
|
4234 && XINT (b->save_length) >= 0
|
|
4235 && (do_handled_files
|
|
4236 || NILP (Ffind_file_name_handler (b->auto_save_file_name,
|
|
4237 Qwrite_region))))
|
|
4238 {
|
|
4239 EMACS_TIME before_time, after_time;
|
|
4240
|
|
4241 EMACS_GET_TIME (before_time);
|
|
4242 /* If we had a failure, don't try again for 20 minutes. */
|
|
4243 if (!preparing_for_armageddon
|
|
4244 && b->auto_save_failure_time >= 0
|
|
4245 && (EMACS_SECS (before_time) - b->auto_save_failure_time <
|
|
4246 1200))
|
|
4247 continue;
|
|
4248
|
|
4249 if (!preparing_for_armageddon &&
|
|
4250 (XINT (b->save_length) * 10
|
|
4251 > (BUF_Z (b) - BUF_BEG (b)) * 13)
|
|
4252 /* A short file is likely to change a large fraction;
|
|
4253 spare the user annoying messages. */
|
|
4254 && XINT (b->save_length) > 5000
|
|
4255 /* These messages are frequent and annoying for `*mail*'. */
|
|
4256 && !EQ (b->filename, Qnil)
|
|
4257 && NILP (no_message)
|
|
4258 && disable_auto_save_when_buffer_shrinks)
|
|
4259 {
|
|
4260 /* It has shrunk too much; turn off auto-saving here.
|
|
4261 Unless we're about to crash, in which case auto-save it
|
|
4262 anyway.
|
|
4263 */
|
|
4264 message
|
|
4265 ("Buffer %s has shrunk a lot; auto save turned off there",
|
16
|
4266 XSTRING_DATA (b->name));
|
0
|
4267 /* Turn off auto-saving until there's a real save,
|
|
4268 and prevent any more warnings. */
|
|
4269 b->save_length = make_int (-1);
|
|
4270 if (!gc_in_progress)
|
|
4271 Fsleep_for (make_int (1));
|
|
4272 continue;
|
|
4273 }
|
|
4274 set_buffer_internal (b);
|
|
4275 if (!auto_saved && GC_NILP (no_message))
|
|
4276 {
|
|
4277 static CONST unsigned char *msg
|
|
4278 = (CONST unsigned char *) "Auto-saving...";
|
|
4279 echo_area_message (selected_frame (), msg, Qnil,
|
|
4280 0, strlen ((CONST char *) msg),
|
|
4281 Qauto_saving);
|
|
4282 }
|
|
4283
|
|
4284 /* Open the auto-save list file, if necessary.
|
|
4285 We only do this now so that the file only exists
|
|
4286 if we actually auto-saved any files. */
|
|
4287 if (!auto_saved && GC_STRINGP (listfile) && listdesc < 0)
|
|
4288 {
|
|
4289 #ifdef DOS_NT
|
70
|
4290 listdesc = open ((char *) XSTRING_DATA (listfile),
|
167
|
4291 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
|
0
|
4292 S_IREAD | S_IWRITE);
|
|
4293 #else /* not DOS_NT */
|
251
|
4294 listdesc = open ((char *) XSTRING_DATA (listfile),
|
|
4295 O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
|
|
4296 CREAT_MODE);
|
0
|
4297 #endif /* not DOS_NT */
|
|
4298
|
|
4299 /* Arrange to close that file whether or not we get
|
|
4300 an error. */
|
|
4301 if (listdesc >= 0)
|
|
4302 record_unwind_protect (do_auto_save_unwind,
|
|
4303 make_int (listdesc));
|
|
4304 }
|
|
4305
|
|
4306 /* Record all the buffers that we are auto-saving in
|
|
4307 the special file that lists them. For each of
|
|
4308 these buffers, record visited name (if any) and
|
|
4309 auto save name. */
|
|
4310 if (listdesc >= 0)
|
|
4311 {
|
259
|
4312 CONST Extbyte *auto_save_file_name_ext;
|
0
|
4313 Extcount auto_save_file_name_ext_len;
|
151
|
4314
|
0
|
4315 GET_STRING_FILENAME_DATA_ALLOCA
|
|
4316 (b->auto_save_file_name,
|
|
4317 auto_save_file_name_ext,
|
|
4318 auto_save_file_name_ext_len);
|
|
4319 if (!NILP (b->filename))
|
|
4320 {
|
259
|
4321 CONST Extbyte *filename_ext;
|
0
|
4322 Extcount filename_ext_len;
|
151
|
4323
|
0
|
4324 GET_STRING_FILENAME_DATA_ALLOCA (b->filename,
|
|
4325 filename_ext,
|
|
4326 filename_ext_len);
|
|
4327 write (listdesc, filename_ext, filename_ext_len);
|
|
4328 }
|
|
4329 write (listdesc, "\n", 1);
|
|
4330 write (listdesc, auto_save_file_name_ext,
|
|
4331 auto_save_file_name_ext_len);
|
|
4332 write (listdesc, "\n", 1);
|
|
4333 }
|
|
4334
|
110
|
4335 /* dmoore - In a bad scenario we've set b=XBUFFER(buf)
|
|
4336 based on values in Vbuffer_alist. auto_save_1 may
|
|
4337 cause lisp handlers to run. Those handlers may kill
|
|
4338 the buffer and then GC. Since the buffer is killed,
|
|
4339 it's no longer in Vbuffer_alist so it might get reaped
|
|
4340 by the GC. We also need to protect tail. */
|
|
4341 /* #### There is probably a lot of other code which has
|
|
4342 pointers into buffers which may get blown away by
|
|
4343 handlers. */
|
|
4344 {
|
169
|
4345 struct gcpro ngcpro1, ngcpro2;
|
|
4346 NGCPRO2 (buf, tail);
|
110
|
4347 condition_case_1 (Qt,
|
|
4348 auto_save_1, Qnil,
|
|
4349 auto_save_error, Qnil);
|
169
|
4350 NUNGCPRO;
|
110
|
4351 }
|
|
4352 /* Handler killed our saved current-buffer! Pick any. */
|
|
4353 if (!BUFFER_LIVE_P (XBUFFER (old)))
|
|
4354 XSETBUFFER (old, current_buffer);
|
|
4355
|
|
4356 set_buffer_internal (XBUFFER (old));
|
0
|
4357 auto_saved++;
|
151
|
4358
|
110
|
4359 /* Handler killed their own buffer! */
|
|
4360 if (!BUFFER_LIVE_P(b))
|
|
4361 continue;
|
|
4362
|
0
|
4363 b->auto_save_modified = BUF_MODIFF (b);
|
|
4364 b->save_length = make_int (BUF_SIZE (b));
|
|
4365 EMACS_GET_TIME (after_time);
|
|
4366 /* If auto-save took more than 60 seconds,
|
|
4367 assume it was an NFS failure that got a timeout. */
|
|
4368 if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60)
|
|
4369 b->auto_save_failure_time = EMACS_SECS (after_time);
|
|
4370 }
|
|
4371 }
|
|
4372 }
|
|
4373
|
|
4374 /* Prevent another auto save till enough input events come in. */
|
|
4375 if (auto_saved)
|
|
4376 record_auto_save ();
|
|
4377
|
|
4378 /* If we didn't save anything into the listfile, remove the old
|
|
4379 one because nothing needed to be auto-saved. Do this afterwards
|
|
4380 rather than before in case we get a crash attempting to autosave
|
|
4381 (in that case we'd still want the old one around). */
|
|
4382 if (listdesc < 0 && !auto_saved && GC_STRINGP (listfile))
|
16
|
4383 unlink ((char *) XSTRING_DATA (listfile));
|
0
|
4384
|
|
4385 /* Show "...done" only if the echo area would otherwise be empty. */
|
|
4386 if (auto_saved && NILP (no_message)
|
|
4387 && NILP (clear_echo_area (selected_frame (), Qauto_saving, 0)))
|
|
4388 {
|
|
4389 static CONST unsigned char *msg
|
|
4390 = (CONST unsigned char *)"Auto-saving...done";
|
|
4391 echo_area_message (selected_frame (), msg, Qnil, 0,
|
|
4392 strlen ((CONST char *) msg), Qauto_saving);
|
|
4393 }
|
|
4394
|
|
4395 Vquit_flag = oquit;
|
|
4396
|
|
4397 RETURN_UNGCPRO (unbind_to (speccount, Qnil));
|
|
4398 }
|
|
4399
|
20
|
4400 DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved, 0, 0, 0, /*
|
0
|
4401 Mark current buffer as auto-saved with its current text.
|
|
4402 No auto-save file will be written until the buffer changes again.
|
20
|
4403 */
|
|
4404 ())
|
0
|
4405 {
|
|
4406 current_buffer->auto_save_modified = BUF_MODIFF (current_buffer);
|
|
4407 current_buffer->save_length = make_int (BUF_SIZE (current_buffer));
|
|
4408 current_buffer->auto_save_failure_time = -1;
|
|
4409 return Qnil;
|
|
4410 }
|
|
4411
|
20
|
4412 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, 0, 0, 0, /*
|
0
|
4413 Clear any record of a recent auto-save failure in the current buffer.
|
20
|
4414 */
|
|
4415 ())
|
0
|
4416 {
|
|
4417 current_buffer->auto_save_failure_time = -1;
|
|
4418 return Qnil;
|
|
4419 }
|
|
4420
|
20
|
4421 DEFUN ("recent-auto-save-p", Frecent_auto_save_p, 0, 0, 0, /*
|
0
|
4422 Return t if buffer has been auto-saved since last read in or saved.
|
20
|
4423 */
|
|
4424 ())
|
0
|
4425 {
|
|
4426 return (BUF_SAVE_MODIFF (current_buffer) <
|
|
4427 current_buffer->auto_save_modified) ? Qt : Qnil;
|
|
4428 }
|
|
4429
|
|
4430
|
|
4431 /************************************************************************/
|
|
4432 /* initialization */
|
|
4433 /************************************************************************/
|
|
4434
|
|
4435 void
|
|
4436 syms_of_fileio (void)
|
|
4437 {
|
|
4438 defsymbol (&Qexpand_file_name, "expand-file-name");
|
|
4439 defsymbol (&Qfile_truename, "file-truename");
|
|
4440 defsymbol (&Qsubstitute_in_file_name, "substitute-in-file-name");
|
|
4441 defsymbol (&Qdirectory_file_name, "directory-file-name");
|
|
4442 defsymbol (&Qfile_name_directory, "file-name-directory");
|
|
4443 defsymbol (&Qfile_name_nondirectory, "file-name-nondirectory");
|
|
4444 defsymbol (&Qunhandled_file_name_directory, "unhandled-file-name-directory");
|
|
4445 defsymbol (&Qfile_name_as_directory, "file-name-as-directory");
|
|
4446 defsymbol (&Qcopy_file, "copy-file");
|
|
4447 defsymbol (&Qmake_directory_internal, "make-directory-internal");
|
|
4448 defsymbol (&Qdelete_directory, "delete-directory");
|
|
4449 defsymbol (&Qdelete_file, "delete-file");
|
|
4450 defsymbol (&Qrename_file, "rename-file");
|
|
4451 defsymbol (&Qadd_name_to_file, "add-name-to-file");
|
|
4452 defsymbol (&Qmake_symbolic_link, "make-symbolic-link");
|
|
4453 defsymbol (&Qfile_exists_p, "file-exists-p");
|
|
4454 defsymbol (&Qfile_executable_p, "file-executable-p");
|
|
4455 defsymbol (&Qfile_readable_p, "file-readable-p");
|
|
4456 defsymbol (&Qfile_symlink_p, "file-symlink-p");
|
|
4457 defsymbol (&Qfile_writable_p, "file-writable-p");
|
|
4458 defsymbol (&Qfile_directory_p, "file-directory-p");
|
|
4459 defsymbol (&Qfile_regular_p, "file-regular-p");
|
|
4460 defsymbol (&Qfile_accessible_directory_p, "file-accessible-directory-p");
|
|
4461 defsymbol (&Qfile_modes, "file-modes");
|
|
4462 defsymbol (&Qset_file_modes, "set-file-modes");
|
|
4463 defsymbol (&Qfile_newer_than_file_p, "file-newer-than-file-p");
|
|
4464 defsymbol (&Qinsert_file_contents, "insert-file-contents");
|
|
4465 defsymbol (&Qwrite_region, "write-region");
|
|
4466 defsymbol (&Qverify_visited_file_modtime, "verify-visited-file-modtime");
|
|
4467 defsymbol (&Qset_visited_file_modtime, "set-visited-file-modtime");
|
|
4468 #ifdef DOS_NT
|
|
4469 defsymbol (&Qfind_buffer_file_type, "find-buffer-file-type");
|
|
4470 #endif /* DOS_NT */
|
|
4471 defsymbol (&Qcar_less_than_car, "car-less-than-car"); /* Vomitous! */
|
|
4472
|
|
4473 defsymbol (&Qfile_name_handler_alist, "file-name-handler-alist");
|
|
4474 defsymbol (&Qauto_save_hook, "auto-save-hook");
|
|
4475 defsymbol (&Qauto_save_error, "auto-save-error");
|
|
4476 defsymbol (&Qauto_saving, "auto-saving");
|
|
4477
|
|
4478 defsymbol (&Qformat_decode, "format-decode");
|
|
4479 defsymbol (&Qformat_annotate_function, "format-annotate-function");
|
|
4480
|
|
4481 defsymbol (&Qcompute_buffer_file_truename, "compute-buffer-file-truename");
|
|
4482 deferror (&Qfile_error, "file-error", "File error", Qio_error);
|
|
4483 deferror (&Qfile_already_exists, "file-already-exists",
|
|
4484 "File already exists", Qfile_error);
|
|
4485
|
20
|
4486 DEFSUBR (Ffind_file_name_handler);
|
|
4487
|
|
4488 DEFSUBR (Ffile_name_directory);
|
|
4489 DEFSUBR (Ffile_name_nondirectory);
|
|
4490 DEFSUBR (Funhandled_file_name_directory);
|
|
4491 DEFSUBR (Ffile_name_as_directory);
|
|
4492 DEFSUBR (Fdirectory_file_name);
|
|
4493 DEFSUBR (Fmake_temp_name);
|
|
4494 DEFSUBR (Fexpand_file_name);
|
|
4495 DEFSUBR (Ffile_truename);
|
|
4496 DEFSUBR (Fsubstitute_in_file_name);
|
|
4497 DEFSUBR (Fcopy_file);
|
|
4498 DEFSUBR (Fmake_directory_internal);
|
|
4499 DEFSUBR (Fdelete_directory);
|
|
4500 DEFSUBR (Fdelete_file);
|
|
4501 DEFSUBR (Frename_file);
|
|
4502 DEFSUBR (Fadd_name_to_file);
|
0
|
4503 #ifdef S_IFLNK
|
20
|
4504 DEFSUBR (Fmake_symbolic_link);
|
0
|
4505 #endif /* S_IFLNK */
|
|
4506 #ifdef HPUX_NET
|
20
|
4507 DEFSUBR (Fsysnetunam);
|
0
|
4508 #endif /* HPUX_NET */
|
20
|
4509 DEFSUBR (Ffile_name_absolute_p);
|
|
4510 DEFSUBR (Ffile_exists_p);
|
|
4511 DEFSUBR (Ffile_executable_p);
|
|
4512 DEFSUBR (Ffile_readable_p);
|
|
4513 DEFSUBR (Ffile_writable_p);
|
|
4514 DEFSUBR (Ffile_symlink_p);
|
|
4515 DEFSUBR (Ffile_directory_p);
|
|
4516 DEFSUBR (Ffile_accessible_directory_p);
|
|
4517 DEFSUBR (Ffile_regular_p);
|
|
4518 DEFSUBR (Ffile_modes);
|
|
4519 DEFSUBR (Fset_file_modes);
|
|
4520 DEFSUBR (Fset_default_file_modes);
|
|
4521 DEFSUBR (Fdefault_file_modes);
|
|
4522 DEFSUBR (Funix_sync);
|
|
4523 DEFSUBR (Ffile_newer_than_file_p);
|
|
4524 DEFSUBR (Finsert_file_contents_internal);
|
|
4525 DEFSUBR (Fwrite_region_internal);
|
|
4526 DEFSUBR (Fcar_less_than_car); /* Vomitous! */
|
|
4527 DEFSUBR (Fcdr_less_than_cdr); /* Yeah oh yeah bucko .... */
|
0
|
4528 #if 0
|
20
|
4529 DEFSUBR (Fencrypt_string);
|
|
4530 DEFSUBR (Fdecrypt_string);
|
0
|
4531 #endif
|
20
|
4532 DEFSUBR (Fverify_visited_file_modtime);
|
|
4533 DEFSUBR (Fclear_visited_file_modtime);
|
|
4534 DEFSUBR (Fvisited_file_modtime);
|
|
4535 DEFSUBR (Fset_visited_file_modtime);
|
|
4536
|
|
4537 DEFSUBR (Fdo_auto_save);
|
|
4538 DEFSUBR (Fset_buffer_auto_saved);
|
|
4539 DEFSUBR (Fclear_buffer_auto_save_failure);
|
|
4540 DEFSUBR (Frecent_auto_save_p);
|
0
|
4541 }
|
|
4542
|
|
4543 void
|
|
4544 vars_of_fileio (void)
|
|
4545 {
|
|
4546 DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format /*
|
|
4547 *Format in which to write auto-save files.
|
|
4548 Should be a list of symbols naming formats that are defined in `format-alist'.
|
|
4549 If it is t, which is the default, auto-save files are written in the
|
|
4550 same format as a regular save would use.
|
|
4551 */ );
|
|
4552 Vauto_save_file_format = Qt;
|
|
4553
|
|
4554 DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist /*
|
|
4555 *Alist of elements (REGEXP . HANDLER) for file names handled specially.
|
|
4556 If a file name matches REGEXP, then all I/O on that file is done by calling
|
|
4557 HANDLER.
|
|
4558
|
|
4559 The first argument given to HANDLER is the name of the I/O primitive
|
|
4560 to be handled; the remaining arguments are the arguments that were
|
|
4561 passed to that primitive. For example, if you do
|
|
4562 (file-exists-p FILENAME)
|
|
4563 and FILENAME is handled by HANDLER, then HANDLER is called like this:
|
|
4564 (funcall HANDLER 'file-exists-p FILENAME)
|
|
4565 The function `find-file-name-handler' checks this list for a handler
|
|
4566 for its argument.
|
|
4567 */ );
|
|
4568 Vfile_name_handler_alist = Qnil;
|
|
4569
|
|
4570 DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions /*
|
|
4571 A list of functions to be called at the end of `insert-file-contents'.
|
|
4572 Each is passed one argument, the number of bytes inserted. It should return
|
|
4573 the new byte count, and leave point the same. If `insert-file-contents' is
|
|
4574 intercepted by a handler from `file-name-handler-alist', that handler is
|
|
4575 responsible for calling the after-insert-file-functions if appropriate.
|
|
4576 */ );
|
|
4577 Vafter_insert_file_functions = Qnil;
|
|
4578
|
|
4579 DEFVAR_LISP ("write-region-annotate-functions",
|
|
4580 &Vwrite_region_annotate_functions /*
|
|
4581 A list of functions to be called at the start of `write-region'.
|
183
|
4582 Each is passed two arguments, START and END, as for `write-region'.
|
0
|
4583 It should return a list of pairs (POSITION . STRING) of strings to be
|
|
4584 effectively inserted at the specified positions of the file being written
|
|
4585 \(1 means to insert before the first byte written). The POSITIONs must be
|
|
4586 sorted into increasing order. If there are several functions in the list,
|
|
4587 the several lists are merged destructively.
|
|
4588 */ );
|
|
4589 Vwrite_region_annotate_functions = Qnil;
|
|
4590
|
|
4591 DEFVAR_LISP ("write-region-annotations-so-far",
|
|
4592 &Vwrite_region_annotations_so_far /*
|
|
4593 When an annotation function is called, this holds the previous annotations.
|
|
4594 These are the annotations made by other annotation functions
|
|
4595 that were already called. See also `write-region-annotate-functions'.
|
|
4596 */ );
|
|
4597 Vwrite_region_annotations_so_far = Qnil;
|
|
4598
|
|
4599 DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers /*
|
|
4600 A list of file name handlers that temporarily should not be used.
|
|
4601 This applies only to the operation `inhibit-file-name-operation'.
|
|
4602 */ );
|
|
4603 Vinhibit_file_name_handlers = Qnil;
|
|
4604
|
|
4605 DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation /*
|
|
4606 The operation for which `inhibit-file-name-handlers' is applicable.
|
|
4607 */ );
|
|
4608 Vinhibit_file_name_operation = Qnil;
|
|
4609
|
|
4610 DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name /*
|
|
4611 File name in which we write a list of all auto save file names.
|
|
4612 */ );
|
|
4613 Vauto_save_list_file_name = Qnil;
|
|
4614
|
|
4615 DEFVAR_BOOL ("disable-auto-save-when-buffer-shrinks",
|
|
4616 &disable_auto_save_when_buffer_shrinks /*
|
|
4617 If non-nil, auto-saving is disabled when a buffer shrinks too much.
|
|
4618 This is to prevent you from losing your edits if you accidentally
|
|
4619 delete a large chunk of the buffer and don't notice it until too late.
|
|
4620 Saving the buffer normally turns auto-save back on.
|
|
4621 */ );
|
|
4622 disable_auto_save_when_buffer_shrinks = 1;
|
235
|
4623
|
211
|
4624 DEFVAR_LISP ("directory-sep-char", &Vdirectory_sep_char /*
|
|
4625 *Directory separator character for built-in functions that return file names.
|
|
4626 The value should be either ?/ or ?\\ (any other value is treated as ?\\).
|
|
4627 This variable affects the built-in functions only on Windows,
|
|
4628 on other platforms, it is initialized so that Lisp code can find out
|
|
4629 what the normal separator is.
|
|
4630 */ );
|
235
|
4631 Vdirectory_sep_char = make_char('/');
|
|
4632
|
|
4633 #ifdef DOS_NT
|
211
|
4634 DEFVAR_LISP ("insert-file-contents-allow-replace", &Vinsert_file_contents_allow_replace /*
|
|
4635 *Allow REPLACE option of insert-file-contents to preserve markers.
|
|
4636 If non-nil, the REPLACE option works as described, preserving markers.
|
|
4637 If nil, the REPLACE option is implemented by deleting the visible region
|
|
4638 then inserting the file contents as if REPLACE was nil.
|
|
4639
|
|
4640 This option is only meaningful on Windows.
|
|
4641 */ );
|
|
4642 Vinsert_file_contents_allow_replace = Qt;
|
|
4643 #endif
|
0
|
4644 }
|