COHERENT manpages

This page displays the COHERENT manpage for path() [Path name for a file].

List of available manpages
Index


path() -- General Function (libc)

Path name for a file
#include <path.h>
#include <stdio.h>
char *path(path, filename, mode);
char *path, *filename;
int mode;

The function path() builds a path name for a file.

path points  to the list of  directories to be searched  for the file.  You
can  use the  function getenv()  to  obtain the  current definition  of the
environmental variable  PATH, or use  the default setting of  PATH found in
the header file path.h, or, you can define path by hand.

filename is the name of the  file for which path is to search.  mode is the
mode in which you wish to access the file, as follows:

     X_OK    Execute the file
     W_OK    Write to the file
     R_OK    Read the file

path() calls the function access()  to check the access status of filename.
If path()  finds the file  you requested and  the file is  available in the
mode that you requested, it returns  a pointer to a static area in which it
has built  the appropriate path  name.  It returns  NULL if either  path or
filename are  NULL, if the search  failed, or if the  requested file is not
available in the correct mode.

Example

This example accepts a file name  and a search mode.  It then tries to find
the  file  in  one of  the  directories  named  in  the PATH  environmental
variable.

#include <path.h>
#include <stdio.h>
#include <stdlib.h>

void
fatal(message)
char *message;
{
    fprintf(stderr, "%s\n", message);
    exit(1);
}

main(argc, argv)
int argc; char *argv[];
{
    char *env, *pathname;
    int mode;

    if (argc != 3)
        fatal("Usage: findpath filename mode");

    if(((mode=atoi(argv[2]))>4) || (mode==3) || (mode<1))
        fatal("modes: 1=execute, 2=write, 4=read");

    env = getenv("PATH");
    if ((pathname = path(env, argv[1], mode)) != NULL) {
        printf("PATH = %s\n", env);
        printf("pathname = %s\n", pathname);
        return;
    } else
        fatal("search failed");
}

See Also

access(),
libc,
PATH,
path.h