Home | History | Annotate | Download | only in src
      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 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
     15 # include "vimio.h"	/* for lseek(), must be before vim.h */
     16 #endif
     17 
     18 #if defined __EMX__
     19 # include "vimio.h"	/* for mktemp(), CJW 1997-12-03 */
     20 #endif
     21 
     22 #include "vim.h"
     23 
     24 #ifdef HAVE_FCNTL_H
     25 # include <fcntl.h>
     26 #endif
     27 
     28 #ifdef __TANDEM
     29 # include <limits.h>		/* for SSIZE_MAX */
     30 #endif
     31 
     32 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
     33 # include <utime.h>		/* for struct utimbuf */
     34 #endif
     35 
     36 #define BUFSIZE		8192	/* size of normal write buffer */
     37 #define SMBUFSIZE	256	/* size of emergency write buffer */
     38 
     39 #ifdef FEAT_CRYPT
     40 # define CRYPT_MAGIC		"VimCrypt~01!"	/* "01" is the version nr */
     41 # define CRYPT_MAGIC_LEN	12		/* must be multiple of 4! */
     42 #endif
     43 
     44 /* Is there any system that doesn't have access()? */
     45 #define USE_MCH_ACCESS
     46 
     47 #ifdef FEAT_MBYTE
     48 static char_u *next_fenc __ARGS((char_u **pp));
     49 # ifdef FEAT_EVAL
     50 static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp));
     51 # endif
     52 #endif
     53 #ifdef FEAT_VIMINFO
     54 static void check_marks_read __ARGS((void));
     55 #endif
     56 #ifdef FEAT_CRYPT
     57 static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, long *filesizep, int newfile));
     58 #endif
     59 #ifdef UNIX
     60 static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
     61 #endif
     62 static int set_rw_fname __ARGS((char_u *fname, char_u *sfname));
     63 static int msg_add_fileformat __ARGS((int eol_type));
     64 static void msg_add_eol __ARGS((void));
     65 static int check_mtime __ARGS((buf_T *buf, struct stat *s));
     66 static int time_differs __ARGS((long t1, long t2));
     67 #ifdef FEAT_AUTOCMD
     68 static int apply_autocmds_exarg __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap));
     69 static int au_find_group __ARGS((char_u *name));
     70 
     71 # define AUGROUP_DEFAULT    -1	    /* default autocmd group */
     72 # define AUGROUP_ERROR	    -2	    /* errornouse autocmd group */
     73 # define AUGROUP_ALL	    -3	    /* all autocmd groups */
     74 #endif
     75 
     76 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE)
     77 # define HAS_BW_FLAGS
     78 # define FIO_LATIN1	0x01	/* convert Latin1 */
     79 # define FIO_UTF8	0x02	/* convert UTF-8 */
     80 # define FIO_UCS2	0x04	/* convert UCS-2 */
     81 # define FIO_UCS4	0x08	/* convert UCS-4 */
     82 # define FIO_UTF16	0x10	/* convert UTF-16 */
     83 # ifdef WIN3264
     84 #  define FIO_CODEPAGE	0x20	/* convert MS-Windows codepage */
     85 #  define FIO_PUT_CP(x) (((x) & 0xffff) << 16)	/* put codepage in top word */
     86 #  define FIO_GET_CP(x)	(((x)>>16) & 0xffff)	/* get codepage from top word */
     87 # endif
     88 # ifdef MACOS_X
     89 #  define FIO_MACROMAN	0x20	/* convert MacRoman */
     90 # endif
     91 # define FIO_ENDIAN_L	0x80	/* little endian */
     92 # define FIO_ENCRYPTED	0x1000	/* encrypt written bytes */
     93 # define FIO_NOCONVERT	0x2000	/* skip encoding conversion */
     94 # define FIO_UCSBOM	0x4000	/* check for BOM at start of file */
     95 # define FIO_ALL	-1	/* allow all formats */
     96 #endif
     97 
     98 /* When converting, a read() or write() may leave some bytes to be converted
     99  * for the next call.  The value is guessed... */
    100 #define CONV_RESTLEN 30
    101 
    102 /* We have to guess how much a sequence of bytes may expand when converting
    103  * with iconv() to be able to allocate a buffer. */
    104 #define ICONV_MULT 8
    105 
    106 /*
    107  * Structure to pass arguments from buf_write() to buf_write_bytes().
    108  */
    109 struct bw_info
    110 {
    111     int		bw_fd;		/* file descriptor */
    112     char_u	*bw_buf;	/* buffer with data to be written */
    113     int		bw_len;	/* lenght of data */
    114 #ifdef HAS_BW_FLAGS
    115     int		bw_flags;	/* FIO_ flags */
    116 #endif
    117 #ifdef FEAT_MBYTE
    118     char_u	bw_rest[CONV_RESTLEN]; /* not converted bytes */
    119     int		bw_restlen;	/* nr of bytes in bw_rest[] */
    120     int		bw_first;	/* first write call */
    121     char_u	*bw_conv_buf;	/* buffer for writing converted chars */
    122     int		bw_conv_buflen; /* size of bw_conv_buf */
    123     int		bw_conv_error;	/* set for conversion error */
    124 # ifdef USE_ICONV
    125     iconv_t	bw_iconv_fd;	/* descriptor for iconv() or -1 */
    126 # endif
    127 #endif
    128 };
    129 
    130 static int  buf_write_bytes __ARGS((struct bw_info *ip));
    131 
    132 #ifdef FEAT_MBYTE
    133 static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
    134 static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
    135 static int same_encoding __ARGS((char_u *a, char_u *b));
    136 static int get_fio_flags __ARGS((char_u *ptr));
    137 static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
    138 static int make_bom __ARGS((char_u *buf, char_u *name));
    139 # ifdef WIN3264
    140 static int get_win_fio_flags __ARGS((char_u *ptr));
    141 # endif
    142 # ifdef MACOS_X
    143 static int get_mac_fio_flags __ARGS((char_u *ptr));
    144 # endif
    145 #endif
    146 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf));
    147 
    148 
    149     void
    150 filemess(buf, name, s, attr)
    151     buf_T	*buf;
    152     char_u	*name;
    153     char_u	*s;
    154     int		attr;
    155 {
    156     int		msg_scroll_save;
    157 
    158     if (msg_silent != 0)
    159 	return;
    160     msg_add_fname(buf, name);	    /* put file name in IObuff with quotes */
    161     /* If it's extremely long, truncate it. */
    162     if (STRLEN(IObuff) > IOSIZE - 80)
    163 	IObuff[IOSIZE - 80] = NUL;
    164     STRCAT(IObuff, s);
    165     /*
    166      * For the first message may have to start a new line.
    167      * For further ones overwrite the previous one, reset msg_scroll before
    168      * calling filemess().
    169      */
    170     msg_scroll_save = msg_scroll;
    171     if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
    172 	msg_scroll = FALSE;
    173     if (!msg_scroll)	/* wait a bit when overwriting an error msg */
    174 	check_for_delay(FALSE);
    175     msg_start();
    176     msg_scroll = msg_scroll_save;
    177     msg_scrolled_ign = TRUE;
    178     /* may truncate the message to avoid a hit-return prompt */
    179     msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
    180     msg_clr_eos();
    181     out_flush();
    182     msg_scrolled_ign = FALSE;
    183 }
    184 
    185 /*
    186  * Read lines from file "fname" into the buffer after line "from".
    187  *
    188  * 1. We allocate blocks with lalloc, as big as possible.
    189  * 2. Each block is filled with characters from the file with a single read().
    190  * 3. The lines are inserted in the buffer with ml_append().
    191  *
    192  * (caller must check that fname != NULL, unless READ_STDIN is used)
    193  *
    194  * "lines_to_skip" is the number of lines that must be skipped
    195  * "lines_to_read" is the number of lines that are appended
    196  * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
    197  *
    198  * flags:
    199  * READ_NEW	starting to edit a new buffer
    200  * READ_FILTER	reading filter output
    201  * READ_STDIN	read from stdin instead of a file
    202  * READ_BUFFER	read from curbuf instead of a file (converting after reading
    203  *		stdin)
    204  * READ_DUMMY	read into a dummy buffer (to check if file contents changed)
    205  *
    206  * return FAIL for failure, OK otherwise
    207  */
    208     int
    209 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
    210     char_u	*fname;
    211     char_u	*sfname;
    212     linenr_T	from;
    213     linenr_T	lines_to_skip;
    214     linenr_T	lines_to_read;
    215     exarg_T	*eap;			/* can be NULL! */
    216     int		flags;
    217 {
    218     int		fd = 0;
    219     int		newfile = (flags & READ_NEW);
    220     int		set_options = newfile || (eap != NULL && eap->read_edit);
    221     int		check_readonly;
    222     int		filtering = (flags & READ_FILTER);
    223     int		read_stdin = (flags & READ_STDIN);
    224     int		read_buffer = (flags & READ_BUFFER);
    225     linenr_T	read_buf_lnum = 1;	/* next line to read from curbuf */
    226     colnr_T	read_buf_col = 0;	/* next char to read from this line */
    227     char_u	c;
    228     linenr_T	lnum = from;
    229     char_u	*ptr = NULL;		/* pointer into read buffer */
    230     char_u	*buffer = NULL;		/* read buffer */
    231     char_u	*new_buffer = NULL;	/* init to shut up gcc */
    232     char_u	*line_start = NULL;	/* init to shut up gcc */
    233     int		wasempty;		/* buffer was empty before reading */
    234     colnr_T	len;
    235     long	size = 0;
    236     char_u	*p;
    237     long	filesize = 0;
    238     int		skip_read = FALSE;
    239 #ifdef FEAT_CRYPT
    240     char_u	*cryptkey = NULL;
    241 #endif
    242     int		split = 0;		/* number of split lines */
    243 #define UNKNOWN	 0x0fffffff		/* file size is unknown */
    244     linenr_T	linecnt;
    245     int		error = FALSE;		/* errors encountered */
    246     int		ff_error = EOL_UNKNOWN; /* file format with errors */
    247     long	linerest = 0;		/* remaining chars in line */
    248 #ifdef UNIX
    249     int		perm = 0;
    250     int		swap_mode = -1;		/* protection bits for swap file */
    251 #else
    252     int		perm;
    253 #endif
    254     int		fileformat = 0;		/* end-of-line format */
    255     int		keep_fileformat = FALSE;
    256     struct stat	st;
    257     int		file_readonly;
    258     linenr_T	skip_count = 0;
    259     linenr_T	read_count = 0;
    260     int		msg_save = msg_scroll;
    261     linenr_T	read_no_eol_lnum = 0;   /* non-zero lnum when last line of
    262 					 * last read was missing the eol */
    263     int		try_mac = (vim_strchr(p_ffs, 'm') != NULL);
    264     int		try_dos = (vim_strchr(p_ffs, 'd') != NULL);
    265     int		try_unix = (vim_strchr(p_ffs, 'x') != NULL);
    266     int		file_rewind = FALSE;
    267 #ifdef FEAT_MBYTE
    268     int		can_retry;
    269     linenr_T	conv_error = 0;		/* line nr with conversion error */
    270     linenr_T	illegal_byte = 0;	/* line nr with illegal byte */
    271     int		keep_dest_enc = FALSE;	/* don't retry when char doesn't fit
    272 					   in destination encoding */
    273     int		bad_char_behavior = BAD_REPLACE;
    274 					/* BAD_KEEP, BAD_DROP or character to
    275 					 * replace with */
    276     char_u	*tmpname = NULL;	/* name of 'charconvert' output file */
    277     int		fio_flags = 0;
    278     char_u	*fenc;			/* fileencoding to use */
    279     int		fenc_alloced;		/* fenc_next is in allocated memory */
    280     char_u	*fenc_next = NULL;	/* next item in 'fencs' or NULL */
    281     int		advance_fenc = FALSE;
    282     long	real_size = 0;
    283 # ifdef USE_ICONV
    284     iconv_t	iconv_fd = (iconv_t)-1;	/* descriptor for iconv() or -1 */
    285 #  ifdef FEAT_EVAL
    286     int		did_iconv = FALSE;	/* TRUE when iconv() failed and trying
    287 					   'charconvert' next */
    288 #  endif
    289 # endif
    290     int		converted = FALSE;	/* TRUE if conversion done */
    291     int		notconverted = FALSE;	/* TRUE if conversion wanted but it
    292 					   wasn't possible */
    293     char_u	conv_rest[CONV_RESTLEN];
    294     int		conv_restlen = 0;	/* nr of bytes in conv_rest[] */
    295 #endif
    296 
    297     write_no_eol_lnum = 0;	/* in case it was set by the previous read */
    298 
    299     /*
    300      * If there is no file name yet, use the one for the read file.
    301      * BF_NOTEDITED is set to reflect this.
    302      * Don't do this for a read from a filter.
    303      * Only do this when 'cpoptions' contains the 'f' flag.
    304      */
    305     if (curbuf->b_ffname == NULL
    306 	    && !filtering
    307 	    && fname != NULL
    308 	    && vim_strchr(p_cpo, CPO_FNAMER) != NULL
    309 	    && !(flags & READ_DUMMY))
    310     {
    311 	if (set_rw_fname(fname, sfname) == FAIL)
    312 	    return FAIL;
    313     }
    314 
    315     /* After reading a file the cursor line changes but we don't want to
    316      * display the line. */
    317     ex_no_reprint = TRUE;
    318 
    319     /* don't display the file info for another buffer now */
    320     need_fileinfo = FALSE;
    321 
    322     /*
    323      * For Unix: Use the short file name whenever possible.
    324      * Avoids problems with networks and when directory names are changed.
    325      * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
    326      * another directory, which we don't detect.
    327      */
    328     if (sfname == NULL)
    329 	sfname = fname;
    330 #if defined(UNIX) || defined(__EMX__)
    331     fname = sfname;
    332 #endif
    333 
    334 #ifdef FEAT_AUTOCMD
    335     /*
    336      * The BufReadCmd and FileReadCmd events intercept the reading process by
    337      * executing the associated commands instead.
    338      */
    339     if (!filtering && !read_stdin && !read_buffer)
    340     {
    341 	pos_T	    pos;
    342 
    343 	pos = curbuf->b_op_start;
    344 
    345 	/* Set '[ mark to the line above where the lines go (line 1 if zero). */
    346 	curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
    347 	curbuf->b_op_start.col = 0;
    348 
    349 	if (newfile)
    350 	{
    351 	    if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
    352 							  FALSE, curbuf, eap))
    353 #ifdef FEAT_EVAL
    354 		return aborting() ? FAIL : OK;
    355 #else
    356 		return OK;
    357 #endif
    358 	}
    359 	else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
    360 							    FALSE, NULL, eap))
    361 #ifdef FEAT_EVAL
    362 	    return aborting() ? FAIL : OK;
    363 #else
    364 	    return OK;
    365 #endif
    366 
    367 	curbuf->b_op_start = pos;
    368     }
    369 #endif
    370 
    371     if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
    372 	msg_scroll = FALSE;	/* overwrite previous file message */
    373     else
    374 	msg_scroll = TRUE;	/* don't overwrite previous file message */
    375 
    376     /*
    377      * If the name ends in a path separator, we can't open it.  Check here,
    378      * because reading the file may actually work, but then creating the swap
    379      * file may destroy it!  Reported on MS-DOS and Win 95.
    380      * If the name is too long we might crash further on, quit here.
    381      */
    382     if (fname != NULL && *fname != NUL)
    383     {
    384 	p = fname + STRLEN(fname);
    385 	if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
    386 	{
    387 	    filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
    388 	    msg_end();
    389 	    msg_scroll = msg_save;
    390 	    return FAIL;
    391 	}
    392     }
    393 
    394 #ifdef UNIX
    395     /*
    396      * On Unix it is possible to read a directory, so we have to
    397      * check for it before the mch_open().
    398      */
    399     if (!read_stdin && !read_buffer)
    400     {
    401 	perm = mch_getperm(fname);
    402 	if (perm >= 0 && !S_ISREG(perm)		    /* not a regular file ... */
    403 # ifdef S_ISFIFO
    404 		      && !S_ISFIFO(perm)	    /* ... or fifo */
    405 # endif
    406 # ifdef S_ISSOCK
    407 		      && !S_ISSOCK(perm)	    /* ... or socket */
    408 # endif
    409 						)
    410 	{
    411 	    if (S_ISDIR(perm))
    412 		filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
    413 	    else
    414 		filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
    415 	    msg_end();
    416 	    msg_scroll = msg_save;
    417 	    return FAIL;
    418 	}
    419 
    420 # if defined(MSDOS) || defined(MSWIN) || defined(OS2)
    421 	/*
    422 	 * MS-Windows allows opening a device, but we will probably get stuck
    423 	 * trying to read it.
    424 	 */
    425 	if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
    426 	{
    427 	    filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option"), 0);
    428 	    msg_end();
    429 	    msg_scroll = msg_save;
    430 	    return FAIL;
    431 	}
    432 # endif
    433     }
    434 #endif
    435 
    436     /* set default 'fileformat' */
    437     if (set_options)
    438     {
    439 	if (eap != NULL && eap->force_ff != 0)
    440 	    set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
    441 	else if (*p_ffs != NUL)
    442 	    set_fileformat(default_fileformat(), OPT_LOCAL);
    443     }
    444 
    445     /* set or reset 'binary' */
    446     if (eap != NULL && eap->force_bin != 0)
    447     {
    448 	int	oldval = curbuf->b_p_bin;
    449 
    450 	curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
    451 	set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
    452     }
    453 
    454     /*
    455      * When opening a new file we take the readonly flag from the file.
    456      * Default is r/w, can be set to r/o below.
    457      * Don't reset it when in readonly mode
    458      * Only set/reset b_p_ro when BF_CHECK_RO is set.
    459      */
    460     check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
    461     if (check_readonly && !readonlymode)
    462 	curbuf->b_p_ro = FALSE;
    463 
    464     if (newfile && !read_stdin && !read_buffer)
    465     {
    466 	/* Remember time of file.
    467 	 * For RISCOS, also remember the filetype.
    468 	 */
    469 	if (mch_stat((char *)fname, &st) >= 0)
    470 	{
    471 	    buf_store_time(curbuf, &st, fname);
    472 	    curbuf->b_mtime_read = curbuf->b_mtime;
    473 
    474 #if defined(RISCOS) && defined(FEAT_OSFILETYPE)
    475 	    /* Read the filetype into the buffer local filetype option. */
    476 	    mch_read_filetype(fname);
    477 #endif
    478 #ifdef UNIX
    479 	    /*
    480 	     * Use the protection bits of the original file for the swap file.
    481 	     * This makes it possible for others to read the name of the
    482 	     * edited file from the swapfile, but only if they can read the
    483 	     * edited file.
    484 	     * Remove the "write" and "execute" bits for group and others
    485 	     * (they must not write the swapfile).
    486 	     * Add the "read" and "write" bits for the user, otherwise we may
    487 	     * not be able to write to the file ourselves.
    488 	     * Setting the bits is done below, after creating the swap file.
    489 	     */
    490 	    swap_mode = (st.st_mode & 0644) | 0600;
    491 #endif
    492 #ifdef FEAT_CW_EDITOR
    493 	    /* Get the FSSpec on MacOS
    494 	     * TODO: Update it properly when the buffer name changes
    495 	     */
    496 	    (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
    497 #endif
    498 #ifdef VMS
    499 	    curbuf->b_fab_rfm = st.st_fab_rfm;
    500 	    curbuf->b_fab_rat = st.st_fab_rat;
    501 	    curbuf->b_fab_mrs = st.st_fab_mrs;
    502 #endif
    503 	}
    504 	else
    505 	{
    506 	    curbuf->b_mtime = 0;
    507 	    curbuf->b_mtime_read = 0;
    508 	    curbuf->b_orig_size = 0;
    509 	    curbuf->b_orig_mode = 0;
    510 	}
    511 
    512 	/* Reset the "new file" flag.  It will be set again below when the
    513 	 * file doesn't exist. */
    514 	curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
    515     }
    516 
    517 /*
    518  * for UNIX: check readonly with perm and mch_access()
    519  * for RISCOS: same as Unix, otherwise file gets re-datestamped!
    520  * for MSDOS and Amiga: check readonly by trying to open the file for writing
    521  */
    522     file_readonly = FALSE;
    523     if (read_stdin)
    524     {
    525 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
    526 	/* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */
    527 	setmode(0, O_BINARY);
    528 #endif
    529     }
    530     else if (!read_buffer)
    531     {
    532 #ifdef USE_MCH_ACCESS
    533 	if (
    534 # ifdef UNIX
    535 	    !(perm & 0222) ||
    536 # endif
    537 				mch_access((char *)fname, W_OK))
    538 	    file_readonly = TRUE;
    539 	fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
    540 #else
    541 	if (!newfile
    542 		|| readonlymode
    543 		|| (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
    544 	{
    545 	    file_readonly = TRUE;
    546 	    /* try to open ro */
    547 	    fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
    548 	}
    549 #endif
    550     }
    551 
    552     if (fd < 0)			    /* cannot open at all */
    553     {
    554 #ifndef UNIX
    555 	int	isdir_f;
    556 #endif
    557 	msg_scroll = msg_save;
    558 #ifndef UNIX
    559 	/*
    560 	 * On MSDOS and Amiga we can't open a directory, check here.
    561 	 */
    562 	isdir_f = (mch_isdir(fname));
    563 	perm = mch_getperm(fname);  /* check if the file exists */
    564 	if (isdir_f)
    565 	{
    566 	    filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
    567 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
    568 	}
    569 	else
    570 #endif
    571 	    if (newfile)
    572 	    {
    573 		if (perm < 0)
    574 		{
    575 		    /*
    576 		     * Set the 'new-file' flag, so that when the file has
    577 		     * been created by someone else, a ":w" will complain.
    578 		     */
    579 		    curbuf->b_flags |= BF_NEW;
    580 
    581 		    /* Create a swap file now, so that other Vims are warned
    582 		     * that we are editing this file.  Don't do this for a
    583 		     * "nofile" or "nowrite" buffer type. */
    584 #ifdef FEAT_QUICKFIX
    585 		    if (!bt_dontwrite(curbuf))
    586 #endif
    587 			check_need_swap(newfile);
    588 		    if (dir_of_file_exists(fname))
    589 			filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
    590 		    else
    591 			filemess(curbuf, sfname,
    592 					   (char_u *)_("[New DIRECTORY]"), 0);
    593 #ifdef FEAT_VIMINFO
    594 		    /* Even though this is a new file, it might have been
    595 		     * edited before and deleted.  Get the old marks. */
    596 		    check_marks_read();
    597 #endif
    598 #ifdef FEAT_MBYTE
    599 		    if (eap != NULL && eap->force_enc != 0)
    600 		    {
    601 			/* set forced 'fileencoding' */
    602 			fenc = enc_canonize(eap->cmd + eap->force_enc);
    603 			if (fenc != NULL)
    604 			    set_string_option_direct((char_u *)"fenc", -1,
    605 						 fenc, OPT_FREE|OPT_LOCAL, 0);
    606 			vim_free(fenc);
    607 		    }
    608 #endif
    609 #ifdef FEAT_AUTOCMD
    610 		    apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
    611 							  FALSE, curbuf, eap);
    612 #endif
    613 		    /* remember the current fileformat */
    614 		    save_file_ff(curbuf);
    615 
    616 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
    617 		    if (aborting())   /* autocmds may abort script processing */
    618 			return FAIL;
    619 #endif
    620 		    return OK;	    /* a new file is not an error */
    621 		}
    622 		else
    623 		{
    624 		    filemess(curbuf, sfname, (char_u *)(
    625 # ifdef EFBIG
    626 			    (errno == EFBIG) ? _("[File too big]") :
    627 # endif
    628 						_("[Permission Denied]")), 0);
    629 		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
    630 		}
    631 	    }
    632 
    633 	return FAIL;
    634     }
    635 
    636     /*
    637      * Only set the 'ro' flag for readonly files the first time they are
    638      * loaded.	Help files always get readonly mode
    639      */
    640     if ((check_readonly && file_readonly) || curbuf->b_help)
    641 	curbuf->b_p_ro = TRUE;
    642 
    643     if (set_options)
    644     {
    645 	curbuf->b_p_eol = TRUE;
    646 	curbuf->b_start_eol = TRUE;
    647 #ifdef FEAT_MBYTE
    648 	curbuf->b_p_bomb = FALSE;
    649 #endif
    650     }
    651 
    652     /* Create a swap file now, so that other Vims are warned that we are
    653      * editing this file.
    654      * Don't do this for a "nofile" or "nowrite" buffer type. */
    655 #ifdef FEAT_QUICKFIX
    656     if (!bt_dontwrite(curbuf))
    657 #endif
    658     {
    659 	check_need_swap(newfile);
    660 #ifdef UNIX
    661 	/* Set swap file protection bits after creating it. */
    662 	if (swap_mode > 0 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
    663 	    (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode);
    664 #endif
    665     }
    666 
    667 #if defined(HAS_SWAP_EXISTS_ACTION)
    668     /* If "Quit" selected at ATTENTION dialog, don't load the file */
    669     if (swap_exists_action == SEA_QUIT)
    670     {
    671 	if (!read_buffer && !read_stdin)
    672 	    close(fd);
    673 	return FAIL;
    674     }
    675 #endif
    676 
    677     ++no_wait_return;	    /* don't wait for return yet */
    678 
    679     /*
    680      * Set '[ mark to the line above where the lines go (line 1 if zero).
    681      */
    682     curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
    683     curbuf->b_op_start.col = 0;
    684 
    685 #ifdef FEAT_AUTOCMD
    686     if (!read_buffer)
    687     {
    688 	int	m = msg_scroll;
    689 	int	n = msg_scrolled;
    690 	buf_T	*old_curbuf = curbuf;
    691 
    692 	/*
    693 	 * The file must be closed again, the autocommands may want to change
    694 	 * the file before reading it.
    695 	 */
    696 	if (!read_stdin)
    697 	    close(fd);		/* ignore errors */
    698 
    699 	/*
    700 	 * The output from the autocommands should not overwrite anything and
    701 	 * should not be overwritten: Set msg_scroll, restore its value if no
    702 	 * output was done.
    703 	 */
    704 	msg_scroll = TRUE;
    705 	if (filtering)
    706 	    apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
    707 							  FALSE, curbuf, eap);
    708 	else if (read_stdin)
    709 	    apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
    710 							  FALSE, curbuf, eap);
    711 	else if (newfile)
    712 	    apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
    713 							  FALSE, curbuf, eap);
    714 	else
    715 	    apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
    716 							    FALSE, NULL, eap);
    717 	if (msg_scrolled == n)
    718 	    msg_scroll = m;
    719 
    720 #ifdef FEAT_EVAL
    721 	if (aborting())	    /* autocmds may abort script processing */
    722 	{
    723 	    --no_wait_return;
    724 	    msg_scroll = msg_save;
    725 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
    726 	    return FAIL;
    727 	}
    728 #endif
    729 	/*
    730 	 * Don't allow the autocommands to change the current buffer.
    731 	 * Try to re-open the file.
    732 	 */
    733 	if (!read_stdin && (curbuf != old_curbuf
    734 		|| (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
    735 	{
    736 	    --no_wait_return;
    737 	    msg_scroll = msg_save;
    738 	    if (fd < 0)
    739 		EMSG(_("E200: *ReadPre autocommands made the file unreadable"));
    740 	    else
    741 		EMSG(_("E201: *ReadPre autocommands must not change current buffer"));
    742 	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
    743 	    return FAIL;
    744 	}
    745     }
    746 #endif /* FEAT_AUTOCMD */
    747 
    748     /* Autocommands may add lines to the file, need to check if it is empty */
    749     wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
    750 
    751     if (!recoverymode && !filtering && !(flags & READ_DUMMY))
    752     {
    753 	/*
    754 	 * Show the user that we are busy reading the input.  Sometimes this
    755 	 * may take a while.  When reading from stdin another program may
    756 	 * still be running, don't move the cursor to the last line, unless
    757 	 * always using the GUI.
    758 	 */
    759 	if (read_stdin)
    760 	{
    761 #ifndef ALWAYS_USE_GUI
    762 	    mch_msg(_("Vim: Reading from stdin...\n"));
    763 #endif
    764 #ifdef FEAT_GUI
    765 	    /* Also write a message in the GUI window, if there is one. */
    766 	    if (gui.in_use && !gui.dying && !gui.starting)
    767 	    {
    768 		p = (char_u *)_("Reading from stdin...");
    769 		gui_write(p, (int)STRLEN(p));
    770 	    }
    771 #endif
    772 	}
    773 	else if (!read_buffer)
    774 	    filemess(curbuf, sfname, (char_u *)"", 0);
    775     }
    776 
    777     msg_scroll = FALSE;			/* overwrite the file message */
    778 
    779     /*
    780      * Set linecnt now, before the "retry" caused by a wrong guess for
    781      * fileformat, and after the autocommands, which may change them.
    782      */
    783     linecnt = curbuf->b_ml.ml_line_count;
    784 
    785 #ifdef FEAT_MBYTE
    786     /* "++bad=" argument. */
    787     if (eap != NULL && eap->bad_char != 0)
    788     {
    789 	bad_char_behavior = eap->bad_char;
    790 	if (set_options)
    791 	    curbuf->b_bad_char = eap->bad_char;
    792     }
    793     else
    794 	curbuf->b_bad_char = 0;
    795 
    796     /*
    797      * Decide which 'encoding' to use or use first.
    798      */
    799     if (eap != NULL && eap->force_enc != 0)
    800     {
    801 	fenc = enc_canonize(eap->cmd + eap->force_enc);
    802 	fenc_alloced = TRUE;
    803 	keep_dest_enc = TRUE;
    804     }
    805     else if (curbuf->b_p_bin)
    806     {
    807 	fenc = (char_u *)"";		/* binary: don't convert */
    808 	fenc_alloced = FALSE;
    809     }
    810     else if (curbuf->b_help)
    811     {
    812 	char_u	    firstline[80];
    813 	int	    fc;
    814 
    815 	/* Help files are either utf-8 or latin1.  Try utf-8 first, if this
    816 	 * fails it must be latin1.
    817 	 * Always do this when 'encoding' is "utf-8".  Otherwise only do
    818 	 * this when needed to avoid [converted] remarks all the time.
    819 	 * It is needed when the first line contains non-ASCII characters.
    820 	 * That is only in *.??x files. */
    821 	fenc = (char_u *)"latin1";
    822 	c = enc_utf8;
    823 	if (!c && !read_stdin)
    824 	{
    825 	    fc = fname[STRLEN(fname) - 1];
    826 	    if (TOLOWER_ASC(fc) == 'x')
    827 	    {
    828 		/* Read the first line (and a bit more).  Immediately rewind to
    829 		 * the start of the file.  If the read() fails "len" is -1. */
    830 		len = vim_read(fd, firstline, 80);
    831 		lseek(fd, (off_t)0L, SEEK_SET);
    832 		for (p = firstline; p < firstline + len; ++p)
    833 		    if (*p >= 0x80)
    834 		    {
    835 			c = TRUE;
    836 			break;
    837 		    }
    838 	    }
    839 	}
    840 
    841 	if (c)
    842 	{
    843 	    fenc_next = fenc;
    844 	    fenc = (char_u *)"utf-8";
    845 
    846 	    /* When the file is utf-8 but a character doesn't fit in
    847 	     * 'encoding' don't retry.  In help text editing utf-8 bytes
    848 	     * doesn't make sense. */
    849 	    if (!enc_utf8)
    850 		keep_dest_enc = TRUE;
    851 	}
    852 	fenc_alloced = FALSE;
    853     }
    854     else if (*p_fencs == NUL)
    855     {
    856 	fenc = curbuf->b_p_fenc;	/* use format from buffer */
    857 	fenc_alloced = FALSE;
    858     }
    859     else
    860     {
    861 	fenc_next = p_fencs;		/* try items in 'fileencodings' */
    862 	fenc = next_fenc(&fenc_next);
    863 	fenc_alloced = TRUE;
    864     }
    865 #endif
    866 
    867     /*
    868      * Jump back here to retry reading the file in different ways.
    869      * Reasons to retry:
    870      * - encoding conversion failed: try another one from "fenc_next"
    871      * - BOM detected and fenc was set, need to setup conversion
    872      * - "fileformat" check failed: try another
    873      *
    874      * Variables set for special retry actions:
    875      * "file_rewind"	Rewind the file to start reading it again.
    876      * "advance_fenc"	Advance "fenc" using "fenc_next".
    877      * "skip_read"	Re-use already read bytes (BOM detected).
    878      * "did_iconv"	iconv() conversion failed, try 'charconvert'.
    879      * "keep_fileformat" Don't reset "fileformat".
    880      *
    881      * Other status indicators:
    882      * "tmpname"	When != NULL did conversion with 'charconvert'.
    883      *			Output file has to be deleted afterwards.
    884      * "iconv_fd"	When != -1 did conversion with iconv().
    885      */
    886 retry:
    887 
    888     if (file_rewind)
    889     {
    890 	if (read_buffer)
    891 	{
    892 	    read_buf_lnum = 1;
    893 	    read_buf_col = 0;
    894 	}
    895 	else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0)
    896 	{
    897 	    /* Can't rewind the file, give up. */
    898 	    error = TRUE;
    899 	    goto failed;
    900 	}
    901 	/* Delete the previously read lines. */
    902 	while (lnum > from)
    903 	    ml_delete(lnum--, FALSE);
    904 	file_rewind = FALSE;
    905 #ifdef FEAT_MBYTE
    906 	if (set_options)
    907 	    curbuf->b_p_bomb = FALSE;
    908 	conv_error = 0;
    909 #endif
    910     }
    911 
    912     /*
    913      * When retrying with another "fenc" and the first time "fileformat"
    914      * will be reset.
    915      */
    916     if (keep_fileformat)
    917 	keep_fileformat = FALSE;
    918     else
    919     {
    920 	if (eap != NULL && eap->force_ff != 0)
    921 	    fileformat = get_fileformat_force(curbuf, eap);
    922 	else if (curbuf->b_p_bin)
    923 	    fileformat = EOL_UNIX;		/* binary: use Unix format */
    924 	else if (*p_ffs == NUL)
    925 	    fileformat = get_fileformat(curbuf);/* use format from buffer */
    926 	else
    927 	    fileformat = EOL_UNKNOWN;		/* detect from file */
    928     }
    929 
    930 #ifdef FEAT_MBYTE
    931 # ifdef USE_ICONV
    932     if (iconv_fd != (iconv_t)-1)
    933     {
    934 	/* aborted conversion with iconv(), close the descriptor */
    935 	iconv_close(iconv_fd);
    936 	iconv_fd = (iconv_t)-1;
    937     }
    938 # endif
    939 
    940     if (advance_fenc)
    941     {
    942 	/*
    943 	 * Try the next entry in 'fileencodings'.
    944 	 */
    945 	advance_fenc = FALSE;
    946 
    947 	if (eap != NULL && eap->force_enc != 0)
    948 	{
    949 	    /* Conversion given with "++cc=" wasn't possible, read
    950 	     * without conversion. */
    951 	    notconverted = TRUE;
    952 	    conv_error = 0;
    953 	    if (fenc_alloced)
    954 		vim_free(fenc);
    955 	    fenc = (char_u *)"";
    956 	    fenc_alloced = FALSE;
    957 	}
    958 	else
    959 	{
    960 	    if (fenc_alloced)
    961 		vim_free(fenc);
    962 	    if (fenc_next != NULL)
    963 	    {
    964 		fenc = next_fenc(&fenc_next);
    965 		fenc_alloced = (fenc_next != NULL);
    966 	    }
    967 	    else
    968 	    {
    969 		fenc = (char_u *)"";
    970 		fenc_alloced = FALSE;
    971 	    }
    972 	}
    973 	if (tmpname != NULL)
    974 	{
    975 	    mch_remove(tmpname);		/* delete converted file */
    976 	    vim_free(tmpname);
    977 	    tmpname = NULL;
    978 	}
    979     }
    980 
    981     /*
    982      * Conversion is required when the encoding of the file is different
    983      * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4 (requires
    984      * conversion to UTF-8).
    985      */
    986     fio_flags = 0;
    987     converted = (*fenc != NUL && !same_encoding(p_enc, fenc));
    988     if (converted || enc_unicode != 0)
    989     {
    990 
    991 	/* "ucs-bom" means we need to check the first bytes of the file
    992 	 * for a BOM. */
    993 	if (STRCMP(fenc, ENC_UCSBOM) == 0)
    994 	    fio_flags = FIO_UCSBOM;
    995 
    996 	/*
    997 	 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
    998 	 * done.  This is handled below after read().  Prepare the
    999 	 * fio_flags to avoid having to parse the string each time.
   1000 	 * Also check for Unicode to Latin1 conversion, because iconv()
   1001 	 * appears not to handle this correctly.  This works just like
   1002 	 * conversion to UTF-8 except how the resulting character is put in
   1003 	 * the buffer.
   1004 	 */
   1005 	else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
   1006 	    fio_flags = get_fio_flags(fenc);
   1007 
   1008 # ifdef WIN3264
   1009 	/*
   1010 	 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
   1011 	 * is handled with MultiByteToWideChar().
   1012 	 */
   1013 	if (fio_flags == 0)
   1014 	    fio_flags = get_win_fio_flags(fenc);
   1015 # endif
   1016 
   1017 # ifdef MACOS_X
   1018 	/* Conversion from Apple MacRoman to latin1 or UTF-8 */
   1019 	if (fio_flags == 0)
   1020 	    fio_flags = get_mac_fio_flags(fenc);
   1021 # endif
   1022 
   1023 # ifdef USE_ICONV
   1024 	/*
   1025 	 * Try using iconv() if we can't convert internally.
   1026 	 */
   1027 	if (fio_flags == 0
   1028 #  ifdef FEAT_EVAL
   1029 		&& !did_iconv
   1030 #  endif
   1031 		)
   1032 	    iconv_fd = (iconv_t)my_iconv_open(
   1033 				  enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
   1034 # endif
   1035 
   1036 # ifdef FEAT_EVAL
   1037 	/*
   1038 	 * Use the 'charconvert' expression when conversion is required
   1039 	 * and we can't do it internally or with iconv().
   1040 	 */
   1041 	if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
   1042 #  ifdef USE_ICONV
   1043 						    && iconv_fd == (iconv_t)-1
   1044 #  endif
   1045 		)
   1046 	{
   1047 #  ifdef USE_ICONV
   1048 	    did_iconv = FALSE;
   1049 #  endif
   1050 	    /* Skip conversion when it's already done (retry for wrong
   1051 	     * "fileformat"). */
   1052 	    if (tmpname == NULL)
   1053 	    {
   1054 		tmpname = readfile_charconvert(fname, fenc, &fd);
   1055 		if (tmpname == NULL)
   1056 		{
   1057 		    /* Conversion failed.  Try another one. */
   1058 		    advance_fenc = TRUE;
   1059 		    if (fd < 0)
   1060 		    {
   1061 			/* Re-opening the original file failed! */
   1062 			EMSG(_("E202: Conversion made file unreadable!"));
   1063 			error = TRUE;
   1064 			goto failed;
   1065 		    }
   1066 		    goto retry;
   1067 		}
   1068 	    }
   1069 	}
   1070 	else
   1071 # endif
   1072 	{
   1073 	    if (fio_flags == 0
   1074 # ifdef USE_ICONV
   1075 		    && iconv_fd == (iconv_t)-1
   1076 # endif
   1077 	       )
   1078 	    {
   1079 		/* Conversion wanted but we can't.
   1080 		 * Try the next conversion in 'fileencodings' */
   1081 		advance_fenc = TRUE;
   1082 		goto retry;
   1083 	    }
   1084 	}
   1085     }
   1086 
   1087     /* Set "can_retry" when it's possible to rewind the file and try with
   1088      * another "fenc" value.  It's FALSE when no other "fenc" to try, reading
   1089      * stdin or fixed at a specific enc