dirname(p1)
public
Returns all components of the filename given in file_name except
the last one. The filename must be formed using forward slashes (“/”)
regardless of the separator used on the local file system.
File.dirname("/home/gumby/work/ruby.rb")
Show source
/*
* call-seq:
* File.dirname(file_name ) -> dir_name
*
* Returns all components of the filename given in <i>file_name</i>
* except the last one. The filename must be formed using forward
* slashes (``<code>/</code>'') regardless of the separator used on the
* local file system.
*
* File.dirname("/home/gumby/work/ruby.rb")
*/
static VALUE
rb_file_s_dirname(klass, fname)
VALUE klass, fname;
{
const char *name, *root, *p;
VALUE dirname;
name = StringValueCStr(fname);
root = skiproot(name);
#ifdef DOSISH_UNC
if (root > name + 1 && isdirsep(*name))
root = skipprefix(name = root - 2);
#else
if (root > name + 1)
name = root - 1;
#endif
p = strrdirsep(root);
if (!p) {
p = root;
}
if (p == name)
return rb_str_new2(".");
#ifdef DOSISH_DRIVE_LETTER
if (has_drive_letter(name) && isdirsep(*(name + 2))) {
const char *top = skiproot(name + 2);
dirname = rb_str_new(name, 3);
rb_str_cat(dirname, top, p - top);
}
else
#endif
dirname = rb_str_new(name, p - name);
#ifdef DOSISH_DRIVE_LETTER
if (has_drive_letter(name) && root == name + 2 && p - name == 2)
rb_str_cat(dirname, ".", 1);
#endif
OBJ_INFECT(dirname, fname);
return dirname;
}