truncate(p1, p2)
public
Truncates the file file_name to be at most integer bytes
long. Not available on all platforms.
f = File.new("out", "w")
f.write("1234567890")
f.close
File.truncate("out", 5)
File.size("out")
Show source
/*
* call-seq:
* File.truncate(file_name, integer) => 0
*
* Truncates the file <i>file_name</i> to be at most <i>integer</i>
* bytes long. Not available on all platforms.
*
* f = File.new("out", "w")
* f.write("1234567890") #=> 10
* f.close #=> nil
* File.truncate("out", 5) #=> 0
* File.size("out") #=> 5
*
*/
static VALUE
rb_file_s_truncate(klass, path, len)
VALUE klass, path, len;
{
off_t pos;
rb_secure(2);
pos = NUM2OFFT(len);
SafeStringValue(path);
if (truncate(StringValueCStr(path), pos) < 0)
rb_sys_fail(RSTRING(path)->ptr);
{
int tmpfd;
if ((tmpfd = open(StringValueCStr(path), O_RDWR)) < 0) {
rb_sys_fail(RSTRING(path)->ptr);
}
if ((tmpfd = open(StringValueCStr(path), 0)) < 0) {
rb_sys_fail(RSTRING(path)->ptr);
}
if (chsize(tmpfd, pos) < 0) {
close(tmpfd);
rb_sys_fail(RSTRING(path)->ptr);
}
close(tmpfd);
}
rb_notimplement();
return INT2FIX(0);
}