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