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