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