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