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