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