annotate src/bufwrite.c @ 32928:35df5aacd019

Added tag v9.0.1771 for changeset 49d43532787f90e934ef427fd74a0c0ec5b9db0a
author Christian Brabandt <cb@256bit.org>
date Sun, 20 Aug 2023 20:45:05 +0200
parents d8e7d725a666
children dcfbfe57141c
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
28809
d0241e74bfdb patch 8.2.4928: various white space and cosmetic mistakes
Bram Moolenaar <Bram@vim.org>
parents: 26883
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
28809
d0241e74bfdb patch 8.2.4928: various white space and cosmetic mistakes
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
42 linenr_T bw_start_lnum; // line number at start of buffer
18199
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 && !bt_nofilename(buf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746 && !filtering
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747 && (!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
748 && 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
749 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
750 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
751 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
752 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
753 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
754
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
755 if (sfname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
756 sfname = fname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
757 // 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
758 // 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
759 // 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
760 // 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
761 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
762 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
763 fname = sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
764 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
765
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
766 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
767 overwriting = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
768 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
769 overwriting = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
770
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
771 if (exiting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
772 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
773
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
774 ++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
775
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
776 // 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
777 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
778 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
779 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
780 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
781
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
782 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
783 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
784 int buf_ffname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785 int buf_sfname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
786 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
787 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
788 int did_cmd = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789 int nofile_err = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
790 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
791 bufref_T bufref;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
792
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
793 // Apply PRE autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
794 // 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
795 // 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
796 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
797 buf_ffname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
798 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
799 buf_sfname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
800 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
801 buf_fname_f = 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_sfname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
803 buf_fname_s = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
804
31263
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
805 // Set curwin/curbuf to buf and save a few things.
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
806 aucmd_prepbuf(&aco, buf);
31263
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
807 if (curbuf != buf)
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
808 {
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
809 // Could not find a window for "buf". Doing more might cause
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
810 // problems, better bail out.
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
811 return FAIL;
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
812 }
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
813
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 set_bufref(&bufref, buf);
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 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
817 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
818 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
819 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
820 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
821 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
822 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
823 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
824 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
825 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
826 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
827 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
828 else if (filtering)
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 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
831 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
832 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
833 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
834 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
835 int was_changed = curbufIsChanged();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
836
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
837 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
838 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
839 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
840 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
841 if (was_changed && !curbufIsChanged())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
842 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
843 // 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
844 // '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
845 // undo now sets 'modified'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846 u_unchanged(curbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 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
848 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
849 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
850 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
851 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
852 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
853 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
854 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
855 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
856 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
857 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
858 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
859 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
860 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
861 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
862 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
863 {
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 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
868 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
869 }
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 // 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
873 aucmd_restbuf(&aco);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
874
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
875 // 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
876 // 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
877 // 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
878 // 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
879 if (!bufref_valid(&bufref))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
880 buf = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
881 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
882 || did_cmd || nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
883 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
884 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
885 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
886 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887 {
22699
e82579016863 patch 8.2.1898: command modifier parsing always uses global cmdmod
Bram Moolenaar <Bram@vim.org>
parents: 20828
diff changeset
888 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
889 {
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
890 // 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
891 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
892 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
893 }
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
894
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
895 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 if (nofile_err)
30347
ea2935ec4435 patch 9.0.0509: confusing error for "saveas" command with "nofile" buffer
Bram Moolenaar <Bram@vim.org>
parents: 29879
diff changeset
898 semsg(_(e_no_matching_autocommands_for_buftype_str_buffer),
ea2935ec4435 patch 9.0.0509: confusing error for "saveas" command with "nofile" buffer
Bram Moolenaar <Bram@vim.org>
parents: 29879
diff changeset
899 curbuf->b_p_bt);
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 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
1148 dobackup = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1149
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1150 // 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
1151 // 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
1152 // abort it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1153 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
1154 got_int = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1155
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1156 // 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
1157 buf->b_saving = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1158
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1159 // 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
1160 // '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
1161 // 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
1162 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1163 // 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
1164 // 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
1165 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
1166 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1167 #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
1168 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1169 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1170
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1171 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
1172 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1173 #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
1174 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
1175 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1176 int i;
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 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1179 // 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
1180 // - 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
1181 // - 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
1182 // - 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
1183 // - 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
1184 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
1185 || 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
1186 || 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
1187 || 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
1188 # ifndef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1189 || 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
1190 || 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
1191 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1192 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1193 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1194 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1195 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1196 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1197 // 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
1198 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
1199 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1200 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1201 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1202 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1203 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1204 // 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
1205 // 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
1206 // 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
1207 // arbitrary numbers).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1208 STRCPY(IObuff, fname);
29556
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1209 fd = -1;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1210 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
1211 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1212 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
1213 if (mch_lstat((char *)IObuff, &st) < 0)
29556
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1214 {
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1215 fd = mch_open((char *)IObuff,
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1216 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1217 if (fd < 0 && errno == EEXIST)
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1218 // If the same file name is created by another
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1219 // process between lstat() and open(), find another
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1220 // name.
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1221 continue;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1222 break;
29556
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1223 }
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1224 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1225 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
1226 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1227 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1228 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1229 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1230 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1231 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
1232 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1233 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
1234 || 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
1235 || 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
1236 || (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
1237 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1238 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1239 // 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
1240 // 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
1241 close(fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1242 mch_remove(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1243 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1244 // 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
1245 // 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
1246 // second.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1247 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1248 int try;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1249
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1250 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
1251 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1252 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
1253 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1254 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
1255 mch_remove(IObuff);
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1258 # endif
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1261 }
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 // 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
1264 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
1265 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1266 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1267 int lstat_res;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1268
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1269 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
1270
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1271 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1272 if ((bkc & BKC_BREAKSYMLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1273 && lstat_res == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1274 && 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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1277 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1278 if ((bkc & BKC_BREAKHARDLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1279 && 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
1280 && (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
1281 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1282 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1283 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1284 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1285 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
1286 backup_copy = FALSE;
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 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1289 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
1290 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1291 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1292 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1293 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1294
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1295 #endif
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 // 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
1298 if (*p_bex == NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1299 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
1300 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1301 backup_ext = p_bex;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1302
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1303 if (backup_copy
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1304 && (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
1305 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1306 int bfd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1307 char_u *copybuf, *wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1308 int some_error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1309 stat_T st_new;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1310 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1311 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1312 #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
1313 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1314 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1315 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1316 int did_set_shortname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1317 mode_t umask_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1318 #endif
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 copybuf = alloc(WRITEBUFSIZE + 1);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1321 if (copybuf == NULL)
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 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
1324 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1325 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1326
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1327 // 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
1328 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1329 // 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
1330 // 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
1331 // - 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
1332 // - 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
1333 // - 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
1334 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1335 // 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
1336 // 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
1337 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1338 while (*dirp)
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 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1341 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
1342 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
1343 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
1344 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1345
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1346 // 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
1347 (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
1348
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1349 #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
1350 p = copybuf + STRLEN(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1351 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
1352 // 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
1353 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
1354 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1355 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
1356 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1357 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1358 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1359 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
1360 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1361 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1362 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
1363 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1364 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1365
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1366 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1367 did_set_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1368 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1369
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1370 // 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
1371 for (;;)
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 // 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
1374 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1375 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
1376 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1377 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1378 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1379 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1380 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
1381 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1382 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1383
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1384 // 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
1385 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
1386 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1387 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1388 // 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
1389 // 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
1390 // 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
1391 // 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
1392 // 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
1393 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
1394 && 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
1395 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1396 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
1397 // 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
1398 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
1399 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1400 buf->b_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1401 did_set_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1402 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1403 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1404 // 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
1405 if (did_set_shortname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1406 buf->b_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1407 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1408 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1409 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1410
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1411 // 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
1412 // 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
1413 // 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
1414 if (!p_bk)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1415 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1416 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
1417 - STRLEN(backup_ext);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1418 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
1419 wp = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1420 *wp = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1421 while (*wp > 'a'
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1422 && 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
1423 --*wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1424 // 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
1425 if (*wp == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1426 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1427 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1428 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1429 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1430 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1431 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1432
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1433 // 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
1434 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1435 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1436 // 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
1437 mch_remove(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1438 // 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
1439 // 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
1440 // 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
1441 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1442 umask_save = umask(0);
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 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
1445 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
1446 perm & 0777);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1447 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1448 (void)umask(umask_save);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1449 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1450 if (bfd < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1451 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1452 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1453 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1454 // 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
1455 // 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
1456 // above.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1457 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1458 (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
1459 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1460 // 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
1461 // 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
1462 // 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
1463 // others.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1464 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
1465 # 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
1466 && 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
1467 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1468 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1469 mch_setperm(backup,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1470 (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
1471 # 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
1472 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
1473 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1474 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1475
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1476 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1477 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
1478 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
1479 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
1480 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
1481 WRITEBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1482 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1483 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
1484 {
30986
360f286b5869 patch 9.0.0828: various typos
Bram Moolenaar <Bram@vim.org>
parents: 30982
diff changeset
1485 errmsg = (char_u *)_(e_cant_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
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 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1489 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1490 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
1491 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
1492 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1493 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1494 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1495
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1496 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
1497 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
1498 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
1499 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
1500 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1501 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
1502 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1503 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1504 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
1505 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1506 #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
1507 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
1508 #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
1509 #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
1510 (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
1511 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1512 break;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1515 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1516 nobackup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1517 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
1518 vim_free(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1519
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1520 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
1521 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
1522 // 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
1523 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
1524 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1525 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1526 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1527 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1528 errmsg = NULL;
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 else
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 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1533 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1534 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1535
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1536 // 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
1537
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1538 // 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
1539 // 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
1540 // 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
1541 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
1542 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1543 errnum = (char_u *)"E504: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1544 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
1545 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1546 }
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 // 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
1549 // 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
1550 // that works is used.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1551 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1552 while (*dirp)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1553 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1554 // 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
1555 (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
1556
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1557 #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
1558 p = IObuff + STRLEN(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1559 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
1560 // 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
1561 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
1562 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1563 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
1564 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1565 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1566 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1567 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1568 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1569 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
1570 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1571 backup = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1572 else
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 backup = buf_modname(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1575 (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
1576 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1577 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1578 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1579 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1580
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1581 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1582 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1583 // 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
1584 // 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
1585 // 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
1586 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
1587 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1588 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
1589 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
1590 p = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1591 *p = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1592 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
1593 --*p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1594 // 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
1595 if (*p == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1596 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1597 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1598 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1599 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1600 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1601 // 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
1602 // 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
1603 // 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
1604 // '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
1605
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1606 // 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
1607 // works, quit here.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1608 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
1609 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1610
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1611 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
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 if (backup == NULL && !forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1615 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1616 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
1617 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1618 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1619 }
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1622 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1623 // 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
1624 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
1625 && 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
1626 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1627 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1628 (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
1629 made_writable = TRUE;
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1632
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1633 // 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
1634 // 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
1635 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
1636 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1637 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
1638 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
1639 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
1640 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1641
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1642 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
1643 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
1644 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
1645 start = end + 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1646
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1647 // 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
1648 // 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
1649 // 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
1650 // the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1651 // 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
1652 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
1653 && !(exiting && backup != NULL))
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 ml_preserve(buf, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1656 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1657 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
1658 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
1659 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1660 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1661 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1662
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1663 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1664 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
1665 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1666 // 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
1667 // multi-byte conversion.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1668 wfname = fname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1669
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1670 // 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
1671 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
1672 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1673 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
1674 fenc = enc_canonize(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1675 fenc_tofree = 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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1678 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
1679
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1680 // 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
1681 converted = need_conversion(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1682
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1683 // 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
1684 // 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
1685 // 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
1686 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
1687 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1688 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
1689 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
1690 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1691 // 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
1692 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
1693 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
1694 else // FIO_UCS4
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1695 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
1696 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
1697 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
1698 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1699 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1700 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1701
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1702 #ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1703 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
1704 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1705 // 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
1706 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
1707 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
1708 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
1709 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1710 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1711 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1712
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1713 #ifdef MACOS_CONVERT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1714 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
1715 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1716 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
1717 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
1718 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
1719 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1720 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1721 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1722
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1723 #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
1724 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
1725 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1726 # ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1727 // 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
1728 // internally.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1729 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
1730 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
1731 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
1732 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1733 // 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
1734 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
1735 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
1736 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
1737 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1738 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
1739 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1740 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1741 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1742 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1743 # endif
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 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1746 // 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
1747 // 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
1748 // overwrite the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1749 if (*p_ccv != NUL)
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 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
1752 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
1753 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1754 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
1755 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1756 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1757 }
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1760 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1761 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
1762 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1763 && 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
1764 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1765 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1766 && wfname == fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1767 # endif
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 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1770 if (!forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1771 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1772 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
1773 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1774 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1775 notconverted = TRUE;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1778 // 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
1779 // 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
1780 // 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
1781 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
1782 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1783 // 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
1784 // - there is no conversion
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1785 // - 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
1786 // failure.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1787 if (!converted || dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1788 checking_conversion = FALSE;
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 if (checking_conversion)
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 // 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
1793 fd = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1794 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
1795 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1796 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1797 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1798 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1799 # 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
1800 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1801 # 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
1802 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1803 // 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
1804 // 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
1805 // 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
1806 // 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
1807 // 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
1808 // 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
1809 // 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
1810 // FALSE.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1811 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
1812 ? (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
1813 : (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
1814 , 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
1815 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1816 // 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
1817 // 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
1818 // 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
1819 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1820 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1821 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1822 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1823
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1824 // 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
1825 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
1826 || (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
1827 && (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
1828 || 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
1829 errmsg =
2aeea8611342 patch 8.2.3957: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26436
diff changeset
1830 (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
1831 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1832 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1833 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1834 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
1835 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
1836 && perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1837 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1838 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1839 // 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
1840 // writable after all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1841 if (!(perm & 0200))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1842 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1843 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1844 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
1845 || 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
1846 perm &= 0777;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1847 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1848 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
1849 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1850 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1851 }
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1854
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1855 restore_backup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1856 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1857 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1858
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1859 // 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
1860 // 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
1861 // 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
1862 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
1863 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1864 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1865 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1866 // 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
1867 // 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
1868 // 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
1869 // 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
1870
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1871 // 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
1872 // place
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1873 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
1874 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1875 // 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
1876 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
1877 mch_remove(backup);
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1880 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1881 // 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
1882 vim_rename(backup, fname);
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1886 // 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
1887 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
1888 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1889 }
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 (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1892 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1893 goto fail;
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 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
1896
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1897 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1898 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1899 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1900
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1901 // 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
1902 // any changes.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1903 if (overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1904 && (!dobackup || backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1905 && fname == wfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1906 && perm >= 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1907 && 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
1908 && 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
1909 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1910 close(fd);
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1911 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
1912 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1913 }
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1916 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1917 if (!append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1918 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
1919 #endif
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 defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1922 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
1923 (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
1924
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1925 if (!overwriting && !append)
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 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
1928 (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
1929 // Should copy resource fork
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1930 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1931 #endif
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 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1934 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
1935 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1936 char_u *header;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1937 int header_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1938
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1939 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
1940 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
1941 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
1942 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
1943 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1944 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1945 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1946 // 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
1947 // 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
1948 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
1949 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
1950 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
1951 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
1952 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1953 wb_flags |= FIO_ENCRYPTED;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1954 vim_free(header);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1955 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1956 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1957 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1958 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1959 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1960
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1961 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
1962 nchars = 0;
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 // 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
1965 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
1966 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
1967 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1968 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
1969
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1970 // 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
1971 // 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
1972 // 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
1973 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
1974 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1975 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
1976 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
1977 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1978 // 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
1979 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
1980 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
1981 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1982 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1983 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
1984 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1985 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1986 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
1987
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1988 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1989 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
1990 && overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1991 && !append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1992 && !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
1993 # 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
1994 // 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
1995 // 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
1996 && (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
1997 || 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
1998 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1999 && reset_changed
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2000 && !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
2001 # 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
2002 // 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
2003 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
2004 && overwriting
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2005 && !append
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2006 && !filtering
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2007 && !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
2008 && 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
2009 && !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
2010 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
2011 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2012 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2013 // 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
2014 sha256_start(&sha_ctx);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2015 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2016
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2017 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
2018 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
2019 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
2020 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2021 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2022 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
2023 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2024 // 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
2025 // Keep it fast!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2026 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
2027 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2028 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2029 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
2030 (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
2031 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2032 while ((c = *++ptr) != NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2033 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2034 if (c == NL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2035 *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
2036 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
2037 *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
2038 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2039 *s = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2040 ++s;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2041 if (++len != bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2042 continue;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2043 #ifdef FEAT_CRYPT
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2044 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
2045 && (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
2046 && *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
2047 && *ptr == NUL)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2048 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
2049 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2050 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
2051 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2052 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
2053 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2054 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2055 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2056 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2057 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2058 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
2059 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2060 // 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
2061 if (end == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2062 || (lnum == end
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2063 && (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
2064 && ((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
2065 || (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
2066 && !buf->b_p_eol))))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2067 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2068 ++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
2069 no_eol = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2070 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2071 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2072 if (fileformat == EOL_UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2073 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2074 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2075 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2076 *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
2077 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
2078 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2079 if (++len == bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2080 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2081 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
2082 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2083 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
2084 break;
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 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2087 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2088 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2089 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2090 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2091 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2092 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2093 if (++len == bufsize && end)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2094 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2095 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
2096 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2097 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
2098 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2099 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2100 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2101 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2102 len = 0;
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 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2105 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2106 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2107 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
2108 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2109 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2110 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2111 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2112 // 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
2113 // 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
2114 // This is much slower!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2115 // 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
2116 // 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
2117 // 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
2118 // 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
2119 # ifndef MIN
4481f3b29fc5 patch 8.2.0009: VMS: terminal version doesn't build
Bram Moolenaar <Bram@vim.org>
parents: 18619
diff changeset
2120 // 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
2121 # 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
2122 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2123 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
2124 || ((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
2125 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2126 int b2write;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2127
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2128 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
2129 ? MIN(4096, bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2130 : 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
2131
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2132 b2write = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2133 while (b2write > 0)
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 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
2136 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
2137 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2138 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2139 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2140 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2141 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
2142 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2143 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
2144 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2145 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2146 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2147 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2148 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2149 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2150 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
2151 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2152 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
2153 #ifdef FEAT_CRYPT
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2154 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
2155 && (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
2156 && *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
2157 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
2158 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2159 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
2160 end = 0; // write error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2161 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2162 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2163
30982
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2164 if (!buf->b_p_fixeol && buf->b_p_eof)
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2165 {
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2166 // write trailing CTRL-Z
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2167 (void)write_eintr(write_info.bw_fd, "\x1a", 1);
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2168 nchars++;
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2169 }
92b1338023ee patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place
Bram Moolenaar <Bram@vim.org>
parents: 30965
diff changeset
2170
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2171 // 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
2172 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
2173 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2174
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2175 // 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
2176 // really write the buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2177 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2178
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2179 // 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
2180 // encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2181 if (!checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2182 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2183 #if defined(UNIX) && defined(HAVE_FSYNC)
23229
b545334ae654 patch 8.2.2160: various typos
Bram Moolenaar <Bram@vim.org>
parents: 22985
diff changeset
2184 // 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
2185 // 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
2186 // 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
2187 // 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
2188 // 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
2189 // 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
2190 // 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
2191 // 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
2192 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
2193 {
26877
06a137af96f8 patch 8.2.3967: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
2194 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
2195 end = 0;
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2198
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2199 #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
2200 // 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
2201 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2202 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
2203 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2204
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2205 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2206 // 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
2207 // 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
2208 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
2209 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2210 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2211 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2212
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2213 // 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
2214 // permission or ACL stuff.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2215 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
2216 || 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
2217 || 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
2218 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2219 // 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
2220 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
2221 // 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
2222 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
2223 perm &= ~070;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2224 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2225 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2226 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2227 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2228 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
2229 // 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
2230 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2231 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2232
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2233 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2234 if (made_writable)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2235 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
2236 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2237 #ifdef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2238 // 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
2239 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2240 (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
2241 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2242 if (close(fd) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2243 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2244 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
2245 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2246 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2247
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2248 #ifndef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2249 // 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
2250 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2251 (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
2252 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2253 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2254 // 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
2255 // 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
2256 // 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
2257 // 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
2258 // 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
2259 // 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
2260 # 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
2261 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2262 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2263 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
2264 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2265 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2266 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
2267 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2268 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
2269 buf->b_cryptstate = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2270 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2271 #endif
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 #if defined(FEAT_EVAL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2274 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2275 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2276 // 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
2277 // 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
2278 if (end != 0)
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 (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
2281 fenc, wfname, fname) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2282 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2283 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
2284 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2285 }
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 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2288 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2289 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2290 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2291 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2292
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2293 if (end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2294 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2295 // Error encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2296 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2297 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2298 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
2299 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2300 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
2301 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
2302 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2303 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2304 errmsg_allocated = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2305 errmsg = alloc(300);
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2306 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
2307 (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
2308 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2309 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2310 else if (got_int)
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
2311 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
2312 else
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2313 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
2314 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2315
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2316 // 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
2317 // 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
2318 // 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
2319 // second time.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2320 // 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
2321 // 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
2322 // 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
2323 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2324 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2325 if (backup_copy)
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 // 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
2328 // 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
2329 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2330 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
2331 msg(_(e_interrupted));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2332 out_flush();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2333 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2334 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
2335 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2336 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
2337 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
2338 perm & 0777)) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2339 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2340 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2341 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
2342 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
2343 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
2344 SMALLBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2345 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
2346 break;
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 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
2349 && 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
2350 end = 1; // success
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2351 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2352 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
2353 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2354 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2355 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2356 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2357 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
2358 end = 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2359 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2360 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2361 goto fail;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2364 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
2365 --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
2366
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2367 #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
2368 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
2369 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2370 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2371 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2372 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
2373 c = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2374 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
2375 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2376 STRCAT(IObuff, _(" CONVERSION ERROR"));
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 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
2379 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
2380 (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
2381 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2382 else if (notconverted)
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 STRCAT(IObuff, _("[NOT converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2385 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2386 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2387 else if (converted)
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 STRCAT(IObuff, _("[converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2390 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2391 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2392 if (device)
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 STRCAT(IObuff, _("[Device]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2395 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2396 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2397 else if (newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2398 {
20828
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
2399 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
2400 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2401 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2402 if (no_eol)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2403 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2404 msg_add_eol();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2405 c = TRUE;
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 // 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
2408 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
2409 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2410 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2411 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
2412 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2413 crypt_append_msg(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2414 c = TRUE;
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2417 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
2418 if (!shortmess(SHM_WRITE))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2419 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2420 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2421 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
2422 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2423 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
2424 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2425
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2426 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
2427 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2428
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2429 // 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
2430 // 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
2431 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
2432 && !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
2433 && (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
2434 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2435 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
2436 // 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
2437 // 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
2438 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
2439 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
2440 u_unchanged(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2441 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
2442 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2443
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2444 // 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
2445 // 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
2446 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2447 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2448 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2449 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2450 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
2451 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2452 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
2453 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2454
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2455 // 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
2456 // 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
2457 if (*p_pm && dobackup)
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 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
2460 fname, p_pm, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2461
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2462 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2463 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2464 stat_T st;
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 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
2467 // 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
2468 if (org == NULL)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2469 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
2470 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
2471 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2472 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
2473 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
2474 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2475 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
2476 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2477 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2478 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2479 // 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
2480 // created.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2481 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2482 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2483 int empty_fd;
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 if (org == NULL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2486 || (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
2487 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
2488 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
2489 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
2490 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2491 close(empty_fd);
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 if (org != NULL)
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 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
2496 vim_free(org);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2497 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2498 }
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 // 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
2501 // conversion error.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2502 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
2503 && mch_remove(backup) != 0)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2504 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
2505
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2506 goto nofail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2507
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2508 // 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
2509 fail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2510 --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
2511 nofail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2512
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2513 // 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
2514 buf->b_saving = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2515
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2516 vim_free(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2517 if (buffer != smallbuf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2518 vim_free(buffer);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2519 vim_free(fenc_tofree);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2520 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
2521 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2522 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
2523 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2524 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
2525 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
2526 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2527 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2528 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2529 mch_free_acl(acl);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2530 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2531
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2532 if (errmsg != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2533 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2534 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
2535
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2536 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
2537 msg_add_fname(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2538 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2539 sfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2540 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2541 fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2542 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2543 ); // 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
2544 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
2545 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
2546 // 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
2547 // 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
2548 if (errnum != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2549 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2550 STRMOVE(IObuff + numlen, IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2551 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
2552 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2553 STRCAT(IObuff, errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2554 emsg((char *)IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2555 if (errmsg_allocated)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2556 vim_free(errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2557
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2558 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2559 if (end == 0)
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 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
2562 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2563 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
2564 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2565
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2566 // 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
2567 // prompt when writing again.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2568 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
2569 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2570 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
2571 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
2572 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
2573 }
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2576 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2577
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2578 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2579 // 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
2580 // file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2581 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
2582 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2583 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
2584
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2585 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
2586 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
2587 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2588 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2589
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2590 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2591 if (!should_abort(retval))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2592 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2593 if (!got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2594 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2595 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2596 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2597
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2598 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
2599
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2600 // Apply POST autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2601 // Careful: The autocommands may call buf_write() recursively!
31263
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2602 // Only do this when a window was found for "buf".
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2603 aucmd_prepbuf(&aco, buf);
31263
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2604 if (curbuf == buf)
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2605 {
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2606 if (append)
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2607 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2608 FALSE, curbuf, eap);
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2609 else if (filtering)
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2610 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2611 FALSE, curbuf, eap);
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2612 else if (reset_changed && whole)
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2613 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2614 FALSE, curbuf, eap);
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2615 else
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2616 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2617 FALSE, curbuf, eap);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2618
31263
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2619 // restore curwin/curbuf and a few other things
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2620 aucmd_restbuf(&aco);
d8e7d725a666 patch 9.0.0965: using one window for executing autocommands is insufficient
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
2621 }
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2622
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2623 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2624 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
2625 retval = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2626 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2627 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2628
22985
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2629 #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
2630 // 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
2631 // 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
2632 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
2633 #endif
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2634
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2635 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
2636
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2637 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2638 }