xref: /vim-8.2.3635/src/fileio.c (revision 94688b8a)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
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 
14 #include "vim.h"
15 
16 #if defined(__TANDEM) || defined(__MINT__)
17 # include <limits.h>		/* for SSIZE_MAX */
18 #endif
19 
20 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
21 # include <utime.h>		/* for struct utimbuf */
22 #endif
23 
24 #define BUFSIZE		8192	/* size of normal write buffer */
25 #define SMBUFSIZE	256	/* size of emergency write buffer */
26 
27 /* Is there any system that doesn't have access()? */
28 #define USE_MCH_ACCESS
29 
30 static char_u *next_fenc(char_u **pp);
31 #ifdef FEAT_EVAL
32 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp);
33 #endif
34 #ifdef FEAT_VIMINFO
35 static void check_marks_read(void);
36 #endif
37 #ifdef FEAT_CRYPT
38 static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask);
39 #endif
40 static int set_rw_fname(char_u *fname, char_u *sfname);
41 static int msg_add_fileformat(int eol_type);
42 static void msg_add_eol(void);
43 static int check_mtime(buf_T *buf, stat_T *s);
44 static int time_differs(long t1, long t2);
45 
46 #define HAS_BW_FLAGS
47 #define FIO_LATIN1	0x01	/* convert Latin1 */
48 #define FIO_UTF8	0x02	/* convert UTF-8 */
49 #define FIO_UCS2	0x04	/* convert UCS-2 */
50 #define FIO_UCS4	0x08	/* convert UCS-4 */
51 #define FIO_UTF16	0x10	/* convert UTF-16 */
52 #ifdef WIN3264
53 # define FIO_CODEPAGE	0x20	/* convert MS-Windows codepage */
54 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16)	/* put codepage in top word */
55 # define FIO_GET_CP(x)	(((x)>>16) & 0xffff)	/* get codepage from top word */
56 #endif
57 #ifdef MACOS_CONVERT
58 # define FIO_MACROMAN	0x20	/* convert MacRoman */
59 #endif
60 #define FIO_ENDIAN_L	0x80	/* little endian */
61 #define FIO_ENCRYPTED	0x1000	/* encrypt written bytes */
62 #define FIO_NOCONVERT	0x2000	/* skip encoding conversion */
63 #define FIO_UCSBOM	0x4000	/* check for BOM at start of file */
64 #define FIO_ALL	-1	/* allow all formats */
65 
66 /* When converting, a read() or write() may leave some bytes to be converted
67  * for the next call.  The value is guessed... */
68 #define CONV_RESTLEN 30
69 
70 /* We have to guess how much a sequence of bytes may expand when converting
71  * with iconv() to be able to allocate a buffer. */
72 #define ICONV_MULT 8
73 
74 /*
75  * Structure to pass arguments from buf_write() to buf_write_bytes().
76  */
77 struct bw_info
78 {
79     int		bw_fd;		/* file descriptor */
80     char_u	*bw_buf;	/* buffer with data to be written */
81     int		bw_len;		/* length of data */
82 #ifdef HAS_BW_FLAGS
83     int		bw_flags;	/* FIO_ flags */
84 #endif
85 #ifdef FEAT_CRYPT
86     buf_T	*bw_buffer;	/* buffer being written */
87 #endif
88     char_u	bw_rest[CONV_RESTLEN]; /* not converted bytes */
89     int		bw_restlen;	/* nr of bytes in bw_rest[] */
90     int		bw_first;	/* first write call */
91     char_u	*bw_conv_buf;	/* buffer for writing converted chars */
92     int		bw_conv_buflen; /* size of bw_conv_buf */
93     int		bw_conv_error;	/* set for conversion error */
94     linenr_T	bw_conv_error_lnum;  /* first line with error or zero */
95     linenr_T	bw_start_lnum;  /* line number at start of buffer */
96 #ifdef USE_ICONV
97     iconv_t	bw_iconv_fd;	/* descriptor for iconv() or -1 */
98 #endif
99 };
100 
101 static int  buf_write_bytes(struct bw_info *ip);
102 
103 static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp);
104 static int ucs2bytes(unsigned c, char_u **pp, int flags);
105 static int need_conversion(char_u *fenc);
106 static int get_fio_flags(char_u *ptr);
107 static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
108 static int make_bom(char_u *buf, char_u *name);
109 #ifdef WIN3264
110 static int get_win_fio_flags(char_u *ptr);
111 #endif
112 #ifdef MACOS_CONVERT
113 static int get_mac_fio_flags(char_u *ptr);
114 #endif
115 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
116 
117     void
118 filemess(
119     buf_T	*buf,
120     char_u	*name,
121     char_u	*s,
122     int		attr)
123 {
124     int		msg_scroll_save;
125 
126     if (msg_silent != 0)
127 	return;
128     msg_add_fname(buf, name);	    /* put file name in IObuff with quotes */
129     /* If it's extremely long, truncate it. */
130     if (STRLEN(IObuff) > IOSIZE - 80)
131 	IObuff[IOSIZE - 80] = NUL;
132     STRCAT(IObuff, s);
133     /*
134      * For the first message may have to start a new line.
135      * For further ones overwrite the previous one, reset msg_scroll before
136      * calling filemess().
137      */
138     msg_scroll_save = msg_scroll;
139     if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
140 	msg_scroll = FALSE;
141     if (!msg_scroll)	/* wait a bit when overwriting an error msg */
142 	check_for_delay(FALSE);
143     msg_start();
144     msg_scroll = msg_scroll_save;
145     msg_scrolled_ign = TRUE;
146     /* may truncate the message to avoid a hit-return prompt */
147     msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
148     msg_clr_eos();
149     out_flush();
150     msg_scrolled_ign = FALSE;
151 }
152 
153 /*
154  * Read lines from file "fname" into the buffer after line "from".
155  *
156  * 1. We allocate blocks with lalloc, as big as possible.
157  * 2. Each block is filled with characters from the file with a single read().
158  * 3. The lines are inserted in the buffer with ml_append().
159  *
160  * (caller must check that fname != NULL, unless READ_STDIN is used)
161  *
162  * "lines_to_skip" is the number of lines that must be skipped
163  * "lines_to_read" is the number of lines that are appended
164  * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
165  *
166  * flags:
167  * READ_NEW	starting to edit a new buffer
168  * READ_FILTER	reading filter output
169  * READ_STDIN	read from stdin instead of a file
170  * READ_BUFFER	read from curbuf instead of a file (converting after reading
171  *		stdin)
172  * READ_DUMMY	read into a dummy buffer (to check if file contents changed)
173  * READ_KEEP_UNDO  don't clear undo info or read it from a file
174  * READ_FIFO	read from fifo/socket instead of a file
175  *
176  * return FAIL for failure, NOTDONE for directory (failure), or OK
177  */
178     int
179 readfile(
180     char_u	*fname,
181     char_u	*sfname,
182     linenr_T	from,
183     linenr_T	lines_to_skip,
184     linenr_T	lines_to_read,
185     exarg_T	*eap,			/* can be NULL! */
186     int		flags)
187 {
188     int		fd = 0;
189     int		newfile = (flags & READ_NEW);
190     int		check_readonly;
191     int		filtering = (flags & READ_FILTER);
192     int		read_stdin = (flags & READ_STDIN);
193     int		read_buffer = (flags & READ_BUFFER);
194     int		read_fifo = (flags & READ_FIFO);
195     int		set_options = newfile || read_buffer
196 					   || (eap != NULL && eap->read_edit);
197     linenr_T	read_buf_lnum = 1;	/* next line to read from curbuf */
198     colnr_T	read_buf_col = 0;	/* next char to read from this line */
199     char_u	c;
200     linenr_T	lnum = from;
201     char_u	*ptr = NULL;		/* pointer into read buffer */
202     char_u	*buffer = NULL;		/* read buffer */
203     char_u	*new_buffer = NULL;	/* init to shut up gcc */
204     char_u	*line_start = NULL;	/* init to shut up gcc */
205     int		wasempty;		/* buffer was empty before reading */
206     colnr_T	len;
207     long	size = 0;
208     char_u	*p;
209     off_T	filesize = 0;
210     int		skip_read = FALSE;
211 #ifdef FEAT_CRYPT
212     char_u	*cryptkey = NULL;
213     int		did_ask_for_key = FALSE;
214 #endif
215 #ifdef FEAT_PERSISTENT_UNDO
216     context_sha256_T sha_ctx;
217     int		read_undo_file = FALSE;
218 #endif
219     int		split = 0;		/* number of split lines */
220 #define UNKNOWN	 0x0fffffff		/* file size is unknown */
221     linenr_T	linecnt;
222     int		error = FALSE;		/* errors encountered */
223     int		ff_error = EOL_UNKNOWN; /* file format with errors */
224     long	linerest = 0;		/* remaining chars in line */
225 #ifdef UNIX
226     int		perm = 0;
227     int		swap_mode = -1;		/* protection bits for swap file */
228 #else
229     int		perm;
230 #endif
231     int		fileformat = 0;		/* end-of-line format */
232     int		keep_fileformat = FALSE;
233     stat_T	st;
234     int		file_readonly;
235     linenr_T	skip_count = 0;
236     linenr_T	read_count = 0;
237     int		msg_save = msg_scroll;
238     linenr_T	read_no_eol_lnum = 0;   /* non-zero lnum when last line of
239 					 * last read was missing the eol */
240     int		try_mac;
241     int		try_dos;
242     int		try_unix;
243     int		file_rewind = FALSE;
244     int		can_retry;
245     linenr_T	conv_error = 0;		/* line nr with conversion error */
246     linenr_T	illegal_byte = 0;	/* line nr with illegal byte */
247     int		keep_dest_enc = FALSE;	/* don't retry when char doesn't fit
248 					   in destination encoding */
249     int		bad_char_behavior = BAD_REPLACE;
250 					/* BAD_KEEP, BAD_DROP or character to
251 					 * replace with */
252     char_u	*tmpname = NULL;	/* name of 'charconvert' output file */
253     int		fio_flags = 0;
254     char_u	*fenc;			/* fileencoding to use */
255     int		fenc_alloced;		/* fenc_next is in allocated memory */
256     char_u	*fenc_next = NULL;	/* next item in 'fencs' or NULL */
257     int		advance_fenc = FALSE;
258     long	real_size = 0;
259 #ifdef USE_ICONV
260     iconv_t	iconv_fd = (iconv_t)-1;	/* descriptor for iconv() or -1 */
261 # ifdef FEAT_EVAL
262     int		did_iconv = FALSE;	/* TRUE when iconv() failed and trying
263 					   'charconvert' next */
264 # endif
265 #endif
266     int		converted = FALSE;	/* TRUE if conversion done */
267     int		notconverted = FALSE;	/* TRUE if conversion wanted but it
268 					   wasn't possible */
269     char_u	conv_rest[CONV_RESTLEN];
270     int		conv_restlen = 0;	/* nr of bytes in conv_rest[] */
271     buf_T	*old_curbuf;
272     char_u	*old_b_ffname;
273     char_u	*old_b_fname;
274     int		using_b_ffname;
275     int		using_b_fname;
276 
277     au_did_filetype = FALSE; /* reset before triggering any autocommands */
278 
279     curbuf->b_no_eol_lnum = 0;	/* in case it was set by the previous read */
280 
281     /*
282      * If there is no file name yet, use the one for the read file.
283      * BF_NOTEDITED is set to reflect this.
284      * Don't do this for a read from a filter.
285      * Only do this when 'cpoptions' contains the 'f' flag.
286      */
287     if (curbuf->b_ffname == NULL
288 	    && !filtering
289 	    && fname != NULL
290 	    && vim_strchr(p_cpo, CPO_FNAMER) != NULL
291 	    && !(flags & READ_DUMMY))
292     {
293 	if (set_rw_fname(fname, sfname) == FAIL)
294 	    return FAIL;
295     }
296 
297     /* Remember the initial values of curbuf, curbuf->b_ffname and
298      * curbuf->b_fname to detect whether they are altered as a result of
299      * executing nasty autocommands.  Also check if "fname" and "sfname"
300      * point to one of these values. */
301     old_curbuf = curbuf;
302     old_b_ffname = curbuf->b_ffname;
303     old_b_fname = curbuf->b_fname;
304     using_b_ffname = (fname == curbuf->b_ffname)
305 					      || (sfname == curbuf->b_ffname);
306     using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
307 
308     /* After reading a file the cursor line changes but we don't want to
309      * display the line. */
310     ex_no_reprint = TRUE;
311 
312     /* don't display the file info for another buffer now */
313     need_fileinfo = FALSE;
314 
315     /*
316      * For Unix: Use the short file name whenever possible.
317      * Avoids problems with networks and when directory names are changed.
318      * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
319      * another directory, which we don't detect.
320      */
321     if (sfname == NULL)
322 	sfname = fname;
323 #if defined(UNIX)
324     fname = sfname;
325 #endif
326 
327     /*
328      * The BufReadCmd and FileReadCmd events intercept the reading process by
329      * executing the associated commands instead.
330      */
331     if (!filtering && !read_stdin && !read_buffer)
332     {
333 	pos_T	    pos;
334 
335 	pos = curbuf->b_op_start;
336 
337 	/* Set '[ mark to the line above where the lines go (line 1 if zero). */
338 	curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
339 	curbuf->b_op_start.col = 0;
340 
341 	if (newfile)
342 	{
343 	    if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
344 							  FALSE, curbuf, eap))
345 #ifdef FEAT_EVAL
346 		return aborting() ? FAIL : OK;
347 #else
348 		return OK;
349 #endif
350 	}
351 	else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
352 							    FALSE, NULL, eap))
353 #ifdef FEAT_EVAL
354 	    return aborting() ? FAIL : OK;
355 #else
356 	    return OK;
357 #endif
358 
359 	curbuf->b_op_start = pos;
360     }
361 
362     if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
363 	msg_scroll = FALSE;	/* overwrite previous file message */
364     else
365 	msg_scroll = TRUE;	/* don't overwrite previous file message */
366 
367     /*
368      * If the name ends in a path separator, we can't open it.  Check here,
369      * because reading the file may actually work, but then creating the swap
370      * file may destroy it!  Reported on MS-DOS and Win 95.
371      * If the name is too long we might crash further on, quit here.
372      */
373     if (fname != NULL && *fname != NUL)
374     {
375 	p = fname + STRLEN(fname);
376 	if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
377 	{
378 	    filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
379 	    msg_end();
380 	    msg_scroll = msg_save;
381 	    return FAIL;
382 	}
383     }
384 
385     if (!read_stdin && !read_buffer && !read_fifo)
386     {
387 #ifdef UNIX
388 	/*
389 	 * On Unix it is possible to read a directory, so we have to
390 	 * check for it before the mch_open().
391 	 */
392 	perm = mch_getperm(fname);
393 	if (perm >= 0 && !S_ISREG(perm)		    /* not a regular file ... */
394 		      && !S_ISFIFO(perm)	    /* ... or fifo */
395 		      && !S_ISSOCK(perm)	    /* ... or socket */
396 # ifdef OPEN_CHR_FILES
397 		      && !(S_ISCHR(perm) && is_dev_fd_file(fname))
398 			/* ... or a character special file named /dev/fd/<n> */
399 # endif
400 						)
401 	{
402 	    int retval = FAIL;
403 
404 	    if (S_ISDIR(perm))
405 	    {
406 		filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
407 		retval = NOTDONE;
408 	    }
409 	    else
410 		filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
411 	    msg_end();
412 	    msg_scroll = msg_save;
413 	    return retval;
414 	}
415 #endif
416 #if defined(MSWIN)
417 	/*
418 	 * MS-Windows allows opening a device, but we will probably get stuck
419 	 * trying to read it.
420 	 */
421 	if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
422 	{
423 	    filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
424 	    msg_end();
425 	    msg_scroll = msg_save;
426 	    return FAIL;
427 	}
428 #endif
429     }
430 
431     /* Set default or forced 'fileformat' and 'binary'. */
432     set_file_options(set_options, eap);
433 
434     /*
435      * When opening a new file we take the readonly flag from the file.
436      * Default is r/w, can be set to r/o below.
437      * Don't reset it when in readonly mode
438      * Only set/reset b_p_ro when BF_CHECK_RO is set.
439      */
440     check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
441     if (check_readonly && !readonlymode)
442 	curbuf->b_p_ro = FALSE;
443 
444     if (newfile && !read_stdin && !read_buffer && !read_fifo)
445     {
446 	/* Remember time of file. */
447 	if (mch_stat((char *)fname, &st) >= 0)
448 	{
449 	    buf_store_time(curbuf, &st, fname);
450 	    curbuf->b_mtime_read = curbuf->b_mtime;
451 #ifdef UNIX
452 	    /*
453 	     * Use the protection bits of the original file for the swap file.
454 	     * This makes it possible for others to read the name of the
455 	     * edited file from the swapfile, but only if they can read the
456 	     * edited file.
457 	     * Remove the "write" and "execute" bits for group and others
458 	     * (they must not write the swapfile).
459 	     * Add the "read" and "write" bits for the user, otherwise we may
460 	     * not be able to write to the file ourselves.
461 	     * Setting the bits is done below, after creating the swap file.
462 	     */
463 	    swap_mode = (st.st_mode & 0644) | 0600;
464 #endif
465 #ifdef FEAT_CW_EDITOR
466 	    /* Get the FSSpec on MacOS
467 	     * TODO: Update it properly when the buffer name changes
468 	     */
469 	    (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
470 #endif
471 #ifdef VMS
472 	    curbuf->b_fab_rfm = st.st_fab_rfm;
473 	    curbuf->b_fab_rat = st.st_fab_rat;
474 	    curbuf->b_fab_mrs = st.st_fab_mrs;
475 #endif
476 	}
477 	else
478 	{
479 	    curbuf->b_mtime = 0;
480 	    curbuf->b_mtime_read = 0;
481 	    curbuf->b_orig_size = 0;
482 	    curbuf->b_orig_mode = 0;
483 	}
484 
485 	/* Reset the "new file" flag.  It will be set again below when the
486 	 * file doesn't exist. */
487 	curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
488     }
489 
490 /*
491  * for UNIX: check readonly with perm and mch_access()
492  * for Amiga: check readonly by trying to open the file for writing
493  */
494     file_readonly = FALSE;
495     if (read_stdin)
496     {
497 #if defined(MSWIN)
498 	/* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */
499 	setmode(0, O_BINARY);
500 #endif
501     }
502     else if (!read_buffer)
503     {
504 #ifdef USE_MCH_ACCESS
505 	if (
506 # ifdef UNIX
507 	    !(perm & 0222) ||
508 # endif
509 				mch_access((char *)fname, W_OK))
510 	    file_readonly = TRUE;
511 	fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
512 #else
513 	if (!newfile
514 		|| readonlymode
515 		|| (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
516 	{
517 	    file_readonly = TRUE;
518 	    /* try to open ro */
519 	    fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
520 	}
521 #endif
522     }
523 
524     if (fd < 0)			    /* cannot open at all */
525     {
526 #ifndef UNIX
527 	int	isdir_f;
528 #endif
529 	msg_scroll = msg_save;
530 #ifndef UNIX
531 	/*
532 	 * On Amiga we can't open a directory, check here.
533 	 */
534 	isdir_f = (mch_isdir(fname));
535 	perm = mch_getperm(fname);  /* check if the file exists */
536 	if (isdir_f)
537 	{
538 	    filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
539 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
540 	}
541 	else
542 #endif
543 	    if (newfile)
544 	    {
545 		if (perm < 0
546 #ifdef ENOENT
547 			&& errno == ENOENT
548 #endif
549 		   )
550 		{
551 		    /*
552 		     * Set the 'new-file' flag, so that when the file has
553 		     * been created by someone else, a ":w" will complain.
554 		     */
555 		    curbuf->b_flags |= BF_NEW;
556 
557 		    /* Create a swap file now, so that other Vims are warned
558 		     * that we are editing this file.  Don't do this for a
559 		     * "nofile" or "nowrite" buffer type. */
560 #ifdef FEAT_QUICKFIX
561 		    if (!bt_dontwrite(curbuf))
562 #endif
563 		    {
564 			check_need_swap(newfile);
565 			/* SwapExists autocommand may mess things up */
566 			if (curbuf != old_curbuf
567 				|| (using_b_ffname
568 					&& (old_b_ffname != curbuf->b_ffname))
569 				|| (using_b_fname
570 					 && (old_b_fname != curbuf->b_fname)))
571 			{
572 			    emsg(_(e_auchangedbuf));
573 			    return FAIL;
574 			}
575 		    }
576 		    if (dir_of_file_exists(fname))
577 			filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
578 		    else
579 			filemess(curbuf, sfname,
580 					   (char_u *)_("[New DIRECTORY]"), 0);
581 #ifdef FEAT_VIMINFO
582 		    /* Even though this is a new file, it might have been
583 		     * edited before and deleted.  Get the old marks. */
584 		    check_marks_read();
585 #endif
586 		    /* Set forced 'fileencoding'.  */
587 		    if (eap != NULL)
588 			set_forced_fenc(eap);
589 		    apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
590 							  FALSE, curbuf, eap);
591 		    /* remember the current fileformat */
592 		    save_file_ff(curbuf);
593 
594 #if defined(FEAT_EVAL)
595 		    if (aborting())   /* autocmds may abort script processing */
596 			return FAIL;
597 #endif
598 		    return OK;	    /* a new file is not an error */
599 		}
600 		else
601 		{
602 		    filemess(curbuf, sfname, (char_u *)(
603 # ifdef EFBIG
604 			    (errno == EFBIG) ? _("[File too big]") :
605 # endif
606 # ifdef EOVERFLOW
607 			    (errno == EOVERFLOW) ? _("[File too big]") :
608 # endif
609 						_("[Permission Denied]")), 0);
610 		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
611 		}
612 	    }
613 
614 	return FAIL;
615     }
616 
617     /*
618      * Only set the 'ro' flag for readonly files the first time they are
619      * loaded.	Help files always get readonly mode
620      */
621     if ((check_readonly && file_readonly) || curbuf->b_help)
622 	curbuf->b_p_ro = TRUE;
623 
624     if (set_options)
625     {
626 	/* Don't change 'eol' if reading from buffer as it will already be
627 	 * correctly set when reading stdin. */
628 	if (!read_buffer)
629 	{
630 	    curbuf->b_p_eol = TRUE;
631 	    curbuf->b_start_eol = TRUE;
632 	}
633 	curbuf->b_p_bomb = FALSE;
634 	curbuf->b_start_bomb = FALSE;
635     }
636 
637     /* Create a swap file now, so that other Vims are warned that we are
638      * editing this file.
639      * Don't do this for a "nofile" or "nowrite" buffer type. */
640 #ifdef FEAT_QUICKFIX
641     if (!bt_dontwrite(curbuf))
642 #endif
643     {
644 	check_need_swap(newfile);
645 	if (!read_stdin && (curbuf != old_curbuf
646 		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
647 		|| (using_b_fname && (old_b_fname != curbuf->b_fname))))
648 	{
649 	    emsg(_(e_auchangedbuf));
650 	    if (!read_buffer)
651 		close(fd);
652 	    return FAIL;
653 	}
654 #ifdef UNIX
655 	/* Set swap file protection bits after creating it. */
656 	if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
657 			  && curbuf->b_ml.ml_mfp->mf_fname != NULL)
658 	{
659 	    char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname;
660 
661 	    /*
662 	     * If the group-read bit is set but not the world-read bit, then
663 	     * the group must be equal to the group of the original file.  If
664 	     * we can't make that happen then reset the group-read bit.  This
665 	     * avoids making the swap file readable to more users when the
666 	     * primary group of the user is too permissive.
667 	     */
668 	    if ((swap_mode & 044) == 040)
669 	    {
670 		stat_T	swap_st;
671 
672 		if (mch_stat((char *)swap_fname, &swap_st) >= 0
673 			&& st.st_gid != swap_st.st_gid
674 # ifdef HAVE_FCHOWN
675 			&& fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid)
676 									  == -1
677 # endif
678 		   )
679 		    swap_mode &= 0600;
680 	    }
681 
682 	    (void)mch_setperm(swap_fname, (long)swap_mode);
683 	}
684 #endif
685     }
686 
687 #if defined(HAS_SWAP_EXISTS_ACTION)
688     /* If "Quit" selected at ATTENTION dialog, don't load the file */
689     if (swap_exists_action == SEA_QUIT)
690     {
691 	if (!read_buffer && !read_stdin)
692 	    close(fd);
693 	return FAIL;
694     }
695 #endif
696 
697     ++no_wait_return;	    /* don't wait for return yet */
698 
699     /*
700      * Set '[ mark to the line above where the lines go (line 1 if zero).
701      */
702     curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
703     curbuf->b_op_start.col = 0;
704 
705     try_mac = (vim_strchr(p_ffs, 'm') != NULL);
706     try_dos = (vim_strchr(p_ffs, 'd') != NULL);
707     try_unix = (vim_strchr(p_ffs, 'x') != NULL);
708 
709     if (!read_buffer)
710     {
711 	int	m = msg_scroll;
712 	int	n = msg_scrolled;
713 
714 	/*
715 	 * The file must be closed again, the autocommands may want to change
716 	 * the file before reading it.
717 	 */
718 	if (!read_stdin)
719 	    close(fd);		/* ignore errors */
720 
721 	/*
722 	 * The output from the autocommands should not overwrite anything and
723 	 * should not be overwritten: Set msg_scroll, restore its value if no
724 	 * output was done.
725 	 */
726 	msg_scroll = TRUE;
727 	if (filtering)
728 	    apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
729 							  FALSE, curbuf, eap);
730 	else if (read_stdin)
731 	    apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
732 							  FALSE, curbuf, eap);
733 	else if (newfile)
734 	    apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
735 							  FALSE, curbuf, eap);
736 	else
737 	    apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
738 							    FALSE, NULL, eap);
739 	/* autocommands may have changed it */
740 	try_mac = (vim_strchr(p_ffs, 'm') != NULL);
741 	try_dos = (vim_strchr(p_ffs, 'd') != NULL);
742 	try_unix = (vim_strchr(p_ffs, 'x') != NULL);
743 
744 	if (msg_scrolled == n)
745 	    msg_scroll = m;
746 
747 #ifdef FEAT_EVAL
748 	if (aborting())	    /* autocmds may abort script processing */
749 	{
750 	    --no_wait_return;
751 	    msg_scroll = msg_save;
752 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
753 	    return FAIL;
754 	}
755 #endif
756 	/*
757 	 * Don't allow the autocommands to change the current buffer.
758 	 * Try to re-open the file.
759 	 *
760 	 * Don't allow the autocommands to change the buffer name either
761 	 * (cd for example) if it invalidates fname or sfname.
762 	 */
763 	if (!read_stdin && (curbuf != old_curbuf
764 		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
765 		|| (using_b_fname && (old_b_fname != curbuf->b_fname))
766 		|| (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
767 	{
768 	    --no_wait_return;
769 	    msg_scroll = msg_save;
770 	    if (fd < 0)
771 		emsg(_("E200: *ReadPre autocommands made the file unreadable"));
772 	    else
773 		emsg(_("E201: *ReadPre autocommands must not change current buffer"));
774 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
775 	    return FAIL;
776 	}
777     }
778 
779     /* Autocommands may add lines to the file, need to check if it is empty */
780     wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
781 
782     if (!recoverymode && !filtering && !(flags & READ_DUMMY))
783     {
784 	/*
785 	 * Show the user that we are busy reading the input.  Sometimes this
786 	 * may take a while.  When reading from stdin another program may
787 	 * still be running, don't move the cursor to the last line, unless
788 	 * always using the GUI.
789 	 */
790 	if (read_stdin)
791 	{
792 	    if (!is_not_a_term())
793 	    {
794 #ifndef ALWAYS_USE_GUI
795 		mch_msg(_("Vim: Reading from stdin...\n"));
796 #endif
797 #ifdef FEAT_GUI
798 		/* Also write a message in the GUI window, if there is one. */
799 		if (gui.in_use && !gui.dying && !gui.starting)
800 		{
801 		    p = (char_u *)_("Reading from stdin...");
802 		    gui_write(p, (int)STRLEN(p));
803 		}
804 #endif
805 	    }
806 	}
807 	else if (!read_buffer)
808 	    filemess(curbuf, sfname, (char_u *)"", 0);
809     }
810 
811     msg_scroll = FALSE;			/* overwrite the file message */
812 
813     /*
814      * Set linecnt now, before the "retry" caused by a wrong guess for
815      * fileformat, and after the autocommands, which may change them.
816      */
817     linecnt = curbuf->b_ml.ml_line_count;
818 
819     /* "++bad=" argument. */
820     if (eap != NULL && eap->bad_char != 0)
821     {
822 	bad_char_behavior = eap->bad_char;
823 	if (set_options)
824 	    curbuf->b_bad_char = eap->bad_char;
825     }
826     else
827 	curbuf->b_bad_char = 0;
828 
829     /*
830      * Decide which 'encoding' to use or use first.
831      */
832     if (eap != NULL && eap->force_enc != 0)
833     {
834 	fenc = enc_canonize(eap->cmd + eap->force_enc);
835 	fenc_alloced = TRUE;
836 	keep_dest_enc = TRUE;
837     }
838     else if (curbuf->b_p_bin)
839     {
840 	fenc = (char_u *)"";		/* binary: don't convert */
841 	fenc_alloced = FALSE;
842     }
843     else if (curbuf->b_help)
844     {
845 	char_u	    firstline[80];
846 	int	    fc;
847 
848 	/* Help files are either utf-8 or latin1.  Try utf-8 first, if this
849 	 * fails it must be latin1.
850 	 * Always do this when 'encoding' is "utf-8".  Otherwise only do
851 	 * this when needed to avoid [converted] remarks all the time.
852 	 * It is needed when the first line contains non-ASCII characters.
853 	 * That is only in *.??x files. */
854 	fenc = (char_u *)"latin1";
855 	c = enc_utf8;
856 	if (!c && !read_stdin)
857 	{
858 	    fc = fname[STRLEN(fname) - 1];
859 	    if (TOLOWER_ASC(fc) == 'x')
860 	    {
861 		/* Read the first line (and a bit more).  Immediately rewind to
862 		 * the start of the file.  If the read() fails "len" is -1. */
863 		len = read_eintr(fd, firstline, 80);
864 		vim_lseek(fd, (off_T)0L, SEEK_SET);
865 		for (p = firstline; p < firstline + len; ++p)
866 		    if (*p >= 0x80)
867 		    {
868 			c = TRUE;
869 			break;
870 		    }
871 	    }
872 	}
873 
874 	if (c)
875 	{
876 	    fenc_next = fenc;
877 	    fenc = (char_u *)"utf-8";
878 
879 	    /* When the file is utf-8 but a character doesn't fit in
880 	     * 'encoding' don't retry.  In help text editing utf-8 bytes
881 	     * doesn't make sense. */
882 	    if (!enc_utf8)
883 		keep_dest_enc = TRUE;
884 	}
885 	fenc_alloced = FALSE;
886     }
887     else if (*p_fencs == NUL)
888     {
889 	fenc = curbuf->b_p_fenc;	/* use format from buffer */
890 	fenc_alloced = FALSE;
891     }
892     else
893     {
894 	fenc_next = p_fencs;		/* try items in 'fileencodings' */
895 	fenc = next_fenc(&fenc_next);
896 	fenc_alloced = TRUE;
897     }
898 
899     /*
900      * Jump back here to retry reading the file in different ways.
901      * Reasons to retry:
902      * - encoding conversion failed: try another one from "fenc_next"
903      * - BOM detected and fenc was set, need to setup conversion
904      * - "fileformat" check failed: try another
905      *
906      * Variables set for special retry actions:
907      * "file_rewind"	Rewind the file to start reading it again.
908      * "advance_fenc"	Advance "fenc" using "fenc_next".
909      * "skip_read"	Re-use already read bytes (BOM detected).
910      * "did_iconv"	iconv() conversion failed, try 'charconvert'.
911      * "keep_fileformat" Don't reset "fileformat".
912      *
913      * Other status indicators:
914      * "tmpname"	When != NULL did conversion with 'charconvert'.
915      *			Output file has to be deleted afterwards.
916      * "iconv_fd"	When != -1 did conversion with iconv().
917      */
918 retry:
919 
920     if (file_rewind)
921     {
922 	if (read_buffer)
923 	{
924 	    read_buf_lnum = 1;
925 	    read_buf_col = 0;
926 	}
927 	else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0)
928 	{
929 	    /* Can't rewind the file, give up. */
930 	    error = TRUE;
931 	    goto failed;
932 	}
933 	/* Delete the previously read lines. */
934 	while (lnum > from)
935 	    ml_delete(lnum--, FALSE);
936 	file_rewind = FALSE;
937 	if (set_options)
938 	{
939 	    curbuf->b_p_bomb = FALSE;
940 	    curbuf->b_start_bomb = FALSE;
941 	}
942 	conv_error = 0;
943     }
944 
945     /*
946      * When retrying with another "fenc" and the first time "fileformat"
947      * will be reset.
948      */
949     if (keep_fileformat)
950 	keep_fileformat = FALSE;
951     else
952     {
953 	if (eap != NULL && eap->force_ff != 0)
954 	{
955 	    fileformat = get_fileformat_force(curbuf, eap);
956 	    try_unix = try_dos = try_mac = FALSE;
957 	}
958 	else if (curbuf->b_p_bin)
959 	    fileformat = EOL_UNIX;		/* binary: use Unix format */
960 	else if (*p_ffs == NUL)
961 	    fileformat = get_fileformat(curbuf);/* use format from buffer */
962 	else
963 	    fileformat = EOL_UNKNOWN;		/* detect from file */
964     }
965 
966 #ifdef USE_ICONV
967     if (iconv_fd != (iconv_t)-1)
968     {
969 	/* aborted conversion with iconv(), close the descriptor */
970 	iconv_close(iconv_fd);
971 	iconv_fd = (iconv_t)-1;
972     }
973 #endif
974 
975     if (advance_fenc)
976     {
977 	/*
978 	 * Try the next entry in 'fileencodings'.
979 	 */
980 	advance_fenc = FALSE;
981 
982 	if (eap != NULL && eap->force_enc != 0)
983 	{
984 	    /* Conversion given with "++cc=" wasn't possible, read
985 	     * without conversion. */
986 	    notconverted = TRUE;
987 	    conv_error = 0;
988 	    if (fenc_alloced)
989 		vim_free(fenc);
990 	    fenc = (char_u *)"";
991 	    fenc_alloced = FALSE;
992 	}
993 	else
994 	{
995 	    if (fenc_alloced)
996 		vim_free(fenc);
997 	    if (fenc_next != NULL)
998 	    {
999 		fenc = next_fenc(&fenc_next);
1000 		fenc_alloced = (fenc_next != NULL);
1001 	    }
1002 	    else
1003 	    {
1004 		fenc = (char_u *)"";
1005 		fenc_alloced = FALSE;
1006 	    }
1007 	}
1008 	if (tmpname != NULL)
1009 	{
1010 	    mch_remove(tmpname);		/* delete converted file */
1011 	    VIM_CLEAR(tmpname);
1012 	}
1013     }
1014 
1015     /*
1016      * Conversion may be required when the encoding of the file is different
1017      * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
1018      */
1019     fio_flags = 0;
1020     converted = need_conversion(fenc);
1021     if (converted)
1022     {
1023 
1024 	/* "ucs-bom" means we need to check the first bytes of the file
1025 	 * for a BOM. */
1026 	if (STRCMP(fenc, ENC_UCSBOM) == 0)
1027 	    fio_flags = FIO_UCSBOM;
1028 
1029 	/*
1030 	 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1031 	 * done.  This is handled below after read().  Prepare the
1032 	 * fio_flags to avoid having to parse the string each time.
1033 	 * Also check for Unicode to Latin1 conversion, because iconv()
1034 	 * appears not to handle this correctly.  This works just like
1035 	 * conversion to UTF-8 except how the resulting character is put in
1036 	 * the buffer.
1037 	 */
1038 	else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1039 	    fio_flags = get_fio_flags(fenc);
1040 
1041 #ifdef WIN3264
1042 	/*
1043 	 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1044 	 * is handled with MultiByteToWideChar().
1045 	 */
1046 	if (fio_flags == 0)
1047 	    fio_flags = get_win_fio_flags(fenc);
1048 #endif
1049 
1050 #ifdef MACOS_CONVERT
1051 	/* Conversion from Apple MacRoman to latin1 or UTF-8 */
1052 	if (fio_flags == 0)
1053 	    fio_flags = get_mac_fio_flags(fenc);
1054 #endif
1055 
1056 #ifdef USE_ICONV
1057 	/*
1058 	 * Try using iconv() if we can't convert internally.
1059 	 */
1060 	if (fio_flags == 0
1061 # ifdef FEAT_EVAL
1062 		&& !did_iconv
1063 # endif
1064 		)
1065 	    iconv_fd = (iconv_t)my_iconv_open(
1066 				  enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
1067 #endif
1068 
1069 #ifdef FEAT_EVAL
1070 	/*
1071 	 * Use the 'charconvert' expression when conversion is required
1072 	 * and we can't do it internally or with iconv().
1073 	 */
1074 	if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
1075 						    && !read_fifo
1076 # ifdef USE_ICONV
1077 						    && iconv_fd == (iconv_t)-1
1078 # endif
1079 		)
1080 	{
1081 # ifdef USE_ICONV
1082 	    did_iconv = FALSE;
1083 # endif
1084 	    /* Skip conversion when it's already done (retry for wrong
1085 	     * "fileformat"). */
1086 	    if (tmpname == NULL)
1087 	    {
1088 		tmpname = readfile_charconvert(fname, fenc, &fd);
1089 		if (tmpname == NULL)
1090 		{
1091 		    /* Conversion failed.  Try another one. */
1092 		    advance_fenc = TRUE;
1093 		    if (fd < 0)
1094 		    {
1095 			/* Re-opening the original file failed! */
1096 			emsg(_("E202: Conversion made file unreadable!"));
1097 			error = TRUE;
1098 			goto failed;
1099 		    }
1100 		    goto retry;
1101 		}
1102 	    }
1103 	}
1104 	else
1105 #endif
1106 	{
1107 	    if (fio_flags == 0
1108 #ifdef USE_ICONV
1109 		    && iconv_fd == (iconv_t)-1
1110 #endif
1111 	       )
1112 	    {
1113 		/* Conversion wanted but we can't.
1114 		 * Try the next conversion in 'fileencodings' */
1115 		advance_fenc = TRUE;
1116 		goto retry;
1117 	    }
1118 	}
1119     }
1120 
1121     /* Set "can_retry" when it's possible to rewind the file and try with
1122      * another "fenc" value.  It's FALSE when no other "fenc" to try, reading
1123      * stdin or fixed at a specific encoding. */
1124     can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc);
1125 
1126     if (!skip_read)
1127     {
1128 	linerest = 0;
1129 	filesize = 0;
1130 	skip_count = lines_to_skip;
1131 	read_count = lines_to_read;
1132 	conv_restlen = 0;
1133 #ifdef FEAT_PERSISTENT_UNDO
1134 	read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1135 				  && curbuf->b_ffname != NULL
1136 				  && curbuf->b_p_udf
1137 				  && !filtering
1138 				  && !read_fifo
1139 				  && !read_stdin
1140 				  && !read_buffer);
1141 	if (read_undo_file)
1142 	    sha256_start(&sha_ctx);
1143 #endif
1144 #ifdef FEAT_CRYPT
1145 	if (curbuf->b_cryptstate != NULL)
1146 	{
1147 	    /* Need to free the state, but keep the key, don't want to ask for
1148 	     * it again. */
1149 	    crypt_free_state(curbuf->b_cryptstate);
1150 	    curbuf->b_cryptstate = NULL;
1151 	}
1152 #endif
1153     }
1154 
1155     while (!error && !got_int)
1156     {
1157 	/*
1158 	 * We allocate as much space for the file as we can get, plus
1159 	 * space for the old line plus room for one terminating NUL.
1160 	 * The amount is limited by the fact that read() only can read
1161 	 * upto max_unsigned characters (and other things).
1162 	 */
1163 	if (!skip_read)
1164 	{
1165 #if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
1166 		size = SSIZE_MAX;		    /* use max I/O size, 52K */
1167 #else
1168 		/* Use buffer >= 64K.  Add linerest to double the size if the
1169 		 * line gets very long, to avoid a lot of copying. But don't
1170 		 * read more than 1 Mbyte at a time, so we can be interrupted.
1171 		 */
1172 		size = 0x10000L + linerest;
1173 		if (size > 0x100000L)
1174 		    size = 0x100000L;
1175 #endif
1176 	}
1177 
1178 	/* Protect against the argument of lalloc() going negative. */
1179 	if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL)
1180 	{
1181 	    ++split;
1182 	    *ptr = NL;		    /* split line by inserting a NL */
1183 	    size = 1;
1184 	}
1185 	else
1186 	{
1187 	    if (!skip_read)
1188 	    {
1189 		for ( ; size >= 10; size = (long)((long_u)size >> 1))
1190 		{
1191 		    if ((new_buffer = lalloc((long_u)(size + linerest + 1),
1192 							      FALSE)) != NULL)
1193 			break;
1194 		}
1195 		if (new_buffer == NULL)
1196 		{
1197 		    do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1198 		    error = TRUE;
1199 		    break;
1200 		}
1201 		if (linerest)	/* copy characters from the previous buffer */
1202 		    mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1203 		vim_free(buffer);
1204 		buffer = new_buffer;
1205 		ptr = buffer + linerest;
1206 		line_start = buffer;
1207 
1208 		/* May need room to translate into.
1209 		 * For iconv() we don't really know the required space, use a
1210 		 * factor ICONV_MULT.
1211 		 * latin1 to utf-8: 1 byte becomes up to 2 bytes
1212 		 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1213 		 * become up to 4 bytes, size must be multiple of 2
1214 		 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1215 		 * multiple of 2
1216 		 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1217 		 * multiple of 4 */
1218 		real_size = (int)size;
1219 #ifdef USE_ICONV
1220 		if (iconv_fd != (iconv_t)-1)
1221 		    size = size / ICONV_MULT;
1222 		else
1223 #endif
1224 		    if (fio_flags & FIO_LATIN1)
1225 		    size = size / 2;
1226 		else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1227 		    size = (size * 2 / 3) & ~1;
1228 		else if (fio_flags & FIO_UCS4)
1229 		    size = (size * 2 / 3) & ~3;
1230 		else if (fio_flags == FIO_UCSBOM)
1231 		    size = size / ICONV_MULT;	/* worst case */
1232 #ifdef WIN3264
1233 		else if (fio_flags & FIO_CODEPAGE)
1234 		    size = size / ICONV_MULT;	/* also worst case */
1235 #endif
1236 #ifdef MACOS_CONVERT
1237 		else if (fio_flags & FIO_MACROMAN)
1238 		    size = size / ICONV_MULT;	/* also worst case */
1239 #endif
1240 
1241 		if (conv_restlen > 0)
1242 		{
1243 		    /* Insert unconverted bytes from previous line. */
1244 		    mch_memmove(ptr, conv_rest, conv_restlen);
1245 		    ptr += conv_restlen;
1246 		    size -= conv_restlen;
1247 		}
1248 
1249 		if (read_buffer)
1250 		{
1251 		    /*
1252 		     * Read bytes from curbuf.  Used for converting text read
1253 		     * from stdin.
1254 		     */
1255 		    if (read_buf_lnum > from)
1256 			size = 0;
1257 		    else
1258 		    {
1259 			int	n, ni;
1260 			long	tlen;
1261 
1262 			tlen = 0;
1263 			for (;;)
1264 			{
1265 			    p = ml_get(read_buf_lnum) + read_buf_col;
1266 			    n = (int)STRLEN(p);
1267 			    if ((int)tlen + n + 1 > size)
1268 			    {
1269 				/* Filled up to "size", append partial line.
1270 				 * Change NL to NUL to reverse the effect done
1271 				 * below. */
1272 				n = (int)(size - tlen);
1273 				for (ni = 0; ni < n; ++ni)
1274 				{
1275 				    if (p[ni] == NL)
1276 					ptr[tlen++] = NUL;
1277 				    else
1278 					ptr[tlen++] = p[ni];
1279 				}
1280 				read_buf_col += n;
1281 				break;
1282 			    }
1283 			    else
1284 			    {
1285 				/* Append whole line and new-line.  Change NL
1286 				 * to NUL to reverse the effect done below. */
1287 				for (ni = 0; ni < n; ++ni)
1288 				{
1289 				    if (p[ni] == NL)
1290 					ptr[tlen++] = NUL;
1291 				    else
1292 					ptr[tlen++] = p[ni];
1293 				}
1294 				ptr[tlen++] = NL;
1295 				read_buf_col = 0;
1296 				if (++read_buf_lnum > from)
1297 				{
1298 				    /* When the last line didn't have an
1299 				     * end-of-line don't add it now either. */
1300 				    if (!curbuf->b_p_eol)
1301 					--tlen;
1302 				    size = tlen;
1303 				    break;
1304 				}
1305 			    }
1306 			}
1307 		    }
1308 		}
1309 		else
1310 		{
1311 		    /*
1312 		     * Read bytes from the file.
1313 		     */
1314 		    size = read_eintr(fd, ptr, size);
1315 		}
1316 
1317 #ifdef FEAT_CRYPT
1318 		/*
1319 		 * At start of file: Check for magic number of encryption.
1320 		 */
1321 		if (filesize == 0 && size > 0)
1322 		    cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1323 						  &filesize, newfile, sfname,
1324 						  &did_ask_for_key);
1325 		/*
1326 		 * Decrypt the read bytes.  This is done before checking for
1327 		 * EOF because the crypt layer may be buffering.
1328 		 */
1329 		if (cryptkey != NULL && curbuf->b_cryptstate != NULL
1330 								   && size > 0)
1331 		{
1332 # ifdef CRYPT_NOT_INPLACE
1333 		    if (crypt_works_inplace(curbuf->b_cryptstate))
1334 		    {
1335 # endif
1336 			crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
1337 # ifdef CRYPT_NOT_INPLACE
1338 		    }
1339 		    else
1340 		    {
1341 			char_u	*newptr = NULL;
1342 			int	decrypted_size;
1343 
1344 			decrypted_size = crypt_decode_alloc(
1345 				    curbuf->b_cryptstate, ptr, size, &newptr);
1346 
1347 			/* If the crypt layer is buffering, not producing
1348 			 * anything yet, need to read more. */
1349 			if (decrypted_size == 0)
1350 			    continue;
1351 
1352 			if (linerest == 0)
1353 			{
1354 			    /* Simple case: reuse returned buffer (may be
1355 			     * NULL, checked later). */
1356 			    new_buffer = newptr;
1357 			}
1358 			else
1359 			{
1360 			    long_u	new_size;
1361 
1362 			    /* Need new buffer to add bytes carried over. */
1363 			    new_size = (long_u)(decrypted_size + linerest + 1);
1364 			    new_buffer = lalloc(new_size, FALSE);
1365 			    if (new_buffer == NULL)
1366 			    {
1367 				do_outofmem_msg(new_size);
1368 				error = TRUE;
1369 				break;
1370 			    }
1371 
1372 			    mch_memmove(new_buffer, buffer, linerest);
1373 			    if (newptr != NULL)
1374 				mch_memmove(new_buffer + linerest, newptr,
1375 							      decrypted_size);
1376 			}
1377 
1378 			if (new_buffer != NULL)
1379 			{
1380 			    vim_free(buffer);
1381 			    buffer = new_buffer;
1382 			    new_buffer = NULL;
1383 			    line_start = buffer;
1384 			    ptr = buffer + linerest;
1385 			}
1386 			size = decrypted_size;
1387 		    }
1388 # endif
1389 		}
1390 #endif
1391 
1392 		if (size <= 0)
1393 		{
1394 		    if (size < 0)		    /* read error */
1395 			error = TRUE;
1396 		    else if (conv_restlen > 0)
1397 		    {
1398 			/*
1399 			 * Reached end-of-file but some trailing bytes could
1400 			 * not be converted.  Truncated file?
1401 			 */
1402 
1403 			/* When we did a conversion report an error. */
1404 			if (fio_flags != 0
1405 #ifdef USE_ICONV
1406 				|| iconv_fd != (iconv_t)-1
1407 #endif
1408 			   )
1409 			{
1410 			    if (can_retry)
1411 				goto rewind_retry;
1412 			    if (conv_error == 0)
1413 				conv_error = curbuf->b_ml.ml_line_count
1414 								- linecnt + 1;
1415 			}
1416 			/* Remember the first linenr with an illegal byte */
1417 			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 			{
1427 			    /* 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. */
1431 			    if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
1432 #ifdef USE_ICONV
1433 				    || iconv_fd != (iconv_t)-1
1434 #endif
1435 			       ))
1436 			    {
1437 				while (conv_restlen > 0)
1438 				{
1439 				    *(--ptr) = bad_char_behavior;
1440 				    --conv_restlen;
1441 				}
1442 			    }
1443 			    fio_flags = 0;	/* don't convert this */
1444 #ifdef USE_ICONV
1445 			    if (iconv_fd != (iconv_t)-1)
1446 			    {
1447 				iconv_close(iconv_fd);
1448 				iconv_fd = (iconv_t)-1;
1449 			    }
1450 #endif
1451 			}
1452 		    }
1453 		}
1454 	    }
1455 	    skip_read = FALSE;
1456 
1457 	    /*
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
1464 #ifdef FEAT_CRYPT
1465 		   || (cryptkey != NULL
1466 			&& filesize == crypt_get_header_len(
1467 						 crypt_get_method_nr(curbuf)))
1468 #endif
1469 		       )
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 
1478 		/* no BOM detection in a short file or in binary mode */
1479 		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 		{
1486 		    /* Remove BOM from the text */
1487 		    filesize += blen;
1488 		    size -= blen;
1489 		    mch_memmove(ptr, ptr + blen, (size_t)size);
1490 		    if (set_options)
1491 		    {
1492 			curbuf->b_p_bomb = TRUE;
1493 			curbuf->b_start_bomb = TRUE;
1494 		    }
1495 		}
1496 
1497 		if (fio_flags == FIO_UCSBOM)
1498 		{
1499 		    if (ccname == NULL)
1500 		    {
1501 			/* No BOM detected: retry with next encoding. */
1502 			advance_fenc = TRUE;
1503 		    }
1504 		    else
1505 		    {
1506 			/* BOM detected: set "fenc" and jump back */
1507 			if (fenc_alloced)
1508 			    vim_free(fenc);
1509 			fenc = ccname;
1510 			fenc_alloced = FALSE;
1511 		    }
1512 		    /* retry reading without getting new bytes or rewinding */
1513 		    skip_read = TRUE;
1514 		    goto retry;
1515 		}
1516 	    }
1517 
1518 	    /* Include not converted bytes. */
1519 	    ptr -= conv_restlen;
1520 	    size += conv_restlen;
1521 	    conv_restlen = 0;
1522 	    /*
1523 	     * Break here for a read error or end-of-file.
1524 	     */
1525 	    if (size <= 0)
1526 		break;
1527 
1528 
1529 #ifdef USE_ICONV
1530 	    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
1549 		 * another conversion.  Except for when there is no
1550 		 * alternative (help files).
1551 		 */
1552 		while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1553 							       &top, &to_size)
1554 			    == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1555 						  || from_size > CONV_RESTLEN)
1556 		{
1557 		    if (can_retry)
1558 			goto rewind_retry;
1559 		    if (conv_error == 0)
1560 			conv_error = readfile_linenr(linecnt,
1561 							  ptr, (char_u *)top);
1562 
1563 		    /* Deal with a bad byte and continue with the next. */
1564 		    ++fromp;
1565 		    --from_size;
1566 		    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 		    }
1576 		}
1577 
1578 		if (from_size > 0)
1579 		{
1580 		    /* Some remaining characters, keep them for the next
1581 		     * round. */
1582 		    mch_memmove(conv_rest, (char_u *)fromp, from_size);
1583 		    conv_restlen = (int)from_size;
1584 		}
1585 
1586 		/* move the linerest to before the converted characters */
1587 		line_start = ptr - linerest;
1588 		mch_memmove(line_start, buffer, (size_t)linerest);
1589 		size = (long)((char_u *)top - ptr);
1590 	    }
1591 #endif
1592 
1593 #ifdef WIN3264
1594 	    if (fio_flags & FIO_CODEPAGE)
1595 	    {
1596 		char_u	*src, *dst;
1597 		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 
1604 		/*
1605 		 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
1606 		 * 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'
1610 		 *
1611 		 * 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.
1614 		 */
1615 
1616 		/* Replacement string for WideCharToMultiByte(). */
1617 		if (bad_char_behavior > 0)
1618 		    replstr[0] = bad_char_behavior;
1619 		else
1620 		    replstr[0] = '?';
1621 		replstr[1] = NUL;
1622 
1623 		/*
1624 		 * Move the bytes to the end of the buffer, so that we have
1625 		 * room to put the result at the start.
1626 		 */
1627 		src = ptr + real_size - size;
1628 		mch_memmove(src, ptr, size);
1629 
1630 		/*
1631 		 * Do the conversion.
1632 		 */
1633 		dst = ptr;
1634 		size = size;
1635 		while (size > 0)
1636 		{
1637 		    found_bad = FALSE;
1638 
1639 #  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
1640 		    if (codepage == CP_UTF8)
1641 		    {
1642 			/* Handle CP_UTF8 input ourselves to be able to handle
1643 			 * trailing bytes properly.
1644 			 * Get one UTF-8 character from src. */
1645 			bytelen = (int)utf_ptr2len_len(src, size);
1646 			if (bytelen > size)
1647 			{
1648 			    /* 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. */
1651 			    if (bytelen <= CONV_RESTLEN)
1652 				break;
1653 
1654 			    /* weird overlong byte sequence */
1655 			    bytelen = size;
1656 			    found_bad = TRUE;
1657 			}
1658 			else
1659 			{
1660 			    int	    u8c = utf_ptr2char(src);
1661 
1662 			    if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
1663 				found_bad = TRUE;
1664 			    ucs2buf[0] = u8c;
1665 			    ucs2len = 1;
1666 			}
1667 		    }
1668 		    else
1669 #  endif
1670 		    {
1671 			/* We don't know how long the byte sequence is, try
1672 			 * from one to three bytes. */
1673 			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 			{
1685 			    /* If we have only one byte then it's probably an
1686 			     * incomplete byte sequence.  Otherwise discard
1687 			     * one byte as a bad character. */
1688 			    if (size == 1)
1689 				break;
1690 			    found_bad = TRUE;
1691 			    bytelen = 1;
1692 			}
1693 		    }
1694 
1695 		    if (!found_bad)
1696 		    {
1697 			int	i;
1698 
1699 			/* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */
1700 			if (enc_utf8)
1701 			{
1702 			    /* From UCS-2 to UTF-8.  Cannot fail. */
1703 			    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 
1711 			    /* From UCS-2 to "enc_codepage".  If the
1712 			     * conversion uses the default character "?",
1713 			     * the data doesn't fit in this encoding. */
1714 			    dstlen = WideCharToMultiByte(enc_codepage, 0,
1715 				    (LPCWSTR)ucs2buf, ucs2len,
1716 				    (LPSTR)dst, (int)(src - dst),
1717 				    replstr, &bad);
1718 			    if (bad)
1719 				found_bad = TRUE;
1720 			    else
1721 				dst += dstlen;
1722 			}
1723 		    }
1724 
1725 		    if (found_bad)
1726 		    {
1727 			/* Deal with bytes we can't convert. */
1728 			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;
1746 		}
1747 
1748 		if (size > 0)
1749 		{
1750 		    /* An incomplete byte sequence remaining. */
1751 		    mch_memmove(conv_rest, src, size);
1752 		    conv_restlen = size;
1753 		}
1754 
1755 		/* The new size is equal to how much "dst" was advanced. */
1756 		size = (long)(dst - ptr);
1757 	    }
1758 	    else
1759 #endif
1760 #ifdef MACOS_CONVERT
1761 	    if (fio_flags & FIO_MACROMAN)
1762 	    {
1763 		/*
1764 		 * Conversion from Apple MacRoman char encoding to UTF-8 or
1765 		 * latin1.  This is in os_mac_conv.c.
1766 		 */
1767 		if (macroman2enc(ptr, &size, real_size) == FAIL)
1768 		    goto rewind_retry;
1769 	    }
1770 	    else
1771 #endif
1772 	    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 		    {
1792 			/* Check for a trailing incomplete UTF-8 sequence */
1793 			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 		{
1804 		    /* Check for a trailing byte */
1805 		    p = ptr + (size & ~1);
1806 		    if (size & 1)
1807 			tail = p;
1808 		    if ((fio_flags & FIO_UTF16) && p > ptr)
1809 		    {
1810 			/* Check for a trailing leading word */
1811 			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 		}
1827 		else /*  FIO_UCS4 */
1828 		{
1829 		    /* Check for trailing 1, 2 or 3 bytes */
1830 		    p = ptr + (size & ~3);
1831 		    if (size & 3)
1832 			tail = p;
1833 		}
1834 
1835 		/* If there is a trailing incomplete sequence move it to
1836 		 * conv_rest[]. */
1837 		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 			    {
1868 				/* Missing leading word. */
1869 				if (can_retry)
1870 				    goto rewind_retry;
1871 				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;
1878 			    }
1879 
1880 			    /* found second word of double-word, get the first
1881 			     * word and compute the resulting character */
1882 			    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 			    }
1892 			    u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1893 							      + (u8c & 0x3ff);
1894 
1895 			    /* Check if the word is indeed a leading word. */
1896 			    if (u16c < 0xd800 || u16c > 0xdbff)
1897 			    {
1898 				if (can_retry)
1899 				    goto rewind_retry;
1900 				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;
1907 			    }
1908 			}
1909 		    }
1910 		    else if (fio_flags & FIO_UCS4)
1911 		    {
1912 			if (fio_flags & FIO_ENDIAN_L)
1913 			{
1914 			    u8c = (unsigned)*--p << 24;
1915 			    u8c += (unsigned)*--p << 16;
1916 			    u8c += (unsigned)*--p << 8;
1917 			    u8c += *--p;
1918 			}
1919 			else	/* big endian */
1920 			{
1921 			    u8c = *--p;
1922 			    u8c += (unsigned)*--p << 8;
1923 			    u8c += (unsigned)*--p << 16;
1924 			    u8c += (unsigned)*--p << 24;
1925 			}
1926 		    }
1927 		    else    /* UTF-8 */
1928 		    {
1929 			if (*--p < 0x80)
1930 			    u8c = *p;
1931 			else
1932 			{
1933 			    len = utf_head_off(ptr, p);
1934 			    p -= len;
1935 			    u8c = utf_ptr2char(p);
1936 			    if (len == 0)
1937 			    {
1938 				/* Not a valid UTF-8 character, retry with
1939 				 * another fenc when possible, otherwise just
1940 				 * report the error. */
1941 				if (can_retry)
1942 				    goto rewind_retry;
1943 				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;
1950 			    }
1951 			}
1952 		    }
1953 		    if (enc_utf8)	/* produce UTF-8 */
1954 		    {
1955 			dest -= utf_char2len(u8c);
1956 			(void)utf_char2bytes(u8c, dest);
1957 		    }
1958 		    else		/* produce Latin1 */
1959 		    {
1960 			--dest;
1961 			if (u8c >= 0x100)
1962 			{
1963 			    /* character doesn't fit in latin1, retry with
1964 			     * another fenc when possible, otherwise just
1965 			     * report the error. */
1966 			    if (can_retry)
1967 				goto rewind_retry;
1968 			    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;
1978 			}
1979 			else
1980 			    *dest = u8c;
1981 		    }
1982 		}
1983 
1984 		/* move the linerest to before the converted characters */
1985 		line_start = dest - linerest;
1986 		mch_memmove(line_start, buffer, (size_t)linerest);
1987 		size = (long)((ptr + real_size) - dest);
1988 		ptr = dest;
1989 	    }
1990 	    else if (enc_utf8 && !curbuf->b_p_bin)
1991 	    {
1992 		int  incomplete_tail = FALSE;
1993 
1994 		/* Reading UTF-8: Check if the bytes are valid UTF-8. */
1995 		for (p = ptr; ; ++p)
1996 		{
1997 		    int	 todo = (int)((ptr + size) - p);
1998 		    int	 l;
1999 
2000 		    if (todo <= 0)
2001 			break;
2002 		    if (*p >= 0x80)
2003 		    {
2004 			/* 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. */
2008 			l = utf_ptr2len_len(p, todo);
2009 			if (l > todo && !incomplete_tail)
2010 			{
2011 			    /* 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. */
2015 			    if (p > ptr || filesize > 0)
2016 				incomplete_tail = TRUE;
2017 			    /* Incomplete byte sequence, move it to conv_rest[]
2018 			     * and try to read the rest of it, unless we've
2019 			     * already done so. */
2020 			    if (p > ptr)
2021 			    {
2022 				conv_restlen = todo;
2023 				mch_memmove(conv_rest, p, conv_restlen);
2024 				size -= conv_restlen;
2025 				break;
2026 			    }
2027 			}
2028 			if (l == 1 || l > todo)
2029 			{
2030 			    /* 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. */
2033 			    if (can_retry && !incomplete_tail)
2034 				break;
2035 #ifdef USE_ICONV
2036 			    /* When we did a conversion report an error. */
2037 			    if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2038 				conv_error = readfile_linenr(linecnt, ptr, p);
2039 #endif
2040 			    /* Remember the first linenr with an illegal byte */
2041 			    if (conv_error == 0 && illegal_byte == 0)
2042 				illegal_byte = readfile_linenr(linecnt, ptr, p);
2043 
2044 			    /* Drop, keep or replace the bad byte. */
2045 			    if (bad_char_behavior == BAD_DROP)
2046 			    {
2047 				mch_memmove(p, p + 1, todo - 1);
2048 				--p;
2049 				--size;
2050 			    }
2051 			    else if (bad_char_behavior != BAD_KEEP)
2052 				*p = bad_char_behavior;
2053 			}
2054 			else
2055 			    p += l - 1;
2056 		    }
2057 		}
2058 		if (p < ptr + size && !incomplete_tail)
2059 		{
2060 		    /* Detected a UTF-8 error. */
2061 rewind_retry:
2062 		    /* Retry reading with another conversion. */
2063 #if defined(FEAT_EVAL) && defined(USE_ICONV)
2064 		    if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
2065 			/* iconv() failed, try 'charconvert' */
2066 			did_iconv = TRUE;
2067 		    else
2068 #endif
2069 			/* use next item from 'fileencodings' */
2070 			advance_fenc = TRUE;
2071 		    file_rewind = TRUE;
2072 		    goto retry;
2073 		}
2074 	    }
2075 
2076 	    /* count the number of characters (after conversion!) */
2077 	    filesize += size;
2078 
2079 	    /*
2080 	     * when reading the first part of a file: guess EOL type
2081 	     */
2082 	    if (fileformat == EOL_UNKNOWN)
2083 	    {
2084 		/* First try finding a NL, for Dos and Unix */
2085 		if (try_dos || try_unix)
2086 		{
2087 		    /* Reset the carriage return counter. */
2088 		    if (try_mac)
2089 			try_mac = 1;
2090 
2091 		    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 			}
2102 			else if (*p == CAR && try_mac)
2103 			    try_mac++;
2104 		    }
2105 
2106 		    /* Don't give in to EOL_UNIX if EOL_MAC is more likely */
2107 		    if (fileformat == EOL_UNIX && try_mac)
2108 		    {
2109 			/* Need to reset the counters when retrying fenc. */
2110 			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 		    }
2127 		    else if (fileformat == EOL_UNKNOWN && try_mac == 1)
2128 			/* Looking for CR but found no end-of-line markers at
2129 			 * all: use the default format. */
2130 			fileformat = default_fileformat();
2131 		}
2132 
2133 		/* No NL found: may use Mac format */
2134 		if (fileformat == EOL_UNKNOWN && try_mac)
2135 		    fileformat = EOL_MAC;
2136 
2137 		/* Still nothing found?  Use first format in 'ffs' */
2138 		if (fileformat == EOL_UNKNOWN)
2139 		    fileformat = default_fileformat();
2140 
2141 		/* if editing a new file: may set p_tx and p_ff */
2142 		if (set_options)
2143 		    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 	    {
2156 		/* catch most common case first */
2157 		if ((c = *ptr) != NUL && c != CAR && c != NL)
2158 		    continue;
2159 		if (c == NUL)
2160 		    *ptr = NL;	/* NULs are replaced by newlines! */
2161 		else if (c == NL)
2162 		    *ptr = CAR;	/* NLs are replaced by CRs! */
2163 		else
2164 		{
2165 		    if (skip_count == 0)
2166 		    {
2167 			*ptr = NUL;	    /* end of line */
2168 			len = (colnr_T) (ptr - line_start + 1);
2169 			if (ml_append(lnum, line_start, len, newfile) == FAIL)
2170 			{
2171 			    error = TRUE;
2172 			    break;
2173 			}
2174 #ifdef FEAT_PERSISTENT_UNDO
2175 			if (read_undo_file)
2176 			    sha256_update(&sha_ctx, line_start, len);
2177 #endif
2178 			++lnum;
2179 			if (--read_count == 0)
2180 			{
2181 			    error = TRUE;	/* break loop */
2182 			    line_start = ptr;	/* nothing left to write */
2183 			    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 	    {
2197 		if ((c = *ptr) != NUL && c != NL)  /* catch most common case */
2198 		    continue;
2199 		if (c == NUL)
2200 		    *ptr = NL;	/* NULs are replaced by newlines! */
2201 		else
2202 		{
2203 		    if (skip_count == 0)
2204 		    {
2205 			*ptr = NUL;		/* end of line */
2206 			len = (colnr_T)(ptr - line_start + 1);
2207 			if (fileformat == EOL_DOS)
2208 			{
2209 			    if (ptr > line_start && ptr[-1] == CAR)
2210 			    {
2211 				/* remove CR before NL */
2212 				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
2226 					|| vim_lseek(fd, (off_T)0L, SEEK_SET)
2227 									  == 0))
2228 				{
2229 				    fileformat = EOL_UNIX;
2230 				    if (set_options)
2231 					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 			}
2244 #ifdef FEAT_PERSISTENT_UNDO
2245 			if (read_undo_file)
2246 			    sha256_update(&sha_ctx, line_start, len);
2247 #endif
2248 			++lnum;
2249 			if (--read_count == 0)
2250 			{
2251 			    error = TRUE;	    /* break loop */
2252 			    line_start = ptr;	/* nothing left to write */
2253 			    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 
2266 failed:
2267     /* not an error, max. number of lines reached */
2268     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     {
2284 	/* remember for when writing */
2285 	if (set_options)
2286 	    curbuf->b_p_eol = FALSE;
2287 	*ptr = NUL;
2288 	len = (colnr_T)(ptr - line_start + 1);
2289 	if (ml_append(lnum, line_start, len, newfile) == FAIL)
2290 	    error = TRUE;
2291 	else
2292 	{
2293 #ifdef FEAT_PERSISTENT_UNDO
2294 	    if (read_undo_file)
2295 		sha256_update(&sha_ctx, line_start, len);
2296 #endif
2297 	    read_no_eol_lnum = ++lnum;
2298 	}
2299     }
2300 
2301     if (set_options)
2302 	save_file_ff(curbuf);		/* remember the current file format */
2303 
2304 #ifdef FEAT_CRYPT
2305     if (curbuf->b_cryptstate != NULL)
2306     {
2307 	crypt_free_state(curbuf->b_cryptstate);
2308 	curbuf->b_cryptstate = NULL;
2309     }
2310     if (cryptkey != NULL && cryptkey != curbuf->b_p_key)
2311 	crypt_free_key(cryptkey);
2312     /* Don't set cryptkey to NULL, it's used below as a flag that
2313      * encryption was used. */
2314 #endif
2315 
2316     /* If editing a new file: set 'fenc' for the current buffer.
2317      * Also for ":read ++edit file". */
2318     if (set_options)
2319 	set_string_option_direct((char_u *)"fenc", -1, fenc,
2320 						       OPT_FREE|OPT_LOCAL, 0);
2321     if (fenc_alloced)
2322 	vim_free(fenc);
2323 #ifdef USE_ICONV
2324     if (iconv_fd != (iconv_t)-1)
2325     {
2326 	iconv_close(iconv_fd);
2327 	iconv_fd = (iconv_t)-1;
2328     }
2329 #endif
2330 
2331     if (!read_buffer && !read_stdin)
2332 	close(fd);				/* errors are ignored */
2333 #ifdef HAVE_FD_CLOEXEC
2334     else
2335     {
2336 	int fdflags = fcntl(fd, F_GETFD);
2337 	if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
2338 	    (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
2339     }
2340 #endif
2341     vim_free(buffer);
2342 
2343 #ifdef HAVE_DUP
2344     if (read_stdin)
2345     {
2346 	/* Use stderr for stdin, makes shell commands work. */
2347 	close(0);
2348 	vim_ignored = dup(2);
2349     }
2350 #endif
2351 
2352     if (tmpname != NULL)
2353     {
2354 	mch_remove(tmpname);		/* delete converted file */
2355 	vim_free(tmpname);
2356     }
2357     --no_wait_return;			/* may wait for return now */
2358 
2359     /*
2360      * In recovery mode everything but autocommands is skipped.
2361      */
2362     if (!recoverymode)
2363     {
2364 	/* need to delete the last line, which comes from the empty buffer */
2365 	if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2366 	{
2367 #ifdef FEAT_NETBEANS_INTG
2368 	    netbeansFireChanges = 0;
2369 #endif
2370 	    ml_delete(curbuf->b_ml.ml_line_count, FALSE);
2371 #ifdef FEAT_NETBEANS_INTG
2372 	    netbeansFireChanges = 1;
2373 #endif
2374 	    --linecnt;
2375 	}
2376 	linecnt = curbuf->b_ml.ml_line_count - linecnt;
2377 	if (filesize == 0)
2378 	    linecnt = 0;
2379 	if (newfile || read_buffer)
2380 	{
2381 	    redraw_curbuf_later(NOT_VALID);
2382 #ifdef FEAT_DIFF
2383 	    /* After reading the text into the buffer the diff info needs to
2384 	     * be updated. */
2385 	    diff_invalidate(curbuf);
2386 #endif
2387 #ifdef FEAT_FOLDING
2388 	    /* All folds in the window are invalid now.  Mark them for update
2389 	     * before triggering autocommands. */
2390 	    foldUpdateAll(curwin);
2391 #endif
2392 	}
2393 	else if (linecnt)		/* appended at least one line */
2394 	    appended_lines_mark(from, linecnt);
2395 
2396 #ifndef ALWAYS_USE_GUI
2397 	/*
2398 	 * If we were reading from the same terminal as where messages go,
2399 	 * the screen will have been messed up.
2400 	 * Switch on raw mode now and clear the screen.
2401 	 */
2402 	if (read_stdin)
2403 	{
2404 	    settmode(TMODE_RAW);	/* set to raw mode */
2405 	    starttermcap();
2406 	    screenclear();
2407 	}
2408 #endif
2409 
2410 	if (got_int)
2411 	{
2412 	    if (!(flags & READ_DUMMY))
2413 	    {
2414 		filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2415 		if (newfile)
2416 		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
2417 	    }
2418 	    msg_scroll = msg_save;
2419 #ifdef FEAT_VIMINFO
2420 	    check_marks_read();
2421 #endif
2422 	    return OK;		/* an interrupt isn't really an error */
2423 	}
2424 
2425 	if (!filtering && !(flags & READ_DUMMY))
2426 	{
2427 	    msg_add_fname(curbuf, sfname);   /* fname in IObuff with quotes */
2428 	    c = FALSE;
2429 
2430 #ifdef UNIX
2431 	    if (S_ISFIFO(perm))			    /* fifo */
2432 	    {
2433 		STRCAT(IObuff, _("[fifo]"));
2434 		c = TRUE;
2435 	    }
2436 	    if (S_ISSOCK(perm))			    /* or socket */
2437 	    {
2438 		STRCAT(IObuff, _("[socket]"));
2439 		c = TRUE;
2440 	    }
2441 # ifdef OPEN_CHR_FILES
2442 	    if (S_ISCHR(perm))			    /* or character special */
2443 	    {
2444 		STRCAT(IObuff, _("[character special]"));
2445 		c = TRUE;
2446 	    }
2447 # endif
2448 #endif
2449 	    if (curbuf->b_p_ro)
2450 	    {
2451 		STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2452 		c = TRUE;
2453 	    }
2454 	    if (read_no_eol_lnum)
2455 	    {
2456 		msg_add_eol();
2457 		c = TRUE;
2458 	    }
2459 	    if (ff_error == EOL_DOS)
2460 	    {
2461 		STRCAT(IObuff, _("[CR missing]"));
2462 		c = TRUE;
2463 	    }
2464 	    if (split)
2465 	    {
2466 		STRCAT(IObuff, _("[long lines split]"));
2467 		c = TRUE;
2468 	    }
2469 	    if (notconverted)
2470 	    {
2471 		STRCAT(IObuff, _("[NOT converted]"));
2472 		c = TRUE;
2473 	    }
2474 	    else if (converted)
2475 	    {
2476 		STRCAT(IObuff, _("[converted]"));
2477 		c = TRUE;
2478 	    }
2479 #ifdef FEAT_CRYPT
2480 	    if (cryptkey != NULL)
2481 	    {
2482 		crypt_append_msg(curbuf);
2483 		c = TRUE;
2484 	    }
2485 #endif
2486 	    if (conv_error != 0)
2487 	    {
2488 		sprintf((char *)IObuff + STRLEN(IObuff),
2489 		       _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
2490 		c = TRUE;
2491 	    }
2492 	    else if (illegal_byte > 0)
2493 	    {
2494 		sprintf((char *)IObuff + STRLEN(IObuff),
2495 			 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2496 		c = TRUE;
2497 	    }
2498 	    else if (error)
2499 	    {
2500 		STRCAT(IObuff, _("[READ ERRORS]"));
2501 		c = TRUE;
2502 	    }
2503 	    if (msg_add_fileformat(fileformat))
2504 		c = TRUE;
2505 #ifdef FEAT_CRYPT
2506 	    if (cryptkey != NULL)
2507 		msg_add_lines(c, (long)linecnt, filesize
2508 			 - crypt_get_header_len(crypt_get_method_nr(curbuf)));
2509 	    else
2510 #endif
2511 		msg_add_lines(c, (long)linecnt, filesize);
2512 
2513 	    VIM_CLEAR(keep_msg);
2514 	    msg_scrolled_ign = TRUE;
2515 #ifdef ALWAYS_USE_GUI
2516 	    /* Don't show the message when reading stdin, it would end up in a
2517 	     * message box (which might be shown when exiting!) */
2518 	    if (read_stdin || read_buffer)
2519 		p = msg_may_trunc(FALSE, IObuff);
2520 	    else
2521 #endif
2522 		p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
2523 	    if (read_stdin || read_buffer || restart_edit != 0
2524 		    || (msg_scrolled != 0 && !need_wait_return))
2525 		/* Need to repeat the message after redrawing when:
2526 		 * - When reading from stdin (the screen will be cleared next).
2527 		 * - When restart_edit is set (otherwise there will be a delay
2528 		 *   before redrawing).
2529 		 * - When the screen was scrolled but there is no wait-return
2530 		 *   prompt. */
2531 		set_keep_msg(p, 0);
2532 	    msg_scrolled_ign = FALSE;
2533 	}
2534 
2535 	/* with errors writing the file requires ":w!" */
2536 	if (newfile && (error
2537 		    || conv_error != 0
2538 		    || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)))
2539 	    curbuf->b_p_ro = TRUE;
2540 
2541 	u_clearline();	    /* cannot use "U" command after adding lines */
2542 
2543 	/*
2544 	 * In Ex mode: cursor at last new line.
2545 	 * Otherwise: cursor at first new line.
2546 	 */
2547 	if (exmode_active)
2548 	    curwin->w_cursor.lnum = from + linecnt;
2549 	else
2550 	    curwin->w_cursor.lnum = from + 1;
2551 	check_cursor_lnum();
2552 	beginline(BL_WHITE | BL_FIX);	    /* on first non-blank */
2553 
2554 	/*
2555 	 * Set '[ and '] marks to the newly read lines.
2556 	 */
2557 	curbuf->b_op_start.lnum = from + 1;
2558 	curbuf->b_op_start.col = 0;
2559 	curbuf->b_op_end.lnum = from + linecnt;
2560 	curbuf->b_op_end.col = 0;
2561 
2562 #ifdef WIN32
2563 	/*
2564 	 * Work around a weird problem: When a file has two links (only
2565 	 * possible on NTFS) and we write through one link, then stat() it
2566 	 * through the other link, the timestamp information may be wrong.
2567 	 * It's correct again after reading the file, thus reset the timestamp
2568 	 * here.
2569 	 */
2570 	if (newfile && !read_stdin && !read_buffer
2571 					 && mch_stat((char *)fname, &st) >= 0)
2572 	{
2573 	    buf_store_time(curbuf, &st, fname);
2574 	    curbuf->b_mtime_read = curbuf->b_mtime;
2575 	}
2576 #endif
2577     }
2578     msg_scroll = msg_save;
2579 
2580 #ifdef FEAT_VIMINFO
2581     /*
2582      * Get the marks before executing autocommands, so they can be used there.
2583      */
2584     check_marks_read();
2585 #endif
2586 
2587     /*
2588      * We remember if the last line of the read didn't have
2589      * an eol even when 'binary' is off, to support turning 'fixeol' off,
2590      * or writing the read again with 'binary' on.  The latter is required
2591      * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
2592      */
2593     curbuf->b_no_eol_lnum = read_no_eol_lnum;
2594 
2595     /* When reloading a buffer put the cursor at the first line that is
2596      * different. */
2597     if (flags & READ_KEEP_UNDO)
2598 	u_find_first_changed();
2599 
2600 #ifdef FEAT_PERSISTENT_UNDO
2601     /*
2602      * When opening a new file locate undo info and read it.
2603      */
2604     if (read_undo_file)
2605     {
2606 	char_u	hash[UNDO_HASH_SIZE];
2607 
2608 	sha256_finish(&sha_ctx, hash);
2609 	u_read_undo(NULL, hash, fname);
2610     }
2611 #endif
2612 
2613     if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
2614     {
2615 	int m = msg_scroll;
2616 	int n = msg_scrolled;
2617 
2618 	/* Save the fileformat now, otherwise the buffer will be considered
2619 	 * modified if the format/encoding was automatically detected. */
2620 	if (set_options)
2621 	    save_file_ff(curbuf);
2622 
2623 	/*
2624 	 * The output from the autocommands should not overwrite anything and
2625 	 * should not be overwritten: Set msg_scroll, restore its value if no
2626 	 * output was done.
2627 	 */
2628 	msg_scroll = TRUE;
2629 	if (filtering)
2630 	    apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2631 							  FALSE, curbuf, eap);
2632 	else if (newfile || (read_buffer && sfname != NULL))
2633 	{
2634 	    apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2635 							  FALSE, curbuf, eap);
2636 	    if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2637 		/*
2638 		 * EVENT_FILETYPE was not triggered but the buffer already has a
2639 		 * filetype. Trigger EVENT_FILETYPE using the existing filetype.
2640 		 */
2641 		apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2642 			TRUE, curbuf);
2643 	}
2644 	else
2645 	    apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2646 							    FALSE, NULL, eap);
2647 	if (msg_scrolled == n)
2648 	    msg_scroll = m;
2649 # ifdef FEAT_EVAL
2650 	if (aborting())	    /* autocmds may abort script processing */
2651 	    return FAIL;
2652 # endif
2653     }
2654 
2655     if (recoverymode && error)
2656 	return FAIL;
2657     return OK;
2658 }
2659 
2660 #if defined(OPEN_CHR_FILES) || defined(PROTO)
2661 /*
2662  * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2663  * which is the name of files used for process substitution output by
2664  * some shells on some operating systems, e.g., bash on SunOS.
2665  * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2666  */
2667     int
2668 is_dev_fd_file(char_u *fname)
2669 {
2670     return (STRNCMP(fname, "/dev/fd/", 8) == 0
2671 	    && VIM_ISDIGIT(fname[8])
2672 	    && *skipdigits(fname + 9) == NUL
2673 	    && (fname[9] != NUL
2674 		|| (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2675 }
2676 #endif
2677 
2678 /*
2679  * From the current line count and characters read after that, estimate the
2680  * line number where we are now.
2681  * Used for error messages that include a line number.
2682  */
2683     static linenr_T
2684 readfile_linenr(
2685     linenr_T	linecnt,	/* line count before reading more bytes */
2686     char_u	*p,		/* start of more bytes read */
2687     char_u	*endp)		/* end of more bytes read */
2688 {
2689     char_u	*s;
2690     linenr_T	lnum;
2691 
2692     lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2693     for (s = p; s < endp; ++s)
2694 	if (*s == '\n')
2695 	    ++lnum;
2696     return lnum;
2697 }
2698 
2699 /*
2700  * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2701  * equal to the buffer "buf".  Used for calling readfile().
2702  * Returns OK or FAIL.
2703  */
2704     int
2705 prep_exarg(exarg_T *eap, buf_T *buf)
2706 {
2707     eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc));
2708     if (eap->cmd == NULL)
2709 	return FAIL;
2710 
2711     sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc);
2712     eap->force_enc = 8;
2713     eap->bad_char = buf->b_bad_char;
2714     eap->force_ff = *buf->b_p_ff;
2715 
2716     eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
2717     eap->read_edit = FALSE;
2718     eap->forceit = FALSE;
2719     return OK;
2720 }
2721 
2722 /*
2723  * Set default or forced 'fileformat' and 'binary'.
2724  */
2725     void
2726 set_file_options(int set_options, exarg_T *eap)
2727 {
2728     /* set default 'fileformat' */
2729     if (set_options)
2730     {
2731 	if (eap != NULL && eap->force_ff != 0)
2732 	    set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
2733 	else if (*p_ffs != NUL)
2734 	    set_fileformat(default_fileformat(), OPT_LOCAL);
2735     }
2736 
2737     /* set or reset 'binary' */
2738     if (eap != NULL && eap->force_bin != 0)
2739     {
2740 	int	oldval = curbuf->b_p_bin;
2741 
2742 	curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
2743 	set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
2744     }
2745 }
2746 
2747 /*
2748  * Set forced 'fileencoding'.
2749  */
2750     void
2751 set_forced_fenc(exarg_T *eap)
2752 {
2753     if (eap->force_enc != 0)
2754     {
2755 	char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
2756 
2757 	if (fenc != NULL)
2758 	    set_string_option_direct((char_u *)"fenc", -1,
2759 				 fenc, OPT_FREE|OPT_LOCAL, 0);
2760 	vim_free(fenc);
2761     }
2762 }
2763 
2764 /*
2765  * Find next fileencoding to use from 'fileencodings'.
2766  * "pp" points to fenc_next.  It's advanced to the next item.
2767  * When there are no more items, an empty string is returned and *pp is set to
2768  * NULL.
2769  * When *pp is not set to NULL, the result is in allocated memory.
2770  */
2771     static char_u *
2772 next_fenc(char_u **pp)
2773 {
2774     char_u	*p;
2775     char_u	*r;
2776 
2777     if (**pp == NUL)
2778     {
2779 	*pp = NULL;
2780 	return (char_u *)"";
2781     }
2782     p = vim_strchr(*pp, ',');
2783     if (p == NULL)
2784     {
2785 	r = enc_canonize(*pp);
2786 	*pp += STRLEN(*pp);
2787     }
2788     else
2789     {
2790 	r = vim_strnsave(*pp, (int)(p - *pp));
2791 	*pp = p + 1;
2792 	if (r != NULL)
2793 	{
2794 	    p = enc_canonize(r);
2795 	    vim_free(r);
2796 	    r = p;
2797 	}
2798     }
2799     if (r == NULL)	/* out of memory */
2800     {
2801 	r = (char_u *)"";
2802 	*pp = NULL;
2803     }
2804     return r;
2805 }
2806 
2807 #ifdef FEAT_EVAL
2808 /*
2809  * Convert a file with the 'charconvert' expression.
2810  * This closes the file which is to be read, converts it and opens the
2811  * resulting file for reading.
2812  * Returns name of the resulting converted file (the caller should delete it
2813  * after reading it).
2814  * Returns NULL if the conversion failed ("*fdp" is not set) .
2815  */
2816     static char_u *
2817 readfile_charconvert(
2818     char_u	*fname,		/* name of input file */
2819     char_u	*fenc,		/* converted from */
2820     int		*fdp)		/* in/out: file descriptor of file */
2821 {
2822     char_u	*tmpname;
2823     char	*errmsg = NULL;
2824 
2825     tmpname = vim_tempname('r', FALSE);
2826     if (tmpname == NULL)
2827 	errmsg = _("Can't find temp file for conversion");
2828     else
2829     {
2830 	close(*fdp);		/* close the input file, ignore errors */
2831 	*fdp = -1;
2832 	if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2833 						      fname, tmpname) == FAIL)
2834 	    errmsg = _("Conversion with 'charconvert' failed");
2835 	if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2836 						  O_RDONLY | O_EXTRA, 0)) < 0)
2837 	    errmsg = _("can't read output of 'charconvert'");
2838     }
2839 
2840     if (errmsg != NULL)
2841     {
2842 	/* Don't use emsg(), it breaks mappings, the retry with
2843 	 * another type of conversion might still work. */
2844 	msg(errmsg);
2845 	if (tmpname != NULL)
2846 	{
2847 	    mch_remove(tmpname);	/* delete converted file */
2848 	    VIM_CLEAR(tmpname);
2849 	}
2850     }
2851 
2852     /* If the input file is closed, open it (caller should check for error). */
2853     if (*fdp < 0)
2854 	*fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2855 
2856     return tmpname;
2857 }
2858 #endif
2859 
2860 
2861 #ifdef FEAT_VIMINFO
2862 /*
2863  * Read marks for the current buffer from the viminfo file, when we support
2864  * buffer marks and the buffer has a name.
2865  */
2866     static void
2867 check_marks_read(void)
2868 {
2869     if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
2870 						  && curbuf->b_ffname != NULL)
2871 	read_viminfo(NULL, VIF_WANT_MARKS);
2872 
2873     /* Always set b_marks_read; needed when 'viminfo' is changed to include
2874      * the ' parameter after opening a buffer. */
2875     curbuf->b_marks_read = TRUE;
2876 }
2877 #endif
2878 
2879 #if defined(FEAT_CRYPT) || defined(PROTO)
2880 /*
2881  * Check for magic number used for encryption.  Applies to the current buffer.
2882  * If found, the magic number is removed from ptr[*sizep] and *sizep and
2883  * *filesizep are updated.
2884  * Return the (new) encryption key, NULL for no encryption.
2885  */
2886     static char_u *
2887 check_for_cryptkey(
2888     char_u	*cryptkey,	/* previous encryption key or NULL */
2889     char_u	*ptr,		/* pointer to read bytes */
2890     long	*sizep,		/* length of read bytes */
2891     off_T	*filesizep,	/* nr of bytes used from file */
2892     int		newfile,	/* editing a new buffer */
2893     char_u	*fname,		/* file name to display */
2894     int		*did_ask)	/* flag: whether already asked for key */
2895 {
2896     int method = crypt_method_nr_from_magic((char *)ptr, *sizep);
2897     int b_p_ro = curbuf->b_p_ro;
2898 
2899     if (method >= 0)
2900     {
2901 	/* Mark the buffer as read-only until the decryption has taken place.
2902 	 * Avoids accidentally overwriting the file with garbage. */
2903 	curbuf->b_p_ro = TRUE;
2904 
2905 	/* Set the cryptmethod local to the buffer. */
2906 	crypt_set_cm_option(curbuf, method);
2907 	if (cryptkey == NULL && !*did_ask)
2908 	{
2909 	    if (*curbuf->b_p_key)
2910 		cryptkey = curbuf->b_p_key;
2911 	    else
2912 	    {
2913 		/* When newfile is TRUE, store the typed key in the 'key'
2914 		 * option and don't free it.  bf needs hash of the key saved.
2915 		 * Don't ask for the key again when first time Enter was hit.
2916 		 * Happens when retrying to detect encoding. */
2917 		smsg(_(need_key_msg), fname);
2918 		msg_scroll = TRUE;
2919 		crypt_check_method(method);
2920 		cryptkey = crypt_get_key(newfile, FALSE);
2921 		*did_ask = TRUE;
2922 
2923 		/* check if empty key entered */
2924 		if (cryptkey != NULL && *cryptkey == NUL)
2925 		{
2926 		    if (cryptkey != curbuf->b_p_key)
2927 			vim_free(cryptkey);
2928 		    cryptkey = NULL;
2929 		}
2930 	    }
2931 	}
2932 
2933 	if (cryptkey != NULL)
2934 	{
2935 	    int header_len;
2936 
2937 	    curbuf->b_cryptstate = crypt_create_from_header(
2938 						       method, cryptkey, ptr);
2939 	    crypt_set_cm_option(curbuf, method);
2940 
2941 	    /* Remove cryptmethod specific header from the text. */
2942 	    header_len = crypt_get_header_len(method);
2943 	    if (*sizep <= header_len)
2944 		/* invalid header, buffer can't be encrypted */
2945 		return NULL;
2946 	    *filesizep += header_len;
2947 	    *sizep -= header_len;
2948 	    mch_memmove(ptr, ptr + header_len, (size_t)*sizep);
2949 
2950 	    /* Restore the read-only flag. */
2951 	    curbuf->b_p_ro = b_p_ro;
2952 	}
2953     }
2954     /* When starting to edit a new file which does not have encryption, clear
2955      * the 'key' option, except when starting up (called with -x argument) */
2956     else if (newfile && *curbuf->b_p_key != NUL && !starting)
2957 	set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2958 
2959     return cryptkey;
2960 }
2961 #endif  /* FEAT_CRYPT */
2962 
2963 #ifdef UNIX
2964     static void
2965 set_file_time(
2966     char_u  *fname,
2967     time_t  atime,	    /* access time */
2968     time_t  mtime)	    /* modification time */
2969 {
2970 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
2971     struct utimbuf  buf;
2972 
2973     buf.actime	= atime;
2974     buf.modtime	= mtime;
2975     (void)utime((char *)fname, &buf);
2976 # else
2977 #  if defined(HAVE_UTIMES)
2978     struct timeval  tvp[2];
2979 
2980     tvp[0].tv_sec   = atime;
2981     tvp[0].tv_usec  = 0;
2982     tvp[1].tv_sec   = mtime;
2983     tvp[1].tv_usec  = 0;
2984 #   ifdef NeXT
2985     (void)utimes((char *)fname, tvp);
2986 #   else
2987     (void)utimes((char *)fname, (const struct timeval *)&tvp);
2988 #   endif
2989 #  endif
2990 # endif
2991 }
2992 #endif /* UNIX */
2993 
2994 #if defined(VMS) && !defined(MIN)
2995 /* Older DECC compiler for VAX doesn't define MIN() */
2996 # define MIN(a, b) ((a) < (b) ? (a) : (b))
2997 #endif
2998 
2999 /*
3000  * Return TRUE if a file appears to be read-only from the file permissions.
3001  */
3002     int
3003 check_file_readonly(
3004     char_u	*fname,		/* full path to file */
3005     int		perm)		/* known permissions on file */
3006 {
3007 #ifndef USE_MCH_ACCESS
3008     int	    fd = 0;
3009 #endif
3010 
3011     return (
3012 #ifdef USE_MCH_ACCESS
3013 # ifdef UNIX
3014 	(perm & 0222) == 0 ||
3015 # endif
3016 	mch_access((char *)fname, W_OK)
3017 #else
3018 	(fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3019 					? TRUE : (close(fd), FALSE)
3020 #endif
3021 	);
3022 }
3023 
3024 
3025 /*
3026  * buf_write() - write to file "fname" lines "start" through "end"
3027  *
3028  * We do our own buffering here because fwrite() is so slow.
3029  *
3030  * If "forceit" is true, we don't care for errors when attempting backups.
3031  * In case of an error everything possible is done to restore the original
3032  * file.  But when "forceit" is TRUE, we risk losing it.
3033  *
3034  * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and
3035  * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
3036  *
3037  * This function must NOT use NameBuff (because it's called by autowrite()).
3038  *
3039  * return FAIL for failure, OK otherwise
3040  */
3041     int
3042 buf_write(
3043     buf_T	    *buf,
3044     char_u	    *fname,
3045     char_u	    *sfname,
3046     linenr_T	    start,
3047     linenr_T	    end,
3048     exarg_T	    *eap,		/* for forced 'ff' and 'fenc', can be
3049 					   NULL! */
3050     int		    append,		/* append to the file */
3051     int		    forceit,
3052     int		    reset_changed,
3053     int		    filtering)
3054 {
3055     int		    fd;
3056     char_u	    *backup = NULL;
3057     int		    backup_copy = FALSE; /* copy the original file? */
3058     int		    dobackup;
3059     char_u	    *ffname;
3060     char_u	    *wfname = NULL;	/* name of file to write to */
3061     char_u	    *s;
3062     char_u	    *ptr;
3063     char_u	    c;
3064     int		    len;
3065     linenr_T	    lnum;
3066     long	    nchars;
3067     char_u	    *errmsg = NULL;
3068     int		    errmsg_allocated = FALSE;
3069     char_u	    *errnum = NULL;
3070     char_u	    *buffer;
3071     char_u	    smallbuf[SMBUFSIZE];
3072     char_u	    *backup_ext;
3073     int		    bufsize;
3074     long	    perm;		    /* file permissions */
3075     int		    retval = OK;
3076     int		    newfile = FALSE;	    /* TRUE if file doesn't exist yet */
3077     int		    msg_save = msg_scroll;
3078     int		    overwriting;	    /* TRUE if writing over original */
3079     int		    no_eol = FALSE;	    /* no end-of-line written */
3080     int		    device = FALSE;	    /* writing to a device */
3081     stat_T	    st_old;
3082     int		    prev_got_int = got_int;
3083     int		    checking_conversion;
3084     int		    file_readonly = FALSE;  /* overwritten file is read-only */
3085     static char	    *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')";
3086 #if defined(UNIX)			    /*XXX fix me sometime? */
3087     int		    made_writable = FALSE;  /* 'w' bit has been set */
3088 #endif
3089 					/* writing everything */
3090     int		    whole = (start == 1 && end == buf->b_ml.ml_line_count);
3091     linenr_T	    old_line_count = buf->b_ml.ml_line_count;
3092     int		    attr;
3093     int		    fileformat;
3094     int		    write_bin;
3095     struct bw_info  write_info;		/* info for buf_write_bytes() */
3096     int		    converted = FALSE;
3097     int		    notconverted = FALSE;
3098     char_u	    *fenc;		/* effective 'fileencoding' */
3099     char_u	    *fenc_tofree = NULL; /* allocated "fenc" */
3100 #ifdef HAS_BW_FLAGS
3101     int		    wb_flags = 0;
3102 #endif
3103 #ifdef HAVE_ACL
3104     vim_acl_T	    acl = NULL;		/* ACL copied from original file to
3105 					   backup or new file */
3106 #endif
3107 #ifdef FEAT_PERSISTENT_UNDO
3108     int		    write_undo_file = FALSE;
3109     context_sha256_T sha_ctx;
3110 #endif
3111     unsigned int    bkc = get_bkc_value(buf);
3112 
3113     if (fname == NULL || *fname == NUL)	/* safety check */
3114 	return FAIL;
3115     if (buf->b_ml.ml_mfp == NULL)
3116     {
3117 	/* This can happen during startup when there is a stray "w" in the
3118 	 * vimrc file. */
3119 	emsg(_(e_emptybuf));
3120 	return FAIL;
3121     }
3122 
3123     /*
3124      * Disallow writing from .exrc and .vimrc in current directory for
3125      * security reasons.
3126      */
3127     if (check_secure())
3128 	return FAIL;
3129 
3130     /* Avoid a crash for a long name. */
3131     if (STRLEN(fname) >= MAXPATHL)
3132     {
3133 	emsg(_(e_longname));
3134 	return FAIL;
3135     }
3136 
3137     /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */
3138     write_info.bw_conv_buf = NULL;
3139     write_info.bw_conv_error = FALSE;
3140     write_info.bw_conv_error_lnum = 0;
3141     write_info.bw_restlen = 0;
3142 #ifdef USE_ICONV
3143     write_info.bw_iconv_fd = (iconv_t)-1;
3144 #endif
3145 #ifdef FEAT_CRYPT
3146     write_info.bw_buffer = buf;
3147 #endif
3148 
3149     /* After writing a file changedtick changes but we don't want to display
3150      * the line. */
3151     ex_no_reprint = TRUE;
3152 
3153     /*
3154      * If there is no file name yet, use the one for the written file.
3155      * BF_NOTEDITED is set to reflect this (in case the write fails).
3156      * Don't do this when the write is for a filter command.
3157      * Don't do this when appending.
3158      * Only do this when 'cpoptions' contains the 'F' flag.
3159      */
3160     if (buf->b_ffname == NULL
3161 	    && reset_changed
3162 	    && whole
3163 	    && buf == curbuf
3164 #ifdef FEAT_QUICKFIX
3165 	    && !bt_nofile(buf)
3166 #endif
3167 	    && !filtering
3168 	    && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
3169 	    && vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
3170     {
3171 	if (set_rw_fname(fname, sfname) == FAIL)
3172 	    return FAIL;
3173 	buf = curbuf;	    /* just in case autocmds made "buf" invalid */
3174     }
3175 
3176     if (sfname == NULL)
3177 	sfname = fname;
3178     /*
3179      * For Unix: Use the short file name whenever possible.
3180      * Avoids problems with networks and when directory names are changed.
3181      * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
3182      * another directory, which we don't detect
3183      */
3184     ffname = fname;			    /* remember full fname */
3185 #ifdef UNIX
3186     fname = sfname;
3187 #endif
3188 
3189     if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
3190 	overwriting = TRUE;
3191     else
3192 	overwriting = FALSE;
3193 
3194     if (exiting)
3195 	settmode(TMODE_COOK);	    /* when exiting allow typeahead now */
3196 
3197     ++no_wait_return;		    /* don't wait for return yet */
3198 
3199     /*
3200      * Set '[ and '] marks to the lines to be written.
3201      */
3202     buf->b_op_start.lnum = start;
3203     buf->b_op_start.col = 0;
3204     buf->b_op_end.lnum = end;
3205     buf->b_op_end.col = 0;
3206 
3207     {
3208 	aco_save_T	aco;
3209 	int		buf_ffname = FALSE;
3210 	int		buf_sfname = FALSE;
3211 	int		buf_fname_f = FALSE;
3212 	int		buf_fname_s = FALSE;
3213 	int		did_cmd = FALSE;
3214 	int		nofile_err = FALSE;
3215 	int		empty_memline = (buf->b_ml.ml_mfp == NULL);
3216 	bufref_T	bufref;
3217 
3218 	/*
3219 	 * Apply PRE autocommands.
3220 	 * Set curbuf to the buffer to be written.
3221 	 * Careful: The autocommands may call buf_write() recursively!
3222 	 */
3223 	if (ffname == buf->b_ffname)
3224 	    buf_ffname = TRUE;
3225 	if (sfname == buf->b_sfname)
3226 	    buf_sfname = TRUE;
3227 	if (fname == buf->b_ffname)
3228 	    buf_fname_f = TRUE;
3229 	if (fname == buf->b_sfname)
3230 	    buf_fname_s = TRUE;
3231 
3232 	/* set curwin/curbuf to buf and save a few things */
3233 	aucmd_prepbuf(&aco, buf);
3234 	set_bufref(&bufref, buf);
3235 
3236 	if (append)
3237 	{
3238 	    if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
3239 					 sfname, sfname, FALSE, curbuf, eap)))
3240 	    {
3241 #ifdef FEAT_QUICKFIX
3242 		if (overwriting && bt_nofile(curbuf))
3243 		    nofile_err = TRUE;
3244 		else
3245 #endif
3246 		    apply_autocmds_exarg(EVENT_FILEAPPENDPRE,
3247 					  sfname, sfname, FALSE, curbuf, eap);
3248 	    }
3249 	}
3250 	else if (filtering)
3251 	{
3252 	    apply_autocmds_exarg(EVENT_FILTERWRITEPRE,
3253 					    NULL, sfname, FALSE, curbuf, eap);
3254 	}
3255 	else if (reset_changed && whole)
3256 	{
3257 	    int was_changed = curbufIsChanged();
3258 
3259 	    did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD,
3260 					  sfname, sfname, FALSE, curbuf, eap);
3261 	    if (did_cmd)
3262 	    {
3263 		if (was_changed && !curbufIsChanged())
3264 		{
3265 		    /* Written everything correctly and BufWriteCmd has reset
3266 		     * 'modified': Correct the undo information so that an
3267 		     * undo now sets 'modified'. */
3268 		    u_unchanged(curbuf);
3269 		    u_update_save_nr(curbuf);
3270 		}
3271 	    }
3272 	    else
3273 	    {
3274 #ifdef FEAT_QUICKFIX
3275 		if (overwriting && bt_nofile(curbuf))
3276 		    nofile_err = TRUE;
3277 		else
3278 #endif
3279 		    apply_autocmds_exarg(EVENT_BUFWRITEPRE,
3280 					  sfname, sfname, FALSE, curbuf, eap);
3281 	    }
3282 	}
3283 	else
3284 	{
3285 	    if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD,
3286 					 sfname, sfname, FALSE, curbuf, eap)))
3287 	    {
3288 #ifdef FEAT_QUICKFIX
3289 		if (overwriting && bt_nofile(curbuf))
3290 		    nofile_err = TRUE;
3291 		else
3292 #endif
3293 		    apply_autocmds_exarg(EVENT_FILEWRITEPRE,
3294 					  sfname, sfname, FALSE, curbuf, eap);
3295 	    }
3296 	}
3297 
3298 	/* restore curwin/curbuf and a few other things */
3299 	aucmd_restbuf(&aco);
3300 
3301 	/*
3302 	 * In three situations we return here and don't write the file:
3303 	 * 1. the autocommands deleted or unloaded the buffer.
3304 	 * 2. The autocommands abort script processing.
3305 	 * 3. If one of the "Cmd" autocommands was executed.
3306 	 */
3307 	if (!bufref_valid(&bufref))
3308 	    buf = NULL;
3309 	if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
3310 				       || did_cmd || nofile_err
3311 #ifdef FEAT_EVAL
3312 				       || aborting()
3313 #endif
3314 				       )
3315 	{
3316 	    --no_wait_return;
3317 	    msg_scroll = msg_save;
3318 	    if (nofile_err)
3319 		emsg(_("E676: No matching autocommands for acwrite buffer"));
3320 
3321 	    if (nofile_err
3322 #ifdef FEAT_EVAL
3323 		    || aborting()
3324 #endif
3325 		    )
3326 		/* An aborting error, interrupt or exception in the
3327 		 * autocommands. */
3328 		return FAIL;
3329 	    if (did_cmd)
3330 	    {
3331 		if (buf == NULL)
3332 		    /* The buffer was deleted.  We assume it was written
3333 		     * (can't retry anyway). */
3334 		    return OK;
3335 		if (overwriting)
3336 		{
3337 		    /* Assume the buffer was written, update the timestamp. */
3338 		    ml_timestamp(buf);
3339 		    if (append)
3340 			buf->b_flags &= ~BF_NEW;
3341 		    else
3342 			buf->b_flags &= ~BF_WRITE_MASK;
3343 		}
3344 		if (reset_changed && buf->b_changed && !append
3345 			&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
3346 		    /* Buffer still changed, the autocommands didn't work
3347 		     * properly. */
3348 		    return FAIL;
3349 		return OK;
3350 	    }
3351 #ifdef FEAT_EVAL
3352 	    if (!aborting())
3353 #endif
3354 		emsg(_("E203: Autocommands deleted or unloaded buffer to be written"));
3355 	    return FAIL;
3356 	}
3357 
3358 	/*
3359 	 * The autocommands may have changed the number of lines in the file.
3360 	 * When writing the whole file, adjust the end.
3361 	 * When writing part of the file, assume that the autocommands only
3362 	 * changed the number of lines that are to be written (tricky!).
3363 	 */
3364 	if (buf->b_ml.ml_line_count != old_line_count)
3365 	{
3366 	    if (whole)						/* write all */
3367 		end = buf->b_ml.ml_line_count;
3368 	    else if (buf->b_ml.ml_line_count > old_line_count)	/* more lines */
3369 		end += buf->b_ml.ml_line_count - old_line_count;
3370 	    else						/* less lines */
3371 	    {
3372 		end -= old_line_count - buf->b_ml.ml_line_count;
3373 		if (end < start)
3374 		{
3375 		    --no_wait_return;
3376 		    msg_scroll = msg_save;
3377 		    emsg(_("E204: Autocommand changed number of lines in unexpected way"));
3378 		    return FAIL;
3379 		}
3380 	    }
3381 	}
3382 
3383 	/*
3384 	 * The autocommands may have changed the name of the buffer, which may
3385 	 * be kept in fname, ffname and sfname.
3386 	 */
3387 	if (buf_ffname)
3388 	    ffname = buf->b_ffname;
3389 	if (buf_sfname)
3390 	    sfname = buf->b_sfname;
3391 	if (buf_fname_f)
3392 	    fname = buf->b_ffname;
3393 	if (buf_fname_s)
3394 	    fname = buf->b_sfname;
3395     }
3396 
3397 #ifdef FEAT_NETBEANS_INTG
3398     if (netbeans_active() && isNetbeansBuffer(buf))
3399     {
3400 	if (whole)
3401 	{
3402 	    /*
3403 	     * b_changed can be 0 after an undo, but we still need to write
3404 	     * the buffer to NetBeans.
3405 	     */
3406 	    if (buf->b_changed || isNetbeansModified(buf))
3407 	    {
3408 		--no_wait_return;		/* may wait for return now */
3409 		msg_scroll = msg_save;
3410 		netbeans_save_buffer(buf);	/* no error checking... */
3411 		return retval;
3412 	    }
3413 	    else
3414 	    {
3415 		errnum = (char_u *)"E656: ";
3416 		errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers");
3417 		buffer = NULL;
3418 		goto fail;
3419 	    }
3420 	}
3421 	else
3422 	{
3423 	    errnum = (char_u *)"E657: ";
3424 	    errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers");
3425 	    buffer = NULL;
3426 	    goto fail;
3427 	}
3428     }
3429 #endif
3430 
3431     if (shortmess(SHM_OVER) && !exiting)
3432 	msg_scroll = FALSE;	    /* overwrite previous file message */
3433     else
3434 	msg_scroll = TRUE;	    /* don't overwrite previous file message */
3435     if (!filtering)
3436 	filemess(buf,
3437 #ifndef UNIX
3438 		sfname,
3439 #else
3440 		fname,
3441 #endif
3442 		    (char_u *)"", 0);	/* show that we are busy */
3443     msg_scroll = FALSE;		    /* always overwrite the file message now */
3444 
3445     buffer = alloc(BUFSIZE);
3446     if (buffer == NULL)		    /* can't allocate big buffer, use small
3447 				     * one (to be able to write when out of
3448 				     * memory) */
3449     {
3450 	buffer = smallbuf;
3451 	bufsize = SMBUFSIZE;
3452     }
3453     else
3454 	bufsize = BUFSIZE;
3455 
3456     /*
3457      * Get information about original file (if there is one).
3458      */
3459 #if defined(UNIX)
3460     st_old.st_dev = 0;
3461     st_old.st_ino = 0;
3462     perm = -1;
3463     if (mch_stat((char *)fname, &st_old) < 0)
3464 	newfile = TRUE;
3465     else
3466     {
3467 	perm = st_old.st_mode;
3468 	if (!S_ISREG(st_old.st_mode))		/* not a file */
3469 	{
3470 	    if (S_ISDIR(st_old.st_mode))
3471 	    {
3472 		errnum = (char_u *)"E502: ";
3473 		errmsg = (char_u *)_("is a directory");
3474 		goto fail;
3475 	    }
3476 	    if (mch_nodetype(fname) != NODE_WRITABLE)
3477 	    {
3478 		errnum = (char_u *)"E503: ";
3479 		errmsg = (char_u *)_("is not a file or writable device");
3480 		goto fail;
3481 	    }
3482 	    /* It's a device of some kind (or a fifo) which we can write to
3483 	     * but for which we can't make a backup. */
3484 	    device = TRUE;
3485 	    newfile = TRUE;
3486 	    perm = -1;
3487 	}
3488     }
3489 #else /* !UNIX */
3490     /*
3491      * Check for a writable device name.
3492      */
3493     c = mch_nodetype(fname);
3494     if (c == NODE_OTHER)
3495     {
3496 	errnum = (char_u *)"E503: ";
3497 	errmsg = (char_u *)_("is not a file or writable device");
3498 	goto fail;
3499     }
3500     if (c == NODE_WRITABLE)
3501     {
3502 # if defined(MSWIN)
3503 	/* MS-Windows allows opening a device, but we will probably get stuck
3504 	 * trying to write to it.  */
3505 	if (!p_odev)
3506 	{
3507 	    errnum = (char_u *)"E796: ";
3508 	    errmsg = (char_u *)_("writing to device disabled with 'opendevice' option");
3509 	    goto fail;
3510 	}
3511 # endif
3512 	device = TRUE;
3513 	newfile = TRUE;
3514 	perm = -1;
3515     }
3516     else
3517     {
3518 	perm = mch_getperm(fname);
3519 	if (perm < 0)
3520 	    newfile = TRUE;
3521 	else if (mch_isdir(fname))
3522 	{
3523 	    errnum = (char_u *)"E502: ";
3524 	    errmsg = (char_u *)_("is a directory");
3525 	    goto fail;
3526 	}
3527 	if (overwriting)
3528 	    (void)mch_stat((char *)fname, &st_old);
3529     }
3530 #endif /* !UNIX */
3531 
3532     if (!device && !newfile)
3533     {
3534 	/*
3535 	 * Check if the file is really writable (when renaming the file to
3536 	 * make a backup we won't discover it later).
3537 	 */
3538 	file_readonly = check_file_readonly(fname, (int)perm);
3539 
3540 	if (!forceit && file_readonly)
3541 	{
3542 	    if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
3543 	    {
3544 		errnum = (char_u *)"E504: ";
3545 		errmsg = (char_u *)_(err_readonly);
3546 	    }
3547 	    else
3548 	    {
3549 		errnum = (char_u *)"E505: ";
3550 		errmsg = (char_u *)_("is read-only (add ! to override)");
3551 	    }
3552 	    goto fail;
3553 	}
3554 
3555 	/*
3556 	 * Check if the timestamp hasn't changed since reading the file.
3557 	 */
3558 	if (overwriting)
3559 	{
3560 	    retval = check_mtime(buf, &st_old);
3561 	    if (retval == FAIL)
3562 		goto fail;
3563 	}
3564     }
3565 
3566 #ifdef HAVE_ACL
3567     /*
3568      * For systems that support ACL: get the ACL from the original file.
3569      */
3570     if (!newfile)
3571 	acl = mch_get_acl(fname);
3572 #endif
3573 
3574     /*
3575      * If 'backupskip' is not empty, don't make a backup for some files.
3576      */
3577     dobackup = (p_wb || p_bk || *p_pm != NUL);
3578 #ifdef FEAT_WILDIGN
3579     if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname))
3580 	dobackup = FALSE;
3581 #endif
3582 
3583     /*
3584      * Save the value of got_int and reset it.  We don't want a previous
3585      * interruption cancel writing, only hitting CTRL-C while writing should
3586      * abort it.
3587      */
3588     prev_got_int = got_int;
3589     got_int = FALSE;
3590 
3591     /* Mark the buffer as 'being saved' to prevent changed buffer warnings */
3592     buf->b_saving = TRUE;
3593 
3594     /*
3595      * If we are not appending or filtering, the file exists, and the
3596      * 'writebackup', 'backup' or 'patchmode' option is set, need a backup.
3597      * When 'patchmode' is set also make a backup when appending.
3598      *
3599      * Do not make any backup, if 'writebackup' and 'backup' are both switched
3600      * off.  This helps when editing large files on almost-full disks.
3601      */
3602     if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup)
3603     {
3604 #if defined(UNIX) || defined(WIN32)
3605 	stat_T	    st;
3606 #endif
3607 
3608 	if ((bkc & BKC_YES) || append)	/* "yes" */
3609 	    backup_copy = TRUE;
3610 #if defined(UNIX) || defined(WIN32)
3611 	else if ((bkc & BKC_AUTO))	/* "auto" */
3612 	{
3613 	    int		i;
3614 
3615 # ifdef UNIX
3616 	    /*
3617 	     * Don't rename the file when:
3618 	     * - it's a hard link
3619 	     * - it's a symbolic link
3620 	     * - we don't have write permission in the directory
3621 	     * - we can't set the owner/group of the new file
3622 	     */
3623 	    if (st_old.st_nlink > 1
3624 		    || mch_lstat((char *)fname, &st) < 0
3625 		    || st.st_dev != st_old.st_dev
3626 		    || st.st_ino != st_old.st_ino
3627 #  ifndef HAVE_FCHOWN
3628 		    || st.st_uid != st_old.st_uid
3629 		    || st.st_gid != st_old.st_gid
3630 #  endif
3631 		    )
3632 		backup_copy = TRUE;
3633 	    else
3634 # else
3635 #  ifdef WIN32
3636 	    /* On NTFS file systems hard links are possible. */
3637 	    if (mch_is_linked(fname))
3638 		backup_copy = TRUE;
3639 	    else
3640 #  endif
3641 # endif
3642 	    {
3643 		/*
3644 		 * Check if we can create a file and set the owner/group to
3645 		 * the ones from the original file.
3646 		 * First find a file name that doesn't exist yet (use some
3647 		 * arbitrary numbers).
3648 		 */
3649 		STRCPY(IObuff, fname);
3650 		for (i = 4913; ; i += 123)
3651 		{
3652 		    sprintf((char *)gettail(IObuff), "%d", i);
3653 		    if (mch_lstat((char *)IObuff, &st) < 0)
3654 			break;
3655 		}
3656 		fd = mch_open((char *)IObuff,
3657 				    O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
3658 		if (fd < 0)	/* can't write in directory */
3659 		    backup_copy = TRUE;
3660 		else
3661 		{
3662 # ifdef UNIX
3663 #  ifdef HAVE_FCHOWN
3664 		    vim_ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
3665 #  endif
3666 		    if (mch_stat((char *)IObuff, &st) < 0
3667 			    || st.st_uid != st_old.st_uid
3668 			    || st.st_gid != st_old.st_gid
3669 			    || (long)st.st_mode != perm)
3670 			backup_copy = TRUE;
3671 # endif
3672 		    /* Close the file before removing it, on MS-Windows we
3673 		     * can't delete an open file. */
3674 		    close(fd);
3675 		    mch_remove(IObuff);
3676 # ifdef MSWIN
3677 		    /* MS-Windows may trigger a virus scanner to open the
3678 		     * file, we can't delete it then.  Keep trying for half a
3679 		     * second. */
3680 		    {
3681 			int try;
3682 
3683 			for (try = 0; try < 10; ++try)
3684 			{
3685 			    if (mch_lstat((char *)IObuff, &st) < 0)
3686 				break;
3687 			    ui_delay(50L, TRUE);  /* wait 50 msec */
3688 			    mch_remove(IObuff);
3689 			}
3690 		    }
3691 # endif
3692 		}
3693 	    }
3694 	}
3695 
3696 	/*
3697 	 * Break symlinks and/or hardlinks if we've been asked to.
3698 	 */
3699 	if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK))
3700 	{
3701 # ifdef UNIX
3702 	    int	lstat_res;
3703 
3704 	    lstat_res = mch_lstat((char *)fname, &st);
3705 
3706 	    /* Symlinks. */
3707 	    if ((bkc & BKC_BREAKSYMLINK)
3708 		    && lstat_res == 0
3709 		    && st.st_ino != st_old.st_ino)
3710 		backup_copy = FALSE;
3711 
3712 	    /* Hardlinks. */
3713 	    if ((bkc & BKC_BREAKHARDLINK)
3714 		    && st_old.st_nlink > 1
3715 		    && (lstat_res != 0 || st.st_ino == st_old.st_ino))
3716 		backup_copy = FALSE;
3717 # else
3718 #  if defined(WIN32)
3719 	    /* Symlinks. */
3720 	    if ((bkc & BKC_BREAKSYMLINK) && mch_is_symbolic_link(fname))
3721 		backup_copy = FALSE;
3722 
3723 	    /* Hardlinks. */
3724 	    if ((bkc & BKC_BREAKHARDLINK) && mch_is_hard_link(fname))
3725 		backup_copy = FALSE;
3726 #  endif
3727 # endif
3728 	}
3729 
3730 #endif
3731 
3732 	/* make sure we have a valid backup extension to use */
3733 	if (*p_bex == NUL)
3734 	    backup_ext = (char_u *)".bak";
3735 	else
3736 	    backup_ext = p_bex;
3737 
3738 	if (backup_copy
3739 		&& (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
3740 	{
3741 	    int		bfd;
3742 	    char_u	*copybuf, *wp;
3743 	    int		some_error = FALSE;
3744 	    stat_T	st_new;
3745 	    char_u	*dirp;
3746 	    char_u	*rootname;
3747 #if defined(UNIX) || defined(WIN3264)
3748 	    char_u      *p;
3749 #endif
3750 #if defined(UNIX)
3751 	    int		did_set_shortname;
3752 	    mode_t	umask_save;
3753 #endif
3754 
3755 	    copybuf = alloc(BUFSIZE + 1);
3756 	    if (copybuf == NULL)
3757 	    {
3758 		some_error = TRUE;	    /* out of memory */
3759 		goto nobackup;
3760 	    }
3761 
3762 	    /*
3763 	     * Try to make the backup in each directory in the 'bdir' option.
3764 	     *
3765 	     * Unix semantics has it, that we may have a writable file,
3766 	     * that cannot be recreated with a simple open(..., O_CREAT, ) e.g:
3767 	     *  - the directory is not writable,
3768 	     *  - the file may be a symbolic link,
3769 	     *  - the file may belong to another user/group, etc.
3770 	     *
3771 	     * For these reasons, the existing writable file must be truncated
3772 	     * and reused. Creation of a backup COPY will be attempted.
3773 	     */
3774 	    dirp = p_bdir;
3775 	    while (*dirp)
3776 	    {
3777 #ifdef UNIX
3778 		st_new.st_ino = 0;
3779 		st_new.st_dev = 0;
3780 		st_new.st_gid = 0;
3781 #endif
3782 
3783 		/*
3784 		 * Isolate one directory name, using an entry in 'bdir'.
3785 		 */
3786 		(void)copy_option_part(&dirp, copybuf, BUFSIZE, ",");
3787 
3788 #if defined(UNIX) || defined(WIN3264)
3789 		p = copybuf + STRLEN(copybuf);
3790 		if (after_pathsep(copybuf, p) && p[-1] == p[-2])
3791 		    // Ends with '//', use full path
3792 		    if ((p = make_percent_swname(copybuf, fname)) != NULL)
3793 		    {
3794 			backup = modname(p, backup_ext, FALSE);
3795 			vim_free(p);
3796 		    }
3797 #endif
3798 		rootname = get_file_in_dir(fname, copybuf);
3799 		if (rootname == NULL)
3800 		{
3801 		    some_error = TRUE;	    /* out of memory */
3802 		    goto nobackup;
3803 		}
3804 
3805 #if defined(UNIX)
3806 		did_set_shortname = FALSE;
3807 #endif
3808 
3809 		/*
3810 		 * May try twice if 'shortname' not set.
3811 		 */
3812 		for (;;)
3813 		{
3814 		    /*
3815 		     * Make the backup file name.
3816 		     */
3817 		    if (backup == NULL)
3818 			backup = buf_modname((buf->b_p_sn || buf->b_shortname),
3819 						 rootname, backup_ext, FALSE);
3820 		    if (backup == NULL)
3821 		    {
3822 			vim_free(rootname);
3823 			some_error = TRUE;		/* out of memory */
3824 			goto nobackup;
3825 		    }
3826 
3827 		    /*
3828 		     * Check if backup file already exists.
3829 		     */
3830 		    if (mch_stat((char *)backup, &st_new) >= 0)
3831 		    {
3832 #ifdef UNIX
3833 			/*
3834 			 * Check if backup file is same as original file.
3835 			 * May happen when modname() gave the same file back.
3836 			 * E.g. silly link, or file name-length reached.
3837 			 * If we don't check here, we either ruin the file
3838 			 * when copying or erase it after writing. jw.
3839 			 */
3840 			if (st_new.st_dev == st_old.st_dev
3841 					    && st_new.st_ino == st_old.st_ino)
3842 			{
3843 			    VIM_CLEAR(backup);	/* no backup file to delete */
3844 			    /*
3845 			     * may try again with 'shortname' set
3846 			     */
3847 			    if (!(buf->b_shortname || buf->b_p_sn))
3848 			    {
3849 				buf->b_shortname = TRUE;
3850 				did_set_shortname = TRUE;
3851 				continue;
3852 			    }
3853 				/* setting shortname didn't help */
3854 			    if (did_set_shortname)
3855 				buf->b_shortname = FALSE;
3856 			    break;
3857 			}
3858 #endif
3859 
3860 			/*
3861 			 * If we are not going to keep the backup file, don't
3862 			 * delete an existing one, try to use another name.
3863 			 * Change one character, just before the extension.
3864 			 */
3865 			if (!p_bk)
3866 			{
3867 			    wp = backup + STRLEN(backup) - 1
3868 							 - STRLEN(backup_ext);
3869 			    if (wp < backup)	/* empty file name ??? */
3870 				wp = backup;
3871 			    *wp = 'z';
3872 			    while (*wp > 'a'
3873 				    && mch_stat((char *)backup, &st_new) >= 0)
3874 				--*wp;
3875 			    /* They all exist??? Must be something wrong. */
3876 			    if (*wp == 'a')
3877 				VIM_CLEAR(backup);
3878 			}
3879 		    }
3880 		    break;
3881 		}
3882 		vim_free(rootname);
3883 
3884 		/*
3885 		 * Try to create the backup file
3886 		 */
3887 		if (backup != NULL)
3888 		{
3889 		    /* remove old backup, if present */
3890 		    mch_remove(backup);
3891 		    /* Open with O_EXCL to avoid the file being created while
3892 		     * we were sleeping (symlink hacker attack?). Reset umask
3893 		     * if possible to avoid mch_setperm() below. */
3894 #ifdef UNIX
3895 		    umask_save = umask(0);
3896 #endif
3897 		    bfd = mch_open((char *)backup,
3898 				O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW,
3899 								 perm & 0777);
3900 #ifdef UNIX
3901 		    (void)umask(umask_save);
3902 #endif
3903 		    if (bfd < 0)
3904 			VIM_CLEAR(backup);
3905 		    else
3906 		    {
3907 			/* Set file protection same as original file, but
3908 			 * strip s-bit.  Only needed if umask() wasn't used
3909 			 * above. */
3910 #ifndef UNIX
3911 			(void)mch_setperm(backup, perm & 0777);
3912 #else
3913 			/*
3914 			 * Try to set the group of the backup same as the
3915 			 * original file. If this fails, set the protection
3916 			 * bits for the group same as the protection bits for
3917 			 * others.
3918 			 */
3919 			if (st_new.st_gid != st_old.st_gid
3920 # ifdef HAVE_FCHOWN  /* sequent-ptx lacks fchown() */
3921 				&& fchown(bfd, (uid_t)-1, st_old.st_gid) != 0
3922 # endif
3923 						)
3924 			    mch_setperm(backup,
3925 					  (perm & 0707) | ((perm & 07) << 3));
3926 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
3927 			mch_copy_sec(fname, backup);
3928 # endif
3929 #endif
3930 
3931 			/*
3932 			 * copy the file.
3933 			 */
3934 			write_info.bw_fd = bfd;
3935 			write_info.bw_buf = copybuf;
3936 #ifdef HAS_BW_FLAGS
3937 			write_info.bw_flags = FIO_NOCONVERT;
3938 #endif
3939 			while ((write_info.bw_len = read_eintr(fd, copybuf,
3940 								BUFSIZE)) > 0)
3941 			{
3942 			    if (buf_write_bytes(&write_info) == FAIL)
3943 			    {
3944 				errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)");
3945 				break;
3946 			    }
3947 			    ui_breakcheck();
3948 			    if (got_int)
3949 			    {
3950 				errmsg = (char_u *)_(e_interr);
3951 				break;
3952 			    }
3953 			}
3954 
3955 			if (close(bfd) < 0 && errmsg == NULL)
3956 			    errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)");
3957 			if (write_info.bw_len < 0)
3958 			    errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)");
3959 #ifdef UNIX
3960 			set_file_time(backup, st_old.st_atime, st_old.st_mtime);
3961 #endif
3962 #ifdef HAVE_ACL
3963 			mch_set_acl(backup, acl);
3964 #endif
3965 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
3966 			mch_copy_sec(fname, backup);
3967 #endif
3968 			break;
3969 		    }
3970 		}
3971 	    }
3972     nobackup:
3973 	    close(fd);		/* ignore errors for closing read file */
3974 	    vim_free(copybuf);
3975 
3976 	    if (backup == NULL && errmsg == NULL)
3977 		errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)");
3978 	    /* ignore errors when forceit is TRUE */
3979 	    if ((some_error || errmsg != NULL) && !forceit)
3980 	    {
3981 		retval = FAIL;
3982 		goto fail;
3983 	    }
3984 	    errmsg = NULL;
3985 	}
3986 	else
3987 	{
3988 	    char_u	*dirp;
3989 	    char_u	*p;
3990 	    char_u	*rootname;
3991 
3992 	    /*
3993 	     * Make a backup by renaming the original file.
3994 	     */
3995 	    /*
3996 	     * If 'cpoptions' includes the "W" flag, we don't want to
3997 	     * overwrite a read-only file.  But rename may be possible
3998 	     * anyway, thus we need an extra check here.
3999 	     */
4000 	    if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL)
4001 	    {
4002 		errnum = (char_u *)"E504: ";
4003 		errmsg = (char_u *)_(err_readonly);
4004 		goto fail;
4005 	    }
4006 
4007 	    /*
4008 	     *
4009 	     * Form the backup file name - change path/fo.o.h to
4010 	     * path/fo.o.h.bak Try all directories in 'backupdir', first one
4011 	     * that works is used.
4012 	     */
4013 	    dirp = p_bdir;
4014 	    while (*dirp)
4015 	    {
4016 		/*
4017 		 * Isolate one directory name and make the backup file name.
4018 		 */
4019 		(void)copy_option_part(&dirp, IObuff, IOSIZE, ",");
4020 
4021 #if defined(UNIX) || defined(WIN3264)
4022 		p = IObuff + STRLEN(IObuff);
4023 		if (after_pathsep(IObuff, p) && p[-1] == p[-2])
4024 		    // path ends with '//', use full path
4025 		    if ((p = make_percent_swname(IObuff, fname)) != NULL)
4026 		    {
4027 			backup = modname(p, backup_ext, FALSE);
4028 			vim_free(p);
4029 		    }
4030 #endif
4031 		if (backup == NULL)
4032 		{
4033 		    rootname = get_file_in_dir(fname, IObuff);
4034 		    if (rootname == NULL)
4035 			backup = NULL;
4036 		    else
4037 		    {
4038 			backup = buf_modname(
4039 				(buf->b_p_sn || buf->b_shortname),
4040 						rootname, backup_ext, FALSE);
4041 			vim_free(rootname);
4042 		    }
4043 		}
4044 
4045 		if (backup != NULL)
4046 		{
4047 		    /*
4048 		     * If we are not going to keep the backup file, don't
4049 		     * delete an existing one, try to use another name.
4050 		     * Change one character, just before the extension.
4051 		     */
4052 		    if (!p_bk && mch_getperm(backup) >= 0)
4053 		    {
4054 			p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
4055 			if (p < backup)	/* empty file name ??? */
4056 			    p = backup;
4057 			*p = 'z';
4058 			while (*p > 'a' && mch_getperm(backup) >= 0)
4059 			    --*p;
4060 			/* They all exist??? Must be something wrong! */
4061 			if (*p == 'a')
4062 			    VIM_CLEAR(backup);
4063 		    }
4064 		}
4065 		if (backup != NULL)
4066 		{
4067 		    /*
4068 		     * Delete any existing backup and move the current version
4069 		     * to the backup.	For safety, we don't remove the backup
4070 		     * until the write has finished successfully. And if the
4071 		     * 'backup' option is set, leave it around.
4072 		     */
4073 		    /*
4074 		     * If the renaming of the original file to the backup file
4075 		     * works, quit here.
4076 		     */
4077 		    if (vim_rename(fname, backup) == 0)
4078 			break;
4079 
4080 		    VIM_CLEAR(backup);   /* don't do the rename below */
4081 		}
4082 	    }
4083 	    if (backup == NULL && !forceit)
4084 	    {
4085 		errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)");
4086 		goto fail;
4087 	    }
4088 	}
4089     }
4090 
4091 #if defined(UNIX)
4092     /* When using ":w!" and the file was read-only: make it writable */
4093     if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
4094 				     && vim_strchr(p_cpo, CPO_FWRITE) == NULL)
4095     {
4096 	perm |= 0200;
4097 	(void)mch_setperm(fname, perm);
4098 	made_writable = TRUE;
4099     }
4100 #endif
4101 
4102     /* When using ":w!" and writing to the current file, 'readonly' makes no
4103      * sense, reset it, unless 'Z' appears in 'cpoptions'.  */
4104     if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL)
4105     {
4106 	buf->b_p_ro = FALSE;
4107 #ifdef FEAT_TITLE
4108 	need_maketitle = TRUE;	    /* set window title later */
4109 #endif
4110 	status_redraw_all();	    /* redraw status lines later */
4111     }
4112 
4113     if (end > buf->b_ml.ml_line_count)
4114 	end = buf->b_ml.ml_line_count;
4115     if (buf->b_ml.ml_flags & ML_EMPTY)
4116 	start = end + 1;
4117 
4118     /*
4119      * If the original file is being overwritten, there is a small chance that
4120      * we crash in the middle of writing. Therefore the file is preserved now.
4121      * This makes all block numbers positive so that recovery does not need
4122      * the original file.
4123      * Don't do this if there is a backup file and we are exiting.
4124      */
4125     if (reset_changed && !newfile && overwriting
4126 					      && !(exiting && backup != NULL))
4127     {
4128 	ml_preserve(buf, FALSE);
4129 	if (got_int)
4130 	{
4131 	    errmsg = (char_u *)_(e_interr);
4132 	    goto restore_backup;
4133 	}
4134     }
4135 
4136 #ifdef VMS
4137     vms_remove_version(fname); /* remove version */
4138 #endif
4139     /* Default: write the file directly.  May write to a temp file for
4140      * multi-byte conversion. */
4141     wfname = fname;
4142 
4143     /* Check for forced 'fileencoding' from "++opt=val" argument. */
4144     if (eap != NULL && eap->force_enc != 0)
4145     {
4146 	fenc = eap->cmd + eap->force_enc;
4147 	fenc = enc_canonize(fenc);
4148 	fenc_tofree = fenc;
4149     }
4150     else
4151 	fenc = buf->b_p_fenc;
4152 
4153     /*
4154      * Check if the file needs to be converted.
4155      */
4156     converted = need_conversion(fenc);
4157 
4158     /*
4159      * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done.  Or
4160      * Latin1 to Unicode conversion.  This is handled in buf_write_bytes().
4161      * Prepare the flags for it and allocate bw_conv_buf when needed.
4162      */
4163     if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0))
4164     {
4165 	wb_flags = get_fio_flags(fenc);
4166 	if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8))
4167 	{
4168 	    /* Need to allocate a buffer to translate into. */
4169 	    if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8))
4170 		write_info.bw_conv_buflen = bufsize * 2;
4171 	    else /* FIO_UCS4 */
4172 		write_info.bw_conv_buflen = bufsize * 4;
4173 	    write_info.bw_conv_buf
4174 			   = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4175 	    if (write_info.bw_conv_buf == NULL)
4176 		end = 0;
4177 	}
4178     }
4179 
4180 #ifdef WIN3264
4181     if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0)
4182     {
4183 	/* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS.  Worst-case * 4: */
4184 	write_info.bw_conv_buflen = bufsize * 4;
4185 	write_info.bw_conv_buf
4186 			    = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4187 	if (write_info.bw_conv_buf == NULL)
4188 	    end = 0;
4189     }
4190 #endif
4191 
4192 #ifdef MACOS_CONVERT
4193     if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0)
4194     {
4195 	write_info.bw_conv_buflen = bufsize * 3;
4196 	write_info.bw_conv_buf
4197 			    = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4198 	if (write_info.bw_conv_buf == NULL)
4199 	    end = 0;
4200     }
4201 #endif
4202 
4203 #if defined(FEAT_EVAL) || defined(USE_ICONV)
4204     if (converted && wb_flags == 0)
4205     {
4206 # ifdef USE_ICONV
4207 	/*
4208 	 * Use iconv() conversion when conversion is needed and it's not done
4209 	 * internally.
4210 	 */
4211 	write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc,
4212 					enc_utf8 ? (char_u *)"utf-8" : p_enc);
4213 	if (write_info.bw_iconv_fd != (iconv_t)-1)
4214 	{
4215 	    /* We're going to use iconv(), allocate a buffer to convert in. */
4216 	    write_info.bw_conv_buflen = bufsize * ICONV_MULT;
4217 	    write_info.bw_conv_buf
4218 			   = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4219 	    if (write_info.bw_conv_buf == NULL)
4220 		end = 0;
4221 	    write_info.bw_first = TRUE;
4222 	}
4223 #  ifdef FEAT_EVAL
4224 	else
4225 #  endif
4226 # endif
4227 
4228 # ifdef FEAT_EVAL
4229 	    /*
4230 	     * When the file needs to be converted with 'charconvert' after
4231 	     * writing, write to a temp file instead and let the conversion
4232 	     * overwrite the original file.
4233 	     */
4234 	    if (*p_ccv != NUL)
4235 	    {
4236 		wfname = vim_tempname('w', FALSE);
4237 		if (wfname == NULL)	/* Can't write without a tempfile! */
4238 		{
4239 		    errmsg = (char_u *)_("E214: Can't find temp file for writing");
4240 		    goto restore_backup;
4241 		}
4242 	    }
4243 # endif
4244     }
4245 #endif
4246     if (converted && wb_flags == 0
4247 #ifdef USE_ICONV
4248 	    && write_info.bw_iconv_fd == (iconv_t)-1
4249 # endif
4250 # ifdef FEAT_EVAL
4251 	    && wfname == fname
4252 # endif
4253 	    )
4254     {
4255 	if (!forceit)
4256 	{
4257 	    errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)");
4258 	    goto restore_backup;
4259 	}
4260 	notconverted = TRUE;
4261     }
4262 
4263     /*
4264      * If conversion is taking place, we may first pretend to write and check
4265      * for conversion errors.  Then loop again to write for real.
4266      * When not doing conversion this writes for real right away.
4267      */
4268     for (checking_conversion = TRUE; ; checking_conversion = FALSE)
4269     {
4270 	/*
4271 	 * There is no need to check conversion when:
4272 	 * - there is no conversion
4273 	 * - we make a backup file, that can be restored in case of conversion
4274 	 *   failure.
4275 	 */
4276 	if (!converted || dobackup)
4277 	    checking_conversion = FALSE;
4278 
4279 	if (checking_conversion)
4280 	{
4281 	    /* Make sure we don't write anything. */
4282 	    fd = -1;
4283 	    write_info.bw_fd = fd;
4284 	}
4285 	else
4286 	{
4287 #ifdef HAVE_FTRUNCATE
4288 # define TRUNC_ON_OPEN 0
4289 #else
4290 # define TRUNC_ON_OPEN O_TRUNC
4291 #endif
4292 	    /*
4293 	     * Open the file "wfname" for writing.
4294 	     * We may try to open the file twice: If we can't write to the file
4295 	     * and forceit is TRUE we delete the existing file and try to
4296 	     * create a new one. If this still fails we may have lost the
4297 	     * original file!  (this may happen when the user reached his
4298 	     * quotum for number of files).
4299 	     * Appending will fail if the file does not exist and forceit is
4300 	     * FALSE.
4301 	     */
4302 	    while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append
4303 				? (forceit ? (O_APPEND | O_CREAT) : O_APPEND)
4304 				: (O_CREAT | TRUNC_ON_OPEN))
4305 				, perm < 0 ? 0666 : (perm & 0777))) < 0)
4306 	    {
4307 		/*
4308 		 * A forced write will try to create a new file if the old one
4309 		 * is still readonly. This may also happen when the directory
4310 		 * is read-only. In that case the mch_remove() will fail.
4311 		 */
4312 		if (errmsg == NULL)
4313 		{
4314 #ifdef UNIX
4315 		    stat_T	st;
4316 
4317 		    /* Don't delete the file when it's a hard or symbolic link.
4318 		     */
4319 		    if ((!newfile && st_old.st_nlink > 1)
4320 			    || (mch_lstat((char *)fname, &st) == 0
4321 				&& (st.st_dev != st_old.st_dev
4322 				    || st.st_ino != st_old.st_ino)))
4323 			errmsg = (char_u *)_("E166: Can't open linked file for writing");
4324 		    else
4325 #endif
4326 		    {
4327 			errmsg = (char_u *)_("E212: Can't open file for writing");
4328 			if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL
4329 								  && perm >= 0)
4330 			{
4331 #ifdef UNIX
4332 			    /* we write to the file, thus it should be marked
4333 			       writable after all */
4334 			    if (!(perm & 0200))
4335 				made_writable = TRUE;
4336 			    perm |= 0200;
4337 			    if (st_old.st_uid != getuid()
4338 						  || st_old.st_gid != getgid())
4339 				perm &= 0777;
4340 #endif
4341 			    if (!append)  /* don't remove when appending */
4342 				mch_remove(wfname);
4343 			    continue;
4344 			}
4345 		    }
4346 		}
4347 
4348 restore_backup:
4349 		{
4350 		    stat_T	st;
4351 
4352 		    /*
4353 		     * If we failed to open the file, we don't need a backup.
4354 		     * Throw it away.  If we moved or removed the original file
4355 		     * try to put the backup in its place.
4356 		     */
4357 		    if (backup != NULL && wfname == fname)
4358 		    {
4359 			if (backup_copy)
4360 			{
4361 			    /*
4362 			     * There is a small chance that we removed the
4363 			     * original, try to move the copy in its place.
4364 			     * This may not work if the vim_rename() fails.
4365 			     * In that case we leave the copy around.
4366 			     */
4367 			    /* If file does not exist, put the copy in its
4368 			     * place */
4369 			    if (mch_stat((char *)fname, &st) < 0)
4370 				vim_rename(backup, fname);
4371 			    /* if original file does exist throw away the copy
4372 			     */
4373 			    if (mch_stat((char *)fname, &st) >= 0)
4374 				mch_remove(backup);
4375 			}
4376 			else
4377 			{
4378 			    /* try to put the original file back */
4379 			    vim_rename(backup, fname);
4380 			}
4381 		    }
4382 
4383 		    /* if original file no longer exists give an extra warning
4384 		     */
4385 		    if (!newfile && mch_stat((char *)fname, &st) < 0)
4386 			end = 0;
4387 		}
4388 
4389 		if (wfname != fname)
4390 		    vim_free(wfname);
4391 		goto fail;
4392 	    }
4393 	    write_info.bw_fd = fd;
4394 
4395 #if defined(UNIX)
4396 	    {
4397 		stat_T	st;
4398 
4399 		/* Double check we are writing the intended file before making
4400 		 * any changes. */
4401 		if (overwriting
4402 			&& (!dobackup || backup_copy)
4403 			&& fname == wfname
4404 			&& perm >= 0
4405 			&& mch_fstat(fd, &st) == 0
4406 			&& st.st_ino != st_old.st_ino)
4407 		{
4408 		    close(fd);
4409 		    errmsg = (char_u *)_("E949: File changed while writing");
4410 		    goto fail;
4411 		}
4412 	    }
4413 #endif
4414 #ifdef HAVE_FTRUNCATE
4415 	    if (!append)
4416 		vim_ignored = ftruncate(fd, (off_t)0);
4417 #endif
4418 
4419 #if defined(WIN3264)
4420 	    if (backup != NULL && overwriting && !append)
4421 	    {
4422 		if (backup_copy)
4423 		    (void)mch_copy_file_attribute(wfname, backup);
4424 		else
4425 		    (void)mch_copy_file_attribute(backup, wfname);
4426 	    }
4427 
4428 	    if (!overwriting && !append)
4429 	    {
4430 		if (buf->b_ffname != NULL)
4431 		    (void)mch_copy_file_attribute(buf->b_ffname, wfname);
4432 		/* Should copy resource fork */
4433 	    }
4434 #endif
4435 
4436 #ifdef FEAT_CRYPT
4437 	    if (*buf->b_p_key != NUL && !filtering)
4438 	    {
4439 		char_u		*header;
4440 		int		header_len;
4441 
4442 		buf->b_cryptstate = crypt_create_for_writing(
4443 						      crypt_get_method_nr(buf),
4444 					   buf->b_p_key, &header, &header_len);
4445 		if (buf->b_cryptstate == NULL || header == NULL)
4446 		    end = 0;
4447 		else
4448 		{
4449 		    /* Write magic number, so that Vim knows how this file is
4450 		     * encrypted when reading it back. */
4451 		    write_info.bw_buf = header;
4452 		    write_info.bw_len = header_len;
4453 		    write_info.bw_flags = FIO_NOCONVERT;
4454 		    if (buf_write_bytes(&write_info) == FAIL)
4455 			end = 0;
4456 		    wb_flags |= FIO_ENCRYPTED;
4457 		    vim_free(header);
4458 		}
4459 	    }
4460 #endif
4461 	}
4462 	errmsg = NULL;
4463 
4464 	write_info.bw_buf = buffer;
4465 	nchars = 0;
4466 
4467 	/* use "++bin", "++nobin" or 'binary' */
4468 	if (eap != NULL && eap->force_bin != 0)
4469 	    write_bin = (eap->force_bin == FORCE_BIN);
4470 	else
4471 	    write_bin = buf->b_p_bin;
4472 
4473 	/*
4474 	 * The BOM is written just after the encryption magic number.
4475 	 * Skip it when appending and the file already existed, the BOM only
4476 	 * makes sense at the start of the file.
4477 	 */
4478 	if (buf->b_p_bomb && !write_bin && (!append || perm < 0))
4479 	{
4480 	    write_info.bw_len = make_bom(buffer, fenc);
4481 	    if (write_info.bw_len > 0)
4482 	    {
4483 		/* don't convert, do encryption */
4484 		write_info.bw_flags = FIO_NOCONVERT | wb_flags;
4485 		if (buf_write_bytes(&write_info) == FAIL)
4486 		    end = 0;
4487 		else
4488 		    nchars += write_info.bw_len;
4489 	    }
4490 	}
4491 	write_info.bw_start_lnum = start;
4492 
4493 #ifdef FEAT_PERSISTENT_UNDO
4494 	write_undo_file = (buf->b_p_udf
4495 			    && overwriting
4496 			    && !append
4497 			    && !filtering
4498 			    && reset_changed
4499 			    && !checking_conversion);
4500 	if (write_undo_file)
4501 	    /* Prepare for computing the hash value of the text. */
4502 	    sha256_start(&sha_ctx);
4503 #endif
4504 
4505 	write_info.bw_len = bufsize;
4506 #ifdef HAS_BW_FLAGS
4507 	write_info.bw_flags = wb_flags;
4508 #endif
4509 	fileformat = get_fileformat_force(buf, eap);
4510 	s = buffer;
4511 	len = 0;
4512 	for (lnum = start; lnum <= end; ++lnum)
4513 	{
4514 	    /*
4515 	     * The next while loop is done once for each character written.
4516 	     * Keep it fast!
4517 	     */
4518 	    ptr = ml_get_buf(buf, lnum, FALSE) - 1;
4519 #ifdef FEAT_PERSISTENT_UNDO
4520 	    if (write_undo_file)
4521 		sha256_update(&sha_ctx, ptr + 1,
4522 					      (UINT32_T)(STRLEN(ptr + 1) + 1));
4523 #endif
4524 	    while ((c = *++ptr) != NUL)
4525 	    {
4526 		if (c == NL)
4527 		    *s = NUL;		/* replace newlines with NULs */
4528 		else if (c == CAR && fileformat == EOL_MAC)
4529 		    *s = NL;		/* Mac: replace CRs with NLs */
4530 		else
4531 		    *s = c;
4532 		++s;
4533 		if (++len != bufsize)
4534 		    continue;
4535 		if (buf_write_bytes(&write_info) == FAIL)
4536 		{
4537 		    end = 0;		/* write error: break loop */
4538 		    break;
4539 		}
4540 		nchars += bufsize;
4541 		s = buffer;
4542 		len = 0;
4543 		write_info.bw_start_lnum = lnum;
4544 	    }
4545 	    /* write failed or last line has no EOL: stop here */
4546 	    if (end == 0
4547 		    || (lnum == end
4548 			&& (write_bin || !buf->b_p_fixeol)
4549 			&& (lnum == buf->b_no_eol_lnum
4550 			    || (lnum == buf->b_ml.ml_line_count
4551 							   && !buf->b_p_eol))))
4552 	    {
4553 		++lnum;			/* written the line, count it */
4554 		no_eol = TRUE;
4555 		break;
4556 	    }
4557 	    if (fileformat == EOL_UNIX)
4558 		*s++ = NL;
4559 	    else
4560 	    {
4561 		*s++ = CAR;		    /* EOL_MAC or EOL_DOS: write CR */
4562 		if (fileformat == EOL_DOS)  /* write CR-NL */
4563 		{
4564 		    if (++len == bufsize)
4565 		    {
4566 			if (buf_write_bytes(&write_info) == FAIL)
4567 			{
4568 			    end = 0;	/* write error: break loop */
4569 			    break;
4570 			}
4571 			nchars += bufsize;
4572 			s = buffer;
4573 			len = 0;
4574 		    }
4575 		    *s++ = NL;
4576 		}
4577 	    }
4578 	    if (++len == bufsize && end)
4579 	    {
4580 		if (buf_write_bytes(&write_info) == FAIL)
4581 		{
4582 		    end = 0;		/* write error: break loop */
4583 		    break;
4584 		}
4585 		nchars += bufsize;
4586 		s = buffer;
4587 		len = 0;
4588 
4589 		ui_breakcheck();
4590 		if (got_int)
4591 		{
4592 		    end = 0;		/* Interrupted, break loop */
4593 		    break;
4594 		}
4595 	    }
4596 #ifdef VMS
4597 	    /*
4598 	     * On VMS there is a problem: newlines get added when writing
4599 	     * blocks at a time. Fix it by writing a line at a time.
4600 	     * This is much slower!
4601 	     * Explanation: VAX/DECC RTL insists that records in some RMS
4602 	     * structures end with a newline (carriage return) character, and
4603 	     * if they don't it adds one.
4604 	     * With other RMS structures it works perfect without this fix.
4605 	     */
4606 	    if (buf->b_fab_rfm == FAB$C_VFC
4607 		    || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4608 	    {
4609 		int b2write;
4610 
4611 		buf->b_fab_mrs = (buf->b_fab_mrs == 0
4612 			? MIN(4096, bufsize)
4613 			: MIN(buf->b_fab_mrs, bufsize));
4614 
4615 		b2write = len;
4616 		while (b2write > 0)
4617 		{
4618 		    write_info.bw_len = MIN(b2write, buf->b_fab_mrs);
4619 		    if (buf_write_bytes(&write_info) == FAIL)
4620 		    {
4621 			end = 0;
4622 			break;
4623 		    }
4624 		    b2write -= MIN(b2write, buf->b_fab_mrs);
4625 		}
4626 		write_info.bw_len = bufsize;
4627 		nchars += len;
4628 		s = buffer;
4629 		len = 0;
4630 	    }
4631 #endif
4632 	}
4633 	if (len > 0 && end > 0)
4634 	{
4635 	    write_info.bw_len = len;
4636 	    if (buf_write_bytes(&write_info) == FAIL)
4637 		end = 0;		    /* write error */
4638 	    nchars += len;
4639 	}
4640 
4641 	/* Stop when writing done or an error was encountered. */
4642 	if (!checking_conversion || end == 0)
4643 	    break;
4644 
4645 	/* If no error happened until now, writing should be ok, so loop to
4646 	 * really write the buffer. */
4647     }
4648 
4649     /* If we started writing, finish writing. Also when an error was
4650      * encountered. */
4651     if (!checking_conversion)
4652     {
4653 #if defined(UNIX) && defined(HAVE_FSYNC)
4654 	/*
4655 	 * On many journalling file systems there is a bug that causes both the
4656 	 * original and the backup file to be lost when halting the system
4657 	 * right after writing the file.  That's because only the meta-data is
4658 	 * journalled.  Syncing the file slows down the system, but assures it
4659 	 * has been written to disk and we don't lose it.
4660 	 * For a device do try the fsync() but don't complain if it does not
4661 	 * work (could be a pipe).
4662 	 * If the 'fsync' option is FALSE, don't fsync().  Useful for laptops.
4663 	 */
4664 	if (p_fs && fsync(fd) != 0 && !device)
4665 	{
4666 	    errmsg = (char_u *)_(e_fsync);
4667 	    end = 0;
4668 	}
4669 #endif
4670 
4671 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
4672 	/* Probably need to set the security context. */
4673 	if (!backup_copy)
4674 	    mch_copy_sec(backup, wfname);
4675 #endif
4676 
4677 #ifdef UNIX
4678 	/* When creating a new file, set its owner/group to that of the
4679 	 * original file.  Get the new device and inode number. */
4680 	if (backup != NULL && !backup_copy)
4681 	{
4682 # ifdef HAVE_FCHOWN
4683 	    stat_T	st;
4684 
4685 	    /* Don't change the owner when it's already OK, some systems remove
4686 	     * permission or ACL stuff. */
4687 	    if (mch_stat((char *)wfname, &st) < 0
4688 		    || st.st_uid != st_old.st_uid
4689 		    || st.st_gid != st_old.st_gid)
4690 	    {
4691 		/* changing owner might not be possible */
4692 		vim_ignored = fchown(fd, st_old.st_uid, -1);
4693 		/* if changing group fails clear the group permissions */
4694 		if (fchown(fd, -1, st_old.st_gid) == -1 && perm > 0)
4695 		    perm &= ~070;
4696 	    }
4697 # endif
4698 	    buf_setino(buf);
4699 	}
4700 	else if (!buf->b_dev_valid)
4701 	    /* Set the inode when creating a new file. */
4702 	    buf_setino(buf);
4703 #endif
4704 
4705 #ifdef UNIX
4706 	if (made_writable)
4707 	    perm &= ~0200;	/* reset 'w' bit for security reasons */
4708 #endif
4709 #ifdef HAVE_FCHMOD
4710 	/* set permission of new file same as old file */
4711 	if (perm >= 0)
4712 	    (void)mch_fsetperm(fd, perm);
4713 #endif
4714 	if (close(fd) != 0)
4715 	{
4716 	    errmsg = (char_u *)_("E512: Close failed");
4717 	    end = 0;
4718 	}
4719 
4720 #ifndef HAVE_FCHMOD
4721 	/* set permission of new file same as old file */
4722 	if (perm >= 0)
4723 	    (void)mch_setperm(wfname, perm);
4724 #endif
4725 #ifdef HAVE_ACL
4726 	/*
4727 	 * Probably need to set the ACL before changing the user (can't set the
4728 	 * ACL on a file the user doesn't own).
4729 	 * On Solaris, with ZFS and the aclmode property set to "discard" (the
4730 	 * default), chmod() discards all part of a file's ACL that don't
4731 	 * represent the mode of the file.  It's non-trivial for us to discover
4732 	 * whether we're in that situation, so we simply always re-set the ACL.
4733 	 */
4734 # ifndef HAVE_SOLARIS_ZFS_ACL
4735 	if (!backup_copy)
4736 # endif
4737 	    mch_set_acl(wfname, acl);
4738 #endif
4739 #ifdef FEAT_CRYPT
4740 	if (buf->b_cryptstate != NULL)
4741 	{
4742 	    crypt_free_state(buf->b_cryptstate);
4743 	    buf->b_cryptstate = NULL;
4744 	}
4745 #endif
4746 
4747 #if defined(FEAT_EVAL)
4748 	if (wfname != fname)
4749 	{
4750 	    /*
4751 	     * The file was written to a temp file, now it needs to be
4752 	     * converted with 'charconvert' to (overwrite) the output file.
4753 	     */
4754 	    if (end != 0)
4755 	    {
4756 		if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc,
4757 						  fenc, wfname, fname) == FAIL)
4758 		{
4759 		    write_info.bw_conv_error = TRUE;
4760 		    end = 0;
4761 		}
4762 	    }
4763 	    mch_remove(wfname);
4764 	    vim_free(wfname);
4765 	}
4766 #endif
4767     }
4768 
4769     if (end == 0)
4770     {
4771 	/*
4772 	 * Error encountered.
4773 	 */
4774 	if (errmsg == NULL)
4775 	{
4776 	    if (write_info.bw_conv_error)
4777 	    {
4778 		if (write_info.bw_conv_error_lnum == 0)
4779 		    errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)");
4780 		else
4781 		{
4782 		    errmsg_allocated = TRUE;
4783 		    errmsg = alloc(300);
4784 		    vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"),
4785 					 (long)write_info.bw_conv_error_lnum);
4786 		}
4787 	    }
4788 	    else if (got_int)
4789 		errmsg = (char_u *)_(e_interr);
4790 	    else
4791 		errmsg = (char_u *)_("E514: write error (file system full?)");
4792 	}
4793 
4794 	/*
4795 	 * If we have a backup file, try to put it in place of the new file,
4796 	 * because the new file is probably corrupt.  This avoids losing the
4797 	 * original file when trying to make a backup when writing the file a
4798 	 * second time.
4799 	 * When "backup_copy" is set we need to copy the backup over the new
4800 	 * file.  Otherwise rename the backup file.
4801 	 * If this is OK, don't give the extra warning message.
4802 	 */
4803 	if (backup != NULL)
4804 	{
4805 	    if (backup_copy)
4806 	    {
4807 		/* This may take a while, if we were interrupted let the user
4808 		 * know we got the message. */
4809 		if (got_int)
4810 		{
4811 		    msg(_(e_interr));
4812 		    out_flush();
4813 		}
4814 		if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0)
4815 		{
4816 		    if ((write_info.bw_fd = mch_open((char *)fname,
4817 				    O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA,
4818 							   perm & 0777)) >= 0)
4819 		    {
4820 			/* copy the file. */
4821 			write_info.bw_buf = smallbuf;
4822 #ifdef HAS_BW_FLAGS
4823 			write_info.bw_flags = FIO_NOCONVERT;
4824 #endif
4825 			while ((write_info.bw_len = read_eintr(fd, smallbuf,
4826 						      SMBUFSIZE)) > 0)
4827 			    if (buf_write_bytes(&write_info) == FAIL)
4828 				break;
4829 
4830 			if (close(write_info.bw_fd) >= 0
4831 						   && write_info.bw_len == 0)
4832 			    end = 1;		/* success */
4833 		    }
4834 		    close(fd);	/* ignore errors for closing read file */
4835 		}
4836 	    }
4837 	    else
4838 	    {
4839 		if (vim_rename(backup, fname) == 0)
4840 		    end = 1;
4841 	    }
4842 	}
4843 	goto fail;
4844     }
4845 
4846     lnum -= start;	    /* compute number of written lines */
4847     --no_wait_return;	    /* may wait for return now */
4848 
4849 #if !(defined(UNIX) || defined(VMS))
4850     fname = sfname;	    /* use shortname now, for the messages */
4851 #endif
4852     if (!filtering)
4853     {
4854 	msg_add_fname(buf, fname);	/* put fname in IObuff with quotes */
4855 	c = FALSE;
4856 	if (write_info.bw_conv_error)
4857 	{
4858 	    STRCAT(IObuff, _(" CONVERSION ERROR"));
4859 	    c = TRUE;
4860 	    if (write_info.bw_conv_error_lnum != 0)
4861 		vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"),
4862 			(long)write_info.bw_conv_error_lnum);
4863 	}
4864 	else if (notconverted)
4865 	{
4866 	    STRCAT(IObuff, _("[NOT converted]"));
4867 	    c = TRUE;
4868 	}
4869 	else if (converted)
4870 	{
4871 	    STRCAT(IObuff, _("[converted]"));
4872 	    c = TRUE;
4873 	}
4874 	if (device)
4875 	{
4876 	    STRCAT(IObuff, _("[Device]"));
4877 	    c = TRUE;
4878 	}
4879 	else if (newfile)
4880 	{
4881 	    STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]"));
4882 	    c = TRUE;
4883 	}
4884 	if (no_eol)
4885 	{
4886 	    msg_add_eol();
4887 	    c = TRUE;
4888 	}
4889 	/* may add [unix/dos/mac] */
4890 	if (msg_add_fileformat(fileformat))
4891 	    c = TRUE;
4892 #ifdef FEAT_CRYPT
4893 	if (wb_flags & FIO_ENCRYPTED)
4894 	{
4895 	    crypt_append_msg(buf);
4896 	    c = TRUE;
4897 	}
4898 #endif
4899 	msg_add_lines(c, (long)lnum, nchars);	/* add line/char count */
4900 	if (!shortmess(SHM_WRITE))
4901 	{
4902 	    if (append)
4903 		STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended"));
4904 	    else
4905 		STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written"));
4906 	}
4907 
4908 	set_keep_msg((char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0), 0);
4909     }
4910 
4911     /* When written everything correctly: reset 'modified'.  Unless not
4912      * writing to the original file and '+' is not in 'cpoptions'. */
4913     if (reset_changed && whole && !append
4914 	    && !write_info.bw_conv_error
4915 	    && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
4916     {
4917 	unchanged(buf, TRUE);
4918 	/* b:changedtick is always incremented in unchanged() but that
4919 	 * should not trigger a TextChanged event. */
4920 	if (buf->b_last_changedtick + 1 == CHANGEDTICK(buf))
4921 	    buf->b_last_changedtick = CHANGEDTICK(buf);
4922 	u_unchanged(buf);
4923 	u_update_save_nr(buf);
4924     }
4925 
4926     /*
4927      * If written to the current file, update the timestamp of the swap file
4928      * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime.
4929      */
4930     if (overwriting)
4931     {
4932 	ml_timestamp(buf);
4933 	if (append)
4934 	    buf->b_flags &= ~BF_NEW;
4935 	else
4936 	    buf->b_flags &= ~BF_WRITE_MASK;
4937     }
4938 
4939     /*
4940      * If we kept a backup until now, and we are in patch mode, then we make
4941      * the backup file our 'original' file.
4942      */
4943     if (*p_pm && dobackup)
4944     {
4945 	char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname),
4946 							  fname, p_pm, FALSE);
4947 
4948 	if (backup != NULL)
4949 	{
4950 	    stat_T	st;
4951 
4952 	    /*
4953 	     * If the original file does not exist yet
4954 	     * the current backup file becomes the original file
4955 	     */
4956 	    if (org == NULL)
4957 		emsg(_("E205: Patchmode: can't save original file"));
4958 	    else if (mch_stat(org, &st) < 0)
4959 	    {
4960 		vim_rename(backup, (char_u *)org);
4961 		VIM_CLEAR(backup);	    /* don't delete the file */
4962 #ifdef UNIX
4963 		set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime);
4964 #endif
4965 	    }
4966 	}
4967 	/*
4968 	 * If there is no backup file, remember that a (new) file was
4969 	 * created.
4970 	 */
4971 	else
4972 	{
4973 	    int empty_fd;
4974 
4975 	    if (org == NULL
4976 		    || (empty_fd = mch_open(org,
4977 				      O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW,
4978 					perm < 0 ? 0666 : (perm & 0777))) < 0)
4979 	      emsg(_("E206: patchmode: can't touch empty original file"));
4980 	    else
4981 	      close(empty_fd);
4982 	}
4983 	if (org != NULL)
4984 	{
4985 	    mch_setperm((char_u *)org, mch_getperm(fname) & 0777);
4986 	    vim_free(org);
4987 	}
4988     }
4989 
4990     /*
4991      * Remove the backup unless 'backup' option is set
4992      */
4993     if (!p_bk && backup != NULL && mch_remove(backup) != 0)
4994 	emsg(_("E207: Can't delete backup file"));
4995 
4996     goto nofail;
4997 
4998     /*
4999      * Finish up.  We get here either after failure or success.
5000      */
5001 fail:
5002     --no_wait_return;		/* may wait for return now */
5003 nofail:
5004 
5005     /* Done saving, we accept changed buffer warnings again */
5006     buf->b_saving = FALSE;
5007 
5008     vim_free(backup);
5009     if (buffer != smallbuf)
5010 	vim_free(buffer);
5011     vim_free(fenc_tofree);
5012     vim_free(write_info.bw_conv_buf);
5013 #ifdef USE_ICONV
5014     if (write_info.bw_iconv_fd != (iconv_t)-1)
5015     {
5016 	iconv_close(write_info.bw_iconv_fd);
5017 	write_info.bw_iconv_fd = (iconv_t)-1;
5018     }
5019 #endif
5020 #ifdef HAVE_ACL
5021     mch_free_acl(acl);
5022 #endif
5023 
5024     if (errmsg != NULL)
5025     {
5026 	int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
5027 
5028 	attr = HL_ATTR(HLF_E);	/* set highlight for error messages */
5029 	msg_add_fname(buf,
5030 #ifndef UNIX
5031 		sfname
5032 #else
5033 		fname
5034 #endif
5035 		     );		/* put file name in IObuff with quotes */
5036 	if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE)
5037 	    IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL;
5038 	/* If the error message has the form "is ...", put the error number in
5039 	 * front of the file name. */
5040 	if (errnum != NULL)
5041 	{
5042 	    STRMOVE(IObuff + numlen, IObuff);
5043 	    mch_memmove(IObuff, errnum, (size_t)numlen);
5044 	}
5045 	STRCAT(IObuff, errmsg);
5046 	emsg((char *)IObuff);
5047 	if (errmsg_allocated)
5048 	    vim_free(errmsg);
5049 
5050 	retval = FAIL;
5051 	if (end == 0)
5052 	{
5053 	    msg_puts_attr(_("\nWARNING: Original file may be lost or damaged\n"),
5054 		    attr | MSG_HIST);
5055 	    msg_puts_attr(_("don't quit the editor until the file is successfully written!"),
5056 		    attr | MSG_HIST);
5057 
5058 	    /* Update the timestamp to avoid an "overwrite changed file"
5059 	     * prompt when writing again. */
5060 	    if (mch_stat((char *)fname, &st_old) >= 0)
5061 	    {
5062 		buf_store_time(buf, &st_old, fname);
5063 		buf->b_mtime_read = buf->b_mtime;
5064 	    }
5065 	}
5066     }
5067     msg_scroll = msg_save;
5068 
5069 #ifdef FEAT_PERSISTENT_UNDO
5070     /*
5071      * When writing the whole file and 'undofile' is set, also write the undo
5072      * file.
5073      */
5074     if (retval == OK && write_undo_file)
5075     {
5076 	char_u	    hash[UNDO_HASH_SIZE];
5077 
5078 	sha256_finish(&sha_ctx, hash);
5079 	u_write_undo(NULL, FALSE, buf, hash);
5080     }
5081 #endif
5082 
5083 #ifdef FEAT_EVAL
5084     if (!should_abort(retval))
5085 #else
5086     if (!got_int)
5087 #endif
5088     {
5089 	aco_save_T	aco;
5090 
5091 	curbuf->b_no_eol_lnum = 0;  /* in case it was set by the previous read */
5092 
5093 	/*
5094 	 * Apply POST autocommands.
5095 	 * Careful: The autocommands may call buf_write() recursively!
5096 	 */
5097 	aucmd_prepbuf(&aco, buf);
5098 
5099 	if (append)
5100 	    apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
5101 							  FALSE, curbuf, eap);
5102 	else if (filtering)
5103 	    apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
5104 							  FALSE, curbuf, eap);
5105 	else if (reset_changed && whole)
5106 	    apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
5107 							  FALSE, curbuf, eap);
5108 	else
5109 	    apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
5110 							  FALSE, curbuf, eap);
5111 
5112 	/* restore curwin/curbuf and a few other things */
5113 	aucmd_restbuf(&aco);
5114 
5115 #ifdef FEAT_EVAL
5116 	if (aborting())	    /* autocmds may abort script processing */
5117 	    retval = FALSE;
5118 #endif
5119     }
5120 
5121     got_int |= prev_got_int;
5122 
5123     return retval;
5124 }
5125 
5126 /*
5127  * Set the name of the current buffer.  Use when the buffer doesn't have a
5128  * name and a ":r" or ":w" command with a file name is used.
5129  */
5130     static int
5131 set_rw_fname(char_u *fname, char_u *sfname)
5132 {
5133     buf_T	*buf = curbuf;
5134 
5135     /* It's like the unnamed buffer is deleted.... */
5136     if (curbuf->b_p_bl)
5137 	apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5138     apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
5139 #ifdef FEAT_EVAL
5140     if (aborting())	    /* autocmds may abort script processing */
5141 	return FAIL;
5142 #endif
5143     if (curbuf != buf)
5144     {
5145 	/* We are in another buffer now, don't do the renaming. */
5146 	emsg(_(e_auchangedbuf));
5147 	return FAIL;
5148     }
5149 
5150     if (setfname(curbuf, fname, sfname, FALSE) == OK)
5151 	curbuf->b_flags |= BF_NOTEDITED;
5152 
5153     /* ....and a new named one is created */
5154     apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
5155     if (curbuf->b_p_bl)
5156 	apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5157 #ifdef FEAT_EVAL
5158     if (aborting())	    /* autocmds may abort script processing */
5159 	return FAIL;
5160 #endif
5161 
5162     /* Do filetype detection now if 'filetype' is empty. */
5163     if (*curbuf->b_p_ft == NUL)
5164     {
5165 	if (au_has_group((char_u *)"filetypedetect"))
5166 	    (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
5167 	do_modelines(0);
5168     }
5169 
5170     return OK;
5171 }
5172 
5173 /*
5174  * Put file name into IObuff with quotes.
5175  */
5176     void
5177 msg_add_fname(buf_T *buf, char_u *fname)
5178 {
5179     if (fname == NULL)
5180 	fname = (char_u *)"-stdin-";
5181     home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
5182     IObuff[0] = '"';
5183     STRCAT(IObuff, "\" ");
5184 }
5185 
5186 /*
5187  * Append message for text mode to IObuff.
5188  * Return TRUE if something appended.
5189  */
5190     static int
5191 msg_add_fileformat(int eol_type)
5192 {
5193 #ifndef USE_CRNL
5194     if (eol_type == EOL_DOS)
5195     {
5196 	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
5197 	return TRUE;
5198     }
5199 #endif
5200 #ifndef USE_CR
5201     if (eol_type == EOL_MAC)
5202     {
5203 	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
5204 	return TRUE;
5205     }
5206 #endif
5207 #if defined(USE_CRNL) || defined(USE_CR)
5208     if (eol_type == EOL_UNIX)
5209     {
5210 	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
5211 	return TRUE;
5212     }
5213 #endif
5214     return FALSE;
5215 }
5216 
5217 /*
5218  * Append line and character count to IObuff.
5219  */
5220     void
5221 msg_add_lines(
5222     int	    insert_space,
5223     long    lnum,
5224     off_T   nchars)
5225 {
5226     char_u  *p;
5227 
5228     p = IObuff + STRLEN(IObuff);
5229 
5230     if (insert_space)
5231 	*p++ = ' ';
5232     if (shortmess(SHM_LINES))
5233 	vim_snprintf((char *)p, IOSIZE - (p - IObuff),
5234 		"%ldL, %lldC", lnum, (long_long_T)nchars);
5235     else
5236     {
5237 	sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum);
5238 	p += STRLEN(p);
5239 	vim_snprintf((char *)p, IOSIZE - (p - IObuff),
5240 		NGETTEXT("%lld character", "%lld characters", nchars),
5241 		(long_long_T)nchars);
5242     }
5243 }
5244 
5245 /*
5246  * Append message for missing line separator to IObuff.
5247  */
5248     static void
5249 msg_add_eol(void)
5250 {
5251     STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
5252 }
5253 
5254 /*
5255  * Check modification time of file, before writing to it.
5256  * The size isn't checked, because using a tool like "gzip" takes care of
5257  * using the same timestamp but can't set the size.
5258  */
5259     static int
5260 check_mtime(buf_T *buf, stat_T *st)
5261 {
5262     if (buf->b_mtime_read != 0
5263 	    && time_differs((long)st->st_mtime, buf->b_mtime_read))
5264     {
5265 	msg_scroll = TRUE;	    /* don't overwrite messages here */
5266 	msg_silent = 0;		    /* must give this prompt */
5267 	/* don't use emsg() here, don't want to flush the buffers */
5268 	msg_attr(_("WARNING: The file has been changed since reading it!!!"),
5269 						       HL_ATTR(HLF_E));
5270 	if (ask_yesno((char_u *)_("Do you really want to write to it"),
5271 								 TRUE) == 'n')
5272 	    return FAIL;
5273 	msg_scroll = FALSE;	    /* always overwrite the file message now */
5274     }
5275     return OK;
5276 }
5277 
5278     static int
5279 time_differs(long t1, long t2)
5280 {
5281 #if defined(__linux__) || defined(MSWIN)
5282     /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store
5283      * the seconds.  Since the roundoff is done when flushing the inode, the
5284      * time may change unexpectedly by one second!!! */
5285     return (t1 - t2 > 1 || t2 - t1 > 1);
5286 #else
5287     return (t1 != t2);
5288 #endif
5289 }
5290 
5291 /*
5292  * Call write() to write a number of bytes to the file.
5293  * Handles encryption and 'encoding' conversion.
5294  *
5295  * Return FAIL for failure, OK otherwise.
5296  */
5297     static int
5298 buf_write_bytes(struct bw_info *ip)
5299 {
5300     int		wlen;
5301     char_u	*buf = ip->bw_buf;	/* data to write */
5302     int		len = ip->bw_len;	/* length of data */
5303 #ifdef HAS_BW_FLAGS
5304     int		flags = ip->bw_flags;	/* extra flags */
5305 #endif
5306 
5307     /*
5308      * Skip conversion when writing the crypt magic number or the BOM.
5309      */
5310     if (!(flags & FIO_NOCONVERT))
5311     {
5312 	char_u		*p;
5313 	unsigned	c;
5314 	int		n;
5315 
5316 	if (flags & FIO_UTF8)
5317 	{
5318 	    /*
5319 	     * Convert latin1 in the buffer to UTF-8 in the file.
5320 	     */
5321 	    p = ip->bw_conv_buf;	/* translate to buffer */
5322 	    for (wlen = 0; wlen < len; ++wlen)
5323 		p += utf_char2bytes(buf[wlen], p);
5324 	    buf = ip->bw_conv_buf;
5325 	    len = (int)(p - ip->bw_conv_buf);
5326 	}
5327 	else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1))
5328 	{
5329 	    /*
5330 	     * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or
5331 	     * Latin1 chars in the file.
5332 	     */
5333 	    if (flags & FIO_LATIN1)
5334 		p = buf;	/* translate in-place (can only get shorter) */
5335 	    else
5336 		p = ip->bw_conv_buf;	/* translate to buffer */
5337 	    for (wlen = 0; wlen < len; wlen += n)
5338 	    {
5339 		if (wlen == 0 && ip->bw_restlen != 0)
5340 		{
5341 		    int		l;
5342 
5343 		    /* Use remainder of previous call.  Append the start of
5344 		     * buf[] to get a full sequence.  Might still be too
5345 		     * short! */
5346 		    l = CONV_RESTLEN - ip->bw_restlen;
5347 		    if (l > len)
5348 			l = len;
5349 		    mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
5350 		    n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l);
5351 		    if (n > ip->bw_restlen + len)
5352 		    {
5353 			/* We have an incomplete byte sequence at the end to
5354 			 * be written.  We can't convert it without the
5355 			 * remaining bytes.  Keep them for the next call. */
5356 			if (ip->bw_restlen + len > CONV_RESTLEN)
5357 			    return FAIL;
5358 			ip->bw_restlen += len;
5359 			break;
5360 		    }
5361 		    if (n > 1)
5362 			c = utf_ptr2char(ip->bw_rest);
5363 		    else
5364 			c = ip->bw_rest[0];
5365 		    if (n >= ip->bw_restlen)
5366 		    {
5367 			n -= ip->bw_restlen;
5368 			ip->bw_restlen = 0;
5369 		    }
5370 		    else
5371 		    {
5372 			ip->bw_restlen -= n;
5373 			mch_memmove(ip->bw_rest, ip->bw_rest + n,
5374 						      (size_t)ip->bw_restlen);
5375 			n = 0;
5376 		    }
5377 		}
5378 		else
5379 		{
5380 		    n = utf_ptr2len_len(buf + wlen, len - wlen);
5381 		    if (n > len - wlen)
5382 		    {
5383 			/* We have an incomplete byte sequence at the end to
5384 			 * be written.  We can't convert it without the
5385 			 * remaining bytes.  Keep them for the next call. */
5386 			if (len - wlen > CONV_RESTLEN)
5387 			    return FAIL;
5388 			ip->bw_restlen = len - wlen;
5389 			mch_memmove(ip->bw_rest, buf + wlen,
5390 						      (size_t)ip->bw_restlen);
5391 			break;
5392 		    }
5393 		    if (n > 1)
5394 			c = utf_ptr2char(buf + wlen);
5395 		    else
5396 			c = buf[wlen];
5397 		}
5398 
5399 		if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error)
5400 		{
5401 		    ip->bw_conv_error = TRUE;
5402 		    ip->bw_conv_error_lnum = ip->bw_start_lnum;
5403 		}
5404 		if (c == NL)
5405 		    ++ip->bw_start_lnum;
5406 	    }
5407 	    if (flags & FIO_LATIN1)
5408 		len = (int)(p - buf);
5409 	    else
5410 	    {
5411 		buf = ip->bw_conv_buf;
5412 		len = (int)(p - ip->bw_conv_buf);
5413 	    }
5414 	}
5415 
5416 #ifdef WIN3264
5417 	else if (flags & FIO_CODEPAGE)
5418 	{
5419 	    /*
5420 	     * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows
5421 	     * codepage.
5422 	     */
5423 	    char_u	*from;
5424 	    size_t	fromlen;
5425 	    char_u	*to;
5426 	    int		u8c;
5427 	    BOOL	bad = FALSE;
5428 	    int		needed;
5429 
5430 	    if (ip->bw_restlen > 0)
5431 	    {
5432 		/* Need to concatenate the remainder of the previous call and
5433 		 * the bytes of the current call.  Use the end of the
5434 		 * conversion buffer for this. */
5435 		fromlen = len + ip->bw_restlen;
5436 		from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5437 		mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5438 		mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5439 	    }
5440 	    else
5441 	    {
5442 		from = buf;
5443 		fromlen = len;
5444 	    }
5445 
5446 	    to = ip->bw_conv_buf;
5447 	    if (enc_utf8)
5448 	    {
5449 		/* Convert from UTF-8 to UCS-2, to the start of the buffer.
5450 		 * The buffer has been allocated to be big enough. */
5451 		while (fromlen > 0)
5452 		{
5453 		    n = (int)utf_ptr2len_len(from, (int)fromlen);
5454 		    if (n > (int)fromlen)	/* incomplete byte sequence */
5455 			break;
5456 		    u8c = utf_ptr2char(from);
5457 		    *to++ = (u8c & 0xff);
5458 		    *to++ = (u8c >> 8);
5459 		    fromlen -= n;
5460 		    from += n;
5461 		}
5462 
5463 		/* Copy remainder to ip->bw_rest[] to be used for the next
5464 		 * call. */
5465 		if (fromlen > CONV_RESTLEN)
5466 		{
5467 		    /* weird overlong sequence */
5468 		    ip->bw_conv_error = TRUE;
5469 		    return FAIL;
5470 		}
5471 		mch_memmove(ip->bw_rest, from, fromlen);
5472 		ip->bw_restlen = (int)fromlen;
5473 	    }
5474 	    else
5475 	    {
5476 		/* Convert from enc_codepage to UCS-2, to the start of the
5477 		 * buffer.  The buffer has been allocated to be big enough. */
5478 		ip->bw_restlen = 0;
5479 		needed = MultiByteToWideChar(enc_codepage,
5480 			     MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
5481 								     NULL, 0);
5482 		if (needed == 0)
5483 		{
5484 		    /* When conversion fails there may be a trailing byte. */
5485 		    needed = MultiByteToWideChar(enc_codepage,
5486 			 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
5487 								     NULL, 0);
5488 		    if (needed == 0)
5489 		    {
5490 			/* Conversion doesn't work. */
5491 			ip->bw_conv_error = TRUE;
5492 			return FAIL;
5493 		    }
5494 		    /* Save the trailing byte for the next call. */
5495 		    ip->bw_rest[0] = from[fromlen - 1];
5496 		    ip->bw_restlen = 1;
5497 		}
5498 		needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
5499 				(LPCSTR)from, (int)(fromlen - ip->bw_restlen),
5500 							  (LPWSTR)to, needed);
5501 		if (needed == 0)
5502 		{
5503 		    /* Safety check: Conversion doesn't work. */
5504 		    ip->bw_conv_error = TRUE;
5505 		    return FAIL;
5506 		}
5507 		to += needed * 2;
5508 	    }
5509 
5510 	    fromlen = to - ip->bw_conv_buf;
5511 	    buf = to;
5512 # ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
5513 	    if (FIO_GET_CP(flags) == CP_UTF8)
5514 	    {
5515 		/* Convert from UCS-2 to UTF-8, using the remainder of the
5516 		 * conversion buffer.  Fails when out of space. */
5517 		for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2)
5518 		{
5519 		    u8c = *from++;
5520 		    u8c += (*from++ << 8);
5521 		    to += utf_char2bytes(u8c, to);
5522 		    if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen)
5523 		    {
5524 			ip->bw_conv_error = TRUE;
5525 			return FAIL;
5526 		    }
5527 		}
5528 		len = (int)(to - buf);
5529 	    }
5530 	    else
5531 # endif
5532 	    {
5533 		/* Convert from UCS-2 to the codepage, using the remainder of
5534 		 * the conversion buffer.  If the conversion uses the default
5535 		 * character "0", the data doesn't fit in this encoding, so
5536 		 * fail. */
5537 		len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
5538 			(LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
5539 			(LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0,
5540 									&bad);
5541 		if (bad)
5542 		{
5543 		    ip->bw_conv_error = TRUE;
5544 		    return FAIL;
5545 		}
5546 	    }
5547 	}
5548 #endif
5549 
5550 #ifdef MACOS_CONVERT
5551 	else if (flags & FIO_MACROMAN)
5552 	{
5553 	    /*
5554 	     * Convert UTF-8 or latin1 to Apple MacRoman.
5555 	     */
5556 	    char_u	*from;
5557 	    size_t	fromlen;
5558 
5559 	    if (ip->bw_restlen > 0)
5560 	    {
5561 		/* Need to concatenate the remainder of the previous call and
5562 		 * the bytes of the current call.  Use the end of the
5563 		 * conversion buffer for this. */
5564 		fromlen = len + ip->bw_restlen;
5565 		from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5566 		mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5567 		mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5568 	    }
5569 	    else
5570 	    {
5571 		from = buf;
5572 		fromlen = len;
5573 	    }
5574 
5575 	    if (enc2macroman(from, fromlen,
5576 			ip->bw_conv_buf, &len, ip->bw_conv_buflen,
5577 			ip->bw_rest, &ip->bw_restlen) == FAIL)
5578 	    {
5579 		ip->bw_conv_error = TRUE;
5580 		return FAIL;
5581 	    }
5582 	    buf = ip->bw_conv_buf;
5583 	}
5584 #endif
5585 
5586 #ifdef USE_ICONV
5587 	if (ip->bw_iconv_fd != (iconv_t)-1)
5588 	{
5589 	    const char	*from;
5590 	    size_t	fromlen;
5591 	    char	*to;
5592 	    size_t	tolen;
5593 
5594 	    /* Convert with iconv(). */
5595 	    if (ip->bw_restlen > 0)
5596 	    {
5597 		char *fp;
5598 
5599 		/* Need to concatenate the remainder of the previous call and
5600 		 * the bytes of the current call.  Use the end of the
5601 		 * conversion buffer for this. */
5602 		fromlen = len + ip->bw_restlen;
5603 		fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5604 		mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen);
5605 		mch_memmove(fp + ip->bw_restlen, buf, (size_t)len);
5606 		from = fp;
5607 		tolen = ip->bw_conv_buflen - fromlen;
5608 	    }
5609 	    else
5610 	    {
5611 		from = (const char *)buf;
5612 		fromlen = len;
5613 		tolen = ip->bw_conv_buflen;
5614 	    }
5615 	    to = (char *)ip->bw_conv_buf;
5616 
5617 	    if (ip->bw_first)
5618 	    {
5619 		size_t	save_len = tolen;
5620 
5621 		/* output the initial shift state sequence */
5622 		(void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen);
5623 
5624 		/* There is a bug in iconv() on Linux (which appears to be
5625 		 * wide-spread) which sets "to" to NULL and messes up "tolen".
5626 		 */
5627 		if (to == NULL)
5628 		{
5629 		    to = (char *)ip->bw_conv_buf;
5630 		    tolen = save_len;
5631 		}
5632 		ip->bw_first = FALSE;
5633 	    }
5634 
5635 	    /*
5636 	     * If iconv() has an error or there is not enough room, fail.
5637 	     */
5638 	    if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen)
5639 			== (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
5640 						    || fromlen > CONV_RESTLEN)
5641 	    {
5642 		ip->bw_conv_error = TRUE;
5643 		return FAIL;
5644 	    }
5645 
5646 	    /* copy remainder to ip->bw_rest[] to be used for the next call. */
5647 	    if (fromlen > 0)
5648 		mch_memmove(ip->bw_rest, (void *)from, fromlen);
5649 	    ip->bw_restlen = (int)fromlen;
5650 
5651 	    buf = ip->bw_conv_buf;
5652 	    len = (int)((char_u *)to - ip->bw_conv_buf);
5653 	}
5654 #endif
5655     }
5656 
5657     if (ip->bw_fd < 0)
5658 	/* Only checking conversion, which is OK if we get here. */
5659 	return OK;
5660 
5661 #ifdef FEAT_CRYPT
5662     if (flags & FIO_ENCRYPTED)
5663     {
5664 	/* Encrypt the data. Do it in-place if possible, otherwise use an
5665 	 * allocated buffer. */
5666 # ifdef CRYPT_NOT_INPLACE
5667 	if (crypt_works_inplace(ip->bw_buffer->b_cryptstate))
5668 	{
5669 # endif
5670 	    crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len);
5671 # ifdef CRYPT_NOT_INPLACE
5672 	}
5673 	else
5674 	{
5675 	    char_u *outbuf;
5676 
5677 	    len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf);
5678 	    if (len == 0)
5679 		return OK;  /* Crypt layer is buffering, will flush later. */
5680 	    wlen = write_eintr(ip->bw_fd, outbuf, len);
5681 	    vim_free(outbuf);
5682 	    return (wlen < len) ? FAIL : OK;
5683 	}
5684 # endif
5685     }
5686 #endif
5687 
5688     wlen = write_eintr(ip->bw_fd, buf, len);
5689     return (wlen < len) ? FAIL : OK;
5690 }
5691 
5692 /*
5693  * Convert a Unicode character to bytes.
5694  * Return TRUE for an error, FALSE when it's OK.
5695  */
5696     static int
5697 ucs2bytes(
5698     unsigned	c,		/* in: character */
5699     char_u	**pp,		/* in/out: pointer to result */
5700     int		flags)		/* FIO_ flags */
5701 {
5702     char_u	*p = *pp;
5703     int		error = FALSE;
5704     int		cc;
5705 
5706 
5707     if (flags & FIO_UCS4)
5708     {
5709 	if (flags & FIO_ENDIAN_L)
5710 	{
5711 	    *p++ = c;
5712 	    *p++ = (c >> 8);
5713 	    *p++ = (c >> 16);
5714 	    *p++ = (c >> 24);
5715 	}
5716 	else
5717 	{
5718 	    *p++ = (c >> 24);
5719 	    *p++ = (c >> 16);
5720 	    *p++ = (c >> 8);
5721 	    *p++ = c;
5722 	}
5723     }
5724     else if (flags & (FIO_UCS2 | FIO_UTF16))
5725     {
5726 	if (c >= 0x10000)
5727 	{
5728 	    if (flags & FIO_UTF16)
5729 	    {
5730 		/* Make two words, ten bits of the character in each.  First
5731 		 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */
5732 		c -= 0x10000;
5733 		if (c >= 0x100000)
5734 		    error = TRUE;
5735 		cc = ((c >> 10) & 0x3ff) + 0xd800;
5736 		if (flags & FIO_ENDIAN_L)
5737 		{
5738 		    *p++ = cc;
5739 		    *p++ = ((unsigned)cc >> 8);
5740 		}
5741 		else
5742 		{
5743 		    *p++ = ((unsigned)cc >> 8);
5744 		    *p++ = cc;
5745 		}
5746 		c = (c & 0x3ff) + 0xdc00;
5747 	    }
5748 	    else
5749 		error = TRUE;
5750 	}
5751 	if (flags & FIO_ENDIAN_L)
5752 	{
5753 	    *p++ = c;
5754 	    *p++ = (c >> 8);
5755 	}
5756 	else
5757 	{
5758 	    *p++ = (c >> 8);
5759 	    *p++ = c;
5760 	}
5761     }
5762     else    /* Latin1 */
5763     {
5764 	if (c >= 0x100)
5765 	{
5766 	    error = TRUE;
5767 	    *p++ = 0xBF;
5768 	}
5769 	else
5770 	    *p++ = c;
5771     }
5772 
5773     *pp = p;
5774     return error;
5775 }
5776 
5777 /*
5778  * Return TRUE if file encoding "fenc" requires conversion from or to
5779  * 'encoding'.
5780  */
5781     static int
5782 need_conversion(char_u *fenc)
5783 {
5784     int		same_encoding;
5785     int		enc_flags;
5786     int		fenc_flags;
5787 
5788     if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
5789     {
5790 	same_encoding = TRUE;
5791 	fenc_flags = 0;
5792     }
5793     else
5794     {
5795 	/* Ignore difference between "ansi" and "latin1", "ucs-4" and
5796 	 * "ucs-4be", etc. */
5797 	enc_flags = get_fio_flags(p_enc);
5798 	fenc_flags = get_fio_flags(fenc);
5799 	same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
5800     }
5801     if (same_encoding)
5802     {
5803 	/* Specified encoding matches with 'encoding'.  This requires
5804 	 * conversion when 'encoding' is Unicode but not UTF-8. */
5805 	return enc_unicode != 0;
5806     }
5807 
5808     /* Encodings differ.  However, conversion is not needed when 'enc' is any
5809      * Unicode encoding and the file is UTF-8. */
5810     return !(enc_utf8 && fenc_flags == FIO_UTF8);
5811 }
5812 
5813 /*
5814  * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
5815  * internal conversion.
5816  * if "ptr" is an empty string, use 'encoding'.
5817  */
5818     static int
5819 get_fio_flags(char_u *ptr)
5820 {
5821     int		prop;
5822 
5823     if (*ptr == NUL)
5824 	ptr = p_enc;
5825 
5826     prop = enc_canon_props(ptr);
5827     if (prop & ENC_UNICODE)
5828     {
5829 	if (prop & ENC_2BYTE)
5830 	{
5831 	    if (prop & ENC_ENDIAN_L)
5832 		return FIO_UCS2 | FIO_ENDIAN_L;
5833 	    return FIO_UCS2;
5834 	}
5835 	if (prop & ENC_4BYTE)
5836 	{
5837 	    if (prop & ENC_ENDIAN_L)
5838 		return FIO_UCS4 | FIO_ENDIAN_L;
5839 	    return FIO_UCS4;
5840 	}
5841 	if (prop & ENC_2WORD)
5842 	{
5843 	    if (prop & ENC_ENDIAN_L)
5844 		return FIO_UTF16 | FIO_ENDIAN_L;
5845 	    return FIO_UTF16;
5846 	}
5847 	return FIO_UTF8;
5848     }
5849     if (prop & ENC_LATIN1)
5850 	return FIO_LATIN1;
5851     /* must be ENC_DBCS, requires iconv() */
5852     return 0;
5853 }
5854 
5855 #ifdef WIN3264
5856 /*
5857  * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
5858  * for the conversion MS-Windows can do for us.  Also accept "utf-8".
5859  * Used for conversion between 'encoding' and 'fileencoding'.
5860  */
5861     static int
5862 get_win_fio_flags(char_u *ptr)
5863 {
5864     int		cp;
5865 
5866     /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */
5867     if (!enc_utf8 && enc_codepage <= 0)
5868 	return 0;
5869 
5870     cp = encname2codepage(ptr);
5871     if (cp == 0)
5872     {
5873 #  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
5874 	if (STRCMP(ptr, "utf-8") == 0)
5875 	    cp = CP_UTF8;
5876 	else
5877 #  endif
5878 	    return 0;
5879     }
5880     return FIO_PUT_CP(cp) | FIO_CODEPAGE;
5881 }
5882 #endif
5883 
5884 #ifdef MACOS_CONVERT
5885 /*
5886  * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
5887  * needed for the internal conversion to/from utf-8 or latin1.
5888  */
5889     static int
5890 get_mac_fio_flags(char_u *ptr)
5891 {
5892     if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
5893 				     && (enc_canon_props(ptr) & ENC_MACROMAN))
5894 	return FIO_MACROMAN;
5895     return 0;
5896 }
5897 #endif
5898 
5899 /*
5900  * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
5901  * "size" must be at least 2.
5902  * Return the name of the encoding and set "*lenp" to the length.
5903  * Returns NULL when no BOM found.
5904  */
5905     static char_u *
5906 check_for_bom(
5907     char_u	*p,
5908     long	size,
5909     int		*lenp,
5910     int		flags)
5911 {
5912     char	*name = NULL;
5913     int		len = 2;
5914 
5915     if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
5916 	    && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
5917     {
5918 	name = "utf-8";		/* EF BB BF */
5919 	len = 3;
5920     }
5921     else if (p[0] == 0xff && p[1] == 0xfe)
5922     {
5923 	if (size >= 4 && p[2] == 0 && p[3] == 0
5924 	    && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
5925 	{
5926 	    name = "ucs-4le";	/* FF FE 00 00 */
5927 	    len = 4;
5928 	}
5929 	else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
5930 	    name = "ucs-2le";	/* FF FE */
5931 	else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
5932 	    /* utf-16le is preferred, it also works for ucs-2le text */
5933 	    name = "utf-16le";	/* FF FE */
5934     }
5935     else if (p[0] == 0xfe && p[1] == 0xff
5936 	    && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
5937     {
5938 	/* Default to utf-16, it works also for ucs-2 text. */
5939 	if (flags == FIO_UCS2)
5940 	    name = "ucs-2";	/* FE FF */
5941 	else
5942 	    name = "utf-16";	/* FE FF */
5943     }
5944     else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
5945 	    && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
5946     {
5947 	name = "ucs-4";		/* 00 00 FE FF */
5948 	len = 4;
5949     }
5950 
5951     *lenp = len;
5952     return (char_u *)name;
5953 }
5954 
5955 /*
5956  * Generate a BOM in "buf[4]" for encoding "name".
5957  * Return the length of the BOM (zero when no BOM).
5958  */
5959     static int
5960 make_bom(char_u *buf, char_u *name)
5961 {
5962     int		flags;
5963     char_u	*p;
5964 
5965     flags = get_fio_flags(name);
5966 
5967     /* Can't put a BOM in a non-Unicode file. */
5968     if (flags == FIO_LATIN1 || flags == 0)
5969 	return 0;
5970 
5971     if (flags == FIO_UTF8)	/* UTF-8 */
5972     {
5973 	buf[0] = 0xef;
5974 	buf[1] = 0xbb;
5975 	buf[2] = 0xbf;
5976 	return 3;
5977     }
5978     p = buf;
5979     (void)ucs2bytes(0xfeff, &p, flags);
5980     return (int)(p - buf);
5981 }
5982 
5983 /*
5984  * Try to find a shortname by comparing the fullname with the current
5985  * directory.
5986  * Returns "full_path" or pointer into "full_path" if shortened.
5987  */
5988     char_u *
5989 shorten_fname1(char_u *full_path)
5990 {
5991     char_u	*dirname;
5992     char_u	*p = full_path;
5993 
5994     dirname = alloc(MAXPATHL);
5995     if (dirname == NULL)
5996 	return full_path;
5997     if (mch_dirname(dirname, MAXPATHL) == OK)
5998     {
5999 	p = shorten_fname(full_path, dirname);
6000 	if (p == NULL || *p == NUL)
6001 	    p = full_path;
6002     }
6003     vim_free(dirname);
6004     return p;
6005 }
6006 
6007 /*
6008  * Try to find a shortname by comparing the fullname with the current
6009  * directory.
6010  * Returns NULL if not shorter name possible, pointer into "full_path"
6011  * otherwise.
6012  */
6013     char_u *
6014 shorten_fname(char_u *full_path, char_u *dir_name)
6015 {
6016     int		len;
6017     char_u	*p;
6018 
6019     if (full_path == NULL)
6020 	return NULL;
6021     len = (int)STRLEN(dir_name);
6022     if (fnamencmp(dir_name, full_path, len) == 0)
6023     {
6024 	p = full_path + len;
6025 #if defined(MSWIN)
6026 	/*
6027 	 * MSWIN: when a file is in the root directory, dir_name will end in a
6028 	 * slash, since C: by itself does not define a specific dir. In this
6029 	 * case p may already be correct. <negri>
6030 	 */
6031 	if (!((len > 2) && (*(p - 2) == ':')))
6032 #endif
6033 	{
6034 	    if (vim_ispathsep(*p))
6035 		++p;
6036 #ifndef VMS   /* the path separator is always part of the path */
6037 	    else
6038 		p = NULL;
6039 #endif
6040 	}
6041     }
6042 #if defined(MSWIN)
6043     /*
6044      * When using a file in the current drive, remove the drive name:
6045      * "A:\dir\file" -> "\dir\file".  This helps when moving a session file on
6046      * a floppy from "A:\dir" to "B:\dir".
6047      */
6048     else if (len > 3
6049 	    && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
6050 	    && full_path[1] == ':'
6051 	    && vim_ispathsep(full_path[2]))
6052 	p = full_path + 2;
6053 #endif
6054     else
6055 	p = NULL;
6056     return p;
6057 }
6058 
6059 /*
6060  * Shorten filename of a buffer.
6061  * When "force" is TRUE: Use full path from now on for files currently being
6062  * edited, both for file name and swap file name.  Try to shorten the file
6063  * names a bit, if safe to do so.
6064  * When "force" is FALSE: Only try to shorten absolute file names.
6065  * For buffers that have buftype "nofile" or "scratch": never change the file
6066  * name.
6067  */
6068     void
6069 shorten_buf_fname(buf_T *buf, char_u *dirname, int force)
6070 {
6071     char_u	*p;
6072 
6073     if (buf->b_fname != NULL
6074 #ifdef FEAT_QUICKFIX
6075 	    && !bt_nofile(buf)
6076 #endif
6077 	    && !path_with_url(buf->b_fname)
6078 	    && (force
6079 		|| buf->b_sfname == NULL
6080 		|| mch_isFullName(buf->b_sfname)))
6081     {
6082 	if (buf->b_sfname != buf->b_ffname)
6083 	    VIM_CLEAR(buf->b_sfname);
6084 	p = shorten_fname(buf->b_ffname, dirname);
6085 	if (p != NULL)
6086 	{
6087 	    buf->b_sfname = vim_strsave(p);
6088 	    buf->b_fname = buf->b_sfname;
6089 	}
6090 	if (p == NULL || buf->b_fname == NULL)
6091 	    buf->b_fname = buf->b_ffname;
6092     }
6093 }
6094 
6095 /*
6096  * Shorten filenames for all buffers.
6097  */
6098     void
6099 shorten_fnames(int force)
6100 {
6101     char_u	dirname[MAXPATHL];
6102     buf_T	*buf;
6103 
6104     mch_dirname(dirname, MAXPATHL);
6105     FOR_ALL_BUFFERS(buf)
6106     {
6107 	shorten_buf_fname(buf, dirname, force);
6108 
6109 	/* Always make the swap file name a full path, a "nofile" buffer may
6110 	 * also have a swap file. */
6111 	mf_fullname(buf->b_ml.ml_mfp);
6112     }
6113     status_redraw_all();
6114     redraw_tabline = TRUE;
6115 }
6116 
6117 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
6118 	|| defined(FEAT_GUI_MSWIN) \
6119 	|| defined(FEAT_GUI_MAC) \
6120 	|| defined(PROTO)
6121 /*
6122  * Shorten all filenames in "fnames[count]" by current directory.
6123  */
6124     void
6125 shorten_filenames(char_u **fnames, int count)
6126 {
6127     int		i;
6128     char_u	dirname[MAXPATHL];
6129     char_u	*p;
6130 
6131     if (fnames == NULL || count < 1)
6132 	return;
6133     mch_dirname(dirname, sizeof(dirname));
6134     for (i = 0; i < count; ++i)
6135     {
6136 	if ((p = shorten_fname(fnames[i], dirname)) != NULL)
6137 	{
6138 	    /* shorten_fname() returns pointer in given "fnames[i]".  If free
6139 	     * "fnames[i]" first, "p" becomes invalid.  So we need to copy
6140 	     * "p" first then free fnames[i]. */
6141 	    p = vim_strsave(p);
6142 	    vim_free(fnames[i]);
6143 	    fnames[i] = p;
6144 	}
6145     }
6146 }
6147 #endif
6148 
6149 /*
6150  * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
6151  * fo_o_h.ext for MSDOS or when shortname option set.
6152  *
6153  * Assumed that fname is a valid name found in the filesystem we assure that
6154  * the return value is a different name and ends in 'ext'.
6155  * "ext" MUST be at most 4 characters long if it starts with a dot, 3
6156  * characters otherwise.
6157  * Space for the returned name is allocated, must be freed later.
6158  * Returns NULL when out of memory.
6159  */
6160     char_u *
6161 modname(
6162     char_u *fname,
6163     char_u *ext,
6164     int	    prepend_dot)	/* may prepend a '.' to file name */
6165 {
6166     return buf_modname((curbuf->b_p_sn || curbuf->b_shortname),
6167 						     fname, ext, prepend_dot);
6168 }
6169 
6170     char_u *
6171 buf_modname(
6172     int	    shortname,		/* use 8.3 file name */
6173     char_u  *fname,
6174     char_u  *ext,
6175     int	    prepend_dot)	/* may prepend a '.' to file name */
6176 {
6177     char_u	*retval;
6178     char_u	*s;
6179     char_u	*e;
6180     char_u	*ptr;
6181     int		fnamelen, extlen;
6182 
6183     extlen = (int)STRLEN(ext);
6184 
6185     /*
6186      * If there is no file name we must get the name of the current directory
6187      * (we need the full path in case :cd is used).
6188      */
6189     if (fname == NULL || *fname == NUL)
6190     {
6191 	retval = alloc((unsigned)(MAXPATHL + extlen + 3));
6192 	if (retval == NULL)
6193 	    return NULL;
6194 	if (mch_dirname(retval, MAXPATHL) == FAIL ||
6195 				     (fnamelen = (int)STRLEN(retval)) == 0)
6196 	{
6197 	    vim_free(retval);
6198 	    return NULL;
6199 	}
6200 	if (!after_pathsep(retval, retval + fnamelen))
6201 	{
6202 	    retval[fnamelen++] = PATHSEP;
6203 	    retval[fnamelen] = NUL;
6204 	}
6205 	prepend_dot = FALSE;	    /* nothing to prepend a dot to */
6206     }
6207     else
6208     {
6209 	fnamelen = (int)STRLEN(fname);
6210 	retval = alloc((unsigned)(fnamelen + extlen + 3));
6211 	if (retval == NULL)
6212 	    return NULL;
6213 	STRCPY(retval, fname);
6214 #ifdef VMS
6215 	vms_remove_version(retval); /* we do not need versions here */
6216 #endif
6217     }
6218 
6219     /*
6220      * search backwards until we hit a '/', '\' or ':' replacing all '.'
6221      * by '_' for MSDOS or when shortname option set and ext starts with a dot.
6222      * Then truncate what is after the '/', '\' or ':' to 8 characters for
6223      * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
6224      */
6225     for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr))
6226     {
6227 	if (*ext == '.'
6228 #ifdef USE_LONG_FNAME
6229 		    && (!USE_LONG_FNAME || shortname)
6230 #else
6231 		    && shortname
6232 #endif
6233 								)
6234 	    if (*ptr == '.')	/* replace '.' by '_' */
6235 		*ptr = '_';
6236 	if (vim_ispathsep(*ptr))
6237 	{
6238 	    ++ptr;
6239 	    break;
6240 	}
6241     }
6242 
6243     /* the file name has at most BASENAMELEN characters. */
6244     if (STRLEN(ptr) > (unsigned)BASENAMELEN)
6245 	ptr[BASENAMELEN] = '\0';
6246 
6247     s = ptr + STRLEN(ptr);
6248 
6249     /*
6250      * For 8.3 file names we may have to reduce the length.
6251      */
6252 #ifdef USE_LONG_FNAME
6253     if (!USE_LONG_FNAME || shortname)
6254 #else
6255     if (shortname)
6256 #endif
6257     {
6258 	/*
6259 	 * If there is no file name, or the file name ends in '/', and the
6260 	 * extension starts with '.', put a '_' before the dot, because just
6261 	 * ".ext" is invalid.
6262 	 */
6263 	if (fname == NULL || *fname == NUL
6264 				   || vim_ispathsep(fname[STRLEN(fname) - 1]))
6265 	{
6266 	    if (*ext == '.')
6267 		*s++ = '_';
6268 	}
6269 	/*
6270 	 * If the extension starts with '.', truncate the base name at 8
6271 	 * characters
6272 	 */
6273 	else if (*ext == '.')
6274 	{
6275 	    if ((size_t)(s - ptr) > (size_t)8)
6276 	    {
6277 		s = ptr + 8;
6278 		*s = '\0';
6279 	    }
6280 	}
6281 	/*
6282 	 * If the extension doesn't start with '.', and the file name
6283 	 * doesn't have an extension yet, append a '.'
6284 	 */
6285 	else if ((e = vim_strchr(ptr, '.')) == NULL)
6286 	    *s++ = '.';
6287 	/*
6288 	 * If the extension doesn't start with '.', and there already is an
6289 	 * extension, it may need to be truncated
6290 	 */
6291 	else if ((int)STRLEN(e) + extlen > 4)
6292 	    s = e + 4 - extlen;
6293     }
6294 #if defined(USE_LONG_FNAME) || defined(WIN3264)
6295     /*
6296      * If there is no file name, and the extension starts with '.', put a
6297      * '_' before the dot, because just ".ext" may be invalid if it's on a
6298      * FAT partition, and on HPFS it doesn't matter.
6299      */
6300     else if ((fname == NULL || *fname == NUL) && *ext == '.')
6301 	*s++ = '_';
6302 #endif
6303 
6304     /*
6305      * Append the extension.
6306      * ext can start with '.' and cannot exceed 3 more characters.
6307      */
6308     STRCPY(s, ext);
6309 
6310     /*
6311      * Prepend the dot.
6312      */
6313     if (prepend_dot && !shortname && *(e = gettail(retval)) != '.'
6314 #ifdef USE_LONG_FNAME
6315 	    && USE_LONG_FNAME
6316 #endif
6317 				)
6318     {
6319 	STRMOVE(e + 1, e);
6320 	*e = '.';
6321     }
6322 
6323     /*
6324      * Check that, after appending the extension, the file name is really
6325      * different.
6326      */
6327     if (fname != NULL && STRCMP(fname, retval) == 0)
6328     {
6329 	/* we search for a character that can be replaced by '_' */
6330 	while (--s >= ptr)
6331 	{
6332 	    if (*s != '_')
6333 	    {
6334 		*s = '_';
6335 		break;
6336 	    }
6337 	}
6338 	if (s < ptr)	/* fname was "________.<ext>", how tricky! */
6339 	    *ptr = 'v';
6340     }
6341     return retval;
6342 }
6343 
6344 /*
6345  * Like fgets(), but if the file line is too long, it is truncated and the
6346  * rest of the line is thrown away.  Returns TRUE for end-of-file.
6347  * If the line is truncated then buf[size - 2] will not be NUL.
6348  */
6349     int
6350 vim_fgets(char_u *buf, int size, FILE *fp)
6351 {
6352     char	*eof;
6353 #define FGETS_SIZE 200
6354     char	tbuf[FGETS_SIZE];
6355 
6356     buf[size - 2] = NUL;
6357 #ifdef USE_CR
6358     eof = fgets_cr((char *)buf, size, fp);
6359 #else
6360     eof = fgets((char *)buf, size, fp);
6361 #endif
6362     if (buf[size - 2] != NUL && buf[size - 2] != '\n')
6363     {
6364 	buf[size - 1] = NUL;	    /* Truncate the line */
6365 
6366 	/* Now throw away the rest of the line: */
6367 	do
6368 	{
6369 	    tbuf[FGETS_SIZE - 2] = NUL;
6370 #ifdef USE_CR
6371 	    vim_ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp);
6372 #else
6373 	    vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
6374 #endif
6375 	} while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
6376     }
6377     return (eof == NULL);
6378 }
6379 
6380 #if defined(USE_CR) || defined(PROTO)
6381 /*
6382  * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF.
6383  * Returns TRUE for end-of-file.
6384  * Only used for the Mac, because it's much slower than vim_fgets().
6385  */
6386     int
6387 tag_fgets(char_u *buf, int size, FILE *fp)
6388 {
6389     int		i = 0;
6390     int		c;
6391     int		eof = FALSE;
6392 
6393     for (;;)
6394     {
6395 	c = fgetc(fp);
6396 	if (c == EOF)
6397 	{
6398 	    eof = TRUE;
6399 	    break;
6400 	}
6401 	if (c == '\r')
6402 	{
6403 	    /* Always store a NL for end-of-line. */
6404 	    if (i < size - 1)
6405 		buf[i++] = '\n';
6406 	    c = fgetc(fp);
6407 	    if (c != '\n')	/* Macintosh format: single CR. */
6408 		ungetc(c, fp);
6409 	    break;
6410 	}
6411 	if (i < size - 1)
6412 	    buf[i++] = c;
6413 	if (c == '\n')
6414 	    break;
6415     }
6416     buf[i] = NUL;
6417     return eof;
6418 }
6419 #endif
6420 
6421 /*
6422  * rename() only works if both files are on the same file system, this
6423  * function will (attempts to?) copy the file across if rename fails -- webb
6424  * Return -1 for failure, 0 for success.
6425  */
6426     int
6427 vim_rename(char_u *from, char_u *to)
6428 {
6429     int		fd_in;
6430     int		fd_out;
6431     int		n;
6432     char	*errmsg = NULL;
6433     char	*buffer;
6434 #ifdef AMIGA
6435     BPTR	flock;
6436 #endif
6437     stat_T	st;
6438     long	perm;
6439 #ifdef HAVE_ACL
6440     vim_acl_T	acl;		/* ACL from original file */
6441 #endif
6442     int		use_tmp_file = FALSE;
6443 
6444     /*
6445      * When the names are identical, there is nothing to do.  When they refer
6446      * to the same file (ignoring case and slash/backslash differences) but
6447      * the file name differs we need to go through a temp file.
6448      */
6449     if (fnamecmp(from, to) == 0)
6450     {
6451 	if (p_fic && STRCMP(gettail(from), gettail(to)) != 0)
6452 	    use_tmp_file = TRUE;
6453 	else
6454 	    return 0;
6455     }
6456 
6457     /*
6458      * Fail if the "from" file doesn't exist.  Avoids that "to" is deleted.
6459      */
6460     if (mch_stat((char *)from, &st) < 0)
6461 	return -1;
6462 
6463 #ifdef UNIX
6464     {
6465 	stat_T	st_to;
6466 
6467 	/* It's possible for the source and destination to be the same file.
6468 	 * This happens when "from" and "to" differ in case and are on a FAT32
6469 	 * filesystem.  In that case go through a temp file name. */
6470 	if (mch_stat((char *)to, &st_to) >= 0
6471 		&& st.st_dev == st_to.st_dev
6472 		&& st.st_ino == st_to.st_ino)
6473 	    use_tmp_file = TRUE;
6474     }
6475 #endif
6476 #ifdef WIN3264
6477     {
6478 	BY_HANDLE_FILE_INFORMATION info1, info2;
6479 
6480 	/* It's possible for the source and destination to be the same file.
6481 	 * In that case go through a temp file name.  This makes rename("foo",
6482 	 * "./foo") a no-op (in a complicated way). */
6483 	if (win32_fileinfo(from, &info1) == FILEINFO_OK
6484 		&& win32_fileinfo(to, &info2) == FILEINFO_OK
6485 		&& info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
6486 		&& info1.nFileIndexHigh == info2.nFileIndexHigh
6487 		&& info1.nFileIndexLow == info2.nFileIndexLow)
6488 	    use_tmp_file = TRUE;
6489     }
6490 #endif
6491 
6492     if (use_tmp_file)
6493     {
6494 	char	tempname[MAXPATHL + 1];
6495 
6496 	/*
6497 	 * Find a name that doesn't exist and is in the same directory.
6498 	 * Rename "from" to "tempname" and then rename "tempname" to "to".
6499 	 */
6500 	if (STRLEN(from) >= MAXPATHL - 5)
6501 	    return -1;
6502 	STRCPY(tempname, from);
6503 	for (n = 123; n < 99999; ++n)
6504 	{
6505 	    sprintf((char *)gettail((char_u *)tempname), "%d", n);
6506 	    if (mch_stat(tempname, &st) < 0)
6507 	    {
6508 		if (mch_rename((char *)from, tempname) == 0)
6509 		{
6510 		    if (mch_rename(tempname, (char *)to) == 0)
6511 			return 0;
6512 		    /* Strange, the second step failed.  Try moving the
6513 		     * file back and return failure. */
6514 		    mch_rename(tempname, (char *)from);
6515 		    return -1;
6516 		}
6517 		/* If it fails for one temp name it will most likely fail
6518 		 * for any temp name, give up. */
6519 		return -1;
6520 	    }
6521 	}
6522 	return -1;
6523     }
6524 
6525     /*
6526      * Delete the "to" file, this is required on some systems to make the
6527      * mch_rename() work, on other systems it makes sure that we don't have
6528      * two files when the mch_rename() fails.
6529      */
6530 
6531 #ifdef AMIGA
6532     /*
6533      * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
6534      * that the name of the "to" file is the same as the "from" file, even
6535      * though the names are different. To avoid the chance of accidentally
6536      * deleting the "from" file (horror!) we lock it during the remove.
6537      *
6538      * When used for making a backup before writing the file: This should not
6539      * happen with ":w", because startscript() should detect this problem and
6540      * set buf->b_shortname, causing modname() to return a correct ".bak" file
6541      * name.  This problem does exist with ":w filename", but then the
6542      * original file will be somewhere else so the backup isn't really
6543      * important. If autoscripting is off the rename may fail.
6544      */
6545     flock = Lock((UBYTE *)from, (long)ACCESS_READ);
6546 #endif
6547     mch_remove(to);
6548 #ifdef AMIGA
6549     if (flock)
6550 	UnLock(flock);
6551 #endif
6552 
6553     /*
6554      * First try a normal rename, return if it works.
6555      */
6556     if (mch_rename((char *)from, (char *)to) == 0)
6557 	return 0;
6558 
6559     /*
6560      * Rename() failed, try copying the file.
6561      */
6562     perm = mch_getperm(from);
6563 #ifdef HAVE_ACL
6564     /* For systems that support ACL: get the ACL from the original file. */
6565     acl = mch_get_acl(from);
6566 #endif
6567     fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
6568     if (fd_in == -1)
6569     {
6570 #ifdef HAVE_ACL
6571 	mch_free_acl(acl);
6572 #endif
6573 	return -1;
6574     }
6575 
6576     /* Create the new file with same permissions as the original. */
6577     fd_out = mch_open((char *)to,
6578 		       O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
6579     if (fd_out == -1)
6580     {
6581 	close(fd_in);
6582 #ifdef HAVE_ACL
6583 	mch_free_acl(acl);
6584 #endif
6585 	return -1;
6586     }
6587 
6588     buffer = (char *)alloc(BUFSIZE);
6589     if (buffer == NULL)
6590     {
6591 	close(fd_out);
6592 	close(fd_in);
6593 #ifdef HAVE_ACL
6594 	mch_free_acl(acl);
6595 #endif
6596 	return -1;
6597     }
6598 
6599     while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0)
6600 	if (write_eintr(fd_out, buffer, n) != n)
6601 	{
6602 	    errmsg = _("E208: Error writing to \"%s\"");
6603 	    break;
6604 	}
6605 
6606     vim_free(buffer);
6607     close(fd_in);
6608     if (close(fd_out) < 0)
6609 	errmsg = _("E209: Error closing \"%s\"");
6610     if (n < 0)
6611     {
6612 	errmsg = _("E210: Error reading \"%s\"");
6613 	to = from;
6614     }
6615 #ifndef UNIX	    /* for Unix mch_open() already set the permission */
6616     mch_setperm(to, perm);
6617 #endif
6618 #ifdef HAVE_ACL
6619     mch_set_acl(to, acl);
6620     mch_free_acl(acl);
6621 #endif
6622 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
6623     mch_copy_sec(from, to);
6624 #endif
6625     if (errmsg != NULL)
6626     {
6627 	semsg(errmsg, to);
6628 	return -1;
6629     }
6630     mch_remove(from);
6631     return 0;
6632 }
6633 
6634 static int already_warned = FALSE;
6635 
6636 /*
6637  * Check if any not hidden buffer has been changed.
6638  * Postpone the check if there are characters in the stuff buffer, a global
6639  * command is being executed, a mapping is being executed or an autocommand is
6640  * busy.
6641  * Returns TRUE if some message was written (screen should be redrawn and
6642  * cursor positioned).
6643  */
6644     int
6645 check_timestamps(
6646     int		focus)		/* called for GUI focus event */
6647 {
6648     buf_T	*buf;
6649     int		didit = 0;
6650     int		n;
6651 
6652     /* Don't check timestamps while system() or another low-level function may
6653      * cause us to lose and gain focus. */
6654     if (no_check_timestamps > 0)
6655 	return FALSE;
6656 
6657     /* Avoid doing a check twice.  The OK/Reload dialog can cause a focus
6658      * event and we would keep on checking if the file is steadily growing.
6659      * Do check again after typing something. */
6660     if (focus && did_check_timestamps)
6661     {
6662 	need_check_timestamps = TRUE;
6663 	return FALSE;
6664     }
6665 
6666     if (!stuff_empty() || global_busy || !typebuf_typed()
6667 			|| autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
6668 	need_check_timestamps = TRUE;		/* check later */
6669     else
6670     {
6671 	++no_wait_return;
6672 	did_check_timestamps = TRUE;
6673 	already_warned = FALSE;
6674 	FOR_ALL_BUFFERS(buf)
6675 	{
6676 	    /* Only check buffers in a window. */
6677 	    if (buf->b_nwindows > 0)
6678 	    {
6679 		bufref_T bufref;
6680 
6681 		set_bufref(&bufref, buf);
6682 		n = buf_check_timestamp(buf, focus);
6683 		if (didit < n)
6684 		    didit = n;
6685 		if (n > 0 && !bufref_valid(&bufref))
6686 		{
6687 		    /* Autocommands have removed the buffer, start at the
6688 		     * first one again. */
6689 		    buf = firstbuf;
6690 		    continue;
6691 		}
6692 	    }
6693 	}
6694 	--no_wait_return;
6695 	need_check_timestamps = FALSE;
6696 	if (need_wait_return && didit == 2)
6697 	{
6698 	    /* make sure msg isn't overwritten */
6699 	    msg_puts("\n");
6700 	    out_flush();
6701 	}
6702     }
6703     return didit;
6704 }
6705 
6706 /*
6707  * Move all the lines from buffer "frombuf" to buffer "tobuf".
6708  * Return OK or FAIL.  When FAIL "tobuf" is incomplete and/or "frombuf" is not
6709  * empty.
6710  */
6711     static int
6712 move_lines(buf_T *frombuf, buf_T *tobuf)
6713 {
6714     buf_T	*tbuf = curbuf;
6715     int		retval = OK;
6716     linenr_T	lnum;
6717     char_u	*p;
6718 
6719     /* Copy the lines in "frombuf" to "tobuf". */
6720     curbuf = tobuf;
6721     for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
6722     {
6723 	p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
6724 	if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
6725 	{
6726 	    vim_free(p);
6727 	    retval = FAIL;
6728 	    break;
6729 	}
6730 	vim_free(p);
6731     }
6732 
6733     /* Delete all the lines in "frombuf". */
6734     if (retval != FAIL)
6735     {
6736 	curbuf = frombuf;
6737 	for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
6738 	    if (ml_delete(lnum, FALSE) == FAIL)
6739 	    {
6740 		/* Oops!  We could try putting back the saved lines, but that
6741 		 * might fail again... */
6742 		retval = FAIL;
6743 		break;
6744 	    }
6745     }
6746 
6747     curbuf = tbuf;
6748     return retval;
6749 }
6750 
6751 /*
6752  * Check if buffer "buf" has been changed.
6753  * Also check if the file for a new buffer unexpectedly appeared.
6754  * return 1 if a changed buffer was found.
6755  * return 2 if a message has been displayed.
6756  * return 0 otherwise.
6757  */
6758     int
6759 buf_check_timestamp(
6760     buf_T	*buf,
6761     int		focus UNUSED)	/* called for GUI focus event */
6762 {
6763     stat_T	st;
6764     int		stat_res;
6765     int		retval = 0;
6766     char_u	*path;
6767     char	*tbuf;
6768     char	*mesg = NULL;
6769     char	*mesg2 = "";
6770     int		helpmesg = FALSE;
6771     int		reload = FALSE;
6772     char	*reason;
6773 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6774     int		can_reload = FALSE;
6775 #endif
6776     off_T	orig_size = buf->b_orig_size;
6777     int		orig_mode = buf->b_orig_mode;
6778 #ifdef FEAT_GUI
6779     int		save_mouse_correct = need_mouse_correct;
6780 #endif
6781     static int	busy = FALSE;
6782     int		n;
6783 #ifdef FEAT_EVAL
6784     char_u	*s;
6785 #endif
6786     bufref_T	bufref;
6787 
6788     set_bufref(&bufref, buf);
6789 
6790     /* If there is no file name, the buffer is not loaded, 'buftype' is
6791      * set, we are in the middle of a save or being called recursively: ignore
6792      * this buffer. */
6793     if (buf->b_ffname == NULL
6794 	    || buf->b_ml.ml_mfp == NULL
6795 	    || !bt_normal(buf)
6796 	    || buf->b_saving
6797 	    || busy
6798 #ifdef FEAT_NETBEANS_INTG
6799 	    || isNetbeansBuffer(buf)
6800 #endif
6801 #ifdef FEAT_TERMINAL
6802 	    || buf->b_term != NULL
6803 #endif
6804 	    )
6805 	return 0;
6806 
6807     if (       !(buf->b_flags & BF_NOTEDITED)
6808 	    && buf->b_mtime != 0
6809 	    && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
6810 		|| time_differs((long)st.st_mtime, buf->b_mtime)
6811 		|| st.st_size != buf->b_orig_size
6812 #ifdef HAVE_ST_MODE
6813 		|| (int)st.st_mode != buf->b_orig_mode
6814 #else
6815 		|| mch_getperm(buf->b_ffname) != buf->b_orig_mode
6816 #endif
6817 		))
6818     {
6819 	retval = 1;
6820 
6821 	// set b_mtime to stop further warnings (e.g., when executing
6822 	// FileChangedShell autocmd)
6823 	if (stat_res < 0)
6824 	{
6825 	    // When 'autoread' is set we'll check the file again to see if it
6826 	    // re-appears.
6827 	    buf->b_mtime = buf->b_p_ar;
6828 	    buf->b_orig_size = 0;
6829 	    buf->b_orig_mode = 0;
6830 	}
6831 	else
6832 	    buf_store_time(buf, &st, buf->b_ffname);
6833 
6834 	/* Don't do anything for a directory.  Might contain the file
6835 	 * explorer. */
6836 	if (mch_isdir(buf->b_fname))
6837 	    ;
6838 
6839 	/*
6840 	 * If 'autoread' is set, the buffer has no changes and the file still
6841 	 * exists, reload the buffer.  Use the buffer-local option value if it
6842 	 * was set, the global option value otherwise.
6843 	 */
6844 	else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
6845 				       && !bufIsChanged(buf) && stat_res >= 0)
6846 	    reload = TRUE;
6847 	else
6848 	{
6849 	    if (stat_res < 0)
6850 		reason = "deleted";
6851 	    else if (bufIsChanged(buf))
6852 		reason = "conflict";
6853 	    /*
6854 	     * Check if the file contents really changed to avoid giving a
6855 	     * warning when only the timestamp was set (e.g., checked out of
6856 	     * CVS).  Always warn when the buffer was changed.
6857 	     */
6858 	    else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
6859 		reason = "changed";
6860 	    else if (orig_mode != buf->b_orig_mode)
6861 		reason = "mode";
6862 	    else
6863 		reason = "time";
6864 
6865 	    /*
6866 	     * Only give the warning if there are no FileChangedShell
6867 	     * autocommands.
6868 	     * Avoid being called recursively by setting "busy".
6869 	     */
6870 	    busy = TRUE;
6871 #ifdef FEAT_EVAL
6872 	    set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
6873 	    set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
6874 #endif
6875 	    ++allbuf_lock;
6876 	    n = apply_autocmds(EVENT_FILECHANGEDSHELL,
6877 				      buf->b_fname, buf->b_fname, FALSE, buf);
6878 	    --allbuf_lock;
6879 	    busy = FALSE;
6880 	    if (n)
6881 	    {
6882 		if (!bufref_valid(&bufref))
6883 		    emsg(_("E246: FileChangedShell autocommand deleted buffer"));
6884 #ifdef FEAT_EVAL
6885 		s = get_vim_var_str(VV_FCS_CHOICE);
6886 		if (STRCMP(s, "reload") == 0 && *reason != 'd')
6887 		    reload = TRUE;
6888 		else if (STRCMP(s, "ask") == 0)
6889 		    n = FALSE;
6890 		else
6891 #endif
6892 		    return 2;
6893 	    }
6894 	    if (!n)
6895 	    {
6896 		if (*reason == 'd')
6897 		    mesg = _("E211: File \"%s\" no longer available");
6898 		else
6899 		{
6900 		    helpmesg = TRUE;
6901 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6902 		    can_reload = TRUE;
6903 #endif
6904 		    if (reason[2] == 'n')
6905 		    {
6906 			mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
6907 			mesg2 = _("See \":help W12\" for more info.");
6908 		    }
6909 		    else if (reason[1] == 'h')
6910 		    {
6911 			mesg = _("W11: Warning: File \"%s\" has changed since editing started");
6912 			mesg2 = _("See \":help W11\" for more info.");
6913 		    }
6914 		    else if (*reason == 'm')
6915 		    {
6916 			mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
6917 			mesg2 = _("See \":help W16\" for more info.");
6918 		    }
6919 		    else
6920 			/* Only timestamp changed, store it to avoid a warning
6921 			 * in check_mtime() later. */
6922 			buf->b_mtime_read = buf->b_mtime;
6923 		}
6924 	    }
6925 	}
6926 
6927     }
6928     else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
6929 						&& vim_fexists(buf->b_ffname))
6930     {
6931 	retval = 1;
6932 	mesg = _("W13: Warning: File \"%s\" has been created after editing started");
6933 	buf->b_flags |= BF_NEW_W;
6934 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6935 	can_reload = TRUE;
6936 #endif
6937     }
6938 
6939     if (mesg != NULL)
6940     {
6941 	path = home_replace_save(buf, buf->b_fname);
6942 	if (path != NULL)
6943 	{
6944 	    if (!helpmesg)
6945 		mesg2 = "";
6946 	    tbuf = (char *)alloc((unsigned)(STRLEN(path) + STRLEN(mesg)
6947 							+ STRLEN(mesg2) + 2));
6948 	    sprintf(tbuf, mesg, path);
6949 #ifdef FEAT_EVAL
6950 	    /* Set warningmsg here, before the unimportant and output-specific
6951 	     * mesg2 has been appended. */
6952 	    set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1);
6953 #endif
6954 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6955 	    if (can_reload)
6956 	    {
6957 		if (*mesg2 != NUL)
6958 		{
6959 		    STRCAT(tbuf, "\n");
6960 		    STRCAT(tbuf, mesg2);
6961 		}
6962 		if (do_dialog(VIM_WARNING, (char_u *)_("Warning"),
6963 			    (char_u *)tbuf,
6964 			  (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
6965 		    reload = TRUE;
6966 	    }
6967 	    else
6968 #endif
6969 	    if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
6970 	    {
6971 		if (*mesg2 != NUL)
6972 		{
6973 		    STRCAT(tbuf, "; ");
6974 		    STRCAT(tbuf, mesg2);
6975 		}
6976 		emsg(tbuf);
6977 		retval = 2;
6978 	    }
6979 	    else
6980 	    {
6981 		if (!autocmd_busy)
6982 		{
6983 		    msg_start();
6984 		    msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
6985 		    if (*mesg2 != NUL)
6986 			msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST);
6987 		    msg_clr_eos();
6988 		    (void)msg_end();
6989 		    if (emsg_silent == 0)
6990 		    {
6991 			out_flush();
6992 #ifdef FEAT_GUI
6993 			if (!focus)
6994 #endif
6995 			    /* give the user some time to think about it */
6996 			    ui_delay(1000L, TRUE);
6997 
6998 			/* don't redraw and erase the message */
6999 			redraw_cmdline = FALSE;
7000 		    }
7001 		}
7002 		already_warned = TRUE;
7003 	    }
7004 
7005 	    vim_free(path);
7006 	    vim_free(tbuf);
7007 	}
7008     }
7009 
7010     if (reload)
7011     {
7012 	/* Reload the buffer. */
7013 	buf_reload(buf, orig_mode);
7014 #ifdef FEAT_PERSISTENT_UNDO
7015 	if (buf->b_p_udf && buf->b_ffname != NULL)
7016 	{
7017 	    char_u	    hash[UNDO_HASH_SIZE];
7018 	    buf_T	    *save_curbuf = curbuf;
7019 
7020 	    /* Any existing undo file is unusable, write it now. */
7021 	    curbuf = buf;
7022 	    u_compute_hash(hash);
7023 	    u_write_undo(NULL, FALSE, buf, hash);
7024 	    curbuf = save_curbuf;
7025 	}
7026 #endif
7027     }
7028 
7029     /* Trigger FileChangedShell when the file was changed in any way. */
7030     if (bufref_valid(&bufref) && retval != 0)
7031 	(void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
7032 				      buf->b_fname, buf->b_fname, FALSE, buf);
7033 #ifdef FEAT_GUI
7034     /* restore this in case an autocommand has set it; it would break
7035      * 'mousefocus' */
7036     need_mouse_correct = save_mouse_correct;
7037 #endif
7038 
7039     return retval;
7040 }
7041 
7042 /*
7043  * Reload a buffer that is already loaded.
7044  * Used when the file was changed outside of Vim.
7045  * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
7046  * buf->b_orig_mode may have been reset already.
7047  */
7048     void
7049 buf_reload(buf_T *buf, int orig_mode)
7050 {
7051     exarg_T	ea;
7052     pos_T	old_cursor;
7053     linenr_T	old_topline;
7054     int		old_ro = buf->b_p_ro;
7055     buf_T	*savebuf;
7056     bufref_T	bufref;
7057     int		saved = OK;
7058     aco_save_T	aco;
7059     int		flags = READ_NEW;
7060 
7061     /* set curwin/curbuf for "buf" and save some things */
7062     aucmd_prepbuf(&aco, buf);
7063 
7064     /* We only want to read the text from the file, not reset the syntax
7065      * highlighting, clear marks, diff status, etc.  Force the fileformat
7066      * and encoding to be the same. */
7067     if (prep_exarg(&ea, buf) == OK)
7068     {
7069 	old_cursor = curwin->w_cursor;
7070 	old_topline = curwin->w_topline;
7071 
7072 	if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
7073 	{
7074 	    /* Save all the text, so that the reload can be undone.
7075 	     * Sync first so that this is a separate undo-able action. */
7076 	    u_sync(FALSE);
7077 	    saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
7078 	    flags |= READ_KEEP_UNDO;
7079 	}
7080 
7081 	/*
7082 	 * To behave like when a new file is edited (matters for
7083 	 * BufReadPost autocommands) we first need to delete the current
7084 	 * buffer contents.  But if reading the file fails we should keep
7085 	 * the old contents.  Can't use memory only, the file might be
7086 	 * too big.  Use a hidden buffer to move the buffer contents to.
7087 	 */
7088 	if (BUFEMPTY() || saved == FAIL)
7089 	    savebuf = NULL;
7090 	else
7091 	{
7092 	    /* Allocate a buffer without putting it in the buffer list. */
7093 	    savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
7094 	    set_bufref(&bufref, savebuf);
7095 	    if (savebuf != NULL && buf == curbuf)
7096 	    {
7097 		/* Open the memline. */
7098 		curbuf = savebuf;
7099 		curwin->w_buffer = savebuf;
7100 		saved = ml_open(curbuf);
7101 		curbuf = buf;
7102 		curwin->w_buffer = buf;
7103 	    }
7104 	    if (savebuf == NULL || saved == FAIL || buf != curbuf
7105 				      || move_lines(buf, savebuf) == FAIL)
7106 	    {
7107 		semsg(_("E462: Could not prepare for reloading \"%s\""),
7108 							    buf->b_fname);
7109 		saved = FAIL;
7110 	    }
7111 	}
7112 
7113 	if (saved == OK)
7114 	{
7115 	    curbuf->b_flags |= BF_CHECK_RO;	/* check for RO again */
7116 	    keep_filetype = TRUE;		/* don't detect 'filetype' */
7117 	    if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
7118 			(linenr_T)0,
7119 			(linenr_T)MAXLNUM, &ea, flags) != OK)
7120 	    {
7121 #if defined(FEAT_EVAL)
7122 		if (!aborting())
7123 #endif
7124 		    semsg(_("E321: Could not reload \"%s\""), buf->b_fname);
7125 		if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf)
7126 		{
7127 		    /* Put the text back from the save buffer.  First
7128 		     * delete any lines that readfile() added. */
7129 		    while (!BUFEMPTY())
7130 			if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL)
7131 			    break;
7132 		    (void)move_lines(savebuf, buf);
7133 		}
7134 	    }
7135 	    else if (buf == curbuf)  /* "buf" still valid */
7136 	    {
7137 		/* Mark the buffer as unmodified and free undo info. */
7138 		unchanged(buf, TRUE);
7139 		if ((flags & READ_KEEP_UNDO) == 0)
7140 		{
7141 		    u_blockfree(buf);
7142 		    u_clearall(buf);
7143 		}
7144 		else
7145 		{
7146 		    /* Mark all undo states as changed. */
7147 		    u_unchanged(curbuf);
7148 		}
7149 	    }
7150 	}
7151 	vim_free(ea.cmd);
7152 
7153 	if (savebuf != NULL && bufref_valid(&bufref))
7154 	    wipe_buffer(savebuf, FALSE);
7155 
7156 #ifdef FEAT_DIFF
7157 	/* Invalidate diff info if necessary. */
7158 	diff_invalidate(curbuf);
7159 #endif
7160 
7161 	/* Restore the topline and cursor position and check it (lines may
7162 	 * have been removed). */
7163 	if (old_topline > curbuf->b_ml.ml_line_count)
7164 	    curwin->w_topline = curbuf->b_ml.ml_line_count;
7165 	else
7166 	    curwin->w_topline = old_topline;
7167 	curwin->w_cursor = old_cursor;
7168 	check_cursor();
7169 	update_topline();
7170 	keep_filetype = FALSE;
7171 #ifdef FEAT_FOLDING
7172 	{
7173 	    win_T	*wp;
7174 	    tabpage_T	*tp;
7175 
7176 	    /* Update folds unless they are defined manually. */
7177 	    FOR_ALL_TAB_WINDOWS(tp, wp)
7178 		if (wp->w_buffer == curwin->w_buffer
7179 			&& !foldmethodIsManual(wp))
7180 		    foldUpdateAll(wp);
7181 	}
7182 #endif
7183 	/* If the mode didn't change and 'readonly' was set, keep the old
7184 	 * value; the user probably used the ":view" command.  But don't
7185 	 * reset it, might have had a read error. */
7186 	if (orig_mode == curbuf->b_orig_mode)
7187 	    curbuf->b_p_ro |= old_ro;
7188 
7189 	/* Modelines must override settings done by autocommands. */
7190 	do_modelines(0);
7191     }
7192 
7193     /* restore curwin/curbuf and a few other things */
7194     aucmd_restbuf(&aco);
7195     /* Careful: autocommands may have made "buf" invalid! */
7196 }
7197 
7198     void
7199 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED)
7200 {
7201     buf->b_mtime = (long)st->st_mtime;
7202     buf->b_orig_size = st->st_size;
7203 #ifdef HAVE_ST_MODE
7204     buf->b_orig_mode = (int)st->st_mode;
7205 #else
7206     buf->b_orig_mode = mch_getperm(fname);
7207 #endif
7208 }
7209 
7210 /*
7211  * Adjust the line with missing eol, used for the next write.
7212  * Used for do_filter(), when the input lines for the filter are deleted.
7213  */
7214     void
7215 write_lnum_adjust(linenr_T offset)
7216 {
7217     if (curbuf->b_no_eol_lnum != 0)	/* only if there is a missing eol */
7218 	curbuf->b_no_eol_lnum += offset;
7219 }
7220 
7221 #if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO)
7222 /*
7223  * Delete "name" and everything in it, recursively.
7224  * return 0 for succes, -1 if some file was not deleted.
7225  */
7226     int
7227 delete_recursive(char_u *name)
7228 {
7229     int result = 0;
7230     char_u	**files;
7231     int		file_count;
7232     int		i;
7233     char_u	*exp;
7234 
7235     /* A symbolic link to a directory itself is deleted, not the directory it
7236      * points to. */
7237     if (
7238 # if defined(UNIX) || defined(WIN32)
7239 	 mch_isrealdir(name)
7240 # else
7241 	 mch_isdir(name)
7242 # endif
7243 	    )
7244     {
7245 	vim_snprintf((char *)NameBuff, MAXPATHL, "%s/*", name);
7246 	exp = vim_strsave(NameBuff);
7247 	if (exp == NULL)
7248 	    return -1;
7249 	if (gen_expand_wildcards(1, &exp, &file_count, &files,
7250 	      EW_DIR|EW_FILE|EW_SILENT|EW_ALLLINKS|EW_DODOT|EW_EMPTYOK) == OK)
7251 	{
7252 	    for (i = 0; i < file_count; ++i)
7253 		if (delete_recursive(files[i]) != 0)
7254 		    result = -1;
7255 	    FreeWild(file_count, files);
7256 	}
7257 	else
7258 	    result = -1;
7259 	vim_free(exp);
7260 	(void)mch_rmdir(name);
7261     }
7262     else
7263 	result = mch_remove(name) == 0 ? 0 : -1;
7264 
7265     return result;
7266 }
7267 #endif
7268 
7269 #if defined(TEMPDIRNAMES) || defined(PROTO)
7270 static long	temp_count = 0;		/* Temp filename counter. */
7271 
7272 /*
7273  * Delete the temp directory and all files it contains.
7274  */
7275     void
7276 vim_deltempdir(void)
7277 {
7278     if (vim_tempdir != NULL)
7279     {
7280 	/* remove the trailing path separator */
7281 	gettail(vim_tempdir)[-1] = NUL;
7282 	delete_recursive(vim_tempdir);
7283 	VIM_CLEAR(vim_tempdir);
7284     }
7285 }
7286 
7287 /*
7288  * Directory "tempdir" was created.  Expand this name to a full path and put
7289  * it in "vim_tempdir".  This avoids that using ":cd" would confuse us.
7290  * "tempdir" must be no longer than MAXPATHL.
7291  */
7292     static void
7293 vim_settempdir(char_u *tempdir)
7294 {
7295     char_u	*buf;
7296 
7297     buf = alloc((unsigned)MAXPATHL + 2);
7298     if (buf != NULL)
7299     {
7300 	if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
7301 	    STRCPY(buf, tempdir);
7302 	add_pathsep(buf);
7303 	vim_tempdir = vim_strsave(buf);
7304 	vim_free(buf);
7305     }
7306 }
7307 #endif
7308 
7309 /*
7310  * vim_tempname(): Return a unique name that can be used for a temp file.
7311  *
7312  * The temp file is NOT guaranteed to be created.  If "keep" is FALSE it is
7313  * guaranteed to NOT be created.
7314  *
7315  * The returned pointer is to allocated memory.
7316  * The returned pointer is NULL if no valid name was found.
7317  */
7318     char_u  *
7319 vim_tempname(
7320     int	    extra_char UNUSED,  /* char to use in the name instead of '?' */
7321     int	    keep UNUSED)
7322 {
7323 #ifdef USE_TMPNAM
7324     char_u	itmp[L_tmpnam];	/* use tmpnam() */
7325 #else
7326     char_u	itmp[TEMPNAMELEN];
7327 #endif
7328 
7329 #ifdef TEMPDIRNAMES
7330     static char	*(tempdirs[]) = {TEMPDIRNAMES};
7331     int		i;
7332 # ifndef EEXIST
7333     stat_T	st;
7334 # endif
7335 
7336     /*
7337      * This will create a directory for private use by this instance of Vim.
7338      * This is done once, and the same directory is used for all temp files.
7339      * This method avoids security problems because of symlink attacks et al.
7340      * It's also a bit faster, because we only need to check for an existing
7341      * file when creating the directory and not for each temp file.
7342      */
7343     if (vim_tempdir == NULL)
7344     {
7345 	/*
7346 	 * Try the entries in TEMPDIRNAMES to create the temp directory.
7347 	 */
7348 	for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
7349 	{
7350 # ifndef HAVE_MKDTEMP
7351 	    size_t	itmplen;
7352 	    long	nr;
7353 	    long	off;
7354 # endif
7355 
7356 	    /* Expand $TMP, leave room for "/v1100000/999999999".
7357 	     * Skip the directory check if the expansion fails. */
7358 	    expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
7359 	    if (itmp[0] != '$' && mch_isdir(itmp))
7360 	    {
7361 		/* directory exists */
7362 		add_pathsep(itmp);
7363 
7364 # ifdef HAVE_MKDTEMP
7365 		{
7366 #  if defined(UNIX) || defined(VMS)
7367 		    /* Make sure the umask doesn't remove the executable bit.
7368 		     * "repl" has been reported to use "177". */
7369 		    mode_t	umask_save = umask(077);
7370 #  endif
7371 		    /* Leave room for filename */
7372 		    STRCAT(itmp, "vXXXXXX");
7373 		    if (mkdtemp((char *)itmp) != NULL)
7374 			vim_settempdir(itmp);
7375 #  if defined(UNIX) || defined(VMS)
7376 		    (void)umask(umask_save);
7377 #  endif
7378 		}
7379 # else
7380 		/* Get an arbitrary number of up to 6 digits.  When it's
7381 		 * unlikely that it already exists it will be faster,
7382 		 * otherwise it doesn't matter.  The use of mkdir() avoids any
7383 		 * security problems because of the predictable number. */
7384 		nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
7385 		itmplen = STRLEN(itmp);
7386 
7387 		/* Try up to 10000 different values until we find a name that
7388 		 * doesn't exist. */
7389 		for (off = 0; off < 10000L; ++off)
7390 		{
7391 		    int		r;
7392 #  if defined(UNIX) || defined(VMS)
7393 		    mode_t	umask_save;
7394 #  endif
7395 
7396 		    sprintf((char *)itmp + itmplen, "v%ld", nr + off);
7397 #  ifndef EEXIST
7398 		    /* If mkdir() does not set errno to EEXIST, check for
7399 		     * existing file here.  There is a race condition then,
7400 		     * although it's fail-safe. */
7401 		    if (mch_stat((char *)itmp, &st) >= 0)
7402 			continue;
7403 #  endif
7404 #  if defined(UNIX) || defined(VMS)
7405 		    /* Make sure the umask doesn't remove the executable bit.
7406 		     * "repl" has been reported to use "177". */
7407 		    umask_save = umask(077);
7408 #  endif
7409 		    r = vim_mkdir(itmp, 0700);
7410 #  if defined(UNIX) || defined(VMS)
7411 		    (void)umask(umask_save);
7412 #  endif
7413 		    if (r == 0)
7414 		    {
7415 			vim_settempdir(itmp);
7416 			break;
7417 		    }
7418 #  ifdef EEXIST
7419 		    /* If the mkdir() didn't fail because the file/dir exists,
7420 		     * we probably can't create any dir here, try another
7421 		     * place. */
7422 		    if (errno != EEXIST)
7423 #  endif
7424 			break;
7425 		}
7426 # endif /* HAVE_MKDTEMP */
7427 		if (vim_tempdir != NULL)
7428 		    break;
7429 	    }
7430 	}
7431     }
7432 
7433     if (vim_tempdir != NULL)
7434     {
7435 	/* There is no need to check if the file exists, because we own the
7436 	 * directory and nobody else creates a file in it. */
7437 	sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
7438 	return vim_strsave(itmp);
7439     }
7440 
7441     return NULL;
7442 
7443 #else /* TEMPDIRNAMES */
7444 
7445 # ifdef WIN3264
7446     char	szTempFile[_MAX_PATH + 1];
7447     char	buf4[4];
7448     char_u	*retval;
7449     char_u	*p;
7450 
7451     STRCPY(itmp, "");
7452     if (GetTempPath(_MAX_PATH, szTempFile) == 0)
7453     {
7454 	szTempFile[0] = '.';	/* GetTempPath() failed, use current dir */
7455 	szTempFile[1] = NUL;
7456     }
7457     strcpy(buf4, "VIM");
7458     buf4[2] = extra_char;   /* make it "VIa", "VIb", etc. */
7459     if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0)
7460 	return NULL;
7461     if (!keep)
7462 	/* GetTempFileName() will create the file, we don't want that */
7463 	(void)DeleteFile((LPSTR)itmp);
7464 
7465     /* Backslashes in a temp file name cause problems when filtering with
7466      * "sh".  NOTE: This also checks 'shellcmdflag' to help those people who
7467      * didn't set 'shellslash'. */
7468     retval = vim_strsave(itmp);
7469     if (*p_shcf == '-' || p_ssl)
7470 	for (p = retval; *p; ++p)
7471 	    if (*p == '\\')
7472 		*p = '/';
7473     return retval;
7474 
7475 # else /* WIN3264 */
7476 
7477 #  ifdef USE_TMPNAM
7478     char_u	*p;
7479 
7480     /* tmpnam() will make its own name */
7481     p = tmpnam((char *)itmp);
7482     if (p == NULL || *p == NUL)
7483 	return NULL;
7484 #  else
7485     char_u	*p;
7486 
7487 #   ifdef VMS_TEMPNAM
7488     /* mktemp() is not working on VMS.  It seems to be
7489      * a do-nothing function. Therefore we use tempnam().
7490      */
7491     sprintf((char *)itmp, "VIM%c", extra_char);
7492     p = (char_u *)tempnam("tmp:", (char *)itmp);
7493     if (p != NULL)
7494     {
7495 	/* VMS will use '.LIS' if we don't explicitly specify an extension,
7496 	 * and VIM will then be unable to find the file later */
7497 	STRCPY(itmp, p);
7498 	STRCAT(itmp, ".txt");
7499 	free(p);
7500     }
7501     else
7502 	return NULL;
7503 #   else
7504     STRCPY(itmp, TEMPNAME);
7505     if ((p = vim_strchr(itmp, '?')) != NULL)
7506 	*p = extra_char;
7507     if (mktemp((char *)itmp) == NULL)
7508 	return NULL;
7509 #   endif
7510 #  endif
7511 
7512     return vim_strsave(itmp);
7513 # endif /* WIN3264 */
7514 #endif /* TEMPDIRNAMES */
7515 }
7516 
7517 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7518 /*
7519  * Convert all backslashes in fname to forward slashes in-place, unless when
7520  * it looks like a URL.
7521  */
7522     void
7523 forward_slash(char_u *fname)
7524 {
7525     char_u	*p;
7526 
7527     if (path_with_url(fname))
7528 	return;
7529     for (p = fname; *p != NUL; ++p)
7530 	/* The Big5 encoding can have '\' in the trail byte. */
7531 	if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
7532 	    ++p;
7533 	else if (*p == '\\')
7534 	    *p = '/';
7535 }
7536 #endif
7537 
7538 /*
7539  * Try matching a filename with a "pattern" ("prog" is NULL), or use the
7540  * precompiled regprog "prog" ("pattern" is NULL).  That avoids calling
7541  * vim_regcomp() often.
7542  * Used for autocommands and 'wildignore'.
7543  * Returns TRUE if there is a match, FALSE otherwise.
7544  */
7545     int
7546 match_file_pat(
7547     char_u	*pattern,		/* pattern to match with */
7548     regprog_T	**prog,			/* pre-compiled regprog or NULL */
7549     char_u	*fname,			/* full path of file name */
7550     char_u	*sfname,		/* short file name or NULL */
7551     char_u	*tail,			/* tail of path */
7552     int		allow_dirs)		/* allow matching with dir */
7553 {
7554     regmatch_T	regmatch;
7555     int		result = FALSE;
7556 
7557     regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */
7558     if (prog != NULL)
7559 	regmatch.regprog = *prog;
7560     else
7561 	regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
7562 
7563     /*
7564      * Try for a match with the pattern with:
7565      * 1. the full file name, when the pattern has a '/'.
7566      * 2. the short file name, when the pattern has a '/'.
7567      * 3. the tail of the file name, when the pattern has no '/'.
7568      */
7569     if (regmatch.regprog != NULL
7570 	     && ((allow_dirs
7571 		     && (vim_regexec(&regmatch, fname, (colnr_T)0)
7572 			 || (sfname != NULL
7573 			     && vim_regexec(&regmatch, sfname, (colnr_T)0))))
7574 		 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0))))
7575 	result = TRUE;
7576 
7577     if (prog != NULL)
7578 	*prog = regmatch.regprog;
7579     else
7580 	vim_regfree(regmatch.regprog);
7581     return result;
7582 }
7583 
7584 #if defined(FEAT_WILDIGN) || defined(PROTO)
7585 /*
7586  * Return TRUE if a file matches with a pattern in "list".
7587  * "list" is a comma-separated list of patterns, like 'wildignore'.
7588  * "sfname" is the short file name or NULL, "ffname" the long file name.
7589  */
7590     int
7591 match_file_list(char_u *list, char_u *sfname, char_u *ffname)
7592 {
7593     char_u	buf[100];
7594     char_u	*tail;
7595     char_u	*regpat;
7596     char	allow_dirs;
7597     int		match;
7598     char_u	*p;
7599 
7600     tail = gettail(sfname);
7601 
7602     /* try all patterns in 'wildignore' */
7603     p = list;
7604     while (*p)
7605     {
7606 	copy_option_part(&p, buf, 100, ",");
7607 	regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
7608 	if (regpat == NULL)
7609 	    break;
7610 	match = match_file_pat(regpat, NULL, ffname, sfname,
7611 						       tail, (int)allow_dirs);
7612 	vim_free(regpat);
7613 	if (match)
7614 	    return TRUE;
7615     }
7616     return FALSE;
7617 }
7618 #endif
7619 
7620 /*
7621  * Convert the given pattern "pat" which has shell style wildcards in it, into
7622  * a regular expression, and return the result in allocated memory.  If there
7623  * is a directory path separator to be matched, then TRUE is put in
7624  * allow_dirs, otherwise FALSE is put there -- webb.
7625  * Handle backslashes before special characters, like "\*" and "\ ".
7626  *
7627  * Returns NULL when out of memory.
7628  */
7629     char_u *
7630 file_pat_to_reg_pat(
7631     char_u	*pat,
7632     char_u	*pat_end,	/* first char after pattern or NULL */
7633     char	*allow_dirs,	/* Result passed back out in here */
7634     int		no_bslash UNUSED) /* Don't use a backward slash as pathsep */
7635 {
7636     int		size = 2; /* '^' at start, '$' at end */
7637     char_u	*endp;
7638     char_u	*reg_pat;
7639     char_u	*p;
7640     int		i;
7641     int		nested = 0;
7642     int		add_dollar = TRUE;
7643 
7644     if (allow_dirs != NULL)
7645 	*allow_dirs = FALSE;
7646     if (pat_end == NULL)
7647 	pat_end = pat + STRLEN(pat);
7648 
7649     for (p = pat; p < pat_end; p++)
7650     {
7651 	switch (*p)
7652 	{
7653 	    case '*':
7654 	    case '.':
7655 	    case ',':
7656 	    case '{':
7657 	    case '}':
7658 	    case '~':
7659 		size += 2;	/* extra backslash */
7660 		break;
7661 #ifdef BACKSLASH_IN_FILENAME
7662 	    case '\\':
7663 	    case '/':
7664 		size += 4;	/* could become "[\/]" */
7665 		break;
7666 #endif
7667 	    default:
7668 		size++;
7669 		if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
7670 		{
7671 		    ++p;
7672 		    ++size;
7673 		}
7674 		break;
7675 	}
7676     }
7677     reg_pat = alloc(size + 1);
7678     if (reg_pat == NULL)
7679 	return NULL;
7680 
7681     i = 0;
7682 
7683     if (pat[0] == '*')
7684 	while (pat[0] == '*' && pat < pat_end - 1)
7685 	    pat++;
7686     else
7687 	reg_pat[i++] = '^';
7688     endp = pat_end - 1;
7689     if (endp >= pat && *endp == '*')
7690     {
7691 	while (endp - pat > 0 && *endp == '*')
7692 	    endp--;
7693 	add_dollar = FALSE;
7694     }
7695     for (p = pat; *p && nested >= 0 && p <= endp; p++)
7696     {
7697 	switch (*p)
7698 	{
7699 	    case '*':
7700 		reg_pat[i++] = '.';
7701 		reg_pat[i++] = '*';
7702 		while (p[1] == '*')	/* "**" matches like "*" */
7703 		    ++p;
7704 		break;
7705 	    case '.':
7706 	    case '~':
7707 		reg_pat[i++] = '\\';
7708 		reg_pat[i++] = *p;
7709 		break;
7710 	    case '?':
7711 		reg_pat[i++] = '.';
7712 		break;
7713 	    case '\\':
7714 		if (p[1] == NUL)
7715 		    break;
7716 #ifdef BACKSLASH_IN_FILENAME
7717 		if (!no_bslash)
7718 		{
7719 		    /* translate:
7720 		     * "\x" to "\\x"  e.g., "dir\file"
7721 		     * "\*" to "\\.*" e.g., "dir\*.c"
7722 		     * "\?" to "\\."  e.g., "dir\??.c"
7723 		     * "\+" to "\+"   e.g., "fileX\+.c"
7724 		     */
7725 		    if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
7726 			    && p[1] != '+')
7727 		    {
7728 			reg_pat[i++] = '[';
7729 			reg_pat[i++] = '\\';
7730 			reg_pat[i++] = '/';
7731 			reg_pat[i++] = ']';
7732 			if (allow_dirs != NULL)
7733 			    *allow_dirs = TRUE;
7734 			break;
7735 		    }
7736 		}
7737 #endif
7738 		/* Undo escaping from ExpandEscape():
7739 		 * foo\?bar -> foo?bar
7740 		 * foo\%bar -> foo%bar
7741 		 * foo\,bar -> foo,bar
7742 		 * foo\ bar -> foo bar
7743 		 * Don't unescape \, * and others that are also special in a
7744 		 * regexp.
7745 		 * An escaped { must be unescaped since we use magic not
7746 		 * verymagic.  Use "\\\{n,m\}"" to get "\{n,m}".
7747 		 */
7748 		if (*++p == '?'
7749 #ifdef BACKSLASH_IN_FILENAME
7750 			&& no_bslash
7751 #endif
7752 			)
7753 		    reg_pat[i++] = '?';
7754 		else
7755 		    if (*p == ',' || *p == '%' || *p == '#'
7756 			       || vim_isspace(*p) || *p == '{' || *p == '}')
7757 			reg_pat[i++] = *p;
7758 		    else if (*p == '\\' && p[1] == '\\' && p[2] == '{')
7759 		    {
7760 			reg_pat[i++] = '\\';
7761 			reg_pat[i++] = '{';
7762 			p += 2;
7763 		    }
7764 		    else
7765 		    {
7766 			if (allow_dirs != NULL && vim_ispathsep(*p)
7767 #ifdef BACKSLASH_IN_FILENAME
7768 				&& (!no_bslash || *p != '\\')
7769 #endif
7770 				)
7771 			    *allow_dirs = TRUE;
7772 			reg_pat[i++] = '\\';
7773 			reg_pat[i++] = *p;
7774 		    }
7775 		break;
7776 #ifdef BACKSLASH_IN_FILENAME
7777 	    case '/':
7778 		reg_pat[i++] = '[';
7779 		reg_pat[i++] = '\\';
7780 		reg_pat[i++] = '/';
7781 		reg_pat[i++] = ']';
7782 		if (allow_dirs != NULL)
7783 		    *allow_dirs = TRUE;
7784 		break;
7785 #endif
7786 	    case '{':
7787 		reg_pat[i++] = '\\';
7788 		reg_pat[i++] = '(';
7789 		nested++;
7790 		break;
7791 	    case '}':
7792 		reg_pat[i++] = '\\';
7793 		reg_pat[i++] = ')';
7794 		--nested;
7795 		break;
7796 	    case ',':
7797 		if (nested)
7798 		{
7799 		    reg_pat[i++] = '\\';
7800 		    reg_pat[i++] = '|';
7801 		}
7802 		else
7803 		    reg_pat[i++] = ',';
7804 		break;
7805 	    default:
7806 		if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
7807 		    reg_pat[i++] = *p++;
7808 		else if (allow_dirs != NULL && vim_ispathsep(*p))
7809 		    *allow_dirs = TRUE;
7810 		reg_pat[i++] = *p;
7811 		break;
7812 	}
7813     }
7814     if (add_dollar)
7815 	reg_pat[i++] = '$';
7816     reg_pat[i] = NUL;
7817     if (nested != 0)
7818     {
7819 	if (nested < 0)
7820 	    emsg(_("E219: Missing {."));
7821 	else
7822 	    emsg(_("E220: Missing }."));
7823 	VIM_CLEAR(reg_pat);
7824     }
7825     return reg_pat;
7826 }
7827 
7828 #if defined(EINTR) || defined(PROTO)
7829 /*
7830  * Version of read() that retries when interrupted by EINTR (possibly
7831  * by a SIGWINCH).
7832  */
7833     long
7834 read_eintr(int fd, void *buf, size_t bufsize)
7835 {
7836     long ret;
7837 
7838     for (;;)
7839     {
7840 	ret = vim_read(fd, buf, bufsize);
7841 	if (ret >= 0 || errno != EINTR)
7842 	    break;
7843     }
7844     return ret;
7845 }
7846 
7847 /*
7848  * Version of write() that retries when interrupted by EINTR (possibly
7849  * by a SIGWINCH).
7850  */
7851     long
7852 write_eintr(int fd, void *buf, size_t bufsize)
7853 {
7854     long    ret = 0;
7855     long    wlen;
7856 
7857     /* Repeat the write() so long it didn't fail, other than being interrupted
7858      * by a signal. */
7859     while (ret < (long)bufsize)
7860     {
7861 	wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
7862 	if (wlen < 0)
7863 	{
7864 	    if (errno != EINTR)
7865 		break;
7866 	}
7867 	else
7868 	    ret += wlen;
7869     }
7870     return ret;
7871 }
7872 #endif
7873