blob: d47d5a0bfd43e908d8163cf013ffbb4bce139323 [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
Rob Landleydfba7412006-03-06 20:47:33 +000019int mktemp_main(int argc, char **argv)
Erik Andersen227a59b2000-04-25 23:24:55 +000020{
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000021 unsigned long flags = getopt32(argc, argv, "dqt");
22 char *chp;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000023
Rob Landleyb5ca9e02005-12-02 17:54:01 +000024 if (optind + 1 != argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 bb_show_usage();
Rob Landleyb5ca9e02005-12-02 17:54:01 +000026
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000027 chp = argv[optind];
28
29 if (flags & 4) {
30 char *dir = getenv("TMPDIR");
31 if (dir && *dir != '\0')
32 chp = concat_path_file(dir, chp);
33 else
34 chp = concat_path_file("/tmp/", chp);
Rob Landleyb5ca9e02005-12-02 17:54:01 +000035 }
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000036
37 if (flags & 1) {
38 if (mkdtemp(chp) == NULL)
39 return EXIT_FAILURE;
40 } else {
41 if (mkstemp(chp) < 0)
Rob Landleyb5ca9e02005-12-02 17:54:01 +000042 return EXIT_FAILURE;
Glenn L McGrath69f28e72003-04-26 04:56:17 +000043 }
44
Bernhard Reutner-Fischer1ac42bf2006-10-10 15:28:41 +000045 puts(chp);
Glenn L McGrath69f28e72003-04-26 04:56:17 +000046
Matt Kraai3e856ce2000-12-01 02:55:13 +000047 return EXIT_SUCCESS;
Erik Andersen227a59b2000-04-25 23:24:55 +000048}