DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
A UNIX Path Name Library for C++ - Path(C++)

Search paths

A search path is a list of paths, all of which denote directories (or possible directories) in the underlying file system. Search paths are written by placing a colon (':') between the individual paths. Search paths are used to specify the default places in which many programs are likely to be kept. The Path library provides the ability to easily manipulate search paths.

Creating a Search_path is easy.

       #include <Search_path.h>
       Search_path sp1(".");
       Search_path sp2("/usr/ucb:/usr/bin:/bin");

Notice that the header file Search_path.h should be included to get Search_path functionality. This is to make compilation faster for those users who do not need Search_paths.

The following Search_path comes pre-defined in the library:\classindex{PATH}{[Search__path]}

       extern Search_path PATH;

This is the value of the environment variable PATH at the time of program start-up. (Incidentally, notice that the name ``PATH'' is a misnomer. ``PATH'' actually represents a search path, not a path.)

Since Search_paths inherit from List<Path>, any operation we can perform on a List<Path> can also be performed on a Search_path. For example, the following code removes all occurrences of ``/usr/5bin'' from PATH:

       Path fivebin("/usr/5bin");
       Listiter<Path> it(PATH);
       while (!it.at_end()) {
           if (it.peek_next() == fivebin)
               it.remove_next();
           else
               it.next();
       }

If we want to put the resulting PATH back into the environment, putenv can be used as usual.

       String s = "PATH=";
       s += (String)PATH;
       putenv(s);

Searching in a Search_path is also easy. The following code finds the location of the first executable file named ``more'' in PATH:

       Path p("more");
       Path more;
       if (PATH.find(p, more))
           cout << "location of \ "more\ " is "
                << more << endl;
       else
           cout << "no \ "more\ " in " << PATH << endl;

The above program looks for the first executable file in the Search_path. In general, we might want to look for the first file which satisfies a given ksh_test criterion. For example, the following code looks for the first non-empty file ``foo'':

       Path p("foo");
       Path foo;
       if (PATH.find(p, foo, Ksh_test::s))
       // ...

In rare cases even this generality may not be enough. For example, it is not possible to find the first executable directory ``foo'': although there are Ksh_test::-unary's for both ``is executable'' and ``is directory,'' there is no Ksh_test::-unary for ``is executable directory.'' In such a case, the user can get a list containing all the ``foo''s in the Search_path satisfying one of the criteria, from which the one(s) satisfying the other criterion can be selected.

       Path p("foo");
       List<Path> plist;
       PATH.find_all(p, plist, Ksh_test::d)
           // get all directories
       // now find first executable file in plist
       // ...

Next topic: Wildcards
Previous topic: The ksh ``test'' function

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005