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