SystemTools.cxx 43 KB

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