diff options
author | Colin Watson <cjwatson@debian.org> | 2018-08-24 12:49:36 +0100 |
---|---|---|
committer | Colin Watson <cjwatson@debian.org> | 2018-08-30 00:57:27 +0100 |
commit | 816386e17654ca36834bebbf351419e460fad8f6 (patch) | |
tree | 3dc79d831cb73bc25b92f5a4d18f8e328c0c570a /regress/mkdtemp.c | |
parent | 3e6f76c7039d3df22b1d0a3a5f30150efb09b69d (diff) | |
parent | 16a47fc4b04977a14f44dd433c8da1499fa80671 (diff) |
New upstream release (7.8p1)
Closes: #907534
Diffstat (limited to 'regress/mkdtemp.c')
-rw-r--r-- | regress/mkdtemp.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/regress/mkdtemp.c b/regress/mkdtemp.c new file mode 100644 index 000000000..a7be1bdab --- /dev/null +++ b/regress/mkdtemp.c | |||
@@ -0,0 +1,61 @@ | |||
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 <stdarg.h> | ||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <unistd.h> | ||
26 | |||
27 | #include "log.h" | ||
28 | |||
29 | static void | ||
30 | usage(void) | ||
31 | { | ||
32 | fprintf(stderr, "mkdtemp template\n"); | ||
33 | exit(1); | ||
34 | } | ||
35 | |||
36 | int | ||
37 | main(int argc, char **argv) | ||
38 | { | ||
39 | const char *base; | ||
40 | const char *tmpdir; | ||
41 | char template[PATH_MAX]; | ||
42 | int r; | ||
43 | char *dir; | ||
44 | |||
45 | if (argc != 2) | ||
46 | usage(); | ||
47 | base = argv[1]; | ||
48 | |||
49 | if ((tmpdir = getenv("TMPDIR")) == NULL) | ||
50 | tmpdir = "/tmp"; | ||
51 | r = snprintf(template, sizeof(template), "%s/%s", tmpdir, base); | ||
52 | if (r < 0 || (size_t)r >= sizeof(template)) | ||
53 | fatal("template string too long"); | ||
54 | dir = mkdtemp(template); | ||
55 | if (dir == NULL) { | ||
56 | perror("mkdtemp"); | ||
57 | exit(1); | ||
58 | } | ||
59 | puts(dir); | ||
60 | return 0; | ||
61 | } | ||