HPlogo HP-UX Reference Volume 4 of 5 > b

basename(3C)

» 

Technical documentation

Complete book in PDF

 » 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);

APPLICATION USAGE

basename() and dirname() are thread-safe. They are not async-cancel-safe.

WARNINGS

basename() and dirname() may overwrite path.

AUTHOR

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

SEE ALSO

basename(1).

STANDARDS CONFORMANCE

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

© Hewlett-Packard Development Company, L.P.