Blame man-pages-posix-2013-a/man3p/dirname.3p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH DIRNAME "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
dirname
Packit 7cfc04
\(em report the parent directory name of a file pathname
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
#include <libgen.h>
Packit 7cfc04
.P
Packit 7cfc04
char *dirname(char *\fIpath\fP);
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
function shall take a pointer to a character string that contains a
Packit 7cfc04
pathname, and return a pointer to a string that is a pathname of the
Packit 7cfc04
parent directory of that file. Trailing
Packit 7cfc04
.BR '/' 
Packit 7cfc04
characters in the path are not counted as part of the path.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
.IR path
Packit 7cfc04
does not contain a
Packit 7cfc04
.BR '/' ,
Packit 7cfc04
then
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
shall return a pointer to the string
Packit 7cfc04
.BR \(dq.\(dq .
Packit 7cfc04
If
Packit 7cfc04
.IR path
Packit 7cfc04
is a null pointer or points to an empty string,
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
shall return a pointer to the string
Packit 7cfc04
.BR \(dq.\(dq .
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
function need not be thread-safe.
Packit 7cfc04
.SH "RETURN VALUE"
Packit 7cfc04
The
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
function shall return a pointer to a string that is the parent
Packit 7cfc04
directory of
Packit 7cfc04
.IR path .
Packit 7cfc04
If
Packit 7cfc04
.IR path
Packit 7cfc04
is a null pointer or points to an empty string, a pointer to a string
Packit 7cfc04
.BR \(dq.\(dq 
Packit 7cfc04
is returned.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
function may modify the string pointed to by
Packit 7cfc04
.IR path ,
Packit 7cfc04
and may return a pointer to internal storage. The returned pointer might
Packit 7cfc04
be invalidated or the storage might be overwritten by a subsequent call to
Packit 7cfc04
\fIdirname\fR().
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
No errors are defined.
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
The following code fragment reads a pathname, changes the current
Packit 7cfc04
working directory to the parent directory, and opens the file.
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
char *path = NULL, *pathcopy;
Packit 7cfc04
size_t buflen = 0;
Packit 7cfc04
ssize_t linelen = 0;
Packit 7cfc04
int fd;
Packit 7cfc04
.P
Packit 7cfc04
linelen = getline(&path, &buflen, stdin);
Packit 7cfc04
.P
Packit 7cfc04
path[linelen-1] = 0;
Packit 7cfc04
pathcopy = strdup(path);
Packit 7cfc04
if (chdir(dirname(pathcopy)) < 0) {
Packit 7cfc04
    ...
Packit 7cfc04
}
Packit 7cfc04
if ((fd = open(basename(path), O_RDONLY)) >= 0) {
Packit 7cfc04
    ...
Packit 7cfc04
    close (fd);
Packit 7cfc04
}
Packit 7cfc04
\&...
Packit 7cfc04
free (pathcopy);
Packit 7cfc04
free (path);
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SS "Sample Input and Output Strings for dirname(\|)"
Packit 7cfc04
.P
Packit 7cfc04
In the following table, the input string is the value pointed to by
Packit 7cfc04
.IR path ,
Packit 7cfc04
and the output string is the return value of the
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
function.
Packit 7cfc04
.TS
Packit 7cfc04
center tab(!) box;
Packit 7cfc04
cB | cB
Packit 7cfc04
lf5 | lf5.
Packit 7cfc04
Input String!Output String
Packit 7cfc04
_
Packit 7cfc04
"/usr/lib"!"/usr"
Packit 7cfc04
"/usr/"!"/"
Packit 7cfc04
"usr"!"."
Packit 7cfc04
"/"!"/"
Packit 7cfc04
"."!"."
Packit 7cfc04
".."!"."
Packit 7cfc04
.TE
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
The
Packit 7cfc04
\fIdirname\fR()
Packit 7cfc04
and
Packit 7cfc04
\fIbasename\fR()
Packit 7cfc04
functions together yield a complete pathname. The expression
Packit 7cfc04
\fIdirname\fP\^(\fIpath\fP) obtains the pathname of the directory where
Packit 7cfc04
\fIbasename\fP\^(\fIpath\fP) is found.
Packit 7cfc04
.P
Packit 7cfc04
Since the meaning of the leading
Packit 7cfc04
.BR \(dq//\(dq 
Packit 7cfc04
is implementation-defined,
Packit 7cfc04
.IR dirname ("\c
Packit 7cfc04
.BR //foo ")
Packit 7cfc04
may return either
Packit 7cfc04
.BR \(dq//\(dq 
Packit 7cfc04
or
Packit 7cfc04
.BR '/' 
Packit 7cfc04
(but nothing else).
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
None.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "\fIbasename\fR\^(\|)"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "\fB<libgen.h>\fP"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .