aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/diff.c1
-rw-r--r--src/nvim/eval.c1
-rw-r--r--src/nvim/ex_cmds.c1
-rw-r--r--src/nvim/fileio.c110
-rw-r--r--src/nvim/hardcopy.c1
-rw-r--r--src/nvim/if_cscope.c1
-rw-r--r--src/nvim/memline.c1
-rw-r--r--src/nvim/misc1.c1
-rw-r--r--src/nvim/os/server.c2
-rw-r--r--src/nvim/os_unix.c1
-rw-r--r--src/nvim/quickfix.c1
-rw-r--r--src/nvim/spell.c1
-rw-r--r--src/nvim/tempfile.c124
-rw-r--r--src/nvim/tempfile.h8
14 files changed, 144 insertions, 110 deletions
diff --git a/src/nvim/diff.c b/src/nvim/diff.c
index be4a923d14..ab0c80112f 100644
--- a/src/nvim/diff.c
+++ b/src/nvim/diff.c
@@ -30,6 +30,7 @@
#include "nvim/path.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
+#include "nvim/tempfile.h"
#include "nvim/undo.h"
#include "nvim/window.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 7dc95bcded..3459b5c4a0 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -68,6 +68,7 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
+#include "nvim/tempfile.h"
#include "nvim/term.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 05ca5bbb58..384eebe556 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -59,6 +59,7 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
+#include "nvim/tempfile.h"
#include "nvim/term.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c
index 23609ab92f..c867211a66 100644
--- a/src/nvim/fileio.c
+++ b/src/nvim/fileio.c
@@ -49,6 +49,7 @@
#include "nvim/search.h"
#include "nvim/sha256.h"
#include "nvim/strings.h"
+#include "nvim/tempfile.h"
#include "nvim/term.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
@@ -5156,115 +5157,6 @@ void write_lnum_adjust(linenr_T offset)
curbuf->b_no_eol_lnum += offset;
}
-/* Name of Vim's own temp dir. Ends in a slash. */
-static char_u *vim_tempdir = NULL;
-static uint32_t temp_count = 0; /* Temp filename counter. */
-
-/*
- * This will create a directory for private use by this instance of Vim.
- * This is done once, and 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 const char *temp_dirs[] = TEMP_DIR_NAMES;
- int i;
- /*
- * Try the entries in `TEMP_DIR_NAMES` to create the temp directory.
- */
- char_u itmp[TEMP_FILE_PATH_MAXLEN];
- for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) {
- /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */
- expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22);
- if (os_isdir(itmp)) { /* directory exists */
- add_pathsep(itmp);
-
- /* Leave room for filename */
- STRCAT(itmp, "nvimXXXXXX");
- if (os_mkdtemp((char *)itmp) != NULL)
- vim_settempdir(itmp);
- if (vim_tempdir != NULL)
- break;
- }
- }
-}
-
-/*
- * Delete the temp directory and all files it contains.
- */
-void vim_deltempdir(void)
-{
- char_u **files;
- int file_count;
- int i;
-
- if (vim_tempdir != NULL) {
- sprintf((char *)NameBuff, "%s*", vim_tempdir);
- if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
- EW_DIR|EW_FILE|EW_SILENT) == OK) {
- for (i = 0; i < file_count; ++i)
- os_remove((char *)files[i]);
- FreeWild(file_count, files);
- }
- path_tail(NameBuff)[-1] = NUL;
- os_rmdir((char *)NameBuff);
-
- free(vim_tempdir);
- vim_tempdir = NULL;
- }
-}
-
-char_u *vim_gettempdir(void)
-{
- if (vim_tempdir == NULL) {
- vim_maketempdir();
- }
-
- return vim_tempdir;
-}
-
-/*
- * Directory "tempdir" was created. Expand this name to a full path and put
- * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
- * "tempdir" must be no longer than MAXPATHL.
- */
-static void vim_settempdir(char_u *tempdir)
-{
- char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2);
- if (buf) {
- if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
- STRCPY(buf, tempdir);
- add_pathsep(buf);
- vim_tempdir = vim_strsave(buf);
- free(buf);
- }
-}
-
-/*
- * vim_tempname(): Return a unique name that can be used for a temp file.
- *
- * The temp file is NOT created.
- *
- * The returned pointer is to allocated memory.
- * The returned pointer is NULL if no valid name was found.
- */
-char_u *vim_tempname(void)
-{
- char_u itmp[TEMP_FILE_PATH_MAXLEN];
-
- char_u *tempdir = vim_gettempdir();
- if (tempdir != NULL) {
- /* There is no need to check if the file exists, because we own the
- * directory and nobody else creates a file in it. */
- sprintf((char *)itmp, "%s%" PRIu32, tempdir, temp_count++);
- return vim_strsave(itmp);
- }
-
- return NULL;
-}
-
#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
/*
* Convert all backslashes in fname to forward slashes in-place.
diff --git a/src/nvim/hardcopy.c b/src/nvim/hardcopy.c
index 024564f700..64a5879d7d 100644
--- a/src/nvim/hardcopy.c
+++ b/src/nvim/hardcopy.c
@@ -40,6 +40,7 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/term.h"
+#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/if_cscope.c b/src/nvim/if_cscope.c
index 1b7202dbab..9e07a60ee1 100644
--- a/src/nvim/if_cscope.c
+++ b/src/nvim/if_cscope.c
@@ -28,6 +28,7 @@
#include "nvim/quickfix.h"
#include "nvim/strings.h"
#include "nvim/tag.h"
+#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/window.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index c41942301f..a10fc2b8c5 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -69,6 +69,7 @@
#include "nvim/spell.h"
#include "nvim/strings.h"
#include "nvim/term.h"
+#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/window.h"
diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c
index 4e3879ddef..6ba1e68d64 100644
--- a/src/nvim/misc1.c
+++ b/src/nvim/misc1.c
@@ -52,6 +52,7 @@
#include "nvim/strings.h"
#include "nvim/tag.h"
#include "nvim/term.h"
+#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/window.h"
diff --git a/src/nvim/os/server.c b/src/nvim/os/server.c
index 2494dfeb9f..2e8934ecfb 100644
--- a/src/nvim/os/server.c
+++ b/src/nvim/os/server.c
@@ -11,7 +11,7 @@
#include "nvim/vim.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/fileio.h"
+#include "nvim/tempfile.h"
#include "nvim/map.h"
#define MAX_CONNECTIONS 32
diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c
index 239d7d81db..4a0fbf5c18 100644
--- a/src/nvim/os_unix.c
+++ b/src/nvim/os_unix.c
@@ -52,6 +52,7 @@
#include "nvim/screen.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
+#include "nvim/tempfile.h"
#include "nvim/term.h"
#include "nvim/ui.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c
index 60bd15701d..ff2123bac1 100644
--- a/src/nvim/quickfix.c
+++ b/src/nvim/quickfix.c
@@ -47,6 +47,7 @@
#include "nvim/search.h"
#include "nvim/strings.h"
#include "nvim/term.h"
+#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/window.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/spell.c b/src/nvim/spell.c
index 4000235304..8ef67c95f7 100644
--- a/src/nvim/spell.c
+++ b/src/nvim/spell.c
@@ -322,6 +322,7 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/term.h"
+#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/os/os.h"
diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c
new file mode 100644
index 0000000000..a9472bc66a
--- /dev/null
+++ b/src/nvim/tempfile.c
@@ -0,0 +1,124 @@
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "nvim/ascii.h"
+#include "nvim/memory.h"
+#include "nvim/misc1.h"
+#include "nvim/os/os.h"
+#include "nvim/path.h"
+#include "nvim/strings.h"
+#include "nvim/tempfile.h"
+
+#ifdef INCLUDE_GENERATED_DECLARATIONS
+# include "tempfile.c.generated.h"
+#endif
+
+/* Name of Vim's own temp dir. Ends in a slash. */
+static char_u *vim_tempdir = NULL;
+static uint32_t temp_count = 0; /* Temp filename counter. */
+
+/*
+ * This will create a directory for private use by this instance of Vim.
+ * This is done once, and 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 const char *temp_dirs[] = TEMP_DIR_NAMES;
+ int i;
+ /*
+ * Try the entries in `TEMP_DIR_NAMES` to create the temp directory.
+ */
+ char_u itmp[TEMP_FILE_PATH_MAXLEN];
+ for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) {
+ /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */
+ expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22);
+ if (os_isdir(itmp)) { /* directory exists */
+ add_pathsep(itmp);
+
+ /* Leave room for filename */
+ STRCAT(itmp, "nvimXXXXXX");
+ if (os_mkdtemp((char *)itmp) != NULL)
+ vim_settempdir(itmp);
+ if (vim_tempdir != NULL)
+ break;
+ }
+ }
+}
+
+/*
+ * Delete the temp directory and all files it contains.
+ */
+void vim_deltempdir(void)
+{
+ char_u **files;
+ int file_count;
+ int i;
+
+ if (vim_tempdir != NULL) {
+ sprintf((char *)NameBuff, "%s*", vim_tempdir);
+ if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
+ EW_DIR|EW_FILE|EW_SILENT) == OK) {
+ for (i = 0; i < file_count; ++i)
+ os_remove((char *)files[i]);
+ FreeWild(file_count, files);
+ }
+ path_tail(NameBuff)[-1] = NUL;
+ os_rmdir((char *)NameBuff);
+
+ free(vim_tempdir);
+ vim_tempdir = NULL;
+ }
+}
+
+char_u *vim_gettempdir(void)
+{
+ if (vim_tempdir == NULL) {
+ vim_maketempdir();
+ }
+
+ return vim_tempdir;
+}
+
+/*
+ * Directory "tempdir" was created. Expand this name to a full path and put
+ * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
+ * "tempdir" must be no longer than MAXPATHL.
+ */
+static void vim_settempdir(char_u *tempdir)
+{
+ char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2);
+ if (buf) {
+ if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
+ STRCPY(buf, tempdir);
+ add_pathsep(buf);
+ vim_tempdir = vim_strsave(buf);
+ free(buf);
+ }
+}
+
+/*
+ * vim_tempname(): Return a unique name that can be used for a temp file.
+ *
+ * The temp file is NOT created.
+ *
+ * The returned pointer is to allocated memory.
+ * The returned pointer is NULL if no valid name was found.
+ */
+char_u *vim_tempname(void)
+{
+ char_u itmp[TEMP_FILE_PATH_MAXLEN];
+
+ char_u *tempdir = vim_gettempdir();
+ if (tempdir != NULL) {
+ /* There is no need to check if the file exists, because we own the
+ * directory and nobody else creates a file in it. */
+ sprintf((char *)itmp, "%s%" PRIu32, tempdir, temp_count++);
+ return vim_strsave(itmp);
+ }
+
+ return NULL;
+}
diff --git a/src/nvim/tempfile.h b/src/nvim/tempfile.h
new file mode 100644
index 0000000000..c030a70eeb
--- /dev/null
+++ b/src/nvim/tempfile.h
@@ -0,0 +1,8 @@
+#ifndef NVIM_TEMPFILE_H
+#define NVIM_TEMPFILE_H
+
+#ifdef INCLUDE_GENERATED_DECLARATIONS
+# include "tempfile.h.generated.h"
+#endif
+
+#endif // NVIM_TEMPFILE_H