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