Refactor vim_tempname

- temp_count is uint32_t now instead of long because it supposed to be
  at most 999999999 (comment on line 5227) temporary files. The most
  probably it was a long for compatibility with systems where int is
  16-bit.
- Use "nvim" as prefix for temp folder name instead of "v"
- Remove unused parameter from vim_tempname
This commit is contained in:
Pavel Platto 2014-06-19 23:46:51 +03:00 committed by Nicolas Hillegeer
parent edd7a8c5dd
commit 29e0cd1571
12 changed files with 26 additions and 43 deletions

View File

@ -661,9 +661,9 @@ void ex_diffupdate(exarg_T *eap)
} }
// We need three temp file names. // We need three temp file names.
char_u *tmp_orig = vim_tempname('o'); char_u *tmp_orig = vim_tempname();
char_u *tmp_new = vim_tempname('n'); char_u *tmp_new = vim_tempname();
char_u *tmp_diff = vim_tempname('d'); char_u *tmp_diff = vim_tempname();
if ((tmp_orig == NULL) || (tmp_new == NULL) || (tmp_diff == NULL)) { if ((tmp_orig == NULL) || (tmp_new == NULL) || (tmp_diff == NULL)) {
goto theend; goto theend;
@ -852,9 +852,9 @@ void ex_diffpatch(exarg_T *eap)
#endif // ifdef UNIX #endif // ifdef UNIX
// We need two temp file names. // We need two temp file names.
// Name of original temp file. // Name of original temp file.
char_u *tmp_orig = vim_tempname('o'); char_u *tmp_orig = vim_tempname();
// Name of patched temp file. // Name of patched temp file.
char_u *tmp_new = vim_tempname('n'); char_u *tmp_new = vim_tempname();
if ((tmp_orig == NULL) || (tmp_new == NULL)) { if ((tmp_orig == NULL) || (tmp_new == NULL)) {
goto theend; goto theend;

View File

@ -14030,7 +14030,7 @@ static void f_system(typval_T *argvars, typval_T *rettv)
* Write the string to a temp file, to be used for input of the shell * Write the string to a temp file, to be used for input of the shell
* command. * command.
*/ */
if ((infile = vim_tempname('i')) == NULL) { if ((infile = vim_tempname()) == NULL) {
EMSG(_(e_notmp)); EMSG(_(e_notmp));
goto done; goto done;
} }
@ -14231,22 +14231,8 @@ static void f_taglist(typval_T *argvars, typval_T *rettv)
*/ */
static void f_tempname(typval_T *argvars, typval_T *rettv) static void f_tempname(typval_T *argvars, typval_T *rettv)
{ {
static int x = 'A';
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
rettv->vval.v_string = vim_tempname(x); rettv->vval.v_string = vim_tempname();
/* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
* names. Skip 'I' and 'O', they are used for shell redirection. */
do {
if (x == 'Z')
x = '0';
else if (x == '9')
x = 'A';
else {
++x;
}
} while (x == 'I' || x == 'O');
} }
/* /*

View File

@ -1032,8 +1032,8 @@ do_filter (
curbuf->b_op_start.lnum = line1; curbuf->b_op_start.lnum = line1;
curbuf->b_op_end.lnum = line2; curbuf->b_op_end.lnum = line2;
curwin->w_cursor.lnum = line2; curwin->w_cursor.lnum = line2;
} else if ((do_in && (itmp = vim_tempname('i')) == NULL) } else if ((do_in && (itmp = vim_tempname()) == NULL)
|| (do_out && (otmp = vim_tempname('o')) == NULL)) { || (do_out && (otmp = vim_tempname()) == NULL)) {
EMSG(_(e_notmp)); EMSG(_(e_notmp));
goto filterend; goto filterend;
} }
@ -1601,7 +1601,7 @@ void write_viminfo(char_u *file, int forceit)
*/ */
if (fp_out == NULL) { if (fp_out == NULL) {
free(tempname); free(tempname);
if ((tempname = vim_tempname('o')) != NULL) if ((tempname = vim_tempname()) != NULL)
fp_out = mch_fopen((char *)tempname, WRITEBIN); fp_out = mch_fopen((char *)tempname, WRITEBIN);
} }

View File

@ -2146,7 +2146,7 @@ readfile_charconvert (
char_u *tmpname; char_u *tmpname;
char_u *errmsg = NULL; char_u *errmsg = NULL;
tmpname = vim_tempname('r'); tmpname = vim_tempname();
if (tmpname == NULL) if (tmpname == NULL)
errmsg = (char_u *)_("Can't find temp file for conversion"); errmsg = (char_u *)_("Can't find temp file for conversion");
else { else {
@ -3163,7 +3163,7 @@ nobackup:
* overwrite the original file. * overwrite the original file.
*/ */
if (*p_ccv != NUL) { if (*p_ccv != NUL) {
wfname = vim_tempname('w'); wfname = vim_tempname();
if (wfname == NULL) { /* Can't write without a tempfile! */ if (wfname == NULL) { /* Can't write without a tempfile! */
errmsg = (char_u *)_("E214: Can't find temp file for writing"); errmsg = (char_u *)_("E214: Can't find temp file for writing");
goto restore_backup; goto restore_backup;
@ -5156,7 +5156,7 @@ void write_lnum_adjust(linenr_T offset)
curbuf->b_no_eol_lnum += offset; curbuf->b_no_eol_lnum += offset;
} }
static long temp_count = 0; /* Temp filename counter. */ static uint32_t temp_count = 0; /* Temp filename counter. */
/* /*
* Delete the temp directory and all files it contains. * Delete the temp directory and all files it contains.
@ -5208,10 +5208,7 @@ static void vim_settempdir(char_u *tempdir)
* The returned pointer is to allocated memory. * The returned pointer is to allocated memory.
* The returned pointer is NULL if no valid name was found. * The returned pointer is NULL if no valid name was found.
*/ */
char_u * char_u *vim_tempname(void)
vim_tempname (
int extra_char /* char to use in the name instead of '?' */
)
{ {
char_u itmp[TEMP_FILE_PATH_MAXLEN]; char_u itmp[TEMP_FILE_PATH_MAXLEN];
@ -5230,13 +5227,13 @@ vim_tempname (
* Try the entries in `TEMP_DIR_NAMES` to create the temp directory. * Try the entries in `TEMP_DIR_NAMES` to create the temp directory.
*/ */
for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) {
/* expand $TMP, leave room for "/v1100000/999999999" */ /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */
expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 20); expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22);
if (os_isdir(itmp)) { /* directory exists */ if (os_isdir(itmp)) { /* directory exists */
add_pathsep(itmp); add_pathsep(itmp);
/* Leave room for filename */ /* Leave room for filename */
STRCAT(itmp, "vXXXXXX"); STRCAT(itmp, "nvimXXXXXX");
if (os_mkdtemp((char *)itmp) != NULL) if (os_mkdtemp((char *)itmp) != NULL)
vim_settempdir(itmp); vim_settempdir(itmp);
if (vim_tempdir != NULL) if (vim_tempdir != NULL)
@ -5248,7 +5245,7 @@ vim_tempname (
if (vim_tempdir != NULL) { if (vim_tempdir != NULL) {
/* There is no need to check if the file exists, because we own the /* There is no need to check if the file exists, because we own the
* directory and nobody else creates a file in it. */ * directory and nobody else creates a file in it. */
sprintf((char *)itmp, "%s%" PRId64, vim_tempdir, (int64_t)temp_count++); sprintf((char *)itmp, "%s%" PRIu32, vim_tempdir, temp_count++);
return vim_strsave(itmp); return vim_strsave(itmp);
} }

View File

@ -2346,7 +2346,7 @@ int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
/* If the user didn't specify a file name, use a temp file. */ /* If the user didn't specify a file name, use a temp file. */
if (psettings->outfile == NULL) { if (psettings->outfile == NULL) {
prt_ps_file_name = vim_tempname('p'); prt_ps_file_name = vim_tempname();
if (prt_ps_file_name == NULL) { if (prt_ps_file_name == NULL) {
EMSG(_(e_notmp)); EMSG(_(e_notmp));
return FAIL; return FAIL;

View File

@ -1044,7 +1044,7 @@ static int cs_find_common(char *opt, char *pat, int forceit, int verbose, int us
if (qfpos != NULL && *qfpos != '0' && totmatches > 0) { if (qfpos != NULL && *qfpos != '0' && totmatches > 0) {
/* fill error list */ /* fill error list */
FILE *f; FILE *f;
char_u *tmp = vim_tempname('c'); char_u *tmp = vim_tempname();
qf_info_T *qi = NULL; qf_info_T *qi = NULL;
win_T *wp = NULL; win_T *wp = NULL;

View File

@ -490,7 +490,7 @@ void ml_open_file(buf_T *buf)
/* For a spell buffer use a temp file name. */ /* For a spell buffer use a temp file name. */
if (buf->b_spell) { if (buf->b_spell) {
fname = vim_tempname('s'); fname = vim_tempname();
if (fname != NULL) if (fname != NULL)
(void)mf_open_file(mfp, fname); /* consumes fname! */ (void)mf_open_file(mfp, fname); /* consumes fname! */
buf->b_may_swap = FALSE; buf->b_may_swap = FALSE;

View File

@ -3423,7 +3423,7 @@ get_cmd_output (
return NULL; return NULL;
/* get a name for the temp file */ /* get a name for the temp file */
if ((tempname = vim_tempname('o')) == NULL) { if ((tempname = vim_tempname()) == NULL) {
EMSG(_(e_notmp)); EMSG(_(e_notmp));
return NULL; return NULL;
} }

View File

@ -52,7 +52,7 @@ void server_init(void)
servers = pmap_new(cstr_t)(); servers = pmap_new(cstr_t)();
if (!os_getenv("NEOVIM_LISTEN_ADDRESS")) { if (!os_getenv("NEOVIM_LISTEN_ADDRESS")) {
char *listen_address = (char *)vim_tempname('s'); char *listen_address = (char *)vim_tempname();
os_setenv("NEOVIM_LISTEN_ADDRESS", listen_address, 1); os_setenv("NEOVIM_LISTEN_ADDRESS", listen_address, 1);
free(listen_address); free(listen_address);
} }

View File

@ -1034,7 +1034,7 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
/* /*
* get a name for the temp file * get a name for the temp file
*/ */
if ((tempname = vim_tempname('o')) == NULL) { if ((tempname = vim_tempname()) == NULL) {
EMSG(_(e_notmp)); EMSG(_(e_notmp));
return FAIL; return FAIL;
} }

View File

@ -2537,7 +2537,7 @@ static char_u *get_mef_name(void)
static int off = 0; static int off = 0;
if (*p_mef == NUL) { if (*p_mef == NUL) {
name = vim_tempname('e'); name = vim_tempname();
if (name == NULL) if (name == NULL)
EMSG(_(e_notmp)); EMSG(_(e_notmp));
return name; return name;

View File

@ -7814,7 +7814,7 @@ spell_add_word (
if (idx == 0) { // use internal wordlist if (idx == 0) { // use internal wordlist
if (int_wordlist == NULL) { if (int_wordlist == NULL) {
int_wordlist = vim_tempname('s'); int_wordlist = vim_tempname();
if (int_wordlist == NULL) if (int_wordlist == NULL)
return; return;
} }