blob: 4bd773e0bd944614fa64e23bd878a5d03e48d79e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Christian Brabandtf573c6e2021-06-20 14:02:16 +020016#ifdef FEAT_SODIUM
17# include <sodium.h>
18#endif
19
Bram Moolenaare3f915d2020-07-14 23:02:44 +020020#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010021# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010023#if (defined(UNIX) || defined(VMS)) && defined(FEAT_EVAL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020024# include <pwd.h>
25# include <grp.h>
26#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010027#if defined(VMS) && defined(HAVE_XOS_R_H)
28# include <x11/xos_r.h>
29#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar217e1b82019-12-01 21:41:28 +010031// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000032#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
Bram Moolenaar9d8bfae2020-09-02 21:21:35 +020034#if defined(__hpux) && !defined(HAVE_DIRFD)
35# define dirfd(x) ((x)->__dd_fd)
36# define HAVE_DIRFD
37#endif
38
Bram Moolenaarf077db22019-08-13 00:18:24 +020039static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010040#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020044static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010047static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000048static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000049
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020050#ifdef FEAT_EVAL
51static int readdirex_sort;
52#endif
53
Bram Moolenaar473952e2019-09-28 16:30:04 +020054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010055filemess(
56 buf_T *buf,
57 char_u *name,
58 char_u *s,
59 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
61 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020062 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64 if (msg_silent != 0)
65 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010066 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020067
Bram Moolenaar217e1b82019-12-01 21:41:28 +010068 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020069 if (STRLEN(IObuff) > IOSIZE - 100)
70 IObuff[IOSIZE - 100] = NUL;
71
72 // Avoid an over-long translation to cause trouble.
73 STRNCAT(IObuff, s, 99);
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 /*
76 * For the first message may have to start a new line.
77 * For further ones overwrite the previous one, reset msg_scroll before
78 * calling filemess().
79 */
80 msg_scroll_save = msg_scroll;
81 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
82 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010083 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 check_for_delay(FALSE);
85 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020086 if (prev_msg_col != 0 && msg_col == 0)
87 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 msg_scroll = msg_scroll_save;
89 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010090 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
92 msg_clr_eos();
93 out_flush();
94 msg_scrolled_ign = FALSE;
95}
96
97/*
98 * Read lines from file "fname" into the buffer after line "from".
99 *
100 * 1. We allocate blocks with lalloc, as big as possible.
101 * 2. Each block is filled with characters from the file with a single read().
102 * 3. The lines are inserted in the buffer with ml_append().
103 *
104 * (caller must check that fname != NULL, unless READ_STDIN is used)
105 *
106 * "lines_to_skip" is the number of lines that must be skipped
107 * "lines_to_read" is the number of lines that are appended
108 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
109 *
110 * flags:
111 * READ_NEW starting to edit a new buffer
112 * READ_FILTER reading filter output
113 * READ_STDIN read from stdin instead of a file
114 * READ_BUFFER read from curbuf instead of a file (converting after reading
115 * stdin)
116 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200117 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200118 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100120 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 */
122 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100123readfile(
124 char_u *fname,
125 char_u *sfname,
126 linenr_T from,
127 linenr_T lines_to_skip,
128 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100129 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100130 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 int fd = 0;
133 int newfile = (flags & READ_NEW);
134 int check_readonly;
135 int filtering = (flags & READ_FILTER);
136 int read_stdin = (flags & READ_STDIN);
137 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200138 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000139 int set_options = newfile || read_buffer
140 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100141 linenr_T read_buf_lnum = 1; // next line to read from curbuf
142 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 char_u c;
144 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100145 char_u *ptr = NULL; // pointer into read buffer
146 char_u *buffer = NULL; // read buffer
147 char_u *new_buffer = NULL; // init to shut up gcc
148 char_u *line_start = NULL; // init to shut up gcc
149 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 colnr_T len;
151 long size = 0;
152 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200153 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 int skip_read = FALSE;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200155 off_T filesize_disk = 0; // file size read from disk
156 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_CRYPT
158 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200159 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200161#ifdef FEAT_PERSISTENT_UNDO
162 context_sha256_T sha_ctx;
163 int read_undo_file = FALSE;
164#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100165 int split = 0; // number of split lines
166#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100168 int error = FALSE; // errors encountered
169 int ff_error = EOL_UNKNOWN; // file format with errors
170 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#ifdef UNIX
172 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100173 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#else
175 int perm;
176#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100177 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200179 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 int file_readonly;
181 linenr_T skip_count = 0;
182 linenr_T read_count = 0;
183 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100184 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
185 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100186 int try_mac;
187 int try_dos;
188 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100191 linenr_T conv_error = 0; // line nr with conversion error
192 linenr_T illegal_byte = 0; // line nr with illegal byte
193 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
194 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000195 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100196 // BAD_KEEP, BAD_DROP or character to
197 // replace with
198 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100200 char_u *fenc; // fileencoding to use
201 int fenc_alloced; // fenc_next is in allocated memory
202 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 int advance_fenc = FALSE;
204 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100205#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100206 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100207# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100208 int did_iconv = FALSE; // TRUE when iconv() failed and trying
209 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100211#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100212 int converted = FALSE; // TRUE if conversion done
213 int notconverted = FALSE; // TRUE if conversion wanted but it
214 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100216 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100217 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200218 buf_T *old_curbuf;
219 char_u *old_b_ffname;
220 char_u *old_b_fname;
221 int using_b_ffname;
222 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200223 static char *msg_is_a_directory = N_("is a directory");
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200224 int eof;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200225
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100226 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200227
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100228 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
230 /*
231 * If there is no file name yet, use the one for the read file.
232 * BF_NOTEDITED is set to reflect this.
233 * Don't do this for a read from a filter.
234 * Only do this when 'cpoptions' contains the 'f' flag.
235 */
236 if (curbuf->b_ffname == NULL
237 && !filtering
238 && fname != NULL
239 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
240 && !(flags & READ_DUMMY))
241 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000242 if (set_rw_fname(fname, sfname) == FAIL)
243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100246 // Remember the initial values of curbuf, curbuf->b_ffname and
247 // curbuf->b_fname to detect whether they are altered as a result of
248 // executing nasty autocommands. Also check if "fname" and "sfname"
249 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200250 old_curbuf = curbuf;
251 old_b_ffname = curbuf->b_ffname;
252 old_b_fname = curbuf->b_fname;
253 using_b_ffname = (fname == curbuf->b_ffname)
254 || (sfname == curbuf->b_ffname);
255 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200256
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100257 // After reading a file the cursor line changes but we don't want to
258 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000259 ex_no_reprint = TRUE;
260
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100261 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000262 need_fileinfo = FALSE;
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 /*
265 * For Unix: Use the short file name whenever possible.
266 * Avoids problems with networks and when directory names are changed.
267 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
268 * another directory, which we don't detect.
269 */
270 if (sfname == NULL)
271 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200272#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 fname = sfname;
274#endif
275
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 /*
277 * The BufReadCmd and FileReadCmd events intercept the reading process by
278 * executing the associated commands instead.
279 */
280 if (!filtering && !read_stdin && !read_buffer)
281 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100282 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100284 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
286 curbuf->b_op_start.col = 0;
287
288 if (newfile)
289 {
290 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
291 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200292 {
293 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200295 if (aborting())
296 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200298 // The BufReadCmd code usually uses ":read" to get the text and
299 // perhaps ":file" to change the buffer name. But we should
300 // consider this to work like ":edit", thus reset the
301 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
302 // same file.
303 if (status == OK)
304 curbuf->b_flags &= ~BF_NOTEDITED;
305 return status;
306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 }
308 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
309 FALSE, NULL, eap))
310#ifdef FEAT_EVAL
311 return aborting() ? FAIL : OK;
312#else
313 return OK;
314#endif
315
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100316 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
319 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100320 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100322 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000324 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200326 size_t namelen = STRLEN(fname);
327
328 // If the name is too long we might crash further on, quit here.
329 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000330 {
331 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
332 msg_end();
333 msg_scroll = msg_save;
334 return FAIL;
335 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200336
337 // If the name ends in a path separator, we can't open it. Check here,
338 // because reading the file may actually work, but then creating the
339 // swap file may destroy it! Reported on MS-DOS and Win 95.
340 if (after_pathsep(fname, fname + namelen))
341 {
342 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
343 msg_end();
344 msg_scroll = msg_save;
345 return FAIL;
346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200349 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100351#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200352 /*
353 * On Unix it is possible to read a directory, so we have to
354 * check for it before the mch_open().
355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100357 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
358 && !S_ISFIFO(perm) // ... or fifo
359 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000360# ifdef OPEN_CHR_FILES
361 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100362 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 )
365 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100366 int retval = FAIL;
367
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100369 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200370 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100371 retval = NOTDONE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 else
374 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
375 msg_end();
376 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100377 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200379#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100380#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000381 /*
382 * MS-Windows allows opening a device, but we will probably get stuck
383 * trying to read it.
384 */
385 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
386 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000387 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000388 msg_end();
389 msg_scroll = msg_save;
390 return FAIL;
391 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000392#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200393 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000394
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200396 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
398 /*
399 * When opening a new file we take the readonly flag from the file.
400 * Default is r/w, can be set to r/o below.
401 * Don't reset it when in readonly mode
402 * Only set/reset b_p_ro when BF_CHECK_RO is set.
403 */
404 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000405 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 curbuf->b_p_ro = FALSE;
407
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200408 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100410 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 if (mch_stat((char *)fname, &st) >= 0)
412 {
413 buf_store_time(curbuf, &st, fname);
414 curbuf->b_mtime_read = curbuf->b_mtime;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200415 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#ifdef UNIX
417 /*
418 * Use the protection bits of the original file for the swap file.
419 * This makes it possible for others to read the name of the
420 * edited file from the swapfile, but only if they can read the
421 * edited file.
422 * Remove the "write" and "execute" bits for group and others
423 * (they must not write the swapfile).
424 * Add the "read" and "write" bits for the user, otherwise we may
425 * not be able to write to the file ourselves.
426 * Setting the bits is done below, after creating the swap file.
427 */
428 swap_mode = (st.st_mode & 0644) | 0600;
429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#ifdef VMS
431 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000432 curbuf->b_fab_rat = st.st_fab_rat;
433 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#endif
435 }
436 else
437 {
438 curbuf->b_mtime = 0;
439 curbuf->b_mtime_read = 0;
440 curbuf->b_orig_size = 0;
441 curbuf->b_orig_mode = 0;
442 }
443
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100444 // Reset the "new file" flag. It will be set again below when the
445 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
447 }
448
449/*
450 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100451 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 */
453 file_readonly = FALSE;
454 if (read_stdin)
455 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100456#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100457 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 setmode(0, O_BINARY);
459#endif
460 }
461 else if (!read_buffer)
462 {
463#ifdef USE_MCH_ACCESS
464 if (
465# ifdef UNIX
466 !(perm & 0222) ||
467# endif
468 mch_access((char *)fname, W_OK))
469 file_readonly = TRUE;
470 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
471#else
472 if (!newfile
473 || readonlymode
474 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
475 {
476 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100477 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
479 }
480#endif
481 }
482
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {
485#ifndef UNIX
486 int isdir_f;
487#endif
488 msg_scroll = msg_save;
489#ifndef UNIX
490 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100491 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 */
493 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 if (isdir_f)
496 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200497 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 }
500 else
501#endif
502 if (newfile)
503 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200504 if (perm < 0
505#ifdef ENOENT
506 && errno == ENOENT
507#endif
508 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
510 /*
511 * Set the 'new-file' flag, so that when the file has
512 * been created by someone else, a ":w" will complain.
513 */
514 curbuf->b_flags |= BF_NEW;
515
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100516 // Create a swap file now, so that other Vims are warned
517 // that we are editing this file. Don't do this for a
518 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519#ifdef FEAT_QUICKFIX
520 if (!bt_dontwrite(curbuf))
521#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100524 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000525 if (curbuf != old_curbuf
526 || (using_b_ffname
527 && (old_b_ffname != curbuf->b_ffname))
528 || (using_b_fname
529 && (old_b_fname != curbuf->b_fname)))
530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000532 return FAIL;
533 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000534 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000535 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200536 filemess(curbuf, sfname,
537 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000538 else
539 filemess(curbuf, sfname,
540 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100542 // Even though this is a new file, it might have been
543 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 check_marks_read();
545#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200547 if (eap != NULL)
548 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
550 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100551 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 save_file_ff(curbuf);
553
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100554#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100555 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return FAIL;
557#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100558 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 }
560 else
561 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000562 filemess(curbuf, sfname, (char_u *)(
563# ifdef EFBIG
564 (errno == EFBIG) ? _("[File too big]") :
565# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200566# ifdef EOVERFLOW
567 (errno == EOVERFLOW) ? _("[File too big]") :
568# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000569 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100570 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
572 }
573
574 return FAIL;
575 }
576
577 /*
578 * Only set the 'ro' flag for readonly files the first time they are
579 * loaded. Help files always get readonly mode
580 */
581 if ((check_readonly && file_readonly) || curbuf->b_help)
582 curbuf->b_p_ro = TRUE;
583
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000584 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100586 // Don't change 'eol' if reading from buffer as it will already be
587 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000588 if (!read_buffer)
589 {
590 curbuf->b_p_eol = TRUE;
591 curbuf->b_start_eol = TRUE;
592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000594 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100597 // Create a swap file now, so that other Vims are warned that we are
598 // editing this file.
599 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600#ifdef FEAT_QUICKFIX
601 if (!bt_dontwrite(curbuf))
602#endif
603 {
604 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000605 if (!read_stdin && (curbuf != old_curbuf
606 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
607 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100609 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000610 if (!read_buffer)
611 close(fd);
612 return FAIL;
613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100615 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000616 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
617 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100618 {
619 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
620
621 /*
622 * If the group-read bit is set but not the world-read bit, then
623 * the group must be equal to the group of the original file. If
624 * we can't make that happen then reset the group-read bit. This
625 * avoids making the swap file readable to more users when the
626 * primary group of the user is too permissive.
627 */
628 if ((swap_mode & 044) == 040)
629 {
630 stat_T swap_st;
631
632 if (mch_stat((char *)swap_fname, &swap_st) >= 0
633 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200634# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100635 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200636 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200637# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200638 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100639 swap_mode &= 0600;
640 }
641
642 (void)mch_setperm(swap_fname, (long)swap_mode);
643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#endif
645 }
646
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200647 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (swap_exists_action == SEA_QUIT)
649 {
650 if (!read_buffer && !read_stdin)
651 close(fd);
652 return FAIL;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 /*
658 * Set '[ mark to the line above where the lines go (line 1 if zero).
659 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100660 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
662 curbuf->b_op_start.col = 0;
663
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100664 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
665 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
666 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (!read_buffer)
669 {
670 int m = msg_scroll;
671 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
673 /*
674 * The file must be closed again, the autocommands may want to change
675 * the file before reading it.
676 */
677 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 /*
681 * The output from the autocommands should not overwrite anything and
682 * should not be overwritten: Set msg_scroll, restore its value if no
683 * output was done.
684 */
685 msg_scroll = TRUE;
686 if (filtering)
687 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
688 FALSE, curbuf, eap);
689 else if (read_stdin)
690 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
691 FALSE, curbuf, eap);
692 else if (newfile)
693 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
694 FALSE, curbuf, eap);
695 else
696 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
697 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100699 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
700 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
701 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100702 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 if (msg_scrolled == n)
705 msg_scroll = m;
706
707#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100708 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
710 --no_wait_return;
711 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100712 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 return FAIL;
714 }
715#endif
716 /*
717 * Don't allow the autocommands to change the current buffer.
718 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000719 *
720 * Don't allow the autocommands to change the buffer name either
721 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 */
723 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000724 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
725 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
727 {
728 --no_wait_return;
729 msg_scroll = msg_save;
730 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100733 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100734 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 return FAIL;
736 }
737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100739 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
741
742 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
743 {
744 /*
745 * Show the user that we are busy reading the input. Sometimes this
746 * may take a while. When reading from stdin another program may
747 * still be running, don't move the cursor to the last line, unless
748 * always using the GUI.
749 */
750 if (read_stdin)
751 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100752 if (!is_not_a_term())
753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200755# ifdef VIMDLL
756 if (!gui.in_use)
757# endif
758 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#endif
760#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100761 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100762 if (gui.in_use && !gui.dying && !gui.starting)
763 {
764 p = (char_u *)_("Reading from stdin...");
765 gui_write(p, (int)STRLEN(p));
766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 }
770 else if (!read_buffer)
771 filemess(curbuf, sfname, (char_u *)"", 0);
772 }
773
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
776 /*
777 * Set linecnt now, before the "retry" caused by a wrong guess for
778 * fileformat, and after the autocommands, which may change them.
779 */
780 linecnt = curbuf->b_ml.ml_line_count;
781
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000783 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000784 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000785 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000786 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000787 curbuf->b_bad_char = eap->bad_char;
788 }
789 else
790 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000793 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 */
795 if (eap != NULL && eap->force_enc != 0)
796 {
797 fenc = enc_canonize(eap->cmd + eap->force_enc);
798 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000799 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 else if (curbuf->b_p_bin)
802 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 fenc_alloced = FALSE;
805 }
806 else if (curbuf->b_help)
807 {
808 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000809 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Help files are either utf-8 or latin1. Try utf-8 first, if this
812 // fails it must be latin1.
813 // Always do this when 'encoding' is "utf-8". Otherwise only do
814 // this when needed to avoid [converted] remarks all the time.
815 // It is needed when the first line contains non-ASCII characters.
816 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 fenc = (char_u *)"latin1";
818 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000819 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000821 fc = fname[STRLEN(fname) - 1];
822 if (TOLOWER_ASC(fc) == 'x')
823 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // Read the first line (and a bit more). Immediately rewind to
825 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100826 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200827 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000828 for (p = firstline; p < firstline + len; ++p)
829 if (*p >= 0x80)
830 {
831 c = TRUE;
832 break;
833 }
834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
837 if (c)
838 {
839 fenc_next = fenc;
840 fenc = (char_u *)"utf-8";
841
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100842 // When the file is utf-8 but a character doesn't fit in
843 // 'encoding' don't retry. In help text editing utf-8 bytes
844 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000845 if (!enc_utf8)
846 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 fenc_alloced = FALSE;
849 }
850 else if (*p_fencs == NUL)
851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100852 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 fenc_alloced = FALSE;
854 }
855 else
856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200858 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861 /*
862 * Jump back here to retry reading the file in different ways.
863 * Reasons to retry:
864 * - encoding conversion failed: try another one from "fenc_next"
865 * - BOM detected and fenc was set, need to setup conversion
866 * - "fileformat" check failed: try another
867 *
868 * Variables set for special retry actions:
869 * "file_rewind" Rewind the file to start reading it again.
870 * "advance_fenc" Advance "fenc" using "fenc_next".
871 * "skip_read" Re-use already read bytes (BOM detected).
872 * "did_iconv" iconv() conversion failed, try 'charconvert'.
873 * "keep_fileformat" Don't reset "fileformat".
874 *
875 * Other status indicators:
876 * "tmpname" When != NULL did conversion with 'charconvert'.
877 * Output file has to be deleted afterwards.
878 * "iconv_fd" When != -1 did conversion with iconv().
879 */
880retry:
881
882 if (file_rewind)
883 {
884 if (read_buffer)
885 {
886 read_buf_lnum = 1;
887 read_buf_col = 0;
888 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200889 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 error = TRUE;
893 goto failed;
894 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100895 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200897 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000899 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000902 curbuf->b_start_bomb = FALSE;
903 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000904 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906
907 /*
908 * When retrying with another "fenc" and the first time "fileformat"
909 * will be reset.
910 */
911 if (keep_fileformat)
912 keep_fileformat = FALSE;
913 else
914 {
915 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000918 try_unix = try_dos = try_mac = FALSE;
919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100921 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100923 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100925 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927
Bram Moolenaar13505972019-01-24 15:04:48 +0100928#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (iconv_fd != (iconv_t)-1)
930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100931 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 iconv_close(iconv_fd);
933 iconv_fd = (iconv_t)-1;
934 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937 if (advance_fenc)
938 {
939 /*
940 * Try the next entry in 'fileencodings'.
941 */
942 advance_fenc = FALSE;
943
944 if (eap != NULL && eap->force_enc != 0)
945 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100946 // Conversion given with "++cc=" wasn't possible, read
947 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000949 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 if (fenc_alloced)
951 vim_free(fenc);
952 fenc = (char_u *)"";
953 fenc_alloced = FALSE;
954 }
955 else
956 {
957 if (fenc_alloced)
958 vim_free(fenc);
959 if (fenc_next != NULL)
960 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200961 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963 else
964 {
965 fenc = (char_u *)"";
966 fenc_alloced = FALSE;
967 }
968 }
969 if (tmpname != NULL)
970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100971 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100972 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974 }
975
976 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000977 * Conversion may be required when the encoding of the file is different
978 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 */
980 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000981 converted = need_conversion(fenc);
982 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
984
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 // "ucs-bom" means we need to check the first bytes of the file
986 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (STRCMP(fenc, ENC_UCSBOM) == 0)
988 fio_flags = FIO_UCSBOM;
989
990 /*
991 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
992 * done. This is handled below after read(). Prepare the
993 * fio_flags to avoid having to parse the string each time.
994 * Also check for Unicode to Latin1 conversion, because iconv()
995 * appears not to handle this correctly. This works just like
996 * conversion to UTF-8 except how the resulting character is put in
997 * the buffer.
998 */
999 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1000 fio_flags = get_fio_flags(fenc);
1001
Bram Moolenaar4f974752019-02-17 17:44:42 +01001002#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1005 * is handled with MultiByteToWideChar().
1006 */
1007 if (fio_flags == 0)
1008 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar13505972019-01-24 15:04:48 +01001011#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (fio_flags == 0)
1014 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar13505972019-01-24 15:04:48 +01001017#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 /*
1019 * Try using iconv() if we can't convert internally.
1020 */
1021 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001022# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 )
1026 iconv_fd = (iconv_t)my_iconv_open(
1027 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
Bram Moolenaar13505972019-01-24 15:04:48 +01001030#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 /*
1032 * Use the 'charconvert' expression when conversion is required
1033 * and we can't do it internally or with iconv().
1034 */
1035 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001036 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001037# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 )
1041 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001042# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001044# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001045 // Skip conversion when it's already done (retry for wrong
1046 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (tmpname == NULL)
1048 {
1049 tmpname = readfile_charconvert(fname, fenc, &fd);
1050 if (tmpname == NULL)
1051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 advance_fenc = TRUE;
1054 if (fd < 0)
1055 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001057 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 error = TRUE;
1059 goto failed;
1060 }
1061 goto retry;
1062 }
1063 }
1064 }
1065 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001069#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 )
1073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // Conversion wanted but we can't.
1075 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 advance_fenc = TRUE;
1077 goto retry;
1078 }
1079 }
1080 }
1081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // Set "can_retry" when it's possible to rewind the file and try with
1083 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1084 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001085 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
1087 if (!skip_read)
1088 {
1089 linerest = 0;
1090 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001091 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 skip_count = lines_to_skip;
1093 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001095#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001096 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1097 && curbuf->b_ffname != NULL
1098 && curbuf->b_p_udf
1099 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001100 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001101 && !read_stdin
1102 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001103 if (read_undo_file)
1104 sha256_start(&sha_ctx);
1105#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001106#ifdef FEAT_CRYPT
1107 if (curbuf->b_cryptstate != NULL)
1108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // Need to free the state, but keep the key, don't want to ask for
1110 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001111 crypt_free_state(curbuf->b_cryptstate);
1112 curbuf->b_cryptstate = NULL;
1113 }
1114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 }
1116
1117 while (!error && !got_int)
1118 {
1119 /*
1120 * We allocate as much space for the file as we can get, plus
1121 * space for the old line plus room for one terminating NUL.
1122 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001123 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001125 if (!skip_read)
1126 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001127#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001128 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001129#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001130 // Use buffer >= 64K. Add linerest to double the size if the
1131 // line gets very long, to avoid a lot of copying. But don't
1132 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001133 size = 0x10000L + linerest;
1134 if (size > 0x100000L)
1135 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001136#endif
1137 }
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001140 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 size = 1;
1145 }
1146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
1148 if (!skip_read)
1149 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001150 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001152 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 FALSE)) != NULL)
1154 break;
1155 }
1156 if (new_buffer == NULL)
1157 {
1158 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1159 error = TRUE;
1160 break;
1161 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1164 vim_free(buffer);
1165 buffer = new_buffer;
1166 ptr = buffer + linerest;
1167 line_start = buffer;
1168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001169 // May need room to translate into.
1170 // For iconv() we don't really know the required space, use a
1171 // factor ICONV_MULT.
1172 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1173 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1174 // become up to 4 bytes, size must be multiple of 2
1175 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1176 // multiple of 2
1177 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1178 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001179 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001180#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (iconv_fd != (iconv_t)-1)
1182 size = size / ICONV_MULT;
1183 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (fio_flags & FIO_LATIN1)
1186 size = size / 2;
1187 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1188 size = (size * 2 / 3) & ~1;
1189 else if (fio_flags & FIO_UCS4)
1190 size = (size * 2 / 3) & ~3;
1191 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001192 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001193#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001196#endif
1197#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001199 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200#endif
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (conv_restlen > 0)
1203 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001204 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 mch_memmove(ptr, conv_rest, conv_restlen);
1206 ptr += conv_restlen;
1207 size -= conv_restlen;
1208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
1210 if (read_buffer)
1211 {
1212 /*
1213 * Read bytes from curbuf. Used for converting text read
1214 * from stdin.
1215 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001216 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (read_buf_lnum > from)
1218 size = 0;
1219 else
1220 {
1221 int n, ni;
1222 long tlen;
1223
1224 tlen = 0;
1225 for (;;)
1226 {
1227 p = ml_get(read_buf_lnum) + read_buf_col;
1228 n = (int)STRLEN(p);
1229 if ((int)tlen + n + 1 > size)
1230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001231 // Filled up to "size", append partial line.
1232 // Change NL to NUL to reverse the effect done
1233 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001234 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 for (ni = 0; ni < n; ++ni)
1236 {
1237 if (p[ni] == NL)
1238 ptr[tlen++] = NUL;
1239 else
1240 ptr[tlen++] = p[ni];
1241 }
1242 read_buf_col += n;
1243 break;
1244 }
1245 else
1246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001247 // Append whole line and new-line. Change NL
1248 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 for (ni = 0; ni < n; ++ni)
1250 {
1251 if (p[ni] == NL)
1252 ptr[tlen++] = NUL;
1253 else
1254 ptr[tlen++] = p[ni];
1255 }
1256 ptr[tlen++] = NL;
1257 read_buf_col = 0;
1258 if (++read_buf_lnum > from)
1259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001260 // When the last line didn't have an
1261 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (!curbuf->b_p_eol)
1263 --tlen;
1264 size = tlen;
Christian Brabandt226b28b2021-06-21 21:08:08 +02001265 eof = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 break;
1267 }
1268 }
1269 }
1270 }
1271 }
1272 else
1273 {
1274 /*
1275 * Read bytes from the file.
1276 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001277# ifdef FEAT_SODIUM
1278 // Let the crypt layer work with a buffer size of 8192
1279 if (filesize == 0)
1280 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001281 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001282 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1283 + crypto_secretstream_xchacha20poly1305_ABYTES;
1284
1285 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1286 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1287 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1288# endif
1289 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001290 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001291 filesize_count += size;
1292 // hit end of file
1293 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001296#ifdef FEAT_CRYPT
1297 /*
1298 * At start of file: Check for magic number of encryption.
1299 */
1300 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001301 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001302 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1303 &filesize, newfile, sfname,
1304 &did_ask_for_key);
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001305# ifdef CRYPT_NOT_INPLACE
1306 if (curbuf->b_cryptstate != NULL
1307 && !crypt_works_inplace(curbuf->b_cryptstate))
1308 // reading undo file requires crypt_decode_inplace()
1309 read_undo_file = FALSE;
1310# endif
1311 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001312 /*
1313 * Decrypt the read bytes. This is done before checking for
1314 * EOF because the crypt layer may be buffering.
1315 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001316 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1317 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001319# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001320 if (crypt_works_inplace(curbuf->b_cryptstate))
1321 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001322# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001323 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1324 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001325# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 }
1327 else
1328 {
1329 char_u *newptr = NULL;
1330 int decrypted_size;
1331
1332 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001333 curbuf->b_cryptstate, ptr, size,
1334 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001335
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001336 if (decrypted_size < 0)
1337 {
1338 // error message already given
1339 error = TRUE;
1340 vim_free(newptr);
1341 break;
1342 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001343 // If the crypt layer is buffering, not producing
1344 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001345 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001346 continue;
1347
1348 if (linerest == 0)
1349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001350 // Simple case: reuse returned buffer (may be
1351 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001352 new_buffer = newptr;
1353 }
1354 else
1355 {
1356 long_u new_size;
1357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001358 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001359 new_size = (long_u)(decrypted_size + linerest + 1);
1360 new_buffer = lalloc(new_size, FALSE);
1361 if (new_buffer == NULL)
1362 {
1363 do_outofmem_msg(new_size);
1364 error = TRUE;
1365 break;
1366 }
1367
1368 mch_memmove(new_buffer, buffer, linerest);
1369 if (newptr != NULL)
1370 mch_memmove(new_buffer + linerest, newptr,
1371 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001372 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001373 }
1374
1375 if (new_buffer != NULL)
1376 {
1377 vim_free(buffer);
1378 buffer = new_buffer;
1379 new_buffer = NULL;
1380 line_start = buffer;
1381 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001382 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001383 }
1384 size = decrypted_size;
1385 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001386# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001387 }
1388#endif
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (size <= 0)
1391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001395 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001396 /*
1397 * Reached end-of-file but some trailing bytes could
1398 * not be converted. Truncated file?
1399 */
1400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001401 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001402 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001403#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001404 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001405#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001406 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001407 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001408 if (can_retry)
1409 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001410 if (conv_error == 0)
1411 conv_error = curbuf->b_ml.ml_line_count
1412 - linecnt + 1;
1413 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001414 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001415 else if (illegal_byte == 0)
1416 illegal_byte = curbuf->b_ml.ml_line_count
1417 - linecnt + 1;
1418 if (bad_char_behavior == BAD_DROP)
1419 {
1420 *(ptr - conv_restlen) = NUL;
1421 conv_restlen = 0;
1422 }
1423 else
1424 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001425 // Replace the trailing bytes with the replacement
1426 // character if we were converting; if we weren't,
1427 // leave the UTF8 checking code to do it, as it
1428 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001429 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001430#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001431 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001432#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001433 ))
1434 {
1435 while (conv_restlen > 0)
1436 {
1437 *(--ptr) = bad_char_behavior;
1438 --conv_restlen;
1439 }
1440 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001441 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001442#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001443 if (iconv_fd != (iconv_t)-1)
1444 {
1445 iconv_close(iconv_fd);
1446 iconv_fd = (iconv_t)-1;
1447 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001448#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001449 }
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453 skip_read = FALSE;
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /*
1456 * At start of file (or after crypt magic number): Check for BOM.
1457 * Also check for a BOM for other Unicode encodings, but not after
1458 * converting with 'charconvert' or when a BOM has already been
1459 * found.
1460 */
1461 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001462#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001463 || (cryptkey != NULL
1464 && filesize == crypt_get_header_len(
1465 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 )
1468 && (fio_flags == FIO_UCSBOM
1469 || (!curbuf->b_p_bomb
1470 && tmpname == NULL
1471 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1472 {
1473 char_u *ccname;
1474 int blen;
1475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001476 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (size < 2 || curbuf->b_p_bin)
1478 ccname = NULL;
1479 else
1480 ccname = check_for_bom(ptr, size, &blen,
1481 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1482 if (ccname != NULL)
1483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001484 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 filesize += blen;
1486 size -= blen;
1487 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001488 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001491 curbuf->b_start_bomb = TRUE;
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494
1495 if (fio_flags == FIO_UCSBOM)
1496 {
1497 if (ccname == NULL)
1498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001499 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 advance_fenc = TRUE;
1501 }
1502 else
1503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (fenc_alloced)
1506 vim_free(fenc);
1507 fenc = ccname;
1508 fenc_alloced = FALSE;
1509 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001510 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 skip_read = TRUE;
1512 goto retry;
1513 }
1514 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001517 ptr -= conv_restlen;
1518 size += conv_restlen;
1519 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /*
1521 * Break here for a read error or end-of-file.
1522 */
1523 if (size <= 0)
1524 break;
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar13505972019-01-24 15:04:48 +01001527#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (iconv_fd != (iconv_t)-1)
1529 {
1530 /*
1531 * Attempt conversion of the read bytes to 'encoding' using
1532 * iconv().
1533 */
1534 const char *fromp;
1535 char *top;
1536 size_t from_size;
1537 size_t to_size;
1538
1539 fromp = (char *)ptr;
1540 from_size = size;
1541 ptr += size;
1542 top = (char *)ptr;
1543 to_size = real_size - size;
1544
1545 /*
1546 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001547 * another conversion. Except for when there is no
1548 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001550 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1551 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1553 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001554 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001555 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001556 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001557 if (conv_error == 0)
1558 conv_error = readfile_linenr(linecnt,
1559 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001561 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001562 ++fromp;
1563 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001564 if (bad_char_behavior == BAD_KEEP)
1565 {
1566 *top++ = *(fromp - 1);
1567 --to_size;
1568 }
1569 else if (bad_char_behavior != BAD_DROP)
1570 {
1571 *top++ = bad_char_behavior;
1572 --to_size;
1573 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 if (from_size > 0)
1577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001578 // Some remaining characters, keep them for the next
1579 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1581 conv_restlen = (int)from_size;
1582 }
1583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001584 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 line_start = ptr - linerest;
1586 mch_memmove(line_start, buffer, (size_t)linerest);
1587 size = (long)((char_u *)top - ptr);
1588 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaar4f974752019-02-17 17:44:42 +01001591#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (fio_flags & FIO_CODEPAGE)
1593 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001594 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001595 WCHAR ucs2buf[3];
1596 int ucs2len;
1597 int codepage = FIO_GET_CP(fio_flags);
1598 int bytelen;
1599 int found_bad;
1600 char replstr[2];
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 /*
1603 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001604 * a codepage, using standard MS-Windows functions. This
1605 * requires two steps:
1606 * 1. convert from 'fileencoding' to ucs-2
1607 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001609 * Because there may be illegal bytes AND an incomplete byte
1610 * sequence at the end, we may have to do the conversion one
1611 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001614 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001615 if (bad_char_behavior > 0)
1616 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001618 replstr[0] = '?';
1619 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001622 * Move the bytes to the end of the buffer, so that we have
1623 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001625 src = ptr + real_size - size;
1626 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001628 /*
1629 * Do the conversion.
1630 */
1631 dst = ptr;
1632 size = size;
1633 while (size > 0)
1634 {
1635 found_bad = FALSE;
1636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001637# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001638 if (codepage == CP_UTF8)
1639 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // Handle CP_UTF8 input ourselves to be able to handle
1641 // trailing bytes properly.
1642 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001643 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001644 if (bytelen > size)
1645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001646 // Only got some bytes of a character. Normally
1647 // it's put in "conv_rest", but if it's too long
1648 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001649 if (bytelen <= CONV_RESTLEN)
1650 break;
1651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001652 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001653 bytelen = size;
1654 found_bad = TRUE;
1655 }
1656 else
1657 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001658 int u8c = utf_ptr2char(src);
1659
Bram Moolenaar86e01082005-12-29 22:45:34 +00001660 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001661 found_bad = TRUE;
1662 ucs2buf[0] = u8c;
1663 ucs2len = 1;
1664 }
1665 }
1666 else
1667# endif
1668 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001669 // We don't know how long the byte sequence is, try
1670 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001671 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1672 ++bytelen)
1673 {
1674 ucs2len = MultiByteToWideChar(codepage,
1675 MB_ERR_INVALID_CHARS,
1676 (LPCSTR)src, bytelen,
1677 ucs2buf, 3);
1678 if (ucs2len > 0)
1679 break;
1680 }
1681 if (ucs2len == 0)
1682 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001683 // If we have only one byte then it's probably an
1684 // incomplete byte sequence. Otherwise discard
1685 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001686 if (size == 1)
1687 break;
1688 found_bad = TRUE;
1689 bytelen = 1;
1690 }
1691 }
1692
1693 if (!found_bad)
1694 {
1695 int i;
1696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001697 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001698 if (enc_utf8)
1699 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001700 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001701 for (i = 0; i < ucs2len; ++i)
1702 dst += utf_char2bytes(ucs2buf[i], dst);
1703 }
1704 else
1705 {
1706 BOOL bad = FALSE;
1707 int dstlen;
1708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001709 // From UCS-2 to "enc_codepage". If the
1710 // conversion uses the default character "?",
1711 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001712 dstlen = WideCharToMultiByte(enc_codepage, 0,
1713 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001714 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001715 replstr, &bad);
1716 if (bad)
1717 found_bad = TRUE;
1718 else
1719 dst += dstlen;
1720 }
1721 }
1722
1723 if (found_bad)
1724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001725 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001726 if (can_retry)
1727 goto rewind_retry;
1728 if (conv_error == 0)
1729 conv_error = readfile_linenr(linecnt, ptr, dst);
1730 if (bad_char_behavior != BAD_DROP)
1731 {
1732 if (bad_char_behavior == BAD_KEEP)
1733 {
1734 mch_memmove(dst, src, bytelen);
1735 dst += bytelen;
1736 }
1737 else
1738 *dst++ = bad_char_behavior;
1739 }
1740 }
1741
1742 src += bytelen;
1743 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001745
1746 if (size > 0)
1747 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001748 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001749 mch_memmove(conv_rest, src, size);
1750 conv_restlen = size;
1751 }
1752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001754 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001757#endif
1758#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (fio_flags & FIO_MACROMAN)
1760 {
1761 /*
1762 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001763 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001765 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (fio_flags != 0)
1771 {
1772 int u8c;
1773 char_u *dest;
1774 char_u *tail = NULL;
1775
1776 /*
1777 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1778 * "enc_utf8" not set: Convert Unicode to Latin1.
1779 * Go from end to start through the buffer, because the number
1780 * of bytes may increase.
1781 * "dest" points to after where the UTF-8 bytes go, "p" points
1782 * to after the next character to convert.
1783 */
1784 dest = ptr + real_size;
1785 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1786 {
1787 p = ptr + size;
1788 if (fio_flags == FIO_UTF8)
1789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001790 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 tail = ptr + size - 1;
1792 while (tail > ptr && (*tail & 0xc0) == 0x80)
1793 --tail;
1794 if (tail + utf_byte2len(*tail) <= ptr + size)
1795 tail = NULL;
1796 else
1797 p = tail;
1798 }
1799 }
1800 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1801 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001802 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 p = ptr + (size & ~1);
1804 if (size & 1)
1805 tail = p;
1806 if ((fio_flags & FIO_UTF16) && p > ptr)
1807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001808 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (fio_flags & FIO_ENDIAN_L)
1810 {
1811 u8c = (*--p << 8);
1812 u8c += *--p;
1813 }
1814 else
1815 {
1816 u8c = *--p;
1817 u8c += (*--p << 8);
1818 }
1819 if (u8c >= 0xd800 && u8c <= 0xdbff)
1820 tail = p;
1821 else
1822 p += 2;
1823 }
1824 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001825 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001827 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 p = ptr + (size & ~3);
1829 if (size & 3)
1830 tail = p;
1831 }
1832
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001833 // If there is a trailing incomplete sequence move it to
1834 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (tail != NULL)
1836 {
1837 conv_restlen = (int)((ptr + size) - tail);
1838 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1839 size -= conv_restlen;
1840 }
1841
1842
1843 while (p > ptr)
1844 {
1845 if (fio_flags & FIO_LATIN1)
1846 u8c = *--p;
1847 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1848 {
1849 if (fio_flags & FIO_ENDIAN_L)
1850 {
1851 u8c = (*--p << 8);
1852 u8c += *--p;
1853 }
1854 else
1855 {
1856 u8c = *--p;
1857 u8c += (*--p << 8);
1858 }
1859 if ((fio_flags & FIO_UTF16)
1860 && u8c >= 0xdc00 && u8c <= 0xdfff)
1861 {
1862 int u16c;
1863
1864 if (p == ptr)
1865 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001866 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (can_retry)
1868 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001869 if (conv_error == 0)
1870 conv_error = readfile_linenr(linecnt,
1871 ptr, p);
1872 if (bad_char_behavior == BAD_DROP)
1873 continue;
1874 if (bad_char_behavior != BAD_KEEP)
1875 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001878 // found second word of double-word, get the first
1879 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (fio_flags & FIO_ENDIAN_L)
1881 {
1882 u16c = (*--p << 8);
1883 u16c += *--p;
1884 }
1885 else
1886 {
1887 u16c = *--p;
1888 u16c += (*--p << 8);
1889 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001890 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1891 + (u8c & 0x3ff);
1892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (u16c < 0xd800 || u16c > 0xdbff)
1895 {
1896 if (can_retry)
1897 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001898 if (conv_error == 0)
1899 conv_error = readfile_linenr(linecnt,
1900 ptr, p);
1901 if (bad_char_behavior == BAD_DROP)
1902 continue;
1903 if (bad_char_behavior != BAD_KEEP)
1904 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 }
1908 else if (fio_flags & FIO_UCS4)
1909 {
1910 if (fio_flags & FIO_ENDIAN_L)
1911 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001912 u8c = (unsigned)*--p << 24;
1913 u8c += (unsigned)*--p << 16;
1914 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 u8c += *--p;
1916 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001917 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001920 u8c += (unsigned)*--p << 8;
1921 u8c += (unsigned)*--p << 16;
1922 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001925 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 if (*--p < 0x80)
1928 u8c = *p;
1929 else
1930 {
1931 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001932 p -= len;
1933 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (len == 0)
1935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // Not a valid UTF-8 character, retry with
1937 // another fenc when possible, otherwise just
1938 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (can_retry)
1940 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001941 if (conv_error == 0)
1942 conv_error = readfile_linenr(linecnt,
1943 ptr, p);
1944 if (bad_char_behavior == BAD_DROP)
1945 continue;
1946 if (bad_char_behavior != BAD_KEEP)
1947 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001951 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 dest -= utf_char2len(u8c);
1954 (void)utf_char2bytes(u8c, dest);
1955 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001956 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 --dest;
1959 if (u8c >= 0x100)
1960 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001961 // character doesn't fit in latin1, retry with
1962 // another fenc when possible, otherwise just
1963 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001964 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001966 if (conv_error == 0)
1967 conv_error = readfile_linenr(linecnt, ptr, p);
1968 if (bad_char_behavior == BAD_DROP)
1969 ++dest;
1970 else if (bad_char_behavior == BAD_KEEP)
1971 *dest = u8c;
1972 else if (eap != NULL && eap->bad_char != 0)
1973 *dest = bad_char_behavior;
1974 else
1975 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 else
1978 *dest = u8c;
1979 }
1980 }
1981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001982 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 line_start = dest - linerest;
1984 mch_memmove(line_start, buffer, (size_t)linerest);
1985 size = (long)((ptr + real_size) - dest);
1986 ptr = dest;
1987 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001988 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001990 int incomplete_tail = FALSE;
1991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001992 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001993 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001995 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001996 int l;
1997
1998 if (todo <= 0)
1999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (*p >= 0x80)
2001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // A length of 1 means it's an illegal byte. Accept
2003 // an incomplete character at the end though, the next
2004 // read() will get the next bytes, we'll check it
2005 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002006 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002007 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 // Avoid retrying with a different encoding when
2010 // a truncated file is more likely, or attempting
2011 // to read the rest of an incomplete sequence when
2012 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002013 if (p > ptr || filesize > 0)
2014 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // Incomplete byte sequence, move it to conv_rest[]
2016 // and try to read the rest of it, unless we've
2017 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002018 if (p > ptr)
2019 {
2020 conv_restlen = todo;
2021 mch_memmove(conv_rest, p, conv_restlen);
2022 size -= conv_restlen;
2023 break;
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002026 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // Illegal byte. If we can try another encoding
2029 // do that, unless at EOF where a truncated
2030 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002031 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002032 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002033#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002035 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2036 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002037#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002039 if (conv_error == 0 && illegal_byte == 0)
2040 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002043 if (bad_char_behavior == BAD_DROP)
2044 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002045 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002046 --p;
2047 --size;
2048 }
2049 else if (bad_char_behavior != BAD_KEEP)
2050 *p = bad_char_behavior;
2051 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002052 else
2053 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002056 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002060 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002061#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002062 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002064 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002066#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002067 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002068 advance_fenc = TRUE;
2069 file_rewind = TRUE;
2070 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 filesize += size;
2076
2077 /*
2078 * when reading the first part of a file: guess EOL type
2079 */
2080 if (fileformat == EOL_UNKNOWN)
2081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (try_dos || try_unix)
2084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002086 if (try_mac)
2087 try_mac = 1;
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 for (p = ptr; p < ptr + size; ++p)
2090 {
2091 if (*p == NL)
2092 {
2093 if (!try_unix
2094 || (try_dos && p > ptr && p[-1] == CAR))
2095 fileformat = EOL_DOS;
2096 else
2097 fileformat = EOL_UNIX;
2098 break;
2099 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002100 else if (*p == CAR && try_mac)
2101 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002104 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (fileformat == EOL_UNIX && try_mac)
2106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 try_mac = 1;
2109 try_unix = 1;
2110 for (; p >= ptr && *p != CAR; p--)
2111 ;
2112 if (p >= ptr)
2113 {
2114 for (p = ptr; p < ptr + size; ++p)
2115 {
2116 if (*p == NL)
2117 try_unix++;
2118 else if (*p == CAR)
2119 try_mac++;
2120 }
2121 if (try_mac > try_unix)
2122 fileformat = EOL_MAC;
2123 }
2124 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002125 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 // Looking for CR but found no end-of-line markers at
2127 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002128 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (fileformat == EOL_UNKNOWN && try_mac)
2133 fileformat = EOL_MAC;
2134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (fileformat == EOL_UNKNOWN)
2137 fileformat = default_fileformat();
2138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002139 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 set_fileformat(fileformat, OPT_LOCAL);
2142 }
2143 }
2144
2145 /*
2146 * This loop is executed once for every character read.
2147 * Keep it fast!
2148 */
2149 if (fileformat == EOL_MAC)
2150 {
2151 --ptr;
2152 while (++ptr, --size >= 0)
2153 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002154 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if ((c = *ptr) != NUL && c != CAR && c != NL)
2156 continue;
2157 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002158 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 else
2162 {
2163 if (skip_count == 0)
2164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002165 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 len = (colnr_T) (ptr - line_start + 1);
2167 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2168 {
2169 error = TRUE;
2170 break;
2171 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002172#ifdef FEAT_PERSISTENT_UNDO
2173 if (read_undo_file)
2174 sha256_update(&sha_ctx, line_start, len);
2175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 ++lnum;
2177 if (--read_count == 0)
2178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 error = TRUE; // break loop
2180 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 break;
2182 }
2183 }
2184 else
2185 --skip_count;
2186 line_start = ptr + 1;
2187 }
2188 }
2189 }
2190 else
2191 {
2192 --ptr;
2193 while (++ptr, --size >= 0)
2194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002195 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 continue;
2197 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002198 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 else
2200 {
2201 if (skip_count == 0)
2202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 len = (colnr_T)(ptr - line_start + 1);
2205 if (fileformat == EOL_DOS)
2206 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002207 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002209 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 ptr[-1] = NUL;
2211 --len;
2212 }
2213 /*
2214 * Reading in Dos format, but no CR-LF found!
2215 * When 'fileformats' includes "unix", delete all
2216 * the lines read so far and start all over again.
2217 * Otherwise give an error message later.
2218 */
2219 else if (ff_error != EOL_DOS)
2220 {
2221 if ( try_unix
2222 && !read_stdin
2223 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002224 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2225 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002228 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 set_fileformat(EOL_UNIX, OPT_LOCAL);
2230 file_rewind = TRUE;
2231 keep_fileformat = TRUE;
2232 goto retry;
2233 }
2234 ff_error = EOL_DOS;
2235 }
2236 }
2237 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2238 {
2239 error = TRUE;
2240 break;
2241 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002242#ifdef FEAT_PERSISTENT_UNDO
2243 if (read_undo_file)
2244 sha256_update(&sha_ctx, line_start, len);
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 ++lnum;
2247 if (--read_count == 0)
2248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 error = TRUE; // break loop
2250 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 break;
2252 }
2253 }
2254 else
2255 --skip_count;
2256 line_start = ptr + 1;
2257 }
2258 }
2259 }
2260 linerest = (long)(ptr - line_start);
2261 ui_breakcheck();
2262 }
2263
2264failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002265 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (error && read_count == 0)
2267 error = FALSE;
2268
2269 /*
2270 * If we get EOF in the middle of a line, note the fact and
2271 * complete the line ourselves.
2272 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2273 */
2274 if (!error
2275 && !got_int
2276 && linerest != 0
2277 && !(!curbuf->b_p_bin
2278 && fileformat == EOL_DOS
2279 && *line_start == Ctrl_Z
2280 && ptr == line_start + 1))
2281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002283 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 curbuf->b_p_eol = FALSE;
2285 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002286 len = (colnr_T)(ptr - line_start + 1);
2287 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 error = TRUE;
2289 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002290 {
2291#ifdef FEAT_PERSISTENT_UNDO
2292 if (read_undo_file)
2293 sha256_update(&sha_ctx, line_start, len);
2294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002303 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002304 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002305 crypt_free_state(curbuf->b_cryptstate);
2306 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002307 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002308 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2309 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // Don't set cryptkey to NULL, it's used below as a flag that
2311 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#endif
2313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // If editing a new file: set 'fenc' for the current buffer.
2315 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002316 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002318 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (fenc_alloced)
2320 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002321#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#endif
2325
2326 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002328#ifdef HAVE_FD_CLOEXEC
2329 else
2330 {
2331 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002332
Bram Moolenaarf05da212009-11-17 16:13:15 +00002333 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002334 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002335 }
2336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 vim_free(buffer);
2338
2339#ifdef HAVE_DUP
2340 if (read_stdin)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002344 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346#endif
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (tmpname != NULL)
2349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 vim_free(tmpname);
2352 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002353 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 /*
2356 * In recovery mode everything but autocommands is skipped.
2357 */
2358 if (!recoverymode)
2359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2362 {
2363#ifdef FEAT_NETBEANS_INTG
2364 netbeansFireChanges = 0;
2365#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002366 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#ifdef FEAT_NETBEANS_INTG
2368 netbeansFireChanges = 1;
2369#endif
2370 --linecnt;
2371 }
2372 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2373 if (filesize == 0)
2374 linecnt = 0;
2375 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002378#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002379 // After reading the text into the buffer the diff info needs to
2380 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002381 diff_invalidate(curbuf);
2382#endif
2383#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002384 // All folds in the window are invalid now. Mark them for update
2385 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002386 foldUpdateAll(curwin);
2387#endif
2388 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002389 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 appended_lines_mark(from, linecnt);
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifndef ALWAYS_USE_GUI
2393 /*
2394 * If we were reading from the same terminal as where messages go,
2395 * the screen will have been messed up.
2396 * Switch on raw mode now and clear the screen.
2397 */
2398 if (read_stdin)
2399 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002400 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 starttermcap();
2402 screenclear();
2403 }
2404#endif
2405
2406 if (got_int)
2407 {
2408 if (!(flags & READ_DUMMY))
2409 {
2410 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2411 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002412 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 msg_scroll = msg_save;
2415#ifdef FEAT_VIMINFO
2416 check_marks_read();
2417#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002418 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420
2421 if (!filtering && !(flags & READ_DUMMY))
2422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 c = FALSE;
2425
2426#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002427 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
2429 STRCAT(IObuff, _("[fifo]"));
2430 c = TRUE;
2431 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002432 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 STRCAT(IObuff, _("[socket]"));
2435 c = TRUE;
2436 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002437# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002438 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002439 {
2440 STRCAT(IObuff, _("[character special]"));
2441 c = TRUE;
2442 }
2443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444#endif
2445 if (curbuf->b_p_ro)
2446 {
2447 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2448 c = TRUE;
2449 }
2450 if (read_no_eol_lnum)
2451 {
2452 msg_add_eol();
2453 c = TRUE;
2454 }
2455 if (ff_error == EOL_DOS)
2456 {
2457 STRCAT(IObuff, _("[CR missing]"));
2458 c = TRUE;
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (split)
2461 {
2462 STRCAT(IObuff, _("[long lines split]"));
2463 c = TRUE;
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (notconverted)
2466 {
2467 STRCAT(IObuff, _("[NOT converted]"));
2468 c = TRUE;
2469 }
2470 else if (converted)
2471 {
2472 STRCAT(IObuff, _("[converted]"));
2473 c = TRUE;
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_CRYPT
2476 if (cryptkey != NULL)
2477 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002478 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 c = TRUE;
2480 }
2481#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002482 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002484 sprintf((char *)IObuff + STRLEN(IObuff),
2485 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 c = TRUE;
2487 }
2488 else if (illegal_byte > 0)
2489 {
2490 sprintf((char *)IObuff + STRLEN(IObuff),
2491 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2492 c = TRUE;
2493 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002494 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 STRCAT(IObuff, _("[READ ERRORS]"));
2497 c = TRUE;
2498 }
2499 if (msg_add_fileformat(fileformat))
2500 c = TRUE;
2501#ifdef FEAT_CRYPT
2502 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002503 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002504 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 else
2506#endif
2507 msg_add_lines(c, (long)linecnt, filesize);
2508
Bram Moolenaard23a8232018-02-10 18:45:26 +01002509 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 msg_scrolled_ign = TRUE;
2511#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002512 // Don't show the message when reading stdin, it would end up in a
2513 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (read_stdin || read_buffer)
2515 p = msg_may_trunc(FALSE, IObuff);
2516 else
2517#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002518 {
2519 if (msg_col > 0)
2520 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002521 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002524 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002525 // Need to repeat the message after redrawing when:
2526 // - When reading from stdin (the screen will be cleared next).
2527 // - When restart_edit is set (otherwise there will be a delay
2528 // before redrawing).
2529 // - When the screen was scrolled but there is no wait-return
2530 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002531 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 msg_scrolled_ign = FALSE;
2533 }
2534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002535 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002537 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002538 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 curbuf->b_p_ro = TRUE;
2540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002541 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 /*
2544 * In Ex mode: cursor at last new line.
2545 * Otherwise: cursor at first new line.
2546 */
2547 if (exmode_active)
2548 curwin->w_cursor.lnum = from + linecnt;
2549 else
2550 curwin->w_cursor.lnum = from + 1;
2551 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002552 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaare1004402020-10-24 20:49:43 +02002554 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002555 {
2556 // Set '[ and '] marks to the newly read lines.
2557 curbuf->b_op_start.lnum = from + 1;
2558 curbuf->b_op_start.col = 0;
2559 curbuf->b_op_end.lnum = from + linecnt;
2560 curbuf->b_op_end.col = 0;
2561 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002562
Bram Moolenaar4f974752019-02-17 17:44:42 +01002563#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002564 /*
2565 * Work around a weird problem: When a file has two links (only
2566 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002567 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002568 * It's correct again after reading the file, thus reset the timestamp
2569 * here.
2570 */
2571 if (newfile && !read_stdin && !read_buffer
2572 && mch_stat((char *)fname, &st) >= 0)
2573 {
2574 buf_store_time(curbuf, &st, fname);
2575 curbuf->b_mtime_read = curbuf->b_mtime;
2576 }
2577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 msg_scroll = msg_save;
2580
2581#ifdef FEAT_VIMINFO
2582 /*
2583 * Get the marks before executing autocommands, so they can be used there.
2584 */
2585 check_marks_read();
2586#endif
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002589 * We remember if the last line of the read didn't have
2590 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2591 * or writing the read again with 'binary' on. The latter is required
2592 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002594 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002596 // When reloading a buffer put the cursor at the first line that is
2597 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002598 if (flags & READ_KEEP_UNDO)
2599 u_find_first_changed();
2600
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002601#ifdef FEAT_PERSISTENT_UNDO
2602 /*
2603 * When opening a new file locate undo info and read it.
2604 */
2605 if (read_undo_file)
2606 {
2607 char_u hash[UNDO_HASH_SIZE];
2608
2609 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002610 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002611 }
2612#endif
2613
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002614 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 int m = msg_scroll;
2617 int n = msg_scrolled;
2618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002619 // Save the fileformat now, otherwise the buffer will be considered
2620 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002621 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 save_file_ff(curbuf);
2623
2624 /*
2625 * The output from the autocommands should not overwrite anything and
2626 * should not be overwritten: Set msg_scroll, restore its value if no
2627 * output was done.
2628 */
2629 msg_scroll = TRUE;
2630 if (filtering)
2631 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2632 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002633 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2636 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002637 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2638 /*
2639 * EVENT_FILETYPE was not triggered but the buffer already has a
2640 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2641 */
2642 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2643 TRUE, curbuf);
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else
2646 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2647 FALSE, NULL, eap);
2648 if (msg_scrolled == n)
2649 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002650# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002651 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 if (recoverymode && error)
2657 return FAIL;
2658 return OK;
2659}
2660
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002661#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002662/*
2663 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2664 * which is the name of files used for process substitution output by
2665 * some shells on some operating systems, e.g., bash on SunOS.
2666 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2667 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002668 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002669is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002670{
2671 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2672 && VIM_ISDIGIT(fname[8])
2673 && *skipdigits(fname + 9) == NUL
2674 && (fname[9] != NUL
2675 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2676}
2677#endif
2678
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002679/*
2680 * From the current line count and characters read after that, estimate the
2681 * line number where we are now.
2682 * Used for error messages that include a line number.
2683 */
2684 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002685readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002686 linenr_T linecnt, // line count before reading more bytes
2687 char_u *p, // start of more bytes read
2688 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002689{
2690 char_u *s;
2691 linenr_T lnum;
2692
2693 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2694 for (s = p; s < endp; ++s)
2695 if (*s == '\n')
2696 ++lnum;
2697 return lnum;
2698}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002701 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2702 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 * Returns OK or FAIL.
2704 */
2705 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002706prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
Bram Moolenaar13505972019-01-24 15:04:48 +01002708 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (eap->cmd == NULL)
2710 return FAIL;
2711
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002712 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2713 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002714 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002715 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002716
2717 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002718 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002719 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return OK;
2721}
2722
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002723/*
2724 * Set default or forced 'fileformat' and 'binary'.
2725 */
2726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002727set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002728{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002729 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002730 if (set_options)
2731 {
2732 if (eap != NULL && eap->force_ff != 0)
2733 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2734 else if (*p_ffs != NUL)
2735 set_fileformat(default_fileformat(), OPT_LOCAL);
2736 }
2737
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002738 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002739 if (eap != NULL && eap->force_bin != 0)
2740 {
2741 int oldval = curbuf->b_p_bin;
2742
2743 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2744 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2745 }
2746}
2747
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002748/*
2749 * Set forced 'fileencoding'.
2750 */
2751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002752set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002753{
2754 if (eap->force_enc != 0)
2755 {
2756 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2757
2758 if (fenc != NULL)
2759 set_string_option_direct((char_u *)"fenc", -1,
2760 fenc, OPT_FREE|OPT_LOCAL, 0);
2761 vim_free(fenc);
2762 }
2763}
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765/*
2766 * Find next fileencoding to use from 'fileencodings'.
2767 * "pp" points to fenc_next. It's advanced to the next item.
2768 * When there are no more items, an empty string is returned and *pp is set to
2769 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002770 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2771 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 */
2773 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002774next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 char_u *p;
2777 char_u *r;
2778
Bram Moolenaarf077db22019-08-13 00:18:24 +02002779 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (**pp == NUL)
2781 {
2782 *pp = NULL;
2783 return (char_u *)"";
2784 }
2785 p = vim_strchr(*pp, ',');
2786 if (p == NULL)
2787 {
2788 r = enc_canonize(*pp);
2789 *pp += STRLEN(*pp);
2790 }
2791 else
2792 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002793 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 *pp = p + 1;
2795 if (r != NULL)
2796 {
2797 p = enc_canonize(r);
2798 vim_free(r);
2799 r = p;
2800 }
2801 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002802 if (r != NULL)
2803 *alloced = TRUE;
2804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002806 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 r = (char_u *)"";
2808 *pp = NULL;
2809 }
2810 return r;
2811}
2812
Bram Moolenaar13505972019-01-24 15:04:48 +01002813#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814/*
2815 * Convert a file with the 'charconvert' expression.
2816 * This closes the file which is to be read, converts it and opens the
2817 * resulting file for reading.
2818 * Returns name of the resulting converted file (the caller should delete it
2819 * after reading it).
2820 * Returns NULL if the conversion failed ("*fdp" is not set) .
2821 */
2822 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002823readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002824 char_u *fname, // name of input file
2825 char_u *fenc, // converted from
2826 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002829 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002831 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002833 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else
2835 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002836 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 *fdp = -1;
2838 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2839 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002840 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2842 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002843 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
2845
2846 if (errmsg != NULL)
2847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002848 // Don't use emsg(), it breaks mappings, the retry with
2849 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002850 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (tmpname != NULL)
2852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002853 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002854 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856 }
2857
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002858 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (*fdp < 0)
2860 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2861
2862 return tmpname;
2863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002866#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002868 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2870 * *filesizep are updated.
2871 * Return the (new) encryption key, NULL for no encryption.
2872 */
2873 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002874check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 char_u *cryptkey, // previous encryption key or NULL
2876 char_u *ptr, // pointer to read bytes
2877 long *sizep, // length of read bytes
2878 off_T *filesizep, // nr of bytes used from file
2879 int newfile, // editing a new buffer
2880 char_u *fname, // file name to display
2881 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002883 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002884 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002885
2886 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002888 // Mark the buffer as read-only until the decryption has taken place.
2889 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002890 curbuf->b_p_ro = TRUE;
2891
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002892 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002893 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002894 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 if (*curbuf->b_p_key)
2897 cryptkey = curbuf->b_p_key;
2898 else
2899 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002900 // When newfile is TRUE, store the typed key in the 'key'
2901 // option and don't free it. bf needs hash of the key saved.
2902 // Don't ask for the key again when first time Enter was hit.
2903 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002904 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002905 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002906 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002907 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002908 *did_ask = TRUE;
2909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002910 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 if (cryptkey != NULL && *cryptkey == NUL)
2912 {
2913 if (cryptkey != curbuf->b_p_key)
2914 vim_free(cryptkey);
2915 cryptkey = NULL;
2916 }
2917 }
2918 }
2919
2920 if (cryptkey != NULL)
2921 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002922 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002923
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002924 curbuf->b_cryptstate = crypt_create_from_header(
2925 method, cryptkey, ptr);
2926 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002928 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002929 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002930 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002931 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002932 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002933 *filesizep += header_len;
2934 *sizep -= header_len;
2935 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002938 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002941 // When starting to edit a new file which does not have encryption, clear
2942 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002943 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2945
2946 return cryptkey;
2947}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002948#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002951 * Return TRUE if a file appears to be read-only from the file permissions.
2952 */
2953 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002954check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002955 char_u *fname, // full path to file
2956 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002957{
2958#ifndef USE_MCH_ACCESS
2959 int fd = 0;
2960#endif
2961
2962 return (
2963#ifdef USE_MCH_ACCESS
2964# ifdef UNIX
2965 (perm & 0222) == 0 ||
2966# endif
2967 mch_access((char *)fname, W_OK)
2968#else
2969 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2970 ? TRUE : (close(fd), FALSE)
2971#endif
2972 );
2973}
2974
Bram Moolenaara7870192019-02-14 12:56:36 +01002975#if defined(HAVE_FSYNC) || defined(PROTO)
2976/*
2977 * Call fsync() with Mac-specific exception.
2978 * Return fsync() result: zero for success.
2979 */
2980 int
2981vim_fsync(int fd)
2982{
2983 int r;
2984
2985# ifdef MACOS_X
2986 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002987 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002988# endif
2989 r = fsync(fd);
2990 return r;
2991}
2992#endif
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002995 * Set the name of the current buffer. Use when the buffer doesn't have a
2996 * name and a ":r" or ":w" command with a file name is used.
2997 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002999set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003000{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003001 buf_T *buf = curbuf;
3002
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003003 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003004 if (curbuf->b_p_bl)
3005 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3006 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003007#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003008 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003009 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003010#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003011 if (curbuf != buf)
3012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003015 return FAIL;
3016 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003017
3018 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3019 curbuf->b_flags |= BF_NOTEDITED;
3020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003021 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003022 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3023 if (curbuf->b_p_bl)
3024 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003025#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003026 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003027 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003028#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003030 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003031 if (*curbuf->b_p_ft == NUL)
3032 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003033 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003034 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003035 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003036 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003037
3038 return OK;
3039}
3040
3041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 * Put file name into IObuff with quotes.
3043 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003045msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 if (fname == NULL)
3048 fname = (char_u *)"-stdin-";
3049 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3050 IObuff[0] = '"';
3051 STRCAT(IObuff, "\" ");
3052}
3053
3054/*
3055 * Append message for text mode to IObuff.
3056 * Return TRUE if something appended.
3057 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061#ifndef USE_CRNL
3062 if (eol_type == EOL_DOS)
3063 {
3064 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3065 return TRUE;
3066 }
3067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (eol_type == EOL_MAC)
3069 {
3070 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3071 return TRUE;
3072 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003073#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (eol_type == EOL_UNIX)
3075 {
3076 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3077 return TRUE;
3078 }
3079#endif
3080 return FALSE;
3081}
3082
3083/*
3084 * Append line and character count to IObuff.
3085 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087msg_add_lines(
3088 int insert_space,
3089 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003090 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 char_u *p;
3093
3094 p = IObuff + STRLEN(IObuff);
3095
3096 if (insert_space)
3097 *p++ = ' ';
3098 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003099 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003100 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
3102 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003103 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003105 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003106 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003107 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
3109}
3110
3111/*
3112 * Append message for missing line separator to IObuff.
3113 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003115msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3118}
3119
Bram Moolenaar473952e2019-09-28 16:30:04 +02003120 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003121time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003123#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003124 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3125 // the seconds. Since the roundoff is done when flushing the inode, the
3126 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return (t1 - t2 > 1 || t2 - t1 > 1);
3128#else
3129 return (t1 != t2);
3130#endif
3131}
3132
3133/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003134 * Return TRUE if file encoding "fenc" requires conversion from or to
3135 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003137 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003138need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003140 int same_encoding;
3141 int enc_flags;
3142 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003144 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003145 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003146 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003147 fenc_flags = 0;
3148 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003149 else
3150 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003151 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3152 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003153 enc_flags = get_fio_flags(p_enc);
3154 fenc_flags = get_fio_flags(fenc);
3155 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3156 }
3157 if (same_encoding)
3158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003159 // Specified encoding matches with 'encoding'. This requires
3160 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003161 return enc_unicode != 0;
3162 }
3163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003164 // Encodings differ. However, conversion is not needed when 'enc' is any
3165 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003166 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167}
3168
3169/*
3170 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3171 * internal conversion.
3172 * if "ptr" is an empty string, use 'encoding'.
3173 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003174 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003175get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 int prop;
3178
3179 if (*ptr == NUL)
3180 ptr = p_enc;
3181
3182 prop = enc_canon_props(ptr);
3183 if (prop & ENC_UNICODE)
3184 {
3185 if (prop & ENC_2BYTE)
3186 {
3187 if (prop & ENC_ENDIAN_L)
3188 return FIO_UCS2 | FIO_ENDIAN_L;
3189 return FIO_UCS2;
3190 }
3191 if (prop & ENC_4BYTE)
3192 {
3193 if (prop & ENC_ENDIAN_L)
3194 return FIO_UCS4 | FIO_ENDIAN_L;
3195 return FIO_UCS4;
3196 }
3197 if (prop & ENC_2WORD)
3198 {
3199 if (prop & ENC_ENDIAN_L)
3200 return FIO_UTF16 | FIO_ENDIAN_L;
3201 return FIO_UTF16;
3202 }
3203 return FIO_UTF8;
3204 }
3205 if (prop & ENC_LATIN1)
3206 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003207 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 return 0;
3209}
3210
Bram Moolenaar473952e2019-09-28 16:30:04 +02003211#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212/*
3213 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3214 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3215 * Used for conversion between 'encoding' and 'fileencoding'.
3216 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003217 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003218get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 int cp;
3221
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003222 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (!enc_utf8 && enc_codepage <= 0)
3224 return 0;
3225
3226 cp = encname2codepage(ptr);
3227 if (cp == 0)
3228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (STRCMP(ptr, "utf-8") == 0)
3231 cp = CP_UTF8;
3232 else
3233# endif
3234 return 0;
3235 }
3236 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3237}
3238#endif
3239
Bram Moolenaar473952e2019-09-28 16:30:04 +02003240#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241/*
3242 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3243 * needed for the internal conversion to/from utf-8 or latin1.
3244 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003245 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003246get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3249 && (enc_canon_props(ptr) & ENC_MACROMAN))
3250 return FIO_MACROMAN;
3251 return 0;
3252}
3253#endif
3254
3255/*
3256 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3257 * "size" must be at least 2.
3258 * Return the name of the encoding and set "*lenp" to the length.
3259 * Returns NULL when no BOM found.
3260 */
3261 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003262check_for_bom(
3263 char_u *p,
3264 long size,
3265 int *lenp,
3266 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 char *name = NULL;
3269 int len = 2;
3270
3271 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003272 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003274 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 len = 3;
3276 }
3277 else if (p[0] == 0xff && p[1] == 0xfe)
3278 {
3279 if (size >= 4 && p[2] == 0 && p[3] == 0
3280 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003282 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 len = 4;
3284 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003285 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003286 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003287 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003288 // utf-16le is preferred, it also works for ucs-2le text
3289 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 else if (p[0] == 0xfe && p[1] == 0xff
3292 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003294 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003295 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003296 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003297 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003298 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3301 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003303 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 len = 4;
3305 }
3306
3307 *lenp = len;
3308 return (char_u *)name;
3309}
3310
3311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 * Try to find a shortname by comparing the fullname with the current
3313 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003314 * Returns "full_path" or pointer into "full_path" if shortened.
3315 */
3316 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003317shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003318{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003319 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003320 char_u *p = full_path;
3321
Bram Moolenaard9462e32011-04-11 21:35:11 +02003322 dirname = alloc(MAXPATHL);
3323 if (dirname == NULL)
3324 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003325 if (mch_dirname(dirname, MAXPATHL) == OK)
3326 {
3327 p = shorten_fname(full_path, dirname);
3328 if (p == NULL || *p == NUL)
3329 p = full_path;
3330 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003331 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003332 return p;
3333}
3334
3335/*
3336 * Try to find a shortname by comparing the fullname with the current
3337 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 * Returns NULL if not shorter name possible, pointer into "full_path"
3339 * otherwise.
3340 */
3341 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003342shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int len;
3345 char_u *p;
3346
3347 if (full_path == NULL)
3348 return NULL;
3349 len = (int)STRLEN(dir_name);
3350 if (fnamencmp(dir_name, full_path, len) == 0)
3351 {
3352 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003353#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003355 * MS-Windows: when a file is in the root directory, dir_name will end
3356 * in a slash, since C: by itself does not define a specific dir. In
3357 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
3359 if (!((len > 2) && (*(p - 2) == ':')))
3360#endif
3361 {
3362 if (vim_ispathsep(*p))
3363 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003364#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 else
3366 p = NULL;
3367#endif
3368 }
3369 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003370#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 /*
3372 * When using a file in the current drive, remove the drive name:
3373 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3374 * a floppy from "A:\dir" to "B:\dir".
3375 */
3376 else if (len > 3
3377 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3378 && full_path[1] == ':'
3379 && vim_ispathsep(full_path[2]))
3380 p = full_path + 2;
3381#endif
3382 else
3383 p = NULL;
3384 return p;
3385}
3386
3387/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003388 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * When "force" is TRUE: Use full path from now on for files currently being
3390 * edited, both for file name and swap file name. Try to shorten the file
3391 * names a bit, if safe to do so.
3392 * When "force" is FALSE: Only try to shorten absolute file names.
3393 * For buffers that have buftype "nofile" or "scratch": never change the file
3394 * name.
3395 */
3396 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003397shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3398{
3399 char_u *p;
3400
3401 if (buf->b_fname != NULL
3402#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003403 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003404#endif
3405 && !path_with_url(buf->b_fname)
3406 && (force
3407 || buf->b_sfname == NULL
3408 || mch_isFullName(buf->b_sfname)))
3409 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003410 if (buf->b_sfname != buf->b_ffname)
3411 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003412 p = shorten_fname(buf->b_ffname, dirname);
3413 if (p != NULL)
3414 {
3415 buf->b_sfname = vim_strsave(p);
3416 buf->b_fname = buf->b_sfname;
3417 }
3418 if (p == NULL || buf->b_fname == NULL)
3419 buf->b_fname = buf->b_ffname;
3420 }
3421}
3422
3423/*
3424 * Shorten filenames for all buffers.
3425 */
3426 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 char_u dirname[MAXPATHL];
3430 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003433 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003435 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003436
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003437 // Always make the swap file name a full path, a "nofile" buffer may
3438 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003439 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003442 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003443#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003444 popup_update_preview_title();
3445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446}
3447
3448#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3449 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003450 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 || defined(PROTO)
3452/*
3453 * Shorten all filenames in "fnames[count]" by current directory.
3454 */
3455 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003456shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 int i;
3459 char_u dirname[MAXPATHL];
3460 char_u *p;
3461
3462 if (fnames == NULL || count < 1)
3463 return;
3464 mch_dirname(dirname, sizeof(dirname));
3465 for (i = 0; i < count; ++i)
3466 {
3467 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3468 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003469 // shorten_fname() returns pointer in given "fnames[i]". If free
3470 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3471 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 p = vim_strsave(p);
3473 vim_free(fnames[i]);
3474 fnames[i] = p;
3475 }
3476 }
3477}
3478#endif
3479
3480/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003481 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * fo_o_h.ext for MSDOS or when shortname option set.
3483 *
3484 * Assumed that fname is a valid name found in the filesystem we assure that
3485 * the return value is a different name and ends in 'ext'.
3486 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3487 * characters otherwise.
3488 * Space for the returned name is allocated, must be freed later.
3489 * Returns NULL when out of memory.
3490 */
3491 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003492modname(
3493 char_u *fname,
3494 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003495 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003497 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 fname, ext, prepend_dot);
3499}
3500
3501 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003502buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003503 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003504 char_u *fname,
3505 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003506 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 char_u *retval;
3509 char_u *s;
3510 char_u *e;
3511 char_u *ptr;
3512 int fnamelen, extlen;
3513
3514 extlen = (int)STRLEN(ext);
3515
3516 /*
3517 * If there is no file name we must get the name of the current directory
3518 * (we need the full path in case :cd is used).
3519 */
3520 if (fname == NULL || *fname == NUL)
3521 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003522 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (retval == NULL)
3524 return NULL;
3525 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3526 (fnamelen = (int)STRLEN(retval)) == 0)
3527 {
3528 vim_free(retval);
3529 return NULL;
3530 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003531 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 retval[fnamelen++] = PATHSEP;
3534 retval[fnamelen] = NUL;
3535 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003536 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 else
3539 {
3540 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003541 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (retval == NULL)
3543 return NULL;
3544 STRCPY(retval, fname);
3545#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003546 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547#endif
3548 }
3549
3550 /*
3551 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3552 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3553 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3554 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3555 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003556 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003558 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003559 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003562 {
3563 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003568 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3570 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 s = ptr + STRLEN(ptr);
3573
3574 /*
3575 * For 8.3 file names we may have to reduce the length.
3576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 /*
3580 * If there is no file name, or the file name ends in '/', and the
3581 * extension starts with '.', put a '_' before the dot, because just
3582 * ".ext" is invalid.
3583 */
3584 if (fname == NULL || *fname == NUL
3585 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 *s++ = '_';
3589 }
3590 /*
3591 * If the extension starts with '.', truncate the base name at 8
3592 * characters
3593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003596 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 s = ptr + 8;
3599 *s = '\0';
3600 }
3601 }
3602 /*
3603 * If the extension doesn't start with '.', and the file name
3604 * doesn't have an extension yet, append a '.'
3605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 else if ((e = vim_strchr(ptr, '.')) == NULL)
3607 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 /*
3609 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003610 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 */
3612 else if ((int)STRLEN(e) + extlen > 4)
3613 s = e + 4 - extlen;
3614 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003615#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /*
3617 * If there is no file name, and the extension starts with '.', put a
3618 * '_' before the dot, because just ".ext" may be invalid if it's on a
3619 * FAT partition, and on HPFS it doesn't matter.
3620 */
3621 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3622 *s++ = '_';
3623#endif
3624
3625 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003626 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 * ext can start with '.' and cannot exceed 3 more characters.
3628 */
3629 STRCPY(s, ext);
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 /*
3632 * Prepend the dot.
3633 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003634 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003636 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 /*
3641 * Check that, after appending the extension, the file name is really
3642 * different.
3643 */
3644 if (fname != NULL && STRCMP(fname, retval) == 0)
3645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003646 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 while (--s >= ptr)
3648 {
3649 if (*s != '_')
3650 {
3651 *s = '_';
3652 break;
3653 }
3654 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003655 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 *ptr = 'v';
3657 }
3658 return retval;
3659}
3660
3661/*
3662 * Like fgets(), but if the file line is too long, it is truncated and the
3663 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003664 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003667vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669 char *eof;
3670#define FGETS_SIZE 200
3671 char tbuf[FGETS_SIZE];
3672
3673 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3676 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003677 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003679 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 do
3681 {
3682 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003683 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3685 }
3686 return (eof == NULL);
3687}
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689/*
3690 * rename() only works if both files are on the same file system, this
3691 * function will (attempts to?) copy the file across if rename fails -- webb
3692 * Return -1 for failure, 0 for success.
3693 */
3694 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003695vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
3697 int fd_in;
3698 int fd_out;
3699 int n;
3700 char *errmsg = NULL;
3701 char *buffer;
3702#ifdef AMIGA
3703 BPTR flock;
3704#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003705 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003706 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003707#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003708 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003709#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003710 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003713 * When the names are identical, there is nothing to do. When they refer
3714 * to the same file (ignoring case and slash/backslash differences) but
3715 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
3717 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003718 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003719 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003720 use_tmp_file = TRUE;
3721 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003722 return 0;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 /*
3726 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3727 */
3728 if (mch_stat((char *)from, &st) < 0)
3729 return -1;
3730
Bram Moolenaar3576da72008-12-30 15:15:57 +00003731#ifdef UNIX
3732 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003733 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003735 // It's possible for the source and destination to be the same file.
3736 // This happens when "from" and "to" differ in case and are on a FAT32
3737 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003738 if (mch_stat((char *)to, &st_to) >= 0
3739 && st.st_dev == st_to.st_dev
3740 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003741 use_tmp_file = TRUE;
3742 }
3743#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003744#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003745 {
3746 BY_HANDLE_FILE_INFORMATION info1, info2;
3747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003748 // It's possible for the source and destination to be the same file.
3749 // In that case go through a temp file name. This makes rename("foo",
3750 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003751 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3752 && win32_fileinfo(to, &info2) == FILEINFO_OK
3753 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3754 && info1.nFileIndexHigh == info2.nFileIndexHigh
3755 && info1.nFileIndexLow == info2.nFileIndexLow)
3756 use_tmp_file = TRUE;
3757 }
3758#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003759
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003760 if (use_tmp_file)
3761 {
3762 char tempname[MAXPATHL + 1];
3763
3764 /*
3765 * Find a name that doesn't exist and is in the same directory.
3766 * Rename "from" to "tempname" and then rename "tempname" to "to".
3767 */
3768 if (STRLEN(from) >= MAXPATHL - 5)
3769 return -1;
3770 STRCPY(tempname, from);
3771 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003772 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003773 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3774 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003775 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003776 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003777 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003778 if (mch_rename(tempname, (char *)to) == 0)
3779 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003780 // Strange, the second step failed. Try moving the
3781 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003782 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003783 return -1;
3784 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003785 // If it fails for one temp name it will most likely fail
3786 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003787 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003788 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003789 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003790 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003791 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 /*
3794 * Delete the "to" file, this is required on some systems to make the
3795 * mch_rename() work, on other systems it makes sure that we don't have
3796 * two files when the mch_rename() fails.
3797 */
3798
3799#ifdef AMIGA
3800 /*
3801 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3802 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003803 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 * deleting the "from" file (horror!) we lock it during the remove.
3805 *
3806 * When used for making a backup before writing the file: This should not
3807 * happen with ":w", because startscript() should detect this problem and
3808 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3809 * name. This problem does exist with ":w filename", but then the
3810 * original file will be somewhere else so the backup isn't really
3811 * important. If autoscripting is off the rename may fail.
3812 */
3813 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3814#endif
3815 mch_remove(to);
3816#ifdef AMIGA
3817 if (flock)
3818 UnLock(flock);
3819#endif
3820
3821 /*
3822 * First try a normal rename, return if it works.
3823 */
3824 if (mch_rename((char *)from, (char *)to) == 0)
3825 return 0;
3826
3827 /*
3828 * Rename() failed, try copying the file.
3829 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003830 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003831#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003832 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003833 acl = mch_get_acl(from);
3834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3836 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003837 {
3838#ifdef HAVE_ACL
3839 mch_free_acl(acl);
3840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003842 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003844 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003845 fd_out = mch_open((char *)to,
3846 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (fd_out == -1)
3848 {
3849 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003850#ifdef HAVE_ACL
3851 mch_free_acl(acl);
3852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return -1;
3854 }
3855
Bram Moolenaar473952e2019-09-28 16:30:04 +02003856 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (buffer == NULL)
3858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003860 close(fd_in);
3861#ifdef HAVE_ACL
3862 mch_free_acl(acl);
3863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 return -1;
3865 }
3866
Bram Moolenaar473952e2019-09-28 16:30:04 +02003867 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003868 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 errmsg = _("E208: Error writing to \"%s\"");
3871 break;
3872 }
3873
3874 vim_free(buffer);
3875 close(fd_in);
3876 if (close(fd_out) < 0)
3877 errmsg = _("E209: Error closing \"%s\"");
3878 if (n < 0)
3879 {
3880 errmsg = _("E210: Error reading \"%s\"");
3881 to = from;
3882 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003884 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003885#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003886#ifdef HAVE_ACL
3887 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003888 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003889#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003890#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003891 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (errmsg != NULL)
3894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003895 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return -1;
3897 }
3898 mch_remove(from);
3899 return 0;
3900}
3901
3902static int already_warned = FALSE;
3903
3904/*
3905 * Check if any not hidden buffer has been changed.
3906 * Postpone the check if there are characters in the stuff buffer, a global
3907 * command is being executed, a mapping is being executed or an autocommand is
3908 * busy.
3909 * Returns TRUE if some message was written (screen should be redrawn and
3910 * cursor positioned).
3911 */
3912 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003913check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003914 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 buf_T *buf;
3917 int didit = 0;
3918 int n;
3919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003920 // Don't check timestamps while system() or another low-level function may
3921 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (no_check_timestamps > 0)
3923 return FALSE;
3924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003925 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3926 // event and we would keep on checking if the file is steadily growing.
3927 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (focus && did_check_timestamps)
3929 {
3930 need_check_timestamps = TRUE;
3931 return FALSE;
3932 }
3933
3934 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003935 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 else
3938 {
3939 ++no_wait_return;
3940 did_check_timestamps = TRUE;
3941 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003942 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003944 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (buf->b_nwindows > 0)
3946 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003947 bufref_T bufref;
3948
3949 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 n = buf_check_timestamp(buf, focus);
3951 if (didit < n)
3952 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003953 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003955 // Autocommands have removed the buffer, start at the
3956 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 buf = firstbuf;
3958 continue;
3959 }
3960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 --no_wait_return;
3963 need_check_timestamps = FALSE;
3964 if (need_wait_return && didit == 2)
3965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003966 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003967 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 out_flush();
3969 }
3970 }
3971 return didit;
3972}
3973
3974/*
3975 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3976 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3977 * empty.
3978 */
3979 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003980move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 buf_T *tbuf = curbuf;
3983 int retval = OK;
3984 linenr_T lnum;
3985 char_u *p;
3986
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003987 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 curbuf = tobuf;
3989 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3990 {
3991 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3992 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3993 {
3994 vim_free(p);
3995 retval = FAIL;
3996 break;
3997 }
3998 vim_free(p);
3999 }
4000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004001 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (retval != FAIL)
4003 {
4004 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004005 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004006 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004008 // Oops! We could try putting back the saved lines, but that
4009 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 retval = FAIL;
4011 break;
4012 }
4013 }
4014
4015 curbuf = tbuf;
4016 return retval;
4017}
4018
4019/*
4020 * Check if buffer "buf" has been changed.
4021 * Also check if the file for a new buffer unexpectedly appeared.
4022 * return 1 if a changed buffer was found.
4023 * return 2 if a message has been displayed.
4024 * return 0 otherwise.
4025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004027buf_check_timestamp(
4028 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004029 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004031 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int stat_res;
4033 int retval = 0;
4034 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004035 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004037 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 int helpmesg = FALSE;
4039 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004040 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4042 int can_reload = FALSE;
4043#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004044 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 int orig_mode = buf->b_orig_mode;
4046#ifdef FEAT_GUI
4047 int save_mouse_correct = need_mouse_correct;
4048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004050 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004051#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004052 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004053#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004054 bufref_T bufref;
4055
4056 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004058 // If there is no file name, the buffer is not loaded, 'buftype' is
4059 // set, we are in the middle of a save or being called recursively: ignore
4060 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if (buf->b_ffname == NULL
4062 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004063 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004066#ifdef FEAT_NETBEANS_INTG
4067 || isNetbeansBuffer(buf)
4068#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004069#ifdef FEAT_TERMINAL
4070 || buf->b_term != NULL
4071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 )
4073 return 0;
4074
4075 if ( !(buf->b_flags & BF_NOTEDITED)
4076 && buf->b_mtime != 0
4077 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4078 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004079 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080#ifdef HAVE_ST_MODE
4081 || (int)st.st_mode != buf->b_orig_mode
4082#else
4083 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4084#endif
4085 ))
4086 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004087 long prev_b_mtime = buf->b_mtime;
4088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 retval = 1;
4090
Bram Moolenaar386bc822018-07-07 18:34:12 +02004091 // set b_mtime to stop further warnings (e.g., when executing
4092 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (stat_res < 0)
4094 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004095 // Check the file again later to see if it re-appears.
4096 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 buf->b_orig_size = 0;
4098 buf->b_orig_mode = 0;
4099 }
4100 else
4101 buf_store_time(buf, &st, buf->b_ffname);
4102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004103 // Don't do anything for a directory. Might contain the file
4104 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (mch_isdir(buf->b_fname))
4106 ;
4107
4108 /*
4109 * If 'autoread' is set, the buffer has no changes and the file still
4110 * exists, reload the buffer. Use the buffer-local option value if it
4111 * was set, the global option value otherwise.
4112 */
4113 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4114 && !bufIsChanged(buf) && stat_res >= 0)
4115 reload = TRUE;
4116 else
4117 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004118 if (stat_res < 0)
4119 reason = "deleted";
4120 else if (bufIsChanged(buf))
4121 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004122 /*
4123 * Check if the file contents really changed to avoid giving a
4124 * warning when only the timestamp was set (e.g., checked out of
4125 * CVS). Always warn when the buffer was changed.
4126 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004127 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4128 reason = "changed";
4129 else if (orig_mode != buf->b_orig_mode)
4130 reason = "mode";
4131 else
4132 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134 /*
4135 * Only give the warning if there are no FileChangedShell
4136 * autocommands.
4137 * Avoid being called recursively by setting "busy".
4138 */
4139 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004140#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004141 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4142 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004143#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004144 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4146 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004147 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 busy = FALSE;
4149 if (n)
4150 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004151 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004153#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004154 s = get_vim_var_str(VV_FCS_CHOICE);
4155 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4156 reload = TRUE;
4157 else if (STRCMP(s, "ask") == 0)
4158 n = FALSE;
4159 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004160#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004161 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004163 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004165 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004166 {
4167 // Only give the message once.
4168 if (prev_b_mtime != -1)
4169 mesg = _("E211: File \"%s\" no longer available");
4170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
4172 {
4173 helpmesg = TRUE;
4174#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4175 can_reload = TRUE;
4176#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004177 if (reason[2] == 'n')
4178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004180 mesg2 = _("See \":help W12\" for more info.");
4181 }
4182 else if (reason[1] == 'h')
4183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004185 mesg2 = _("See \":help W11\" for more info.");
4186 }
4187 else if (*reason == 'm')
4188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004190 mesg2 = _("See \":help W16\" for more info.");
4191 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004192 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004193 // Only timestamp changed, store it to avoid a warning
4194 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004195 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 }
4198 }
4199
4200 }
4201 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4202 && vim_fexists(buf->b_ffname))
4203 {
4204 retval = 1;
4205 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4206 buf->b_flags |= BF_NEW_W;
4207#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4208 can_reload = TRUE;
4209#endif
4210 }
4211
4212 if (mesg != NULL)
4213 {
4214 path = home_replace_save(buf, buf->b_fname);
4215 if (path != NULL)
4216 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004217 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004219 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004220 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004221#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004222 // Set warningmsg here, before the unimportant and output-specific
4223 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004224 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4227 if (can_reload)
4228 {
4229 if (*mesg2 != NUL)
4230 {
4231 STRCAT(tbuf, "\n");
4232 STRCAT(tbuf, mesg2);
4233 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004234 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4235 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004236 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 reload = TRUE;
4238 }
4239 else
4240#endif
4241 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4242 {
4243 if (*mesg2 != NUL)
4244 {
4245 STRCAT(tbuf, "; ");
4246 STRCAT(tbuf, mesg2);
4247 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004248 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 retval = 2;
4250 }
4251 else
4252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004256 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 msg_clr_eos();
4260 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004261 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004264#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004266#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004267 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004268 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004270 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 redraw_cmdline = FALSE;
4272 }
4273 }
4274 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276
4277 vim_free(path);
4278 vim_free(tbuf);
4279 }
4280 }
4281
4282 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004284 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004285 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004286#ifdef FEAT_PERSISTENT_UNDO
4287 if (buf->b_p_udf && buf->b_ffname != NULL)
4288 {
4289 char_u hash[UNDO_HASH_SIZE];
4290 buf_T *save_curbuf = curbuf;
4291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004292 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004293 curbuf = buf;
4294 u_compute_hash(hash);
4295 u_write_undo(NULL, FALSE, buf, hash);
4296 curbuf = save_curbuf;
4297 }
4298#endif
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004301 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004302 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004303 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4304 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 // restore this in case an autocommand has set it; it would break
4307 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 need_mouse_correct = save_mouse_correct;
4309#endif
4310
4311 return retval;
4312}
4313
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004314/*
4315 * Reload a buffer that is already loaded.
4316 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004317 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4318 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004319 */
4320 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004321buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004322{
4323 exarg_T ea;
4324 pos_T old_cursor;
4325 linenr_T old_topline;
4326 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004328 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004329 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004330 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004331 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004332
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004333 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004334 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004336 // We only want to read the text from the file, not reset the syntax
4337 // highlighting, clear marks, diff status, etc. Force the fileformat
4338 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004339 if (prep_exarg(&ea, buf) == OK)
4340 {
4341 old_cursor = curwin->w_cursor;
4342 old_topline = curwin->w_topline;
4343
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004344 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 // Save all the text, so that the reload can be undone.
4347 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004348 u_sync(FALSE);
4349 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4350 flags |= READ_KEEP_UNDO;
4351 }
4352
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004353 /*
4354 * To behave like when a new file is edited (matters for
4355 * BufReadPost autocommands) we first need to delete the current
4356 * buffer contents. But if reading the file fails we should keep
4357 * the old contents. Can't use memory only, the file might be
4358 * too big. Use a hidden buffer to move the buffer contents to.
4359 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004360 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 savebuf = NULL;
4362 else
4363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004366 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004367 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004368 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004370 curbuf = savebuf;
4371 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004372 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004373 curbuf = buf;
4374 curwin->w_buffer = buf;
4375 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004376 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004377 || move_lines(buf, savebuf) == FAIL)
4378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004379 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004380 buf->b_fname);
4381 saved = FAIL;
4382 }
4383 }
4384
4385 if (saved == OK)
4386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4388 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004389 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4390 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004391 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004392 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004393#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004394 if (!aborting())
4395#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004396 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004397 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // Put the text back from the save buffer. First
4400 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004401 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004402 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004403 break;
4404 (void)move_lines(savebuf, buf);
4405 }
4406 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004407 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004408 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004410 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004411 if ((flags & READ_KEEP_UNDO) == 0)
4412 {
4413 u_blockfree(buf);
4414 u_clearall(buf);
4415 }
4416 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004418 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004419 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004420 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004421 }
4422 }
4423 vim_free(ea.cmd);
4424
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004425 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004426 wipe_buffer(savebuf, FALSE);
4427
4428#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004430 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004431#endif
4432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004433 // Restore the topline and cursor position and check it (lines may
4434 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004435 if (old_topline > curbuf->b_ml.ml_line_count)
4436 curwin->w_topline = curbuf->b_ml.ml_line_count;
4437 else
4438 curwin->w_topline = old_topline;
4439 curwin->w_cursor = old_cursor;
4440 check_cursor();
4441 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004442 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004443#ifdef FEAT_FOLDING
4444 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004445 win_T *wp;
4446 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004449 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004450 if (wp->w_buffer == curwin->w_buffer
4451 && !foldmethodIsManual(wp))
4452 foldUpdateAll(wp);
4453 }
4454#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 // If the mode didn't change and 'readonly' was set, keep the old
4456 // value; the user probably used the ":view" command. But don't
4457 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004458 if (orig_mode == curbuf->b_orig_mode)
4459 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004462 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004463 }
4464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004465 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004466 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004467 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004468}
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004471buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004474 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475#ifdef HAVE_ST_MODE
4476 buf->b_orig_mode = (int)st->st_mode;
4477#else
4478 buf->b_orig_mode = mch_getperm(fname);
4479#endif
4480}
4481
4482/*
4483 * Adjust the line with missing eol, used for the next write.
4484 * Used for do_filter(), when the input lines for the filter are deleted.
4485 */
4486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004487write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004490 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491}
4492
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004493// Subfuncions for readdirex()
4494#ifdef FEAT_EVAL
4495# ifdef MSWIN
4496 static char_u *
4497getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4498{
4499 stat_T st;
4500 unsigned short st_mode;
4501 DWORD flag = wfd->dwFileAttributes;
4502 WCHAR *wp;
4503
4504 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4505 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4506 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4507 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4508
4509 wp = wcsrchr(wfd->cFileName, L'.');
4510 if (wp != NULL)
4511 {
4512 if (_wcsicmp(wp, L".exe") == 0 ||
4513 _wcsicmp(wp, L".com") == 0 ||
4514 _wcsicmp(wp, L".cmd") == 0 ||
4515 _wcsicmp(wp, L".bat") == 0)
4516 st_mode |= _S_IEXEC;
4517 }
4518
4519 // Copy user bits to group/other.
4520 st_mode |= (st_mode & 0700) >> 3;
4521 st_mode |= (st_mode & 0700) >> 6;
4522
4523 st.st_mode = st_mode;
4524 return getfpermst(&st, perm);
4525}
4526
4527 static char_u *
4528getftypewfd(WIN32_FIND_DATAW *wfd)
4529{
4530 DWORD flag = wfd->dwFileAttributes;
4531 DWORD tag = wfd->dwReserved0;
4532
4533 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4534 {
4535 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4536 return (char_u*)"junction";
4537 else if (tag == IO_REPARSE_TAG_SYMLINK)
4538 {
4539 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4540 return (char_u*)"linkd";
4541 else
4542 return (char_u*)"link";
4543 }
4544 return (char_u*)"reparse"; // unknown reparse point type
4545 }
4546 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4547 return (char_u*)"dir";
4548 else
4549 return (char_u*)"file";
4550}
4551
4552 static dict_T *
4553create_readdirex_item(WIN32_FIND_DATAW *wfd)
4554{
4555 dict_T *item;
4556 char_u *p;
4557 varnumber_T size, time;
4558 char_u permbuf[] = "---------";
4559
4560 item = dict_alloc();
4561 if (item == NULL)
4562 return NULL;
4563 item->dv_refcount++;
4564
4565 p = utf16_to_enc(wfd->cFileName, NULL);
4566 if (p == NULL)
4567 goto theend;
4568 if (dict_add_string(item, "name", p) == FAIL)
4569 {
4570 vim_free(p);
4571 goto theend;
4572 }
4573 vim_free(p);
4574
4575 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4576 if (dict_add_number(item, "size", size) == FAIL)
4577 goto theend;
4578
4579 // Convert FILETIME to unix time.
4580 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4581 wfd->ftLastWriteTime.dwLowDateTime)
4582 - 116444736000000000) / 10000000;
4583 if (dict_add_number(item, "time", time) == FAIL)
4584 goto theend;
4585
4586 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4587 goto theend;
4588 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4589 goto theend;
4590
4591 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4592 goto theend;
4593 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4594 goto theend;
4595
4596 return item;
4597
4598theend:
4599 dict_unref(item);
4600 return NULL;
4601}
4602# else
4603 static dict_T *
4604create_readdirex_item(char_u *path, char_u *name)
4605{
4606 dict_T *item;
4607 char *p;
4608 size_t len;
4609 stat_T st;
4610 int ret, link = FALSE;
4611 varnumber_T size;
4612 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004613 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004614 struct passwd *pw;
4615 struct group *gr;
4616
4617 item = dict_alloc();
4618 if (item == NULL)
4619 return NULL;
4620 item->dv_refcount++;
4621
4622 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4623 p = alloc(len);
4624 if (p == NULL)
4625 goto theend;
4626 vim_snprintf(p, len, "%s/%s", path, name);
4627 ret = mch_lstat(p, &st);
4628 if (ret >= 0 && S_ISLNK(st.st_mode))
4629 {
4630 link = TRUE;
4631 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004632 if (ret < 0)
4633 q = (char_u*)"link";
4634
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004635 }
4636 vim_free(p);
4637
4638 if (dict_add_string(item, "name", name) == FAIL)
4639 goto theend;
4640
4641 if (ret >= 0)
4642 {
4643 size = (varnumber_T)st.st_size;
4644 if (S_ISDIR(st.st_mode))
4645 size = 0;
4646 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004647 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004648 size = -2;
4649 if (dict_add_number(item, "size", size) == FAIL)
4650 goto theend;
4651 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4652 goto theend;
4653
4654 if (link)
4655 {
4656 if (S_ISDIR(st.st_mode))
4657 q = (char_u*)"linkd";
4658 else
4659 q = (char_u*)"link";
4660 }
4661 else
4662 q = getftypest(&st);
4663 if (dict_add_string(item, "type", q) == FAIL)
4664 goto theend;
4665 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4666 goto theend;
4667
4668 pw = getpwuid(st.st_uid);
4669 if (pw == NULL)
4670 q = (char_u*)"";
4671 else
4672 q = (char_u*)pw->pw_name;
4673 if (dict_add_string(item, "user", q) == FAIL)
4674 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004675# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004676 gr = getgrgid(st.st_gid);
4677 if (gr == NULL)
4678 q = (char_u*)"";
4679 else
4680 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004681# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004682 if (dict_add_string(item, "group", q) == FAIL)
4683 goto theend;
4684 }
4685 else
4686 {
4687 if (dict_add_number(item, "size", -1) == FAIL)
4688 goto theend;
4689 if (dict_add_number(item, "time", -1) == FAIL)
4690 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004691 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004692 goto theend;
4693 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4694 goto theend;
4695 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4696 goto theend;
4697 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4698 goto theend;
4699 }
4700 return item;
4701
4702theend:
4703 dict_unref(item);
4704 return NULL;
4705}
4706# endif
4707
4708 static int
4709compare_readdirex_item(const void *p1, const void *p2)
4710{
4711 char_u *name1, *name2;
4712
4713 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4714 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004715 if (readdirex_sort == READDIR_SORT_BYTE)
4716 return STRCMP(name1, name2);
4717 else if (readdirex_sort == READDIR_SORT_IC)
4718 return STRICMP(name1, name2);
4719 else
4720 return STRCOLL(name1, name2);
4721}
4722
4723 static int
4724compare_readdir_item(const void *s1, const void *s2)
4725{
4726 if (readdirex_sort == READDIR_SORT_BYTE)
4727 return STRCMP(*(char **)s1, *(char **)s2);
4728 else if (readdirex_sort == READDIR_SORT_IC)
4729 return STRICMP(*(char **)s1, *(char **)s2);
4730 else
4731 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004732}
4733#endif
4734
Bram Moolenaarda440d22016-01-16 21:27:23 +01004735#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4736/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004737 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004738 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004739 * If "withattr" is TRUE, retrieve the names and their attributes.
4740 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004741 * Return OK for success, FAIL for failure.
4742 */
4743 int
4744readdir_core(
4745 garray_T *gap,
4746 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004747 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004748 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004749 int (*checkitem)(void *context, void *item),
4750 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004751{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004752 int failed = FALSE;
4753 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004754# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004755 char_u *buf;
4756 int ok;
4757 HANDLE hFind = INVALID_HANDLE_VALUE;
4758 WIN32_FIND_DATAW wfd;
4759 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004760# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004761 DIR *dirp;
4762 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004763# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004764
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004765 ga_init2(gap, (int)sizeof(void *), 20);
4766
4767# ifdef FEAT_EVAL
4768# define FREE_ITEM(item) do { \
4769 if (withattr) \
4770 dict_unref((dict_T*)item); \
4771 else \
4772 vim_free(item); \
4773 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004774
4775 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004776# else
4777# define FREE_ITEM(item) vim_free(item)
4778# endif
4779
4780# ifdef MSWIN
4781 buf = alloc(MAXPATHL);
4782 if (buf == NULL)
4783 return FAIL;
4784 STRNCPY(buf, path, MAXPATHL-5);
4785 p = buf + STRLEN(buf);
4786 MB_PTR_BACK(buf, p);
4787 if (*p == '\\' || *p == '/')
4788 *p = NUL;
4789 STRCAT(p, "\\*");
4790
4791 wn = enc_to_utf16(buf, NULL);
4792 if (wn != NULL)
4793 hFind = FindFirstFileW(wn, &wfd);
4794 ok = (hFind != INVALID_HANDLE_VALUE);
4795 if (!ok)
4796 {
4797 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004798 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004799 }
4800 else
4801 {
4802 while (ok)
4803 {
4804 int ignore;
4805 void *item;
4806 WCHAR *wp;
4807
4808 wp = wfd.cFileName;
4809 ignore = wp[0] == L'.' &&
4810 (wp[1] == NUL ||
4811 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004812 if (ignore)
4813 {
4814 ok = FindNextFileW(hFind, &wfd);
4815 continue;
4816 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004817# ifdef FEAT_EVAL
4818 if (withattr)
4819 item = (void*)create_readdirex_item(&wfd);
4820 else
4821# endif
4822 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4823 if (item == NULL)
4824 {
4825 failed = TRUE;
4826 break;
4827 }
4828
4829 if (!ignore && checkitem != NULL)
4830 {
4831 int r = checkitem(context, item);
4832
4833 if (r < 0)
4834 {
4835 FREE_ITEM(item);
4836 break;
4837 }
4838 if (r == 0)
4839 ignore = TRUE;
4840 }
4841
4842 if (!ignore)
4843 {
4844 if (ga_grow(gap, 1) == OK)
4845 ((void**)gap->ga_data)[gap->ga_len++] = item;
4846 else
4847 {
4848 failed = TRUE;
4849 FREE_ITEM(item);
4850 break;
4851 }
4852 }
4853 else
4854 FREE_ITEM(item);
4855
4856 ok = FindNextFileW(hFind, &wfd);
4857 }
4858 FindClose(hFind);
4859 }
4860
4861 vim_free(buf);
4862 vim_free(wn);
4863# else // MSWIN
4864 dirp = opendir((char *)path);
4865 if (dirp == NULL)
4866 {
4867 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004868 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004869 }
4870 else
4871 {
4872 for (;;)
4873 {
4874 int ignore;
4875 void *item;
4876
4877 dp = readdir(dirp);
4878 if (dp == NULL)
4879 break;
4880 p = (char_u *)dp->d_name;
4881
4882 ignore = p[0] == '.' &&
4883 (p[1] == NUL ||
4884 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004885 if (ignore)
4886 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004887# ifdef FEAT_EVAL
4888 if (withattr)
4889 item = (void*)create_readdirex_item(path, p);
4890 else
4891# endif
4892 item = (void*)vim_strsave(p);
4893 if (item == NULL)
4894 {
4895 failed = TRUE;
4896 break;
4897 }
4898
4899 if (!ignore && checkitem != NULL)
4900 {
4901 int r = checkitem(context, item);
4902
4903 if (r < 0)
4904 {
4905 FREE_ITEM(item);
4906 break;
4907 }
4908 if (r == 0)
4909 ignore = TRUE;
4910 }
4911
4912 if (!ignore)
4913 {
4914 if (ga_grow(gap, 1) == OK)
4915 ((void**)gap->ga_data)[gap->ga_len++] = item;
4916 else
4917 {
4918 failed = TRUE;
4919 FREE_ITEM(item);
4920 break;
4921 }
4922 }
4923 else
4924 FREE_ITEM(item);
4925 }
4926
4927 closedir(dirp);
4928 }
4929# endif // MSWIN
4930
4931# undef FREE_ITEM
4932
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004933 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004934 {
4935# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004936 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004937 if (withattr)
4938 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4939 compare_readdirex_item);
4940 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004941 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4942 compare_readdir_item);
4943# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004944 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004945# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004946 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004947
4948 return failed ? FAIL : OK;
4949}
4950
4951/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004952 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004953 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004954 */
4955 int
4956delete_recursive(char_u *name)
4957{
4958 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004959 int i;
4960 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004961 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004962
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004963 // A symbolic link to a directory itself is deleted, not the directory it
4964 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004965 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004966# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004967 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004968# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004969 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004970# endif
4971 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004972 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004973 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004974 if (exp == NULL)
4975 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004976 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004977 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004978 for (i = 0; i < ga.ga_len; ++i)
4979 {
4980 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4981 ((char_u **)ga.ga_data)[i]);
4982 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004983 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004984 }
4985 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004986 }
4987 else
4988 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004989 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004990 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004991 }
4992 else
4993 result = mch_remove(name) == 0 ? 0 : -1;
4994
4995 return result;
4996}
4997#endif
4998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005000static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005002# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5003/*
5004 * Open temporary directory and take file lock to prevent
5005 * to be auto-cleaned.
5006 */
5007 static void
5008vim_opentempdir(void)
5009{
5010 DIR *dp = NULL;
5011
5012 if (vim_tempdir_dp != NULL)
5013 return;
5014
5015 dp = opendir((const char*)vim_tempdir);
5016
5017 if (dp != NULL)
5018 {
5019 vim_tempdir_dp = dp;
5020 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5021 }
5022}
5023
5024/*
5025 * Close temporary directory - it automatically release file lock.
5026 */
5027 static void
5028vim_closetempdir(void)
5029{
5030 if (vim_tempdir_dp != NULL)
5031 {
5032 closedir(vim_tempdir_dp);
5033 vim_tempdir_dp = NULL;
5034 }
5035}
5036# endif
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038/*
5039 * Delete the temp directory and all files it contains.
5040 */
5041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005042vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (vim_tempdir != NULL)
5045 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005046# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5047 vim_closetempdir();
5048# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005049 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005050 gettail(vim_tempdir)[-1] = NUL;
5051 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005052 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005057 * Directory "tempdir" was created. Expand this name to a full path and put
5058 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5059 * "tempdir" must be no longer than MAXPATHL.
5060 */
5061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005063{
5064 char_u *buf;
5065
Bram Moolenaar964b3742019-05-24 18:54:09 +02005066 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005067 if (buf != NULL)
5068 {
5069 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5070 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005071 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005072 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005073# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5074 vim_opentempdir();
5075# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005076 vim_free(buf);
5077 }
5078}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005079#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005080
5081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * vim_tempname(): Return a unique name that can be used for a temp file.
5083 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005084 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5085 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 *
5087 * The returned pointer is to allocated memory.
5088 * The returned pointer is NULL if no valid name was found.
5089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005091vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005092 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005093 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
5095#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005097#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005098 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099#else
5100 char_u itmp[TEMPNAMELEN];
5101#endif
5102
5103#ifdef TEMPDIRNAMES
5104 static char *(tempdirs[]) = {TEMPDIRNAMES};
5105 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005107 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108# endif
5109
5110 /*
5111 * This will create a directory for private use by this instance of Vim.
5112 * This is done once, and the same directory is used for all temp files.
5113 * This method avoids security problems because of symlink attacks et al.
5114 * It's also a bit faster, because we only need to check for an existing
5115 * file when creating the directory and not for each temp file.
5116 */
5117 if (vim_tempdir == NULL)
5118 {
5119 /*
5120 * Try the entries in TEMPDIRNAMES to create the temp directory.
5121 */
K.Takataeeec2542021-06-02 13:28:16 +02005122 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005124# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005125 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005126 long nr;
5127 long off;
5128# endif
5129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005130 // Expand $TMP, leave room for "/v1100000/999999999".
5131 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005133 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005136 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
Bram Moolenaareaf03392009-11-17 11:08:52 +00005138# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005139 {
5140# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005141 // Make sure the umask doesn't remove the executable bit.
5142 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005143 mode_t umask_save = umask(077);
5144# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005145 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005146 STRCAT(itmp, "vXXXXXX");
5147 if (mkdtemp((char *)itmp) != NULL)
5148 vim_settempdir(itmp);
5149# if defined(UNIX) || defined(VMS)
5150 (void)umask(umask_save);
5151# endif
5152 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005153# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005154 // Get an arbitrary number of up to 6 digits. When it's
5155 // unlikely that it already exists it will be faster,
5156 // otherwise it doesn't matter. The use of mkdir() avoids any
5157 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005159 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005161 // Try up to 10000 different values until we find a name that
5162 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 for (off = 0; off < 10000L; ++off)
5164 {
5165 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005166# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
Bram Moolenaareaf03392009-11-17 11:08:52 +00005170 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5171# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 // If mkdir() does not set errno to EEXIST, check for
5173 // existing file here. There is a race condition then,
5174 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (mch_stat((char *)itmp, &st) >= 0)
5176 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005177# endif
5178# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005179 // Make sure the umask doesn't remove the executable bit.
5180 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005184# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (r == 0)
5188 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005189 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005192# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // If the mkdir() didn't fail because the file/dir exists,
5194 // we probably can't create any dir here, try another
5195 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005200# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (vim_tempdir != NULL)
5202 break;
5203 }
5204 }
5205 }
5206
5207 if (vim_tempdir != NULL)
5208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209 // There is no need to check if the file exists, because we own the
5210 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5212 return vim_strsave(itmp);
5213 }
5214
5215 return NULL;
5216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005217#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaar4f974752019-02-17 17:44:42 +01005219# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005220 WCHAR wszTempFile[_MAX_PATH + 1];
5221 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005222 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 char_u *retval;
5224 char_u *p;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005225 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005227 wcscpy(itmp, L"");
5228 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005229 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005230 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005231 wszTempFile[1] = L'\\';
5232 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005233 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005234 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005235
5236 // randomize the name to avoid collisions
5237 i = mch_get_pid() + extra_char;
5238 buf4[1] = chartab[i % 36];
5239 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005240 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005242 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005243 // GetTempFileName() will create the file, we don't want that
5244 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005246 // Backslashes in a temp file name cause problems when filtering with
5247 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5248 // didn't set 'shellslash'.
5249 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (*p_shcf == '-' || p_ssl)
5251 for (p = retval; *p; ++p)
5252 if (*p == '\\')
5253 *p = '/';
5254 return retval;
5255
Bram Moolenaar4f974752019-02-17 17:44:42 +01005256# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005259 char_u *p;
5260
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005261 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005262 p = tmpnam((char *)itmp);
5263 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return NULL;
5265# else
5266 char_u *p;
5267
5268# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005269 // mktemp() is not working on VMS. It seems to be
5270 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 sprintf((char *)itmp, "VIM%c", extra_char);
5272 p = (char_u *)tempnam("tmp:", (char *)itmp);
5273 if (p != NULL)
5274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 // VMS will use '.LIS' if we don't explicitly specify an extension,
5276 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 STRCPY(itmp, p);
5278 STRCAT(itmp, ".txt");
5279 free(p);
5280 }
5281 else
5282 return NULL;
5283# else
5284 STRCPY(itmp, TEMPNAME);
5285 if ((p = vim_strchr(itmp, '?')) != NULL)
5286 *p = extra_char;
5287 if (mktemp((char *)itmp) == NULL)
5288 return NULL;
5289# endif
5290# endif
5291
5292 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005293# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005294#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295}
5296
5297#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5298/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005299 * Convert all backslashes in fname to forward slashes in-place, unless when
5300 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
5302 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005303forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 char_u *p;
5306
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005307 if (path_with_url(fname))
5308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005311 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005313 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 *p = '/';
5315}
5316#endif
5317
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005318/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005319 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5320 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5321 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * Used for autocommands and 'wildignore'.
5323 * Returns TRUE if there is a match, FALSE otherwise.
5324 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005325 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005327 char_u *pattern, // pattern to match with
5328 regprog_T **prog, // pre-compiled regprog or NULL
5329 char_u *fname, // full path of file name
5330 char_u *sfname, // short file name or NULL
5331 char_u *tail, // tail of path
5332 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 regmatch_T regmatch;
5335 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005338 if (prog != NULL)
5339 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005341 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 /*
5344 * Try for a match with the pattern with:
5345 * 1. the full file name, when the pattern has a '/'.
5346 * 2. the short file name, when the pattern has a '/'.
5347 * 3. the tail of the file name, when the pattern has no '/'.
5348 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005349 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 && ((allow_dirs
5351 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5352 || (sfname != NULL
5353 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005354 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 result = TRUE;
5356
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005357 if (prog != NULL)
5358 *prog = regmatch.regprog;
5359 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005360 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 return result;
5362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364#if defined(FEAT_WILDIGN) || defined(PROTO)
5365/*
5366 * Return TRUE if a file matches with a pattern in "list".
5367 * "list" is a comma-separated list of patterns, like 'wildignore'.
5368 * "sfname" is the short file name or NULL, "ffname" the long file name.
5369 */
5370 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 char_u buf[100];
5374 char_u *tail;
5375 char_u *regpat;
5376 char allow_dirs;
5377 int match;
5378 char_u *p;
5379
5380 tail = gettail(sfname);
5381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 p = list;
5384 while (*p)
5385 {
5386 copy_option_part(&p, buf, 100, ",");
5387 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5388 if (regpat == NULL)
5389 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005390 match = match_file_pat(regpat, NULL, ffname, sfname,
5391 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 vim_free(regpat);
5393 if (match)
5394 return TRUE;
5395 }
5396 return FALSE;
5397}
5398#endif
5399
5400/*
5401 * Convert the given pattern "pat" which has shell style wildcards in it, into
5402 * a regular expression, and return the result in allocated memory. If there
5403 * is a directory path separator to be matched, then TRUE is put in
5404 * allow_dirs, otherwise FALSE is put there -- webb.
5405 * Handle backslashes before special characters, like "\*" and "\ ".
5406 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 * Returns NULL when out of memory.
5408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410file_pat_to_reg_pat(
5411 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005412 char_u *pat_end, // first char after pattern or NULL
5413 char *allow_dirs, // Result passed back out in here
5414 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005416 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 char_u *endp;
5418 char_u *reg_pat;
5419 char_u *p;
5420 int i;
5421 int nested = 0;
5422 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
5424 if (allow_dirs != NULL)
5425 *allow_dirs = FALSE;
5426 if (pat_end == NULL)
5427 pat_end = pat + STRLEN(pat);
5428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 for (p = pat; p < pat_end; p++)
5430 {
5431 switch (*p)
5432 {
5433 case '*':
5434 case '.':
5435 case ',':
5436 case '{':
5437 case '}':
5438 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005439 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 break;
5441#ifdef BACKSLASH_IN_FILENAME
5442 case '\\':
5443 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005444 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 break;
5446#endif
5447 default:
5448 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005449 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 ++p;
5452 ++size;
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 break;
5455 }
5456 }
5457 reg_pat = alloc(size + 1);
5458 if (reg_pat == NULL)
5459 return NULL;
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if (pat[0] == '*')
5464 while (pat[0] == '*' && pat < pat_end - 1)
5465 pat++;
5466 else
5467 reg_pat[i++] = '^';
5468 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005469 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
5471 while (endp - pat > 0 && *endp == '*')
5472 endp--;
5473 add_dollar = FALSE;
5474 }
5475 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5476 {
5477 switch (*p)
5478 {
5479 case '*':
5480 reg_pat[i++] = '.';
5481 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005482 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005483 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 break;
5485 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 case '~':
5487 reg_pat[i++] = '\\';
5488 reg_pat[i++] = *p;
5489 break;
5490 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 reg_pat[i++] = '.';
5492 break;
5493 case '\\':
5494 if (p[1] == NUL)
5495 break;
5496#ifdef BACKSLASH_IN_FILENAME
5497 if (!no_bslash)
5498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005499 // translate:
5500 // "\x" to "\\x" e.g., "dir\file"
5501 // "\*" to "\\.*" e.g., "dir\*.c"
5502 // "\?" to "\\." e.g., "dir\??.c"
5503 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5505 && p[1] != '+')
5506 {
5507 reg_pat[i++] = '[';
5508 reg_pat[i++] = '\\';
5509 reg_pat[i++] = '/';
5510 reg_pat[i++] = ']';
5511 if (allow_dirs != NULL)
5512 *allow_dirs = TRUE;
5513 break;
5514 }
5515 }
5516#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 // Undo escaping from ExpandEscape():
5518 // foo\?bar -> foo?bar
5519 // foo\%bar -> foo%bar
5520 // foo\,bar -> foo,bar
5521 // foo\ bar -> foo bar
5522 // Don't unescape \, * and others that are also special in a
5523 // regexp.
5524 // An escaped { must be unescaped since we use magic not
5525 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (*++p == '?'
5527#ifdef BACKSLASH_IN_FILENAME
5528 && no_bslash
5529#endif
5530 )
5531 reg_pat[i++] = '?';
5532 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005533 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005534 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005535 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005536 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5537 {
5538 reg_pat[i++] = '\\';
5539 reg_pat[i++] = '{';
5540 p += 2;
5541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 else
5543 {
5544 if (allow_dirs != NULL && vim_ispathsep(*p)
5545#ifdef BACKSLASH_IN_FILENAME
5546 && (!no_bslash || *p != '\\')
5547#endif
5548 )
5549 *allow_dirs = TRUE;
5550 reg_pat[i++] = '\\';
5551 reg_pat[i++] = *p;
5552 }
5553 break;
5554#ifdef BACKSLASH_IN_FILENAME
5555 case '/':
5556 reg_pat[i++] = '[';
5557 reg_pat[i++] = '\\';
5558 reg_pat[i++] = '/';
5559 reg_pat[i++] = ']';
5560 if (allow_dirs != NULL)
5561 *allow_dirs = TRUE;
5562 break;
5563#endif
5564 case '{':
5565 reg_pat[i++] = '\\';
5566 reg_pat[i++] = '(';
5567 nested++;
5568 break;
5569 case '}':
5570 reg_pat[i++] = '\\';
5571 reg_pat[i++] = ')';
5572 --nested;
5573 break;
5574 case ',':
5575 if (nested)
5576 {
5577 reg_pat[i++] = '\\';
5578 reg_pat[i++] = '|';
5579 }
5580 else
5581 reg_pat[i++] = ',';
5582 break;
5583 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005584 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005586 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 *allow_dirs = TRUE;
5588 reg_pat[i++] = *p;
5589 break;
5590 }
5591 }
5592 if (add_dollar)
5593 reg_pat[i++] = '$';
5594 reg_pat[i] = NUL;
5595 if (nested != 0)
5596 {
5597 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005598 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005600 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005601 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 return reg_pat;
5604}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005605
5606#if defined(EINTR) || defined(PROTO)
5607/*
5608 * Version of read() that retries when interrupted by EINTR (possibly
5609 * by a SIGWINCH).
5610 */
5611 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005613{
5614 long ret;
5615
5616 for (;;)
5617 {
5618 ret = vim_read(fd, buf, bufsize);
5619 if (ret >= 0 || errno != EINTR)
5620 break;
5621 }
5622 return ret;
5623}
5624
5625/*
5626 * Version of write() that retries when interrupted by EINTR (possibly
5627 * by a SIGWINCH).
5628 */
5629 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005631{
5632 long ret = 0;
5633 long wlen;
5634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005635 // Repeat the write() so long it didn't fail, other than being interrupted
5636 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005637 while (ret < (long)bufsize)
5638 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005639 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005640 if (wlen < 0)
5641 {
5642 if (errno != EINTR)
5643 break;
5644 }
5645 else
5646 ret += wlen;
5647 }
5648 return ret;
5649}
5650#endif