xref: /titanic_51/usr/src/lib/libcmd/common/mktemp.c (revision a31148363f598def767ac48c5d82e1572e44b935)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2009 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) 2009-06-19 $\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         "[+*?Pseudo-random part is \a3-chars\a.\a3-chars\a to accomodate "
38             "8.3 filesystems.]"
39     "}"
40 "[+?A consecutive sequence of \bX\b's in \aprefix\a is replaced by the "
41     "pseudo-random part. If there are no \bX\b's then the pseudo-random part "
42     "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 "[t:tmp|temporary-directory?Create a path rooted in a temporary "
51     "directory.]"
52 "[u:unsafe|dry-run?Check for file/directory existence but do not create. "
53     "Who would want to do that.]"
54 "\n"
55 "\n[ prefix ]\n"
56 "\n"
57 "[+SEE ALSO?\bmkdir\b(1), \bpathtemp\b(3), \bmktemp\b(3)]"
58 ;
59 
60 #include <cmd.h>
61 #include <ls.h>
62 
63 int
64 b_mktemp(int argc, char** argv, void* context)
65 {
66 	mode_t		mode = 0;
67 	mode_t		mask;
68 	int		fd;
69 	int		i;
70 	int		quiet = 0;
71 	int		unsafe = 0;
72 	int*		fdp = &fd;
73 	char*		dir = "";
74 	char*		pfx;
75 	char*		t;
76 	char		path[PATH_MAX];
77 
78 	cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
79 	for (;;)
80 	{
81 		switch (optget(argv, usage))
82 		{
83 		case 0:
84 			break;
85 		case 'd':
86 			fdp = 0;
87 			continue;
88 		case 'm':
89 			mode = strperm(pfx = opt_info.arg, &opt_info.arg, S_IRWXU);
90 			if (*opt_info.arg)
91 				error(ERROR_exit(0), "%s: invalid mode", pfx);
92 			continue;
93 		case 'p':
94 			if ((t = getenv("TMPDIR")) && *t)
95 				dir = 0;
96 			else
97 				dir = opt_info.arg;
98 			continue;
99 		case 'q':
100 			quiet = 1;
101 			continue;
102 		case 't':
103 			dir = 0;
104 			continue;
105 		case 'u':
106 			unsafe = 1;
107 			fdp = 0;
108 			continue;
109 		case ':':
110 			error(2, "%s", opt_info.arg);
111 			continue;
112 		case '?':
113 			error(ERROR_usage(2), "%s", opt_info.arg);
114 			continue;
115 		}
116 		break;
117 	}
118 	argv += opt_info.index;
119 	if (error_info.errors || (pfx = *argv++) && *argv)
120 		error(ERROR_usage(2), "%s", optusage(NiL));
121 	mask = umask(0);
122 	if (!mode)
123 		mode = (fdp ? (S_IRUSR|S_IWUSR) : S_IRWXU) & ~mask;
124 	umask(~mode & (S_IRWXU|S_IRWXG|S_IRWXO));
125 	if (!pfx)
126 	{
127 		pfx = "tmp_";
128 		if (dir && !*dir)
129 			dir = 0;
130 	}
131 	if (t = strrchr(pfx, '/'))
132 	{
133 		i = ++t - pfx;
134 		dir = fmtbuf(i);
135 		memcpy(dir, pfx, i);
136 		dir[i] = 0;
137 		pfx = t;
138 	}
139 	for (;;)
140 	{
141 		if (!pathtemp(path, sizeof(path), dir, pfx, fdp))
142 		{
143 			if (quiet)
144 				error_info.errors++;
145 			else
146 				error(ERROR_SYSTEM|2, "cannot create temporary path");
147 			break;
148 		}
149 		if (fdp || unsafe || !mkdir(path, mode))
150 		{
151 			if (fdp)
152 				close(*fdp);
153 			sfputr(sfstdout, path, '\n');
154 			break;
155 		}
156 		if (sh_checksig(context))
157 		{
158 			error_info.errors++;
159 			break;
160 		}
161 	}
162 	umask(mask);
163 	return error_info.errors != 0;
164 }
165