2 Copyright 2007-2008 Wincent Colaiuta
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 Dir.mkdtemp([string]) -> String or nil
26 This method securely creates temporary directories. It is a wrapper for the mkdtemp() function in the standard C library. It takes
27 an optional String parameter as a template describing the desired form of the directory name and overwriting the template in-place;
28 if no template is supplied then "/tmp/temp.XXXXXX" is used as a default.
30 Note that the exact implementation of mkdtemp() may vary depending on the target system. For example, on Mac OS X at the time of
31 writing, the man page states that the template may contain "some number" of "Xs" on the end of the string, whereas on Red Hat
32 Enterprise Linux it states that the template suffix "must be XXXXXX".
35 static VALUE dir_mkdtemp_m(int argc, VALUE *argv, VALUE self)
39 if (rb_scan_args(argc, argv, "01", &template) == 0) /* check for 0 mandatory arguments, 1 optional argument */
40 template = Qnil; /* default to nil if no argument passed */
42 template = rb_str_new2("/tmp/temp.XXXXXX"); /* fallback to this template if passed nil */
43 SafeStringValue(template); /* raises if template is tainted and SAFE level > 0 */
44 template = StringValue(template); /* duck typing support */
45 path = mkdtemp(RSTRING(template)->ptr);
47 rb_raise(rb_eSystemCallError, "mkdtemp failed (error: %d)", errno);
53 rb_define_module_function(rb_cDir, "mkdtemp", dir_mkdtemp_m, -1);