1 /***********************************************************************
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 *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
23 static const char usage
[] =
24 "[-?\n@(#)$Id: mktemp (AT&T Research) 2010-03-05 $\n]"
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:]"
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.]"
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 "
55 "[u:unsafe|dry-run?Check for file/directory existence but do not create. "
56 "Use this for testing only.]"
60 "[+SEE ALSO?\bmkdir\b(1), \bpathtemp\b(3), \bmktemp\b(3)]"
67 b_mktemp(int argc
, char** argv
, void* context
)
81 cmdinit(argc
, argv
, context
, ERROR_CATALOG
, ERROR_NOTIFY
);
84 switch (optget(argv
, usage
))
92 mode
= strperm(pfx
= opt_info
.arg
, &opt_info
.arg
, S_IRWXU
);
94 error(ERROR_exit(0), "%s: invalid mode", pfx
);
97 if ((t
= getenv("TMPDIR")) && *t
)
113 if (!pathtemp(NiL
, 0, opt_info
.arg
, "/seed", NiL
))
114 error(2, "%s: regression test initializtion failed", opt_info
.arg
);
117 error(2, "%s", opt_info
.arg
);
120 error(ERROR_usage(2), "%s", opt_info
.arg
);
125 argv
+= opt_info
.index
;
126 if (error_info
.errors
|| (pfx
= *argv
++) && *argv
)
127 error(ERROR_usage(2), "%s", optusage(NiL
));
130 mode
= (fdp
? (S_IRUSR
|S_IWUSR
) : S_IRWXU
) & ~mask
;
131 umask(~mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
));
138 if (t
= strrchr(pfx
, '/'))
148 if (!pathtemp(path
, sizeof(path
), dir
, pfx
, fdp
))
153 error(ERROR_SYSTEM
|2, "cannot create temporary path");
156 if (fdp
|| unsafe
|| !mkdir(path
, mode
))
160 sfputr(sfstdout
, path
, '\n');
163 if (sh_checksig(context
))
170 return error_info
.errors
!= 0;