From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1eeFkr-0005TR-VG for barebox@lists.infradead.org; Wed, 24 Jan 2018 07:46:01 +0000 From: Sascha Hauer Date: Wed, 24 Jan 2018 08:45:30 +0100 Message-Id: <20180124074534.7966-4-s.hauer@pengutronix.de> In-Reply-To: <20180124074534.7966-1-s.hauer@pengutronix.de> References: <20180124074534.7966-1-s.hauer@pengutronix.de> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH 3/7] libfile: implement make_temp To: Barebox List Create a make_temp() function which creates a name for a temporary file. Since we do not have any concurrency in barebox we do not need to create the file right away and can leave that to the caller. Unlike unix mktemp the resulting filename is dynamically allocated and must be freed by the caller. Signed-off-by: Sascha Hauer --- include/libfile.h | 2 ++ lib/libfile.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/libfile.h b/include/libfile.h index dd0b00f988..6dbb81a241 100644 --- a/include/libfile.h +++ b/include/libfile.h @@ -26,4 +26,6 @@ int make_directory(const char *pathname); int unlink_recursive(const char *path, char **failedpath); +char *make_temp(const char *template); + #endif /* __LIBFILE_H */ diff --git a/lib/libfile.c b/lib/libfile.c index 6b70306dbd..79054eb5ac 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -20,6 +20,7 @@ #include #include #include +#include #include /* @@ -485,3 +486,29 @@ int open_and_lseek(const char *filename, int mode, loff_t pos) return fd; } + +/** + * make_temp - create a name for a temporary file + * @template: The filename prefix + * + * This function creates a name for a temporary file. @template is used as a + * template for the name which gets appended a 8-digit hexadecimal number to + * create a unique filename. + * + * Return: This function returns a filename which can be used as a temporary + * file lateron. The returned filename must be freed by the caller. + */ +char *make_temp(const char *template) +{ + char *name = NULL; + struct stat s; + int ret; + + do { + free(name); + name = basprintf("/tmp/%s-%08x", template, random32()); + ret = stat(name, &s); + } while (!ret); + + return name; +} -- 2.11.0 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox