Mercurial > hg > toybox
view toys/lsb/mktemp.c @ 1692:ed94226a879e draft
Only apply mktemp directory to targets that aren't already paths.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 11 Feb 2015 17:10:28 -0600 |
parents | 9b1cbc13dfdc |
children | 1e0af8b8c8ec |
line wrap: on
line source
/* mktemp.c - Create a temporary file or directory. * * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> * * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html USE_MKTEMP(NEWTOY(mktemp, ">1qd(directory)p(tmpdir):", TOYFLAG_BIN)) config MKTEMP bool "mktemp" default y help usage: mktemp [-dq] [-p DIR] [TEMPLATE] Safely create a new file "DIR/TEMPLATE" and print its name. -d Create directory instead of file (--directory) -p Put new file in DIR (--tmpdir) -q Quiet, no error messages Each X in TEMPLATE is replaced with a random printable character. The default TEMPLATE is tmp.XXXXXX, and the default DIR is $TMPDIR if set, else "/tmp". */ #define FOR_mktemp #include "toys.h" GLOBALS( char *tmpdir; ) void mktemp_main(void) { int d_flag = toys.optflags & FLAG_d; char *template = *toys.optargs; if (!template) template = "tmp.XXXXXX"; if (!TT.tmpdir) TT.tmpdir = getenv("TMPDIR"); if (!TT.tmpdir) TT.tmpdir = "/tmp"; if (!strchr(template, '/')) template = xmprintf("%s/%s", TT.tmpdir, template); if (d_flag ? !mkdtemp(template) : mkstemp(template) == -1) { if (toys.optflags & FLAG_q) toys.exitval = 1; else perror_exit("Failed to create %s %s/%s", d_flag ? "directory" : "file", TT.tmpdir, template); } else xputs(toybuf); }