blob: 81a7b5091595fe70ee341d152331bea5077bfe10 [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 Moolenaarbf1b7a72009-03-05 02:15:53 +000044static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000045
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020046#ifdef FEAT_EVAL
47static int readdirex_sort;
48#endif
49
Bram Moolenaar473952e2019-09-28 16:30:04 +020050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010051filemess(
52 buf_T *buf,
53 char_u *name,
54 char_u *s,
55 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020058 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
60 if (msg_silent != 0)
61 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010062 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020063
Bram Moolenaar217e1b82019-12-01 21:41:28 +010064 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020065 if (STRLEN(IObuff) > IOSIZE - 100)
66 IObuff[IOSIZE - 100] = NUL;
67
68 // Avoid an over-long translation to cause trouble.
69 STRNCAT(IObuff, s, 99);
70
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 /*
72 * For the first message may have to start a new line.
73 * For further ones overwrite the previous one, reset msg_scroll before
74 * calling filemess().
75 */
76 msg_scroll_save = msg_scroll;
77 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
78 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010079 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 check_for_delay(FALSE);
81 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020082 if (prev_msg_col != 0 && msg_col == 0)
83 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 msg_scroll = msg_scroll_save;
85 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010086 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
88 msg_clr_eos();
89 out_flush();
90 msg_scrolled_ign = FALSE;
91}
92
93/*
94 * Read lines from file "fname" into the buffer after line "from".
95 *
96 * 1. We allocate blocks with lalloc, as big as possible.
97 * 2. Each block is filled with characters from the file with a single read().
98 * 3. The lines are inserted in the buffer with ml_append().
99 *
100 * (caller must check that fname != NULL, unless READ_STDIN is used)
101 *
102 * "lines_to_skip" is the number of lines that must be skipped
103 * "lines_to_read" is the number of lines that are appended
104 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
105 *
106 * flags:
107 * READ_NEW starting to edit a new buffer
108 * READ_FILTER reading filter output
109 * READ_STDIN read from stdin instead of a file
110 * READ_BUFFER read from curbuf instead of a file (converting after reading
111 * stdin)
112 * 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");
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200220 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 Moolenaar071d4272004-06-13 20:20:40 +0000313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100316 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100318 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000320 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200322 size_t namelen = STRLEN(fname);
323
324 // If the name is too long we might crash further on, quit here.
325 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000326 {
327 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
328 msg_end();
329 msg_scroll = msg_save;
330 return FAIL;
331 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200332
333 // If the name ends in a path separator, we can't open it. Check here,
334 // because reading the file may actually work, but then creating the
335 // swap file may destroy it! Reported on MS-DOS and Win 95.
336 if (after_pathsep(fname, fname + namelen))
337 {
338 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
339 msg_end();
340 msg_scroll = msg_save;
341 return FAIL;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
344
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200345 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100347#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200348 /*
349 * On Unix it is possible to read a directory, so we have to
350 * check for it before the mch_open().
351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100353 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
354 && !S_ISFIFO(perm) // ... or fifo
355 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000356# ifdef OPEN_CHR_FILES
357 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100358 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 )
361 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100362 int retval = FAIL;
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100365 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200366 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100367 retval = NOTDONE;
368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 else
370 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
371 msg_end();
372 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100373 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200375#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100376#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000377 /*
378 * MS-Windows allows opening a device, but we will probably get stuck
379 * trying to read it.
380 */
381 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
382 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000383 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000384 msg_end();
385 msg_scroll = msg_save;
386 return FAIL;
387 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000388#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200389 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000390
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200392 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
394 /*
395 * When opening a new file we take the readonly flag from the file.
396 * Default is r/w, can be set to r/o below.
397 * Don't reset it when in readonly mode
398 * Only set/reset b_p_ro when BF_CHECK_RO is set.
399 */
400 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000401 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 curbuf->b_p_ro = FALSE;
403
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200404 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100406 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 if (mch_stat((char *)fname, &st) >= 0)
408 {
409 buf_store_time(curbuf, &st, fname);
410 curbuf->b_mtime_read = curbuf->b_mtime;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200411 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#ifdef UNIX
413 /*
414 * Use the protection bits of the original file for the swap file.
415 * This makes it possible for others to read the name of the
416 * edited file from the swapfile, but only if they can read the
417 * edited file.
418 * Remove the "write" and "execute" bits for group and others
419 * (they must not write the swapfile).
420 * Add the "read" and "write" bits for the user, otherwise we may
421 * not be able to write to the file ourselves.
422 * Setting the bits is done below, after creating the swap file.
423 */
424 swap_mode = (st.st_mode & 0644) | 0600;
425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef VMS
427 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000428 curbuf->b_fab_rat = st.st_fab_rat;
429 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#endif
431 }
432 else
433 {
434 curbuf->b_mtime = 0;
435 curbuf->b_mtime_read = 0;
436 curbuf->b_orig_size = 0;
437 curbuf->b_orig_mode = 0;
438 }
439
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100440 // Reset the "new file" flag. It will be set again below when the
441 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
443 }
444
445/*
446 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100447 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 */
449 file_readonly = FALSE;
450 if (read_stdin)
451 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100452#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100453 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 setmode(0, O_BINARY);
455#endif
456 }
457 else if (!read_buffer)
458 {
459#ifdef USE_MCH_ACCESS
460 if (
461# ifdef UNIX
462 !(perm & 0222) ||
463# endif
464 mch_access((char *)fname, W_OK))
465 file_readonly = TRUE;
466 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
467#else
468 if (!newfile
469 || readonlymode
470 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
471 {
472 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100473 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
475 }
476#endif
477 }
478
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100479 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {
481#ifndef UNIX
482 int isdir_f;
483#endif
484 msg_scroll = msg_save;
485#ifndef UNIX
486 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100487 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 */
489 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (isdir_f)
492 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200493 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
496 else
497#endif
498 if (newfile)
499 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200500 if (perm < 0
501#ifdef ENOENT
502 && errno == ENOENT
503#endif
504 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {
506 /*
507 * Set the 'new-file' flag, so that when the file has
508 * been created by someone else, a ":w" will complain.
509 */
510 curbuf->b_flags |= BF_NEW;
511
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100512 // Create a swap file now, so that other Vims are warned
513 // that we are editing this file. Don't do this for a
514 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515#ifdef FEAT_QUICKFIX
516 if (!bt_dontwrite(curbuf))
517#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100520 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000521 if (curbuf != old_curbuf
522 || (using_b_ffname
523 && (old_b_ffname != curbuf->b_ffname))
524 || (using_b_fname
525 && (old_b_fname != curbuf->b_fname)))
526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000528 return FAIL;
529 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000530 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000531 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200532 filemess(curbuf, sfname,
533 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000534 else
535 filemess(curbuf, sfname,
536 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100538 // Even though this is a new file, it might have been
539 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 check_marks_read();
541#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100542 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200543 if (eap != NULL)
544 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
546 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 save_file_ff(curbuf);
549
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100550#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100551 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 return FAIL;
553#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100554 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556 else
557 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000558 filemess(curbuf, sfname, (char_u *)(
559# ifdef EFBIG
560 (errno == EFBIG) ? _("[File too big]") :
561# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200562# ifdef EOVERFLOW
563 (errno == EOVERFLOW) ? _("[File too big]") :
564# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000565 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100566 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 }
568 }
569
570 return FAIL;
571 }
572
573 /*
574 * Only set the 'ro' flag for readonly files the first time they are
575 * loaded. Help files always get readonly mode
576 */
577 if ((check_readonly && file_readonly) || curbuf->b_help)
578 curbuf->b_p_ro = TRUE;
579
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000580 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100582 // Don't change 'eol' if reading from buffer as it will already be
583 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000584 if (!read_buffer)
585 {
586 curbuf->b_p_eol = TRUE;
587 curbuf->b_start_eol = TRUE;
588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000590 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 }
592
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100593 // Create a swap file now, so that other Vims are warned that we are
594 // editing this file.
595 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_QUICKFIX
597 if (!bt_dontwrite(curbuf))
598#endif
599 {
600 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000601 if (!read_stdin && (curbuf != old_curbuf
602 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
603 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100605 emsg(_(e_auchangedbuf));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000606 if (!read_buffer)
607 close(fd);
608 return FAIL;
609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100611 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000612 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
613 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100614 {
615 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
616
617 /*
618 * If the group-read bit is set but not the world-read bit, then
619 * the group must be equal to the group of the original file. If
620 * we can't make that happen then reset the group-read bit. This
621 * avoids making the swap file readable to more users when the
622 * primary group of the user is too permissive.
623 */
624 if ((swap_mode & 044) == 040)
625 {
626 stat_T swap_st;
627
628 if (mch_stat((char *)swap_fname, &swap_st) >= 0
629 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200630# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100631 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200632 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200633# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200634 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100635 swap_mode &= 0600;
636 }
637
638 (void)mch_setperm(swap_fname, (long)swap_mode);
639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#endif
641 }
642
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200643 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (swap_exists_action == SEA_QUIT)
645 {
646 if (!read_buffer && !read_stdin)
647 close(fd);
648 return FAIL;
649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100651 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 /*
654 * Set '[ mark to the line above where the lines go (line 1 if zero).
655 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100656 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
658 curbuf->b_op_start.col = 0;
659
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100660 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
661 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
662 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (!read_buffer)
665 {
666 int m = msg_scroll;
667 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 /*
670 * The file must be closed again, the autocommands may want to change
671 * the file before reading it.
672 */
673 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100674 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 /*
677 * The output from the autocommands should not overwrite anything and
678 * should not be overwritten: Set msg_scroll, restore its value if no
679 * output was done.
680 */
681 msg_scroll = TRUE;
682 if (filtering)
683 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
684 FALSE, curbuf, eap);
685 else if (read_stdin)
686 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
687 FALSE, curbuf, eap);
688 else if (newfile)
689 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
690 FALSE, curbuf, eap);
691 else
692 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
693 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100695 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
696 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
697 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100698 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100699
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (msg_scrolled == n)
701 msg_scroll = m;
702
703#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 --no_wait_return;
707 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100708 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 return FAIL;
710 }
711#endif
712 /*
713 * Don't allow the autocommands to change the current buffer.
714 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000715 *
716 * Don't allow the autocommands to change the buffer name either
717 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 */
719 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000720 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
721 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
723 {
724 --no_wait_return;
725 msg_scroll = msg_save;
726 if (fd < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100727 emsg(_("E200: *ReadPre autocommands made the file unreadable"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100729 emsg(_("E201: *ReadPre autocommands must not change current buffer"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100730 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 return FAIL;
732 }
733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100735 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
737
738 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
739 {
740 /*
741 * Show the user that we are busy reading the input. Sometimes this
742 * may take a while. When reading from stdin another program may
743 * still be running, don't move the cursor to the last line, unless
744 * always using the GUI.
745 */
746 if (read_stdin)
747 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100748 if (!is_not_a_term())
749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200751# ifdef VIMDLL
752 if (!gui.in_use)
753# endif
754 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#endif
756#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100757 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100758 if (gui.in_use && !gui.dying && !gui.starting)
759 {
760 p = (char_u *)_("Reading from stdin...");
761 gui_write(p, (int)STRLEN(p));
762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766 else if (!read_buffer)
767 filemess(curbuf, sfname, (char_u *)"", 0);
768 }
769
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100770 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 /*
773 * Set linecnt now, before the "retry" caused by a wrong guess for
774 * fileformat, and after the autocommands, which may change them.
775 */
776 linecnt = curbuf->b_ml.ml_line_count;
777
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100778 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000779 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000780 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000781 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000782 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000783 curbuf->b_bad_char = eap->bad_char;
784 }
785 else
786 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000789 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 */
791 if (eap != NULL && eap->force_enc != 0)
792 {
793 fenc = enc_canonize(eap->cmd + eap->force_enc);
794 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000795 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 else if (curbuf->b_p_bin)
798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100799 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 fenc_alloced = FALSE;
801 }
802 else if (curbuf->b_help)
803 {
804 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000805 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // Help files are either utf-8 or latin1. Try utf-8 first, if this
808 // fails it must be latin1.
809 // Always do this when 'encoding' is "utf-8". Otherwise only do
810 // this when needed to avoid [converted] remarks all the time.
811 // It is needed when the first line contains non-ASCII characters.
812 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 fenc = (char_u *)"latin1";
814 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000815 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000817 fc = fname[STRLEN(fname) - 1];
818 if (TOLOWER_ASC(fc) == 'x')
819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // Read the first line (and a bit more). Immediately rewind to
821 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100822 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200823 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000824 for (p = firstline; p < firstline + len; ++p)
825 if (*p >= 0x80)
826 {
827 c = TRUE;
828 break;
829 }
830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832
833 if (c)
834 {
835 fenc_next = fenc;
836 fenc = (char_u *)"utf-8";
837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // When the file is utf-8 but a character doesn't fit in
839 // 'encoding' don't retry. In help text editing utf-8 bytes
840 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000841 if (!enc_utf8)
842 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 fenc_alloced = FALSE;
845 }
846 else if (*p_fencs == NUL)
847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100848 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 fenc_alloced = FALSE;
850 }
851 else
852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100853 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200854 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857 /*
858 * Jump back here to retry reading the file in different ways.
859 * Reasons to retry:
860 * - encoding conversion failed: try another one from "fenc_next"
861 * - BOM detected and fenc was set, need to setup conversion
862 * - "fileformat" check failed: try another
863 *
864 * Variables set for special retry actions:
865 * "file_rewind" Rewind the file to start reading it again.
866 * "advance_fenc" Advance "fenc" using "fenc_next".
867 * "skip_read" Re-use already read bytes (BOM detected).
868 * "did_iconv" iconv() conversion failed, try 'charconvert'.
869 * "keep_fileformat" Don't reset "fileformat".
870 *
871 * Other status indicators:
872 * "tmpname" When != NULL did conversion with 'charconvert'.
873 * Output file has to be deleted afterwards.
874 * "iconv_fd" When != -1 did conversion with iconv().
875 */
876retry:
877
878 if (file_rewind)
879 {
880 if (read_buffer)
881 {
882 read_buf_lnum = 1;
883 read_buf_col = 0;
884 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200885 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 error = TRUE;
889 goto failed;
890 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200893 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000895 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000898 curbuf->b_start_bomb = FALSE;
899 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000900 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902
903 /*
904 * When retrying with another "fenc" and the first time "fileformat"
905 * will be reset.
906 */
907 if (keep_fileformat)
908 keep_fileformat = FALSE;
909 else
910 {
911 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000914 try_unix = try_dos = try_mac = FALSE;
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100917 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100919 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100921 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923
Bram Moolenaar13505972019-01-24 15:04:48 +0100924#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (iconv_fd != (iconv_t)-1)
926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100927 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 iconv_close(iconv_fd);
929 iconv_fd = (iconv_t)-1;
930 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 if (advance_fenc)
934 {
935 /*
936 * Try the next entry in 'fileencodings'.
937 */
938 advance_fenc = FALSE;
939
940 if (eap != NULL && eap->force_enc != 0)
941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100942 // Conversion given with "++cc=" wasn't possible, read
943 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000945 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (fenc_alloced)
947 vim_free(fenc);
948 fenc = (char_u *)"";
949 fenc_alloced = FALSE;
950 }
951 else
952 {
953 if (fenc_alloced)
954 vim_free(fenc);
955 if (fenc_next != NULL)
956 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200957 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 else
960 {
961 fenc = (char_u *)"";
962 fenc_alloced = FALSE;
963 }
964 }
965 if (tmpname != NULL)
966 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100967 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100968 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 }
971
972 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000973 * Conversion may be required when the encoding of the file is different
974 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 */
976 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000977 converted = need_conversion(fenc);
978 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100981 // "ucs-bom" means we need to check the first bytes of the file
982 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (STRCMP(fenc, ENC_UCSBOM) == 0)
984 fio_flags = FIO_UCSBOM;
985
986 /*
987 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
988 * done. This is handled below after read(). Prepare the
989 * fio_flags to avoid having to parse the string each time.
990 * Also check for Unicode to Latin1 conversion, because iconv()
991 * appears not to handle this correctly. This works just like
992 * conversion to UTF-8 except how the resulting character is put in
993 * the buffer.
994 */
995 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
996 fio_flags = get_fio_flags(fenc);
997
Bram Moolenaar4f974752019-02-17 17:44:42 +0100998#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /*
1000 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1001 * is handled with MultiByteToWideChar().
1002 */
1003 if (fio_flags == 0)
1004 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar13505972019-01-24 15:04:48 +01001007#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001008 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (fio_flags == 0)
1010 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaar13505972019-01-24 15:04:48 +01001013#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 /*
1015 * Try using iconv() if we can't convert internally.
1016 */
1017 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001018# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 )
1022 iconv_fd = (iconv_t)my_iconv_open(
1023 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar13505972019-01-24 15:04:48 +01001026#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /*
1028 * Use the 'charconvert' expression when conversion is required
1029 * and we can't do it internally or with iconv().
1030 */
1031 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001032 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001033# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 )
1037 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001038# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001040# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // Skip conversion when it's already done (retry for wrong
1042 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (tmpname == NULL)
1044 {
1045 tmpname = readfile_charconvert(fname, fenc, &fd);
1046 if (tmpname == NULL)
1047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001048 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 advance_fenc = TRUE;
1050 if (fd < 0)
1051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 // Re-opening the original file failed!
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_("E202: Conversion made file unreadable!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 error = TRUE;
1055 goto failed;
1056 }
1057 goto retry;
1058 }
1059 }
1060 }
1061 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
1064 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001065#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 )
1069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 // Conversion wanted but we can't.
1071 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 advance_fenc = TRUE;
1073 goto retry;
1074 }
1075 }
1076 }
1077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 // Set "can_retry" when it's possible to rewind the file and try with
1079 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1080 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001081 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 if (!skip_read)
1084 {
1085 linerest = 0;
1086 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001087 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 skip_count = lines_to_skip;
1089 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001091#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001092 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1093 && curbuf->b_ffname != NULL
1094 && curbuf->b_p_udf
1095 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001096 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001097 && !read_stdin
1098 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001099 if (read_undo_file)
1100 sha256_start(&sha_ctx);
1101#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001102#ifdef FEAT_CRYPT
1103 if (curbuf->b_cryptstate != NULL)
1104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 // Need to free the state, but keep the key, don't want to ask for
1106 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001107 crypt_free_state(curbuf->b_cryptstate);
1108 curbuf->b_cryptstate = NULL;
1109 }
1110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112
1113 while (!error && !got_int)
1114 {
1115 /*
1116 * We allocate as much space for the file as we can get, plus
1117 * space for the old line plus room for one terminating NUL.
1118 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001119 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001121 if (!skip_read)
1122 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001123#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001124 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001125#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // Use buffer >= 64K. Add linerest to double the size if the
1127 // line gets very long, to avoid a lot of copying. But don't
1128 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001129 size = 0x10000L + linerest;
1130 if (size > 0x100000L)
1131 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001132#endif
1133 }
1134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001136 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 size = 1;
1141 }
1142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 if (!skip_read)
1145 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001146 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001148 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 FALSE)) != NULL)
1150 break;
1151 }
1152 if (new_buffer == NULL)
1153 {
1154 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1155 error = TRUE;
1156 break;
1157 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001158 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1160 vim_free(buffer);
1161 buffer = new_buffer;
1162 ptr = buffer + linerest;
1163 line_start = buffer;
1164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001165 // May need room to translate into.
1166 // For iconv() we don't really know the required space, use a
1167 // factor ICONV_MULT.
1168 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1169 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1170 // become up to 4 bytes, size must be multiple of 2
1171 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1172 // multiple of 2
1173 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1174 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001175 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001176#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (iconv_fd != (iconv_t)-1)
1178 size = size / ICONV_MULT;
1179 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (fio_flags & FIO_LATIN1)
1182 size = size / 2;
1183 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1184 size = (size * 2 / 3) & ~1;
1185 else if (fio_flags & FIO_UCS4)
1186 size = (size * 2 / 3) & ~3;
1187 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001188 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001189#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001191 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001192#endif
1193#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#endif
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (conv_restlen > 0)
1199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001200 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 mch_memmove(ptr, conv_rest, conv_restlen);
1202 ptr += conv_restlen;
1203 size -= conv_restlen;
1204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206 if (read_buffer)
1207 {
1208 /*
1209 * Read bytes from curbuf. Used for converting text read
1210 * from stdin.
1211 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001212 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (read_buf_lnum > from)
1214 size = 0;
1215 else
1216 {
1217 int n, ni;
1218 long tlen;
1219
1220 tlen = 0;
1221 for (;;)
1222 {
1223 p = ml_get(read_buf_lnum) + read_buf_col;
1224 n = (int)STRLEN(p);
1225 if ((int)tlen + n + 1 > size)
1226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001227 // Filled up to "size", append partial line.
1228 // Change NL to NUL to reverse the effect done
1229 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001230 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 for (ni = 0; ni < n; ++ni)
1232 {
1233 if (p[ni] == NL)
1234 ptr[tlen++] = NUL;
1235 else
1236 ptr[tlen++] = p[ni];
1237 }
1238 read_buf_col += n;
1239 break;
1240 }
1241 else
1242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 // Append whole line and new-line. Change NL
1244 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 for (ni = 0; ni < n; ++ni)
1246 {
1247 if (p[ni] == NL)
1248 ptr[tlen++] = NUL;
1249 else
1250 ptr[tlen++] = p[ni];
1251 }
1252 ptr[tlen++] = NL;
1253 read_buf_col = 0;
1254 if (++read_buf_lnum > from)
1255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001256 // When the last line didn't have an
1257 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (!curbuf->b_p_eol)
1259 --tlen;
1260 size = tlen;
Christian Brabandt226b28b2021-06-21 21:08:08 +02001261 eof = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 break;
1263 }
1264 }
1265 }
1266 }
1267 }
1268 else
1269 {
1270 /*
1271 * Read bytes from the file.
1272 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001273# ifdef FEAT_SODIUM
1274 // Let the crypt layer work with a buffer size of 8192
1275 if (filesize == 0)
1276 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001277 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001278 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1279 + crypto_secretstream_xchacha20poly1305_ABYTES;
1280
1281 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1282 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1283 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1284# endif
1285 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001286 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001287 filesize_count += size;
1288 // hit end of file
1289 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001292#ifdef FEAT_CRYPT
1293 /*
1294 * At start of file: Check for magic number of encryption.
1295 */
1296 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001297 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001298 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1299 &filesize, newfile, sfname,
1300 &did_ask_for_key);
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001301# ifdef CRYPT_NOT_INPLACE
1302 if (curbuf->b_cryptstate != NULL
1303 && !crypt_works_inplace(curbuf->b_cryptstate))
1304 // reading undo file requires crypt_decode_inplace()
1305 read_undo_file = FALSE;
1306# endif
1307 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 /*
1309 * Decrypt the read bytes. This is done before checking for
1310 * EOF because the crypt layer may be buffering.
1311 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001312 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1313 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001314 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001315# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001316 if (crypt_works_inplace(curbuf->b_cryptstate))
1317 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001318# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001319 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1320 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001321# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001322 }
1323 else
1324 {
1325 char_u *newptr = NULL;
1326 int decrypted_size;
1327
1328 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001329 curbuf->b_cryptstate, ptr, size,
1330 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001331
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001332 if (decrypted_size < 0)
1333 {
1334 // error message already given
1335 error = TRUE;
1336 vim_free(newptr);
1337 break;
1338 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001339 // If the crypt layer is buffering, not producing
1340 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001341 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001342 continue;
1343
1344 if (linerest == 0)
1345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001346 // Simple case: reuse returned buffer (may be
1347 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001348 new_buffer = newptr;
1349 }
1350 else
1351 {
1352 long_u new_size;
1353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001354 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001355 new_size = (long_u)(decrypted_size + linerest + 1);
1356 new_buffer = lalloc(new_size, FALSE);
1357 if (new_buffer == NULL)
1358 {
1359 do_outofmem_msg(new_size);
1360 error = TRUE;
1361 break;
1362 }
1363
1364 mch_memmove(new_buffer, buffer, linerest);
1365 if (newptr != NULL)
1366 mch_memmove(new_buffer + linerest, newptr,
1367 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001368 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001369 }
1370
1371 if (new_buffer != NULL)
1372 {
1373 vim_free(buffer);
1374 buffer = new_buffer;
1375 new_buffer = NULL;
1376 line_start = buffer;
1377 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001378 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001379 }
1380 size = decrypted_size;
1381 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001382# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001383 }
1384#endif
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (size <= 0)
1387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001388 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001391 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001392 /*
1393 * Reached end-of-file but some trailing bytes could
1394 * not be converted. Truncated file?
1395 */
1396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001398 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001399#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001400 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001401#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001402 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001403 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001404 if (can_retry)
1405 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001406 if (conv_error == 0)
1407 conv_error = curbuf->b_ml.ml_line_count
1408 - linecnt + 1;
1409 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001410 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001411 else if (illegal_byte == 0)
1412 illegal_byte = curbuf->b_ml.ml_line_count
1413 - linecnt + 1;
1414 if (bad_char_behavior == BAD_DROP)
1415 {
1416 *(ptr - conv_restlen) = NUL;
1417 conv_restlen = 0;
1418 }
1419 else
1420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001421 // Replace the trailing bytes with the replacement
1422 // character if we were converting; if we weren't,
1423 // leave the UTF8 checking code to do it, as it
1424 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001425 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001426#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001427 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001428#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001429 ))
1430 {
1431 while (conv_restlen > 0)
1432 {
1433 *(--ptr) = bad_char_behavior;
1434 --conv_restlen;
1435 }
1436 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001437 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001438#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001439 if (iconv_fd != (iconv_t)-1)
1440 {
1441 iconv_close(iconv_fd);
1442 iconv_fd = (iconv_t)-1;
1443 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001444#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001445 }
1446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449 skip_read = FALSE;
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 /*
1452 * At start of file (or after crypt magic number): Check for BOM.
1453 * Also check for a BOM for other Unicode encodings, but not after
1454 * converting with 'charconvert' or when a BOM has already been
1455 * found.
1456 */
1457 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001458#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001459 || (cryptkey != NULL
1460 && filesize == crypt_get_header_len(
1461 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 )
1464 && (fio_flags == FIO_UCSBOM
1465 || (!curbuf->b_p_bomb
1466 && tmpname == NULL
1467 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1468 {
1469 char_u *ccname;
1470 int blen;
1471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001472 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (size < 2 || curbuf->b_p_bin)
1474 ccname = NULL;
1475 else
1476 ccname = check_for_bom(ptr, size, &blen,
1477 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1478 if (ccname != NULL)
1479 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001480 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 filesize += blen;
1482 size -= blen;
1483 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001484 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001487 curbuf->b_start_bomb = TRUE;
1488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 }
1490
1491 if (fio_flags == FIO_UCSBOM)
1492 {
1493 if (ccname == NULL)
1494 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 advance_fenc = TRUE;
1497 }
1498 else
1499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (fenc_alloced)
1502 vim_free(fenc);
1503 fenc = ccname;
1504 fenc_alloced = FALSE;
1505 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001506 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 skip_read = TRUE;
1508 goto retry;
1509 }
1510 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001511
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001513 ptr -= conv_restlen;
1514 size += conv_restlen;
1515 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 /*
1517 * Break here for a read error or end-of-file.
1518 */
1519 if (size <= 0)
1520 break;
1521
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar13505972019-01-24 15:04:48 +01001523#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (iconv_fd != (iconv_t)-1)
1525 {
1526 /*
1527 * Attempt conversion of the read bytes to 'encoding' using
1528 * iconv().
1529 */
1530 const char *fromp;
1531 char *top;
1532 size_t from_size;
1533 size_t to_size;
1534
1535 fromp = (char *)ptr;
1536 from_size = size;
1537 ptr += size;
1538 top = (char *)ptr;
1539 to_size = real_size - size;
1540
1541 /*
1542 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001543 * another conversion. Except for when there is no
1544 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001546 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1547 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1549 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001550 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001551 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001552 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001553 if (conv_error == 0)
1554 conv_error = readfile_linenr(linecnt,
1555 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001558 ++fromp;
1559 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001560 if (bad_char_behavior == BAD_KEEP)
1561 {
1562 *top++ = *(fromp - 1);
1563 --to_size;
1564 }
1565 else if (bad_char_behavior != BAD_DROP)
1566 {
1567 *top++ = bad_char_behavior;
1568 --to_size;
1569 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 if (from_size > 0)
1573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001574 // Some remaining characters, keep them for the next
1575 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1577 conv_restlen = (int)from_size;
1578 }
1579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001580 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 line_start = ptr - linerest;
1582 mch_memmove(line_start, buffer, (size_t)linerest);
1583 size = (long)((char_u *)top - ptr);
1584 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaar4f974752019-02-17 17:44:42 +01001587#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (fio_flags & FIO_CODEPAGE)
1589 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001590 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001591 WCHAR ucs2buf[3];
1592 int ucs2len;
1593 int codepage = FIO_GET_CP(fio_flags);
1594 int bytelen;
1595 int found_bad;
1596 char replstr[2];
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /*
1599 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001600 * a codepage, using standard MS-Windows functions. This
1601 * requires two steps:
1602 * 1. convert from 'fileencoding' to ucs-2
1603 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001605 * Because there may be illegal bytes AND an incomplete byte
1606 * sequence at the end, we may have to do the conversion one
1607 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001610 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001611 if (bad_char_behavior > 0)
1612 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001614 replstr[0] = '?';
1615 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001618 * Move the bytes to the end of the buffer, so that we have
1619 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001621 src = ptr + real_size - size;
1622 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001624 /*
1625 * Do the conversion.
1626 */
1627 dst = ptr;
1628 size = size;
1629 while (size > 0)
1630 {
1631 found_bad = FALSE;
1632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001633# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001634 if (codepage == CP_UTF8)
1635 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001636 // Handle CP_UTF8 input ourselves to be able to handle
1637 // trailing bytes properly.
1638 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001639 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001640 if (bytelen > size)
1641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001642 // Only got some bytes of a character. Normally
1643 // it's put in "conv_rest", but if it's too long
1644 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001645 if (bytelen <= CONV_RESTLEN)
1646 break;
1647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001648 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001649 bytelen = size;
1650 found_bad = TRUE;
1651 }
1652 else
1653 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001654 int u8c = utf_ptr2char(src);
1655
Bram Moolenaar86e01082005-12-29 22:45:34 +00001656 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001657 found_bad = TRUE;
1658 ucs2buf[0] = u8c;
1659 ucs2len = 1;
1660 }
1661 }
1662 else
1663# endif
1664 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001665 // We don't know how long the byte sequence is, try
1666 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001667 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1668 ++bytelen)
1669 {
1670 ucs2len = MultiByteToWideChar(codepage,
1671 MB_ERR_INVALID_CHARS,
1672 (LPCSTR)src, bytelen,
1673 ucs2buf, 3);
1674 if (ucs2len > 0)
1675 break;
1676 }
1677 if (ucs2len == 0)
1678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // If we have only one byte then it's probably an
1680 // incomplete byte sequence. Otherwise discard
1681 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001682 if (size == 1)
1683 break;
1684 found_bad = TRUE;
1685 bytelen = 1;
1686 }
1687 }
1688
1689 if (!found_bad)
1690 {
1691 int i;
1692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001693 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001694 if (enc_utf8)
1695 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001696 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001697 for (i = 0; i < ucs2len; ++i)
1698 dst += utf_char2bytes(ucs2buf[i], dst);
1699 }
1700 else
1701 {
1702 BOOL bad = FALSE;
1703 int dstlen;
1704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001705 // From UCS-2 to "enc_codepage". If the
1706 // conversion uses the default character "?",
1707 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001708 dstlen = WideCharToMultiByte(enc_codepage, 0,
1709 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001710 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001711 replstr, &bad);
1712 if (bad)
1713 found_bad = TRUE;
1714 else
1715 dst += dstlen;
1716 }
1717 }
1718
1719 if (found_bad)
1720 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001721 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001722 if (can_retry)
1723 goto rewind_retry;
1724 if (conv_error == 0)
1725 conv_error = readfile_linenr(linecnt, ptr, dst);
1726 if (bad_char_behavior != BAD_DROP)
1727 {
1728 if (bad_char_behavior == BAD_KEEP)
1729 {
1730 mch_memmove(dst, src, bytelen);
1731 dst += bytelen;
1732 }
1733 else
1734 *dst++ = bad_char_behavior;
1735 }
1736 }
1737
1738 src += bytelen;
1739 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001741
1742 if (size > 0)
1743 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001744 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001745 mch_memmove(conv_rest, src, size);
1746 conv_restlen = size;
1747 }
1748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001749 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001750 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001753#endif
1754#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (fio_flags & FIO_MACROMAN)
1756 {
1757 /*
1758 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001759 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001761 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
1764 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (fio_flags != 0)
1767 {
1768 int u8c;
1769 char_u *dest;
1770 char_u *tail = NULL;
1771
1772 /*
1773 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1774 * "enc_utf8" not set: Convert Unicode to Latin1.
1775 * Go from end to start through the buffer, because the number
1776 * of bytes may increase.
1777 * "dest" points to after where the UTF-8 bytes go, "p" points
1778 * to after the next character to convert.
1779 */
1780 dest = ptr + real_size;
1781 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1782 {
1783 p = ptr + size;
1784 if (fio_flags == FIO_UTF8)
1785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001786 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 tail = ptr + size - 1;
1788 while (tail > ptr && (*tail & 0xc0) == 0x80)
1789 --tail;
1790 if (tail + utf_byte2len(*tail) <= ptr + size)
1791 tail = NULL;
1792 else
1793 p = tail;
1794 }
1795 }
1796 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1797 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 p = ptr + (size & ~1);
1800 if (size & 1)
1801 tail = p;
1802 if ((fio_flags & FIO_UTF16) && p > ptr)
1803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001804 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (fio_flags & FIO_ENDIAN_L)
1806 {
1807 u8c = (*--p << 8);
1808 u8c += *--p;
1809 }
1810 else
1811 {
1812 u8c = *--p;
1813 u8c += (*--p << 8);
1814 }
1815 if (u8c >= 0xd800 && u8c <= 0xdbff)
1816 tail = p;
1817 else
1818 p += 2;
1819 }
1820 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001821 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001823 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 p = ptr + (size & ~3);
1825 if (size & 3)
1826 tail = p;
1827 }
1828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001829 // If there is a trailing incomplete sequence move it to
1830 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (tail != NULL)
1832 {
1833 conv_restlen = (int)((ptr + size) - tail);
1834 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1835 size -= conv_restlen;
1836 }
1837
1838
1839 while (p > ptr)
1840 {
1841 if (fio_flags & FIO_LATIN1)
1842 u8c = *--p;
1843 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1844 {
1845 if (fio_flags & FIO_ENDIAN_L)
1846 {
1847 u8c = (*--p << 8);
1848 u8c += *--p;
1849 }
1850 else
1851 {
1852 u8c = *--p;
1853 u8c += (*--p << 8);
1854 }
1855 if ((fio_flags & FIO_UTF16)
1856 && u8c >= 0xdc00 && u8c <= 0xdfff)
1857 {
1858 int u16c;
1859
1860 if (p == ptr)
1861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001862 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (can_retry)
1864 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001865 if (conv_error == 0)
1866 conv_error = readfile_linenr(linecnt,
1867 ptr, p);
1868 if (bad_char_behavior == BAD_DROP)
1869 continue;
1870 if (bad_char_behavior != BAD_KEEP)
1871 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001874 // found second word of double-word, get the first
1875 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (fio_flags & FIO_ENDIAN_L)
1877 {
1878 u16c = (*--p << 8);
1879 u16c += *--p;
1880 }
1881 else
1882 {
1883 u16c = *--p;
1884 u16c += (*--p << 8);
1885 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001886 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1887 + (u8c & 0x3ff);
1888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001889 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (u16c < 0xd800 || u16c > 0xdbff)
1891 {
1892 if (can_retry)
1893 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001894 if (conv_error == 0)
1895 conv_error = readfile_linenr(linecnt,
1896 ptr, p);
1897 if (bad_char_behavior == BAD_DROP)
1898 continue;
1899 if (bad_char_behavior != BAD_KEEP)
1900 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904 else if (fio_flags & FIO_UCS4)
1905 {
1906 if (fio_flags & FIO_ENDIAN_L)
1907 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001908 u8c = (unsigned)*--p << 24;
1909 u8c += (unsigned)*--p << 16;
1910 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 u8c += *--p;
1912 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001913 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001916 u8c += (unsigned)*--p << 8;
1917 u8c += (unsigned)*--p << 16;
1918 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001921 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 if (*--p < 0x80)
1924 u8c = *p;
1925 else
1926 {
1927 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001928 p -= len;
1929 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (len == 0)
1931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // Not a valid UTF-8 character, retry with
1933 // another fenc when possible, otherwise just
1934 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (can_retry)
1936 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001937 if (conv_error == 0)
1938 conv_error = readfile_linenr(linecnt,
1939 ptr, p);
1940 if (bad_char_behavior == BAD_DROP)
1941 continue;
1942 if (bad_char_behavior != BAD_KEEP)
1943 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001947 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
1949 dest -= utf_char2len(u8c);
1950 (void)utf_char2bytes(u8c, dest);
1951 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 --dest;
1955 if (u8c >= 0x100)
1956 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001957 // character doesn't fit in latin1, retry with
1958 // another fenc when possible, otherwise just
1959 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001960 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001962 if (conv_error == 0)
1963 conv_error = readfile_linenr(linecnt, ptr, p);
1964 if (bad_char_behavior == BAD_DROP)
1965 ++dest;
1966 else if (bad_char_behavior == BAD_KEEP)
1967 *dest = u8c;
1968 else if (eap != NULL && eap->bad_char != 0)
1969 *dest = bad_char_behavior;
1970 else
1971 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973 else
1974 *dest = u8c;
1975 }
1976 }
1977
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 line_start = dest - linerest;
1980 mch_memmove(line_start, buffer, (size_t)linerest);
1981 size = (long)((ptr + real_size) - dest);
1982 ptr = dest;
1983 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001984 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001986 int incomplete_tail = FALSE;
1987
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001988 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001989 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001991 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001992 int l;
1993
1994 if (todo <= 0)
1995 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (*p >= 0x80)
1997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // A length of 1 means it's an illegal byte. Accept
1999 // an incomplete character at the end though, the next
2000 // read() will get the next bytes, we'll check it
2001 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002002 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002003 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002005 // Avoid retrying with a different encoding when
2006 // a truncated file is more likely, or attempting
2007 // to read the rest of an incomplete sequence when
2008 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002009 if (p > ptr || filesize > 0)
2010 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // Incomplete byte sequence, move it to conv_rest[]
2012 // and try to read the rest of it, unless we've
2013 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002014 if (p > ptr)
2015 {
2016 conv_restlen = todo;
2017 mch_memmove(conv_rest, p, conv_restlen);
2018 size -= conv_restlen;
2019 break;
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002022 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // Illegal byte. If we can try another encoding
2025 // do that, unless at EOF where a truncated
2026 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002027 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002028 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002029#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002031 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2032 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002033#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002035 if (conv_error == 0 && illegal_byte == 0)
2036 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002039 if (bad_char_behavior == BAD_DROP)
2040 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002041 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002042 --p;
2043 --size;
2044 }
2045 else if (bad_char_behavior != BAD_KEEP)
2046 *p = bad_char_behavior;
2047 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002048 else
2049 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002052 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002054 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002056 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002057#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002058 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002060 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002062#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002064 advance_fenc = TRUE;
2065 file_rewind = TRUE;
2066 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 filesize += size;
2072
2073 /*
2074 * when reading the first part of a file: guess EOL type
2075 */
2076 if (fileformat == EOL_UNKNOWN)
2077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (try_dos || try_unix)
2080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002081 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002082 if (try_mac)
2083 try_mac = 1;
2084
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 for (p = ptr; p < ptr + size; ++p)
2086 {
2087 if (*p == NL)
2088 {
2089 if (!try_unix
2090 || (try_dos && p > ptr && p[-1] == CAR))
2091 fileformat = EOL_DOS;
2092 else
2093 fileformat = EOL_UNIX;
2094 break;
2095 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002096 else if (*p == CAR && try_mac)
2097 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
2099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (fileformat == EOL_UNIX && try_mac)
2102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 try_mac = 1;
2105 try_unix = 1;
2106 for (; p >= ptr && *p != CAR; p--)
2107 ;
2108 if (p >= ptr)
2109 {
2110 for (p = ptr; p < ptr + size; ++p)
2111 {
2112 if (*p == NL)
2113 try_unix++;
2114 else if (*p == CAR)
2115 try_mac++;
2116 }
2117 if (try_mac > try_unix)
2118 fileformat = EOL_MAC;
2119 }
2120 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002121 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 // Looking for CR but found no end-of-line markers at
2123 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002124 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (fileformat == EOL_UNKNOWN && try_mac)
2129 fileformat = EOL_MAC;
2130
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (fileformat == EOL_UNKNOWN)
2133 fileformat = default_fileformat();
2134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002136 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 set_fileformat(fileformat, OPT_LOCAL);
2138 }
2139 }
2140
2141 /*
2142 * This loop is executed once for every character read.
2143 * Keep it fast!
2144 */
2145 if (fileformat == EOL_MAC)
2146 {
2147 --ptr;
2148 while (++ptr, --size >= 0)
2149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002150 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if ((c = *ptr) != NUL && c != CAR && c != NL)
2152 continue;
2153 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002154 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 else
2158 {
2159 if (skip_count == 0)
2160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 len = (colnr_T) (ptr - line_start + 1);
2163 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2164 {
2165 error = TRUE;
2166 break;
2167 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002168#ifdef FEAT_PERSISTENT_UNDO
2169 if (read_undo_file)
2170 sha256_update(&sha_ctx, line_start, len);
2171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 ++lnum;
2173 if (--read_count == 0)
2174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002175 error = TRUE; // break loop
2176 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 break;
2178 }
2179 }
2180 else
2181 --skip_count;
2182 line_start = ptr + 1;
2183 }
2184 }
2185 }
2186 else
2187 {
2188 --ptr;
2189 while (++ptr, --size >= 0)
2190 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002191 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 continue;
2193 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 else
2196 {
2197 if (skip_count == 0)
2198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002199 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 len = (colnr_T)(ptr - line_start + 1);
2201 if (fileformat == EOL_DOS)
2202 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002203 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002205 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 ptr[-1] = NUL;
2207 --len;
2208 }
2209 /*
2210 * Reading in Dos format, but no CR-LF found!
2211 * When 'fileformats' includes "unix", delete all
2212 * the lines read so far and start all over again.
2213 * Otherwise give an error message later.
2214 */
2215 else if (ff_error != EOL_DOS)
2216 {
2217 if ( try_unix
2218 && !read_stdin
2219 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002220 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2221 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002224 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 set_fileformat(EOL_UNIX, OPT_LOCAL);
2226 file_rewind = TRUE;
2227 keep_fileformat = TRUE;
2228 goto retry;
2229 }
2230 ff_error = EOL_DOS;
2231 }
2232 }
2233 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2234 {
2235 error = TRUE;
2236 break;
2237 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002238#ifdef FEAT_PERSISTENT_UNDO
2239 if (read_undo_file)
2240 sha256_update(&sha_ctx, line_start, len);
2241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ++lnum;
2243 if (--read_count == 0)
2244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 error = TRUE; // break loop
2246 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 break;
2248 }
2249 }
2250 else
2251 --skip_count;
2252 line_start = ptr + 1;
2253 }
2254 }
2255 }
2256 linerest = (long)(ptr - line_start);
2257 ui_breakcheck();
2258 }
2259
2260failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (error && read_count == 0)
2263 error = FALSE;
2264
2265 /*
2266 * If we get EOF in the middle of a line, note the fact and
2267 * complete the line ourselves.
2268 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2269 */
2270 if (!error
2271 && !got_int
2272 && linerest != 0
2273 && !(!curbuf->b_p_bin
2274 && fileformat == EOL_DOS
2275 && *line_start == Ctrl_Z
2276 && ptr == line_start + 1))
2277 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002278 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 curbuf->b_p_eol = FALSE;
2281 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002282 len = (colnr_T)(ptr - line_start + 1);
2283 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 error = TRUE;
2285 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002286 {
2287#ifdef FEAT_PERSISTENT_UNDO
2288 if (read_undo_file)
2289 sha256_update(&sha_ctx, line_start, len);
2290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002295 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002296 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002299 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002300 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002301 crypt_free_state(curbuf->b_cryptstate);
2302 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002303 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002304 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2305 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002306 // Don't set cryptkey to NULL, it's used below as a flag that
2307 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
2309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // If editing a new file: set 'fenc' for the current buffer.
2311 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002312 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002314 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (fenc_alloced)
2316 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002317#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321
2322 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002323 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002324#ifdef HAVE_FD_CLOEXEC
2325 else
2326 {
2327 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002328
Bram Moolenaarf05da212009-11-17 16:13:15 +00002329 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002330 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002331 }
2332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 vim_free(buffer);
2334
2335#ifdef HAVE_DUP
2336 if (read_stdin)
2337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002340 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342#endif
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (tmpname != NULL)
2345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 vim_free(tmpname);
2348 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002349 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /*
2352 * In recovery mode everything but autocommands is skipped.
2353 */
2354 if (!recoverymode)
2355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2358 {
2359#ifdef FEAT_NETBEANS_INTG
2360 netbeansFireChanges = 0;
2361#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002362 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_NETBEANS_INTG
2364 netbeansFireChanges = 1;
2365#endif
2366 --linecnt;
2367 }
2368 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2369 if (filesize == 0)
2370 linecnt = 0;
2371 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002374#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 // After reading the text into the buffer the diff info needs to
2376 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002377 diff_invalidate(curbuf);
2378#endif
2379#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002380 // All folds in the window are invalid now. Mark them for update
2381 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002382 foldUpdateAll(curwin);
2383#endif
2384 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002385 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 appended_lines_mark(from, linecnt);
2387
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#ifndef ALWAYS_USE_GUI
2389 /*
2390 * If we were reading from the same terminal as where messages go,
2391 * the screen will have been messed up.
2392 * Switch on raw mode now and clear the screen.
2393 */
2394 if (read_stdin)
2395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002396 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 starttermcap();
2398 screenclear();
2399 }
2400#endif
2401
2402 if (got_int)
2403 {
2404 if (!(flags & READ_DUMMY))
2405 {
2406 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2407 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002408 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 msg_scroll = msg_save;
2411#ifdef FEAT_VIMINFO
2412 check_marks_read();
2413#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002414 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416
2417 if (!filtering && !(flags & READ_DUMMY))
2418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002419 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 c = FALSE;
2421
2422#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
2425 STRCAT(IObuff, _("[fifo]"));
2426 c = TRUE;
2427 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002428 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 STRCAT(IObuff, _("[socket]"));
2431 c = TRUE;
2432 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002433# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002435 {
2436 STRCAT(IObuff, _("[character special]"));
2437 c = TRUE;
2438 }
2439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#endif
2441 if (curbuf->b_p_ro)
2442 {
2443 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2444 c = TRUE;
2445 }
2446 if (read_no_eol_lnum)
2447 {
2448 msg_add_eol();
2449 c = TRUE;
2450 }
2451 if (ff_error == EOL_DOS)
2452 {
2453 STRCAT(IObuff, _("[CR missing]"));
2454 c = TRUE;
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (split)
2457 {
2458 STRCAT(IObuff, _("[long lines split]"));
2459 c = TRUE;
2460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (notconverted)
2462 {
2463 STRCAT(IObuff, _("[NOT converted]"));
2464 c = TRUE;
2465 }
2466 else if (converted)
2467 {
2468 STRCAT(IObuff, _("[converted]"));
2469 c = TRUE;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#ifdef FEAT_CRYPT
2472 if (cryptkey != NULL)
2473 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002474 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 c = TRUE;
2476 }
2477#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002478 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002480 sprintf((char *)IObuff + STRLEN(IObuff),
2481 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 c = TRUE;
2483 }
2484 else if (illegal_byte > 0)
2485 {
2486 sprintf((char *)IObuff + STRLEN(IObuff),
2487 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2488 c = TRUE;
2489 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002490 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 STRCAT(IObuff, _("[READ ERRORS]"));
2493 c = TRUE;
2494 }
2495 if (msg_add_fileformat(fileformat))
2496 c = TRUE;
2497#ifdef FEAT_CRYPT
2498 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002499 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002500 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 else
2502#endif
2503 msg_add_lines(c, (long)linecnt, filesize);
2504
Bram Moolenaard23a8232018-02-10 18:45:26 +01002505 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 msg_scrolled_ign = TRUE;
2507#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002508 // Don't show the message when reading stdin, it would end up in a
2509 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (read_stdin || read_buffer)
2511 p = msg_may_trunc(FALSE, IObuff);
2512 else
2513#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002514 {
2515 if (msg_col > 0)
2516 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002517 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002520 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002521 // Need to repeat the message after redrawing when:
2522 // - When reading from stdin (the screen will be cleared next).
2523 // - When restart_edit is set (otherwise there will be a delay
2524 // before redrawing).
2525 // - When the screen was scrolled but there is no wait-return
2526 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002527 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 msg_scrolled_ign = FALSE;
2529 }
2530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002531 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002533 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002534 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 curbuf->b_p_ro = TRUE;
2536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002537 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539 /*
2540 * In Ex mode: cursor at last new line.
2541 * Otherwise: cursor at first new line.
2542 */
2543 if (exmode_active)
2544 curwin->w_cursor.lnum = from + linecnt;
2545 else
2546 curwin->w_cursor.lnum = from + 1;
2547 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002548 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaare1004402020-10-24 20:49:43 +02002550 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002551 {
2552 // Set '[ and '] marks to the newly read lines.
2553 curbuf->b_op_start.lnum = from + 1;
2554 curbuf->b_op_start.col = 0;
2555 curbuf->b_op_end.lnum = from + linecnt;
2556 curbuf->b_op_end.col = 0;
2557 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002558
Bram Moolenaar4f974752019-02-17 17:44:42 +01002559#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002560 /*
2561 * Work around a weird problem: When a file has two links (only
2562 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002563 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002564 * It's correct again after reading the file, thus reset the timestamp
2565 * here.
2566 */
2567 if (newfile && !read_stdin && !read_buffer
2568 && mch_stat((char *)fname, &st) >= 0)
2569 {
2570 buf_store_time(curbuf, &st, fname);
2571 curbuf->b_mtime_read = curbuf->b_mtime;
2572 }
2573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 msg_scroll = msg_save;
2576
2577#ifdef FEAT_VIMINFO
2578 /*
2579 * Get the marks before executing autocommands, so they can be used there.
2580 */
2581 check_marks_read();
2582#endif
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002585 * We remember if the last line of the read didn't have
2586 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2587 * or writing the read again with 'binary' on. The latter is required
2588 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002590 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002592 // When reloading a buffer put the cursor at the first line that is
2593 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002594 if (flags & READ_KEEP_UNDO)
2595 u_find_first_changed();
2596
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002597#ifdef FEAT_PERSISTENT_UNDO
2598 /*
2599 * When opening a new file locate undo info and read it.
2600 */
2601 if (read_undo_file)
2602 {
2603 char_u hash[UNDO_HASH_SIZE];
2604
2605 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002606 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002607 }
2608#endif
2609
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002610 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 int m = msg_scroll;
2613 int n = msg_scrolled;
2614
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002615 // Save the fileformat now, otherwise the buffer will be considered
2616 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002617 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 save_file_ff(curbuf);
2619
2620 /*
2621 * The output from the autocommands should not overwrite anything and
2622 * should not be overwritten: Set msg_scroll, restore its value if no
2623 * output was done.
2624 */
2625 msg_scroll = TRUE;
2626 if (filtering)
2627 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2628 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002629 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2632 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002633 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2634 /*
2635 * EVENT_FILETYPE was not triggered but the buffer already has a
2636 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2637 */
2638 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2639 TRUE, curbuf);
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 else
2642 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2643 FALSE, NULL, eap);
2644 if (msg_scrolled == n)
2645 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002646# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002647 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 if (recoverymode && error)
2653 return FAIL;
2654 return OK;
2655}
2656
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002657#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002658/*
2659 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2660 * which is the name of files used for process substitution output by
2661 * some shells on some operating systems, e.g., bash on SunOS.
2662 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2663 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002664 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002665is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002666{
2667 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2668 && VIM_ISDIGIT(fname[8])
2669 && *skipdigits(fname + 9) == NUL
2670 && (fname[9] != NUL
2671 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2672}
2673#endif
2674
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002675/*
2676 * From the current line count and characters read after that, estimate the
2677 * line number where we are now.
2678 * Used for error messages that include a line number.
2679 */
2680 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002681readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002682 linenr_T linecnt, // line count before reading more bytes
2683 char_u *p, // start of more bytes read
2684 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002685{
2686 char_u *s;
2687 linenr_T lnum;
2688
2689 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2690 for (s = p; s < endp; ++s)
2691 if (*s == '\n')
2692 ++lnum;
2693 return lnum;
2694}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002697 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2698 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 * Returns OK or FAIL.
2700 */
2701 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002702prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
Bram Moolenaar13505972019-01-24 15:04:48 +01002704 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (eap->cmd == NULL)
2706 return FAIL;
2707
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002708 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2709 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002710 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002711 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002712
2713 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002714 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002715 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 return OK;
2717}
2718
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002719/*
2720 * Set default or forced 'fileformat' and 'binary'.
2721 */
2722 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002723set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002724{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002725 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002726 if (set_options)
2727 {
2728 if (eap != NULL && eap->force_ff != 0)
2729 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2730 else if (*p_ffs != NUL)
2731 set_fileformat(default_fileformat(), OPT_LOCAL);
2732 }
2733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002734 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002735 if (eap != NULL && eap->force_bin != 0)
2736 {
2737 int oldval = curbuf->b_p_bin;
2738
2739 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2740 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2741 }
2742}
2743
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002744/*
2745 * Set forced 'fileencoding'.
2746 */
2747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002748set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002749{
2750 if (eap->force_enc != 0)
2751 {
2752 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2753
2754 if (fenc != NULL)
2755 set_string_option_direct((char_u *)"fenc", -1,
2756 fenc, OPT_FREE|OPT_LOCAL, 0);
2757 vim_free(fenc);
2758 }
2759}
2760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761/*
2762 * Find next fileencoding to use from 'fileencodings'.
2763 * "pp" points to fenc_next. It's advanced to the next item.
2764 * When there are no more items, an empty string is returned and *pp is set to
2765 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002766 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2767 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 */
2769 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002770next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 char_u *p;
2773 char_u *r;
2774
Bram Moolenaarf077db22019-08-13 00:18:24 +02002775 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (**pp == NUL)
2777 {
2778 *pp = NULL;
2779 return (char_u *)"";
2780 }
2781 p = vim_strchr(*pp, ',');
2782 if (p == NULL)
2783 {
2784 r = enc_canonize(*pp);
2785 *pp += STRLEN(*pp);
2786 }
2787 else
2788 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002789 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 *pp = p + 1;
2791 if (r != NULL)
2792 {
2793 p = enc_canonize(r);
2794 vim_free(r);
2795 r = p;
2796 }
2797 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002798 if (r != NULL)
2799 *alloced = TRUE;
2800 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002802 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 r = (char_u *)"";
2804 *pp = NULL;
2805 }
2806 return r;
2807}
2808
Bram Moolenaar13505972019-01-24 15:04:48 +01002809#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810/*
2811 * Convert a file with the 'charconvert' expression.
2812 * This closes the file which is to be read, converts it and opens the
2813 * resulting file for reading.
2814 * Returns name of the resulting converted file (the caller should delete it
2815 * after reading it).
2816 * Returns NULL if the conversion failed ("*fdp" is not set) .
2817 */
2818 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002819readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002820 char_u *fname, // name of input file
2821 char_u *fenc, // converted from
2822 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002825 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002827 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002829 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 else
2831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002832 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 *fdp = -1;
2834 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2835 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002836 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2838 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002839 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841
2842 if (errmsg != NULL)
2843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002844 // Don't use emsg(), it breaks mappings, the retry with
2845 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002846 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (tmpname != NULL)
2848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002849 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002850 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852 }
2853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002854 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (*fdp < 0)
2856 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2857
2858 return tmpname;
2859}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#endif
2861
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002862#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002864 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2866 * *filesizep are updated.
2867 * Return the (new) encryption key, NULL for no encryption.
2868 */
2869 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002870check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002871 char_u *cryptkey, // previous encryption key or NULL
2872 char_u *ptr, // pointer to read bytes
2873 long *sizep, // length of read bytes
2874 off_T *filesizep, // nr of bytes used from file
2875 int newfile, // editing a new buffer
2876 char_u *fname, // file name to display
2877 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002879 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002880 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002881
2882 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002884 // Mark the buffer as read-only until the decryption has taken place.
2885 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002886 curbuf->b_p_ro = TRUE;
2887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002888 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002889 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002890 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
2892 if (*curbuf->b_p_key)
2893 cryptkey = curbuf->b_p_key;
2894 else
2895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002896 // When newfile is TRUE, store the typed key in the 'key'
2897 // option and don't free it. bf needs hash of the key saved.
2898 // Don't ask for the key again when first time Enter was hit.
2899 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002900 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002901 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002902 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002903 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002904 *did_ask = TRUE;
2905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002906 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 if (cryptkey != NULL && *cryptkey == NUL)
2908 {
2909 if (cryptkey != curbuf->b_p_key)
2910 vim_free(cryptkey);
2911 cryptkey = NULL;
2912 }
2913 }
2914 }
2915
2916 if (cryptkey != NULL)
2917 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002918 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002919
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002920 curbuf->b_cryptstate = crypt_create_from_header(
2921 method, cryptkey, ptr);
2922 crypt_set_cm_option(curbuf, method);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002924 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002925 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002926 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002927 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002928 return NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002929 *filesizep += header_len;
2930 *sizep -= header_len;
2931 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002933 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002934 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // When starting to edit a new file which does not have encryption, clear
2938 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002939 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2941
2942 return cryptkey;
2943}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002944#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002947 * Return TRUE if a file appears to be read-only from the file permissions.
2948 */
2949 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002950check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002951 char_u *fname, // full path to file
2952 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002953{
2954#ifndef USE_MCH_ACCESS
2955 int fd = 0;
2956#endif
2957
2958 return (
2959#ifdef USE_MCH_ACCESS
2960# ifdef UNIX
2961 (perm & 0222) == 0 ||
2962# endif
2963 mch_access((char *)fname, W_OK)
2964#else
2965 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2966 ? TRUE : (close(fd), FALSE)
2967#endif
2968 );
2969}
2970
Bram Moolenaara7870192019-02-14 12:56:36 +01002971#if defined(HAVE_FSYNC) || defined(PROTO)
2972/*
2973 * Call fsync() with Mac-specific exception.
2974 * Return fsync() result: zero for success.
2975 */
2976 int
2977vim_fsync(int fd)
2978{
2979 int r;
2980
2981# ifdef MACOS_X
2982 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002983 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002984# endif
2985 r = fsync(fd);
2986 return r;
2987}
2988#endif
2989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002991 * Set the name of the current buffer. Use when the buffer doesn't have a
2992 * name and a ":r" or ":w" command with a file name is used.
2993 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002994 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002996{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00002997 buf_T *buf = curbuf;
2998
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002999 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003000 if (curbuf->b_p_bl)
3001 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3002 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003003#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003004 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003005 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003006#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003007 if (curbuf != buf)
3008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003009 // We are in another buffer now, don't do the renaming.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003010 emsg(_(e_auchangedbuf));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003011 return FAIL;
3012 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003013
3014 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3015 curbuf->b_flags |= BF_NOTEDITED;
3016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003017 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003018 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3019 if (curbuf->b_p_bl)
3020 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003021#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003022 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003023 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003024#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003026 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003027 if (*curbuf->b_p_ft == NUL)
3028 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003029 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003030 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003031 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003032 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003033
3034 return OK;
3035}
3036
3037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * Put file name into IObuff with quotes.
3039 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003041msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 if (fname == NULL)
3044 fname = (char_u *)"-stdin-";
3045 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3046 IObuff[0] = '"';
3047 STRCAT(IObuff, "\" ");
3048}
3049
3050/*
3051 * Append message for text mode to IObuff.
3052 * Return TRUE if something appended.
3053 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003055msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057#ifndef USE_CRNL
3058 if (eol_type == EOL_DOS)
3059 {
3060 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3061 return TRUE;
3062 }
3063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 if (eol_type == EOL_MAC)
3065 {
3066 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3067 return TRUE;
3068 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003069#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (eol_type == EOL_UNIX)
3071 {
3072 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3073 return TRUE;
3074 }
3075#endif
3076 return FALSE;
3077}
3078
3079/*
3080 * Append line and character count to IObuff.
3081 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003082 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003083msg_add_lines(
3084 int insert_space,
3085 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003086 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 char_u *p;
3089
3090 p = IObuff + STRLEN(IObuff);
3091
3092 if (insert_space)
3093 *p++ = ' ';
3094 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003095 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003096 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
3098 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003099 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003101 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003102 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003103 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105}
3106
3107/*
3108 * Append message for missing line separator to IObuff.
3109 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003111msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3114}
3115
Bram Moolenaar473952e2019-09-28 16:30:04 +02003116 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003117time_differs(long t1, long t2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003119#if defined(__linux__) || defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003120 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3121 // the seconds. Since the roundoff is done when flushing the inode, the
3122 // time may change unexpectedly by one second!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return (t1 - t2 > 1 || t2 - t1 > 1);
3124#else
3125 return (t1 != t2);
3126#endif
3127}
3128
3129/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003130 * Return TRUE if file encoding "fenc" requires conversion from or to
3131 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003133 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003134need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003136 int same_encoding;
3137 int enc_flags;
3138 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003140 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003141 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003142 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003143 fenc_flags = 0;
3144 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003145 else
3146 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003147 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3148 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003149 enc_flags = get_fio_flags(p_enc);
3150 fenc_flags = get_fio_flags(fenc);
3151 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3152 }
3153 if (same_encoding)
3154 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003155 // Specified encoding matches with 'encoding'. This requires
3156 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003157 return enc_unicode != 0;
3158 }
3159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003160 // Encodings differ. However, conversion is not needed when 'enc' is any
3161 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003162 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163}
3164
3165/*
3166 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3167 * internal conversion.
3168 * if "ptr" is an empty string, use 'encoding'.
3169 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003170 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 int prop;
3174
3175 if (*ptr == NUL)
3176 ptr = p_enc;
3177
3178 prop = enc_canon_props(ptr);
3179 if (prop & ENC_UNICODE)
3180 {
3181 if (prop & ENC_2BYTE)
3182 {
3183 if (prop & ENC_ENDIAN_L)
3184 return FIO_UCS2 | FIO_ENDIAN_L;
3185 return FIO_UCS2;
3186 }
3187 if (prop & ENC_4BYTE)
3188 {
3189 if (prop & ENC_ENDIAN_L)
3190 return FIO_UCS4 | FIO_ENDIAN_L;
3191 return FIO_UCS4;
3192 }
3193 if (prop & ENC_2WORD)
3194 {
3195 if (prop & ENC_ENDIAN_L)
3196 return FIO_UTF16 | FIO_ENDIAN_L;
3197 return FIO_UTF16;
3198 }
3199 return FIO_UTF8;
3200 }
3201 if (prop & ENC_LATIN1)
3202 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003203 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 return 0;
3205}
3206
Bram Moolenaar473952e2019-09-28 16:30:04 +02003207#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208/*
3209 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3210 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3211 * Used for conversion between 'encoding' and 'fileencoding'.
3212 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003213 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003214get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 int cp;
3217
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003218 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (!enc_utf8 && enc_codepage <= 0)
3220 return 0;
3221
3222 cp = encname2codepage(ptr);
3223 if (cp == 0)
3224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003225# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (STRCMP(ptr, "utf-8") == 0)
3227 cp = CP_UTF8;
3228 else
3229# endif
3230 return 0;
3231 }
3232 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3233}
3234#endif
3235
Bram Moolenaar473952e2019-09-28 16:30:04 +02003236#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237/*
3238 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3239 * needed for the internal conversion to/from utf-8 or latin1.
3240 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003241 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003242get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
3244 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3245 && (enc_canon_props(ptr) & ENC_MACROMAN))
3246 return FIO_MACROMAN;
3247 return 0;
3248}
3249#endif
3250
3251/*
3252 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3253 * "size" must be at least 2.
3254 * Return the name of the encoding and set "*lenp" to the length.
3255 * Returns NULL when no BOM found.
3256 */
3257 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003258check_for_bom(
3259 char_u *p,
3260 long size,
3261 int *lenp,
3262 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 char *name = NULL;
3265 int len = 2;
3266
3267 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003268 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003270 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 len = 3;
3272 }
3273 else if (p[0] == 0xff && p[1] == 0xfe)
3274 {
3275 if (size >= 4 && p[2] == 0 && p[3] == 0
3276 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3277 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003278 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 len = 4;
3280 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003281 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003282 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003283 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003284 // utf-16le is preferred, it also works for ucs-2le text
3285 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287 else if (p[0] == 0xfe && p[1] == 0xff
3288 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3289 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003290 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003291 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003292 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003293 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003294 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3297 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003299 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 len = 4;
3301 }
3302
3303 *lenp = len;
3304 return (char_u *)name;
3305}
3306
3307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 * Try to find a shortname by comparing the fullname with the current
3309 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003310 * Returns "full_path" or pointer into "full_path" if shortened.
3311 */
3312 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003313shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003314{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003315 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003316 char_u *p = full_path;
3317
Bram Moolenaard9462e32011-04-11 21:35:11 +02003318 dirname = alloc(MAXPATHL);
3319 if (dirname == NULL)
3320 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003321 if (mch_dirname(dirname, MAXPATHL) == OK)
3322 {
3323 p = shorten_fname(full_path, dirname);
3324 if (p == NULL || *p == NUL)
3325 p = full_path;
3326 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003327 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003328 return p;
3329}
3330
3331/*
3332 * Try to find a shortname by comparing the fullname with the current
3333 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * Returns NULL if not shorter name possible, pointer into "full_path"
3335 * otherwise.
3336 */
3337 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003338shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int len;
3341 char_u *p;
3342
3343 if (full_path == NULL)
3344 return NULL;
3345 len = (int)STRLEN(dir_name);
3346 if (fnamencmp(dir_name, full_path, len) == 0)
3347 {
3348 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003349#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003351 * MS-Windows: when a file is in the root directory, dir_name will end
3352 * in a slash, since C: by itself does not define a specific dir. In
3353 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 */
3355 if (!((len > 2) && (*(p - 2) == ':')))
3356#endif
3357 {
3358 if (vim_ispathsep(*p))
3359 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003360#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else
3362 p = NULL;
3363#endif
3364 }
3365 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003366#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 /*
3368 * When using a file in the current drive, remove the drive name:
3369 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3370 * a floppy from "A:\dir" to "B:\dir".
3371 */
3372 else if (len > 3
3373 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3374 && full_path[1] == ':'
3375 && vim_ispathsep(full_path[2]))
3376 p = full_path + 2;
3377#endif
3378 else
3379 p = NULL;
3380 return p;
3381}
3382
3383/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003384 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * When "force" is TRUE: Use full path from now on for files currently being
3386 * edited, both for file name and swap file name. Try to shorten the file
3387 * names a bit, if safe to do so.
3388 * When "force" is FALSE: Only try to shorten absolute file names.
3389 * For buffers that have buftype "nofile" or "scratch": never change the file
3390 * name.
3391 */
3392 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003393shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3394{
3395 char_u *p;
3396
3397 if (buf->b_fname != NULL
3398#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003399 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003400#endif
3401 && !path_with_url(buf->b_fname)
3402 && (force
3403 || buf->b_sfname == NULL
3404 || mch_isFullName(buf->b_sfname)))
3405 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003406 if (buf->b_sfname != buf->b_ffname)
3407 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003408 p = shorten_fname(buf->b_ffname, dirname);
3409 if (p != NULL)
3410 {
3411 buf->b_sfname = vim_strsave(p);
3412 buf->b_fname = buf->b_sfname;
3413 }
3414 if (p == NULL || buf->b_fname == NULL)
3415 buf->b_fname = buf->b_ffname;
3416 }
3417}
3418
3419/*
3420 * Shorten filenames for all buffers.
3421 */
3422 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003423shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 char_u dirname[MAXPATHL];
3426 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003429 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003431 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003433 // Always make the swap file name a full path, a "nofile" buffer may
3434 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003435 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003438 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003439#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003440 popup_update_preview_title();
3441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3445 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003446 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 || defined(PROTO)
3448/*
3449 * Shorten all filenames in "fnames[count]" by current directory.
3450 */
3451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 int i;
3455 char_u dirname[MAXPATHL];
3456 char_u *p;
3457
3458 if (fnames == NULL || count < 1)
3459 return;
3460 mch_dirname(dirname, sizeof(dirname));
3461 for (i = 0; i < count; ++i)
3462 {
3463 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003465 // shorten_fname() returns pointer in given "fnames[i]". If free
3466 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3467 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 p = vim_strsave(p);
3469 vim_free(fnames[i]);
3470 fnames[i] = p;
3471 }
3472 }
3473}
3474#endif
3475
3476/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003477 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 * fo_o_h.ext for MSDOS or when shortname option set.
3479 *
3480 * Assumed that fname is a valid name found in the filesystem we assure that
3481 * the return value is a different name and ends in 'ext'.
3482 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3483 * characters otherwise.
3484 * Space for the returned name is allocated, must be freed later.
3485 * Returns NULL when out of memory.
3486 */
3487 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003488modname(
3489 char_u *fname,
3490 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003491 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003493 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 fname, ext, prepend_dot);
3495}
3496
3497 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003498buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003499 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003500 char_u *fname,
3501 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003502 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 char_u *retval;
3505 char_u *s;
3506 char_u *e;
3507 char_u *ptr;
3508 int fnamelen, extlen;
3509
3510 extlen = (int)STRLEN(ext);
3511
3512 /*
3513 * If there is no file name we must get the name of the current directory
3514 * (we need the full path in case :cd is used).
3515 */
3516 if (fname == NULL || *fname == NUL)
3517 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003518 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (retval == NULL)
3520 return NULL;
3521 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3522 (fnamelen = (int)STRLEN(retval)) == 0)
3523 {
3524 vim_free(retval);
3525 return NULL;
3526 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003527 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 retval[fnamelen++] = PATHSEP;
3530 retval[fnamelen] = NUL;
3531 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003532 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 else
3535 {
3536 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003537 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (retval == NULL)
3539 return NULL;
3540 STRCPY(retval, fname);
3541#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003542 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543#endif
3544 }
3545
3546 /*
3547 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3548 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3549 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3550 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3551 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003552 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003554 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003555 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003558 {
3559 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003564 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3566 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 s = ptr + STRLEN(ptr);
3569
3570 /*
3571 * For 8.3 file names we may have to reduce the length.
3572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 /*
3576 * If there is no file name, or the file name ends in '/', and the
3577 * extension starts with '.', put a '_' before the dot, because just
3578 * ".ext" is invalid.
3579 */
3580 if (fname == NULL || *fname == NUL
3581 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 *s++ = '_';
3585 }
3586 /*
3587 * If the extension starts with '.', truncate the base name at 8
3588 * characters
3589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003592 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 s = ptr + 8;
3595 *s = '\0';
3596 }
3597 }
3598 /*
3599 * If the extension doesn't start with '.', and the file name
3600 * doesn't have an extension yet, append a '.'
3601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else if ((e = vim_strchr(ptr, '.')) == NULL)
3603 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 /*
3605 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003606 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 */
3608 else if ((int)STRLEN(e) + extlen > 4)
3609 s = e + 4 - extlen;
3610 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003611#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 /*
3613 * If there is no file name, and the extension starts with '.', put a
3614 * '_' before the dot, because just ".ext" may be invalid if it's on a
3615 * FAT partition, and on HPFS it doesn't matter.
3616 */
3617 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3618 *s++ = '_';
3619#endif
3620
3621 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003622 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 * ext can start with '.' and cannot exceed 3 more characters.
3624 */
3625 STRCPY(s, ext);
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 /*
3628 * Prepend the dot.
3629 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003630 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003632 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 /*
3637 * Check that, after appending the extension, the file name is really
3638 * different.
3639 */
3640 if (fname != NULL && STRCMP(fname, retval) == 0)
3641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003642 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 while (--s >= ptr)
3644 {
3645 if (*s != '_')
3646 {
3647 *s = '_';
3648 break;
3649 }
3650 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 *ptr = 'v';
3653 }
3654 return retval;
3655}
3656
3657/*
3658 * Like fgets(), but if the file line is too long, it is truncated and the
3659 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003660 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 */
3662 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003663vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 char *eof;
3666#define FGETS_SIZE 200
3667 char tbuf[FGETS_SIZE];
3668
3669 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3672 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003673 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003675 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 do
3677 {
3678 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003679 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3681 }
3682 return (eof == NULL);
3683}
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685/*
3686 * rename() only works if both files are on the same file system, this
3687 * function will (attempts to?) copy the file across if rename fails -- webb
3688 * Return -1 for failure, 0 for success.
3689 */
3690 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003691vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
3693 int fd_in;
3694 int fd_out;
3695 int n;
3696 char *errmsg = NULL;
3697 char *buffer;
3698#ifdef AMIGA
3699 BPTR flock;
3700#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003701 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003702 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003703#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003704 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003705#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003706 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003709 * When the names are identical, there is nothing to do. When they refer
3710 * to the same file (ignoring case and slash/backslash differences) but
3711 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 */
3713 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003714 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003715 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003716 use_tmp_file = TRUE;
3717 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003718 return 0;
3719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 /*
3722 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3723 */
3724 if (mch_stat((char *)from, &st) < 0)
3725 return -1;
3726
Bram Moolenaar3576da72008-12-30 15:15:57 +00003727#ifdef UNIX
3728 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003729 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 // It's possible for the source and destination to be the same file.
3732 // This happens when "from" and "to" differ in case and are on a FAT32
3733 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003734 if (mch_stat((char *)to, &st_to) >= 0
3735 && st.st_dev == st_to.st_dev
3736 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003737 use_tmp_file = TRUE;
3738 }
3739#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003740#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003741 {
3742 BY_HANDLE_FILE_INFORMATION info1, info2;
3743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003744 // It's possible for the source and destination to be the same file.
3745 // In that case go through a temp file name. This makes rename("foo",
3746 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003747 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3748 && win32_fileinfo(to, &info2) == FILEINFO_OK
3749 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3750 && info1.nFileIndexHigh == info2.nFileIndexHigh
3751 && info1.nFileIndexLow == info2.nFileIndexLow)
3752 use_tmp_file = TRUE;
3753 }
3754#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003755
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003756 if (use_tmp_file)
3757 {
3758 char tempname[MAXPATHL + 1];
3759
3760 /*
3761 * Find a name that doesn't exist and is in the same directory.
3762 * Rename "from" to "tempname" and then rename "tempname" to "to".
3763 */
3764 if (STRLEN(from) >= MAXPATHL - 5)
3765 return -1;
3766 STRCPY(tempname, from);
3767 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003768 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003769 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3770 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003771 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003772 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003773 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003774 if (mch_rename(tempname, (char *)to) == 0)
3775 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003776 // Strange, the second step failed. Try moving the
3777 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003778 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003779 return -1;
3780 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003781 // If it fails for one temp name it will most likely fail
3782 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003783 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003784 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003785 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003786 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003787 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 /*
3790 * Delete the "to" file, this is required on some systems to make the
3791 * mch_rename() work, on other systems it makes sure that we don't have
3792 * two files when the mch_rename() fails.
3793 */
3794
3795#ifdef AMIGA
3796 /*
3797 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3798 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003799 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * deleting the "from" file (horror!) we lock it during the remove.
3801 *
3802 * When used for making a backup before writing the file: This should not
3803 * happen with ":w", because startscript() should detect this problem and
3804 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3805 * name. This problem does exist with ":w filename", but then the
3806 * original file will be somewhere else so the backup isn't really
3807 * important. If autoscripting is off the rename may fail.
3808 */
3809 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3810#endif
3811 mch_remove(to);
3812#ifdef AMIGA
3813 if (flock)
3814 UnLock(flock);
3815#endif
3816
3817 /*
3818 * First try a normal rename, return if it works.
3819 */
3820 if (mch_rename((char *)from, (char *)to) == 0)
3821 return 0;
3822
3823 /*
3824 * Rename() failed, try copying the file.
3825 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003826 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003827#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003828 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003829 acl = mch_get_acl(from);
3830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3832 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003833 {
3834#ifdef HAVE_ACL
3835 mch_free_acl(acl);
3836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003838 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003839
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003840 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003841 fd_out = mch_open((char *)to,
3842 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (fd_out == -1)
3844 {
3845 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003846#ifdef HAVE_ACL
3847 mch_free_acl(acl);
3848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return -1;
3850 }
3851
Bram Moolenaar473952e2019-09-28 16:30:04 +02003852 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (buffer == NULL)
3854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003856 close(fd_in);
3857#ifdef HAVE_ACL
3858 mch_free_acl(acl);
3859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 return -1;
3861 }
3862
Bram Moolenaar473952e2019-09-28 16:30:04 +02003863 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003864 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 errmsg = _("E208: Error writing to \"%s\"");
3867 break;
3868 }
3869
3870 vim_free(buffer);
3871 close(fd_in);
3872 if (close(fd_out) < 0)
3873 errmsg = _("E209: Error closing \"%s\"");
3874 if (n < 0)
3875 {
3876 errmsg = _("E210: Error reading \"%s\"");
3877 to = from;
3878 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003879#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003880 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003881#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003882#ifdef HAVE_ACL
3883 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003884 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003885#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003886#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003887 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (errmsg != NULL)
3890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003891 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return -1;
3893 }
3894 mch_remove(from);
3895 return 0;
3896}
3897
3898static int already_warned = FALSE;
3899
3900/*
3901 * Check if any not hidden buffer has been changed.
3902 * Postpone the check if there are characters in the stuff buffer, a global
3903 * command is being executed, a mapping is being executed or an autocommand is
3904 * busy.
3905 * Returns TRUE if some message was written (screen should be redrawn and
3906 * cursor positioned).
3907 */
3908 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003909check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003910 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 buf_T *buf;
3913 int didit = 0;
3914 int n;
3915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003916 // Don't check timestamps while system() or another low-level function may
3917 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (no_check_timestamps > 0)
3919 return FALSE;
3920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003921 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3922 // event and we would keep on checking if the file is steadily growing.
3923 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (focus && did_check_timestamps)
3925 {
3926 need_check_timestamps = TRUE;
3927 return FALSE;
3928 }
3929
3930 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003931 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003932 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 else
3934 {
3935 ++no_wait_return;
3936 did_check_timestamps = TRUE;
3937 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003938 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003940 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if (buf->b_nwindows > 0)
3942 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003943 bufref_T bufref;
3944
3945 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 n = buf_check_timestamp(buf, focus);
3947 if (didit < n)
3948 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003949 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003951 // Autocommands have removed the buffer, start at the
3952 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 buf = firstbuf;
3954 continue;
3955 }
3956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958 --no_wait_return;
3959 need_check_timestamps = FALSE;
3960 if (need_wait_return && didit == 2)
3961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003962 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003963 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 out_flush();
3965 }
3966 }
3967 return didit;
3968}
3969
3970/*
3971 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3972 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3973 * empty.
3974 */
3975 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003976move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 buf_T *tbuf = curbuf;
3979 int retval = OK;
3980 linenr_T lnum;
3981 char_u *p;
3982
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003983 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 curbuf = tobuf;
3985 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3986 {
3987 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3988 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3989 {
3990 vim_free(p);
3991 retval = FAIL;
3992 break;
3993 }
3994 vim_free(p);
3995 }
3996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003997 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (retval != FAIL)
3999 {
4000 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004001 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004002 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004004 // Oops! We could try putting back the saved lines, but that
4005 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 retval = FAIL;
4007 break;
4008 }
4009 }
4010
4011 curbuf = tbuf;
4012 return retval;
4013}
4014
4015/*
4016 * Check if buffer "buf" has been changed.
4017 * Also check if the file for a new buffer unexpectedly appeared.
4018 * return 1 if a changed buffer was found.
4019 * return 2 if a message has been displayed.
4020 * return 0 otherwise.
4021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004023buf_check_timestamp(
4024 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004025 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004027 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 int stat_res;
4029 int retval = 0;
4030 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004031 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004033 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 int helpmesg = FALSE;
4035 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004036 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4038 int can_reload = FALSE;
4039#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004040 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 int orig_mode = buf->b_orig_mode;
4042#ifdef FEAT_GUI
4043 int save_mouse_correct = need_mouse_correct;
4044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004046 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004047#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004048 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004049#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004050 bufref_T bufref;
4051
4052 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004054 // If there is no file name, the buffer is not loaded, 'buftype' is
4055 // set, we are in the middle of a save or being called recursively: ignore
4056 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (buf->b_ffname == NULL
4058 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004059 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004062#ifdef FEAT_NETBEANS_INTG
4063 || isNetbeansBuffer(buf)
4064#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004065#ifdef FEAT_TERMINAL
4066 || buf->b_term != NULL
4067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 )
4069 return 0;
4070
4071 if ( !(buf->b_flags & BF_NOTEDITED)
4072 && buf->b_mtime != 0
4073 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
4074 || time_differs((long)st.st_mtime, buf->b_mtime)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004075 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#ifdef HAVE_ST_MODE
4077 || (int)st.st_mode != buf->b_orig_mode
4078#else
4079 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4080#endif
4081 ))
4082 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004083 long prev_b_mtime = buf->b_mtime;
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 retval = 1;
4086
Bram Moolenaar386bc822018-07-07 18:34:12 +02004087 // set b_mtime to stop further warnings (e.g., when executing
4088 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 if (stat_res < 0)
4090 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004091 // Check the file again later to see if it re-appears.
4092 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 buf->b_orig_size = 0;
4094 buf->b_orig_mode = 0;
4095 }
4096 else
4097 buf_store_time(buf, &st, buf->b_ffname);
4098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004099 // Don't do anything for a directory. Might contain the file
4100 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (mch_isdir(buf->b_fname))
4102 ;
4103
4104 /*
4105 * If 'autoread' is set, the buffer has no changes and the file still
4106 * exists, reload the buffer. Use the buffer-local option value if it
4107 * was set, the global option value otherwise.
4108 */
4109 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4110 && !bufIsChanged(buf) && stat_res >= 0)
4111 reload = TRUE;
4112 else
4113 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004114 if (stat_res < 0)
4115 reason = "deleted";
4116 else if (bufIsChanged(buf))
4117 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004118 /*
4119 * Check if the file contents really changed to avoid giving a
4120 * warning when only the timestamp was set (e.g., checked out of
4121 * CVS). Always warn when the buffer was changed.
4122 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004123 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4124 reason = "changed";
4125 else if (orig_mode != buf->b_orig_mode)
4126 reason = "mode";
4127 else
4128 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 /*
4131 * Only give the warning if there are no FileChangedShell
4132 * autocommands.
4133 * Avoid being called recursively by setting "busy".
4134 */
4135 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004136#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004137 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4138 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004139#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004140 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4142 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004143 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 busy = FALSE;
4145 if (n)
4146 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004147 if (!bufref_valid(&bufref))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004148 emsg(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004149#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004150 s = get_vim_var_str(VV_FCS_CHOICE);
4151 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4152 reload = TRUE;
4153 else if (STRCMP(s, "ask") == 0)
4154 n = FALSE;
4155 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004156#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004157 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004159 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004161 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004162 {
4163 // Only give the message once.
4164 if (prev_b_mtime != -1)
4165 mesg = _("E211: File \"%s\" no longer available");
4166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 else
4168 {
4169 helpmesg = TRUE;
4170#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4171 can_reload = TRUE;
4172#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004173 if (reason[2] == 'n')
4174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004176 mesg2 = _("See \":help W12\" for more info.");
4177 }
4178 else if (reason[1] == 'h')
4179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004181 mesg2 = _("See \":help W11\" for more info.");
4182 }
4183 else if (*reason == 'm')
4184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186 mesg2 = _("See \":help W16\" for more info.");
4187 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004188 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004189 // Only timestamp changed, store it to avoid a warning
4190 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004191 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 }
4194 }
4195
4196 }
4197 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4198 && vim_fexists(buf->b_ffname))
4199 {
4200 retval = 1;
4201 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4202 buf->b_flags |= BF_NEW_W;
4203#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4204 can_reload = TRUE;
4205#endif
4206 }
4207
4208 if (mesg != NULL)
4209 {
4210 path = home_replace_save(buf, buf->b_fname);
4211 if (path != NULL)
4212 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004213 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004215 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004216 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004217#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004218 // Set warningmsg here, before the unimportant and output-specific
4219 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004220 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4223 if (can_reload)
4224 {
4225 if (*mesg2 != NUL)
4226 {
4227 STRCAT(tbuf, "\n");
4228 STRCAT(tbuf, mesg2);
4229 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004230 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4231 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004232 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 reload = TRUE;
4234 }
4235 else
4236#endif
4237 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4238 {
4239 if (*mesg2 != NUL)
4240 {
4241 STRCAT(tbuf, "; ");
4242 STRCAT(tbuf, mesg2);
4243 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004244 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 retval = 2;
4246 }
4247 else
4248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
4251 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004252 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004254 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 msg_clr_eos();
4256 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004257 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004260#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004262#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004263 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004264 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004266 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 redraw_cmdline = FALSE;
4268 }
4269 }
4270 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272
4273 vim_free(path);
4274 vim_free(tbuf);
4275 }
4276 }
4277
4278 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004280 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004281 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004282#ifdef FEAT_PERSISTENT_UNDO
4283 if (buf->b_p_udf && buf->b_ffname != NULL)
4284 {
4285 char_u hash[UNDO_HASH_SIZE];
4286 buf_T *save_curbuf = curbuf;
4287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004289 curbuf = buf;
4290 u_compute_hash(hash);
4291 u_write_undo(NULL, FALSE, buf, hash);
4292 curbuf = save_curbuf;
4293 }
4294#endif
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004298 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004299 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4300 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004302 // restore this in case an autocommand has set it; it would break
4303 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 need_mouse_correct = save_mouse_correct;
4305#endif
4306
4307 return retval;
4308}
4309
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004310/*
4311 * Reload a buffer that is already loaded.
4312 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004313 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4314 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004315 */
4316 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004317buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004318{
4319 exarg_T ea;
4320 pos_T old_cursor;
4321 linenr_T old_topline;
4322 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004323 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004324 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004325 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004326 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004327 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004329 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004330 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004331
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 // We only want to read the text from the file, not reset the syntax
4333 // highlighting, clear marks, diff status, etc. Force the fileformat
4334 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004335 if (prep_exarg(&ea, buf) == OK)
4336 {
4337 old_cursor = curwin->w_cursor;
4338 old_topline = curwin->w_topline;
4339
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004340 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004342 // Save all the text, so that the reload can be undone.
4343 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004344 u_sync(FALSE);
4345 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4346 flags |= READ_KEEP_UNDO;
4347 }
4348
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004349 /*
4350 * To behave like when a new file is edited (matters for
4351 * BufReadPost autocommands) we first need to delete the current
4352 * buffer contents. But if reading the file fails we should keep
4353 * the old contents. Can't use memory only, the file might be
4354 * too big. Use a hidden buffer to move the buffer contents to.
4355 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004356 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004357 savebuf = NULL;
4358 else
4359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004362 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004363 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004365 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004366 curbuf = savebuf;
4367 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004368 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004369 curbuf = buf;
4370 curwin->w_buffer = buf;
4371 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004372 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004373 || move_lines(buf, savebuf) == FAIL)
4374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004375 semsg(_("E462: Could not prepare for reloading \"%s\""),
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004376 buf->b_fname);
4377 saved = FAIL;
4378 }
4379 }
4380
4381 if (saved == OK)
4382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4384 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4386 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004387 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004388 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004389#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004390 if (!aborting())
4391#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004392 semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004393 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004394 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004395 // Put the text back from the save buffer. First
4396 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004397 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004398 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004399 break;
4400 (void)move_lines(savebuf, buf);
4401 }
4402 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004403 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004405 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004406 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004407 if ((flags & READ_KEEP_UNDO) == 0)
4408 {
4409 u_blockfree(buf);
4410 u_clearall(buf);
4411 }
4412 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004415 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004416 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004417 }
4418 }
4419 vim_free(ea.cmd);
4420
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004421 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004422 wipe_buffer(savebuf, FALSE);
4423
4424#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004426 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004427#endif
4428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 // Restore the topline and cursor position and check it (lines may
4430 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004431 if (old_topline > curbuf->b_ml.ml_line_count)
4432 curwin->w_topline = curbuf->b_ml.ml_line_count;
4433 else
4434 curwin->w_topline = old_topline;
4435 curwin->w_cursor = old_cursor;
4436 check_cursor();
4437 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004438 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004439#ifdef FEAT_FOLDING
4440 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004441 win_T *wp;
4442 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004445 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004446 if (wp->w_buffer == curwin->w_buffer
4447 && !foldmethodIsManual(wp))
4448 foldUpdateAll(wp);
4449 }
4450#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004451 // If the mode didn't change and 'readonly' was set, keep the old
4452 // value; the user probably used the ":view" command. But don't
4453 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004454 if (orig_mode == curbuf->b_orig_mode)
4455 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004456
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004457 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004458 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004459 }
4460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004462 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004463 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004464}
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004467buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02004470 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471#ifdef HAVE_ST_MODE
4472 buf->b_orig_mode = (int)st->st_mode;
4473#else
4474 buf->b_orig_mode = mch_getperm(fname);
4475#endif
4476}
4477
4478/*
4479 * Adjust the line with missing eol, used for the next write.
4480 * Used for do_filter(), when the input lines for the filter are deleted.
4481 */
4482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004483write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004486 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487}
4488
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004489// Subfuncions for readdirex()
4490#ifdef FEAT_EVAL
4491# ifdef MSWIN
4492 static char_u *
4493getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4494{
4495 stat_T st;
4496 unsigned short st_mode;
4497 DWORD flag = wfd->dwFileAttributes;
4498 WCHAR *wp;
4499
4500 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4501 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4502 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4503 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4504
4505 wp = wcsrchr(wfd->cFileName, L'.');
4506 if (wp != NULL)
4507 {
4508 if (_wcsicmp(wp, L".exe") == 0 ||
4509 _wcsicmp(wp, L".com") == 0 ||
4510 _wcsicmp(wp, L".cmd") == 0 ||
4511 _wcsicmp(wp, L".bat") == 0)
4512 st_mode |= _S_IEXEC;
4513 }
4514
4515 // Copy user bits to group/other.
4516 st_mode |= (st_mode & 0700) >> 3;
4517 st_mode |= (st_mode & 0700) >> 6;
4518
4519 st.st_mode = st_mode;
4520 return getfpermst(&st, perm);
4521}
4522
4523 static char_u *
4524getftypewfd(WIN32_FIND_DATAW *wfd)
4525{
4526 DWORD flag = wfd->dwFileAttributes;
4527 DWORD tag = wfd->dwReserved0;
4528
4529 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4530 {
4531 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4532 return (char_u*)"junction";
4533 else if (tag == IO_REPARSE_TAG_SYMLINK)
4534 {
4535 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4536 return (char_u*)"linkd";
4537 else
4538 return (char_u*)"link";
4539 }
4540 return (char_u*)"reparse"; // unknown reparse point type
4541 }
4542 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4543 return (char_u*)"dir";
4544 else
4545 return (char_u*)"file";
4546}
4547
4548 static dict_T *
4549create_readdirex_item(WIN32_FIND_DATAW *wfd)
4550{
4551 dict_T *item;
4552 char_u *p;
4553 varnumber_T size, time;
4554 char_u permbuf[] = "---------";
4555
4556 item = dict_alloc();
4557 if (item == NULL)
4558 return NULL;
4559 item->dv_refcount++;
4560
4561 p = utf16_to_enc(wfd->cFileName, NULL);
4562 if (p == NULL)
4563 goto theend;
4564 if (dict_add_string(item, "name", p) == FAIL)
4565 {
4566 vim_free(p);
4567 goto theend;
4568 }
4569 vim_free(p);
4570
4571 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4572 if (dict_add_number(item, "size", size) == FAIL)
4573 goto theend;
4574
4575 // Convert FILETIME to unix time.
4576 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4577 wfd->ftLastWriteTime.dwLowDateTime)
4578 - 116444736000000000) / 10000000;
4579 if (dict_add_number(item, "time", time) == FAIL)
4580 goto theend;
4581
4582 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4583 goto theend;
4584 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4585 goto theend;
4586
4587 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4588 goto theend;
4589 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4590 goto theend;
4591
4592 return item;
4593
4594theend:
4595 dict_unref(item);
4596 return NULL;
4597}
4598# else
4599 static dict_T *
4600create_readdirex_item(char_u *path, char_u *name)
4601{
4602 dict_T *item;
4603 char *p;
4604 size_t len;
4605 stat_T st;
4606 int ret, link = FALSE;
4607 varnumber_T size;
4608 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004609 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004610 struct passwd *pw;
4611 struct group *gr;
4612
4613 item = dict_alloc();
4614 if (item == NULL)
4615 return NULL;
4616 item->dv_refcount++;
4617
4618 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4619 p = alloc(len);
4620 if (p == NULL)
4621 goto theend;
4622 vim_snprintf(p, len, "%s/%s", path, name);
4623 ret = mch_lstat(p, &st);
4624 if (ret >= 0 && S_ISLNK(st.st_mode))
4625 {
4626 link = TRUE;
4627 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004628 if (ret < 0)
4629 q = (char_u*)"link";
4630
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004631 }
4632 vim_free(p);
4633
4634 if (dict_add_string(item, "name", name) == FAIL)
4635 goto theend;
4636
4637 if (ret >= 0)
4638 {
4639 size = (varnumber_T)st.st_size;
4640 if (S_ISDIR(st.st_mode))
4641 size = 0;
4642 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004643 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004644 size = -2;
4645 if (dict_add_number(item, "size", size) == FAIL)
4646 goto theend;
4647 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4648 goto theend;
4649
4650 if (link)
4651 {
4652 if (S_ISDIR(st.st_mode))
4653 q = (char_u*)"linkd";
4654 else
4655 q = (char_u*)"link";
4656 }
4657 else
4658 q = getftypest(&st);
4659 if (dict_add_string(item, "type", q) == FAIL)
4660 goto theend;
4661 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4662 goto theend;
4663
4664 pw = getpwuid(st.st_uid);
4665 if (pw == NULL)
4666 q = (char_u*)"";
4667 else
4668 q = (char_u*)pw->pw_name;
4669 if (dict_add_string(item, "user", q) == FAIL)
4670 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004671# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004672 gr = getgrgid(st.st_gid);
4673 if (gr == NULL)
4674 q = (char_u*)"";
4675 else
4676 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004677# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004678 if (dict_add_string(item, "group", q) == FAIL)
4679 goto theend;
4680 }
4681 else
4682 {
4683 if (dict_add_number(item, "size", -1) == FAIL)
4684 goto theend;
4685 if (dict_add_number(item, "time", -1) == FAIL)
4686 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004687 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004688 goto theend;
4689 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4690 goto theend;
4691 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4692 goto theend;
4693 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4694 goto theend;
4695 }
4696 return item;
4697
4698theend:
4699 dict_unref(item);
4700 return NULL;
4701}
4702# endif
4703
4704 static int
4705compare_readdirex_item(const void *p1, const void *p2)
4706{
4707 char_u *name1, *name2;
4708
4709 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4710 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004711 if (readdirex_sort == READDIR_SORT_BYTE)
4712 return STRCMP(name1, name2);
4713 else if (readdirex_sort == READDIR_SORT_IC)
4714 return STRICMP(name1, name2);
4715 else
4716 return STRCOLL(name1, name2);
4717}
4718
4719 static int
4720compare_readdir_item(const void *s1, const void *s2)
4721{
4722 if (readdirex_sort == READDIR_SORT_BYTE)
4723 return STRCMP(*(char **)s1, *(char **)s2);
4724 else if (readdirex_sort == READDIR_SORT_IC)
4725 return STRICMP(*(char **)s1, *(char **)s2);
4726 else
4727 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004728}
4729#endif
4730
Bram Moolenaarda440d22016-01-16 21:27:23 +01004731#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4732/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004733 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004734 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004735 * If "withattr" is TRUE, retrieve the names and their attributes.
4736 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004737 * Return OK for success, FAIL for failure.
4738 */
4739 int
4740readdir_core(
4741 garray_T *gap,
4742 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004743 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004744 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004745 int (*checkitem)(void *context, void *item),
4746 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004747{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004748 int failed = FALSE;
4749 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004750# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004751 char_u *buf;
4752 int ok;
4753 HANDLE hFind = INVALID_HANDLE_VALUE;
4754 WIN32_FIND_DATAW wfd;
4755 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004756# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004757 DIR *dirp;
4758 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004759# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004760
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004761 ga_init2(gap, (int)sizeof(void *), 20);
4762
4763# ifdef FEAT_EVAL
4764# define FREE_ITEM(item) do { \
4765 if (withattr) \
4766 dict_unref((dict_T*)item); \
4767 else \
4768 vim_free(item); \
4769 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004770
4771 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004772# else
4773# define FREE_ITEM(item) vim_free(item)
4774# endif
4775
4776# ifdef MSWIN
4777 buf = alloc(MAXPATHL);
4778 if (buf == NULL)
4779 return FAIL;
4780 STRNCPY(buf, path, MAXPATHL-5);
4781 p = buf + STRLEN(buf);
4782 MB_PTR_BACK(buf, p);
4783 if (*p == '\\' || *p == '/')
4784 *p = NUL;
4785 STRCAT(p, "\\*");
4786
4787 wn = enc_to_utf16(buf, NULL);
4788 if (wn != NULL)
4789 hFind = FindFirstFileW(wn, &wfd);
4790 ok = (hFind != INVALID_HANDLE_VALUE);
4791 if (!ok)
4792 {
4793 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004794 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004795 }
4796 else
4797 {
4798 while (ok)
4799 {
4800 int ignore;
4801 void *item;
4802 WCHAR *wp;
4803
4804 wp = wfd.cFileName;
4805 ignore = wp[0] == L'.' &&
4806 (wp[1] == NUL ||
4807 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004808 if (ignore)
4809 {
4810 ok = FindNextFileW(hFind, &wfd);
4811 continue;
4812 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004813# ifdef FEAT_EVAL
4814 if (withattr)
4815 item = (void*)create_readdirex_item(&wfd);
4816 else
4817# endif
4818 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4819 if (item == NULL)
4820 {
4821 failed = TRUE;
4822 break;
4823 }
4824
4825 if (!ignore && checkitem != NULL)
4826 {
4827 int r = checkitem(context, item);
4828
4829 if (r < 0)
4830 {
4831 FREE_ITEM(item);
4832 break;
4833 }
4834 if (r == 0)
4835 ignore = TRUE;
4836 }
4837
4838 if (!ignore)
4839 {
4840 if (ga_grow(gap, 1) == OK)
4841 ((void**)gap->ga_data)[gap->ga_len++] = item;
4842 else
4843 {
4844 failed = TRUE;
4845 FREE_ITEM(item);
4846 break;
4847 }
4848 }
4849 else
4850 FREE_ITEM(item);
4851
4852 ok = FindNextFileW(hFind, &wfd);
4853 }
4854 FindClose(hFind);
4855 }
4856
4857 vim_free(buf);
4858 vim_free(wn);
4859# else // MSWIN
4860 dirp = opendir((char *)path);
4861 if (dirp == NULL)
4862 {
4863 failed = TRUE;
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02004864 semsg(_(e_notopen), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004865 }
4866 else
4867 {
4868 for (;;)
4869 {
4870 int ignore;
4871 void *item;
4872
4873 dp = readdir(dirp);
4874 if (dp == NULL)
4875 break;
4876 p = (char_u *)dp->d_name;
4877
4878 ignore = p[0] == '.' &&
4879 (p[1] == NUL ||
4880 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004881 if (ignore)
4882 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004883# ifdef FEAT_EVAL
4884 if (withattr)
4885 item = (void*)create_readdirex_item(path, p);
4886 else
4887# endif
4888 item = (void*)vim_strsave(p);
4889 if (item == NULL)
4890 {
4891 failed = TRUE;
4892 break;
4893 }
4894
4895 if (!ignore && checkitem != NULL)
4896 {
4897 int r = checkitem(context, item);
4898
4899 if (r < 0)
4900 {
4901 FREE_ITEM(item);
4902 break;
4903 }
4904 if (r == 0)
4905 ignore = TRUE;
4906 }
4907
4908 if (!ignore)
4909 {
4910 if (ga_grow(gap, 1) == OK)
4911 ((void**)gap->ga_data)[gap->ga_len++] = item;
4912 else
4913 {
4914 failed = TRUE;
4915 FREE_ITEM(item);
4916 break;
4917 }
4918 }
4919 else
4920 FREE_ITEM(item);
4921 }
4922
4923 closedir(dirp);
4924 }
4925# endif // MSWIN
4926
4927# undef FREE_ITEM
4928
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004929 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004930 {
4931# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004932 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004933 if (withattr)
4934 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4935 compare_readdirex_item);
4936 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004937 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4938 compare_readdir_item);
4939# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004940 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004941# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004942 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004943
4944 return failed ? FAIL : OK;
4945}
4946
4947/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004948 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004949 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004950 */
4951 int
4952delete_recursive(char_u *name)
4953{
4954 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004955 int i;
4956 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004957 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004958
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004959 // A symbolic link to a directory itself is deleted, not the directory it
4960 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004961 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004962# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004963 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004964# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004965 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004966# endif
4967 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004968 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004969 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004970 if (exp == NULL)
4971 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004972 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004973 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004974 for (i = 0; i < ga.ga_len; ++i)
4975 {
4976 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4977 ((char_u **)ga.ga_data)[i]);
4978 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004979 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004980 }
4981 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004982 }
4983 else
4984 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004985 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004986 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004987 }
4988 else
4989 result = mch_remove(name) == 0 ? 0 : -1;
4990
4991 return result;
4992}
4993#endif
4994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004996static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02004998# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
4999/*
5000 * Open temporary directory and take file lock to prevent
5001 * to be auto-cleaned.
5002 */
5003 static void
5004vim_opentempdir(void)
5005{
5006 DIR *dp = NULL;
5007
5008 if (vim_tempdir_dp != NULL)
5009 return;
5010
5011 dp = opendir((const char*)vim_tempdir);
5012
5013 if (dp != NULL)
5014 {
5015 vim_tempdir_dp = dp;
5016 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5017 }
5018}
5019
5020/*
5021 * Close temporary directory - it automatically release file lock.
5022 */
5023 static void
5024vim_closetempdir(void)
5025{
5026 if (vim_tempdir_dp != NULL)
5027 {
5028 closedir(vim_tempdir_dp);
5029 vim_tempdir_dp = NULL;
5030 }
5031}
5032# endif
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034/*
5035 * Delete the temp directory and all files it contains.
5036 */
5037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005038vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (vim_tempdir != NULL)
5041 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005042# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5043 vim_closetempdir();
5044# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005045 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005046 gettail(vim_tempdir)[-1] = NUL;
5047 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005048 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
5052/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005053 * Directory "tempdir" was created. Expand this name to a full path and put
5054 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5055 * "tempdir" must be no longer than MAXPATHL.
5056 */
5057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005059{
5060 char_u *buf;
5061
Bram Moolenaar964b3742019-05-24 18:54:09 +02005062 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005063 if (buf != NULL)
5064 {
5065 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5066 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005067 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005068 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005069# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5070 vim_opentempdir();
5071# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005072 vim_free(buf);
5073 }
5074}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005075#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005076
5077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 * vim_tempname(): Return a unique name that can be used for a temp file.
5079 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005080 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5081 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 *
5083 * The returned pointer is to allocated memory.
5084 * The returned pointer is NULL if no valid name was found.
5085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005087vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005089 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090{
5091#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005092 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005093#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005094 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095#else
5096 char_u itmp[TEMPNAMELEN];
5097#endif
5098
5099#ifdef TEMPDIRNAMES
5100 static char *(tempdirs[]) = {TEMPDIRNAMES};
5101 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005103 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104# endif
5105
5106 /*
5107 * This will create a directory for private use by this instance of Vim.
5108 * This is done once, and the same directory is used for all temp files.
5109 * This method avoids security problems because of symlink attacks et al.
5110 * It's also a bit faster, because we only need to check for an existing
5111 * file when creating the directory and not for each temp file.
5112 */
5113 if (vim_tempdir == NULL)
5114 {
5115 /*
5116 * Try the entries in TEMPDIRNAMES to create the temp directory.
5117 */
K.Takataeeec2542021-06-02 13:28:16 +02005118 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005120# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005121 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005122 long nr;
5123 long off;
5124# endif
5125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 // Expand $TMP, leave room for "/v1100000/999999999".
5127 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005129 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005132 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaareaf03392009-11-17 11:08:52 +00005134# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005135 {
5136# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005137 // Make sure the umask doesn't remove the executable bit.
5138 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005139 mode_t umask_save = umask(077);
5140# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005141 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005142 STRCAT(itmp, "vXXXXXX");
5143 if (mkdtemp((char *)itmp) != NULL)
5144 vim_settempdir(itmp);
5145# if defined(UNIX) || defined(VMS)
5146 (void)umask(umask_save);
5147# endif
5148 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005149# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005150 // Get an arbitrary number of up to 6 digits. When it's
5151 // unlikely that it already exists it will be faster,
5152 // otherwise it doesn't matter. The use of mkdir() avoids any
5153 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005155 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 // Try up to 10000 different values until we find a name that
5158 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 for (off = 0; off < 10000L; ++off)
5160 {
5161 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005162# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
Bram Moolenaareaf03392009-11-17 11:08:52 +00005166 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5167# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005168 // If mkdir() does not set errno to EEXIST, check for
5169 // existing file here. There is a race condition then,
5170 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (mch_stat((char *)itmp, &st) >= 0)
5172 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005173# endif
5174# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005175 // Make sure the umask doesn't remove the executable bit.
5176 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005180# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 if (r == 0)
5184 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005185 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005188# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005189 // If the mkdir() didn't fail because the file/dir exists,
5190 // we probably can't create any dir here, try another
5191 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 break;
5195 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005196# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (vim_tempdir != NULL)
5198 break;
5199 }
5200 }
5201 }
5202
5203 if (vim_tempdir != NULL)
5204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // There is no need to check if the file exists, because we own the
5206 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5208 return vim_strsave(itmp);
5209 }
5210
5211 return NULL;
5212
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005213#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar4f974752019-02-17 17:44:42 +01005215# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005216 WCHAR wszTempFile[_MAX_PATH + 1];
5217 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005218 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 char_u *retval;
5220 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005221 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005222 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005224 wcscpy(itmp, L"");
5225 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005226 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005227 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005228 wszTempFile[1] = L'\\';
5229 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005230 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005231 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005232
5233 // randomize the name to avoid collisions
5234 i = mch_get_pid() + extra_char;
5235 buf4[1] = chartab[i % 36];
5236 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005237 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005239 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005240 // GetTempFileName() will create the file, we don't want that
5241 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005243 // Backslashes in a temp file name cause problems when filtering with
5244 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005245 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005246 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005247 shname = gettail(p_sh);
5248 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5249 || strstr((char *)shname, "pwsh") != NULL ))
5250 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 for (p = retval; *p; ++p)
5252 if (*p == '\\')
5253 *p = '/';
5254 return retval;
5255
Bram Moolenaar4f974752019-02-17 17:44:42 +01005256# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005259 char_u *p;
5260
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005261 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005262 p = tmpnam((char *)itmp);
5263 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return NULL;
5265# else
5266 char_u *p;
5267
5268# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005269 // mktemp() is not working on VMS. It seems to be
5270 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 sprintf((char *)itmp, "VIM%c", extra_char);
5272 p = (char_u *)tempnam("tmp:", (char *)itmp);
5273 if (p != NULL)
5274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 // VMS will use '.LIS' if we don't explicitly specify an extension,
5276 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 STRCPY(itmp, p);
5278 STRCAT(itmp, ".txt");
5279 free(p);
5280 }
5281 else
5282 return NULL;
5283# else
5284 STRCPY(itmp, TEMPNAME);
5285 if ((p = vim_strchr(itmp, '?')) != NULL)
5286 *p = extra_char;
5287 if (mktemp((char *)itmp) == NULL)
5288 return NULL;
5289# endif
5290# endif
5291
5292 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005293# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005294#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295}
5296
5297#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5298/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005299 * Convert all backslashes in fname to forward slashes in-place, unless when
5300 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
5302 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005303forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 char_u *p;
5306
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005307 if (path_with_url(fname))
5308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005311 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005313 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 *p = '/';
5315}
5316#endif
5317
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005318/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005319 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5320 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5321 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * Used for autocommands and 'wildignore'.
5323 * Returns TRUE if there is a match, FALSE otherwise.
5324 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005325 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005327 char_u *pattern, // pattern to match with
5328 regprog_T **prog, // pre-compiled regprog or NULL
5329 char_u *fname, // full path of file name
5330 char_u *sfname, // short file name or NULL
5331 char_u *tail, // tail of path
5332 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 regmatch_T regmatch;
5335 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005338 if (prog != NULL)
5339 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005341 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 /*
5344 * Try for a match with the pattern with:
5345 * 1. the full file name, when the pattern has a '/'.
5346 * 2. the short file name, when the pattern has a '/'.
5347 * 3. the tail of the file name, when the pattern has no '/'.
5348 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005349 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 && ((allow_dirs
5351 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5352 || (sfname != NULL
5353 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005354 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 result = TRUE;
5356
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005357 if (prog != NULL)
5358 *prog = regmatch.regprog;
5359 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005360 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 return result;
5362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364#if defined(FEAT_WILDIGN) || defined(PROTO)
5365/*
5366 * Return TRUE if a file matches with a pattern in "list".
5367 * "list" is a comma-separated list of patterns, like 'wildignore'.
5368 * "sfname" is the short file name or NULL, "ffname" the long file name.
5369 */
5370 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 char_u buf[100];
5374 char_u *tail;
5375 char_u *regpat;
5376 char allow_dirs;
5377 int match;
5378 char_u *p;
5379
5380 tail = gettail(sfname);
5381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 p = list;
5384 while (*p)
5385 {
5386 copy_option_part(&p, buf, 100, ",");
5387 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5388 if (regpat == NULL)
5389 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005390 match = match_file_pat(regpat, NULL, ffname, sfname,
5391 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 vim_free(regpat);
5393 if (match)
5394 return TRUE;
5395 }
5396 return FALSE;
5397}
5398#endif
5399
5400/*
5401 * Convert the given pattern "pat" which has shell style wildcards in it, into
5402 * a regular expression, and return the result in allocated memory. If there
5403 * is a directory path separator to be matched, then TRUE is put in
5404 * allow_dirs, otherwise FALSE is put there -- webb.
5405 * Handle backslashes before special characters, like "\*" and "\ ".
5406 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 * Returns NULL when out of memory.
5408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410file_pat_to_reg_pat(
5411 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005412 char_u *pat_end, // first char after pattern or NULL
5413 char *allow_dirs, // Result passed back out in here
5414 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005416 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 char_u *endp;
5418 char_u *reg_pat;
5419 char_u *p;
5420 int i;
5421 int nested = 0;
5422 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
5424 if (allow_dirs != NULL)
5425 *allow_dirs = FALSE;
5426 if (pat_end == NULL)
5427 pat_end = pat + STRLEN(pat);
5428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 for (p = pat; p < pat_end; p++)
5430 {
5431 switch (*p)
5432 {
5433 case '*':
5434 case '.':
5435 case ',':
5436 case '{':
5437 case '}':
5438 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005439 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 break;
5441#ifdef BACKSLASH_IN_FILENAME
5442 case '\\':
5443 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005444 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 break;
5446#endif
5447 default:
5448 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005449 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 ++p;
5452 ++size;
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 break;
5455 }
5456 }
5457 reg_pat = alloc(size + 1);
5458 if (reg_pat == NULL)
5459 return NULL;
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if (pat[0] == '*')
5464 while (pat[0] == '*' && pat < pat_end - 1)
5465 pat++;
5466 else
5467 reg_pat[i++] = '^';
5468 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005469 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
5471 while (endp - pat > 0 && *endp == '*')
5472 endp--;
5473 add_dollar = FALSE;
5474 }
5475 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5476 {
5477 switch (*p)
5478 {
5479 case '*':
5480 reg_pat[i++] = '.';
5481 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005482 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005483 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 break;
5485 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 case '~':
5487 reg_pat[i++] = '\\';
5488 reg_pat[i++] = *p;
5489 break;
5490 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 reg_pat[i++] = '.';
5492 break;
5493 case '\\':
5494 if (p[1] == NUL)
5495 break;
5496#ifdef BACKSLASH_IN_FILENAME
5497 if (!no_bslash)
5498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005499 // translate:
5500 // "\x" to "\\x" e.g., "dir\file"
5501 // "\*" to "\\.*" e.g., "dir\*.c"
5502 // "\?" to "\\." e.g., "dir\??.c"
5503 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5505 && p[1] != '+')
5506 {
5507 reg_pat[i++] = '[';
5508 reg_pat[i++] = '\\';
5509 reg_pat[i++] = '/';
5510 reg_pat[i++] = ']';
5511 if (allow_dirs != NULL)
5512 *allow_dirs = TRUE;
5513 break;
5514 }
5515 }
5516#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 // Undo escaping from ExpandEscape():
5518 // foo\?bar -> foo?bar
5519 // foo\%bar -> foo%bar
5520 // foo\,bar -> foo,bar
5521 // foo\ bar -> foo bar
5522 // Don't unescape \, * and others that are also special in a
5523 // regexp.
5524 // An escaped { must be unescaped since we use magic not
5525 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (*++p == '?'
5527#ifdef BACKSLASH_IN_FILENAME
5528 && no_bslash
5529#endif
5530 )
5531 reg_pat[i++] = '?';
5532 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005533 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005534 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005535 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005536 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5537 {
5538 reg_pat[i++] = '\\';
5539 reg_pat[i++] = '{';
5540 p += 2;
5541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 else
5543 {
5544 if (allow_dirs != NULL && vim_ispathsep(*p)
5545#ifdef BACKSLASH_IN_FILENAME
5546 && (!no_bslash || *p != '\\')
5547#endif
5548 )
5549 *allow_dirs = TRUE;
5550 reg_pat[i++] = '\\';
5551 reg_pat[i++] = *p;
5552 }
5553 break;
5554#ifdef BACKSLASH_IN_FILENAME
5555 case '/':
5556 reg_pat[i++] = '[';
5557 reg_pat[i++] = '\\';
5558 reg_pat[i++] = '/';
5559 reg_pat[i++] = ']';
5560 if (allow_dirs != NULL)
5561 *allow_dirs = TRUE;
5562 break;
5563#endif
5564 case '{':
5565 reg_pat[i++] = '\\';
5566 reg_pat[i++] = '(';
5567 nested++;
5568 break;
5569 case '}':
5570 reg_pat[i++] = '\\';
5571 reg_pat[i++] = ')';
5572 --nested;
5573 break;
5574 case ',':
5575 if (nested)
5576 {
5577 reg_pat[i++] = '\\';
5578 reg_pat[i++] = '|';
5579 }
5580 else
5581 reg_pat[i++] = ',';
5582 break;
5583 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005584 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005586 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 *allow_dirs = TRUE;
5588 reg_pat[i++] = *p;
5589 break;
5590 }
5591 }
5592 if (add_dollar)
5593 reg_pat[i++] = '$';
5594 reg_pat[i] = NUL;
5595 if (nested != 0)
5596 {
5597 if (nested < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005598 emsg(_("E219: Missing {."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005600 emsg(_("E220: Missing }."));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005601 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 return reg_pat;
5604}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005605
5606#if defined(EINTR) || defined(PROTO)
5607/*
5608 * Version of read() that retries when interrupted by EINTR (possibly
5609 * by a SIGWINCH).
5610 */
5611 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005613{
5614 long ret;
5615
5616 for (;;)
5617 {
5618 ret = vim_read(fd, buf, bufsize);
5619 if (ret >= 0 || errno != EINTR)
5620 break;
5621 }
5622 return ret;
5623}
5624
5625/*
5626 * Version of write() that retries when interrupted by EINTR (possibly
5627 * by a SIGWINCH).
5628 */
5629 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005631{
5632 long ret = 0;
5633 long wlen;
5634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005635 // Repeat the write() so long it didn't fail, other than being interrupted
5636 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005637 while (ret < (long)bufsize)
5638 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005639 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005640 if (wlen < 0)
5641 {
5642 if (errno != EINTR)
5643 break;
5644 }
5645 else
5646 ret += wlen;
5647 }
5648 return ret;
5649}
5650#endif