blob: 9916de8a63e714e9fe2cf9928f456da0868b981c [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;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200221
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100222 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200223
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100224 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
226 /*
227 * If there is no file name yet, use the one for the read file.
228 * BF_NOTEDITED is set to reflect this.
229 * Don't do this for a read from a filter.
230 * Only do this when 'cpoptions' contains the 'f' flag.
231 */
232 if (curbuf->b_ffname == NULL
233 && !filtering
234 && fname != NULL
235 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
236 && !(flags & READ_DUMMY))
237 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000238 if (set_rw_fname(fname, sfname) == FAIL)
239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 }
241
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100242 // Remember the initial values of curbuf, curbuf->b_ffname and
243 // curbuf->b_fname to detect whether they are altered as a result of
244 // executing nasty autocommands. Also check if "fname" and "sfname"
245 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200246 old_curbuf = curbuf;
247 old_b_ffname = curbuf->b_ffname;
248 old_b_fname = curbuf->b_fname;
249 using_b_ffname = (fname == curbuf->b_ffname)
250 || (sfname == curbuf->b_ffname);
251 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200252
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100253 // After reading a file the cursor line changes but we don't want to
254 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000255 ex_no_reprint = TRUE;
256
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100257 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000258 need_fileinfo = FALSE;
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 /*
261 * For Unix: Use the short file name whenever possible.
262 * Avoids problems with networks and when directory names are changed.
263 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
264 * another directory, which we don't detect.
265 */
266 if (sfname == NULL)
267 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200268#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 fname = sfname;
270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 /*
273 * The BufReadCmd and FileReadCmd events intercept the reading process by
274 * executing the associated commands instead.
275 */
276 if (!filtering && !read_stdin && !read_buffer)
277 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100278 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100280 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
282 curbuf->b_op_start.col = 0;
283
284 if (newfile)
285 {
286 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
287 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200288 {
289 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200291 if (aborting())
292 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200294 // The BufReadCmd code usually uses ":read" to get the text and
295 // perhaps ":file" to change the buffer name. But we should
296 // consider this to work like ":edit", thus reset the
297 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
298 // same file.
299 if (status == OK)
300 curbuf->b_flags &= ~BF_NOTEDITED;
301 return status;
302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 }
304 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
305 FALSE, NULL, eap))
306#ifdef FEAT_EVAL
307 return aborting() ? FAIL : OK;
308#else
309 return OK;
310#endif
311
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100312 curbuf->b_op_start = orig_start;
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100313
314 if (flags & READ_NOFILE)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100315 // Return NOTDONE instead of FAIL so that BufEnter can be triggered
316 // and other operations don't fail.
317 return NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
320 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100321 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100323 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000325 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200327 size_t namelen = STRLEN(fname);
328
329 // If the name is too long we might crash further on, quit here.
330 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000331 {
332 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
333 msg_end();
334 msg_scroll = msg_save;
335 return FAIL;
336 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200337
338 // If the name ends in a path separator, we can't open it. Check here,
339 // because reading the file may actually work, but then creating the
340 // swap file may destroy it! Reported on MS-DOS and Win 95.
341 if (after_pathsep(fname, fname + namelen))
342 {
343 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
344 msg_end();
345 msg_scroll = msg_save;
Bram Moolenaar40fa12a2021-09-22 14:18:13 +0200346 return NOTDONE;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
349
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200350 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100352#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200353 /*
354 * On Unix it is possible to read a directory, so we have to
355 * check for it before the mch_open().
356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100358 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
359 && !S_ISFIFO(perm) // ... or fifo
360 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000361# ifdef OPEN_CHR_FILES
362 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100363 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 )
366 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100367 int retval = FAIL;
368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100370 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200371 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100372 retval = NOTDONE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 else
375 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
376 msg_end();
377 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100378 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200380#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100381#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000382 /*
383 * MS-Windows allows opening a device, but we will probably get stuck
384 * trying to read it.
385 */
386 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
387 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000388 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000389 msg_end();
390 msg_scroll = msg_save;
391 return FAIL;
392 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000393#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200394 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000395
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100396 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200397 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398
399 /*
400 * When opening a new file we take the readonly flag from the file.
401 * Default is r/w, can be set to r/o below.
402 * Don't reset it when in readonly mode
403 * Only set/reset b_p_ro when BF_CHECK_RO is set.
404 */
405 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000406 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 curbuf->b_p_ro = FALSE;
408
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200409 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 if (mch_stat((char *)fname, &st) >= 0)
413 {
414 buf_store_time(curbuf, &st, fname);
415 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100416 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200417 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#ifdef UNIX
419 /*
420 * Use the protection bits of the original file for the swap file.
421 * This makes it possible for others to read the name of the
422 * edited file from the swapfile, but only if they can read the
423 * edited file.
424 * Remove the "write" and "execute" bits for group and others
425 * (they must not write the swapfile).
426 * Add the "read" and "write" bits for the user, otherwise we may
427 * not be able to write to the file ourselves.
428 * Setting the bits is done below, after creating the swap file.
429 */
430 swap_mode = (st.st_mode & 0644) | 0600;
431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef VMS
433 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000434 curbuf->b_fab_rat = st.st_fab_rat;
435 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
437 }
438 else
439 {
440 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100441 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100443 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 curbuf->b_orig_size = 0;
445 curbuf->b_orig_mode = 0;
446 }
447
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100448 // Reset the "new file" flag. It will be set again below when the
449 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
451 }
452
453/*
454 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100455 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 */
457 file_readonly = FALSE;
458 if (read_stdin)
459 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100460#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100461 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 setmode(0, O_BINARY);
463#endif
464 }
465 else if (!read_buffer)
466 {
467#ifdef USE_MCH_ACCESS
468 if (
469# ifdef UNIX
470 !(perm & 0222) ||
471# endif
472 mch_access((char *)fname, W_OK))
473 file_readonly = TRUE;
474 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
475#else
476 if (!newfile
477 || readonlymode
478 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
479 {
480 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100481 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
483 }
484#endif
485 }
486
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100487 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
489#ifndef UNIX
490 int isdir_f;
491#endif
492 msg_scroll = msg_save;
493#ifndef UNIX
494 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100495 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 */
497 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (isdir_f)
500 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200501 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100502 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
504 else
505#endif
506 if (newfile)
507 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200508 if (perm < 0
509#ifdef ENOENT
510 && errno == ENOENT
511#endif
512 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 {
514 /*
515 * Set the 'new-file' flag, so that when the file has
516 * been created by someone else, a ":w" will complain.
517 */
518 curbuf->b_flags |= BF_NEW;
519
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100520 // Create a swap file now, so that other Vims are warned
521 // that we are editing this file. Don't do this for a
522 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100526 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000527 if (curbuf != old_curbuf
528 || (using_b_ffname
529 && (old_b_ffname != curbuf->b_ffname))
530 || (using_b_fname
531 && (old_b_fname != curbuf->b_fname)))
532 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000533 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000534 return FAIL;
535 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000536 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000537 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200538 filemess(curbuf, sfname,
539 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000540 else
541 filemess(curbuf, sfname,
542 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 // Even though this is a new file, it might have been
545 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 check_marks_read();
547#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100548 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200549 if (eap != NULL)
550 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
552 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100553 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 save_file_ff(curbuf);
555
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100556#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100557 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 return FAIL;
559#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100560 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
562 else
563 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000564 filemess(curbuf, sfname, (char_u *)(
565# ifdef EFBIG
566 (errno == EFBIG) ? _("[File too big]") :
567# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200568# ifdef EOVERFLOW
569 (errno == EOVERFLOW) ? _("[File too big]") :
570# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000571 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100572 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574 }
575
576 return FAIL;
577 }
578
579 /*
580 * Only set the 'ro' flag for readonly files the first time they are
581 * loaded. Help files always get readonly mode
582 */
583 if ((check_readonly && file_readonly) || curbuf->b_help)
584 curbuf->b_p_ro = TRUE;
585
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000586 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100588 // Don't change 'eol' if reading from buffer as it will already be
589 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000590 if (!read_buffer)
591 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100592 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100593 curbuf->b_start_eof = FALSE;
594 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000595 curbuf->b_start_eol = TRUE;
596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000598 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100601 // Create a swap file now, so that other Vims are warned that we are
602 // editing this file.
603 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {
606 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000607 if (!read_stdin && (curbuf != old_curbuf
608 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
609 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
610 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000611 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000612 if (!read_buffer)
613 close(fd);
614 return FAIL;
615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100617 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000618 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
619 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100620 {
621 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
622
623 /*
624 * If the group-read bit is set but not the world-read bit, then
625 * the group must be equal to the group of the original file. If
626 * we can't make that happen then reset the group-read bit. This
627 * avoids making the swap file readable to more users when the
628 * primary group of the user is too permissive.
629 */
630 if ((swap_mode & 044) == 040)
631 {
632 stat_T swap_st;
633
634 if (mch_stat((char *)swap_fname, &swap_st) >= 0
635 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200636# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100637 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200638 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200639# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200640 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100641 swap_mode &= 0600;
642 }
643
644 (void)mch_setperm(swap_fname, (long)swap_mode);
645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#endif
647 }
648
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200649 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (swap_exists_action == SEA_QUIT)
651 {
652 if (!read_buffer && !read_stdin)
653 close(fd);
654 return FAIL;
655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100657 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
659 /*
660 * Set '[ mark to the line above where the lines go (line 1 if zero).
661 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100662 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
664 curbuf->b_op_start.col = 0;
665
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100666 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
667 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
668 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (!read_buffer)
671 {
672 int m = msg_scroll;
673 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675 /*
676 * The file must be closed again, the autocommands may want to change
677 * the file before reading it.
678 */
679 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 /*
683 * The output from the autocommands should not overwrite anything and
684 * should not be overwritten: Set msg_scroll, restore its value if no
685 * output was done.
686 */
687 msg_scroll = TRUE;
688 if (filtering)
689 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
690 FALSE, curbuf, eap);
691 else if (read_stdin)
692 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
693 FALSE, curbuf, eap);
694 else if (newfile)
695 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
696 FALSE, curbuf, eap);
697 else
698 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
699 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100701 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
702 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
703 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100704 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (msg_scrolled == n)
707 msg_scroll = m;
708
709#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
712 --no_wait_return;
713 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100714 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 return FAIL;
716 }
717#endif
718 /*
719 * Don't allow the autocommands to change the current buffer.
720 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000721 *
722 * Don't allow the autocommands to change the buffer name either
723 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
725 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000726 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
727 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
729 {
730 --no_wait_return;
731 msg_scroll = msg_save;
732 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000733 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000735 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100736 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 return FAIL;
738 }
739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100741 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
743
744 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
745 {
746 /*
747 * Show the user that we are busy reading the input. Sometimes this
748 * may take a while. When reading from stdin another program may
749 * still be running, don't move the cursor to the last line, unless
750 * always using the GUI.
751 */
752 if (read_stdin)
753 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100754 if (!is_not_a_term())
755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200757# ifdef VIMDLL
758 if (!gui.in_use)
759# endif
760 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761#endif
762#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100763 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100764 if (gui.in_use && !gui.dying && !gui.starting)
765 {
Amon Sha10197932022-02-21 15:07:12 +0000766 // make a copy, gui_write() may try to change it
767 p = vim_strsave((char_u *)_("Reading from stdin..."));
768 if (p != NULL)
769 {
770 gui_write(p, (int)STRLEN(p));
771 vim_free(p);
772 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 }
777 else if (!read_buffer)
778 filemess(curbuf, sfname, (char_u *)"", 0);
779 }
780
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100781 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
783 /*
784 * Set linecnt now, before the "retry" caused by a wrong guess for
785 * fileformat, and after the autocommands, which may change them.
786 */
787 linecnt = curbuf->b_ml.ml_line_count;
788
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000790 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000791 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000792 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000793 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000794 curbuf->b_bad_char = eap->bad_char;
795 }
796 else
797 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000800 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
802 if (eap != NULL && eap->force_enc != 0)
803 {
804 fenc = enc_canonize(eap->cmd + eap->force_enc);
805 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000806 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
808 else if (curbuf->b_p_bin)
809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 fenc_alloced = FALSE;
812 }
813 else if (curbuf->b_help)
814 {
815 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000816 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100818 // Help files are either utf-8 or latin1. Try utf-8 first, if this
819 // fails it must be latin1.
820 // Always do this when 'encoding' is "utf-8". Otherwise only do
821 // this when needed to avoid [converted] remarks all the time.
822 // It is needed when the first line contains non-ASCII characters.
823 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 fenc = (char_u *)"latin1";
825 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000826 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000828 fc = fname[STRLEN(fname) - 1];
829 if (TOLOWER_ASC(fc) == 'x')
830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Read the first line (and a bit more). Immediately rewind to
832 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100833 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200834 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000835 for (p = firstline; p < firstline + len; ++p)
836 if (*p >= 0x80)
837 {
838 c = TRUE;
839 break;
840 }
841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843
844 if (c)
845 {
846 fenc_next = fenc;
847 fenc = (char_u *)"utf-8";
848
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 // When the file is utf-8 but a character doesn't fit in
850 // 'encoding' don't retry. In help text editing utf-8 bytes
851 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000852 if (!enc_utf8)
853 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 fenc_alloced = FALSE;
856 }
857 else if (*p_fencs == NUL)
858 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 fenc_alloced = FALSE;
861 }
862 else
863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100864 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200865 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868 /*
869 * Jump back here to retry reading the file in different ways.
870 * Reasons to retry:
871 * - encoding conversion failed: try another one from "fenc_next"
872 * - BOM detected and fenc was set, need to setup conversion
873 * - "fileformat" check failed: try another
874 *
875 * Variables set for special retry actions:
876 * "file_rewind" Rewind the file to start reading it again.
877 * "advance_fenc" Advance "fenc" using "fenc_next".
878 * "skip_read" Re-use already read bytes (BOM detected).
879 * "did_iconv" iconv() conversion failed, try 'charconvert'.
880 * "keep_fileformat" Don't reset "fileformat".
881 *
882 * Other status indicators:
883 * "tmpname" When != NULL did conversion with 'charconvert'.
884 * Output file has to be deleted afterwards.
885 * "iconv_fd" When != -1 did conversion with iconv().
886 */
887retry:
888
889 if (file_rewind)
890 {
891 if (read_buffer)
892 {
893 read_buf_lnum = 1;
894 read_buf_col = 0;
895 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200896 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 error = TRUE;
900 goto failed;
901 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100902 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200904 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000906 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000909 curbuf->b_start_bomb = FALSE;
910 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000911 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913
914 /*
915 * When retrying with another "fenc" and the first time "fileformat"
916 * will be reset.
917 */
918 if (keep_fileformat)
919 keep_fileformat = FALSE;
920 else
921 {
922 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000925 try_unix = try_dos = try_mac = FALSE;
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100928 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100930 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100932 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934
Bram Moolenaar13505972019-01-24 15:04:48 +0100935#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (iconv_fd != (iconv_t)-1)
937 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100938 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 iconv_close(iconv_fd);
940 iconv_fd = (iconv_t)-1;
941 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
944 if (advance_fenc)
945 {
946 /*
947 * Try the next entry in 'fileencodings'.
948 */
949 advance_fenc = FALSE;
950
951 if (eap != NULL && eap->force_enc != 0)
952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100953 // Conversion given with "++cc=" wasn't possible, read
954 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000956 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 if (fenc_alloced)
958 vim_free(fenc);
959 fenc = (char_u *)"";
960 fenc_alloced = FALSE;
961 }
962 else
963 {
964 if (fenc_alloced)
965 vim_free(fenc);
966 if (fenc_next != NULL)
967 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200968 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 else
971 {
972 fenc = (char_u *)"";
973 fenc_alloced = FALSE;
974 }
975 }
976 if (tmpname != NULL)
977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100978 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100979 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981 }
982
983 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000984 * Conversion may be required when the encoding of the file is different
985 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 */
987 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000988 converted = need_conversion(fenc);
989 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
991
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100992 // "ucs-bom" means we need to check the first bytes of the file
993 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (STRCMP(fenc, ENC_UCSBOM) == 0)
995 fio_flags = FIO_UCSBOM;
996
997 /*
998 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
999 * done. This is handled below after read(). Prepare the
1000 * fio_flags to avoid having to parse the string each time.
1001 * Also check for Unicode to Latin1 conversion, because iconv()
1002 * appears not to handle this correctly. This works just like
1003 * conversion to UTF-8 except how the resulting character is put in
1004 * the buffer.
1005 */
1006 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1007 fio_flags = get_fio_flags(fenc);
1008
Bram Moolenaar4f974752019-02-17 17:44:42 +01001009#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1012 * is handled with MultiByteToWideChar().
1013 */
1014 if (fio_flags == 0)
1015 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar13505972019-01-24 15:04:48 +01001018#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001019 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (fio_flags == 0)
1021 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar13505972019-01-24 15:04:48 +01001024#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 /*
1026 * Try using iconv() if we can't convert internally.
1027 */
1028 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001029# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 )
1033 iconv_fd = (iconv_t)my_iconv_open(
1034 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar13505972019-01-24 15:04:48 +01001037#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 /*
1039 * Use the 'charconvert' expression when conversion is required
1040 * and we can't do it internally or with iconv().
1041 */
1042 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001043 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001044# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 )
1048 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001049# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001051# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 // Skip conversion when it's already done (retry for wrong
1053 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (tmpname == NULL)
1055 {
1056 tmpname = readfile_charconvert(fname, fenc, &fd);
1057 if (tmpname == NULL)
1058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 advance_fenc = TRUE;
1061 if (fd < 0)
1062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001064 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 error = TRUE;
1066 goto failed;
1067 }
1068 goto retry;
1069 }
1070 }
1071 }
1072 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001076#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 )
1080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001081 // Conversion wanted but we can't.
1082 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 advance_fenc = TRUE;
1084 goto retry;
1085 }
1086 }
1087 }
1088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001089 // Set "can_retry" when it's possible to rewind the file and try with
1090 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1091 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001092 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 if (!skip_read)
1095 {
1096 linerest = 0;
1097 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001098 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 skip_count = lines_to_skip;
1100 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001102#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001103 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1104 && curbuf->b_ffname != NULL
1105 && curbuf->b_p_udf
1106 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001107 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001108 && !read_stdin
1109 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001110 if (read_undo_file)
1111 sha256_start(&sha_ctx);
1112#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001113#ifdef FEAT_CRYPT
1114 if (curbuf->b_cryptstate != NULL)
1115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001116 // Need to free the state, but keep the key, don't want to ask for
1117 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001118 crypt_free_state(curbuf->b_cryptstate);
1119 curbuf->b_cryptstate = NULL;
1120 }
1121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123
1124 while (!error && !got_int)
1125 {
1126 /*
1127 * We allocate as much space for the file as we can get, plus
1128 * space for the old line plus room for one terminating NUL.
1129 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001130 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001132 if (!skip_read)
1133 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001134#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001136#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Use buffer >= 64K. Add linerest to double the size if the
1138 // line gets very long, to avoid a lot of copying. But don't
1139 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001140 size = 0x10000L + linerest;
1141 if (size > 0x100000L)
1142 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001143#endif
1144 }
1145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001147 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 size = 1;
1152 }
1153 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 if (!skip_read)
1156 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001157 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001159 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 FALSE)) != NULL)
1161 break;
1162 }
1163 if (new_buffer == NULL)
1164 {
1165 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1166 error = TRUE;
1167 break;
1168 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001169 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1171 vim_free(buffer);
1172 buffer = new_buffer;
1173 ptr = buffer + linerest;
1174 line_start = buffer;
1175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001176 // May need room to translate into.
1177 // For iconv() we don't really know the required space, use a
1178 // factor ICONV_MULT.
1179 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1180 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1181 // become up to 4 bytes, size must be multiple of 2
1182 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1183 // multiple of 2
1184 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1185 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001186 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001187#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (iconv_fd != (iconv_t)-1)
1189 size = size / ICONV_MULT;
1190 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (fio_flags & FIO_LATIN1)
1193 size = size / 2;
1194 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1195 size = (size * 2 / 3) & ~1;
1196 else if (fio_flags & FIO_UCS4)
1197 size = (size * 2 / 3) & ~3;
1198 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001199 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001200#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001202 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001203#endif
1204#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001206 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (conv_restlen > 0)
1210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001211 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 mch_memmove(ptr, conv_rest, conv_restlen);
1213 ptr += conv_restlen;
1214 size -= conv_restlen;
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217 if (read_buffer)
1218 {
1219 /*
1220 * Read bytes from curbuf. Used for converting text read
1221 * from stdin.
1222 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001223 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (read_buf_lnum > from)
1225 size = 0;
1226 else
1227 {
1228 int n, ni;
1229 long tlen;
1230
1231 tlen = 0;
1232 for (;;)
1233 {
1234 p = ml_get(read_buf_lnum) + read_buf_col;
1235 n = (int)STRLEN(p);
1236 if ((int)tlen + n + 1 > size)
1237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001238 // Filled up to "size", append partial line.
1239 // Change NL to NUL to reverse the effect done
1240 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001241 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 for (ni = 0; ni < n; ++ni)
1243 {
1244 if (p[ni] == NL)
1245 ptr[tlen++] = NUL;
1246 else
1247 ptr[tlen++] = p[ni];
1248 }
1249 read_buf_col += n;
1250 break;
1251 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001252
1253 // Append whole line and new-line. Change NL
1254 // to NUL to reverse the effect done below.
1255 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001257 if (p[ni] == NL)
1258 ptr[tlen++] = NUL;
1259 else
1260 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001262 ptr[tlen++] = NL;
1263 read_buf_col = 0;
1264 if (++read_buf_lnum > from)
1265 {
1266 // When the last line didn't have an
1267 // end-of-line don't add it now either.
1268 if (!curbuf->b_p_eol)
1269 --tlen;
1270 size = tlen;
1271 eof = TRUE;
1272 break;
1273 }
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 }
1277 }
1278 else
1279 {
1280 /*
1281 * Read bytes from the file.
1282 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001283# ifdef FEAT_SODIUM
1284 // Let the crypt layer work with a buffer size of 8192
1285 if (filesize == 0)
1286 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001287 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001288 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1289 + crypto_secretstream_xchacha20poly1305_ABYTES;
1290
1291 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1292 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1293 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1294# endif
1295 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001296 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001297 filesize_count += size;
1298 // hit end of file
1299 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001302#ifdef FEAT_CRYPT
1303 /*
1304 * At start of file: Check for magic number of encryption.
1305 */
1306 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001307 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1309 &filesize, newfile, sfname,
1310 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001311# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001312 if (curbuf->b_cryptstate != NULL
1313 && !crypt_works_inplace(curbuf->b_cryptstate))
1314 // reading undo file requires crypt_decode_inplace()
1315 read_undo_file = FALSE;
1316# endif
1317 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 /*
1319 * Decrypt the read bytes. This is done before checking for
1320 * EOF because the crypt layer may be buffering.
1321 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001322 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1323 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001324 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001325# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 if (crypt_works_inplace(curbuf->b_cryptstate))
1327 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001328# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001329 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1330 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001331# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001332 }
1333 else
1334 {
1335 char_u *newptr = NULL;
1336 int decrypted_size;
1337
1338 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001339 curbuf->b_cryptstate, ptr, size,
1340 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001341
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001342 if (decrypted_size < 0)
1343 {
1344 // error message already given
1345 error = TRUE;
1346 vim_free(newptr);
1347 break;
1348 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001349 // If the crypt layer is buffering, not producing
1350 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001351 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001352 continue;
1353
1354 if (linerest == 0)
1355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001356 // Simple case: reuse returned buffer (may be
1357 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001358 new_buffer = newptr;
1359 }
1360 else
1361 {
1362 long_u new_size;
1363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001364 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001365 new_size = (long_u)(decrypted_size + linerest + 1);
1366 new_buffer = lalloc(new_size, FALSE);
1367 if (new_buffer == NULL)
1368 {
1369 do_outofmem_msg(new_size);
1370 error = TRUE;
1371 break;
1372 }
1373
1374 mch_memmove(new_buffer, buffer, linerest);
1375 if (newptr != NULL)
1376 mch_memmove(new_buffer + linerest, newptr,
1377 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001378 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001379 }
1380
1381 if (new_buffer != NULL)
1382 {
1383 vim_free(buffer);
1384 buffer = new_buffer;
1385 new_buffer = NULL;
1386 line_start = buffer;
1387 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001388 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001389 }
1390 size = decrypted_size;
1391 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001392# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001393 }
1394#endif
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (size <= 0)
1397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001398 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001401 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001402 /*
1403 * Reached end-of-file but some trailing bytes could
1404 * not be converted. Truncated file?
1405 */
1406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001407 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001408 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001409#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001410 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001411#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001412 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001413 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001414 if (can_retry)
1415 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001416 if (conv_error == 0)
1417 conv_error = curbuf->b_ml.ml_line_count
1418 - linecnt + 1;
1419 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001420 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001421 else if (illegal_byte == 0)
1422 illegal_byte = curbuf->b_ml.ml_line_count
1423 - linecnt + 1;
1424 if (bad_char_behavior == BAD_DROP)
1425 {
1426 *(ptr - conv_restlen) = NUL;
1427 conv_restlen = 0;
1428 }
1429 else
1430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 // Replace the trailing bytes with the replacement
1432 // character if we were converting; if we weren't,
1433 // leave the UTF8 checking code to do it, as it
1434 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001435 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001436#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001437 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001438#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001439 ))
1440 {
1441 while (conv_restlen > 0)
1442 {
1443 *(--ptr) = bad_char_behavior;
1444 --conv_restlen;
1445 }
1446 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001447 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001448#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001449 if (iconv_fd != (iconv_t)-1)
1450 {
1451 iconv_close(iconv_fd);
1452 iconv_fd = (iconv_t)-1;
1453 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001454#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001455 }
1456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459 skip_read = FALSE;
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 /*
1462 * At start of file (or after crypt magic number): Check for BOM.
1463 * Also check for a BOM for other Unicode encodings, but not after
1464 * converting with 'charconvert' or when a BOM has already been
1465 * found.
1466 */
1467 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001468#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001469 || (cryptkey != NULL
1470 && filesize == crypt_get_header_len(
1471 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 )
1474 && (fio_flags == FIO_UCSBOM
1475 || (!curbuf->b_p_bomb
1476 && tmpname == NULL
1477 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1478 {
1479 char_u *ccname;
1480 int blen;
1481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001482 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (size < 2 || curbuf->b_p_bin)
1484 ccname = NULL;
1485 else
1486 ccname = check_for_bom(ptr, size, &blen,
1487 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1488 if (ccname != NULL)
1489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001490 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 filesize += blen;
1492 size -= blen;
1493 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001494 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001497 curbuf->b_start_bomb = TRUE;
1498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500
1501 if (fio_flags == FIO_UCSBOM)
1502 {
1503 if (ccname == NULL)
1504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001505 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 advance_fenc = TRUE;
1507 }
1508 else
1509 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001510 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (fenc_alloced)
1512 vim_free(fenc);
1513 fenc = ccname;
1514 fenc_alloced = FALSE;
1515 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 skip_read = TRUE;
1518 goto retry;
1519 }
1520 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001523 ptr -= conv_restlen;
1524 size += conv_restlen;
1525 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /*
1527 * Break here for a read error or end-of-file.
1528 */
1529 if (size <= 0)
1530 break;
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaar13505972019-01-24 15:04:48 +01001533#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (iconv_fd != (iconv_t)-1)
1535 {
1536 /*
1537 * Attempt conversion of the read bytes to 'encoding' using
1538 * iconv().
1539 */
1540 const char *fromp;
1541 char *top;
1542 size_t from_size;
1543 size_t to_size;
1544
1545 fromp = (char *)ptr;
1546 from_size = size;
1547 ptr += size;
1548 top = (char *)ptr;
1549 to_size = real_size - size;
1550
1551 /*
1552 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001553 * another conversion. Except for when there is no
1554 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001556 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1557 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1559 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001560 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001561 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001562 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001563 if (conv_error == 0)
1564 conv_error = readfile_linenr(linecnt,
1565 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001567 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001568 ++fromp;
1569 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001570 if (bad_char_behavior == BAD_KEEP)
1571 {
1572 *top++ = *(fromp - 1);
1573 --to_size;
1574 }
1575 else if (bad_char_behavior != BAD_DROP)
1576 {
1577 *top++ = bad_char_behavior;
1578 --to_size;
1579 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 if (from_size > 0)
1583 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001584 // Some remaining characters, keep them for the next
1585 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1587 conv_restlen = (int)from_size;
1588 }
1589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001590 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 line_start = ptr - linerest;
1592 mch_memmove(line_start, buffer, (size_t)linerest);
1593 size = (long)((char_u *)top - ptr);
1594 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaar4f974752019-02-17 17:44:42 +01001597#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (fio_flags & FIO_CODEPAGE)
1599 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001600 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001601 WCHAR ucs2buf[3];
1602 int ucs2len;
1603 int codepage = FIO_GET_CP(fio_flags);
1604 int bytelen;
1605 int found_bad;
1606 char replstr[2];
1607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /*
1609 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001610 * a codepage, using standard MS-Windows functions. This
1611 * requires two steps:
1612 * 1. convert from 'fileencoding' to ucs-2
1613 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001615 * Because there may be illegal bytes AND an incomplete byte
1616 * sequence at the end, we may have to do the conversion one
1617 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001620 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001621 if (bad_char_behavior > 0)
1622 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001624 replstr[0] = '?';
1625 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001628 * Move the bytes to the end of the buffer, so that we have
1629 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001631 src = ptr + real_size - size;
1632 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001634 /*
1635 * Do the conversion.
1636 */
1637 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001638 while (size > 0)
1639 {
1640 found_bad = FALSE;
1641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001642# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001643 if (codepage == CP_UTF8)
1644 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001645 // Handle CP_UTF8 input ourselves to be able to handle
1646 // trailing bytes properly.
1647 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001648 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001649 if (bytelen > size)
1650 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001651 // Only got some bytes of a character. Normally
1652 // it's put in "conv_rest", but if it's too long
1653 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001654 if (bytelen <= CONV_RESTLEN)
1655 break;
1656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001657 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001658 bytelen = size;
1659 found_bad = TRUE;
1660 }
1661 else
1662 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001663 int u8c = utf_ptr2char(src);
1664
Bram Moolenaar86e01082005-12-29 22:45:34 +00001665 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001666 found_bad = TRUE;
1667 ucs2buf[0] = u8c;
1668 ucs2len = 1;
1669 }
1670 }
1671 else
1672# endif
1673 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001674 // We don't know how long the byte sequence is, try
1675 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001676 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1677 ++bytelen)
1678 {
1679 ucs2len = MultiByteToWideChar(codepage,
1680 MB_ERR_INVALID_CHARS,
1681 (LPCSTR)src, bytelen,
1682 ucs2buf, 3);
1683 if (ucs2len > 0)
1684 break;
1685 }
1686 if (ucs2len == 0)
1687 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001688 // If we have only one byte then it's probably an
1689 // incomplete byte sequence. Otherwise discard
1690 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001691 if (size == 1)
1692 break;
1693 found_bad = TRUE;
1694 bytelen = 1;
1695 }
1696 }
1697
1698 if (!found_bad)
1699 {
1700 int i;
1701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001702 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001703 if (enc_utf8)
1704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001705 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001706 for (i = 0; i < ucs2len; ++i)
1707 dst += utf_char2bytes(ucs2buf[i], dst);
1708 }
1709 else
1710 {
1711 BOOL bad = FALSE;
1712 int dstlen;
1713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001714 // From UCS-2 to "enc_codepage". If the
1715 // conversion uses the default character "?",
1716 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001717 dstlen = WideCharToMultiByte(enc_codepage, 0,
1718 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001719 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001720 replstr, &bad);
1721 if (bad)
1722 found_bad = TRUE;
1723 else
1724 dst += dstlen;
1725 }
1726 }
1727
1728 if (found_bad)
1729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001731 if (can_retry)
1732 goto rewind_retry;
1733 if (conv_error == 0)
1734 conv_error = readfile_linenr(linecnt, ptr, dst);
1735 if (bad_char_behavior != BAD_DROP)
1736 {
1737 if (bad_char_behavior == BAD_KEEP)
1738 {
1739 mch_memmove(dst, src, bytelen);
1740 dst += bytelen;
1741 }
1742 else
1743 *dst++ = bad_char_behavior;
1744 }
1745 }
1746
1747 src += bytelen;
1748 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001750
1751 if (size > 0)
1752 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001754 mch_memmove(conv_rest, src, size);
1755 conv_restlen = size;
1756 }
1757
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001759 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001762#endif
1763#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (fio_flags & FIO_MACROMAN)
1765 {
1766 /*
1767 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001768 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001770 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
1773 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (fio_flags != 0)
1776 {
1777 int u8c;
1778 char_u *dest;
1779 char_u *tail = NULL;
1780
1781 /*
1782 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1783 * "enc_utf8" not set: Convert Unicode to Latin1.
1784 * Go from end to start through the buffer, because the number
1785 * of bytes may increase.
1786 * "dest" points to after where the UTF-8 bytes go, "p" points
1787 * to after the next character to convert.
1788 */
1789 dest = ptr + real_size;
1790 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1791 {
1792 p = ptr + size;
1793 if (fio_flags == FIO_UTF8)
1794 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001795 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 tail = ptr + size - 1;
1797 while (tail > ptr && (*tail & 0xc0) == 0x80)
1798 --tail;
1799 if (tail + utf_byte2len(*tail) <= ptr + size)
1800 tail = NULL;
1801 else
1802 p = tail;
1803 }
1804 }
1805 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001807 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 p = ptr + (size & ~1);
1809 if (size & 1)
1810 tail = p;
1811 if ((fio_flags & FIO_UTF16) && p > ptr)
1812 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001813 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (fio_flags & FIO_ENDIAN_L)
1815 {
1816 u8c = (*--p << 8);
1817 u8c += *--p;
1818 }
1819 else
1820 {
1821 u8c = *--p;
1822 u8c += (*--p << 8);
1823 }
1824 if (u8c >= 0xd800 && u8c <= 0xdbff)
1825 tail = p;
1826 else
1827 p += 2;
1828 }
1829 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001830 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001832 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 p = ptr + (size & ~3);
1834 if (size & 3)
1835 tail = p;
1836 }
1837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001838 // If there is a trailing incomplete sequence move it to
1839 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (tail != NULL)
1841 {
1842 conv_restlen = (int)((ptr + size) - tail);
1843 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1844 size -= conv_restlen;
1845 }
1846
1847
1848 while (p > ptr)
1849 {
1850 if (fio_flags & FIO_LATIN1)
1851 u8c = *--p;
1852 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1853 {
1854 if (fio_flags & FIO_ENDIAN_L)
1855 {
1856 u8c = (*--p << 8);
1857 u8c += *--p;
1858 }
1859 else
1860 {
1861 u8c = *--p;
1862 u8c += (*--p << 8);
1863 }
1864 if ((fio_flags & FIO_UTF16)
1865 && u8c >= 0xdc00 && u8c <= 0xdfff)
1866 {
1867 int u16c;
1868
1869 if (p == ptr)
1870 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (can_retry)
1873 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001874 if (conv_error == 0)
1875 conv_error = readfile_linenr(linecnt,
1876 ptr, p);
1877 if (bad_char_behavior == BAD_DROP)
1878 continue;
1879 if (bad_char_behavior != BAD_KEEP)
1880 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001883 // found second word of double-word, get the first
1884 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (fio_flags & FIO_ENDIAN_L)
1886 {
1887 u16c = (*--p << 8);
1888 u16c += *--p;
1889 }
1890 else
1891 {
1892 u16c = *--p;
1893 u16c += (*--p << 8);
1894 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001895 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1896 + (u8c & 0x3ff);
1897
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001898 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (u16c < 0xd800 || u16c > 0xdbff)
1900 {
1901 if (can_retry)
1902 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001903 if (conv_error == 0)
1904 conv_error = readfile_linenr(linecnt,
1905 ptr, p);
1906 if (bad_char_behavior == BAD_DROP)
1907 continue;
1908 if (bad_char_behavior != BAD_KEEP)
1909 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 }
1913 else if (fio_flags & FIO_UCS4)
1914 {
1915 if (fio_flags & FIO_ENDIAN_L)
1916 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001917 u8c = (unsigned)*--p << 24;
1918 u8c += (unsigned)*--p << 16;
1919 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 u8c += *--p;
1921 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001922 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
1924 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001925 u8c += (unsigned)*--p << 8;
1926 u8c += (unsigned)*--p << 16;
1927 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 if (*--p < 0x80)
1933 u8c = *p;
1934 else
1935 {
1936 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001937 p -= len;
1938 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (len == 0)
1940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001941 // Not a valid UTF-8 character, retry with
1942 // another fenc when possible, otherwise just
1943 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (can_retry)
1945 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001946 if (conv_error == 0)
1947 conv_error = readfile_linenr(linecnt,
1948 ptr, p);
1949 if (bad_char_behavior == BAD_DROP)
1950 continue;
1951 if (bad_char_behavior != BAD_KEEP)
1952 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
1955 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001956 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 dest -= utf_char2len(u8c);
1959 (void)utf_char2bytes(u8c, dest);
1960 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001961 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 --dest;
1964 if (u8c >= 0x100)
1965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // character doesn't fit in latin1, retry with
1967 // another fenc when possible, otherwise just
1968 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001969 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001971 if (conv_error == 0)
1972 conv_error = readfile_linenr(linecnt, ptr, p);
1973 if (bad_char_behavior == BAD_DROP)
1974 ++dest;
1975 else if (bad_char_behavior == BAD_KEEP)
1976 *dest = u8c;
1977 else if (eap != NULL && eap->bad_char != 0)
1978 *dest = bad_char_behavior;
1979 else
1980 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 else
1983 *dest = u8c;
1984 }
1985 }
1986
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001987 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 line_start = dest - linerest;
1989 mch_memmove(line_start, buffer, (size_t)linerest);
1990 size = (long)((ptr + real_size) - dest);
1991 ptr = dest;
1992 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001993 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001995 int incomplete_tail = FALSE;
1996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001998 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002000 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002001 int l;
2002
2003 if (todo <= 0)
2004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (*p >= 0x80)
2006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002007 // A length of 1 means it's an illegal byte. Accept
2008 // an incomplete character at the end though, the next
2009 // read() will get the next bytes, we'll check it
2010 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002011 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002012 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 // Avoid retrying with a different encoding when
2015 // a truncated file is more likely, or attempting
2016 // to read the rest of an incomplete sequence when
2017 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002018 if (p > ptr || filesize > 0)
2019 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // Incomplete byte sequence, move it to conv_rest[]
2021 // and try to read the rest of it, unless we've
2022 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002023 if (p > ptr)
2024 {
2025 conv_restlen = todo;
2026 mch_memmove(conv_rest, p, conv_restlen);
2027 size -= conv_restlen;
2028 break;
2029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002031 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // Illegal byte. If we can try another encoding
2034 // do that, unless at EOF where a truncated
2035 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002036 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002037 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002038#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002040 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2041 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002042#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002044 if (conv_error == 0 && illegal_byte == 0)
2045 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002047 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002048 if (bad_char_behavior == BAD_DROP)
2049 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002050 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002051 --p;
2052 --size;
2053 }
2054 else if (bad_char_behavior != BAD_KEEP)
2055 *p = bad_char_behavior;
2056 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002057 else
2058 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002061 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002066#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002067 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002068 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002069 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002071#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002073 advance_fenc = TRUE;
2074 file_rewind = TRUE;
2075 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002079 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 filesize += size;
2081
2082 /*
2083 * when reading the first part of a file: guess EOL type
2084 */
2085 if (fileformat == EOL_UNKNOWN)
2086 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002087 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (try_dos || try_unix)
2089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002090 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002091 if (try_mac)
2092 try_mac = 1;
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 for (p = ptr; p < ptr + size; ++p)
2095 {
2096 if (*p == NL)
2097 {
2098 if (!try_unix
2099 || (try_dos && p > ptr && p[-1] == CAR))
2100 fileformat = EOL_DOS;
2101 else
2102 fileformat = EOL_UNIX;
2103 break;
2104 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002105 else if (*p == CAR && try_mac)
2106 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (fileformat == EOL_UNIX && try_mac)
2111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002112 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 try_mac = 1;
2114 try_unix = 1;
2115 for (; p >= ptr && *p != CAR; p--)
2116 ;
2117 if (p >= ptr)
2118 {
2119 for (p = ptr; p < ptr + size; ++p)
2120 {
2121 if (*p == NL)
2122 try_unix++;
2123 else if (*p == CAR)
2124 try_mac++;
2125 }
2126 if (try_mac > try_unix)
2127 fileformat = EOL_MAC;
2128 }
2129 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002130 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 // Looking for CR but found no end-of-line markers at
2132 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002133 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002136 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (fileformat == EOL_UNKNOWN && try_mac)
2138 fileformat = EOL_MAC;
2139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (fileformat == EOL_UNKNOWN)
2142 fileformat = default_fileformat();
2143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002144 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 set_fileformat(fileformat, OPT_LOCAL);
2147 }
2148 }
2149
2150 /*
2151 * This loop is executed once for every character read.
2152 * Keep it fast!
2153 */
2154 if (fileformat == EOL_MAC)
2155 {
2156 --ptr;
2157 while (++ptr, --size >= 0)
2158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002159 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if ((c = *ptr) != NUL && c != CAR && c != NL)
2161 continue;
2162 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002165 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else
2167 {
2168 if (skip_count == 0)
2169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 len = (colnr_T) (ptr - line_start + 1);
2172 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2173 {
2174 error = TRUE;
2175 break;
2176 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002177#ifdef FEAT_PERSISTENT_UNDO
2178 if (read_undo_file)
2179 sha256_update(&sha_ctx, line_start, len);
2180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 ++lnum;
2182 if (--read_count == 0)
2183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002184 error = TRUE; // break loop
2185 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 break;
2187 }
2188 }
2189 else
2190 --skip_count;
2191 line_start = ptr + 1;
2192 }
2193 }
2194 }
2195 else
2196 {
2197 --ptr;
2198 while (++ptr, --size >= 0)
2199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 continue;
2202 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 else
2205 {
2206 if (skip_count == 0)
2207 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 len = (colnr_T)(ptr - line_start + 1);
2210 if (fileformat == EOL_DOS)
2211 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002212 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002214 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 ptr[-1] = NUL;
2216 --len;
2217 }
2218 /*
2219 * Reading in Dos format, but no CR-LF found!
2220 * When 'fileformats' includes "unix", delete all
2221 * the lines read so far and start all over again.
2222 * Otherwise give an error message later.
2223 */
2224 else if (ff_error != EOL_DOS)
2225 {
2226 if ( try_unix
2227 && !read_stdin
2228 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002229 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2230 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 set_fileformat(EOL_UNIX, OPT_LOCAL);
2235 file_rewind = TRUE;
2236 keep_fileformat = TRUE;
2237 goto retry;
2238 }
2239 ff_error = EOL_DOS;
2240 }
2241 }
2242 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2243 {
2244 error = TRUE;
2245 break;
2246 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002247#ifdef FEAT_PERSISTENT_UNDO
2248 if (read_undo_file)
2249 sha256_update(&sha_ctx, line_start, len);
2250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 ++lnum;
2252 if (--read_count == 0)
2253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002254 error = TRUE; // break loop
2255 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 break;
2257 }
2258 }
2259 else
2260 --skip_count;
2261 line_start = ptr + 1;
2262 }
2263 }
2264 }
2265 linerest = (long)(ptr - line_start);
2266 ui_breakcheck();
2267 }
2268
2269failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (error && read_count == 0)
2272 error = FALSE;
2273
K.Takata3af98212022-11-01 20:36:19 +00002274 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2275 // In old days the file length was in sector count and the CTRL-Z the
2276 // marker where the file really ended. Assuming we write it to a file
2277 // system that keeps file length properly the CTRL-Z should be dropped.
2278 // Set the 'endoffile' option so the user can decide what to write later.
2279 // In Unix format the CTRL-Z is just another character.
2280 if (linerest != 0
2281 && !curbuf->b_p_bin
2282 && fileformat == EOL_DOS
2283 && ptr[-1] == Ctrl_Z)
2284 {
2285 ptr--;
2286 linerest--;
2287 if (set_options)
2288 curbuf->b_p_eof = TRUE;
2289 }
2290
2291 // If we get EOF in the middle of a line, note the fact by resetting
2292 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (!error
2294 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002295 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002298 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 curbuf->b_p_eol = FALSE;
2300 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002301 len = (colnr_T)(ptr - line_start + 1);
2302 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 error = TRUE;
2304 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002305 {
2306#ifdef FEAT_PERSISTENT_UNDO
2307 if (read_undo_file)
2308 sha256_update(&sha_ctx, line_start, len);
2309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002314 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
2317#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002318 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002319 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002320 crypt_free_state(curbuf->b_cryptstate);
2321 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002322 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002323 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2324 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002325 // Don't set cryptkey to NULL, it's used below as a flag that
2326 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#endif
2328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 // If editing a new file: set 'fenc' for the current buffer.
2330 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002331 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002333 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (fenc_alloced)
2335 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002336#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#endif
2340
2341 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002343#ifdef HAVE_FD_CLOEXEC
2344 else
2345 {
2346 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002347
Bram Moolenaarf05da212009-11-17 16:13:15 +00002348 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002349 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002350 }
2351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 vim_free(buffer);
2353
2354#ifdef HAVE_DUP
2355 if (read_stdin)
2356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002357 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002359 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361#endif
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (tmpname != NULL)
2364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 vim_free(tmpname);
2367 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002368 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /*
2371 * In recovery mode everything but autocommands is skipped.
2372 */
2373 if (!recoverymode)
2374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2377 {
2378#ifdef FEAT_NETBEANS_INTG
2379 netbeansFireChanges = 0;
2380#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002381 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef FEAT_NETBEANS_INTG
2383 netbeansFireChanges = 1;
2384#endif
2385 --linecnt;
2386 }
2387 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2388 if (filesize == 0)
2389 linecnt = 0;
2390 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002391 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002392 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002393#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002394 // After reading the text into the buffer the diff info needs to
2395 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002396 diff_invalidate(curbuf);
2397#endif
2398#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002399 // All folds in the window are invalid now. Mark them for update
2400 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002401 foldUpdateAll(curwin);
2402#endif
2403 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002404 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 appended_lines_mark(from, linecnt);
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407#ifndef ALWAYS_USE_GUI
2408 /*
2409 * If we were reading from the same terminal as where messages go,
2410 * the screen will have been messed up.
2411 * Switch on raw mode now and clear the screen.
2412 */
2413 if (read_stdin)
2414 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002415 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 starttermcap();
2417 screenclear();
2418 }
2419#endif
2420
2421 if (got_int)
2422 {
2423 if (!(flags & READ_DUMMY))
2424 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002425 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002427 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 msg_scroll = msg_save;
2430#ifdef FEAT_VIMINFO
2431 check_marks_read();
2432#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002433 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435
2436 if (!filtering && !(flags & READ_DUMMY))
2437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002438 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 c = FALSE;
2440
2441#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002442 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 STRCAT(IObuff, _("[fifo]"));
2445 c = TRUE;
2446 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002447 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
2449 STRCAT(IObuff, _("[socket]"));
2450 c = TRUE;
2451 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002452# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002453 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002454 {
2455 STRCAT(IObuff, _("[character special]"));
2456 c = TRUE;
2457 }
2458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459#endif
2460 if (curbuf->b_p_ro)
2461 {
2462 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2463 c = TRUE;
2464 }
2465 if (read_no_eol_lnum)
2466 {
2467 msg_add_eol();
2468 c = TRUE;
2469 }
2470 if (ff_error == EOL_DOS)
2471 {
2472 STRCAT(IObuff, _("[CR missing]"));
2473 c = TRUE;
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 if (split)
2476 {
2477 STRCAT(IObuff, _("[long lines split]"));
2478 c = TRUE;
2479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (notconverted)
2481 {
2482 STRCAT(IObuff, _("[NOT converted]"));
2483 c = TRUE;
2484 }
2485 else if (converted)
2486 {
2487 STRCAT(IObuff, _("[converted]"));
2488 c = TRUE;
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490#ifdef FEAT_CRYPT
2491 if (cryptkey != NULL)
2492 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002493 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 c = TRUE;
2495 }
2496#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002497 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002499 sprintf((char *)IObuff + STRLEN(IObuff),
2500 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 c = TRUE;
2502 }
2503 else if (illegal_byte > 0)
2504 {
2505 sprintf((char *)IObuff + STRLEN(IObuff),
2506 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2507 c = TRUE;
2508 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002509 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 STRCAT(IObuff, _("[READ ERRORS]"));
2512 c = TRUE;
2513 }
2514 if (msg_add_fileformat(fileformat))
2515 c = TRUE;
2516#ifdef FEAT_CRYPT
2517 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002518 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002519 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 else
2521#endif
2522 msg_add_lines(c, (long)linecnt, filesize);
2523
Bram Moolenaard23a8232018-02-10 18:45:26 +01002524 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 msg_scrolled_ign = TRUE;
2526#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002527 // Don't show the message when reading stdin, it would end up in a
2528 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 if (read_stdin || read_buffer)
2530 p = msg_may_trunc(FALSE, IObuff);
2531 else
2532#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002533 {
2534 if (msg_col > 0)
2535 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002536 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002539 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002540 // Need to repeat the message after redrawing when:
2541 // - When reading from stdin (the screen will be cleared next).
2542 // - When restart_edit is set (otherwise there will be a delay
2543 // before redrawing).
2544 // - When the screen was scrolled but there is no wait-return
2545 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002546 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 msg_scrolled_ign = FALSE;
2548 }
2549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002550 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002552 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002553 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 curbuf->b_p_ro = TRUE;
2555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002556 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
2559 * In Ex mode: cursor at last new line.
2560 * Otherwise: cursor at first new line.
2561 */
2562 if (exmode_active)
2563 curwin->w_cursor.lnum = from + linecnt;
2564 else
2565 curwin->w_cursor.lnum = from + 1;
2566 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002567 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaare1004402020-10-24 20:49:43 +02002569 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002570 {
2571 // Set '[ and '] marks to the newly read lines.
2572 curbuf->b_op_start.lnum = from + 1;
2573 curbuf->b_op_start.col = 0;
2574 curbuf->b_op_end.lnum = from + linecnt;
2575 curbuf->b_op_end.col = 0;
2576 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002577
Bram Moolenaar4f974752019-02-17 17:44:42 +01002578#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002579 /*
2580 * Work around a weird problem: When a file has two links (only
2581 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002582 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002583 * It's correct again after reading the file, thus reset the timestamp
2584 * here.
2585 */
2586 if (newfile && !read_stdin && !read_buffer
2587 && mch_stat((char *)fname, &st) >= 0)
2588 {
2589 buf_store_time(curbuf, &st, fname);
2590 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002591 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002592 }
2593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595 msg_scroll = msg_save;
2596
2597#ifdef FEAT_VIMINFO
2598 /*
2599 * Get the marks before executing autocommands, so they can be used there.
2600 */
2601 check_marks_read();
2602#endif
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002605 * We remember if the last line of the read didn't have
2606 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2607 * or writing the read again with 'binary' on. The latter is required
2608 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002610 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002612 // When reloading a buffer put the cursor at the first line that is
2613 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002614 if (flags & READ_KEEP_UNDO)
2615 u_find_first_changed();
2616
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002617#ifdef FEAT_PERSISTENT_UNDO
2618 /*
2619 * When opening a new file locate undo info and read it.
2620 */
2621 if (read_undo_file)
2622 {
2623 char_u hash[UNDO_HASH_SIZE];
2624
2625 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002626 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002627 }
2628#endif
2629
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002630 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 int m = msg_scroll;
2633 int n = msg_scrolled;
2634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002635 // Save the fileformat now, otherwise the buffer will be considered
2636 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002637 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 save_file_ff(curbuf);
2639
2640 /*
2641 * The output from the autocommands should not overwrite anything and
2642 * should not be overwritten: Set msg_scroll, restore its value if no
2643 * output was done.
2644 */
2645 msg_scroll = TRUE;
2646 if (filtering)
2647 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2648 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002649 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2652 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002653 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2654 /*
2655 * EVENT_FILETYPE was not triggered but the buffer already has a
2656 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2657 */
2658 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2659 TRUE, curbuf);
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 else
2662 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2663 FALSE, NULL, eap);
2664 if (msg_scrolled == n)
2665 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002666# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 if (recoverymode && error)
2673 return FAIL;
2674 return OK;
2675}
2676
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002677#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002678/*
2679 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2680 * which is the name of files used for process substitution output by
2681 * some shells on some operating systems, e.g., bash on SunOS.
2682 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2683 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002684 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002685is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002686{
2687 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2688 && VIM_ISDIGIT(fname[8])
2689 && *skipdigits(fname + 9) == NUL
2690 && (fname[9] != NUL
2691 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2692}
2693#endif
2694
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002695/*
2696 * From the current line count and characters read after that, estimate the
2697 * line number where we are now.
2698 * Used for error messages that include a line number.
2699 */
2700 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002701readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002702 linenr_T linecnt, // line count before reading more bytes
2703 char_u *p, // start of more bytes read
2704 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002705{
2706 char_u *s;
2707 linenr_T lnum;
2708
2709 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2710 for (s = p; s < endp; ++s)
2711 if (*s == '\n')
2712 ++lnum;
2713 return lnum;
2714}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716/*
Rob Pilling8196e942022-02-11 15:12:10 +00002717 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002718 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 * Returns OK or FAIL.
2720 */
2721 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002722prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
Bram Moolenaar13505972019-01-24 15:04:48 +01002724 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (eap->cmd == NULL)
2726 return FAIL;
2727
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002728 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2729 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002730 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002731 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002732
2733 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002734 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002735 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 return OK;
2737}
2738
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002739/*
2740 * Set default or forced 'fileformat' and 'binary'.
2741 */
2742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002743set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002744{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002745 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002746 if (set_options)
2747 {
2748 if (eap != NULL && eap->force_ff != 0)
2749 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2750 else if (*p_ffs != NUL)
2751 set_fileformat(default_fileformat(), OPT_LOCAL);
2752 }
2753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002754 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002755 if (eap != NULL && eap->force_bin != 0)
2756 {
2757 int oldval = curbuf->b_p_bin;
2758
2759 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2760 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2761 }
2762}
2763
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002764/*
2765 * Set forced 'fileencoding'.
2766 */
2767 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002768set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002769{
2770 if (eap->force_enc != 0)
2771 {
2772 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2773
2774 if (fenc != NULL)
2775 set_string_option_direct((char_u *)"fenc", -1,
2776 fenc, OPT_FREE|OPT_LOCAL, 0);
2777 vim_free(fenc);
2778 }
2779}
2780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781/*
2782 * Find next fileencoding to use from 'fileencodings'.
2783 * "pp" points to fenc_next. It's advanced to the next item.
2784 * When there are no more items, an empty string is returned and *pp is set to
2785 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002786 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2787 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
2789 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002790next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 char_u *p;
2793 char_u *r;
2794
Bram Moolenaarf077db22019-08-13 00:18:24 +02002795 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (**pp == NUL)
2797 {
2798 *pp = NULL;
2799 return (char_u *)"";
2800 }
2801 p = vim_strchr(*pp, ',');
2802 if (p == NULL)
2803 {
2804 r = enc_canonize(*pp);
2805 *pp += STRLEN(*pp);
2806 }
2807 else
2808 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002809 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 *pp = p + 1;
2811 if (r != NULL)
2812 {
2813 p = enc_canonize(r);
2814 vim_free(r);
2815 r = p;
2816 }
2817 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002818 if (r != NULL)
2819 *alloced = TRUE;
2820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002822 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 r = (char_u *)"";
2824 *pp = NULL;
2825 }
2826 return r;
2827}
2828
Bram Moolenaar13505972019-01-24 15:04:48 +01002829#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830/*
2831 * Convert a file with the 'charconvert' expression.
2832 * This closes the file which is to be read, converts it and opens the
2833 * resulting file for reading.
2834 * Returns name of the resulting converted file (the caller should delete it
2835 * after reading it).
2836 * Returns NULL if the conversion failed ("*fdp" is not set) .
2837 */
2838 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002839readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002840 char_u *fname, // name of input file
2841 char_u *fenc, // converted from
2842 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002845 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002847 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002849 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else
2851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 *fdp = -1;
2854 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2855 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002856 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2858 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002859 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861
2862 if (errmsg != NULL)
2863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002864 // Don't use emsg(), it breaks mappings, the retry with
2865 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002866 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (tmpname != NULL)
2868 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002869 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002870 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872 }
2873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (*fdp < 0)
2876 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2877
2878 return tmpname;
2879}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#endif
2881
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002882#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002884 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2886 * *filesizep are updated.
2887 * Return the (new) encryption key, NULL for no encryption.
2888 */
2889 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002890check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 char_u *cryptkey, // previous encryption key or NULL
2892 char_u *ptr, // pointer to read bytes
2893 long *sizep, // length of read bytes
2894 off_T *filesizep, // nr of bytes used from file
2895 int newfile, // editing a new buffer
2896 char_u *fname, // file name to display
2897 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002899 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002900 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002901
2902 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002904 // Mark the buffer as read-only until the decryption has taken place.
2905 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002906 curbuf->b_p_ro = TRUE;
2907
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002908 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002909 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002910 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 if (*curbuf->b_p_key)
2913 cryptkey = curbuf->b_p_key;
2914 else
2915 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002916 // When newfile is TRUE, store the typed key in the 'key'
2917 // option and don't free it. bf needs hash of the key saved.
2918 // Don't ask for the key again when first time Enter was hit.
2919 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002920 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002921 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002922 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002923 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002924 *did_ask = TRUE;
2925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002926 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 if (cryptkey != NULL && *cryptkey == NUL)
2928 {
2929 if (cryptkey != curbuf->b_p_key)
2930 vim_free(cryptkey);
2931 cryptkey = NULL;
2932 }
2933 }
2934 }
2935
2936 if (cryptkey != NULL)
2937 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002938 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002939
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002940 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002941 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002942 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002943 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002944
2945 curbuf->b_cryptstate = crypt_create_from_header(
2946 method, cryptkey, ptr);
2947 crypt_set_cm_option(curbuf, method);
2948
2949 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002950 *filesizep += header_len;
2951 *sizep -= header_len;
2952 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002954 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002955 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
2957 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002958 // When starting to edit a new file which does not have encryption, clear
2959 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002960 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01002961 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 return cryptkey;
2964}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002965#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002968 * Return TRUE if a file appears to be read-only from the file permissions.
2969 */
2970 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002971check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002972 char_u *fname, // full path to file
2973 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002974{
2975#ifndef USE_MCH_ACCESS
2976 int fd = 0;
2977#endif
2978
2979 return (
2980#ifdef USE_MCH_ACCESS
2981# ifdef UNIX
2982 (perm & 0222) == 0 ||
2983# endif
2984 mch_access((char *)fname, W_OK)
2985#else
2986 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2987 ? TRUE : (close(fd), FALSE)
2988#endif
2989 );
2990}
2991
Bram Moolenaara7870192019-02-14 12:56:36 +01002992#if defined(HAVE_FSYNC) || defined(PROTO)
2993/*
2994 * Call fsync() with Mac-specific exception.
2995 * Return fsync() result: zero for success.
2996 */
2997 int
2998vim_fsync(int fd)
2999{
3000 int r;
3001
3002# ifdef MACOS_X
3003 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003004 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003005# endif
3006 r = fsync(fd);
3007 return r;
3008}
3009#endif
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003012 * Set the name of the current buffer. Use when the buffer doesn't have a
3013 * name and a ":r" or ":w" command with a file name is used.
3014 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003015 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003016set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003017{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003018 buf_T *buf = curbuf;
3019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003020 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003021 if (curbuf->b_p_bl)
3022 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3023 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003024#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003025 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003026 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003027#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003028 if (curbuf != buf)
3029 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003030 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003031 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003032 return FAIL;
3033 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003034
3035 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3036 curbuf->b_flags |= BF_NOTEDITED;
3037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003038 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003039 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3040 if (curbuf->b_p_bl)
3041 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003042#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003043 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003044 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003045#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003047 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003048 if (*curbuf->b_p_ft == NUL)
3049 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003050 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003051 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003052 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003053 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003054
3055 return OK;
3056}
3057
3058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 * Put file name into IObuff with quotes.
3060 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003062msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 if (fname == NULL)
3065 fname = (char_u *)"-stdin-";
3066 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3067 IObuff[0] = '"';
3068 STRCAT(IObuff, "\" ");
3069}
3070
3071/*
3072 * Append message for text mode to IObuff.
3073 * Return TRUE if something appended.
3074 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003075 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078#ifndef USE_CRNL
3079 if (eol_type == EOL_DOS)
3080 {
3081 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3082 return TRUE;
3083 }
3084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (eol_type == EOL_MAC)
3086 {
3087 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3088 return TRUE;
3089 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003090#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (eol_type == EOL_UNIX)
3092 {
3093 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3094 return TRUE;
3095 }
3096#endif
3097 return FALSE;
3098}
3099
3100/*
3101 * Append line and character count to IObuff.
3102 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003104msg_add_lines(
3105 int insert_space,
3106 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003107 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 char_u *p;
3110
3111 p = IObuff + STRLEN(IObuff);
3112
3113 if (insert_space)
3114 *p++ = ' ';
3115 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003116 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003117 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else
3119 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003120 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003122 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003123 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003124 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
3126}
3127
3128/*
3129 * Append message for missing line separator to IObuff.
3130 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003132msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3135}
3136
Bram Moolenaar473952e2019-09-28 16:30:04 +02003137 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003138time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
ichizokdef69df2021-10-15 17:23:12 +01003140 return
3141#ifdef ST_MTIM_NSEC
3142 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#endif
ichizokdef69df2021-10-15 17:23:12 +01003144#if defined(__linux__) || defined(MSWIN)
3145 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3146 // the seconds. Since the roundoff is done when flushing the inode, the
3147 // time may change unexpectedly by one second!!!
3148 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3149#else
3150 (long)st->st_mtime != mtime
3151#endif
3152 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153}
3154
3155/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003156 * Return TRUE if file encoding "fenc" requires conversion from or to
3157 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003159 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003160need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003162 int same_encoding;
3163 int enc_flags;
3164 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003166 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003167 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003168 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003169 fenc_flags = 0;
3170 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003171 else
3172 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003173 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3174 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003175 enc_flags = get_fio_flags(p_enc);
3176 fenc_flags = get_fio_flags(fenc);
3177 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3178 }
3179 if (same_encoding)
3180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003181 // Specified encoding matches with 'encoding'. This requires
3182 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003183 return enc_unicode != 0;
3184 }
3185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003186 // Encodings differ. However, conversion is not needed when 'enc' is any
3187 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003188 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189}
3190
3191/*
3192 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3193 * internal conversion.
3194 * if "ptr" is an empty string, use 'encoding'.
3195 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003197get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 int prop;
3200
3201 if (*ptr == NUL)
3202 ptr = p_enc;
3203
3204 prop = enc_canon_props(ptr);
3205 if (prop & ENC_UNICODE)
3206 {
3207 if (prop & ENC_2BYTE)
3208 {
3209 if (prop & ENC_ENDIAN_L)
3210 return FIO_UCS2 | FIO_ENDIAN_L;
3211 return FIO_UCS2;
3212 }
3213 if (prop & ENC_4BYTE)
3214 {
3215 if (prop & ENC_ENDIAN_L)
3216 return FIO_UCS4 | FIO_ENDIAN_L;
3217 return FIO_UCS4;
3218 }
3219 if (prop & ENC_2WORD)
3220 {
3221 if (prop & ENC_ENDIAN_L)
3222 return FIO_UTF16 | FIO_ENDIAN_L;
3223 return FIO_UTF16;
3224 }
3225 return FIO_UTF8;
3226 }
3227 if (prop & ENC_LATIN1)
3228 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 return 0;
3231}
3232
Bram Moolenaar473952e2019-09-28 16:30:04 +02003233#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234/*
3235 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3236 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3237 * Used for conversion between 'encoding' and 'fileencoding'.
3238 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003239 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003240get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 int cp;
3243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003244 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (!enc_utf8 && enc_codepage <= 0)
3246 return 0;
3247
3248 cp = encname2codepage(ptr);
3249 if (cp == 0)
3250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003251# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (STRCMP(ptr, "utf-8") == 0)
3253 cp = CP_UTF8;
3254 else
3255# endif
3256 return 0;
3257 }
3258 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3259}
3260#endif
3261
Bram Moolenaar473952e2019-09-28 16:30:04 +02003262#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263/*
3264 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3265 * needed for the internal conversion to/from utf-8 or latin1.
3266 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003268get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3271 && (enc_canon_props(ptr) & ENC_MACROMAN))
3272 return FIO_MACROMAN;
3273 return 0;
3274}
3275#endif
3276
3277/*
3278 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3279 * "size" must be at least 2.
3280 * Return the name of the encoding and set "*lenp" to the length.
3281 * Returns NULL when no BOM found.
3282 */
3283 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003284check_for_bom(
3285 char_u *p,
3286 long size,
3287 int *lenp,
3288 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 char *name = NULL;
3291 int len = 2;
3292
3293 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003294 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003296 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 len = 3;
3298 }
3299 else if (p[0] == 0xff && p[1] == 0xfe)
3300 {
3301 if (size >= 4 && p[2] == 0 && p[3] == 0
3302 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3303 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003304 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 len = 4;
3306 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003307 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003308 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003309 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003310 // utf-16le is preferred, it also works for ucs-2le text
3311 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 else if (p[0] == 0xfe && p[1] == 0xff
3314 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003316 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003317 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003318 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003319 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003320 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
3322 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3323 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3324 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003325 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 len = 4;
3327 }
3328
3329 *lenp = len;
3330 return (char_u *)name;
3331}
3332
3333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * Try to find a shortname by comparing the fullname with the current
3335 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003336 * Returns "full_path" or pointer into "full_path" if shortened.
3337 */
3338 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003339shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003340{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003341 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003342 char_u *p = full_path;
3343
Bram Moolenaard9462e32011-04-11 21:35:11 +02003344 dirname = alloc(MAXPATHL);
3345 if (dirname == NULL)
3346 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003347 if (mch_dirname(dirname, MAXPATHL) == OK)
3348 {
3349 p = shorten_fname(full_path, dirname);
3350 if (p == NULL || *p == NUL)
3351 p = full_path;
3352 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003353 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003354 return p;
3355}
3356
3357/*
3358 * Try to find a shortname by comparing the fullname with the current
3359 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * Returns NULL if not shorter name possible, pointer into "full_path"
3361 * otherwise.
3362 */
3363 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003364shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 int len;
3367 char_u *p;
3368
3369 if (full_path == NULL)
3370 return NULL;
3371 len = (int)STRLEN(dir_name);
3372 if (fnamencmp(dir_name, full_path, len) == 0)
3373 {
3374 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003375#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003377 * MS-Windows: when a file is in the root directory, dir_name will end
3378 * in a slash, since C: by itself does not define a specific dir. In
3379 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
3381 if (!((len > 2) && (*(p - 2) == ':')))
3382#endif
3383 {
3384 if (vim_ispathsep(*p))
3385 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003386#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
3388 p = NULL;
3389#endif
3390 }
3391 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003392#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 /*
3394 * When using a file in the current drive, remove the drive name:
3395 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3396 * a floppy from "A:\dir" to "B:\dir".
3397 */
3398 else if (len > 3
3399 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3400 && full_path[1] == ':'
3401 && vim_ispathsep(full_path[2]))
3402 p = full_path + 2;
3403#endif
3404 else
3405 p = NULL;
3406 return p;
3407}
3408
3409/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003410 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * When "force" is TRUE: Use full path from now on for files currently being
3412 * edited, both for file name and swap file name. Try to shorten the file
3413 * names a bit, if safe to do so.
3414 * When "force" is FALSE: Only try to shorten absolute file names.
3415 * For buffers that have buftype "nofile" or "scratch": never change the file
3416 * name.
3417 */
3418 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003419shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3420{
3421 char_u *p;
3422
3423 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003424 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003425 && !path_with_url(buf->b_fname)
3426 && (force
3427 || buf->b_sfname == NULL
3428 || mch_isFullName(buf->b_sfname)))
3429 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003430 if (buf->b_sfname != buf->b_ffname)
3431 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003432 p = shorten_fname(buf->b_ffname, dirname);
3433 if (p != NULL)
3434 {
3435 buf->b_sfname = vim_strsave(p);
3436 buf->b_fname = buf->b_sfname;
3437 }
3438 if (p == NULL || buf->b_fname == NULL)
3439 buf->b_fname = buf->b_ffname;
3440 }
3441}
3442
3443/*
3444 * Shorten filenames for all buffers.
3445 */
3446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 char_u dirname[MAXPATHL];
3450 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
3452 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003453 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003455 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003456
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003457 // Always make the swap file name a full path, a "nofile" buffer may
3458 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003459 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003462 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003463#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003464 popup_update_preview_title();
3465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467
3468#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3469 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003470 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 || defined(PROTO)
3472/*
3473 * Shorten all filenames in "fnames[count]" by current directory.
3474 */
3475 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003476shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 int i;
3479 char_u dirname[MAXPATHL];
3480 char_u *p;
3481
3482 if (fnames == NULL || count < 1)
3483 return;
3484 mch_dirname(dirname, sizeof(dirname));
3485 for (i = 0; i < count; ++i)
3486 {
3487 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3488 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003489 // shorten_fname() returns pointer in given "fnames[i]". If free
3490 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3491 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 p = vim_strsave(p);
3493 vim_free(fnames[i]);
3494 fnames[i] = p;
3495 }
3496 }
3497}
3498#endif
3499
3500/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003501 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 * fo_o_h.ext for MSDOS or when shortname option set.
3503 *
3504 * Assumed that fname is a valid name found in the filesystem we assure that
3505 * the return value is a different name and ends in 'ext'.
3506 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3507 * characters otherwise.
3508 * Space for the returned name is allocated, must be freed later.
3509 * Returns NULL when out of memory.
3510 */
3511 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003512modname(
3513 char_u *fname,
3514 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003515 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003517 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 fname, ext, prepend_dot);
3519}
3520
3521 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003522buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003523 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003524 char_u *fname,
3525 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003526 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528 char_u *retval;
3529 char_u *s;
3530 char_u *e;
3531 char_u *ptr;
3532 int fnamelen, extlen;
3533
3534 extlen = (int)STRLEN(ext);
3535
3536 /*
3537 * If there is no file name we must get the name of the current directory
3538 * (we need the full path in case :cd is used).
3539 */
3540 if (fname == NULL || *fname == NUL)
3541 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003542 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (retval == NULL)
3544 return NULL;
3545 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3546 (fnamelen = (int)STRLEN(retval)) == 0)
3547 {
3548 vim_free(retval);
3549 return NULL;
3550 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003551 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
3553 retval[fnamelen++] = PATHSEP;
3554 retval[fnamelen] = NUL;
3555 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003556 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 else
3559 {
3560 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003561 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (retval == NULL)
3563 return NULL;
3564 STRCPY(retval, fname);
3565#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003566 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#endif
3568 }
3569
3570 /*
3571 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3572 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3573 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3574 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3575 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003576 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003578 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003579 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003582 {
3583 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003588 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3590 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 s = ptr + STRLEN(ptr);
3593
3594 /*
3595 * For 8.3 file names we may have to reduce the length.
3596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 /*
3600 * If there is no file name, or the file name ends in '/', and the
3601 * extension starts with '.', put a '_' before the dot, because just
3602 * ".ext" is invalid.
3603 */
3604 if (fname == NULL || *fname == NUL
3605 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 *s++ = '_';
3609 }
3610 /*
3611 * If the extension starts with '.', truncate the base name at 8
3612 * characters
3613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003616 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 s = ptr + 8;
3619 *s = '\0';
3620 }
3621 }
3622 /*
3623 * If the extension doesn't start with '.', and the file name
3624 * doesn't have an extension yet, append a '.'
3625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 else if ((e = vim_strchr(ptr, '.')) == NULL)
3627 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 /*
3629 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003630 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
3632 else if ((int)STRLEN(e) + extlen > 4)
3633 s = e + 4 - extlen;
3634 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003635#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 /*
3637 * If there is no file name, and the extension starts with '.', put a
3638 * '_' before the dot, because just ".ext" may be invalid if it's on a
3639 * FAT partition, and on HPFS it doesn't matter.
3640 */
3641 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3642 *s++ = '_';
3643#endif
3644
3645 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003646 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 * ext can start with '.' and cannot exceed 3 more characters.
3648 */
3649 STRCPY(s, ext);
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 /*
3652 * Prepend the dot.
3653 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003654 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003656 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 /*
3661 * Check that, after appending the extension, the file name is really
3662 * different.
3663 */
3664 if (fname != NULL && STRCMP(fname, retval) == 0)
3665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003666 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 while (--s >= ptr)
3668 {
3669 if (*s != '_')
3670 {
3671 *s = '_';
3672 break;
3673 }
3674 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003675 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 *ptr = 'v';
3677 }
3678 return retval;
3679}
3680
3681/*
3682 * Like fgets(), but if the file line is too long, it is truncated and the
3683 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003684 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
3686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003687vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 char *eof;
3690#define FGETS_SIZE 200
3691 char tbuf[FGETS_SIZE];
3692
3693 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003699 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 do
3701 {
3702 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003703 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3705 }
3706 return (eof == NULL);
3707}
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709/*
3710 * rename() only works if both files are on the same file system, this
3711 * function will (attempts to?) copy the file across if rename fails -- webb
3712 * Return -1 for failure, 0 for success.
3713 */
3714 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003715vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 int fd_in;
3718 int fd_out;
3719 int n;
3720 char *errmsg = NULL;
3721 char *buffer;
3722#ifdef AMIGA
3723 BPTR flock;
3724#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003725 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003726 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003727#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003728 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003729#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003730 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003733 * When the names are identical, there is nothing to do. When they refer
3734 * to the same file (ignoring case and slash/backslash differences) but
3735 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
3737 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003738 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003739 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003740 use_tmp_file = TRUE;
3741 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003742 return 0;
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 /*
3746 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3747 */
3748 if (mch_stat((char *)from, &st) < 0)
3749 return -1;
3750
Bram Moolenaar3576da72008-12-30 15:15:57 +00003751#ifdef UNIX
3752 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003753 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003755 // It's possible for the source and destination to be the same file.
3756 // This happens when "from" and "to" differ in case and are on a FAT32
3757 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003758 if (mch_stat((char *)to, &st_to) >= 0
3759 && st.st_dev == st_to.st_dev
3760 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003761 use_tmp_file = TRUE;
3762 }
3763#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003764#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003765 {
3766 BY_HANDLE_FILE_INFORMATION info1, info2;
3767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003768 // It's possible for the source and destination to be the same file.
3769 // In that case go through a temp file name. This makes rename("foo",
3770 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003771 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3772 && win32_fileinfo(to, &info2) == FILEINFO_OK
3773 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3774 && info1.nFileIndexHigh == info2.nFileIndexHigh
3775 && info1.nFileIndexLow == info2.nFileIndexLow)
3776 use_tmp_file = TRUE;
3777 }
3778#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003779
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003780 if (use_tmp_file)
3781 {
3782 char tempname[MAXPATHL + 1];
3783
3784 /*
3785 * Find a name that doesn't exist and is in the same directory.
3786 * Rename "from" to "tempname" and then rename "tempname" to "to".
3787 */
3788 if (STRLEN(from) >= MAXPATHL - 5)
3789 return -1;
3790 STRCPY(tempname, from);
3791 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003792 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003793 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3794 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003795 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003796 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003797 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003798 if (mch_rename(tempname, (char *)to) == 0)
3799 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003800 // Strange, the second step failed. Try moving the
3801 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003802 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003803 return -1;
3804 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003805 // If it fails for one temp name it will most likely fail
3806 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003807 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003808 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003809 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003810 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003811 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 /*
3814 * Delete the "to" file, this is required on some systems to make the
3815 * mch_rename() work, on other systems it makes sure that we don't have
3816 * two files when the mch_rename() fails.
3817 */
3818
3819#ifdef AMIGA
3820 /*
3821 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3822 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003823 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 * deleting the "from" file (horror!) we lock it during the remove.
3825 *
3826 * When used for making a backup before writing the file: This should not
3827 * happen with ":w", because startscript() should detect this problem and
3828 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3829 * name. This problem does exist with ":w filename", but then the
3830 * original file will be somewhere else so the backup isn't really
3831 * important. If autoscripting is off the rename may fail.
3832 */
3833 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3834#endif
3835 mch_remove(to);
3836#ifdef AMIGA
3837 if (flock)
3838 UnLock(flock);
3839#endif
3840
3841 /*
3842 * First try a normal rename, return if it works.
3843 */
3844 if (mch_rename((char *)from, (char *)to) == 0)
3845 return 0;
3846
3847 /*
3848 * Rename() failed, try copying the file.
3849 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003850 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003851#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003853 acl = mch_get_acl(from);
3854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3856 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003857 {
3858#ifdef HAVE_ACL
3859 mch_free_acl(acl);
3860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003862 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003864 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003865 fd_out = mch_open((char *)to,
3866 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (fd_out == -1)
3868 {
3869 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003870#ifdef HAVE_ACL
3871 mch_free_acl(acl);
3872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return -1;
3874 }
3875
Bram Moolenaar473952e2019-09-28 16:30:04 +02003876 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (buffer == NULL)
3878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003880 close(fd_in);
3881#ifdef HAVE_ACL
3882 mch_free_acl(acl);
3883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 return -1;
3885 }
3886
Bram Moolenaar473952e2019-09-28 16:30:04 +02003887 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003888 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003890 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892 }
3893
3894 vim_free(buffer);
3895 close(fd_in);
3896 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003897 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (n < 0)
3899 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003900 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 to = from;
3902 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003903#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003904 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003905#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003906#ifdef HAVE_ACL
3907 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003908 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003909#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003910#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003911 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (errmsg != NULL)
3914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003915 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 return -1;
3917 }
3918 mch_remove(from);
3919 return 0;
3920}
3921
3922static int already_warned = FALSE;
3923
3924/*
3925 * Check if any not hidden buffer has been changed.
3926 * Postpone the check if there are characters in the stuff buffer, a global
3927 * command is being executed, a mapping is being executed or an autocommand is
3928 * busy.
3929 * Returns TRUE if some message was written (screen should be redrawn and
3930 * cursor positioned).
3931 */
3932 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003933check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003934 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936 buf_T *buf;
3937 int didit = 0;
3938 int n;
3939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003940 // Don't check timestamps while system() or another low-level function may
3941 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (no_check_timestamps > 0)
3943 return FALSE;
3944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003945 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3946 // event and we would keep on checking if the file is steadily growing.
3947 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (focus && did_check_timestamps)
3949 {
3950 need_check_timestamps = TRUE;
3951 return FALSE;
3952 }
3953
3954 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003955 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003956 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
3958 {
3959 ++no_wait_return;
3960 did_check_timestamps = TRUE;
3961 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003962 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003964 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (buf->b_nwindows > 0)
3966 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003967 bufref_T bufref;
3968
3969 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 n = buf_check_timestamp(buf, focus);
3971 if (didit < n)
3972 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003973 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003975 // Autocommands have removed the buffer, start at the
3976 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 buf = firstbuf;
3978 continue;
3979 }
3980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 --no_wait_return;
3983 need_check_timestamps = FALSE;
3984 if (need_wait_return && didit == 2)
3985 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003986 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003987 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 out_flush();
3989 }
3990 }
3991 return didit;
3992}
3993
3994/*
3995 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3996 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3997 * empty.
3998 */
3999 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004000move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
4002 buf_T *tbuf = curbuf;
4003 int retval = OK;
4004 linenr_T lnum;
4005 char_u *p;
4006
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004007 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 curbuf = tobuf;
4009 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4010 {
4011 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4012 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4013 {
4014 vim_free(p);
4015 retval = FAIL;
4016 break;
4017 }
4018 vim_free(p);
4019 }
4020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004021 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 if (retval != FAIL)
4023 {
4024 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004025 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004026 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004028 // Oops! We could try putting back the saved lines, but that
4029 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 retval = FAIL;
4031 break;
4032 }
4033 }
4034
4035 curbuf = tbuf;
4036 return retval;
4037}
4038
4039/*
4040 * Check if buffer "buf" has been changed.
4041 * Also check if the file for a new buffer unexpectedly appeared.
4042 * return 1 if a changed buffer was found.
4043 * return 2 if a message has been displayed.
4044 * return 0 otherwise.
4045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004047buf_check_timestamp(
4048 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004049 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004051 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 int stat_res;
4053 int retval = 0;
4054 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004055 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004057 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004059 enum {
4060 RELOAD_NONE,
4061 RELOAD_NORMAL,
4062 RELOAD_DETECT
4063 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004064 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4066 int can_reload = FALSE;
4067#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004068 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 int orig_mode = buf->b_orig_mode;
4070#ifdef FEAT_GUI
4071 int save_mouse_correct = need_mouse_correct;
4072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004074 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004075#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004076 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004077#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004078 bufref_T bufref;
4079
4080 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004082 // If there is no file name, the buffer is not loaded, 'buftype' is
4083 // set, we are in the middle of a save or being called recursively: ignore
4084 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (buf->b_ffname == NULL
4086 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004087 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004090#ifdef FEAT_NETBEANS_INTG
4091 || isNetbeansBuffer(buf)
4092#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004093#ifdef FEAT_TERMINAL
4094 || buf->b_term != NULL
4095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 )
4097 return 0;
4098
4099 if ( !(buf->b_flags & BF_NOTEDITED)
4100 && buf->b_mtime != 0
4101 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004102 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004103 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#ifdef HAVE_ST_MODE
4105 || (int)st.st_mode != buf->b_orig_mode
4106#else
4107 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4108#endif
4109 ))
4110 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004111 long prev_b_mtime = buf->b_mtime;
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 retval = 1;
4114
Bram Moolenaar386bc822018-07-07 18:34:12 +02004115 // set b_mtime to stop further warnings (e.g., when executing
4116 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (stat_res < 0)
4118 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004119 // Check the file again later to see if it re-appears.
4120 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 buf->b_orig_size = 0;
4122 buf->b_orig_mode = 0;
4123 }
4124 else
4125 buf_store_time(buf, &st, buf->b_ffname);
4126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004127 // Don't do anything for a directory. Might contain the file
4128 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (mch_isdir(buf->b_fname))
4130 ;
4131
4132 /*
4133 * If 'autoread' is set, the buffer has no changes and the file still
4134 * exists, reload the buffer. Use the buffer-local option value if it
4135 * was set, the global option value otherwise.
4136 */
4137 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4138 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004139 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 else
4141 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004142 if (stat_res < 0)
4143 reason = "deleted";
4144 else if (bufIsChanged(buf))
4145 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004146 /*
4147 * Check if the file contents really changed to avoid giving a
4148 * warning when only the timestamp was set (e.g., checked out of
4149 * CVS). Always warn when the buffer was changed.
4150 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004151 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4152 reason = "changed";
4153 else if (orig_mode != buf->b_orig_mode)
4154 reason = "mode";
4155 else
4156 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 /*
4159 * Only give the warning if there are no FileChangedShell
4160 * autocommands.
4161 * Avoid being called recursively by setting "busy".
4162 */
4163 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004164#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004165 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4166 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004167#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004168 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4170 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004171 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 busy = FALSE;
4173 if (n)
4174 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004175 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004176 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004177#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004178 s = get_vim_var_str(VV_FCS_CHOICE);
4179 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004180 reload = RELOAD_NORMAL;
4181 else if (STRCMP(s, "edit") == 0)
4182 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004183 else if (STRCMP(s, "ask") == 0)
4184 n = FALSE;
4185 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004186#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004187 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004189 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004191 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004192 {
4193 // Only give the message once.
4194 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004195 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 else
4198 {
4199 helpmesg = TRUE;
4200#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4201 can_reload = TRUE;
4202#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004203 if (reason[2] == 'n')
4204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004206 mesg2 = _("See \":help W12\" for more info.");
4207 }
4208 else if (reason[1] == 'h')
4209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004211 mesg2 = _("See \":help W11\" for more info.");
4212 }
4213 else if (*reason == 'm')
4214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004216 mesg2 = _("See \":help W16\" for more info.");
4217 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004218 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004220 // Only timestamp changed, store it to avoid a warning
4221 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004222 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004223 buf->b_mtime_read_ns = buf->b_mtime_ns;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227 }
4228
4229 }
4230 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4231 && vim_fexists(buf->b_ffname))
4232 {
4233 retval = 1;
4234 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4235 buf->b_flags |= BF_NEW_W;
4236#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4237 can_reload = TRUE;
4238#endif
4239 }
4240
4241 if (mesg != NULL)
4242 {
4243 path = home_replace_save(buf, buf->b_fname);
4244 if (path != NULL)
4245 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004246 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004248 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004249 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004250#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004251 // Set warningmsg here, before the unimportant and output-specific
4252 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004253 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4256 if (can_reload)
4257 {
4258 if (*mesg2 != NUL)
4259 {
4260 STRCAT(tbuf, "\n");
4261 STRCAT(tbuf, mesg2);
4262 }
Rob Pilling8196e942022-02-11 15:12:10 +00004263 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4264 (char_u *)tbuf,
4265 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4266 1, NULL, TRUE))
4267 {
4268 case 2:
4269 reload = RELOAD_NORMAL;
4270 break;
4271 case 3:
4272 reload = RELOAD_DETECT;
4273 break;
4274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 else
4277#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004278 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4279 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 if (*mesg2 != NUL)
4282 {
4283 STRCAT(tbuf, "; ");
4284 STRCAT(tbuf, mesg2);
4285 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 retval = 2;
4288 }
4289 else
4290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004294 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004296 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 msg_clr_eos();
4298 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004299 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004302#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004304#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004305 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004306 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004308 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 redraw_cmdline = FALSE;
4310 }
4311 }
4312 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
4315 vim_free(path);
4316 vim_free(tbuf);
4317 }
4318 }
4319
Rob Pilling8196e942022-02-11 15:12:10 +00004320 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004322 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004323 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004324#ifdef FEAT_PERSISTENT_UNDO
4325 if (buf->b_p_udf && buf->b_ffname != NULL)
4326 {
4327 char_u hash[UNDO_HASH_SIZE];
4328 buf_T *save_curbuf = curbuf;
4329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004330 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004331 curbuf = buf;
4332 u_compute_hash(hash);
4333 u_write_undo(NULL, FALSE, buf, hash);
4334 curbuf = save_curbuf;
4335 }
4336#endif
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004340 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004341 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4342 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 // restore this in case an autocommand has set it; it would break
4345 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 need_mouse_correct = save_mouse_correct;
4347#endif
4348
4349 return retval;
4350}
4351
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004352/*
4353 * Reload a buffer that is already loaded.
4354 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004355 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4356 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004357 */
4358 void
Rob Pilling8196e942022-02-11 15:12:10 +00004359buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004360{
4361 exarg_T ea;
4362 pos_T old_cursor;
4363 linenr_T old_topline;
4364 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004366 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004367 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004368 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004369 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004370 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004373 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004374
Rob Pilling8196e942022-02-11 15:12:10 +00004375 // Unless reload_options is set, we only want to read the text from the
4376 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4377 // Force the fileformat and encoding to be the same.
4378 if (reload_options)
4379 memset(&ea, 0, sizeof(ea));
4380 else
4381 prepped = prep_exarg(&ea, buf);
4382
4383 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004384 {
4385 old_cursor = curwin->w_cursor;
4386 old_topline = curwin->w_topline;
4387
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004388 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 // Save all the text, so that the reload can be undone.
4391 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004392 u_sync(FALSE);
4393 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4394 flags |= READ_KEEP_UNDO;
4395 }
4396
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397 /*
4398 * To behave like when a new file is edited (matters for
4399 * BufReadPost autocommands) we first need to delete the current
4400 * buffer contents. But if reading the file fails we should keep
4401 * the old contents. Can't use memory only, the file might be
4402 * too big. Use a hidden buffer to move the buffer contents to.
4403 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004404 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 savebuf = NULL;
4406 else
4407 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004408 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004409 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004410 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004411 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004413 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004414 curbuf = savebuf;
4415 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004416 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004417 curbuf = buf;
4418 curwin->w_buffer = buf;
4419 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004420 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004421 || move_lines(buf, savebuf) == FAIL)
4422 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004423 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004424 saved = FAIL;
4425 }
4426 }
4427
4428 if (saved == OK)
4429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4431 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004432 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4433 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004434 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004435 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004436#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004437 if (!aborting())
4438#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004439 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004440 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004442 // Put the text back from the save buffer. First
4443 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004444 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004445 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004446 break;
4447 (void)move_lines(savebuf, buf);
4448 }
4449 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004450 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004451 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004453 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004454 if ((flags & READ_KEEP_UNDO) == 0)
4455 {
4456 u_blockfree(buf);
4457 u_clearall(buf);
4458 }
4459 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004462 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004463 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004464 }
4465 }
4466 vim_free(ea.cmd);
4467
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004468 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004469 wipe_buffer(savebuf, FALSE);
4470
4471#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004472 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004473 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004474#endif
4475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004476 // Restore the topline and cursor position and check it (lines may
4477 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004478 if (old_topline > curbuf->b_ml.ml_line_count)
4479 curwin->w_topline = curbuf->b_ml.ml_line_count;
4480 else
4481 curwin->w_topline = old_topline;
4482 curwin->w_cursor = old_cursor;
4483 check_cursor();
4484 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004485 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004486#ifdef FEAT_FOLDING
4487 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004488 win_T *wp;
4489 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004492 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004493 if (wp->w_buffer == curwin->w_buffer
4494 && !foldmethodIsManual(wp))
4495 foldUpdateAll(wp);
4496 }
4497#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 // If the mode didn't change and 'readonly' was set, keep the old
4499 // value; the user probably used the ":view" command. But don't
4500 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004501 if (orig_mode == curbuf->b_orig_mode)
4502 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004505 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004506 }
4507
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004508 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004509 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004510 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004511}
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004514buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004517#ifdef ST_MTIM_NSEC
4518 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4519#else
4520 buf->b_mtime_ns = 0;
4521#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004522 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#ifdef HAVE_ST_MODE
4524 buf->b_orig_mode = (int)st->st_mode;
4525#else
4526 buf->b_orig_mode = mch_getperm(fname);
4527#endif
4528}
4529
4530/*
4531 * Adjust the line with missing eol, used for the next write.
4532 * Used for do_filter(), when the input lines for the filter are deleted.
4533 */
4534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004535write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004537 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004538 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004541// Subfuncions for readdirex()
4542#ifdef FEAT_EVAL
4543# ifdef MSWIN
4544 static char_u *
4545getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4546{
4547 stat_T st;
4548 unsigned short st_mode;
4549 DWORD flag = wfd->dwFileAttributes;
4550 WCHAR *wp;
4551
4552 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4553 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4554 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4555 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4556
4557 wp = wcsrchr(wfd->cFileName, L'.');
4558 if (wp != NULL)
4559 {
4560 if (_wcsicmp(wp, L".exe") == 0 ||
4561 _wcsicmp(wp, L".com") == 0 ||
4562 _wcsicmp(wp, L".cmd") == 0 ||
4563 _wcsicmp(wp, L".bat") == 0)
4564 st_mode |= _S_IEXEC;
4565 }
4566
4567 // Copy user bits to group/other.
4568 st_mode |= (st_mode & 0700) >> 3;
4569 st_mode |= (st_mode & 0700) >> 6;
4570
4571 st.st_mode = st_mode;
4572 return getfpermst(&st, perm);
4573}
4574
4575 static char_u *
4576getftypewfd(WIN32_FIND_DATAW *wfd)
4577{
4578 DWORD flag = wfd->dwFileAttributes;
4579 DWORD tag = wfd->dwReserved0;
4580
4581 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4582 {
4583 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4584 return (char_u*)"junction";
4585 else if (tag == IO_REPARSE_TAG_SYMLINK)
4586 {
4587 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4588 return (char_u*)"linkd";
4589 else
4590 return (char_u*)"link";
4591 }
4592 return (char_u*)"reparse"; // unknown reparse point type
4593 }
4594 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4595 return (char_u*)"dir";
4596 else
4597 return (char_u*)"file";
4598}
4599
4600 static dict_T *
4601create_readdirex_item(WIN32_FIND_DATAW *wfd)
4602{
4603 dict_T *item;
4604 char_u *p;
4605 varnumber_T size, time;
4606 char_u permbuf[] = "---------";
4607
4608 item = dict_alloc();
4609 if (item == NULL)
4610 return NULL;
4611 item->dv_refcount++;
4612
4613 p = utf16_to_enc(wfd->cFileName, NULL);
4614 if (p == NULL)
4615 goto theend;
4616 if (dict_add_string(item, "name", p) == FAIL)
4617 {
4618 vim_free(p);
4619 goto theend;
4620 }
4621 vim_free(p);
4622
4623 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4624 if (dict_add_number(item, "size", size) == FAIL)
4625 goto theend;
4626
4627 // Convert FILETIME to unix time.
4628 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4629 wfd->ftLastWriteTime.dwLowDateTime)
4630 - 116444736000000000) / 10000000;
4631 if (dict_add_number(item, "time", time) == FAIL)
4632 goto theend;
4633
4634 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4635 goto theend;
4636 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4637 goto theend;
4638
4639 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4640 goto theend;
4641 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4642 goto theend;
4643
4644 return item;
4645
4646theend:
4647 dict_unref(item);
4648 return NULL;
4649}
4650# else
4651 static dict_T *
4652create_readdirex_item(char_u *path, char_u *name)
4653{
4654 dict_T *item;
4655 char *p;
4656 size_t len;
4657 stat_T st;
4658 int ret, link = FALSE;
4659 varnumber_T size;
4660 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004661 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004662 struct passwd *pw;
4663 struct group *gr;
4664
4665 item = dict_alloc();
4666 if (item == NULL)
4667 return NULL;
4668 item->dv_refcount++;
4669
4670 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4671 p = alloc(len);
4672 if (p == NULL)
4673 goto theend;
4674 vim_snprintf(p, len, "%s/%s", path, name);
4675 ret = mch_lstat(p, &st);
4676 if (ret >= 0 && S_ISLNK(st.st_mode))
4677 {
4678 link = TRUE;
4679 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004680 if (ret < 0)
4681 q = (char_u*)"link";
4682
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004683 }
4684 vim_free(p);
4685
4686 if (dict_add_string(item, "name", name) == FAIL)
4687 goto theend;
4688
4689 if (ret >= 0)
4690 {
4691 size = (varnumber_T)st.st_size;
4692 if (S_ISDIR(st.st_mode))
4693 size = 0;
4694 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004695 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004696 size = -2;
4697 if (dict_add_number(item, "size", size) == FAIL)
4698 goto theend;
4699 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4700 goto theend;
4701
4702 if (link)
4703 {
4704 if (S_ISDIR(st.st_mode))
4705 q = (char_u*)"linkd";
4706 else
4707 q = (char_u*)"link";
4708 }
4709 else
4710 q = getftypest(&st);
4711 if (dict_add_string(item, "type", q) == FAIL)
4712 goto theend;
4713 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4714 goto theend;
4715
4716 pw = getpwuid(st.st_uid);
4717 if (pw == NULL)
4718 q = (char_u*)"";
4719 else
4720 q = (char_u*)pw->pw_name;
4721 if (dict_add_string(item, "user", q) == FAIL)
4722 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004723# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004724 gr = getgrgid(st.st_gid);
4725 if (gr == NULL)
4726 q = (char_u*)"";
4727 else
4728 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004729# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004730 if (dict_add_string(item, "group", q) == FAIL)
4731 goto theend;
4732 }
4733 else
4734 {
4735 if (dict_add_number(item, "size", -1) == FAIL)
4736 goto theend;
4737 if (dict_add_number(item, "time", -1) == FAIL)
4738 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004739 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004740 goto theend;
4741 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4742 goto theend;
4743 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4744 goto theend;
4745 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4746 goto theend;
4747 }
4748 return item;
4749
4750theend:
4751 dict_unref(item);
4752 return NULL;
4753}
4754# endif
4755
4756 static int
4757compare_readdirex_item(const void *p1, const void *p2)
4758{
4759 char_u *name1, *name2;
4760
Bram Moolenaard61efa52022-07-23 09:52:04 +01004761 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4762 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004763 if (readdirex_sort == READDIR_SORT_BYTE)
4764 return STRCMP(name1, name2);
4765 else if (readdirex_sort == READDIR_SORT_IC)
4766 return STRICMP(name1, name2);
4767 else
4768 return STRCOLL(name1, name2);
4769}
4770
4771 static int
4772compare_readdir_item(const void *s1, const void *s2)
4773{
4774 if (readdirex_sort == READDIR_SORT_BYTE)
4775 return STRCMP(*(char **)s1, *(char **)s2);
4776 else if (readdirex_sort == READDIR_SORT_IC)
4777 return STRICMP(*(char **)s1, *(char **)s2);
4778 else
4779 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004780}
4781#endif
4782
Bram Moolenaarda440d22016-01-16 21:27:23 +01004783#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4784/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004785 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004786 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004787 * If "withattr" is TRUE, retrieve the names and their attributes.
4788 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004789 * Return OK for success, FAIL for failure.
4790 */
4791 int
4792readdir_core(
4793 garray_T *gap,
4794 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004795 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004796 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004797 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004798 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004799{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004800 int failed = FALSE;
4801 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004802# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004803 char_u *buf;
4804 int ok;
4805 HANDLE hFind = INVALID_HANDLE_VALUE;
4806 WIN32_FIND_DATAW wfd;
4807 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004808# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004809 DIR *dirp;
4810 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004811# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004812
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004813 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004814
4815# ifdef FEAT_EVAL
4816# define FREE_ITEM(item) do { \
4817 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004818 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004819 else \
4820 vim_free(item); \
4821 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004822
4823 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004824# else
4825# define FREE_ITEM(item) vim_free(item)
4826# endif
4827
4828# ifdef MSWIN
4829 buf = alloc(MAXPATHL);
4830 if (buf == NULL)
4831 return FAIL;
4832 STRNCPY(buf, path, MAXPATHL-5);
4833 p = buf + STRLEN(buf);
4834 MB_PTR_BACK(buf, p);
4835 if (*p == '\\' || *p == '/')
4836 *p = NUL;
4837 STRCAT(p, "\\*");
4838
4839 wn = enc_to_utf16(buf, NULL);
4840 if (wn != NULL)
4841 hFind = FindFirstFileW(wn, &wfd);
4842 ok = (hFind != INVALID_HANDLE_VALUE);
4843 if (!ok)
4844 {
4845 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004846 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004847 }
4848 else
4849 {
4850 while (ok)
4851 {
4852 int ignore;
4853 void *item;
4854 WCHAR *wp;
4855
4856 wp = wfd.cFileName;
4857 ignore = wp[0] == L'.' &&
4858 (wp[1] == NUL ||
4859 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004860 if (ignore)
4861 {
4862 ok = FindNextFileW(hFind, &wfd);
4863 continue;
4864 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004865# ifdef FEAT_EVAL
4866 if (withattr)
4867 item = (void*)create_readdirex_item(&wfd);
4868 else
4869# endif
4870 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4871 if (item == NULL)
4872 {
4873 failed = TRUE;
4874 break;
4875 }
4876
4877 if (!ignore && checkitem != NULL)
4878 {
4879 int r = checkitem(context, item);
4880
4881 if (r < 0)
4882 {
4883 FREE_ITEM(item);
4884 break;
4885 }
4886 if (r == 0)
4887 ignore = TRUE;
4888 }
4889
4890 if (!ignore)
4891 {
4892 if (ga_grow(gap, 1) == OK)
4893 ((void**)gap->ga_data)[gap->ga_len++] = item;
4894 else
4895 {
4896 failed = TRUE;
4897 FREE_ITEM(item);
4898 break;
4899 }
4900 }
4901 else
4902 FREE_ITEM(item);
4903
4904 ok = FindNextFileW(hFind, &wfd);
4905 }
4906 FindClose(hFind);
4907 }
4908
4909 vim_free(buf);
4910 vim_free(wn);
4911# else // MSWIN
4912 dirp = opendir((char *)path);
4913 if (dirp == NULL)
4914 {
4915 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004916 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004917 }
4918 else
4919 {
4920 for (;;)
4921 {
4922 int ignore;
4923 void *item;
4924
4925 dp = readdir(dirp);
4926 if (dp == NULL)
4927 break;
4928 p = (char_u *)dp->d_name;
4929
4930 ignore = p[0] == '.' &&
4931 (p[1] == NUL ||
4932 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004933 if (ignore)
4934 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004935# ifdef FEAT_EVAL
4936 if (withattr)
4937 item = (void*)create_readdirex_item(path, p);
4938 else
4939# endif
4940 item = (void*)vim_strsave(p);
4941 if (item == NULL)
4942 {
4943 failed = TRUE;
4944 break;
4945 }
4946
Bram Moolenaarfe154992022-03-22 20:42:12 +00004947 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004948 {
4949 int r = checkitem(context, item);
4950
4951 if (r < 0)
4952 {
4953 FREE_ITEM(item);
4954 break;
4955 }
4956 if (r == 0)
4957 ignore = TRUE;
4958 }
4959
4960 if (!ignore)
4961 {
4962 if (ga_grow(gap, 1) == OK)
4963 ((void**)gap->ga_data)[gap->ga_len++] = item;
4964 else
4965 {
4966 failed = TRUE;
4967 FREE_ITEM(item);
4968 break;
4969 }
4970 }
4971 else
4972 FREE_ITEM(item);
4973 }
4974
4975 closedir(dirp);
4976 }
4977# endif // MSWIN
4978
4979# undef FREE_ITEM
4980
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004981 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004982 {
4983# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004984 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004985 if (withattr)
4986 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4987 compare_readdirex_item);
4988 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004989 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4990 compare_readdir_item);
4991# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004992 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004993# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004994 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004995
4996 return failed ? FAIL : OK;
4997}
4998
4999/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005000 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005001 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005002 */
5003 int
5004delete_recursive(char_u *name)
5005{
5006 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005007 int i;
5008 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005009 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005010
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005011 // A symbolic link to a directory itself is deleted, not the directory it
5012 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005013 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005014# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005015 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005016# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005017 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005018# endif
5019 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005020 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005021 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005022 if (exp == NULL)
5023 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005024 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005025 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005026 for (i = 0; i < ga.ga_len; ++i)
5027 {
5028 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5029 ((char_u **)ga.ga_data)[i]);
5030 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005031 // Remember the failure but continue deleting any further
5032 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005033 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005034 }
5035 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005036 if (mch_rmdir(exp) != 0)
5037 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005038 }
5039 else
5040 result = -1;
5041 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005042 }
5043 else
5044 result = mch_remove(name) == 0 ? 0 : -1;
5045
5046 return result;
5047}
5048#endif
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005053# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5054/*
5055 * Open temporary directory and take file lock to prevent
5056 * to be auto-cleaned.
5057 */
5058 static void
5059vim_opentempdir(void)
5060{
5061 DIR *dp = NULL;
5062
5063 if (vim_tempdir_dp != NULL)
5064 return;
5065
5066 dp = opendir((const char*)vim_tempdir);
5067
5068 if (dp != NULL)
5069 {
5070 vim_tempdir_dp = dp;
5071 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5072 }
5073}
5074
5075/*
5076 * Close temporary directory - it automatically release file lock.
5077 */
5078 static void
5079vim_closetempdir(void)
5080{
5081 if (vim_tempdir_dp != NULL)
5082 {
5083 closedir(vim_tempdir_dp);
5084 vim_tempdir_dp = NULL;
5085 }
5086}
5087# endif
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089/*
5090 * Delete the temp directory and all files it contains.
5091 */
5092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005093vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (vim_tempdir != NULL)
5096 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005097# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5098 vim_closetempdir();
5099# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005100 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005101 gettail(vim_tempdir)[-1] = NUL;
5102 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005103 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005108 * Directory "tempdir" was created. Expand this name to a full path and put
5109 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5110 * "tempdir" must be no longer than MAXPATHL.
5111 */
5112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005113vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005114{
5115 char_u *buf;
5116
Bram Moolenaar964b3742019-05-24 18:54:09 +02005117 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005118 if (buf != NULL)
5119 {
5120 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5121 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005122 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005123 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005124# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5125 vim_opentempdir();
5126# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005127 vim_free(buf);
5128 }
5129}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005130#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005131
5132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 * vim_tempname(): Return a unique name that can be used for a temp file.
5134 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005135 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5136 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 *
5138 * The returned pointer is to allocated memory.
5139 * The returned pointer is NULL if no valid name was found.
5140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005142vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005143 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005144 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005148#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005149 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#else
5151 char_u itmp[TEMPNAMELEN];
5152#endif
5153
5154#ifdef TEMPDIRNAMES
5155 static char *(tempdirs[]) = {TEMPDIRNAMES};
5156 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005158 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159# endif
5160
5161 /*
5162 * This will create a directory for private use by this instance of Vim.
5163 * This is done once, and the same directory is used for all temp files.
5164 * This method avoids security problems because of symlink attacks et al.
5165 * It's also a bit faster, because we only need to check for an existing
5166 * file when creating the directory and not for each temp file.
5167 */
5168 if (vim_tempdir == NULL)
5169 {
5170 /*
5171 * Try the entries in TEMPDIRNAMES to create the temp directory.
5172 */
K.Takataeeec2542021-06-02 13:28:16 +02005173 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005175# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005176 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005177 long nr;
5178 long off;
5179# endif
5180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005181 // Expand $TMP, leave room for "/v1100000/999999999".
5182 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005184 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005186 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005187 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
Bram Moolenaareaf03392009-11-17 11:08:52 +00005189# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005190 {
5191# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005192 // Make sure the umask doesn't remove the executable bit.
5193 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005194 mode_t umask_save = umask(077);
5195# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005196 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005197 STRCAT(itmp, "vXXXXXX");
5198 if (mkdtemp((char *)itmp) != NULL)
5199 vim_settempdir(itmp);
5200# if defined(UNIX) || defined(VMS)
5201 (void)umask(umask_save);
5202# endif
5203 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005204# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // Get an arbitrary number of up to 6 digits. When it's
5206 // unlikely that it already exists it will be faster,
5207 // otherwise it doesn't matter. The use of mkdir() avoids any
5208 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005210 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005212 // Try up to 10000 different values until we find a name that
5213 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 for (off = 0; off < 10000L; ++off)
5215 {
5216 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005217# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaareaf03392009-11-17 11:08:52 +00005221 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5222# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 // If mkdir() does not set errno to EEXIST, check for
5224 // existing file here. There is a race condition then,
5225 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (mch_stat((char *)itmp, &st) >= 0)
5227 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005228# endif
5229# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 // Make sure the umask doesn't remove the executable bit.
5231 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005235# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (r == 0)
5239 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005240 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 break;
5242 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005243# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005244 // If the mkdir() didn't fail because the file/dir exists,
5245 // we probably can't create any dir here, try another
5246 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005251# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (vim_tempdir != NULL)
5253 break;
5254 }
5255 }
5256 }
5257
5258 if (vim_tempdir != NULL)
5259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005260 // There is no need to check if the file exists, because we own the
5261 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5263 return vim_strsave(itmp);
5264 }
5265
5266 return NULL;
5267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005268#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
Bram Moolenaar4f974752019-02-17 17:44:42 +01005270# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005271 WCHAR wszTempFile[_MAX_PATH + 1];
5272 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005273 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 char_u *retval;
5275 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005276 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005277 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005279 wcscpy(itmp, L"");
5280 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005281 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005282 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005283 wszTempFile[1] = L'\\';
5284 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005285 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005286 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005287
5288 // randomize the name to avoid collisions
5289 i = mch_get_pid() + extra_char;
5290 buf4[1] = chartab[i % 36];
5291 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005292 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005294 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005295 // GetTempFileName() will create the file, we don't want that
5296 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005298 // Backslashes in a temp file name cause problems when filtering with
5299 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005300 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005301 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005302 shname = gettail(p_sh);
5303 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5304 || strstr((char *)shname, "pwsh") != NULL ))
5305 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 for (p = retval; *p; ++p)
5307 if (*p == '\\')
5308 *p = '/';
5309 return retval;
5310
Bram Moolenaar4f974752019-02-17 17:44:42 +01005311# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005314 char_u *p;
5315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005316 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005317 p = tmpnam((char *)itmp);
5318 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 return NULL;
5320# else
5321 char_u *p;
5322
5323# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005324 // mktemp() is not working on VMS. It seems to be
5325 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 sprintf((char *)itmp, "VIM%c", extra_char);
5327 p = (char_u *)tempnam("tmp:", (char *)itmp);
5328 if (p != NULL)
5329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005330 // VMS will use '.LIS' if we don't explicitly specify an extension,
5331 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 STRCPY(itmp, p);
5333 STRCAT(itmp, ".txt");
5334 free(p);
5335 }
5336 else
5337 return NULL;
5338# else
5339 STRCPY(itmp, TEMPNAME);
5340 if ((p = vim_strchr(itmp, '?')) != NULL)
5341 *p = extra_char;
5342 if (mktemp((char *)itmp) == NULL)
5343 return NULL;
5344# endif
5345# endif
5346
5347 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005348# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005349#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350}
5351
5352#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5353/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005354 * Convert all backslashes in fname to forward slashes in-place, unless when
5355 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 */
5357 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005358forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
5360 char_u *p;
5361
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005362 if (path_with_url(fname))
5363 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005365 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005366 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005368 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 *p = '/';
5370}
5371#endif
5372
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005373/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005374 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5375 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5376 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * Used for autocommands and 'wildignore'.
5378 * Returns TRUE if there is a match, FALSE otherwise.
5379 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005380 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 char_u *pattern, // pattern to match with
5383 regprog_T **prog, // pre-compiled regprog or NULL
5384 char_u *fname, // full path of file name
5385 char_u *sfname, // short file name or NULL
5386 char_u *tail, // tail of path
5387 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 regmatch_T regmatch;
5390 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005392 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005393 if (prog != NULL)
5394 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005396 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398 /*
5399 * Try for a match with the pattern with:
5400 * 1. the full file name, when the pattern has a '/'.
5401 * 2. the short file name, when the pattern has a '/'.
5402 * 3. the tail of the file name, when the pattern has no '/'.
5403 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005404 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 && ((allow_dirs
5406 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5407 || (sfname != NULL
5408 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005409 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 result = TRUE;
5411
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005412 if (prog != NULL)
5413 *prog = regmatch.regprog;
5414 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005415 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return result;
5417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419/*
5420 * Return TRUE if a file matches with a pattern in "list".
5421 * "list" is a comma-separated list of patterns, like 'wildignore'.
5422 * "sfname" is the short file name or NULL, "ffname" the long file name.
5423 */
5424 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427 char_u buf[100];
5428 char_u *tail;
5429 char_u *regpat;
5430 char allow_dirs;
5431 int match;
5432 char_u *p;
5433
5434 tail = gettail(sfname);
5435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005436 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 p = list;
5438 while (*p)
5439 {
5440 copy_option_part(&p, buf, 100, ",");
5441 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5442 if (regpat == NULL)
5443 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005444 match = match_file_pat(regpat, NULL, ffname, sfname,
5445 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 vim_free(regpat);
5447 if (match)
5448 return TRUE;
5449 }
5450 return FALSE;
5451}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453/*
5454 * Convert the given pattern "pat" which has shell style wildcards in it, into
5455 * a regular expression, and return the result in allocated memory. If there
5456 * is a directory path separator to be matched, then TRUE is put in
5457 * allow_dirs, otherwise FALSE is put there -- webb.
5458 * Handle backslashes before special characters, like "\*" and "\ ".
5459 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 * Returns NULL when out of memory.
5461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463file_pat_to_reg_pat(
5464 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005465 char_u *pat_end, // first char after pattern or NULL
5466 char *allow_dirs, // Result passed back out in here
5467 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005469 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 char_u *endp;
5471 char_u *reg_pat;
5472 char_u *p;
5473 int i;
5474 int nested = 0;
5475 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 if (allow_dirs != NULL)
5478 *allow_dirs = FALSE;
5479 if (pat_end == NULL)
5480 pat_end = pat + STRLEN(pat);
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 for (p = pat; p < pat_end; p++)
5483 {
5484 switch (*p)
5485 {
5486 case '*':
5487 case '.':
5488 case ',':
5489 case '{':
5490 case '}':
5491 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005492 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 break;
5494#ifdef BACKSLASH_IN_FILENAME
5495 case '\\':
5496 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 break;
5499#endif
5500 default:
5501 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005502 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 ++p;
5505 ++size;
5506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 break;
5508 }
5509 }
5510 reg_pat = alloc(size + 1);
5511 if (reg_pat == NULL)
5512 return NULL;
5513
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
5516 if (pat[0] == '*')
5517 while (pat[0] == '*' && pat < pat_end - 1)
5518 pat++;
5519 else
5520 reg_pat[i++] = '^';
5521 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005522 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
5524 while (endp - pat > 0 && *endp == '*')
5525 endp--;
5526 add_dollar = FALSE;
5527 }
5528 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5529 {
5530 switch (*p)
5531 {
5532 case '*':
5533 reg_pat[i++] = '.';
5534 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005535 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005536 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 break;
5538 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 case '~':
5540 reg_pat[i++] = '\\';
5541 reg_pat[i++] = *p;
5542 break;
5543 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 reg_pat[i++] = '.';
5545 break;
5546 case '\\':
5547 if (p[1] == NUL)
5548 break;
5549#ifdef BACKSLASH_IN_FILENAME
5550 if (!no_bslash)
5551 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005552 // translate:
5553 // "\x" to "\\x" e.g., "dir\file"
5554 // "\*" to "\\.*" e.g., "dir\*.c"
5555 // "\?" to "\\." e.g., "dir\??.c"
5556 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5558 && p[1] != '+')
5559 {
5560 reg_pat[i++] = '[';
5561 reg_pat[i++] = '\\';
5562 reg_pat[i++] = '/';
5563 reg_pat[i++] = ']';
5564 if (allow_dirs != NULL)
5565 *allow_dirs = TRUE;
5566 break;
5567 }
5568 }
5569#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005570 // Undo escaping from ExpandEscape():
5571 // foo\?bar -> foo?bar
5572 // foo\%bar -> foo%bar
5573 // foo\,bar -> foo,bar
5574 // foo\ bar -> foo bar
5575 // Don't unescape \, * and others that are also special in a
5576 // regexp.
5577 // An escaped { must be unescaped since we use magic not
5578 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if (*++p == '?'
5580#ifdef BACKSLASH_IN_FILENAME
5581 && no_bslash
5582#endif
5583 )
5584 reg_pat[i++] = '?';
5585 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005586 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005587 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005588 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005589 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5590 {
5591 reg_pat[i++] = '\\';
5592 reg_pat[i++] = '{';
5593 p += 2;
5594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 else
5596 {
5597 if (allow_dirs != NULL && vim_ispathsep(*p)
5598#ifdef BACKSLASH_IN_FILENAME
5599 && (!no_bslash || *p != '\\')
5600#endif
5601 )
5602 *allow_dirs = TRUE;
5603 reg_pat[i++] = '\\';
5604 reg_pat[i++] = *p;
5605 }
5606 break;
5607#ifdef BACKSLASH_IN_FILENAME
5608 case '/':
5609 reg_pat[i++] = '[';
5610 reg_pat[i++] = '\\';
5611 reg_pat[i++] = '/';
5612 reg_pat[i++] = ']';
5613 if (allow_dirs != NULL)
5614 *allow_dirs = TRUE;
5615 break;
5616#endif
5617 case '{':
5618 reg_pat[i++] = '\\';
5619 reg_pat[i++] = '(';
5620 nested++;
5621 break;
5622 case '}':
5623 reg_pat[i++] = '\\';
5624 reg_pat[i++] = ')';
5625 --nested;
5626 break;
5627 case ',':
5628 if (nested)
5629 {
5630 reg_pat[i++] = '\\';
5631 reg_pat[i++] = '|';
5632 }
5633 else
5634 reg_pat[i++] = ',';
5635 break;
5636 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005637 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005639 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 *allow_dirs = TRUE;
5641 reg_pat[i++] = *p;
5642 break;
5643 }
5644 }
5645 if (add_dollar)
5646 reg_pat[i++] = '$';
5647 reg_pat[i] = NUL;
5648 if (nested != 0)
5649 {
5650 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005651 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005653 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005654 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
5656 return reg_pat;
5657}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005658
5659#if defined(EINTR) || defined(PROTO)
5660/*
5661 * Version of read() that retries when interrupted by EINTR (possibly
5662 * by a SIGWINCH).
5663 */
5664 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005665read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005666{
5667 long ret;
5668
5669 for (;;)
5670 {
5671 ret = vim_read(fd, buf, bufsize);
5672 if (ret >= 0 || errno != EINTR)
5673 break;
5674 }
5675 return ret;
5676}
5677
5678/*
5679 * Version of write() that retries when interrupted by EINTR (possibly
5680 * by a SIGWINCH).
5681 */
5682 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005684{
5685 long ret = 0;
5686 long wlen;
5687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005688 // Repeat the write() so long it didn't fail, other than being interrupted
5689 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005690 while (ret < (long)bufsize)
5691 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005692 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005693 if (wlen < 0)
5694 {
5695 if (errno != EINTR)
5696 break;
5697 }
5698 else
5699 ret += wlen;
5700 }
5701 return ret;
5702}
5703#endif