blob: b011fc10c8fb3431a8f83545398a4695bc57d933 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Erik Andersen227a59b2000-04-25 23:24:55 +000013
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000015int mktemp_main(int argc ATTRIBUTE_UNUSED, char **argv)
Erik Andersen227a59b2000-04-25 23:24:55 +000016{
Denis Vlasenko65581f32008-02-09 06:26:53 +000017 // -d Make a directory instead of a file
18 // -q Fail silently if an error occurs [bbox: ignored]
19 // -t Generate a path rooted in temporary directory
20 // -p DIR Use DIR as a temporary directory (implies -t)
21 const char *path;
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000022 char *chp;
Denis Vlasenko65581f32008-02-09 06:26:53 +000023 unsigned flags;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000024
Denis Vlasenko65581f32008-02-09 06:26:53 +000025 opt_complementary = "=1"; /* exactly one arg */
26 flags = getopt32(argv, "dqtp:", &path);
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000027 chp = argv[optind];
28
Denis Vlasenko65581f32008-02-09 06:26:53 +000029 if (flags & (4|8)) { /* -t and/or -p */
30 const char *dir = getenv("TMPDIR");
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000031 if (dir && *dir != '\0')
Denis Vlasenko65581f32008-02-09 06:26:53 +000032 path = dir;
33 else if (!(flags & 8)) /* No -p */
34 path = "/tmp/";
35 /* else path comes from -p DIR */
36 chp = concat_path_file(path, chp);
Rob Landleyb5ca9e02005-12-02 17:54:01 +000037 }
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000038
Denis Vlasenko65581f32008-02-09 06:26:53 +000039 if (flags & 1) { /* -d */
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000040 if (mkdtemp(chp) == NULL)
41 return EXIT_FAILURE;
42 } else {
43 if (mkstemp(chp) < 0)
Rob Landleyb5ca9e02005-12-02 17:54:01 +000044 return EXIT_FAILURE;
Glenn L McGrath69f28e72003-04-26 04:56:17 +000045 }
46
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000047 puts(chp);
Glenn L McGrath69f28e72003-04-26 04:56:17 +000048
Matt Kraai3e856ce2000-12-01 02:55:13 +000049 return EXIT_SUCCESS;
Erik Andersen227a59b2000-04-25 23:24:55 +000050}