xref: /titanic_41/usr/src/lib/libcmd/common/mktemp.c (revision a970c705050f9e90c4b6d7982a9b3211719353fb)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 
23 static const char usage[] =
24 "[-?\n@(#)$Id: mktemp (AT&T Research) 2010-03-05 $\n]"
25 USAGE_LICENSE
26 "[+NAME?mktemp - make temporary file or directory]"
27 "[+DESCRIPTION?\bmktemp\b creates a temporary file with optional base "
28     "name prefix \aprefix\a. If \aprefix\a is omitted then \btmp_\b is used "
29     "and \b--tmp\b is implied. If \aprefix\a contains a directory prefix "
30     "then that directory overrides any of the directories described below. A "
31     "temporary file will have mode \brw-------\b and a temporary directory "
32     "will have mode \brwx------\b, subject to \bumask\b(1). Generated paths "
33     "have these attributes:]"
34     "{"
35         "[+*?Lower case to avoid clashes on case ignorant filesystems.]"
36         "[+*?Pseudo-random part to deter denial of service attacks.]"
37         "[+*?Default pseudo-random part (no specific \bX...\b template) "
38             "formatted to accomodate 8.3 filesystems.]"
39     "}"
40 "[+?A consecutive trailing sequence of \bX\b's in \aprefix\a is replaced "
41     "by the pseudo-random part. If there are no \bX\b's then the "
42     "pseudo-random part is appended to the prefix.]"
43 "[d:directory?Create a directory instead of a regular file.]"
44 "[m:mode]:[mode?Set the mode of the created temporary to \amode\a. "
45     "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative modes "
46     "assume an initial mode of \bu=rwx\b.]"
47 "[p:default?Use \adirectory\a if the \bTMPDIR\b environment variable is "
48     "not defined. Implies \b--tmp\b.]:[directory]"
49 "[q:quiet?Suppress file and directory error diagnostics.]"
50 "[R:regress?The pseudo random generator is seeded with \aseed\a instead "
51     "of process/system specific transient data. Use for testing "
52     "only. A seed of \b0\b is silently changed to \b1\b.]#[seed]"
53 "[t:tmp|temporary-directory?Create a path rooted in a temporary "
54     "directory.]"
55 "[u:unsafe|dry-run?Check for file/directory existence but do not create. "
56     "Use this for testing only.]"
57 "\n"
58 "\n[ prefix ]\n"
59 "\n"
60 "[+SEE ALSO?\bmkdir\b(1), \bpathtemp\b(3), \bmktemp\b(3)]"
61 ;
62 
63 #include <cmd.h>
64 #include <ls.h>
65 
66 int
67 b_mktemp(int argc, char** argv, void* context)
68 {
69 	mode_t		mode = 0;
70 	mode_t		mask;
71 	int		fd;
72 	int		i;
73 	int		quiet = 0;
74 	int		unsafe = 0;
75 	int*		fdp = &fd;
76 	char*		dir = "";
77 	char*		pfx;
78 	char*		t;
79 	char		path[PATH_MAX];
80 
81 	cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
82 	for (;;)
83 	{
84 		switch (optget(argv, usage))
85 		{
86 		case 0:
87 			break;
88 		case 'd':
89 			fdp = 0;
90 			continue;
91 		case 'm':
92 			mode = strperm(pfx = opt_info.arg, &opt_info.arg, S_IRWXU);
93 			if (*opt_info.arg)
94 				error(ERROR_exit(0), "%s: invalid mode", pfx);
95 			continue;
96 		case 'p':
97 			if ((t = getenv("TMPDIR")) && *t)
98 				dir = 0;
99 			else
100 				dir = opt_info.arg;
101 			continue;
102 		case 'q':
103 			quiet = 1;
104 			continue;
105 		case 't':
106 			dir = 0;
107 			continue;
108 		case 'u':
109 			unsafe = 1;
110 			fdp = 0;
111 			continue;
112 		case 'R':
113 			if (!pathtemp(NiL, 0, opt_info.arg, "/seed", NiL))
114 				error(2, "%s: regression test initializtion failed", opt_info.arg);
115 			continue;
116 		case ':':
117 			error(2, "%s", opt_info.arg);
118 			continue;
119 		case '?':
120 			error(ERROR_usage(2), "%s", opt_info.arg);
121 			continue;
122 		}
123 		break;
124 	}
125 	argv += opt_info.index;
126 	if (error_info.errors || (pfx = *argv++) && *argv)
127 		error(ERROR_usage(2), "%s", optusage(NiL));
128 	mask = umask(0);
129 	if (!mode)
130 		mode = (fdp ? (S_IRUSR|S_IWUSR) : S_IRWXU) & ~mask;
131 	umask(~mode & (S_IRWXU|S_IRWXG|S_IRWXO));
132 	if (!pfx)
133 	{
134 		pfx = "tmp_";
135 		if (dir && !*dir)
136 			dir = 0;
137 	}
138 	if (t = strrchr(pfx, '/'))
139 	{
140 		i = ++t - pfx;
141 		dir = fmtbuf(i);
142 		memcpy(dir, pfx, i);
143 		dir[i] = 0;
144 		pfx = t;
145 	}
146 	for (;;)
147 	{
148 		if (!pathtemp(path, sizeof(path), dir, pfx, fdp))
149 		{
150 			if (quiet)
151 				error_info.errors++;
152 			else
153 				error(ERROR_SYSTEM|2, "cannot create temporary path");
154 			break;
155 		}
156 		if (fdp || unsafe || !mkdir(path, mode))
157 		{
158 			if (fdp)
159 				close(*fdp);
160 			sfputr(sfstdout, path, '\n');
161 			break;
162 		}
163 		if (sh_checksig(context))
164 		{
165 			error_info.errors++;
166 			break;
167 		}
168 	}
169 	umask(mask);
170 	return error_info.errors != 0;
171 }
172