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