/*
 *  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);

#ifdef HAVE_TRUNCATE
    if (truncate(StringValueCStr(path), pos) < 0)
        rb_sys_fail(RSTRING(path)->ptr);
#else
# ifdef HAVE_CHSIZE
    {
        int tmpfd;

#  ifdef _WIN32
        if ((tmpfd = open(StringValueCStr(path), O_RDWR)) < 0) {
            rb_sys_fail(RSTRING(path)->ptr);
        }
#  else
        if ((tmpfd = open(StringValueCStr(path), 0)) < 0) {
            rb_sys_fail(RSTRING(path)->ptr);
        }
#  endif
        if (chsize(tmpfd, pos) < 0) {
            close(tmpfd);
            rb_sys_fail(RSTRING(path)->ptr);
        }
        close(tmpfd);
    }
# else
    rb_notimplement();
# endif
#endif
    return INT2FIX(0);
}