diff options
Diffstat (limited to 'regress/mkdtemp.c')
-rw-r--r-- | regress/mkdtemp.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/regress/mkdtemp.c b/regress/mkdtemp.c new file mode 100644 index 000000000..8c7d2e219 --- /dev/null +++ b/regress/mkdtemp.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017 Colin Watson <cjwatson@debian.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | /* Roughly equivalent to "mktemp -d -t TEMPLATE", but portable. */ | ||
18 | |||
19 | #include "includes.h" | ||
20 | |||
21 | #include <limits.h> | ||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | |||
25 | #include "log.h" | ||
26 | |||
27 | static void | ||
28 | usage(void) | ||
29 | { | ||
30 | fprintf(stderr, "mkdtemp template\n"); | ||
31 | exit(1); | ||
32 | } | ||
33 | |||
34 | int | ||
35 | main(int argc, char **argv) | ||
36 | { | ||
37 | const char *base; | ||
38 | const char *tmpdir; | ||
39 | char template[PATH_MAX]; | ||
40 | int r; | ||
41 | char *dir; | ||
42 | |||
43 | if (argc != 2) | ||
44 | usage(); | ||
45 | base = argv[1]; | ||
46 | |||
47 | if ((tmpdir = getenv("TMPDIR")) == NULL) | ||
48 | tmpdir = "/tmp"; | ||
49 | r = snprintf(template, sizeof(template), "%s/%s", tmpdir, base); | ||
50 | if (r < 0 || (size_t)r >= sizeof(template)) | ||
51 | fatal("template string too long"); | ||
52 | dir = mkdtemp(template); | ||
53 | if (dir == NULL) { | ||
54 | perror("mkdtemp"); | ||
55 | exit(1); | ||
56 | } | ||
57 | puts(dir); | ||
58 | return 0; | ||
59 | } | ||