Mercurial > hg > toybox
annotate 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 |
rev | line source |
---|---|
575 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * mktemp.c - Create a temporary file or directory. | |
4 * | |
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> | |
6 * | |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mktemp.html |
575 | 8 |
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
|
9 USE_MKTEMP(NEWTOY(mktemp, ">1q(directory)d(tmpdir)p:", TOYFLAG_BIN)) |
575 | 10 |
11 config MKTEMP | |
12 bool "mktemp" | |
13 default y | |
14 help | |
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
|
15 usage: mktemp [-dq] [-p DIR] [TEMPLATE] |
575 | 16 |
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
|
17 Safely create new file and print its name. Default TEMPLATE is |
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
|
18 /tmp/tmp.XXXXXX and each trailing X is replaced with random char. |
575 | 19 |
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
|
20 -d, --directory Create directory instead of file |
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
|
21 -p DIR, --tmpdir=DIR Put new file in DIR |
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
|
22 -q Quiet |
575 | 23 */ |
24 | |
25 #include "toys.h" | |
26 | |
27 DEFINE_GLOBALS( | |
28 char * tmpdir; | |
29 ) | |
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
|
30 |
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
|
31 #define FLAG_p 1 |
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 #define FLAG_d 2 |
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
|
33 #define FLAG_q 4 |
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
|
34 |
575 | 35 #define TT this.mktemp |
36 | |
37 void mktemp_main(void) | |
38 { | |
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
|
39 int d_flag = toys.optflags & FLAG_d; |
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
|
40 char *tmp; |
575 | 41 |
586
0353ed084559
Convert mktemp to use xrealpath, and general clean up while there.
Rob Landley <rob@landley.net>
parents:
575
diff
changeset
|
42 tmp = *toys.optargs; |
0353ed084559
Convert mktemp to use xrealpath, and general clean up while there.
Rob Landley <rob@landley.net>
parents:
575
diff
changeset
|
43 |
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
|
44 if (!tmp) { |
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
|
45 if (!TT.tmpdir) TT.tmpdir = "/tmp"; |
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
|
46 tmp = "tmp.xxxxxx"; |
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
|
47 } |
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
|
48 if (TT.tmpdir) tmp = xmsprintf("%s/%s", TT.tmpdir ? TT.tmpdir : "/tmp", |
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
|
49 *toys.optargs ? *toys.optargs : "tmp.XXXXXX"); |
575 | 50 |
586
0353ed084559
Convert mktemp to use xrealpath, and general clean up while there.
Rob Landley <rob@landley.net>
parents:
575
diff
changeset
|
51 if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1) |
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
|
52 if (toys.optflags & FLAG_q) |
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
|
53 perror_exit("Failed to create temporary %s", |
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
|
54 d_flag ? "directory" : "file"); |
586
0353ed084559
Convert mktemp to use xrealpath, and general clean up while there.
Rob Landley <rob@landley.net>
parents:
575
diff
changeset
|
55 |
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
|
56 xputs(tmp); |
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
|
57 |
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
|
58 if (CFG_TOYBOX_FREE && TT.tmpdir) free(tmp); |
575 | 59 } |