Function: file-name-as-directory

file-name-as-directory is a function defined in fileio.c.

Signature

(file-name-as-directory FILE)

Documentation

Return a string representing the file name FILE interpreted as a directory.

This operation exists because a directory is also a file, but its name as a directory is different from its name as a file. The result can be used as the value of default-directory or passed as second argument to expand-file-name. For a Unix-syntax file name, just appends a slash unless a trailing slash is already present.

Other relevant functions are documented in the file-name group.

View in manual

Probably introduced at or before Emacs version 18.

Shortdoc

;; file-name
(file-name-as-directory "/tmp/foo")
    => "/tmp/foo/"

Source Code

// Defined in /usr/src/emacs/src/fileio.c
{
  char *buf;
  ptrdiff_t length;
  Lisp_Object handler, val;
  USE_SAFE_ALLOCA;

  CHECK_STRING (file);

  /* If the file name has special constructs in it,
     call the corresponding file name handler.  */
  handler = Ffind_file_name_handler (file, Qfile_name_as_directory);
  if (!NILP (handler))
    {
      Lisp_Object handled_name = calln (handler, Qfile_name_as_directory,
					file);
      if (STRINGP (handled_name))
	return handled_name;
      error ("Invalid handler in `file-name-handler-alist'");
    }

#ifdef WINDOWSNT
  if (!NILP (Vw32_downcase_file_names))
    file = Fdowncase (file);
#endif
  buf = SAFE_ALLOCA (SBYTES (file) + file_name_as_directory_slop + 1);
  length = file_name_as_directory (buf, SSDATA (file), SBYTES (file),
				   STRING_MULTIBYTE (file));
  val = make_specified_string (buf, -1, length, STRING_MULTIBYTE (file));
  SAFE_FREE ();
  return val;
}