blob: 8d3e6f5fa7300d434025d924198de8cc33eae7b1 [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
Bram Moolenaare3f915d2020-07-14 23:02:44 +020016#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010019#if (defined(UNIX) || defined(VMS)) && defined(FEAT_EVAL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020020# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010023#if defined(VMS) && defined(HAVE_XOS_R_H)
24# include <x11/xos_r.h>
25#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar217e1b82019-12-01 21:41:28 +010027// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000028#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar9d8bfae2020-09-02 21:21:35 +020030#if defined(__hpux) && !defined(HAVE_DIRFD)
31# define dirfd(x) ((x)->__dd_fd)
32# define HAVE_DIRFD
33#endif
34
Bram Moolenaarf077db22019-08-13 00:18:24 +020035static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010036#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010037static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020040static 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 +000041#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000044
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020045#ifdef FEAT_EVAL
46static int readdirex_sort;
47#endif
48
Bram Moolenaar473952e2019-09-28 16:30:04 +020049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010050filemess(
51 buf_T *buf,
52 char_u *name,
53 char_u *s,
54 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055{
56 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020057 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
59 if (msg_silent != 0)
60 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010061 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020062
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020064 if (STRLEN(IObuff) > IOSIZE - 100)
65 IObuff[IOSIZE - 100] = NUL;
66
67 // Avoid an over-long translation to cause trouble.
68 STRNCAT(IObuff, s, 99);
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 /*
71 * For the first message may have to start a new line.
72 * For further ones overwrite the previous one, reset msg_scroll before
73 * calling filemess().
74 */
75 msg_scroll_save = msg_scroll;
76 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
77 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010078 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 check_for_delay(FALSE);
80 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020081 if (prev_msg_col != 0 && msg_col == 0)
82 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 msg_scroll = msg_scroll_save;
84 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010085 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
87 msg_clr_eos();
88 out_flush();
89 msg_scrolled_ign = FALSE;
90}
91
92/*
93 * Read lines from file "fname" into the buffer after line "from".
94 *
95 * 1. We allocate blocks with lalloc, as big as possible.
96 * 2. Each block is filled with characters from the file with a single read().
97 * 3. The lines are inserted in the buffer with ml_append().
98 *
99 * (caller must check that fname != NULL, unless READ_STDIN is used)
100 *
101 * "lines_to_skip" is the number of lines that must be skipped
102 * "lines_to_read" is the number of lines that are appended
103 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
104 *
105 * flags:
106 * READ_NEW starting to edit a new buffer
107 * READ_FILTER reading filter output
108 * READ_STDIN read from stdin instead of a file
109 * READ_BUFFER read from curbuf instead of a file (converting after reading
110 * stdin)
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100111 * READ_NOFILE do not read a file, only trigger BufReadCmd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200113 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 */
118 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100119readfile(
120 char_u *fname,
121 char_u *sfname,
122 linenr_T from,
123 linenr_T lines_to_skip,
124 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100125 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100126 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
128 int fd = 0;
129 int newfile = (flags & READ_NEW);
130 int check_readonly;
131 int filtering = (flags & READ_FILTER);
132 int read_stdin = (flags & READ_STDIN);
133 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200134 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000135 int set_options = newfile || read_buffer
136 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100137 linenr_T read_buf_lnum = 1; // next line to read from curbuf
138 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 char_u c;
140 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100141 char_u *ptr = NULL; // pointer into read buffer
142 char_u *buffer = NULL; // read buffer
143 char_u *new_buffer = NULL; // init to shut up gcc
144 char_u *line_start = NULL; // init to shut up gcc
145 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 colnr_T len;
147 long size = 0;
148 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200149 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 int skip_read = FALSE;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200151 off_T filesize_disk = 0; // file size read from disk
152 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_CRYPT
154 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200155 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200157#ifdef FEAT_PERSISTENT_UNDO
158 context_sha256_T sha_ctx;
159 int read_undo_file = FALSE;
160#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100161 int split = 0; // number of split lines
162#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100164 int error = FALSE; // errors encountered
165 int ff_error = EOL_UNKNOWN; // file format with errors
166 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#ifdef UNIX
168 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100169 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#else
171 int perm;
172#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100173 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200175 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 int file_readonly;
177 linenr_T skip_count = 0;
178 linenr_T read_count = 0;
179 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100180 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
181 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100182 int try_mac;
183 int try_dos;
184 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100187 linenr_T conv_error = 0; // line nr with conversion error
188 linenr_T illegal_byte = 0; // line nr with illegal byte
189 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
190 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000191 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100192 // BAD_KEEP, BAD_DROP or character to
193 // replace with
194 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100196 char_u *fenc; // fileencoding to use
197 int fenc_alloced; // fenc_next is in allocated memory
198 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 int advance_fenc = FALSE;
200 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100201#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100202 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100203# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100204 int did_iconv = FALSE; // TRUE when iconv() failed and trying
205 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100207#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100208 int converted = FALSE; // TRUE if conversion done
209 int notconverted = FALSE; // TRUE if conversion wanted but it
210 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100212 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100213 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200214 buf_T *old_curbuf;
215 char_u *old_b_ffname;
216 char_u *old_b_fname;
217 int using_b_ffname;
218 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200219 static char *msg_is_a_directory = N_("is a directory");
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100220 int eof;
Christian Brabandtaae58342023-04-23 17:50:22 +0100221#ifdef FEAT_SODIUM
222 int may_need_lseek = FALSE;
223#endif
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200224
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100225 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200226
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100227 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 /*
230 * If there is no file name yet, use the one for the read file.
231 * BF_NOTEDITED is set to reflect this.
232 * Don't do this for a read from a filter.
233 * Only do this when 'cpoptions' contains the 'f' flag.
234 */
235 if (curbuf->b_ffname == NULL
236 && !filtering
237 && fname != NULL
238 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
239 && !(flags & READ_DUMMY))
240 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000241 if (set_rw_fname(fname, sfname) == FAIL)
242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100245 // Remember the initial values of curbuf, curbuf->b_ffname and
246 // curbuf->b_fname to detect whether they are altered as a result of
247 // executing nasty autocommands. Also check if "fname" and "sfname"
248 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200249 old_curbuf = curbuf;
250 old_b_ffname = curbuf->b_ffname;
251 old_b_fname = curbuf->b_fname;
252 using_b_ffname = (fname == curbuf->b_ffname)
253 || (sfname == curbuf->b_ffname);
254 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200255
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100256 // After reading a file the cursor line changes but we don't want to
257 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000258 ex_no_reprint = TRUE;
259
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100260 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000261 need_fileinfo = FALSE;
262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 /*
264 * For Unix: Use the short file name whenever possible.
265 * Avoids problems with networks and when directory names are changed.
266 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
267 * another directory, which we don't detect.
268 */
269 if (sfname == NULL)
270 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200271#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 fname = sfname;
273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 /*
276 * The BufReadCmd and FileReadCmd events intercept the reading process by
277 * executing the associated commands instead.
278 */
279 if (!filtering && !read_stdin && !read_buffer)
280 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100281 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100283 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
285 curbuf->b_op_start.col = 0;
286
287 if (newfile)
288 {
289 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
290 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200291 {
292 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200294 if (aborting())
295 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200297 // The BufReadCmd code usually uses ":read" to get the text and
298 // perhaps ":file" to change the buffer name. But we should
299 // consider this to work like ":edit", thus reset the
300 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
301 // same file.
302 if (status == OK)
303 curbuf->b_flags &= ~BF_NOTEDITED;
304 return status;
305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
308 FALSE, NULL, eap))
309#ifdef FEAT_EVAL
310 return aborting() ? FAIL : OK;
311#else
312 return OK;
313#endif
314
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100315 curbuf->b_op_start = orig_start;
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100316
317 if (flags & READ_NOFILE)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100318 // Return NOTDONE instead of FAIL so that BufEnter can be triggered
319 // and other operations don't fail.
320 return NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
323 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100324 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100326 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000328 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200330 size_t namelen = STRLEN(fname);
331
332 // If the name is too long we might crash further on, quit here.
333 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000334 {
335 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
336 msg_end();
337 msg_scroll = msg_save;
338 return FAIL;
339 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200340
341 // If the name ends in a path separator, we can't open it. Check here,
342 // because reading the file may actually work, but then creating the
343 // swap file may destroy it! Reported on MS-DOS and Win 95.
344 if (after_pathsep(fname, fname + namelen))
345 {
346 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
347 msg_end();
348 msg_scroll = msg_save;
Bram Moolenaar40fa12a2021-09-22 14:18:13 +0200349 return NOTDONE;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
352
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200353 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100355#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200356 /*
357 * On Unix it is possible to read a directory, so we have to
358 * check for it before the mch_open().
359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100361 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
362 && !S_ISFIFO(perm) // ... or fifo
363 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000364# ifdef OPEN_CHR_FILES
365 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100366 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 )
369 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100370 int retval = FAIL;
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100373 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200374 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100375 retval = NOTDONE;
376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 else
378 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
379 msg_end();
380 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100381 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200383#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100384#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000385 /*
386 * MS-Windows allows opening a device, but we will probably get stuck
387 * trying to read it.
388 */
389 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
390 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000391 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000392 msg_end();
393 msg_scroll = msg_save;
394 return FAIL;
395 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000396#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200397 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000398
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200400 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402 /*
403 * When opening a new file we take the readonly flag from the file.
404 * Default is r/w, can be set to r/o below.
405 * Don't reset it when in readonly mode
406 * Only set/reset b_p_ro when BF_CHECK_RO is set.
407 */
408 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000409 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 curbuf->b_p_ro = FALSE;
411
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200412 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 if (mch_stat((char *)fname, &st) >= 0)
416 {
417 buf_store_time(curbuf, &st, fname);
418 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100419 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200420 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef UNIX
422 /*
423 * Use the protection bits of the original file for the swap file.
424 * This makes it possible for others to read the name of the
425 * edited file from the swapfile, but only if they can read the
426 * edited file.
427 * Remove the "write" and "execute" bits for group and others
428 * (they must not write the swapfile).
429 * Add the "read" and "write" bits for the user, otherwise we may
430 * not be able to write to the file ourselves.
431 * Setting the bits is done below, after creating the swap file.
432 */
433 swap_mode = (st.st_mode & 0644) | 0600;
434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#ifdef VMS
436 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000437 curbuf->b_fab_rat = st.st_fab_rat;
438 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#endif
440 }
441 else
442 {
443 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100444 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100446 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 curbuf->b_orig_size = 0;
448 curbuf->b_orig_mode = 0;
449 }
450
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100451 // Reset the "new file" flag. It will be set again below when the
452 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
454 }
455
456/*
457 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100458 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 */
460 file_readonly = FALSE;
461 if (read_stdin)
462 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100463#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100464 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 setmode(0, O_BINARY);
466#endif
467 }
468 else if (!read_buffer)
469 {
470#ifdef USE_MCH_ACCESS
471 if (
472# ifdef UNIX
473 !(perm & 0222) ||
474# endif
475 mch_access((char *)fname, W_OK))
476 file_readonly = TRUE;
477 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
478#else
479 if (!newfile
480 || readonlymode
481 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
482 {
483 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100484 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
486 }
487#endif
488 }
489
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 {
492#ifndef UNIX
493 int isdir_f;
494#endif
495 msg_scroll = msg_save;
496#ifndef UNIX
497 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100498 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 */
500 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (isdir_f)
503 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200504 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 }
507 else
508#endif
509 if (newfile)
510 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200511 if (perm < 0
512#ifdef ENOENT
513 && errno == ENOENT
514#endif
515 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {
517 /*
518 * Set the 'new-file' flag, so that when the file has
519 * been created by someone else, a ":w" will complain.
520 */
521 curbuf->b_flags |= BF_NEW;
522
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 // Create a swap file now, so that other Vims are warned
524 // that we are editing this file. Don't do this for a
525 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000530 if (curbuf != old_curbuf
531 || (using_b_ffname
532 && (old_b_ffname != curbuf->b_ffname))
533 || (using_b_fname
534 && (old_b_fname != curbuf->b_fname)))
535 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000536 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000537 return FAIL;
538 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000539 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000540 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200541 filemess(curbuf, sfname,
542 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000543 else
544 filemess(curbuf, sfname,
545 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 // Even though this is a new file, it might have been
548 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 check_marks_read();
550#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100551 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200552 if (eap != NULL)
553 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
555 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100556 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 save_file_ff(curbuf);
558
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100559#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100560 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 return FAIL;
562#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100563 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 }
565 else
566 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000567 filemess(curbuf, sfname, (char_u *)(
568# ifdef EFBIG
569 (errno == EFBIG) ? _("[File too big]") :
570# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200571# ifdef EOVERFLOW
572 (errno == EOVERFLOW) ? _("[File too big]") :
573# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000574 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100575 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577 }
578
579 return FAIL;
580 }
581
582 /*
583 * Only set the 'ro' flag for readonly files the first time they are
584 * loaded. Help files always get readonly mode
585 */
586 if ((check_readonly && file_readonly) || curbuf->b_help)
587 curbuf->b_p_ro = TRUE;
588
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000589 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100591 // Don't change 'eol' if reading from buffer as it will already be
592 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000593 if (!read_buffer)
594 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100595 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100596 curbuf->b_start_eof = FALSE;
597 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000598 curbuf->b_start_eol = TRUE;
599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000601 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100604 // Create a swap file now, so that other Vims are warned that we are
605 // editing this file.
606 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
609 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000610 if (!read_stdin && (curbuf != old_curbuf
611 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
612 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
613 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000614 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000615 if (!read_buffer)
616 close(fd);
617 return FAIL;
618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100620 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000621 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
622 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100623 {
624 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
625
626 /*
627 * If the group-read bit is set but not the world-read bit, then
628 * the group must be equal to the group of the original file. If
629 * we can't make that happen then reset the group-read bit. This
630 * avoids making the swap file readable to more users when the
631 * primary group of the user is too permissive.
632 */
633 if ((swap_mode & 044) == 040)
634 {
635 stat_T swap_st;
636
637 if (mch_stat((char *)swap_fname, &swap_st) >= 0
638 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200639# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100640 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200641 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200642# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200643 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100644 swap_mode &= 0600;
645 }
646
647 (void)mch_setperm(swap_fname, (long)swap_mode);
648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
650 }
651
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200652 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (swap_exists_action == SEA_QUIT)
654 {
655 if (!read_buffer && !read_stdin)
656 close(fd);
657 return FAIL;
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 /*
663 * Set '[ mark to the line above where the lines go (line 1 if zero).
664 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100665 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
667 curbuf->b_op_start.col = 0;
668
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100669 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
670 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
671 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (!read_buffer)
674 {
675 int m = msg_scroll;
676 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 /*
679 * The file must be closed again, the autocommands may want to change
680 * the file before reading it.
681 */
682 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100683 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685 /*
686 * The output from the autocommands should not overwrite anything and
687 * should not be overwritten: Set msg_scroll, restore its value if no
688 * output was done.
689 */
690 msg_scroll = TRUE;
691 if (filtering)
692 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
693 FALSE, curbuf, eap);
694 else if (read_stdin)
695 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
696 FALSE, curbuf, eap);
697 else if (newfile)
698 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
699 FALSE, curbuf, eap);
700 else
701 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
702 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100704 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
705 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
706 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100707 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (msg_scrolled == n)
710 msg_scroll = m;
711
712#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100713 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 --no_wait_return;
716 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100717 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 return FAIL;
719 }
720#endif
721 /*
722 * Don't allow the autocommands to change the current buffer.
723 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000724 *
725 * Don't allow the autocommands to change the buffer name either
726 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
728 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000729 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
730 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
732 {
733 --no_wait_return;
734 msg_scroll = msg_save;
735 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000736 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000738 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100739 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 return FAIL;
741 }
742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100744 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
746
747 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
748 {
749 /*
750 * Show the user that we are busy reading the input. Sometimes this
751 * may take a while. When reading from stdin another program may
752 * still be running, don't move the cursor to the last line, unless
753 * always using the GUI.
754 */
755 if (read_stdin)
756 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100757 if (!is_not_a_term())
758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200760# ifdef VIMDLL
761 if (!gui.in_use)
762# endif
763 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764#endif
765#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100766 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100767 if (gui.in_use && !gui.dying && !gui.starting)
768 {
Amon Sha10197932022-02-21 15:07:12 +0000769 // make a copy, gui_write() may try to change it
770 p = vim_strsave((char_u *)_("Reading from stdin..."));
771 if (p != NULL)
772 {
773 gui_write(p, (int)STRLEN(p));
774 vim_free(p);
775 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 else if (!read_buffer)
781 filemess(curbuf, sfname, (char_u *)"", 0);
782 }
783
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100784 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 /*
787 * Set linecnt now, before the "retry" caused by a wrong guess for
788 * fileformat, and after the autocommands, which may change them.
789 */
790 linecnt = curbuf->b_ml.ml_line_count;
791
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000793 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000794 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000795 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000796 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000797 curbuf->b_bad_char = eap->bad_char;
798 }
799 else
800 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000803 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 */
805 if (eap != NULL && eap->force_enc != 0)
806 {
807 fenc = enc_canonize(eap->cmd + eap->force_enc);
808 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000809 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811 else if (curbuf->b_p_bin)
812 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 fenc_alloced = FALSE;
815 }
816 else if (curbuf->b_help)
817 {
818 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000819 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 // Help files are either utf-8 or latin1. Try utf-8 first, if this
822 // fails it must be latin1.
823 // Always do this when 'encoding' is "utf-8". Otherwise only do
824 // this when needed to avoid [converted] remarks all the time.
825 // It is needed when the first line contains non-ASCII characters.
826 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 fenc = (char_u *)"latin1";
828 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000829 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000831 fc = fname[STRLEN(fname) - 1];
832 if (TOLOWER_ASC(fc) == 'x')
833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Read the first line (and a bit more). Immediately rewind to
835 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100836 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200837 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000838 for (p = firstline; p < firstline + len; ++p)
839 if (*p >= 0x80)
840 {
841 c = TRUE;
842 break;
843 }
844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846
847 if (c)
848 {
849 fenc_next = fenc;
850 fenc = (char_u *)"utf-8";
851
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100852 // When the file is utf-8 but a character doesn't fit in
853 // 'encoding' don't retry. In help text editing utf-8 bytes
854 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000855 if (!enc_utf8)
856 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858 fenc_alloced = FALSE;
859 }
860 else if (*p_fencs == NUL)
861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100862 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 fenc_alloced = FALSE;
864 }
865 else
866 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200868 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
871 /*
872 * Jump back here to retry reading the file in different ways.
873 * Reasons to retry:
874 * - encoding conversion failed: try another one from "fenc_next"
875 * - BOM detected and fenc was set, need to setup conversion
876 * - "fileformat" check failed: try another
877 *
878 * Variables set for special retry actions:
879 * "file_rewind" Rewind the file to start reading it again.
880 * "advance_fenc" Advance "fenc" using "fenc_next".
881 * "skip_read" Re-use already read bytes (BOM detected).
882 * "did_iconv" iconv() conversion failed, try 'charconvert'.
883 * "keep_fileformat" Don't reset "fileformat".
884 *
885 * Other status indicators:
886 * "tmpname" When != NULL did conversion with 'charconvert'.
887 * Output file has to be deleted afterwards.
888 * "iconv_fd" When != -1 did conversion with iconv().
889 */
890retry:
891
892 if (file_rewind)
893 {
894 if (read_buffer)
895 {
896 read_buf_lnum = 1;
897 read_buf_col = 0;
898 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200899 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100901 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 error = TRUE;
903 goto failed;
904 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100905 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200907 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000909 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000912 curbuf->b_start_bomb = FALSE;
913 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000914 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916
917 /*
918 * When retrying with another "fenc" and the first time "fileformat"
919 * will be reset.
920 */
921 if (keep_fileformat)
922 keep_fileformat = FALSE;
923 else
924 {
925 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000928 try_unix = try_dos = try_mac = FALSE;
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100931 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100933 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100935 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937
Bram Moolenaar13505972019-01-24 15:04:48 +0100938#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (iconv_fd != (iconv_t)-1)
940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100941 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 iconv_close(iconv_fd);
943 iconv_fd = (iconv_t)-1;
944 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 if (advance_fenc)
948 {
949 /*
950 * Try the next entry in 'fileencodings'.
951 */
952 advance_fenc = FALSE;
953
954 if (eap != NULL && eap->force_enc != 0)
955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100956 // Conversion given with "++cc=" wasn't possible, read
957 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000959 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (fenc_alloced)
961 vim_free(fenc);
962 fenc = (char_u *)"";
963 fenc_alloced = FALSE;
964 }
965 else
966 {
967 if (fenc_alloced)
968 vim_free(fenc);
969 if (fenc_next != NULL)
970 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200971 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973 else
974 {
975 fenc = (char_u *)"";
976 fenc_alloced = FALSE;
977 }
978 }
979 if (tmpname != NULL)
980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100981 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100982 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984 }
985
986 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000987 * Conversion may be required when the encoding of the file is different
988 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 */
990 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000991 converted = need_conversion(fenc);
992 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100995 // "ucs-bom" means we need to check the first bytes of the file
996 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (STRCMP(fenc, ENC_UCSBOM) == 0)
998 fio_flags = FIO_UCSBOM;
999
1000 /*
1001 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1002 * done. This is handled below after read(). Prepare the
1003 * fio_flags to avoid having to parse the string each time.
1004 * Also check for Unicode to Latin1 conversion, because iconv()
1005 * appears not to handle this correctly. This works just like
1006 * conversion to UTF-8 except how the resulting character is put in
1007 * the buffer.
1008 */
1009 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1010 fio_flags = get_fio_flags(fenc);
1011
Bram Moolenaar4f974752019-02-17 17:44:42 +01001012#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 /*
1014 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1015 * is handled with MultiByteToWideChar().
1016 */
1017 if (fio_flags == 0)
1018 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
Bram Moolenaar13505972019-01-24 15:04:48 +01001021#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001022 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (fio_flags == 0)
1024 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaar13505972019-01-24 15:04:48 +01001027#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 /*
1029 * Try using iconv() if we can't convert internally.
1030 */
1031 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001032# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 )
1036 iconv_fd = (iconv_t)my_iconv_open(
1037 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar13505972019-01-24 15:04:48 +01001040#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 /*
1042 * Use the 'charconvert' expression when conversion is required
1043 * and we can't do it internally or with iconv().
1044 */
1045 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001046 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001047# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 )
1051 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001052# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001054# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Skip conversion when it's already done (retry for wrong
1056 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (tmpname == NULL)
1058 {
1059 tmpname = readfile_charconvert(fname, fenc, &fd);
1060 if (tmpname == NULL)
1061 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001062 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 advance_fenc = TRUE;
1064 if (fd < 0)
1065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001066 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001067 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 error = TRUE;
1069 goto failed;
1070 }
1071 goto retry;
1072 }
1073 }
1074 }
1075 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
1078 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001079#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 )
1083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // Conversion wanted but we can't.
1085 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 advance_fenc = TRUE;
1087 goto retry;
1088 }
1089 }
1090 }
1091
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // Set "can_retry" when it's possible to rewind the file and try with
1093 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1094 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001095 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097 if (!skip_read)
1098 {
1099 linerest = 0;
1100 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001101 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 skip_count = lines_to_skip;
1103 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001105#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001106 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1107 && curbuf->b_ffname != NULL
1108 && curbuf->b_p_udf
1109 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001110 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001111 && !read_stdin
1112 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001113 if (read_undo_file)
1114 sha256_start(&sha_ctx);
1115#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001116#ifdef FEAT_CRYPT
1117 if (curbuf->b_cryptstate != NULL)
1118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 // Need to free the state, but keep the key, don't want to ask for
1120 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001121 crypt_free_state(curbuf->b_cryptstate);
1122 curbuf->b_cryptstate = NULL;
1123 }
1124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126
1127 while (!error && !got_int)
1128 {
1129 /*
1130 * We allocate as much space for the file as we can get, plus
1131 * space for the old line plus room for one terminating NUL.
1132 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001133 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001135 if (!skip_read)
1136 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001137#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001138 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001139#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // Use buffer >= 64K. Add linerest to double the size if the
1141 // line gets very long, to avoid a lot of copying. But don't
1142 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001143 size = 0x10000L + linerest;
1144 if (size > 0x100000L)
1145 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001146#endif
1147 }
1148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001149 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001150 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 size = 1;
1155 }
1156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 if (!skip_read)
1159 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001160 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001162 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 FALSE)) != NULL)
1164 break;
1165 }
1166 if (new_buffer == NULL)
1167 {
1168 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1169 error = TRUE;
1170 break;
1171 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001172 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1174 vim_free(buffer);
1175 buffer = new_buffer;
1176 ptr = buffer + linerest;
1177 line_start = buffer;
1178
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // May need room to translate into.
1180 // For iconv() we don't really know the required space, use a
1181 // factor ICONV_MULT.
1182 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1183 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1184 // become up to 4 bytes, size must be multiple of 2
1185 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1186 // multiple of 2
1187 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1188 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001189 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001190#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (iconv_fd != (iconv_t)-1)
1192 size = size / ICONV_MULT;
1193 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (fio_flags & FIO_LATIN1)
1196 size = size / 2;
1197 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1198 size = (size * 2 / 3) & ~1;
1199 else if (fio_flags & FIO_UCS4)
1200 size = (size * 2 / 3) & ~3;
1201 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001202 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001203#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001205 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001206#endif
1207#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001209 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210#endif
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (conv_restlen > 0)
1213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001214 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 mch_memmove(ptr, conv_rest, conv_restlen);
1216 ptr += conv_restlen;
1217 size -= conv_restlen;
1218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 if (read_buffer)
1221 {
1222 /*
1223 * Read bytes from curbuf. Used for converting text read
1224 * from stdin.
1225 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001226 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (read_buf_lnum > from)
1228 size = 0;
1229 else
1230 {
1231 int n, ni;
1232 long tlen;
1233
1234 tlen = 0;
1235 for (;;)
1236 {
1237 p = ml_get(read_buf_lnum) + read_buf_col;
1238 n = (int)STRLEN(p);
1239 if ((int)tlen + n + 1 > size)
1240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001241 // Filled up to "size", append partial line.
1242 // Change NL to NUL to reverse the effect done
1243 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001244 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 for (ni = 0; ni < n; ++ni)
1246 {
1247 if (p[ni] == NL)
1248 ptr[tlen++] = NUL;
1249 else
1250 ptr[tlen++] = p[ni];
1251 }
1252 read_buf_col += n;
1253 break;
1254 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001255
1256 // Append whole line and new-line. Change NL
1257 // to NUL to reverse the effect done below.
1258 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001260 if (p[ni] == NL)
1261 ptr[tlen++] = NUL;
1262 else
1263 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001265 ptr[tlen++] = NL;
1266 read_buf_col = 0;
1267 if (++read_buf_lnum > from)
1268 {
1269 // When the last line didn't have an
1270 // end-of-line don't add it now either.
1271 if (!curbuf->b_p_eol)
1272 --tlen;
1273 size = tlen;
1274 eof = TRUE;
1275 break;
1276 }
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 }
1281 else
1282 {
1283 /*
1284 * Read bytes from the file.
1285 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001286# ifdef FEAT_SODIUM
1287 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001288 //
1289 // Sodium encryption requires a fixed block size to
1290 // successfully decrypt. However, unfortunately the file
1291 // header size changes between xchacha20 and xchacha20v2 by
1292 // 'add_len' bytes.
1293 // So we will now read the maximum header size + encryption
1294 // metadata, but after determining to read an xchacha20
1295 // encrypted file, we have to rewind the file descriptor by
1296 // 'add_len' bytes in the second round.
1297 //
1298 // Be careful with changing it, it needs to stay the same
1299 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001300 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001301 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001302 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001303 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001304 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1305 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001306 may_need_lseek = TRUE;
1307 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001308
Christian Brabandtaae58342023-04-23 17:50:22 +01001309 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1310 && crypt_method_is_sodium(
1311 curbuf->b_cryptstate->method_nr)))
1312 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001313 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001314 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1315 // description above)
1316 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1317 && !eof && may_need_lseek)
1318 {
1319 lseek(fd, crypt_get_header_len(
1320 curbuf->b_cryptstate->method_nr)
1321 - crypt_get_max_header_len(), SEEK_CUR);
1322 may_need_lseek = FALSE;
1323 }
1324 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001325# endif
1326 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001327 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001328 filesize_count += size;
1329 // hit end of file
1330 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001333#ifdef FEAT_CRYPT
1334 /*
1335 * At start of file: Check for magic number of encryption.
1336 */
1337 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001338 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001339 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1340 &filesize, newfile, sfname,
1341 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001342# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001343 if (curbuf->b_cryptstate != NULL
1344 && !crypt_works_inplace(curbuf->b_cryptstate))
1345 // reading undo file requires crypt_decode_inplace()
1346 read_undo_file = FALSE;
1347# endif
1348 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001349 /*
1350 * Decrypt the read bytes. This is done before checking for
1351 * EOF because the crypt layer may be buffering.
1352 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001353 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1354 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001355 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001356# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001357 if (crypt_works_inplace(curbuf->b_cryptstate))
1358 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001359# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001360 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1361 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001362# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001363 }
1364 else
1365 {
1366 char_u *newptr = NULL;
1367 int decrypted_size;
1368
1369 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001370 curbuf->b_cryptstate, ptr, size,
1371 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001372
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001373 if (decrypted_size < 0)
1374 {
1375 // error message already given
1376 error = TRUE;
1377 vim_free(newptr);
1378 break;
1379 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001380 // If the crypt layer is buffering, not producing
1381 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001382 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001383 continue;
1384
1385 if (linerest == 0)
1386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001387 // Simple case: reuse returned buffer (may be
1388 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001389 new_buffer = newptr;
1390 }
1391 else
1392 {
1393 long_u new_size;
1394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001395 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001396 new_size = (long_u)(decrypted_size + linerest + 1);
1397 new_buffer = lalloc(new_size, FALSE);
1398 if (new_buffer == NULL)
1399 {
1400 do_outofmem_msg(new_size);
1401 error = TRUE;
1402 break;
1403 }
1404
1405 mch_memmove(new_buffer, buffer, linerest);
1406 if (newptr != NULL)
1407 mch_memmove(new_buffer + linerest, newptr,
1408 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001409 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001410 }
1411
1412 if (new_buffer != NULL)
1413 {
1414 vim_free(buffer);
1415 buffer = new_buffer;
1416 new_buffer = NULL;
1417 line_start = buffer;
1418 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001419 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001420 }
1421 size = decrypted_size;
1422 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001423# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001424 }
1425#endif
1426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (size <= 0)
1428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001432 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001433 /*
1434 * Reached end-of-file but some trailing bytes could
1435 * not be converted. Truncated file?
1436 */
1437
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001439 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001440#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001441 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001442#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001443 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001444 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001445 if (can_retry)
1446 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001447 if (conv_error == 0)
1448 conv_error = curbuf->b_ml.ml_line_count
1449 - linecnt + 1;
1450 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001451 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001452 else if (illegal_byte == 0)
1453 illegal_byte = curbuf->b_ml.ml_line_count
1454 - linecnt + 1;
1455 if (bad_char_behavior == BAD_DROP)
1456 {
1457 *(ptr - conv_restlen) = NUL;
1458 conv_restlen = 0;
1459 }
1460 else
1461 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001462 // Replace the trailing bytes with the replacement
1463 // character if we were converting; if we weren't,
1464 // leave the UTF8 checking code to do it, as it
1465 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001466 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001467#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001468 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001469#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001470 ))
1471 {
1472 while (conv_restlen > 0)
1473 {
1474 *(--ptr) = bad_char_behavior;
1475 --conv_restlen;
1476 }
1477 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001478 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001479#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001480 if (iconv_fd != (iconv_t)-1)
1481 {
1482 iconv_close(iconv_fd);
1483 iconv_fd = (iconv_t)-1;
1484 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001485#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001486 }
1487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 }
1490 skip_read = FALSE;
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 /*
1493 * At start of file (or after crypt magic number): Check for BOM.
1494 * Also check for a BOM for other Unicode encodings, but not after
1495 * converting with 'charconvert' or when a BOM has already been
1496 * found.
1497 */
1498 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001499#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001500 || (cryptkey != NULL
1501 && filesize == crypt_get_header_len(
1502 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 )
1505 && (fio_flags == FIO_UCSBOM
1506 || (!curbuf->b_p_bomb
1507 && tmpname == NULL
1508 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1509 {
1510 char_u *ccname;
1511 int blen;
1512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001513 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (size < 2 || curbuf->b_p_bin)
1515 ccname = NULL;
1516 else
1517 ccname = check_for_bom(ptr, size, &blen,
1518 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1519 if (ccname != NULL)
1520 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 filesize += blen;
1523 size -= blen;
1524 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001525 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001528 curbuf->b_start_bomb = TRUE;
1529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 }
1531
1532 if (fio_flags == FIO_UCSBOM)
1533 {
1534 if (ccname == NULL)
1535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001536 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 advance_fenc = TRUE;
1538 }
1539 else
1540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001541 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (fenc_alloced)
1543 vim_free(fenc);
1544 fenc = ccname;
1545 fenc_alloced = FALSE;
1546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001547 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 skip_read = TRUE;
1549 goto retry;
1550 }
1551 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001554 ptr -= conv_restlen;
1555 size += conv_restlen;
1556 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 /*
1558 * Break here for a read error or end-of-file.
1559 */
1560 if (size <= 0)
1561 break;
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar13505972019-01-24 15:04:48 +01001564#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (iconv_fd != (iconv_t)-1)
1566 {
1567 /*
1568 * Attempt conversion of the read bytes to 'encoding' using
1569 * iconv().
1570 */
1571 const char *fromp;
1572 char *top;
1573 size_t from_size;
1574 size_t to_size;
1575
1576 fromp = (char *)ptr;
1577 from_size = size;
1578 ptr += size;
1579 top = (char *)ptr;
1580 to_size = real_size - size;
1581
1582 /*
1583 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001584 * another conversion. Except for when there is no
1585 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001587 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1588 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1590 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001591 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001592 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001593 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001594 if (conv_error == 0)
1595 conv_error = readfile_linenr(linecnt,
1596 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001599 ++fromp;
1600 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001601 if (bad_char_behavior == BAD_KEEP)
1602 {
1603 *top++ = *(fromp - 1);
1604 --to_size;
1605 }
1606 else if (bad_char_behavior != BAD_DROP)
1607 {
1608 *top++ = bad_char_behavior;
1609 --to_size;
1610 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 if (from_size > 0)
1614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001615 // Some remaining characters, keep them for the next
1616 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1618 conv_restlen = (int)from_size;
1619 }
1620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001621 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 line_start = ptr - linerest;
1623 mch_memmove(line_start, buffer, (size_t)linerest);
1624 size = (long)((char_u *)top - ptr);
1625 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar4f974752019-02-17 17:44:42 +01001628#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (fio_flags & FIO_CODEPAGE)
1630 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001631 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001632 WCHAR ucs2buf[3];
1633 int ucs2len;
1634 int codepage = FIO_GET_CP(fio_flags);
1635 int bytelen;
1636 int found_bad;
1637 char replstr[2];
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 /*
1640 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001641 * a codepage, using standard MS-Windows functions. This
1642 * requires two steps:
1643 * 1. convert from 'fileencoding' to ucs-2
1644 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001646 * Because there may be illegal bytes AND an incomplete byte
1647 * sequence at the end, we may have to do the conversion one
1648 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001651 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001652 if (bad_char_behavior > 0)
1653 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001655 replstr[0] = '?';
1656 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001659 * Move the bytes to the end of the buffer, so that we have
1660 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001662 src = ptr + real_size - size;
1663 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001665 /*
1666 * Do the conversion.
1667 */
1668 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 while (size > 0)
1670 {
1671 found_bad = FALSE;
1672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001674 if (codepage == CP_UTF8)
1675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 // Handle CP_UTF8 input ourselves to be able to handle
1677 // trailing bytes properly.
1678 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001679 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001680 if (bytelen > size)
1681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 // Only got some bytes of a character. Normally
1683 // it's put in "conv_rest", but if it's too long
1684 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001685 if (bytelen <= CONV_RESTLEN)
1686 break;
1687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001688 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001689 bytelen = size;
1690 found_bad = TRUE;
1691 }
1692 else
1693 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001694 int u8c = utf_ptr2char(src);
1695
Bram Moolenaar86e01082005-12-29 22:45:34 +00001696 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001697 found_bad = TRUE;
1698 ucs2buf[0] = u8c;
1699 ucs2len = 1;
1700 }
1701 }
1702 else
1703# endif
1704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001705 // We don't know how long the byte sequence is, try
1706 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001707 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1708 ++bytelen)
1709 {
1710 ucs2len = MultiByteToWideChar(codepage,
1711 MB_ERR_INVALID_CHARS,
1712 (LPCSTR)src, bytelen,
1713 ucs2buf, 3);
1714 if (ucs2len > 0)
1715 break;
1716 }
1717 if (ucs2len == 0)
1718 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001719 // If we have only one byte then it's probably an
1720 // incomplete byte sequence. Otherwise discard
1721 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001722 if (size == 1)
1723 break;
1724 found_bad = TRUE;
1725 bytelen = 1;
1726 }
1727 }
1728
1729 if (!found_bad)
1730 {
1731 int i;
1732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001734 if (enc_utf8)
1735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001736 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001737 for (i = 0; i < ucs2len; ++i)
1738 dst += utf_char2bytes(ucs2buf[i], dst);
1739 }
1740 else
1741 {
1742 BOOL bad = FALSE;
1743 int dstlen;
1744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001745 // From UCS-2 to "enc_codepage". If the
1746 // conversion uses the default character "?",
1747 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001748 dstlen = WideCharToMultiByte(enc_codepage, 0,
1749 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001750 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001751 replstr, &bad);
1752 if (bad)
1753 found_bad = TRUE;
1754 else
1755 dst += dstlen;
1756 }
1757 }
1758
1759 if (found_bad)
1760 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001762 if (can_retry)
1763 goto rewind_retry;
1764 if (conv_error == 0)
1765 conv_error = readfile_linenr(linecnt, ptr, dst);
1766 if (bad_char_behavior != BAD_DROP)
1767 {
1768 if (bad_char_behavior == BAD_KEEP)
1769 {
1770 mch_memmove(dst, src, bytelen);
1771 dst += bytelen;
1772 }
1773 else
1774 *dst++ = bad_char_behavior;
1775 }
1776 }
1777
1778 src += bytelen;
1779 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001781
1782 if (size > 0)
1783 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001784 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001785 mch_memmove(conv_rest, src, size);
1786 conv_restlen = size;
1787 }
1788
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001789 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001790 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001793#endif
1794#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (fio_flags & FIO_MACROMAN)
1796 {
1797 /*
1798 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001799 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001801 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (fio_flags != 0)
1807 {
1808 int u8c;
1809 char_u *dest;
1810 char_u *tail = NULL;
1811
1812 /*
1813 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1814 * "enc_utf8" not set: Convert Unicode to Latin1.
1815 * Go from end to start through the buffer, because the number
1816 * of bytes may increase.
1817 * "dest" points to after where the UTF-8 bytes go, "p" points
1818 * to after the next character to convert.
1819 */
1820 dest = ptr + real_size;
1821 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1822 {
1823 p = ptr + size;
1824 if (fio_flags == FIO_UTF8)
1825 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001826 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 tail = ptr + size - 1;
1828 while (tail > ptr && (*tail & 0xc0) == 0x80)
1829 --tail;
1830 if (tail + utf_byte2len(*tail) <= ptr + size)
1831 tail = NULL;
1832 else
1833 p = tail;
1834 }
1835 }
1836 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001838 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 p = ptr + (size & ~1);
1840 if (size & 1)
1841 tail = p;
1842 if ((fio_flags & FIO_UTF16) && p > ptr)
1843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001844 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (fio_flags & FIO_ENDIAN_L)
1846 {
1847 u8c = (*--p << 8);
1848 u8c += *--p;
1849 }
1850 else
1851 {
1852 u8c = *--p;
1853 u8c += (*--p << 8);
1854 }
1855 if (u8c >= 0xd800 && u8c <= 0xdbff)
1856 tail = p;
1857 else
1858 p += 2;
1859 }
1860 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001861 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001863 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 p = ptr + (size & ~3);
1865 if (size & 3)
1866 tail = p;
1867 }
1868
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001869 // If there is a trailing incomplete sequence move it to
1870 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (tail != NULL)
1872 {
1873 conv_restlen = (int)((ptr + size) - tail);
1874 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1875 size -= conv_restlen;
1876 }
1877
1878
1879 while (p > ptr)
1880 {
1881 if (fio_flags & FIO_LATIN1)
1882 u8c = *--p;
1883 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1884 {
1885 if (fio_flags & FIO_ENDIAN_L)
1886 {
1887 u8c = (*--p << 8);
1888 u8c += *--p;
1889 }
1890 else
1891 {
1892 u8c = *--p;
1893 u8c += (*--p << 8);
1894 }
1895 if ((fio_flags & FIO_UTF16)
1896 && u8c >= 0xdc00 && u8c <= 0xdfff)
1897 {
1898 int u16c;
1899
1900 if (p == ptr)
1901 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001902 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (can_retry)
1904 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001905 if (conv_error == 0)
1906 conv_error = readfile_linenr(linecnt,
1907 ptr, p);
1908 if (bad_char_behavior == BAD_DROP)
1909 continue;
1910 if (bad_char_behavior != BAD_KEEP)
1911 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001914 // found second word of double-word, get the first
1915 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (fio_flags & FIO_ENDIAN_L)
1917 {
1918 u16c = (*--p << 8);
1919 u16c += *--p;
1920 }
1921 else
1922 {
1923 u16c = *--p;
1924 u16c += (*--p << 8);
1925 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001926 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1927 + (u8c & 0x3ff);
1928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001929 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (u16c < 0xd800 || u16c > 0xdbff)
1931 {
1932 if (can_retry)
1933 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001934 if (conv_error == 0)
1935 conv_error = readfile_linenr(linecnt,
1936 ptr, p);
1937 if (bad_char_behavior == BAD_DROP)
1938 continue;
1939 if (bad_char_behavior != BAD_KEEP)
1940 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 }
1944 else if (fio_flags & FIO_UCS4)
1945 {
1946 if (fio_flags & FIO_ENDIAN_L)
1947 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001948 u8c = (unsigned)*--p << 24;
1949 u8c += (unsigned)*--p << 16;
1950 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 u8c += *--p;
1952 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001956 u8c += (unsigned)*--p << 8;
1957 u8c += (unsigned)*--p << 16;
1958 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001961 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 if (*--p < 0x80)
1964 u8c = *p;
1965 else
1966 {
1967 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001968 p -= len;
1969 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (len == 0)
1971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // Not a valid UTF-8 character, retry with
1973 // another fenc when possible, otherwise just
1974 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (can_retry)
1976 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001977 if (conv_error == 0)
1978 conv_error = readfile_linenr(linecnt,
1979 ptr, p);
1980 if (bad_char_behavior == BAD_DROP)
1981 continue;
1982 if (bad_char_behavior != BAD_KEEP)
1983 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001987 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 dest -= utf_char2len(u8c);
1990 (void)utf_char2bytes(u8c, dest);
1991 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001992 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 --dest;
1995 if (u8c >= 0x100)
1996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // character doesn't fit in latin1, retry with
1998 // another fenc when possible, otherwise just
1999 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002000 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002002 if (conv_error == 0)
2003 conv_error = readfile_linenr(linecnt, ptr, p);
2004 if (bad_char_behavior == BAD_DROP)
2005 ++dest;
2006 else if (bad_char_behavior == BAD_KEEP)
2007 *dest = u8c;
2008 else if (eap != NULL && eap->bad_char != 0)
2009 *dest = bad_char_behavior;
2010 else
2011 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013 else
2014 *dest = u8c;
2015 }
2016 }
2017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 line_start = dest - linerest;
2020 mch_memmove(line_start, buffer, (size_t)linerest);
2021 size = (long)((ptr + real_size) - dest);
2022 ptr = dest;
2023 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002024 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002026 int incomplete_tail = FALSE;
2027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002029 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002031 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002032 int l;
2033
2034 if (todo <= 0)
2035 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (*p >= 0x80)
2037 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // A length of 1 means it's an illegal byte. Accept
2039 // an incomplete character at the end though, the next
2040 // read() will get the next bytes, we'll check it
2041 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002042 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002043 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002045 // Avoid retrying with a different encoding when
2046 // a truncated file is more likely, or attempting
2047 // to read the rest of an incomplete sequence when
2048 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002049 if (p > ptr || filesize > 0)
2050 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // Incomplete byte sequence, move it to conv_rest[]
2052 // and try to read the rest of it, unless we've
2053 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002054 if (p > ptr)
2055 {
2056 conv_restlen = todo;
2057 mch_memmove(conv_rest, p, conv_restlen);
2058 size -= conv_restlen;
2059 break;
2060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002062 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // Illegal byte. If we can try another encoding
2065 // do that, unless at EOF where a truncated
2066 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002067 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002068 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002069#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002071 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2072 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002073#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002075 if (conv_error == 0 && illegal_byte == 0)
2076 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002079 if (bad_char_behavior == BAD_DROP)
2080 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002081 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002082 --p;
2083 --size;
2084 }
2085 else if (bad_char_behavior != BAD_KEEP)
2086 *p = bad_char_behavior;
2087 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002088 else
2089 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002092 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002094 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002096 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002097#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002098 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002100 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002102#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002104 advance_fenc = TRUE;
2105 file_rewind = TRUE;
2106 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 filesize += size;
2112
2113 /*
2114 * when reading the first part of a file: guess EOL type
2115 */
2116 if (fileformat == EOL_UNKNOWN)
2117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (try_dos || try_unix)
2120 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002121 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002122 if (try_mac)
2123 try_mac = 1;
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 for (p = ptr; p < ptr + size; ++p)
2126 {
2127 if (*p == NL)
2128 {
2129 if (!try_unix
2130 || (try_dos && p > ptr && p[-1] == CAR))
2131 fileformat = EOL_DOS;
2132 else
2133 fileformat = EOL_UNIX;
2134 break;
2135 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002136 else if (*p == CAR && try_mac)
2137 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
2139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (fileformat == EOL_UNIX && try_mac)
2142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002143 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 try_mac = 1;
2145 try_unix = 1;
2146 for (; p >= ptr && *p != CAR; p--)
2147 ;
2148 if (p >= ptr)
2149 {
2150 for (p = ptr; p < ptr + size; ++p)
2151 {
2152 if (*p == NL)
2153 try_unix++;
2154 else if (*p == CAR)
2155 try_mac++;
2156 }
2157 if (try_mac > try_unix)
2158 fileformat = EOL_MAC;
2159 }
2160 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002161 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 // Looking for CR but found no end-of-line markers at
2163 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002164 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002167 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (fileformat == EOL_UNKNOWN && try_mac)
2169 fileformat = EOL_MAC;
2170
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002171 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (fileformat == EOL_UNKNOWN)
2173 fileformat = default_fileformat();
2174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002175 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 set_fileformat(fileformat, OPT_LOCAL);
2178 }
2179 }
2180
2181 /*
2182 * This loop is executed once for every character read.
2183 * Keep it fast!
2184 */
2185 if (fileformat == EOL_MAC)
2186 {
2187 --ptr;
2188 while (++ptr, --size >= 0)
2189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002190 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if ((c = *ptr) != NUL && c != CAR && c != NL)
2192 continue;
2193 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 else
2198 {
2199 if (skip_count == 0)
2200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 len = (colnr_T) (ptr - line_start + 1);
2203 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2204 {
2205 error = TRUE;
2206 break;
2207 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002208#ifdef FEAT_PERSISTENT_UNDO
2209 if (read_undo_file)
2210 sha256_update(&sha_ctx, line_start, len);
2211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ++lnum;
2213 if (--read_count == 0)
2214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002215 error = TRUE; // break loop
2216 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 break;
2218 }
2219 }
2220 else
2221 --skip_count;
2222 line_start = ptr + 1;
2223 }
2224 }
2225 }
2226 else
2227 {
2228 --ptr;
2229 while (++ptr, --size >= 0)
2230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002231 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 continue;
2233 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 else
2236 {
2237 if (skip_count == 0)
2238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 len = (colnr_T)(ptr - line_start + 1);
2241 if (fileformat == EOL_DOS)
2242 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002243 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 ptr[-1] = NUL;
2247 --len;
2248 }
2249 /*
2250 * Reading in Dos format, but no CR-LF found!
2251 * When 'fileformats' includes "unix", delete all
2252 * the lines read so far and start all over again.
2253 * Otherwise give an error message later.
2254 */
2255 else if (ff_error != EOL_DOS)
2256 {
2257 if ( try_unix
2258 && !read_stdin
2259 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002260 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2261 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
2263 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002264 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 set_fileformat(EOL_UNIX, OPT_LOCAL);
2266 file_rewind = TRUE;
2267 keep_fileformat = TRUE;
2268 goto retry;
2269 }
2270 ff_error = EOL_DOS;
2271 }
2272 }
2273 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2274 {
2275 error = TRUE;
2276 break;
2277 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002278#ifdef FEAT_PERSISTENT_UNDO
2279 if (read_undo_file)
2280 sha256_update(&sha_ctx, line_start, len);
2281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 ++lnum;
2283 if (--read_count == 0)
2284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002285 error = TRUE; // break loop
2286 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 break;
2288 }
2289 }
2290 else
2291 --skip_count;
2292 line_start = ptr + 1;
2293 }
2294 }
2295 }
2296 linerest = (long)(ptr - line_start);
2297 ui_breakcheck();
2298 }
2299
2300failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002301 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (error && read_count == 0)
2303 error = FALSE;
2304
K.Takata3af98212022-11-01 20:36:19 +00002305 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2306 // In old days the file length was in sector count and the CTRL-Z the
2307 // marker where the file really ended. Assuming we write it to a file
2308 // system that keeps file length properly the CTRL-Z should be dropped.
2309 // Set the 'endoffile' option so the user can decide what to write later.
2310 // In Unix format the CTRL-Z is just another character.
2311 if (linerest != 0
2312 && !curbuf->b_p_bin
2313 && fileformat == EOL_DOS
2314 && ptr[-1] == Ctrl_Z)
2315 {
2316 ptr--;
2317 linerest--;
2318 if (set_options)
2319 curbuf->b_p_eof = TRUE;
2320 }
2321
2322 // If we get EOF in the middle of a line, note the fact by resetting
2323 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (!error
2325 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002326 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002328 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002329 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 curbuf->b_p_eol = FALSE;
2331 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002332 len = (colnr_T)(ptr - line_start + 1);
2333 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 error = TRUE;
2335 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002336 {
2337#ifdef FEAT_PERSISTENT_UNDO
2338 if (read_undo_file)
2339 sha256_update(&sha_ctx, line_start, len);
2340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002345 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002349 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002350 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002351 crypt_free_state(curbuf->b_cryptstate);
2352 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002353 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002354 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2355 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 // Don't set cryptkey to NULL, it's used below as a flag that
2357 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#endif
2359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 // If editing a new file: set 'fenc' for the current buffer.
2361 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002362 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002364 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (fenc_alloced)
2366 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002367#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#endif
2371
2372 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002373 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002374#ifdef HAVE_FD_CLOEXEC
2375 else
2376 {
2377 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002378
Bram Moolenaarf05da212009-11-17 16:13:15 +00002379 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002380 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002381 }
2382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 vim_free(buffer);
2384
2385#ifdef HAVE_DUP
2386 if (read_stdin)
2387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002388 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002390 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392#endif
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (tmpname != NULL)
2395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002396 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 vim_free(tmpname);
2398 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002399 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 /*
2402 * In recovery mode everything but autocommands is skipped.
2403 */
2404 if (!recoverymode)
2405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2408 {
2409#ifdef FEAT_NETBEANS_INTG
2410 netbeansFireChanges = 0;
2411#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002412 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#ifdef FEAT_NETBEANS_INTG
2414 netbeansFireChanges = 1;
2415#endif
2416 --linecnt;
2417 }
2418 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2419 if (filesize == 0)
2420 linecnt = 0;
2421 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002422 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002423 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002424#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002425 // After reading the text into the buffer the diff info needs to
2426 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002427 diff_invalidate(curbuf);
2428#endif
2429#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 // All folds in the window are invalid now. Mark them for update
2431 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002432 foldUpdateAll(curwin);
2433#endif
2434 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002435 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 appended_lines_mark(from, linecnt);
2437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#ifndef ALWAYS_USE_GUI
2439 /*
2440 * If we were reading from the same terminal as where messages go,
2441 * the screen will have been messed up.
2442 * Switch on raw mode now and clear the screen.
2443 */
2444 if (read_stdin)
2445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002446 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 starttermcap();
2448 screenclear();
2449 }
2450#endif
2451
2452 if (got_int)
2453 {
2454 if (!(flags & READ_DUMMY))
2455 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002456 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002458 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460 msg_scroll = msg_save;
2461#ifdef FEAT_VIMINFO
2462 check_marks_read();
2463#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002464 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 if (!filtering && !(flags & READ_DUMMY))
2468 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002469 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 c = FALSE;
2471
2472#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002473 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 STRCAT(IObuff, _("[fifo]"));
2476 c = TRUE;
2477 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 STRCAT(IObuff, _("[socket]"));
2481 c = TRUE;
2482 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002483# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002484 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002485 {
2486 STRCAT(IObuff, _("[character special]"));
2487 c = TRUE;
2488 }
2489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490#endif
2491 if (curbuf->b_p_ro)
2492 {
2493 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2494 c = TRUE;
2495 }
2496 if (read_no_eol_lnum)
2497 {
2498 msg_add_eol();
2499 c = TRUE;
2500 }
2501 if (ff_error == EOL_DOS)
2502 {
2503 STRCAT(IObuff, _("[CR missing]"));
2504 c = TRUE;
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (split)
2507 {
2508 STRCAT(IObuff, _("[long lines split]"));
2509 c = TRUE;
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (notconverted)
2512 {
2513 STRCAT(IObuff, _("[NOT converted]"));
2514 c = TRUE;
2515 }
2516 else if (converted)
2517 {
2518 STRCAT(IObuff, _("[converted]"));
2519 c = TRUE;
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521#ifdef FEAT_CRYPT
2522 if (cryptkey != NULL)
2523 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002524 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 c = TRUE;
2526 }
2527#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002528 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002530 sprintf((char *)IObuff + STRLEN(IObuff),
2531 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 c = TRUE;
2533 }
2534 else if (illegal_byte > 0)
2535 {
2536 sprintf((char *)IObuff + STRLEN(IObuff),
2537 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2538 c = TRUE;
2539 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002540 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 STRCAT(IObuff, _("[READ ERRORS]"));
2543 c = TRUE;
2544 }
2545 if (msg_add_fileformat(fileformat))
2546 c = TRUE;
2547#ifdef FEAT_CRYPT
2548 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002549 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002550 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 else
2552#endif
2553 msg_add_lines(c, (long)linecnt, filesize);
2554
Bram Moolenaard23a8232018-02-10 18:45:26 +01002555 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 msg_scrolled_ign = TRUE;
2557#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002558 // Don't show the message when reading stdin, it would end up in a
2559 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (read_stdin || read_buffer)
2561 p = msg_may_trunc(FALSE, IObuff);
2562 else
2563#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002564 {
2565 if (msg_col > 0)
2566 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002567 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002570 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002571 // Need to repeat the message after redrawing when:
2572 // - When reading from stdin (the screen will be cleared next).
2573 // - When restart_edit is set (otherwise there will be a delay
2574 // before redrawing).
2575 // - When the screen was scrolled but there is no wait-return
2576 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002577 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 msg_scrolled_ign = FALSE;
2579 }
2580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002581 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002583 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002584 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 curbuf->b_p_ro = TRUE;
2586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002587 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 /*
2590 * In Ex mode: cursor at last new line.
2591 * Otherwise: cursor at first new line.
2592 */
2593 if (exmode_active)
2594 curwin->w_cursor.lnum = from + linecnt;
2595 else
2596 curwin->w_cursor.lnum = from + 1;
2597 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002598 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
Bram Moolenaare1004402020-10-24 20:49:43 +02002600 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002601 {
2602 // Set '[ and '] marks to the newly read lines.
2603 curbuf->b_op_start.lnum = from + 1;
2604 curbuf->b_op_start.col = 0;
2605 curbuf->b_op_end.lnum = from + linecnt;
2606 curbuf->b_op_end.col = 0;
2607 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002608
Bram Moolenaar4f974752019-02-17 17:44:42 +01002609#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002610 /*
2611 * Work around a weird problem: When a file has two links (only
2612 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002613 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002614 * It's correct again after reading the file, thus reset the timestamp
2615 * here.
2616 */
2617 if (newfile && !read_stdin && !read_buffer
2618 && mch_stat((char *)fname, &st) >= 0)
2619 {
2620 buf_store_time(curbuf, &st, fname);
2621 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002622 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002623 }
2624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
2626 msg_scroll = msg_save;
2627
2628#ifdef FEAT_VIMINFO
2629 /*
2630 * Get the marks before executing autocommands, so they can be used there.
2631 */
2632 check_marks_read();
2633#endif
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002636 * We remember if the last line of the read didn't have
2637 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2638 * or writing the read again with 'binary' on. The latter is required
2639 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002641 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002643 // When reloading a buffer put the cursor at the first line that is
2644 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002645 if (flags & READ_KEEP_UNDO)
2646 u_find_first_changed();
2647
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002648#ifdef FEAT_PERSISTENT_UNDO
2649 /*
2650 * When opening a new file locate undo info and read it.
2651 */
2652 if (read_undo_file)
2653 {
2654 char_u hash[UNDO_HASH_SIZE];
2655
2656 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002657 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002658 }
2659#endif
2660
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002661 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 int m = msg_scroll;
2664 int n = msg_scrolled;
2665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002666 // Save the fileformat now, otherwise the buffer will be considered
2667 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002668 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 save_file_ff(curbuf);
2670
2671 /*
2672 * The output from the autocommands should not overwrite anything and
2673 * should not be overwritten: Set msg_scroll, restore its value if no
2674 * output was done.
2675 */
2676 msg_scroll = TRUE;
2677 if (filtering)
2678 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2679 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002680 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2683 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002684 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2685 /*
2686 * EVENT_FILETYPE was not triggered but the buffer already has a
2687 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2688 */
2689 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2690 TRUE, curbuf);
2691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 else
2693 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2694 FALSE, NULL, eap);
2695 if (msg_scrolled == n)
2696 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002697# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002698 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 if (recoverymode && error)
2704 return FAIL;
2705 return OK;
2706}
2707
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002708#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002709/*
2710 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2711 * which is the name of files used for process substitution output by
2712 * some shells on some operating systems, e.g., bash on SunOS.
2713 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2714 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002715 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002716is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002717{
2718 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2719 && VIM_ISDIGIT(fname[8])
2720 && *skipdigits(fname + 9) == NUL
2721 && (fname[9] != NUL
2722 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2723}
2724#endif
2725
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002726/*
2727 * From the current line count and characters read after that, estimate the
2728 * line number where we are now.
2729 * Used for error messages that include a line number.
2730 */
2731 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002732readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002733 linenr_T linecnt, // line count before reading more bytes
2734 char_u *p, // start of more bytes read
2735 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002736{
2737 char_u *s;
2738 linenr_T lnum;
2739
2740 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2741 for (s = p; s < endp; ++s)
2742 if (*s == '\n')
2743 ++lnum;
2744 return lnum;
2745}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747/*
Rob Pilling8196e942022-02-11 15:12:10 +00002748 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002749 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 * Returns OK or FAIL.
2751 */
2752 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002753prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
Bram Moolenaar13505972019-01-24 15:04:48 +01002755 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if (eap->cmd == NULL)
2757 return FAIL;
2758
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002759 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2760 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002761 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002762 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002763
2764 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002765 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002766 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 return OK;
2768}
2769
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002770/*
2771 * Set default or forced 'fileformat' and 'binary'.
2772 */
2773 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002774set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002775{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002776 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002777 if (set_options)
2778 {
2779 if (eap != NULL && eap->force_ff != 0)
2780 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2781 else if (*p_ffs != NUL)
2782 set_fileformat(default_fileformat(), OPT_LOCAL);
2783 }
2784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002785 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002786 if (eap != NULL && eap->force_bin != 0)
2787 {
2788 int oldval = curbuf->b_p_bin;
2789
2790 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2791 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2792 }
2793}
2794
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002795/*
2796 * Set forced 'fileencoding'.
2797 */
2798 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002799set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002800{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002801 if (eap->force_enc == 0)
2802 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002803
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002804 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2805
2806 if (fenc != NULL)
2807 set_string_option_direct((char_u *)"fenc", -1,
2808 fenc, OPT_FREE|OPT_LOCAL, 0);
2809 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002810}
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812/*
2813 * Find next fileencoding to use from 'fileencodings'.
2814 * "pp" points to fenc_next. It's advanced to the next item.
2815 * When there are no more items, an empty string is returned and *pp is set to
2816 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002817 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2818 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 */
2820 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002821next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
2823 char_u *p;
2824 char_u *r;
2825
Bram Moolenaarf077db22019-08-13 00:18:24 +02002826 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (**pp == NUL)
2828 {
2829 *pp = NULL;
2830 return (char_u *)"";
2831 }
2832 p = vim_strchr(*pp, ',');
2833 if (p == NULL)
2834 {
2835 r = enc_canonize(*pp);
2836 *pp += STRLEN(*pp);
2837 }
2838 else
2839 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002840 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 *pp = p + 1;
2842 if (r != NULL)
2843 {
2844 p = enc_canonize(r);
2845 vim_free(r);
2846 r = p;
2847 }
2848 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002849 if (r != NULL)
2850 *alloced = TRUE;
2851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002853 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 r = (char_u *)"";
2855 *pp = NULL;
2856 }
2857 return r;
2858}
2859
Bram Moolenaar13505972019-01-24 15:04:48 +01002860#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861/*
2862 * Convert a file with the 'charconvert' expression.
2863 * This closes the file which is to be read, converts it and opens the
2864 * resulting file for reading.
2865 * Returns name of the resulting converted file (the caller should delete it
2866 * after reading it).
2867 * Returns NULL if the conversion failed ("*fdp" is not set) .
2868 */
2869 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002870readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002871 char_u *fname, // name of input file
2872 char_u *fenc, // converted from
2873 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002876 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002878 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002880 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 else
2882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002883 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *fdp = -1;
2885 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2886 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002887 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2889 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002890 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892
2893 if (errmsg != NULL)
2894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002895 // Don't use emsg(), it breaks mappings, the retry with
2896 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002897 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 if (tmpname != NULL)
2899 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002900 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002901 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903 }
2904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002905 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 if (*fdp < 0)
2907 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2908
2909 return tmpname;
2910}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#endif
2912
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002913#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002915 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2917 * *filesizep are updated.
2918 * Return the (new) encryption key, NULL for no encryption.
2919 */
2920 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002921check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002922 char_u *cryptkey, // previous encryption key or NULL
2923 char_u *ptr, // pointer to read bytes
2924 long *sizep, // length of read bytes
2925 off_T *filesizep, // nr of bytes used from file
2926 int newfile, // editing a new buffer
2927 char_u *fname, // file name to display
2928 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002930 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002931 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002932
2933 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002935 // Mark the buffer as read-only until the decryption has taken place.
2936 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002937 curbuf->b_p_ro = TRUE;
2938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002939 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002940 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002941 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
2943 if (*curbuf->b_p_key)
2944 cryptkey = curbuf->b_p_key;
2945 else
2946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002947 // When newfile is TRUE, store the typed key in the 'key'
2948 // option and don't free it. bf needs hash of the key saved.
2949 // Don't ask for the key again when first time Enter was hit.
2950 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002951 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002952 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002953 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002954 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002955 *did_ask = TRUE;
2956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002957 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 if (cryptkey != NULL && *cryptkey == NUL)
2959 {
2960 if (cryptkey != curbuf->b_p_key)
2961 vim_free(cryptkey);
2962 cryptkey = NULL;
2963 }
2964 }
2965 }
2966
2967 if (cryptkey != NULL)
2968 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002969 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002970
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002971 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002972 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002973 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002974 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002975
2976 curbuf->b_cryptstate = crypt_create_from_header(
2977 method, cryptkey, ptr);
2978 crypt_set_cm_option(curbuf, method);
2979
2980 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002981 *filesizep += header_len;
2982 *sizep -= header_len;
2983 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002985 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002986 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
2988 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002989 // When starting to edit a new file which does not have encryption, clear
2990 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002991 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01002992 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 return cryptkey;
2995}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002996#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002999 * Return TRUE if a file appears to be read-only from the file permissions.
3000 */
3001 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003002check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003003 char_u *fname, // full path to file
3004 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003005{
3006#ifndef USE_MCH_ACCESS
3007 int fd = 0;
3008#endif
3009
3010 return (
3011#ifdef USE_MCH_ACCESS
3012# ifdef UNIX
3013 (perm & 0222) == 0 ||
3014# endif
3015 mch_access((char *)fname, W_OK)
3016#else
3017 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3018 ? TRUE : (close(fd), FALSE)
3019#endif
3020 );
3021}
3022
Bram Moolenaara7870192019-02-14 12:56:36 +01003023#if defined(HAVE_FSYNC) || defined(PROTO)
3024/*
3025 * Call fsync() with Mac-specific exception.
3026 * Return fsync() result: zero for success.
3027 */
3028 int
3029vim_fsync(int fd)
3030{
3031 int r;
3032
3033# ifdef MACOS_X
3034 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003035 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003036# endif
3037 r = fsync(fd);
3038 return r;
3039}
3040#endif
3041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003043 * Set the name of the current buffer. Use when the buffer doesn't have a
3044 * name and a ":r" or ":w" command with a file name is used.
3045 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003047set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003048{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003049 buf_T *buf = curbuf;
3050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003051 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003052 if (curbuf->b_p_bl)
3053 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3054 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003055#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003056 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003057 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003058#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003059 if (curbuf != buf)
3060 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003061 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003062 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003063 return FAIL;
3064 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003065
3066 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3067 curbuf->b_flags |= BF_NOTEDITED;
3068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003069 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003070 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3071 if (curbuf->b_p_bl)
3072 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003073#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003074 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003075 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003076#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003078 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003079 if (*curbuf->b_p_ft == NUL)
3080 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003081 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003082 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003083 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003084 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003085
3086 return OK;
3087}
3088
3089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 * Put file name into IObuff with quotes.
3091 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003093msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 if (fname == NULL)
3096 fname = (char_u *)"-stdin-";
3097 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3098 IObuff[0] = '"';
3099 STRCAT(IObuff, "\" ");
3100}
3101
3102/*
3103 * Append message for text mode to IObuff.
3104 * Return TRUE if something appended.
3105 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003106 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109#ifndef USE_CRNL
3110 if (eol_type == EOL_DOS)
3111 {
3112 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3113 return TRUE;
3114 }
3115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (eol_type == EOL_MAC)
3117 {
3118 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3119 return TRUE;
3120 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003121#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 if (eol_type == EOL_UNIX)
3123 {
3124 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3125 return TRUE;
3126 }
3127#endif
3128 return FALSE;
3129}
3130
3131/*
3132 * Append line and character count to IObuff.
3133 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003135msg_add_lines(
3136 int insert_space,
3137 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003138 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 char_u *p;
3141
3142 p = IObuff + STRLEN(IObuff);
3143
3144 if (insert_space)
3145 *p++ = ' ';
3146 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003147 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003148 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 else
3150 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003151 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003153 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003154 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003155 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157}
3158
3159/*
3160 * Append message for missing line separator to IObuff.
3161 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003162 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003163msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3166}
3167
Bram Moolenaar473952e2019-09-28 16:30:04 +02003168 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003169time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
ichizokdef69df2021-10-15 17:23:12 +01003171 return
3172#ifdef ST_MTIM_NSEC
3173 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#endif
ichizokdef69df2021-10-15 17:23:12 +01003175#if defined(__linux__) || defined(MSWIN)
3176 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3177 // the seconds. Since the roundoff is done when flushing the inode, the
3178 // time may change unexpectedly by one second!!!
3179 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3180#else
3181 (long)st->st_mtime != mtime
3182#endif
3183 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184}
3185
3186/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003187 * Return TRUE if file encoding "fenc" requires conversion from or to
3188 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003190 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003191need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003193 int same_encoding;
3194 int enc_flags;
3195 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003197 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003198 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003199 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003200 fenc_flags = 0;
3201 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003202 else
3203 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003204 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3205 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003206 enc_flags = get_fio_flags(p_enc);
3207 fenc_flags = get_fio_flags(fenc);
3208 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3209 }
3210 if (same_encoding)
3211 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003212 // Specified encoding matches with 'encoding'. This requires
3213 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003214 return enc_unicode != 0;
3215 }
3216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003217 // Encodings differ. However, conversion is not needed when 'enc' is any
3218 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003219 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
3222/*
3223 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3224 * internal conversion.
3225 * if "ptr" is an empty string, use 'encoding'.
3226 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003227 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003228get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 int prop;
3231
3232 if (*ptr == NUL)
3233 ptr = p_enc;
3234
3235 prop = enc_canon_props(ptr);
3236 if (prop & ENC_UNICODE)
3237 {
3238 if (prop & ENC_2BYTE)
3239 {
3240 if (prop & ENC_ENDIAN_L)
3241 return FIO_UCS2 | FIO_ENDIAN_L;
3242 return FIO_UCS2;
3243 }
3244 if (prop & ENC_4BYTE)
3245 {
3246 if (prop & ENC_ENDIAN_L)
3247 return FIO_UCS4 | FIO_ENDIAN_L;
3248 return FIO_UCS4;
3249 }
3250 if (prop & ENC_2WORD)
3251 {
3252 if (prop & ENC_ENDIAN_L)
3253 return FIO_UTF16 | FIO_ENDIAN_L;
3254 return FIO_UTF16;
3255 }
3256 return FIO_UTF8;
3257 }
3258 if (prop & ENC_LATIN1)
3259 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003260 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 return 0;
3262}
3263
Bram Moolenaar473952e2019-09-28 16:30:04 +02003264#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3267 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3268 * Used for conversion between 'encoding' and 'fileencoding'.
3269 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003270 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 int cp;
3274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003275 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (!enc_utf8 && enc_codepage <= 0)
3277 return 0;
3278
3279 cp = encname2codepage(ptr);
3280 if (cp == 0)
3281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003282# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (STRCMP(ptr, "utf-8") == 0)
3284 cp = CP_UTF8;
3285 else
3286# endif
3287 return 0;
3288 }
3289 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3290}
3291#endif
3292
Bram Moolenaar473952e2019-09-28 16:30:04 +02003293#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294/*
3295 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3296 * needed for the internal conversion to/from utf-8 or latin1.
3297 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003298 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003299get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3302 && (enc_canon_props(ptr) & ENC_MACROMAN))
3303 return FIO_MACROMAN;
3304 return 0;
3305}
3306#endif
3307
3308/*
3309 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3310 * "size" must be at least 2.
3311 * Return the name of the encoding and set "*lenp" to the length.
3312 * Returns NULL when no BOM found.
3313 */
3314 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003315check_for_bom(
3316 char_u *p,
3317 long size,
3318 int *lenp,
3319 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 char *name = NULL;
3322 int len = 2;
3323
3324 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003325 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003327 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 len = 3;
3329 }
3330 else if (p[0] == 0xff && p[1] == 0xfe)
3331 {
3332 if (size >= 4 && p[2] == 0 && p[3] == 0
3333 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003335 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 len = 4;
3337 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003338 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003339 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003340 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003341 // utf-16le is preferred, it also works for ucs-2le text
3342 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 else if (p[0] == 0xfe && p[1] == 0xff
3345 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3346 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003347 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003348 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003349 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003350 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003351 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3354 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003356 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 len = 4;
3358 }
3359
3360 *lenp = len;
3361 return (char_u *)name;
3362}
3363
3364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 * Try to find a shortname by comparing the fullname with the current
3366 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003367 * Returns "full_path" or pointer into "full_path" if shortened.
3368 */
3369 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003370shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003371{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003372 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003373 char_u *p = full_path;
3374
Bram Moolenaard9462e32011-04-11 21:35:11 +02003375 dirname = alloc(MAXPATHL);
3376 if (dirname == NULL)
3377 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003378 if (mch_dirname(dirname, MAXPATHL) == OK)
3379 {
3380 p = shorten_fname(full_path, dirname);
3381 if (p == NULL || *p == NUL)
3382 p = full_path;
3383 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003384 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003385 return p;
3386}
3387
3388/*
3389 * Try to find a shortname by comparing the fullname with the current
3390 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 * Returns NULL if not shorter name possible, pointer into "full_path"
3392 * otherwise.
3393 */
3394 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003395shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 int len;
3398 char_u *p;
3399
3400 if (full_path == NULL)
3401 return NULL;
3402 len = (int)STRLEN(dir_name);
3403 if (fnamencmp(dir_name, full_path, len) == 0)
3404 {
3405 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003406#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003408 * MS-Windows: when a file is in the root directory, dir_name will end
3409 * in a slash, since C: by itself does not define a specific dir. In
3410 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
3412 if (!((len > 2) && (*(p - 2) == ':')))
3413#endif
3414 {
3415 if (vim_ispathsep(*p))
3416 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003417#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else
3419 p = NULL;
3420#endif
3421 }
3422 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003423#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 /*
3425 * When using a file in the current drive, remove the drive name:
3426 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3427 * a floppy from "A:\dir" to "B:\dir".
3428 */
3429 else if (len > 3
3430 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3431 && full_path[1] == ':'
3432 && vim_ispathsep(full_path[2]))
3433 p = full_path + 2;
3434#endif
3435 else
3436 p = NULL;
3437 return p;
3438}
3439
3440/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003441 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 * When "force" is TRUE: Use full path from now on for files currently being
3443 * edited, both for file name and swap file name. Try to shorten the file
3444 * names a bit, if safe to do so.
3445 * When "force" is FALSE: Only try to shorten absolute file names.
3446 * For buffers that have buftype "nofile" or "scratch": never change the file
3447 * name.
3448 */
3449 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003450shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3451{
3452 char_u *p;
3453
3454 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003455 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003456 && !path_with_url(buf->b_fname)
3457 && (force
3458 || buf->b_sfname == NULL
3459 || mch_isFullName(buf->b_sfname)))
3460 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003461 if (buf->b_sfname != buf->b_ffname)
3462 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003463 p = shorten_fname(buf->b_ffname, dirname);
3464 if (p != NULL)
3465 {
3466 buf->b_sfname = vim_strsave(p);
3467 buf->b_fname = buf->b_sfname;
3468 }
3469 if (p == NULL || buf->b_fname == NULL)
3470 buf->b_fname = buf->b_ffname;
3471 }
3472}
3473
3474/*
3475 * Shorten filenames for all buffers.
3476 */
3477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003478shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 char_u dirname[MAXPATHL];
3481 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003484 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003486 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003488 // Always make the swap file name a full path, a "nofile" buffer may
3489 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003490 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003493 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003494#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003495 popup_update_preview_title();
3496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497}
3498
3499#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3500 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003501 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 || defined(PROTO)
3503/*
3504 * Shorten all filenames in "fnames[count]" by current directory.
3505 */
3506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
3509 int i;
3510 char_u dirname[MAXPATHL];
3511 char_u *p;
3512
3513 if (fnames == NULL || count < 1)
3514 return;
3515 mch_dirname(dirname, sizeof(dirname));
3516 for (i = 0; i < count; ++i)
3517 {
3518 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3519 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003520 // shorten_fname() returns pointer in given "fnames[i]". If free
3521 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3522 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 p = vim_strsave(p);
3524 vim_free(fnames[i]);
3525 fnames[i] = p;
3526 }
3527 }
3528}
3529#endif
3530
3531/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003532 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * fo_o_h.ext for MSDOS or when shortname option set.
3534 *
3535 * Assumed that fname is a valid name found in the filesystem we assure that
3536 * the return value is a different name and ends in 'ext'.
3537 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3538 * characters otherwise.
3539 * Space for the returned name is allocated, must be freed later.
3540 * Returns NULL when out of memory.
3541 */
3542 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003543modname(
3544 char_u *fname,
3545 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003546 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003548 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 fname, ext, prepend_dot);
3550}
3551
3552 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003553buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003554 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003555 char_u *fname,
3556 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003557 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 char_u *retval;
3560 char_u *s;
3561 char_u *e;
3562 char_u *ptr;
3563 int fnamelen, extlen;
3564
3565 extlen = (int)STRLEN(ext);
3566
3567 /*
3568 * If there is no file name we must get the name of the current directory
3569 * (we need the full path in case :cd is used).
3570 */
3571 if (fname == NULL || *fname == NUL)
3572 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003573 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (retval == NULL)
3575 return NULL;
3576 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3577 (fnamelen = (int)STRLEN(retval)) == 0)
3578 {
3579 vim_free(retval);
3580 return NULL;
3581 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003582 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 retval[fnamelen++] = PATHSEP;
3585 retval[fnamelen] = NUL;
3586 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003587 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589 else
3590 {
3591 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003592 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (retval == NULL)
3594 return NULL;
3595 STRCPY(retval, fname);
3596#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003597 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598#endif
3599 }
3600
3601 /*
3602 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3603 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3604 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3605 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3606 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003607 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003609 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003610 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003613 {
3614 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003619 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3621 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 s = ptr + STRLEN(ptr);
3624
3625 /*
3626 * For 8.3 file names we may have to reduce the length.
3627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 /*
3631 * If there is no file name, or the file name ends in '/', and the
3632 * extension starts with '.', put a '_' before the dot, because just
3633 * ".ext" is invalid.
3634 */
3635 if (fname == NULL || *fname == NUL
3636 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 *s++ = '_';
3640 }
3641 /*
3642 * If the extension starts with '.', truncate the base name at 8
3643 * characters
3644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003647 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
3649 s = ptr + 8;
3650 *s = '\0';
3651 }
3652 }
3653 /*
3654 * If the extension doesn't start with '.', and the file name
3655 * doesn't have an extension yet, append a '.'
3656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 else if ((e = vim_strchr(ptr, '.')) == NULL)
3658 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 /*
3660 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003661 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 */
3663 else if ((int)STRLEN(e) + extlen > 4)
3664 s = e + 4 - extlen;
3665 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003666#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 /*
3668 * If there is no file name, and the extension starts with '.', put a
3669 * '_' before the dot, because just ".ext" may be invalid if it's on a
3670 * FAT partition, and on HPFS it doesn't matter.
3671 */
3672 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3673 *s++ = '_';
3674#endif
3675
3676 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003677 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 * ext can start with '.' and cannot exceed 3 more characters.
3679 */
3680 STRCPY(s, ext);
3681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 /*
3683 * Prepend the dot.
3684 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003685 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003687 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 /*
3692 * Check that, after appending the extension, the file name is really
3693 * different.
3694 */
3695 if (fname != NULL && STRCMP(fname, retval) == 0)
3696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 while (--s >= ptr)
3699 {
3700 if (*s != '_')
3701 {
3702 *s = '_';
3703 break;
3704 }
3705 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003706 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 *ptr = 'v';
3708 }
3709 return retval;
3710}
3711
3712/*
3713 * Like fgets(), but if the file line is too long, it is truncated and the
3714 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003715 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
3717 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003718vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
3720 char *eof;
3721#define FGETS_SIZE 200
3722 char tbuf[FGETS_SIZE];
3723
3724 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3727 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003728 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003730 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 do
3732 {
3733 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003734 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3736 }
3737 return (eof == NULL);
3738}
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740/*
3741 * rename() only works if both files are on the same file system, this
3742 * function will (attempts to?) copy the file across if rename fails -- webb
3743 * Return -1 for failure, 0 for success.
3744 */
3745 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003746vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
3748 int fd_in;
3749 int fd_out;
3750 int n;
3751 char *errmsg = NULL;
3752 char *buffer;
3753#ifdef AMIGA
3754 BPTR flock;
3755#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003756 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003757 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003758#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003759 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003760#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003761 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003764 * When the names are identical, there is nothing to do. When they refer
3765 * to the same file (ignoring case and slash/backslash differences) but
3766 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 */
3768 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003769 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003770 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003771 use_tmp_file = TRUE;
3772 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003773 return 0;
3774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 /*
3777 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3778 */
3779 if (mch_stat((char *)from, &st) < 0)
3780 return -1;
3781
Bram Moolenaar3576da72008-12-30 15:15:57 +00003782#ifdef UNIX
3783 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003784 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003785
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003786 // It's possible for the source and destination to be the same file.
3787 // This happens when "from" and "to" differ in case and are on a FAT32
3788 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003789 if (mch_stat((char *)to, &st_to) >= 0
3790 && st.st_dev == st_to.st_dev
3791 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003792 use_tmp_file = TRUE;
3793 }
3794#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003795#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003796 {
3797 BY_HANDLE_FILE_INFORMATION info1, info2;
3798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003799 // It's possible for the source and destination to be the same file.
3800 // In that case go through a temp file name. This makes rename("foo",
3801 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003802 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3803 && win32_fileinfo(to, &info2) == FILEINFO_OK
3804 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3805 && info1.nFileIndexHigh == info2.nFileIndexHigh
3806 && info1.nFileIndexLow == info2.nFileIndexLow)
3807 use_tmp_file = TRUE;
3808 }
3809#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003810
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003811 if (use_tmp_file)
3812 {
3813 char tempname[MAXPATHL + 1];
3814
3815 /*
3816 * Find a name that doesn't exist and is in the same directory.
3817 * Rename "from" to "tempname" and then rename "tempname" to "to".
3818 */
3819 if (STRLEN(from) >= MAXPATHL - 5)
3820 return -1;
3821 STRCPY(tempname, from);
3822 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003823 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003824 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3825 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003826 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003827 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003828 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003829 if (mch_rename(tempname, (char *)to) == 0)
3830 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 // Strange, the second step failed. Try moving the
3832 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003833 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003834 return -1;
3835 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003836 // If it fails for one temp name it will most likely fail
3837 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003838 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003839 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003840 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003841 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003842 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 /*
3845 * Delete the "to" file, this is required on some systems to make the
3846 * mch_rename() work, on other systems it makes sure that we don't have
3847 * two files when the mch_rename() fails.
3848 */
3849
3850#ifdef AMIGA
3851 /*
3852 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3853 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003854 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 * deleting the "from" file (horror!) we lock it during the remove.
3856 *
3857 * When used for making a backup before writing the file: This should not
3858 * happen with ":w", because startscript() should detect this problem and
3859 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3860 * name. This problem does exist with ":w filename", but then the
3861 * original file will be somewhere else so the backup isn't really
3862 * important. If autoscripting is off the rename may fail.
3863 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003864 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865#endif
3866 mch_remove(to);
3867#ifdef AMIGA
3868 if (flock)
3869 UnLock(flock);
3870#endif
3871
3872 /*
3873 * First try a normal rename, return if it works.
3874 */
3875 if (mch_rename((char *)from, (char *)to) == 0)
3876 return 0;
3877
3878 /*
3879 * Rename() failed, try copying the file.
3880 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003881 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003882#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003884 acl = mch_get_acl(from);
3885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3887 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003888 {
3889#ifdef HAVE_ACL
3890 mch_free_acl(acl);
3891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003893 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003895 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003896 fd_out = mch_open((char *)to,
3897 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (fd_out == -1)
3899 {
3900 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003901#ifdef HAVE_ACL
3902 mch_free_acl(acl);
3903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 return -1;
3905 }
3906
Bram Moolenaar473952e2019-09-28 16:30:04 +02003907 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (buffer == NULL)
3909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003911 close(fd_in);
3912#ifdef HAVE_ACL
3913 mch_free_acl(acl);
3914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return -1;
3916 }
3917
Bram Moolenaar473952e2019-09-28 16:30:04 +02003918 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003919 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003921 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923 }
3924
3925 vim_free(buffer);
3926 close(fd_in);
3927 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003928 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (n < 0)
3930 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003931 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 to = from;
3933 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003934#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003935 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003936#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003937#ifdef HAVE_ACL
3938 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003939 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003940#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003941#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003942 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (errmsg != NULL)
3945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003946 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 return -1;
3948 }
3949 mch_remove(from);
3950 return 0;
3951}
3952
3953static int already_warned = FALSE;
3954
3955/*
3956 * Check if any not hidden buffer has been changed.
3957 * Postpone the check if there are characters in the stuff buffer, a global
3958 * command is being executed, a mapping is being executed or an autocommand is
3959 * busy.
3960 * Returns TRUE if some message was written (screen should be redrawn and
3961 * cursor positioned).
3962 */
3963 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003964check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003965 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 buf_T *buf;
3968 int didit = 0;
3969 int n;
3970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003971 // Don't check timestamps while system() or another low-level function may
3972 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (no_check_timestamps > 0)
3974 return FALSE;
3975
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003976 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3977 // event and we would keep on checking if the file is steadily growing.
3978 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (focus && did_check_timestamps)
3980 {
3981 need_check_timestamps = TRUE;
3982 return FALSE;
3983 }
3984
3985 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003986 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003987 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 else
3989 {
3990 ++no_wait_return;
3991 did_check_timestamps = TRUE;
3992 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003993 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003995 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (buf->b_nwindows > 0)
3997 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003998 bufref_T bufref;
3999
4000 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 n = buf_check_timestamp(buf, focus);
4002 if (didit < n)
4003 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004004 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004006 // Autocommands have removed the buffer, start at the
4007 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 buf = firstbuf;
4009 continue;
4010 }
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 --no_wait_return;
4014 need_check_timestamps = FALSE;
4015 if (need_wait_return && didit == 2)
4016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004017 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004018 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 out_flush();
4020 }
4021 }
4022 return didit;
4023}
4024
4025/*
4026 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4027 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4028 * empty.
4029 */
4030 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004031move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 buf_T *tbuf = curbuf;
4034 int retval = OK;
4035 linenr_T lnum;
4036 char_u *p;
4037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004038 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 curbuf = tobuf;
4040 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4041 {
4042 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4043 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4044 {
4045 vim_free(p);
4046 retval = FAIL;
4047 break;
4048 }
4049 vim_free(p);
4050 }
4051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004052 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (retval != FAIL)
4054 {
4055 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004056 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004057 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004059 // Oops! We could try putting back the saved lines, but that
4060 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 retval = FAIL;
4062 break;
4063 }
4064 }
4065
4066 curbuf = tbuf;
4067 return retval;
4068}
4069
4070/*
4071 * Check if buffer "buf" has been changed.
4072 * Also check if the file for a new buffer unexpectedly appeared.
4073 * return 1 if a changed buffer was found.
4074 * return 2 if a message has been displayed.
4075 * return 0 otherwise.
4076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004078buf_check_timestamp(
4079 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004080 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004082 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int stat_res;
4084 int retval = 0;
4085 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004086 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004088 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004090 enum {
4091 RELOAD_NONE,
4092 RELOAD_NORMAL,
4093 RELOAD_DETECT
4094 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004095 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4097 int can_reload = FALSE;
4098#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004099 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 int orig_mode = buf->b_orig_mode;
4101#ifdef FEAT_GUI
4102 int save_mouse_correct = need_mouse_correct;
4103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004105 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004106#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004107 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004108#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004109 bufref_T bufref;
4110
4111 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004113 // If there is no file name, the buffer is not loaded, 'buftype' is
4114 // set, we are in the middle of a save or being called recursively: ignore
4115 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (buf->b_ffname == NULL
4117 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004118 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004121#ifdef FEAT_NETBEANS_INTG
4122 || isNetbeansBuffer(buf)
4123#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004124#ifdef FEAT_TERMINAL
4125 || buf->b_term != NULL
4126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 )
4128 return 0;
4129
4130 if ( !(buf->b_flags & BF_NOTEDITED)
4131 && buf->b_mtime != 0
4132 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004133 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004134 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#ifdef HAVE_ST_MODE
4136 || (int)st.st_mode != buf->b_orig_mode
4137#else
4138 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4139#endif
4140 ))
4141 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004142 long prev_b_mtime = buf->b_mtime;
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 retval = 1;
4145
Bram Moolenaar386bc822018-07-07 18:34:12 +02004146 // set b_mtime to stop further warnings (e.g., when executing
4147 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (stat_res < 0)
4149 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004150 // Check the file again later to see if it re-appears.
4151 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 buf->b_orig_size = 0;
4153 buf->b_orig_mode = 0;
4154 }
4155 else
4156 buf_store_time(buf, &st, buf->b_ffname);
4157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004158 // Don't do anything for a directory. Might contain the file
4159 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (mch_isdir(buf->b_fname))
4161 ;
4162
4163 /*
4164 * If 'autoread' is set, the buffer has no changes and the file still
4165 * exists, reload the buffer. Use the buffer-local option value if it
4166 * was set, the global option value otherwise.
4167 */
4168 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4169 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004170 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
4172 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004173 if (stat_res < 0)
4174 reason = "deleted";
4175 else if (bufIsChanged(buf))
4176 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004177 /*
4178 * Check if the file contents really changed to avoid giving a
4179 * warning when only the timestamp was set (e.g., checked out of
4180 * CVS). Always warn when the buffer was changed.
4181 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4183 reason = "changed";
4184 else if (orig_mode != buf->b_orig_mode)
4185 reason = "mode";
4186 else
4187 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 /*
4190 * Only give the warning if there are no FileChangedShell
4191 * autocommands.
4192 * Avoid being called recursively by setting "busy".
4193 */
4194 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004195#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004196 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4197 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004198#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004199 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4201 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004202 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 busy = FALSE;
4204 if (n)
4205 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004206 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004207 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004208#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004209 s = get_vim_var_str(VV_FCS_CHOICE);
4210 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004211 reload = RELOAD_NORMAL;
4212 else if (STRCMP(s, "edit") == 0)
4213 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004214 else if (STRCMP(s, "ask") == 0)
4215 n = FALSE;
4216 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004217#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004218 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004220 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004222 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004223 {
4224 // Only give the message once.
4225 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004226 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 else
4229 {
4230 helpmesg = TRUE;
4231#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4232 can_reload = TRUE;
4233#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 if (reason[2] == 'n')
4235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 mesg2 = _("See \":help W12\" for more info.");
4238 }
4239 else if (reason[1] == 'h')
4240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004242 mesg2 = _("See \":help W11\" for more info.");
4243 }
4244 else if (*reason == 'm')
4245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004247 mesg2 = _("See \":help W16\" for more info.");
4248 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004249 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004251 // Only timestamp changed, store it to avoid a warning
4252 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004253 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004254 buf->b_mtime_read_ns = buf->b_mtime_ns;
4255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 }
4258 }
4259
4260 }
4261 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4262 && vim_fexists(buf->b_ffname))
4263 {
4264 retval = 1;
4265 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4266 buf->b_flags |= BF_NEW_W;
4267#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4268 can_reload = TRUE;
4269#endif
4270 }
4271
4272 if (mesg != NULL)
4273 {
4274 path = home_replace_save(buf, buf->b_fname);
4275 if (path != NULL)
4276 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004277 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004279 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004280 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004281#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004282 // Set warningmsg here, before the unimportant and output-specific
4283 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004284 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4287 if (can_reload)
4288 {
4289 if (*mesg2 != NUL)
4290 {
4291 STRCAT(tbuf, "\n");
4292 STRCAT(tbuf, mesg2);
4293 }
Rob Pilling8196e942022-02-11 15:12:10 +00004294 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4295 (char_u *)tbuf,
4296 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4297 1, NULL, TRUE))
4298 {
4299 case 2:
4300 reload = RELOAD_NORMAL;
4301 break;
4302 case 3:
4303 reload = RELOAD_DETECT;
4304 break;
4305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 else
4308#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004309 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4310 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 if (*mesg2 != NUL)
4313 {
4314 STRCAT(tbuf, "; ");
4315 STRCAT(tbuf, mesg2);
4316 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004317 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 retval = 2;
4319 }
4320 else
4321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004325 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004327 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 msg_clr_eos();
4329 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004330 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
4332 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004333#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004335#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004336 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004337 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 redraw_cmdline = FALSE;
4341 }
4342 }
4343 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345
4346 vim_free(path);
4347 vim_free(tbuf);
4348 }
4349 }
4350
Rob Pilling8196e942022-02-11 15:12:10 +00004351 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004354 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004355#ifdef FEAT_PERSISTENT_UNDO
4356 if (buf->b_p_udf && buf->b_ffname != NULL)
4357 {
4358 char_u hash[UNDO_HASH_SIZE];
4359 buf_T *save_curbuf = curbuf;
4360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004362 curbuf = buf;
4363 u_compute_hash(hash);
4364 u_write_undo(NULL, FALSE, buf, hash);
4365 curbuf = save_curbuf;
4366 }
4367#endif
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004370 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004371 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004372 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4373 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004375 // restore this in case an autocommand has set it; it would break
4376 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 need_mouse_correct = save_mouse_correct;
4378#endif
4379
4380 return retval;
4381}
4382
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004383/*
4384 * Reload a buffer that is already loaded.
4385 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004386 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4387 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004388 */
4389 void
Rob Pilling8196e942022-02-11 15:12:10 +00004390buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004391{
4392 exarg_T ea;
4393 pos_T old_cursor;
4394 linenr_T old_topline;
4395 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004396 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004397 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004398 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004399 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004400 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004401 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004402
Bram Moolenaare76062c2022-11-28 18:51:43 +00004403 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004404 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004405 if (curbuf != buf)
4406 {
4407 // Failed to find a window for "buf", it is dangerous to continue,
4408 // better bail out.
4409 return;
4410 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004411
Rob Pilling8196e942022-02-11 15:12:10 +00004412 // Unless reload_options is set, we only want to read the text from the
4413 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4414 // Force the fileformat and encoding to be the same.
4415 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004416 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004417 else
4418 prepped = prep_exarg(&ea, buf);
4419
4420 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004421 {
4422 old_cursor = curwin->w_cursor;
4423 old_topline = curwin->w_topline;
4424
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004425 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 // Save all the text, so that the reload can be undone.
4428 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004429 u_sync(FALSE);
4430 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4431 flags |= READ_KEEP_UNDO;
4432 }
4433
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004434 /*
4435 * To behave like when a new file is edited (matters for
4436 * BufReadPost autocommands) we first need to delete the current
4437 * buffer contents. But if reading the file fails we should keep
4438 * the old contents. Can't use memory only, the file might be
4439 * too big. Use a hidden buffer to move the buffer contents to.
4440 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004441 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004442 savebuf = NULL;
4443 else
4444 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004446 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004447 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004448 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004449 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004450 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004451 curbuf = savebuf;
4452 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004453 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004454 curbuf = buf;
4455 curwin->w_buffer = buf;
4456 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004457 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004458 || move_lines(buf, savebuf) == FAIL)
4459 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004460 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004461 saved = FAIL;
4462 }
4463 }
4464
4465 if (saved == OK)
4466 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004467 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4468 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004469 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4470 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004471 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004472 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004473#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004474 if (!aborting())
4475#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004476 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004477 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004478 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 // Put the text back from the save buffer. First
4480 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004481 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004482 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004483 break;
4484 (void)move_lines(savebuf, buf);
4485 }
4486 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004487 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004488 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004490 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004491 if ((flags & READ_KEEP_UNDO) == 0)
4492 {
4493 u_blockfree(buf);
4494 u_clearall(buf);
4495 }
4496 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004497 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004499 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004500 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004501 }
4502 }
4503 vim_free(ea.cmd);
4504
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004505 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004506 wipe_buffer(savebuf, FALSE);
4507
4508#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004509 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004510 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004511#endif
4512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004513 // Restore the topline and cursor position and check it (lines may
4514 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004515 if (old_topline > curbuf->b_ml.ml_line_count)
4516 curwin->w_topline = curbuf->b_ml.ml_line_count;
4517 else
4518 curwin->w_topline = old_topline;
4519 curwin->w_cursor = old_cursor;
4520 check_cursor();
4521 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004522 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004523#ifdef FEAT_FOLDING
4524 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004525 win_T *wp;
4526 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004528 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004529 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004530 if (wp->w_buffer == curwin->w_buffer
4531 && !foldmethodIsManual(wp))
4532 foldUpdateAll(wp);
4533 }
4534#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004535 // If the mode didn't change and 'readonly' was set, keep the old
4536 // value; the user probably used the ":view" command. But don't
4537 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004538 if (orig_mode == curbuf->b_orig_mode)
4539 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004541 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004542 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004543 }
4544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004546 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004547 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004548}
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004551buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
4553 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004554#ifdef ST_MTIM_NSEC
4555 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4556#else
4557 buf->b_mtime_ns = 0;
4558#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004559 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#ifdef HAVE_ST_MODE
4561 buf->b_orig_mode = (int)st->st_mode;
4562#else
4563 buf->b_orig_mode = mch_getperm(fname);
4564#endif
4565}
4566
4567/*
4568 * Adjust the line with missing eol, used for the next write.
4569 * Used for do_filter(), when the input lines for the filter are deleted.
4570 */
4571 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004572write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004574 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004575 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576}
4577
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004578// Subfuncions for readdirex()
4579#ifdef FEAT_EVAL
4580# ifdef MSWIN
4581 static char_u *
4582getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4583{
4584 stat_T st;
4585 unsigned short st_mode;
4586 DWORD flag = wfd->dwFileAttributes;
4587 WCHAR *wp;
4588
4589 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4590 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4591 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4592 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4593
4594 wp = wcsrchr(wfd->cFileName, L'.');
4595 if (wp != NULL)
4596 {
4597 if (_wcsicmp(wp, L".exe") == 0 ||
4598 _wcsicmp(wp, L".com") == 0 ||
4599 _wcsicmp(wp, L".cmd") == 0 ||
4600 _wcsicmp(wp, L".bat") == 0)
4601 st_mode |= _S_IEXEC;
4602 }
4603
4604 // Copy user bits to group/other.
4605 st_mode |= (st_mode & 0700) >> 3;
4606 st_mode |= (st_mode & 0700) >> 6;
4607
4608 st.st_mode = st_mode;
4609 return getfpermst(&st, perm);
4610}
4611
4612 static char_u *
4613getftypewfd(WIN32_FIND_DATAW *wfd)
4614{
4615 DWORD flag = wfd->dwFileAttributes;
4616 DWORD tag = wfd->dwReserved0;
4617
4618 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4619 {
4620 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4621 return (char_u*)"junction";
4622 else if (tag == IO_REPARSE_TAG_SYMLINK)
4623 {
4624 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4625 return (char_u*)"linkd";
4626 else
4627 return (char_u*)"link";
4628 }
4629 return (char_u*)"reparse"; // unknown reparse point type
4630 }
4631 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4632 return (char_u*)"dir";
4633 else
4634 return (char_u*)"file";
4635}
4636
4637 static dict_T *
4638create_readdirex_item(WIN32_FIND_DATAW *wfd)
4639{
4640 dict_T *item;
4641 char_u *p;
4642 varnumber_T size, time;
4643 char_u permbuf[] = "---------";
4644
4645 item = dict_alloc();
4646 if (item == NULL)
4647 return NULL;
4648 item->dv_refcount++;
4649
4650 p = utf16_to_enc(wfd->cFileName, NULL);
4651 if (p == NULL)
4652 goto theend;
4653 if (dict_add_string(item, "name", p) == FAIL)
4654 {
4655 vim_free(p);
4656 goto theend;
4657 }
4658 vim_free(p);
4659
4660 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4661 if (dict_add_number(item, "size", size) == FAIL)
4662 goto theend;
4663
4664 // Convert FILETIME to unix time.
4665 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4666 wfd->ftLastWriteTime.dwLowDateTime)
4667 - 116444736000000000) / 10000000;
4668 if (dict_add_number(item, "time", time) == FAIL)
4669 goto theend;
4670
4671 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4672 goto theend;
4673 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4674 goto theend;
4675
4676 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4677 goto theend;
4678 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4679 goto theend;
4680
4681 return item;
4682
4683theend:
4684 dict_unref(item);
4685 return NULL;
4686}
4687# else
4688 static dict_T *
4689create_readdirex_item(char_u *path, char_u *name)
4690{
4691 dict_T *item;
4692 char *p;
4693 size_t len;
4694 stat_T st;
4695 int ret, link = FALSE;
4696 varnumber_T size;
4697 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004698 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004699 struct passwd *pw;
4700 struct group *gr;
4701
4702 item = dict_alloc();
4703 if (item == NULL)
4704 return NULL;
4705 item->dv_refcount++;
4706
4707 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4708 p = alloc(len);
4709 if (p == NULL)
4710 goto theend;
4711 vim_snprintf(p, len, "%s/%s", path, name);
4712 ret = mch_lstat(p, &st);
4713 if (ret >= 0 && S_ISLNK(st.st_mode))
4714 {
4715 link = TRUE;
4716 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004717 if (ret < 0)
4718 q = (char_u*)"link";
4719
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004720 }
4721 vim_free(p);
4722
4723 if (dict_add_string(item, "name", name) == FAIL)
4724 goto theend;
4725
4726 if (ret >= 0)
4727 {
4728 size = (varnumber_T)st.st_size;
4729 if (S_ISDIR(st.st_mode))
4730 size = 0;
4731 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004732 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004733 size = -2;
4734 if (dict_add_number(item, "size", size) == FAIL)
4735 goto theend;
4736 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4737 goto theend;
4738
4739 if (link)
4740 {
4741 if (S_ISDIR(st.st_mode))
4742 q = (char_u*)"linkd";
4743 else
4744 q = (char_u*)"link";
4745 }
4746 else
4747 q = getftypest(&st);
4748 if (dict_add_string(item, "type", q) == FAIL)
4749 goto theend;
4750 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4751 goto theend;
4752
4753 pw = getpwuid(st.st_uid);
4754 if (pw == NULL)
4755 q = (char_u*)"";
4756 else
4757 q = (char_u*)pw->pw_name;
4758 if (dict_add_string(item, "user", q) == FAIL)
4759 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004760# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004761 gr = getgrgid(st.st_gid);
4762 if (gr == NULL)
4763 q = (char_u*)"";
4764 else
4765 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004766# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004767 if (dict_add_string(item, "group", q) == FAIL)
4768 goto theend;
4769 }
4770 else
4771 {
4772 if (dict_add_number(item, "size", -1) == FAIL)
4773 goto theend;
4774 if (dict_add_number(item, "time", -1) == FAIL)
4775 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004776 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004777 goto theend;
4778 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4779 goto theend;
4780 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4781 goto theend;
4782 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4783 goto theend;
4784 }
4785 return item;
4786
4787theend:
4788 dict_unref(item);
4789 return NULL;
4790}
4791# endif
4792
4793 static int
4794compare_readdirex_item(const void *p1, const void *p2)
4795{
4796 char_u *name1, *name2;
4797
Bram Moolenaard61efa52022-07-23 09:52:04 +01004798 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4799 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004800 if (readdirex_sort == READDIR_SORT_BYTE)
4801 return STRCMP(name1, name2);
4802 else if (readdirex_sort == READDIR_SORT_IC)
4803 return STRICMP(name1, name2);
4804 else
4805 return STRCOLL(name1, name2);
4806}
4807
4808 static int
4809compare_readdir_item(const void *s1, const void *s2)
4810{
4811 if (readdirex_sort == READDIR_SORT_BYTE)
4812 return STRCMP(*(char **)s1, *(char **)s2);
4813 else if (readdirex_sort == READDIR_SORT_IC)
4814 return STRICMP(*(char **)s1, *(char **)s2);
4815 else
4816 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004817}
4818#endif
4819
Bram Moolenaarda440d22016-01-16 21:27:23 +01004820#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4821/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004822 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004823 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004824 * If "withattr" is TRUE, retrieve the names and their attributes.
4825 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004826 * Return OK for success, FAIL for failure.
4827 */
4828 int
4829readdir_core(
4830 garray_T *gap,
4831 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004832 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004833 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004834 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004835 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004836{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004837 int failed = FALSE;
4838 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004839# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004840 char_u *buf;
4841 int ok;
4842 HANDLE hFind = INVALID_HANDLE_VALUE;
4843 WIN32_FIND_DATAW wfd;
4844 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004845# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004846 DIR *dirp;
4847 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004848# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004849
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004850 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004851
4852# ifdef FEAT_EVAL
4853# define FREE_ITEM(item) do { \
4854 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004855 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004856 else \
4857 vim_free(item); \
4858 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004859
4860 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004861# else
4862# define FREE_ITEM(item) vim_free(item)
4863# endif
4864
4865# ifdef MSWIN
4866 buf = alloc(MAXPATHL);
4867 if (buf == NULL)
4868 return FAIL;
4869 STRNCPY(buf, path, MAXPATHL-5);
4870 p = buf + STRLEN(buf);
4871 MB_PTR_BACK(buf, p);
4872 if (*p == '\\' || *p == '/')
4873 *p = NUL;
4874 STRCAT(p, "\\*");
4875
4876 wn = enc_to_utf16(buf, NULL);
4877 if (wn != NULL)
4878 hFind = FindFirstFileW(wn, &wfd);
4879 ok = (hFind != INVALID_HANDLE_VALUE);
4880 if (!ok)
4881 {
4882 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004883 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004884 }
4885 else
4886 {
4887 while (ok)
4888 {
4889 int ignore;
4890 void *item;
4891 WCHAR *wp;
4892
4893 wp = wfd.cFileName;
4894 ignore = wp[0] == L'.' &&
4895 (wp[1] == NUL ||
4896 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004897 if (ignore)
4898 {
4899 ok = FindNextFileW(hFind, &wfd);
4900 continue;
4901 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004902# ifdef FEAT_EVAL
4903 if (withattr)
4904 item = (void*)create_readdirex_item(&wfd);
4905 else
4906# endif
4907 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4908 if (item == NULL)
4909 {
4910 failed = TRUE;
4911 break;
4912 }
4913
4914 if (!ignore && checkitem != NULL)
4915 {
4916 int r = checkitem(context, item);
4917
4918 if (r < 0)
4919 {
4920 FREE_ITEM(item);
4921 break;
4922 }
4923 if (r == 0)
4924 ignore = TRUE;
4925 }
4926
4927 if (!ignore)
4928 {
4929 if (ga_grow(gap, 1) == OK)
4930 ((void**)gap->ga_data)[gap->ga_len++] = item;
4931 else
4932 {
4933 failed = TRUE;
4934 FREE_ITEM(item);
4935 break;
4936 }
4937 }
4938 else
4939 FREE_ITEM(item);
4940
4941 ok = FindNextFileW(hFind, &wfd);
4942 }
4943 FindClose(hFind);
4944 }
4945
4946 vim_free(buf);
4947 vim_free(wn);
4948# else // MSWIN
4949 dirp = opendir((char *)path);
4950 if (dirp == NULL)
4951 {
4952 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004953 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004954 }
4955 else
4956 {
4957 for (;;)
4958 {
4959 int ignore;
4960 void *item;
4961
4962 dp = readdir(dirp);
4963 if (dp == NULL)
4964 break;
4965 p = (char_u *)dp->d_name;
4966
4967 ignore = p[0] == '.' &&
4968 (p[1] == NUL ||
4969 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004970 if (ignore)
4971 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004972# ifdef FEAT_EVAL
4973 if (withattr)
4974 item = (void*)create_readdirex_item(path, p);
4975 else
4976# endif
4977 item = (void*)vim_strsave(p);
4978 if (item == NULL)
4979 {
4980 failed = TRUE;
4981 break;
4982 }
4983
Bram Moolenaarfe154992022-03-22 20:42:12 +00004984 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004985 {
4986 int r = checkitem(context, item);
4987
4988 if (r < 0)
4989 {
4990 FREE_ITEM(item);
4991 break;
4992 }
4993 if (r == 0)
4994 ignore = TRUE;
4995 }
4996
4997 if (!ignore)
4998 {
4999 if (ga_grow(gap, 1) == OK)
5000 ((void**)gap->ga_data)[gap->ga_len++] = item;
5001 else
5002 {
5003 failed = TRUE;
5004 FREE_ITEM(item);
5005 break;
5006 }
5007 }
5008 else
5009 FREE_ITEM(item);
5010 }
5011
5012 closedir(dirp);
5013 }
5014# endif // MSWIN
5015
5016# undef FREE_ITEM
5017
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005018 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005019 {
5020# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005021 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005022 if (withattr)
5023 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5024 compare_readdirex_item);
5025 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005026 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5027 compare_readdir_item);
5028# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005029 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005030# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005031 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005032
5033 return failed ? FAIL : OK;
5034}
5035
5036/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005037 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005038 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005039 */
5040 int
5041delete_recursive(char_u *name)
5042{
5043 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005044 int i;
5045 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005046 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005047
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005048 // A symbolic link to a directory itself is deleted, not the directory it
5049 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005050 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005051# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005052 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005053# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005054 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005055# endif
5056 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005057 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005058 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005059 if (exp == NULL)
5060 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005061 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005062 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005063 for (i = 0; i < ga.ga_len; ++i)
5064 {
5065 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5066 ((char_u **)ga.ga_data)[i]);
5067 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005068 // Remember the failure but continue deleting any further
5069 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005070 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005071 }
5072 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005073 if (mch_rmdir(exp) != 0)
5074 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005075 }
5076 else
5077 result = -1;
5078 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005079 }
5080 else
5081 result = mch_remove(name) == 0 ? 0 : -1;
5082
5083 return result;
5084}
5085#endif
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005090# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5091/*
5092 * Open temporary directory and take file lock to prevent
5093 * to be auto-cleaned.
5094 */
5095 static void
5096vim_opentempdir(void)
5097{
5098 DIR *dp = NULL;
5099
5100 if (vim_tempdir_dp != NULL)
5101 return;
5102
5103 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005104 if (dp == NULL)
5105 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005106
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005107 vim_tempdir_dp = dp;
5108 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005109}
5110
5111/*
5112 * Close temporary directory - it automatically release file lock.
5113 */
5114 static void
5115vim_closetempdir(void)
5116{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005117 if (vim_tempdir_dp == NULL)
5118 return;
5119
5120 closedir(vim_tempdir_dp);
5121 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005122}
5123# endif
5124
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125/*
5126 * Delete the temp directory and all files it contains.
5127 */
5128 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005131 if (vim_tempdir == NULL)
5132 return;
5133
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005134# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005135 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005136# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005137 // remove the trailing path separator
5138 gettail(vim_tempdir)[-1] = NUL;
5139 delete_recursive(vim_tempdir);
5140 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005144 * Directory "tempdir" was created. Expand this name to a full path and put
5145 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5146 * "tempdir" must be no longer than MAXPATHL.
5147 */
5148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005149vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005150{
5151 char_u *buf;
5152
Bram Moolenaar964b3742019-05-24 18:54:09 +02005153 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005154 if (buf == NULL)
5155 return;
5156
5157 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5158 STRCPY(buf, tempdir);
5159 add_pathsep(buf);
5160 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005161# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005162 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005163# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005164 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005165}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005166#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005167
5168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 * vim_tempname(): Return a unique name that can be used for a temp file.
5170 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005171 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5172 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 *
5174 * The returned pointer is to allocated memory.
5175 * The returned pointer is NULL if no valid name was found.
5176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005178vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005179 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005180 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
5182#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005183 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005184#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005185 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#else
5187 char_u itmp[TEMPNAMELEN];
5188#endif
5189
5190#ifdef TEMPDIRNAMES
5191 static char *(tempdirs[]) = {TEMPDIRNAMES};
5192 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005194 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195# endif
5196
5197 /*
5198 * This will create a directory for private use by this instance of Vim.
5199 * This is done once, and the same directory is used for all temp files.
5200 * This method avoids security problems because of symlink attacks et al.
5201 * It's also a bit faster, because we only need to check for an existing
5202 * file when creating the directory and not for each temp file.
5203 */
5204 if (vim_tempdir == NULL)
5205 {
5206 /*
5207 * Try the entries in TEMPDIRNAMES to create the temp directory.
5208 */
K.Takataeeec2542021-06-02 13:28:16 +02005209 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005211# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005212 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005213 long nr;
5214 long off;
5215# endif
5216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005217 // Expand $TMP, leave room for "/v1100000/999999999".
5218 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005220 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005222 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005223 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaareaf03392009-11-17 11:08:52 +00005225# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005226 {
5227# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 // Make sure the umask doesn't remove the executable bit.
5229 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005230 mode_t umask_save = umask(077);
5231# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005233 STRCAT(itmp, "vXXXXXX");
5234 if (mkdtemp((char *)itmp) != NULL)
5235 vim_settempdir(itmp);
5236# if defined(UNIX) || defined(VMS)
5237 (void)umask(umask_save);
5238# endif
5239 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005240# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005241 // Get an arbitrary number of up to 6 digits. When it's
5242 // unlikely that it already exists it will be faster,
5243 // otherwise it doesn't matter. The use of mkdir() avoids any
5244 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005246 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // Try up to 10000 different values until we find a name that
5249 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 for (off = 0; off < 10000L; ++off)
5251 {
5252 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005253# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
Bram Moolenaareaf03392009-11-17 11:08:52 +00005257 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5258# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259 // If mkdir() does not set errno to EEXIST, check for
5260 // existing file here. There is a race condition then,
5261 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (mch_stat((char *)itmp, &st) >= 0)
5263 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005264# endif
5265# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005266 // Make sure the umask doesn't remove the executable bit.
5267 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005271# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (r == 0)
5275 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005276 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 break;
5278 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005279# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005280 // If the mkdir() didn't fail because the file/dir exists,
5281 // we probably can't create any dir here, try another
5282 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 break;
5286 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005287# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (vim_tempdir != NULL)
5289 break;
5290 }
5291 }
5292 }
5293
5294 if (vim_tempdir != NULL)
5295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005296 // There is no need to check if the file exists, because we own the
5297 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5299 return vim_strsave(itmp);
5300 }
5301
5302 return NULL;
5303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaar4f974752019-02-17 17:44:42 +01005306# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005307 WCHAR wszTempFile[_MAX_PATH + 1];
5308 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005309 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 char_u *retval;
5311 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005312 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005313 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005315 wcscpy(itmp, L"");
5316 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005317 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005318 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005319 wszTempFile[1] = L'\\';
5320 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005321 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005322 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005323
5324 // randomize the name to avoid collisions
5325 i = mch_get_pid() + extra_char;
5326 buf4[1] = chartab[i % 36];
5327 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005328 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005330 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005331 // GetTempFileName() will create the file, we don't want that
5332 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005334 // Backslashes in a temp file name cause problems when filtering with
5335 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005336 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005337 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005338 shname = gettail(p_sh);
5339 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5340 || strstr((char *)shname, "pwsh") != NULL ))
5341 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 for (p = retval; *p; ++p)
5343 if (*p == '\\')
5344 *p = '/';
5345 return retval;
5346
Bram Moolenaar4f974752019-02-17 17:44:42 +01005347# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
5349# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005350 char_u *p;
5351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005352 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005353 p = tmpnam((char *)itmp);
5354 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return NULL;
5356# else
5357 char_u *p;
5358
5359# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 // mktemp() is not working on VMS. It seems to be
5361 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 sprintf((char *)itmp, "VIM%c", extra_char);
5363 p = (char_u *)tempnam("tmp:", (char *)itmp);
5364 if (p != NULL)
5365 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005366 // VMS will use '.LIS' if we don't explicitly specify an extension,
5367 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 STRCPY(itmp, p);
5369 STRCAT(itmp, ".txt");
5370 free(p);
5371 }
5372 else
5373 return NULL;
5374# else
5375 STRCPY(itmp, TEMPNAME);
5376 if ((p = vim_strchr(itmp, '?')) != NULL)
5377 *p = extra_char;
5378 if (mktemp((char *)itmp) == NULL)
5379 return NULL;
5380# endif
5381# endif
5382
5383 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005384# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386}
5387
5388#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5389/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005390 * Convert all backslashes in fname to forward slashes in-place, unless when
5391 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 */
5393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005394forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 char_u *p;
5397
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005398 if (path_with_url(fname))
5399 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005401 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005402 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005404 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 *p = '/';
5406}
5407#endif
5408
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005409/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005410 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5411 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5412 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 * Used for autocommands and 'wildignore'.
5414 * Returns TRUE if there is a match, FALSE otherwise.
5415 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005416 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 char_u *pattern, // pattern to match with
5419 regprog_T **prog, // pre-compiled regprog or NULL
5420 char_u *fname, // full path of file name
5421 char_u *sfname, // short file name or NULL
5422 char_u *tail, // tail of path
5423 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 regmatch_T regmatch;
5426 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005429 if (prog != NULL)
5430 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005432 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
5434 /*
5435 * Try for a match with the pattern with:
5436 * 1. the full file name, when the pattern has a '/'.
5437 * 2. the short file name, when the pattern has a '/'.
5438 * 3. the tail of the file name, when the pattern has no '/'.
5439 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005440 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 && ((allow_dirs
5442 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5443 || (sfname != NULL
5444 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005445 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 result = TRUE;
5447
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005448 if (prog != NULL)
5449 *prog = regmatch.regprog;
5450 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005451 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 return result;
5453}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455/*
5456 * Return TRUE if a file matches with a pattern in "list".
5457 * "list" is a comma-separated list of patterns, like 'wildignore'.
5458 * "sfname" is the short file name or NULL, "ffname" the long file name.
5459 */
5460 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 char_u buf[100];
5464 char_u *tail;
5465 char_u *regpat;
5466 char allow_dirs;
5467 int match;
5468 char_u *p;
5469
5470 tail = gettail(sfname);
5471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005472 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 p = list;
5474 while (*p)
5475 {
5476 copy_option_part(&p, buf, 100, ",");
5477 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5478 if (regpat == NULL)
5479 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005480 match = match_file_pat(regpat, NULL, ffname, sfname,
5481 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 vim_free(regpat);
5483 if (match)
5484 return TRUE;
5485 }
5486 return FALSE;
5487}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489/*
5490 * Convert the given pattern "pat" which has shell style wildcards in it, into
5491 * a regular expression, and return the result in allocated memory. If there
5492 * is a directory path separator to be matched, then TRUE is put in
5493 * allow_dirs, otherwise FALSE is put there -- webb.
5494 * Handle backslashes before special characters, like "\*" and "\ ".
5495 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 * Returns NULL when out of memory.
5497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499file_pat_to_reg_pat(
5500 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005501 char_u *pat_end, // first char after pattern or NULL
5502 char *allow_dirs, // Result passed back out in here
5503 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005505 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 char_u *endp;
5507 char_u *reg_pat;
5508 char_u *p;
5509 int i;
5510 int nested = 0;
5511 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
5513 if (allow_dirs != NULL)
5514 *allow_dirs = FALSE;
5515 if (pat_end == NULL)
5516 pat_end = pat + STRLEN(pat);
5517
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 for (p = pat; p < pat_end; p++)
5519 {
5520 switch (*p)
5521 {
5522 case '*':
5523 case '.':
5524 case ',':
5525 case '{':
5526 case '}':
5527 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005528 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 break;
5530#ifdef BACKSLASH_IN_FILENAME
5531 case '\\':
5532 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005533 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 break;
5535#endif
5536 default:
5537 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005538 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 ++p;
5541 ++size;
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 break;
5544 }
5545 }
5546 reg_pat = alloc(size + 1);
5547 if (reg_pat == NULL)
5548 return NULL;
5549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 if (pat[0] == '*')
5553 while (pat[0] == '*' && pat < pat_end - 1)
5554 pat++;
5555 else
5556 reg_pat[i++] = '^';
5557 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005558 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
5560 while (endp - pat > 0 && *endp == '*')
5561 endp--;
5562 add_dollar = FALSE;
5563 }
5564 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5565 {
5566 switch (*p)
5567 {
5568 case '*':
5569 reg_pat[i++] = '.';
5570 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005571 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005572 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 break;
5574 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 case '~':
5576 reg_pat[i++] = '\\';
5577 reg_pat[i++] = *p;
5578 break;
5579 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 reg_pat[i++] = '.';
5581 break;
5582 case '\\':
5583 if (p[1] == NUL)
5584 break;
5585#ifdef BACKSLASH_IN_FILENAME
5586 if (!no_bslash)
5587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 // translate:
5589 // "\x" to "\\x" e.g., "dir\file"
5590 // "\*" to "\\.*" e.g., "dir\*.c"
5591 // "\?" to "\\." e.g., "dir\??.c"
5592 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5594 && p[1] != '+')
5595 {
5596 reg_pat[i++] = '[';
5597 reg_pat[i++] = '\\';
5598 reg_pat[i++] = '/';
5599 reg_pat[i++] = ']';
5600 if (allow_dirs != NULL)
5601 *allow_dirs = TRUE;
5602 break;
5603 }
5604 }
5605#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 // Undo escaping from ExpandEscape():
5607 // foo\?bar -> foo?bar
5608 // foo\%bar -> foo%bar
5609 // foo\,bar -> foo,bar
5610 // foo\ bar -> foo bar
5611 // Don't unescape \, * and others that are also special in a
5612 // regexp.
5613 // An escaped { must be unescaped since we use magic not
5614 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 if (*++p == '?'
5616#ifdef BACKSLASH_IN_FILENAME
5617 && no_bslash
5618#endif
5619 )
5620 reg_pat[i++] = '?';
5621 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005622 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005623 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005624 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005625 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5626 {
5627 reg_pat[i++] = '\\';
5628 reg_pat[i++] = '{';
5629 p += 2;
5630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
5632 {
5633 if (allow_dirs != NULL && vim_ispathsep(*p)
5634#ifdef BACKSLASH_IN_FILENAME
5635 && (!no_bslash || *p != '\\')
5636#endif
5637 )
5638 *allow_dirs = TRUE;
5639 reg_pat[i++] = '\\';
5640 reg_pat[i++] = *p;
5641 }
5642 break;
5643#ifdef BACKSLASH_IN_FILENAME
5644 case '/':
5645 reg_pat[i++] = '[';
5646 reg_pat[i++] = '\\';
5647 reg_pat[i++] = '/';
5648 reg_pat[i++] = ']';
5649 if (allow_dirs != NULL)
5650 *allow_dirs = TRUE;
5651 break;
5652#endif
5653 case '{':
5654 reg_pat[i++] = '\\';
5655 reg_pat[i++] = '(';
5656 nested++;
5657 break;
5658 case '}':
5659 reg_pat[i++] = '\\';
5660 reg_pat[i++] = ')';
5661 --nested;
5662 break;
5663 case ',':
5664 if (nested)
5665 {
5666 reg_pat[i++] = '\\';
5667 reg_pat[i++] = '|';
5668 }
5669 else
5670 reg_pat[i++] = ',';
5671 break;
5672 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005673 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005675 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 *allow_dirs = TRUE;
5677 reg_pat[i++] = *p;
5678 break;
5679 }
5680 }
5681 if (add_dollar)
5682 reg_pat[i++] = '$';
5683 reg_pat[i] = NUL;
5684 if (nested != 0)
5685 {
5686 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005687 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005689 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005690 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 return reg_pat;
5693}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005694
5695#if defined(EINTR) || defined(PROTO)
5696/*
5697 * Version of read() that retries when interrupted by EINTR (possibly
5698 * by a SIGWINCH).
5699 */
5700 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005702{
5703 long ret;
5704
5705 for (;;)
5706 {
5707 ret = vim_read(fd, buf, bufsize);
5708 if (ret >= 0 || errno != EINTR)
5709 break;
5710 }
5711 return ret;
5712}
5713
5714/*
5715 * Version of write() that retries when interrupted by EINTR (possibly
5716 * by a SIGWINCH).
5717 */
5718 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005720{
5721 long ret = 0;
5722 long wlen;
5723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005724 // Repeat the write() so long it didn't fail, other than being interrupted
5725 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005726 while (ret < (long)bufsize)
5727 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005728 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005729 if (wlen < 0)
5730 {
5731 if (errno != EINTR)
5732 break;
5733 }
5734 else
5735 ret += wlen;
5736 }
5737 return ret;
5738}
5739#endif