dirname(3)

dirname3

Extract the directory part of a pathname

Usage

#include <libgen.h>

char *

dirname(char *path);

Overview

The dirname() function is the converse of basename(3); it returns a pointer to the parent directory of the pathname pointed to by path. Any trailing ‘/’ characters are not counted as part of the directory name.

Implementation notes

This implementation of dirname() uses the buffer provided by the caller to store the resulting parent directory. Other vendor implementations may return a pointer to internal storage space instead. The advantage of the former approach is that it ensures thread-safety, while also placing no upper limit on the supported length of the pathname.

Return values

If path is a null pointer, the empty string, or contains no ‘/’ characters, dirname() returns a pointer to the string ".", signifying the current directory. Otherwise, it returns a pointer to the parent directory of path.

Releases

67 releases since FreeBSD 4.2-RELEASE; the text changed 40 times.

  1. 4.2
  2. 4.3
  3. 4.4
  4. 4.5–4.7
  5. 4.8–4.11
  6. 5.0
  7. 5.1–5.2.1
  8. 5.3–6.1
  9. 6.2–6.3
  10. 6.4
  11. 7.0
  12. 7.1
  13. 7.2
  14. 7.3
  15. 7.4
  16. 8.0
  17. 8.1
  18. 8.2
  19. 8.3
  20. 8.4
  21. 9.0
  22. 9.1
  23. 9.2
  24. 9.3
  25. 10.0
  26. 10.1
  27. 10.2
  28. 10.3
  29. 10.4
  30. 11.0
  31. 11.1
  32. 11.2
  33. 11.3
  34. 11.4
  35. 12.0
  36. 12.1
  37. 12.2
  38. 12.3
  39. 12.4
  40. 13.0–13.2
  41. 13.3–16.0

Standards

The dirname() function conforms to X/Open Portability Guide Issue 4, Version 2 (“XPG4.2”).

History

The dirname() function first appeared in OpenBSD 2.2 and FreeBSD 4.2.

In FreeBSD 12.0, this function was reimplemented to store its result in the provided input buffer.

Authors

Nuxi, the Netherlands

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.