ppaatthh() -- General Function (libc)

Path name for a file
#iinncclluuddee <ppaatthh.hh>
#iinncclluuddee <ssttddiioo.hh>
cchhaarr *ppaatthh(_p_a_t_h, _f_i_l_e_n_a_m_e, _m_o_d_e);
cchhaarr *_p_a_t_h, *_f_i_l_e_n_a_m_e;
iinntt _m_o_d_e;

The function ppaatthh() builds a path name for a file.

_p_a_t_h points  to the list of  directories to be searched  for the file.  You
can  use the  function ggeetteennvv()  to  obtain the  current definition  of the
environmental variable  PPAATTHH, or use  the default setting of  PPAATTHH found in
the header file ppaatthh.hh, or, you can define _p_a_t_h by hand.

_f_i_l_e_n_a_m_e is the name of the  file for which ppaatthh is to search.  _m_o_d_e is the
mode in which you wish to access the file, as follows:

     XX_OOKK    Execute the file
     WW_OOKK    Write to the file
     RR_OOKK    Read the file

ppaatthh() calls the function aacccceessss()  to check the access status of _f_i_l_e_n_a_m_e.
If ppaatthh()  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  _p_a_t_h or
_f_i_l_e_n_a_m_e are  NULL, if the search  failed, or if the  requested file is not
available in the correct mode.

_E_x_a_m_p_l_e
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 PPAATTHH  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");
}

_S_e_e _A_l_s_o
aacccceessss(), lliibbcc, PPAATTHH, ppaatthh.hh
