A UNIX Path Name Library for C++ - Path(C++)

The ksh ``test'' function

As mentioned in the introduction, a Path represents only a possible file in the underlying file system. To test whether the file represented by a given Path actually exists, (specifically, whether it can be ``statted'') use the function ksh_test.

       Path p(...);
       if (!ksh_test(p))
           cerr << "file not found!" << endl;
       else {
           // process the file
           // ...

The function ksh_test is modeled after the file testing functionality of the Korn shell ``test'' function. This means that all the flags which make sense with file names in the Korn shell ``test'' have equivalents in Ksh_test.

       ksh_test(Ksh_test::r, p)   // same as `test -r p`
       ksh_test(Ksh_test::w, p)   // same as `test -w p`
       ksh_test(Ksh_test::x, p)   // same as `test -x p`
       ksh_test(Ksh_test::d, p)   // same as `test -d p`
       // etc.

The first argument to ksh_test can be any member of the enumeration Ksh_test::-unary. If missing (as in the first program above), it is treated as Ksh_test::a. The Korn shell binary tests are also available.

       ksh_test(p1, Ksh_test::ef, p2)
           // same as `test p1 -ef p2`
       ksh_test(p1, Ksh_test::nt, p2)
           // same as `test p1 -nt p2`
       ksh_test(p1, Ksh_test::ot, p2)
           // same as `test p1 -ot p2`

In 90% of the cases, one of the two enumerations Ksh_test::-unary or Ksh_test::-binary will contain the test the user needs to perform. In the remaining 10% of the cases, the user will have to do the desired test directly by calling stat(2).

       #include <sys/stat.h>
       int owned_by_root(const Path & p) {
           stat s;
           return (::stat(p, &s) && s.st_uid == 0);

Remember that inheritance is always available, so that such functions can be ``added'' to the Path interface.

       class myPath : public Path {
           int owned_by_root() {
               return ::owned_by_root(*this); }

Next topic: Search paths
Previous topic: The basic operations

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