comparison toys/lsb/mktemp.c @ 660:760494af5163

mktemp broke kernel build, so new rules: if you don't specify anything, /tmp/tmp.* Specify a file, ./file. Specify -p dir then dir/tmp.*. Specify -p dir and file, dir/file. Also implement -q which lsb wants.
author Rob Landley <rob@landley.net>
date Mon, 03 Sep 2012 21:24:46 -0500
parents 6df4ccc0acbe
children 7e846e281e38
comparison
equal deleted inserted replaced
659:f142928f144a 660:760494af5163
4 * 4 *
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> 5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
6 * 6 *
7 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html 7 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html
8 8
9 USE_MKTEMP(NEWTOY(mktemp, ">1(directory)d(tmpdir)p:", TOYFLAG_BIN)) 9 USE_MKTEMP(NEWTOY(mktemp, ">1q(directory)d(tmpdir)p:", TOYFLAG_BIN))
10 10
11 config MKTEMP 11 config MKTEMP
12 bool "mktemp" 12 bool "mktemp"
13 default y 13 default y
14 help 14 help
15 usage: mktemp [OPTION] [TEMPLATE] 15 usage: mktemp [-dq] [-p DIR] [TEMPLATE]
16 16
17 Safely create a temporary file or directory and print its name. 17 Safely create new file and print its name. Default TEMPLATE is
18 TEMPLATE should end in 6 consecutive X's, the default 18 /tmp/tmp.XXXXXX and each trailing X is replaced with random char.
19 template is tmp.XXXXXX and the default directory is /tmp/.
20
21 -d, --directory Create a directory, instead of a file
22 -p DIR, --tmpdir=DIR Use DIR as a base path
23 19
20 -d, --directory Create directory instead of file
21 -p DIR, --tmpdir=DIR Put new file in DIR
22 -q Quiet
24 */ 23 */
25 24
26 #include "toys.h" 25 #include "toys.h"
27 26
28 DEFINE_GLOBALS( 27 DEFINE_GLOBALS(
29 char * tmpdir; 28 char * tmpdir;
30 ) 29 )
30
31 #define FLAG_p 1
32 #define FLAG_d 2
33 #define FLAG_q 4
34
31 #define TT this.mktemp 35 #define TT this.mktemp
32 36
33 void mktemp_main(void) 37 void mktemp_main(void)
34 { 38 {
35 int d_flag = toys.optflags & 2; 39 int d_flag = toys.optflags & FLAG_d;
36 char *tmp, *path; 40 char *tmp;
37 41
38 tmp = *toys.optargs; 42 tmp = *toys.optargs;
39 if (!tmp) tmp = "tmp.XXXXXX";
40 if (!TT.tmpdir) TT.tmpdir = "/tmp/";
41 43
42 tmp = xmsprintf("%s/%s", TT.tmpdir, tmp); 44 if (!tmp) {
45 if (!TT.tmpdir) TT.tmpdir = "/tmp";
46 tmp = "tmp.xxxxxx";
47 }
48 if (TT.tmpdir) tmp = xmsprintf("%s/%s", TT.tmpdir ? TT.tmpdir : "/tmp",
49 *toys.optargs ? *toys.optargs : "tmp.XXXXXX");
43 50
44 if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1) 51 if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1)
45 perror_exit("Failed to create temporary %s", 52 if (toys.optflags & FLAG_q)
46 d_flag ? "directory" : "file"); 53 perror_exit("Failed to create temporary %s",
54 d_flag ? "directory" : "file");
47 55
48 xputs(path = xrealpath(tmp)); 56 xputs(tmp);
49 57
50 if (CFG_TOYBOX_FREE) { 58 if (CFG_TOYBOX_FREE && TT.tmpdir) free(tmp);
51 free(path);
52 free(tmp);
53 }
54 } 59 }