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