blob: 7c8f00de3aff697ef9d81877fa7ccab50c60f574 [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)
1301 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1302 &filesize, newfile, sfname,
1303 &did_ask_for_key);
1304 /*
1305 * Decrypt the read bytes. This is done before checking for
1306 * EOF because the crypt layer may be buffering.
1307 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001308 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1309 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001310 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001311# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001312 if (crypt_works_inplace(curbuf->b_cryptstate))
1313 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001314# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001315 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1316 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001317# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 }
1319 else
1320 {
1321 char_u *newptr = NULL;
1322 int decrypted_size;
1323
1324 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001325 curbuf->b_cryptstate, ptr, size,
1326 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001327
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001328 if (decrypted_size < 0)
1329 {
1330 // error message already given
1331 error = TRUE;
1332 vim_free(newptr);
1333 break;
1334 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001335 // If the crypt layer is buffering, not producing
1336 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001337 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001338 continue;
1339
1340 if (linerest == 0)
1341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001342 // Simple case: reuse returned buffer (may be
1343 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001344 new_buffer = newptr;
1345 }
1346 else
1347 {
1348 long_u new_size;
1349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001350 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001351 new_size = (long_u)(decrypted_size + linerest + 1);
1352 new_buffer = lalloc(new_size, FALSE);
1353 if (new_buffer == NULL)
1354 {
1355 do_outofmem_msg(new_size);
1356 error = TRUE;
1357 break;
1358 }
1359
1360 mch_memmove(new_buffer, buffer, linerest);
1361 if (newptr != NULL)
1362 mch_memmove(new_buffer + linerest, newptr,
1363 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001364 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001365 }
1366
1367 if (new_buffer != NULL)
1368 {
1369 vim_free(buffer);
1370 buffer = new_buffer;
1371 new_buffer = NULL;
1372 line_start = buffer;
1373 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001374 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001375 }
1376 size = decrypted_size;
1377 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001378# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001379 }
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (size <= 0)
1383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001384 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001387 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001388 /*
1389 * Reached end-of-file but some trailing bytes could
1390 * not be converted. Truncated file?
1391 */
1392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001393 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001394 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001395#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001396 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001397#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001398 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001399 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001400 if (can_retry)
1401 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001402 if (conv_error == 0)
1403 conv_error = curbuf->b_ml.ml_line_count
1404 - linecnt + 1;
1405 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001407 else if (illegal_byte == 0)
1408 illegal_byte = curbuf->b_ml.ml_line_count
1409 - linecnt + 1;
1410 if (bad_char_behavior == BAD_DROP)
1411 {
1412 *(ptr - conv_restlen) = NUL;
1413 conv_restlen = 0;
1414 }
1415 else
1416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001417 // Replace the trailing bytes with the replacement
1418 // character if we were converting; if we weren't,
1419 // leave the UTF8 checking code to do it, as it
1420 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001421 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001422#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001423 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001424#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001425 ))
1426 {
1427 while (conv_restlen > 0)
1428 {
1429 *(--ptr) = bad_char_behavior;
1430 --conv_restlen;
1431 }
1432 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001434#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001435 if (iconv_fd != (iconv_t)-1)
1436 {
1437 iconv_close(iconv_fd);
1438 iconv_fd = (iconv_t)-1;
1439 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001440#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001441 }
1442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 skip_read = FALSE;
1446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /*
1448 * At start of file (or after crypt magic number): Check for BOM.
1449 * Also check for a BOM for other Unicode encodings, but not after
1450 * converting with 'charconvert' or when a BOM has already been
1451 * found.
1452 */
1453 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001454#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001455 || (cryptkey != NULL
1456 && filesize == crypt_get_header_len(
1457 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 )
1460 && (fio_flags == FIO_UCSBOM
1461 || (!curbuf->b_p_bomb
1462 && tmpname == NULL
1463 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1464 {
1465 char_u *ccname;
1466 int blen;
1467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (size < 2 || curbuf->b_p_bin)
1470 ccname = NULL;
1471 else
1472 ccname = check_for_bom(ptr, size, &blen,
1473 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1474 if (ccname != NULL)
1475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001476 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 filesize += blen;
1478 size -= blen;
1479 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001480 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001483 curbuf->b_start_bomb = TRUE;
1484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486
1487 if (fio_flags == FIO_UCSBOM)
1488 {
1489 if (ccname == NULL)
1490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001491 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 advance_fenc = TRUE;
1493 }
1494 else
1495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001496 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (fenc_alloced)
1498 vim_free(fenc);
1499 fenc = ccname;
1500 fenc_alloced = FALSE;
1501 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001502 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 skip_read = TRUE;
1504 goto retry;
1505 }
1506 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001507
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001508 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001509 ptr -= conv_restlen;
1510 size += conv_restlen;
1511 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 /*
1513 * Break here for a read error or end-of-file.
1514 */
1515 if (size <= 0)
1516 break;
1517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar13505972019-01-24 15:04:48 +01001519#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (iconv_fd != (iconv_t)-1)
1521 {
1522 /*
1523 * Attempt conversion of the read bytes to 'encoding' using
1524 * iconv().
1525 */
1526 const char *fromp;
1527 char *top;
1528 size_t from_size;
1529 size_t to_size;
1530
1531 fromp = (char *)ptr;
1532 from_size = size;
1533 ptr += size;
1534 top = (char *)ptr;
1535 to_size = real_size - size;
1536
1537 /*
1538 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001539 * another conversion. Except for when there is no
1540 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001542 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1543 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1545 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001546 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001547 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001548 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001549 if (conv_error == 0)
1550 conv_error = readfile_linenr(linecnt,
1551 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001554 ++fromp;
1555 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001556 if (bad_char_behavior == BAD_KEEP)
1557 {
1558 *top++ = *(fromp - 1);
1559 --to_size;
1560 }
1561 else if (bad_char_behavior != BAD_DROP)
1562 {
1563 *top++ = bad_char_behavior;
1564 --to_size;
1565 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 if (from_size > 0)
1569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001570 // Some remaining characters, keep them for the next
1571 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1573 conv_restlen = (int)from_size;
1574 }
1575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001576 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 line_start = ptr - linerest;
1578 mch_memmove(line_start, buffer, (size_t)linerest);
1579 size = (long)((char_u *)top - ptr);
1580 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar4f974752019-02-17 17:44:42 +01001583#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (fio_flags & FIO_CODEPAGE)
1585 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001586 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001587 WCHAR ucs2buf[3];
1588 int ucs2len;
1589 int codepage = FIO_GET_CP(fio_flags);
1590 int bytelen;
1591 int found_bad;
1592 char replstr[2];
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 /*
1595 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001596 * a codepage, using standard MS-Windows functions. This
1597 * requires two steps:
1598 * 1. convert from 'fileencoding' to ucs-2
1599 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001601 * Because there may be illegal bytes AND an incomplete byte
1602 * sequence at the end, we may have to do the conversion one
1603 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001607 if (bad_char_behavior > 0)
1608 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001610 replstr[0] = '?';
1611 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001614 * Move the bytes to the end of the buffer, so that we have
1615 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001617 src = ptr + real_size - size;
1618 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001620 /*
1621 * Do the conversion.
1622 */
1623 dst = ptr;
1624 size = size;
1625 while (size > 0)
1626 {
1627 found_bad = FALSE;
1628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001629# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001630 if (codepage == CP_UTF8)
1631 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 // Handle CP_UTF8 input ourselves to be able to handle
1633 // trailing bytes properly.
1634 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001635 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001636 if (bytelen > size)
1637 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // Only got some bytes of a character. Normally
1639 // it's put in "conv_rest", but if it's too long
1640 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001641 if (bytelen <= CONV_RESTLEN)
1642 break;
1643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001644 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001645 bytelen = size;
1646 found_bad = TRUE;
1647 }
1648 else
1649 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001650 int u8c = utf_ptr2char(src);
1651
Bram Moolenaar86e01082005-12-29 22:45:34 +00001652 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001653 found_bad = TRUE;
1654 ucs2buf[0] = u8c;
1655 ucs2len = 1;
1656 }
1657 }
1658 else
1659# endif
1660 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // We don't know how long the byte sequence is, try
1662 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001663 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1664 ++bytelen)
1665 {
1666 ucs2len = MultiByteToWideChar(codepage,
1667 MB_ERR_INVALID_CHARS,
1668 (LPCSTR)src, bytelen,
1669 ucs2buf, 3);
1670 if (ucs2len > 0)
1671 break;
1672 }
1673 if (ucs2len == 0)
1674 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001675 // If we have only one byte then it's probably an
1676 // incomplete byte sequence. Otherwise discard
1677 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001678 if (size == 1)
1679 break;
1680 found_bad = TRUE;
1681 bytelen = 1;
1682 }
1683 }
1684
1685 if (!found_bad)
1686 {
1687 int i;
1688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001689 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001690 if (enc_utf8)
1691 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001692 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001693 for (i = 0; i < ucs2len; ++i)
1694 dst += utf_char2bytes(ucs2buf[i], dst);
1695 }
1696 else
1697 {
1698 BOOL bad = FALSE;
1699 int dstlen;
1700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001701 // From UCS-2 to "enc_codepage". If the
1702 // conversion uses the default character "?",
1703 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001704 dstlen = WideCharToMultiByte(enc_codepage, 0,
1705 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001706 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001707 replstr, &bad);
1708 if (bad)
1709 found_bad = TRUE;
1710 else
1711 dst += dstlen;
1712 }
1713 }
1714
1715 if (found_bad)
1716 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001717 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001718 if (can_retry)
1719 goto rewind_retry;
1720 if (conv_error == 0)
1721 conv_error = readfile_linenr(linecnt, ptr, dst);
1722 if (bad_char_behavior != BAD_DROP)
1723 {
1724 if (bad_char_behavior == BAD_KEEP)
1725 {
1726 mch_memmove(dst, src, bytelen);
1727 dst += bytelen;
1728 }
1729 else
1730 *dst++ = bad_char_behavior;
1731 }
1732 }
1733
1734 src += bytelen;
1735 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001737
1738 if (size > 0)
1739 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001740 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001741 mch_memmove(conv_rest, src, size);
1742 conv_restlen = size;
1743 }
1744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001745 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001746 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001749#endif
1750#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 if (fio_flags & FIO_MACROMAN)
1752 {
1753 /*
1754 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001755 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001757 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (fio_flags != 0)
1763 {
1764 int u8c;
1765 char_u *dest;
1766 char_u *tail = NULL;
1767
1768 /*
1769 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1770 * "enc_utf8" not set: Convert Unicode to Latin1.
1771 * Go from end to start through the buffer, because the number
1772 * of bytes may increase.
1773 * "dest" points to after where the UTF-8 bytes go, "p" points
1774 * to after the next character to convert.
1775 */
1776 dest = ptr + real_size;
1777 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1778 {
1779 p = ptr + size;
1780 if (fio_flags == FIO_UTF8)
1781 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001782 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 tail = ptr + size - 1;
1784 while (tail > ptr && (*tail & 0xc0) == 0x80)
1785 --tail;
1786 if (tail + utf_byte2len(*tail) <= ptr + size)
1787 tail = NULL;
1788 else
1789 p = tail;
1790 }
1791 }
1792 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001794 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 p = ptr + (size & ~1);
1796 if (size & 1)
1797 tail = p;
1798 if ((fio_flags & FIO_UTF16) && p > ptr)
1799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001800 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (fio_flags & FIO_ENDIAN_L)
1802 {
1803 u8c = (*--p << 8);
1804 u8c += *--p;
1805 }
1806 else
1807 {
1808 u8c = *--p;
1809 u8c += (*--p << 8);
1810 }
1811 if (u8c >= 0xd800 && u8c <= 0xdbff)
1812 tail = p;
1813 else
1814 p += 2;
1815 }
1816 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001817 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001819 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 p = ptr + (size & ~3);
1821 if (size & 3)
1822 tail = p;
1823 }
1824
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001825 // If there is a trailing incomplete sequence move it to
1826 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (tail != NULL)
1828 {
1829 conv_restlen = (int)((ptr + size) - tail);
1830 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1831 size -= conv_restlen;
1832 }
1833
1834
1835 while (p > ptr)
1836 {
1837 if (fio_flags & FIO_LATIN1)
1838 u8c = *--p;
1839 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1840 {
1841 if (fio_flags & FIO_ENDIAN_L)
1842 {
1843 u8c = (*--p << 8);
1844 u8c += *--p;
1845 }
1846 else
1847 {
1848 u8c = *--p;
1849 u8c += (*--p << 8);
1850 }
1851 if ((fio_flags & FIO_UTF16)
1852 && u8c >= 0xdc00 && u8c <= 0xdfff)
1853 {
1854 int u16c;
1855
1856 if (p == ptr)
1857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001858 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (can_retry)
1860 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001861 if (conv_error == 0)
1862 conv_error = readfile_linenr(linecnt,
1863 ptr, p);
1864 if (bad_char_behavior == BAD_DROP)
1865 continue;
1866 if (bad_char_behavior != BAD_KEEP)
1867 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001870 // found second word of double-word, get the first
1871 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (fio_flags & FIO_ENDIAN_L)
1873 {
1874 u16c = (*--p << 8);
1875 u16c += *--p;
1876 }
1877 else
1878 {
1879 u16c = *--p;
1880 u16c += (*--p << 8);
1881 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001882 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1883 + (u8c & 0x3ff);
1884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (u16c < 0xd800 || u16c > 0xdbff)
1887 {
1888 if (can_retry)
1889 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001890 if (conv_error == 0)
1891 conv_error = readfile_linenr(linecnt,
1892 ptr, p);
1893 if (bad_char_behavior == BAD_DROP)
1894 continue;
1895 if (bad_char_behavior != BAD_KEEP)
1896 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899 }
1900 else if (fio_flags & FIO_UCS4)
1901 {
1902 if (fio_flags & FIO_ENDIAN_L)
1903 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001904 u8c = (unsigned)*--p << 24;
1905 u8c += (unsigned)*--p << 16;
1906 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 u8c += *--p;
1908 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001909 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001912 u8c += (unsigned)*--p << 8;
1913 u8c += (unsigned)*--p << 16;
1914 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001917 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 if (*--p < 0x80)
1920 u8c = *p;
1921 else
1922 {
1923 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001924 p -= len;
1925 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (len == 0)
1927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 // Not a valid UTF-8 character, retry with
1929 // another fenc when possible, otherwise just
1930 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (can_retry)
1932 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001933 if (conv_error == 0)
1934 conv_error = readfile_linenr(linecnt,
1935 ptr, p);
1936 if (bad_char_behavior == BAD_DROP)
1937 continue;
1938 if (bad_char_behavior != BAD_KEEP)
1939 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 dest -= utf_char2len(u8c);
1946 (void)utf_char2bytes(u8c, dest);
1947 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 --dest;
1951 if (u8c >= 0x100)
1952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 // character doesn't fit in latin1, retry with
1954 // another fenc when possible, otherwise just
1955 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001956 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001958 if (conv_error == 0)
1959 conv_error = readfile_linenr(linecnt, ptr, p);
1960 if (bad_char_behavior == BAD_DROP)
1961 ++dest;
1962 else if (bad_char_behavior == BAD_KEEP)
1963 *dest = u8c;
1964 else if (eap != NULL && eap->bad_char != 0)
1965 *dest = bad_char_behavior;
1966 else
1967 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969 else
1970 *dest = u8c;
1971 }
1972 }
1973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001974 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 line_start = dest - linerest;
1976 mch_memmove(line_start, buffer, (size_t)linerest);
1977 size = (long)((ptr + real_size) - dest);
1978 ptr = dest;
1979 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001980 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001982 int incomplete_tail = FALSE;
1983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001984 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001985 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001987 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001988 int l;
1989
1990 if (todo <= 0)
1991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (*p >= 0x80)
1993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // A length of 1 means it's an illegal byte. Accept
1995 // an incomplete character at the end though, the next
1996 // read() will get the next bytes, we'll check it
1997 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001998 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00001999 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // Avoid retrying with a different encoding when
2002 // a truncated file is more likely, or attempting
2003 // to read the rest of an incomplete sequence when
2004 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002005 if (p > ptr || filesize > 0)
2006 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002007 // Incomplete byte sequence, move it to conv_rest[]
2008 // and try to read the rest of it, unless we've
2009 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002010 if (p > ptr)
2011 {
2012 conv_restlen = todo;
2013 mch_memmove(conv_rest, p, conv_restlen);
2014 size -= conv_restlen;
2015 break;
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002018 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // Illegal byte. If we can try another encoding
2021 // do that, unless at EOF where a truncated
2022 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002023 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002024 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002025#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002027 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2028 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002029#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002031 if (conv_error == 0 && illegal_byte == 0)
2032 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002035 if (bad_char_behavior == BAD_DROP)
2036 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002037 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002038 --p;
2039 --size;
2040 }
2041 else if (bad_char_behavior != BAD_KEEP)
2042 *p = bad_char_behavior;
2043 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002044 else
2045 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002048 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002050 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002053#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002054 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002055 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002056 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002058#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002060 advance_fenc = TRUE;
2061 file_rewind = TRUE;
2062 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 filesize += size;
2068
2069 /*
2070 * when reading the first part of a file: guess EOL type
2071 */
2072 if (fileformat == EOL_UNKNOWN)
2073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (try_dos || try_unix)
2076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002077 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002078 if (try_mac)
2079 try_mac = 1;
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 for (p = ptr; p < ptr + size; ++p)
2082 {
2083 if (*p == NL)
2084 {
2085 if (!try_unix
2086 || (try_dos && p > ptr && p[-1] == CAR))
2087 fileformat = EOL_DOS;
2088 else
2089 fileformat = EOL_UNIX;
2090 break;
2091 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002092 else if (*p == CAR && try_mac)
2093 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002096 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 if (fileformat == EOL_UNIX && try_mac)
2098 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 try_mac = 1;
2101 try_unix = 1;
2102 for (; p >= ptr && *p != CAR; p--)
2103 ;
2104 if (p >= ptr)
2105 {
2106 for (p = ptr; p < ptr + size; ++p)
2107 {
2108 if (*p == NL)
2109 try_unix++;
2110 else if (*p == CAR)
2111 try_mac++;
2112 }
2113 if (try_mac > try_unix)
2114 fileformat = EOL_MAC;
2115 }
2116 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002117 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 // Looking for CR but found no end-of-line markers at
2119 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002120 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002123 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (fileformat == EOL_UNKNOWN && try_mac)
2125 fileformat = EOL_MAC;
2126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (fileformat == EOL_UNKNOWN)
2129 fileformat = default_fileformat();
2130
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002132 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 set_fileformat(fileformat, OPT_LOCAL);
2134 }
2135 }
2136
2137 /*
2138 * This loop is executed once for every character read.
2139 * Keep it fast!
2140 */
2141 if (fileformat == EOL_MAC)
2142 {
2143 --ptr;
2144 while (++ptr, --size >= 0)
2145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if ((c = *ptr) != NUL && c != CAR && c != NL)
2148 continue;
2149 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002150 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 else
2154 {
2155 if (skip_count == 0)
2156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002157 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 len = (colnr_T) (ptr - line_start + 1);
2159 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2160 {
2161 error = TRUE;
2162 break;
2163 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002164#ifdef FEAT_PERSISTENT_UNDO
2165 if (read_undo_file)
2166 sha256_update(&sha_ctx, line_start, len);
2167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ++lnum;
2169 if (--read_count == 0)
2170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002171 error = TRUE; // break loop
2172 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 break;
2174 }
2175 }
2176 else
2177 --skip_count;
2178 line_start = ptr + 1;
2179 }
2180 }
2181 }
2182 else
2183 {
2184 --ptr;
2185 while (++ptr, --size >= 0)
2186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002187 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 continue;
2189 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002190 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 else
2192 {
2193 if (skip_count == 0)
2194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002195 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 len = (colnr_T)(ptr - line_start + 1);
2197 if (fileformat == EOL_DOS)
2198 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002199 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 ptr[-1] = NUL;
2203 --len;
2204 }
2205 /*
2206 * Reading in Dos format, but no CR-LF found!
2207 * When 'fileformats' includes "unix", delete all
2208 * the lines read so far and start all over again.
2209 * Otherwise give an error message later.
2210 */
2211 else if (ff_error != EOL_DOS)
2212 {
2213 if ( try_unix
2214 && !read_stdin
2215 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002216 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2217 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 set_fileformat(EOL_UNIX, OPT_LOCAL);
2222 file_rewind = TRUE;
2223 keep_fileformat = TRUE;
2224 goto retry;
2225 }
2226 ff_error = EOL_DOS;
2227 }
2228 }
2229 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2230 {
2231 error = TRUE;
2232 break;
2233 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002234#ifdef FEAT_PERSISTENT_UNDO
2235 if (read_undo_file)
2236 sha256_update(&sha_ctx, line_start, len);
2237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 ++lnum;
2239 if (--read_count == 0)
2240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 error = TRUE; // break loop
2242 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 break;
2244 }
2245 }
2246 else
2247 --skip_count;
2248 line_start = ptr + 1;
2249 }
2250 }
2251 }
2252 linerest = (long)(ptr - line_start);
2253 ui_breakcheck();
2254 }
2255
2256failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (error && read_count == 0)
2259 error = FALSE;
2260
2261 /*
2262 * If we get EOF in the middle of a line, note the fact and
2263 * complete the line ourselves.
2264 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2265 */
2266 if (!error
2267 && !got_int
2268 && linerest != 0
2269 && !(!curbuf->b_p_bin
2270 && fileformat == EOL_DOS
2271 && *line_start == Ctrl_Z
2272 && ptr == line_start + 1))
2273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002275 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 curbuf->b_p_eol = FALSE;
2277 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002278 len = (colnr_T)(ptr - line_start + 1);
2279 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 error = TRUE;
2281 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002282 {
2283#ifdef FEAT_PERSISTENT_UNDO
2284 if (read_undo_file)
2285 sha256_update(&sha_ctx, line_start, len);
2286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002295 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002296 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002297 crypt_free_state(curbuf->b_cryptstate);
2298 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002299 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002300 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2301 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002302 // Don't set cryptkey to NULL, it's used below as a flag that
2303 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif
2305
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002306 // If editing a new file: set 'fenc' for the current buffer.
2307 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002308 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002310 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (fenc_alloced)
2312 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002313#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#endif
2317
2318 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002320#ifdef HAVE_FD_CLOEXEC
2321 else
2322 {
2323 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002324
Bram Moolenaarf05da212009-11-17 16:13:15 +00002325 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002326 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002327 }
2328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 vim_free(buffer);
2330
2331#ifdef HAVE_DUP
2332 if (read_stdin)
2333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002336 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338#endif
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (tmpname != NULL)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 vim_free(tmpname);
2344 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 /*
2348 * In recovery mode everything but autocommands is skipped.
2349 */
2350 if (!recoverymode)
2351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002352 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2354 {
2355#ifdef FEAT_NETBEANS_INTG
2356 netbeansFireChanges = 0;
2357#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002358 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#ifdef FEAT_NETBEANS_INTG
2360 netbeansFireChanges = 1;
2361#endif
2362 --linecnt;
2363 }
2364 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2365 if (filesize == 0)
2366 linecnt = 0;
2367 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002370#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002371 // After reading the text into the buffer the diff info needs to
2372 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002373 diff_invalidate(curbuf);
2374#endif
2375#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002376 // All folds in the window are invalid now. Mark them for update
2377 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002378 foldUpdateAll(curwin);
2379#endif
2380 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 appended_lines_mark(from, linecnt);
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifndef ALWAYS_USE_GUI
2385 /*
2386 * If we were reading from the same terminal as where messages go,
2387 * the screen will have been messed up.
2388 * Switch on raw mode now and clear the screen.
2389 */
2390 if (read_stdin)
2391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002392 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 starttermcap();
2394 screenclear();
2395 }
2396#endif
2397
2398 if (got_int)
2399 {
2400 if (!(flags & READ_DUMMY))
2401 {
2402 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2403 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002404 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406 msg_scroll = msg_save;
2407#ifdef FEAT_VIMINFO
2408 check_marks_read();
2409#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002410 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412
2413 if (!filtering && !(flags & READ_DUMMY))
2414 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002415 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 c = FALSE;
2417
2418#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002419 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
2421 STRCAT(IObuff, _("[fifo]"));
2422 c = TRUE;
2423 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002424 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
2426 STRCAT(IObuff, _("[socket]"));
2427 c = TRUE;
2428 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002429# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002431 {
2432 STRCAT(IObuff, _("[character special]"));
2433 c = TRUE;
2434 }
2435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
2437 if (curbuf->b_p_ro)
2438 {
2439 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2440 c = TRUE;
2441 }
2442 if (read_no_eol_lnum)
2443 {
2444 msg_add_eol();
2445 c = TRUE;
2446 }
2447 if (ff_error == EOL_DOS)
2448 {
2449 STRCAT(IObuff, _("[CR missing]"));
2450 c = TRUE;
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (split)
2453 {
2454 STRCAT(IObuff, _("[long lines split]"));
2455 c = TRUE;
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (notconverted)
2458 {
2459 STRCAT(IObuff, _("[NOT converted]"));
2460 c = TRUE;
2461 }
2462 else if (converted)
2463 {
2464 STRCAT(IObuff, _("[converted]"));
2465 c = TRUE;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifdef FEAT_CRYPT
2468 if (cryptkey != NULL)
2469 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002470 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 c = TRUE;
2472 }
2473#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002474 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002476 sprintf((char *)IObuff + STRLEN(IObuff),
2477 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 c = TRUE;
2479 }
2480 else if (illegal_byte > 0)
2481 {
2482 sprintf((char *)IObuff + STRLEN(IObuff),
2483 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2484 c = TRUE;
2485 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002486 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 STRCAT(IObuff, _("[READ ERRORS]"));
2489 c = TRUE;
2490 }
2491 if (msg_add_fileformat(fileformat))
2492 c = TRUE;
2493#ifdef FEAT_CRYPT
2494 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002495 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002496 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 else
2498#endif
2499 msg_add_lines(c, (long)linecnt, filesize);
2500
Bram Moolenaard23a8232018-02-10 18:45:26 +01002501 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 msg_scrolled_ign = TRUE;
2503#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002504 // Don't show the message when reading stdin, it would end up in a
2505 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (read_stdin || read_buffer)
2507 p = msg_may_trunc(FALSE, IObuff);
2508 else
2509#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002510 {
2511 if (msg_col > 0)
2512 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002513 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002516 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002517 // Need to repeat the message after redrawing when:
2518 // - When reading from stdin (the screen will be cleared next).
2519 // - When restart_edit is set (otherwise there will be a delay
2520 // before redrawing).
2521 // - When the screen was scrolled but there is no wait-return
2522 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002523 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 msg_scrolled_ign = FALSE;
2525 }
2526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002527 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002529 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002530 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 curbuf->b_p_ro = TRUE;
2532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002533 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 /*
2536 * In Ex mode: cursor at last new line.
2537 * Otherwise: cursor at first new line.
2538 */
2539 if (exmode_active)
2540 curwin->w_cursor.lnum = from + linecnt;
2541 else
2542 curwin->w_cursor.lnum = from + 1;
2543 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002544 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
Bram Moolenaare1004402020-10-24 20:49:43 +02002546 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002547 {
2548 // Set '[ and '] marks to the newly read lines.
2549 curbuf->b_op_start.lnum = from + 1;
2550 curbuf->b_op_start.col = 0;
2551 curbuf->b_op_end.lnum = from + linecnt;
2552 curbuf->b_op_end.col = 0;
2553 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002554
Bram Moolenaar4f974752019-02-17 17:44:42 +01002555#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002556 /*
2557 * Work around a weird problem: When a file has two links (only
2558 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002559 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002560 * It's correct again after reading the file, thus reset the timestamp
2561 * here.
2562 */
2563 if (newfile && !read_stdin && !read_buffer
2564 && mch_stat((char *)fname, &st) >= 0)
2565 {
2566 buf_store_time(curbuf, &st, fname);
2567 curbuf->b_mtime_read = curbuf->b_mtime;
2568 }
2569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 msg_scroll = msg_save;
2572
2573#ifdef FEAT_VIMINFO
2574 /*
2575 * Get the marks before executing autocommands, so they can be used there.
2576 */
2577 check_marks_read();
2578#endif
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002581 * We remember if the last line of the read didn't have
2582 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2583 * or writing the read again with 'binary' on. The latter is required
2584 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002586 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 // When reloading a buffer put the cursor at the first line that is
2589 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002590 if (flags & READ_KEEP_UNDO)
2591 u_find_first_changed();
2592
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002593#ifdef FEAT_PERSISTENT_UNDO
2594 /*
2595 * When opening a new file locate undo info and read it.
2596 */
2597 if (read_undo_file)
2598 {
2599 char_u hash[UNDO_HASH_SIZE];
2600
2601 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002602 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002603 }
2604#endif
2605
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002606 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
2608 int m = msg_scroll;
2609 int n = msg_scrolled;
2610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002611 // Save the fileformat now, otherwise the buffer will be considered
2612 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002613 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 save_file_ff(curbuf);
2615
2616 /*
2617 * The output from the autocommands should not overwrite anything and
2618 * should not be overwritten: Set msg_scroll, restore its value if no
2619 * output was done.
2620 */
2621 msg_scroll = TRUE;
2622 if (filtering)
2623 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2624 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002625 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2628 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002629 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2630 /*
2631 * EVENT_FILETYPE was not triggered but the buffer already has a
2632 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2633 */
2634 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2635 TRUE, curbuf);
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 else
2638 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2639 FALSE, NULL, eap);
2640 if (msg_scrolled == n)
2641 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002642# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002643 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 if (recoverymode && error)
2649 return FAIL;
2650 return OK;
2651}
2652
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002653#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002654/*
2655 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2656 * which is the name of files used for process substitution output by
2657 * some shells on some operating systems, e.g., bash on SunOS.
2658 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2659 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002660 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002661is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002662{
2663 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2664 && VIM_ISDIGIT(fname[8])
2665 && *skipdigits(fname + 9) == NUL
2666 && (fname[9] != NUL
2667 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2668}
2669#endif
2670
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002671/*
2672 * From the current line count and characters read after that, estimate the
2673 * line number where we are now.
2674 * Used for error messages that include a line number.
2675 */
2676 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002677readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002678 linenr_T linecnt, // line count before reading more bytes
2679 char_u *p, // start of more bytes read
2680 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002681{
2682 char_u *s;
2683 linenr_T lnum;
2684
2685 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2686 for (s = p; s < endp; ++s)
2687 if (*s == '\n')
2688 ++lnum;
2689 return lnum;
2690}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002693 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2694 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 * Returns OK or FAIL.
2696 */
2697 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002698prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
Bram Moolenaar13505972019-01-24 15:04:48 +01002700 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (eap->cmd == NULL)
2702 return FAIL;
2703
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002704 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2705 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002706 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002707 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002708
2709 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002710 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002711 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 return OK;
2713}
2714
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002715/*
2716 * Set default or forced 'fileformat' and 'binary'.
2717 */
2718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002719set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002720{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002721 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002722 if (set_options)
2723 {
2724 if (eap != NULL && eap->force_ff != 0)
2725 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2726 else if (*p_ffs != NUL)
2727 set_fileformat(default_fileformat(), OPT_LOCAL);
2728 }
2729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002730 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002731 if (eap != NULL && eap->force_bin != 0)
2732 {
2733 int oldval = curbuf->b_p_bin;
2734
2735 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2736 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2737 }
2738}
2739
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002740/*
2741 * Set forced 'fileencoding'.
2742 */
2743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002744set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002745{
2746 if (eap->force_enc != 0)
2747 {
2748 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2749
2750 if (fenc != NULL)
2751 set_string_option_direct((char_u *)"fenc", -1,
2752 fenc, OPT_FREE|OPT_LOCAL, 0);
2753 vim_free(fenc);
2754 }
2755}
2756
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757/*
2758 * Find next fileencoding to use from 'fileencodings'.
2759 * "pp" points to fenc_next. It's advanced to the next item.
2760 * When there are no more items, an empty string is returned and *pp is set to
2761 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002762 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2763 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 */
2765 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002766next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
2768 char_u *p;
2769 char_u *r;
2770
Bram Moolenaarf077db22019-08-13 00:18:24 +02002771 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (**pp == NUL)
2773 {
2774 *pp = NULL;
2775 return (char_u *)"";
2776 }
2777 p = vim_strchr(*pp, ',');
2778 if (p == NULL)
2779 {
2780 r = enc_canonize(*pp);
2781 *pp += STRLEN(*pp);
2782 }
2783 else
2784 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002785 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 *pp = p + 1;
2787 if (r != NULL)
2788 {
2789 p = enc_canonize(r);
2790 vim_free(r);
2791 r = p;
2792 }
2793 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002794 if (r != NULL)
2795 *alloced = TRUE;
2796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002798 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 r = (char_u *)"";
2800 *pp = NULL;
2801 }
2802 return r;
2803}
2804
Bram Moolenaar13505972019-01-24 15:04:48 +01002805#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806/*
2807 * Convert a file with the 'charconvert' expression.
2808 * This closes the file which is to be read, converts it and opens the
2809 * resulting file for reading.
2810 * Returns name of the resulting converted file (the caller should delete it
2811 * after reading it).
2812 * Returns NULL if the conversion failed ("*fdp" is not set) .
2813 */
2814 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002815readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002816 char_u *fname, // name of input file
2817 char_u *fenc, // converted from
2818 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002821 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002823 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002825 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 else
2827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002828 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 *fdp = -1;
2830 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2831 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002832 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2834 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002835 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837
2838 if (errmsg != NULL)
2839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002840 // Don't use emsg(), it breaks mappings, the retry with
2841 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002842 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (tmpname != NULL)
2844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002845 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002846 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 }
2849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002850 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (*fdp < 0)
2852 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2853
2854 return tmpname;
2855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856#endif
2857
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002858#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002860 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2862 * *filesizep are updated.
2863 * Return the (new) encryption key, NULL for no encryption.
2864 */
2865 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002866check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002867 char_u *cryptkey, // previous encryption key or NULL
2868 char_u *ptr, // pointer to read bytes
2869 long *sizep, // length of read bytes
2870 off_T *filesizep, // nr of bytes used from file
2871 int newfile, // editing a new buffer
2872 char_u *fname, // file name to display
2873 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002875 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002876 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002877
2878 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002880 // Mark the buffer as read-only until the decryption has taken place.
2881 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002882 curbuf->b_p_ro = TRUE;
2883
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002884 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002885 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002886 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
2888 if (*curbuf->b_p_key)
2889 cryptkey = curbuf->b_p_key;
2890 else
2891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002892 // When newfile is TRUE, store the typed key in the 'key'
2893 // option and don't free it. bf needs hash of the key saved.
2894 // Don't ask for the key again when first time Enter was hit.
2895 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002897 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002898 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002899 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002900 *did_ask = TRUE;
2901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002902 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (cryptkey != NULL && *cryptkey == NUL)
2904 {
2905 if (cryptkey != curbuf->b_p_key)
2906 vim_free(cryptkey);
2907 cryptkey = NULL;
2908 }
2909 }
2910 }
2911
2912 if (cryptkey != NULL)
2913 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002914 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002915
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002916 curbuf->b_cryptstate = crypt_create_from_header(
2917 method, cryptkey, ptr);
2918 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002920 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002921 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002922 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002923 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002924 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002925 *filesizep += header_len;
2926 *sizep -= header_len;
2927 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002929 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002930 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002933 // When starting to edit a new file which does not have encryption, clear
2934 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002935 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2937
2938 return cryptkey;
2939}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002940#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002943 * Return TRUE if a file appears to be read-only from the file permissions.
2944 */
2945 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002946check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002947 char_u *fname, // full path to file
2948 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002949{
2950#ifndef USE_MCH_ACCESS
2951 int fd = 0;
2952#endif
2953
2954 return (
2955#ifdef USE_MCH_ACCESS
2956# ifdef UNIX
2957 (perm & 0222) == 0 ||
2958# endif
2959 mch_access((char *)fname, W_OK)
2960#else
2961 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2962 ? TRUE : (close(fd), FALSE)
2963#endif
2964 );
2965}
2966
Bram Moolenaara7870192019-02-14 12:56:36 +01002967#if defined(HAVE_FSYNC) || defined(PROTO)
2968/*
2969 * Call fsync() with Mac-specific exception.
2970 * Return fsync() result: zero for success.
2971 */
2972 int
2973vim_fsync(int fd)
2974{
2975 int r;
2976
2977# ifdef MACOS_X
2978 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002979 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002980# endif
2981 r = fsync(fd);
2982 return r;
2983}
2984#endif
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002987 * Set the name of the current buffer. Use when the buffer doesn't have a
2988 * name and a ":r" or ":w" command with a file name is used.
2989 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002990 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002991set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002992{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002993 buf_T *buf = curbuf;
2994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002995 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002996 if (curbuf->b_p_bl)
2997 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2998 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002999#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003000 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003001 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003002#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003003 if (curbuf != buf)
3004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003005 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003006 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003007 return FAIL;
3008 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003009
3010 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3011 curbuf->b_flags |= BF_NOTEDITED;
3012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003014 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3015 if (curbuf->b_p_bl)
3016 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003017#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003018 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003019 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003020#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003022 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003023 if (*curbuf->b_p_ft == NUL)
3024 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003025 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003026 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003027 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003028 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003029
3030 return OK;
3031}
3032
3033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 * Put file name into IObuff with quotes.
3035 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003036 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003037msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 if (fname == NULL)
3040 fname = (char_u *)"-stdin-";
3041 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3042 IObuff[0] = '"';
3043 STRCAT(IObuff, "\" ");
3044}
3045
3046/*
3047 * Append message for text mode to IObuff.
3048 * Return TRUE if something appended.
3049 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003051msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053#ifndef USE_CRNL
3054 if (eol_type == EOL_DOS)
3055 {
3056 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3057 return TRUE;
3058 }
3059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (eol_type == EOL_MAC)
3061 {
3062 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3063 return TRUE;
3064 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003065#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if (eol_type == EOL_UNIX)
3067 {
3068 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3069 return TRUE;
3070 }
3071#endif
3072 return FALSE;
3073}
3074
3075/*
3076 * Append line and character count to IObuff.
3077 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079msg_add_lines(
3080 int insert_space,
3081 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003082 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 char_u *p;
3085
3086 p = IObuff + STRLEN(IObuff);
3087
3088 if (insert_space)
3089 *p++ = ' ';
3090 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003091 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003092 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else
3094 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003095 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003097 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003098 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003099 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101}
3102
3103/*
3104 * Append message for missing line separator to IObuff.
3105 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3110}
3111
Bram Moolenaar473952e2019-09-28 16:30:04 +02003112 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003113time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003115#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003116 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3117 // the seconds. Since the roundoff is done when flushing the inode, the
3118 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 return (t1 - t2 > 1 || t2 - t1 > 1);
3120#else
3121 return (t1 != t2);
3122#endif
3123}
3124
3125/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003126 * Return TRUE if file encoding "fenc" requires conversion from or to
3127 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003129 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003130need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003132 int same_encoding;
3133 int enc_flags;
3134 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003136 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003137 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003138 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003139 fenc_flags = 0;
3140 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003141 else
3142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003143 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3144 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003145 enc_flags = get_fio_flags(p_enc);
3146 fenc_flags = get_fio_flags(fenc);
3147 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3148 }
3149 if (same_encoding)
3150 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003151 // Specified encoding matches with 'encoding'. This requires
3152 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003153 return enc_unicode != 0;
3154 }
3155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003156 // Encodings differ. However, conversion is not needed when 'enc' is any
3157 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003158 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159}
3160
3161/*
3162 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3163 * internal conversion.
3164 * if "ptr" is an empty string, use 'encoding'.
3165 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003166 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003167get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 int prop;
3170
3171 if (*ptr == NUL)
3172 ptr = p_enc;
3173
3174 prop = enc_canon_props(ptr);
3175 if (prop & ENC_UNICODE)
3176 {
3177 if (prop & ENC_2BYTE)
3178 {
3179 if (prop & ENC_ENDIAN_L)
3180 return FIO_UCS2 | FIO_ENDIAN_L;
3181 return FIO_UCS2;
3182 }
3183 if (prop & ENC_4BYTE)
3184 {
3185 if (prop & ENC_ENDIAN_L)
3186 return FIO_UCS4 | FIO_ENDIAN_L;
3187 return FIO_UCS4;
3188 }
3189 if (prop & ENC_2WORD)
3190 {
3191 if (prop & ENC_ENDIAN_L)
3192 return FIO_UTF16 | FIO_ENDIAN_L;
3193 return FIO_UTF16;
3194 }
3195 return FIO_UTF8;
3196 }
3197 if (prop & ENC_LATIN1)
3198 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003199 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return 0;
3201}
3202
Bram Moolenaar473952e2019-09-28 16:30:04 +02003203#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204/*
3205 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3206 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3207 * Used for conversion between 'encoding' and 'fileencoding'.
3208 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003209 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003210get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 int cp;
3213
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003214 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (!enc_utf8 && enc_codepage <= 0)
3216 return 0;
3217
3218 cp = encname2codepage(ptr);
3219 if (cp == 0)
3220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003221# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (STRCMP(ptr, "utf-8") == 0)
3223 cp = CP_UTF8;
3224 else
3225# endif
3226 return 0;
3227 }
3228 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3229}
3230#endif
3231
Bram Moolenaar473952e2019-09-28 16:30:04 +02003232#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233/*
3234 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3235 * needed for the internal conversion to/from utf-8 or latin1.
3236 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003237 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003238get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3241 && (enc_canon_props(ptr) & ENC_MACROMAN))
3242 return FIO_MACROMAN;
3243 return 0;
3244}
3245#endif
3246
3247/*
3248 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3249 * "size" must be at least 2.
3250 * Return the name of the encoding and set "*lenp" to the length.
3251 * Returns NULL when no BOM found.
3252 */
3253 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003254check_for_bom(
3255 char_u *p,
3256 long size,
3257 int *lenp,
3258 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 char *name = NULL;
3261 int len = 2;
3262
3263 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003264 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003266 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 len = 3;
3268 }
3269 else if (p[0] == 0xff && p[1] == 0xfe)
3270 {
3271 if (size >= 4 && p[2] == 0 && p[3] == 0
3272 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003274 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 len = 4;
3276 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003277 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003278 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003279 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003280 // utf-16le is preferred, it also works for ucs-2le text
3281 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283 else if (p[0] == 0xfe && p[1] == 0xff
3284 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003286 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003287 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003288 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003289 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003290 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3293 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003295 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 len = 4;
3297 }
3298
3299 *lenp = len;
3300 return (char_u *)name;
3301}
3302
3303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 * Try to find a shortname by comparing the fullname with the current
3305 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003306 * Returns "full_path" or pointer into "full_path" if shortened.
3307 */
3308 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003309shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003310{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003311 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003312 char_u *p = full_path;
3313
Bram Moolenaard9462e32011-04-11 21:35:11 +02003314 dirname = alloc(MAXPATHL);
3315 if (dirname == NULL)
3316 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003317 if (mch_dirname(dirname, MAXPATHL) == OK)
3318 {
3319 p = shorten_fname(full_path, dirname);
3320 if (p == NULL || *p == NUL)
3321 p = full_path;
3322 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003323 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003324 return p;
3325}
3326
3327/*
3328 * Try to find a shortname by comparing the fullname with the current
3329 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 * Returns NULL if not shorter name possible, pointer into "full_path"
3331 * otherwise.
3332 */
3333 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003334shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 int len;
3337 char_u *p;
3338
3339 if (full_path == NULL)
3340 return NULL;
3341 len = (int)STRLEN(dir_name);
3342 if (fnamencmp(dir_name, full_path, len) == 0)
3343 {
3344 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003345#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003347 * MS-Windows: when a file is in the root directory, dir_name will end
3348 * in a slash, since C: by itself does not define a specific dir. In
3349 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 if (!((len > 2) && (*(p - 2) == ':')))
3352#endif
3353 {
3354 if (vim_ispathsep(*p))
3355 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003356#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
3358 p = NULL;
3359#endif
3360 }
3361 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003362#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 /*
3364 * When using a file in the current drive, remove the drive name:
3365 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3366 * a floppy from "A:\dir" to "B:\dir".
3367 */
3368 else if (len > 3
3369 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3370 && full_path[1] == ':'
3371 && vim_ispathsep(full_path[2]))
3372 p = full_path + 2;
3373#endif
3374 else
3375 p = NULL;
3376 return p;
3377}
3378
3379/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003380 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * When "force" is TRUE: Use full path from now on for files currently being
3382 * edited, both for file name and swap file name. Try to shorten the file
3383 * names a bit, if safe to do so.
3384 * When "force" is FALSE: Only try to shorten absolute file names.
3385 * For buffers that have buftype "nofile" or "scratch": never change the file
3386 * name.
3387 */
3388 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003389shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3390{
3391 char_u *p;
3392
3393 if (buf->b_fname != NULL
3394#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003395 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003396#endif
3397 && !path_with_url(buf->b_fname)
3398 && (force
3399 || buf->b_sfname == NULL
3400 || mch_isFullName(buf->b_sfname)))
3401 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003402 if (buf->b_sfname != buf->b_ffname)
3403 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003404 p = shorten_fname(buf->b_ffname, dirname);
3405 if (p != NULL)
3406 {
3407 buf->b_sfname = vim_strsave(p);
3408 buf->b_fname = buf->b_sfname;
3409 }
3410 if (p == NULL || buf->b_fname == NULL)
3411 buf->b_fname = buf->b_ffname;
3412 }
3413}
3414
3415/*
3416 * Shorten filenames for all buffers.
3417 */
3418 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003419shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 char_u dirname[MAXPATHL];
3422 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
3424 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003425 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003427 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003429 // Always make the swap file name a full path, a "nofile" buffer may
3430 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003431 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003434 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003435#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003436 popup_update_preview_title();
3437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438}
3439
3440#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3441 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003442 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 || defined(PROTO)
3444/*
3445 * Shorten all filenames in "fnames[count]" by current directory.
3446 */
3447 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003448shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 int i;
3451 char_u dirname[MAXPATHL];
3452 char_u *p;
3453
3454 if (fnames == NULL || count < 1)
3455 return;
3456 mch_dirname(dirname, sizeof(dirname));
3457 for (i = 0; i < count; ++i)
3458 {
3459 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003461 // shorten_fname() returns pointer in given "fnames[i]". If free
3462 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3463 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 p = vim_strsave(p);
3465 vim_free(fnames[i]);
3466 fnames[i] = p;
3467 }
3468 }
3469}
3470#endif
3471
3472/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003473 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 * fo_o_h.ext for MSDOS or when shortname option set.
3475 *
3476 * Assumed that fname is a valid name found in the filesystem we assure that
3477 * the return value is a different name and ends in 'ext'.
3478 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3479 * characters otherwise.
3480 * Space for the returned name is allocated, must be freed later.
3481 * Returns NULL when out of memory.
3482 */
3483 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003484modname(
3485 char_u *fname,
3486 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003487 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003489 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 fname, ext, prepend_dot);
3491}
3492
3493 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003494buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003495 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003496 char_u *fname,
3497 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003498 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 char_u *retval;
3501 char_u *s;
3502 char_u *e;
3503 char_u *ptr;
3504 int fnamelen, extlen;
3505
3506 extlen = (int)STRLEN(ext);
3507
3508 /*
3509 * If there is no file name we must get the name of the current directory
3510 * (we need the full path in case :cd is used).
3511 */
3512 if (fname == NULL || *fname == NUL)
3513 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003514 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (retval == NULL)
3516 return NULL;
3517 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3518 (fnamelen = (int)STRLEN(retval)) == 0)
3519 {
3520 vim_free(retval);
3521 return NULL;
3522 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003523 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 retval[fnamelen++] = PATHSEP;
3526 retval[fnamelen] = NUL;
3527 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003528 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 else
3531 {
3532 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003533 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (retval == NULL)
3535 return NULL;
3536 STRCPY(retval, fname);
3537#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003538 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
3540 }
3541
3542 /*
3543 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3544 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3545 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3546 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3547 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003548 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003550 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003551 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003554 {
3555 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003560 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3562 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 s = ptr + STRLEN(ptr);
3565
3566 /*
3567 * For 8.3 file names we may have to reduce the length.
3568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 /*
3572 * If there is no file name, or the file name ends in '/', and the
3573 * extension starts with '.', put a '_' before the dot, because just
3574 * ".ext" is invalid.
3575 */
3576 if (fname == NULL || *fname == NUL
3577 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 *s++ = '_';
3581 }
3582 /*
3583 * If the extension starts with '.', truncate the base name at 8
3584 * characters
3585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003588 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 s = ptr + 8;
3591 *s = '\0';
3592 }
3593 }
3594 /*
3595 * If the extension doesn't start with '.', and the file name
3596 * doesn't have an extension yet, append a '.'
3597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 else if ((e = vim_strchr(ptr, '.')) == NULL)
3599 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 /*
3601 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003602 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 */
3604 else if ((int)STRLEN(e) + extlen > 4)
3605 s = e + 4 - extlen;
3606 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003607#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 /*
3609 * If there is no file name, and the extension starts with '.', put a
3610 * '_' before the dot, because just ".ext" may be invalid if it's on a
3611 * FAT partition, and on HPFS it doesn't matter.
3612 */
3613 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3614 *s++ = '_';
3615#endif
3616
3617 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003618 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 * ext can start with '.' and cannot exceed 3 more characters.
3620 */
3621 STRCPY(s, ext);
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 /*
3624 * Prepend the dot.
3625 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003626 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003628 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 /*
3633 * Check that, after appending the extension, the file name is really
3634 * different.
3635 */
3636 if (fname != NULL && STRCMP(fname, retval) == 0)
3637 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003638 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 while (--s >= ptr)
3640 {
3641 if (*s != '_')
3642 {
3643 *s = '_';
3644 break;
3645 }
3646 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003647 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 *ptr = 'v';
3649 }
3650 return retval;
3651}
3652
3653/*
3654 * Like fgets(), but if the file line is too long, it is truncated and the
3655 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003656 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
3658 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003659vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660{
3661 char *eof;
3662#define FGETS_SIZE 200
3663 char tbuf[FGETS_SIZE];
3664
3665 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3668 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003669 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003671 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 do
3673 {
3674 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003675 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3677 }
3678 return (eof == NULL);
3679}
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681/*
3682 * rename() only works if both files are on the same file system, this
3683 * function will (attempts to?) copy the file across if rename fails -- webb
3684 * Return -1 for failure, 0 for success.
3685 */
3686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003687vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 int fd_in;
3690 int fd_out;
3691 int n;
3692 char *errmsg = NULL;
3693 char *buffer;
3694#ifdef AMIGA
3695 BPTR flock;
3696#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003697 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003698 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003699#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003700 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003701#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003702 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003705 * When the names are identical, there is nothing to do. When they refer
3706 * to the same file (ignoring case and slash/backslash differences) but
3707 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 */
3709 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003710 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003711 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003712 use_tmp_file = TRUE;
3713 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003714 return 0;
3715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
3718 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3719 */
3720 if (mch_stat((char *)from, &st) < 0)
3721 return -1;
3722
Bram Moolenaar3576da72008-12-30 15:15:57 +00003723#ifdef UNIX
3724 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003725 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003727 // It's possible for the source and destination to be the same file.
3728 // This happens when "from" and "to" differ in case and are on a FAT32
3729 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003730 if (mch_stat((char *)to, &st_to) >= 0
3731 && st.st_dev == st_to.st_dev
3732 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003733 use_tmp_file = TRUE;
3734 }
3735#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003736#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003737 {
3738 BY_HANDLE_FILE_INFORMATION info1, info2;
3739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003740 // It's possible for the source and destination to be the same file.
3741 // In that case go through a temp file name. This makes rename("foo",
3742 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003743 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3744 && win32_fileinfo(to, &info2) == FILEINFO_OK
3745 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3746 && info1.nFileIndexHigh == info2.nFileIndexHigh
3747 && info1.nFileIndexLow == info2.nFileIndexLow)
3748 use_tmp_file = TRUE;
3749 }
3750#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003751
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003752 if (use_tmp_file)
3753 {
3754 char tempname[MAXPATHL + 1];
3755
3756 /*
3757 * Find a name that doesn't exist and is in the same directory.
3758 * Rename "from" to "tempname" and then rename "tempname" to "to".
3759 */
3760 if (STRLEN(from) >= MAXPATHL - 5)
3761 return -1;
3762 STRCPY(tempname, from);
3763 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003764 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003765 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3766 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003767 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003768 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003769 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003770 if (mch_rename(tempname, (char *)to) == 0)
3771 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003772 // Strange, the second step failed. Try moving the
3773 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003774 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003775 return -1;
3776 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003777 // If it fails for one temp name it will most likely fail
3778 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003779 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003780 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003781 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003782 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003783 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 /*
3786 * Delete the "to" file, this is required on some systems to make the
3787 * mch_rename() work, on other systems it makes sure that we don't have
3788 * two files when the mch_rename() fails.
3789 */
3790
3791#ifdef AMIGA
3792 /*
3793 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3794 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003795 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 * deleting the "from" file (horror!) we lock it during the remove.
3797 *
3798 * When used for making a backup before writing the file: This should not
3799 * happen with ":w", because startscript() should detect this problem and
3800 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3801 * name. This problem does exist with ":w filename", but then the
3802 * original file will be somewhere else so the backup isn't really
3803 * important. If autoscripting is off the rename may fail.
3804 */
3805 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3806#endif
3807 mch_remove(to);
3808#ifdef AMIGA
3809 if (flock)
3810 UnLock(flock);
3811#endif
3812
3813 /*
3814 * First try a normal rename, return if it works.
3815 */
3816 if (mch_rename((char *)from, (char *)to) == 0)
3817 return 0;
3818
3819 /*
3820 * Rename() failed, try copying the file.
3821 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003822 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003823#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003824 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003825 acl = mch_get_acl(from);
3826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3828 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003829 {
3830#ifdef HAVE_ACL
3831 mch_free_acl(acl);
3832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003834 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003836 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003837 fd_out = mch_open((char *)to,
3838 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (fd_out == -1)
3840 {
3841 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003842#ifdef HAVE_ACL
3843 mch_free_acl(acl);
3844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 return -1;
3846 }
3847
Bram Moolenaar473952e2019-09-28 16:30:04 +02003848 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (buffer == NULL)
3850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003852 close(fd_in);
3853#ifdef HAVE_ACL
3854 mch_free_acl(acl);
3855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 return -1;
3857 }
3858
Bram Moolenaar473952e2019-09-28 16:30:04 +02003859 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003860 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
3862 errmsg = _("E208: Error writing to \"%s\"");
3863 break;
3864 }
3865
3866 vim_free(buffer);
3867 close(fd_in);
3868 if (close(fd_out) < 0)
3869 errmsg = _("E209: Error closing \"%s\"");
3870 if (n < 0)
3871 {
3872 errmsg = _("E210: Error reading \"%s\"");
3873 to = from;
3874 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003875#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003876 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003877#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003878#ifdef HAVE_ACL
3879 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003880 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003881#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003882#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003883 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (errmsg != NULL)
3886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003887 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return -1;
3889 }
3890 mch_remove(from);
3891 return 0;
3892}
3893
3894static int already_warned = FALSE;
3895
3896/*
3897 * Check if any not hidden buffer has been changed.
3898 * Postpone the check if there are characters in the stuff buffer, a global
3899 * command is being executed, a mapping is being executed or an autocommand is
3900 * busy.
3901 * Returns TRUE if some message was written (screen should be redrawn and
3902 * cursor positioned).
3903 */
3904 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003905check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
3908 buf_T *buf;
3909 int didit = 0;
3910 int n;
3911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003912 // Don't check timestamps while system() or another low-level function may
3913 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (no_check_timestamps > 0)
3915 return FALSE;
3916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003917 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3918 // event and we would keep on checking if the file is steadily growing.
3919 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (focus && did_check_timestamps)
3921 {
3922 need_check_timestamps = TRUE;
3923 return FALSE;
3924 }
3925
3926 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003927 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 else
3930 {
3931 ++no_wait_return;
3932 did_check_timestamps = TRUE;
3933 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003934 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (buf->b_nwindows > 0)
3938 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003939 bufref_T bufref;
3940
3941 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 n = buf_check_timestamp(buf, focus);
3943 if (didit < n)
3944 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003945 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003947 // Autocommands have removed the buffer, start at the
3948 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 buf = firstbuf;
3950 continue;
3951 }
3952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 --no_wait_return;
3955 need_check_timestamps = FALSE;
3956 if (need_wait_return && didit == 2)
3957 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003958 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003959 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 out_flush();
3961 }
3962 }
3963 return didit;
3964}
3965
3966/*
3967 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3968 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3969 * empty.
3970 */
3971 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003972move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
3974 buf_T *tbuf = curbuf;
3975 int retval = OK;
3976 linenr_T lnum;
3977 char_u *p;
3978
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003979 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 curbuf = tobuf;
3981 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3982 {
3983 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3984 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3985 {
3986 vim_free(p);
3987 retval = FAIL;
3988 break;
3989 }
3990 vim_free(p);
3991 }
3992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003993 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (retval != FAIL)
3995 {
3996 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00003997 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003998 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004000 // Oops! We could try putting back the saved lines, but that
4001 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 retval = FAIL;
4003 break;
4004 }
4005 }
4006
4007 curbuf = tbuf;
4008 return retval;
4009}
4010
4011/*
4012 * Check if buffer "buf" has been changed.
4013 * Also check if the file for a new buffer unexpectedly appeared.
4014 * return 1 if a changed buffer was found.
4015 * return 2 if a message has been displayed.
4016 * return 0 otherwise.
4017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004019buf_check_timestamp(
4020 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004021 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004023 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 int stat_res;
4025 int retval = 0;
4026 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004027 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004029 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int helpmesg = FALSE;
4031 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004032 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4034 int can_reload = FALSE;
4035#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004036 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 int orig_mode = buf->b_orig_mode;
4038#ifdef FEAT_GUI
4039 int save_mouse_correct = need_mouse_correct;
4040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004042 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004043#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004044 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004045#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004046 bufref_T bufref;
4047
4048 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004050 // If there is no file name, the buffer is not loaded, 'buftype' is
4051 // set, we are in the middle of a save or being called recursively: ignore
4052 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (buf->b_ffname == NULL
4054 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004055 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004058#ifdef FEAT_NETBEANS_INTG
4059 || isNetbeansBuffer(buf)
4060#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004061#ifdef FEAT_TERMINAL
4062 || buf->b_term != NULL
4063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 )
4065 return 0;
4066
4067 if ( !(buf->b_flags & BF_NOTEDITED)
4068 && buf->b_mtime != 0
4069 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4070 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004071 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#ifdef HAVE_ST_MODE
4073 || (int)st.st_mode != buf->b_orig_mode
4074#else
4075 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4076#endif
4077 ))
4078 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004079 long prev_b_mtime = buf->b_mtime;
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 retval = 1;
4082
Bram Moolenaar386bc822018-07-07 18:34:12 +02004083 // set b_mtime to stop further warnings (e.g., when executing
4084 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (stat_res < 0)
4086 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004087 // Check the file again later to see if it re-appears.
4088 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 buf->b_orig_size = 0;
4090 buf->b_orig_mode = 0;
4091 }
4092 else
4093 buf_store_time(buf, &st, buf->b_ffname);
4094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004095 // Don't do anything for a directory. Might contain the file
4096 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (mch_isdir(buf->b_fname))
4098 ;
4099
4100 /*
4101 * If 'autoread' is set, the buffer has no changes and the file still
4102 * exists, reload the buffer. Use the buffer-local option value if it
4103 * was set, the global option value otherwise.
4104 */
4105 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4106 && !bufIsChanged(buf) && stat_res >= 0)
4107 reload = TRUE;
4108 else
4109 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004110 if (stat_res < 0)
4111 reason = "deleted";
4112 else if (bufIsChanged(buf))
4113 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004114 /*
4115 * Check if the file contents really changed to avoid giving a
4116 * warning when only the timestamp was set (e.g., checked out of
4117 * CVS). Always warn when the buffer was changed.
4118 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004119 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4120 reason = "changed";
4121 else if (orig_mode != buf->b_orig_mode)
4122 reason = "mode";
4123 else
4124 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
4127 * Only give the warning if there are no FileChangedShell
4128 * autocommands.
4129 * Avoid being called recursively by setting "busy".
4130 */
4131 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004132#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004133 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4134 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004135#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004136 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4138 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004139 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 busy = FALSE;
4141 if (n)
4142 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004143 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004144 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004145#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004146 s = get_vim_var_str(VV_FCS_CHOICE);
4147 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4148 reload = TRUE;
4149 else if (STRCMP(s, "ask") == 0)
4150 n = FALSE;
4151 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004152#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004153 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004155 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004157 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004158 {
4159 // Only give the message once.
4160 if (prev_b_mtime != -1)
4161 mesg = _("E211: File \"%s\" no longer available");
4162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 else
4164 {
4165 helpmesg = TRUE;
4166#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4167 can_reload = TRUE;
4168#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004169 if (reason[2] == 'n')
4170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004172 mesg2 = _("See \":help W12\" for more info.");
4173 }
4174 else if (reason[1] == 'h')
4175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004177 mesg2 = _("See \":help W11\" for more info.");
4178 }
4179 else if (*reason == 'm')
4180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 mesg2 = _("See \":help W16\" for more info.");
4183 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004184 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004185 // Only timestamp changed, store it to avoid a warning
4186 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004187 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 }
4190 }
4191
4192 }
4193 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4194 && vim_fexists(buf->b_ffname))
4195 {
4196 retval = 1;
4197 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4198 buf->b_flags |= BF_NEW_W;
4199#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4200 can_reload = TRUE;
4201#endif
4202 }
4203
4204 if (mesg != NULL)
4205 {
4206 path = home_replace_save(buf, buf->b_fname);
4207 if (path != NULL)
4208 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004209 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004211 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004212 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004213#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004214 // Set warningmsg here, before the unimportant and output-specific
4215 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004216 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4219 if (can_reload)
4220 {
4221 if (*mesg2 != NUL)
4222 {
4223 STRCAT(tbuf, "\n");
4224 STRCAT(tbuf, mesg2);
4225 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004226 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4227 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004228 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 reload = TRUE;
4230 }
4231 else
4232#endif
4233 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4234 {
4235 if (*mesg2 != NUL)
4236 {
4237 STRCAT(tbuf, "; ");
4238 STRCAT(tbuf, mesg2);
4239 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004240 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 retval = 2;
4242 }
4243 else
4244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
4247 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004248 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004250 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 msg_clr_eos();
4252 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004253 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004256#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004258#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004259 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004260 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004262 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 redraw_cmdline = FALSE;
4264 }
4265 }
4266 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
4269 vim_free(path);
4270 vim_free(tbuf);
4271 }
4272 }
4273
4274 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004277 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004278#ifdef FEAT_PERSISTENT_UNDO
4279 if (buf->b_p_udf && buf->b_ffname != NULL)
4280 {
4281 char_u hash[UNDO_HASH_SIZE];
4282 buf_T *save_curbuf = curbuf;
4283
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004284 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004285 curbuf = buf;
4286 u_compute_hash(hash);
4287 u_write_undo(NULL, FALSE, buf, hash);
4288 curbuf = save_curbuf;
4289 }
4290#endif
4291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004293 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004294 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004295 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4296 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004298 // restore this in case an autocommand has set it; it would break
4299 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 need_mouse_correct = save_mouse_correct;
4301#endif
4302
4303 return retval;
4304}
4305
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004306/*
4307 * Reload a buffer that is already loaded.
4308 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004309 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4310 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004311 */
4312 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004313buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004314{
4315 exarg_T ea;
4316 pos_T old_cursor;
4317 linenr_T old_topline;
4318 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004319 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004320 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004321 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004322 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004323 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004325 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004326 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 // We only want to read the text from the file, not reset the syntax
4329 // highlighting, clear marks, diff status, etc. Force the fileformat
4330 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004331 if (prep_exarg(&ea, buf) == OK)
4332 {
4333 old_cursor = curwin->w_cursor;
4334 old_topline = curwin->w_topline;
4335
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004336 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 // Save all the text, so that the reload can be undone.
4339 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004340 u_sync(FALSE);
4341 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4342 flags |= READ_KEEP_UNDO;
4343 }
4344
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004345 /*
4346 * To behave like when a new file is edited (matters for
4347 * BufReadPost autocommands) we first need to delete the current
4348 * buffer contents. But if reading the file fails we should keep
4349 * the old contents. Can't use memory only, the file might be
4350 * too big. Use a hidden buffer to move the buffer contents to.
4351 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004352 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004353 savebuf = NULL;
4354 else
4355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004356 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004357 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004358 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004359 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004362 curbuf = savebuf;
4363 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004364 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365 curbuf = buf;
4366 curwin->w_buffer = buf;
4367 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004368 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004369 || move_lines(buf, savebuf) == FAIL)
4370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004371 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004372 buf->b_fname);
4373 saved = FAIL;
4374 }
4375 }
4376
4377 if (saved == OK)
4378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4380 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004381 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4382 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004383 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004384 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004385#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004386 if (!aborting())
4387#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004388 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004389 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 // Put the text back from the save buffer. First
4392 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004393 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004394 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004395 break;
4396 (void)move_lines(savebuf, buf);
4397 }
4398 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004402 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004403 if ((flags & READ_KEEP_UNDO) == 0)
4404 {
4405 u_blockfree(buf);
4406 u_clearall(buf);
4407 }
4408 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004409 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004410 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004411 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004412 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004413 }
4414 }
4415 vim_free(ea.cmd);
4416
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004417 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004418 wipe_buffer(savebuf, FALSE);
4419
4420#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004422 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004423#endif
4424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 // Restore the topline and cursor position and check it (lines may
4426 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004427 if (old_topline > curbuf->b_ml.ml_line_count)
4428 curwin->w_topline = curbuf->b_ml.ml_line_count;
4429 else
4430 curwin->w_topline = old_topline;
4431 curwin->w_cursor = old_cursor;
4432 check_cursor();
4433 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004434 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004435#ifdef FEAT_FOLDING
4436 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004437 win_T *wp;
4438 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004441 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004442 if (wp->w_buffer == curwin->w_buffer
4443 && !foldmethodIsManual(wp))
4444 foldUpdateAll(wp);
4445 }
4446#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004447 // If the mode didn't change and 'readonly' was set, keep the old
4448 // value; the user probably used the ":view" command. But don't
4449 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004450 if (orig_mode == curbuf->b_orig_mode)
4451 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004453 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004454 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004455 }
4456
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004457 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004458 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004459 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004460}
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004463buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004466 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467#ifdef HAVE_ST_MODE
4468 buf->b_orig_mode = (int)st->st_mode;
4469#else
4470 buf->b_orig_mode = mch_getperm(fname);
4471#endif
4472}
4473
4474/*
4475 * Adjust the line with missing eol, used for the next write.
4476 * Used for do_filter(), when the input lines for the filter are deleted.
4477 */
4478 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004479write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004482 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483}
4484
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004485// Subfuncions for readdirex()
4486#ifdef FEAT_EVAL
4487# ifdef MSWIN
4488 static char_u *
4489getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4490{
4491 stat_T st;
4492 unsigned short st_mode;
4493 DWORD flag = wfd->dwFileAttributes;
4494 WCHAR *wp;
4495
4496 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4497 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4498 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4499 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4500
4501 wp = wcsrchr(wfd->cFileName, L'.');
4502 if (wp != NULL)
4503 {
4504 if (_wcsicmp(wp, L".exe") == 0 ||
4505 _wcsicmp(wp, L".com") == 0 ||
4506 _wcsicmp(wp, L".cmd") == 0 ||
4507 _wcsicmp(wp, L".bat") == 0)
4508 st_mode |= _S_IEXEC;
4509 }
4510
4511 // Copy user bits to group/other.
4512 st_mode |= (st_mode & 0700) >> 3;
4513 st_mode |= (st_mode & 0700) >> 6;
4514
4515 st.st_mode = st_mode;
4516 return getfpermst(&st, perm);
4517}
4518
4519 static char_u *
4520getftypewfd(WIN32_FIND_DATAW *wfd)
4521{
4522 DWORD flag = wfd->dwFileAttributes;
4523 DWORD tag = wfd->dwReserved0;
4524
4525 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4526 {
4527 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4528 return (char_u*)"junction";
4529 else if (tag == IO_REPARSE_TAG_SYMLINK)
4530 {
4531 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4532 return (char_u*)"linkd";
4533 else
4534 return (char_u*)"link";
4535 }
4536 return (char_u*)"reparse"; // unknown reparse point type
4537 }
4538 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4539 return (char_u*)"dir";
4540 else
4541 return (char_u*)"file";
4542}
4543
4544 static dict_T *
4545create_readdirex_item(WIN32_FIND_DATAW *wfd)
4546{
4547 dict_T *item;
4548 char_u *p;
4549 varnumber_T size, time;
4550 char_u permbuf[] = "---------";
4551
4552 item = dict_alloc();
4553 if (item == NULL)
4554 return NULL;
4555 item->dv_refcount++;
4556
4557 p = utf16_to_enc(wfd->cFileName, NULL);
4558 if (p == NULL)
4559 goto theend;
4560 if (dict_add_string(item, "name", p) == FAIL)
4561 {
4562 vim_free(p);
4563 goto theend;
4564 }
4565 vim_free(p);
4566
4567 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4568 if (dict_add_number(item, "size", size) == FAIL)
4569 goto theend;
4570
4571 // Convert FILETIME to unix time.
4572 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4573 wfd->ftLastWriteTime.dwLowDateTime)
4574 - 116444736000000000) / 10000000;
4575 if (dict_add_number(item, "time", time) == FAIL)
4576 goto theend;
4577
4578 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4579 goto theend;
4580 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4581 goto theend;
4582
4583 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4584 goto theend;
4585 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4586 goto theend;
4587
4588 return item;
4589
4590theend:
4591 dict_unref(item);
4592 return NULL;
4593}
4594# else
4595 static dict_T *
4596create_readdirex_item(char_u *path, char_u *name)
4597{
4598 dict_T *item;
4599 char *p;
4600 size_t len;
4601 stat_T st;
4602 int ret, link = FALSE;
4603 varnumber_T size;
4604 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004605 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004606 struct passwd *pw;
4607 struct group *gr;
4608
4609 item = dict_alloc();
4610 if (item == NULL)
4611 return NULL;
4612 item->dv_refcount++;
4613
4614 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4615 p = alloc(len);
4616 if (p == NULL)
4617 goto theend;
4618 vim_snprintf(p, len, "%s/%s", path, name);
4619 ret = mch_lstat(p, &st);
4620 if (ret >= 0 && S_ISLNK(st.st_mode))
4621 {
4622 link = TRUE;
4623 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004624 if (ret < 0)
4625 q = (char_u*)"link";
4626
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004627 }
4628 vim_free(p);
4629
4630 if (dict_add_string(item, "name", name) == FAIL)
4631 goto theend;
4632
4633 if (ret >= 0)
4634 {
4635 size = (varnumber_T)st.st_size;
4636 if (S_ISDIR(st.st_mode))
4637 size = 0;
4638 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004639 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004640 size = -2;
4641 if (dict_add_number(item, "size", size) == FAIL)
4642 goto theend;
4643 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4644 goto theend;
4645
4646 if (link)
4647 {
4648 if (S_ISDIR(st.st_mode))
4649 q = (char_u*)"linkd";
4650 else
4651 q = (char_u*)"link";
4652 }
4653 else
4654 q = getftypest(&st);
4655 if (dict_add_string(item, "type", q) == FAIL)
4656 goto theend;
4657 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4658 goto theend;
4659
4660 pw = getpwuid(st.st_uid);
4661 if (pw == NULL)
4662 q = (char_u*)"";
4663 else
4664 q = (char_u*)pw->pw_name;
4665 if (dict_add_string(item, "user", q) == FAIL)
4666 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004667# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004668 gr = getgrgid(st.st_gid);
4669 if (gr == NULL)
4670 q = (char_u*)"";
4671 else
4672 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004673# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004674 if (dict_add_string(item, "group", q) == FAIL)
4675 goto theend;
4676 }
4677 else
4678 {
4679 if (dict_add_number(item, "size", -1) == FAIL)
4680 goto theend;
4681 if (dict_add_number(item, "time", -1) == FAIL)
4682 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004683 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004684 goto theend;
4685 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4686 goto theend;
4687 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4688 goto theend;
4689 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4690 goto theend;
4691 }
4692 return item;
4693
4694theend:
4695 dict_unref(item);
4696 return NULL;
4697}
4698# endif
4699
4700 static int
4701compare_readdirex_item(const void *p1, const void *p2)
4702{
4703 char_u *name1, *name2;
4704
4705 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4706 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004707 if (readdirex_sort == READDIR_SORT_BYTE)
4708 return STRCMP(name1, name2);
4709 else if (readdirex_sort == READDIR_SORT_IC)
4710 return STRICMP(name1, name2);
4711 else
4712 return STRCOLL(name1, name2);
4713}
4714
4715 static int
4716compare_readdir_item(const void *s1, const void *s2)
4717{
4718 if (readdirex_sort == READDIR_SORT_BYTE)
4719 return STRCMP(*(char **)s1, *(char **)s2);
4720 else if (readdirex_sort == READDIR_SORT_IC)
4721 return STRICMP(*(char **)s1, *(char **)s2);
4722 else
4723 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004724}
4725#endif
4726
Bram Moolenaarda440d22016-01-16 21:27:23 +01004727#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4728/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004729 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004730 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004731 * If "withattr" is TRUE, retrieve the names and their attributes.
4732 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004733 * Return OK for success, FAIL for failure.
4734 */
4735 int
4736readdir_core(
4737 garray_T *gap,
4738 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004739 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004740 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004741 int (*checkitem)(void *context, void *item),
4742 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004743{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004744 int failed = FALSE;
4745 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004746# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004747 char_u *buf;
4748 int ok;
4749 HANDLE hFind = INVALID_HANDLE_VALUE;
4750 WIN32_FIND_DATAW wfd;
4751 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004752# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004753 DIR *dirp;
4754 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004755# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004756
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004757 ga_init2(gap, (int)sizeof(void *), 20);
4758
4759# ifdef FEAT_EVAL
4760# define FREE_ITEM(item) do { \
4761 if (withattr) \
4762 dict_unref((dict_T*)item); \
4763 else \
4764 vim_free(item); \
4765 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004766
4767 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004768# else
4769# define FREE_ITEM(item) vim_free(item)
4770# endif
4771
4772# ifdef MSWIN
4773 buf = alloc(MAXPATHL);
4774 if (buf == NULL)
4775 return FAIL;
4776 STRNCPY(buf, path, MAXPATHL-5);
4777 p = buf + STRLEN(buf);
4778 MB_PTR_BACK(buf, p);
4779 if (*p == '\\' || *p == '/')
4780 *p = NUL;
4781 STRCAT(p, "\\*");
4782
4783 wn = enc_to_utf16(buf, NULL);
4784 if (wn != NULL)
4785 hFind = FindFirstFileW(wn, &wfd);
4786 ok = (hFind != INVALID_HANDLE_VALUE);
4787 if (!ok)
4788 {
4789 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004790 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004791 }
4792 else
4793 {
4794 while (ok)
4795 {
4796 int ignore;
4797 void *item;
4798 WCHAR *wp;
4799
4800 wp = wfd.cFileName;
4801 ignore = wp[0] == L'.' &&
4802 (wp[1] == NUL ||
4803 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004804 if (ignore)
4805 {
4806 ok = FindNextFileW(hFind, &wfd);
4807 continue;
4808 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004809# ifdef FEAT_EVAL
4810 if (withattr)
4811 item = (void*)create_readdirex_item(&wfd);
4812 else
4813# endif
4814 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4815 if (item == NULL)
4816 {
4817 failed = TRUE;
4818 break;
4819 }
4820
4821 if (!ignore && checkitem != NULL)
4822 {
4823 int r = checkitem(context, item);
4824
4825 if (r < 0)
4826 {
4827 FREE_ITEM(item);
4828 break;
4829 }
4830 if (r == 0)
4831 ignore = TRUE;
4832 }
4833
4834 if (!ignore)
4835 {
4836 if (ga_grow(gap, 1) == OK)
4837 ((void**)gap->ga_data)[gap->ga_len++] = item;
4838 else
4839 {
4840 failed = TRUE;
4841 FREE_ITEM(item);
4842 break;
4843 }
4844 }
4845 else
4846 FREE_ITEM(item);
4847
4848 ok = FindNextFileW(hFind, &wfd);
4849 }
4850 FindClose(hFind);
4851 }
4852
4853 vim_free(buf);
4854 vim_free(wn);
4855# else // MSWIN
4856 dirp = opendir((char *)path);
4857 if (dirp == NULL)
4858 {
4859 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004860 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004861 }
4862 else
4863 {
4864 for (;;)
4865 {
4866 int ignore;
4867 void *item;
4868
4869 dp = readdir(dirp);
4870 if (dp == NULL)
4871 break;
4872 p = (char_u *)dp->d_name;
4873
4874 ignore = p[0] == '.' &&
4875 (p[1] == NUL ||
4876 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004877 if (ignore)
4878 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004879# ifdef FEAT_EVAL
4880 if (withattr)
4881 item = (void*)create_readdirex_item(path, p);
4882 else
4883# endif
4884 item = (void*)vim_strsave(p);
4885 if (item == NULL)
4886 {
4887 failed = TRUE;
4888 break;
4889 }
4890
4891 if (!ignore && checkitem != NULL)
4892 {
4893 int r = checkitem(context, item);
4894
4895 if (r < 0)
4896 {
4897 FREE_ITEM(item);
4898 break;
4899 }
4900 if (r == 0)
4901 ignore = TRUE;
4902 }
4903
4904 if (!ignore)
4905 {
4906 if (ga_grow(gap, 1) == OK)
4907 ((void**)gap->ga_data)[gap->ga_len++] = item;
4908 else
4909 {
4910 failed = TRUE;
4911 FREE_ITEM(item);
4912 break;
4913 }
4914 }
4915 else
4916 FREE_ITEM(item);
4917 }
4918
4919 closedir(dirp);
4920 }
4921# endif // MSWIN
4922
4923# undef FREE_ITEM
4924
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004925 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004926 {
4927# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004928 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004929 if (withattr)
4930 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4931 compare_readdirex_item);
4932 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004933 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4934 compare_readdir_item);
4935# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004936 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004937# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004938 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004939
4940 return failed ? FAIL : OK;
4941}
4942
4943/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004944 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004945 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004946 */
4947 int
4948delete_recursive(char_u *name)
4949{
4950 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004951 int i;
4952 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004953 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004954
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004955 // A symbolic link to a directory itself is deleted, not the directory it
4956 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004957 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004958# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004959 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004960# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004961 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004962# endif
4963 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004964 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004965 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004966 if (exp == NULL)
4967 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004968 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004969 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004970 for (i = 0; i < ga.ga_len; ++i)
4971 {
4972 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4973 ((char_u **)ga.ga_data)[i]);
4974 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004975 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004976 }
4977 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004978 }
4979 else
4980 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004981 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004982 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004983 }
4984 else
4985 result = mch_remove(name) == 0 ? 0 : -1;
4986
4987 return result;
4988}
4989#endif
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004992static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004994# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4995/*
4996 * Open temporary directory and take file lock to prevent
4997 * to be auto-cleaned.
4998 */
4999 static void
5000vim_opentempdir(void)
5001{
5002 DIR *dp = NULL;
5003
5004 if (vim_tempdir_dp != NULL)
5005 return;
5006
5007 dp = opendir((const char*)vim_tempdir);
5008
5009 if (dp != NULL)
5010 {
5011 vim_tempdir_dp = dp;
5012 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5013 }
5014}
5015
5016/*
5017 * Close temporary directory - it automatically release file lock.
5018 */
5019 static void
5020vim_closetempdir(void)
5021{
5022 if (vim_tempdir_dp != NULL)
5023 {
5024 closedir(vim_tempdir_dp);
5025 vim_tempdir_dp = NULL;
5026 }
5027}
5028# endif
5029
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030/*
5031 * Delete the temp directory and all files it contains.
5032 */
5033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (vim_tempdir != NULL)
5037 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005038# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5039 vim_closetempdir();
5040# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005042 gettail(vim_tempdir)[-1] = NUL;
5043 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005044 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005049 * Directory "tempdir" was created. Expand this name to a full path and put
5050 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5051 * "tempdir" must be no longer than MAXPATHL.
5052 */
5053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005054vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005055{
5056 char_u *buf;
5057
Bram Moolenaar964b3742019-05-24 18:54:09 +02005058 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005059 if (buf != NULL)
5060 {
5061 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5062 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005063 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005064 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005065# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5066 vim_opentempdir();
5067# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005068 vim_free(buf);
5069 }
5070}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005071#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005072
5073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 * vim_tempname(): Return a unique name that can be used for a temp file.
5075 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005076 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5077 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 *
5079 * The returned pointer is to allocated memory.
5080 * The returned pointer is NULL if no valid name was found.
5081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005089#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005090 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#else
5092 char_u itmp[TEMPNAMELEN];
5093#endif
5094
5095#ifdef TEMPDIRNAMES
5096 static char *(tempdirs[]) = {TEMPDIRNAMES};
5097 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005099 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100# endif
5101
5102 /*
5103 * This will create a directory for private use by this instance of Vim.
5104 * This is done once, and the same directory is used for all temp files.
5105 * This method avoids security problems because of symlink attacks et al.
5106 * It's also a bit faster, because we only need to check for an existing
5107 * file when creating the directory and not for each temp file.
5108 */
5109 if (vim_tempdir == NULL)
5110 {
5111 /*
5112 * Try the entries in TEMPDIRNAMES to create the temp directory.
5113 */
K.Takataeeec2542021-06-02 13:28:16 +02005114 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005116# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005117 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005118 long nr;
5119 long off;
5120# endif
5121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005122 // Expand $TMP, leave room for "/v1100000/999999999".
5123 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005125 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005128 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaareaf03392009-11-17 11:08:52 +00005130# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005131 {
5132# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005133 // Make sure the umask doesn't remove the executable bit.
5134 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005135 mode_t umask_save = umask(077);
5136# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005137 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005138 STRCAT(itmp, "vXXXXXX");
5139 if (mkdtemp((char *)itmp) != NULL)
5140 vim_settempdir(itmp);
5141# if defined(UNIX) || defined(VMS)
5142 (void)umask(umask_save);
5143# endif
5144 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005145# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005146 // Get an arbitrary number of up to 6 digits. When it's
5147 // unlikely that it already exists it will be faster,
5148 // otherwise it doesn't matter. The use of mkdir() avoids any
5149 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005151 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005153 // Try up to 10000 different values until we find a name that
5154 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 for (off = 0; off < 10000L; ++off)
5156 {
5157 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005158# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
Bram Moolenaareaf03392009-11-17 11:08:52 +00005162 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5163# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005164 // If mkdir() does not set errno to EEXIST, check for
5165 // existing file here. There is a race condition then,
5166 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (mch_stat((char *)itmp, &st) >= 0)
5168 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005169# endif
5170# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 // Make sure the umask doesn't remove the executable bit.
5172 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005176# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (r == 0)
5180 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005181 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005184# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005185 // If the mkdir() didn't fail because the file/dir exists,
5186 // we probably can't create any dir here, try another
5187 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005192# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (vim_tempdir != NULL)
5194 break;
5195 }
5196 }
5197 }
5198
5199 if (vim_tempdir != NULL)
5200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005201 // There is no need to check if the file exists, because we own the
5202 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5204 return vim_strsave(itmp);
5205 }
5206
5207 return NULL;
5208
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
Bram Moolenaar4f974752019-02-17 17:44:42 +01005211# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005212 WCHAR wszTempFile[_MAX_PATH + 1];
5213 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005214 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 char_u *retval;
5216 char_u *p;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005217 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005219 wcscpy(itmp, L"");
5220 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005221 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005222 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005223 wszTempFile[1] = L'\\';
5224 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005225 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005226 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005227
5228 // randomize the name to avoid collisions
5229 i = mch_get_pid() + extra_char;
5230 buf4[1] = chartab[i % 36];
5231 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005232 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005234 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005235 // GetTempFileName() will create the file, we don't want that
5236 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005238 // Backslashes in a temp file name cause problems when filtering with
5239 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
5240 // didn't set 'shellslash'.
5241 retval = utf16_to_enc(itmp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 if (*p_shcf == '-' || p_ssl)
5243 for (p = retval; *p; ++p)
5244 if (*p == '\\')
5245 *p = '/';
5246 return retval;
5247
Bram Moolenaar4f974752019-02-17 17:44:42 +01005248# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005251 char_u *p;
5252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005254 p = tmpnam((char *)itmp);
5255 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 return NULL;
5257# else
5258 char_u *p;
5259
5260# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005261 // mktemp() is not working on VMS. It seems to be
5262 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 sprintf((char *)itmp, "VIM%c", extra_char);
5264 p = (char_u *)tempnam("tmp:", (char *)itmp);
5265 if (p != NULL)
5266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005267 // VMS will use '.LIS' if we don't explicitly specify an extension,
5268 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 STRCPY(itmp, p);
5270 STRCAT(itmp, ".txt");
5271 free(p);
5272 }
5273 else
5274 return NULL;
5275# else
5276 STRCPY(itmp, TEMPNAME);
5277 if ((p = vim_strchr(itmp, '?')) != NULL)
5278 *p = extra_char;
5279 if (mktemp((char *)itmp) == NULL)
5280 return NULL;
5281# endif
5282# endif
5283
5284 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005285# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287}
5288
5289#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5290/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005291 * Convert all backslashes in fname to forward slashes in-place, unless when
5292 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 */
5294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 char_u *p;
5298
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005299 if (path_with_url(fname))
5300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005303 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005305 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 *p = '/';
5307}
5308#endif
5309
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005310/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005311 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5312 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5313 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 * Used for autocommands and 'wildignore'.
5315 * Returns TRUE if there is a match, FALSE otherwise.
5316 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005317 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 char_u *pattern, // pattern to match with
5320 regprog_T **prog, // pre-compiled regprog or NULL
5321 char_u *fname, // full path of file name
5322 char_u *sfname, // short file name or NULL
5323 char_u *tail, // tail of path
5324 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 regmatch_T regmatch;
5327 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005329 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005330 if (prog != NULL)
5331 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005333 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 /*
5336 * Try for a match with the pattern with:
5337 * 1. the full file name, when the pattern has a '/'.
5338 * 2. the short file name, when the pattern has a '/'.
5339 * 3. the tail of the file name, when the pattern has no '/'.
5340 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005341 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 && ((allow_dirs
5343 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5344 || (sfname != NULL
5345 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005346 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 result = TRUE;
5348
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005349 if (prog != NULL)
5350 *prog = regmatch.regprog;
5351 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005352 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 return result;
5354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356#if defined(FEAT_WILDIGN) || defined(PROTO)
5357/*
5358 * Return TRUE if a file matches with a pattern in "list".
5359 * "list" is a comma-separated list of patterns, like 'wildignore'.
5360 * "sfname" is the short file name or NULL, "ffname" the long file name.
5361 */
5362 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 char_u buf[100];
5366 char_u *tail;
5367 char_u *regpat;
5368 char allow_dirs;
5369 int match;
5370 char_u *p;
5371
5372 tail = gettail(sfname);
5373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 p = list;
5376 while (*p)
5377 {
5378 copy_option_part(&p, buf, 100, ",");
5379 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5380 if (regpat == NULL)
5381 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005382 match = match_file_pat(regpat, NULL, ffname, sfname,
5383 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 vim_free(regpat);
5385 if (match)
5386 return TRUE;
5387 }
5388 return FALSE;
5389}
5390#endif
5391
5392/*
5393 * Convert the given pattern "pat" which has shell style wildcards in it, into
5394 * a regular expression, and return the result in allocated memory. If there
5395 * is a directory path separator to be matched, then TRUE is put in
5396 * allow_dirs, otherwise FALSE is put there -- webb.
5397 * Handle backslashes before special characters, like "\*" and "\ ".
5398 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 * Returns NULL when out of memory.
5400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402file_pat_to_reg_pat(
5403 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005404 char_u *pat_end, // first char after pattern or NULL
5405 char *allow_dirs, // Result passed back out in here
5406 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005408 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 char_u *endp;
5410 char_u *reg_pat;
5411 char_u *p;
5412 int i;
5413 int nested = 0;
5414 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416 if (allow_dirs != NULL)
5417 *allow_dirs = FALSE;
5418 if (pat_end == NULL)
5419 pat_end = pat + STRLEN(pat);
5420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 for (p = pat; p < pat_end; p++)
5422 {
5423 switch (*p)
5424 {
5425 case '*':
5426 case '.':
5427 case ',':
5428 case '{':
5429 case '}':
5430 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005431 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 break;
5433#ifdef BACKSLASH_IN_FILENAME
5434 case '\\':
5435 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005436 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 break;
5438#endif
5439 default:
5440 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005441 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 ++p;
5444 ++size;
5445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 break;
5447 }
5448 }
5449 reg_pat = alloc(size + 1);
5450 if (reg_pat == NULL)
5451 return NULL;
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 if (pat[0] == '*')
5456 while (pat[0] == '*' && pat < pat_end - 1)
5457 pat++;
5458 else
5459 reg_pat[i++] = '^';
5460 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005461 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 while (endp - pat > 0 && *endp == '*')
5464 endp--;
5465 add_dollar = FALSE;
5466 }
5467 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5468 {
5469 switch (*p)
5470 {
5471 case '*':
5472 reg_pat[i++] = '.';
5473 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005474 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005475 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 break;
5477 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 case '~':
5479 reg_pat[i++] = '\\';
5480 reg_pat[i++] = *p;
5481 break;
5482 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 reg_pat[i++] = '.';
5484 break;
5485 case '\\':
5486 if (p[1] == NUL)
5487 break;
5488#ifdef BACKSLASH_IN_FILENAME
5489 if (!no_bslash)
5490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005491 // translate:
5492 // "\x" to "\\x" e.g., "dir\file"
5493 // "\*" to "\\.*" e.g., "dir\*.c"
5494 // "\?" to "\\." e.g., "dir\??.c"
5495 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5497 && p[1] != '+')
5498 {
5499 reg_pat[i++] = '[';
5500 reg_pat[i++] = '\\';
5501 reg_pat[i++] = '/';
5502 reg_pat[i++] = ']';
5503 if (allow_dirs != NULL)
5504 *allow_dirs = TRUE;
5505 break;
5506 }
5507 }
5508#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 // Undo escaping from ExpandEscape():
5510 // foo\?bar -> foo?bar
5511 // foo\%bar -> foo%bar
5512 // foo\,bar -> foo,bar
5513 // foo\ bar -> foo bar
5514 // Don't unescape \, * and others that are also special in a
5515 // regexp.
5516 // An escaped { must be unescaped since we use magic not
5517 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (*++p == '?'
5519#ifdef BACKSLASH_IN_FILENAME
5520 && no_bslash
5521#endif
5522 )
5523 reg_pat[i++] = '?';
5524 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005525 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005526 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005527 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005528 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5529 {
5530 reg_pat[i++] = '\\';
5531 reg_pat[i++] = '{';
5532 p += 2;
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 else
5535 {
5536 if (allow_dirs != NULL && vim_ispathsep(*p)
5537#ifdef BACKSLASH_IN_FILENAME
5538 && (!no_bslash || *p != '\\')
5539#endif
5540 )
5541 *allow_dirs = TRUE;
5542 reg_pat[i++] = '\\';
5543 reg_pat[i++] = *p;
5544 }
5545 break;
5546#ifdef BACKSLASH_IN_FILENAME
5547 case '/':
5548 reg_pat[i++] = '[';
5549 reg_pat[i++] = '\\';
5550 reg_pat[i++] = '/';
5551 reg_pat[i++] = ']';
5552 if (allow_dirs != NULL)
5553 *allow_dirs = TRUE;
5554 break;
5555#endif
5556 case '{':
5557 reg_pat[i++] = '\\';
5558 reg_pat[i++] = '(';
5559 nested++;
5560 break;
5561 case '}':
5562 reg_pat[i++] = '\\';
5563 reg_pat[i++] = ')';
5564 --nested;
5565 break;
5566 case ',':
5567 if (nested)
5568 {
5569 reg_pat[i++] = '\\';
5570 reg_pat[i++] = '|';
5571 }
5572 else
5573 reg_pat[i++] = ',';
5574 break;
5575 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005576 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005578 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 *allow_dirs = TRUE;
5580 reg_pat[i++] = *p;
5581 break;
5582 }
5583 }
5584 if (add_dollar)
5585 reg_pat[i++] = '$';
5586 reg_pat[i] = NUL;
5587 if (nested != 0)
5588 {
5589 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005590 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005592 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005593 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 return reg_pat;
5596}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005597
5598#if defined(EINTR) || defined(PROTO)
5599/*
5600 * Version of read() that retries when interrupted by EINTR (possibly
5601 * by a SIGWINCH).
5602 */
5603 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005605{
5606 long ret;
5607
5608 for (;;)
5609 {
5610 ret = vim_read(fd, buf, bufsize);
5611 if (ret >= 0 || errno != EINTR)
5612 break;
5613 }
5614 return ret;
5615}
5616
5617/*
5618 * Version of write() that retries when interrupted by EINTR (possibly
5619 * by a SIGWINCH).
5620 */
5621 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005623{
5624 long ret = 0;
5625 long wlen;
5626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005627 // Repeat the write() so long it didn't fail, other than being interrupted
5628 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005629 while (ret < (long)bufsize)
5630 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005631 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005632 if (wlen < 0)
5633 {
5634 if (errno != EINTR)
5635 break;
5636 }
5637 else
5638 ret += wlen;
5639 }
5640 return ret;
5641}
5642#endif