aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/support/env_posix.c3
-rw-r--r--src/core/support/env_win32.c3
-rw-r--r--src/core/support/file.h10
-rw-r--r--src/core/support/file_posix.c25
-rw-r--r--src/core/support/file_win32.c26
5 files changed, 51 insertions, 16 deletions
diff --git a/src/core/support/env_posix.c b/src/core/support/env_posix.c
index 4af5aea9b0..79b8fcd7d7 100644
--- a/src/core/support/env_posix.c
+++ b/src/core/support/env_posix.c
@@ -54,7 +54,8 @@ char *gpr_getenv(const char *name) {
}
void gpr_setenv(const char *name, const char *value) {
- GPR_ASSERT(setenv(name, value, 1) == 0);
+ int res = setenv(name, value, 1);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_POSIX_ENV */
diff --git a/src/core/support/env_win32.c b/src/core/support/env_win32.c
index 7964409364..a31fa79d68 100644
--- a/src/core/support/env_win32.c
+++ b/src/core/support/env_win32.c
@@ -53,7 +53,8 @@ char *gpr_getenv(const char *name) {
}
void gpr_setenv(const char *name, const char *value) {
- GPR_ASSERT(_putenv_s(name, value) == 0);
+ errno_t res = _putenv_s(name, value);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_WIN32 */
diff --git a/src/core/support/file.h b/src/core/support/file.h
index a9d81498e5..92f420e7ce 100644
--- a/src/core/support/file.h
+++ b/src/core/support/file.h
@@ -48,11 +48,11 @@ extern "C" {
will be set to 1 in case of success and 0 in case of failure. */
gpr_slice gpr_load_file(const char *filename, int *success);
-/* Creates a temporary file from a template.
- The last six characters of template must be "XXXXXX" and these are replaced
- with a string that makes the filename unique. Since it will be modified,
- template must not be a string constant. */
-FILE *gpr_tmpfile(char *template);
+/* Creates a temporary file from a prefix.
+ If tmp_filename is not NULL, *tmp_filename is assigned the name of the
+ created file and it is the responsibility of the caller to gpr_free it
+ unless an error occurs in which case it will be set to NULL. */
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename);
#ifdef __cplusplus
}
diff --git a/src/core/support/file_posix.c b/src/core/support/file_posix.c
index a156e7703c..a763fbcda2 100644
--- a/src/core/support/file_posix.c
+++ b/src/core/support/file_posix.c
@@ -54,15 +54,26 @@
#include <string.h>
#include <unistd.h>
+#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-FILE *gpr_tmpfile(char *template) {
+#include "src/core/support/string.h"
+
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) {
FILE *result = NULL;
- int fd = mkstemp(template);
+ char *template;
+ int fd;
+
+ if (tmp_filename != NULL) *tmp_filename = NULL;
+
+ gpr_asprintf(&template, "%s_XXXXXX", prefix);
+ GPR_ASSERT(template != NULL);
+
+ fd = mkstemp(template);
if (fd == -1) {
gpr_log(GPR_ERROR, "mkstemp failed for template %s with error %s.",
template, strerror(errno));
- return NULL;
+ goto end;
}
result = fdopen(fd, "w+");
if (result == NULL) {
@@ -70,6 +81,14 @@ FILE *gpr_tmpfile(char *template) {
template, fd, strerror(errno));
unlink(template);
close(fd);
+ goto end;
+ }
+
+end:
+ if (result != NULL && tmp_filename != NULL) {
+ *tmp_filename = template;
+ } else {
+ gpr_free(template);
}
return result;
}
diff --git a/src/core/support/file_win32.c b/src/core/support/file_win32.c
index b85422c372..d415281e0d 100644
--- a/src/core/support/file_win32.c
+++ b/src/core/support/file_win32.c
@@ -41,24 +41,38 @@
#include <stdio.h>
#include <string.h>
+#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-FILE *gpr_tmpfile(char *template) {
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) {
FILE *result = NULL;
+ char *template;
+
+ if (tmp_filename != NULL) *tmp_filename = NULL;
+
+ gpr_asprintf(&template, "%s_XXXXXX", prefix);
+ GPR_ASSERT(template != NULL);
/* _mktemp_s can only create a maximum of 26 file names for any combination of
base and template values which is kind of sad... We may revisit this
function later to have something better... */
if (_mktemp_s(template, strlen(template) + 1) != 0) {
gpr_log(LOG_ERROR, "Could not create tmp file.");
- return NULL;
+ goto end;
}
- if (fopen_s(&result, template, "wb+") == 0) {
- return result;
- } else {
+ if (fopen_s(&result, template, "wb+") != 0) {
gpr_log(GPR_ERROR, "Could not open file %s", template);
- return NULL;
+ result = NULL;
+ goto end;
+ }
+
+end:
+ if (result != NULL && tmp_filename != NULL) {
+ *tmp_filename = template;
+ } else {
+ gpr_free(template);
}
+ return result;
}
#endif /* GPR_WIN32 */