Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:14

0001 // name=pathtype_test ; gcc $name.cc -std=c++11 -lstdc++ -o /tmp/$name && /tmp/$name
0002 
0003 #include <string>
0004 #include <sstream>
0005 #include <iostream>
0006 #include <iomanip>
0007 
0008 #include <sys/types.h>
0009 #include <sys/stat.h>
0010 #include <unistd.h>
0011 
0012 int pathtype(const char* path)
0013 {
0014     int rc = -1 ; 
0015     struct stat st ;
0016     if(0 == stat(path, &st))
0017     {
0018         if(     S_ISDIR(st.st_mode)) rc = 1 ; 
0019         else if(S_ISREG(st.st_mode)) rc = 2 ;  
0020         else                         rc = 3 ; 
0021     }
0022     return rc ;  
0023 }
0024 
0025 
0026 
0027 const char* PATHS = R"(
0028 /tmp
0029 /tmp/hello
0030 /tmp/hello/red
0031 /tmp/hello/green
0032 /tmp/hello/blue
0033 /tmp/hello/blue/world
0034 )" ; 
0035 
0036 int main(int argc, char** argv)
0037 {
0038     std::stringstream ss(PATHS); 
0039     std::string path ; 
0040     while(std::getline(ss, path))
0041     {
0042         int pt = pathtype(path.c_str()); 
0043         std::cout << " pt " << std::setw(2) << pt  << " path [" << path << "]" << std::endl ;  
0044     } 
0045     return 0 ; 
0046 }