blob: 439789e5987c52da0334621a4d5d08893d198216 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaare3f915d2020-07-14 23:02:44 +020016#if defined(__TANDEM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +010017# include <limits.h> // for SSIZE_MAX
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010019#if (defined(UNIX) || defined(VMS)) && defined(FEAT_EVAL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +020020# include <pwd.h>
21# include <grp.h>
22#endif
Bram Moolenaar82c38fe2021-01-04 10:47:26 +010023#if defined(VMS) && defined(HAVE_XOS_R_H)
24# include <x11/xos_r.h>
25#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar217e1b82019-12-01 21:41:28 +010027// Is there any system that doesn't have access()?
Bram Moolenaar9372a112005-12-06 19:59:18 +000028#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar9d8bfae2020-09-02 21:21:35 +020030#if defined(__hpux) && !defined(HAVE_DIRFD)
31# define dirfd(x) ((x)->__dd_fd)
32# define HAVE_DIRFD
33#endif
34
Bram Moolenaarf077db22019-08-13 00:18:24 +020035static char_u *next_fenc(char_u **pp, int *alloced);
Bram Moolenaar13505972019-01-24 15:04:48 +010036#ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010037static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +020040static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +000044
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +020045#ifdef FEAT_EVAL
46static int readdirex_sort;
47#endif
48
Bram Moolenaar473952e2019-09-28 16:30:04 +020049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010050filemess(
51 buf_T *buf,
52 char_u *name,
53 char_u *s,
54 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055{
56 int msg_scroll_save;
Bram Moolenaar473952e2019-09-28 16:30:04 +020057 int prev_msg_col = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
59 if (msg_silent != 0)
60 return;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010061 msg_add_fname(buf, name); // put file name in IObuff with quotes
Bram Moolenaar6378b212020-06-29 21:32:06 +020062
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 // If it's extremely long, truncate it.
Bram Moolenaar6378b212020-06-29 21:32:06 +020064 if (STRLEN(IObuff) > IOSIZE - 100)
65 IObuff[IOSIZE - 100] = NUL;
66
67 // Avoid an over-long translation to cause trouble.
68 STRNCAT(IObuff, s, 99);
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 /*
71 * For the first message may have to start a new line.
72 * For further ones overwrite the previous one, reset msg_scroll before
73 * calling filemess().
74 */
75 msg_scroll_save = msg_scroll;
76 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
77 msg_scroll = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010078 if (!msg_scroll) // wait a bit when overwriting an error msg
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 check_for_delay(FALSE);
80 msg_start();
Bram Moolenaar473952e2019-09-28 16:30:04 +020081 if (prev_msg_col != 0 && msg_col == 0)
82 msg_putchar('\r'); // overwrite any previous message.
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 msg_scroll = msg_scroll_save;
84 msg_scrolled_ign = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010085 // may truncate the message to avoid a hit-return prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
87 msg_clr_eos();
88 out_flush();
89 msg_scrolled_ign = FALSE;
90}
91
92/*
93 * Read lines from file "fname" into the buffer after line "from".
94 *
95 * 1. We allocate blocks with lalloc, as big as possible.
96 * 2. Each block is filled with characters from the file with a single read().
97 * 3. The lines are inserted in the buffer with ml_append().
98 *
99 * (caller must check that fname != NULL, unless READ_STDIN is used)
100 *
101 * "lines_to_skip" is the number of lines that must be skipped
102 * "lines_to_read" is the number of lines that are appended
103 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
104 *
105 * flags:
106 * READ_NEW starting to edit a new buffer
107 * READ_FILTER reading filter output
108 * READ_STDIN read from stdin instead of a file
109 * READ_BUFFER read from curbuf instead of a file (converting after reading
110 * stdin)
111 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200112 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200113 * READ_FIFO read from fifo/socket instead of a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 *
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100115 * return FAIL for failure, NOTDONE for directory (failure), or OK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 */
117 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100118readfile(
119 char_u *fname,
120 char_u *sfname,
121 linenr_T from,
122 linenr_T lines_to_skip,
123 linenr_T lines_to_read,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100124 exarg_T *eap, // can be NULL!
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100125 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126{
127 int fd = 0;
128 int newfile = (flags & READ_NEW);
129 int check_readonly;
130 int filtering = (flags & READ_FILTER);
131 int read_stdin = (flags & READ_STDIN);
132 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200133 int read_fifo = (flags & READ_FIFO);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000134 int set_options = newfile || read_buffer
135 || (eap != NULL && eap->read_edit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100136 linenr_T read_buf_lnum = 1; // next line to read from curbuf
137 colnr_T read_buf_col = 0; // next char to read from this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 char_u c;
139 linenr_T lnum = from;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100140 char_u *ptr = NULL; // pointer into read buffer
141 char_u *buffer = NULL; // read buffer
142 char_u *new_buffer = NULL; // init to shut up gcc
143 char_u *line_start = NULL; // init to shut up gcc
144 int wasempty; // buffer was empty before reading
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 colnr_T len;
146 long size = 0;
147 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200148 off_T filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 int skip_read = FALSE;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200150 off_T filesize_disk = 0; // file size read from disk
151 off_T filesize_count = 0; // counter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#ifdef FEAT_CRYPT
153 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200154 int did_ask_for_key = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200156#ifdef FEAT_PERSISTENT_UNDO
157 context_sha256_T sha_ctx;
158 int read_undo_file = FALSE;
159#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100160 int split = 0; // number of split lines
161#define UNKNOWN 0x0fffffff // file size is unknown
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 linenr_T linecnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100163 int error = FALSE; // errors encountered
164 int ff_error = EOL_UNKNOWN; // file format with errors
165 long linerest = 0; // remaining chars in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef UNIX
167 int perm = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100168 int swap_mode = -1; // protection bits for swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#else
170 int perm;
171#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100172 int fileformat = 0; // end-of-line format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 int keep_fileformat = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200174 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 int file_readonly;
176 linenr_T skip_count = 0;
177 linenr_T read_count = 0;
178 int msg_save = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100179 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of
180 // last read was missing the eol
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100181 int try_mac;
182 int try_dos;
183 int try_unix;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 int file_rewind = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int can_retry;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 linenr_T conv_error = 0; // line nr with conversion error
187 linenr_T illegal_byte = 0; // line nr with illegal byte
188 int keep_dest_enc = FALSE; // don't retry when char doesn't fit
189 // in destination encoding
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000190 int bad_char_behavior = BAD_REPLACE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100191 // BAD_KEEP, BAD_DROP or character to
192 // replace with
193 char_u *tmpname = NULL; // name of 'charconvert' output file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 int fio_flags = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100195 char_u *fenc; // fileencoding to use
196 int fenc_alloced; // fenc_next is in allocated memory
197 char_u *fenc_next = NULL; // next item in 'fencs' or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 int advance_fenc = FALSE;
199 long real_size = 0;
Bram Moolenaar13505972019-01-24 15:04:48 +0100200#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100201 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1
Bram Moolenaar13505972019-01-24 15:04:48 +0100202# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100203 int did_iconv = FALSE; // TRUE when iconv() failed and trying
204 // 'charconvert' next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205# endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100206#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100207 int converted = FALSE; // TRUE if conversion done
208 int notconverted = FALSE; // TRUE if conversion wanted but it
209 // wasn't possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 char_u conv_rest[CONV_RESTLEN];
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100211 int conv_restlen = 0; // nr of bytes in conv_rest[]
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100212 pos_T orig_start;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200213 buf_T *old_curbuf;
214 char_u *old_b_ffname;
215 char_u *old_b_fname;
216 int using_b_ffname;
217 int using_b_fname;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200218 static char *msg_is_a_directory = N_("is a directory");
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200219 int eof;
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200220
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100221 au_did_filetype = FALSE; // reset before triggering any autocommands
Bram Moolenaarc3691332016-04-20 12:49:49 +0200222
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100223 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
225 /*
226 * If there is no file name yet, use the one for the read file.
227 * BF_NOTEDITED is set to reflect this.
228 * Don't do this for a read from a filter.
229 * Only do this when 'cpoptions' contains the 'f' flag.
230 */
231 if (curbuf->b_ffname == NULL
232 && !filtering
233 && fname != NULL
234 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
235 && !(flags & READ_DUMMY))
236 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000237 if (set_rw_fname(fname, sfname) == FAIL)
238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 }
240
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100241 // Remember the initial values of curbuf, curbuf->b_ffname and
242 // curbuf->b_fname to detect whether they are altered as a result of
243 // executing nasty autocommands. Also check if "fname" and "sfname"
244 // point to one of these values.
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200245 old_curbuf = curbuf;
246 old_b_ffname = curbuf->b_ffname;
247 old_b_fname = curbuf->b_fname;
248 using_b_ffname = (fname == curbuf->b_ffname)
249 || (sfname == curbuf->b_ffname);
250 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200251
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100252 // After reading a file the cursor line changes but we don't want to
253 // display the line.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000254 ex_no_reprint = TRUE;
255
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100256 // don't display the file info for another buffer now
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000257 need_fileinfo = FALSE;
258
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 /*
260 * For Unix: Use the short file name whenever possible.
261 * Avoids problems with networks and when directory names are changed.
262 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
263 * another directory, which we don't detect.
264 */
265 if (sfname == NULL)
266 sfname = fname;
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200267#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 fname = sfname;
269#endif
270
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 /*
272 * The BufReadCmd and FileReadCmd events intercept the reading process by
273 * executing the associated commands instead.
274 */
275 if (!filtering && !read_stdin && !read_buffer)
276 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100277 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100279 // Set '[ mark to the line above where the lines go (line 1 if zero).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
281 curbuf->b_op_start.col = 0;
282
283 if (newfile)
284 {
285 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
286 FALSE, curbuf, eap))
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200287 {
288 int status = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifdef FEAT_EVAL
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200290 if (aborting())
291 status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
Bram Moolenaar0fff4412020-03-29 16:06:29 +0200293 // The BufReadCmd code usually uses ":read" to get the text and
294 // perhaps ":file" to change the buffer name. But we should
295 // consider this to work like ":edit", thus reset the
296 // BF_NOTEDITED flag. Then ":write" will work to overwrite the
297 // same file.
298 if (status == OK)
299 curbuf->b_flags &= ~BF_NOTEDITED;
300 return status;
301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 }
303 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
304 FALSE, NULL, eap))
305#ifdef FEAT_EVAL
306 return aborting() ? FAIL : OK;
307#else
308 return OK;
309#endif
310
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100311 curbuf->b_op_start = orig_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100315 msg_scroll = FALSE; // overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100317 msg_scroll = TRUE; // don't overwrite previous file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000319 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200321 size_t namelen = STRLEN(fname);
322
323 // If the name is too long we might crash further on, quit here.
324 if (namelen >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000325 {
326 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
327 msg_end();
328 msg_scroll = msg_save;
329 return FAIL;
330 }
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200331
332 // If the name ends in a path separator, we can't open it. Check here,
333 // because reading the file may actually work, but then creating the
334 // swap file may destroy it! Reported on MS-DOS and Win 95.
335 if (after_pathsep(fname, fname + namelen))
336 {
337 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
338 msg_end();
339 msg_scroll = msg_save;
Bram Moolenaar40fa12a2021-09-22 14:18:13 +0200340 return NOTDONE;
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 }
343
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200344 if (!read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
Bram Moolenaar82c38fe2021-01-04 10:47:26 +0100346#if defined(UNIX) || defined(VMS)
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200347 /*
348 * On Unix it is possible to read a directory, so we have to
349 * check for it before the mch_open().
350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 perm = mch_getperm(fname);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100352 if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
353 && !S_ISFIFO(perm) // ... or fifo
354 && !S_ISSOCK(perm) // ... or socket
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000355# ifdef OPEN_CHR_FILES
356 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100357 // ... or a character special file named /dev/fd/<n>
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 )
360 {
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100361 int retval = FAIL;
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (S_ISDIR(perm))
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100364 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200365 filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100366 retval = NOTDONE;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else
369 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
370 msg_end();
371 msg_scroll = msg_save;
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100372 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 }
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200374#endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100375#if defined(MSWIN)
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000376 /*
377 * MS-Windows allows opening a device, but we will probably get stuck
378 * trying to read it.
379 */
380 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
381 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000382 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000383 msg_end();
384 msg_scroll = msg_save;
385 return FAIL;
386 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000387#endif
Bram Moolenaar4e4f5292013-08-30 17:07:01 +0200388 }
Bram Moolenaar043545e2006-10-10 16:44:07 +0000389
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390 // Set default or forced 'fileformat' and 'binary'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200391 set_file_options(set_options, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
393 /*
394 * When opening a new file we take the readonly flag from the file.
395 * Default is r/w, can be set to r/o below.
396 * Don't reset it when in readonly mode
397 * Only set/reset b_p_ro when BF_CHECK_RO is set.
398 */
399 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000400 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 curbuf->b_p_ro = FALSE;
402
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200403 if (newfile && !read_stdin && !read_buffer && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 // Remember time of file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (mch_stat((char *)fname, &st) >= 0)
407 {
408 buf_store_time(curbuf, &st, fname);
409 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100410 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200411 filesize_disk = st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#ifdef UNIX
413 /*
414 * Use the protection bits of the original file for the swap file.
415 * This makes it possible for others to read the name of the
416 * edited file from the swapfile, but only if they can read the
417 * edited file.
418 * Remove the "write" and "execute" bits for group and others
419 * (they must not write the swapfile).
420 * Add the "read" and "write" bits for the user, otherwise we may
421 * not be able to write to the file ourselves.
422 * Setting the bits is done below, after creating the swap file.
423 */
424 swap_mode = (st.st_mode & 0644) | 0600;
425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef VMS
427 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000428 curbuf->b_fab_rat = st.st_fab_rat;
429 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#endif
431 }
432 else
433 {
434 curbuf->b_mtime = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100435 curbuf->b_mtime_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 curbuf->b_mtime_read = 0;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +0100437 curbuf->b_mtime_read_ns = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 curbuf->b_orig_size = 0;
439 curbuf->b_orig_mode = 0;
440 }
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Reset the "new file" flag. It will be set again below when the
443 // file doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
445 }
446
447/*
448 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100449 * for Amiga: check readonly by trying to open the file for writing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 */
451 file_readonly = FALSE;
452 if (read_stdin)
453 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100454#if defined(MSWIN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100455 // Force binary I/O on stdin to avoid CR-LF -> LF conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 setmode(0, O_BINARY);
457#endif
458 }
459 else if (!read_buffer)
460 {
461#ifdef USE_MCH_ACCESS
462 if (
463# ifdef UNIX
464 !(perm & 0222) ||
465# endif
466 mch_access((char *)fname, W_OK))
467 file_readonly = TRUE;
468 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
469#else
470 if (!newfile
471 || readonlymode
472 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
473 {
474 file_readonly = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 // try to open ro
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
477 }
478#endif
479 }
480
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100481 if (fd < 0) // cannot open at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
483#ifndef UNIX
484 int isdir_f;
485#endif
486 msg_scroll = msg_save;
487#ifndef UNIX
488 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100489 * On Amiga we can't open a directory, check here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 */
491 isdir_f = (mch_isdir(fname));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 perm = mch_getperm(fname); // check if the file exists
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (isdir_f)
494 {
Bram Moolenaarc8fe6452020-10-03 17:04:37 +0200495 filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 else
499#endif
500 if (newfile)
501 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200502 if (perm < 0
503#ifdef ENOENT
504 && errno == ENOENT
505#endif
506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /*
509 * Set the 'new-file' flag, so that when the file has
510 * been created by someone else, a ":w" will complain.
511 */
512 curbuf->b_flags |= BF_NEW;
513
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100514 // Create a swap file now, so that other Vims are warned
515 // that we are editing this file. Don't do this for a
516 // "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517#ifdef FEAT_QUICKFIX
518 if (!bt_dontwrite(curbuf))
519#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 check_need_swap(newfile);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100522 // SwapExists autocommand may mess things up
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000523 if (curbuf != old_curbuf
524 || (using_b_ffname
525 && (old_b_ffname != curbuf->b_ffname))
526 || (using_b_fname
527 && (old_b_fname != curbuf->b_fname)))
528 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000529 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000530 return FAIL;
531 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000532 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000533 if (dir_of_file_exists(fname))
Bram Moolenaar722e5052020-06-12 22:31:00 +0200534 filemess(curbuf, sfname,
535 (char_u *)new_file_message(), 0);
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000536 else
537 filemess(curbuf, sfname,
538 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef FEAT_VIMINFO
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 // Even though this is a new file, it might have been
541 // edited before and deleted. Get the old marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 check_marks_read();
543#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 // Set forced 'fileencoding'.
Bram Moolenaarad875fb2013-07-24 15:02:03 +0200545 if (eap != NULL)
546 set_forced_fenc(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
548 FALSE, curbuf, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100549 // remember the current fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 save_file_ff(curbuf);
551
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100552#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100553 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 return FAIL;
555#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100556 return OK; // a new file is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 }
558 else
559 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000560 filemess(curbuf, sfname, (char_u *)(
561# ifdef EFBIG
562 (errno == EFBIG) ? _("[File too big]") :
563# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200564# ifdef EOVERFLOW
565 (errno == EOVERFLOW) ? _("[File too big]") :
566# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000567 _("[Permission Denied]")), 0);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100568 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
570 }
571
572 return FAIL;
573 }
574
575 /*
576 * Only set the 'ro' flag for readonly files the first time they are
577 * loaded. Help files always get readonly mode
578 */
579 if ((check_readonly && file_readonly) || curbuf->b_help)
580 curbuf->b_p_ro = TRUE;
581
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100584 // Don't change 'eol' if reading from buffer as it will already be
585 // correctly set when reading stdin.
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000586 if (!read_buffer)
587 {
588 curbuf->b_p_eol = TRUE;
589 curbuf->b_start_eol = TRUE;
590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000592 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100595 // Create a swap file now, so that other Vims are warned that we are
596 // editing this file.
597 // Don't do this for a "nofile" or "nowrite" buffer type.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#ifdef FEAT_QUICKFIX
599 if (!bt_dontwrite(curbuf))
600#endif
601 {
602 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000603 if (!read_stdin && (curbuf != old_curbuf
604 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
605 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
606 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000607 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000608 if (!read_buffer)
609 close(fd);
610 return FAIL;
611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100613 // Set swap file protection bits after creating it.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000614 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
615 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100616 {
617 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
618
619 /*
620 * If the group-read bit is set but not the world-read bit, then
621 * the group must be equal to the group of the original file. If
622 * we can't make that happen then reset the group-read bit. This
623 * avoids making the swap file readable to more users when the
624 * primary group of the user is too permissive.
625 */
626 if ((swap_mode & 044) == 040)
627 {
628 stat_T swap_st;
629
630 if (mch_stat((char *)swap_fname, &swap_st) >= 0
631 && st.st_gid != swap_st.st_gid
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200632# ifdef HAVE_FCHOWN
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100633 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200634 == -1
Bram Moolenaar1f131ae2018-05-21 13:39:40 +0200635# endif
Bram Moolenaar02e802b2018-04-19 21:15:27 +0200636 )
Bram Moolenaar5a73e0c2017-11-04 21:35:01 +0100637 swap_mode &= 0600;
638 }
639
640 (void)mch_setperm(swap_fname, (long)swap_mode);
641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643 }
644
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200645 // If "Quit" selected at ATTENTION dialog, don't load the file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (swap_exists_action == SEA_QUIT)
647 {
648 if (!read_buffer && !read_stdin)
649 close(fd);
650 return FAIL;
651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 ++no_wait_return; // don't wait for return yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 /*
656 * Set '[ mark to the line above where the lines go (line 1 if zero).
657 */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100658 orig_start = curbuf->b_op_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
660 curbuf->b_op_start.col = 0;
661
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100662 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
663 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
664 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (!read_buffer)
667 {
668 int m = msg_scroll;
669 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671 /*
672 * The file must be closed again, the autocommands may want to change
673 * the file before reading it.
674 */
675 if (!read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 close(fd); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 /*
679 * The output from the autocommands should not overwrite anything and
680 * should not be overwritten: Set msg_scroll, restore its value if no
681 * output was done.
682 */
683 msg_scroll = TRUE;
684 if (filtering)
685 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
686 FALSE, curbuf, eap);
687 else if (read_stdin)
688 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
689 FALSE, curbuf, eap);
690 else if (newfile)
691 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
692 FALSE, curbuf, eap);
693 else
694 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
695 FALSE, NULL, eap);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // autocommands may have changed it
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100697 try_mac = (vim_strchr(p_ffs, 'm') != NULL);
698 try_dos = (vim_strchr(p_ffs, 'd') != NULL);
699 try_unix = (vim_strchr(p_ffs, 'x') != NULL);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100700 curbuf->b_op_start = orig_start;
Bram Moolenaar7a2699e2017-01-23 21:31:09 +0100701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (msg_scrolled == n)
703 msg_scroll = m;
704
705#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 --no_wait_return;
709 msg_scroll = msg_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return FAIL;
712 }
713#endif
714 /*
715 * Don't allow the autocommands to change the current buffer.
716 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000717 *
718 * Don't allow the autocommands to change the buffer name either
719 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 */
721 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000722 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
723 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
725 {
726 --no_wait_return;
727 msg_scroll = msg_save;
728 if (fd < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000729 emsg(_(e_readpre_autocommands_made_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000731 emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100732 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return FAIL;
734 }
735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100737 // Autocommands may add lines to the file, need to check if it is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
739
740 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
741 {
742 /*
743 * Show the user that we are busy reading the input. Sometimes this
744 * may take a while. When reading from stdin another program may
745 * still be running, don't move the cursor to the last line, unless
746 * always using the GUI.
747 */
748 if (read_stdin)
749 {
Bram Moolenaar234d1622017-11-18 14:55:23 +0100750 if (!is_not_a_term())
751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#ifndef ALWAYS_USE_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200753# ifdef VIMDLL
754 if (!gui.in_use)
755# endif
756 mch_msg(_("Vim: Reading from stdin...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#endif
758#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100759 // Also write a message in the GUI window, if there is one.
Bram Moolenaar234d1622017-11-18 14:55:23 +0100760 if (gui.in_use && !gui.dying && !gui.starting)
761 {
Amon Sha10197932022-02-21 15:07:12 +0000762 // make a copy, gui_write() may try to change it
763 p = vim_strsave((char_u *)_("Reading from stdin..."));
764 if (p != NULL)
765 {
766 gui_write(p, (int)STRLEN(p));
767 vim_free(p);
768 }
Bram Moolenaar234d1622017-11-18 14:55:23 +0100769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
Bram Moolenaar234d1622017-11-18 14:55:23 +0100771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 }
773 else if (!read_buffer)
774 filemess(curbuf, sfname, (char_u *)"", 0);
775 }
776
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100777 msg_scroll = FALSE; // overwrite the file message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 /*
780 * Set linecnt now, before the "retry" caused by a wrong guess for
781 * fileformat, and after the autocommands, which may change them.
782 */
783 linecnt = curbuf->b_ml.ml_line_count;
784
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // "++bad=" argument.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000786 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000787 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000788 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000789 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000790 curbuf->b_bad_char = eap->bad_char;
791 }
792 else
793 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000796 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 */
798 if (eap != NULL && eap->force_enc != 0)
799 {
800 fenc = enc_canonize(eap->cmd + eap->force_enc);
801 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000802 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
804 else if (curbuf->b_p_bin)
805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100806 fenc = (char_u *)""; // binary: don't convert
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 fenc_alloced = FALSE;
808 }
809 else if (curbuf->b_help)
810 {
811 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000812 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // Help files are either utf-8 or latin1. Try utf-8 first, if this
815 // fails it must be latin1.
816 // Always do this when 'encoding' is "utf-8". Otherwise only do
817 // this when needed to avoid [converted] remarks all the time.
818 // It is needed when the first line contains non-ASCII characters.
819 // That is only in *.??x files.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 fenc = (char_u *)"latin1";
821 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000822 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000824 fc = fname[STRLEN(fname) - 1];
825 if (TOLOWER_ASC(fc) == 'x')
826 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100827 // Read the first line (and a bit more). Immediately rewind to
828 // the start of the file. If the read() fails "len" is -1.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100829 len = read_eintr(fd, firstline, 80);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200830 vim_lseek(fd, (off_T)0L, SEEK_SET);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000831 for (p = firstline; p < firstline + len; ++p)
832 if (*p >= 0x80)
833 {
834 c = TRUE;
835 break;
836 }
837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 }
839
840 if (c)
841 {
842 fenc_next = fenc;
843 fenc = (char_u *)"utf-8";
844
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100845 // When the file is utf-8 but a character doesn't fit in
846 // 'encoding' don't retry. In help text editing utf-8 bytes
847 // doesn't make sense.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000848 if (!enc_utf8)
849 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 fenc_alloced = FALSE;
852 }
853 else if (*p_fencs == NUL)
854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100855 fenc = curbuf->b_p_fenc; // use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 fenc_alloced = FALSE;
857 }
858 else
859 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100860 fenc_next = p_fencs; // try items in 'fileencodings'
Bram Moolenaarf077db22019-08-13 00:18:24 +0200861 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 /*
865 * Jump back here to retry reading the file in different ways.
866 * Reasons to retry:
867 * - encoding conversion failed: try another one from "fenc_next"
868 * - BOM detected and fenc was set, need to setup conversion
869 * - "fileformat" check failed: try another
870 *
871 * Variables set for special retry actions:
872 * "file_rewind" Rewind the file to start reading it again.
873 * "advance_fenc" Advance "fenc" using "fenc_next".
874 * "skip_read" Re-use already read bytes (BOM detected).
875 * "did_iconv" iconv() conversion failed, try 'charconvert'.
876 * "keep_fileformat" Don't reset "fileformat".
877 *
878 * Other status indicators:
879 * "tmpname" When != NULL did conversion with 'charconvert'.
880 * Output file has to be deleted afterwards.
881 * "iconv_fd" When != -1 did conversion with iconv().
882 */
883retry:
884
885 if (file_rewind)
886 {
887 if (read_buffer)
888 {
889 read_buf_lnum = 1;
890 read_buf_col = 0;
891 }
Bram Moolenaar8767f522016-07-01 17:17:39 +0200892 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 // Can't rewind the file, give up.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 error = TRUE;
896 goto failed;
897 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 // Delete the previously read lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 while (lnum > from)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200900 ml_delete(lnum--);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 file_rewind = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000902 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000905 curbuf->b_start_bomb = FALSE;
906 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000907 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909
910 /*
911 * When retrying with another "fenc" and the first time "fileformat"
912 * will be reset.
913 */
914 if (keep_fileformat)
915 keep_fileformat = FALSE;
916 else
917 {
918 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +0000919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +0000921 try_unix = try_dos = try_mac = FALSE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 else if (curbuf->b_p_bin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100924 fileformat = EOL_UNIX; // binary: use Unix format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else if (*p_ffs == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100926 fileformat = get_fileformat(curbuf);// use format from buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100928 fileformat = EOL_UNKNOWN; // detect from file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 }
930
Bram Moolenaar13505972019-01-24 15:04:48 +0100931#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (iconv_fd != (iconv_t)-1)
933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100934 // aborted conversion with iconv(), close the descriptor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 iconv_close(iconv_fd);
936 iconv_fd = (iconv_t)-1;
937 }
Bram Moolenaar13505972019-01-24 15:04:48 +0100938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 if (advance_fenc)
941 {
942 /*
943 * Try the next entry in 'fileencodings'.
944 */
945 advance_fenc = FALSE;
946
947 if (eap != NULL && eap->force_enc != 0)
948 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100949 // Conversion given with "++cc=" wasn't possible, read
950 // without conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000952 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (fenc_alloced)
954 vim_free(fenc);
955 fenc = (char_u *)"";
956 fenc_alloced = FALSE;
957 }
958 else
959 {
960 if (fenc_alloced)
961 vim_free(fenc);
962 if (fenc_next != NULL)
963 {
Bram Moolenaarf077db22019-08-13 00:18:24 +0200964 fenc = next_fenc(&fenc_next, &fenc_alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 else
967 {
968 fenc = (char_u *)"";
969 fenc_alloced = FALSE;
970 }
971 }
972 if (tmpname != NULL)
973 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +0100975 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 }
978
979 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000980 * Conversion may be required when the encoding of the file is different
981 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 */
983 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000984 converted = need_conversion(fenc);
985 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
987
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100988 // "ucs-bom" means we need to check the first bytes of the file
989 // for a BOM.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (STRCMP(fenc, ENC_UCSBOM) == 0)
991 fio_flags = FIO_UCSBOM;
992
993 /*
994 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
995 * done. This is handled below after read(). Prepare the
996 * fio_flags to avoid having to parse the string each time.
997 * Also check for Unicode to Latin1 conversion, because iconv()
998 * appears not to handle this correctly. This works just like
999 * conversion to UTF-8 except how the resulting character is put in
1000 * the buffer.
1001 */
1002 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1003 fio_flags = get_fio_flags(fenc);
1004
Bram Moolenaar4f974752019-02-17 17:44:42 +01001005#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /*
1007 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1008 * is handled with MultiByteToWideChar().
1009 */
1010 if (fio_flags == 0)
1011 fio_flags = get_win_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar13505972019-01-24 15:04:48 +01001014#ifdef MACOS_CONVERT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001015 // Conversion from Apple MacRoman to latin1 or UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (fio_flags == 0)
1017 fio_flags = get_mac_fio_flags(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
Bram Moolenaar13505972019-01-24 15:04:48 +01001020#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 /*
1022 * Try using iconv() if we can't convert internally.
1023 */
1024 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001025# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && !did_iconv
Bram Moolenaar13505972019-01-24 15:04:48 +01001027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 )
1029 iconv_fd = (iconv_t)my_iconv_open(
1030 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01001031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaar13505972019-01-24 15:04:48 +01001033#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 /*
1035 * Use the 'charconvert' expression when conversion is required
1036 * and we can't do it internally or with iconv().
1037 */
1038 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001039 && !read_fifo
Bram Moolenaar13505972019-01-24 15:04:48 +01001040# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 )
1044 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001045# ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 did_iconv = FALSE;
Bram Moolenaar13505972019-01-24 15:04:48 +01001047# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001048 // Skip conversion when it's already done (retry for wrong
1049 // "fileformat").
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (tmpname == NULL)
1051 {
1052 tmpname = readfile_charconvert(fname, fenc, &fd);
1053 if (tmpname == NULL)
1054 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Conversion failed. Try another one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 advance_fenc = TRUE;
1057 if (fd < 0)
1058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 // Re-opening the original file failed!
Bram Moolenaar6d057012021-12-31 18:49:43 +00001060 emsg(_(e_conversion_mad_file_unreadable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 error = TRUE;
1062 goto failed;
1063 }
1064 goto retry;
1065 }
1066 }
1067 }
1068 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
1071 if (fio_flags == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001072#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 && iconv_fd == (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 )
1076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 // Conversion wanted but we can't.
1078 // Try the next conversion in 'fileencodings'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 advance_fenc = TRUE;
1080 goto retry;
1081 }
1082 }
1083 }
1084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 // Set "can_retry" when it's possible to rewind the file and try with
1086 // another "fenc" value. It's FALSE when no other "fenc" to try, reading
1087 // stdin or fixed at a specific encoding.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001088 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 if (!skip_read)
1091 {
1092 linerest = 0;
1093 filesize = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001094 filesize_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 skip_count = lines_to_skip;
1096 read_count = lines_to_read;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 conv_restlen = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001098#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001099 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1100 && curbuf->b_ffname != NULL
1101 && curbuf->b_p_udf
1102 && !filtering
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02001103 && !read_fifo
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001104 && !read_stdin
1105 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001106 if (read_undo_file)
1107 sha256_start(&sha_ctx);
1108#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001109#ifdef FEAT_CRYPT
1110 if (curbuf->b_cryptstate != NULL)
1111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001112 // Need to free the state, but keep the key, don't want to ask for
1113 // it again.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001114 crypt_free_state(curbuf->b_cryptstate);
1115 curbuf->b_cryptstate = NULL;
1116 }
1117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
1119
1120 while (!error && !got_int)
1121 {
1122 /*
1123 * We allocate as much space for the file as we can get, plus
1124 * space for the old line plus room for one terminating NUL.
1125 * The amount is limited by the fact that read() only can read
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001126 * up to max_unsigned characters (and other things).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 */
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001128 if (!skip_read)
1129 {
Bram Moolenaar30276f22019-01-24 17:59:39 +01001130#if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001131 size = SSIZE_MAX; // use max I/O size, 52K
Bram Moolenaar30276f22019-01-24 17:59:39 +01001132#else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001133 // Use buffer >= 64K. Add linerest to double the size if the
1134 // line gets very long, to avoid a lot of copying. But don't
1135 // read more than 1 Mbyte at a time, so we can be interrupted.
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001136 size = 0x10000L + linerest;
1137 if (size > 0x100000L)
1138 size = 0x100000L;
Bram Moolenaar13d3b052018-04-29 13:34:47 +02001139#endif
1140 }
1141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142 // Protect against the argument of lalloc() going negative.
Bram Moolenaar30276f22019-01-24 17:59:39 +01001143 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 ++split;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146 *ptr = NL; // split line by inserting a NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 size = 1;
1148 }
1149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 if (!skip_read)
1152 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001153 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001155 if ((new_buffer = lalloc(size + linerest + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 FALSE)) != NULL)
1157 break;
1158 }
1159 if (new_buffer == NULL)
1160 {
1161 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1162 error = TRUE;
1163 break;
1164 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001165 if (linerest) // copy characters from the previous buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1167 vim_free(buffer);
1168 buffer = new_buffer;
1169 ptr = buffer + linerest;
1170 line_start = buffer;
1171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001172 // May need room to translate into.
1173 // For iconv() we don't really know the required space, use a
1174 // factor ICONV_MULT.
1175 // latin1 to utf-8: 1 byte becomes up to 2 bytes
1176 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1177 // become up to 4 bytes, size must be multiple of 2
1178 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1179 // multiple of 2
1180 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1181 // multiple of 4
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001182 real_size = (int)size;
Bram Moolenaar13505972019-01-24 15:04:48 +01001183#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (iconv_fd != (iconv_t)-1)
1185 size = size / ICONV_MULT;
1186 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (fio_flags & FIO_LATIN1)
1189 size = size / 2;
1190 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1191 size = (size * 2 / 3) & ~1;
1192 else if (fio_flags & FIO_UCS4)
1193 size = (size * 2 / 3) & ~3;
1194 else if (fio_flags == FIO_UCSBOM)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195 size = size / ICONV_MULT; // worst case
Bram Moolenaar4f974752019-02-17 17:44:42 +01001196#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else if (fio_flags & FIO_CODEPAGE)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001198 size = size / ICONV_MULT; // also worst case
Bram Moolenaar13505972019-01-24 15:04:48 +01001199#endif
1200#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else if (fio_flags & FIO_MACROMAN)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001202 size = size / ICONV_MULT; // also worst case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (conv_restlen > 0)
1206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001207 // Insert unconverted bytes from previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 mch_memmove(ptr, conv_rest, conv_restlen);
1209 ptr += conv_restlen;
1210 size -= conv_restlen;
1211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
1213 if (read_buffer)
1214 {
1215 /*
1216 * Read bytes from curbuf. Used for converting text read
1217 * from stdin.
1218 */
Christian Brabandt226b28b2021-06-21 21:08:08 +02001219 eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (read_buf_lnum > from)
1221 size = 0;
1222 else
1223 {
1224 int n, ni;
1225 long tlen;
1226
1227 tlen = 0;
1228 for (;;)
1229 {
1230 p = ml_get(read_buf_lnum) + read_buf_col;
1231 n = (int)STRLEN(p);
1232 if ((int)tlen + n + 1 > size)
1233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001234 // Filled up to "size", append partial line.
1235 // Change NL to NUL to reverse the effect done
1236 // below.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001237 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 for (ni = 0; ni < n; ++ni)
1239 {
1240 if (p[ni] == NL)
1241 ptr[tlen++] = NUL;
1242 else
1243 ptr[tlen++] = p[ni];
1244 }
1245 read_buf_col += n;
1246 break;
1247 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001248
1249 // Append whole line and new-line. Change NL
1250 // to NUL to reverse the effect done below.
1251 for (ni = 0; ni < n; ++ni)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001253 if (p[ni] == NL)
1254 ptr[tlen++] = NUL;
1255 else
1256 ptr[tlen++] = p[ni];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01001258 ptr[tlen++] = NL;
1259 read_buf_col = 0;
1260 if (++read_buf_lnum > from)
1261 {
1262 // When the last line didn't have an
1263 // end-of-line don't add it now either.
1264 if (!curbuf->b_p_eol)
1265 --tlen;
1266 size = tlen;
1267 eof = TRUE;
1268 break;
1269 }
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 }
1273 }
1274 else
1275 {
1276 /*
1277 * Read bytes from the file.
1278 */
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001279# ifdef FEAT_SODIUM
1280 // Let the crypt layer work with a buffer size of 8192
1281 if (filesize == 0)
1282 // set size to 8K + Sodium Crypt Metadata
Christian Brabandt226b28b2021-06-21 21:08:08 +02001283 size = WRITEBUFSIZE + crypt_get_max_header_len()
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001284 + crypto_secretstream_xchacha20poly1305_HEADERBYTES
1285 + crypto_secretstream_xchacha20poly1305_ABYTES;
1286
1287 else if (filesize > 0 && (curbuf->b_cryptstate != NULL &&
1288 curbuf->b_cryptstate->method_nr == CRYPT_M_SOD))
1289 size = WRITEBUFSIZE + crypto_secretstream_xchacha20poly1305_ABYTES;
1290# endif
1291 eof = size;
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001292 size = read_eintr(fd, ptr, size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001293 filesize_count += size;
1294 // hit end of file
1295 eof = (size < eof || filesize_count == filesize_disk);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001298#ifdef FEAT_CRYPT
1299 /*
1300 * At start of file: Check for magic number of encryption.
1301 */
1302 if (filesize == 0 && size > 0)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001303 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001304 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1305 &filesize, newfile, sfname,
1306 &did_ask_for_key);
Bram Moolenaarb4868ed2022-01-19 11:24:40 +00001307# if defined(CRYPT_NOT_INPLACE) && defined(FEAT_PERSISTENT_UNDO)
Bram Moolenaar65aee0b2021-06-27 14:08:24 +02001308 if (curbuf->b_cryptstate != NULL
1309 && !crypt_works_inplace(curbuf->b_cryptstate))
1310 // reading undo file requires crypt_decode_inplace()
1311 read_undo_file = FALSE;
1312# endif
1313 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001314 /*
1315 * Decrypt the read bytes. This is done before checking for
1316 * EOF because the crypt layer may be buffering.
1317 */
Bram Moolenaar829aa642017-08-23 22:32:35 +02001318 if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1319 && size > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001320 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001321# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001322 if (crypt_works_inplace(curbuf->b_cryptstate))
1323 {
Bram Moolenaar987411d2019-01-18 22:48:34 +01001324# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001325 crypt_decode_inplace(curbuf->b_cryptstate, ptr,
1326 size, eof);
Bram Moolenaar987411d2019-01-18 22:48:34 +01001327# ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001328 }
1329 else
1330 {
1331 char_u *newptr = NULL;
1332 int decrypted_size;
1333
1334 decrypted_size = crypt_decode_alloc(
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001335 curbuf->b_cryptstate, ptr, size,
1336 &newptr, eof);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001337
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001338 if (decrypted_size < 0)
1339 {
1340 // error message already given
1341 error = TRUE;
1342 vim_free(newptr);
1343 break;
1344 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001345 // If the crypt layer is buffering, not producing
1346 // anything yet, need to read more.
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02001347 if (decrypted_size == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001348 continue;
1349
1350 if (linerest == 0)
1351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001352 // Simple case: reuse returned buffer (may be
1353 // NULL, checked later).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001354 new_buffer = newptr;
1355 }
1356 else
1357 {
1358 long_u new_size;
1359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001360 // Need new buffer to add bytes carried over.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001361 new_size = (long_u)(decrypted_size + linerest + 1);
1362 new_buffer = lalloc(new_size, FALSE);
1363 if (new_buffer == NULL)
1364 {
1365 do_outofmem_msg(new_size);
1366 error = TRUE;
1367 break;
1368 }
1369
1370 mch_memmove(new_buffer, buffer, linerest);
1371 if (newptr != NULL)
1372 mch_memmove(new_buffer + linerest, newptr,
1373 decrypted_size);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001374 vim_free(newptr);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001375 }
1376
1377 if (new_buffer != NULL)
1378 {
1379 vim_free(buffer);
1380 buffer = new_buffer;
1381 new_buffer = NULL;
1382 line_start = buffer;
1383 ptr = buffer + linerest;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001384 real_size = size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001385 }
1386 size = decrypted_size;
1387 }
Bram Moolenaar987411d2019-01-18 22:48:34 +01001388# endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001389 }
1390#endif
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (size <= 0)
1393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001394 if (size < 0) // read error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001397 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001398 /*
1399 * Reached end-of-file but some trailing bytes could
1400 * not be converted. Truncated file?
1401 */
1402
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001403 // When we did a conversion report an error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001404 if (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001405#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001406 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001407#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001408 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001409 {
Bram Moolenaare8d95302013-04-24 16:34:02 +02001410 if (can_retry)
1411 goto rewind_retry;
Bram Moolenaarf453d352008-06-04 17:37:34 +00001412 if (conv_error == 0)
1413 conv_error = curbuf->b_ml.ml_line_count
1414 - linecnt + 1;
1415 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001416 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00001417 else if (illegal_byte == 0)
1418 illegal_byte = curbuf->b_ml.ml_line_count
1419 - linecnt + 1;
1420 if (bad_char_behavior == BAD_DROP)
1421 {
1422 *(ptr - conv_restlen) = NUL;
1423 conv_restlen = 0;
1424 }
1425 else
1426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001427 // Replace the trailing bytes with the replacement
1428 // character if we were converting; if we weren't,
1429 // leave the UTF8 checking code to do it, as it
1430 // works slightly differently.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001431 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001432#ifdef USE_ICONV
Bram Moolenaarf453d352008-06-04 17:37:34 +00001433 || iconv_fd != (iconv_t)-1
Bram Moolenaar13505972019-01-24 15:04:48 +01001434#endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00001435 ))
1436 {
1437 while (conv_restlen > 0)
1438 {
1439 *(--ptr) = bad_char_behavior;
1440 --conv_restlen;
1441 }
1442 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001443 fio_flags = 0; // don't convert this
Bram Moolenaar13505972019-01-24 15:04:48 +01001444#ifdef USE_ICONV
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001445 if (iconv_fd != (iconv_t)-1)
1446 {
1447 iconv_close(iconv_fd);
1448 iconv_fd = (iconv_t)-1;
1449 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001450#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001451 }
1452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455 skip_read = FALSE;
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 /*
1458 * At start of file (or after crypt magic number): Check for BOM.
1459 * Also check for a BOM for other Unicode encodings, but not after
1460 * converting with 'charconvert' or when a BOM has already been
1461 * found.
1462 */
1463 if ((filesize == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01001464#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001465 || (cryptkey != NULL
1466 && filesize == crypt_get_header_len(
1467 crypt_get_method_nr(curbuf)))
Bram Moolenaar13505972019-01-24 15:04:48 +01001468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 )
1470 && (fio_flags == FIO_UCSBOM
1471 || (!curbuf->b_p_bomb
1472 && tmpname == NULL
1473 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1474 {
1475 char_u *ccname;
1476 int blen;
1477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001478 // no BOM detection in a short file or in binary mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (size < 2 || curbuf->b_p_bin)
1480 ccname = NULL;
1481 else
1482 ccname = check_for_bom(ptr, size, &blen,
1483 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1484 if (ccname != NULL)
1485 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 // Remove BOM from the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 filesize += blen;
1488 size -= blen;
1489 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001490 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001493 curbuf->b_start_bomb = TRUE;
1494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496
1497 if (fio_flags == FIO_UCSBOM)
1498 {
1499 if (ccname == NULL)
1500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001501 // No BOM detected: retry with next encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 advance_fenc = TRUE;
1503 }
1504 else
1505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001506 // BOM detected: set "fenc" and jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (fenc_alloced)
1508 vim_free(fenc);
1509 fenc = ccname;
1510 fenc_alloced = FALSE;
1511 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 // retry reading without getting new bytes or rewinding
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 skip_read = TRUE;
1514 goto retry;
1515 }
1516 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 // Include not converted bytes.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001519 ptr -= conv_restlen;
1520 size += conv_restlen;
1521 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /*
1523 * Break here for a read error or end-of-file.
1524 */
1525 if (size <= 0)
1526 break;
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar13505972019-01-24 15:04:48 +01001529#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (iconv_fd != (iconv_t)-1)
1531 {
1532 /*
1533 * Attempt conversion of the read bytes to 'encoding' using
1534 * iconv().
1535 */
1536 const char *fromp;
1537 char *top;
1538 size_t from_size;
1539 size_t to_size;
1540
1541 fromp = (char *)ptr;
1542 from_size = size;
1543 ptr += size;
1544 top = (char *)ptr;
1545 to_size = real_size - size;
1546
1547 /*
1548 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001549 * another conversion. Except for when there is no
1550 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001552 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1553 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1555 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001556 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001557 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001558 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001559 if (conv_error == 0)
1560 conv_error = readfile_linenr(linecnt,
1561 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001563 // Deal with a bad byte and continue with the next.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001564 ++fromp;
1565 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001566 if (bad_char_behavior == BAD_KEEP)
1567 {
1568 *top++ = *(fromp - 1);
1569 --to_size;
1570 }
1571 else if (bad_char_behavior != BAD_DROP)
1572 {
1573 *top++ = bad_char_behavior;
1574 --to_size;
1575 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 if (from_size > 0)
1579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001580 // Some remaining characters, keep them for the next
1581 // round.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1583 conv_restlen = (int)from_size;
1584 }
1585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001586 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 line_start = ptr - linerest;
1588 mch_memmove(line_start, buffer, (size_t)linerest);
1589 size = (long)((char_u *)top - ptr);
1590 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
Bram Moolenaar4f974752019-02-17 17:44:42 +01001593#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (fio_flags & FIO_CODEPAGE)
1595 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001596 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001597 WCHAR ucs2buf[3];
1598 int ucs2len;
1599 int codepage = FIO_GET_CP(fio_flags);
1600 int bytelen;
1601 int found_bad;
1602 char replstr[2];
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 /*
1605 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001606 * a codepage, using standard MS-Windows functions. This
1607 * requires two steps:
1608 * 1. convert from 'fileencoding' to ucs-2
1609 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001611 * Because there may be illegal bytes AND an incomplete byte
1612 * sequence at the end, we may have to do the conversion one
1613 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001616 // Replacement string for WideCharToMultiByte().
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001617 if (bad_char_behavior > 0)
1618 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001620 replstr[0] = '?';
1621 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001624 * Move the bytes to the end of the buffer, so that we have
1625 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001627 src = ptr + real_size - size;
1628 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001630 /*
1631 * Do the conversion.
1632 */
1633 dst = ptr;
1634 size = size;
1635 while (size > 0)
1636 {
1637 found_bad = FALSE;
1638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001640 if (codepage == CP_UTF8)
1641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001642 // Handle CP_UTF8 input ourselves to be able to handle
1643 // trailing bytes properly.
1644 // Get one UTF-8 character from src.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001645 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001646 if (bytelen > size)
1647 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001648 // Only got some bytes of a character. Normally
1649 // it's put in "conv_rest", but if it's too long
1650 // deal with it as if they were illegal bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001651 if (bytelen <= CONV_RESTLEN)
1652 break;
1653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001654 // weird overlong byte sequence
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001655 bytelen = size;
1656 found_bad = TRUE;
1657 }
1658 else
1659 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001660 int u8c = utf_ptr2char(src);
1661
Bram Moolenaar86e01082005-12-29 22:45:34 +00001662 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001663 found_bad = TRUE;
1664 ucs2buf[0] = u8c;
1665 ucs2len = 1;
1666 }
1667 }
1668 else
1669# endif
1670 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001671 // We don't know how long the byte sequence is, try
1672 // from one to three bytes.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001673 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1674 ++bytelen)
1675 {
1676 ucs2len = MultiByteToWideChar(codepage,
1677 MB_ERR_INVALID_CHARS,
1678 (LPCSTR)src, bytelen,
1679 ucs2buf, 3);
1680 if (ucs2len > 0)
1681 break;
1682 }
1683 if (ucs2len == 0)
1684 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001685 // If we have only one byte then it's probably an
1686 // incomplete byte sequence. Otherwise discard
1687 // one byte as a bad character.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001688 if (size == 1)
1689 break;
1690 found_bad = TRUE;
1691 bytelen = 1;
1692 }
1693 }
1694
1695 if (!found_bad)
1696 {
1697 int i;
1698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001699 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst".
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001700 if (enc_utf8)
1701 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001702 // From UCS-2 to UTF-8. Cannot fail.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001703 for (i = 0; i < ucs2len; ++i)
1704 dst += utf_char2bytes(ucs2buf[i], dst);
1705 }
1706 else
1707 {
1708 BOOL bad = FALSE;
1709 int dstlen;
1710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001711 // From UCS-2 to "enc_codepage". If the
1712 // conversion uses the default character "?",
1713 // the data doesn't fit in this encoding.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001714 dstlen = WideCharToMultiByte(enc_codepage, 0,
1715 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001716 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001717 replstr, &bad);
1718 if (bad)
1719 found_bad = TRUE;
1720 else
1721 dst += dstlen;
1722 }
1723 }
1724
1725 if (found_bad)
1726 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001727 // Deal with bytes we can't convert.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001728 if (can_retry)
1729 goto rewind_retry;
1730 if (conv_error == 0)
1731 conv_error = readfile_linenr(linecnt, ptr, dst);
1732 if (bad_char_behavior != BAD_DROP)
1733 {
1734 if (bad_char_behavior == BAD_KEEP)
1735 {
1736 mch_memmove(dst, src, bytelen);
1737 dst += bytelen;
1738 }
1739 else
1740 *dst++ = bad_char_behavior;
1741 }
1742 }
1743
1744 src += bytelen;
1745 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001747
1748 if (size > 0)
1749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001750 // An incomplete byte sequence remaining.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001751 mch_memmove(conv_rest, src, size);
1752 conv_restlen = size;
1753 }
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // The new size is equal to how much "dst" was advanced.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001756 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001759#endif
1760#ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (fio_flags & FIO_MACROMAN)
1762 {
1763 /*
1764 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001765 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001767 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 if (fio_flags != 0)
1773 {
1774 int u8c;
1775 char_u *dest;
1776 char_u *tail = NULL;
1777
1778 /*
1779 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1780 * "enc_utf8" not set: Convert Unicode to Latin1.
1781 * Go from end to start through the buffer, because the number
1782 * of bytes may increase.
1783 * "dest" points to after where the UTF-8 bytes go, "p" points
1784 * to after the next character to convert.
1785 */
1786 dest = ptr + real_size;
1787 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1788 {
1789 p = ptr + size;
1790 if (fio_flags == FIO_UTF8)
1791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001792 // Check for a trailing incomplete UTF-8 sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 tail = ptr + size - 1;
1794 while (tail > ptr && (*tail & 0xc0) == 0x80)
1795 --tail;
1796 if (tail + utf_byte2len(*tail) <= ptr + size)
1797 tail = NULL;
1798 else
1799 p = tail;
1800 }
1801 }
1802 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001804 // Check for a trailing byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 p = ptr + (size & ~1);
1806 if (size & 1)
1807 tail = p;
1808 if ((fio_flags & FIO_UTF16) && p > ptr)
1809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001810 // Check for a trailing leading word
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (fio_flags & FIO_ENDIAN_L)
1812 {
1813 u8c = (*--p << 8);
1814 u8c += *--p;
1815 }
1816 else
1817 {
1818 u8c = *--p;
1819 u8c += (*--p << 8);
1820 }
1821 if (u8c >= 0xd800 && u8c <= 0xdbff)
1822 tail = p;
1823 else
1824 p += 2;
1825 }
1826 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001827 else // FIO_UCS4
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001829 // Check for trailing 1, 2 or 3 bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 p = ptr + (size & ~3);
1831 if (size & 3)
1832 tail = p;
1833 }
1834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001835 // If there is a trailing incomplete sequence move it to
1836 // conv_rest[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (tail != NULL)
1838 {
1839 conv_restlen = (int)((ptr + size) - tail);
1840 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1841 size -= conv_restlen;
1842 }
1843
1844
1845 while (p > ptr)
1846 {
1847 if (fio_flags & FIO_LATIN1)
1848 u8c = *--p;
1849 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1850 {
1851 if (fio_flags & FIO_ENDIAN_L)
1852 {
1853 u8c = (*--p << 8);
1854 u8c += *--p;
1855 }
1856 else
1857 {
1858 u8c = *--p;
1859 u8c += (*--p << 8);
1860 }
1861 if ((fio_flags & FIO_UTF16)
1862 && u8c >= 0xdc00 && u8c <= 0xdfff)
1863 {
1864 int u16c;
1865
1866 if (p == ptr)
1867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 // Missing leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (can_retry)
1870 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001871 if (conv_error == 0)
1872 conv_error = readfile_linenr(linecnt,
1873 ptr, p);
1874 if (bad_char_behavior == BAD_DROP)
1875 continue;
1876 if (bad_char_behavior != BAD_KEEP)
1877 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001880 // found second word of double-word, get the first
1881 // word and compute the resulting character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (fio_flags & FIO_ENDIAN_L)
1883 {
1884 u16c = (*--p << 8);
1885 u16c += *--p;
1886 }
1887 else
1888 {
1889 u16c = *--p;
1890 u16c += (*--p << 8);
1891 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001892 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1893 + (u8c & 0x3ff);
1894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001895 // Check if the word is indeed a leading word.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (u16c < 0xd800 || u16c > 0xdbff)
1897 {
1898 if (can_retry)
1899 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001900 if (conv_error == 0)
1901 conv_error = readfile_linenr(linecnt,
1902 ptr, p);
1903 if (bad_char_behavior == BAD_DROP)
1904 continue;
1905 if (bad_char_behavior != BAD_KEEP)
1906 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 }
1910 else if (fio_flags & FIO_UCS4)
1911 {
1912 if (fio_flags & FIO_ENDIAN_L)
1913 {
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001914 u8c = (unsigned)*--p << 24;
1915 u8c += (unsigned)*--p << 16;
1916 u8c += (unsigned)*--p << 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 u8c += *--p;
1918 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001919 else // big endian
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921 u8c = *--p;
Bram Moolenaardc1c9812017-10-27 22:15:24 +02001922 u8c += (unsigned)*--p << 8;
1923 u8c += (unsigned)*--p << 16;
1924 u8c += (unsigned)*--p << 24;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001927 else // UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 if (*--p < 0x80)
1930 u8c = *p;
1931 else
1932 {
1933 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001934 p -= len;
1935 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (len == 0)
1937 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001938 // Not a valid UTF-8 character, retry with
1939 // another fenc when possible, otherwise just
1940 // report the error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (can_retry)
1942 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001943 if (conv_error == 0)
1944 conv_error = readfile_linenr(linecnt,
1945 ptr, p);
1946 if (bad_char_behavior == BAD_DROP)
1947 continue;
1948 if (bad_char_behavior != BAD_KEEP)
1949 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
1952 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 if (enc_utf8) // produce UTF-8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 dest -= utf_char2len(u8c);
1956 (void)utf_char2bytes(u8c, dest);
1957 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001958 else // produce Latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 --dest;
1961 if (u8c >= 0x100)
1962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 // character doesn't fit in latin1, retry with
1964 // another fenc when possible, otherwise just
1965 // report the error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001966 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001968 if (conv_error == 0)
1969 conv_error = readfile_linenr(linecnt, ptr, p);
1970 if (bad_char_behavior == BAD_DROP)
1971 ++dest;
1972 else if (bad_char_behavior == BAD_KEEP)
1973 *dest = u8c;
1974 else if (eap != NULL && eap->bad_char != 0)
1975 *dest = bad_char_behavior;
1976 else
1977 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 else
1980 *dest = u8c;
1981 }
1982 }
1983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001984 // move the linerest to before the converted characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 line_start = dest - linerest;
1986 mch_memmove(line_start, buffer, (size_t)linerest);
1987 size = (long)((ptr + real_size) - dest);
1988 ptr = dest;
1989 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001990 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001992 int incomplete_tail = FALSE;
1993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // Reading UTF-8: Check if the bytes are valid UTF-8.
Bram Moolenaarf453d352008-06-04 17:37:34 +00001995 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001997 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001998 int l;
1999
2000 if (todo <= 0)
2001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (*p >= 0x80)
2003 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002004 // A length of 1 means it's an illegal byte. Accept
2005 // an incomplete character at the end though, the next
2006 // read() will get the next bytes, we'll check it
2007 // then.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002008 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002009 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 // Avoid retrying with a different encoding when
2012 // a truncated file is more likely, or attempting
2013 // to read the rest of an incomplete sequence when
2014 // we have already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002015 if (p > ptr || filesize > 0)
2016 incomplete_tail = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 // Incomplete byte sequence, move it to conv_rest[]
2018 // and try to read the rest of it, unless we've
2019 // already done so.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002020 if (p > ptr)
2021 {
2022 conv_restlen = todo;
2023 mch_memmove(conv_rest, p, conv_restlen);
2024 size -= conv_restlen;
2025 break;
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002028 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002029 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // Illegal byte. If we can try another encoding
2031 // do that, unless at EOF where a truncated
2032 // file is more likely than a conversion error.
Bram Moolenaarf453d352008-06-04 17:37:34 +00002033 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002034 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01002035#ifdef USE_ICONV
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002036 // When we did a conversion report an error.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002037 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2038 conv_error = readfile_linenr(linecnt, ptr, p);
Bram Moolenaar13505972019-01-24 15:04:48 +01002039#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002040 // Remember the first linenr with an illegal byte
Bram Moolenaarf453d352008-06-04 17:37:34 +00002041 if (conv_error == 0 && illegal_byte == 0)
2042 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002043
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002044 // Drop, keep or replace the bad byte.
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002045 if (bad_char_behavior == BAD_DROP)
2046 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002047 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002048 --p;
2049 --size;
2050 }
2051 else if (bad_char_behavior != BAD_KEEP)
2052 *p = bad_char_behavior;
2053 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002054 else
2055 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002058 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002060 // Detected a UTF-8 error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061rewind_retry:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002062 // Retry reading with another conversion.
Bram Moolenaar13505972019-01-24 15:04:48 +01002063#if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002064 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // iconv() failed, try 'charconvert'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002066 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002068#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // use next item from 'fileencodings'
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002070 advance_fenc = TRUE;
2071 file_rewind = TRUE;
2072 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002076 // count the number of characters (after conversion!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 filesize += size;
2078
2079 /*
2080 * when reading the first part of a file: guess EOL type
2081 */
2082 if (fileformat == EOL_UNKNOWN)
2083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // First try finding a NL, for Dos and Unix
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (try_dos || try_unix)
2086 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002087 // Reset the carriage return counter.
Bram Moolenaarc6b72172015-02-27 17:48:09 +01002088 if (try_mac)
2089 try_mac = 1;
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 for (p = ptr; p < ptr + size; ++p)
2092 {
2093 if (*p == NL)
2094 {
2095 if (!try_unix
2096 || (try_dos && p > ptr && p[-1] == CAR))
2097 fileformat = EOL_DOS;
2098 else
2099 fileformat = EOL_UNIX;
2100 break;
2101 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002102 else if (*p == CAR && try_mac)
2103 try_mac++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002106 // Don't give in to EOL_UNIX if EOL_MAC is more likely
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (fileformat == EOL_UNIX && try_mac)
2108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // Need to reset the counters when retrying fenc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 try_mac = 1;
2111 try_unix = 1;
2112 for (; p >= ptr && *p != CAR; p--)
2113 ;
2114 if (p >= ptr)
2115 {
2116 for (p = ptr; p < ptr + size; ++p)
2117 {
2118 if (*p == NL)
2119 try_unix++;
2120 else if (*p == CAR)
2121 try_mac++;
2122 }
2123 if (try_mac > try_unix)
2124 fileformat = EOL_MAC;
2125 }
2126 }
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002127 else if (fileformat == EOL_UNKNOWN && try_mac == 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 // Looking for CR but found no end-of-line markers at
2129 // all: use the default format.
Bram Moolenaar05eb6122015-02-17 14:15:19 +01002130 fileformat = default_fileformat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 // No NL found: may use Mac format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (fileformat == EOL_UNKNOWN && try_mac)
2135 fileformat = EOL_MAC;
2136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 // Still nothing found? Use first format in 'ffs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (fileformat == EOL_UNKNOWN)
2139 fileformat = default_fileformat();
2140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002141 // if editing a new file: may set p_tx and p_ff
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 set_fileformat(fileformat, OPT_LOCAL);
2144 }
2145 }
2146
2147 /*
2148 * This loop is executed once for every character read.
2149 * Keep it fast!
2150 */
2151 if (fileformat == EOL_MAC)
2152 {
2153 --ptr;
2154 while (++ptr, --size >= 0)
2155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // catch most common case first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if ((c = *ptr) != NUL && c != CAR && c != NL)
2158 continue;
2159 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 else if (c == NL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 *ptr = CAR; // NLs are replaced by CRs!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 else
2164 {
2165 if (skip_count == 0)
2166 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002167 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 len = (colnr_T) (ptr - line_start + 1);
2169 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2170 {
2171 error = TRUE;
2172 break;
2173 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002174#ifdef FEAT_PERSISTENT_UNDO
2175 if (read_undo_file)
2176 sha256_update(&sha_ctx, line_start, len);
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ++lnum;
2179 if (--read_count == 0)
2180 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002181 error = TRUE; // break loop
2182 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 break;
2184 }
2185 }
2186 else
2187 --skip_count;
2188 line_start = ptr + 1;
2189 }
2190 }
2191 }
2192 else
2193 {
2194 --ptr;
2195 while (++ptr, --size >= 0)
2196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002197 if ((c = *ptr) != NUL && c != NL) // catch most common case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 continue;
2199 if (c == NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 *ptr = NL; // NULs are replaced by newlines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 else
2202 {
2203 if (skip_count == 0)
2204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002205 *ptr = NUL; // end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 len = (colnr_T)(ptr - line_start + 1);
2207 if (fileformat == EOL_DOS)
2208 {
Bram Moolenaar2aa5f692017-01-24 15:46:48 +01002209 if (ptr > line_start && ptr[-1] == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002211 // remove CR before NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ptr[-1] = NUL;
2213 --len;
2214 }
2215 /*
2216 * Reading in Dos format, but no CR-LF found!
2217 * When 'fileformats' includes "unix", delete all
2218 * the lines read so far and start all over again.
2219 * Otherwise give an error message later.
2220 */
2221 else if (ff_error != EOL_DOS)
2222 {
2223 if ( try_unix
2224 && !read_stdin
2225 && (read_buffer
Bram Moolenaar8767f522016-07-01 17:17:39 +02002226 || vim_lseek(fd, (off_T)0L, SEEK_SET)
2227 == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002230 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 set_fileformat(EOL_UNIX, OPT_LOCAL);
2232 file_rewind = TRUE;
2233 keep_fileformat = TRUE;
2234 goto retry;
2235 }
2236 ff_error = EOL_DOS;
2237 }
2238 }
2239 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2240 {
2241 error = TRUE;
2242 break;
2243 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002244#ifdef FEAT_PERSISTENT_UNDO
2245 if (read_undo_file)
2246 sha256_update(&sha_ctx, line_start, len);
2247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 ++lnum;
2249 if (--read_count == 0)
2250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 error = TRUE; // break loop
2252 line_start = ptr; // nothing left to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 break;
2254 }
2255 }
2256 else
2257 --skip_count;
2258 line_start = ptr + 1;
2259 }
2260 }
2261 }
2262 linerest = (long)(ptr - line_start);
2263 ui_breakcheck();
2264 }
2265
2266failed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 // not an error, max. number of lines reached
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (error && read_count == 0)
2269 error = FALSE;
2270
2271 /*
2272 * If we get EOF in the middle of a line, note the fact and
2273 * complete the line ourselves.
2274 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2275 */
2276 if (!error
2277 && !got_int
2278 && linerest != 0
2279 && !(!curbuf->b_p_bin
2280 && fileformat == EOL_DOS
2281 && *line_start == Ctrl_Z
2282 && ptr == line_start + 1))
2283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 // remember for when writing
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 curbuf->b_p_eol = FALSE;
2287 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002288 len = (colnr_T)(ptr - line_start + 1);
2289 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 error = TRUE;
2291 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002292 {
2293#ifdef FEAT_PERSISTENT_UNDO
2294 if (read_undo_file)
2295 sha256_update(&sha_ctx, line_start, len);
2296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301 if (set_options)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002302 save_file_ff(curbuf); // remember the current file format
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002305 if (curbuf->b_cryptstate != NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002306 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002307 crypt_free_state(curbuf->b_cryptstate);
2308 curbuf->b_cryptstate = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002309 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002310 if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2311 crypt_free_key(cryptkey);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // Don't set cryptkey to NULL, it's used below as a flag that
2313 // encryption was used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // If editing a new file: set 'fenc' for the current buffer.
2317 // Also for ":read ++edit file".
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002318 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002320 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (fenc_alloced)
2322 vim_free(fenc);
Bram Moolenaar13505972019-01-24 15:04:48 +01002323#ifdef USE_ICONV
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (iconv_fd != (iconv_t)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 iconv_close(iconv_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#endif
2327
2328 if (!read_buffer && !read_stdin)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 close(fd); // errors are ignored
Bram Moolenaarf05da212009-11-17 16:13:15 +00002330#ifdef HAVE_FD_CLOEXEC
2331 else
2332 {
2333 int fdflags = fcntl(fd, F_GETFD);
Bram Moolenaara7251492021-01-02 16:53:13 +01002334
Bram Moolenaarf05da212009-11-17 16:13:15 +00002335 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002336 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +00002337 }
2338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 vim_free(buffer);
2340
2341#ifdef HAVE_DUP
2342 if (read_stdin)
2343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002344 // Use stderr for stdin, makes shell commands work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002346 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348#endif
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (tmpname != NULL)
2351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002352 mch_remove(tmpname); // delete converted file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 vim_free(tmpname);
2354 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 --no_wait_return; // may wait for return now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 /*
2358 * In recovery mode everything but autocommands is skipped.
2359 */
2360 if (!recoverymode)
2361 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // need to delete the last line, which comes from the empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2364 {
2365#ifdef FEAT_NETBEANS_INTG
2366 netbeansFireChanges = 0;
2367#endif
Bram Moolenaarca70c072020-05-30 20:30:46 +02002368 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369#ifdef FEAT_NETBEANS_INTG
2370 netbeansFireChanges = 1;
2371#endif
2372 --linecnt;
2373 }
2374 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2375 if (filesize == 0)
2376 linecnt = 0;
2377 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002380#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 // After reading the text into the buffer the diff info needs to
2382 // be updated.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002383 diff_invalidate(curbuf);
2384#endif
2385#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002386 // All folds in the window are invalid now. Mark them for update
2387 // before triggering autocommands.
Bram Moolenaar7263a772007-05-10 17:35:54 +00002388 foldUpdateAll(curwin);
2389#endif
2390 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002391 else if (linecnt) // appended at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 appended_lines_mark(from, linecnt);
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#ifndef ALWAYS_USE_GUI
2395 /*
2396 * If we were reading from the same terminal as where messages go,
2397 * the screen will have been messed up.
2398 * Switch on raw mode now and clear the screen.
2399 */
2400 if (read_stdin)
2401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002402 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 starttermcap();
2404 screenclear();
2405 }
2406#endif
2407
2408 if (got_int)
2409 {
2410 if (!(flags & READ_DUMMY))
2411 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002412 filemess(curbuf, sfname, (char_u *)_(e_interrupted), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (newfile)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002414 curbuf->b_p_ro = TRUE; // must use "w!" now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 msg_scroll = msg_save;
2417#ifdef FEAT_VIMINFO
2418 check_marks_read();
2419#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002420 return OK; // an interrupt isn't really an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422
2423 if (!filtering && !(flags & READ_DUMMY))
2424 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002425 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 c = FALSE;
2427
2428#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002429 if (S_ISFIFO(perm)) // fifo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 STRCAT(IObuff, _("[fifo]"));
2432 c = TRUE;
2433 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 if (S_ISSOCK(perm)) // or socket
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 STRCAT(IObuff, _("[socket]"));
2437 c = TRUE;
2438 }
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002439# ifdef OPEN_CHR_FILES
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002440 if (S_ISCHR(perm)) // or character special
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002441 {
2442 STRCAT(IObuff, _("[character special]"));
2443 c = TRUE;
2444 }
2445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446#endif
2447 if (curbuf->b_p_ro)
2448 {
2449 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2450 c = TRUE;
2451 }
2452 if (read_no_eol_lnum)
2453 {
2454 msg_add_eol();
2455 c = TRUE;
2456 }
2457 if (ff_error == EOL_DOS)
2458 {
2459 STRCAT(IObuff, _("[CR missing]"));
2460 c = TRUE;
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (split)
2463 {
2464 STRCAT(IObuff, _("[long lines split]"));
2465 c = TRUE;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (notconverted)
2468 {
2469 STRCAT(IObuff, _("[NOT converted]"));
2470 c = TRUE;
2471 }
2472 else if (converted)
2473 {
2474 STRCAT(IObuff, _("[converted]"));
2475 c = TRUE;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477#ifdef FEAT_CRYPT
2478 if (cryptkey != NULL)
2479 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002480 crypt_append_msg(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 c = TRUE;
2482 }
2483#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002484 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002486 sprintf((char *)IObuff + STRLEN(IObuff),
2487 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 c = TRUE;
2489 }
2490 else if (illegal_byte > 0)
2491 {
2492 sprintf((char *)IObuff + STRLEN(IObuff),
2493 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2494 c = TRUE;
2495 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002496 else if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 STRCAT(IObuff, _("[READ ERRORS]"));
2499 c = TRUE;
2500 }
2501 if (msg_add_fileformat(fileformat))
2502 c = TRUE;
2503#ifdef FEAT_CRYPT
2504 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002505 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002506 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 else
2508#endif
2509 msg_add_lines(c, (long)linecnt, filesize);
2510
Bram Moolenaard23a8232018-02-10 18:45:26 +01002511 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 msg_scrolled_ign = TRUE;
2513#ifdef ALWAYS_USE_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002514 // Don't show the message when reading stdin, it would end up in a
2515 // message box (which might be shown when exiting!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 if (read_stdin || read_buffer)
2517 p = msg_may_trunc(FALSE, IObuff);
2518 else
2519#endif
Bram Moolenaar473952e2019-09-28 16:30:04 +02002520 {
2521 if (msg_col > 0)
2522 msg_putchar('\r'); // overwrite previous message
Bram Moolenaar32526b32019-01-19 17:43:09 +01002523 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
Bram Moolenaar473952e2019-09-28 16:30:04 +02002524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002526 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002527 // Need to repeat the message after redrawing when:
2528 // - When reading from stdin (the screen will be cleared next).
2529 // - When restart_edit is set (otherwise there will be a delay
2530 // before redrawing).
2531 // - When the screen was scrolled but there is no wait-return
2532 // prompt.
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002533 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 msg_scrolled_ign = FALSE;
2535 }
2536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002537 // with errors writing the file requires ":w!"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (newfile && (error
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002539 || conv_error != 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002540 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 curbuf->b_p_ro = TRUE;
2542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 u_clearline(); // cannot use "U" command after adding lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 /*
2546 * In Ex mode: cursor at last new line.
2547 * Otherwise: cursor at first new line.
2548 */
2549 if (exmode_active)
2550 curwin->w_cursor.lnum = from + linecnt;
2551 else
2552 curwin->w_cursor.lnum = from + 1;
2553 check_cursor_lnum();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002554 beginline(BL_WHITE | BL_FIX); // on first non-blank
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaare1004402020-10-24 20:49:43 +02002556 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002557 {
2558 // Set '[ and '] marks to the newly read lines.
2559 curbuf->b_op_start.lnum = from + 1;
2560 curbuf->b_op_start.col = 0;
2561 curbuf->b_op_end.lnum = from + linecnt;
2562 curbuf->b_op_end.col = 0;
2563 }
Bram Moolenaar03f48552006-02-28 23:52:23 +00002564
Bram Moolenaar4f974752019-02-17 17:44:42 +01002565#ifdef MSWIN
Bram Moolenaar03f48552006-02-28 23:52:23 +00002566 /*
2567 * Work around a weird problem: When a file has two links (only
2568 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002569 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002570 * It's correct again after reading the file, thus reset the timestamp
2571 * here.
2572 */
2573 if (newfile && !read_stdin && !read_buffer
2574 && mch_stat((char *)fname, &st) >= 0)
2575 {
2576 buf_store_time(curbuf, &st, fname);
2577 curbuf->b_mtime_read = curbuf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01002578 curbuf->b_mtime_read_ns = curbuf->b_mtime_ns;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002579 }
2580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 msg_scroll = msg_save;
2583
2584#ifdef FEAT_VIMINFO
2585 /*
2586 * Get the marks before executing autocommands, so they can be used there.
2587 */
2588 check_marks_read();
2589#endif
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 /*
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002592 * We remember if the last line of the read didn't have
2593 * an eol even when 'binary' is off, to support turning 'fixeol' off,
2594 * or writing the read again with 'binary' on. The latter is required
2595 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002597 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002599 // When reloading a buffer put the cursor at the first line that is
2600 // different.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002601 if (flags & READ_KEEP_UNDO)
2602 u_find_first_changed();
2603
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002604#ifdef FEAT_PERSISTENT_UNDO
2605 /*
2606 * When opening a new file locate undo info and read it.
2607 */
2608 if (read_undo_file)
2609 {
2610 char_u hash[UNDO_HASH_SIZE];
2611
2612 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002613 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002614 }
2615#endif
2616
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002617 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
2619 int m = msg_scroll;
2620 int n = msg_scrolled;
2621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 // Save the fileformat now, otherwise the buffer will be considered
2623 // modified if the format/encoding was automatically detected.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002624 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 save_file_ff(curbuf);
2626
2627 /*
2628 * The output from the autocommands should not overwrite anything and
2629 * should not be overwritten: Set msg_scroll, restore its value if no
2630 * output was done.
2631 */
2632 msg_scroll = TRUE;
2633 if (filtering)
2634 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2635 FALSE, curbuf, eap);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +02002636 else if (newfile || (read_buffer && sfname != NULL))
Bram Moolenaarc3691332016-04-20 12:49:49 +02002637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2639 FALSE, curbuf, eap);
Bram Moolenaarc3691332016-04-20 12:49:49 +02002640 if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2641 /*
2642 * EVENT_FILETYPE was not triggered but the buffer already has a
2643 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2644 */
2645 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2646 TRUE, curbuf);
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 else
2649 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2650 FALSE, NULL, eap);
2651 if (msg_scrolled == n)
2652 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002653# ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002654 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 if (recoverymode && error)
2660 return FAIL;
2661 return OK;
2662}
2663
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002664#if defined(OPEN_CHR_FILES) || defined(PROTO)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002665/*
2666 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2667 * which is the name of files used for process substitution output by
2668 * some shells on some operating systems, e.g., bash on SunOS.
2669 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2670 */
Bram Moolenaarf04507d2016-08-20 15:05:39 +02002671 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002672is_dev_fd_file(char_u *fname)
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002673{
2674 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2675 && VIM_ISDIGIT(fname[8])
2676 && *skipdigits(fname + 9) == NUL
2677 && (fname[9] != NUL
2678 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2679}
2680#endif
2681
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002682/*
2683 * From the current line count and characters read after that, estimate the
2684 * line number where we are now.
2685 * Used for error messages that include a line number.
2686 */
2687 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002688readfile_linenr(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 linenr_T linecnt, // line count before reading more bytes
2690 char_u *p, // start of more bytes read
2691 char_u *endp) // end of more bytes read
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002692{
2693 char_u *s;
2694 linenr_T lnum;
2695
2696 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2697 for (s = p; s < endp; ++s)
2698 if (*s == '\n')
2699 ++lnum;
2700 return lnum;
2701}
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703/*
Rob Pilling8196e942022-02-11 15:12:10 +00002704 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be
Bram Moolenaar195d6352005-12-19 22:08:24 +00002705 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * Returns OK or FAIL.
2707 */
2708 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002709prep_exarg(exarg_T *eap, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Bram Moolenaar13505972019-01-24 15:04:48 +01002711 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (eap->cmd == NULL)
2713 return FAIL;
2714
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002715 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2716 eap->force_enc = 8;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002717 eap->bad_char = buf->b_bad_char;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002718 eap->force_ff = *buf->b_p_ff;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002719
2720 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002721 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002722 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 return OK;
2724}
2725
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002726/*
2727 * Set default or forced 'fileformat' and 'binary'.
2728 */
2729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002730set_file_options(int set_options, exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002731{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002732 // set default 'fileformat'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002733 if (set_options)
2734 {
2735 if (eap != NULL && eap->force_ff != 0)
2736 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2737 else if (*p_ffs != NUL)
2738 set_fileformat(default_fileformat(), OPT_LOCAL);
2739 }
2740
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002741 // set or reset 'binary'
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002742 if (eap != NULL && eap->force_bin != 0)
2743 {
2744 int oldval = curbuf->b_p_bin;
2745
2746 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2747 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2748 }
2749}
2750
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002751/*
2752 * Set forced 'fileencoding'.
2753 */
2754 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002755set_forced_fenc(exarg_T *eap)
Bram Moolenaarad875fb2013-07-24 15:02:03 +02002756{
2757 if (eap->force_enc != 0)
2758 {
2759 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2760
2761 if (fenc != NULL)
2762 set_string_option_direct((char_u *)"fenc", -1,
2763 fenc, OPT_FREE|OPT_LOCAL, 0);
2764 vim_free(fenc);
2765 }
2766}
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768/*
2769 * Find next fileencoding to use from 'fileencodings'.
2770 * "pp" points to fenc_next. It's advanced to the next item.
2771 * When there are no more items, an empty string is returned and *pp is set to
2772 * NULL.
Bram Moolenaarf077db22019-08-13 00:18:24 +02002773 * When *pp is not set to NULL, the result is in allocated memory and "alloced"
2774 * is set to TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 */
2776 static char_u *
Bram Moolenaarf077db22019-08-13 00:18:24 +02002777next_fenc(char_u **pp, int *alloced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 char_u *p;
2780 char_u *r;
2781
Bram Moolenaarf077db22019-08-13 00:18:24 +02002782 *alloced = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (**pp == NUL)
2784 {
2785 *pp = NULL;
2786 return (char_u *)"";
2787 }
2788 p = vim_strchr(*pp, ',');
2789 if (p == NULL)
2790 {
2791 r = enc_canonize(*pp);
2792 *pp += STRLEN(*pp);
2793 }
2794 else
2795 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002796 r = vim_strnsave(*pp, p - *pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 *pp = p + 1;
2798 if (r != NULL)
2799 {
2800 p = enc_canonize(r);
2801 vim_free(r);
2802 r = p;
2803 }
2804 }
Bram Moolenaarf077db22019-08-13 00:18:24 +02002805 if (r != NULL)
2806 *alloced = TRUE;
2807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaarf077db22019-08-13 00:18:24 +02002809 // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 r = (char_u *)"";
2811 *pp = NULL;
2812 }
2813 return r;
2814}
2815
Bram Moolenaar13505972019-01-24 15:04:48 +01002816#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
2818 * Convert a file with the 'charconvert' expression.
2819 * This closes the file which is to be read, converts it and opens the
2820 * resulting file for reading.
2821 * Returns name of the resulting converted file (the caller should delete it
2822 * after reading it).
2823 * Returns NULL if the conversion failed ("*fdp" is not set) .
2824 */
2825 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002826readfile_charconvert(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002827 char_u *fname, // name of input file
2828 char_u *fenc, // converted from
2829 int *fdp) // in/out: file descriptor of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 char_u *tmpname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01002832 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002834 tmpname = vim_tempname('r', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (tmpname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002836 errmsg = _("Can't find temp file for conversion");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 else
2838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 close(*fdp); // close the input file, ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 *fdp = -1;
2841 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2842 fname, tmpname) == FAIL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002843 errmsg = _("Conversion with 'charconvert' failed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2845 O_RDONLY | O_EXTRA, 0)) < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002846 errmsg = _("can't read output of 'charconvert'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848
2849 if (errmsg != NULL)
2850 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002851 // Don't use emsg(), it breaks mappings, the retry with
2852 // another type of conversion might still work.
Bram Moolenaar32526b32019-01-19 17:43:09 +01002853 msg(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (tmpname != NULL)
2855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002856 mch_remove(tmpname); // delete converted file
Bram Moolenaard23a8232018-02-10 18:45:26 +01002857 VIM_CLEAR(tmpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 }
2860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002861 // If the input file is closed, open it (caller should check for error).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (*fdp < 0)
2863 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2864
2865 return tmpname;
2866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
2868
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002869#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002871 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2873 * *filesizep are updated.
2874 * Return the (new) encryption key, NULL for no encryption.
2875 */
2876 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002877check_for_cryptkey(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002878 char_u *cryptkey, // previous encryption key or NULL
2879 char_u *ptr, // pointer to read bytes
2880 long *sizep, // length of read bytes
2881 off_T *filesizep, // nr of bytes used from file
2882 int newfile, // editing a new buffer
2883 char_u *fname, // file name to display
2884 int *did_ask) // flag: whether already asked for key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002886 int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002887 int b_p_ro = curbuf->b_p_ro;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002888
2889 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 // Mark the buffer as read-only until the decryption has taken place.
2892 // Avoids accidentally overwriting the file with garbage.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002893 curbuf->b_p_ro = TRUE;
2894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002895 // Set the cryptmethod local to the buffer.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002896 crypt_set_cm_option(curbuf, method);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002897 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
2899 if (*curbuf->b_p_key)
2900 cryptkey = curbuf->b_p_key;
2901 else
2902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002903 // When newfile is TRUE, store the typed key in the 'key'
2904 // option and don't free it. bf needs hash of the key saved.
2905 // Don't ask for the key again when first time Enter was hit.
2906 // Happens when retrying to detect encoding.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002907 smsg(_(need_key_msg), fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002908 msg_scroll = TRUE;
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01002909 crypt_check_method(method);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002910 cryptkey = crypt_get_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002911 *did_ask = TRUE;
2912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 // check if empty key entered
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (cryptkey != NULL && *cryptkey == NUL)
2915 {
2916 if (cryptkey != curbuf->b_p_key)
2917 vim_free(cryptkey);
2918 cryptkey = NULL;
2919 }
2920 }
2921 }
2922
2923 if (cryptkey != NULL)
2924 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002925 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002926
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002927 header_len = crypt_get_header_len(method);
Bram Moolenaar680e0152016-09-25 20:54:11 +02002928 if (*sizep <= header_len)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002929 // invalid header, buffer can't be encrypted
Bram Moolenaar680e0152016-09-25 20:54:11 +02002930 return NULL;
Bram Moolenaar77ab4e22021-07-29 21:23:50 +02002931
2932 curbuf->b_cryptstate = crypt_create_from_header(
2933 method, cryptkey, ptr);
2934 crypt_set_cm_option(curbuf, method);
2935
2936 // Remove cryptmethod specific header from the text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002937 *filesizep += header_len;
2938 *sizep -= header_len;
2939 mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002941 // Restore the read-only flag.
Bram Moolenaarcf81aef2013-08-25 17:46:08 +02002942 curbuf->b_p_ro = b_p_ro;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 // When starting to edit a new file which does not have encryption, clear
2946 // the 'key' option, except when starting up (called with -x argument)
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002947 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2949
2950 return cryptkey;
2951}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002952#endif // FEAT_CRYPT
Bram Moolenaar80794b12010-06-13 05:20:42 +02002953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00002955 * Return TRUE if a file appears to be read-only from the file permissions.
2956 */
2957 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002958check_file_readonly(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002959 char_u *fname, // full path to file
2960 int perm UNUSED) // known permissions on file
Bram Moolenaar5386a122007-06-28 20:02:32 +00002961{
2962#ifndef USE_MCH_ACCESS
2963 int fd = 0;
2964#endif
2965
2966 return (
2967#ifdef USE_MCH_ACCESS
2968# ifdef UNIX
2969 (perm & 0222) == 0 ||
2970# endif
2971 mch_access((char *)fname, W_OK)
2972#else
2973 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
2974 ? TRUE : (close(fd), FALSE)
2975#endif
2976 );
2977}
2978
Bram Moolenaara7870192019-02-14 12:56:36 +01002979#if defined(HAVE_FSYNC) || defined(PROTO)
2980/*
2981 * Call fsync() with Mac-specific exception.
2982 * Return fsync() result: zero for success.
2983 */
2984 int
2985vim_fsync(int fd)
2986{
2987 int r;
2988
2989# ifdef MACOS_X
2990 r = fcntl(fd, F_FULLFSYNC);
Bram Moolenaar91668382019-02-21 12:16:12 +01002991 if (r != 0) // F_FULLFSYNC not working or not supported
Bram Moolenaara7870192019-02-14 12:56:36 +01002992# endif
2993 r = fsync(fd);
2994 return r;
2995}
2996#endif
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002999 * Set the name of the current buffer. Use when the buffer doesn't have a
3000 * name and a ":r" or ":w" command with a file name is used.
3001 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003002 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003003set_rw_fname(char_u *fname, char_u *sfname)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003004{
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003005 buf_T *buf = curbuf;
3006
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003007 // It's like the unnamed buffer is deleted....
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003008 if (curbuf->b_p_bl)
3009 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
3010 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003011#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003012 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003013 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003014#endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003015 if (curbuf != buf)
3016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003017 // We are in another buffer now, don't do the renaming.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003018 emsg(_(e_autocommands_changed_buffer_or_buffer_name));
Bram Moolenaar8b38e242009-06-16 13:35:20 +00003019 return FAIL;
3020 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003021
3022 if (setfname(curbuf, fname, sfname, FALSE) == OK)
3023 curbuf->b_flags |= BF_NOTEDITED;
3024
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003025 // ....and a new named one is created
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003026 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
3027 if (curbuf->b_p_bl)
3028 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003029#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003030 if (aborting()) // autocmds may abort script processing
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003031 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003032#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003034 // Do filetype detection now if 'filetype' is empty.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003035 if (*curbuf->b_p_ft == NUL)
3036 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003037 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar1610d052016-06-09 22:53:01 +02003038 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00003039 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003040 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003041
3042 return OK;
3043}
3044
3045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 * Put file name into IObuff with quotes.
3047 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049msg_add_fname(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 if (fname == NULL)
3052 fname = (char_u *)"-stdin-";
3053 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
3054 IObuff[0] = '"';
3055 STRCAT(IObuff, "\" ");
3056}
3057
3058/*
3059 * Append message for text mode to IObuff.
3060 * Return TRUE if something appended.
3061 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003062 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003063msg_add_fileformat(int eol_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065#ifndef USE_CRNL
3066 if (eol_type == EOL_DOS)
3067 {
3068 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
3069 return TRUE;
3070 }
3071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (eol_type == EOL_MAC)
3073 {
3074 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
3075 return TRUE;
3076 }
Bram Moolenaar00590742019-02-15 21:06:09 +01003077#ifdef USE_CRNL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if (eol_type == EOL_UNIX)
3079 {
3080 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
3081 return TRUE;
3082 }
3083#endif
3084 return FALSE;
3085}
3086
3087/*
3088 * Append line and character count to IObuff.
3089 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00003090 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091msg_add_lines(
3092 int insert_space,
3093 long lnum,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003094 off_T nchars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 char_u *p;
3097
3098 p = IObuff + STRLEN(IObuff);
3099
3100 if (insert_space)
3101 *p++ = ' ';
3102 if (shortmess(SHM_LINES))
Bram Moolenaarbde98102016-07-01 20:03:42 +02003103 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003104 "%ldL, %lldB", lnum, (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 else
3106 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003107 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 p += STRLEN(p);
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003109 vim_snprintf((char *)p, IOSIZE - (p - IObuff),
Bram Moolenaar3f40ce72020-07-05 14:10:13 +02003110 NGETTEXT("%lld byte", "%lld bytes", nchars),
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003111 (varnumber_T)nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113}
3114
3115/*
3116 * Append message for missing line separator to IObuff.
3117 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003119msg_add_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3122}
3123
Bram Moolenaar473952e2019-09-28 16:30:04 +02003124 int
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01003125time_differs(stat_T *st, long mtime, long mtime_ns UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
ichizokdef69df2021-10-15 17:23:12 +01003127 return
3128#ifdef ST_MTIM_NSEC
3129 (long)st->ST_MTIM_NSEC != mtime_ns ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130#endif
ichizokdef69df2021-10-15 17:23:12 +01003131#if defined(__linux__) || defined(MSWIN)
3132 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
3133 // the seconds. Since the roundoff is done when flushing the inode, the
3134 // time may change unexpectedly by one second!!!
3135 (long)st->st_mtime - mtime > 1 || mtime - (long)st->st_mtime > 1
3136#else
3137 (long)st->st_mtime != mtime
3138#endif
3139 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
3142/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003143 * Return TRUE if file encoding "fenc" requires conversion from or to
3144 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003146 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147need_conversion(char_u *fenc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003149 int same_encoding;
3150 int enc_flags;
3151 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003153 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02003154 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003155 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02003156 fenc_flags = 0;
3157 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003158 else
3159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003160 // Ignore difference between "ansi" and "latin1", "ucs-4" and
3161 // "ucs-4be", etc.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003162 enc_flags = get_fio_flags(p_enc);
3163 fenc_flags = get_fio_flags(fenc);
3164 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
3165 }
3166 if (same_encoding)
3167 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003168 // Specified encoding matches with 'encoding'. This requires
3169 // conversion when 'encoding' is Unicode but not UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003170 return enc_unicode != 0;
3171 }
3172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003173 // Encodings differ. However, conversion is not needed when 'enc' is any
3174 // Unicode encoding and the file is UTF-8.
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00003175 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178/*
3179 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
3180 * internal conversion.
3181 * if "ptr" is an empty string, use 'encoding'.
3182 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003183 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184get_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 int prop;
3187
3188 if (*ptr == NUL)
3189 ptr = p_enc;
3190
3191 prop = enc_canon_props(ptr);
3192 if (prop & ENC_UNICODE)
3193 {
3194 if (prop & ENC_2BYTE)
3195 {
3196 if (prop & ENC_ENDIAN_L)
3197 return FIO_UCS2 | FIO_ENDIAN_L;
3198 return FIO_UCS2;
3199 }
3200 if (prop & ENC_4BYTE)
3201 {
3202 if (prop & ENC_ENDIAN_L)
3203 return FIO_UCS4 | FIO_ENDIAN_L;
3204 return FIO_UCS4;
3205 }
3206 if (prop & ENC_2WORD)
3207 {
3208 if (prop & ENC_ENDIAN_L)
3209 return FIO_UTF16 | FIO_ENDIAN_L;
3210 return FIO_UTF16;
3211 }
3212 return FIO_UTF8;
3213 }
3214 if (prop & ENC_LATIN1)
3215 return FIO_LATIN1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003216 // must be ENC_DBCS, requires iconv()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 return 0;
3218}
3219
Bram Moolenaar473952e2019-09-28 16:30:04 +02003220#if defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221/*
3222 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
3223 * for the conversion MS-Windows can do for us. Also accept "utf-8".
3224 * Used for conversion between 'encoding' and 'fileencoding'.
3225 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003226 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003227get_win_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 int cp;
3230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003231 // Cannot do this when 'encoding' is not utf-8 and not a codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (!enc_utf8 && enc_codepage <= 0)
3233 return 0;
3234
3235 cp = encname2codepage(ptr);
3236 if (cp == 0)
3237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003238# ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (STRCMP(ptr, "utf-8") == 0)
3240 cp = CP_UTF8;
3241 else
3242# endif
3243 return 0;
3244 }
3245 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
3246}
3247#endif
3248
Bram Moolenaar473952e2019-09-28 16:30:04 +02003249#if defined(MACOS_CONVERT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250/*
3251 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
3252 * needed for the internal conversion to/from utf-8 or latin1.
3253 */
Bram Moolenaar473952e2019-09-28 16:30:04 +02003254 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003255get_mac_fio_flags(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
3258 && (enc_canon_props(ptr) & ENC_MACROMAN))
3259 return FIO_MACROMAN;
3260 return 0;
3261}
3262#endif
3263
3264/*
3265 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
3266 * "size" must be at least 2.
3267 * Return the name of the encoding and set "*lenp" to the length.
3268 * Returns NULL when no BOM found.
3269 */
3270 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271check_for_bom(
3272 char_u *p,
3273 long size,
3274 int *lenp,
3275 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 char *name = NULL;
3278 int len = 2;
3279
3280 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00003281 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003283 name = "utf-8"; // EF BB BF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 len = 3;
3285 }
3286 else if (p[0] == 0xff && p[1] == 0xfe)
3287 {
3288 if (size >= 4 && p[2] == 0 && p[3] == 0
3289 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
3290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003291 name = "ucs-4le"; // FF FE 00 00
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 len = 4;
3293 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00003294 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003295 name = "ucs-2le"; // FF FE
Bram Moolenaar223a1892008-11-11 20:57:11 +00003296 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003297 // utf-16le is preferred, it also works for ucs-2le text
3298 name = "utf-16le"; // FF FE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 else if (p[0] == 0xfe && p[1] == 0xff
3301 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
3302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003303 // Default to utf-16, it works also for ucs-2 text.
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003304 if (flags == FIO_UCS2)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003305 name = "ucs-2"; // FE FF
Bram Moolenaarffd82c52008-02-20 17:15:26 +00003306 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003307 name = "utf-16"; // FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
3310 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
3311 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003312 name = "ucs-4"; // 00 00 FE FF
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 len = 4;
3314 }
3315
3316 *lenp = len;
3317 return (char_u *)name;
3318}
3319
3320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * Try to find a shortname by comparing the fullname with the current
3322 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003323 * Returns "full_path" or pointer into "full_path" if shortened.
3324 */
3325 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003326shorten_fname1(char_u *full_path)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003327{
Bram Moolenaard9462e32011-04-11 21:35:11 +02003328 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003329 char_u *p = full_path;
3330
Bram Moolenaard9462e32011-04-11 21:35:11 +02003331 dirname = alloc(MAXPATHL);
3332 if (dirname == NULL)
3333 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003334 if (mch_dirname(dirname, MAXPATHL) == OK)
3335 {
3336 p = shorten_fname(full_path, dirname);
3337 if (p == NULL || *p == NUL)
3338 p = full_path;
3339 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02003340 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003341 return p;
3342}
3343
3344/*
3345 * Try to find a shortname by comparing the fullname with the current
3346 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 * Returns NULL if not shorter name possible, pointer into "full_path"
3348 * otherwise.
3349 */
3350 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003351shorten_fname(char_u *full_path, char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 int len;
3354 char_u *p;
3355
3356 if (full_path == NULL)
3357 return NULL;
3358 len = (int)STRLEN(dir_name);
3359 if (fnamencmp(dir_name, full_path, len) == 0)
3360 {
3361 p = full_path + len;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003362#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 /*
Bram Moolenaar4f974752019-02-17 17:44:42 +01003364 * MS-Windows: when a file is in the root directory, dir_name will end
3365 * in a slash, since C: by itself does not define a specific dir. In
3366 * this case p may already be correct. <negri>
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 */
3368 if (!((len > 2) && (*(p - 2) == ':')))
3369#endif
3370 {
3371 if (vim_ispathsep(*p))
3372 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003373#ifndef VMS // the path separator is always part of the path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
3375 p = NULL;
3376#endif
3377 }
3378 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003379#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 /*
3381 * When using a file in the current drive, remove the drive name:
3382 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
3383 * a floppy from "A:\dir" to "B:\dir".
3384 */
3385 else if (len > 3
3386 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
3387 && full_path[1] == ':'
3388 && vim_ispathsep(full_path[2]))
3389 p = full_path + 2;
3390#endif
3391 else
3392 p = NULL;
3393 return p;
3394}
3395
3396/*
Bram Moolenaara796d462018-05-01 14:30:36 +02003397 * Shorten filename of a buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 * When "force" is TRUE: Use full path from now on for files currently being
3399 * edited, both for file name and swap file name. Try to shorten the file
3400 * names a bit, if safe to do so.
3401 * When "force" is FALSE: Only try to shorten absolute file names.
3402 * For buffers that have buftype "nofile" or "scratch": never change the file
3403 * name.
3404 */
3405 void
Bram Moolenaara796d462018-05-01 14:30:36 +02003406shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
3407{
3408 char_u *p;
3409
3410 if (buf->b_fname != NULL
3411#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02003412 && !bt_nofilename(buf)
Bram Moolenaara796d462018-05-01 14:30:36 +02003413#endif
3414 && !path_with_url(buf->b_fname)
3415 && (force
3416 || buf->b_sfname == NULL
3417 || mch_isFullName(buf->b_sfname)))
3418 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003419 if (buf->b_sfname != buf->b_ffname)
3420 VIM_CLEAR(buf->b_sfname);
Bram Moolenaara796d462018-05-01 14:30:36 +02003421 p = shorten_fname(buf->b_ffname, dirname);
3422 if (p != NULL)
3423 {
3424 buf->b_sfname = vim_strsave(p);
3425 buf->b_fname = buf->b_sfname;
3426 }
3427 if (p == NULL || buf->b_fname == NULL)
3428 buf->b_fname = buf->b_ffname;
3429 }
3430}
3431
3432/*
3433 * Shorten filenames for all buffers.
3434 */
3435 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003436shorten_fnames(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 char_u dirname[MAXPATHL];
3439 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 mch_dirname(dirname, MAXPATHL);
Bram Moolenaar29323592016-07-24 22:04:11 +02003442 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaara796d462018-05-01 14:30:36 +02003444 shorten_buf_fname(buf, dirname, force);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003446 // Always make the swap file name a full path, a "nofile" buffer may
3447 // also have a swap file.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003448 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003451 redraw_tabline = TRUE;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003452#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003453 popup_update_preview_title();
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455}
3456
3457#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
3458 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003459 || defined(FEAT_GUI_HAIKU) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 || defined(PROTO)
3461/*
3462 * Shorten all filenames in "fnames[count]" by current directory.
3463 */
3464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003465shorten_filenames(char_u **fnames, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 int i;
3468 char_u dirname[MAXPATHL];
3469 char_u *p;
3470
3471 if (fnames == NULL || count < 1)
3472 return;
3473 mch_dirname(dirname, sizeof(dirname));
3474 for (i = 0; i < count; ++i)
3475 {
3476 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
3477 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003478 // shorten_fname() returns pointer in given "fnames[i]". If free
3479 // "fnames[i]" first, "p" becomes invalid. So we need to copy
3480 // "p" first then free fnames[i].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 p = vim_strsave(p);
3482 vim_free(fnames[i]);
3483 fnames[i] = p;
3484 }
3485 }
3486}
3487#endif
3488
3489/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02003490 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 * fo_o_h.ext for MSDOS or when shortname option set.
3492 *
3493 * Assumed that fname is a valid name found in the filesystem we assure that
3494 * the return value is a different name and ends in 'ext'.
3495 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
3496 * characters otherwise.
3497 * Space for the returned name is allocated, must be freed later.
3498 * Returns NULL when out of memory.
3499 */
3500 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003501modname(
3502 char_u *fname,
3503 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003504 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003506 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 fname, ext, prepend_dot);
3508}
3509
3510 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003511buf_modname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003512 int shortname, // use 8.3 file name
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513 char_u *fname,
3514 char_u *ext,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003515 int prepend_dot) // may prepend a '.' to file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 char_u *retval;
3518 char_u *s;
3519 char_u *e;
3520 char_u *ptr;
3521 int fnamelen, extlen;
3522
3523 extlen = (int)STRLEN(ext);
3524
3525 /*
3526 * If there is no file name we must get the name of the current directory
3527 * (we need the full path in case :cd is used).
3528 */
3529 if (fname == NULL || *fname == NUL)
3530 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003531 retval = alloc(MAXPATHL + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (retval == NULL)
3533 return NULL;
3534 if (mch_dirname(retval, MAXPATHL) == FAIL ||
3535 (fnamelen = (int)STRLEN(retval)) == 0)
3536 {
3537 vim_free(retval);
3538 return NULL;
3539 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003540 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 retval[fnamelen++] = PATHSEP;
3543 retval[fnamelen] = NUL;
3544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003545 prepend_dot = FALSE; // nothing to prepend a dot to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547 else
3548 {
3549 fnamelen = (int)STRLEN(fname);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003550 retval = alloc(fnamelen + extlen + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (retval == NULL)
3552 return NULL;
3553 STRCPY(retval, fname);
3554#ifdef VMS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003555 vms_remove_version(retval); // we do not need versions here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
3557 }
3558
3559 /*
3560 * search backwards until we hit a '/', '\' or ':' replacing all '.'
3561 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
3562 * Then truncate what is after the '/', '\' or ':' to 8 characters for
3563 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
3564 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003565 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003567 if (*ext == '.' && shortname)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003568 if (*ptr == '.') // replace '.' by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003571 {
3572 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00003574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003577 // the file name has at most BASENAMELEN characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
3579 ptr[BASENAMELEN] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
3581 s = ptr + STRLEN(ptr);
3582
3583 /*
3584 * For 8.3 file names we may have to reduce the length.
3585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (shortname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 /*
3589 * If there is no file name, or the file name ends in '/', and the
3590 * extension starts with '.', put a '_' before the dot, because just
3591 * ".ext" is invalid.
3592 */
3593 if (fname == NULL || *fname == NUL
3594 || vim_ispathsep(fname[STRLEN(fname) - 1]))
3595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 *s++ = '_';
3598 }
3599 /*
3600 * If the extension starts with '.', truncate the base name at 8
3601 * characters
3602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00003605 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 s = ptr + 8;
3608 *s = '\0';
3609 }
3610 }
3611 /*
3612 * If the extension doesn't start with '.', and the file name
3613 * doesn't have an extension yet, append a '.'
3614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 else if ((e = vim_strchr(ptr, '.')) == NULL)
3616 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 /*
3618 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00003619 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 */
3621 else if ((int)STRLEN(e) + extlen > 4)
3622 s = e + 4 - extlen;
3623 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003624#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 /*
3626 * If there is no file name, and the extension starts with '.', put a
3627 * '_' before the dot, because just ".ext" may be invalid if it's on a
3628 * FAT partition, and on HPFS it doesn't matter.
3629 */
3630 else if ((fname == NULL || *fname == NUL) && *ext == '.')
3631 *s++ = '_';
3632#endif
3633
3634 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003635 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * ext can start with '.' and cannot exceed 3 more characters.
3637 */
3638 STRCPY(s, ext);
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 /*
3641 * Prepend the dot.
3642 */
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003643 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003645 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 /*
3650 * Check that, after appending the extension, the file name is really
3651 * different.
3652 */
3653 if (fname != NULL && STRCMP(fname, retval) == 0)
3654 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003655 // we search for a character that can be replaced by '_'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 while (--s >= ptr)
3657 {
3658 if (*s != '_')
3659 {
3660 *s = '_';
3661 break;
3662 }
3663 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003664 if (s < ptr) // fname was "________.<ext>", how tricky!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 *ptr = 'v';
3666 }
3667 return retval;
3668}
3669
3670/*
3671 * Like fgets(), but if the file line is too long, it is truncated and the
3672 * rest of the line is thrown away. Returns TRUE for end-of-file.
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003673 * If the line is truncated then buf[size - 2] will not be NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003676vim_fgets(char_u *buf, int size, FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
3678 char *eof;
3679#define FGETS_SIZE 200
3680 char tbuf[FGETS_SIZE];
3681
3682 buf[size - 2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 eof = fgets((char *)buf, size, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
3685 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003686 buf[size - 1] = NUL; // Truncate the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003688 // Now throw away the rest of the line:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 do
3690 {
3691 tbuf[FGETS_SIZE - 2] = NUL;
Bram Moolenaar42335f52018-09-13 15:33:43 +02003692 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
3694 }
3695 return (eof == NULL);
3696}
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698/*
3699 * rename() only works if both files are on the same file system, this
3700 * function will (attempts to?) copy the file across if rename fails -- webb
3701 * Return -1 for failure, 0 for success.
3702 */
3703 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003704vim_rename(char_u *from, char_u *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 int fd_in;
3707 int fd_out;
3708 int n;
3709 char *errmsg = NULL;
3710 char *buffer;
3711#ifdef AMIGA
3712 BPTR flock;
3713#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02003714 stat_T st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003715 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003716#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003717 vim_acl_T acl; // ACL from original file
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003718#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003719 int use_tmp_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003722 * When the names are identical, there is nothing to do. When they refer
3723 * to the same file (ignoring case and slash/backslash differences) but
3724 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 */
3726 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003727 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003728 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003729 use_tmp_file = TRUE;
3730 else
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003731 return 0;
3732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 /*
3735 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
3736 */
3737 if (mch_stat((char *)from, &st) < 0)
3738 return -1;
3739
Bram Moolenaar3576da72008-12-30 15:15:57 +00003740#ifdef UNIX
3741 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003742 stat_T st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003744 // It's possible for the source and destination to be the same file.
3745 // This happens when "from" and "to" differ in case and are on a FAT32
3746 // filesystem. In that case go through a temp file name.
Bram Moolenaar3576da72008-12-30 15:15:57 +00003747 if (mch_stat((char *)to, &st_to) >= 0
3748 && st.st_dev == st_to.st_dev
3749 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003750 use_tmp_file = TRUE;
3751 }
3752#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003753#ifdef MSWIN
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003754 {
3755 BY_HANDLE_FILE_INFORMATION info1, info2;
3756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003757 // It's possible for the source and destination to be the same file.
3758 // In that case go through a temp file name. This makes rename("foo",
3759 // "./foo") a no-op (in a complicated way).
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02003760 if (win32_fileinfo(from, &info1) == FILEINFO_OK
3761 && win32_fileinfo(to, &info2) == FILEINFO_OK
3762 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
3763 && info1.nFileIndexHigh == info2.nFileIndexHigh
3764 && info1.nFileIndexLow == info2.nFileIndexLow)
3765 use_tmp_file = TRUE;
3766 }
3767#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003768
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003769 if (use_tmp_file)
3770 {
3771 char tempname[MAXPATHL + 1];
3772
3773 /*
3774 * Find a name that doesn't exist and is in the same directory.
3775 * Rename "from" to "tempname" and then rename "tempname" to "to".
3776 */
3777 if (STRLEN(from) >= MAXPATHL - 5)
3778 return -1;
3779 STRCPY(tempname, from);
3780 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003781 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003782 sprintf((char *)gettail((char_u *)tempname), "%d", n);
3783 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003784 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003785 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00003786 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003787 if (mch_rename(tempname, (char *)to) == 0)
3788 return 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003789 // Strange, the second step failed. Try moving the
3790 // file back and return failure.
Bram Moolenaar97a6c6a2021-05-03 19:49:51 +02003791 (void)mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00003792 return -1;
3793 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003794 // If it fails for one temp name it will most likely fail
3795 // for any temp name, give up.
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003796 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003797 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003798 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00003799 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00003800 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00003801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 /*
3803 * Delete the "to" file, this is required on some systems to make the
3804 * mch_rename() work, on other systems it makes sure that we don't have
3805 * two files when the mch_rename() fails.
3806 */
3807
3808#ifdef AMIGA
3809 /*
3810 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
3811 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00003812 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 * deleting the "from" file (horror!) we lock it during the remove.
3814 *
3815 * When used for making a backup before writing the file: This should not
3816 * happen with ":w", because startscript() should detect this problem and
3817 * set buf->b_shortname, causing modname() to return a correct ".bak" file
3818 * name. This problem does exist with ":w filename", but then the
3819 * original file will be somewhere else so the backup isn't really
3820 * important. If autoscripting is off the rename may fail.
3821 */
3822 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
3823#endif
3824 mch_remove(to);
3825#ifdef AMIGA
3826 if (flock)
3827 UnLock(flock);
3828#endif
3829
3830 /*
3831 * First try a normal rename, return if it works.
3832 */
3833 if (mch_rename((char *)from, (char *)to) == 0)
3834 return 0;
3835
3836 /*
3837 * Rename() failed, try copying the file.
3838 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003839 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003840#ifdef HAVE_ACL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003841 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003842 acl = mch_get_acl(from);
3843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
3845 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003846 {
3847#ifdef HAVE_ACL
3848 mch_free_acl(acl);
3849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003851 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003852
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003853 // Create the new file with same permissions as the original.
Bram Moolenaara5792f52005-11-23 21:25:05 +00003854 fd_out = mch_open((char *)to,
3855 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (fd_out == -1)
3857 {
3858 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003859#ifdef HAVE_ACL
3860 mch_free_acl(acl);
3861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 return -1;
3863 }
3864
Bram Moolenaar473952e2019-09-28 16:30:04 +02003865 buffer = alloc(WRITEBUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (buffer == NULL)
3867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003869 close(fd_in);
3870#ifdef HAVE_ACL
3871 mch_free_acl(acl);
3872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return -1;
3874 }
3875
Bram Moolenaar473952e2019-09-28 16:30:04 +02003876 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01003877 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003879 errmsg = _(e_error_writing_to_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 break;
3881 }
3882
3883 vim_free(buffer);
3884 close(fd_in);
3885 if (close(fd_out) < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00003886 errmsg = _(e_error_closing_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (n < 0)
3888 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003889 errmsg = _(e_error_reading_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 to = from;
3891 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003892#ifndef UNIX // for Unix mch_open() already set the permission
Bram Moolenaar9be038d2005-03-08 22:34:32 +00003893 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00003894#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003895#ifdef HAVE_ACL
3896 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00003897 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00003898#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02003899#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaare8747442013-11-12 18:09:29 +01003900 mch_copy_sec(from, to);
Bram Moolenaar0671de32013-11-12 05:12:03 +01003901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (errmsg != NULL)
3903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003904 semsg(errmsg, to);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 return -1;
3906 }
3907 mch_remove(from);
3908 return 0;
3909}
3910
3911static int already_warned = FALSE;
3912
3913/*
3914 * Check if any not hidden buffer has been changed.
3915 * Postpone the check if there are characters in the stuff buffer, a global
3916 * command is being executed, a mapping is being executed or an autocommand is
3917 * busy.
3918 * Returns TRUE if some message was written (screen should be redrawn and
3919 * cursor positioned).
3920 */
3921 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003922check_timestamps(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003923 int focus) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 buf_T *buf;
3926 int didit = 0;
3927 int n;
3928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003929 // Don't check timestamps while system() or another low-level function may
3930 // cause us to lose and gain focus.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (no_check_timestamps > 0)
3932 return FALSE;
3933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003934 // Avoid doing a check twice. The OK/Reload dialog can cause a focus
3935 // event and we would keep on checking if the file is steadily growing.
3936 // Do check again after typing something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (focus && did_check_timestamps)
3938 {
3939 need_check_timestamps = TRUE;
3940 return FALSE;
3941 }
3942
3943 if (!stuff_empty() || global_busy || !typebuf_typed()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003944 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003945 need_check_timestamps = TRUE; // check later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
3947 {
3948 ++no_wait_return;
3949 did_check_timestamps = TRUE;
3950 already_warned = FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02003951 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003953 // Only check buffers in a window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (buf->b_nwindows > 0)
3955 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003956 bufref_T bufref;
3957
3958 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 n = buf_check_timestamp(buf, focus);
3960 if (didit < n)
3961 didit = n;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003962 if (n > 0 && !bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003964 // Autocommands have removed the buffer, start at the
3965 // first one again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 buf = firstbuf;
3967 continue;
3968 }
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 --no_wait_return;
3972 need_check_timestamps = FALSE;
3973 if (need_wait_return && didit == 2)
3974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003975 // make sure msg isn't overwritten
Bram Moolenaar32526b32019-01-19 17:43:09 +01003976 msg_puts("\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 out_flush();
3978 }
3979 }
3980 return didit;
3981}
3982
3983/*
3984 * Move all the lines from buffer "frombuf" to buffer "tobuf".
3985 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
3986 * empty.
3987 */
3988 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003989move_lines(buf_T *frombuf, buf_T *tobuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 buf_T *tbuf = curbuf;
3992 int retval = OK;
3993 linenr_T lnum;
3994 char_u *p;
3995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003996 // Copy the lines in "frombuf" to "tobuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 curbuf = tobuf;
3998 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
3999 {
4000 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
4001 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
4002 {
4003 vim_free(p);
4004 retval = FAIL;
4005 break;
4006 }
4007 vim_free(p);
4008 }
4009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004010 // Delete all the lines in "frombuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (retval != FAIL)
4012 {
4013 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00004014 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004015 if (ml_delete(lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004017 // Oops! We could try putting back the saved lines, but that
4018 // might fail again...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 retval = FAIL;
4020 break;
4021 }
4022 }
4023
4024 curbuf = tbuf;
4025 return retval;
4026}
4027
4028/*
4029 * Check if buffer "buf" has been changed.
4030 * Also check if the file for a new buffer unexpectedly appeared.
4031 * return 1 if a changed buffer was found.
4032 * return 2 if a message has been displayed.
4033 * return 0 otherwise.
4034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004036buf_check_timestamp(
4037 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004038 int focus UNUSED) // called for GUI focus event
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004040 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 int stat_res;
4042 int retval = 0;
4043 char_u *path;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004044 char *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00004046 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 int helpmesg = FALSE;
Rob Pilling8196e942022-02-11 15:12:10 +00004048 enum {
4049 RELOAD_NONE,
4050 RELOAD_NORMAL,
4051 RELOAD_DETECT
4052 } reload = RELOAD_NONE;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004053 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4055 int can_reload = FALSE;
4056#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02004057 off_T orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int orig_mode = buf->b_orig_mode;
4059#ifdef FEAT_GUI
4060 int save_mouse_correct = need_mouse_correct;
4061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004063 int n;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004064#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004065 char_u *s;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004066#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004067 bufref_T bufref;
4068
4069 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004071 // If there is no file name, the buffer is not loaded, 'buftype' is
4072 // set, we are in the middle of a save or being called recursively: ignore
4073 // this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 if (buf->b_ffname == NULL
4075 || buf->b_ml.ml_mfp == NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +02004076 || !bt_normal(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 || buf->b_saving
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 || busy
Bram Moolenaar009b2592004-10-24 19:18:58 +00004079#ifdef FEAT_NETBEANS_INTG
4080 || isNetbeansBuffer(buf)
4081#endif
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004082#ifdef FEAT_TERMINAL
4083 || buf->b_term != NULL
4084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 )
4086 return 0;
4087
4088 if ( !(buf->b_flags & BF_NOTEDITED)
4089 && buf->b_mtime != 0
4090 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004091 || time_differs(&st, buf->b_mtime, buf->b_mtime_ns)
Bram Moolenaara7611f62014-05-02 15:46:14 +02004092 || st.st_size != buf->b_orig_size
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#ifdef HAVE_ST_MODE
4094 || (int)st.st_mode != buf->b_orig_mode
4095#else
4096 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
4097#endif
4098 ))
4099 {
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004100 long prev_b_mtime = buf->b_mtime;
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 retval = 1;
4103
Bram Moolenaar386bc822018-07-07 18:34:12 +02004104 // set b_mtime to stop further warnings (e.g., when executing
4105 // FileChangedShell autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (stat_res < 0)
4107 {
Bram Moolenaar8239c622019-05-24 16:46:01 +02004108 // Check the file again later to see if it re-appears.
4109 buf->b_mtime = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 buf->b_orig_size = 0;
4111 buf->b_orig_mode = 0;
4112 }
4113 else
4114 buf_store_time(buf, &st, buf->b_ffname);
4115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 // Don't do anything for a directory. Might contain the file
4117 // explorer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (mch_isdir(buf->b_fname))
4119 ;
4120
4121 /*
4122 * If 'autoread' is set, the buffer has no changes and the file still
4123 * exists, reload the buffer. Use the buffer-local option value if it
4124 * was set, the global option value otherwise.
4125 */
4126 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
4127 && !bufIsChanged(buf) && stat_res >= 0)
Rob Pilling8196e942022-02-11 15:12:10 +00004128 reload = RELOAD_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else
4130 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004131 if (stat_res < 0)
4132 reason = "deleted";
4133 else if (bufIsChanged(buf))
4134 reason = "conflict";
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01004135 /*
4136 * Check if the file contents really changed to avoid giving a
4137 * warning when only the timestamp was set (e.g., checked out of
4138 * CVS). Always warn when the buffer was changed.
4139 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004140 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
4141 reason = "changed";
4142 else if (orig_mode != buf->b_orig_mode)
4143 reason = "mode";
4144 else
4145 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 /*
4148 * Only give the warning if there are no FileChangedShell
4149 * autocommands.
4150 * Avoid being called recursively by setting "busy".
4151 */
4152 busy = TRUE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004153#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004154 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
4155 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004156#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004157 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
4159 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00004160 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 busy = FALSE;
4162 if (n)
4163 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004164 if (!bufref_valid(&bufref))
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004165 emsg(_(e_filechangedshell_autocommand_deleted_buffer));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004166#ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004167 s = get_vim_var_str(VV_FCS_CHOICE);
4168 if (STRCMP(s, "reload") == 0 && *reason != 'd')
Rob Pilling8196e942022-02-11 15:12:10 +00004169 reload = RELOAD_NORMAL;
4170 else if (STRCMP(s, "edit") == 0)
4171 reload = RELOAD_DETECT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004172 else if (STRCMP(s, "ask") == 0)
4173 n = FALSE;
4174 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004175#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004176 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004178 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004180 if (*reason == 'd')
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004181 {
4182 // Only give the message once.
4183 if (prev_b_mtime != -1)
Bram Moolenaar6d057012021-12-31 18:49:43 +00004184 mesg = _(e_file_str_no_longer_available);
Bram Moolenaar674e2bd2019-07-31 20:21:01 +02004185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 else
4187 {
4188 helpmesg = TRUE;
4189#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4190 can_reload = TRUE;
4191#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004192 if (reason[2] == 'n')
4193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004195 mesg2 = _("See \":help W12\" for more info.");
4196 }
4197 else if (reason[1] == 'h')
4198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004200 mesg2 = _("See \":help W11\" for more info.");
4201 }
4202 else if (*reason == 'm')
4203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004205 mesg2 = _("See \":help W16\" for more info.");
4206 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00004207 else
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004209 // Only timestamp changed, store it to avoid a warning
4210 // in check_mtime() later.
Bram Moolenaar85388b52009-06-24 09:58:32 +00004211 buf->b_mtime_read = buf->b_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004212 buf->b_mtime_read_ns = buf->b_mtime_ns;
4213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 }
4216 }
4217
4218 }
4219 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
4220 && vim_fexists(buf->b_ffname))
4221 {
4222 retval = 1;
4223 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
4224 buf->b_flags |= BF_NEW_W;
4225#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4226 can_reload = TRUE;
4227#endif
4228 }
4229
4230 if (mesg != NULL)
4231 {
4232 path = home_replace_save(buf, buf->b_fname);
4233 if (path != NULL)
4234 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004235 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 mesg2 = "";
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004237 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004238 sprintf(tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004239#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004240 // Set warningmsg here, before the unimportant and output-specific
4241 // mesg2 has been appended.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004242 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
Bram Moolenaar496c5262009-03-18 14:42:00 +00004243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4245 if (can_reload)
4246 {
4247 if (*mesg2 != NUL)
4248 {
4249 STRCAT(tbuf, "\n");
4250 STRCAT(tbuf, mesg2);
4251 }
Rob Pilling8196e942022-02-11 15:12:10 +00004252 switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
4253 (char_u *)tbuf,
4254 (char_u *)_("&OK\n&Load File\nLoad File &and Options"),
4255 1, NULL, TRUE))
4256 {
4257 case 2:
4258 reload = RELOAD_NORMAL;
4259 break;
4260 case 3:
4261 reload = RELOAD_DETECT;
4262 break;
4263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 else
4266#endif
4267 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
4268 {
4269 if (*mesg2 != NUL)
4270 {
4271 STRCAT(tbuf, "; ");
4272 STRCAT(tbuf, mesg2);
4273 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 emsg(tbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 retval = 2;
4276 }
4277 else
4278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (!autocmd_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004282 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (*mesg2 != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004284 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 msg_clr_eos();
4286 (void)msg_end();
Bram Moolenaar28ee8922020-10-28 20:20:00 +01004287 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 out_flush();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004290#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (!focus)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004292#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004293 // give the user some time to think about it
Bram Moolenaareda1da02019-11-17 17:06:33 +01004294 ui_delay(1004L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004296 // don't redraw and erase the message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 redraw_cmdline = FALSE;
4298 }
4299 }
4300 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302
4303 vim_free(path);
4304 vim_free(tbuf);
4305 }
4306 }
4307
Rob Pilling8196e942022-02-11 15:12:10 +00004308 if (reload != RELOAD_NONE)
Bram Moolenaar465748e2012-08-29 18:50:54 +02004309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004310 // Reload the buffer.
Rob Pilling8196e942022-02-11 15:12:10 +00004311 buf_reload(buf, orig_mode, reload == RELOAD_DETECT);
Bram Moolenaar465748e2012-08-29 18:50:54 +02004312#ifdef FEAT_PERSISTENT_UNDO
4313 if (buf->b_p_udf && buf->b_ffname != NULL)
4314 {
4315 char_u hash[UNDO_HASH_SIZE];
4316 buf_T *save_curbuf = curbuf;
4317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 // Any existing undo file is unusable, write it now.
Bram Moolenaar465748e2012-08-29 18:50:54 +02004319 curbuf = buf;
4320 u_compute_hash(hash);
4321 u_write_undo(NULL, FALSE, buf, hash);
4322 curbuf = save_curbuf;
4323 }
4324#endif
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004327 // Trigger FileChangedShell when the file was changed in any way.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004328 if (bufref_valid(&bufref) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00004329 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
4330 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 // restore this in case an autocommand has set it; it would break
4333 // 'mousefocus'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 need_mouse_correct = save_mouse_correct;
4335#endif
4336
4337 return retval;
4338}
4339
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004340/*
4341 * Reload a buffer that is already loaded.
4342 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00004343 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
4344 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004345 */
4346 void
Rob Pilling8196e942022-02-11 15:12:10 +00004347buf_reload(buf_T *buf, int orig_mode, int reload_options)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004348{
4349 exarg_T ea;
4350 pos_T old_cursor;
4351 linenr_T old_topline;
4352 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004353 buf_T *savebuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004354 bufref_T bufref;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004355 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004356 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004357 int flags = READ_NEW;
Rob Pilling8196e942022-02-11 15:12:10 +00004358 int prepped = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004361 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004362
Rob Pilling8196e942022-02-11 15:12:10 +00004363 // Unless reload_options is set, we only want to read the text from the
4364 // file, not reset the syntax highlighting, clear marks, diff status, etc.
4365 // Force the fileformat and encoding to be the same.
4366 if (reload_options)
4367 memset(&ea, 0, sizeof(ea));
4368 else
4369 prepped = prep_exarg(&ea, buf);
4370
4371 if (prepped == OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004372 {
4373 old_cursor = curwin->w_cursor;
4374 old_topline = curwin->w_topline;
4375
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004376 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004377 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 // Save all the text, so that the reload can be undone.
4379 // Sync first so that this is a separate undo-able action.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004380 u_sync(FALSE);
4381 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
4382 flags |= READ_KEEP_UNDO;
4383 }
4384
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004385 /*
4386 * To behave like when a new file is edited (matters for
4387 * BufReadPost autocommands) we first need to delete the current
4388 * buffer contents. But if reading the file fails we should keep
4389 * the old contents. Can't use memory only, the file might be
4390 * too big. Use a hidden buffer to move the buffer contents to.
4391 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004392 if (BUFEMPTY() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004393 savebuf = NULL;
4394 else
4395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004397 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004398 set_bufref(&bufref, savebuf);
Bram Moolenaar8424a622006-04-19 21:23:36 +00004399 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 // Open the memline.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004402 curbuf = savebuf;
4403 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004404 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004405 curbuf = buf;
4406 curwin->w_buffer = buf;
4407 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00004408 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004409 || move_lines(buf, savebuf) == FAIL)
4410 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004411 semsg(_(e_could_not_prepare_for_reloading_str), buf->b_fname);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004412 saved = FAIL;
4413 }
4414 }
4415
4416 if (saved == OK)
4417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004418 curbuf->b_flags |= BF_CHECK_RO; // check for RO again
4419 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004420 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
4421 (linenr_T)0,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01004422 (linenr_T)MAXLNUM, &ea, flags) != OK)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004423 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004424#if defined(FEAT_EVAL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004425 if (!aborting())
4426#endif
Bram Moolenaareaaac012022-01-02 17:00:40 +00004427 semsg(_(e_could_not_reload_str), buf->b_fname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004428 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004429 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 // Put the text back from the save buffer. First
4431 // delete any lines that readfile() added.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004432 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02004433 if (ml_delete(buf->b_ml.ml_line_count) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004434 break;
4435 (void)move_lines(savebuf, buf);
4436 }
4437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004438 else if (buf == curbuf) // "buf" still valid
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004439 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 // Mark the buffer as unmodified and free undo info.
Bram Moolenaarc024b462019-06-08 18:07:21 +02004441 unchanged(buf, TRUE, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004442 if ((flags & READ_KEEP_UNDO) == 0)
4443 {
4444 u_blockfree(buf);
4445 u_clearall(buf);
4446 }
4447 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004448 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 // Mark all undo states as changed.
Bram Moolenaar59f931e2010-07-24 20:27:03 +02004450 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02004451 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004452 }
4453 }
4454 vim_free(ea.cmd);
4455
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004456 if (savebuf != NULL && bufref_valid(&bufref))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004457 wipe_buffer(savebuf, FALSE);
4458
4459#ifdef FEAT_DIFF
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004460 // Invalidate diff info if necessary.
Bram Moolenaar8424a622006-04-19 21:23:36 +00004461 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004462#endif
4463
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004464 // Restore the topline and cursor position and check it (lines may
4465 // have been removed).
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004466 if (old_topline > curbuf->b_ml.ml_line_count)
4467 curwin->w_topline = curbuf->b_ml.ml_line_count;
4468 else
4469 curwin->w_topline = old_topline;
4470 curwin->w_cursor = old_cursor;
4471 check_cursor();
4472 update_topline();
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004473 keep_filetype = FALSE;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004474#ifdef FEAT_FOLDING
4475 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004476 win_T *wp;
4477 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 // Update folds unless they are defined manually.
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00004480 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004481 if (wp->w_buffer == curwin->w_buffer
4482 && !foldmethodIsManual(wp))
4483 foldUpdateAll(wp);
4484 }
4485#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 // If the mode didn't change and 'readonly' was set, keep the old
4487 // value; the user probably used the ":view" command. But don't
4488 // reset it, might have had a read error.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004489 if (orig_mode == curbuf->b_orig_mode)
4490 curbuf->b_p_ro |= old_ro;
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 // Modelines must override settings done by autocommands.
Bram Moolenaar52f85b72013-01-30 14:13:56 +01004493 do_modelines(0);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004494 }
4495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // restore curwin/curbuf and a few other things
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004497 aucmd_restbuf(&aco);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaar631d6f62005-06-07 21:02:10 +00004499}
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 void
Bram Moolenaar8767f522016-07-01 17:17:39 +02004502buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
4504 buf->b_mtime = (long)st->st_mtime;
Leah Neukirchen0a7984a2021-10-14 21:27:55 +01004505#ifdef ST_MTIM_NSEC
4506 buf->b_mtime_ns = (long)st->ST_MTIM_NSEC;
4507#else
4508 buf->b_mtime_ns = 0;
4509#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02004510 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#ifdef HAVE_ST_MODE
4512 buf->b_orig_mode = (int)st->st_mode;
4513#else
4514 buf->b_orig_mode = mch_getperm(fname);
4515#endif
4516}
4517
4518/*
4519 * Adjust the line with missing eol, used for the next write.
4520 * Used for do_filter(), when the input lines for the filter are deleted.
4521 */
4522 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004523write_lnum_adjust(linenr_T offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004526 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527}
4528
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004529// Subfuncions for readdirex()
4530#ifdef FEAT_EVAL
4531# ifdef MSWIN
4532 static char_u *
4533getfpermwfd(WIN32_FIND_DATAW *wfd, char_u *perm)
4534{
4535 stat_T st;
4536 unsigned short st_mode;
4537 DWORD flag = wfd->dwFileAttributes;
4538 WCHAR *wp;
4539
4540 st_mode = (flag & FILE_ATTRIBUTE_DIRECTORY)
4541 ? (_S_IFDIR | _S_IEXEC) : _S_IFREG;
4542 st_mode |= (flag & FILE_ATTRIBUTE_READONLY)
4543 ? _S_IREAD : (_S_IREAD | _S_IWRITE);
4544
4545 wp = wcsrchr(wfd->cFileName, L'.');
4546 if (wp != NULL)
4547 {
4548 if (_wcsicmp(wp, L".exe") == 0 ||
4549 _wcsicmp(wp, L".com") == 0 ||
4550 _wcsicmp(wp, L".cmd") == 0 ||
4551 _wcsicmp(wp, L".bat") == 0)
4552 st_mode |= _S_IEXEC;
4553 }
4554
4555 // Copy user bits to group/other.
4556 st_mode |= (st_mode & 0700) >> 3;
4557 st_mode |= (st_mode & 0700) >> 6;
4558
4559 st.st_mode = st_mode;
4560 return getfpermst(&st, perm);
4561}
4562
4563 static char_u *
4564getftypewfd(WIN32_FIND_DATAW *wfd)
4565{
4566 DWORD flag = wfd->dwFileAttributes;
4567 DWORD tag = wfd->dwReserved0;
4568
4569 if (flag & FILE_ATTRIBUTE_REPARSE_POINT)
4570 {
4571 if (tag == IO_REPARSE_TAG_MOUNT_POINT)
4572 return (char_u*)"junction";
4573 else if (tag == IO_REPARSE_TAG_SYMLINK)
4574 {
4575 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4576 return (char_u*)"linkd";
4577 else
4578 return (char_u*)"link";
4579 }
4580 return (char_u*)"reparse"; // unknown reparse point type
4581 }
4582 if (flag & FILE_ATTRIBUTE_DIRECTORY)
4583 return (char_u*)"dir";
4584 else
4585 return (char_u*)"file";
4586}
4587
4588 static dict_T *
4589create_readdirex_item(WIN32_FIND_DATAW *wfd)
4590{
4591 dict_T *item;
4592 char_u *p;
4593 varnumber_T size, time;
4594 char_u permbuf[] = "---------";
4595
4596 item = dict_alloc();
4597 if (item == NULL)
4598 return NULL;
4599 item->dv_refcount++;
4600
4601 p = utf16_to_enc(wfd->cFileName, NULL);
4602 if (p == NULL)
4603 goto theend;
4604 if (dict_add_string(item, "name", p) == FAIL)
4605 {
4606 vim_free(p);
4607 goto theend;
4608 }
4609 vim_free(p);
4610
4611 size = (((varnumber_T)wfd->nFileSizeHigh) << 32) | wfd->nFileSizeLow;
4612 if (dict_add_number(item, "size", size) == FAIL)
4613 goto theend;
4614
4615 // Convert FILETIME to unix time.
4616 time = (((((varnumber_T)wfd->ftLastWriteTime.dwHighDateTime) << 32) |
4617 wfd->ftLastWriteTime.dwLowDateTime)
4618 - 116444736000000000) / 10000000;
4619 if (dict_add_number(item, "time", time) == FAIL)
4620 goto theend;
4621
4622 if (dict_add_string(item, "type", getftypewfd(wfd)) == FAIL)
4623 goto theend;
4624 if (dict_add_string(item, "perm", getfpermwfd(wfd, permbuf)) == FAIL)
4625 goto theend;
4626
4627 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4628 goto theend;
4629 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4630 goto theend;
4631
4632 return item;
4633
4634theend:
4635 dict_unref(item);
4636 return NULL;
4637}
4638# else
4639 static dict_T *
4640create_readdirex_item(char_u *path, char_u *name)
4641{
4642 dict_T *item;
4643 char *p;
4644 size_t len;
4645 stat_T st;
4646 int ret, link = FALSE;
4647 varnumber_T size;
4648 char_u permbuf[] = "---------";
Bram Moolenaarab540322020-06-10 15:55:36 +02004649 char_u *q = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004650 struct passwd *pw;
4651 struct group *gr;
4652
4653 item = dict_alloc();
4654 if (item == NULL)
4655 return NULL;
4656 item->dv_refcount++;
4657
4658 len = STRLEN(path) + 1 + STRLEN(name) + 1;
4659 p = alloc(len);
4660 if (p == NULL)
4661 goto theend;
4662 vim_snprintf(p, len, "%s/%s", path, name);
4663 ret = mch_lstat(p, &st);
4664 if (ret >= 0 && S_ISLNK(st.st_mode))
4665 {
4666 link = TRUE;
4667 ret = mch_stat(p, &st);
Bram Moolenaarab540322020-06-10 15:55:36 +02004668 if (ret < 0)
4669 q = (char_u*)"link";
4670
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004671 }
4672 vim_free(p);
4673
4674 if (dict_add_string(item, "name", name) == FAIL)
4675 goto theend;
4676
4677 if (ret >= 0)
4678 {
4679 size = (varnumber_T)st.st_size;
4680 if (S_ISDIR(st.st_mode))
4681 size = 0;
4682 // non-perfect check for overflow
Bram Moolenaar441d60e2020-06-02 22:19:50 +02004683 else if ((off_T)size != (off_T)st.st_size)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004684 size = -2;
4685 if (dict_add_number(item, "size", size) == FAIL)
4686 goto theend;
4687 if (dict_add_number(item, "time", (varnumber_T)st.st_mtime) == FAIL)
4688 goto theend;
4689
4690 if (link)
4691 {
4692 if (S_ISDIR(st.st_mode))
4693 q = (char_u*)"linkd";
4694 else
4695 q = (char_u*)"link";
4696 }
4697 else
4698 q = getftypest(&st);
4699 if (dict_add_string(item, "type", q) == FAIL)
4700 goto theend;
4701 if (dict_add_string(item, "perm", getfpermst(&st, permbuf)) == FAIL)
4702 goto theend;
4703
4704 pw = getpwuid(st.st_uid);
4705 if (pw == NULL)
4706 q = (char_u*)"";
4707 else
4708 q = (char_u*)pw->pw_name;
4709 if (dict_add_string(item, "user", q) == FAIL)
4710 goto theend;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004711# if !defined(VMS) || (defined(VMS) && defined(HAVE_XOS_R_H))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004712 gr = getgrgid(st.st_gid);
4713 if (gr == NULL)
4714 q = (char_u*)"";
4715 else
4716 q = (char_u*)gr->gr_name;
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004717# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004718 if (dict_add_string(item, "group", q) == FAIL)
4719 goto theend;
4720 }
4721 else
4722 {
4723 if (dict_add_number(item, "size", -1) == FAIL)
4724 goto theend;
4725 if (dict_add_number(item, "time", -1) == FAIL)
4726 goto theend;
Bram Moolenaarab540322020-06-10 15:55:36 +02004727 if (dict_add_string(item, "type", q == NULL ? (char_u*)"" : q) == FAIL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004728 goto theend;
4729 if (dict_add_string(item, "perm", (char_u*)"") == FAIL)
4730 goto theend;
4731 if (dict_add_string(item, "user", (char_u*)"") == FAIL)
4732 goto theend;
4733 if (dict_add_string(item, "group", (char_u*)"") == FAIL)
4734 goto theend;
4735 }
4736 return item;
4737
4738theend:
4739 dict_unref(item);
4740 return NULL;
4741}
4742# endif
4743
4744 static int
4745compare_readdirex_item(const void *p1, const void *p2)
4746{
4747 char_u *name1, *name2;
4748
4749 name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
4750 name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004751 if (readdirex_sort == READDIR_SORT_BYTE)
4752 return STRCMP(name1, name2);
4753 else if (readdirex_sort == READDIR_SORT_IC)
4754 return STRICMP(name1, name2);
4755 else
4756 return STRCOLL(name1, name2);
4757}
4758
4759 static int
4760compare_readdir_item(const void *s1, const void *s2)
4761{
4762 if (readdirex_sort == READDIR_SORT_BYTE)
4763 return STRCMP(*(char **)s1, *(char **)s2);
4764 else if (readdirex_sort == READDIR_SORT_IC)
4765 return STRICMP(*(char **)s1, *(char **)s2);
4766 else
4767 return STRCOLL(*(char **)s1, *(char **)s2);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004768}
4769#endif
4770
Bram Moolenaarda440d22016-01-16 21:27:23 +01004771#if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
4772/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004773 * Core part of "readdir()" and "readdirex()" function.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004774 * Retrieve the list of files/directories of "path" into "gap".
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004775 * If "withattr" is TRUE, retrieve the names and their attributes.
4776 * If "withattr" is FALSE, retrieve the names only.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004777 * Return OK for success, FAIL for failure.
4778 */
4779 int
4780readdir_core(
4781 garray_T *gap,
4782 char_u *path,
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004783 int withattr UNUSED,
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004784 void *context,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004785 int (*checkitem)(void *context, void *item),
4786 int sort)
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004787{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004788 int failed = FALSE;
4789 char_u *p;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004790# ifdef MSWIN
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004791 char_u *buf;
4792 int ok;
4793 HANDLE hFind = INVALID_HANDLE_VALUE;
4794 WIN32_FIND_DATAW wfd;
4795 WCHAR *wn = NULL; // UTF-16 name, NULL when not used.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004796# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004797 DIR *dirp;
4798 struct dirent *dp;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01004799# endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004800
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004801 ga_init2(gap, sizeof(void *), 20);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004802
4803# ifdef FEAT_EVAL
4804# define FREE_ITEM(item) do { \
4805 if (withattr) \
kylo252ae6f1d82022-02-16 19:24:07 +00004806 dict_unref((dict_T*)(item)); \
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004807 else \
4808 vim_free(item); \
4809 } while (0)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004810
4811 readdirex_sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004812# else
4813# define FREE_ITEM(item) vim_free(item)
4814# endif
4815
4816# ifdef MSWIN
4817 buf = alloc(MAXPATHL);
4818 if (buf == NULL)
4819 return FAIL;
4820 STRNCPY(buf, path, MAXPATHL-5);
4821 p = buf + STRLEN(buf);
4822 MB_PTR_BACK(buf, p);
4823 if (*p == '\\' || *p == '/')
4824 *p = NUL;
4825 STRCAT(p, "\\*");
4826
4827 wn = enc_to_utf16(buf, NULL);
4828 if (wn != NULL)
4829 hFind = FindFirstFileW(wn, &wfd);
4830 ok = (hFind != INVALID_HANDLE_VALUE);
4831 if (!ok)
4832 {
4833 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004834 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004835 }
4836 else
4837 {
4838 while (ok)
4839 {
4840 int ignore;
4841 void *item;
4842 WCHAR *wp;
4843
4844 wp = wfd.cFileName;
4845 ignore = wp[0] == L'.' &&
4846 (wp[1] == NUL ||
4847 (wp[1] == L'.' && wp[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004848 if (ignore)
4849 {
4850 ok = FindNextFileW(hFind, &wfd);
4851 continue;
4852 }
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004853# ifdef FEAT_EVAL
4854 if (withattr)
4855 item = (void*)create_readdirex_item(&wfd);
4856 else
4857# endif
4858 item = (void*)utf16_to_enc(wfd.cFileName, NULL);
4859 if (item == NULL)
4860 {
4861 failed = TRUE;
4862 break;
4863 }
4864
4865 if (!ignore && checkitem != NULL)
4866 {
4867 int r = checkitem(context, item);
4868
4869 if (r < 0)
4870 {
4871 FREE_ITEM(item);
4872 break;
4873 }
4874 if (r == 0)
4875 ignore = TRUE;
4876 }
4877
4878 if (!ignore)
4879 {
4880 if (ga_grow(gap, 1) == OK)
4881 ((void**)gap->ga_data)[gap->ga_len++] = item;
4882 else
4883 {
4884 failed = TRUE;
4885 FREE_ITEM(item);
4886 break;
4887 }
4888 }
4889 else
4890 FREE_ITEM(item);
4891
4892 ok = FindNextFileW(hFind, &wfd);
4893 }
4894 FindClose(hFind);
4895 }
4896
4897 vim_free(buf);
4898 vim_free(wn);
4899# else // MSWIN
4900 dirp = opendir((char *)path);
4901 if (dirp == NULL)
4902 {
4903 failed = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004904 semsg(_(e_cant_open_file_str), path);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004905 }
4906 else
4907 {
4908 for (;;)
4909 {
4910 int ignore;
4911 void *item;
4912
4913 dp = readdir(dirp);
4914 if (dp == NULL)
4915 break;
4916 p = (char_u *)dp->d_name;
4917
4918 ignore = p[0] == '.' &&
4919 (p[1] == NUL ||
4920 (p[1] == '.' && p[2] == NUL));
Bram Moolenaarab540322020-06-10 15:55:36 +02004921 if (ignore)
4922 continue;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004923# ifdef FEAT_EVAL
4924 if (withattr)
4925 item = (void*)create_readdirex_item(path, p);
4926 else
4927# endif
4928 item = (void*)vim_strsave(p);
4929 if (item == NULL)
4930 {
4931 failed = TRUE;
4932 break;
4933 }
4934
Bram Moolenaarfe154992022-03-22 20:42:12 +00004935 if (checkitem != NULL)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004936 {
4937 int r = checkitem(context, item);
4938
4939 if (r < 0)
4940 {
4941 FREE_ITEM(item);
4942 break;
4943 }
4944 if (r == 0)
4945 ignore = TRUE;
4946 }
4947
4948 if (!ignore)
4949 {
4950 if (ga_grow(gap, 1) == OK)
4951 ((void**)gap->ga_data)[gap->ga_len++] = item;
4952 else
4953 {
4954 failed = TRUE;
4955 FREE_ITEM(item);
4956 break;
4957 }
4958 }
4959 else
4960 FREE_ITEM(item);
4961 }
4962
4963 closedir(dirp);
4964 }
4965# endif // MSWIN
4966
4967# undef FREE_ITEM
4968
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004969 if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004970 {
4971# ifdef FEAT_EVAL
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004972 readdirex_sort = sort;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004973 if (withattr)
4974 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
4975 compare_readdirex_item);
4976 else
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004977 qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
4978 compare_readdir_item);
4979# else
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004980 sort_strings((char_u **)gap->ga_data, gap->ga_len);
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02004981# endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02004982 }
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004983
4984 return failed ? FAIL : OK;
4985}
4986
4987/*
Bram Moolenaarda440d22016-01-16 21:27:23 +01004988 * Delete "name" and everything in it, recursively.
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004989 * return 0 for success, -1 if some file was not deleted.
Bram Moolenaarda440d22016-01-16 21:27:23 +01004990 */
4991 int
4992delete_recursive(char_u *name)
4993{
4994 int result = 0;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004995 int i;
4996 char_u *exp;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004997 garray_T ga;
Bram Moolenaarda440d22016-01-16 21:27:23 +01004998
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02004999 // A symbolic link to a directory itself is deleted, not the directory it
5000 // points to.
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005001 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01005002# if defined(UNIX) || defined(MSWIN)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005003 mch_isrealdir(name)
Bram Moolenaar203258c2016-01-17 22:15:16 +01005004# else
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005005 mch_isdir(name)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01005006# endif
5007 )
Bram Moolenaarda440d22016-01-16 21:27:23 +01005008 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005009 exp = vim_strsave(name);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005010 if (exp == NULL)
5011 return -1;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02005012 if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
Bram Moolenaarda440d22016-01-16 21:27:23 +01005013 {
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005014 for (i = 0; i < ga.ga_len; ++i)
5015 {
5016 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp,
5017 ((char_u **)ga.ga_data)[i]);
5018 if (delete_recursive(NameBuff) != 0)
zeertzjq47870032022-04-05 15:31:01 +01005019 // Remember the failure but continue deleting any further
5020 // entries.
Bram Moolenaarda440d22016-01-16 21:27:23 +01005021 result = -1;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02005022 }
5023 ga_clear_strings(&ga);
zeertzjq47870032022-04-05 15:31:01 +01005024 if (mch_rmdir(exp) != 0)
5025 result = -1;
Bram Moolenaarda440d22016-01-16 21:27:23 +01005026 }
5027 else
5028 result = -1;
5029 vim_free(exp);
Bram Moolenaarda440d22016-01-16 21:27:23 +01005030 }
5031 else
5032 result = mch_remove(name) == 0 ? 0 : -1;
5033
5034 return result;
5035}
5036#endif
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#if defined(TEMPDIRNAMES) || defined(PROTO)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039static long temp_count = 0; // Temp filename counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005041# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5042/*
5043 * Open temporary directory and take file lock to prevent
5044 * to be auto-cleaned.
5045 */
5046 static void
5047vim_opentempdir(void)
5048{
5049 DIR *dp = NULL;
5050
5051 if (vim_tempdir_dp != NULL)
5052 return;
5053
5054 dp = opendir((const char*)vim_tempdir);
5055
5056 if (dp != NULL)
5057 {
5058 vim_tempdir_dp = dp;
5059 flock(dirfd(vim_tempdir_dp), LOCK_SH);
5060 }
5061}
5062
5063/*
5064 * Close temporary directory - it automatically release file lock.
5065 */
5066 static void
5067vim_closetempdir(void)
5068{
5069 if (vim_tempdir_dp != NULL)
5070 {
5071 closedir(vim_tempdir_dp);
5072 vim_tempdir_dp = NULL;
5073 }
5074}
5075# endif
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077/*
5078 * Delete the temp directory and all files it contains.
5079 */
5080 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005081vim_deltempdir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 if (vim_tempdir != NULL)
5084 {
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005085# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5086 vim_closetempdir();
5087# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 // remove the trailing path separator
Bram Moolenaarda440d22016-01-16 21:27:23 +01005089 gettail(vim_tempdir)[-1] = NUL;
5090 delete_recursive(vim_tempdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005091 VIM_CLEAR(vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00005096 * Directory "tempdir" was created. Expand this name to a full path and put
5097 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
5098 * "tempdir" must be no longer than MAXPATHL.
5099 */
5100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005101vim_settempdir(char_u *tempdir)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005102{
5103 char_u *buf;
5104
Bram Moolenaar964b3742019-05-24 18:54:09 +02005105 buf = alloc(MAXPATHL + 2);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005106 if (buf != NULL)
5107 {
5108 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
5109 STRCPY(buf, tempdir);
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005110 add_pathsep(buf);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005111 vim_tempdir = vim_strsave(buf);
Bram Moolenaarb2d0e512020-05-07 18:37:03 +02005112# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
5113 vim_opentempdir();
5114# endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005115 vim_free(buf);
5116 }
5117}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00005118#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00005119
5120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 * vim_tempname(): Return a unique name that can be used for a temp file.
5122 *
Bram Moolenaar76ae22f2016-06-13 20:00:29 +02005123 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is
5124 * guaranteed to NOT be created.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 *
5126 * The returned pointer is to allocated memory.
5127 * The returned pointer is NULL if no valid name was found.
5128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005130vim_tempname(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005131 int extra_char UNUSED, // char to use in the name instead of '?'
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005132 int keep UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134#ifdef USE_TMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135 char_u itmp[L_tmpnam]; // use tmpnam()
Bram Moolenaar4f974752019-02-17 17:44:42 +01005136#elif defined(MSWIN)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005137 WCHAR itmp[TEMPNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#else
5139 char_u itmp[TEMPNAMELEN];
5140#endif
5141
5142#ifdef TEMPDIRNAMES
5143 static char *(tempdirs[]) = {TEMPDIRNAMES};
5144 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145# ifndef EEXIST
Bram Moolenaar8767f522016-07-01 17:17:39 +02005146 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147# endif
5148
5149 /*
5150 * This will create a directory for private use by this instance of Vim.
5151 * This is done once, and the same directory is used for all temp files.
5152 * This method avoids security problems because of symlink attacks et al.
5153 * It's also a bit faster, because we only need to check for an existing
5154 * file when creating the directory and not for each temp file.
5155 */
5156 if (vim_tempdir == NULL)
5157 {
5158 /*
5159 * Try the entries in TEMPDIRNAMES to create the temp directory.
5160 */
K.Takataeeec2542021-06-02 13:28:16 +02005161 for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005163# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005164 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005165 long nr;
5166 long off;
5167# endif
5168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005169 // Expand $TMP, leave room for "/v1100000/999999999".
5170 // Skip the directory check if the expansion fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
Bram Moolenaare1a61992015-12-03 21:02:27 +01005172 if (itmp[0] != '$' && mch_isdir(itmp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005174 // directory exists
Bram Moolenaara06ecab2016-07-16 14:47:36 +02005175 add_pathsep(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
Bram Moolenaareaf03392009-11-17 11:08:52 +00005177# ifdef HAVE_MKDTEMP
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005178 {
5179# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005180 // Make sure the umask doesn't remove the executable bit.
5181 // "repl" has been reported to use "177".
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005182 mode_t umask_save = umask(077);
5183# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005184 // Leave room for filename
Bram Moolenaar35d88f42016-06-04 14:52:00 +02005185 STRCAT(itmp, "vXXXXXX");
5186 if (mkdtemp((char *)itmp) != NULL)
5187 vim_settempdir(itmp);
5188# if defined(UNIX) || defined(VMS)
5189 (void)umask(umask_save);
5190# endif
5191 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005192# else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // Get an arbitrary number of up to 6 digits. When it's
5194 // unlikely that it already exists it will be faster,
5195 // otherwise it doesn't matter. The use of mkdir() avoids any
5196 // security problems because of the predictable number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005198 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005200 // Try up to 10000 different values until we find a name that
5201 // doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 for (off = 0; off < 10000L; ++off)
5203 {
5204 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005205# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
Bram Moolenaareaf03392009-11-17 11:08:52 +00005209 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
5210# ifndef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005211 // If mkdir() does not set errno to EEXIST, check for
5212 // existing file here. There is a race condition then,
5213 // although it's fail-safe.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (mch_stat((char *)itmp, &st) >= 0)
5215 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00005216# endif
5217# if defined(UNIX) || defined(VMS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005218 // Make sure the umask doesn't remove the executable bit.
5219 // "repl" has been reported to use "177".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005223# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00005225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (r == 0)
5227 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00005228 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 break;
5230 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00005231# ifdef EEXIST
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // If the mkdir() didn't fail because the file/dir exists,
5233 // we probably can't create any dir here, try another
5234 // place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00005236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005239# endif // HAVE_MKDTEMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (vim_tempdir != NULL)
5241 break;
5242 }
5243 }
5244 }
5245
5246 if (vim_tempdir != NULL)
5247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // There is no need to check if the file exists, because we own the
5249 // directory and nobody else creates a file in it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
5251 return vim_strsave(itmp);
5252 }
5253
5254 return NULL;
5255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005256#else // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
Bram Moolenaar4f974752019-02-17 17:44:42 +01005258# ifdef MSWIN
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005259 WCHAR wszTempFile[_MAX_PATH + 1];
5260 WCHAR buf4[4];
Bram Moolenaar2472a742020-11-26 19:47:28 +01005261 WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 char_u *retval;
5263 char_u *p;
Mike Williamsa3d1b292021-06-30 20:56:00 +02005264 char_u *shname;
Bram Moolenaar2472a742020-11-26 19:47:28 +01005265 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005267 wcscpy(itmp, L"");
5268 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01005269 {
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005270 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir
Bram Moolenaar2472a742020-11-26 19:47:28 +01005271 wszTempFile[1] = L'\\';
5272 wszTempFile[2] = NUL;
Bram Moolenaarb1891912011-02-09 14:47:03 +01005273 }
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005274 wcscpy(buf4, L"VIM");
Bram Moolenaar2472a742020-11-26 19:47:28 +01005275
5276 // randomize the name to avoid collisions
5277 i = mch_get_pid() + extra_char;
5278 buf4[1] = chartab[i % 36];
5279 buf4[2] = chartab[101 * i % 36];
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005280 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 return NULL;
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005282 if (!keep)
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005283 // GetTempFileName() will create the file, we don't want that
5284 (void)DeleteFileW(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005286 // Backslashes in a temp file name cause problems when filtering with
5287 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who
Mike Williams12795022021-06-28 20:53:58 +02005288 // didn't set 'shellslash' but only if not using PowerShell.
Bram Moolenaarec0f50a2019-02-10 23:26:13 +01005289 retval = utf16_to_enc(itmp, NULL);
Mike Williamsa3d1b292021-06-30 20:56:00 +02005290 shname = gettail(p_sh);
5291 if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
5292 || strstr((char *)shname, "pwsh") != NULL ))
5293 || p_ssl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 for (p = retval; *p; ++p)
5295 if (*p == '\\')
5296 *p = '/';
5297 return retval;
5298
Bram Moolenaar4f974752019-02-17 17:44:42 +01005299# else // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005302 char_u *p;
5303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304 // tmpnam() will make its own name
Bram Moolenaar95474ca2011-02-09 16:44:51 +01005305 p = tmpnam((char *)itmp);
5306 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 return NULL;
5308# else
5309 char_u *p;
5310
5311# ifdef VMS_TEMPNAM
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005312 // mktemp() is not working on VMS. It seems to be
5313 // a do-nothing function. Therefore we use tempnam().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 sprintf((char *)itmp, "VIM%c", extra_char);
5315 p = (char_u *)tempnam("tmp:", (char *)itmp);
5316 if (p != NULL)
5317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 // VMS will use '.LIS' if we don't explicitly specify an extension,
5319 // and VIM will then be unable to find the file later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 STRCPY(itmp, p);
5321 STRCAT(itmp, ".txt");
5322 free(p);
5323 }
5324 else
5325 return NULL;
5326# else
5327 STRCPY(itmp, TEMPNAME);
5328 if ((p = vim_strchr(itmp, '?')) != NULL)
5329 *p = extra_char;
5330 if (mktemp((char *)itmp) == NULL)
5331 return NULL;
5332# endif
5333# endif
5334
5335 return vim_strsave(itmp);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005336# endif // MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337#endif // TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338}
5339
5340#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
5341/*
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005342 * Convert all backslashes in fname to forward slashes in-place, unless when
5343 * it looks like a URL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 */
5345 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346forward_slash(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 char_u *p;
5349
Bram Moolenaarb4f6a462015-10-13 19:43:17 +02005350 if (path_with_url(fname))
5351 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 for (p = fname; *p != NUL; ++p)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 // The Big5 encoding can have '\' in the trail byte.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005354 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 ++p;
Bram Moolenaar13505972019-01-24 15:04:48 +01005356 else if (*p == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 *p = '/';
5358}
5359#endif
5360
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00005361/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00005362 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
5363 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
5364 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 * Used for autocommands and 'wildignore'.
5366 * Returns TRUE if there is a match, FALSE otherwise.
5367 */
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01005368 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005369match_file_pat(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 char_u *pattern, // pattern to match with
5371 regprog_T **prog, // pre-compiled regprog or NULL
5372 char_u *fname, // full path of file name
5373 char_u *sfname, // short file name or NULL
5374 char_u *tail, // tail of path
5375 int allow_dirs) // allow matching with dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 regmatch_T regmatch;
5378 int result = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005381 if (prog != NULL)
5382 regmatch.regprog = *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005384 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
5386 /*
5387 * Try for a match with the pattern with:
5388 * 1. the full file name, when the pattern has a '/'.
5389 * 2. the short file name, when the pattern has a '/'.
5390 * 3. the tail of the file name, when the pattern has no '/'.
5391 */
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005392 if (regmatch.regprog != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 && ((allow_dirs
5394 && (vim_regexec(&regmatch, fname, (colnr_T)0)
5395 || (sfname != NULL
5396 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
Bram Moolenaar49a6ed82015-01-07 14:43:39 +01005397 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 result = TRUE;
5399
Bram Moolenaardffa5b82014-11-19 16:38:07 +01005400 if (prog != NULL)
5401 *prog = regmatch.regprog;
5402 else
Bram Moolenaar473de612013-06-08 18:19:48 +02005403 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return result;
5405}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
5407#if defined(FEAT_WILDIGN) || defined(PROTO)
5408/*
5409 * Return TRUE if a file matches with a pattern in "list".
5410 * "list" is a comma-separated list of patterns, like 'wildignore'.
5411 * "sfname" is the short file name or NULL, "ffname" the long file name.
5412 */
5413 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414match_file_list(char_u *list, char_u *sfname, char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 char_u buf[100];
5417 char_u *tail;
5418 char_u *regpat;
5419 char allow_dirs;
5420 int match;
5421 char_u *p;
5422
5423 tail = gettail(sfname);
5424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005425 // try all patterns in 'wildignore'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 p = list;
5427 while (*p)
5428 {
5429 copy_option_part(&p, buf, 100, ",");
5430 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
5431 if (regpat == NULL)
5432 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005433 match = match_file_pat(regpat, NULL, ffname, sfname,
5434 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 vim_free(regpat);
5436 if (match)
5437 return TRUE;
5438 }
5439 return FALSE;
5440}
5441#endif
5442
5443/*
5444 * Convert the given pattern "pat" which has shell style wildcards in it, into
5445 * a regular expression, and return the result in allocated memory. If there
5446 * is a directory path separator to be matched, then TRUE is put in
5447 * allow_dirs, otherwise FALSE is put there -- webb.
5448 * Handle backslashes before special characters, like "\*" and "\ ".
5449 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * Returns NULL when out of memory.
5451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005453file_pat_to_reg_pat(
5454 char_u *pat,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005455 char_u *pat_end, // first char after pattern or NULL
5456 char *allow_dirs, // Result passed back out in here
5457 int no_bslash UNUSED) // Don't use a backward slash as pathsep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005459 int size = 2; // '^' at start, '$' at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 char_u *endp;
5461 char_u *reg_pat;
5462 char_u *p;
5463 int i;
5464 int nested = 0;
5465 int add_dollar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 if (allow_dirs != NULL)
5468 *allow_dirs = FALSE;
5469 if (pat_end == NULL)
5470 pat_end = pat + STRLEN(pat);
5471
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 for (p = pat; p < pat_end; p++)
5473 {
5474 switch (*p)
5475 {
5476 case '*':
5477 case '.':
5478 case ',':
5479 case '{':
5480 case '}':
5481 case '~':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005482 size += 2; // extra backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 break;
5484#ifdef BACKSLASH_IN_FILENAME
5485 case '\\':
5486 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005487 size += 4; // could become "[\/]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 break;
5489#endif
5490 default:
5491 size++;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005492 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 {
5494 ++p;
5495 ++size;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 break;
5498 }
5499 }
5500 reg_pat = alloc(size + 1);
5501 if (reg_pat == NULL)
5502 return NULL;
5503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506 if (pat[0] == '*')
5507 while (pat[0] == '*' && pat < pat_end - 1)
5508 pat++;
5509 else
5510 reg_pat[i++] = '^';
5511 endp = pat_end - 1;
Bram Moolenaar8fee8782015-08-11 18:45:48 +02005512 if (endp >= pat && *endp == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 while (endp - pat > 0 && *endp == '*')
5515 endp--;
5516 add_dollar = FALSE;
5517 }
5518 for (p = pat; *p && nested >= 0 && p <= endp; p++)
5519 {
5520 switch (*p)
5521 {
5522 case '*':
5523 reg_pat[i++] = '.';
5524 reg_pat[i++] = '*';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005525 while (p[1] == '*') // "**" matches like "*"
Bram Moolenaar02743632005-07-25 20:42:36 +00005526 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 break;
5528 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 case '~':
5530 reg_pat[i++] = '\\';
5531 reg_pat[i++] = *p;
5532 break;
5533 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 reg_pat[i++] = '.';
5535 break;
5536 case '\\':
5537 if (p[1] == NUL)
5538 break;
5539#ifdef BACKSLASH_IN_FILENAME
5540 if (!no_bslash)
5541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005542 // translate:
5543 // "\x" to "\\x" e.g., "dir\file"
5544 // "\*" to "\\.*" e.g., "dir\*.c"
5545 // "\?" to "\\." e.g., "dir\??.c"
5546 // "\+" to "\+" e.g., "fileX\+.c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
5548 && p[1] != '+')
5549 {
5550 reg_pat[i++] = '[';
5551 reg_pat[i++] = '\\';
5552 reg_pat[i++] = '/';
5553 reg_pat[i++] = ']';
5554 if (allow_dirs != NULL)
5555 *allow_dirs = TRUE;
5556 break;
5557 }
5558 }
5559#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005560 // Undo escaping from ExpandEscape():
5561 // foo\?bar -> foo?bar
5562 // foo\%bar -> foo%bar
5563 // foo\,bar -> foo,bar
5564 // foo\ bar -> foo bar
5565 // Don't unescape \, * and others that are also special in a
5566 // regexp.
5567 // An escaped { must be unescaped since we use magic not
5568 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 if (*++p == '?'
5570#ifdef BACKSLASH_IN_FILENAME
5571 && no_bslash
5572#endif
5573 )
5574 reg_pat[i++] = '?';
5575 else
Bram Moolenaarf4e11432013-07-03 16:53:03 +02005576 if (*p == ',' || *p == '%' || *p == '#'
Bram Moolenaar2288afe2015-08-11 16:20:05 +02005577 || vim_isspace(*p) || *p == '{' || *p == '}')
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02005578 reg_pat[i++] = *p;
Bram Moolenaara946afe2013-08-02 15:22:39 +02005579 else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
5580 {
5581 reg_pat[i++] = '\\';
5582 reg_pat[i++] = '{';
5583 p += 2;
5584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 else
5586 {
5587 if (allow_dirs != NULL && vim_ispathsep(*p)
5588#ifdef BACKSLASH_IN_FILENAME
5589 && (!no_bslash || *p != '\\')
5590#endif
5591 )
5592 *allow_dirs = TRUE;
5593 reg_pat[i++] = '\\';
5594 reg_pat[i++] = *p;
5595 }
5596 break;
5597#ifdef BACKSLASH_IN_FILENAME
5598 case '/':
5599 reg_pat[i++] = '[';
5600 reg_pat[i++] = '\\';
5601 reg_pat[i++] = '/';
5602 reg_pat[i++] = ']';
5603 if (allow_dirs != NULL)
5604 *allow_dirs = TRUE;
5605 break;
5606#endif
5607 case '{':
5608 reg_pat[i++] = '\\';
5609 reg_pat[i++] = '(';
5610 nested++;
5611 break;
5612 case '}':
5613 reg_pat[i++] = '\\';
5614 reg_pat[i++] = ')';
5615 --nested;
5616 break;
5617 case ',':
5618 if (nested)
5619 {
5620 reg_pat[i++] = '\\';
5621 reg_pat[i++] = '|';
5622 }
5623 else
5624 reg_pat[i++] = ',';
5625 break;
5626 default:
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005627 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 reg_pat[i++] = *p++;
Bram Moolenaar13505972019-01-24 15:04:48 +01005629 else if (allow_dirs != NULL && vim_ispathsep(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 *allow_dirs = TRUE;
5631 reg_pat[i++] = *p;
5632 break;
5633 }
5634 }
5635 if (add_dollar)
5636 reg_pat[i++] = '$';
5637 reg_pat[i] = NUL;
5638 if (nested != 0)
5639 {
5640 if (nested < 0)
Bram Moolenaar6d057012021-12-31 18:49:43 +00005641 emsg(_(e_missing_open_curly));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 else
Bram Moolenaar6d057012021-12-31 18:49:43 +00005643 emsg(_(e_missing_close_curly));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005644 VIM_CLEAR(reg_pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646 return reg_pat;
5647}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005648
5649#if defined(EINTR) || defined(PROTO)
5650/*
5651 * Version of read() that retries when interrupted by EINTR (possibly
5652 * by a SIGWINCH).
5653 */
5654 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655read_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005656{
5657 long ret;
5658
5659 for (;;)
5660 {
5661 ret = vim_read(fd, buf, bufsize);
5662 if (ret >= 0 || errno != EINTR)
5663 break;
5664 }
5665 return ret;
5666}
5667
5668/*
5669 * Version of write() that retries when interrupted by EINTR (possibly
5670 * by a SIGWINCH).
5671 */
5672 long
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673write_eintr(int fd, void *buf, size_t bufsize)
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005674{
5675 long ret = 0;
5676 long wlen;
5677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005678 // Repeat the write() so long it didn't fail, other than being interrupted
5679 // by a signal.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005680 while (ret < (long)bufsize)
5681 {
Bram Moolenaar9c263032010-12-17 18:06:06 +01005682 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005683 if (wlen < 0)
5684 {
5685 if (errno != EINTR)
5686 break;
5687 }
5688 else
5689 ret += wlen;
5690 }
5691 return ret;
5692}
5693#endif