blob: ab867b7c8fe06919b3dcec9701ab56643e220d4c [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 {
592 curbuf->b_p_eol = TRUE;
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100593 curbuf->b_p_eof = FALSE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000594 curbuf->b_start_eol = TRUE;
595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000597 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 }
599
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100600 // Create a swap file now, so that other Vims are warned that we are
601 // editing this file.
602 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000606 if (!read_stdin && (curbuf != old_curbuf
607 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
608 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
609 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000610 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000611 if (!read_buffer)
612 close(fd);
613 return FAIL;
614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000617 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
618 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100619 {
620 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
621
622 /*
623 * If the group-read bit is set but not the world-read bit, then
624 * the group must be equal to the group of the original file. If
625 * we can't make that happen then reset the group-read bit. This
626 * avoids making the swap file readable to more users when the
627 * primary group of the user is too permissive.
628 */
629 if ((swap_mode & 044) == 040)
630 {
631 stat_T swap_st;
632
633 if (mch_stat((char *)swap_fname, &swap_st) >= 0
634 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200635# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100636 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200637 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200638# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200639 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100640 swap_mode &= 0600;
641 }
642
643 (void)mch_setperm(swap_fname, (long)swap_mode);
644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#endif
646 }
647
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200648 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (swap_exists_action == SEA_QUIT)
650 {
651 if (!read_buffer && !read_stdin)
652 close(fd);
653 return FAIL;
654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658 /*
659 * Set '[ mark to the line above where the lines go (line 1 if zero).
660 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100661 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
663 curbuf->b_op_start.col = 0;
664
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100665 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
666 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
667 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (!read_buffer)
670 {
671 int m = msg_scroll;
672 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674 /*
675 * The file must be closed again, the autocommands may want to change
676 * the file before reading it.
677 */
678 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 /*
682 * The output from the autocommands should not overwrite anything and
683 * should not be overwritten: Set msg_scroll, restore its value if no
684 * output was done.
685 */
686 msg_scroll = TRUE;
687 if (filtering)
688 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
689 FALSE, curbuf, eap);
690 else if (read_stdin)
691 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
692 FALSE, curbuf, eap);
693 else if (newfile)
694 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
695 FALSE, curbuf, eap);
696 else
697 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
698 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100700 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
701 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
702 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100703 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (msg_scrolled == n)
706 msg_scroll = m;
707
708#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100709 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
711 --no_wait_return;
712 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100713 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 return FAIL;
715 }
716#endif
717 /*
718 * Don't allow the autocommands to change the current buffer.
719 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000720 *
721 * Don't allow the autocommands to change the buffer name either
722 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 */
724 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000725 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
726 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
728 {
729 --no_wait_return;
730 msg_scroll = msg_save;
731 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000732 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000734 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100735 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 return FAIL;
737 }
738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100740 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
742
743 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
744 {
745 /*
746 * Show the user that we are busy reading the input. Sometimes this
747 * may take a while. When reading from stdin another program may
748 * still be running, don't move the cursor to the last line, unless
749 * always using the GUI.
750 */
751 if (read_stdin)
752 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100753 if (!is_not_a_term())
754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200756# ifdef VIMDLL
757 if (!gui.in_use)
758# endif
759 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#endif
761#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100762 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100763 if (gui.in_use && !gui.dying && !gui.starting)
764 {
Amon Sha10197932022-02-21 15:07:12 +0000765 // make a copy, gui_write() may try to change it
766 p = vim_strsave((char_u *)_("Reading from stdin..."));
767 if (p != NULL)
768 {
769 gui_write(p, (int)STRLEN(p));
770 vim_free(p);
771 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
776 else if (!read_buffer)
777 filemess(curbuf, sfname, (char_u *)"", 0);
778 }
779
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100780 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 /*
783 * Set linecnt now, before the "retry" caused by a wrong guess for
784 * fileformat, and after the autocommands, which may change them.
785 */
786 linecnt = curbuf->b_ml.ml_line_count;
787
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000789 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000790 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000791 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000792 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000793 curbuf->b_bad_char = eap->bad_char;
794 }
795 else
796 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000799 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 */
801 if (eap != NULL && eap->force_enc != 0)
802 {
803 fenc = enc_canonize(eap->cmd + eap->force_enc);
804 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000805 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807 else if (curbuf->b_p_bin)
808 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100809 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 fenc_alloced = FALSE;
811 }
812 else if (curbuf->b_help)
813 {
814 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000815 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100817 // Help files are either utf-8 or latin1. Try utf-8 first, if this
818 // fails it must be latin1.
819 // Always do this when 'encoding' is "utf-8". Otherwise only do
820 // this when needed to avoid [converted] remarks all the time.
821 // It is needed when the first line contains non-ASCII characters.
822 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 fenc = (char_u *)"latin1";
824 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000825 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000827 fc = fname[STRLEN(fname) - 1];
828 if (TOLOWER_ASC(fc) == 'x')
829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // Read the first line (and a bit more). Immediately rewind to
831 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100832 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200833 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000834 for (p = firstline; p < firstline + len; ++p)
835 if (*p >= 0x80)
836 {
837 c = TRUE;
838 break;
839 }
840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842
843 if (c)
844 {
845 fenc_next = fenc;
846 fenc = (char_u *)"utf-8";
847
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100848 // When the file is utf-8 but a character doesn't fit in
849 // 'encoding' don't retry. In help text editing utf-8 bytes
850 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000851 if (!enc_utf8)
852 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 fenc_alloced = FALSE;
855 }
856 else if (*p_fencs == NUL)
857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 fenc_alloced = FALSE;
860 }
861 else
862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100863 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200864 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 /*
868 * Jump back here to retry reading the file in different ways.
869 * Reasons to retry:
870 * - encoding conversion failed: try another one from "fenc_next"
871 * - BOM detected and fenc was set, need to setup conversion
872 * - "fileformat" check failed: try another
873 *
874 * Variables set for special retry actions:
875 * "file_rewind" Rewind the file to start reading it again.
876 * "advance_fenc" Advance "fenc" using "fenc_next".
877 * "skip_read" Re-use already read bytes (BOM detected).
878 * "did_iconv" iconv() conversion failed, try 'charconvert'.
879 * "keep_fileformat" Don't reset "fileformat".
880 *
881 * Other status indicators:
882 * "tmpname" When != NULL did conversion with 'charconvert'.
883 * Output file has to be deleted afterwards.
884 * "iconv_fd" When != -1 did conversion with iconv().
885 */
886retry:
887
888 if (file_rewind)
889 {
890 if (read_buffer)
891 {
892 read_buf_lnum = 1;
893 read_buf_col = 0;
894 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200895 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100897 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 error = TRUE;
899 goto failed;
900 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100901 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200903 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000905 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000908 curbuf->b_start_bomb = FALSE;
909 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000910 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912
913 /*
914 * When retrying with another "fenc" and the first time "fileformat"
915 * will be reset.
916 */
917 if (keep_fileformat)
918 keep_fileformat = FALSE;
919 else
920 {
921 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000924 try_unix = try_dos = try_mac = FALSE;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100927 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100929 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100931 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933
Bram Moolenaar13505972019-01-24 15:04:48 +0100934#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 if (iconv_fd != (iconv_t)-1)
936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100937 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 iconv_close(iconv_fd);
939 iconv_fd = (iconv_t)-1;
940 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 if (advance_fenc)
944 {
945 /*
946 * Try the next entry in 'fileencodings'.
947 */
948 advance_fenc = FALSE;
949
950 if (eap != NULL && eap->force_enc != 0)
951 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100952 // Conversion given with "++cc=" wasn't possible, read
953 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000955 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (fenc_alloced)
957 vim_free(fenc);
958 fenc = (char_u *)"";
959 fenc_alloced = FALSE;
960 }
961 else
962 {
963 if (fenc_alloced)
964 vim_free(fenc);
965 if (fenc_next != NULL)
966 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200967 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969 else
970 {
971 fenc = (char_u *)"";
972 fenc_alloced = FALSE;
973 }
974 }
975 if (tmpname != NULL)
976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100978 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 }
981
982 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000983 * Conversion may be required when the encoding of the file is different
984 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 */
986 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000987 converted = need_conversion(fenc);
988 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100991 // "ucs-bom" means we need to check the first bytes of the file
992 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (STRCMP(fenc, ENC_UCSBOM) == 0)
994 fio_flags = FIO_UCSBOM;
995
996 /*
997 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
998 * done. This is handled below after read(). Prepare the
999 * fio_flags to avoid having to parse the string each time.
1000 * Also check for Unicode to Latin1 conversion, because iconv()
1001 * appears not to handle this correctly. This works just like
1002 * conversion to UTF-8 except how the resulting character is put in
1003 * the buffer.
1004 */
1005 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1006 fio_flags = get_fio_flags(fenc);
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
1010 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1011 * is handled with MultiByteToWideChar().
1012 */
1013 if (fio_flags == 0)
1014 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar13505972019-01-24 15:04:48 +01001017#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (fio_flags == 0)
1020 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaar13505972019-01-24 15:04:48 +01001023#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 /*
1025 * Try using iconv() if we can't convert internally.
1026 */
1027 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001028# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 )
1032 iconv_fd = (iconv_t)my_iconv_open(
1033 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar13505972019-01-24 15:04:48 +01001036#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /*
1038 * Use the 'charconvert' expression when conversion is required
1039 * and we can't do it internally or with iconv().
1040 */
1041 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001042 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001043# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 )
1047 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001048# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001050# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001051 // Skip conversion when it's already done (retry for wrong
1052 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (tmpname == NULL)
1054 {
1055 tmpname = readfile_charconvert(fname, fenc, &fd);
1056 if (tmpname == NULL)
1057 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 advance_fenc = TRUE;
1060 if (fd < 0)
1061 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001062 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001063 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 error = TRUE;
1065 goto failed;
1066 }
1067 goto retry;
1068 }
1069 }
1070 }
1071 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
1074 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001075#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 )
1079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Conversion wanted but we can't.
1081 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 advance_fenc = TRUE;
1083 goto retry;
1084 }
1085 }
1086 }
1087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001088 // Set "can_retry" when it's possible to rewind the file and try with
1089 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1090 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001091 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 if (!skip_read)
1094 {
1095 linerest = 0;
1096 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001097 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 skip_count = lines_to_skip;
1099 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001101#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001102 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1103 && curbuf->b_ffname != NULL
1104 && curbuf->b_p_udf
1105 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001106 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001107 && !read_stdin
1108 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001109 if (read_undo_file)
1110 sha256_start(&sha_ctx);
1111#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001112#ifdef FEAT_CRYPT
1113 if (curbuf->b_cryptstate != NULL)
1114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001115 // Need to free the state, but keep the key, don't want to ask for
1116 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001117 crypt_free_state(curbuf->b_cryptstate);
1118 curbuf->b_cryptstate = NULL;
1119 }
1120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122
1123 while (!error && !got_int)
1124 {
1125 /*
1126 * We allocate as much space for the file as we can get, plus
1127 * space for the old line plus room for one terminating NUL.
1128 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001129 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001131 if (!skip_read)
1132 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001133#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001134 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001135#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001136 // Use buffer >= 64K. Add linerest to double the size if the
1137 // line gets very long, to avoid a lot of copying. But don't
1138 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001139 size = 0x10000L + linerest;
1140 if (size > 0x100000L)
1141 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001142#endif
1143 }
1144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001146 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
1148 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001149 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 size = 1;
1151 }
1152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 if (!skip_read)
1155 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001156 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001158 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 FALSE)) != NULL)
1160 break;
1161 }
1162 if (new_buffer == NULL)
1163 {
1164 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1165 error = TRUE;
1166 break;
1167 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001168 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1170 vim_free(buffer);
1171 buffer = new_buffer;
1172 ptr = buffer + linerest;
1173 line_start = buffer;
1174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 // May need room to translate into.
1176 // For iconv() we don't really know the required space, use a
1177 // factor ICONV_MULT.
1178 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1179 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1180 // become up to 4 bytes, size must be multiple of 2
1181 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1182 // multiple of 2
1183 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1184 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001185 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001186#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (iconv_fd != (iconv_t)-1)
1188 size = size / ICONV_MULT;
1189 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (fio_flags & FIO_LATIN1)
1192 size = size / 2;
1193 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1194 size = (size * 2 / 3) & ~1;
1195 else if (fio_flags & FIO_UCS4)
1196 size = (size * 2 / 3) & ~3;
1197 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001198 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001199#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001201 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001202#endif
1203#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001205 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206#endif
1207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (conv_restlen > 0)
1209 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001210 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 mch_memmove(ptr, conv_rest, conv_restlen);
1212 ptr += conv_restlen;
1213 size -= conv_restlen;
1214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 if (read_buffer)
1217 {
1218 /*
1219 * Read bytes from curbuf. Used for converting text read
1220 * from stdin.
1221 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001222 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (read_buf_lnum > from)
1224 size = 0;
1225 else
1226 {
1227 int n, ni;
1228 long tlen;
1229
1230 tlen = 0;
1231 for (;;)
1232 {
1233 p = ml_get(read_buf_lnum) + read_buf_col;
1234 n = (int)STRLEN(p);
1235 if ((int)tlen + n + 1 > size)
1236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001237 // Filled up to "size", append partial line.
1238 // Change NL to NUL to reverse the effect done
1239 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001240 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 for (ni = 0; ni < n; ++ni)
1242 {
1243 if (p[ni] == NL)
1244 ptr[tlen++] = NUL;
1245 else
1246 ptr[tlen++] = p[ni];
1247 }
1248 read_buf_col += n;
1249 break;
1250 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001251
1252 // Append whole line and new-line. Change NL
1253 // to NUL to reverse the effect done below.
1254 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001256 if (p[ni] == NL)
1257 ptr[tlen++] = NUL;
1258 else
1259 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001261 ptr[tlen++] = NL;
1262 read_buf_col = 0;
1263 if (++read_buf_lnum > from)
1264 {
1265 // When the last line didn't have an
1266 // end-of-line don't add it now either.
1267 if (!curbuf->b_p_eol)
1268 --tlen;
1269 size = tlen;
1270 eof = TRUE;
1271 break;
1272 }
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 }
1276 }
1277 else
1278 {
1279 /*
1280 * Read bytes from the file.
1281 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001282# ifdef FEAT_SODIUM
1283 // Let the crypt layer work with a buffer size of 8192
1284 if (filesize == 0)
1285 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001286 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001287 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1288 + crypto_secretstream_xchacha20poly1305_ABYTES;
1289
1290 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1291 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1292 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1293# endif
1294 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001295 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001296 filesize_count += size;
1297 // hit end of file
1298 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001301#ifdef FEAT_CRYPT
1302 /*
1303 * At start of file: Check for magic number of encryption.
1304 */
1305 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001306 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001307 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1308 &filesize, newfile, sfname,
1309 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001310# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001311 if (curbuf->b_cryptstate != NULL
1312 && !crypt_works_inplace(curbuf->b_cryptstate))
1313 // reading undo file requires crypt_decode_inplace()
1314 read_undo_file = FALSE;
1315# endif
1316 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001317 /*
1318 * Decrypt the read bytes. This is done before checking for
1319 * EOF because the crypt layer may be buffering.
1320 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001321 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1322 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001323 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001324# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001325 if (crypt_works_inplace(curbuf->b_cryptstate))
1326 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001327# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001328 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1329 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001330# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001331 }
1332 else
1333 {
1334 char_u *newptr = NULL;
1335 int decrypted_size;
1336
1337 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001338 curbuf->b_cryptstate, ptr, size,
1339 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001340
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001341 if (decrypted_size < 0)
1342 {
1343 // error message already given
1344 error = TRUE;
1345 vim_free(newptr);
1346 break;
1347 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001348 // If the crypt layer is buffering, not producing
1349 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001350 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001351 continue;
1352
1353 if (linerest == 0)
1354 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 // Simple case: reuse returned buffer (may be
1356 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001357 new_buffer = newptr;
1358 }
1359 else
1360 {
1361 long_u new_size;
1362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001363 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001364 new_size = (long_u)(decrypted_size + linerest + 1);
1365 new_buffer = lalloc(new_size, FALSE);
1366 if (new_buffer == NULL)
1367 {
1368 do_outofmem_msg(new_size);
1369 error = TRUE;
1370 break;
1371 }
1372
1373 mch_memmove(new_buffer, buffer, linerest);
1374 if (newptr != NULL)
1375 mch_memmove(new_buffer + linerest, newptr,
1376 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001377 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001378 }
1379
1380 if (new_buffer != NULL)
1381 {
1382 vim_free(buffer);
1383 buffer = new_buffer;
1384 new_buffer = NULL;
1385 line_start = buffer;
1386 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001387 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001388 }
1389 size = decrypted_size;
1390 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001391# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001392 }
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (size <= 0)
1396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001400 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001401 /*
1402 * Reached end-of-file but some trailing bytes could
1403 * not be converted. Truncated file?
1404 */
1405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001407 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001408#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001409 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001410#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001411 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001412 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001413 if (can_retry)
1414 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001415 if (conv_error == 0)
1416 conv_error = curbuf->b_ml.ml_line_count
1417 - linecnt + 1;
1418 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001419 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001420 else if (illegal_byte == 0)
1421 illegal_byte = curbuf->b_ml.ml_line_count
1422 - linecnt + 1;
1423 if (bad_char_behavior == BAD_DROP)
1424 {
1425 *(ptr - conv_restlen) = NUL;
1426 conv_restlen = 0;
1427 }
1428 else
1429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001430 // Replace the trailing bytes with the replacement
1431 // character if we were converting; if we weren't,
1432 // leave the UTF8 checking code to do it, as it
1433 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001434 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001435#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001436 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001437#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001438 ))
1439 {
1440 while (conv_restlen > 0)
1441 {
1442 *(--ptr) = bad_char_behavior;
1443 --conv_restlen;
1444 }
1445 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001446 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001447#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001448 if (iconv_fd != (iconv_t)-1)
1449 {
1450 iconv_close(iconv_fd);
1451 iconv_fd = (iconv_t)-1;
1452 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001453#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001454 }
1455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 skip_read = FALSE;
1459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 /*
1461 * At start of file (or after crypt magic number): Check for BOM.
1462 * Also check for a BOM for other Unicode encodings, but not after
1463 * converting with 'charconvert' or when a BOM has already been
1464 * found.
1465 */
1466 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001467#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001468 || (cryptkey != NULL
1469 && filesize == crypt_get_header_len(
1470 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 )
1473 && (fio_flags == FIO_UCSBOM
1474 || (!curbuf->b_p_bomb
1475 && tmpname == NULL
1476 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1477 {
1478 char_u *ccname;
1479 int blen;
1480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001481 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (size < 2 || curbuf->b_p_bin)
1483 ccname = NULL;
1484 else
1485 ccname = check_for_bom(ptr, size, &blen,
1486 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1487 if (ccname != NULL)
1488 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001489 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 filesize += blen;
1491 size -= blen;
1492 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001493 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001496 curbuf->b_start_bomb = TRUE;
1497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499
1500 if (fio_flags == FIO_UCSBOM)
1501 {
1502 if (ccname == NULL)
1503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 advance_fenc = TRUE;
1506 }
1507 else
1508 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001509 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if (fenc_alloced)
1511 vim_free(fenc);
1512 fenc = ccname;
1513 fenc_alloced = FALSE;
1514 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 skip_read = TRUE;
1517 goto retry;
1518 }
1519 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001520
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001522 ptr -= conv_restlen;
1523 size += conv_restlen;
1524 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 /*
1526 * Break here for a read error or end-of-file.
1527 */
1528 if (size <= 0)
1529 break;
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
Bram Moolenaar13505972019-01-24 15:04:48 +01001532#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (iconv_fd != (iconv_t)-1)
1534 {
1535 /*
1536 * Attempt conversion of the read bytes to 'encoding' using
1537 * iconv().
1538 */
1539 const char *fromp;
1540 char *top;
1541 size_t from_size;
1542 size_t to_size;
1543
1544 fromp = (char *)ptr;
1545 from_size = size;
1546 ptr += size;
1547 top = (char *)ptr;
1548 to_size = real_size - size;
1549
1550 /*
1551 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001552 * another conversion. Except for when there is no
1553 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001555 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1556 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1558 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001559 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001560 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001561 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001562 if (conv_error == 0)
1563 conv_error = readfile_linenr(linecnt,
1564 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001566 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001567 ++fromp;
1568 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001569 if (bad_char_behavior == BAD_KEEP)
1570 {
1571 *top++ = *(fromp - 1);
1572 --to_size;
1573 }
1574 else if (bad_char_behavior != BAD_DROP)
1575 {
1576 *top++ = bad_char_behavior;
1577 --to_size;
1578 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 if (from_size > 0)
1582 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001583 // Some remaining characters, keep them for the next
1584 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1586 conv_restlen = (int)from_size;
1587 }
1588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001589 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 line_start = ptr - linerest;
1591 mch_memmove(line_start, buffer, (size_t)linerest);
1592 size = (long)((char_u *)top - ptr);
1593 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar4f974752019-02-17 17:44:42 +01001596#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (fio_flags & FIO_CODEPAGE)
1598 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001599 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001600 WCHAR ucs2buf[3];
1601 int ucs2len;
1602 int codepage = FIO_GET_CP(fio_flags);
1603 int bytelen;
1604 int found_bad;
1605 char replstr[2];
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /*
1608 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001609 * a codepage, using standard MS-Windows functions. This
1610 * requires two steps:
1611 * 1. convert from 'fileencoding' to ucs-2
1612 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001614 * Because there may be illegal bytes AND an incomplete byte
1615 * sequence at the end, we may have to do the conversion one
1616 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001619 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001620 if (bad_char_behavior > 0)
1621 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001623 replstr[0] = '?';
1624 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001627 * Move the bytes to the end of the buffer, so that we have
1628 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001630 src = ptr + real_size - size;
1631 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001633 /*
1634 * Do the conversion.
1635 */
1636 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001637 while (size > 0)
1638 {
1639 found_bad = FALSE;
1640
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001641# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001642 if (codepage == CP_UTF8)
1643 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001644 // Handle CP_UTF8 input ourselves to be able to handle
1645 // trailing bytes properly.
1646 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001647 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001648 if (bytelen > size)
1649 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001650 // Only got some bytes of a character. Normally
1651 // it's put in "conv_rest", but if it's too long
1652 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001653 if (bytelen <= CONV_RESTLEN)
1654 break;
1655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001656 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001657 bytelen = size;
1658 found_bad = TRUE;
1659 }
1660 else
1661 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001662 int u8c = utf_ptr2char(src);
1663
Bram Moolenaar86e01082005-12-29 22:45:34 +00001664 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001665 found_bad = TRUE;
1666 ucs2buf[0] = u8c;
1667 ucs2len = 1;
1668 }
1669 }
1670 else
1671# endif
1672 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // We don't know how long the byte sequence is, try
1674 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001675 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1676 ++bytelen)
1677 {
1678 ucs2len = MultiByteToWideChar(codepage,
1679 MB_ERR_INVALID_CHARS,
1680 (LPCSTR)src, bytelen,
1681 ucs2buf, 3);
1682 if (ucs2len > 0)
1683 break;
1684 }
1685 if (ucs2len == 0)
1686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 // If we have only one byte then it's probably an
1688 // incomplete byte sequence. Otherwise discard
1689 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001690 if (size == 1)
1691 break;
1692 found_bad = TRUE;
1693 bytelen = 1;
1694 }
1695 }
1696
1697 if (!found_bad)
1698 {
1699 int i;
1700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001701 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001702 if (enc_utf8)
1703 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001704 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001705 for (i = 0; i < ucs2len; ++i)
1706 dst += utf_char2bytes(ucs2buf[i], dst);
1707 }
1708 else
1709 {
1710 BOOL bad = FALSE;
1711 int dstlen;
1712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001713 // From UCS-2 to "enc_codepage". If the
1714 // conversion uses the default character "?",
1715 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001716 dstlen = WideCharToMultiByte(enc_codepage, 0,
1717 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001718 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001719 replstr, &bad);
1720 if (bad)
1721 found_bad = TRUE;
1722 else
1723 dst += dstlen;
1724 }
1725 }
1726
1727 if (found_bad)
1728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001730 if (can_retry)
1731 goto rewind_retry;
1732 if (conv_error == 0)
1733 conv_error = readfile_linenr(linecnt, ptr, dst);
1734 if (bad_char_behavior != BAD_DROP)
1735 {
1736 if (bad_char_behavior == BAD_KEEP)
1737 {
1738 mch_memmove(dst, src, bytelen);
1739 dst += bytelen;
1740 }
1741 else
1742 *dst++ = bad_char_behavior;
1743 }
1744 }
1745
1746 src += bytelen;
1747 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001749
1750 if (size > 0)
1751 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001752 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001753 mch_memmove(conv_rest, src, size);
1754 conv_restlen = size;
1755 }
1756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001758 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001761#endif
1762#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (fio_flags & FIO_MACROMAN)
1764 {
1765 /*
1766 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001767 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001769 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
1772 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (fio_flags != 0)
1775 {
1776 int u8c;
1777 char_u *dest;
1778 char_u *tail = NULL;
1779
1780 /*
1781 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1782 * "enc_utf8" not set: Convert Unicode to Latin1.
1783 * Go from end to start through the buffer, because the number
1784 * of bytes may increase.
1785 * "dest" points to after where the UTF-8 bytes go, "p" points
1786 * to after the next character to convert.
1787 */
1788 dest = ptr + real_size;
1789 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1790 {
1791 p = ptr + size;
1792 if (fio_flags == FIO_UTF8)
1793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001794 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 tail = ptr + size - 1;
1796 while (tail > ptr && (*tail & 0xc0) == 0x80)
1797 --tail;
1798 if (tail + utf_byte2len(*tail) <= ptr + size)
1799 tail = NULL;
1800 else
1801 p = tail;
1802 }
1803 }
1804 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 p = ptr + (size & ~1);
1808 if (size & 1)
1809 tail = p;
1810 if ((fio_flags & FIO_UTF16) && p > ptr)
1811 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001812 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (fio_flags & FIO_ENDIAN_L)
1814 {
1815 u8c = (*--p << 8);
1816 u8c += *--p;
1817 }
1818 else
1819 {
1820 u8c = *--p;
1821 u8c += (*--p << 8);
1822 }
1823 if (u8c >= 0xd800 && u8c <= 0xdbff)
1824 tail = p;
1825 else
1826 p += 2;
1827 }
1828 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001829 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001831 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 p = ptr + (size & ~3);
1833 if (size & 3)
1834 tail = p;
1835 }
1836
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001837 // If there is a trailing incomplete sequence move it to
1838 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (tail != NULL)
1840 {
1841 conv_restlen = (int)((ptr + size) - tail);
1842 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1843 size -= conv_restlen;
1844 }
1845
1846
1847 while (p > ptr)
1848 {
1849 if (fio_flags & FIO_LATIN1)
1850 u8c = *--p;
1851 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1852 {
1853 if (fio_flags & FIO_ENDIAN_L)
1854 {
1855 u8c = (*--p << 8);
1856 u8c += *--p;
1857 }
1858 else
1859 {
1860 u8c = *--p;
1861 u8c += (*--p << 8);
1862 }
1863 if ((fio_flags & FIO_UTF16)
1864 && u8c >= 0xdc00 && u8c <= 0xdfff)
1865 {
1866 int u16c;
1867
1868 if (p == ptr)
1869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001870 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (can_retry)
1872 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001873 if (conv_error == 0)
1874 conv_error = readfile_linenr(linecnt,
1875 ptr, p);
1876 if (bad_char_behavior == BAD_DROP)
1877 continue;
1878 if (bad_char_behavior != BAD_KEEP)
1879 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
1881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001882 // found second word of double-word, get the first
1883 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (fio_flags & FIO_ENDIAN_L)
1885 {
1886 u16c = (*--p << 8);
1887 u16c += *--p;
1888 }
1889 else
1890 {
1891 u16c = *--p;
1892 u16c += (*--p << 8);
1893 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001894 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1895 + (u8c & 0x3ff);
1896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001897 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (u16c < 0xd800 || u16c > 0xdbff)
1899 {
1900 if (can_retry)
1901 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001902 if (conv_error == 0)
1903 conv_error = readfile_linenr(linecnt,
1904 ptr, p);
1905 if (bad_char_behavior == BAD_DROP)
1906 continue;
1907 if (bad_char_behavior != BAD_KEEP)
1908 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911 }
1912 else if (fio_flags & FIO_UCS4)
1913 {
1914 if (fio_flags & FIO_ENDIAN_L)
1915 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001916 u8c = (unsigned)*--p << 24;
1917 u8c += (unsigned)*--p << 16;
1918 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 u8c += *--p;
1920 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001921 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001924 u8c += (unsigned)*--p << 8;
1925 u8c += (unsigned)*--p << 16;
1926 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001929 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 if (*--p < 0x80)
1932 u8c = *p;
1933 else
1934 {
1935 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001936 p -= len;
1937 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (len == 0)
1939 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // Not a valid UTF-8 character, retry with
1941 // another fenc when possible, otherwise just
1942 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (can_retry)
1944 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001945 if (conv_error == 0)
1946 conv_error = readfile_linenr(linecnt,
1947 ptr, p);
1948 if (bad_char_behavior == BAD_DROP)
1949 continue;
1950 if (bad_char_behavior != BAD_KEEP)
1951 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001955 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 dest -= utf_char2len(u8c);
1958 (void)utf_char2bytes(u8c, dest);
1959 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001960 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 --dest;
1963 if (u8c >= 0x100)
1964 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 // character doesn't fit in latin1, retry with
1966 // another fenc when possible, otherwise just
1967 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001968 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001970 if (conv_error == 0)
1971 conv_error = readfile_linenr(linecnt, ptr, p);
1972 if (bad_char_behavior == BAD_DROP)
1973 ++dest;
1974 else if (bad_char_behavior == BAD_KEEP)
1975 *dest = u8c;
1976 else if (eap != NULL && eap->bad_char != 0)
1977 *dest = bad_char_behavior;
1978 else
1979 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 }
1981 else
1982 *dest = u8c;
1983 }
1984 }
1985
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001986 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 line_start = dest - linerest;
1988 mch_memmove(line_start, buffer, (size_t)linerest);
1989 size = (long)((ptr + real_size) - dest);
1990 ptr = dest;
1991 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001992 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001994 int incomplete_tail = FALSE;
1995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001996 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001997 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001999 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002000 int l;
2001
2002 if (todo <= 0)
2003 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (*p >= 0x80)
2005 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002006 // A length of 1 means it's an illegal byte. Accept
2007 // an incomplete character at the end though, the next
2008 // read() will get the next bytes, we'll check it
2009 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002010 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002011 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 // Avoid retrying with a different encoding when
2014 // a truncated file is more likely, or attempting
2015 // to read the rest of an incomplete sequence when
2016 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002017 if (p > ptr || filesize > 0)
2018 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 // Incomplete byte sequence, move it to conv_rest[]
2020 // and try to read the rest of it, unless we've
2021 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002022 if (p > ptr)
2023 {
2024 conv_restlen = todo;
2025 mch_memmove(conv_rest, p, conv_restlen);
2026 size -= conv_restlen;
2027 break;
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002030 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 // Illegal byte. If we can try another encoding
2033 // do that, unless at EOF where a truncated
2034 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002035 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002036 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002037#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002039 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2040 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002041#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002043 if (conv_error == 0 && illegal_byte == 0)
2044 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002046 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002047 if (bad_char_behavior == BAD_DROP)
2048 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002049 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002050 --p;
2051 --size;
2052 }
2053 else if (bad_char_behavior != BAD_KEEP)
2054 *p = bad_char_behavior;
2055 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002056 else
2057 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002060 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002062 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002065#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002066 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002067 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002068 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002070#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002072 advance_fenc = TRUE;
2073 file_rewind = TRUE;
2074 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 filesize += size;
2080
2081 /*
2082 * when reading the first part of a file: guess EOL type
2083 */
2084 if (fileformat == EOL_UNKNOWN)
2085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (try_dos || try_unix)
2088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002090 if (try_mac)
2091 try_mac = 1;
2092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 for (p = ptr; p < ptr + size; ++p)
2094 {
2095 if (*p == NL)
2096 {
2097 if (!try_unix
2098 || (try_dos && p > ptr && p[-1] == CAR))
2099 fileformat = EOL_DOS;
2100 else
2101 fileformat = EOL_UNIX;
2102 break;
2103 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002104 else if (*p == CAR && try_mac)
2105 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (fileformat == EOL_UNIX && try_mac)
2110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002111 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 try_mac = 1;
2113 try_unix = 1;
2114 for (; p >= ptr && *p != CAR; p--)
2115 ;
2116 if (p >= ptr)
2117 {
2118 for (p = ptr; p < ptr + size; ++p)
2119 {
2120 if (*p == NL)
2121 try_unix++;
2122 else if (*p == CAR)
2123 try_mac++;
2124 }
2125 if (try_mac > try_unix)
2126 fileformat = EOL_MAC;
2127 }
2128 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002129 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002130 // Looking for CR but found no end-of-line markers at
2131 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002132 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (fileformat == EOL_UNKNOWN && try_mac)
2137 fileformat = EOL_MAC;
2138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002139 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (fileformat == EOL_UNKNOWN)
2141 fileformat = default_fileformat();
2142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002143 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 set_fileformat(fileformat, OPT_LOCAL);
2146 }
2147 }
2148
2149 /*
2150 * This loop is executed once for every character read.
2151 * Keep it fast!
2152 */
2153 if (fileformat == EOL_MAC)
2154 {
2155 --ptr;
2156 while (++ptr, --size >= 0)
2157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002158 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if ((c = *ptr) != NUL && c != CAR && c != NL)
2160 continue;
2161 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002164 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else
2166 {
2167 if (skip_count == 0)
2168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 len = (colnr_T) (ptr - line_start + 1);
2171 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2172 {
2173 error = TRUE;
2174 break;
2175 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002176#ifdef FEAT_PERSISTENT_UNDO
2177 if (read_undo_file)
2178 sha256_update(&sha_ctx, line_start, len);
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 ++lnum;
2181 if (--read_count == 0)
2182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002183 error = TRUE; // break loop
2184 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 break;
2186 }
2187 }
2188 else
2189 --skip_count;
2190 line_start = ptr + 1;
2191 }
2192 }
2193 }
2194 else
2195 {
2196 --ptr;
2197 while (++ptr, --size >= 0)
2198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002199 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 continue;
2201 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else
2204 {
2205 if (skip_count == 0)
2206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002207 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 len = (colnr_T)(ptr - line_start + 1);
2209 if (fileformat == EOL_DOS)
2210 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002211 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002213 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 ptr[-1] = NUL;
2215 --len;
2216 }
2217 /*
2218 * Reading in Dos format, but no CR-LF found!
2219 * When 'fileformats' includes "unix", delete all
2220 * the lines read so far and start all over again.
2221 * Otherwise give an error message later.
2222 */
2223 else if (ff_error != EOL_DOS)
2224 {
2225 if ( try_unix
2226 && !read_stdin
2227 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002228 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2229 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 set_fileformat(EOL_UNIX, OPT_LOCAL);
2234 file_rewind = TRUE;
2235 keep_fileformat = TRUE;
2236 goto retry;
2237 }
2238 ff_error = EOL_DOS;
2239 }
2240 }
2241 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2242 {
2243 error = TRUE;
2244 break;
2245 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002246#ifdef FEAT_PERSISTENT_UNDO
2247 if (read_undo_file)
2248 sha256_update(&sha_ctx, line_start, len);
2249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 ++lnum;
2251 if (--read_count == 0)
2252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 error = TRUE; // break loop
2254 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 break;
2256 }
2257 }
2258 else
2259 --skip_count;
2260 line_start = ptr + 1;
2261 }
2262 }
2263 }
2264 linerest = (long)(ptr - line_start);
2265 ui_breakcheck();
2266 }
2267
2268failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (error && read_count == 0)
2271 error = FALSE;
2272
2273 /*
2274 * If we get EOF in the middle of a line, note the fact and
2275 * complete the line ourselves.
2276 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2277 */
2278 if (!error
2279 && !got_int
2280 && linerest != 0
2281 && !(!curbuf->b_p_bin
Bram Moolenaarfb0cf232022-10-22 11:25:19 +01002282 && fileformat == EOL_DOS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285 if (set_options)
Bram Moolenaarfb0cf232022-10-22 11:25:19 +01002286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 curbuf->b_p_eol = FALSE;
Bram Moolenaarfb0cf232022-10-22 11:25:19 +01002288 if (*line_start == Ctrl_Z && ptr == line_start + 1)
2289 curbuf->b_p_eof = FALSE;
2290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002292 len = (colnr_T)(ptr - line_start + 1);
2293 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 error = TRUE;
2295 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002296 {
2297#ifdef FEAT_PERSISTENT_UNDO
2298 if (read_undo_file)
2299 sha256_update(&sha_ctx, line_start, len);
2300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002305 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002306 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002309 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002310 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002311 crypt_free_state(curbuf->b_cryptstate);
2312 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002313 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002314 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2315 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // Don't set cryptkey to NULL, it's used below as a flag that
2317 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // If editing a new file: set 'fenc' for the current buffer.
2321 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002322 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002324 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (fenc_alloced)
2326 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002327#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331
2332 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002334#ifdef HAVE_FD_CLOEXEC
2335 else
2336 {
2337 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002338
Bram Moolenaarf05da212009-11-17 16:13:15 +00002339 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002340 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002341 }
2342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 vim_free(buffer);
2344
2345#ifdef HAVE_DUP
2346 if (read_stdin)
2347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002348 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002350 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352#endif
2353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (tmpname != NULL)
2355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 vim_free(tmpname);
2358 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 /*
2362 * In recovery mode everything but autocommands is skipped.
2363 */
2364 if (!recoverymode)
2365 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2368 {
2369#ifdef FEAT_NETBEANS_INTG
2370 netbeansFireChanges = 0;
2371#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002372 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#ifdef FEAT_NETBEANS_INTG
2374 netbeansFireChanges = 1;
2375#endif
2376 --linecnt;
2377 }
2378 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2379 if (filesize == 0)
2380 linecnt = 0;
2381 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002382 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002383 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002384#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002385 // After reading the text into the buffer the diff info needs to
2386 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002387 diff_invalidate(curbuf);
2388#endif
2389#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002390 // All folds in the window are invalid now. Mark them for update
2391 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002392 foldUpdateAll(curwin);
2393#endif
2394 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002395 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 appended_lines_mark(from, linecnt);
2397
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#ifndef ALWAYS_USE_GUI
2399 /*
2400 * If we were reading from the same terminal as where messages go,
2401 * the screen will have been messed up.
2402 * Switch on raw mode now and clear the screen.
2403 */
2404 if (read_stdin)
2405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 starttermcap();
2408 screenclear();
2409 }
2410#endif
2411
2412 if (got_int)
2413 {
2414 if (!(flags & READ_DUMMY))
2415 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002416 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002418 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 msg_scroll = msg_save;
2421#ifdef FEAT_VIMINFO
2422 check_marks_read();
2423#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002424 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426
2427 if (!filtering && !(flags & READ_DUMMY))
2428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002429 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 c = FALSE;
2431
2432#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002433 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 STRCAT(IObuff, _("[fifo]"));
2436 c = TRUE;
2437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002438 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 STRCAT(IObuff, _("[socket]"));
2441 c = TRUE;
2442 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002443# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002444 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002445 {
2446 STRCAT(IObuff, _("[character special]"));
2447 c = TRUE;
2448 }
2449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450#endif
2451 if (curbuf->b_p_ro)
2452 {
2453 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2454 c = TRUE;
2455 }
2456 if (read_no_eol_lnum)
2457 {
2458 msg_add_eol();
2459 c = TRUE;
2460 }
2461 if (ff_error == EOL_DOS)
2462 {
2463 STRCAT(IObuff, _("[CR missing]"));
2464 c = TRUE;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (split)
2467 {
2468 STRCAT(IObuff, _("[long lines split]"));
2469 c = TRUE;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (notconverted)
2472 {
2473 STRCAT(IObuff, _("[NOT converted]"));
2474 c = TRUE;
2475 }
2476 else if (converted)
2477 {
2478 STRCAT(IObuff, _("[converted]"));
2479 c = TRUE;
2480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481#ifdef FEAT_CRYPT
2482 if (cryptkey != NULL)
2483 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002484 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 c = TRUE;
2486 }
2487#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002488 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002490 sprintf((char *)IObuff + STRLEN(IObuff),
2491 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 c = TRUE;
2493 }
2494 else if (illegal_byte > 0)
2495 {
2496 sprintf((char *)IObuff + STRLEN(IObuff),
2497 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2498 c = TRUE;
2499 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002500 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
2502 STRCAT(IObuff, _("[READ ERRORS]"));
2503 c = TRUE;
2504 }
2505 if (msg_add_fileformat(fileformat))
2506 c = TRUE;
2507#ifdef FEAT_CRYPT
2508 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002509 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002510 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 else
2512#endif
2513 msg_add_lines(c, (long)linecnt, filesize);
2514
Bram Moolenaard23a8232018-02-10 18:45:26 +01002515 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 msg_scrolled_ign = TRUE;
2517#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002518 // Don't show the message when reading stdin, it would end up in a
2519 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (read_stdin || read_buffer)
2521 p = msg_may_trunc(FALSE, IObuff);
2522 else
2523#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002524 {
2525 if (msg_col > 0)
2526 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002527 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002530 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002531 // Need to repeat the message after redrawing when:
2532 // - When reading from stdin (the screen will be cleared next).
2533 // - When restart_edit is set (otherwise there will be a delay
2534 // before redrawing).
2535 // - When the screen was scrolled but there is no wait-return
2536 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002537 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 msg_scrolled_ign = FALSE;
2539 }
2540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002541 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002543 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002544 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 curbuf->b_p_ro = TRUE;
2546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002547 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 /*
2550 * In Ex mode: cursor at last new line.
2551 * Otherwise: cursor at first new line.
2552 */
2553 if (exmode_active)
2554 curwin->w_cursor.lnum = from + linecnt;
2555 else
2556 curwin->w_cursor.lnum = from + 1;
2557 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002558 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaare1004402020-10-24 20:49:43 +02002560 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002561 {
2562 // Set '[ and '] marks to the newly read lines.
2563 curbuf->b_op_start.lnum = from + 1;
2564 curbuf->b_op_start.col = 0;
2565 curbuf->b_op_end.lnum = from + linecnt;
2566 curbuf->b_op_end.col = 0;
2567 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002568
Bram Moolenaar4f974752019-02-17 17:44:42 +01002569#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002570 /*
2571 * Work around a weird problem: When a file has two links (only
2572 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002573 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002574 * It's correct again after reading the file, thus reset the timestamp
2575 * here.
2576 */
2577 if (newfile && !read_stdin && !read_buffer
2578 && mch_stat((char *)fname, &st) >= 0)
2579 {
2580 buf_store_time(curbuf, &st, fname);
2581 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002582 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002583 }
2584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586 msg_scroll = msg_save;
2587
2588#ifdef FEAT_VIMINFO
2589 /*
2590 * Get the marks before executing autocommands, so they can be used there.
2591 */
2592 check_marks_read();
2593#endif
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002596 * We remember if the last line of the read didn't have
2597 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2598 * or writing the read again with 'binary' on. The latter is required
2599 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002601 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 // When reloading a buffer put the cursor at the first line that is
2604 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002605 if (flags & READ_KEEP_UNDO)
2606 u_find_first_changed();
2607
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002608#ifdef FEAT_PERSISTENT_UNDO
2609 /*
2610 * When opening a new file locate undo info and read it.
2611 */
2612 if (read_undo_file)
2613 {
2614 char_u hash[UNDO_HASH_SIZE];
2615
2616 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002617 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002618 }
2619#endif
2620
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002621 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 int m = msg_scroll;
2624 int n = msg_scrolled;
2625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002626 // Save the fileformat now, otherwise the buffer will be considered
2627 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002628 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 save_file_ff(curbuf);
2630
2631 /*
2632 * The output from the autocommands should not overwrite anything and
2633 * should not be overwritten: Set msg_scroll, restore its value if no
2634 * output was done.
2635 */
2636 msg_scroll = TRUE;
2637 if (filtering)
2638 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2639 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002640 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2643 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002644 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2645 /*
2646 * EVENT_FILETYPE was not triggered but the buffer already has a
2647 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2648 */
2649 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2650 TRUE, curbuf);
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 else
2653 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2654 FALSE, NULL, eap);
2655 if (msg_scrolled == n)
2656 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002657# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002658 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 if (recoverymode && error)
2664 return FAIL;
2665 return OK;
2666}
2667
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002668#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002669/*
2670 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2671 * which is the name of files used for process substitution output by
2672 * some shells on some operating systems, e.g., bash on SunOS.
2673 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2674 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002675 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002676is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002677{
2678 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2679 && VIM_ISDIGIT(fname[8])
2680 && *skipdigits(fname + 9) == NUL
2681 && (fname[9] != NUL
2682 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2683}
2684#endif
2685
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002686/*
2687 * From the current line count and characters read after that, estimate the
2688 * line number where we are now.
2689 * Used for error messages that include a line number.
2690 */
2691 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002692readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002693 linenr_T linecnt, // line count before reading more bytes
2694 char_u *p, // start of more bytes read
2695 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002696{
2697 char_u *s;
2698 linenr_T lnum;
2699
2700 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2701 for (s = p; s < endp; ++s)
2702 if (*s == '\n')
2703 ++lnum;
2704 return lnum;
2705}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707/*
Rob Pilling8196e942022-02-11 15:12:10 +00002708 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002709 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 * Returns OK or FAIL.
2711 */
2712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002713prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
Bram Moolenaar13505972019-01-24 15:04:48 +01002715 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (eap->cmd == NULL)
2717 return FAIL;
2718
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002719 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2720 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002721 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002722 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002723
2724 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002725 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002726 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return OK;
2728}
2729
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002730/*
2731 * Set default or forced 'fileformat' and 'binary'.
2732 */
2733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002734set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002735{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002736 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002737 if (set_options)
2738 {
2739 if (eap != NULL && eap->force_ff != 0)
2740 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2741 else if (*p_ffs != NUL)
2742 set_fileformat(default_fileformat(), OPT_LOCAL);
2743 }
2744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002745 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002746 if (eap != NULL && eap->force_bin != 0)
2747 {
2748 int oldval = curbuf->b_p_bin;
2749
2750 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2751 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2752 }
2753}
2754
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002755/*
2756 * Set forced 'fileencoding'.
2757 */
2758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002759set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002760{
2761 if (eap->force_enc != 0)
2762 {
2763 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2764
2765 if (fenc != NULL)
2766 set_string_option_direct((char_u *)"fenc", -1,
2767 fenc, OPT_FREE|OPT_LOCAL, 0);
2768 vim_free(fenc);
2769 }
2770}
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772/*
2773 * Find next fileencoding to use from 'fileencodings'.
2774 * "pp" points to fenc_next. It's advanced to the next item.
2775 * When there are no more items, an empty string is returned and *pp is set to
2776 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002777 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2778 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 */
2780 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002781next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 char_u *p;
2784 char_u *r;
2785
Bram Moolenaarf077db22019-08-13 00:18:24 +02002786 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (**pp == NUL)
2788 {
2789 *pp = NULL;
2790 return (char_u *)"";
2791 }
2792 p = vim_strchr(*pp, ',');
2793 if (p == NULL)
2794 {
2795 r = enc_canonize(*pp);
2796 *pp += STRLEN(*pp);
2797 }
2798 else
2799 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002800 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 *pp = p + 1;
2802 if (r != NULL)
2803 {
2804 p = enc_canonize(r);
2805 vim_free(r);
2806 r = p;
2807 }
2808 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002809 if (r != NULL)
2810 *alloced = TRUE;
2811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002813 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 r = (char_u *)"";
2815 *pp = NULL;
2816 }
2817 return r;
2818}
2819
Bram Moolenaar13505972019-01-24 15:04:48 +01002820#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821/*
2822 * Convert a file with the 'charconvert' expression.
2823 * This closes the file which is to be read, converts it and opens the
2824 * resulting file for reading.
2825 * Returns name of the resulting converted file (the caller should delete it
2826 * after reading it).
2827 * Returns NULL if the conversion failed ("*fdp" is not set) .
2828 */
2829 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002830readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002831 char_u *fname, // name of input file
2832 char_u *fenc, // converted from
2833 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002836 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002838 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002840 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else
2842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002843 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *fdp = -1;
2845 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2846 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002847 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2849 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002850 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852
2853 if (errmsg != NULL)
2854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002855 // Don't use emsg(), it breaks mappings, the retry with
2856 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002857 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (tmpname != NULL)
2859 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002860 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002861 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 }
2864
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002865 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (*fdp < 0)
2867 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2868
2869 return tmpname;
2870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871#endif
2872
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002873#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002875 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2877 * *filesizep are updated.
2878 * Return the (new) encryption key, NULL for no encryption.
2879 */
2880 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002881check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002882 char_u *cryptkey, // previous encryption key or NULL
2883 char_u *ptr, // pointer to read bytes
2884 long *sizep, // length of read bytes
2885 off_T *filesizep, // nr of bytes used from file
2886 int newfile, // editing a new buffer
2887 char_u *fname, // file name to display
2888 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002890 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002891 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002892
2893 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002895 // Mark the buffer as read-only until the decryption has taken place.
2896 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002897 curbuf->b_p_ro = TRUE;
2898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002899 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002900 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002901 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 if (*curbuf->b_p_key)
2904 cryptkey = curbuf->b_p_key;
2905 else
2906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002907 // When newfile is TRUE, store the typed key in the 'key'
2908 // option and don't free it. bf needs hash of the key saved.
2909 // Don't ask for the key again when first time Enter was hit.
2910 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002911 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002912 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002913 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002914 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002915 *did_ask = TRUE;
2916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002917 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (cryptkey != NULL && *cryptkey == NUL)
2919 {
2920 if (cryptkey != curbuf->b_p_key)
2921 vim_free(cryptkey);
2922 cryptkey = NULL;
2923 }
2924 }
2925 }
2926
2927 if (cryptkey != NULL)
2928 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002929 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002930
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002931 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002932 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002933 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002934 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002935
2936 curbuf->b_cryptstate = crypt_create_from_header(
2937 method, cryptkey, ptr);
2938 crypt_set_cm_option(curbuf, method);
2939
2940 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002941 *filesizep += header_len;
2942 *sizep -= header_len;
2943 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002946 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002949 // When starting to edit a new file which does not have encryption, clear
2950 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002951 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01002952 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 return cryptkey;
2955}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002956#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002959 * Return TRUE if a file appears to be read-only from the file permissions.
2960 */
2961 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002962check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 char_u *fname, // full path to file
2964 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002965{
2966#ifndef USE_MCH_ACCESS
2967 int fd = 0;
2968#endif
2969
2970 return (
2971#ifdef USE_MCH_ACCESS
2972# ifdef UNIX
2973 (perm & 0222) == 0 ||
2974# endif
2975 mch_access((char *)fname, W_OK)
2976#else
2977 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2978 ? TRUE : (close(fd), FALSE)
2979#endif
2980 );
2981}
2982
Bram Moolenaara7870192019-02-14 12:56:36 +01002983#if defined(HAVE_FSYNC) || defined(PROTO)
2984/*
2985 * Call fsync() with Mac-specific exception.
2986 * Return fsync() result: zero for success.
2987 */
2988 int
2989vim_fsync(int fd)
2990{
2991 int r;
2992
2993# ifdef MACOS_X
2994 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002995 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002996# endif
2997 r = fsync(fd);
2998 return r;
2999}
3000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003003 * Set the name of the current buffer. Use when the buffer doesn't have a
3004 * name and a ":r" or ":w" command with a file name is used.
3005 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003006 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003007set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003008{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003009 buf_T *buf = curbuf;
3010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003011 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003012 if (curbuf->b_p_bl)
3013 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3014 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003015#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003016 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003017 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003018#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003019 if (curbuf != buf)
3020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003021 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003022 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003023 return FAIL;
3024 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003025
3026 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3027 curbuf->b_flags |= BF_NOTEDITED;
3028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003029 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003030 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3031 if (curbuf->b_p_bl)
3032 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003033#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003034 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003035 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003036#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003038 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003039 if (*curbuf->b_p_ft == NUL)
3040 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003041 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003042 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003043 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003044 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003045
3046 return OK;
3047}
3048
3049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 * Put file name into IObuff with quotes.
3051 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 if (fname == NULL)
3056 fname = (char_u *)"-stdin-";
3057 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3058 IObuff[0] = '"';
3059 STRCAT(IObuff, "\" ");
3060}
3061
3062/*
3063 * Append message for text mode to IObuff.
3064 * Return TRUE if something appended.
3065 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003067msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069#ifndef USE_CRNL
3070 if (eol_type == EOL_DOS)
3071 {
3072 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3073 return TRUE;
3074 }
3075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (eol_type == EOL_MAC)
3077 {
3078 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3079 return TRUE;
3080 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003081#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (eol_type == EOL_UNIX)
3083 {
3084 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3085 return TRUE;
3086 }
3087#endif
3088 return FALSE;
3089}
3090
3091/*
3092 * Append line and character count to IObuff.
3093 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003095msg_add_lines(
3096 int insert_space,
3097 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003098 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 char_u *p;
3101
3102 p = IObuff + STRLEN(IObuff);
3103
3104 if (insert_space)
3105 *p++ = ' ';
3106 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003107 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003108 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 else
3110 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003111 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003113 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003114 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003115 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117}
3118
3119/*
3120 * Append message for missing line separator to IObuff.
3121 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003122 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003123msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3126}
3127
Bram Moolenaar473952e2019-09-28 16:30:04 +02003128 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003129time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
ichizokdef69df2021-10-15 17:23:12 +01003131 return
3132#ifdef ST_MTIM_NSEC
3133 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#endif
ichizokdef69df2021-10-15 17:23:12 +01003135#if defined(__linux__) || defined(MSWIN)
3136 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3137 // the seconds. Since the roundoff is done when flushing the inode, the
3138 // time may change unexpectedly by one second!!!
3139 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3140#else
3141 (long)st->st_mtime != mtime
3142#endif
3143 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144}
3145
3146/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003147 * Return TRUE if file encoding "fenc" requires conversion from or to
3148 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003150 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003153 int same_encoding;
3154 int enc_flags;
3155 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003157 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003158 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003159 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003160 fenc_flags = 0;
3161 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003162 else
3163 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003164 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3165 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003166 enc_flags = get_fio_flags(p_enc);
3167 fenc_flags = get_fio_flags(fenc);
3168 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3169 }
3170 if (same_encoding)
3171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003172 // Specified encoding matches with 'encoding'. This requires
3173 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003174 return enc_unicode != 0;
3175 }
3176
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003177 // Encodings differ. However, conversion is not needed when 'enc' is any
3178 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003179 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180}
3181
3182/*
3183 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3184 * internal conversion.
3185 * if "ptr" is an empty string, use 'encoding'.
3186 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003187 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003188get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 int prop;
3191
3192 if (*ptr == NUL)
3193 ptr = p_enc;
3194
3195 prop = enc_canon_props(ptr);
3196 if (prop & ENC_UNICODE)
3197 {
3198 if (prop & ENC_2BYTE)
3199 {
3200 if (prop & ENC_ENDIAN_L)
3201 return FIO_UCS2 | FIO_ENDIAN_L;
3202 return FIO_UCS2;
3203 }
3204 if (prop & ENC_4BYTE)
3205 {
3206 if (prop & ENC_ENDIAN_L)
3207 return FIO_UCS4 | FIO_ENDIAN_L;
3208 return FIO_UCS4;
3209 }
3210 if (prop & ENC_2WORD)
3211 {
3212 if (prop & ENC_ENDIAN_L)
3213 return FIO_UTF16 | FIO_ENDIAN_L;
3214 return FIO_UTF16;
3215 }
3216 return FIO_UTF8;
3217 }
3218 if (prop & ENC_LATIN1)
3219 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003220 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 return 0;
3222}
3223
Bram Moolenaar473952e2019-09-28 16:30:04 +02003224#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225/*
3226 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3227 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3228 * Used for conversion between 'encoding' and 'fileencoding'.
3229 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003230 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003231get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233 int cp;
3234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003235 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (!enc_utf8 && enc_codepage <= 0)
3237 return 0;
3238
3239 cp = encname2codepage(ptr);
3240 if (cp == 0)
3241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003242# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (STRCMP(ptr, "utf-8") == 0)
3244 cp = CP_UTF8;
3245 else
3246# endif
3247 return 0;
3248 }
3249 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3250}
3251#endif
3252
Bram Moolenaar473952e2019-09-28 16:30:04 +02003253#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254/*
3255 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3256 * needed for the internal conversion to/from utf-8 or latin1.
3257 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003258 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003259get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3262 && (enc_canon_props(ptr) & ENC_MACROMAN))
3263 return FIO_MACROMAN;
3264 return 0;
3265}
3266#endif
3267
3268/*
3269 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3270 * "size" must be at least 2.
3271 * Return the name of the encoding and set "*lenp" to the length.
3272 * Returns NULL when no BOM found.
3273 */
3274 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003275check_for_bom(
3276 char_u *p,
3277 long size,
3278 int *lenp,
3279 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 char *name = NULL;
3282 int len = 2;
3283
3284 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003285 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003287 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 len = 3;
3289 }
3290 else if (p[0] == 0xff && p[1] == 0xfe)
3291 {
3292 if (size >= 4 && p[2] == 0 && p[3] == 0
3293 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003295 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 len = 4;
3297 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003298 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003299 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003300 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003301 // utf-16le is preferred, it also works for ucs-2le text
3302 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304 else if (p[0] == 0xfe && p[1] == 0xff
3305 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003307 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003308 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003309 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003310 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003311 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3314 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003316 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 len = 4;
3318 }
3319
3320 *lenp = len;
3321 return (char_u *)name;
3322}
3323
3324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 * Try to find a shortname by comparing the fullname with the current
3326 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003327 * Returns "full_path" or pointer into "full_path" if shortened.
3328 */
3329 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003330shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003331{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003332 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003333 char_u *p = full_path;
3334
Bram Moolenaard9462e32011-04-11 21:35:11 +02003335 dirname = alloc(MAXPATHL);
3336 if (dirname == NULL)
3337 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003338 if (mch_dirname(dirname, MAXPATHL) == OK)
3339 {
3340 p = shorten_fname(full_path, dirname);
3341 if (p == NULL || *p == NUL)
3342 p = full_path;
3343 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003344 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003345 return p;
3346}
3347
3348/*
3349 * Try to find a shortname by comparing the fullname with the current
3350 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 * Returns NULL if not shorter name possible, pointer into "full_path"
3352 * otherwise.
3353 */
3354 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003355shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 int len;
3358 char_u *p;
3359
3360 if (full_path == NULL)
3361 return NULL;
3362 len = (int)STRLEN(dir_name);
3363 if (fnamencmp(dir_name, full_path, len) == 0)
3364 {
3365 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003366#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003368 * MS-Windows: when a file is in the root directory, dir_name will end
3369 * in a slash, since C: by itself does not define a specific dir. In
3370 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 */
3372 if (!((len > 2) && (*(p - 2) == ':')))
3373#endif
3374 {
3375 if (vim_ispathsep(*p))
3376 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003377#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else
3379 p = NULL;
3380#endif
3381 }
3382 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003383#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /*
3385 * When using a file in the current drive, remove the drive name:
3386 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3387 * a floppy from "A:\dir" to "B:\dir".
3388 */
3389 else if (len > 3
3390 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3391 && full_path[1] == ':'
3392 && vim_ispathsep(full_path[2]))
3393 p = full_path + 2;
3394#endif
3395 else
3396 p = NULL;
3397 return p;
3398}
3399
3400/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003401 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * When "force" is TRUE: Use full path from now on for files currently being
3403 * edited, both for file name and swap file name. Try to shorten the file
3404 * names a bit, if safe to do so.
3405 * When "force" is FALSE: Only try to shorten absolute file names.
3406 * For buffers that have buftype "nofile" or "scratch": never change the file
3407 * name.
3408 */
3409 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003410shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3411{
3412 char_u *p;
3413
3414 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003415 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003416 && !path_with_url(buf->b_fname)
3417 && (force
3418 || buf->b_sfname == NULL
3419 || mch_isFullName(buf->b_sfname)))
3420 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003421 if (buf->b_sfname != buf->b_ffname)
3422 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003423 p = shorten_fname(buf->b_ffname, dirname);
3424 if (p != NULL)
3425 {
3426 buf->b_sfname = vim_strsave(p);
3427 buf->b_fname = buf->b_sfname;
3428 }
3429 if (p == NULL || buf->b_fname == NULL)
3430 buf->b_fname = buf->b_ffname;
3431 }
3432}
3433
3434/*
3435 * Shorten filenames for all buffers.
3436 */
3437 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003438shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 char_u dirname[MAXPATHL];
3441 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003444 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003446 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003448 // Always make the swap file name a full path, a "nofile" buffer may
3449 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003450 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003453 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003454#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003455 popup_update_preview_title();
3456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457}
3458
3459#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3460 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003461 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 || defined(PROTO)
3463/*
3464 * Shorten all filenames in "fnames[count]" by current directory.
3465 */
3466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003467shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
3469 int i;
3470 char_u dirname[MAXPATHL];
3471 char_u *p;
3472
3473 if (fnames == NULL || count < 1)
3474 return;
3475 mch_dirname(dirname, sizeof(dirname));
3476 for (i = 0; i < count; ++i)
3477 {
3478 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3479 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003480 // shorten_fname() returns pointer in given "fnames[i]". If free
3481 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3482 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 p = vim_strsave(p);
3484 vim_free(fnames[i]);
3485 fnames[i] = p;
3486 }
3487 }
3488}
3489#endif
3490
3491/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003492 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 * fo_o_h.ext for MSDOS or when shortname option set.
3494 *
3495 * Assumed that fname is a valid name found in the filesystem we assure that
3496 * the return value is a different name and ends in 'ext'.
3497 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3498 * characters otherwise.
3499 * Space for the returned name is allocated, must be freed later.
3500 * Returns NULL when out of memory.
3501 */
3502 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003503modname(
3504 char_u *fname,
3505 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003506 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003508 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 fname, ext, prepend_dot);
3510}
3511
3512 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003514 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003515 char_u *fname,
3516 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003517 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 char_u *retval;
3520 char_u *s;
3521 char_u *e;
3522 char_u *ptr;
3523 int fnamelen, extlen;
3524
3525 extlen = (int)STRLEN(ext);
3526
3527 /*
3528 * If there is no file name we must get the name of the current directory
3529 * (we need the full path in case :cd is used).
3530 */
3531 if (fname == NULL || *fname == NUL)
3532 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003533 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (retval == NULL)
3535 return NULL;
3536 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3537 (fnamelen = (int)STRLEN(retval)) == 0)
3538 {
3539 vim_free(retval);
3540 return NULL;
3541 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003542 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 retval[fnamelen++] = PATHSEP;
3545 retval[fnamelen] = NUL;
3546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003547 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549 else
3550 {
3551 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003552 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (retval == NULL)
3554 return NULL;
3555 STRCPY(retval, fname);
3556#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003557 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#endif
3559 }
3560
3561 /*
3562 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3563 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3564 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3565 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3566 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003567 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003569 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003570 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003573 {
3574 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003579 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3581 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 s = ptr + STRLEN(ptr);
3584
3585 /*
3586 * For 8.3 file names we may have to reduce the length.
3587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 /*
3591 * If there is no file name, or the file name ends in '/', and the
3592 * extension starts with '.', put a '_' before the dot, because just
3593 * ".ext" is invalid.
3594 */
3595 if (fname == NULL || *fname == NUL
3596 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *s++ = '_';
3600 }
3601 /*
3602 * If the extension starts with '.', truncate the base name at 8
3603 * characters
3604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003607 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 s = ptr + 8;
3610 *s = '\0';
3611 }
3612 }
3613 /*
3614 * If the extension doesn't start with '.', and the file name
3615 * doesn't have an extension yet, append a '.'
3616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 else if ((e = vim_strchr(ptr, '.')) == NULL)
3618 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003621 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
3623 else if ((int)STRLEN(e) + extlen > 4)
3624 s = e + 4 - extlen;
3625 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003626#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 /*
3628 * If there is no file name, and the extension starts with '.', put a
3629 * '_' before the dot, because just ".ext" may be invalid if it's on a
3630 * FAT partition, and on HPFS it doesn't matter.
3631 */
3632 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3633 *s++ = '_';
3634#endif
3635
3636 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003637 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 * ext can start with '.' and cannot exceed 3 more characters.
3639 */
3640 STRCPY(s, ext);
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 /*
3643 * Prepend the dot.
3644 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003645 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003647 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
3652 * Check that, after appending the extension, the file name is really
3653 * different.
3654 */
3655 if (fname != NULL && STRCMP(fname, retval) == 0)
3656 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003657 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 while (--s >= ptr)
3659 {
3660 if (*s != '_')
3661 {
3662 *s = '_';
3663 break;
3664 }
3665 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003666 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 *ptr = 'v';
3668 }
3669 return retval;
3670}
3671
3672/*
3673 * Like fgets(), but if the file line is too long, it is truncated and the
3674 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003675 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 */
3677 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003678vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 char *eof;
3681#define FGETS_SIZE 200
3682 char tbuf[FGETS_SIZE];
3683
3684 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3687 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003688 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 do
3692 {
3693 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003694 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3696 }
3697 return (eof == NULL);
3698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * rename() only works if both files are on the same file system, this
3702 * function will (attempts to?) copy the file across if rename fails -- webb
3703 * Return -1 for failure, 0 for success.
3704 */
3705 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003706vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 int fd_in;
3709 int fd_out;
3710 int n;
3711 char *errmsg = NULL;
3712 char *buffer;
3713#ifdef AMIGA
3714 BPTR flock;
3715#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003716 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003717 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003718#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003720#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003721 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003724 * When the names are identical, there is nothing to do. When they refer
3725 * to the same file (ignoring case and slash/backslash differences) but
3726 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003729 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003730 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003731 use_tmp_file = TRUE;
3732 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003733 return 0;
3734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 /*
3737 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3738 */
3739 if (mch_stat((char *)from, &st) < 0)
3740 return -1;
3741
Bram Moolenaar3576da72008-12-30 15:15:57 +00003742#ifdef UNIX
3743 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003744 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003746 // It's possible for the source and destination to be the same file.
3747 // This happens when "from" and "to" differ in case and are on a FAT32
3748 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003749 if (mch_stat((char *)to, &st_to) >= 0
3750 && st.st_dev == st_to.st_dev
3751 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003752 use_tmp_file = TRUE;
3753 }
3754#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003755#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003756 {
3757 BY_HANDLE_FILE_INFORMATION info1, info2;
3758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003759 // It's possible for the source and destination to be the same file.
3760 // In that case go through a temp file name. This makes rename("foo",
3761 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003762 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3763 && win32_fileinfo(to, &info2) == FILEINFO_OK
3764 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3765 && info1.nFileIndexHigh == info2.nFileIndexHigh
3766 && info1.nFileIndexLow == info2.nFileIndexLow)
3767 use_tmp_file = TRUE;
3768 }
3769#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003770
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003771 if (use_tmp_file)
3772 {
3773 char tempname[MAXPATHL + 1];
3774
3775 /*
3776 * Find a name that doesn't exist and is in the same directory.
3777 * Rename "from" to "tempname" and then rename "tempname" to "to".
3778 */
3779 if (STRLEN(from) >= MAXPATHL - 5)
3780 return -1;
3781 STRCPY(tempname, from);
3782 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003783 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003784 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3785 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003786 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003787 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003788 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003789 if (mch_rename(tempname, (char *)to) == 0)
3790 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003791 // Strange, the second step failed. Try moving the
3792 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003793 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003794 return -1;
3795 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 // If it fails for one temp name it will most likely fail
3797 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003798 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003799 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003800 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003801 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003802 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 /*
3805 * Delete the "to" file, this is required on some systems to make the
3806 * mch_rename() work, on other systems it makes sure that we don't have
3807 * two files when the mch_rename() fails.
3808 */
3809
3810#ifdef AMIGA
3811 /*
3812 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3813 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003814 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 * deleting the "from" file (horror!) we lock it during the remove.
3816 *
3817 * When used for making a backup before writing the file: This should not
3818 * happen with ":w", because startscript() should detect this problem and
3819 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3820 * name. This problem does exist with ":w filename", but then the
3821 * original file will be somewhere else so the backup isn't really
3822 * important. If autoscripting is off the rename may fail.
3823 */
3824 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3825#endif
3826 mch_remove(to);
3827#ifdef AMIGA
3828 if (flock)
3829 UnLock(flock);
3830#endif
3831
3832 /*
3833 * First try a normal rename, return if it works.
3834 */
3835 if (mch_rename((char *)from, (char *)to) == 0)
3836 return 0;
3837
3838 /*
3839 * Rename() failed, try copying the file.
3840 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003841 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003842#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003843 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003844 acl = mch_get_acl(from);
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3847 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003848 {
3849#ifdef HAVE_ACL
3850 mch_free_acl(acl);
3851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003853 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003855 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003856 fd_out = mch_open((char *)to,
3857 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (fd_out == -1)
3859 {
3860 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003861#ifdef HAVE_ACL
3862 mch_free_acl(acl);
3863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 return -1;
3865 }
3866
Bram Moolenaar473952e2019-09-28 16:30:04 +02003867 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (buffer == NULL)
3869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003871 close(fd_in);
3872#ifdef HAVE_ACL
3873 mch_free_acl(acl);
3874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 return -1;
3876 }
3877
Bram Moolenaar473952e2019-09-28 16:30:04 +02003878 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003879 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003881 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 break;
3883 }
3884
3885 vim_free(buffer);
3886 close(fd_in);
3887 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003888 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (n < 0)
3890 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003891 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 to = from;
3893 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003895 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003896#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003897#ifdef HAVE_ACL
3898 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003899 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003900#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003901#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003902 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (errmsg != NULL)
3905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003906 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return -1;
3908 }
3909 mch_remove(from);
3910 return 0;
3911}
3912
3913static int already_warned = FALSE;
3914
3915/*
3916 * Check if any not hidden buffer has been changed.
3917 * Postpone the check if there are characters in the stuff buffer, a global
3918 * command is being executed, a mapping is being executed or an autocommand is
3919 * busy.
3920 * Returns TRUE if some message was written (screen should be redrawn and
3921 * cursor positioned).
3922 */
3923 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003924check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003925 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 buf_T *buf;
3928 int didit = 0;
3929 int n;
3930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003931 // Don't check timestamps while system() or another low-level function may
3932 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 if (no_check_timestamps > 0)
3934 return FALSE;
3935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3937 // event and we would keep on checking if the file is steadily growing.
3938 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (focus && did_check_timestamps)
3940 {
3941 need_check_timestamps = TRUE;
3942 return FALSE;
3943 }
3944
3945 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003946 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003947 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 else
3949 {
3950 ++no_wait_return;
3951 did_check_timestamps = TRUE;
3952 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003953 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003955 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (buf->b_nwindows > 0)
3957 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003958 bufref_T bufref;
3959
3960 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 n = buf_check_timestamp(buf, focus);
3962 if (didit < n)
3963 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003964 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003966 // Autocommands have removed the buffer, start at the
3967 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 buf = firstbuf;
3969 continue;
3970 }
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 --no_wait_return;
3974 need_check_timestamps = FALSE;
3975 if (need_wait_return && didit == 2)
3976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003977 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003978 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 out_flush();
3980 }
3981 }
3982 return didit;
3983}
3984
3985/*
3986 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3987 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3988 * empty.
3989 */
3990 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003991move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 buf_T *tbuf = curbuf;
3994 int retval = OK;
3995 linenr_T lnum;
3996 char_u *p;
3997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003998 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 curbuf = tobuf;
4000 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4001 {
4002 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4003 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4004 {
4005 vim_free(p);
4006 retval = FAIL;
4007 break;
4008 }
4009 vim_free(p);
4010 }
4011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004012 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (retval != FAIL)
4014 {
4015 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004016 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004017 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004019 // Oops! We could try putting back the saved lines, but that
4020 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 retval = FAIL;
4022 break;
4023 }
4024 }
4025
4026 curbuf = tbuf;
4027 return retval;
4028}
4029
4030/*
4031 * Check if buffer "buf" has been changed.
4032 * Also check if the file for a new buffer unexpectedly appeared.
4033 * return 1 if a changed buffer was found.
4034 * return 2 if a message has been displayed.
4035 * return 0 otherwise.
4036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004038buf_check_timestamp(
4039 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004040 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004042 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 int stat_res;
4044 int retval = 0;
4045 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004046 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004048 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004050 enum {
4051 RELOAD_NONE,
4052 RELOAD_NORMAL,
4053 RELOAD_DETECT
4054 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004055 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4057 int can_reload = FALSE;
4058#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004059 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 int orig_mode = buf->b_orig_mode;
4061#ifdef FEAT_GUI
4062 int save_mouse_correct = need_mouse_correct;
4063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004065 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004066#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004067 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004068#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004069 bufref_T bufref;
4070
4071 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004073 // If there is no file name, the buffer is not loaded, 'buftype' is
4074 // set, we are in the middle of a save or being called recursively: ignore
4075 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (buf->b_ffname == NULL
4077 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004078 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004081#ifdef FEAT_NETBEANS_INTG
4082 || isNetbeansBuffer(buf)
4083#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004084#ifdef FEAT_TERMINAL
4085 || buf->b_term != NULL
4086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 )
4088 return 0;
4089
4090 if ( !(buf->b_flags & BF_NOTEDITED)
4091 && buf->b_mtime != 0
4092 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004093 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004094 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#ifdef HAVE_ST_MODE
4096 || (int)st.st_mode != buf->b_orig_mode
4097#else
4098 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4099#endif
4100 ))
4101 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004102 long prev_b_mtime = buf->b_mtime;
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 retval = 1;
4105
Bram Moolenaar386bc822018-07-07 18:34:12 +02004106 // set b_mtime to stop further warnings (e.g., when executing
4107 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (stat_res < 0)
4109 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004110 // Check the file again later to see if it re-appears.
4111 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 buf->b_orig_size = 0;
4113 buf->b_orig_mode = 0;
4114 }
4115 else
4116 buf_store_time(buf, &st, buf->b_ffname);
4117
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004118 // Don't do anything for a directory. Might contain the file
4119 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (mch_isdir(buf->b_fname))
4121 ;
4122
4123 /*
4124 * If 'autoread' is set, the buffer has no changes and the file still
4125 * exists, reload the buffer. Use the buffer-local option value if it
4126 * was set, the global option value otherwise.
4127 */
4128 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4129 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004130 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 else
4132 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004133 if (stat_res < 0)
4134 reason = "deleted";
4135 else if (bufIsChanged(buf))
4136 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004137 /*
4138 * Check if the file contents really changed to avoid giving a
4139 * warning when only the timestamp was set (e.g., checked out of
4140 * CVS). Always warn when the buffer was changed.
4141 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004142 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4143 reason = "changed";
4144 else if (orig_mode != buf->b_orig_mode)
4145 reason = "mode";
4146 else
4147 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Only give the warning if there are no FileChangedShell
4151 * autocommands.
4152 * Avoid being called recursively by setting "busy".
4153 */
4154 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004155#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4157 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004158#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004159 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4161 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004162 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 busy = FALSE;
4164 if (n)
4165 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004166 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004167 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004168#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004169 s = get_vim_var_str(VV_FCS_CHOICE);
4170 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004171 reload = RELOAD_NORMAL;
4172 else if (STRCMP(s, "edit") == 0)
4173 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004174 else if (STRCMP(s, "ask") == 0)
4175 n = FALSE;
4176 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004177#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004178 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004180 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004183 {
4184 // Only give the message once.
4185 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004186 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
4189 {
4190 helpmesg = TRUE;
4191#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4192 can_reload = TRUE;
4193#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004194 if (reason[2] == 'n')
4195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004197 mesg2 = _("See \":help W12\" for more info.");
4198 }
4199 else if (reason[1] == 'h')
4200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004202 mesg2 = _("See \":help W11\" for more info.");
4203 }
4204 else if (*reason == 'm')
4205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004207 mesg2 = _("See \":help W16\" for more info.");
4208 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004209 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 // Only timestamp changed, store it to avoid a warning
4212 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004213 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004214 buf->b_mtime_read_ns = buf->b_mtime_ns;
4215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217 }
4218 }
4219
4220 }
4221 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4222 && vim_fexists(buf->b_ffname))
4223 {
4224 retval = 1;
4225 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4226 buf->b_flags |= BF_NEW_W;
4227#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4228 can_reload = TRUE;
4229#endif
4230 }
4231
4232 if (mesg != NULL)
4233 {
4234 path = home_replace_save(buf, buf->b_fname);
4235 if (path != NULL)
4236 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004237 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004239 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004240 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004241#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004242 // Set warningmsg here, before the unimportant and output-specific
4243 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004244 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4247 if (can_reload)
4248 {
4249 if (*mesg2 != NUL)
4250 {
4251 STRCAT(tbuf, "\n");
4252 STRCAT(tbuf, mesg2);
4253 }
Rob Pilling8196e942022-02-11 15:12:10 +00004254 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4255 (char_u *)tbuf,
4256 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4257 1, NULL, TRUE))
4258 {
4259 case 2:
4260 reload = RELOAD_NORMAL;
4261 break;
4262 case 3:
4263 reload = RELOAD_DETECT;
4264 break;
4265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 else
4268#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004269 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4270 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
4272 if (*mesg2 != NUL)
4273 {
4274 STRCAT(tbuf, "; ");
4275 STRCAT(tbuf, mesg2);
4276 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004277 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 retval = 2;
4279 }
4280 else
4281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
4284 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004285 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004287 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 msg_clr_eos();
4289 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004290 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
4292 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004293#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004295#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004296 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004297 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004299 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 redraw_cmdline = FALSE;
4301 }
4302 }
4303 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305
4306 vim_free(path);
4307 vim_free(tbuf);
4308 }
4309 }
4310
Rob Pilling8196e942022-02-11 15:12:10 +00004311 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004313 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004314 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004315#ifdef FEAT_PERSISTENT_UNDO
4316 if (buf->b_p_udf && buf->b_ffname != NULL)
4317 {
4318 char_u hash[UNDO_HASH_SIZE];
4319 buf_T *save_curbuf = curbuf;
4320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004322 curbuf = buf;
4323 u_compute_hash(hash);
4324 u_write_undo(NULL, FALSE, buf, hash);
4325 curbuf = save_curbuf;
4326 }
4327#endif
4328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004330 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004331 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004332 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4333 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004335 // restore this in case an autocommand has set it; it would break
4336 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 need_mouse_correct = save_mouse_correct;
4338#endif
4339
4340 return retval;
4341}
4342
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004343/*
4344 * Reload a buffer that is already loaded.
4345 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004346 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4347 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004348 */
4349 void
Rob Pilling8196e942022-02-11 15:12:10 +00004350buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004351{
4352 exarg_T ea;
4353 pos_T old_cursor;
4354 linenr_T old_topline;
4355 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004356 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004357 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004358 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004359 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004360 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004361 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004364 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004365
Rob Pilling8196e942022-02-11 15:12:10 +00004366 // Unless reload_options is set, we only want to read the text from the
4367 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4368 // Force the fileformat and encoding to be the same.
4369 if (reload_options)
4370 memset(&ea, 0, sizeof(ea));
4371 else
4372 prepped = prep_exarg(&ea, buf);
4373
4374 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004375 {
4376 old_cursor = curwin->w_cursor;
4377 old_topline = curwin->w_topline;
4378
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004379 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004381 // Save all the text, so that the reload can be undone.
4382 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004383 u_sync(FALSE);
4384 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4385 flags |= READ_KEEP_UNDO;
4386 }
4387
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004388 /*
4389 * To behave like when a new file is edited (matters for
4390 * BufReadPost autocommands) we first need to delete the current
4391 * buffer contents. But if reading the file fails we should keep
4392 * the old contents. Can't use memory only, the file might be
4393 * too big. Use a hidden buffer to move the buffer contents to.
4394 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004395 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004396 savebuf = NULL;
4397 else
4398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004401 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004402 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004403 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 curbuf = savebuf;
4406 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004407 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004408 curbuf = buf;
4409 curwin->w_buffer = buf;
4410 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004411 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412 || move_lines(buf, savebuf) == FAIL)
4413 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004414 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004415 saved = FAIL;
4416 }
4417 }
4418
4419 if (saved == OK)
4420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4422 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004423 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4424 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004425 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004426 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004427#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004428 if (!aborting())
4429#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004430 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004431 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004433 // Put the text back from the save buffer. First
4434 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004435 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004436 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004437 break;
4438 (void)move_lines(savebuf, buf);
4439 }
4440 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004442 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004444 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004445 if ((flags & READ_KEEP_UNDO) == 0)
4446 {
4447 u_blockfree(buf);
4448 u_clearall(buf);
4449 }
4450 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004451 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004453 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004454 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004455 }
4456 }
4457 vim_free(ea.cmd);
4458
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004459 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004460 wipe_buffer(savebuf, FALSE);
4461
4462#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004463 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004464 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004465#endif
4466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004467 // Restore the topline and cursor position and check it (lines may
4468 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004469 if (old_topline > curbuf->b_ml.ml_line_count)
4470 curwin->w_topline = curbuf->b_ml.ml_line_count;
4471 else
4472 curwin->w_topline = old_topline;
4473 curwin->w_cursor = old_cursor;
4474 check_cursor();
4475 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004476 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004477#ifdef FEAT_FOLDING
4478 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004479 win_T *wp;
4480 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004482 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004483 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004484 if (wp->w_buffer == curwin->w_buffer
4485 && !foldmethodIsManual(wp))
4486 foldUpdateAll(wp);
4487 }
4488#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // If the mode didn't change and 'readonly' was set, keep the old
4490 // value; the user probably used the ":view" command. But don't
4491 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004492 if (orig_mode == curbuf->b_orig_mode)
4493 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004496 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004497 }
4498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004500 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004501 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004502}
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004505buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
4507 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004508#ifdef ST_MTIM_NSEC
4509 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4510#else
4511 buf->b_mtime_ns = 0;
4512#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004513 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#ifdef HAVE_ST_MODE
4515 buf->b_orig_mode = (int)st->st_mode;
4516#else
4517 buf->b_orig_mode = mch_getperm(fname);
4518#endif
4519}
4520
4521/*
4522 * Adjust the line with missing eol, used for the next write.
4523 * Used for do_filter(), when the input lines for the filter are deleted.
4524 */
4525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004526write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004528 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004529 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
4531
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004532// Subfuncions for readdirex()
4533#ifdef FEAT_EVAL
4534# ifdef MSWIN
4535 static char_u *
4536getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4537{
4538 stat_T st;
4539 unsigned short st_mode;
4540 DWORD flag = wfd->dwFileAttributes;
4541 WCHAR *wp;
4542
4543 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4544 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4545 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4546 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4547
4548 wp = wcsrchr(wfd->cFileName, L'.');
4549 if (wp != NULL)
4550 {
4551 if (_wcsicmp(wp, L".exe") == 0 ||
4552 _wcsicmp(wp, L".com") == 0 ||
4553 _wcsicmp(wp, L".cmd") == 0 ||
4554 _wcsicmp(wp, L".bat") == 0)
4555 st_mode |= _S_IEXEC;
4556 }
4557
4558 // Copy user bits to group/other.
4559 st_mode |= (st_mode & 0700) >> 3;
4560 st_mode |= (st_mode & 0700) >> 6;
4561
4562 st.st_mode = st_mode;
4563 return getfpermst(&st, perm);
4564}
4565
4566 static char_u *
4567getftypewfd(WIN32_FIND_DATAW *wfd)
4568{
4569 DWORD flag = wfd->dwFileAttributes;
4570 DWORD tag = wfd->dwReserved0;
4571
4572 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4573 {
4574 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4575 return (char_u*)"junction";
4576 else if (tag == IO_REPARSE_TAG_SYMLINK)
4577 {
4578 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4579 return (char_u*)"linkd";
4580 else
4581 return (char_u*)"link";
4582 }
4583 return (char_u*)"reparse"; // unknown reparse point type
4584 }
4585 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4586 return (char_u*)"dir";
4587 else
4588 return (char_u*)"file";
4589}
4590
4591 static dict_T *
4592create_readdirex_item(WIN32_FIND_DATAW *wfd)
4593{
4594 dict_T *item;
4595 char_u *p;
4596 varnumber_T size, time;
4597 char_u permbuf[] = "---------";
4598
4599 item = dict_alloc();
4600 if (item == NULL)
4601 return NULL;
4602 item->dv_refcount++;
4603
4604 p = utf16_to_enc(wfd->cFileName, NULL);
4605 if (p == NULL)
4606 goto theend;
4607 if (dict_add_string(item, "name", p) == FAIL)
4608 {
4609 vim_free(p);
4610 goto theend;
4611 }
4612 vim_free(p);
4613
4614 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4615 if (dict_add_number(item, "size", size) == FAIL)
4616 goto theend;
4617
4618 // Convert FILETIME to unix time.
4619 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4620 wfd->ftLastWriteTime.dwLowDateTime)
4621 - 116444736000000000) / 10000000;
4622 if (dict_add_number(item, "time", time) == FAIL)
4623 goto theend;
4624
4625 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4626 goto theend;
4627 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4628 goto theend;
4629
4630 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4631 goto theend;
4632 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4633 goto theend;
4634
4635 return item;
4636
4637theend:
4638 dict_unref(item);
4639 return NULL;
4640}
4641# else
4642 static dict_T *
4643create_readdirex_item(char_u *path, char_u *name)
4644{
4645 dict_T *item;
4646 char *p;
4647 size_t len;
4648 stat_T st;
4649 int ret, link = FALSE;
4650 varnumber_T size;
4651 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004652 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004653 struct passwd *pw;
4654 struct group *gr;
4655
4656 item = dict_alloc();
4657 if (item == NULL)
4658 return NULL;
4659 item->dv_refcount++;
4660
4661 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4662 p = alloc(len);
4663 if (p == NULL)
4664 goto theend;
4665 vim_snprintf(p, len, "%s/%s", path, name);
4666 ret = mch_lstat(p, &st);
4667 if (ret >= 0 && S_ISLNK(st.st_mode))
4668 {
4669 link = TRUE;
4670 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004671 if (ret < 0)
4672 q = (char_u*)"link";
4673
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004674 }
4675 vim_free(p);
4676
4677 if (dict_add_string(item, "name", name) == FAIL)
4678 goto theend;
4679
4680 if (ret >= 0)
4681 {
4682 size = (varnumber_T)st.st_size;
4683 if (S_ISDIR(st.st_mode))
4684 size = 0;
4685 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004686 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004687 size = -2;
4688 if (dict_add_number(item, "size", size) == FAIL)
4689 goto theend;
4690 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4691 goto theend;
4692
4693 if (link)
4694 {
4695 if (S_ISDIR(st.st_mode))
4696 q = (char_u*)"linkd";
4697 else
4698 q = (char_u*)"link";
4699 }
4700 else
4701 q = getftypest(&st);
4702 if (dict_add_string(item, "type", q) == FAIL)
4703 goto theend;
4704 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4705 goto theend;
4706
4707 pw = getpwuid(st.st_uid);
4708 if (pw == NULL)
4709 q = (char_u*)"";
4710 else
4711 q = (char_u*)pw->pw_name;
4712 if (dict_add_string(item, "user", q) == FAIL)
4713 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004714# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004715 gr = getgrgid(st.st_gid);
4716 if (gr == NULL)
4717 q = (char_u*)"";
4718 else
4719 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004720# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004721 if (dict_add_string(item, "group", q) == FAIL)
4722 goto theend;
4723 }
4724 else
4725 {
4726 if (dict_add_number(item, "size", -1) == FAIL)
4727 goto theend;
4728 if (dict_add_number(item, "time", -1) == FAIL)
4729 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004730 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004731 goto theend;
4732 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4733 goto theend;
4734 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4735 goto theend;
4736 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4737 goto theend;
4738 }
4739 return item;
4740
4741theend:
4742 dict_unref(item);
4743 return NULL;
4744}
4745# endif
4746
4747 static int
4748compare_readdirex_item(const void *p1, const void *p2)
4749{
4750 char_u *name1, *name2;
4751
Bram Moolenaard61efa52022-07-23 09:52:04 +01004752 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4753 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004754 if (readdirex_sort == READDIR_SORT_BYTE)
4755 return STRCMP(name1, name2);
4756 else if (readdirex_sort == READDIR_SORT_IC)
4757 return STRICMP(name1, name2);
4758 else
4759 return STRCOLL(name1, name2);
4760}
4761
4762 static int
4763compare_readdir_item(const void *s1, const void *s2)
4764{
4765 if (readdirex_sort == READDIR_SORT_BYTE)
4766 return STRCMP(*(char **)s1, *(char **)s2);
4767 else if (readdirex_sort == READDIR_SORT_IC)
4768 return STRICMP(*(char **)s1, *(char **)s2);
4769 else
4770 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004771}
4772#endif
4773
Bram Moolenaarda440d22016-01-16 21:27:23 +01004774#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4775/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004776 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004777 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004778 * If "withattr" is TRUE, retrieve the names and their attributes.
4779 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004780 * Return OK for success, FAIL for failure.
4781 */
4782 int
4783readdir_core(
4784 garray_T *gap,
4785 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004786 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004787 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004788 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004789 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004790{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004791 int failed = FALSE;
4792 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004793# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004794 char_u *buf;
4795 int ok;
4796 HANDLE hFind = INVALID_HANDLE_VALUE;
4797 WIN32_FIND_DATAW wfd;
4798 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004799# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004800 DIR *dirp;
4801 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004802# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004803
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004804 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004805
4806# ifdef FEAT_EVAL
4807# define FREE_ITEM(item) do { \
4808 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004809 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004810 else \
4811 vim_free(item); \
4812 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004813
4814 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004815# else
4816# define FREE_ITEM(item) vim_free(item)
4817# endif
4818
4819# ifdef MSWIN
4820 buf = alloc(MAXPATHL);
4821 if (buf == NULL)
4822 return FAIL;
4823 STRNCPY(buf, path, MAXPATHL-5);
4824 p = buf + STRLEN(buf);
4825 MB_PTR_BACK(buf, p);
4826 if (*p == '\\' || *p == '/')
4827 *p = NUL;
4828 STRCAT(p, "\\*");
4829
4830 wn = enc_to_utf16(buf, NULL);
4831 if (wn != NULL)
4832 hFind = FindFirstFileW(wn, &wfd);
4833 ok = (hFind != INVALID_HANDLE_VALUE);
4834 if (!ok)
4835 {
4836 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004837 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004838 }
4839 else
4840 {
4841 while (ok)
4842 {
4843 int ignore;
4844 void *item;
4845 WCHAR *wp;
4846
4847 wp = wfd.cFileName;
4848 ignore = wp[0] == L'.' &&
4849 (wp[1] == NUL ||
4850 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004851 if (ignore)
4852 {
4853 ok = FindNextFileW(hFind, &wfd);
4854 continue;
4855 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004856# ifdef FEAT_EVAL
4857 if (withattr)
4858 item = (void*)create_readdirex_item(&wfd);
4859 else
4860# endif
4861 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4862 if (item == NULL)
4863 {
4864 failed = TRUE;
4865 break;
4866 }
4867
4868 if (!ignore && checkitem != NULL)
4869 {
4870 int r = checkitem(context, item);
4871
4872 if (r < 0)
4873 {
4874 FREE_ITEM(item);
4875 break;
4876 }
4877 if (r == 0)
4878 ignore = TRUE;
4879 }
4880
4881 if (!ignore)
4882 {
4883 if (ga_grow(gap, 1) == OK)
4884 ((void**)gap->ga_data)[gap->ga_len++] = item;
4885 else
4886 {
4887 failed = TRUE;
4888 FREE_ITEM(item);
4889 break;
4890 }
4891 }
4892 else
4893 FREE_ITEM(item);
4894
4895 ok = FindNextFileW(hFind, &wfd);
4896 }
4897 FindClose(hFind);
4898 }
4899
4900 vim_free(buf);
4901 vim_free(wn);
4902# else // MSWIN
4903 dirp = opendir((char *)path);
4904 if (dirp == NULL)
4905 {
4906 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004907 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004908 }
4909 else
4910 {
4911 for (;;)
4912 {
4913 int ignore;
4914 void *item;
4915
4916 dp = readdir(dirp);
4917 if (dp == NULL)
4918 break;
4919 p = (char_u *)dp->d_name;
4920
4921 ignore = p[0] == '.' &&
4922 (p[1] == NUL ||
4923 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004924 if (ignore)
4925 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004926# ifdef FEAT_EVAL
4927 if (withattr)
4928 item = (void*)create_readdirex_item(path, p);
4929 else
4930# endif
4931 item = (void*)vim_strsave(p);
4932 if (item == NULL)
4933 {
4934 failed = TRUE;
4935 break;
4936 }
4937
Bram Moolenaarfe154992022-03-22 20:42:12 +00004938 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004939 {
4940 int r = checkitem(context, item);
4941
4942 if (r < 0)
4943 {
4944 FREE_ITEM(item);
4945 break;
4946 }
4947 if (r == 0)
4948 ignore = TRUE;
4949 }
4950
4951 if (!ignore)
4952 {
4953 if (ga_grow(gap, 1) == OK)
4954 ((void**)gap->ga_data)[gap->ga_len++] = item;
4955 else
4956 {
4957 failed = TRUE;
4958 FREE_ITEM(item);
4959 break;
4960 }
4961 }
4962 else
4963 FREE_ITEM(item);
4964 }
4965
4966 closedir(dirp);
4967 }
4968# endif // MSWIN
4969
4970# undef FREE_ITEM
4971
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004972 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004973 {
4974# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004975 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004976 if (withattr)
4977 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4978 compare_readdirex_item);
4979 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004980 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4981 compare_readdir_item);
4982# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004983 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004984# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004985 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004986
4987 return failed ? FAIL : OK;
4988}
4989
4990/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004991 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004992 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004993 */
4994 int
4995delete_recursive(char_u *name)
4996{
4997 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004998 int i;
4999 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005000 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005001
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005002 // A symbolic link to a directory itself is deleted, not the directory it
5003 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005004 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005005# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005006 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005007# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005008 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005009# endif
5010 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005011 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005012 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005013 if (exp == NULL)
5014 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005015 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005016 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005017 for (i = 0; i < ga.ga_len; ++i)
5018 {
5019 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5020 ((char_u **)ga.ga_data)[i]);
5021 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005022 // Remember the failure but continue deleting any further
5023 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005024 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005025 }
5026 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005027 if (mch_rmdir(exp) != 0)
5028 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005029 }
5030 else
5031 result = -1;
5032 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005033 }
5034 else
5035 result = mch_remove(name) == 0 ? 0 : -1;
5036
5037 return result;
5038}
5039#endif
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005042static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005044# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5045/*
5046 * Open temporary directory and take file lock to prevent
5047 * to be auto-cleaned.
5048 */
5049 static void
5050vim_opentempdir(void)
5051{
5052 DIR *dp = NULL;
5053
5054 if (vim_tempdir_dp != NULL)
5055 return;
5056
5057 dp = opendir((const char*)vim_tempdir);
5058
5059 if (dp != NULL)
5060 {
5061 vim_tempdir_dp = dp;
5062 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5063 }
5064}
5065
5066/*
5067 * Close temporary directory - it automatically release file lock.
5068 */
5069 static void
5070vim_closetempdir(void)
5071{
5072 if (vim_tempdir_dp != NULL)
5073 {
5074 closedir(vim_tempdir_dp);
5075 vim_tempdir_dp = NULL;
5076 }
5077}
5078# endif
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080/*
5081 * Delete the temp directory and all files it contains.
5082 */
5083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005084vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (vim_tempdir != NULL)
5087 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005088# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5089 vim_closetempdir();
5090# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005092 gettail(vim_tempdir)[-1] = NUL;
5093 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005094 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
5098/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005099 * Directory "tempdir" was created. Expand this name to a full path and put
5100 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5101 * "tempdir" must be no longer than MAXPATHL.
5102 */
5103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005104vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005105{
5106 char_u *buf;
5107
Bram Moolenaar964b3742019-05-24 18:54:09 +02005108 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005109 if (buf != NULL)
5110 {
5111 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5112 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005113 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005114 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005115# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5116 vim_opentempdir();
5117# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005118 vim_free(buf);
5119 }
5120}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005121#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005122
5123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 * vim_tempname(): Return a unique name that can be used for a temp file.
5125 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005126 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5127 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 *
5129 * The returned pointer is to allocated memory.
5130 * The returned pointer is NULL if no valid name was found.
5131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005133vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005134 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005135 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
5137#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005138 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005139#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005140 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#else
5142 char_u itmp[TEMPNAMELEN];
5143#endif
5144
5145#ifdef TEMPDIRNAMES
5146 static char *(tempdirs[]) = {TEMPDIRNAMES};
5147 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005149 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150# endif
5151
5152 /*
5153 * This will create a directory for private use by this instance of Vim.
5154 * This is done once, and the same directory is used for all temp files.
5155 * This method avoids security problems because of symlink attacks et al.
5156 * It's also a bit faster, because we only need to check for an existing
5157 * file when creating the directory and not for each temp file.
5158 */
5159 if (vim_tempdir == NULL)
5160 {
5161 /*
5162 * Try the entries in TEMPDIRNAMES to create the temp directory.
5163 */
K.Takataeeec2542021-06-02 13:28:16 +02005164 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005166# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005167 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005168 long nr;
5169 long off;
5170# endif
5171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 // Expand $TMP, leave room for "/v1100000/999999999".
5173 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005175 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005177 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005178 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaareaf03392009-11-17 11:08:52 +00005180# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005181 {
5182# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005183 // Make sure the umask doesn't remove the executable bit.
5184 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005185 mode_t umask_save = umask(077);
5186# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005187 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005188 STRCAT(itmp, "vXXXXXX");
5189 if (mkdtemp((char *)itmp) != NULL)
5190 vim_settempdir(itmp);
5191# if defined(UNIX) || defined(VMS)
5192 (void)umask(umask_save);
5193# endif
5194 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005195# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005196 // Get an arbitrary number of up to 6 digits. When it's
5197 // unlikely that it already exists it will be faster,
5198 // otherwise it doesn't matter. The use of mkdir() avoids any
5199 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005201 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005203 // Try up to 10000 different values until we find a name that
5204 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 for (off = 0; off < 10000L; ++off)
5206 {
5207 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005208# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
Bram Moolenaareaf03392009-11-17 11:08:52 +00005212 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5213# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005214 // If mkdir() does not set errno to EEXIST, check for
5215 // existing file here. There is a race condition then,
5216 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (mch_stat((char *)itmp, &st) >= 0)
5218 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005219# endif
5220# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 // Make sure the umask doesn't remove the executable bit.
5222 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005226# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (r == 0)
5230 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005231 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005234# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 // If the mkdir() didn't fail because the file/dir exists,
5236 // we probably can't create any dir here, try another
5237 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005242# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (vim_tempdir != NULL)
5244 break;
5245 }
5246 }
5247 }
5248
5249 if (vim_tempdir != NULL)
5250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005251 // There is no need to check if the file exists, because we own the
5252 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5254 return vim_strsave(itmp);
5255 }
5256
5257 return NULL;
5258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
Bram Moolenaar4f974752019-02-17 17:44:42 +01005261# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005262 WCHAR wszTempFile[_MAX_PATH + 1];
5263 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005264 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 char_u *retval;
5266 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005267 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005268 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005270 wcscpy(itmp, L"");
5271 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005272 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005273 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005274 wszTempFile[1] = L'\\';
5275 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005276 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005277 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005278
5279 // randomize the name to avoid collisions
5280 i = mch_get_pid() + extra_char;
5281 buf4[1] = chartab[i % 36];
5282 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005283 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005285 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005286 // GetTempFileName() will create the file, we don't want that
5287 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005289 // Backslashes in a temp file name cause problems when filtering with
5290 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005291 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005292 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005293 shname = gettail(p_sh);
5294 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5295 || strstr((char *)shname, "pwsh") != NULL ))
5296 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 for (p = retval; *p; ++p)
5298 if (*p == '\\')
5299 *p = '/';
5300 return retval;
5301
Bram Moolenaar4f974752019-02-17 17:44:42 +01005302# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005305 char_u *p;
5306
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005307 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005308 p = tmpnam((char *)itmp);
5309 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return NULL;
5311# else
5312 char_u *p;
5313
5314# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 // mktemp() is not working on VMS. It seems to be
5316 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 sprintf((char *)itmp, "VIM%c", extra_char);
5318 p = (char_u *)tempnam("tmp:", (char *)itmp);
5319 if (p != NULL)
5320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005321 // VMS will use '.LIS' if we don't explicitly specify an extension,
5322 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 STRCPY(itmp, p);
5324 STRCAT(itmp, ".txt");
5325 free(p);
5326 }
5327 else
5328 return NULL;
5329# else
5330 STRCPY(itmp, TEMPNAME);
5331 if ((p = vim_strchr(itmp, '?')) != NULL)
5332 *p = extra_char;
5333 if (mktemp((char *)itmp) == NULL)
5334 return NULL;
5335# endif
5336# endif
5337
5338 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005339# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005340#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341}
5342
5343#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5344/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005345 * Convert all backslashes in fname to forward slashes in-place, unless when
5346 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 */
5348 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005349forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 char_u *p;
5352
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005353 if (path_with_url(fname))
5354 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005356 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005357 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005359 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 *p = '/';
5361}
5362#endif
5363
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005364/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005365 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5366 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5367 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 * Used for autocommands and 'wildignore'.
5369 * Returns TRUE if there is a match, FALSE otherwise.
5370 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005371 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005372match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 char_u *pattern, // pattern to match with
5374 regprog_T **prog, // pre-compiled regprog or NULL
5375 char_u *fname, // full path of file name
5376 char_u *sfname, // short file name or NULL
5377 char_u *tail, // tail of path
5378 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 regmatch_T regmatch;
5381 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005383 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005384 if (prog != NULL)
5385 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005387 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389 /*
5390 * Try for a match with the pattern with:
5391 * 1. the full file name, when the pattern has a '/'.
5392 * 2. the short file name, when the pattern has a '/'.
5393 * 3. the tail of the file name, when the pattern has no '/'.
5394 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005395 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 && ((allow_dirs
5397 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5398 || (sfname != NULL
5399 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005400 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 result = TRUE;
5402
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005403 if (prog != NULL)
5404 *prog = regmatch.regprog;
5405 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005406 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return result;
5408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
5411 * Return TRUE if a file matches with a pattern in "list".
5412 * "list" is a comma-separated list of patterns, like 'wildignore'.
5413 * "sfname" is the short file name or NULL, "ffname" the long file name.
5414 */
5415 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 char_u buf[100];
5419 char_u *tail;
5420 char_u *regpat;
5421 char allow_dirs;
5422 int match;
5423 char_u *p;
5424
5425 tail = gettail(sfname);
5426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005427 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 p = list;
5429 while (*p)
5430 {
5431 copy_option_part(&p, buf, 100, ",");
5432 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5433 if (regpat == NULL)
5434 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005435 match = match_file_pat(regpat, NULL, ffname, sfname,
5436 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 vim_free(regpat);
5438 if (match)
5439 return TRUE;
5440 }
5441 return FALSE;
5442}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444/*
5445 * Convert the given pattern "pat" which has shell style wildcards in it, into
5446 * a regular expression, and return the result in allocated memory. If there
5447 * is a directory path separator to be matched, then TRUE is put in
5448 * allow_dirs, otherwise FALSE is put there -- webb.
5449 * Handle backslashes before special characters, like "\*" and "\ ".
5450 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 * Returns NULL when out of memory.
5452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454file_pat_to_reg_pat(
5455 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005456 char_u *pat_end, // first char after pattern or NULL
5457 char *allow_dirs, // Result passed back out in here
5458 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 char_u *endp;
5462 char_u *reg_pat;
5463 char_u *p;
5464 int i;
5465 int nested = 0;
5466 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
5468 if (allow_dirs != NULL)
5469 *allow_dirs = FALSE;
5470 if (pat_end == NULL)
5471 pat_end = pat + STRLEN(pat);
5472
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 for (p = pat; p < pat_end; p++)
5474 {
5475 switch (*p)
5476 {
5477 case '*':
5478 case '.':
5479 case ',':
5480 case '{':
5481 case '}':
5482 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 break;
5485#ifdef BACKSLASH_IN_FILENAME
5486 case '\\':
5487 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005488 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 break;
5490#endif
5491 default:
5492 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005493 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
5495 ++p;
5496 ++size;
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 break;
5499 }
5500 }
5501 reg_pat = alloc(size + 1);
5502 if (reg_pat == NULL)
5503 return NULL;
5504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507 if (pat[0] == '*')
5508 while (pat[0] == '*' && pat < pat_end - 1)
5509 pat++;
5510 else
5511 reg_pat[i++] = '^';
5512 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005513 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
5515 while (endp - pat > 0 && *endp == '*')
5516 endp--;
5517 add_dollar = FALSE;
5518 }
5519 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5520 {
5521 switch (*p)
5522 {
5523 case '*':
5524 reg_pat[i++] = '.';
5525 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005527 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 break;
5529 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 case '~':
5531 reg_pat[i++] = '\\';
5532 reg_pat[i++] = *p;
5533 break;
5534 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 reg_pat[i++] = '.';
5536 break;
5537 case '\\':
5538 if (p[1] == NUL)
5539 break;
5540#ifdef BACKSLASH_IN_FILENAME
5541 if (!no_bslash)
5542 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005543 // translate:
5544 // "\x" to "\\x" e.g., "dir\file"
5545 // "\*" to "\\.*" e.g., "dir\*.c"
5546 // "\?" to "\\." e.g., "dir\??.c"
5547 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5549 && p[1] != '+')
5550 {
5551 reg_pat[i++] = '[';
5552 reg_pat[i++] = '\\';
5553 reg_pat[i++] = '/';
5554 reg_pat[i++] = ']';
5555 if (allow_dirs != NULL)
5556 *allow_dirs = TRUE;
5557 break;
5558 }
5559 }
5560#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005561 // Undo escaping from ExpandEscape():
5562 // foo\?bar -> foo?bar
5563 // foo\%bar -> foo%bar
5564 // foo\,bar -> foo,bar
5565 // foo\ bar -> foo bar
5566 // Don't unescape \, * and others that are also special in a
5567 // regexp.
5568 // An escaped { must be unescaped since we use magic not
5569 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 if (*++p == '?'
5571#ifdef BACKSLASH_IN_FILENAME
5572 && no_bslash
5573#endif
5574 )
5575 reg_pat[i++] = '?';
5576 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005577 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005578 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005579 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005580 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5581 {
5582 reg_pat[i++] = '\\';
5583 reg_pat[i++] = '{';
5584 p += 2;
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 else
5587 {
5588 if (allow_dirs != NULL && vim_ispathsep(*p)
5589#ifdef BACKSLASH_IN_FILENAME
5590 && (!no_bslash || *p != '\\')
5591#endif
5592 )
5593 *allow_dirs = TRUE;
5594 reg_pat[i++] = '\\';
5595 reg_pat[i++] = *p;
5596 }
5597 break;
5598#ifdef BACKSLASH_IN_FILENAME
5599 case '/':
5600 reg_pat[i++] = '[';
5601 reg_pat[i++] = '\\';
5602 reg_pat[i++] = '/';
5603 reg_pat[i++] = ']';
5604 if (allow_dirs != NULL)
5605 *allow_dirs = TRUE;
5606 break;
5607#endif
5608 case '{':
5609 reg_pat[i++] = '\\';
5610 reg_pat[i++] = '(';
5611 nested++;
5612 break;
5613 case '}':
5614 reg_pat[i++] = '\\';
5615 reg_pat[i++] = ')';
5616 --nested;
5617 break;
5618 case ',':
5619 if (nested)
5620 {
5621 reg_pat[i++] = '\\';
5622 reg_pat[i++] = '|';
5623 }
5624 else
5625 reg_pat[i++] = ',';
5626 break;
5627 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005628 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005630 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 *allow_dirs = TRUE;
5632 reg_pat[i++] = *p;
5633 break;
5634 }
5635 }
5636 if (add_dollar)
5637 reg_pat[i++] = '$';
5638 reg_pat[i] = NUL;
5639 if (nested != 0)
5640 {
5641 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005642 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005644 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005645 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 return reg_pat;
5648}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005649
5650#if defined(EINTR) || defined(PROTO)
5651/*
5652 * Version of read() that retries when interrupted by EINTR (possibly
5653 * by a SIGWINCH).
5654 */
5655 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005657{
5658 long ret;
5659
5660 for (;;)
5661 {
5662 ret = vim_read(fd, buf, bufsize);
5663 if (ret >= 0 || errno != EINTR)
5664 break;
5665 }
5666 return ret;
5667}
5668
5669/*
5670 * Version of write() that retries when interrupted by EINTR (possibly
5671 * by a SIGWINCH).
5672 */
5673 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005674write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005675{
5676 long ret = 0;
5677 long wlen;
5678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 // Repeat the write() so long it didn't fail, other than being interrupted
5680 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005681 while (ret < (long)bufsize)
5682 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005683 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005684 if (wlen < 0)
5685 {
5686 if (errno != EINTR)
5687 break;
5688 }
5689 else
5690 ret += wlen;
5691 }
5692 return ret;
5693}
5694#endif