SystemTools.cxx 43 KB

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