SystemTools.cxx 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(SystemTools.hxx)
  12. #include KWSYS_HEADER(Directory.hxx)
  13. #include KWSYS_HEADER(ios/iostream)
  14. #include KWSYS_HEADER(ios/fstream)
  15. #include KWSYS_HEADER(ios/sstream)
  16. #ifdef _MSC_VER
  17. # pragma warning (disable: 4786)
  18. #endif
  19. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. // support for realpath call
  25. #ifndef _WIN32
  26. #include <limits.h>
  27. #include <stdlib.h>
  28. #include <sys/param.h>
  29. #include <sys/wait.h>
  30. #endif
  31. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__))
  32. #include <io.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <windows.h>
  37. #include <direct.h>
  38. #define _unlink unlink
  39. inline int Mkdir(const char* dir)
  40. {
  41. return _mkdir(dir);
  42. }
  43. inline int Rmdir(const char* dir)
  44. {
  45. return _rmdir(dir);
  46. }
  47. inline const char* Getcwd(char* buf, unsigned int len)
  48. {
  49. return _getcwd(buf, len);
  50. }
  51. inline int Chdir(const char* dir)
  52. {
  53. #if defined(__BORLANDC__)
  54. return chdir(dir);
  55. #else
  56. return _chdir(dir);
  57. #endif
  58. }
  59. #else
  60. #include <sys/types.h>
  61. #include <fcntl.h>
  62. #include <unistd.h>
  63. inline int Mkdir(const char* dir)
  64. {
  65. return mkdir(dir, 00777);
  66. }
  67. inline int Rmdir(const char* dir)
  68. {
  69. return rmdir(dir);
  70. }
  71. inline const char* Getcwd(char* buf, unsigned int len)
  72. {
  73. return getcwd(buf, len);
  74. }
  75. inline int Chdir(const char* dir)
  76. {
  77. return chdir(dir);
  78. }
  79. #endif
  80. #if !defined(_WIN32) && defined(__COMO__)
  81. // Hack for como strict mode to avoid defining _SVID_SOURCE or _BSD_SOURCE.
  82. extern "C"
  83. {
  84. extern FILE *popen (__const char *__command, __const char *__modes) __THROW;
  85. extern int pclose (FILE *__stream) __THROW;
  86. extern char *realpath (__const char *__restrict __name,
  87. char *__restrict __resolved) __THROW;
  88. extern char *strdup (__const char *__s) __THROW;
  89. extern int putenv (char *__string) __THROW;
  90. }
  91. #endif
  92. /* Implement floattime() for various platforms */
  93. // Taken from Python 2.1.3
  94. #if defined( _WIN32 ) && !defined( __CYGWIN__ )
  95. # include <sys/timeb.h>
  96. # define HAVE_FTIME
  97. # if defined( __BORLANDC__)
  98. # define FTIME ftime
  99. # define TIMEB timeb
  100. # else // Visual studio?
  101. # define FTIME _ftime
  102. # define TIMEB _timeb
  103. # endif
  104. #elif defined( __CYGWIN__ ) || defined( __linux__ )
  105. # include <sys/time.h>
  106. # include <time.h>
  107. # define HAVE_GETTIMEOFDAY
  108. #endif
  109. namespace KWSYS_NAMESPACE
  110. {
  111. double
  112. SystemTools::GetTime(void)
  113. {
  114. /* There are three ways to get the time:
  115. (1) gettimeofday() -- resolution in microseconds
  116. (2) ftime() -- resolution in milliseconds
  117. (3) time() -- resolution in seconds
  118. In all cases the return value is a float in seconds.
  119. Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  120. fail, so we fall back on ftime() or time().
  121. Note: clock resolution does not imply clock accuracy! */
  122. #ifdef HAVE_GETTIMEOFDAY
  123. {
  124. struct timeval t;
  125. #ifdef GETTIMEOFDAY_NO_TZ
  126. if (gettimeofday(&t) == 0)
  127. return (double)t.tv_sec + t.tv_usec*0.000001;
  128. #else /* !GETTIMEOFDAY_NO_TZ */
  129. if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  130. return (double)t.tv_sec + t.tv_usec*0.000001;
  131. #endif /* !GETTIMEOFDAY_NO_TZ */
  132. }
  133. #endif /* !HAVE_GETTIMEOFDAY */
  134. {
  135. #if defined(HAVE_FTIME)
  136. struct TIMEB t;
  137. ::FTIME(&t);
  138. return (double)t.time + (double)t.millitm * (double)0.001;
  139. #else /* !HAVE_FTIME */
  140. time_t secs;
  141. time(&secs);
  142. return (double)secs;
  143. #endif /* !HAVE_FTIME */
  144. }
  145. }
  146. // adds the elements of the env variable path to the arg passed in
  147. void SystemTools::GetPath(kwsys_stl::vector<kwsys_stl::string>& path, const char* env)
  148. {
  149. #if defined(_WIN32) && !defined(__CYGWIN__)
  150. const char* pathSep = ";";
  151. #else
  152. const char* pathSep = ":";
  153. #endif
  154. if(!env)
  155. {
  156. env = "PATH";
  157. }
  158. const char* cpathEnv = SystemTools::GetEnv(env);
  159. if ( !cpathEnv )
  160. {
  161. return;
  162. }
  163. kwsys_stl::string pathEnv = cpathEnv;
  164. // A hack to make the below algorithm work.
  165. if(pathEnv[pathEnv.length()-1] != ':')
  166. {
  167. pathEnv += pathSep;
  168. }
  169. kwsys_stl::string::size_type start =0;
  170. bool done = false;
  171. while(!done)
  172. {
  173. kwsys_stl::string::size_type endpos = pathEnv.find(pathSep, start);
  174. if(endpos != kwsys_stl::string::npos)
  175. {
  176. path.push_back(pathEnv.substr(start, endpos-start));
  177. start = endpos+1;
  178. }
  179. else
  180. {
  181. done = true;
  182. }
  183. }
  184. for(kwsys_stl::vector<kwsys_stl::string>::iterator i = path.begin();
  185. i != path.end(); ++i)
  186. {
  187. SystemTools::ConvertToUnixSlashes(*i);
  188. }
  189. }
  190. const char* SystemTools::GetEnv(const char* key)
  191. {
  192. return getenv(key);
  193. }
  194. bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
  195. {
  196. const char* v = getenv(key);
  197. if(v)
  198. {
  199. result = v;
  200. return true;
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. }
  207. const char* SystemTools::GetExecutableExtension()
  208. {
  209. #if defined(_WIN32) || defined(__CYGWIN__)
  210. return ".exe";
  211. #else
  212. return "";
  213. #endif
  214. }
  215. bool SystemTools::MakeDirectory(const char* path)
  216. {
  217. if(SystemTools::FileExists(path))
  218. {
  219. return true;
  220. }
  221. kwsys_stl::string dir = path;
  222. if(dir.size() == 0)
  223. {
  224. return false;
  225. }
  226. SystemTools::ConvertToUnixSlashes(dir);
  227. kwsys_stl::string::size_type pos = dir.find(':');
  228. if(pos == kwsys_stl::string::npos)
  229. {
  230. pos = 0;
  231. }
  232. kwsys_stl::string topdir;
  233. while((pos = dir.find('/', pos)) != kwsys_stl::string::npos)
  234. {
  235. topdir = dir.substr(0, pos);
  236. Mkdir(topdir.c_str());
  237. pos++;
  238. }
  239. if(dir[dir.size()-1] == '/')
  240. {
  241. topdir = dir.substr(0, dir.size());
  242. }
  243. else
  244. {
  245. topdir = dir;
  246. }
  247. if(Mkdir(topdir.c_str()) != 0)
  248. {
  249. // There is a bug in the Borland Run time library which makes MKDIR
  250. // return EACCES when it should return EEXISTS
  251. // if it is some other error besides directory exists
  252. // then return false
  253. if( (errno != EEXIST)
  254. #ifdef __BORLANDC__
  255. && (errno != EACCES)
  256. #endif
  257. )
  258. {
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. // replace replace with with as many times as it shows up in source.
  265. // write the result into source.
  266. void SystemTools::ReplaceString(kwsys_stl::string& source,
  267. const char* replace,
  268. const char* with)
  269. {
  270. const char *src = source.c_str();
  271. char *searchPos = const_cast<char *>(strstr(src,replace));
  272. // get out quick if string is not found
  273. if (!searchPos)
  274. {
  275. return;
  276. }
  277. // perform replacements until done
  278. size_t replaceSize = strlen(replace);
  279. char *orig = strdup(src);
  280. char *currentPos = orig;
  281. searchPos = searchPos - src + orig;
  282. // initialize the result
  283. source.erase(source.begin(),source.end());
  284. do
  285. {
  286. *searchPos = '\0';
  287. source += currentPos;
  288. currentPos = searchPos + replaceSize;
  289. // replace
  290. source += with;
  291. searchPos = strstr(currentPos,replace);
  292. }
  293. while (searchPos);
  294. // copy any trailing text
  295. source += currentPos;
  296. free(orig);
  297. }
  298. // Read a registry value.
  299. // Example :
  300. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  301. // => will return the data of the "default" value of the key
  302. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  303. // => will return the data of the "Root" value of the key
  304. #if defined(_WIN32) && !defined(__CYGWIN__)
  305. bool SystemTools::ReadRegistryValue(const char *key, kwsys_stl::string &value)
  306. {
  307. kwsys_stl::string primary = key;
  308. kwsys_stl::string second;
  309. kwsys_stl::string valuename;
  310. size_t start = primary.find("\\");
  311. if (start == kwsys_stl::string::npos)
  312. {
  313. return false;
  314. }
  315. size_t valuenamepos = primary.find(";");
  316. if (valuenamepos != kwsys_stl::string::npos)
  317. {
  318. valuename = primary.substr(valuenamepos+1);
  319. }
  320. second = primary.substr(start+1, valuenamepos-start-1);
  321. primary = primary.substr(0, start);
  322. HKEY primaryKey = HKEY_CURRENT_USER;
  323. if (primary == "HKEY_CURRENT_USER")
  324. {
  325. primaryKey = HKEY_CURRENT_USER;
  326. }
  327. if (primary == "HKEY_CURRENT_CONFIG")
  328. {
  329. primaryKey = HKEY_CURRENT_CONFIG;
  330. }
  331. if (primary == "HKEY_CLASSES_ROOT")
  332. {
  333. primaryKey = HKEY_CLASSES_ROOT;
  334. }
  335. if (primary == "HKEY_LOCAL_MACHINE")
  336. {
  337. primaryKey = HKEY_LOCAL_MACHINE;
  338. }
  339. if (primary == "HKEY_USERS")
  340. {
  341. primaryKey = HKEY_USERS;
  342. }
  343. HKEY hKey;
  344. if(RegOpenKeyEx(primaryKey,
  345. second.c_str(),
  346. 0,
  347. KEY_READ,
  348. &hKey) != ERROR_SUCCESS)
  349. {
  350. return false;
  351. }
  352. else
  353. {
  354. DWORD dwType, dwSize;
  355. dwSize = 1023;
  356. char data[1024];
  357. if(RegQueryValueEx(hKey,
  358. (LPTSTR)valuename.c_str(),
  359. NULL,
  360. &dwType,
  361. (BYTE *)data,
  362. &dwSize) == ERROR_SUCCESS)
  363. {
  364. if (dwType == REG_SZ)
  365. {
  366. value = data;
  367. return true;
  368. }
  369. }
  370. }
  371. return false;
  372. }
  373. #else
  374. bool SystemTools::ReadRegistryValue(const char *, kwsys_stl::string &)
  375. {
  376. return false;
  377. }
  378. #endif
  379. // Write a registry value.
  380. // Example :
  381. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  382. // => will set the data of the "default" value of the key
  383. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  384. // => will set the data of the "Root" value of the key
  385. #if defined(_WIN32) && !defined(__CYGWIN__)
  386. bool SystemTools::WriteRegistryValue(const char *key, const char *value)
  387. {
  388. kwsys_stl::string primary = key;
  389. kwsys_stl::string second;
  390. kwsys_stl::string valuename;
  391. size_t start = primary.find("\\");
  392. if (start == kwsys_stl::string::npos)
  393. {
  394. return false;
  395. }
  396. size_t valuenamepos = primary.find(";");
  397. if (valuenamepos != kwsys_stl::string::npos)
  398. {
  399. valuename = primary.substr(valuenamepos+1);
  400. }
  401. second = primary.substr(start+1, valuenamepos-start-1);
  402. primary = primary.substr(0, start);
  403. HKEY primaryKey = HKEY_CURRENT_USER;
  404. if (primary == "HKEY_CURRENT_USER")
  405. {
  406. primaryKey = HKEY_CURRENT_USER;
  407. }
  408. if (primary == "HKEY_CURRENT_CONFIG")
  409. {
  410. primaryKey = HKEY_CURRENT_CONFIG;
  411. }
  412. if (primary == "HKEY_CLASSES_ROOT")
  413. {
  414. primaryKey = HKEY_CLASSES_ROOT;
  415. }
  416. if (primary == "HKEY_LOCAL_MACHINE")
  417. {
  418. primaryKey = HKEY_LOCAL_MACHINE;
  419. }
  420. if (primary == "HKEY_USERS")
  421. {
  422. primaryKey = HKEY_USERS;
  423. }
  424. HKEY hKey;
  425. DWORD dwDummy;
  426. if(RegCreateKeyEx(primaryKey,
  427. second.c_str(),
  428. 0,
  429. "",
  430. REG_OPTION_NON_VOLATILE,
  431. KEY_WRITE,
  432. NULL,
  433. &hKey,
  434. &dwDummy) != ERROR_SUCCESS)
  435. {
  436. return false;
  437. }
  438. if(RegSetValueEx(hKey,
  439. (LPTSTR)valuename.c_str(),
  440. 0,
  441. REG_SZ,
  442. (CONST BYTE *)value,
  443. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  444. {
  445. return true;
  446. }
  447. return false;
  448. }
  449. #else
  450. bool SystemTools::WriteRegistryValue(const char *, const char *)
  451. {
  452. return false;
  453. }
  454. #endif
  455. // Delete a registry value.
  456. // Example :
  457. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  458. // => will delete the data of the "default" value of the key
  459. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  460. // => will delete the data of the "Root" value of the key
  461. #if defined(_WIN32) && !defined(__CYGWIN__)
  462. bool SystemTools::DeleteRegistryValue(const char *key)
  463. {
  464. kwsys_stl::string primary = key;
  465. kwsys_stl::string second;
  466. kwsys_stl::string valuename;
  467. size_t start = primary.find("\\");
  468. if (start == kwsys_stl::string::npos)
  469. {
  470. return false;
  471. }
  472. size_t valuenamepos = primary.find(";");
  473. if (valuenamepos != kwsys_stl::string::npos)
  474. {
  475. valuename = primary.substr(valuenamepos+1);
  476. }
  477. second = primary.substr(start+1, valuenamepos-start-1);
  478. primary = primary.substr(0, start);
  479. HKEY primaryKey = HKEY_CURRENT_USER;
  480. if (primary == "HKEY_CURRENT_USER")
  481. {
  482. primaryKey = HKEY_CURRENT_USER;
  483. }
  484. if (primary == "HKEY_CURRENT_CONFIG")
  485. {
  486. primaryKey = HKEY_CURRENT_CONFIG;
  487. }
  488. if (primary == "HKEY_CLASSES_ROOT")
  489. {
  490. primaryKey = HKEY_CLASSES_ROOT;
  491. }
  492. if (primary == "HKEY_LOCAL_MACHINE")
  493. {
  494. primaryKey = HKEY_LOCAL_MACHINE;
  495. }
  496. if (primary == "HKEY_USERS")
  497. {
  498. primaryKey = HKEY_USERS;
  499. }
  500. HKEY hKey;
  501. if(RegOpenKeyEx(primaryKey,
  502. second.c_str(),
  503. 0,
  504. KEY_WRITE,
  505. &hKey) != ERROR_SUCCESS)
  506. {
  507. return false;
  508. }
  509. else
  510. {
  511. if(RegDeleteValue(hKey,
  512. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  513. {
  514. return true;
  515. }
  516. }
  517. return false;
  518. }
  519. #else
  520. bool SystemTools::DeleteRegistryValue(const char *)
  521. {
  522. return false;
  523. }
  524. #endif
  525. bool SystemTools::SameFile(const char* file1, const char* file2)
  526. {
  527. #ifdef _WIN32
  528. HANDLE hFile1, hFile2;
  529. hFile1 = CreateFile( file1,
  530. GENERIC_READ,
  531. FILE_SHARE_READ ,
  532. NULL,
  533. OPEN_EXISTING,
  534. FILE_FLAG_BACKUP_SEMANTICS,
  535. NULL
  536. );
  537. hFile2 = CreateFile( file2,
  538. GENERIC_READ,
  539. FILE_SHARE_READ,
  540. NULL,
  541. OPEN_EXISTING,
  542. FILE_FLAG_BACKUP_SEMANTICS,
  543. NULL
  544. );
  545. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  546. {
  547. if(hFile1 != INVALID_HANDLE_VALUE)
  548. {
  549. CloseHandle(hFile1);
  550. }
  551. if(hFile2 != INVALID_HANDLE_VALUE)
  552. {
  553. CloseHandle(hFile2);
  554. }
  555. return false;
  556. }
  557. BY_HANDLE_FILE_INFORMATION fiBuf1;
  558. BY_HANDLE_FILE_INFORMATION fiBuf2;
  559. GetFileInformationByHandle( hFile1, &fiBuf1 );
  560. GetFileInformationByHandle( hFile2, &fiBuf2 );
  561. CloseHandle(hFile1);
  562. CloseHandle(hFile2);
  563. return (fiBuf1.dwVolumeSerialNumber == fiBuf2.dwVolumeSerialNumber &&
  564. fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  565. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  566. #else
  567. struct stat fileStat1, fileStat2;
  568. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  569. {
  570. // see if the files are the same file
  571. // check the device inode and size
  572. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  573. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  574. fileStat2.st_size == fileStat1.st_size
  575. )
  576. {
  577. return true;
  578. }
  579. }
  580. return false;
  581. #endif
  582. }
  583. // return true if the file exists
  584. bool SystemTools::FileExists(const char* filename)
  585. {
  586. #ifdef _MSC_VER
  587. # define access _access
  588. #endif
  589. #ifndef R_OK
  590. # define R_OK 04
  591. #endif
  592. if ( access(filename, R_OK) != 0 )
  593. {
  594. return false;
  595. }
  596. else
  597. {
  598. return true;
  599. }
  600. }
  601. // Return a capitalized string (i.e the first letter is uppercased, all other
  602. // are lowercased)
  603. kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
  604. {
  605. kwsys_stl::string n;
  606. if(s.size() == 0)
  607. {
  608. return n;
  609. }
  610. n.resize(s.size());
  611. n[0] = static_cast<kwsys_stl::string::value_type>(toupper(s[0]));
  612. for (size_t i = 1; i < s.size(); i++)
  613. {
  614. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  615. }
  616. return n;
  617. }
  618. // Return a lower case string
  619. kwsys_stl::string SystemTools::LowerCase(const kwsys_stl::string& s)
  620. {
  621. kwsys_stl::string n;
  622. n.resize(s.size());
  623. for (size_t i = 0; i < s.size(); i++)
  624. {
  625. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  626. }
  627. return n;
  628. }
  629. // Return a lower case string
  630. kwsys_stl::string SystemTools::UpperCase(const kwsys_stl::string& s)
  631. {
  632. kwsys_stl::string n;
  633. n.resize(s.size());
  634. for (size_t i = 0; i < s.size(); i++)
  635. {
  636. n[i] = static_cast<kwsys_stl::string::value_type>(toupper(s[i]));
  637. }
  638. return n;
  639. }
  640. // convert windows slashes to unix slashes
  641. void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path)
  642. {
  643. kwsys_stl::string::size_type pos = 0;
  644. while((pos = path.find('\\', pos)) != kwsys_stl::string::npos)
  645. {
  646. path[pos] = '/';
  647. pos++;
  648. }
  649. // Remove all // from the path just like most unix shells
  650. int start_find = 0;
  651. #ifdef _WIN32
  652. // However, on windows if the first characters are both slashes,
  653. // then keep them that way, so that network paths can be handled.
  654. start_find = 1;
  655. #endif
  656. while((pos = path.find("//", start_find)) != kwsys_stl::string::npos)
  657. {
  658. SystemTools::ReplaceString(path, "//", "/");
  659. }
  660. // remove any trailing slash
  661. if(path.size() > 1 && path[path.size()-1] == '/')
  662. {
  663. path = path.substr(0, path.size()-1);
  664. }
  665. // if there is a tilda ~ then replace it with HOME
  666. if(path.find("~") == 0)
  667. {
  668. if (getenv("HOME"))
  669. {
  670. path = kwsys_stl::string(getenv("HOME")) + path.substr(1);
  671. }
  672. }
  673. // if there is a /tmp_mnt in a path get rid of it!
  674. if(path.find("/tmp_mnt") == 0)
  675. {
  676. path = path.substr(8);
  677. }
  678. }
  679. // change // to /, and escape any spaces in the path
  680. kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const char* path)
  681. {
  682. kwsys_stl::string ret = path;
  683. // remove // except at the beginning might be a cygwin drive
  684. kwsys_stl::string::size_type pos = 1;
  685. while((pos = ret.find("//", pos)) != kwsys_stl::string::npos)
  686. {
  687. ret.erase(pos, 1);
  688. }
  689. // now escape spaces if there is a space in the path
  690. if(ret.find(" ") != kwsys_stl::string::npos)
  691. {
  692. kwsys_stl::string result = "";
  693. char lastch = 1;
  694. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  695. {
  696. // if it is already escaped then don't try to escape it again
  697. if(*ch == ' ' && lastch != '\\')
  698. {
  699. result += '\\';
  700. }
  701. result += *ch;
  702. lastch = *ch;
  703. }
  704. ret = result;
  705. }
  706. return ret;
  707. }
  708. kwsys_stl::string SystemTools::ConvertToOutputPath(const char* path)
  709. {
  710. #if defined(_WIN32) && !defined(__CYGWIN__)
  711. return SystemTools::ConvertToWindowsOutputPath(path);
  712. #else
  713. return SystemTools::ConvertToUnixOutputPath(path);
  714. #endif
  715. }
  716. // remove double slashes not at the start
  717. kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const char* path)
  718. {
  719. kwsys_stl::string ret = path;
  720. kwsys_stl::string::size_type pos = 0;
  721. // first convert all of the slashes
  722. while((pos = ret.find('/', pos)) != kwsys_stl::string::npos)
  723. {
  724. ret[pos] = '\\';
  725. pos++;
  726. }
  727. // check for really small paths
  728. if(ret.size() < 2)
  729. {
  730. return ret;
  731. }
  732. // now clean up a bit and remove double slashes
  733. // Only if it is not the first position in the path which is a network
  734. // path on windows
  735. pos = 1; // start at position 1
  736. if(ret[0] == '\"')
  737. {
  738. pos = 2; // if the string is already quoted then start at 2
  739. if(ret.size() < 3)
  740. {
  741. return ret;
  742. }
  743. }
  744. while((pos = ret.find("\\\\", pos)) != kwsys_stl::string::npos)
  745. {
  746. ret.erase(pos, 1);
  747. }
  748. // now double quote the path if it has spaces in it
  749. // and is not already double quoted
  750. if(ret.find(" ") != kwsys_stl::string::npos
  751. && ret[0] != '\"')
  752. {
  753. kwsys_stl::string result;
  754. result = "\"" + ret + "\"";
  755. ret = result;
  756. }
  757. return ret;
  758. }
  759. bool SystemTools::CopyFileIfDifferent(const char* source,
  760. const char* destination)
  761. {
  762. if(SystemTools::FilesDiffer(source, destination))
  763. {
  764. return SystemTools::CopyFileAlways(source, destination);
  765. }
  766. return true;
  767. }
  768. bool SystemTools::FilesDiffer(const char* source,
  769. const char* destination)
  770. {
  771. struct stat statSource;
  772. if (stat(source, &statSource) != 0)
  773. {
  774. return true;
  775. }
  776. struct stat statDestination;
  777. if (stat(destination, &statDestination) != 0)
  778. {
  779. return true;
  780. }
  781. if(statSource.st_size != statDestination.st_size)
  782. {
  783. return true;
  784. }
  785. if(statSource.st_size == 0)
  786. {
  787. return false;
  788. }
  789. #if defined(_WIN32) || defined(__CYGWIN__)
  790. kwsys_ios::ifstream finSource(source, (kwsys_ios::ios::binary |
  791. kwsys_ios::ios::in));
  792. kwsys_ios::ifstream finDestination(destination, (kwsys_ios::ios::binary |
  793. kwsys_ios::ios::in));
  794. #else
  795. kwsys_ios::ifstream finSource(source);
  796. kwsys_ios::ifstream finDestination(destination);
  797. #endif
  798. if(!finSource || !finDestination)
  799. {
  800. return true;
  801. }
  802. char* source_buf = new char[statSource.st_size];
  803. char* dest_buf = new char[statSource.st_size];
  804. finSource.read(source_buf, statSource.st_size);
  805. finDestination.read(dest_buf, statSource.st_size);
  806. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  807. statSource.st_size != static_cast<long>(finDestination.gcount()))
  808. {
  809. // Failed to read files.
  810. delete [] source_buf;
  811. delete [] dest_buf;
  812. return true;
  813. }
  814. int ret = memcmp((const void*)source_buf,
  815. (const void*)dest_buf,
  816. statSource.st_size);
  817. delete [] dest_buf;
  818. delete [] source_buf;
  819. return ret != 0;
  820. }
  821. /**
  822. * Copy a file named by "source" to the file named by "destination".
  823. */
  824. bool SystemTools::CopyFileAlways(const char* source, const char* destination)
  825. {
  826. // If files are the same do not copy
  827. if ( SystemTools::SameFile(source, destination) )
  828. {
  829. return true;
  830. }
  831. const int bufferSize = 4096;
  832. char buffer[bufferSize];
  833. // If destination is a directory, try to create a file with the same
  834. // name as the source in that directory.
  835. kwsys_stl::string new_destination;
  836. if(SystemTools::FileExists(destination) &&
  837. SystemTools::FileIsDirectory(destination))
  838. {
  839. new_destination = destination;
  840. SystemTools::ConvertToUnixSlashes(new_destination);
  841. new_destination += '/';
  842. kwsys_stl::string source_name = source;
  843. new_destination += SystemTools::GetFilenameName(source_name);
  844. destination = new_destination.c_str();
  845. }
  846. // Create destination directory
  847. kwsys_stl::string destination_dir = destination;
  848. destination_dir = SystemTools::GetFilenamePath(destination_dir);
  849. SystemTools::MakeDirectory(destination_dir.c_str());
  850. // Open files
  851. #if defined(_WIN32) || defined(__CYGWIN__)
  852. kwsys_ios::ifstream fin(source,
  853. kwsys_ios::ios::binary | kwsys_ios::ios::in);
  854. #else
  855. kwsys_ios::ifstream fin(source);
  856. #endif
  857. if(!fin)
  858. {
  859. return false;
  860. }
  861. #if defined(_WIN32) || defined(__CYGWIN__)
  862. kwsys_ios::ofstream fout(destination,
  863. kwsys_ios::ios::binary | kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  864. #else
  865. kwsys_ios::ofstream fout(destination,
  866. kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  867. #endif
  868. if(!fout)
  869. {
  870. return false;
  871. }
  872. // This copy loop is very sensitive on certain platforms with
  873. // slightly broken stream libraries (like HPUX). Normally, it is
  874. // incorrect to not check the error condition on the fin.read()
  875. // before using the data, but the fin.gcount() will be zero if an
  876. // error occurred. Therefore, the loop should be safe everywhere.
  877. while(fin)
  878. {
  879. fin.read(buffer, bufferSize);
  880. if(fin.gcount())
  881. {
  882. fout.write(buffer, fin.gcount());
  883. }
  884. }
  885. // Make sure the operating system has finished writing the file
  886. // before closing it. This will ensure the file is finished before
  887. // the check below.
  888. fout.flush();
  889. fin.close();
  890. fout.close();
  891. // More checks.
  892. struct stat statSource, statDestination;
  893. statSource.st_size = 12345;
  894. statDestination.st_size = 12345;
  895. if(stat(source, &statSource) != 0)
  896. {
  897. return false;
  898. }
  899. else if(stat(destination, &statDestination) != 0)
  900. {
  901. return false;
  902. }
  903. else if(statSource.st_size != statDestination.st_size)
  904. {
  905. return false;
  906. }
  907. return true;
  908. }
  909. // return size of file; also returns zero if no file exists
  910. unsigned long SystemTools::FileLength(const char* filename)
  911. {
  912. struct stat fs;
  913. if (stat(filename, &fs) != 0)
  914. {
  915. return 0;
  916. }
  917. else
  918. {
  919. return fs.st_size;
  920. }
  921. }
  922. int SystemTools::Strucmp(const char *s1, const char *s2)
  923. {
  924. // lifted from Graphvis http://www.graphviz.org
  925. while ((*s1 != '\0')
  926. && (tolower(*s1) == tolower(*s2)))
  927. {
  928. s1++;
  929. s2++;
  930. }
  931. return tolower(*s1) - tolower(*s2);
  932. }
  933. // return true if the file exists
  934. long int SystemTools::ModifiedTime(const char* filename)
  935. {
  936. struct stat fs;
  937. if (stat(filename, &fs) != 0)
  938. {
  939. return 0;
  940. }
  941. else
  942. {
  943. return (long int)fs.st_mtime;
  944. }
  945. }
  946. kwsys_stl::string SystemTools::GetLastSystemError()
  947. {
  948. int e = errno;
  949. return strerror(e);
  950. }
  951. bool SystemTools::RemoveFile(const char* source)
  952. {
  953. return unlink(source) != 0 ? false : true;
  954. }
  955. bool SystemTools::RemoveADirectory(const char* source)
  956. {
  957. Directory dir;
  958. dir.Load(source);
  959. size_t fileNum;
  960. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  961. {
  962. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  963. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  964. {
  965. kwsys_stl::string fullPath = source;
  966. fullPath += "/";
  967. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  968. if(SystemTools::FileIsDirectory(fullPath.c_str()))
  969. {
  970. if (!SystemTools::RemoveADirectory(fullPath.c_str()))
  971. {
  972. return false;
  973. }
  974. }
  975. else
  976. {
  977. if(!SystemTools::RemoveFile(fullPath.c_str()))
  978. {
  979. return false;
  980. }
  981. }
  982. }
  983. }
  984. return (Rmdir(source) == 0);
  985. }
  986. /**
  987. * Find the file the given name. Searches the given path and then
  988. * the system search path. Returns the full path to the file if it is
  989. * found. Otherwise, the empty string is returned.
  990. */
  991. kwsys_stl::string SystemTools::FindFile(const char* name,
  992. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  993. {
  994. // Add the system search path to our path first
  995. kwsys_stl::vector<kwsys_stl::string> path;
  996. SystemTools::GetPath(path, "CMAKE_FILE_PATH");
  997. SystemTools::GetPath(path);
  998. // now add the additional paths
  999. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1000. // now look for the file
  1001. kwsys_stl::string tryPath;
  1002. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1003. p != path.end(); ++p)
  1004. {
  1005. tryPath = *p;
  1006. tryPath += "/";
  1007. tryPath += name;
  1008. if(SystemTools::FileExists(tryPath.c_str()) &&
  1009. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1010. {
  1011. return SystemTools::CollapseFullPath(tryPath.c_str());
  1012. }
  1013. }
  1014. // Couldn't find the file.
  1015. return "";
  1016. }
  1017. /**
  1018. * Find the executable with the given name. Searches the given path and then
  1019. * the system search path. Returns the full path to the executable if it is
  1020. * found. Otherwise, the empty string is returned.
  1021. */
  1022. kwsys_stl::string SystemTools::FindProgram(
  1023. const char* name,
  1024. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1025. bool no_system_path)
  1026. {
  1027. if(!name)
  1028. {
  1029. return "";
  1030. }
  1031. // See if the executable exists as written.
  1032. if(SystemTools::FileExists(name) &&
  1033. !SystemTools::FileIsDirectory(name))
  1034. {
  1035. return SystemTools::CollapseFullPath(name);
  1036. }
  1037. kwsys_stl::string tryPath = name;
  1038. tryPath += SystemTools::GetExecutableExtension();
  1039. if(SystemTools::FileExists(tryPath.c_str()) &&
  1040. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1041. {
  1042. return SystemTools::CollapseFullPath(tryPath.c_str());
  1043. }
  1044. kwsys_stl::vector<kwsys_stl::string> path;
  1045. SystemTools::GetPath(path, "CMAKE_PROGRAM_PATH");
  1046. // Add the system search path to our path.
  1047. if (!no_system_path)
  1048. {
  1049. SystemTools::GetPath(path);
  1050. }
  1051. // now add the additional paths
  1052. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1053. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1054. p != path.end(); ++p)
  1055. {
  1056. tryPath = *p;
  1057. tryPath += "/";
  1058. tryPath += name;
  1059. if(SystemTools::FileExists(tryPath.c_str()) &&
  1060. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1061. {
  1062. return SystemTools::CollapseFullPath(tryPath.c_str());
  1063. }
  1064. #ifdef _WIN32
  1065. tryPath += ".com";
  1066. if(SystemTools::FileExists(tryPath.c_str()) &&
  1067. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1068. {
  1069. return SystemTools::CollapseFullPath(tryPath.c_str());
  1070. }
  1071. tryPath = *p;
  1072. tryPath += "/";
  1073. tryPath += name;
  1074. #endif
  1075. tryPath += SystemTools::GetExecutableExtension();
  1076. if(SystemTools::FileExists(tryPath.c_str()) &&
  1077. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1078. {
  1079. return SystemTools::CollapseFullPath(tryPath.c_str());
  1080. }
  1081. }
  1082. // Couldn't find the program.
  1083. return "";
  1084. }
  1085. /**
  1086. * Find the library with the given name. Searches the given path and then
  1087. * the system search path. Returns the full path to the library if it is
  1088. * found. Otherwise, the empty string is returned.
  1089. */
  1090. kwsys_stl::string SystemTools::FindLibrary(const char* name,
  1091. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  1092. {
  1093. // See if the executable exists as written.
  1094. if(SystemTools::FileExists(name) &&
  1095. !SystemTools::FileIsDirectory(name))
  1096. {
  1097. return SystemTools::CollapseFullPath(name);
  1098. }
  1099. // Add the system search path to our path.
  1100. kwsys_stl::vector<kwsys_stl::string> path;
  1101. SystemTools::GetPath(path);
  1102. // now add the additional paths
  1103. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1104. kwsys_stl::string tryPath;
  1105. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1106. p != path.end(); ++p)
  1107. {
  1108. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  1109. tryPath = *p;
  1110. tryPath += "/";
  1111. tryPath += name;
  1112. tryPath += ".lib";
  1113. if(SystemTools::FileExists(tryPath.c_str())
  1114. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1115. {
  1116. return SystemTools::CollapseFullPath(tryPath.c_str());
  1117. }
  1118. #else
  1119. tryPath = *p;
  1120. tryPath += "/lib";
  1121. tryPath += name;
  1122. tryPath += ".so";
  1123. if(SystemTools::FileExists(tryPath.c_str())
  1124. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1125. {
  1126. return SystemTools::CollapseFullPath(tryPath.c_str());
  1127. }
  1128. tryPath = *p;
  1129. tryPath += "/lib";
  1130. tryPath += name;
  1131. tryPath += ".a";
  1132. if(SystemTools::FileExists(tryPath.c_str())
  1133. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1134. {
  1135. return SystemTools::CollapseFullPath(tryPath.c_str());
  1136. }
  1137. tryPath = *p;
  1138. tryPath += "/lib";
  1139. tryPath += name;
  1140. tryPath += ".sl";
  1141. if(SystemTools::FileExists(tryPath.c_str())
  1142. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1143. {
  1144. return SystemTools::CollapseFullPath(tryPath.c_str());
  1145. }
  1146. tryPath = *p;
  1147. tryPath += "/lib";
  1148. tryPath += name;
  1149. tryPath += ".dylib";
  1150. if(SystemTools::FileExists(tryPath.c_str())
  1151. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1152. {
  1153. return SystemTools::CollapseFullPath(tryPath.c_str());
  1154. }
  1155. tryPath = *p;
  1156. tryPath += "/lib";
  1157. tryPath += name;
  1158. tryPath += ".dll";
  1159. if(SystemTools::FileExists(tryPath.c_str())
  1160. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1161. {
  1162. return SystemTools::CollapseFullPath(tryPath.c_str());
  1163. }
  1164. #endif
  1165. }
  1166. // Couldn't find the library.
  1167. return "";
  1168. }
  1169. bool SystemTools::FileIsDirectory(const char* name)
  1170. {
  1171. struct stat fs;
  1172. if(stat(name, &fs) == 0)
  1173. {
  1174. #if _WIN32
  1175. return ((fs.st_mode & _S_IFDIR) != 0);
  1176. #else
  1177. return S_ISDIR(fs.st_mode);
  1178. #endif
  1179. }
  1180. else
  1181. {
  1182. return false;
  1183. }
  1184. }
  1185. int SystemTools::ChangeDirectory(const char *dir)
  1186. {
  1187. return Chdir(dir);
  1188. }
  1189. kwsys_stl::string SystemTools::GetCurrentWorkingDirectory()
  1190. {
  1191. char buf[2048];
  1192. const char* cwd = Getcwd(buf, 2048);
  1193. kwsys_stl::string path;
  1194. if ( cwd )
  1195. {
  1196. path = cwd;
  1197. }
  1198. return path;
  1199. }
  1200. kwsys_stl::string SystemTools::GetProgramPath(const char* in_name)
  1201. {
  1202. kwsys_stl::string dir, file;
  1203. SystemTools::SplitProgramPath(in_name, dir, file);
  1204. return dir;
  1205. }
  1206. bool SystemTools::SplitProgramPath(const char* in_name,
  1207. kwsys_stl::string& dir,
  1208. kwsys_stl::string& file,
  1209. bool)
  1210. {
  1211. dir = in_name;
  1212. file = "";
  1213. SystemTools::ConvertToUnixSlashes(dir);
  1214. if(!SystemTools::FileIsDirectory(dir.c_str()))
  1215. {
  1216. kwsys_stl::string::size_type slashPos = dir.rfind("/");
  1217. if(slashPos != kwsys_stl::string::npos)
  1218. {
  1219. file = dir.substr(slashPos+1);
  1220. dir = dir.substr(0, slashPos);
  1221. }
  1222. else
  1223. {
  1224. file = dir;
  1225. dir = "";
  1226. }
  1227. }
  1228. if(!(dir == "") && !SystemTools::FileIsDirectory(dir.c_str()))
  1229. {
  1230. kwsys_stl::string oldDir = in_name;
  1231. SystemTools::ConvertToUnixSlashes(oldDir);
  1232. dir = in_name;
  1233. return false;
  1234. }
  1235. return true;
  1236. }
  1237. bool SystemTools::FindProgramPath(const char* argv0,
  1238. kwsys_stl::string& pathOut,
  1239. kwsys_stl::string& errorMsg,
  1240. const char* exeName,
  1241. const char* buildDir,
  1242. const char* installPrefix )
  1243. {
  1244. kwsys_stl::vector<kwsys_stl::string> failures;
  1245. kwsys_stl::string self = argv0;
  1246. SystemTools::ConvertToUnixSlashes(self);
  1247. failures.push_back(argv0);
  1248. self = SystemTools::FindProgram(self.c_str());
  1249. if(!SystemTools::FileExists(self.c_str()))
  1250. {
  1251. if(buildDir)
  1252. {
  1253. kwsys_stl::string intdir = ".";
  1254. #ifdef CMAKE_INTDIR
  1255. intdir = CMAKE_INTDIR;
  1256. #endif
  1257. self = buildDir;
  1258. self += "/bin/";
  1259. self += intdir;
  1260. self += "/";
  1261. self += exeName;
  1262. self += SystemTools::GetExecutableExtension();
  1263. }
  1264. }
  1265. if(installPrefix)
  1266. {
  1267. if(!SystemTools::FileExists(self.c_str()))
  1268. {
  1269. failures.push_back(self);
  1270. self = installPrefix;
  1271. self += "/bin/";
  1272. self += exeName;
  1273. }
  1274. }
  1275. if(!SystemTools::FileExists(self.c_str()))
  1276. {
  1277. failures.push_back(self);
  1278. kwsys_ios::ostringstream msg;
  1279. msg << "Can not find the command line program " << exeName << "\n";
  1280. msg << " argv[0] = \"" << argv0 << "\"\n";
  1281. msg << " Attempted paths:\n";
  1282. kwsys_stl::vector<kwsys_stl::string>::iterator i;
  1283. for(i=failures.begin(); i != failures.end(); ++i)
  1284. {
  1285. msg << " \"" << i->c_str() << "\"\n";
  1286. }
  1287. errorMsg = msg.str();
  1288. return false;
  1289. }
  1290. pathOut = self;
  1291. return true;
  1292. }
  1293. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative)
  1294. {
  1295. return SystemTools::CollapseFullPath(in_relative, 0);
  1296. }
  1297. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative,
  1298. const char* in_base)
  1299. {
  1300. // Save original working directory.
  1301. kwsys_stl::string orig = SystemTools::GetCurrentWorkingDirectory();
  1302. // Change to base of relative path.
  1303. if(in_base)
  1304. {
  1305. Chdir(in_base);
  1306. }
  1307. kwsys_stl::string dir, file;
  1308. SystemTools::SplitProgramPath(in_relative, dir, file, false);
  1309. if(dir.size() == 0 &&
  1310. in_relative && strlen(in_relative) > 0 &&
  1311. in_relative[0] == '/')
  1312. {
  1313. dir = "/";
  1314. }
  1315. #ifdef _WIN32
  1316. // Follow relative path.
  1317. if(!(dir == ""))
  1318. {
  1319. Chdir(dir.c_str());
  1320. }
  1321. // Get the resulting directory.
  1322. kwsys_stl::string newDir = SystemTools::GetCurrentWorkingDirectory();
  1323. // Add the file back on to the directory.
  1324. SystemTools::ConvertToUnixSlashes(newDir);
  1325. #else
  1326. # ifdef MAXPATHLEN
  1327. char resolved_name[MAXPATHLEN];
  1328. # else
  1329. # ifdef PATH_MAX
  1330. char resolved_name[PATH_MAX];
  1331. # else
  1332. char resolved_name[5024];
  1333. # endif
  1334. # endif
  1335. // Resolve relative path.
  1336. kwsys_stl::string newDir;
  1337. if(!(dir == ""))
  1338. {
  1339. realpath(dir.c_str(), resolved_name);
  1340. newDir = resolved_name;
  1341. }
  1342. else
  1343. {
  1344. newDir = SystemTools::GetCurrentWorkingDirectory();
  1345. }
  1346. // if there is a /tmp_mnt in a path get rid of it!
  1347. if(newDir.find("/tmp_mnt") == 0)
  1348. {
  1349. newDir = newDir.substr(8);
  1350. }
  1351. #endif
  1352. // Restore original working directory.
  1353. Chdir(orig.c_str());
  1354. // Construct and return the full path.
  1355. kwsys_stl::string newPath = newDir;
  1356. if(!(file == ""))
  1357. {
  1358. if(!(newDir.size() == 1 && newDir[0] == '/'))
  1359. {
  1360. newPath += "/";
  1361. }
  1362. newPath += file;
  1363. }
  1364. return newPath;
  1365. }
  1366. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines)
  1367. {
  1368. kwsys_stl::string data(str);
  1369. kwsys_stl::string::size_type lpos = 0;
  1370. while(lpos < data.length())
  1371. {
  1372. kwsys_stl::string::size_type rpos = data.find_first_of("\n", lpos);
  1373. if(rpos == kwsys_stl::string::npos)
  1374. {
  1375. // Line ends at end of string without a newline.
  1376. lines.push_back(data.substr(lpos));
  1377. return false;
  1378. }
  1379. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1380. {
  1381. // Line ends in a "\r\n" pair, remove both characters.
  1382. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1383. }
  1384. else
  1385. {
  1386. // Line ends in a "\n", remove the character.
  1387. lines.push_back(data.substr(lpos, rpos-lpos));
  1388. }
  1389. lpos = rpos+1;
  1390. }
  1391. return true;
  1392. }
  1393. /**
  1394. * Return path of a full filename (no trailing slashes).
  1395. * Warning: returned path is converted to Unix slashes format.
  1396. */
  1397. kwsys_stl::string SystemTools::GetFilenamePath(const kwsys_stl::string& filename)
  1398. {
  1399. kwsys_stl::string fn = filename;
  1400. SystemTools::ConvertToUnixSlashes(fn);
  1401. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1402. if(slash_pos != kwsys_stl::string::npos)
  1403. {
  1404. return fn.substr(0, slash_pos);
  1405. }
  1406. else
  1407. {
  1408. return "";
  1409. }
  1410. }
  1411. /**
  1412. * Return file name of a full filename (i.e. file name without path).
  1413. */
  1414. kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename)
  1415. {
  1416. kwsys_stl::string fn = filename;
  1417. SystemTools::ConvertToUnixSlashes(fn);
  1418. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1419. if(slash_pos != kwsys_stl::string::npos)
  1420. {
  1421. return fn.substr(slash_pos + 1);
  1422. }
  1423. else
  1424. {
  1425. return filename;
  1426. }
  1427. }
  1428. /**
  1429. * Return file extension of a full filename (dot included).
  1430. * Warning: this is the longest extension (for example: .tar.gz)
  1431. */
  1432. kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename)
  1433. {
  1434. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1435. kwsys_stl::string::size_type dot_pos = name.find(".");
  1436. if(dot_pos != kwsys_stl::string::npos)
  1437. {
  1438. return name.substr(dot_pos);
  1439. }
  1440. else
  1441. {
  1442. return "";
  1443. }
  1444. }
  1445. /**
  1446. * Return file extension of a full filename (dot included).
  1447. * Warning: this is the shortest extension (for example: .tar.gz)
  1448. */
  1449. kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename)
  1450. {
  1451. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1452. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1453. if(dot_pos != kwsys_stl::string::npos)
  1454. {
  1455. return name.substr(dot_pos);
  1456. }
  1457. else
  1458. {
  1459. return "";
  1460. }
  1461. }
  1462. /**
  1463. * Return file name without extension of a full filename (i.e. without path).
  1464. * Warning: it considers the longest extension (for example: .tar.gz)
  1465. */
  1466. kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename)
  1467. {
  1468. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1469. kwsys_stl::string::size_type dot_pos = name.find(".");
  1470. if(dot_pos != kwsys_stl::string::npos)
  1471. {
  1472. return name.substr(0, dot_pos);
  1473. }
  1474. else
  1475. {
  1476. return name;
  1477. }
  1478. }
  1479. /**
  1480. * Return file name without extension of a full filename (i.e. without path).
  1481. * Warning: it considers the last extension (for example: removes .gz
  1482. * from .tar.gz)
  1483. */
  1484. kwsys_stl::string
  1485. SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename)
  1486. {
  1487. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1488. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1489. if(dot_pos != kwsys_stl::string::npos)
  1490. {
  1491. return name.substr(0, dot_pos);
  1492. }
  1493. else
  1494. {
  1495. return name;
  1496. }
  1497. }
  1498. bool SystemTools::FileIsFullPath(const char* in_name)
  1499. {
  1500. kwsys_stl::string name = in_name;
  1501. #if defined(_WIN32) || defined(__CYGWIN__)
  1502. // On Windows, the name must be at least two characters long.
  1503. if(name.length() < 2)
  1504. {
  1505. return false;
  1506. }
  1507. if(name[1] == ':')
  1508. {
  1509. return true;
  1510. }
  1511. if(name[0] == '\\')
  1512. {
  1513. return true;
  1514. }
  1515. #else
  1516. // On UNIX, the name must be at least one character long.
  1517. if(name.length() < 1)
  1518. {
  1519. return false;
  1520. }
  1521. #endif
  1522. // On UNIX, the name must begin in a '/'.
  1523. // On Windows, if the name begins in a '/', then it is a full
  1524. // network path.
  1525. if(name[0] == '/')
  1526. {
  1527. return true;
  1528. }
  1529. return false;
  1530. }
  1531. bool SystemTools::GetShortPath(const char* path, kwsys_stl::string& shortPath)
  1532. {
  1533. #if defined(WIN32) && !defined(__CYGWIN__)
  1534. const int size = int(strlen(path)) +1; // size of return
  1535. char *buffer = new char[size]; // create a buffer
  1536. char *tempPath = new char[size]; // create a buffer
  1537. int ret;
  1538. // if the path passed in has quotes around it, first remove the quotes
  1539. if (path[0] == '"' && path[strlen(path)-1] == '"')
  1540. {
  1541. strcpy(tempPath,path+1);
  1542. tempPath[strlen(tempPath)-1] = '\0';
  1543. }
  1544. else
  1545. {
  1546. strcpy(tempPath,path);
  1547. }
  1548. buffer[0] = 0;
  1549. ret = GetShortPathName(tempPath, buffer, size);
  1550. if(buffer[0] == 0 || ret > size)
  1551. {
  1552. delete [] buffer;
  1553. delete [] tempPath;
  1554. return false;
  1555. }
  1556. else
  1557. {
  1558. shortPath = buffer;
  1559. delete [] buffer;
  1560. delete [] tempPath;
  1561. return true;
  1562. }
  1563. #else
  1564. shortPath = path;
  1565. return true;
  1566. #endif
  1567. }
  1568. void SystemTools::SplitProgramFromArgs(const char* path,
  1569. kwsys_stl::string& program, kwsys_stl::string& args)
  1570. {
  1571. if(SystemTools::FileExists(path))
  1572. {
  1573. program = path;
  1574. args = "";
  1575. return;
  1576. }
  1577. kwsys_stl::vector<kwsys_stl::string> e;
  1578. kwsys_stl::string findProg = SystemTools::FindProgram(path, e);
  1579. if(findProg.size())
  1580. {
  1581. program = findProg;
  1582. args = "";
  1583. return;
  1584. }
  1585. kwsys_stl::string dir = path;
  1586. kwsys_stl::string::size_type spacePos = dir.rfind(' ');
  1587. if(spacePos == kwsys_stl::string::npos)
  1588. {
  1589. program = "";
  1590. args = "";
  1591. return;
  1592. }
  1593. while(spacePos != kwsys_stl::string::npos)
  1594. {
  1595. kwsys_stl::string tryProg = dir.substr(0, spacePos);
  1596. if(SystemTools::FileExists(tryProg.c_str()))
  1597. {
  1598. program = tryProg;
  1599. args = dir.substr(spacePos, dir.size()-spacePos);
  1600. return;
  1601. }
  1602. findProg = SystemTools::FindProgram(tryProg.c_str(), e);
  1603. if(findProg.size())
  1604. {
  1605. program = findProg;
  1606. args = dir.substr(spacePos, dir.size()-spacePos);
  1607. return;
  1608. }
  1609. spacePos = dir.rfind(' ', spacePos--);
  1610. }
  1611. program = "";
  1612. args = "";
  1613. }
  1614. kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format)
  1615. {
  1616. char buf[1024];
  1617. time_t t;
  1618. time(&t);
  1619. strftime(buf, sizeof(buf), format, localtime(&t));
  1620. return buf;
  1621. }
  1622. kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
  1623. {
  1624. kwsys_stl::string str(s);
  1625. if (str.find_first_of("0123456789") == 0)
  1626. {
  1627. str = "_" + str;
  1628. }
  1629. kwsys_stl::string permited_chars("_"
  1630. "abcdefghijklmnopqrstuvwxyz"
  1631. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1632. "0123456789");
  1633. kwsys_stl::string::size_type pos = 0;
  1634. while ((pos = str.find_first_not_of(permited_chars, pos)) != kwsys_stl::string::npos)
  1635. {
  1636. str[pos] = '_';
  1637. }
  1638. return str;
  1639. }
  1640. // Due to a buggy stream library on the HP and another on Mac OSX, we
  1641. // need this very carefully written version of getline. Returns true
  1642. // if any data were read before the end-of-file was reached.
  1643. bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& line,
  1644. bool *has_newline /* = 0 */)
  1645. {
  1646. const int bufferSize = 1024;
  1647. char buffer[bufferSize];
  1648. line = "";
  1649. bool haveData = false;
  1650. if ( has_newline )
  1651. {
  1652. *has_newline = false;
  1653. }
  1654. // If no characters are read from the stream, the end of file has
  1655. // been reached.
  1656. while((is.getline(buffer, bufferSize), is.gcount() > 0))
  1657. {
  1658. haveData = true;
  1659. line.append(buffer);
  1660. // If newline character was read, the gcount includes the
  1661. // character, but the buffer does not. The end of line has been
  1662. // reached.
  1663. if(strlen(buffer) < static_cast<size_t>(is.gcount()))
  1664. {
  1665. if ( has_newline )
  1666. {
  1667. *has_newline = true;
  1668. }
  1669. break;
  1670. }
  1671. // The fail bit may be set. Clear it.
  1672. is.clear(is.rdstate() & ~kwsys_ios::ios::failbit);
  1673. }
  1674. return haveData;
  1675. }
  1676. } // namespace KWSYS_NAMESPACE
  1677. #if defined(_MSC_VER) && defined(_DEBUG)
  1678. # include <crtdbg.h>
  1679. # include <stdio.h>
  1680. # include <stdlib.h>
  1681. namespace KWSYS_NAMESPACE
  1682. {
  1683. static int SystemToolsDebugReport(int, char* message, int*)
  1684. {
  1685. fprintf(stderr, message);
  1686. exit(1);
  1687. }
  1688. void SystemTools::EnableMSVCDebugHook()
  1689. {
  1690. if(getenv("DART_TEST_FROM_DART"))
  1691. {
  1692. _CrtSetReportHook(SystemToolsDebugReport);
  1693. }
  1694. }
  1695. } // namespace KWSYS_NAMESPACE
  1696. #else
  1697. namespace KWSYS_NAMESPACE
  1698. {
  1699. void SystemTools::EnableMSVCDebugHook() {}
  1700. } // namespace KWSYS_NAMESPACE
  1701. #endif