blob: ce5bda515fd14f7348fa77870176eaef3c7ec8b4 [file] [log] [blame]
Erik Andersen227a59b2000-04-25 23:24:55 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini mktemp implementation for busybox
4 *
5 *
6 * Copyright (C) 2000 by Daniel Jacobowitz
7 * Written by Daniel Jacobowitz <dan@debian.org>
8 *
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00009 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Erik Andersen227a59b2000-04-25 23:24:55 +000010 */
11
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000012#include "busybox.h"
Erik Andersen227a59b2000-04-25 23:24:55 +000013#include <stdio.h>
14#include <errno.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000015#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000016#include <unistd.h>
17#include <stdlib.h>
Erik Andersen227a59b2000-04-25 23:24:55 +000018
Denis Vlasenko06af2162007-02-03 17:28:39 +000019int mktemp_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000020int mktemp_main(int argc, char **argv)
Erik Andersen227a59b2000-04-25 23:24:55 +000021{
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000022 unsigned long flags = getopt32(argc, argv, "dqt");
23 char *chp;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000024
Rob Landleyb5ca9e02005-12-02 17:54:01 +000025 if (optind + 1 != argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 bb_show_usage();
Rob Landleyb5ca9e02005-12-02 17:54:01 +000027
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000028 chp = argv[optind];
29
30 if (flags & 4) {
31 char *dir = getenv("TMPDIR");
32 if (dir && *dir != '\0')
33 chp = concat_path_file(dir, chp);
34 else
35 chp = concat_path_file("/tmp/", chp);
Rob Landleyb5ca9e02005-12-02 17:54:01 +000036 }
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000037
38 if (flags & 1) {
39 if (mkdtemp(chp) == NULL)
40 return EXIT_FAILURE;
41 } else {
42 if (mkstemp(chp) < 0)
Rob Landleyb5ca9e02005-12-02 17:54:01 +000043 return EXIT_FAILURE;
Glenn L McGrath69f28e72003-04-26 04:56:17 +000044 }
45
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000046 puts(chp);
Glenn L McGrath69f28e72003-04-26 04:56:17 +000047
Matt Kraai3e856ce2000-12-01 02:55:13 +000048 return EXIT_SUCCESS;
Erik Andersen227a59b2000-04-25 23:24:55 +000049}