Data Structures | Macros | Functions | Variables
fileio.c File Reference
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include "nvim/api/private/helpers.h"
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/buffer_updates.h"
#include "nvim/change.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/diff.h"
#include "nvim/edit.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/userfunc.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/func_attr.h"
#include "nvim/garray.h"
#include "nvim/getchar.h"
#include "nvim/hashtab.h"
#include "nvim/iconv.h"
#include "nvim/input.h"
#include "nvim/mbyte.h"
#include "nvim/memfile.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/option.h"
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
#include "nvim/os/time.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/quickfix.h"
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/sha256.h"
#include "nvim/shada.h"
#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/types.h"
#include "nvim/ui.h"
#include "nvim/ui_compositor.h"
#include "nvim/undo.h"
#include "nvim/vim.h"
#include "nvim/window.h"

Data Structures

struct  bw_info


#define BUFSIZE   8192
#define SMBUFSIZE   256
#define HAS_BW_FLAGS
#define FIO_LATIN1   0x01
#define FIO_UTF8   0x02
#define FIO_UCS2   0x04
#define FIO_UCS4   0x08
#define FIO_UTF16   0x10
#define FIO_ENDIAN_L   0x80
#define FIO_NOCONVERT   0x2000
#define FIO_UCSBOM   0x4000
#define FIO_ALL   (-1)
#define CONV_RESTLEN   30
#define ICONV_MULT   8
#define SET_ERRMSG_NUM(num, msg)   errnum = (num), errmsg = (msg), errmsgarg = 0
#define SET_ERRMSG_ARG(msg, error)   errnum = NULL, errmsg = (msg), errmsgarg = error
#define SET_ERRMSG(msg)   errnum = NULL, errmsg = (msg), errmsgarg = 0


void filemess (buf_T *buf, char_u *name, char_u *s, int attr)
int readfile (char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, linenr_T lines_to_read, exarg_T *eap, int flags, bool silent)
void prep_exarg (exarg_T *eap, const buf_T *buf) FUNC_ATTR_NONNULL_ALL
void set_file_options (int set_options, exarg_T *eap)
 Set default or forced 'fileformat' and 'binary'. More...
void set_forced_fenc (exarg_T *eap)
 Set forced 'fileencoding'. More...
char * new_file_message (void)
int buf_write (buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T end, exarg_T *eap, int append, int forceit, int reset_changed, int filtering)
void msg_add_lines (int insert_space, long lnum, off_T nchars)
 Append line and character count to IObuff. More...
void shorten_buf_fname (buf_T *buf, char_u *dirname, int force)
void shorten_fnames (int force)
 Shorten filenames for all buffers. More...
char * modname (const char *fname, const char *ext, bool prepend_dot) FUNC_ATTR_NONNULL_ARG(2)
bool vim_fgets (char_u *buf, int size, FILE *fp) FUNC_ATTR_NONNULL_ALL
int get2c (FILE *fd)
int get3c (FILE *fd)
int get4c (FILE *fd)
time_t get8ctime (FILE *fd)
char * read_string (FILE *fd, size_t cnt)
bool put_bytes (FILE *fd, uintmax_t number, size_t len)
int put_time (FILE *fd, time_t time_)
int vim_rename (const char_u *from, const char_u *to) FUNC_ATTR_NONNULL_ALL
int check_timestamps (int focus)
int buf_check_timestamp (buf_T *buf) FUNC_ATTR_NONNULL_ALL
void buf_reload (buf_T *buf, int orig_mode, bool reload_options)
void buf_store_file_info (buf_T *buf, FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
void write_lnum_adjust (linenr_T offset)
int readdir_core (garray_T *gap, const char *path, void *context, CheckItem checkitem) FUNC_ATTR_NONNULL_ARG(1
 if (!os_scandir(&dir, path))
 for (;;)
 if (gap->ga_len > 0)
int delete_recursive (const char *name) FUNC_ATTR_NONNULL_ALL
void vim_deltempdir (void)
 Delete the temp directory and all files it contains. More...
char_uvim_gettempdir (void)
char_uvim_tempname (void)
bool match_file_pat (char *pattern, regprog_T **prog, char *fname, char *sfname, char *tail, int allow_dirs)
bool match_file_list (char_u *list, char_u *sfname, char_u *ffname) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1
 if (regpat==NULL)
 xfree (regpat)
 if (match)
char * file_pat_to_reg_pat (const char *pat, const char *pat_end, char *allow_dirs, int no_bslash) FUNC_ATTR_NONNULL_ARG(1)


int Directory dir
return OK
bool char_utail
char_uregpat = (char_u *)file_pat_to_reg_pat((char *)buf, NULL, &allow_dirs, false)
char allow_dirs
bool match
char_up = list
return false

Macro Definition Documentation


#define BUFSIZE   8192


#define CONV_RESTLEN   30


#define FIO_ALL   (-1)


#define FIO_ENDIAN_L   0x80


#define FIO_LATIN1   0x01


#define FIO_NOCONVERT   0x2000


#define FIO_UCS2   0x04


#define FIO_UCS4   0x08


#define FIO_UCSBOM   0x4000


#define FIO_UTF16   0x10


#define FIO_UTF8   0x02


#define HAS_BW_FLAGS


#define ICONV_MULT   8


#define SET_ERRMSG (   msg)    errnum = NULL, errmsg = (msg), errmsgarg = 0


#define SET_ERRMSG_ARG (   msg,
)    errnum = NULL, errmsg = (msg), errmsgarg = error


#define SET_ERRMSG_NUM (   num,
)    errnum = (num), errmsg = (msg), errmsgarg = 0


#define SMBUFSIZE   256



Function Documentation

◆ buf_check_timestamp()

int buf_check_timestamp ( buf_T buf)

Check if buffer "buf" has been changed. Also check if the file for a new buffer unexpectedly appeared.

1 if a changed buffer was found or, 2 if a message has been displayed or, 0 otherwise.

◆ buf_reload()

void buf_reload ( buf_T buf,
int  orig_mode,
bool  reload_options 

Reload a buffer that is already loaded. Used when the file was changed outside of Vim. "orig_mode" is buf->b_orig_mode before the need for reloading was detected. buf->b_orig_mode may have been reset already.

◆ buf_store_file_info()

void buf_store_file_info ( buf_T buf,
FileInfo file_info 

◆ buf_write()

int buf_write ( buf_T buf,
char *  fname,
char *  sfname,
linenr_T  start,
linenr_T  end,
exarg_T eap,
int  append,
int  forceit,
int  reset_changed,
int  filtering 

buf_write() - write to file "fname" lines "start" through "end"

We do our own buffering here because fwrite() is so slow.

If "forceit" is true, we don't care for errors when attempting backups. In case of an error everything possible is done to restore the original file. But when "forceit" is TRUE, we risk losing it.

When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.

This function must NOT use NameBuff (because it's called by autowrite()).

eapfor forced 'ff' and 'fenc', can be NULL!
appendappend to the file
FAIL for failure, OK otherwise

◆ check_timestamps()

int check_timestamps ( int  focus)

Check if any not hidden buffer has been changed. Postpone the check if there are characters in the stuff buffer, a global command is being executed, a mapping is being executed or an autocommand is busy.

focuscalled for GUI focus event
TRUE if some message was written (screen should be redrawn and cursor positioned).

◆ delete_recursive()

int delete_recursive ( const char *  name)

Delete "name" and everything in it, recursively.

nameThe path which should be deleted.
0 for success, -1 if some file was not deleted.

◆ file_pat_to_reg_pat()

char* file_pat_to_reg_pat ( const char *  pat,
const char *  pat_end,
char *  allow_dirs,
int  no_bslash 

Convert the given pattern "pat" which has shell style wildcards in it, into a regular expression, and return the result in allocated memory. If there is a directory path separator to be matched, then TRUE is put in allow_dirs, otherwise FALSE is put there – webb. Handle backslashes before special characters, like "\*" and "\ ".

pat_endfirst char after pattern or NULL
allow_dirsResult passed back out in here
no_bslashDon't use a backward slash as pathsep
NULL on failure.

◆ filemess()

void filemess ( buf_T buf,
char_u name,
char_u s,
int  attr 

◆ for()

for ( ;;  )

◆ get2c()

int get2c ( FILE *  fd)

Read 2 bytes from "fd" and turn them into an int, MSB first.

-1 when encountering EOF.

◆ get3c()

int get3c ( FILE *  fd)

Read 3 bytes from "fd" and turn them into an int, MSB first.

-1 when encountering EOF.

◆ get4c()

int get4c ( FILE *  fd)

Read 4 bytes from "fd" and turn them into an int, MSB first.

-1 when encountering EOF.

◆ get8ctime()

time_t get8ctime ( FILE *  fd)

Read 8 bytes from fd and turn them into a time_t, MSB first.

-1 when encountering EOF.

◆ if() [1/4]

if ( !os_scandir &,  path)

◆ if() [2/4]

if ( gap->  ga_len,

◆ if() [3/4]

if ( match  )

◆ if() [4/4]

if ( regpat  = NULL)

◆ match_file_list()

bool match_file_list ( char_u list,
char_u sfname,
char_u ffname 

Check if a file matches with a pattern in "list". "list" is a comma-separated list of patterns, like 'wildignore'. "sfname" is the short file name or NULL, "ffname" the long file name.

listlist of patterns to match
sfnameshort file name
ffnamefull file name
true if there was a match

◆ match_file_pat()

bool match_file_pat ( char *  pattern,
regprog_T **  prog,
char *  fname,
char *  sfname,
char *  tail,
int  allow_dirs 

Tries matching a filename with a "pattern" ("prog" is NULL), or use the precompiled regprog "prog" ("pattern" is NULL). That avoids calling vim_regcomp() often.

Used for autocommands and 'wildignore'.

patternpattern to match with
progpre-compiled regprog or NULL
fnamefull path of the file name
sfnameshort file name or NULL
tailtail of the path
allow_dirsAllow matching with dir
true if there is a match, false otherwise

◆ modname()

char* modname ( const char *  fname,
const char *  ext,
bool  prepend_dot 

Get new filename ended by given extension.

fnameThe original filename. If NULL, use current directory name and ext to compute new filename.
extThe extension to add to the filename. 4 chars max if prefixed with a dot, 3 otherwise.
prepend_dotIf true, prefix ext with a dot. Does nothing if ext already starts with a dot, or if fname is NULL.
[allocated] - A new filename, made up from:
  • fname + ext, if fname not NULL.
  • current dir + ext, if fname is NULL. Result is guaranteed to:
  • be ended by <ext>.
  • have a basename with at most BASENAMELEN chars: original basename is truncated if necessary.
  • be different than original: basename chars are replaced by "_" if necessary. If that can't be done because truncated value of original filename was made of all underscores, replace first "_" by "v".
  • NULL, if fname is NULL and there was a problem trying to get current directory.

◆ msg_add_lines()

void msg_add_lines ( int  insert_space,
long  lnum,
off_T  nchars 

Append line and character count to IObuff.

◆ new_file_message()

char* new_file_message ( void  )

◆ prep_exarg()

void prep_exarg ( exarg_T eap,
const buf_T buf 

Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be equal to the buffer "buf". Used for calling readfile().

◆ put_bytes()

bool put_bytes ( FILE *  fd,
uintmax_t  number,
size_t  len 

Writes a number to file "fd", most significant bit first, in "len" bytes.

false in case of an error.

◆ put_time()

int put_time ( FILE *  fd,
time_t  time_ 

Writes time_t to file "fd" in 8 bytes.

FAIL when the write failed.

◆ read_string()

char* read_string ( FILE *  fd,
size_t  cnt 

Reads a string of length "cnt" from "fd" into allocated memory.

pointer to the string or NULL when unable to read that many bytes.

◆ readdir_core()

int readdir_core ( garray_T gap,
const char *  path,
void context,
CheckItem  checkitem 

Core part of "readdir()" function. Retrieve the list of files/directories of "path" into "gap".

OK for success, FAIL for failure.

◆ readfile()

int readfile ( char *  fname,
char *  sfname,
linenr_T  from,
linenr_T  lines_to_skip,
linenr_T  lines_to_read,
exarg_T eap,
int  flags,
bool  silent 

Read lines from file "fname" into the buffer after line "from".

  1. We allocate blocks with try_malloc, as big as possible.
  2. Each block is filled with characters from the file with a single read().
  3. The lines are inserted in the buffer with ml_append().

(caller must check that fname != NULL, unless READ_STDIN is used)

"lines_to_skip" is the number of lines that must be skipped "lines_to_read" is the number of lines that are appended When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.

flags: READ_NEW starting to edit a new buffer READ_FILTER reading filter output READ_STDIN read from stdin instead of a file READ_BUFFER read from curbuf instead of a file (converting after reading stdin) READ_DUMMY read into a dummy buffer (to check if file contents changed) READ_KEEP_UNDO don't clear undo info or read it from a file READ_FIFO read from fifo/socket instead of a file

eapcan be NULL!
FAIL for failure, NOTDONE for directory (failure), or OK

◆ set_file_options()

void set_file_options ( int  set_options,
exarg_T eap 

Set default or forced 'fileformat' and 'binary'.

◆ set_forced_fenc()

void set_forced_fenc ( exarg_T eap)

Set forced 'fileencoding'.

◆ shorten_buf_fname()

void shorten_buf_fname ( buf_T buf,
char_u dirname,
int  force 

Shorten filename of a buffer.

forcewhen TRUE: Use full path from now on for files currently being edited, both for file name and swap file name. Try to shorten the file names a bit, if safe to do so. when FALSE: Only try to shorten absolute file names.

For buffers that have buftype "nofile" or "scratch": never change the file name.

◆ shorten_fnames()

void shorten_fnames ( int  force)

Shorten filenames for all buffers.

◆ vim_deltempdir()

void vim_deltempdir ( void  )

Delete the temp directory and all files it contains.

◆ vim_fgets()

bool vim_fgets ( char_u buf,
int  size,
FILE *  fp 

Like fgets(), but if the file line is too long, it is truncated and the rest of the line is thrown away.

[out]bufbuffer to fill
sizesize of the buffer
fpfile to read from
true for EOF or error

◆ vim_gettempdir()

char_u* vim_gettempdir ( void  )
the name of temp directory. This directory would be created on the first call to this function.

◆ vim_rename()

int vim_rename ( const char_u from,
const char_u to 

os_rename() only works if both files are on the same file system, this function will (attempts to?) copy the file across if rename fails – webb

-1 for failure, 0 for success

◆ vim_tempname()

char_u* vim_tempname ( void  )

Return a unique name that can be used for a temp file.

The temp file is NOT created.
pointer to the temp file name or NULL if Neovim can't create temporary directory for its own temporary files.

◆ write_lnum_adjust()

void write_lnum_adjust ( linenr_T  offset)

Adjust the line with missing eol, used for the next write. Used for do_filter(), when the input lines for the filter are deleted.

◆ xfree()

xfree ( regpat  )

Variable Documentation

◆ allow_dirs

char allow_dirs

◆ dir

os_closedir & dir
Initial value:
ga_init(gap, (int)sizeof(char *), 20)

◆ false

return false

◆ match

Initial value:
= match_file_pat((char *)regpat, NULL, (char *)ffname, (char *)sfname, (char *)tail,

◆ OK

return OK

◆ p

while p = list

◆ regpat

regpat = (char_u *)file_pat_to_reg_pat((char *)buf, NULL, &allow_dirs, false)

◆ tail

Initial value:
char_u buf[100]
garray_T * gap
Definition: ex_docmd.c:5496
char allow_dirs
Definition: fileio.c:5554
bool char_u * tail
Definition: fileio.c:5550
char_u * regpat
Definition: fileio.c:5553
char *const buf
Definition: encode.c:234
bool match_file_pat(char *pattern, regprog_T **prog, char *fname, char *sfname, char *tail, int allow_dirs)
Definition: fileio.c:5501
unsigned char char_u
Definition: types.h:12
void ga_init(garray_T *gap, int itemsize, int growsize)
Definition: garray.c:50
return NULL
Definition: eval.c:9914