blob: c33625ad66e4a942d05a9722f0277f4f7bf1d037 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaare3f915d2020-07-14 23:02:44 +020016#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010019#if (defined(UNIX) || defined(VMS)) && defined(FEAT_EVAL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020020# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010023#if defined(VMS) && defined(HAVE_XOS_R_H)
24# include <x11/xos_r.h>
25#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar217e1b82019-12-01 21:41:28 +010027// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000028#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar9d8bfae2020-09-02 21:21:35 +020030#if defined(__hpux) && !defined(HAVE_DIRFD)
31# define dirfd(x) ((x)->__dd_fd)
32# define HAVE_DIRFD
33#endif
34
Bram Moolenaarf077db22019-08-13 00:18:24 +020035static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010036#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010037static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020040static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000044
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020045#ifdef FEAT_EVAL
46static int readdirex_sort;
47#endif
48
Bram Moolenaar473952e2019-09-28 16:30:04 +020049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010050filemess(
51 buf_T *buf,
52 char_u *name,
53 char_u *s,
54 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055{
56 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020057 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
59 if (msg_silent != 0)
60 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010061 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020062
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020064 if (STRLEN(IObuff) > IOSIZE - 100)
65 IObuff[IOSIZE - 100] = NUL;
66
67 // Avoid an over-long translation to cause trouble.
68 STRNCAT(IObuff, s, 99);
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 /*
71 * For the first message may have to start a new line.
72 * For further ones overwrite the previous one, reset msg_scroll before
73 * calling filemess().
74 */
75 msg_scroll_save = msg_scroll;
76 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
77 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010078 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 check_for_delay(FALSE);
80 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020081 if (prev_msg_col != 0 && msg_col == 0)
82 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 msg_scroll = msg_scroll_save;
84 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010085 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
87 msg_clr_eos();
88 out_flush();
89 msg_scrolled_ign = FALSE;
90}
91
92/*
93 * Read lines from file "fname" into the buffer after line "from".
94 *
95 * 1. We allocate blocks with lalloc, as big as possible.
96 * 2. Each block is filled with characters from the file with a single read().
97 * 3. The lines are inserted in the buffer with ml_append().
98 *
99 * (caller must check that fname != NULL, unless READ_STDIN is used)
100 *
101 * "lines_to_skip" is the number of lines that must be skipped
102 * "lines_to_read" is the number of lines that are appended
103 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
104 *
105 * flags:
106 * READ_NEW starting to edit a new buffer
107 * READ_FILTER reading filter output
108 * READ_STDIN read from stdin instead of a file
109 * READ_BUFFER read from curbuf instead of a file (converting after reading
110 * stdin)
111 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200112 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200113 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100115 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 */
117 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100118readfile(
119 char_u *fname,
120 char_u *sfname,
121 linenr_T from,
122 linenr_T lines_to_skip,
123 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100124 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100125 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126{
127 int fd = 0;
128 int newfile = (flags & READ_NEW);
129 int check_readonly;
130 int filtering = (flags & READ_FILTER);
131 int read_stdin = (flags & READ_STDIN);
132 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200133 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000134 int set_options = newfile || read_buffer
135 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100136 linenr_T read_buf_lnum = 1; // next line to read from curbuf
137 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 char_u c;
139 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100140 char_u *ptr = NULL; // pointer into read buffer
141 char_u *buffer = NULL; // read buffer
142 char_u *new_buffer = NULL; // init to shut up gcc
143 char_u *line_start = NULL; // init to shut up gcc
144 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 colnr_T len;
146 long size = 0;
147 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200148 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 int skip_read = FALSE;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200150 off_T filesize_disk = 0; // file size read from disk
151 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#ifdef FEAT_CRYPT
153 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200154 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200156#ifdef FEAT_PERSISTENT_UNDO
157 context_sha256_T sha_ctx;
158 int read_undo_file = FALSE;
159#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100160 int split = 0; // number of split lines
161#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100163 int error = FALSE; // errors encountered
164 int ff_error = EOL_UNKNOWN; // file format with errors
165 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef UNIX
167 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100168 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#else
170 int perm;
171#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100172 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200174 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 int file_readonly;
176 linenr_T skip_count = 0;
177 linenr_T read_count = 0;
178 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100179 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
180 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100181 int try_mac;
182 int try_dos;
183 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 linenr_T conv_error = 0; // line nr with conversion error
187 linenr_T illegal_byte = 0; // line nr with illegal byte
188 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
189 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000190 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100191 // BAD_KEEP, BAD_DROP or character to
192 // replace with
193 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100195 char_u *fenc; // fileencoding to use
196 int fenc_alloced; // fenc_next is in allocated memory
197 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 int advance_fenc = FALSE;
199 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100200#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100201 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100202# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100203 int did_iconv = FALSE; // TRUE when iconv() failed and trying
204 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100206#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100207 int converted = FALSE; // TRUE if conversion done
208 int notconverted = FALSE; // TRUE if conversion wanted but it
209 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100211 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100212 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200213 buf_T *old_curbuf;
214 char_u *old_b_ffname;
215 char_u *old_b_fname;
216 int using_b_ffname;
217 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200218 static char *msg_is_a_directory = N_("is a directory");
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200219 int eof;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200220
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100221 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200222
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100223 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
225 /*
226 * If there is no file name yet, use the one for the read file.
227 * BF_NOTEDITED is set to reflect this.
228 * Don't do this for a read from a filter.
229 * Only do this when 'cpoptions' contains the 'f' flag.
230 */
231 if (curbuf->b_ffname == NULL
232 && !filtering
233 && fname != NULL
234 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
235 && !(flags & READ_DUMMY))
236 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000237 if (set_rw_fname(fname, sfname) == FAIL)
238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 }
240
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100241 // Remember the initial values of curbuf, curbuf->b_ffname and
242 // curbuf->b_fname to detect whether they are altered as a result of
243 // executing nasty autocommands. Also check if "fname" and "sfname"
244 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200245 old_curbuf = curbuf;
246 old_b_ffname = curbuf->b_ffname;
247 old_b_fname = curbuf->b_fname;
248 using_b_ffname = (fname == curbuf->b_ffname)
249 || (sfname == curbuf->b_ffname);
250 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200251
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100252 // After reading a file the cursor line changes but we don't want to
253 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000254 ex_no_reprint = TRUE;
255
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100256 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000257 need_fileinfo = FALSE;
258
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 /*
260 * For Unix: Use the short file name whenever possible.
261 * Avoids problems with networks and when directory names are changed.
262 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
263 * another directory, which we don't detect.
264 */
265 if (sfname == NULL)
266 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200267#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 fname = sfname;
269#endif
270
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 /*
272 * The BufReadCmd and FileReadCmd events intercept the reading process by
273 * executing the associated commands instead.
274 */
275 if (!filtering && !read_stdin && !read_buffer)
276 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100277 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100279 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
281 curbuf->b_op_start.col = 0;
282
283 if (newfile)
284 {
285 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
286 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200287 {
288 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200290 if (aborting())
291 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200293 // The BufReadCmd code usually uses ":read" to get the text and
294 // perhaps ":file" to change the buffer name. But we should
295 // consider this to work like ":edit", thus reset the
296 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
297 // same file.
298 if (status == OK)
299 curbuf->b_flags &= ~BF_NOTEDITED;
300 return status;
301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 }
303 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
304 FALSE, NULL, eap))
305#ifdef FEAT_EVAL
306 return aborting() ? FAIL : OK;
307#else
308 return OK;
309#endif
310
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100311 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100315 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100317 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000319 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200321 size_t namelen = STRLEN(fname);
322
323 // If the name is too long we might crash further on, quit here.
324 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000325 {
326 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
327 msg_end();
328 msg_scroll = msg_save;
329 return FAIL;
330 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200331
332 // If the name ends in a path separator, we can't open it. Check here,
333 // because reading the file may actually work, but then creating the
334 // swap file may destroy it! Reported on MS-DOS and Win 95.
335 if (after_pathsep(fname, fname + namelen))
336 {
337 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
338 msg_end();
339 msg_scroll = msg_save;
Bram Moolenaar40fa12a2021-09-22 14:18:13 +0200340 return NOTDONE;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 }
343
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200344 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100346#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200347 /*
348 * On Unix it is possible to read a directory, so we have to
349 * check for it before the mch_open().
350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100352 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
353 && !S_ISFIFO(perm) // ... or fifo
354 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000355# ifdef OPEN_CHR_FILES
356 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100357 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 )
360 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100361 int retval = FAIL;
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100364 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200365 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100366 retval = NOTDONE;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else
369 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
370 msg_end();
371 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100372 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200374#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100375#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000376 /*
377 * MS-Windows allows opening a device, but we will probably get stuck
378 * trying to read it.
379 */
380 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
381 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000382 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000383 msg_end();
384 msg_scroll = msg_save;
385 return FAIL;
386 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000387#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200388 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000389
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200391 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
393 /*
394 * When opening a new file we take the readonly flag from the file.
395 * Default is r/w, can be set to r/o below.
396 * Don't reset it when in readonly mode
397 * Only set/reset b_p_ro when BF_CHECK_RO is set.
398 */
399 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000400 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 curbuf->b_p_ro = FALSE;
402
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200403 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (mch_stat((char *)fname, &st) >= 0)
407 {
408 buf_store_time(curbuf, &st, fname);
409 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100410 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
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;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100435 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100437 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 curbuf->b_orig_size = 0;
439 curbuf->b_orig_mode = 0;
440 }
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Reset the "new file" flag. It will be set again below when the
443 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
445 }
446
447/*
448 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100449 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 */
451 file_readonly = FALSE;
452 if (read_stdin)
453 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100454#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100455 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 setmode(0, O_BINARY);
457#endif
458 }
459 else if (!read_buffer)
460 {
461#ifdef USE_MCH_ACCESS
462 if (
463# ifdef UNIX
464 !(perm & 0222) ||
465# endif
466 mch_access((char *)fname, W_OK))
467 file_readonly = TRUE;
468 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
469#else
470 if (!newfile
471 || readonlymode
472 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
473 {
474 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
477 }
478#endif
479 }
480
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100481 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
483#ifndef UNIX
484 int isdir_f;
485#endif
486 msg_scroll = msg_save;
487#ifndef UNIX
488 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100489 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 */
491 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (isdir_f)
494 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200495 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 else
499#endif
500 if (newfile)
501 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200502 if (perm < 0
503#ifdef ENOENT
504 && errno == ENOENT
505#endif
506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /*
509 * Set the 'new-file' flag, so that when the file has
510 * been created by someone else, a ":w" will complain.
511 */
512 curbuf->b_flags |= BF_NEW;
513
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100514 // Create a swap file now, so that other Vims are warned
515 // that we are editing this file. Don't do this for a
516 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517#ifdef FEAT_QUICKFIX
518 if (!bt_dontwrite(curbuf))
519#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100522 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000523 if (curbuf != old_curbuf
524 || (using_b_ffname
525 && (old_b_ffname != curbuf->b_ffname))
526 || (using_b_fname
527 && (old_b_fname != curbuf->b_fname)))
528 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000529 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000530 return FAIL;
531 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000532 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000533 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200534 filemess(curbuf, sfname,
535 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000536 else
537 filemess(curbuf, sfname,
538 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 // Even though this is a new file, it might have been
541 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 check_marks_read();
543#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200545 if (eap != NULL)
546 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
548 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100549 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 save_file_ff(curbuf);
551
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100552#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100553 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 return FAIL;
555#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100556 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 }
558 else
559 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000560 filemess(curbuf, sfname, (char_u *)(
561# ifdef EFBIG
562 (errno == EFBIG) ? _("[File too big]") :
563# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200564# ifdef EOVERFLOW
565 (errno == EOVERFLOW) ? _("[File too big]") :
566# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000567 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100568 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
570 }
571
572 return FAIL;
573 }
574
575 /*
576 * Only set the 'ro' flag for readonly files the first time they are
577 * loaded. Help files always get readonly mode
578 */
579 if ((check_readonly && file_readonly) || curbuf->b_help)
580 curbuf->b_p_ro = TRUE;
581
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100584 // Don't change 'eol' if reading from buffer as it will already be
585 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000586 if (!read_buffer)
587 {
588 curbuf->b_p_eol = TRUE;
589 curbuf->b_start_eol = TRUE;
590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000592 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100595 // Create a swap file now, so that other Vims are warned that we are
596 // editing this file.
597 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#ifdef FEAT_QUICKFIX
599 if (!bt_dontwrite(curbuf))
600#endif
601 {
602 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000603 if (!read_stdin && (curbuf != old_curbuf
604 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
605 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
606 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000607 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000608 if (!read_buffer)
609 close(fd);
610 return FAIL;
611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100613 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000614 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
615 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100616 {
617 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
618
619 /*
620 * If the group-read bit is set but not the world-read bit, then
621 * the group must be equal to the group of the original file. If
622 * we can't make that happen then reset the group-read bit. This
623 * avoids making the swap file readable to more users when the
624 * primary group of the user is too permissive.
625 */
626 if ((swap_mode & 044) == 040)
627 {
628 stat_T swap_st;
629
630 if (mch_stat((char *)swap_fname, &swap_st) >= 0
631 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200632# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100633 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200634 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200635# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200636 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100637 swap_mode &= 0600;
638 }
639
640 (void)mch_setperm(swap_fname, (long)swap_mode);
641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643 }
644
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200645 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (swap_exists_action == SEA_QUIT)
647 {
648 if (!read_buffer && !read_stdin)
649 close(fd);
650 return FAIL;
651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 /*
656 * Set '[ mark to the line above where the lines go (line 1 if zero).
657 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100658 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
660 curbuf->b_op_start.col = 0;
661
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100662 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
663 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
664 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (!read_buffer)
667 {
668 int m = msg_scroll;
669 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671 /*
672 * The file must be closed again, the autocommands may want to change
673 * the file before reading it.
674 */
675 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 /*
679 * The output from the autocommands should not overwrite anything and
680 * should not be overwritten: Set msg_scroll, restore its value if no
681 * output was done.
682 */
683 msg_scroll = TRUE;
684 if (filtering)
685 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
686 FALSE, curbuf, eap);
687 else if (read_stdin)
688 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
689 FALSE, curbuf, eap);
690 else if (newfile)
691 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
692 FALSE, curbuf, eap);
693 else
694 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
695 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100697 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
698 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
699 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100700 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (msg_scrolled == n)
703 msg_scroll = m;
704
705#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 --no_wait_return;
709 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return FAIL;
712 }
713#endif
714 /*
715 * Don't allow the autocommands to change the current buffer.
716 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000717 *
718 * Don't allow the autocommands to change the buffer name either
719 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 */
721 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000722 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
723 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
725 {
726 --no_wait_return;
727 msg_scroll = msg_save;
728 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000729 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000731 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100732 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return FAIL;
734 }
735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100737 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
739
740 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
741 {
742 /*
743 * Show the user that we are busy reading the input. Sometimes this
744 * may take a while. When reading from stdin another program may
745 * still be running, don't move the cursor to the last line, unless
746 * always using the GUI.
747 */
748 if (read_stdin)
749 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100750 if (!is_not_a_term())
751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200753# ifdef VIMDLL
754 if (!gui.in_use)
755# endif
756 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#endif
758#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100759 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100760 if (gui.in_use && !gui.dying && !gui.starting)
761 {
762 p = (char_u *)_("Reading from stdin...");
763 gui_write(p, (int)STRLEN(p));
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 else if (!read_buffer)
769 filemess(curbuf, sfname, (char_u *)"", 0);
770 }
771
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100772 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 /*
775 * Set linecnt now, before the "retry" caused by a wrong guess for
776 * fileformat, and after the autocommands, which may change them.
777 */
778 linecnt = curbuf->b_ml.ml_line_count;
779
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100780 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000781 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000782 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000783 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000784 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000785 curbuf->b_bad_char = eap->bad_char;
786 }
787 else
788 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000791 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 */
793 if (eap != NULL && eap->force_enc != 0)
794 {
795 fenc = enc_canonize(eap->cmd + eap->force_enc);
796 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000797 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799 else if (curbuf->b_p_bin)
800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 fenc_alloced = FALSE;
803 }
804 else if (curbuf->b_help)
805 {
806 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000807 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100809 // Help files are either utf-8 or latin1. Try utf-8 first, if this
810 // fails it must be latin1.
811 // Always do this when 'encoding' is "utf-8". Otherwise only do
812 // this when needed to avoid [converted] remarks all the time.
813 // It is needed when the first line contains non-ASCII characters.
814 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 fenc = (char_u *)"latin1";
816 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000817 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000819 fc = fname[STRLEN(fname) - 1];
820 if (TOLOWER_ASC(fc) == 'x')
821 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100822 // Read the first line (and a bit more). Immediately rewind to
823 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100824 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200825 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000826 for (p = firstline; p < firstline + len; ++p)
827 if (*p >= 0x80)
828 {
829 c = TRUE;
830 break;
831 }
832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834
835 if (c)
836 {
837 fenc_next = fenc;
838 fenc = (char_u *)"utf-8";
839
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // When the file is utf-8 but a character doesn't fit in
841 // 'encoding' don't retry. In help text editing utf-8 bytes
842 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000843 if (!enc_utf8)
844 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 fenc_alloced = FALSE;
847 }
848 else if (*p_fencs == NUL)
849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 fenc_alloced = FALSE;
852 }
853 else
854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100855 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200856 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /*
860 * Jump back here to retry reading the file in different ways.
861 * Reasons to retry:
862 * - encoding conversion failed: try another one from "fenc_next"
863 * - BOM detected and fenc was set, need to setup conversion
864 * - "fileformat" check failed: try another
865 *
866 * Variables set for special retry actions:
867 * "file_rewind" Rewind the file to start reading it again.
868 * "advance_fenc" Advance "fenc" using "fenc_next".
869 * "skip_read" Re-use already read bytes (BOM detected).
870 * "did_iconv" iconv() conversion failed, try 'charconvert'.
871 * "keep_fileformat" Don't reset "fileformat".
872 *
873 * Other status indicators:
874 * "tmpname" When != NULL did conversion with 'charconvert'.
875 * Output file has to be deleted afterwards.
876 * "iconv_fd" When != -1 did conversion with iconv().
877 */
878retry:
879
880 if (file_rewind)
881 {
882 if (read_buffer)
883 {
884 read_buf_lnum = 1;
885 read_buf_col = 0;
886 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200887 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100889 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 error = TRUE;
891 goto failed;
892 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200895 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000897 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000900 curbuf->b_start_bomb = FALSE;
901 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000902 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904
905 /*
906 * When retrying with another "fenc" and the first time "fileformat"
907 * will be reset.
908 */
909 if (keep_fileformat)
910 keep_fileformat = FALSE;
911 else
912 {
913 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000916 try_unix = try_dos = try_mac = FALSE;
917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100919 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100921 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100923 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925
Bram Moolenaar13505972019-01-24 15:04:48 +0100926#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (iconv_fd != (iconv_t)-1)
928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100929 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 iconv_close(iconv_fd);
931 iconv_fd = (iconv_t)-1;
932 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935 if (advance_fenc)
936 {
937 /*
938 * Try the next entry in 'fileencodings'.
939 */
940 advance_fenc = FALSE;
941
942 if (eap != NULL && eap->force_enc != 0)
943 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100944 // Conversion given with "++cc=" wasn't possible, read
945 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000947 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (fenc_alloced)
949 vim_free(fenc);
950 fenc = (char_u *)"";
951 fenc_alloced = FALSE;
952 }
953 else
954 {
955 if (fenc_alloced)
956 vim_free(fenc);
957 if (fenc_next != NULL)
958 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200959 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961 else
962 {
963 fenc = (char_u *)"";
964 fenc_alloced = FALSE;
965 }
966 }
967 if (tmpname != NULL)
968 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100969 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100970 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 }
973
974 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000975 * Conversion may be required when the encoding of the file is different
976 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 */
978 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000979 converted = need_conversion(fenc);
980 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 // "ucs-bom" means we need to check the first bytes of the file
984 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (STRCMP(fenc, ENC_UCSBOM) == 0)
986 fio_flags = FIO_UCSBOM;
987
988 /*
989 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
990 * done. This is handled below after read(). Prepare the
991 * fio_flags to avoid having to parse the string each time.
992 * Also check for Unicode to Latin1 conversion, because iconv()
993 * appears not to handle this correctly. This works just like
994 * conversion to UTF-8 except how the resulting character is put in
995 * the buffer.
996 */
997 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
998 fio_flags = get_fio_flags(fenc);
999
Bram Moolenaar4f974752019-02-17 17:44:42 +01001000#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1003 * is handled with MultiByteToWideChar().
1004 */
1005 if (fio_flags == 0)
1006 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaar13505972019-01-24 15:04:48 +01001009#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (fio_flags == 0)
1012 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaar13505972019-01-24 15:04:48 +01001015#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 /*
1017 * Try using iconv() if we can't convert internally.
1018 */
1019 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001020# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 )
1024 iconv_fd = (iconv_t)my_iconv_open(
1025 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
Bram Moolenaar13505972019-01-24 15:04:48 +01001028#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 /*
1030 * Use the 'charconvert' expression when conversion is required
1031 * and we can't do it internally or with iconv().
1032 */
1033 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001034 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001035# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 )
1039 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001040# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001042# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // Skip conversion when it's already done (retry for wrong
1044 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (tmpname == NULL)
1046 {
1047 tmpname = readfile_charconvert(fname, fenc, &fd);
1048 if (tmpname == NULL)
1049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 advance_fenc = TRUE;
1052 if (fd < 0)
1053 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001055 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 error = TRUE;
1057 goto failed;
1058 }
1059 goto retry;
1060 }
1061 }
1062 }
1063 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {
1066 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001067#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 )
1071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // Conversion wanted but we can't.
1073 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 advance_fenc = TRUE;
1075 goto retry;
1076 }
1077 }
1078 }
1079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Set "can_retry" when it's possible to rewind the file and try with
1081 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1082 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001083 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 if (!skip_read)
1086 {
1087 linerest = 0;
1088 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001089 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 skip_count = lines_to_skip;
1091 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001093#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001094 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1095 && curbuf->b_ffname != NULL
1096 && curbuf->b_p_udf
1097 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001098 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001099 && !read_stdin
1100 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001101 if (read_undo_file)
1102 sha256_start(&sha_ctx);
1103#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001104#ifdef FEAT_CRYPT
1105 if (curbuf->b_cryptstate != NULL)
1106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001107 // Need to free the state, but keep the key, don't want to ask for
1108 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001109 crypt_free_state(curbuf->b_cryptstate);
1110 curbuf->b_cryptstate = NULL;
1111 }
1112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114
1115 while (!error && !got_int)
1116 {
1117 /*
1118 * We allocate as much space for the file as we can get, plus
1119 * space for the old line plus room for one terminating NUL.
1120 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001121 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001123 if (!skip_read)
1124 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001125#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001127#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001128 // Use buffer >= 64K. Add linerest to double the size if the
1129 // line gets very long, to avoid a lot of copying. But don't
1130 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001131 size = 0x10000L + linerest;
1132 if (size > 0x100000L)
1133 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001134#endif
1135 }
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001138 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 size = 1;
1143 }
1144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 if (!skip_read)
1147 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001148 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001150 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 FALSE)) != NULL)
1152 break;
1153 }
1154 if (new_buffer == NULL)
1155 {
1156 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1157 error = TRUE;
1158 break;
1159 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001160 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1162 vim_free(buffer);
1163 buffer = new_buffer;
1164 ptr = buffer + linerest;
1165 line_start = buffer;
1166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001167 // May need room to translate into.
1168 // For iconv() we don't really know the required space, use a
1169 // factor ICONV_MULT.
1170 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1171 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1172 // become up to 4 bytes, size must be multiple of 2
1173 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1174 // multiple of 2
1175 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1176 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001177 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001178#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (iconv_fd != (iconv_t)-1)
1180 size = size / ICONV_MULT;
1181 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (fio_flags & FIO_LATIN1)
1184 size = size / 2;
1185 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1186 size = (size * 2 / 3) & ~1;
1187 else if (fio_flags & FIO_UCS4)
1188 size = (size * 2 / 3) & ~3;
1189 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001190 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001191#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001193 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001194#endif
1195#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001197 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#endif
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (conv_restlen > 0)
1201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001202 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 mch_memmove(ptr, conv_rest, conv_restlen);
1204 ptr += conv_restlen;
1205 size -= conv_restlen;
1206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
1208 if (read_buffer)
1209 {
1210 /*
1211 * Read bytes from curbuf. Used for converting text read
1212 * from stdin.
1213 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001214 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (read_buf_lnum > from)
1216 size = 0;
1217 else
1218 {
1219 int n, ni;
1220 long tlen;
1221
1222 tlen = 0;
1223 for (;;)
1224 {
1225 p = ml_get(read_buf_lnum) + read_buf_col;
1226 n = (int)STRLEN(p);
1227 if ((int)tlen + n + 1 > size)
1228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001229 // Filled up to "size", append partial line.
1230 // Change NL to NUL to reverse the effect done
1231 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001232 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 for (ni = 0; ni < n; ++ni)
1234 {
1235 if (p[ni] == NL)
1236 ptr[tlen++] = NUL;
1237 else
1238 ptr[tlen++] = p[ni];
1239 }
1240 read_buf_col += n;
1241 break;
1242 }
1243 else
1244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001245 // Append whole line and new-line. Change NL
1246 // to NUL to reverse the effect done below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 for (ni = 0; ni < n; ++ni)
1248 {
1249 if (p[ni] == NL)
1250 ptr[tlen++] = NUL;
1251 else
1252 ptr[tlen++] = p[ni];
1253 }
1254 ptr[tlen++] = NL;
1255 read_buf_col = 0;
1256 if (++read_buf_lnum > from)
1257 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001258 // When the last line didn't have an
1259 // end-of-line don't add it now either.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (!curbuf->b_p_eol)
1261 --tlen;
1262 size = tlen;
Christian Brabandt226b28b2021-06-21 21:08:08 +02001263 eof = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 break;
1265 }
1266 }
1267 }
1268 }
1269 }
1270 else
1271 {
1272 /*
1273 * Read bytes from the file.
1274 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001275# ifdef FEAT_SODIUM
1276 // Let the crypt layer work with a buffer size of 8192
1277 if (filesize == 0)
1278 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001279 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001280 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1281 + crypto_secretstream_xchacha20poly1305_ABYTES;
1282
1283 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1284 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1285 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1286# endif
1287 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001288 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001289 filesize_count += size;
1290 // hit end of file
1291 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001294#ifdef FEAT_CRYPT
1295 /*
1296 * At start of file: Check for magic number of encryption.
1297 */
1298 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001299 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001300 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1301 &filesize, newfile, sfname,
1302 &did_ask_for_key);
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001303# ifdef CRYPT_NOT_INPLACE
1304 if (curbuf->b_cryptstate != NULL
1305 && !crypt_works_inplace(curbuf->b_cryptstate))
1306 // reading undo file requires crypt_decode_inplace()
1307 read_undo_file = FALSE;
1308# endif
1309 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001310 /*
1311 * Decrypt the read bytes. This is done before checking for
1312 * EOF because the crypt layer may be buffering.
1313 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001314 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1315 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001316 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001317# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 if (crypt_works_inplace(curbuf->b_cryptstate))
1319 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001320# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001321 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1322 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001323# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001324 }
1325 else
1326 {
1327 char_u *newptr = NULL;
1328 int decrypted_size;
1329
1330 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001331 curbuf->b_cryptstate, ptr, size,
1332 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001333
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001334 if (decrypted_size < 0)
1335 {
1336 // error message already given
1337 error = TRUE;
1338 vim_free(newptr);
1339 break;
1340 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001341 // If the crypt layer is buffering, not producing
1342 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001343 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001344 continue;
1345
1346 if (linerest == 0)
1347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001348 // Simple case: reuse returned buffer (may be
1349 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001350 new_buffer = newptr;
1351 }
1352 else
1353 {
1354 long_u new_size;
1355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001356 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001357 new_size = (long_u)(decrypted_size + linerest + 1);
1358 new_buffer = lalloc(new_size, FALSE);
1359 if (new_buffer == NULL)
1360 {
1361 do_outofmem_msg(new_size);
1362 error = TRUE;
1363 break;
1364 }
1365
1366 mch_memmove(new_buffer, buffer, linerest);
1367 if (newptr != NULL)
1368 mch_memmove(new_buffer + linerest, newptr,
1369 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001370 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001371 }
1372
1373 if (new_buffer != NULL)
1374 {
1375 vim_free(buffer);
1376 buffer = new_buffer;
1377 new_buffer = NULL;
1378 line_start = buffer;
1379 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001380 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001381 }
1382 size = decrypted_size;
1383 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001384# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001385 }
1386#endif
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (size <= 0)
1389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001390 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001393 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001394 /*
1395 * Reached end-of-file but some trailing bytes could
1396 * not be converted. Truncated file?
1397 */
1398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001399 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001400 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001401#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001402 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001403#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001404 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001405 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001406 if (can_retry)
1407 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001408 if (conv_error == 0)
1409 conv_error = curbuf->b_ml.ml_line_count
1410 - linecnt + 1;
1411 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001413 else if (illegal_byte == 0)
1414 illegal_byte = curbuf->b_ml.ml_line_count
1415 - linecnt + 1;
1416 if (bad_char_behavior == BAD_DROP)
1417 {
1418 *(ptr - conv_restlen) = NUL;
1419 conv_restlen = 0;
1420 }
1421 else
1422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001423 // Replace the trailing bytes with the replacement
1424 // character if we were converting; if we weren't,
1425 // leave the UTF8 checking code to do it, as it
1426 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001427 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001428#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001429 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001430#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001431 ))
1432 {
1433 while (conv_restlen > 0)
1434 {
1435 *(--ptr) = bad_char_behavior;
1436 --conv_restlen;
1437 }
1438 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001439 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001440#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001441 if (iconv_fd != (iconv_t)-1)
1442 {
1443 iconv_close(iconv_fd);
1444 iconv_fd = (iconv_t)-1;
1445 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001446#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001447 }
1448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451 skip_read = FALSE;
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 /*
1454 * At start of file (or after crypt magic number): Check for BOM.
1455 * Also check for a BOM for other Unicode encodings, but not after
1456 * converting with 'charconvert' or when a BOM has already been
1457 * found.
1458 */
1459 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001460#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001461 || (cryptkey != NULL
1462 && filesize == crypt_get_header_len(
1463 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 )
1466 && (fio_flags == FIO_UCSBOM
1467 || (!curbuf->b_p_bomb
1468 && tmpname == NULL
1469 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1470 {
1471 char_u *ccname;
1472 int blen;
1473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001474 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (size < 2 || curbuf->b_p_bin)
1476 ccname = NULL;
1477 else
1478 ccname = check_for_bom(ptr, size, &blen,
1479 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1480 if (ccname != NULL)
1481 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001482 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 filesize += blen;
1484 size -= blen;
1485 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001486 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001489 curbuf->b_start_bomb = TRUE;
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492
1493 if (fio_flags == FIO_UCSBOM)
1494 {
1495 if (ccname == NULL)
1496 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001497 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 advance_fenc = TRUE;
1499 }
1500 else
1501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001502 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (fenc_alloced)
1504 vim_free(fenc);
1505 fenc = ccname;
1506 fenc_alloced = FALSE;
1507 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001508 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 skip_read = TRUE;
1510 goto retry;
1511 }
1512 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001515 ptr -= conv_restlen;
1516 size += conv_restlen;
1517 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 /*
1519 * Break here for a read error or end-of-file.
1520 */
1521 if (size <= 0)
1522 break;
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar13505972019-01-24 15:04:48 +01001525#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (iconv_fd != (iconv_t)-1)
1527 {
1528 /*
1529 * Attempt conversion of the read bytes to 'encoding' using
1530 * iconv().
1531 */
1532 const char *fromp;
1533 char *top;
1534 size_t from_size;
1535 size_t to_size;
1536
1537 fromp = (char *)ptr;
1538 from_size = size;
1539 ptr += size;
1540 top = (char *)ptr;
1541 to_size = real_size - size;
1542
1543 /*
1544 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001545 * another conversion. Except for when there is no
1546 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001548 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1549 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1551 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001552 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001553 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001554 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001555 if (conv_error == 0)
1556 conv_error = readfile_linenr(linecnt,
1557 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001559 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001560 ++fromp;
1561 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001562 if (bad_char_behavior == BAD_KEEP)
1563 {
1564 *top++ = *(fromp - 1);
1565 --to_size;
1566 }
1567 else if (bad_char_behavior != BAD_DROP)
1568 {
1569 *top++ = bad_char_behavior;
1570 --to_size;
1571 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 if (from_size > 0)
1575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001576 // Some remaining characters, keep them for the next
1577 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1579 conv_restlen = (int)from_size;
1580 }
1581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001582 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 line_start = ptr - linerest;
1584 mch_memmove(line_start, buffer, (size_t)linerest);
1585 size = (long)((char_u *)top - ptr);
1586 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaar4f974752019-02-17 17:44:42 +01001589#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (fio_flags & FIO_CODEPAGE)
1591 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001592 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001593 WCHAR ucs2buf[3];
1594 int ucs2len;
1595 int codepage = FIO_GET_CP(fio_flags);
1596 int bytelen;
1597 int found_bad;
1598 char replstr[2];
1599
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 /*
1601 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001602 * a codepage, using standard MS-Windows functions. This
1603 * requires two steps:
1604 * 1. convert from 'fileencoding' to ucs-2
1605 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001607 * Because there may be illegal bytes AND an incomplete byte
1608 * sequence at the end, we may have to do the conversion one
1609 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001612 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001613 if (bad_char_behavior > 0)
1614 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001616 replstr[0] = '?';
1617 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001620 * Move the bytes to the end of the buffer, so that we have
1621 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001623 src = ptr + real_size - size;
1624 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001626 /*
1627 * Do the conversion.
1628 */
1629 dst = ptr;
1630 size = size;
1631 while (size > 0)
1632 {
1633 found_bad = FALSE;
1634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001635# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001636 if (codepage == CP_UTF8)
1637 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // Handle CP_UTF8 input ourselves to be able to handle
1639 // trailing bytes properly.
1640 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001641 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001642 if (bytelen > size)
1643 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001644 // Only got some bytes of a character. Normally
1645 // it's put in "conv_rest", but if it's too long
1646 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001647 if (bytelen <= CONV_RESTLEN)
1648 break;
1649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001650 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001651 bytelen = size;
1652 found_bad = TRUE;
1653 }
1654 else
1655 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001656 int u8c = utf_ptr2char(src);
1657
Bram Moolenaar86e01082005-12-29 22:45:34 +00001658 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001659 found_bad = TRUE;
1660 ucs2buf[0] = u8c;
1661 ucs2len = 1;
1662 }
1663 }
1664 else
1665# endif
1666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001667 // We don't know how long the byte sequence is, try
1668 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001669 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1670 ++bytelen)
1671 {
1672 ucs2len = MultiByteToWideChar(codepage,
1673 MB_ERR_INVALID_CHARS,
1674 (LPCSTR)src, bytelen,
1675 ucs2buf, 3);
1676 if (ucs2len > 0)
1677 break;
1678 }
1679 if (ucs2len == 0)
1680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 // If we have only one byte then it's probably an
1682 // incomplete byte sequence. Otherwise discard
1683 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001684 if (size == 1)
1685 break;
1686 found_bad = TRUE;
1687 bytelen = 1;
1688 }
1689 }
1690
1691 if (!found_bad)
1692 {
1693 int i;
1694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001696 if (enc_utf8)
1697 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001698 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001699 for (i = 0; i < ucs2len; ++i)
1700 dst += utf_char2bytes(ucs2buf[i], dst);
1701 }
1702 else
1703 {
1704 BOOL bad = FALSE;
1705 int dstlen;
1706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001707 // From UCS-2 to "enc_codepage". If the
1708 // conversion uses the default character "?",
1709 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001710 dstlen = WideCharToMultiByte(enc_codepage, 0,
1711 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001712 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001713 replstr, &bad);
1714 if (bad)
1715 found_bad = TRUE;
1716 else
1717 dst += dstlen;
1718 }
1719 }
1720
1721 if (found_bad)
1722 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001723 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001724 if (can_retry)
1725 goto rewind_retry;
1726 if (conv_error == 0)
1727 conv_error = readfile_linenr(linecnt, ptr, dst);
1728 if (bad_char_behavior != BAD_DROP)
1729 {
1730 if (bad_char_behavior == BAD_KEEP)
1731 {
1732 mch_memmove(dst, src, bytelen);
1733 dst += bytelen;
1734 }
1735 else
1736 *dst++ = bad_char_behavior;
1737 }
1738 }
1739
1740 src += bytelen;
1741 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001743
1744 if (size > 0)
1745 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001746 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001747 mch_memmove(conv_rest, src, size);
1748 conv_restlen = size;
1749 }
1750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001752 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001755#endif
1756#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (fio_flags & FIO_MACROMAN)
1758 {
1759 /*
1760 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001761 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001763 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (fio_flags != 0)
1769 {
1770 int u8c;
1771 char_u *dest;
1772 char_u *tail = NULL;
1773
1774 /*
1775 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1776 * "enc_utf8" not set: Convert Unicode to Latin1.
1777 * Go from end to start through the buffer, because the number
1778 * of bytes may increase.
1779 * "dest" points to after where the UTF-8 bytes go, "p" points
1780 * to after the next character to convert.
1781 */
1782 dest = ptr + real_size;
1783 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1784 {
1785 p = ptr + size;
1786 if (fio_flags == FIO_UTF8)
1787 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001788 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 tail = ptr + size - 1;
1790 while (tail > ptr && (*tail & 0xc0) == 0x80)
1791 --tail;
1792 if (tail + utf_byte2len(*tail) <= ptr + size)
1793 tail = NULL;
1794 else
1795 p = tail;
1796 }
1797 }
1798 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001800 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 p = ptr + (size & ~1);
1802 if (size & 1)
1803 tail = p;
1804 if ((fio_flags & FIO_UTF16) && p > ptr)
1805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (fio_flags & FIO_ENDIAN_L)
1808 {
1809 u8c = (*--p << 8);
1810 u8c += *--p;
1811 }
1812 else
1813 {
1814 u8c = *--p;
1815 u8c += (*--p << 8);
1816 }
1817 if (u8c >= 0xd800 && u8c <= 0xdbff)
1818 tail = p;
1819 else
1820 p += 2;
1821 }
1822 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001823 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001825 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 p = ptr + (size & ~3);
1827 if (size & 3)
1828 tail = p;
1829 }
1830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001831 // If there is a trailing incomplete sequence move it to
1832 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 if (tail != NULL)
1834 {
1835 conv_restlen = (int)((ptr + size) - tail);
1836 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1837 size -= conv_restlen;
1838 }
1839
1840
1841 while (p > ptr)
1842 {
1843 if (fio_flags & FIO_LATIN1)
1844 u8c = *--p;
1845 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1846 {
1847 if (fio_flags & FIO_ENDIAN_L)
1848 {
1849 u8c = (*--p << 8);
1850 u8c += *--p;
1851 }
1852 else
1853 {
1854 u8c = *--p;
1855 u8c += (*--p << 8);
1856 }
1857 if ((fio_flags & FIO_UTF16)
1858 && u8c >= 0xdc00 && u8c <= 0xdfff)
1859 {
1860 int u16c;
1861
1862 if (p == ptr)
1863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001864 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (can_retry)
1866 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001867 if (conv_error == 0)
1868 conv_error = readfile_linenr(linecnt,
1869 ptr, p);
1870 if (bad_char_behavior == BAD_DROP)
1871 continue;
1872 if (bad_char_behavior != BAD_KEEP)
1873 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001876 // found second word of double-word, get the first
1877 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (fio_flags & FIO_ENDIAN_L)
1879 {
1880 u16c = (*--p << 8);
1881 u16c += *--p;
1882 }
1883 else
1884 {
1885 u16c = *--p;
1886 u16c += (*--p << 8);
1887 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001888 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1889 + (u8c & 0x3ff);
1890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001891 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (u16c < 0xd800 || u16c > 0xdbff)
1893 {
1894 if (can_retry)
1895 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001896 if (conv_error == 0)
1897 conv_error = readfile_linenr(linecnt,
1898 ptr, p);
1899 if (bad_char_behavior == BAD_DROP)
1900 continue;
1901 if (bad_char_behavior != BAD_KEEP)
1902 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 }
1906 else if (fio_flags & FIO_UCS4)
1907 {
1908 if (fio_flags & FIO_ENDIAN_L)
1909 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001910 u8c = (unsigned)*--p << 24;
1911 u8c += (unsigned)*--p << 16;
1912 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 u8c += *--p;
1914 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001915 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001918 u8c += (unsigned)*--p << 8;
1919 u8c += (unsigned)*--p << 16;
1920 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001923 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 if (*--p < 0x80)
1926 u8c = *p;
1927 else
1928 {
1929 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001930 p -= len;
1931 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (len == 0)
1933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001934 // Not a valid UTF-8 character, retry with
1935 // another fenc when possible, otherwise just
1936 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (can_retry)
1938 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001939 if (conv_error == 0)
1940 conv_error = readfile_linenr(linecnt,
1941 ptr, p);
1942 if (bad_char_behavior == BAD_DROP)
1943 continue;
1944 if (bad_char_behavior != BAD_KEEP)
1945 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001949 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 dest -= utf_char2len(u8c);
1952 (void)utf_char2bytes(u8c, dest);
1953 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 --dest;
1957 if (u8c >= 0x100)
1958 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001959 // character doesn't fit in latin1, retry with
1960 // another fenc when possible, otherwise just
1961 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001962 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001964 if (conv_error == 0)
1965 conv_error = readfile_linenr(linecnt, ptr, p);
1966 if (bad_char_behavior == BAD_DROP)
1967 ++dest;
1968 else if (bad_char_behavior == BAD_KEEP)
1969 *dest = u8c;
1970 else if (eap != NULL && eap->bad_char != 0)
1971 *dest = bad_char_behavior;
1972 else
1973 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975 else
1976 *dest = u8c;
1977 }
1978 }
1979
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 line_start = dest - linerest;
1982 mch_memmove(line_start, buffer, (size_t)linerest);
1983 size = (long)((ptr + real_size) - dest);
1984 ptr = dest;
1985 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001986 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001988 int incomplete_tail = FALSE;
1989
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001990 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001991 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001993 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001994 int l;
1995
1996 if (todo <= 0)
1997 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 if (*p >= 0x80)
1999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002000 // A length of 1 means it's an illegal byte. Accept
2001 // an incomplete character at the end though, the next
2002 // read() will get the next bytes, we'll check it
2003 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002004 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002005 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002007 // Avoid retrying with a different encoding when
2008 // a truncated file is more likely, or attempting
2009 // to read the rest of an incomplete sequence when
2010 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002011 if (p > ptr || filesize > 0)
2012 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 // Incomplete byte sequence, move it to conv_rest[]
2014 // and try to read the rest of it, unless we've
2015 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002016 if (p > ptr)
2017 {
2018 conv_restlen = todo;
2019 mch_memmove(conv_rest, p, conv_restlen);
2020 size -= conv_restlen;
2021 break;
2022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002024 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // Illegal byte. If we can try another encoding
2027 // do that, unless at EOF where a truncated
2028 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002029 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002030 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002031#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002033 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2034 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002035#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002036 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002037 if (conv_error == 0 && illegal_byte == 0)
2038 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002040 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002041 if (bad_char_behavior == BAD_DROP)
2042 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002043 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002044 --p;
2045 --size;
2046 }
2047 else if (bad_char_behavior != BAD_KEEP)
2048 *p = bad_char_behavior;
2049 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002050 else
2051 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002054 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002056 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002059#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002060 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002062 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002064#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002066 advance_fenc = TRUE;
2067 file_rewind = TRUE;
2068 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 filesize += size;
2074
2075 /*
2076 * when reading the first part of a file: guess EOL type
2077 */
2078 if (fileformat == EOL_UNKNOWN)
2079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (try_dos || try_unix)
2082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002083 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002084 if (try_mac)
2085 try_mac = 1;
2086
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 for (p = ptr; p < ptr + size; ++p)
2088 {
2089 if (*p == NL)
2090 {
2091 if (!try_unix
2092 || (try_dos && p > ptr && p[-1] == CAR))
2093 fileformat = EOL_DOS;
2094 else
2095 fileformat = EOL_UNIX;
2096 break;
2097 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002098 else if (*p == CAR && try_mac)
2099 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (fileformat == EOL_UNIX && try_mac)
2104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 try_mac = 1;
2107 try_unix = 1;
2108 for (; p >= ptr && *p != CAR; p--)
2109 ;
2110 if (p >= ptr)
2111 {
2112 for (p = ptr; p < ptr + size; ++p)
2113 {
2114 if (*p == NL)
2115 try_unix++;
2116 else if (*p == CAR)
2117 try_mac++;
2118 }
2119 if (try_mac > try_unix)
2120 fileformat = EOL_MAC;
2121 }
2122 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002123 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 // Looking for CR but found no end-of-line markers at
2125 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002126 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if (fileformat == EOL_UNKNOWN && try_mac)
2131 fileformat = EOL_MAC;
2132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (fileformat == EOL_UNKNOWN)
2135 fileformat = default_fileformat();
2136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 set_fileformat(fileformat, OPT_LOCAL);
2140 }
2141 }
2142
2143 /*
2144 * This loop is executed once for every character read.
2145 * Keep it fast!
2146 */
2147 if (fileformat == EOL_MAC)
2148 {
2149 --ptr;
2150 while (++ptr, --size >= 0)
2151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if ((c = *ptr) != NUL && c != CAR && c != NL)
2154 continue;
2155 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002158 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else
2160 {
2161 if (skip_count == 0)
2162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 len = (colnr_T) (ptr - line_start + 1);
2165 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2166 {
2167 error = TRUE;
2168 break;
2169 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002170#ifdef FEAT_PERSISTENT_UNDO
2171 if (read_undo_file)
2172 sha256_update(&sha_ctx, line_start, len);
2173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ++lnum;
2175 if (--read_count == 0)
2176 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002177 error = TRUE; // break loop
2178 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 break;
2180 }
2181 }
2182 else
2183 --skip_count;
2184 line_start = ptr + 1;
2185 }
2186 }
2187 }
2188 else
2189 {
2190 --ptr;
2191 while (++ptr, --size >= 0)
2192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 continue;
2195 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 else
2198 {
2199 if (skip_count == 0)
2200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 len = (colnr_T)(ptr - line_start + 1);
2203 if (fileformat == EOL_DOS)
2204 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002205 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002207 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 ptr[-1] = NUL;
2209 --len;
2210 }
2211 /*
2212 * Reading in Dos format, but no CR-LF found!
2213 * When 'fileformats' includes "unix", delete all
2214 * the lines read so far and start all over again.
2215 * Otherwise give an error message later.
2216 */
2217 else if (ff_error != EOL_DOS)
2218 {
2219 if ( try_unix
2220 && !read_stdin
2221 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002222 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2223 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 set_fileformat(EOL_UNIX, OPT_LOCAL);
2228 file_rewind = TRUE;
2229 keep_fileformat = TRUE;
2230 goto retry;
2231 }
2232 ff_error = EOL_DOS;
2233 }
2234 }
2235 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2236 {
2237 error = TRUE;
2238 break;
2239 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002240#ifdef FEAT_PERSISTENT_UNDO
2241 if (read_undo_file)
2242 sha256_update(&sha_ctx, line_start, len);
2243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 ++lnum;
2245 if (--read_count == 0)
2246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 error = TRUE; // break loop
2248 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 break;
2250 }
2251 }
2252 else
2253 --skip_count;
2254 line_start = ptr + 1;
2255 }
2256 }
2257 }
2258 linerest = (long)(ptr - line_start);
2259 ui_breakcheck();
2260 }
2261
2262failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (error && read_count == 0)
2265 error = FALSE;
2266
2267 /*
2268 * If we get EOF in the middle of a line, note the fact and
2269 * complete the line ourselves.
2270 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2271 */
2272 if (!error
2273 && !got_int
2274 && linerest != 0
2275 && !(!curbuf->b_p_bin
2276 && fileformat == EOL_DOS
2277 && *line_start == Ctrl_Z
2278 && ptr == line_start + 1))
2279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002280 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 curbuf->b_p_eol = FALSE;
2283 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002284 len = (colnr_T)(ptr - line_start + 1);
2285 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 error = TRUE;
2287 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002288 {
2289#ifdef FEAT_PERSISTENT_UNDO
2290 if (read_undo_file)
2291 sha256_update(&sha_ctx, line_start, len);
2292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
2300#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002301 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002302 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002303 crypt_free_state(curbuf->b_cryptstate);
2304 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002305 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002306 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2307 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002308 // Don't set cryptkey to NULL, it's used below as a flag that
2309 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310#endif
2311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // If editing a new file: set 'fenc' for the current buffer.
2313 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002314 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002316 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (fenc_alloced)
2318 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002319#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#endif
2323
2324 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002325 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002326#ifdef HAVE_FD_CLOEXEC
2327 else
2328 {
2329 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002330
Bram Moolenaarf05da212009-11-17 16:13:15 +00002331 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002332 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002333 }
2334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 vim_free(buffer);
2336
2337#ifdef HAVE_DUP
2338 if (read_stdin)
2339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002340 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002342 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344#endif
2345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (tmpname != NULL)
2347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002348 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 vim_free(tmpname);
2350 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002351 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 /*
2354 * In recovery mode everything but autocommands is skipped.
2355 */
2356 if (!recoverymode)
2357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2360 {
2361#ifdef FEAT_NETBEANS_INTG
2362 netbeansFireChanges = 0;
2363#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002364 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_NETBEANS_INTG
2366 netbeansFireChanges = 1;
2367#endif
2368 --linecnt;
2369 }
2370 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2371 if (filesize == 0)
2372 linecnt = 0;
2373 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002376#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002377 // After reading the text into the buffer the diff info needs to
2378 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002379 diff_invalidate(curbuf);
2380#endif
2381#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 // All folds in the window are invalid now. Mark them for update
2383 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002384 foldUpdateAll(curwin);
2385#endif
2386 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002387 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 appended_lines_mark(from, linecnt);
2389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390#ifndef ALWAYS_USE_GUI
2391 /*
2392 * If we were reading from the same terminal as where messages go,
2393 * the screen will have been messed up.
2394 * Switch on raw mode now and clear the screen.
2395 */
2396 if (read_stdin)
2397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002398 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 starttermcap();
2400 screenclear();
2401 }
2402#endif
2403
2404 if (got_int)
2405 {
2406 if (!(flags & READ_DUMMY))
2407 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002408 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002410 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 msg_scroll = msg_save;
2413#ifdef FEAT_VIMINFO
2414 check_marks_read();
2415#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002416 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418
2419 if (!filtering && !(flags & READ_DUMMY))
2420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002421 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 c = FALSE;
2423
2424#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002425 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 STRCAT(IObuff, _("[fifo]"));
2428 c = TRUE;
2429 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 STRCAT(IObuff, _("[socket]"));
2433 c = TRUE;
2434 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002435# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002436 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002437 {
2438 STRCAT(IObuff, _("[character special]"));
2439 c = TRUE;
2440 }
2441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442#endif
2443 if (curbuf->b_p_ro)
2444 {
2445 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2446 c = TRUE;
2447 }
2448 if (read_no_eol_lnum)
2449 {
2450 msg_add_eol();
2451 c = TRUE;
2452 }
2453 if (ff_error == EOL_DOS)
2454 {
2455 STRCAT(IObuff, _("[CR missing]"));
2456 c = TRUE;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (split)
2459 {
2460 STRCAT(IObuff, _("[long lines split]"));
2461 c = TRUE;
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (notconverted)
2464 {
2465 STRCAT(IObuff, _("[NOT converted]"));
2466 c = TRUE;
2467 }
2468 else if (converted)
2469 {
2470 STRCAT(IObuff, _("[converted]"));
2471 c = TRUE;
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473#ifdef FEAT_CRYPT
2474 if (cryptkey != NULL)
2475 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002476 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 c = TRUE;
2478 }
2479#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002480 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002482 sprintf((char *)IObuff + STRLEN(IObuff),
2483 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 c = TRUE;
2485 }
2486 else if (illegal_byte > 0)
2487 {
2488 sprintf((char *)IObuff + STRLEN(IObuff),
2489 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2490 c = TRUE;
2491 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002492 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 STRCAT(IObuff, _("[READ ERRORS]"));
2495 c = TRUE;
2496 }
2497 if (msg_add_fileformat(fileformat))
2498 c = TRUE;
2499#ifdef FEAT_CRYPT
2500 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002501 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002502 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 else
2504#endif
2505 msg_add_lines(c, (long)linecnt, filesize);
2506
Bram Moolenaard23a8232018-02-10 18:45:26 +01002507 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 msg_scrolled_ign = TRUE;
2509#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002510 // Don't show the message when reading stdin, it would end up in a
2511 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (read_stdin || read_buffer)
2513 p = msg_may_trunc(FALSE, IObuff);
2514 else
2515#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002516 {
2517 if (msg_col > 0)
2518 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002519 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002522 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002523 // Need to repeat the message after redrawing when:
2524 // - When reading from stdin (the screen will be cleared next).
2525 // - When restart_edit is set (otherwise there will be a delay
2526 // before redrawing).
2527 // - When the screen was scrolled but there is no wait-return
2528 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002529 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 msg_scrolled_ign = FALSE;
2531 }
2532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002533 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002535 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002536 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 curbuf->b_p_ro = TRUE;
2538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002539 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 /*
2542 * In Ex mode: cursor at last new line.
2543 * Otherwise: cursor at first new line.
2544 */
2545 if (exmode_active)
2546 curwin->w_cursor.lnum = from + linecnt;
2547 else
2548 curwin->w_cursor.lnum = from + 1;
2549 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002550 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaare1004402020-10-24 20:49:43 +02002552 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002553 {
2554 // Set '[ and '] marks to the newly read lines.
2555 curbuf->b_op_start.lnum = from + 1;
2556 curbuf->b_op_start.col = 0;
2557 curbuf->b_op_end.lnum = from + linecnt;
2558 curbuf->b_op_end.col = 0;
2559 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002560
Bram Moolenaar4f974752019-02-17 17:44:42 +01002561#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002562 /*
2563 * Work around a weird problem: When a file has two links (only
2564 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002565 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002566 * It's correct again after reading the file, thus reset the timestamp
2567 * here.
2568 */
2569 if (newfile && !read_stdin && !read_buffer
2570 && mch_stat((char *)fname, &st) >= 0)
2571 {
2572 buf_store_time(curbuf, &st, fname);
2573 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002574 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002575 }
2576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578 msg_scroll = msg_save;
2579
2580#ifdef FEAT_VIMINFO
2581 /*
2582 * Get the marks before executing autocommands, so they can be used there.
2583 */
2584 check_marks_read();
2585#endif
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002588 * We remember if the last line of the read didn't have
2589 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2590 * or writing the read again with 'binary' on. The latter is required
2591 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002593 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002595 // When reloading a buffer put the cursor at the first line that is
2596 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002597 if (flags & READ_KEEP_UNDO)
2598 u_find_first_changed();
2599
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002600#ifdef FEAT_PERSISTENT_UNDO
2601 /*
2602 * When opening a new file locate undo info and read it.
2603 */
2604 if (read_undo_file)
2605 {
2606 char_u hash[UNDO_HASH_SIZE];
2607
2608 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002609 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002610 }
2611#endif
2612
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002613 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 int m = msg_scroll;
2616 int n = msg_scrolled;
2617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002618 // Save the fileformat now, otherwise the buffer will be considered
2619 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002620 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 save_file_ff(curbuf);
2622
2623 /*
2624 * The output from the autocommands should not overwrite anything and
2625 * should not be overwritten: Set msg_scroll, restore its value if no
2626 * output was done.
2627 */
2628 msg_scroll = TRUE;
2629 if (filtering)
2630 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2631 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002632 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2635 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002636 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2637 /*
2638 * EVENT_FILETYPE was not triggered but the buffer already has a
2639 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2640 */
2641 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2642 TRUE, curbuf);
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else
2645 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2646 FALSE, NULL, eap);
2647 if (msg_scrolled == n)
2648 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002649# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655 if (recoverymode && error)
2656 return FAIL;
2657 return OK;
2658}
2659
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002660#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002661/*
2662 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2663 * which is the name of files used for process substitution output by
2664 * some shells on some operating systems, e.g., bash on SunOS.
2665 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2666 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002667 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002668is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002669{
2670 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2671 && VIM_ISDIGIT(fname[8])
2672 && *skipdigits(fname + 9) == NUL
2673 && (fname[9] != NUL
2674 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2675}
2676#endif
2677
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002678/*
2679 * From the current line count and characters read after that, estimate the
2680 * line number where we are now.
2681 * Used for error messages that include a line number.
2682 */
2683 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002684readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002685 linenr_T linecnt, // line count before reading more bytes
2686 char_u *p, // start of more bytes read
2687 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002688{
2689 char_u *s;
2690 linenr_T lnum;
2691
2692 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2693 for (s = p; s < endp; ++s)
2694 if (*s == '\n')
2695 ++lnum;
2696 return lnum;
2697}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002700 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2701 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 * Returns OK or FAIL.
2703 */
2704 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002705prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaar13505972019-01-24 15:04:48 +01002707 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (eap->cmd == NULL)
2709 return FAIL;
2710
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002711 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2712 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002713 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002714 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002715
2716 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002717 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002718 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return OK;
2720}
2721
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002722/*
2723 * Set default or forced 'fileformat' and 'binary'.
2724 */
2725 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002726set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002727{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002728 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002729 if (set_options)
2730 {
2731 if (eap != NULL && eap->force_ff != 0)
2732 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2733 else if (*p_ffs != NUL)
2734 set_fileformat(default_fileformat(), OPT_LOCAL);
2735 }
2736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002738 if (eap != NULL && eap->force_bin != 0)
2739 {
2740 int oldval = curbuf->b_p_bin;
2741
2742 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2743 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2744 }
2745}
2746
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002747/*
2748 * Set forced 'fileencoding'.
2749 */
2750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002751set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002752{
2753 if (eap->force_enc != 0)
2754 {
2755 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2756
2757 if (fenc != NULL)
2758 set_string_option_direct((char_u *)"fenc", -1,
2759 fenc, OPT_FREE|OPT_LOCAL, 0);
2760 vim_free(fenc);
2761 }
2762}
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764/*
2765 * Find next fileencoding to use from 'fileencodings'.
2766 * "pp" points to fenc_next. It's advanced to the next item.
2767 * When there are no more items, an empty string is returned and *pp is set to
2768 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002769 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2770 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 */
2772 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002773next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 char_u *p;
2776 char_u *r;
2777
Bram Moolenaarf077db22019-08-13 00:18:24 +02002778 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (**pp == NUL)
2780 {
2781 *pp = NULL;
2782 return (char_u *)"";
2783 }
2784 p = vim_strchr(*pp, ',');
2785 if (p == NULL)
2786 {
2787 r = enc_canonize(*pp);
2788 *pp += STRLEN(*pp);
2789 }
2790 else
2791 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002792 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 *pp = p + 1;
2794 if (r != NULL)
2795 {
2796 p = enc_canonize(r);
2797 vim_free(r);
2798 r = p;
2799 }
2800 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002801 if (r != NULL)
2802 *alloced = TRUE;
2803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002805 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 r = (char_u *)"";
2807 *pp = NULL;
2808 }
2809 return r;
2810}
2811
Bram Moolenaar13505972019-01-24 15:04:48 +01002812#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813/*
2814 * Convert a file with the 'charconvert' expression.
2815 * This closes the file which is to be read, converts it and opens the
2816 * resulting file for reading.
2817 * Returns name of the resulting converted file (the caller should delete it
2818 * after reading it).
2819 * Returns NULL if the conversion failed ("*fdp" is not set) .
2820 */
2821 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002822readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002823 char_u *fname, // name of input file
2824 char_u *fenc, // converted from
2825 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002828 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002830 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002832 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else
2834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002835 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 *fdp = -1;
2837 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2838 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002839 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2841 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002842 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844
2845 if (errmsg != NULL)
2846 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002847 // Don't use emsg(), it breaks mappings, the retry with
2848 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002849 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (tmpname != NULL)
2851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002853 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 }
2856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (*fdp < 0)
2859 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2860
2861 return tmpname;
2862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
2864
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002865#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002867 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2869 * *filesizep are updated.
2870 * Return the (new) encryption key, NULL for no encryption.
2871 */
2872 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002873check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 char_u *cryptkey, // previous encryption key or NULL
2875 char_u *ptr, // pointer to read bytes
2876 long *sizep, // length of read bytes
2877 off_T *filesizep, // nr of bytes used from file
2878 int newfile, // editing a new buffer
2879 char_u *fname, // file name to display
2880 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002882 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002883 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002884
2885 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002887 // Mark the buffer as read-only until the decryption has taken place.
2888 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002889 curbuf->b_p_ro = TRUE;
2890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002892 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002893 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
2895 if (*curbuf->b_p_key)
2896 cryptkey = curbuf->b_p_key;
2897 else
2898 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002899 // When newfile is TRUE, store the typed key in the 'key'
2900 // option and don't free it. bf needs hash of the key saved.
2901 // Don't ask for the key again when first time Enter was hit.
2902 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002903 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002904 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002905 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002906 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002907 *did_ask = TRUE;
2908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002909 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (cryptkey != NULL && *cryptkey == NUL)
2911 {
2912 if (cryptkey != curbuf->b_p_key)
2913 vim_free(cryptkey);
2914 cryptkey = NULL;
2915 }
2916 }
2917 }
2918
2919 if (cryptkey != NULL)
2920 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002921 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002922
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002923 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002924 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002925 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002926 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002927
2928 curbuf->b_cryptstate = crypt_create_from_header(
2929 method, cryptkey, ptr);
2930 crypt_set_cm_option(curbuf, method);
2931
2932 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002933 *filesizep += header_len;
2934 *sizep -= header_len;
2935 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002938 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002941 // When starting to edit a new file which does not have encryption, clear
2942 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002943 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2945
2946 return cryptkey;
2947}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002948#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002951 * Return TRUE if a file appears to be read-only from the file permissions.
2952 */
2953 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002954check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002955 char_u *fname, // full path to file
2956 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002957{
2958#ifndef USE_MCH_ACCESS
2959 int fd = 0;
2960#endif
2961
2962 return (
2963#ifdef USE_MCH_ACCESS
2964# ifdef UNIX
2965 (perm & 0222) == 0 ||
2966# endif
2967 mch_access((char *)fname, W_OK)
2968#else
2969 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2970 ? TRUE : (close(fd), FALSE)
2971#endif
2972 );
2973}
2974
Bram Moolenaara7870192019-02-14 12:56:36 +01002975#if defined(HAVE_FSYNC) || defined(PROTO)
2976/*
2977 * Call fsync() with Mac-specific exception.
2978 * Return fsync() result: zero for success.
2979 */
2980 int
2981vim_fsync(int fd)
2982{
2983 int r;
2984
2985# ifdef MACOS_X
2986 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002987 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002988# endif
2989 r = fsync(fd);
2990 return r;
2991}
2992#endif
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002995 * Set the name of the current buffer. Use when the buffer doesn't have a
2996 * name and a ":r" or ":w" command with a file name is used.
2997 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02002998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002999set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003000{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003001 buf_T *buf = curbuf;
3002
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003003 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003004 if (curbuf->b_p_bl)
3005 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3006 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003007#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003008 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003009 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003010#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003011 if (curbuf != buf)
3012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003014 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003015 return FAIL;
3016 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003017
3018 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3019 curbuf->b_flags |= BF_NOTEDITED;
3020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003021 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003022 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3023 if (curbuf->b_p_bl)
3024 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003025#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003026 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003027 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003028#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003030 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003031 if (*curbuf->b_p_ft == NUL)
3032 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003033 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003034 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003035 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003036 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003037
3038 return OK;
3039}
3040
3041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 * Put file name into IObuff with quotes.
3043 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003045msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 if (fname == NULL)
3048 fname = (char_u *)"-stdin-";
3049 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3050 IObuff[0] = '"';
3051 STRCAT(IObuff, "\" ");
3052}
3053
3054/*
3055 * Append message for text mode to IObuff.
3056 * Return TRUE if something appended.
3057 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061#ifndef USE_CRNL
3062 if (eol_type == EOL_DOS)
3063 {
3064 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3065 return TRUE;
3066 }
3067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (eol_type == EOL_MAC)
3069 {
3070 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3071 return TRUE;
3072 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003073#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (eol_type == EOL_UNIX)
3075 {
3076 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3077 return TRUE;
3078 }
3079#endif
3080 return FALSE;
3081}
3082
3083/*
3084 * Append line and character count to IObuff.
3085 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087msg_add_lines(
3088 int insert_space,
3089 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003090 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 char_u *p;
3093
3094 p = IObuff + STRLEN(IObuff);
3095
3096 if (insert_space)
3097 *p++ = ' ';
3098 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003099 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003100 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
3102 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003103 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003105 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003106 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003107 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
3109}
3110
3111/*
3112 * Append message for missing line separator to IObuff.
3113 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003115msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3118}
3119
Bram Moolenaar473952e2019-09-28 16:30:04 +02003120 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003121time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
ichizokdef69df2021-10-15 17:23:12 +01003123 return
3124#ifdef ST_MTIM_NSEC
3125 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
ichizokdef69df2021-10-15 17:23:12 +01003127#if defined(__linux__) || defined(MSWIN)
3128 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3129 // the seconds. Since the roundoff is done when flushing the inode, the
3130 // time may change unexpectedly by one second!!!
3131 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3132#else
3133 (long)st->st_mtime != mtime
3134#endif
3135 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136}
3137
3138/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003139 * Return TRUE if file encoding "fenc" requires conversion from or to
3140 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003142 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003143need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003145 int same_encoding;
3146 int enc_flags;
3147 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003149 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003150 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003151 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003152 fenc_flags = 0;
3153 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003154 else
3155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003156 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3157 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003158 enc_flags = get_fio_flags(p_enc);
3159 fenc_flags = get_fio_flags(fenc);
3160 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3161 }
3162 if (same_encoding)
3163 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003164 // Specified encoding matches with 'encoding'. This requires
3165 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003166 return enc_unicode != 0;
3167 }
3168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003169 // Encodings differ. However, conversion is not needed when 'enc' is any
3170 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003171 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172}
3173
3174/*
3175 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3176 * internal conversion.
3177 * if "ptr" is an empty string, use 'encoding'.
3178 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003180get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 int prop;
3183
3184 if (*ptr == NUL)
3185 ptr = p_enc;
3186
3187 prop = enc_canon_props(ptr);
3188 if (prop & ENC_UNICODE)
3189 {
3190 if (prop & ENC_2BYTE)
3191 {
3192 if (prop & ENC_ENDIAN_L)
3193 return FIO_UCS2 | FIO_ENDIAN_L;
3194 return FIO_UCS2;
3195 }
3196 if (prop & ENC_4BYTE)
3197 {
3198 if (prop & ENC_ENDIAN_L)
3199 return FIO_UCS4 | FIO_ENDIAN_L;
3200 return FIO_UCS4;
3201 }
3202 if (prop & ENC_2WORD)
3203 {
3204 if (prop & ENC_ENDIAN_L)
3205 return FIO_UTF16 | FIO_ENDIAN_L;
3206 return FIO_UTF16;
3207 }
3208 return FIO_UTF8;
3209 }
3210 if (prop & ENC_LATIN1)
3211 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003212 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 return 0;
3214}
3215
Bram Moolenaar473952e2019-09-28 16:30:04 +02003216#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217/*
3218 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3219 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3220 * Used for conversion between 'encoding' and 'fileencoding'.
3221 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003222 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003223get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 int cp;
3226
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003227 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (!enc_utf8 && enc_codepage <= 0)
3229 return 0;
3230
3231 cp = encname2codepage(ptr);
3232 if (cp == 0)
3233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003234# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (STRCMP(ptr, "utf-8") == 0)
3236 cp = CP_UTF8;
3237 else
3238# endif
3239 return 0;
3240 }
3241 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3242}
3243#endif
3244
Bram Moolenaar473952e2019-09-28 16:30:04 +02003245#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
3247 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3248 * needed for the internal conversion to/from utf-8 or latin1.
3249 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003250 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003251get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3254 && (enc_canon_props(ptr) & ENC_MACROMAN))
3255 return FIO_MACROMAN;
3256 return 0;
3257}
3258#endif
3259
3260/*
3261 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3262 * "size" must be at least 2.
3263 * Return the name of the encoding and set "*lenp" to the length.
3264 * Returns NULL when no BOM found.
3265 */
3266 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003267check_for_bom(
3268 char_u *p,
3269 long size,
3270 int *lenp,
3271 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 char *name = NULL;
3274 int len = 2;
3275
3276 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003277 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003279 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 len = 3;
3281 }
3282 else if (p[0] == 0xff && p[1] == 0xfe)
3283 {
3284 if (size >= 4 && p[2] == 0 && p[3] == 0
3285 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003287 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 len = 4;
3289 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003290 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003291 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003292 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003293 // utf-16le is preferred, it also works for ucs-2le text
3294 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else if (p[0] == 0xfe && p[1] == 0xff
3297 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003299 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003300 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003301 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003302 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003303 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3306 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003308 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 len = 4;
3310 }
3311
3312 *lenp = len;
3313 return (char_u *)name;
3314}
3315
3316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 * Try to find a shortname by comparing the fullname with the current
3318 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003319 * Returns "full_path" or pointer into "full_path" if shortened.
3320 */
3321 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003322shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003323{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003324 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003325 char_u *p = full_path;
3326
Bram Moolenaard9462e32011-04-11 21:35:11 +02003327 dirname = alloc(MAXPATHL);
3328 if (dirname == NULL)
3329 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003330 if (mch_dirname(dirname, MAXPATHL) == OK)
3331 {
3332 p = shorten_fname(full_path, dirname);
3333 if (p == NULL || *p == NUL)
3334 p = full_path;
3335 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003336 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003337 return p;
3338}
3339
3340/*
3341 * Try to find a shortname by comparing the fullname with the current
3342 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 * Returns NULL if not shorter name possible, pointer into "full_path"
3344 * otherwise.
3345 */
3346 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 int len;
3350 char_u *p;
3351
3352 if (full_path == NULL)
3353 return NULL;
3354 len = (int)STRLEN(dir_name);
3355 if (fnamencmp(dir_name, full_path, len) == 0)
3356 {
3357 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003358#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003360 * MS-Windows: when a file is in the root directory, dir_name will end
3361 * in a slash, since C: by itself does not define a specific dir. In
3362 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 */
3364 if (!((len > 2) && (*(p - 2) == ':')))
3365#endif
3366 {
3367 if (vim_ispathsep(*p))
3368 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003369#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
3371 p = NULL;
3372#endif
3373 }
3374 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003375#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 /*
3377 * When using a file in the current drive, remove the drive name:
3378 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3379 * a floppy from "A:\dir" to "B:\dir".
3380 */
3381 else if (len > 3
3382 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3383 && full_path[1] == ':'
3384 && vim_ispathsep(full_path[2]))
3385 p = full_path + 2;
3386#endif
3387 else
3388 p = NULL;
3389 return p;
3390}
3391
3392/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003393 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 * When "force" is TRUE: Use full path from now on for files currently being
3395 * edited, both for file name and swap file name. Try to shorten the file
3396 * names a bit, if safe to do so.
3397 * When "force" is FALSE: Only try to shorten absolute file names.
3398 * For buffers that have buftype "nofile" or "scratch": never change the file
3399 * name.
3400 */
3401 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003402shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3403{
3404 char_u *p;
3405
3406 if (buf->b_fname != NULL
3407#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003408 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003409#endif
3410 && !path_with_url(buf->b_fname)
3411 && (force
3412 || buf->b_sfname == NULL
3413 || mch_isFullName(buf->b_sfname)))
3414 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003415 if (buf->b_sfname != buf->b_ffname)
3416 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003417 p = shorten_fname(buf->b_ffname, dirname);
3418 if (p != NULL)
3419 {
3420 buf->b_sfname = vim_strsave(p);
3421 buf->b_fname = buf->b_sfname;
3422 }
3423 if (p == NULL || buf->b_fname == NULL)
3424 buf->b_fname = buf->b_ffname;
3425 }
3426}
3427
3428/*
3429 * Shorten filenames for all buffers.
3430 */
3431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003432shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u dirname[MAXPATHL];
3435 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003438 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003440 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003442 // Always make the swap file name a full path, a "nofile" buffer may
3443 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003444 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003448#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003449 popup_update_preview_title();
3450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451}
3452
3453#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3454 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003455 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 || defined(PROTO)
3457/*
3458 * Shorten all filenames in "fnames[count]" by current directory.
3459 */
3460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003461shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463 int i;
3464 char_u dirname[MAXPATHL];
3465 char_u *p;
3466
3467 if (fnames == NULL || count < 1)
3468 return;
3469 mch_dirname(dirname, sizeof(dirname));
3470 for (i = 0; i < count; ++i)
3471 {
3472 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3473 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003474 // shorten_fname() returns pointer in given "fnames[i]". If free
3475 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3476 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 p = vim_strsave(p);
3478 vim_free(fnames[i]);
3479 fnames[i] = p;
3480 }
3481 }
3482}
3483#endif
3484
3485/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003486 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 * fo_o_h.ext for MSDOS or when shortname option set.
3488 *
3489 * Assumed that fname is a valid name found in the filesystem we assure that
3490 * the return value is a different name and ends in 'ext'.
3491 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3492 * characters otherwise.
3493 * Space for the returned name is allocated, must be freed later.
3494 * Returns NULL when out of memory.
3495 */
3496 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003497modname(
3498 char_u *fname,
3499 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003500 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003502 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 fname, ext, prepend_dot);
3504}
3505
3506 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003508 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003509 char_u *fname,
3510 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003511 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
3513 char_u *retval;
3514 char_u *s;
3515 char_u *e;
3516 char_u *ptr;
3517 int fnamelen, extlen;
3518
3519 extlen = (int)STRLEN(ext);
3520
3521 /*
3522 * If there is no file name we must get the name of the current directory
3523 * (we need the full path in case :cd is used).
3524 */
3525 if (fname == NULL || *fname == NUL)
3526 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003527 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (retval == NULL)
3529 return NULL;
3530 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3531 (fnamelen = (int)STRLEN(retval)) == 0)
3532 {
3533 vim_free(retval);
3534 return NULL;
3535 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003536 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 retval[fnamelen++] = PATHSEP;
3539 retval[fnamelen] = NUL;
3540 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003541 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543 else
3544 {
3545 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003546 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (retval == NULL)
3548 return NULL;
3549 STRCPY(retval, fname);
3550#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003551 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#endif
3553 }
3554
3555 /*
3556 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3557 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3558 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3559 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3560 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003561 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003563 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003564 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003567 {
3568 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003573 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3575 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 s = ptr + STRLEN(ptr);
3578
3579 /*
3580 * For 8.3 file names we may have to reduce the length.
3581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 /*
3585 * If there is no file name, or the file name ends in '/', and the
3586 * extension starts with '.', put a '_' before the dot, because just
3587 * ".ext" is invalid.
3588 */
3589 if (fname == NULL || *fname == NUL
3590 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 *s++ = '_';
3594 }
3595 /*
3596 * If the extension starts with '.', truncate the base name at 8
3597 * characters
3598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003601 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
3603 s = ptr + 8;
3604 *s = '\0';
3605 }
3606 }
3607 /*
3608 * If the extension doesn't start with '.', and the file name
3609 * doesn't have an extension yet, append a '.'
3610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 else if ((e = vim_strchr(ptr, '.')) == NULL)
3612 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 /*
3614 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003615 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 */
3617 else if ((int)STRLEN(e) + extlen > 4)
3618 s = e + 4 - extlen;
3619 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003620#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 /*
3622 * If there is no file name, and the extension starts with '.', put a
3623 * '_' before the dot, because just ".ext" may be invalid if it's on a
3624 * FAT partition, and on HPFS it doesn't matter.
3625 */
3626 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3627 *s++ = '_';
3628#endif
3629
3630 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003631 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * ext can start with '.' and cannot exceed 3 more characters.
3633 */
3634 STRCPY(s, ext);
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 /*
3637 * Prepend the dot.
3638 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003639 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003641 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
3646 * Check that, after appending the extension, the file name is really
3647 * different.
3648 */
3649 if (fname != NULL && STRCMP(fname, retval) == 0)
3650 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 while (--s >= ptr)
3653 {
3654 if (*s != '_')
3655 {
3656 *s = '_';
3657 break;
3658 }
3659 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003660 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 *ptr = 'v';
3662 }
3663 return retval;
3664}
3665
3666/*
3667 * Like fgets(), but if the file line is too long, it is truncated and the
3668 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003669 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
3671 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003672vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 char *eof;
3675#define FGETS_SIZE 200
3676 char tbuf[FGETS_SIZE];
3677
3678 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003682 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003684 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 do
3686 {
3687 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003688 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3690 }
3691 return (eof == NULL);
3692}
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * rename() only works if both files are on the same file system, this
3696 * function will (attempts to?) copy the file across if rename fails -- webb
3697 * Return -1 for failure, 0 for success.
3698 */
3699 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003700vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 int fd_in;
3703 int fd_out;
3704 int n;
3705 char *errmsg = NULL;
3706 char *buffer;
3707#ifdef AMIGA
3708 BPTR flock;
3709#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003710 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003711 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003712#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003713 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003714#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003715 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003718 * When the names are identical, there is nothing to do. When they refer
3719 * to the same file (ignoring case and slash/backslash differences) but
3720 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
3722 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003723 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003724 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003725 use_tmp_file = TRUE;
3726 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003727 return 0;
3728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
3730 /*
3731 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3732 */
3733 if (mch_stat((char *)from, &st) < 0)
3734 return -1;
3735
Bram Moolenaar3576da72008-12-30 15:15:57 +00003736#ifdef UNIX
3737 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003738 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003740 // It's possible for the source and destination to be the same file.
3741 // This happens when "from" and "to" differ in case and are on a FAT32
3742 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003743 if (mch_stat((char *)to, &st_to) >= 0
3744 && st.st_dev == st_to.st_dev
3745 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003746 use_tmp_file = TRUE;
3747 }
3748#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003749#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003750 {
3751 BY_HANDLE_FILE_INFORMATION info1, info2;
3752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003753 // It's possible for the source and destination to be the same file.
3754 // In that case go through a temp file name. This makes rename("foo",
3755 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003756 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3757 && win32_fileinfo(to, &info2) == FILEINFO_OK
3758 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3759 && info1.nFileIndexHigh == info2.nFileIndexHigh
3760 && info1.nFileIndexLow == info2.nFileIndexLow)
3761 use_tmp_file = TRUE;
3762 }
3763#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003764
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003765 if (use_tmp_file)
3766 {
3767 char tempname[MAXPATHL + 1];
3768
3769 /*
3770 * Find a name that doesn't exist and is in the same directory.
3771 * Rename "from" to "tempname" and then rename "tempname" to "to".
3772 */
3773 if (STRLEN(from) >= MAXPATHL - 5)
3774 return -1;
3775 STRCPY(tempname, from);
3776 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003777 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003778 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3779 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003780 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003781 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003782 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003783 if (mch_rename(tempname, (char *)to) == 0)
3784 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003785 // Strange, the second step failed. Try moving the
3786 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003787 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003788 return -1;
3789 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003790 // If it fails for one temp name it will most likely fail
3791 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003792 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003793 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003794 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003795 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003796 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 /*
3799 * Delete the "to" file, this is required on some systems to make the
3800 * mch_rename() work, on other systems it makes sure that we don't have
3801 * two files when the mch_rename() fails.
3802 */
3803
3804#ifdef AMIGA
3805 /*
3806 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3807 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003808 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 * deleting the "from" file (horror!) we lock it during the remove.
3810 *
3811 * When used for making a backup before writing the file: This should not
3812 * happen with ":w", because startscript() should detect this problem and
3813 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3814 * name. This problem does exist with ":w filename", but then the
3815 * original file will be somewhere else so the backup isn't really
3816 * important. If autoscripting is off the rename may fail.
3817 */
3818 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3819#endif
3820 mch_remove(to);
3821#ifdef AMIGA
3822 if (flock)
3823 UnLock(flock);
3824#endif
3825
3826 /*
3827 * First try a normal rename, return if it works.
3828 */
3829 if (mch_rename((char *)from, (char *)to) == 0)
3830 return 0;
3831
3832 /*
3833 * Rename() failed, try copying the file.
3834 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003835 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003836#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003837 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003838 acl = mch_get_acl(from);
3839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3841 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003842 {
3843#ifdef HAVE_ACL
3844 mch_free_acl(acl);
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003847 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003848
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003849 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003850 fd_out = mch_open((char *)to,
3851 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if (fd_out == -1)
3853 {
3854 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003855#ifdef HAVE_ACL
3856 mch_free_acl(acl);
3857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return -1;
3859 }
3860
Bram Moolenaar473952e2019-09-28 16:30:04 +02003861 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (buffer == NULL)
3863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003865 close(fd_in);
3866#ifdef HAVE_ACL
3867 mch_free_acl(acl);
3868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return -1;
3870 }
3871
Bram Moolenaar473952e2019-09-28 16:30:04 +02003872 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003873 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003875 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 break;
3877 }
3878
3879 vim_free(buffer);
3880 close(fd_in);
3881 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003882 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (n < 0)
3884 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003885 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 to = from;
3887 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003889 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003890#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003891#ifdef HAVE_ACL
3892 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003893 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003894#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003895#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003896 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (errmsg != NULL)
3899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003900 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 return -1;
3902 }
3903 mch_remove(from);
3904 return 0;
3905}
3906
3907static int already_warned = FALSE;
3908
3909/*
3910 * Check if any not hidden buffer has been changed.
3911 * Postpone the check if there are characters in the stuff buffer, a global
3912 * command is being executed, a mapping is being executed or an autocommand is
3913 * busy.
3914 * Returns TRUE if some message was written (screen should be redrawn and
3915 * cursor positioned).
3916 */
3917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003918check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003919 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921 buf_T *buf;
3922 int didit = 0;
3923 int n;
3924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003925 // Don't check timestamps while system() or another low-level function may
3926 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (no_check_timestamps > 0)
3928 return FALSE;
3929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003930 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3931 // event and we would keep on checking if the file is steadily growing.
3932 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 if (focus && did_check_timestamps)
3934 {
3935 need_check_timestamps = TRUE;
3936 return FALSE;
3937 }
3938
3939 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003940 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003941 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 else
3943 {
3944 ++no_wait_return;
3945 did_check_timestamps = TRUE;
3946 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003947 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003949 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 if (buf->b_nwindows > 0)
3951 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003952 bufref_T bufref;
3953
3954 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 n = buf_check_timestamp(buf, focus);
3956 if (didit < n)
3957 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003958 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003960 // Autocommands have removed the buffer, start at the
3961 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 buf = firstbuf;
3963 continue;
3964 }
3965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 --no_wait_return;
3968 need_check_timestamps = FALSE;
3969 if (need_wait_return && didit == 2)
3970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003971 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003972 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 out_flush();
3974 }
3975 }
3976 return didit;
3977}
3978
3979/*
3980 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3981 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3982 * empty.
3983 */
3984 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003985move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 buf_T *tbuf = curbuf;
3988 int retval = OK;
3989 linenr_T lnum;
3990 char_u *p;
3991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003992 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 curbuf = tobuf;
3994 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3995 {
3996 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
3997 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
3998 {
3999 vim_free(p);
4000 retval = FAIL;
4001 break;
4002 }
4003 vim_free(p);
4004 }
4005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004006 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (retval != FAIL)
4008 {
4009 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004010 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004011 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004013 // Oops! We could try putting back the saved lines, but that
4014 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 retval = FAIL;
4016 break;
4017 }
4018 }
4019
4020 curbuf = tbuf;
4021 return retval;
4022}
4023
4024/*
4025 * Check if buffer "buf" has been changed.
4026 * Also check if the file for a new buffer unexpectedly appeared.
4027 * return 1 if a changed buffer was found.
4028 * return 2 if a message has been displayed.
4029 * return 0 otherwise.
4030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004032buf_check_timestamp(
4033 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004034 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004036 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 int stat_res;
4038 int retval = 0;
4039 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004040 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004042 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 int helpmesg = FALSE;
4044 int reload = FALSE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004045 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4047 int can_reload = FALSE;
4048#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004049 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int orig_mode = buf->b_orig_mode;
4051#ifdef FEAT_GUI
4052 int save_mouse_correct = need_mouse_correct;
4053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004055 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004056#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004057 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004058#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004059 bufref_T bufref;
4060
4061 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004063 // If there is no file name, the buffer is not loaded, 'buftype' is
4064 // set, we are in the middle of a save or being called recursively: ignore
4065 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (buf->b_ffname == NULL
4067 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004068 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004071#ifdef FEAT_NETBEANS_INTG
4072 || isNetbeansBuffer(buf)
4073#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004074#ifdef FEAT_TERMINAL
4075 || buf->b_term != NULL
4076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 )
4078 return 0;
4079
4080 if ( !(buf->b_flags & BF_NOTEDITED)
4081 && buf->b_mtime != 0
4082 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004083 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004084 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#ifdef HAVE_ST_MODE
4086 || (int)st.st_mode != buf->b_orig_mode
4087#else
4088 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4089#endif
4090 ))
4091 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004092 long prev_b_mtime = buf->b_mtime;
4093
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 retval = 1;
4095
Bram Moolenaar386bc822018-07-07 18:34:12 +02004096 // set b_mtime to stop further warnings (e.g., when executing
4097 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (stat_res < 0)
4099 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004100 // Check the file again later to see if it re-appears.
4101 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 buf->b_orig_size = 0;
4103 buf->b_orig_mode = 0;
4104 }
4105 else
4106 buf_store_time(buf, &st, buf->b_ffname);
4107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 // Don't do anything for a directory. Might contain the file
4109 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (mch_isdir(buf->b_fname))
4111 ;
4112
4113 /*
4114 * If 'autoread' is set, the buffer has no changes and the file still
4115 * exists, reload the buffer. Use the buffer-local option value if it
4116 * was set, the global option value otherwise.
4117 */
4118 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4119 && !bufIsChanged(buf) && stat_res >= 0)
4120 reload = TRUE;
4121 else
4122 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004123 if (stat_res < 0)
4124 reason = "deleted";
4125 else if (bufIsChanged(buf))
4126 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004127 /*
4128 * Check if the file contents really changed to avoid giving a
4129 * warning when only the timestamp was set (e.g., checked out of
4130 * CVS). Always warn when the buffer was changed.
4131 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004132 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4133 reason = "changed";
4134 else if (orig_mode != buf->b_orig_mode)
4135 reason = "mode";
4136 else
4137 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 /*
4140 * Only give the warning if there are no FileChangedShell
4141 * autocommands.
4142 * Avoid being called recursively by setting "busy".
4143 */
4144 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004145#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004146 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4147 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004148#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004149 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4151 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004152 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 busy = FALSE;
4154 if (n)
4155 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004156 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004157 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004158#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004159 s = get_vim_var_str(VV_FCS_CHOICE);
4160 if (STRCMP(s, "reload") == 0 && *reason != 'd')
4161 reload = TRUE;
4162 else if (STRCMP(s, "ask") == 0)
4163 n = FALSE;
4164 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004165#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004166 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004168 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004170 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004171 {
4172 // Only give the message once.
4173 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004174 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 else
4177 {
4178 helpmesg = TRUE;
4179#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4180 can_reload = TRUE;
4181#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 if (reason[2] == 'n')
4183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004185 mesg2 = _("See \":help W12\" for more info.");
4186 }
4187 else if (reason[1] == 'h')
4188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004190 mesg2 = _("See \":help W11\" for more info.");
4191 }
4192 else if (*reason == 'm')
4193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004195 mesg2 = _("See \":help W16\" for more info.");
4196 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004197 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004199 // Only timestamp changed, store it to avoid a warning
4200 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004201 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004202 buf->b_mtime_read_ns = buf->b_mtime_ns;
4203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 }
4206 }
4207
4208 }
4209 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4210 && vim_fexists(buf->b_ffname))
4211 {
4212 retval = 1;
4213 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4214 buf->b_flags |= BF_NEW_W;
4215#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4216 can_reload = TRUE;
4217#endif
4218 }
4219
4220 if (mesg != NULL)
4221 {
4222 path = home_replace_save(buf, buf->b_fname);
4223 if (path != NULL)
4224 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004225 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004227 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004228 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004229#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004230 // Set warningmsg here, before the unimportant and output-specific
4231 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004232 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4235 if (can_reload)
4236 {
4237 if (*mesg2 != NUL)
4238 {
4239 STRCAT(tbuf, "\n");
4240 STRCAT(tbuf, mesg2);
4241 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004242 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4243 (char_u *)tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004244 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 reload = TRUE;
4246 }
4247 else
4248#endif
4249 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4250 {
4251 if (*mesg2 != NUL)
4252 {
4253 STRCAT(tbuf, "; ");
4254 STRCAT(tbuf, mesg2);
4255 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 retval = 2;
4258 }
4259 else
4260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004264 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004266 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 msg_clr_eos();
4268 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004269 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004272#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004274#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004275 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004276 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004278 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 redraw_cmdline = FALSE;
4280 }
4281 }
4282 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284
4285 vim_free(path);
4286 vim_free(tbuf);
4287 }
4288 }
4289
4290 if (reload)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004292 // Reload the buffer.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004293 buf_reload(buf, orig_mode);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004294#ifdef FEAT_PERSISTENT_UNDO
4295 if (buf->b_p_udf && buf->b_ffname != NULL)
4296 {
4297 char_u hash[UNDO_HASH_SIZE];
4298 buf_T *save_curbuf = curbuf;
4299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004300 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004301 curbuf = buf;
4302 u_compute_hash(hash);
4303 u_write_undo(NULL, FALSE, buf, hash);
4304 curbuf = save_curbuf;
4305 }
4306#endif
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004310 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004311 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4312 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004314 // restore this in case an autocommand has set it; it would break
4315 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 need_mouse_correct = save_mouse_correct;
4317#endif
4318
4319 return retval;
4320}
4321
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004322/*
4323 * Reload a buffer that is already loaded.
4324 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004325 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4326 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004327 */
4328 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004329buf_reload(buf_T *buf, int orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004330{
4331 exarg_T ea;
4332 pos_T old_cursor;
4333 linenr_T old_topline;
4334 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004335 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004336 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004337 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004338 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004339 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004340
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004341 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004342 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 // We only want to read the text from the file, not reset the syntax
4345 // highlighting, clear marks, diff status, etc. Force the fileformat
4346 // and encoding to be the same.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004347 if (prep_exarg(&ea, buf) == OK)
4348 {
4349 old_cursor = curwin->w_cursor;
4350 old_topline = curwin->w_topline;
4351
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004352 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004353 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004354 // Save all the text, so that the reload can be undone.
4355 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004356 u_sync(FALSE);
4357 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4358 flags |= READ_KEEP_UNDO;
4359 }
4360
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 /*
4362 * To behave like when a new file is edited (matters for
4363 * BufReadPost autocommands) we first need to delete the current
4364 * buffer contents. But if reading the file fails we should keep
4365 * the old contents. Can't use memory only, the file might be
4366 * too big. Use a hidden buffer to move the buffer contents to.
4367 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004368 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004369 savebuf = NULL;
4370 else
4371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004373 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004374 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004375 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004376 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004377 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004378 curbuf = savebuf;
4379 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004380 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004381 curbuf = buf;
4382 curwin->w_buffer = buf;
4383 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004384 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 || move_lines(buf, savebuf) == FAIL)
4386 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004387 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004388 saved = FAIL;
4389 }
4390 }
4391
4392 if (saved == OK)
4393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004394 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4395 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004396 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4397 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004398 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004399 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004400#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004401 if (!aborting())
4402#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004403 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004404 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 // Put the text back from the save buffer. First
4407 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004408 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004409 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004410 break;
4411 (void)move_lines(savebuf, buf);
4412 }
4413 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004415 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004416 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004417 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004418 if ((flags & READ_KEEP_UNDO) == 0)
4419 {
4420 u_blockfree(buf);
4421 u_clearall(buf);
4422 }
4423 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004424 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004426 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004427 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004428 }
4429 }
4430 vim_free(ea.cmd);
4431
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004432 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004433 wipe_buffer(savebuf, FALSE);
4434
4435#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004437 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004438#endif
4439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 // Restore the topline and cursor position and check it (lines may
4441 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004442 if (old_topline > curbuf->b_ml.ml_line_count)
4443 curwin->w_topline = curbuf->b_ml.ml_line_count;
4444 else
4445 curwin->w_topline = old_topline;
4446 curwin->w_cursor = old_cursor;
4447 check_cursor();
4448 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004449 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004450#ifdef FEAT_FOLDING
4451 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004452 win_T *wp;
4453 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004456 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004457 if (wp->w_buffer == curwin->w_buffer
4458 && !foldmethodIsManual(wp))
4459 foldUpdateAll(wp);
4460 }
4461#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004462 // If the mode didn't change and 'readonly' was set, keep the old
4463 // value; the user probably used the ":view" command. But don't
4464 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004465 if (orig_mode == curbuf->b_orig_mode)
4466 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004468 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004469 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004470 }
4471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004472 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004473 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004474 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004475}
4476
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004478buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004481#ifdef ST_MTIM_NSEC
4482 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4483#else
4484 buf->b_mtime_ns = 0;
4485#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004486 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#ifdef HAVE_ST_MODE
4488 buf->b_orig_mode = (int)st->st_mode;
4489#else
4490 buf->b_orig_mode = mch_getperm(fname);
4491#endif
4492}
4493
4494/*
4495 * Adjust the line with missing eol, used for the next write.
4496 * Used for do_filter(), when the input lines for the filter are deleted.
4497 */
4498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004499write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004501 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004502 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503}
4504
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004505// Subfuncions for readdirex()
4506#ifdef FEAT_EVAL
4507# ifdef MSWIN
4508 static char_u *
4509getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4510{
4511 stat_T st;
4512 unsigned short st_mode;
4513 DWORD flag = wfd->dwFileAttributes;
4514 WCHAR *wp;
4515
4516 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4517 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4518 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4519 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4520
4521 wp = wcsrchr(wfd->cFileName, L'.');
4522 if (wp != NULL)
4523 {
4524 if (_wcsicmp(wp, L".exe") == 0 ||
4525 _wcsicmp(wp, L".com") == 0 ||
4526 _wcsicmp(wp, L".cmd") == 0 ||
4527 _wcsicmp(wp, L".bat") == 0)
4528 st_mode |= _S_IEXEC;
4529 }
4530
4531 // Copy user bits to group/other.
4532 st_mode |= (st_mode & 0700) >> 3;
4533 st_mode |= (st_mode & 0700) >> 6;
4534
4535 st.st_mode = st_mode;
4536 return getfpermst(&st, perm);
4537}
4538
4539 static char_u *
4540getftypewfd(WIN32_FIND_DATAW *wfd)
4541{
4542 DWORD flag = wfd->dwFileAttributes;
4543 DWORD tag = wfd->dwReserved0;
4544
4545 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4546 {
4547 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4548 return (char_u*)"junction";
4549 else if (tag == IO_REPARSE_TAG_SYMLINK)
4550 {
4551 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4552 return (char_u*)"linkd";
4553 else
4554 return (char_u*)"link";
4555 }
4556 return (char_u*)"reparse"; // unknown reparse point type
4557 }
4558 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4559 return (char_u*)"dir";
4560 else
4561 return (char_u*)"file";
4562}
4563
4564 static dict_T *
4565create_readdirex_item(WIN32_FIND_DATAW *wfd)
4566{
4567 dict_T *item;
4568 char_u *p;
4569 varnumber_T size, time;
4570 char_u permbuf[] = "---------";
4571
4572 item = dict_alloc();
4573 if (item == NULL)
4574 return NULL;
4575 item->dv_refcount++;
4576
4577 p = utf16_to_enc(wfd->cFileName, NULL);
4578 if (p == NULL)
4579 goto theend;
4580 if (dict_add_string(item, "name", p) == FAIL)
4581 {
4582 vim_free(p);
4583 goto theend;
4584 }
4585 vim_free(p);
4586
4587 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4588 if (dict_add_number(item, "size", size) == FAIL)
4589 goto theend;
4590
4591 // Convert FILETIME to unix time.
4592 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4593 wfd->ftLastWriteTime.dwLowDateTime)
4594 - 116444736000000000) / 10000000;
4595 if (dict_add_number(item, "time", time) == FAIL)
4596 goto theend;
4597
4598 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4599 goto theend;
4600 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4601 goto theend;
4602
4603 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4604 goto theend;
4605 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4606 goto theend;
4607
4608 return item;
4609
4610theend:
4611 dict_unref(item);
4612 return NULL;
4613}
4614# else
4615 static dict_T *
4616create_readdirex_item(char_u *path, char_u *name)
4617{
4618 dict_T *item;
4619 char *p;
4620 size_t len;
4621 stat_T st;
4622 int ret, link = FALSE;
4623 varnumber_T size;
4624 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004625 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004626 struct passwd *pw;
4627 struct group *gr;
4628
4629 item = dict_alloc();
4630 if (item == NULL)
4631 return NULL;
4632 item->dv_refcount++;
4633
4634 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4635 p = alloc(len);
4636 if (p == NULL)
4637 goto theend;
4638 vim_snprintf(p, len, "%s/%s", path, name);
4639 ret = mch_lstat(p, &st);
4640 if (ret >= 0 && S_ISLNK(st.st_mode))
4641 {
4642 link = TRUE;
4643 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004644 if (ret < 0)
4645 q = (char_u*)"link";
4646
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004647 }
4648 vim_free(p);
4649
4650 if (dict_add_string(item, "name", name) == FAIL)
4651 goto theend;
4652
4653 if (ret >= 0)
4654 {
4655 size = (varnumber_T)st.st_size;
4656 if (S_ISDIR(st.st_mode))
4657 size = 0;
4658 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004659 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004660 size = -2;
4661 if (dict_add_number(item, "size", size) == FAIL)
4662 goto theend;
4663 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4664 goto theend;
4665
4666 if (link)
4667 {
4668 if (S_ISDIR(st.st_mode))
4669 q = (char_u*)"linkd";
4670 else
4671 q = (char_u*)"link";
4672 }
4673 else
4674 q = getftypest(&st);
4675 if (dict_add_string(item, "type", q) == FAIL)
4676 goto theend;
4677 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4678 goto theend;
4679
4680 pw = getpwuid(st.st_uid);
4681 if (pw == NULL)
4682 q = (char_u*)"";
4683 else
4684 q = (char_u*)pw->pw_name;
4685 if (dict_add_string(item, "user", q) == FAIL)
4686 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004687# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004688 gr = getgrgid(st.st_gid);
4689 if (gr == NULL)
4690 q = (char_u*)"";
4691 else
4692 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004693# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004694 if (dict_add_string(item, "group", q) == FAIL)
4695 goto theend;
4696 }
4697 else
4698 {
4699 if (dict_add_number(item, "size", -1) == FAIL)
4700 goto theend;
4701 if (dict_add_number(item, "time", -1) == FAIL)
4702 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004703 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004704 goto theend;
4705 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4706 goto theend;
4707 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4708 goto theend;
4709 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4710 goto theend;
4711 }
4712 return item;
4713
4714theend:
4715 dict_unref(item);
4716 return NULL;
4717}
4718# endif
4719
4720 static int
4721compare_readdirex_item(const void *p1, const void *p2)
4722{
4723 char_u *name1, *name2;
4724
4725 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4726 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004727 if (readdirex_sort == READDIR_SORT_BYTE)
4728 return STRCMP(name1, name2);
4729 else if (readdirex_sort == READDIR_SORT_IC)
4730 return STRICMP(name1, name2);
4731 else
4732 return STRCOLL(name1, name2);
4733}
4734
4735 static int
4736compare_readdir_item(const void *s1, const void *s2)
4737{
4738 if (readdirex_sort == READDIR_SORT_BYTE)
4739 return STRCMP(*(char **)s1, *(char **)s2);
4740 else if (readdirex_sort == READDIR_SORT_IC)
4741 return STRICMP(*(char **)s1, *(char **)s2);
4742 else
4743 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004744}
4745#endif
4746
Bram Moolenaarda440d22016-01-16 21:27:23 +01004747#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4748/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004749 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004750 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004751 * If "withattr" is TRUE, retrieve the names and their attributes.
4752 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004753 * Return OK for success, FAIL for failure.
4754 */
4755 int
4756readdir_core(
4757 garray_T *gap,
4758 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004759 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004760 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004761 int (*checkitem)(void *context, void *item),
4762 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004763{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004764 int failed = FALSE;
4765 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004766# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004767 char_u *buf;
4768 int ok;
4769 HANDLE hFind = INVALID_HANDLE_VALUE;
4770 WIN32_FIND_DATAW wfd;
4771 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004772# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004773 DIR *dirp;
4774 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004775# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004776
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004777 ga_init2(gap, (int)sizeof(void *), 20);
4778
4779# ifdef FEAT_EVAL
4780# define FREE_ITEM(item) do { \
4781 if (withattr) \
4782 dict_unref((dict_T*)item); \
4783 else \
4784 vim_free(item); \
4785 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004786
4787 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004788# else
4789# define FREE_ITEM(item) vim_free(item)
4790# endif
4791
4792# ifdef MSWIN
4793 buf = alloc(MAXPATHL);
4794 if (buf == NULL)
4795 return FAIL;
4796 STRNCPY(buf, path, MAXPATHL-5);
4797 p = buf + STRLEN(buf);
4798 MB_PTR_BACK(buf, p);
4799 if (*p == '\\' || *p == '/')
4800 *p = NUL;
4801 STRCAT(p, "\\*");
4802
4803 wn = enc_to_utf16(buf, NULL);
4804 if (wn != NULL)
4805 hFind = FindFirstFileW(wn, &wfd);
4806 ok = (hFind != INVALID_HANDLE_VALUE);
4807 if (!ok)
4808 {
4809 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004810 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004811 }
4812 else
4813 {
4814 while (ok)
4815 {
4816 int ignore;
4817 void *item;
4818 WCHAR *wp;
4819
4820 wp = wfd.cFileName;
4821 ignore = wp[0] == L'.' &&
4822 (wp[1] == NUL ||
4823 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004824 if (ignore)
4825 {
4826 ok = FindNextFileW(hFind, &wfd);
4827 continue;
4828 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004829# ifdef FEAT_EVAL
4830 if (withattr)
4831 item = (void*)create_readdirex_item(&wfd);
4832 else
4833# endif
4834 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4835 if (item == NULL)
4836 {
4837 failed = TRUE;
4838 break;
4839 }
4840
4841 if (!ignore && checkitem != NULL)
4842 {
4843 int r = checkitem(context, item);
4844
4845 if (r < 0)
4846 {
4847 FREE_ITEM(item);
4848 break;
4849 }
4850 if (r == 0)
4851 ignore = TRUE;
4852 }
4853
4854 if (!ignore)
4855 {
4856 if (ga_grow(gap, 1) == OK)
4857 ((void**)gap->ga_data)[gap->ga_len++] = item;
4858 else
4859 {
4860 failed = TRUE;
4861 FREE_ITEM(item);
4862 break;
4863 }
4864 }
4865 else
4866 FREE_ITEM(item);
4867
4868 ok = FindNextFileW(hFind, &wfd);
4869 }
4870 FindClose(hFind);
4871 }
4872
4873 vim_free(buf);
4874 vim_free(wn);
4875# else // MSWIN
4876 dirp = opendir((char *)path);
4877 if (dirp == NULL)
4878 {
4879 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004880 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004881 }
4882 else
4883 {
4884 for (;;)
4885 {
4886 int ignore;
4887 void *item;
4888
4889 dp = readdir(dirp);
4890 if (dp == NULL)
4891 break;
4892 p = (char_u *)dp->d_name;
4893
4894 ignore = p[0] == '.' &&
4895 (p[1] == NUL ||
4896 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004897 if (ignore)
4898 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004899# ifdef FEAT_EVAL
4900 if (withattr)
4901 item = (void*)create_readdirex_item(path, p);
4902 else
4903# endif
4904 item = (void*)vim_strsave(p);
4905 if (item == NULL)
4906 {
4907 failed = TRUE;
4908 break;
4909 }
4910
4911 if (!ignore && checkitem != NULL)
4912 {
4913 int r = checkitem(context, item);
4914
4915 if (r < 0)
4916 {
4917 FREE_ITEM(item);
4918 break;
4919 }
4920 if (r == 0)
4921 ignore = TRUE;
4922 }
4923
4924 if (!ignore)
4925 {
4926 if (ga_grow(gap, 1) == OK)
4927 ((void**)gap->ga_data)[gap->ga_len++] = item;
4928 else
4929 {
4930 failed = TRUE;
4931 FREE_ITEM(item);
4932 break;
4933 }
4934 }
4935 else
4936 FREE_ITEM(item);
4937 }
4938
4939 closedir(dirp);
4940 }
4941# endif // MSWIN
4942
4943# undef FREE_ITEM
4944
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004945 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004946 {
4947# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004948 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004949 if (withattr)
4950 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4951 compare_readdirex_item);
4952 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004953 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4954 compare_readdir_item);
4955# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004956 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004957# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004958 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004959
4960 return failed ? FAIL : OK;
4961}
4962
4963/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004964 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004965 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004966 */
4967 int
4968delete_recursive(char_u *name)
4969{
4970 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004971 int i;
4972 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004973 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004974
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004975 // A symbolic link to a directory itself is deleted, not the directory it
4976 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004977 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01004978# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004979 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01004980# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004981 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01004982# endif
4983 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01004984 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004985 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004986 if (exp == NULL)
4987 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004988 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004989 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004990 for (i = 0; i < ga.ga_len; ++i)
4991 {
4992 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
4993 ((char_u **)ga.ga_data)[i]);
4994 if (delete_recursive(NameBuff) != 0)
Bram Moolenaarda440d22016-01-16 21:27:23 +01004995 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004996 }
4997 ga_clear_strings(&ga);
Bram Moolenaarda440d22016-01-16 21:27:23 +01004998 }
4999 else
5000 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005001 (void)mch_rmdir(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005002 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005003 }
5004 else
5005 result = mch_remove(name) == 0 ? 0 : -1;
5006
5007 return result;
5008}
5009#endif
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005012static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005014# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5015/*
5016 * Open temporary directory and take file lock to prevent
5017 * to be auto-cleaned.
5018 */
5019 static void
5020vim_opentempdir(void)
5021{
5022 DIR *dp = NULL;
5023
5024 if (vim_tempdir_dp != NULL)
5025 return;
5026
5027 dp = opendir((const char*)vim_tempdir);
5028
5029 if (dp != NULL)
5030 {
5031 vim_tempdir_dp = dp;
5032 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5033 }
5034}
5035
5036/*
5037 * Close temporary directory - it automatically release file lock.
5038 */
5039 static void
5040vim_closetempdir(void)
5041{
5042 if (vim_tempdir_dp != NULL)
5043 {
5044 closedir(vim_tempdir_dp);
5045 vim_tempdir_dp = NULL;
5046 }
5047}
5048# endif
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050/*
5051 * Delete the temp directory and all files it contains.
5052 */
5053 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005054vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (vim_tempdir != NULL)
5057 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005058# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5059 vim_closetempdir();
5060# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005061 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005062 gettail(vim_tempdir)[-1] = NUL;
5063 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005064 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005069 * Directory "tempdir" was created. Expand this name to a full path and put
5070 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5071 * "tempdir" must be no longer than MAXPATHL.
5072 */
5073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005074vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005075{
5076 char_u *buf;
5077
Bram Moolenaar964b3742019-05-24 18:54:09 +02005078 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005079 if (buf != NULL)
5080 {
5081 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5082 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005083 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005084 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005085# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5086 vim_opentempdir();
5087# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005088 vim_free(buf);
5089 }
5090}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005091#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005092
5093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 * vim_tempname(): Return a unique name that can be used for a temp file.
5095 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005096 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5097 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *
5099 * The returned pointer is to allocated memory.
5100 * The returned pointer is NULL if no valid name was found.
5101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005103vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005104 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005105 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106{
5107#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005108 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005109#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005110 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111#else
5112 char_u itmp[TEMPNAMELEN];
5113#endif
5114
5115#ifdef TEMPDIRNAMES
5116 static char *(tempdirs[]) = {TEMPDIRNAMES};
5117 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005119 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120# endif
5121
5122 /*
5123 * This will create a directory for private use by this instance of Vim.
5124 * This is done once, and the same directory is used for all temp files.
5125 * This method avoids security problems because of symlink attacks et al.
5126 * It's also a bit faster, because we only need to check for an existing
5127 * file when creating the directory and not for each temp file.
5128 */
5129 if (vim_tempdir == NULL)
5130 {
5131 /*
5132 * Try the entries in TEMPDIRNAMES to create the temp directory.
5133 */
K.Takataeeec2542021-06-02 13:28:16 +02005134 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005136# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005137 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005138 long nr;
5139 long off;
5140# endif
5141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005142 // Expand $TMP, leave room for "/v1100000/999999999".
5143 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005145 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005148 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
Bram Moolenaareaf03392009-11-17 11:08:52 +00005150# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005151 {
5152# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005153 // Make sure the umask doesn't remove the executable bit.
5154 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005155 mode_t umask_save = umask(077);
5156# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005158 STRCAT(itmp, "vXXXXXX");
5159 if (mkdtemp((char *)itmp) != NULL)
5160 vim_settempdir(itmp);
5161# if defined(UNIX) || defined(VMS)
5162 (void)umask(umask_save);
5163# endif
5164 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005165# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005166 // Get an arbitrary number of up to 6 digits. When it's
5167 // unlikely that it already exists it will be faster,
5168 // otherwise it doesn't matter. The use of mkdir() avoids any
5169 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005171 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005173 // Try up to 10000 different values until we find a name that
5174 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 for (off = 0; off < 10000L; ++off)
5176 {
5177 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005178# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
Bram Moolenaareaf03392009-11-17 11:08:52 +00005182 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5183# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 // If mkdir() does not set errno to EEXIST, check for
5185 // existing file here. There is a race condition then,
5186 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (mch_stat((char *)itmp, &st) >= 0)
5188 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005189# endif
5190# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005191 // Make sure the umask doesn't remove the executable bit.
5192 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005196# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (r == 0)
5200 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005201 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005204# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 // If the mkdir() didn't fail because the file/dir exists,
5206 // we probably can't create any dir here, try another
5207 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 break;
5211 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005212# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (vim_tempdir != NULL)
5214 break;
5215 }
5216 }
5217 }
5218
5219 if (vim_tempdir != NULL)
5220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 // There is no need to check if the file exists, because we own the
5222 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5224 return vim_strsave(itmp);
5225 }
5226
5227 return NULL;
5228
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
Bram Moolenaar4f974752019-02-17 17:44:42 +01005231# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005232 WCHAR wszTempFile[_MAX_PATH + 1];
5233 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005234 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 char_u *retval;
5236 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005237 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005238 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005240 wcscpy(itmp, L"");
5241 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005242 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005243 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005244 wszTempFile[1] = L'\\';
5245 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005246 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005247 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005248
5249 // randomize the name to avoid collisions
5250 i = mch_get_pid() + extra_char;
5251 buf4[1] = chartab[i % 36];
5252 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005253 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005255 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005256 // GetTempFileName() will create the file, we don't want that
5257 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005259 // Backslashes in a temp file name cause problems when filtering with
5260 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005261 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005262 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005263 shname = gettail(p_sh);
5264 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5265 || strstr((char *)shname, "pwsh") != NULL ))
5266 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 for (p = retval; *p; ++p)
5268 if (*p == '\\')
5269 *p = '/';
5270 return retval;
5271
Bram Moolenaar4f974752019-02-17 17:44:42 +01005272# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005275 char_u *p;
5276
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005277 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005278 p = tmpnam((char *)itmp);
5279 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 return NULL;
5281# else
5282 char_u *p;
5283
5284# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 // mktemp() is not working on VMS. It seems to be
5286 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 sprintf((char *)itmp, "VIM%c", extra_char);
5288 p = (char_u *)tempnam("tmp:", (char *)itmp);
5289 if (p != NULL)
5290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005291 // VMS will use '.LIS' if we don't explicitly specify an extension,
5292 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 STRCPY(itmp, p);
5294 STRCAT(itmp, ".txt");
5295 free(p);
5296 }
5297 else
5298 return NULL;
5299# else
5300 STRCPY(itmp, TEMPNAME);
5301 if ((p = vim_strchr(itmp, '?')) != NULL)
5302 *p = extra_char;
5303 if (mktemp((char *)itmp) == NULL)
5304 return NULL;
5305# endif
5306# endif
5307
5308 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005309# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005310#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311}
5312
5313#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5314/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005315 * Convert all backslashes in fname to forward slashes in-place, unless when
5316 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 */
5318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005319forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
5321 char_u *p;
5322
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005323 if (path_with_url(fname))
5324 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005326 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005327 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005329 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 *p = '/';
5331}
5332#endif
5333
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005334/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005335 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5336 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5337 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 * Used for autocommands and 'wildignore'.
5339 * Returns TRUE if there is a match, FALSE otherwise.
5340 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005342match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005343 char_u *pattern, // pattern to match with
5344 regprog_T **prog, // pre-compiled regprog or NULL
5345 char_u *fname, // full path of file name
5346 char_u *sfname, // short file name or NULL
5347 char_u *tail, // tail of path
5348 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 regmatch_T regmatch;
5351 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005354 if (prog != NULL)
5355 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005357 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
5359 /*
5360 * Try for a match with the pattern with:
5361 * 1. the full file name, when the pattern has a '/'.
5362 * 2. the short file name, when the pattern has a '/'.
5363 * 3. the tail of the file name, when the pattern has no '/'.
5364 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005365 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 && ((allow_dirs
5367 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5368 || (sfname != NULL
5369 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005370 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 result = TRUE;
5372
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005373 if (prog != NULL)
5374 *prog = regmatch.regprog;
5375 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005376 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 return result;
5378}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380#if defined(FEAT_WILDIGN) || defined(PROTO)
5381/*
5382 * Return TRUE if a file matches with a pattern in "list".
5383 * "list" is a comma-separated list of patterns, like 'wildignore'.
5384 * "sfname" is the short file name or NULL, "ffname" the long file name.
5385 */
5386 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 char_u buf[100];
5390 char_u *tail;
5391 char_u *regpat;
5392 char allow_dirs;
5393 int match;
5394 char_u *p;
5395
5396 tail = gettail(sfname);
5397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005398 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 p = list;
5400 while (*p)
5401 {
5402 copy_option_part(&p, buf, 100, ",");
5403 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5404 if (regpat == NULL)
5405 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005406 match = match_file_pat(regpat, NULL, ffname, sfname,
5407 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 vim_free(regpat);
5409 if (match)
5410 return TRUE;
5411 }
5412 return FALSE;
5413}
5414#endif
5415
5416/*
5417 * Convert the given pattern "pat" which has shell style wildcards in it, into
5418 * a regular expression, and return the result in allocated memory. If there
5419 * is a directory path separator to be matched, then TRUE is put in
5420 * allow_dirs, otherwise FALSE is put there -- webb.
5421 * Handle backslashes before special characters, like "\*" and "\ ".
5422 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 * Returns NULL when out of memory.
5424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005426file_pat_to_reg_pat(
5427 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 char_u *pat_end, // first char after pattern or NULL
5429 char *allow_dirs, // Result passed back out in here
5430 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005432 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 char_u *endp;
5434 char_u *reg_pat;
5435 char_u *p;
5436 int i;
5437 int nested = 0;
5438 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440 if (allow_dirs != NULL)
5441 *allow_dirs = FALSE;
5442 if (pat_end == NULL)
5443 pat_end = pat + STRLEN(pat);
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 for (p = pat; p < pat_end; p++)
5446 {
5447 switch (*p)
5448 {
5449 case '*':
5450 case '.':
5451 case ',':
5452 case '{':
5453 case '}':
5454 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005455 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 break;
5457#ifdef BACKSLASH_IN_FILENAME
5458 case '\\':
5459 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 break;
5462#endif
5463 default:
5464 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005465 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 ++p;
5468 ++size;
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 break;
5471 }
5472 }
5473 reg_pat = alloc(size + 1);
5474 if (reg_pat == NULL)
5475 return NULL;
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479 if (pat[0] == '*')
5480 while (pat[0] == '*' && pat < pat_end - 1)
5481 pat++;
5482 else
5483 reg_pat[i++] = '^';
5484 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005485 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 while (endp - pat > 0 && *endp == '*')
5488 endp--;
5489 add_dollar = FALSE;
5490 }
5491 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5492 {
5493 switch (*p)
5494 {
5495 case '*':
5496 reg_pat[i++] = '.';
5497 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005498 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005499 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 break;
5501 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 case '~':
5503 reg_pat[i++] = '\\';
5504 reg_pat[i++] = *p;
5505 break;
5506 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 reg_pat[i++] = '.';
5508 break;
5509 case '\\':
5510 if (p[1] == NUL)
5511 break;
5512#ifdef BACKSLASH_IN_FILENAME
5513 if (!no_bslash)
5514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005515 // translate:
5516 // "\x" to "\\x" e.g., "dir\file"
5517 // "\*" to "\\.*" e.g., "dir\*.c"
5518 // "\?" to "\\." e.g., "dir\??.c"
5519 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5521 && p[1] != '+')
5522 {
5523 reg_pat[i++] = '[';
5524 reg_pat[i++] = '\\';
5525 reg_pat[i++] = '/';
5526 reg_pat[i++] = ']';
5527 if (allow_dirs != NULL)
5528 *allow_dirs = TRUE;
5529 break;
5530 }
5531 }
5532#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005533 // Undo escaping from ExpandEscape():
5534 // foo\?bar -> foo?bar
5535 // foo\%bar -> foo%bar
5536 // foo\,bar -> foo,bar
5537 // foo\ bar -> foo bar
5538 // Don't unescape \, * and others that are also special in a
5539 // regexp.
5540 // An escaped { must be unescaped since we use magic not
5541 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (*++p == '?'
5543#ifdef BACKSLASH_IN_FILENAME
5544 && no_bslash
5545#endif
5546 )
5547 reg_pat[i++] = '?';
5548 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005549 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005550 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005551 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005552 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5553 {
5554 reg_pat[i++] = '\\';
5555 reg_pat[i++] = '{';
5556 p += 2;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 else
5559 {
5560 if (allow_dirs != NULL && vim_ispathsep(*p)
5561#ifdef BACKSLASH_IN_FILENAME
5562 && (!no_bslash || *p != '\\')
5563#endif
5564 )
5565 *allow_dirs = TRUE;
5566 reg_pat[i++] = '\\';
5567 reg_pat[i++] = *p;
5568 }
5569 break;
5570#ifdef BACKSLASH_IN_FILENAME
5571 case '/':
5572 reg_pat[i++] = '[';
5573 reg_pat[i++] = '\\';
5574 reg_pat[i++] = '/';
5575 reg_pat[i++] = ']';
5576 if (allow_dirs != NULL)
5577 *allow_dirs = TRUE;
5578 break;
5579#endif
5580 case '{':
5581 reg_pat[i++] = '\\';
5582 reg_pat[i++] = '(';
5583 nested++;
5584 break;
5585 case '}':
5586 reg_pat[i++] = '\\';
5587 reg_pat[i++] = ')';
5588 --nested;
5589 break;
5590 case ',':
5591 if (nested)
5592 {
5593 reg_pat[i++] = '\\';
5594 reg_pat[i++] = '|';
5595 }
5596 else
5597 reg_pat[i++] = ',';
5598 break;
5599 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005600 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005602 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 *allow_dirs = TRUE;
5604 reg_pat[i++] = *p;
5605 break;
5606 }
5607 }
5608 if (add_dollar)
5609 reg_pat[i++] = '$';
5610 reg_pat[i] = NUL;
5611 if (nested != 0)
5612 {
5613 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005614 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005616 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005617 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 return reg_pat;
5620}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005621
5622#if defined(EINTR) || defined(PROTO)
5623/*
5624 * Version of read() that retries when interrupted by EINTR (possibly
5625 * by a SIGWINCH).
5626 */
5627 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005629{
5630 long ret;
5631
5632 for (;;)
5633 {
5634 ret = vim_read(fd, buf, bufsize);
5635 if (ret >= 0 || errno != EINTR)
5636 break;
5637 }
5638 return ret;
5639}
5640
5641/*
5642 * Version of write() that retries when interrupted by EINTR (possibly
5643 * by a SIGWINCH).
5644 */
5645 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005647{
5648 long ret = 0;
5649 long wlen;
5650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 // Repeat the write() so long it didn't fail, other than being interrupted
5652 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005653 while (ret < (long)bufsize)
5654 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005655 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005656 if (wlen < 0)
5657 {
5658 if (errno != EINTR)
5659 break;
5660 }
5661 else
5662 ret += wlen;
5663 }
5664 return ret;
5665}
5666#endif