aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/ex_cmds.c2
-rw-r--r--src/nvim/fileio.c136
-rw-r--r--src/nvim/macros.h2
-rw-r--r--src/nvim/memline.c8
4 files changed, 75 insertions, 73 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 830636fb66..bc2485afc6 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -1530,7 +1530,7 @@ void write_viminfo(char_u *file, int forceit)
#endif
// Make tempname
- tempname = modname(fname, (char_u *)".tmp", FALSE);
+ tempname = (char_u *)modname((char *)fname, ".tmp", FALSE);
if (tempname != NULL) {
/*
* Check if tempfile already exists. Never overwrite an
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c
index 44bed92133..a33863b3a9 100644
--- a/src/nvim/fileio.c
+++ b/src/nvim/fileio.c
@@ -2814,7 +2814,7 @@ buf_write (
/*
* Make backup file name.
*/
- backup = modname(rootname, backup_ext, FALSE);
+ backup = (char_u *)modname((char *)rootname, (char *)backup_ext, FALSE);
if (backup == NULL) {
xfree(rootname);
some_error = TRUE; /* out of memory */
@@ -2985,7 +2985,7 @@ nobackup:
if (rootname == NULL)
backup = NULL;
else {
- backup = modname(rootname, backup_ext, FALSE);
+ backup = (char_u *)modname((char *)rootname, (char *)backup_ext, FALSE);
xfree(rootname);
}
@@ -3595,7 +3595,7 @@ restore_backup:
* the backup file our 'original' file.
*/
if (*p_pm && dobackup) {
- char *org = (char *)modname(fname, p_pm, FALSE);
+ char *org = modname((char *)fname, (char *)p_pm, FALSE);
if (backup != NULL) {
/*
@@ -4333,108 +4333,110 @@ void shorten_fnames(int force)
redraw_tabline = TRUE;
}
-/*
- * add extension to file name - change path/fo.o.h to path/fo.o.h.ext
- *
- * Assumed that fname is a valid name found in the filesystem we assure that
- * the return value is a different name and ends in 'ext'.
- * "ext" MUST be at most 4 characters long if it starts with a dot, 3
- * characters otherwise.
- * Space for the returned name is allocated, must be freed later.
- * Returns NULL when out of memory.
- */
-char_u *
-modname (
- char_u *fname,
- char_u *ext,
- int prepend_dot /* may prepend a '.' to file name */
-)
+/// Get new filename ended by given extension.
+///
+/// @param fname The original filename.
+/// If NULL, use current directory name and ext to
+/// compute new filename.
+/// @param ext The extension to add to the filename.
+/// 4 chars max if prefixed with a dot, 3 otherwise.
+/// @param prepend_dot If true, prefix ext with a dot.
+/// Does nothing if ext already starts with a dot, or
+/// if fname is NULL.
+///
+/// @return [allocated] - A new filename, made up from:
+/// * fname + ext, if fname not NULL.
+/// * current dir + ext, if fname is NULL.
+/// On Windows, and if ext starts with ".", a "_" is
+/// preprended to ext (for filename to be valid).
+/// Result is guaranteed to:
+/// * be ended by <ext>.
+/// * have a basename with at most BASENAMELEN chars:
+/// original basename is truncated if necessary.
+/// * be different than original: basename chars are
+/// replaced by "_" if necessary. If that can't be done
+/// because truncated value of original filename was
+/// made of all underscores, replace first "_" by "v".
+/// - NULL, if fname is NULL and there was a problem trying
+/// to get current directory.
+char *modname(const char *fname, const char *ext, bool prepend_dot)
+ FUNC_ATTR_NONNULL_ARG(2)
{
- char_u *retval;
- char_u *s;
- char_u *e;
- char_u *ptr;
- int fnamelen, extlen;
-
- extlen = (int)STRLEN(ext);
+ char *retval;
+ size_t fnamelen;
+ size_t extlen = strlen(ext);
- /*
- * If there is no file name we must get the name of the current directory
- * (we need the full path in case :cd is used).
- */
+ // If there is no file name we must get the name of the current directory
+ // (we need the full path in case :cd is used).
if (fname == NULL || *fname == NUL) {
- retval = xmalloc(MAXPATHL + extlen + 3);
- if (os_dirname(retval, MAXPATHL) == FAIL ||
- (fnamelen = (int)STRLEN(retval)) == 0) {
+ retval = xmalloc(MAXPATHL + extlen + 3); // +3 for PATHSEP, "_" (Win), NUL
+ if (os_dirname((char_u *)retval, MAXPATHL) == FAIL ||
+ (fnamelen = strlen(retval)) == 0) {
xfree(retval);
return NULL;
}
- if (!after_pathsep((char *)retval, (char *)retval + fnamelen)) {
+ if (!after_pathsep(retval, retval + fnamelen)) {
retval[fnamelen++] = PATHSEP;
retval[fnamelen] = NUL;
}
- prepend_dot = FALSE; /* nothing to prepend a dot to */
+ prepend_dot = FALSE; // nothing to prepend a dot to
} else {
- fnamelen = (int)STRLEN(fname);
+ fnamelen = strlen(fname);
retval = xmalloc(fnamelen + extlen + 3);
- STRCPY(retval, fname);
+ strcpy(retval, fname);
}
- /*
- * search backwards until we hit a '/', '\' or ':'.
- * Then truncate what is after the '/', '\' or ':' to BASENAMELEN characters.
- */
+ // Search backwards until we hit a '/', '\' or ':'.
+ // Then truncate what is after the '/', '\' or ':' to BASENAMELEN characters.
+ char *ptr = NULL;
for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) {
if (vim_ispathsep(*ptr)) {
- ++ptr;
+ ptr++;
break;
}
}
- /* the file name has at most BASENAMELEN characters. */
- if (STRLEN(ptr) > BASENAMELEN)
+ // the file name has at most BASENAMELEN characters.
+ if (strlen(ptr) > BASENAMELEN) {
ptr[BASENAMELEN] = '\0';
+ }
- s = ptr + STRLEN(ptr);
+ char *s;
+ s = ptr + strlen(ptr);
#if defined(WIN3264)
- /*
- * If there is no file name, and the extension starts with '.', put a
- * '_' before the dot, because just ".ext" may be invalid if it's on a
- * FAT partition, and on HPFS it doesn't matter.
- */
- else if ((fname == NULL || *fname == NUL) && *ext == '.')
+ // If there is no file name, and the extension starts with '.', put a
+ // '_' before the dot, because just ".ext" may be invalid if it's on a
+ // FAT partition, and on HPFS it doesn't matter.
+ else if ((fname == NULL || *fname == NUL) && *ext == '.') {
*s++ = '_';
+ }
#endif
- /*
- * Append the extension.
- * ext can start with '.' and cannot exceed 3 more characters.
- */
- STRCPY(s, ext);
+ // Append the extension.
+ // ext can start with '.' and cannot exceed 3 more characters.
+ strcpy(s, ext);
- /*
- * Prepend the dot.
- */
- if (prepend_dot && *(e = path_tail(retval)) != '.') {
+ char *e;
+ // Prepend the dot if needed.
+ if (prepend_dot && *(e = (char *)path_tail((char_u *)retval)) != '.') {
STRMOVE(e + 1, e);
*e = '.';
}
- /*
- * Check that, after appending the extension, the file name is really
- * different.
- */
- if (fname != NULL && STRCMP(fname, retval) == 0) {
- /* we search for a character that can be replaced by '_' */
+ // Check that, after appending the extension, the file name is really
+ // different.
+ if (fname != NULL && strcmp(fname, retval) == 0) {
+ // we search for a character that can be replaced by '_'
while (--s >= ptr) {
if (*s != '_') {
*s = '_';
break;
}
}
- if (s < ptr) /* fname was "________.<ext>", how tricky! */
+ if (s < ptr) { // fname was "________.<ext>", how tricky!
*ptr = 'v';
+ }
}
return retval;
}
diff --git a/src/nvim/macros.h b/src/nvim/macros.h
index 46f591eb33..c637351c84 100644
--- a/src/nvim/macros.h
+++ b/src/nvim/macros.h
@@ -133,7 +133,7 @@
# define mb_cptr_adv(p) p += \
enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
/* Backup multi-byte pointer. Only use with "p" > "s" ! */
-# define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
+# define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)((char_u *)s, (char_u *)p - 1) + 1) : 1
/* get length of multi-byte char, not including composing chars */
# define mb_cptr2len(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index 8e87bef244..0a65e8a0e9 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -1364,7 +1364,7 @@ recover_names (
* Try finding a swap file by simply adding ".swp" to the file name.
*/
if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) {
- char_u *swapname = modname(fname_res, (char_u *)".swp", TRUE);
+ char_u *swapname = (char_u *)modname((char *)fname_res, ".swp", TRUE);
if (swapname != NULL) {
if (os_file_exists(swapname)) {
files = (char_u **)xmalloc(sizeof(char_u *));
@@ -1565,7 +1565,7 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot)
// May also add the file name with a dot prepended, for swap file in same
// dir as original file.
if (prepend_dot) {
- names[num_names] = modname(path, (char_u *)".sw?", TRUE);
+ names[num_names] = (char_u *)modname((char *)path, ".sw?", TRUE);
if (names[num_names] == NULL)
return num_names;
++num_names;
@@ -3069,7 +3069,7 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name
if (after_pathsep((char *)dir_name, (char *)s) && s[-1] == s[-2]) { /* Ends with '//', Use Full path */
r = NULL;
if ((s = make_percent_swname(dir_name, fname)) != NULL) {
- r = modname(s, (char_u *)".swp", FALSE);
+ r = (char_u *)modname((char *)s, ".swp", FALSE);
xfree(s);
}
return r;
@@ -3083,7 +3083,7 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name
#endif
// Prepend a '.' to the swap file name for the current directory.
- r = modname(fname_res, (char_u *)".swp",
+ r = (char_u *)modname((char *)fname_res, ".swp",
dir_name[0] == '.' && dir_name[1] == NUL);
if (r == NULL) /* out of memory */
return NULL;