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 <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, Shbltin_t* 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 'd': 87 fdp = 0; 88 continue; 89 case 'm': 90 mode = strperm(pfx = opt_info.arg, &opt_info.arg, S_IRWXU); 91 if (*opt_info.arg) 92 error(ERROR_exit(0), "%s: invalid mode", pfx); 93 continue; 94 case 'p': 95 if ((t = getenv("TMPDIR")) && *t) 96 dir = 0; 97 else 98 dir = opt_info.arg; 99 continue; 100 case 'q': 101 quiet = 1; 102 continue; 103 case 't': 104 dir = 0; 105 continue; 106 case 'u': 107 unsafe = 1; 108 fdp = 0; 109 continue; 110 case 'R': 111 if (!pathtemp(NiL, 0, opt_info.arg, "/seed", NiL)) 112 error(2, "%s: regression test initializtion failed", opt_info.arg); 113 continue; 114 case ':': 115 error(2, "%s", opt_info.arg); 116 break; 117 case '?': 118 error(ERROR_usage(2), "%s", opt_info.arg); 119 break; 120 } 121 break; 122 } 123 argv += opt_info.index; 124 if (error_info.errors || (pfx = *argv++) && *argv) 125 error(ERROR_usage(2), "%s", optusage(NiL)); 126 mask = umask(0); 127 if (!mode) 128 mode = (fdp ? (S_IRUSR|S_IWUSR) : S_IRWXU) & ~mask; 129 umask(~mode & (S_IRWXU|S_IRWXG|S_IRWXO)); 130 if (!pfx) 131 { 132 pfx = "tmp_"; 133 if (dir && !*dir) 134 dir = 0; 135 } 136 if (t = strrchr(pfx, '/')) 137 { 138 i = ++t - pfx; 139 dir = fmtbuf(i); 140 memcpy(dir, pfx, i); 141 dir[i] = 0; 142 pfx = t; 143 } 144 for (;;) 145 { 146 if (!pathtemp(path, sizeof(path), dir, pfx, fdp)) 147 { 148 if (quiet) 149 error_info.errors++; 150 else 151 error(ERROR_SYSTEM|2, "cannot create temporary path"); 152 break; 153 } 154 if (fdp || unsafe || !mkdir(path, mode)) 155 { 156 if (fdp) 157 close(*fdp); 158 sfputr(sfstdout, path, '\n'); 159 break; 160 } 161 if (sh_checksig(context)) 162 { 163 error_info.errors++; 164 break; 165 } 166 } 167 umask(mask); 168 return error_info.errors != 0; 169 } 170