Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > B

basename(3C)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

NAME

basename(), dirname() — extract components of a path name

SYNOPSIS

#include <libgen.h>

char *basename(char *path);

char *dirname(char *path);

DESCRIPTION

basename() takes the path name pointed to by path and returns a pointer to the final component of the path name, deleting any trailing '/' characters. If the string consists entirely of '/' characters, basename() returns a pointer to the string "/". If path is a null pointer or points to the empty string, basename() returns a pointer to the string ".".

dirname() takes the path name pointed to by path and returns a pointer to a string that is a path name of the parent directory of that file. If path is a null pointer, points to the empty string, or does not contain a '/' character, then dirname() returns a pointer to the string ".".

RETURN VALUE

basename() returns a pointer to the final component of path.

dirname() returns a pointer to a string that is the parent directory of path.

EXAMPLES

The following code fragment calls basename() and dirname().

#include <libgen.h> const char *path="/usr/local/bin/foo"; char *base, *dir; char *dup0, *dup1; char *dup2, *dup3;

/*Use strdup in case literals * are in text, or basename() * and dirname() modify the * input string. */

dup0=strdup(path); dup1=strdup(path);

base=basename(dup0); dir=dirname(dup1);

/* Use strdup before modifying * return string in case a * pointer to a literal is * returned. */

dup2=strdup(base); dup3=strdup(dir);

WARNINGS

basename() and dirname() may overwrite path.

basename() and dirname() in non-threaded applications currently use a static buffer in each function to hold the result string. Any subsequent calls to basename() and dirname() may overwrite the static buffer. At some release after HP-UX 11i Version 1, HP may use malloc() to allocate the buffers. Once allocated, those buffers will be reused and their addresses will not change; however, if the malloc() fails, basename() and dirname() would return "." and errno would be set to ENOMEM.

AUTHOR

basename() and dirname() were developed by HP.

STANDARDS CONFORMANCE

basename(): XPG4.2 dirname(): XPG4.2

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.