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