comparison toys/lsb/mktemp.c @ 1682:abe691083cfe draft

Shameless meddling.
author Rob Landley <rob@landley.net>
date Sat, 07 Feb 2015 19:45:23 -0600
parents 4bcfe4cf3e50
children 9b1cbc13dfdc
comparison
equal deleted inserted replaced
1681:4bcfe4cf3e50 1682:abe691083cfe
10 bool "mktemp" 10 bool "mktemp"
11 default y 11 default y
12 help 12 help
13 usage: mktemp [-dq] [-p DIR] [TEMPLATE] 13 usage: mktemp [-dq] [-p DIR] [TEMPLATE]
14 14
15 Safely create a new file and print its name. The default TEMPLATE is 15 Safely create a new file "DIR/TEMPLATE" and print its name.
16 tmp.XXXXXX. The default DIR is $TMPDIR, or /tmp if $TMPDIR is not set.
17 16
18 -d, --directory Create directory instead of file 17 -d Create directory instead of file (--directory)
19 -p DIR, --tmpdir=DIR Put new file in DIR 18 -p Put new file in DIR (--tmpdir)
20 -q Quiet 19 -q Quiet, no error messages
20
21 Each X in TEMPLATE is replaced with a random printable character. The
22 default TEMPLATE is tmp.XXXXXX, and the default DIR is $TMPDIR if set,
23 else "/tmp".
21 */ 24 */
22 25
23 #define FOR_mktemp 26 #define FOR_mktemp
24 #include "toys.h" 27 #include "toys.h"
25 28
26 GLOBALS( 29 GLOBALS(
27 char * tmpdir; 30 char *tmpdir;
28 ) 31 )
29 32
30 void mktemp_main(void) 33 void mktemp_main(void)
31 { 34 {
32 int d_flag = toys.optflags & FLAG_d; 35 int d_flag = toys.optflags & FLAG_d;
33 char *template = *toys.optargs; 36 char *template = *toys.optargs;
34 int success;
35 37
36 if (!template) { 38 if (!template) template = "tmp.XXXXXX";
37 template = "tmp.XXXXXX";
38 }
39 39
40 if (!TT.tmpdir) TT.tmpdir = getenv("TMPDIR"); 40 if (!TT.tmpdir) TT.tmpdir = getenv("TMPDIR");
41 if (!TT.tmpdir) TT.tmpdir = "/tmp"; 41 if (!TT.tmpdir) TT.tmpdir = "/tmp";
42 42
43 snprintf(toybuf, sizeof(toybuf), "%s/%s", TT.tmpdir, template); 43 snprintf(toybuf, sizeof(toybuf), "%s/%s", TT.tmpdir, template);
44 44
45 if (d_flag ? mkdtemp(toybuf) == NULL : mkstemp(toybuf) == -1) { 45 if (d_flag ? !mkdtemp(toybuf) : mkstemp(toybuf) == -1) {
46 if (toys.optflags & FLAG_q) { 46 if (toys.optflags & FLAG_q) toys.exitval = 1;
47 toys.exitval = 1; 47 else perror_exit("Failed to create %s %s/%s",
48 } else { 48 d_flag ? "directory" : "file", TT.tmpdir, template);
49 perror_exit("Failed to create temporary %s with template %s/%s", 49 } else xputs(toybuf);
50 d_flag ? "directory" : "file", TT.tmpdir, template);
51 }
52 }
53
54 xputs(toybuf);
55 } 50 }