/*
 *  call-seq:
 *     File.extname(path) -> string
 *  
 *  Returns the extension (the portion of file name in <i>path</i>
 *  after the period).
 *     
 *     File.extname("test.rb")         #=> ".rb"
 *     File.extname("a/b/d/test.rb")   #=> ".rb"
 *     File.extname("test")            #=> ""
 *     File.extname(".profile")        #=> ""
 *     
 */

static VALUE
rb_file_s_extname(klass, fname)
    VALUE klass, fname;
{
    const char *name, *p, *e;
    VALUE extname;

    name = StringValueCStr(fname);
    p = strrdirsep(name);       /* get the last path component */
    if (!p)
        p = name;
    else
        name = ++p;

    e = 0;
    while (*p) {
        if (*p == '.' || istrailinggabage(*p)) {
#if USE_NTFS
            const char *last = p++, *dot = last;
            while (istrailinggabage(*p)) {
                if (*p == '.') dot = p;
                p++;
            }
            if (!*p || *p == ':') {
                p = last;
                break;
            }
            if (*last == '.') e = dot;
            continue;
#else
            e = p;       /* get the last dot of the last component */
#endif
        }
#if USE_NTFS
        else if (*p == ':') {
            break;
        }
#endif
        else if (isdirsep(*p))
            break;
        p = CharNext(p);
    }
    if (!e || e == name || e+1 == p)    /* no dot, or the only dot is first or end? */
        return rb_str_new(0, 0);
    extname = rb_str_new(e, p - e);     /* keep the dot, too! */
    OBJ_INFECT(extname, fname);
    return extname;
}