Mercurial > hg > toybox
annotate 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 |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
1 /* mktemp.c - Create a temporary file or directory. |
575 | 2 * |
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> | |
4 * | |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html |
575 | 6 |
1691
9b1cbc13dfdc
Fix longopts (pointed out by Hyejin Kim).
Rob Landley <rob@landley.net>
parents:
1682
diff
changeset
|
7 USE_MKTEMP(NEWTOY(mktemp, ">1qd(directory)p(tmpdir):", TOYFLAG_BIN)) |
575 | 8 |
9 config MKTEMP | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
10 bool "mktemp" |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
11 default y |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
12 help |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
13 usage: mktemp [-dq] [-p DIR] [TEMPLATE] |
575 | 14 |
1682 | 15 Safely create a new file "DIR/TEMPLATE" and print its name. |
575 | 16 |
1682 | 17 -d Create directory instead of file (--directory) |
18 -p Put new file in DIR (--tmpdir) | |
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". | |
575 | 24 */ |
25 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
660
diff
changeset
|
26 #define FOR_mktemp |
575 | 27 #include "toys.h" |
28 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
660
diff
changeset
|
29 GLOBALS( |
1682 | 30 char *tmpdir; |
575 | 31 ) |
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.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
32 |
575 | 33 void mktemp_main(void) |
34 { | |
1681
4bcfe4cf3e50
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Elliot Hughes <enh@google.com>
parents:
1183
diff
changeset
|
35 int d_flag = toys.optflags & FLAG_d; |
4bcfe4cf3e50
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Elliot Hughes <enh@google.com>
parents:
1183
diff
changeset
|
36 char *template = *toys.optargs; |
575 | 37 |
1682 | 38 if (!template) template = "tmp.XXXXXX"; |
1681
4bcfe4cf3e50
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Elliot Hughes <enh@google.com>
parents:
1183
diff
changeset
|
39 |
4bcfe4cf3e50
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Elliot Hughes <enh@google.com>
parents:
1183
diff
changeset
|
40 if (!TT.tmpdir) TT.tmpdir = getenv("TMPDIR"); |
4bcfe4cf3e50
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Elliot Hughes <enh@google.com>
parents:
1183
diff
changeset
|
41 if (!TT.tmpdir) TT.tmpdir = "/tmp"; |
4bcfe4cf3e50
Use $TMPDIR if set (necessary on Android, where there is no /tmp).
Elliot Hughes <enh@google.com>
parents:
1183
diff
changeset
|
42 |
1692
ed94226a879e
Only apply mktemp directory to targets that aren't already paths.
Rob Landley <rob@landley.net>
parents:
1691
diff
changeset
|
43 if (!strchr(template, '/')) template = xmprintf("%s/%s", TT.tmpdir, template); |
575 | 44 |
1692
ed94226a879e
Only apply mktemp directory to targets that aren't already paths.
Rob Landley <rob@landley.net>
parents:
1691
diff
changeset
|
45 if (d_flag ? !mkdtemp(template) : mkstemp(template) == -1) { |
1682 | 46 if (toys.optflags & FLAG_q) toys.exitval = 1; |
47 else perror_exit("Failed to create %s %s/%s", | |
48 d_flag ? "directory" : "file", TT.tmpdir, template); | |
49 } else xputs(toybuf); | |
575 | 50 } |