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