annotate src/bufwrite.c @ 18440:d6cb1e706fb7 v8.1.2214

patch 8.1.2214: too much is redrawn when 'cursorline' is set Commit: https://github.com/vim/vim/commit/11a58af66fa5c442f0a22c5d59beabf187ed4e89 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Oct 24 22:32:31 2019 +0200 patch 8.1.2214: too much is redrawn when 'cursorline' is set Problem: Too much is redrawn when 'cursorline' is set. Solution: Don't do a complete redraw. (closes https://github.com/vim/vim/issues/5079)
author Bram Moolenaar <Bram@vim.org>
date Thu, 24 Oct 2019 22:45:04 +0200
parents e2be5a6485f5
children 788d76db02ac
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 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
35 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
36 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
37 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
38 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
39 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
40 linenr_T bw_conv_error_lnum; // first line with error or zero
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 linenr_T bw_start_lnum; // line number at start of buffer
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 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
44 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 };
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 * 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
49 * 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
50 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 ucs2bytes(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 unsigned c, // in: character
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 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
55 int flags) // FIO_ flags
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 char_u *p = *pp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 int error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 int cc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60
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 if (flags & FIO_UCS4)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 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
65 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 *p++ = (c >> 16);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 *p++ = (c >> 24);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 *p++ = (c >> 24);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 *p++ = (c >> 16);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 }
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 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
80 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 if (c >= 0x10000)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 if (flags & FIO_UTF16)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 // 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
86 // 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
87 c -= 0x10000;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 if (c >= 0x100000)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 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
91 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
92 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 *p++ = cc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 *p++ = ((unsigned)cc >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 *p++ = ((unsigned)cc >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 *p++ = cc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 c = (c & 0x3ff) + 0xdc00;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 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
107 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 *p++ = (c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 }
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 else // Latin1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 if (c >= 0x100)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 error = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 *p++ = 0xBF;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 *p++ = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 }
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 *pp = p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 return error;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 }
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 * 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
134 * 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
135 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 * 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
137 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 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
140 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 int wlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 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
143 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
144 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
145
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 // 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
147 if (!(flags & FIO_NOCONVERT))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 unsigned c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 int n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 if (flags & FIO_UTF8)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 // 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
156 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
157 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
158 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
159 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
160 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
161 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 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
163 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 // 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
165 // 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
166 if (flags & FIO_LATIN1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 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
168 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 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
170 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
171 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 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
173 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 int l;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 // 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
177 // 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
178 // short!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 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
180 if (l > len)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 l = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 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
183 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
184 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
185 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 // 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
187 // 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
188 // 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
189 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
190 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 ip->bw_restlen += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 if (n > 1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 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
196 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 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
198 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
199 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 n -= ip->bw_restlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 ip->bw_restlen = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 ip->bw_restlen -= n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 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
207 (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
208 n = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 }
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 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
214 if (n > len - wlen)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 // 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
217 // 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
218 // 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
219 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
220 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 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
222 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
223 (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
224 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 if (n > 1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 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
228 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 c = buf[wlen];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 }
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 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
233 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 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
235 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
236 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 if (c == NL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 ++ip->bw_start_lnum;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 if (flags & FIO_LATIN1)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 len = (int)(p - buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 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
245 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
246 }
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 #ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 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
251 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 // 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
253 // codepage.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 char_u *from;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 size_t fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 char_u *to;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 int u8c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 BOOL bad = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 int needed;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 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
262 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 // 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
264 // 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
265 // conversion buffer for this.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 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
267 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
268 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
269 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
270 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 from = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 fromlen = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 }
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 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
278 if (enc_utf8)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 // 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
281 // 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
282 while (fromlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 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
285 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
286 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 u8c = utf_ptr2char(from);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288 *to++ = (u8c & 0xff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 *to++ = (u8c >> 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 fromlen -= n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 from += n;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 }
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 // 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
295 // call.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 if (fromlen > CONV_RESTLEN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 // weird overlong sequence
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299 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
300 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 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
303 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
304 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
307 // 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
308 // 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
309 ip->bw_restlen = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 needed = MultiByteToWideChar(enc_codepage,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 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
312 NULL, 0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 if (needed == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 // 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
316 needed = MultiByteToWideChar(enc_codepage,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 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
318 NULL, 0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319 if (needed == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321 // Conversion doesn't work.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 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
323 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 // 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
326 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
327 ip->bw_restlen = 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
329 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
330 (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
331 (LPWSTR)to, needed);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332 if (needed == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 // 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
335 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
336 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338 to += needed * 2;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 }
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 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
342 buf = to;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 # 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
344 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
345 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 // 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
347 // 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
348 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
349 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 u8c = *from++;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 u8c += (*from++ << 8);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 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
353 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
354 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 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
356 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 }
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 len = (int)(to - buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364 // 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
365 // 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
366 // 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
367 // fail.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368 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
369 (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
370 (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
371 &bad);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 if (bad)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
374 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
375 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 }
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381 #ifdef MACOS_CONVERT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 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
383 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384 // 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
385 char_u *from;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 size_t fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388 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
389 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390 // 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
391 // 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
392 // conversion buffer for this.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 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
394 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
395 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
396 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
397 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
398 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 from = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 fromlen = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 }
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 if (enc2macroman(from, fromlen,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
405 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
406 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
407 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
408 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
409 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 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
412 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
413 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
415 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 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
417 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 const char *from;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 size_t fromlen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 char *to;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 size_t tolen;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423 // Convert with iconv().
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 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
425 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 char *fp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428 // 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
429 // 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
430 // conversion buffer for this.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 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
432 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
433 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
434 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
435 from = fp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
436 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
437 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440 from = (const char *)buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441 fromlen = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 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
443 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
444 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
445
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 if (ip->bw_first)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448 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
449
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 // 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
451 (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
452
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453 // 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
454 // 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
455 if (to == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457 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
458 tolen = save_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460 ip->bw_first = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
461 }
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 // 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
464 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
465 == (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
466 || fromlen > CONV_RESTLEN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
467 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
468 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
469 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470 }
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 // 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
473 if (fromlen > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474 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
475 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
476
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477 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
478 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
479 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481 }
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 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
484 // 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
485 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
487 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488 if (flags & FIO_ENCRYPTED)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 // 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
491 // allocated buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492 # ifdef CRYPT_NOT_INPLACE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493 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
494 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
495 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
496 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
497 # ifdef CRYPT_NOT_INPLACE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
498 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
499 else
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 char_u *outbuf;
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 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504 if (len == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
505 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
506 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
507 vim_free(outbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 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
509 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514 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
515 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
516 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519 * 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
520 * 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
521 * 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
522 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 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
525 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526 if (buf->b_mtime_read != 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 && time_differs((long)st->st_mtime, buf->b_mtime_read))
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 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
530 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
531 // 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
532 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
533 HL_ATTR(HLF_E));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534 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
535 TRUE) == 'n')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
536 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
537 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
538 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 }
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 /*
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543 * 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
544 * 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
545 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
546 static int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
547 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
548 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
549 int flags;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
550 char_u *p;
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 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
553
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554 // 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
555 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
556 return 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 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
559 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560 buf[0] = 0xef;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
561 buf[1] = 0xbb;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 buf[2] = 0xbf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563 return 3;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
564 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
565 p = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
566 (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
567 return (int)(p - buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571 static void
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
572 set_file_time(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
573 char_u *fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
574 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
575 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
576 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
577 # 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
578 struct utimbuf buf;
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 buf.actime = atime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
581 buf.modtime = mtime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
582 (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
583 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
584 # if defined(HAVE_UTIMES)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
585 struct timeval tvp[2];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
586
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587 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
588 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
589 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
590 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
591 # ifdef NeXT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592 (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
593 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 (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
595 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
596 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
597 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
598 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 #endif // UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
600
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 * 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
603 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
604 * 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
605 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
606 * 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
607 * 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
608 * 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
609 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
610 * 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
611 * "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
612 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
613 * 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
614 *
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
615 * 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
616 */
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
617 int
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
618 buf_write(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
619 buf_T *buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
620 char_u *fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
621 char_u *sfname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
622 linenr_T start,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
623 linenr_T end,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
624 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
625 // NULL!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
626 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
627 int forceit,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
628 int reset_changed,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
629 int filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
630 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
631 int fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
632 char_u *backup = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
633 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
634 int dobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
635 char_u *ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
636 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
637 char_u *s;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
638 char_u *ptr;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
639 char_u c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
640 int len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
641 linenr_T lnum;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
642 long nchars;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
643 char_u *errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
644 int errmsg_allocated = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
645 char_u *errnum = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
646 char_u *buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
647 char_u smallbuf[SMALLBUFSIZE];
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
648 char_u *backup_ext;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
649 int bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
650 long perm; // file permissions
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
651 int retval = OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 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
653 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
654 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
655 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
656 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
657 stat_T st_old;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
658 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
659 int checking_conversion;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660 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
661 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
662 #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
663 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
664 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
665 // writing everything
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
666 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
667 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
668 int attr;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669 int fileformat;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
670 int write_bin;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
671 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
672 int converted = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
673 int notconverted = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
674 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
675 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
676 int wb_flags = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
677 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
678 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
679 // backup or new file
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
680 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
681 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
682 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
683 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
684 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
685 unsigned int bkc = get_bkc_value(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
686
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
687 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
688 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
689 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
690 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
691 // 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
692 // vimrc file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
693 emsg(_(e_emptybuf));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
694 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
695 }
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 // 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
698 // security reasons.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
699 if (check_secure())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
700 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
701
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
702 // 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
703 if (STRLEN(fname) >= MAXPATHL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
704 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
705 emsg(_(e_longname));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
706 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
707 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
708
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
709 // 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
710 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
711 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
712 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
713 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
714 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
715 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
716 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
717 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
718 write_info.bw_buffer = buf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
719 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
720
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
721 // 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
722 // the line.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
723 ex_no_reprint = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
724
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
725 // 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
726 // 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
727 // 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
728 // 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
729 // 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
730 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
731 && reset_changed
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
732 && whole
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
733 && buf == curbuf
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
734 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
735 && !bt_nofilename(buf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
736 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
737 && !filtering
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
738 && (!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
739 && 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
740 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
741 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
742 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
743 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
744 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
745
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746 if (sfname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747 sfname = fname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
748 // 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
749 // 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
750 // 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
751 // 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
752 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
753 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
754 fname = sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
755 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
756
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
757 if (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
758 overwriting = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 overwriting = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
762 if (exiting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
763 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
764
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
765 ++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
766
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
767 // 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
768 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
769 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
770 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
771 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
772
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 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
775 int buf_ffname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
776 int buf_sfname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
777 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
778 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
779 int did_cmd = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
780 int nofile_err = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
781 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
782 bufref_T bufref;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
783
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
784 // Apply PRE autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785 // 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
786 // 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
787 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
788 buf_ffname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789 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
790 buf_sfname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
791 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
792 buf_fname_f = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
793 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
794 buf_fname_s = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
795
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
796 // 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
797 aucmd_prepbuf(&aco, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
798 set_bufref(&bufref, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
799
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
800 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
801 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
802 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
803 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
804 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
806 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
807 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
810 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
811 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
812 }
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 else if (filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
815 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
816 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
817 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
818 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
819 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
820 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
821 int was_changed = curbufIsChanged();
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 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
824 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
825 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
826 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
827 if (was_changed && !curbufIsChanged())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
828 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
829 // 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
830 // '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
831 // undo now sets 'modified'.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
832 u_unchanged(curbuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
833 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
834 }
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
837 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
838 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 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
840 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
841 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
842 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
843 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
844 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
845 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
848 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
849 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
850 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
851 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
852 #ifdef FEAT_QUICKFIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
853 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
854 nofile_err = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
855 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
856 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
857 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
858 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
859 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
860 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
861
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
862 // 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
863 aucmd_restbuf(&aco);
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 // 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
866 // 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
867 // 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
868 // 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
869 if (!bufref_valid(&bufref))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
870 buf = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
871 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
872 || did_cmd || nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
873 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
874 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
875 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
876 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
877 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
878 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
879 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
880 if (nofile_err)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
881 emsg(_("E676: No matching autocommands for acwrite buffer"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
882
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
883 if (nofile_err
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
884 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
885 || aborting()
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
886 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
888 // 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
889 // autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
890 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
891 if (did_cmd)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
892 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
893 if (buf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
894 // 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
895 // (can't retry anyway).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 if (overwriting)
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 // 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
900 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
901 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
902 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
903 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
904 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
905 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
906 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
907 && (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
908 // 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
909 // properly.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
910 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
911 return OK;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
912 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
913 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
914 if (!aborting())
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
915 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
916 emsg(_("E203: Autocommands deleted or unloaded buffer to be written"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
917 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
918 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
919
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
920 // 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
921 // 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
922 // 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
923 // 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
924 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
925 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
926 if (whole) // write all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
927 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
928 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
929 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
930 else // less lines
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
931 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
932 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
933 if (end < start)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
934 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
935 --no_wait_return;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
936 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
937 emsg(_("E204: Autocommand changed number of lines in unexpected way"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
938 return FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
939 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
940 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
941 }
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 // 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
944 // 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
945 if (buf_ffname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
946 ffname = buf->b_ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
947 if (buf_sfname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
948 sfname = buf->b_sfname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
949 if (buf_fname_f)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
950 fname = buf->b_ffname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
951 if (buf_fname_s)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
952 fname = buf->b_sfname;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
955 #ifdef FEAT_NETBEANS_INTG
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
956 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
957 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
958 if (whole)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
959 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
960 // 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
961 // the buffer to NetBeans.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
962 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
963 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
964 --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
965 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
966 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
967 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
968 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
969 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
970 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
971 errnum = (char_u *)"E656: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
972 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
973 buffer = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
974 goto fail;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
977 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
978 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
979 errnum = (char_u *)"E657: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
980 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
981 buffer = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
982 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
983 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
984 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
985 #endif
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 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
988 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
989 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
990 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
991 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
992 filemess(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
993 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
994 sfname,
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 fname,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
997 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
998 (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
999 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
1000
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1001 buffer = alloc(WRITEBUFSIZE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1002 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
1003 // 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
1004 // memory)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1005 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1006 buffer = smallbuf;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1007 bufsize = SMALLBUFSIZE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1008 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1009 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1010 bufsize = WRITEBUFSIZE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1011
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1012 // 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
1013 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1014 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
1015 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
1016 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1017 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
1018 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1019 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1020 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1021 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
1022 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
1023 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1024 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
1025 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1026 errnum = (char_u *)"E502: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1027 errmsg = (char_u *)_("is a directory");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1028 goto fail;
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 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
1031 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1032 errnum = (char_u *)"E503: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1033 errmsg = (char_u *)_("is not a file or writable device");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1034 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1035 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1036 // 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
1037 // 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
1038 device = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1039 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1040 perm = -1;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1043 #else // !UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1044 // 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
1045 c = mch_nodetype(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1046 if (c == NODE_OTHER)
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 errnum = (char_u *)"E503: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1049 errmsg = (char_u *)_("is not a file or writable device");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1050 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1051 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1052 if (c == NODE_WRITABLE)
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 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1055 // 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
1056 // 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
1057 if (!p_odev)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1058 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1059 errnum = (char_u *)"E796: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1060 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1061 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1062 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1063 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1064 device = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1065 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1066 perm = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1067 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1068 else
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 perm = mch_getperm(fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1071 if (perm < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1072 newfile = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1073 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
1074 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1075 errnum = (char_u *)"E502: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1076 errmsg = (char_u *)_("is a directory");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1077 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1078 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1079 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1080 (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
1081 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1082 #endif // !UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1083
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1084 if (!device && !newfile)
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 // 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
1087 // 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
1088 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
1089
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1090 if (!forceit && file_readonly)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1091 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1092 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
1093 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1094 errnum = (char_u *)"E504: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1095 errmsg = (char_u *)_(err_readonly);
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1098 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1099 errnum = (char_u *)"E505: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1100 errmsg = (char_u *)_("is read-only (add ! to override)");
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 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1103 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1104
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1105 // 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
1106 if (overwriting)
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 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
1109 if (retval == FAIL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1110 goto fail;
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1113
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1114 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1115 // 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
1116 if (!newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1117 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
1118 #endif
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 // 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
1121 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
1122 #ifdef FEAT_WILDIGN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1123 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
1124 dobackup = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1125 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1126
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1127 // 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
1128 // 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
1129 // abort it.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1130 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
1131 got_int = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1132
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1133 // 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
1134 buf->b_saving = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1135
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1136 // 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
1137 // '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
1138 // 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
1139 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1140 // 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
1141 // 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
1142 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
1143 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1144 #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
1145 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1146 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1147
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1148 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
1149 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1150 #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
1151 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
1152 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1153 int i;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1154
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1155 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1156 // 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
1157 // - 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
1158 // - 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
1159 // - 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
1160 // - 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
1161 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
1162 || 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
1163 || 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
1164 || 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
1165 # ifndef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1166 || 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
1167 || 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
1168 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1169 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1170 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1171 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1172 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1173 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1174 // 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
1175 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
1176 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1177 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1178 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1179 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1180 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1181 // 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
1182 // 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
1183 // 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
1184 // arbitrary numbers).
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1185 STRCPY(IObuff, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1186 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
1187 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1188 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
1189 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
1190 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1191 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1192 fd = mch_open((char *)IObuff,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1193 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1194 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
1195 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1196 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1197 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1198 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1199 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1200 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
1201 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1202 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
1203 || 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
1204 || 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
1205 || (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
1206 backup_copy = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1207 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1208 // 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
1209 // 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
1210 close(fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1211 mch_remove(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1212 # ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1213 // 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
1214 // 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
1215 // second.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1216 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1217 int try;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1218
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1219 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
1220 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1221 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
1222 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1223 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
1224 mch_remove(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1225 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1226 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1227 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1228 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1229 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1230 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1231
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1232 // 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
1233 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
1234 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1235 # ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1236 int lstat_res;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1237
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1238 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
1239
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1240 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1241 if ((bkc & BKC_BREAKSYMLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1242 && lstat_res == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1243 && 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
1244 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1245
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1246 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1247 if ((bkc & BKC_BREAKHARDLINK)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1248 && 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
1249 && (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
1250 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1251 # else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1252 # if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1253 // Symlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1254 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
1255 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1256
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1257 // Hardlinks.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1258 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
1259 backup_copy = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1260 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1261 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1262 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1263
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1264 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1265
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1266 // 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
1267 if (*p_bex == NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1268 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
1269 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1270 backup_ext = p_bex;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1271
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1272 if (backup_copy
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1273 && (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
1274 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1275 int bfd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1276 char_u *copybuf, *wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1277 int some_error = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1278 stat_T st_new;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1279 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1280 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1281 #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
1282 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1283 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1284 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1285 int did_set_shortname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1286 mode_t umask_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1287 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1288
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1289 copybuf = alloc(WRITEBUFSIZE + 1);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1290 if (copybuf == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1291 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1292 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
1293 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1294 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1295
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1296 // 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
1297 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1298 // 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
1299 // 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
1300 // - 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
1301 // - 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
1302 // - 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
1303 //
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1304 // 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
1305 // 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
1306 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1307 while (*dirp)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1308 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1309 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1310 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
1311 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
1312 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
1313 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1314
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1315 // 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
1316 (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
1317
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1318 #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
1319 p = copybuf + STRLEN(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1320 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
1321 // 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
1322 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
1323 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1324 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
1325 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1326 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1327 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1328 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
1329 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1330 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1331 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
1332 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1333 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1334
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1335 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1336 did_set_shortname = FALSE;
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 // 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
1340 for (;;)
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 // 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
1343 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1344 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
1345 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1346 if (backup == 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 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1349 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
1350 goto nobackup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1351 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1352
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1353 // 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
1354 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
1355 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1356 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1357 // 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
1358 // 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
1359 // 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
1360 // 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
1361 // 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
1362 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
1363 && 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
1364 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1365 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
1366 // 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
1367 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
1368 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1369 buf->b_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1370 did_set_shortname = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1371 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1372 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1373 // 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
1374 if (did_set_shortname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1375 buf->b_shortname = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1376 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1377 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1378 #endif
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 // 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
1381 // 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
1382 // 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
1383 if (!p_bk)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1384 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1385 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
1386 - STRLEN(backup_ext);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1387 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
1388 wp = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1389 *wp = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1390 while (*wp > 'a'
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1391 && 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
1392 --*wp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1393 // 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
1394 if (*wp == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1395 VIM_CLEAR(backup);
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1398 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1399 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1400 vim_free(rootname);
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 // 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
1403 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1404 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1405 // 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
1406 mch_remove(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1407 // 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
1408 // 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
1409 // 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
1410 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1411 umask_save = umask(0);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1412 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1413 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
1414 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
1415 perm & 0777);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1416 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1417 (void)umask(umask_save);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1418 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1419 if (bfd < 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1420 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1421 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1422 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1423 // 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
1424 // 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
1425 // above.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1426 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1427 (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
1428 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1429 // 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
1430 // 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
1431 // 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
1432 // others.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1433 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
1434 # 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
1435 && 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
1436 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1437 )
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1438 mch_setperm(backup,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1439 (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
1440 # 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
1441 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
1442 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1443 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1444
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1445 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1446 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
1447 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
1448 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
1449 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
1450 WRITEBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1451 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1452 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
1453 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1454 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1455 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1456 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1457 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1458 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1459 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1460 errmsg = (char_u *)_(e_interr);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1461 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1462 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1463 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1464
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1465 if (close(bfd) < 0 && errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1466 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1467 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
1468 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1469 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1470 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
1471 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1472 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1473 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
1474 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1475 #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
1476 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
1477 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1478 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1479 }
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 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1482 nobackup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1483 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
1484 vim_free(copybuf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1485
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1486 if (backup == NULL && errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1487 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1488 // 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
1489 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
1490 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1491 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1492 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1493 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1494 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1495 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1496 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1497 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1498 char_u *dirp;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1499 char_u *p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1500 char_u *rootname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1501
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1502 // 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
1503
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1504 // 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
1505 // 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
1506 // 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
1507 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
1508 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1509 errnum = (char_u *)"E504: ";
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1510 errmsg = (char_u *)_(err_readonly);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1511 goto fail;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1514 // 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
1515 // 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
1516 // that works is used.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1517 dirp = p_bdir;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1518 while (*dirp)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1519 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1520 // 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
1521 (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
1522
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1523 #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
1524 p = IObuff + STRLEN(IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1525 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
1526 // 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
1527 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
1528 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1529 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
1530 vim_free(p);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1531 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1532 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1533 if (backup == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1534 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1535 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
1536 if (rootname == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1537 backup = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1538 else
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 backup = buf_modname(
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1541 (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
1542 rootname, backup_ext, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1543 vim_free(rootname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1544 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1545 }
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 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1548 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1549 // 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
1550 // 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
1551 // 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
1552 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
1553 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1554 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
1555 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
1556 p = backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1557 *p = 'z';
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1558 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
1559 --*p;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1560 // 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
1561 if (*p == 'a')
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1562 VIM_CLEAR(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1563 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1564 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1565 if (backup != NULL)
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 // 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
1568 // 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
1569 // 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
1570 // '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
1571
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1572 // 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
1573 // works, quit here.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1574 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
1575 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1576
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1577 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
1578 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1579 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1580 if (backup == NULL && !forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1581 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1582 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1583 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1584 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1585 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1586 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1587
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1588 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1589 // 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
1590 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
1591 && 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
1592 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1593 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1594 (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
1595 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1596 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1597 #endif
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 // 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
1600 // 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
1601 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
1602 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1603 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
1604 #ifdef FEAT_TITLE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1605 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
1606 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1607 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
1608 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1609
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1610 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
1611 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
1612 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
1613 start = end + 1;
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 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
1616 // 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
1617 // 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
1618 // the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1619 // 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
1620 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
1621 && !(exiting && backup != NULL))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1622 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1623 ml_preserve(buf, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1624 if (got_int)
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 errmsg = (char_u *)_(e_interr);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1627 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1628 }
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1631 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1632 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
1633 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1634 // 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
1635 // multi-byte conversion.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1636 wfname = fname;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1637
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1638 // 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
1639 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
1640 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1641 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
1642 fenc = enc_canonize(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1643 fenc_tofree = fenc;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1644 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1645 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1646 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
1647
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1648 // 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
1649 converted = need_conversion(fenc);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1650
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1651 // 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
1652 // 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
1653 // 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
1654 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
1655 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1656 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
1657 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
1658 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1659 // 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
1660 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
1661 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
1662 else // FIO_UCS4
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1663 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
1664 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
1665 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
1666 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1667 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1668 }
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 #ifdef MSWIN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1671 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
1672 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1673 // 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
1674 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
1675 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
1676 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
1677 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1678 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1679 #endif
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 #ifdef MACOS_CONVERT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1682 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
1683 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1684 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
1685 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
1686 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
1687 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1688 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1689 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1690
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1691 #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
1692 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
1693 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1694 # ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1695 // 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
1696 // internally.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1697 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
1698 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
1699 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
1700 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1701 // 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
1702 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
1703 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
1704 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
1705 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1706 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
1707 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1708 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1709 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1710 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1711 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1712
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1713 # ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1714 // 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
1715 // 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
1716 // overwrite the original file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1717 if (*p_ccv != NUL)
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 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
1720 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
1721 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1722 errmsg = (char_u *)_("E214: Can't find temp file for writing");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1723 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1724 }
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 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1727 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1728 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1729 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
1730 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1731 && write_info.bw_iconv_fd == (iconv_t)-1
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1732 # endif
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 && wfname == fname
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 )
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 if (!forceit)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1739 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1740 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1741 goto restore_backup;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1742 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1743 notconverted = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1744 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1745
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1746 // 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
1747 // 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
1748 // 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
1749 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
1750 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1751 // 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
1752 // - there is no conversion
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1753 // - 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
1754 // failure.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1755 if (!converted || dobackup)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1756 checking_conversion = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1757
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1758 if (checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1759 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1760 // 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
1761 fd = -1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1762 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
1763 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1764 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1765 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1766 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1767 # 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
1768 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1769 # 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
1770 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1771 // 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
1772 // 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
1773 // 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
1774 // 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
1775 // 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
1776 // 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
1777 // 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
1778 // FALSE.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1779 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
1780 ? (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
1781 : (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
1782 , 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
1783 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1784 // 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
1785 // 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
1786 // 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
1787 if (errmsg == NULL)
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 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1790 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1791
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1792 // 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
1793 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
1794 || (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
1795 && (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
1796 || 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
1797 errmsg = (char_u *)_("E166: Can't open linked file for writing");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1798 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1799 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1800 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1801 errmsg = (char_u *)_("E212: Can't open file for writing");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1802 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
1803 && perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1804 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1805 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1806 // 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
1807 // writable after all
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1808 if (!(perm & 0200))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1809 made_writable = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1810 perm |= 0200;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1811 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
1812 || 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
1813 perm &= 0777;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1814 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1815 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
1816 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1817 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1818 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1819 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1820 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1821
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1822 restore_backup:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1823 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1824 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1825
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1826 // 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
1827 // 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
1828 // 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
1829 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
1830 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1831 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1832 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1833 // 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
1834 // 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
1835 // 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
1836 // 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
1837
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1838 // 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
1839 // place
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1840 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
1841 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1842 // 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
1843 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
1844 mch_remove(backup);
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 else
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 // 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
1849 vim_rename(backup, fname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1850 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1851 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1852
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1853 // if 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
1854 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
1855 end = 0;
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1858 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1859 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1860 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1861 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1862 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
1863
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1864 #if defined(UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1865 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1866 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1867
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1868 // 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
1869 // any changes.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1870 if (overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1871 && (!dobackup || backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1872 && fname == wfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1873 && perm >= 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1874 && 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
1875 && 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
1876 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1877 close(fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1878 errmsg = (char_u *)_("E949: File changed while writing");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1879 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1880 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1881 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1882 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1883 #ifdef HAVE_FTRUNCATE
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1884 if (!append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1885 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
1886 #endif
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 #if defined(MSWIN)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1889 if (backup != NULL && overwriting && !append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1890 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1891 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1892 (void)mch_copy_file_attribute(wfname, backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1893 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1894 (void)mch_copy_file_attribute(backup, wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1895 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1896
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1897 if (!overwriting && !append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1898 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1899 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
1900 (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
1901 // Should copy resource fork
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1904
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1905 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1906 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
1907 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1908 char_u *header;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1909 int header_len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1910
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1911 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
1912 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
1913 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
1914 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
1915 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1916 else
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 // 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
1919 // 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
1920 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
1921 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
1922 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
1923 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
1924 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1925 wb_flags |= FIO_ENCRYPTED;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1926 vim_free(header);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1927 }
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1930 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1931 errmsg = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1932
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1933 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
1934 nchars = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1935
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1936 // 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
1937 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
1938 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
1939 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1940 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
1941
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1942 // 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
1943 // 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
1944 // 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
1945 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
1946 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1947 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
1948 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
1949 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1950 // 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
1951 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
1952 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
1953 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1954 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1955 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
1956 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1957 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1958 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
1959
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1960 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1961 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
1962 && overwriting
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1963 && !append
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1964 && !filtering
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1965 && reset_changed
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1966 && !checking_conversion);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1967 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1968 // 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
1969 sha256_start(&sha_ctx);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1970 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1971
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1972 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
1973 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
1974 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
1975 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1976 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1977 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
1978 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1979 // 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
1980 // Keep it fast!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1981 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
1982 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1983 if (write_undo_file)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1984 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
1985 (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
1986 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1987 while ((c = *++ptr) != NUL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1988 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1989 if (c == NL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1990 *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
1991 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
1992 *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
1993 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1994 *s = c;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1995 ++s;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1996 if (++len != bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1997 continue;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1998 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
1999 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2000 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
2001 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2002 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2003 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2004 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2005 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2006 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
2007 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2008 // 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
2009 if (end == 0
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2010 || (lnum == end
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2011 && (write_bin || !buf->b_p_fixeol)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2012 && (lnum == buf->b_no_eol_lnum
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2013 || (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
2014 && !buf->b_p_eol))))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2015 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2016 ++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
2017 no_eol = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2018 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2019 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2020 if (fileformat == EOL_UNIX)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2021 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2022 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2023 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2024 *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
2025 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
2026 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2027 if (++len == bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2028 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2029 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
2030 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2031 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
2032 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2033 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2034 nchars += bufsize;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2035 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2036 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2037 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2038 *s++ = NL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2039 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2040 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2041 if (++len == bufsize && end)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2042 {
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2052 ui_breakcheck();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2053 if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2054 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2055 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
2056 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2057 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2058 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2059 #ifdef VMS
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2060 // 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
2061 // 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
2062 // This is much slower!
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2063 // 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
2064 // 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
2065 // 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
2066 // With other RMS structures it works perfect without this fix.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2067 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
2068 || ((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
2069 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2070 int b2write;
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 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
2073 ? MIN(4096, bufsize)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2074 : 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
2075
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2076 b2write = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2077 while (b2write > 0)
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 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
2080 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
2081 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2082 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2083 break;
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 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
2086 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2087 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
2088 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2089 s = buffer;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2090 len = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2091 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2092 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2093 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2094 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
2095 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2096 write_info.bw_len = len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2097 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
2098 end = 0; // write error
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2099 nchars += len;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2100 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2101
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2102 // 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
2103 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
2104 break;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2105
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2106 // 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
2107 // really write the buffer.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2108 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2109
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2110 // 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
2111 // encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2112 if (!checking_conversion)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2113 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2114 #if defined(UNIX) && defined(HAVE_FSYNC)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2115 // On many journalling file systems there is a bug that causes both the
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2116 // 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
2117 // 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
2118 // 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
2119 // 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
2120 // 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
2121 // 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
2122 // 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
2123 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
2124 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2125 errmsg = (char_u *)_(e_fsync);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2126 end = 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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2129
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2130 #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
2131 // 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
2132 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2133 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
2134 #endif
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 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2137 // 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
2138 // 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
2139 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
2140 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2141 # ifdef HAVE_FCHOWN
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2142 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2143
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2144 // 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
2145 // permission or ACL stuff.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2146 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
2147 || 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
2148 || 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
2149 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2150 // 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
2151 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
2152 // 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
2153 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
2154 perm &= ~070;
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 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2157 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2158 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2159 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
2160 // 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
2161 buf_setino(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2162 #endif
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 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2165 if (made_writable)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2166 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
2167 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2168 #ifdef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2169 // 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
2170 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2171 (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
2172 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2173 if (close(fd) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2174 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2175 errmsg = (char_u *)_("E512: Close failed");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2176 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2177 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2178
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2179 #ifndef HAVE_FCHMOD
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2180 // 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
2181 if (perm >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2182 (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
2183 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2184 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2185 // 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
2186 // 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
2187 // 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
2188 // 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
2189 // 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
2190 // 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
2191 # 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
2192 if (!backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2193 # endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2194 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
2195 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2196 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2197 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
2198 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2199 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
2200 buf->b_cryptstate = NULL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2201 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2202 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2203
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2204 #if defined(FEAT_EVAL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2205 if (wfname != fname)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2206 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2207 // 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
2208 // 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
2209 if (end != 0)
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 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
2212 fenc, wfname, fname) == FAIL)
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 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
2215 end = 0;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2216 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2217 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2218 mch_remove(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2219 vim_free(wfname);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2220 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2221 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2222 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2223
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2224 if (end == 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2225 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2226 // Error encountered.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2227 if (errmsg == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2228 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2229 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
2230 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2231 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
2232 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2233 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2234 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2235 errmsg_allocated = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2236 errmsg = alloc(300);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2237 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"),
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2238 (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
2239 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2240 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2241 else if (got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2242 errmsg = (char_u *)_(e_interr);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2243 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2244 errmsg = (char_u *)_("E514: write error (file system full?)");
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2245 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2246
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2247 // 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
2248 // 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
2249 // 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
2250 // second time.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2251 // 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
2252 // 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
2253 // 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
2254 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2255 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2256 if (backup_copy)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2257 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2258 // 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
2259 // 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
2260 if (got_int)
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 msg(_(e_interr));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2263 out_flush();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2264 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2265 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
2266 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2267 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
2268 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
2269 perm & 0777)) >= 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2270 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2271 // copy the file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2272 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
2273 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
2274 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
2275 SMALLBUFSIZE)) > 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2276 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
2277 break;
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 (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
2280 && 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
2281 end = 1; // success
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2282 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2283 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
2284 }
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 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2287 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2288 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
2289 end = 1;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2290 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2291 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2292 goto fail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2293 }
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 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
2296 --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
2297
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2298 #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
2299 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
2300 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2301 if (!filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2302 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2303 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
2304 c = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2305 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
2306 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2307 STRCAT(IObuff, _(" CONVERSION ERROR"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2308 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2309 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
2310 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
2311 (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
2312 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2313 else if (notconverted)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2314 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2315 STRCAT(IObuff, _("[NOT converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2316 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2317 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2318 else if (converted)
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 STRCAT(IObuff, _("[converted]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2321 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2322 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2323 if (device)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2324 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2325 STRCAT(IObuff, _("[Device]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2326 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2327 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2328 else if (newfile)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2329 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2330 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2331 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2332 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2333 if (no_eol)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2334 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2335 msg_add_eol();
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2336 c = TRUE;
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 // 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
2339 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
2340 c = TRUE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2341 #ifdef FEAT_CRYPT
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2342 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
2343 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2344 crypt_append_msg(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2345 c = TRUE;
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 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2348 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
2349 if (!shortmess(SHM_WRITE))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2350 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2351 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2352 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
2353 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2354 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
2355 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2356
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2357 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
2358 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2359
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2360 // 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
2361 // 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
2362 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
2363 && !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
2364 && (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
2365 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2366 unchanged(buf, TRUE, FALSE);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2367 // b:changedtick is may be incremented in unchanged() but that
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2368 // should not trigger a TextChanged event.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2369 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
2370 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
2371 u_unchanged(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2372 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
2373 }
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 // 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
2376 // 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
2377 if (overwriting)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2378 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2379 ml_timestamp(buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2380 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2381 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
2382 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2383 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
2384 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2385
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2386 // 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
2387 // 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
2388 if (*p_pm && dobackup)
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 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
2391 fname, p_pm, FALSE);
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 if (backup != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2394 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2395 stat_T st;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2396
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2397 // 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
2398 // 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
2399 if (org == NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2400 emsg(_("E205: Patchmode: can't save original file"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2401 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
2402 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2403 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
2404 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
2405 #ifdef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2406 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
2407 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2408 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2409 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2410 // 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
2411 // created.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2412 else
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 int empty_fd;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2415
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2416 if (org == NULL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2417 || (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
2418 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
2419 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
2420 emsg(_("E206: patchmode: can't touch empty original file"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2421 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2422 close(empty_fd);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2423 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2424 if (org != NULL)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2425 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2426 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
2427 vim_free(org);
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
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2431 // 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
2432 // conversion error.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2433 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
2434 && mch_remove(backup) != 0)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2435 emsg(_("E207: Can't delete backup file"));
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2436
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2437 goto nofail;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2438
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2439 // 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
2440 fail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2441 --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
2442 nofail:
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2443
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2444 // 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
2445 buf->b_saving = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2446
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2447 vim_free(backup);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2448 if (buffer != smallbuf)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2449 vim_free(buffer);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2450 vim_free(fenc_tofree);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2451 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
2452 #ifdef USE_ICONV
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2453 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
2454 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2455 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
2456 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
2457 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2458 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2459 #ifdef HAVE_ACL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2460 mch_free_acl(acl);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2461 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2462
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2463 if (errmsg != NULL)
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 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
2466
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2467 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
2468 msg_add_fname(buf,
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2469 #ifndef UNIX
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2470 sfname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2471 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2472 fname
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2473 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2474 ); // 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
2475 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
2476 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
2477 // 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
2478 // 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
2479 if (errnum != 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 STRMOVE(IObuff + numlen, IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2482 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
2483 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2484 STRCAT(IObuff, errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2485 emsg((char *)IObuff);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2486 if (errmsg_allocated)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2487 vim_free(errmsg);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2488
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2489 retval = FAIL;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2490 if (end == 0)
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 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
2493 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2494 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
2495 attr | MSG_HIST);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2496
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2497 // 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
2498 // prompt when writing again.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2499 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
2500 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2501 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
2502 buf->b_mtime_read = buf->b_mtime;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2503 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2504 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2505 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2506 msg_scroll = msg_save;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2507
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2508 #ifdef FEAT_PERSISTENT_UNDO
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2509 // 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
2510 // file.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2511 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
2512 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2513 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
2514
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2515 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
2516 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
2517 }
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2518 #endif
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 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2521 if (!should_abort(retval))
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2522 #else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2523 if (!got_int)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2524 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2525 {
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2526 aco_save_T aco;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2527
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2528 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
2529
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2530 // Apply POST autocommands.
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2531 // 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
2532 aucmd_prepbuf(&aco, buf);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2533
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2534 if (append)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2535 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
2536 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2537 else if (filtering)
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2538 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
2539 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2540 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
2541 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
2542 FALSE, curbuf, eap);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2543 else
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2544 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
2545 FALSE, curbuf, eap);
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 // 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
2548 aucmd_restbuf(&aco);
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2549
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2550 #ifdef FEAT_EVAL
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2551 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
2552 retval = FALSE;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2553 #endif
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2554 }
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 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
2557
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2558 return retval;
e2be5a6485f5 patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2559 }