blob: 5772ad9ee3f7a854eed3b24b4d0d761b40d3c08d [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;
Rob Landleydfba7412006-03-06 20:47:33 +000015int mktemp_main(int argc, char **argv)
Erik Andersen227a59b2000-04-25 23:24:55 +000016{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000017 unsigned long flags = getopt32(argv, "dqt");
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000018 char *chp;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000019
Rob Landleyb5ca9e02005-12-02 17:54:01 +000020 if (optind + 1 != argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 bb_show_usage();
Rob Landleyb5ca9e02005-12-02 17:54:01 +000022
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000023 chp = argv[optind];
24
25 if (flags & 4) {
26 char *dir = getenv("TMPDIR");
27 if (dir && *dir != '\0')
28 chp = concat_path_file(dir, chp);
29 else
30 chp = concat_path_file("/tmp/", chp);
Rob Landleyb5ca9e02005-12-02 17:54:01 +000031 }
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000032
33 if (flags & 1) {
34 if (mkdtemp(chp) == NULL)
35 return EXIT_FAILURE;
36 } else {
37 if (mkstemp(chp) < 0)
Rob Landleyb5ca9e02005-12-02 17:54:01 +000038 return EXIT_FAILURE;
Glenn L McGrath69f28e72003-04-26 04:56:17 +000039 }
40
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000041 puts(chp);
Glenn L McGrath69f28e72003-04-26 04:56:17 +000042
Matt Kraai3e856ce2000-12-01 02:55:13 +000043 return EXIT_SUCCESS;
Erik Andersen227a59b2000-04-25 23:24:55 +000044}