~ubuntu-branches/ubuntu/jaunty/cmake/jaunty-security

« back to all changes in this revision

Viewing changes to Source/kwsys/SystemTools.cxx

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-06-18 16:34:11 UTC
  • mfrom: (1.4.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060618163411-pi234s3v6jwlcmof
Tags: 2.4.2-1
* New upstream release (Closes: #338324)
* Put cmake .vim files into /usr/share/vim/addons/plugin/
  where they can be used. (Closes: #366663)
* Install cmake-mode.el so it can be used. (Closes: #366664)
* Ensure cmake FindKDE locates KDE libraries on Debian
  based distributions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
}
86
86
#endif
87
87
 
88
 
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__))
 
88
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__) ||defined(__BORLANDC__) || defined(__MINGW32__))
89
89
#include <io.h>
90
90
#include <direct.h>
91
91
#define _unlink unlink
99
99
#else
100
100
# define KWSYS_SYSTEMTOOLS_MAXPATH 16384
101
101
#endif
 
102
#if defined(__WATCOMC__)
 
103
#include <direct.h>
 
104
#define _mkdir mkdir
 
105
#define _rmdir rmdir
 
106
#define _getcwd getcwd
 
107
#define _chdir chdir
 
108
#endif
102
109
 
103
 
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__))
 
110
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__)) 
104
111
inline int Mkdir(const char* dir)
105
112
{
106
113
  return _mkdir(dir);
111
118
}
112
119
inline const char* Getcwd(char* buf, unsigned int len)
113
120
{
114
 
  return _getcwd(buf, len);
 
121
  const char* ret = _getcwd(buf, len);
 
122
  if(!ret)
 
123
    {
 
124
    fprintf(stderr, "No current working directory.\n");
 
125
    abort();
 
126
    }
 
127
  return ret;
115
128
}
116
129
inline int Chdir(const char* dir)
117
130
{
145
158
}
146
159
inline const char* Getcwd(char* buf, unsigned int len)
147
160
{
148
 
  return getcwd(buf, len);
 
161
  const char* ret = getcwd(buf, len);
 
162
  if(!ret)
 
163
    {
 
164
    fprintf(stderr, "No current working directory\n");
 
165
    abort();
 
166
    }
 
167
  return ret;
149
168
}
 
169
 
150
170
inline int Chdir(const char* dir)
151
171
{
152
172
  return chdir(dir);
256
276
    }
257
277
 
258
278
  kwsys_stl::string pathEnv = cpathEnv;
259
 
   
260
 
  // A hack to make the below algorithm work.  
 
279
 
 
280
  // A hack to make the below algorithm work.
261
281
  if(pathEnv[pathEnv.length()-1] != ':')
262
282
    {
263
283
    pathEnv += pathSep;
352
372
    // return EACCES when it should return EEXISTS
353
373
    // if it is some other error besides directory exists
354
374
    // then return false
355
 
    if( (errno != EEXIST) 
 
375
    if( (errno != EEXIST)
356
376
#ifdef __BORLANDC__
357
 
        && (errno != EACCES) 
358
 
#endif      
 
377
        && (errno != EACCES)
 
378
#endif
359
379
      )
360
380
      {
361
381
      return false;
829
849
  kwsys_stl::string n(s);
830
850
  for (size_t i = 0; i < s.size(); i++)
831
851
    {
 
852
#if defined(_MSC_VER) && defined (_MT) && defined (_DEBUG)
 
853
    // MS has an assert that will fail if s[i] < 0; setting
 
854
    // LC_CTYPE using setlocale() does *not* help. Painful.
 
855
    if ((int)s[i] >= 0 && isalpha(s[i]) && 
 
856
        (i == 0 || ((int)s[i - 1] >= 0 && isspace(s[i - 1]))))
 
857
#else
832
858
    if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1])))
 
859
#endif        
833
860
      {
834
861
      n[i] = static_cast<kwsys_stl::string::value_type>(toupper(s[i]));
835
862
      }
843
870
  kwsys_stl::string n(s);
844
871
  for (size_t i = 0; i < s.size(); i++)
845
872
    {
 
873
#if defined(_MSC_VER) && defined (_MT) && defined (_DEBUG)
 
874
    // MS has an assert that will fail if s[i] < 0; setting
 
875
    // LC_CTYPE using setlocale() does *not* help. Painful.
 
876
    if ((int)s[i] >= 0 && isalpha(s[i]) && 
 
877
        (i == 0 || ((int)s[i - 1] >= 0 && isspace(s[i - 1]))))
 
878
#else
846
879
    if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1])))
 
880
#endif        
847
881
      {
848
882
      n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
849
883
      }
1124
1158
}
1125
1159
 
1126
1160
//----------------------------------------------------------------------------
 
1161
kwsys_stl::vector<kwsys::String> SystemTools::SplitString(const char* p, char sep, bool isPath)
 
1162
{
 
1163
  kwsys_stl::string path = p;
 
1164
  kwsys_stl::vector<kwsys::String> paths;
 
1165
  if(isPath && path[0] == '/')
 
1166
    {
 
1167
    path.erase(path.begin());
 
1168
    paths.push_back("/"); 
 
1169
    }
 
1170
  kwsys_stl::string::size_type pos1 = 0;
 
1171
  kwsys_stl::string::size_type pos2 = path.find(sep, pos1+1);
 
1172
  while(pos2 != kwsys_stl::string::npos)
 
1173
    {
 
1174
    paths.push_back(path.substr(pos1, pos2-pos1));
 
1175
    pos1 = pos2+1;
 
1176
    pos2 = path.find(sep, pos1+1);
 
1177
    } 
 
1178
  paths.push_back(path.substr(pos1, pos2-pos1));
 
1179
  
 
1180
  return paths;
 
1181
}
 
1182
 
 
1183
//----------------------------------------------------------------------------
1127
1184
int SystemTools::EstimateFormatLength(const char *format, va_list ap)
1128
1185
{
1129
1186
  if (!format)
1136
1193
  
1137
1194
  // Start with the length of the format string itself.
1138
1195
 
1139
 
  int length = strlen(format);
 
1196
  size_t length = strlen(format);
1140
1197
  
1141
1198
  // Increase the length for every argument in the format.
1142
1199
 
1189
1246
      }
1190
1247
    }
1191
1248
  
1192
 
  return length;
 
1249
  return (int)length;
1193
1250
}
1194
1251
 
1195
1252
kwsys_stl::string SystemTools::EscapeChars(
1389
1446
  return true;
1390
1447
}
1391
1448
 
1392
 
  
 
1449
#define KWSYS_ST_BUFFER 4096
 
1450
 
1393
1451
bool SystemTools::FilesDiffer(const char* source,
1394
1452
                                const char* destination)
1395
1453
{
1429
1487
    return true;
1430
1488
    }
1431
1489
 
1432
 
  char* source_buf = new char[statSource.st_size];
1433
 
  char* dest_buf = new char[statSource.st_size];
1434
 
 
1435
 
  finSource.read(source_buf, statSource.st_size);
1436
 
  finDestination.read(dest_buf, statSource.st_size);
1437
 
 
1438
 
  if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
1439
 
     statSource.st_size != static_cast<long>(finDestination.gcount()))
 
1490
  // Compare the files a block at a time.
 
1491
  char source_buf[KWSYS_ST_BUFFER];
 
1492
  char dest_buf[KWSYS_ST_BUFFER];
 
1493
  long nleft = statSource.st_size;
 
1494
  while(nleft > 0)
1440
1495
    {
1441
 
    // Failed to read files.
1442
 
    delete [] source_buf;
1443
 
    delete [] dest_buf;
1444
 
    return true;
 
1496
    // Read a block from each file.
 
1497
    long nnext = (nleft > KWSYS_ST_BUFFER)? KWSYS_ST_BUFFER : nleft;
 
1498
    finSource.read(source_buf, nnext);
 
1499
    finDestination.read(dest_buf, nnext);
 
1500
 
 
1501
    // If either failed to read assume they are different.
 
1502
    if(static_cast<long>(finSource.gcount()) != nnext ||
 
1503
       static_cast<long>(finDestination.gcount()) != nnext)
 
1504
      {
 
1505
      return true;
 
1506
      }
 
1507
 
 
1508
    // If this block differs the file differs.
 
1509
    if(memcmp((const void*)source_buf, (const void*)dest_buf, nnext) != 0)
 
1510
      {
 
1511
      return true;
 
1512
      }
 
1513
 
 
1514
    // Update the byte count remaining.
 
1515
    nleft -= nnext;
1445
1516
    }
1446
 
  int ret = memcmp((const void*)source_buf, 
1447
 
                   (const void*)dest_buf, 
1448
 
                   statSource.st_size);
1449
 
 
1450
 
  delete [] dest_buf;
1451
 
  delete [] source_buf;
1452
 
 
1453
 
  return ret != 0;
 
1517
 
 
1518
  // No differences found.
 
1519
  return false;
1454
1520
}
1455
1521
 
1456
1522
 
1571
1637
  return true;
1572
1638
}
1573
1639
 
 
1640
//----------------------------------------------------------------------------
 
1641
bool SystemTools::CopyAFile(const char* source, const char* destination,
 
1642
                            bool always)
 
1643
{
 
1644
  if(always)
 
1645
    {
 
1646
    return SystemTools::CopyFileAlways(source, destination);
 
1647
    }
 
1648
  else
 
1649
    {
 
1650
    return SystemTools::CopyFileIfDifferent(source, destination);
 
1651
    }
 
1652
}
 
1653
 
1574
1654
/**
1575
1655
 * Copy a directory content from "source" directory to the directory named by
1576
1656
 * "destination".
1577
1657
 */
1578
 
bool SystemTools::CopyADirectory(const char* source, const char* destination)
 
1658
bool SystemTools::CopyADirectory(const char* source, const char* destination,
 
1659
                                 bool always)
1579
1660
{
1580
1661
  Directory dir;
1581
1662
  dir.Load(source);
1597
1678
        kwsys_stl::string fullDestPath = destination;
1598
1679
        fullDestPath += "/";
1599
1680
        fullDestPath += dir.GetFile(static_cast<unsigned long>(fileNum));
1600
 
        if (!SystemTools::CopyADirectory(fullPath.c_str(), fullDestPath.c_str()))
 
1681
        if (!SystemTools::CopyADirectory(fullPath.c_str(),
 
1682
                                         fullDestPath.c_str(),
 
1683
                                         always))
1601
1684
          {
1602
1685
          return false;
1603
1686
          }
1604
1687
        }
1605
1688
      else
1606
1689
        {
1607
 
        if(!SystemTools::CopyFileAlways(fullPath.c_str(), destination))
 
1690
        if(!SystemTools::CopyAFile(fullPath.c_str(), destination, always))
1608
1691
          {
1609
1692
          return false;
1610
1693
          }
1632
1715
 
1633
1716
int SystemTools::Strucmp(const char *s1, const char *s2)
1634
1717
{
1635
 
// lifted from Graphvis http://www.graphviz.org 
1636
 
  while ((*s1 != '\0') 
 
1718
  // lifted from Graphvis http://www.graphviz.org
 
1719
  while ((*s1 != '\0')
1637
1720
         && (tolower(*s1) == tolower(*s2)))
1638
1721
    {
1639
1722
      s1++;
1640
1723
      s2++;
1641
1724
    }
1642
 
 
 
1725
 
1643
1726
  return tolower(*s1) - tolower(*s2);
1644
 
 
1645
1727
}
1646
1728
 
1647
1729
// return file's modified time
1648
1730
long int SystemTools::ModifiedTime(const char* filename)
1649
1731
{
1650
1732
  struct stat fs;
1651
 
  if (stat(filename, &fs) != 0) 
 
1733
  if (stat(filename, &fs) != 0)
1652
1734
    {
1653
1735
    return 0;
1654
1736
    }
1662
1744
long int SystemTools::CreationTime(const char* filename)
1663
1745
{
1664
1746
  struct stat fs;
1665
 
  if (stat(filename, &fs) != 0) 
 
1747
  if (stat(filename, &fs) != 0)
1666
1748
    {
1667
1749
    return 0;
1668
1750
    }
1684
1766
  // __DATE__
1685
1767
  // The compilation date of the current source file. The date is a string
1686
1768
  // literal of the form Mmm dd yyyy. The month name Mmm is the same as for
1687
 
  // dates generated by the library function asctime declared in TIME.H. 
 
1769
  // dates generated by the library function asctime declared in TIME.H.
1688
1770
 
1689
1771
  // index:   012345678901
1690
1772
  // format:  Mmm dd yyyy
1730
1812
  struct tm tmt2;
1731
1813
 
1732
1814
  // __TIMESTAMP__
1733
 
  // The date and time of the last modification of the current source file, 
1734
 
  // expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy, 
1735
 
  /// where Ddd is the abbreviated day of the week and Date is an integer 
 
1815
  // The date and time of the last modification of the current source file,
 
1816
  // expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy,
 
1817
  /// where Ddd is the abbreviated day of the week and Date is an integer
1736
1818
  // from 1 to 31.
1737
1819
 
1738
1820
  // index:   0123456789
1836
1918
}
1837
1919
 
1838
1920
/**
 
1921
 */
 
1922
size_t SystemTools::GetMaximumFilePathLength()
 
1923
{
 
1924
  return KWSYS_SYSTEMTOOLS_MAXPATH;
 
1925
}
 
1926
 
 
1927
/**
1839
1928
 * Find the file the given name.  Searches the given path and then
1840
1929
 * the system search path.  Returns the full path to the file if it is
1841
1930
 * found.  Otherwise, the empty string is returned.
1842
1931
 */
1843
1932
kwsys_stl::string SystemTools
1844
 
::FindFile(const char* name, 
1845
 
           const kwsys_stl::vector<kwsys_stl::string>& userPaths)
 
1933
::FindName(const char* name,
 
1934
           const kwsys_stl::vector<kwsys_stl::string>& userPaths,
 
1935
           bool no_system_path)
1846
1936
{
1847
1937
  // Add the system search path to our path first
1848
 
  kwsys_stl::vector<kwsys_stl::string> path; 
1849
 
  SystemTools::GetPath(path, "CMAKE_FILE_PATH");
1850
 
  SystemTools::GetPath(path);
 
1938
  kwsys_stl::vector<kwsys_stl::string> path;
 
1939
  if (!no_system_path) 
 
1940
    {
 
1941
    SystemTools::GetPath(path, "CMAKE_FILE_PATH");
 
1942
    SystemTools::GetPath(path);
 
1943
    }
1851
1944
  // now add the additional paths
1852
 
  path.insert(path.end(), userPaths.begin(), userPaths.end());
 
1945
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = userPaths.begin();
 
1946
        i != userPaths.end(); ++i)
 
1947
    {
 
1948
    path.push_back(*i);
 
1949
    }
1853
1950
  // now look for the file
1854
1951
  kwsys_stl::string tryPath;
1855
1952
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
1858
1955
    tryPath = *p;
1859
1956
    tryPath += "/";
1860
1957
    tryPath += name;
1861
 
    if(SystemTools::FileExists(tryPath.c_str()) &&
1862
 
      !SystemTools::FileIsDirectory(tryPath.c_str()))
 
1958
    if(SystemTools::FileExists(tryPath.c_str()))
1863
1959
      {
1864
 
      return SystemTools::CollapseFullPath(tryPath.c_str());
 
1960
      return tryPath;
1865
1961
      }
1866
1962
    }
1867
1963
  // Couldn't find the file.
1869
1965
}
1870
1966
 
1871
1967
/**
 
1968
 * Find the file the given name.  Searches the given path and then
 
1969
 * the system search path.  Returns the full path to the file if it is
 
1970
 * found.  Otherwise, the empty string is returned.
 
1971
 */
 
1972
kwsys_stl::string SystemTools
 
1973
::FindFile(const char* name,
 
1974
           const kwsys_stl::vector<kwsys_stl::string>& userPaths,
 
1975
           bool no_system_path)
 
1976
{
 
1977
  kwsys_stl::string tryPath = SystemTools::FindName(name, userPaths, no_system_path);
 
1978
  if(tryPath != "" && !SystemTools::FileIsDirectory(tryPath.c_str()))
 
1979
    {
 
1980
    return SystemTools::CollapseFullPath(tryPath.c_str());
 
1981
    }
 
1982
  // Couldn't find the file.
 
1983
  return "";
 
1984
}
 
1985
 
 
1986
/**
 
1987
 * Find the directory the given name.  Searches the given path and then
 
1988
 * the system search path.  Returns the full path to the directory if it is
 
1989
 * found.  Otherwise, the empty string is returned.
 
1990
 */
 
1991
kwsys_stl::string SystemTools
 
1992
::FindDirectory(const char* name,
 
1993
                const kwsys_stl::vector<kwsys_stl::string>& userPaths,
 
1994
                bool no_system_path)
 
1995
{
 
1996
  kwsys_stl::string tryPath = SystemTools::FindName(name, userPaths, no_system_path);
 
1997
  if(tryPath != "" && SystemTools::FileIsDirectory(tryPath.c_str()))
 
1998
    {
 
1999
    return SystemTools::CollapseFullPath(tryPath.c_str());
 
2000
    }
 
2001
  // Couldn't find the file.
 
2002
  return "";
 
2003
}
 
2004
 
 
2005
/**
1872
2006
 * Find the executable with the given name.  Searches the given path and then
1873
2007
 * the system search path.  Returns the full path to the executable if it is
1874
2008
 * found.  Otherwise, the empty string is returned.
1875
2009
 */
1876
2010
kwsys_stl::string SystemTools::FindProgram(
1877
 
  const char* name,
 
2011
  const char* nameIn,
1878
2012
  const kwsys_stl::vector<kwsys_stl::string>& userPaths,
1879
2013
  bool no_system_path)
1880
2014
{
1881
 
  if(!name)
 
2015
  if(!nameIn || !*nameIn)
1882
2016
    {
1883
2017
    return "";
1884
2018
    }
1885
 
  // See if the executable exists as written.
1886
 
  if(SystemTools::FileExists(name) &&
1887
 
      !SystemTools::FileIsDirectory(name))
1888
 
    {
1889
 
    return SystemTools::CollapseFullPath(name);
1890
 
    }
1891
 
  kwsys_stl::string tryPath = name;
1892
 
  tryPath += SystemTools::GetExecutableExtension();
1893
 
  if(SystemTools::FileExists(tryPath.c_str()) &&
1894
 
     !SystemTools::FileIsDirectory(tryPath.c_str()))
1895
 
    {
1896
 
    return SystemTools::CollapseFullPath(tryPath.c_str());
1897
 
    }
 
2019
  kwsys_stl::string name = nameIn;
 
2020
  kwsys_stl::vector<kwsys_stl::string> extensions;
 
2021
#if defined (_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
 
2022
  bool hasExtension = false;
 
2023
  // check to see if the name already has a .xxx at
 
2024
  // the end of it
 
2025
  if(name.size() > 3 && name[name.size()-4] == '.')
 
2026
    {
 
2027
    hasExtension = true;
 
2028
    }
 
2029
  // on windows try .com then .exe
 
2030
  if(!hasExtension)
 
2031
    {
 
2032
    extensions.push_back(".com");
 
2033
    extensions.push_back(".exe");
 
2034
    }
 
2035
#endif
 
2036
  kwsys_stl::string tryPath;
 
2037
 
 
2038
  // first try with extensions if the os supports them
 
2039
  if(extensions.size())
 
2040
    {
 
2041
    for(kwsys_stl::vector<kwsys_stl::string>::iterator i = 
 
2042
          extensions.begin(); i != extensions.end(); ++i)
 
2043
      {
 
2044
      tryPath = name;
 
2045
      tryPath += *i;
 
2046
      if(SystemTools::FileExists(tryPath.c_str()) &&
 
2047
         !SystemTools::FileIsDirectory(tryPath.c_str()))
 
2048
        {
 
2049
        return SystemTools::CollapseFullPath(tryPath.c_str());
 
2050
        }
 
2051
      }
 
2052
    }
 
2053
  // now try just the name
 
2054
    tryPath = name;
 
2055
    if(SystemTools::FileExists(tryPath.c_str()) &&
 
2056
       !SystemTools::FileIsDirectory(tryPath.c_str()))
 
2057
      {
 
2058
      return SystemTools::CollapseFullPath(tryPath.c_str());
 
2059
      }
 
2060
  // now construct the path
1898
2061
  kwsys_stl::vector<kwsys_stl::string> path;
1899
 
  SystemTools::GetPath(path, "CMAKE_PROGRAM_PATH");
1900
2062
  // Add the system search path to our path.
1901
2063
  if (!no_system_path)
1902
2064
    {
1903
2065
    SystemTools::GetPath(path);
1904
2066
    }
1905
 
  
1906
2067
  // now add the additional paths
1907
 
  path.insert(path.end(), userPaths.begin(), userPaths.end());
1908
 
  
1909
 
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
 
2068
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = 
 
2069
        userPaths.begin();  i != userPaths.end(); ++i)
 
2070
    {
 
2071
    path.push_back(*i);
 
2072
    }
 
2073
  // Try each path
 
2074
  for(kwsys_stl::vector<kwsys_stl::string>::iterator p = path.begin();
1910
2075
      p != path.end(); ++p)
1911
2076
    {
1912
 
    tryPath = *p;
1913
 
    tryPath += "/";
1914
 
    tryPath += name;
1915
 
    if(SystemTools::FileExists(tryPath.c_str()) &&
1916
 
      !SystemTools::FileIsDirectory(tryPath.c_str()))
1917
 
      {
1918
 
      return SystemTools::CollapseFullPath(tryPath.c_str());
1919
 
      }
1920
2077
#ifdef _WIN32
1921
 
    tryPath += ".com";
1922
 
    if(SystemTools::FileExists(tryPath.c_str()) &&
1923
 
       !SystemTools::FileIsDirectory(tryPath.c_str()))
1924
 
      {
1925
 
      return SystemTools::CollapseFullPath(tryPath.c_str());
1926
 
      }
1927
 
    tryPath = *p;
1928
 
    tryPath += "/";
1929
 
    tryPath += name;
 
2078
    // Remove double quotes from the path on windows
 
2079
    SystemTools::ReplaceString(*p, "\"", "");
1930
2080
#endif
1931
 
    tryPath += SystemTools::GetExecutableExtension();
1932
 
    if(SystemTools::FileExists(tryPath.c_str()) &&
1933
 
       !SystemTools::FileIsDirectory(tryPath.c_str()))
 
2081
    // first try with extensions
 
2082
    if(extensions.size())
1934
2083
      {
1935
 
      return SystemTools::CollapseFullPath(tryPath.c_str());
 
2084
      for(kwsys_stl::vector<kwsys_stl::string>::iterator ext 
 
2085
            = extensions.begin(); ext != extensions.end(); ++ext)
 
2086
        {
 
2087
        tryPath = *p;
 
2088
        tryPath += "/";
 
2089
        tryPath += name;
 
2090
        tryPath += *ext;
 
2091
        if(SystemTools::FileExists(tryPath.c_str()) &&
 
2092
           !SystemTools::FileIsDirectory(tryPath.c_str()))
 
2093
          {
 
2094
          return SystemTools::CollapseFullPath(tryPath.c_str());
 
2095
          }
 
2096
        }
 
2097
      }
 
2098
    // now try it without them
 
2099
      tryPath = *p;
 
2100
      tryPath += "/";
 
2101
      tryPath += name;
 
2102
      if(SystemTools::FileExists(tryPath.c_str()) &&
 
2103
         !SystemTools::FileIsDirectory(tryPath.c_str()))
 
2104
        {
 
2105
        return SystemTools::CollapseFullPath(tryPath.c_str());
1936
2106
      }
1937
2107
    }
1938
 
 
1939
2108
  // Couldn't find the program.
1940
2109
  return "";
1941
2110
}
1942
2111
 
 
2112
kwsys_stl::string SystemTools::FindProgram(
 
2113
  const kwsys_stl::vector<kwsys_stl::string>& names,
 
2114
  const kwsys_stl::vector<kwsys_stl::string>& path,
 
2115
  bool noSystemPath)
 
2116
{
 
2117
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator it = names.begin();
 
2118
      it != names.end() ; ++it)
 
2119
    {
 
2120
    // Try to find the program.
 
2121
    kwsys_stl::string result = SystemTools::FindProgram(it->c_str(),
 
2122
                                                  path,
 
2123
                                                  noSystemPath);
 
2124
    if ( !result.empty() )
 
2125
      {
 
2126
      return result;
 
2127
      }
 
2128
    }
 
2129
  return "";
 
2130
}
1943
2131
 
1944
2132
/**
1945
2133
 * Find the library with the given name.  Searches the given path and then
1956
2144
    {
1957
2145
    return SystemTools::CollapseFullPath(name);
1958
2146
    }
1959
 
  
 
2147
 
1960
2148
  // Add the system search path to our path.
1961
2149
  kwsys_stl::vector<kwsys_stl::string> path;
1962
2150
  SystemTools::GetPath(path);
1963
2151
   // now add the additional paths
1964
 
  path.insert(path.end(), userPaths.begin(), userPaths.end());
 
2152
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = userPaths.begin();
 
2153
        i != userPaths.end(); ++i)
 
2154
    {
 
2155
    path.push_back(*i);
 
2156
    }
1965
2157
  kwsys_stl::string tryPath;
1966
2158
  for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
1967
2159
      p != path.end(); ++p)
1968
2160
    {
 
2161
#if defined(__APPLE__)
 
2162
    tryPath = *p;
 
2163
    tryPath += "/";
 
2164
    tryPath += name;
 
2165
    tryPath += ".framework";
 
2166
    if(SystemTools::FileExists(tryPath.c_str())
 
2167
       && SystemTools::FileIsDirectory(tryPath.c_str()))
 
2168
      {
 
2169
      return SystemTools::CollapseFullPath(tryPath.c_str());
 
2170
      }
 
2171
#endif
1969
2172
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
1970
2173
    tryPath = *p;
1971
2174
    tryPath += "/";
1972
2175
    tryPath += name;
1973
2176
    tryPath += ".lib";
1974
 
    if(SystemTools::FileExists(tryPath.c_str()) 
 
2177
    if(SystemTools::FileExists(tryPath.c_str())
1975
2178
       && !SystemTools::FileIsDirectory(tryPath.c_str()))
1976
2179
      {
1977
2180
      return SystemTools::CollapseFullPath(tryPath.c_str());
1981
2184
    tryPath += "/lib";
1982
2185
    tryPath += name;
1983
2186
    tryPath += ".so";
1984
 
    if(SystemTools::FileExists(tryPath.c_str()) 
 
2187
    if(SystemTools::FileExists(tryPath.c_str())
1985
2188
       && !SystemTools::FileIsDirectory(tryPath.c_str()))
1986
2189
      {
1987
2190
      return SystemTools::CollapseFullPath(tryPath.c_str());
1990
2193
    tryPath += "/lib";
1991
2194
    tryPath += name;
1992
2195
    tryPath += ".a";
1993
 
    if(SystemTools::FileExists(tryPath.c_str()) 
 
2196
    if(SystemTools::FileExists(tryPath.c_str())
1994
2197
       && !SystemTools::FileIsDirectory(tryPath.c_str()))
1995
2198
      {
1996
2199
      return SystemTools::CollapseFullPath(tryPath.c_str());
1999
2202
    tryPath += "/lib";
2000
2203
    tryPath += name;
2001
2204
    tryPath += ".sl";
2002
 
    if(SystemTools::FileExists(tryPath.c_str()) 
 
2205
    if(SystemTools::FileExists(tryPath.c_str())
2003
2206
       && !SystemTools::FileIsDirectory(tryPath.c_str()))
2004
2207
      {
2005
2208
      return SystemTools::CollapseFullPath(tryPath.c_str());
2008
2211
    tryPath += "/lib";
2009
2212
    tryPath += name;
2010
2213
    tryPath += ".dylib";
2011
 
    if(SystemTools::FileExists(tryPath.c_str()) 
 
2214
    if(SystemTools::FileExists(tryPath.c_str())
2012
2215
       && !SystemTools::FileIsDirectory(tryPath.c_str()))
2013
2216
      {
2014
2217
      return SystemTools::CollapseFullPath(tryPath.c_str());
2017
2220
    tryPath += "/lib";
2018
2221
    tryPath += name;
2019
2222
    tryPath += ".dll";
2020
 
    if(SystemTools::FileExists(tryPath.c_str()) 
 
2223
    if(SystemTools::FileExists(tryPath.c_str())
2021
2224
       && !SystemTools::FileIsDirectory(tryPath.c_str()))
2022
2225
      {
2023
2226
      return SystemTools::CollapseFullPath(tryPath.c_str());
2024
2227
      }
2025
2228
#endif
2026
2229
    }
2027
 
  
 
2230
 
2028
2231
  // Couldn't find the library.
2029
2232
  return "";
2030
2233
}
2031
2234
 
2032
2235
bool SystemTools::FileIsDirectory(const char* name)
2033
 
{  
 
2236
{
2034
2237
  struct stat fs;
2035
2238
  if(stat(name, &fs) == 0)
2036
2239
    {
2047
2250
}
2048
2251
 
2049
2252
bool SystemTools::FileIsSymlink(const char* name)
2050
 
{  
 
2253
{
2051
2254
#if _WIN32
2052
2255
  (void)name;
2053
2256
  return false;
2100
2303
  dir = in_name;
2101
2304
  file = "";
2102
2305
  SystemTools::ConvertToUnixSlashes(dir);
2103
 
  
 
2306
 
2104
2307
  if(!SystemTools::FileIsDirectory(dir.c_str()))
2105
2308
    {
2106
2309
    kwsys_stl::string::size_type slashPos = dir.rfind("/");
2125
2328
  return true;
2126
2329
}
2127
2330
 
2128
 
bool SystemTools::FindProgramPath(const char* argv0, 
 
2331
bool SystemTools::FindProgramPath(const char* argv0,
2129
2332
                                  kwsys_stl::string& pathOut,
2130
2333
                                  kwsys_stl::string& errorMsg,
2131
2334
                                  const char* exeName,
2132
 
                                  const char* buildDir,         
 
2335
                                  const char* buildDir,
2133
2336
                                  const char* installPrefix )
2134
2337
{
2135
2338
  kwsys_stl::vector<kwsys_stl::string> failures;
2136
 
  kwsys_stl::string self = argv0;
 
2339
  kwsys_stl::string self = argv0 ? argv0 : "";
 
2340
  failures.push_back(self);
2137
2341
  SystemTools::ConvertToUnixSlashes(self);
2138
 
  failures.push_back(argv0);
2139
2342
  self = SystemTools::FindProgram(self.c_str());
2140
2343
  if(!SystemTools::FileExists(self.c_str()))
2141
2344
    {
2167
2370
    {
2168
2371
    failures.push_back(self);
2169
2372
    kwsys_ios::ostringstream msg;
2170
 
    msg << "Can not find the command line program " << exeName << "\n";
2171
 
    msg << "  argv[0] = \"" << argv0 << "\"\n";
 
2373
    msg << "Can not find the command line program ";
 
2374
    if (exeName)
 
2375
      {
 
2376
      msg << exeName;
 
2377
      }
 
2378
    msg << "\n";
 
2379
    if (argv0)
 
2380
      {
 
2381
      msg << "  argv[0] = \"" << argv0 << "\"\n";
 
2382
      }
2172
2383
    msg << "  Attempted paths:\n";
2173
2384
    kwsys_stl::vector<kwsys_stl::string>::iterator i;
2174
2385
    for(i=failures.begin(); i != failures.end(); ++i)
2199
2410
  if( SystemTools::FileIsDirectory( path_a.c_str() ) )
2200
2411
    {
2201
2412
    // Make sure the path is a full path and does not contain no '..'
2202
 
    if( SystemTools::FileIsFullPath(path_b.c_str()) && path_b.find("..") 
 
2413
    if( SystemTools::FileIsFullPath(path_b.c_str()) && path_b.find("..")
2203
2414
        == kwsys_stl::string::npos )
2204
2415
      {
2205
2416
      // Before inserting make sure path ends with '/'
2206
2417
      if(path_a.size() && path_a[path_a.size() -1] != '/')
2207
2418
        {
2208
 
        path_a += '/'; 
 
2419
        path_a += '/';
2209
2420
        }
2210
2421
      if(path_b.size() && path_b[path_b.size() -1] != '/')
2211
2422
        {
2334
2545
  SystemTools::CheckTranslationPath(newPath);
2335
2546
#ifdef _WIN32
2336
2547
  newPath = SystemTools::GetActualCaseForPath(newPath.c_str());
 
2548
  SystemTools::ConvertToUnixSlashes(newPath);
2337
2549
#endif
2338
2550
  // Return the reconstructed path.
2339
2551
  return newPath;
2340
2552
}
2341
2553
 
 
2554
// compute the relative path from here to there
 
2555
kwsys_stl::string SystemTools::RelativePath(const char* local, const char* remote)
 
2556
{
 
2557
  if(!SystemTools::FileIsFullPath(local))
 
2558
    {
 
2559
    return "";
 
2560
    }
 
2561
  if(!SystemTools::FileIsFullPath(remote))
 
2562
    {
 
2563
    return "";
 
2564
    }
 
2565
 
 
2566
  // split up both paths into arrays of strings using / as a separator
 
2567
  kwsys_stl::vector<kwsys::String> localSplit = SystemTools::SplitString(local, '/', true); 
 
2568
  kwsys_stl::vector<kwsys::String> remoteSplit = SystemTools::SplitString(remote, '/', true);
 
2569
  kwsys_stl::vector<kwsys::String> commonPath; // store shared parts of path in this array
 
2570
  kwsys_stl::vector<kwsys::String> finalPath;  // store the final relative path here
 
2571
  // count up how many matching directory names there are from the start
 
2572
  unsigned int sameCount = 0;
 
2573
  while(
 
2574
    ((sameCount <= (localSplit.size()-1)) && (sameCount <= (remoteSplit.size()-1)))
 
2575
    &&
 
2576
// for windows and apple do a case insensitive string compare
 
2577
#if defined(_WIN32) || defined(__APPLE__)
 
2578
    SystemTools::Strucmp(localSplit[sameCount].c_str(),
 
2579
                         remoteSplit[sameCount].c_str()) == 0
 
2580
#else
 
2581
    localSplit[sameCount] == remoteSplit[sameCount]
 
2582
#endif
 
2583
    )
 
2584
    {
 
2585
    // put the common parts of the path into the commonPath array
 
2586
    commonPath.push_back(localSplit[sameCount]);
 
2587
    // erase the common parts of the path from the original path arrays
 
2588
    localSplit[sameCount] = "";
 
2589
    remoteSplit[sameCount] = "";
 
2590
    sameCount++;
 
2591
    }
 
2592
  // If there is nothing in common but the root directory, then just
 
2593
  // return the full path.
 
2594
  if(sameCount <= 1)
 
2595
    {
 
2596
    return remote;
 
2597
    }
 
2598
  
 
2599
  // for each entry that is not common in the local path
 
2600
  // add a ../ to the finalpath array, this gets us out of the local
 
2601
  // path into the remote dir
 
2602
  for(unsigned int i = 0; i < localSplit.size(); ++i)
 
2603
    {
 
2604
    if(localSplit[i].size())
 
2605
      {
 
2606
      finalPath.push_back("../");
 
2607
      }
 
2608
    }
 
2609
  // for each entry that is not common in the remote path add it
 
2610
  // to the final path.
 
2611
  for(kwsys_stl::vector<String>::iterator vit = remoteSplit.begin();
 
2612
      vit != remoteSplit.end(); ++vit)
 
2613
    {
 
2614
    if(vit->size())
 
2615
      {
 
2616
      finalPath.push_back(*vit);
 
2617
      }
 
2618
    }
 
2619
  kwsys_stl::string relativePath;     // result string
 
2620
  // now turn the array of directories into a unix path by puttint / 
 
2621
  // between each entry that does not already have one
 
2622
  for(kwsys_stl::vector<String>::iterator vit1 = finalPath.begin();
 
2623
      vit1 != finalPath.end(); ++vit1)
 
2624
    {
 
2625
    if(relativePath.size() && relativePath[relativePath.size()-1] != '/')
 
2626
      {
 
2627
      relativePath += "/";
 
2628
      }
 
2629
    relativePath += *vit1;
 
2630
    }
 
2631
  return relativePath;
 
2632
}
2342
2633
 
2343
2634
// OK, some fun stuff to get the actual case of a given path.
2344
2635
// Basically, you just need to call ShortPath, then GetLongPathName,
2381
2672
    {
2382
2673
    longPath = shortPath;
2383
2674
    }
2384
 
  return longPath.size();
 
2675
  return (int)longPath.size();
2385
2676
}
2386
2677
 
2387
2678
 
2388
2679
int PortableGetLongPathName(const char* pathIn,
2389
2680
                            kwsys_stl::string & longPath)
2390
2681
2391
 
  kwsys_stl::string shortPath;
2392
 
  if(!SystemTools::GetShortPath(pathIn, shortPath))
2393
 
    {
2394
 
    return 0;
2395
 
    }
2396
2682
  HMODULE lh = LoadLibrary("Kernel32.dll");
2397
2683
  if(lh)
2398
2684
    {
2402
2688
      typedef  DWORD (WINAPI * GetLongFunctionPtr) (LPCSTR,LPSTR,DWORD); 
2403
2689
      GetLongFunctionPtr func = (GetLongFunctionPtr)proc;
2404
2690
      char buffer[MAX_PATH+1];
2405
 
      int len = (*func)(shortPath.c_str(), buffer, MAX_PATH+1);
 
2691
      int len = (*func)(pathIn, buffer, MAX_PATH+1);
2406
2692
      if(len == 0 || len > MAX_PATH+1)
2407
2693
        {
2408
2694
        FreeLibrary(lh);
2414
2700
      }
2415
2701
    FreeLibrary(lh);
2416
2702
    }
2417
 
  return OldWindowsGetLongPath(shortPath.c_str(), longPath);
 
2703
  return OldWindowsGetLongPath(pathIn, longPath);
2418
2704
}
2419
2705
#endif
2420
2706
 
2526
2812
bool SystemTools::ComparePath(const char* c1, const char* c2)
2527
2813
{
2528
2814
#if defined(_WIN32) || defined(__APPLE__)
 
2815
# ifdef _MSC_VER
 
2816
  return _stricmp(c1, c2) == 0;
 
2817
# elif defined(__APPLE__) || defined(__GNUC__)
 
2818
  return strcasecmp(c1, c2) == 0;
 
2819
#else
2529
2820
  return SystemTools::Strucmp(c1, c2) == 0;
 
2821
# endif
2530
2822
#else
2531
2823
  return strcmp(c1, c2) == 0;
2532
2824
#endif
3201
3493
  do
3202
3494
    {
3203
3495
    path = SystemTools::GetParentDirectory(path.c_str());
3204
 
    if ( dir == path )
 
3496
    if(SystemTools::ComparePath(dir.c_str(), path.c_str()))
3205
3497
      {
3206
3498
      return true;
3207
3499
      }
3683
3975
        Realpath(pwd_str.c_str(), pwd_path);
3684
3976
        }
3685
3977
 
3686
 
        // Add the translation to keep the logical path name.
3687
 
        if(!cwd_changed.empty() && !pwd_changed.empty())
3688
 
          {
3689
 
          SystemTools::AddTranslationPath(cwd_changed.c_str(),
3690
 
                                          pwd_changed.c_str());
 
3978
      // Add the translation to keep the logical path name.
 
3979
      if(!cwd_changed.empty() && !pwd_changed.empty())
 
3980
        {
 
3981
        SystemTools::AddTranslationPath(cwd_changed.c_str(),
 
3982
                                        pwd_changed.c_str());
3691
3983
        }
3692
3984
      }
3693
3985
    }
3730
4022
} // namespace KWSYS_NAMESPACE
3731
4023
#endif
3732
4024
 
 
4025