1dirname(3C)              Standard C Library Functions              dirname(3C)
2
3
4

NAME

6       dirname - report the parent directory name of a file path name
7

SYNOPSIS

9       #include <libgen.h>
10
11       char *dirname(char *path);
12
13

DESCRIPTION

15       The  dirname() function takes a pointer to a character string that con‐
16       tains a pathname, and returns a pointer to a string that is a  pathname
17       of  the  parent directory of that file.  Trailing '/' characters in the
18       path are not counted as part of the path.
19
20
21       If path does not contain a '/', then dirname() returns a pointer to the
22       string  "."  .  If path is a null pointer or points to an empty string,
23       dirname() returns a pointer to the string "." .
24

RETURN VALUES

26       The dirname() function returns a pointer to a string that is the parent
27       directory  of  path.  If  path  is a null pointer or points to an empty
28       string, a pointer to a string "." is returned.
29

ERRORS

31       No errors are defined.
32

EXAMPLES

34       Example 1 Changing the Current Directory to the Parent Directory.
35
36
37       The following code fragment reads a pathname, changes the current work‐
38       ing directory to the parent directory of the named file (see chdir(2)),
39       and opens the file.
40
41
42         char path[[MAXPATHLEN], *pathcopy;
43         int fd;
44         fgets(path, MAXPATHLEN, stdin);
45         pathcopy = strdup(path);
46         chdir(dirname(pathcopy));
47         fd = open(basename(path), O_RDONLY);
48
49
50       Example 2 Sample Input and Output Strings for dirname().
51
52
53       In the following table, the input string is the  value  pointed  to  by
54       path,  and the output string is the return value of the dirname() func‐
55       tion.
56
57
58
59
60
61       ┌─────────────────────────────┬─────────────────────────────┐
62Input String          Output String         
63       ├─────────────────────────────┼─────────────────────────────┤
64       │"/usr/lib""                  │"/usr"                       │
65       ├─────────────────────────────┼─────────────────────────────┤
66       │"/usr/"                      │"/"                          │
67       ├─────────────────────────────┼─────────────────────────────┤
68       │"usr"                        │"/"                          │
69       ├─────────────────────────────┼─────────────────────────────┤
70       │"/"                          │"/"                          │
71       ├─────────────────────────────┼─────────────────────────────┤
72       │"."                          │"."                          │
73       ├─────────────────────────────┼─────────────────────────────┤
74       │".."                         │"."                          │
75       └─────────────────────────────┴─────────────────────────────┘
76

USAGE

78       The dirname() function modifies the string pointed to by path.
79
80
81       The dirname() and basename(3C)  functions  together  yield  a  complete
82       pathname.   The  expression  dirname(path)  obtains the pathname of the
83       directory where basename(path) is found.
84

ATTRIBUTES

86       See attributes(5) for descriptions of the following attributes:
87
88
89
90
91       ┌─────────────────────────────┬─────────────────────────────┐
92       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
93       ├─────────────────────────────┼─────────────────────────────┤
94       │Interface Stability          │Standard                     │
95       ├─────────────────────────────┼─────────────────────────────┤
96       │MT-Level                     │MT-Safe                      │
97       └─────────────────────────────┴─────────────────────────────┘
98

SEE ALSO

100       basename(1), chdir(2), basename(3C), attributes(5), standards(5)
101
102
103
104SunOS 5.11                        18 Mar 2002                      dirname(3C)
Impressum