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
|
16
|
424 beg = XSTRING_DATA (file);
|
|
425 p = beg + XSTRING_LENGTH (file);
|
0
|
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
|
16
|
491 beg = XSTRING_DATA (file);
|
|
492 end = p = beg + XSTRING_LENGTH (file);
|
0
|
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
|
16
|
631 buf = (char *) alloca (XSTRING_LENGTH (file) + 10);
|
0
|
632 return build_string (file_name_as_directory
|
16
|
633 (buf, (char *) XSTRING_DATA (file)));
|
0
|
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 */
|
16
|
827 buf = (char *) alloca (XSTRING_LENGTH (directory) + 20 + 255);
|
0
|
828 #else
|
16
|
829 buf = (char *) alloca (XSTRING_LENGTH (directory) + 20);
|
0
|
830 #endif
|
16
|
831 directory_file_name ((char *) XSTRING_DATA (directory), buf);
|
0
|
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);
|
16
|
849 len = XSTRING_LENGTH (prefix);
|
0
|
850 val = make_uninit_string (len + countof (suffix) - 1);
|
16
|
851 data = XSTRING_DATA (val);
|
|
852 memcpy (data, XSTRING_DATA (prefix), len);
|
0
|
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. */
|
16
|
943 && ! (XSTRING_LENGTH (defalt) >= 3
|
|
944 && IS_DIRECTORY_SEP (XSTRING_BYTE (defalt, 0))
|
|
945 && IS_DEVICE_SEP (XSTRING_BYTE (defalt, 1))))
|
0
|
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
|
16
|
962 nm = XSTRING_DATA (name);
|
0
|
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
|
16
|
1128 if (nm == XSTRING_DATA (name))
|
0
|
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 {
|
16
|
1207 newdir = XSTRING_DATA (defalt);
|
0
|
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;
|
16
|
1429 int elen = XSTRING_LENGTH (expanded_name);
|
0
|
1430
|
|
1431 if (elen >= countof (path))
|
|
1432 goto toolong;
|
|
1433
|
16
|
1434 memcpy (path, XSTRING_DATA (expanded_name), elen + 1);
|
0
|
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);
|
16
|
1493 if (elen > 0 && XSTRING_BYTE (expanded_name, elen - 1) == '/'
|
0
|
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
|
16
|
1555 nm = XSTRING_DATA (string);
|
0
|
1556 #ifdef MSDOS
|
|
1557 dostounix_filename (nm = strcpy (alloca (strlen (nm) + 1), nm));
|
16
|
1558 substituted = !strcmp (nm, XSTRING_DATA (string));
|
0
|
1559 #endif
|
16
|
1560 endp = nm + XSTRING_LENGTH (string);
|
0
|
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 */
|
16
|
1654 xnm = (Bufbyte *) alloca (XSTRING_LENGTH (string) + total + 1);
|
0
|
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 =
|
16
|
1756 XSTRING_BYTE (abspath, XSTRING_LENGTH (abspath) - 1);
|
0
|
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! */
|
16
|
1763 if (XSTRING_LENGTH (abspath) > 1
|
|
1764 && IS_DIRECTORY_SEP (XSTRING_BYTE (abspath, XSTRING_LENGTH (abspath) - 1))
|
|
1765 && !IS_DEVICE_SEP (XSTRING_BYTE (abspath, XSTRING_LENGTH (abspath) - 2)))
|
0
|
1766 /* We cannot take shortcuts; they might be wrong for magic file names. */
|
|
1767 abspath = Fdirectory_file_name (abspath);
|
|
1768 #endif
|
|
1769 UNGCPRO;
|
|
1770 return abspath;
|
|
1771 }
|
|
1772
|
|
1773 /* Signal an error if the file ABSNAME already exists.
|
|
1774 If INTERACTIVE is nonzero, ask the user whether to proceed,
|
|
1775 and bypass the error if the user says to go ahead.
|
|
1776 QUERYSTRING is a name for the action that is being considered
|
|
1777 to alter the file.
|
|
1778 *STATPTR is used to store the stat information if the file exists.
|
|
1779 If the file does not exist, STATPTR->st_mode is set to 0. */
|
|
1780
|
|
1781 static void
|
|
1782 barf_or_query_if_file_exists (Lisp_Object absname, CONST char *querystring,
|
|
1783 int interactive, struct stat *statptr)
|
|
1784 {
|
|
1785 struct stat statbuf;
|
|
1786
|
|
1787 /* stat is a good way to tell whether the file exists,
|
|
1788 regardless of what access permissions it has. */
|
16
|
1789 if (stat ((char *) XSTRING_DATA (absname), &statbuf) >= 0)
|
0
|
1790 {
|
|
1791 Lisp_Object tem;
|
|
1792 struct gcpro gcpro1;
|
|
1793
|
|
1794 GCPRO1 (absname);
|
|
1795 if (interactive)
|
|
1796 tem = call1
|
|
1797 (Qyes_or_no_p,
|
|
1798 (emacs_doprnt_string_c
|
|
1799 ((CONST Bufbyte *) GETTEXT ("File %s already exists; %s anyway? "),
|
16
|
1800 Qnil, -1, XSTRING_DATA (absname),
|
0
|
1801 GETTEXT (querystring))));
|
|
1802 else
|
|
1803 tem = Qnil;
|
|
1804 UNGCPRO;
|
|
1805 if (NILP (tem))
|
|
1806 Fsignal (Qfile_already_exists,
|
|
1807 list2 (build_translated_string ("File already exists"),
|
|
1808 absname));
|
|
1809 if (statptr)
|
|
1810 *statptr = statbuf;
|
|
1811 }
|
|
1812 else
|
|
1813 {
|
|
1814 if (statptr)
|
|
1815 statptr->st_mode = 0;
|
|
1816 }
|
|
1817 return;
|
|
1818 }
|
|
1819
|
|
1820 DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4,
|
|
1821 "fCopy file: \nFCopy %s to file: \np\nP" /*
|
|
1822 Copy FILE to NEWNAME. Both args must be strings.
|
|
1823 Signals a `file-already-exists' error if file NEWNAME already exists,
|
|
1824 unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
|
|
1825 A number as third arg means request confirmation if NEWNAME already exists.
|
|
1826 This is what happens in interactive use with M-x.
|
|
1827 Fourth arg KEEP-TIME non-nil means give the new file the same
|
|
1828 last-modified time as the old one. (This works on only some systems.)
|
|
1829 A prefix arg makes KEEP-TIME non-nil.
|
|
1830 */ )
|
16
|
1831 (filename, newname, ok_if_already_exists, keep_time)
|
|
1832 Lisp_Object filename, newname, ok_if_already_exists, keep_time;
|
0
|
1833 {
|
|
1834 /* This function can GC */
|
|
1835 int ifd, ofd, n;
|
|
1836 char buf[16 * 1024];
|
|
1837 struct stat st, out_st;
|
|
1838 Lisp_Object handler;
|
|
1839 int speccount = specpdl_depth ();
|
|
1840 struct gcpro gcpro1, gcpro2;
|
|
1841 /* Lisp_Object args[6]; */
|
|
1842 int input_file_statable_p;
|
|
1843
|
|
1844 GCPRO2 (filename, newname);
|
|
1845 CHECK_STRING (filename);
|
|
1846 CHECK_STRING (newname);
|
|
1847 filename = Fexpand_file_name (filename, Qnil);
|
|
1848 newname = Fexpand_file_name (newname, Qnil);
|
|
1849
|
|
1850 /* If the input file name has special constructs in it,
|
|
1851 call the corresponding file handler. */
|
|
1852 handler = Ffind_file_name_handler (filename, Qcopy_file);
|
|
1853 /* Likewise for output file name. */
|
|
1854 if (NILP (handler))
|
|
1855 handler = Ffind_file_name_handler (newname, Qcopy_file);
|
|
1856 if (!NILP (handler))
|
|
1857 {
|
|
1858 UNGCPRO;
|
|
1859 return call5 (handler, Qcopy_file, filename, newname,
|
16
|
1860 ok_if_already_exists, keep_time);
|
0
|
1861 }
|
|
1862
|
|
1863 /* When second argument is a directory, copy the file into it.
|
|
1864 (copy-file "foo" "bar/") == (copy-file "foo" "bar/foo")
|
|
1865 */
|
|
1866 if (!NILP (Ffile_directory_p (newname)))
|
|
1867 {
|
|
1868 Lisp_Object args[3];
|
|
1869 struct gcpro ngcpro1;
|
|
1870 int i = 1;
|
|
1871
|
|
1872 args[0] = newname;
|
|
1873 args[1] = Qnil; args[2] = Qnil;
|
|
1874 NGCPRO1 (*args);
|
|
1875 ngcpro1.nvars = 3;
|
16
|
1876 if (XSTRING_BYTE (newname, XSTRING_LENGTH (newname) - 1) != '/')
|
0
|
1877 args[i++] = build_string ("/");
|
|
1878 args[i++] = Ffile_name_nondirectory (filename);
|
|
1879 newname = Fconcat (i, args);
|
|
1880 NUNGCPRO;
|
|
1881 }
|
|
1882
|
|
1883 if (NILP (ok_if_already_exists)
|
|
1884 || INTP (ok_if_already_exists))
|
|
1885 barf_or_query_if_file_exists (newname, "copy to it",
|
|
1886 INTP (ok_if_already_exists), &out_st);
|
16
|
1887 else if (stat ((CONST char *) XSTRING_DATA (newname), &out_st) < 0)
|
0
|
1888 out_st.st_mode = 0;
|
|
1889
|
16
|
1890 ifd = open ((char *) XSTRING_DATA (filename), O_RDONLY, 0);
|
0
|
1891 if (ifd < 0)
|
|
1892 report_file_error ("Opening input file", Fcons (filename, Qnil));
|
|
1893
|
|
1894 record_unwind_protect (close_file_unwind, make_int (ifd));
|
|
1895
|
|
1896 /* We can only copy regular files and symbolic links. Other files are not
|
|
1897 copyable by us. */
|
|
1898 input_file_statable_p = (fstat (ifd, &st) >= 0);
|
|
1899
|
|
1900 #ifndef DOS_NT
|
|
1901 if (out_st.st_mode != 0
|
|
1902 && st.st_dev == out_st.st_dev && st.st_ino == out_st.st_ino)
|
|
1903 {
|
|
1904 errno = 0;
|
|
1905 report_file_error ("Input and output files are the same",
|
|
1906 Fcons (filename, Fcons (newname, Qnil)));
|
|
1907 }
|
|
1908 #endif
|
|
1909
|
|
1910 #if defined (S_ISREG) && defined (S_ISLNK)
|
|
1911 if (input_file_statable_p)
|
|
1912 {
|
|
1913 if (!(S_ISREG (st.st_mode))
|
|
1914 /* XEmacs: have to allow S_ISCHR in order to copy /dev/null */
|
|
1915 #ifdef S_ISCHR
|
|
1916 && !(S_ISCHR (st.st_mode))
|
|
1917 #endif
|
|
1918 && !(S_ISLNK (st.st_mode)))
|
|
1919 {
|
|
1920 #if defined (EISDIR)
|
|
1921 /* Get a better looking error message. */
|
|
1922 errno = EISDIR;
|
|
1923 #endif /* EISDIR */
|
|
1924 report_file_error ("Non-regular file", Fcons (filename, Qnil));
|
|
1925 }
|
|
1926 }
|
|
1927 #endif /* S_ISREG && S_ISLNK */
|
|
1928
|
|
1929 #ifdef VMS
|
|
1930 /* Create the copy file with the same record format as the input file */
|
16
|
1931 ofd = sys_creat ((char *) XSTRING_DATA (newname), 0666, ifd);
|
0
|
1932 #else
|
|
1933 #ifdef MSDOS
|
|
1934 /* System's default file type was set to binary by _fmode in emacs.c. */
|
16
|
1935 ofd = creat ((char *) XSTRING_DATA (newname), S_IREAD | S_IWRITE);
|
0
|
1936 #else /* not MSDOS */
|
16
|
1937 ofd = creat ((char *) XSTRING_DATA (newname), 0666);
|
0
|
1938 #endif /* not MSDOS */
|
|
1939 #endif /* VMS */
|
|
1940 if (ofd < 0)
|
|
1941 report_file_error ("Opening output file", list1 (newname));
|
|
1942
|
|
1943 {
|
|
1944 Lisp_Object ofd_locative = noseeum_cons (make_int (ofd), Qnil);
|
|
1945
|
|
1946 record_unwind_protect (close_file_unwind, ofd_locative);
|
|
1947
|
|
1948 while ((n = read_allowing_quit (ifd, buf, sizeof (buf))) > 0)
|
|
1949 {
|
|
1950 if (write_allowing_quit (ofd, buf, n) != n)
|
|
1951 report_file_error ("I/O error", list1 (newname));
|
|
1952 }
|
|
1953
|
|
1954 /* Closing the output clobbers the file times on some systems. */
|
|
1955 if (close (ofd) < 0)
|
|
1956 report_file_error ("I/O error", Fcons (newname, Qnil));
|
|
1957
|
|
1958 if (input_file_statable_p)
|
|
1959 {
|
16
|
1960 if (!NILP (keep_time))
|
0
|
1961 {
|
|
1962 EMACS_TIME atime, mtime;
|
|
1963 EMACS_SET_SECS_USECS (atime, st.st_atime, 0);
|
|
1964 EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
|
16
|
1965 if (set_file_times ((char *) XSTRING_DATA (newname), atime,
|
0
|
1966 mtime))
|
|
1967 report_file_error ("I/O error", Fcons (newname, Qnil));
|
|
1968 }
|
|
1969 #ifndef MSDOS
|
16
|
1970 chmod ((CONST char *) XSTRING_DATA (newname),
|
0
|
1971 st.st_mode & 07777);
|
|
1972 #else /* MSDOS */
|
|
1973 #if defined (__DJGPP__) && __DJGPP__ > 1
|
|
1974 /* In DJGPP v2.0 and later, fstat usually returns true file mode bits,
|
|
1975 and if it can't, it tells so. Otherwise, under MSDOS we usually
|
|
1976 get only the READ bit, which will make the copied file read-only,
|
|
1977 so it's better not to chmod at all. */
|
|
1978 if ((_djstat_flags & _STFAIL_WRITEBIT) == 0)
|
16
|
1979 chmod ((char *) XSTRING_DATA (newname), st.st_mode & 07777);
|
0
|
1980 #endif /* DJGPP version 2 or newer */
|
|
1981 #endif /* MSDOS */
|
|
1982 }
|
|
1983
|
|
1984 /* We'll close it by hand */
|
|
1985 XCAR (ofd_locative) = Qnil;
|
|
1986
|
|
1987 /* Close ifd */
|
|
1988 unbind_to (speccount, Qnil);
|
|
1989 }
|
|
1990
|
|
1991 UNGCPRO;
|
|
1992 return Qnil;
|
|
1993 }
|
|
1994
|
|
1995 DEFUN ("make-directory-internal", Fmake_directory_internal,
|
|
1996 Smake_directory_internal, 1, 1, 0 /*
|
|
1997 Create a directory. One argument, a file name string.
|
|
1998 */ )
|
|
1999 (dirname)
|
|
2000 Lisp_Object dirname;
|
|
2001 {
|
|
2002 /* This function can GC */
|
|
2003 char dir [MAXPATHLEN];
|
|
2004 Lisp_Object handler;
|
|
2005
|
|
2006 struct gcpro gcpro1;
|
|
2007
|
|
2008 GCPRO1 (dirname);
|
|
2009 CHECK_STRING (dirname);
|
|
2010 dirname = Fexpand_file_name (dirname, Qnil);
|
|
2011
|
|
2012 handler = Ffind_file_name_handler (dirname, Qmake_directory_internal);
|
|
2013 UNGCPRO;
|
|
2014 if (!NILP (handler))
|
|
2015 return (call2 (handler, Qmake_directory_internal,
|
|
2016 dirname));
|
|
2017
|
16
|
2018 if (XSTRING_LENGTH (dirname) > (sizeof (dir) - 1))
|
0
|
2019 {
|
|
2020 return Fsignal (Qfile_error,
|
|
2021 list3 (build_translated_string ("Creating directory"),
|
|
2022 build_translated_string ("pathame too long"),
|
|
2023 dirname));
|
|
2024 }
|
16
|
2025 strncpy (dir, (char *) XSTRING_DATA (dirname),
|
|
2026 XSTRING_LENGTH (dirname) + 1);
|
0
|
2027
|
|
2028 #ifndef VMS
|
16
|
2029 if (dir [XSTRING_LENGTH (dirname) - 1] == '/')
|
|
2030 dir [XSTRING_LENGTH (dirname) - 1] = 0;
|
0
|
2031 #endif
|
|
2032
|
|
2033 #ifdef WINDOWSNT
|
|
2034 if (mkdir (dir) != 0)
|
|
2035 #else
|
|
2036 if (mkdir (dir, 0777) != 0)
|
|
2037 #endif
|
|
2038 report_file_error ("Creating directory", list1 (dirname));
|
|
2039
|
|
2040 return Qnil;
|
|
2041 }
|
|
2042
|
|
2043 DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1,
|
|
2044 "FDelete directory: " /*
|
|
2045 Delete a directory. One argument, a file name or directory name string.
|
|
2046 */ )
|
|
2047 (dirname)
|
|
2048 Lisp_Object dirname;
|
|
2049 {
|
|
2050 /* This function can GC */
|
|
2051 Lisp_Object handler;
|
|
2052 struct gcpro gcpro1;
|
|
2053
|
|
2054 GCPRO1 (dirname);
|
|
2055 CHECK_STRING (dirname);
|
|
2056 dirname =
|
|
2057 Fdirectory_file_name (Fexpand_file_name (dirname, Qnil));
|
|
2058
|
|
2059 handler = Ffind_file_name_handler (dirname, Qdelete_directory);
|
|
2060 UNGCPRO;
|
|
2061 if (!NILP (handler))
|
|
2062 return (call2 (handler, Qdelete_directory, dirname));
|
|
2063
|
16
|
2064 if (rmdir ((char *) XSTRING_DATA (dirname)) != 0)
|
0
|
2065 report_file_error ("Removing directory", list1 (dirname));
|
|
2066
|
|
2067 return Qnil;
|
|
2068 }
|
|
2069
|
|
2070 DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: " /*
|
|
2071 Delete specified file. One argument, a file name string.
|
|
2072 If file has multiple names, it continues to exist with the other names.
|
|
2073 */ )
|
|
2074 (filename)
|
|
2075 Lisp_Object filename;
|
|
2076 {
|
|
2077 /* This function can GC */
|
|
2078 Lisp_Object handler;
|
|
2079 struct gcpro gcpro1;
|
|
2080
|
|
2081 GCPRO1 (filename);
|
|
2082 CHECK_STRING (filename);
|
|
2083 filename = Fexpand_file_name (filename, Qnil);
|
|
2084
|
|
2085 handler = Ffind_file_name_handler (filename, Qdelete_file);
|
|
2086 UNGCPRO;
|
|
2087 if (!NILP (handler))
|
|
2088 return call2 (handler, Qdelete_file, filename);
|
|
2089
|
16
|
2090 if (0 > unlink ((char *) XSTRING_DATA (filename)))
|
0
|
2091 report_file_error ("Removing old name", list1 (filename));
|
|
2092 return Qnil;
|
|
2093 }
|
|
2094
|
|
2095 static Lisp_Object
|
|
2096 internal_delete_file_1 (Lisp_Object ignore, Lisp_Object ignore2)
|
|
2097 {
|
|
2098 return Qt;
|
|
2099 }
|
|
2100
|
|
2101 /* Delete file FILENAME, returning 1 if successful and 0 if failed. */
|
|
2102
|
|
2103 int
|
|
2104 internal_delete_file (Lisp_Object filename)
|
|
2105 {
|
|
2106 return NILP (condition_case_1 (Qt, Fdelete_file, filename,
|
|
2107 internal_delete_file_1, Qnil));
|
|
2108 }
|
|
2109
|
|
2110 DEFUN ("rename-file", Frename_file, Srename_file, 2, 3,
|
|
2111 "fRename file: \nFRename %s to file: \np" /*
|
|
2112 Rename FILE as NEWNAME. Both args strings.
|
|
2113 If file has names other than FILE, it continues to have those names.
|
|
2114 Signals a `file-already-exists' error if a file NEWNAME already exists
|
|
2115 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
|
|
2116 A number as third arg means request confirmation if NEWNAME already exists.
|
|
2117 This is what happens in interactive use with M-x.
|
|
2118 */ )
|
|
2119 (filename, newname, ok_if_already_exists)
|
|
2120 Lisp_Object filename, newname, ok_if_already_exists;
|
|
2121 {
|
|
2122 /* This function can GC */
|
|
2123 Lisp_Object handler;
|
|
2124 struct gcpro gcpro1, gcpro2;
|
|
2125
|
|
2126 GCPRO2 (filename, newname);
|
|
2127 CHECK_STRING (filename);
|
|
2128 CHECK_STRING (newname);
|
|
2129 filename = Fexpand_file_name (filename, Qnil);
|
|
2130 newname = Fexpand_file_name (newname, Qnil);
|
|
2131
|
|
2132 /* If the file name has special constructs in it,
|
|
2133 call the corresponding file handler. */
|
|
2134 handler = Ffind_file_name_handler (filename, Qrename_file);
|
|
2135 if (NILP (handler))
|
|
2136 handler = Ffind_file_name_handler (newname, Qrename_file);
|
|
2137 if (!NILP (handler))
|
|
2138 {
|
|
2139 UNGCPRO;
|
|
2140 return call4 (handler, Qrename_file,
|
|
2141 filename, newname, ok_if_already_exists);
|
|
2142 }
|
|
2143
|
|
2144 /* When second argument is a directory, rename the file into it.
|
|
2145 (rename-file "foo" "bar/") == (rename-file "foo" "bar/foo")
|
|
2146 */
|
|
2147 if (!NILP (Ffile_directory_p (newname)))
|
|
2148 {
|
|
2149 Lisp_Object args[3];
|
|
2150 struct gcpro ngcpro1;
|
|
2151 int i = 1;
|
|
2152
|
|
2153 args[0] = newname;
|
|
2154 args[1] = Qnil; args[2] = Qnil;
|
|
2155 NGCPRO1 (*args);
|
|
2156 ngcpro1.nvars = 3;
|
16
|
2157 if (XSTRING_BYTE (newname, XSTRING_LENGTH (newname) - 1) != '/')
|
0
|
2158 args[i++] = build_string ("/");
|
|
2159 args[i++] = Ffile_name_nondirectory (filename);
|
|
2160 newname = Fconcat (i, args);
|
|
2161 NUNGCPRO;
|
|
2162 }
|
|
2163
|
|
2164 if (NILP (ok_if_already_exists)
|
|
2165 || INTP (ok_if_already_exists))
|
|
2166 barf_or_query_if_file_exists (newname, "rename to it",
|
|
2167 INTP (ok_if_already_exists), 0);
|
|
2168
|
|
2169 #ifdef WINDOWSNT
|
|
2170 if (!MoveFile (XSTRING (filename)->data, XSTRING (newname)->data))
|
|
2171 #else /* not WINDOWSNT */
|
|
2172 /* FSFmacs only calls rename() here under BSD 4.1, and calls
|
|
2173 link() and unlink() otherwise, but that's bogus. Sometimes
|
|
2174 rename() succeeds where link()/unlink() fail, and we have
|
|
2175 configure check for rename() and emulate using link()/unlink()
|
|
2176 if necessary. */
|
16
|
2177 if (0 > rename ((char *) XSTRING_DATA (filename),
|
|
2178 (char *) XSTRING_DATA (newname)))
|
0
|
2179 #endif /* not WINDOWSNT */
|
|
2180 {
|
|
2181 #ifdef WINDOWSNT
|
|
2182 /* Why two? And why doesn't MS document what MoveFile will return? */
|
|
2183 if (GetLastError () == ERROR_FILE_EXISTS
|
|
2184 || GetLastError () == ERROR_ALREADY_EXISTS)
|
|
2185 #else /* not WINDOWSNT */
|
|
2186 if (errno == EXDEV)
|
|
2187 #endif /* not WINDOWSNT */
|
|
2188 {
|
|
2189 Fcopy_file (filename, newname,
|
|
2190 /* We have already prompted if it was an integer,
|
|
2191 so don't have copy-file prompt again. */
|
|
2192 ((NILP (ok_if_already_exists)) ? Qnil : Qt),
|
|
2193 Qt);
|
|
2194 Fdelete_file (filename);
|
|
2195 }
|
|
2196 else
|
|
2197 {
|
|
2198 report_file_error ("Renaming", list2 (filename, newname));
|
|
2199 }
|
|
2200 }
|
|
2201 UNGCPRO;
|
|
2202 return Qnil;
|
|
2203 }
|
|
2204
|
|
2205 DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3,
|
|
2206 "fAdd name to file: \nFName to add to %s: \np" /*
|
|
2207 Give FILE additional name NEWNAME. Both args strings.
|
|
2208 Signals a `file-already-exists' error if a file NEWNAME already exists
|
|
2209 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
|
|
2210 A number as third arg means request confirmation if NEWNAME already exists.
|
|
2211 This is what happens in interactive use with M-x.
|
|
2212 */ )
|
|
2213 (filename, newname, ok_if_already_exists)
|
|
2214 Lisp_Object filename, newname, ok_if_already_exists;
|
|
2215 {
|
|
2216 /* This function can GC */
|
|
2217 Lisp_Object handler;
|
|
2218 struct gcpro gcpro1, gcpro2;
|
|
2219
|
|
2220 GCPRO2 (filename, newname);
|
|
2221 CHECK_STRING (filename);
|
|
2222 CHECK_STRING (newname);
|
|
2223 filename = Fexpand_file_name (filename, Qnil);
|
|
2224 newname = Fexpand_file_name (newname, Qnil);
|
|
2225
|
|
2226 /* If the file name has special constructs in it,
|
|
2227 call the corresponding file handler. */
|
|
2228 handler = Ffind_file_name_handler (filename, Qadd_name_to_file);
|
|
2229 if (!NILP (handler))
|
|
2230 RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, filename,
|
|
2231 newname, ok_if_already_exists));
|
|
2232
|
|
2233 /* If the new name has special constructs in it,
|
|
2234 call the corresponding file handler. */
|
|
2235 handler = Ffind_file_name_handler (newname, Qadd_name_to_file);
|
|
2236 if (!NILP (handler))
|
|
2237 RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, filename,
|
|
2238 newname, ok_if_already_exists));
|
|
2239
|
|
2240 if (NILP (ok_if_already_exists)
|
|
2241 || INTP (ok_if_already_exists))
|
|
2242 barf_or_query_if_file_exists (newname, "make it a new name",
|
|
2243 INTP (ok_if_already_exists), 0);
|
|
2244 #ifdef WINDOWSNT
|
|
2245 /* Windows does not support this operation. */
|
|
2246 report_file_error ("Adding new name", Flist (2, &filename));
|
|
2247 #else /* not WINDOWSNT */
|
|
2248
|
16
|
2249 unlink ((char *) XSTRING_DATA (newname));
|
|
2250 if (0 > link ((char *) XSTRING_DATA (filename),
|
|
2251 (char *) XSTRING_DATA (newname)))
|
0
|
2252 {
|
|
2253 report_file_error ("Adding new name",
|
|
2254 list2 (filename, newname));
|
|
2255 }
|
|
2256 #endif /* not WINDOWSNT */
|
|
2257
|
|
2258 UNGCPRO;
|
|
2259 return Qnil;
|
|
2260 }
|
|
2261
|
|
2262 #ifdef S_IFLNK
|
|
2263 DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3,
|
|
2264 "FMake symbolic link to file: \nFMake symbolic link to file %s: \np" /*
|
|
2265 Make a symbolic link to FILENAME, named LINKNAME. Both args strings.
|
|
2266 Signals a `file-already-exists' error if a file LINKNAME already exists
|
|
2267 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
|
|
2268 A number as third arg means request confirmation if LINKNAME already exists.
|
|
2269 This happens for interactive use with M-x.
|
|
2270 */ )
|
|
2271 (filename, linkname, ok_if_already_exists)
|
|
2272 Lisp_Object filename, linkname, ok_if_already_exists;
|
|
2273 {
|
|
2274 /* This function can GC */
|
|
2275 Lisp_Object handler;
|
|
2276 struct gcpro gcpro1, gcpro2;
|
|
2277
|
|
2278 GCPRO2 (filename, linkname);
|
|
2279 CHECK_STRING (filename);
|
|
2280 CHECK_STRING (linkname);
|
|
2281 /* If the link target has a ~, we must expand it to get
|
|
2282 a truly valid file name. Otherwise, do not expand;
|
|
2283 we want to permit links to relative file names. */
|
16
|
2284 if (XSTRING_BYTE (filename, 0) == '~') /* #### Un*x-specific */
|
0
|
2285 filename = Fexpand_file_name (filename, Qnil);
|
|
2286 linkname = Fexpand_file_name (linkname, Qnil);
|
|
2287
|
|
2288 /* If the file name has special constructs in it,
|
|
2289 call the corresponding file handler. */
|
|
2290 handler = Ffind_file_name_handler (filename, Qmake_symbolic_link);
|
|
2291 if (!NILP (handler))
|
|
2292 RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, linkname,
|
|
2293 ok_if_already_exists));
|
|
2294
|
|
2295 /* If the new link name has special constructs in it,
|
|
2296 call the corresponding file handler. */
|
|
2297 handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link);
|
|
2298 if (!NILP (handler))
|
|
2299 RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename,
|
|
2300 linkname, ok_if_already_exists));
|
|
2301
|
|
2302 if (NILP (ok_if_already_exists)
|
|
2303 || INTP (ok_if_already_exists))
|
|
2304 barf_or_query_if_file_exists (linkname, "make it a link",
|
|
2305 INTP (ok_if_already_exists), 0);
|
|
2306
|
16
|
2307 unlink ((char *) XSTRING_DATA (linkname));
|
|
2308 if (0 > symlink ((char *) XSTRING_DATA (filename),
|
|
2309 (char *) XSTRING_DATA (linkname)))
|
0
|
2310 {
|
|
2311 report_file_error ("Making symbolic link",
|
|
2312 list2 (filename, linkname));
|
|
2313 }
|
|
2314 UNGCPRO;
|
|
2315 return Qnil;
|
|
2316 }
|
|
2317 #endif /* S_IFLNK */
|
|
2318
|
|
2319 #ifdef VMS
|
|
2320
|
|
2321 DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name,
|
|
2322 2, 2, "sDefine logical name: \nsDefine logical name %s as: " /*
|
|
2323 Define the job-wide logical name NAME to have the value STRING.
|
|
2324 If STRING is nil or a null string, the logical name NAME is deleted.
|
|
2325 */ )
|
|
2326 (varname, string)
|
|
2327 Lisp_Object varname;
|
|
2328 Lisp_Object string;
|
|
2329 {
|
|
2330 CHECK_STRING (varname);
|
|
2331 if (NILP (string))
|
16
|
2332 delete_logical_name ((char *) XSTRING_DATA (varname));
|
0
|
2333 else
|
|
2334 {
|
|
2335 CHECK_STRING (string);
|
|
2336
|
16
|
2337 if (XSTRING_LENGTH (string) == 0)
|
|
2338 delete_logical_name ((char *) XSTRING_DATA (varname));
|
0
|
2339 else
|
16
|
2340 define_logical_name ((char *) XSTRING_DATA (varname), (char *) XSTRING_DATA (string));
|
0
|
2341 }
|
|
2342
|
|
2343 return string;
|
|
2344 }
|
|
2345 #endif /* VMS */
|
|
2346
|
|
2347 #ifdef HPUX_NET
|
|
2348
|
|
2349 DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0 /*
|
|
2350 Open a network connection to PATH using LOGIN as the login string.
|
|
2351 */ )
|
|
2352 (path, login)
|
|
2353 Lisp_Object path, login;
|
|
2354 {
|
|
2355 int netresult;
|
|
2356
|
|
2357 CHECK_STRING (path);
|
|
2358 CHECK_STRING (login);
|
|
2359
|
|
2360 /* netunam, being a strange-o system call only used once, is not
|
|
2361 encapsulated. */
|
|
2362 {
|
|
2363 char *path_ext;
|
|
2364 char *login_ext;
|
|
2365
|
|
2366 GET_C_STRING_FILENAME_DATA_ALLOCA (path, path_ext);
|
|
2367 GET_C_STRING_EXT_DATA_ALLOCA (login, FORMAT_OS, login_ext);
|
|
2368
|
|
2369 netresult = netunam (path_ext, login_ext);
|
|
2370 }
|
|
2371
|
|
2372 if (netresult == -1)
|
|
2373 return Qnil;
|
|
2374 else
|
|
2375 return Qt;
|
|
2376 }
|
|
2377 #endif /* HPUX_NET */
|
|
2378
|
|
2379 DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p,
|
|
2380 1, 1, 0 /*
|
|
2381 Return t if file FILENAME specifies an absolute path name.
|
|
2382 On Unix, this is a name starting with a `/' or a `~'.
|
|
2383 */ )
|
|
2384 (filename)
|
|
2385 Lisp_Object filename;
|
|
2386 {
|
|
2387 Bufbyte *ptr;
|
|
2388
|
|
2389 CHECK_STRING (filename);
|
16
|
2390 ptr = XSTRING_DATA (filename);
|
0
|
2391 if (IS_DIRECTORY_SEP (*ptr) || *ptr == '~'
|
|
2392 #ifdef VMS
|
|
2393 /* ??? This criterion is probably wrong for '<'. */
|
|
2394 || strchr (ptr, ':') || strchr (ptr, '<')
|
|
2395 || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']'))
|
|
2396 && ptr[1] != '.')
|
|
2397 #endif /* VMS */
|
|
2398 #ifdef DOS_NT
|
|
2399 || (*ptr != 0 && ptr[1] == ':' && (ptr[2] == '/' || ptr[2] == '\\'))
|
|
2400 #endif
|
|
2401 )
|
|
2402 return Qt;
|
|
2403 else
|
|
2404 return Qnil;
|
|
2405 }
|
|
2406
|
|
2407 /* Return nonzero if file FILENAME exists and can be executed. */
|
|
2408
|
|
2409 static int
|
|
2410 check_executable (char *filename)
|
|
2411 {
|
|
2412 #ifdef DOS_NT
|
|
2413 int len = strlen (filename);
|
|
2414 char *suffix;
|
|
2415 struct stat st;
|
|
2416 if (stat (filename, &st) < 0)
|
|
2417 return 0;
|
|
2418 return (S_ISREG (st.st_mode)
|
|
2419 && len >= 5
|
|
2420 && (stricmp ((suffix = filename + len-4), ".com") == 0
|
|
2421 || stricmp (suffix, ".exe") == 0
|
|
2422 || stricmp (suffix, ".bat") == 0)
|
|
2423 || (st.st_mode & S_IFMT) == S_IFDIR);
|
|
2424 #else /* not DOS_NT */
|
|
2425 #ifdef HAVE_EACCESS
|
|
2426 return (eaccess (filename, 1) >= 0);
|
|
2427 #else
|
|
2428 /* Access isn't quite right because it uses the real uid
|
|
2429 and we really want to test with the effective uid.
|
|
2430 But Unix doesn't give us a right way to do it. */
|
|
2431 return (access (filename, 1) >= 0);
|
|
2432 #endif
|
|
2433 #endif /* not DOS_NT */
|
|
2434 }
|
|
2435
|
|
2436 /* Return nonzero if file FILENAME exists and can be written. */
|
|
2437
|
|
2438 static int
|
|
2439 check_writable (CONST char *filename)
|
|
2440 {
|
|
2441 #ifdef MSDOS
|
|
2442 struct stat st;
|
|
2443 if (stat (filename, &st) < 0)
|
|
2444 return 0;
|
|
2445 return (st.st_mode & S_IWRITE || (st.st_mode & S_IFMT) == S_IFDIR);
|
|
2446 #else /* not MSDOS */
|
|
2447 #ifdef HAVE_EACCESS
|
|
2448 return (eaccess (filename, 2) >= 0);
|
|
2449 #else
|
|
2450 /* Access isn't quite right because it uses the real uid
|
|
2451 and we really want to test with the effective uid.
|
|
2452 But Unix doesn't give us a right way to do it.
|
|
2453 Opening with O_WRONLY could work for an ordinary file,
|
|
2454 but would lose for directories. */
|
|
2455 return (access (filename, 2) >= 0);
|
|
2456 #endif
|
|
2457 #endif /* not MSDOS */
|
|
2458 }
|
|
2459
|
|
2460 DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0 /*
|
|
2461 Return t if file FILENAME exists. (This does not mean you can read it.)
|
|
2462 See also `file-readable-p' and `file-attributes'.
|
|
2463 */ )
|
|
2464 (filename)
|
|
2465 Lisp_Object filename;
|
|
2466 {
|
|
2467 /* This function can GC */
|
|
2468 Lisp_Object abspath;
|
|
2469 Lisp_Object handler;
|
|
2470 struct stat statbuf;
|
|
2471 struct gcpro gcpro1;
|
|
2472
|
|
2473 GCPRO1 (filename);
|
|
2474 CHECK_STRING (filename);
|
|
2475 abspath = Fexpand_file_name (filename, Qnil);
|
|
2476 UNGCPRO;
|
|
2477
|
|
2478 /* If the file name has special constructs in it,
|
|
2479 call the corresponding file handler. */
|
|
2480 GCPRO1 (abspath);
|
|
2481 handler = Ffind_file_name_handler (abspath, Qfile_exists_p);
|
|
2482 UNGCPRO;
|
|
2483 if (!NILP (handler))
|
|
2484 return call2 (handler, Qfile_exists_p, abspath);
|
|
2485
|
16
|
2486 if (stat ((char *) XSTRING_DATA (abspath), &statbuf) >= 0)
|
0
|
2487 return (Qt);
|
|
2488 else
|
|
2489 return (Qnil);
|
|
2490 }
|
|
2491
|
|
2492 DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0 /*
|
|
2493 Return t if FILENAME can be executed by you.
|
|
2494 For a directory, this means you can access files in that directory.
|
|
2495 */ )
|
|
2496 (filename)
|
|
2497 Lisp_Object filename;
|
|
2498
|
|
2499 {
|
|
2500 /* This function can GC */
|
|
2501 Lisp_Object abspath;
|
|
2502 Lisp_Object handler;
|
|
2503 struct gcpro gcpro1;
|
|
2504
|
|
2505 GCPRO1 (filename);
|
|
2506 CHECK_STRING (filename);
|
|
2507 abspath = Fexpand_file_name (filename, Qnil);
|
|
2508 UNGCPRO;
|
|
2509
|
|
2510 /* If the file name has special constructs in it,
|
|
2511 call the corresponding file handler. */
|
|
2512 GCPRO1 (abspath);
|
|
2513 handler = Ffind_file_name_handler (abspath, Qfile_executable_p);
|
|
2514 UNGCPRO;
|
|
2515 if (!NILP (handler))
|
|
2516 return call2 (handler, Qfile_executable_p, abspath);
|
|
2517
|
16
|
2518 return (check_executable ((char *) XSTRING_DATA (abspath))
|
0
|
2519 ? Qt : Qnil);
|
|
2520 }
|
|
2521
|
|
2522 DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0 /*
|
|
2523 Return t if file FILENAME exists and you can read it.
|
|
2524 See also `file-exists-p' and `file-attributes'.
|
|
2525 */ )
|
|
2526 (filename)
|
|
2527 Lisp_Object filename;
|
|
2528 {
|
|
2529 /* This function can GC */
|
|
2530 Lisp_Object abspath;
|
|
2531 Lisp_Object handler;
|
|
2532 int desc;
|
|
2533 struct gcpro gcpro1;
|
|
2534
|
|
2535 GCPRO1 (filename);
|
|
2536 CHECK_STRING (filename);
|
|
2537 abspath = Fexpand_file_name (filename, Qnil);
|
|
2538 UNGCPRO;
|
|
2539
|
|
2540 /* If the file name has special constructs in it,
|
|
2541 call the corresponding file handler. */
|
|
2542 GCPRO1 (abspath);
|
|
2543 handler = Ffind_file_name_handler (abspath, Qfile_readable_p);
|
|
2544 UNGCPRO;
|
|
2545 if (!NILP (handler))
|
|
2546 return call2 (handler, Qfile_readable_p, abspath);
|
|
2547
|
16
|
2548 desc = open ((char *) XSTRING_DATA (abspath), O_RDONLY, 0);
|
0
|
2549 if (desc < 0)
|
|
2550 return Qnil;
|
|
2551 close (desc);
|
|
2552 return Qt;
|
|
2553 }
|
|
2554
|
|
2555 /* Having this before file-symlink-p mysteriously caused it to be forgotten
|
|
2556 on the RT/PC. */
|
|
2557 DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0 /*
|
|
2558 Return t if file FILENAME can be written or created by you.
|
|
2559 */ )
|
|
2560 (filename)
|
|
2561 Lisp_Object filename;
|
|
2562 {
|
|
2563 /* This function can GC */
|
|
2564 Lisp_Object abspath, dir;
|
|
2565 Lisp_Object handler;
|
|
2566 struct stat statbuf;
|
|
2567 struct gcpro gcpro1;
|
|
2568
|
|
2569 GCPRO1 (filename);
|
|
2570 CHECK_STRING (filename);
|
|
2571 abspath = Fexpand_file_name (filename, Qnil);
|
|
2572 UNGCPRO;
|
|
2573
|
|
2574 /* If the file name has special constructs in it,
|
|
2575 call the corresponding file handler. */
|
|
2576 GCPRO1 (abspath);
|
|
2577 handler = Ffind_file_name_handler (abspath, Qfile_writable_p);
|
|
2578 UNGCPRO;
|
|
2579 if (!NILP (handler))
|
|
2580 return call2 (handler, Qfile_writable_p, abspath);
|
|
2581
|
16
|
2582 if (stat ((char *) XSTRING_DATA (abspath), &statbuf) >= 0)
|
|
2583 return (check_writable ((char *) XSTRING_DATA (abspath))
|
0
|
2584 ? Qt : Qnil);
|
|
2585
|
|
2586
|
|
2587 dir = Ffile_name_directory (abspath);
|
|
2588 #if defined (VMS) || defined (MSDOS)
|
|
2589 if (!NILP (dir))
|
|
2590 dir = Fdirectory_file_name (dir);
|
|
2591 #endif /* VMS or MSDOS */
|
16
|
2592 return (check_writable (!NILP (dir) ? (char *) XSTRING_DATA (dir)
|
0
|
2593 : "")
|
|
2594 ? Qt : Qnil);
|
|
2595 }
|
|
2596
|
|
2597 DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0 /*
|
|
2598 Return non-nil if file FILENAME is the name of a symbolic link.
|
|
2599 The value is the name of the file to which it is linked.
|
|
2600 Otherwise returns nil.
|
|
2601 */ )
|
|
2602 (filename)
|
|
2603 Lisp_Object filename;
|
|
2604 {
|
|
2605 /* This function can GC */
|
|
2606 #ifdef S_IFLNK
|
|
2607 char *buf;
|
|
2608 int bufsize;
|
|
2609 int valsize;
|
|
2610 Lisp_Object val;
|
|
2611 Lisp_Object handler;
|
|
2612 struct gcpro gcpro1;
|
|
2613
|
|
2614 GCPRO1 (filename);
|
|
2615 CHECK_STRING (filename);
|
|
2616 filename = Fexpand_file_name (filename, Qnil);
|
|
2617 UNGCPRO;
|
|
2618
|
|
2619 /* If the file name has special constructs in it,
|
|
2620 call the corresponding file handler. */
|
|
2621 handler = Ffind_file_name_handler (filename, Qfile_symlink_p);
|
|
2622 if (!NILP (handler))
|
|
2623 return call2 (handler, Qfile_symlink_p, filename);
|
|
2624
|
|
2625 bufsize = 100;
|
|
2626 while (1)
|
|
2627 {
|
|
2628 buf = (char *) xmalloc (bufsize);
|
|
2629 memset (buf, 0, bufsize);
|
16
|
2630 valsize = readlink ((char *) XSTRING_DATA (filename),
|
0
|
2631 buf, bufsize);
|
|
2632 if (valsize < bufsize) break;
|
|
2633 /* Buffer was not long enough */
|
|
2634 xfree (buf);
|
|
2635 bufsize *= 2;
|
|
2636 }
|
|
2637 if (valsize == -1)
|
|
2638 {
|
|
2639 xfree (buf);
|
|
2640 return Qnil;
|
|
2641 }
|
|
2642 val = make_string ((Bufbyte *) buf, valsize);
|
|
2643 xfree (buf);
|
|
2644 return val;
|
|
2645 #else /* not S_IFLNK */
|
|
2646 return Qnil;
|
|
2647 #endif /* not S_IFLNK */
|
|
2648 }
|
|
2649
|
|
2650 DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0 /*
|
|
2651 Return t if file FILENAME is the name of a directory as a file.
|
|
2652 A directory name spec may be given instead; then the value is t
|
|
2653 if the directory so specified exists and really is a directory.
|
|
2654 */ )
|
|
2655 (filename)
|
|
2656 Lisp_Object filename;
|
|
2657 {
|
|
2658 /* This function can GC */
|
|
2659 Lisp_Object abspath;
|
|
2660 struct stat st;
|
|
2661 Lisp_Object handler;
|
|
2662 struct gcpro gcpro1;
|
|
2663
|
|
2664 GCPRO1 (filename);
|
|
2665 abspath = expand_and_dir_to_file (filename,
|
|
2666 current_buffer->directory);
|
|
2667 UNGCPRO;
|
|
2668
|
|
2669 /* If the file name has special constructs in it,
|
|
2670 call the corresponding file handler. */
|
|
2671 GCPRO1 (abspath);
|
|
2672 handler = Ffind_file_name_handler (abspath, Qfile_directory_p);
|
|
2673 UNGCPRO;
|
|
2674 if (!NILP (handler))
|
|
2675 return call2 (handler, Qfile_directory_p, abspath);
|
|
2676
|
16
|
2677 if (stat ((char *) XSTRING_DATA (abspath), &st) < 0)
|
0
|
2678 return Qnil;
|
|
2679 return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
|
|
2680 }
|
|
2681
|
|
2682 DEFUN ("file-accessible-directory-p",
|
|
2683 Ffile_accessible_directory_p, Sfile_accessible_directory_p, 1, 1, 0 /*
|
|
2684 Return t if file FILENAME is the name of a directory as a file,
|
|
2685 and files in that directory can be opened by you. In order to use a
|
|
2686 directory as a buffer's current directory, this predicate must return true.
|
|
2687 A directory name spec may be given instead; then the value is t
|
|
2688 if the directory so specified exists and really is a readable and
|
|
2689 searchable directory.
|
|
2690 */ )
|
|
2691 (filename)
|
|
2692 Lisp_Object filename;
|
|
2693 {
|
|
2694 /* This function can GC */
|
|
2695 Lisp_Object handler;
|
|
2696 struct gcpro gcpro1;
|
|
2697
|
|
2698 /* If the file name has special constructs in it,
|
|
2699 call the corresponding file handler. */
|
|
2700 handler = Ffind_file_name_handler (filename, Qfile_accessible_directory_p);
|
|
2701 if (!NILP (handler))
|
|
2702 return call2 (handler, Qfile_accessible_directory_p,
|
|
2703 filename);
|
|
2704
|
|
2705 GCPRO1 (filename);
|
|
2706 if (NILP (Ffile_directory_p (filename)))
|
|
2707 {
|
|
2708 UNGCPRO;
|
|
2709 return (Qnil);
|
|
2710 }
|
|
2711 handler = Ffile_executable_p (filename);
|
|
2712 UNGCPRO;
|
|
2713 return (handler);
|
|
2714 }
|
|
2715
|
|
2716 DEFUN ("file-regular-p", Ffile_regular_p, Sfile_regular_p, 1, 1, 0 /*
|
|
2717 "Return t if file FILENAME is the name of a regular file.
|
|
2718 This is the sort of file that holds an ordinary stream of data bytes.
|
|
2719 */ )
|
|
2720 (filename)
|
|
2721 Lisp_Object filename;
|
|
2722 {
|
|
2723 REGISTER Lisp_Object abspath;
|
|
2724 struct stat st;
|
|
2725 Lisp_Object handler;
|
|
2726
|
|
2727 abspath = expand_and_dir_to_file (filename, current_buffer->directory);
|
|
2728
|
|
2729 /* If the file name has special constructs in it,
|
|
2730 call the corresponding file handler. */
|
|
2731 handler = Ffind_file_name_handler (abspath, Qfile_regular_p);
|
|
2732 if (!NILP (handler))
|
|
2733 return call2 (handler, Qfile_regular_p, abspath);
|
|
2734
|
16
|
2735 if (stat ((char *) XSTRING_DATA (abspath), &st) < 0)
|
0
|
2736 return Qnil;
|
|
2737 return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
|
|
2738 }
|
|
2739
|
|
2740 DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0 /*
|
|
2741 Return mode bits of FILE, as an integer.
|
|
2742 */ )
|
|
2743 (filename)
|
|
2744 Lisp_Object filename;
|
|
2745 {
|
|
2746 /* This function can GC */
|
|
2747 Lisp_Object abspath;
|
|
2748 struct stat st;
|
|
2749 Lisp_Object handler;
|
|
2750 struct gcpro gcpro1;
|
|
2751
|
|
2752 GCPRO1 (filename);
|
|
2753 abspath = expand_and_dir_to_file (filename,
|
|
2754 current_buffer->directory);
|
|
2755 UNGCPRO;
|
|
2756
|
|
2757 /* If the file name has special constructs in it,
|
|
2758 call the corresponding file handler. */
|
|
2759 GCPRO1 (abspath);
|
|
2760 handler = Ffind_file_name_handler (abspath, Qfile_modes);
|
|
2761 UNGCPRO;
|
|
2762 if (!NILP (handler))
|
|
2763 return call2 (handler, Qfile_modes, abspath);
|
|
2764
|
16
|
2765 if (stat ((char *) XSTRING_DATA (abspath), &st) < 0)
|
0
|
2766 return Qnil;
|
|
2767 #ifdef DOS_NT
|
|
2768 if (check_executable (XSTRING (abspath)->data))
|
|
2769 st.st_mode |= S_IEXEC;
|
|
2770 #endif /* DOS_NT */
|
|
2771
|
|
2772 return make_int (st.st_mode & 07777);
|
|
2773 }
|
|
2774
|
|
2775 DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0 /*
|
|
2776 Set mode bits of FILE to MODE (an integer).
|
|
2777 Only the 12 low bits of MODE are used.
|
|
2778 */ )
|
|
2779 (filename, mode)
|
|
2780 Lisp_Object filename, mode;
|
|
2781 {
|
|
2782 /* This function can GC */
|
|
2783 Lisp_Object abspath;
|
|
2784 Lisp_Object handler;
|
|
2785 struct gcpro gcpro1, gcpro2;
|
|
2786
|
|
2787 GCPRO2 (filename, mode);
|
|
2788 abspath = Fexpand_file_name (filename, current_buffer->directory);
|
|
2789 CHECK_INT (mode);
|
|
2790 UNGCPRO;
|
|
2791
|
|
2792 /* If the file name has special constructs in it,
|
|
2793 call the corresponding file handler. */
|
|
2794 GCPRO1 (abspath);
|
|
2795 handler = Ffind_file_name_handler (abspath, Qset_file_modes);
|
|
2796 UNGCPRO;
|
|
2797 if (!NILP (handler))
|
|
2798 return call3 (handler, Qset_file_modes, abspath, mode);
|
|
2799
|
16
|
2800 if (chmod ((char *) XSTRING_DATA (abspath), XINT (mode)) < 0)
|
0
|
2801 report_file_error ("Doing chmod", Fcons (abspath, Qnil));
|
|
2802
|
|
2803 return Qnil;
|
|
2804 }
|
|
2805
|
|
2806 DEFUN ("set-default-file-modes", Fset_default_file_modes,
|
|
2807 Sset_default_file_modes, 1, 1, 0 /*
|
|
2808 Set the file permission bits for newly created files.
|
|
2809 MASK should be an integer; if a permission's bit in MASK is 1,
|
|
2810 subsequently created files will not have that permission enabled.
|
|
2811 Only the low 9 bits are used.
|
|
2812 This setting is inherited by subprocesses.
|
|
2813 */ )
|
|
2814 (mode)
|
|
2815 Lisp_Object mode;
|
|
2816 {
|
|
2817 CHECK_INT (mode);
|
|
2818
|
|
2819 umask ((~ XINT (mode)) & 0777);
|
|
2820
|
|
2821 return Qnil;
|
|
2822 }
|
|
2823
|
|
2824 DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0 /*
|
|
2825 Return the default file protection for created files.
|
|
2826 The umask value determines which permissions are enabled in newly
|
|
2827 created files. If a permission's bit in the umask is 1, subsequently
|
|
2828 created files will not have that permission enabled.
|
|
2829 */ )
|
|
2830 ()
|
|
2831 {
|
|
2832 int mode;
|
|
2833
|
|
2834 mode = umask (0);
|
|
2835 umask (mode);
|
|
2836
|
|
2837 return make_int ((~ mode) & 0777);
|
|
2838 }
|
|
2839
|
|
2840 #ifndef VMS
|
|
2841 DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "" /*
|
|
2842 Tell Unix to finish all pending disk updates.
|
|
2843 */ )
|
|
2844 ()
|
|
2845 {
|
|
2846 sync ();
|
|
2847 return Qnil;
|
|
2848 }
|
|
2849 #endif /* !VMS */
|
|
2850
|
|
2851
|
|
2852 DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p,
|
|
2853 Sfile_newer_than_file_p, 2, 2, 0 /*
|
|
2854 Return t if file FILE1 is newer than file FILE2.
|
|
2855 If FILE1 does not exist, the answer is nil;
|
|
2856 otherwise, if FILE2 does not exist, the answer is t.
|
|
2857 */ )
|
|
2858 (file1, file2)
|
|
2859 Lisp_Object file1, file2;
|
|
2860 {
|
|
2861 /* This function can GC */
|
|
2862 Lisp_Object abspath1, abspath2;
|
|
2863 struct stat st;
|
|
2864 int mtime1;
|
|
2865 Lisp_Object handler;
|
|
2866 struct gcpro gcpro1, gcpro2;
|
|
2867
|
|
2868 CHECK_STRING (file1);
|
|
2869 CHECK_STRING (file2);
|
|
2870
|
|
2871 abspath1 = Qnil;
|
|
2872 GCPRO2 (abspath1, file2);
|
|
2873 abspath1 = expand_and_dir_to_file (file1,
|
|
2874 current_buffer->directory);
|
|
2875 abspath2 = expand_and_dir_to_file (file2,
|
|
2876 current_buffer->directory);
|
|
2877 UNGCPRO;
|
|
2878
|
|
2879 /* If the file name has special constructs in it,
|
|
2880 call the corresponding file handler. */
|
|
2881 GCPRO2 (abspath1, abspath2);
|
|
2882 handler = Ffind_file_name_handler (abspath1, Qfile_newer_than_file_p);
|
|
2883 if (NILP (handler))
|
|
2884 handler = Ffind_file_name_handler (abspath2, Qfile_newer_than_file_p);
|
|
2885 UNGCPRO;
|
|
2886 if (!NILP (handler))
|
|
2887 return call3 (handler, Qfile_newer_than_file_p, abspath1,
|
|
2888 abspath2);
|
|
2889
|
16
|
2890 if (stat ((char *) XSTRING_DATA (abspath1), &st) < 0)
|
0
|
2891 return Qnil;
|
|
2892
|
|
2893 mtime1 = st.st_mtime;
|
|
2894
|
16
|
2895 if (stat ((char *) XSTRING_DATA (abspath2), &st) < 0)
|
0
|
2896 return Qt;
|
|
2897
|
|
2898 return (mtime1 > st.st_mtime) ? Qt : Qnil;
|
|
2899 }
|
|
2900
|
|
2901
|
|
2902 #ifdef DOS_NT
|
|
2903 Lisp_Object Qfind_buffer_file_type;
|
|
2904 #endif /* DOS_NT */
|
|
2905
|
2
|
2906 /* Stack sizes > 2**16 is a good way to elicit compiler bugs */
|
|
2907 /* #define READ_BUF_SIZE (2 << 16) */
|
|
2908 #define READ_BUF_SIZE (1 << 15)
|
0
|
2909
|
|
2910 DEFUN ("insert-file-contents-internal", Finsert_file_contents_internal,
|
|
2911 Sinsert_file_contents_internal, 1, 5, 0 /*
|
|
2912 Insert contents of file FILENAME after point.
|
|
2913 Returns list of absolute file name and length of data inserted.
|
|
2914 If second argument VISIT is non-nil, the buffer's visited filename
|
|
2915 and last save file modtime are set, and it is marked unmodified.
|
|
2916 If visiting and the file does not exist, visiting is completed
|
|
2917 before the error is signaled.
|
|
2918
|
|
2919 The optional third and fourth arguments BEG and END
|
|
2920 specify what portion of the file to insert.
|
|
2921 If VISIT is non-nil, BEG and END must be nil.
|
|
2922 If optional fifth argument REPLACE is non-nil,
|
|
2923 it means replace the current buffer contents (in the accessible portion)
|
|
2924 with the file contents. This is better than simply deleting and inserting
|
|
2925 the whole thing because (1) it preserves some marker positions
|
|
2926 and (2) it puts less data in the undo list.
|
|
2927 */ )
|
|
2928 (filename, visit, beg, end, replace)
|
|
2929 Lisp_Object filename, visit, beg, end, replace;
|
|
2930 {
|
|
2931 /* This function can GC */
|
|
2932 struct stat st;
|
|
2933 int fd;
|
|
2934 int saverrno = 0;
|
|
2935 Charcount inserted = 0;
|
|
2936 int speccount;
|
|
2937 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
2938 Lisp_Object handler = Qnil, val;
|
|
2939 int total;
|
|
2940 Bufbyte read_buf[READ_BUF_SIZE];
|
|
2941 int mc_count;
|
|
2942 struct buffer *buf = current_buffer;
|
|
2943 int not_regular = 0;
|
|
2944
|
|
2945 if (buf->base_buffer && ! NILP (visit))
|
|
2946 error ("Cannot do file visiting in an indirect buffer");
|
|
2947
|
|
2948 /* No need to call Fbarf_if_buffer_read_only() here.
|
|
2949 That's called in begin_multiple_change() or wherever. */
|
|
2950
|
|
2951 val = Qnil;
|
|
2952
|
|
2953 GCPRO4 (filename, val, visit, handler);
|
|
2954
|
|
2955 mc_count = (NILP (replace)) ?
|
|
2956 begin_multiple_change (buf, BUF_PT (buf), BUF_PT (buf)) :
|
|
2957 begin_multiple_change (buf, BUF_BEG (buf), BUF_Z (buf));
|
|
2958
|
|
2959 speccount = specpdl_depth (); /* begin_multiple_change also adds
|
|
2960 an unwind_protect */
|
|
2961
|
|
2962 filename = Fexpand_file_name (filename, Qnil);
|
|
2963
|
|
2964 /* If the file name has special constructs in it,
|
|
2965 call the corresponding file handler. */
|
|
2966 handler = Ffind_file_name_handler (filename, Qinsert_file_contents);
|
|
2967 if (!NILP (handler))
|
|
2968 {
|
|
2969 val = call6 (handler, Qinsert_file_contents, filename,
|
16
|
2970 visit, beg, end, replace);
|
0
|
2971 goto handled;
|
|
2972 }
|
|
2973
|
|
2974 if ( (!NILP (beg) || !NILP (end)) && !NILP (visit) )
|
|
2975 error ("Attempt to visit less than an entire file");
|
|
2976
|
|
2977 if (!NILP (beg))
|
|
2978 CHECK_INT (beg);
|
|
2979 else
|
|
2980 beg = Qzero;
|
|
2981
|
|
2982 if (!NILP (end))
|
|
2983 CHECK_INT (end);
|
|
2984
|
|
2985 fd = -1;
|
|
2986
|
|
2987 #ifndef APOLLO
|
16
|
2988 if (stat ((char *) XSTRING_DATA (filename), &st) < 0)
|
0
|
2989 #else /* APOLLO */
|
16
|
2990 if ((fd = open ((char *) XSTRING_DATA (filename), O_RDONLY, 0)) < 0
|
0
|
2991 || fstat (fd, &st) < 0)
|
|
2992 #endif /* APOLLO */
|
|
2993 {
|
|
2994 if (fd >= 0) close (fd);
|
|
2995 badopen:
|
|
2996 if (NILP (visit))
|
|
2997 report_file_error ("Opening input file",
|
|
2998 Fcons (filename, Qnil));
|
|
2999 st.st_mtime = -1;
|
|
3000 goto notfound;
|
|
3001 }
|
|
3002
|
|
3003 #ifdef S_IFREG
|
|
3004 /* This code will need to be changed in order to work on named
|
|
3005 pipes, and it's probably just not worth it. So we should at
|
|
3006 least signal an error. */
|
|
3007 if (!S_ISREG (st.st_mode))
|
|
3008 {
|
|
3009 if (NILP (visit))
|
|
3010 {
|
|
3011 end_multiple_change (buf, mc_count);
|
|
3012
|
|
3013 return Fsignal (Qfile_error,
|
|
3014 list2 (build_translated_string("not a regular file"),
|
|
3015 filename));
|
|
3016 }
|
|
3017 else
|
|
3018 {
|
|
3019 not_regular = 1;
|
|
3020 goto notfound;
|
|
3021 }
|
|
3022 }
|
|
3023 #endif
|
|
3024
|
|
3025 if (fd < 0)
|
16
|
3026 if ((fd = open ((char *) XSTRING_DATA (filename), O_RDONLY, 0)) < 0)
|
0
|
3027 goto badopen;
|
|
3028
|
|
3029 /* Replacement should preserve point as it preserves markers. */
|
|
3030 if (!NILP (replace))
|
|
3031 record_unwind_protect (restore_point_unwind, Fpoint_marker (Qnil, Qnil));
|
|
3032
|
|
3033 record_unwind_protect (close_file_unwind, make_int (fd));
|
|
3034
|
|
3035 /* Supposedly happens on VMS. */
|
|
3036 if (st.st_size < 0)
|
|
3037 error ("File size is negative");
|
|
3038
|
|
3039 if (NILP (end))
|
|
3040 {
|
|
3041 end = make_int (st.st_size);
|
|
3042 if (XINT (end) != st.st_size)
|
|
3043 error ("maximum buffer size exceeded");
|
|
3044 }
|
|
3045
|
|
3046 /* If requested, replace the accessible part of the buffer
|
|
3047 with the file contents. Avoid replacing text at the
|
|
3048 beginning or end of the buffer that matches the file contents;
|
|
3049 that preserves markers pointing to the unchanged parts. */
|
16
|
3050 #if !defined (DOS_NT) && !defined (MULE)
|
0
|
3051 /* The replace-mode code currently only works when the assumption
|
|
3052 'one byte == one char' holds true. This fails under MSDOS and
|
|
3053 Windows NT (because newlines are represented as CR-LF in text
|
16
|
3054 files). and under Mule because files may contain multibyte characters. */
|
0
|
3055 # define FSFMACS_SPEEDY_INSERT
|
|
3056 #endif
|
|
3057 #ifndef FSFMACS_SPEEDY_INSERT
|
|
3058 if (!NILP (replace))
|
|
3059 {
|
|
3060 buffer_delete_range (buf, BUF_BEG (buf), BUF_Z (buf),
|
|
3061 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3062 }
|
|
3063 #else /* FSFMACS_SPEEDY_INSERT */
|
|
3064 if (!NILP (replace))
|
|
3065 {
|
|
3066 char buffer[1 << 14];
|
|
3067 Bufpos same_at_start = BUF_BEGV (buf);
|
|
3068 Bufpos same_at_end = BUF_ZV (buf);
|
|
3069 int overlap;
|
|
3070
|
|
3071 /* Count how many chars at the start of the file
|
|
3072 match the text at the beginning of the buffer. */
|
|
3073 while (1)
|
|
3074 {
|
|
3075 int nread;
|
|
3076 Bufpos bufpos;
|
|
3077
|
|
3078 nread = read_allowing_quit (fd, buffer, sizeof buffer);
|
|
3079 if (nread < 0)
|
|
3080 error ("IO error reading %s: %s",
|
16
|
3081 XSTRING_DATA (filename), strerror (errno));
|
0
|
3082 else if (nread == 0)
|
|
3083 break;
|
|
3084 bufpos = 0;
|
|
3085 while (bufpos < nread && same_at_start < BUF_ZV (buf)
|
|
3086 && BUF_FETCH_CHAR (buf, same_at_start) == buffer[bufpos])
|
|
3087 same_at_start++, bufpos++;
|
|
3088 /* If we found a discrepancy, stop the scan.
|
|
3089 Otherwise loop around and scan the next bufferfull. */
|
|
3090 if (bufpos != nread)
|
|
3091 break;
|
|
3092 }
|
|
3093 /* If the file matches the buffer completely,
|
|
3094 there's no need to replace anything. */
|
|
3095 if (same_at_start - BUF_BEGV (buf) == st.st_size)
|
|
3096 {
|
|
3097 close (fd);
|
|
3098 unbind_to (speccount, Qnil);
|
|
3099 /* Truncate the buffer to the size of the file. */
|
|
3100 buffer_delete_range (buf, same_at_start, same_at_end,
|
|
3101 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3102 goto handled;
|
|
3103 }
|
|
3104 /* Count how many chars at the end of the file
|
|
3105 match the text at the end of the buffer. */
|
|
3106 while (1)
|
|
3107 {
|
|
3108 int total_read, nread;
|
|
3109 Bufpos bufpos, curpos, trial;
|
|
3110
|
|
3111 /* At what file position are we now scanning? */
|
|
3112 curpos = st.st_size - (BUF_ZV (buf) - same_at_end);
|
|
3113 /* If the entire file matches the buffer tail, stop the scan. */
|
|
3114 if (curpos == 0)
|
|
3115 break;
|
|
3116 /* How much can we scan in the next step? */
|
|
3117 trial = min (curpos, sizeof buffer);
|
|
3118 if (lseek (fd, curpos - trial, 0) < 0)
|
|
3119 report_file_error ("Setting file position",
|
|
3120 Fcons (filename, Qnil));
|
|
3121
|
|
3122 total_read = 0;
|
|
3123 while (total_read < trial)
|
|
3124 {
|
|
3125 nread = read_allowing_quit (fd, buffer + total_read,
|
|
3126 trial - total_read);
|
|
3127 if (nread <= 0)
|
|
3128 error ("IO error reading %s: %s",
|
16
|
3129 XSTRING_DATA (filename), strerror (errno));
|
0
|
3130 total_read += nread;
|
|
3131 }
|
|
3132 /* Scan this bufferfull from the end, comparing with
|
|
3133 the Emacs buffer. */
|
|
3134 bufpos = total_read;
|
|
3135 /* Compare with same_at_start to avoid counting some buffer text
|
|
3136 as matching both at the file's beginning and at the end. */
|
|
3137 while (bufpos > 0 && same_at_end > same_at_start
|
|
3138 && BUF_FETCH_CHAR (buf, same_at_end - 1) ==
|
|
3139 buffer[bufpos - 1])
|
|
3140 same_at_end--, bufpos--;
|
|
3141 /* If we found a discrepancy, stop the scan.
|
|
3142 Otherwise loop around and scan the preceding bufferfull. */
|
|
3143 if (bufpos != 0)
|
|
3144 break;
|
|
3145 /* If display current starts at beginning of line,
|
|
3146 keep it that way. */
|
|
3147 if (XBUFFER (XWINDOW (Fselected_window (Qnil))->buffer) == buf)
|
|
3148 XWINDOW (Fselected_window (Qnil))->start_at_line_beg =
|
|
3149 !NILP (Fbolp (make_buffer (buf)));
|
|
3150 }
|
|
3151
|
|
3152 /* Don't try to reuse the same piece of text twice. */
|
|
3153 overlap = same_at_start - BUF_BEGV (buf) -
|
|
3154 (same_at_end + st.st_size - BUF_ZV (buf));
|
|
3155 if (overlap > 0)
|
|
3156 same_at_end += overlap;
|
|
3157
|
|
3158 /* Arrange to read only the nonmatching middle part of the file. */
|
|
3159 beg = make_int (same_at_start - BUF_BEGV (buf));
|
|
3160 end = make_int (st.st_size - (BUF_ZV (buf) - same_at_end));
|
|
3161
|
|
3162 buffer_delete_range (buf, same_at_start, same_at_end,
|
|
3163 !NILP (visit) ? INSDEL_NO_LOCKING : 0);
|
|
3164 /* Insert from the file at the proper position. */
|
|
3165 BUF_SET_PT (buf, same_at_start);
|
|
3166 }
|
|
3167 #endif /* FSFMACS_SPEEDY_INSERT */
|
|
3168
|
|
3169 total = XINT (end) - XINT (beg);
|
|
3170
|
|
3171 if (XINT (beg) != 0
|
|
3172 #ifdef FSFMACS_SPEEDY_INSERT
|
|
3173 /* why was this here? asked jwz. The reason is that the replace-mode
|
|
3174 connivings above will normally put the file pointer other than
|
|
3175 where it should be. */
|
|
3176 || !NILP (replace)
|
|
3177 #endif /* !FSFMACS_SPEEDY_INSERT */
|
|
3178 )
|
|
3179 {
|
|
3180 if (lseek (fd, XINT (beg), 0) < 0)
|
|
3181 report_file_error ("Setting file position",
|
|
3182 Fcons (filename, Qnil));
|
|
3183 }
|
|
3184
|
|
3185 {
|
|
3186 Bufpos cur_point = BUF_PT (buf);
|
|
3187 struct gcpro ngcpro1;
|
|
3188 Lisp_Object stream = make_filedesc_input_stream (fd, 0, total,
|
|
3189 LSTR_ALLOW_QUIT);
|
|
3190
|
|
3191 NGCPRO1 (stream);
|
|
3192 Lstream_set_buffering (XLSTREAM (stream), LSTREAM_BLOCKN_BUFFERED, 65536);
|
|
3193
|
|
3194 record_unwind_protect (close_stream_unwind, stream);
|
|
3195
|
16
|
3196 /* No need to limit the amount of stuff we attempt to read. (It would
|
|
3197 be incorrect, anyway, when Mule is enabled.) Instead, the limiting
|
|
3198 occurs inside of the filedesc stream. */
|
0
|
3199 while (1)
|
|
3200 {
|
|
3201 Bytecount this_len;
|
|
3202 Charcount cc_inserted;
|
|
3203
|
|
3204 QUIT;
|
|
3205 this_len = Lstream_read (XLSTREAM (stream), read_buf,
|
|
3206 sizeof (read_buf));
|
|
3207
|
|
3208 if (this_len <= 0)
|
|
3209 {
|
|
3210 if (this_len < 0)
|
|
3211 saverrno = errno;
|
|
3212 break;
|
|
3213 }
|
|
3214
|
|
3215 cc_inserted = buffer_insert_raw_string_1 (buf, cur_point, read_buf,
|
|
3216 this_len,
|
|
3217 !NILP (visit)
|
|
3218 ? INSDEL_NO_LOCKING : 0);
|
|
3219 inserted += cc_inserted;
|
|
3220 cur_point += cc_inserted;
|
|
3221 }
|
|
3222 NUNGCPRO;
|
|
3223 }
|
|
3224
|
|
3225 #ifdef DOS_NT
|
|
3226 /* Determine file type from name and remove LFs from CR-LFs if the file
|
|
3227 is deemed to be a text file. */
|
|
3228 {
|
|
3229 struct gcpro gcpro1;
|
|
3230 GCPRO1 (filename);
|
|
3231 buf->buffer_file_type
|
|
3232 = call1_in_buffer (buf, Qfind_buffer_file_type, filename);
|
|
3233 UNGCPRO;
|
|
3234 if (NILP (buf->buffer_file_type))
|
|
3235 {
|
|
3236 buffer_do_msdos_crlf_to_lf (buf, ####);
|
|
3237 }
|
|
3238 }
|
|
3239 #endif
|
|
3240
|
|
3241 /* Close the file/stream */
|
|
3242 unbind_to (speccount, Qnil);
|
|
3243
|
|
3244 if (saverrno != 0)
|
|
3245 {
|
|
3246 error ("IO error reading %s: %s",
|
16
|
3247 XSTRING_DATA (filename), strerror (saverrno));
|
0
|
3248 }
|
|
3249
|
|
3250 notfound:
|
|
3251 handled:
|
|
3252
|
|
3253 end_multiple_change (buf, mc_count);
|
|
3254
|
|
3255 if (!NILP (visit))
|
|
3256 {
|
|
3257 if (!EQ (buf->undo_list, Qt))
|
|
3258 buf->undo_list = Qnil;
|
|
3259 #ifdef APOLLO
|
16
|
3260 stat ((char *) XSTRING_DATA (filename), &st);
|
0
|
3261 #endif
|
|
3262 if (NILP (handler))
|
|
3263 {
|
|
3264 buf->modtime = st.st_mtime;
|
|
3265 buf->filename = filename;
|
|
3266 /* XEmacs addition: */
|
|
3267 /* This function used to be in C, ostensibly so that
|
|
3268 it could be called here. But that's just silly.
|
|
3269 There's no reason C code can't call out to Lisp
|
|
3270 code, and it's a lot cleaner this way. */
|
|
3271 if (!NILP (Ffboundp (Qcompute_buffer_file_truename)))
|
|
3272 call1 (Qcompute_buffer_file_truename, make_buffer (buf));
|
|
3273 }
|
|
3274 BUF_SAVE_MODIFF (buf) = BUF_MODIFF (buf);
|
|
3275 buf->auto_save_modified = BUF_MODIFF (buf);
|
|
3276 buf->save_length = make_int (BUF_SIZE (buf));
|
|
3277 #ifdef CLASH_DETECTION
|
|
3278 if (NILP (handler))
|
|
3279 {
|
|
3280 if (!NILP (buf->file_truename))
|
|
3281 unlock_file (buf->file_truename);
|
|
3282 unlock_file (filename);
|
|
3283 }
|
|
3284 #endif /* CLASH_DETECTION */
|
|
3285 if (not_regular)
|
|
3286 RETURN_UNGCPRO (Fsignal (Qfile_error,
|
|
3287 list2 (build_string ("not a regular file"),
|
|
3288 filename)));
|
|
3289
|
|
3290 /* If visiting nonexistent file, return nil. */
|
|
3291 if (buf->modtime == -1)
|
|
3292 report_file_error ("Opening input file",
|
|
3293 list1 (filename));
|
|
3294 }
|
|
3295
|
|
3296 /* Decode file format */
|
|
3297 if (inserted > 0)
|
|
3298 {
|
|
3299 Lisp_Object insval = call3 (Qformat_decode,
|
|
3300 Qnil, make_int (inserted), visit);
|
|
3301 CHECK_INT (insval);
|
|
3302 inserted = XINT (insval);
|
|
3303 }
|
|
3304
|
|
3305 if (inserted > 0)
|
|
3306 {
|
|
3307 Lisp_Object p = Vafter_insert_file_functions;
|
|
3308 struct gcpro ngcpro1;
|
|
3309
|
|
3310 NGCPRO1 (p);
|
|
3311 while (!NILP (p))
|
|
3312 {
|
|
3313 Lisp_Object insval =
|
|
3314 call1 (Fcar (p), make_int (inserted));
|
|
3315 if (!NILP (insval))
|
|
3316 {
|
|
3317 CHECK_NATNUM (insval);
|
|
3318 inserted = XINT (insval);
|
|
3319 }
|
|
3320 QUIT;
|
|
3321 p = Fcdr (p);
|
|
3322 }
|
|
3323 NUNGCPRO;
|
|
3324 }
|
|
3325
|
|
3326 UNGCPRO;
|
|
3327
|
|
3328 if (!NILP (val))
|
|
3329 return (val);
|
|
3330 else
|
|
3331 return (list2 (filename, make_int (inserted)));
|
|
3332 }
|
|
3333
|
|
3334
|
|
3335 static int a_write (Lisp_Object outstream, Lisp_Object instream, int pos,
|
|
3336 Lisp_Object *annot);
|
|
3337 static Lisp_Object build_annotations (Lisp_Object start, Lisp_Object end);
|
|
3338
|
|
3339 /* If build_annotations switched buffers, switch back to BUF.
|
|
3340 Kill the temporary buffer that was selected in the meantime. */
|
|
3341
|
|
3342 static Lisp_Object
|
|
3343 build_annotations_unwind (Lisp_Object buf)
|
|
3344 {
|
|
3345 Lisp_Object tembuf;
|
|
3346
|
|
3347 if (XBUFFER (buf) == current_buffer)
|
|
3348 return Qnil;
|
|
3349 tembuf = Fcurrent_buffer ();
|
|
3350 Fset_buffer (buf);
|
|
3351 Fkill_buffer (tembuf);
|
|
3352 return Qnil;
|
|
3353 }
|
|
3354
|
|
3355 DEFUN ("write-region-internal", Fwrite_region_internal,
|
|
3356 Swrite_region_internal, 3, 6,
|
|
3357 "r\nFWrite region to file: " /*
|
|
3358 Write current region into specified file.
|
|
3359 When called from a program, takes three arguments:
|
|
3360 START, END and FILENAME. START and END are buffer positions.
|
|
3361 Optional fourth argument APPEND if non-nil means
|
|
3362 append to existing file contents (if any).
|
|
3363 Optional fifth argument VISIT if t means
|
|
3364 set the last-save-file-modtime of buffer to this file's modtime
|
|
3365 and mark buffer not modified.
|
|
3366 If VISIT is a string, it is a second file name;
|
|
3367 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
|
|
3368 VISIT is also the file name to lock and unlock for clash detection.
|
|
3369 If VISIT is neither t nor nil nor a string,
|
|
3370 that means do not print the \"Wrote file\" message.
|
|
3371 Kludgy feature: if START is a string, then that string is written
|
|
3372 to the file, instead of any buffer contents, and END is ignored.
|
|
3373 */ )
|
|
3374 (start, end, filename, append, visit, lockname)
|
|
3375 Lisp_Object start, end, filename, append, visit, lockname;
|
|
3376 {
|
|
3377 /* This function can GC */
|
|
3378 int desc;
|
|
3379 int failure;
|
|
3380 int save_errno = 0;
|
|
3381 struct stat st;
|
|
3382 Lisp_Object fn;
|
|
3383 int speccount = specpdl_depth ();
|
|
3384 #ifdef VMS
|
|
3385 unsigned char *fname = 0; /* If non-0, original filename (must rename) */
|
|
3386 #endif /* VMS */
|
|
3387 int visiting_other = STRINGP (visit);
|
|
3388 int visiting = (EQ (visit, Qt) || visiting_other);
|
|
3389 int quietly = (!visiting && !NILP (visit));
|
|
3390 Lisp_Object visit_file = Qnil;
|
|
3391 Lisp_Object annotations = Qnil;
|
|
3392 struct buffer *given_buffer;
|
|
3393 Bufpos start1, end1;
|
|
3394
|
|
3395 #ifdef DOS_NT
|
|
3396 int buffer_file_type
|
|
3397 = NILP (current_buffer->buffer_file_type) ? O_TEXT : O_BINARY;
|
|
3398 #endif /* DOS_NT */
|
|
3399
|
|
3400 if (current_buffer->base_buffer && ! NILP (visit))
|
|
3401 error ("Cannot do file visiting in an indirect buffer");
|
|
3402
|
|
3403 if (!NILP (start) && !STRINGP (start))
|
|
3404 get_buffer_range_char (current_buffer, start, end, &start1, &end1, 0);
|
|
3405
|
|
3406 {
|
|
3407 Lisp_Object handler;
|
|
3408 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
3409 GCPRO5 (start, filename, visit, visit_file, lockname);
|
|
3410
|
|
3411 if (visiting_other)
|
|
3412 visit_file = Fexpand_file_name (visit, Qnil);
|
|
3413 else
|
|
3414 visit_file = filename;
|
|
3415 filename = Fexpand_file_name (filename, Qnil);
|
|
3416
|
|
3417 UNGCPRO;
|
|
3418
|
|
3419 if (NILP (lockname))
|
|
3420 lockname = visit_file;
|
|
3421
|
|
3422 /* If the file name has special constructs in it,
|
|
3423 call the corresponding file handler. */
|
|
3424 handler = Ffind_file_name_handler (filename, Qwrite_region);
|
|
3425 /* If FILENAME has no handler, see if VISIT has one. */
|
|
3426 if (NILP (handler) && STRINGP (visit))
|
|
3427 handler = Ffind_file_name_handler (visit, Qwrite_region);
|
|
3428
|
|
3429 if (!NILP (handler))
|
|
3430 {
|
|
3431 Lisp_Object val = call6 (handler, Qwrite_region, start, end,
|
|
3432 filename, append, visit);
|
|
3433 if (visiting)
|
|
3434 {
|
|
3435 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
|
|
3436 current_buffer->save_length =
|
|
3437 make_int (BUF_SIZE (current_buffer));
|
|
3438 current_buffer->filename = visit_file;
|
|
3439 MARK_MODELINE_CHANGED;
|
|
3440 }
|
|
3441 return val;
|
|
3442 }
|
|
3443 }
|
|
3444
|
|
3445 #ifdef CLASH_DETECTION
|
|
3446 if (!auto_saving)
|
|
3447 {
|
|
3448 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
3449 GCPRO4 (start, filename, visit_file, lockname);
|
|
3450 lock_file (lockname);
|
|
3451 UNGCPRO;
|
|
3452 }
|
|
3453 #endif /* CLASH_DETECTION */
|
|
3454
|
|
3455 /* Special kludge to simplify auto-saving. */
|
|
3456 if (NILP (start))
|
|
3457 {
|
|
3458 start1 = BUF_BEG (current_buffer);
|
|
3459 end1 = BUF_Z (current_buffer);
|
|
3460 }
|
|
3461
|
|
3462 record_unwind_protect (build_annotations_unwind, Fcurrent_buffer ());
|
|
3463
|
|
3464 given_buffer = current_buffer;
|
|
3465 annotations = build_annotations (start, end);
|
|
3466 if (current_buffer != given_buffer)
|
|
3467 {
|
|
3468 start1 = BUF_BEGV (current_buffer);
|
|
3469 end1 = BUF_ZV (current_buffer);
|
|
3470 }
|
|
3471
|
|
3472 fn = filename;
|
|
3473 desc = -1;
|
|
3474 if (!NILP (append))
|
|
3475 #ifdef DOS_NT
|
16
|
3476 desc = open ((char *) XSTRING_DATA (fn),
|
0
|
3477 (O_WRONLY | buffer_file_type), 0);
|
|
3478 #else /* not DOS_NT */
|
16
|
3479 desc = open ((char *) XSTRING_DATA (fn), O_WRONLY, 0);
|
0
|
3480 #endif /* not DOS_NT */
|
|
3481
|
|
3482 if (desc < 0)
|
|
3483 #ifndef VMS
|
|
3484 {
|
|
3485 #ifdef DOS_NT
|
16
|
3486 desc = open ((char *) XSTRING_DATA (fn),
|
0
|
3487 (O_WRONLY | O_TRUNC | O_CREAT | buffer_file_type),
|
|
3488 (S_IREAD | S_IWRITE));
|
|
3489 #else /* not DOS_NT */
|
16
|
3490 desc = creat ((char *) XSTRING_DATA (fn),
|
0
|
3491 ((auto_saving) ? auto_save_mode_bits : 0666));
|
|
3492 #endif /* DOS_NT */
|
|
3493 }
|
|
3494 #else /* VMS */
|
|
3495 {
|
|
3496 if (auto_saving) /* Overwrite any previous version of autosave file */
|
|
3497 {
|
16
|
3498 char *fn_data = XSTRING_DATA (fn);
|
0
|
3499 /* if fn exists, truncate to zero length */
|
|
3500 vms_truncate (fn_data);
|
|
3501 desc = open (fn_data, O_RDWR, 0);
|
|
3502 if (desc < 0)
|
|
3503 desc = creat_copy_attrs ((STRINGP (current_buffer->filename)
|
16
|
3504 ? (char *)
|
|
3505 XSTRING_DATA (current_buffer->filename)
|
0
|
3506 : 0),
|
|
3507 fn_data);
|
|
3508 }
|
|
3509 else /* Write to temporary name and rename if no errors */
|
|
3510 {
|
|
3511 Lisp_Object temp_name;
|
|
3512
|
|
3513 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
3514 GCPRO3 (start, filename, visit_file);
|
|
3515 {
|
|
3516 struct gcpro gcpro1, gcpro2, gcpro3; /* Don't have GCPRO6 */
|
|
3517
|
|
3518 GCPRO3 (fn, fname, annotations);
|
|
3519
|
|
3520 temp_name = Ffile_name_directory (filename);
|
|
3521
|
|
3522 if (NILP (temp_name))
|
16
|
3523 desc = creat ((char *) XSTRING_DATA (fn), 0666);
|
0
|
3524 else
|
|
3525 {
|
|
3526 temp_name =
|
|
3527 Fmake_temp_name (concat2 (temp_name,
|
|
3528 build_string ("$$SAVE$$")));
|
|
3529 fname = filename;
|
|
3530 fn = temp_name;
|
|
3531 desc = creat_copy_attrs (fname,
|
16
|
3532 (char *) XSTRING_DATA (fn));
|
0
|
3533 if (desc < 0)
|
|
3534 {
|
|
3535 char *fn_data;
|
|
3536 /* If we can't open the temporary file, try creating a new
|
|
3537 version of the original file. VMS "creat" creates a
|
|
3538 new version rather than truncating an existing file. */
|
|
3539 fn = fname;
|
|
3540 fname = Qnil;
|
16
|
3541 fn_data = XSTRING_DATA (fn);
|
0
|
3542 desc = creat (fn_data, 0666);
|
|
3543 #if 0 /* This can clobber an existing file and fail
|
|
3544 to replace it, if the user runs out of
|
|
3545 space. */
|
|
3546 if (desc < 0)
|
|
3547 {
|
|
3548 /* We can't make a new version;
|
|
3549 try to truncate and rewrite existing version if any.
|
|
3550 */
|
|
3551 vms_truncate (fn_data);
|
|
3552 desc = open (fn_data, O_RDWR, 0);
|
|
3553 }
|
|
3554 #endif
|
|
3555 }
|
|
3556 }
|
|
3557 UNGCPRO;
|
|
3558 }
|
|
3559 UNGCPRO;
|
|
3560 }
|
|
3561 }
|
|
3562 #endif /* VMS */
|
|
3563
|
|
3564 if (desc < 0)
|
|
3565 {
|
|
3566 #ifdef CLASH_DETECTION
|
|
3567 save_errno = errno;
|
|
3568 if (!auto_saving) unlock_file (lockname);
|
|
3569 errno = save_errno;
|
|
3570 #endif /* CLASH_DETECTION */
|
|
3571 report_file_error ("Opening output file",
|
|
3572 Fcons (filename, Qnil));
|
|
3573 }
|
|
3574
|
|
3575 {
|
|
3576 Lisp_Object desc_locative = Fcons (make_int (desc), Qnil);
|
|
3577 Lisp_Object instream = Qnil, outstream = Qnil;
|
|
3578 struct gcpro gcpro1, gcpro2;
|
|
3579 /* need to gcpro; QUIT could happen out of call to write() */
|
|
3580 GCPRO2 (instream, outstream);
|
|
3581
|
|
3582 record_unwind_protect (close_file_unwind, desc_locative);
|
|
3583
|
|
3584 if (!NILP (append))
|
|
3585 {
|
|
3586 if (lseek (desc, 0, 2) < 0)
|
|
3587 {
|
|
3588 #ifdef CLASH_DETECTION
|
|
3589 if (!auto_saving) unlock_file (lockname);
|
|
3590 #endif /* CLASH_DETECTION */
|
|
3591 report_file_error ("Lseek error",
|
|
3592 list1 (filename));
|
|
3593 }
|
|
3594 }
|
|
3595
|
|
3596 #ifdef VMS
|
|
3597 /*
|
|
3598 * Kludge Warning: The VMS C RTL likes to insert carriage returns
|
|
3599 * if we do writes that don't end with a carriage return. Furthermore
|
|
3600 * it cannot handle writes of more then 16K. The modified
|
|
3601 * version of "sys_write" in SYSDEP.C (see comment there) copes with
|
|
3602 * this EXCEPT for the last record (iff it doesn't end with a carriage
|
|
3603 * return). This implies that if your buffer doesn't end with a carriage
|
|
3604 * return, you get one free... tough. However it also means that if
|
|
3605 * we make two calls to sys_write (a la the following code) you can
|
|
3606 * get one at the gap as well. The easiest way to fix this (honest)
|
|
3607 * is to move the gap to the next newline (or the end of the buffer).
|
|
3608 * Thus this change.
|
|
3609 *
|
|
3610 * Yech!
|
|
3611 */
|
|
3612 you lose -- fix this
|
|
3613 if (GPT > BUF_BEG (current_buffer) && *GPT_ADDR[-1] != '\n')
|
|
3614 move_gap (find_next_newline (current_buffer, GPT, 1));
|
|
3615 #endif
|
|
3616
|
|
3617 failure = 0;
|
|
3618
|
|
3619 /* Note: I tried increasing the buffering size, along with
|
|
3620 various other tricks, but nothing seemed to make much of
|
|
3621 a difference in the time it took to save a large file.
|
|
3622 (Actually that's not true. With a local disk, changing
|
|
3623 the buffer size doesn't seem to make much difference.
|
|
3624 With an NFS-mounted disk, it could make a lot of difference
|
|
3625 because you're affecting the number of network requests
|
|
3626 that need to be made, and there could be a large latency
|
|
3627 for each request. So I've increased the buffer size
|
|
3628 to 64K.) */
|
|
3629 outstream = make_filedesc_output_stream (desc, 0, -1, 0);
|
|
3630 Lstream_set_buffering (XLSTREAM (outstream),
|
|
3631 LSTREAM_BLOCKN_BUFFERED, 65536);
|
|
3632 if (STRINGP (start))
|
|
3633 {
|
|
3634 instream = make_lisp_string_input_stream (start, 0, -1);
|
|
3635 start1 = 0;
|
|
3636 }
|
|
3637 else
|
|
3638 instream = make_lisp_buffer_input_stream (current_buffer, start1, end1,
|
|
3639 LSTR_SELECTIVE |
|
|
3640 LSTR_IGNORE_ACCESSIBLE);
|
|
3641 failure = (0 > (a_write (outstream, instream, start1,
|
|
3642 &annotations)));
|
|
3643 save_errno = errno;
|
|
3644 /* Note that this doesn't close the desc since we created the
|
|
3645 stream without the LSTR_CLOSING flag, but it does
|
|
3646 flush out any buffered data. */
|
|
3647 if (Lstream_close (XLSTREAM (outstream)) < 0)
|
|
3648 {
|
|
3649 failure = 1;
|
|
3650 save_errno = errno;
|
|
3651 }
|
|
3652 Lstream_close (XLSTREAM (instream));
|
|
3653 UNGCPRO;
|
|
3654
|
|
3655 #ifdef HAVE_FSYNC
|
|
3656 /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
|
|
3657 Disk full in NFS may be reported here. */
|
|
3658 /* mib says that closing the file will try to write as fast as NFS can do
|
|
3659 it, and that means the fsync here is not crucial for autosave files. */
|
|
3660 if (!auto_saving && fsync (desc) < 0
|
|
3661 /* If fsync fails with EINTR, don't treat that as serious. */
|
|
3662 && errno != EINTR)
|
|
3663 {
|
|
3664 failure = 1;
|
|
3665 save_errno = errno;
|
|
3666 }
|
|
3667 #endif
|
|
3668
|
|
3669 /* Spurious "file has changed on disk" warnings have been
|
|
3670 observed on Suns as well.
|
|
3671 It seems that `close' can change the modtime, under nfs.
|
|
3672
|
|
3673 (This has supposedly been fixed in Sunos 4,
|
|
3674 but who knows about all the other machines with NFS?) */
|
|
3675 /* On VMS and APOLLO, must do the stat after the close
|
|
3676 since closing changes the modtime. */
|
|
3677 #if 0 /* !defined (VMS) && !defined (APOLLO) */
|
|
3678 fstat (desc, &st);
|
|
3679 #endif
|
|
3680
|
|
3681 /* NFS can report a write failure now. */
|
|
3682 if (close (desc) < 0)
|
|
3683 {
|
|
3684 failure = 1;
|
|
3685 save_errno = errno;
|
|
3686 }
|
|
3687
|
|
3688 /* Discard the close unwind-protect. Execute the one for
|
|
3689 build_annotations (switches back to the original current buffer
|
|
3690 as necessary). */
|
|
3691 XCAR (desc_locative) = Qnil;
|
|
3692 unbind_to (speccount, Qnil);
|
|
3693 }
|
|
3694
|
|
3695
|
|
3696 #ifdef VMS
|
|
3697 /* If we wrote to a temporary name and had no errors, rename to real name. */
|
|
3698 if (!NILP (fname))
|
|
3699 {
|
|
3700 if (!failure)
|
|
3701 {
|
16
|
3702 failure = (rename ((char *) XSTRING_DATA (fn),
|
|
3703 (char *) XSTRING_DATA (fname))
|
0
|
3704 != 0);
|
|
3705 save_errno = errno;
|
|
3706 }
|
|
3707 fn = fname;
|
|
3708 }
|
|
3709 #endif /* VMS */
|
|
3710
|
|
3711 #if 1 /* defined (VMS) || defined (APOLLO) */
|
16
|
3712 stat ((char *) XSTRING_DATA (fn), &st);
|
0
|
3713 #endif
|
|
3714
|
|
3715 #ifdef CLASH_DETECTION
|
|
3716 if (!auto_saving)
|
|
3717 unlock_file (lockname);
|
|
3718 #endif /* CLASH_DETECTION */
|
|
3719
|
|
3720 /* Do this before reporting IO error
|
|
3721 to avoid a "file has changed on disk" warning on
|
|
3722 next attempt to save. */
|
|
3723 if (visiting)
|
|
3724 current_buffer->modtime = st.st_mtime;
|
|
3725
|
|
3726 if (failure)
|
|
3727 error ("IO error writing %s: %s",
|
16
|
3728 XSTRING_DATA (fn),
|
0
|
3729 strerror (save_errno));
|
|
3730
|
|
3731 if (visiting)
|
|
3732 {
|
|
3733 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
|
|
3734 current_buffer->save_length = make_int (BUF_SIZE (current_buffer));
|
|
3735 current_buffer->filename = visit_file;
|
|
3736 MARK_MODELINE_CHANGED;
|
|
3737 }
|
|
3738 else if (quietly)
|
|
3739 {
|
|
3740 return Qnil;
|
|
3741 }
|
|
3742
|
|
3743 if (!auto_saving)
|
|
3744 {
|
|
3745 if (visiting_other)
|
16
|
3746 message ("Wrote %s", XSTRING_DATA (visit_file));
|
0
|
3747 else
|
|
3748 {
|
|
3749 struct gcpro gcpro1;
|
|
3750 Lisp_Object fsp;
|
|
3751 GCPRO1 (fn);
|
|
3752
|
|
3753 fsp = Ffile_symlink_p (fn);
|
|
3754 if (NILP (fsp))
|
16
|
3755 message ("Wrote %s", XSTRING_DATA (fn));
|
0
|
3756 else
|
|
3757 message ("Wrote %s (symlink to %s)",
|
16
|
3758 XSTRING_DATA (fn), XSTRING_DATA (fsp));
|
0
|
3759 UNGCPRO;
|
|
3760 }
|
|
3761 }
|
|
3762 return Qnil;
|
|
3763 }
|
|
3764
|
|
3765 /* #### This is such a load of shit!!!! There is no way we should define
|
|
3766 something so stupid as a subr, just sort the fucking list more
|
|
3767 intelligently. */
|
|
3768 DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0 /*
|
|
3769 Return t if (car A) is numerically less than (car B).
|
|
3770 */ )
|
|
3771 (a, b)
|
|
3772 Lisp_Object a, b;
|
|
3773 {
|
|
3774 return Flss (Fcar (a), Fcar (b));
|
|
3775 }
|
|
3776
|
|
3777 /* Heh heh heh, let's define this too, just to aggravate the person who
|
|
3778 wrote the above comment. */
|
|
3779 DEFUN ("cdr-less-than-cdr", Fcdr_less_than_cdr, Scdr_less_than_cdr, 2, 2, 0 /*
|
|
3780 Return t if (cdr A) is numerically less than (cdr B).
|
|
3781 */ )
|
|
3782 (a, b)
|
|
3783 Lisp_Object a, b;
|
|
3784 {
|
|
3785 return Flss (Fcdr (a), Fcdr (b));
|
|
3786 }
|
|
3787
|
|
3788 /* Build the complete list of annotations appropriate for writing out
|
|
3789 the text between START and END, by calling all the functions in
|
|
3790 write-region-annotate-functions and merging the lists they return.
|
|
3791 If one of these functions switches to a different buffer, we assume
|
|
3792 that buffer contains altered text. Therefore, the caller must
|
|
3793 make sure to restore the current buffer in all cases,
|
|
3794 as save-excursion would do. */
|
|
3795
|
|
3796 static Lisp_Object
|
|
3797 build_annotations (Lisp_Object start, Lisp_Object end)
|
|
3798 {
|
|
3799 /* This function can GC */
|
|
3800 Lisp_Object annotations;
|
|
3801 Lisp_Object p, res;
|
|
3802 struct gcpro gcpro1, gcpro2;
|
|
3803
|
|
3804 annotations = Qnil;
|
|
3805 p = Vwrite_region_annotate_functions;
|
|
3806 GCPRO2 (annotations, p);
|
|
3807 while (!NILP (p))
|
|
3808 {
|
|
3809 struct buffer *given_buffer = current_buffer;
|
|
3810 Vwrite_region_annotations_so_far = annotations;
|
|
3811 res = call2 (Fcar (p), start, end);
|
|
3812 /* If the function makes a different buffer current,
|
|
3813 assume that means this buffer contains altered text to be output.
|
|
3814 Reset START and END from the buffer bounds
|
|
3815 and discard all previous annotations because they should have
|
|
3816 been dealt with by this function. */
|
|
3817 if (current_buffer != given_buffer)
|
|
3818 {
|
|
3819 start = make_int (BUF_BEGV (current_buffer));
|
|
3820 end = make_int (BUF_ZV (current_buffer));
|
|
3821 annotations = Qnil;
|
|
3822 }
|
|
3823 (void) Flength (res); /* Check basic validity of return value */
|
|
3824 annotations = merge (annotations, res, Qcar_less_than_car);
|
|
3825 p = Fcdr (p);
|
|
3826 }
|
|
3827
|
|
3828 /* Now do the same for annotation functions implied by the file-format */
|
|
3829 if (auto_saving && (!EQ (Vauto_save_file_format, Qt)))
|
|
3830 p = Vauto_save_file_format;
|
|
3831 else
|
|
3832 p = current_buffer->file_format;
|
|
3833 while (!NILP (p))
|
|
3834 {
|
|
3835 struct buffer *given_buffer = current_buffer;
|
|
3836 Vwrite_region_annotations_so_far = annotations;
|
|
3837 res = call3 (Qformat_annotate_function, Fcar (p), start, end);
|
|
3838 if (current_buffer != given_buffer)
|
|
3839 {
|
|
3840 start = make_int (BUF_BEGV (current_buffer));
|
|
3841 end = make_int (BUF_ZV (current_buffer));
|
|
3842 annotations = Qnil;
|
|
3843 }
|
|
3844 (void) Flength (res);
|
|
3845 annotations = merge (annotations, res, Qcar_less_than_car);
|
|
3846 p = Fcdr (p);
|
|
3847 }
|
|
3848 UNGCPRO;
|
|
3849 return annotations;
|
|
3850 }
|
|
3851
|
|
3852 /* Write to stream OUTSTREAM the characters from INSTREAM (it is read until
|
|
3853 EOF is encountered), assuming they start at position POS in the buffer
|
|
3854 of string that STREAM refers to. Intersperse with them the annotations
|
|
3855 from *ANNOT that fall into the range of positions we are reading from,
|
|
3856 each at its appropriate position.
|
|
3857
|
|
3858 Modify *ANNOT by discarding elements as we output them.
|
|
3859 The return value is negative in case of system call failure. */
|
|
3860
|
|
3861 /* 4K should probably be fine. We just need to reduce the number of
|
|
3862 function calls to reasonable level. The Lstream stuff itself will
|
|
3863 batch to 64K to reduce the number of system calls. */
|
|
3864
|
|
3865 #define A_WRITE_BATCH_SIZE 4096
|
|
3866
|
|
3867 static int
|
|
3868 a_write (Lisp_Object outstream, Lisp_Object instream, int pos,
|
|
3869 Lisp_Object *annot)
|
|
3870 {
|
|
3871 Lisp_Object tem;
|
|
3872 int nextpos;
|
|
3873 unsigned char largebuf[A_WRITE_BATCH_SIZE];
|
|
3874 Lstream *instr = XLSTREAM (instream);
|
|
3875 Lstream *outstr = XLSTREAM (outstream);
|
|
3876
|
|
3877 while (NILP (*annot) || CONSP (*annot))
|
|
3878 {
|
|
3879 tem = Fcar_safe (Fcar (*annot));
|
|
3880 if (INTP (tem))
|
|
3881 nextpos = XINT (tem);
|
|
3882 else
|
|
3883 nextpos = INT_MAX;
|
|
3884 {
|
|
3885 while (pos != nextpos)
|
|
3886 {
|
|
3887 /* Otherwise there is no point to that. Just go in batches. */
|
|
3888 int chunk = min (nextpos - pos, A_WRITE_BATCH_SIZE);
|
|
3889
|
|
3890 chunk = Lstream_read (instr, largebuf, chunk);
|
|
3891 if (chunk < 0)
|
|
3892 return -1;
|
|
3893 if (chunk == 0) /* EOF */
|
|
3894 break;
|
|
3895 if (Lstream_write (outstr, largebuf, chunk) < chunk)
|
|
3896 return -1;
|
|
3897 pos += chunk;
|
|
3898 }
|
|
3899 }
|
|
3900 if (pos == nextpos)
|
|
3901 {
|
|
3902 tem = Fcdr (Fcar (*annot));
|
|
3903 if (STRINGP (tem))
|
|
3904 {
|
16
|
3905 if (Lstream_write (outstr, XSTRING_DATA (tem),
|
|
3906 XSTRING_LENGTH (tem)) < 0)
|
0
|
3907 return -1;
|
|
3908 }
|
|
3909 *annot = Fcdr (*annot);
|
|
3910 }
|
|
3911 else
|
|
3912 return 0;
|
|
3913 }
|
|
3914 return -1;
|
|
3915 }
|
|
3916
|
|
3917
|
|
3918
|
|
3919 #if 0
|
|
3920 #include <des_crypt.h>
|
|
3921
|
|
3922 #define CRYPT_BLOCK_SIZE 8 /* bytes */
|
|
3923 #define CRYPT_KEY_SIZE 8 /* bytes */
|
|
3924
|
|
3925 DEFUN ("encrypt-string", Fencrypt_string, Sencrypt_string, 2, 2, 0 /*
|
|
3926 Encrypt STRING using KEY.
|
|
3927 */ )
|
|
3928 (string, key)
|
|
3929 Lisp_Object string, key;
|
|
3930 {
|
|
3931 char *encrypted_string, *raw_key;
|
|
3932 int rounded_size, extra, key_size;
|
|
3933
|
|
3934 /* !!#### May produce bogus data under Mule. */
|
|
3935 CHECK_STRING (string);
|
|
3936 CHECK_STRING (key);
|
|
3937
|
16
|
3938 extra = XSTRING_LENGTH (string) % CRYPT_BLOCK_SIZE;
|
|
3939 rounded_size = XSTRING_LENGTH (string) + extra;
|
0
|
3940 encrypted_string = alloca (rounded_size + 1);
|
16
|
3941 memcpy (encrypted_string, XSTRING_DATA (string), XSTRING_LENGTH (string));
|
0
|
3942 memset (encrypted_string + rounded_size - extra, 0, extra + 1);
|
|
3943
|
16
|
3944 if (XSTRING_LENGTH (key) > CRYPT_KEY_SIZE)
|
0
|
3945 key_size = CRYPT_KEY_SIZE;
|
|
3946 else
|
16
|
3947 key_size = XSTRING_LENGTH (key);
|
0
|
3948
|
|
3949 raw_key = alloca (CRYPT_KEY_SIZE + 1);
|
16
|
3950 memcpy (raw_key, XSTRING_DATA (key), key_size);
|
0
|
3951 memset (raw_key + key_size, 0, (CRYPT_KEY_SIZE + 1) - key_size);
|
|
3952
|
|
3953 (void) ecb_crypt (raw_key, encrypted_string, rounded_size,
|
|
3954 DES_ENCRYPT | DES_SW);
|
|
3955 return make_string (encrypted_string, rounded_size);
|
|
3956 }
|
|
3957
|
|
3958 DEFUN ("decrypt-string", Fdecrypt_string, Sdecrypt_string, 2, 2, 0 /*
|
|
3959 Decrypt STRING using KEY.
|
|
3960 */ )
|
|
3961 (string, key)
|
|
3962 Lisp_Object string, key;
|
|
3963 {
|
|
3964 char *decrypted_string, *raw_key;
|
|
3965 int string_size, key_size;
|
|
3966
|
|
3967 CHECK_STRING (string);
|
|
3968 CHECK_STRING (key);
|
|
3969
|
16
|
3970 string_size = XSTRING_LENGTH (string) + 1;
|
0
|
3971 decrypted_string = alloca (string_size);
|
16
|
3972 memcpy (decrypted_string, XSTRING_DATA (string), string_size);
|
0
|
3973 decrypted_string[string_size - 1] = '\0';
|
|
3974
|
16
|
3975 if (XSTRING_LENGTH (key) > CRYPT_KEY_SIZE)
|
0
|
3976 key_size = CRYPT_KEY_SIZE;
|
|
3977 else
|
16
|
3978 key_size = XSTRING_LENGTH (key);
|
0
|
3979
|
|
3980 raw_key = alloca (CRYPT_KEY_SIZE + 1);
|
16
|
3981 memcpy (raw_key, XSTRING_DATA (key), key_size);
|
0
|
3982 memset (raw_key + key_size, 0, (CRYPT_KEY_SIZE + 1) - key_size);
|
|
3983
|
|
3984
|
|
3985 (void) ecb_crypt (raw_key, decrypted_string, string_size,
|
|
3986 DES_DECRYPT | DES_SW);
|
|
3987 return make_string (decrypted_string, string_size - 1);
|
|
3988 }
|
|
3989 #endif
|
|
3990
|
|
3991
|
|
3992 DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime,
|
|
3993 Sverify_visited_file_modtime, 1, 1, 0 /*
|
|
3994 Return t if last mod time of BUF's visited file matches what BUF records.
|
|
3995 This means that the file has not been changed since it was visited or saved.
|
|
3996 */ )
|
|
3997 (buf)
|
|
3998 Lisp_Object buf;
|
|
3999 {
|
|
4000 /* This function can GC */
|
|
4001 struct buffer *b;
|
|
4002 struct stat st;
|
|
4003 Lisp_Object handler;
|
|
4004
|
|
4005 CHECK_BUFFER (buf);
|
|
4006 b = XBUFFER (buf);
|
|
4007
|
|
4008 if (!STRINGP (b->filename)) return Qt;
|
|
4009 if (b->modtime == 0) return Qt;
|
|
4010
|
|
4011 /* If the file name has special constructs in it,
|
|
4012 call the corresponding file handler. */
|
|
4013 handler = Ffind_file_name_handler (b->filename,
|
|
4014 Qverify_visited_file_modtime);
|
|
4015 if (!NILP (handler))
|
|
4016 return call2 (handler, Qverify_visited_file_modtime, buf);
|
|
4017
|
16
|
4018 if (stat ((char *) XSTRING_DATA (b->filename), &st) < 0)
|
0
|
4019 {
|
|
4020 /* If the file doesn't exist now and didn't exist before,
|
|
4021 we say that it isn't modified, provided the error is a tame one. */
|
|
4022 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
|
|
4023 st.st_mtime = -1;
|
|
4024 else
|
|
4025 st.st_mtime = 0;
|
|
4026 }
|
|
4027 if (st.st_mtime == b->modtime
|
|
4028 /* If both are positive, accept them if they are off by one second. */
|
|
4029 || (st.st_mtime > 0 && b->modtime > 0
|
|
4030 && (st.st_mtime == b->modtime + 1
|
|
4031 || st.st_mtime == b->modtime - 1)))
|
|
4032 return Qt;
|
|
4033 return Qnil;
|
|
4034 }
|
|
4035
|
|
4036 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime,
|
|
4037 Sclear_visited_file_modtime, 0, 0, 0 /*
|
|
4038 Clear out records of last mod time of visited file.
|
|
4039 Next attempt to save will certainly not complain of a discrepancy.
|
|
4040 */ )
|
|
4041 ()
|
|
4042 {
|
|
4043 current_buffer->modtime = 0;
|
|
4044 return Qnil;
|
|
4045 }
|
|
4046
|
|
4047 DEFUN ("visited-file-modtime", Fvisited_file_modtime,
|
|
4048 Svisited_file_modtime, 0, 0, 0 /*
|
|
4049 Return the current buffer's recorded visited file modification time.
|
|
4050 The value is a list of the form (HIGH . LOW), like the time values
|
|
4051 that `file-attributes' returns.
|
|
4052 */ )
|
|
4053 ()
|
|
4054 {
|
|
4055 return time_to_lisp ((time_t) current_buffer->modtime);
|
|
4056 }
|
|
4057
|
|
4058 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime,
|
|
4059 Sset_visited_file_modtime, 0, 1, 0 /*
|
|
4060 Update buffer's recorded modification time from the visited file's time.
|
|
4061 Useful if the buffer was not read from the file normally
|
|
4062 or if the file itself has been changed for some known benign reason.
|
|
4063 An argument specifies the modification time value to use
|
|
4064 (instead of that of the visited file), in the form of a list
|
|
4065 (HIGH . LOW) or (HIGH LOW).
|
|
4066 */ )
|
|
4067 (time_list)
|
|
4068 Lisp_Object time_list;
|
|
4069 {
|
|
4070 /* This function can GC */
|
|
4071 if (!NILP (time_list))
|
|
4072 {
|
|
4073 time_t the_time;
|
|
4074 lisp_to_time (time_list, &the_time);
|
|
4075 current_buffer->modtime = (int) the_time;
|
|
4076 }
|
|
4077 else
|
|
4078 {
|
|
4079 Lisp_Object filename;
|
|
4080 struct stat st;
|
|
4081 Lisp_Object handler;
|
|
4082 struct gcpro gcpro1, gcpro2;
|
|
4083
|
|
4084 GCPRO2 (filename, time_list);
|
|
4085 filename = Fexpand_file_name (current_buffer->filename,
|
|
4086 Qnil);
|
|
4087
|
|
4088 /* If the file name has special constructs in it,
|
|
4089 call the corresponding file handler. */
|
|
4090 handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime);
|
|
4091 UNGCPRO;
|
|
4092 if (!NILP (handler))
|
|
4093 /* The handler can find the file name the same way we did. */
|
|
4094 return call2 (handler, Qset_visited_file_modtime, Qnil);
|
16
|
4095 else if (stat ((char *) XSTRING_DATA (filename), &st) >= 0)
|
0
|
4096 current_buffer->modtime = st.st_mtime;
|
|
4097 }
|
|
4098
|
|
4099 return Qnil;
|
|
4100 }
|
|
4101
|
|
4102 DEFUN ("set-buffer-modtime", Fset_buffer_modtime,
|
|
4103 Sset_buffer_modtime, 1, 2, 0 /*
|
|
4104 Update BUFFER's recorded modification time from the associated
|
|
4105 file's modtime, if there is an associated file. If not, use the
|
|
4106 current time. In either case, if the optional arg TIME is supplied,
|
|
4107 it will be used if it is either an integer or a cons of two integers.
|
|
4108 */ )
|
|
4109 (buf, in_time)
|
|
4110 Lisp_Object buf, in_time;
|
|
4111 {
|
|
4112 /* This function can GC */
|
|
4113 unsigned long time_to_use = 0;
|
|
4114 int set_time_to_use = 0;
|
|
4115 struct stat st;
|
|
4116
|
|
4117 CHECK_BUFFER (buf);
|
|
4118
|
|
4119 if (!NILP (in_time))
|
|
4120 {
|
|
4121 if (INTP (in_time))
|
|
4122 {
|
|
4123 time_to_use = XINT (in_time);
|
|
4124 set_time_to_use = 1;
|
|
4125 }
|
|
4126 else if ((CONSP (in_time)) &&
|
|
4127 (INTP (Fcar (in_time))) &&
|
|
4128 (INTP (Fcdr (in_time))))
|
|
4129 {
|
|
4130 time_t the_time;
|
|
4131 lisp_to_time (in_time, &the_time);
|
|
4132 time_to_use = (unsigned long) the_time;
|
|
4133 set_time_to_use = 1;
|
|
4134 }
|
|
4135 }
|
|
4136
|
|
4137 if (!set_time_to_use)
|
|
4138 {
|
|
4139 Lisp_Object filename = Qnil;
|
|
4140 struct gcpro gcpro1, gcpro2;
|
|
4141 GCPRO2 (buf, filename);
|
|
4142
|
|
4143 if (STRINGP (XBUFFER (buf)->filename))
|
|
4144 filename = Fexpand_file_name (XBUFFER (buf)->filename,
|
|
4145 Qnil);
|
|
4146 else
|
|
4147 filename = Qnil;
|
|
4148
|
|
4149 UNGCPRO;
|
|
4150
|
|
4151 if (!NILP (filename) && !NILP (Ffile_exists_p (filename)))
|
|
4152 {
|
|
4153 Lisp_Object handler;
|
|
4154
|
|
4155 /* If the file name has special constructs in it,
|
|
4156 call the corresponding file handler. */
|
|
4157 GCPRO1 (filename);
|
|
4158 handler = Ffind_file_name_handler (filename, Qset_buffer_modtime);
|
|
4159 UNGCPRO;
|
|
4160 if (!NILP (handler))
|
|
4161 /* The handler can find the file name the same way we did. */
|
|
4162 return (call2 (handler, Qset_buffer_modtime, Qnil));
|
|
4163 else
|
|
4164 {
|
16
|
4165 if (stat ((char *) XSTRING_DATA (filename), &st) >= 0)
|
0
|
4166 time_to_use = st.st_mtime;
|
|
4167 else
|
|
4168 time_to_use = time ((time_t *) 0);
|
|
4169 }
|
|
4170 }
|
|
4171 else
|
|
4172 time_to_use = time ((time_t *) 0);
|
|
4173 }
|
|
4174
|
|
4175 XBUFFER (buf)->modtime = time_to_use;
|
|
4176
|
|
4177 return Qnil;
|
|
4178 }
|
|
4179
|
|
4180
|
|
4181 static Lisp_Object
|
|
4182 auto_save_error (Lisp_Object condition_object, Lisp_Object ignored)
|
|
4183 {
|
|
4184 /* This function can GC */
|
|
4185 if (gc_in_progress)
|
|
4186 return Qnil;
|
|
4187 clear_echo_area (selected_frame (), Qauto_saving, 1);
|
|
4188 Fding (Qt, Qauto_save_error, Qnil);
|
16
|
4189 message ("Auto-saving...error for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4190 Fsleep_for (make_int (1));
|
16
|
4191 message ("Auto-saving...error!for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4192 Fsleep_for (make_int (1));
|
16
|
4193 message ("Auto-saving...error for %s", XSTRING_DATA (current_buffer->name));
|
0
|
4194 Fsleep_for (make_int (1));
|
|
4195 return Qnil;
|
|
4196 }
|
|
4197
|
|
4198 static Lisp_Object
|
|
4199 auto_save_1 (Lisp_Object ignored)
|
|
4200 {
|
|
4201 /* This function can GC */
|
|
4202 struct stat st;
|
|
4203 Lisp_Object fn = current_buffer->filename;
|
|
4204 Lisp_Object a = current_buffer->auto_save_file_name;
|
|
4205
|
|
4206 if (!STRINGP (a))
|
|
4207 return (Qnil);
|
|
4208
|
|
4209 /* Get visited file's mode to become the auto save file's mode. */
|
|
4210 if (STRINGP (fn) &&
|
16
|
4211 stat ((char *) XSTRING_DATA (fn), &st) >= 0)
|
0
|
4212 /* But make sure we can overwrite it later! */
|
|
4213 auto_save_mode_bits = st.st_mode | 0600;
|
|
4214 else
|
|
4215 /* default mode for auto-save files of buffers with no file is
|
|
4216 readable by owner only. This may annoy some small number of
|
|
4217 people, but the alternative removes all privacy from email. */
|
|
4218 auto_save_mode_bits = 0600;
|
|
4219
|
|
4220 return
|
|
4221 Fwrite_region_internal (Qnil, Qnil, a, Qnil, Qlambda, Qnil);
|
|
4222 }
|
|
4223
|
|
4224
|
|
4225 static Lisp_Object
|
|
4226 do_auto_save_unwind (Lisp_Object fd)
|
|
4227 {
|
|
4228 close (XINT (fd));
|
|
4229 return (fd);
|
|
4230 }
|
|
4231
|
|
4232 static Lisp_Object
|
|
4233 do_auto_save_unwind_2 (Lisp_Object old_auto_saving)
|
|
4234 {
|
|
4235 auto_saving = XINT (old_auto_saving);
|
|
4236 return Qnil;
|
|
4237 }
|
|
4238
|
|
4239 /* Fdo_auto_save() checks whether a GC is in progress when it is called,
|
|
4240 and if so, tries to avoid touching lisp objects.
|
|
4241
|
|
4242 The only time that Fdo_auto_save() is called while GC is in progress
|
|
4243 is if we're going down, as a result of an abort() or a kill signal.
|
|
4244 It's fairly important that we generate autosave files in that case!
|
|
4245 */
|
|
4246
|
|
4247 DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "" /*
|
|
4248 Auto-save all buffers that need it.
|
|
4249 This is all buffers that have auto-saving enabled
|
|
4250 and are changed since last auto-saved.
|
|
4251 Auto-saving writes the buffer into a file
|
|
4252 so that your editing is not lost if the system crashes.
|
|
4253 This file is not the file you visited; that changes only when you save.
|
|
4254 Normally we run the normal hook `auto-save-hook' before saving.
|
|
4255
|
|
4256 Non-nil first argument means do not print any message if successful.
|
|
4257 Non-nil second argument means save only current buffer.
|
|
4258 */ )
|
|
4259 (no_message, current_only)
|
|
4260 Lisp_Object no_message, current_only;
|
|
4261 {
|
|
4262 /* This function can GC */
|
|
4263 struct buffer *old = current_buffer, *b;
|
|
4264 Lisp_Object tail, buf;
|
|
4265 int auto_saved = 0;
|
|
4266 int do_handled_files;
|
|
4267 Lisp_Object oquit = Qnil;
|
|
4268 Lisp_Object listfile = Qnil;
|
|
4269 int listdesc = -1;
|
|
4270 int speccount = specpdl_depth ();
|
|
4271 struct gcpro gcpro1, gcpro2;
|
|
4272
|
|
4273 GCPRO2 (oquit, listfile);
|
|
4274 check_quit (); /* make Vquit_flag accurate */
|
|
4275 /* Ordinarily don't quit within this function,
|
|
4276 but don't make it impossible to quit (in case we get hung in I/O). */
|
|
4277 oquit = Vquit_flag;
|
|
4278 Vquit_flag = Qnil;
|
|
4279
|
|
4280 /* No further GCPRO needed, because (when it matters) all Lisp_Object
|
|
4281 variables point to non-strings reached from Vbuffer_alist. */
|
|
4282
|
|
4283 if (minibuf_level != 0 || preparing_for_armageddon)
|
|
4284 no_message = Qt;
|
|
4285
|
|
4286 run_hook (Qauto_save_hook);
|
|
4287
|
|
4288 if (GC_STRINGP (Vauto_save_list_file_name))
|
|
4289 listfile = Fexpand_file_name (Vauto_save_list_file_name, Qnil);
|
|
4290
|
|
4291 /* Make sure auto_saving is reset. */
|
|
4292 record_unwind_protect (do_auto_save_unwind_2, make_int (auto_saving));
|
|
4293
|
|
4294 auto_saving = 1;
|
|
4295
|
|
4296 /* First, save all files which don't have handlers. If Emacs is
|
|
4297 crashing, the handlers may tweak what is causing Emacs to crash
|
|
4298 in the first place, and it would be a shame if Emacs failed to
|
|
4299 autosave perfectly ordinary files because it couldn't handle some
|
|
4300 ange-ftp'd file. */
|
|
4301 for (do_handled_files = 0; do_handled_files < 2; do_handled_files++)
|
|
4302 {
|
|
4303 for (tail = Vbuffer_alist;
|
|
4304 GC_CONSP (tail);
|
|
4305 tail = XCDR (tail))
|
|
4306 {
|
|
4307 buf = XCDR (XCAR (tail));
|
|
4308 b = XBUFFER (buf);
|
|
4309
|
|
4310 if (!GC_NILP (current_only)
|
|
4311 && b != current_buffer)
|
|
4312 continue;
|
|
4313
|
|
4314 /* Don't auto-save indirect buffers.
|
|
4315 The base buffer takes care of it. */
|
|
4316 if (b->base_buffer)
|
|
4317 continue;
|
|
4318
|
|
4319 /* Check for auto save enabled
|
|
4320 and file changed since last auto save
|
|
4321 and file changed since last real save. */
|
|
4322 if (GC_STRINGP (b->auto_save_file_name)
|
|
4323 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
|
|
4324 && b->auto_save_modified < BUF_MODIFF (b)
|
|
4325 /* -1 means we've turned off autosaving for a while--see below. */
|
|
4326 && XINT (b->save_length) >= 0
|
|
4327 && (do_handled_files
|
|
4328 || NILP (Ffind_file_name_handler (b->auto_save_file_name,
|
|
4329 Qwrite_region))))
|
|
4330 {
|
|
4331 EMACS_TIME before_time, after_time;
|
|
4332
|
|
4333 EMACS_GET_TIME (before_time);
|
|
4334 /* If we had a failure, don't try again for 20 minutes. */
|
|
4335 if (!preparing_for_armageddon
|
|
4336 && b->auto_save_failure_time >= 0
|
|
4337 && (EMACS_SECS (before_time) - b->auto_save_failure_time <
|
|
4338 1200))
|
|
4339 continue;
|
|
4340
|
|
4341 if (!preparing_for_armageddon &&
|
|
4342 (XINT (b->save_length) * 10
|
|
4343 > (BUF_Z (b) - BUF_BEG (b)) * 13)
|
|
4344 /* A short file is likely to change a large fraction;
|
|
4345 spare the user annoying messages. */
|
|
4346 && XINT (b->save_length) > 5000
|
|
4347 /* These messages are frequent and annoying for `*mail*'. */
|
|
4348 && !EQ (b->filename, Qnil)
|
|
4349 && NILP (no_message)
|
|
4350 && disable_auto_save_when_buffer_shrinks)
|
|
4351 {
|
|
4352 /* It has shrunk too much; turn off auto-saving here.
|
|
4353 Unless we're about to crash, in which case auto-save it
|
|
4354 anyway.
|
|
4355 */
|
|
4356 message
|
|
4357 ("Buffer %s has shrunk a lot; auto save turned off there",
|
16
|
4358 XSTRING_DATA (b->name));
|
0
|
4359 /* Turn off auto-saving until there's a real save,
|
|
4360 and prevent any more warnings. */
|
|
4361 b->save_length = make_int (-1);
|
|
4362 if (!gc_in_progress)
|
|
4363 Fsleep_for (make_int (1));
|
|
4364 continue;
|
|
4365 }
|
|
4366 set_buffer_internal (b);
|
|
4367 if (!auto_saved && GC_NILP (no_message))
|
|
4368 {
|
|
4369 static CONST unsigned char *msg
|
|
4370 = (CONST unsigned char *) "Auto-saving...";
|
|
4371 echo_area_message (selected_frame (), msg, Qnil,
|
|
4372 0, strlen ((CONST char *) msg),
|
|
4373 Qauto_saving);
|
|
4374 }
|
|
4375
|
|
4376 /* Open the auto-save list file, if necessary.
|
|
4377 We only do this now so that the file only exists
|
|
4378 if we actually auto-saved any files. */
|
|
4379 if (!auto_saved && GC_STRINGP (listfile) && listdesc < 0)
|
|
4380 {
|
|
4381 #ifdef DOS_NT
|
16
|
4382 listdesc = open ((char *) XSTRING_DATA (listfile),
|
0
|
4383 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
|
|
4384 S_IREAD | S_IWRITE);
|
|
4385 #else /* not DOS_NT */
|
16
|
4386 listdesc = creat ((char *) XSTRING_DATA (listfile), 0666);
|
0
|
4387 #endif /* not DOS_NT */
|
|
4388
|
|
4389 /* Arrange to close that file whether or not we get
|
|
4390 an error. */
|
|
4391 if (listdesc >= 0)
|
|
4392 record_unwind_protect (do_auto_save_unwind,
|
|
4393 make_int (listdesc));
|
|
4394 }
|
|
4395
|
|
4396 /* Record all the buffers that we are auto-saving in
|
|
4397 the special file that lists them. For each of
|
|
4398 these buffers, record visited name (if any) and
|
|
4399 auto save name. */
|
|
4400 if (listdesc >= 0)
|
|
4401 {
|
|
4402 Extbyte *auto_save_file_name_ext;
|
|
4403 Extcount auto_save_file_name_ext_len;
|
|
4404
|
|
4405 GET_STRING_FILENAME_DATA_ALLOCA
|
|
4406 (b->auto_save_file_name,
|
|
4407 auto_save_file_name_ext,
|
|
4408 auto_save_file_name_ext_len);
|
|
4409 if (!NILP (b->filename))
|
|
4410 {
|
|
4411 Extbyte *filename_ext;
|
|
4412 Extcount filename_ext_len;
|
|
4413
|
|
4414 GET_STRING_FILENAME_DATA_ALLOCA (b->filename,
|
|
4415 filename_ext,
|
|
4416 filename_ext_len);
|
|
4417 write (listdesc, filename_ext, filename_ext_len);
|
|
4418 }
|
|
4419 write (listdesc, "\n", 1);
|
|
4420 write (listdesc, auto_save_file_name_ext,
|
|
4421 auto_save_file_name_ext_len);
|
|
4422 write (listdesc, "\n", 1);
|
|
4423 }
|
|
4424
|
|
4425 condition_case_1 (Qt,
|
|
4426 auto_save_1, Qnil,
|
|
4427 auto_save_error, Qnil);
|
|
4428 auto_saved++;
|
|
4429 b->auto_save_modified = BUF_MODIFF (b);
|
|
4430 b->save_length = make_int (BUF_SIZE (b));
|
|
4431 set_buffer_internal (old);
|
|
4432
|
|
4433 EMACS_GET_TIME (after_time);
|
|
4434 /* If auto-save took more than 60 seconds,
|
|
4435 assume it was an NFS failure that got a timeout. */
|
|
4436 if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60)
|
|
4437 b->auto_save_failure_time = EMACS_SECS (after_time);
|
|
4438 }
|
|
4439 }
|
|
4440 }
|
|
4441
|
|
4442 /* Prevent another auto save till enough input events come in. */
|
|
4443 if (auto_saved)
|
|
4444 record_auto_save ();
|
|
4445
|
|
4446 /* If we didn't save anything into the listfile, remove the old
|
|
4447 one because nothing needed to be auto-saved. Do this afterwards
|
|
4448 rather than before in case we get a crash attempting to autosave
|
|
4449 (in that case we'd still want the old one around). */
|
|
4450 if (listdesc < 0 && !auto_saved && GC_STRINGP (listfile))
|
16
|
4451 unlink ((char *) XSTRING_DATA (listfile));
|
0
|
4452
|
|
4453 /* Show "...done" only if the echo area would otherwise be empty. */
|
|
4454 if (auto_saved && NILP (no_message)
|
|
4455 && NILP (clear_echo_area (selected_frame (), Qauto_saving, 0)))
|
|
4456 {
|
|
4457 static CONST unsigned char *msg
|
|
4458 = (CONST unsigned char *)"Auto-saving...done";
|
|
4459 echo_area_message (selected_frame (), msg, Qnil, 0,
|
|
4460 strlen ((CONST char *) msg), Qauto_saving);
|
|
4461 }
|
|
4462
|
|
4463 Vquit_flag = oquit;
|
|
4464
|
|
4465 RETURN_UNGCPRO (unbind_to (speccount, Qnil));
|
|
4466 }
|
|
4467
|
|
4468 DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved,
|
|
4469 Sset_buffer_auto_saved, 0, 0, 0 /*
|
|
4470 Mark current buffer as auto-saved with its current text.
|
|
4471 No auto-save file will be written until the buffer changes again.
|
|
4472 */ )
|
|
4473 ()
|
|
4474 {
|
|
4475 current_buffer->auto_save_modified = BUF_MODIFF (current_buffer);
|
|
4476 current_buffer->save_length = make_int (BUF_SIZE (current_buffer));
|
|
4477 current_buffer->auto_save_failure_time = -1;
|
|
4478 return Qnil;
|
|
4479 }
|
|
4480
|
|
4481 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure,
|
|
4482 Sclear_buffer_auto_save_failure, 0, 0, 0 /*
|
|
4483 Clear any record of a recent auto-save failure in the current buffer.
|
|
4484 */ )
|
|
4485 ()
|
|
4486 {
|
|
4487 current_buffer->auto_save_failure_time = -1;
|
|
4488 return Qnil;
|
|
4489 }
|
|
4490
|
|
4491 DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p,
|
|
4492 0, 0, 0 /*
|
|
4493 Return t if buffer has been auto-saved since last read in or saved.
|
|
4494 */ )
|
|
4495 ()
|
|
4496 {
|
|
4497 return (BUF_SAVE_MODIFF (current_buffer) <
|
|
4498 current_buffer->auto_save_modified) ? Qt : Qnil;
|
|
4499 }
|
|
4500
|
|
4501
|
|
4502 /************************************************************************/
|
|
4503 /* initialization */
|
|
4504 /************************************************************************/
|
|
4505
|
|
4506 void
|
|
4507 syms_of_fileio (void)
|
|
4508 {
|
|
4509 defsymbol (&Qexpand_file_name, "expand-file-name");
|
|
4510 defsymbol (&Qfile_truename, "file-truename");
|
|
4511 defsymbol (&Qsubstitute_in_file_name, "substitute-in-file-name");
|
|
4512 defsymbol (&Qdirectory_file_name, "directory-file-name");
|
|
4513 defsymbol (&Qfile_name_directory, "file-name-directory");
|
|
4514 defsymbol (&Qfile_name_nondirectory, "file-name-nondirectory");
|
|
4515 defsymbol (&Qunhandled_file_name_directory, "unhandled-file-name-directory");
|
|
4516 defsymbol (&Qfile_name_as_directory, "file-name-as-directory");
|
|
4517 defsymbol (&Qcopy_file, "copy-file");
|
|
4518 defsymbol (&Qmake_directory_internal, "make-directory-internal");
|
|
4519 defsymbol (&Qdelete_directory, "delete-directory");
|
|
4520 defsymbol (&Qdelete_file, "delete-file");
|
|
4521 defsymbol (&Qrename_file, "rename-file");
|
|
4522 defsymbol (&Qadd_name_to_file, "add-name-to-file");
|
|
4523 defsymbol (&Qmake_symbolic_link, "make-symbolic-link");
|
|
4524 defsymbol (&Qfile_exists_p, "file-exists-p");
|
|
4525 defsymbol (&Qfile_executable_p, "file-executable-p");
|
|
4526 defsymbol (&Qfile_readable_p, "file-readable-p");
|
|
4527 defsymbol (&Qfile_symlink_p, "file-symlink-p");
|
|
4528 defsymbol (&Qfile_writable_p, "file-writable-p");
|
|
4529 defsymbol (&Qfile_directory_p, "file-directory-p");
|
|
4530 defsymbol (&Qfile_regular_p, "file-regular-p");
|
|
4531 defsymbol (&Qfile_accessible_directory_p, "file-accessible-directory-p");
|
|
4532 defsymbol (&Qfile_modes, "file-modes");
|
|
4533 defsymbol (&Qset_file_modes, "set-file-modes");
|
|
4534 defsymbol (&Qfile_newer_than_file_p, "file-newer-than-file-p");
|
|
4535 defsymbol (&Qinsert_file_contents, "insert-file-contents");
|
|
4536 defsymbol (&Qwrite_region, "write-region");
|
|
4537 defsymbol (&Qverify_visited_file_modtime, "verify-visited-file-modtime");
|
|
4538 defsymbol (&Qset_visited_file_modtime, "set-visited-file-modtime");
|
|
4539 defsymbol (&Qset_buffer_modtime, "set-buffer-modtime");
|
|
4540 #ifdef DOS_NT
|
|
4541 defsymbol (&Qfind_buffer_file_type, "find-buffer-file-type");
|
|
4542 #endif /* DOS_NT */
|
|
4543 defsymbol (&Qcar_less_than_car, "car-less-than-car"); /* Vomitous! */
|
|
4544
|
|
4545 defsymbol (&Qfile_name_handler_alist, "file-name-handler-alist");
|
|
4546 defsymbol (&Qauto_save_hook, "auto-save-hook");
|
|
4547 defsymbol (&Qauto_save_error, "auto-save-error");
|
|
4548 defsymbol (&Qauto_saving, "auto-saving");
|
|
4549
|
|
4550 defsymbol (&Qformat_decode, "format-decode");
|
|
4551 defsymbol (&Qformat_annotate_function, "format-annotate-function");
|
|
4552
|
|
4553 defsymbol (&Qcompute_buffer_file_truename, "compute-buffer-file-truename");
|
|
4554 deferror (&Qfile_error, "file-error", "File error", Qio_error);
|
|
4555 deferror (&Qfile_already_exists, "file-already-exists",
|
|
4556 "File already exists", Qfile_error);
|
|
4557
|
|
4558 defsubr (&Sfind_file_name_handler);
|
|
4559
|
|
4560 defsubr (&Sfile_name_directory);
|
|
4561 defsubr (&Sfile_name_nondirectory);
|
|
4562 defsubr (&Sunhandled_file_name_directory);
|
|
4563 defsubr (&Sfile_name_as_directory);
|
|
4564 defsubr (&Sdirectory_file_name);
|
|
4565 defsubr (&Smake_temp_name);
|
|
4566 defsubr (&Sexpand_file_name);
|
|
4567 defsubr (&Sfile_truename);
|
|
4568 defsubr (&Ssubstitute_in_file_name);
|
|
4569 defsubr (&Scopy_file);
|
|
4570 defsubr (&Smake_directory_internal);
|
|
4571 defsubr (&Sdelete_directory);
|
|
4572 defsubr (&Sdelete_file);
|
|
4573 defsubr (&Srename_file);
|
|
4574 defsubr (&Sadd_name_to_file);
|
|
4575 #ifdef S_IFLNK
|
|
4576 defsubr (&Smake_symbolic_link);
|
|
4577 #endif /* S_IFLNK */
|
|
4578 #ifdef VMS
|
|
4579 defsubr (&Sdefine_logical_name);
|
|
4580 #endif /* VMS */
|
|
4581 #ifdef HPUX_NET
|
|
4582 defsubr (&Ssysnetunam);
|
|
4583 #endif /* HPUX_NET */
|
|
4584 defsubr (&Sfile_name_absolute_p);
|
|
4585 defsubr (&Sfile_exists_p);
|
|
4586 defsubr (&Sfile_executable_p);
|
|
4587 defsubr (&Sfile_readable_p);
|
|
4588 defsubr (&Sfile_writable_p);
|
|
4589 defsubr (&Sfile_symlink_p);
|
|
4590 defsubr (&Sfile_directory_p);
|
|
4591 defsubr (&Sfile_accessible_directory_p);
|
|
4592 defsubr (&Sfile_regular_p);
|
|
4593 defsubr (&Sfile_modes);
|
|
4594 defsubr (&Sset_file_modes);
|
|
4595 defsubr (&Sset_default_file_modes);
|
|
4596 defsubr (&Sdefault_file_modes);
|
|
4597 defsubr (&Sunix_sync);
|
|
4598 defsubr (&Sfile_newer_than_file_p);
|
|
4599 defsubr (&Sinsert_file_contents_internal);
|
|
4600 defsubr (&Swrite_region_internal);
|
|
4601 defsubr (&Scar_less_than_car); /* Vomitous! */
|
|
4602 defsubr (&Scdr_less_than_cdr); /* Yeah oh yeah bucko .... */
|
|
4603 #if 0
|
|
4604 defsubr (&Sencrypt_string);
|
|
4605 defsubr (&Sdecrypt_string);
|
|
4606 #endif
|
|
4607 defsubr (&Sverify_visited_file_modtime);
|
|
4608 defsubr (&Sclear_visited_file_modtime);
|
|
4609 defsubr (&Svisited_file_modtime);
|
|
4610 defsubr (&Sset_visited_file_modtime);
|
|
4611 defsubr (&Sset_buffer_modtime);
|
|
4612
|
|
4613 defsubr (&Sdo_auto_save);
|
|
4614 defsubr (&Sset_buffer_auto_saved);
|
|
4615 defsubr (&Sclear_buffer_auto_save_failure);
|
|
4616 defsubr (&Srecent_auto_save_p);
|
|
4617 }
|
|
4618
|
|
4619 void
|
|
4620 vars_of_fileio (void)
|
|
4621 {
|
|
4622 DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format /*
|
|
4623 *Format in which to write auto-save files.
|
|
4624 Should be a list of symbols naming formats that are defined in `format-alist'.
|
|
4625 If it is t, which is the default, auto-save files are written in the
|
|
4626 same format as a regular save would use.
|
|
4627 */ );
|
|
4628 Vauto_save_file_format = Qt;
|
|
4629
|
|
4630 DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm /*
|
|
4631 *Non-nil means write new files with record format `stmlf'.
|
|
4632 nil means use format `var'. This variable is meaningful only on VMS.
|
|
4633 */ );
|
|
4634 vms_stmlf_recfm = 0;
|
|
4635
|
|
4636 DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist /*
|
|
4637 *Alist of elements (REGEXP . HANDLER) for file names handled specially.
|
|
4638 If a file name matches REGEXP, then all I/O on that file is done by calling
|
|
4639 HANDLER.
|
|
4640
|
|
4641 The first argument given to HANDLER is the name of the I/O primitive
|
|
4642 to be handled; the remaining arguments are the arguments that were
|
|
4643 passed to that primitive. For example, if you do
|
|
4644 (file-exists-p FILENAME)
|
|
4645 and FILENAME is handled by HANDLER, then HANDLER is called like this:
|
|
4646 (funcall HANDLER 'file-exists-p FILENAME)
|
|
4647 The function `find-file-name-handler' checks this list for a handler
|
|
4648 for its argument.
|
|
4649 */ );
|
|
4650 Vfile_name_handler_alist = Qnil;
|
|
4651
|
|
4652 DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions /*
|
|
4653 A list of functions to be called at the end of `insert-file-contents'.
|
|
4654 Each is passed one argument, the number of bytes inserted. It should return
|
|
4655 the new byte count, and leave point the same. If `insert-file-contents' is
|
|
4656 intercepted by a handler from `file-name-handler-alist', that handler is
|
|
4657 responsible for calling the after-insert-file-functions if appropriate.
|
|
4658 */ );
|
|
4659 Vafter_insert_file_functions = Qnil;
|
|
4660
|
|
4661 DEFVAR_LISP ("write-region-annotate-functions",
|
|
4662 &Vwrite_region_annotate_functions /*
|
|
4663 A list of functions to be called at the start of `write-region'.
|
|
4664 Each is passed two arguments, START and END as for `write-region'.
|
|
4665 It should return a list of pairs (POSITION . STRING) of strings to be
|
|
4666 effectively inserted at the specified positions of the file being written
|
|
4667 \(1 means to insert before the first byte written). The POSITIONs must be
|
|
4668 sorted into increasing order. If there are several functions in the list,
|
|
4669 the several lists are merged destructively.
|
|
4670 */ );
|
|
4671 Vwrite_region_annotate_functions = Qnil;
|
|
4672
|
|
4673 DEFVAR_LISP ("write-region-annotations-so-far",
|
|
4674 &Vwrite_region_annotations_so_far /*
|
|
4675 When an annotation function is called, this holds the previous annotations.
|
|
4676 These are the annotations made by other annotation functions
|
|
4677 that were already called. See also `write-region-annotate-functions'.
|
|
4678 */ );
|
|
4679 Vwrite_region_annotations_so_far = Qnil;
|
|
4680
|
|
4681 DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers /*
|
|
4682 A list of file name handlers that temporarily should not be used.
|
|
4683 This applies only to the operation `inhibit-file-name-operation'.
|
|
4684 */ );
|
|
4685 Vinhibit_file_name_handlers = Qnil;
|
|
4686
|
|
4687 DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation /*
|
|
4688 The operation for which `inhibit-file-name-handlers' is applicable.
|
|
4689 */ );
|
|
4690 Vinhibit_file_name_operation = Qnil;
|
|
4691
|
|
4692 DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name /*
|
|
4693 File name in which we write a list of all auto save file names.
|
|
4694 */ );
|
|
4695 Vauto_save_list_file_name = Qnil;
|
|
4696
|
|
4697 DEFVAR_BOOL ("disable-auto-save-when-buffer-shrinks",
|
|
4698 &disable_auto_save_when_buffer_shrinks /*
|
|
4699 If non-nil, auto-saving is disabled when a buffer shrinks too much.
|
|
4700 This is to prevent you from losing your edits if you accidentally
|
|
4701 delete a large chunk of the buffer and don't notice it until too late.
|
|
4702 Saving the buffer normally turns auto-save back on.
|
|
4703 */ );
|
|
4704 disable_auto_save_when_buffer_shrinks = 1;
|
|
4705 }
|