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))
|
225
|
773 && IS_DIRECTORY_SEP (XSTRING_BYTE (default_, 2))))
|
211
|
774 #ifdef WINDOWSNT
|
225
|
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)))
|
211
|
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))
|
225
|
784 || IS_DEVICE_SEP (XSTRING_BYTE (default_, 1)))
|
211
|
785 #endif /* not DOS_NT */
|
225
|
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
|
225
|
1119 (/ /server/share for UNC, nothing otherwise). */
|
211
|
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
|
225
|
1729 ifd = interruptible_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 {
|
225
|
2312 /* This function can GC */
|
|
2313 Lisp_Object abspath = Qnil;
|
0
|
2314 Lisp_Object handler;
|
|
2315 int desc;
|
|
2316 struct gcpro gcpro1;
|
225
|
2317 GCPRO1 (abspath);
|
151
|
2318
|
0
|
2319 CHECK_STRING (filename);
|
|
2320 abspath = Fexpand_file_name (filename, Qnil);
|
|
2321
|
|
2322 /* If the file name has special constructs in it,
|
|
2323 call the corresponding file handler. */
|
|
2324 handler = Ffind_file_name_handler (abspath, Qfile_readable_p);
|
|
2325 if (!NILP (handler))
|
225
|
2326 RETURN_UNGCPRO (call2 (handler, Qfile_readable_p, abspath));
|
0
|
2327
|
211
|
2328 #ifdef DOS_NT
|
|
2329 /* Under MS-DOS and Windows, open does not work for directories. */
|
|
2330 if (access (XSTRING_DATA (abspath), 0) == 0)
|
|
2331 return Qt;
|
|
2332 return Qnil;
|
|
2333 #else /* not DOS_NT */
|
225
|
2334 desc = interruptible_open ((char *) XSTRING_DATA (abspath), O_RDONLY, 0);
|
|
2335 UNGCPRO;
|
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
|
225
|
2843 if (
|
0
|
2844 #ifndef APOLLO
|
225
|
2845 (stat ((char *) XSTRING_DATA (filename), &st) < 0)
|
0
|
2846 #else /* APOLLO */
|
225
|
2847 /* Don't even bother with interruptible_open. APOLLO sucks. */
|
|
2848 ((fd = open ((char *) XSTRING_DATA (filename), O_RDONLY, 0)) < 0
|
|
2849 || fstat (fd, &st) < 0)
|
0
|
2850 #endif /* APOLLO */
|
225
|
2851 )
|
0
|
2852 {
|
|
2853 if (fd >= 0) close (fd);
|
|
2854 badopen:
|
|
2855 if (NILP (visit))
|
|
2856 report_file_error ("Opening input file",
|
|
2857 Fcons (filename, Qnil));
|
|
2858 st.st_mtime = -1;
|
|
2859 goto notfound;
|
|
2860 }
|
|
2861
|
|
2862 #ifdef S_IFREG
|
209
|
2863 /* Signal an error if we are accessing a non-regular file, with
|
|
2864 REPLACE, BEG or END being non-nil. */
|
0
|
2865 if (!S_ISREG (st.st_mode))
|
|
2866 {
|
173
|
2867 not_regular = 1;
|
|
2868
|
|
2869 if (!NILP (visit))
|
|
2870 goto notfound;
|
|
2871
|
|
2872 if (!NILP (replace) || !NILP (beg) || !NILP (end))
|
0
|
2873 {
|
|
2874 end_multiple_change (buf, mc_count);
|
|
2875
|
|
2876 return Fsignal (Qfile_error,
|
|
2877 list2 (build_translated_string("not a regular file"),
|
|
2878 filename));
|
|
2879 }
|
|
2880 }
|
183
|
2881 #endif /* S_IFREG */
|
0
|
2882
|
173
|
2883 if (!NILP (beg))
|
|
2884 CHECK_INT (beg);
|
|
2885 else
|
|
2886 beg = Qzero;
|
|
2887
|
|
2888 if (!NILP (end))
|
|
2889 CHECK_INT (end);
|
|
2890
|
0
|
2891 if (fd < 0)
|
225
|
2892 {
|
|
2893 if ((fd = interruptible_open ((char *) XSTRING_DATA (filename),
|
|
2894 O_RDONLY, 0)) < 0)
|
|
2895 goto badopen;
|
|
2896 }
|
0
|
2897
|
|
2898 /* Replacement should preserve point as it preserves markers. */
|
|
2899 if (!NILP (replace))
|
|
2900 record_unwind_protect (restore_point_unwind, Fpoint_marker (Qnil, Qnil));
|
|
2901
|
|
2902 record_unwind_protect (close_file_unwind, make_int (fd));
|
|
2903
|
|
2904 /* Supposedly happens on VMS. */
|
|
2905 if (st.st_size < 0)
|
|
2906 error ("File size is negative");
|
|
2907
|
|
2908 if (NILP (end))
|
|
2909 {
|
173
|
2910 if (!not_regular)
|
|
2911 {
|
|
2912 end = make_int (st.st_size);
|
|
2913 if (XINT (end) != st.st_size)
|
|
2914 error ("Maximum buffer size exceeded");
|
|
2915 }
|
211
|
2916
|
|
2917 #ifdef DOS_NT
|
|
2918 /* Permit old behaviour if desired. */
|
|
2919 if (NILP (Vinsert_file_contents_allow_replace) && !NILP (replace))
|
|
2920 {
|
|
2921 replace = Qnil;
|
|
2922 /* Surely this was never right! */
|
|
2923 /* XSETFASTINT (beg, 0);
|
|
2924 XSETFASTINT (end, st.st_size); */
|
|
2925 buffer_delete_range (buf, BUF_BEGV(buf), BUF_ZV(buf),
|
|
2926 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
2927 }
|
|
2928 #endif /* DOS_NT */
|
0
|
2929 }
|
|
2930
|
|
2931 /* If requested, replace the accessible part of the buffer
|
|
2932 with the file contents. Avoid replacing text at the
|
|
2933 beginning or end of the buffer that matches the file contents;
|
|
2934 that preserves markers pointing to the unchanged parts. */
|
211
|
2935 #if !defined (MULE)
|
0
|
2936 /* The replace-mode code currently only works when the assumption
|
211
|
2937 'one byte == one char' holds true. This fails Mule because
|
|
2938 files may contain multibyte characters. It holds under Windows NT
|
|
2939 provided we convert CRLF into LF. */
|
0
|
2940 # define FSFMACS_SPEEDY_INSERT
|
|
2941 #endif
|
|
2942 #ifndef FSFMACS_SPEEDY_INSERT
|
|
2943 if (!NILP (replace))
|
|
2944 {
|
|
2945 buffer_delete_range (buf, BUF_BEG (buf), BUF_Z (buf),
|
|
2946 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
2947 }
|
|
2948 #else /* FSFMACS_SPEEDY_INSERT */
|
|
2949 if (!NILP (replace))
|
|
2950 {
|
|
2951 char buffer[1 << 14];
|
|
2952 Bufpos same_at_start = BUF_BEGV (buf);
|
|
2953 Bufpos same_at_end = BUF_ZV (buf);
|
|
2954 int overlap;
|
211
|
2955 #ifdef DOS_NT
|
|
2956 /* Syncing with 19.34.6 note: same_at_start_in_file and
|
|
2957 same_at_end_in_file are not in XEmacs 20.4.
|
|
2958 First try to introduce them as-is and see what happens.
|
|
2959 Might be necessary to use constructs like
|
|
2960 st.st_size - (BUF_ZV (buf) - same_at_end)
|
|
2961 instead.
|
|
2962 --marcpa
|
|
2963 */
|
|
2964 /* Offset into the file where discrepancy begins. */
|
|
2965 int same_at_start_in_file = 0;
|
|
2966 /* Offset into the file where discrepancy ends. */
|
|
2967 int same_at_end_in_file = st.st_size;
|
|
2968 /* DOS_NT only: is there a `\r' character left in the buffer? */
|
|
2969 int cr_left_in_buffer = 0;
|
|
2970 /* DOS_NT only: was `\n' the first character in previous bufferful? */
|
|
2971 int last_was_lf = 0;
|
|
2972
|
|
2973 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
|
|
2974 /* Determine file type (text/binary) from its name.
|
|
2975 Note that the buffer_file_type changes here when the file
|
|
2976 being inserted is not of the same type as the original buffer. */
|
|
2977 current_buffer->buffer_file_type = call1 (Qfind_buffer_file_type, filename);
|
|
2978 if (NILP (current_buffer->buffer_file_type))
|
|
2979 crlf_conversion_required = 1;
|
|
2980 else if (current_buffer->buffer_file_type != Qt)
|
|
2981 /* Use heuristic to decide whether file is text or binary (based
|
|
2982 on the first bufferful) if buffer-file-type is not nil or t.
|
|
2983 If no decision is made (because no line endings were ever
|
|
2984 seen) then let buffer-file-type default to nil. */
|
|
2985 crlf_conversion_required = -1;
|
|
2986 #endif /* DOS_NT */
|
0
|
2987
|
|
2988 /* Count how many chars at the start of the file
|
|
2989 match the text at the beginning of the buffer. */
|
|
2990 while (1)
|
|
2991 {
|
|
2992 int nread;
|
|
2993 Bufpos bufpos;
|
211
|
2994 #ifdef DOS_NT
|
|
2995 if (cr_left_in_buffer)
|
|
2996 {
|
|
2997 nread = read_allowing_quit (fd, buffer + 1, sizeof(buffer) - 1);
|
|
2998 cr_left_in_buffer = 0;
|
|
2999 if (nread >= 0)
|
|
3000 nread++;
|
|
3001 }
|
|
3002 else
|
|
3003 #endif /* DOS_NT */
|
0
|
3004 nread = read_allowing_quit (fd, buffer, sizeof buffer);
|
|
3005 if (nread < 0)
|
|
3006 error ("IO error reading %s: %s",
|
16
|
3007 XSTRING_DATA (filename), strerror (errno));
|
0
|
3008 else if (nread == 0)
|
|
3009 break;
|
|
3010 bufpos = 0;
|
211
|
3011 #ifdef DOS_NT
|
|
3012 /* If requested, we do a simple check on the first bufferful
|
|
3013 to decide whether the file is binary or text. (If text, we
|
|
3014 count LF and CRLF occurences to determine whether the file
|
|
3015 was in Unix or DOS format.) */
|
|
3016 if (crlf_conversion_required < 0)
|
|
3017 {
|
|
3018 crlf_conversion_required = decide_buffer_type (buffer, nread);
|
|
3019 current_buffer->buffer_file_type =
|
|
3020 crlf_conversion_required ? Qnil : Qt;
|
|
3021 }
|
|
3022
|
|
3023 /* DOS_NT text files require that we ignore a `\r' before a `\n'. */
|
|
3024 if (crlf_conversion_required > 0)
|
|
3025 while (bufpos < nread && same_at_start < BUF_ZV (buf))
|
|
3026 {
|
|
3027 int filec = buffer[bufpos];
|
|
3028 int bufc = BUF_FETCH_CHAR (buf, same_at_start);
|
|
3029
|
|
3030 if (filec == '\n')
|
|
3031 lf_count++;
|
|
3032
|
|
3033 if (filec == bufc)
|
|
3034 same_at_start++, bufpos++, same_at_start_in_file++;
|
|
3035 else if (filec == '\r' && bufc == '\n')
|
|
3036 {
|
|
3037 /* If the `\r' is the last character in this buffer,
|
|
3038 it will be examined with the next bufferful. */
|
|
3039 if (bufpos == nread)
|
|
3040 {
|
|
3041 buffer[0] = filec;
|
|
3042 cr_left_in_buffer = 1;
|
|
3043 }
|
|
3044 else if (buffer[bufpos + 1] == bufc)
|
|
3045 {
|
|
3046 bufpos += 2;
|
|
3047 same_at_start_in_file += 2;
|
|
3048 same_at_start++;
|
|
3049 crlf_count++;
|
|
3050 lf_count++;
|
|
3051 }
|
|
3052 else
|
|
3053 break;
|
|
3054 }
|
|
3055 else
|
|
3056 break;
|
|
3057 }
|
|
3058 else
|
|
3059 #endif /* DOS_NT */
|
0
|
3060 while (bufpos < nread && same_at_start < BUF_ZV (buf)
|
|
3061 && BUF_FETCH_CHAR (buf, same_at_start) == buffer[bufpos])
|
211
|
3062 #ifdef DOS_NT
|
|
3063 same_at_start_in_file++,
|
|
3064 #endif
|
0
|
3065 same_at_start++, bufpos++;
|
|
3066 /* If we found a discrepancy, stop the scan.
|
211
|
3067 Otherwise loop around and scan the next bufferful. */
|
0
|
3068 if (bufpos != nread)
|
|
3069 break;
|
|
3070 }
|
|
3071 /* If the file matches the buffer completely,
|
|
3072 there's no need to replace anything. */
|
211
|
3073 #ifdef DOS_NT
|
|
3074 if (same_at_start_in_file == st.st_size)
|
|
3075 #else
|
0
|
3076 if (same_at_start - BUF_BEGV (buf) == st.st_size)
|
211
|
3077 #endif /* DOS_NT */
|
0
|
3078 {
|
|
3079 close (fd);
|
|
3080 unbind_to (speccount, Qnil);
|
|
3081 /* Truncate the buffer to the size of the file. */
|
|
3082 buffer_delete_range (buf, same_at_start, same_at_end,
|
|
3083 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3084 goto handled;
|
|
3085 }
|
|
3086 /* Count how many chars at the end of the file
|
|
3087 match the text at the end of the buffer. */
|
|
3088 while (1)
|
|
3089 {
|
|
3090 int total_read, nread;
|
|
3091 Bufpos bufpos, curpos, trial;
|
|
3092
|
|
3093 /* At what file position are we now scanning? */
|
211
|
3094 #ifdef DOS_NT
|
|
3095 curpos = same_at_end_in_file;
|
|
3096 #else
|
0
|
3097 curpos = st.st_size - (BUF_ZV (buf) - same_at_end);
|
211
|
3098 #endif /* DOS_NT */
|
0
|
3099 /* If the entire file matches the buffer tail, stop the scan. */
|
|
3100 if (curpos == 0)
|
|
3101 break;
|
|
3102 /* How much can we scan in the next step? */
|
|
3103 trial = min (curpos, sizeof buffer);
|
|
3104 if (lseek (fd, curpos - trial, 0) < 0)
|
|
3105 report_file_error ("Setting file position",
|
|
3106 Fcons (filename, Qnil));
|
|
3107
|
|
3108 total_read = 0;
|
|
3109 while (total_read < trial)
|
|
3110 {
|
|
3111 nread = read_allowing_quit (fd, buffer + total_read,
|
|
3112 trial - total_read);
|
|
3113 if (nread <= 0)
|
|
3114 error ("IO error reading %s: %s",
|
16
|
3115 XSTRING_DATA (filename), strerror (errno));
|
0
|
3116 total_read += nread;
|
|
3117 }
|
211
|
3118 /* Scan this bufferful from the end, comparing with
|
0
|
3119 the Emacs buffer. */
|
|
3120 bufpos = total_read;
|
211
|
3121 #ifdef DOS_NT
|
|
3122 /* DOS_NT text files require that we ignore a `\r' before a `\n'. */
|
|
3123 if (crlf_conversion_required)
|
|
3124 #endif /* DOS_NT */
|
0
|
3125 /* Compare with same_at_start to avoid counting some buffer text
|
|
3126 as matching both at the file's beginning and at the end. */
|
211
|
3127 #if !defined(DOS_NT)
|
0
|
3128 while (bufpos > 0 && same_at_end > same_at_start
|
|
3129 && BUF_FETCH_CHAR (buf, same_at_end - 1) ==
|
|
3130 buffer[bufpos - 1])
|
|
3131 same_at_end--, bufpos--;
|
211
|
3132 #else /* DOS_NT */
|
|
3133 while (bufpos > 0 && same_at_end > same_at_start
|
|
3134 && same_at_end_in_file > same_at_start_in_file)
|
|
3135 {
|
|
3136 int filec = buffer[bufpos - 1];
|
|
3137 int bufc = BUF_FETCH_CHAR (buf, same_at_end - 1);
|
|
3138
|
|
3139 /* Account for `\n' in previous bufferful. */
|
|
3140 if (last_was_lf && filec == '\r')
|
|
3141 {
|
|
3142 same_at_end_in_file--, bufpos--;
|
|
3143 last_was_lf = 0;
|
|
3144 crlf_count++;
|
|
3145 }
|
|
3146 else if (filec == bufc)
|
|
3147 {
|
|
3148 last_was_lf = 0;
|
|
3149 same_at_end--, same_at_end_in_file--, bufpos--;
|
|
3150 if (bufc == '\n')
|
|
3151 {
|
|
3152 lf_count++;
|
|
3153 if (bufpos <= 0)
|
|
3154 last_was_lf = 1;
|
|
3155 else if (same_at_end_in_file <= same_at_start_in_file)
|
|
3156 break;
|
|
3157 else if (buffer[bufpos - 1] == '\r')
|
|
3158 same_at_end_in_file--, bufpos--, crlf_count++;
|
|
3159 }
|
|
3160 }
|
|
3161 else
|
|
3162 {
|
|
3163 last_was_lf = 0;
|
|
3164 break;
|
|
3165 }
|
|
3166 }
|
|
3167 else
|
|
3168 while (bufpos > 0 && same_at_end > same_at_start
|
|
3169 && same_at_end_in_file > same_at_start_in_file
|
|
3170 && BUF_FETCH_CHAR (buf, same_at_end - 1) ==
|
|
3171 buffer[bufpos - 1])
|
|
3172 same_at_end--, same_at_end_in_file--, bufpos--;
|
|
3173 #endif /* !defined(DOS_NT) */
|
0
|
3174 /* If we found a discrepancy, stop the scan.
|
211
|
3175 Otherwise loop around and scan the preceding bufferful. */
|
0
|
3176 if (bufpos != 0)
|
|
3177 break;
|
|
3178 /* If display current starts at beginning of line,
|
|
3179 keep it that way. */
|
|
3180 if (XBUFFER (XWINDOW (Fselected_window (Qnil))->buffer) == buf)
|
|
3181 XWINDOW (Fselected_window (Qnil))->start_at_line_beg =
|
|
3182 !NILP (Fbolp (make_buffer (buf)));
|
|
3183 }
|
|
3184
|
|
3185 /* Don't try to reuse the same piece of text twice. */
|
|
3186 overlap = same_at_start - BUF_BEGV (buf) -
|
|
3187 (same_at_end + st.st_size - BUF_ZV (buf));
|
|
3188 if (overlap > 0)
|
|
3189 same_at_end += overlap;
|
|
3190
|
|
3191 /* Arrange to read only the nonmatching middle part of the file. */
|
211
|
3192 #ifdef DOS_NT
|
|
3193 beg = make_int (same_at_start_in_file);
|
|
3194 end = make_int (same_at_end_in_file);
|
|
3195 #else
|
0
|
3196 beg = make_int (same_at_start - BUF_BEGV (buf));
|
|
3197 end = make_int (st.st_size - (BUF_ZV (buf) - same_at_end));
|
211
|
3198 #endif /* DOS_NT */
|
0
|
3199
|
|
3200 buffer_delete_range (buf, same_at_start, same_at_end,
|
|
3201 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3202 /* Insert from the file at the proper position. */
|
|
3203 BUF_SET_PT (buf, same_at_start);
|
|
3204 }
|
|
3205 #endif /* FSFMACS_SPEEDY_INSERT */
|
|
3206
|
173
|
3207 if (!not_regular)
|
|
3208 {
|
|
3209 Lisp_Object temp;
|
|
3210
|
|
3211 total = XINT (end) - XINT (beg);
|
|
3212
|
|
3213 /* Make sure point-max won't overflow after this insertion. */
|
|
3214 XSETINT (temp, total);
|
|
3215 if (total != XINT (temp))
|
|
3216 error ("Maximum buffer size exceeded");
|
|
3217 }
|
|
3218 else
|
|
3219 /* For a special file, all we can do is guess. */
|
|
3220 total = READ_BUF_SIZE;
|
0
|
3221
|
|
3222 if (XINT (beg) != 0
|
|
3223 #ifdef FSFMACS_SPEEDY_INSERT
|
|
3224 /* why was this here? asked jwz. The reason is that the replace-mode
|
|
3225 connivings above will normally put the file pointer other than
|
|
3226 where it should be. */
|
|
3227 || !NILP (replace)
|
|
3228 #endif /* !FSFMACS_SPEEDY_INSERT */
|
|
3229 )
|
|
3230 {
|
|
3231 if (lseek (fd, XINT (beg), 0) < 0)
|
|
3232 report_file_error ("Setting file position",
|
|
3233 Fcons (filename, Qnil));
|
|
3234 }
|
|
3235
|
|
3236 {
|
|
3237 Bufpos cur_point = BUF_PT (buf);
|
|
3238 struct gcpro ngcpro1;
|
|
3239 Lisp_Object stream = make_filedesc_input_stream (fd, 0, total,
|
|
3240 LSTR_ALLOW_QUIT);
|
|
3241
|
|
3242 NGCPRO1 (stream);
|
|
3243 Lstream_set_buffering (XLSTREAM (stream), LSTREAM_BLOCKN_BUFFERED, 65536);
|
70
|
3244 #ifdef MULE
|
|
3245 stream = make_decoding_input_stream
|
|
3246 (XLSTREAM (stream), Fget_coding_system (codesys));
|
|
3247 Lstream_set_character_mode (XLSTREAM (stream));
|
|
3248 Lstream_set_buffering (XLSTREAM (stream), LSTREAM_BLOCKN_BUFFERED, 65536);
|
183
|
3249 #endif /* MULE */
|
0
|
3250
|
|
3251 record_unwind_protect (close_stream_unwind, stream);
|
|
3252
|
16
|
3253 /* No need to limit the amount of stuff we attempt to read. (It would
|
|
3254 be incorrect, anyway, when Mule is enabled.) Instead, the limiting
|
|
3255 occurs inside of the filedesc stream. */
|
0
|
3256 while (1)
|
|
3257 {
|
|
3258 Bytecount this_len;
|
|
3259 Charcount cc_inserted;
|
|
3260
|
|
3261 QUIT;
|
|
3262 this_len = Lstream_read (XLSTREAM (stream), read_buf,
|
|
3263 sizeof (read_buf));
|
151
|
3264
|
0
|
3265 if (this_len <= 0)
|
|
3266 {
|
|
3267 if (this_len < 0)
|
|
3268 saverrno = errno;
|
|
3269 break;
|
|
3270 }
|
211
|
3271 #ifdef DOS_NT
|
|
3272 /* XEmacs (--marcpa) change: FSF does buffer_insert_raw_string_1() first
|
|
3273 then checks if conversion is needed, calling lisp
|
|
3274 (find-buffer-file-type) which can call a user-function that
|
|
3275 might look at the unconverted buffer to decide if
|
|
3276 conversion is needed.
|
|
3277 I removed the possibility for lisp functions called from
|
|
3278 find-buffer-file-type to look at the buffer's content, for
|
|
3279 simplicity reasons: it is easier to do the CRLF -> LF
|
|
3280 conversion on read_buf than on buffer contents because
|
|
3281 BUF_FETCH_CHAR does not return a pointer to an unsigned
|
|
3282 char memory location, and because we must cope with bytind
|
|
3283 VS bufpos in XEmacs, thus complicating crlf_to_lf().
|
|
3284 This decision (of doing Lstream_read(), crlf_to_lf() then
|
|
3285 buffer_insert_raw_string_1()) is debatable.
|
|
3286 --marcpa
|
|
3287 */
|
|
3288 /* Following FSF note no longer apply now. See comment above.
|
|
3289 --marcpa*/
|
|
3290 /* For compatability with earlier versions that did not support the
|
|
3291 REPLACE funtionality, we call find-buffer-file-type after inserting
|
|
3292 the contents to allow it to inspect the inserted data. (This was
|
|
3293 not intentional usage, but proved to be quite useful.) */
|
|
3294 if (NILP (replace))
|
|
3295 {
|
|
3296 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
|
|
3297 /* Determine file type (text/binary) from its name.
|
|
3298 Note that the buffer_file_type changes here when the file
|
|
3299 being inserted is not of the same type as the original buffer. */
|
|
3300 current_buffer->buffer_file_type = call1 (Qfind_buffer_file_type, filename);
|
|
3301 if (NILP (current_buffer->buffer_file_type))
|
|
3302 crlf_conversion_required = 1;
|
|
3303 else if (current_buffer->buffer_file_type != Qt)
|
|
3304 /* Use heuristic to decide whether file is text or binary (based
|
|
3305 on the first bufferful) if buffer-file-type is not nil or t.
|
|
3306 If no decision is made (because no line endings were ever
|
|
3307 seen) then let buffer-file-type default to nil. */
|
|
3308 crlf_conversion_required = -1;
|
|
3309 }
|
|
3310
|
|
3311 /* If requested, we check the inserted data to decide whether the file
|
|
3312 is binary or text. (If text, we count LF and CRLF occurences to
|
|
3313 determine whether the file was in Unix or DOS format.) */
|
|
3314 if (crlf_conversion_required < 0)
|
|
3315 {
|
|
3316 crlf_conversion_required =
|
|
3317 decide_buffer_type (read_buf, this_len);
|
|
3318 current_buffer->buffer_file_type =
|
|
3319 crlf_conversion_required ? Qnil : Qt;
|
|
3320 }
|
|
3321
|
|
3322 /* Demacs 1.1.1 91/10/16 HIRANO Satoshi, MW July 1993 */
|
|
3323 /* Remove CRs from CR-LFs if the file is deemed to be a text file. */
|
|
3324 if (crlf_conversion_required)
|
|
3325 {
|
|
3326 int reduced_size
|
|
3327 = this_len - crlf_to_lf (this_len, read_buf,
|
|
3328 &lf_count);
|
|
3329 crlf_count += reduced_size;
|
|
3330 /* XEmacs (--marcpa) change: No need for this since we havent
|
|
3331 inserted in buffer yet. */
|
|
3332 #if 0
|
|
3333 ZV -= reduced_size;
|
|
3334 Z -= reduced_size;
|
|
3335 GPT -= reduced_size;
|
|
3336 GAP_SIZE += reduced_size;
|
|
3337 inserted -= reduced_size;
|
|
3338 #endif
|
|
3339 this_len -= reduced_size;
|
|
3340
|
|
3341 /* Change buffer_file_type back to binary if Unix eol format. */
|
|
3342 if (crlf_count == 0 && lf_count > 0)
|
|
3343 current_buffer->buffer_file_type = Qt;
|
|
3344 }
|
|
3345
|
|
3346 /* Make crlf_count and lf_count available for inspection. */
|
|
3347 Fset (intern ("buffer-file-lines"), make_int (lf_count));
|
|
3348 Fset (intern ("buffer-file-dos-lines"), make_int (crlf_count));
|
|
3349 #endif /* DOS_NT */
|
0
|
3350
|
|
3351 cc_inserted = buffer_insert_raw_string_1 (buf, cur_point, read_buf,
|
|
3352 this_len,
|
|
3353 !NILP (visit)
|
|
3354 ? INSDEL_NO_LOCKING : 0);
|
|
3355 inserted += cc_inserted;
|
|
3356 cur_point += cc_inserted;
|
|
3357 }
|
70
|
3358 #ifdef MULE
|
|
3359 if (!NILP (used_codesys))
|
|
3360 {
|
|
3361 Fset (used_codesys,
|
|
3362 XCODING_SYSTEM_NAME (decoding_stream_coding_system (XLSTREAM (stream))));
|
|
3363 }
|
183
|
3364 #endif /* MULE */
|
0
|
3365 NUNGCPRO;
|
|
3366 }
|
|
3367
|
|
3368 /* Close the file/stream */
|
|
3369 unbind_to (speccount, Qnil);
|
|
3370
|
|
3371 if (saverrno != 0)
|
|
3372 {
|
|
3373 error ("IO error reading %s: %s",
|
16
|
3374 XSTRING_DATA (filename), strerror (saverrno));
|
0
|
3375 }
|
|
3376
|
|
3377 notfound:
|
|
3378 handled:
|
|
3379
|
|
3380 end_multiple_change (buf, mc_count);
|
|
3381
|
|
3382 if (!NILP (visit))
|
|
3383 {
|
|
3384 if (!EQ (buf->undo_list, Qt))
|
|
3385 buf->undo_list = Qnil;
|
|
3386 #ifdef APOLLO
|
16
|
3387 stat ((char *) XSTRING_DATA (filename), &st);
|
0
|
3388 #endif
|
|
3389 if (NILP (handler))
|
|
3390 {
|
|
3391 buf->modtime = st.st_mtime;
|
|
3392 buf->filename = filename;
|
|
3393 /* XEmacs addition: */
|
|
3394 /* This function used to be in C, ostensibly so that
|
|
3395 it could be called here. But that's just silly.
|
|
3396 There's no reason C code can't call out to Lisp
|
|
3397 code, and it's a lot cleaner this way. */
|
|
3398 if (!NILP (Ffboundp (Qcompute_buffer_file_truename)))
|
|
3399 call1 (Qcompute_buffer_file_truename, make_buffer (buf));
|
|
3400 }
|
|
3401 BUF_SAVE_MODIFF (buf) = BUF_MODIFF (buf);
|
|
3402 buf->auto_save_modified = BUF_MODIFF (buf);
|
|
3403 buf->save_length = make_int (BUF_SIZE (buf));
|
|
3404 #ifdef CLASH_DETECTION
|
|
3405 if (NILP (handler))
|
|
3406 {
|
|
3407 if (!NILP (buf->file_truename))
|
|
3408 unlock_file (buf->file_truename);
|
|
3409 unlock_file (filename);
|
|
3410 }
|
|
3411 #endif /* CLASH_DETECTION */
|
|
3412 if (not_regular)
|
|
3413 RETURN_UNGCPRO (Fsignal (Qfile_error,
|
|
3414 list2 (build_string ("not a regular file"),
|
|
3415 filename)));
|
|
3416
|
|
3417 /* If visiting nonexistent file, return nil. */
|
|
3418 if (buf->modtime == -1)
|
|
3419 report_file_error ("Opening input file",
|
|
3420 list1 (filename));
|
|
3421 }
|
|
3422
|
|
3423 /* Decode file format */
|
|
3424 if (inserted > 0)
|
|
3425 {
|
151
|
3426 Lisp_Object insval = call3 (Qformat_decode,
|
0
|
3427 Qnil, make_int (inserted), visit);
|
|
3428 CHECK_INT (insval);
|
|
3429 inserted = XINT (insval);
|
|
3430 }
|
|
3431
|
|
3432 if (inserted > 0)
|
|
3433 {
|
|
3434 Lisp_Object p = Vafter_insert_file_functions;
|
|
3435 struct gcpro ngcpro1;
|
|
3436
|
|
3437 NGCPRO1 (p);
|
|
3438 while (!NILP (p))
|
|
3439 {
|
|
3440 Lisp_Object insval =
|
|
3441 call1 (Fcar (p), make_int (inserted));
|
|
3442 if (!NILP (insval))
|
|
3443 {
|
|
3444 CHECK_NATNUM (insval);
|
|
3445 inserted = XINT (insval);
|
|
3446 }
|
|
3447 QUIT;
|
|
3448 p = Fcdr (p);
|
|
3449 }
|
|
3450 NUNGCPRO;
|
|
3451 }
|
|
3452
|
|
3453 UNGCPRO;
|
|
3454
|
|
3455 if (!NILP (val))
|
|
3456 return (val);
|
|
3457 else
|
|
3458 return (list2 (filename, make_int (inserted)));
|
|
3459 }
|
|
3460
|
|
3461
|
|
3462 static int a_write (Lisp_Object outstream, Lisp_Object instream, int pos,
|
|
3463 Lisp_Object *annot);
|
|
3464 static Lisp_Object build_annotations (Lisp_Object start, Lisp_Object end);
|
|
3465
|
|
3466 /* If build_annotations switched buffers, switch back to BUF.
|
|
3467 Kill the temporary buffer that was selected in the meantime. */
|
|
3468
|
151
|
3469 static Lisp_Object
|
0
|
3470 build_annotations_unwind (Lisp_Object buf)
|
|
3471 {
|
|
3472 Lisp_Object tembuf;
|
|
3473
|
|
3474 if (XBUFFER (buf) == current_buffer)
|
|
3475 return Qnil;
|
|
3476 tembuf = Fcurrent_buffer ();
|
|
3477 Fset_buffer (buf);
|
|
3478 Fkill_buffer (tembuf);
|
|
3479 return Qnil;
|
|
3480 }
|
|
3481
|
70
|
3482 DEFUN ("write-region-internal", Fwrite_region_internal, 3, 7,
|
20
|
3483 "r\nFWrite region to file: ", /*
|
70
|
3484 Write current region into specified file; no coding-system frobbing.
|
|
3485 This function is identical to `write-region' except for the handling
|
|
3486 of the CODESYS argument under XEmacs/Mule. (When Mule support is not
|
|
3487 present, both functions are identical and ignore the CODESYS argument.)
|
|
3488 If support for Mule exists in this Emacs, the file is encoded according
|
|
3489 to the value of CODESYS. If this is nil, no code conversion occurs.
|
20
|
3490 */
|
70
|
3491 (start, end, filename, append, visit, lockname, codesys))
|
0
|
3492 {
|
116
|
3493 /* This function can call lisp */
|
0
|
3494 int desc;
|
|
3495 int failure;
|
|
3496 int save_errno = 0;
|
|
3497 struct stat st;
|
|
3498 Lisp_Object fn;
|
|
3499 int speccount = specpdl_depth ();
|
|
3500 int visiting_other = STRINGP (visit);
|
|
3501 int visiting = (EQ (visit, Qt) || visiting_other);
|
|
3502 int quietly = (!visiting && !NILP (visit));
|
|
3503 Lisp_Object visit_file = Qnil;
|
|
3504 Lisp_Object annotations = Qnil;
|
|
3505 struct buffer *given_buffer;
|
|
3506 Bufpos start1, end1;
|
|
3507
|
114
|
3508 /* #### dmoore - if Fexpand_file_name or handlers kill the buffer,
|
|
3509 we should signal an error rather than blissfully continuing
|
|
3510 along. ARGH, this function is going to lose lose lose. We need
|
|
3511 to protect the current_buffer from being destroyed, but the
|
|
3512 multiple return points make this a pain in the butt. */
|
|
3513
|
0
|
3514 #ifdef DOS_NT
|
|
3515 int buffer_file_type
|
|
3516 = NILP (current_buffer->buffer_file_type) ? O_TEXT : O_BINARY;
|
|
3517 #endif /* DOS_NT */
|
|
3518
|
70
|
3519 #ifdef MULE
|
|
3520 codesys = Fget_coding_system (codesys);
|
|
3521 #endif /* MULE */
|
|
3522
|
0
|
3523 if (current_buffer->base_buffer && ! NILP (visit))
|
|
3524 error ("Cannot do file visiting in an indirect buffer");
|
|
3525
|
|
3526 if (!NILP (start) && !STRINGP (start))
|
|
3527 get_buffer_range_char (current_buffer, start, end, &start1, &end1, 0);
|
|
3528
|
|
3529 {
|
|
3530 Lisp_Object handler;
|
|
3531 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
114
|
3532
|
0
|
3533 GCPRO5 (start, filename, visit, visit_file, lockname);
|
|
3534
|
|
3535 if (visiting_other)
|
|
3536 visit_file = Fexpand_file_name (visit, Qnil);
|
|
3537 else
|
|
3538 visit_file = filename;
|
|
3539 filename = Fexpand_file_name (filename, Qnil);
|
|
3540
|
|
3541 UNGCPRO;
|
|
3542
|
|
3543 if (NILP (lockname))
|
|
3544 lockname = visit_file;
|
|
3545
|
|
3546 /* If the file name has special constructs in it,
|
|
3547 call the corresponding file handler. */
|
|
3548 handler = Ffind_file_name_handler (filename, Qwrite_region);
|
|
3549 /* If FILENAME has no handler, see if VISIT has one. */
|
|
3550 if (NILP (handler) && STRINGP (visit))
|
|
3551 handler = Ffind_file_name_handler (visit, Qwrite_region);
|
|
3552
|
|
3553 if (!NILP (handler))
|
|
3554 {
|
151
|
3555 Lisp_Object val = call8 (handler, Qwrite_region, start, end,
|
86
|
3556 filename, append, visit, lockname, codesys);
|
0
|
3557 if (visiting)
|
|
3558 {
|
|
3559 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
|
|
3560 current_buffer->save_length =
|
|
3561 make_int (BUF_SIZE (current_buffer));
|
|
3562 current_buffer->filename = visit_file;
|
|
3563 MARK_MODELINE_CHANGED;
|
|
3564 }
|
|
3565 return val;
|
|
3566 }
|
|
3567 }
|
|
3568
|
|
3569 #ifdef CLASH_DETECTION
|
|
3570 if (!auto_saving)
|
|
3571 {
|
114
|
3572 Lisp_Object curbuf;
|
|
3573 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
3574
|
|
3575 XSETBUFFER (curbuf, current_buffer);
|
|
3576 GCPRO5 (start, filename, visit_file, lockname, curbuf);
|
0
|
3577 lock_file (lockname);
|
|
3578 UNGCPRO;
|
|
3579 }
|
|
3580 #endif /* CLASH_DETECTION */
|
|
3581
|
|
3582 /* Special kludge to simplify auto-saving. */
|
|
3583 if (NILP (start))
|
|
3584 {
|
|
3585 start1 = BUF_BEG (current_buffer);
|
|
3586 end1 = BUF_Z (current_buffer);
|
|
3587 }
|
|
3588
|
|
3589 record_unwind_protect (build_annotations_unwind, Fcurrent_buffer ());
|
|
3590
|
|
3591 given_buffer = current_buffer;
|
|
3592 annotations = build_annotations (start, end);
|
|
3593 if (current_buffer != given_buffer)
|
|
3594 {
|
|
3595 start1 = BUF_BEGV (current_buffer);
|
|
3596 end1 = BUF_ZV (current_buffer);
|
|
3597 }
|
|
3598
|
|
3599 fn = filename;
|
|
3600 desc = -1;
|
|
3601 if (!NILP (append))
|
|
3602 #ifdef DOS_NT
|
16
|
3603 desc = open ((char *) XSTRING_DATA (fn),
|
211
|
3604 (O_WRONLY | buffer_file_type), 0);
|
0
|
3605 #else /* not DOS_NT */
|
16
|
3606 desc = open ((char *) XSTRING_DATA (fn), O_WRONLY, 0);
|
0
|
3607 #endif /* not DOS_NT */
|
|
3608
|
|
3609 if (desc < 0)
|
|
3610 {
|
|
3611 #ifdef DOS_NT
|
151
|
3612 desc = open ((char *) XSTRING_DATA (fn),
|
211
|
3613 (O_WRONLY | O_TRUNC | O_CREAT | buffer_file_type),
|
0
|
3614 (S_IREAD | S_IWRITE));
|
|
3615 #else /* not DOS_NT */
|
16
|
3616 desc = creat ((char *) XSTRING_DATA (fn),
|
0
|
3617 ((auto_saving) ? auto_save_mode_bits : 0666));
|
|
3618 #endif /* DOS_NT */
|
|
3619 }
|
|
3620
|
|
3621 if (desc < 0)
|
|
3622 {
|
|
3623 #ifdef CLASH_DETECTION
|
|
3624 save_errno = errno;
|
|
3625 if (!auto_saving) unlock_file (lockname);
|
|
3626 errno = save_errno;
|
|
3627 #endif /* CLASH_DETECTION */
|
|
3628 report_file_error ("Opening output file",
|
|
3629 Fcons (filename, Qnil));
|
|
3630 }
|
|
3631
|
|
3632 {
|
|
3633 Lisp_Object desc_locative = Fcons (make_int (desc), Qnil);
|
|
3634 Lisp_Object instream = Qnil, outstream = Qnil;
|
|
3635 struct gcpro gcpro1, gcpro2;
|
|
3636 /* need to gcpro; QUIT could happen out of call to write() */
|
|
3637 GCPRO2 (instream, outstream);
|
|
3638
|
|
3639 record_unwind_protect (close_file_unwind, desc_locative);
|
|
3640
|
|
3641 if (!NILP (append))
|
|
3642 {
|
|
3643 if (lseek (desc, 0, 2) < 0)
|
|
3644 {
|
|
3645 #ifdef CLASH_DETECTION
|
|
3646 if (!auto_saving) unlock_file (lockname);
|
|
3647 #endif /* CLASH_DETECTION */
|
|
3648 report_file_error ("Lseek error",
|
|
3649 list1 (filename));
|
|
3650 }
|
|
3651 }
|
|
3652
|
|
3653 failure = 0;
|
|
3654
|
|
3655 /* Note: I tried increasing the buffering size, along with
|
|
3656 various other tricks, but nothing seemed to make much of
|
|
3657 a difference in the time it took to save a large file.
|
|
3658 (Actually that's not true. With a local disk, changing
|
|
3659 the buffer size doesn't seem to make much difference.
|
|
3660 With an NFS-mounted disk, it could make a lot of difference
|
|
3661 because you're affecting the number of network requests
|
|
3662 that need to be made, and there could be a large latency
|
|
3663 for each request. So I've increased the buffer size
|
|
3664 to 64K.) */
|
|
3665 outstream = make_filedesc_output_stream (desc, 0, -1, 0);
|
|
3666 Lstream_set_buffering (XLSTREAM (outstream),
|
|
3667 LSTREAM_BLOCKN_BUFFERED, 65536);
|
70
|
3668 #ifdef MULE
|
|
3669 outstream =
|
185
|
3670 make_encoding_output_stream (XLSTREAM (outstream), codesys);
|
70
|
3671 Lstream_set_buffering (XLSTREAM (outstream),
|
|
3672 LSTREAM_BLOCKN_BUFFERED, 65536);
|
183
|
3673 #endif /* MULE */
|
0
|
3674 if (STRINGP (start))
|
|
3675 {
|
|
3676 instream = make_lisp_string_input_stream (start, 0, -1);
|
|
3677 start1 = 0;
|
|
3678 }
|
|
3679 else
|
|
3680 instream = make_lisp_buffer_input_stream (current_buffer, start1, end1,
|
|
3681 LSTR_SELECTIVE |
|
|
3682 LSTR_IGNORE_ACCESSIBLE);
|
|
3683 failure = (0 > (a_write (outstream, instream, start1,
|
|
3684 &annotations)));
|
|
3685 save_errno = errno;
|
|
3686 /* Note that this doesn't close the desc since we created the
|
|
3687 stream without the LSTR_CLOSING flag, but it does
|
|
3688 flush out any buffered data. */
|
|
3689 if (Lstream_close (XLSTREAM (outstream)) < 0)
|
|
3690 {
|
|
3691 failure = 1;
|
|
3692 save_errno = errno;
|
|
3693 }
|
|
3694 Lstream_close (XLSTREAM (instream));
|
|
3695 UNGCPRO;
|
|
3696
|
|
3697 #ifdef HAVE_FSYNC
|
|
3698 /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
|
|
3699 Disk full in NFS may be reported here. */
|
|
3700 /* mib says that closing the file will try to write as fast as NFS can do
|
|
3701 it, and that means the fsync here is not crucial for autosave files. */
|
|
3702 if (!auto_saving && fsync (desc) < 0
|
|
3703 /* If fsync fails with EINTR, don't treat that as serious. */
|
|
3704 && errno != EINTR)
|
|
3705 {
|
|
3706 failure = 1;
|
|
3707 save_errno = errno;
|
|
3708 }
|
183
|
3709 #endif /* HAVE_FSYNC */
|
0
|
3710
|
151
|
3711 /* Spurious "file has changed on disk" warnings have been
|
0
|
3712 observed on Suns as well.
|
|
3713 It seems that `close' can change the modtime, under nfs.
|
|
3714
|
|
3715 (This has supposedly been fixed in Sunos 4,
|
|
3716 but who knows about all the other machines with NFS?) */
|
|
3717 /* On VMS and APOLLO, must do the stat after the close
|
|
3718 since closing changes the modtime. */
|
|
3719 #if 0 /* !defined (VMS) && !defined (APOLLO) */
|
|
3720 fstat (desc, &st);
|
|
3721 #endif
|
|
3722
|
|
3723 /* NFS can report a write failure now. */
|
|
3724 if (close (desc) < 0)
|
|
3725 {
|
|
3726 failure = 1;
|
|
3727 save_errno = errno;
|
|
3728 }
|
|
3729
|
|
3730 /* Discard the close unwind-protect. Execute the one for
|
|
3731 build_annotations (switches back to the original current buffer
|
|
3732 as necessary). */
|
|
3733 XCAR (desc_locative) = Qnil;
|
|
3734 unbind_to (speccount, Qnil);
|
|
3735 }
|
151
|
3736
|
0
|
3737
|
|
3738 #if 1 /* defined (VMS) || defined (APOLLO) */
|
16
|
3739 stat ((char *) XSTRING_DATA (fn), &st);
|
0
|
3740 #endif
|
|
3741
|
|
3742 #ifdef CLASH_DETECTION
|
|
3743 if (!auto_saving)
|
|
3744 unlock_file (lockname);
|
|
3745 #endif /* CLASH_DETECTION */
|
|
3746
|
|
3747 /* Do this before reporting IO error
|
|
3748 to avoid a "file has changed on disk" warning on
|
|
3749 next attempt to save. */
|
|
3750 if (visiting)
|
|
3751 current_buffer->modtime = st.st_mtime;
|
|
3752
|
|
3753 if (failure)
|
151
|
3754 error ("IO error writing %s: %s",
|
|
3755 XSTRING_DATA (fn),
|
0
|
3756 strerror (save_errno));
|
|
3757
|
|
3758 if (visiting)
|
|
3759 {
|
|
3760 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
|
|
3761 current_buffer->save_length = make_int (BUF_SIZE (current_buffer));
|
|
3762 current_buffer->filename = visit_file;
|
|
3763 MARK_MODELINE_CHANGED;
|
|
3764 }
|
|
3765 else if (quietly)
|
|
3766 {
|
|
3767 return Qnil;
|
|
3768 }
|
|
3769
|
|
3770 if (!auto_saving)
|
|
3771 {
|
|
3772 if (visiting_other)
|
16
|
3773 message ("Wrote %s", XSTRING_DATA (visit_file));
|
0
|
3774 else
|
|
3775 {
|
|
3776 struct gcpro gcpro1;
|
|
3777 Lisp_Object fsp;
|
|
3778 GCPRO1 (fn);
|
|
3779
|
|
3780 fsp = Ffile_symlink_p (fn);
|
|
3781 if (NILP (fsp))
|
16
|
3782 message ("Wrote %s", XSTRING_DATA (fn));
|
0
|
3783 else
|
151
|
3784 message ("Wrote %s (symlink to %s)",
|
16
|
3785 XSTRING_DATA (fn), XSTRING_DATA (fsp));
|
0
|
3786 UNGCPRO;
|
|
3787 }
|
|
3788 }
|
|
3789 return Qnil;
|
|
3790 }
|
|
3791
|
|
3792 /* #### This is such a load of shit!!!! There is no way we should define
|
|
3793 something so stupid as a subr, just sort the fucking list more
|
|
3794 intelligently. */
|
20
|
3795 DEFUN ("car-less-than-car", Fcar_less_than_car, 2, 2, 0, /*
|
0
|
3796 Return t if (car A) is numerically less than (car B).
|
20
|
3797 */
|
|
3798 (a, b))
|
0
|
3799 {
|
|
3800 return Flss (Fcar (a), Fcar (b));
|
|
3801 }
|
|
3802
|
|
3803 /* Heh heh heh, let's define this too, just to aggravate the person who
|
|
3804 wrote the above comment. */
|
20
|
3805 DEFUN ("cdr-less-than-cdr", Fcdr_less_than_cdr, 2, 2, 0, /*
|
0
|
3806 Return t if (cdr A) is numerically less than (cdr B).
|
20
|
3807 */
|
|
3808 (a, b))
|
0
|
3809 {
|
|
3810 return Flss (Fcdr (a), Fcdr (b));
|
|
3811 }
|
|
3812
|
|
3813 /* Build the complete list of annotations appropriate for writing out
|
|
3814 the text between START and END, by calling all the functions in
|
|
3815 write-region-annotate-functions and merging the lists they return.
|
|
3816 If one of these functions switches to a different buffer, we assume
|
|
3817 that buffer contains altered text. Therefore, the caller must
|
|
3818 make sure to restore the current buffer in all cases,
|
|
3819 as save-excursion would do. */
|
|
3820
|
|
3821 static Lisp_Object
|
|
3822 build_annotations (Lisp_Object start, Lisp_Object end)
|
|
3823 {
|
|
3824 /* This function can GC */
|
|
3825 Lisp_Object annotations;
|
|
3826 Lisp_Object p, res;
|
|
3827 struct gcpro gcpro1, gcpro2;
|
215
|
3828 Lisp_Object original_buffer;
|
|
3829
|
|
3830 XSETBUFFER (original_buffer, current_buffer);
|
0
|
3831
|
|
3832 annotations = Qnil;
|
|
3833 p = Vwrite_region_annotate_functions;
|
|
3834 GCPRO2 (annotations, p);
|
|
3835 while (!NILP (p))
|
|
3836 {
|
|
3837 struct buffer *given_buffer = current_buffer;
|
|
3838 Vwrite_region_annotations_so_far = annotations;
|
|
3839 res = call2 (Fcar (p), start, end);
|
|
3840 /* If the function makes a different buffer current,
|
|
3841 assume that means this buffer contains altered text to be output.
|
|
3842 Reset START and END from the buffer bounds
|
|
3843 and discard all previous annotations because they should have
|
|
3844 been dealt with by this function. */
|
|
3845 if (current_buffer != given_buffer)
|
|
3846 {
|
|
3847 start = make_int (BUF_BEGV (current_buffer));
|
|
3848 end = make_int (BUF_ZV (current_buffer));
|
|
3849 annotations = Qnil;
|
|
3850 }
|
183
|
3851 Flength (res); /* Check basic validity of return value */
|
0
|
3852 annotations = merge (annotations, res, Qcar_less_than_car);
|
|
3853 p = Fcdr (p);
|
|
3854 }
|
|
3855
|
|
3856 /* Now do the same for annotation functions implied by the file-format */
|
|
3857 if (auto_saving && (!EQ (Vauto_save_file_format, Qt)))
|
|
3858 p = Vauto_save_file_format;
|
|
3859 else
|
|
3860 p = current_buffer->file_format;
|
|
3861 while (!NILP (p))
|
|
3862 {
|
|
3863 struct buffer *given_buffer = current_buffer;
|
|
3864 Vwrite_region_annotations_so_far = annotations;
|
215
|
3865 res = call4 (Qformat_annotate_function, Fcar (p), start, end,
|
|
3866 original_buffer);
|
0
|
3867 if (current_buffer != given_buffer)
|
|
3868 {
|
|
3869 start = make_int (BUF_BEGV (current_buffer));
|
|
3870 end = make_int (BUF_ZV (current_buffer));
|
|
3871 annotations = Qnil;
|
|
3872 }
|
183
|
3873 Flength (res);
|
0
|
3874 annotations = merge (annotations, res, Qcar_less_than_car);
|
|
3875 p = Fcdr (p);
|
|
3876 }
|
|
3877 UNGCPRO;
|
|
3878 return annotations;
|
|
3879 }
|
|
3880
|
|
3881 /* Write to stream OUTSTREAM the characters from INSTREAM (it is read until
|
|
3882 EOF is encountered), assuming they start at position POS in the buffer
|
|
3883 of string that STREAM refers to. Intersperse with them the annotations
|
|
3884 from *ANNOT that fall into the range of positions we are reading from,
|
|
3885 each at its appropriate position.
|
|
3886
|
|
3887 Modify *ANNOT by discarding elements as we output them.
|
|
3888 The return value is negative in case of system call failure. */
|
|
3889
|
|
3890 /* 4K should probably be fine. We just need to reduce the number of
|
|
3891 function calls to reasonable level. The Lstream stuff itself will
|
|
3892 batch to 64K to reduce the number of system calls. */
|
|
3893
|
|
3894 #define A_WRITE_BATCH_SIZE 4096
|
|
3895
|
|
3896 static int
|
|
3897 a_write (Lisp_Object outstream, Lisp_Object instream, int pos,
|
|
3898 Lisp_Object *annot)
|
|
3899 {
|
|
3900 Lisp_Object tem;
|
|
3901 int nextpos;
|
|
3902 unsigned char largebuf[A_WRITE_BATCH_SIZE];
|
|
3903 Lstream *instr = XLSTREAM (instream);
|
|
3904 Lstream *outstr = XLSTREAM (outstream);
|
|
3905
|
|
3906 while (NILP (*annot) || CONSP (*annot))
|
|
3907 {
|
|
3908 tem = Fcar_safe (Fcar (*annot));
|
|
3909 if (INTP (tem))
|
|
3910 nextpos = XINT (tem);
|
|
3911 else
|
|
3912 nextpos = INT_MAX;
|
70
|
3913 #ifdef MULE
|
|
3914 /* If there are annotations left and we have Mule, then we
|
|
3915 have to do the I/O one emchar at a time so we can
|
|
3916 determine when to insert the annotation. */
|
|
3917 if (!NILP (*annot))
|
|
3918 {
|
|
3919 Emchar ch;
|
|
3920 while (pos != nextpos && (ch = Lstream_get_emchar (instr)) != EOF)
|
|
3921 {
|
|
3922 if (Lstream_put_emchar (outstr, ch) < 0)
|
|
3923 return -1;
|
|
3924 pos++;
|
|
3925 }
|
|
3926 }
|
|
3927 else
|
183
|
3928 #endif /* MULE */
|
0
|
3929 {
|
|
3930 while (pos != nextpos)
|
|
3931 {
|
|
3932 /* Otherwise there is no point to that. Just go in batches. */
|
|
3933 int chunk = min (nextpos - pos, A_WRITE_BATCH_SIZE);
|
151
|
3934
|
0
|
3935 chunk = Lstream_read (instr, largebuf, chunk);
|
|
3936 if (chunk < 0)
|
|
3937 return -1;
|
|
3938 if (chunk == 0) /* EOF */
|
|
3939 break;
|
|
3940 if (Lstream_write (outstr, largebuf, chunk) < chunk)
|
|
3941 return -1;
|
|
3942 pos += chunk;
|
|
3943 }
|
|
3944 }
|
|
3945 if (pos == nextpos)
|
|
3946 {
|
|
3947 tem = Fcdr (Fcar (*annot));
|
|
3948 if (STRINGP (tem))
|
|
3949 {
|
16
|
3950 if (Lstream_write (outstr, XSTRING_DATA (tem),
|
|
3951 XSTRING_LENGTH (tem)) < 0)
|
0
|
3952 return -1;
|
|
3953 }
|
|
3954 *annot = Fcdr (*annot);
|
|
3955 }
|
|
3956 else
|
|
3957 return 0;
|
|
3958 }
|
|
3959 return -1;
|
|
3960 }
|
|
3961
|
|
3962
|
|
3963
|
|
3964 #if 0
|
|
3965 #include <des_crypt.h>
|
|
3966
|
|
3967 #define CRYPT_BLOCK_SIZE 8 /* bytes */
|
|
3968 #define CRYPT_KEY_SIZE 8 /* bytes */
|
|
3969
|
20
|
3970 DEFUN ("encrypt-string", Fencrypt_string, 2, 2, 0, /*
|
0
|
3971 Encrypt STRING using KEY.
|
20
|
3972 */
|
|
3973 (string, key))
|
0
|
3974 {
|
|
3975 char *encrypted_string, *raw_key;
|
|
3976 int rounded_size, extra, key_size;
|
|
3977
|
|
3978 /* !!#### May produce bogus data under Mule. */
|
|
3979 CHECK_STRING (string);
|
|
3980 CHECK_STRING (key);
|
|
3981
|
16
|
3982 extra = XSTRING_LENGTH (string) % CRYPT_BLOCK_SIZE;
|
|
3983 rounded_size = XSTRING_LENGTH (string) + extra;
|
0
|
3984 encrypted_string = alloca (rounded_size + 1);
|
16
|
3985 memcpy (encrypted_string, XSTRING_DATA (string), XSTRING_LENGTH (string));
|
0
|
3986 memset (encrypted_string + rounded_size - extra, 0, extra + 1);
|
|
3987
|
183
|
3988 key_size = min (CRYPT_KEY_SIZE, XSTRING_LENGTH (key))
|
0
|
3989
|
|
3990 raw_key = alloca (CRYPT_KEY_SIZE + 1);
|
16
|
3991 memcpy (raw_key, XSTRING_DATA (key), key_size);
|
0
|
3992 memset (raw_key + key_size, 0, (CRYPT_KEY_SIZE + 1) - key_size);
|
|
3993
|
183
|
3994 ecb_crypt (raw_key, encrypted_string, rounded_size,
|
|
3995 DES_ENCRYPT | DES_SW);
|
0
|
3996 return make_string (encrypted_string, rounded_size);
|
|
3997 }
|
|
3998
|
20
|
3999 DEFUN ("decrypt-string", Fdecrypt_string, 2, 2, 0, /*
|
0
|
4000 Decrypt STRING using KEY.
|
20
|
4001 */
|
|
4002 (string, key))
|
0
|
4003 {
|
|
4004 char *decrypted_string, *raw_key;
|
|
4005 int string_size, key_size;
|
|
4006
|
|
4007 CHECK_STRING (string);
|
|
4008 CHECK_STRING (key);
|
|
4009
|
16
|
4010 string_size = XSTRING_LENGTH (string) + 1;
|
0
|
4011 decrypted_string = alloca (string_size);
|
16
|
4012 memcpy (decrypted_string, XSTRING_DATA (string), string_size);
|
0
|
4013 decrypted_string[string_size - 1] = '\0';
|
|
4014
|
183
|
4015 key_size = min (CRYPT_KEY_SIZE, XSTRING_LENGTH (key))
|
0
|
4016
|
|
4017 raw_key = alloca (CRYPT_KEY_SIZE + 1);
|
16
|
4018 memcpy (raw_key, XSTRING_DATA (key), key_size);
|
0
|
4019 memset (raw_key + key_size, 0, (CRYPT_KEY_SIZE + 1) - key_size);
|
|
4020
|
|
4021
|
183
|
4022 ecb_crypt (raw_key, decrypted_string, string_size, D | DES_SW);
|
0
|
4023 return make_string (decrypted_string, string_size - 1);
|
|
4024 }
|
183
|
4025 #endif /* 0 */
|
0
|
4026
|
|
4027
|
20
|
4028 DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime, 1, 1, 0, /*
|
0
|
4029 Return t if last mod time of BUF's visited file matches what BUF records.
|
|
4030 This means that the file has not been changed since it was visited or saved.
|
20
|
4031 */
|
|
4032 (buf))
|
0
|
4033 {
|
116
|
4034 /* This function can call lisp */
|
0
|
4035 struct buffer *b;
|
|
4036 struct stat st;
|
|
4037 Lisp_Object handler;
|
|
4038
|
|
4039 CHECK_BUFFER (buf);
|
|
4040 b = XBUFFER (buf);
|
|
4041
|
|
4042 if (!STRINGP (b->filename)) return Qt;
|
|
4043 if (b->modtime == 0) return Qt;
|
|
4044
|
|
4045 /* If the file name has special constructs in it,
|
|
4046 call the corresponding file handler. */
|
|
4047 handler = Ffind_file_name_handler (b->filename,
|
|
4048 Qverify_visited_file_modtime);
|
|
4049 if (!NILP (handler))
|
|
4050 return call2 (handler, Qverify_visited_file_modtime, buf);
|
|
4051
|
16
|
4052 if (stat ((char *) XSTRING_DATA (b->filename), &st) < 0)
|
0
|
4053 {
|
|
4054 /* If the file doesn't exist now and didn't exist before,
|
|
4055 we say that it isn't modified, provided the error is a tame one. */
|
|
4056 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
|
|
4057 st.st_mtime = -1;
|
|
4058 else
|
|
4059 st.st_mtime = 0;
|
|
4060 }
|
|
4061 if (st.st_mtime == b->modtime
|
|
4062 /* If both are positive, accept them if they are off by one second. */
|
|
4063 || (st.st_mtime > 0 && b->modtime > 0
|
|
4064 && (st.st_mtime == b->modtime + 1
|
|
4065 || st.st_mtime == b->modtime - 1)))
|
|
4066 return Qt;
|
|
4067 return Qnil;
|
|
4068 }
|
|
4069
|
20
|
4070 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, 0, 0, 0, /*
|
0
|
4071 Clear out records of last mod time of visited file.
|
|
4072 Next attempt to save will certainly not complain of a discrepancy.
|
20
|
4073 */
|
|
4074 ())
|
0
|
4075 {
|
|
4076 current_buffer->modtime = 0;
|
|
4077 return Qnil;
|
|
4078 }
|
|
4079
|
20
|
4080 DEFUN ("visited-file-modtime", Fvisited_file_modtime, 0, 0, 0, /*
|
0
|
4081 Return the current buffer's recorded visited file modification time.
|
|
4082 The value is a list of the form (HIGH . LOW), like the time values
|
|
4083 that `file-attributes' returns.
|
20
|
4084 */
|
|
4085 ())
|
0
|
4086 {
|
|
4087 return time_to_lisp ((time_t) current_buffer->modtime);
|
|
4088 }
|
|
4089
|
20
|
4090 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, 0, 1, 0, /*
|
0
|
4091 Update buffer's recorded modification time from the visited file's time.
|
|
4092 Useful if the buffer was not read from the file normally
|
|
4093 or if the file itself has been changed for some known benign reason.
|
|
4094 An argument specifies the modification time value to use
|
|
4095 (instead of that of the visited file), in the form of a list
|
|
4096 (HIGH . LOW) or (HIGH LOW).
|
20
|
4097 */
|
|
4098 (time_list))
|
0
|
4099 {
|
116
|
4100 /* This function can call lisp */
|
0
|
4101 if (!NILP (time_list))
|
|
4102 {
|
|
4103 time_t the_time;
|
|
4104 lisp_to_time (time_list, &the_time);
|
|
4105 current_buffer->modtime = (int) the_time;
|
|
4106 }
|
|
4107 else
|
|
4108 {
|
|
4109 Lisp_Object filename;
|
|
4110 struct stat st;
|
|
4111 Lisp_Object handler;
|
114
|
4112 struct gcpro gcpro1, gcpro2, gcpro3;
|
151
|
4113
|
114
|
4114 GCPRO3 (filename, time_list, current_buffer->filename);
|
|
4115 filename = Fexpand_file_name (current_buffer->filename, Qnil);
|
0
|
4116
|
|
4117 /* If the file name has special constructs in it,
|
|
4118 call the corresponding file handler. */
|
|
4119 handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime);
|
|
4120 UNGCPRO;
|
|
4121 if (!NILP (handler))
|
|
4122 /* The handler can find the file name the same way we did. */
|
|
4123 return call2 (handler, Qset_visited_file_modtime, Qnil);
|
16
|
4124 else if (stat ((char *) XSTRING_DATA (filename), &st) >= 0)
|
0
|
4125 current_buffer->modtime = st.st_mtime;
|
|
4126 }
|
|
4127
|
|
4128 return Qnil;
|
|
4129 }
|
|
4130
|
|
4131 static Lisp_Object
|
|
4132 auto_save_error (Lisp_Object condition_object, Lisp_Object ignored)
|
|
4133 {
|
116
|
4134 /* This function can call lisp */
|
0
|
4135 if (gc_in_progress)
|
|
4136 return Qnil;
|
110
|
4137 /* Don't try printing an error message after everything is gone! */
|
|
4138 if (preparing_for_armageddon)
|
|
4139 return Qnil;
|
0
|
4140 clear_echo_area (selected_frame (), Qauto_saving, 1);
|
|
4141 Fding (Qt, Qauto_save_error, Qnil);
|
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));
|
82
|
4146 message ("Auto-saving...error for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4147 Fsleep_for (make_int (1));
|
|
4148 return Qnil;
|
|
4149 }
|
|
4150
|
|
4151 static Lisp_Object
|
|
4152 auto_save_1 (Lisp_Object ignored)
|
|
4153 {
|
116
|
4154 /* This function can call lisp */
|
|
4155 /* #### I think caller is protecting current_buffer? */
|
0
|
4156 struct stat st;
|
|
4157 Lisp_Object fn = current_buffer->filename;
|
|
4158 Lisp_Object a = current_buffer->auto_save_file_name;
|
|
4159
|
|
4160 if (!STRINGP (a))
|
|
4161 return (Qnil);
|
|
4162
|
|
4163 /* Get visited file's mode to become the auto save file's mode. */
|
|
4164 if (STRINGP (fn) &&
|
16
|
4165 stat ((char *) XSTRING_DATA (fn), &st) >= 0)
|
0
|
4166 /* But make sure we can overwrite it later! */
|
|
4167 auto_save_mode_bits = st.st_mode | 0600;
|
|
4168 else
|
|
4169 /* default mode for auto-save files of buffers with no file is
|
|
4170 readable by owner only. This may annoy some small number of
|
|
4171 people, but the alternative removes all privacy from email. */
|
|
4172 auto_save_mode_bits = 0600;
|
|
4173
|
|
4174 return
|
70
|
4175 /* !!#### need to deal with this 'escape-quoted everywhere */
|
|
4176 Fwrite_region_internal (Qnil, Qnil, a, Qnil, Qlambda, Qnil,
|
|
4177 #ifdef MULE
|
|
4178 Qescape_quoted
|
|
4179 #else
|
|
4180 Qnil
|
|
4181 #endif
|
|
4182 );
|
44
|
4183 }
|
|
4184
|
116
|
4185 static Lisp_Object
|
|
4186 auto_save_expand_name_error (Lisp_Object condition_object, Lisp_Object ignored)
|
|
4187 {
|
|
4188 /* #### this function should spew an error message about not being
|
|
4189 able to open the .saves file. */
|
|
4190 return Qnil;
|
|
4191 }
|
|
4192
|
|
4193 static Lisp_Object
|
|
4194 auto_save_expand_name (Lisp_Object name)
|
|
4195 {
|
|
4196 struct gcpro gcpro1;
|
|
4197
|
|
4198 /* note that caller did NOT gc protect name, so we do it. */
|
|
4199 /* #### dmoore - this might not be neccessary, if condition_case_1
|
|
4200 protects it. but I don't think it does. */
|
|
4201 GCPRO1 (name);
|
|
4202 RETURN_UNGCPRO (Fexpand_file_name (name, Qnil));
|
|
4203 }
|
|
4204
|
0
|
4205
|
|
4206 static Lisp_Object
|
|
4207 do_auto_save_unwind (Lisp_Object fd)
|
|
4208 {
|
|
4209 close (XINT (fd));
|
|
4210 return (fd);
|
|
4211 }
|
|
4212
|
|
4213 static Lisp_Object
|
|
4214 do_auto_save_unwind_2 (Lisp_Object old_auto_saving)
|
|
4215 {
|
|
4216 auto_saving = XINT (old_auto_saving);
|
|
4217 return Qnil;
|
|
4218 }
|
|
4219
|
|
4220 /* Fdo_auto_save() checks whether a GC is in progress when it is called,
|
|
4221 and if so, tries to avoid touching lisp objects.
|
|
4222
|
|
4223 The only time that Fdo_auto_save() is called while GC is in progress
|
|
4224 is if we're going down, as a result of an abort() or a kill signal.
|
|
4225 It's fairly important that we generate autosave files in that case!
|
|
4226 */
|
|
4227
|
20
|
4228 DEFUN ("do-auto-save", Fdo_auto_save, 0, 2, "", /*
|
0
|
4229 Auto-save all buffers that need it.
|
|
4230 This is all buffers that have auto-saving enabled
|
|
4231 and are changed since last auto-saved.
|
|
4232 Auto-saving writes the buffer into a file
|
|
4233 so that your editing is not lost if the system crashes.
|
|
4234 This file is not the file you visited; that changes only when you save.
|
|
4235 Normally we run the normal hook `auto-save-hook' before saving.
|
|
4236
|
|
4237 Non-nil first argument means do not print any message if successful.
|
|
4238 Non-nil second argument means save only current buffer.
|
20
|
4239 */
|
|
4240 (no_message, current_only))
|
0
|
4241 {
|
116
|
4242 /* This function can call lisp */
|
110
|
4243 struct buffer *b;
|
0
|
4244 Lisp_Object tail, buf;
|
|
4245 int auto_saved = 0;
|
|
4246 int do_handled_files;
|
|
4247 Lisp_Object oquit = Qnil;
|
|
4248 Lisp_Object listfile = Qnil;
|
110
|
4249 Lisp_Object old;
|
0
|
4250 int listdesc = -1;
|
|
4251 int speccount = specpdl_depth ();
|
110
|
4252 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
4253
|
|
4254 XSETBUFFER (old, current_buffer);
|
|
4255 GCPRO3 (oquit, listfile, old);
|
0
|
4256 check_quit (); /* make Vquit_flag accurate */
|
|
4257 /* Ordinarily don't quit within this function,
|
|
4258 but don't make it impossible to quit (in case we get hung in I/O). */
|
|
4259 oquit = Vquit_flag;
|
|
4260 Vquit_flag = Qnil;
|
|
4261
|
|
4262 /* No further GCPRO needed, because (when it matters) all Lisp_Object
|
|
4263 variables point to non-strings reached from Vbuffer_alist. */
|
|
4264
|
|
4265 if (minibuf_level != 0 || preparing_for_armageddon)
|
|
4266 no_message = Qt;
|
|
4267
|
|
4268 run_hook (Qauto_save_hook);
|
|
4269
|
|
4270 if (GC_STRINGP (Vauto_save_list_file_name))
|
116
|
4271 listfile = condition_case_1 (Qt,
|
|
4272 auto_save_expand_name,
|
|
4273 Vauto_save_list_file_name,
|
|
4274 auto_save_expand_name_error, Qnil);
|
0
|
4275
|
|
4276 /* Make sure auto_saving is reset. */
|
|
4277 record_unwind_protect (do_auto_save_unwind_2, make_int (auto_saving));
|
|
4278
|
|
4279 auto_saving = 1;
|
|
4280
|
|
4281 /* First, save all files which don't have handlers. If Emacs is
|
|
4282 crashing, the handlers may tweak what is causing Emacs to crash
|
|
4283 in the first place, and it would be a shame if Emacs failed to
|
|
4284 autosave perfectly ordinary files because it couldn't handle some
|
|
4285 ange-ftp'd file. */
|
|
4286 for (do_handled_files = 0; do_handled_files < 2; do_handled_files++)
|
|
4287 {
|
|
4288 for (tail = Vbuffer_alist;
|
|
4289 GC_CONSP (tail);
|
|
4290 tail = XCDR (tail))
|
|
4291 {
|
|
4292 buf = XCDR (XCAR (tail));
|
|
4293 b = XBUFFER (buf);
|
|
4294
|
|
4295 if (!GC_NILP (current_only)
|
|
4296 && b != current_buffer)
|
|
4297 continue;
|
151
|
4298
|
0
|
4299 /* Don't auto-save indirect buffers.
|
|
4300 The base buffer takes care of it. */
|
|
4301 if (b->base_buffer)
|
|
4302 continue;
|
|
4303
|
|
4304 /* Check for auto save enabled
|
|
4305 and file changed since last auto save
|
|
4306 and file changed since last real save. */
|
|
4307 if (GC_STRINGP (b->auto_save_file_name)
|
|
4308 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
|
|
4309 && b->auto_save_modified < BUF_MODIFF (b)
|
|
4310 /* -1 means we've turned off autosaving for a while--see below. */
|
|
4311 && XINT (b->save_length) >= 0
|
|
4312 && (do_handled_files
|
|
4313 || NILP (Ffind_file_name_handler (b->auto_save_file_name,
|
|
4314 Qwrite_region))))
|
|
4315 {
|
|
4316 EMACS_TIME before_time, after_time;
|
|
4317
|
|
4318 EMACS_GET_TIME (before_time);
|
|
4319 /* If we had a failure, don't try again for 20 minutes. */
|
|
4320 if (!preparing_for_armageddon
|
|
4321 && b->auto_save_failure_time >= 0
|
|
4322 && (EMACS_SECS (before_time) - b->auto_save_failure_time <
|
|
4323 1200))
|
|
4324 continue;
|
|
4325
|
|
4326 if (!preparing_for_armageddon &&
|
|
4327 (XINT (b->save_length) * 10
|
|
4328 > (BUF_Z (b) - BUF_BEG (b)) * 13)
|
|
4329 /* A short file is likely to change a large fraction;
|
|
4330 spare the user annoying messages. */
|
|
4331 && XINT (b->save_length) > 5000
|
|
4332 /* These messages are frequent and annoying for `*mail*'. */
|
|
4333 && !EQ (b->filename, Qnil)
|
|
4334 && NILP (no_message)
|
|
4335 && disable_auto_save_when_buffer_shrinks)
|
|
4336 {
|
|
4337 /* It has shrunk too much; turn off auto-saving here.
|
|
4338 Unless we're about to crash, in which case auto-save it
|
|
4339 anyway.
|
|
4340 */
|
|
4341 message
|
|
4342 ("Buffer %s has shrunk a lot; auto save turned off there",
|
16
|
4343 XSTRING_DATA (b->name));
|
0
|
4344 /* Turn off auto-saving until there's a real save,
|
|
4345 and prevent any more warnings. */
|
|
4346 b->save_length = make_int (-1);
|
|
4347 if (!gc_in_progress)
|
|
4348 Fsleep_for (make_int (1));
|
|
4349 continue;
|
|
4350 }
|
|
4351 set_buffer_internal (b);
|
|
4352 if (!auto_saved && GC_NILP (no_message))
|
|
4353 {
|
|
4354 static CONST unsigned char *msg
|
|
4355 = (CONST unsigned char *) "Auto-saving...";
|
|
4356 echo_area_message (selected_frame (), msg, Qnil,
|
|
4357 0, strlen ((CONST char *) msg),
|
|
4358 Qauto_saving);
|
|
4359 }
|
|
4360
|
|
4361 /* Open the auto-save list file, if necessary.
|
|
4362 We only do this now so that the file only exists
|
|
4363 if we actually auto-saved any files. */
|
|
4364 if (!auto_saved && GC_STRINGP (listfile) && listdesc < 0)
|
|
4365 {
|
|
4366 #ifdef DOS_NT
|
70
|
4367 listdesc = open ((char *) XSTRING_DATA (listfile),
|
167
|
4368 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
|
0
|
4369 S_IREAD | S_IWRITE);
|
|
4370 #else /* not DOS_NT */
|
16
|
4371 listdesc = creat ((char *) XSTRING_DATA (listfile), 0666);
|
0
|
4372 #endif /* not DOS_NT */
|
|
4373
|
|
4374 /* Arrange to close that file whether or not we get
|
|
4375 an error. */
|
|
4376 if (listdesc >= 0)
|
|
4377 record_unwind_protect (do_auto_save_unwind,
|
|
4378 make_int (listdesc));
|
|
4379 }
|
|
4380
|
|
4381 /* Record all the buffers that we are auto-saving in
|
|
4382 the special file that lists them. For each of
|
|
4383 these buffers, record visited name (if any) and
|
|
4384 auto save name. */
|
|
4385 if (listdesc >= 0)
|
|
4386 {
|
|
4387 Extbyte *auto_save_file_name_ext;
|
|
4388 Extcount auto_save_file_name_ext_len;
|
151
|
4389
|
0
|
4390 GET_STRING_FILENAME_DATA_ALLOCA
|
|
4391 (b->auto_save_file_name,
|
|
4392 auto_save_file_name_ext,
|
|
4393 auto_save_file_name_ext_len);
|
|
4394 if (!NILP (b->filename))
|
|
4395 {
|
|
4396 Extbyte *filename_ext;
|
|
4397 Extcount filename_ext_len;
|
151
|
4398
|
0
|
4399 GET_STRING_FILENAME_DATA_ALLOCA (b->filename,
|
|
4400 filename_ext,
|
|
4401 filename_ext_len);
|
|
4402 write (listdesc, filename_ext, filename_ext_len);
|
|
4403 }
|
|
4404 write (listdesc, "\n", 1);
|
|
4405 write (listdesc, auto_save_file_name_ext,
|
|
4406 auto_save_file_name_ext_len);
|
|
4407 write (listdesc, "\n", 1);
|
|
4408 }
|
|
4409
|
110
|
4410 /* dmoore - In a bad scenario we've set b=XBUFFER(buf)
|
|
4411 based on values in Vbuffer_alist. auto_save_1 may
|
|
4412 cause lisp handlers to run. Those handlers may kill
|
|
4413 the buffer and then GC. Since the buffer is killed,
|
|
4414 it's no longer in Vbuffer_alist so it might get reaped
|
|
4415 by the GC. We also need to protect tail. */
|
|
4416 /* #### There is probably a lot of other code which has
|
|
4417 pointers into buffers which may get blown away by
|
|
4418 handlers. */
|
|
4419 {
|
169
|
4420 struct gcpro ngcpro1, ngcpro2;
|
|
4421 NGCPRO2 (buf, tail);
|
110
|
4422 condition_case_1 (Qt,
|
|
4423 auto_save_1, Qnil,
|
|
4424 auto_save_error, Qnil);
|
169
|
4425 NUNGCPRO;
|
110
|
4426 }
|
|
4427 /* Handler killed our saved current-buffer! Pick any. */
|
|
4428 if (!BUFFER_LIVE_P (XBUFFER (old)))
|
|
4429 XSETBUFFER (old, current_buffer);
|
|
4430
|
|
4431 set_buffer_internal (XBUFFER (old));
|
0
|
4432 auto_saved++;
|
151
|
4433
|
110
|
4434 /* Handler killed their own buffer! */
|
|
4435 if (!BUFFER_LIVE_P(b))
|
|
4436 continue;
|
|
4437
|
0
|
4438 b->auto_save_modified = BUF_MODIFF (b);
|
|
4439 b->save_length = make_int (BUF_SIZE (b));
|
|
4440 EMACS_GET_TIME (after_time);
|
|
4441 /* If auto-save took more than 60 seconds,
|
|
4442 assume it was an NFS failure that got a timeout. */
|
|
4443 if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60)
|
|
4444 b->auto_save_failure_time = EMACS_SECS (after_time);
|
|
4445 }
|
|
4446 }
|
|
4447 }
|
|
4448
|
|
4449 /* Prevent another auto save till enough input events come in. */
|
|
4450 if (auto_saved)
|
|
4451 record_auto_save ();
|
|
4452
|
|
4453 /* If we didn't save anything into the listfile, remove the old
|
|
4454 one because nothing needed to be auto-saved. Do this afterwards
|
|
4455 rather than before in case we get a crash attempting to autosave
|
|
4456 (in that case we'd still want the old one around). */
|
|
4457 if (listdesc < 0 && !auto_saved && GC_STRINGP (listfile))
|
16
|
4458 unlink ((char *) XSTRING_DATA (listfile));
|
0
|
4459
|
|
4460 /* Show "...done" only if the echo area would otherwise be empty. */
|
|
4461 if (auto_saved && NILP (no_message)
|
|
4462 && NILP (clear_echo_area (selected_frame (), Qauto_saving, 0)))
|
|
4463 {
|
|
4464 static CONST unsigned char *msg
|
|
4465 = (CONST unsigned char *)"Auto-saving...done";
|
|
4466 echo_area_message (selected_frame (), msg, Qnil, 0,
|
|
4467 strlen ((CONST char *) msg), Qauto_saving);
|
|
4468 }
|
|
4469
|
|
4470 Vquit_flag = oquit;
|
|
4471
|
|
4472 RETURN_UNGCPRO (unbind_to (speccount, Qnil));
|
|
4473 }
|
|
4474
|
20
|
4475 DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved, 0, 0, 0, /*
|
0
|
4476 Mark current buffer as auto-saved with its current text.
|
|
4477 No auto-save file will be written until the buffer changes again.
|
20
|
4478 */
|
|
4479 ())
|
0
|
4480 {
|
|
4481 current_buffer->auto_save_modified = BUF_MODIFF (current_buffer);
|
|
4482 current_buffer->save_length = make_int (BUF_SIZE (current_buffer));
|
|
4483 current_buffer->auto_save_failure_time = -1;
|
|
4484 return Qnil;
|
|
4485 }
|
|
4486
|
20
|
4487 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, 0, 0, 0, /*
|
0
|
4488 Clear any record of a recent auto-save failure in the current buffer.
|
20
|
4489 */
|
|
4490 ())
|
0
|
4491 {
|
|
4492 current_buffer->auto_save_failure_time = -1;
|
|
4493 return Qnil;
|
|
4494 }
|
|
4495
|
20
|
4496 DEFUN ("recent-auto-save-p", Frecent_auto_save_p, 0, 0, 0, /*
|
0
|
4497 Return t if buffer has been auto-saved since last read in or saved.
|
20
|
4498 */
|
|
4499 ())
|
0
|
4500 {
|
|
4501 return (BUF_SAVE_MODIFF (current_buffer) <
|
|
4502 current_buffer->auto_save_modified) ? Qt : Qnil;
|
|
4503 }
|
|
4504
|
|
4505
|
|
4506 /************************************************************************/
|
|
4507 /* initialization */
|
|
4508 /************************************************************************/
|
|
4509
|
|
4510 void
|
|
4511 syms_of_fileio (void)
|
|
4512 {
|
|
4513 defsymbol (&Qexpand_file_name, "expand-file-name");
|
|
4514 defsymbol (&Qfile_truename, "file-truename");
|
|
4515 defsymbol (&Qsubstitute_in_file_name, "substitute-in-file-name");
|
|
4516 defsymbol (&Qdirectory_file_name, "directory-file-name");
|
|
4517 defsymbol (&Qfile_name_directory, "file-name-directory");
|
|
4518 defsymbol (&Qfile_name_nondirectory, "file-name-nondirectory");
|
|
4519 defsymbol (&Qunhandled_file_name_directory, "unhandled-file-name-directory");
|
|
4520 defsymbol (&Qfile_name_as_directory, "file-name-as-directory");
|
|
4521 defsymbol (&Qcopy_file, "copy-file");
|
|
4522 defsymbol (&Qmake_directory_internal, "make-directory-internal");
|
|
4523 defsymbol (&Qdelete_directory, "delete-directory");
|
|
4524 defsymbol (&Qdelete_file, "delete-file");
|
|
4525 defsymbol (&Qrename_file, "rename-file");
|
|
4526 defsymbol (&Qadd_name_to_file, "add-name-to-file");
|
|
4527 defsymbol (&Qmake_symbolic_link, "make-symbolic-link");
|
|
4528 defsymbol (&Qfile_exists_p, "file-exists-p");
|
|
4529 defsymbol (&Qfile_executable_p, "file-executable-p");
|
|
4530 defsymbol (&Qfile_readable_p, "file-readable-p");
|
|
4531 defsymbol (&Qfile_symlink_p, "file-symlink-p");
|
|
4532 defsymbol (&Qfile_writable_p, "file-writable-p");
|
|
4533 defsymbol (&Qfile_directory_p, "file-directory-p");
|
|
4534 defsymbol (&Qfile_regular_p, "file-regular-p");
|
|
4535 defsymbol (&Qfile_accessible_directory_p, "file-accessible-directory-p");
|
|
4536 defsymbol (&Qfile_modes, "file-modes");
|
|
4537 defsymbol (&Qset_file_modes, "set-file-modes");
|
|
4538 defsymbol (&Qfile_newer_than_file_p, "file-newer-than-file-p");
|
|
4539 defsymbol (&Qinsert_file_contents, "insert-file-contents");
|
|
4540 defsymbol (&Qwrite_region, "write-region");
|
|
4541 defsymbol (&Qverify_visited_file_modtime, "verify-visited-file-modtime");
|
|
4542 defsymbol (&Qset_visited_file_modtime, "set-visited-file-modtime");
|
|
4543 #ifdef DOS_NT
|
|
4544 defsymbol (&Qfind_buffer_file_type, "find-buffer-file-type");
|
|
4545 #endif /* DOS_NT */
|
|
4546 defsymbol (&Qcar_less_than_car, "car-less-than-car"); /* Vomitous! */
|
|
4547
|
|
4548 defsymbol (&Qfile_name_handler_alist, "file-name-handler-alist");
|
|
4549 defsymbol (&Qauto_save_hook, "auto-save-hook");
|
|
4550 defsymbol (&Qauto_save_error, "auto-save-error");
|
|
4551 defsymbol (&Qauto_saving, "auto-saving");
|
|
4552
|
|
4553 defsymbol (&Qformat_decode, "format-decode");
|
|
4554 defsymbol (&Qformat_annotate_function, "format-annotate-function");
|
|
4555
|
|
4556 defsymbol (&Qcompute_buffer_file_truename, "compute-buffer-file-truename");
|
|
4557 deferror (&Qfile_error, "file-error", "File error", Qio_error);
|
|
4558 deferror (&Qfile_already_exists, "file-already-exists",
|
|
4559 "File already exists", Qfile_error);
|
|
4560
|
20
|
4561 DEFSUBR (Ffind_file_name_handler);
|
|
4562
|
|
4563 DEFSUBR (Ffile_name_directory);
|
|
4564 DEFSUBR (Ffile_name_nondirectory);
|
|
4565 DEFSUBR (Funhandled_file_name_directory);
|
|
4566 DEFSUBR (Ffile_name_as_directory);
|
|
4567 DEFSUBR (Fdirectory_file_name);
|
|
4568 DEFSUBR (Fmake_temp_name);
|
|
4569 DEFSUBR (Fexpand_file_name);
|
|
4570 DEFSUBR (Ffile_truename);
|
|
4571 DEFSUBR (Fsubstitute_in_file_name);
|
|
4572 DEFSUBR (Fcopy_file);
|
|
4573 DEFSUBR (Fmake_directory_internal);
|
|
4574 DEFSUBR (Fdelete_directory);
|
|
4575 DEFSUBR (Fdelete_file);
|
|
4576 DEFSUBR (Frename_file);
|
|
4577 DEFSUBR (Fadd_name_to_file);
|
0
|
4578 #ifdef S_IFLNK
|
20
|
4579 DEFSUBR (Fmake_symbolic_link);
|
0
|
4580 #endif /* S_IFLNK */
|
|
4581 #ifdef HPUX_NET
|
20
|
4582 DEFSUBR (Fsysnetunam);
|
0
|
4583 #endif /* HPUX_NET */
|
20
|
4584 DEFSUBR (Ffile_name_absolute_p);
|
|
4585 DEFSUBR (Ffile_exists_p);
|
|
4586 DEFSUBR (Ffile_executable_p);
|
|
4587 DEFSUBR (Ffile_readable_p);
|
|
4588 DEFSUBR (Ffile_writable_p);
|
|
4589 DEFSUBR (Ffile_symlink_p);
|
|
4590 DEFSUBR (Ffile_directory_p);
|
|
4591 DEFSUBR (Ffile_accessible_directory_p);
|
|
4592 DEFSUBR (Ffile_regular_p);
|
|
4593 DEFSUBR (Ffile_modes);
|
|
4594 DEFSUBR (Fset_file_modes);
|
|
4595 DEFSUBR (Fset_default_file_modes);
|
|
4596 DEFSUBR (Fdefault_file_modes);
|
|
4597 DEFSUBR (Funix_sync);
|
|
4598 DEFSUBR (Ffile_newer_than_file_p);
|
|
4599 DEFSUBR (Finsert_file_contents_internal);
|
|
4600 DEFSUBR (Fwrite_region_internal);
|
|
4601 DEFSUBR (Fcar_less_than_car); /* Vomitous! */
|
|
4602 DEFSUBR (Fcdr_less_than_cdr); /* Yeah oh yeah bucko .... */
|
0
|
4603 #if 0
|
20
|
4604 DEFSUBR (Fencrypt_string);
|
|
4605 DEFSUBR (Fdecrypt_string);
|
0
|
4606 #endif
|
20
|
4607 DEFSUBR (Fverify_visited_file_modtime);
|
|
4608 DEFSUBR (Fclear_visited_file_modtime);
|
|
4609 DEFSUBR (Fvisited_file_modtime);
|
|
4610 DEFSUBR (Fset_visited_file_modtime);
|
|
4611
|
|
4612 DEFSUBR (Fdo_auto_save);
|
|
4613 DEFSUBR (Fset_buffer_auto_saved);
|
|
4614 DEFSUBR (Fclear_buffer_auto_save_failure);
|
|
4615 DEFSUBR (Frecent_auto_save_p);
|
0
|
4616 }
|
|
4617
|
|
4618 void
|
|
4619 vars_of_fileio (void)
|
|
4620 {
|
|
4621 DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format /*
|
|
4622 *Format in which to write auto-save files.
|
|
4623 Should be a list of symbols naming formats that are defined in `format-alist'.
|
|
4624 If it is t, which is the default, auto-save files are written in the
|
|
4625 same format as a regular save would use.
|
|
4626 */ );
|
|
4627 Vauto_save_file_format = Qt;
|
|
4628
|
|
4629 DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist /*
|
|
4630 *Alist of elements (REGEXP . HANDLER) for file names handled specially.
|
|
4631 If a file name matches REGEXP, then all I/O on that file is done by calling
|
|
4632 HANDLER.
|
|
4633
|
|
4634 The first argument given to HANDLER is the name of the I/O primitive
|
|
4635 to be handled; the remaining arguments are the arguments that were
|
|
4636 passed to that primitive. For example, if you do
|
|
4637 (file-exists-p FILENAME)
|
|
4638 and FILENAME is handled by HANDLER, then HANDLER is called like this:
|
|
4639 (funcall HANDLER 'file-exists-p FILENAME)
|
|
4640 The function `find-file-name-handler' checks this list for a handler
|
|
4641 for its argument.
|
|
4642 */ );
|
|
4643 Vfile_name_handler_alist = Qnil;
|
|
4644
|
|
4645 DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions /*
|
|
4646 A list of functions to be called at the end of `insert-file-contents'.
|
|
4647 Each is passed one argument, the number of bytes inserted. It should return
|
|
4648 the new byte count, and leave point the same. If `insert-file-contents' is
|
|
4649 intercepted by a handler from `file-name-handler-alist', that handler is
|
|
4650 responsible for calling the after-insert-file-functions if appropriate.
|
|
4651 */ );
|
|
4652 Vafter_insert_file_functions = Qnil;
|
|
4653
|
|
4654 DEFVAR_LISP ("write-region-annotate-functions",
|
|
4655 &Vwrite_region_annotate_functions /*
|
|
4656 A list of functions to be called at the start of `write-region'.
|
183
|
4657 Each is passed two arguments, START and END, as for `write-region'.
|
0
|
4658 It should return a list of pairs (POSITION . STRING) of strings to be
|
|
4659 effectively inserted at the specified positions of the file being written
|
|
4660 \(1 means to insert before the first byte written). The POSITIONs must be
|
|
4661 sorted into increasing order. If there are several functions in the list,
|
|
4662 the several lists are merged destructively.
|
|
4663 */ );
|
|
4664 Vwrite_region_annotate_functions = Qnil;
|
|
4665
|
|
4666 DEFVAR_LISP ("write-region-annotations-so-far",
|
|
4667 &Vwrite_region_annotations_so_far /*
|
|
4668 When an annotation function is called, this holds the previous annotations.
|
|
4669 These are the annotations made by other annotation functions
|
|
4670 that were already called. See also `write-region-annotate-functions'.
|
|
4671 */ );
|
|
4672 Vwrite_region_annotations_so_far = Qnil;
|
|
4673
|
|
4674 DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers /*
|
|
4675 A list of file name handlers that temporarily should not be used.
|
|
4676 This applies only to the operation `inhibit-file-name-operation'.
|
|
4677 */ );
|
|
4678 Vinhibit_file_name_handlers = Qnil;
|
|
4679
|
|
4680 DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation /*
|
|
4681 The operation for which `inhibit-file-name-handlers' is applicable.
|
|
4682 */ );
|
|
4683 Vinhibit_file_name_operation = Qnil;
|
|
4684
|
|
4685 DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name /*
|
|
4686 File name in which we write a list of all auto save file names.
|
|
4687 */ );
|
|
4688 Vauto_save_list_file_name = Qnil;
|
|
4689
|
|
4690 DEFVAR_BOOL ("disable-auto-save-when-buffer-shrinks",
|
|
4691 &disable_auto_save_when_buffer_shrinks /*
|
|
4692 If non-nil, auto-saving is disabled when a buffer shrinks too much.
|
|
4693 This is to prevent you from losing your edits if you accidentally
|
|
4694 delete a large chunk of the buffer and don't notice it until too late.
|
|
4695 Saving the buffer normally turns auto-save back on.
|
|
4696 */ );
|
|
4697 disable_auto_save_when_buffer_shrinks = 1;
|
211
|
4698 #ifdef DOS_NT
|
|
4699 DEFVAR_LISP ("directory-sep-char", &Vdirectory_sep_char /*
|
|
4700 *Directory separator character for built-in functions that return file names.
|
|
4701 The value should be either ?/ or ?\\ (any other value is treated as ?\\).
|
|
4702 This variable affects the built-in functions only on Windows,
|
|
4703 on other platforms, it is initialized so that Lisp code can find out
|
|
4704 what the normal separator is.
|
|
4705 */ );
|
|
4706 Vdirectory_sep_char = '/';
|
|
4707
|
|
4708 DEFVAR_LISP ("insert-file-contents-allow-replace", &Vinsert_file_contents_allow_replace /*
|
|
4709 *Allow REPLACE option of insert-file-contents to preserve markers.
|
|
4710 If non-nil, the REPLACE option works as described, preserving markers.
|
|
4711 If nil, the REPLACE option is implemented by deleting the visible region
|
|
4712 then inserting the file contents as if REPLACE was nil.
|
|
4713
|
|
4714 This option is only meaningful on Windows.
|
|
4715 */ );
|
|
4716 Vinsert_file_contents_allow_replace = Qt;
|
|
4717 #endif
|
0
|
4718 }
|