SystemTools.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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, "CMAKE_FILE_PATH");
  991. SystemTools::GetPath(path);
  992. // now add the additional paths
  993. path.insert(path.end(), userPaths.begin(), userPaths.end());
  994. // now look for the file
  995. kwsys_stl::string tryPath;
  996. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  997. p != path.end(); ++p)
  998. {
  999. tryPath = *p;
  1000. tryPath += "/";
  1001. tryPath += name;
  1002. if(SystemTools::FileExists(tryPath.c_str()) &&
  1003. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1004. {
  1005. return SystemTools::CollapseFullPath(tryPath.c_str());
  1006. }
  1007. }
  1008. // Couldn't find the file.
  1009. return "";
  1010. }
  1011. /**
  1012. * Find the executable with the given name. Searches the given path and then
  1013. * the system search path. Returns the full path to the executable if it is
  1014. * found. Otherwise, the empty string is returned.
  1015. */
  1016. kwsys_stl::string SystemTools::FindProgram(
  1017. const char* name,
  1018. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1019. bool no_system_path)
  1020. {
  1021. if(!name)
  1022. {
  1023. return "";
  1024. }
  1025. // See if the executable exists as written.
  1026. if(SystemTools::FileExists(name) &&
  1027. !SystemTools::FileIsDirectory(name))
  1028. {
  1029. return SystemTools::CollapseFullPath(name);
  1030. }
  1031. kwsys_stl::string tryPath = name;
  1032. tryPath += SystemTools::GetExecutableExtension();
  1033. if(SystemTools::FileExists(tryPath.c_str()) &&
  1034. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1035. {
  1036. return SystemTools::CollapseFullPath(tryPath.c_str());
  1037. }
  1038. kwsys_stl::vector<kwsys_stl::string> path;
  1039. SystemTools::GetPath(path, "CMAKE_PROGRAM_PATH");
  1040. // Add the system search path to our path.
  1041. if (!no_system_path)
  1042. {
  1043. SystemTools::GetPath(path);
  1044. }
  1045. // now add the additional paths
  1046. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1047. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1048. p != path.end(); ++p)
  1049. {
  1050. tryPath = *p;
  1051. tryPath += "/";
  1052. tryPath += name;
  1053. if(SystemTools::FileExists(tryPath.c_str()) &&
  1054. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1055. {
  1056. return SystemTools::CollapseFullPath(tryPath.c_str());
  1057. }
  1058. #ifdef _WIN32
  1059. tryPath += ".com";
  1060. if(SystemTools::FileExists(tryPath.c_str()) &&
  1061. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1062. {
  1063. return SystemTools::CollapseFullPath(tryPath.c_str());
  1064. }
  1065. tryPath = *p;
  1066. tryPath += "/";
  1067. tryPath += name;
  1068. #endif
  1069. tryPath += SystemTools::GetExecutableExtension();
  1070. if(SystemTools::FileExists(tryPath.c_str()) &&
  1071. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1072. {
  1073. return SystemTools::CollapseFullPath(tryPath.c_str());
  1074. }
  1075. }
  1076. // Couldn't find the program.
  1077. return "";
  1078. }
  1079. /**
  1080. * Find the library with the given name. Searches the given path and then
  1081. * the system search path. Returns the full path to the library if it is
  1082. * found. Otherwise, the empty string is returned.
  1083. */
  1084. kwsys_stl::string SystemTools::FindLibrary(const char* name,
  1085. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  1086. {
  1087. // See if the executable exists as written.
  1088. if(SystemTools::FileExists(name) &&
  1089. !SystemTools::FileIsDirectory(name))
  1090. {
  1091. return SystemTools::CollapseFullPath(name);
  1092. }
  1093. // Add the system search path to our path.
  1094. kwsys_stl::vector<kwsys_stl::string> path;
  1095. SystemTools::GetPath(path);
  1096. // now add the additional paths
  1097. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1098. kwsys_stl::string tryPath;
  1099. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1100. p != path.end(); ++p)
  1101. {
  1102. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  1103. tryPath = *p;
  1104. tryPath += "/";
  1105. tryPath += name;
  1106. tryPath += ".lib";
  1107. if(SystemTools::FileExists(tryPath.c_str())
  1108. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1109. {
  1110. return SystemTools::CollapseFullPath(tryPath.c_str());
  1111. }
  1112. #else
  1113. tryPath = *p;
  1114. tryPath += "/lib";
  1115. tryPath += name;
  1116. tryPath += ".so";
  1117. if(SystemTools::FileExists(tryPath.c_str())
  1118. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1119. {
  1120. return SystemTools::CollapseFullPath(tryPath.c_str());
  1121. }
  1122. tryPath = *p;
  1123. tryPath += "/lib";
  1124. tryPath += name;
  1125. tryPath += ".a";
  1126. if(SystemTools::FileExists(tryPath.c_str())
  1127. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1128. {
  1129. return SystemTools::CollapseFullPath(tryPath.c_str());
  1130. }
  1131. tryPath = *p;
  1132. tryPath += "/lib";
  1133. tryPath += name;
  1134. tryPath += ".sl";
  1135. if(SystemTools::FileExists(tryPath.c_str())
  1136. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1137. {
  1138. return SystemTools::CollapseFullPath(tryPath.c_str());
  1139. }
  1140. tryPath = *p;
  1141. tryPath += "/lib";
  1142. tryPath += name;
  1143. tryPath += ".dylib";
  1144. if(SystemTools::FileExists(tryPath.c_str())
  1145. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1146. {
  1147. return SystemTools::CollapseFullPath(tryPath.c_str());
  1148. }
  1149. tryPath = *p;
  1150. tryPath += "/lib";
  1151. tryPath += name;
  1152. tryPath += ".dll";
  1153. if(SystemTools::FileExists(tryPath.c_str())
  1154. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1155. {
  1156. return SystemTools::CollapseFullPath(tryPath.c_str());
  1157. }
  1158. #endif
  1159. }
  1160. // Couldn't find the library.
  1161. return "";
  1162. }
  1163. bool SystemTools::FileIsDirectory(const char* name)
  1164. {
  1165. struct stat fs;
  1166. if(stat(name, &fs) == 0)
  1167. {
  1168. #if _WIN32
  1169. return ((fs.st_mode & _S_IFDIR) != 0);
  1170. #else
  1171. return S_ISDIR(fs.st_mode);
  1172. #endif
  1173. }
  1174. else
  1175. {
  1176. return false;
  1177. }
  1178. }
  1179. int SystemTools::ChangeDirectory(const char *dir)
  1180. {
  1181. return Chdir(dir);
  1182. }
  1183. kwsys_stl::string SystemTools::GetCurrentWorkingDirectory()
  1184. {
  1185. char buf[2048];
  1186. const char* cwd = Getcwd(buf, 2048);
  1187. kwsys_stl::string path;
  1188. if ( cwd )
  1189. {
  1190. path = cwd;
  1191. }
  1192. return path;
  1193. }
  1194. kwsys_stl::string SystemTools::GetProgramPath(const char* in_name)
  1195. {
  1196. kwsys_stl::string dir, file;
  1197. SystemTools::SplitProgramPath(in_name, dir, file);
  1198. return dir;
  1199. }
  1200. bool SystemTools::SplitProgramPath(const char* in_name,
  1201. kwsys_stl::string& dir,
  1202. kwsys_stl::string& file,
  1203. bool)
  1204. {
  1205. dir = in_name;
  1206. file = "";
  1207. SystemTools::ConvertToUnixSlashes(dir);
  1208. if(!SystemTools::FileIsDirectory(dir.c_str()))
  1209. {
  1210. kwsys_stl::string::size_type slashPos = dir.rfind("/");
  1211. if(slashPos != kwsys_stl::string::npos)
  1212. {
  1213. file = dir.substr(slashPos+1);
  1214. dir = dir.substr(0, slashPos);
  1215. }
  1216. else
  1217. {
  1218. file = dir;
  1219. dir = "";
  1220. }
  1221. }
  1222. if(!(dir == "") && !SystemTools::FileIsDirectory(dir.c_str()))
  1223. {
  1224. kwsys_stl::string oldDir = in_name;
  1225. SystemTools::ConvertToUnixSlashes(oldDir);
  1226. dir = in_name;
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative)
  1232. {
  1233. return SystemTools::CollapseFullPath(in_relative, 0);
  1234. }
  1235. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative,
  1236. const char* in_base)
  1237. {
  1238. // Save original working directory.
  1239. kwsys_stl::string orig = SystemTools::GetCurrentWorkingDirectory();
  1240. // Change to base of relative path.
  1241. if(in_base)
  1242. {
  1243. Chdir(in_base);
  1244. }
  1245. kwsys_stl::string dir, file;
  1246. SystemTools::SplitProgramPath(in_relative, dir, file, false);
  1247. if(dir.size() == 0 &&
  1248. in_relative && strlen(in_relative) > 0 &&
  1249. in_relative[0] == '/')
  1250. {
  1251. dir = "/";
  1252. }
  1253. #ifdef _WIN32
  1254. // Follow relative path.
  1255. if(!(dir == ""))
  1256. {
  1257. Chdir(dir.c_str());
  1258. }
  1259. // Get the resulting directory.
  1260. kwsys_stl::string newDir = SystemTools::GetCurrentWorkingDirectory();
  1261. // Add the file back on to the directory.
  1262. SystemTools::ConvertToUnixSlashes(newDir);
  1263. #else
  1264. # ifdef MAXPATHLEN
  1265. char resolved_name[MAXPATHLEN];
  1266. # else
  1267. # ifdef PATH_MAX
  1268. char resolved_name[PATH_MAX];
  1269. # else
  1270. char resolved_name[5024];
  1271. # endif
  1272. # endif
  1273. // Resolve relative path.
  1274. kwsys_stl::string newDir;
  1275. if(!(dir == ""))
  1276. {
  1277. realpath(dir.c_str(), resolved_name);
  1278. newDir = resolved_name;
  1279. }
  1280. else
  1281. {
  1282. newDir = SystemTools::GetCurrentWorkingDirectory();
  1283. }
  1284. // if there is a /tmp_mnt in a path get rid of it!
  1285. if(newDir.find("/tmp_mnt") == 0)
  1286. {
  1287. newDir = newDir.substr(8);
  1288. }
  1289. #endif
  1290. // Restore original working directory.
  1291. Chdir(orig.c_str());
  1292. // Construct and return the full path.
  1293. kwsys_stl::string newPath = newDir;
  1294. if(!(file == ""))
  1295. {
  1296. if(!(newDir.size() == 1 && newDir[0] == '/'))
  1297. {
  1298. newPath += "/";
  1299. }
  1300. newPath += file;
  1301. }
  1302. return newPath;
  1303. }
  1304. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines)
  1305. {
  1306. kwsys_stl::string data(str);
  1307. kwsys_stl::string::size_type lpos = 0;
  1308. while(lpos < data.length())
  1309. {
  1310. kwsys_stl::string::size_type rpos = data.find_first_of("\n", lpos);
  1311. if(rpos == kwsys_stl::string::npos)
  1312. {
  1313. // Line ends at end of string without a newline.
  1314. lines.push_back(data.substr(lpos));
  1315. return false;
  1316. }
  1317. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1318. {
  1319. // Line ends in a "\r\n" pair, remove both characters.
  1320. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1321. }
  1322. else
  1323. {
  1324. // Line ends in a "\n", remove the character.
  1325. lines.push_back(data.substr(lpos, rpos-lpos));
  1326. }
  1327. lpos = rpos+1;
  1328. }
  1329. return true;
  1330. }
  1331. /**
  1332. * Return path of a full filename (no trailing slashes).
  1333. * Warning: returned path is converted to Unix slashes format.
  1334. */
  1335. kwsys_stl::string SystemTools::GetFilenamePath(const kwsys_stl::string& filename)
  1336. {
  1337. kwsys_stl::string fn = filename;
  1338. SystemTools::ConvertToUnixSlashes(fn);
  1339. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1340. if(slash_pos != kwsys_stl::string::npos)
  1341. {
  1342. return fn.substr(0, slash_pos);
  1343. }
  1344. else
  1345. {
  1346. return "";
  1347. }
  1348. }
  1349. /**
  1350. * Return file name of a full filename (i.e. file name without path).
  1351. */
  1352. kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename)
  1353. {
  1354. kwsys_stl::string fn = filename;
  1355. SystemTools::ConvertToUnixSlashes(fn);
  1356. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1357. if(slash_pos != kwsys_stl::string::npos)
  1358. {
  1359. return fn.substr(slash_pos + 1);
  1360. }
  1361. else
  1362. {
  1363. return filename;
  1364. }
  1365. }
  1366. /**
  1367. * Return file extension of a full filename (dot included).
  1368. * Warning: this is the longest extension (for example: .tar.gz)
  1369. */
  1370. kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename)
  1371. {
  1372. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1373. kwsys_stl::string::size_type dot_pos = name.find(".");
  1374. if(dot_pos != kwsys_stl::string::npos)
  1375. {
  1376. return name.substr(dot_pos);
  1377. }
  1378. else
  1379. {
  1380. return "";
  1381. }
  1382. }
  1383. /**
  1384. * Return file extension of a full filename (dot included).
  1385. * Warning: this is the shortest extension (for example: .tar.gz)
  1386. */
  1387. kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename)
  1388. {
  1389. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1390. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1391. if(dot_pos != kwsys_stl::string::npos)
  1392. {
  1393. return name.substr(dot_pos);
  1394. }
  1395. else
  1396. {
  1397. return "";
  1398. }
  1399. }
  1400. /**
  1401. * Return file name without extension of a full filename (i.e. without path).
  1402. * Warning: it considers the longest extension (for example: .tar.gz)
  1403. */
  1404. kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename)
  1405. {
  1406. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1407. kwsys_stl::string::size_type dot_pos = name.find(".");
  1408. if(dot_pos != kwsys_stl::string::npos)
  1409. {
  1410. return name.substr(0, dot_pos);
  1411. }
  1412. else
  1413. {
  1414. return name;
  1415. }
  1416. }
  1417. /**
  1418. * Return file name without extension of a full filename (i.e. without path).
  1419. * Warning: it considers the last extension (for example: removes .gz
  1420. * from .tar.gz)
  1421. */
  1422. kwsys_stl::string
  1423. SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename)
  1424. {
  1425. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1426. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1427. if(dot_pos != kwsys_stl::string::npos)
  1428. {
  1429. return name.substr(0, dot_pos);
  1430. }
  1431. else
  1432. {
  1433. return name;
  1434. }
  1435. }
  1436. bool SystemTools::FileIsFullPath(const char* in_name)
  1437. {
  1438. kwsys_stl::string name = in_name;
  1439. #if defined(_WIN32)
  1440. // On Windows, the name must be at least two characters long.
  1441. if(name.length() < 2)
  1442. {
  1443. return false;
  1444. }
  1445. if(name[1] == ':')
  1446. {
  1447. return true;
  1448. }
  1449. if(name[0] == '\\')
  1450. {
  1451. return true;
  1452. }
  1453. #else
  1454. // On UNIX, the name must be at least one character long.
  1455. if(name.length() < 1)
  1456. {
  1457. return false;
  1458. }
  1459. #endif
  1460. // On UNIX, the name must begin in a '/'.
  1461. // On Windows, if the name begins in a '/', then it is a full
  1462. // network path.
  1463. if(name[0] == '/')
  1464. {
  1465. return true;
  1466. }
  1467. return false;
  1468. }
  1469. bool SystemTools::GetShortPath(const char* path, kwsys_stl::string& shortPath)
  1470. {
  1471. #if defined(WIN32) && !defined(__CYGWIN__)
  1472. const int size = int(strlen(path)) +1; // size of return
  1473. char *buffer = new char[size]; // create a buffer
  1474. char *tempPath = new char[size]; // create a buffer
  1475. int ret;
  1476. // if the path passed in has quotes around it, first remove the quotes
  1477. if (path[0] == '"' && path[strlen(path)-1] == '"')
  1478. {
  1479. strcpy(tempPath,path+1);
  1480. tempPath[strlen(tempPath)-1] = '\0';
  1481. }
  1482. else
  1483. {
  1484. strcpy(tempPath,path);
  1485. }
  1486. buffer[0] = 0;
  1487. ret = GetShortPathName(tempPath, buffer, size);
  1488. if(buffer[0] == 0 || ret > size)
  1489. {
  1490. delete [] buffer;
  1491. delete [] tempPath;
  1492. return false;
  1493. }
  1494. else
  1495. {
  1496. shortPath = buffer;
  1497. delete [] buffer;
  1498. delete [] tempPath;
  1499. return true;
  1500. }
  1501. #else
  1502. shortPath = path;
  1503. return true;
  1504. #endif
  1505. }
  1506. void SystemTools::SplitProgramFromArgs(const char* path,
  1507. kwsys_stl::string& program, kwsys_stl::string& args)
  1508. {
  1509. if(SystemTools::FileExists(path))
  1510. {
  1511. program = path;
  1512. args = "";
  1513. return;
  1514. }
  1515. kwsys_stl::vector<kwsys_stl::string> e;
  1516. kwsys_stl::string findProg = SystemTools::FindProgram(path, e);
  1517. if(findProg.size())
  1518. {
  1519. program = findProg;
  1520. args = "";
  1521. return;
  1522. }
  1523. kwsys_stl::string dir = path;
  1524. kwsys_stl::string::size_type spacePos = dir.rfind(' ');
  1525. if(spacePos == kwsys_stl::string::npos)
  1526. {
  1527. program = "";
  1528. args = "";
  1529. return;
  1530. }
  1531. while(spacePos != kwsys_stl::string::npos)
  1532. {
  1533. kwsys_stl::string tryProg = dir.substr(0, spacePos);
  1534. if(SystemTools::FileExists(tryProg.c_str()))
  1535. {
  1536. program = tryProg;
  1537. args = dir.substr(spacePos, dir.size()-spacePos);
  1538. return;
  1539. }
  1540. findProg = SystemTools::FindProgram(tryProg.c_str(), e);
  1541. if(findProg.size())
  1542. {
  1543. program = findProg;
  1544. args = dir.substr(spacePos, dir.size()-spacePos);
  1545. return;
  1546. }
  1547. spacePos = dir.rfind(' ', spacePos--);
  1548. }
  1549. program = "";
  1550. args = "";
  1551. }
  1552. kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format)
  1553. {
  1554. char buf[1024];
  1555. time_t t;
  1556. time(&t);
  1557. strftime(buf, sizeof(buf), format, localtime(&t));
  1558. return buf;
  1559. }
  1560. kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
  1561. {
  1562. kwsys_stl::string str(s);
  1563. if (str.find_first_of("0123456789") == 0)
  1564. {
  1565. str = "_" + str;
  1566. }
  1567. kwsys_stl::string permited_chars("_"
  1568. "abcdefghijklmnopqrstuvwxyz"
  1569. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1570. "0123456789");
  1571. kwsys_stl::string::size_type pos = 0;
  1572. while ((pos = str.find_first_not_of(permited_chars, pos)) != kwsys_stl::string::npos)
  1573. {
  1574. str[pos] = '_';
  1575. }
  1576. return str;
  1577. }
  1578. // Due to a buggy stream library on the HP and another on Mac OSX, we
  1579. // need this very carefully written version of getline. Returns true
  1580. // if any data were read before the end-of-file was reached.
  1581. bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& line,
  1582. bool *has_newline /* = 0 */)
  1583. {
  1584. const int bufferSize = 1024;
  1585. char buffer[bufferSize];
  1586. line = "";
  1587. bool haveData = false;
  1588. if ( has_newline )
  1589. {
  1590. *has_newline = false;
  1591. }
  1592. // If no characters are read from the stream, the end of file has
  1593. // been reached.
  1594. while((is.getline(buffer, bufferSize), is.gcount() > 0))
  1595. {
  1596. haveData = true;
  1597. line.append(buffer);
  1598. // If newline character was read, the gcount includes the
  1599. // character, but the buffer does not. The end of line has been
  1600. // reached.
  1601. if(strlen(buffer) < static_cast<size_t>(is.gcount()))
  1602. {
  1603. if ( has_newline )
  1604. {
  1605. *has_newline = true;
  1606. }
  1607. break;
  1608. }
  1609. // The fail bit may be set. Clear it.
  1610. is.clear(is.rdstate() & ~kwsys_ios::ios::failbit);
  1611. }
  1612. return haveData;
  1613. }
  1614. } // namespace KWSYS_NAMESPACE
  1615. #if defined(_MSC_VER) && defined(_DEBUG)
  1616. # include <crtdbg.h>
  1617. # include <stdio.h>
  1618. # include <stdlib.h>
  1619. namespace KWSYS_NAMESPACE
  1620. {
  1621. static int SystemToolsDebugReport(int, char* message, int*)
  1622. {
  1623. fprintf(stderr, message);
  1624. exit(1);
  1625. }
  1626. void SystemTools::EnableMSVCDebugHook()
  1627. {
  1628. if(getenv("DART_TEST_FROM_DART"))
  1629. {
  1630. _CrtSetReportHook(SystemToolsDebugReport);
  1631. }
  1632. }
  1633. } // namespace KWSYS_NAMESPACE
  1634. #else
  1635. namespace KWSYS_NAMESPACE
  1636. {
  1637. void SystemTools::EnableMSVCDebugHook() {}
  1638. } // namespace KWSYS_NAMESPACE
  1639. #endif