/*
* call-seq:
* File.basename(file_name [, suffix] ) -> base_name
*
* Returns the last component of the filename given in <i>file_name</i>,
* which must be formed using forward slashes (``<code>/</code>'')
* regardless of the separator used on the local file system. If
* <i>suffix</i> is given and present at the end of <i>file_name</i>,
* it is removed.
*
* File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
* File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
*/
static VALUE
rb_file_s_basename(argc, argv)
int argc;
VALUE *argv;
{
VALUE fname, fext, basename;
char *name, *p, *ext = NULL;
int f;
if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
ext = StringValueCStr(fext);
}
StringValue(fname);
if (RSTRING(fname)->len == 0 || !*(name = RSTRING(fname)->ptr))
return fname;
if (!*(name = skiproot(name))) {
p = name - 1;
f = 1;
#ifdef DOSISH_DRIVE_LETTER
if (*p == ':') {
p++;
f = 0;
}
#endif
}
else if (!(p = strrdirsep(name))) {
if (NIL_P(fext) || !(f = rmext(name, ext))) {
f = chompdirsep(name) - name;
if (f == RSTRING(fname)->len) return fname;
}
p = name;
}
else {
while (isdirsep(*p)) p++; /* skip last / */
if (NIL_P(fext) || !(f = rmext(p, ext))) {
f = chompdirsep(p) - p;
}
}
basename = rb_str_new(p, f);
OBJ_INFECT(basename, fname);
return basename;
}