From 5864edac7be49a0cd0080a65330dd7661ffb5641 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Wed, 29 Dec 2021 02:01:02 +0000 Subject: vim-patch:8.1.2302: :lockmarks does not work for '[ and '] Problem: :lockmarks does not work for '[ and ']. Solution: save and restore '[ and '] marks. (James McCoy, closes vim/vim#5222) https://github.com/vim/vim/commit/f4a1d1c0542df151bc59ac3b798ed198b5c71ccc Test_diff_maintains_change_mark doesn't actually fail without these changes. This is fixed in v8.2.3936. --- src/nvim/fileio.c | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f8cf341836..f6d37adf89 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -242,6 +242,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski bool notconverted = false; // true if conversion wanted but it wasn't possible char_u conv_rest[CONV_RESTLEN]; int conv_restlen = 0; // nr of bytes in conv_rest[] + pos_T orig_start; buf_T *old_curbuf; char_u *old_b_ffname; char_u *old_b_fname; @@ -298,14 +299,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski fname = sfname; #endif - /* - * The BufReadCmd and FileReadCmd events intercept the reading process by - * executing the associated commands instead. - */ + // The BufReadCmd and FileReadCmd events intercept the reading process by + // executing the associated commands instead. if (!filtering && !read_stdin && !read_buffer) { - pos_T pos; - - pos = curbuf->b_op_start; + orig_start = curbuf->b_op_start; // Set '[ mark to the line above where the lines go (line 1 if zero). curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); @@ -335,7 +332,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski return aborting() ? FAIL : OK; } - curbuf->b_op_start = pos; + curbuf->b_op_start = orig_start; } if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) { @@ -576,9 +573,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski ++no_wait_return; // don't wait for return yet - /* - * Set '[ mark to the line above where the lines go (line 1 if zero). - */ + // Set '[ mark to the line above where the lines go (line 1 if zero). + orig_start = curbuf->b_op_start; curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); curbuf->b_op_start.col = 0; @@ -618,6 +614,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski try_mac = (vim_strchr(p_ffs, 'm') != NULL); try_dos = (vim_strchr(p_ffs, 'd') != NULL); try_unix = (vim_strchr(p_ffs, 'x') != NULL); + curbuf->b_op_start = orig_start; if (msg_scrolled == n) { msg_scroll = m; @@ -1888,13 +1885,13 @@ failed: check_cursor_lnum(); beginline(BL_WHITE | BL_FIX); // on first non-blank - /* - * Set '[ and '] marks to the newly read lines. - */ - curbuf->b_op_start.lnum = from + 1; - curbuf->b_op_start.col = 0; - curbuf->b_op_end.lnum = from + linecnt; - curbuf->b_op_end.col = 0; + if (!cmdmod.lockmarks) { + // Set '[ and '] marks to the newly read lines. + curbuf->b_op_start.lnum = from + 1; + curbuf->b_op_start.col = 0; + curbuf->b_op_end.lnum = from + linecnt; + curbuf->b_op_end.col = 0; + } } msg_scroll = msg_save; @@ -2252,6 +2249,8 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ int write_undo_file = FALSE; context_sha256_T sha_ctx; unsigned int bkc = get_bkc_value(buf); + const pos_T orig_start = buf->b_op_start; + const pos_T orig_end = buf->b_op_end; if (fname == NULL || *fname == NUL) { // safety check return FAIL; @@ -2432,7 +2431,13 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) || did_cmd || nofile_err || aborting()) { - --no_wait_return; + if (buf != NULL && cmdmod.lockmarks) { + // restore the original '[ and '] positions + buf->b_op_start = orig_start; + buf->b_op_end = orig_end; + } + + no_wait_return--; msg_scroll = msg_save; if (nofile_err) { emsg(_("E676: No matching autocommands for acwrite buffer")); @@ -2513,6 +2518,11 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ } } + if (cmdmod.lockmarks) { + // restore the original '[ and '] positions + buf->b_op_start = orig_start; + buf->b_op_end = orig_end; + } if (shortmess(SHM_OVER) && !exiting) { msg_scroll = FALSE; // overwrite previous file message -- cgit From 9b0363d365f92b1a31bcf0fc32969a4667c11e95 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 4 Feb 2022 22:17:25 +0800 Subject: vim-patch:8.2.1128: the write message mentions characters, but it's bytes Problem: The write message mentions characters, but it's actually bytes. Solution: Change "C" to "B" and "characters" to "bytes". https://github.com/vim/vim/commit/3f40ce78f5c178d15871bd784ed878c78f0b8a44 --- src/nvim/fileio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f6d37adf89..f28ee1bfcb 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3863,7 +3863,7 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) *p++ = ' '; } if (shortmess(SHM_LINES)) { - vim_snprintf((char *)p, IOSIZE - (p - IObuff), "%" PRId64 "L, %" PRId64 "C", + vim_snprintf((char *)p, IOSIZE - (p - IObuff), "%" PRId64 "L, %" PRId64 "B", (int64_t)lnum, (int64_t)nchars); } else { vim_snprintf((char *)p, IOSIZE - (p - IObuff), @@ -3871,7 +3871,7 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) (int64_t)lnum); p += STRLEN(p); vim_snprintf((char *)p, IOSIZE - (p - IObuff), - NGETTEXT("%" PRId64 " character", "%" PRId64 " characters", nchars), + NGETTEXT("%" PRId64 " byte", "%" PRId64 " bytes", nchars), (int64_t)nchars); } } -- cgit From 47a98ab3941fbb664879f67aa0270a9f93d2bed4 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 9 Feb 2022 19:24:41 +0800 Subject: vim-patch:8.2.2199: first write after setting 'eol' does not have NL added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: First write after setting 'eol' does not have NL added. (Tomáš Janoušek) Solution: Only use b_no_eol_lnum when doing a binary write. (closes vim/vim#7535) https://github.com/vim/vim/commit/b3c8b1d25414f2e24ad03551cdf125b3e2c142b1 --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f28ee1bfcb..8e1be3bbf7 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3318,7 +3318,7 @@ restore_backup: if (end == 0 || (lnum == end && (write_bin || !buf->b_p_fixeol) - && (lnum == buf->b_no_eol_lnum + && ((write_bin && lnum == buf->b_no_eol_lnum) || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) { lnum++; // written the line, count it no_eol = true; -- cgit From 03348e5b9db3f057057a70581ef71180c3cb6527 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 13 Feb 2022 21:33:28 +0800 Subject: vim-patch:8.2.3510: changes are only detected with one second accuracy Problem: Changes are only detected with one second accuracy. Solution: Use the nanosecond time if possible. (Leah Neukirchen, closes vim/vim#8873, closes vim/vim#8875) https://github.com/vim/vim/commit/0a7984af5601323fae7b3398f05a48087db7b767 In Nvim Test_checktime_fast() is also flaky. Add a delay to avoid that. --- src/nvim/fileio.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 8e1be3bbf7..475b44bc49 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -405,6 +405,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski if (os_fileinfo((char *)fname, &file_info)) { buf_store_file_info(curbuf, &file_info); curbuf->b_mtime_read = curbuf->b_mtime; + curbuf->b_mtime_read_ns = curbuf->b_mtime_ns; #ifdef UNIX /* * Use the protection bits of the original file for the swap file. @@ -421,7 +422,9 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski #endif } else { curbuf->b_mtime = 0; + curbuf->b_mtime_ns = 0; curbuf->b_mtime_read = 0; + curbuf->b_mtime_read_ns = 0; curbuf->b_orig_size = 0; curbuf->b_orig_mode = 0; } @@ -3695,11 +3698,12 @@ nofail: msg_puts_attr(_("don't quit the editor until the file is successfully written!"), attr | MSG_HIST); - /* Update the timestamp to avoid an "overwrite changed file" - * prompt when writing again. */ + // Update the timestamp to avoid an "overwrite changed file" + // prompt when writing again. if (os_fileinfo((char *)fname, &file_info_old)) { buf_store_file_info(buf, &file_info_old); buf->b_mtime_read = buf->b_mtime; + buf->b_mtime_read_ns = buf->b_mtime_ns; } } } @@ -3893,8 +3897,7 @@ static void msg_add_eol(void) static int check_mtime(buf_T *buf, FileInfo *file_info) { if (buf->b_mtime_read != 0 - && time_differs(file_info->stat.st_mtim.tv_sec, - buf->b_mtime_read)) { + && time_differs(file_info, buf->b_mtime_read, buf->b_mtime_read_ns)) { msg_scroll = true; // Don't overwrite messages here. msg_silent = 0; // Must give this prompt. // Don't use emsg() here, don't want to flush the buffers. @@ -3908,19 +3911,17 @@ static int check_mtime(buf_T *buf, FileInfo *file_info) return OK; } -/// Return true if the times differ -/// -/// @param t1 first time -/// @param t2 second time -static bool time_differs(long t1, long t2) FUNC_ATTR_CONST +static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST { #if defined(__linux__) || defined(MSWIN) // On a FAT filesystem, esp. under Linux, there are only 5 bits to store // the seconds. Since the roundoff is done when flushing the inode, the // time may change unexpectedly by one second!!! - return t1 - t2 > 1 || t2 - t1 > 1; + return (long)file_info->stat.st_mtim.tv_sec - mtime > 1 + || mtime - (long)file_info->stat.st_mtim.tv_sec > 1 + || (long)file_info->stat.st_mtim.tv_nsec != mtime_ns; #else - return t1 != t2; + return (long)file_info->stat.st_mtim.tv_sec != mtime; #endif } @@ -4943,7 +4944,7 @@ int buf_check_timestamp(buf_T *buf) if (!(buf->b_flags & BF_NOTEDITED) && buf->b_mtime != 0 && (!(file_info_ok = os_fileinfo((char *)buf->b_ffname, &file_info)) - || time_differs(file_info.stat.st_mtim.tv_sec, buf->b_mtime) + || time_differs(&file_info, buf->b_mtime, buf->b_mtime_ns) || (int)file_info.stat.st_mode != buf->b_orig_mode)) { const long prev_b_mtime = buf->b_mtime; @@ -5034,6 +5035,7 @@ int buf_check_timestamp(buf_T *buf) // Only timestamp changed, store it to avoid a warning // in check_mtime() later. buf->b_mtime_read = buf->b_mtime; + buf->b_mtime_read_ns = buf->b_mtime_ns; } } } @@ -5262,6 +5264,7 @@ void buf_store_file_info(buf_T *buf, FileInfo *file_info) FUNC_ATTR_NONNULL_ALL { buf->b_mtime = file_info->stat.st_mtim.tv_sec; + buf->b_mtime_ns = file_info->stat.st_mtim.tv_nsec; buf->b_orig_size = os_fileinfo_size(file_info); buf->b_orig_mode = (int)file_info->stat.st_mode; } -- cgit From 0f1c7059361f5db894fa0715ee6ee15d444a9c2b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 13 Feb 2022 21:33:28 +0800 Subject: vim-patch:8.2.3515: nano time test fails on Mac and FreeBSD Problem: Nano time test fails on Mac and FreeBSD. Solution: Also check nano time when not on Linux. (Ozaki Kiichi, closes vim/vim#9000) https://github.com/vim/vim/commit/def69dffb3d09a69629b071c89b7893a1783ba53 --- src/nvim/fileio.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 475b44bc49..110981159f 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3913,15 +3913,15 @@ static int check_mtime(buf_T *buf, FileInfo *file_info) static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST { + return (long)file_info->stat.st_mtim.tv_nsec != mtime_ns #if defined(__linux__) || defined(MSWIN) - // On a FAT filesystem, esp. under Linux, there are only 5 bits to store - // the seconds. Since the roundoff is done when flushing the inode, the - // time may change unexpectedly by one second!!! - return (long)file_info->stat.st_mtim.tv_sec - mtime > 1 - || mtime - (long)file_info->stat.st_mtim.tv_sec > 1 - || (long)file_info->stat.st_mtim.tv_nsec != mtime_ns; + // On a FAT filesystem, esp. under Linux, there are only 5 bits to store + // the seconds. Since the roundoff is done when flushing the inode, the + // time may change unexpectedly by one second!!! + || (long)file_info->stat.st_mtim.tv_sec - mtime > 1 + || mtime - (long)file_info->stat.st_mtim.tv_sec > 1; #else - return (long)file_info->stat.st_mtim.tv_sec != mtime; + || (long)file_info->stat.st_mtim.tv_sec != mtime; #endif } -- cgit From 5220891571a799fd630cbcbe836d1f9e3d2dc1fa Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 14 Feb 2022 11:35:25 +0800 Subject: vim-patch:8.2.4343: when reloading not all properties are detected Problem: When reloading not all properties are detected. Solution: Add the "edit" value to v:fcs_choice. (Rob Pilling, closes vim/vim#9579) https://github.com/vim/vim/commit/8196e94a8b72ed8618605cb66615571313097d78 Cherry-pick some test changes from patch 8.1.1826. --- src/nvim/fileio.c | 70 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 30 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 110981159f..f5824d83be 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2013,10 +2013,8 @@ static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp) return lnum; } -/* - * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be - * equal to the buffer "buf". Used for calling readfile(). - */ +/// Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary' to be +/// equal to the buffer "buf". Used for calling readfile(). void prep_exarg(exarg_T *eap, const buf_T *buf) FUNC_ATTR_NONNULL_ALL { @@ -4901,13 +4899,11 @@ static int move_lines(buf_T *frombuf, buf_T *tobuf) return retval; } -/* - * Check if buffer "buf" has been changed. - * Also check if the file for a new buffer unexpectedly appeared. - * return 1 if a changed buffer was found. - * return 2 if a message has been displayed. - * return 0 otherwise. - */ +/// Check if buffer "buf" has been changed. +/// Also check if the file for a new buffer unexpectedly appeared. +/// return 1 if a changed buffer was found. +/// return 2 if a message has been displayed. +/// return 0 otherwise. int buf_check_timestamp(buf_T *buf) FUNC_ATTR_NONNULL_ALL { @@ -4916,7 +4912,11 @@ int buf_check_timestamp(buf_T *buf) char *mesg = NULL; char *mesg2 = ""; bool helpmesg = false; - bool reload = false; + enum { + RELOAD_NONE, + RELOAD_NORMAL, + RELOAD_DETECT + } reload = RELOAD_NONE; bool can_reload = false; uint64_t orig_size = buf->b_orig_size; int orig_mode = buf->b_orig_mode; @@ -4969,7 +4969,7 @@ int buf_check_timestamp(buf_T *buf) // If 'autoread' is set, the buffer has no changes and the file still // exists, reload the buffer. Use the buffer-local option value if it // was set, the global option value otherwise. - reload = true; + reload = RELOAD_NORMAL; } else { if (!file_info_ok) { reason = "deleted"; @@ -5000,7 +5000,9 @@ int buf_check_timestamp(buf_T *buf) } s = get_vim_var_str(VV_FCS_CHOICE); if (STRCMP(s, "reload") == 0 && *reason != 'd') { - reload = true; + reload = RELOAD_NORMAL; + } else if (STRCMP(s, "edit") == 0) { + reload = RELOAD_DETECT; } else if (STRCMP(s, "ask") == 0) { n = false; } else { @@ -5064,9 +5066,15 @@ int buf_check_timestamp(buf_T *buf) xstrlcat(tbuf, "\n", tbuf_len - 1); xstrlcat(tbuf, mesg2, tbuf_len - 1); } - if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), (char_u *)tbuf, - (char_u *)_("&OK\n&Load File"), 1, NULL, true) == 2) { - reload = true; + switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"), (char_u *)tbuf, + (char_u *)_("&OK\n&Load File\nLoad File &and Options"), + 1, NULL, true)) { + case 2: + reload = RELOAD_NORMAL; + break; + case 3: + reload = RELOAD_DETECT; + break; } } else if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) { if (*mesg2 != NUL) { @@ -5100,9 +5108,9 @@ int buf_check_timestamp(buf_T *buf) xfree(tbuf); } - if (reload) { + if (reload != RELOAD_NONE) { // Reload the buffer. - buf_reload(buf, orig_mode); + buf_reload(buf, orig_mode, reload == RELOAD_DETECT); if (buf->b_p_udf && buf->b_ffname != NULL) { char_u hash[UNDO_HASH_SIZE]; @@ -5120,13 +5128,11 @@ int buf_check_timestamp(buf_T *buf) return retval; } -/* - * 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. - */ -void buf_reload(buf_T *buf, int orig_mode) +/// 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. +void buf_reload(buf_T *buf, int orig_mode, bool reload_options) { exarg_T ea; pos_T old_cursor; @@ -5141,11 +5147,15 @@ void buf_reload(buf_T *buf, int orig_mode) // set curwin/curbuf for "buf" and save some things aucmd_prepbuf(&aco, buf); - // We only want to read the text from the file, not reset the syntax - // highlighting, clear marks, diff status, etc. Force the fileformat and - // encoding to be the same. + // Unless reload_options is set, we only want to read the text from the + // file, not reset the syntax highlighting, clear marks, diff status, etc. + // Force the fileformat and encoding to be the same. + if (reload_options) { + memset(&ea, 0, sizeof(ea)); + } else { + prep_exarg(&ea, buf); + } - prep_exarg(&ea, buf); old_cursor = curwin->w_cursor; old_topline = curwin->w_topline; -- cgit From 991e472881bf29805982b402c1a010cde051ded3 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Fri, 28 May 2021 15:45:34 -0400 Subject: feat(lua): add api and lua autocmds --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f5824d83be..965aa8749d 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3796,7 +3796,7 @@ static int set_rw_fname(char_u *fname, char_u *sfname) // Do filetype detection now if 'filetype' is empty. if (*curbuf->b_p_ft == NUL) { - if (au_has_group((char_u *)"filetypedetect")) { + if (augroup_exists("filetypedetect")) { (void)do_doautocmd((char_u *)"filetypedetect BufRead", false, NULL); } do_modelines(0); -- cgit From ff032f2710974dcf5930187f1925534da93db199 Mon Sep 17 00:00:00 2001 From: Dundar Göc Date: Sun, 6 Mar 2022 12:40:31 +0100 Subject: refactor: remove redundant casts --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 965aa8749d..c5a89d3371 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3911,13 +3911,13 @@ static int check_mtime(buf_T *buf, FileInfo *file_info) static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST { - return (long)file_info->stat.st_mtim.tv_nsec != mtime_ns + return file_info->stat.st_mtim.tv_nsec != mtime_ns #if defined(__linux__) || defined(MSWIN) // On a FAT filesystem, esp. under Linux, there are only 5 bits to store // the seconds. Since the roundoff is done when flushing the inode, the // time may change unexpectedly by one second!!! - || (long)file_info->stat.st_mtim.tv_sec - mtime > 1 - || mtime - (long)file_info->stat.st_mtim.tv_sec > 1; + || file_info->stat.st_mtim.tv_sec - mtime > 1 + || mtime - file_info->stat.st_mtim.tv_sec > 1; #else || (long)file_info->stat.st_mtim.tv_sec != mtime; #endif -- cgit From 05f643f9d235b0db881acf94ce05ad3359ffb058 Mon Sep 17 00:00:00 2001 From: Dundar Göc Date: Tue, 8 Mar 2022 19:58:45 +0100 Subject: chore(lgtm): fix "empty block without comment" warnings --- src/nvim/fileio.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c5a89d3371..971ae9abae 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -4961,9 +4961,8 @@ int buf_check_timestamp(buf_T *buf) buf_store_file_info(buf, &file_info); } - // Don't do anything for a directory. Might contain the file - // explorer. if (os_isdir(buf->b_fname)) { + // Don't do anything for a directory. Might contain the file explorer. } else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) && !bufIsChanged(buf) && file_info_ok) { // If 'autoread' is set, the buffer has no changes and the file still -- cgit From d0cb8744d84822209edd0ac242f2400c784e6dc5 Mon Sep 17 00:00:00 2001 From: Dundar Göc Date: Wed, 9 Mar 2022 21:00:39 +0100 Subject: refactor(uncrustify): disable uncrustify for misformatted code sections Uncrustify version 0.74 has a bug that deindents and misformats the entire fileio.c. --- src/nvim/fileio.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 971ae9abae..41e67e5b3b 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3,6 +3,8 @@ // fileio.c: read from and write to a file +// uncrustify:off + #include #include #include -- cgit From 534f5a419d2ef1c2ad78204a4de48388cc2d7fa2 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Thu, 24 Mar 2022 12:17:21 +0100 Subject: refactor: convert function comments to doxygen format (#17710) --- src/nvim/fileio.c | 156 +++++++++++++++++++++++++----------------------------- 1 file changed, 72 insertions(+), 84 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 41e67e5b3b..fe61a2fc90 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2033,9 +2033,7 @@ void prep_exarg(exarg_T *eap, const buf_T *buf) eap->forceit = FALSE; } -/* - * Set default or forced 'fileformat' and 'binary'. - */ +/// Set default or forced 'fileformat' and 'binary'. void set_file_options(int set_options, exarg_T *eap) { // set default 'fileformat' @@ -2056,9 +2054,7 @@ void set_file_options(int set_options, exarg_T *eap) } } -/* - * Set forced 'fileencoding'. - */ +/// Set forced 'fileencoding'. void set_forced_fenc(exarg_T *eap) { if (eap->force_enc != 0) { @@ -2068,12 +2064,12 @@ void set_forced_fenc(exarg_T *eap) } } -// Find next fileencoding to use from 'fileencodings'. -// "pp" points to fenc_next. It's advanced to the next item. -// When there are no more items, an empty string is returned and *pp is set to -// NULL. -// When *pp is not set to NULL, the result is in allocated memory and "alloced" -// is set to true. +/// Find next fileencoding to use from 'fileencodings'. +/// "pp" points to fenc_next. It's advanced to the next item. +/// When there are no more items, an empty string is returned and *pp is set to +/// NULL. +/// When *pp is not set to NULL, the result is in allocated memory and "alloced" +/// is set to true. static char_u *next_fenc(char_u **pp, bool *alloced) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { @@ -2149,10 +2145,8 @@ static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp) } -/* - * Read marks for the current buffer from the ShaDa file, when we support - * buffer marks and the buffer has a name. - */ +/// Read marks for the current buffer from the ShaDa file, when we support +/// buffer marks and the buffer has a name. static void check_marks_read(void) { if (!curbuf->b_marks_read && get_shada_parameter('\'') > 0 @@ -3761,10 +3755,8 @@ nofail: #undef SET_ERRMSG_NUM } -/* - * Set the name of the current buffer. Use when the buffer doesn't have a - * name and a ":r" or ":w" command with a file name is used. - */ +/// Set the name of the current buffer. Use when the buffer doesn't have a +/// name and a ":r" or ":w" command with a file name is used. static int set_rw_fname(char_u *fname, char_u *sfname) { buf_T *buf = curbuf; @@ -3854,9 +3846,7 @@ static bool msg_add_fileformat(int eol_type) return false; } -/* - * Append line and character count to IObuff. - */ +/// Append line and character count to IObuff. void msg_add_lines(int insert_space, long lnum, off_T nchars) { char_u *p; @@ -3880,20 +3870,16 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) } } -/* - * Append message for missing line separator to IObuff. - */ +/// Append message for missing line separator to IObuff. static void msg_add_eol(void) { STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); } -/* - * Check modification time of file, before writing to it. - * The size isn't checked, because using a tool like "gzip" takes care of - * using the same timestamp but can't set the size. - */ +/// Check modification time of file, before writing to it. +/// The size isn't checked, because using a tool like "gzip" takes care of +/// using the same timestamp but can't set the size. static int check_mtime(buf_T *buf, FileInfo *file_info) { if (buf->b_mtime_read != 0 @@ -3925,12 +3911,10 @@ static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) F #endif } -/* - * Call write() to write a number of bytes to the file. - * Handles 'encoding' conversion. - * - * Return FAIL for failure, OK otherwise. - */ +/// Call write() to write a number of bytes to the file. +/// Handles 'encoding' conversion. +/// +/// @return FAIL for failure, OK otherwise. static int buf_write_bytes(struct bw_info *ip) { int wlen; @@ -4258,12 +4242,11 @@ static int get_fio_flags(const char_u *name) } -/* - * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. - * "size" must be at least 2. - * Return the name of the encoding and set "*lenp" to the length. - * Returns NULL when no BOM found. - */ +/// Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. +/// "size" must be at least 2. +/// +/// @return the name of the encoding and set "*lenp" to the length or, +/// NULL when no BOM found. static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags) { char *name = NULL; @@ -4304,10 +4287,9 @@ static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags) return (char_u *)name; } -/* - * Generate a BOM in "buf[4]" for encoding "name". - * Return the length of the BOM (zero when no BOM). - */ +/// Generate a BOM in "buf[4]" for encoding "name". +/// +/// @return the length of the BOM (zero when no BOM). static int make_bom(char_u *buf, char_u *name) { int flags; @@ -4332,10 +4314,12 @@ static int make_bom(char_u *buf, char_u *name) } /// Shorten filename of a buffer. -/// When "force" is 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 "force" is FALSE: Only try to shorten absolute file names. +/// +/// @param force when 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. void shorten_buf_fname(buf_T *buf, char_u *dirname, int force) @@ -4513,7 +4497,8 @@ bool vim_fgets(char_u *buf, int size, FILE *fp) FUNC_ATTR_NONNULL_ALL } /// Read 2 bytes from "fd" and turn them into an int, MSB first. -/// Returns -1 when encountering EOF. +/// +/// @return -1 when encountering EOF. int get2c(FILE *fd) { const int n = getc(fd); @@ -4528,7 +4513,8 @@ int get2c(FILE *fd) } /// Read 3 bytes from "fd" and turn them into an int, MSB first. -/// Returns -1 when encountering EOF. +/// +/// @return -1 when encountering EOF. int get3c(FILE *fd) { int n = getc(fd); @@ -4548,7 +4534,8 @@ int get3c(FILE *fd) } /// Read 4 bytes from "fd" and turn them into an int, MSB first. -/// Returns -1 when encountering EOF. +/// +/// @return -1 when encountering EOF. int get4c(FILE *fd) { // Use unsigned rather than int otherwise result is undefined @@ -4579,7 +4566,8 @@ int get4c(FILE *fd) } /// Read 8 bytes from `fd` and turn them into a time_t, MSB first. -/// Returns -1 when encountering EOF. +/// +/// @return -1 when encountering EOF. time_t get8ctime(FILE *fd) { time_t n = 0; @@ -4595,7 +4583,8 @@ time_t get8ctime(FILE *fd) } /// Reads a string of length "cnt" from "fd" into allocated memory. -/// @return pointer to the string or NULL when unable to read that many bytes. +/// +/// @return pointer to the string or NULL when unable to read that many bytes. char *read_string(FILE *fd, size_t cnt) { char *str = xmallocz(cnt); @@ -4611,7 +4600,8 @@ char *read_string(FILE *fd, size_t cnt) } /// Writes a number to file "fd", most significant bit first, in "len" bytes. -/// @returns false in case of an error. +/// +/// @return false in case of an error. bool put_bytes(FILE *fd, uintmax_t number, size_t len) { assert(len > 0); @@ -4624,7 +4614,8 @@ bool put_bytes(FILE *fd, uintmax_t number, size_t len) } /// Writes time_t to file "fd" in 8 bytes. -/// @returns FAIL when the write failed. +/// +/// @return FAIL when the write failed. int put_time(FILE *fd, time_t time_) { uint8_t buf[8]; @@ -4635,7 +4626,7 @@ int put_time(FILE *fd, time_t time_) /// 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 /// -/// @return -1 for failure, 0 for success +/// @return -1 for failure, 0 for success int vim_rename(const char_u *from, const char_u *to) FUNC_ATTR_NONNULL_ALL { @@ -4860,11 +4851,10 @@ int check_timestamps(int focus) return didit; } -/* - * Move all the lines from buffer "frombuf" to buffer "tobuf". - * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not - * empty. - */ +/// Move all the lines from buffer "frombuf" to buffer "tobuf". +/// +/// @return OK or FAIL. +/// When FAIL "tobuf" is incomplete and/or "frombuf" is not empty. static int move_lines(buf_T *frombuf, buf_T *tobuf) { buf_T *tbuf = curbuf; @@ -4903,9 +4893,10 @@ static int move_lines(buf_T *frombuf, buf_T *tobuf) /// Check if buffer "buf" has been changed. /// Also check if the file for a new buffer unexpectedly appeared. -/// return 1 if a changed buffer was found. -/// return 2 if a message has been displayed. -/// return 0 otherwise. +/// +/// @return 1 if a changed buffer was found or, +/// 2 if a message has been displayed or, +/// 0 otherwise. int buf_check_timestamp(buf_T *buf) FUNC_ATTR_NONNULL_ALL { @@ -5280,10 +5271,8 @@ void buf_store_file_info(buf_T *buf, FileInfo *file_info) buf->b_orig_mode = (int)file_info->stat.st_mode; } -/* - * Adjust the line with missing eol, used for the next write. - * Used for do_filter(), when the input lines for the filter are deleted. - */ +/// Adjust the line with missing eol, used for the next write. +/// Used for do_filter(), when the input lines for the filter are deleted. void write_lnum_adjust(linenr_T offset) { if (curbuf->b_no_eol_lnum != 0) { // only if there is a missing eol @@ -5355,8 +5344,10 @@ static void vim_maketempdir(void) } /// Delete "name" and everything in it, recursively. +/// /// @param name The path which should be deleted. -/// @return 0 for success, -1 if some file was not deleted. +/// +/// @return 0 for success, -1 if some file was not deleted. int delete_recursive(const char *name) { int result = 0; @@ -5400,8 +5391,8 @@ void vim_deltempdir(void) } } -/// Get the name of temp directory. This directory would be created on the first -/// call to this function. +/// @return the name of temp directory. This directory would be created on the first +/// call to this function. char_u *vim_gettempdir(void) { if (vim_tempdir == NULL) { @@ -5435,8 +5426,8 @@ static bool vim_settempdir(char *tempdir) /// /// @note The temp file is NOT created. /// -/// @return pointer to the temp file name or NULL if Neovim can't create -/// temporary directory for its own temporary files. +/// @return pointer to the temp file name or NULL if Neovim can't create +/// temporary directory for its own temporary files. char_u *vim_tempname(void) { // Temp filename counter. @@ -5747,10 +5738,9 @@ char_u *file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *allo } #if defined(EINTR) -/* - * Version of read() that retries when interrupted by EINTR (possibly - * by a SIGWINCH). - */ + +/// Version of read() that retries when interrupted by EINTR (possibly +/// by a SIGWINCH). long read_eintr(int fd, void *buf, size_t bufsize) { long ret; @@ -5764,10 +5754,8 @@ long read_eintr(int fd, void *buf, size_t bufsize) return ret; } -/* - * Version of write() that retries when interrupted by EINTR (possibly - * by a SIGWINCH). - */ +/// Version of write() that retries when interrupted by EINTR (possibly +/// by a SIGWINCH). long write_eintr(int fd, void *buf, size_t bufsize) { long ret = 0; -- cgit From c2378be3dd5cd7d97f7a22a2472fcee9992b1c31 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 26 Mar 2022 08:27:29 +0800 Subject: vim-patch:8.2.3453: autocmd not executed when editing a directory (#17846) Problem: Autocmd not executed when editing a directory ending in a path separator inside try block. Solution: Return NOTDONE instead of FAIL. (closes vim/vim#8885) https://github.com/vim/vim/commit/40fa12aea352474d229f2f750e954a4318aead4e --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index fe61a2fc90..7905b29876 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -361,7 +361,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); msg_end(); msg_scroll = msg_save; - return FAIL; + return NOTDONE; } } -- cgit From e9e16655af1bacd4b1499fed00a142512c120710 Mon Sep 17 00:00:00 2001 From: Shougo Date: Sun, 3 Apr 2022 21:27:46 +0900 Subject: [RFC] vim-patch:8.1.1378: delete() can not handle a file name that looks li… (#16268) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Delete() can not handle a file name that looks like a pattern. Solution: Use readdir() instead of appending "/*" and expanding wildcards. (Ken Takata, closes vim/vim#4424, closes vim/vim#696) https://github.com/vim/vim/commit/701ff0a3e53d253d7300c385e582659bbff7860d Cherry-pick a change to Test_delete_rf() from patch 8.1.1921. Co-authored-by: zeertzjq --- src/nvim/fileio.c | 74 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 14 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 7905b29876..5e11a9683e 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -22,6 +22,7 @@ #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/eval/userfunc.h" +#include "nvim/eval/typval.h" #include "nvim/ex_cmds.h" #include "nvim/ex_docmd.h" #include "nvim/ex_eval.h" @@ -5343,37 +5344,82 @@ static void vim_maketempdir(void) (void)umask(umask_save); } +/// Core part of "readdir()" function. +/// Retrieve the list of files/directories of "path" into "gap". +/// +/// @return OK for success, FAIL for failure. +int readdir_core(garray_T *gap, const char *path, void *context, CheckItem checkitem) + FUNC_ATTR_NONNULL_ARG(1, 2) +{ + ga_init(gap, (int)sizeof(char *), 20); + + Directory dir; + if (!os_scandir(&dir, path)) { + smsg(_(e_notopen), path); + return FAIL; + } + + for (;;) { + const char *p = os_scandir_next(&dir); + if (p == NULL) { + break; + } + + bool ignore = (p[0] == '.' && (p[1] == NUL || (p[1] == '.' && p[2] == NUL))); + if (!ignore && checkitem != NULL) { + varnumber_T r = checkitem(context, p); + if (r < 0) { + break; + } + if (r == 0) { + ignore = true; + } + } + + if (!ignore) { + ga_grow(gap, 1); + ((char **)gap->ga_data)[gap->ga_len++] = xstrdup(p); + } + } + + os_closedir(&dir); + + if (gap->ga_len > 0) { + sort_strings((char_u **)gap->ga_data, gap->ga_len); + } + + return OK; +} + /// Delete "name" and everything in it, recursively. /// -/// @param name The path which should be deleted. +/// @param name The path which should be deleted. /// /// @return 0 for success, -1 if some file was not deleted. int delete_recursive(const char *name) + FUNC_ATTR_NONNULL_ALL { int result = 0; if (os_isrealdir(name)) { - snprintf((char *)NameBuff, MAXPATHL, "%s/*", name); // NOLINT - - char_u **files; - int file_count; - char_u *exp = vim_strsave(NameBuff); - if (gen_expand_wildcards(1, &exp, &file_count, &files, - EW_DIR | EW_FILE | EW_SILENT | EW_ALLLINKS - | EW_DODOT | EW_EMPTYOK) == OK) { - for (int i = 0; i < file_count; i++) { - if (delete_recursive((const char *)files[i]) != 0) { + char *exp = xstrdup(name); + garray_T ga; + if (readdir_core(&ga, exp, NULL, NULL) == OK) { + for (int i = 0; i < ga.ga_len; i++) { + vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp, ((char_u **)ga.ga_data)[i]); + if (delete_recursive((const char *)NameBuff) != 0) { result = -1; } } - FreeWild(file_count, files); + ga_clear_strings(&ga); } else { result = -1; } - + // Note: "name" value may be changed in delete_recursive(). Must use the saved value. + result = os_rmdir(exp) == 0 ? 0 : -1; xfree(exp); - os_rmdir(name); } else { + // Delete symlink only. result = os_remove(name) == 0 ? 0 : -1; } -- cgit From 271bb32855853b011fceaf0ad2f829bce66b2a19 Mon Sep 17 00:00:00 2001 From: Brian Leung Date: Sun, 3 Apr 2022 01:37:28 -0700 Subject: vim-patch:8.2.4639: not sufficient parenthesis in preprocessor macros Problem: Not sufficient parenthesis in preprocessor macros. Solution: Add more parenthesis. https://github.com/vim/vim/commit/9dac9b1751dd43c02470cc6a2aecaeea27abcc80 --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 5e11a9683e..64ad9a6d80 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -80,7 +80,7 @@ #define FIO_ENDIAN_L 0x80 // little endian #define FIO_NOCONVERT 0x2000 // skip encoding conversion #define FIO_UCSBOM 0x4000 // check for BOM at start of file -#define FIO_ALL -1 // allow all formats +#define FIO_ALL (-1) // allow all formats /* When converting, a read() or write() may leave some bytes to be converted * for the next call. The value is guessed... */ -- cgit From 128bedc0d2435bbc754cdb954447fc1cbfd4ac13 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 6 Apr 2022 05:12:49 +0800 Subject: vim-patch:8.2.4696: delete() with "rf" argument does not report a failure (#18002) Problem: delete() with "rf" argument does not report a failure. Solution: Return -1 if the directory could not be removed. (closes vim/vim#10078) https://github.com/vim/vim/commit/478700336d1c72e133b8ff6841e968c1bb1658ed --- src/nvim/fileio.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 64ad9a6d80..d68dda15f3 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5408,15 +5408,18 @@ int delete_recursive(const char *name) for (int i = 0; i < ga.ga_len; i++) { vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp, ((char_u **)ga.ga_data)[i]); if (delete_recursive((const char *)NameBuff) != 0) { + // Remember the failure but continue deleting any further + // entries. result = -1; } } ga_clear_strings(&ga); + if (os_rmdir(exp) != 0) { + result = -1; + } } else { result = -1; } - // Note: "name" value may be changed in delete_recursive(). Must use the saved value. - result = os_rmdir(exp) == 0 ? 0 : -1; xfree(exp); } else { // Delete symlink only. -- cgit From 356baae80ad42e8a13d650675a0e56e931f08541 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 8 Apr 2022 10:25:22 +0800 Subject: vim-patch:8.2.4704: using "else" after return or break increases indent (#18032) Problem: Using "else" after return or break increases indent. Solution: Remove "else" and reduce indent. (Goc Dundar, closes vim/vim#10099) https://github.com/vim/vim/commit/f26c16144ddb27642c09f2cf5271afd163b36306 --- src/nvim/fileio.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index d68dda15f3..d4407b4982 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1015,27 +1015,27 @@ retry: } read_buf_col += n; break; - } else { - // Append whole line and new-line. Change NL - // to NUL to reverse the effect done below. - for (ni = 0; ni < n; ni++) { - if (p[ni] == NL) { - ptr[tlen++] = NUL; - } else { - ptr[tlen++] = p[ni]; - } + } + + // Append whole line and new-line. Change NL + // to NUL to reverse the effect done below. + for (ni = 0; ni < n; ni++) { + if (p[ni] == NL) { + ptr[tlen++] = NUL; + } else { + ptr[tlen++] = p[ni]; } - ptr[tlen++] = NL; - read_buf_col = 0; - if (++read_buf_lnum > from) { - // When the last line didn't have an - // end-of-line don't add it now either. - if (!curbuf->b_p_eol) { - --tlen; - } - size = tlen; - break; + } + ptr[tlen++] = NL; + read_buf_col = 0; + if (++read_buf_lnum > from) { + // When the last line didn't have an + // end-of-line don't add it now either. + if (!curbuf->b_p_eol) { + tlen--; } + size = tlen; + break; } } } -- cgit From 933274c438107adadde5ff854340ed1fae18d3fe Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Mon, 25 Apr 2022 03:51:22 +0200 Subject: fix/PVS #17863 * fix(PVS/V002): disable rule completely V002: "Some diagnostic messages may contain incorrect line number in this file." This particular check seems unreliable. It says on their website https://pvs-studio.com/en/docs/warnings/v002/ that this warning occurs when there are multiline pragmas, but there are none in extmark.c. * fix(PVS/V756): ignore "counter is not used inside a nested loop" warning The nested loop starts with "AutoCmd *ac = ap->cmds" so "ap" is definitely used. * fix(PVS/V560): disable "a part of conditional expression is always true" * fix(PVS/V614): potentially uninitialized variable 'blen' used --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index d4407b4982..be66a6ce80 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1118,7 +1118,7 @@ retry: && tmpname == NULL && (*fenc == 'u' || *fenc == NUL)))) { char_u *ccname; - int blen; + int blen = 0; // no BOM detection in a short file or in binary mode if (size < 2 || curbuf->b_p_bin) { -- cgit From 0648100fed65cbe8efe774ae997ab841cae01872 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Mon, 25 Apr 2022 04:18:43 +0200 Subject: refactor: convert macros to all-caps (#17895) Closes https://github.com/neovim/neovim/issues/6297 --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index be66a6ce80..282e25e10e 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2321,8 +2321,8 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ fname = sfname; #endif - if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) { - overwriting = TRUE; + if (buf->b_ffname != NULL && FNAMECMP(ffname, buf->b_ffname) == 0) { + overwriting = true; } else { overwriting = FALSE; } @@ -4647,7 +4647,7 @@ int vim_rename(const char_u *from, const char_u *to) * to the same file (ignoring case and slash/backslash differences) but * the file name differs we need to go through a temp file. */ - if (fnamecmp(from, to) == 0) { + if (FNAMECMP(from, to) == 0) { if (p_fic && (STRCMP(path_tail((char_u *)from), path_tail((char_u *)to)) != 0)) { use_tmp_file = true; -- cgit From 2dddc86a42a77bd099a031165e57add84b9b0e82 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Mon, 25 Apr 2022 08:44:18 -0600 Subject: fix: show autocmd output when F is in shortmess (#18251) The default value of including F in 'shortmess' has the unfortunate side effect of hiding output from autocommands. This is a common source of confusion and often leads people to think their autocommands are not working when they are. There is a small snippet in the docs for 'shortmess' indicating that the F flag suppresses autocmd output, but it's not easy to find if you don't already know to look for it. This commit removes that behavior of the F flag to make it only suppress file info when opening a new file. --- src/nvim/fileio.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 282e25e10e..25c99af41b 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -176,7 +176,7 @@ void filemess(buf_T *buf, char_u *name, char_u *s, int attr) /// /// @return FAIL for failure, NOTDONE for directory (failure), or OK int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_skip, - linenr_T lines_to_read, exarg_T *eap, int flags) + linenr_T lines_to_read, exarg_T *eap, int flags, bool silent) { int fd = 0; int newfile = (flags & READ_NEW); @@ -472,10 +472,12 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski return FAIL; } } - if (dir_of_file_exists(fname)) { - filemess(curbuf, sfname, (char_u *)new_file_message(), 0); - } else { - filemess(curbuf, sfname, (char_u *)_("[New DIRECTORY]"), 0); + if (!silent) { + if (dir_of_file_exists(fname)) { + filemess(curbuf, sfname, (char_u *)new_file_message(), 0); + } else { + filemess(curbuf, sfname, (char_u *)_("[New DIRECTORY]"), 0); + } } // Even though this is a new file, it might have been // edited before and deleted. Get the old marks. @@ -658,7 +660,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // Autocommands may add lines to the file, need to check if it is empty wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); - if (!recoverymode && !filtering && !(flags & READ_DUMMY)) { + if (!recoverymode && !filtering && !(flags & READ_DUMMY) && !silent) { if (!read_stdin && !read_buffer) { filemess(curbuf, sfname, (char_u *)"", 0); } @@ -1788,7 +1790,7 @@ failed: return OK; // an interrupt isn't really an error } - if (!filtering && !(flags & READ_DUMMY)) { + if (!filtering && !(flags & READ_DUMMY) && !silent) { add_quoted_fname((char *)IObuff, IOSIZE, curbuf, (const char *)sfname); c = false; @@ -5191,7 +5193,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) curbuf->b_flags |= BF_CHECK_RO; // check for RO again keep_filetype = true; // don't detect 'filetype' if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0, - (linenr_T)MAXLNUM, &ea, flags) != OK) { + (linenr_T)MAXLNUM, &ea, flags, false) != OK) { if (!aborting()) { semsg(_("E321: Could not reload \"%s\""), buf->b_fname); } -- cgit From 188537efb32d02081c1821cb5b48fbcf59230732 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 28 Apr 2022 15:27:34 -0600 Subject: fix: suppress "is a directory" messages with shortmess 'F' (#18296) When 'F' is in 'shortmess', don't show messages when editing a directory. This fixes a regression introduced by 0956283. --- src/nvim/fileio.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 25c99af41b..c5bb9442bc 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -359,7 +359,9 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // because reading the file may actually work, but then creating the // swap file may destroy it! Reported on MS-DOS and Win 95. if (after_pathsep((const char *)fname, (const char *)(fname + namelen))) { - filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); + if (!silent) { + filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); + } msg_end(); msg_scroll = msg_save; return NOTDONE; @@ -379,7 +381,9 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski #endif ) { if (S_ISDIR(perm)) { - filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); + if (!silent) { + filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); + } } else { filemess(curbuf, fname, (char_u *)_("is not a file"), 0); } -- cgit From 995c1863685d7fa0cc2638b55efee55c4cb7ffc9 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Mon, 18 Apr 2022 12:54:25 +0200 Subject: refactor(uncrustify): disable formatting in problematic code sections --- src/nvim/fileio.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c5bb9442bc..c4eb1200ec 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3,8 +3,6 @@ // fileio.c: read from and write to a file -// uncrustify:off - #include #include #include @@ -4912,11 +4910,15 @@ int buf_check_timestamp(buf_T *buf) char *mesg = NULL; char *mesg2 = ""; bool helpmesg = false; + + // uncrustify:off enum { RELOAD_NONE, RELOAD_NORMAL, RELOAD_DETECT } reload = RELOAD_NONE; + // uncrustify:on + bool can_reload = false; uint64_t orig_size = buf->b_orig_size; int orig_mode = buf->b_orig_mode; -- cgit From 0b3ae64480ea28bb57783c2269a61f0a60ffc55e Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Fri, 29 Apr 2022 13:52:43 +0200 Subject: refactor(uncrustify): format all c code under /src/nvim/ --- src/nvim/fileio.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c4eb1200ec..d8040219ca 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -19,8 +19,8 @@ #include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" -#include "nvim/eval/userfunc.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" @@ -5070,12 +5070,12 @@ int buf_check_timestamp(buf_T *buf) switch (do_dialog(VIM_WARNING, (char_u *)_("Warning"), (char_u *)tbuf, (char_u *)_("&OK\n&Load File\nLoad File &and Options"), 1, NULL, true)) { - case 2: - reload = RELOAD_NORMAL; - break; - case 3: - reload = RELOAD_DETECT; - break; + case 2: + reload = RELOAD_NORMAL; + break; + case 3: + reload = RELOAD_DETECT; + break; } } else if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) { if (*mesg2 != NUL) { -- cgit From eef8de4df0247157e57f306062b1b86e01a41454 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Fri, 29 Apr 2022 13:53:42 +0200 Subject: refactor(uncrustify): change rules to better align with the style guide Add space around arithmetic operators '+' and '-'. Remove space between back-to-back parentheses, i.e. ')(' vs. ') ('. Remove space between '((' or '))' of control statements. Add space between ')' and '{' of control statements. Remove space between function name and '(' on function declaration. Collapse empty blocks between '{' and '}'. Remove newline at the end of the file. Remove newline between 'enum' and '{'. Remove newline between '}' and ')' in a function invocation. Remove newline between '}' and 'while' of 'do' statement. --- src/nvim/fileio.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index d8040219ca..f1195f1644 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1528,8 +1528,7 @@ rewind_retry: // Need to reset the counters when retrying fenc. try_mac = 1; try_unix = 1; - for (; p >= ptr && *p != CAR; p--) { - } + for (; p >= ptr && *p != CAR; p--) {} if (p >= ptr) { for (p = ptr; p < ptr + size; ++p) { if (*p == NL) { -- cgit From 3c23100130725bb79c04e933c505bbeda96fb3bb Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Sat, 30 Apr 2022 16:48:00 +0200 Subject: refactor: replace char_u variables and functions with char (#18288) Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f1195f1644..7ca7abccdc 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -690,7 +690,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski * Decide which 'encoding' to use or use first. */ if (eap != NULL && eap->force_enc != 0) { - fenc = enc_canonize(eap->cmd + eap->force_enc); + fenc = enc_canonize((char_u *)eap->cmd + eap->force_enc); fenc_alloced = true; keep_dest_enc = true; } else if (curbuf->b_p_bin) { @@ -2027,7 +2027,7 @@ void prep_exarg(exarg_T *eap, const buf_T *buf) const size_t cmd_len = 15 + STRLEN(buf->b_p_fenc); eap->cmd = xmalloc(cmd_len); - snprintf((char *)eap->cmd, cmd_len, "e ++enc=%s", buf->b_p_fenc); + snprintf(eap->cmd, cmd_len, "e ++enc=%s", buf->b_p_fenc); eap->force_enc = 8; eap->bad_char = buf->b_bad_char; eap->force_ff = *buf->b_p_ff; @@ -2062,7 +2062,7 @@ void set_file_options(int set_options, exarg_T *eap) void set_forced_fenc(exarg_T *eap) { if (eap->force_enc != 0) { - char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); + char_u *fenc = enc_canonize((char_u *)eap->cmd + eap->force_enc); set_string_option_direct("fenc", -1, fenc, OPT_FREE|OPT_LOCAL, 0); xfree(fenc); } @@ -3066,7 +3066,7 @@ nobackup: // Check for forced 'fileencoding' from "++opt=val" argument. if (eap != NULL && eap->force_enc != 0) { - fenc = eap->cmd + eap->force_enc; + fenc = (char_u *)eap->cmd + eap->force_enc; fenc = enc_canonize(fenc); fenc_tofree = fenc; } else { -- cgit From ad63b94b03c166f37bda477db6cbac2a9583d586 Mon Sep 17 00:00:00 2001 From: bfredl Date: Fri, 22 Apr 2022 20:56:31 +0200 Subject: refactor(ui): simplify stdin handling --- src/nvim/fileio.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 7ca7abccdc..95c373ec5c 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -176,7 +176,7 @@ void filemess(buf_T *buf, char_u *name, char_u *s, int attr) int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_skip, linenr_T lines_to_read, exarg_T *eap, int flags, bool silent) { - int fd = 0; + int fd = stdin_fd >= 0 ? stdin_fd : 0; int newfile = (flags & READ_NEW); int check_readonly; int filtering = (flags & READ_FILTER); @@ -1722,17 +1722,19 @@ failed: xfree(buffer); if (read_stdin) { - close(0); + close(fd); + if (stdin_fd < 0) { #ifndef WIN32 - // On Unix, use stderr for stdin, makes shell commands work. - vim_ignored = dup(2); + // On Unix, use stderr for stdin, makes shell commands work. + vim_ignored = dup(2); #else - // On Windows, use the console input handle for stdin. - HANDLE conin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, - OPEN_EXISTING, 0, (HANDLE)NULL); - vim_ignored = _open_osfhandle(conin, _O_RDONLY); + // On Windows, use the console input handle for stdin. + HANDLE conin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, + OPEN_EXISTING, 0, (HANDLE)NULL); + vim_ignored = _open_osfhandle(conin, _O_RDONLY); #endif + } } if (tmpname != NULL) { -- cgit From f08477789fe241c3868d3856643c78da0760cd19 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Wed, 4 May 2022 13:00:18 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 95c373ec5c..1c91df6387 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3797,7 +3797,7 @@ static int set_rw_fname(char_u *fname, char_u *sfname) // Do filetype detection now if 'filetype' is empty. if (*curbuf->b_p_ft == NUL) { if (augroup_exists("filetypedetect")) { - (void)do_doautocmd((char_u *)"filetypedetect BufRead", false, NULL); + (void)do_doautocmd("filetypedetect BufRead", false, NULL); } do_modelines(0); } -- cgit From 9a671e6a24243a5ff2879599d91ab5aec8b4e77d Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Wed, 4 May 2022 18:27:22 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 1c91df6387..24052d4951 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1584,7 +1584,7 @@ rewind_retry: if (skip_count == 0) { *ptr = NUL; // end of line len = (colnr_T)(ptr - line_start + 1); - if (ml_append(lnum, line_start, len, newfile) == FAIL) { + if (ml_append(lnum, (char *)line_start, len, newfile) == FAIL) { error = true; break; } @@ -1640,7 +1640,7 @@ rewind_retry: ff_error = EOL_DOS; } } - if (ml_append(lnum, line_start, len, newfile) == FAIL) { + if (ml_append(lnum, (char *)line_start, len, newfile) == FAIL) { error = true; break; } @@ -1688,7 +1688,7 @@ failed: } *ptr = NUL; len = (colnr_T)(ptr - line_start + 1); - if (ml_append(lnum, line_start, len, newfile) == FAIL) { + if (ml_append(lnum, (char *)line_start, len, newfile) == FAIL) { error = true; } else { if (read_undo_file) { @@ -4872,7 +4872,7 @@ static int move_lines(buf_T *frombuf, buf_T *tobuf) curbuf = tobuf; for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; lnum++) { p = vim_strsave(ml_get_buf(frombuf, lnum, false)); - if (ml_append(lnum - 1, p, 0, false) == FAIL) { + if (ml_append(lnum - 1, (char *)p, 0, false) == FAIL) { xfree(p); retval = FAIL; break; -- cgit From 2a378e6e82cececb12339f2df51ffe107039d867 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Wed, 4 May 2022 22:35:50 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 24052d4951..ad0440e0b3 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1377,7 +1377,7 @@ retry: } else { len = utf_head_off(ptr, p); p -= len; - u8c = utf_ptr2char(p); + u8c = utf_ptr2char((char *)p); if (len == 0) { // Not a valid UTF-8 character, retry with // another fenc when possible, otherwise just @@ -3982,7 +3982,7 @@ static int buf_write_bytes(struct bw_info *ip) break; } if (n > 1) { - c = utf_ptr2char(ip->bw_rest); + c = utf_ptr2char((char *)ip->bw_rest); } else { c = ip->bw_rest[0]; } @@ -4010,7 +4010,7 @@ static int buf_write_bytes(struct bw_info *ip) break; } if (n > 1) { - c = utf_ptr2char(buf + wlen); + c = utf_ptr2char((char *)buf + wlen); } else { c = buf[wlen]; } -- cgit From e31b32a293f6ba8708499a176234f8be1df6a145 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Thu, 5 May 2022 13:36:14 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index ad0440e0b3..a132c2db43 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1401,7 +1401,7 @@ retry: assert(u8c <= INT_MAX); // produce UTF-8 dest -= utf_char2len((int)u8c); - (void)utf_char2bytes((int)u8c, dest); + (void)utf_char2bytes((int)u8c, (char *)dest); } // move the linerest to before the converted characters @@ -3943,8 +3943,8 @@ static int buf_write_bytes(struct bw_info *ip) * Convert latin1 in the buffer to UTF-8 in the file. */ p = ip->bw_conv_buf; // translate to buffer - for (wlen = 0; wlen < len; ++wlen) { - p += utf_char2bytes(buf[wlen], p); + for (wlen = 0; wlen < len; wlen++) { + p += utf_char2bytes(buf[wlen], (char *)p); } buf = ip->bw_conv_buf; len = (int)(p - ip->bw_conv_buf); -- cgit From 9aa5647e686e5420e5b9b51828ec7d55631f98ed Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 10 May 2022 07:58:58 +0800 Subject: vim-patch:8.2.4911: the mode #defines are not clearly named (#18499) Problem: The mode #defines are not clearly named. Solution: Prepend MODE_. Renumber them to put the mapped modes first. https://github.com/vim/vim/commit/249591057b4840785c50e41dd850efb8a8faf435 A hunk from the patch depends on patch 8.2.4861, which hasn't been ported yet, but that should be easy to notice. --- src/nvim/fileio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index a132c2db43..c802920fa9 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5078,7 +5078,7 @@ int buf_check_timestamp(buf_T *buf) reload = RELOAD_DETECT; break; } - } else if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) { + } else if (State > MODE_NORMAL_BUSY || (State & MODE_CMDLINE) || already_warned) { if (*mesg2 != NUL) { xstrlcat(tbuf, "; ", tbuf_len - 1); xstrlcat(tbuf, mesg2, tbuf_len - 1); -- cgit From 85aae12a6dea48621ea2d24a946b3e7b86f9014d Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Sun, 8 May 2022 14:43:16 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 474 +++++++++++++++++++++++++++--------------------------- 1 file changed, 234 insertions(+), 240 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c802920fa9..c412af0eaa 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -173,7 +173,7 @@ void filemess(buf_T *buf, char_u *name, char_u *s, int attr) /// @param eap can be NULL! /// /// @return FAIL for failure, NOTDONE for directory (failure), or OK -int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_skip, +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) { int fd = stdin_fd >= 0 ? stdin_fd : 0; @@ -189,10 +189,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski colnr_T read_buf_col = 0; // next char to read from this line char_u c; linenr_T lnum = from; - char_u *ptr = NULL; // pointer into read buffer - char_u *buffer = NULL; // read buffer - char_u *new_buffer = NULL; // init to shut up gcc - char_u *line_start = NULL; // init to shut up gcc + char *ptr = NULL; // pointer into read buffer + char *buffer = NULL; // read buffer + char *new_buffer = NULL; // init to shut up gcc + char *line_start = NULL; // init to shut up gcc int wasempty; // buffer was empty before reading colnr_T len; long size = 0; @@ -227,11 +227,11 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski int bad_char_behavior = BAD_REPLACE; // BAD_KEEP, BAD_DROP or character to // replace with - char_u *tmpname = NULL; // name of 'charconvert' output file + char *tmpname = NULL; // name of 'charconvert' output file int fio_flags = 0; - char_u *fenc; // fileencoding to use + char *fenc; // fileencoding to use bool fenc_alloced; // fenc_next is in allocated memory - char_u *fenc_next = NULL; // next item in 'fencs' or NULL + char *fenc_next = NULL; // next item in 'fencs' or NULL bool advance_fenc = false; long real_size = 0; #ifdef HAVE_ICONV @@ -241,12 +241,12 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski #endif bool converted = false; // true if conversion done bool notconverted = false; // true if conversion wanted but it wasn't possible - char_u conv_rest[CONV_RESTLEN]; + char conv_rest[CONV_RESTLEN]; int conv_restlen = 0; // nr of bytes in conv_rest[] pos_T orig_start; buf_T *old_curbuf; - char_u *old_b_ffname; - char_u *old_b_fname; + char *old_b_ffname; + char *old_b_fname; int using_b_ffname; int using_b_fname; static char *msg_is_a_directory = N_("is a directory"); @@ -266,7 +266,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski && fname != NULL && vim_strchr(p_cpo, CPO_FNAMER) != NULL && !(flags & READ_DUMMY)) { - if (set_rw_fname(fname, sfname) == FAIL) { + if (set_rw_fname((char_u *)fname, (char_u *)sfname) == FAIL) { return FAIL; } } @@ -276,11 +276,11 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // executing nasty autocommands. Also check if "fname" and "sfname" // point to one of these values. old_curbuf = curbuf; - old_b_ffname = curbuf->b_ffname; - old_b_fname = curbuf->b_fname; - using_b_ffname = (fname == curbuf->b_ffname) - || (sfname == curbuf->b_ffname); - using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); + old_b_ffname = (char *)curbuf->b_ffname; + old_b_fname = (char *)curbuf->b_fname; + using_b_ffname = ((char_u *)fname == curbuf->b_ffname) + || ((char_u *)sfname == curbuf->b_ffname); + using_b_fname = ((char_u *)fname == curbuf->b_fname) || ((char_u *)sfname == curbuf->b_fname); // After reading a file the cursor line changes but we don't want to // display the line. @@ -347,7 +347,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // If the name is too long we might crash further on, quit here. if (namelen >= MAXPATHL) { - filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); + filemess(curbuf, (char_u *)fname, (char_u *)_("Illegal file name"), 0); msg_end(); msg_scroll = msg_save; return FAIL; @@ -356,9 +356,9 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // If the name ends in a path separator, we can't open it. Check here, // because reading the file may actually work, but then creating the // swap file may destroy it! Reported on MS-DOS and Win 95. - if (after_pathsep((const char *)fname, (const char *)(fname + namelen))) { + if (after_pathsep(fname, fname + namelen)) { if (!silent) { - filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); + filemess(curbuf, (char_u *)fname, (char_u *)_(msg_is_a_directory), 0); } msg_end(); msg_scroll = msg_save; @@ -367,23 +367,23 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski } if (!read_buffer && !read_stdin && !read_fifo) { - perm = os_getperm((const char *)fname); + perm = os_getperm(fname); // On Unix it is possible to read a directory, so we have to // check for it before os_open(). if (perm >= 0 && !S_ISREG(perm) // not a regular file ... && !S_ISFIFO(perm) // ... or fifo && !S_ISSOCK(perm) // ... or socket #ifdef OPEN_CHR_FILES - && !(S_ISCHR(perm) && is_dev_fd_file(fname)) + && !(S_ISCHR(perm) && is_dev_fd_file((char_u *)fname)) // ... or a character special file named /dev/fd/ #endif ) { if (S_ISDIR(perm)) { if (!silent) { - filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0); + filemess(curbuf, (char_u *)fname, (char_u *)_(msg_is_a_directory), 0); } } else { - filemess(curbuf, fname, (char_u *)_("is not a file"), 0); + filemess(curbuf, (char_u *)fname, (char_u *)_("is not a file"), 0); } msg_end(); msg_scroll = msg_save; @@ -407,7 +407,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski if (newfile && !read_stdin && !read_buffer && !read_fifo) { // Remember time of file. - if (os_fileinfo((char *)fname, &file_info)) { + if (os_fileinfo(fname, &file_info)) { buf_store_file_info(curbuf, &file_info); curbuf->b_mtime_read = curbuf->b_mtime; curbuf->b_mtime_read_ns = curbuf->b_mtime_ns; @@ -443,10 +443,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski bool file_readonly = false; if (!read_buffer && !read_stdin) { if (!newfile || readonlymode || !(perm & 0222) - || !os_file_is_writable((char *)fname)) { + || !os_file_is_writable(fname)) { file_readonly = true; } - fd = os_open((char *)fname, O_RDONLY, 0); + fd = os_open(fname, O_RDONLY, 0); } if (fd < 0) { // cannot open at all @@ -467,18 +467,18 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // SwapExists autocommand may mess things up if (curbuf != old_curbuf || (using_b_ffname - && (old_b_ffname != curbuf->b_ffname)) + && ((char_u *)old_b_ffname != curbuf->b_ffname)) || (using_b_fname - && (old_b_fname != curbuf->b_fname))) { + && ((char_u *)old_b_fname != curbuf->b_fname))) { emsg(_(e_auchangedbuf)); return FAIL; } } if (!silent) { - if (dir_of_file_exists(fname)) { - filemess(curbuf, sfname, (char_u *)new_file_message(), 0); + if (dir_of_file_exists((char_u *)fname)) { + filemess(curbuf, (char_u *)sfname, (char_u *)new_file_message(), 0); } else { - filemess(curbuf, sfname, (char_u *)_("[New DIRECTORY]"), 0); + filemess(curbuf, (char_u *)sfname, (char_u *)_("[New DIRECTORY]"), 0); } } // Even though this is a new file, it might have been @@ -498,15 +498,16 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski } return OK; // a new file is not an error } else { - filemess(curbuf, sfname, (char_u *)( - (fd == UV_EFBIG) ? _("[File too big]") : + filemess(curbuf, (char_u *)sfname, (char_u *)((fd == UV_EFBIG) ? _("[File too big]") : #if defined(UNIX) && defined(EOVERFLOW) - // libuv only returns -errno in Unix and in Windows open() does not - // set EOVERFLOW - (fd == -EOVERFLOW) ? _("[File too big]") : + // libuv only returns -errno + // in Unix and in Windows + // open() does not set + // EOVERFLOW + (fd == -EOVERFLOW) ? _("[File too big]") : #endif - _("[Permission Denied]")), 0); - curbuf->b_p_ro = TRUE; // must use "w!" now + _("[Permission Denied]")), 0); + curbuf->b_p_ro = true; // must use "w!" now } return FAIL; @@ -538,8 +539,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski check_need_swap(newfile); if (!read_stdin && (curbuf != old_curbuf - || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) - || (using_b_fname && (old_b_fname != curbuf->b_fname)))) { + || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) + || (using_b_fname && ((char_u *)old_b_fname != curbuf->b_fname)))) { emsg(_(e_auchangedbuf)); if (!read_buffer) { close(fd); @@ -644,10 +645,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski * (cd for example) if it invalidates fname or sfname. */ if (!read_stdin && (curbuf != old_curbuf - || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) - || (using_b_fname && (old_b_fname != curbuf->b_fname)) - || (fd = os_open((char *)fname, O_RDONLY, 0)) < 0)) { - --no_wait_return; + || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) + || (using_b_fname && ((char_u *)old_b_fname != curbuf->b_fname)) + || (fd = os_open(fname, O_RDONLY, 0)) < 0)) { + no_wait_return--; msg_scroll = msg_save; if (fd < 0) { emsg(_("E200: *ReadPre autocommands made the file unreadable")); @@ -664,7 +665,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski if (!recoverymode && !filtering && !(flags & READ_DUMMY) && !silent) { if (!read_stdin && !read_buffer) { - filemess(curbuf, sfname, (char_u *)"", 0); + filemess(curbuf, (char_u *)sfname, (char_u *)"", 0); } } @@ -690,27 +691,27 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski * Decide which 'encoding' to use or use first. */ if (eap != NULL && eap->force_enc != 0) { - fenc = enc_canonize((char_u *)eap->cmd + eap->force_enc); + fenc = (char *)enc_canonize((char_u *)eap->cmd + eap->force_enc); fenc_alloced = true; keep_dest_enc = true; } else if (curbuf->b_p_bin) { - fenc = (char_u *)""; // binary: don't convert + fenc = ""; // binary: don't convert fenc_alloced = false; } else if (curbuf->b_help) { // Help files are either utf-8 or latin1. Try utf-8 first, if this // fails it must be latin1. // It is needed when the first line contains non-ASCII characters. // That is only in *.??x files. - fenc_next = (char_u *)"latin1"; - fenc = (char_u *)"utf-8"; + fenc_next = "latin1"; + fenc = "utf-8"; fenc_alloced = false; } else if (*p_fencs == NUL) { - fenc = curbuf->b_p_fenc; // use format from buffer + fenc = (char *)curbuf->b_p_fenc; // use format from buffer fenc_alloced = false; } else { - fenc_next = p_fencs; // try items in 'fileencodings' - fenc = next_fenc(&fenc_next, &fenc_alloced); + fenc_next = (char *)p_fencs; // try items in 'fileencodings' + fenc = (char *)next_fenc((char_u **)&fenc_next, &fenc_alloced); } /* @@ -797,21 +798,21 @@ retry: if (fenc_alloced) { xfree(fenc); } - fenc = (char_u *)""; + fenc = ""; fenc_alloced = false; } else { if (fenc_alloced) { xfree(fenc); } if (fenc_next != NULL) { - fenc = next_fenc(&fenc_next, &fenc_alloced); + fenc = (char *)next_fenc((char_u **)&fenc_next, &fenc_alloced); } else { - fenc = (char_u *)""; + fenc = ""; fenc_alloced = false; } } if (tmpname != NULL) { - os_remove((char *)tmpname); // delete converted file + os_remove(tmpname); // delete converted file XFREE_CLEAR(tmpname); } } @@ -821,7 +822,7 @@ retry: * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. */ fio_flags = 0; - converted = need_conversion(fenc); + converted = need_conversion((char_u *)fenc); if (converted) { // "ucs-bom" means we need to check the first bytes of the file // for a BOM. @@ -835,7 +836,7 @@ retry: // appears not to handle this correctly. This works just like // conversion to UTF-8 except how the resulting character is put in // the buffer. - fio_flags = get_fio_flags(fenc); + fio_flags = get_fio_flags((char_u *)fenc); } @@ -843,7 +844,7 @@ retry: // Try using iconv() if we can't convert internally. if (fio_flags == 0 && !did_iconv) { - iconv_fd = (iconv_t)my_iconv_open((char_u *)"utf-8", fenc); + iconv_fd = (iconv_t)my_iconv_open((char_u *)"utf-8", (char_u *)fenc); } #endif @@ -863,7 +864,7 @@ retry: // Skip conversion when it's already done (retry for wrong // "fileformat"). if (tmpname == NULL) { - tmpname = readfile_charconvert(fname, fenc, &fd); + tmpname = (char *)readfile_charconvert((char_u *)fname, (char_u *)fenc, &fd); if (tmpname == NULL) { // Conversion failed. Try another one. advance_fenc = true; @@ -1128,8 +1129,8 @@ retry: if (size < 2 || curbuf->b_p_bin) { ccname = NULL; } else { - ccname = check_for_bom(ptr, size, &blen, - fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); + ccname = check_for_bom((char_u *)ptr, size, &blen, + fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags((char_u *)fenc)); } if (ccname != NULL) { // Remove BOM from the text @@ -1151,7 +1152,7 @@ retry: if (fenc_alloced) { xfree(fenc); } - fenc = ccname; + fenc = (char *)ccname; fenc_alloced = false; } // retry reading without getting new bytes or rewinding @@ -1182,10 +1183,10 @@ retry: size_t from_size; size_t to_size; - fromp = (char *)ptr; + fromp = ptr; from_size = size; ptr += size; - top = (char *)ptr; + top = ptr; to_size = real_size - size; /* @@ -1201,8 +1202,7 @@ retry: goto rewind_retry; } if (conv_error == 0) { - conv_error = readfile_linenr(linecnt, - ptr, (char_u *)top); + conv_error = readfile_linenr(linecnt, (char_u *)ptr, (char_u *)top); } // Deal with a bad byte and continue with the next. @@ -1227,14 +1227,14 @@ retry: // move the linerest to before the converted characters line_start = ptr - linerest; memmove(line_start, buffer, (size_t)linerest); - size = ((char_u *)top - ptr); + size = (top - ptr); } #endif if (fio_flags != 0) { unsigned int u8c; - char_u *dest; - char_u *tail = NULL; + char *dest; + char *tail = NULL; // Convert Unicode or Latin1 to UTF-8. // Go from end to start through the buffer, because the number @@ -1243,7 +1243,7 @@ retry: // to after the next character to convert. dest = ptr + real_size; if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) { - p = ptr + size; + p = (uint8_t *)ptr + size; if (fio_flags == FIO_UTF8) { // Check for a trailing incomplete UTF-8 sequence tail = ptr + size - 1; @@ -1253,16 +1253,16 @@ retry: if (tail + utf_byte2len(*tail) <= ptr + size) { tail = NULL; } else { - p = tail; + p = (uint8_t *)tail; } } } else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) { // Check for a trailing byte - p = ptr + (size & ~1); + p = (uint8_t *)ptr + (size & ~1); if (size & 1) { - tail = p; + tail = (char *)p; } - if ((fio_flags & FIO_UTF16) && p > ptr) { + if ((fio_flags & FIO_UTF16) && p > (uint8_t *)ptr) { // Check for a trailing leading word if (fio_flags & FIO_ENDIAN_L) { u8c = (*--p << 8); @@ -1272,16 +1272,16 @@ retry: u8c += (*--p << 8); } if (u8c >= 0xd800 && u8c <= 0xdbff) { - tail = p; + tail = (char *)p; } else { p += 2; } } } else { // FIO_UCS4 // Check for trailing 1, 2 or 3 bytes - p = ptr + (size & ~3); + p = (uint8_t *)ptr + (size & ~3); if (size & 3) { - tail = p; + tail = (char *)p; } } @@ -1294,7 +1294,7 @@ retry: } - while (p > ptr) { + while (p > (uint8_t *)ptr) { if (fio_flags & FIO_LATIN1) { u8c = *--p; } else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) { @@ -1309,14 +1309,13 @@ retry: && u8c >= 0xdc00 && u8c <= 0xdfff) { int u16c; - if (p == ptr) { + if (p == (uint8_t *)ptr) { // Missing leading word. if (can_retry) { goto rewind_retry; } if (conv_error == 0) { - conv_error = readfile_linenr(linecnt, - ptr, p); + conv_error = readfile_linenr(linecnt, (char_u *)ptr, p); } if (bad_char_behavior == BAD_DROP) { continue; @@ -1344,8 +1343,7 @@ retry: goto rewind_retry; } if (conv_error == 0) { - conv_error = readfile_linenr(linecnt, - ptr, p); + conv_error = readfile_linenr(linecnt, (char_u *)ptr, p); } if (bad_char_behavior == BAD_DROP) { continue; @@ -1375,7 +1373,7 @@ retry: if (*--p < 0x80) { u8c = *p; } else { - len = utf_head_off(ptr, p); + len = utf_head_off((char_u *)ptr, p); p -= len; u8c = utf_ptr2char((char *)p); if (len == 0) { @@ -1386,8 +1384,7 @@ retry: goto rewind_retry; } if (conv_error == 0) { - conv_error = readfile_linenr(linecnt, - ptr, p); + conv_error = readfile_linenr(linecnt, (char_u *)ptr, p); } if (bad_char_behavior == BAD_DROP) { continue; @@ -1401,7 +1398,7 @@ retry: assert(u8c <= INT_MAX); // produce UTF-8 dest -= utf_char2len((int)u8c); - (void)utf_char2bytes((int)u8c, (char *)dest); + (void)utf_char2bytes((int)u8c, dest); } // move the linerest to before the converted characters @@ -1413,8 +1410,8 @@ retry: bool incomplete_tail = false; // Reading UTF-8: Check if the bytes are valid UTF-8. - for (p = ptr;; p++) { - int todo = (int)((ptr + size) - p); + for (p = (uint8_t *)ptr;; p++) { + int todo = (int)(((uint8_t *)ptr + size) - p); int l; if (todo <= 0) { @@ -1431,13 +1428,13 @@ retry: // a truncated file is more likely, or attempting // to read the rest of an incomplete sequence when // we have already done so. - if (p > ptr || filesize > 0) { + if (p > (uint8_t *)ptr || filesize > 0) { incomplete_tail = true; } // Incomplete byte sequence, move it to conv_rest[] // and try to read the rest of it, unless we've // already done so. - if (p > ptr) { + if (p > (uint8_t *)ptr) { conv_restlen = todo; memmove(conv_rest, p, conv_restlen); size -= conv_restlen; @@ -1454,12 +1451,12 @@ retry: #ifdef HAVE_ICONV // When we did a conversion report an error. if (iconv_fd != (iconv_t)-1 && conv_error == 0) { - conv_error = readfile_linenr(linecnt, ptr, p); + conv_error = readfile_linenr(linecnt, (char_u *)ptr, p); } #endif // Remember the first linenr with an illegal byte if (conv_error == 0 && illegal_byte == 0) { - illegal_byte = readfile_linenr(linecnt, ptr, p); + illegal_byte = readfile_linenr(linecnt, (char_u *)ptr, p); } // Drop, keep or replace the bad byte. @@ -1475,7 +1472,7 @@ retry: } } } - if (p < ptr + size && !incomplete_tail) { + if (p < (uint8_t *)ptr + size && !incomplete_tail) { // Detected a UTF-8 error. rewind_retry: // Retry reading with another conversion. @@ -1509,10 +1506,10 @@ rewind_retry: try_mac = 1; } - for (p = ptr; p < ptr + size; ++p) { + for (p = (uint8_t *)ptr; p < (uint8_t *)ptr + size; p++) { if (*p == NL) { if (!try_unix - || (try_dos && p > ptr && p[-1] == CAR)) { + || (try_dos && p > (uint8_t *)ptr && p[-1] == CAR)) { fileformat = EOL_DOS; } else { fileformat = EOL_UNIX; @@ -1528,9 +1525,9 @@ rewind_retry: // Need to reset the counters when retrying fenc. try_mac = 1; try_unix = 1; - for (; p >= ptr && *p != CAR; p--) {} - if (p >= ptr) { - for (p = ptr; p < ptr + size; ++p) { + for (; p >= (uint8_t *)ptr && *p != CAR; p--) {} + if (p >= (uint8_t *)ptr) { + for (p = (uint8_t *)ptr; p < (uint8_t *)ptr + size; p++) { if (*p == NL) { try_unix++; } else if (*p == CAR) { @@ -1584,12 +1581,12 @@ rewind_retry: if (skip_count == 0) { *ptr = NUL; // end of line len = (colnr_T)(ptr - line_start + 1); - if (ml_append(lnum, (char *)line_start, len, newfile) == FAIL) { + if (ml_append(lnum, line_start, len, newfile) == FAIL) { error = true; break; } if (read_undo_file) { - sha256_update(&sha_ctx, line_start, len); + sha256_update(&sha_ctx, (char_u *)line_start, len); } ++lnum; if (--read_count == 0) { @@ -1640,12 +1637,12 @@ rewind_retry: ff_error = EOL_DOS; } } - if (ml_append(lnum, (char *)line_start, len, newfile) == FAIL) { + if (ml_append(lnum, line_start, len, newfile) == FAIL) { error = true; break; } if (read_undo_file) { - sha256_update(&sha_ctx, line_start, len); + sha256_update(&sha_ctx, (char_u *)line_start, len); } ++lnum; if (--read_count == 0) { @@ -1688,11 +1685,11 @@ failed: } *ptr = NUL; len = (colnr_T)(ptr - line_start + 1); - if (ml_append(lnum, (char *)line_start, len, newfile) == FAIL) { + if (ml_append(lnum, line_start, len, newfile) == FAIL) { error = true; } else { if (read_undo_file) { - sha256_update(&sha_ctx, line_start, len); + sha256_update(&sha_ctx, (char_u *)line_start, len); } read_no_eol_lnum = ++lnum; } @@ -1703,7 +1700,7 @@ failed: save_file_ff(curbuf); // If editing a new file: set 'fenc' for the current buffer. // Also for ":read ++edit file". - set_string_option_direct("fenc", -1, fenc, OPT_FREE | OPT_LOCAL, 0); + set_string_option_direct("fenc", -1, (char_u *)fenc, OPT_FREE | OPT_LOCAL, 0); } if (fenc_alloced) { xfree(fenc); @@ -1738,7 +1735,7 @@ failed: } if (tmpname != NULL) { - os_remove((char *)tmpname); // delete converted file + os_remove(tmpname); // delete converted file xfree(tmpname); } --no_wait_return; // may wait for return now @@ -1783,7 +1780,7 @@ failed: if (got_int) { if (!(flags & READ_DUMMY)) { - filemess(curbuf, sfname, (char_u *)_(e_interr), 0); + filemess(curbuf, (char_u *)sfname, (char_u *)_(e_interr), 0); if (newfile) { curbuf->b_p_ro = TRUE; // must use "w!" now } @@ -1932,7 +1929,7 @@ failed: char_u hash[UNDO_HASH_SIZE]; sha256_finish(&sha_ctx, hash); - u_read_undo(NULL, hash, fname); + u_read_undo(NULL, hash, (char_u *)fname); } if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) { @@ -1993,7 +1990,7 @@ bool is_dev_fd_file(char_u *fname) { return STRNCMP(fname, "/dev/fd/", 8) == 0 && ascii_isdigit(fname[8]) - && *skipdigits(fname + 9) == NUL + && *skipdigits((char *)fname + 9) == NUL && (fname[9] != NUL || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')); } @@ -2188,18 +2185,18 @@ char *new_file_message(void) /// @param append append to the file /// /// @return FAIL for failure, OK otherwise -int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_T end, exarg_T *eap, +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) { int fd; - char_u *backup = NULL; - int backup_copy = FALSE; // copy the original file? + char *backup = NULL; + int backup_copy = false; // copy the original file? int dobackup; - char_u *ffname; - char_u *wfname = NULL; // name of file to write to - char_u *s; - char_u *ptr; - char_u c; + char *ffname; + char *wfname = NULL; // name of file to write to + char *s; + char *ptr; + char c; int len; linenr_T lnum; long nchars; @@ -2213,9 +2210,9 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ char *errmsg = NULL; int errmsgarg = 0; bool errmsg_allocated = false; - char_u *buffer; - char_u smallbuf[SMBUFSIZE]; - char_u *backup_ext; + char *buffer; + char smallbuf[SMBUFSIZE]; + char *backup_ext; int bufsize; long perm; // file permissions int retval = OK; @@ -2238,10 +2235,10 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ int fileformat; int write_bin; struct bw_info write_info; // info for buf_write_bytes() - int converted = FALSE; - int notconverted = FALSE; - char_u *fenc; // effective 'fileencoding' - char_u *fenc_tofree = NULL; // allocated "fenc" + int converted = false; + int notconverted = false; + char *fenc; // effective 'fileencoding' + char *fenc_tofree = NULL; // allocated "fenc" #ifdef HAS_BW_FLAGS int wb_flags = 0; #endif @@ -2307,7 +2304,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ && !filtering && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) { - if (set_rw_fname(fname, sfname) == FAIL) { + if (set_rw_fname((char_u *)fname, (char_u *)sfname) == FAIL) { return FAIL; } buf = curbuf; // just in case autocmds made "buf" invalid @@ -2358,17 +2355,17 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * Set curbuf to the buffer to be written. * Careful: The autocommands may call buf_write() recursively! */ - if (ffname == buf->b_ffname) { - buf_ffname = TRUE; + if ((char_u *)ffname == buf->b_ffname) { + buf_ffname = true; } - if (sfname == buf->b_sfname) { - buf_sfname = TRUE; + if ((char_u *)sfname == buf->b_sfname) { + buf_sfname = true; } - if (fname == buf->b_ffname) { - buf_fname_f = TRUE; + if ((char_u *)fname == buf->b_ffname) { + buf_fname_f = true; } - if (fname == buf->b_sfname) { - buf_fname_s = TRUE; + if ((char_u *)fname == buf->b_sfname) { + buf_fname_s = true; } // Set curwin/curbuf to buf and save a few things. @@ -2508,16 +2505,16 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * be kept in fname, ffname and sfname. */ if (buf_ffname) { - ffname = buf->b_ffname; + ffname = (char *)buf->b_ffname; } if (buf_sfname) { - sfname = buf->b_sfname; + sfname = (char *)buf->b_sfname; } if (buf_fname_f) { - fname = buf->b_ffname; + fname = (char *)buf->b_ffname; } if (buf_fname_s) { - fname = buf->b_sfname; + fname = (char *)buf->b_sfname; } } @@ -2535,9 +2532,9 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ if (!filtering) { filemess(buf, #ifndef UNIX - sfname, + (char_u *)sfname, #else - fname, + (char_u *)fname, #endif (char_u *)"", 0); // show that we are busy } @@ -2559,8 +2556,8 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ FileInfo file_info_old; #if defined(UNIX) perm = -1; - if (!os_fileinfo((char *)fname, &file_info_old)) { - newfile = TRUE; + if (!os_fileinfo(fname, &file_info_old)) { + newfile = true; } else { perm = file_info_old.stat.st_mode; if (!S_ISREG(file_info_old.stat.st_mode)) { // not a file @@ -2568,7 +2565,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ SET_ERRMSG_NUM("E502", _("is a directory")); goto fail; } - if (os_nodetype((char *)fname) != NODE_WRITABLE) { + if (os_nodetype(fname) != NODE_WRITABLE) { SET_ERRMSG_NUM("E503", _("is not a file or writable device")); goto fail; } @@ -2609,7 +2606,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * Check if the file is really writable (when renaming the file to * make a backup we won't discover it later). */ - file_readonly = !os_file_is_writable((char *)fname); + file_readonly = !os_file_is_writable(fname); if (!forceit && file_readonly) { if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) { @@ -2636,7 +2633,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * For systems that support ACL: get the ACL from the original file. */ if (!newfile) { - acl = mch_get_acl(fname); + acl = mch_get_acl((char_u *)fname); } #endif @@ -2644,8 +2641,8 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * If 'backupskip' is not empty, don't make a backup for some files. */ dobackup = (p_wb || p_bk || *p_pm != NUL); - if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) { - dobackup = FALSE; + if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, (char_u *)sfname, (char_u *)ffname)) { + dobackup = false; } /* @@ -2683,7 +2680,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * - we don't have write permission in the directory */ if (os_fileinfo_hardlinks(&file_info_old) > 1 - || !os_fileinfo_link((char *)fname, &file_info) + || !os_fileinfo_link(fname, &file_info) || !os_fileinfo_id_equal(&file_info, &file_info_old)) { backup_copy = TRUE; } else { @@ -2695,7 +2692,9 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ */ STRCPY(IObuff, fname); for (i = 4913;; i += 123) { - sprintf((char *)path_tail(IObuff), "%d", i); + char *tail = path_tail((char *)IObuff); + size_t size = (char_u *)tail - IObuff; + snprintf(tail, IOSIZE - size, "%d", i); if (!os_fileinfo_link((char *)IObuff, &file_info)) { break; } @@ -2727,7 +2726,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ */ if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) { #ifdef UNIX - bool file_info_link_ok = os_fileinfo_link((char *)fname, &file_info); + bool file_info_link_ok = os_fileinfo_link(fname, &file_info); // Symlinks. if ((bkc & BKC_BREAKSYMLINK) @@ -2748,17 +2747,17 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ // make sure we have a valid backup extension to use if (*p_bex == NUL) { - backup_ext = (char_u *)".bak"; + backup_ext = ".bak"; } else { - backup_ext = p_bex; + backup_ext = (char *)p_bex; } if (backup_copy) { - char_u *wp; + char *wp; int some_error = false; - char_u *dirp; - char_u *rootname; - char_u *p; + char *dirp; + char *rootname; + char *p; /* * Try to make the backup in each directory in the 'bdir' option. @@ -2772,14 +2771,14 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ * For these reasons, the existing writable file must be truncated * and reused. Creation of a backup COPY will be attempted. */ - dirp = p_bdir; + dirp = (char *)p_bdir; while (*dirp) { /* * Isolate one directory name, using an entry in 'bdir'. */ - size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); - p = IObuff + dir_len; - bool trailing_pathseps = after_pathsep((char *)IObuff, (char *)p) && p[-1] == p[-2]; + size_t dir_len = copy_option_part((char_u **)&dirp, IObuff, IOSIZE, ","); + p = (char *)IObuff + dir_len; + bool trailing_pathseps = after_pathsep((char *)IObuff, p) && p[-1] == p[-2]; if (trailing_pathseps) { IObuff[dir_len - 2] = NUL; } @@ -2794,15 +2793,14 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ } if (trailing_pathseps) { // Ends with '//', Use Full path - if ((p = (char_u *)make_percent_swname((char *)IObuff, (char *)fname)) + if ((p = make_percent_swname((char *)IObuff, fname)) != NULL) { - backup = (char_u *)modname((char *)p, (char *)backup_ext, - no_prepend_dot); + backup = modname(p, backup_ext, no_prepend_dot); xfree(p); } } - rootname = get_file_in_dir(fname, IObuff); + rootname = (char *)get_file_in_dir((char_u *)fname, IObuff); if (rootname == NULL) { some_error = TRUE; // out of memory goto nobackup; @@ -2814,8 +2812,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ // Make the backup file name. // if (backup == NULL) { - backup = (char_u *)modname((char *)rootname, (char *)backup_ext, - no_prepend_dot); + backup = modname(rootname, backup_ext, no_prepend_dot); } if (backup == NULL) { @@ -2827,7 +2824,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ /* * Check if backup file already exists. */ - if (os_fileinfo((char *)backup, &file_info_new)) { + if (os_fileinfo(backup, &file_info_new)) { if (os_fileinfo_id_equal(&file_info_new, &file_info_old)) { // // Backup file is same as original file. @@ -2846,9 +2843,8 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ wp = backup; } *wp = 'z'; - while (*wp > 'a' - && os_fileinfo((char *)backup, &file_info_new)) { - --*wp; + while (*wp > 'a' && os_fileinfo(backup, &file_info_new)) { + (*wp)--; } // They all exist??? Must be something wrong. if (*wp == 'a') { @@ -2864,7 +2860,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ */ if (backup != NULL) { // remove old backup, if present - os_remove((char *)backup); + os_remove(backup); // set file protection same as original file, but // strip s-bit. @@ -2877,26 +2873,26 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ // protection bits for others. // if (file_info_new.stat.st_gid != file_info_old.stat.st_gid - && os_chown((char *)backup, -1, file_info_old.stat.st_gid) != 0) { + && os_chown(backup, -1, file_info_old.stat.st_gid) != 0) { os_setperm((const char *)backup, (perm & 0707) | ((perm & 07) << 3)); } #endif // copy the file - if (os_copy((char *)fname, (char *)backup, UV_FS_COPYFILE_FICLONE) + if (os_copy(fname, backup, UV_FS_COPYFILE_FICLONE) != 0) { SET_ERRMSG(_("E506: Can't write to backup file " "(add ! to override)")); } #ifdef UNIX - os_file_settime((char *)backup, + os_file_settime(backup, file_info_old.stat.st_atim.tv_sec, file_info_old.stat.st_mtim.tv_sec); #endif #ifdef HAVE_ACL - mch_set_acl(backup, acl); + mch_set_acl((char_u *)backup, acl); #endif break; } @@ -2913,9 +2909,9 @@ nobackup: } SET_ERRMSG(NULL); } else { - char_u *dirp; - char_u *p; - char_u *rootname; + char *dirp; + char *p; + char *rootname; /* * Make a backup by renaming the original file. @@ -2936,14 +2932,14 @@ nobackup: * path/fo.o.h.bak Try all directories in 'backupdir', first one * that works is used. */ - dirp = p_bdir; + dirp = (char *)p_bdir; while (*dirp) { /* * Isolate one directory name and make the backup file name. */ - size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); - p = IObuff + dir_len; - bool trailing_pathseps = after_pathsep((char *)IObuff, (char *)p) && p[-1] == p[-2]; + size_t dir_len = copy_option_part((char_u **)&dirp, IObuff, IOSIZE, ","); + p = (char *)IObuff + dir_len; + bool trailing_pathseps = after_pathsep((char *)IObuff, p) && p[-1] == p[-2]; if (trailing_pathseps) { IObuff[dir_len - 2] = NUL; } @@ -2958,21 +2954,19 @@ nobackup: } if (trailing_pathseps) { // path ends with '//', use full path - if ((p = (char_u *)make_percent_swname((char *)IObuff, (char *)fname)) + if ((p = make_percent_swname((char *)IObuff, fname)) != NULL) { - backup = (char_u *)modname((char *)p, (char *)backup_ext, - no_prepend_dot); + backup = modname(p, backup_ext, no_prepend_dot); xfree(p); } } if (backup == NULL) { - rootname = get_file_in_dir(fname, IObuff); + rootname = (char *)get_file_in_dir((char_u *)fname, IObuff); if (rootname == NULL) { backup = NULL; } else { - backup = (char_u *)modname((char *)rootname, (char *)backup_ext, - no_prepend_dot); + backup = modname(rootname, backup_ext, no_prepend_dot); xfree(rootname); } } @@ -2983,13 +2977,13 @@ nobackup: * delete an existing one, try to use another name. * Change one character, just before the extension. */ - if (!p_bk && os_path_exists(backup)) { + if (!p_bk && os_path_exists((char_u *)backup)) { p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); if (p < backup) { // empty file name ??? p = backup; } *p = 'z'; - while (*p > 'a' && os_path_exists(backup)) { + while (*p > 'a' && os_path_exists((char_u *)backup)) { (*p)--; } // They all exist??? Must be something wrong! @@ -3007,7 +3001,7 @@ nobackup: // If the renaming of the original file to the backup file // works, quit here. /// - if (vim_rename(fname, backup) == 0) { + if (vim_rename((char_u *)fname, (char_u *)backup) == 0) { break; } @@ -3068,21 +3062,21 @@ nobackup: // Check for forced 'fileencoding' from "++opt=val" argument. if (eap != NULL && eap->force_enc != 0) { - fenc = (char_u *)eap->cmd + eap->force_enc; - fenc = enc_canonize(fenc); + fenc = eap->cmd + eap->force_enc; + fenc = (char *)enc_canonize((char_u *)fenc); fenc_tofree = fenc; } else { - fenc = buf->b_p_fenc; + fenc = (char *)buf->b_p_fenc; } // Check if the file needs to be converted. - converted = need_conversion(fenc); + converted = need_conversion((char_u *)fenc); // Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or // Latin1 to Unicode conversion. This is handled in buf_write_bytes(). // Prepare the flags for it and allocate bw_conv_buf when needed. if (converted) { - wb_flags = get_fio_flags(fenc); + wb_flags = get_fio_flags((char_u *)fenc); if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) { // Need to allocate a buffer to translate into. if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) { @@ -3102,7 +3096,7 @@ nobackup: #ifdef HAVE_ICONV // Use iconv() conversion when conversion is needed and it's not done // internally. - write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, (char_u *)"utf-8"); + write_info.bw_iconv_fd = (iconv_t)my_iconv_open((char_u *)fenc, (char_u *)"utf-8"); if (write_info.bw_iconv_fd != (iconv_t)-1) { // We're going to use iconv(), allocate a buffer to convert in. write_info.bw_conv_buflen = bufsize * ICONV_MULT; @@ -3120,7 +3114,7 @@ nobackup: * overwrite the original file. */ if (*p_ccv != NUL) { - wfname = vim_tempname(); + wfname = (char *)vim_tempname(); if (wfname == NULL) { // Can't write without a tempfile! SET_ERRMSG(_("E214: Can't find temp file for writing")); goto restore_backup; @@ -3164,7 +3158,7 @@ nobackup: // quotum for number of files). // Appending will fail if the file does not exist and forceit is // FALSE. - while ((fd = os_open((char *)wfname, + while ((fd = os_open(wfname, O_WRONLY | (append ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) @@ -3179,7 +3173,7 @@ nobackup: // Don't delete the file when it's a hard or symbolic link. if ((!newfile && os_fileinfo_hardlinks(&file_info_old) > 1) - || (os_fileinfo_link((char *)fname, &file_info) + || (os_fileinfo_link(fname, &file_info) && !os_fileinfo_id_equal(&file_info, &file_info_old))) { SET_ERRMSG(_("E166: Can't open linked file for writing")); } else { @@ -3200,7 +3194,7 @@ nobackup: } #endif if (!append) { // don't remove when appending - os_remove((char *)wfname); + os_remove(wfname); } continue; } @@ -3221,21 +3215,21 @@ restore_backup: // This may not work if the vim_rename() fails. // In that case we leave the copy around. // If file does not exist, put the copy in its place - if (!os_path_exists(fname)) { - vim_rename(backup, fname); + if (!os_path_exists((char_u *)fname)) { + vim_rename((char_u *)backup, (char_u *)fname); } // if original file does exist throw away the copy - if (os_path_exists(fname)) { - os_remove((char *)backup); + if (os_path_exists((char_u *)fname)) { + os_remove(backup); } } else { // try to put the original file back - vim_rename(backup, fname); + vim_rename((char_u *)backup, (char_u *)fname); } } // if original file no longer exists give an extra warning - if (!newfile && !os_path_exists(fname)) { + if (!newfile && !os_path_exists((char_u *)fname)) { end = 0; } } @@ -3249,7 +3243,7 @@ restore_backup: } SET_ERRMSG(NULL); - write_info.bw_buf = buffer; + write_info.bw_buf = (char_u *)buffer; nchars = 0; // use "++bin", "++nobin" or 'binary' @@ -3262,7 +3256,7 @@ restore_backup: // Skip the BOM when appending and the file already existed, the BOM // only makes sense at the start of the file. if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) { - write_info.bw_len = make_bom(buffer, fenc); + write_info.bw_len = make_bom((char_u *)buffer, (char_u *)fenc); if (write_info.bw_len > 0) { // don't convert write_info.bw_flags = FIO_NOCONVERT | wb_flags; @@ -3292,9 +3286,9 @@ restore_backup: for (lnum = start; lnum <= end; lnum++) { // The next while loop is done once for each character written. // Keep it fast! - ptr = ml_get_buf(buf, lnum, false) - 1; + ptr = (char *)ml_get_buf(buf, lnum, false) - 1; if (write_undo_file) { - sha256_update(&sha_ctx, ptr + 1, (uint32_t)(STRLEN(ptr + 1) + 1)); + sha256_update(&sha_ctx, (char_u *)ptr + 1, (uint32_t)(STRLEN(ptr + 1) + 1)); } while ((c = *++ptr) != NUL) { if (c == NL) { @@ -3403,7 +3397,7 @@ restore_backup: // don't change the owner when it's already OK, some systems remove // permission or ACL stuff FileInfo file_info; - if (!os_fileinfo((char *)wfname, &file_info) + if (!os_fileinfo(wfname, &file_info) || file_info.stat.st_uid != file_info_old.stat.st_uid || file_info.stat.st_gid != file_info_old.stat.st_gid) { os_fchown(fd, file_info_old.stat.st_uid, file_info_old.stat.st_gid); @@ -3435,7 +3429,7 @@ restore_backup: // Probably need to set the ACL before changing the user (can't set the // ACL on a file the user doesn't own). if (!backup_copy) { - mch_set_acl(wfname, acl); + mch_set_acl((char_u *)wfname, acl); } #endif @@ -3443,13 +3437,12 @@ restore_backup: // The file was written to a temp file, now it needs to be converted // with 'charconvert' to (overwrite) the output file. if (end != 0) { - if (eval_charconvert("utf-8", (char *)fenc, - (char *)wfname, (char *)fname) == FAIL) { + if (eval_charconvert("utf-8", fenc, wfname, fname) == FAIL) { write_info.bw_conv_error = true; end = 0; } } - os_remove((char *)wfname); + os_remove(wfname); xfree(wfname); } } @@ -3493,12 +3486,12 @@ restore_backup: } // copy the file. - if (os_copy((char *)backup, (char *)fname, UV_FS_COPYFILE_FICLONE) + if (os_copy(backup, fname, UV_FS_COPYFILE_FICLONE) == 0) { end = 1; // success } } else { - if (vim_rename(backup, fname) == 0) { + if (vim_rename((char_u *)backup, (char_u *)fname) == 0) { end = 1; } } @@ -3590,7 +3583,7 @@ restore_backup: * the backup file our 'original' file. */ if (*p_pm && dobackup) { - char *const org = modname((char *)fname, (char *)p_pm, false); + char *const org = modname(fname, (char *)p_pm, false); if (backup != NULL) { /* @@ -3600,7 +3593,7 @@ restore_backup: if (org == NULL) { emsg(_("E205: Patchmode: can't save original file")); } else if (!os_path_exists((char_u *)org)) { - vim_rename(backup, (char_u *)org); + vim_rename((char_u *)backup, (char_u *)org); XFREE_CLEAR(backup); // don't delete the file #ifdef UNIX os_file_settime(org, @@ -3636,7 +3629,7 @@ restore_backup: */ if (!p_bk && backup != NULL && !write_info.bw_conv_error - && os_remove((char *)backup) != 0) { + && os_remove(backup) != 0) { emsg(_("E207: Can't delete backup file")); } @@ -3700,7 +3693,7 @@ nofail: // Update the timestamp to avoid an "overwrite changed file" // prompt when writing again. - if (os_fileinfo((char *)fname, &file_info_old)) { + if (os_fileinfo(fname, &file_info_old)) { buf_store_file_info(buf, &file_info_old); buf->b_mtime_read = buf->b_mtime; buf->b_mtime_read_ns = buf->b_mtime_ns; @@ -3714,10 +3707,10 @@ nofail: * file. */ if (retval == OK && write_undo_file) { - char_u hash[UNDO_HASH_SIZE]; + char hash[UNDO_HASH_SIZE]; - sha256_finish(&sha_ctx, hash); - u_write_undo(NULL, FALSE, buf, hash); + sha256_finish(&sha_ctx, (char_u *)hash); + u_write_undo(NULL, false, buf, (char_u *)hash); } if (!should_abort(retval)) { @@ -4442,7 +4435,7 @@ char *modname(const char *fname, const char *ext, bool prepend_dot) char *e; // Prepend the dot if needed. - if (prepend_dot && *(e = (char *)path_tail((char_u *)retval)) != '.') { + if (prepend_dot && *(e = path_tail(retval)) != '.') { STRMOVE(e + 1, e); *e = '.'; } @@ -4653,7 +4646,7 @@ int vim_rename(const char_u *from, const char_u *to) * the file name differs we need to go through a temp file. */ if (FNAMECMP(from, to) == 0) { - if (p_fic && (STRCMP(path_tail((char_u *)from), path_tail((char_u *)to)) + if (p_fic && (STRCMP(path_tail((char *)from), path_tail((char *)to)) != 0)) { use_tmp_file = true; } else { @@ -4688,7 +4681,7 @@ int vim_rename(const char_u *from, const char_u *to) } STRCPY(tempname, from); for (n = 123; n < 99999; n++) { - char *tail = (char *)path_tail(tempname); + char *tail = path_tail((char *)tempname); snprintf(tail, (MAXPATHL + 1) - (tail - (char *)tempname - 1), "%d", n); if (!os_path_exists(tempname)) { @@ -5000,7 +4993,7 @@ int buf_check_timestamp(buf_T *buf) if (!bufref_valid(&bufref)) { emsg(_("E246: FileChangedShell autocommand deleted buffer")); } - s = get_vim_var_str(VV_FCS_CHOICE); + s = (char_u *)get_vim_var_str(VV_FCS_CHOICE); if (STRCMP(s, "reload") == 0 && *reason != 'd') { reload = RELOAD_NORMAL; } else if (STRCMP(s, "edit") == 0) { @@ -5199,7 +5192,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) if (saved == OK) { curbuf->b_flags |= BF_CHECK_RO; // check for RO again keep_filetype = true; // don't detect 'filetype' - if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0, + if (readfile((char *)buf->b_ffname, (char *)buf->b_fname, (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, &ea, flags, false) != OK) { if (!aborting()) { semsg(_("E321: Could not reload \"%s\""), buf->b_fname); @@ -5443,7 +5436,7 @@ void vim_deltempdir(void) { if (vim_tempdir != NULL) { // remove the trailing path separator - path_tail(vim_tempdir)[-1] = NUL; + path_tail((char *)vim_tempdir)[-1] = NUL; delete_recursive((const char *)vim_tempdir); XFREE_CLEAR(vim_tempdir); } @@ -5519,7 +5512,7 @@ char_u *vim_tempname(void) /// @param allow_dirs Allow matching with dir /// /// @return true if there is a match, false otherwise -bool match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, +bool match_file_pat(char *pattern, regprog_T **prog, char *fname, char *sfname, char *tail, int allow_dirs) { regmatch_T regmatch; @@ -5530,7 +5523,7 @@ bool match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sf if (prog != NULL) { regmatch.regprog = *prog; } else { - regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); + regmatch.regprog = vim_regcomp((char_u *)pattern, RE_MAGIC); } } @@ -5542,10 +5535,10 @@ bool match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sf */ if (regmatch.regprog != NULL && ((allow_dirs - && (vim_regexec(®match, fname, (colnr_T)0) + && (vim_regexec(®match, (char_u *)fname, (colnr_T)0) || (sfname != NULL - && vim_regexec(®match, sfname, (colnr_T)0)))) - || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) { + && vim_regexec(®match, (char_u *)sfname, (colnr_T)0)))) + || (!allow_dirs && vim_regexec(®match, (char_u *)tail, (colnr_T)0)))) { result = true; } @@ -5576,7 +5569,7 @@ bool match_file_list(char_u *list, char_u *sfname, char_u *ffname) bool match; char_u *p; - tail = path_tail(sfname); + tail = (char_u *)path_tail((char *)sfname); // try all patterns in 'wildignore' p = list; @@ -5586,7 +5579,8 @@ bool match_file_list(char_u *list, char_u *sfname, char_u *ffname) if (regpat == NULL) { break; } - match = match_file_pat(regpat, NULL, ffname, sfname, tail, (int)allow_dirs); + match = match_file_pat((char *)regpat, NULL, (char *)ffname, (char *)sfname, (char *)tail, + (int)allow_dirs); xfree(regpat); if (match) { return true; -- cgit From f0148de7907ec297647816d51c79745be729439e Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Mon, 9 May 2022 11:49:32 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c412af0eaa..06d72f3395 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -277,10 +277,10 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, // point to one of these values. old_curbuf = curbuf; old_b_ffname = (char *)curbuf->b_ffname; - old_b_fname = (char *)curbuf->b_fname; + old_b_fname = curbuf->b_fname; using_b_ffname = ((char_u *)fname == curbuf->b_ffname) || ((char_u *)sfname == curbuf->b_ffname); - using_b_fname = ((char_u *)fname == curbuf->b_fname) || ((char_u *)sfname == curbuf->b_fname); + using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); // After reading a file the cursor line changes but we don't want to // display the line. @@ -469,7 +469,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) || (using_b_fname - && ((char_u *)old_b_fname != curbuf->b_fname))) { + && (old_b_fname != curbuf->b_fname))) { emsg(_(e_auchangedbuf)); return FAIL; } @@ -540,7 +540,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, if (!read_stdin && (curbuf != old_curbuf || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) - || (using_b_fname && ((char_u *)old_b_fname != curbuf->b_fname)))) { + || (using_b_fname && (old_b_fname != curbuf->b_fname)))) { emsg(_(e_auchangedbuf)); if (!read_buffer) { close(fd); @@ -646,7 +646,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, */ if (!read_stdin && (curbuf != old_curbuf || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) - || (using_b_fname && ((char_u *)old_b_fname != curbuf->b_fname)) + || (using_b_fname && (old_b_fname != curbuf->b_fname)) || (fd = os_open(fname, O_RDONLY, 0)) < 0)) { no_wait_return--; msg_scroll = msg_save; @@ -1957,8 +1957,7 @@ failed: if (!au_did_filetype && *curbuf->b_p_ft != NUL) { // EVENT_FILETYPE was not triggered but the buffer already has a // filetype. Trigger EVENT_FILETYPE using the existing filetype. - apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname, - true, curbuf); + apply_autocmds(EVENT_FILETYPE, (char *)curbuf->b_p_ft, curbuf->b_fname, true, curbuf); } } else { apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, @@ -2084,7 +2083,7 @@ static char_u *next_fenc(char_u **pp, bool *alloced) *pp = NULL; return (char_u *)""; } - p = vim_strchr(*pp, ','); + p = (char_u *)vim_strchr((char *)(*pp), ','); if (p == NULL) { r = enc_canonize(*pp); *pp += STRLEN(*pp); @@ -2466,8 +2465,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en } if (reset_changed && buf->b_changed && !append && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) { - /* Buffer still changed, the autocommands didn't work - * properly. */ + // Buffer still changed, the autocommands didn't work properly. return FAIL; } return OK; @@ -4327,7 +4325,7 @@ void shorten_buf_fname(buf_T *buf, char_u *dirname, int force) if (buf->b_fname != NULL && !bt_nofile(buf) - && !path_with_url((char *)buf->b_fname) + && !path_with_url(buf->b_fname) && (force || buf->b_sfname == NULL || path_is_absolute(buf->b_sfname))) { @@ -4337,10 +4335,10 @@ void shorten_buf_fname(buf_T *buf, char_u *dirname, int force) p = path_shorten_fname(buf->b_ffname, dirname); if (p != NULL) { buf->b_sfname = vim_strsave(p); - buf->b_fname = buf->b_sfname; + buf->b_fname = (char *)buf->b_sfname; } if (p == NULL) { - buf->b_fname = buf->b_ffname; + buf->b_fname = (char *)buf->b_ffname; } } } @@ -4957,7 +4955,7 @@ int buf_check_timestamp(buf_T *buf) buf_store_file_info(buf, &file_info); } - if (os_isdir(buf->b_fname)) { + if (os_isdir((char_u *)buf->b_fname)) { // Don't do anything for a directory. Might contain the file explorer. } else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) && !bufIsChanged(buf) && file_info_ok) { @@ -4985,8 +4983,7 @@ int buf_check_timestamp(buf_T *buf) set_vim_var_string(VV_FCS_REASON, reason, -1); set_vim_var_string(VV_FCS_CHOICE, "", -1); allbuf_lock++; - bool n = apply_autocmds(EVENT_FILECHANGEDSHELL, - buf->b_fname, buf->b_fname, false, buf); + bool n = apply_autocmds(EVENT_FILECHANGEDSHELL, buf->b_fname, buf->b_fname, false, buf); allbuf_lock--; busy = false; if (n) { @@ -5046,7 +5043,7 @@ int buf_check_timestamp(buf_T *buf) } if (mesg != NULL) { - path = home_replace_save(buf, buf->b_fname); + path = home_replace_save(buf, (char_u *)buf->b_fname); if (!helpmesg) { mesg2 = ""; } @@ -5117,8 +5114,7 @@ int buf_check_timestamp(buf_T *buf) // Trigger FileChangedShell when the file was changed in any way. if (bufref_valid(&bufref) && retval != 0) { - (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, buf->b_fname, buf->b_fname, - false, buf); + (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, buf->b_fname, buf->b_fname, false, buf); } return retval; } @@ -5192,7 +5188,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) if (saved == OK) { curbuf->b_flags |= BF_CHECK_RO; // check for RO again keep_filetype = true; // don't detect 'filetype' - if (readfile((char *)buf->b_ffname, (char *)buf->b_fname, (linenr_T)0, (linenr_T)0, + if (readfile((char *)buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, &ea, flags, false) != OK) { if (!aborting()) { semsg(_("E321: Could not reload \"%s\""), buf->b_fname); @@ -5523,7 +5519,7 @@ bool match_file_pat(char *pattern, regprog_T **prog, char *fname, char *sfname, if (prog != NULL) { regmatch.regprog = *prog; } else { - regmatch.regprog = vim_regcomp((char_u *)pattern, RE_MAGIC); + regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); } } @@ -5575,7 +5571,7 @@ bool match_file_list(char_u *list, char_u *sfname, char_u *ffname) p = list; while (*p) { copy_option_part(&p, buf, ARRAY_SIZE(buf), ","); - regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, false); + regpat = (char_u *)file_pat_to_reg_pat((char *)buf, NULL, &allow_dirs, false); if (regpat == NULL) { break; } @@ -5600,13 +5596,12 @@ bool match_file_list(char_u *list, char_u *sfname, char_u *ffname) /// @param no_bslash Don't use a backward slash as pathsep /// /// @return NULL on failure. -char_u *file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *allow_dirs, - int no_bslash) +char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs, int no_bslash) FUNC_ATTR_NONNULL_ARG(1) { - const char_u *endp; - char_u *reg_pat; - const char_u *p; + const char *endp; + char *reg_pat; + const char *p; int nested = 0; bool add_dollar = true; @@ -5618,7 +5613,7 @@ char_u *file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *allo } if (pat_end == pat) { - return (char_u *)xstrdup("^$"); + return xstrdup("^$"); } size_t size = 2; // '^' at start, '$' at end. -- cgit From 9fec6dc9a25b5cf9c9a444ac2bd0728e8af3229e Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Wed, 25 May 2022 20:31:14 +0200 Subject: refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) --- src/nvim/fileio.c | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 06d72f3395..e869a9a6de 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -147,7 +147,6 @@ void filemess(buf_T *buf, char_u *name, char_u *s, int attr) msg_scrolled_ign = false; } - /// Read lines from file "fname" into the buffer after line "from". /// /// 1. We allocate blocks with try_malloc, as big as possible. @@ -839,7 +838,6 @@ retry: fio_flags = get_fio_flags((char_u *)fenc); } - #ifdef HAVE_ICONV // Try using iconv() if we can't convert internally. if (fio_flags == 0 @@ -1293,7 +1291,6 @@ retry: size -= conv_restlen; } - while (p > (uint8_t *)ptr) { if (fio_flags & FIO_LATIN1) { u8c = *--p; @@ -1995,7 +1992,6 @@ bool is_dev_fd_file(char_u *fname) } #endif - /// From the current line count and characters read after that, estimate the /// line number where we are now. /// Used for error messages that include a line number. @@ -2146,7 +2142,6 @@ static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp) return tmpname; } - /// Read marks for the current buffer from the ShaDa file, when we support /// buffer marks and the buffer has a name. static void check_marks_read(void) @@ -3053,7 +3048,6 @@ nobackup: } } - // Default: write the file directly. May write to a temp file for // multi-byte conversion. wfname = fname; @@ -3089,7 +3083,6 @@ nobackup: } } - if (converted && wb_flags == 0) { #ifdef HAVE_ICONV // Use iconv() conversion when conversion is needed and it's not done @@ -4109,7 +4102,6 @@ static bool ucs2bytes(unsigned c, char_u **pp, int flags) FUNC_ATTR_NONNULL_ALL bool error = false; int cc; - if (flags & FIO_UCS4) { if (flags & FIO_ENDIAN_L) { *p++ = c; @@ -4238,7 +4230,6 @@ static int get_fio_flags(const char_u *name) return 0; } - /// Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. /// "size" must be at least 2. /// @@ -5493,7 +5484,6 @@ char_u *vim_tempname(void) return vim_strsave(template); } - /// 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. -- cgit From 7b952793d5c46e862a9cdec3d6ac4762370296ed Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Thu, 26 May 2022 04:49:25 +0200 Subject: refactor: missing parenthesis may cause unexpected problems (#17443) related vim-8.2.{4402,4639} --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index e869a9a6de..8d8c10f823 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2195,11 +2195,11 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en linenr_T lnum; long nchars; #define SET_ERRMSG_NUM(num, msg) \ - errnum = num, errmsg = msg, errmsgarg = 0 + errnum = (num), errmsg = (msg), errmsgarg = 0 #define SET_ERRMSG_ARG(msg, error) \ - errnum = NULL, errmsg = msg, errmsgarg = error + errnum = NULL, errmsg = (msg), errmsgarg = error #define SET_ERRMSG(msg) \ - errnum = NULL, errmsg = msg, errmsgarg = 0 + errnum = NULL, errmsg = (msg), errmsgarg = 0 const char *errnum = NULL; char *errmsg = NULL; int errmsgarg = 0; -- cgit From 6130b4a84b41b71f4c0c58093a29585c6c67ff16 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 13 Jun 2022 20:20:34 +0800 Subject: vim-patch:8.2.1898: command modifier parsing always uses global cmdmod Problem: Command modifier parsing always uses global cmdmod. Solution: Pass in cmdmod_T to use. Rename struct fields consistently. https://github.com/vim/vim/commit/e10044015841711b989f9a898d427bcc1fdb4c32 --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 8d8c10f823..4c1dd63b8f 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1890,7 +1890,7 @@ failed: check_cursor_lnum(); beginline(BL_WHITE | BL_FIX); // on first non-blank - if (!cmdmod.lockmarks) { + if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) { // Set '[ and '] marks to the newly read lines. curbuf->b_op_start.lnum = from + 1; curbuf->b_op_start.col = 0; @@ -2425,7 +2425,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) || did_cmd || nofile_err || aborting()) { - if (buf != NULL && cmdmod.lockmarks) { + if (buf != NULL && (cmdmod.cmod_flags & CMOD_LOCKMARKS)) { // restore the original '[ and '] positions buf->b_op_start = orig_start; buf->b_op_end = orig_end; @@ -2511,7 +2511,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en } } - if (cmdmod.lockmarks) { + if (cmdmod.cmod_flags & CMOD_LOCKMARKS) { // restore the original '[ and '] positions buf->b_op_start = orig_start; buf->b_op_end = orig_end; -- cgit From 014a88799a1d175ad121c520c9cc5bd0bb2d8813 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Tue, 28 Jun 2022 11:31:54 +0200 Subject: refactor: replace char_u #18429 Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 4c1dd63b8f..f53f70dcda 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -1697,7 +1697,7 @@ failed: save_file_ff(curbuf); // If editing a new file: set 'fenc' for the current buffer. // Also for ":read ++edit file". - set_string_option_direct("fenc", -1, (char_u *)fenc, OPT_FREE | OPT_LOCAL, 0); + set_string_option_direct("fenc", -1, fenc, OPT_FREE | OPT_LOCAL, 0); } if (fenc_alloced) { xfree(fenc); @@ -2057,7 +2057,7 @@ void set_forced_fenc(exarg_T *eap) { if (eap->force_enc != 0) { char_u *fenc = enc_canonize((char_u *)eap->cmd + eap->force_enc); - set_string_option_direct("fenc", -1, fenc, OPT_FREE|OPT_LOCAL, 0); + set_string_option_direct("fenc", -1, (char *)fenc, OPT_FREE|OPT_LOCAL, 0); xfree(fenc); } } @@ -3765,7 +3765,7 @@ static int set_rw_fname(char_u *fname, char_u *sfname) return FAIL; } - if (setfname(curbuf, fname, sfname, false) == OK) { + if (setfname(curbuf, (char *)fname, (char *)sfname, false) == OK) { curbuf->b_flags |= BF_NOTEDITED; } @@ -3805,8 +3805,7 @@ static void add_quoted_fname(char *const ret_buf, const size_t buf_len, const bu fname = "-stdin-"; } ret_buf[0] = '"'; - home_replace(buf, (const char_u *)fname, (char_u *)ret_buf + 1, - (int)buf_len - 4, true); + home_replace(buf, fname, ret_buf + 1, (int)buf_len - 4, true); xstrlcat(ret_buf, "\" ", buf_len); } -- cgit From 593f2f753fe9dbef0fcbafae0f6b66b044bd9d84 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Tue, 28 Jun 2022 12:04:46 +0200 Subject: refactor: enable -Wconversion warning for fileio.c Work on https://github.com/neovim/neovim/issues/567 --- src/nvim/fileio.c | 176 ++++++++++++++++++++++++++---------------------------- 1 file changed, 84 insertions(+), 92 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f53f70dcda..ba3e7a0150 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -186,7 +186,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, || (eap != NULL && eap->read_edit); linenr_T read_buf_lnum = 1; // next line to read from curbuf colnr_T read_buf_col = 0; // next char to read from this line - char_u c; + char c; linenr_T lnum = from; char *ptr = NULL; // pointer into read buffer char *buffer = NULL; // read buffer @@ -422,7 +422,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, * not be able to write to the file ourselves. * Setting the bits is done below, after creating the swap file. */ - swap_mode = (file_info.stat.st_mode & 0644) | 0600; + swap_mode = ((int)file_info.stat.st_mode & 0644) | 0600; #endif } else { curbuf->b_mtime = 0; @@ -562,7 +562,8 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, if (os_fileinfo(swap_fname, &swap_info) && file_info.stat.st_gid != swap_info.stat.st_gid - && os_fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, file_info.stat.st_gid) + && os_fchown(curbuf->b_ml.ml_mfp->mf_fd, (uv_uid_t)(-1), + (uv_gid_t)file_info.stat.st_gid) == -1) { swap_mode &= 0600; } @@ -984,7 +985,7 @@ retry: #endif if (conv_restlen > 0) { // Insert unconverted bytes from previous line. - memmove(ptr, conv_rest, conv_restlen); // -V614 + memmove(ptr, conv_rest, (size_t)conv_restlen); // -V614 ptr += conv_restlen; size -= conv_restlen; } @@ -1013,7 +1014,7 @@ retry: if (p[ni] == NL) { ptr[tlen++] = NUL; } else { - ptr[tlen++] = p[ni]; + ptr[tlen++] = (char)p[ni]; } } read_buf_col += n; @@ -1026,7 +1027,7 @@ retry: if (p[ni] == NL) { ptr[tlen++] = NUL; } else { - ptr[tlen++] = p[ni]; + ptr[tlen++] = (char)p[ni]; } } ptr[tlen++] = NL; @@ -1046,7 +1047,7 @@ retry: /* * Read bytes from the file. */ - size = read_eintr(fd, ptr, size); + size = read_eintr(fd, ptr, (size_t)size); } if (size <= 0) { @@ -1091,8 +1092,8 @@ retry: #endif )) { while (conv_restlen > 0) { - *(--ptr) = bad_char_behavior; - --conv_restlen; + *(--ptr) = (char)bad_char_behavior; + conv_restlen--; } } fio_flags = 0; // don't convert this @@ -1172,20 +1173,12 @@ retry: #ifdef HAVE_ICONV if (iconv_fd != (iconv_t)-1) { - /* - * Attempt conversion of the read bytes to 'encoding' using - * iconv(). - */ - const char *fromp; - char *top; - size_t from_size; - size_t to_size; - - fromp = ptr; - from_size = size; + // Attempt conversion of the read bytes to 'encoding' using iconv(). + const char *fromp = ptr; + size_t from_size = (size_t)size; ptr += size; - top = ptr; - to_size = real_size - size; + char *top = ptr; + size_t to_size = (size_t)(real_size - size); /* * If there is conversion error or not enough room try using @@ -1210,8 +1203,8 @@ retry: *top++ = *(fromp - 1); --to_size; } else if (bad_char_behavior != BAD_DROP) { - *top++ = bad_char_behavior; - --to_size; + *top++ = (char)bad_char_behavior; + to_size--; } } @@ -1263,11 +1256,11 @@ retry: if ((fio_flags & FIO_UTF16) && p > (uint8_t *)ptr) { // Check for a trailing leading word if (fio_flags & FIO_ENDIAN_L) { - u8c = (*--p << 8); + u8c = (unsigned)(*--p) << 8; u8c += *--p; } else { u8c = *--p; - u8c += (*--p << 8); + u8c += (unsigned)(*--p) << 8; } if (u8c >= 0xd800 && u8c <= 0xdbff) { tail = (char *)p; @@ -1287,7 +1280,7 @@ retry: // conv_rest[]. if (tail != NULL) { conv_restlen = (int)((ptr + size) - tail); - memmove(conv_rest, tail, conv_restlen); + memmove(conv_rest, tail, (size_t)conv_restlen); size -= conv_restlen; } @@ -1296,11 +1289,11 @@ retry: u8c = *--p; } else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) { if (fio_flags & FIO_ENDIAN_L) { - u8c = (*--p << 8); + u8c = (unsigned)(*--p) << 8; u8c += *--p; } else { u8c = *--p; - u8c += (*--p << 8); + u8c += (unsigned)(*--p) << 8; } if ((fio_flags & FIO_UTF16) && u8c >= 0xdc00 && u8c <= 0xdfff) { @@ -1318,7 +1311,7 @@ retry: continue; } if (bad_char_behavior != BAD_KEEP) { - u8c = bad_char_behavior; + u8c = (unsigned)bad_char_behavior; } } @@ -1331,7 +1324,7 @@ retry: u16c = *--p; u16c += (*--p << 8); } - u8c = 0x10000 + ((u16c & 0x3ff) << 10) + u8c = 0x10000 + (((unsigned)u16c & 0x3ff) << 10) + (u8c & 0x3ff); // Check if the word is indeed a leading word. @@ -1346,7 +1339,7 @@ retry: continue; } if (bad_char_behavior != BAD_KEEP) { - u8c = bad_char_behavior; + u8c = (unsigned)bad_char_behavior; } } } @@ -1372,7 +1365,7 @@ retry: } else { len = utf_head_off((char_u *)ptr, p); p -= len; - u8c = utf_ptr2char((char *)p); + u8c = (unsigned)utf_ptr2char((char *)p); if (len == 0) { // Not a valid UTF-8 character, retry with // another fenc when possible, otherwise just @@ -1387,7 +1380,7 @@ retry: continue; } if (bad_char_behavior != BAD_KEEP) { - u8c = bad_char_behavior; + u8c = (unsigned)bad_char_behavior; } } } @@ -1433,7 +1426,7 @@ retry: // already done so. if (p > (uint8_t *)ptr) { conv_restlen = todo; - memmove(conv_rest, p, conv_restlen); + memmove(conv_rest, p, (size_t)conv_restlen); size -= conv_restlen; break; } @@ -1458,11 +1451,11 @@ retry: // Drop, keep or replace the bad byte. if (bad_char_behavior == BAD_DROP) { - memmove(p, p + 1, todo - 1); - --p; - --size; + memmove(p, p + 1, (size_t)(todo - 1)); + p--; + size--; } else if (bad_char_behavior != BAD_KEEP) { - *p = bad_char_behavior; + *p = (uint8_t)bad_char_behavior; } } else { p += l - 1; @@ -1583,7 +1576,7 @@ rewind_retry: break; } if (read_undo_file) { - sha256_update(&sha_ctx, (char_u *)line_start, len); + sha256_update(&sha_ctx, (char_u *)line_start, (size_t)len); } ++lnum; if (--read_count == 0) { @@ -1639,7 +1632,7 @@ rewind_retry: break; } if (read_undo_file) { - sha256_update(&sha_ctx, (char_u *)line_start, len); + sha256_update(&sha_ctx, (char_u *)line_start, (size_t)len); } ++lnum; if (--read_count == 0) { @@ -1686,7 +1679,7 @@ failed: error = true; } else { if (read_undo_file) { - sha256_update(&sha_ctx, (char_u *)line_start, len); + sha256_update(&sha_ctx, (char_u *)line_start, (size_t)len); } read_no_eol_lnum = ++lnum; } @@ -2084,7 +2077,7 @@ static char_u *next_fenc(char_u **pp, bool *alloced) r = enc_canonize(*pp); *pp += STRLEN(*pp); } else { - r = vim_strnsave(*pp, p - *pp); + r = vim_strnsave(*pp, (size_t)(p - *pp)); *pp = p + 1; p = enc_canonize(r); xfree(r); @@ -2552,7 +2545,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en if (!os_fileinfo(fname, &file_info_old)) { newfile = true; } else { - perm = file_info_old.stat.st_mode; + perm = (long)file_info_old.stat.st_mode; if (!S_ISREG(file_info_old.stat.st_mode)) { // not a file if (S_ISDIR(file_info_old.stat.st_mode)) { SET_ERRMSG_NUM("E502", _("is a directory")); @@ -2686,19 +2679,19 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en STRCPY(IObuff, fname); for (i = 4913;; i += 123) { char *tail = path_tail((char *)IObuff); - size_t size = (char_u *)tail - IObuff; + size_t size = (size_t)((char_u *)tail - IObuff); snprintf(tail, IOSIZE - size, "%d", i); if (!os_fileinfo_link((char *)IObuff, &file_info)) { break; } } fd = os_open((char *)IObuff, - O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); + O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, (int)perm); if (fd < 0) { // can't write in directory backup_copy = TRUE; } else { #ifdef UNIX - os_fchown(fd, file_info_old.stat.st_uid, file_info_old.stat.st_gid); + os_fchown(fd, (uv_uid_t)file_info_old.stat.st_uid, (uv_gid_t)file_info_old.stat.st_gid); if (!os_fileinfo((char *)IObuff, &file_info) || file_info.stat.st_uid != file_info_old.stat.st_uid || file_info.stat.st_gid != file_info_old.stat.st_gid @@ -2866,9 +2859,9 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en // protection bits for others. // if (file_info_new.stat.st_gid != file_info_old.stat.st_gid - && os_chown(backup, -1, file_info_old.stat.st_gid) != 0) { + && os_chown(backup, (uv_uid_t)-1, (uv_gid_t)file_info_old.stat.st_gid) != 0) { os_setperm((const char *)backup, - (perm & 0707) | ((perm & 07) << 3)); + ((int)perm & 0707) | (((int)perm & 07) << 3)); } #endif @@ -2881,8 +2874,8 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en #ifdef UNIX os_file_settime(backup, - file_info_old.stat.st_atim.tv_sec, - file_info_old.stat.st_mtim.tv_sec); + (double)file_info_old.stat.st_atim.tv_sec, + (double)file_info_old.stat.st_mtim.tv_sec); #endif #ifdef HAVE_ACL mch_set_acl((char_u *)backup, acl); @@ -3014,7 +3007,7 @@ nobackup: && file_info_old.stat.st_uid == getuid() && vim_strchr(p_cpo, CPO_FWRITE) == NULL) { perm |= 0200; - (void)os_setperm((const char *)fname, perm); + (void)os_setperm((const char *)fname, (int)perm); made_writable = true; } #endif @@ -3072,9 +3065,9 @@ nobackup: if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) { // Need to allocate a buffer to translate into. if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) { - write_info.bw_conv_buflen = bufsize * 2; + write_info.bw_conv_buflen = (size_t)bufsize * 2; } else { // FIO_UCS4 - write_info.bw_conv_buflen = bufsize * 4; + write_info.bw_conv_buflen = (size_t)bufsize * 4; } write_info.bw_conv_buf = verbose_try_malloc(write_info.bw_conv_buflen); if (!write_info.bw_conv_buf) { @@ -3090,7 +3083,7 @@ nobackup: write_info.bw_iconv_fd = (iconv_t)my_iconv_open((char_u *)fenc, (char_u *)"utf-8"); if (write_info.bw_iconv_fd != (iconv_t)-1) { // We're going to use iconv(), allocate a buffer to convert in. - write_info.bw_conv_buflen = bufsize * ICONV_MULT; + write_info.bw_conv_buflen = (size_t)bufsize * ICONV_MULT; write_info.bw_conv_buf = verbose_try_malloc(write_info.bw_conv_buflen); if (!write_info.bw_conv_buf) { end = 0; @@ -3391,9 +3384,9 @@ restore_backup: if (!os_fileinfo(wfname, &file_info) || file_info.stat.st_uid != file_info_old.stat.st_uid || file_info.stat.st_gid != file_info_old.stat.st_gid) { - os_fchown(fd, file_info_old.stat.st_uid, file_info_old.stat.st_gid); + os_fchown(fd, (uv_uid_t)file_info_old.stat.st_uid, (uv_gid_t)file_info_old.stat.st_gid); if (perm >= 0) { // Set permission again, may have changed. - (void)os_setperm((const char *)wfname, perm); + (void)os_setperm(wfname, (int)perm); } } buf_set_file_id(buf); @@ -3414,7 +3407,7 @@ restore_backup: } #endif if (perm >= 0) { // Set perm. of new file same as old file. - (void)os_setperm((const char *)wfname, perm); + (void)os_setperm((const char *)wfname, (int)perm); } #ifdef HAVE_ACL // Probably need to set the ACL before changing the user (can't set the @@ -3588,8 +3581,8 @@ restore_backup: XFREE_CLEAR(backup); // don't delete the file #ifdef UNIX os_file_settime(org, - file_info_old.stat.st_atim.tv_sec, - file_info_old.stat.st_mtim.tv_sec); + (double)file_info_old.stat.st_atim.tv_sec, + (double)file_info_old.stat.st_mtim.tv_sec); #endif } } @@ -3805,7 +3798,7 @@ static void add_quoted_fname(char *const ret_buf, const size_t buf_len, const bu fname = "-stdin-"; } ret_buf[0] = '"'; - home_replace(buf, fname, ret_buf + 1, (int)buf_len - 4, true); + home_replace(buf, fname, ret_buf + 1, buf_len - 4, true); xstrlcat(ret_buf, "\" ", buf_len); } @@ -3846,14 +3839,14 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) *p++ = ' '; } if (shortmess(SHM_LINES)) { - vim_snprintf((char *)p, IOSIZE - (p - IObuff), "%" PRId64 "L, %" PRId64 "B", + vim_snprintf((char *)p, (size_t)(IOSIZE - (p - IObuff)), "%" PRId64 "L, %" PRId64 "B", (int64_t)lnum, (int64_t)nchars); } else { - vim_snprintf((char *)p, IOSIZE - (p - IObuff), + vim_snprintf((char *)p, (size_t)(IOSIZE - (p - IObuff)), NGETTEXT("%" PRId64 " line, ", "%" PRId64 " lines, ", lnum), (int64_t)lnum); p += STRLEN(p); - vim_snprintf((char *)p, IOSIZE - (p - IObuff), + vim_snprintf((char *)p, (size_t)(IOSIZE - (p - IObuff)), NGETTEXT("%" PRId64 " byte", "%" PRId64 " bytes", nchars), (int64_t)nchars); } @@ -3965,7 +3958,7 @@ static int buf_write_bytes(struct bw_info *ip) break; } if (n > 1) { - c = utf_ptr2char((char *)ip->bw_rest); + c = (unsigned)utf_ptr2char((char *)ip->bw_rest); } else { c = ip->bw_rest[0]; } @@ -3993,7 +3986,7 @@ static int buf_write_bytes(struct bw_info *ip) break; } if (n > 1) { - c = utf_ptr2char((char *)buf + wlen); + c = (unsigned)utf_ptr2char((char *)buf + wlen); } else { c = buf[wlen]; } @@ -4029,7 +4022,7 @@ static int buf_write_bytes(struct bw_info *ip) /* Need to concatenate the remainder of the previous call and * the bytes of the current call. Use the end of the * conversion buffer for this. */ - fromlen = len + ip->bw_restlen; + fromlen = (size_t)len + (size_t)ip->bw_restlen; fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); memmove(fp + ip->bw_restlen, buf, (size_t)len); @@ -4037,7 +4030,7 @@ static int buf_write_bytes(struct bw_info *ip) tolen = ip->bw_conv_buflen - fromlen; } else { from = (const char *)buf; - fromlen = len; + fromlen = (size_t)len; tolen = ip->bw_conv_buflen; } to = (char *)ip->bw_conv_buf; @@ -4084,7 +4077,7 @@ static int buf_write_bytes(struct bw_info *ip) // Only checking conversion, which is OK if we get here. return OK; } - wlen = write_eintr(ip->bw_fd, buf, len); + wlen = (int)write_eintr(ip->bw_fd, buf, (size_t)len); return (wlen < len) ? FAIL : OK; } @@ -4103,15 +4096,15 @@ static bool ucs2bytes(unsigned c, char_u **pp, int flags) FUNC_ATTR_NONNULL_ALL if (flags & FIO_UCS4) { if (flags & FIO_ENDIAN_L) { - *p++ = c; - *p++ = (c >> 8); - *p++ = (c >> 16); - *p++ = (c >> 24); + *p++ = (uint8_t)c; + *p++ = (uint8_t)(c >> 8); + *p++ = (uint8_t)(c >> 16); + *p++ = (uint8_t)(c >> 24); } else { - *p++ = (c >> 24); - *p++ = (c >> 16); - *p++ = (c >> 8); - *p++ = c; + *p++ = (uint8_t)(c >> 24); + *p++ = (uint8_t)(c >> 16); + *p++ = (uint8_t)(c >> 8); + *p++ = (uint8_t)c; } } else if (flags & (FIO_UCS2 | FIO_UTF16)) { if (c >= 0x10000) { @@ -4122,13 +4115,13 @@ static bool ucs2bytes(unsigned c, char_u **pp, int flags) FUNC_ATTR_NONNULL_ALL if (c >= 0x100000) { error = true; } - cc = ((c >> 10) & 0x3ff) + 0xd800; + cc = (int)(((c >> 10) & 0x3ff) + 0xd800); if (flags & FIO_ENDIAN_L) { - *p++ = cc; - *p++ = ((unsigned)cc >> 8); + *p++ = (uint8_t)cc; + *p++ = (uint8_t)(cc >> 8); } else { - *p++ = ((unsigned)cc >> 8); - *p++ = cc; + *p++ = (uint8_t)(cc >> 8); + *p++ = (uint8_t)cc; } c = (c & 0x3ff) + 0xdc00; } else { @@ -4136,18 +4129,18 @@ static bool ucs2bytes(unsigned c, char_u **pp, int flags) FUNC_ATTR_NONNULL_ALL } } if (flags & FIO_ENDIAN_L) { - *p++ = c; - *p++ = (c >> 8); + *p++ = (uint8_t)c; + *p++ = (uint8_t)(c >> 8); } else { - *p++ = (c >> 8); - *p++ = c; + *p++ = (uint8_t)(c >> 8); + *p++ = (uint8_t)c; } } else { // Latin1 if (c >= 0x100) { error = true; *p++ = 0xBF; } else { - *p++ = c; + *p++ = (uint8_t)c; } } @@ -4670,7 +4663,7 @@ int vim_rename(const char_u *from, const char_u *to) STRCPY(tempname, from); for (n = 123; n < 99999; n++) { char *tail = path_tail((char *)tempname); - snprintf(tail, (MAXPATHL + 1) - (tail - (char *)tempname - 1), "%d", n); + snprintf(tail, (size_t)((MAXPATHL + 1) - (tail - (char *)tempname - 1)), "%d", n); if (!os_path_exists(tempname)) { if (os_rename(from, tempname) == OK) { @@ -4744,8 +4737,8 @@ int vim_rename(const char_u *from, const char_u *to) return -1; } - while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) { - if (write_eintr(fd_out, buffer, n) != n) { + while ((n = (int)read_eintr(fd_in, buffer, BUFSIZE)) > 0) { + if (write_eintr(fd_out, buffer, (size_t)n) != n) { errmsg = _("E208: Error writing to \"%s\""); break; } @@ -5795,12 +5788,11 @@ long read_eintr(int fd, void *buf, size_t bufsize) long write_eintr(int fd, void *buf, size_t bufsize) { long ret = 0; - long wlen; // Repeat the write() so long it didn't fail, other than being interrupted // by a signal. while (ret < (long)bufsize) { - wlen = write(fd, (char *)buf + ret, bufsize - ret); + long wlen = write(fd, (char *)buf + ret, bufsize - (size_t)ret); if (wlen < 0) { if (errno != EINTR) { break; -- cgit From f50135a32e11c535e1dc3a8e9460c5b4e640ee86 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 30 Jun 2022 13:16:46 +0200 Subject: feat: stdpath('run'), /tmp/nvim.user/ #18993 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: - Since c57f6b28d71d #8519, sockets are created in ~/.local/… but XDG spec says: "XDG_RUNTIME_DIR: Must be on the local filesystem", which implies that XDG_STATE_DIR is potentially non-local. - Not easy to inspect Nvim-created temp files (for debugging etc). Solution: - Store sockets in stdpath('run') ($XDG_RUNTIME_DIR). - Establish "/tmp/nvim.user/" as the tempdir root shared by all Nvims. - Make ok() actually useful. - Introduce assert_nolog(). closes #3517 closes #17093 --- src/nvim/fileio.c | 96 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 30 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index ba3e7a0150..33ae1dbad0 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5281,45 +5281,80 @@ void forward_slash(char_u *fname) } #endif -/// Name of Vim's own temp dir. Ends in a slash. -static char_u *vim_tempdir = NULL; +/// Path to Nvim's own temp dir. Ends in a slash. +static char *vim_tempdir = NULL; -/// Create a directory for private use by this instance of Neovim. -/// This is done once, and the same directory is used for all temp files. +/// Creates a directory for private use by this instance of Nvim, trying each of +/// `TEMP_DIR_NAMES` until one succeeds. +/// +/// Only done once, the same directory is used for all temp files. /// This method avoids security problems because of symlink attacks et al. /// It's also a bit faster, because we only need to check for an existing /// file when creating the directory and not for each temp file. -static void vim_maketempdir(void) +static void vim_mktempdir(void) { - static const char *temp_dirs[] = TEMP_DIR_NAMES; - // Try the entries in `TEMP_DIR_NAMES` to create the temp directory. - char_u template[TEMP_FILE_PATH_MAXLEN]; - char_u path[TEMP_FILE_PATH_MAXLEN]; + static const char *temp_dirs[] = TEMP_DIR_NAMES; // Try each of these until one succeeds. + char tmp[TEMP_FILE_PATH_MAXLEN]; + char path[TEMP_FILE_PATH_MAXLEN]; + char user[40] = { 0 }; + + (void)os_get_username(user, sizeof(user)); // Make sure the umask doesn't remove the executable bit. // "repl" has been reported to use "0177". mode_t umask_save = umask(0077); for (size_t i = 0; i < ARRAY_SIZE(temp_dirs); i++) { - // Expand environment variables, leave room for "/nvimXXXXXX/999999999" - expand_env((char_u *)temp_dirs[i], template, TEMP_FILE_PATH_MAXLEN - 22); - if (!os_isdir(template)) { // directory doesn't exist + // Expand environment variables, leave room for "/tmp/nvim./XXXXXX/999999999". + expand_env((char_u *)temp_dirs[i], (char_u *)tmp, TEMP_FILE_PATH_MAXLEN - 64); + if (!os_isdir((char_u *)tmp)) { continue; } - add_pathsep((char *)template); - // Concatenate with temporary directory name pattern - STRCAT(template, "nvimXXXXXX"); + // "/tmp/" exists, now try to create "/tmp/nvim./". + add_pathsep(tmp); + xstrlcat(tmp, "nvim.", sizeof(tmp)); + xstrlcat(tmp, user, sizeof(tmp)); + (void)os_mkdir(tmp, 0700); // Always create, to avoid a race. + bool owned = os_file_owned(tmp); + bool isdir = os_isdir((char_u *)tmp); +#ifdef UNIX + int perm = os_getperm(tmp); // XDG_RUNTIME_DIR must be owned by the user, mode 0700. + bool valid = isdir && owned && 0700 == (perm & 0777); +#else + bool valid = isdir && owned; // TODO(justinmk): Windows ACL? +#endif + if (valid) { + add_pathsep(tmp); + } else { + if (!owned) { + ELOG("tempdir root not owned by current user (%s): %s", user, tmp); + } else if (!isdir) { + ELOG("tempdir root not a directory: %s", tmp); + } +#ifdef UNIX + if (0700 != (perm & 0777)) { + ELOG("tempdir root has invalid permissions (%o): %s", perm, tmp); + } +#endif + // If our "root" tempdir is invalid or fails, proceed without "/". + // Else user1 could break user2 by creating "/tmp/nvim.user2/". + tmp[strlen(tmp) - strlen(user)] = '\0'; + } - if (os_mkdtemp((const char *)template, (char *)path) != 0) { + // Now try to create "/tmp/nvim./XXXXXX". + xstrlcat(tmp, "XXXXXX", sizeof(tmp)); // mkdtemp "template", will be replaced with random alphanumeric chars. + int r = os_mkdtemp(tmp, path); + if (r != 0) { + WLOG("tempdir create failed: %s: %s", os_strerror(r), tmp); continue; } - if (vim_settempdir((char *)path)) { + if (vim_settempdir(path)) { // Successfully created and set temporary directory so stop trying. break; } else { // Couldn't set `vim_tempdir` to `path` so remove created directory. - os_rmdir((char *)path); + os_rmdir(path); } } (void)umask(umask_save); @@ -5415,26 +5450,27 @@ void vim_deltempdir(void) { if (vim_tempdir != NULL) { // remove the trailing path separator - path_tail((char *)vim_tempdir)[-1] = NUL; - delete_recursive((const char *)vim_tempdir); + path_tail(vim_tempdir)[-1] = NUL; + delete_recursive(vim_tempdir); XFREE_CLEAR(vim_tempdir); } } -/// @return the name of temp directory. This directory would be created on the first -/// call to this function. -char_u *vim_gettempdir(void) +/// Gets path to Nvim's own temp dir (ending with slash). +/// +/// Creates the directory on the first call. +char *vim_gettempdir(void) { if (vim_tempdir == NULL) { - vim_maketempdir(); + vim_mktempdir(); } return vim_tempdir; } -/// Set Neovim own temporary directory name to `tempdir`. This directory should -/// be already created. Expand this name to a full path and put it in -/// `vim_tempdir`. This avoids that using `:cd` would confuse us. +/// Sets Nvim's own temporary directory name to `tempdir`. This directory must +/// already exist. Expands the name to a full path and put it in `vim_tempdir`. +/// This avoids that using `:cd` would confuse us. /// /// @param tempdir must be no longer than MAXPATHL. /// @@ -5447,7 +5483,7 @@ static bool vim_settempdir(char *tempdir) } vim_FullName(tempdir, buf, MAXPATHL, false); add_pathsep(buf); - vim_tempdir = (char_u *)xstrdup(buf); + vim_tempdir = xstrdup(buf); xfree(buf); return true; } @@ -5456,14 +5492,14 @@ static bool vim_settempdir(char *tempdir) /// /// @note The temp file is NOT created. /// -/// @return pointer to the temp file name or NULL if Neovim can't create +/// @return pointer to the temp file name or NULL if Nvim can't create /// temporary directory for its own temporary files. char_u *vim_tempname(void) { // Temp filename counter. static uint64_t temp_count; - char_u *tempdir = vim_gettempdir(); + char *tempdir = vim_gettempdir(); if (!tempdir) { return NULL; } -- cgit From cb84f5ee530f0f32b92bed5b4ad41344e8b551aa Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:27:52 +0200 Subject: refactor(uncrustify): change config to better align with style guide (#18803) refactor(uncrustify): change config to better align with neovim style --- src/nvim/fileio.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 33ae1dbad0..9ba55befdd 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -4886,13 +4886,11 @@ int buf_check_timestamp(buf_T *buf) char *mesg2 = ""; bool helpmesg = false; - // uncrustify:off enum { RELOAD_NONE, RELOAD_NORMAL, - RELOAD_DETECT + RELOAD_DETECT, } reload = RELOAD_NONE; - // uncrustify:on bool can_reload = false; uint64_t orig_size = buf->b_orig_size; -- cgit From 3b8804571c565a91c9ce729bb487c7ba21b659e0 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Tue, 28 Jun 2022 13:03:09 +0200 Subject: refactor: replace char_u Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/fileio.c | 67 +++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 34 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 9ba55befdd..febde53ce2 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -275,10 +275,9 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, // executing nasty autocommands. Also check if "fname" and "sfname" // point to one of these values. old_curbuf = curbuf; - old_b_ffname = (char *)curbuf->b_ffname; + old_b_ffname = curbuf->b_ffname; old_b_fname = curbuf->b_fname; - using_b_ffname = ((char_u *)fname == curbuf->b_ffname) - || ((char_u *)sfname == curbuf->b_ffname); + using_b_ffname = (fname == curbuf->b_ffname) || (sfname == curbuf->b_ffname); using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); // After reading a file the cursor line changes but we don't want to @@ -373,7 +372,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, && !S_ISFIFO(perm) // ... or fifo && !S_ISSOCK(perm) // ... or socket #ifdef OPEN_CHR_FILES - && !(S_ISCHR(perm) && is_dev_fd_file((char_u *)fname)) + && !(S_ISCHR(perm) && is_dev_fd_file(fname)) // ... or a character special file named /dev/fd/ #endif ) { @@ -466,7 +465,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, // SwapExists autocommand may mess things up if (curbuf != old_curbuf || (using_b_ffname - && ((char_u *)old_b_ffname != curbuf->b_ffname)) + && (old_b_ffname != curbuf->b_ffname)) || (using_b_fname && (old_b_fname != curbuf->b_fname))) { emsg(_(e_auchangedbuf)); @@ -538,7 +537,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, check_need_swap(newfile); if (!read_stdin && (curbuf != old_curbuf - || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) + || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) || (using_b_fname && (old_b_fname != curbuf->b_fname)))) { emsg(_(e_auchangedbuf)); if (!read_buffer) { @@ -645,7 +644,7 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip, * (cd for example) if it invalidates fname or sfname. */ if (!read_stdin && (curbuf != old_curbuf - || (using_b_ffname && ((char_u *)old_b_ffname != curbuf->b_ffname)) + || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) || (using_b_fname && (old_b_fname != curbuf->b_fname)) || (fd = os_open(fname, O_RDONLY, 0)) < 0)) { no_wait_return--; @@ -1974,12 +1973,12 @@ failed: /// Do not accept "/dev/fd/[012]", opening these may hang Vim. /// /// @param fname file name to check -bool is_dev_fd_file(char_u *fname) +bool is_dev_fd_file(char *fname) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { return STRNCMP(fname, "/dev/fd/", 8) == 0 - && ascii_isdigit(fname[8]) - && *skipdigits((char *)fname + 9) == NUL + && ascii_isdigit((uint8_t)fname[8]) + && *skipdigits(fname + 9) == NUL && (fname[9] != NUL || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')); } @@ -2342,16 +2341,16 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en * Set curbuf to the buffer to be written. * Careful: The autocommands may call buf_write() recursively! */ - if ((char_u *)ffname == buf->b_ffname) { + if (ffname == buf->b_ffname) { buf_ffname = true; } - if ((char_u *)sfname == buf->b_sfname) { + if (sfname == buf->b_sfname) { buf_sfname = true; } - if ((char_u *)fname == buf->b_ffname) { + if (fname == buf->b_ffname) { buf_fname_f = true; } - if ((char_u *)fname == buf->b_sfname) { + if (fname == buf->b_sfname) { buf_fname_s = true; } @@ -2491,16 +2490,16 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en * be kept in fname, ffname and sfname. */ if (buf_ffname) { - ffname = (char *)buf->b_ffname; + ffname = buf->b_ffname; } if (buf_sfname) { - sfname = (char *)buf->b_sfname; + sfname = buf->b_sfname; } if (buf_fname_f) { - fname = (char *)buf->b_ffname; + fname = buf->b_ffname; } if (buf_fname_s) { - fname = (char *)buf->b_sfname; + fname = buf->b_sfname; } } @@ -2762,7 +2761,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en /* * Isolate one directory name, using an entry in 'bdir'. */ - size_t dir_len = copy_option_part((char_u **)&dirp, IObuff, IOSIZE, ","); + size_t dir_len = copy_option_part(&dirp, (char *)IObuff, IOSIZE, ","); p = (char *)IObuff + dir_len; bool trailing_pathseps = after_pathsep((char *)IObuff, p) && p[-1] == p[-2]; if (trailing_pathseps) { @@ -2923,7 +2922,7 @@ nobackup: /* * Isolate one directory name and make the backup file name. */ - size_t dir_len = copy_option_part((char_u **)&dirp, IObuff, IOSIZE, ","); + size_t dir_len = copy_option_part(&dirp, (char *)IObuff, IOSIZE, ","); p = (char *)IObuff + dir_len; bool trailing_pathseps = after_pathsep((char *)IObuff, p) && p[-1] == p[-2]; if (trailing_pathseps) { @@ -4304,24 +4303,24 @@ static int make_bom(char_u *buf, char_u *name) /// name. void shorten_buf_fname(buf_T *buf, char_u *dirname, int force) { - char_u *p; + char *p; if (buf->b_fname != NULL && !bt_nofile(buf) && !path_with_url(buf->b_fname) && (force || buf->b_sfname == NULL - || path_is_absolute(buf->b_sfname))) { + || path_is_absolute((char_u *)buf->b_sfname))) { if (buf->b_sfname != buf->b_ffname) { XFREE_CLEAR(buf->b_sfname); } - p = path_shorten_fname(buf->b_ffname, dirname); + p = (char *)path_shorten_fname((char_u *)buf->b_ffname, dirname); if (p != NULL) { - buf->b_sfname = vim_strsave(p); - buf->b_fname = (char *)buf->b_sfname; + buf->b_sfname = xstrdup(p); + buf->b_fname = buf->b_sfname; } if (p == NULL) { - buf->b_fname = (char *)buf->b_ffname; + buf->b_fname = buf->b_ffname; } } } @@ -4918,7 +4917,7 @@ int buf_check_timestamp(buf_T *buf) bool file_info_ok; if (!(buf->b_flags & BF_NOTEDITED) && buf->b_mtime != 0 - && (!(file_info_ok = os_fileinfo((char *)buf->b_ffname, &file_info)) + && (!(file_info_ok = os_fileinfo(buf->b_ffname, &file_info)) || time_differs(&file_info, buf->b_mtime, buf->b_mtime_ns) || (int)file_info.stat.st_mode != buf->b_orig_mode)) { const long prev_b_mtime = buf->b_mtime; @@ -5016,7 +5015,7 @@ int buf_check_timestamp(buf_T *buf) } } } else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) - && os_path_exists(buf->b_ffname)) { + && os_path_exists((char_u *)buf->b_ffname)) { retval = 1; mesg = _("W13: Warning: File \"%s\" has been created after editing started"); buf->b_flags |= BF_NEW_W; @@ -5024,7 +5023,7 @@ int buf_check_timestamp(buf_T *buf) } if (mesg != NULL) { - path = home_replace_save(buf, (char_u *)buf->b_fname); + path = (char_u *)home_replace_save(buf, buf->b_fname); if (!helpmesg) { mesg2 = ""; } @@ -5169,7 +5168,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) if (saved == OK) { curbuf->b_flags |= BF_CHECK_RO; // check for RO again keep_filetype = true; // don't detect 'filetype' - if (readfile((char *)buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0, + if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, &ea, flags, false) != OK) { if (!aborting()) { semsg(_("E321: Could not reload \"%s\""), buf->b_fname); @@ -5547,10 +5546,10 @@ bool match_file_pat(char *pattern, regprog_T **prog, char *fname, char *sfname, */ if (regmatch.regprog != NULL && ((allow_dirs - && (vim_regexec(®match, (char_u *)fname, (colnr_T)0) + && (vim_regexec(®match, fname, (colnr_T)0) || (sfname != NULL - && vim_regexec(®match, (char_u *)sfname, (colnr_T)0)))) - || (!allow_dirs && vim_regexec(®match, (char_u *)tail, (colnr_T)0)))) { + && vim_regexec(®match, sfname, (colnr_T)0)))) + || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) { result = true; } @@ -5586,7 +5585,7 @@ bool match_file_list(char_u *list, char_u *sfname, char_u *ffname) // try all patterns in 'wildignore' p = list; while (*p) { - copy_option_part(&p, buf, ARRAY_SIZE(buf), ","); + copy_option_part((char **)&p, (char *)buf, ARRAY_SIZE(buf), ","); regpat = (char_u *)file_pat_to_reg_pat((char *)buf, NULL, &allow_dirs, false); if (regpat == NULL) { break; -- cgit From 4a64cdafd64198d3607fabaed8fc54e473614269 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 15 Jul 2022 17:53:00 +0800 Subject: vim-patch:8.1.1547: functionality of bt_nofile() is confusing Problem: Functionality of bt_nofile() is confusing. Solution: Split into bt_nofile() and bt_nofilename(). https://github.com/vim/vim/commit/26910de8b0da6abab87bd5a397330f9cbe483309 --- src/nvim/fileio.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index febde53ce2..6782465ef1 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2286,7 +2286,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en && reset_changed && whole && buf == curbuf - && !bt_nofile(buf) + && !bt_nofilename(buf) && !filtering && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) { @@ -2360,22 +2360,22 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en if (append) { if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, - sfname, sfname, FALSE, curbuf, eap))) { - if (overwriting && bt_nofile(curbuf)) { - nofile_err = TRUE; + sfname, sfname, false, curbuf, eap))) { + if (overwriting && bt_nofilename(curbuf)) { + nofile_err = true; } else { apply_autocmds_exarg(EVENT_FILEAPPENDPRE, - sfname, sfname, FALSE, curbuf, eap); + sfname, sfname, false, curbuf, eap); } } } else if (filtering) { apply_autocmds_exarg(EVENT_FILTERWRITEPRE, - NULL, sfname, FALSE, curbuf, eap); + NULL, sfname, false, curbuf, eap); } else if (reset_changed && whole) { int was_changed = curbufIsChanged(); did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, - sfname, sfname, FALSE, curbuf, eap); + sfname, sfname, false, curbuf, eap); if (did_cmd) { if (was_changed && !curbufIsChanged()) { /* Written everything correctly and BufWriteCmd has reset @@ -2385,21 +2385,21 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en u_update_save_nr(curbuf); } } else { - if (overwriting && bt_nofile(curbuf)) { - nofile_err = TRUE; + if (overwriting && bt_nofilename(curbuf)) { + nofile_err = true; } else { apply_autocmds_exarg(EVENT_BUFWRITEPRE, - sfname, sfname, FALSE, curbuf, eap); + sfname, sfname, false, curbuf, eap); } } } else { if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, - sfname, sfname, FALSE, curbuf, eap))) { - if (overwriting && bt_nofile(curbuf)) { - nofile_err = TRUE; + sfname, sfname, false, curbuf, eap))) { + if (overwriting && bt_nofilename(curbuf)) { + nofile_err = true; } else { apply_autocmds_exarg(EVENT_FILEWRITEPRE, - sfname, sfname, FALSE, curbuf, eap); + sfname, sfname, false, curbuf, eap); } } } @@ -4306,7 +4306,7 @@ void shorten_buf_fname(buf_T *buf, char_u *dirname, int force) char *p; if (buf->b_fname != NULL - && !bt_nofile(buf) + && !bt_nofilename(buf) && !path_with_url(buf->b_fname) && (force || buf->b_sfname == NULL -- cgit