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