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