Module: STemp

Defined in:
ext/stemp.c

Class Method Summary collapse

Class Method Details

.mkdtemp(template) ⇒ String

Wraps mkdtemp(3) for Ruby. See man mkdtemp on your system for exact semantics.

Notes:

  • template is overwritten with the eventual directory name.

  • raises a SystemCallError if any of the syscalls involved fail.

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/stemp.c', line 55

static VALUE stemp_mkdtemp(VALUE self, VALUE template) {
  VALUE duckstr;
  char *ret;

  SafeStringValue(template);

  duckstr = StringValue(template);

  ret = mkdtemp(RSTRING(duckstr)->ptr);

  if (NULL == ret) raise_systemcallerror(errno, "in mkdtemp");

  return duckstr;
}

.mkstemp(template) ⇒ File

Wraps mkstemp(3) for Ruby. See man mkstemp on your system for exact semantics.

Notes:

  • returns a File object rather than a simple file descriptor.

  • template is overwritten with the eventual directory name.

  • raises a SystemCallError if any of the syscalls involved fail.

  • the Debian manpage for mkstemp suggests you shouldn’t use it, but rather stick with tmpfile. However, I think it’s better all round to have the choice; there are times when you want to know the tempfile name.

Returns:

  • (File)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/stemp.c', line 85

static VALUE stemp_mkstemp(VALUE self, VALUE template) {
  VALUE duckstr, filedesc, file;
  int fd;

  SafeStringValue(template);

  duckstr = StringValue(template);

  fd = mkstemp(RSTRING(duckstr)->ptr);

  if (-1 == fd) raise_systemcallerror(errno, "in mkstemp");

  filedesc = INT2FIX(fd);

  file = rb_class_new_instance(1, &filedesc,
    rb_const_get(rb_cObject, rb_intern("File")));
  
  return file;
}

.tmpfileFile

Wraps tmpfile(3) for Ruby. See man tmpfile on your system for exact semantics.

Notes:

  • raises a SystemCallError if any of the syscalls involved fail.

Returns:

  • (File)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/stemp.c', line 114

static VALUE stemp_tmpfile(VALUE self) {
  VALUE filedesc, file;
  FILE *fp;

  fp = tmpfile();

  if (NULL == fp) raise_systemcallerror(errno, "in tmpfile");

  /* A shame to have to move back to fileno from the FILE *, but there doesn't
   * seem to be a clean way to do this in Ruby's C bindings otherwise.
   * Note that fileno() is not ANSI C, but should be present wherever mkstemp
   * and company are. */
  filedesc = INT2FIX(fileno(fp));

  file = rb_class_new_instance(1, &filedesc,
    rb_const_get(rb_cObject, rb_intern("File")));
  
  return file;
}