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