# dirname

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           dirname — report the parent directory name of a file pathname
    


## SYNOPSIS



           #include <libgen.h>
    
           char *dirname(char *_path_);
    


## DESCRIPTION



           The _dirname_() function shall take a pointer to a character string
           that contains a pathname, and return a pointer to a string that is
           a pathname of the parent directory of that file. The _dirname_()
           function shall not perform pathname resolution; the result shall
           not be affected by whether or not _path_ exists or by its file type.
           Trailing **'/'** characters in the path that are not also leading **'/'**
           characters shall not be counted as part of the path.
    
           If _path_ does not contain a **'/'** , then _dirname_() shall return a
           pointer to the string **"."**.  If _path_ is a null pointer or points to
           an empty string, _dirname_() shall return a pointer to the string
           **"."**.
    
           The _dirname_() function may modify the string pointed to by _path_ ,
           and may return a pointer to static storage that may then be
           overwritten by a subsequent call to _dirname_().
    
           The _dirname_() function need not be thread-safe.
    


## RETURN VALUE



           The _dirname_() function shall return a pointer to a string as
           described above.
    
           The _dirname_() function may modify the string pointed to by _path_ ,
           and may return a pointer to internal storage. The returned pointer
           might be invalidated or the storage might be overwritten by a
           subsequent call to _dirname_().  The returned pointer might also be
           invalidated if the calling thread is terminated.
    


## ERRORS



           No errors are defined.
    
           _The following sections are informative._
    


## EXAMPLES



           The following code fragment reads a pathname, changes the current
           working directory to the parent directory, and opens the file.
    
               char *path = NULL, *pathcopy;
               size_t buflen = 0;
               ssize_t linelen = 0;
               int fd;
    
               linelen = getline(&path, &buflen, stdin);
    
               path[linelen-1] = 0;
               pathcopy = strdup(path);
               if (chdir(dirname(pathcopy)) < 0) {
                   ...
               }
               if ((fd = open(basename(path), O_RDONLY)) >= 0) {
                   ...
                   close (fd);
               }
               ...
               free (pathcopy);
               free (path);
    
           The EXAMPLES section of the _basename_() function (see basename(3p))
           includes a table showing examples of the results of processing
           several sample pathnames by the _basename_() and _dirname_() functions
           and by the _basename_ and _dirname_ utilities.
    


## APPLICATION USAGE



           The _dirname_() and _basename_() functions together yield a complete
           pathname. The expression _dirname_(_path_) obtains the pathname of the
           directory where _basename_(_path_) is found.
    
           Since the meaning of the leading **"//"** is implementation-defined,
           _dirname_("**//foo**) may return either **"//"** or **'/'**(but nothing else).
    


## RATIONALE



           None.
    


## FUTURE DIRECTIONS



           None.
    


* * *