annotate src/bufwrite.c @ 30347:ea2935ec4435 v9.0.0509

patch 9.0.0509: confusing error for "saveas" command with "nofile" buffer Commit: https://github.com/vim/vim/commit/500a1f9972afa354f0bc77bc535aabf9f5f0116d Author: Bram Moolenaar <Bram@vim.org> Date: Tue Sep 20 11:49:10 2022 +0100 patch 9.0.0509: confusing error for "saveas" command with "nofile" buffer Problem: Confusing error for "saveas" command with "nofile" buffer. Solution: Give a clearer error message. (closes https://github.com/vim/vim/issues/11171)
author Bram Moolenaar <Bram@vim.org>
date Tue, 20 Sep 2022 13:00:04 +0200
parents 77141226eb2e
children 3a01529f17f5
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 // set curwin/curbuf to buf and save a few things
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
806 aucmd_prepbuf(&aco, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
807 set_bufref(&bufref, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
810 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
811 if (!(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
812 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
813 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 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
815 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
816 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
817 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
818 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
819 }
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 else if (filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
822 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
823 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
824 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
825 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
826 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
827 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
828 int was_changed = curbufIsChanged();
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 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
831 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
832 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
833 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
834 if (was_changed && !curbufIsChanged())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
835 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
836 // 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
837 // '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
838 // undo now sets 'modified'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 u_unchanged(curbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
840 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
841 }
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
844 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
845 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
846 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
848 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
849 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
850 }
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
853 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
854 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
855 sfname, sfname, FALSE, curbuf, eap)))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
856 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
857 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
858 nofile_err = TRUE;
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 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
861 sfname, sfname, FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
862 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
863 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
864
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
865 // 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
866 aucmd_restbuf(&aco);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
867
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
868 // 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
869 // 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
870 // 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
871 // 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
872 if (!bufref_valid(&bufref))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
873 buf = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
874 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
875 || did_cmd || nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
876 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
877 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
878 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
879 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
880 {
22699
e82579016863 patch 8.2.1898: command modifier parsing always uses global cmdmod
Bram Moolenaar <Bram@vim.org>
parents: 20828
diff changeset
881 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
882 {
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
883 // 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
884 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
885 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
886 }
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
887
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
888 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
889 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
890 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
891 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
892 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
893
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
894 if (nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
895 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
898 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
899 // 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
900 // autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
901 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
902 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
903 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
904 if (buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
905 // 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
906 // (can't retry anyway).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
907 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
908 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
909 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
910 // 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
911 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
912 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
913 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
914 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
915 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
916 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
917 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
918 && (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
919 // 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
920 // properly.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
921 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
922 return OK;
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 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
925 if (!aborting())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
926 #endif
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
927 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
928 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
929 }
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 // 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
932 // 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
933 // 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
934 // 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
935 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
936 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
937 if (whole) // write all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
938 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
939 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
940 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
941 else // less lines
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
942 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
943 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
944 if (end < start)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
945 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
946 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
947 msg_scroll = msg_save;
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
948 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
949 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
950 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
951 }
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
954 // 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
955 // 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
956 if (buf_ffname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
957 ffname = buf->b_ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
958 if (buf_sfname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
959 sfname = buf->b_sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
960 if (buf_fname_f)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
961 fname = buf->b_ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
962 if (buf_fname_s)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
963 fname = buf->b_sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
964 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
965
22699
e82579016863 patch 8.2.1898: command modifier parsing always uses global cmdmod
Bram Moolenaar <Bram@vim.org>
parents: 20828
diff changeset
966 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
967 {
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
968 // 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
969 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
970 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
971 }
788d76db02ac patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents: 18199
diff changeset
972
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
973 #ifdef FEAT_NETBEANS_INTG
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
974 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
975 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
976 if (whole)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
977 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
978 // 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
979 // the buffer to NetBeans.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
980 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
981 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
982 --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
983 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
984 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
985 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
986 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
987 else
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 errnum = (char_u *)"E656: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
990 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
991 buffer = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
992 goto fail;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
995 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
996 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
997 errnum = (char_u *)"E657: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
998 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
999 buffer = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1000 goto fail;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1003 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1004
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1005 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
1006 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
1007 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1008 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
1009 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1010 filemess(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1011 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1012 sfname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1013 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1014 fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1015 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1016 (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
1017 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
1018
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1019 buffer = alloc(WRITEBUFSIZE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1020 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
1021 // 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
1022 // memory)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1023 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1024 buffer = smallbuf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1025 bufsize = SMALLBUFSIZE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1026 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1027 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1028 bufsize = WRITEBUFSIZE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1029
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1030 // 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
1031 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1032 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
1033 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
1034 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1035 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
1036 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1037 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1038 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1039 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
1040 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
1041 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1042 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
1043 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1044 errnum = (char_u *)"E502: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1045 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
1046 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1047 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1048 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
1049 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1050 errnum = (char_u *)"E503: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1051 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
1052 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1053 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1054 // 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
1055 // 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
1056 device = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1057 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1058 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1059 }
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 #else // !UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1062 // 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
1063 c = mch_nodetype(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1064 if (c == NODE_OTHER)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1065 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1066 errnum = (char_u *)"E503: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1067 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
1068 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1069 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1070 if (c == NODE_WRITABLE)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1071 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1072 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1073 // 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
1074 // 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
1075 if (!p_odev)
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 errnum = (char_u *)"E796: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1078 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
1079 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1080 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1081 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1082 device = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1083 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1084 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1085 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1086 else
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 perm = mch_getperm(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1089 if (perm < 0)
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 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
1092 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1093 errnum = (char_u *)"E502: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1094 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
1095 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1096 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1097 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1098 (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
1099 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1100 #endif // !UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1101
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1102 if (!device && !newfile)
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 // 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
1105 // 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
1106 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
1107
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1108 if (!forceit && file_readonly)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1109 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1110 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
1111 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1112 errnum = (char_u *)"E504: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1113 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
1114 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1115 else
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 errnum = (char_u *)"E505: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1118 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
1119 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1120 goto fail;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1123 // 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
1124 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1125 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1126 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
1127 if (retval == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1128 goto fail;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1131
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1132 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1133 // 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
1134 if (!newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1135 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
1136 #endif
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 // 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
1139 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
1140 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
1141 dobackup = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1142
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1143 // 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
1144 // 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
1145 // abort it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1146 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
1147 got_int = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1148
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1149 // 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
1150 buf->b_saving = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1151
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1152 // 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
1153 // '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
1154 // 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
1155 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1156 // 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
1157 // 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
1158 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
1159 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1160 #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
1161 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1162 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1163
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1164 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
1165 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1166 #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
1167 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
1168 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1169 int i;
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 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1172 // 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
1173 // - 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
1174 // - 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
1175 // - 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
1176 // - 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
1177 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
1178 || 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
1179 || 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
1180 || 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
1181 # ifndef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1182 || 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
1183 || 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
1184 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1185 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1186 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1187 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1188 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1189 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1190 // 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
1191 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
1192 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1193 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1194 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1195 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1196 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1197 // 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
1198 // 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
1199 // 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
1200 // arbitrary numbers).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1201 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
1202 fd = -1;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1203 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
1204 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1205 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
1206 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
1207 {
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1208 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
1209 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
1210 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
1211 // 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
1212 // 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
1213 // name.
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1214 continue;
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1215 break;
29556
261069440cb1 patch 9.0.0119: tiny chance that creating a backup file fails
Bram Moolenaar <Bram@vim.org>
parents: 28809
diff changeset
1216 }
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1217 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1218 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
1219 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1220 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1221 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1222 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1223 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1224 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
1225 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1226 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
1227 || 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
1228 || 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
1229 || (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
1230 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1231 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1232 // 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
1233 // 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
1234 close(fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1235 mch_remove(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1236 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1237 // 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
1238 // 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
1239 // second.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1240 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1241 int try;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1242
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1243 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
1244 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1245 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
1246 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1247 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
1248 mch_remove(IObuff);
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1251 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1252 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1253 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1254 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1255
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1256 // 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
1257 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
1258 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1259 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1260 int lstat_res;
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 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
1263
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1264 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1265 if ((bkc & BKC_BREAKSYMLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1266 && lstat_res == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1267 && 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
1268 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1269
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1270 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1271 if ((bkc & BKC_BREAKHARDLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1272 && 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
1273 && (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
1274 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1275 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1276 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1277 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1278 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
1279 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1280
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1281 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1282 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
1283 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1284 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1285 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1286 }
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1289
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1290 // 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
1291 if (*p_bex == NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1292 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
1293 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1294 backup_ext = p_bex;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1295
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1296 if (backup_copy
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1297 && (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
1298 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1299 int bfd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1300 char_u *copybuf, *wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1301 int some_error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1302 stat_T st_new;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1303 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1304 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1305 #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
1306 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1307 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1308 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1309 int did_set_shortname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1310 mode_t umask_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1311 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1312
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1313 copybuf = alloc(WRITEBUFSIZE + 1);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1314 if (copybuf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1315 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1316 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
1317 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1318 }
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 // 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
1321 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1322 // 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
1323 // 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
1324 // - 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
1325 // - 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
1326 // - 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
1327 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1328 // 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
1329 // 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
1330 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1331 while (*dirp)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1332 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1333 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1334 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
1335 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
1336 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
1337 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1338
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1339 // 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
1340 (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
1341
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1342 #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
1343 p = copybuf + STRLEN(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1344 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
1345 // 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
1346 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
1347 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1348 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
1349 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1350 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1351 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1352 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
1353 if (rootname == 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 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
1356 goto nobackup;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1359 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1360 did_set_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1361 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1362
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1363 // 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
1364 for (;;)
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 // 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
1367 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1368 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
1369 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1370 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1371 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1372 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1373 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
1374 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1375 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1376
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1377 // 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
1378 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
1379 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1380 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1381 // 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
1382 // 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
1383 // 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
1384 // 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
1385 // 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
1386 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
1387 && 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
1388 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1389 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
1390 // 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
1391 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
1392 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1393 buf->b_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1394 did_set_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1395 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1396 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1397 // 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
1398 if (did_set_shortname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1399 buf->b_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1400 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1401 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1402 #endif
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 // 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
1405 // 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
1406 // 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
1407 if (!p_bk)
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 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
1410 - STRLEN(backup_ext);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1411 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
1412 wp = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1413 *wp = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1414 while (*wp > 'a'
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1415 && 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
1416 --*wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1417 // 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
1418 if (*wp == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1419 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1420 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1421 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1422 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1423 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1424 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1425
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1426 // 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
1427 if (backup != NULL)
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 // 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
1430 mch_remove(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1431 // 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
1432 // 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
1433 // 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
1434 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1435 umask_save = umask(0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1436 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1437 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
1438 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
1439 perm & 0777);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1440 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1441 (void)umask(umask_save);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1442 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1443 if (bfd < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1444 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1445 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1446 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1447 // 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
1448 // 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
1449 // above.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1450 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1451 (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
1452 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1453 // 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
1454 // 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
1455 // 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
1456 // others.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1457 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
1458 # 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
1459 && 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
1460 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1461 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1462 mch_setperm(backup,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1463 (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
1464 # 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
1465 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
1466 # endif
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 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1470 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
1471 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
1472 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
1473 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
1474 WRITEBUFSIZE)) > 0)
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 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
1477 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1478 errmsg = (char_u *)_(e_canot_write_to_backup_file_add_bang_to_override);
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1479 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1480 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1481 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1482 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1483 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
1484 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
1485 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1486 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1487 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1488
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1489 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
1490 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
1491 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
1492 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
1493 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1494 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
1495 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1496 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1497 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
1498 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1499 #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
1500 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
1501 #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
1502 #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
1503 (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
1504 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1505 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1506 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1507 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1508 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1509 nobackup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1510 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
1511 vim_free(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1512
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1513 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
1514 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
1515 // 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
1516 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
1517 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1518 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1519 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1520 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1521 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1522 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1523 else
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 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1526 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1527 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1528
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1529 // 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
1530
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1531 // 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
1532 // 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
1533 // 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
1534 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
1535 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1536 errnum = (char_u *)"E504: ";
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1537 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
1538 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1539 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1540
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1541 // 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
1542 // 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
1543 // that works is used.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1544 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1545 while (*dirp)
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 // 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
1548 (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
1549
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1550 #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
1551 p = IObuff + STRLEN(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1552 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
1553 // 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
1554 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
1555 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1556 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
1557 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1558 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1559 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1560 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1561 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1562 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
1563 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1564 backup = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1565 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1566 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1567 backup = buf_modname(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1568 (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
1569 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1570 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1571 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1572 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1573
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1574 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1575 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1576 // 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
1577 // 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
1578 // 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
1579 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
1580 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1581 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
1582 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
1583 p = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1584 *p = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1585 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
1586 --*p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1587 // 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
1588 if (*p == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1589 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1590 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1591 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1592 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1593 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1594 // 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
1595 // 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
1596 // 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
1597 // '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
1598
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1599 // 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
1600 // works, quit here.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1601 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
1602 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1603
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1604 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
1605 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1606 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1607 if (backup == NULL && !forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1608 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1609 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
1610 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1611 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1612 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1613 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1614
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1615 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1616 // 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
1617 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
1618 && 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
1619 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1620 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1621 (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
1622 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1623 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1624 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1625
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1626 // 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
1627 // 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
1628 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
1629 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1630 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
1631 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
1632 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
1633 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1634
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1635 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
1636 end = buf->b_ml.ml_line_count;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1637 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
1638 start = end + 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1639
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1640 // 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
1641 // 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
1642 // 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
1643 // the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1644 // 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
1645 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
1646 && !(exiting && backup != NULL))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1647 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1648 ml_preserve(buf, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1649 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1650 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
1651 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
1652 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1653 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1654 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1655
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1656 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1657 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
1658 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1659 // 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
1660 // multi-byte conversion.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1661 wfname = fname;
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 // 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
1664 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
1665 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1666 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
1667 fenc = enc_canonize(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1668 fenc_tofree = fenc;
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1671 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
1672
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1673 // 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
1674 converted = need_conversion(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1675
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1676 // 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
1677 // 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
1678 // 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
1679 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
1680 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1681 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
1682 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
1683 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1684 // 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
1685 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
1686 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
1687 else // FIO_UCS4
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1688 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
1689 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
1690 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
1691 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1692 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1693 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1694
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1695 #ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1696 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
1697 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1698 // 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
1699 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
1700 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
1701 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
1702 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1703 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1704 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1705
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1706 #ifdef MACOS_CONVERT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1707 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
1708 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1709 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
1710 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
1711 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
1712 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1713 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1714 #endif
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 #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
1717 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
1718 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1719 # ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1720 // 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
1721 // internally.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1722 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
1723 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
1724 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
1725 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1726 // 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
1727 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
1728 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
1729 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
1730 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1731 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
1732 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1733 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1734 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1735 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1736 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1737
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1738 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1739 // 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
1740 // 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
1741 // overwrite the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1742 if (*p_ccv != NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1743 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1744 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
1745 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
1746 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1747 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
1748 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1749 }
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 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1752 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1753 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1754 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
1755 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1756 && 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
1757 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1758 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1759 && wfname == fname
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 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1762 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1763 if (!forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1764 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1765 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
1766 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1767 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1768 notconverted = TRUE;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1771 // 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
1772 // 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
1773 // 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
1774 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
1775 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1776 // 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
1777 // - there is no conversion
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1778 // - 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
1779 // failure.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1780 if (!converted || dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1781 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 if (checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1784 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1785 // 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
1786 fd = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1787 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
1788 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1789 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1790 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1791 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1792 # 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
1793 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1794 # 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
1795 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1796 // 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
1797 // 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
1798 // 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
1799 // 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
1800 // 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
1801 // 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
1802 // 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
1803 // FALSE.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1804 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
1805 ? (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
1806 : (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
1807 , 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
1808 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1809 // 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
1810 // 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
1811 // 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
1812 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1813 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1814 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1815 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1816
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1817 // 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
1818 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
1819 || (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
1820 && (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
1821 || 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
1822 errmsg =
2aeea8611342 patch 8.2.3957: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26436
diff changeset
1823 (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
1824 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1825 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1826 {
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
1827 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
1828 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
1829 && perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1830 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1831 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1832 // 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
1833 // writable after all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1834 if (!(perm & 0200))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1835 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1836 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1837 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
1838 || 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
1839 perm &= 0777;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1840 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1841 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
1842 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1843 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1844 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1845 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1846 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1847
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1848 restore_backup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1849 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1850 stat_T st;
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 // 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
1853 // 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
1854 // 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
1855 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
1856 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1857 if (backup_copy)
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 // 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
1860 // 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
1861 // 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
1862 // 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
1863
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1864 // 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
1865 // place
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1866 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
1867 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1868 // 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
1869 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
1870 mch_remove(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1871 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1872 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1873 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1874 // 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
1875 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1876 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1877 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1878
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1879 // 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
1880 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
1881 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1882 }
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 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1885 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1886 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1887 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1888 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
1889
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1890 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1891 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1892 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1893
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1894 // 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
1895 // any changes.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1896 if (overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1897 && (!dobackup || backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1898 && fname == wfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1899 && perm >= 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1900 && 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
1901 && 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
1902 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1903 close(fd);
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
1904 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
1905 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1906 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1907 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1908 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1909 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1910 if (!append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1911 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
1912 #endif
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 #if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1915 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
1916 (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
1917
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1918 if (!overwriting && !append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1919 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1920 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
1921 (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
1922 // Should copy resource fork
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1923 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1924 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1925
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1926 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1927 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
1928 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1929 char_u *header;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1930 int header_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1931
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1932 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
1933 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
1934 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
1935 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
1936 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1937 else
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 // 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
1940 // 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
1941 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
1942 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
1943 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
1944 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
1945 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1946 wb_flags |= FIO_ENCRYPTED;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1947 vim_free(header);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1948 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1949 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1950 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1951 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1952 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1953
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1954 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
1955 nchars = 0;
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 // 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
1958 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
1959 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
1960 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1961 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
1962
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1963 // 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
1964 // 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
1965 // 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
1966 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
1967 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1968 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
1969 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
1970 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1971 // 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
1972 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
1973 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
1974 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1975 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1976 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
1977 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1978 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1979 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
1980
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1981 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1982 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
1983 && overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1984 && !append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1985 && !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
1986 # 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
1987 // 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
1988 // 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
1989 && (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
1990 || 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
1991 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1992 && reset_changed
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1993 && !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
1994 # 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
1995 // 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
1996 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
1997 && overwriting
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1998 && !append
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
1999 && !filtering
68a7e6d70a5e patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents: 25052
diff changeset
2000 && !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
2001 && 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
2002 && !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
2003 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
2004 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2005 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2006 // 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
2007 sha256_start(&sha_ctx);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2008 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2009
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2010 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
2011 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
2012 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
2013 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2014 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2015 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
2016 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2017 // 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
2018 // Keep it fast!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2019 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
2020 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2021 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2022 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
2023 (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
2024 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2025 while ((c = *++ptr) != NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2026 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2027 if (c == NL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2028 *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
2029 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
2030 *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
2031 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2032 *s = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2033 ++s;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2034 if (++len != bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2035 continue;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2036 #ifdef FEAT_CRYPT
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2037 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
2038 && (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
2039 && *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
2040 && *ptr == NUL)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2041 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
2042 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2043 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
2044 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2045 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
2046 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2047 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2048 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2049 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2050 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2051 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
2052 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2053 // 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
2054 if (end == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2055 || (lnum == end
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2056 && (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
2057 && ((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
2058 || (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
2059 && !buf->b_p_eol))))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2060 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2061 ++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
2062 no_eol = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2063 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2064 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2065 if (fileformat == EOL_UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2066 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2067 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2068 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2069 *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
2070 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
2071 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2072 if (++len == bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2073 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2074 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
2075 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2076 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
2077 break;
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 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2080 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2081 len = 0;
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 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2084 }
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 if (++len == bufsize && end)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2087 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2088 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
2089 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2090 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
2091 break;
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 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2094 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2095 len = 0;
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 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2098 if (got_int)
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 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
2101 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2102 }
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 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2105 // 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
2106 // 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
2107 // This is much slower!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2108 // 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
2109 // 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
2110 // 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
2111 // 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
2112 # ifndef MIN
4481f3b29fc5 patch 8.2.0009: VMS: terminal version doesn't build
Bram Moolenaar <Bram@vim.org>
parents: 18619
diff changeset
2113 // 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
2114 # 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
2115 # endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2116 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
2117 || ((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
2118 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2119 int b2write;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2120
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2121 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
2122 ? MIN(4096, bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2123 : 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
2124
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2125 b2write = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2126 while (b2write > 0)
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 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
2129 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
2130 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2131 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2132 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2133 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2134 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
2135 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2136 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
2137 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2138 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2139 len = 0;
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 #endif
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 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
2144 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2145 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
2146 #ifdef FEAT_CRYPT
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 23817
diff changeset
2147 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
2148 && (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
2149 && *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
2150 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
2151 #endif
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2152 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
2153 end = 0; // write error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2154 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2155 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2156
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2157 // 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
2158 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
2159 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2160
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2161 // 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
2162 // really write the buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2163 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2164
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2165 // 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
2166 // encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2167 if (!checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2168 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2169 #if defined(UNIX) && defined(HAVE_FSYNC)
23229
b545334ae654 patch 8.2.2160: various typos
Bram Moolenaar <Bram@vim.org>
parents: 22985
diff changeset
2170 // 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
2171 // 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
2172 // 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
2173 // 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
2174 // 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
2175 // 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
2176 // 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
2177 // 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
2178 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
2179 {
26877
06a137af96f8 patch 8.2.3967: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
2180 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
2181 end = 0;
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2184
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2185 #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
2186 // 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
2187 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2188 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
2189 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2190
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2191 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2192 // 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
2193 // 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
2194 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
2195 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2196 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2197 stat_T st;
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 // 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
2200 // permission or ACL stuff.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2201 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
2202 || 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
2203 || 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
2204 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2205 // 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
2206 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
2207 // 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
2208 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
2209 perm &= ~070;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2210 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2211 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2212 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2213 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2214 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
2215 // 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
2216 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2217 #endif
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 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2220 if (made_writable)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2221 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
2222 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2223 #ifdef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2224 // 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
2225 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2226 (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
2227 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2228 if (close(fd) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2229 {
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2230 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
2231 end = 0;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2234 #ifndef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2235 // 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
2236 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2237 (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
2238 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2239 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2240 // 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
2241 // 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
2242 // 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
2243 // 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
2244 // 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
2245 // 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
2246 # 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
2247 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2248 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2249 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
2250 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2251 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2252 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
2253 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2254 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
2255 buf->b_cryptstate = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2256 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2257 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2258
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2259 #if defined(FEAT_EVAL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2260 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2261 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2262 // 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
2263 // 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
2264 if (end != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2265 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2266 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
2267 fenc, wfname, fname) == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2268 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2269 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
2270 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2271 }
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 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2274 vim_free(wfname);
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2277 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2278
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2279 if (end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2280 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2281 // Error encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2282 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2283 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2284 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
2285 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2286 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
2287 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
2288 else
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 errmsg_allocated = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2291 errmsg = alloc(300);
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2292 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
2293 (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
2294 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2295 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2296 else if (got_int)
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
2297 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
2298 else
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26861
diff changeset
2299 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
2300 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2301
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2302 // 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
2303 // 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
2304 // 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
2305 // second time.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2306 // 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
2307 // 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
2308 // 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
2309 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2310 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2311 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2312 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2313 // 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
2314 // 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
2315 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2316 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
2317 msg(_(e_interrupted));
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2318 out_flush();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2319 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2320 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
2321 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2322 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
2323 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
2324 perm & 0777)) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2325 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2326 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2327 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
2328 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
2329 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
2330 SMALLBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2331 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
2332 break;
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 (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
2335 && 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
2336 end = 1; // success
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2337 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2338 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
2339 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2340 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2341 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2342 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2343 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
2344 end = 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2345 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2346 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2347 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2348 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2349
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2350 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
2351 --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
2352
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2353 #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
2354 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
2355 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2356 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2357 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2358 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
2359 c = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2360 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
2361 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2362 STRCAT(IObuff, _(" CONVERSION ERROR"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2363 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2364 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
2365 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
2366 (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
2367 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2368 else if (notconverted)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2369 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2370 STRCAT(IObuff, _("[NOT converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2371 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2372 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2373 else if (converted)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2374 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2375 STRCAT(IObuff, _("[converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2376 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2377 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2378 if (device)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2379 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2380 STRCAT(IObuff, _("[Device]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2381 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2382 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2383 else if (newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2384 {
20828
db18625d8134 patch 8.2.0966: 'shortmess' flag "n" not used in two places
Bram Moolenaar <Bram@vim.org>
parents: 18896
diff changeset
2385 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
2386 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2387 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2388 if (no_eol)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2389 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2390 msg_add_eol();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2391 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2392 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2393 // 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
2394 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
2395 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2396 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2397 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
2398 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2399 crypt_append_msg(buf);
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2403 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
2404 if (!shortmess(SHM_WRITE))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2405 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2406 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2407 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
2408 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2409 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
2410 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2411
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2412 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
2413 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2414
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2415 // 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
2416 // 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
2417 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
2418 && !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
2419 && (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
2420 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2421 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
2422 // 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
2423 // 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
2424 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
2425 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
2426 u_unchanged(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2427 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
2428 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2429
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2430 // 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
2431 // 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
2432 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2433 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2434 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2435 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2436 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
2437 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2438 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
2439 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2440
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2441 // 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
2442 // 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
2443 if (*p_pm && dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2444 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2445 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
2446 fname, p_pm, FALSE);
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 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2449 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2450 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2451
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2452 // 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
2453 // 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
2454 if (org == NULL)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2455 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
2456 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
2457 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2458 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
2459 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
2460 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2461 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
2462 #endif
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2465 // 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
2466 // created.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2467 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2468 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2469 int empty_fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2470
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2471 if (org == NULL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2472 || (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
2473 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
2474 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
2475 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
2476 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2477 close(empty_fd);
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 (org != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2480 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2481 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
2482 vim_free(org);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2483 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2484 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2485
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2486 // 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
2487 // conversion error.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2488 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
2489 && mch_remove(backup) != 0)
26861
df2de1e63de0 patch 8.2.3959: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26857
diff changeset
2490 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
2491
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2492 goto nofail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2493
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2494 // 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
2495 fail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2496 --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
2497 nofail:
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 // 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
2500 buf->b_saving = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2501
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2502 vim_free(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2503 if (buffer != smallbuf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2504 vim_free(buffer);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2505 vim_free(fenc_tofree);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2506 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
2507 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2508 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
2509 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2510 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
2511 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
2512 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2513 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2514 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2515 mch_free_acl(acl);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2516 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2517
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2518 if (errmsg != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2519 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2520 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
2521
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2522 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
2523 msg_add_fname(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2524 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2525 sfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2526 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2527 fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2528 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2529 ); // 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
2530 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
2531 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
2532 // 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
2533 // 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
2534 if (errnum != NULL)
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 STRMOVE(IObuff + numlen, IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2537 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
2538 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2539 STRCAT(IObuff, errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2540 emsg((char *)IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2541 if (errmsg_allocated)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2542 vim_free(errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2543
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2544 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2545 if (end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2546 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2547 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
2548 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2549 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
2550 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2551
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2552 // 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
2553 // prompt when writing again.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2554 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
2555 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2556 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
2557 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
2558 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
2559 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2560 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2561 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2562 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2563
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2564 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2565 // 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
2566 // file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2567 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
2568 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2569 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
2570
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2571 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
2572 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
2573 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2574 #endif
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 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2577 if (!should_abort(retval))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2578 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2579 if (!got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2580 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2581 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2582 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2583
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2584 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
2585
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2586 // Apply POST autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2587 // 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
2588 aucmd_prepbuf(&aco, buf);
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 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2591 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2592 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2593 else if (filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2594 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2595 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2596 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
2597 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2598 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2599 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2600 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2601 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2602
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2603 // 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
2604 aucmd_restbuf(&aco);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2605
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2606 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2607 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
2608 retval = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2609 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2610 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2611
22985
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2612 #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
2613 // 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
2614 // 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
2615 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
2616 #endif
d768cdd50602 patch 8.2.2039: viminfo is not written when creating a new file
Bram Moolenaar <Bram@vim.org>
parents: 22699
diff changeset
2617
18199
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2618 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
2619
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2620 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2621 }