diff options
| author | Elie De Brauwer <eliedebrauwer@gmail.com> | 2012-05-01 11:45:45 +0200 | 
|---|---|---|
| committer | Elie De Brauwer <eliedebrauwer@gmail.com> | 2012-05-01 11:45:45 +0200 | 
| commit | d6f7e788ac34c05f7be31a8eb751b7595d48ec0a (patch) | |
| tree | b8d96db440127bbed09ed421c76439dce309f98d | |
| parent | e3b171e6df244be8f7d5163ec5818406cce513f0 (diff) | |
| download | toybox-d6f7e788ac34c05f7be31a8eb751b7595d48ec0a.tar.gz | |
Addition of mktemp
| -rw-r--r-- | toys/mktemp.c | 56 | 
1 files changed, 56 insertions, 0 deletions
| diff --git a/toys/mktemp.c b/toys/mktemp.c new file mode 100644 index 00000000..91caf20e --- /dev/null +++ b/toys/mktemp.c @@ -0,0 +1,56 @@ +/* vi: set sw=4 ts=4: + * + * mktemp.c - Create a temporary file or directory. + * + * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> + * + * Not in SUSv4. + +USE_MKTEMP(NEWTOY(mktemp, ">1(directory)d(tmpdir)p:", TOYFLAG_BIN)) + +config MKTEMP +	bool "mktemp" +	default y +	help +	  usage: mktemp [OPTION] [TEMPLATE] + +	  Safely create a temporary file or directory and print its name. +	  TEMPLATE should end in 6 consecutive X's, the default +	  template is tmp.XXXXXX and the default directory is /tmp/. +	  -d, --directory        Create a directory, instead of a file +	  -p DIR, --tmpdir=DIR   Use DIR as a base path + +*/ + +#include "toys.h" + +DEFINE_GLOBALS( +	char * tmpdir; +) +#define TT this.mktemp + +void mktemp_main(void) +{ +	int  p_flag = (toys.optflags & 1); +	int  d_flag = (toys.optflags & 2) >> 1; +	char * result; + +	int size = snprintf(toybuf, sizeof(toybuf)-1, "%s/%s", +			(p_flag && TT.tmpdir)?TT.tmpdir:"/tmp/", +			(toys.optargs[0])?toys.optargs[0]:"tmp.XXXXXX"); +	toybuf[size] = 0; + +	if (d_flag) { +		if (mkdtemp(toybuf) == NULL) +			perror_exit("Failed to create temporary directory"); +	} else { +		if (mkstemp(toybuf) == -1) +			perror_exit("Failed to create temporary file"); +	} + +	result = realpath(toybuf, NULL); +	xputs(result); + +	if (CFG_TOYBOX_FREE) +		free(result); +} | 
