xref: /titanic_41/usr/src/lib/libcmd/common/mktemp.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
134f9b3eeSRoland Mainz /***********************************************************************
234f9b3eeSRoland Mainz *                                                                      *
334f9b3eeSRoland Mainz *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
534f9b3eeSRoland Mainz *                      and is licensed under the                       *
634f9b3eeSRoland Mainz *                  Common Public License, Version 1.0                  *
734f9b3eeSRoland Mainz *                    by AT&T Intellectual Property                     *
834f9b3eeSRoland Mainz *                                                                      *
934f9b3eeSRoland Mainz *                A copy of the License is available at                 *
1034f9b3eeSRoland Mainz *            http://www.opensource.org/licenses/cpl1.0.txt             *
1134f9b3eeSRoland Mainz *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
1234f9b3eeSRoland Mainz *                                                                      *
1334f9b3eeSRoland Mainz *              Information and Software Systems Research               *
1434f9b3eeSRoland Mainz *                            AT&T Research                             *
1534f9b3eeSRoland Mainz *                           Florham Park NJ                            *
1634f9b3eeSRoland Mainz *                                                                      *
1734f9b3eeSRoland Mainz *                 Glenn Fowler <gsf@research.att.com>                  *
1834f9b3eeSRoland Mainz *                  David Korn <dgk@research.att.com>                   *
1934f9b3eeSRoland Mainz *                                                                      *
2034f9b3eeSRoland Mainz ***********************************************************************/
2134f9b3eeSRoland Mainz #pragma prototyped
2234f9b3eeSRoland Mainz 
2334f9b3eeSRoland Mainz static const char usage[] =
24*3e14f97fSRoger A. Faulkner "[-?\n@(#)$Id: mktemp (AT&T Research) 2010-03-05 $\n]"
2534f9b3eeSRoland Mainz USAGE_LICENSE
2634f9b3eeSRoland Mainz "[+NAME?mktemp - make temporary file or directory]"
2734f9b3eeSRoland Mainz "[+DESCRIPTION?\bmktemp\b creates a temporary file with optional base "
2834f9b3eeSRoland Mainz     "name prefix \aprefix\a. If \aprefix\a is omitted then \btmp_\b is used "
2934f9b3eeSRoland Mainz     "and \b--tmp\b is implied. If \aprefix\a contains a directory prefix "
3034f9b3eeSRoland Mainz     "then that directory overrides any of the directories described below. A "
3134f9b3eeSRoland Mainz     "temporary file will have mode \brw-------\b and a temporary directory "
3234f9b3eeSRoland Mainz     "will have mode \brwx------\b, subject to \bumask\b(1). Generated paths "
3334f9b3eeSRoland Mainz     "have these attributes:]"
3434f9b3eeSRoland Mainz     "{"
3534f9b3eeSRoland Mainz         "[+*?Lower case to avoid clashes on case ignorant filesystems.]"
3634f9b3eeSRoland Mainz         "[+*?Pseudo-random part to deter denial of service attacks.]"
37*3e14f97fSRoger A. Faulkner         "[+*?Default pseudo-random part (no specific \bX...\b template) "
38*3e14f97fSRoger A. Faulkner             "formatted to accomodate 8.3 filesystems.]"
3934f9b3eeSRoland Mainz     "}"
40*3e14f97fSRoger A. Faulkner "[+?A consecutive trailing sequence of \bX\b's in \aprefix\a is replaced "
41*3e14f97fSRoger A. Faulkner     "by the pseudo-random part. If there are no \bX\b's then the "
42*3e14f97fSRoger A. Faulkner     "pseudo-random part is appended to the prefix.]"
4334f9b3eeSRoland Mainz "[d:directory?Create a directory instead of a regular file.]"
4434f9b3eeSRoland Mainz "[m:mode]:[mode?Set the mode of the created temporary to \amode\a. "
4534f9b3eeSRoland Mainz     "\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative modes "
4634f9b3eeSRoland Mainz     "assume an initial mode of \bu=rwx\b.]"
4734f9b3eeSRoland Mainz "[p:default?Use \adirectory\a if the \bTMPDIR\b environment variable is "
4834f9b3eeSRoland Mainz     "not defined. Implies \b--tmp\b.]:[directory]"
4934f9b3eeSRoland Mainz "[q:quiet?Suppress file and directory error diagnostics.]"
50*3e14f97fSRoger A. Faulkner "[R:regress?The pseudo random generator is seeded with \aseed\a instead "
51*3e14f97fSRoger A. Faulkner     "of process/system specific transient data. Use for testing "
52*3e14f97fSRoger A. Faulkner     "only. A seed of \b0\b is silently changed to \b1\b.]#[seed]"
5334f9b3eeSRoland Mainz "[t:tmp|temporary-directory?Create a path rooted in a temporary "
5434f9b3eeSRoland Mainz     "directory.]"
5534f9b3eeSRoland Mainz "[u:unsafe|dry-run?Check for file/directory existence but do not create. "
56*3e14f97fSRoger A. Faulkner     "Use this for testing only.]"
5734f9b3eeSRoland Mainz "\n"
5834f9b3eeSRoland Mainz "\n[ prefix ]\n"
5934f9b3eeSRoland Mainz "\n"
6034f9b3eeSRoland Mainz "[+SEE ALSO?\bmkdir\b(1), \bpathtemp\b(3), \bmktemp\b(3)]"
6134f9b3eeSRoland Mainz ;
6234f9b3eeSRoland Mainz 
6334f9b3eeSRoland Mainz #include <cmd.h>
6434f9b3eeSRoland Mainz #include <ls.h>
6534f9b3eeSRoland Mainz 
6634f9b3eeSRoland Mainz int
b_mktemp(int argc,char ** argv,void * context)6734f9b3eeSRoland Mainz b_mktemp(int argc, char** argv, void* context)
6834f9b3eeSRoland Mainz {
6934f9b3eeSRoland Mainz 	mode_t		mode = 0;
7034f9b3eeSRoland Mainz 	mode_t		mask;
7134f9b3eeSRoland Mainz 	int		fd;
7234f9b3eeSRoland Mainz 	int		i;
7334f9b3eeSRoland Mainz 	int		quiet = 0;
7434f9b3eeSRoland Mainz 	int		unsafe = 0;
7534f9b3eeSRoland Mainz 	int*		fdp = &fd;
7634f9b3eeSRoland Mainz 	char*		dir = "";
7734f9b3eeSRoland Mainz 	char*		pfx;
7834f9b3eeSRoland Mainz 	char*		t;
7934f9b3eeSRoland Mainz 	char		path[PATH_MAX];
8034f9b3eeSRoland Mainz 
8134f9b3eeSRoland Mainz 	cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
8234f9b3eeSRoland Mainz 	for (;;)
8334f9b3eeSRoland Mainz 	{
8434f9b3eeSRoland Mainz 		switch (optget(argv, usage))
8534f9b3eeSRoland Mainz 		{
8634f9b3eeSRoland Mainz 		case 0:
8734f9b3eeSRoland Mainz 			break;
8834f9b3eeSRoland Mainz 		case 'd':
8934f9b3eeSRoland Mainz 			fdp = 0;
9034f9b3eeSRoland Mainz 			continue;
9134f9b3eeSRoland Mainz 		case 'm':
9234f9b3eeSRoland Mainz 			mode = strperm(pfx = opt_info.arg, &opt_info.arg, S_IRWXU);
9334f9b3eeSRoland Mainz 			if (*opt_info.arg)
9434f9b3eeSRoland Mainz 				error(ERROR_exit(0), "%s: invalid mode", pfx);
9534f9b3eeSRoland Mainz 			continue;
9634f9b3eeSRoland Mainz 		case 'p':
9734f9b3eeSRoland Mainz 			if ((t = getenv("TMPDIR")) && *t)
9834f9b3eeSRoland Mainz 				dir = 0;
9934f9b3eeSRoland Mainz 			else
10034f9b3eeSRoland Mainz 				dir = opt_info.arg;
10134f9b3eeSRoland Mainz 			continue;
10234f9b3eeSRoland Mainz 		case 'q':
10334f9b3eeSRoland Mainz 			quiet = 1;
10434f9b3eeSRoland Mainz 			continue;
10534f9b3eeSRoland Mainz 		case 't':
10634f9b3eeSRoland Mainz 			dir = 0;
10734f9b3eeSRoland Mainz 			continue;
10834f9b3eeSRoland Mainz 		case 'u':
10934f9b3eeSRoland Mainz 			unsafe = 1;
11034f9b3eeSRoland Mainz 			fdp = 0;
11134f9b3eeSRoland Mainz 			continue;
112*3e14f97fSRoger A. Faulkner 		case 'R':
113*3e14f97fSRoger A. Faulkner 			if (!pathtemp(NiL, 0, opt_info.arg, "/seed", NiL))
114*3e14f97fSRoger A. Faulkner 				error(2, "%s: regression test initializtion failed", opt_info.arg);
115*3e14f97fSRoger A. Faulkner 			continue;
11634f9b3eeSRoland Mainz 		case ':':
11734f9b3eeSRoland Mainz 			error(2, "%s", opt_info.arg);
11834f9b3eeSRoland Mainz 			continue;
11934f9b3eeSRoland Mainz 		case '?':
12034f9b3eeSRoland Mainz 			error(ERROR_usage(2), "%s", opt_info.arg);
12134f9b3eeSRoland Mainz 			continue;
12234f9b3eeSRoland Mainz 		}
12334f9b3eeSRoland Mainz 		break;
12434f9b3eeSRoland Mainz 	}
12534f9b3eeSRoland Mainz 	argv += opt_info.index;
12634f9b3eeSRoland Mainz 	if (error_info.errors || (pfx = *argv++) && *argv)
12734f9b3eeSRoland Mainz 		error(ERROR_usage(2), "%s", optusage(NiL));
12834f9b3eeSRoland Mainz 	mask = umask(0);
12934f9b3eeSRoland Mainz 	if (!mode)
13034f9b3eeSRoland Mainz 		mode = (fdp ? (S_IRUSR|S_IWUSR) : S_IRWXU) & ~mask;
13134f9b3eeSRoland Mainz 	umask(~mode & (S_IRWXU|S_IRWXG|S_IRWXO));
13234f9b3eeSRoland Mainz 	if (!pfx)
13334f9b3eeSRoland Mainz 	{
13434f9b3eeSRoland Mainz 		pfx = "tmp_";
13534f9b3eeSRoland Mainz 		if (dir && !*dir)
13634f9b3eeSRoland Mainz 			dir = 0;
13734f9b3eeSRoland Mainz 	}
13834f9b3eeSRoland Mainz 	if (t = strrchr(pfx, '/'))
13934f9b3eeSRoland Mainz 	{
14034f9b3eeSRoland Mainz 		i = ++t - pfx;
14134f9b3eeSRoland Mainz 		dir = fmtbuf(i);
14234f9b3eeSRoland Mainz 		memcpy(dir, pfx, i);
14334f9b3eeSRoland Mainz 		dir[i] = 0;
14434f9b3eeSRoland Mainz 		pfx = t;
14534f9b3eeSRoland Mainz 	}
14634f9b3eeSRoland Mainz 	for (;;)
14734f9b3eeSRoland Mainz 	{
14834f9b3eeSRoland Mainz 		if (!pathtemp(path, sizeof(path), dir, pfx, fdp))
14934f9b3eeSRoland Mainz 		{
15034f9b3eeSRoland Mainz 			if (quiet)
15134f9b3eeSRoland Mainz 				error_info.errors++;
15234f9b3eeSRoland Mainz 			else
15334f9b3eeSRoland Mainz 				error(ERROR_SYSTEM|2, "cannot create temporary path");
15434f9b3eeSRoland Mainz 			break;
15534f9b3eeSRoland Mainz 		}
15634f9b3eeSRoland Mainz 		if (fdp || unsafe || !mkdir(path, mode))
15734f9b3eeSRoland Mainz 		{
15834f9b3eeSRoland Mainz 			if (fdp)
15934f9b3eeSRoland Mainz 				close(*fdp);
16034f9b3eeSRoland Mainz 			sfputr(sfstdout, path, '\n');
16134f9b3eeSRoland Mainz 			break;
16234f9b3eeSRoland Mainz 		}
16334f9b3eeSRoland Mainz 		if (sh_checksig(context))
16434f9b3eeSRoland Mainz 		{
16534f9b3eeSRoland Mainz 			error_info.errors++;
16634f9b3eeSRoland Mainz 			break;
16734f9b3eeSRoland Mainz 		}
16834f9b3eeSRoland Mainz 	}
16934f9b3eeSRoland Mainz 	umask(mask);
17034f9b3eeSRoland Mainz 	return error_info.errors != 0;
17134f9b3eeSRoland Mainz }
172