blob: a2031e96f7f09f29292921cbd18026d095edf02b [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 }
369
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200370 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100372#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200373 /*
374 * On Unix it is possible to read a directory, so we have to
375 * check for it before the mch_open().
376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100378 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
379 && !S_ISFIFO(perm) // ... or fifo
380 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000381# ifdef OPEN_CHR_FILES
382 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100383 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 )
386 {
387 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100388 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200389 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100390 retval = NOTDONE;
391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 else
393 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
394 msg_end();
395 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100396 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200398#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100399#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000400 /*
401 * MS-Windows allows opening a device, but we will probably get stuck
402 * trying to read it.
403 */
404 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
405 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000406 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000407 msg_end();
408 msg_scroll = msg_save;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100409 goto theend;
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000410 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000411#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200412 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200415 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
417 /*
418 * When opening a new file we take the readonly flag from the file.
419 * Default is r/w, can be set to r/o below.
420 * Don't reset it when in readonly mode
421 * Only set/reset b_p_ro when BF_CHECK_RO is set.
422 */
423 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000424 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 curbuf->b_p_ro = FALSE;
426
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200427 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100429 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 if (mch_stat((char *)fname, &st) >= 0)
431 {
432 buf_store_time(curbuf, &st, fname);
433 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100434 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100435#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200436 filesize_disk = st.st_size;
Bram Moolenaar7f291222023-06-17 16:19:30 +0100437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#ifdef UNIX
439 /*
440 * Use the protection bits of the original file for the swap file.
441 * This makes it possible for others to read the name of the
442 * edited file from the swapfile, but only if they can read the
443 * edited file.
444 * Remove the "write" and "execute" bits for group and others
445 * (they must not write the swapfile).
446 * Add the "read" and "write" bits for the user, otherwise we may
447 * not be able to write to the file ourselves.
448 * Setting the bits is done below, after creating the swap file.
449 */
450 swap_mode = (st.st_mode & 0644) | 0600;
451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452#ifdef VMS
453 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000454 curbuf->b_fab_rat = st.st_fab_rat;
455 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456#endif
457 }
458 else
459 {
460 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100461 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100463 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 curbuf->b_orig_size = 0;
465 curbuf->b_orig_mode = 0;
466 }
467
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100468 // Reset the "new file" flag. It will be set again below when the
469 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
471 }
472
473/*
474 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100475 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 */
477 file_readonly = FALSE;
478 if (read_stdin)
479 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100480#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100481 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 setmode(0, O_BINARY);
483#endif
484 }
485 else if (!read_buffer)
486 {
487#ifdef USE_MCH_ACCESS
488 if (
489# ifdef UNIX
490 !(perm & 0222) ||
491# endif
492 mch_access((char *)fname, W_OK))
493 file_readonly = TRUE;
494 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
495#else
496 if (!newfile
497 || readonlymode
498 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
499 {
500 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
503 }
504#endif
505 }
506
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100507 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
509#ifndef UNIX
510 int isdir_f;
511#endif
512 msg_scroll = msg_save;
513#ifndef UNIX
514 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100515 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 */
517 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100518 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (isdir_f)
520 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200521 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100522 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
524 else
525#endif
526 if (newfile)
527 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200528 if (perm < 0
529#ifdef ENOENT
530 && errno == ENOENT
531#endif
532 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
534 /*
535 * Set the 'new-file' flag, so that when the file has
536 * been created by someone else, a ":w" will complain.
537 */
538 curbuf->b_flags |= BF_NEW;
539
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 // Create a swap file now, so that other Vims are warned
541 // that we are editing this file. Don't do this for a
542 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (!bt_dontwrite(curbuf))
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000547 if (curbuf != old_curbuf
548 || (using_b_ffname
549 && (old_b_ffname != curbuf->b_ffname))
550 || (using_b_fname
551 && (old_b_fname != curbuf->b_fname)))
552 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000553 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100554 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000555 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000556 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000557 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200558 filemess(curbuf, sfname,
559 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000560 else
561 filemess(curbuf, sfname,
562 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100564 // Even though this is a new file, it might have been
565 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 check_marks_read();
567#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100568 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200569 if (eap != NULL)
570 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
572 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100573 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 save_file_ff(curbuf);
575
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100576#if defined(FEAT_EVAL)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100577 if (!aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100579 retval = OK; // a new file is not an error
580 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 }
582 else
583 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000584 filemess(curbuf, sfname, (char_u *)(
585# ifdef EFBIG
586 (errno == EFBIG) ? _("[File too big]") :
587# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200588# ifdef EOVERFLOW
589 (errno == EOVERFLOW) ? _("[File too big]") :
590# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000591 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100592 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594 }
595
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100596 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598
599 /*
600 * Only set the 'ro' flag for readonly files the first time they are
601 * loaded. Help files always get readonly mode
602 */
603 if ((check_readonly && file_readonly) || curbuf->b_help)
604 curbuf->b_p_ro = TRUE;
605
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000606 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100608 // Don't change 'eol' if reading from buffer as it will already be
609 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000610 if (!read_buffer)
611 {
Bram Moolenaarfb0cf232022-10-22 11:25:19 +0100612 curbuf->b_p_eof = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100613 curbuf->b_start_eof = FALSE;
614 curbuf->b_p_eol = TRUE;
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000615 curbuf->b_start_eol = TRUE;
616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000618 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
620
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 // Create a swap file now, so that other Vims are warned that we are
622 // editing this file.
623 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (!bt_dontwrite(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 {
626 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000627 if (!read_stdin && (curbuf != old_curbuf
628 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
629 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
630 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000631 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000632 if (!read_buffer)
633 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100634 goto theend;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000638 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
639 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100640 {
641 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
642
643 /*
644 * If the group-read bit is set but not the world-read bit, then
645 * the group must be equal to the group of the original file. If
646 * we can't make that happen then reset the group-read bit. This
647 * avoids making the swap file readable to more users when the
648 * primary group of the user is too permissive.
649 */
650 if ((swap_mode & 044) == 040)
651 {
652 stat_T swap_st;
653
654 if (mch_stat((char *)swap_fname, &swap_st) >= 0
655 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200656# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100657 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200658 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200659# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200660 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100661 swap_mode &= 0600;
662 }
663
664 (void)mch_setperm(swap_fname, (long)swap_mode);
665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667 }
668
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200669 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (swap_exists_action == SEA_QUIT)
671 {
672 if (!read_buffer && !read_stdin)
673 close(fd);
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100674 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 /*
680 * Set '[ mark to the line above where the lines go (line 1 if zero).
681 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100682 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
684 curbuf->b_op_start.col = 0;
685
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100686 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
687 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
688 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (!read_buffer)
691 {
692 int m = msg_scroll;
693 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 /*
696 * The file must be closed again, the autocommands may want to change
697 * the file before reading it.
698 */
699 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702 /*
703 * The output from the autocommands should not overwrite anything and
704 * should not be overwritten: Set msg_scroll, restore its value if no
705 * output was done.
706 */
707 msg_scroll = TRUE;
708 if (filtering)
709 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
710 FALSE, curbuf, eap);
711 else if (read_stdin)
712 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
713 FALSE, curbuf, eap);
714 else if (newfile)
715 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
716 FALSE, curbuf, eap);
717 else
718 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
719 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100720 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100721 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
722 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
723 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100724 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (msg_scrolled == n)
727 msg_scroll = m;
728
729#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100730 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
732 --no_wait_return;
733 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100734 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100735 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737#endif
738 /*
739 * Don't allow the autocommands to change the current buffer.
740 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000741 *
742 * Don't allow the autocommands to change the buffer name either
743 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
745 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000746 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
747 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
749 {
750 --no_wait_return;
751 msg_scroll = msg_save;
752 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000753 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000755 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100756 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100757 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100761 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
763
764 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
765 {
766 /*
767 * Show the user that we are busy reading the input. Sometimes this
768 * may take a while. When reading from stdin another program may
769 * still be running, don't move the cursor to the last line, unless
770 * always using the GUI.
771 */
772 if (read_stdin)
773 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100774 if (!is_not_a_term())
775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200777# ifdef VIMDLL
778 if (!gui.in_use)
779# endif
780 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781#endif
782#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100783 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100784 if (gui.in_use && !gui.dying && !gui.starting)
785 {
John Marriott14ede182024-12-29 16:14:19 +0100786 size_t plen = STRLEN(_("Reading from stdin..."));
787
Amon Sha10197932022-02-21 15:07:12 +0000788 // make a copy, gui_write() may try to change it
John Marriott14ede182024-12-29 16:14:19 +0100789 p = vim_strnsave((char_u *)_("Reading from stdin..."), plen);
Amon Sha10197932022-02-21 15:07:12 +0000790 if (p != NULL)
791 {
John Marriott14ede182024-12-29 16:14:19 +0100792 gui_write(p, (int)plen);
Amon Sha10197932022-02-21 15:07:12 +0000793 vim_free(p);
794 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799 else if (!read_buffer)
800 filemess(curbuf, sfname, (char_u *)"", 0);
801 }
802
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /*
806 * Set linecnt now, before the "retry" caused by a wrong guess for
807 * fileformat, and after the autocommands, which may change them.
808 */
809 linecnt = curbuf->b_ml.ml_line_count;
810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000812 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000813 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000814 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000815 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000816 curbuf->b_bad_char = eap->bad_char;
817 }
818 else
819 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000822 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 */
824 if (eap != NULL && eap->force_enc != 0)
825 {
826 fenc = enc_canonize(eap->cmd + eap->force_enc);
827 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000828 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830 else if (curbuf->b_p_bin)
831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 fenc_alloced = FALSE;
834 }
835 else if (curbuf->b_help)
836 {
837 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000838 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Help files are either utf-8 or latin1. Try utf-8 first, if this
841 // fails it must be latin1.
842 // Always do this when 'encoding' is "utf-8". Otherwise only do
843 // this when needed to avoid [converted] remarks all the time.
844 // It is needed when the first line contains non-ASCII characters.
845 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 fenc = (char_u *)"latin1";
847 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000848 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
John Marriott14ede182024-12-29 16:14:19 +0100850 fc = fname[fnamelen - 1];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000851 if (TOLOWER_ASC(fc) == 'x')
852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100853 // Read the first line (and a bit more). Immediately rewind to
854 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100855 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200856 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000857 for (p = firstline; p < firstline + len; ++p)
858 if (*p >= 0x80)
859 {
860 c = TRUE;
861 break;
862 }
863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865
866 if (c)
867 {
868 fenc_next = fenc;
869 fenc = (char_u *)"utf-8";
870
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100871 // When the file is utf-8 but a character doesn't fit in
872 // 'encoding' don't retry. In help text editing utf-8 bytes
873 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000874 if (!enc_utf8)
875 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877 fenc_alloced = FALSE;
878 }
879 else if (*p_fencs == NUL)
880 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100881 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 fenc_alloced = FALSE;
883 }
884 else
885 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100886 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200887 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /*
891 * Jump back here to retry reading the file in different ways.
892 * Reasons to retry:
893 * - encoding conversion failed: try another one from "fenc_next"
894 * - BOM detected and fenc was set, need to setup conversion
895 * - "fileformat" check failed: try another
896 *
897 * Variables set for special retry actions:
898 * "file_rewind" Rewind the file to start reading it again.
899 * "advance_fenc" Advance "fenc" using "fenc_next".
900 * "skip_read" Re-use already read bytes (BOM detected).
901 * "did_iconv" iconv() conversion failed, try 'charconvert'.
902 * "keep_fileformat" Don't reset "fileformat".
903 *
904 * Other status indicators:
905 * "tmpname" When != NULL did conversion with 'charconvert'.
906 * Output file has to be deleted afterwards.
907 * "iconv_fd" When != -1 did conversion with iconv().
908 */
909retry:
910
911 if (file_rewind)
912 {
913 if (read_buffer)
914 {
915 read_buf_lnum = 1;
916 read_buf_col = 0;
917 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200918 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100920 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 error = TRUE;
922 goto failed;
923 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100924 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200926 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000928 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000931 curbuf->b_start_bomb = FALSE;
932 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000933 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935
936 /*
937 * When retrying with another "fenc" and the first time "fileformat"
938 * will be reset.
939 */
940 if (keep_fileformat)
941 keep_fileformat = FALSE;
942 else
943 {
944 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000947 try_unix = try_dos = try_mac = FALSE;
948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100950 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100952 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100954 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956
Bram Moolenaar13505972019-01-24 15:04:48 +0100957#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (iconv_fd != (iconv_t)-1)
959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100960 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 iconv_close(iconv_fd);
962 iconv_fd = (iconv_t)-1;
963 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 if (advance_fenc)
967 {
968 /*
969 * Try the next entry in 'fileencodings'.
970 */
971 advance_fenc = FALSE;
972
973 if (eap != NULL && eap->force_enc != 0)
974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100975 // Conversion given with "++cc=" wasn't possible, read
976 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000978 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 if (fenc_alloced)
980 vim_free(fenc);
981 fenc = (char_u *)"";
982 fenc_alloced = FALSE;
983 }
984 else
985 {
986 if (fenc_alloced)
987 vim_free(fenc);
988 if (fenc_next != NULL)
989 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200990 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992 else
993 {
994 fenc = (char_u *)"";
995 fenc_alloced = FALSE;
996 }
997 }
998 if (tmpname != NULL)
999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001000 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01001001 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 }
1004
1005 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001006 * Conversion may be required when the encoding of the file is different
1007 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 */
1009 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001010 converted = need_conversion(fenc);
1011 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001013 // "ucs-bom" means we need to check the first bytes of the file
1014 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (STRCMP(fenc, ENC_UCSBOM) == 0)
1016 fio_flags = FIO_UCSBOM;
1017
1018 /*
1019 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1020 * done. This is handled below after read(). Prepare the
1021 * fio_flags to avoid having to parse the string each time.
1022 * Also check for Unicode to Latin1 conversion, because iconv()
1023 * appears not to handle this correctly. This works just like
1024 * conversion to UTF-8 except how the resulting character is put in
1025 * the buffer.
1026 */
1027 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1028 fio_flags = get_fio_flags(fenc);
1029
Bram Moolenaar4f974752019-02-17 17:44:42 +01001030#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 /*
1032 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1033 * is handled with MultiByteToWideChar().
1034 */
1035 if (fio_flags == 0)
1036 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaar13505972019-01-24 15:04:48 +01001039#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (fio_flags == 0)
1042 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar13505972019-01-24 15:04:48 +01001045#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 /*
1047 * Try using iconv() if we can't convert internally.
1048 */
1049 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001050# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 )
1054 iconv_fd = (iconv_t)my_iconv_open(
1055 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar13505972019-01-24 15:04:48 +01001058#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 /*
1060 * Use the 'charconvert' expression when conversion is required
1061 * and we can't do it internally or with iconv().
1062 */
1063 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001064 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001065# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 )
1069 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001070# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001072# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // Skip conversion when it's already done (retry for wrong
1074 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (tmpname == NULL)
1076 {
1077 tmpname = readfile_charconvert(fname, fenc, &fd);
1078 if (tmpname == NULL)
1079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 advance_fenc = TRUE;
1082 if (fd < 0)
1083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001085 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 error = TRUE;
1087 goto failed;
1088 }
1089 goto retry;
1090 }
1091 }
1092 }
1093 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
1096 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001097#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 )
1101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001102 // Conversion wanted but we can't.
1103 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 advance_fenc = TRUE;
1105 goto retry;
1106 }
1107 }
1108 }
1109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // Set "can_retry" when it's possible to rewind the file and try with
1111 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1112 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001113 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
1115 if (!skip_read)
1116 {
1117 linerest = 0;
1118 filesize = 0;
Bram Moolenaar7f291222023-06-17 16:19:30 +01001119#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001120 filesize_count = 0;
Bram Moolenaar7f291222023-06-17 16:19:30 +01001121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 skip_count = lines_to_skip;
1123 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001125#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001126 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1127 && curbuf->b_ffname != NULL
1128 && curbuf->b_p_udf
1129 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001130 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001131 && !read_stdin
1132 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001133 if (read_undo_file)
1134 sha256_start(&sha_ctx);
1135#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001136#ifdef FEAT_CRYPT
1137 if (curbuf->b_cryptstate != NULL)
1138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Need to free the state, but keep the key, don't want to ask for
1140 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001141 crypt_free_state(curbuf->b_cryptstate);
1142 curbuf->b_cryptstate = NULL;
1143 }
1144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146
1147 while (!error && !got_int)
1148 {
1149 /*
1150 * We allocate as much space for the file as we can get, plus
1151 * space for the old line plus room for one terminating NUL.
1152 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001153 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001155 if (!skip_read)
1156 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001157#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001158 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001159#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001160 // Use buffer >= 64K. Add linerest to double the size if the
1161 // line gets very long, to avoid a lot of copying. But don't
1162 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001163 size = 0x10000L + linerest;
1164 if (size > 0x100000L)
1165 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001166#endif
1167 }
1168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001169 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001170 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001173 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 size = 1;
1175 }
1176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
1178 if (!skip_read)
1179 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001180 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001182 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 FALSE)) != NULL)
1184 break;
1185 }
1186 if (new_buffer == NULL)
1187 {
1188 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1189 error = TRUE;
1190 break;
1191 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001192 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1194 vim_free(buffer);
1195 buffer = new_buffer;
1196 ptr = buffer + linerest;
1197 line_start = buffer;
1198
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001199 // May need room to translate into.
1200 // For iconv() we don't really know the required space, use a
1201 // factor ICONV_MULT.
1202 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1203 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1204 // become up to 4 bytes, size must be multiple of 2
1205 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1206 // multiple of 2
1207 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1208 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001209 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001210#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (iconv_fd != (iconv_t)-1)
1212 size = size / ICONV_MULT;
1213 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (fio_flags & FIO_LATIN1)
1216 size = size / 2;
1217 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1218 size = (size * 2 / 3) & ~1;
1219 else if (fio_flags & FIO_UCS4)
1220 size = (size * 2 / 3) & ~3;
1221 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001222 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001223#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001225 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001226#endif
1227#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001229 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#endif
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (conv_restlen > 0)
1233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001234 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 mch_memmove(ptr, conv_rest, conv_restlen);
1236 ptr += conv_restlen;
1237 size -= conv_restlen;
1238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 if (read_buffer)
1241 {
1242 /*
1243 * Read bytes from curbuf. Used for converting text read
1244 * from stdin.
1245 */
1246 if (read_buf_lnum > from)
1247 size = 0;
1248 else
1249 {
1250 int n, ni;
1251 long tlen;
1252
1253 tlen = 0;
1254 for (;;)
1255 {
1256 p = ml_get(read_buf_lnum) + read_buf_col;
zeertzjq94b7c322024-03-12 21:50:32 +01001257 n = ml_get_len(read_buf_lnum) - read_buf_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if ((int)tlen + n + 1 > size)
1259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001260 // Filled up to "size", append partial line.
1261 // Change NL to NUL to reverse the effect done
1262 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001263 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 for (ni = 0; ni < n; ++ni)
1265 {
1266 if (p[ni] == NL)
1267 ptr[tlen++] = NUL;
1268 else
1269 ptr[tlen++] = p[ni];
1270 }
1271 read_buf_col += n;
1272 break;
1273 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001274
1275 // Append whole line and new-line. Change NL
1276 // to NUL to reverse the effect done below.
1277 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001279 if (p[ni] == NL)
1280 ptr[tlen++] = NUL;
1281 else
1282 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001284 ptr[tlen++] = NL;
1285 read_buf_col = 0;
1286 if (++read_buf_lnum > from)
1287 {
1288 // When the last line didn't have an
1289 // end-of-line don't add it now either.
1290 if (!curbuf->b_p_eol)
1291 --tlen;
1292 size = tlen;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001293#ifdef FEAT_CRYPT
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001294 eof = TRUE;
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001295#endif
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001296 break;
1297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 }
1301 else
1302 {
1303 /*
1304 * Read bytes from the file.
1305 */
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001306#ifdef FEAT_SODIUM
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001307 // Let the crypt layer work with a buffer size of 8192
Christian Brabandtaae58342023-04-23 17:50:22 +01001308 //
1309 // Sodium encryption requires a fixed block size to
1310 // successfully decrypt. However, unfortunately the file
1311 // header size changes between xchacha20 and xchacha20v2 by
1312 // 'add_len' bytes.
1313 // So we will now read the maximum header size + encryption
1314 // metadata, but after determining to read an xchacha20
1315 // encrypted file, we have to rewind the file descriptor by
1316 // 'add_len' bytes in the second round.
1317 //
1318 // Be careful with changing it, it needs to stay the same
1319 // for reading back previously encrypted files!
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001320 if (filesize == 0)
Christian Brabandtaae58342023-04-23 17:50:22 +01001321 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001322 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001323 size = WRITEBUFSIZE + crypt_get_max_header_len()
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001324 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1325 + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001326 may_need_lseek = TRUE;
1327 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001328
Christian Brabandtaae58342023-04-23 17:50:22 +01001329 else if (filesize > 0 && (curbuf->b_cryptstate != NULL
1330 && crypt_method_is_sodium(
1331 curbuf->b_cryptstate->method_nr)))
1332 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001333 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
Christian Brabandtaae58342023-04-23 17:50:22 +01001334 // need to rewind by - add_len from CRYPT_M_SOD2 (see
1335 // description above)
1336 if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
1337 && !eof && may_need_lseek)
1338 {
1339 lseek(fd, crypt_get_header_len(
1340 curbuf->b_cryptstate->method_nr)
1341 - crypt_get_max_header_len(), SEEK_CUR);
1342 may_need_lseek = FALSE;
1343 }
1344 }
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001345#endif
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001346 long read_size = size;
1347 size = read_eintr(fd, ptr, read_size);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001348#ifdef FEAT_CRYPT
Bram Moolenaar7f291222023-06-17 16:19:30 +01001349 // Did we reach end of file?
1350 filesize_count += size;
Bram Moolenaar438d0c52023-06-17 15:00:27 +01001351 eof = (size < read_size || filesize_count == filesize_disk);
Bram Moolenaarbc385a12023-06-17 15:35:03 +01001352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001355#ifdef FEAT_CRYPT
1356 /*
1357 * At start of file: Check for magic number of encryption.
1358 */
1359 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001360 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001361 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1362 &filesize, newfile, sfname,
1363 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001364# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001365 if (curbuf->b_cryptstate != NULL
1366 && !crypt_works_inplace(curbuf->b_cryptstate))
1367 // reading undo file requires crypt_decode_inplace()
1368 read_undo_file = FALSE;
1369# endif
1370 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001371 /*
1372 * Decrypt the read bytes. This is done before checking for
1373 * EOF because the crypt layer may be buffering.
1374 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001375 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1376 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001378# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001379 if (crypt_works_inplace(curbuf->b_cryptstate))
1380 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001381# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001382 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1383 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001384# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001385 }
1386 else
1387 {
1388 char_u *newptr = NULL;
1389 int decrypted_size;
1390
1391 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001392 curbuf->b_cryptstate, ptr, size,
1393 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001394
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001395 if (decrypted_size < 0)
1396 {
1397 // error message already given
1398 error = TRUE;
1399 vim_free(newptr);
1400 break;
1401 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001402 // If the crypt layer is buffering, not producing
1403 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001404 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001405 continue;
1406
1407 if (linerest == 0)
1408 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001409 // Simple case: reuse returned buffer (may be
1410 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001411 new_buffer = newptr;
1412 }
1413 else
1414 {
1415 long_u new_size;
1416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001417 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001418 new_size = (long_u)(decrypted_size + linerest + 1);
1419 new_buffer = lalloc(new_size, FALSE);
1420 if (new_buffer == NULL)
1421 {
1422 do_outofmem_msg(new_size);
1423 error = TRUE;
1424 break;
1425 }
1426
1427 mch_memmove(new_buffer, buffer, linerest);
1428 if (newptr != NULL)
1429 mch_memmove(new_buffer + linerest, newptr,
1430 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001431 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001432 }
1433
1434 if (new_buffer != NULL)
1435 {
1436 vim_free(buffer);
1437 buffer = new_buffer;
1438 new_buffer = NULL;
1439 line_start = buffer;
1440 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001441 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001442 }
1443 size = decrypted_size;
1444 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001445# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001446 }
1447#endif
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (size <= 0)
1450 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001451 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001454 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001455 /*
1456 * Reached end-of-file but some trailing bytes could
1457 * not be converted. Truncated file?
1458 */
1459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001460 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001461 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001462#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001463 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001464#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001465 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001466 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001467 if (can_retry)
1468 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001469 if (conv_error == 0)
1470 conv_error = curbuf->b_ml.ml_line_count
1471 - linecnt + 1;
1472 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001473 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001474 else if (illegal_byte == 0)
1475 illegal_byte = curbuf->b_ml.ml_line_count
1476 - linecnt + 1;
1477 if (bad_char_behavior == BAD_DROP)
1478 {
1479 *(ptr - conv_restlen) = NUL;
1480 conv_restlen = 0;
1481 }
1482 else
1483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001484 // Replace the trailing bytes with the replacement
1485 // character if we were converting; if we weren't,
1486 // leave the UTF8 checking code to do it, as it
1487 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001488 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001489#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001490 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001491#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001492 ))
1493 {
1494 while (conv_restlen > 0)
1495 {
1496 *(--ptr) = bad_char_behavior;
1497 --conv_restlen;
1498 }
1499 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001501#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001502 if (iconv_fd != (iconv_t)-1)
1503 {
1504 iconv_close(iconv_fd);
1505 iconv_fd = (iconv_t)-1;
1506 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001507#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001508 }
1509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 skip_read = FALSE;
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /*
1515 * At start of file (or after crypt magic number): Check for BOM.
1516 * Also check for a BOM for other Unicode encodings, but not after
1517 * converting with 'charconvert' or when a BOM has already been
1518 * found.
1519 */
1520 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001521#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001522 || (cryptkey != NULL
1523 && filesize == crypt_get_header_len(
1524 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 )
1527 && (fio_flags == FIO_UCSBOM
1528 || (!curbuf->b_p_bomb
1529 && tmpname == NULL
1530 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1531 {
1532 char_u *ccname;
1533 int blen;
1534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001535 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (size < 2 || curbuf->b_p_bin)
1537 ccname = NULL;
1538 else
1539 ccname = check_for_bom(ptr, size, &blen,
1540 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1541 if (ccname != NULL)
1542 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001543 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 filesize += blen;
1545 size -= blen;
1546 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001547 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001550 curbuf->b_start_bomb = TRUE;
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553
1554 if (fio_flags == FIO_UCSBOM)
1555 {
1556 if (ccname == NULL)
1557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001558 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 advance_fenc = TRUE;
1560 }
1561 else
1562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001563 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if (fenc_alloced)
1565 vim_free(fenc);
1566 fenc = ccname;
1567 fenc_alloced = FALSE;
1568 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 skip_read = TRUE;
1571 goto retry;
1572 }
1573 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001575 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001576 ptr -= conv_restlen;
1577 size += conv_restlen;
1578 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /*
1580 * Break here for a read error or end-of-file.
1581 */
1582 if (size <= 0)
1583 break;
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaar13505972019-01-24 15:04:48 +01001586#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (iconv_fd != (iconv_t)-1)
1588 {
1589 /*
1590 * Attempt conversion of the read bytes to 'encoding' using
1591 * iconv().
1592 */
1593 const char *fromp;
1594 char *top;
1595 size_t from_size;
1596 size_t to_size;
1597
1598 fromp = (char *)ptr;
1599 from_size = size;
1600 ptr += size;
1601 top = (char *)ptr;
1602 to_size = real_size - size;
1603
1604 /*
1605 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001606 * another conversion. Except for when there is no
1607 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001609 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1610 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1612 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001613 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001614 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001615 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001616 if (conv_error == 0)
1617 conv_error = readfile_linenr(linecnt,
1618 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001620 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001621 ++fromp;
1622 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001623 if (bad_char_behavior == BAD_KEEP)
1624 {
1625 *top++ = *(fromp - 1);
1626 --to_size;
1627 }
1628 else if (bad_char_behavior != BAD_DROP)
1629 {
1630 *top++ = bad_char_behavior;
1631 --to_size;
1632 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
1635 if (from_size > 0)
1636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001637 // Some remaining characters, keep them for the next
1638 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1640 conv_restlen = (int)from_size;
1641 }
1642
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001643 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 line_start = ptr - linerest;
1645 mch_memmove(line_start, buffer, (size_t)linerest);
1646 size = (long)((char_u *)top - ptr);
1647 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
Bram Moolenaar4f974752019-02-17 17:44:42 +01001650#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (fio_flags & FIO_CODEPAGE)
1652 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001653 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001654 WCHAR ucs2buf[3];
1655 int ucs2len;
1656 int codepage = FIO_GET_CP(fio_flags);
1657 int bytelen;
1658 int found_bad;
1659 char replstr[2];
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 /*
1662 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001663 * a codepage, using standard MS-Windows functions. This
1664 * requires two steps:
1665 * 1. convert from 'fileencoding' to ucs-2
1666 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001668 * Because there may be illegal bytes AND an incomplete byte
1669 * sequence at the end, we may have to do the conversion one
1670 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001674 if (bad_char_behavior > 0)
1675 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001677 replstr[0] = '?';
1678 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001681 * Move the bytes to the end of the buffer, so that we have
1682 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001684 src = ptr + real_size - size;
1685 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001687 /*
1688 * Do the conversion.
1689 */
1690 dst = ptr;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001691 while (size > 0)
1692 {
1693 found_bad = FALSE;
1694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001696 if (codepage == CP_UTF8)
1697 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001698 // Handle CP_UTF8 input ourselves to be able to handle
1699 // trailing bytes properly.
1700 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001701 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001702 if (bytelen > size)
1703 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001704 // Only got some bytes of a character. Normally
1705 // it's put in "conv_rest", but if it's too long
1706 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001707 if (bytelen <= CONV_RESTLEN)
1708 break;
1709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001710 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001711 bytelen = size;
1712 found_bad = TRUE;
1713 }
1714 else
1715 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001716 int u8c = utf_ptr2char(src);
1717
Bram Moolenaar86e01082005-12-29 22:45:34 +00001718 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001719 found_bad = TRUE;
1720 ucs2buf[0] = u8c;
1721 ucs2len = 1;
1722 }
1723 }
1724 else
1725# endif
1726 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001727 // We don't know how long the byte sequence is, try
1728 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001729 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1730 ++bytelen)
1731 {
1732 ucs2len = MultiByteToWideChar(codepage,
1733 MB_ERR_INVALID_CHARS,
1734 (LPCSTR)src, bytelen,
1735 ucs2buf, 3);
1736 if (ucs2len > 0)
1737 break;
1738 }
1739 if (ucs2len == 0)
1740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001741 // If we have only one byte then it's probably an
1742 // incomplete byte sequence. Otherwise discard
1743 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001744 if (size == 1)
1745 break;
1746 found_bad = TRUE;
1747 bytelen = 1;
1748 }
1749 }
1750
1751 if (!found_bad)
1752 {
1753 int i;
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001756 if (enc_utf8)
1757 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001759 for (i = 0; i < ucs2len; ++i)
1760 dst += utf_char2bytes(ucs2buf[i], dst);
1761 }
1762 else
1763 {
1764 BOOL bad = FALSE;
1765 int dstlen;
1766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001767 // From UCS-2 to "enc_codepage". If the
1768 // conversion uses the default character "?",
1769 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001770 dstlen = WideCharToMultiByte(enc_codepage, 0,
1771 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001772 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001773 replstr, &bad);
1774 if (bad)
1775 found_bad = TRUE;
1776 else
1777 dst += dstlen;
1778 }
1779 }
1780
1781 if (found_bad)
1782 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001783 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001784 if (can_retry)
1785 goto rewind_retry;
1786 if (conv_error == 0)
1787 conv_error = readfile_linenr(linecnt, ptr, dst);
1788 if (bad_char_behavior != BAD_DROP)
1789 {
1790 if (bad_char_behavior == BAD_KEEP)
1791 {
1792 mch_memmove(dst, src, bytelen);
1793 dst += bytelen;
1794 }
1795 else
1796 *dst++ = bad_char_behavior;
1797 }
1798 }
1799
1800 src += bytelen;
1801 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001803
1804 if (size > 0)
1805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001807 mch_memmove(conv_rest, src, size);
1808 conv_restlen = size;
1809 }
1810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001811 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001812 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001815#endif
1816#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (fio_flags & FIO_MACROMAN)
1818 {
1819 /*
1820 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001821 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001823 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (fio_flags != 0)
1829 {
1830 int u8c;
1831 char_u *dest;
1832 char_u *tail = NULL;
1833
1834 /*
1835 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1836 * "enc_utf8" not set: Convert Unicode to Latin1.
1837 * Go from end to start through the buffer, because the number
1838 * of bytes may increase.
1839 * "dest" points to after where the UTF-8 bytes go, "p" points
1840 * to after the next character to convert.
1841 */
1842 dest = ptr + real_size;
1843 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1844 {
1845 p = ptr + size;
1846 if (fio_flags == FIO_UTF8)
1847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001848 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 tail = ptr + size - 1;
1850 while (tail > ptr && (*tail & 0xc0) == 0x80)
1851 --tail;
1852 if (tail + utf_byte2len(*tail) <= ptr + size)
1853 tail = NULL;
1854 else
1855 p = tail;
1856 }
1857 }
1858 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1859 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001860 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 p = ptr + (size & ~1);
1862 if (size & 1)
1863 tail = p;
1864 if ((fio_flags & FIO_UTF16) && p > ptr)
1865 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001866 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (fio_flags & FIO_ENDIAN_L)
1868 {
1869 u8c = (*--p << 8);
1870 u8c += *--p;
1871 }
1872 else
1873 {
1874 u8c = *--p;
1875 u8c += (*--p << 8);
1876 }
1877 if (u8c >= 0xd800 && u8c <= 0xdbff)
1878 tail = p;
1879 else
1880 p += 2;
1881 }
1882 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001883 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 p = ptr + (size & ~3);
1887 if (size & 3)
1888 tail = p;
1889 }
1890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001891 // If there is a trailing incomplete sequence move it to
1892 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (tail != NULL)
1894 {
1895 conv_restlen = (int)((ptr + size) - tail);
1896 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1897 size -= conv_restlen;
1898 }
1899
1900
1901 while (p > ptr)
1902 {
1903 if (fio_flags & FIO_LATIN1)
1904 u8c = *--p;
1905 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1906 {
1907 if (fio_flags & FIO_ENDIAN_L)
1908 {
1909 u8c = (*--p << 8);
1910 u8c += *--p;
1911 }
1912 else
1913 {
1914 u8c = *--p;
1915 u8c += (*--p << 8);
1916 }
1917 if ((fio_flags & FIO_UTF16)
1918 && u8c >= 0xdc00 && u8c <= 0xdfff)
1919 {
1920 int u16c;
1921
1922 if (p == ptr)
1923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001924 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (can_retry)
1926 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001927 if (conv_error == 0)
1928 conv_error = readfile_linenr(linecnt,
1929 ptr, p);
1930 if (bad_char_behavior == BAD_DROP)
1931 continue;
1932 if (bad_char_behavior != BAD_KEEP)
1933 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
1935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // found second word of double-word, get the first
1937 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (fio_flags & FIO_ENDIAN_L)
1939 {
1940 u16c = (*--p << 8);
1941 u16c += *--p;
1942 }
1943 else
1944 {
1945 u16c = *--p;
1946 u16c += (*--p << 8);
1947 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001948 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1949 + (u8c & 0x3ff);
1950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001951 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (u16c < 0xd800 || u16c > 0xdbff)
1953 {
1954 if (can_retry)
1955 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001956 if (conv_error == 0)
1957 conv_error = readfile_linenr(linecnt,
1958 ptr, p);
1959 if (bad_char_behavior == BAD_DROP)
1960 continue;
1961 if (bad_char_behavior != BAD_KEEP)
1962 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
1966 else if (fio_flags & FIO_UCS4)
1967 {
1968 if (fio_flags & FIO_ENDIAN_L)
1969 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001970 u8c = (unsigned)*--p << 24;
1971 u8c += (unsigned)*--p << 16;
1972 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 u8c += *--p;
1974 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001978 u8c += (unsigned)*--p << 8;
1979 u8c += (unsigned)*--p << 16;
1980 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001983 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 if (*--p < 0x80)
1986 u8c = *p;
1987 else
1988 {
1989 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001990 p -= len;
1991 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (len == 0)
1993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // Not a valid UTF-8 character, retry with
1995 // another fenc when possible, otherwise just
1996 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (can_retry)
1998 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001999 if (conv_error == 0)
2000 conv_error = readfile_linenr(linecnt,
2001 ptr, p);
2002 if (bad_char_behavior == BAD_DROP)
2003 continue;
2004 if (bad_char_behavior != BAD_KEEP)
2005 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 dest -= utf_char2len(u8c);
2012 (void)utf_char2bytes(u8c, dest);
2013 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 --dest;
2017 if (u8c >= 0x100)
2018 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 // character doesn't fit in latin1, retry with
2020 // another fenc when possible, otherwise just
2021 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002022 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002024 if (conv_error == 0)
2025 conv_error = readfile_linenr(linecnt, ptr, p);
2026 if (bad_char_behavior == BAD_DROP)
2027 ++dest;
2028 else if (bad_char_behavior == BAD_KEEP)
2029 *dest = u8c;
2030 else if (eap != NULL && eap->bad_char != 0)
2031 *dest = bad_char_behavior;
2032 else
2033 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 else
2036 *dest = u8c;
2037 }
2038 }
2039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002040 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 line_start = dest - linerest;
2042 mch_memmove(line_start, buffer, (size_t)linerest);
2043 size = (long)((ptr + real_size) - dest);
2044 ptr = dest;
2045 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002046 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002048 int incomplete_tail = FALSE;
2049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002050 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002051 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002053 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002054 int l;
2055
2056 if (todo <= 0)
2057 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (*p >= 0x80)
2059 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002060 // A length of 1 means it's an illegal byte. Accept
2061 // an incomplete character at the end though, the next
2062 // read() will get the next bytes, we'll check it
2063 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002064 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002065 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002067 // Avoid retrying with a different encoding when
2068 // a truncated file is more likely, or attempting
2069 // to read the rest of an incomplete sequence when
2070 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002071 if (p > ptr || filesize > 0)
2072 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002073 // Incomplete byte sequence, move it to conv_rest[]
2074 // and try to read the rest of it, unless we've
2075 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002076 if (p > ptr)
2077 {
2078 conv_restlen = todo;
2079 mch_memmove(conv_rest, p, conv_restlen);
2080 size -= conv_restlen;
2081 break;
2082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002084 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // Illegal byte. If we can try another encoding
2087 // do that, unless at EOF where a truncated
2088 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002089 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002090 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002091#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002092 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002093 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2094 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002095#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002096 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002097 if (conv_error == 0 && illegal_byte == 0)
2098 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002101 if (bad_char_behavior == BAD_DROP)
2102 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002103 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002104 --p;
2105 --size;
2106 }
2107 else if (bad_char_behavior != BAD_KEEP)
2108 *p = bad_char_behavior;
2109 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002110 else
2111 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
2113 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002114 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002119#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002120 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002121 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002122 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002124#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002126 advance_fenc = TRUE;
2127 file_rewind = TRUE;
2128 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 filesize += size;
2134
2135 /*
2136 * when reading the first part of a file: guess EOL type
2137 */
2138 if (fileformat == EOL_UNKNOWN)
2139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (try_dos || try_unix)
2142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002143 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002144 if (try_mac)
2145 try_mac = 1;
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 for (p = ptr; p < ptr + size; ++p)
2148 {
2149 if (*p == NL)
2150 {
2151 if (!try_unix
2152 || (try_dos && p > ptr && p[-1] == CAR))
2153 fileformat = EOL_DOS;
2154 else
2155 fileformat = EOL_UNIX;
2156 break;
2157 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002158 else if (*p == CAR && try_mac)
2159 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (fileformat == EOL_UNIX && try_mac)
2164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002165 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 try_mac = 1;
2167 try_unix = 1;
2168 for (; p >= ptr && *p != CAR; p--)
2169 ;
2170 if (p >= ptr)
2171 {
2172 for (p = ptr; p < ptr + size; ++p)
2173 {
2174 if (*p == NL)
2175 try_unix++;
2176 else if (*p == CAR)
2177 try_mac++;
2178 }
2179 if (try_mac > try_unix)
2180 fileformat = EOL_MAC;
2181 }
2182 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002183 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002184 // Looking for CR but found no end-of-line markers at
2185 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002186 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
2188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002189 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (fileformat == EOL_UNKNOWN && try_mac)
2191 fileformat = EOL_MAC;
2192
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (fileformat == EOL_UNKNOWN)
2195 fileformat = default_fileformat();
2196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002197 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 set_fileformat(fileformat, OPT_LOCAL);
2200 }
2201 }
2202
2203 /*
2204 * This loop is executed once for every character read.
2205 * Keep it fast!
2206 */
2207 if (fileformat == EOL_MAC)
2208 {
2209 --ptr;
2210 while (++ptr, --size >= 0)
2211 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002212 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if ((c = *ptr) != NUL && c != CAR && c != NL)
2214 continue;
2215 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002216 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 else
2220 {
2221 if (skip_count == 0)
2222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 len = (colnr_T) (ptr - line_start + 1);
2225 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2226 {
2227 error = TRUE;
2228 break;
2229 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002230#ifdef FEAT_PERSISTENT_UNDO
2231 if (read_undo_file)
2232 sha256_update(&sha_ctx, line_start, len);
2233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 ++lnum;
2235 if (--read_count == 0)
2236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002237 error = TRUE; // break loop
2238 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 break;
2240 }
2241 }
2242 else
2243 --skip_count;
2244 line_start = ptr + 1;
2245 }
2246 }
2247 }
2248 else
2249 {
2250 --ptr;
2251 while (++ptr, --size >= 0)
2252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 continue;
2255 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002256 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 else
2258 {
2259 if (skip_count == 0)
2260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 len = (colnr_T)(ptr - line_start + 1);
2263 if (fileformat == EOL_DOS)
2264 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002265 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 ptr[-1] = NUL;
2269 --len;
2270 }
2271 /*
2272 * Reading in Dos format, but no CR-LF found!
2273 * When 'fileformats' includes "unix", delete all
2274 * the lines read so far and start all over again.
2275 * Otherwise give an error message later.
2276 */
2277 else if (ff_error != EOL_DOS)
2278 {
2279 if ( try_unix
2280 && !read_stdin
2281 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002282 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2283 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002286 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 set_fileformat(EOL_UNIX, OPT_LOCAL);
2288 file_rewind = TRUE;
2289 keep_fileformat = TRUE;
2290 goto retry;
2291 }
2292 ff_error = EOL_DOS;
2293 }
2294 }
2295 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2296 {
2297 error = TRUE;
2298 break;
2299 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002300#ifdef FEAT_PERSISTENT_UNDO
2301 if (read_undo_file)
2302 sha256_update(&sha_ctx, line_start, len);
2303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 ++lnum;
2305 if (--read_count == 0)
2306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002307 error = TRUE; // break loop
2308 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 break;
2310 }
2311 }
2312 else
2313 --skip_count;
2314 line_start = ptr + 1;
2315 }
2316 }
2317 }
2318 linerest = (long)(ptr - line_start);
2319 ui_breakcheck();
2320 }
2321
2322failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002323 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (error && read_count == 0)
2325 error = FALSE;
2326
K.Takata3af98212022-11-01 20:36:19 +00002327 // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
2328 // In old days the file length was in sector count and the CTRL-Z the
2329 // marker where the file really ended. Assuming we write it to a file
2330 // system that keeps file length properly the CTRL-Z should be dropped.
2331 // Set the 'endoffile' option so the user can decide what to write later.
2332 // In Unix format the CTRL-Z is just another character.
2333 if (linerest != 0
2334 && !curbuf->b_p_bin
2335 && fileformat == EOL_DOS
2336 && ptr[-1] == Ctrl_Z)
2337 {
2338 ptr--;
2339 linerest--;
2340 if (set_options)
2341 curbuf->b_p_eof = TRUE;
2342 }
2343
2344 // If we get EOF in the middle of a line, note the fact by resetting
2345 // 'endofline' and add the line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (!error
2347 && !got_int
K.Takata3af98212022-11-01 20:36:19 +00002348 && linerest != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002351 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 curbuf->b_p_eol = FALSE;
2353 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002354 len = (colnr_T)(ptr - line_start + 1);
2355 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 error = TRUE;
2357 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002358 {
2359#ifdef FEAT_PERSISTENT_UNDO
2360 if (read_undo_file)
2361 sha256_update(&sha_ctx, line_start, len);
2362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002367 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002368 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002371 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002372 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002373 crypt_free_state(curbuf->b_cryptstate);
2374 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002375 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002376 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2377 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002378 // Don't set cryptkey to NULL, it's used below as a flag that
2379 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#endif
2381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 // If editing a new file: set 'fenc' for the current buffer.
2383 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002384 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002386 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (fenc_alloced)
2388 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002389#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#endif
2393
2394 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002395 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002396#ifdef HAVE_FD_CLOEXEC
2397 else
2398 {
2399 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002400
Bram Moolenaarf05da212009-11-17 16:13:15 +00002401 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002402 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002403 }
2404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 vim_free(buffer);
2406
2407#ifdef HAVE_DUP
2408 if (read_stdin)
2409 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002410 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002412 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414#endif
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (tmpname != NULL)
2417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002418 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 vim_free(tmpname);
2420 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002421 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 /*
2424 * In recovery mode everything but autocommands is skipped.
2425 */
2426 if (!recoverymode)
2427 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002428 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2430 {
2431#ifdef FEAT_NETBEANS_INTG
2432 netbeansFireChanges = 0;
2433#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002434 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#ifdef FEAT_NETBEANS_INTG
2436 netbeansFireChanges = 1;
2437#endif
2438 --linecnt;
2439 }
2440 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2441 if (filesize == 0)
2442 linecnt = 0;
2443 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002444 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002445 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002446#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002447 // After reading the text into the buffer the diff info needs to
2448 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002449 diff_invalidate(curbuf);
2450#endif
2451#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002452 // All folds in the window are invalid now. Mark them for update
2453 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002454 foldUpdateAll(curwin);
2455#endif
2456 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002457 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 appended_lines_mark(from, linecnt);
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460#ifndef ALWAYS_USE_GUI
2461 /*
2462 * If we were reading from the same terminal as where messages go,
2463 * the screen will have been messed up.
2464 * Switch on raw mode now and clear the screen.
2465 */
2466 if (read_stdin)
2467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002468 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 starttermcap();
2470 screenclear();
2471 }
2472#endif
2473
2474 if (got_int)
2475 {
2476 if (!(flags & READ_DUMMY))
2477 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002478 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002480 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482 msg_scroll = msg_save;
2483#ifdef FEAT_VIMINFO
2484 check_marks_read();
2485#endif
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002486 retval = OK; // an interrupt isn't really an error
2487 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489
2490 if (!filtering && !(flags & READ_DUMMY))
2491 {
John Marriott14ede182024-12-29 16:14:19 +01002492 int buflen;
2493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002494 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 c = FALSE;
2496
John Marriott14ede182024-12-29 16:14:19 +01002497 buflen = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002499 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
John Marriott14ede182024-12-29 16:14:19 +01002501 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2502 _("[fifo]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 c = TRUE;
2504 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002505 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
John Marriott14ede182024-12-29 16:14:19 +01002507 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2508 _("[socket]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 c = TRUE;
2510 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002511# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002512 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002513 {
John Marriott14ede182024-12-29 16:14:19 +01002514 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2515 _("[character special]"));
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002516 c = TRUE;
2517 }
2518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519#endif
2520 if (curbuf->b_p_ro)
2521 {
John Marriott14ede182024-12-29 16:14:19 +01002522 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2523 "%s", shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 c = TRUE;
2525 }
2526 if (read_no_eol_lnum)
2527 {
2528 msg_add_eol();
2529 c = TRUE;
2530 }
2531 if (ff_error == EOL_DOS)
2532 {
John Marriott14ede182024-12-29 16:14:19 +01002533 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
John Marriottdf4b3ca2024-12-30 09:48:06 +01002534 _("[CR missing]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 c = TRUE;
2536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (split)
2538 {
John Marriott14ede182024-12-29 16:14:19 +01002539 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2540 _("[long lines split]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 c = TRUE;
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (notconverted)
2544 {
John Marriott14ede182024-12-29 16:14:19 +01002545 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2546 _("[NOT converted]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 c = TRUE;
2548 }
2549 else if (converted)
2550 {
John Marriott14ede182024-12-29 16:14:19 +01002551 buflen += vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2552 _("[converted]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 c = TRUE;
2554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555#ifdef FEAT_CRYPT
2556 if (cryptkey != NULL)
2557 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002558 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 c = TRUE;
2560 }
2561#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002562 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
John Marriott14ede182024-12-29 16:14:19 +01002564 vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2565 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 c = TRUE;
2567 }
2568 else if (illegal_byte > 0)
2569 {
John Marriott14ede182024-12-29 16:14:19 +01002570 vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2571 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 c = TRUE;
2573 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002574 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
John Marriott14ede182024-12-29 16:14:19 +01002576 vim_snprintf((char *)IObuff + buflen, IOSIZE - buflen,
2577 _("[READ ERRORS]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 c = TRUE;
2579 }
2580 if (msg_add_fileformat(fileformat))
2581 c = TRUE;
2582#ifdef FEAT_CRYPT
2583 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002584 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002585 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 else
2587#endif
2588 msg_add_lines(c, (long)linecnt, filesize);
2589
Bram Moolenaard23a8232018-02-10 18:45:26 +01002590 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 msg_scrolled_ign = TRUE;
2592#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002593 // Don't show the message when reading stdin, it would end up in a
2594 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (read_stdin || read_buffer)
2596 p = msg_may_trunc(FALSE, IObuff);
2597 else
2598#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002599 {
2600 if (msg_col > 0)
2601 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002602 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002605 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002606 // Need to repeat the message after redrawing when:
2607 // - When reading from stdin (the screen will be cleared next).
2608 // - When restart_edit is set (otherwise there will be a delay
2609 // before redrawing).
2610 // - When the screen was scrolled but there is no wait-return
2611 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002612 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 msg_scrolled_ign = FALSE;
2614 }
2615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002616 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002618 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002619 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 curbuf->b_p_ro = TRUE;
2621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 /*
2625 * In Ex mode: cursor at last new line.
2626 * Otherwise: cursor at first new line.
2627 */
2628 if (exmode_active)
2629 curwin->w_cursor.lnum = from + linecnt;
2630 else
2631 curwin->w_cursor.lnum = from + 1;
2632 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002633 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
Bram Moolenaare1004402020-10-24 20:49:43 +02002635 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002636 {
2637 // Set '[ and '] marks to the newly read lines.
2638 curbuf->b_op_start.lnum = from + 1;
2639 curbuf->b_op_start.col = 0;
2640 curbuf->b_op_end.lnum = from + linecnt;
2641 curbuf->b_op_end.col = 0;
2642 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002643
Bram Moolenaar4f974752019-02-17 17:44:42 +01002644#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002645 /*
2646 * Work around a weird problem: When a file has two links (only
2647 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002648 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002649 * It's correct again after reading the file, thus reset the timestamp
2650 * here.
2651 */
2652 if (newfile && !read_stdin && !read_buffer
2653 && mch_stat((char *)fname, &st) >= 0)
2654 {
2655 buf_store_time(curbuf, &st, fname);
2656 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002657 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002658 }
2659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661 msg_scroll = msg_save;
2662
2663#ifdef FEAT_VIMINFO
2664 /*
2665 * Get the marks before executing autocommands, so they can be used there.
2666 */
2667 check_marks_read();
2668#endif
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002671 * We remember if the last line of the read didn't have
2672 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2673 * or writing the read again with 'binary' on. The latter is required
2674 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002676 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002678 // When reloading a buffer put the cursor at the first line that is
2679 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002680 if (flags & READ_KEEP_UNDO)
2681 u_find_first_changed();
2682
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002683#ifdef FEAT_PERSISTENT_UNDO
2684 /*
2685 * When opening a new file locate undo info and read it.
2686 */
2687 if (read_undo_file)
2688 {
2689 char_u hash[UNDO_HASH_SIZE];
2690
2691 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002692 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002693 }
2694#endif
2695
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002696 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 int m = msg_scroll;
2699 int n = msg_scrolled;
2700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002701 // Save the fileformat now, otherwise the buffer will be considered
2702 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002703 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 save_file_ff(curbuf);
2705
2706 /*
2707 * The output from the autocommands should not overwrite anything and
2708 * should not be overwritten: Set msg_scroll, restore its value if no
2709 * output was done.
2710 */
2711 msg_scroll = TRUE;
2712 if (filtering)
2713 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2714 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002715 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2718 FALSE, curbuf, eap);
zeertzjq5bf6c212024-03-31 18:41:27 +02002719 if (!curbuf->b_au_did_filetype && *curbuf->b_p_ft != NUL)
Bram Moolenaarc3691332016-04-20 12:49:49 +02002720 /*
2721 * EVENT_FILETYPE was not triggered but the buffer already has a
2722 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2723 */
2724 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2725 TRUE, curbuf);
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 else
2728 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2729 FALSE, NULL, eap);
2730 if (msg_scrolled == n)
2731 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002732# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002733 if (aborting()) // autocmds may abort script processing
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002734 goto theend;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002738 if (!(recoverymode && error))
2739 retval = OK;
2740
2741theend:
2742 if (curbuf->b_ml.ml_mfp != NULL
2743 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
2744 // OK to sync the swap file now
2745 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
2746
2747 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748}
2749
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002750#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002751/*
2752 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2753 * which is the name of files used for process substitution output by
2754 * some shells on some operating systems, e.g., bash on SunOS.
2755 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2756 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002757 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002758is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002759{
2760 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2761 && VIM_ISDIGIT(fname[8])
2762 && *skipdigits(fname + 9) == NUL
2763 && (fname[9] != NUL
2764 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2765}
2766#endif
2767
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002768/*
2769 * From the current line count and characters read after that, estimate the
2770 * line number where we are now.
2771 * Used for error messages that include a line number.
2772 */
2773 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002774readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002775 linenr_T linecnt, // line count before reading more bytes
2776 char_u *p, // start of more bytes read
2777 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002778{
2779 char_u *s;
2780 linenr_T lnum;
2781
2782 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2783 for (s = p; s < endp; ++s)
2784 if (*s == '\n')
2785 ++lnum;
2786 return lnum;
2787}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789/*
Rob Pilling8196e942022-02-11 15:12:10 +00002790 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002791 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 * Returns OK or FAIL.
2793 */
2794 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002795prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
Bram Moolenaar13505972019-01-24 15:04:48 +01002797 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (eap->cmd == NULL)
2799 return FAIL;
2800
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002801 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2802 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002803 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002804 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002805
2806 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002807 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002808 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 return OK;
2810}
2811
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002812/*
2813 * Set default or forced 'fileformat' and 'binary'.
2814 */
2815 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002816set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002817{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002818 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002819 if (set_options)
2820 {
2821 if (eap != NULL && eap->force_ff != 0)
2822 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2823 else if (*p_ffs != NUL)
2824 set_fileformat(default_fileformat(), OPT_LOCAL);
2825 }
2826
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002827 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002828 if (eap != NULL && eap->force_bin != 0)
2829 {
2830 int oldval = curbuf->b_p_bin;
2831
2832 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2833 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2834 }
2835}
2836
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002837/*
2838 * Set forced 'fileencoding'.
2839 */
2840 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002841set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002842{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002843 if (eap->force_enc == 0)
2844 return;
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002845
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002846 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2847
2848 if (fenc != NULL)
2849 set_string_option_direct((char_u *)"fenc", -1,
2850 fenc, OPT_FREE|OPT_LOCAL, 0);
2851 vim_free(fenc);
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002852}
2853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854/*
2855 * Find next fileencoding to use from 'fileencodings'.
2856 * "pp" points to fenc_next. It's advanced to the next item.
2857 * When there are no more items, an empty string is returned and *pp is set to
2858 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002859 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2860 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
2862 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002863next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 char_u *p;
2866 char_u *r;
2867
Bram Moolenaarf077db22019-08-13 00:18:24 +02002868 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (**pp == NUL)
2870 {
2871 *pp = NULL;
2872 return (char_u *)"";
2873 }
2874 p = vim_strchr(*pp, ',');
2875 if (p == NULL)
2876 {
2877 r = enc_canonize(*pp);
2878 *pp += STRLEN(*pp);
2879 }
2880 else
2881 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002882 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 *pp = p + 1;
2884 if (r != NULL)
2885 {
2886 p = enc_canonize(r);
2887 vim_free(r);
2888 r = p;
2889 }
2890 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002891 if (r != NULL)
2892 *alloced = TRUE;
2893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002895 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 r = (char_u *)"";
2897 *pp = NULL;
2898 }
2899 return r;
2900}
2901
Bram Moolenaar13505972019-01-24 15:04:48 +01002902#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903/*
2904 * Convert a file with the 'charconvert' expression.
2905 * This closes the file which is to be read, converts it and opens the
2906 * resulting file for reading.
2907 * Returns name of the resulting converted file (the caller should delete it
2908 * after reading it).
2909 * Returns NULL if the conversion failed ("*fdp" is not set) .
2910 */
2911 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002912readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 char_u *fname, // name of input file
2914 char_u *fenc, // converted from
2915 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002918 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002920 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002922 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 else
2924 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002925 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 *fdp = -1;
2927 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2928 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002929 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2931 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002932 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934
2935 if (errmsg != NULL)
2936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // Don't use emsg(), it breaks mappings, the retry with
2938 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002939 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (tmpname != NULL)
2941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002942 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002943 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945 }
2946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002947 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (*fdp < 0)
2949 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2950
2951 return tmpname;
2952}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953#endif
2954
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002955#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002957 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2959 * *filesizep are updated.
2960 * Return the (new) encryption key, NULL for no encryption.
2961 */
2962 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002963check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 char_u *cryptkey, // previous encryption key or NULL
2965 char_u *ptr, // pointer to read bytes
2966 long *sizep, // length of read bytes
2967 off_T *filesizep, // nr of bytes used from file
2968 int newfile, // editing a new buffer
2969 char_u *fname, // file name to display
2970 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002972 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002973 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002974
2975 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002977 // Mark the buffer as read-only until the decryption has taken place.
2978 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002979 curbuf->b_p_ro = TRUE;
2980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002981 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002982 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002983 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
2985 if (*curbuf->b_p_key)
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 cryptkey = curbuf->b_p_key;
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01002988 crypt_check_swapfile_curbuf();
2989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else
2991 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002992 // When newfile is TRUE, store the typed key in the 'key'
2993 // option and don't free it. bf needs hash of the key saved.
2994 // Don't ask for the key again when first time Enter was hit.
2995 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002996 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002997 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002998 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002999 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02003000 *did_ask = TRUE;
3001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003002 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (cryptkey != NULL && *cryptkey == NUL)
3004 {
3005 if (cryptkey != curbuf->b_p_key)
3006 vim_free(cryptkey);
3007 cryptkey = NULL;
3008 }
3009 }
3010 }
3011
3012 if (cryptkey != NULL)
3013 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003014 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02003015
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003016 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02003017 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003018 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02003019 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02003020
3021 curbuf->b_cryptstate = crypt_create_from_header(
3022 method, cryptkey, ptr);
3023 crypt_set_cm_option(curbuf, method);
3024
3025 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003026 *filesizep += header_len;
3027 *sizep -= header_len;
3028 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
3029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003030 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02003031 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003034 // When starting to edit a new file which does not have encryption, clear
3035 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02003036 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar24959102022-05-07 20:01:16 +01003037 set_option_value_give_err((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 return cryptkey;
3040}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003041#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02003042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003044 * Return TRUE if a file appears to be read-only from the file permissions.
3045 */
3046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003047check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003048 char_u *fname, // full path to file
3049 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00003050{
3051#ifndef USE_MCH_ACCESS
3052 int fd = 0;
3053#endif
3054
3055 return (
3056#ifdef USE_MCH_ACCESS
3057# ifdef UNIX
3058 (perm & 0222) == 0 ||
3059# endif
3060 mch_access((char *)fname, W_OK)
3061#else
3062 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3063 ? TRUE : (close(fd), FALSE)
3064#endif
3065 );
3066}
3067
Bram Moolenaara7870192019-02-14 12:56:36 +01003068#if defined(HAVE_FSYNC) || defined(PROTO)
3069/*
3070 * Call fsync() with Mac-specific exception.
3071 * Return fsync() result: zero for success.
3072 */
3073 int
3074vim_fsync(int fd)
3075{
3076 int r;
3077
3078# ifdef MACOS_X
3079 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01003080 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01003081# endif
3082 r = fsync(fd);
3083 return r;
3084}
3085#endif
3086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003088 * Set the name of the current buffer. Use when the buffer doesn't have a
3089 * name and a ":r" or ":w" command with a file name is used.
3090 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003091 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003092set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003093{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003094 buf_T *buf = curbuf;
3095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003096 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003097 if (curbuf->b_p_bl)
3098 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3099 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003100#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003101 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003102 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003103#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003104 if (curbuf != buf)
3105 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003106 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003107 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003108 return FAIL;
3109 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003110
3111 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3112 curbuf->b_flags |= BF_NOTEDITED;
3113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003114 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003115 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3116 if (curbuf->b_p_bl)
3117 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003118#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003119 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003120 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003121#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003123 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003124 if (*curbuf->b_p_ft == NUL)
3125 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003126 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003127 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003128 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003129 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003130
3131 return OK;
3132}
3133
3134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * Put file name into IObuff with quotes.
3136 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003137 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003138msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 if (fname == NULL)
3141 fname = (char_u *)"-stdin-";
3142 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3143 IObuff[0] = '"';
3144 STRCAT(IObuff, "\" ");
3145}
3146
3147/*
3148 * Append message for text mode to IObuff.
3149 * Return TRUE if something appended.
3150 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003151 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154#ifndef USE_CRNL
3155 if (eol_type == EOL_DOS)
3156 {
3157 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3158 return TRUE;
3159 }
3160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (eol_type == EOL_MAC)
3162 {
3163 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3164 return TRUE;
3165 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003166#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (eol_type == EOL_UNIX)
3168 {
3169 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3170 return TRUE;
3171 }
3172#endif
3173 return FALSE;
3174}
3175
3176/*
3177 * Append line and character count to IObuff.
3178 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003180msg_add_lines(
3181 int insert_space,
3182 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003183 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
John Marriott14ede182024-12-29 16:14:19 +01003185 int len = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (shortmess(SHM_LINES))
John Marriott14ede182024-12-29 16:14:19 +01003188 vim_snprintf((char *)IObuff + len, IOSIZE - (size_t)len,
3189 "%s%ldL, %lldB", insert_space ? " " : "", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191 {
John Marriott14ede182024-12-29 16:14:19 +01003192 len += vim_snprintf((char *)IObuff + len, IOSIZE - (size_t)len,
3193 NGETTEXT("%s%ld line, ", "%s%ld lines, ", lnum), insert_space ? " " : "", lnum);
3194 vim_snprintf((char *)IObuff + len, IOSIZE - (size_t)len,
3195 NGETTEXT("%lld byte", "%lld bytes", nchars), (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197}
3198
3199/*
3200 * Append message for missing line separator to IObuff.
3201 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003203msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
3205 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3206}
3207
Bram Moolenaar473952e2019-09-28 16:30:04 +02003208 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003209time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
ichizokdef69df2021-10-15 17:23:12 +01003211 return
3212#ifdef ST_MTIM_NSEC
3213 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif
ichizokdef69df2021-10-15 17:23:12 +01003215#if defined(__linux__) || defined(MSWIN)
3216 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3217 // the seconds. Since the roundoff is done when flushing the inode, the
3218 // time may change unexpectedly by one second!!!
3219 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3220#else
3221 (long)st->st_mtime != mtime
3222#endif
3223 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003227 * Return TRUE if file encoding "fenc" requires conversion from or to
3228 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003230 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003231need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003233 int same_encoding;
3234 int enc_flags;
3235 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003237 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003238 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003239 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003240 fenc_flags = 0;
3241 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003242 else
3243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003244 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3245 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003246 enc_flags = get_fio_flags(p_enc);
3247 fenc_flags = get_fio_flags(fenc);
3248 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3249 }
3250 if (same_encoding)
3251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003252 // Specified encoding matches with 'encoding'. This requires
3253 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003254 return enc_unicode != 0;
3255 }
3256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003257 // Encodings differ. However, conversion is not needed when 'enc' is any
3258 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003259 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261
3262/*
3263 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3264 * internal conversion.
3265 * if "ptr" is an empty string, use 'encoding'.
3266 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003268get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 int prop;
3271
3272 if (*ptr == NUL)
3273 ptr = p_enc;
3274
3275 prop = enc_canon_props(ptr);
3276 if (prop & ENC_UNICODE)
3277 {
3278 if (prop & ENC_2BYTE)
3279 {
3280 if (prop & ENC_ENDIAN_L)
3281 return FIO_UCS2 | FIO_ENDIAN_L;
3282 return FIO_UCS2;
3283 }
3284 if (prop & ENC_4BYTE)
3285 {
3286 if (prop & ENC_ENDIAN_L)
3287 return FIO_UCS4 | FIO_ENDIAN_L;
3288 return FIO_UCS4;
3289 }
3290 if (prop & ENC_2WORD)
3291 {
3292 if (prop & ENC_ENDIAN_L)
3293 return FIO_UTF16 | FIO_ENDIAN_L;
3294 return FIO_UTF16;
3295 }
3296 return FIO_UTF8;
3297 }
3298 if (prop & ENC_LATIN1)
3299 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003300 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 return 0;
3302}
3303
Bram Moolenaar473952e2019-09-28 16:30:04 +02003304#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305/*
3306 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3307 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3308 * Used for conversion between 'encoding' and 'fileencoding'.
3309 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003310 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003311get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 int cp;
3314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003315 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (!enc_utf8 && enc_codepage <= 0)
3317 return 0;
3318
3319 cp = encname2codepage(ptr);
3320 if (cp == 0)
3321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003322# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (STRCMP(ptr, "utf-8") == 0)
3324 cp = CP_UTF8;
3325 else
3326# endif
3327 return 0;
3328 }
3329 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3330}
3331#endif
3332
Bram Moolenaar473952e2019-09-28 16:30:04 +02003333#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3336 * needed for the internal conversion to/from utf-8 or latin1.
3337 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003338 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003339get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3342 && (enc_canon_props(ptr) & ENC_MACROMAN))
3343 return FIO_MACROMAN;
3344 return 0;
3345}
3346#endif
3347
3348/*
3349 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3350 * "size" must be at least 2.
3351 * Return the name of the encoding and set "*lenp" to the length.
3352 * Returns NULL when no BOM found.
3353 */
3354 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003355check_for_bom(
3356 char_u *p,
3357 long size,
3358 int *lenp,
3359 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 char *name = NULL;
3362 int len = 2;
3363
3364 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003365 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003367 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 len = 3;
3369 }
3370 else if (p[0] == 0xff && p[1] == 0xfe)
3371 {
3372 if (size >= 4 && p[2] == 0 && p[3] == 0
3373 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003375 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 len = 4;
3377 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003378 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003379 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003380 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003381 // utf-16le is preferred, it also works for ucs-2le text
3382 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (p[0] == 0xfe && p[1] == 0xff
3385 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003387 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003388 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003389 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003390 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003391 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
3393 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3394 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003396 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 len = 4;
3398 }
3399
3400 *lenp = len;
3401 return (char_u *)name;
3402}
3403
3404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * Try to find a shortname by comparing the fullname with the current
3406 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003407 * Returns "full_path" or pointer into "full_path" if shortened.
3408 */
3409 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003410shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003411{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003412 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003413 char_u *p = full_path;
3414
Bram Moolenaard9462e32011-04-11 21:35:11 +02003415 dirname = alloc(MAXPATHL);
3416 if (dirname == NULL)
3417 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003418 if (mch_dirname(dirname, MAXPATHL) == OK)
3419 {
3420 p = shorten_fname(full_path, dirname);
3421 if (p == NULL || *p == NUL)
3422 p = full_path;
3423 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003424 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003425 return p;
3426}
3427
3428/*
3429 * Try to find a shortname by comparing the fullname with the current
3430 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 * Returns NULL if not shorter name possible, pointer into "full_path"
3432 * otherwise.
3433 */
3434 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 int len;
3438 char_u *p;
3439
3440 if (full_path == NULL)
3441 return NULL;
3442 len = (int)STRLEN(dir_name);
3443 if (fnamencmp(dir_name, full_path, len) == 0)
3444 {
3445 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003446#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003448 * MS-Windows: when a file is in the root directory, dir_name will end
3449 * in a slash, since C: by itself does not define a specific dir. In
3450 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
3452 if (!((len > 2) && (*(p - 2) == ':')))
3453#endif
3454 {
3455 if (vim_ispathsep(*p))
3456 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003457#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else
3459 p = NULL;
3460#endif
3461 }
3462 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003463#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 /*
3465 * When using a file in the current drive, remove the drive name:
3466 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3467 * a floppy from "A:\dir" to "B:\dir".
3468 */
3469 else if (len > 3
3470 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3471 && full_path[1] == ':'
3472 && vim_ispathsep(full_path[2]))
3473 p = full_path + 2;
3474#endif
3475 else
3476 p = NULL;
3477 return p;
3478}
3479
3480/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003481 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * When "force" is TRUE: Use full path from now on for files currently being
3483 * edited, both for file name and swap file name. Try to shorten the file
3484 * names a bit, if safe to do so.
3485 * When "force" is FALSE: Only try to shorten absolute file names.
3486 * For buffers that have buftype "nofile" or "scratch": never change the file
3487 * name.
3488 */
3489 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003490shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3491{
3492 char_u *p;
3493
3494 if (buf->b_fname != NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02003495 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003496 && !path_with_url(buf->b_fname)
3497 && (force
3498 || buf->b_sfname == NULL
3499 || mch_isFullName(buf->b_sfname)))
3500 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003501 if (buf->b_sfname != buf->b_ffname)
3502 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003503 p = shorten_fname(buf->b_ffname, dirname);
3504 if (p != NULL)
3505 {
3506 buf->b_sfname = vim_strsave(p);
3507 buf->b_fname = buf->b_sfname;
3508 }
3509 if (p == NULL || buf->b_fname == NULL)
3510 buf->b_fname = buf->b_ffname;
3511 }
3512}
3513
3514/*
3515 * Shorten filenames for all buffers.
3516 */
3517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003518shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 char_u dirname[MAXPATHL];
3521 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003524 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003526 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003528 // Always make the swap file name a full path, a "nofile" buffer may
3529 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003530 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003533 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003534#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003535 popup_update_preview_title();
3536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537}
3538
3539#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3540 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003541 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 || defined(PROTO)
3543/*
3544 * Shorten all filenames in "fnames[count]" by current directory.
3545 */
3546 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003547shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
3549 int i;
3550 char_u dirname[MAXPATHL];
3551 char_u *p;
3552
3553 if (fnames == NULL || count < 1)
3554 return;
3555 mch_dirname(dirname, sizeof(dirname));
3556 for (i = 0; i < count; ++i)
3557 {
3558 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3559 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003560 // shorten_fname() returns pointer in given "fnames[i]". If free
3561 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3562 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 p = vim_strsave(p);
3564 vim_free(fnames[i]);
3565 fnames[i] = p;
3566 }
3567 }
3568}
3569#endif
3570
3571/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003572 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 * fo_o_h.ext for MSDOS or when shortname option set.
3574 *
3575 * Assumed that fname is a valid name found in the filesystem we assure that
3576 * the return value is a different name and ends in 'ext'.
3577 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3578 * characters otherwise.
3579 * Space for the returned name is allocated, must be freed later.
3580 * Returns NULL when out of memory.
3581 */
3582 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003583modname(
3584 char_u *fname,
3585 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003586 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003588 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 fname, ext, prepend_dot);
3590}
3591
3592 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003593buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003594 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003595 char_u *fname,
3596 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003597 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 char_u *retval;
3600 char_u *s;
3601 char_u *e;
3602 char_u *ptr;
John Marriott14ede182024-12-29 16:14:19 +01003603 size_t ptrlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 int fnamelen, extlen;
3605
3606 extlen = (int)STRLEN(ext);
3607
3608 /*
3609 * If there is no file name we must get the name of the current directory
3610 * (we need the full path in case :cd is used).
3611 */
3612 if (fname == NULL || *fname == NUL)
3613 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003614 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (retval == NULL)
3616 return NULL;
3617 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3618 (fnamelen = (int)STRLEN(retval)) == 0)
3619 {
3620 vim_free(retval);
3621 return NULL;
3622 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003623 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 retval[fnamelen++] = PATHSEP;
3626 retval[fnamelen] = NUL;
3627 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003628 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630 else
3631 {
3632 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003633 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (retval == NULL)
3635 return NULL;
3636 STRCPY(retval, fname);
3637#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003638 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
3640 }
3641
3642 /*
3643 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3644 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3645 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3646 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3647 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003648 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003650 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003654 {
3655 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003660 // the file name has at most BASENAMELEN characters.
John Marriott14ede182024-12-29 16:14:19 +01003661 ptrlen = (size_t)(fnamelen - (ptr - retval));
3662 if (ptrlen > (unsigned)BASENAMELEN)
3663 {
3664 ptrlen = BASENAMELEN;
3665 ptr[ptrlen] = NUL;
3666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
John Marriott14ede182024-12-29 16:14:19 +01003668 s = ptr + ptrlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 /*
3671 * For 8.3 file names we may have to reduce the length.
3672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 /*
3676 * If there is no file name, or the file name ends in '/', and the
3677 * extension starts with '.', put a '_' before the dot, because just
3678 * ".ext" is invalid.
3679 */
3680 if (fname == NULL || *fname == NUL
3681 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 *s++ = '_';
3685 }
3686 /*
3687 * If the extension starts with '.', truncate the base name at 8
3688 * characters
3689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003692 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 s = ptr + 8;
3695 *s = '\0';
3696 }
3697 }
3698 /*
3699 * If the extension doesn't start with '.', and the file name
3700 * doesn't have an extension yet, append a '.'
3701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 else if ((e = vim_strchr(ptr, '.')) == NULL)
3703 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 /*
3705 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003706 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 */
John Marriott14ede182024-12-29 16:14:19 +01003708 else if ((int)(ptrlen - (e - retval)) + extlen > 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 s = e + 4 - extlen;
3710 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003711#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 /*
3713 * If there is no file name, and the extension starts with '.', put a
3714 * '_' before the dot, because just ".ext" may be invalid if it's on a
3715 * FAT partition, and on HPFS it doesn't matter.
3716 */
3717 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3718 *s++ = '_';
3719#endif
3720
3721 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003722 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 * ext can start with '.' and cannot exceed 3 more characters.
3724 */
3725 STRCPY(s, ext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 /*
3727 * Prepend the dot.
3728 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003729 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
John Marriott14ede182024-12-29 16:14:19 +01003731 mch_memmove(e + 1, e, (size_t)(((fnamelen + extlen) - (e - retval)) + 1)); // +1 for NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 /*
3736 * Check that, after appending the extension, the file name is really
3737 * different.
3738 */
3739 if (fname != NULL && STRCMP(fname, retval) == 0)
3740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003741 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 while (--s >= ptr)
3743 {
3744 if (*s != '_')
3745 {
3746 *s = '_';
3747 break;
3748 }
3749 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003750 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 *ptr = 'v';
3752 }
3753 return retval;
3754}
3755
3756/*
3757 * Like fgets(), but if the file line is too long, it is truncated and the
3758 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003759 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 */
3761 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003762vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 char *eof;
3765#define FGETS_SIZE 200
3766 char tbuf[FGETS_SIZE];
3767
3768 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003772 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003774 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 do
3776 {
3777 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003778 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3780 }
3781 return (eof == NULL);
3782}
3783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784/*
3785 * rename() only works if both files are on the same file system, this
3786 * function will (attempts to?) copy the file across if rename fails -- webb
3787 * Return -1 for failure, 0 for success.
3788 */
3789 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003790vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int n;
Shougo Matsushita60c87432024-06-03 22:59:27 +02003793 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794#ifdef AMIGA
3795 BPTR flock;
3796#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003797 stat_T st;
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003798 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003801 * When the names are identical, there is nothing to do. When they refer
3802 * to the same file (ignoring case and slash/backslash differences) but
3803 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003806 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003807 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003808 use_tmp_file = TRUE;
3809 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003810 return 0;
3811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 /*
3814 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3815 */
3816 if (mch_stat((char *)from, &st) < 0)
3817 return -1;
3818
Bram Moolenaar3576da72008-12-30 15:15:57 +00003819#ifdef UNIX
3820 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003821 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003823 // It's possible for the source and destination to be the same file.
3824 // This happens when "from" and "to" differ in case and are on a FAT32
3825 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003826 if (mch_stat((char *)to, &st_to) >= 0
3827 && st.st_dev == st_to.st_dev
3828 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003829 use_tmp_file = TRUE;
3830 }
3831#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003832#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003833 {
3834 BY_HANDLE_FILE_INFORMATION info1, info2;
3835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003836 // It's possible for the source and destination to be the same file.
3837 // In that case go through a temp file name. This makes rename("foo",
3838 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003839 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3840 && win32_fileinfo(to, &info2) == FILEINFO_OK
3841 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3842 && info1.nFileIndexHigh == info2.nFileIndexHigh
3843 && info1.nFileIndexLow == info2.nFileIndexLow)
3844 use_tmp_file = TRUE;
3845 }
3846#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003847
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003848 if (use_tmp_file)
3849 {
3850 char tempname[MAXPATHL + 1];
3851
3852 /*
3853 * Find a name that doesn't exist and is in the same directory.
3854 * Rename "from" to "tempname" and then rename "tempname" to "to".
3855 */
3856 if (STRLEN(from) >= MAXPATHL - 5)
3857 return -1;
3858 STRCPY(tempname, from);
3859 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003860 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003861 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3862 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003863 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003864 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003865 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003866 if (mch_rename(tempname, (char *)to) == 0)
3867 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // Strange, the second step failed. Try moving the
3869 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003870 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003871 return -1;
3872 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003873 // If it fails for one temp name it will most likely fail
3874 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003875 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003876 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003877 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003878 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003879 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 /*
3882 * Delete the "to" file, this is required on some systems to make the
3883 * mch_rename() work, on other systems it makes sure that we don't have
3884 * two files when the mch_rename() fails.
3885 */
3886
3887#ifdef AMIGA
3888 /*
3889 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3890 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003891 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 * deleting the "from" file (horror!) we lock it during the remove.
3893 *
3894 * When used for making a backup before writing the file: This should not
3895 * happen with ":w", because startscript() should detect this problem and
3896 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3897 * name. This problem does exist with ":w filename", but then the
3898 * original file will be somewhere else so the backup isn't really
3899 * important. If autoscripting is off the rename may fail.
3900 */
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00003901 flock = Lock((UBYTE *)from, (long)VIM_ACCESS_READ);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#endif
3903 mch_remove(to);
3904#ifdef AMIGA
3905 if (flock)
3906 UnLock(flock);
3907#endif
3908
3909 /*
3910 * First try a normal rename, return if it works.
3911 */
3912 if (mch_rename((char *)from, (char *)to) == 0)
3913 return 0;
3914
3915 /*
3916 * Rename() failed, try copying the file.
3917 */
Shougo Matsushita60c87432024-06-03 22:59:27 +02003918 ret = vim_copyfile(from, to);
3919 if (ret != OK)
3920 return -1;
3921
3922 /*
3923 * Remove copied original file
3924 */
3925 if (mch_stat((char *)from, &st) >= 0)
3926 mch_remove(from);
3927
3928 return 0;
3929}
3930
3931
3932/*
3933 * Create the new file with same permissions as the original.
zeertzjqda090f92024-08-06 19:51:09 +02003934 * Return FAIL for failure, OK for success.
Shougo Matsushita60c87432024-06-03 22:59:27 +02003935 */
3936 int
3937vim_copyfile(char_u *from, char_u *to)
3938{
3939 int fd_in;
3940 int fd_out;
3941 int n;
3942 char *errmsg = NULL;
3943 char *buffer;
3944 long perm;
3945#ifdef HAVE_ACL
3946 vim_acl_T acl; // ACL from original file
3947#endif
3948
3949#ifdef HAVE_READLINK
3950 int ret;
3951 int len;
3952 stat_T st;
3953 char linkbuf[MAXPATHL + 1];
3954
3955 ret = mch_lstat((char *)from, &st);
3956 if (ret >= 0 && S_ISLNK(st.st_mode))
3957 {
zeertzjqda090f92024-08-06 19:51:09 +02003958 ret = -1;
Shougo Matsushita60c87432024-06-03 22:59:27 +02003959
3960 len = readlink((char *)from, linkbuf, MAXPATHL);
3961 if (len > 0)
3962 {
3963 linkbuf[len] = NUL;
3964
3965 // Create link
3966 ret = symlink(linkbuf, (char *)to);
3967 }
3968
3969 return ret == 0 ? OK : FAIL;
3970 }
3971#endif
3972
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003973 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003974#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003975 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003976 acl = mch_get_acl(from);
3977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3979 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003980 {
3981#ifdef HAVE_ACL
3982 mch_free_acl(acl);
3983#endif
Shougo Matsushita60c87432024-06-03 22:59:27 +02003984 return FAIL;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003985 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003986
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003987 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003988 fd_out = mch_open((char *)to,
3989 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (fd_out == -1)
3991 {
3992 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003993#ifdef HAVE_ACL
3994 mch_free_acl(acl);
3995#endif
Shougo Matsushita60c87432024-06-03 22:59:27 +02003996 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998
Bram Moolenaar473952e2019-09-28 16:30:04 +02003999 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (buffer == NULL)
4001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00004003 close(fd_in);
4004#ifdef HAVE_ACL
4005 mch_free_acl(acl);
4006#endif
Shougo Matsushita60c87432024-06-03 22:59:27 +02004007 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009
Bram Moolenaar473952e2019-09-28 16:30:04 +02004010 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004011 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00004013 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 break;
4015 }
4016
4017 vim_free(buffer);
4018 close(fd_in);
4019 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004020 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (n < 0)
4022 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00004023 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 to = from;
4025 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004026#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00004027 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00004028#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00004029#ifdef HAVE_ACL
4030 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00004031 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00004032#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02004033#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01004034 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01004035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (errmsg != NULL)
4037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004038 semsg(errmsg, to);
Shougo Matsushita60c87432024-06-03 22:59:27 +02004039 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
Shougo Matsushita60c87432024-06-03 22:59:27 +02004041 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042}
4043
4044static int already_warned = FALSE;
4045
4046/*
4047 * Check if any not hidden buffer has been changed.
4048 * Postpone the check if there are characters in the stuff buffer, a global
4049 * command is being executed, a mapping is being executed or an autocommand is
4050 * busy.
4051 * Returns TRUE if some message was written (screen should be redrawn and
4052 * cursor positioned).
4053 */
4054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004055check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004056 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 buf_T *buf;
4059 int didit = 0;
4060 int n;
4061
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004062 // Don't check timestamps while system() or another low-level function may
4063 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (no_check_timestamps > 0)
4065 return FALSE;
4066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004067 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
4068 // event and we would keep on checking if the file is steadily growing.
4069 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (focus && did_check_timestamps)
4071 {
4072 need_check_timestamps = TRUE;
4073 return FALSE;
4074 }
4075
4076 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004077 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004078 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else
4080 {
4081 ++no_wait_return;
4082 did_check_timestamps = TRUE;
4083 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02004084 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004086 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (buf->b_nwindows > 0)
4088 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004089 bufref_T bufref;
4090
4091 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 n = buf_check_timestamp(buf, focus);
4093 if (didit < n)
4094 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004095 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004097 // Autocommands have removed the buffer, start at the
4098 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 buf = firstbuf;
4100 continue;
4101 }
4102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 --no_wait_return;
4105 need_check_timestamps = FALSE;
4106 if (need_wait_return && didit == 2)
4107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01004109 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 out_flush();
4111 }
4112 }
4113 return didit;
4114}
4115
4116/*
4117 * Move all the lines from buffer "frombuf" to buffer "tobuf".
4118 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
4119 * empty.
4120 */
4121 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004122move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 buf_T *tbuf = curbuf;
4125 int retval = OK;
4126 linenr_T lnum;
4127 char_u *p;
4128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004129 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 curbuf = tobuf;
4131 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
4132 {
John Marriott14ede182024-12-29 16:14:19 +01004133 p = vim_strnsave(ml_get_buf(frombuf, lnum, FALSE), ml_get_buf_len(frombuf, lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4135 {
4136 vim_free(p);
4137 retval = FAIL;
4138 break;
4139 }
4140 vim_free(p);
4141 }
4142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004143 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (retval != FAIL)
4145 {
4146 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004147 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004148 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 // Oops! We could try putting back the saved lines, but that
4151 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 retval = FAIL;
4153 break;
4154 }
4155 }
4156
4157 curbuf = tbuf;
4158 return retval;
4159}
4160
4161/*
4162 * Check if buffer "buf" has been changed.
4163 * Also check if the file for a new buffer unexpectedly appeared.
4164 * return 1 if a changed buffer was found.
4165 * return 2 if a message has been displayed.
4166 * return 0 otherwise.
4167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004169buf_check_timestamp(
4170 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004171 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004173 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 int stat_res;
4175 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004177 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004179 enum {
4180 RELOAD_NONE,
4181 RELOAD_NORMAL,
4182 RELOAD_DETECT
4183 } reload = RELOAD_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4185 int can_reload = FALSE;
4186#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004187 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 int orig_mode = buf->b_orig_mode;
4189#ifdef FEAT_GUI
4190 int save_mouse_correct = need_mouse_correct;
4191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004193 int n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004194 bufref_T bufref;
4195
4196 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004198 // If there is no file name, the buffer is not loaded, 'buftype' is
4199 // set, we are in the middle of a save or being called recursively: ignore
4200 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 if (buf->b_ffname == NULL
4202 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004203 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004206#ifdef FEAT_NETBEANS_INTG
4207 || isNetbeansBuffer(buf)
4208#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004209#ifdef FEAT_TERMINAL
4210 || buf->b_term != NULL
4211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 )
4213 return 0;
4214
4215 if ( !(buf->b_flags & BF_NOTEDITED)
4216 && buf->b_mtime != 0
4217 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004218 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004219 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#ifdef HAVE_ST_MODE
4221 || (int)st.st_mode != buf->b_orig_mode
4222#else
4223 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4224#endif
4225 ))
4226 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004227 long prev_b_mtime = buf->b_mtime;
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 retval = 1;
4230
Bram Moolenaar386bc822018-07-07 18:34:12 +02004231 // set b_mtime to stop further warnings (e.g., when executing
4232 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (stat_res < 0)
4234 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004235 // Check the file again later to see if it re-appears.
4236 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 buf->b_orig_size = 0;
4238 buf->b_orig_mode = 0;
4239 }
4240 else
4241 buf_store_time(buf, &st, buf->b_ffname);
4242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 // Don't do anything for a directory. Might contain the file
4244 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (mch_isdir(buf->b_fname))
4246 ;
4247
4248 /*
4249 * If 'autoread' is set, the buffer has no changes and the file still
4250 * exists, reload the buffer. Use the buffer-local option value if it
4251 * was set, the global option value otherwise.
4252 */
4253 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4254 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004255 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 else
4257 {
John Marriott14ede182024-12-29 16:14:19 +01004258 char *reason;
4259#ifdef FEAT_EVAL
4260 size_t reasonlen;
4261#endif
4262
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004263 if (stat_res < 0)
John Marriott14ede182024-12-29 16:14:19 +01004264 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004265 reason = "deleted";
John Marriott14ede182024-12-29 16:14:19 +01004266#ifdef FEAT_EVAL
4267 reasonlen = STRLEN_LITERAL("deleted");
4268#endif
4269 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004270 else if (bufIsChanged(buf))
John Marriott14ede182024-12-29 16:14:19 +01004271 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004272 reason = "conflict";
John Marriott14ede182024-12-29 16:14:19 +01004273#ifdef FEAT_EVAL
4274 reasonlen = STRLEN_LITERAL("conflict");
4275#endif
4276 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004277 /*
4278 * Check if the file contents really changed to avoid giving a
4279 * warning when only the timestamp was set (e.g., checked out of
4280 * CVS). Always warn when the buffer was changed.
4281 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004282 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
John Marriott14ede182024-12-29 16:14:19 +01004283 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004284 reason = "changed";
John Marriott14ede182024-12-29 16:14:19 +01004285#ifdef FEAT_EVAL
4286 reasonlen = STRLEN_LITERAL("changed");
4287#endif
4288 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004289 else if (orig_mode != buf->b_orig_mode)
John Marriott14ede182024-12-29 16:14:19 +01004290 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004291 reason = "mode";
John Marriott14ede182024-12-29 16:14:19 +01004292#ifdef FEAT_EVAL
4293 reasonlen = STRLEN_LITERAL("mode");
4294#endif
4295 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004296 else
John Marriott14ede182024-12-29 16:14:19 +01004297 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004298 reason = "time";
John Marriott14ede182024-12-29 16:14:19 +01004299#ifdef FEAT_EVAL
4300 reasonlen = STRLEN_LITERAL("time");
4301#endif
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /*
4305 * Only give the warning if there are no FileChangedShell
4306 * autocommands.
4307 * Avoid being called recursively by setting "busy".
4308 */
4309 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004310#ifdef FEAT_EVAL
Yegappan Lakshmanan5e9aaed2025-01-18 10:24:25 +01004311 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, (int)reasonlen);
John Marriott14ede182024-12-29 16:14:19 +01004312 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", 0);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004313#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004314 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4316 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004317 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 busy = FALSE;
4319 if (n)
4320 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004321 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004322 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004323#ifdef FEAT_EVAL
John Marriott14ede182024-12-29 16:14:19 +01004324 char_u *s = get_vim_var_str(VV_FCS_CHOICE);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004325 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004326 reload = RELOAD_NORMAL;
4327 else if (STRCMP(s, "edit") == 0)
4328 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004329 else if (STRCMP(s, "ask") == 0)
4330 n = FALSE;
4331 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004332#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004333 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004335 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004337 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004338 {
4339 // Only give the message once.
4340 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004341 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 else
4344 {
4345 helpmesg = TRUE;
4346#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4347 can_reload = TRUE;
4348#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004349 if (reason[2] == 'n')
4350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004352 mesg2 = _("See \":help W12\" for more info.");
4353 }
4354 else if (reason[1] == 'h')
4355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004357 mesg2 = _("See \":help W11\" for more info.");
4358 }
4359 else if (*reason == 'm')
4360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004362 mesg2 = _("See \":help W16\" for more info.");
4363 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004364 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004365 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004366 // Only timestamp changed, store it to avoid a warning
4367 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004368 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004369 buf->b_mtime_read_ns = buf->b_mtime_ns;
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372 }
4373 }
4374
4375 }
4376 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4377 && vim_fexists(buf->b_ffname))
4378 {
4379 retval = 1;
4380 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4381 buf->b_flags |= BF_NEW_W;
4382#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4383 can_reload = TRUE;
4384#endif
4385 }
4386
zeertzjq8a017442024-03-07 21:48:33 +01004387 if (mesg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
John Marriott14ede182024-12-29 16:14:19 +01004389 char_u *path;
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 path = home_replace_save(buf, buf->b_fname);
4392 if (path != NULL)
4393 {
John Marriott14ede182024-12-29 16:14:19 +01004394 size_t tbufsize;
4395 char *tbuf;
4396
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004397 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 mesg2 = "";
John Marriott14ede182024-12-29 16:14:19 +01004399 tbufsize = STRLEN(mesg) + STRLEN(path) + 2 + STRLEN(mesg2) + 1; // +2 for either '\n' or "; "
4400 // and +1 for NUL
4401 tbuf = alloc(tbufsize);
4402 if (tbuf != NULL)
4403 {
4404 int tbuflen;
4405
4406 tbuflen = vim_snprintf(tbuf, tbufsize, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004407#ifdef FEAT_EVAL
John Marriott14ede182024-12-29 16:14:19 +01004408 // Set warningmsg here, before the unimportant and output-specific
4409 // mesg2 has been appended.
4410 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, tbuflen);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
John Marriott14ede182024-12-29 16:14:19 +01004413 if (can_reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (*mesg2 != NUL)
John Marriott14ede182024-12-29 16:14:19 +01004416 vim_snprintf(tbuf + tbuflen, tbufsize - tbuflen, "\n%s", mesg2);
4417 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4418 (char_u *)tbuf,
4419 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4420 1, NULL, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
John Marriott14ede182024-12-29 16:14:19 +01004422 case 2:
4423 reload = RELOAD_NORMAL;
4424 break;
4425 case 3:
4426 reload = RELOAD_DETECT;
4427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 }
John Marriott14ede182024-12-29 16:14:19 +01004430 else
4431#endif
4432 if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE)
4433 || already_warned)
4434 {
4435 if (*mesg2 != NUL)
4436 vim_snprintf(tbuf + tbuflen, tbufsize - tbuflen, "; %s", mesg2);
4437 emsg(tbuf);
4438 retval = 2;
4439 }
4440 else
4441 {
4442 if (!autocmd_busy)
4443 {
4444 msg_start();
4445 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
4446 if (*mesg2 != NUL)
4447 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
4448 msg_clr_eos();
4449 (void)msg_end();
4450 if (emsg_silent == 0 && !in_assert_fails)
4451 {
4452 out_flush();
4453 #ifdef FEAT_GUI
4454 if (!focus)
4455 #endif
4456 // give the user some time to think about it
4457 ui_delay(1004L, TRUE);
4458
4459 // don't redraw and erase the message
4460 redraw_cmdline = FALSE;
4461 }
4462 }
4463 already_warned = TRUE;
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 vim_free(tbuf);
John Marriott14ede182024-12-29 16:14:19 +01004468 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 }
4471
Rob Pilling8196e942022-02-11 15:12:10 +00004472 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004473 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004474 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004475 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004476#ifdef FEAT_PERSISTENT_UNDO
4477 if (buf->b_p_udf && buf->b_ffname != NULL)
4478 {
4479 char_u hash[UNDO_HASH_SIZE];
4480 buf_T *save_curbuf = curbuf;
4481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004482 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004483 curbuf = buf;
4484 u_compute_hash(hash);
4485 u_write_undo(NULL, FALSE, buf, hash);
4486 curbuf = save_curbuf;
4487 }
4488#endif
4489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004492 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004493 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4494 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // restore this in case an autocommand has set it; it would break
4497 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 need_mouse_correct = save_mouse_correct;
4499#endif
4500
4501 return retval;
4502}
4503
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004504/*
4505 * Reload a buffer that is already loaded.
4506 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004507 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4508 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004509 */
4510 void
Rob Pilling8196e942022-02-11 15:12:10 +00004511buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004512{
4513 exarg_T ea;
4514 pos_T old_cursor;
4515 linenr_T old_topline;
4516 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004517 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004518 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004519 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004520 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004521 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004522 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004523
Bram Moolenaare76062c2022-11-28 18:51:43 +00004524 // Set curwin/curbuf for "buf" and save some things.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004525 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004526 if (curbuf != buf)
4527 {
4528 // Failed to find a window for "buf", it is dangerous to continue,
4529 // better bail out.
4530 return;
4531 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004532
Rob Pilling8196e942022-02-11 15:12:10 +00004533 // Unless reload_options is set, we only want to read the text from the
4534 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4535 // Force the fileformat and encoding to be the same.
4536 if (reload_options)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004537 CLEAR_FIELD(ea);
Rob Pilling8196e942022-02-11 15:12:10 +00004538 else
4539 prepped = prep_exarg(&ea, buf);
4540
4541 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004542 {
4543 old_cursor = curwin->w_cursor;
4544 old_topline = curwin->w_topline;
4545
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004546 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004547 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004548 // Save all the text, so that the reload can be undone.
4549 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004550 u_sync(FALSE);
4551 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4552 flags |= READ_KEEP_UNDO;
4553 }
4554
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004555 /*
4556 * To behave like when a new file is edited (matters for
4557 * BufReadPost autocommands) we first need to delete the current
4558 * buffer contents. But if reading the file fails we should keep
4559 * the old contents. Can't use memory only, the file might be
4560 * too big. Use a hidden buffer to move the buffer contents to.
4561 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004562 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004563 savebuf = NULL;
4564 else
4565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004566 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004567 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004568 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004569 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004570 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004571 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004572 curbuf = savebuf;
4573 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004574 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004575 curbuf = buf;
4576 curwin->w_buffer = buf;
4577 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004578 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004579 || move_lines(buf, savebuf) == FAIL)
4580 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004581 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004582 saved = FAIL;
4583 }
4584 }
4585
4586 if (saved == OK)
4587 {
zeertzjq8a017442024-03-07 21:48:33 +01004588 int old_msg_silent = msg_silent;
4589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004590 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
zeertzjq5bf6c212024-03-31 18:41:27 +02004591 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
zeertzjq8a017442024-03-07 21:48:33 +01004592
4593 if (shortmess(SHM_FILEINFO))
4594 msg_silent = 1;
4595
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004596 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4597 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004598 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004599 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004600#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004601 if (!aborting())
4602#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004603 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004604 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004605 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004606 // Put the text back from the save buffer. First
4607 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004608 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004609 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004610 break;
4611 (void)move_lines(savebuf, buf);
4612 }
4613 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004614 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004616 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004617 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004618 if ((flags & READ_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +01004619 u_clearallandblockfree(buf);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004620 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004621 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004622 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004623 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004624 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004625 }
zeertzjq8a017442024-03-07 21:48:33 +01004626
4627 msg_silent = old_msg_silent;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004628 }
4629 vim_free(ea.cmd);
4630
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004631 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004632 wipe_buffer(savebuf, FALSE);
4633
4634#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004635 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004636 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004637#endif
4638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004639 // Restore the topline and cursor position and check it (lines may
4640 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004641 if (old_topline > curbuf->b_ml.ml_line_count)
4642 curwin->w_topline = curbuf->b_ml.ml_line_count;
4643 else
4644 curwin->w_topline = old_topline;
4645 curwin->w_cursor = old_cursor;
4646 check_cursor();
4647 update_topline();
zeertzjq5bf6c212024-03-31 18:41:27 +02004648 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004649#ifdef FEAT_FOLDING
4650 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004651 win_T *wp;
4652 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004654 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004655 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004656 if (wp->w_buffer == curwin->w_buffer
4657 && !foldmethodIsManual(wp))
4658 foldUpdateAll(wp);
4659 }
4660#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004661 // If the mode didn't change and 'readonly' was set, keep the old
4662 // value; the user probably used the ":view" command. But don't
4663 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004664 if (orig_mode == curbuf->b_orig_mode)
4665 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004668 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004669 }
4670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004671 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004672 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004673 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004674}
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004677buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004680#ifdef ST_MTIM_NSEC
4681 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4682#else
4683 buf->b_mtime_ns = 0;
4684#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004685 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#ifdef HAVE_ST_MODE
4687 buf->b_orig_mode = (int)st->st_mode;
4688#else
4689 buf->b_orig_mode = mch_getperm(fname);
4690#endif
4691}
4692
4693/*
4694 * Adjust the line with missing eol, used for the next write.
4695 * Used for do_filter(), when the input lines for the filter are deleted.
4696 */
4697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004698write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004700 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004701 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702}
4703
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004704// Subfuncions for readdirex()
4705#ifdef FEAT_EVAL
4706# ifdef MSWIN
4707 static char_u *
4708getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4709{
4710 stat_T st;
4711 unsigned short st_mode;
4712 DWORD flag = wfd->dwFileAttributes;
4713 WCHAR *wp;
4714
4715 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4716 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4717 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4718 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4719
4720 wp = wcsrchr(wfd->cFileName, L'.');
4721 if (wp != NULL)
4722 {
4723 if (_wcsicmp(wp, L".exe") == 0 ||
4724 _wcsicmp(wp, L".com") == 0 ||
4725 _wcsicmp(wp, L".cmd") == 0 ||
4726 _wcsicmp(wp, L".bat") == 0)
4727 st_mode |= _S_IEXEC;
4728 }
4729
4730 // Copy user bits to group/other.
4731 st_mode |= (st_mode & 0700) >> 3;
4732 st_mode |= (st_mode & 0700) >> 6;
4733
4734 st.st_mode = st_mode;
4735 return getfpermst(&st, perm);
4736}
4737
4738 static char_u *
4739getftypewfd(WIN32_FIND_DATAW *wfd)
4740{
4741 DWORD flag = wfd->dwFileAttributes;
4742 DWORD tag = wfd->dwReserved0;
4743
4744 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4745 {
4746 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4747 return (char_u*)"junction";
John Marriott14ede182024-12-29 16:14:19 +01004748 if (tag == IO_REPARSE_TAG_SYMLINK)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004749 {
4750 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4751 return (char_u*)"linkd";
John Marriott14ede182024-12-29 16:14:19 +01004752
4753 return (char_u*)"link";
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004754 }
4755 return (char_u*)"reparse"; // unknown reparse point type
4756 }
4757 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4758 return (char_u*)"dir";
John Marriott14ede182024-12-29 16:14:19 +01004759
4760 return (char_u*)"file";
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004761}
4762
4763 static dict_T *
4764create_readdirex_item(WIN32_FIND_DATAW *wfd)
4765{
4766 dict_T *item;
4767 char_u *p;
4768 varnumber_T size, time;
4769 char_u permbuf[] = "---------";
4770
4771 item = dict_alloc();
4772 if (item == NULL)
4773 return NULL;
4774 item->dv_refcount++;
4775
4776 p = utf16_to_enc(wfd->cFileName, NULL);
4777 if (p == NULL)
4778 goto theend;
4779 if (dict_add_string(item, "name", p) == FAIL)
4780 {
4781 vim_free(p);
4782 goto theend;
4783 }
4784 vim_free(p);
4785
4786 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4787 if (dict_add_number(item, "size", size) == FAIL)
4788 goto theend;
4789
4790 // Convert FILETIME to unix time.
4791 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4792 wfd->ftLastWriteTime.dwLowDateTime)
4793 - 116444736000000000) / 10000000;
4794 if (dict_add_number(item, "time", time) == FAIL)
4795 goto theend;
4796
4797 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4798 goto theend;
4799 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4800 goto theend;
4801
4802 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4803 goto theend;
4804 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4805 goto theend;
4806
4807 return item;
4808
4809theend:
4810 dict_unref(item);
4811 return NULL;
4812}
4813# else
4814 static dict_T *
4815create_readdirex_item(char_u *path, char_u *name)
4816{
4817 dict_T *item;
4818 char *p;
4819 size_t len;
4820 stat_T st;
4821 int ret, link = FALSE;
4822 varnumber_T size;
4823 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004824 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004825 struct passwd *pw;
4826 struct group *gr;
4827
4828 item = dict_alloc();
4829 if (item == NULL)
4830 return NULL;
4831 item->dv_refcount++;
4832
4833 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4834 p = alloc(len);
4835 if (p == NULL)
4836 goto theend;
4837 vim_snprintf(p, len, "%s/%s", path, name);
4838 ret = mch_lstat(p, &st);
4839 if (ret >= 0 && S_ISLNK(st.st_mode))
4840 {
4841 link = TRUE;
4842 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004843 if (ret < 0)
4844 q = (char_u*)"link";
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004845 }
4846 vim_free(p);
4847
4848 if (dict_add_string(item, "name", name) == FAIL)
4849 goto theend;
4850
4851 if (ret >= 0)
4852 {
4853 size = (varnumber_T)st.st_size;
4854 if (S_ISDIR(st.st_mode))
4855 size = 0;
4856 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004857 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004858 size = -2;
4859 if (dict_add_number(item, "size", size) == FAIL)
4860 goto theend;
4861 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4862 goto theend;
4863
4864 if (link)
4865 {
4866 if (S_ISDIR(st.st_mode))
4867 q = (char_u*)"linkd";
4868 else
4869 q = (char_u*)"link";
4870 }
4871 else
4872 q = getftypest(&st);
4873 if (dict_add_string(item, "type", q) == FAIL)
4874 goto theend;
4875 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4876 goto theend;
4877
4878 pw = getpwuid(st.st_uid);
4879 if (pw == NULL)
4880 q = (char_u*)"";
4881 else
4882 q = (char_u*)pw->pw_name;
4883 if (dict_add_string(item, "user", q) == FAIL)
4884 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004885# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004886 gr = getgrgid(st.st_gid);
4887 if (gr == NULL)
4888 q = (char_u*)"";
4889 else
4890 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004891# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004892 if (dict_add_string(item, "group", q) == FAIL)
4893 goto theend;
4894 }
4895 else
4896 {
4897 if (dict_add_number(item, "size", -1) == FAIL)
4898 goto theend;
4899 if (dict_add_number(item, "time", -1) == FAIL)
4900 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004901 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004902 goto theend;
4903 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4904 goto theend;
4905 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4906 goto theend;
4907 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4908 goto theend;
4909 }
4910 return item;
4911
4912theend:
4913 dict_unref(item);
4914 return NULL;
4915}
4916# endif
4917
4918 static int
4919compare_readdirex_item(const void *p1, const void *p2)
4920{
4921 char_u *name1, *name2;
4922
Bram Moolenaard61efa52022-07-23 09:52:04 +01004923 name1 = dict_get_string(*(dict_T**)p1, "name", FALSE);
4924 name2 = dict_get_string(*(dict_T**)p2, "name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004925 if (readdirex_sort == READDIR_SORT_BYTE)
4926 return STRCMP(name1, name2);
John Marriott14ede182024-12-29 16:14:19 +01004927 if (readdirex_sort == READDIR_SORT_IC)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004928 return STRICMP(name1, name2);
John Marriott14ede182024-12-29 16:14:19 +01004929
4930 return STRCOLL(name1, name2);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004931}
4932
4933 static int
4934compare_readdir_item(const void *s1, const void *s2)
4935{
4936 if (readdirex_sort == READDIR_SORT_BYTE)
4937 return STRCMP(*(char **)s1, *(char **)s2);
John Marriott14ede182024-12-29 16:14:19 +01004938 if (readdirex_sort == READDIR_SORT_IC)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004939 return STRICMP(*(char **)s1, *(char **)s2);
John Marriott14ede182024-12-29 16:14:19 +01004940
4941 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004942}
4943#endif
4944
Bram Moolenaarda440d22016-01-16 21:27:23 +01004945#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4946/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004947 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004948 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004949 * If "withattr" is TRUE, retrieve the names and their attributes.
4950 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004951 * Return OK for success, FAIL for failure.
4952 */
4953 int
4954readdir_core(
4955 garray_T *gap,
4956 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004957 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004958 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004959 int (*checkitem)(void *context, void *item),
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004960 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004961{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004962 int failed = FALSE;
4963 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004964# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004965 char_u *buf;
4966 int ok;
4967 HANDLE hFind = INVALID_HANDLE_VALUE;
4968 WIN32_FIND_DATAW wfd;
4969 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
John Marriott14ede182024-12-29 16:14:19 +01004970 char_u *p_end;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004971# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004972 DIR *dirp;
4973 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004974# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004975
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004976 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004977
4978# ifdef FEAT_EVAL
4979# define FREE_ITEM(item) do { \
4980 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004981 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004982 else \
4983 vim_free(item); \
4984 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004985
4986 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004987# else
4988# define FREE_ITEM(item) vim_free(item)
4989# endif
4990
4991# ifdef MSWIN
4992 buf = alloc(MAXPATHL);
4993 if (buf == NULL)
4994 return FAIL;
4995 STRNCPY(buf, path, MAXPATHL-5);
John Marriott14ede182024-12-29 16:14:19 +01004996 p = p_end = buf + STRLEN(buf);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004997 MB_PTR_BACK(buf, p);
4998 if (*p == '\\' || *p == '/')
John Marriott14ede182024-12-29 16:14:19 +01004999 p_end = p;
5000 STRCPY(p_end, "\\*");
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005001
5002 wn = enc_to_utf16(buf, NULL);
5003 if (wn != NULL)
5004 hFind = FindFirstFileW(wn, &wfd);
5005 ok = (hFind != INVALID_HANDLE_VALUE);
5006 if (!ok)
5007 {
5008 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005009 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005010 }
5011 else
5012 {
5013 while (ok)
5014 {
5015 int ignore;
5016 void *item;
5017 WCHAR *wp;
5018
5019 wp = wfd.cFileName;
5020 ignore = wp[0] == L'.' &&
5021 (wp[1] == NUL ||
5022 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02005023 if (ignore)
5024 {
5025 ok = FindNextFileW(hFind, &wfd);
5026 continue;
5027 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005028# ifdef FEAT_EVAL
5029 if (withattr)
5030 item = (void*)create_readdirex_item(&wfd);
5031 else
5032# endif
5033 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
5034 if (item == NULL)
5035 {
5036 failed = TRUE;
5037 break;
5038 }
5039
5040 if (!ignore && checkitem != NULL)
5041 {
5042 int r = checkitem(context, item);
5043
5044 if (r < 0)
5045 {
5046 FREE_ITEM(item);
5047 break;
5048 }
5049 if (r == 0)
5050 ignore = TRUE;
5051 }
5052
5053 if (!ignore)
5054 {
5055 if (ga_grow(gap, 1) == OK)
5056 ((void**)gap->ga_data)[gap->ga_len++] = item;
5057 else
5058 {
5059 failed = TRUE;
5060 FREE_ITEM(item);
5061 break;
5062 }
5063 }
5064 else
5065 FREE_ITEM(item);
5066
5067 ok = FindNextFileW(hFind, &wfd);
5068 }
5069 FindClose(hFind);
5070 }
5071
5072 vim_free(buf);
5073 vim_free(wn);
5074# else // MSWIN
5075 dirp = opendir((char *)path);
5076 if (dirp == NULL)
5077 {
5078 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005079 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005080 }
5081 else
5082 {
5083 for (;;)
5084 {
5085 int ignore;
5086 void *item;
5087
5088 dp = readdir(dirp);
5089 if (dp == NULL)
5090 break;
5091 p = (char_u *)dp->d_name;
5092
5093 ignore = p[0] == '.' &&
5094 (p[1] == NUL ||
5095 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02005096 if (ignore)
5097 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005098# ifdef FEAT_EVAL
5099 if (withattr)
5100 item = (void*)create_readdirex_item(path, p);
5101 else
5102# endif
5103 item = (void*)vim_strsave(p);
5104 if (item == NULL)
5105 {
5106 failed = TRUE;
5107 break;
5108 }
5109
Bram Moolenaarfe154992022-03-22 20:42:12 +00005110 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005111 {
5112 int r = checkitem(context, item);
5113
5114 if (r < 0)
5115 {
5116 FREE_ITEM(item);
5117 break;
5118 }
5119 if (r == 0)
5120 ignore = TRUE;
5121 }
5122
5123 if (!ignore)
5124 {
5125 if (ga_grow(gap, 1) == OK)
5126 ((void**)gap->ga_data)[gap->ga_len++] = item;
5127 else
5128 {
5129 failed = TRUE;
5130 FREE_ITEM(item);
5131 break;
5132 }
5133 }
5134 else
5135 FREE_ITEM(item);
5136 }
5137
5138 closedir(dirp);
5139 }
5140# endif // MSWIN
5141
5142# undef FREE_ITEM
5143
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005144 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005145 {
5146# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005147 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005148 if (withattr)
5149 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
5150 compare_readdirex_item);
5151 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005152 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
5153 compare_readdir_item);
5154# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005155 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005156# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02005157 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005158
5159 return failed ? FAIL : OK;
5160}
5161
5162/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01005163 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005164 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005165 */
5166 int
5167delete_recursive(char_u *name)
5168{
5169 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005170
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005171 // A symbolic link to a directory itself is deleted, not the directory it
5172 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005173 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005174# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005175 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005176# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005177 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005178# endif
5179 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005180 {
John Marriott14ede182024-12-29 16:14:19 +01005181 char_u *exp = vim_strsave(name);
5182 garray_T ga;
5183
Bram Moolenaarda440d22016-01-16 21:27:23 +01005184 if (exp == NULL)
5185 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005186 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005187 {
John Marriott14ede182024-12-29 16:14:19 +01005188 int len = vim_snprintf((char *)NameBuff, MAXPATHL, "%s/", exp);
5189 int i;
5190
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005191 for (i = 0; i < ga.ga_len; ++i)
5192 {
John Marriott14ede182024-12-29 16:14:19 +01005193 vim_snprintf((char *)NameBuff + len, MAXPATHL - len, "%s", ((char_u **)ga.ga_data)[i]);
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005194 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005195 // Remember the failure but continue deleting any further
5196 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005197 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005198 }
5199 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005200 if (mch_rmdir(exp) != 0)
5201 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005202 }
5203 else
5204 result = -1;
5205 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005206 }
5207 else
5208 result = mch_remove(name) == 0 ? 0 : -1;
5209
5210 return result;
5211}
5212#endif
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005215static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005217# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5218/*
5219 * Open temporary directory and take file lock to prevent
5220 * to be auto-cleaned.
5221 */
5222 static void
5223vim_opentempdir(void)
5224{
5225 DIR *dp = NULL;
5226
5227 if (vim_tempdir_dp != NULL)
5228 return;
5229
5230 dp = opendir((const char*)vim_tempdir);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005231 if (dp == NULL)
5232 return;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005233
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005234 vim_tempdir_dp = dp;
5235 flock(dirfd(vim_tempdir_dp), LOCK_SH);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005236}
5237
5238/*
5239 * Close temporary directory - it automatically release file lock.
5240 */
5241 static void
5242vim_closetempdir(void)
5243{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005244 if (vim_tempdir_dp == NULL)
5245 return;
5246
5247 closedir(vim_tempdir_dp);
5248 vim_tempdir_dp = NULL;
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005249}
5250# endif
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252/*
5253 * Delete the temp directory and all files it contains.
5254 */
5255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005256vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005258 if (vim_tempdir == NULL)
5259 return;
5260
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005261# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005262 vim_closetempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005263# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005264 // remove the trailing path separator
5265 gettail(vim_tempdir)[-1] = NUL;
5266 delete_recursive(vim_tempdir);
5267 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005271 * Directory "tempdir" was created. Expand this name to a full path and put
5272 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5273 * "tempdir" must be no longer than MAXPATHL.
5274 */
5275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005277{
5278 char_u *buf;
John Marriott14ede182024-12-29 16:14:19 +01005279 size_t buflen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005280
Bram Moolenaar964b3742019-05-24 18:54:09 +02005281 buf = alloc(MAXPATHL + 2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005282 if (buf == NULL)
5283 return;
5284
5285 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5286 STRCPY(buf, tempdir);
John Marriott14ede182024-12-29 16:14:19 +01005287 buflen = STRLEN(buf);
5288 if (!after_pathsep(buf, buf + buflen))
5289 {
5290 STRCPY(buf + buflen, PATHSEPSTR);
5291 buflen += STRLEN_LITERAL(PATHSEPSTR);
5292 }
5293 vim_tempdir = vim_strnsave(buf, buflen);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005294# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005295 vim_opentempdir();
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005296# endif
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005297 vim_free(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005298}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005299#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005300
5301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 * vim_tempname(): Return a unique name that can be used for a temp file.
5303 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005304 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5305 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 *
5307 * The returned pointer is to allocated memory.
5308 * The returned pointer is NULL if no valid name was found.
5309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005316 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005317#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005318 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319#else
5320 char_u itmp[TEMPNAMELEN];
5321#endif
5322
5323#ifdef TEMPDIRNAMES
5324 static char *(tempdirs[]) = {TEMPDIRNAMES};
5325 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005327 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328# endif
5329
5330 /*
5331 * This will create a directory for private use by this instance of Vim.
5332 * This is done once, and the same directory is used for all temp files.
5333 * This method avoids security problems because of symlink attacks et al.
5334 * It's also a bit faster, because we only need to check for an existing
5335 * file when creating the directory and not for each temp file.
5336 */
5337 if (vim_tempdir == NULL)
5338 {
5339 /*
5340 * Try the entries in TEMPDIRNAMES to create the temp directory.
5341 */
K.Takataeeec2542021-06-02 13:28:16 +02005342 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005344# ifndef HAVE_MKDTEMP
5345 long nr;
5346 long off;
5347# endif
5348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005349 // Expand $TMP, leave room for "/v1100000/999999999".
5350 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005352 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
John Marriott14ede182024-12-29 16:14:19 +01005354 size_t itmplen = STRLEN(itmp);
5355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005356 // directory exists
John Marriott14ede182024-12-29 16:14:19 +01005357 if (!after_pathsep(itmp, itmp + itmplen))
5358 {
5359 STRCPY(itmp + itmplen, PATHSEPSTR);
5360 itmplen += STRLEN_LITERAL(PATHSEPSTR);
5361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
Bram Moolenaareaf03392009-11-17 11:08:52 +00005363# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005364 {
5365# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005366 // Make sure the umask doesn't remove the executable bit.
5367 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005368 mode_t umask_save = umask(077);
5369# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 // Leave room for filename
John Marriott14ede182024-12-29 16:14:19 +01005371 STRCPY(itmp + itmplen, "vXXXXXX");
5372 itmplen += STRLEN_LITERAL("vXXXXXX");
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005373 if (mkdtemp((char *)itmp) != NULL)
5374 vim_settempdir(itmp);
5375# if defined(UNIX) || defined(VMS)
5376 (void)umask(umask_save);
5377# endif
5378 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005379# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 // Get an arbitrary number of up to 6 digits. When it's
5381 // unlikely that it already exists it will be faster,
5382 // otherwise it doesn't matter. The use of mkdir() avoids any
5383 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
5385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005386 // Try up to 10000 different values until we find a name that
5387 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 for (off = 0; off < 10000L; ++off)
5389 {
5390 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005391# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
John Marriott14ede182024-12-29 16:14:19 +01005395 vim_snprintf((char *)itmp + itmplen, sizeof(itmp) - itmplen, "v%ld", nr + off);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005396# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005397 // If mkdir() does not set errno to EEXIST, check for
5398 // existing file here. There is a race condition then,
5399 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 if (mch_stat((char *)itmp, &st) >= 0)
5401 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005402# endif
5403# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005404 // Make sure the umask doesn't remove the executable bit.
5405 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005409# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (r == 0)
5413 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005414 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 break;
5416 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005417# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 // If the mkdir() didn't fail because the file/dir exists,
5419 // we probably can't create any dir here, try another
5420 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 break;
5424 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005425# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (vim_tempdir != NULL)
5427 break;
5428 }
5429 }
5430 }
5431
5432 if (vim_tempdir != NULL)
5433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 // There is no need to check if the file exists, because we own the
5435 // directory and nobody else creates a file in it.
John Marriott14ede182024-12-29 16:14:19 +01005436 int itmplen = vim_snprintf((char *)itmp, sizeof(itmp), "%s%ld", vim_tempdir, temp_count++);
5437 return vim_strnsave(itmp, (size_t)itmplen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439
5440 return NULL;
5441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005442#else // TEMPDIRNAMES
John Marriott14ede182024-12-29 16:14:19 +01005443 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar4f974752019-02-17 17:44:42 +01005445# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005446 WCHAR wszTempFile[_MAX_PATH + 1];
5447 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005448 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 char_u *retval;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005450 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005451 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005453 wcscpy(itmp, L"");
5454 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005455 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005456 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005457 wszTempFile[1] = L'\\';
5458 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005459 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005460 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005461
5462 // randomize the name to avoid collisions
5463 i = mch_get_pid() + extra_char;
5464 buf4[1] = chartab[i % 36];
5465 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005466 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005468 if (!keep)
John Marriott14ede182024-12-29 16:14:19 +01005469 // GetTempFileNameW() will create the file, we don't want that
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005470 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005472 // Backslashes in a temp file name cause problems when filtering with
5473 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005474 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005475 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005476 shname = gettail(p_sh);
5477 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5478 || strstr((char *)shname, "pwsh") != NULL ))
5479 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 for (p = retval; *p; ++p)
5481 if (*p == '\\')
5482 *p = '/';
5483 return retval;
5484
Bram Moolenaar4f974752019-02-17 17:44:42 +01005485# else // MSWIN
John Marriott14ede182024-12-29 16:14:19 +01005486 int itmplen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488# ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005489 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005490 p = tmpnam((char *)itmp);
5491 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 return NULL;
5493# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005496 // mktemp() is not working on VMS. It seems to be
5497 // a do-nothing function. Therefore we use tempnam().
John Marriott14ede182024-12-29 16:14:19 +01005498 vim_snprintf((char *)itmp, sizeof(itmp), "VIM%c", extra_char);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 p = (char_u *)tempnam("tmp:", (char *)itmp);
5500 if (p != NULL)
5501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005502 // VMS will use '.LIS' if we don't explicitly specify an extension,
5503 // and VIM will then be unable to find the file later
John Marriott14ede182024-12-29 16:14:19 +01005504 itmplen = vim_snprintf((char *)itmp, sizeof(itmp), "%s.txt", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 free(p);
5506 }
5507 else
5508 return NULL;
5509# else
5510 STRCPY(itmp, TEMPNAME);
John Marriott14ede182024-12-29 16:14:19 +01005511 itmplen = STRLEN_LITERAL(TEMPNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if ((p = vim_strchr(itmp, '?')) != NULL)
5513 *p = extra_char;
5514 if (mktemp((char *)itmp) == NULL)
5515 return NULL;
5516# endif
5517# endif
5518
John Marriott14ede182024-12-29 16:14:19 +01005519 return vim_strnsave(itmp, (size_t)itmplen);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005520# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523
5524#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5525/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005526 * Convert all backslashes in fname to forward slashes in-place, unless when
5527 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 */
5529 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 char_u *p;
5533
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005534 if (path_with_url(fname))
5535 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005537 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005538 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005540 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 *p = '/';
5542}
5543#endif
5544
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005545/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005546 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5547 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5548 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * Used for autocommands and 'wildignore'.
5550 * Returns TRUE if there is a match, FALSE otherwise.
5551 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005552 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005554 char_u *pattern, // pattern to match with
5555 regprog_T **prog, // pre-compiled regprog or NULL
5556 char_u *fname, // full path of file name
5557 char_u *sfname, // short file name or NULL
5558 char_u *tail, // tail of path
5559 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 regmatch_T regmatch;
5562 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005564 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005565 if (prog != NULL)
5566 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005568 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570 /*
5571 * Try for a match with the pattern with:
5572 * 1. the full file name, when the pattern has a '/'.
5573 * 2. the short file name, when the pattern has a '/'.
5574 * 3. the tail of the file name, when the pattern has no '/'.
5575 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005576 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 && ((allow_dirs
5578 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5579 || (sfname != NULL
5580 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005581 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 result = TRUE;
5583
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005584 if (prog != NULL)
5585 *prog = regmatch.regprog;
5586 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005587 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 return result;
5589}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591/*
5592 * Return TRUE if a file matches with a pattern in "list".
5593 * "list" is a comma-separated list of patterns, like 'wildignore'.
5594 * "sfname" is the short file name or NULL, "ffname" the long file name.
5595 */
5596 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005599 char_u buf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 char_u *tail;
5601 char_u *regpat;
5602 char allow_dirs;
5603 int match;
5604 char_u *p;
5605
5606 tail = gettail(sfname);
5607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005608 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 p = list;
5610 while (*p)
5611 {
Christian Brabandt54f50cb2023-06-16 21:42:06 +01005612 copy_option_part(&p, buf, MAXPATHL, ",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5614 if (regpat == NULL)
5615 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005616 match = match_file_pat(regpat, NULL, ffname, sfname,
5617 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 vim_free(regpat);
5619 if (match)
5620 return TRUE;
5621 }
5622 return FALSE;
5623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625/*
5626 * Convert the given pattern "pat" which has shell style wildcards in it, into
5627 * a regular expression, and return the result in allocated memory. If there
5628 * is a directory path separator to be matched, then TRUE is put in
5629 * allow_dirs, otherwise FALSE is put there -- webb.
5630 * Handle backslashes before special characters, like "\*" and "\ ".
5631 *
Christian Brabandt1a31c432024-10-06 16:34:20 +02005632 * no_bslash only makes a difference, when BACKSLASH_IN_FILENAME is defined
5633 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 * Returns NULL when out of memory.
5635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637file_pat_to_reg_pat(
5638 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005639 char_u *pat_end, // first char after pattern or NULL
5640 char *allow_dirs, // Result passed back out in here
5641 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005643 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 char_u *endp;
5645 char_u *reg_pat;
5646 char_u *p;
5647 int i;
5648 int nested = 0;
5649 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 if (allow_dirs != NULL)
5652 *allow_dirs = FALSE;
5653 if (pat_end == NULL)
5654 pat_end = pat + STRLEN(pat);
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 for (p = pat; p < pat_end; p++)
5657 {
5658 switch (*p)
5659 {
5660 case '*':
5661 case '.':
5662 case ',':
5663 case '{':
5664 case '}':
5665 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005666 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 break;
5668#ifdef BACKSLASH_IN_FILENAME
5669 case '\\':
5670 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005671 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 break;
5673#endif
5674 default:
5675 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005676 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
5678 ++p;
5679 ++size;
5680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 break;
5682 }
5683 }
5684 reg_pat = alloc(size + 1);
5685 if (reg_pat == NULL)
5686 return NULL;
5687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 if (pat[0] == '*')
5691 while (pat[0] == '*' && pat < pat_end - 1)
5692 pat++;
5693 else
5694 reg_pat[i++] = '^';
5695 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005696 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 while (endp - pat > 0 && *endp == '*')
5699 endp--;
5700 add_dollar = FALSE;
5701 }
5702 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5703 {
5704 switch (*p)
5705 {
5706 case '*':
5707 reg_pat[i++] = '.';
5708 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005709 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005710 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 case '~':
5714 reg_pat[i++] = '\\';
5715 reg_pat[i++] = *p;
5716 break;
5717 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 reg_pat[i++] = '.';
5719 break;
5720 case '\\':
5721 if (p[1] == NUL)
5722 break;
5723#ifdef BACKSLASH_IN_FILENAME
5724 if (!no_bslash)
5725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005726 // translate:
5727 // "\x" to "\\x" e.g., "dir\file"
5728 // "\*" to "\\.*" e.g., "dir\*.c"
5729 // "\?" to "\\." e.g., "dir\??.c"
5730 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5732 && p[1] != '+')
5733 {
5734 reg_pat[i++] = '[';
5735 reg_pat[i++] = '\\';
5736 reg_pat[i++] = '/';
5737 reg_pat[i++] = ']';
5738 if (allow_dirs != NULL)
5739 *allow_dirs = TRUE;
5740 break;
5741 }
5742 }
5743#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005744 // Undo escaping from ExpandEscape():
5745 // foo\?bar -> foo?bar
5746 // foo\%bar -> foo%bar
5747 // foo\,bar -> foo,bar
5748 // foo\ bar -> foo bar
5749 // Don't unescape \, * and others that are also special in a
5750 // regexp.
5751 // An escaped { must be unescaped since we use magic not
5752 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (*++p == '?'
5754#ifdef BACKSLASH_IN_FILENAME
5755 && no_bslash
5756#endif
5757 )
5758 reg_pat[i++] = '?';
5759 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005760 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005761 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005762 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005763 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5764 {
5765 reg_pat[i++] = '\\';
5766 reg_pat[i++] = '{';
5767 p += 2;
5768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 else
5770 {
5771 if (allow_dirs != NULL && vim_ispathsep(*p)
5772#ifdef BACKSLASH_IN_FILENAME
5773 && (!no_bslash || *p != '\\')
5774#endif
5775 )
5776 *allow_dirs = TRUE;
5777 reg_pat[i++] = '\\';
Christian Brabandtc9bfed22024-08-29 22:12:05 +02005778 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
5779 reg_pat[i++] = *p++;
Christian Brabandtf459d682024-08-28 23:46:53 +02005780 reg_pat[i++] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782 break;
5783#ifdef BACKSLASH_IN_FILENAME
5784 case '/':
5785 reg_pat[i++] = '[';
5786 reg_pat[i++] = '\\';
5787 reg_pat[i++] = '/';
5788 reg_pat[i++] = ']';
5789 if (allow_dirs != NULL)
5790 *allow_dirs = TRUE;
5791 break;
5792#endif
5793 case '{':
5794 reg_pat[i++] = '\\';
5795 reg_pat[i++] = '(';
5796 nested++;
5797 break;
5798 case '}':
5799 reg_pat[i++] = '\\';
5800 reg_pat[i++] = ')';
5801 --nested;
5802 break;
5803 case ',':
5804 if (nested)
5805 {
5806 reg_pat[i++] = '\\';
5807 reg_pat[i++] = '|';
5808 }
5809 else
5810 reg_pat[i++] = ',';
5811 break;
5812 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005813 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005815 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 *allow_dirs = TRUE;
5817 reg_pat[i++] = *p;
5818 break;
5819 }
5820 }
5821 if (add_dollar)
5822 reg_pat[i++] = '$';
5823 reg_pat[i] = NUL;
5824 if (nested != 0)
5825 {
5826 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005827 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005829 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005830 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
5832 return reg_pat;
5833}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005834
5835#if defined(EINTR) || defined(PROTO)
5836/*
5837 * Version of read() that retries when interrupted by EINTR (possibly
5838 * by a SIGWINCH).
5839 */
5840 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005841read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005842{
5843 long ret;
5844
5845 for (;;)
5846 {
5847 ret = vim_read(fd, buf, bufsize);
5848 if (ret >= 0 || errno != EINTR)
5849 break;
5850 }
5851 return ret;
5852}
5853
5854/*
5855 * Version of write() that retries when interrupted by EINTR (possibly
5856 * by a SIGWINCH).
5857 */
5858 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005860{
5861 long ret = 0;
5862 long wlen;
5863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005864 // Repeat the write() so long it didn't fail, other than being interrupted
5865 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005866 while (ret < (long)bufsize)
5867 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005868 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005869 if (wlen < 0)
5870 {
5871 if (errno != EINTR)
5872 break;
5873 }
5874 else
5875 ret += wlen;
5876 }
5877 return ret;
5878}
5879#endif