blob: 701521a4191a49688ab9295b841b10ffb9750061 [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;
593 curbuf->b_start_eol = TRUE;
594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000596 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100599 // Create a swap file now, so that other Vims are warned that we are
600 // editing this file.
601 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {
604 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000605 if (!read_stdin && (curbuf != old_curbuf
606 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
607 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
608 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000609 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000610 if (!read_buffer)
611 close(fd);
612 return FAIL;
613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100615 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000616 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
617 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100618 {
619 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
620
621 /*
622 * If the group-read bit is set but not the world-read bit, then
623 * the group must be equal to the group of the original file. If
624 * we can't make that happen then reset the group-read bit. This
625 * avoids making the swap file readable to more users when the
626 * primary group of the user is too permissive.
627 */
628 if ((swap_mode & 044) == 040)
629 {
630 stat_T swap_st;
631
632 if (mch_stat((char *)swap_fname, &swap_st) >= 0
633 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200634# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100635 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200636 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200637# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200638 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100639 swap_mode &= 0600;
640 }
641
642 (void)mch_setperm(swap_fname, (long)swap_mode);
643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#endif
645 }
646
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200647 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (swap_exists_action == SEA_QUIT)
649 {
650 if (!read_buffer && !read_stdin)
651 close(fd);
652 return FAIL;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 /*
658 * Set '[ mark to the line above where the lines go (line 1 if zero).
659 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100660 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
662 curbuf->b_op_start.col = 0;
663
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100664 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
665 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
666 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (!read_buffer)
669 {
670 int m = msg_scroll;
671 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
673 /*
674 * The file must be closed again, the autocommands may want to change
675 * the file before reading it.
676 */
677 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 /*
681 * The output from the autocommands should not overwrite anything and
682 * should not be overwritten: Set msg_scroll, restore its value if no
683 * output was done.
684 */
685 msg_scroll = TRUE;
686 if (filtering)
687 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
688 FALSE, curbuf, eap);
689 else if (read_stdin)
690 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
691 FALSE, curbuf, eap);
692 else if (newfile)
693 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
694 FALSE, curbuf, eap);
695 else
696 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
697 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100699 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
700 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
701 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100702 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 if (msg_scrolled == n)
705 msg_scroll = m;
706
707#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100708 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
710 --no_wait_return;
711 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100712 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 return FAIL;
714 }
715#endif
716 /*
717 * Don't allow the autocommands to change the current buffer.
718 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000719 *
720 * Don't allow the autocommands to change the buffer name either
721 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 */
723 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000724 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
725 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
727 {
728 --no_wait_return;
729 msg_scroll = msg_save;
730 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000731 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000733 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100734 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 return FAIL;
736 }
737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100739 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
741
742 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
743 {
744 /*
745 * Show the user that we are busy reading the input. Sometimes this
746 * may take a while. When reading from stdin another program may
747 * still be running, don't move the cursor to the last line, unless
748 * always using the GUI.
749 */
750 if (read_stdin)
751 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100752 if (!is_not_a_term())
753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200755# ifdef VIMDLL
756 if (!gui.in_use)
757# endif
758 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#endif
760#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100761 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100762 if (gui.in_use && !gui.dying && !gui.starting)
763 {
Amon Sha10197932022-02-21 15:07:12 +0000764 // make a copy, gui_write() may try to change it
765 p = vim_strsave((char_u *)_("Reading from stdin..."));
766 if (p != NULL)
767 {
768 gui_write(p, (int)STRLEN(p));
769 vim_free(p);
770 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
775 else if (!read_buffer)
776 filemess(curbuf, sfname, (char_u *)"", 0);
777 }
778
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100779 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 /*
782 * Set linecnt now, before the "retry" caused by a wrong guess for
783 * fileformat, and after the autocommands, which may change them.
784 */
785 linecnt = curbuf->b_ml.ml_line_count;
786
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100787 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000788 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000789 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000790 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000791 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000792 curbuf->b_bad_char = eap->bad_char;
793 }
794 else
795 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000798 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 */
800 if (eap != NULL && eap->force_enc != 0)
801 {
802 fenc = enc_canonize(eap->cmd + eap->force_enc);
803 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000804 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 else if (curbuf->b_p_bin)
807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100808 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 fenc_alloced = FALSE;
810 }
811 else if (curbuf->b_help)
812 {
813 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000814 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 // Help files are either utf-8 or latin1. Try utf-8 first, if this
817 // fails it must be latin1.
818 // Always do this when 'encoding' is "utf-8". Otherwise only do
819 // this when needed to avoid [converted] remarks all the time.
820 // It is needed when the first line contains non-ASCII characters.
821 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 fenc = (char_u *)"latin1";
823 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000824 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000826 fc = fname[STRLEN(fname) - 1];
827 if (TOLOWER_ASC(fc) == 'x')
828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100829 // Read the first line (and a bit more). Immediately rewind to
830 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100831 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200832 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000833 for (p = firstline; p < firstline + len; ++p)
834 if (*p >= 0x80)
835 {
836 c = TRUE;
837 break;
838 }
839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841
842 if (c)
843 {
844 fenc_next = fenc;
845 fenc = (char_u *)"utf-8";
846
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100847 // When the file is utf-8 but a character doesn't fit in
848 // 'encoding' don't retry. In help text editing utf-8 bytes
849 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000850 if (!enc_utf8)
851 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 fenc_alloced = FALSE;
854 }
855 else if (*p_fencs == NUL)
856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 fenc_alloced = FALSE;
859 }
860 else
861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100862 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200863 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 /*
867 * Jump back here to retry reading the file in different ways.
868 * Reasons to retry:
869 * - encoding conversion failed: try another one from "fenc_next"
870 * - BOM detected and fenc was set, need to setup conversion
871 * - "fileformat" check failed: try another
872 *
873 * Variables set for special retry actions:
874 * "file_rewind" Rewind the file to start reading it again.
875 * "advance_fenc" Advance "fenc" using "fenc_next".
876 * "skip_read" Re-use already read bytes (BOM detected).
877 * "did_iconv" iconv() conversion failed, try 'charconvert'.
878 * "keep_fileformat" Don't reset "fileformat".
879 *
880 * Other status indicators:
881 * "tmpname" When != NULL did conversion with 'charconvert'.
882 * Output file has to be deleted afterwards.
883 * "iconv_fd" When != -1 did conversion with iconv().
884 */
885retry:
886
887 if (file_rewind)
888 {
889 if (read_buffer)
890 {
891 read_buf_lnum = 1;
892 read_buf_col = 0;
893 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200894 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100896 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 error = TRUE;
898 goto failed;
899 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100900 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200902 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000904 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000907 curbuf->b_start_bomb = FALSE;
908 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000909 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911
912 /*
913 * When retrying with another "fenc" and the first time "fileformat"
914 * will be reset.
915 */
916 if (keep_fileformat)
917 keep_fileformat = FALSE;
918 else
919 {
920 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000923 try_unix = try_dos = try_mac = FALSE;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100926 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100928 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100930 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
932
Bram Moolenaar13505972019-01-24 15:04:48 +0100933#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (iconv_fd != (iconv_t)-1)
935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100936 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 iconv_close(iconv_fd);
938 iconv_fd = (iconv_t)-1;
939 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942 if (advance_fenc)
943 {
944 /*
945 * Try the next entry in 'fileencodings'.
946 */
947 advance_fenc = FALSE;
948
949 if (eap != NULL && eap->force_enc != 0)
950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100951 // Conversion given with "++cc=" wasn't possible, read
952 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000954 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (fenc_alloced)
956 vim_free(fenc);
957 fenc = (char_u *)"";
958 fenc_alloced = FALSE;
959 }
960 else
961 {
962 if (fenc_alloced)
963 vim_free(fenc);
964 if (fenc_next != NULL)
965 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200966 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 else
969 {
970 fenc = (char_u *)"";
971 fenc_alloced = FALSE;
972 }
973 }
974 if (tmpname != NULL)
975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100977 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979 }
980
981 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000982 * Conversion may be required when the encoding of the file is different
983 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 */
985 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000986 converted = need_conversion(fenc);
987 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
989
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 // "ucs-bom" means we need to check the first bytes of the file
991 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (STRCMP(fenc, ENC_UCSBOM) == 0)
993 fio_flags = FIO_UCSBOM;
994
995 /*
996 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
997 * done. This is handled below after read(). Prepare the
998 * fio_flags to avoid having to parse the string each time.
999 * Also check for Unicode to Latin1 conversion, because iconv()
1000 * appears not to handle this correctly. This works just like
1001 * conversion to UTF-8 except how the resulting character is put in
1002 * the buffer.
1003 */
1004 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1005 fio_flags = get_fio_flags(fenc);
1006
Bram Moolenaar4f974752019-02-17 17:44:42 +01001007#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 /*
1009 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1010 * is handled with MultiByteToWideChar().
1011 */
1012 if (fio_flags == 0)
1013 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar13505972019-01-24 15:04:48 +01001016#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001017 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (fio_flags == 0)
1019 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
Bram Moolenaar13505972019-01-24 15:04:48 +01001022#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /*
1024 * Try using iconv() if we can't convert internally.
1025 */
1026 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001027# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 )
1031 iconv_fd = (iconv_t)my_iconv_open(
1032 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar13505972019-01-24 15:04:48 +01001035#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 /*
1037 * Use the 'charconvert' expression when conversion is required
1038 * and we can't do it internally or with iconv().
1039 */
1040 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001041 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001042# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 )
1046 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001047# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001049# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // Skip conversion when it's already done (retry for wrong
1051 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (tmpname == NULL)
1053 {
1054 tmpname = readfile_charconvert(fname, fenc, &fd);
1055 if (tmpname == NULL)
1056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 advance_fenc = TRUE;
1059 if (fd < 0)
1060 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001061 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001062 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 error = TRUE;
1064 goto failed;
1065 }
1066 goto retry;
1067 }
1068 }
1069 }
1070 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001074#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 )
1078 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 // Conversion wanted but we can't.
1080 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 advance_fenc = TRUE;
1082 goto retry;
1083 }
1084 }
1085 }
1086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Set "can_retry" when it's possible to rewind the file and try with
1088 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1089 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001090 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 if (!skip_read)
1093 {
1094 linerest = 0;
1095 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001096 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 skip_count = lines_to_skip;
1098 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001100#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001101 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1102 && curbuf->b_ffname != NULL
1103 && curbuf->b_p_udf
1104 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001105 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001106 && !read_stdin
1107 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001108 if (read_undo_file)
1109 sha256_start(&sha_ctx);
1110#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001111#ifdef FEAT_CRYPT
1112 if (curbuf->b_cryptstate != NULL)
1113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001114 // Need to free the state, but keep the key, don't want to ask for
1115 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001116 crypt_free_state(curbuf->b_cryptstate);
1117 curbuf->b_cryptstate = NULL;
1118 }
1119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
1121
1122 while (!error && !got_int)
1123 {
1124 /*
1125 * We allocate as much space for the file as we can get, plus
1126 * space for the old line plus room for one terminating NUL.
1127 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001128 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001130 if (!skip_read)
1131 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001132#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001133 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001134#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135 // Use buffer >= 64K. Add linerest to double the size if the
1136 // line gets very long, to avoid a lot of copying. But don't
1137 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001138 size = 0x10000L + linerest;
1139 if (size > 0x100000L)
1140 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001141#endif
1142 }
1143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001145 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
1147 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001148 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 size = 1;
1150 }
1151 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 if (!skip_read)
1154 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001155 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001157 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 FALSE)) != NULL)
1159 break;
1160 }
1161 if (new_buffer == NULL)
1162 {
1163 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1164 error = TRUE;
1165 break;
1166 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001167 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1169 vim_free(buffer);
1170 buffer = new_buffer;
1171 ptr = buffer + linerest;
1172 line_start = buffer;
1173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001174 // May need room to translate into.
1175 // For iconv() we don't really know the required space, use a
1176 // factor ICONV_MULT.
1177 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1178 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1179 // become up to 4 bytes, size must be multiple of 2
1180 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1181 // multiple of 2
1182 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1183 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001184 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001185#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (iconv_fd != (iconv_t)-1)
1187 size = size / ICONV_MULT;
1188 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (fio_flags & FIO_LATIN1)
1191 size = size / 2;
1192 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1193 size = (size * 2 / 3) & ~1;
1194 else if (fio_flags & FIO_UCS4)
1195 size = (size * 2 / 3) & ~3;
1196 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001197 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001198#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001200 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001201#endif
1202#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001204 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (conv_restlen > 0)
1208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001209 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 mch_memmove(ptr, conv_rest, conv_restlen);
1211 ptr += conv_restlen;
1212 size -= conv_restlen;
1213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 if (read_buffer)
1216 {
1217 /*
1218 * Read bytes from curbuf. Used for converting text read
1219 * from stdin.
1220 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001221 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (read_buf_lnum > from)
1223 size = 0;
1224 else
1225 {
1226 int n, ni;
1227 long tlen;
1228
1229 tlen = 0;
1230 for (;;)
1231 {
1232 p = ml_get(read_buf_lnum) + read_buf_col;
1233 n = (int)STRLEN(p);
1234 if ((int)tlen + n + 1 > size)
1235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001236 // Filled up to "size", append partial line.
1237 // Change NL to NUL to reverse the effect done
1238 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001239 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 for (ni = 0; ni < n; ++ni)
1241 {
1242 if (p[ni] == NL)
1243 ptr[tlen++] = NUL;
1244 else
1245 ptr[tlen++] = p[ni];
1246 }
1247 read_buf_col += n;
1248 break;
1249 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001250
1251 // Append whole line and new-line. Change NL
1252 // to NUL to reverse the effect done below.
1253 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001255 if (p[ni] == NL)
1256 ptr[tlen++] = NUL;
1257 else
1258 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001260 ptr[tlen++] = NL;
1261 read_buf_col = 0;
1262 if (++read_buf_lnum > from)
1263 {
1264 // When the last line didn't have an
1265 // end-of-line don't add it now either.
1266 if (!curbuf->b_p_eol)
1267 --tlen;
1268 size = tlen;
1269 eof = TRUE;
1270 break;
1271 }
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 }
1275 }
1276 else
1277 {
1278 /*
1279 * Read bytes from the file.
1280 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001281# ifdef FEAT_SODIUM
1282 // Let the crypt layer work with a buffer size of 8192
1283 if (filesize == 0)
1284 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001285 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001286 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1287 + crypto_secretstream_xchacha20poly1305_ABYTES;
1288
1289 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1290 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1291 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1292# endif
1293 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001294 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001295 filesize_count += size;
1296 // hit end of file
1297 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001300#ifdef FEAT_CRYPT
1301 /*
1302 * At start of file: Check for magic number of encryption.
1303 */
1304 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001305 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001306 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1307 &filesize, newfile, sfname,
1308 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001309# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001310 if (curbuf->b_cryptstate != NULL
1311 && !crypt_works_inplace(curbuf->b_cryptstate))
1312 // reading undo file requires crypt_decode_inplace()
1313 read_undo_file = FALSE;
1314# endif
1315 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001316 /*
1317 * Decrypt the read bytes. This is done before checking for
1318 * EOF because the crypt layer may be buffering.
1319 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001320 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1321 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001322 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001323# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001324 if (crypt_works_inplace(curbuf->b_cryptstate))
1325 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001326# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001327 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1328 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001329# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001330 }
1331 else
1332 {
1333 char_u *newptr = NULL;
1334 int decrypted_size;
1335
1336 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001337 curbuf->b_cryptstate, ptr, size,
1338 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001339
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001340 if (decrypted_size < 0)
1341 {
1342 // error message already given
1343 error = TRUE;
1344 vim_free(newptr);
1345 break;
1346 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001347 // If the crypt layer is buffering, not producing
1348 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001349 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001350 continue;
1351
1352 if (linerest == 0)
1353 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001354 // Simple case: reuse returned buffer (may be
1355 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001356 new_buffer = newptr;
1357 }
1358 else
1359 {
1360 long_u new_size;
1361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001362 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001363 new_size = (long_u)(decrypted_size + linerest + 1);
1364 new_buffer = lalloc(new_size, FALSE);
1365 if (new_buffer == NULL)
1366 {
1367 do_outofmem_msg(new_size);
1368 error = TRUE;
1369 break;
1370 }
1371
1372 mch_memmove(new_buffer, buffer, linerest);
1373 if (newptr != NULL)
1374 mch_memmove(new_buffer + linerest, newptr,
1375 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001376 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377 }
1378
1379 if (new_buffer != NULL)
1380 {
1381 vim_free(buffer);
1382 buffer = new_buffer;
1383 new_buffer = NULL;
1384 line_start = buffer;
1385 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001386 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001387 }
1388 size = decrypted_size;
1389 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001390# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001391 }
1392#endif
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (size <= 0)
1395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001396 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001399 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001400 /*
1401 * Reached end-of-file but some trailing bytes could
1402 * not be converted. Truncated file?
1403 */
1404
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001405 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001406 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001407#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001408 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001409#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001410 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001411 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001412 if (can_retry)
1413 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001414 if (conv_error == 0)
1415 conv_error = curbuf->b_ml.ml_line_count
1416 - linecnt + 1;
1417 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001418 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001419 else if (illegal_byte == 0)
1420 illegal_byte = curbuf->b_ml.ml_line_count
1421 - linecnt + 1;
1422 if (bad_char_behavior == BAD_DROP)
1423 {
1424 *(ptr - conv_restlen) = NUL;
1425 conv_restlen = 0;
1426 }
1427 else
1428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 // Replace the trailing bytes with the replacement
1430 // character if we were converting; if we weren't,
1431 // leave the UTF8 checking code to do it, as it
1432 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001433 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001434#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001435 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001436#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001437 ))
1438 {
1439 while (conv_restlen > 0)
1440 {
1441 *(--ptr) = bad_char_behavior;
1442 --conv_restlen;
1443 }
1444 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001445 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001446#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001447 if (iconv_fd != (iconv_t)-1)
1448 {
1449 iconv_close(iconv_fd);
1450 iconv_fd = (iconv_t)-1;
1451 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001452#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001453 }
1454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 skip_read = FALSE;
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 /*
1460 * At start of file (or after crypt magic number): Check for BOM.
1461 * Also check for a BOM for other Unicode encodings, but not after
1462 * converting with 'charconvert' or when a BOM has already been
1463 * found.
1464 */
1465 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001466#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001467 || (cryptkey != NULL
1468 && filesize == crypt_get_header_len(
1469 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 )
1472 && (fio_flags == FIO_UCSBOM
1473 || (!curbuf->b_p_bomb
1474 && tmpname == NULL
1475 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1476 {
1477 char_u *ccname;
1478 int blen;
1479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001480 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (size < 2 || curbuf->b_p_bin)
1482 ccname = NULL;
1483 else
1484 ccname = check_for_bom(ptr, size, &blen,
1485 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1486 if (ccname != NULL)
1487 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001488 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 filesize += blen;
1490 size -= blen;
1491 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001492 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001495 curbuf->b_start_bomb = TRUE;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498
1499 if (fio_flags == FIO_UCSBOM)
1500 {
1501 if (ccname == NULL)
1502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001503 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 advance_fenc = TRUE;
1505 }
1506 else
1507 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001508 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (fenc_alloced)
1510 vim_free(fenc);
1511 fenc = ccname;
1512 fenc_alloced = FALSE;
1513 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 skip_read = TRUE;
1516 goto retry;
1517 }
1518 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001521 ptr -= conv_restlen;
1522 size += conv_restlen;
1523 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 /*
1525 * Break here for a read error or end-of-file.
1526 */
1527 if (size <= 0)
1528 break;
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
Bram Moolenaar13505972019-01-24 15:04:48 +01001531#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (iconv_fd != (iconv_t)-1)
1533 {
1534 /*
1535 * Attempt conversion of the read bytes to 'encoding' using
1536 * iconv().
1537 */
1538 const char *fromp;
1539 char *top;
1540 size_t from_size;
1541 size_t to_size;
1542
1543 fromp = (char *)ptr;
1544 from_size = size;
1545 ptr += size;
1546 top = (char *)ptr;
1547 to_size = real_size - size;
1548
1549 /*
1550 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001551 * another conversion. Except for when there is no
1552 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001554 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1555 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1557 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001558 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001559 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001560 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001561 if (conv_error == 0)
1562 conv_error = readfile_linenr(linecnt,
1563 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001565 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001566 ++fromp;
1567 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001568 if (bad_char_behavior == BAD_KEEP)
1569 {
1570 *top++ = *(fromp - 1);
1571 --to_size;
1572 }
1573 else if (bad_char_behavior != BAD_DROP)
1574 {
1575 *top++ = bad_char_behavior;
1576 --to_size;
1577 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 if (from_size > 0)
1581 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001582 // Some remaining characters, keep them for the next
1583 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1585 conv_restlen = (int)from_size;
1586 }
1587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001588 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 line_start = ptr - linerest;
1590 mch_memmove(line_start, buffer, (size_t)linerest);
1591 size = (long)((char_u *)top - ptr);
1592 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Bram Moolenaar4f974752019-02-17 17:44:42 +01001595#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (fio_flags & FIO_CODEPAGE)
1597 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001598 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001599 WCHAR ucs2buf[3];
1600 int ucs2len;
1601 int codepage = FIO_GET_CP(fio_flags);
1602 int bytelen;
1603 int found_bad;
1604 char replstr[2];
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 /*
1607 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001608 * a codepage, using standard MS-Windows functions. This
1609 * requires two steps:
1610 * 1. convert from 'fileencoding' to ucs-2
1611 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001613 * Because there may be illegal bytes AND an incomplete byte
1614 * sequence at the end, we may have to do the conversion one
1615 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001618 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001619 if (bad_char_behavior > 0)
1620 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001622 replstr[0] = '?';
1623 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
1625 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001626 * Move the bytes to the end of the buffer, so that we have
1627 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001629 src = ptr + real_size - size;
1630 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001632 /*
1633 * Do the conversion.
1634 */
1635 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001636 while (size > 0)
1637 {
1638 found_bad = FALSE;
1639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001641 if (codepage == CP_UTF8)
1642 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001643 // Handle CP_UTF8 input ourselves to be able to handle
1644 // trailing bytes properly.
1645 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001646 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001647 if (bytelen > size)
1648 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001649 // Only got some bytes of a character. Normally
1650 // it's put in "conv_rest", but if it's too long
1651 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001652 if (bytelen <= CONV_RESTLEN)
1653 break;
1654
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001655 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001656 bytelen = size;
1657 found_bad = TRUE;
1658 }
1659 else
1660 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001661 int u8c = utf_ptr2char(src);
1662
Bram Moolenaar86e01082005-12-29 22:45:34 +00001663 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001664 found_bad = TRUE;
1665 ucs2buf[0] = u8c;
1666 ucs2len = 1;
1667 }
1668 }
1669 else
1670# endif
1671 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001672 // We don't know how long the byte sequence is, try
1673 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001674 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1675 ++bytelen)
1676 {
1677 ucs2len = MultiByteToWideChar(codepage,
1678 MB_ERR_INVALID_CHARS,
1679 (LPCSTR)src, bytelen,
1680 ucs2buf, 3);
1681 if (ucs2len > 0)
1682 break;
1683 }
1684 if (ucs2len == 0)
1685 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001686 // If we have only one byte then it's probably an
1687 // incomplete byte sequence. Otherwise discard
1688 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001689 if (size == 1)
1690 break;
1691 found_bad = TRUE;
1692 bytelen = 1;
1693 }
1694 }
1695
1696 if (!found_bad)
1697 {
1698 int i;
1699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001700 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001701 if (enc_utf8)
1702 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001703 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001704 for (i = 0; i < ucs2len; ++i)
1705 dst += utf_char2bytes(ucs2buf[i], dst);
1706 }
1707 else
1708 {
1709 BOOL bad = FALSE;
1710 int dstlen;
1711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001712 // From UCS-2 to "enc_codepage". If the
1713 // conversion uses the default character "?",
1714 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001715 dstlen = WideCharToMultiByte(enc_codepage, 0,
1716 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001717 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001718 replstr, &bad);
1719 if (bad)
1720 found_bad = TRUE;
1721 else
1722 dst += dstlen;
1723 }
1724 }
1725
1726 if (found_bad)
1727 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001728 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001729 if (can_retry)
1730 goto rewind_retry;
1731 if (conv_error == 0)
1732 conv_error = readfile_linenr(linecnt, ptr, dst);
1733 if (bad_char_behavior != BAD_DROP)
1734 {
1735 if (bad_char_behavior == BAD_KEEP)
1736 {
1737 mch_memmove(dst, src, bytelen);
1738 dst += bytelen;
1739 }
1740 else
1741 *dst++ = bad_char_behavior;
1742 }
1743 }
1744
1745 src += bytelen;
1746 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001748
1749 if (size > 0)
1750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001752 mch_memmove(conv_rest, src, size);
1753 conv_restlen = size;
1754 }
1755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001757 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001760#endif
1761#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (fio_flags & FIO_MACROMAN)
1763 {
1764 /*
1765 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001766 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001768 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
1771 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (fio_flags != 0)
1774 {
1775 int u8c;
1776 char_u *dest;
1777 char_u *tail = NULL;
1778
1779 /*
1780 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1781 * "enc_utf8" not set: Convert Unicode to Latin1.
1782 * Go from end to start through the buffer, because the number
1783 * of bytes may increase.
1784 * "dest" points to after where the UTF-8 bytes go, "p" points
1785 * to after the next character to convert.
1786 */
1787 dest = ptr + real_size;
1788 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1789 {
1790 p = ptr + size;
1791 if (fio_flags == FIO_UTF8)
1792 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001793 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 tail = ptr + size - 1;
1795 while (tail > ptr && (*tail & 0xc0) == 0x80)
1796 --tail;
1797 if (tail + utf_byte2len(*tail) <= ptr + size)
1798 tail = NULL;
1799 else
1800 p = tail;
1801 }
1802 }
1803 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001805 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 p = ptr + (size & ~1);
1807 if (size & 1)
1808 tail = p;
1809 if ((fio_flags & FIO_UTF16) && p > ptr)
1810 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001811 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (fio_flags & FIO_ENDIAN_L)
1813 {
1814 u8c = (*--p << 8);
1815 u8c += *--p;
1816 }
1817 else
1818 {
1819 u8c = *--p;
1820 u8c += (*--p << 8);
1821 }
1822 if (u8c >= 0xd800 && u8c <= 0xdbff)
1823 tail = p;
1824 else
1825 p += 2;
1826 }
1827 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001828 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001830 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 p = ptr + (size & ~3);
1832 if (size & 3)
1833 tail = p;
1834 }
1835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // If there is a trailing incomplete sequence move it to
1837 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (tail != NULL)
1839 {
1840 conv_restlen = (int)((ptr + size) - tail);
1841 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1842 size -= conv_restlen;
1843 }
1844
1845
1846 while (p > ptr)
1847 {
1848 if (fio_flags & FIO_LATIN1)
1849 u8c = *--p;
1850 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1851 {
1852 if (fio_flags & FIO_ENDIAN_L)
1853 {
1854 u8c = (*--p << 8);
1855 u8c += *--p;
1856 }
1857 else
1858 {
1859 u8c = *--p;
1860 u8c += (*--p << 8);
1861 }
1862 if ((fio_flags & FIO_UTF16)
1863 && u8c >= 0xdc00 && u8c <= 0xdfff)
1864 {
1865 int u16c;
1866
1867 if (p == ptr)
1868 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001869 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (can_retry)
1871 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001872 if (conv_error == 0)
1873 conv_error = readfile_linenr(linecnt,
1874 ptr, p);
1875 if (bad_char_behavior == BAD_DROP)
1876 continue;
1877 if (bad_char_behavior != BAD_KEEP)
1878 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001881 // found second word of double-word, get the first
1882 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (fio_flags & FIO_ENDIAN_L)
1884 {
1885 u16c = (*--p << 8);
1886 u16c += *--p;
1887 }
1888 else
1889 {
1890 u16c = *--p;
1891 u16c += (*--p << 8);
1892 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001893 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1894 + (u8c & 0x3ff);
1895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001896 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (u16c < 0xd800 || u16c > 0xdbff)
1898 {
1899 if (can_retry)
1900 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001901 if (conv_error == 0)
1902 conv_error = readfile_linenr(linecnt,
1903 ptr, p);
1904 if (bad_char_behavior == BAD_DROP)
1905 continue;
1906 if (bad_char_behavior != BAD_KEEP)
1907 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911 else if (fio_flags & FIO_UCS4)
1912 {
1913 if (fio_flags & FIO_ENDIAN_L)
1914 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001915 u8c = (unsigned)*--p << 24;
1916 u8c += (unsigned)*--p << 16;
1917 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 u8c += *--p;
1919 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001920 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
1922 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001923 u8c += (unsigned)*--p << 8;
1924 u8c += (unsigned)*--p << 16;
1925 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
1927 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 if (*--p < 0x80)
1931 u8c = *p;
1932 else
1933 {
1934 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001935 p -= len;
1936 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (len == 0)
1938 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001939 // Not a valid UTF-8 character, retry with
1940 // another fenc when possible, otherwise just
1941 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (can_retry)
1943 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001944 if (conv_error == 0)
1945 conv_error = readfile_linenr(linecnt,
1946 ptr, p);
1947 if (bad_char_behavior == BAD_DROP)
1948 continue;
1949 if (bad_char_behavior != BAD_KEEP)
1950 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 dest -= utf_char2len(u8c);
1957 (void)utf_char2bytes(u8c, dest);
1958 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001959 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 --dest;
1962 if (u8c >= 0x100)
1963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 // character doesn't fit in latin1, retry with
1965 // another fenc when possible, otherwise just
1966 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001967 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001969 if (conv_error == 0)
1970 conv_error = readfile_linenr(linecnt, ptr, p);
1971 if (bad_char_behavior == BAD_DROP)
1972 ++dest;
1973 else if (bad_char_behavior == BAD_KEEP)
1974 *dest = u8c;
1975 else if (eap != NULL && eap->bad_char != 0)
1976 *dest = bad_char_behavior;
1977 else
1978 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980 else
1981 *dest = u8c;
1982 }
1983 }
1984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 line_start = dest - linerest;
1987 mch_memmove(line_start, buffer, (size_t)linerest);
1988 size = (long)((ptr + real_size) - dest);
1989 ptr = dest;
1990 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001991 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001993 int incomplete_tail = FALSE;
1994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001995 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001996 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001998 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001999 int l;
2000
2001 if (todo <= 0)
2002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (*p >= 0x80)
2004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002005 // A length of 1 means it's an illegal byte. Accept
2006 // an incomplete character at the end though, the next
2007 // read() will get the next bytes, we'll check it
2008 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002009 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002010 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // Avoid retrying with a different encoding when
2013 // a truncated file is more likely, or attempting
2014 // to read the rest of an incomplete sequence when
2015 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002016 if (p > ptr || filesize > 0)
2017 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 // Incomplete byte sequence, move it to conv_rest[]
2019 // and try to read the rest of it, unless we've
2020 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002021 if (p > ptr)
2022 {
2023 conv_restlen = todo;
2024 mch_memmove(conv_rest, p, conv_restlen);
2025 size -= conv_restlen;
2026 break;
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002029 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002031 // Illegal byte. If we can try another encoding
2032 // do that, unless at EOF where a truncated
2033 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002034 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002035 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002036#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002037 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002038 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2039 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002040#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002041 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002042 if (conv_error == 0 && illegal_byte == 0)
2043 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002045 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002046 if (bad_char_behavior == BAD_DROP)
2047 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002048 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002049 --p;
2050 --size;
2051 }
2052 else if (bad_char_behavior != BAD_KEEP)
2053 *p = bad_char_behavior;
2054 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002055 else
2056 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002059 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002064#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002065 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002067 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002069#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002071 advance_fenc = TRUE;
2072 file_rewind = TRUE;
2073 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002077 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 filesize += size;
2079
2080 /*
2081 * when reading the first part of a file: guess EOL type
2082 */
2083 if (fileformat == EOL_UNKNOWN)
2084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (try_dos || try_unix)
2087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002089 if (try_mac)
2090 try_mac = 1;
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 for (p = ptr; p < ptr + size; ++p)
2093 {
2094 if (*p == NL)
2095 {
2096 if (!try_unix
2097 || (try_dos && p > ptr && p[-1] == CAR))
2098 fileformat = EOL_DOS;
2099 else
2100 fileformat = EOL_UNIX;
2101 break;
2102 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002103 else if (*p == CAR && try_mac)
2104 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (fileformat == EOL_UNIX && try_mac)
2109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 try_mac = 1;
2112 try_unix = 1;
2113 for (; p >= ptr && *p != CAR; p--)
2114 ;
2115 if (p >= ptr)
2116 {
2117 for (p = ptr; p < ptr + size; ++p)
2118 {
2119 if (*p == NL)
2120 try_unix++;
2121 else if (*p == CAR)
2122 try_mac++;
2123 }
2124 if (try_mac > try_unix)
2125 fileformat = EOL_MAC;
2126 }
2127 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002128 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 // Looking for CR but found no end-of-line markers at
2130 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002131 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (fileformat == EOL_UNKNOWN && try_mac)
2136 fileformat = EOL_MAC;
2137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (fileformat == EOL_UNKNOWN)
2140 fileformat = default_fileformat();
2141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002142 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 set_fileformat(fileformat, OPT_LOCAL);
2145 }
2146 }
2147
2148 /*
2149 * This loop is executed once for every character read.
2150 * Keep it fast!
2151 */
2152 if (fileformat == EOL_MAC)
2153 {
2154 --ptr;
2155 while (++ptr, --size >= 0)
2156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002157 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if ((c = *ptr) != NUL && c != CAR && c != NL)
2159 continue;
2160 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 else
2165 {
2166 if (skip_count == 0)
2167 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002168 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 len = (colnr_T) (ptr - line_start + 1);
2170 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2171 {
2172 error = TRUE;
2173 break;
2174 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002175#ifdef FEAT_PERSISTENT_UNDO
2176 if (read_undo_file)
2177 sha256_update(&sha_ctx, line_start, len);
2178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 ++lnum;
2180 if (--read_count == 0)
2181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002182 error = TRUE; // break loop
2183 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 break;
2185 }
2186 }
2187 else
2188 --skip_count;
2189 line_start = ptr + 1;
2190 }
2191 }
2192 }
2193 else
2194 {
2195 --ptr;
2196 while (++ptr, --size >= 0)
2197 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002198 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 continue;
2200 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else
2203 {
2204 if (skip_count == 0)
2205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002206 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 len = (colnr_T)(ptr - line_start + 1);
2208 if (fileformat == EOL_DOS)
2209 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002210 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002212 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 ptr[-1] = NUL;
2214 --len;
2215 }
2216 /*
2217 * Reading in Dos format, but no CR-LF found!
2218 * When 'fileformats' includes "unix", delete all
2219 * the lines read so far and start all over again.
2220 * Otherwise give an error message later.
2221 */
2222 else if (ff_error != EOL_DOS)
2223 {
2224 if ( try_unix
2225 && !read_stdin
2226 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002227 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2228 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 set_fileformat(EOL_UNIX, OPT_LOCAL);
2233 file_rewind = TRUE;
2234 keep_fileformat = TRUE;
2235 goto retry;
2236 }
2237 ff_error = EOL_DOS;
2238 }
2239 }
2240 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2241 {
2242 error = TRUE;
2243 break;
2244 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002245#ifdef FEAT_PERSISTENT_UNDO
2246 if (read_undo_file)
2247 sha256_update(&sha_ctx, line_start, len);
2248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 ++lnum;
2250 if (--read_count == 0)
2251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 error = TRUE; // break loop
2253 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 break;
2255 }
2256 }
2257 else
2258 --skip_count;
2259 line_start = ptr + 1;
2260 }
2261 }
2262 }
2263 linerest = (long)(ptr - line_start);
2264 ui_breakcheck();
2265 }
2266
2267failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (error && read_count == 0)
2270 error = FALSE;
2271
2272 /*
2273 * If we get EOF in the middle of a line, note the fact and
2274 * complete the line ourselves.
2275 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2276 */
2277 if (!error
2278 && !got_int
2279 && linerest != 0
2280 && !(!curbuf->b_p_bin
2281 && fileformat == EOL_DOS
2282 && *line_start == Ctrl_Z
2283 && ptr == line_start + 1))
2284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002285 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002286 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 curbuf->b_p_eol = FALSE;
2288 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002289 len = (colnr_T)(ptr - line_start + 1);
2290 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 error = TRUE;
2292 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002293 {
2294#ifdef FEAT_PERSISTENT_UNDO
2295 if (read_undo_file)
2296 sha256_update(&sha_ctx, line_start, len);
2297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002302 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002303 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002306 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002307 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002308 crypt_free_state(curbuf->b_cryptstate);
2309 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002310 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002311 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2312 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // Don't set cryptkey to NULL, it's used below as a flag that
2314 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
2316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002317 // If editing a new file: set 'fenc' for the current buffer.
2318 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002321 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (fenc_alloced)
2323 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002324#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#endif
2328
2329 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002331#ifdef HAVE_FD_CLOEXEC
2332 else
2333 {
2334 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002335
Bram Moolenaarf05da212009-11-17 16:13:15 +00002336 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002337 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002338 }
2339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 vim_free(buffer);
2341
2342#ifdef HAVE_DUP
2343 if (read_stdin)
2344 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002347 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349#endif
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (tmpname != NULL)
2352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002353 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 vim_free(tmpname);
2355 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 /*
2359 * In recovery mode everything but autocommands is skipped.
2360 */
2361 if (!recoverymode)
2362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2365 {
2366#ifdef FEAT_NETBEANS_INTG
2367 netbeansFireChanges = 0;
2368#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002369 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#ifdef FEAT_NETBEANS_INTG
2371 netbeansFireChanges = 1;
2372#endif
2373 --linecnt;
2374 }
2375 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2376 if (filesize == 0)
2377 linecnt = 0;
2378 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002379 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002380 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002381#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 // After reading the text into the buffer the diff info needs to
2383 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002384 diff_invalidate(curbuf);
2385#endif
2386#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002387 // All folds in the window are invalid now. Mark them for update
2388 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002389 foldUpdateAll(curwin);
2390#endif
2391 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002392 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 appended_lines_mark(from, linecnt);
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#ifndef ALWAYS_USE_GUI
2396 /*
2397 * If we were reading from the same terminal as where messages go,
2398 * the screen will have been messed up.
2399 * Switch on raw mode now and clear the screen.
2400 */
2401 if (read_stdin)
2402 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002403 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 starttermcap();
2405 screenclear();
2406 }
2407#endif
2408
2409 if (got_int)
2410 {
2411 if (!(flags & READ_DUMMY))
2412 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002413 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002415 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 msg_scroll = msg_save;
2418#ifdef FEAT_VIMINFO
2419 check_marks_read();
2420#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002421 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423
2424 if (!filtering && !(flags & READ_DUMMY))
2425 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002426 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 c = FALSE;
2428
2429#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 STRCAT(IObuff, _("[fifo]"));
2433 c = TRUE;
2434 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002435 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 STRCAT(IObuff, _("[socket]"));
2438 c = TRUE;
2439 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002440# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002441 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002442 {
2443 STRCAT(IObuff, _("[character special]"));
2444 c = TRUE;
2445 }
2446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#endif
2448 if (curbuf->b_p_ro)
2449 {
2450 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2451 c = TRUE;
2452 }
2453 if (read_no_eol_lnum)
2454 {
2455 msg_add_eol();
2456 c = TRUE;
2457 }
2458 if (ff_error == EOL_DOS)
2459 {
2460 STRCAT(IObuff, _("[CR missing]"));
2461 c = TRUE;
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (split)
2464 {
2465 STRCAT(IObuff, _("[long lines split]"));
2466 c = TRUE;
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (notconverted)
2469 {
2470 STRCAT(IObuff, _("[NOT converted]"));
2471 c = TRUE;
2472 }
2473 else if (converted)
2474 {
2475 STRCAT(IObuff, _("[converted]"));
2476 c = TRUE;
2477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478#ifdef FEAT_CRYPT
2479 if (cryptkey != NULL)
2480 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002481 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 c = TRUE;
2483 }
2484#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002485 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002487 sprintf((char *)IObuff + STRLEN(IObuff),
2488 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 c = TRUE;
2490 }
2491 else if (illegal_byte > 0)
2492 {
2493 sprintf((char *)IObuff + STRLEN(IObuff),
2494 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2495 c = TRUE;
2496 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002497 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 STRCAT(IObuff, _("[READ ERRORS]"));
2500 c = TRUE;
2501 }
2502 if (msg_add_fileformat(fileformat))
2503 c = TRUE;
2504#ifdef FEAT_CRYPT
2505 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002506 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002507 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 else
2509#endif
2510 msg_add_lines(c, (long)linecnt, filesize);
2511
Bram Moolenaard23a8232018-02-10 18:45:26 +01002512 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 msg_scrolled_ign = TRUE;
2514#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002515 // Don't show the message when reading stdin, it would end up in a
2516 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (read_stdin || read_buffer)
2518 p = msg_may_trunc(FALSE, IObuff);
2519 else
2520#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002521 {
2522 if (msg_col > 0)
2523 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002524 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002527 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002528 // Need to repeat the message after redrawing when:
2529 // - When reading from stdin (the screen will be cleared next).
2530 // - When restart_edit is set (otherwise there will be a delay
2531 // before redrawing).
2532 // - When the screen was scrolled but there is no wait-return
2533 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002534 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 msg_scrolled_ign = FALSE;
2536 }
2537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002538 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002540 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002541 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 curbuf->b_p_ro = TRUE;
2543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002544 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 /*
2547 * In Ex mode: cursor at last new line.
2548 * Otherwise: cursor at first new line.
2549 */
2550 if (exmode_active)
2551 curwin->w_cursor.lnum = from + linecnt;
2552 else
2553 curwin->w_cursor.lnum = from + 1;
2554 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002555 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaare1004402020-10-24 20:49:43 +02002557 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002558 {
2559 // Set '[ and '] marks to the newly read lines.
2560 curbuf->b_op_start.lnum = from + 1;
2561 curbuf->b_op_start.col = 0;
2562 curbuf->b_op_end.lnum = from + linecnt;
2563 curbuf->b_op_end.col = 0;
2564 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002565
Bram Moolenaar4f974752019-02-17 17:44:42 +01002566#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002567 /*
2568 * Work around a weird problem: When a file has two links (only
2569 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002570 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002571 * It's correct again after reading the file, thus reset the timestamp
2572 * here.
2573 */
2574 if (newfile && !read_stdin && !read_buffer
2575 && mch_stat((char *)fname, &st) >= 0)
2576 {
2577 buf_store_time(curbuf, &st, fname);
2578 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002579 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002580 }
2581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 msg_scroll = msg_save;
2584
2585#ifdef FEAT_VIMINFO
2586 /*
2587 * Get the marks before executing autocommands, so they can be used there.
2588 */
2589 check_marks_read();
2590#endif
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002593 * We remember if the last line of the read didn't have
2594 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2595 * or writing the read again with 'binary' on. The latter is required
2596 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002598 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002600 // When reloading a buffer put the cursor at the first line that is
2601 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002602 if (flags & READ_KEEP_UNDO)
2603 u_find_first_changed();
2604
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002605#ifdef FEAT_PERSISTENT_UNDO
2606 /*
2607 * When opening a new file locate undo info and read it.
2608 */
2609 if (read_undo_file)
2610 {
2611 char_u hash[UNDO_HASH_SIZE];
2612
2613 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002614 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002615 }
2616#endif
2617
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002618 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 int m = msg_scroll;
2621 int n = msg_scrolled;
2622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002623 // Save the fileformat now, otherwise the buffer will be considered
2624 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002625 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 save_file_ff(curbuf);
2627
2628 /*
2629 * The output from the autocommands should not overwrite anything and
2630 * should not be overwritten: Set msg_scroll, restore its value if no
2631 * output was done.
2632 */
2633 msg_scroll = TRUE;
2634 if (filtering)
2635 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2636 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002637 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2640 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002641 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2642 /*
2643 * EVENT_FILETYPE was not triggered but the buffer already has a
2644 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2645 */
2646 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2647 TRUE, curbuf);
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 else
2650 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2651 FALSE, NULL, eap);
2652 if (msg_scrolled == n)
2653 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002654# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002655 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 if (recoverymode && error)
2661 return FAIL;
2662 return OK;
2663}
2664
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002665#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002666/*
2667 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2668 * which is the name of files used for process substitution output by
2669 * some shells on some operating systems, e.g., bash on SunOS.
2670 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2671 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002672 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002673is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002674{
2675 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2676 && VIM_ISDIGIT(fname[8])
2677 && *skipdigits(fname + 9) == NUL
2678 && (fname[9] != NUL
2679 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2680}
2681#endif
2682
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002683/*
2684 * From the current line count and characters read after that, estimate the
2685 * line number where we are now.
2686 * Used for error messages that include a line number.
2687 */
2688 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002689readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002690 linenr_T linecnt, // line count before reading more bytes
2691 char_u *p, // start of more bytes read
2692 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002693{
2694 char_u *s;
2695 linenr_T lnum;
2696
2697 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2698 for (s = p; s < endp; ++s)
2699 if (*s == '\n')
2700 ++lnum;
2701 return lnum;
2702}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704/*
Rob Pilling8196e942022-02-11 15:12:10 +00002705 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002706 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 * Returns OK or FAIL.
2708 */
2709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002710prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
Bram Moolenaar13505972019-01-24 15:04:48 +01002712 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (eap->cmd == NULL)
2714 return FAIL;
2715
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002716 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2717 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002718 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002719 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002720
2721 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002722 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002723 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return OK;
2725}
2726
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002727/*
2728 * Set default or forced 'fileformat' and 'binary'.
2729 */
2730 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002731set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002732{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002733 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002734 if (set_options)
2735 {
2736 if (eap != NULL && eap->force_ff != 0)
2737 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2738 else if (*p_ffs != NUL)
2739 set_fileformat(default_fileformat(), OPT_LOCAL);
2740 }
2741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002742 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002743 if (eap != NULL && eap->force_bin != 0)
2744 {
2745 int oldval = curbuf->b_p_bin;
2746
2747 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2748 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2749 }
2750}
2751
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002752/*
2753 * Set forced 'fileencoding'.
2754 */
2755 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002756set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002757{
2758 if (eap->force_enc != 0)
2759 {
2760 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2761
2762 if (fenc != NULL)
2763 set_string_option_direct((char_u *)"fenc", -1,
2764 fenc, OPT_FREE|OPT_LOCAL, 0);
2765 vim_free(fenc);
2766 }
2767}
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769/*
2770 * Find next fileencoding to use from 'fileencodings'.
2771 * "pp" points to fenc_next. It's advanced to the next item.
2772 * When there are no more items, an empty string is returned and *pp is set to
2773 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002774 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2775 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 */
2777 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002778next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 char_u *p;
2781 char_u *r;
2782
Bram Moolenaarf077db22019-08-13 00:18:24 +02002783 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (**pp == NUL)
2785 {
2786 *pp = NULL;
2787 return (char_u *)"";
2788 }
2789 p = vim_strchr(*pp, ',');
2790 if (p == NULL)
2791 {
2792 r = enc_canonize(*pp);
2793 *pp += STRLEN(*pp);
2794 }
2795 else
2796 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002797 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 *pp = p + 1;
2799 if (r != NULL)
2800 {
2801 p = enc_canonize(r);
2802 vim_free(r);
2803 r = p;
2804 }
2805 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002806 if (r != NULL)
2807 *alloced = TRUE;
2808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002810 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 r = (char_u *)"";
2812 *pp = NULL;
2813 }
2814 return r;
2815}
2816
Bram Moolenaar13505972019-01-24 15:04:48 +01002817#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818/*
2819 * Convert a file with the 'charconvert' expression.
2820 * This closes the file which is to be read, converts it and opens the
2821 * resulting file for reading.
2822 * Returns name of the resulting converted file (the caller should delete it
2823 * after reading it).
2824 * Returns NULL if the conversion failed ("*fdp" is not set) .
2825 */
2826 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002827readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002828 char_u *fname, // name of input file
2829 char_u *fenc, // converted from
2830 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002833 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002835 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002837 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else
2839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002840 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 *fdp = -1;
2842 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2843 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002844 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2846 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002847 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
2850 if (errmsg != NULL)
2851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 // Don't use emsg(), it breaks mappings, the retry with
2853 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002854 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (tmpname != NULL)
2856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002858 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 }
2861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002862 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (*fdp < 0)
2864 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2865
2866 return tmpname;
2867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#endif
2869
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002870#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002872 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2874 * *filesizep are updated.
2875 * Return the (new) encryption key, NULL for no encryption.
2876 */
2877 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002878check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 char_u *cryptkey, // previous encryption key or NULL
2880 char_u *ptr, // pointer to read bytes
2881 long *sizep, // length of read bytes
2882 off_T *filesizep, // nr of bytes used from file
2883 int newfile, // editing a new buffer
2884 char_u *fname, // file name to display
2885 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002887 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002888 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002889
2890 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002892 // Mark the buffer as read-only until the decryption has taken place.
2893 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002894 curbuf->b_p_ro = TRUE;
2895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002896 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002897 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002898 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
2900 if (*curbuf->b_p_key)
2901 cryptkey = curbuf->b_p_key;
2902 else
2903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002904 // When newfile is TRUE, store the typed key in the 'key'
2905 // option and don't free it. bf needs hash of the key saved.
2906 // Don't ask for the key again when first time Enter was hit.
2907 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002908 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002909 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002910 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002911 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002912 *did_ask = TRUE;
2913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002914 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (cryptkey != NULL && *cryptkey == NUL)
2916 {
2917 if (cryptkey != curbuf->b_p_key)
2918 vim_free(cryptkey);
2919 cryptkey = NULL;
2920 }
2921 }
2922 }
2923
2924 if (cryptkey != NULL)
2925 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002926 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002927
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002928 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002929 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002930 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002931 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002932
2933 curbuf->b_cryptstate = crypt_create_from_header(
2934 method, cryptkey, ptr);
2935 crypt_set_cm_option(curbuf, method);
2936
2937 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002938 *filesizep += header_len;
2939 *sizep -= header_len;
2940 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2941
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002942 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002943 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002946 // When starting to edit a new file which does not have encryption, clear
2947 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002948 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01002949 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 return cryptkey;
2952}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002953#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002956 * Return TRUE if a file appears to be read-only from the file permissions.
2957 */
2958 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002959check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002960 char_u *fname, // full path to file
2961 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002962{
2963#ifndef USE_MCH_ACCESS
2964 int fd = 0;
2965#endif
2966
2967 return (
2968#ifdef USE_MCH_ACCESS
2969# ifdef UNIX
2970 (perm & 0222) == 0 ||
2971# endif
2972 mch_access((char *)fname, W_OK)
2973#else
2974 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2975 ? TRUE : (close(fd), FALSE)
2976#endif
2977 );
2978}
2979
Bram Moolenaara7870192019-02-14 12:56:36 +01002980#if defined(HAVE_FSYNC) || defined(PROTO)
2981/*
2982 * Call fsync() with Mac-specific exception.
2983 * Return fsync() result: zero for success.
2984 */
2985 int
2986vim_fsync(int fd)
2987{
2988 int r;
2989
2990# ifdef MACOS_X
2991 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002992 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002993# endif
2994 r = fsync(fd);
2995 return r;
2996}
2997#endif
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003000 * Set the name of the current buffer. Use when the buffer doesn't have a
3001 * name and a ":r" or ":w" command with a file name is used.
3002 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003004set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003005{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003006 buf_T *buf = curbuf;
3007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003008 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003009 if (curbuf->b_p_bl)
3010 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3011 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003012#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003014 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003015#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003016 if (curbuf != buf)
3017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003018 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003019 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003020 return FAIL;
3021 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003022
3023 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3024 curbuf->b_flags |= BF_NOTEDITED;
3025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003026 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003027 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3028 if (curbuf->b_p_bl)
3029 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003030#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003031 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003032 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003033#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003035 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003036 if (*curbuf->b_p_ft == NUL)
3037 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003038 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003039 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003040 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003041 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003042
3043 return OK;
3044}
3045
3046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 * Put file name into IObuff with quotes.
3048 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (fname == NULL)
3053 fname = (char_u *)"-stdin-";
3054 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3055 IObuff[0] = '"';
3056 STRCAT(IObuff, "\" ");
3057}
3058
3059/*
3060 * Append message for text mode to IObuff.
3061 * Return TRUE if something appended.
3062 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003063 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003064msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066#ifndef USE_CRNL
3067 if (eol_type == EOL_DOS)
3068 {
3069 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3070 return TRUE;
3071 }
3072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (eol_type == EOL_MAC)
3074 {
3075 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3076 return TRUE;
3077 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003078#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (eol_type == EOL_UNIX)
3080 {
3081 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3082 return TRUE;
3083 }
3084#endif
3085 return FALSE;
3086}
3087
3088/*
3089 * Append line and character count to IObuff.
3090 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003092msg_add_lines(
3093 int insert_space,
3094 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003095 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 char_u *p;
3098
3099 p = IObuff + STRLEN(IObuff);
3100
3101 if (insert_space)
3102 *p++ = ' ';
3103 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003104 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003105 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 else
3107 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003108 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003110 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003111 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003112 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
3114}
3115
3116/*
3117 * Append message for missing line separator to IObuff.
3118 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003119 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003120msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3123}
3124
Bram Moolenaar473952e2019-09-28 16:30:04 +02003125 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003126time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
ichizokdef69df2021-10-15 17:23:12 +01003128 return
3129#ifdef ST_MTIM_NSEC
3130 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
ichizokdef69df2021-10-15 17:23:12 +01003132#if defined(__linux__) || defined(MSWIN)
3133 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3134 // the seconds. Since the roundoff is done when flushing the inode, the
3135 // time may change unexpectedly by one second!!!
3136 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3137#else
3138 (long)st->st_mtime != mtime
3139#endif
3140 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141}
3142
3143/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003144 * Return TRUE if file encoding "fenc" requires conversion from or to
3145 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003147 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003150 int same_encoding;
3151 int enc_flags;
3152 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003154 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003155 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003156 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003157 fenc_flags = 0;
3158 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003159 else
3160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003161 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3162 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003163 enc_flags = get_fio_flags(p_enc);
3164 fenc_flags = get_fio_flags(fenc);
3165 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3166 }
3167 if (same_encoding)
3168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003169 // Specified encoding matches with 'encoding'. This requires
3170 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003171 return enc_unicode != 0;
3172 }
3173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003174 // Encodings differ. However, conversion is not needed when 'enc' is any
3175 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003176 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177}
3178
3179/*
3180 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3181 * internal conversion.
3182 * if "ptr" is an empty string, use 'encoding'.
3183 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003184 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003185get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 int prop;
3188
3189 if (*ptr == NUL)
3190 ptr = p_enc;
3191
3192 prop = enc_canon_props(ptr);
3193 if (prop & ENC_UNICODE)
3194 {
3195 if (prop & ENC_2BYTE)
3196 {
3197 if (prop & ENC_ENDIAN_L)
3198 return FIO_UCS2 | FIO_ENDIAN_L;
3199 return FIO_UCS2;
3200 }
3201 if (prop & ENC_4BYTE)
3202 {
3203 if (prop & ENC_ENDIAN_L)
3204 return FIO_UCS4 | FIO_ENDIAN_L;
3205 return FIO_UCS4;
3206 }
3207 if (prop & ENC_2WORD)
3208 {
3209 if (prop & ENC_ENDIAN_L)
3210 return FIO_UTF16 | FIO_ENDIAN_L;
3211 return FIO_UTF16;
3212 }
3213 return FIO_UTF8;
3214 }
3215 if (prop & ENC_LATIN1)
3216 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003217 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 return 0;
3219}
3220
Bram Moolenaar473952e2019-09-28 16:30:04 +02003221#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222/*
3223 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3224 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3225 * Used for conversion between 'encoding' and 'fileencoding'.
3226 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003227 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003228get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 int cp;
3231
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003232 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (!enc_utf8 && enc_codepage <= 0)
3234 return 0;
3235
3236 cp = encname2codepage(ptr);
3237 if (cp == 0)
3238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003239# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (STRCMP(ptr, "utf-8") == 0)
3241 cp = CP_UTF8;
3242 else
3243# endif
3244 return 0;
3245 }
3246 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3247}
3248#endif
3249
Bram Moolenaar473952e2019-09-28 16:30:04 +02003250#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251/*
3252 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3253 * needed for the internal conversion to/from utf-8 or latin1.
3254 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003255 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003256get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3259 && (enc_canon_props(ptr) & ENC_MACROMAN))
3260 return FIO_MACROMAN;
3261 return 0;
3262}
3263#endif
3264
3265/*
3266 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3267 * "size" must be at least 2.
3268 * Return the name of the encoding and set "*lenp" to the length.
3269 * Returns NULL when no BOM found.
3270 */
3271 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003272check_for_bom(
3273 char_u *p,
3274 long size,
3275 int *lenp,
3276 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 char *name = NULL;
3279 int len = 2;
3280
3281 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003282 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003284 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 len = 3;
3286 }
3287 else if (p[0] == 0xff && p[1] == 0xfe)
3288 {
3289 if (size >= 4 && p[2] == 0 && p[3] == 0
3290 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003292 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 len = 4;
3294 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003295 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003296 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003297 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003298 // utf-16le is preferred, it also works for ucs-2le text
3299 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else if (p[0] == 0xfe && p[1] == 0xff
3302 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3303 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003304 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003305 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003306 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003307 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003308 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3311 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003313 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 len = 4;
3315 }
3316
3317 *lenp = len;
3318 return (char_u *)name;
3319}
3320
3321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 * Try to find a shortname by comparing the fullname with the current
3323 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003324 * Returns "full_path" or pointer into "full_path" if shortened.
3325 */
3326 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003327shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003328{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003329 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003330 char_u *p = full_path;
3331
Bram Moolenaard9462e32011-04-11 21:35:11 +02003332 dirname = alloc(MAXPATHL);
3333 if (dirname == NULL)
3334 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003335 if (mch_dirname(dirname, MAXPATHL) == OK)
3336 {
3337 p = shorten_fname(full_path, dirname);
3338 if (p == NULL || *p == NUL)
3339 p = full_path;
3340 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003341 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003342 return p;
3343}
3344
3345/*
3346 * Try to find a shortname by comparing the fullname with the current
3347 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * Returns NULL if not shorter name possible, pointer into "full_path"
3349 * otherwise.
3350 */
3351 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003352shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int len;
3355 char_u *p;
3356
3357 if (full_path == NULL)
3358 return NULL;
3359 len = (int)STRLEN(dir_name);
3360 if (fnamencmp(dir_name, full_path, len) == 0)
3361 {
3362 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003363#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003365 * MS-Windows: when a file is in the root directory, dir_name will end
3366 * in a slash, since C: by itself does not define a specific dir. In
3367 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 */
3369 if (!((len > 2) && (*(p - 2) == ':')))
3370#endif
3371 {
3372 if (vim_ispathsep(*p))
3373 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003374#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 else
3376 p = NULL;
3377#endif
3378 }
3379 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003380#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 /*
3382 * When using a file in the current drive, remove the drive name:
3383 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3384 * a floppy from "A:\dir" to "B:\dir".
3385 */
3386 else if (len > 3
3387 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3388 && full_path[1] == ':'
3389 && vim_ispathsep(full_path[2]))
3390 p = full_path + 2;
3391#endif
3392 else
3393 p = NULL;
3394 return p;
3395}
3396
3397/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003398 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * When "force" is TRUE: Use full path from now on for files currently being
3400 * edited, both for file name and swap file name. Try to shorten the file
3401 * names a bit, if safe to do so.
3402 * When "force" is FALSE: Only try to shorten absolute file names.
3403 * For buffers that have buftype "nofile" or "scratch": never change the file
3404 * name.
3405 */
3406 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003407shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3408{
3409 char_u *p;
3410
3411 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003412 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003413 && !path_with_url(buf->b_fname)
3414 && (force
3415 || buf->b_sfname == NULL
3416 || mch_isFullName(buf->b_sfname)))
3417 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003418 if (buf->b_sfname != buf->b_ffname)
3419 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003420 p = shorten_fname(buf->b_ffname, dirname);
3421 if (p != NULL)
3422 {
3423 buf->b_sfname = vim_strsave(p);
3424 buf->b_fname = buf->b_sfname;
3425 }
3426 if (p == NULL || buf->b_fname == NULL)
3427 buf->b_fname = buf->b_ffname;
3428 }
3429}
3430
3431/*
3432 * Shorten filenames for all buffers.
3433 */
3434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 char_u dirname[MAXPATHL];
3438 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003441 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003443 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003445 // Always make the swap file name a full path, a "nofile" buffer may
3446 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003447 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003450 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003451#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003452 popup_update_preview_title();
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454}
3455
3456#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3457 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003458 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 || defined(PROTO)
3460/*
3461 * Shorten all filenames in "fnames[count]" by current directory.
3462 */
3463 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003464shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 int i;
3467 char_u dirname[MAXPATHL];
3468 char_u *p;
3469
3470 if (fnames == NULL || count < 1)
3471 return;
3472 mch_dirname(dirname, sizeof(dirname));
3473 for (i = 0; i < count; ++i)
3474 {
3475 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003477 // shorten_fname() returns pointer in given "fnames[i]". If free
3478 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3479 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 p = vim_strsave(p);
3481 vim_free(fnames[i]);
3482 fnames[i] = p;
3483 }
3484 }
3485}
3486#endif
3487
3488/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003489 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * fo_o_h.ext for MSDOS or when shortname option set.
3491 *
3492 * Assumed that fname is a valid name found in the filesystem we assure that
3493 * the return value is a different name and ends in 'ext'.
3494 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3495 * characters otherwise.
3496 * Space for the returned name is allocated, must be freed later.
3497 * Returns NULL when out of memory.
3498 */
3499 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003500modname(
3501 char_u *fname,
3502 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003503 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003505 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 fname, ext, prepend_dot);
3507}
3508
3509 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003510buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003511 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003512 char_u *fname,
3513 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003514 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 char_u *retval;
3517 char_u *s;
3518 char_u *e;
3519 char_u *ptr;
3520 int fnamelen, extlen;
3521
3522 extlen = (int)STRLEN(ext);
3523
3524 /*
3525 * If there is no file name we must get the name of the current directory
3526 * (we need the full path in case :cd is used).
3527 */
3528 if (fname == NULL || *fname == NUL)
3529 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003530 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (retval == NULL)
3532 return NULL;
3533 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3534 (fnamelen = (int)STRLEN(retval)) == 0)
3535 {
3536 vim_free(retval);
3537 return NULL;
3538 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003539 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 retval[fnamelen++] = PATHSEP;
3542 retval[fnamelen] = NUL;
3543 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003544 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546 else
3547 {
3548 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003549 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (retval == NULL)
3551 return NULL;
3552 STRCPY(retval, fname);
3553#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003554 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555#endif
3556 }
3557
3558 /*
3559 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3560 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3561 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3562 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3563 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003564 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003566 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003567 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003570 {
3571 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003576 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3578 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 s = ptr + STRLEN(ptr);
3581
3582 /*
3583 * For 8.3 file names we may have to reduce the length.
3584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 /*
3588 * If there is no file name, or the file name ends in '/', and the
3589 * extension starts with '.', put a '_' before the dot, because just
3590 * ".ext" is invalid.
3591 */
3592 if (fname == NULL || *fname == NUL
3593 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 *s++ = '_';
3597 }
3598 /*
3599 * If the extension starts with '.', truncate the base name at 8
3600 * characters
3601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003604 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 s = ptr + 8;
3607 *s = '\0';
3608 }
3609 }
3610 /*
3611 * If the extension doesn't start with '.', and the file name
3612 * doesn't have an extension yet, append a '.'
3613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 else if ((e = vim_strchr(ptr, '.')) == NULL)
3615 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /*
3617 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003618 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
3620 else if ((int)STRLEN(e) + extlen > 4)
3621 s = e + 4 - extlen;
3622 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003623#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 /*
3625 * If there is no file name, and the extension starts with '.', put a
3626 * '_' before the dot, because just ".ext" may be invalid if it's on a
3627 * FAT partition, and on HPFS it doesn't matter.
3628 */
3629 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3630 *s++ = '_';
3631#endif
3632
3633 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003634 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 * ext can start with '.' and cannot exceed 3 more characters.
3636 */
3637 STRCPY(s, ext);
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 /*
3640 * Prepend the dot.
3641 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003642 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003644 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 /*
3649 * Check that, after appending the extension, the file name is really
3650 * different.
3651 */
3652 if (fname != NULL && STRCMP(fname, retval) == 0)
3653 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003654 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 while (--s >= ptr)
3656 {
3657 if (*s != '_')
3658 {
3659 *s = '_';
3660 break;
3661 }
3662 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003663 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 *ptr = 'v';
3665 }
3666 return retval;
3667}
3668
3669/*
3670 * Like fgets(), but if the file line is too long, it is truncated and the
3671 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003672 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
3674 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003675vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 char *eof;
3678#define FGETS_SIZE 200
3679 char tbuf[FGETS_SIZE];
3680
3681 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3684 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003685 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003687 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 do
3689 {
3690 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003691 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3693 }
3694 return (eof == NULL);
3695}
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697/*
3698 * rename() only works if both files are on the same file system, this
3699 * function will (attempts to?) copy the file across if rename fails -- webb
3700 * Return -1 for failure, 0 for success.
3701 */
3702 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003703vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 int fd_in;
3706 int fd_out;
3707 int n;
3708 char *errmsg = NULL;
3709 char *buffer;
3710#ifdef AMIGA
3711 BPTR flock;
3712#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003713 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003714 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003715#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003716 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003717#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003718 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003721 * When the names are identical, there is nothing to do. When they refer
3722 * to the same file (ignoring case and slash/backslash differences) but
3723 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003726 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003727 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003728 use_tmp_file = TRUE;
3729 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003730 return 0;
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /*
3734 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3735 */
3736 if (mch_stat((char *)from, &st) < 0)
3737 return -1;
3738
Bram Moolenaar3576da72008-12-30 15:15:57 +00003739#ifdef UNIX
3740 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003741 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003742
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003743 // It's possible for the source and destination to be the same file.
3744 // This happens when "from" and "to" differ in case and are on a FAT32
3745 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003746 if (mch_stat((char *)to, &st_to) >= 0
3747 && st.st_dev == st_to.st_dev
3748 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003749 use_tmp_file = TRUE;
3750 }
3751#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003752#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003753 {
3754 BY_HANDLE_FILE_INFORMATION info1, info2;
3755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003756 // It's possible for the source and destination to be the same file.
3757 // In that case go through a temp file name. This makes rename("foo",
3758 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003759 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3760 && win32_fileinfo(to, &info2) == FILEINFO_OK
3761 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3762 && info1.nFileIndexHigh == info2.nFileIndexHigh
3763 && info1.nFileIndexLow == info2.nFileIndexLow)
3764 use_tmp_file = TRUE;
3765 }
3766#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003767
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003768 if (use_tmp_file)
3769 {
3770 char tempname[MAXPATHL + 1];
3771
3772 /*
3773 * Find a name that doesn't exist and is in the same directory.
3774 * Rename "from" to "tempname" and then rename "tempname" to "to".
3775 */
3776 if (STRLEN(from) >= MAXPATHL - 5)
3777 return -1;
3778 STRCPY(tempname, from);
3779 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003780 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003781 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3782 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003783 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003784 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003785 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003786 if (mch_rename(tempname, (char *)to) == 0)
3787 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003788 // Strange, the second step failed. Try moving the
3789 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003790 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003791 return -1;
3792 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003793 // If it fails for one temp name it will most likely fail
3794 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003795 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003796 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003797 }
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 Moolenaar071d4272004-06-13 20:20:40 +00003801 /*
3802 * Delete the "to" file, this is required on some systems to make the
3803 * mch_rename() work, on other systems it makes sure that we don't have
3804 * two files when the mch_rename() fails.
3805 */
3806
3807#ifdef AMIGA
3808 /*
3809 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3810 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003811 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 * deleting the "from" file (horror!) we lock it during the remove.
3813 *
3814 * When used for making a backup before writing the file: This should not
3815 * happen with ":w", because startscript() should detect this problem and
3816 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3817 * name. This problem does exist with ":w filename", but then the
3818 * original file will be somewhere else so the backup isn't really
3819 * important. If autoscripting is off the rename may fail.
3820 */
3821 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3822#endif
3823 mch_remove(to);
3824#ifdef AMIGA
3825 if (flock)
3826 UnLock(flock);
3827#endif
3828
3829 /*
3830 * First try a normal rename, return if it works.
3831 */
3832 if (mch_rename((char *)from, (char *)to) == 0)
3833 return 0;
3834
3835 /*
3836 * Rename() failed, try copying the file.
3837 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003838 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003839#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003840 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003841 acl = mch_get_acl(from);
3842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3844 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003845 {
3846#ifdef HAVE_ACL
3847 mch_free_acl(acl);
3848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003850 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003851
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003853 fd_out = mch_open((char *)to,
3854 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (fd_out == -1)
3856 {
3857 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003858#ifdef HAVE_ACL
3859 mch_free_acl(acl);
3860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return -1;
3862 }
3863
Bram Moolenaar473952e2019-09-28 16:30:04 +02003864 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (buffer == NULL)
3866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003868 close(fd_in);
3869#ifdef HAVE_ACL
3870 mch_free_acl(acl);
3871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return -1;
3873 }
3874
Bram Moolenaar473952e2019-09-28 16:30:04 +02003875 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003876 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003878 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 break;
3880 }
3881
3882 vim_free(buffer);
3883 close(fd_in);
3884 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003885 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (n < 0)
3887 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003888 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 to = from;
3890 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003891#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003892 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003893#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003894#ifdef HAVE_ACL
3895 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003896 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003897#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003898#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003899 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (errmsg != NULL)
3902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003903 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 return -1;
3905 }
3906 mch_remove(from);
3907 return 0;
3908}
3909
3910static int already_warned = FALSE;
3911
3912/*
3913 * Check if any not hidden buffer has been changed.
3914 * Postpone the check if there are characters in the stuff buffer, a global
3915 * command is being executed, a mapping is being executed or an autocommand is
3916 * busy.
3917 * Returns TRUE if some message was written (screen should be redrawn and
3918 * cursor positioned).
3919 */
3920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003921check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923{
3924 buf_T *buf;
3925 int didit = 0;
3926 int n;
3927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 // Don't check timestamps while system() or another low-level function may
3929 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if (no_check_timestamps > 0)
3931 return FALSE;
3932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003933 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3934 // event and we would keep on checking if the file is steadily growing.
3935 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if (focus && did_check_timestamps)
3937 {
3938 need_check_timestamps = TRUE;
3939 return FALSE;
3940 }
3941
3942 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003943 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003944 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else
3946 {
3947 ++no_wait_return;
3948 did_check_timestamps = TRUE;
3949 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003950 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003952 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (buf->b_nwindows > 0)
3954 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003955 bufref_T bufref;
3956
3957 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 n = buf_check_timestamp(buf, focus);
3959 if (didit < n)
3960 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003961 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003963 // Autocommands have removed the buffer, start at the
3964 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 buf = firstbuf;
3966 continue;
3967 }
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 --no_wait_return;
3971 need_check_timestamps = FALSE;
3972 if (need_wait_return && didit == 2)
3973 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003974 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 out_flush();
3977 }
3978 }
3979 return didit;
3980}
3981
3982/*
3983 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3984 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3985 * empty.
3986 */
3987 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003988move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 buf_T *tbuf = curbuf;
3991 int retval = OK;
3992 linenr_T lnum;
3993 char_u *p;
3994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003995 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 curbuf = tobuf;
3997 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3998 {
3999 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4000 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4001 {
4002 vim_free(p);
4003 retval = FAIL;
4004 break;
4005 }
4006 vim_free(p);
4007 }
4008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004009 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (retval != FAIL)
4011 {
4012 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004013 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004014 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004016 // Oops! We could try putting back the saved lines, but that
4017 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 retval = FAIL;
4019 break;
4020 }
4021 }
4022
4023 curbuf = tbuf;
4024 return retval;
4025}
4026
4027/*
4028 * Check if buffer "buf" has been changed.
4029 * Also check if the file for a new buffer unexpectedly appeared.
4030 * return 1 if a changed buffer was found.
4031 * return 2 if a message has been displayed.
4032 * return 0 otherwise.
4033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004035buf_check_timestamp(
4036 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004037 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004039 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 int stat_res;
4041 int retval = 0;
4042 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004043 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004045 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004047 enum {
4048 RELOAD_NONE,
4049 RELOAD_NORMAL,
4050 RELOAD_DETECT
4051 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004052 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4054 int can_reload = FALSE;
4055#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004056 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int orig_mode = buf->b_orig_mode;
4058#ifdef FEAT_GUI
4059 int save_mouse_correct = need_mouse_correct;
4060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004062 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004063#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004064 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004065#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004066 bufref_T bufref;
4067
4068 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004070 // If there is no file name, the buffer is not loaded, 'buftype' is
4071 // set, we are in the middle of a save or being called recursively: ignore
4072 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (buf->b_ffname == NULL
4074 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004075 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004078#ifdef FEAT_NETBEANS_INTG
4079 || isNetbeansBuffer(buf)
4080#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004081#ifdef FEAT_TERMINAL
4082 || buf->b_term != NULL
4083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 )
4085 return 0;
4086
4087 if ( !(buf->b_flags & BF_NOTEDITED)
4088 && buf->b_mtime != 0
4089 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004090 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004091 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#ifdef HAVE_ST_MODE
4093 || (int)st.st_mode != buf->b_orig_mode
4094#else
4095 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4096#endif
4097 ))
4098 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004099 long prev_b_mtime = buf->b_mtime;
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 retval = 1;
4102
Bram Moolenaar386bc822018-07-07 18:34:12 +02004103 // set b_mtime to stop further warnings (e.g., when executing
4104 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (stat_res < 0)
4106 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004107 // Check the file again later to see if it re-appears.
4108 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 buf->b_orig_size = 0;
4110 buf->b_orig_mode = 0;
4111 }
4112 else
4113 buf_store_time(buf, &st, buf->b_ffname);
4114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004115 // Don't do anything for a directory. Might contain the file
4116 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (mch_isdir(buf->b_fname))
4118 ;
4119
4120 /*
4121 * If 'autoread' is set, the buffer has no changes and the file still
4122 * exists, reload the buffer. Use the buffer-local option value if it
4123 * was set, the global option value otherwise.
4124 */
4125 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4126 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004127 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 else
4129 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004130 if (stat_res < 0)
4131 reason = "deleted";
4132 else if (bufIsChanged(buf))
4133 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004134 /*
4135 * Check if the file contents really changed to avoid giving a
4136 * warning when only the timestamp was set (e.g., checked out of
4137 * CVS). Always warn when the buffer was changed.
4138 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4140 reason = "changed";
4141 else if (orig_mode != buf->b_orig_mode)
4142 reason = "mode";
4143 else
4144 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 /*
4147 * Only give the warning if there are no FileChangedShell
4148 * autocommands.
4149 * Avoid being called recursively by setting "busy".
4150 */
4151 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004152#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004153 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4154 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004155#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004156 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4158 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004159 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 busy = FALSE;
4161 if (n)
4162 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004163 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004164 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004165#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004166 s = get_vim_var_str(VV_FCS_CHOICE);
4167 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004168 reload = RELOAD_NORMAL;
4169 else if (STRCMP(s, "edit") == 0)
4170 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004171 else if (STRCMP(s, "ask") == 0)
4172 n = FALSE;
4173 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004174#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004175 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004177 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004179 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004180 {
4181 // Only give the message once.
4182 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004183 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 else
4186 {
4187 helpmesg = TRUE;
4188#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4189 can_reload = TRUE;
4190#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004191 if (reason[2] == 'n')
4192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004194 mesg2 = _("See \":help W12\" for more info.");
4195 }
4196 else if (reason[1] == 'h')
4197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004199 mesg2 = _("See \":help W11\" for more info.");
4200 }
4201 else if (*reason == 'm')
4202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004204 mesg2 = _("See \":help W16\" for more info.");
4205 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004206 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004207 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004208 // Only timestamp changed, store it to avoid a warning
4209 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004210 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004211 buf->b_mtime_read_ns = buf->b_mtime_ns;
4212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 }
4215 }
4216
4217 }
4218 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4219 && vim_fexists(buf->b_ffname))
4220 {
4221 retval = 1;
4222 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4223 buf->b_flags |= BF_NEW_W;
4224#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4225 can_reload = TRUE;
4226#endif
4227 }
4228
4229 if (mesg != NULL)
4230 {
4231 path = home_replace_save(buf, buf->b_fname);
4232 if (path != NULL)
4233 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004236 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004237 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004238#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004239 // Set warningmsg here, before the unimportant and output-specific
4240 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004241 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4244 if (can_reload)
4245 {
4246 if (*mesg2 != NUL)
4247 {
4248 STRCAT(tbuf, "\n");
4249 STRCAT(tbuf, mesg2);
4250 }
Rob Pilling8196e942022-02-11 15:12:10 +00004251 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4252 (char_u *)tbuf,
4253 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4254 1, NULL, TRUE))
4255 {
4256 case 2:
4257 reload = RELOAD_NORMAL;
4258 break;
4259 case 3:
4260 reload = RELOAD_DETECT;
4261 break;
4262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 else
4265#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004266 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4267 || already_warned)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 if (*mesg2 != NUL)
4270 {
4271 STRCAT(tbuf, "; ");
4272 STRCAT(tbuf, mesg2);
4273 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 retval = 2;
4276 }
4277 else
4278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004282 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004284 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 msg_clr_eos();
4286 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004287 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004290#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004292#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004293 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004294 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004296 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 redraw_cmdline = FALSE;
4298 }
4299 }
4300 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302
4303 vim_free(path);
4304 vim_free(tbuf);
4305 }
4306 }
4307
Rob Pilling8196e942022-02-11 15:12:10 +00004308 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004310 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004311 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004312#ifdef FEAT_PERSISTENT_UNDO
4313 if (buf->b_p_udf && buf->b_ffname != NULL)
4314 {
4315 char_u hash[UNDO_HASH_SIZE];
4316 buf_T *save_curbuf = curbuf;
4317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004319 curbuf = buf;
4320 u_compute_hash(hash);
4321 u_write_undo(NULL, FALSE, buf, hash);
4322 curbuf = save_curbuf;
4323 }
4324#endif
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004327 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004328 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004329 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4330 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 // restore this in case an autocommand has set it; it would break
4333 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 need_mouse_correct = save_mouse_correct;
4335#endif
4336
4337 return retval;
4338}
4339
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004340/*
4341 * Reload a buffer that is already loaded.
4342 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004343 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4344 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004345 */
4346 void
Rob Pilling8196e942022-02-11 15:12:10 +00004347buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004348{
4349 exarg_T ea;
4350 pos_T old_cursor;
4351 linenr_T old_topline;
4352 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004353 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004354 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004355 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004356 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004357 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004358 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004362
Rob Pilling8196e942022-02-11 15:12:10 +00004363 // Unless reload_options is set, we only want to read the text from the
4364 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4365 // Force the fileformat and encoding to be the same.
4366 if (reload_options)
4367 memset(&ea, 0, sizeof(ea));
4368 else
4369 prepped = prep_exarg(&ea, buf);
4370
4371 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004372 {
4373 old_cursor = curwin->w_cursor;
4374 old_topline = curwin->w_topline;
4375
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004376 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004377 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 // Save all the text, so that the reload can be undone.
4379 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004380 u_sync(FALSE);
4381 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4382 flags |= READ_KEEP_UNDO;
4383 }
4384
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 /*
4386 * To behave like when a new file is edited (matters for
4387 * BufReadPost autocommands) we first need to delete the current
4388 * buffer contents. But if reading the file fails we should keep
4389 * the old contents. Can't use memory only, the file might be
4390 * too big. Use a hidden buffer to move the buffer contents to.
4391 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004392 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393 savebuf = NULL;
4394 else
4395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004398 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004399 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004402 curbuf = savebuf;
4403 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004404 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 curbuf = buf;
4406 curwin->w_buffer = buf;
4407 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004408 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004409 || move_lines(buf, savebuf) == FAIL)
4410 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004411 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412 saved = FAIL;
4413 }
4414 }
4415
4416 if (saved == OK)
4417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004418 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4419 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004420 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4421 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004422 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004423 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004424#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004425 if (!aborting())
4426#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004427 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004428 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 // Put the text back from the save buffer. First
4431 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004432 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004433 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004434 break;
4435 (void)move_lines(savebuf, buf);
4436 }
4437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004438 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004439 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004441 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004442 if ((flags & READ_KEEP_UNDO) == 0)
4443 {
4444 u_blockfree(buf);
4445 u_clearall(buf);
4446 }
4447 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004448 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004450 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004451 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004452 }
4453 }
4454 vim_free(ea.cmd);
4455
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004456 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004457 wipe_buffer(savebuf, FALSE);
4458
4459#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004460 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004461 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004462#endif
4463
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004464 // Restore the topline and cursor position and check it (lines may
4465 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004466 if (old_topline > curbuf->b_ml.ml_line_count)
4467 curwin->w_topline = curbuf->b_ml.ml_line_count;
4468 else
4469 curwin->w_topline = old_topline;
4470 curwin->w_cursor = old_cursor;
4471 check_cursor();
4472 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004473 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004474#ifdef FEAT_FOLDING
4475 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004476 win_T *wp;
4477 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004480 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004481 if (wp->w_buffer == curwin->w_buffer
4482 && !foldmethodIsManual(wp))
4483 foldUpdateAll(wp);
4484 }
4485#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 // If the mode didn't change and 'readonly' was set, keep the old
4487 // value; the user probably used the ":view" command. But don't
4488 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004489 if (orig_mode == curbuf->b_orig_mode)
4490 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004493 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004494 }
4495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004497 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004499}
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004502buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
4504 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004505#ifdef ST_MTIM_NSEC
4506 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4507#else
4508 buf->b_mtime_ns = 0;
4509#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004510 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#ifdef HAVE_ST_MODE
4512 buf->b_orig_mode = (int)st->st_mode;
4513#else
4514 buf->b_orig_mode = mch_getperm(fname);
4515#endif
4516}
4517
4518/*
4519 * Adjust the line with missing eol, used for the next write.
4520 * Used for do_filter(), when the input lines for the filter are deleted.
4521 */
4522 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004523write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004526 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527}
4528
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004529// Subfuncions for readdirex()
4530#ifdef FEAT_EVAL
4531# ifdef MSWIN
4532 static char_u *
4533getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4534{
4535 stat_T st;
4536 unsigned short st_mode;
4537 DWORD flag = wfd->dwFileAttributes;
4538 WCHAR *wp;
4539
4540 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4541 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4542 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4543 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4544
4545 wp = wcsrchr(wfd->cFileName, L'.');
4546 if (wp != NULL)
4547 {
4548 if (_wcsicmp(wp, L".exe") == 0 ||
4549 _wcsicmp(wp, L".com") == 0 ||
4550 _wcsicmp(wp, L".cmd") == 0 ||
4551 _wcsicmp(wp, L".bat") == 0)
4552 st_mode |= _S_IEXEC;
4553 }
4554
4555 // Copy user bits to group/other.
4556 st_mode |= (st_mode & 0700) >> 3;
4557 st_mode |= (st_mode & 0700) >> 6;
4558
4559 st.st_mode = st_mode;
4560 return getfpermst(&st, perm);
4561}
4562
4563 static char_u *
4564getftypewfd(WIN32_FIND_DATAW *wfd)
4565{
4566 DWORD flag = wfd->dwFileAttributes;
4567 DWORD tag = wfd->dwReserved0;
4568
4569 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4570 {
4571 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4572 return (char_u*)"junction";
4573 else if (tag == IO_REPARSE_TAG_SYMLINK)
4574 {
4575 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4576 return (char_u*)"linkd";
4577 else
4578 return (char_u*)"link";
4579 }
4580 return (char_u*)"reparse"; // unknown reparse point type
4581 }
4582 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4583 return (char_u*)"dir";
4584 else
4585 return (char_u*)"file";
4586}
4587
4588 static dict_T *
4589create_readdirex_item(WIN32_FIND_DATAW *wfd)
4590{
4591 dict_T *item;
4592 char_u *p;
4593 varnumber_T size, time;
4594 char_u permbuf[] = "---------";
4595
4596 item = dict_alloc();
4597 if (item == NULL)
4598 return NULL;
4599 item->dv_refcount++;
4600
4601 p = utf16_to_enc(wfd->cFileName, NULL);
4602 if (p == NULL)
4603 goto theend;
4604 if (dict_add_string(item, "name", p) == FAIL)
4605 {
4606 vim_free(p);
4607 goto theend;
4608 }
4609 vim_free(p);
4610
4611 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4612 if (dict_add_number(item, "size", size) == FAIL)
4613 goto theend;
4614
4615 // Convert FILETIME to unix time.
4616 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4617 wfd->ftLastWriteTime.dwLowDateTime)
4618 - 116444736000000000) / 10000000;
4619 if (dict_add_number(item, "time", time) == FAIL)
4620 goto theend;
4621
4622 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4623 goto theend;
4624 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4625 goto theend;
4626
4627 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4628 goto theend;
4629 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4630 goto theend;
4631
4632 return item;
4633
4634theend:
4635 dict_unref(item);
4636 return NULL;
4637}
4638# else
4639 static dict_T *
4640create_readdirex_item(char_u *path, char_u *name)
4641{
4642 dict_T *item;
4643 char *p;
4644 size_t len;
4645 stat_T st;
4646 int ret, link = FALSE;
4647 varnumber_T size;
4648 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004649 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004650 struct passwd *pw;
4651 struct group *gr;
4652
4653 item = dict_alloc();
4654 if (item == NULL)
4655 return NULL;
4656 item->dv_refcount++;
4657
4658 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4659 p = alloc(len);
4660 if (p == NULL)
4661 goto theend;
4662 vim_snprintf(p, len, "%s/%s", path, name);
4663 ret = mch_lstat(p, &st);
4664 if (ret >= 0 && S_ISLNK(st.st_mode))
4665 {
4666 link = TRUE;
4667 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004668 if (ret < 0)
4669 q = (char_u*)"link";
4670
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004671 }
4672 vim_free(p);
4673
4674 if (dict_add_string(item, "name", name) == FAIL)
4675 goto theend;
4676
4677 if (ret >= 0)
4678 {
4679 size = (varnumber_T)st.st_size;
4680 if (S_ISDIR(st.st_mode))
4681 size = 0;
4682 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004683 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004684 size = -2;
4685 if (dict_add_number(item, "size", size) == FAIL)
4686 goto theend;
4687 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4688 goto theend;
4689
4690 if (link)
4691 {
4692 if (S_ISDIR(st.st_mode))
4693 q = (char_u*)"linkd";
4694 else
4695 q = (char_u*)"link";
4696 }
4697 else
4698 q = getftypest(&st);
4699 if (dict_add_string(item, "type", q) == FAIL)
4700 goto theend;
4701 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4702 goto theend;
4703
4704 pw = getpwuid(st.st_uid);
4705 if (pw == NULL)
4706 q = (char_u*)"";
4707 else
4708 q = (char_u*)pw->pw_name;
4709 if (dict_add_string(item, "user", q) == FAIL)
4710 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004711# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004712 gr = getgrgid(st.st_gid);
4713 if (gr == NULL)
4714 q = (char_u*)"";
4715 else
4716 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004717# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004718 if (dict_add_string(item, "group", q) == FAIL)
4719 goto theend;
4720 }
4721 else
4722 {
4723 if (dict_add_number(item, "size", -1) == FAIL)
4724 goto theend;
4725 if (dict_add_number(item, "time", -1) == FAIL)
4726 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004727 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004728 goto theend;
4729 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4730 goto theend;
4731 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4732 goto theend;
4733 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4734 goto theend;
4735 }
4736 return item;
4737
4738theend:
4739 dict_unref(item);
4740 return NULL;
4741}
4742# endif
4743
4744 static int
4745compare_readdirex_item(const void *p1, const void *p2)
4746{
4747 char_u *name1, *name2;
4748
Bram Moolenaard61efa52022-07-23 09:52:04 +01004749 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4750 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004751 if (readdirex_sort == READDIR_SORT_BYTE)
4752 return STRCMP(name1, name2);
4753 else if (readdirex_sort == READDIR_SORT_IC)
4754 return STRICMP(name1, name2);
4755 else
4756 return STRCOLL(name1, name2);
4757}
4758
4759 static int
4760compare_readdir_item(const void *s1, const void *s2)
4761{
4762 if (readdirex_sort == READDIR_SORT_BYTE)
4763 return STRCMP(*(char **)s1, *(char **)s2);
4764 else if (readdirex_sort == READDIR_SORT_IC)
4765 return STRICMP(*(char **)s1, *(char **)s2);
4766 else
4767 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004768}
4769#endif
4770
Bram Moolenaarda440d22016-01-16 21:27:23 +01004771#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4772/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004773 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004774 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004775 * If "withattr" is TRUE, retrieve the names and their attributes.
4776 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004777 * Return OK for success, FAIL for failure.
4778 */
4779 int
4780readdir_core(
4781 garray_T *gap,
4782 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004783 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004784 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004785 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004786 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004787{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004788 int failed = FALSE;
4789 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004790# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004791 char_u *buf;
4792 int ok;
4793 HANDLE hFind = INVALID_HANDLE_VALUE;
4794 WIN32_FIND_DATAW wfd;
4795 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004796# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004797 DIR *dirp;
4798 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004799# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004800
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004801 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004802
4803# ifdef FEAT_EVAL
4804# define FREE_ITEM(item) do { \
4805 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004806 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004807 else \
4808 vim_free(item); \
4809 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004810
4811 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004812# else
4813# define FREE_ITEM(item) vim_free(item)
4814# endif
4815
4816# ifdef MSWIN
4817 buf = alloc(MAXPATHL);
4818 if (buf == NULL)
4819 return FAIL;
4820 STRNCPY(buf, path, MAXPATHL-5);
4821 p = buf + STRLEN(buf);
4822 MB_PTR_BACK(buf, p);
4823 if (*p == '\\' || *p == '/')
4824 *p = NUL;
4825 STRCAT(p, "\\*");
4826
4827 wn = enc_to_utf16(buf, NULL);
4828 if (wn != NULL)
4829 hFind = FindFirstFileW(wn, &wfd);
4830 ok = (hFind != INVALID_HANDLE_VALUE);
4831 if (!ok)
4832 {
4833 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004834 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004835 }
4836 else
4837 {
4838 while (ok)
4839 {
4840 int ignore;
4841 void *item;
4842 WCHAR *wp;
4843
4844 wp = wfd.cFileName;
4845 ignore = wp[0] == L'.' &&
4846 (wp[1] == NUL ||
4847 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004848 if (ignore)
4849 {
4850 ok = FindNextFileW(hFind, &wfd);
4851 continue;
4852 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004853# ifdef FEAT_EVAL
4854 if (withattr)
4855 item = (void*)create_readdirex_item(&wfd);
4856 else
4857# endif
4858 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4859 if (item == NULL)
4860 {
4861 failed = TRUE;
4862 break;
4863 }
4864
4865 if (!ignore && checkitem != NULL)
4866 {
4867 int r = checkitem(context, item);
4868
4869 if (r < 0)
4870 {
4871 FREE_ITEM(item);
4872 break;
4873 }
4874 if (r == 0)
4875 ignore = TRUE;
4876 }
4877
4878 if (!ignore)
4879 {
4880 if (ga_grow(gap, 1) == OK)
4881 ((void**)gap->ga_data)[gap->ga_len++] = item;
4882 else
4883 {
4884 failed = TRUE;
4885 FREE_ITEM(item);
4886 break;
4887 }
4888 }
4889 else
4890 FREE_ITEM(item);
4891
4892 ok = FindNextFileW(hFind, &wfd);
4893 }
4894 FindClose(hFind);
4895 }
4896
4897 vim_free(buf);
4898 vim_free(wn);
4899# else // MSWIN
4900 dirp = opendir((char *)path);
4901 if (dirp == NULL)
4902 {
4903 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004904 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004905 }
4906 else
4907 {
4908 for (;;)
4909 {
4910 int ignore;
4911 void *item;
4912
4913 dp = readdir(dirp);
4914 if (dp == NULL)
4915 break;
4916 p = (char_u *)dp->d_name;
4917
4918 ignore = p[0] == '.' &&
4919 (p[1] == NUL ||
4920 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004921 if (ignore)
4922 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004923# ifdef FEAT_EVAL
4924 if (withattr)
4925 item = (void*)create_readdirex_item(path, p);
4926 else
4927# endif
4928 item = (void*)vim_strsave(p);
4929 if (item == NULL)
4930 {
4931 failed = TRUE;
4932 break;
4933 }
4934
Bram Moolenaarfe154992022-03-22 20:42:12 +00004935 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004936 {
4937 int r = checkitem(context, item);
4938
4939 if (r < 0)
4940 {
4941 FREE_ITEM(item);
4942 break;
4943 }
4944 if (r == 0)
4945 ignore = TRUE;
4946 }
4947
4948 if (!ignore)
4949 {
4950 if (ga_grow(gap, 1) == OK)
4951 ((void**)gap->ga_data)[gap->ga_len++] = item;
4952 else
4953 {
4954 failed = TRUE;
4955 FREE_ITEM(item);
4956 break;
4957 }
4958 }
4959 else
4960 FREE_ITEM(item);
4961 }
4962
4963 closedir(dirp);
4964 }
4965# endif // MSWIN
4966
4967# undef FREE_ITEM
4968
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004969 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004970 {
4971# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004972 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004973 if (withattr)
4974 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4975 compare_readdirex_item);
4976 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004977 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4978 compare_readdir_item);
4979# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004980 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004981# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004982 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004983
4984 return failed ? FAIL : OK;
4985}
4986
4987/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004988 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004989 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004990 */
4991 int
4992delete_recursive(char_u *name)
4993{
4994 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004995 int i;
4996 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004997 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004998
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004999 // A symbolic link to a directory itself is deleted, not the directory it
5000 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005001 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005002# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005003 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005004# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005005 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005006# endif
5007 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005008 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005009 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005010 if (exp == NULL)
5011 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005012 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005013 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005014 for (i = 0; i < ga.ga_len; ++i)
5015 {
5016 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5017 ((char_u **)ga.ga_data)[i]);
5018 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005019 // Remember the failure but continue deleting any further
5020 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005021 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005022 }
5023 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005024 if (mch_rmdir(exp) != 0)
5025 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005026 }
5027 else
5028 result = -1;
5029 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005030 }
5031 else
5032 result = mch_remove(name) == 0 ? 0 : -1;
5033
5034 return result;
5035}
5036#endif
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005041# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5042/*
5043 * Open temporary directory and take file lock to prevent
5044 * to be auto-cleaned.
5045 */
5046 static void
5047vim_opentempdir(void)
5048{
5049 DIR *dp = NULL;
5050
5051 if (vim_tempdir_dp != NULL)
5052 return;
5053
5054 dp = opendir((const char*)vim_tempdir);
5055
5056 if (dp != NULL)
5057 {
5058 vim_tempdir_dp = dp;
5059 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5060 }
5061}
5062
5063/*
5064 * Close temporary directory - it automatically release file lock.
5065 */
5066 static void
5067vim_closetempdir(void)
5068{
5069 if (vim_tempdir_dp != NULL)
5070 {
5071 closedir(vim_tempdir_dp);
5072 vim_tempdir_dp = NULL;
5073 }
5074}
5075# endif
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077/*
5078 * Delete the temp directory and all files it contains.
5079 */
5080 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005081vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 if (vim_tempdir != NULL)
5084 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005085# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5086 vim_closetempdir();
5087# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005089 gettail(vim_tempdir)[-1] = NUL;
5090 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005091 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005096 * Directory "tempdir" was created. Expand this name to a full path and put
5097 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5098 * "tempdir" must be no longer than MAXPATHL.
5099 */
5100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005101vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005102{
5103 char_u *buf;
5104
Bram Moolenaar964b3742019-05-24 18:54:09 +02005105 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005106 if (buf != NULL)
5107 {
5108 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5109 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005110 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005111 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005112# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5113 vim_opentempdir();
5114# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005115 vim_free(buf);
5116 }
5117}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005118#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005119
5120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 * vim_tempname(): Return a unique name that can be used for a temp file.
5122 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005123 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5124 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 *
5126 * The returned pointer is to allocated memory.
5127 * The returned pointer is NULL if no valid name was found.
5128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005130vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005132 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005136#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005137 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#else
5139 char_u itmp[TEMPNAMELEN];
5140#endif
5141
5142#ifdef TEMPDIRNAMES
5143 static char *(tempdirs[]) = {TEMPDIRNAMES};
5144 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005146 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147# endif
5148
5149 /*
5150 * This will create a directory for private use by this instance of Vim.
5151 * This is done once, and the same directory is used for all temp files.
5152 * This method avoids security problems because of symlink attacks et al.
5153 * It's also a bit faster, because we only need to check for an existing
5154 * file when creating the directory and not for each temp file.
5155 */
5156 if (vim_tempdir == NULL)
5157 {
5158 /*
5159 * Try the entries in TEMPDIRNAMES to create the temp directory.
5160 */
K.Takataeeec2542021-06-02 13:28:16 +02005161 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005163# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005164 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005165 long nr;
5166 long off;
5167# endif
5168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 // Expand $TMP, leave room for "/v1100000/999999999".
5170 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005172 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005174 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005175 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
Bram Moolenaareaf03392009-11-17 11:08:52 +00005177# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005178 {
5179# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005180 // Make sure the umask doesn't remove the executable bit.
5181 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005182 mode_t umask_save = umask(077);
5183# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005185 STRCAT(itmp, "vXXXXXX");
5186 if (mkdtemp((char *)itmp) != NULL)
5187 vim_settempdir(itmp);
5188# if defined(UNIX) || defined(VMS)
5189 (void)umask(umask_save);
5190# endif
5191 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005192# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // Get an arbitrary number of up to 6 digits. When it's
5194 // unlikely that it already exists it will be faster,
5195 // otherwise it doesn't matter. The use of mkdir() avoids any
5196 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005198 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005200 // Try up to 10000 different values until we find a name that
5201 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 for (off = 0; off < 10000L; ++off)
5203 {
5204 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005205# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
Bram Moolenaareaf03392009-11-17 11:08:52 +00005209 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5210# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005211 // If mkdir() does not set errno to EEXIST, check for
5212 // existing file here. There is a race condition then,
5213 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (mch_stat((char *)itmp, &st) >= 0)
5215 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005216# endif
5217# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005218 // Make sure the umask doesn't remove the executable bit.
5219 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005223# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (r == 0)
5227 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005228 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 break;
5230 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005231# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // If the mkdir() didn't fail because the file/dir exists,
5233 // we probably can't create any dir here, try another
5234 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005239# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (vim_tempdir != NULL)
5241 break;
5242 }
5243 }
5244 }
5245
5246 if (vim_tempdir != NULL)
5247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // There is no need to check if the file exists, because we own the
5249 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5251 return vim_strsave(itmp);
5252 }
5253
5254 return NULL;
5255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005256#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
Bram Moolenaar4f974752019-02-17 17:44:42 +01005258# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005259 WCHAR wszTempFile[_MAX_PATH + 1];
5260 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005261 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 char_u *retval;
5263 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005264 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005265 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005267 wcscpy(itmp, L"");
5268 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005269 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005270 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005271 wszTempFile[1] = L'\\';
5272 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005273 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005274 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005275
5276 // randomize the name to avoid collisions
5277 i = mch_get_pid() + extra_char;
5278 buf4[1] = chartab[i % 36];
5279 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005280 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005282 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005283 // GetTempFileName() will create the file, we don't want that
5284 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005286 // Backslashes in a temp file name cause problems when filtering with
5287 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005288 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005289 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005290 shname = gettail(p_sh);
5291 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5292 || strstr((char *)shname, "pwsh") != NULL ))
5293 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 for (p = retval; *p; ++p)
5295 if (*p == '\\')
5296 *p = '/';
5297 return retval;
5298
Bram Moolenaar4f974752019-02-17 17:44:42 +01005299# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005302 char_u *p;
5303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005305 p = tmpnam((char *)itmp);
5306 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 return NULL;
5308# else
5309 char_u *p;
5310
5311# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 // mktemp() is not working on VMS. It seems to be
5313 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 sprintf((char *)itmp, "VIM%c", extra_char);
5315 p = (char_u *)tempnam("tmp:", (char *)itmp);
5316 if (p != NULL)
5317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 // VMS will use '.LIS' if we don't explicitly specify an extension,
5319 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 STRCPY(itmp, p);
5321 STRCAT(itmp, ".txt");
5322 free(p);
5323 }
5324 else
5325 return NULL;
5326# else
5327 STRCPY(itmp, TEMPNAME);
5328 if ((p = vim_strchr(itmp, '?')) != NULL)
5329 *p = extra_char;
5330 if (mktemp((char *)itmp) == NULL)
5331 return NULL;
5332# endif
5333# endif
5334
5335 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005336# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338}
5339
5340#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5341/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005342 * Convert all backslashes in fname to forward slashes in-place, unless when
5343 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 */
5345 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 char_u *p;
5349
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005350 if (path_with_url(fname))
5351 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005354 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005356 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 *p = '/';
5358}
5359#endif
5360
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005361/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005362 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5363 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5364 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 * Used for autocommands and 'wildignore'.
5366 * Returns TRUE if there is a match, FALSE otherwise.
5367 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005368 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005369match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 char_u *pattern, // pattern to match with
5371 regprog_T **prog, // pre-compiled regprog or NULL
5372 char_u *fname, // full path of file name
5373 char_u *sfname, // short file name or NULL
5374 char_u *tail, // tail of path
5375 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 regmatch_T regmatch;
5378 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005381 if (prog != NULL)
5382 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005384 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
5386 /*
5387 * Try for a match with the pattern with:
5388 * 1. the full file name, when the pattern has a '/'.
5389 * 2. the short file name, when the pattern has a '/'.
5390 * 3. the tail of the file name, when the pattern has no '/'.
5391 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005392 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 && ((allow_dirs
5394 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5395 || (sfname != NULL
5396 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005397 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 result = TRUE;
5399
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005400 if (prog != NULL)
5401 *prog = regmatch.regprog;
5402 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005403 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return result;
5405}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Return TRUE if a file matches with a pattern in "list".
5409 * "list" is a comma-separated list of patterns, like 'wildignore'.
5410 * "sfname" is the short file name or NULL, "ffname" the long file name.
5411 */
5412 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u buf[100];
5416 char_u *tail;
5417 char_u *regpat;
5418 char allow_dirs;
5419 int match;
5420 char_u *p;
5421
5422 tail = gettail(sfname);
5423
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005424 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 p = list;
5426 while (*p)
5427 {
5428 copy_option_part(&p, buf, 100, ",");
5429 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5430 if (regpat == NULL)
5431 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005432 match = match_file_pat(regpat, NULL, ffname, sfname,
5433 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 vim_free(regpat);
5435 if (match)
5436 return TRUE;
5437 }
5438 return FALSE;
5439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441/*
5442 * Convert the given pattern "pat" which has shell style wildcards in it, into
5443 * a regular expression, and return the result in allocated memory. If there
5444 * is a directory path separator to be matched, then TRUE is put in
5445 * allow_dirs, otherwise FALSE is put there -- webb.
5446 * Handle backslashes before special characters, like "\*" and "\ ".
5447 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 * Returns NULL when out of memory.
5449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451file_pat_to_reg_pat(
5452 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 char_u *pat_end, // first char after pattern or NULL
5454 char *allow_dirs, // Result passed back out in here
5455 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005457 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 char_u *endp;
5459 char_u *reg_pat;
5460 char_u *p;
5461 int i;
5462 int nested = 0;
5463 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 if (allow_dirs != NULL)
5466 *allow_dirs = FALSE;
5467 if (pat_end == NULL)
5468 pat_end = pat + STRLEN(pat);
5469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 for (p = pat; p < pat_end; p++)
5471 {
5472 switch (*p)
5473 {
5474 case '*':
5475 case '.':
5476 case ',':
5477 case '{':
5478 case '}':
5479 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005480 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 break;
5482#ifdef BACKSLASH_IN_FILENAME
5483 case '\\':
5484 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005485 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 break;
5487#endif
5488 default:
5489 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005490 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 ++p;
5493 ++size;
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 break;
5496 }
5497 }
5498 reg_pat = alloc(size + 1);
5499 if (reg_pat == NULL)
5500 return NULL;
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
5504 if (pat[0] == '*')
5505 while (pat[0] == '*' && pat < pat_end - 1)
5506 pat++;
5507 else
5508 reg_pat[i++] = '^';
5509 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005510 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
5512 while (endp - pat > 0 && *endp == '*')
5513 endp--;
5514 add_dollar = FALSE;
5515 }
5516 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5517 {
5518 switch (*p)
5519 {
5520 case '*':
5521 reg_pat[i++] = '.';
5522 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005523 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005524 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 break;
5526 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 case '~':
5528 reg_pat[i++] = '\\';
5529 reg_pat[i++] = *p;
5530 break;
5531 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 reg_pat[i++] = '.';
5533 break;
5534 case '\\':
5535 if (p[1] == NUL)
5536 break;
5537#ifdef BACKSLASH_IN_FILENAME
5538 if (!no_bslash)
5539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 // translate:
5541 // "\x" to "\\x" e.g., "dir\file"
5542 // "\*" to "\\.*" e.g., "dir\*.c"
5543 // "\?" to "\\." e.g., "dir\??.c"
5544 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5546 && p[1] != '+')
5547 {
5548 reg_pat[i++] = '[';
5549 reg_pat[i++] = '\\';
5550 reg_pat[i++] = '/';
5551 reg_pat[i++] = ']';
5552 if (allow_dirs != NULL)
5553 *allow_dirs = TRUE;
5554 break;
5555 }
5556 }
5557#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005558 // Undo escaping from ExpandEscape():
5559 // foo\?bar -> foo?bar
5560 // foo\%bar -> foo%bar
5561 // foo\,bar -> foo,bar
5562 // foo\ bar -> foo bar
5563 // Don't unescape \, * and others that are also special in a
5564 // regexp.
5565 // An escaped { must be unescaped since we use magic not
5566 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (*++p == '?'
5568#ifdef BACKSLASH_IN_FILENAME
5569 && no_bslash
5570#endif
5571 )
5572 reg_pat[i++] = '?';
5573 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005574 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005575 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005576 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005577 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5578 {
5579 reg_pat[i++] = '\\';
5580 reg_pat[i++] = '{';
5581 p += 2;
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 else
5584 {
5585 if (allow_dirs != NULL && vim_ispathsep(*p)
5586#ifdef BACKSLASH_IN_FILENAME
5587 && (!no_bslash || *p != '\\')
5588#endif
5589 )
5590 *allow_dirs = TRUE;
5591 reg_pat[i++] = '\\';
5592 reg_pat[i++] = *p;
5593 }
5594 break;
5595#ifdef BACKSLASH_IN_FILENAME
5596 case '/':
5597 reg_pat[i++] = '[';
5598 reg_pat[i++] = '\\';
5599 reg_pat[i++] = '/';
5600 reg_pat[i++] = ']';
5601 if (allow_dirs != NULL)
5602 *allow_dirs = TRUE;
5603 break;
5604#endif
5605 case '{':
5606 reg_pat[i++] = '\\';
5607 reg_pat[i++] = '(';
5608 nested++;
5609 break;
5610 case '}':
5611 reg_pat[i++] = '\\';
5612 reg_pat[i++] = ')';
5613 --nested;
5614 break;
5615 case ',':
5616 if (nested)
5617 {
5618 reg_pat[i++] = '\\';
5619 reg_pat[i++] = '|';
5620 }
5621 else
5622 reg_pat[i++] = ',';
5623 break;
5624 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005625 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005627 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 *allow_dirs = TRUE;
5629 reg_pat[i++] = *p;
5630 break;
5631 }
5632 }
5633 if (add_dollar)
5634 reg_pat[i++] = '$';
5635 reg_pat[i] = NUL;
5636 if (nested != 0)
5637 {
5638 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005639 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005641 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005642 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
5644 return reg_pat;
5645}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005646
5647#if defined(EINTR) || defined(PROTO)
5648/*
5649 * Version of read() that retries when interrupted by EINTR (possibly
5650 * by a SIGWINCH).
5651 */
5652 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005654{
5655 long ret;
5656
5657 for (;;)
5658 {
5659 ret = vim_read(fd, buf, bufsize);
5660 if (ret >= 0 || errno != EINTR)
5661 break;
5662 }
5663 return ret;
5664}
5665
5666/*
5667 * Version of write() that retries when interrupted by EINTR (possibly
5668 * by a SIGWINCH).
5669 */
5670 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005672{
5673 long ret = 0;
5674 long wlen;
5675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005676 // Repeat the write() so long it didn't fail, other than being interrupted
5677 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005678 while (ret < (long)bufsize)
5679 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005680 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005681 if (wlen < 0)
5682 {
5683 if (errno != EINTR)
5684 break;
5685 }
5686 else
5687 ret += wlen;
5688 }
5689 return ret;
5690}
5691#endif