view toys/lsb/mktemp.c @ 1539:3e85af1f7e22 draft

First batch of sed tests. Only good for TEST_HOST=1 at the moment because the test infrastructure itself depends on sed, so if an unfinished sed is in the $PATH it goes boing. But hey, corner cases! I have... more.
author Rob Landley <>
date Wed, 29 Oct 2014 18:44:33 -0500
parents 0752b2d58909
children 4bcfe4cf3e50
line wrap: on
line source

/* mktemp.c - Create a temporary file or directory.
 * Copyright 2012 Elie De Brauwer <>

USE_MKTEMP(NEWTOY(mktemp, ">1q(directory)d(tmpdir)p:", TOYFLAG_BIN))

config MKTEMP
  bool "mktemp"
  default y
    usage: mktemp [-dq] [-p DIR] [TEMPLATE]

    Safely create new file and print its name. Default TEMPLATE is
    /tmp/tmp.XXXXXX and each trailing X is replaced with random char.

    -d, --directory        Create directory instead of file
    -p DIR, --tmpdir=DIR   Put new file in DIR
    -q                     Quiet

#define FOR_mktemp
#include "toys.h"

  char * tmpdir;

void mktemp_main(void)
  int  d_flag = toys.optflags & FLAG_d;
  char *tmp;

  tmp = *toys.optargs;

  if (!tmp) {
    if (!TT.tmpdir) TT.tmpdir = "/tmp";
    tmp = "tmp.xxxxxx";
  if (TT.tmpdir) tmp = xmprintf("%s/%s", TT.tmpdir ? TT.tmpdir : "/tmp",
    *toys.optargs ? *toys.optargs : "tmp.XXXXXX");

  if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1)
    if (toys.optflags & FLAG_q)
      perror_exit("Failed to create temporary %s",
        d_flag ? "directory" : "file");


  if (CFG_TOYBOX_FREE && TT.tmpdir) free(tmp);