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