summaryrefslogtreecommitdiff
path: root/libavutil/file.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2011-10-16 15:21:58 +0200
committerMichael Niedermayer <michaelni@gmx.at>2011-10-16 17:12:37 +0200
commit885158c887384c6da247cc061458f2e53367e6b5 (patch)
treed915138f0a53ec1862a0e7782ade014cd0810957 /libavutil/file.c
parent7f16ec61e17ddc039e68637f1ca27844718312a6 (diff)
downloadffmpeg-885158c887384c6da247cc061458f2e53367e6b5.tar.gz
Move av_tempfile() into libavutil, it is a generically usefull thing and its small.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavutil/file.c')
-rw-r--r--libavutil/file.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/libavutil/file.c b/libavutil/file.c
index 31a3b7564f..4d14292ba1 100644
--- a/libavutil/file.c
+++ b/libavutil/file.c
@@ -130,6 +130,37 @@ void av_file_unmap(uint8_t *bufptr, size_t size)
#endif
}
+int av_tempfile(const char *prefix, char **filename) {
+ int fd=-1;
+#if !HAVE_MKSTEMP
+ *filename = tempnam(".", prefix);
+#else
+ size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
+ *filename = av_malloc(len);
+#endif
+ /* -----common section-----*/
+ if (*filename == NULL) {
+ av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
+ return -1;
+ }
+#if !HAVE_MKSTEMP
+ fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
+#else
+ snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
+ fd = mkstemp(*filename);
+ if (fd < 0) {
+ snprintf(*filename, len, "./%sXXXXXX", prefix);
+ fd = mkstemp(*filename);
+ }
+#endif
+ /* -----common section-----*/
+ if (fd < 0) {
+ av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
+ return -1;
+ }
+ return fd; /* success */
+}
+
#ifdef TEST
#undef printf