annotate src/bufwrite.c @ 28678:a16dae0be398 v8.2.4863

patch 8.2.4863: accessing freed memory in test without the +channel feature Commit: https://github.com/vim/vim/commit/c9af617ac62b15bfcbbfe8c54071146e2af01f65 Author: Bram Moolenaar <Bram@vim.org> Date: Wed May 4 16:46:54 2022 +0100 patch 8.2.4863: accessing freed memory in test without the +channel feature Problem: Accessing freed memory in test without the +channel feature. (Dominique Pell?) Solution: Do not generted PUSHCHANNEL or PUSHJOB if they are not implemented. (closes #10350)
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 May 2022 18:00:04 +0200
parents 7f150a4936f2
children d0241e74bfdb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * bufwrite.c: functions for writing a buffer
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #include "vim.h"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 # include <utime.h> // for struct utimbuf
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 #define SMALLBUFSIZE 256 // size of emergency write buffer
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 * Structure to pass arguments from buf_write() to buf_write_bytes().
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 struct bw_info
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 int bw_fd; // file descriptor
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 char_u *bw_buf; // buffer with data to be written
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 int bw_len; // length of data
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 int bw_flags; // FIO_ flags
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 buf_T *bw_buffer; // buffer being written
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
33 int bw_finish; // finish encrypting
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 char_u bw_rest[CONV_RESTLEN]; // not converted bytes
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 int bw_restlen; // nr of bytes in bw_rest[]
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 int bw_first; // first write call
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 char_u *bw_conv_buf; // buffer for writing converted chars
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 size_t bw_conv_buflen; // size of bw_conv_buf
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 int bw_conv_error; // set for conversion error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 linenr_T bw_conv_error_lnum; // first line with error or zero
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 linenr_T bw_start_lnum; // line number at start of buffer
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 iconv_t bw_iconv_fd; // descriptor for iconv() or -1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 };
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 * Convert a Unicode character to bytes.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 * Return TRUE for an error, FALSE when it's OK.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 ucs2bytes(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 unsigned c, // in: character
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 char_u **pp, // in/out: pointer to result
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 int flags) // FIO_ flags
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 char_u *p = *pp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 int error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 int cc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 if (flags & FIO_UCS4)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 if (flags & FIO_ENDIAN_L)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 *p++ = (c >> 16);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 *p++ = (c >> 24);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 *p++ = (c >> 24);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 *p++ = (c >> 16);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 else if (flags & (FIO_UCS2 | FIO_UTF16))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 if (c >= 0x10000)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 if (flags & FIO_UTF16)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 // Make two words, ten bits of the character in each. First
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 // word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 c -= 0x10000;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 if (c >= 0x100000)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 cc = ((c >> 10) & 0x3ff) + 0xd800;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 if (flags & FIO_ENDIAN_L)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 *p++ = cc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 *p++ = ((unsigned)cc >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 *p++ = ((unsigned)cc >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 *p++ = cc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 c = (c & 0x3ff) + 0xdc00;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 if (flags & FIO_ENDIAN_L)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 else // Latin1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 if (c >= 0x100)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 *p++ = 0xBF;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 *pp = p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 return error;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 * Call write() to write a number of bytes to the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 * Handles encryption and 'encoding' conversion.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 * Return FAIL for failure, OK otherwise.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 buf_write_bytes(struct bw_info *ip)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 int wlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 char_u *buf = ip->bw_buf; // data to write
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 int len = ip->bw_len; // length of data
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 int flags = ip->bw_flags; // extra flags
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 // Skip conversion when writing the crypt magic number or the BOM.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 if (!(flags & FIO_NOCONVERT))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 unsigned c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 int n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 if (flags & FIO_UTF8)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 // Convert latin1 in the buffer to UTF-8 in the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 p = ip->bw_conv_buf; // translate to buffer
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 for (wlen = 0; wlen < len; ++wlen)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 p += utf_char2bytes(buf[wlen], p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 buf = ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 len = (int)(p - ip->bw_conv_buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 // Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 // Latin1 chars in the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 if (flags & FIO_LATIN1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 p = buf; // translate in-place (can only get shorter)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 p = ip->bw_conv_buf; // translate to buffer
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 for (wlen = 0; wlen < len; wlen += n)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 if (wlen == 0 && ip->bw_restlen != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 int l;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 // Use remainder of previous call. Append the start of
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 // buf[] to get a full sequence. Might still be too
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 // short!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 l = CONV_RESTLEN - ip->bw_restlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 if (l > len)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 l = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 if (n > ip->bw_restlen + len)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 // We have an incomplete byte sequence at the end to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 // be written. We can't convert it without the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 // remaining bytes. Keep them for the next call.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 if (ip->bw_restlen + len > CONV_RESTLEN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 ip->bw_restlen += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 if (n > 1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 c = utf_ptr2char(ip->bw_rest);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 c = ip->bw_rest[0];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199 if (n >= ip->bw_restlen)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 n -= ip->bw_restlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202 ip->bw_restlen = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 ip->bw_restlen -= n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 mch_memmove(ip->bw_rest, ip->bw_rest + n,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 (size_t)ip->bw_restlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 n = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 n = utf_ptr2len_len(buf + wlen, len - wlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 if (n > len - wlen)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 // We have an incomplete byte sequence at the end to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218 // be written. We can't convert it without the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 // remaining bytes. Keep them for the next call.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 if (len - wlen > CONV_RESTLEN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 ip->bw_restlen = len - wlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 mch_memmove(ip->bw_rest, buf + wlen,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 (size_t)ip->bw_restlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 if (n > 1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 c = utf_ptr2char(buf + wlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 c = buf[wlen];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236 ip->bw_conv_error_lnum = ip->bw_start_lnum;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 if (c == NL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 ++ip->bw_start_lnum;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 if (flags & FIO_LATIN1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 len = (int)(p - buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 buf = ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 len = (int)(p - ip->bw_conv_buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 #ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 else if (flags & FIO_CODEPAGE)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 // Convert UTF-8 or codepage to UCS-2 and then to MS-Windows
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 // codepage.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 char_u *from;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 size_t fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 char_u *to;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 int u8c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 BOOL bad = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 int needed;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 if (ip->bw_restlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 // Need to concatenate the remainder of the previous call and
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265 // the bytes of the current call. Use the end of the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 // conversion buffer for this.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 fromlen = len + ip->bw_restlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
269 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 from = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 fromlen = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278 to = ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 if (enc_utf8)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 // Convert from UTF-8 to UCS-2, to the start of the buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282 // The buffer has been allocated to be big enough.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 while (fromlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 n = (int)utf_ptr2len_len(from, (int)fromlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 if (n > (int)fromlen) // incomplete byte sequence
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288 u8c = utf_ptr2char(from);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 *to++ = (u8c & 0xff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 *to++ = (u8c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 fromlen -= n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 from += n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 // Copy remainder to ip->bw_rest[] to be used for the next
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 // call.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 if (fromlen > CONV_RESTLEN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299 // weird overlong sequence
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
303 mch_memmove(ip->bw_rest, from, fromlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 ip->bw_restlen = (int)fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
307 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 // Convert from enc_codepage to UCS-2, to the start of the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
309 // buffer. The buffer has been allocated to be big enough.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 ip->bw_restlen = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 needed = MultiByteToWideChar(enc_codepage,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 NULL, 0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 if (needed == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316 // When conversion fails there may be a trailing byte.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 needed = MultiByteToWideChar(enc_codepage,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319 NULL, 0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 if (needed == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 // Conversion doesn't work.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
323 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326 // Save the trailing byte for the next call.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 ip->bw_rest[0] = from[fromlen - 1];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 ip->bw_restlen = 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
329 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 (LPCSTR)from, (int)(fromlen - ip->bw_restlen),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332 (LPWSTR)to, needed);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 if (needed == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 // Safety check: Conversion doesn't work.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
336 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 to += needed * 2;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
341
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342 fromlen = to - ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 buf = to;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344 # ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345 if (FIO_GET_CP(flags) == CP_UTF8)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 // Convert from UCS-2 to UTF-8, using the remainder of the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 // conversion buffer. Fails when out of space.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 u8c = *from++;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 u8c += (*from++ << 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 to += utf_char2bytes(u8c, to);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
359 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 len = (int)(to - buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 // Convert from UCS-2 to the codepage, using the remainder of
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
366 // the conversion buffer. If the conversion uses the default
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
367 // character "0", the data doesn't fit in this encoding, so
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368 // fail.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
370 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 &bad);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 if (bad)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
374 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
375 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
377 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
378 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
379 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 #ifdef MACOS_CONVERT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
383 else if (flags & FIO_MACROMAN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385 // Convert UTF-8 or latin1 to Apple MacRoman.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 char_u *from;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387 size_t fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
389 if (ip->bw_restlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
391 // Need to concatenate the remainder of the previous call and
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392 // the bytes of the current call. Use the end of the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 // conversion buffer for this.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394 fromlen = len + ip->bw_restlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
395 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
397 mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
398 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 from = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 fromlen = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
404
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
405 if (enc2macroman(from, fromlen,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406 ip->bw_conv_buf, &len, ip->bw_conv_buflen,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407 ip->bw_rest, &ip->bw_restlen) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
408 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
409 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
412 buf = ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
413 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
415
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
417 if (ip->bw_iconv_fd != (iconv_t)-1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 const char *from;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 size_t fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 char *to;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422 size_t tolen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 // Convert with iconv().
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 if (ip->bw_restlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 char *fp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429 // Need to concatenate the remainder of the previous call and
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 // the bytes of the current call. Use the end of the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 // conversion buffer for this.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432 fromlen = len + ip->bw_restlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
436 from = fp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
437 tolen = ip->bw_conv_buflen - fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441 from = (const char *)buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 fromlen = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443 tolen = ip->bw_conv_buflen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
444 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 to = (char *)ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 if (ip->bw_first)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449 size_t save_len = tolen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451 // output the initial shift state sequence
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
454 // There is a bug in iconv() on Linux (which appears to be
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
455 // wide-spread) which sets "to" to NULL and messes up "tolen".
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 if (to == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
458 to = (char *)ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 tolen = save_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
461 ip->bw_first = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
463
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
464 // If iconv() has an error or there is not enough room, fail.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
465 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
467 || fromlen > CONV_RESTLEN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
468 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
469 ip->bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
471 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
472
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
473 // copy remainder to ip->bw_rest[] to be used for the next call.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474 if (fromlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
475 mch_memmove(ip->bw_rest, (void *)from, fromlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 ip->bw_restlen = (int)fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
478 buf = ip->bw_conv_buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479 len = (int)((char_u *)to - ip->bw_conv_buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
482 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
483
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
484 if (ip->bw_fd < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
485 // Only checking conversion, which is OK if we get here.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
487
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489 if (flags & FIO_ENCRYPTED)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
491 // Encrypt the data. Do it in-place if possible, otherwise use an
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492 // allocated buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493 # ifdef CRYPT_NOT_INPLACE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
494 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
495 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
496 # endif
25052
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
497 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len,
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
498 ip->bw_finish);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
499 # ifdef CRYPT_NOT_INPLACE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
501 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
502 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
503 char_u *outbuf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504
25052
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
505 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf,
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
506 ip->bw_finish);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507 if (len == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 return OK; // Crypt layer is buffering, will flush later.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509 wlen = write_eintr(ip->bw_fd, outbuf, len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 vim_free(outbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 return (wlen < len) ? FAIL : OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517 wlen = write_eintr(ip->bw_fd, buf, len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 return (wlen < len) ? FAIL : OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
522 * Check modification time of file, before writing to it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 * The size isn't checked, because using a tool like "gzip" takes care of
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 * using the same timestamp but can't set the size.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
525 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 check_mtime(buf_T *buf, stat_T *st)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529 if (buf->b_mtime_read != 0
25953
d7e1cf30728c patch 8.2.3510: changes are only detected with one second accuracy
Bram Moolenaar <Bram@vim.org>
parents: 25362
diff changeset
530 && time_differs(st, buf->b_mtime_read, buf->b_mtime_read_ns))
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
531 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
532 msg_scroll = TRUE; // don't overwrite messages here
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 msg_silent = 0; // must give this prompt
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534 // don't use emsg() here, don't want to flush the buffers
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
535 msg_attr(_("WARNING: The file has been changed since reading it!!!"),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
536 HL_ATTR(HLF_E));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
537 if (ask_yesno((char_u *)_("Do you really want to write to it"),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538 TRUE) == 'n')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 msg_scroll = FALSE; // always overwrite the file message now
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
544
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
545 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
546 * Generate a BOM in "buf[4]" for encoding "name".
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
547 * Return the length of the BOM (zero when no BOM).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
548 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
549 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
550 make_bom(char_u *buf, char_u *name)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
552 int flags;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
553 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555 flags = get_fio_flags(name);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557 // Can't put a BOM in a non-Unicode file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 if (flags == FIO_LATIN1 || flags == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
559 return 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
561 if (flags == FIO_UTF8) // UTF-8
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563 buf[0] = 0xef;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
564 buf[1] = 0xbb;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
565 buf[2] = 0xbf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
566 return 3;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
567 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568 p = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569 (void)ucs2bytes(0xfeff, &p, flags);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570 return (int)(p - buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
572
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
573 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
574 static void
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
575 set_file_time(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
576 char_u *fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
577 time_t atime, // access time
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
578 time_t mtime) // modification time
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
579 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
580 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
581 struct utimbuf buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
582
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
583 buf.actime = atime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
584 buf.modtime = mtime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
585 (void)utime((char *)fname, &buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
586 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587 # if defined(HAVE_UTIMES)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
588 struct timeval tvp[2];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
589
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
590 tvp[0].tv_sec = atime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
591 tvp[0].tv_usec = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592 tvp[1].tv_sec = mtime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
593 tvp[1].tv_usec = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 # ifdef NeXT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
595 (void)utimes((char *)fname, tvp);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
596 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
597 (void)utimes((char *)fname, (const struct timeval *)&tvp);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
598 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
600 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
601 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
602 #endif // UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
603
20828
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
604 char *
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
605 new_file_message(void)
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
606 {
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
607 return shortmess(SHM_NEW) ? _("[New]") : _("[New File]");
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
608 }
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
609
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
610 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
611 * buf_write() - write to file "fname" lines "start" through "end"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
612 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
613 * We do our own buffering here because fwrite() is so slow.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
614 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
615 * If "forceit" is true, we don't care for errors when attempting backups.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
616 * In case of an error everything possible is done to restore the original
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
617 * file. But when "forceit" is TRUE, we risk losing it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
618 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
619 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
620 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
621 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
622 * This function must NOT use NameBuff (because it's called by autowrite()).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
623 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
624 * return FAIL for failure, OK otherwise
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
625 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
626 int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
627 buf_write(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
628 buf_T *buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
629 char_u *fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
630 char_u *sfname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
631 linenr_T start,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
632 linenr_T end,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
633 exarg_T *eap, // for forced 'ff' and 'fenc', can be
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
634 // NULL!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
635 int append, // append to the file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
636 int forceit,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
637 int reset_changed,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
638 int filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
639 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
640 int fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
641 char_u *backup = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
642 int backup_copy = FALSE; // copy the original file?
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
643 int dobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
644 char_u *ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
645 char_u *wfname = NULL; // name of file to write to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
646 char_u *s;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
647 char_u *ptr;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
648 char_u c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
649 int len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
650 linenr_T lnum;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
651 long nchars;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 char_u *errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
653 int errmsg_allocated = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
654 char_u *errnum = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
655 char_u *buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
656 char_u smallbuf[SMALLBUFSIZE];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
657 char_u *backup_ext;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
658 int bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
659 long perm; // file permissions
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660 int retval = OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
661 int newfile = FALSE; // TRUE if file doesn't exist yet
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
662 int msg_save = msg_scroll;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
663 int overwriting; // TRUE if writing over original
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
664 int no_eol = FALSE; // no end-of-line written
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
665 int device = FALSE; // writing to a device
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
666 stat_T st_old;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
667 int prev_got_int = got_int;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
668 int checking_conversion;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669 int file_readonly = FALSE; // overwritten file is read-only
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
670 #if defined(UNIX) // XXX fix me sometime?
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
671 int made_writable = FALSE; // 'w' bit has been set
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
672 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
673 // writing everything
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
674 int whole = (start == 1 && end == buf->b_ml.ml_line_count);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
675 linenr_T old_line_count = buf->b_ml.ml_line_count;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
676 int attr;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
677 int fileformat;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
678 int write_bin;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
679 struct bw_info write_info; // info for buf_write_bytes()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
680 int converted = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
681 int notconverted = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
682 char_u *fenc; // effective 'fileencoding'
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
683 char_u *fenc_tofree = NULL; // allocated "fenc"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
684 int wb_flags = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
685 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
686 vim_acl_T acl = NULL; // ACL copied from original file to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
687 // backup or new file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
688 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
689 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
690 int write_undo_file = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
691 context_sha256_T sha_ctx;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
692 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
693 unsigned int bkc = get_bkc_value(buf);
18619
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
694 pos_T orig_start = buf->b_op_start;
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
695 pos_T orig_end = buf->b_op_end;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
696
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
697 if (fname == NULL || *fname == NUL) // safety check
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
698 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
699 if (buf->b_ml.ml_mfp == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
700 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
701 // This can happen during startup when there is a stray "w" in the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
702 // vimrc file.
26883
7f150a4936f2 patch 8.2.3970: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26877
diff changeset
703 emsg(_(e_empty_buffer));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
704 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
705 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
706
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
707 // Disallow writing from .exrc and .vimrc in current directory for
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
708 // security reasons.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
709 if (check_secure())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
710 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
711
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
712 // Avoid a crash for a long name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
713 if (STRLEN(fname) >= MAXPATHL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
714 {
26436
ef0c07cbf53f patch 8.2.3749: error messages are everywhere
Bram Moolenaar <Bram@vim.org>
parents: 26336
diff changeset
715 emsg(_(e_name_too_long));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
716 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
717 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
718
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
719 // must init bw_conv_buf and bw_iconv_fd before jumping to "fail"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
720 write_info.bw_conv_buf = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
721 write_info.bw_conv_error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
722 write_info.bw_conv_error_lnum = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
723 write_info.bw_restlen = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
724 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
725 write_info.bw_iconv_fd = (iconv_t)-1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
726 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
727 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
728 write_info.bw_buffer = buf;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
729 write_info.bw_finish = FALSE;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
730 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
731
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
732 // After writing a file changedtick changes but we don't want to display
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
733 // the line.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
734 ex_no_reprint = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
735
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
736 // If there is no file name yet, use the one for the written file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
737 // BF_NOTEDITED is set to reflect this (in case the write fails).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
738 // Don't do this when the write is for a filter command.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
739 // Don't do this when appending.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
740 // Only do this when 'cpoptions' contains the 'F' flag.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
741 if (buf->b_ffname == NULL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
742 && reset_changed
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
743 && whole
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
744 && buf == curbuf
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
745 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746 && !bt_nofilename(buf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
748 && !filtering
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
749 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
750 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
751 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
752 if (set_rw_fname(fname, sfname) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
753 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
754 buf = curbuf; // just in case autocmds made "buf" invalid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
755 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
756
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
757 if (sfname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
758 sfname = fname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759 // For Unix: Use the short file name whenever possible.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 // Avoids problems with networks and when directory names are changed.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761 // Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
762 // another directory, which we don't detect
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
763 ffname = fname; // remember full fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
764 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
765 fname = sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
766 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
767
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
768 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
769 overwriting = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
770 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
771 overwriting = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
772
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
773 if (exiting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
774 settmode(TMODE_COOK); // when exiting allow typeahead now
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
775
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
776 ++no_wait_return; // don't wait for return yet
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
777
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
778 // Set '[ and '] marks to the lines to be written.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
779 buf->b_op_start.lnum = start;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
780 buf->b_op_start.col = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
781 buf->b_op_end.lnum = end;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
782 buf->b_op_end.col = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
783
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
784 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
786 int buf_ffname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
787 int buf_sfname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
788 int buf_fname_f = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789 int buf_fname_s = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
790 int did_cmd = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
791 int nofile_err = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
792 int empty_memline = (buf->b_ml.ml_mfp == NULL);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
793 bufref_T bufref;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
794
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
795 // Apply PRE autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
796 // Set curbuf to the buffer to be written.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
797 // Careful: The autocommands may call buf_write() recursively!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
798 if (ffname == buf->b_ffname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
799 buf_ffname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
800 if (sfname == buf->b_sfname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
801 buf_sfname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
802 if (fname == buf->b_ffname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
803 buf_fname_f = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
804 if (fname == buf->b_sfname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 buf_fname_s = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
806
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
807 // set curwin/curbuf to buf and save a few things
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808 aucmd_prepbuf(&aco, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809 set_bufref(&bufref, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
810
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
811 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
812 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
813 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 sfname, sfname, FALSE, curbuf, eap)))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
815 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
816 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
817 if (overwriting && bt_nofilename(curbuf))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
818 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
819 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
820 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
821 apply_autocmds_exarg(EVENT_FILEAPPENDPRE,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
822 sfname, sfname, FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
823 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
824 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
825 else if (filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
826 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
827 apply_autocmds_exarg(EVENT_FILTERWRITEPRE,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
828 NULL, sfname, FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
829 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
830 else if (reset_changed && whole)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
831 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
832 int was_changed = curbufIsChanged();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
833
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
834 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
835 sfname, sfname, FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
836 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
837 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
838 if (was_changed && !curbufIsChanged())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
840 // Written everything correctly and BufWriteCmd has reset
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
841 // 'modified': Correct the undo information so that an
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
842 // undo now sets 'modified'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
843 u_unchanged(curbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
844 u_update_save_nr(curbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
845 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
848 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
849 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
850 if (overwriting && bt_nofilename(curbuf))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
851 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
852 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
853 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
854 apply_autocmds_exarg(EVENT_BUFWRITEPRE,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
855 sfname, sfname, FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
856 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
857 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
858 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
859 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
860 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
861 sfname, sfname, FALSE, curbuf, eap)))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
862 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
863 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
864 if (overwriting && bt_nofilename(curbuf))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
865 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
866 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
867 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
868 apply_autocmds_exarg(EVENT_FILEWRITEPRE,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
869 sfname, sfname, FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
870 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
871 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
872
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
873 // restore curwin/curbuf and a few other things
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
874 aucmd_restbuf(&aco);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
875
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
876 // In three situations we return here and don't write the file:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
877 // 1. the autocommands deleted or unloaded the buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
878 // 2. The autocommands abort script processing.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
879 // 3. If one of the "Cmd" autocommands was executed.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
880 if (!bufref_valid(&bufref))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
881 buf = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
882 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
883 || did_cmd || nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
884 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
885 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
886 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
888 {
22699
e82579016863 patch 8.2.1898: command modifier parsing always uses global cmdmod
Bram Moolenaar <Bram@vim.org>
parents: 20828
diff changeset
889 if (buf != NULL && (cmdmod.cmod_flags & CMOD_LOCKMARKS))
18619
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
890 {
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
891 // restore the original '[ and '] positions
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
892 buf->b_op_start = orig_start;
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
893 buf->b_op_end = orig_end;
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
894 }
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
895
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
898 if (nofile_err)
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
899 emsg(_(e_no_matching_autocommands_for_acwrite_buffer));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
900
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
901 if (nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
902 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
903 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
904 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
905 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
906 // An aborting error, interrupt or exception in the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
907 // autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
908 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
909 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
910 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
911 if (buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
912 // The buffer was deleted. We assume it was written
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
913 // (can't retry anyway).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
914 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
915 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
916 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
917 // Assume the buffer was written, update the timestamp.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
918 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
919 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
920 buf->b_flags &= ~BF_NEW;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
921 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
922 buf->b_flags &= ~BF_WRITE_MASK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
923 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
924 if (reset_changed && buf->b_changed && !append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
925 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
926 // Buffer still changed, the autocommands didn't work
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
927 // properly.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
928 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
929 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
930 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
931 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
932 if (!aborting())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
933 #endif
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
934 emsg(_(e_autocommands_deleted_or_unloaded_buffer_to_be_written));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
935 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
936 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
937
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
938 // The autocommands may have changed the number of lines in the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
939 // When writing the whole file, adjust the end.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
940 // When writing part of the file, assume that the autocommands only
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
941 // changed the number of lines that are to be written (tricky!).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
942 if (buf->b_ml.ml_line_count != old_line_count)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
943 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
944 if (whole) // write all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
945 end = buf->b_ml.ml_line_count;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
946 else if (buf->b_ml.ml_line_count > old_line_count) // more lines
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
947 end += buf->b_ml.ml_line_count - old_line_count;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
948 else // less lines
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
949 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
950 end -= old_line_count - buf->b_ml.ml_line_count;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
951 if (end < start)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
952 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
953 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
954 msg_scroll = msg_save;
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
955 emsg(_(e_autocommands_changed_number_of_lines_in_unexpected_way));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
956 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
957 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
958 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
959 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
960
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
961 // The autocommands may have changed the name of the buffer, which may
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
962 // be kept in fname, ffname and sfname.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
963 if (buf_ffname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
964 ffname = buf->b_ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
965 if (buf_sfname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
966 sfname = buf->b_sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
967 if (buf_fname_f)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
968 fname = buf->b_ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
969 if (buf_fname_s)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
970 fname = buf->b_sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
971 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
972
22699
e82579016863 patch 8.2.1898: command modifier parsing always uses global cmdmod
Bram Moolenaar <Bram@vim.org>
parents: 20828
diff changeset
973 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
18619
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
974 {
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
975 // restore the original '[ and '] positions
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
976 buf->b_op_start = orig_start;
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
977 buf->b_op_end = orig_end;
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
978 }
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
979
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
980 #ifdef FEAT_NETBEANS_INTG
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
981 if (netbeans_active() && isNetbeansBuffer(buf))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
982 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
983 if (whole)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
984 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
985 // b_changed can be 0 after an undo, but we still need to write
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
986 // the buffer to NetBeans.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
987 if (buf->b_changed || isNetbeansModified(buf))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
988 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
989 --no_wait_return; // may wait for return now
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
990 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
991 netbeans_save_buffer(buf); // no error checking...
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
992 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
993 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
994 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
995 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
996 errnum = (char_u *)"E656: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
997 errmsg = (char_u *)_(e_netbeans_disallows_writes_of_unmodified_buffers);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
998 buffer = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
999 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1000 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1001 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1002 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1003 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1004 errnum = (char_u *)"E657: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1005 errmsg = (char_u *)_(e_partial_writes_disallowed_for_netbeans_buffers);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1006 buffer = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1007 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1008 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1009 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1010 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1011
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1012 if (shortmess(SHM_OVER) && !exiting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1013 msg_scroll = FALSE; // overwrite previous file message
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1014 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1015 msg_scroll = TRUE; // don't overwrite previous file message
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1016 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1017 filemess(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1018 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1019 sfname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1020 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1021 fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1022 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1023 (char_u *)"", 0); // show that we are busy
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1024 msg_scroll = FALSE; // always overwrite the file message now
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1025
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1026 buffer = alloc(WRITEBUFSIZE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1027 if (buffer == NULL) // can't allocate big buffer, use small
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1028 // one (to be able to write when out of
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1029 // memory)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1030 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1031 buffer = smallbuf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1032 bufsize = SMALLBUFSIZE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1033 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1034 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1035 bufsize = WRITEBUFSIZE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1036
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1037 // Get information about original file (if there is one).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1038 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1039 st_old.st_dev = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1040 st_old.st_ino = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1041 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1042 if (mch_stat((char *)fname, &st_old) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1043 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1044 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1045 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1046 perm = st_old.st_mode;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1047 if (!S_ISREG(st_old.st_mode)) // not a file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1048 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1049 if (S_ISDIR(st_old.st_mode))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1050 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1051 errnum = (char_u *)"E502: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1052 errmsg = (char_u *)_(e_is_a_directory);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1053 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1054 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1055 if (mch_nodetype(fname) != NODE_WRITABLE)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1056 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1057 errnum = (char_u *)"E503: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1058 errmsg = (char_u *)_(e_is_not_file_or_writable_device);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1059 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1060 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1061 // It's a device of some kind (or a fifo) which we can write to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1062 // but for which we can't make a backup.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1063 device = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1064 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1065 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1066 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1067 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1068 #else // !UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1069 // Check for a writable device name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1070 c = mch_nodetype(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1071 if (c == NODE_OTHER)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1072 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1073 errnum = (char_u *)"E503: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1074 errmsg = (char_u *)_(e_is_not_file_or_writable_device);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1075 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1076 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1077 if (c == NODE_WRITABLE)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1078 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1079 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1080 // MS-Windows allows opening a device, but we will probably get stuck
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1081 // trying to write to it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1082 if (!p_odev)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1083 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1084 errnum = (char_u *)"E796: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1085 errmsg = (char_u *)_(e_writing_to_device_disabled_with_opendevice_option);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1086 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1087 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1088 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1089 device = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1090 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1091 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1092 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1093 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1094 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1095 perm = mch_getperm(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1096 if (perm < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1097 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1098 else if (mch_isdir(fname))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1099 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1100 errnum = (char_u *)"E502: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1101 errmsg = (char_u *)_(e_is_a_directory);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1102 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1103 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1104 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1105 (void)mch_stat((char *)fname, &st_old);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1106 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1107 #endif // !UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1108
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1109 if (!device && !newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1110 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1111 // Check if the file is really writable (when renaming the file to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1112 // make a backup we won't discover it later).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1113 file_readonly = check_file_readonly(fname, (int)perm);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1114
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1115 if (!forceit && file_readonly)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1116 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1117 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1118 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1119 errnum = (char_u *)"E504: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1120 errmsg = (char_u *)_(e_is_read_only_cannot_override_W_in_cpoptions);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1121 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1122 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1123 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1124 errnum = (char_u *)"E505: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1125 errmsg = (char_u *)_(e_is_read_only_add_bang_to_override);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1126 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1127 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1128 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1129
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1130 // Check if the timestamp hasn't changed since reading the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1131 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1132 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1133 retval = check_mtime(buf, &st_old);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1134 if (retval == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1135 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1136 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1137 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1138
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1139 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1140 // For systems that support ACL: get the ACL from the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1141 if (!newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1142 acl = mch_get_acl(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1143 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1144
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1145 // If 'backupskip' is not empty, don't make a backup for some files.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1146 dobackup = (p_wb || p_bk || *p_pm != NUL);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1147 #ifdef FEAT_WILDIGN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1148 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1149 dobackup = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1150 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1151
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1152 // Save the value of got_int and reset it. We don't want a previous
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1153 // interruption cancel writing, only hitting CTRL-C while writing should
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1154 // abort it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1155 prev_got_int = got_int;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1156 got_int = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1157
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1158 // Mark the buffer as 'being saved' to prevent changed buffer warnings
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1159 buf->b_saving = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1160
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1161 // If we are not appending or filtering, the file exists, and the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1162 // 'writebackup', 'backup' or 'patchmode' option is set, need a backup.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1163 // When 'patchmode' is set also make a backup when appending.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1164 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1165 // Do not make any backup, if 'writebackup' and 'backup' are both switched
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1166 // off. This helps when editing large files on almost-full disks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1167 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1168 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1169 #if defined(UNIX) || defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1170 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1171 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1172
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1173 if ((bkc & BKC_YES) || append) // "yes"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1174 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1175 #if defined(UNIX) || defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1176 else if ((bkc & BKC_AUTO)) // "auto"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1177 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1178 int i;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1179
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1180 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1181 // Don't rename the file when:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1182 // - it's a hard link
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1183 // - it's a symbolic link
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1184 // - we don't have write permission in the directory
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1185 // - we can't set the owner/group of the new file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1186 if (st_old.st_nlink > 1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1187 || mch_lstat((char *)fname, &st) < 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1188 || st.st_dev != st_old.st_dev
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1189 || st.st_ino != st_old.st_ino
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1190 # ifndef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1191 || st.st_uid != st_old.st_uid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1192 || st.st_gid != st_old.st_gid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1193 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1194 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1195 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1196 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1197 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1198 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1199 // On NTFS file systems hard links are possible.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1200 if (mch_is_linked(fname))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1201 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1202 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1203 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1204 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1205 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1206 // Check if we can create a file and set the owner/group to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1207 // the ones from the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1208 // First find a file name that doesn't exist yet (use some
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1209 // arbitrary numbers).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1210 STRCPY(IObuff, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1211 for (i = 4913; ; i += 123)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1212 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1213 sprintf((char *)gettail(IObuff), "%d", i);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1214 if (mch_lstat((char *)IObuff, &st) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1215 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1216 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1217 fd = mch_open((char *)IObuff,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1218 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1219 if (fd < 0) // can't write in directory
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1220 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1221 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1222 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1223 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1224 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1225 vim_ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1226 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1227 if (mch_stat((char *)IObuff, &st) < 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1228 || st.st_uid != st_old.st_uid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1229 || st.st_gid != st_old.st_gid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1230 || (long)st.st_mode != perm)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1231 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1232 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1233 // Close the file before removing it, on MS-Windows we
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1234 // can't delete an open file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1235 close(fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1236 mch_remove(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1237 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1238 // MS-Windows may trigger a virus scanner to open the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1239 // file, we can't delete it then. Keep trying for half a
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1240 // second.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1241 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1242 int try;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1243
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1244 for (try = 0; try < 10; ++try)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1245 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1246 if (mch_lstat((char *)IObuff, &st) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1247 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1248 ui_delay(50L, TRUE); // wait 50 msec
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1249 mch_remove(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1250 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1251 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1252 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1253 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1254 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1255 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1256
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1257 // Break symlinks and/or hardlinks if we've been asked to.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1258 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1259 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1260 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1261 int lstat_res;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1262
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1263 lstat_res = mch_lstat((char *)fname, &st);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1264
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1265 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1266 if ((bkc & BKC_BREAKSYMLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1267 && lstat_res == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1268 && st.st_ino != st_old.st_ino)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1269 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1270
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1271 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1272 if ((bkc & BKC_BREAKHARDLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1273 && st_old.st_nlink > 1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1274 && (lstat_res != 0 || st.st_ino == st_old.st_ino))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1275 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1276 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1277 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1278 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1279 if ((bkc & BKC_BREAKSYMLINK) && mch_is_symbolic_link(fname))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1280 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1281
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1282 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1283 if ((bkc & BKC_BREAKHARDLINK) && mch_is_hard_link(fname))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1284 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1285 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1286 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1287 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1288
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1289 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1290
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1291 // make sure we have a valid backup extension to use
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1292 if (*p_bex == NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1293 backup_ext = (char_u *)".bak";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1294 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1295 backup_ext = p_bex;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1296
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1297 if (backup_copy
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1298 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1299 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1300 int bfd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1301 char_u *copybuf, *wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1302 int some_error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1303 stat_T st_new;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1304 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1305 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1306 #if defined(UNIX) || defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1307 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1308 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1309 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1310 int did_set_shortname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1311 mode_t umask_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1312 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1313
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1314 copybuf = alloc(WRITEBUFSIZE + 1);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1315 if (copybuf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1316 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1317 some_error = TRUE; // out of memory
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1318 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1319 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1320
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1321 // Try to make the backup in each directory in the 'bdir' option.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1322 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1323 // Unix semantics has it, that we may have a writable file,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1324 // that cannot be recreated with a simple open(..., O_CREAT, ) e.g:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1325 // - the directory is not writable,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1326 // - the file may be a symbolic link,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1327 // - the file may belong to another user/group, etc.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1328 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1329 // For these reasons, the existing writable file must be truncated
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1330 // and reused. Creation of a backup COPY will be attempted.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1331 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1332 while (*dirp)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1333 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1334 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1335 st_new.st_ino = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1336 st_new.st_dev = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1337 st_new.st_gid = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1338 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1339
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1340 // Isolate one directory name, using an entry in 'bdir'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1341 (void)copy_option_part(&dirp, copybuf, WRITEBUFSIZE, ",");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1342
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1343 #if defined(UNIX) || defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1344 p = copybuf + STRLEN(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1345 if (after_pathsep(copybuf, p) && p[-1] == p[-2])
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1346 // Ends with '//', use full path
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1347 if ((p = make_percent_swname(copybuf, fname)) != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1348 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1349 backup = modname(p, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1350 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1351 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1352 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1353 rootname = get_file_in_dir(fname, copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1354 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1355 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1356 some_error = TRUE; // out of memory
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1357 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1358 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1359
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1360 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1361 did_set_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1362 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1363
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1364 // May try twice if 'shortname' not set.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1365 for (;;)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1366 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1367 // Make the backup file name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1368 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1369 backup = buf_modname((buf->b_p_sn || buf->b_shortname),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1370 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1371 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1372 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1373 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1374 some_error = TRUE; // out of memory
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1375 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1376 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1377
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1378 // Check if backup file already exists.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1379 if (mch_stat((char *)backup, &st_new) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1380 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1381 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1382 // Check if backup file is same as original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1383 // May happen when modname() gave the same file back.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1384 // E.g. silly link, or file name-length reached.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1385 // If we don't check here, we either ruin the file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1386 // when copying or erase it after writing. jw.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1387 if (st_new.st_dev == st_old.st_dev
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1388 && st_new.st_ino == st_old.st_ino)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1389 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1390 VIM_CLEAR(backup); // no backup file to delete
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1391 // may try again with 'shortname' set
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1392 if (!(buf->b_shortname || buf->b_p_sn))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1393 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1394 buf->b_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1395 did_set_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1396 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1397 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1398 // setting shortname didn't help
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1399 if (did_set_shortname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1400 buf->b_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1401 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1402 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1403 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1404
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1405 // If we are not going to keep the backup file, don't
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1406 // delete an existing one, try to use another name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1407 // Change one character, just before the extension.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1408 if (!p_bk)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1409 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1410 wp = backup + STRLEN(backup) - 1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1411 - STRLEN(backup_ext);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1412 if (wp < backup) // empty file name ???
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1413 wp = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1414 *wp = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1415 while (*wp > 'a'
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1416 && mch_stat((char *)backup, &st_new) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1417 --*wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1418 // They all exist??? Must be something wrong.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1419 if (*wp == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1420 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1421 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1422 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1423 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1424 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1425 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1426
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1427 // Try to create the backup file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1428 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1429 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1430 // remove old backup, if present
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1431 mch_remove(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1432 // Open with O_EXCL to avoid the file being created while
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1433 // we were sleeping (symlink hacker attack?). Reset umask
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1434 // if possible to avoid mch_setperm() below.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1435 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1436 umask_save = umask(0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1437 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1438 bfd = mch_open((char *)backup,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1439 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1440 perm & 0777);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1441 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1442 (void)umask(umask_save);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1443 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1444 if (bfd < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1445 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1446 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1447 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1448 // Set file protection same as original file, but
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1449 // strip s-bit. Only needed if umask() wasn't used
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1450 // above.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1451 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1452 (void)mch_setperm(backup, perm & 0777);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1453 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1454 // Try to set the group of the backup same as the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1455 // original file. If this fails, set the protection
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1456 // bits for the group same as the protection bits for
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1457 // others.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1458 if (st_new.st_gid != st_old.st_gid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1459 # ifdef HAVE_FCHOWN // sequent-ptx lacks fchown()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1460 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1461 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1462 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1463 mch_setperm(backup,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1464 (perm & 0707) | ((perm & 07) << 3));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1465 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1466 mch_copy_sec(fname, backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1467 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1468 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1469
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1470 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1471 write_info.bw_fd = bfd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1472 write_info.bw_buf = copybuf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1473 write_info.bw_flags = FIO_NOCONVERT;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1474 while ((write_info.bw_len = read_eintr(fd, copybuf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1475 WRITEBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1476 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1477 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1478 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1479 errmsg = (char_u *)_(e_canot_write_to_backup_file_add_bang_to_override);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1480 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1481 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1482 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1483 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1484 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
1485 errmsg = (char_u *)_(e_interrupted);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1486 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1487 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1488 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1489
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1490 if (close(bfd) < 0 && errmsg == NULL)
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1491 errmsg = (char_u *)_(e_close_error_for_backup_file_add_bang_to_write_anyway);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1492 if (write_info.bw_len < 0)
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1493 errmsg = (char_u *)_(e_cant_read_file_for_backup_add_bang_to_write_anyway);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1494 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1495 set_file_time(backup, st_old.st_atime, st_old.st_mtime);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1496 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1497 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1498 mch_set_acl(backup, acl);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1499 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1500 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1501 mch_copy_sec(fname, backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1502 #endif
23817
e6b37eca27b6 patch 8.2.2450: MS-Windows: ADS was not preserved if 'backupcopy' is "yes"
Bram Moolenaar <Bram@vim.org>
parents: 23308
diff changeset
1503 #ifdef MSWIN
e6b37eca27b6 patch 8.2.2450: MS-Windows: ADS was not preserved if 'backupcopy' is "yes"
Bram Moolenaar <Bram@vim.org>
parents: 23308
diff changeset
1504 (void)mch_copy_file_attribute(fname, backup);
e6b37eca27b6 patch 8.2.2450: MS-Windows: ADS was not preserved if 'backupcopy' is "yes"
Bram Moolenaar <Bram@vim.org>
parents: 23308
diff changeset
1505 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1506 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1507 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1508 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1509 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1510 nobackup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1511 close(fd); // ignore errors for closing read file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1512 vim_free(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1513
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1514 if (backup == NULL && errmsg == NULL)
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1515 errmsg = (char_u *)_(e_cannot_create_backup_file_add_bang_to_write_anyway);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1516 // ignore errors when forceit is TRUE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1517 if ((some_error || errmsg != NULL) && !forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1518 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1519 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1520 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1521 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1522 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1523 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1524 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1525 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1526 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1527 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1528 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1529
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1530 // Make a backup by renaming the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1531
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1532 // If 'cpoptions' includes the "W" flag, we don't want to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1533 // overwrite a read-only file. But rename may be possible
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1534 // anyway, thus we need an extra check here.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1535 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1536 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1537 errnum = (char_u *)"E504: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1538 errmsg = (char_u *)_(e_is_read_only_cannot_override_W_in_cpoptions);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1539 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1540 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1541
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1542 // Form the backup file name - change path/fo.o.h to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1543 // path/fo.o.h.bak Try all directories in 'backupdir', first one
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1544 // that works is used.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1545 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1546 while (*dirp)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1547 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1548 // Isolate one directory name and make the backup file name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1549 (void)copy_option_part(&dirp, IObuff, IOSIZE, ",");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1550
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1551 #if defined(UNIX) || defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1552 p = IObuff + STRLEN(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1553 if (after_pathsep(IObuff, p) && p[-1] == p[-2])
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1554 // path ends with '//', use full path
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1555 if ((p = make_percent_swname(IObuff, fname)) != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1556 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1557 backup = modname(p, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1558 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1559 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1560 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1561 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1562 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1563 rootname = get_file_in_dir(fname, IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1564 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1565 backup = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1566 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1567 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1568 backup = buf_modname(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1569 (buf->b_p_sn || buf->b_shortname),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1570 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1571 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1572 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1573 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1574
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1575 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1576 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1577 // If we are not going to keep the backup file, don't
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1578 // delete an existing one, try to use another name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1579 // Change one character, just before the extension.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1580 if (!p_bk && mch_getperm(backup) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1581 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1582 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1583 if (p < backup) // empty file name ???
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1584 p = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1585 *p = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1586 while (*p > 'a' && mch_getperm(backup) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1587 --*p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1588 // They all exist??? Must be something wrong!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1589 if (*p == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1590 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1591 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1592 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1593 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1594 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1595 // Delete any existing backup and move the current version
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1596 // to the backup. For safety, we don't remove the backup
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1597 // until the write has finished successfully. And if the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1598 // 'backup' option is set, leave it around.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1599
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1600 // If the renaming of the original file to the backup file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1601 // works, quit here.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1602 if (vim_rename(fname, backup) == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1603 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1604
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1605 VIM_CLEAR(backup); // don't do the rename below
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1606 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1607 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1608 if (backup == NULL && !forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1609 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1610 errmsg = (char_u *)_(e_cant_make_backup_file_add_bang_to_write_anyway);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1611 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1612 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1613 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1614 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1615
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1616 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1617 // When using ":w!" and the file was read-only: make it writable
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1618 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1619 && vim_strchr(p_cpo, CPO_FWRITE) == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1620 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1621 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1622 (void)mch_setperm(fname, perm);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1623 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1624 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1625 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1626
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1627 // When using ":w!" and writing to the current file, 'readonly' makes no
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1628 // sense, reset it, unless 'Z' appears in 'cpoptions'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1629 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1630 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1631 buf->b_p_ro = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1632 need_maketitle = TRUE; // set window title later
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1633 status_redraw_all(); // redraw status lines later
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1634 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1635
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1636 if (end > buf->b_ml.ml_line_count)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1637 end = buf->b_ml.ml_line_count;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1638 if (buf->b_ml.ml_flags & ML_EMPTY)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1639 start = end + 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1640
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1641 // If the original file is being overwritten, there is a small chance that
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1642 // we crash in the middle of writing. Therefore the file is preserved now.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1643 // This makes all block numbers positive so that recovery does not need
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1644 // the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1645 // Don't do this if there is a backup file and we are exiting.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1646 if (reset_changed && !newfile && overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1647 && !(exiting && backup != NULL))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1648 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1649 ml_preserve(buf, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1650 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1651 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
1652 errmsg = (char_u *)_(e_interrupted);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1653 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1654 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1655 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1656
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1657 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1658 vms_remove_version(fname); // remove version
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1659 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1660 // Default: write the file directly. May write to a temp file for
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1661 // multi-byte conversion.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1662 wfname = fname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1663
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1664 // Check for forced 'fileencoding' from "++opt=val" argument.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1665 if (eap != NULL && eap->force_enc != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1666 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1667 fenc = eap->cmd + eap->force_enc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1668 fenc = enc_canonize(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1669 fenc_tofree = fenc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1670 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1671 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1672 fenc = buf->b_p_fenc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1673
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1674 // Check if the file needs to be converted.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1675 converted = need_conversion(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1676
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1677 // Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1678 // Latin1 to Unicode conversion. This is handled in buf_write_bytes().
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1679 // Prepare the flags for it and allocate bw_conv_buf when needed.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1680 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1681 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1682 wb_flags = get_fio_flags(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1683 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1684 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1685 // Need to allocate a buffer to translate into.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1686 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1687 write_info.bw_conv_buflen = bufsize * 2;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1688 else // FIO_UCS4
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1689 write_info.bw_conv_buflen = bufsize * 4;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1690 write_info.bw_conv_buf = alloc(write_info.bw_conv_buflen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1691 if (write_info.bw_conv_buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1692 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1693 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1694 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1695
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1696 #ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1697 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1698 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1699 // Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1700 write_info.bw_conv_buflen = bufsize * 4;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1701 write_info.bw_conv_buf = alloc(write_info.bw_conv_buflen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1702 if (write_info.bw_conv_buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1703 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1704 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1705 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1706
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1707 #ifdef MACOS_CONVERT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1708 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1709 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1710 write_info.bw_conv_buflen = bufsize * 3;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1711 write_info.bw_conv_buf = alloc(write_info.bw_conv_buflen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1712 if (write_info.bw_conv_buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1713 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1714 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1715 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1716
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1717 #if defined(FEAT_EVAL) || defined(USE_ICONV)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1718 if (converted && wb_flags == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1719 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1720 # ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1721 // Use iconv() conversion when conversion is needed and it's not done
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1722 // internally.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1723 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1724 enc_utf8 ? (char_u *)"utf-8" : p_enc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1725 if (write_info.bw_iconv_fd != (iconv_t)-1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1726 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1727 // We're going to use iconv(), allocate a buffer to convert in.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1728 write_info.bw_conv_buflen = bufsize * ICONV_MULT;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1729 write_info.bw_conv_buf = alloc(write_info.bw_conv_buflen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1730 if (write_info.bw_conv_buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1731 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1732 write_info.bw_first = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1733 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1734 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1735 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1736 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1737 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1738
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1739 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1740 // When the file needs to be converted with 'charconvert' after
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1741 // writing, write to a temp file instead and let the conversion
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1742 // overwrite the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1743 if (*p_ccv != NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1744 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1745 wfname = vim_tempname('w', FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1746 if (wfname == NULL) // Can't write without a tempfile!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1747 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1748 errmsg = (char_u *)_(e_cant_find_temp_file_for_writing);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1749 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1750 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1751 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1752 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1753 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1754 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1755 if (converted && wb_flags == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1756 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1757 && write_info.bw_iconv_fd == (iconv_t)-1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1758 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1759 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1760 && wfname == fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1761 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1762 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1763 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1764 if (!forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1765 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1766 errmsg = (char_u *)_(e_cannot_convert_add_bang_to_write_without_conversion);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1767 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1768 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1769 notconverted = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1770 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1771
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1772 // If conversion is taking place, we may first pretend to write and check
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1773 // for conversion errors. Then loop again to write for real.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1774 // When not doing conversion this writes for real right away.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1775 for (checking_conversion = TRUE; ; checking_conversion = FALSE)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1776 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1777 // There is no need to check conversion when:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1778 // - there is no conversion
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1779 // - we make a backup file, that can be restored in case of conversion
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1780 // failure.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1781 if (!converted || dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1782 checking_conversion = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1783
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1784 if (checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1785 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1786 // Make sure we don't write anything.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1787 fd = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1788 write_info.bw_fd = fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1789 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1790 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1791 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1792 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1793 # define TRUNC_ON_OPEN 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1794 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1795 # define TRUNC_ON_OPEN O_TRUNC
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1796 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1797 // Open the file "wfname" for writing.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1798 // We may try to open the file twice: If we can't write to the file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1799 // and forceit is TRUE we delete the existing file and try to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1800 // create a new one. If this still fails we may have lost the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1801 // original file! (this may happen when the user reached his
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1802 // quotum for number of files).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1803 // Appending will fail if the file does not exist and forceit is
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1804 // FALSE.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1805 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1806 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1807 : (O_CREAT | TRUNC_ON_OPEN))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1808 , perm < 0 ? 0666 : (perm & 0777))) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1809 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1810 // A forced write will try to create a new file if the old one
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1811 // is still readonly. This may also happen when the directory
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1812 // is read-only. In that case the mch_remove() will fail.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1813 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1814 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1815 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1816 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1817
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1818 // Don't delete the file when it's a hard or symbolic link.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1819 if ((!newfile && st_old.st_nlink > 1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1820 || (mch_lstat((char *)fname, &st) == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1821 && (st.st_dev != st_old.st_dev
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1822 || st.st_ino != st_old.st_ino)))
26857
2aeea8611342 patch 8.2.3957: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26436
diff changeset
1823 errmsg =
2aeea8611342 patch 8.2.3957: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26436
diff changeset
1824 (char_u *)_(e_cant_open_linked_file_for_writing);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1825 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1826 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1827 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1828 errmsg = (char_u *)_(e_cant_open_file_for_writing);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1829 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1830 && perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1831 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1832 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1833 // we write to the file, thus it should be marked
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1834 // writable after all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1835 if (!(perm & 0200))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1836 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1837 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1838 if (st_old.st_uid != getuid()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1839 || st_old.st_gid != getgid())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1840 perm &= 0777;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1841 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1842 if (!append) // don't remove when appending
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1843 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1844 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1845 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1846 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1847 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1848
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1849 restore_backup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1850 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1851 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1852
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1853 // If we failed to open the file, we don't need a backup.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1854 // Throw it away. If we moved or removed the original file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1855 // try to put the backup in its place.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1856 if (backup != NULL && wfname == fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1857 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1858 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1859 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1860 // There is a small chance that we removed the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1861 // original, try to move the copy in its place.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1862 // This may not work if the vim_rename() fails.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1863 // In that case we leave the copy around.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1864
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1865 // If file does not exist, put the copy in its
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1866 // place
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1867 if (mch_stat((char *)fname, &st) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1868 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1869 // if original file does exist throw away the copy
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1870 if (mch_stat((char *)fname, &st) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1871 mch_remove(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1872 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1873 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1874 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1875 // try to put the original file back
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1876 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1877 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1878 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1879
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1880 // if original file no longer exists give an extra warning
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1881 if (!newfile && mch_stat((char *)fname, &st) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1882 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1883 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1884
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1885 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1886 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1887 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1888 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1889 write_info.bw_fd = fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1890
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1891 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1892 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1893 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1894
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1895 // Double check we are writing the intended file before making
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1896 // any changes.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1897 if (overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1898 && (!dobackup || backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1899 && fname == wfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1900 && perm >= 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1901 && mch_fstat(fd, &st) == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1902 && st.st_ino != st_old.st_ino)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1903 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1904 close(fd);
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1905 errmsg = (char_u *)_(e_file_changed_while_writing);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1906 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1907 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1908 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1909 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1910 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1911 if (!append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1912 vim_ignored = ftruncate(fd, (off_t)0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1913 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1914
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1915 #if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1916 if (backup != NULL && overwriting && !append)
23817
e6b37eca27b6 patch 8.2.2450: MS-Windows: ADS was not preserved if 'backupcopy' is "yes"
Bram Moolenaar <Bram@vim.org>
parents: 23308
diff changeset
1917 (void)mch_copy_file_attribute(backup, wfname);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1918
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1919 if (!overwriting && !append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1920 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1921 if (buf->b_ffname != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1922 (void)mch_copy_file_attribute(buf->b_ffname, wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1923 // Should copy resource fork
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1924 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1925 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1926
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1927 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1928 if (*buf->b_p_key != NUL && !filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1929 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1930 char_u *header;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1931 int header_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1932
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1933 buf->b_cryptstate = crypt_create_for_writing(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1934 crypt_get_method_nr(buf),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1935 buf->b_p_key, &header, &header_len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1936 if (buf->b_cryptstate == NULL || header == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1937 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1938 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1939 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1940 // Write magic number, so that Vim knows how this file is
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1941 // encrypted when reading it back.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1942 write_info.bw_buf = header;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1943 write_info.bw_len = header_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1944 write_info.bw_flags = FIO_NOCONVERT;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1945 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1946 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1947 wb_flags |= FIO_ENCRYPTED;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1948 vim_free(header);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1949 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1950 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1951 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1952 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1953 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1954
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1955 write_info.bw_buf = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1956 nchars = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1957
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1958 // use "++bin", "++nobin" or 'binary'
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1959 if (eap != NULL && eap->force_bin != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1960 write_bin = (eap->force_bin == FORCE_BIN);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1961 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1962 write_bin = buf->b_p_bin;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1963
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1964 // The BOM is written just after the encryption magic number.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1965 // Skip it when appending and the file already existed, the BOM only
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1966 // makes sense at the start of the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1967 if (buf->b_p_bomb && !write_bin && (!append || perm < 0))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1968 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1969 write_info.bw_len = make_bom(buffer, fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1970 if (write_info.bw_len > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1971 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1972 // don't convert, do encryption
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1973 write_info.bw_flags = FIO_NOCONVERT | wb_flags;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1974 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1975 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1976 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1977 nchars += write_info.bw_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1978 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1979 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1980 write_info.bw_start_lnum = start;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1981
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1982 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1983 write_undo_file = (buf->b_p_udf
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1984 && overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1985 && !append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1986 && !filtering
25052
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
1987 # ifdef CRYPT_NOT_INPLACE
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
1988 // writing undo file requires
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
1989 // crypt_encode_inplace()
25362
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1990 && (buf->b_cryptstate == NULL
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1991 || crypt_works_inplace(buf->b_cryptstate))
25052
965d97f8208b patch 8.2.3063: crash when switching 'cryptmethod' to xchaha20 with undo file
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
1992 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1993 && reset_changed
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1994 && !checking_conversion);
25362
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1995 # ifdef CRYPT_NOT_INPLACE
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1996 // remove undo file if encrypting it is not possible
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1997 if (buf->b_p_udf
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1998 && overwriting
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1999 && !append
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2000 && !filtering
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2001 && !checking_conversion
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2002 && buf->b_cryptstate != NULL
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2003 && !crypt_works_inplace(buf->b_cryptstate))
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2004 u_undofile_reset_and_delete(buf);
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2005 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2006 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2007 // Prepare for computing the hash value of the text.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2008 sha256_start(&sha_ctx);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2009 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2010
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2011 write_info.bw_len = bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2012 write_info.bw_flags = wb_flags;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2013 fileformat = get_fileformat_force(buf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2014 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2015 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2016 for (lnum = start; lnum <= end; ++lnum)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2017 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2018 // The next while loop is done once for each character written.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2019 // Keep it fast!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2020 ptr = ml_get_buf(buf, lnum, FALSE) - 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2021 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2022 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2023 sha256_update(&sha_ctx, ptr + 1,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2024 (UINT32_T)(STRLEN(ptr + 1) + 1));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2025 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2026 while ((c = *++ptr) != NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2027 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2028 if (c == NL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2029 *s = NUL; // replace newlines with NULs
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2030 else if (c == CAR && fileformat == EOL_MAC)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2031 *s = NL; // Mac: replace CRs with NLs
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2032 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2033 *s = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2034 ++s;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2035 if (++len != bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2036 continue;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2037 #ifdef FEAT_CRYPT
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2038 if (write_info.bw_fd > 0 && lnum == end
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2039 && (write_info.bw_flags & FIO_ENCRYPTED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2040 && *buf->b_p_key != NUL && !filtering
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2041 && *ptr == NUL)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2042 write_info.bw_finish = TRUE;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2043 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2044 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2045 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2046 end = 0; // write error: break loop
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2047 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2048 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2049 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2050 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2051 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2052 write_info.bw_start_lnum = lnum;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2053 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2054 // write failed or last line has no EOL: stop here
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2055 if (end == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2056 || (lnum == end
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2057 && (write_bin || !buf->b_p_fixeol)
23308
4d6c1b17f66a patch 8.2.2199: first write after setting 'eol' does not have NL added
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
2058 && ((write_bin && lnum == buf->b_no_eol_lnum)
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2059 || (lnum == buf->b_ml.ml_line_count
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2060 && !buf->b_p_eol))))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2061 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2062 ++lnum; // written the line, count it
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2063 no_eol = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2064 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2065 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2066 if (fileformat == EOL_UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2067 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2068 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2069 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2070 *s++ = CAR; // EOL_MAC or EOL_DOS: write CR
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2071 if (fileformat == EOL_DOS) // write CR-NL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2072 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2073 if (++len == bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2074 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2075 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2076 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2077 end = 0; // write error: break loop
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2078 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2079 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2080 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2081 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2082 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2083 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2084 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2085 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2086 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2087 if (++len == bufsize && end)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2088 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2089 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2090 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2091 end = 0; // write error: break loop
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2092 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2093 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2094 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2095 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2096 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2097
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2098 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2099 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2100 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2101 end = 0; // Interrupted, break loop
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2102 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2103 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2104 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2105 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2106 // On VMS there is a problem: newlines get added when writing
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2107 // blocks at a time. Fix it by writing a line at a time.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2108 // This is much slower!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2109 // Explanation: VAX/DECC RTL insists that records in some RMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2110 // structures end with a newline (carriage return) character, and
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2111 // if they don't it adds one.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2112 // With other RMS structures it works perfect without this fix.
18896
4481f3b29fc5 patch 8.2.0009: VMS: terminal version doesn't build
Bram Moolenaar <Bram@vim.org>
parents: 18619
diff changeset
2113 # ifndef MIN
4481f3b29fc5 patch 8.2.0009: VMS: terminal version doesn't build
Bram Moolenaar <Bram@vim.org>
parents: 18619
diff changeset
2114 // Older DECC compiler for VAX doesn't define MIN()
4481f3b29fc5 patch 8.2.0009: VMS: terminal version doesn't build
Bram Moolenaar <Bram@vim.org>
parents: 18619
diff changeset
2115 # define MIN(a, b) ((a) < (b) ? (a) : (b))
4481f3b29fc5 patch 8.2.0009: VMS: terminal version doesn't build
Bram Moolenaar <Bram@vim.org>
parents: 18619
diff changeset
2116 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2117 if (buf->b_fab_rfm == FAB$C_VFC
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2118 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2119 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2120 int b2write;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2121
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2122 buf->b_fab_mrs = (buf->b_fab_mrs == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2123 ? MIN(4096, bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2124 : MIN(buf->b_fab_mrs, bufsize));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2125
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2126 b2write = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2127 while (b2write > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2128 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2129 write_info.bw_len = MIN(b2write, buf->b_fab_mrs);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2130 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2131 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2132 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2133 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2134 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2135 b2write -= MIN(b2write, buf->b_fab_mrs);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2136 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2137 write_info.bw_len = bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2138 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2139 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2140 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2141 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2142 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2143 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2144 if (len > 0 && end > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2145 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2146 write_info.bw_len = len;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2147 #ifdef FEAT_CRYPT
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2148 if (write_info.bw_fd > 0 && lnum >= end
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2149 && (write_info.bw_flags & FIO_ENCRYPTED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2150 && *buf->b_p_key != NUL && !filtering)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2151 write_info.bw_finish = TRUE;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2152 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2153 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2154 end = 0; // write error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2155 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2156 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2157
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2158 // Stop when writing done or an error was encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2159 if (!checking_conversion || end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2160 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2161
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2162 // If no error happened until now, writing should be ok, so loop to
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2163 // really write the buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2164 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2165
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2166 // If we started writing, finish writing. Also when an error was
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2167 // encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2168 if (!checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2169 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2170 #if defined(UNIX) && defined(HAVE_FSYNC)
23229
b545334ae654 patch 8.2.2160: various typos
Bram Moolenaar <Bram@vim.org>
parents: 22985
diff changeset
2171 // On many journaling file systems there is a bug that causes both the
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2172 // original and the backup file to be lost when halting the system
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2173 // right after writing the file. That's because only the meta-data is
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2174 // journalled. Syncing the file slows down the system, but assures it
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2175 // has been written to disk and we don't lose it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2176 // For a device do try the fsync() but don't complain if it does not
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2177 // work (could be a pipe).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2178 // If the 'fsync' option is FALSE, don't fsync(). Useful for laptops.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2179 if (p_fs && vim_fsync(fd) != 0 && !device)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2180 {
26877
06a137af96f8 patch 8.2.3967: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
2181 errmsg = (char_u *)_(e_fsync_failed);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2182 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2183 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2184 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2185
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2186 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2187 // Probably need to set the security context.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2188 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2189 mch_copy_sec(backup, wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2190 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2191
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2192 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2193 // When creating a new file, set its owner/group to that of the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2194 // original file. Get the new device and inode number.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2195 if (backup != NULL && !backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2196 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2197 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2198 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2200 // Don't change the owner when it's already OK, some systems remove
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2201 // permission or ACL stuff.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2202 if (mch_stat((char *)wfname, &st) < 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2203 || st.st_uid != st_old.st_uid
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2204 || st.st_gid != st_old.st_gid)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2205 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2206 // changing owner might not be possible
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2207 vim_ignored = fchown(fd, st_old.st_uid, -1);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2208 // if changing group fails clear the group permissions
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2209 if (fchown(fd, -1, st_old.st_gid) == -1 && perm > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2210 perm &= ~070;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2211 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2212 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2213 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2214 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2215 else if (!buf->b_dev_valid)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2216 // Set the inode when creating a new file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2217 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2218 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2219
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2220 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2221 if (made_writable)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2222 perm &= ~0200; // reset 'w' bit for security reasons
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2223 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2224 #ifdef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2225 // set permission of new file same as old file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2226 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2227 (void)mch_fsetperm(fd, perm);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2228 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2229 if (close(fd) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2230 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2231 errmsg = (char_u *)_(e_close_failed);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2232 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2233 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2234
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2235 #ifndef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2236 // set permission of new file same as old file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2237 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2238 (void)mch_setperm(wfname, perm);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2239 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2240 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2241 // Probably need to set the ACL before changing the user (can't set the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2242 // ACL on a file the user doesn't own).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2243 // On Solaris, with ZFS and the aclmode property set to "discard" (the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2244 // default), chmod() discards all part of a file's ACL that don't
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2245 // represent the mode of the file. It's non-trivial for us to discover
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2246 // whether we're in that situation, so we simply always re-set the ACL.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2247 # ifndef HAVE_SOLARIS_ZFS_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2248 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2249 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2250 mch_set_acl(wfname, acl);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2251 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2252 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2253 if (buf->b_cryptstate != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2254 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2255 crypt_free_state(buf->b_cryptstate);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2256 buf->b_cryptstate = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2257 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2258 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2259
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2260 #if defined(FEAT_EVAL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2261 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2262 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2263 // The file was written to a temp file, now it needs to be
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2264 // converted with 'charconvert' to (overwrite) the output file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2265 if (end != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2266 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2267 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2268 fenc, wfname, fname) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2269 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2270 write_info.bw_conv_error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2271 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2272 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2273 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2274 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2275 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2276 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2277 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2278 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2279
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2280 if (end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2281 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2282 // Error encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2283 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2284 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2285 if (write_info.bw_conv_error)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2286 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2287 if (write_info.bw_conv_error_lnum == 0)
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2288 errmsg = (char_u *)_(e_write_error_conversion_failed_make_fenc_empty_to_override);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2289 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2290 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2291 errmsg_allocated = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2292 errmsg = alloc(300);
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2293 vim_snprintf((char *)errmsg, 300, _(e_write_error_conversion_failed_in_line_nr_make_fenc_empty_to_override),
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2294 (long)write_info.bw_conv_error_lnum);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2295 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2296 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2297 else if (got_int)
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
2298 errmsg = (char_u *)_(e_interrupted);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2299 else
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2300 errmsg = (char_u *)_(e_write_error_file_system_full);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2301 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2302
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2303 // If we have a backup file, try to put it in place of the new file,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2304 // because the new file is probably corrupt. This avoids losing the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2305 // original file when trying to make a backup when writing the file a
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2306 // second time.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2307 // When "backup_copy" is set we need to copy the backup over the new
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2308 // file. Otherwise rename the backup file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2309 // If this is OK, don't give the extra warning message.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2310 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2311 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2312 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2313 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2314 // This may take a while, if we were interrupted let the user
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2315 // know we got the message.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2316 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2317 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
2318 msg(_(e_interrupted));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2319 out_flush();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2320 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2321 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2322 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2323 if ((write_info.bw_fd = mch_open((char *)fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2324 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2325 perm & 0777)) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2326 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2327 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2328 write_info.bw_buf = smallbuf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2329 write_info.bw_flags = FIO_NOCONVERT;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2330 while ((write_info.bw_len = read_eintr(fd, smallbuf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2331 SMALLBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2332 if (buf_write_bytes(&write_info) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2333 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2334
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2335 if (close(write_info.bw_fd) >= 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2336 && write_info.bw_len == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2337 end = 1; // success
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2338 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2339 close(fd); // ignore errors for closing read file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2340 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2341 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2342 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2343 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2344 if (vim_rename(backup, fname) == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2345 end = 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2346 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2347 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2348 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2349 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2350
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2351 lnum -= start; // compute number of written lines
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2352 --no_wait_return; // may wait for return now
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2353
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2354 #if !(defined(UNIX) || defined(VMS))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2355 fname = sfname; // use shortname now, for the messages
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2356 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2357 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2358 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2359 msg_add_fname(buf, fname); // put fname in IObuff with quotes
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2360 c = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2361 if (write_info.bw_conv_error)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2362 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2363 STRCAT(IObuff, _(" CONVERSION ERROR"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2364 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2365 if (write_info.bw_conv_error_lnum != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2366 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2367 (long)write_info.bw_conv_error_lnum);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2368 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2369 else if (notconverted)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2370 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2371 STRCAT(IObuff, _("[NOT converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2372 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2373 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2374 else if (converted)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2375 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2376 STRCAT(IObuff, _("[converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2377 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2378 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2379 if (device)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2380 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2381 STRCAT(IObuff, _("[Device]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2382 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2383 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2384 else if (newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2385 {
20828
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
2386 STRCAT(IObuff, new_file_message());
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2387 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2388 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2389 if (no_eol)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2390 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2391 msg_add_eol();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2392 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2393 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2394 // may add [unix/dos/mac]
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2395 if (msg_add_fileformat(fileformat))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2396 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2397 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2398 if (wb_flags & FIO_ENCRYPTED)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2399 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2400 crypt_append_msg(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2401 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2402 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2403 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2404 msg_add_lines(c, (long)lnum, nchars); // add line/char count
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2405 if (!shortmess(SHM_WRITE))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2406 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2407 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2408 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2409 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2410 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2411 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2412
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2413 set_keep_msg((char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0), 0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2414 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2415
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2416 // When written everything correctly: reset 'modified'. Unless not
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2417 // writing to the original file and '+' is not in 'cpoptions'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2418 if (reset_changed && whole && !append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2419 && !write_info.bw_conv_error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2420 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2421 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2422 unchanged(buf, TRUE, FALSE);
25967
46205b125fbd patch 8.2.3517: TextChanged does not trigger after TextChangedI
Bram Moolenaar <Bram@vim.org>
parents: 25953
diff changeset
2423 // b:changedtick may be incremented in unchanged() but that should not
46205b125fbd patch 8.2.3517: TextChanged does not trigger after TextChangedI
Bram Moolenaar <Bram@vim.org>
parents: 25953
diff changeset
2424 // trigger a TextChanged event.
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2425 if (buf->b_last_changedtick + 1 == CHANGEDTICK(buf))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2426 buf->b_last_changedtick = CHANGEDTICK(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2427 u_unchanged(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2428 u_update_save_nr(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2429 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2430
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2431 // If written to the current file, update the timestamp of the swap file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2432 // and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2433 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2434 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2435 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2436 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2437 buf->b_flags &= ~BF_NEW;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2438 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2439 buf->b_flags &= ~BF_WRITE_MASK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2440 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2441
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2442 // If we kept a backup until now, and we are in patch mode, then we make
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2443 // the backup file our 'original' file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2444 if (*p_pm && dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2445 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2446 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2447 fname, p_pm, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2448
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2449 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2450 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2451 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2452
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2453 // If the original file does not exist yet
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2454 // the current backup file becomes the original file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2455 if (org == NULL)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2456 emsg(_(e_patchmode_cant_save_original_file));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2457 else if (mch_stat(org, &st) < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2458 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2459 vim_rename(backup, (char_u *)org);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2460 VIM_CLEAR(backup); // don't delete the file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2461 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2462 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2463 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2464 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2465 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2466 // If there is no backup file, remember that a (new) file was
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2467 // created.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2468 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2469 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2470 int empty_fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2471
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2472 if (org == NULL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2473 || (empty_fd = mch_open(org,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2474 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2475 perm < 0 ? 0666 : (perm & 0777))) < 0)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2476 emsg(_(e_patchmode_cant_touch_empty_original_file));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2477 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2478 close(empty_fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2479 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2480 if (org != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2481 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2482 mch_setperm((char_u *)org, mch_getperm(fname) & 0777);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2483 vim_free(org);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2484 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2485 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2486
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2487 // Remove the backup unless 'backup' option is set or there was a
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2488 // conversion error.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2489 if (!p_bk && backup != NULL && !write_info.bw_conv_error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2490 && mch_remove(backup) != 0)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2491 emsg(_(e_cant_delete_backup_file));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2492
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2493 goto nofail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2494
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2495 // Finish up. We get here either after failure or success.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2496 fail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2497 --no_wait_return; // may wait for return now
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2498 nofail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2499
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2500 // Done saving, we accept changed buffer warnings again
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2501 buf->b_saving = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2502
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2503 vim_free(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2504 if (buffer != smallbuf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2505 vim_free(buffer);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2506 vim_free(fenc_tofree);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2507 vim_free(write_info.bw_conv_buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2508 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2509 if (write_info.bw_iconv_fd != (iconv_t)-1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2510 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2511 iconv_close(write_info.bw_iconv_fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2512 write_info.bw_iconv_fd = (iconv_t)-1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2513 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2514 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2515 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2516 mch_free_acl(acl);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2517 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2518
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2519 if (errmsg != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2520 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2521 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2522
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2523 attr = HL_ATTR(HLF_E); // set highlight for error messages
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2524 msg_add_fname(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2525 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2526 sfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2527 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2528 fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2529 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2530 ); // put file name in IObuff with quotes
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2531 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2532 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2533 // If the error message has the form "is ...", put the error number in
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2534 // front of the file name.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2535 if (errnum != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2536 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2537 STRMOVE(IObuff + numlen, IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2538 mch_memmove(IObuff, errnum, (size_t)numlen);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2539 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2540 STRCAT(IObuff, errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2541 emsg((char *)IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2542 if (errmsg_allocated)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2543 vim_free(errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2544
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2545 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2546 if (end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2547 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2548 msg_puts_attr(_("\nWARNING: Original file may be lost or damaged\n"),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2549 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2550 msg_puts_attr(_("don't quit the editor until the file is successfully written!"),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2551 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2552
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2553 // Update the timestamp to avoid an "overwrite changed file"
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2554 // prompt when writing again.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2555 if (mch_stat((char *)fname, &st_old) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2556 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2557 buf_store_time(buf, &st_old, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2558 buf->b_mtime_read = buf->b_mtime;
25953
d7e1cf30728c patch 8.2.3510: changes are only detected with one second accuracy
Bram Moolenaar <Bram@vim.org>
parents: 25362
diff changeset
2559 buf->b_mtime_read_ns = buf->b_mtime_ns;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2560 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2561 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2562 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2563 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2564
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2565 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2566 // When writing the whole file and 'undofile' is set, also write the undo
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2567 // file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2568 if (retval == OK && write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2569 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2570 char_u hash[UNDO_HASH_SIZE];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2571
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2572 sha256_finish(&sha_ctx, hash);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2573 u_write_undo(NULL, FALSE, buf, hash);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2574 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2575 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2576
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2577 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2578 if (!should_abort(retval))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2579 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2580 if (!got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2581 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2582 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2583 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2584
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2585 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2586
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2587 // Apply POST autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2588 // Careful: The autocommands may call buf_write() recursively!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2589 aucmd_prepbuf(&aco, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2590
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2591 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2592 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2593 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2594 else if (filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2595 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2596 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2597 else if (reset_changed && whole)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2598 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2599 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2600 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2601 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2602 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2603
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2604 // restore curwin/curbuf and a few other things
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2605 aucmd_restbuf(&aco);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2606
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2607 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2608 if (aborting()) // autocmds may abort script processing
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2609 retval = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2610 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2611 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2612
22985
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2613 #ifdef FEAT_VIMINFO
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2614 // Make sure marks will be written out to the viminfo file later, even when
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2615 // the file is new.
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2616 curbuf->b_marks_read = TRUE;
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2617 #endif
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2618
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2619 got_int |= prev_got_int;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2620
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2621 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2622 }