xref: /vim-8.2.3635/src/fileio.c (revision e48a2dd3)
1 /* vi:set ts=8 sts=4 sw=4:
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 #ifdef FEAT_CRYPT
28 /* crypt_magic[0] is pkzip crypt, crypt_magic[1] is sha2+blowfish */
29 static char	*crypt_magic[] = {"VimCrypt~01!", "VimCrypt~02!"};
30 static char	crypt_magic_head[] = "VimCrypt~";
31 # define CRYPT_MAGIC_LEN	12		/* must be multiple of 4! */
32 
33 /* For blowfish, after the magic header, we store 8 bytes of salt and then 8
34  * bytes of seed (initialisation vector). */
35 static int	crypt_salt_len[] = {0, 8};
36 static int	crypt_seed_len[] = {0, 8};
37 #define CRYPT_SALT_LEN_MAX 8
38 #define CRYPT_SEED_LEN_MAX 8
39 #endif
40 
41 /* Is there any system that doesn't have access()? */
42 #define USE_MCH_ACCESS
43 
44 #if defined(sun) && defined(S_ISCHR)
45 # define OPEN_CHR_FILES
46 static int is_dev_fd_file(char_u *fname);
47 #endif
48 #ifdef FEAT_MBYTE
49 static char_u *next_fenc __ARGS((char_u **pp));
50 # ifdef FEAT_EVAL
51 static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp));
52 # endif
53 #endif
54 #ifdef FEAT_VIMINFO
55 static void check_marks_read __ARGS((void));
56 #endif
57 #ifdef FEAT_CRYPT
58 static int crypt_method_from_magic __ARGS((char *ptr, int len));
59 static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask));
60 #endif
61 #ifdef UNIX
62 static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
63 #endif
64 static int set_rw_fname __ARGS((char_u *fname, char_u *sfname));
65 static int msg_add_fileformat __ARGS((int eol_type));
66 static void msg_add_eol __ARGS((void));
67 static int check_mtime __ARGS((buf_T *buf, struct stat *s));
68 static int time_differs __ARGS((long t1, long t2));
69 #ifdef FEAT_AUTOCMD
70 static int apply_autocmds_exarg __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap));
71 static int au_find_group __ARGS((char_u *name));
72 
73 # define AUGROUP_DEFAULT    -1	    /* default autocmd group */
74 # define AUGROUP_ERROR	    -2	    /* erroneous autocmd group */
75 # define AUGROUP_ALL	    -3	    /* all autocmd groups */
76 #endif
77 
78 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE)
79 # define HAS_BW_FLAGS
80 # define FIO_LATIN1	0x01	/* convert Latin1 */
81 # define FIO_UTF8	0x02	/* convert UTF-8 */
82 # define FIO_UCS2	0x04	/* convert UCS-2 */
83 # define FIO_UCS4	0x08	/* convert UCS-4 */
84 # define FIO_UTF16	0x10	/* convert UTF-16 */
85 # ifdef WIN3264
86 #  define FIO_CODEPAGE	0x20	/* convert MS-Windows codepage */
87 #  define FIO_PUT_CP(x) (((x) & 0xffff) << 16)	/* put codepage in top word */
88 #  define FIO_GET_CP(x)	(((x)>>16) & 0xffff)	/* get codepage from top word */
89 # endif
90 # ifdef MACOS_X
91 #  define FIO_MACROMAN	0x20	/* convert MacRoman */
92 # endif
93 # define FIO_ENDIAN_L	0x80	/* little endian */
94 # define FIO_ENCRYPTED	0x1000	/* encrypt written bytes */
95 # define FIO_NOCONVERT	0x2000	/* skip encoding conversion */
96 # define FIO_UCSBOM	0x4000	/* check for BOM at start of file */
97 # define FIO_ALL	-1	/* allow all formats */
98 #endif
99 
100 /* When converting, a read() or write() may leave some bytes to be converted
101  * for the next call.  The value is guessed... */
102 #define CONV_RESTLEN 30
103 
104 /* We have to guess how much a sequence of bytes may expand when converting
105  * with iconv() to be able to allocate a buffer. */
106 #define ICONV_MULT 8
107 
108 /*
109  * Structure to pass arguments from buf_write() to buf_write_bytes().
110  */
111 struct bw_info
112 {
113     int		bw_fd;		/* file descriptor */
114     char_u	*bw_buf;	/* buffer with data to be written */
115     int		bw_len;		/* length of data */
116 #ifdef HAS_BW_FLAGS
117     int		bw_flags;	/* FIO_ flags */
118 #endif
119 #ifdef FEAT_MBYTE
120     char_u	bw_rest[CONV_RESTLEN]; /* not converted bytes */
121     int		bw_restlen;	/* nr of bytes in bw_rest[] */
122     int		bw_first;	/* first write call */
123     char_u	*bw_conv_buf;	/* buffer for writing converted chars */
124     int		bw_conv_buflen; /* size of bw_conv_buf */
125     int		bw_conv_error;	/* set for conversion error */
126     linenr_T	bw_conv_error_lnum;  /* first line with error or zero */
127     linenr_T	bw_start_lnum;  /* line number at start of buffer */
128 # ifdef USE_ICONV
129     iconv_t	bw_iconv_fd;	/* descriptor for iconv() or -1 */
130 # endif
131 #endif
132 };
133 
134 static int  buf_write_bytes __ARGS((struct bw_info *ip));
135 
136 #ifdef FEAT_MBYTE
137 static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
138 static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
139 static int need_conversion __ARGS((char_u *fenc));
140 static int get_fio_flags __ARGS((char_u *ptr));
141 static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
142 static int make_bom __ARGS((char_u *buf, char_u *name));
143 # ifdef WIN3264
144 static int get_win_fio_flags __ARGS((char_u *ptr));
145 # endif
146 # ifdef MACOS_X
147 static int get_mac_fio_flags __ARGS((char_u *ptr));
148 # endif
149 #endif
150 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf));
151 #ifdef TEMPDIRNAMES
152 static void vim_settempdir __ARGS((char_u *tempdir));
153 #endif
154 #ifdef FEAT_AUTOCMD
155 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
156 #endif
157 
158     void
159 filemess(buf, name, s, attr)
160     buf_T	*buf;
161     char_u	*name;
162     char_u	*s;
163     int		attr;
164 {
165     int		msg_scroll_save;
166 
167     if (msg_silent != 0)
168 	return;
169     msg_add_fname(buf, name);	    /* put file name in IObuff with quotes */
170     /* If it's extremely long, truncate it. */
171     if (STRLEN(IObuff) > IOSIZE - 80)
172 	IObuff[IOSIZE - 80] = NUL;
173     STRCAT(IObuff, s);
174     /*
175      * For the first message may have to start a new line.
176      * For further ones overwrite the previous one, reset msg_scroll before
177      * calling filemess().
178      */
179     msg_scroll_save = msg_scroll;
180     if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
181 	msg_scroll = FALSE;
182     if (!msg_scroll)	/* wait a bit when overwriting an error msg */
183 	check_for_delay(FALSE);
184     msg_start();
185     msg_scroll = msg_scroll_save;
186     msg_scrolled_ign = TRUE;
187     /* may truncate the message to avoid a hit-return prompt */
188     msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
189     msg_clr_eos();
190     out_flush();
191     msg_scrolled_ign = FALSE;
192 }
193 
194 /*
195  * Read lines from file "fname" into the buffer after line "from".
196  *
197  * 1. We allocate blocks with lalloc, as big as possible.
198  * 2. Each block is filled with characters from the file with a single read().
199  * 3. The lines are inserted in the buffer with ml_append().
200  *
201  * (caller must check that fname != NULL, unless READ_STDIN is used)
202  *
203  * "lines_to_skip" is the number of lines that must be skipped
204  * "lines_to_read" is the number of lines that are appended
205  * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
206  *
207  * flags:
208  * READ_NEW	starting to edit a new buffer
209  * READ_FILTER	reading filter output
210  * READ_STDIN	read from stdin instead of a file
211  * READ_BUFFER	read from curbuf instead of a file (converting after reading
212  *		stdin)
213  * READ_DUMMY	read into a dummy buffer (to check if file contents changed)
214  * READ_KEEP_UNDO  don't clear undo info or read it from a file
215  *
216  * return FAIL for failure, OK otherwise
217  */
218     int
219 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
220     char_u	*fname;
221     char_u	*sfname;
222     linenr_T	from;
223     linenr_T	lines_to_skip;
224     linenr_T	lines_to_read;
225     exarg_T	*eap;			/* can be NULL! */
226     int		flags;
227 {
228     int		fd = 0;
229     int		newfile = (flags & READ_NEW);
230     int		check_readonly;
231     int		filtering = (flags & READ_FILTER);
232     int		read_stdin = (flags & READ_STDIN);
233     int		read_buffer = (flags & READ_BUFFER);
234     int		set_options = newfile || read_buffer
235 					   || (eap != NULL && eap->read_edit);
236     linenr_T	read_buf_lnum = 1;	/* next line to read from curbuf */
237     colnr_T	read_buf_col = 0;	/* next char to read from this line */
238     char_u	c;
239     linenr_T	lnum = from;
240     char_u	*ptr = NULL;		/* pointer into read buffer */
241     char_u	*buffer = NULL;		/* read buffer */
242     char_u	*new_buffer = NULL;	/* init to shut up gcc */
243     char_u	*line_start = NULL;	/* init to shut up gcc */
244     int		wasempty;		/* buffer was empty before reading */
245     colnr_T	len;
246     long	size = 0;
247     char_u	*p;
248     off_t	filesize = 0;
249     int		skip_read = FALSE;
250 #ifdef FEAT_CRYPT
251     char_u	*cryptkey = NULL;
252     int		did_ask_for_key = FALSE;
253     int		crypt_method_used;
254 #endif
255 #ifdef FEAT_PERSISTENT_UNDO
256     context_sha256_T sha_ctx;
257     int		read_undo_file = FALSE;
258 #endif
259     int		split = 0;		/* number of split lines */
260 #define UNKNOWN	 0x0fffffff		/* file size is unknown */
261     linenr_T	linecnt;
262     int		error = FALSE;		/* errors encountered */
263     int		ff_error = EOL_UNKNOWN; /* file format with errors */
264     long	linerest = 0;		/* remaining chars in line */
265 #ifdef UNIX
266     int		perm = 0;
267     int		swap_mode = -1;		/* protection bits for swap file */
268 #else
269     int		perm;
270 #endif
271     int		fileformat = 0;		/* end-of-line format */
272     int		keep_fileformat = FALSE;
273     struct stat	st;
274     int		file_readonly;
275     linenr_T	skip_count = 0;
276     linenr_T	read_count = 0;
277     int		msg_save = msg_scroll;
278     linenr_T	read_no_eol_lnum = 0;   /* non-zero lnum when last line of
279 					 * last read was missing the eol */
280     int		try_mac = (vim_strchr(p_ffs, 'm') != NULL);
281     int		try_dos = (vim_strchr(p_ffs, 'd') != NULL);
282     int		try_unix = (vim_strchr(p_ffs, 'x') != NULL);
283     int		file_rewind = FALSE;
284 #ifdef FEAT_MBYTE
285     int		can_retry;
286     linenr_T	conv_error = 0;		/* line nr with conversion error */
287     linenr_T	illegal_byte = 0;	/* line nr with illegal byte */
288     int		keep_dest_enc = FALSE;	/* don't retry when char doesn't fit
289 					   in destination encoding */
290     int		bad_char_behavior = BAD_REPLACE;
291 					/* BAD_KEEP, BAD_DROP or character to
292 					 * replace with */
293     char_u	*tmpname = NULL;	/* name of 'charconvert' output file */
294     int		fio_flags = 0;
295     char_u	*fenc;			/* fileencoding to use */
296     int		fenc_alloced;		/* fenc_next is in allocated memory */
297     char_u	*fenc_next = NULL;	/* next item in 'fencs' or NULL */
298     int		advance_fenc = FALSE;
299     long	real_size = 0;
300 # ifdef USE_ICONV
301     iconv_t	iconv_fd = (iconv_t)-1;	/* descriptor for iconv() or -1 */
302 #  ifdef FEAT_EVAL
303     int		did_iconv = FALSE;	/* TRUE when iconv() failed and trying
304 					   'charconvert' next */
305 #  endif
306 # endif
307     int		converted = FALSE;	/* TRUE if conversion done */
308     int		notconverted = FALSE;	/* TRUE if conversion wanted but it
309 					   wasn't possible */
310     char_u	conv_rest[CONV_RESTLEN];
311     int		conv_restlen = 0;	/* nr of bytes in conv_rest[] */
312 #endif
313 #ifdef FEAT_AUTOCMD
314     buf_T	*old_curbuf;
315     char_u	*old_b_ffname;
316     char_u	*old_b_fname;
317     int		using_b_ffname;
318     int		using_b_fname;
319 #endif
320 
321     curbuf->b_no_eol_lnum = 0;	/* in case it was set by the previous read */
322 
323     /*
324      * If there is no file name yet, use the one for the read file.
325      * BF_NOTEDITED is set to reflect this.
326      * Don't do this for a read from a filter.
327      * Only do this when 'cpoptions' contains the 'f' flag.
328      */
329     if (curbuf->b_ffname == NULL
330 	    && !filtering
331 	    && fname != NULL
332 	    && vim_strchr(p_cpo, CPO_FNAMER) != NULL
333 	    && !(flags & READ_DUMMY))
334     {
335 	if (set_rw_fname(fname, sfname) == FAIL)
336 	    return FAIL;
337     }
338 
339 #ifdef FEAT_AUTOCMD
340     /* Remember the initial values of curbuf, curbuf->b_ffname and
341      * curbuf->b_fname to detect whether they are altered as a result of
342      * executing nasty autocommands.  Also check if "fname" and "sfname"
343      * point to one of these values. */
344     old_curbuf = curbuf;
345     old_b_ffname = curbuf->b_ffname;
346     old_b_fname = curbuf->b_fname;
347     using_b_ffname = (fname == curbuf->b_ffname)
348 					      || (sfname == curbuf->b_ffname);
349     using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
350 #endif
351 
352     /* After reading a file the cursor line changes but we don't want to
353      * display the line. */
354     ex_no_reprint = TRUE;
355 
356     /* don't display the file info for another buffer now */
357     need_fileinfo = FALSE;
358 
359     /*
360      * For Unix: Use the short file name whenever possible.
361      * Avoids problems with networks and when directory names are changed.
362      * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
363      * another directory, which we don't detect.
364      */
365     if (sfname == NULL)
366 	sfname = fname;
367 #if defined(UNIX) || defined(__EMX__)
368     fname = sfname;
369 #endif
370 
371 #ifdef FEAT_AUTOCMD
372     /*
373      * The BufReadCmd and FileReadCmd events intercept the reading process by
374      * executing the associated commands instead.
375      */
376     if (!filtering && !read_stdin && !read_buffer)
377     {
378 	pos_T	    pos;
379 
380 	pos = curbuf->b_op_start;
381 
382 	/* Set '[ mark to the line above where the lines go (line 1 if zero). */
383 	curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
384 	curbuf->b_op_start.col = 0;
385 
386 	if (newfile)
387 	{
388 	    if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
389 							  FALSE, curbuf, eap))
390 #ifdef FEAT_EVAL
391 		return aborting() ? FAIL : OK;
392 #else
393 		return OK;
394 #endif
395 	}
396 	else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
397 							    FALSE, NULL, eap))
398 #ifdef FEAT_EVAL
399 	    return aborting() ? FAIL : OK;
400 #else
401 	    return OK;
402 #endif
403 
404 	curbuf->b_op_start = pos;
405     }
406 #endif
407 
408     if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
409 	msg_scroll = FALSE;	/* overwrite previous file message */
410     else
411 	msg_scroll = TRUE;	/* don't overwrite previous file message */
412 
413     /*
414      * If the name ends in a path separator, we can't open it.  Check here,
415      * because reading the file may actually work, but then creating the swap
416      * file may destroy it!  Reported on MS-DOS and Win 95.
417      * If the name is too long we might crash further on, quit here.
418      */
419     if (fname != NULL && *fname != NUL)
420     {
421 	p = fname + STRLEN(fname);
422 	if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
423 	{
424 	    filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
425 	    msg_end();
426 	    msg_scroll = msg_save;
427 	    return FAIL;
428 	}
429     }
430 
431 #ifdef UNIX
432     /*
433      * On Unix it is possible to read a directory, so we have to
434      * check for it before the mch_open().
435      */
436     if (!read_stdin && !read_buffer)
437     {
438 	perm = mch_getperm(fname);
439 	if (perm >= 0 && !S_ISREG(perm)		    /* not a regular file ... */
440 # ifdef S_ISFIFO
441 		      && !S_ISFIFO(perm)	    /* ... or fifo */
442 # endif
443 # ifdef S_ISSOCK
444 		      && !S_ISSOCK(perm)	    /* ... or socket */
445 # endif
446 # ifdef OPEN_CHR_FILES
447 		      && !(S_ISCHR(perm) && is_dev_fd_file(fname))
448 			/* ... or a character special file named /dev/fd/<n> */
449 # endif
450 						)
451 	{
452 	    if (S_ISDIR(perm))
453 		filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
454 	    else
455 		filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
456 	    msg_end();
457 	    msg_scroll = msg_save;
458 	    return FAIL;
459 	}
460 
461 # if defined(MSDOS) || defined(MSWIN) || defined(OS2)
462 	/*
463 	 * MS-Windows allows opening a device, but we will probably get stuck
464 	 * trying to read it.
465 	 */
466 	if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
467 	{
468 	    filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
469 	    msg_end();
470 	    msg_scroll = msg_save;
471 	    return FAIL;
472 	}
473 # endif
474     }
475 #endif
476 
477     /* set default 'fileformat' */
478     if (set_options)
479     {
480 	if (eap != NULL && eap->force_ff != 0)
481 	    set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
482 	else if (*p_ffs != NUL)
483 	    set_fileformat(default_fileformat(), OPT_LOCAL);
484     }
485 
486     /* set or reset 'binary' */
487     if (eap != NULL && eap->force_bin != 0)
488     {
489 	int	oldval = curbuf->b_p_bin;
490 
491 	curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
492 	set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
493     }
494 
495     /*
496      * When opening a new file we take the readonly flag from the file.
497      * Default is r/w, can be set to r/o below.
498      * Don't reset it when in readonly mode
499      * Only set/reset b_p_ro when BF_CHECK_RO is set.
500      */
501     check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
502     if (check_readonly && !readonlymode)
503 	curbuf->b_p_ro = FALSE;
504 
505     if (newfile && !read_stdin && !read_buffer)
506     {
507 	/* Remember time of file. */
508 	if (mch_stat((char *)fname, &st) >= 0)
509 	{
510 	    buf_store_time(curbuf, &st, fname);
511 	    curbuf->b_mtime_read = curbuf->b_mtime;
512 #ifdef UNIX
513 	    /*
514 	     * Use the protection bits of the original file for the swap file.
515 	     * This makes it possible for others to read the name of the
516 	     * edited file from the swapfile, but only if they can read the
517 	     * edited file.
518 	     * Remove the "write" and "execute" bits for group and others
519 	     * (they must not write the swapfile).
520 	     * Add the "read" and "write" bits for the user, otherwise we may
521 	     * not be able to write to the file ourselves.
522 	     * Setting the bits is done below, after creating the swap file.
523 	     */
524 	    swap_mode = (st.st_mode & 0644) | 0600;
525 #endif
526 #ifdef FEAT_CW_EDITOR
527 	    /* Get the FSSpec on MacOS
528 	     * TODO: Update it properly when the buffer name changes
529 	     */
530 	    (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
531 #endif
532 #ifdef VMS
533 	    curbuf->b_fab_rfm = st.st_fab_rfm;
534 	    curbuf->b_fab_rat = st.st_fab_rat;
535 	    curbuf->b_fab_mrs = st.st_fab_mrs;
536 #endif
537 	}
538 	else
539 	{
540 	    curbuf->b_mtime = 0;
541 	    curbuf->b_mtime_read = 0;
542 	    curbuf->b_orig_size = 0;
543 	    curbuf->b_orig_mode = 0;
544 	}
545 
546 	/* Reset the "new file" flag.  It will be set again below when the
547 	 * file doesn't exist. */
548 	curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
549     }
550 
551 /*
552  * for UNIX: check readonly with perm and mch_access()
553  * for MSDOS and Amiga: check readonly by trying to open the file for writing
554  */
555     file_readonly = FALSE;
556     if (read_stdin)
557     {
558 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
559 	/* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */
560 	setmode(0, O_BINARY);
561 #endif
562     }
563     else if (!read_buffer)
564     {
565 #ifdef USE_MCH_ACCESS
566 	if (
567 # ifdef UNIX
568 	    !(perm & 0222) ||
569 # endif
570 				mch_access((char *)fname, W_OK))
571 	    file_readonly = TRUE;
572 	fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
573 #else
574 	if (!newfile
575 		|| readonlymode
576 		|| (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
577 	{
578 	    file_readonly = TRUE;
579 	    /* try to open ro */
580 	    fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
581 	}
582 #endif
583     }
584 
585     if (fd < 0)			    /* cannot open at all */
586     {
587 #ifndef UNIX
588 	int	isdir_f;
589 #endif
590 	msg_scroll = msg_save;
591 #ifndef UNIX
592 	/*
593 	 * On MSDOS and Amiga we can't open a directory, check here.
594 	 */
595 	isdir_f = (mch_isdir(fname));
596 	perm = mch_getperm(fname);  /* check if the file exists */
597 	if (isdir_f)
598 	{
599 	    filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
600 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
601 	}
602 	else
603 #endif
604 	    if (newfile)
605 	    {
606 		if (perm < 0
607 #ifdef ENOENT
608 			&& errno == ENOENT
609 #endif
610 		   )
611 		{
612 		    /*
613 		     * Set the 'new-file' flag, so that when the file has
614 		     * been created by someone else, a ":w" will complain.
615 		     */
616 		    curbuf->b_flags |= BF_NEW;
617 
618 		    /* Create a swap file now, so that other Vims are warned
619 		     * that we are editing this file.  Don't do this for a
620 		     * "nofile" or "nowrite" buffer type. */
621 #ifdef FEAT_QUICKFIX
622 		    if (!bt_dontwrite(curbuf))
623 #endif
624 		    {
625 			check_need_swap(newfile);
626 #ifdef FEAT_AUTOCMD
627 			/* SwapExists autocommand may mess things up */
628 			if (curbuf != old_curbuf
629 				|| (using_b_ffname
630 					&& (old_b_ffname != curbuf->b_ffname))
631 				|| (using_b_fname
632 					 && (old_b_fname != curbuf->b_fname)))
633 			{
634 			    EMSG(_(e_auchangedbuf));
635 			    return FAIL;
636 			}
637 #endif
638 		    }
639 		    if (dir_of_file_exists(fname))
640 			filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
641 		    else
642 			filemess(curbuf, sfname,
643 					   (char_u *)_("[New DIRECTORY]"), 0);
644 #ifdef FEAT_VIMINFO
645 		    /* Even though this is a new file, it might have been
646 		     * edited before and deleted.  Get the old marks. */
647 		    check_marks_read();
648 #endif
649 #ifdef FEAT_MBYTE
650 		    if (eap != NULL && eap->force_enc != 0)
651 		    {
652 			/* set forced 'fileencoding' */
653 			fenc = enc_canonize(eap->cmd + eap->force_enc);
654 			if (fenc != NULL)
655 			    set_string_option_direct((char_u *)"fenc", -1,
656 						 fenc, OPT_FREE|OPT_LOCAL, 0);
657 			vim_free(fenc);
658 		    }
659 #endif
660 #ifdef FEAT_AUTOCMD
661 		    apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
662 							  FALSE, curbuf, eap);
663 #endif
664 		    /* remember the current fileformat */
665 		    save_file_ff(curbuf);
666 
667 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
668 		    if (aborting())   /* autocmds may abort script processing */
669 			return FAIL;
670 #endif
671 		    return OK;	    /* a new file is not an error */
672 		}
673 		else
674 		{
675 		    filemess(curbuf, sfname, (char_u *)(
676 # ifdef EFBIG
677 			    (errno == EFBIG) ? _("[File too big]") :
678 # endif
679 # ifdef EOVERFLOW
680 			    (errno == EOVERFLOW) ? _("[File too big]") :
681 # endif
682 						_("[Permission Denied]")), 0);
683 		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
684 		}
685 	    }
686 
687 	return FAIL;
688     }
689 
690     /*
691      * Only set the 'ro' flag for readonly files the first time they are
692      * loaded.	Help files always get readonly mode
693      */
694     if ((check_readonly && file_readonly) || curbuf->b_help)
695 	curbuf->b_p_ro = TRUE;
696 
697     if (set_options)
698     {
699 	/* Don't change 'eol' if reading from buffer as it will already be
700 	 * correctly set when reading stdin. */
701 	if (!read_buffer)
702 	{
703 	    curbuf->b_p_eol = TRUE;
704 	    curbuf->b_start_eol = TRUE;
705 	}
706 #ifdef FEAT_MBYTE
707 	curbuf->b_p_bomb = FALSE;
708 	curbuf->b_start_bomb = FALSE;
709 #endif
710     }
711 
712     /* Create a swap file now, so that other Vims are warned that we are
713      * editing this file.
714      * Don't do this for a "nofile" or "nowrite" buffer type. */
715 #ifdef FEAT_QUICKFIX
716     if (!bt_dontwrite(curbuf))
717 #endif
718     {
719 	check_need_swap(newfile);
720 #ifdef FEAT_AUTOCMD
721 	if (!read_stdin && (curbuf != old_curbuf
722 		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
723 		|| (using_b_fname && (old_b_fname != curbuf->b_fname))))
724 	{
725 	    EMSG(_(e_auchangedbuf));
726 	    if (!read_buffer)
727 		close(fd);
728 	    return FAIL;
729 	}
730 #endif
731 #ifdef UNIX
732 	/* Set swap file protection bits after creating it. */
733 	if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
734 			  && curbuf->b_ml.ml_mfp->mf_fname != NULL)
735 	    (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode);
736 #endif
737     }
738 
739 #if defined(HAS_SWAP_EXISTS_ACTION)
740     /* If "Quit" selected at ATTENTION dialog, don't load the file */
741     if (swap_exists_action == SEA_QUIT)
742     {
743 	if (!read_buffer && !read_stdin)
744 	    close(fd);
745 	return FAIL;
746     }
747 #endif
748 
749     ++no_wait_return;	    /* don't wait for return yet */
750 
751     /*
752      * Set '[ mark to the line above where the lines go (line 1 if zero).
753      */
754     curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
755     curbuf->b_op_start.col = 0;
756 
757 #ifdef FEAT_AUTOCMD
758     if (!read_buffer)
759     {
760 	int	m = msg_scroll;
761 	int	n = msg_scrolled;
762 
763 	/*
764 	 * The file must be closed again, the autocommands may want to change
765 	 * the file before reading it.
766 	 */
767 	if (!read_stdin)
768 	    close(fd);		/* ignore errors */
769 
770 	/*
771 	 * The output from the autocommands should not overwrite anything and
772 	 * should not be overwritten: Set msg_scroll, restore its value if no
773 	 * output was done.
774 	 */
775 	msg_scroll = TRUE;
776 	if (filtering)
777 	    apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
778 							  FALSE, curbuf, eap);
779 	else if (read_stdin)
780 	    apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
781 							  FALSE, curbuf, eap);
782 	else if (newfile)
783 	    apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
784 							  FALSE, curbuf, eap);
785 	else
786 	    apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
787 							    FALSE, NULL, eap);
788 	if (msg_scrolled == n)
789 	    msg_scroll = m;
790 
791 #ifdef FEAT_EVAL
792 	if (aborting())	    /* autocmds may abort script processing */
793 	{
794 	    --no_wait_return;
795 	    msg_scroll = msg_save;
796 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
797 	    return FAIL;
798 	}
799 #endif
800 	/*
801 	 * Don't allow the autocommands to change the current buffer.
802 	 * Try to re-open the file.
803 	 *
804 	 * Don't allow the autocommands to change the buffer name either
805 	 * (cd for example) if it invalidates fname or sfname.
806 	 */
807 	if (!read_stdin && (curbuf != old_curbuf
808 		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
809 		|| (using_b_fname && (old_b_fname != curbuf->b_fname))
810 		|| (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
811 	{
812 	    --no_wait_return;
813 	    msg_scroll = msg_save;
814 	    if (fd < 0)
815 		EMSG(_("E200: *ReadPre autocommands made the file unreadable"));
816 	    else
817 		EMSG(_("E201: *ReadPre autocommands must not change current buffer"));
818 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
819 	    return FAIL;
820 	}
821     }
822 #endif /* FEAT_AUTOCMD */
823 
824     /* Autocommands may add lines to the file, need to check if it is empty */
825     wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
826 
827     if (!recoverymode && !filtering && !(flags & READ_DUMMY))
828     {
829 	/*
830 	 * Show the user that we are busy reading the input.  Sometimes this
831 	 * may take a while.  When reading from stdin another program may
832 	 * still be running, don't move the cursor to the last line, unless
833 	 * always using the GUI.
834 	 */
835 	if (read_stdin)
836 	{
837 #ifndef ALWAYS_USE_GUI
838 	    mch_msg(_("Vim: Reading from stdin...\n"));
839 #endif
840 #ifdef FEAT_GUI
841 	    /* Also write a message in the GUI window, if there is one. */
842 	    if (gui.in_use && !gui.dying && !gui.starting)
843 	    {
844 		p = (char_u *)_("Reading from stdin...");
845 		gui_write(p, (int)STRLEN(p));
846 	    }
847 #endif
848 	}
849 	else if (!read_buffer)
850 	    filemess(curbuf, sfname, (char_u *)"", 0);
851     }
852 
853     msg_scroll = FALSE;			/* overwrite the file message */
854 
855     /*
856      * Set linecnt now, before the "retry" caused by a wrong guess for
857      * fileformat, and after the autocommands, which may change them.
858      */
859     linecnt = curbuf->b_ml.ml_line_count;
860 
861 #ifdef FEAT_MBYTE
862     /* "++bad=" argument. */
863     if (eap != NULL && eap->bad_char != 0)
864     {
865 	bad_char_behavior = eap->bad_char;
866 	if (set_options)
867 	    curbuf->b_bad_char = eap->bad_char;
868     }
869     else
870 	curbuf->b_bad_char = 0;
871 
872     /*
873      * Decide which 'encoding' to use or use first.
874      */
875     if (eap != NULL && eap->force_enc != 0)
876     {
877 	fenc = enc_canonize(eap->cmd + eap->force_enc);
878 	fenc_alloced = TRUE;
879 	keep_dest_enc = TRUE;
880     }
881     else if (curbuf->b_p_bin)
882     {
883 	fenc = (char_u *)"";		/* binary: don't convert */
884 	fenc_alloced = FALSE;
885     }
886     else if (curbuf->b_help)
887     {
888 	char_u	    firstline[80];
889 	int	    fc;
890 
891 	/* Help files are either utf-8 or latin1.  Try utf-8 first, if this
892 	 * fails it must be latin1.
893 	 * Always do this when 'encoding' is "utf-8".  Otherwise only do
894 	 * this when needed to avoid [converted] remarks all the time.
895 	 * It is needed when the first line contains non-ASCII characters.
896 	 * That is only in *.??x files. */
897 	fenc = (char_u *)"latin1";
898 	c = enc_utf8;
899 	if (!c && !read_stdin)
900 	{
901 	    fc = fname[STRLEN(fname) - 1];
902 	    if (TOLOWER_ASC(fc) == 'x')
903 	    {
904 		/* Read the first line (and a bit more).  Immediately rewind to
905 		 * the start of the file.  If the read() fails "len" is -1. */
906 		len = read_eintr(fd, firstline, 80);
907 		lseek(fd, (off_t)0L, SEEK_SET);
908 		for (p = firstline; p < firstline + len; ++p)
909 		    if (*p >= 0x80)
910 		    {
911 			c = TRUE;
912 			break;
913 		    }
914 	    }
915 	}
916 
917 	if (c)
918 	{
919 	    fenc_next = fenc;
920 	    fenc = (char_u *)"utf-8";
921 
922 	    /* When the file is utf-8 but a character doesn't fit in
923 	     * 'encoding' don't retry.  In help text editing utf-8 bytes
924 	     * doesn't make sense. */
925 	    if (!enc_utf8)
926 		keep_dest_enc = TRUE;
927 	}
928 	fenc_alloced = FALSE;
929     }
930     else if (*p_fencs == NUL)
931     {
932 	fenc = curbuf->b_p_fenc;	/* use format from buffer */
933 	fenc_alloced = FALSE;
934     }
935     else
936     {
937 	fenc_next = p_fencs;		/* try items in 'fileencodings' */
938 	fenc = next_fenc(&fenc_next);
939 	fenc_alloced = TRUE;
940     }
941 #endif
942 
943     /*
944      * Jump back here to retry reading the file in different ways.
945      * Reasons to retry:
946      * - encoding conversion failed: try another one from "fenc_next"
947      * - BOM detected and fenc was set, need to setup conversion
948      * - "fileformat" check failed: try another
949      *
950      * Variables set for special retry actions:
951      * "file_rewind"	Rewind the file to start reading it again.
952      * "advance_fenc"	Advance "fenc" using "fenc_next".
953      * "skip_read"	Re-use already read bytes (BOM detected).
954      * "did_iconv"	iconv() conversion failed, try 'charconvert'.
955      * "keep_fileformat" Don't reset "fileformat".
956      *
957      * Other status indicators:
958      * "tmpname"	When != NULL did conversion with 'charconvert'.
959      *			Output file has to be deleted afterwards.
960      * "iconv_fd"	When != -1 did conversion with iconv().
961      */
962 retry:
963 
964     if (file_rewind)
965     {
966 	if (read_buffer)
967 	{
968 	    read_buf_lnum = 1;
969 	    read_buf_col = 0;
970 	}
971 	else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0)
972 	{
973 	    /* Can't rewind the file, give up. */
974 	    error = TRUE;
975 	    goto failed;
976 	}
977 	/* Delete the previously read lines. */
978 	while (lnum > from)
979 	    ml_delete(lnum--, FALSE);
980 	file_rewind = FALSE;
981 #ifdef FEAT_MBYTE
982 	if (set_options)
983 	{
984 	    curbuf->b_p_bomb = FALSE;
985 	    curbuf->b_start_bomb = FALSE;
986 	}
987 	conv_error = 0;
988 #endif
989     }
990 
991 #ifdef FEAT_CRYPT
992     if (cryptkey != NULL)
993 	/* Need to reset the state, but keep the key, don't want to ask for it
994 	 * again. */
995 	crypt_pop_state();
996 #endif
997 
998     /*
999      * When retrying with another "fenc" and the first time "fileformat"
1000      * will be reset.
1001      */
1002     if (keep_fileformat)
1003 	keep_fileformat = FALSE;
1004     else
1005     {
1006 	if (eap != NULL && eap->force_ff != 0)
1007 	{
1008 	    fileformat = get_fileformat_force(curbuf, eap);
1009 	    try_unix = try_dos = try_mac = FALSE;
1010 	}
1011 	else if (curbuf->b_p_bin)
1012 	    fileformat = EOL_UNIX;		/* binary: use Unix format */
1013 	else if (*p_ffs == NUL)
1014 	    fileformat = get_fileformat(curbuf);/* use format from buffer */
1015 	else
1016 	    fileformat = EOL_UNKNOWN;		/* detect from file */
1017     }
1018 
1019 #ifdef FEAT_MBYTE
1020 # ifdef USE_ICONV
1021     if (iconv_fd != (iconv_t)-1)
1022     {
1023 	/* aborted conversion with iconv(), close the descriptor */
1024 	iconv_close(iconv_fd);
1025 	iconv_fd = (iconv_t)-1;
1026     }
1027 # endif
1028 
1029     if (advance_fenc)
1030     {
1031 	/*
1032 	 * Try the next entry in 'fileencodings'.
1033 	 */
1034 	advance_fenc = FALSE;
1035 
1036 	if (eap != NULL && eap->force_enc != 0)
1037 	{
1038 	    /* Conversion given with "++cc=" wasn't possible, read
1039 	     * without conversion. */
1040 	    notconverted = TRUE;
1041 	    conv_error = 0;
1042 	    if (fenc_alloced)
1043 		vim_free(fenc);
1044 	    fenc = (char_u *)"";
1045 	    fenc_alloced = FALSE;
1046 	}
1047 	else
1048 	{
1049 	    if (fenc_alloced)
1050 		vim_free(fenc);
1051 	    if (fenc_next != NULL)
1052 	    {
1053 		fenc = next_fenc(&fenc_next);
1054 		fenc_alloced = (fenc_next != NULL);
1055 	    }
1056 	    else
1057 	    {
1058 		fenc = (char_u *)"";
1059 		fenc_alloced = FALSE;
1060 	    }
1061 	}
1062 	if (tmpname != NULL)
1063 	{
1064 	    mch_remove(tmpname);		/* delete converted file */
1065 	    vim_free(tmpname);
1066 	    tmpname = NULL;
1067 	}
1068     }
1069 
1070     /*
1071      * Conversion may be required when the encoding of the file is different
1072      * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
1073      */
1074     fio_flags = 0;
1075     converted = need_conversion(fenc);
1076     if (converted)
1077     {
1078 
1079 	/* "ucs-bom" means we need to check the first bytes of the file
1080 	 * for a BOM. */
1081 	if (STRCMP(fenc, ENC_UCSBOM) == 0)
1082 	    fio_flags = FIO_UCSBOM;
1083 
1084 	/*
1085 	 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1086 	 * done.  This is handled below after read().  Prepare the
1087 	 * fio_flags to avoid having to parse the string each time.
1088 	 * Also check for Unicode to Latin1 conversion, because iconv()
1089 	 * appears not to handle this correctly.  This works just like
1090 	 * conversion to UTF-8 except how the resulting character is put in
1091 	 * the buffer.
1092 	 */
1093 	else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1094 	    fio_flags = get_fio_flags(fenc);
1095 
1096 # ifdef WIN3264
1097 	/*
1098 	 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1099 	 * is handled with MultiByteToWideChar().
1100 	 */
1101 	if (fio_flags == 0)
1102 	    fio_flags = get_win_fio_flags(fenc);
1103 # endif
1104 
1105 # ifdef MACOS_X
1106 	/* Conversion from Apple MacRoman to latin1 or UTF-8 */
1107 	if (fio_flags == 0)
1108 	    fio_flags = get_mac_fio_flags(fenc);
1109 # endif
1110 
1111 # ifdef USE_ICONV
1112 	/*
1113 	 * Try using iconv() if we can't convert internally.
1114 	 */
1115 	if (fio_flags == 0
1116 #  ifdef FEAT_EVAL
1117 		&& !did_iconv
1118 #  endif
1119 		)
1120 	    iconv_fd = (iconv_t)my_iconv_open(
1121 				  enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
1122 # endif
1123 
1124 # ifdef FEAT_EVAL
1125 	/*
1126 	 * Use the 'charconvert' expression when conversion is required
1127 	 * and we can't do it internally or with iconv().
1128 	 */
1129 	if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
1130 #  ifdef USE_ICONV
1131 						    && iconv_fd == (iconv_t)-1
1132 #  endif
1133 		)
1134 	{
1135 #  ifdef USE_ICONV
1136 	    did_iconv = FALSE;
1137 #  endif
1138 	    /* Skip conversion when it's already done (retry for wrong
1139 	     * "fileformat"). */
1140 	    if (tmpname == NULL)
1141 	    {
1142 		tmpname = readfile_charconvert(fname, fenc, &fd);
1143 		if (tmpname == NULL)
1144 		{
1145 		    /* Conversion failed.  Try another one. */
1146 		    advance_fenc = TRUE;
1147 		    if (fd < 0)
1148 		    {
1149 			/* Re-opening the original file failed! */
1150 			EMSG(_("E202: Conversion made file unreadable!"));
1151 			error = TRUE;
1152 			goto failed;
1153 		    }
1154 		    goto retry;
1155 		}
1156 	    }
1157 	}
1158 	else
1159 # endif
1160 	{
1161 	    if (fio_flags == 0
1162 # ifdef USE_ICONV
1163 		    && iconv_fd == (iconv_t)-1
1164 # endif
1165 	       )
1166 	    {
1167 		/* Conversion wanted but we can't.
1168 		 * Try the next conversion in 'fileencodings' */
1169 		advance_fenc = TRUE;
1170 		goto retry;
1171 	    }
1172 	}
1173     }
1174 
1175     /* Set "can_retry" when it's possible to rewind the file and try with
1176      * another "fenc" value.  It's FALSE when no other "fenc" to try, reading
1177      * stdin or fixed at a specific encoding. */
1178     can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc);
1179 #endif
1180 
1181     if (!skip_read)
1182     {
1183 	linerest = 0;
1184 	filesize = 0;
1185 	skip_count = lines_to_skip;
1186 	read_count = lines_to_read;
1187 #ifdef FEAT_MBYTE
1188 	conv_restlen = 0;
1189 #endif
1190 #ifdef FEAT_PERSISTENT_UNDO
1191 	read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1192 				  && curbuf->b_ffname != NULL
1193 				  && curbuf->b_p_udf
1194 				  && !filtering
1195 				  && !read_stdin
1196 				  && !read_buffer);
1197 	if (read_undo_file)
1198 	    sha256_start(&sha_ctx);
1199 #endif
1200     }
1201 
1202     while (!error && !got_int)
1203     {
1204 	/*
1205 	 * We allocate as much space for the file as we can get, plus
1206 	 * space for the old line plus room for one terminating NUL.
1207 	 * The amount is limited by the fact that read() only can read
1208 	 * upto max_unsigned characters (and other things).
1209 	 */
1210 #if SIZEOF_INT <= 2
1211 	if (linerest >= 0x7ff0)
1212 	{
1213 	    ++split;
1214 	    *ptr = NL;		    /* split line by inserting a NL */
1215 	    size = 1;
1216 	}
1217 	else
1218 #endif
1219 	{
1220 	    if (!skip_read)
1221 	    {
1222 #if SIZEOF_INT > 2
1223 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
1224 		size = SSIZE_MAX;		    /* use max I/O size, 52K */
1225 # else
1226 		size = 0x10000L;		    /* use buffer >= 64K */
1227 # endif
1228 #else
1229 		size = 0x7ff0L - linerest;	    /* limit buffer to 32K */
1230 #endif
1231 
1232 		for ( ; size >= 10; size = (long)((long_u)size >> 1))
1233 		{
1234 		    if ((new_buffer = lalloc((long_u)(size + linerest + 1),
1235 							      FALSE)) != NULL)
1236 			break;
1237 		}
1238 		if (new_buffer == NULL)
1239 		{
1240 		    do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1241 		    error = TRUE;
1242 		    break;
1243 		}
1244 		if (linerest)	/* copy characters from the previous buffer */
1245 		    mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1246 		vim_free(buffer);
1247 		buffer = new_buffer;
1248 		ptr = buffer + linerest;
1249 		line_start = buffer;
1250 
1251 #ifdef FEAT_MBYTE
1252 		/* May need room to translate into.
1253 		 * For iconv() we don't really know the required space, use a
1254 		 * factor ICONV_MULT.
1255 		 * latin1 to utf-8: 1 byte becomes up to 2 bytes
1256 		 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1257 		 * become up to 4 bytes, size must be multiple of 2
1258 		 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1259 		 * multiple of 2
1260 		 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1261 		 * multiple of 4 */
1262 		real_size = (int)size;
1263 # ifdef USE_ICONV
1264 		if (iconv_fd != (iconv_t)-1)
1265 		    size = size / ICONV_MULT;
1266 		else
1267 # endif
1268 		    if (fio_flags & FIO_LATIN1)
1269 		    size = size / 2;
1270 		else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1271 		    size = (size * 2 / 3) & ~1;
1272 		else if (fio_flags & FIO_UCS4)
1273 		    size = (size * 2 / 3) & ~3;
1274 		else if (fio_flags == FIO_UCSBOM)
1275 		    size = size / ICONV_MULT;	/* worst case */
1276 # ifdef WIN3264
1277 		else if (fio_flags & FIO_CODEPAGE)
1278 		    size = size / ICONV_MULT;	/* also worst case */
1279 # endif
1280 # ifdef MACOS_X
1281 		else if (fio_flags & FIO_MACROMAN)
1282 		    size = size / ICONV_MULT;	/* also worst case */
1283 # endif
1284 #endif
1285 
1286 #ifdef FEAT_MBYTE
1287 		if (conv_restlen > 0)
1288 		{
1289 		    /* Insert unconverted bytes from previous line. */
1290 		    mch_memmove(ptr, conv_rest, conv_restlen);
1291 		    ptr += conv_restlen;
1292 		    size -= conv_restlen;
1293 		}
1294 #endif
1295 
1296 		if (read_buffer)
1297 		{
1298 		    /*
1299 		     * Read bytes from curbuf.  Used for converting text read
1300 		     * from stdin.
1301 		     */
1302 		    if (read_buf_lnum > from)
1303 			size = 0;
1304 		    else
1305 		    {
1306 			int	n, ni;
1307 			long	tlen;
1308 
1309 			tlen = 0;
1310 			for (;;)
1311 			{
1312 			    p = ml_get(read_buf_lnum) + read_buf_col;
1313 			    n = (int)STRLEN(p);
1314 			    if ((int)tlen + n + 1 > size)
1315 			    {
1316 				/* Filled up to "size", append partial line.
1317 				 * Change NL to NUL to reverse the effect done
1318 				 * below. */
1319 				n = (int)(size - tlen);
1320 				for (ni = 0; ni < n; ++ni)
1321 				{
1322 				    if (p[ni] == NL)
1323 					ptr[tlen++] = NUL;
1324 				    else
1325 					ptr[tlen++] = p[ni];
1326 				}
1327 				read_buf_col += n;
1328 				break;
1329 			    }
1330 			    else
1331 			    {
1332 				/* Append whole line and new-line.  Change NL
1333 				 * to NUL to reverse the effect done below. */
1334 				for (ni = 0; ni < n; ++ni)
1335 				{
1336 				    if (p[ni] == NL)
1337 					ptr[tlen++] = NUL;
1338 				    else
1339 					ptr[tlen++] = p[ni];
1340 				}
1341 				ptr[tlen++] = NL;
1342 				read_buf_col = 0;
1343 				if (++read_buf_lnum > from)
1344 				{
1345 				    /* When the last line didn't have an
1346 				     * end-of-line don't add it now either. */
1347 				    if (!curbuf->b_p_eol)
1348 					--tlen;
1349 				    size = tlen;
1350 				    break;
1351 				}
1352 			    }
1353 			}
1354 		    }
1355 		}
1356 		else
1357 		{
1358 		    /*
1359 		     * Read bytes from the file.
1360 		     */
1361 		    size = read_eintr(fd, ptr, size);
1362 		}
1363 
1364 		if (size <= 0)
1365 		{
1366 		    if (size < 0)		    /* read error */
1367 			error = TRUE;
1368 #ifdef FEAT_MBYTE
1369 		    else if (conv_restlen > 0)
1370 		    {
1371 			/*
1372 			 * Reached end-of-file but some trailing bytes could
1373 			 * not be converted.  Truncated file?
1374 			 */
1375 
1376 			/* When we did a conversion report an error. */
1377 			if (fio_flags != 0
1378 # ifdef USE_ICONV
1379 				|| iconv_fd != (iconv_t)-1
1380 # endif
1381 			   )
1382 			{
1383 			    if (conv_error == 0)
1384 				conv_error = curbuf->b_ml.ml_line_count
1385 								- linecnt + 1;
1386 			}
1387 			/* Remember the first linenr with an illegal byte */
1388 			else if (illegal_byte == 0)
1389 			    illegal_byte = curbuf->b_ml.ml_line_count
1390 								- linecnt + 1;
1391 			if (bad_char_behavior == BAD_DROP)
1392 			{
1393 			    *(ptr - conv_restlen) = NUL;
1394 			    conv_restlen = 0;
1395 			}
1396 			else
1397 			{
1398 			    /* Replace the trailing bytes with the replacement
1399 			     * character if we were converting; if we weren't,
1400 			     * leave the UTF8 checking code to do it, as it
1401 			     * works slightly differently. */
1402 			    if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
1403 # ifdef USE_ICONV
1404 				    || iconv_fd != (iconv_t)-1
1405 # endif
1406 			       ))
1407 			    {
1408 				while (conv_restlen > 0)
1409 				{
1410 				    *(--ptr) = bad_char_behavior;
1411 				    --conv_restlen;
1412 				}
1413 			    }
1414 			    fio_flags = 0;	/* don't convert this */
1415 # ifdef USE_ICONV
1416 			    if (iconv_fd != (iconv_t)-1)
1417 			    {
1418 				iconv_close(iconv_fd);
1419 				iconv_fd = (iconv_t)-1;
1420 			    }
1421 # endif
1422 			}
1423 		    }
1424 #endif
1425 		}
1426 
1427 #ifdef FEAT_CRYPT
1428 		/*
1429 		 * At start of file: Check for magic number of encryption.
1430 		 */
1431 		if (filesize == 0)
1432 		    cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1433 					&filesize, newfile, sfname,
1434 					&did_ask_for_key);
1435 		/*
1436 		 * Decrypt the read bytes.
1437 		 */
1438 		if (cryptkey != NULL && size > 0)
1439 		    crypt_decode(ptr, size);
1440 #endif
1441 	    }
1442 	    skip_read = FALSE;
1443 
1444 #ifdef FEAT_MBYTE
1445 	    /*
1446 	     * At start of file (or after crypt magic number): Check for BOM.
1447 	     * Also check for a BOM for other Unicode encodings, but not after
1448 	     * converting with 'charconvert' or when a BOM has already been
1449 	     * found.
1450 	     */
1451 	    if ((filesize == 0
1452 # ifdef FEAT_CRYPT
1453 		   || (filesize == (CRYPT_MAGIC_LEN
1454 					   + crypt_salt_len[use_crypt_method]
1455 					   + crypt_seed_len[use_crypt_method])
1456 							  && cryptkey != NULL)
1457 # endif
1458 		       )
1459 		    && (fio_flags == FIO_UCSBOM
1460 			|| (!curbuf->b_p_bomb
1461 			    && tmpname == NULL
1462 			    && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1463 	    {
1464 		char_u	*ccname;
1465 		int	blen;
1466 
1467 		/* no BOM detection in a short file or in binary mode */
1468 		if (size < 2 || curbuf->b_p_bin)
1469 		    ccname = NULL;
1470 		else
1471 		    ccname = check_for_bom(ptr, size, &blen,
1472 		      fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1473 		if (ccname != NULL)
1474 		{
1475 		    /* Remove BOM from the text */
1476 		    filesize += blen;
1477 		    size -= blen;
1478 		    mch_memmove(ptr, ptr + blen, (size_t)size);
1479 		    if (set_options)
1480 		    {
1481 			curbuf->b_p_bomb = TRUE;
1482 			curbuf->b_start_bomb = TRUE;
1483 		    }
1484 		}
1485 
1486 		if (fio_flags == FIO_UCSBOM)
1487 		{
1488 		    if (ccname == NULL)
1489 		    {
1490 			/* No BOM detected: retry with next encoding. */
1491 			advance_fenc = TRUE;
1492 		    }
1493 		    else
1494 		    {
1495 			/* BOM detected: set "fenc" and jump back */
1496 			if (fenc_alloced)
1497 			    vim_free(fenc);
1498 			fenc = ccname;
1499 			fenc_alloced = FALSE;
1500 		    }
1501 		    /* retry reading without getting new bytes or rewinding */
1502 		    skip_read = TRUE;
1503 		    goto retry;
1504 		}
1505 	    }
1506 
1507 	    /* Include not converted bytes. */
1508 	    ptr -= conv_restlen;
1509 	    size += conv_restlen;
1510 	    conv_restlen = 0;
1511 #endif
1512 	    /*
1513 	     * Break here for a read error or end-of-file.
1514 	     */
1515 	    if (size <= 0)
1516 		break;
1517 
1518 #ifdef FEAT_MBYTE
1519 
1520 # ifdef USE_ICONV
1521 	    if (iconv_fd != (iconv_t)-1)
1522 	    {
1523 		/*
1524 		 * Attempt conversion of the read bytes to 'encoding' using
1525 		 * iconv().
1526 		 */
1527 		const char	*fromp;
1528 		char		*top;
1529 		size_t		from_size;
1530 		size_t		to_size;
1531 
1532 		fromp = (char *)ptr;
1533 		from_size = size;
1534 		ptr += size;
1535 		top = (char *)ptr;
1536 		to_size = real_size - size;
1537 
1538 		/*
1539 		 * If there is conversion error or not enough room try using
1540 		 * another conversion.  Except for when there is no
1541 		 * alternative (help files).
1542 		 */
1543 		while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1544 							       &top, &to_size)
1545 			    == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1546 						  || from_size > CONV_RESTLEN)
1547 		{
1548 		    if (can_retry)
1549 			goto rewind_retry;
1550 		    if (conv_error == 0)
1551 			conv_error = readfile_linenr(linecnt,
1552 							  ptr, (char_u *)top);
1553 
1554 		    /* Deal with a bad byte and continue with the next. */
1555 		    ++fromp;
1556 		    --from_size;
1557 		    if (bad_char_behavior == BAD_KEEP)
1558 		    {
1559 			*top++ = *(fromp - 1);
1560 			--to_size;
1561 		    }
1562 		    else if (bad_char_behavior != BAD_DROP)
1563 		    {
1564 			*top++ = bad_char_behavior;
1565 			--to_size;
1566 		    }
1567 		}
1568 
1569 		if (from_size > 0)
1570 		{
1571 		    /* Some remaining characters, keep them for the next
1572 		     * round. */
1573 		    mch_memmove(conv_rest, (char_u *)fromp, from_size);
1574 		    conv_restlen = (int)from_size;
1575 		}
1576 
1577 		/* move the linerest to before the converted characters */
1578 		line_start = ptr - linerest;
1579 		mch_memmove(line_start, buffer, (size_t)linerest);
1580 		size = (long)((char_u *)top - ptr);
1581 	    }
1582 # endif
1583 
1584 # ifdef WIN3264
1585 	    if (fio_flags & FIO_CODEPAGE)
1586 	    {
1587 		char_u	*src, *dst;
1588 		WCHAR	ucs2buf[3];
1589 		int	ucs2len;
1590 		int	codepage = FIO_GET_CP(fio_flags);
1591 		int	bytelen;
1592 		int	found_bad;
1593 		char	replstr[2];
1594 
1595 		/*
1596 		 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
1597 		 * a codepage, using standard MS-Windows functions.  This
1598 		 * requires two steps:
1599 		 * 1. convert from 'fileencoding' to ucs-2
1600 		 * 2. convert from ucs-2 to 'encoding'
1601 		 *
1602 		 * Because there may be illegal bytes AND an incomplete byte
1603 		 * sequence at the end, we may have to do the conversion one
1604 		 * character at a time to get it right.
1605 		 */
1606 
1607 		/* Replacement string for WideCharToMultiByte(). */
1608 		if (bad_char_behavior > 0)
1609 		    replstr[0] = bad_char_behavior;
1610 		else
1611 		    replstr[0] = '?';
1612 		replstr[1] = NUL;
1613 
1614 		/*
1615 		 * Move the bytes to the end of the buffer, so that we have
1616 		 * room to put the result at the start.
1617 		 */
1618 		src = ptr + real_size - size;
1619 		mch_memmove(src, ptr, size);
1620 
1621 		/*
1622 		 * Do the conversion.
1623 		 */
1624 		dst = ptr;
1625 		size = size;
1626 		while (size > 0)
1627 		{
1628 		    found_bad = FALSE;
1629 
1630 #  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
1631 		    if (codepage == CP_UTF8)
1632 		    {
1633 			/* Handle CP_UTF8 input ourselves to be able to handle
1634 			 * trailing bytes properly.
1635 			 * Get one UTF-8 character from src. */
1636 			bytelen = (int)utf_ptr2len_len(src, size);
1637 			if (bytelen > size)
1638 			{
1639 			    /* Only got some bytes of a character.  Normally
1640 			     * it's put in "conv_rest", but if it's too long
1641 			     * deal with it as if they were illegal bytes. */
1642 			    if (bytelen <= CONV_RESTLEN)
1643 				break;
1644 
1645 			    /* weird overlong byte sequence */
1646 			    bytelen = size;
1647 			    found_bad = TRUE;
1648 			}
1649 			else
1650 			{
1651 			    int	    u8c = utf_ptr2char(src);
1652 
1653 			    if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
1654 				found_bad = TRUE;
1655 			    ucs2buf[0] = u8c;
1656 			    ucs2len = 1;
1657 			}
1658 		    }
1659 		    else
1660 #  endif
1661 		    {
1662 			/* We don't know how long the byte sequence is, try
1663 			 * from one to three bytes. */
1664 			for (bytelen = 1; bytelen <= size && bytelen <= 3;
1665 								    ++bytelen)
1666 			{
1667 			    ucs2len = MultiByteToWideChar(codepage,
1668 							 MB_ERR_INVALID_CHARS,
1669 							 (LPCSTR)src, bytelen,
1670 								   ucs2buf, 3);
1671 			    if (ucs2len > 0)
1672 				break;
1673 			}
1674 			if (ucs2len == 0)
1675 			{
1676 			    /* If we have only one byte then it's probably an
1677 			     * incomplete byte sequence.  Otherwise discard
1678 			     * one byte as a bad character. */
1679 			    if (size == 1)
1680 				break;
1681 			    found_bad = TRUE;
1682 			    bytelen = 1;
1683 			}
1684 		    }
1685 
1686 		    if (!found_bad)
1687 		    {
1688 			int	i;
1689 
1690 			/* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */
1691 			if (enc_utf8)
1692 			{
1693 			    /* From UCS-2 to UTF-8.  Cannot fail. */
1694 			    for (i = 0; i < ucs2len; ++i)
1695 				dst += utf_char2bytes(ucs2buf[i], dst);
1696 			}
1697 			else
1698 			{
1699 			    BOOL	bad = FALSE;
1700 			    int		dstlen;
1701 
1702 			    /* From UCS-2 to "enc_codepage".  If the
1703 			     * conversion uses the default character "?",
1704 			     * the data doesn't fit in this encoding. */
1705 			    dstlen = WideCharToMultiByte(enc_codepage, 0,
1706 				    (LPCWSTR)ucs2buf, ucs2len,
1707 				    (LPSTR)dst, (int)(src - dst),
1708 				    replstr, &bad);
1709 			    if (bad)
1710 				found_bad = TRUE;
1711 			    else
1712 				dst += dstlen;
1713 			}
1714 		    }
1715 
1716 		    if (found_bad)
1717 		    {
1718 			/* Deal with bytes we can't convert. */
1719 			if (can_retry)
1720 			    goto rewind_retry;
1721 			if (conv_error == 0)
1722 			    conv_error = readfile_linenr(linecnt, ptr, dst);
1723 			if (bad_char_behavior != BAD_DROP)
1724 			{
1725 			    if (bad_char_behavior == BAD_KEEP)
1726 			    {
1727 				mch_memmove(dst, src, bytelen);
1728 				dst += bytelen;
1729 			    }
1730 			    else
1731 				*dst++ = bad_char_behavior;
1732 			}
1733 		    }
1734 
1735 		    src += bytelen;
1736 		    size -= bytelen;
1737 		}
1738 
1739 		if (size > 0)
1740 		{
1741 		    /* An incomplete byte sequence remaining. */
1742 		    mch_memmove(conv_rest, src, size);
1743 		    conv_restlen = size;
1744 		}
1745 
1746 		/* The new size is equal to how much "dst" was advanced. */
1747 		size = (long)(dst - ptr);
1748 	    }
1749 	    else
1750 # endif
1751 # ifdef MACOS_CONVERT
1752 	    if (fio_flags & FIO_MACROMAN)
1753 	    {
1754 		/*
1755 		 * Conversion from Apple MacRoman char encoding to UTF-8 or
1756 		 * latin1.  This is in os_mac_conv.c.
1757 		 */
1758 		if (macroman2enc(ptr, &size, real_size) == FAIL)
1759 		    goto rewind_retry;
1760 	    }
1761 	    else
1762 # endif
1763 	    if (fio_flags != 0)
1764 	    {
1765 		int	u8c;
1766 		char_u	*dest;
1767 		char_u	*tail = NULL;
1768 
1769 		/*
1770 		 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1771 		 * "enc_utf8" not set: Convert Unicode to Latin1.
1772 		 * Go from end to start through the buffer, because the number
1773 		 * of bytes may increase.
1774 		 * "dest" points to after where the UTF-8 bytes go, "p" points
1775 		 * to after the next character to convert.
1776 		 */
1777 		dest = ptr + real_size;
1778 		if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1779 		{
1780 		    p = ptr + size;
1781 		    if (fio_flags == FIO_UTF8)
1782 		    {
1783 			/* Check for a trailing incomplete UTF-8 sequence */
1784 			tail = ptr + size - 1;
1785 			while (tail > ptr && (*tail & 0xc0) == 0x80)
1786 			    --tail;
1787 			if (tail + utf_byte2len(*tail) <= ptr + size)
1788 			    tail = NULL;
1789 			else
1790 			    p = tail;
1791 		    }
1792 		}
1793 		else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1794 		{
1795 		    /* Check for a trailing byte */
1796 		    p = ptr + (size & ~1);
1797 		    if (size & 1)
1798 			tail = p;
1799 		    if ((fio_flags & FIO_UTF16) && p > ptr)
1800 		    {
1801 			/* Check for a trailing leading word */
1802 			if (fio_flags & FIO_ENDIAN_L)
1803 			{
1804 			    u8c = (*--p << 8);
1805 			    u8c += *--p;
1806 			}
1807 			else
1808 			{
1809 			    u8c = *--p;
1810 			    u8c += (*--p << 8);
1811 			}
1812 			if (u8c >= 0xd800 && u8c <= 0xdbff)
1813 			    tail = p;
1814 			else
1815 			    p += 2;
1816 		    }
1817 		}
1818 		else /*  FIO_UCS4 */
1819 		{
1820 		    /* Check for trailing 1, 2 or 3 bytes */
1821 		    p = ptr + (size & ~3);
1822 		    if (size & 3)
1823 			tail = p;
1824 		}
1825 
1826 		/* If there is a trailing incomplete sequence move it to
1827 		 * conv_rest[]. */
1828 		if (tail != NULL)
1829 		{
1830 		    conv_restlen = (int)((ptr + size) - tail);
1831 		    mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1832 		    size -= conv_restlen;
1833 		}
1834 
1835 
1836 		while (p > ptr)
1837 		{
1838 		    if (fio_flags & FIO_LATIN1)
1839 			u8c = *--p;
1840 		    else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1841 		    {
1842 			if (fio_flags & FIO_ENDIAN_L)
1843 			{
1844 			    u8c = (*--p << 8);
1845 			    u8c += *--p;
1846 			}
1847 			else
1848 			{
1849 			    u8c = *--p;
1850 			    u8c += (*--p << 8);
1851 			}
1852 			if ((fio_flags & FIO_UTF16)
1853 					    && u8c >= 0xdc00 && u8c <= 0xdfff)
1854 			{
1855 			    int u16c;
1856 
1857 			    if (p == ptr)
1858 			    {
1859 				/* Missing leading word. */
1860 				if (can_retry)
1861 				    goto rewind_retry;
1862 				if (conv_error == 0)
1863 				    conv_error = readfile_linenr(linecnt,
1864 								      ptr, p);
1865 				if (bad_char_behavior == BAD_DROP)
1866 				    continue;
1867 				if (bad_char_behavior != BAD_KEEP)
1868 				    u8c = bad_char_behavior;
1869 			    }
1870 
1871 			    /* found second word of double-word, get the first
1872 			     * word and compute the resulting character */
1873 			    if (fio_flags & FIO_ENDIAN_L)
1874 			    {
1875 				u16c = (*--p << 8);
1876 				u16c += *--p;
1877 			    }
1878 			    else
1879 			    {
1880 				u16c = *--p;
1881 				u16c += (*--p << 8);
1882 			    }
1883 			    u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1884 							      + (u8c & 0x3ff);
1885 
1886 			    /* Check if the word is indeed a leading word. */
1887 			    if (u16c < 0xd800 || u16c > 0xdbff)
1888 			    {
1889 				if (can_retry)
1890 				    goto rewind_retry;
1891 				if (conv_error == 0)
1892 				    conv_error = readfile_linenr(linecnt,
1893 								      ptr, p);
1894 				if (bad_char_behavior == BAD_DROP)
1895 				    continue;
1896 				if (bad_char_behavior != BAD_KEEP)
1897 				    u8c = bad_char_behavior;
1898 			    }
1899 			}
1900 		    }
1901 		    else if (fio_flags & FIO_UCS4)
1902 		    {
1903 			if (fio_flags & FIO_ENDIAN_L)
1904 			{
1905 			    u8c = (*--p << 24);
1906 			    u8c += (*--p << 16);
1907 			    u8c += (*--p << 8);
1908 			    u8c += *--p;
1909 			}
1910 			else	/* big endian */
1911 			{
1912 			    u8c = *--p;
1913 			    u8c += (*--p << 8);
1914 			    u8c += (*--p << 16);
1915 			    u8c += (*--p << 24);
1916 			}
1917 		    }
1918 		    else    /* UTF-8 */
1919 		    {
1920 			if (*--p < 0x80)
1921 			    u8c = *p;
1922 			else
1923 			{
1924 			    len = utf_head_off(ptr, p);
1925 			    p -= len;
1926 			    u8c = utf_ptr2char(p);
1927 			    if (len == 0)
1928 			    {
1929 				/* Not a valid UTF-8 character, retry with
1930 				 * another fenc when possible, otherwise just
1931 				 * report the error. */
1932 				if (can_retry)
1933 				    goto rewind_retry;
1934 				if (conv_error == 0)
1935 				    conv_error = readfile_linenr(linecnt,
1936 								      ptr, p);
1937 				if (bad_char_behavior == BAD_DROP)
1938 				    continue;
1939 				if (bad_char_behavior != BAD_KEEP)
1940 				    u8c = bad_char_behavior;
1941 			    }
1942 			}
1943 		    }
1944 		    if (enc_utf8)	/* produce UTF-8 */
1945 		    {
1946 			dest -= utf_char2len(u8c);
1947 			(void)utf_char2bytes(u8c, dest);
1948 		    }
1949 		    else		/* produce Latin1 */
1950 		    {
1951 			--dest;
1952 			if (u8c >= 0x100)
1953 			{
1954 			    /* character doesn't fit in latin1, retry with
1955 			     * another fenc when possible, otherwise just
1956 			     * report the error. */
1957 			    if (can_retry)
1958 				goto rewind_retry;
1959 			    if (conv_error == 0)
1960 				conv_error = readfile_linenr(linecnt, ptr, p);
1961 			    if (bad_char_behavior == BAD_DROP)
1962 				++dest;
1963 			    else if (bad_char_behavior == BAD_KEEP)
1964 				*dest = u8c;
1965 			    else if (eap != NULL && eap->bad_char != 0)
1966 				*dest = bad_char_behavior;
1967 			    else
1968 				*dest = 0xBF;
1969 			}
1970 			else
1971 			    *dest = u8c;
1972 		    }
1973 		}
1974 
1975 		/* move the linerest to before the converted characters */
1976 		line_start = dest - linerest;
1977 		mch_memmove(line_start, buffer, (size_t)linerest);
1978 		size = (long)((ptr + real_size) - dest);
1979 		ptr = dest;
1980 	    }
1981 	    else if (enc_utf8 && !curbuf->b_p_bin)
1982 	    {
1983 		int  incomplete_tail = FALSE;
1984 
1985 		/* Reading UTF-8: Check if the bytes are valid UTF-8. */
1986 		for (p = ptr; ; ++p)
1987 		{
1988 		    int	 todo = (int)((ptr + size) - p);
1989 		    int	 l;
1990 
1991 		    if (todo <= 0)
1992 			break;
1993 		    if (*p >= 0x80)
1994 		    {
1995 			/* A length of 1 means it's an illegal byte.  Accept
1996 			 * an incomplete character at the end though, the next
1997 			 * read() will get the next bytes, we'll check it
1998 			 * then. */
1999 			l = utf_ptr2len_len(p, todo);
2000 			if (l > todo && !incomplete_tail)
2001 			{
2002 			    /* Avoid retrying with a different encoding when
2003 			     * a truncated file is more likely, or attempting
2004 			     * to read the rest of an incomplete sequence when
2005 			     * we have already done so. */
2006 			    if (p > ptr || filesize > 0)
2007 				incomplete_tail = TRUE;
2008 			    /* Incomplete byte sequence, move it to conv_rest[]
2009 			     * and try to read the rest of it, unless we've
2010 			     * already done so. */
2011 			    if (p > ptr)
2012 			    {
2013 				conv_restlen = todo;
2014 				mch_memmove(conv_rest, p, conv_restlen);
2015 				size -= conv_restlen;
2016 				break;
2017 			    }
2018 			}
2019 			if (l == 1 || l > todo)
2020 			{
2021 			    /* Illegal byte.  If we can try another encoding
2022 			     * do that, unless at EOF where a truncated
2023 			     * file is more likely than a conversion error. */
2024 			    if (can_retry && !incomplete_tail)
2025 				break;
2026 # ifdef USE_ICONV
2027 			    /* When we did a conversion report an error. */
2028 			    if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2029 				conv_error = readfile_linenr(linecnt, ptr, p);
2030 # endif
2031 			    /* Remember the first linenr with an illegal byte */
2032 			    if (conv_error == 0 && illegal_byte == 0)
2033 				illegal_byte = readfile_linenr(linecnt, ptr, p);
2034 
2035 			    /* Drop, keep or replace the bad byte. */
2036 			    if (bad_char_behavior == BAD_DROP)
2037 			    {
2038 				mch_memmove(p, p + 1, todo - 1);
2039 				--p;
2040 				--size;
2041 			    }
2042 			    else if (bad_char_behavior != BAD_KEEP)
2043 				*p = bad_char_behavior;
2044 			}
2045 			else
2046 			    p += l - 1;
2047 		    }
2048 		}
2049 		if (p < ptr + size && !incomplete_tail)
2050 		{
2051 		    /* Detected a UTF-8 error. */
2052 rewind_retry:
2053 		    /* Retry reading with another conversion. */
2054 # if defined(FEAT_EVAL) && defined(USE_ICONV)
2055 		    if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
2056 			/* iconv() failed, try 'charconvert' */
2057 			did_iconv = TRUE;
2058 		    else
2059 # endif
2060 			/* use next item from 'fileencodings' */
2061 			advance_fenc = TRUE;
2062 		    file_rewind = TRUE;
2063 		    goto retry;
2064 		}
2065 	    }
2066 #endif
2067 
2068 	    /* count the number of characters (after conversion!) */
2069 	    filesize += size;
2070 
2071 	    /*
2072 	     * when reading the first part of a file: guess EOL type
2073 	     */
2074 	    if (fileformat == EOL_UNKNOWN)
2075 	    {
2076 		/* First try finding a NL, for Dos and Unix */
2077 		if (try_dos || try_unix)
2078 		{
2079 		    for (p = ptr; p < ptr + size; ++p)
2080 		    {
2081 			if (*p == NL)
2082 			{
2083 			    if (!try_unix
2084 				    || (try_dos && p > ptr && p[-1] == CAR))
2085 				fileformat = EOL_DOS;
2086 			    else
2087 				fileformat = EOL_UNIX;
2088 			    break;
2089 			}
2090 		    }
2091 
2092 		    /* Don't give in to EOL_UNIX if EOL_MAC is more likely */
2093 		    if (fileformat == EOL_UNIX && try_mac)
2094 		    {
2095 			/* Need to reset the counters when retrying fenc. */
2096 			try_mac = 1;
2097 			try_unix = 1;
2098 			for (; p >= ptr && *p != CAR; p--)
2099 			    ;
2100 			if (p >= ptr)
2101 			{
2102 			    for (p = ptr; p < ptr + size; ++p)
2103 			    {
2104 				if (*p == NL)
2105 				    try_unix++;
2106 				else if (*p == CAR)
2107 				    try_mac++;
2108 			    }
2109 			    if (try_mac > try_unix)
2110 				fileformat = EOL_MAC;
2111 			}
2112 		    }
2113 		}
2114 
2115 		/* No NL found: may use Mac format */
2116 		if (fileformat == EOL_UNKNOWN && try_mac)
2117 		    fileformat = EOL_MAC;
2118 
2119 		/* Still nothing found?  Use first format in 'ffs' */
2120 		if (fileformat == EOL_UNKNOWN)
2121 		    fileformat = default_fileformat();
2122 
2123 		/* if editing a new file: may set p_tx and p_ff */
2124 		if (set_options)
2125 		    set_fileformat(fileformat, OPT_LOCAL);
2126 	    }
2127 	}
2128 
2129 	/*
2130 	 * This loop is executed once for every character read.
2131 	 * Keep it fast!
2132 	 */
2133 	if (fileformat == EOL_MAC)
2134 	{
2135 	    --ptr;
2136 	    while (++ptr, --size >= 0)
2137 	    {
2138 		/* catch most common case first */
2139 		if ((c = *ptr) != NUL && c != CAR && c != NL)
2140 		    continue;
2141 		if (c == NUL)
2142 		    *ptr = NL;	/* NULs are replaced by newlines! */
2143 		else if (c == NL)
2144 		    *ptr = CAR;	/* NLs are replaced by CRs! */
2145 		else
2146 		{
2147 		    if (skip_count == 0)
2148 		    {
2149 			*ptr = NUL;	    /* end of line */
2150 			len = (colnr_T) (ptr - line_start + 1);
2151 			if (ml_append(lnum, line_start, len, newfile) == FAIL)
2152 			{
2153 			    error = TRUE;
2154 			    break;
2155 			}
2156 #ifdef FEAT_PERSISTENT_UNDO
2157 			if (read_undo_file)
2158 			    sha256_update(&sha_ctx, line_start, len);
2159 #endif
2160 			++lnum;
2161 			if (--read_count == 0)
2162 			{
2163 			    error = TRUE;	/* break loop */
2164 			    line_start = ptr;	/* nothing left to write */
2165 			    break;
2166 			}
2167 		    }
2168 		    else
2169 			--skip_count;
2170 		    line_start = ptr + 1;
2171 		}
2172 	    }
2173 	}
2174 	else
2175 	{
2176 	    --ptr;
2177 	    while (++ptr, --size >= 0)
2178 	    {
2179 		if ((c = *ptr) != NUL && c != NL)  /* catch most common case */
2180 		    continue;
2181 		if (c == NUL)
2182 		    *ptr = NL;	/* NULs are replaced by newlines! */
2183 		else
2184 		{
2185 		    if (skip_count == 0)
2186 		    {
2187 			*ptr = NUL;		/* end of line */
2188 			len = (colnr_T)(ptr - line_start + 1);
2189 			if (fileformat == EOL_DOS)
2190 			{
2191 			    if (ptr[-1] == CAR)	/* remove CR */
2192 			    {
2193 				ptr[-1] = NUL;
2194 				--len;
2195 			    }
2196 			    /*
2197 			     * Reading in Dos format, but no CR-LF found!
2198 			     * When 'fileformats' includes "unix", delete all
2199 			     * the lines read so far and start all over again.
2200 			     * Otherwise give an error message later.
2201 			     */
2202 			    else if (ff_error != EOL_DOS)
2203 			    {
2204 				if (   try_unix
2205 				    && !read_stdin
2206 				    && (read_buffer
2207 					|| lseek(fd, (off_t)0L, SEEK_SET) == 0))
2208 				{
2209 				    fileformat = EOL_UNIX;
2210 				    if (set_options)
2211 					set_fileformat(EOL_UNIX, OPT_LOCAL);
2212 				    file_rewind = TRUE;
2213 				    keep_fileformat = TRUE;
2214 				    goto retry;
2215 				}
2216 				ff_error = EOL_DOS;
2217 			    }
2218 			}
2219 			if (ml_append(lnum, line_start, len, newfile) == FAIL)
2220 			{
2221 			    error = TRUE;
2222 			    break;
2223 			}
2224 #ifdef FEAT_PERSISTENT_UNDO
2225 			if (read_undo_file)
2226 			    sha256_update(&sha_ctx, line_start, len);
2227 #endif
2228 			++lnum;
2229 			if (--read_count == 0)
2230 			{
2231 			    error = TRUE;	    /* break loop */
2232 			    line_start = ptr;	/* nothing left to write */
2233 			    break;
2234 			}
2235 		    }
2236 		    else
2237 			--skip_count;
2238 		    line_start = ptr + 1;
2239 		}
2240 	    }
2241 	}
2242 	linerest = (long)(ptr - line_start);
2243 	ui_breakcheck();
2244     }
2245 
2246 failed:
2247     /* not an error, max. number of lines reached */
2248     if (error && read_count == 0)
2249 	error = FALSE;
2250 
2251     /*
2252      * If we get EOF in the middle of a line, note the fact and
2253      * complete the line ourselves.
2254      * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2255      */
2256     if (!error
2257 	    && !got_int
2258 	    && linerest != 0
2259 	    && !(!curbuf->b_p_bin
2260 		&& fileformat == EOL_DOS
2261 		&& *line_start == Ctrl_Z
2262 		&& ptr == line_start + 1))
2263     {
2264 	/* remember for when writing */
2265 	if (set_options)
2266 	    curbuf->b_p_eol = FALSE;
2267 	*ptr = NUL;
2268 	len = (colnr_T)(ptr - line_start + 1);
2269 	if (ml_append(lnum, line_start, len, newfile) == FAIL)
2270 	    error = TRUE;
2271 	else
2272 	{
2273 #ifdef FEAT_PERSISTENT_UNDO
2274 	    if (read_undo_file)
2275 		sha256_update(&sha_ctx, line_start, len);
2276 #endif
2277 	    read_no_eol_lnum = ++lnum;
2278 	}
2279     }
2280 
2281     if (set_options)
2282 	save_file_ff(curbuf);		/* remember the current file format */
2283 
2284 #ifdef FEAT_CRYPT
2285     crypt_method_used = use_crypt_method;
2286     if (cryptkey != NULL)
2287     {
2288 	crypt_pop_state();
2289 	if (cryptkey != curbuf->b_p_key)
2290 	    free_crypt_key(cryptkey);
2291 	/* don't set cryptkey to NULL, it's used below as a flag that
2292 	 * encryption was used */
2293     }
2294 #endif
2295 
2296 #ifdef FEAT_MBYTE
2297     /* If editing a new file: set 'fenc' for the current buffer.
2298      * Also for ":read ++edit file". */
2299     if (set_options)
2300 	set_string_option_direct((char_u *)"fenc", -1, fenc,
2301 						       OPT_FREE|OPT_LOCAL, 0);
2302     if (fenc_alloced)
2303 	vim_free(fenc);
2304 # ifdef USE_ICONV
2305     if (iconv_fd != (iconv_t)-1)
2306     {
2307 	iconv_close(iconv_fd);
2308 	iconv_fd = (iconv_t)-1;
2309     }
2310 # endif
2311 #endif
2312 
2313     if (!read_buffer && !read_stdin)
2314 	close(fd);				/* errors are ignored */
2315 #ifdef HAVE_FD_CLOEXEC
2316     else
2317     {
2318 	int fdflags = fcntl(fd, F_GETFD);
2319 	if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
2320 	    fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
2321     }
2322 #endif
2323     vim_free(buffer);
2324 
2325 #ifdef HAVE_DUP
2326     if (read_stdin)
2327     {
2328 	/* Use stderr for stdin, makes shell commands work. */
2329 	close(0);
2330 	ignored = dup(2);
2331     }
2332 #endif
2333 
2334 #ifdef FEAT_MBYTE
2335     if (tmpname != NULL)
2336     {
2337 	mch_remove(tmpname);		/* delete converted file */
2338 	vim_free(tmpname);
2339     }
2340 #endif
2341     --no_wait_return;			/* may wait for return now */
2342 
2343     /*
2344      * In recovery mode everything but autocommands is skipped.
2345      */
2346     if (!recoverymode)
2347     {
2348 	/* need to delete the last line, which comes from the empty buffer */
2349 	if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2350 	{
2351 #ifdef FEAT_NETBEANS_INTG
2352 	    netbeansFireChanges = 0;
2353 #endif
2354 	    ml_delete(curbuf->b_ml.ml_line_count, FALSE);
2355 #ifdef FEAT_NETBEANS_INTG
2356 	    netbeansFireChanges = 1;
2357 #endif
2358 	    --linecnt;
2359 	}
2360 	linecnt = curbuf->b_ml.ml_line_count - linecnt;
2361 	if (filesize == 0)
2362 	    linecnt = 0;
2363 	if (newfile || read_buffer)
2364 	{
2365 	    redraw_curbuf_later(NOT_VALID);
2366 #ifdef FEAT_DIFF
2367 	    /* After reading the text into the buffer the diff info needs to
2368 	     * be updated. */
2369 	    diff_invalidate(curbuf);
2370 #endif
2371 #ifdef FEAT_FOLDING
2372 	    /* All folds in the window are invalid now.  Mark them for update
2373 	     * before triggering autocommands. */
2374 	    foldUpdateAll(curwin);
2375 #endif
2376 	}
2377 	else if (linecnt)		/* appended at least one line */
2378 	    appended_lines_mark(from, linecnt);
2379 
2380 #ifndef ALWAYS_USE_GUI
2381 	/*
2382 	 * If we were reading from the same terminal as where messages go,
2383 	 * the screen will have been messed up.
2384 	 * Switch on raw mode now and clear the screen.
2385 	 */
2386 	if (read_stdin)
2387 	{
2388 	    settmode(TMODE_RAW);	/* set to raw mode */
2389 	    starttermcap();
2390 	    screenclear();
2391 	}
2392 #endif
2393 
2394 	if (got_int)
2395 	{
2396 	    if (!(flags & READ_DUMMY))
2397 	    {
2398 		filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2399 		if (newfile)
2400 		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
2401 	    }
2402 	    msg_scroll = msg_save;
2403 #ifdef FEAT_VIMINFO
2404 	    check_marks_read();
2405 #endif
2406 	    return OK;		/* an interrupt isn't really an error */
2407 	}
2408 
2409 	if (!filtering && !(flags & READ_DUMMY))
2410 	{
2411 	    msg_add_fname(curbuf, sfname);   /* fname in IObuff with quotes */
2412 	    c = FALSE;
2413 
2414 #ifdef UNIX
2415 # ifdef S_ISFIFO
2416 	    if (S_ISFIFO(perm))			    /* fifo or socket */
2417 	    {
2418 		STRCAT(IObuff, _("[fifo/socket]"));
2419 		c = TRUE;
2420 	    }
2421 # else
2422 #  ifdef S_IFIFO
2423 	    if ((perm & S_IFMT) == S_IFIFO)	    /* fifo */
2424 	    {
2425 		STRCAT(IObuff, _("[fifo]"));
2426 		c = TRUE;
2427 	    }
2428 #  endif
2429 #  ifdef S_IFSOCK
2430 	    if ((perm & S_IFMT) == S_IFSOCK)	    /* or socket */
2431 	    {
2432 		STRCAT(IObuff, _("[socket]"));
2433 		c = TRUE;
2434 	    }
2435 #  endif
2436 # endif
2437 # ifdef OPEN_CHR_FILES
2438 	    if (S_ISCHR(perm))			    /* or character special */
2439 	    {
2440 		STRCAT(IObuff, _("[character special]"));
2441 		c = TRUE;
2442 	    }
2443 # endif
2444 #endif
2445 	    if (curbuf->b_p_ro)
2446 	    {
2447 		STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2448 		c = TRUE;
2449 	    }
2450 	    if (read_no_eol_lnum)
2451 	    {
2452 		msg_add_eol();
2453 		c = TRUE;
2454 	    }
2455 	    if (ff_error == EOL_DOS)
2456 	    {
2457 		STRCAT(IObuff, _("[CR missing]"));
2458 		c = TRUE;
2459 	    }
2460 	    if (split)
2461 	    {
2462 		STRCAT(IObuff, _("[long lines split]"));
2463 		c = TRUE;
2464 	    }
2465 #ifdef FEAT_MBYTE
2466 	    if (notconverted)
2467 	    {
2468 		STRCAT(IObuff, _("[NOT converted]"));
2469 		c = TRUE;
2470 	    }
2471 	    else if (converted)
2472 	    {
2473 		STRCAT(IObuff, _("[converted]"));
2474 		c = TRUE;
2475 	    }
2476 #endif
2477 #ifdef FEAT_CRYPT
2478 	    if (cryptkey != NULL)
2479 	    {
2480 		if (crypt_method_used == 1)
2481 		    STRCAT(IObuff, _("[blowfish]"));
2482 		else
2483 		    STRCAT(IObuff, _("[crypted]"));
2484 		c = TRUE;
2485 	    }
2486 #endif
2487 #ifdef FEAT_MBYTE
2488 	    if (conv_error != 0)
2489 	    {
2490 		sprintf((char *)IObuff + STRLEN(IObuff),
2491 		       _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
2492 		c = TRUE;
2493 	    }
2494 	    else if (illegal_byte > 0)
2495 	    {
2496 		sprintf((char *)IObuff + STRLEN(IObuff),
2497 			 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2498 		c = TRUE;
2499 	    }
2500 	    else
2501 #endif
2502 		if (error)
2503 	    {
2504 		STRCAT(IObuff, _("[READ ERRORS]"));
2505 		c = TRUE;
2506 	    }
2507 	    if (msg_add_fileformat(fileformat))
2508 		c = TRUE;
2509 #ifdef FEAT_CRYPT
2510 	    if (cryptkey != NULL)
2511 		msg_add_lines(c, (long)linecnt, filesize
2512 			- CRYPT_MAGIC_LEN
2513 			- crypt_salt_len[use_crypt_method]
2514 			- crypt_seed_len[use_crypt_method]);
2515 	    else
2516 #endif
2517 		msg_add_lines(c, (long)linecnt, filesize);
2518 
2519 	    vim_free(keep_msg);
2520 	    keep_msg = NULL;
2521 	    msg_scrolled_ign = TRUE;
2522 #ifdef ALWAYS_USE_GUI
2523 	    /* Don't show the message when reading stdin, it would end up in a
2524 	     * message box (which might be shown when exiting!) */
2525 	    if (read_stdin || read_buffer)
2526 		p = msg_may_trunc(FALSE, IObuff);
2527 	    else
2528 #endif
2529 		p = msg_trunc_attr(IObuff, FALSE, 0);
2530 	    if (read_stdin || read_buffer || restart_edit != 0
2531 		    || (msg_scrolled != 0 && !need_wait_return))
2532 		/* Need to repeat the message after redrawing when:
2533 		 * - When reading from stdin (the screen will be cleared next).
2534 		 * - When restart_edit is set (otherwise there will be a delay
2535 		 *   before redrawing).
2536 		 * - When the screen was scrolled but there is no wait-return
2537 		 *   prompt. */
2538 		set_keep_msg(p, 0);
2539 	    msg_scrolled_ign = FALSE;
2540 	}
2541 
2542 	/* with errors writing the file requires ":w!" */
2543 	if (newfile && (error
2544 #ifdef FEAT_MBYTE
2545 		    || conv_error != 0
2546 		    || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)
2547 #endif
2548 		    ))
2549 	    curbuf->b_p_ro = TRUE;
2550 
2551 	u_clearline();	    /* cannot use "U" command after adding lines */
2552 
2553 	/*
2554 	 * In Ex mode: cursor at last new line.
2555 	 * Otherwise: cursor at first new line.
2556 	 */
2557 	if (exmode_active)
2558 	    curwin->w_cursor.lnum = from + linecnt;
2559 	else
2560 	    curwin->w_cursor.lnum = from + 1;
2561 	check_cursor_lnum();
2562 	beginline(BL_WHITE | BL_FIX);	    /* on first non-blank */
2563 
2564 	/*
2565 	 * Set '[ and '] marks to the newly read lines.
2566 	 */
2567 	curbuf->b_op_start.lnum = from + 1;
2568 	curbuf->b_op_start.col = 0;
2569 	curbuf->b_op_end.lnum = from + linecnt;
2570 	curbuf->b_op_end.col = 0;
2571 
2572 #ifdef WIN32
2573 	/*
2574 	 * Work around a weird problem: When a file has two links (only
2575 	 * possible on NTFS) and we write through one link, then stat() it
2576 	 * through the other link, the timestamp information may be wrong.
2577 	 * It's correct again after reading the file, thus reset the timestamp
2578 	 * here.
2579 	 */
2580 	if (newfile && !read_stdin && !read_buffer
2581 					 && mch_stat((char *)fname, &st) >= 0)
2582 	{
2583 	    buf_store_time(curbuf, &st, fname);
2584 	    curbuf->b_mtime_read = curbuf->b_mtime;
2585 	}
2586 #endif
2587     }
2588     msg_scroll = msg_save;
2589 
2590 #ifdef FEAT_VIMINFO
2591     /*
2592      * Get the marks before executing autocommands, so they can be used there.
2593      */
2594     check_marks_read();
2595 #endif
2596 
2597     /*
2598      * Trick: We remember if the last line of the read didn't have
2599      * an eol even when 'binary' is off, for when writing it again with
2600      * 'binary' on.  This is required for
2601      * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
2602      */
2603     curbuf->b_no_eol_lnum = read_no_eol_lnum;
2604 
2605     /* When reloading a buffer put the cursor at the first line that is
2606      * different. */
2607     if (flags & READ_KEEP_UNDO)
2608 	u_find_first_changed();
2609 
2610 #ifdef FEAT_PERSISTENT_UNDO
2611     /*
2612      * When opening a new file locate undo info and read it.
2613      */
2614     if (read_undo_file)
2615     {
2616 	char_u	hash[UNDO_HASH_SIZE];
2617 
2618 	sha256_finish(&sha_ctx, hash);
2619 	u_read_undo(NULL, hash, fname);
2620     }
2621 #endif
2622 
2623 #ifdef FEAT_AUTOCMD
2624     if (!read_stdin && !read_buffer)
2625     {
2626 	int m = msg_scroll;
2627 	int n = msg_scrolled;
2628 
2629 	/* Save the fileformat now, otherwise the buffer will be considered
2630 	 * modified if the format/encoding was automatically detected. */
2631 	if (set_options)
2632 	    save_file_ff(curbuf);
2633 
2634 	/*
2635 	 * The output from the autocommands should not overwrite anything and
2636 	 * should not be overwritten: Set msg_scroll, restore its value if no
2637 	 * output was done.
2638 	 */
2639 	msg_scroll = TRUE;
2640 	if (filtering)
2641 	    apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2642 							  FALSE, curbuf, eap);
2643 	else if (newfile)
2644 	    apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2645 							  FALSE, curbuf, eap);
2646 	else
2647 	    apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2648 							    FALSE, NULL, eap);
2649 	if (msg_scrolled == n)
2650 	    msg_scroll = m;
2651 # ifdef FEAT_EVAL
2652 	if (aborting())	    /* autocmds may abort script processing */
2653 	    return FAIL;
2654 # endif
2655     }
2656 #endif
2657 
2658     if (recoverymode && error)
2659 	return FAIL;
2660     return OK;
2661 }
2662 
2663 #ifdef OPEN_CHR_FILES
2664 /*
2665  * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2666  * which is the name of files used for process substitution output by
2667  * some shells on some operating systems, e.g., bash on SunOS.
2668  * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2669  */
2670     static int
2671 is_dev_fd_file(fname)
2672     char_u	*fname;
2673 {
2674     return (STRNCMP(fname, "/dev/fd/", 8) == 0
2675 	    && VIM_ISDIGIT(fname[8])
2676 	    && *skipdigits(fname + 9) == NUL
2677 	    && (fname[9] != NUL
2678 		|| (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2679 }
2680 #endif
2681 
2682 #ifdef FEAT_MBYTE
2683 
2684 /*
2685  * From the current line count and characters read after that, estimate the
2686  * line number where we are now.
2687  * Used for error messages that include a line number.
2688  */
2689     static linenr_T
2690 readfile_linenr(linecnt, p, endp)
2691     linenr_T	linecnt;	/* line count before reading more bytes */
2692     char_u	*p;		/* start of more bytes read */
2693     char_u	*endp;		/* end of more bytes read */
2694 {
2695     char_u	*s;
2696     linenr_T	lnum;
2697 
2698     lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2699     for (s = p; s < endp; ++s)
2700 	if (*s == '\n')
2701 	    ++lnum;
2702     return lnum;
2703 }
2704 #endif
2705 
2706 /*
2707  * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2708  * equal to the buffer "buf".  Used for calling readfile().
2709  * Returns OK or FAIL.
2710  */
2711     int
2712 prep_exarg(eap, buf)
2713     exarg_T	*eap;
2714     buf_T	*buf;
2715 {
2716     eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff)
2717 #ifdef FEAT_MBYTE
2718 		+ STRLEN(buf->b_p_fenc)
2719 #endif
2720 						 + 15));
2721     if (eap->cmd == NULL)
2722 	return FAIL;
2723 
2724 #ifdef FEAT_MBYTE
2725     sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc);
2726     eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff);
2727     eap->bad_char = buf->b_bad_char;
2728 #else
2729     sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff);
2730 #endif
2731     eap->force_ff = 7;
2732 
2733     eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
2734     eap->read_edit = FALSE;
2735     eap->forceit = FALSE;
2736     return OK;
2737 }
2738 
2739 #ifdef FEAT_MBYTE
2740 /*
2741  * Find next fileencoding to use from 'fileencodings'.
2742  * "pp" points to fenc_next.  It's advanced to the next item.
2743  * When there are no more items, an empty string is returned and *pp is set to
2744  * NULL.
2745  * When *pp is not set to NULL, the result is in allocated memory.
2746  */
2747     static char_u *
2748 next_fenc(pp)
2749     char_u	**pp;
2750 {
2751     char_u	*p;
2752     char_u	*r;
2753 
2754     if (**pp == NUL)
2755     {
2756 	*pp = NULL;
2757 	return (char_u *)"";
2758     }
2759     p = vim_strchr(*pp, ',');
2760     if (p == NULL)
2761     {
2762 	r = enc_canonize(*pp);
2763 	*pp += STRLEN(*pp);
2764     }
2765     else
2766     {
2767 	r = vim_strnsave(*pp, (int)(p - *pp));
2768 	*pp = p + 1;
2769 	if (r != NULL)
2770 	{
2771 	    p = enc_canonize(r);
2772 	    vim_free(r);
2773 	    r = p;
2774 	}
2775     }
2776     if (r == NULL)	/* out of memory */
2777     {
2778 	r = (char_u *)"";
2779 	*pp = NULL;
2780     }
2781     return r;
2782 }
2783 
2784 # ifdef FEAT_EVAL
2785 /*
2786  * Convert a file with the 'charconvert' expression.
2787  * This closes the file which is to be read, converts it and opens the
2788  * resulting file for reading.
2789  * Returns name of the resulting converted file (the caller should delete it
2790  * after reading it).
2791  * Returns NULL if the conversion failed ("*fdp" is not set) .
2792  */
2793     static char_u *
2794 readfile_charconvert(fname, fenc, fdp)
2795     char_u	*fname;		/* name of input file */
2796     char_u	*fenc;		/* converted from */
2797     int		*fdp;		/* in/out: file descriptor of file */
2798 {
2799     char_u	*tmpname;
2800     char_u	*errmsg = NULL;
2801 
2802     tmpname = vim_tempname('r');
2803     if (tmpname == NULL)
2804 	errmsg = (char_u *)_("Can't find temp file for conversion");
2805     else
2806     {
2807 	close(*fdp);		/* close the input file, ignore errors */
2808 	*fdp = -1;
2809 	if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2810 						      fname, tmpname) == FAIL)
2811 	    errmsg = (char_u *)_("Conversion with 'charconvert' failed");
2812 	if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2813 						  O_RDONLY | O_EXTRA, 0)) < 0)
2814 	    errmsg = (char_u *)_("can't read output of 'charconvert'");
2815     }
2816 
2817     if (errmsg != NULL)
2818     {
2819 	/* Don't use emsg(), it breaks mappings, the retry with
2820 	 * another type of conversion might still work. */
2821 	MSG(errmsg);
2822 	if (tmpname != NULL)
2823 	{
2824 	    mch_remove(tmpname);	/* delete converted file */
2825 	    vim_free(tmpname);
2826 	    tmpname = NULL;
2827 	}
2828     }
2829 
2830     /* If the input file is closed, open it (caller should check for error). */
2831     if (*fdp < 0)
2832 	*fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2833 
2834     return tmpname;
2835 }
2836 # endif
2837 
2838 #endif
2839 
2840 #ifdef FEAT_VIMINFO
2841 /*
2842  * Read marks for the current buffer from the viminfo file, when we support
2843  * buffer marks and the buffer has a name.
2844  */
2845     static void
2846 check_marks_read()
2847 {
2848     if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
2849 						  && curbuf->b_ffname != NULL)
2850 	read_viminfo(NULL, VIF_WANT_MARKS);
2851 
2852     /* Always set b_marks_read; needed when 'viminfo' is changed to include
2853      * the ' parameter after opening a buffer. */
2854     curbuf->b_marks_read = TRUE;
2855 }
2856 #endif
2857 
2858 #if defined(FEAT_CRYPT) || defined(PROTO)
2859 /*
2860  * Get the crypt method used for a file from "ptr[len]", the magic text at the
2861  * start of the file.
2862  * Returns -1 when no encryption used.
2863  */
2864     static int
2865 crypt_method_from_magic(ptr, len)
2866     char  *ptr;
2867     int   len;
2868 {
2869     int i;
2870 
2871     for (i = 0; i < (int)(sizeof(crypt_magic) / sizeof(crypt_magic[0])); i++)
2872     {
2873 	if (len < (CRYPT_MAGIC_LEN + crypt_salt_len[i] + crypt_seed_len[i]))
2874 	    continue;
2875 	if (memcmp(ptr, crypt_magic[i], CRYPT_MAGIC_LEN) == 0)
2876 	    return i;
2877     }
2878 
2879     i = (int)STRLEN(crypt_magic_head);
2880     if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0)
2881 	EMSG(_("E821: File is encrypted with unknown method"));
2882 
2883     return -1;
2884 }
2885 
2886 /*
2887  * Check for magic number used for encryption.  Applies to the current buffer.
2888  * If found, the magic number is removed from ptr[*sizep] and *sizep and
2889  * *filesizep are updated.
2890  * Return the (new) encryption key, NULL for no encryption.
2891  */
2892     static char_u *
2893 check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
2894     char_u	*cryptkey;	/* previous encryption key or NULL */
2895     char_u	*ptr;		/* pointer to read bytes */
2896     long	*sizep;		/* length of read bytes */
2897     off_t	*filesizep;	/* nr of bytes used from file */
2898     int		newfile;	/* editing a new buffer */
2899     char_u	*fname;		/* file name to display */
2900     int		*did_ask;	/* flag: whether already asked for key */
2901 {
2902     int method = crypt_method_from_magic((char *)ptr, *sizep);
2903 
2904     if (method >= 0)
2905     {
2906 	set_crypt_method(curbuf, method);
2907 	if (method > 0)
2908 	    (void)blowfish_self_test();
2909 	if (cryptkey == NULL && !*did_ask)
2910 	{
2911 	    if (*curbuf->b_p_key)
2912 		cryptkey = curbuf->b_p_key;
2913 	    else
2914 	    {
2915 		/* When newfile is TRUE, store the typed key in the 'key'
2916 		 * option and don't free it.  bf needs hash of the key saved.
2917 		 * Don't ask for the key again when first time Enter was hit.
2918 		 * Happens when retrying to detect encoding. */
2919 		smsg((char_u *)_(need_key_msg), fname);
2920 		msg_scroll = TRUE;
2921 		cryptkey = get_crypt_key(newfile, FALSE);
2922 		*did_ask = TRUE;
2923 
2924 		/* check if empty key entered */
2925 		if (cryptkey != NULL && *cryptkey == NUL)
2926 		{
2927 		    if (cryptkey != curbuf->b_p_key)
2928 			vim_free(cryptkey);
2929 		    cryptkey = NULL;
2930 		}
2931 	    }
2932 	}
2933 
2934 	if (cryptkey != NULL)
2935 	{
2936 	    int seed_len = crypt_seed_len[method];
2937 	    int salt_len = crypt_salt_len[method];
2938 
2939 	    crypt_push_state();
2940 	    use_crypt_method = method;
2941 	    if (method == 0)
2942 		crypt_init_keys(cryptkey);
2943 	    else
2944 	    {
2945 		bf_key_init(cryptkey, ptr + CRYPT_MAGIC_LEN, salt_len);
2946 		bf_ofb_init(ptr + CRYPT_MAGIC_LEN + salt_len, seed_len);
2947 	    }
2948 
2949 	    /* Remove magic number from the text */
2950 	    *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len;
2951 	    *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
2952 	    mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
2953 							      (size_t)*sizep);
2954 	}
2955     }
2956     /* When starting to edit a new file which does not have encryption, clear
2957      * the 'key' option, except when starting up (called with -x argument) */
2958     else if (newfile && *curbuf->b_p_key != NUL && !starting)
2959 	set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2960 
2961     return cryptkey;
2962 }
2963 
2964 /*
2965  * Check for magic number used for encryption.  Applies to the current buffer.
2966  * If found and decryption is possible returns OK;
2967  */
2968     int
2969 prepare_crypt_read(fp)
2970     FILE	*fp;
2971 {
2972     int		method;
2973     char_u	buffer[CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
2974 						    + CRYPT_SEED_LEN_MAX + 2];
2975 
2976     if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
2977 	return FAIL;
2978     method = crypt_method_from_magic((char *)buffer,
2979 					CRYPT_MAGIC_LEN +
2980 					CRYPT_SEED_LEN_MAX +
2981 					CRYPT_SALT_LEN_MAX);
2982     if (method < 0 || method != get_crypt_method(curbuf))
2983 	return FAIL;
2984 
2985     crypt_push_state();
2986     if (method == 0)
2987 	crypt_init_keys(curbuf->b_p_key);
2988     else
2989     {
2990 	int salt_len = crypt_salt_len[method];
2991 	int seed_len = crypt_seed_len[method];
2992 
2993 	if (fread(buffer, salt_len + seed_len, 1, fp) != 1)
2994 	    return FAIL;
2995 	bf_key_init(curbuf->b_p_key, buffer, salt_len);
2996 	bf_ofb_init(buffer + salt_len, seed_len);
2997     }
2998     return OK;
2999 }
3000 
3001 /*
3002  * Prepare for writing encrypted bytes for buffer "buf".
3003  * Returns a pointer to an allocated header of length "*lenp".
3004  * When out of memory returns NULL.
3005  * Otherwise calls crypt_push_state(), call crypt_pop_state() later.
3006  */
3007     char_u *
3008 prepare_crypt_write(buf, lenp)
3009     buf_T *buf;
3010     int   *lenp;
3011 {
3012     char_u  *header;
3013     int	    seed_len = crypt_seed_len[get_crypt_method(buf)];
3014     int     salt_len = crypt_salt_len[get_crypt_method(buf)];
3015     char_u  *salt;
3016     char_u  *seed;
3017 
3018     header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
3019 						    + CRYPT_SEED_LEN_MAX + 2);
3020     if (header != NULL)
3021     {
3022 	crypt_push_state();
3023 	use_crypt_method = get_crypt_method(buf);  /* select zip or blowfish */
3024 	vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
3025 							     CRYPT_MAGIC_LEN);
3026 	if (use_crypt_method == 0)
3027 	    crypt_init_keys(buf->b_p_key);
3028 	else
3029 	{
3030 	    /* Using blowfish, add salt and seed. */
3031 	    salt = header + CRYPT_MAGIC_LEN;
3032 	    seed = salt + salt_len;
3033 	    sha2_seed(salt, salt_len, seed, seed_len);
3034 	    bf_key_init(buf->b_p_key, salt, salt_len);
3035 	    bf_ofb_init(seed, seed_len);
3036 	}
3037     }
3038     *lenp = CRYPT_MAGIC_LEN + salt_len + seed_len;
3039     return header;
3040 }
3041 
3042 #endif  /* FEAT_CRYPT */
3043 
3044 #ifdef UNIX
3045     static void
3046 set_file_time(fname, atime, mtime)
3047     char_u  *fname;
3048     time_t  atime;	    /* access time */
3049     time_t  mtime;	    /* modification time */
3050 {
3051 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
3052     struct utimbuf  buf;
3053 
3054     buf.actime	= atime;
3055     buf.modtime	= mtime;
3056     (void)utime((char *)fname, &buf);
3057 # else
3058 #  if defined(HAVE_UTIMES)
3059     struct timeval  tvp[2];
3060 
3061     tvp[0].tv_sec   = atime;
3062     tvp[0].tv_usec  = 0;
3063     tvp[1].tv_sec   = mtime;
3064     tvp[1].tv_usec  = 0;
3065 #   ifdef NeXT
3066     (void)utimes((char *)fname, tvp);
3067 #   else
3068     (void)utimes((char *)fname, (const struct timeval *)&tvp);
3069 #   endif
3070 #  endif
3071 # endif
3072 }
3073 #endif /* UNIX */
3074 
3075 #if defined(VMS) && !defined(MIN)
3076 /* Older DECC compiler for VAX doesn't define MIN() */
3077 # define MIN(a, b) ((a) < (b) ? (a) : (b))
3078 #endif
3079 
3080 /*
3081  * Return TRUE if a file appears to be read-only from the file permissions.
3082  */
3083     int
3084 check_file_readonly(fname, perm)
3085     char_u	*fname;		/* full path to file */
3086     int		perm;		/* known permissions on file */
3087 {
3088 #ifndef USE_MCH_ACCESS
3089     int	    fd = 0;
3090 #endif
3091 
3092     return (
3093 #ifdef USE_MCH_ACCESS
3094 # ifdef UNIX
3095 	(perm & 0222) == 0 ||
3096 # endif
3097 	mch_access((char *)fname, W_OK)
3098 #else
3099 	(fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3100 					? TRUE : (close(fd), FALSE)
3101 #endif
3102 	);
3103 }
3104 
3105 
3106 /*
3107  * buf_write() - write to file "fname" lines "start" through "end"
3108  *
3109  * We do our own buffering here because fwrite() is so slow.
3110  *
3111  * If "forceit" is true, we don't care for errors when attempting backups.
3112  * In case of an error everything possible is done to restore the original
3113  * file.  But when "forceit" is TRUE, we risk losing it.
3114  *
3115  * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and
3116  * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
3117  *
3118  * This function must NOT use NameBuff (because it's called by autowrite()).
3119  *
3120  * return FAIL for failure, OK otherwise
3121  */
3122     int
3123 buf_write(buf, fname, sfname, start, end, eap, append, forceit,
3124 						      reset_changed, filtering)
3125     buf_T	    *buf;
3126     char_u	    *fname;
3127     char_u	    *sfname;
3128     linenr_T	    start, end;
3129     exarg_T	    *eap;		/* for forced 'ff' and 'fenc', can be
3130 					   NULL! */
3131     int		    append;		/* append to the file */
3132     int		    forceit;
3133     int		    reset_changed;
3134     int		    filtering;
3135 {
3136     int		    fd;
3137     char_u	    *backup = NULL;
3138     int		    backup_copy = FALSE; /* copy the original file? */
3139     int		    dobackup;
3140     char_u	    *ffname;
3141     char_u	    *wfname = NULL;	/* name of file to write to */
3142     char_u	    *s;
3143     char_u	    *ptr;
3144     char_u	    c;
3145     int		    len;
3146     linenr_T	    lnum;
3147     long	    nchars;
3148     char_u	    *errmsg = NULL;
3149     int		    errmsg_allocated = FALSE;
3150     char_u	    *errnum = NULL;
3151     char_u	    *buffer;
3152     char_u	    smallbuf[SMBUFSIZE];
3153     char_u	    *backup_ext;
3154     int		    bufsize;
3155     long	    perm;		    /* file permissions */
3156     int		    retval = OK;
3157     int		    newfile = FALSE;	    /* TRUE if file doesn't exist yet */
3158     int		    msg_save = msg_scroll;
3159     int		    overwriting;	    /* TRUE if writing over original */
3160     int		    no_eol = FALSE;	    /* no end-of-line written */
3161     int		    device = FALSE;	    /* writing to a device */
3162     struct stat	    st_old;
3163     int		    prev_got_int = got_int;
3164     int		    file_readonly = FALSE;  /* overwritten file is read-only */
3165     static char	    *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')";
3166 #if defined(UNIX) || defined(__EMX__XX)	    /*XXX fix me sometime? */
3167     int		    made_writable = FALSE;  /* 'w' bit has been set */
3168 #endif
3169 					/* writing everything */
3170     int		    whole = (start == 1 && end == buf->b_ml.ml_line_count);
3171 #ifdef FEAT_AUTOCMD
3172     linenr_T	    old_line_count = buf->b_ml.ml_line_count;
3173 #endif
3174     int		    attr;
3175     int		    fileformat;
3176     int		    write_bin;
3177     struct bw_info  write_info;		/* info for buf_write_bytes() */
3178 #ifdef FEAT_MBYTE
3179     int		    converted = FALSE;
3180     int		    notconverted = FALSE;
3181     char_u	    *fenc;		/* effective 'fileencoding' */
3182     char_u	    *fenc_tofree = NULL; /* allocated "fenc" */
3183 #endif
3184 #ifdef HAS_BW_FLAGS
3185     int		    wb_flags = 0;
3186 #endif
3187 #ifdef HAVE_ACL
3188     vim_acl_T	    acl = NULL;		/* ACL copied from original file to
3189 					   backup or new file */
3190 #endif
3191 #ifdef FEAT_PERSISTENT_UNDO
3192     int		    write_undo_file = FALSE;
3193     context_sha256_T sha_ctx;
3194 #endif
3195 #ifdef FEAT_CRYPT
3196     int		    crypt_method_used;
3197 #endif
3198 
3199     if (fname == NULL || *fname == NUL)	/* safety check */
3200 	return FAIL;
3201     if (buf->b_ml.ml_mfp == NULL)
3202     {
3203 	/* This can happen during startup when there is a stray "w" in the
3204 	 * vimrc file. */
3205 	EMSG(_(e_emptybuf));
3206 	return FAIL;
3207     }
3208 
3209     /*
3210      * Disallow writing from .exrc and .vimrc in current directory for
3211      * security reasons.
3212      */
3213     if (check_secure())
3214 	return FAIL;
3215 
3216     /* Avoid a crash for a long name. */
3217     if (STRLEN(fname) >= MAXPATHL)
3218     {
3219 	EMSG(_(e_longname));
3220 	return FAIL;
3221     }
3222 
3223 #ifdef FEAT_MBYTE
3224     /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */
3225     write_info.bw_conv_buf = NULL;
3226     write_info.bw_conv_error = FALSE;
3227     write_info.bw_conv_error_lnum = 0;
3228     write_info.bw_restlen = 0;
3229 # ifdef USE_ICONV
3230     write_info.bw_iconv_fd = (iconv_t)-1;
3231 # endif
3232 #endif
3233 
3234     /* After writing a file changedtick changes but we don't want to display
3235      * the line. */
3236     ex_no_reprint = TRUE;
3237 
3238     /*
3239      * If there is no file name yet, use the one for the written file.
3240      * BF_NOTEDITED is set to reflect this (in case the write fails).
3241      * Don't do this when the write is for a filter command.
3242      * Don't do this when appending.
3243      * Only do this when 'cpoptions' contains the 'F' flag.
3244      */
3245     if (buf->b_ffname == NULL
3246 	    && reset_changed
3247 	    && whole
3248 	    && buf == curbuf
3249 #ifdef FEAT_QUICKFIX
3250 	    && !bt_nofile(buf)
3251 #endif
3252 	    && !filtering
3253 	    && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
3254 	    && vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
3255     {
3256 	if (set_rw_fname(fname, sfname) == FAIL)
3257 	    return FAIL;
3258 	buf = curbuf;	    /* just in case autocmds made "buf" invalid */
3259     }
3260 
3261     if (sfname == NULL)
3262 	sfname = fname;
3263     /*
3264      * For Unix: Use the short file name whenever possible.
3265      * Avoids problems with networks and when directory names are changed.
3266      * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
3267      * another directory, which we don't detect
3268      */
3269     ffname = fname;			    /* remember full fname */
3270 #ifdef UNIX
3271     fname = sfname;
3272 #endif
3273 
3274     if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
3275 	overwriting = TRUE;
3276     else
3277 	overwriting = FALSE;
3278 
3279     if (exiting)
3280 	settmode(TMODE_COOK);	    /* when exiting allow typahead now */
3281 
3282     ++no_wait_return;		    /* don't wait for return yet */
3283 
3284     /*
3285      * Set '[ and '] marks to the lines to be written.
3286      */
3287     buf->b_op_start.lnum = start;
3288     buf->b_op_start.col = 0;
3289     buf->b_op_end.lnum = end;
3290     buf->b_op_end.col = 0;
3291 
3292 #ifdef FEAT_AUTOCMD
3293     {
3294 	aco_save_T	aco;
3295 	int		buf_ffname = FALSE;
3296 	int		buf_sfname = FALSE;
3297 	int		buf_fname_f = FALSE;
3298 	int		buf_fname_s = FALSE;
3299 	int		did_cmd = FALSE;
3300 	int		nofile_err = FALSE;
3301 	int		empty_memline = (buf->b_ml.ml_mfp == NULL);
3302 
3303 	/*
3304 	 * Apply PRE aucocommands.
3305 	 * Set curbuf to the buffer to be written.
3306 	 * Careful: The autocommands may call buf_write() recursively!
3307 	 */
3308 	if (ffname == buf->b_ffname)
3309 	    buf_ffname = TRUE;
3310 	if (sfname == buf->b_sfname)
3311 	    buf_sfname = TRUE;
3312 	if (fname == buf->b_ffname)
3313 	    buf_fname_f = TRUE;
3314 	if (fname == buf->b_sfname)
3315 	    buf_fname_s = TRUE;
3316 
3317 	/* set curwin/curbuf to buf and save a few things */
3318 	aucmd_prepbuf(&aco, buf);
3319 
3320 	if (append)
3321 	{
3322 	    if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
3323 					 sfname, sfname, FALSE, curbuf, eap)))
3324 	    {
3325 #ifdef FEAT_QUICKFIX
3326 		if (overwriting && bt_nofile(curbuf))
3327 		    nofile_err = TRUE;
3328 		else
3329 #endif
3330 		    apply_autocmds_exarg(EVENT_FILEAPPENDPRE,
3331 					  sfname, sfname, FALSE, curbuf, eap);
3332 	    }
3333 	}
3334 	else if (filtering)
3335 	{
3336 	    apply_autocmds_exarg(EVENT_FILTERWRITEPRE,
3337 					    NULL, sfname, FALSE, curbuf, eap);
3338 	}
3339 	else if (reset_changed && whole)
3340 	{
3341 	    int was_changed = curbufIsChanged();
3342 
3343 	    did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD,
3344 					  sfname, sfname, FALSE, curbuf, eap);
3345 	    if (did_cmd)
3346 	    {
3347 		if (was_changed && !curbufIsChanged())
3348 		{
3349 		    /* Written everything correctly and BufWriteCmd has reset
3350 		     * 'modified': Correct the undo information so that an
3351 		     * undo now sets 'modified'. */
3352 		    u_unchanged(curbuf);
3353 		    u_update_save_nr(curbuf);
3354 		}
3355 	    }
3356 	    else
3357 	    {
3358 #ifdef FEAT_QUICKFIX
3359 		if (overwriting && bt_nofile(curbuf))
3360 		    nofile_err = TRUE;
3361 		else
3362 #endif
3363 		    apply_autocmds_exarg(EVENT_BUFWRITEPRE,
3364 					  sfname, sfname, FALSE, curbuf, eap);
3365 	    }
3366 	}
3367 	else
3368 	{
3369 	    if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD,
3370 					 sfname, sfname, FALSE, curbuf, eap)))
3371 	    {
3372 #ifdef FEAT_QUICKFIX
3373 		if (overwriting && bt_nofile(curbuf))
3374 		    nofile_err = TRUE;
3375 		else
3376 #endif
3377 		    apply_autocmds_exarg(EVENT_FILEWRITEPRE,
3378 					  sfname, sfname, FALSE, curbuf, eap);
3379 	    }
3380 	}
3381 
3382 	/* restore curwin/curbuf and a few other things */
3383 	aucmd_restbuf(&aco);
3384 
3385 	/*
3386 	 * In three situations we return here and don't write the file:
3387 	 * 1. the autocommands deleted or unloaded the buffer.
3388 	 * 2. The autocommands abort script processing.
3389 	 * 3. If one of the "Cmd" autocommands was executed.
3390 	 */
3391 	if (!buf_valid(buf))
3392 	    buf = NULL;
3393 	if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
3394 				       || did_cmd || nofile_err
3395 #ifdef FEAT_EVAL
3396 				       || aborting()
3397 #endif
3398 				       )
3399 	{
3400 	    --no_wait_return;
3401 	    msg_scroll = msg_save;
3402 	    if (nofile_err)
3403 		EMSG(_("E676: No matching autocommands for acwrite buffer"));
3404 
3405 	    if (nofile_err
3406 #ifdef FEAT_EVAL
3407 		    || aborting()
3408 #endif
3409 		    )
3410 		/* An aborting error, interrupt or exception in the
3411 		 * autocommands. */
3412 		return FAIL;
3413 	    if (did_cmd)
3414 	    {
3415 		if (buf == NULL)
3416 		    /* The buffer was deleted.  We assume it was written
3417 		     * (can't retry anyway). */
3418 		    return OK;
3419 		if (overwriting)
3420 		{
3421 		    /* Assume the buffer was written, update the timestamp. */
3422 		    ml_timestamp(buf);
3423 		    if (append)
3424 			buf->b_flags &= ~BF_NEW;
3425 		    else
3426 			buf->b_flags &= ~BF_WRITE_MASK;
3427 		}
3428 		if (reset_changed && buf->b_changed && !append
3429 			&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
3430 		    /* Buffer still changed, the autocommands didn't work
3431 		     * properly. */
3432 		    return FAIL;
3433 		return OK;
3434 	    }
3435 #ifdef FEAT_EVAL
3436 	    if (!aborting())
3437 #endif
3438 		EMSG(_("E203: Autocommands deleted or unloaded buffer to be written"));
3439 	    return FAIL;
3440 	}
3441 
3442 	/*
3443 	 * The autocommands may have changed the number of lines in the file.
3444 	 * When writing the whole file, adjust the end.
3445 	 * When writing part of the file, assume that the autocommands only
3446 	 * changed the number of lines that are to be written (tricky!).
3447 	 */
3448 	if (buf->b_ml.ml_line_count != old_line_count)
3449 	{
3450 	    if (whole)						/* write all */
3451 		end = buf->b_ml.ml_line_count;
3452 	    else if (buf->b_ml.ml_line_count > old_line_count)	/* more lines */
3453 		end += buf->b_ml.ml_line_count - old_line_count;
3454 	    else						/* less lines */
3455 	    {
3456 		end -= old_line_count - buf->b_ml.ml_line_count;
3457 		if (end < start)
3458 		{
3459 		    --no_wait_return;
3460 		    msg_scroll = msg_save;
3461 		    EMSG(_("E204: Autocommand changed number of lines in unexpected way"));
3462 		    return FAIL;
3463 		}
3464 	    }
3465 	}
3466 
3467 	/*
3468 	 * The autocommands may have changed the name of the buffer, which may
3469 	 * be kept in fname, ffname and sfname.
3470 	 */
3471 	if (buf_ffname)
3472 	    ffname = buf->b_ffname;
3473 	if (buf_sfname)
3474 	    sfname = buf->b_sfname;
3475 	if (buf_fname_f)
3476 	    fname = buf->b_ffname;
3477 	if (buf_fname_s)
3478 	    fname = buf->b_sfname;
3479     }
3480 #endif
3481 
3482 #ifdef FEAT_NETBEANS_INTG
3483     if (netbeans_active() && isNetbeansBuffer(buf))
3484     {
3485 	if (whole)
3486 	{
3487 	    /*
3488 	     * b_changed can be 0 after an undo, but we still need to write
3489 	     * the buffer to NetBeans.
3490 	     */
3491 	    if (buf->b_changed || isNetbeansModified(buf))
3492 	    {
3493 		--no_wait_return;		/* may wait for return now */
3494 		msg_scroll = msg_save;
3495 		netbeans_save_buffer(buf);	/* no error checking... */
3496 		return retval;
3497 	    }
3498 	    else
3499 	    {
3500 		errnum = (char_u *)"E656: ";
3501 		errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers");
3502 		buffer = NULL;
3503 		goto fail;
3504 	    }
3505 	}
3506 	else
3507 	{
3508 	    errnum = (char_u *)"E657: ";
3509 	    errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers");
3510 	    buffer = NULL;
3511 	    goto fail;
3512 	}
3513     }
3514 #endif
3515 
3516     if (shortmess(SHM_OVER) && !exiting)
3517 	msg_scroll = FALSE;	    /* overwrite previous file message */
3518     else
3519 	msg_scroll = TRUE;	    /* don't overwrite previous file message */
3520     if (!filtering)
3521 	filemess(buf,
3522 #ifndef UNIX
3523 		sfname,
3524 #else
3525 		fname,
3526 #endif
3527 		    (char_u *)"", 0);	/* show that we are busy */
3528     msg_scroll = FALSE;		    /* always overwrite the file message now */
3529 
3530     buffer = alloc(BUFSIZE);
3531     if (buffer == NULL)		    /* can't allocate big buffer, use small
3532 				     * one (to be able to write when out of
3533 				     * memory) */
3534     {
3535 	buffer = smallbuf;
3536 	bufsize = SMBUFSIZE;
3537     }
3538     else
3539 	bufsize = BUFSIZE;
3540 
3541     /*
3542      * Get information about original file (if there is one).
3543      */
3544 #if defined(UNIX) && !defined(ARCHIE)
3545     st_old.st_dev = 0;
3546     st_old.st_ino = 0;
3547     perm = -1;
3548     if (mch_stat((char *)fname, &st_old) < 0)
3549 	newfile = TRUE;
3550     else
3551     {
3552 	perm = st_old.st_mode;
3553 	if (!S_ISREG(st_old.st_mode))		/* not a file */
3554 	{
3555 	    if (S_ISDIR(st_old.st_mode))
3556 	    {
3557 		errnum = (char_u *)"E502: ";
3558 		errmsg = (char_u *)_("is a directory");
3559 		goto fail;
3560 	    }
3561 	    if (mch_nodetype(fname) != NODE_WRITABLE)
3562 	    {
3563 		errnum = (char_u *)"E503: ";
3564 		errmsg = (char_u *)_("is not a file or writable device");
3565 		goto fail;
3566 	    }
3567 	    /* It's a device of some kind (or a fifo) which we can write to
3568 	     * but for which we can't make a backup. */
3569 	    device = TRUE;
3570 	    newfile = TRUE;
3571 	    perm = -1;
3572 	}
3573     }
3574 #else /* !UNIX */
3575     /*
3576      * Check for a writable device name.
3577      */
3578     c = mch_nodetype(fname);
3579     if (c == NODE_OTHER)
3580     {
3581 	errnum = (char_u *)"E503: ";
3582 	errmsg = (char_u *)_("is not a file or writable device");
3583 	goto fail;
3584     }
3585     if (c == NODE_WRITABLE)
3586     {
3587 # if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3588 	/* MS-Windows allows opening a device, but we will probably get stuck
3589 	 * trying to write to it.  */
3590 	if (!p_odev)
3591 	{
3592 	    errnum = (char_u *)"E796: ";
3593 	    errmsg = (char_u *)_("writing to device disabled with 'opendevice' option");
3594 	    goto fail;
3595 	}
3596 # endif
3597 	device = TRUE;
3598 	newfile = TRUE;
3599 	perm = -1;
3600     }
3601     else
3602     {
3603 	perm = mch_getperm(fname);
3604 	if (perm < 0)
3605 	    newfile = TRUE;
3606 	else if (mch_isdir(fname))
3607 	{
3608 	    errnum = (char_u *)"E502: ";
3609 	    errmsg = (char_u *)_("is a directory");
3610 	    goto fail;
3611 	}
3612 	if (overwriting)
3613 	    (void)mch_stat((char *)fname, &st_old);
3614     }
3615 #endif /* !UNIX */
3616 
3617     if (!device && !newfile)
3618     {
3619 	/*
3620 	 * Check if the file is really writable (when renaming the file to
3621 	 * make a backup we won't discover it later).
3622 	 */
3623 	file_readonly = check_file_readonly(fname, (int)perm);
3624 
3625 	if (!forceit && file_readonly)
3626 	{
3627 	    if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
3628 	    {
3629 		errnum = (char_u *)"E504: ";
3630 		errmsg = (char_u *)_(err_readonly);
3631 	    }
3632 	    else
3633 	    {
3634 		errnum = (char_u *)"E505: ";
3635 		errmsg = (char_u *)_("is read-only (add ! to override)");
3636 	    }
3637 	    goto fail;
3638 	}
3639 
3640 	/*
3641 	 * Check if the timestamp hasn't changed since reading the file.
3642 	 */
3643 	if (overwriting)
3644 	{
3645 	    retval = check_mtime(buf, &st_old);
3646 	    if (retval == FAIL)
3647 		goto fail;
3648 	}
3649     }
3650 
3651 #ifdef HAVE_ACL
3652     /*
3653      * For systems that support ACL: get the ACL from the original file.
3654      */
3655     if (!newfile)
3656 	acl = mch_get_acl(fname);
3657 #endif
3658 
3659     /*
3660      * If 'backupskip' is not empty, don't make a backup for some files.
3661      */
3662     dobackup = (p_wb || p_bk || *p_pm != NUL);
3663 #ifdef FEAT_WILDIGN
3664     if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname))
3665 	dobackup = FALSE;
3666 #endif
3667 
3668     /*
3669      * Save the value of got_int and reset it.  We don't want a previous
3670      * interruption cancel writing, only hitting CTRL-C while writing should
3671      * abort it.
3672      */
3673     prev_got_int = got_int;
3674     got_int = FALSE;
3675 
3676     /* Mark the buffer as 'being saved' to prevent changed buffer warnings */
3677     buf->b_saving = TRUE;
3678 
3679     /*
3680      * If we are not appending or filtering, the file exists, and the
3681      * 'writebackup', 'backup' or 'patchmode' option is set, need a backup.
3682      * When 'patchmode' is set also make a backup when appending.
3683      *
3684      * Do not make any backup, if 'writebackup' and 'backup' are both switched
3685      * off.  This helps when editing large files on almost-full disks.
3686      */
3687     if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup)
3688     {
3689 #if defined(UNIX) || defined(WIN32)
3690 	struct stat st;
3691 #endif
3692 
3693 	if ((bkc_flags & BKC_YES) || append)	/* "yes" */
3694 	    backup_copy = TRUE;
3695 #if defined(UNIX) || defined(WIN32)
3696 	else if ((bkc_flags & BKC_AUTO))	/* "auto" */
3697 	{
3698 	    int		i;
3699 
3700 # ifdef UNIX
3701 	    /*
3702 	     * Don't rename the file when:
3703 	     * - it's a hard link
3704 	     * - it's a symbolic link
3705 	     * - we don't have write permission in the directory
3706 	     * - we can't set the owner/group of the new file
3707 	     */
3708 	    if (st_old.st_nlink > 1
3709 		    || mch_lstat((char *)fname, &st) < 0
3710 		    || st.st_dev != st_old.st_dev
3711 		    || st.st_ino != st_old.st_ino
3712 #  ifndef HAVE_FCHOWN
3713 		    || st.st_uid != st_old.st_uid
3714 		    || st.st_gid != st_old.st_gid
3715 #  endif
3716 		    )
3717 		backup_copy = TRUE;
3718 	    else
3719 # else
3720 #  ifdef WIN32
3721 	    /* On NTFS file systems hard links are possible. */
3722 	    if (mch_is_linked(fname))
3723 		backup_copy = TRUE;
3724 	    else
3725 #  endif
3726 # endif
3727 	    {
3728 		/*
3729 		 * Check if we can create a file and set the owner/group to
3730 		 * the ones from the original file.
3731 		 * First find a file name that doesn't exist yet (use some
3732 		 * arbitrary numbers).
3733 		 */
3734 		STRCPY(IObuff, fname);
3735 		for (i = 4913; ; i += 123)
3736 		{
3737 		    sprintf((char *)gettail(IObuff), "%d", i);
3738 		    if (mch_lstat((char *)IObuff, &st) < 0)
3739 			break;
3740 		}
3741 		fd = mch_open((char *)IObuff,
3742 				    O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
3743 		if (fd < 0)	/* can't write in directory */
3744 		    backup_copy = TRUE;
3745 		else
3746 		{
3747 # ifdef UNIX
3748 #  ifdef HAVE_FCHOWN
3749 		    ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
3750 #  endif
3751 		    if (mch_stat((char *)IObuff, &st) < 0
3752 			    || st.st_uid != st_old.st_uid
3753 			    || st.st_gid != st_old.st_gid
3754 			    || (long)st.st_mode != perm)
3755 			backup_copy = TRUE;
3756 # endif
3757 		    /* Close the file before removing it, on MS-Windows we
3758 		     * can't delete an open file. */
3759 		    close(fd);
3760 		    mch_remove(IObuff);
3761 # ifdef MSWIN
3762 		    /* MS-Windows may trigger a virus scanner to open the
3763 		     * file, we can't delete it then.  Keep trying for half a
3764 		     * second. */
3765 		    {
3766 			int try;
3767 
3768 			for (try = 0; try < 10; ++try)
3769 			{
3770 			    if (mch_lstat((char *)IObuff, &st) < 0)
3771 				break;
3772 			    ui_delay(50L, TRUE);  /* wait 50 msec */
3773 			    mch_remove(IObuff);
3774 			}
3775 		    }
3776 # endif
3777 		}
3778 	    }
3779 	}
3780 
3781 # ifdef UNIX
3782 	/*
3783 	 * Break symlinks and/or hardlinks if we've been asked to.
3784 	 */
3785 	if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK))
3786 	{
3787 	    int	lstat_res;
3788 
3789 	    lstat_res = mch_lstat((char *)fname, &st);
3790 
3791 	    /* Symlinks. */
3792 	    if ((bkc_flags & BKC_BREAKSYMLINK)
3793 		    && lstat_res == 0
3794 		    && st.st_ino != st_old.st_ino)
3795 		backup_copy = FALSE;
3796 
3797 	    /* Hardlinks. */
3798 	    if ((bkc_flags & BKC_BREAKHARDLINK)
3799 		    && st_old.st_nlink > 1
3800 		    && (lstat_res != 0 || st.st_ino == st_old.st_ino))
3801 		backup_copy = FALSE;
3802 	}
3803 #endif
3804 
3805 #endif
3806 
3807 	/* make sure we have a valid backup extension to use */
3808 	if (*p_bex == NUL)
3809 	    backup_ext = (char_u *)".bak";
3810 	else
3811 	    backup_ext = p_bex;
3812 
3813 	if (backup_copy
3814 		&& (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
3815 	{
3816 	    int		bfd;
3817 	    char_u	*copybuf, *wp;
3818 	    int		some_error = FALSE;
3819 	    struct stat	st_new;
3820 	    char_u	*dirp;
3821 	    char_u	*rootname;
3822 #if defined(UNIX) && !defined(SHORT_FNAME)
3823 	    int		did_set_shortname;
3824 #endif
3825 
3826 	    copybuf = alloc(BUFSIZE + 1);
3827 	    if (copybuf == NULL)
3828 	    {
3829 		some_error = TRUE;	    /* out of memory */
3830 		goto nobackup;
3831 	    }
3832 
3833 	    /*
3834 	     * Try to make the backup in each directory in the 'bdir' option.
3835 	     *
3836 	     * Unix semantics has it, that we may have a writable file,
3837 	     * that cannot be recreated with a simple open(..., O_CREAT, ) e.g:
3838 	     *  - the directory is not writable,
3839 	     *  - the file may be a symbolic link,
3840 	     *  - the file may belong to another user/group, etc.
3841 	     *
3842 	     * For these reasons, the existing writable file must be truncated
3843 	     * and reused. Creation of a backup COPY will be attempted.
3844 	     */
3845 	    dirp = p_bdir;
3846 	    while (*dirp)
3847 	    {
3848 #ifdef UNIX
3849 		st_new.st_ino = 0;
3850 		st_new.st_dev = 0;
3851 		st_new.st_gid = 0;
3852 #endif
3853 
3854 		/*
3855 		 * Isolate one directory name, using an entry in 'bdir'.
3856 		 */
3857 		(void)copy_option_part(&dirp, copybuf, BUFSIZE, ",");
3858 		rootname = get_file_in_dir(fname, copybuf);
3859 		if (rootname == NULL)
3860 		{
3861 		    some_error = TRUE;	    /* out of memory */
3862 		    goto nobackup;
3863 		}
3864 
3865 #if defined(UNIX) && !defined(SHORT_FNAME)
3866 		did_set_shortname = FALSE;
3867 #endif
3868 
3869 		/*
3870 		 * May try twice if 'shortname' not set.
3871 		 */
3872 		for (;;)
3873 		{
3874 		    /*
3875 		     * Make backup file name.
3876 		     */
3877 		    backup = buf_modname(
3878 #ifdef SHORT_FNAME
3879 			    TRUE,
3880 #else
3881 			    (buf->b_p_sn || buf->b_shortname),
3882 #endif
3883 						 rootname, backup_ext, FALSE);
3884 		    if (backup == NULL)
3885 		    {
3886 			vim_free(rootname);
3887 			some_error = TRUE;		/* out of memory */
3888 			goto nobackup;
3889 		    }
3890 
3891 		    /*
3892 		     * Check if backup file already exists.
3893 		     */
3894 		    if (mch_stat((char *)backup, &st_new) >= 0)
3895 		    {
3896 #ifdef UNIX
3897 			/*
3898 			 * Check if backup file is same as original file.
3899 			 * May happen when modname() gave the same file back.
3900 			 * E.g. silly link, or file name-length reached.
3901 			 * If we don't check here, we either ruin the file
3902 			 * when copying or erase it after writing. jw.
3903 			 */
3904 			if (st_new.st_dev == st_old.st_dev
3905 					    && st_new.st_ino == st_old.st_ino)
3906 			{
3907 			    vim_free(backup);
3908 			    backup = NULL;	/* no backup file to delete */
3909 # ifndef SHORT_FNAME
3910 			    /*
3911 			     * may try again with 'shortname' set
3912 			     */
3913 			    if (!(buf->b_shortname || buf->b_p_sn))
3914 			    {
3915 				buf->b_shortname = TRUE;
3916 				did_set_shortname = TRUE;
3917 				continue;
3918 			    }
3919 				/* setting shortname didn't help */
3920 			    if (did_set_shortname)
3921 				buf->b_shortname = FALSE;
3922 # endif
3923 			    break;
3924 			}
3925 #endif
3926 
3927 			/*
3928 			 * If we are not going to keep the backup file, don't
3929 			 * delete an existing one, try to use another name.
3930 			 * Change one character, just before the extension.
3931 			 */
3932 			if (!p_bk)
3933 			{
3934 			    wp = backup + STRLEN(backup) - 1
3935 							 - STRLEN(backup_ext);
3936 			    if (wp < backup)	/* empty file name ??? */
3937 				wp = backup;
3938 			    *wp = 'z';
3939 			    while (*wp > 'a'
3940 				    && mch_stat((char *)backup, &st_new) >= 0)
3941 				--*wp;
3942 			    /* They all exist??? Must be something wrong. */
3943 			    if (*wp == 'a')
3944 			    {
3945 				vim_free(backup);
3946 				backup = NULL;
3947 			    }
3948 			}
3949 		    }
3950 		    break;
3951 		}
3952 		vim_free(rootname);
3953 
3954 		/*
3955 		 * Try to create the backup file
3956 		 */
3957 		if (backup != NULL)
3958 		{
3959 		    /* remove old backup, if present */
3960 		    mch_remove(backup);
3961 		    /* Open with O_EXCL to avoid the file being created while
3962 		     * we were sleeping (symlink hacker attack?) */
3963 		    bfd = mch_open((char *)backup,
3964 				O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW,
3965 								 perm & 0777);
3966 		    if (bfd < 0)
3967 		    {
3968 			vim_free(backup);
3969 			backup = NULL;
3970 		    }
3971 		    else
3972 		    {
3973 			/* set file protection same as original file, but
3974 			 * strip s-bit */
3975 			(void)mch_setperm(backup, perm & 0777);
3976 
3977 #ifdef UNIX
3978 			/*
3979 			 * Try to set the group of the backup same as the
3980 			 * original file. If this fails, set the protection
3981 			 * bits for the group same as the protection bits for
3982 			 * others.
3983 			 */
3984 			if (st_new.st_gid != st_old.st_gid
3985 # ifdef HAVE_FCHOWN  /* sequent-ptx lacks fchown() */
3986 				&& fchown(bfd, (uid_t)-1, st_old.st_gid) != 0
3987 # endif
3988 						)
3989 			    mch_setperm(backup,
3990 					  (perm & 0707) | ((perm & 07) << 3));
3991 # ifdef HAVE_SELINUX
3992 			mch_copy_sec(fname, backup);
3993 # endif
3994 #endif
3995 
3996 			/*
3997 			 * copy the file.
3998 			 */
3999 			write_info.bw_fd = bfd;
4000 			write_info.bw_buf = copybuf;
4001 #ifdef HAS_BW_FLAGS
4002 			write_info.bw_flags = FIO_NOCONVERT;
4003 #endif
4004 			while ((write_info.bw_len = read_eintr(fd, copybuf,
4005 								BUFSIZE)) > 0)
4006 			{
4007 			    if (buf_write_bytes(&write_info) == FAIL)
4008 			    {
4009 				errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)");
4010 				break;
4011 			    }
4012 			    ui_breakcheck();
4013 			    if (got_int)
4014 			    {
4015 				errmsg = (char_u *)_(e_interr);
4016 				break;
4017 			    }
4018 			}
4019 
4020 			if (close(bfd) < 0 && errmsg == NULL)
4021 			    errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)");
4022 			if (write_info.bw_len < 0)
4023 			    errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)");
4024 #ifdef UNIX
4025 			set_file_time(backup, st_old.st_atime, st_old.st_mtime);
4026 #endif
4027 #ifdef HAVE_ACL
4028 			mch_set_acl(backup, acl);
4029 #endif
4030 #ifdef HAVE_SELINUX
4031 			mch_copy_sec(fname, backup);
4032 #endif
4033 			break;
4034 		    }
4035 		}
4036 	    }
4037     nobackup:
4038 	    close(fd);		/* ignore errors for closing read file */
4039 	    vim_free(copybuf);
4040 
4041 	    if (backup == NULL && errmsg == NULL)
4042 		errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)");
4043 	    /* ignore errors when forceit is TRUE */
4044 	    if ((some_error || errmsg != NULL) && !forceit)
4045 	    {
4046 		retval = FAIL;
4047 		goto fail;
4048 	    }
4049 	    errmsg = NULL;
4050 	}
4051 	else
4052 	{
4053 	    char_u	*dirp;
4054 	    char_u	*p;
4055 	    char_u	*rootname;
4056 
4057 	    /*
4058 	     * Make a backup by renaming the original file.
4059 	     */
4060 	    /*
4061 	     * If 'cpoptions' includes the "W" flag, we don't want to
4062 	     * overwrite a read-only file.  But rename may be possible
4063 	     * anyway, thus we need an extra check here.
4064 	     */
4065 	    if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL)
4066 	    {
4067 		errnum = (char_u *)"E504: ";
4068 		errmsg = (char_u *)_(err_readonly);
4069 		goto fail;
4070 	    }
4071 
4072 	    /*
4073 	     *
4074 	     * Form the backup file name - change path/fo.o.h to
4075 	     * path/fo.o.h.bak Try all directories in 'backupdir', first one
4076 	     * that works is used.
4077 	     */
4078 	    dirp = p_bdir;
4079 	    while (*dirp)
4080 	    {
4081 		/*
4082 		 * Isolate one directory name and make the backup file name.
4083 		 */
4084 		(void)copy_option_part(&dirp, IObuff, IOSIZE, ",");
4085 		rootname = get_file_in_dir(fname, IObuff);
4086 		if (rootname == NULL)
4087 		    backup = NULL;
4088 		else
4089 		{
4090 		    backup = buf_modname(
4091 #ifdef SHORT_FNAME
4092 			    TRUE,
4093 #else
4094 			    (buf->b_p_sn || buf->b_shortname),
4095 #endif
4096 						 rootname, backup_ext, FALSE);
4097 		    vim_free(rootname);
4098 		}
4099 
4100 		if (backup != NULL)
4101 		{
4102 		    /*
4103 		     * If we are not going to keep the backup file, don't
4104 		     * delete an existing one, try to use another name.
4105 		     * Change one character, just before the extension.
4106 		     */
4107 		    if (!p_bk && mch_getperm(backup) >= 0)
4108 		    {
4109 			p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
4110 			if (p < backup)	/* empty file name ??? */
4111 			    p = backup;
4112 			*p = 'z';
4113 			while (*p > 'a' && mch_getperm(backup) >= 0)
4114 			    --*p;
4115 			/* They all exist??? Must be something wrong! */
4116 			if (*p == 'a')
4117 			{
4118 			    vim_free(backup);
4119 			    backup = NULL;
4120 			}
4121 		    }
4122 		}
4123 		if (backup != NULL)
4124 		{
4125 		    /*
4126 		     * Delete any existing backup and move the current version
4127 		     * to the backup.	For safety, we don't remove the backup
4128 		     * until the write has finished successfully. And if the
4129 		     * 'backup' option is set, leave it around.
4130 		     */
4131 		    /*
4132 		     * If the renaming of the original file to the backup file
4133 		     * works, quit here.
4134 		     */
4135 		    if (vim_rename(fname, backup) == 0)
4136 			break;
4137 
4138 		    vim_free(backup);   /* don't do the rename below */
4139 		    backup = NULL;
4140 		}
4141 	    }
4142 	    if (backup == NULL && !forceit)
4143 	    {
4144 		errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)");
4145 		goto fail;
4146 	    }
4147 	}
4148     }
4149 
4150 #if defined(UNIX) && !defined(ARCHIE)
4151     /* When using ":w!" and the file was read-only: make it writable */
4152     if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
4153 				     && vim_strchr(p_cpo, CPO_FWRITE) == NULL)
4154     {
4155 	perm |= 0200;
4156 	(void)mch_setperm(fname, perm);
4157 	made_writable = TRUE;
4158     }
4159 #endif
4160 
4161     /* When using ":w!" and writing to the current file, 'readonly' makes no
4162      * sense, reset it, unless 'Z' appears in 'cpoptions'.  */
4163     if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL)
4164     {
4165 	buf->b_p_ro = FALSE;
4166 #ifdef FEAT_TITLE
4167 	need_maketitle = TRUE;	    /* set window title later */
4168 #endif
4169 #ifdef FEAT_WINDOWS
4170 	status_redraw_all();	    /* redraw status lines later */
4171 #endif
4172     }
4173 
4174     if (end > buf->b_ml.ml_line_count)
4175 	end = buf->b_ml.ml_line_count;
4176     if (buf->b_ml.ml_flags & ML_EMPTY)
4177 	start = end + 1;
4178 
4179     /*
4180      * If the original file is being overwritten, there is a small chance that
4181      * we crash in the middle of writing. Therefore the file is preserved now.
4182      * This makes all block numbers positive so that recovery does not need
4183      * the original file.
4184      * Don't do this if there is a backup file and we are exiting.
4185      */
4186     if (reset_changed && !newfile && overwriting
4187 					      && !(exiting && backup != NULL))
4188     {
4189 	ml_preserve(buf, FALSE);
4190 	if (got_int)
4191 	{
4192 	    errmsg = (char_u *)_(e_interr);
4193 	    goto restore_backup;
4194 	}
4195     }
4196 
4197 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */
4198     /*
4199      * Before risking to lose the original file verify if there's
4200      * a resource fork to preserve, and if cannot be done warn
4201      * the users. This happens when overwriting without backups.
4202      */
4203     if (backup == NULL && overwriting && !append)
4204 	if (mch_has_resource_fork(fname))
4205 	{
4206 	    errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)");
4207 	    goto restore_backup;
4208 	}
4209 #endif
4210 
4211 #ifdef VMS
4212     vms_remove_version(fname); /* remove version */
4213 #endif
4214     /* Default: write the file directly.  May write to a temp file for
4215      * multi-byte conversion. */
4216     wfname = fname;
4217 
4218 #ifdef FEAT_MBYTE
4219     /* Check for forced 'fileencoding' from "++opt=val" argument. */
4220     if (eap != NULL && eap->force_enc != 0)
4221     {
4222 	fenc = eap->cmd + eap->force_enc;
4223 	fenc = enc_canonize(fenc);
4224 	fenc_tofree = fenc;
4225     }
4226     else
4227 	fenc = buf->b_p_fenc;
4228 
4229     /*
4230      * Check if the file needs to be converted.
4231      */
4232     converted = need_conversion(fenc);
4233 
4234     /*
4235      * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done.  Or
4236      * Latin1 to Unicode conversion.  This is handled in buf_write_bytes().
4237      * Prepare the flags for it and allocate bw_conv_buf when needed.
4238      */
4239     if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0))
4240     {
4241 	wb_flags = get_fio_flags(fenc);
4242 	if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8))
4243 	{
4244 	    /* Need to allocate a buffer to translate into. */
4245 	    if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8))
4246 		write_info.bw_conv_buflen = bufsize * 2;
4247 	    else /* FIO_UCS4 */
4248 		write_info.bw_conv_buflen = bufsize * 4;
4249 	    write_info.bw_conv_buf
4250 			   = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4251 	    if (write_info.bw_conv_buf == NULL)
4252 		end = 0;
4253 	}
4254     }
4255 
4256 # ifdef WIN3264
4257     if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0)
4258     {
4259 	/* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS.  Worst-case * 4: */
4260 	write_info.bw_conv_buflen = bufsize * 4;
4261 	write_info.bw_conv_buf
4262 			    = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4263 	if (write_info.bw_conv_buf == NULL)
4264 	    end = 0;
4265     }
4266 # endif
4267 
4268 # ifdef MACOS_X
4269     if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0)
4270     {
4271 	write_info.bw_conv_buflen = bufsize * 3;
4272 	write_info.bw_conv_buf
4273 			    = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4274 	if (write_info.bw_conv_buf == NULL)
4275 	    end = 0;
4276     }
4277 # endif
4278 
4279 # if defined(FEAT_EVAL) || defined(USE_ICONV)
4280     if (converted && wb_flags == 0)
4281     {
4282 #  ifdef USE_ICONV
4283 	/*
4284 	 * Use iconv() conversion when conversion is needed and it's not done
4285 	 * internally.
4286 	 */
4287 	write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc,
4288 					enc_utf8 ? (char_u *)"utf-8" : p_enc);
4289 	if (write_info.bw_iconv_fd != (iconv_t)-1)
4290 	{
4291 	    /* We're going to use iconv(), allocate a buffer to convert in. */
4292 	    write_info.bw_conv_buflen = bufsize * ICONV_MULT;
4293 	    write_info.bw_conv_buf
4294 			   = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4295 	    if (write_info.bw_conv_buf == NULL)
4296 		end = 0;
4297 	    write_info.bw_first = TRUE;
4298 	}
4299 #   ifdef FEAT_EVAL
4300 	else
4301 #   endif
4302 #  endif
4303 
4304 #  ifdef FEAT_EVAL
4305 	    /*
4306 	     * When the file needs to be converted with 'charconvert' after
4307 	     * writing, write to a temp file instead and let the conversion
4308 	     * overwrite the original file.
4309 	     */
4310 	    if (*p_ccv != NUL)
4311 	    {
4312 		wfname = vim_tempname('w');
4313 		if (wfname == NULL)	/* Can't write without a tempfile! */
4314 		{
4315 		    errmsg = (char_u *)_("E214: Can't find temp file for writing");
4316 		    goto restore_backup;
4317 		}
4318 	    }
4319 #  endif
4320     }
4321 # endif
4322     if (converted && wb_flags == 0
4323 #  ifdef USE_ICONV
4324 	    && write_info.bw_iconv_fd == (iconv_t)-1
4325 #  endif
4326 #  ifdef FEAT_EVAL
4327 	    && wfname == fname
4328 #  endif
4329 	    )
4330     {
4331 	if (!forceit)
4332 	{
4333 	    errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)");
4334 	    goto restore_backup;
4335 	}
4336 	notconverted = TRUE;
4337     }
4338 #endif
4339 
4340     /*
4341      * Open the file "wfname" for writing.
4342      * We may try to open the file twice: If we can't write to the
4343      * file and forceit is TRUE we delete the existing file and try to create
4344      * a new one. If this still fails we may have lost the original file!
4345      * (this may happen when the user reached his quotum for number of files).
4346      * Appending will fail if the file does not exist and forceit is FALSE.
4347      */
4348     while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append
4349 			? (forceit ? (O_APPEND | O_CREAT) : O_APPEND)
4350 			: (O_CREAT | O_TRUNC))
4351 			, perm < 0 ? 0666 : (perm & 0777))) < 0)
4352     {
4353 	/*
4354 	 * A forced write will try to create a new file if the old one is
4355 	 * still readonly. This may also happen when the directory is
4356 	 * read-only. In that case the mch_remove() will fail.
4357 	 */
4358 	if (errmsg == NULL)
4359 	{
4360 #ifdef UNIX
4361 	    struct stat	st;
4362 
4363 	    /* Don't delete the file when it's a hard or symbolic link. */
4364 	    if ((!newfile && st_old.st_nlink > 1)
4365 		    || (mch_lstat((char *)fname, &st) == 0
4366 			&& (st.st_dev != st_old.st_dev
4367 			    || st.st_ino != st_old.st_ino)))
4368 		errmsg = (char_u *)_("E166: Can't open linked file for writing");
4369 	    else
4370 #endif
4371 	    {
4372 		errmsg = (char_u *)_("E212: Can't open file for writing");
4373 		if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL
4374 								 && perm >= 0)
4375 		{
4376 #ifdef UNIX
4377 		    /* we write to the file, thus it should be marked
4378 		       writable after all */
4379 		    if (!(perm & 0200))
4380 			made_writable = TRUE;
4381 		    perm |= 0200;
4382 		    if (st_old.st_uid != getuid() || st_old.st_gid != getgid())
4383 			perm &= 0777;
4384 #endif
4385 		    if (!append)	    /* don't remove when appending */
4386 			mch_remove(wfname);
4387 		    continue;
4388 		}
4389 	    }
4390 	}
4391 
4392 restore_backup:
4393 	{
4394 	    struct stat st;
4395 
4396 	    /*
4397 	     * If we failed to open the file, we don't need a backup. Throw it
4398 	     * away.  If we moved or removed the original file try to put the
4399 	     * backup in its place.
4400 	     */
4401 	    if (backup != NULL && wfname == fname)
4402 	    {
4403 		if (backup_copy)
4404 		{
4405 		    /*
4406 		     * There is a small chance that we removed the original,
4407 		     * try to move the copy in its place.
4408 		     * This may not work if the vim_rename() fails.
4409 		     * In that case we leave the copy around.
4410 		     */
4411 		    /* If file does not exist, put the copy in its place */
4412 		    if (mch_stat((char *)fname, &st) < 0)
4413 			vim_rename(backup, fname);
4414 		    /* if original file does exist throw away the copy */
4415 		    if (mch_stat((char *)fname, &st) >= 0)
4416 			mch_remove(backup);
4417 		}
4418 		else
4419 		{
4420 		    /* try to put the original file back */
4421 		    vim_rename(backup, fname);
4422 		}
4423 	    }
4424 
4425 	    /* if original file no longer exists give an extra warning */
4426 	    if (!newfile && mch_stat((char *)fname, &st) < 0)
4427 		end = 0;
4428 	}
4429 
4430 #ifdef FEAT_MBYTE
4431 	if (wfname != fname)
4432 	    vim_free(wfname);
4433 #endif
4434 	goto fail;
4435     }
4436     errmsg = NULL;
4437 
4438 #if defined(MACOS_CLASSIC) || defined(WIN3264)
4439     /* TODO: Is it need for MACOS_X? (Dany) */
4440     /*
4441      * On macintosh copy the original files attributes (i.e. the backup)
4442      * This is done in order to preserve the resource fork and the
4443      * Finder attribute (label, comments, custom icons, file creator)
4444      */
4445     if (backup != NULL && overwriting && !append)
4446     {
4447 	if (backup_copy)
4448 	    (void)mch_copy_file_attribute(wfname, backup);
4449 	else
4450 	    (void)mch_copy_file_attribute(backup, wfname);
4451     }
4452 
4453     if (!overwriting && !append)
4454     {
4455 	if (buf->b_ffname != NULL)
4456 	    (void)mch_copy_file_attribute(buf->b_ffname, wfname);
4457 	/* Should copy resource fork */
4458     }
4459 #endif
4460 
4461     write_info.bw_fd = fd;
4462 
4463 #ifdef FEAT_CRYPT
4464     if (*buf->b_p_key != NUL && !filtering)
4465     {
4466 	char_u *header;
4467 	int    header_len;
4468 
4469 	header = prepare_crypt_write(buf, &header_len);
4470 	if (header == NULL)
4471 	    end = 0;
4472 	else
4473 	{
4474 	    /* Write magic number, so that Vim knows that this file is
4475 	     * encrypted when reading it again.  This also undergoes utf-8 to
4476 	     * ucs-2/4 conversion when needed. */
4477 	    write_info.bw_buf = header;
4478 	    write_info.bw_len = header_len;
4479 	    write_info.bw_flags = FIO_NOCONVERT;
4480 	    if (buf_write_bytes(&write_info) == FAIL)
4481 		end = 0;
4482 	    wb_flags |= FIO_ENCRYPTED;
4483 	    vim_free(header);
4484 	}
4485     }
4486 #endif
4487 
4488     write_info.bw_buf = buffer;
4489     nchars = 0;
4490 
4491     /* use "++bin", "++nobin" or 'binary' */
4492     if (eap != NULL && eap->force_bin != 0)
4493 	write_bin = (eap->force_bin == FORCE_BIN);
4494     else
4495 	write_bin = buf->b_p_bin;
4496 
4497 #ifdef FEAT_MBYTE
4498     /*
4499      * The BOM is written just after the encryption magic number.
4500      * Skip it when appending and the file already existed, the BOM only makes
4501      * sense at the start of the file.
4502      */
4503     if (buf->b_p_bomb && !write_bin && (!append || perm < 0))
4504     {
4505 	write_info.bw_len = make_bom(buffer, fenc);
4506 	if (write_info.bw_len > 0)
4507 	{
4508 	    /* don't convert, do encryption */
4509 	    write_info.bw_flags = FIO_NOCONVERT | wb_flags;
4510 	    if (buf_write_bytes(&write_info) == FAIL)
4511 		end = 0;
4512 	    else
4513 		nchars += write_info.bw_len;
4514 	}
4515     }
4516     write_info.bw_start_lnum = start;
4517 #endif
4518 
4519 #ifdef FEAT_PERSISTENT_UNDO
4520     write_undo_file = (buf->b_p_udf && overwriting && !append
4521 					      && !filtering && reset_changed);
4522     if (write_undo_file)
4523 	/* Prepare for computing the hash value of the text. */
4524 	sha256_start(&sha_ctx);
4525 #endif
4526 
4527     write_info.bw_len = bufsize;
4528 #ifdef HAS_BW_FLAGS
4529     write_info.bw_flags = wb_flags;
4530 #endif
4531     fileformat = get_fileformat_force(buf, eap);
4532     s = buffer;
4533     len = 0;
4534     for (lnum = start; lnum <= end; ++lnum)
4535     {
4536 	/*
4537 	 * The next while loop is done once for each character written.
4538 	 * Keep it fast!
4539 	 */
4540 	ptr = ml_get_buf(buf, lnum, FALSE) - 1;
4541 #ifdef FEAT_PERSISTENT_UNDO
4542 	if (write_undo_file)
4543 	    sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1));
4544 #endif
4545 	while ((c = *++ptr) != NUL)
4546 	{
4547 	    if (c == NL)
4548 		*s = NUL;		/* replace newlines with NULs */
4549 	    else if (c == CAR && fileformat == EOL_MAC)
4550 		*s = NL;		/* Mac: replace CRs with NLs */
4551 	    else
4552 		*s = c;
4553 	    ++s;
4554 	    if (++len != bufsize)
4555 		continue;
4556 	    if (buf_write_bytes(&write_info) == FAIL)
4557 	    {
4558 		end = 0;		/* write error: break loop */
4559 		break;
4560 	    }
4561 	    nchars += bufsize;
4562 	    s = buffer;
4563 	    len = 0;
4564 #ifdef FEAT_MBYTE
4565 	    write_info.bw_start_lnum = lnum;
4566 #endif
4567 	}
4568 	/* write failed or last line has no EOL: stop here */
4569 	if (end == 0
4570 		|| (lnum == end
4571 		    && write_bin
4572 		    && (lnum == buf->b_no_eol_lnum
4573 			|| (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol))))
4574 	{
4575 	    ++lnum;			/* written the line, count it */
4576 	    no_eol = TRUE;
4577 	    break;
4578 	}
4579 	if (fileformat == EOL_UNIX)
4580 	    *s++ = NL;
4581 	else
4582 	{
4583 	    *s++ = CAR;			/* EOL_MAC or EOL_DOS: write CR */
4584 	    if (fileformat == EOL_DOS)	/* write CR-NL */
4585 	    {
4586 		if (++len == bufsize)
4587 		{
4588 		    if (buf_write_bytes(&write_info) == FAIL)
4589 		    {
4590 			end = 0;	/* write error: break loop */
4591 			break;
4592 		    }
4593 		    nchars += bufsize;
4594 		    s = buffer;
4595 		    len = 0;
4596 		}
4597 		*s++ = NL;
4598 	    }
4599 	}
4600 	if (++len == bufsize && end)
4601 	{
4602 	    if (buf_write_bytes(&write_info) == FAIL)
4603 	    {
4604 		end = 0;		/* write error: break loop */
4605 		break;
4606 	    }
4607 	    nchars += bufsize;
4608 	    s = buffer;
4609 	    len = 0;
4610 
4611 	    ui_breakcheck();
4612 	    if (got_int)
4613 	    {
4614 		end = 0;		/* Interrupted, break loop */
4615 		break;
4616 	    }
4617 	}
4618 #ifdef VMS
4619 	/*
4620 	 * On VMS there is a problem: newlines get added when writing blocks
4621 	 * at a time. Fix it by writing a line at a time.
4622 	 * This is much slower!
4623 	 * Explanation: VAX/DECC RTL insists that records in some RMS
4624 	 * structures end with a newline (carriage return) character, and if
4625 	 * they don't it adds one.
4626 	 * With other RMS structures it works perfect without this fix.
4627 	 */
4628 	if (buf->b_fab_rfm == FAB$C_VFC
4629 		|| ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4630 	{
4631 	    int b2write;
4632 
4633 	    buf->b_fab_mrs = (buf->b_fab_mrs == 0
4634 		    ? MIN(4096, bufsize)
4635 		    : MIN(buf->b_fab_mrs, bufsize));
4636 
4637 	    b2write = len;
4638 	    while (b2write > 0)
4639 	    {
4640 		write_info.bw_len = MIN(b2write, buf->b_fab_mrs);
4641 		if (buf_write_bytes(&write_info) == FAIL)
4642 		{
4643 		    end = 0;
4644 		    break;
4645 		}
4646 		b2write -= MIN(b2write, buf->b_fab_mrs);
4647 	    }
4648 	    write_info.bw_len = bufsize;
4649 	    nchars += len;
4650 	    s = buffer;
4651 	    len = 0;
4652 	}
4653 #endif
4654     }
4655     if (len > 0 && end > 0)
4656     {
4657 	write_info.bw_len = len;
4658 	if (buf_write_bytes(&write_info) == FAIL)
4659 	    end = 0;		    /* write error */
4660 	nchars += len;
4661     }
4662 
4663 #if defined(UNIX) && defined(HAVE_FSYNC)
4664     /* On many journalling file systems there is a bug that causes both the
4665      * original and the backup file to be lost when halting the system right
4666      * after writing the file.  That's because only the meta-data is
4667      * journalled.  Syncing the file slows down the system, but assures it has
4668      * been written to disk and we don't lose it.
4669      * For a device do try the fsync() but don't complain if it does not work
4670      * (could be a pipe).
4671      * If the 'fsync' option is FALSE, don't fsync().  Useful for laptops. */
4672     if (p_fs && fsync(fd) != 0 && !device)
4673     {
4674 	errmsg = (char_u *)_("E667: Fsync failed");
4675 	end = 0;
4676     }
4677 #endif
4678 
4679 #ifdef HAVE_SELINUX
4680     /* Probably need to set the security context. */
4681     if (!backup_copy)
4682 	mch_copy_sec(backup, wfname);
4683 #endif
4684 
4685 #ifdef UNIX
4686     /* When creating a new file, set its owner/group to that of the original
4687      * file.  Get the new device and inode number. */
4688     if (backup != NULL && !backup_copy)
4689     {
4690 # ifdef HAVE_FCHOWN
4691 	struct stat	st;
4692 
4693 	/* don't change the owner when it's already OK, some systems remove
4694 	 * permission or ACL stuff */
4695 	if (mch_stat((char *)wfname, &st) < 0
4696 		|| st.st_uid != st_old.st_uid
4697 		|| st.st_gid != st_old.st_gid)
4698 	{
4699 	    ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
4700 	    if (perm >= 0)	/* set permission again, may have changed */
4701 		(void)mch_setperm(wfname, perm);
4702 	}
4703 # endif
4704 	buf_setino(buf);
4705     }
4706     else if (!buf->b_dev_valid)
4707 	/* Set the inode when creating a new file. */
4708 	buf_setino(buf);
4709 #endif
4710 
4711     if (close(fd) != 0)
4712     {
4713 	errmsg = (char_u *)_("E512: Close failed");
4714 	end = 0;
4715     }
4716 
4717 #ifdef UNIX
4718     if (made_writable)
4719 	perm &= ~0200;		/* reset 'w' bit for security reasons */
4720 #endif
4721     if (perm >= 0)		/* set perm. of new file same as old file */
4722 	(void)mch_setperm(wfname, perm);
4723 #ifdef HAVE_ACL
4724     /* Probably need to set the ACL before changing the user (can't set the
4725      * ACL on a file the user doesn't own). */
4726     if (!backup_copy)
4727 	mch_set_acl(wfname, acl);
4728 #endif
4729 #ifdef FEAT_CRYPT
4730     crypt_method_used = use_crypt_method;
4731     if (wb_flags & FIO_ENCRYPTED)
4732 	crypt_pop_state();
4733 #endif
4734 
4735 
4736 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL)
4737     if (wfname != fname)
4738     {
4739 	/*
4740 	 * The file was written to a temp file, now it needs to be converted
4741 	 * with 'charconvert' to (overwrite) the output file.
4742 	 */
4743 	if (end != 0)
4744 	{
4745 	    if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc,
4746 						       wfname, fname) == FAIL)
4747 	    {
4748 		write_info.bw_conv_error = TRUE;
4749 		end = 0;
4750 	    }
4751 	}
4752 	mch_remove(wfname);
4753 	vim_free(wfname);
4754     }
4755 #endif
4756 
4757     if (end == 0)
4758     {
4759 	if (errmsg == NULL)
4760 	{
4761 #ifdef FEAT_MBYTE
4762 	    if (write_info.bw_conv_error)
4763 	    {
4764 		if (write_info.bw_conv_error_lnum == 0)
4765 		    errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)");
4766 		else
4767 		{
4768 		    errmsg_allocated = TRUE;
4769 		    errmsg = alloc(300);
4770 		    vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"),
4771 					 (long)write_info.bw_conv_error_lnum);
4772 		}
4773 	    }
4774 	    else
4775 #endif
4776 		if (got_int)
4777 		    errmsg = (char_u *)_(e_interr);
4778 		else
4779 		    errmsg = (char_u *)_("E514: write error (file system full?)");
4780 	}
4781 
4782 	/*
4783 	 * If we have a backup file, try to put it in place of the new file,
4784 	 * because the new file is probably corrupt.  This avoids losing the
4785 	 * original file when trying to make a backup when writing the file a
4786 	 * second time.
4787 	 * When "backup_copy" is set we need to copy the backup over the new
4788 	 * file.  Otherwise rename the backup file.
4789 	 * If this is OK, don't give the extra warning message.
4790 	 */
4791 	if (backup != NULL)
4792 	{
4793 	    if (backup_copy)
4794 	    {
4795 		/* This may take a while, if we were interrupted let the user
4796 		 * know we got the message. */
4797 		if (got_int)
4798 		{
4799 		    MSG(_(e_interr));
4800 		    out_flush();
4801 		}
4802 		if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0)
4803 		{
4804 		    if ((write_info.bw_fd = mch_open((char *)fname,
4805 				    O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA,
4806 							   perm & 0777)) >= 0)
4807 		    {
4808 			/* copy the file. */
4809 			write_info.bw_buf = smallbuf;
4810 #ifdef HAS_BW_FLAGS
4811 			write_info.bw_flags = FIO_NOCONVERT;
4812 #endif
4813 			while ((write_info.bw_len = read_eintr(fd, smallbuf,
4814 						      SMBUFSIZE)) > 0)
4815 			    if (buf_write_bytes(&write_info) == FAIL)
4816 				break;
4817 
4818 			if (close(write_info.bw_fd) >= 0
4819 						   && write_info.bw_len == 0)
4820 			    end = 1;		/* success */
4821 		    }
4822 		    close(fd);	/* ignore errors for closing read file */
4823 		}
4824 	    }
4825 	    else
4826 	    {
4827 		if (vim_rename(backup, fname) == 0)
4828 		    end = 1;
4829 	    }
4830 	}
4831 	goto fail;
4832     }
4833 
4834     lnum -= start;	    /* compute number of written lines */
4835     --no_wait_return;	    /* may wait for return now */
4836 
4837 #if !(defined(UNIX) || defined(VMS))
4838     fname = sfname;	    /* use shortname now, for the messages */
4839 #endif
4840     if (!filtering)
4841     {
4842 	msg_add_fname(buf, fname);	/* put fname in IObuff with quotes */
4843 	c = FALSE;
4844 #ifdef FEAT_MBYTE
4845 	if (write_info.bw_conv_error)
4846 	{
4847 	    STRCAT(IObuff, _(" CONVERSION ERROR"));
4848 	    c = TRUE;
4849 	    if (write_info.bw_conv_error_lnum != 0)
4850 		vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"),
4851 			(long)write_info.bw_conv_error_lnum);
4852 	}
4853 	else if (notconverted)
4854 	{
4855 	    STRCAT(IObuff, _("[NOT converted]"));
4856 	    c = TRUE;
4857 	}
4858 	else if (converted)
4859 	{
4860 	    STRCAT(IObuff, _("[converted]"));
4861 	    c = TRUE;
4862 	}
4863 #endif
4864 	if (device)
4865 	{
4866 	    STRCAT(IObuff, _("[Device]"));
4867 	    c = TRUE;
4868 	}
4869 	else if (newfile)
4870 	{
4871 	    STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]"));
4872 	    c = TRUE;
4873 	}
4874 	if (no_eol)
4875 	{
4876 	    msg_add_eol();
4877 	    c = TRUE;
4878 	}
4879 	/* may add [unix/dos/mac] */
4880 	if (msg_add_fileformat(fileformat))
4881 	    c = TRUE;
4882 #ifdef FEAT_CRYPT
4883 	if (wb_flags & FIO_ENCRYPTED)
4884 	{
4885 	    if (crypt_method_used == 1)
4886 		STRCAT(IObuff, _("[blowfish]"));
4887 	    else
4888 		STRCAT(IObuff, _("[crypted]"));
4889 	    c = TRUE;
4890 	}
4891 #endif
4892 	msg_add_lines(c, (long)lnum, nchars);	/* add line/char count */
4893 	if (!shortmess(SHM_WRITE))
4894 	{
4895 	    if (append)
4896 		STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended"));
4897 	    else
4898 		STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written"));
4899 	}
4900 
4901 	set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0);
4902     }
4903 
4904     /* When written everything correctly: reset 'modified'.  Unless not
4905      * writing to the original file and '+' is not in 'cpoptions'. */
4906     if (reset_changed && whole && !append
4907 #ifdef FEAT_MBYTE
4908 	    && !write_info.bw_conv_error
4909 #endif
4910 	    && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)
4911 	    )
4912     {
4913 	unchanged(buf, TRUE);
4914 	u_unchanged(buf);
4915 	u_update_save_nr(buf);
4916     }
4917 
4918     /*
4919      * If written to the current file, update the timestamp of the swap file
4920      * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime.
4921      */
4922     if (overwriting)
4923     {
4924 	ml_timestamp(buf);
4925 	if (append)
4926 	    buf->b_flags &= ~BF_NEW;
4927 	else
4928 	    buf->b_flags &= ~BF_WRITE_MASK;
4929     }
4930 
4931     /*
4932      * If we kept a backup until now, and we are in patch mode, then we make
4933      * the backup file our 'original' file.
4934      */
4935     if (*p_pm && dobackup)
4936     {
4937 	char *org = (char *)buf_modname(
4938 #ifdef SHORT_FNAME
4939 					TRUE,
4940 #else
4941 					(buf->b_p_sn || buf->b_shortname),
4942 #endif
4943 							  fname, p_pm, FALSE);
4944 
4945 	if (backup != NULL)
4946 	{
4947 	    struct stat st;
4948 
4949 	    /*
4950 	     * If the original file does not exist yet
4951 	     * the current backup file becomes the original file
4952 	     */
4953 	    if (org == NULL)
4954 		EMSG(_("E205: Patchmode: can't save original file"));
4955 	    else if (mch_stat(org, &st) < 0)
4956 	    {
4957 		vim_rename(backup, (char_u *)org);
4958 		vim_free(backup);	    /* don't delete the file */
4959 		backup = NULL;
4960 #ifdef UNIX
4961 		set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime);
4962 #endif
4963 	    }
4964 	}
4965 	/*
4966 	 * If there is no backup file, remember that a (new) file was
4967 	 * created.
4968 	 */
4969 	else
4970 	{
4971 	    int empty_fd;
4972 
4973 	    if (org == NULL
4974 		    || (empty_fd = mch_open(org,
4975 				      O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW,
4976 					perm < 0 ? 0666 : (perm & 0777))) < 0)
4977 	      EMSG(_("E206: patchmode: can't touch empty original file"));
4978 	    else
4979 	      close(empty_fd);
4980 	}
4981 	if (org != NULL)
4982 	{
4983 	    mch_setperm((char_u *)org, mch_getperm(fname) & 0777);
4984 	    vim_free(org);
4985 	}
4986     }
4987 
4988     /*
4989      * Remove the backup unless 'backup' option is set
4990      */
4991     if (!p_bk && backup != NULL && mch_remove(backup) != 0)
4992 	EMSG(_("E207: Can't delete backup file"));
4993 
4994 #ifdef FEAT_SUN_WORKSHOP
4995     if (usingSunWorkShop)
4996 	workshop_file_saved((char *) ffname);
4997 #endif
4998 
4999     goto nofail;
5000 
5001     /*
5002      * Finish up.  We get here either after failure or success.
5003      */
5004 fail:
5005     --no_wait_return;		/* may wait for return now */
5006 nofail:
5007 
5008     /* Done saving, we accept changed buffer warnings again */
5009     buf->b_saving = FALSE;
5010 
5011     vim_free(backup);
5012     if (buffer != smallbuf)
5013 	vim_free(buffer);
5014 #ifdef FEAT_MBYTE
5015     vim_free(fenc_tofree);
5016     vim_free(write_info.bw_conv_buf);
5017 # ifdef USE_ICONV
5018     if (write_info.bw_iconv_fd != (iconv_t)-1)
5019     {
5020 	iconv_close(write_info.bw_iconv_fd);
5021 	write_info.bw_iconv_fd = (iconv_t)-1;
5022     }
5023 # endif
5024 #endif
5025 #ifdef HAVE_ACL
5026     mch_free_acl(acl);
5027 #endif
5028 
5029     if (errmsg != NULL)
5030     {
5031 	int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
5032 
5033 	attr = hl_attr(HLF_E);	/* set highlight for error messages */
5034 	msg_add_fname(buf,
5035 #ifndef UNIX
5036 		sfname
5037 #else
5038 		fname
5039 #endif
5040 		     );		/* put file name in IObuff with quotes */
5041 	if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE)
5042 	    IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL;
5043 	/* If the error message has the form "is ...", put the error number in
5044 	 * front of the file name. */
5045 	if (errnum != NULL)
5046 	{
5047 	    STRMOVE(IObuff + numlen, IObuff);
5048 	    mch_memmove(IObuff, errnum, (size_t)numlen);
5049 	}
5050 	STRCAT(IObuff, errmsg);
5051 	emsg(IObuff);
5052 	if (errmsg_allocated)
5053 	    vim_free(errmsg);
5054 
5055 	retval = FAIL;
5056 	if (end == 0)
5057 	{
5058 	    MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"),
5059 		    attr | MSG_HIST);
5060 	    MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"),
5061 		    attr | MSG_HIST);
5062 
5063 	    /* Update the timestamp to avoid an "overwrite changed file"
5064 	     * prompt when writing again. */
5065 	    if (mch_stat((char *)fname, &st_old) >= 0)
5066 	    {
5067 		buf_store_time(buf, &st_old, fname);
5068 		buf->b_mtime_read = buf->b_mtime;
5069 	    }
5070 	}
5071     }
5072     msg_scroll = msg_save;
5073 
5074 #ifdef FEAT_PERSISTENT_UNDO
5075     /*
5076      * When writing the whole file and 'undofile' is set, also write the undo
5077      * file.
5078      */
5079     if (retval == OK && write_undo_file)
5080     {
5081 	char_u	    hash[UNDO_HASH_SIZE];
5082 
5083 	sha256_finish(&sha_ctx, hash);
5084 	u_write_undo(NULL, FALSE, buf, hash);
5085     }
5086 #endif
5087 
5088 #ifdef FEAT_AUTOCMD
5089 #ifdef FEAT_EVAL
5090     if (!should_abort(retval))
5091 #else
5092     if (!got_int)
5093 #endif
5094     {
5095 	aco_save_T	aco;
5096 
5097 	curbuf->b_no_eol_lnum = 0;  /* in case it was set by the previous read */
5098 
5099 	/*
5100 	 * Apply POST autocommands.
5101 	 * Careful: The autocommands may call buf_write() recursively!
5102 	 */
5103 	aucmd_prepbuf(&aco, buf);
5104 
5105 	if (append)
5106 	    apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
5107 							  FALSE, curbuf, eap);
5108 	else if (filtering)
5109 	    apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
5110 							  FALSE, curbuf, eap);
5111 	else if (reset_changed && whole)
5112 	    apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
5113 							  FALSE, curbuf, eap);
5114 	else
5115 	    apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
5116 							  FALSE, curbuf, eap);
5117 
5118 	/* restore curwin/curbuf and a few other things */
5119 	aucmd_restbuf(&aco);
5120 
5121 #ifdef FEAT_EVAL
5122 	if (aborting())	    /* autocmds may abort script processing */
5123 	    retval = FALSE;
5124 #endif
5125     }
5126 #endif
5127 
5128     got_int |= prev_got_int;
5129 
5130 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */
5131     /* Update machine specific information. */
5132     mch_post_buffer_write(buf);
5133 #endif
5134     return retval;
5135 }
5136 
5137 /*
5138  * Set the name of the current buffer.  Use when the buffer doesn't have a
5139  * name and a ":r" or ":w" command with a file name is used.
5140  */
5141     static int
5142 set_rw_fname(fname, sfname)
5143     char_u	*fname;
5144     char_u	*sfname;
5145 {
5146 #ifdef FEAT_AUTOCMD
5147     buf_T	*buf = curbuf;
5148 
5149     /* It's like the unnamed buffer is deleted.... */
5150     if (curbuf->b_p_bl)
5151 	apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5152     apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
5153 # ifdef FEAT_EVAL
5154     if (aborting())	    /* autocmds may abort script processing */
5155 	return FAIL;
5156 # endif
5157     if (curbuf != buf)
5158     {
5159 	/* We are in another buffer now, don't do the renaming. */
5160 	EMSG(_(e_auchangedbuf));
5161 	return FAIL;
5162     }
5163 #endif
5164 
5165     if (setfname(curbuf, fname, sfname, FALSE) == OK)
5166 	curbuf->b_flags |= BF_NOTEDITED;
5167 
5168 #ifdef FEAT_AUTOCMD
5169     /* ....and a new named one is created */
5170     apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
5171     if (curbuf->b_p_bl)
5172 	apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5173 # ifdef FEAT_EVAL
5174     if (aborting())	    /* autocmds may abort script processing */
5175 	return FAIL;
5176 # endif
5177 
5178     /* Do filetype detection now if 'filetype' is empty. */
5179     if (*curbuf->b_p_ft == NUL)
5180     {
5181 	if (au_has_group((char_u *)"filetypedetect"))
5182 	    (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE);
5183 	do_modelines(0);
5184     }
5185 #endif
5186 
5187     return OK;
5188 }
5189 
5190 /*
5191  * Put file name into IObuff with quotes.
5192  */
5193     void
5194 msg_add_fname(buf, fname)
5195     buf_T	*buf;
5196     char_u	*fname;
5197 {
5198     if (fname == NULL)
5199 	fname = (char_u *)"-stdin-";
5200     home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
5201     IObuff[0] = '"';
5202     STRCAT(IObuff, "\" ");
5203 }
5204 
5205 /*
5206  * Append message for text mode to IObuff.
5207  * Return TRUE if something appended.
5208  */
5209     static int
5210 msg_add_fileformat(eol_type)
5211     int	    eol_type;
5212 {
5213 #ifndef USE_CRNL
5214     if (eol_type == EOL_DOS)
5215     {
5216 	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
5217 	return TRUE;
5218     }
5219 #endif
5220 #ifndef USE_CR
5221     if (eol_type == EOL_MAC)
5222     {
5223 	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
5224 	return TRUE;
5225     }
5226 #endif
5227 #if defined(USE_CRNL) || defined(USE_CR)
5228     if (eol_type == EOL_UNIX)
5229     {
5230 	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
5231 	return TRUE;
5232     }
5233 #endif
5234     return FALSE;
5235 }
5236 
5237 /*
5238  * Append line and character count to IObuff.
5239  */
5240     void
5241 msg_add_lines(insert_space, lnum, nchars)
5242     int	    insert_space;
5243     long    lnum;
5244     off_t   nchars;
5245 {
5246     char_u  *p;
5247 
5248     p = IObuff + STRLEN(IObuff);
5249 
5250     if (insert_space)
5251 	*p++ = ' ';
5252     if (shortmess(SHM_LINES))
5253 	sprintf((char *)p,
5254 #ifdef LONG_LONG_OFF_T
5255 		"%ldL, %lldC", lnum, nchars
5256 #else
5257 		/* Explicit typecast avoids warning on Mac OS X 10.6 */
5258 		"%ldL, %ldC", lnum, (long)nchars
5259 #endif
5260 		);
5261     else
5262     {
5263 	if (lnum == 1)
5264 	    STRCPY(p, _("1 line, "));
5265 	else
5266 	    sprintf((char *)p, _("%ld lines, "), lnum);
5267 	p += STRLEN(p);
5268 	if (nchars == 1)
5269 	    STRCPY(p, _("1 character"));
5270 	else
5271 	    sprintf((char *)p,
5272 #ifdef LONG_LONG_OFF_T
5273 		    _("%lld characters"), nchars
5274 #else
5275 		    /* Explicit typecast avoids warning on Mac OS X 10.6 */
5276 		    _("%ld characters"), (long)nchars
5277 #endif
5278 		    );
5279     }
5280 }
5281 
5282 /*
5283  * Append message for missing line separator to IObuff.
5284  */
5285     static void
5286 msg_add_eol()
5287 {
5288     STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
5289 }
5290 
5291 /*
5292  * Check modification time of file, before writing to it.
5293  * The size isn't checked, because using a tool like "gzip" takes care of
5294  * using the same timestamp but can't set the size.
5295  */
5296     static int
5297 check_mtime(buf, st)
5298     buf_T		*buf;
5299     struct stat		*st;
5300 {
5301     if (buf->b_mtime_read != 0
5302 	    && time_differs((long)st->st_mtime, buf->b_mtime_read))
5303     {
5304 	msg_scroll = TRUE;	    /* don't overwrite messages here */
5305 	msg_silent = 0;		    /* must give this prompt */
5306 	/* don't use emsg() here, don't want to flush the buffers */
5307 	MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"),
5308 						       hl_attr(HLF_E));
5309 	if (ask_yesno((char_u *)_("Do you really want to write to it"),
5310 								 TRUE) == 'n')
5311 	    return FAIL;
5312 	msg_scroll = FALSE;	    /* always overwrite the file message now */
5313     }
5314     return OK;
5315 }
5316 
5317     static int
5318 time_differs(t1, t2)
5319     long	t1, t2;
5320 {
5321 #if defined(__linux__) || defined(MSDOS) || defined(MSWIN)
5322     /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store
5323      * the seconds.  Since the roundoff is done when flushing the inode, the
5324      * time may change unexpectedly by one second!!! */
5325     return (t1 - t2 > 1 || t2 - t1 > 1);
5326 #else
5327     return (t1 != t2);
5328 #endif
5329 }
5330 
5331 /*
5332  * Call write() to write a number of bytes to the file.
5333  * Handles encryption and 'encoding' conversion.
5334  *
5335  * Return FAIL for failure, OK otherwise.
5336  */
5337     static int
5338 buf_write_bytes(ip)
5339     struct bw_info *ip;
5340 {
5341     int		wlen;
5342     char_u	*buf = ip->bw_buf;	/* data to write */
5343     int		len = ip->bw_len;	/* length of data */
5344 #ifdef HAS_BW_FLAGS
5345     int		flags = ip->bw_flags;	/* extra flags */
5346 #endif
5347 
5348 #ifdef FEAT_MBYTE
5349     /*
5350      * Skip conversion when writing the crypt magic number or the BOM.
5351      */
5352     if (!(flags & FIO_NOCONVERT))
5353     {
5354 	char_u		*p;
5355 	unsigned	c;
5356 	int		n;
5357 
5358 	if (flags & FIO_UTF8)
5359 	{
5360 	    /*
5361 	     * Convert latin1 in the buffer to UTF-8 in the file.
5362 	     */
5363 	    p = ip->bw_conv_buf;	/* translate to buffer */
5364 	    for (wlen = 0; wlen < len; ++wlen)
5365 		p += utf_char2bytes(buf[wlen], p);
5366 	    buf = ip->bw_conv_buf;
5367 	    len = (int)(p - ip->bw_conv_buf);
5368 	}
5369 	else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1))
5370 	{
5371 	    /*
5372 	     * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or
5373 	     * Latin1 chars in the file.
5374 	     */
5375 	    if (flags & FIO_LATIN1)
5376 		p = buf;	/* translate in-place (can only get shorter) */
5377 	    else
5378 		p = ip->bw_conv_buf;	/* translate to buffer */
5379 	    for (wlen = 0; wlen < len; wlen += n)
5380 	    {
5381 		if (wlen == 0 && ip->bw_restlen != 0)
5382 		{
5383 		    int		l;
5384 
5385 		    /* Use remainder of previous call.  Append the start of
5386 		     * buf[] to get a full sequence.  Might still be too
5387 		     * short! */
5388 		    l = CONV_RESTLEN - ip->bw_restlen;
5389 		    if (l > len)
5390 			l = len;
5391 		    mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
5392 		    n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l);
5393 		    if (n > ip->bw_restlen + len)
5394 		    {
5395 			/* We have an incomplete byte sequence at the end to
5396 			 * be written.  We can't convert it without the
5397 			 * remaining bytes.  Keep them for the next call. */
5398 			if (ip->bw_restlen + len > CONV_RESTLEN)
5399 			    return FAIL;
5400 			ip->bw_restlen += len;
5401 			break;
5402 		    }
5403 		    if (n > 1)
5404 			c = utf_ptr2char(ip->bw_rest);
5405 		    else
5406 			c = ip->bw_rest[0];
5407 		    if (n >= ip->bw_restlen)
5408 		    {
5409 			n -= ip->bw_restlen;
5410 			ip->bw_restlen = 0;
5411 		    }
5412 		    else
5413 		    {
5414 			ip->bw_restlen -= n;
5415 			mch_memmove(ip->bw_rest, ip->bw_rest + n,
5416 						      (size_t)ip->bw_restlen);
5417 			n = 0;
5418 		    }
5419 		}
5420 		else
5421 		{
5422 		    n = utf_ptr2len_len(buf + wlen, len - wlen);
5423 		    if (n > len - wlen)
5424 		    {
5425 			/* We have an incomplete byte sequence at the end to
5426 			 * be written.  We can't convert it without the
5427 			 * remaining bytes.  Keep them for the next call. */
5428 			if (len - wlen > CONV_RESTLEN)
5429 			    return FAIL;
5430 			ip->bw_restlen = len - wlen;
5431 			mch_memmove(ip->bw_rest, buf + wlen,
5432 						      (size_t)ip->bw_restlen);
5433 			break;
5434 		    }
5435 		    if (n > 1)
5436 			c = utf_ptr2char(buf + wlen);
5437 		    else
5438 			c = buf[wlen];
5439 		}
5440 
5441 		if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error)
5442 		{
5443 		    ip->bw_conv_error = TRUE;
5444 		    ip->bw_conv_error_lnum = ip->bw_start_lnum;
5445 		}
5446 		if (c == NL)
5447 		    ++ip->bw_start_lnum;
5448 	    }
5449 	    if (flags & FIO_LATIN1)
5450 		len = (int)(p - buf);
5451 	    else
5452 	    {
5453 		buf = ip->bw_conv_buf;
5454 		len = (int)(p - ip->bw_conv_buf);
5455 	    }
5456 	}
5457 
5458 # ifdef WIN3264
5459 	else if (flags & FIO_CODEPAGE)
5460 	{
5461 	    /*
5462 	     * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows
5463 	     * codepage.
5464 	     */
5465 	    char_u	*from;
5466 	    size_t	fromlen;
5467 	    char_u	*to;
5468 	    int		u8c;
5469 	    BOOL	bad = FALSE;
5470 	    int		needed;
5471 
5472 	    if (ip->bw_restlen > 0)
5473 	    {
5474 		/* Need to concatenate the remainder of the previous call and
5475 		 * the bytes of the current call.  Use the end of the
5476 		 * conversion buffer for this. */
5477 		fromlen = len + ip->bw_restlen;
5478 		from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5479 		mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5480 		mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5481 	    }
5482 	    else
5483 	    {
5484 		from = buf;
5485 		fromlen = len;
5486 	    }
5487 
5488 	    to = ip->bw_conv_buf;
5489 	    if (enc_utf8)
5490 	    {
5491 		/* Convert from UTF-8 to UCS-2, to the start of the buffer.
5492 		 * The buffer has been allocated to be big enough. */
5493 		while (fromlen > 0)
5494 		{
5495 		    n = (int)utf_ptr2len_len(from, (int)fromlen);
5496 		    if (n > (int)fromlen)	/* incomplete byte sequence */
5497 			break;
5498 		    u8c = utf_ptr2char(from);
5499 		    *to++ = (u8c & 0xff);
5500 		    *to++ = (u8c >> 8);
5501 		    fromlen -= n;
5502 		    from += n;
5503 		}
5504 
5505 		/* Copy remainder to ip->bw_rest[] to be used for the next
5506 		 * call. */
5507 		if (fromlen > CONV_RESTLEN)
5508 		{
5509 		    /* weird overlong sequence */
5510 		    ip->bw_conv_error = TRUE;
5511 		    return FAIL;
5512 		}
5513 		mch_memmove(ip->bw_rest, from, fromlen);
5514 		ip->bw_restlen = (int)fromlen;
5515 	    }
5516 	    else
5517 	    {
5518 		/* Convert from enc_codepage to UCS-2, to the start of the
5519 		 * buffer.  The buffer has been allocated to be big enough. */
5520 		ip->bw_restlen = 0;
5521 		needed = MultiByteToWideChar(enc_codepage,
5522 			     MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
5523 								     NULL, 0);
5524 		if (needed == 0)
5525 		{
5526 		    /* When conversion fails there may be a trailing byte. */
5527 		    needed = MultiByteToWideChar(enc_codepage,
5528 			 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
5529 								     NULL, 0);
5530 		    if (needed == 0)
5531 		    {
5532 			/* Conversion doesn't work. */
5533 			ip->bw_conv_error = TRUE;
5534 			return FAIL;
5535 		    }
5536 		    /* Save the trailing byte for the next call. */
5537 		    ip->bw_rest[0] = from[fromlen - 1];
5538 		    ip->bw_restlen = 1;
5539 		}
5540 		needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
5541 				(LPCSTR)from, (int)(fromlen - ip->bw_restlen),
5542 							  (LPWSTR)to, needed);
5543 		if (needed == 0)
5544 		{
5545 		    /* Safety check: Conversion doesn't work. */
5546 		    ip->bw_conv_error = TRUE;
5547 		    return FAIL;
5548 		}
5549 		to += needed * 2;
5550 	    }
5551 
5552 	    fromlen = to - ip->bw_conv_buf;
5553 	    buf = to;
5554 #  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
5555 	    if (FIO_GET_CP(flags) == CP_UTF8)
5556 	    {
5557 		/* Convert from UCS-2 to UTF-8, using the remainder of the
5558 		 * conversion buffer.  Fails when out of space. */
5559 		for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2)
5560 		{
5561 		    u8c = *from++;
5562 		    u8c += (*from++ << 8);
5563 		    to += utf_char2bytes(u8c, to);
5564 		    if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen)
5565 		    {
5566 			ip->bw_conv_error = TRUE;
5567 			return FAIL;
5568 		    }
5569 		}
5570 		len = (int)(to - buf);
5571 	    }
5572 	    else
5573 #endif
5574 	    {
5575 		/* Convert from UCS-2 to the codepage, using the remainder of
5576 		 * the conversion buffer.  If the conversion uses the default
5577 		 * character "0", the data doesn't fit in this encoding, so
5578 		 * fail. */
5579 		len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
5580 			(LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
5581 			(LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0,
5582 									&bad);
5583 		if (bad)
5584 		{
5585 		    ip->bw_conv_error = TRUE;
5586 		    return FAIL;
5587 		}
5588 	    }
5589 	}
5590 # endif
5591 
5592 # ifdef MACOS_CONVERT
5593 	else if (flags & FIO_MACROMAN)
5594 	{
5595 	    /*
5596 	     * Convert UTF-8 or latin1 to Apple MacRoman.
5597 	     */
5598 	    char_u	*from;
5599 	    size_t	fromlen;
5600 
5601 	    if (ip->bw_restlen > 0)
5602 	    {
5603 		/* Need to concatenate the remainder of the previous call and
5604 		 * the bytes of the current call.  Use the end of the
5605 		 * conversion buffer for this. */
5606 		fromlen = len + ip->bw_restlen;
5607 		from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5608 		mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5609 		mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5610 	    }
5611 	    else
5612 	    {
5613 		from = buf;
5614 		fromlen = len;
5615 	    }
5616 
5617 	    if (enc2macroman(from, fromlen,
5618 			ip->bw_conv_buf, &len, ip->bw_conv_buflen,
5619 			ip->bw_rest, &ip->bw_restlen) == FAIL)
5620 	    {
5621 		ip->bw_conv_error = TRUE;
5622 		return FAIL;
5623 	    }
5624 	    buf = ip->bw_conv_buf;
5625 	}
5626 # endif
5627 
5628 # ifdef USE_ICONV
5629 	if (ip->bw_iconv_fd != (iconv_t)-1)
5630 	{
5631 	    const char	*from;
5632 	    size_t	fromlen;
5633 	    char	*to;
5634 	    size_t	tolen;
5635 
5636 	    /* Convert with iconv(). */
5637 	    if (ip->bw_restlen > 0)
5638 	    {
5639 		char *fp;
5640 
5641 		/* Need to concatenate the remainder of the previous call and
5642 		 * the bytes of the current call.  Use the end of the
5643 		 * conversion buffer for this. */
5644 		fromlen = len + ip->bw_restlen;
5645 		fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5646 		mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen);
5647 		mch_memmove(fp + ip->bw_restlen, buf, (size_t)len);
5648 		from = fp;
5649 		tolen = ip->bw_conv_buflen - fromlen;
5650 	    }
5651 	    else
5652 	    {
5653 		from = (const char *)buf;
5654 		fromlen = len;
5655 		tolen = ip->bw_conv_buflen;
5656 	    }
5657 	    to = (char *)ip->bw_conv_buf;
5658 
5659 	    if (ip->bw_first)
5660 	    {
5661 		size_t	save_len = tolen;
5662 
5663 		/* output the initial shift state sequence */
5664 		(void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen);
5665 
5666 		/* There is a bug in iconv() on Linux (which appears to be
5667 		 * wide-spread) which sets "to" to NULL and messes up "tolen".
5668 		 */
5669 		if (to == NULL)
5670 		{
5671 		    to = (char *)ip->bw_conv_buf;
5672 		    tolen = save_len;
5673 		}
5674 		ip->bw_first = FALSE;
5675 	    }
5676 
5677 	    /*
5678 	     * If iconv() has an error or there is not enough room, fail.
5679 	     */
5680 	    if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen)
5681 			== (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
5682 						    || fromlen > CONV_RESTLEN)
5683 	    {
5684 		ip->bw_conv_error = TRUE;
5685 		return FAIL;
5686 	    }
5687 
5688 	    /* copy remainder to ip->bw_rest[] to be used for the next call. */
5689 	    if (fromlen > 0)
5690 		mch_memmove(ip->bw_rest, (void *)from, fromlen);
5691 	    ip->bw_restlen = (int)fromlen;
5692 
5693 	    buf = ip->bw_conv_buf;
5694 	    len = (int)((char_u *)to - ip->bw_conv_buf);
5695 	}
5696 # endif
5697     }
5698 #endif /* FEAT_MBYTE */
5699 
5700 #ifdef FEAT_CRYPT
5701     if (flags & FIO_ENCRYPTED)		/* encrypt the data */
5702 	crypt_encode(buf, len, buf);
5703 #endif
5704 
5705     wlen = write_eintr(ip->bw_fd, buf, len);
5706     return (wlen < len) ? FAIL : OK;
5707 }
5708 
5709 #ifdef FEAT_MBYTE
5710 /*
5711  * Convert a Unicode character to bytes.
5712  * Return TRUE for an error, FALSE when it's OK.
5713  */
5714     static int
5715 ucs2bytes(c, pp, flags)
5716     unsigned	c;		/* in: character */
5717     char_u	**pp;		/* in/out: pointer to result */
5718     int		flags;		/* FIO_ flags */
5719 {
5720     char_u	*p = *pp;
5721     int		error = FALSE;
5722     int		cc;
5723 
5724 
5725     if (flags & FIO_UCS4)
5726     {
5727 	if (flags & FIO_ENDIAN_L)
5728 	{
5729 	    *p++ = c;
5730 	    *p++ = (c >> 8);
5731 	    *p++ = (c >> 16);
5732 	    *p++ = (c >> 24);
5733 	}
5734 	else
5735 	{
5736 	    *p++ = (c >> 24);
5737 	    *p++ = (c >> 16);
5738 	    *p++ = (c >> 8);
5739 	    *p++ = c;
5740 	}
5741     }
5742     else if (flags & (FIO_UCS2 | FIO_UTF16))
5743     {
5744 	if (c >= 0x10000)
5745 	{
5746 	    if (flags & FIO_UTF16)
5747 	    {
5748 		/* Make two words, ten bits of the character in each.  First
5749 		 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */
5750 		c -= 0x10000;
5751 		if (c >= 0x100000)
5752 		    error = TRUE;
5753 		cc = ((c >> 10) & 0x3ff) + 0xd800;
5754 		if (flags & FIO_ENDIAN_L)
5755 		{
5756 		    *p++ = cc;
5757 		    *p++ = ((unsigned)cc >> 8);
5758 		}
5759 		else
5760 		{
5761 		    *p++ = ((unsigned)cc >> 8);
5762 		    *p++ = cc;
5763 		}
5764 		c = (c & 0x3ff) + 0xdc00;
5765 	    }
5766 	    else
5767 		error = TRUE;
5768 	}
5769 	if (flags & FIO_ENDIAN_L)
5770 	{
5771 	    *p++ = c;
5772 	    *p++ = (c >> 8);
5773 	}
5774 	else
5775 	{
5776 	    *p++ = (c >> 8);
5777 	    *p++ = c;
5778 	}
5779     }
5780     else    /* Latin1 */
5781     {
5782 	if (c >= 0x100)
5783 	{
5784 	    error = TRUE;
5785 	    *p++ = 0xBF;
5786 	}
5787 	else
5788 	    *p++ = c;
5789     }
5790 
5791     *pp = p;
5792     return error;
5793 }
5794 
5795 /*
5796  * Return TRUE if file encoding "fenc" requires conversion from or to
5797  * 'encoding'.
5798  */
5799     static int
5800 need_conversion(fenc)
5801     char_u	*fenc;
5802 {
5803     int		same_encoding;
5804     int		enc_flags;
5805     int		fenc_flags;
5806 
5807     if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
5808     {
5809 	same_encoding = TRUE;
5810 	fenc_flags = 0;
5811     }
5812     else
5813     {
5814 	/* Ignore difference between "ansi" and "latin1", "ucs-4" and
5815 	 * "ucs-4be", etc. */
5816 	enc_flags = get_fio_flags(p_enc);
5817 	fenc_flags = get_fio_flags(fenc);
5818 	same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
5819     }
5820     if (same_encoding)
5821     {
5822 	/* Specified encoding matches with 'encoding'.  This requires
5823 	 * conversion when 'encoding' is Unicode but not UTF-8. */
5824 	return enc_unicode != 0;
5825     }
5826 
5827     /* Encodings differ.  However, conversion is not needed when 'enc' is any
5828      * Unicode encoding and the file is UTF-8. */
5829     return !(enc_utf8 && fenc_flags == FIO_UTF8);
5830 }
5831 
5832 /*
5833  * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
5834  * internal conversion.
5835  * if "ptr" is an empty string, use 'encoding'.
5836  */
5837     static int
5838 get_fio_flags(ptr)
5839     char_u	*ptr;
5840 {
5841     int		prop;
5842 
5843     if (*ptr == NUL)
5844 	ptr = p_enc;
5845 
5846     prop = enc_canon_props(ptr);
5847     if (prop & ENC_UNICODE)
5848     {
5849 	if (prop & ENC_2BYTE)
5850 	{
5851 	    if (prop & ENC_ENDIAN_L)
5852 		return FIO_UCS2 | FIO_ENDIAN_L;
5853 	    return FIO_UCS2;
5854 	}
5855 	if (prop & ENC_4BYTE)
5856 	{
5857 	    if (prop & ENC_ENDIAN_L)
5858 		return FIO_UCS4 | FIO_ENDIAN_L;
5859 	    return FIO_UCS4;
5860 	}
5861 	if (prop & ENC_2WORD)
5862 	{
5863 	    if (prop & ENC_ENDIAN_L)
5864 		return FIO_UTF16 | FIO_ENDIAN_L;
5865 	    return FIO_UTF16;
5866 	}
5867 	return FIO_UTF8;
5868     }
5869     if (prop & ENC_LATIN1)
5870 	return FIO_LATIN1;
5871     /* must be ENC_DBCS, requires iconv() */
5872     return 0;
5873 }
5874 
5875 #ifdef WIN3264
5876 /*
5877  * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
5878  * for the conversion MS-Windows can do for us.  Also accept "utf-8".
5879  * Used for conversion between 'encoding' and 'fileencoding'.
5880  */
5881     static int
5882 get_win_fio_flags(ptr)
5883     char_u	*ptr;
5884 {
5885     int		cp;
5886 
5887     /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */
5888     if (!enc_utf8 && enc_codepage <= 0)
5889 	return 0;
5890 
5891     cp = encname2codepage(ptr);
5892     if (cp == 0)
5893     {
5894 #  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
5895 	if (STRCMP(ptr, "utf-8") == 0)
5896 	    cp = CP_UTF8;
5897 	else
5898 #  endif
5899 	    return 0;
5900     }
5901     return FIO_PUT_CP(cp) | FIO_CODEPAGE;
5902 }
5903 #endif
5904 
5905 #ifdef MACOS_X
5906 /*
5907  * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
5908  * needed for the internal conversion to/from utf-8 or latin1.
5909  */
5910     static int
5911 get_mac_fio_flags(ptr)
5912     char_u	*ptr;
5913 {
5914     if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
5915 				     && (enc_canon_props(ptr) & ENC_MACROMAN))
5916 	return FIO_MACROMAN;
5917     return 0;
5918 }
5919 #endif
5920 
5921 /*
5922  * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
5923  * "size" must be at least 2.
5924  * Return the name of the encoding and set "*lenp" to the length.
5925  * Returns NULL when no BOM found.
5926  */
5927     static char_u *
5928 check_for_bom(p, size, lenp, flags)
5929     char_u	*p;
5930     long	size;
5931     int		*lenp;
5932     int		flags;
5933 {
5934     char	*name = NULL;
5935     int		len = 2;
5936 
5937     if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
5938 	    && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
5939     {
5940 	name = "utf-8";		/* EF BB BF */
5941 	len = 3;
5942     }
5943     else if (p[0] == 0xff && p[1] == 0xfe)
5944     {
5945 	if (size >= 4 && p[2] == 0 && p[3] == 0
5946 	    && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
5947 	{
5948 	    name = "ucs-4le";	/* FF FE 00 00 */
5949 	    len = 4;
5950 	}
5951 	else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
5952 	    name = "ucs-2le";	/* FF FE */
5953 	else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
5954 	    /* utf-16le is preferred, it also works for ucs-2le text */
5955 	    name = "utf-16le";	/* FF FE */
5956     }
5957     else if (p[0] == 0xfe && p[1] == 0xff
5958 	    && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
5959     {
5960 	/* Default to utf-16, it works also for ucs-2 text. */
5961 	if (flags == FIO_UCS2)
5962 	    name = "ucs-2";	/* FE FF */
5963 	else
5964 	    name = "utf-16";	/* FE FF */
5965     }
5966     else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
5967 	    && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
5968     {
5969 	name = "ucs-4";		/* 00 00 FE FF */
5970 	len = 4;
5971     }
5972 
5973     *lenp = len;
5974     return (char_u *)name;
5975 }
5976 
5977 /*
5978  * Generate a BOM in "buf[4]" for encoding "name".
5979  * Return the length of the BOM (zero when no BOM).
5980  */
5981     static int
5982 make_bom(buf, name)
5983     char_u	*buf;
5984     char_u	*name;
5985 {
5986     int		flags;
5987     char_u	*p;
5988 
5989     flags = get_fio_flags(name);
5990 
5991     /* Can't put a BOM in a non-Unicode file. */
5992     if (flags == FIO_LATIN1 || flags == 0)
5993 	return 0;
5994 
5995     if (flags == FIO_UTF8)	/* UTF-8 */
5996     {
5997 	buf[0] = 0xef;
5998 	buf[1] = 0xbb;
5999 	buf[2] = 0xbf;
6000 	return 3;
6001     }
6002     p = buf;
6003     (void)ucs2bytes(0xfeff, &p, flags);
6004     return (int)(p - buf);
6005 }
6006 #endif
6007 
6008 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \
6009     defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO)
6010 /*
6011  * Try to find a shortname by comparing the fullname with the current
6012  * directory.
6013  * Returns "full_path" or pointer into "full_path" if shortened.
6014  */
6015     char_u *
6016 shorten_fname1(full_path)
6017     char_u	*full_path;
6018 {
6019     char_u	*dirname;
6020     char_u	*p = full_path;
6021 
6022     dirname = alloc(MAXPATHL);
6023     if (dirname == NULL)
6024 	return full_path;
6025     if (mch_dirname(dirname, MAXPATHL) == OK)
6026     {
6027 	p = shorten_fname(full_path, dirname);
6028 	if (p == NULL || *p == NUL)
6029 	    p = full_path;
6030     }
6031     vim_free(dirname);
6032     return p;
6033 }
6034 #endif
6035 
6036 /*
6037  * Try to find a shortname by comparing the fullname with the current
6038  * directory.
6039  * Returns NULL if not shorter name possible, pointer into "full_path"
6040  * otherwise.
6041  */
6042     char_u *
6043 shorten_fname(full_path, dir_name)
6044     char_u	*full_path;
6045     char_u	*dir_name;
6046 {
6047     int		len;
6048     char_u	*p;
6049 
6050     if (full_path == NULL)
6051 	return NULL;
6052     len = (int)STRLEN(dir_name);
6053     if (fnamencmp(dir_name, full_path, len) == 0)
6054     {
6055 	p = full_path + len;
6056 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
6057 	/*
6058 	 * MSDOS: when a file is in the root directory, dir_name will end in a
6059 	 * slash, since C: by itself does not define a specific dir. In this
6060 	 * case p may already be correct. <negri>
6061 	 */
6062 	if (!((len > 2) && (*(p - 2) == ':')))
6063 #endif
6064 	{
6065 	    if (vim_ispathsep(*p))
6066 		++p;
6067 #ifndef VMS   /* the path separator is always part of the path */
6068 	    else
6069 		p = NULL;
6070 #endif
6071 	}
6072     }
6073 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
6074     /*
6075      * When using a file in the current drive, remove the drive name:
6076      * "A:\dir\file" -> "\dir\file".  This helps when moving a session file on
6077      * a floppy from "A:\dir" to "B:\dir".
6078      */
6079     else if (len > 3
6080 	    && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
6081 	    && full_path[1] == ':'
6082 	    && vim_ispathsep(full_path[2]))
6083 	p = full_path + 2;
6084 #endif
6085     else
6086 	p = NULL;
6087     return p;
6088 }
6089 
6090 /*
6091  * Shorten filenames for all buffers.
6092  * When "force" is TRUE: Use full path from now on for files currently being
6093  * edited, both for file name and swap file name.  Try to shorten the file
6094  * names a bit, if safe to do so.
6095  * When "force" is FALSE: Only try to shorten absolute file names.
6096  * For buffers that have buftype "nofile" or "scratch": never change the file
6097  * name.
6098  */
6099     void
6100 shorten_fnames(force)
6101     int		force;
6102 {
6103     char_u	dirname[MAXPATHL];
6104     buf_T	*buf;
6105     char_u	*p;
6106 
6107     mch_dirname(dirname, MAXPATHL);
6108     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6109     {
6110 	if (buf->b_fname != NULL
6111 #ifdef FEAT_QUICKFIX
6112 		&& !bt_nofile(buf)
6113 #endif
6114 		&& !path_with_url(buf->b_fname)
6115 		&& (force
6116 		    || buf->b_sfname == NULL
6117 		    || mch_isFullName(buf->b_sfname)))
6118 	{
6119 	    vim_free(buf->b_sfname);
6120 	    buf->b_sfname = NULL;
6121 	    p = shorten_fname(buf->b_ffname, dirname);
6122 	    if (p != NULL)
6123 	    {
6124 		buf->b_sfname = vim_strsave(p);
6125 		buf->b_fname = buf->b_sfname;
6126 	    }
6127 	    if (p == NULL || buf->b_fname == NULL)
6128 		buf->b_fname = buf->b_ffname;
6129 	}
6130 
6131 	/* Always make the swap file name a full path, a "nofile" buffer may
6132 	 * also have a swap file. */
6133 	mf_fullname(buf->b_ml.ml_mfp);
6134     }
6135 #ifdef FEAT_WINDOWS
6136     status_redraw_all();
6137     redraw_tabline = TRUE;
6138 #endif
6139 }
6140 
6141 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
6142 	|| defined(FEAT_GUI_MSWIN) \
6143 	|| defined(FEAT_GUI_MAC) \
6144 	|| defined(PROTO)
6145 /*
6146  * Shorten all filenames in "fnames[count]" by current directory.
6147  */
6148     void
6149 shorten_filenames(fnames, count)
6150     char_u	**fnames;
6151     int		count;
6152 {
6153     int		i;
6154     char_u	dirname[MAXPATHL];
6155     char_u	*p;
6156 
6157     if (fnames == NULL || count < 1)
6158 	return;
6159     mch_dirname(dirname, sizeof(dirname));
6160     for (i = 0; i < count; ++i)
6161     {
6162 	if ((p = shorten_fname(fnames[i], dirname)) != NULL)
6163 	{
6164 	    /* shorten_fname() returns pointer in given "fnames[i]".  If free
6165 	     * "fnames[i]" first, "p" becomes invalid.  So we need to copy
6166 	     * "p" first then free fnames[i]. */
6167 	    p = vim_strsave(p);
6168 	    vim_free(fnames[i]);
6169 	    fnames[i] = p;
6170 	}
6171     }
6172 }
6173 #endif
6174 
6175 /*
6176  * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
6177  * fo_o_h.ext for MSDOS or when shortname option set.
6178  *
6179  * Assumed that fname is a valid name found in the filesystem we assure that
6180  * the return value is a different name and ends in 'ext'.
6181  * "ext" MUST be at most 4 characters long if it starts with a dot, 3
6182  * characters otherwise.
6183  * Space for the returned name is allocated, must be freed later.
6184  * Returns NULL when out of memory.
6185  */
6186     char_u *
6187 modname(fname, ext, prepend_dot)
6188     char_u *fname, *ext;
6189     int	    prepend_dot;	/* may prepend a '.' to file name */
6190 {
6191     return buf_modname(
6192 #ifdef SHORT_FNAME
6193 			TRUE,
6194 #else
6195 			(curbuf->b_p_sn || curbuf->b_shortname),
6196 #endif
6197 						     fname, ext, prepend_dot);
6198 }
6199 
6200     char_u *
6201 buf_modname(shortname, fname, ext, prepend_dot)
6202     int	    shortname;		/* use 8.3 file name */
6203     char_u  *fname, *ext;
6204     int	    prepend_dot;	/* may prepend a '.' to file name */
6205 {
6206     char_u	*retval;
6207     char_u	*s;
6208     char_u	*e;
6209     char_u	*ptr;
6210     int		fnamelen, extlen;
6211 
6212     extlen = (int)STRLEN(ext);
6213 
6214     /*
6215      * If there is no file name we must get the name of the current directory
6216      * (we need the full path in case :cd is used).
6217      */
6218     if (fname == NULL || *fname == NUL)
6219     {
6220 	retval = alloc((unsigned)(MAXPATHL + extlen + 3));
6221 	if (retval == NULL)
6222 	    return NULL;
6223 	if (mch_dirname(retval, MAXPATHL) == FAIL ||
6224 				     (fnamelen = (int)STRLEN(retval)) == 0)
6225 	{
6226 	    vim_free(retval);
6227 	    return NULL;
6228 	}
6229 	if (!after_pathsep(retval, retval + fnamelen))
6230 	{
6231 	    retval[fnamelen++] = PATHSEP;
6232 	    retval[fnamelen] = NUL;
6233 	}
6234 #ifndef SHORT_FNAME
6235 	prepend_dot = FALSE;	    /* nothing to prepend a dot to */
6236 #endif
6237     }
6238     else
6239     {
6240 	fnamelen = (int)STRLEN(fname);
6241 	retval = alloc((unsigned)(fnamelen + extlen + 3));
6242 	if (retval == NULL)
6243 	    return NULL;
6244 	STRCPY(retval, fname);
6245 #ifdef VMS
6246 	vms_remove_version(retval); /* we do not need versions here */
6247 #endif
6248     }
6249 
6250     /*
6251      * search backwards until we hit a '/', '\' or ':' replacing all '.'
6252      * by '_' for MSDOS or when shortname option set and ext starts with a dot.
6253      * Then truncate what is after the '/', '\' or ':' to 8 characters for
6254      * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
6255      */
6256     for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr))
6257     {
6258 	if (*ext == '.'
6259 #ifdef USE_LONG_FNAME
6260 		    && (!USE_LONG_FNAME || shortname)
6261 #else
6262 # ifndef SHORT_FNAME
6263 		    && shortname
6264 # endif
6265 #endif
6266 								)
6267 	    if (*ptr == '.')	/* replace '.' by '_' */
6268 		*ptr = '_';
6269 	if (vim_ispathsep(*ptr))
6270 	{
6271 	    ++ptr;
6272 	    break;
6273 	}
6274     }
6275 
6276     /* the file name has at most BASENAMELEN characters. */
6277 #ifndef SHORT_FNAME
6278     if (STRLEN(ptr) > (unsigned)BASENAMELEN)
6279 	ptr[BASENAMELEN] = '\0';
6280 #endif
6281 
6282     s = ptr + STRLEN(ptr);
6283 
6284     /*
6285      * For 8.3 file names we may have to reduce the length.
6286      */
6287 #ifdef USE_LONG_FNAME
6288     if (!USE_LONG_FNAME || shortname)
6289 #else
6290 # ifndef SHORT_FNAME
6291     if (shortname)
6292 # endif
6293 #endif
6294     {
6295 	/*
6296 	 * If there is no file name, or the file name ends in '/', and the
6297 	 * extension starts with '.', put a '_' before the dot, because just
6298 	 * ".ext" is invalid.
6299 	 */
6300 	if (fname == NULL || *fname == NUL
6301 				   || vim_ispathsep(fname[STRLEN(fname) - 1]))
6302 	{
6303 	    if (*ext == '.')
6304 		*s++ = '_';
6305 	}
6306 	/*
6307 	 * If the extension starts with '.', truncate the base name at 8
6308 	 * characters
6309 	 */
6310 	else if (*ext == '.')
6311 	{
6312 	    if ((size_t)(s - ptr) > (size_t)8)
6313 	    {
6314 		s = ptr + 8;
6315 		*s = '\0';
6316 	    }
6317 	}
6318 	/*
6319 	 * If the extension doesn't start with '.', and the file name
6320 	 * doesn't have an extension yet, append a '.'
6321 	 */
6322 	else if ((e = vim_strchr(ptr, '.')) == NULL)
6323 	    *s++ = '.';
6324 	/*
6325 	 * If the extension doesn't start with '.', and there already is an
6326 	 * extension, it may need to be truncated
6327 	 */
6328 	else if ((int)STRLEN(e) + extlen > 4)
6329 	    s = e + 4 - extlen;
6330     }
6331 #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264)
6332     /*
6333      * If there is no file name, and the extension starts with '.', put a
6334      * '_' before the dot, because just ".ext" may be invalid if it's on a
6335      * FAT partition, and on HPFS it doesn't matter.
6336      */
6337     else if ((fname == NULL || *fname == NUL) && *ext == '.')
6338 	*s++ = '_';
6339 #endif
6340 
6341     /*
6342      * Append the extension.
6343      * ext can start with '.' and cannot exceed 3 more characters.
6344      */
6345     STRCPY(s, ext);
6346 
6347 #ifndef SHORT_FNAME
6348     /*
6349      * Prepend the dot.
6350      */
6351     if (prepend_dot && !shortname && *(e = gettail(retval)) != '.'
6352 #ifdef USE_LONG_FNAME
6353 	    && USE_LONG_FNAME
6354 #endif
6355 				)
6356     {
6357 	STRMOVE(e + 1, e);
6358 	*e = '.';
6359     }
6360 #endif
6361 
6362     /*
6363      * Check that, after appending the extension, the file name is really
6364      * different.
6365      */
6366     if (fname != NULL && STRCMP(fname, retval) == 0)
6367     {
6368 	/* we search for a character that can be replaced by '_' */
6369 	while (--s >= ptr)
6370 	{
6371 	    if (*s != '_')
6372 	    {
6373 		*s = '_';
6374 		break;
6375 	    }
6376 	}
6377 	if (s < ptr)	/* fname was "________.<ext>", how tricky! */
6378 	    *ptr = 'v';
6379     }
6380     return retval;
6381 }
6382 
6383 /*
6384  * Like fgets(), but if the file line is too long, it is truncated and the
6385  * rest of the line is thrown away.  Returns TRUE for end-of-file.
6386  */
6387     int
6388 vim_fgets(buf, size, fp)
6389     char_u	*buf;
6390     int		size;
6391     FILE	*fp;
6392 {
6393     char	*eof;
6394 #define FGETS_SIZE 200
6395     char	tbuf[FGETS_SIZE];
6396 
6397     buf[size - 2] = NUL;
6398 #ifdef USE_CR
6399     eof = fgets_cr((char *)buf, size, fp);
6400 #else
6401     eof = fgets((char *)buf, size, fp);
6402 #endif
6403     if (buf[size - 2] != NUL && buf[size - 2] != '\n')
6404     {
6405 	buf[size - 1] = NUL;	    /* Truncate the line */
6406 
6407 	/* Now throw away the rest of the line: */
6408 	do
6409 	{
6410 	    tbuf[FGETS_SIZE - 2] = NUL;
6411 #ifdef USE_CR
6412 	    ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp);
6413 #else
6414 	    ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
6415 #endif
6416 	} while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
6417     }
6418     return (eof == NULL);
6419 }
6420 
6421 #if defined(USE_CR) || defined(PROTO)
6422 /*
6423  * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF.
6424  * Returns TRUE for end-of-file.
6425  * Only used for the Mac, because it's much slower than vim_fgets().
6426  */
6427     int
6428 tag_fgets(buf, size, fp)
6429     char_u	*buf;
6430     int		size;
6431     FILE	*fp;
6432 {
6433     int		i = 0;
6434     int		c;
6435     int		eof = FALSE;
6436 
6437     for (;;)
6438     {
6439 	c = fgetc(fp);
6440 	if (c == EOF)
6441 	{
6442 	    eof = TRUE;
6443 	    break;
6444 	}
6445 	if (c == '\r')
6446 	{
6447 	    /* Always store a NL for end-of-line. */
6448 	    if (i < size - 1)
6449 		buf[i++] = '\n';
6450 	    c = fgetc(fp);
6451 	    if (c != '\n')	/* Macintosh format: single CR. */
6452 		ungetc(c, fp);
6453 	    break;
6454 	}
6455 	if (i < size - 1)
6456 	    buf[i++] = c;
6457 	if (c == '\n')
6458 	    break;
6459     }
6460     buf[i] = NUL;
6461     return eof;
6462 }
6463 #endif
6464 
6465 /*
6466  * rename() only works if both files are on the same file system, this
6467  * function will (attempts to?) copy the file across if rename fails -- webb
6468  * Return -1 for failure, 0 for success.
6469  */
6470     int
6471 vim_rename(from, to)
6472     char_u	*from;
6473     char_u	*to;
6474 {
6475     int		fd_in;
6476     int		fd_out;
6477     int		n;
6478     char	*errmsg = NULL;
6479     char	*buffer;
6480 #ifdef AMIGA
6481     BPTR	flock;
6482 #endif
6483     struct stat	st;
6484     long	perm;
6485 #ifdef HAVE_ACL
6486     vim_acl_T	acl;		/* ACL from original file */
6487 #endif
6488 #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
6489     int		use_tmp_file = FALSE;
6490 #endif
6491 
6492     /*
6493      * When the names are identical, there is nothing to do.  When they refer
6494      * to the same file (ignoring case and slash/backslash differences) but
6495      * the file name differs we need to go through a temp file.
6496      */
6497     if (fnamecmp(from, to) == 0)
6498     {
6499 #ifdef CASE_INSENSITIVE_FILENAME
6500 	if (STRCMP(gettail(from), gettail(to)) != 0)
6501 	    use_tmp_file = TRUE;
6502 	else
6503 #endif
6504 	    return 0;
6505     }
6506 
6507     /*
6508      * Fail if the "from" file doesn't exist.  Avoids that "to" is deleted.
6509      */
6510     if (mch_stat((char *)from, &st) < 0)
6511 	return -1;
6512 
6513 #ifdef UNIX
6514     {
6515 	struct stat	st_to;
6516 
6517 	/* It's possible for the source and destination to be the same file.
6518 	 * This happens when "from" and "to" differ in case and are on a FAT32
6519 	 * filesystem.  In that case go through a temp file name. */
6520 	if (mch_stat((char *)to, &st_to) >= 0
6521 		&& st.st_dev == st_to.st_dev
6522 		&& st.st_ino == st_to.st_ino)
6523 	    use_tmp_file = TRUE;
6524     }
6525 #endif
6526 #ifdef WIN3264
6527     {
6528 	BY_HANDLE_FILE_INFORMATION info1, info2;
6529 
6530 	/* It's possible for the source and destination to be the same file.
6531 	 * In that case go through a temp file name.  This makes rename("foo",
6532 	 * "./foo") a no-op (in a complicated way). */
6533 	if (win32_fileinfo(from, &info1) == FILEINFO_OK
6534 		&& win32_fileinfo(to, &info2) == FILEINFO_OK
6535 		&& info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
6536 		&& info1.nFileIndexHigh == info2.nFileIndexHigh
6537 		&& info1.nFileIndexLow == info2.nFileIndexLow)
6538 	    use_tmp_file = TRUE;
6539     }
6540 #endif
6541 
6542 #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
6543     if (use_tmp_file)
6544     {
6545 	char	tempname[MAXPATHL + 1];
6546 
6547 	/*
6548 	 * Find a name that doesn't exist and is in the same directory.
6549 	 * Rename "from" to "tempname" and then rename "tempname" to "to".
6550 	 */
6551 	if (STRLEN(from) >= MAXPATHL - 5)
6552 	    return -1;
6553 	STRCPY(tempname, from);
6554 	for (n = 123; n < 99999; ++n)
6555 	{
6556 	    sprintf((char *)gettail((char_u *)tempname), "%d", n);
6557 	    if (mch_stat(tempname, &st) < 0)
6558 	    {
6559 		if (mch_rename((char *)from, tempname) == 0)
6560 		{
6561 		    if (mch_rename(tempname, (char *)to) == 0)
6562 			return 0;
6563 		    /* Strange, the second step failed.  Try moving the
6564 		     * file back and return failure. */
6565 		    mch_rename(tempname, (char *)from);
6566 		    return -1;
6567 		}
6568 		/* If it fails for one temp name it will most likely fail
6569 		 * for any temp name, give up. */
6570 		return -1;
6571 	    }
6572 	}
6573 	return -1;
6574     }
6575 #endif
6576 
6577     /*
6578      * Delete the "to" file, this is required on some systems to make the
6579      * mch_rename() work, on other systems it makes sure that we don't have
6580      * two files when the mch_rename() fails.
6581      */
6582 
6583 #ifdef AMIGA
6584     /*
6585      * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
6586      * that the name of the "to" file is the same as the "from" file, even
6587      * though the names are different. To avoid the chance of accidentally
6588      * deleting the "from" file (horror!) we lock it during the remove.
6589      *
6590      * When used for making a backup before writing the file: This should not
6591      * happen with ":w", because startscript() should detect this problem and
6592      * set buf->b_shortname, causing modname() to return a correct ".bak" file
6593      * name.  This problem does exist with ":w filename", but then the
6594      * original file will be somewhere else so the backup isn't really
6595      * important. If autoscripting is off the rename may fail.
6596      */
6597     flock = Lock((UBYTE *)from, (long)ACCESS_READ);
6598 #endif
6599     mch_remove(to);
6600 #ifdef AMIGA
6601     if (flock)
6602 	UnLock(flock);
6603 #endif
6604 
6605     /*
6606      * First try a normal rename, return if it works.
6607      */
6608     if (mch_rename((char *)from, (char *)to) == 0)
6609 	return 0;
6610 
6611     /*
6612      * Rename() failed, try copying the file.
6613      */
6614     perm = mch_getperm(from);
6615 #ifdef HAVE_ACL
6616     /* For systems that support ACL: get the ACL from the original file. */
6617     acl = mch_get_acl(from);
6618 #endif
6619     fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
6620     if (fd_in == -1)
6621     {
6622 #ifdef HAVE_ACL
6623 	mch_free_acl(acl);
6624 #endif
6625 	return -1;
6626     }
6627 
6628     /* Create the new file with same permissions as the original. */
6629     fd_out = mch_open((char *)to,
6630 		       O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
6631     if (fd_out == -1)
6632     {
6633 	close(fd_in);
6634 #ifdef HAVE_ACL
6635 	mch_free_acl(acl);
6636 #endif
6637 	return -1;
6638     }
6639 
6640     buffer = (char *)alloc(BUFSIZE);
6641     if (buffer == NULL)
6642     {
6643 	close(fd_out);
6644 	close(fd_in);
6645 #ifdef HAVE_ACL
6646 	mch_free_acl(acl);
6647 #endif
6648 	return -1;
6649     }
6650 
6651     while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0)
6652 	if (write_eintr(fd_out, buffer, n) != n)
6653 	{
6654 	    errmsg = _("E208: Error writing to \"%s\"");
6655 	    break;
6656 	}
6657 
6658     vim_free(buffer);
6659     close(fd_in);
6660     if (close(fd_out) < 0)
6661 	errmsg = _("E209: Error closing \"%s\"");
6662     if (n < 0)
6663     {
6664 	errmsg = _("E210: Error reading \"%s\"");
6665 	to = from;
6666     }
6667 #ifndef UNIX	    /* for Unix mch_open() already set the permission */
6668     mch_setperm(to, perm);
6669 #endif
6670 #ifdef HAVE_ACL
6671     mch_set_acl(to, acl);
6672     mch_free_acl(acl);
6673 #endif
6674     if (errmsg != NULL)
6675     {
6676 	EMSG2(errmsg, to);
6677 	return -1;
6678     }
6679     mch_remove(from);
6680     return 0;
6681 }
6682 
6683 static int already_warned = FALSE;
6684 
6685 /*
6686  * Check if any not hidden buffer has been changed.
6687  * Postpone the check if there are characters in the stuff buffer, a global
6688  * command is being executed, a mapping is being executed or an autocommand is
6689  * busy.
6690  * Returns TRUE if some message was written (screen should be redrawn and
6691  * cursor positioned).
6692  */
6693     int
6694 check_timestamps(focus)
6695     int		focus;		/* called for GUI focus event */
6696 {
6697     buf_T	*buf;
6698     int		didit = 0;
6699     int		n;
6700 
6701     /* Don't check timestamps while system() or another low-level function may
6702      * cause us to lose and gain focus. */
6703     if (no_check_timestamps > 0)
6704 	return FALSE;
6705 
6706     /* Avoid doing a check twice.  The OK/Reload dialog can cause a focus
6707      * event and we would keep on checking if the file is steadily growing.
6708      * Do check again after typing something. */
6709     if (focus && did_check_timestamps)
6710     {
6711 	need_check_timestamps = TRUE;
6712 	return FALSE;
6713     }
6714 
6715     if (!stuff_empty() || global_busy || !typebuf_typed()
6716 #ifdef FEAT_AUTOCMD
6717 			|| autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0
6718 #endif
6719 					)
6720 	need_check_timestamps = TRUE;		/* check later */
6721     else
6722     {
6723 	++no_wait_return;
6724 	did_check_timestamps = TRUE;
6725 	already_warned = FALSE;
6726 	for (buf = firstbuf; buf != NULL; )
6727 	{
6728 	    /* Only check buffers in a window. */
6729 	    if (buf->b_nwindows > 0)
6730 	    {
6731 		n = buf_check_timestamp(buf, focus);
6732 		if (didit < n)
6733 		    didit = n;
6734 		if (n > 0 && !buf_valid(buf))
6735 		{
6736 		    /* Autocommands have removed the buffer, start at the
6737 		     * first one again. */
6738 		    buf = firstbuf;
6739 		    continue;
6740 		}
6741 	    }
6742 	    buf = buf->b_next;
6743 	}
6744 	--no_wait_return;
6745 	need_check_timestamps = FALSE;
6746 	if (need_wait_return && didit == 2)
6747 	{
6748 	    /* make sure msg isn't overwritten */
6749 	    msg_puts((char_u *)"\n");
6750 	    out_flush();
6751 	}
6752     }
6753     return didit;
6754 }
6755 
6756 /*
6757  * Move all the lines from buffer "frombuf" to buffer "tobuf".
6758  * Return OK or FAIL.  When FAIL "tobuf" is incomplete and/or "frombuf" is not
6759  * empty.
6760  */
6761     static int
6762 move_lines(frombuf, tobuf)
6763     buf_T	*frombuf;
6764     buf_T	*tobuf;
6765 {
6766     buf_T	*tbuf = curbuf;
6767     int		retval = OK;
6768     linenr_T	lnum;
6769     char_u	*p;
6770 
6771     /* Copy the lines in "frombuf" to "tobuf". */
6772     curbuf = tobuf;
6773     for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
6774     {
6775 	p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
6776 	if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
6777 	{
6778 	    vim_free(p);
6779 	    retval = FAIL;
6780 	    break;
6781 	}
6782 	vim_free(p);
6783     }
6784 
6785     /* Delete all the lines in "frombuf". */
6786     if (retval != FAIL)
6787     {
6788 	curbuf = frombuf;
6789 	for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
6790 	    if (ml_delete(lnum, FALSE) == FAIL)
6791 	    {
6792 		/* Oops!  We could try putting back the saved lines, but that
6793 		 * might fail again... */
6794 		retval = FAIL;
6795 		break;
6796 	    }
6797     }
6798 
6799     curbuf = tbuf;
6800     return retval;
6801 }
6802 
6803 /*
6804  * Check if buffer "buf" has been changed.
6805  * Also check if the file for a new buffer unexpectedly appeared.
6806  * return 1 if a changed buffer was found.
6807  * return 2 if a message has been displayed.
6808  * return 0 otherwise.
6809  */
6810     int
6811 buf_check_timestamp(buf, focus)
6812     buf_T	*buf;
6813     int		focus UNUSED;	/* called for GUI focus event */
6814 {
6815     struct stat	st;
6816     int		stat_res;
6817     int		retval = 0;
6818     char_u	*path;
6819     char_u	*tbuf;
6820     char	*mesg = NULL;
6821     char	*mesg2 = "";
6822     int		helpmesg = FALSE;
6823     int		reload = FALSE;
6824 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6825     int		can_reload = FALSE;
6826 #endif
6827     off_t	orig_size = buf->b_orig_size;
6828     int		orig_mode = buf->b_orig_mode;
6829 #ifdef FEAT_GUI
6830     int		save_mouse_correct = need_mouse_correct;
6831 #endif
6832 #ifdef FEAT_AUTOCMD
6833     static int	busy = FALSE;
6834     int		n;
6835     char_u	*s;
6836 #endif
6837     char	*reason;
6838 
6839     /* If there is no file name, the buffer is not loaded, 'buftype' is
6840      * set, we are in the middle of a save or being called recursively: ignore
6841      * this buffer. */
6842     if (buf->b_ffname == NULL
6843 	    || buf->b_ml.ml_mfp == NULL
6844 #if defined(FEAT_QUICKFIX)
6845 	    || *buf->b_p_bt != NUL
6846 #endif
6847 	    || buf->b_saving
6848 #ifdef FEAT_AUTOCMD
6849 	    || busy
6850 #endif
6851 #ifdef FEAT_NETBEANS_INTG
6852 	    || isNetbeansBuffer(buf)
6853 #endif
6854 	    )
6855 	return 0;
6856 
6857     if (       !(buf->b_flags & BF_NOTEDITED)
6858 	    && buf->b_mtime != 0
6859 	    && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
6860 		|| time_differs((long)st.st_mtime, buf->b_mtime)
6861 #ifdef HAVE_ST_MODE
6862 		|| (int)st.st_mode != buf->b_orig_mode
6863 #else
6864 		|| mch_getperm(buf->b_ffname) != buf->b_orig_mode
6865 #endif
6866 		))
6867     {
6868 	retval = 1;
6869 
6870 	/* set b_mtime to stop further warnings (e.g., when executing
6871 	 * FileChangedShell autocmd) */
6872 	if (stat_res < 0)
6873 	{
6874 	    buf->b_mtime = 0;
6875 	    buf->b_orig_size = 0;
6876 	    buf->b_orig_mode = 0;
6877 	}
6878 	else
6879 	    buf_store_time(buf, &st, buf->b_ffname);
6880 
6881 	/* Don't do anything for a directory.  Might contain the file
6882 	 * explorer. */
6883 	if (mch_isdir(buf->b_fname))
6884 	    ;
6885 
6886 	/*
6887 	 * If 'autoread' is set, the buffer has no changes and the file still
6888 	 * exists, reload the buffer.  Use the buffer-local option value if it
6889 	 * was set, the global option value otherwise.
6890 	 */
6891 	else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
6892 				       && !bufIsChanged(buf) && stat_res >= 0)
6893 	    reload = TRUE;
6894 	else
6895 	{
6896 	    if (stat_res < 0)
6897 		reason = "deleted";
6898 	    else if (bufIsChanged(buf))
6899 		reason = "conflict";
6900 	    else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
6901 		reason = "changed";
6902 	    else if (orig_mode != buf->b_orig_mode)
6903 		reason = "mode";
6904 	    else
6905 		reason = "time";
6906 
6907 #ifdef FEAT_AUTOCMD
6908 	    /*
6909 	     * Only give the warning if there are no FileChangedShell
6910 	     * autocommands.
6911 	     * Avoid being called recursively by setting "busy".
6912 	     */
6913 	    busy = TRUE;
6914 # ifdef FEAT_EVAL
6915 	    set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
6916 	    set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
6917 # endif
6918 	    ++allbuf_lock;
6919 	    n = apply_autocmds(EVENT_FILECHANGEDSHELL,
6920 				      buf->b_fname, buf->b_fname, FALSE, buf);
6921 	    --allbuf_lock;
6922 	    busy = FALSE;
6923 	    if (n)
6924 	    {
6925 		if (!buf_valid(buf))
6926 		    EMSG(_("E246: FileChangedShell autocommand deleted buffer"));
6927 # ifdef FEAT_EVAL
6928 		s = get_vim_var_str(VV_FCS_CHOICE);
6929 		if (STRCMP(s, "reload") == 0 && *reason != 'd')
6930 		    reload = TRUE;
6931 		else if (STRCMP(s, "ask") == 0)
6932 		    n = FALSE;
6933 		else
6934 # endif
6935 		    return 2;
6936 	    }
6937 	    if (!n)
6938 #endif
6939 	    {
6940 		if (*reason == 'd')
6941 		    mesg = _("E211: File \"%s\" no longer available");
6942 		else
6943 		{
6944 		    helpmesg = TRUE;
6945 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6946 		    can_reload = TRUE;
6947 #endif
6948 		    /*
6949 		     * Check if the file contents really changed to avoid
6950 		     * giving a warning when only the timestamp was set (e.g.,
6951 		     * checked out of CVS).  Always warn when the buffer was
6952 		     * changed.
6953 		     */
6954 		    if (reason[2] == 'n')
6955 		    {
6956 			mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
6957 			mesg2 = _("See \":help W12\" for more info.");
6958 		    }
6959 		    else if (reason[1] == 'h')
6960 		    {
6961 			mesg = _("W11: Warning: File \"%s\" has changed since editing started");
6962 			mesg2 = _("See \":help W11\" for more info.");
6963 		    }
6964 		    else if (*reason == 'm')
6965 		    {
6966 			mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
6967 			mesg2 = _("See \":help W16\" for more info.");
6968 		    }
6969 		    else
6970 			/* Only timestamp changed, store it to avoid a warning
6971 			 * in check_mtime() later. */
6972 			buf->b_mtime_read = buf->b_mtime;
6973 		}
6974 	    }
6975 	}
6976 
6977     }
6978     else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
6979 						&& vim_fexists(buf->b_ffname))
6980     {
6981 	retval = 1;
6982 	mesg = _("W13: Warning: File \"%s\" has been created after editing started");
6983 	buf->b_flags |= BF_NEW_W;
6984 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6985 	can_reload = TRUE;
6986 #endif
6987     }
6988 
6989     if (mesg != NULL)
6990     {
6991 	path = home_replace_save(buf, buf->b_fname);
6992 	if (path != NULL)
6993 	{
6994 	    if (!helpmesg)
6995 		mesg2 = "";
6996 	    tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg)
6997 							+ STRLEN(mesg2) + 2));
6998 	    sprintf((char *)tbuf, mesg, path);
6999 #ifdef FEAT_EVAL
7000 	    /* Set warningmsg here, before the unimportant and output-specific
7001 	     * mesg2 has been appended. */
7002 	    set_vim_var_string(VV_WARNINGMSG, tbuf, -1);
7003 #endif
7004 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
7005 	    if (can_reload)
7006 	    {
7007 		if (*mesg2 != NUL)
7008 		{
7009 		    STRCAT(tbuf, "\n");
7010 		    STRCAT(tbuf, mesg2);
7011 		}
7012 		if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf,
7013 			  (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
7014 		    reload = TRUE;
7015 	    }
7016 	    else
7017 #endif
7018 	    if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
7019 	    {
7020 		if (*mesg2 != NUL)
7021 		{
7022 		    STRCAT(tbuf, "; ");
7023 		    STRCAT(tbuf, mesg2);
7024 		}
7025 		EMSG(tbuf);
7026 		retval = 2;
7027 	    }
7028 	    else
7029 	    {
7030 # ifdef FEAT_AUTOCMD
7031 		if (!autocmd_busy)
7032 # endif
7033 		{
7034 		    msg_start();
7035 		    msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST);
7036 		    if (*mesg2 != NUL)
7037 			msg_puts_attr((char_u *)mesg2,
7038 						   hl_attr(HLF_W) + MSG_HIST);
7039 		    msg_clr_eos();
7040 		    (void)msg_end();
7041 		    if (emsg_silent == 0)
7042 		    {
7043 			out_flush();
7044 # ifdef FEAT_GUI
7045 			if (!focus)
7046 # endif
7047 			    /* give the user some time to think about it */
7048 			    ui_delay(1000L, TRUE);
7049 
7050 			/* don't redraw and erase the message */
7051 			redraw_cmdline = FALSE;
7052 		    }
7053 		}
7054 		already_warned = TRUE;
7055 	    }
7056 
7057 	    vim_free(path);
7058 	    vim_free(tbuf);
7059 	}
7060     }
7061 
7062     if (reload)
7063     {
7064 	/* Reload the buffer. */
7065 	buf_reload(buf, orig_mode);
7066 #ifdef FEAT_PERSISTENT_UNDO
7067 	if (buf->b_p_udf && buf->b_ffname != NULL)
7068 	{
7069 	    char_u	    hash[UNDO_HASH_SIZE];
7070 	    buf_T	    *save_curbuf = curbuf;
7071 
7072 	    /* Any existing undo file is unusable, write it now. */
7073 	    curbuf = buf;
7074 	    u_compute_hash(hash);
7075 	    u_write_undo(NULL, FALSE, buf, hash);
7076 	    curbuf = save_curbuf;
7077 	}
7078 #endif
7079     }
7080 
7081 #ifdef FEAT_AUTOCMD
7082     /* Trigger FileChangedShell when the file was changed in any way. */
7083     if (buf_valid(buf) && retval != 0)
7084 	(void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
7085 				      buf->b_fname, buf->b_fname, FALSE, buf);
7086 #endif
7087 #ifdef FEAT_GUI
7088     /* restore this in case an autocommand has set it; it would break
7089      * 'mousefocus' */
7090     need_mouse_correct = save_mouse_correct;
7091 #endif
7092 
7093     return retval;
7094 }
7095 
7096 /*
7097  * Reload a buffer that is already loaded.
7098  * Used when the file was changed outside of Vim.
7099  * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
7100  * buf->b_orig_mode may have been reset already.
7101  */
7102     void
7103 buf_reload(buf, orig_mode)
7104     buf_T	*buf;
7105     int		orig_mode;
7106 {
7107     exarg_T	ea;
7108     pos_T	old_cursor;
7109     linenr_T	old_topline;
7110     int		old_ro = buf->b_p_ro;
7111     buf_T	*savebuf;
7112     int		saved = OK;
7113     aco_save_T	aco;
7114     int		flags = READ_NEW;
7115 
7116     /* set curwin/curbuf for "buf" and save some things */
7117     aucmd_prepbuf(&aco, buf);
7118 
7119     /* We only want to read the text from the file, not reset the syntax
7120      * highlighting, clear marks, diff status, etc.  Force the fileformat
7121      * and encoding to be the same. */
7122     if (prep_exarg(&ea, buf) == OK)
7123     {
7124 	old_cursor = curwin->w_cursor;
7125 	old_topline = curwin->w_topline;
7126 
7127 	if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
7128 	{
7129 	    /* Save all the text, so that the reload can be undone.
7130 	     * Sync first so that this is a separate undo-able action. */
7131 	    u_sync(FALSE);
7132 	    saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
7133 	    flags |= READ_KEEP_UNDO;
7134 	}
7135 
7136 	/*
7137 	 * To behave like when a new file is edited (matters for
7138 	 * BufReadPost autocommands) we first need to delete the current
7139 	 * buffer contents.  But if reading the file fails we should keep
7140 	 * the old contents.  Can't use memory only, the file might be
7141 	 * too big.  Use a hidden buffer to move the buffer contents to.
7142 	 */
7143 	if (bufempty() || saved == FAIL)
7144 	    savebuf = NULL;
7145 	else
7146 	{
7147 	    /* Allocate a buffer without putting it in the buffer list. */
7148 	    savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
7149 	    if (savebuf != NULL && buf == curbuf)
7150 	    {
7151 		/* Open the memline. */
7152 		curbuf = savebuf;
7153 		curwin->w_buffer = savebuf;
7154 		saved = ml_open(curbuf);
7155 		curbuf = buf;
7156 		curwin->w_buffer = buf;
7157 	    }
7158 	    if (savebuf == NULL || saved == FAIL || buf != curbuf
7159 				      || move_lines(buf, savebuf) == FAIL)
7160 	    {
7161 		EMSG2(_("E462: Could not prepare for reloading \"%s\""),
7162 							    buf->b_fname);
7163 		saved = FAIL;
7164 	    }
7165 	}
7166 
7167 	if (saved == OK)
7168 	{
7169 	    curbuf->b_flags |= BF_CHECK_RO;	/* check for RO again */
7170 #ifdef FEAT_AUTOCMD
7171 	    keep_filetype = TRUE;		/* don't detect 'filetype' */
7172 #endif
7173 	    if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
7174 			(linenr_T)0,
7175 			(linenr_T)MAXLNUM, &ea, flags) == FAIL)
7176 	    {
7177 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7178 		if (!aborting())
7179 #endif
7180 		    EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
7181 		if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf)
7182 		{
7183 		    /* Put the text back from the save buffer.  First
7184 		     * delete any lines that readfile() added. */
7185 		    while (!bufempty())
7186 			if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL)
7187 			    break;
7188 		    (void)move_lines(savebuf, buf);
7189 		}
7190 	    }
7191 	    else if (buf == curbuf)  /* "buf" still valid */
7192 	    {
7193 		/* Mark the buffer as unmodified and free undo info. */
7194 		unchanged(buf, TRUE);
7195 		if ((flags & READ_KEEP_UNDO) == 0)
7196 		{
7197 		    u_blockfree(buf);
7198 		    u_clearall(buf);
7199 		}
7200 		else
7201 		{
7202 		    /* Mark all undo states as changed. */
7203 		    u_unchanged(curbuf);
7204 		}
7205 	    }
7206 	}
7207 	vim_free(ea.cmd);
7208 
7209 	if (savebuf != NULL && buf_valid(savebuf))
7210 	    wipe_buffer(savebuf, FALSE);
7211 
7212 #ifdef FEAT_DIFF
7213 	/* Invalidate diff info if necessary. */
7214 	diff_invalidate(curbuf);
7215 #endif
7216 
7217 	/* Restore the topline and cursor position and check it (lines may
7218 	 * have been removed). */
7219 	if (old_topline > curbuf->b_ml.ml_line_count)
7220 	    curwin->w_topline = curbuf->b_ml.ml_line_count;
7221 	else
7222 	    curwin->w_topline = old_topline;
7223 	curwin->w_cursor = old_cursor;
7224 	check_cursor();
7225 	update_topline();
7226 #ifdef FEAT_AUTOCMD
7227 	keep_filetype = FALSE;
7228 #endif
7229 #ifdef FEAT_FOLDING
7230 	{
7231 	    win_T	*wp;
7232 	    tabpage_T	*tp;
7233 
7234 	    /* Update folds unless they are defined manually. */
7235 	    FOR_ALL_TAB_WINDOWS(tp, wp)
7236 		if (wp->w_buffer == curwin->w_buffer
7237 			&& !foldmethodIsManual(wp))
7238 		    foldUpdateAll(wp);
7239 	}
7240 #endif
7241 	/* If the mode didn't change and 'readonly' was set, keep the old
7242 	 * value; the user probably used the ":view" command.  But don't
7243 	 * reset it, might have had a read error. */
7244 	if (orig_mode == curbuf->b_orig_mode)
7245 	    curbuf->b_p_ro |= old_ro;
7246     }
7247 
7248     /* restore curwin/curbuf and a few other things */
7249     aucmd_restbuf(&aco);
7250     /* Careful: autocommands may have made "buf" invalid! */
7251 }
7252 
7253     void
7254 buf_store_time(buf, st, fname)
7255     buf_T	*buf;
7256     struct stat	*st;
7257     char_u	*fname UNUSED;
7258 {
7259     buf->b_mtime = (long)st->st_mtime;
7260     buf->b_orig_size = st->st_size;
7261 #ifdef HAVE_ST_MODE
7262     buf->b_orig_mode = (int)st->st_mode;
7263 #else
7264     buf->b_orig_mode = mch_getperm(fname);
7265 #endif
7266 }
7267 
7268 /*
7269  * Adjust the line with missing eol, used for the next write.
7270  * Used for do_filter(), when the input lines for the filter are deleted.
7271  */
7272     void
7273 write_lnum_adjust(offset)
7274     linenr_T	offset;
7275 {
7276     if (curbuf->b_no_eol_lnum != 0)	/* only if there is a missing eol */
7277 	curbuf->b_no_eol_lnum += offset;
7278 }
7279 
7280 #if defined(TEMPDIRNAMES) || defined(PROTO)
7281 static long	temp_count = 0;		/* Temp filename counter. */
7282 
7283 /*
7284  * Delete the temp directory and all files it contains.
7285  */
7286     void
7287 vim_deltempdir()
7288 {
7289     char_u	**files;
7290     int		file_count;
7291     int		i;
7292 
7293     if (vim_tempdir != NULL)
7294     {
7295 	sprintf((char *)NameBuff, "%s*", vim_tempdir);
7296 	if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
7297 					      EW_DIR|EW_FILE|EW_SILENT) == OK)
7298 	{
7299 	    for (i = 0; i < file_count; ++i)
7300 		mch_remove(files[i]);
7301 	    FreeWild(file_count, files);
7302 	}
7303 	gettail(NameBuff)[-1] = NUL;
7304 	(void)mch_rmdir(NameBuff);
7305 
7306 	vim_free(vim_tempdir);
7307 	vim_tempdir = NULL;
7308     }
7309 }
7310 #endif
7311 
7312 #ifdef TEMPDIRNAMES
7313 /*
7314  * Directory "tempdir" was created.  Expand this name to a full path and put
7315  * it in "vim_tempdir".  This avoids that using ":cd" would confuse us.
7316  * "tempdir" must be no longer than MAXPATHL.
7317  */
7318     static void
7319 vim_settempdir(tempdir)
7320     char_u	*tempdir;
7321 {
7322     char_u	*buf;
7323 
7324     buf = alloc((unsigned)MAXPATHL + 2);
7325     if (buf != NULL)
7326     {
7327 	if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
7328 	    STRCPY(buf, tempdir);
7329 # ifdef __EMX__
7330 	if (vim_strchr(buf, '/') != NULL)
7331 	    STRCAT(buf, "/");
7332 	else
7333 # endif
7334 	    add_pathsep(buf);
7335 	vim_tempdir = vim_strsave(buf);
7336 	vim_free(buf);
7337     }
7338 }
7339 #endif
7340 
7341 /*
7342  * vim_tempname(): Return a unique name that can be used for a temp file.
7343  *
7344  * The temp file is NOT created.
7345  *
7346  * The returned pointer is to allocated memory.
7347  * The returned pointer is NULL if no valid name was found.
7348  */
7349     char_u  *
7350 vim_tempname(extra_char)
7351     int	    extra_char UNUSED;  /* char to use in the name instead of '?' */
7352 {
7353 #ifdef USE_TMPNAM
7354     char_u	itmp[L_tmpnam];	/* use tmpnam() */
7355 #else
7356     char_u	itmp[TEMPNAMELEN];
7357 #endif
7358 
7359 #ifdef TEMPDIRNAMES
7360     static char	*(tempdirs[]) = {TEMPDIRNAMES};
7361     int		i;
7362 # ifndef EEXIST
7363     struct stat	st;
7364 # endif
7365 
7366     /*
7367      * This will create a directory for private use by this instance of Vim.
7368      * This is done once, and the same directory is used for all temp files.
7369      * This method avoids security problems because of symlink attacks et al.
7370      * It's also a bit faster, because we only need to check for an existing
7371      * file when creating the directory and not for each temp file.
7372      */
7373     if (vim_tempdir == NULL)
7374     {
7375 	/*
7376 	 * Try the entries in TEMPDIRNAMES to create the temp directory.
7377 	 */
7378 	for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
7379 	{
7380 # ifndef HAVE_MKDTEMP
7381 	    size_t	itmplen;
7382 	    long	nr;
7383 	    long	off;
7384 # endif
7385 
7386 	    /* expand $TMP, leave room for "/v1100000/999999999" */
7387 	    expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
7388 	    if (mch_isdir(itmp))		/* directory exists */
7389 	    {
7390 # ifdef __EMX__
7391 		/* If $TMP contains a forward slash (perhaps using bash or
7392 		 * tcsh), don't add a backslash, use a forward slash!
7393 		 * Adding 2 backslashes didn't work. */
7394 		if (vim_strchr(itmp, '/') != NULL)
7395 		    STRCAT(itmp, "/");
7396 		else
7397 # endif
7398 		    add_pathsep(itmp);
7399 
7400 # ifdef HAVE_MKDTEMP
7401 		/* Leave room for filename */
7402 		STRCAT(itmp, "vXXXXXX");
7403 		if (mkdtemp((char *)itmp) != NULL)
7404 		    vim_settempdir(itmp);
7405 # else
7406 		/* Get an arbitrary number of up to 6 digits.  When it's
7407 		 * unlikely that it already exists it will be faster,
7408 		 * otherwise it doesn't matter.  The use of mkdir() avoids any
7409 		 * security problems because of the predictable number. */
7410 		nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
7411 		itmplen = STRLEN(itmp);
7412 
7413 		/* Try up to 10000 different values until we find a name that
7414 		 * doesn't exist. */
7415 		for (off = 0; off < 10000L; ++off)
7416 		{
7417 		    int		r;
7418 #  if defined(UNIX) || defined(VMS)
7419 		    mode_t	umask_save;
7420 #  endif
7421 
7422 		    sprintf((char *)itmp + itmplen, "v%ld", nr + off);
7423 #  ifndef EEXIST
7424 		    /* If mkdir() does not set errno to EEXIST, check for
7425 		     * existing file here.  There is a race condition then,
7426 		     * although it's fail-safe. */
7427 		    if (mch_stat((char *)itmp, &st) >= 0)
7428 			continue;
7429 #  endif
7430 #  if defined(UNIX) || defined(VMS)
7431 		    /* Make sure the umask doesn't remove the executable bit.
7432 		     * "repl" has been reported to use "177". */
7433 		    umask_save = umask(077);
7434 #  endif
7435 		    r = vim_mkdir(itmp, 0700);
7436 #  if defined(UNIX) || defined(VMS)
7437 		    (void)umask(umask_save);
7438 #  endif
7439 		    if (r == 0)
7440 		    {
7441 			vim_settempdir(itmp);
7442 			break;
7443 		    }
7444 #  ifdef EEXIST
7445 		    /* If the mkdir() didn't fail because the file/dir exists,
7446 		     * we probably can't create any dir here, try another
7447 		     * place. */
7448 		    if (errno != EEXIST)
7449 #  endif
7450 			break;
7451 		}
7452 # endif /* HAVE_MKDTEMP */
7453 		if (vim_tempdir != NULL)
7454 		    break;
7455 	    }
7456 	}
7457     }
7458 
7459     if (vim_tempdir != NULL)
7460     {
7461 	/* There is no need to check if the file exists, because we own the
7462 	 * directory and nobody else creates a file in it. */
7463 	sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
7464 	return vim_strsave(itmp);
7465     }
7466 
7467     return NULL;
7468 
7469 #else /* TEMPDIRNAMES */
7470 
7471 # ifdef WIN3264
7472     char	szTempFile[_MAX_PATH + 1];
7473     char	buf4[4];
7474     char_u	*retval;
7475     char_u	*p;
7476 
7477     STRCPY(itmp, "");
7478     if (GetTempPath(_MAX_PATH, szTempFile) == 0)
7479     {
7480 	szTempFile[0] = '.';	/* GetTempPath() failed, use current dir */
7481 	szTempFile[1] = NUL;
7482     }
7483     strcpy(buf4, "VIM");
7484     buf4[2] = extra_char;   /* make it "VIa", "VIb", etc. */
7485     if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0)
7486 	return NULL;
7487     /* GetTempFileName() will create the file, we don't want that */
7488     (void)DeleteFile(itmp);
7489 
7490     /* Backslashes in a temp file name cause problems when filtering with
7491      * "sh".  NOTE: This also checks 'shellcmdflag' to help those people who
7492      * didn't set 'shellslash'. */
7493     retval = vim_strsave(itmp);
7494     if (*p_shcf == '-' || p_ssl)
7495 	for (p = retval; *p; ++p)
7496 	    if (*p == '\\')
7497 		*p = '/';
7498     return retval;
7499 
7500 # else /* WIN3264 */
7501 
7502 #  ifdef USE_TMPNAM
7503     char_u	*p;
7504 
7505     /* tmpnam() will make its own name */
7506     p = tmpnam((char *)itmp);
7507     if (p == NULL || *p == NUL)
7508 	return NULL;
7509 #  else
7510     char_u	*p;
7511 
7512 #   ifdef VMS_TEMPNAM
7513     /* mktemp() is not working on VMS.  It seems to be
7514      * a do-nothing function. Therefore we use tempnam().
7515      */
7516     sprintf((char *)itmp, "VIM%c", extra_char);
7517     p = (char_u *)tempnam("tmp:", (char *)itmp);
7518     if (p != NULL)
7519     {
7520 	/* VMS will use '.LOG' if we don't explicitly specify an extension,
7521 	 * and VIM will then be unable to find the file later */
7522 	STRCPY(itmp, p);
7523 	STRCAT(itmp, ".txt");
7524 	free(p);
7525     }
7526     else
7527 	return NULL;
7528 #   else
7529     STRCPY(itmp, TEMPNAME);
7530     if ((p = vim_strchr(itmp, '?')) != NULL)
7531 	*p = extra_char;
7532     if (mktemp((char *)itmp) == NULL)
7533 	return NULL;
7534 #   endif
7535 #  endif
7536 
7537     return vim_strsave(itmp);
7538 # endif /* WIN3264 */
7539 #endif /* TEMPDIRNAMES */
7540 }
7541 
7542 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7543 /*
7544  * Convert all backslashes in fname to forward slashes in-place.
7545  */
7546     void
7547 forward_slash(fname)
7548     char_u	*fname;
7549 {
7550     char_u	*p;
7551 
7552     for (p = fname; *p != NUL; ++p)
7553 # ifdef  FEAT_MBYTE
7554 	/* The Big5 encoding can have '\' in the trail byte. */
7555 	if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
7556 	    ++p;
7557 	else
7558 # endif
7559 	if (*p == '\\')
7560 	    *p = '/';
7561 }
7562 #endif
7563 
7564 
7565 /*
7566  * Code for automatic commands.
7567  *
7568  * Only included when "FEAT_AUTOCMD" has been defined.
7569  */
7570 
7571 #if defined(FEAT_AUTOCMD) || defined(PROTO)
7572 
7573 /*
7574  * The autocommands are stored in a list for each event.
7575  * Autocommands for the same pattern, that are consecutive, are joined
7576  * together, to avoid having to match the pattern too often.
7577  * The result is an array of Autopat lists, which point to AutoCmd lists:
7578  *
7579  * first_autopat[0] --> Autopat.next  -->  Autopat.next -->  NULL
7580  *			Autopat.cmds	   Autopat.cmds
7581  *			    |			 |
7582  *			    V			 V
7583  *			AutoCmd.next	   AutoCmd.next
7584  *			    |			 |
7585  *			    V			 V
7586  *			AutoCmd.next		NULL
7587  *			    |
7588  *			    V
7589  *			   NULL
7590  *
7591  * first_autopat[1] --> Autopat.next  -->  NULL
7592  *			Autopat.cmds
7593  *			    |
7594  *			    V
7595  *			AutoCmd.next
7596  *			    |
7597  *			    V
7598  *			   NULL
7599  *   etc.
7600  *
7601  *   The order of AutoCmds is important, this is the order in which they were
7602  *   defined and will have to be executed.
7603  */
7604 typedef struct AutoCmd
7605 {
7606     char_u	    *cmd;		/* The command to be executed (NULL
7607 					   when command has been removed) */
7608     char	    nested;		/* If autocommands nest here */
7609     char	    last;		/* last command in list */
7610 #ifdef FEAT_EVAL
7611     scid_T	    scriptID;		/* script ID where defined */
7612 #endif
7613     struct AutoCmd  *next;		/* Next AutoCmd in list */
7614 } AutoCmd;
7615 
7616 typedef struct AutoPat
7617 {
7618     int		    group;		/* group ID */
7619     char_u	    *pat;		/* pattern as typed (NULL when pattern
7620 					   has been removed) */
7621     int		    patlen;		/* strlen() of pat */
7622     regprog_T	    *reg_prog;		/* compiled regprog for pattern */
7623     char	    allow_dirs;		/* Pattern may match whole path */
7624     char	    last;		/* last pattern for apply_autocmds() */
7625     AutoCmd	    *cmds;		/* list of commands to do */
7626     struct AutoPat  *next;		/* next AutoPat in AutoPat list */
7627     int		    buflocal_nr;	/* !=0 for buffer-local AutoPat */
7628 } AutoPat;
7629 
7630 static struct event_name
7631 {
7632     char	*name;	/* event name */
7633     event_T	event;	/* event number */
7634 } event_names[] =
7635 {
7636     {"BufAdd",		EVENT_BUFADD},
7637     {"BufCreate",	EVENT_BUFADD},
7638     {"BufDelete",	EVENT_BUFDELETE},
7639     {"BufEnter",	EVENT_BUFENTER},
7640     {"BufFilePost",	EVENT_BUFFILEPOST},
7641     {"BufFilePre",	EVENT_BUFFILEPRE},
7642     {"BufHidden",	EVENT_BUFHIDDEN},
7643     {"BufLeave",	EVENT_BUFLEAVE},
7644     {"BufNew",		EVENT_BUFNEW},
7645     {"BufNewFile",	EVENT_BUFNEWFILE},
7646     {"BufRead",		EVENT_BUFREADPOST},
7647     {"BufReadCmd",	EVENT_BUFREADCMD},
7648     {"BufReadPost",	EVENT_BUFREADPOST},
7649     {"BufReadPre",	EVENT_BUFREADPRE},
7650     {"BufUnload",	EVENT_BUFUNLOAD},
7651     {"BufWinEnter",	EVENT_BUFWINENTER},
7652     {"BufWinLeave",	EVENT_BUFWINLEAVE},
7653     {"BufWipeout",	EVENT_BUFWIPEOUT},
7654     {"BufWrite",	EVENT_BUFWRITEPRE},
7655     {"BufWritePost",	EVENT_BUFWRITEPOST},
7656     {"BufWritePre",	EVENT_BUFWRITEPRE},
7657     {"BufWriteCmd",	EVENT_BUFWRITECMD},
7658     {"CmdwinEnter",	EVENT_CMDWINENTER},
7659     {"CmdwinLeave",	EVENT_CMDWINLEAVE},
7660     {"ColorScheme",	EVENT_COLORSCHEME},
7661     {"CompleteDone",	EVENT_COMPLETEDONE},
7662     {"CursorHold",	EVENT_CURSORHOLD},
7663     {"CursorHoldI",	EVENT_CURSORHOLDI},
7664     {"CursorMoved",	EVENT_CURSORMOVED},
7665     {"CursorMovedI",	EVENT_CURSORMOVEDI},
7666     {"EncodingChanged",	EVENT_ENCODINGCHANGED},
7667     {"FileEncoding",	EVENT_ENCODINGCHANGED},
7668     {"FileAppendPost",	EVENT_FILEAPPENDPOST},
7669     {"FileAppendPre",	EVENT_FILEAPPENDPRE},
7670     {"FileAppendCmd",	EVENT_FILEAPPENDCMD},
7671     {"FileChangedShell",EVENT_FILECHANGEDSHELL},
7672     {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST},
7673     {"FileChangedRO",	EVENT_FILECHANGEDRO},
7674     {"FileReadPost",	EVENT_FILEREADPOST},
7675     {"FileReadPre",	EVENT_FILEREADPRE},
7676     {"FileReadCmd",	EVENT_FILEREADCMD},
7677     {"FileType",	EVENT_FILETYPE},
7678     {"FileWritePost",	EVENT_FILEWRITEPOST},
7679     {"FileWritePre",	EVENT_FILEWRITEPRE},
7680     {"FileWriteCmd",	EVENT_FILEWRITECMD},
7681     {"FilterReadPost",	EVENT_FILTERREADPOST},
7682     {"FilterReadPre",	EVENT_FILTERREADPRE},
7683     {"FilterWritePost",	EVENT_FILTERWRITEPOST},
7684     {"FilterWritePre",	EVENT_FILTERWRITEPRE},
7685     {"FocusGained",	EVENT_FOCUSGAINED},
7686     {"FocusLost",	EVENT_FOCUSLOST},
7687     {"FuncUndefined",	EVENT_FUNCUNDEFINED},
7688     {"GUIEnter",	EVENT_GUIENTER},
7689     {"GUIFailed",	EVENT_GUIFAILED},
7690     {"InsertChange",	EVENT_INSERTCHANGE},
7691     {"InsertEnter",	EVENT_INSERTENTER},
7692     {"InsertLeave",	EVENT_INSERTLEAVE},
7693     {"InsertCharPre",	EVENT_INSERTCHARPRE},
7694     {"MenuPopup",	EVENT_MENUPOPUP},
7695     {"QuickFixCmdPost",	EVENT_QUICKFIXCMDPOST},
7696     {"QuickFixCmdPre",	EVENT_QUICKFIXCMDPRE},
7697     {"QuitPre",		EVENT_QUITPRE},
7698     {"RemoteReply",	EVENT_REMOTEREPLY},
7699     {"SessionLoadPost",	EVENT_SESSIONLOADPOST},
7700     {"ShellCmdPost",	EVENT_SHELLCMDPOST},
7701     {"ShellFilterPost",	EVENT_SHELLFILTERPOST},
7702     {"SourcePre",	EVENT_SOURCEPRE},
7703     {"SourceCmd",	EVENT_SOURCECMD},
7704     {"SpellFileMissing",EVENT_SPELLFILEMISSING},
7705     {"StdinReadPost",	EVENT_STDINREADPOST},
7706     {"StdinReadPre",	EVENT_STDINREADPRE},
7707     {"SwapExists",	EVENT_SWAPEXISTS},
7708     {"Syntax",		EVENT_SYNTAX},
7709     {"TabEnter",	EVENT_TABENTER},
7710     {"TabLeave",	EVENT_TABLEAVE},
7711     {"TermChanged",	EVENT_TERMCHANGED},
7712     {"TermResponse",	EVENT_TERMRESPONSE},
7713     {"User",		EVENT_USER},
7714     {"VimEnter",	EVENT_VIMENTER},
7715     {"VimLeave",	EVENT_VIMLEAVE},
7716     {"VimLeavePre",	EVENT_VIMLEAVEPRE},
7717     {"WinEnter",	EVENT_WINENTER},
7718     {"WinLeave",	EVENT_WINLEAVE},
7719     {"VimResized",	EVENT_VIMRESIZED},
7720     {NULL,		(event_T)0}
7721 };
7722 
7723 static AutoPat *first_autopat[NUM_EVENTS] =
7724 {
7725     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7726     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7727     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7728     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7729     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7730     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
7731 };
7732 
7733 /*
7734  * struct used to keep status while executing autocommands for an event.
7735  */
7736 typedef struct AutoPatCmd
7737 {
7738     AutoPat	*curpat;	/* next AutoPat to examine */
7739     AutoCmd	*nextcmd;	/* next AutoCmd to execute */
7740     int		group;		/* group being used */
7741     char_u	*fname;		/* fname to match with */
7742     char_u	*sfname;	/* sfname to match with */
7743     char_u	*tail;		/* tail of fname */
7744     event_T	event;		/* current event */
7745     int		arg_bufnr;	/* initially equal to <abuf>, set to zero when
7746 				   buf is deleted */
7747     struct AutoPatCmd   *next;	/* chain of active apc-s for auto-invalidation*/
7748 } AutoPatCmd;
7749 
7750 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */
7751 
7752 /*
7753  * augroups stores a list of autocmd group names.
7754  */
7755 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL};
7756 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i])
7757 
7758 /*
7759  * The ID of the current group.  Group 0 is the default one.
7760  */
7761 static int current_augroup = AUGROUP_DEFAULT;
7762 
7763 static int au_need_clean = FALSE;   /* need to delete marked patterns */
7764 
7765 static void show_autocmd __ARGS((AutoPat *ap, event_T event));
7766 static void au_remove_pat __ARGS((AutoPat *ap));
7767 static void au_remove_cmds __ARGS((AutoPat *ap));
7768 static void au_cleanup __ARGS((void));
7769 static int au_new_group __ARGS((char_u *name));
7770 static void au_del_group __ARGS((char_u *name));
7771 static event_T event_name2nr __ARGS((char_u *start, char_u **end));
7772 static char_u *event_nr2name __ARGS((event_T event));
7773 static char_u *find_end_event __ARGS((char_u *arg, int have_group));
7774 static int event_ignored __ARGS((event_T event));
7775 static int au_get_grouparg __ARGS((char_u **argp));
7776 static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group));
7777 static char_u *getnextac __ARGS((int c, void *cookie, int indent));
7778 static int apply_autocmds_group __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap));
7779 static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last));
7780 
7781 
7782 static event_T	last_event;
7783 static int	last_group;
7784 static int	autocmd_blocked = 0;	/* block all autocmds */
7785 
7786 /*
7787  * Show the autocommands for one AutoPat.
7788  */
7789     static void
7790 show_autocmd(ap, event)
7791     AutoPat	*ap;
7792     event_T	event;
7793 {
7794     AutoCmd *ac;
7795 
7796     /* Check for "got_int" (here and at various places below), which is set
7797      * when "q" has been hit for the "--more--" prompt */
7798     if (got_int)
7799 	return;
7800     if (ap->pat == NULL)		/* pattern has been removed */
7801 	return;
7802 
7803     msg_putchar('\n');
7804     if (got_int)
7805 	return;
7806     if (event != last_event || ap->group != last_group)
7807     {
7808 	if (ap->group != AUGROUP_DEFAULT)
7809 	{
7810 	    if (AUGROUP_NAME(ap->group) == NULL)
7811 		msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E));
7812 	    else
7813 		msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T));
7814 	    msg_puts((char_u *)"  ");
7815 	}
7816 	msg_puts_attr(event_nr2name(event), hl_attr(HLF_T));
7817 	last_event = event;
7818 	last_group = ap->group;
7819 	msg_putchar('\n');
7820 	if (got_int)
7821 	    return;
7822     }
7823     msg_col = 4;
7824     msg_outtrans(ap->pat);
7825 
7826     for (ac = ap->cmds; ac != NULL; ac = ac->next)
7827     {
7828 	if (ac->cmd != NULL)		/* skip removed commands */
7829 	{
7830 	    if (msg_col >= 14)
7831 		msg_putchar('\n');
7832 	    msg_col = 14;
7833 	    if (got_int)
7834 		return;
7835 	    msg_outtrans(ac->cmd);
7836 #ifdef FEAT_EVAL
7837 	    if (p_verbose > 0)
7838 		last_set_msg(ac->scriptID);
7839 #endif
7840 	    if (got_int)
7841 		return;
7842 	    if (ac->next != NULL)
7843 	    {
7844 		msg_putchar('\n');
7845 		if (got_int)
7846 		    return;
7847 	    }
7848 	}
7849     }
7850 }
7851 
7852 /*
7853  * Mark an autocommand pattern for deletion.
7854  */
7855     static void
7856 au_remove_pat(ap)
7857     AutoPat *ap;
7858 {
7859     vim_free(ap->pat);
7860     ap->pat = NULL;
7861     ap->buflocal_nr = -1;
7862     au_need_clean = TRUE;
7863 }
7864 
7865 /*
7866  * Mark all commands for a pattern for deletion.
7867  */
7868     static void
7869 au_remove_cmds(ap)
7870     AutoPat *ap;
7871 {
7872     AutoCmd *ac;
7873 
7874     for (ac = ap->cmds; ac != NULL; ac = ac->next)
7875     {
7876 	vim_free(ac->cmd);
7877 	ac->cmd = NULL;
7878     }
7879     au_need_clean = TRUE;
7880 }
7881 
7882 /*
7883  * Cleanup autocommands and patterns that have been deleted.
7884  * This is only done when not executing autocommands.
7885  */
7886     static void
7887 au_cleanup()
7888 {
7889     AutoPat	*ap, **prev_ap;
7890     AutoCmd	*ac, **prev_ac;
7891     event_T	event;
7892 
7893     if (autocmd_busy || !au_need_clean)
7894 	return;
7895 
7896     /* loop over all events */
7897     for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
7898 					    event = (event_T)((int)event + 1))
7899     {
7900 	/* loop over all autocommand patterns */
7901 	prev_ap = &(first_autopat[(int)event]);
7902 	for (ap = *prev_ap; ap != NULL; ap = *prev_ap)
7903 	{
7904 	    /* loop over all commands for this pattern */
7905 	    prev_ac = &(ap->cmds);
7906 	    for (ac = *prev_ac; ac != NULL; ac = *prev_ac)
7907 	    {
7908 		/* remove the command if the pattern is to be deleted or when
7909 		 * the command has been marked for deletion */
7910 		if (ap->pat == NULL || ac->cmd == NULL)
7911 		{
7912 		    *prev_ac = ac->next;
7913 		    vim_free(ac->cmd);
7914 		    vim_free(ac);
7915 		}
7916 		else
7917 		    prev_ac = &(ac->next);
7918 	    }
7919 
7920 	    /* remove the pattern if it has been marked for deletion */
7921 	    if (ap->pat == NULL)
7922 	    {
7923 		*prev_ap = ap->next;
7924 		vim_free(ap->reg_prog);
7925 		vim_free(ap);
7926 	    }
7927 	    else
7928 		prev_ap = &(ap->next);
7929 	}
7930     }
7931 
7932     au_need_clean = FALSE;
7933 }
7934 
7935 /*
7936  * Called when buffer is freed, to remove/invalidate related buffer-local
7937  * autocmds.
7938  */
7939     void
7940 aubuflocal_remove(buf)
7941     buf_T	*buf;
7942 {
7943     AutoPat	*ap;
7944     event_T	event;
7945     AutoPatCmd	*apc;
7946 
7947     /* invalidate currently executing autocommands */
7948     for (apc = active_apc_list; apc; apc = apc->next)
7949 	if (buf->b_fnum == apc->arg_bufnr)
7950 	    apc->arg_bufnr = 0;
7951 
7952     /* invalidate buflocals looping through events */
7953     for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
7954 					    event = (event_T)((int)event + 1))
7955 	/* loop over all autocommand patterns */
7956 	for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
7957 	    if (ap->buflocal_nr == buf->b_fnum)
7958 	    {
7959 		au_remove_pat(ap);
7960 		if (p_verbose >= 6)
7961 		{
7962 		    verbose_enter();
7963 		    smsg((char_u *)
7964 			    _("auto-removing autocommand: %s <buffer=%d>"),
7965 					   event_nr2name(event), buf->b_fnum);
7966 		    verbose_leave();
7967 		}
7968 	    }
7969     au_cleanup();
7970 }
7971 
7972 /*
7973  * Add an autocmd group name.
7974  * Return it's ID.  Returns AUGROUP_ERROR (< 0) for error.
7975  */
7976     static int
7977 au_new_group(name)
7978     char_u	*name;
7979 {
7980     int		i;
7981 
7982     i = au_find_group(name);
7983     if (i == AUGROUP_ERROR)	/* the group doesn't exist yet, add it */
7984     {
7985 	/* First try using a free entry. */
7986 	for (i = 0; i < augroups.ga_len; ++i)
7987 	    if (AUGROUP_NAME(i) == NULL)
7988 		break;
7989 	if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL)
7990 	    return AUGROUP_ERROR;
7991 
7992 	AUGROUP_NAME(i) = vim_strsave(name);
7993 	if (AUGROUP_NAME(i) == NULL)
7994 	    return AUGROUP_ERROR;
7995 	if (i == augroups.ga_len)
7996 	    ++augroups.ga_len;
7997     }
7998 
7999     return i;
8000 }
8001 
8002     static void
8003 au_del_group(name)
8004     char_u	*name;
8005 {
8006     int	    i;
8007 
8008     i = au_find_group(name);
8009     if (i == AUGROUP_ERROR)	/* the group doesn't exist */
8010 	EMSG2(_("E367: No such group: \"%s\""), name);
8011     else
8012     {
8013 	vim_free(AUGROUP_NAME(i));
8014 	AUGROUP_NAME(i) = NULL;
8015     }
8016 }
8017 
8018 /*
8019  * Find the ID of an autocmd group name.
8020  * Return it's ID.  Returns AUGROUP_ERROR (< 0) for error.
8021  */
8022     static int
8023 au_find_group(name)
8024     char_u	*name;
8025 {
8026     int	    i;
8027 
8028     for (i = 0; i < augroups.ga_len; ++i)
8029 	if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0)
8030 	    return i;
8031     return AUGROUP_ERROR;
8032 }
8033 
8034 /*
8035  * Return TRUE if augroup "name" exists.
8036  */
8037     int
8038 au_has_group(name)
8039     char_u	*name;
8040 {
8041     return au_find_group(name) != AUGROUP_ERROR;
8042 }
8043 
8044 /*
8045  * ":augroup {name}".
8046  */
8047     void
8048 do_augroup(arg, del_group)
8049     char_u	*arg;
8050     int		del_group;
8051 {
8052     int	    i;
8053 
8054     if (del_group)
8055     {
8056 	if (*arg == NUL)
8057 	    EMSG(_(e_argreq));
8058 	else
8059 	    au_del_group(arg);
8060     }
8061     else if (STRICMP(arg, "end") == 0)   /* ":aug end": back to group 0 */
8062 	current_augroup = AUGROUP_DEFAULT;
8063     else if (*arg)		    /* ":aug xxx": switch to group xxx */
8064     {
8065 	i = au_new_group(arg);
8066 	if (i != AUGROUP_ERROR)
8067 	    current_augroup = i;
8068     }
8069     else			    /* ":aug": list the group names */
8070     {
8071 	msg_start();
8072 	for (i = 0; i < augroups.ga_len; ++i)
8073 	{
8074 	    if (AUGROUP_NAME(i) != NULL)
8075 	    {
8076 		msg_puts(AUGROUP_NAME(i));
8077 		msg_puts((char_u *)"  ");
8078 	    }
8079 	}
8080 	msg_clr_eos();
8081 	msg_end();
8082     }
8083 }
8084 
8085 #if defined(EXITFREE) || defined(PROTO)
8086     void
8087 free_all_autocmds()
8088 {
8089     for (current_augroup = -1; current_augroup < augroups.ga_len;
8090 							    ++current_augroup)
8091 	do_autocmd((char_u *)"", TRUE);
8092     ga_clear_strings(&augroups);
8093 }
8094 #endif
8095 
8096 /*
8097  * Return the event number for event name "start".
8098  * Return NUM_EVENTS if the event name was not found.
8099  * Return a pointer to the next event name in "end".
8100  */
8101     static event_T
8102 event_name2nr(start, end)
8103     char_u  *start;
8104     char_u  **end;
8105 {
8106     char_u	*p;
8107     int		i;
8108     int		len;
8109 
8110     /* the event name ends with end of line, a blank or a comma */
8111     for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p)
8112 	;
8113     for (i = 0; event_names[i].name != NULL; ++i)
8114     {
8115 	len = (int)STRLEN(event_names[i].name);
8116 	if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0)
8117 	    break;
8118     }
8119     if (*p == ',')
8120 	++p;
8121     *end = p;
8122     if (event_names[i].name == NULL)
8123 	return NUM_EVENTS;
8124     return event_names[i].event;
8125 }
8126 
8127 /*
8128  * Return the name for event "event".
8129  */
8130     static char_u *
8131 event_nr2name(event)
8132     event_T	event;
8133 {
8134     int	    i;
8135 
8136     for (i = 0; event_names[i].name != NULL; ++i)
8137 	if (event_names[i].event == event)
8138 	    return (char_u *)event_names[i].name;
8139     return (char_u *)"Unknown";
8140 }
8141 
8142 /*
8143  * Scan over the events.  "*" stands for all events.
8144  */
8145     static char_u *
8146 find_end_event(arg, have_group)
8147     char_u  *arg;
8148     int	    have_group;	    /* TRUE when group name was found */
8149 {
8150     char_u  *pat;
8151     char_u  *p;
8152 
8153     if (*arg == '*')
8154     {
8155 	if (arg[1] && !vim_iswhite(arg[1]))
8156 	{
8157 	    EMSG2(_("E215: Illegal character after *: %s"), arg);
8158 	    return NULL;
8159 	}
8160 	pat = arg + 1;
8161     }
8162     else
8163     {
8164 	for (pat = arg; *pat && !vim_iswhite(*pat); pat = p)
8165 	{
8166 	    if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS)
8167 	    {
8168 		if (have_group)
8169 		    EMSG2(_("E216: No such event: %s"), pat);
8170 		else
8171 		    EMSG2(_("E216: No such group or event: %s"), pat);
8172 		return NULL;
8173 	    }
8174 	}
8175     }
8176     return pat;
8177 }
8178 
8179 /*
8180  * Return TRUE if "event" is included in 'eventignore'.
8181  */
8182     static int
8183 event_ignored(event)
8184     event_T	event;
8185 {
8186     char_u	*p = p_ei;
8187 
8188     while (*p != NUL)
8189     {
8190 	if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
8191 	    return TRUE;
8192 	if (event_name2nr(p, &p) == event)
8193 	    return TRUE;
8194     }
8195 
8196     return FALSE;
8197 }
8198 
8199 /*
8200  * Return OK when the contents of p_ei is valid, FAIL otherwise.
8201  */
8202     int
8203 check_ei()
8204 {
8205     char_u	*p = p_ei;
8206 
8207     while (*p)
8208     {
8209 	if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
8210 	{
8211 	    p += 3;
8212 	    if (*p == ',')
8213 		++p;
8214 	}
8215 	else if (event_name2nr(p, &p) == NUM_EVENTS)
8216 	    return FAIL;
8217     }
8218 
8219     return OK;
8220 }
8221 
8222 # if defined(FEAT_SYN_HL) || defined(PROTO)
8223 
8224 /*
8225  * Add "what" to 'eventignore' to skip loading syntax highlighting for every
8226  * buffer loaded into the window.  "what" must start with a comma.
8227  * Returns the old value of 'eventignore' in allocated memory.
8228  */
8229     char_u *
8230 au_event_disable(what)
8231     char	*what;
8232 {
8233     char_u	*new_ei;
8234     char_u	*save_ei;
8235 
8236     save_ei = vim_strsave(p_ei);
8237     if (save_ei != NULL)
8238     {
8239 	new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what)));
8240 	if (new_ei != NULL)
8241 	{
8242 	    if (*what == ',' && *p_ei == NUL)
8243 		STRCPY(new_ei, what + 1);
8244 	    else
8245 		STRCAT(new_ei, what);
8246 	    set_string_option_direct((char_u *)"ei", -1, new_ei,
8247 							  OPT_FREE, SID_NONE);
8248 	    vim_free(new_ei);
8249 	}
8250     }
8251     return save_ei;
8252 }
8253 
8254     void
8255 au_event_restore(old_ei)
8256     char_u	*old_ei;
8257 {
8258     if (old_ei != NULL)
8259     {
8260 	set_string_option_direct((char_u *)"ei", -1, old_ei,
8261 							  OPT_FREE, SID_NONE);
8262 	vim_free(old_ei);
8263     }
8264 }
8265 # endif  /* FEAT_SYN_HL */
8266 
8267 /*
8268  * do_autocmd() -- implements the :autocmd command.  Can be used in the
8269  *  following ways:
8270  *
8271  * :autocmd <event> <pat> <cmd>	    Add <cmd> to the list of commands that
8272  *				    will be automatically executed for <event>
8273  *				    when editing a file matching <pat>, in
8274  *				    the current group.
8275  * :autocmd <event> <pat>	    Show the auto-commands associated with
8276  *				    <event> and <pat>.
8277  * :autocmd <event>		    Show the auto-commands associated with
8278  *				    <event>.
8279  * :autocmd			    Show all auto-commands.
8280  * :autocmd! <event> <pat> <cmd>    Remove all auto-commands associated with
8281  *				    <event> and <pat>, and add the command
8282  *				    <cmd>, for the current group.
8283  * :autocmd! <event> <pat>	    Remove all auto-commands associated with
8284  *				    <event> and <pat> for the current group.
8285  * :autocmd! <event>		    Remove all auto-commands associated with
8286  *				    <event> for the current group.
8287  * :autocmd!			    Remove ALL auto-commands for the current
8288  *				    group.
8289  *
8290  *  Multiple events and patterns may be given separated by commas.  Here are
8291  *  some examples:
8292  * :autocmd bufread,bufenter *.c,*.h	set tw=0 smartindent noic
8293  * :autocmd bufleave	     *		set tw=79 nosmartindent ic infercase
8294  *
8295  * :autocmd * *.c		show all autocommands for *.c files.
8296  *
8297  * Mostly a {group} argument can optionally appear before <event>.
8298  */
8299     void
8300 do_autocmd(arg, forceit)
8301     char_u  *arg;
8302     int	    forceit;
8303 {
8304     char_u	*pat;
8305     char_u	*envpat = NULL;
8306     char_u	*cmd;
8307     event_T	event;
8308     int		need_free = FALSE;
8309     int		nested = FALSE;
8310     int		group;
8311 
8312     /*
8313      * Check for a legal group name.  If not, use AUGROUP_ALL.
8314      */
8315     group = au_get_grouparg(&arg);
8316     if (arg == NULL)	    /* out of memory */
8317 	return;
8318 
8319     /*
8320      * Scan over the events.
8321      * If we find an illegal name, return here, don't do anything.
8322      */
8323     pat = find_end_event(arg, group != AUGROUP_ALL);
8324     if (pat == NULL)
8325 	return;
8326 
8327     /*
8328      * Scan over the pattern.  Put a NUL at the end.
8329      */
8330     pat = skipwhite(pat);
8331     cmd = pat;
8332     while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
8333 	cmd++;
8334     if (*cmd)
8335 	*cmd++ = NUL;
8336 
8337     /* Expand environment variables in the pattern.  Set 'shellslash', we want
8338      * forward slashes here. */
8339     if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
8340     {
8341 #ifdef BACKSLASH_IN_FILENAME
8342 	int	p_ssl_save = p_ssl;
8343 
8344 	p_ssl = TRUE;
8345 #endif
8346 	envpat = expand_env_save(pat);
8347 #ifdef BACKSLASH_IN_FILENAME
8348 	p_ssl = p_ssl_save;
8349 #endif
8350 	if (envpat != NULL)
8351 	    pat = envpat;
8352     }
8353 
8354     /*
8355      * Check for "nested" flag.
8356      */
8357     cmd = skipwhite(cmd);
8358     if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6]))
8359     {
8360 	nested = TRUE;
8361 	cmd = skipwhite(cmd + 6);
8362     }
8363 
8364     /*
8365      * Find the start of the commands.
8366      * Expand <sfile> in it.
8367      */
8368     if (*cmd != NUL)
8369     {
8370 	cmd = expand_sfile(cmd);
8371 	if (cmd == NULL)	    /* some error */
8372 	    return;
8373 	need_free = TRUE;
8374     }
8375 
8376     /*
8377      * Print header when showing autocommands.
8378      */
8379     if (!forceit && *cmd == NUL)
8380     {
8381 	/* Highlight title */
8382 	MSG_PUTS_TITLE(_("\n--- Auto-Commands ---"));
8383     }
8384 
8385     /*
8386      * Loop over the events.
8387      */
8388     last_event = (event_T)-1;		/* for listing the event name */
8389     last_group = AUGROUP_ERROR;		/* for listing the group name */
8390     if (*arg == '*' || *arg == NUL)
8391     {
8392 	for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
8393 					    event = (event_T)((int)event + 1))
8394 	    if (do_autocmd_event(event, pat,
8395 					 nested, cmd, forceit, group) == FAIL)
8396 		break;
8397     }
8398     else
8399     {
8400 	while (*arg && !vim_iswhite(*arg))
8401 	    if (do_autocmd_event(event_name2nr(arg, &arg), pat,
8402 					nested,	cmd, forceit, group) == FAIL)
8403 		break;
8404     }
8405 
8406     if (need_free)
8407 	vim_free(cmd);
8408     vim_free(envpat);
8409 }
8410 
8411 /*
8412  * Find the group ID in a ":autocmd" or ":doautocmd" argument.
8413  * The "argp" argument is advanced to the following argument.
8414  *
8415  * Returns the group ID, AUGROUP_ERROR for error (out of memory).
8416  */
8417     static int
8418 au_get_grouparg(argp)
8419     char_u	**argp;
8420 {
8421     char_u	*group_name;
8422     char_u	*p;
8423     char_u	*arg = *argp;
8424     int		group = AUGROUP_ALL;
8425 
8426     p = skiptowhite(arg);
8427     if (p > arg)
8428     {
8429 	group_name = vim_strnsave(arg, (int)(p - arg));
8430 	if (group_name == NULL)		/* out of memory */
8431 	    return AUGROUP_ERROR;
8432 	group = au_find_group(group_name);
8433 	if (group == AUGROUP_ERROR)
8434 	    group = AUGROUP_ALL;	/* no match, use all groups */
8435 	else
8436 	    *argp = skipwhite(p);	/* match, skip over group name */
8437 	vim_free(group_name);
8438     }
8439     return group;
8440 }
8441 
8442 /*
8443  * do_autocmd() for one event.
8444  * If *pat == NUL do for all patterns.
8445  * If *cmd == NUL show entries.
8446  * If forceit == TRUE delete entries.
8447  * If group is not AUGROUP_ALL, only use this group.
8448  */
8449     static int
8450 do_autocmd_event(event, pat, nested, cmd, forceit, group)
8451     event_T	event;
8452     char_u	*pat;
8453     int		nested;
8454     char_u	*cmd;
8455     int		forceit;
8456     int		group;
8457 {
8458     AutoPat	*ap;
8459     AutoPat	**prev_ap;
8460     AutoCmd	*ac;
8461     AutoCmd	**prev_ac;
8462     int		brace_level;
8463     char_u	*endpat;
8464     int		findgroup;
8465     int		allgroups;
8466     int		patlen;
8467     int		is_buflocal;
8468     int		buflocal_nr;
8469     char_u	buflocal_pat[25];	/* for "<buffer=X>" */
8470 
8471     if (group == AUGROUP_ALL)
8472 	findgroup = current_augroup;
8473     else
8474 	findgroup = group;
8475     allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL);
8476 
8477     /*
8478      * Show or delete all patterns for an event.
8479      */
8480     if (*pat == NUL)
8481     {
8482 	for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
8483 	{
8484 	    if (forceit)  /* delete the AutoPat, if it's in the current group */
8485 	    {
8486 		if (ap->group == findgroup)
8487 		    au_remove_pat(ap);
8488 	    }
8489 	    else if (group == AUGROUP_ALL || ap->group == group)
8490 		show_autocmd(ap, event);
8491 	}
8492     }
8493 
8494     /*
8495      * Loop through all the specified patterns.
8496      */
8497     for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat))
8498     {
8499 	/*
8500 	 * Find end of the pattern.
8501 	 * Watch out for a comma in braces, like "*.\{obj,o\}".
8502 	 */
8503 	brace_level = 0;
8504 	for (endpat = pat; *endpat && (*endpat != ',' || brace_level
8505 					     || endpat[-1] == '\\'); ++endpat)
8506 	{
8507 	    if (*endpat == '{')
8508 		brace_level++;
8509 	    else if (*endpat == '}')
8510 		brace_level--;
8511 	}
8512 	if (pat == endpat)		/* ignore single comma */
8513 	    continue;
8514 	patlen = (int)(endpat - pat);
8515 
8516 	/*
8517 	 * detect special <buflocal[=X]> buffer-local patterns
8518 	 */
8519 	is_buflocal = FALSE;
8520 	buflocal_nr = 0;
8521 
8522 	if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0
8523 						    && pat[patlen - 1] == '>')
8524 	{
8525 	    /* Error will be printed only for addition. printing and removing
8526 	     * will proceed silently. */
8527 	    is_buflocal = TRUE;
8528 	    if (patlen == 8)
8529 		buflocal_nr = curbuf->b_fnum;
8530 	    else if (patlen > 9 && pat[7] == '=')
8531 	    {
8532 		/* <buffer=abuf> */
8533 		if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13))
8534 		    buflocal_nr = autocmd_bufnr;
8535 		/* <buffer=123> */
8536 		else if (skipdigits(pat + 8) == pat + patlen - 1)
8537 		    buflocal_nr = atoi((char *)pat + 8);
8538 	    }
8539 	}
8540 
8541 	if (is_buflocal)
8542 	{
8543 	    /* normalize pat into standard "<buffer>#N" form */
8544 	    sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr);
8545 	    pat = buflocal_pat;			/* can modify pat and patlen */
8546 	    patlen = (int)STRLEN(buflocal_pat);	/*   but not endpat */
8547 	}
8548 
8549 	/*
8550 	 * Find AutoPat entries with this pattern.
8551 	 */
8552 	prev_ap = &first_autopat[(int)event];
8553 	while ((ap = *prev_ap) != NULL)
8554 	{
8555 	    if (ap->pat != NULL)
8556 	    {
8557 		/* Accept a pattern when:
8558 		 * - a group was specified and it's that group, or a group was
8559 		 *   not specified and it's the current group, or a group was
8560 		 *   not specified and we are listing
8561 		 * - the length of the pattern matches
8562 		 * - the pattern matches.
8563 		 * For <buffer[=X]>, this condition works because we normalize
8564 		 * all buffer-local patterns.
8565 		 */
8566 		if ((allgroups || ap->group == findgroup)
8567 			&& ap->patlen == patlen
8568 			&& STRNCMP(pat, ap->pat, patlen) == 0)
8569 		{
8570 		    /*
8571 		     * Remove existing autocommands.
8572 		     * If adding any new autocmd's for this AutoPat, don't
8573 		     * delete the pattern from the autopat list, append to
8574 		     * this list.
8575 		     */
8576 		    if (forceit)
8577 		    {
8578 			if (*cmd != NUL && ap->next == NULL)
8579 			{
8580 			    au_remove_cmds(ap);
8581 			    break;
8582 			}
8583 			au_remove_pat(ap);
8584 		    }
8585 
8586 		    /*
8587 		     * Show autocmd's for this autopat, or buflocals <buffer=X>
8588 		     */
8589 		    else if (*cmd == NUL)
8590 			show_autocmd(ap, event);
8591 
8592 		    /*
8593 		     * Add autocmd to this autopat, if it's the last one.
8594 		     */
8595 		    else if (ap->next == NULL)
8596 			break;
8597 		}
8598 	    }
8599 	    prev_ap = &ap->next;
8600 	}
8601 
8602 	/*
8603 	 * Add a new command.
8604 	 */
8605 	if (*cmd != NUL)
8606 	{
8607 	    /*
8608 	     * If the pattern we want to add a command to does appear at the
8609 	     * end of the list (or not is not in the list at all), add the
8610 	     * pattern at the end of the list.
8611 	     */
8612 	    if (ap == NULL)
8613 	    {
8614 		/* refuse to add buffer-local ap if buffer number is invalid */
8615 		if (is_buflocal && (buflocal_nr == 0
8616 				      || buflist_findnr(buflocal_nr) == NULL))
8617 		{
8618 		    EMSGN(_("E680: <buffer=%d>: invalid buffer number "),
8619 								 buflocal_nr);
8620 		    return FAIL;
8621 		}
8622 
8623 		ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat));
8624 		if (ap == NULL)
8625 		    return FAIL;
8626 		ap->pat = vim_strnsave(pat, patlen);
8627 		ap->patlen = patlen;
8628 		if (ap->pat == NULL)
8629 		{
8630 		    vim_free(ap);
8631 		    return FAIL;
8632 		}
8633 
8634 		if (is_buflocal)
8635 		{
8636 		    ap->buflocal_nr = buflocal_nr;
8637 		    ap->reg_prog = NULL;
8638 		}
8639 		else
8640 		{
8641 		    char_u	*reg_pat;
8642 
8643 		    ap->buflocal_nr = 0;
8644 		    reg_pat = file_pat_to_reg_pat(pat, endpat,
8645 							 &ap->allow_dirs, TRUE);
8646 		    if (reg_pat != NULL)
8647 			ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC);
8648 		    vim_free(reg_pat);
8649 		    if (reg_pat == NULL || ap->reg_prog == NULL)
8650 		    {
8651 			vim_free(ap->pat);
8652 			vim_free(ap);
8653 			return FAIL;
8654 		    }
8655 		}
8656 		ap->cmds = NULL;
8657 		*prev_ap = ap;
8658 		ap->next = NULL;
8659 		if (group == AUGROUP_ALL)
8660 		    ap->group = current_augroup;
8661 		else
8662 		    ap->group = group;
8663 	    }
8664 
8665 	    /*
8666 	     * Add the autocmd at the end of the AutoCmd list.
8667 	     */
8668 	    prev_ac = &(ap->cmds);
8669 	    while ((ac = *prev_ac) != NULL)
8670 		prev_ac = &ac->next;
8671 	    ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd));
8672 	    if (ac == NULL)
8673 		return FAIL;
8674 	    ac->cmd = vim_strsave(cmd);
8675 #ifdef FEAT_EVAL
8676 	    ac->scriptID = current_SID;
8677 #endif
8678 	    if (ac->cmd == NULL)
8679 	    {
8680 		vim_free(ac);
8681 		return FAIL;
8682 	    }
8683 	    ac->next = NULL;
8684 	    *prev_ac = ac;
8685 	    ac->nested = nested;
8686 	}
8687     }
8688 
8689     au_cleanup();	/* may really delete removed patterns/commands now */
8690     return OK;
8691 }
8692 
8693 /*
8694  * Implementation of ":doautocmd [group] event [fname]".
8695  * Return OK for success, FAIL for failure;
8696  */
8697     int
8698 do_doautocmd(arg, do_msg)
8699     char_u	*arg;
8700     int		do_msg;	    /* give message for no matching autocmds? */
8701 {
8702     char_u	*fname;
8703     int		nothing_done = TRUE;
8704     int		group;
8705 
8706     /*
8707      * Check for a legal group name.  If not, use AUGROUP_ALL.
8708      */
8709     group = au_get_grouparg(&arg);
8710     if (arg == NULL)	    /* out of memory */
8711 	return FAIL;
8712 
8713     if (*arg == '*')
8714     {
8715 	EMSG(_("E217: Can't execute autocommands for ALL events"));
8716 	return FAIL;
8717     }
8718 
8719     /*
8720      * Scan over the events.
8721      * If we find an illegal name, return here, don't do anything.
8722      */
8723     fname = find_end_event(arg, group != AUGROUP_ALL);
8724     if (fname == NULL)
8725 	return FAIL;
8726 
8727     fname = skipwhite(fname);
8728 
8729     /*
8730      * Loop over the events.
8731      */
8732     while (*arg && !vim_iswhite(*arg))
8733 	if (apply_autocmds_group(event_name2nr(arg, &arg),
8734 				      fname, NULL, TRUE, group, curbuf, NULL))
8735 	    nothing_done = FALSE;
8736 
8737     if (nothing_done && do_msg)
8738 	MSG(_("No matching autocommands"));
8739 
8740 #ifdef FEAT_EVAL
8741     return aborting() ? FAIL : OK;
8742 #else
8743     return OK;
8744 #endif
8745 }
8746 
8747 /*
8748  * ":doautoall": execute autocommands for each loaded buffer.
8749  */
8750     void
8751 ex_doautoall(eap)
8752     exarg_T	*eap;
8753 {
8754     int		retval;
8755     aco_save_T	aco;
8756     buf_T	*buf;
8757     char_u	*arg = eap->arg;
8758     int		call_do_modelines = check_nomodeline(&arg);
8759 
8760     /*
8761      * This is a bit tricky: For some commands curwin->w_buffer needs to be
8762      * equal to curbuf, but for some buffers there may not be a window.
8763      * So we change the buffer for the current window for a moment.  This
8764      * gives problems when the autocommands make changes to the list of
8765      * buffers or windows...
8766      */
8767     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8768     {
8769 	if (buf->b_ml.ml_mfp != NULL)
8770 	{
8771 	    /* find a window for this buffer and save some values */
8772 	    aucmd_prepbuf(&aco, buf);
8773 
8774 	    /* execute the autocommands for this buffer */
8775 	    retval = do_doautocmd(arg, FALSE);
8776 
8777 	    if (call_do_modelines)
8778 	    {
8779 		/* Execute the modeline settings, but don't set window-local
8780 		 * options if we are using the current window for another
8781 		 * buffer. */
8782 		do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0);
8783 	    }
8784 
8785 	    /* restore the current window */
8786 	    aucmd_restbuf(&aco);
8787 
8788 	    /* stop if there is some error or buffer was deleted */
8789 	    if (retval == FAIL || !buf_valid(buf))
8790 		break;
8791 	}
8792     }
8793 
8794     check_cursor();	    /* just in case lines got deleted */
8795 }
8796 
8797 /*
8798  * Check *argp for <nomodeline>.  When it is present return FALSE, otherwise
8799  * return TRUE and advance *argp to after it.
8800  * Thus return TRUE when do_modelines() should be called.
8801  */
8802     int
8803 check_nomodeline(argp)
8804     char_u **argp;
8805 {
8806     if (STRNCMP(*argp, "<nomodeline>", 12) == 0)
8807     {
8808 	*argp = skipwhite(*argp + 12);
8809 	return FALSE;
8810     }
8811     return TRUE;
8812 }
8813 
8814 /*
8815  * Prepare for executing autocommands for (hidden) buffer "buf".
8816  * Search for a visible window containing the current buffer.  If there isn't
8817  * one then use "aucmd_win".
8818  * Set "curbuf" and "curwin" to match "buf".
8819  * When FEAT_AUTOCMD is not defined another version is used, see below.
8820  */
8821     void
8822 aucmd_prepbuf(aco, buf)
8823     aco_save_T	*aco;		/* structure to save values in */
8824     buf_T	*buf;		/* new curbuf */
8825 {
8826     win_T	*win;
8827 #ifdef FEAT_WINDOWS
8828     int		save_ea;
8829 #endif
8830 
8831     /* Find a window that is for the new buffer */
8832     if (buf == curbuf)		/* be quick when buf is curbuf */
8833 	win = curwin;
8834     else
8835 #ifdef FEAT_WINDOWS
8836 	for (win = firstwin; win != NULL; win = win->w_next)
8837 	    if (win->w_buffer == buf)
8838 		break;
8839 #else
8840 	win = NULL;
8841 #endif
8842 
8843     /* Allocate "aucmd_win" when needed.  If this fails (out of memory) fall
8844      * back to using the current window. */
8845     if (win == NULL && aucmd_win == NULL)
8846     {
8847 	win_alloc_aucmd_win();
8848 	if (aucmd_win == NULL)
8849 	    win = curwin;
8850     }
8851     if (win == NULL && aucmd_win_used)
8852 	/* Strange recursive autocommand, fall back to using the current
8853 	 * window.  Expect a few side effects... */
8854 	win = curwin;
8855 
8856     aco->save_curwin = curwin;
8857     aco->save_curbuf = curbuf;
8858     if (win != NULL)
8859     {
8860 	/* There is a window for "buf" in the current tab page, make it the
8861 	 * curwin.  This is preferred, it has the least side effects (esp. if
8862 	 * "buf" is curbuf). */
8863 	aco->use_aucmd_win = FALSE;
8864 	curwin = win;
8865     }
8866     else
8867     {
8868 	/* There is no window for "buf", use "aucmd_win".  To minimize the side
8869 	 * effects, insert it in a the current tab page.
8870 	 * Anything related to a window (e.g., setting folds) may have
8871 	 * unexpected results. */
8872 	aco->use_aucmd_win = TRUE;
8873 	aucmd_win_used = TRUE;
8874 	aucmd_win->w_buffer = buf;
8875 	aucmd_win->w_s = &buf->b_s;
8876 	++buf->b_nwindows;
8877 	win_init_empty(aucmd_win); /* set cursor and topline to safe values */
8878 	vim_free(aucmd_win->w_localdir);
8879 	aucmd_win->w_localdir = NULL;
8880 
8881 	/* Make sure w_localdir and globaldir are NULL to avoid a chdir() in
8882 	 * win_enter_ext(). */
8883 	aucmd_win->w_localdir = NULL;
8884 	aco->globaldir = globaldir;
8885 	globaldir = NULL;
8886 
8887 
8888 #ifdef FEAT_WINDOWS
8889 	/* Split the current window, put the aucmd_win in the upper half.
8890 	 * We don't want the BufEnter or WinEnter autocommands. */
8891 	block_autocmds();
8892 	make_snapshot(SNAP_AUCMD_IDX);
8893 	save_ea = p_ea;
8894 	p_ea = FALSE;
8895 	(void)win_split_ins(0, WSP_TOP, aucmd_win, 0);
8896 	(void)win_comp_pos();   /* recompute window positions */
8897 	p_ea = save_ea;
8898 	unblock_autocmds();
8899 #endif
8900 	curwin = aucmd_win;
8901     }
8902     curbuf = buf;
8903     aco->new_curwin = curwin;
8904     aco->new_curbuf = curbuf;
8905 }
8906 
8907 /*
8908  * Cleanup after executing autocommands for a (hidden) buffer.
8909  * Restore the window as it was (if possible).
8910  * When FEAT_AUTOCMD is not defined another version is used, see below.
8911  */
8912     void
8913 aucmd_restbuf(aco)
8914     aco_save_T	*aco;		/* structure holding saved values */
8915 {
8916 #ifdef FEAT_WINDOWS
8917     int dummy;
8918 #endif
8919 
8920     if (aco->use_aucmd_win)
8921     {
8922 	--curbuf->b_nwindows;
8923 #ifdef FEAT_WINDOWS
8924 	/* Find "aucmd_win", it can't be closed, but it may be in another tab
8925 	 * page. Do not trigger autocommands here. */
8926 	block_autocmds();
8927 	if (curwin != aucmd_win)
8928 	{
8929 	    tabpage_T	*tp;
8930 	    win_T	*wp;
8931 
8932 	    FOR_ALL_TAB_WINDOWS(tp, wp)
8933 	    {
8934 		if (wp == aucmd_win)
8935 		{
8936 		    if (tp != curtab)
8937 			goto_tabpage_tp(tp, TRUE);
8938 		    win_goto(aucmd_win);
8939 		    goto win_found;
8940 		}
8941 	    }
8942 	}
8943 win_found:
8944 
8945 	/* Remove the window and frame from the tree of frames. */
8946 	(void)winframe_remove(curwin, &dummy, NULL);
8947 	win_remove(curwin, NULL);
8948 	aucmd_win_used = FALSE;
8949 	last_status(FALSE);	    /* may need to remove last status line */
8950 	restore_snapshot(SNAP_AUCMD_IDX, FALSE);
8951 	(void)win_comp_pos();   /* recompute window positions */
8952 	unblock_autocmds();
8953 
8954 	if (win_valid(aco->save_curwin))
8955 	    curwin = aco->save_curwin;
8956 	else
8957 	    /* Hmm, original window disappeared.  Just use the first one. */
8958 	    curwin = firstwin;
8959 # ifdef FEAT_EVAL
8960 	vars_clear(&aucmd_win->w_vars.dv_hashtab);  /* free all w: variables */
8961 	hash_init(&aucmd_win->w_vars.dv_hashtab);   /* re-use the hashtab */
8962 # endif
8963 #else
8964 	curwin = aco->save_curwin;
8965 #endif
8966 	curbuf = curwin->w_buffer;
8967 
8968 	vim_free(globaldir);
8969 	globaldir = aco->globaldir;
8970 
8971 	/* the buffer contents may have changed */
8972 	check_cursor();
8973 	if (curwin->w_topline > curbuf->b_ml.ml_line_count)
8974 	{
8975 	    curwin->w_topline = curbuf->b_ml.ml_line_count;
8976 #ifdef FEAT_DIFF
8977 	    curwin->w_topfill = 0;
8978 #endif
8979 	}
8980 #if defined(FEAT_GUI)
8981 	/* Hide the scrollbars from the aucmd_win and update. */
8982 	gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE);
8983 	gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE);
8984 	gui_may_update_scrollbars();
8985 #endif
8986     }
8987     else
8988     {
8989 	/* restore curwin */
8990 #ifdef FEAT_WINDOWS
8991 	if (win_valid(aco->save_curwin))
8992 #endif
8993 	{
8994 	    /* Restore the buffer which was previously edited by curwin, if
8995 	     * it was changed, we are still the same window and the buffer is
8996 	     * valid. */
8997 	    if (curwin == aco->new_curwin
8998 		    && curbuf != aco->new_curbuf
8999 		    && buf_valid(aco->new_curbuf)
9000 		    && aco->new_curbuf->b_ml.ml_mfp != NULL)
9001 	    {
9002 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
9003 		if (curwin->w_s == &curbuf->b_s)
9004 		    curwin->w_s = &aco->new_curbuf->b_s;
9005 # endif
9006 		--curbuf->b_nwindows;
9007 		curbuf = aco->new_curbuf;
9008 		curwin->w_buffer = curbuf;
9009 		++curbuf->b_nwindows;
9010 	    }
9011 
9012 	    curwin = aco->save_curwin;
9013 	    curbuf = curwin->w_buffer;
9014 	}
9015     }
9016 }
9017 
9018 static int	autocmd_nested = FALSE;
9019 
9020 /*
9021  * Execute autocommands for "event" and file name "fname".
9022  * Return TRUE if some commands were executed.
9023  */
9024     int
9025 apply_autocmds(event, fname, fname_io, force, buf)
9026     event_T	event;
9027     char_u	*fname;	    /* NULL or empty means use actual file name */
9028     char_u	*fname_io;  /* fname to use for <afile> on cmdline */
9029     int		force;	    /* when TRUE, ignore autocmd_busy */
9030     buf_T	*buf;	    /* buffer for <abuf> */
9031 {
9032     return apply_autocmds_group(event, fname, fname_io, force,
9033 						      AUGROUP_ALL, buf, NULL);
9034 }
9035 
9036 /*
9037  * Like apply_autocmds(), but with extra "eap" argument.  This takes care of
9038  * setting v:filearg.
9039  */
9040     static int
9041 apply_autocmds_exarg(event, fname, fname_io, force, buf, eap)
9042     event_T	event;
9043     char_u	*fname;
9044     char_u	*fname_io;
9045     int		force;
9046     buf_T	*buf;
9047     exarg_T	*eap;
9048 {
9049     return apply_autocmds_group(event, fname, fname_io, force,
9050 						       AUGROUP_ALL, buf, eap);
9051 }
9052 
9053 /*
9054  * Like apply_autocmds(), but handles the caller's retval.  If the script
9055  * processing is being aborted or if retval is FAIL when inside a try
9056  * conditional, no autocommands are executed.  If otherwise the autocommands
9057  * cause the script to be aborted, retval is set to FAIL.
9058  */
9059     int
9060 apply_autocmds_retval(event, fname, fname_io, force, buf, retval)
9061     event_T	event;
9062     char_u	*fname;	    /* NULL or empty means use actual file name */
9063     char_u	*fname_io;  /* fname to use for <afile> on cmdline */
9064     int		force;	    /* when TRUE, ignore autocmd_busy */
9065     buf_T	*buf;	    /* buffer for <abuf> */
9066     int		*retval;    /* pointer to caller's retval */
9067 {
9068     int		did_cmd;
9069 
9070 #ifdef FEAT_EVAL
9071     if (should_abort(*retval))
9072 	return FALSE;
9073 #endif
9074 
9075     did_cmd = apply_autocmds_group(event, fname, fname_io, force,
9076 						      AUGROUP_ALL, buf, NULL);
9077     if (did_cmd
9078 #ifdef FEAT_EVAL
9079 	    && aborting()
9080 #endif
9081 	    )
9082 	*retval = FAIL;
9083     return did_cmd;
9084 }
9085 
9086 /*
9087  * Return TRUE when there is a CursorHold autocommand defined.
9088  */
9089     int
9090 has_cursorhold()
9091 {
9092     return (first_autopat[(int)(get_real_state() == NORMAL_BUSY
9093 			    ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL);
9094 }
9095 
9096 /*
9097  * Return TRUE if the CursorHold event can be triggered.
9098  */
9099     int
9100 trigger_cursorhold()
9101 {
9102     int		state;
9103 
9104     if (!did_cursorhold
9105 	    && has_cursorhold()
9106 	    && !Recording
9107 	    && typebuf.tb_len == 0
9108 #ifdef FEAT_INS_EXPAND
9109 	    && !ins_compl_active()
9110 #endif
9111 	    )
9112     {
9113 	state = get_real_state();
9114 	if (state == NORMAL_BUSY || (state & INSERT) != 0)
9115 	    return TRUE;
9116     }
9117     return FALSE;
9118 }
9119 
9120 /*
9121  * Return TRUE when there is a CursorMoved autocommand defined.
9122  */
9123     int
9124 has_cursormoved()
9125 {
9126     return (first_autopat[(int)EVENT_CURSORMOVED] != NULL);
9127 }
9128 
9129 /*
9130  * Return TRUE when there is a CursorMovedI autocommand defined.
9131  */
9132     int
9133 has_cursormovedI()
9134 {
9135     return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
9136 }
9137 
9138 /*
9139  * Return TRUE when there is an InsertCharPre autocommand defined.
9140  */
9141     int
9142 has_insertcharpre()
9143 {
9144     return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
9145 }
9146 
9147     static int
9148 apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
9149     event_T	event;
9150     char_u	*fname;	    /* NULL or empty means use actual file name */
9151     char_u	*fname_io;  /* fname to use for <afile> on cmdline, NULL means
9152 			       use fname */
9153     int		force;	    /* when TRUE, ignore autocmd_busy */
9154     int		group;	    /* group ID, or AUGROUP_ALL */
9155     buf_T	*buf;	    /* buffer for <abuf> */
9156     exarg_T	*eap;	    /* command arguments */
9157 {
9158     char_u	*sfname = NULL;	/* short file name */
9159     char_u	*tail;
9160     int		save_changed;
9161     buf_T	*old_curbuf;
9162     int		retval = FALSE;
9163     char_u	*save_sourcing_name;
9164     linenr_T	save_sourcing_lnum;
9165     char_u	*save_autocmd_fname;
9166     int		save_autocmd_fname_full;
9167     int		save_autocmd_bufnr;
9168     char_u	*save_autocmd_match;
9169     int		save_autocmd_busy;
9170     int		save_autocmd_nested;
9171     static int	nesting = 0;
9172     AutoPatCmd	patcmd;
9173     AutoPat	*ap;
9174 #ifdef FEAT_EVAL
9175     scid_T	save_current_SID;
9176     void	*save_funccalp;
9177     char_u	*save_cmdarg;
9178     long	save_cmdbang;
9179 #endif
9180     static int	filechangeshell_busy = FALSE;
9181 #ifdef FEAT_PROFILE
9182     proftime_T	wait_time;
9183 #endif
9184 
9185     /*
9186      * Quickly return if there are no autocommands for this event or
9187      * autocommands are blocked.
9188      */
9189     if (first_autopat[(int)event] == NULL || autocmd_blocked > 0)
9190 	goto BYPASS_AU;
9191 
9192     /*
9193      * When autocommands are busy, new autocommands are only executed when
9194      * explicitly enabled with the "nested" flag.
9195      */
9196     if (autocmd_busy && !(force || autocmd_nested))
9197 	goto BYPASS_AU;
9198 
9199 #ifdef FEAT_EVAL
9200     /*
9201      * Quickly return when immediately aborting on error, or when an interrupt
9202      * occurred or an exception was thrown but not caught.
9203      */
9204     if (aborting())
9205 	goto BYPASS_AU;
9206 #endif
9207 
9208     /*
9209      * FileChangedShell never nests, because it can create an endless loop.
9210      */
9211     if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL
9212 				      || event == EVENT_FILECHANGEDSHELLPOST))
9213 	goto BYPASS_AU;
9214 
9215     /*
9216      * Ignore events in 'eventignore'.
9217      */
9218     if (event_ignored(event))
9219 	goto BYPASS_AU;
9220 
9221     /*
9222      * Allow nesting of autocommands, but restrict the depth, because it's
9223      * possible to create an endless loop.
9224      */
9225     if (nesting == 10)
9226     {
9227 	EMSG(_("E218: autocommand nesting too deep"));
9228 	goto BYPASS_AU;
9229     }
9230 
9231     /*
9232      * Check if these autocommands are disabled.  Used when doing ":all" or
9233      * ":ball".
9234      */
9235     if (       (autocmd_no_enter
9236 		&& (event == EVENT_WINENTER || event == EVENT_BUFENTER))
9237 	    || (autocmd_no_leave
9238 		&& (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE)))
9239 	goto BYPASS_AU;
9240 
9241     /*
9242      * Save the autocmd_* variables and info about the current buffer.
9243      */
9244     save_autocmd_fname = autocmd_fname;
9245     save_autocmd_fname_full = autocmd_fname_full;
9246     save_autocmd_bufnr = autocmd_bufnr;
9247     save_autocmd_match = autocmd_match;
9248     save_autocmd_busy = autocmd_busy;
9249     save_autocmd_nested = autocmd_nested;
9250     save_changed = curbuf->b_changed;
9251     old_curbuf = curbuf;
9252 
9253     /*
9254      * Set the file name to be used for <afile>.
9255      * Make a copy to avoid that changing a buffer name or directory makes it
9256      * invalid.
9257      */
9258     if (fname_io == NULL)
9259     {
9260 	if (fname != NULL && *fname != NUL)
9261 	    autocmd_fname = fname;
9262 	else if (buf != NULL)
9263 	    autocmd_fname = buf->b_ffname;
9264 	else
9265 	    autocmd_fname = NULL;
9266     }
9267     else
9268 	autocmd_fname = fname_io;
9269     if (autocmd_fname != NULL)
9270 	autocmd_fname = vim_strsave(autocmd_fname);
9271     autocmd_fname_full = FALSE; /* call FullName_save() later */
9272 
9273     /*
9274      * Set the buffer number to be used for <abuf>.
9275      */
9276     if (buf == NULL)
9277 	autocmd_bufnr = 0;
9278     else
9279 	autocmd_bufnr = buf->b_fnum;
9280 
9281     /*
9282      * When the file name is NULL or empty, use the file name of buffer "buf".
9283      * Always use the full path of the file name to match with, in case
9284      * "allow_dirs" is set.
9285      */
9286     if (fname == NULL || *fname == NUL)
9287     {
9288 	if (buf == NULL)
9289 	    fname = NULL;
9290 	else
9291 	{
9292 #ifdef FEAT_SYN_HL
9293 	    if (event == EVENT_SYNTAX)
9294 		fname = buf->b_p_syn;
9295 	    else
9296 #endif
9297 		if (event == EVENT_FILETYPE)
9298 		    fname = buf->b_p_ft;
9299 		else
9300 		{
9301 		    if (buf->b_sfname != NULL)
9302 			sfname = vim_strsave(buf->b_sfname);
9303 		    fname = buf->b_ffname;
9304 		}
9305 	}
9306 	if (fname == NULL)
9307 	    fname = (char_u *)"";
9308 	fname = vim_strsave(fname);	/* make a copy, so we can change it */
9309     }
9310     else
9311     {
9312 	sfname = vim_strsave(fname);
9313 	/* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or
9314 	 * QuickFixCmd* */
9315 	if (event == EVENT_FILETYPE
9316 		|| event == EVENT_SYNTAX
9317 		|| event == EVENT_FUNCUNDEFINED
9318 		|| event == EVENT_REMOTEREPLY
9319 		|| event == EVENT_SPELLFILEMISSING
9320 		|| event == EVENT_QUICKFIXCMDPRE
9321 		|| event == EVENT_QUICKFIXCMDPOST)
9322 	    fname = vim_strsave(fname);
9323 	else
9324 	    fname = FullName_save(fname, FALSE);
9325     }
9326     if (fname == NULL)	    /* out of memory */
9327     {
9328 	vim_free(sfname);
9329 	retval = FALSE;
9330 	goto BYPASS_AU;
9331     }
9332 
9333 #ifdef BACKSLASH_IN_FILENAME
9334     /*
9335      * Replace all backslashes with forward slashes.  This makes the
9336      * autocommand patterns portable between Unix and MS-DOS.
9337      */
9338     if (sfname != NULL)
9339 	forward_slash(sfname);
9340     forward_slash(fname);
9341 #endif
9342 
9343 #ifdef VMS
9344     /* remove version for correct match */
9345     if (sfname != NULL)
9346 	vms_remove_version(sfname);
9347     vms_remove_version(fname);
9348 #endif
9349 
9350     /*
9351      * Set the name to be used for <amatch>.
9352      */
9353     autocmd_match = fname;
9354 
9355 
9356     /* Don't redraw while doing auto commands. */
9357     ++RedrawingDisabled;
9358     save_sourcing_name = sourcing_name;
9359     sourcing_name = NULL;	/* don't free this one */
9360     save_sourcing_lnum = sourcing_lnum;
9361     sourcing_lnum = 0;		/* no line number here */
9362 
9363 #ifdef FEAT_EVAL
9364     save_current_SID = current_SID;
9365 
9366 # ifdef FEAT_PROFILE
9367     if (do_profiling == PROF_YES)
9368 	prof_child_enter(&wait_time); /* doesn't count for the caller itself */
9369 # endif
9370 
9371     /* Don't use local function variables, if called from a function */
9372     save_funccalp = save_funccal();
9373 #endif
9374 
9375     /*
9376      * When starting to execute autocommands, save the search patterns.
9377      */
9378     if (!autocmd_busy)
9379     {
9380 	save_search_patterns();
9381 	saveRedobuff();
9382 	did_filetype = keep_filetype;
9383     }
9384 
9385     /*
9386      * Note that we are applying autocmds.  Some commands need to know.
9387      */
9388     autocmd_busy = TRUE;
9389     filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL);
9390     ++nesting;		/* see matching decrement below */
9391 
9392     /* Remember that FileType was triggered.  Used for did_filetype(). */
9393     if (event == EVENT_FILETYPE)
9394 	did_filetype = TRUE;
9395 
9396     tail = gettail(fname);
9397 
9398     /* Find first autocommand that matches */
9399     patcmd.curpat = first_autopat[(int)event];
9400     patcmd.nextcmd = NULL;
9401     patcmd.group = group;
9402     patcmd.fname = fname;
9403     patcmd.sfname = sfname;
9404     patcmd.tail = tail;
9405     patcmd.event = event;
9406     patcmd.arg_bufnr = autocmd_bufnr;
9407     patcmd.next = NULL;
9408     auto_next_pat(&patcmd, FALSE);
9409 
9410     /* found one, start executing the autocommands */
9411     if (patcmd.curpat != NULL)
9412     {
9413 	/* add to active_apc_list */
9414 	patcmd.next = active_apc_list;
9415 	active_apc_list = &patcmd;
9416 
9417 #ifdef FEAT_EVAL
9418 	/* set v:cmdarg (only when there is a matching pattern) */
9419 	save_cmdbang = get_vim_var_nr(VV_CMDBANG);
9420 	if (eap != NULL)
9421 	{
9422 	    save_cmdarg = set_cmdarg(eap, NULL);
9423 	    set_vim_var_nr(VV_CMDBANG, (long)eap->forceit);
9424 	}
9425 	else
9426 	    save_cmdarg = NULL;	/* avoid gcc warning */
9427 #endif
9428 	retval = TRUE;
9429 	/* mark the last pattern, to avoid an endless loop when more patterns
9430 	 * are added when executing autocommands */
9431 	for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
9432 	    ap->last = FALSE;
9433 	ap->last = TRUE;
9434 	check_lnums(TRUE);	/* make sure cursor and topline are valid */
9435 	do_cmdline(NULL, getnextac, (void *)&patcmd,
9436 				     DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
9437 #ifdef FEAT_EVAL
9438 	if (eap != NULL)
9439 	{
9440 	    (void)set_cmdarg(NULL, save_cmdarg);
9441 	    set_vim_var_nr(VV_CMDBANG, save_cmdbang);
9442 	}
9443 #endif
9444 	/* delete from active_apc_list */
9445 	if (active_apc_list == &patcmd)	    /* just in case */
9446 	    active_apc_list = patcmd.next;
9447     }
9448 
9449     --RedrawingDisabled;
9450     autocmd_busy = save_autocmd_busy;
9451     filechangeshell_busy = FALSE;
9452     autocmd_nested = save_autocmd_nested;
9453     vim_free(sourcing_name);
9454     sourcing_name = save_sourcing_name;
9455     sourcing_lnum = save_sourcing_lnum;
9456     vim_free(autocmd_fname);
9457     autocmd_fname = save_autocmd_fname;
9458     autocmd_fname_full = save_autocmd_fname_full;
9459     autocmd_bufnr = save_autocmd_bufnr;
9460     autocmd_match = save_autocmd_match;
9461 #ifdef FEAT_EVAL
9462     current_SID = save_current_SID;
9463     restore_funccal(save_funccalp);
9464 # ifdef FEAT_PROFILE
9465     if (do_profiling == PROF_YES)
9466 	prof_child_exit(&wait_time);
9467 # endif
9468 #endif
9469     vim_free(fname);
9470     vim_free(sfname);
9471     --nesting;		/* see matching increment above */
9472 
9473     /*
9474      * When stopping to execute autocommands, restore the search patterns and
9475      * the redo buffer.
9476      */
9477     if (!autocmd_busy)
9478     {
9479 	restore_search_patterns();
9480 	restoreRedobuff();
9481 	did_filetype = FALSE;
9482     }
9483 
9484     /*
9485      * Some events don't set or reset the Changed flag.
9486      * Check if still in the same buffer!
9487      */
9488     if (curbuf == old_curbuf
9489 	    && (event == EVENT_BUFREADPOST
9490 		|| event == EVENT_BUFWRITEPOST
9491 		|| event == EVENT_FILEAPPENDPOST
9492 		|| event == EVENT_VIMLEAVE
9493 		|| event == EVENT_VIMLEAVEPRE))
9494     {
9495 #ifdef FEAT_TITLE
9496 	if (curbuf->b_changed != save_changed)
9497 	    need_maketitle = TRUE;
9498 #endif
9499 	curbuf->b_changed = save_changed;
9500     }
9501 
9502     au_cleanup();	/* may really delete removed patterns/commands now */
9503 
9504 BYPASS_AU:
9505     /* When wiping out a buffer make sure all its buffer-local autocommands
9506      * are deleted. */
9507     if (event == EVENT_BUFWIPEOUT && buf != NULL)
9508 	aubuflocal_remove(buf);
9509 
9510     return retval;
9511 }
9512 
9513 # ifdef FEAT_EVAL
9514 static char_u	*old_termresponse = NULL;
9515 # endif
9516 
9517 /*
9518  * Block triggering autocommands until unblock_autocmd() is called.
9519  * Can be used recursively, so long as it's symmetric.
9520  */
9521     void
9522 block_autocmds()
9523 {
9524 # ifdef FEAT_EVAL
9525     /* Remember the value of v:termresponse. */
9526     if (autocmd_blocked == 0)
9527 	old_termresponse = get_vim_var_str(VV_TERMRESPONSE);
9528 # endif
9529     ++autocmd_blocked;
9530 }
9531 
9532     void
9533 unblock_autocmds()
9534 {
9535     --autocmd_blocked;
9536 
9537 # ifdef FEAT_EVAL
9538     /* When v:termresponse was set while autocommands were blocked, trigger
9539      * the autocommands now.  Esp. useful when executing a shell command
9540      * during startup (vimdiff). */
9541     if (autocmd_blocked == 0
9542 		      && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse)
9543 	apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf);
9544 # endif
9545 }
9546 
9547 /*
9548  * Find next autocommand pattern that matches.
9549  */
9550     static void
9551 auto_next_pat(apc, stop_at_last)
9552     AutoPatCmd	*apc;
9553     int		stop_at_last;	    /* stop when 'last' flag is set */
9554 {
9555     AutoPat	*ap;
9556     AutoCmd	*cp;
9557     char_u	*name;
9558     char	*s;
9559 
9560     vim_free(sourcing_name);
9561     sourcing_name = NULL;
9562 
9563     for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next)
9564     {
9565 	apc->curpat = NULL;
9566 
9567 	/* Only use a pattern when it has not been removed, has commands and
9568 	 * the group matches. For buffer-local autocommands only check the
9569 	 * buffer number. */
9570 	if (ap->pat != NULL && ap->cmds != NULL
9571 		&& (apc->group == AUGROUP_ALL || apc->group == ap->group))
9572 	{
9573 	    /* execution-condition */
9574 	    if (ap->buflocal_nr == 0
9575 		    ? (match_file_pat(NULL, ap->reg_prog, apc->fname,
9576 				      apc->sfname, apc->tail, ap->allow_dirs))
9577 		    : ap->buflocal_nr == apc->arg_bufnr)
9578 	    {
9579 		name = event_nr2name(apc->event);
9580 		s = _("%s Auto commands for \"%s\"");
9581 		sourcing_name = alloc((unsigned)(STRLEN(s)
9582 					    + STRLEN(name) + ap->patlen + 1));
9583 		if (sourcing_name != NULL)
9584 		{
9585 		    sprintf((char *)sourcing_name, s,
9586 					       (char *)name, (char *)ap->pat);
9587 		    if (p_verbose >= 8)
9588 		    {
9589 			verbose_enter();
9590 			smsg((char_u *)_("Executing %s"), sourcing_name);
9591 			verbose_leave();
9592 		    }
9593 		}
9594 
9595 		apc->curpat = ap;
9596 		apc->nextcmd = ap->cmds;
9597 		/* mark last command */
9598 		for (cp = ap->cmds; cp->next != NULL; cp = cp->next)
9599 		    cp->last = FALSE;
9600 		cp->last = TRUE;
9601 	    }
9602 	    line_breakcheck();
9603 	    if (apc->curpat != NULL)	    /* found a match */
9604 		break;
9605 	}
9606 	if (stop_at_last && ap->last)
9607 	    break;
9608     }
9609 }
9610 
9611 /*
9612  * Get next autocommand command.
9613  * Called by do_cmdline() to get the next line for ":if".
9614  * Returns allocated string, or NULL for end of autocommands.
9615  */
9616     static char_u *
9617 getnextac(c, cookie, indent)
9618     int	    c UNUSED;
9619     void    *cookie;
9620     int	    indent UNUSED;
9621 {
9622     AutoPatCmd	    *acp = (AutoPatCmd *)cookie;
9623     char_u	    *retval;
9624     AutoCmd	    *ac;
9625 
9626     /* Can be called again after returning the last line. */
9627     if (acp->curpat == NULL)
9628 	return NULL;
9629 
9630     /* repeat until we find an autocommand to execute */
9631     for (;;)
9632     {
9633 	/* skip removed commands */
9634 	while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL)
9635 	    if (acp->nextcmd->last)
9636 		acp->nextcmd = NULL;
9637 	    else
9638 		acp->nextcmd = acp->nextcmd->next;
9639 
9640 	if (acp->nextcmd != NULL)
9641 	    break;
9642 
9643 	/* at end of commands, find next pattern that matches */
9644 	if (acp->curpat->last)
9645 	    acp->curpat = NULL;
9646 	else
9647 	    acp->curpat = acp->curpat->next;
9648 	if (acp->curpat != NULL)
9649 	    auto_next_pat(acp, TRUE);
9650 	if (acp->curpat == NULL)
9651 	    return NULL;
9652     }
9653 
9654     ac = acp->nextcmd;
9655 
9656     if (p_verbose >= 9)
9657     {
9658 	verbose_enter_scroll();
9659 	smsg((char_u *)_("autocommand %s"), ac->cmd);
9660 	msg_puts((char_u *)"\n");   /* don't overwrite this either */
9661 	verbose_leave_scroll();
9662     }
9663     retval = vim_strsave(ac->cmd);
9664     autocmd_nested = ac->nested;
9665 #ifdef FEAT_EVAL
9666     current_SID = ac->scriptID;
9667 #endif
9668     if (ac->last)
9669 	acp->nextcmd = NULL;
9670     else
9671 	acp->nextcmd = ac->next;
9672     return retval;
9673 }
9674 
9675 /*
9676  * Return TRUE if there is a matching autocommand for "fname".
9677  * To account for buffer-local autocommands, function needs to know
9678  * in which buffer the file will be opened.
9679  */
9680     int
9681 has_autocmd(event, sfname, buf)
9682     event_T	event;
9683     char_u	*sfname;
9684     buf_T       *buf;
9685 {
9686     AutoPat	*ap;
9687     char_u	*fname;
9688     char_u	*tail = gettail(sfname);
9689     int		retval = FALSE;
9690 
9691     fname = FullName_save(sfname, FALSE);
9692     if (fname == NULL)
9693 	return FALSE;
9694 
9695 #ifdef BACKSLASH_IN_FILENAME
9696     /*
9697      * Replace all backslashes with forward slashes.  This makes the
9698      * autocommand patterns portable between Unix and MS-DOS.
9699      */
9700     sfname = vim_strsave(sfname);
9701     if (sfname != NULL)
9702 	forward_slash(sfname);
9703     forward_slash(fname);
9704 #endif
9705 
9706     for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
9707 	if (ap->pat != NULL && ap->cmds != NULL
9708 	      && (ap->buflocal_nr == 0
9709 		? match_file_pat(NULL, ap->reg_prog,
9710 					  fname, sfname, tail, ap->allow_dirs)
9711 		: buf != NULL && ap->buflocal_nr == buf->b_fnum
9712 	   ))
9713 	{
9714 	    retval = TRUE;
9715 	    break;
9716 	}
9717 
9718     vim_free(fname);
9719 #ifdef BACKSLASH_IN_FILENAME
9720     vim_free(sfname);
9721 #endif
9722 
9723     return retval;
9724 }
9725 
9726 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9727 /*
9728  * Function given to ExpandGeneric() to obtain the list of autocommand group
9729  * names.
9730  */
9731     char_u *
9732 get_augroup_name(xp, idx)
9733     expand_T	*xp UNUSED;
9734     int		idx;
9735 {
9736     if (idx == augroups.ga_len)		/* add "END" add the end */
9737 	return (char_u *)"END";
9738     if (idx >= augroups.ga_len)		/* end of list */
9739 	return NULL;
9740     if (AUGROUP_NAME(idx) == NULL)	/* skip deleted entries */
9741 	return (char_u *)"";
9742     return AUGROUP_NAME(idx);		/* return a name */
9743 }
9744 
9745 static int include_groups = FALSE;
9746 
9747     char_u  *
9748 set_context_in_autocmd(xp, arg, doautocmd)
9749     expand_T	*xp;
9750     char_u	*arg;
9751     int		doautocmd;	/* TRUE for :doauto*, FALSE for :autocmd */
9752 {
9753     char_u	*p;
9754     int		group;
9755 
9756     /* check for a group name, skip it if present */
9757     include_groups = FALSE;
9758     p = arg;
9759     group = au_get_grouparg(&arg);
9760     if (group == AUGROUP_ERROR)
9761 	return NULL;
9762     /* If there only is a group name that's what we expand. */
9763     if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1]))
9764     {
9765 	arg = p;
9766 	group = AUGROUP_ALL;
9767     }
9768 
9769     /* skip over event name */
9770     for (p = arg; *p != NUL && !vim_iswhite(*p); ++p)
9771 	if (*p == ',')
9772 	    arg = p + 1;
9773     if (*p == NUL)
9774     {
9775 	if (group == AUGROUP_ALL)
9776 	    include_groups = TRUE;
9777 	xp->xp_context = EXPAND_EVENTS;	    /* expand event name */
9778 	xp->xp_pattern = arg;
9779 	return NULL;
9780     }
9781 
9782     /* skip over pattern */
9783     arg = skipwhite(p);
9784     while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\'))
9785 	arg++;
9786     if (*arg)
9787 	return arg;			    /* expand (next) command */
9788 
9789     if (doautocmd)
9790 	xp->xp_context = EXPAND_FILES;	    /* expand file names */
9791     else
9792 	xp->xp_context = EXPAND_NOTHING;    /* pattern is not expanded */
9793     return NULL;
9794 }
9795 
9796 /*
9797  * Function given to ExpandGeneric() to obtain the list of event names.
9798  */
9799     char_u *
9800 get_event_name(xp, idx)
9801     expand_T	*xp UNUSED;
9802     int		idx;
9803 {
9804     if (idx < augroups.ga_len)		/* First list group names, if wanted */
9805     {
9806 	if (!include_groups || AUGROUP_NAME(idx) == NULL)
9807 	    return (char_u *)"";	/* skip deleted entries */
9808 	return AUGROUP_NAME(idx);	/* return a name */
9809     }
9810     return (char_u *)event_names[idx - augroups.ga_len].name;
9811 }
9812 
9813 #endif	/* FEAT_CMDL_COMPL */
9814 
9815 /*
9816  * Return TRUE if autocmd is supported.
9817  */
9818     int
9819 autocmd_supported(name)
9820     char_u	*name;
9821 {
9822     char_u *p;
9823 
9824     return (event_name2nr(name, &p) != NUM_EVENTS);
9825 }
9826 
9827 /*
9828  * Return TRUE if an autocommand is defined for a group, event and
9829  * pattern:  The group can be omitted to accept any group. "event" and "pattern"
9830  * can be NULL to accept any event and pattern. "pattern" can be NULL to accept
9831  * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted.
9832  * Used for:
9833  *	exists("#Group") or
9834  *	exists("#Group#Event") or
9835  *	exists("#Group#Event#pat") or
9836  *	exists("#Event") or
9837  *	exists("#Event#pat")
9838  */
9839     int
9840 au_exists(arg)
9841     char_u	*arg;
9842 {
9843     char_u	*arg_save;
9844     char_u	*pattern = NULL;
9845     char_u	*event_name;
9846     char_u	*p;
9847     event_T	event;
9848     AutoPat	*ap;
9849     buf_T	*buflocal_buf = NULL;
9850     int		group;
9851     int		retval = FALSE;
9852 
9853     /* Make a copy so that we can change the '#' chars to a NUL. */
9854     arg_save = vim_strsave(arg);
9855     if (arg_save == NULL)
9856 	return FALSE;
9857     p = vim_strchr(arg_save, '#');
9858     if (p != NULL)
9859 	*p++ = NUL;
9860 
9861     /* First, look for an autocmd group name */
9862     group = au_find_group(arg_save);
9863     if (group == AUGROUP_ERROR)
9864     {
9865 	/* Didn't match a group name, assume the first argument is an event. */
9866 	group = AUGROUP_ALL;
9867 	event_name = arg_save;
9868     }
9869     else
9870     {
9871 	if (p == NULL)
9872 	{
9873 	    /* "Group": group name is present and it's recognized */
9874 	    retval = TRUE;
9875 	    goto theend;
9876 	}
9877 
9878 	/* Must be "Group#Event" or "Group#Event#pat". */
9879 	event_name = p;
9880 	p = vim_strchr(event_name, '#');
9881 	if (p != NULL)
9882 	    *p++ = NUL;	    /* "Group#Event#pat" */
9883     }
9884 
9885     pattern = p;	    /* "pattern" is NULL when there is no pattern */
9886 
9887     /* find the index (enum) for the event name */
9888     event = event_name2nr(event_name, &p);
9889 
9890     /* return FALSE if the event name is not recognized */
9891     if (event == NUM_EVENTS)
9892 	goto theend;
9893 
9894     /* Find the first autocommand for this event.
9895      * If there isn't any, return FALSE;
9896      * If there is one and no pattern given, return TRUE; */
9897     ap = first_autopat[(int)event];
9898     if (ap == NULL)
9899 	goto theend;
9900 
9901     /* if pattern is "<buffer>", special handling is needed which uses curbuf */
9902     /* for pattern "<buffer=N>, fnamecmp() will work fine */
9903     if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0)
9904 	buflocal_buf = curbuf;
9905 
9906     /* Check if there is an autocommand with the given pattern. */
9907     for ( ; ap != NULL; ap = ap->next)
9908 	/* only use a pattern when it has not been removed and has commands. */
9909 	/* For buffer-local autocommands, fnamecmp() works fine. */
9910 	if (ap->pat != NULL && ap->cmds != NULL
9911 	    && (group == AUGROUP_ALL || ap->group == group)
9912 	    && (pattern == NULL
9913 		|| (buflocal_buf == NULL
9914 		    ? fnamecmp(ap->pat, pattern) == 0
9915 		    : ap->buflocal_nr == buflocal_buf->b_fnum)))
9916 	{
9917 	    retval = TRUE;
9918 	    break;
9919 	}
9920 
9921 theend:
9922     vim_free(arg_save);
9923     return retval;
9924 }
9925 
9926 #else	/* FEAT_AUTOCMD */
9927 
9928 /*
9929  * Prepare for executing commands for (hidden) buffer "buf".
9930  * This is the non-autocommand version, it simply saves "curbuf" and sets
9931  * "curbuf" and "curwin" to match "buf".
9932  */
9933     void
9934 aucmd_prepbuf(aco, buf)
9935     aco_save_T	*aco;		/* structure to save values in */
9936     buf_T	*buf;		/* new curbuf */
9937 {
9938     aco->save_curbuf = curbuf;
9939     --curbuf->b_nwindows;
9940     curbuf = buf;
9941     curwin->w_buffer = buf;
9942     ++curbuf->b_nwindows;
9943 }
9944 
9945 /*
9946  * Restore after executing commands for a (hidden) buffer.
9947  * This is the non-autocommand version.
9948  */
9949     void
9950 aucmd_restbuf(aco)
9951     aco_save_T	*aco;		/* structure holding saved values */
9952 {
9953     --curbuf->b_nwindows;
9954     curbuf = aco->save_curbuf;
9955     curwin->w_buffer = curbuf;
9956     ++curbuf->b_nwindows;
9957 }
9958 
9959 #endif	/* FEAT_AUTOCMD */
9960 
9961 
9962 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO)
9963 /*
9964  * Try matching a filename with a "pattern" ("prog" is NULL), or use the
9965  * precompiled regprog "prog" ("pattern" is NULL).  That avoids calling
9966  * vim_regcomp() often.
9967  * Used for autocommands and 'wildignore'.
9968  * Returns TRUE if there is a match, FALSE otherwise.
9969  */
9970     int
9971 match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs)
9972     char_u	*pattern;		/* pattern to match with */
9973     regprog_T	*prog;			/* pre-compiled regprog or NULL */
9974     char_u	*fname;			/* full path of file name */
9975     char_u	*sfname;		/* short file name or NULL */
9976     char_u	*tail;			/* tail of path */
9977     int		allow_dirs;		/* allow matching with dir */
9978 {
9979     regmatch_T	regmatch;
9980     int		result = FALSE;
9981 #ifdef FEAT_OSFILETYPE
9982     int		no_pattern = FALSE; /* TRUE if check is filetype only */
9983     char_u	*type_start;
9984     char_u	c;
9985     int		match = FALSE;
9986 #endif
9987 
9988 #ifdef CASE_INSENSITIVE_FILENAME
9989     regmatch.rm_ic = TRUE;		/* Always ignore case */
9990 #else
9991     regmatch.rm_ic = FALSE;		/* Don't ever ignore case */
9992 #endif
9993 #ifdef FEAT_OSFILETYPE
9994     if (*pattern == '<')
9995     {
9996 	/* There is a filetype condition specified with this pattern.
9997 	 * Check the filetype matches first. If not, don't bother with the
9998 	 * pattern (set regprog to NULL).
9999 	 * Always use magic for the regexp.
10000 	 */
10001 
10002 	for (type_start = pattern + 1; (c = *pattern); pattern++)
10003 	{
10004 	    if ((c == ';' || c == '>') && match == FALSE)
10005 	    {
10006 		*pattern = NUL;	    /* Terminate the string */
10007 		/* TODO: match with 'filetype' of buffer that "fname" comes
10008 		 * from. */
10009 		match = mch_check_filetype(fname, type_start);
10010 		*pattern = c;	    /* Restore the terminator */
10011 		type_start = pattern + 1;
10012 	    }
10013 	    if (c == '>')
10014 		break;
10015 	}
10016 
10017 	/* (c should never be NUL, but check anyway) */
10018 	if (match == FALSE || c == NUL)
10019 	    regmatch.regprog = NULL;	/* Doesn't match - don't check pat. */
10020 	else if (*pattern == NUL)
10021 	{
10022 	    regmatch.regprog = NULL;	/* Vim will try to free regprog later */
10023 	    no_pattern = TRUE;	/* Always matches - don't check pat. */
10024 	}
10025 	else
10026 	    regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC);
10027     }
10028     else
10029 #endif
10030     {
10031 	if (prog != NULL)
10032 	    regmatch.regprog = prog;
10033 	else
10034 	    regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
10035     }
10036 
10037     /*
10038      * Try for a match with the pattern with:
10039      * 1. the full file name, when the pattern has a '/'.
10040      * 2. the short file name, when the pattern has a '/'.
10041      * 3. the tail of the file name, when the pattern has no '/'.
10042      */
10043     if (
10044 #ifdef FEAT_OSFILETYPE
10045 	    /* If the check is for a filetype only and we don't care
10046 	     * about the path then skip all the regexp stuff.
10047 	     */
10048 	    no_pattern ||
10049 #endif
10050 	    (regmatch.regprog != NULL
10051 	     && ((allow_dirs
10052 		     && (vim_regexec(&regmatch, fname, (colnr_T)0)
10053 			 || (sfname != NULL
10054 			     && vim_regexec(&regmatch, sfname, (colnr_T)0))))
10055 		 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0)))))
10056 	result = TRUE;
10057 
10058     if (prog == NULL)
10059 	vim_free(regmatch.regprog);
10060     return result;
10061 }
10062 #endif
10063 
10064 #if defined(FEAT_WILDIGN) || defined(PROTO)
10065 /*
10066  * Return TRUE if a file matches with a pattern in "list".
10067  * "list" is a comma-separated list of patterns, like 'wildignore'.
10068  * "sfname" is the short file name or NULL, "ffname" the long file name.
10069  */
10070     int
10071 match_file_list(list, sfname, ffname)
10072     char_u	*list;
10073     char_u	*sfname;
10074     char_u	*ffname;
10075 {
10076     char_u	buf[100];
10077     char_u	*tail;
10078     char_u	*regpat;
10079     char	allow_dirs;
10080     int		match;
10081     char_u	*p;
10082 
10083     tail = gettail(sfname);
10084 
10085     /* try all patterns in 'wildignore' */
10086     p = list;
10087     while (*p)
10088     {
10089 	copy_option_part(&p, buf, 100, ",");
10090 	regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
10091 	if (regpat == NULL)
10092 	    break;
10093 	match = match_file_pat(regpat, NULL, ffname, sfname,
10094 						       tail, (int)allow_dirs);
10095 	vim_free(regpat);
10096 	if (match)
10097 	    return TRUE;
10098     }
10099     return FALSE;
10100 }
10101 #endif
10102 
10103 /*
10104  * Convert the given pattern "pat" which has shell style wildcards in it, into
10105  * a regular expression, and return the result in allocated memory.  If there
10106  * is a directory path separator to be matched, then TRUE is put in
10107  * allow_dirs, otherwise FALSE is put there -- webb.
10108  * Handle backslashes before special characters, like "\*" and "\ ".
10109  *
10110  * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg:
10111  * '<html>myfile' becomes '<html>^myfile$' -- leonard.
10112  *
10113  * Returns NULL when out of memory.
10114  */
10115     char_u *
10116 file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash)
10117     char_u	*pat;
10118     char_u	*pat_end;	/* first char after pattern or NULL */
10119     char	*allow_dirs;	/* Result passed back out in here */
10120     int		no_bslash UNUSED; /* Don't use a backward slash as pathsep */
10121 {
10122     int		size;
10123     char_u	*endp;
10124     char_u	*reg_pat;
10125     char_u	*p;
10126     int		i;
10127     int		nested = 0;
10128     int		add_dollar = TRUE;
10129 #ifdef FEAT_OSFILETYPE
10130     int		check_length = 0;
10131 #endif
10132 
10133     if (allow_dirs != NULL)
10134 	*allow_dirs = FALSE;
10135     if (pat_end == NULL)
10136 	pat_end = pat + STRLEN(pat);
10137 
10138 #ifdef FEAT_OSFILETYPE
10139     /* Find out how much of the string is the filetype check */
10140     if (*pat == '<')
10141     {
10142 	/* Count chars until the next '>' */
10143 	for (p = pat + 1; p < pat_end && *p != '>'; p++)
10144 	    ;
10145 	if (p < pat_end)
10146 	{
10147 	    /* Pattern is of the form <.*>.*  */
10148 	    check_length = p - pat + 1;
10149 	    if (p + 1 >= pat_end)
10150 	    {
10151 		/* The 'pattern' is a filetype check ONLY */
10152 		reg_pat = (char_u *)alloc(check_length + 1);
10153 		if (reg_pat != NULL)
10154 		{
10155 		    mch_memmove(reg_pat, pat, (size_t)check_length);
10156 		    reg_pat[check_length] = NUL;
10157 		}
10158 		return reg_pat;
10159 	    }
10160 	}
10161 	/* else: there was no closing '>' - assume it was a normal pattern */
10162 
10163     }
10164     pat += check_length;
10165     size = 2 + check_length;
10166 #else
10167     size = 2;		/* '^' at start, '$' at end */
10168 #endif
10169 
10170     for (p = pat; p < pat_end; p++)
10171     {
10172 	switch (*p)
10173 	{
10174 	    case '*':
10175 	    case '.':
10176 	    case ',':
10177 	    case '{':
10178 	    case '}':
10179 	    case '~':
10180 		size += 2;	/* extra backslash */
10181 		break;
10182 #ifdef BACKSLASH_IN_FILENAME
10183 	    case '\\':
10184 	    case '/':
10185 		size += 4;	/* could become "[\/]" */
10186 		break;
10187 #endif
10188 	    default:
10189 		size++;
10190 # ifdef  FEAT_MBYTE
10191 		if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
10192 		{
10193 		    ++p;
10194 		    ++size;
10195 		}
10196 # endif
10197 		break;
10198 	}
10199     }
10200     reg_pat = alloc(size + 1);
10201     if (reg_pat == NULL)
10202 	return NULL;
10203 
10204 #ifdef FEAT_OSFILETYPE
10205     /* Copy the type check in to the start. */
10206     if (check_length)
10207 	mch_memmove(reg_pat, pat - check_length, (size_t)check_length);
10208     i = check_length;
10209 #else
10210     i = 0;
10211 #endif
10212 
10213     if (pat[0] == '*')
10214 	while (pat[0] == '*' && pat < pat_end - 1)
10215 	    pat++;
10216     else
10217 	reg_pat[i++] = '^';
10218     endp = pat_end - 1;
10219     if (*endp == '*')
10220     {
10221 	while (endp - pat > 0 && *endp == '*')
10222 	    endp--;
10223 	add_dollar = FALSE;
10224     }
10225     for (p = pat; *p && nested >= 0 && p <= endp; p++)
10226     {
10227 	switch (*p)
10228 	{
10229 	    case '*':
10230 		reg_pat[i++] = '.';
10231 		reg_pat[i++] = '*';
10232 		while (p[1] == '*')	/* "**" matches like "*" */
10233 		    ++p;
10234 		break;
10235 	    case '.':
10236 	    case '~':
10237 		reg_pat[i++] = '\\';
10238 		reg_pat[i++] = *p;
10239 		break;
10240 	    case '?':
10241 		reg_pat[i++] = '.';
10242 		break;
10243 	    case '\\':
10244 		if (p[1] == NUL)
10245 		    break;
10246 #ifdef BACKSLASH_IN_FILENAME
10247 		if (!no_bslash)
10248 		{
10249 		    /* translate:
10250 		     * "\x" to "\\x"  e.g., "dir\file"
10251 		     * "\*" to "\\.*" e.g., "dir\*.c"
10252 		     * "\?" to "\\."  e.g., "dir\??.c"
10253 		     * "\+" to "\+"   e.g., "fileX\+.c"
10254 		     */
10255 		    if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
10256 			    && p[1] != '+')
10257 		    {
10258 			reg_pat[i++] = '[';
10259 			reg_pat[i++] = '\\';
10260 			reg_pat[i++] = '/';
10261 			reg_pat[i++] = ']';
10262 			if (allow_dirs != NULL)
10263 			    *allow_dirs = TRUE;
10264 			break;
10265 		    }
10266 		}
10267 #endif
10268 		/* Undo escaping from ExpandEscape():
10269 		 * foo\?bar -> foo?bar
10270 		 * foo\%bar -> foo%bar
10271 		 * foo\,bar -> foo,bar
10272 		 * foo\ bar -> foo bar
10273 		 * Don't unescape \, * and others that are also special in a
10274 		 * regexp. */
10275 		if (*++p == '?'
10276 #ifdef BACKSLASH_IN_FILENAME
10277 			&& no_bslash
10278 #endif
10279 			)
10280 		    reg_pat[i++] = '?';
10281 		else
10282 		    if (*p == ',' || *p == '%' || *p == '#' || *p == ' ')
10283 			reg_pat[i++] = *p;
10284 		    else
10285 		    {
10286 			if (allow_dirs != NULL && vim_ispathsep(*p)
10287 #ifdef BACKSLASH_IN_FILENAME
10288 				&& (!no_bslash || *p != '\\')
10289 #endif
10290 				)
10291 			    *allow_dirs = TRUE;
10292 			reg_pat[i++] = '\\';
10293 			reg_pat[i++] = *p;
10294 		    }
10295 		break;
10296 #ifdef BACKSLASH_IN_FILENAME
10297 	    case '/':
10298 		reg_pat[i++] = '[';
10299 		reg_pat[i++] = '\\';
10300 		reg_pat[i++] = '/';
10301 		reg_pat[i++] = ']';
10302 		if (allow_dirs != NULL)
10303 		    *allow_dirs = TRUE;
10304 		break;
10305 #endif
10306 	    case '{':
10307 		reg_pat[i++] = '\\';
10308 		reg_pat[i++] = '(';
10309 		nested++;
10310 		break;
10311 	    case '}':
10312 		reg_pat[i++] = '\\';
10313 		reg_pat[i++] = ')';
10314 		--nested;
10315 		break;
10316 	    case ',':
10317 		if (nested)
10318 		{
10319 		    reg_pat[i++] = '\\';
10320 		    reg_pat[i++] = '|';
10321 		}
10322 		else
10323 		    reg_pat[i++] = ',';
10324 		break;
10325 	    default:
10326 # ifdef  FEAT_MBYTE
10327 		if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
10328 		    reg_pat[i++] = *p++;
10329 		else
10330 # endif
10331 		if (allow_dirs != NULL && vim_ispathsep(*p))
10332 		    *allow_dirs = TRUE;
10333 		reg_pat[i++] = *p;
10334 		break;
10335 	}
10336     }
10337     if (add_dollar)
10338 	reg_pat[i++] = '$';
10339     reg_pat[i] = NUL;
10340     if (nested != 0)
10341     {
10342 	if (nested < 0)
10343 	    EMSG(_("E219: Missing {."));
10344 	else
10345 	    EMSG(_("E220: Missing }."));
10346 	vim_free(reg_pat);
10347 	reg_pat = NULL;
10348     }
10349     return reg_pat;
10350 }
10351 
10352 #if defined(EINTR) || defined(PROTO)
10353 /*
10354  * Version of read() that retries when interrupted by EINTR (possibly
10355  * by a SIGWINCH).
10356  */
10357     long
10358 read_eintr(fd, buf, bufsize)
10359     int	    fd;
10360     void    *buf;
10361     size_t  bufsize;
10362 {
10363     long ret;
10364 
10365     for (;;)
10366     {
10367 	ret = vim_read(fd, buf, bufsize);
10368 	if (ret >= 0 || errno != EINTR)
10369 	    break;
10370     }
10371     return ret;
10372 }
10373 
10374 /*
10375  * Version of write() that retries when interrupted by EINTR (possibly
10376  * by a SIGWINCH).
10377  */
10378     long
10379 write_eintr(fd, buf, bufsize)
10380     int	    fd;
10381     void    *buf;
10382     size_t  bufsize;
10383 {
10384     long    ret = 0;
10385     long    wlen;
10386 
10387     /* Repeat the write() so long it didn't fail, other than being interrupted
10388      * by a signal. */
10389     while (ret < (long)bufsize)
10390     {
10391 	wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
10392 	if (wlen < 0)
10393 	{
10394 	    if (errno != EINTR)
10395 		break;
10396 	}
10397 	else
10398 	    ret += wlen;
10399     }
10400     return ret;
10401 }
10402 #endif
10403