blob: 5771a45e45eaaf0f3d7d181db06422428f999594 [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;
John Marriott14ede182024-12-29 16:14:19 +010058 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
60 if (msg_silent != 0)
61 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010062 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020063
Bram Moolenaar217e1b82019-12-01 21:41:28 +010064 // If it's extremely long, truncate it.
John Marriott14ede182024-12-29 16:14:19 +010065 len = STRLEN(IObuff);
66 if (len > IOSIZE - 100)
67 {
68 len = IOSIZE - 100;
69 IObuff[len] = NUL;
70 }
Bram Moolenaar6378b212020-06-29 21:32:06 +020071
72 // Avoid an over-long translation to cause trouble.
John Marriott14ede182024-12-29 16:14:19 +010073 if (*s != NUL)
74 STRNCPY(IObuff + len, s, 99);
Bram Moolenaar6378b212020-06-29 21:32:06 +020075
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 /*
77 * For the first message may have to start a new line.
78 * For further ones overwrite the previous one, reset msg_scroll before
79 * calling filemess().
80 */
81 msg_scroll_save = msg_scroll;
82 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
83 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010084 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 check_for_delay(FALSE);
86 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020087 if (prev_msg_col != 0 && msg_col == 0)
88 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 msg_scroll = msg_scroll_save;
90 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010091 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
93 msg_clr_eos();
94 out_flush();
95 msg_scrolled_ign = FALSE;
96}
97
98/*
99 * Read lines from file "fname" into the buffer after line "from".
100 *
101 * 1. We allocate blocks with lalloc, as big as possible.
102 * 2. Each block is filled with characters from the file with a single read().
103 * 3. The lines are inserted in the buffer with ml_append().
104 *
105 * (caller must check that fname != NULL, unless READ_STDIN is used)
106 *
107 * "lines_to_skip" is the number of lines that must be skipped
108 * "lines_to_read" is the number of lines that are appended
109 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
110 *
111 * flags:
112 * READ_NEW starting to edit a new buffer
113 * READ_FILTER reading filter output
114 * READ_STDIN read from stdin instead of a file
115 * READ_BUFFER read from curbuf instead of a file (converting after reading
116 * stdin)
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100117 * READ_NOFILE do not read a file, only trigger BufReadCmd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200119 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200120 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100122 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 */
124 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100125readfile(
126 char_u *fname,
127 char_u *sfname,
128 linenr_T from,
129 linenr_T lines_to_skip,
130 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100131 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100132 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100134 int retval = FAIL; // jump to "theend" instead of returning
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 int fd = 0;
136 int newfile = (flags & READ_NEW);
137 int check_readonly;
138 int filtering = (flags & READ_FILTER);
139 int read_stdin = (flags & READ_STDIN);
140 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200141 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000142 int set_options = newfile || read_buffer
143 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100144 linenr_T read_buf_lnum = 1; // next line to read from curbuf
145 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 char_u c;
147 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100148 char_u *ptr = NULL; // pointer into read buffer
149 char_u *buffer = NULL; // read buffer
150 char_u *new_buffer = NULL; // init to shut up gcc
151 char_u *line_start = NULL; // init to shut up gcc
152 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 colnr_T len;
154 long size = 0;
155 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200156 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 int skip_read = FALSE;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100158#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200159 off_T filesize_disk = 0; // file size read from disk
160 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200162 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200164#ifdef FEAT_PERSISTENT_UNDO
165 context_sha256_T sha_ctx;
166 int read_undo_file = FALSE;
167#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100168 int split = 0; // number of split lines
169#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100171 int error = FALSE; // errors encountered
172 int ff_error = EOL_UNKNOWN; // file format with errors
173 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#ifdef UNIX
175 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100176 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#else
178 int perm;
179#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100180 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200182 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 int file_readonly;
184 linenr_T skip_count = 0;
185 linenr_T read_count = 0;
186 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100187 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
188 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100189 int try_mac;
190 int try_dos;
191 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100194 linenr_T conv_error = 0; // line nr with conversion error
195 linenr_T illegal_byte = 0; // line nr with illegal byte
196 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
197 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000198 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100199 // BAD_KEEP, BAD_DROP or character to
200 // replace with
201 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100203 char_u *fenc; // fileencoding to use
204 int fenc_alloced; // fenc_next is in allocated memory
205 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 int advance_fenc = FALSE;
207 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100208#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100209 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100210# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100211 int did_iconv = FALSE; // TRUE when iconv() failed and trying
212 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100214#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100215 int converted = FALSE; // TRUE if conversion done
216 int notconverted = FALSE; // TRUE if conversion wanted but it
217 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100219 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100220 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200221 buf_T *old_curbuf;
222 char_u *old_b_ffname;
223 char_u *old_b_fname;
224 int using_b_ffname;
225 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200226 static char *msg_is_a_directory = N_("is a directory");
Bram Moolenaarbc385a12023-06-17 15:35:03 +0100227#ifdef FEAT_CRYPT
Bram Moolenaar438d0c52023-06-17 15:00:27 +0100228 int eof = FALSE;
Bram Moolenaarbc385a12023-06-17 15:35:03 +0100229#endif
Christian Brabandtaae58342023-04-23 17:50:22 +0100230#ifdef FEAT_SODIUM
231 int may_need_lseek = FALSE;
232#endif
John Marriott14ede182024-12-29 16:14:19 +0100233 size_t fnamelen = 0;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200234
zeertzjq5bf6c212024-03-31 18:41:27 +0200235 curbuf->b_au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200236
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100237 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
239 /*
240 * If there is no file name yet, use the one for the read file.
241 * BF_NOTEDITED is set to reflect this.
242 * Don't do this for a read from a filter.
243 * Only do this when 'cpoptions' contains the 'f' flag.
244 */
245 if (curbuf->b_ffname == NULL
246 && !filtering
247 && fname != NULL
248 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
249 && !(flags & READ_DUMMY))
250 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000251 if (set_rw_fname(fname, sfname) == FAIL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100252 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100255 // Remember the initial values of curbuf, curbuf->b_ffname and
256 // curbuf->b_fname to detect whether they are altered as a result of
257 // executing nasty autocommands. Also check if "fname" and "sfname"
258 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200259 old_curbuf = curbuf;
260 old_b_ffname = curbuf->b_ffname;
261 old_b_fname = curbuf->b_fname;
262 using_b_ffname = (fname == curbuf->b_ffname)
263 || (sfname == curbuf->b_ffname);
264 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200265
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100266 // After reading a file the cursor line changes but we don't want to
267 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000268 ex_no_reprint = TRUE;
269
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100270 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000271 need_fileinfo = FALSE;
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 /*
274 * For Unix: Use the short file name whenever possible.
275 * Avoids problems with networks and when directory names are changed.
276 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
277 * another directory, which we don't detect.
278 */
279 if (sfname == NULL)
280 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200281#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 fname = sfname;
283#endif
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 /*
286 * The BufReadCmd and FileReadCmd events intercept the reading process by
287 * executing the associated commands instead.
288 */
289 if (!filtering && !read_stdin && !read_buffer)
290 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100291 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100293 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
295 curbuf->b_op_start.col = 0;
296
297 if (newfile)
298 {
299 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
300 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200301 {
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100302 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200304 if (aborting())
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100305 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200307 // The BufReadCmd code usually uses ":read" to get the text and
308 // perhaps ":file" to change the buffer name. But we should
309 // consider this to work like ":edit", thus reset the
310 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
311 // same file.
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100312 if (retval == OK)
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200313 curbuf->b_flags &= ~BF_NOTEDITED;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100314 goto theend;
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 }
317 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
318 FALSE, NULL, eap))
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_EVAL
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100321 retval = aborting() ? FAIL : OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#else
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100323 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100325 goto theend;
326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100328 curbuf->b_op_start = orig_start;
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100329
330 if (flags & READ_NOFILE)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100331 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100332 // Return NOTDONE instead of FAIL so that BufEnter can be triggered
333 // and other operations don't fail.
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100334 retval = NOTDONE;
335 goto theend;
336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
339 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100340 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100342 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000344 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
John Marriott14ede182024-12-29 16:14:19 +0100346 fnamelen = STRLEN(fname);
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200347
348 // If the name is too long we might crash further on, quit here.
John Marriott14ede182024-12-29 16:14:19 +0100349 if (fnamelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000350 {
351 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
352 msg_end();
353 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100354 goto theend;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000355 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200356
357 // If the name ends in a path separator, we can't open it. Check here,
358 // because reading the file may actually work, but then creating the
359 // swap file may destroy it! Reported on MS-DOS and Win 95.
John Marriott14ede182024-12-29 16:14:19 +0100360 if (after_pathsep(fname, fname + fnamelen))
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200361 {
362 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
363 msg_end();
364 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100365 retval = NOTDONE;
366 goto theend;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100369#if defined(UNIX) || defined(VMS)
Christian Brabandtf1c31342025-02-23 09:36:56 +0100370 if (!read_stdin && fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 perm = mch_getperm(fname);
Christian Brabandtf1c31342025-02-23 09:36:56 +0100372#endif
373
374 if (!read_stdin && !read_buffer && !read_fifo)
375 {
376#if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100377 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
378 && !S_ISFIFO(perm) // ... or fifo
379 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000380# ifdef OPEN_CHR_FILES
381 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100382 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 )
385 {
zeertzjqb8989fb2025-02-23 09:56:10 +0100386 /*
387 * On Unix it is possible to read a directory, so we have to
388 * check for it before the mch_open().
389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100391 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200392 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100393 retval = NOTDONE;
394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 else
396 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
397 msg_end();
398 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100399 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200401#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100402#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000403 /*
404 * MS-Windows allows opening a device, but we will probably get stuck
405 * trying to read it.
406 */
407 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
408 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000409 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000410 msg_end();
411 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100412 goto theend;
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000413 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000414#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200415 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200418 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
420 /*
421 * When opening a new file we take the readonly flag from the file.
422 * Default is r/w, can be set to r/o below.
423 * Don't reset it when in readonly mode
424 * Only set/reset b_p_ro when BF_CHECK_RO is set.
425 */
426 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000427 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 curbuf->b_p_ro = FALSE;
429
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200430 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100432 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 if (mch_stat((char *)fname, &st) >= 0)
434 {
435 buf_store_time(curbuf, &st, fname);
436 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100437 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100438#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200439 filesize_disk = st.st_size;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef UNIX
442 /*
443 * Use the protection bits of the original file for the swap file.
444 * This makes it possible for others to read the name of the
445 * edited file from the swapfile, but only if they can read the
446 * edited file.
447 * Remove the "write" and "execute" bits for group and others
448 * (they must not write the swapfile).
449 * Add the "read" and "write" bits for the user, otherwise we may
450 * not be able to write to the file ourselves.
451 * Setting the bits is done below, after creating the swap file.
452 */
453 swap_mode = (st.st_mode & 0644) | 0600;
454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455#ifdef VMS
456 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000457 curbuf->b_fab_rat = st.st_fab_rat;
458 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#endif
460 }
461 else
462 {
463 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100464 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100466 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 curbuf->b_orig_size = 0;
468 curbuf->b_orig_mode = 0;
469 }
470
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100471 // Reset the "new file" flag. It will be set again below when the
472 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
474 }
475
476/*
477 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100478 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 */
480 file_readonly = FALSE;
481 if (read_stdin)
482 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100483#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100484 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 setmode(0, O_BINARY);
486#endif
487 }
488 else if (!read_buffer)
489 {
490#ifdef USE_MCH_ACCESS
491 if (
492# ifdef UNIX
493 !(perm & 0222) ||
494# endif
495 mch_access((char *)fname, W_OK))
496 file_readonly = TRUE;
497 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
498#else
499 if (!newfile
500 || readonlymode
501 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
502 {
503 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
506 }
507#endif
508 }
509
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100510 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {
512#ifndef UNIX
513 int isdir_f;
514#endif
515 msg_scroll = msg_save;
516#ifndef UNIX
517 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100518 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 */
520 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100521 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (isdir_f)
523 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200524 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100525 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527 else
528#endif
529 if (newfile)
530 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200531 if (perm < 0
532#ifdef ENOENT
533 && errno == ENOENT
534#endif
535 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 {
537 /*
538 * Set the 'new-file' flag, so that when the file has
539 * been created by someone else, a ":w" will complain.
540 */
541 curbuf->b_flags |= BF_NEW;
542
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 // Create a swap file now, so that other Vims are warned
544 // that we are editing this file. Don't do this for a
545 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100549 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000550 if (curbuf != old_curbuf
551 || (using_b_ffname
552 && (old_b_ffname != curbuf->b_ffname))
553 || (using_b_fname
554 && (old_b_fname != curbuf->b_fname)))
555 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000556 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100557 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000558 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000559 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000560 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200561 filemess(curbuf, sfname,
562 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000563 else
564 filemess(curbuf, sfname,
565 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100567 // Even though this is a new file, it might have been
568 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 check_marks_read();
570#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100571 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200572 if (eap != NULL)
573 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
575 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100576 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 save_file_ff(curbuf);
578
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100579#if defined(FEAT_EVAL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100580 if (!aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100582 retval = OK; // a new file is not an error
583 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585 else
586 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000587 filemess(curbuf, sfname, (char_u *)(
588# ifdef EFBIG
589 (errno == EFBIG) ? _("[File too big]") :
590# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200591# ifdef EOVERFLOW
592 (errno == EOVERFLOW) ? _("[File too big]") :
593# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000594 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100595 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 }
598
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100599 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601
602 /*
603 * Only set the 'ro' flag for readonly files the first time they are
604 * loaded. Help files always get readonly mode
605 */
606 if ((check_readonly && file_readonly) || curbuf->b_help)
607 curbuf->b_p_ro = TRUE;
608
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000609 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100611 // Don't change 'eol' if reading from buffer as it will already be
612 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000613 if (!read_buffer)
614 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100615 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100616 curbuf->b_start_eof = FALSE;
617 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000618 curbuf->b_start_eol = TRUE;
619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000621 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 }
623
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 // Create a swap file now, so that other Vims are warned that we are
625 // editing this file.
626 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000630 if (!read_stdin && (curbuf != old_curbuf
631 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
632 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
633 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000634 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000635 if (!read_buffer)
636 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100637 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000641 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
642 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100643 {
644 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
645
646 /*
647 * If the group-read bit is set but not the world-read bit, then
648 * the group must be equal to the group of the original file. If
649 * we can't make that happen then reset the group-read bit. This
650 * avoids making the swap file readable to more users when the
651 * primary group of the user is too permissive.
652 */
653 if ((swap_mode & 044) == 040)
654 {
655 stat_T swap_st;
656
657 if (mch_stat((char *)swap_fname, &swap_st) >= 0
658 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200659# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100660 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200661 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200662# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200663 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100664 swap_mode &= 0600;
665 }
666
667 (void)mch_setperm(swap_fname, (long)swap_mode);
668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#endif
670 }
671
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200672 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (swap_exists_action == SEA_QUIT)
674 {
675 if (!read_buffer && !read_stdin)
676 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100677 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 /*
683 * Set '[ mark to the line above where the lines go (line 1 if zero).
684 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100685 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
687 curbuf->b_op_start.col = 0;
688
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100689 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
690 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
691 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (!read_buffer)
694 {
695 int m = msg_scroll;
696 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 /*
699 * The file must be closed again, the autocommands may want to change
700 * the file before reading it.
701 */
702 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
705 /*
706 * The output from the autocommands should not overwrite anything and
707 * should not be overwritten: Set msg_scroll, restore its value if no
708 * output was done.
709 */
710 msg_scroll = TRUE;
711 if (filtering)
712 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
713 FALSE, curbuf, eap);
714 else if (read_stdin)
715 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
716 FALSE, curbuf, eap);
717 else if (newfile)
718 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
719 FALSE, curbuf, eap);
720 else
721 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
722 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100723 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100724 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
725 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
726 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100727 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (msg_scrolled == n)
730 msg_scroll = m;
731
732#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100733 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
735 --no_wait_return;
736 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100737 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100738 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740#endif
741 /*
742 * Don't allow the autocommands to change the current buffer.
743 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000744 *
745 * Don't allow the autocommands to change the buffer name either
746 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 */
748 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000749 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
750 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
752 {
753 --no_wait_return;
754 msg_scroll = msg_save;
755 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000756 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000758 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100759 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100760 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100764 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
766
767 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
768 {
769 /*
770 * Show the user that we are busy reading the input. Sometimes this
771 * may take a while. When reading from stdin another program may
772 * still be running, don't move the cursor to the last line, unless
773 * always using the GUI.
774 */
775 if (read_stdin)
776 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100777 if (!is_not_a_term())
778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200780# ifdef VIMDLL
781 if (!gui.in_use)
782# endif
783 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#endif
785#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100787 if (gui.in_use && !gui.dying && !gui.starting)
788 {
John Marriott14ede182024-12-29 16:14:19 +0100789 size_t plen = STRLEN(_("Reading from stdin..."));
790
Amon Sha10197932022-02-21 15:07:12 +0000791 // make a copy, gui_write() may try to change it
John Marriott14ede182024-12-29 16:14:19 +0100792 p = vim_strnsave((char_u *)_("Reading from stdin..."), plen);
Amon Sha10197932022-02-21 15:07:12 +0000793 if (p != NULL)
794 {
John Marriott14ede182024-12-29 16:14:19 +0100795 gui_write(p, (int)plen);
Amon Sha10197932022-02-21 15:07:12 +0000796 vim_free(p);
797 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802 else if (!read_buffer)
803 filemess(curbuf, sfname, (char_u *)"", 0);
804 }
805
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100806 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
808 /*
809 * Set linecnt now, before the "retry" caused by a wrong guess for
810 * fileformat, and after the autocommands, which may change them.
811 */
812 linecnt = curbuf->b_ml.ml_line_count;
813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000815 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000816 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000817 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000818 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000819 curbuf->b_bad_char = eap->bad_char;
820 }
821 else
822 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000825 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 */
827 if (eap != NULL && eap->force_enc != 0)
828 {
829 fenc = enc_canonize(eap->cmd + eap->force_enc);
830 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000831 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 else if (curbuf->b_p_bin)
834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 fenc_alloced = FALSE;
837 }
838 else if (curbuf->b_help)
839 {
840 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000841 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Help files are either utf-8 or latin1. Try utf-8 first, if this
844 // fails it must be latin1.
845 // Always do this when 'encoding' is "utf-8". Otherwise only do
846 // this when needed to avoid [converted] remarks all the time.
847 // It is needed when the first line contains non-ASCII characters.
848 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 fenc = (char_u *)"latin1";
850 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000851 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
John Marriott14ede182024-12-29 16:14:19 +0100853 fc = fname[fnamelen - 1];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000854 if (TOLOWER_ASC(fc) == 'x')
855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Read the first line (and a bit more). Immediately rewind to
857 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100858 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200859 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000860 for (p = firstline; p < firstline + len; ++p)
861 if (*p >= 0x80)
862 {
863 c = TRUE;
864 break;
865 }
866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868
869 if (c)
870 {
871 fenc_next = fenc;
872 fenc = (char_u *)"utf-8";
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // When the file is utf-8 but a character doesn't fit in
875 // 'encoding' don't retry. In help text editing utf-8 bytes
876 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000877 if (!enc_utf8)
878 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880 fenc_alloced = FALSE;
881 }
882 else if (*p_fencs == NUL)
883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 fenc_alloced = FALSE;
886 }
887 else
888 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100889 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200890 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 /*
894 * Jump back here to retry reading the file in different ways.
895 * Reasons to retry:
896 * - encoding conversion failed: try another one from "fenc_next"
897 * - BOM detected and fenc was set, need to setup conversion
898 * - "fileformat" check failed: try another
899 *
900 * Variables set for special retry actions:
901 * "file_rewind" Rewind the file to start reading it again.
902 * "advance_fenc" Advance "fenc" using "fenc_next".
903 * "skip_read" Re-use already read bytes (BOM detected).
904 * "did_iconv" iconv() conversion failed, try 'charconvert'.
905 * "keep_fileformat" Don't reset "fileformat".
906 *
907 * Other status indicators:
908 * "tmpname" When != NULL did conversion with 'charconvert'.
909 * Output file has to be deleted afterwards.
910 * "iconv_fd" When != -1 did conversion with iconv().
911 */
912retry:
913
914 if (file_rewind)
915 {
916 if (read_buffer)
917 {
918 read_buf_lnum = 1;
919 read_buf_col = 0;
920 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200921 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100923 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 error = TRUE;
925 goto failed;
926 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100927 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200929 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000931 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000934 curbuf->b_start_bomb = FALSE;
935 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000936 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
938
939 /*
940 * When retrying with another "fenc" and the first time "fileformat"
941 * will be reset.
942 */
943 if (keep_fileformat)
944 keep_fileformat = FALSE;
945 else
946 {
947 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000950 try_unix = try_dos = try_mac = FALSE;
951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100953 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100955 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100957 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959
Bram Moolenaar13505972019-01-24 15:04:48 +0100960#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (iconv_fd != (iconv_t)-1)
962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 iconv_close(iconv_fd);
965 iconv_fd = (iconv_t)-1;
966 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 if (advance_fenc)
970 {
971 /*
972 * Try the next entry in 'fileencodings'.
973 */
974 advance_fenc = FALSE;
975
976 if (eap != NULL && eap->force_enc != 0)
977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100978 // Conversion given with "++cc=" wasn't possible, read
979 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000981 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (fenc_alloced)
983 vim_free(fenc);
984 fenc = (char_u *)"";
985 fenc_alloced = FALSE;
986 }
987 else
988 {
989 if (fenc_alloced)
990 vim_free(fenc);
991 if (fenc_next != NULL)
992 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200993 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 else
996 {
997 fenc = (char_u *)"";
998 fenc_alloced = FALSE;
999 }
1000 }
1001 if (tmpname != NULL)
1002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001003 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01001004 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 }
1007
1008 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001009 * Conversion may be required when the encoding of the file is different
1010 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 */
1012 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001013 converted = need_conversion(fenc);
1014 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001016 // "ucs-bom" means we need to check the first bytes of the file
1017 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (STRCMP(fenc, ENC_UCSBOM) == 0)
1019 fio_flags = FIO_UCSBOM;
1020
1021 /*
1022 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1023 * done. This is handled below after read(). Prepare the
1024 * fio_flags to avoid having to parse the string each time.
1025 * Also check for Unicode to Latin1 conversion, because iconv()
1026 * appears not to handle this correctly. This works just like
1027 * conversion to UTF-8 except how the resulting character is put in
1028 * the buffer.
1029 */
1030 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1031 fio_flags = get_fio_flags(fenc);
1032
Bram Moolenaar4f974752019-02-17 17:44:42 +01001033#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 /*
1035 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1036 * is handled with MultiByteToWideChar().
1037 */
1038 if (fio_flags == 0)
1039 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar13505972019-01-24 15:04:48 +01001042#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (fio_flags == 0)
1045 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 /*
1050 * Try using iconv() if we can't convert internally.
1051 */
1052 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001053# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 )
1057 iconv_fd = (iconv_t)my_iconv_open(
1058 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 /*
1063 * Use the 'charconvert' expression when conversion is required
1064 * and we can't do it internally or with iconv().
1065 */
1066 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001067 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001068# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 )
1072 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001073# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001075# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // Skip conversion when it's already done (retry for wrong
1077 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (tmpname == NULL)
1079 {
1080 tmpname = readfile_charconvert(fname, fenc, &fd);
1081 if (tmpname == NULL)
1082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001083 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 advance_fenc = TRUE;
1085 if (fd < 0)
1086 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001087 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001088 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 error = TRUE;
1090 goto failed;
1091 }
1092 goto retry;
1093 }
1094 }
1095 }
1096 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001100#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 )
1104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 // Conversion wanted but we can't.
1106 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 advance_fenc = TRUE;
1108 goto retry;
1109 }
1110 }
1111 }
1112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001113 // Set "can_retry" when it's possible to rewind the file and try with
1114 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1115 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001116 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 if (!skip_read)
1119 {
1120 linerest = 0;
1121 filesize = 0;
Bram Moolenaar7f291222023-06-17 16:19:30 +01001122#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001123 filesize_count = 0;
Bram Moolenaar7f291222023-06-17 16:19:30 +01001124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 skip_count = lines_to_skip;
1126 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001128#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001129 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1130 && curbuf->b_ffname != NULL
1131 && curbuf->b_p_udf
1132 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001133 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001134 && !read_stdin
1135 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001136 if (read_undo_file)
1137 sha256_start(&sha_ctx);
1138#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001139#ifdef FEAT_CRYPT
1140 if (curbuf->b_cryptstate != NULL)
1141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142 // Need to free the state, but keep the key, don't want to ask for
1143 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001144 crypt_free_state(curbuf->b_cryptstate);
1145 curbuf->b_cryptstate = NULL;
1146 }
1147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149
1150 while (!error && !got_int)
1151 {
1152 /*
1153 * We allocate as much space for the file as we can get, plus
1154 * space for the old line plus room for one terminating NUL.
1155 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001156 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001158 if (!skip_read)
1159 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001160#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001162#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163 // Use buffer >= 64K. Add linerest to double the size if the
1164 // line gets very long, to avoid a lot of copying. But don't
1165 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001166 size = 0x10000L + linerest;
1167 if (size > 0x100000L)
1168 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001169#endif
1170 }
1171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001172 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001173 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
1175 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001176 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 size = 1;
1178 }
1179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
1181 if (!skip_read)
1182 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001183 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001185 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 FALSE)) != NULL)
1187 break;
1188 }
1189 if (new_buffer == NULL)
1190 {
1191 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1192 error = TRUE;
1193 break;
1194 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1197 vim_free(buffer);
1198 buffer = new_buffer;
1199 ptr = buffer + linerest;
1200 line_start = buffer;
1201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001202 // May need room to translate into.
1203 // For iconv() we don't really know the required space, use a
1204 // factor ICONV_MULT.
1205 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1206 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1207 // become up to 4 bytes, size must be multiple of 2
1208 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1209 // multiple of 2
1210 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1211 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001212 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001213#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (iconv_fd != (iconv_t)-1)
1215 size = size / ICONV_MULT;
1216 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (fio_flags & FIO_LATIN1)
1219 size = size / 2;
1220 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1221 size = (size * 2 / 3) & ~1;
1222 else if (fio_flags & FIO_UCS4)
1223 size = (size * 2 / 3) & ~3;
1224 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001225 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001226#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001228 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001229#endif
1230#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001232 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (conv_restlen > 0)
1236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001237 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 mch_memmove(ptr, conv_rest, conv_restlen);
1239 ptr += conv_restlen;
1240 size -= conv_restlen;
1241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 if (read_buffer)
1244 {
1245 /*
1246 * Read bytes from curbuf. Used for converting text read
1247 * from stdin.
1248 */
1249 if (read_buf_lnum > from)
1250 size = 0;
1251 else
1252 {
1253 int n, ni;
1254 long tlen;
1255
1256 tlen = 0;
1257 for (;;)
1258 {
1259 p = ml_get(read_buf_lnum) + read_buf_col;
zeertzjq94b7c322024-03-12 21:50:32 +01001260 n = ml_get_len(read_buf_lnum) - read_buf_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if ((int)tlen + n + 1 > size)
1262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001263 // Filled up to "size", append partial line.
1264 // Change NL to NUL to reverse the effect done
1265 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001266 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 for (ni = 0; ni < n; ++ni)
1268 {
1269 if (p[ni] == NL)
1270 ptr[tlen++] = NUL;
1271 else
1272 ptr[tlen++] = p[ni];
1273 }
1274 read_buf_col += n;
1275 break;
1276 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001277
1278 // Append whole line and new-line. Change NL
1279 // to NUL to reverse the effect done below.
1280 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001282 if (p[ni] == NL)
1283 ptr[tlen++] = NUL;
1284 else
1285 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001287 ptr[tlen++] = NL;
1288 read_buf_col = 0;
1289 if (++read_buf_lnum > from)
1290 {
1291 // When the last line didn't have an
1292 // end-of-line don't add it now either.
1293 if (!curbuf->b_p_eol)
1294 --tlen;
1295 size = tlen;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001296#ifdef FEAT_CRYPT
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001297 eof = TRUE;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001298#endif
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001299 break;
1300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 }
1303 }
1304 else
1305 {
1306 /*
1307 * Read bytes from the file.
1308 */
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001309#ifdef FEAT_SODIUM
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001310 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001311 //
1312 // Sodium encryption requires a fixed block size to
1313 // successfully decrypt. However, unfortunately the file
1314 // header size changes between xchacha20 and xchacha20v2 by
1315 // 'add_len' bytes.
1316 // So we will now read the maximum header size + encryption
1317 // metadata, but after determining to read an xchacha20
1318 // encrypted file, we have to rewind the file descriptor by
1319 // 'add_len' bytes in the second round.
1320 //
1321 // Be careful with changing it, it needs to stay the same
1322 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001323 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001324 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001325 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001326 size = WRITEBUFSIZE + crypt_get_max_header_len()
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001327 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1328 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001329 may_need_lseek = TRUE;
1330 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001331
Christian Brabandtaae58342023-04-23 17:50:22 +01001332 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1333 && crypt_method_is_sodium(
1334 curbuf->b_cryptstate->method_nr)))
1335 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001336 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001337 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1338 // description above)
1339 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1340 && !eof && may_need_lseek)
1341 {
1342 lseek(fd, crypt_get_header_len(
1343 curbuf->b_cryptstate->method_nr)
1344 - crypt_get_max_header_len(), SEEK_CUR);
1345 may_need_lseek = FALSE;
1346 }
1347 }
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001348#endif
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001349 long read_size = size;
1350 size = read_eintr(fd, ptr, read_size);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001351#ifdef FEAT_CRYPT
Bram Moolenaar7f291222023-06-17 16:19:30 +01001352 // Did we reach end of file?
1353 filesize_count += size;
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001354 eof = (size < read_size || filesize_count == filesize_disk);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001358#ifdef FEAT_CRYPT
1359 /*
1360 * At start of file: Check for magic number of encryption.
1361 */
1362 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001363 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001364 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1365 &filesize, newfile, sfname,
1366 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001367# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001368 if (curbuf->b_cryptstate != NULL
1369 && !crypt_works_inplace(curbuf->b_cryptstate))
1370 // reading undo file requires crypt_decode_inplace()
1371 read_undo_file = FALSE;
1372# endif
1373 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001374 /*
1375 * Decrypt the read bytes. This is done before checking for
1376 * EOF because the crypt layer may be buffering.
1377 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001378 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1379 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001380 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001381# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001382 if (crypt_works_inplace(curbuf->b_cryptstate))
1383 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001384# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001385 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1386 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001387# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001388 }
1389 else
1390 {
1391 char_u *newptr = NULL;
1392 int decrypted_size;
1393
1394 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001395 curbuf->b_cryptstate, ptr, size,
1396 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001397
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001398 if (decrypted_size < 0)
1399 {
1400 // error message already given
1401 error = TRUE;
1402 vim_free(newptr);
1403 break;
1404 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001405 // If the crypt layer is buffering, not producing
1406 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001407 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001408 continue;
1409
1410 if (linerest == 0)
1411 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 // Simple case: reuse returned buffer (may be
1413 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001414 new_buffer = newptr;
1415 }
1416 else
1417 {
1418 long_u new_size;
1419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001420 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001421 new_size = (long_u)(decrypted_size + linerest + 1);
1422 new_buffer = lalloc(new_size, FALSE);
1423 if (new_buffer == NULL)
1424 {
1425 do_outofmem_msg(new_size);
1426 error = TRUE;
1427 break;
1428 }
1429
1430 mch_memmove(new_buffer, buffer, linerest);
1431 if (newptr != NULL)
1432 mch_memmove(new_buffer + linerest, newptr,
1433 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001434 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001435 }
1436
1437 if (new_buffer != NULL)
1438 {
1439 vim_free(buffer);
1440 buffer = new_buffer;
1441 new_buffer = NULL;
1442 line_start = buffer;
1443 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001444 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001445 }
1446 size = decrypted_size;
1447 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001448# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001449 }
1450#endif
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (size <= 0)
1453 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001454 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001457 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001458 /*
1459 * Reached end-of-file but some trailing bytes could
1460 * not be converted. Truncated file?
1461 */
1462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001463 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001464 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001465#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001466 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001467#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001468 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001469 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001470 if (can_retry)
1471 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001472 if (conv_error == 0)
1473 conv_error = curbuf->b_ml.ml_line_count
1474 - linecnt + 1;
1475 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001476 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001477 else if (illegal_byte == 0)
1478 illegal_byte = curbuf->b_ml.ml_line_count
1479 - linecnt + 1;
1480 if (bad_char_behavior == BAD_DROP)
1481 {
1482 *(ptr - conv_restlen) = NUL;
1483 conv_restlen = 0;
1484 }
1485 else
1486 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001487 // Replace the trailing bytes with the replacement
1488 // character if we were converting; if we weren't,
1489 // leave the UTF8 checking code to do it, as it
1490 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001491 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001492#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001493 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001494#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001495 ))
1496 {
1497 while (conv_restlen > 0)
1498 {
1499 *(--ptr) = bad_char_behavior;
1500 --conv_restlen;
1501 }
1502 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001503 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001504#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001505 if (iconv_fd != (iconv_t)-1)
1506 {
1507 iconv_close(iconv_fd);
1508 iconv_fd = (iconv_t)-1;
1509 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001510#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001511 }
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 }
1515 skip_read = FALSE;
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 /*
1518 * At start of file (or after crypt magic number): Check for BOM.
1519 * Also check for a BOM for other Unicode encodings, but not after
1520 * converting with 'charconvert' or when a BOM has already been
1521 * found.
1522 */
1523 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001524#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001525 || (cryptkey != NULL
1526 && filesize == crypt_get_header_len(
1527 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 )
1530 && (fio_flags == FIO_UCSBOM
1531 || (!curbuf->b_p_bomb
1532 && tmpname == NULL
1533 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1534 {
1535 char_u *ccname;
1536 int blen;
1537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001538 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (size < 2 || curbuf->b_p_bin)
1540 ccname = NULL;
1541 else
1542 ccname = check_for_bom(ptr, size, &blen,
1543 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1544 if (ccname != NULL)
1545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 filesize += blen;
1548 size -= blen;
1549 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001550 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001553 curbuf->b_start_bomb = TRUE;
1554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556
1557 if (fio_flags == FIO_UCSBOM)
1558 {
1559 if (ccname == NULL)
1560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001561 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 advance_fenc = TRUE;
1563 }
1564 else
1565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001566 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (fenc_alloced)
1568 vim_free(fenc);
1569 fenc = ccname;
1570 fenc_alloced = FALSE;
1571 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001572 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 skip_read = TRUE;
1574 goto retry;
1575 }
1576 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001577
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001578 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001579 ptr -= conv_restlen;
1580 size += conv_restlen;
1581 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 /*
1583 * Break here for a read error or end-of-file.
1584 */
1585 if (size <= 0)
1586 break;
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaar13505972019-01-24 15:04:48 +01001589#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (iconv_fd != (iconv_t)-1)
1591 {
1592 /*
1593 * Attempt conversion of the read bytes to 'encoding' using
1594 * iconv().
1595 */
1596 const char *fromp;
1597 char *top;
1598 size_t from_size;
1599 size_t to_size;
1600
1601 fromp = (char *)ptr;
1602 from_size = size;
1603 ptr += size;
1604 top = (char *)ptr;
1605 to_size = real_size - size;
1606
1607 /*
1608 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001609 * another conversion. Except for when there is no
1610 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001612 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1613 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1615 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001616 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001617 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001618 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001619 if (conv_error == 0)
1620 conv_error = readfile_linenr(linecnt,
1621 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001623 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001624 ++fromp;
1625 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001626 if (bad_char_behavior == BAD_KEEP)
1627 {
1628 *top++ = *(fromp - 1);
1629 --to_size;
1630 }
1631 else if (bad_char_behavior != BAD_DROP)
1632 {
1633 *top++ = bad_char_behavior;
1634 --to_size;
1635 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 if (from_size > 0)
1639 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // Some remaining characters, keep them for the next
1641 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1643 conv_restlen = (int)from_size;
1644 }
1645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001646 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 line_start = ptr - linerest;
1648 mch_memmove(line_start, buffer, (size_t)linerest);
1649 size = (long)((char_u *)top - ptr);
1650 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
Bram Moolenaar4f974752019-02-17 17:44:42 +01001653#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (fio_flags & FIO_CODEPAGE)
1655 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001656 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001657 WCHAR ucs2buf[3];
1658 int ucs2len;
1659 int codepage = FIO_GET_CP(fio_flags);
1660 int bytelen;
1661 int found_bad;
1662 char replstr[2];
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 /*
1665 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001666 * a codepage, using standard MS-Windows functions. This
1667 * requires two steps:
1668 * 1. convert from 'fileencoding' to ucs-2
1669 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001671 * Because there may be illegal bytes AND an incomplete byte
1672 * sequence at the end, we may have to do the conversion one
1673 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001677 if (bad_char_behavior > 0)
1678 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001680 replstr[0] = '?';
1681 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001684 * Move the bytes to the end of the buffer, so that we have
1685 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001687 src = ptr + real_size - size;
1688 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001690 /*
1691 * Do the conversion.
1692 */
1693 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001694 while (size > 0)
1695 {
1696 found_bad = FALSE;
1697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001698# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001699 if (codepage == CP_UTF8)
1700 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001701 // Handle CP_UTF8 input ourselves to be able to handle
1702 // trailing bytes properly.
1703 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001704 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001705 if (bytelen > size)
1706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001707 // Only got some bytes of a character. Normally
1708 // it's put in "conv_rest", but if it's too long
1709 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001710 if (bytelen <= CONV_RESTLEN)
1711 break;
1712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001713 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001714 bytelen = size;
1715 found_bad = TRUE;
1716 }
1717 else
1718 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001719 int u8c = utf_ptr2char(src);
1720
Bram Moolenaar86e01082005-12-29 22:45:34 +00001721 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001722 found_bad = TRUE;
1723 ucs2buf[0] = u8c;
1724 ucs2len = 1;
1725 }
1726 }
1727 else
1728# endif
1729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // We don't know how long the byte sequence is, try
1731 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001732 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1733 ++bytelen)
1734 {
1735 ucs2len = MultiByteToWideChar(codepage,
1736 MB_ERR_INVALID_CHARS,
1737 (LPCSTR)src, bytelen,
1738 ucs2buf, 3);
1739 if (ucs2len > 0)
1740 break;
1741 }
1742 if (ucs2len == 0)
1743 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001744 // If we have only one byte then it's probably an
1745 // incomplete byte sequence. Otherwise discard
1746 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001747 if (size == 1)
1748 break;
1749 found_bad = TRUE;
1750 bytelen = 1;
1751 }
1752 }
1753
1754 if (!found_bad)
1755 {
1756 int i;
1757
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001759 if (enc_utf8)
1760 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001762 for (i = 0; i < ucs2len; ++i)
1763 dst += utf_char2bytes(ucs2buf[i], dst);
1764 }
1765 else
1766 {
1767 BOOL bad = FALSE;
1768 int dstlen;
1769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // From UCS-2 to "enc_codepage". If the
1771 // conversion uses the default character "?",
1772 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001773 dstlen = WideCharToMultiByte(enc_codepage, 0,
1774 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001775 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001776 replstr, &bad);
1777 if (bad)
1778 found_bad = TRUE;
1779 else
1780 dst += dstlen;
1781 }
1782 }
1783
1784 if (found_bad)
1785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001786 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001787 if (can_retry)
1788 goto rewind_retry;
1789 if (conv_error == 0)
1790 conv_error = readfile_linenr(linecnt, ptr, dst);
1791 if (bad_char_behavior != BAD_DROP)
1792 {
1793 if (bad_char_behavior == BAD_KEEP)
1794 {
1795 mch_memmove(dst, src, bytelen);
1796 dst += bytelen;
1797 }
1798 else
1799 *dst++ = bad_char_behavior;
1800 }
1801 }
1802
1803 src += bytelen;
1804 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001806
1807 if (size > 0)
1808 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001809 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001810 mch_memmove(conv_rest, src, size);
1811 conv_restlen = size;
1812 }
1813
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001814 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001815 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001818#endif
1819#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (fio_flags & FIO_MACROMAN)
1821 {
1822 /*
1823 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001824 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001826 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (fio_flags != 0)
1832 {
1833 int u8c;
1834 char_u *dest;
1835 char_u *tail = NULL;
1836
1837 /*
1838 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1839 * "enc_utf8" not set: Convert Unicode to Latin1.
1840 * Go from end to start through the buffer, because the number
1841 * of bytes may increase.
1842 * "dest" points to after where the UTF-8 bytes go, "p" points
1843 * to after the next character to convert.
1844 */
1845 dest = ptr + real_size;
1846 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1847 {
1848 p = ptr + size;
1849 if (fio_flags == FIO_UTF8)
1850 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001851 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 tail = ptr + size - 1;
1853 while (tail > ptr && (*tail & 0xc0) == 0x80)
1854 --tail;
1855 if (tail + utf_byte2len(*tail) <= ptr + size)
1856 tail = NULL;
1857 else
1858 p = tail;
1859 }
1860 }
1861 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001863 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 p = ptr + (size & ~1);
1865 if (size & 1)
1866 tail = p;
1867 if ((fio_flags & FIO_UTF16) && p > ptr)
1868 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001869 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (fio_flags & FIO_ENDIAN_L)
1871 {
1872 u8c = (*--p << 8);
1873 u8c += *--p;
1874 }
1875 else
1876 {
1877 u8c = *--p;
1878 u8c += (*--p << 8);
1879 }
1880 if (u8c >= 0xd800 && u8c <= 0xdbff)
1881 tail = p;
1882 else
1883 p += 2;
1884 }
1885 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001888 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 p = ptr + (size & ~3);
1890 if (size & 3)
1891 tail = p;
1892 }
1893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 // If there is a trailing incomplete sequence move it to
1895 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (tail != NULL)
1897 {
1898 conv_restlen = (int)((ptr + size) - tail);
1899 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1900 size -= conv_restlen;
1901 }
1902
1903
1904 while (p > ptr)
1905 {
1906 if (fio_flags & FIO_LATIN1)
1907 u8c = *--p;
1908 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1909 {
1910 if (fio_flags & FIO_ENDIAN_L)
1911 {
1912 u8c = (*--p << 8);
1913 u8c += *--p;
1914 }
1915 else
1916 {
1917 u8c = *--p;
1918 u8c += (*--p << 8);
1919 }
1920 if ((fio_flags & FIO_UTF16)
1921 && u8c >= 0xdc00 && u8c <= 0xdfff)
1922 {
1923 int u16c;
1924
1925 if (p == ptr)
1926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001927 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (can_retry)
1929 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001930 if (conv_error == 0)
1931 conv_error = readfile_linenr(linecnt,
1932 ptr, p);
1933 if (bad_char_behavior == BAD_DROP)
1934 continue;
1935 if (bad_char_behavior != BAD_KEEP)
1936 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001939 // found second word of double-word, get the first
1940 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (fio_flags & FIO_ENDIAN_L)
1942 {
1943 u16c = (*--p << 8);
1944 u16c += *--p;
1945 }
1946 else
1947 {
1948 u16c = *--p;
1949 u16c += (*--p << 8);
1950 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001951 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1952 + (u8c & 0x3ff);
1953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (u16c < 0xd800 || u16c > 0xdbff)
1956 {
1957 if (can_retry)
1958 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001959 if (conv_error == 0)
1960 conv_error = readfile_linenr(linecnt,
1961 ptr, p);
1962 if (bad_char_behavior == BAD_DROP)
1963 continue;
1964 if (bad_char_behavior != BAD_KEEP)
1965 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 }
1969 else if (fio_flags & FIO_UCS4)
1970 {
1971 if (fio_flags & FIO_ENDIAN_L)
1972 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001973 u8c = (unsigned)*--p << 24;
1974 u8c += (unsigned)*--p << 16;
1975 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 u8c += *--p;
1977 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001981 u8c += (unsigned)*--p << 8;
1982 u8c += (unsigned)*--p << 16;
1983 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001986 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 if (*--p < 0x80)
1989 u8c = *p;
1990 else
1991 {
1992 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001993 p -= len;
1994 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (len == 0)
1996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // Not a valid UTF-8 character, retry with
1998 // another fenc when possible, otherwise just
1999 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (can_retry)
2001 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002002 if (conv_error == 0)
2003 conv_error = readfile_linenr(linecnt,
2004 ptr, p);
2005 if (bad_char_behavior == BAD_DROP)
2006 continue;
2007 if (bad_char_behavior != BAD_KEEP)
2008 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014 dest -= utf_char2len(u8c);
2015 (void)utf_char2bytes(u8c, dest);
2016 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
2019 --dest;
2020 if (u8c >= 0x100)
2021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002022 // character doesn't fit in latin1, retry with
2023 // another fenc when possible, otherwise just
2024 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002025 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002027 if (conv_error == 0)
2028 conv_error = readfile_linenr(linecnt, ptr, p);
2029 if (bad_char_behavior == BAD_DROP)
2030 ++dest;
2031 else if (bad_char_behavior == BAD_KEEP)
2032 *dest = u8c;
2033 else if (eap != NULL && eap->bad_char != 0)
2034 *dest = bad_char_behavior;
2035 else
2036 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else
2039 *dest = u8c;
2040 }
2041 }
2042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 line_start = dest - linerest;
2045 mch_memmove(line_start, buffer, (size_t)linerest);
2046 size = (long)((ptr + real_size) - dest);
2047 ptr = dest;
2048 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002049 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002051 int incomplete_tail = FALSE;
2052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002053 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002054 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002056 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002057 int l;
2058
2059 if (todo <= 0)
2060 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (*p >= 0x80)
2062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // A length of 1 means it's an illegal byte. Accept
2064 // an incomplete character at the end though, the next
2065 // read() will get the next bytes, we'll check it
2066 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002067 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002068 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // Avoid retrying with a different encoding when
2071 // a truncated file is more likely, or attempting
2072 // to read the rest of an incomplete sequence when
2073 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002074 if (p > ptr || filesize > 0)
2075 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002076 // Incomplete byte sequence, move it to conv_rest[]
2077 // and try to read the rest of it, unless we've
2078 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002079 if (p > ptr)
2080 {
2081 conv_restlen = todo;
2082 mch_memmove(conv_rest, p, conv_restlen);
2083 size -= conv_restlen;
2084 break;
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002087 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 // Illegal byte. If we can try another encoding
2090 // do that, unless at EOF where a truncated
2091 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002092 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002093 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002094#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002095 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002096 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2097 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002098#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002100 if (conv_error == 0 && illegal_byte == 0)
2101 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002104 if (bad_char_behavior == BAD_DROP)
2105 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002106 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002107 --p;
2108 --size;
2109 }
2110 else if (bad_char_behavior != BAD_KEEP)
2111 *p = bad_char_behavior;
2112 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002113 else
2114 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002117 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002119 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002121 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002122#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002123 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002125 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002127#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002129 advance_fenc = TRUE;
2130 file_rewind = TRUE;
2131 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 filesize += size;
2137
2138 /*
2139 * when reading the first part of a file: guess EOL type
2140 */
2141 if (fileformat == EOL_UNKNOWN)
2142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002143 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (try_dos || try_unix)
2145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002147 if (try_mac)
2148 try_mac = 1;
2149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 for (p = ptr; p < ptr + size; ++p)
2151 {
2152 if (*p == NL)
2153 {
2154 if (!try_unix
2155 || (try_dos && p > ptr && p[-1] == CAR))
2156 fileformat = EOL_DOS;
2157 else
2158 fileformat = EOL_UNIX;
2159 break;
2160 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002161 else if (*p == CAR && try_mac)
2162 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002165 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (fileformat == EOL_UNIX && try_mac)
2167 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002168 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 try_mac = 1;
2170 try_unix = 1;
2171 for (; p >= ptr && *p != CAR; p--)
2172 ;
2173 if (p >= ptr)
2174 {
2175 for (p = ptr; p < ptr + size; ++p)
2176 {
2177 if (*p == NL)
2178 try_unix++;
2179 else if (*p == CAR)
2180 try_mac++;
2181 }
2182 if (try_mac > try_unix)
2183 fileformat = EOL_MAC;
2184 }
2185 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002186 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002187 // Looking for CR but found no end-of-line markers at
2188 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002189 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
2191
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002192 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (fileformat == EOL_UNKNOWN && try_mac)
2194 fileformat = EOL_MAC;
2195
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (fileformat == EOL_UNKNOWN)
2198 fileformat = default_fileformat();
2199
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 set_fileformat(fileformat, OPT_LOCAL);
2203 }
2204 }
2205
2206 /*
2207 * This loop is executed once for every character read.
2208 * Keep it fast!
2209 */
2210 if (fileformat == EOL_MAC)
2211 {
2212 --ptr;
2213 while (++ptr, --size >= 0)
2214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002215 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if ((c = *ptr) != NUL && c != CAR && c != NL)
2217 continue;
2218 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 else
2223 {
2224 if (skip_count == 0)
2225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 len = (colnr_T) (ptr - line_start + 1);
2228 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2229 {
2230 error = TRUE;
2231 break;
2232 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002233#ifdef FEAT_PERSISTENT_UNDO
2234 if (read_undo_file)
2235 sha256_update(&sha_ctx, line_start, len);
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ++lnum;
2238 if (--read_count == 0)
2239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 error = TRUE; // break loop
2241 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 break;
2243 }
2244 }
2245 else
2246 --skip_count;
2247 line_start = ptr + 1;
2248 }
2249 }
2250 }
2251 else
2252 {
2253 --ptr;
2254 while (++ptr, --size >= 0)
2255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002256 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 continue;
2258 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 else
2261 {
2262 if (skip_count == 0)
2263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002264 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 len = (colnr_T)(ptr - line_start + 1);
2266 if (fileformat == EOL_DOS)
2267 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002268 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 ptr[-1] = NUL;
2272 --len;
2273 }
2274 /*
2275 * Reading in Dos format, but no CR-LF found!
2276 * When 'fileformats' includes "unix", delete all
2277 * the lines read so far and start all over again.
2278 * Otherwise give an error message later.
2279 */
2280 else if (ff_error != EOL_DOS)
2281 {
2282 if ( try_unix
2283 && !read_stdin
2284 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002285 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2286 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002289 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 set_fileformat(EOL_UNIX, OPT_LOCAL);
2291 file_rewind = TRUE;
2292 keep_fileformat = TRUE;
2293 goto retry;
2294 }
2295 ff_error = EOL_DOS;
2296 }
2297 }
2298 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2299 {
2300 error = TRUE;
2301 break;
2302 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002303#ifdef FEAT_PERSISTENT_UNDO
2304 if (read_undo_file)
2305 sha256_update(&sha_ctx, line_start, len);
2306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 ++lnum;
2308 if (--read_count == 0)
2309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 error = TRUE; // break loop
2311 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 break;
2313 }
2314 }
2315 else
2316 --skip_count;
2317 line_start = ptr + 1;
2318 }
2319 }
2320 }
2321 linerest = (long)(ptr - line_start);
2322 ui_breakcheck();
2323 }
2324
2325failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (error && read_count == 0)
2328 error = FALSE;
2329
K.Takata3af98212022-11-01 20:36:19 +00002330 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2331 // In old days the file length was in sector count and the CTRL-Z the
2332 // marker where the file really ended. Assuming we write it to a file
2333 // system that keeps file length properly the CTRL-Z should be dropped.
2334 // Set the 'endoffile' option so the user can decide what to write later.
2335 // In Unix format the CTRL-Z is just another character.
2336 if (linerest != 0
2337 && !curbuf->b_p_bin
2338 && fileformat == EOL_DOS
2339 && ptr[-1] == Ctrl_Z)
2340 {
2341 ptr--;
2342 linerest--;
2343 if (set_options)
2344 curbuf->b_p_eof = TRUE;
2345 }
2346
2347 // If we get EOF in the middle of a line, note the fact by resetting
2348 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (!error
2350 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002351 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002353 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002354 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 curbuf->b_p_eol = FALSE;
2356 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002357 len = (colnr_T)(ptr - line_start + 1);
2358 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 error = TRUE;
2360 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002361 {
2362#ifdef FEAT_PERSISTENT_UNDO
2363 if (read_undo_file)
2364 sha256_update(&sha_ctx, line_start, len);
2365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002370 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002371 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002374 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002375 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002376 crypt_free_state(curbuf->b_cryptstate);
2377 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002378 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002379 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2380 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 // Don't set cryptkey to NULL, it's used below as a flag that
2382 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#endif
2384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002385 // If editing a new file: set 'fenc' for the current buffer.
2386 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002387 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002389 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (fenc_alloced)
2391 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002392#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#endif
2396
2397 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002398 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002399#ifdef HAVE_FD_CLOEXEC
2400 else
2401 {
2402 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002403
Bram Moolenaarf05da212009-11-17 16:13:15 +00002404 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002405 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002406 }
2407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 vim_free(buffer);
2409
2410#ifdef HAVE_DUP
2411 if (read_stdin)
2412 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002413 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002415 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417#endif
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (tmpname != NULL)
2420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002421 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 vim_free(tmpname);
2423 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002424 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /*
2427 * In recovery mode everything but autocommands is skipped.
2428 */
2429 if (!recoverymode)
2430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2433 {
2434#ifdef FEAT_NETBEANS_INTG
2435 netbeansFireChanges = 0;
2436#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002437 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#ifdef FEAT_NETBEANS_INTG
2439 netbeansFireChanges = 1;
2440#endif
2441 --linecnt;
2442 }
2443 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2444 if (filesize == 0)
2445 linecnt = 0;
2446 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002447 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002448 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002449#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002450 // After reading the text into the buffer the diff info needs to
2451 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002452 diff_invalidate(curbuf);
2453#endif
2454#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002455 // All folds in the window are invalid now. Mark them for update
2456 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002457 foldUpdateAll(curwin);
2458#endif
2459 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002460 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 appended_lines_mark(from, linecnt);
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#ifndef ALWAYS_USE_GUI
2464 /*
2465 * If we were reading from the same terminal as where messages go,
2466 * the screen will have been messed up.
2467 * Switch on raw mode now and clear the screen.
2468 */
2469 if (read_stdin)
2470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002471 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 starttermcap();
2473 screenclear();
2474 }
2475#endif
2476
2477 if (got_int)
2478 {
2479 if (!(flags & READ_DUMMY))
2480 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002481 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002483 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
2485 msg_scroll = msg_save;
2486#ifdef FEAT_VIMINFO
2487 check_marks_read();
2488#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002489 retval = OK; // an interrupt isn't really an error
2490 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492
2493 if (!filtering && !(flags & READ_DUMMY))
2494 {
John Marriott14ede182024-12-29 16:14:19 +01002495 int buflen;
2496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002497 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 c = FALSE;
2499
John Marriott14ede182024-12-29 16:14:19 +01002500 buflen = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002502 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
John Marriott14ede182024-12-29 16:14:19 +01002504 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2505 _("[fifo]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 c = TRUE;
2507 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002508 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
John Marriott14ede182024-12-29 16:14:19 +01002510 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2511 _("[socket]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 c = TRUE;
2513 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002514# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002515 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002516 {
John Marriott14ede182024-12-29 16:14:19 +01002517 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2518 _("[character special]"));
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002519 c = TRUE;
2520 }
2521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522#endif
2523 if (curbuf->b_p_ro)
2524 {
John Marriott14ede182024-12-29 16:14:19 +01002525 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2526 "%s", shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 c = TRUE;
2528 }
2529 if (read_no_eol_lnum)
2530 {
2531 msg_add_eol();
2532 c = TRUE;
2533 }
2534 if (ff_error == EOL_DOS)
2535 {
John Marriott14ede182024-12-29 16:14:19 +01002536 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
John Marriottdf4b3ca2024-12-30 09:48:06 +01002537 _("[CR missing]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 c = TRUE;
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (split)
2541 {
John Marriott14ede182024-12-29 16:14:19 +01002542 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2543 _("[long lines split]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 c = TRUE;
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (notconverted)
2547 {
John Marriott14ede182024-12-29 16:14:19 +01002548 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2549 _("[NOT converted]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 c = TRUE;
2551 }
2552 else if (converted)
2553 {
John Marriott14ede182024-12-29 16:14:19 +01002554 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2555 _("[converted]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 c = TRUE;
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558#ifdef FEAT_CRYPT
2559 if (cryptkey != NULL)
2560 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002561 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 c = TRUE;
2563 }
2564#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002565 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
John Marriott14ede182024-12-29 16:14:19 +01002567 vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2568 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 c = TRUE;
2570 }
2571 else if (illegal_byte > 0)
2572 {
John Marriott14ede182024-12-29 16:14:19 +01002573 vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2574 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 c = TRUE;
2576 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002577 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
John Marriott14ede182024-12-29 16:14:19 +01002579 vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2580 _("[READ ERRORS]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 c = TRUE;
2582 }
2583 if (msg_add_fileformat(fileformat))
2584 c = TRUE;
2585#ifdef FEAT_CRYPT
2586 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002587 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002588 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 else
2590#endif
2591 msg_add_lines(c, (long)linecnt, filesize);
2592
Bram Moolenaard23a8232018-02-10 18:45:26 +01002593 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 msg_scrolled_ign = TRUE;
2595#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002596 // Don't show the message when reading stdin, it would end up in a
2597 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (read_stdin || read_buffer)
2599 p = msg_may_trunc(FALSE, IObuff);
2600 else
2601#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002602 {
2603 if (msg_col > 0)
2604 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002605 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002608 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002609 // Need to repeat the message after redrawing when:
2610 // - When reading from stdin (the screen will be cleared next).
2611 // - When restart_edit is set (otherwise there will be a delay
2612 // before redrawing).
2613 // - When the screen was scrolled but there is no wait-return
2614 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002615 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 msg_scrolled_ign = FALSE;
2617 }
2618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002619 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002621 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002622 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 curbuf->b_p_ro = TRUE;
2624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002625 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /*
2628 * In Ex mode: cursor at last new line.
2629 * Otherwise: cursor at first new line.
2630 */
2631 if (exmode_active)
2632 curwin->w_cursor.lnum = from + linecnt;
2633 else
2634 curwin->w_cursor.lnum = from + 1;
2635 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002636 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaare1004402020-10-24 20:49:43 +02002638 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002639 {
2640 // Set '[ and '] marks to the newly read lines.
2641 curbuf->b_op_start.lnum = from + 1;
2642 curbuf->b_op_start.col = 0;
2643 curbuf->b_op_end.lnum = from + linecnt;
2644 curbuf->b_op_end.col = 0;
2645 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002646
Bram Moolenaar4f974752019-02-17 17:44:42 +01002647#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002648 /*
2649 * Work around a weird problem: When a file has two links (only
2650 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002651 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002652 * It's correct again after reading the file, thus reset the timestamp
2653 * here.
2654 */
2655 if (newfile && !read_stdin && !read_buffer
2656 && mch_stat((char *)fname, &st) >= 0)
2657 {
2658 buf_store_time(curbuf, &st, fname);
2659 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002660 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002661 }
2662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 msg_scroll = msg_save;
2665
2666#ifdef FEAT_VIMINFO
2667 /*
2668 * Get the marks before executing autocommands, so they can be used there.
2669 */
2670 check_marks_read();
2671#endif
2672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002674 * We remember if the last line of the read didn't have
2675 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2676 * or writing the read again with 'binary' on. The latter is required
2677 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002679 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002681 // When reloading a buffer put the cursor at the first line that is
2682 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002683 if (flags & READ_KEEP_UNDO)
2684 u_find_first_changed();
2685
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002686#ifdef FEAT_PERSISTENT_UNDO
2687 /*
2688 * When opening a new file locate undo info and read it.
2689 */
2690 if (read_undo_file)
2691 {
2692 char_u hash[UNDO_HASH_SIZE];
2693
2694 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002695 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002696 }
2697#endif
2698
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002699 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 int m = msg_scroll;
2702 int n = msg_scrolled;
2703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002704 // Save the fileformat now, otherwise the buffer will be considered
2705 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002706 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 save_file_ff(curbuf);
2708
2709 /*
2710 * The output from the autocommands should not overwrite anything and
2711 * should not be overwritten: Set msg_scroll, restore its value if no
2712 * output was done.
2713 */
2714 msg_scroll = TRUE;
2715 if (filtering)
2716 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2717 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002718 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2721 FALSE, curbuf, eap);
zeertzjq5bf6c212024-03-31 18:41:27 +02002722 if (!curbuf->b_au_did_filetype && *curbuf->b_p_ft != NUL)
Bram Moolenaarc3691332016-04-20 12:49:49 +02002723 /*
2724 * EVENT_FILETYPE was not triggered but the buffer already has a
2725 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2726 */
2727 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2728 TRUE, curbuf);
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else
2731 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2732 FALSE, NULL, eap);
2733 if (msg_scrolled == n)
2734 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002735# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002736 if (aborting()) // autocmds may abort script processing
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002737 goto theend;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002741 if (!(recoverymode && error))
2742 retval = OK;
2743
2744theend:
2745 if (curbuf->b_ml.ml_mfp != NULL
2746 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
2747 // OK to sync the swap file now
2748 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
2749
2750 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751}
2752
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002753#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002754/*
2755 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2756 * which is the name of files used for process substitution output by
2757 * some shells on some operating systems, e.g., bash on SunOS.
2758 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2759 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002760 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002761is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002762{
2763 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2764 && VIM_ISDIGIT(fname[8])
2765 && *skipdigits(fname + 9) == NUL
2766 && (fname[9] != NUL
2767 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2768}
2769#endif
2770
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002771/*
2772 * From the current line count and characters read after that, estimate the
2773 * line number where we are now.
2774 * Used for error messages that include a line number.
2775 */
2776 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002777readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002778 linenr_T linecnt, // line count before reading more bytes
2779 char_u *p, // start of more bytes read
2780 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002781{
2782 char_u *s;
2783 linenr_T lnum;
2784
2785 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2786 for (s = p; s < endp; ++s)
2787 if (*s == '\n')
2788 ++lnum;
2789 return lnum;
2790}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792/*
Rob Pilling8196e942022-02-11 15:12:10 +00002793 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002794 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 * Returns OK or FAIL.
2796 */
2797 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002798prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar13505972019-01-24 15:04:48 +01002800 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (eap->cmd == NULL)
2802 return FAIL;
2803
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002804 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2805 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002806 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002807 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002808
2809 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002810 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002811 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 return OK;
2813}
2814
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002815/*
2816 * Set default or forced 'fileformat' and 'binary'.
2817 */
2818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002819set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002820{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002821 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002822 if (set_options)
2823 {
2824 if (eap != NULL && eap->force_ff != 0)
2825 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2826 else if (*p_ffs != NUL)
2827 set_fileformat(default_fileformat(), OPT_LOCAL);
2828 }
2829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002830 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002831 if (eap != NULL && eap->force_bin != 0)
2832 {
2833 int oldval = curbuf->b_p_bin;
2834
2835 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2836 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2837 }
2838}
2839
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002840/*
2841 * Set forced 'fileencoding'.
2842 */
2843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002844set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002845{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002846 if (eap->force_enc == 0)
2847 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002848
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002849 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2850
2851 if (fenc != NULL)
2852 set_string_option_direct((char_u *)"fenc", -1,
2853 fenc, OPT_FREE|OPT_LOCAL, 0);
2854 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002855}
2856
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857/*
2858 * Find next fileencoding to use from 'fileencodings'.
2859 * "pp" points to fenc_next. It's advanced to the next item.
2860 * When there are no more items, an empty string is returned and *pp is set to
2861 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002862 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2863 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 */
2865 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002866next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 char_u *p;
2869 char_u *r;
2870
Bram Moolenaarf077db22019-08-13 00:18:24 +02002871 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (**pp == NUL)
2873 {
2874 *pp = NULL;
2875 return (char_u *)"";
2876 }
2877 p = vim_strchr(*pp, ',');
2878 if (p == NULL)
2879 {
2880 r = enc_canonize(*pp);
2881 *pp += STRLEN(*pp);
2882 }
2883 else
2884 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002885 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 *pp = p + 1;
2887 if (r != NULL)
2888 {
2889 p = enc_canonize(r);
2890 vim_free(r);
2891 r = p;
2892 }
2893 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002894 if (r != NULL)
2895 *alloced = TRUE;
2896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002898 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 r = (char_u *)"";
2900 *pp = NULL;
2901 }
2902 return r;
2903}
2904
Bram Moolenaar13505972019-01-24 15:04:48 +01002905#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906/*
2907 * Convert a file with the 'charconvert' expression.
2908 * This closes the file which is to be read, converts it and opens the
2909 * resulting file for reading.
2910 * Returns name of the resulting converted file (the caller should delete it
2911 * after reading it).
2912 * Returns NULL if the conversion failed ("*fdp" is not set) .
2913 */
2914 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002915readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002916 char_u *fname, // name of input file
2917 char_u *fenc, // converted from
2918 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
2920 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002921 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002923 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002925 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 else
2927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002928 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 *fdp = -1;
2930 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2931 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002932 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2934 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002935 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
2937
2938 if (errmsg != NULL)
2939 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002940 // Don't use emsg(), it breaks mappings, the retry with
2941 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002942 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if (tmpname != NULL)
2944 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002946 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
2949
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002950 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (*fdp < 0)
2952 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2953
2954 return tmpname;
2955}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#endif
2957
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002958#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002960 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2962 * *filesizep are updated.
2963 * Return the (new) encryption key, NULL for no encryption.
2964 */
2965 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002966check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002967 char_u *cryptkey, // previous encryption key or NULL
2968 char_u *ptr, // pointer to read bytes
2969 long *sizep, // length of read bytes
2970 off_T *filesizep, // nr of bytes used from file
2971 int newfile, // editing a new buffer
2972 char_u *fname, // file name to display
2973 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002975 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002976 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002977
2978 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002980 // Mark the buffer as read-only until the decryption has taken place.
2981 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002982 curbuf->b_p_ro = TRUE;
2983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002984 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002985 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002986 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 if (*curbuf->b_p_key)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 cryptkey = curbuf->b_p_key;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002991 crypt_check_swapfile_curbuf();
2992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 else
2994 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002995 // When newfile is TRUE, store the typed key in the 'key'
2996 // option and don't free it. bf needs hash of the key saved.
2997 // Don't ask for the key again when first time Enter was hit.
2998 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002999 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02003000 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01003001 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003002 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02003003 *did_ask = TRUE;
3004
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003005 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (cryptkey != NULL && *cryptkey == NUL)
3007 {
3008 if (cryptkey != curbuf->b_p_key)
3009 vim_free(cryptkey);
3010 cryptkey = NULL;
3011 }
3012 }
3013 }
3014
3015 if (cryptkey != NULL)
3016 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003017 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02003018
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003019 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02003020 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003021 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02003022 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02003023
3024 curbuf->b_cryptstate = crypt_create_from_header(
3025 method, cryptkey, ptr);
3026 crypt_set_cm_option(curbuf, method);
3027
3028 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003029 *filesizep += header_len;
3030 *sizep -= header_len;
3031 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
3032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003033 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02003034 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003037 // When starting to edit a new file which does not have encryption, clear
3038 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02003039 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01003040 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 return cryptkey;
3043}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003044#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02003045
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003047 * Return TRUE if a file appears to be read-only from the file permissions.
3048 */
3049 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003051 char_u *fname, // full path to file
3052 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003053{
3054#ifndef USE_MCH_ACCESS
3055 int fd = 0;
3056#endif
3057
3058 return (
3059#ifdef USE_MCH_ACCESS
3060# ifdef UNIX
3061 (perm & 0222) == 0 ||
3062# endif
3063 mch_access((char *)fname, W_OK)
3064#else
3065 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3066 ? TRUE : (close(fd), FALSE)
3067#endif
3068 );
3069}
3070
Bram Moolenaara7870192019-02-14 12:56:36 +01003071#if defined(HAVE_FSYNC) || defined(PROTO)
3072/*
3073 * Call fsync() with Mac-specific exception.
3074 * Return fsync() result: zero for success.
3075 */
3076 int
3077vim_fsync(int fd)
3078{
3079 int r;
3080
3081# ifdef MACOS_X
3082 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003083 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003084# endif
3085 r = fsync(fd);
3086 return r;
3087}
3088#endif
3089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003091 * Set the name of the current buffer. Use when the buffer doesn't have a
3092 * name and a ":r" or ":w" command with a file name is used.
3093 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003094 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003095set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003096{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003097 buf_T *buf = curbuf;
3098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003099 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003100 if (curbuf->b_p_bl)
3101 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3102 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003103#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003104 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003105 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003106#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003107 if (curbuf != buf)
3108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003109 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003110 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003111 return FAIL;
3112 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003113
3114 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3115 curbuf->b_flags |= BF_NOTEDITED;
3116
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003117 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003118 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3119 if (curbuf->b_p_bl)
3120 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003121#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003122 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003123 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003124#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003126 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003127 if (*curbuf->b_p_ft == NUL)
3128 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003129 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003130 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003131 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003132 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003133
3134 return OK;
3135}
3136
3137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Put file name into IObuff with quotes.
3139 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003141msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 if (fname == NULL)
3144 fname = (char_u *)"-stdin-";
3145 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3146 IObuff[0] = '"';
3147 STRCAT(IObuff, "\" ");
3148}
3149
3150/*
3151 * Append message for text mode to IObuff.
3152 * Return TRUE if something appended.
3153 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003154 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003155msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157#ifndef USE_CRNL
3158 if (eol_type == EOL_DOS)
3159 {
3160 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3161 return TRUE;
3162 }
3163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (eol_type == EOL_MAC)
3165 {
3166 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3167 return TRUE;
3168 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003169#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (eol_type == EOL_UNIX)
3171 {
3172 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3173 return TRUE;
3174 }
3175#endif
3176 return FALSE;
3177}
3178
3179/*
3180 * Append line and character count to IObuff.
3181 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003183msg_add_lines(
3184 int insert_space,
3185 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003186 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
John Marriott14ede182024-12-29 16:14:19 +01003188 int len = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (shortmess(SHM_LINES))
John Marriott14ede182024-12-29 16:14:19 +01003191 vim_snprintf((char *)IObuff + len, IOSIZE - (size_t)len,
Emir SARI81f98152025-06-26 20:38:16 +02003192 // l10n: L as in line, B as in byte
3193 _("%s%ldL, %lldB"), insert_space ? " " : "", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
John Marriott14ede182024-12-29 16:14:19 +01003196 len += vim_snprintf((char *)IObuff + len, IOSIZE - (size_t)len,
3197 NGETTEXT("%s%ld line, ", "%s%ld lines, ", lnum), insert_space ? " " : "", lnum);
3198 vim_snprintf((char *)IObuff + len, IOSIZE - (size_t)len,
3199 NGETTEXT("%lld byte", "%lld bytes", nchars), (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201}
3202
3203/*
3204 * Append message for missing line separator to IObuff.
3205 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003207msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
3209 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3210}
3211
Bram Moolenaar473952e2019-09-28 16:30:04 +02003212 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003213time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
ichizokdef69df2021-10-15 17:23:12 +01003215 return
3216#ifdef ST_MTIM_NSEC
3217 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218#endif
ichizokdef69df2021-10-15 17:23:12 +01003219#if defined(__linux__) || defined(MSWIN)
3220 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3221 // the seconds. Since the roundoff is done when flushing the inode, the
3222 // time may change unexpectedly by one second!!!
3223 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3224#else
3225 (long)st->st_mtime != mtime
3226#endif
3227 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
3229
3230/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003231 * Return TRUE if file encoding "fenc" requires conversion from or to
3232 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003234 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003235need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003237 int same_encoding;
3238 int enc_flags;
3239 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003241 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003242 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003243 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003244 fenc_flags = 0;
3245 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003246 else
3247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003248 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3249 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003250 enc_flags = get_fio_flags(p_enc);
3251 fenc_flags = get_fio_flags(fenc);
3252 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3253 }
3254 if (same_encoding)
3255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003256 // Specified encoding matches with 'encoding'. This requires
3257 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003258 return enc_unicode != 0;
3259 }
3260
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003261 // Encodings differ. However, conversion is not needed when 'enc' is any
3262 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003263 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265
3266/*
3267 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3268 * internal conversion.
3269 * if "ptr" is an empty string, use 'encoding'.
3270 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003271 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003272get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 int prop;
3275
3276 if (*ptr == NUL)
3277 ptr = p_enc;
3278
3279 prop = enc_canon_props(ptr);
3280 if (prop & ENC_UNICODE)
3281 {
3282 if (prop & ENC_2BYTE)
3283 {
3284 if (prop & ENC_ENDIAN_L)
3285 return FIO_UCS2 | FIO_ENDIAN_L;
3286 return FIO_UCS2;
3287 }
3288 if (prop & ENC_4BYTE)
3289 {
3290 if (prop & ENC_ENDIAN_L)
3291 return FIO_UCS4 | FIO_ENDIAN_L;
3292 return FIO_UCS4;
3293 }
3294 if (prop & ENC_2WORD)
3295 {
3296 if (prop & ENC_ENDIAN_L)
3297 return FIO_UTF16 | FIO_ENDIAN_L;
3298 return FIO_UTF16;
3299 }
3300 return FIO_UTF8;
3301 }
3302 if (prop & ENC_LATIN1)
3303 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003304 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 return 0;
3306}
3307
Bram Moolenaar473952e2019-09-28 16:30:04 +02003308#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309/*
3310 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3311 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3312 * Used for conversion between 'encoding' and 'fileencoding'.
3313 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003315get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 int cp;
3318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003319 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (!enc_utf8 && enc_codepage <= 0)
3321 return 0;
3322
3323 cp = encname2codepage(ptr);
3324 if (cp == 0)
3325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003326# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (STRCMP(ptr, "utf-8") == 0)
3328 cp = CP_UTF8;
3329 else
3330# endif
3331 return 0;
3332 }
3333 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3334}
3335#endif
3336
Bram Moolenaar473952e2019-09-28 16:30:04 +02003337#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3340 * needed for the internal conversion to/from utf-8 or latin1.
3341 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003342 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003343get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3346 && (enc_canon_props(ptr) & ENC_MACROMAN))
3347 return FIO_MACROMAN;
3348 return 0;
3349}
3350#endif
3351
3352/*
3353 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3354 * "size" must be at least 2.
3355 * Return the name of the encoding and set "*lenp" to the length.
3356 * Returns NULL when no BOM found.
3357 */
3358 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359check_for_bom(
3360 char_u *p,
3361 long size,
3362 int *lenp,
3363 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 char *name = NULL;
3366 int len = 2;
3367
3368 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003369 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003371 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 len = 3;
3373 }
3374 else if (p[0] == 0xff && p[1] == 0xfe)
3375 {
3376 if (size >= 4 && p[2] == 0 && p[3] == 0
3377 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003379 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 len = 4;
3381 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003382 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003383 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003384 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003385 // utf-16le is preferred, it also works for ucs-2le text
3386 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388 else if (p[0] == 0xfe && p[1] == 0xff
3389 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003391 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003392 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003393 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003394 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003395 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3398 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3399 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003400 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 len = 4;
3402 }
3403
3404 *lenp = len;
3405 return (char_u *)name;
3406}
3407
3408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 * Try to find a shortname by comparing the fullname with the current
3410 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003411 * Returns "full_path" or pointer into "full_path" if shortened.
3412 */
3413 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003414shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003415{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003416 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003417 char_u *p = full_path;
3418
Bram Moolenaard9462e32011-04-11 21:35:11 +02003419 dirname = alloc(MAXPATHL);
3420 if (dirname == NULL)
3421 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003422 if (mch_dirname(dirname, MAXPATHL) == OK)
3423 {
3424 p = shorten_fname(full_path, dirname);
3425 if (p == NULL || *p == NUL)
3426 p = full_path;
3427 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003428 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003429 return p;
3430}
3431
3432/*
3433 * Try to find a shortname by comparing the fullname with the current
3434 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * Returns NULL if not shorter name possible, pointer into "full_path"
3436 * otherwise.
3437 */
3438 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003439shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 int len;
3442 char_u *p;
3443
3444 if (full_path == NULL)
3445 return NULL;
3446 len = (int)STRLEN(dir_name);
3447 if (fnamencmp(dir_name, full_path, len) == 0)
3448 {
3449 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003450#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003452 * MS-Windows: when a file is in the root directory, dir_name will end
3453 * in a slash, since C: by itself does not define a specific dir. In
3454 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 */
3456 if (!((len > 2) && (*(p - 2) == ':')))
3457#endif
3458 {
3459 if (vim_ispathsep(*p))
3460 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003461#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 else
3463 p = NULL;
3464#endif
3465 }
3466 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003467#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 /*
3469 * When using a file in the current drive, remove the drive name:
3470 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3471 * a floppy from "A:\dir" to "B:\dir".
3472 */
3473 else if (len > 3
3474 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3475 && full_path[1] == ':'
3476 && vim_ispathsep(full_path[2]))
3477 p = full_path + 2;
3478#endif
3479 else
3480 p = NULL;
3481 return p;
3482}
3483
3484/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003485 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 * When "force" is TRUE: Use full path from now on for files currently being
3487 * edited, both for file name and swap file name. Try to shorten the file
3488 * names a bit, if safe to do so.
3489 * When "force" is FALSE: Only try to shorten absolute file names.
3490 * For buffers that have buftype "nofile" or "scratch": never change the file
3491 * name.
3492 */
3493 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003494shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3495{
3496 char_u *p;
3497
3498 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003499 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003500 && !path_with_url(buf->b_fname)
3501 && (force
3502 || buf->b_sfname == NULL
3503 || mch_isFullName(buf->b_sfname)))
3504 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003505 if (buf->b_sfname != buf->b_ffname)
3506 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003507 p = shorten_fname(buf->b_ffname, dirname);
3508 if (p != NULL)
3509 {
3510 buf->b_sfname = vim_strsave(p);
3511 buf->b_fname = buf->b_sfname;
3512 }
3513 if (p == NULL || buf->b_fname == NULL)
3514 buf->b_fname = buf->b_ffname;
3515 }
3516}
3517
3518/*
3519 * Shorten filenames for all buffers.
3520 */
3521 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003522shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524 char_u dirname[MAXPATHL];
3525 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
3527 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003528 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003530 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003532 // Always make the swap file name a full path, a "nofile" buffer may
3533 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003534 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003537 redraw_tabline = TRUE;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003538#if defined(FEAT_TABPANEL)
3539 redraw_tabpanel = TRUE;
3540#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003541#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003542 popup_update_preview_title();
3543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544}
3545
3546#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3547 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003548 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 || defined(PROTO)
3550/*
3551 * Shorten all filenames in "fnames[count]" by current directory.
3552 */
3553 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003554shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
3556 int i;
3557 char_u dirname[MAXPATHL];
3558 char_u *p;
3559
3560 if (fnames == NULL || count < 1)
3561 return;
3562 mch_dirname(dirname, sizeof(dirname));
3563 for (i = 0; i < count; ++i)
3564 {
3565 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003567 // shorten_fname() returns pointer in given "fnames[i]". If free
3568 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3569 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 p = vim_strsave(p);
3571 vim_free(fnames[i]);
3572 fnames[i] = p;
3573 }
3574 }
3575}
3576#endif
3577
3578/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003579 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 * fo_o_h.ext for MSDOS or when shortname option set.
3581 *
3582 * Assumed that fname is a valid name found in the filesystem we assure that
3583 * the return value is a different name and ends in 'ext'.
3584 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3585 * characters otherwise.
3586 * Space for the returned name is allocated, must be freed later.
3587 * Returns NULL when out of memory.
3588 */
3589 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003590modname(
3591 char_u *fname,
3592 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003593 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003595 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 fname, ext, prepend_dot);
3597}
3598
3599 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003600buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003601 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003602 char_u *fname,
3603 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003604 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
3606 char_u *retval;
3607 char_u *s;
3608 char_u *e;
3609 char_u *ptr;
John Marriott14ede182024-12-29 16:14:19 +01003610 size_t ptrlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int fnamelen, extlen;
3612
3613 extlen = (int)STRLEN(ext);
3614
3615 /*
3616 * If there is no file name we must get the name of the current directory
3617 * (we need the full path in case :cd is used).
3618 */
3619 if (fname == NULL || *fname == NUL)
3620 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003621 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (retval == NULL)
3623 return NULL;
3624 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3625 (fnamelen = (int)STRLEN(retval)) == 0)
3626 {
3627 vim_free(retval);
3628 return NULL;
3629 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003630 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 retval[fnamelen++] = PATHSEP;
3633 retval[fnamelen] = NUL;
3634 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003635 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
3638 {
3639 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003640 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (retval == NULL)
3642 return NULL;
3643 STRCPY(retval, fname);
3644#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003645 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#endif
3647 }
3648
3649 /*
3650 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3651 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3652 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3653 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3654 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003655 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003657 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003658 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003661 {
3662 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003667 // the file name has at most BASENAMELEN characters.
John Marriott14ede182024-12-29 16:14:19 +01003668 ptrlen = (size_t)(fnamelen - (ptr - retval));
3669 if (ptrlen > (unsigned)BASENAMELEN)
3670 {
3671 ptrlen = BASENAMELEN;
3672 ptr[ptrlen] = NUL;
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
John Marriott14ede182024-12-29 16:14:19 +01003675 s = ptr + ptrlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 /*
3678 * For 8.3 file names we may have to reduce the length.
3679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 /*
3683 * If there is no file name, or the file name ends in '/', and the
3684 * extension starts with '.', put a '_' before the dot, because just
3685 * ".ext" is invalid.
3686 */
3687 if (fname == NULL || *fname == NUL
3688 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 *s++ = '_';
3692 }
3693 /*
3694 * If the extension starts with '.', truncate the base name at 8
3695 * characters
3696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003699 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
3701 s = ptr + 8;
3702 *s = '\0';
3703 }
3704 }
3705 /*
3706 * If the extension doesn't start with '.', and the file name
3707 * doesn't have an extension yet, append a '.'
3708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else if ((e = vim_strchr(ptr, '.')) == NULL)
3710 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 /*
3712 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003713 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 */
John Marriott14ede182024-12-29 16:14:19 +01003715 else if ((int)(ptrlen - (e - retval)) + extlen > 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 s = e + 4 - extlen;
3717 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003718#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 /*
3720 * If there is no file name, and the extension starts with '.', put a
3721 * '_' before the dot, because just ".ext" may be invalid if it's on a
3722 * FAT partition, and on HPFS it doesn't matter.
3723 */
3724 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3725 *s++ = '_';
3726#endif
3727
3728 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003729 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * ext can start with '.' and cannot exceed 3 more characters.
3731 */
3732 STRCPY(s, ext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 /*
3734 * Prepend the dot.
3735 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003736 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
John Marriott14ede182024-12-29 16:14:19 +01003738 mch_memmove(e + 1, e, (size_t)(((fnamelen + extlen) - (e - retval)) + 1)); // +1 for NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 /*
3743 * Check that, after appending the extension, the file name is really
3744 * different.
3745 */
3746 if (fname != NULL && STRCMP(fname, retval) == 0)
3747 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003748 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 while (--s >= ptr)
3750 {
3751 if (*s != '_')
3752 {
3753 *s = '_';
3754 break;
3755 }
3756 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003757 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 *ptr = 'v';
3759 }
3760 return retval;
3761}
3762
3763/*
3764 * Like fgets(), but if the file line is too long, it is truncated and the
3765 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003766 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 */
3768 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003769vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 char *eof;
3772#define FGETS_SIZE 200
3773 char tbuf[FGETS_SIZE];
3774
3775 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3778 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003779 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003781 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 do
3783 {
3784 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003785 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3787 }
3788 return (eof == NULL);
3789}
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791/*
3792 * rename() only works if both files are on the same file system, this
3793 * function will (attempts to?) copy the file across if rename fails -- webb
3794 * Return -1 for failure, 0 for success.
3795 */
3796 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003797vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 int n;
Shougo Matsushita60c87432024-06-03 22:59:27 +02003800 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#ifdef AMIGA
3802 BPTR flock;
3803#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003804 stat_T st;
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003805 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
3807 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003808 * When the names are identical, there is nothing to do. When they refer
3809 * to the same file (ignoring case and slash/backslash differences) but
3810 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 */
3812 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003813 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003814 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003815 use_tmp_file = TRUE;
3816 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003817 return 0;
3818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 /*
3821 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3822 */
3823 if (mch_stat((char *)from, &st) < 0)
3824 return -1;
3825
Bram Moolenaar3576da72008-12-30 15:15:57 +00003826#ifdef UNIX
3827 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003828 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003830 // It's possible for the source and destination to be the same file.
3831 // This happens when "from" and "to" differ in case and are on a FAT32
3832 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003833 if (mch_stat((char *)to, &st_to) >= 0
3834 && st.st_dev == st_to.st_dev
3835 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003836 use_tmp_file = TRUE;
3837 }
3838#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003839#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003840 {
3841 BY_HANDLE_FILE_INFORMATION info1, info2;
3842
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003843 // It's possible for the source and destination to be the same file.
3844 // In that case go through a temp file name. This makes rename("foo",
3845 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003846 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3847 && win32_fileinfo(to, &info2) == FILEINFO_OK
3848 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3849 && info1.nFileIndexHigh == info2.nFileIndexHigh
3850 && info1.nFileIndexLow == info2.nFileIndexLow)
3851 use_tmp_file = TRUE;
3852 }
3853#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003854
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003855 if (use_tmp_file)
3856 {
3857 char tempname[MAXPATHL + 1];
3858
3859 /*
3860 * Find a name that doesn't exist and is in the same directory.
3861 * Rename "from" to "tempname" and then rename "tempname" to "to".
3862 */
3863 if (STRLEN(from) >= MAXPATHL - 5)
3864 return -1;
3865 STRCPY(tempname, from);
3866 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003867 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003868 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3869 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003870 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003871 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003872 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003873 if (mch_rename(tempname, (char *)to) == 0)
3874 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003875 // Strange, the second step failed. Try moving the
3876 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003877 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003878 return -1;
3879 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003880 // If it fails for one temp name it will most likely fail
3881 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003882 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003883 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003884 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003885 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003886 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 /*
3889 * Delete the "to" file, this is required on some systems to make the
3890 * mch_rename() work, on other systems it makes sure that we don't have
3891 * two files when the mch_rename() fails.
3892 */
3893
3894#ifdef AMIGA
3895 /*
3896 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3897 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003898 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 * deleting the "from" file (horror!) we lock it during the remove.
3900 *
3901 * When used for making a backup before writing the file: This should not
3902 * happen with ":w", because startscript() should detect this problem and
3903 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3904 * name. This problem does exist with ":w filename", but then the
3905 * original file will be somewhere else so the backup isn't really
3906 * important. If autoscripting is off the rename may fail.
3907 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003908 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#endif
3910 mch_remove(to);
3911#ifdef AMIGA
3912 if (flock)
3913 UnLock(flock);
3914#endif
3915
3916 /*
3917 * First try a normal rename, return if it works.
3918 */
3919 if (mch_rename((char *)from, (char *)to) == 0)
3920 return 0;
3921
3922 /*
3923 * Rename() failed, try copying the file.
3924 */
Shougo Matsushita60c87432024-06-03 22:59:27 +02003925 ret = vim_copyfile(from, to);
3926 if (ret != OK)
3927 return -1;
3928
3929 /*
3930 * Remove copied original file
3931 */
3932 if (mch_stat((char *)from, &st) >= 0)
3933 mch_remove(from);
3934
3935 return 0;
3936}
3937
3938
3939/*
3940 * Create the new file with same permissions as the original.
zeertzjqda090f92024-08-06 19:51:09 +02003941 * Return FAIL for failure, OK for success.
Shougo Matsushita60c87432024-06-03 22:59:27 +02003942 */
3943 int
3944vim_copyfile(char_u *from, char_u *to)
3945{
3946 int fd_in;
3947 int fd_out;
3948 int n;
3949 char *errmsg = NULL;
3950 char *buffer;
3951 long perm;
3952#ifdef HAVE_ACL
3953 vim_acl_T acl; // ACL from original file
3954#endif
3955
3956#ifdef HAVE_READLINK
3957 int ret;
3958 int len;
3959 stat_T st;
3960 char linkbuf[MAXPATHL + 1];
3961
3962 ret = mch_lstat((char *)from, &st);
3963 if (ret >= 0 && S_ISLNK(st.st_mode))
3964 {
zeertzjqda090f92024-08-06 19:51:09 +02003965 ret = -1;
Shougo Matsushita60c87432024-06-03 22:59:27 +02003966
3967 len = readlink((char *)from, linkbuf, MAXPATHL);
3968 if (len > 0)
3969 {
3970 linkbuf[len] = NUL;
3971
3972 // Create link
3973 ret = symlink(linkbuf, (char *)to);
3974 }
3975
3976 return ret == 0 ? OK : FAIL;
3977 }
3978#endif
3979
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003980 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003981#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003982 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003983 acl = mch_get_acl(from);
3984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3986 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003987 {
3988#ifdef HAVE_ACL
3989 mch_free_acl(acl);
3990#endif
Shougo Matsushita60c87432024-06-03 22:59:27 +02003991 return FAIL;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003992 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003994 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003995 fd_out = mch_open((char *)to,
3996 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (fd_out == -1)
3998 {
3999 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00004000#ifdef HAVE_ACL
4001 mch_free_acl(acl);
4002#endif
Shougo Matsushita60c87432024-06-03 22:59:27 +02004003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005
Bram Moolenaar473952e2019-09-28 16:30:04 +02004006 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (buffer == NULL)
4008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00004010 close(fd_in);
4011#ifdef HAVE_ACL
4012 mch_free_acl(acl);
4013#endif
Shougo Matsushita60c87432024-06-03 22:59:27 +02004014 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016
Bram Moolenaar473952e2019-09-28 16:30:04 +02004017 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004018 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00004020 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 break;
4022 }
4023
4024 vim_free(buffer);
4025 close(fd_in);
4026 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004027 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (n < 0)
4029 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00004030 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 to = from;
4032 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004033#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00004034 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00004035#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00004036#ifdef HAVE_ACL
4037 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00004038 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00004039#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02004040#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01004041 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01004042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (errmsg != NULL)
4044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004045 semsg(errmsg, to);
Shougo Matsushita60c87432024-06-03 22:59:27 +02004046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Shougo Matsushita60c87432024-06-03 22:59:27 +02004048 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049}
4050
4051static int already_warned = FALSE;
4052
4053/*
4054 * Check if any not hidden buffer has been changed.
4055 * Postpone the check if there are characters in the stuff buffer, a global
4056 * command is being executed, a mapping is being executed or an autocommand is
4057 * busy.
4058 * Returns TRUE if some message was written (screen should be redrawn and
4059 * cursor positioned).
4060 */
4061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004062check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004063 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
4065 buf_T *buf;
4066 int didit = 0;
4067 int n;
4068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004069 // Don't check timestamps while system() or another low-level function may
4070 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (no_check_timestamps > 0)
4072 return FALSE;
4073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004074 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
4075 // event and we would keep on checking if the file is steadily growing.
4076 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (focus && did_check_timestamps)
4078 {
4079 need_check_timestamps = TRUE;
4080 return FALSE;
4081 }
4082
4083 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004084 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004085 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
4087 {
4088 ++no_wait_return;
4089 did_check_timestamps = TRUE;
4090 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02004091 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004093 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (buf->b_nwindows > 0)
4095 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004096 bufref_T bufref;
4097
4098 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 n = buf_check_timestamp(buf, focus);
4100 if (didit < n)
4101 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004102 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004104 // Autocommands have removed the buffer, start at the
4105 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 buf = firstbuf;
4107 continue;
4108 }
4109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 --no_wait_return;
4112 need_check_timestamps = FALSE;
4113 if (need_wait_return && didit == 2)
4114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004115 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004116 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 out_flush();
4118 }
4119 }
4120 return didit;
4121}
4122
4123/*
4124 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4125 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4126 * empty.
4127 */
4128 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004129move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
4131 buf_T *tbuf = curbuf;
4132 int retval = OK;
4133 linenr_T lnum;
4134 char_u *p;
4135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004136 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 curbuf = tobuf;
4138 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4139 {
John Marriott14ede182024-12-29 16:14:19 +01004140 p = vim_strnsave(ml_get_buf(frombuf, lnum, FALSE), ml_get_buf_len(frombuf, lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4142 {
4143 vim_free(p);
4144 retval = FAIL;
4145 break;
4146 }
4147 vim_free(p);
4148 }
4149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 if (retval != FAIL)
4152 {
4153 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004154 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004155 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // Oops! We could try putting back the saved lines, but that
4158 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 retval = FAIL;
4160 break;
4161 }
4162 }
4163
4164 curbuf = tbuf;
4165 return retval;
4166}
4167
4168/*
4169 * Check if buffer "buf" has been changed.
4170 * Also check if the file for a new buffer unexpectedly appeared.
4171 * return 1 if a changed buffer was found.
4172 * return 2 if a message has been displayed.
4173 * return 0 otherwise.
4174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004176buf_check_timestamp(
4177 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004178 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004180 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int stat_res;
4182 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004184 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004186 enum {
4187 RELOAD_NONE,
4188 RELOAD_NORMAL,
4189 RELOAD_DETECT
4190 } reload = RELOAD_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4192 int can_reload = FALSE;
4193#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004194 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 int orig_mode = buf->b_orig_mode;
4196#ifdef FEAT_GUI
4197 int save_mouse_correct = need_mouse_correct;
4198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004200 int n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004201 bufref_T bufref;
4202
4203 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004205 // If there is no file name, the buffer is not loaded, 'buftype' is
4206 // set, we are in the middle of a save or being called recursively: ignore
4207 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (buf->b_ffname == NULL
4209 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004210 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004213#ifdef FEAT_NETBEANS_INTG
4214 || isNetbeansBuffer(buf)
4215#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004216#ifdef FEAT_TERMINAL
4217 || buf->b_term != NULL
4218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 )
4220 return 0;
4221
4222 if ( !(buf->b_flags & BF_NOTEDITED)
4223 && buf->b_mtime != 0
4224 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004225 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004226 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227#ifdef HAVE_ST_MODE
4228 || (int)st.st_mode != buf->b_orig_mode
4229#else
4230 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4231#endif
4232 ))
4233 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004234 long prev_b_mtime = buf->b_mtime;
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 retval = 1;
4237
Bram Moolenaar386bc822018-07-07 18:34:12 +02004238 // set b_mtime to stop further warnings (e.g., when executing
4239 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if (stat_res < 0)
4241 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004242 // Check the file again later to see if it re-appears.
4243 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 buf->b_orig_size = 0;
4245 buf->b_orig_mode = 0;
4246 }
4247 else
4248 buf_store_time(buf, &st, buf->b_ffname);
4249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004250 // Don't do anything for a directory. Might contain the file
4251 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (mch_isdir(buf->b_fname))
4253 ;
4254
4255 /*
4256 * If 'autoread' is set, the buffer has no changes and the file still
4257 * exists, reload the buffer. Use the buffer-local option value if it
4258 * was set, the global option value otherwise.
4259 */
4260 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4261 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004262 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 else
4264 {
John Marriott14ede182024-12-29 16:14:19 +01004265 char *reason;
4266#ifdef FEAT_EVAL
4267 size_t reasonlen;
4268#endif
4269
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004270 if (stat_res < 0)
John Marriott14ede182024-12-29 16:14:19 +01004271 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004272 reason = "deleted";
John Marriott14ede182024-12-29 16:14:19 +01004273#ifdef FEAT_EVAL
4274 reasonlen = STRLEN_LITERAL("deleted");
4275#endif
4276 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004277 else if (bufIsChanged(buf))
John Marriott14ede182024-12-29 16:14:19 +01004278 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004279 reason = "conflict";
John Marriott14ede182024-12-29 16:14:19 +01004280#ifdef FEAT_EVAL
4281 reasonlen = STRLEN_LITERAL("conflict");
4282#endif
4283 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004284 /*
4285 * Check if the file contents really changed to avoid giving a
4286 * warning when only the timestamp was set (e.g., checked out of
4287 * CVS). Always warn when the buffer was changed.
4288 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004289 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
John Marriott14ede182024-12-29 16:14:19 +01004290 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004291 reason = "changed";
John Marriott14ede182024-12-29 16:14:19 +01004292#ifdef FEAT_EVAL
4293 reasonlen = STRLEN_LITERAL("changed");
4294#endif
4295 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004296 else if (orig_mode != buf->b_orig_mode)
John Marriott14ede182024-12-29 16:14:19 +01004297 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004298 reason = "mode";
John Marriott14ede182024-12-29 16:14:19 +01004299#ifdef FEAT_EVAL
4300 reasonlen = STRLEN_LITERAL("mode");
4301#endif
4302 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004303 else
John Marriott14ede182024-12-29 16:14:19 +01004304 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004305 reason = "time";
John Marriott14ede182024-12-29 16:14:19 +01004306#ifdef FEAT_EVAL
4307 reasonlen = STRLEN_LITERAL("time");
4308#endif
4309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Only give the warning if there are no FileChangedShell
4313 * autocommands.
4314 * Avoid being called recursively by setting "busy".
4315 */
4316 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004317#ifdef FEAT_EVAL
Yegappan Lakshmanan5e9aaed2025-01-18 10:24:25 +01004318 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, (int)reasonlen);
John Marriott14ede182024-12-29 16:14:19 +01004319 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", 0);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004320#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004321 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4323 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004324 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 busy = FALSE;
4326 if (n)
4327 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004328 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004329 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004330#ifdef FEAT_EVAL
John Marriott14ede182024-12-29 16:14:19 +01004331 char_u *s = get_vim_var_str(VV_FCS_CHOICE);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004332 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004333 reload = RELOAD_NORMAL;
4334 else if (STRCMP(s, "edit") == 0)
4335 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004336 else if (STRCMP(s, "ask") == 0)
4337 n = FALSE;
4338 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004339#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004340 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004342 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004345 {
4346 // Only give the message once.
4347 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004348 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 else
4351 {
4352 helpmesg = TRUE;
4353#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4354 can_reload = TRUE;
4355#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004356 if (reason[2] == 'n')
4357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004359 mesg2 = _("See \":help W12\" for more info.");
4360 }
4361 else if (reason[1] == 'h')
4362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004364 mesg2 = _("See \":help W11\" for more info.");
4365 }
4366 else if (*reason == 'm')
4367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004369 mesg2 = _("See \":help W16\" for more info.");
4370 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004371 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004373 // Only timestamp changed, store it to avoid a warning
4374 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004375 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004376 buf->b_mtime_read_ns = buf->b_mtime_ns;
4377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 }
4380 }
4381
4382 }
4383 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4384 && vim_fexists(buf->b_ffname))
4385 {
4386 retval = 1;
4387 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4388 buf->b_flags |= BF_NEW_W;
4389#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4390 can_reload = TRUE;
4391#endif
4392 }
4393
zeertzjq8a017442024-03-07 21:48:33 +01004394 if (mesg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
John Marriott14ede182024-12-29 16:14:19 +01004396 char_u *path;
4397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 path = home_replace_save(buf, buf->b_fname);
4399 if (path != NULL)
4400 {
John Marriott14ede182024-12-29 16:14:19 +01004401 size_t tbufsize;
4402 char *tbuf;
4403
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004404 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 mesg2 = "";
John Marriott14ede182024-12-29 16:14:19 +01004406 tbufsize = STRLEN(mesg) + STRLEN(path) + 2 + STRLEN(mesg2) + 1; // +2 for either '\n' or "; "
4407 // and +1 for NUL
4408 tbuf = alloc(tbufsize);
4409 if (tbuf != NULL)
4410 {
4411 int tbuflen;
4412
4413 tbuflen = vim_snprintf(tbuf, tbufsize, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004414#ifdef FEAT_EVAL
John Marriott14ede182024-12-29 16:14:19 +01004415 // Set warningmsg here, before the unimportant and output-specific
4416 // mesg2 has been appended.
4417 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, tbuflen);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
John Marriott14ede182024-12-29 16:14:19 +01004420 if (can_reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (*mesg2 != NUL)
John Marriott14ede182024-12-29 16:14:19 +01004423 vim_snprintf(tbuf + tbuflen, tbufsize - tbuflen, "\n%s", mesg2);
4424 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4425 (char_u *)tbuf,
4426 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4427 1, NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
John Marriott14ede182024-12-29 16:14:19 +01004429 case 2:
4430 reload = RELOAD_NORMAL;
4431 break;
4432 case 3:
4433 reload = RELOAD_DETECT;
4434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 }
John Marriott14ede182024-12-29 16:14:19 +01004437 else
4438#endif
4439 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4440 || already_warned)
4441 {
4442 if (*mesg2 != NUL)
4443 vim_snprintf(tbuf + tbuflen, tbufsize - tbuflen, "; %s", mesg2);
4444 emsg(tbuf);
4445 retval = 2;
4446 }
4447 else
4448 {
4449 if (!autocmd_busy)
4450 {
4451 msg_start();
4452 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
4453 if (*mesg2 != NUL)
4454 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
4455 msg_clr_eos();
4456 (void)msg_end();
4457 if (emsg_silent == 0 && !in_assert_fails)
4458 {
4459 out_flush();
4460 #ifdef FEAT_GUI
4461 if (!focus)
4462 #endif
4463 // give the user some time to think about it
4464 ui_delay(1004L, TRUE);
4465
4466 // don't redraw and erase the message
4467 redraw_cmdline = FALSE;
4468 }
4469 }
4470 already_warned = TRUE;
4471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 vim_free(tbuf);
John Marriott14ede182024-12-29 16:14:19 +01004475 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 }
4478
Rob Pilling8196e942022-02-11 15:12:10 +00004479 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004480 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004482 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004483#ifdef FEAT_PERSISTENT_UNDO
4484 if (buf->b_p_udf && buf->b_ffname != NULL)
4485 {
4486 char_u hash[UNDO_HASH_SIZE];
4487 buf_T *save_curbuf = curbuf;
4488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004489 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004490 curbuf = buf;
4491 u_compute_hash(hash);
4492 u_write_undo(NULL, FALSE, buf, hash);
4493 curbuf = save_curbuf;
4494 }
4495#endif
4496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004499 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004500 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4501 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 // restore this in case an autocommand has set it; it would break
4504 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 need_mouse_correct = save_mouse_correct;
4506#endif
4507
4508 return retval;
4509}
4510
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004511/*
4512 * Reload a buffer that is already loaded.
4513 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004514 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4515 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004516 */
4517 void
Rob Pilling8196e942022-02-11 15:12:10 +00004518buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004519{
4520 exarg_T ea;
4521 pos_T old_cursor;
4522 linenr_T old_topline;
4523 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004524 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004525 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004526 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004527 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004528 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004529 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004530
Bram Moolenaare76062c2022-11-28 18:51:43 +00004531 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004532 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004533 if (curbuf != buf)
4534 {
4535 // Failed to find a window for "buf", it is dangerous to continue,
4536 // better bail out.
4537 return;
4538 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004539
Rob Pilling8196e942022-02-11 15:12:10 +00004540 // Unless reload_options is set, we only want to read the text from the
4541 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4542 // Force the fileformat and encoding to be the same.
4543 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004544 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004545 else
4546 prepped = prep_exarg(&ea, buf);
4547
4548 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004549 {
4550 old_cursor = curwin->w_cursor;
4551 old_topline = curwin->w_topline;
4552
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004553 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004554 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004555 // Save all the text, so that the reload can be undone.
4556 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004557 u_sync(FALSE);
4558 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4559 flags |= READ_KEEP_UNDO;
4560 }
4561
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004562 /*
4563 * To behave like when a new file is edited (matters for
4564 * BufReadPost autocommands) we first need to delete the current
4565 * buffer contents. But if reading the file fails we should keep
4566 * the old contents. Can't use memory only, the file might be
4567 * too big. Use a hidden buffer to move the buffer contents to.
4568 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004569 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004570 savebuf = NULL;
4571 else
4572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004573 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004574 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004575 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004576 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004578 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004579 curbuf = savebuf;
4580 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004581 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004582 curbuf = buf;
4583 curwin->w_buffer = buf;
4584 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004585 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004586 || move_lines(buf, savebuf) == FAIL)
4587 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004588 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004589 saved = FAIL;
4590 }
4591 }
4592
4593 if (saved == OK)
4594 {
zeertzjq8a017442024-03-07 21:48:33 +01004595 int old_msg_silent = msg_silent;
4596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004597 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
zeertzjq5bf6c212024-03-31 18:41:27 +02004598 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
zeertzjq8a017442024-03-07 21:48:33 +01004599
4600 if (shortmess(SHM_FILEINFO))
4601 msg_silent = 1;
4602
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004603 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4604 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004605 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004606 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004607#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004608 if (!aborting())
4609#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004610 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004611 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004613 // Put the text back from the save buffer. First
4614 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004615 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004616 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004617 break;
4618 (void)move_lines(savebuf, buf);
4619 }
4620 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004621 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004623 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004624 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004625 if ((flags & READ_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +01004626 u_clearallandblockfree(buf);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004627 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004629 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004630 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004631 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004632 }
zeertzjq8a017442024-03-07 21:48:33 +01004633
4634 msg_silent = old_msg_silent;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004635 }
4636 vim_free(ea.cmd);
4637
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004638 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004639 wipe_buffer(savebuf, FALSE);
4640
4641#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004643 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004644#endif
4645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004646 // Restore the topline and cursor position and check it (lines may
4647 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004648 if (old_topline > curbuf->b_ml.ml_line_count)
4649 curwin->w_topline = curbuf->b_ml.ml_line_count;
4650 else
4651 curwin->w_topline = old_topline;
4652 curwin->w_cursor = old_cursor;
4653 check_cursor();
4654 update_topline();
zeertzjq5bf6c212024-03-31 18:41:27 +02004655 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004656#ifdef FEAT_FOLDING
4657 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004658 win_T *wp;
4659 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004661 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004662 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004663 if (wp->w_buffer == curwin->w_buffer
4664 && !foldmethodIsManual(wp))
4665 foldUpdateAll(wp);
4666 }
4667#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004668 // If the mode didn't change and 'readonly' was set, keep the old
4669 // value; the user probably used the ":view" command. But don't
4670 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004671 if (orig_mode == curbuf->b_orig_mode)
4672 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004674 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004675 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004676 }
4677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004678 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004679 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004680 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004681}
4682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004684buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004687#ifdef ST_MTIM_NSEC
4688 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4689#else
4690 buf->b_mtime_ns = 0;
4691#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004692 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#ifdef HAVE_ST_MODE
4694 buf->b_orig_mode = (int)st->st_mode;
4695#else
4696 buf->b_orig_mode = mch_getperm(fname);
4697#endif
4698}
4699
4700/*
4701 * Adjust the line with missing eol, used for the next write.
4702 * Used for do_filter(), when the input lines for the filter are deleted.
4703 */
4704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004707 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004708 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709}
4710
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004711// Subfuncions for readdirex()
4712#ifdef FEAT_EVAL
4713# ifdef MSWIN
4714 static char_u *
4715getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4716{
4717 stat_T st;
4718 unsigned short st_mode;
4719 DWORD flag = wfd->dwFileAttributes;
4720 WCHAR *wp;
4721
4722 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4723 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4724 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4725 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4726
4727 wp = wcsrchr(wfd->cFileName, L'.');
4728 if (wp != NULL)
4729 {
4730 if (_wcsicmp(wp, L".exe") == 0 ||
4731 _wcsicmp(wp, L".com") == 0 ||
4732 _wcsicmp(wp, L".cmd") == 0 ||
4733 _wcsicmp(wp, L".bat") == 0)
4734 st_mode |= _S_IEXEC;
4735 }
4736
4737 // Copy user bits to group/other.
4738 st_mode |= (st_mode & 0700) >> 3;
4739 st_mode |= (st_mode & 0700) >> 6;
4740
4741 st.st_mode = st_mode;
4742 return getfpermst(&st, perm);
4743}
4744
4745 static char_u *
4746getftypewfd(WIN32_FIND_DATAW *wfd)
4747{
4748 DWORD flag = wfd->dwFileAttributes;
4749 DWORD tag = wfd->dwReserved0;
4750
4751 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4752 {
4753 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4754 return (char_u*)"junction";
John Marriott14ede182024-12-29 16:14:19 +01004755 if (tag == IO_REPARSE_TAG_SYMLINK)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004756 {
4757 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4758 return (char_u*)"linkd";
John Marriott14ede182024-12-29 16:14:19 +01004759
4760 return (char_u*)"link";
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004761 }
4762 return (char_u*)"reparse"; // unknown reparse point type
4763 }
4764 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4765 return (char_u*)"dir";
John Marriott14ede182024-12-29 16:14:19 +01004766
4767 return (char_u*)"file";
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004768}
4769
4770 static dict_T *
4771create_readdirex_item(WIN32_FIND_DATAW *wfd)
4772{
4773 dict_T *item;
4774 char_u *p;
4775 varnumber_T size, time;
4776 char_u permbuf[] = "---------";
4777
4778 item = dict_alloc();
4779 if (item == NULL)
4780 return NULL;
4781 item->dv_refcount++;
4782
4783 p = utf16_to_enc(wfd->cFileName, NULL);
4784 if (p == NULL)
4785 goto theend;
4786 if (dict_add_string(item, "name", p) == FAIL)
4787 {
4788 vim_free(p);
4789 goto theend;
4790 }
4791 vim_free(p);
4792
4793 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4794 if (dict_add_number(item, "size", size) == FAIL)
4795 goto theend;
4796
4797 // Convert FILETIME to unix time.
4798 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4799 wfd->ftLastWriteTime.dwLowDateTime)
4800 - 116444736000000000) / 10000000;
4801 if (dict_add_number(item, "time", time) == FAIL)
4802 goto theend;
4803
4804 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4805 goto theend;
4806 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4807 goto theend;
4808
4809 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4810 goto theend;
4811 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4812 goto theend;
4813
4814 return item;
4815
4816theend:
4817 dict_unref(item);
4818 return NULL;
4819}
4820# else
4821 static dict_T *
4822create_readdirex_item(char_u *path, char_u *name)
4823{
4824 dict_T *item;
4825 char *p;
4826 size_t len;
4827 stat_T st;
4828 int ret, link = FALSE;
4829 varnumber_T size;
4830 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004831 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004832 struct passwd *pw;
4833 struct group *gr;
4834
4835 item = dict_alloc();
4836 if (item == NULL)
4837 return NULL;
4838 item->dv_refcount++;
4839
4840 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4841 p = alloc(len);
4842 if (p == NULL)
4843 goto theend;
4844 vim_snprintf(p, len, "%s/%s", path, name);
4845 ret = mch_lstat(p, &st);
4846 if (ret >= 0 && S_ISLNK(st.st_mode))
4847 {
4848 link = TRUE;
4849 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004850 if (ret < 0)
4851 q = (char_u*)"link";
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004852 }
4853 vim_free(p);
4854
4855 if (dict_add_string(item, "name", name) == FAIL)
4856 goto theend;
4857
4858 if (ret >= 0)
4859 {
4860 size = (varnumber_T)st.st_size;
4861 if (S_ISDIR(st.st_mode))
4862 size = 0;
4863 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004864 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004865 size = -2;
4866 if (dict_add_number(item, "size", size) == FAIL)
4867 goto theend;
4868 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4869 goto theend;
4870
4871 if (link)
4872 {
4873 if (S_ISDIR(st.st_mode))
4874 q = (char_u*)"linkd";
4875 else
4876 q = (char_u*)"link";
4877 }
4878 else
4879 q = getftypest(&st);
4880 if (dict_add_string(item, "type", q) == FAIL)
4881 goto theend;
4882 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4883 goto theend;
4884
4885 pw = getpwuid(st.st_uid);
4886 if (pw == NULL)
4887 q = (char_u*)"";
4888 else
4889 q = (char_u*)pw->pw_name;
4890 if (dict_add_string(item, "user", q) == FAIL)
4891 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004892# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004893 gr = getgrgid(st.st_gid);
4894 if (gr == NULL)
4895 q = (char_u*)"";
4896 else
4897 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004898# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004899 if (dict_add_string(item, "group", q) == FAIL)
4900 goto theend;
4901 }
4902 else
4903 {
4904 if (dict_add_number(item, "size", -1) == FAIL)
4905 goto theend;
4906 if (dict_add_number(item, "time", -1) == FAIL)
4907 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004908 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004909 goto theend;
4910 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4911 goto theend;
4912 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4913 goto theend;
4914 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4915 goto theend;
4916 }
4917 return item;
4918
4919theend:
4920 dict_unref(item);
4921 return NULL;
4922}
4923# endif
4924
4925 static int
4926compare_readdirex_item(const void *p1, const void *p2)
4927{
4928 char_u *name1, *name2;
4929
Bram Moolenaard61efa52022-07-23 09:52:04 +01004930 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4931 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004932 if (readdirex_sort == READDIR_SORT_BYTE)
4933 return STRCMP(name1, name2);
John Marriott14ede182024-12-29 16:14:19 +01004934 if (readdirex_sort == READDIR_SORT_IC)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004935 return STRICMP(name1, name2);
John Marriott14ede182024-12-29 16:14:19 +01004936
4937 return STRCOLL(name1, name2);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004938}
4939
4940 static int
4941compare_readdir_item(const void *s1, const void *s2)
4942{
4943 if (readdirex_sort == READDIR_SORT_BYTE)
4944 return STRCMP(*(char **)s1, *(char **)s2);
John Marriott14ede182024-12-29 16:14:19 +01004945 if (readdirex_sort == READDIR_SORT_IC)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004946 return STRICMP(*(char **)s1, *(char **)s2);
John Marriott14ede182024-12-29 16:14:19 +01004947
4948 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004949}
4950#endif
4951
Bram Moolenaarda440d22016-01-16 21:27:23 +01004952#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4953/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004954 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004955 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004956 * If "withattr" is TRUE, retrieve the names and their attributes.
4957 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004958 * Return OK for success, FAIL for failure.
4959 */
4960 int
4961readdir_core(
4962 garray_T *gap,
4963 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004964 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004965 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004966 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004967 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004968{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004969 int failed = FALSE;
4970 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004971# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004972 char_u *buf;
4973 int ok;
4974 HANDLE hFind = INVALID_HANDLE_VALUE;
4975 WIN32_FIND_DATAW wfd;
4976 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
John Marriott14ede182024-12-29 16:14:19 +01004977 char_u *p_end;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004978# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004979 DIR *dirp;
4980 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004981# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004982
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004983 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004984
4985# ifdef FEAT_EVAL
4986# define FREE_ITEM(item) do { \
4987 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004988 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004989 else \
4990 vim_free(item); \
4991 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004992
4993 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004994# else
4995# define FREE_ITEM(item) vim_free(item)
4996# endif
4997
4998# ifdef MSWIN
4999 buf = alloc(MAXPATHL);
5000 if (buf == NULL)
5001 return FAIL;
5002 STRNCPY(buf, path, MAXPATHL-5);
John Marriott14ede182024-12-29 16:14:19 +01005003 p = p_end = buf + STRLEN(buf);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005004 MB_PTR_BACK(buf, p);
5005 if (*p == '\\' || *p == '/')
John Marriott14ede182024-12-29 16:14:19 +01005006 p_end = p;
5007 STRCPY(p_end, "\\*");
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005008
5009 wn = enc_to_utf16(buf, NULL);
5010 if (wn != NULL)
5011 hFind = FindFirstFileW(wn, &wfd);
5012 ok = (hFind != INVALID_HANDLE_VALUE);
5013 if (!ok)
5014 {
5015 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005016 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005017 }
5018 else
5019 {
5020 while (ok)
5021 {
5022 int ignore;
5023 void *item;
5024 WCHAR *wp;
5025
5026 wp = wfd.cFileName;
5027 ignore = wp[0] == L'.' &&
5028 (wp[1] == NUL ||
5029 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02005030 if (ignore)
5031 {
5032 ok = FindNextFileW(hFind, &wfd);
5033 continue;
5034 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005035# ifdef FEAT_EVAL
5036 if (withattr)
5037 item = (void*)create_readdirex_item(&wfd);
5038 else
5039# endif
5040 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
5041 if (item == NULL)
5042 {
5043 failed = TRUE;
5044 break;
5045 }
5046
5047 if (!ignore && checkitem != NULL)
5048 {
5049 int r = checkitem(context, item);
5050
5051 if (r < 0)
5052 {
5053 FREE_ITEM(item);
5054 break;
5055 }
5056 if (r == 0)
5057 ignore = TRUE;
5058 }
5059
5060 if (!ignore)
5061 {
5062 if (ga_grow(gap, 1) == OK)
5063 ((void**)gap->ga_data)[gap->ga_len++] = item;
5064 else
5065 {
5066 failed = TRUE;
5067 FREE_ITEM(item);
5068 break;
5069 }
5070 }
5071 else
5072 FREE_ITEM(item);
5073
5074 ok = FindNextFileW(hFind, &wfd);
5075 }
5076 FindClose(hFind);
5077 }
5078
5079 vim_free(buf);
5080 vim_free(wn);
5081# else // MSWIN
5082 dirp = opendir((char *)path);
5083 if (dirp == NULL)
5084 {
5085 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005086 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005087 }
5088 else
5089 {
5090 for (;;)
5091 {
5092 int ignore;
5093 void *item;
5094
5095 dp = readdir(dirp);
5096 if (dp == NULL)
5097 break;
5098 p = (char_u *)dp->d_name;
5099
5100 ignore = p[0] == '.' &&
5101 (p[1] == NUL ||
5102 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02005103 if (ignore)
5104 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005105# ifdef FEAT_EVAL
5106 if (withattr)
5107 item = (void*)create_readdirex_item(path, p);
5108 else
5109# endif
5110 item = (void*)vim_strsave(p);
5111 if (item == NULL)
5112 {
5113 failed = TRUE;
5114 break;
5115 }
5116
Bram Moolenaarfe154992022-03-22 20:42:12 +00005117 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005118 {
5119 int r = checkitem(context, item);
5120
5121 if (r < 0)
5122 {
5123 FREE_ITEM(item);
5124 break;
5125 }
5126 if (r == 0)
5127 ignore = TRUE;
5128 }
5129
5130 if (!ignore)
5131 {
5132 if (ga_grow(gap, 1) == OK)
5133 ((void**)gap->ga_data)[gap->ga_len++] = item;
5134 else
5135 {
5136 failed = TRUE;
5137 FREE_ITEM(item);
5138 break;
5139 }
5140 }
5141 else
5142 FREE_ITEM(item);
5143 }
5144
5145 closedir(dirp);
5146 }
5147# endif // MSWIN
5148
5149# undef FREE_ITEM
5150
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005151 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005152 {
5153# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005154 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005155 if (withattr)
5156 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5157 compare_readdirex_item);
5158 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005159 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5160 compare_readdir_item);
5161# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005162 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005163# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005164 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005165
5166 return failed ? FAIL : OK;
5167}
5168
5169/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005170 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005171 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005172 */
5173 int
5174delete_recursive(char_u *name)
5175{
5176 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005177
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005178 // A symbolic link to a directory itself is deleted, not the directory it
5179 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005180 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005181# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005182 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005183# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005184 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005185# endif
5186 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005187 {
John Marriott14ede182024-12-29 16:14:19 +01005188 char_u *exp = vim_strsave(name);
5189 garray_T ga;
5190
Bram Moolenaarda440d22016-01-16 21:27:23 +01005191 if (exp == NULL)
5192 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005193 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005194 {
John Marriott14ede182024-12-29 16:14:19 +01005195 int len = vim_snprintf((char *)NameBuff, MAXPATHL, "%s/", exp);
5196 int i;
5197
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005198 for (i = 0; i < ga.ga_len; ++i)
5199 {
John Marriott14ede182024-12-29 16:14:19 +01005200 vim_snprintf((char *)NameBuff + len, MAXPATHL - len, "%s", ((char_u **)ga.ga_data)[i]);
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005201 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005202 // Remember the failure but continue deleting any further
5203 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005204 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005205 }
5206 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005207 if (mch_rmdir(exp) != 0)
5208 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005209 }
5210 else
5211 result = -1;
5212 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005213 }
5214 else
5215 result = mch_remove(name) == 0 ? 0 : -1;
5216
5217 return result;
5218}
5219#endif
5220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005222static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005224# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5225/*
5226 * Open temporary directory and take file lock to prevent
5227 * to be auto-cleaned.
5228 */
5229 static void
5230vim_opentempdir(void)
5231{
5232 DIR *dp = NULL;
5233
5234 if (vim_tempdir_dp != NULL)
5235 return;
5236
5237 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005238 if (dp == NULL)
5239 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005240
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005241 vim_tempdir_dp = dp;
5242 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005243}
5244
5245/*
5246 * Close temporary directory - it automatically release file lock.
5247 */
5248 static void
5249vim_closetempdir(void)
5250{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005251 if (vim_tempdir_dp == NULL)
5252 return;
5253
5254 closedir(vim_tempdir_dp);
5255 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005256}
5257# endif
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259/*
5260 * Delete the temp directory and all files it contains.
5261 */
5262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005265 if (vim_tempdir == NULL)
5266 return;
5267
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005268# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005269 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005270# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005271 // remove the trailing path separator
5272 gettail(vim_tempdir)[-1] = NUL;
5273 delete_recursive(vim_tempdir);
5274 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005278 * Directory "tempdir" was created. Expand this name to a full path and put
5279 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5280 * "tempdir" must be no longer than MAXPATHL.
5281 */
5282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005283vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005284{
5285 char_u *buf;
John Marriott14ede182024-12-29 16:14:19 +01005286 size_t buflen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005287
Bram Moolenaar964b3742019-05-24 18:54:09 +02005288 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005289 if (buf == NULL)
5290 return;
5291
5292 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5293 STRCPY(buf, tempdir);
John Marriott14ede182024-12-29 16:14:19 +01005294 buflen = STRLEN(buf);
5295 if (!after_pathsep(buf, buf + buflen))
5296 {
5297 STRCPY(buf + buflen, PATHSEPSTR);
5298 buflen += STRLEN_LITERAL(PATHSEPSTR);
5299 }
5300 vim_tempdir = vim_strnsave(buf, buflen);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005301# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005302 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005303# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005304 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005305}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005306#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005307
5308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 * vim_tempname(): Return a unique name that can be used for a temp file.
5310 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005311 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5312 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 *
5314 * The returned pointer is to allocated memory.
5315 * The returned pointer is NULL if no valid name was found.
5316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005320 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005323 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005324#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005325 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#else
5327 char_u itmp[TEMPNAMELEN];
5328#endif
5329
5330#ifdef TEMPDIRNAMES
5331 static char *(tempdirs[]) = {TEMPDIRNAMES};
5332 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005334 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335# endif
5336
5337 /*
5338 * This will create a directory for private use by this instance of Vim.
5339 * This is done once, and the same directory is used for all temp files.
5340 * This method avoids security problems because of symlink attacks et al.
5341 * It's also a bit faster, because we only need to check for an existing
5342 * file when creating the directory and not for each temp file.
5343 */
5344 if (vim_tempdir == NULL)
5345 {
5346 /*
5347 * Try the entries in TEMPDIRNAMES to create the temp directory.
5348 */
K.Takataeeec2542021-06-02 13:28:16 +02005349 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005351# ifndef HAVE_MKDTEMP
5352 long nr;
5353 long off;
5354# endif
John Marriottfff01322025-06-18 18:15:31 +02005355 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005357 // Expand $TMP, leave room for "/v1100000/999999999".
5358 // Skip the directory check if the expansion fails.
John Marriottfff01322025-06-18 18:15:31 +02005359 itmplen = expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005360 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005362 // directory exists
John Marriott14ede182024-12-29 16:14:19 +01005363 if (!after_pathsep(itmp, itmp + itmplen))
5364 {
5365 STRCPY(itmp + itmplen, PATHSEPSTR);
5366 itmplen += STRLEN_LITERAL(PATHSEPSTR);
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
Bram Moolenaareaf03392009-11-17 11:08:52 +00005369# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005370 {
5371# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 // Make sure the umask doesn't remove the executable bit.
5373 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005374 mode_t umask_save = umask(077);
5375# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 // Leave room for filename
John Marriott14ede182024-12-29 16:14:19 +01005377 STRCPY(itmp + itmplen, "vXXXXXX");
5378 itmplen += STRLEN_LITERAL("vXXXXXX");
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005379 if (mkdtemp((char *)itmp) != NULL)
5380 vim_settempdir(itmp);
5381# if defined(UNIX) || defined(VMS)
5382 (void)umask(umask_save);
5383# endif
5384 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005385# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005386 // Get an arbitrary number of up to 6 digits. When it's
5387 // unlikely that it already exists it will be faster,
5388 // otherwise it doesn't matter. The use of mkdir() avoids any
5389 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
5391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005392 // Try up to 10000 different values until we find a name that
5393 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 for (off = 0; off < 10000L; ++off)
5395 {
5396 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005397# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
John Marriott14ede182024-12-29 16:14:19 +01005401 vim_snprintf((char *)itmp + itmplen, sizeof(itmp) - itmplen, "v%ld", nr + off);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005402# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005403 // If mkdir() does not set errno to EEXIST, check for
5404 // existing file here. There is a race condition then,
5405 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 if (mch_stat((char *)itmp, &st) >= 0)
5407 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005408# endif
5409# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 // Make sure the umask doesn't remove the executable bit.
5411 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005415# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 if (r == 0)
5419 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005420 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 break;
5422 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005423# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005424 // If the mkdir() didn't fail because the file/dir exists,
5425 // we probably can't create any dir here, try another
5426 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 break;
5430 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005431# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (vim_tempdir != NULL)
5433 break;
5434 }
5435 }
5436 }
5437
5438 if (vim_tempdir != NULL)
5439 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 // There is no need to check if the file exists, because we own the
5441 // directory and nobody else creates a file in it.
John Marriott14ede182024-12-29 16:14:19 +01005442 int itmplen = vim_snprintf((char *)itmp, sizeof(itmp), "%s%ld", vim_tempdir, temp_count++);
5443 return vim_strnsave(itmp, (size_t)itmplen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445
5446 return NULL;
5447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005448#else // TEMPDIRNAMES
John Marriott14ede182024-12-29 16:14:19 +01005449 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
Bram Moolenaar4f974752019-02-17 17:44:42 +01005451# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005452 WCHAR wszTempFile[_MAX_PATH + 1];
5453 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005454 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 char_u *retval;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005456 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005457 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005459 wcscpy(itmp, L"");
5460 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005461 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005462 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005463 wszTempFile[1] = L'\\';
5464 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005465 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005466 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005467
5468 // randomize the name to avoid collisions
5469 i = mch_get_pid() + extra_char;
5470 buf4[1] = chartab[i % 36];
5471 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005472 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005474 if (!keep)
John Marriott14ede182024-12-29 16:14:19 +01005475 // GetTempFileNameW() will create the file, we don't want that
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005476 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005478 // Backslashes in a temp file name cause problems when filtering with
5479 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005480 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005481 retval = utf16_to_enc(itmp, NULL);
John Marriott031f2272025-04-23 20:56:08 +02005482 if (retval == NULL)
5483 return NULL;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005484 shname = gettail(p_sh);
5485 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5486 || strstr((char *)shname, "pwsh") != NULL ))
5487 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 for (p = retval; *p; ++p)
5489 if (*p == '\\')
5490 *p = '/';
5491 return retval;
5492
Bram Moolenaar4f974752019-02-17 17:44:42 +01005493# else // MSWIN
John Marriott14ede182024-12-29 16:14:19 +01005494 int itmplen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
5496# ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005498 p = tmpnam((char *)itmp);
5499 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 return NULL;
5501# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005504 // mktemp() is not working on VMS. It seems to be
5505 // a do-nothing function. Therefore we use tempnam().
John Marriott14ede182024-12-29 16:14:19 +01005506 vim_snprintf((char *)itmp, sizeof(itmp), "VIM%c", extra_char);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 p = (char_u *)tempnam("tmp:", (char *)itmp);
5508 if (p != NULL)
5509 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005510 // VMS will use '.LIS' if we don't explicitly specify an extension,
5511 // and VIM will then be unable to find the file later
John Marriott14ede182024-12-29 16:14:19 +01005512 itmplen = vim_snprintf((char *)itmp, sizeof(itmp), "%s.txt", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 free(p);
5514 }
5515 else
5516 return NULL;
5517# else
5518 STRCPY(itmp, TEMPNAME);
John Marriott14ede182024-12-29 16:14:19 +01005519 itmplen = STRLEN_LITERAL(TEMPNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if ((p = vim_strchr(itmp, '?')) != NULL)
5521 *p = extra_char;
5522 if (mktemp((char *)itmp) == NULL)
5523 return NULL;
5524# endif
5525# endif
5526
John Marriott14ede182024-12-29 16:14:19 +01005527 return vim_strnsave(itmp, (size_t)itmplen);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005528# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005529#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530}
5531
5532#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5533/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005534 * Convert all backslashes in fname to forward slashes in-place, unless when
5535 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 */
5537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 char_u *p;
5541
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005542 if (path_with_url(fname))
5543 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005545 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005546 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005548 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 *p = '/';
5550}
5551#endif
5552
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005553/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005554 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5555 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5556 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * Used for autocommands and 'wildignore'.
5558 * Returns TRUE if there is a match, FALSE otherwise.
5559 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005562 char_u *pattern, // pattern to match with
5563 regprog_T **prog, // pre-compiled regprog or NULL
5564 char_u *fname, // full path of file name
5565 char_u *sfname, // short file name or NULL
5566 char_u *tail, // tail of path
5567 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 regmatch_T regmatch;
5570 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005572 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005573 if (prog != NULL)
5574 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005576 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 /*
5579 * Try for a match with the pattern with:
5580 * 1. the full file name, when the pattern has a '/'.
5581 * 2. the short file name, when the pattern has a '/'.
5582 * 3. the tail of the file name, when the pattern has no '/'.
5583 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005584 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 && ((allow_dirs
5586 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5587 || (sfname != NULL
5588 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005589 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 result = TRUE;
5591
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005592 if (prog != NULL)
5593 *prog = regmatch.regprog;
5594 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005595 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 return result;
5597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599/*
5600 * Return TRUE if a file matches with a pattern in "list".
5601 * "list" is a comma-separated list of patterns, like 'wildignore'.
5602 * "sfname" is the short file name or NULL, "ffname" the long file name.
5603 */
5604 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005605match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005607 char_u buf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u *tail;
5609 char_u *regpat;
5610 char allow_dirs;
5611 int match;
5612 char_u *p;
5613
5614 tail = gettail(sfname);
5615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005616 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 p = list;
5618 while (*p)
5619 {
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005620 copy_option_part(&p, buf, MAXPATHL, ",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5622 if (regpat == NULL)
5623 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005624 match = match_file_pat(regpat, NULL, ffname, sfname,
5625 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 vim_free(regpat);
5627 if (match)
5628 return TRUE;
5629 }
5630 return FALSE;
5631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633/*
5634 * Convert the given pattern "pat" which has shell style wildcards in it, into
5635 * a regular expression, and return the result in allocated memory. If there
5636 * is a directory path separator to be matched, then TRUE is put in
5637 * allow_dirs, otherwise FALSE is put there -- webb.
5638 * Handle backslashes before special characters, like "\*" and "\ ".
5639 *
Christian Brabandt1a31c432024-10-06 16:34:20 +02005640 * no_bslash only makes a difference, when BACKSLASH_IN_FILENAME is defined
5641 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 * Returns NULL when out of memory.
5643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645file_pat_to_reg_pat(
5646 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005647 char_u *pat_end, // first char after pattern or NULL
5648 char *allow_dirs, // Result passed back out in here
5649 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 char_u *endp;
5653 char_u *reg_pat;
5654 char_u *p;
5655 int i;
5656 int nested = 0;
5657 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 if (allow_dirs != NULL)
5660 *allow_dirs = FALSE;
5661 if (pat_end == NULL)
5662 pat_end = pat + STRLEN(pat);
5663
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 for (p = pat; p < pat_end; p++)
5665 {
5666 switch (*p)
5667 {
5668 case '*':
5669 case '.':
5670 case ',':
5671 case '{':
5672 case '}':
5673 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005674 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 break;
5676#ifdef BACKSLASH_IN_FILENAME
5677 case '\\':
5678 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681#endif
5682 default:
5683 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005684 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 ++p;
5687 ++size;
5688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690 }
5691 }
5692 reg_pat = alloc(size + 1);
5693 if (reg_pat == NULL)
5694 return NULL;
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 if (pat[0] == '*')
5699 while (pat[0] == '*' && pat < pat_end - 1)
5700 pat++;
5701 else
5702 reg_pat[i++] = '^';
5703 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005704 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 while (endp - pat > 0 && *endp == '*')
5707 endp--;
5708 add_dollar = FALSE;
5709 }
5710 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5711 {
5712 switch (*p)
5713 {
5714 case '*':
5715 reg_pat[i++] = '.';
5716 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005717 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005718 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 case '~':
5722 reg_pat[i++] = '\\';
5723 reg_pat[i++] = *p;
5724 break;
5725 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 reg_pat[i++] = '.';
5727 break;
5728 case '\\':
5729 if (p[1] == NUL)
5730 break;
5731#ifdef BACKSLASH_IN_FILENAME
5732 if (!no_bslash)
5733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005734 // translate:
5735 // "\x" to "\\x" e.g., "dir\file"
5736 // "\*" to "\\.*" e.g., "dir\*.c"
5737 // "\?" to "\\." e.g., "dir\??.c"
5738 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5740 && p[1] != '+')
5741 {
5742 reg_pat[i++] = '[';
5743 reg_pat[i++] = '\\';
5744 reg_pat[i++] = '/';
5745 reg_pat[i++] = ']';
5746 if (allow_dirs != NULL)
5747 *allow_dirs = TRUE;
5748 break;
5749 }
5750 }
5751#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005752 // Undo escaping from ExpandEscape():
5753 // foo\?bar -> foo?bar
5754 // foo\%bar -> foo%bar
5755 // foo\,bar -> foo,bar
5756 // foo\ bar -> foo bar
5757 // Don't unescape \, * and others that are also special in a
5758 // regexp.
5759 // An escaped { must be unescaped since we use magic not
5760 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (*++p == '?'
5762#ifdef BACKSLASH_IN_FILENAME
5763 && no_bslash
5764#endif
5765 )
5766 reg_pat[i++] = '?';
5767 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005768 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005769 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005770 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005771 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5772 {
5773 reg_pat[i++] = '\\';
5774 reg_pat[i++] = '{';
5775 p += 2;
5776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 else
5778 {
5779 if (allow_dirs != NULL && vim_ispathsep(*p)
5780#ifdef BACKSLASH_IN_FILENAME
5781 && (!no_bslash || *p != '\\')
5782#endif
5783 )
5784 *allow_dirs = TRUE;
5785 reg_pat[i++] = '\\';
Christian Brabandtc9bfed22024-08-29 22:12:05 +02005786 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
5787 reg_pat[i++] = *p++;
Christian Brabandtf459d682024-08-28 23:46:53 +02005788 reg_pat[i++] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 break;
5791#ifdef BACKSLASH_IN_FILENAME
5792 case '/':
5793 reg_pat[i++] = '[';
5794 reg_pat[i++] = '\\';
5795 reg_pat[i++] = '/';
5796 reg_pat[i++] = ']';
5797 if (allow_dirs != NULL)
5798 *allow_dirs = TRUE;
5799 break;
5800#endif
5801 case '{':
5802 reg_pat[i++] = '\\';
5803 reg_pat[i++] = '(';
5804 nested++;
5805 break;
5806 case '}':
5807 reg_pat[i++] = '\\';
5808 reg_pat[i++] = ')';
5809 --nested;
5810 break;
5811 case ',':
5812 if (nested)
5813 {
5814 reg_pat[i++] = '\\';
5815 reg_pat[i++] = '|';
5816 }
5817 else
5818 reg_pat[i++] = ',';
5819 break;
5820 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005821 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005823 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 *allow_dirs = TRUE;
5825 reg_pat[i++] = *p;
5826 break;
5827 }
5828 }
5829 if (add_dollar)
5830 reg_pat[i++] = '$';
5831 reg_pat[i] = NUL;
5832 if (nested != 0)
5833 {
5834 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005835 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005837 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005838 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
5840 return reg_pat;
5841}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005842
5843#if defined(EINTR) || defined(PROTO)
5844/*
5845 * Version of read() that retries when interrupted by EINTR (possibly
5846 * by a SIGWINCH).
5847 */
5848 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005849read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005850{
5851 long ret;
5852
5853 for (;;)
5854 {
5855 ret = vim_read(fd, buf, bufsize);
5856 if (ret >= 0 || errno != EINTR)
5857 break;
5858 }
5859 return ret;
5860}
5861
5862/*
5863 * Version of write() that retries when interrupted by EINTR (possibly
5864 * by a SIGWINCH).
5865 */
5866 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005868{
5869 long ret = 0;
5870 long wlen;
5871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005872 // Repeat the write() so long it didn't fail, other than being interrupted
5873 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005874 while (ret < (long)bufsize)
5875 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005876 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005877 if (wlen < 0)
5878 {
5879 if (errno != EINTR)
5880 break;
5881 }
5882 else
5883 ret += wlen;
5884 }
5885 return ret;
5886}
5887#endif