/modules/libreg/src/vr_stubs.c

http://github.com/zpao/v8monkey · C · 507 lines · 368 code · 77 blank · 62 comment · 89 complexity · dbfd596c7b1d37614d12d87d03e8d73f MD5 · raw file

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /* this file contains stubs needed to build the registry routines
  38. * into a stand-alone library for use with our installers
  39. */
  40. #ifdef STANDALONE_REGISTRY
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. #else
  45. #include "prtypes.h"
  46. #include "plstr.h"
  47. #endif /*STANDALONE_REGISTRY*/
  48. #include "vr_stubs.h"
  49. #ifdef XP_MACOSX
  50. #include <Carbon/Carbon.h>
  51. #include <stdlib.h>
  52. #endif
  53. #ifdef XP_BEOS
  54. #include <FindDirectory.h>
  55. #endif
  56. #ifdef XP_MACOSX
  57. /* So that we're not dependent on the size of chars in a wide string literal */
  58. static const UniChar kOSXRegParentName[] =
  59. { 'M', 'o', 'z', 'i', 'l', 'l', 'a' };
  60. static const UniChar kOSXRegName[] =
  61. { 'G', 'l', 'o', 'b', 'a', 'l', '.', 'r', 'e', 'g', 's' };
  62. static const UniChar kOSXVersRegName[] =
  63. { 'V', 'e', 'r', 's', 'i', 'o', 'n', 's', '.', 'r', 'e', 'g', 's' };
  64. #define UNICHAR_ARRAY_LEN(s) (sizeof(s) / sizeof(UniChar))
  65. #endif
  66. #define DEF_REG "/.mozilla/registry"
  67. #define WIN_REG "\\mozregistry.dat"
  68. #define MAC_REG "\pMozilla Registry"
  69. #define BEOS_REG "/mozilla/registry"
  70. #define DEF_VERREG "/.mozilla/mozver.dat"
  71. #define WIN_VERREG "\\mozver.dat"
  72. #define MAC_VERREG "\pMozilla Versions"
  73. #define BEOS_VERREG "/mozilla/mozver.dat"
  74. /* ------------------------------------------------------------------
  75. * OS/2 STUBS
  76. * ------------------------------------------------------------------
  77. */
  78. #ifdef XP_OS2
  79. #define INCL_DOS
  80. #include <os2.h>
  81. #ifdef STANDALONE_REGISTRY
  82. extern XP_File vr_fileOpen (const char *name, const char * mode)
  83. {
  84. XP_File fh = NULL;
  85. struct stat st;
  86. if ( name != NULL ) {
  87. if ( stat( name, &st ) == 0 )
  88. fh = fopen( name, XP_FILE_UPDATE_BIN );
  89. else
  90. fh = fopen( name, XP_FILE_TRUNCATE_BIN );
  91. }
  92. return fh;
  93. }
  94. #endif /*STANDALONE_REGISTRY*/
  95. extern void vr_findGlobalRegName ()
  96. {
  97. char path[ CCHMAXPATH ];
  98. int pathlen;
  99. XP_File fh = NULL;
  100. struct stat st;
  101. XP_STRCPY(path, ".");
  102. pathlen = strlen(path);
  103. if ( pathlen > 0 ) {
  104. XP_STRCPY( path+pathlen, WIN_REG );
  105. globalRegName = XP_STRDUP(path);
  106. }
  107. }
  108. char* vr_findVerRegName()
  109. {
  110. /* need to find a global place for the version registry */
  111. if ( verRegName == NULL )
  112. {
  113. if ( globalRegName == NULL)
  114. vr_findGlobalRegName();
  115. verRegName = XP_STRDUP(globalRegName);
  116. }
  117. return verRegName;
  118. }
  119. #endif /* XP_OS2 */
  120. /* ------------------------------------------------------------------
  121. * WINDOWS STUBS
  122. * ------------------------------------------------------------------
  123. */
  124. #if defined(XP_WIN)
  125. #include "windows.h"
  126. #define PATHLEN 260
  127. #ifdef STANDALONE_REGISTRY
  128. extern XP_File vr_fileOpen (const char *name, const char * mode)
  129. {
  130. XP_File fh = NULL;
  131. struct stat st;
  132. if ( name != NULL ) {
  133. if ( stat( name, &st ) == 0 )
  134. fh = fopen( name, XP_FILE_UPDATE_BIN );
  135. else
  136. fh = fopen( name, XP_FILE_TRUNCATE_BIN );
  137. }
  138. return fh;
  139. }
  140. #endif /*STANDALONE_REGISTRY*/
  141. extern void vr_findGlobalRegName ()
  142. {
  143. char path[ PATHLEN ];
  144. int pathlen;
  145. pathlen = GetWindowsDirectory(path, PATHLEN);
  146. if ( pathlen > 0 ) {
  147. XP_FREEIF(globalRegName);
  148. XP_STRCPY( path+pathlen, WIN_REG );
  149. globalRegName = XP_STRDUP(path);
  150. }
  151. }
  152. char* vr_findVerRegName()
  153. {
  154. char path[ PATHLEN ];
  155. int pathlen;
  156. if ( verRegName == NULL )
  157. {
  158. pathlen = GetWindowsDirectory(path, PATHLEN);
  159. if ( pathlen > 0 ) {
  160. XP_STRCPY( path+pathlen, WIN_VERREG );
  161. verRegName = XP_STRDUP(path);
  162. }
  163. }
  164. return verRegName;
  165. }
  166. #if !defined(WIN32) && !defined(__BORLANDC__)
  167. int FAR PASCAL _export WEP(int);
  168. int FAR PASCAL LibMain(HANDLE hInst, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
  169. {
  170. if ( wHeapSize > 0 )
  171. UnlockData(0);
  172. return 1;
  173. }
  174. int FAR PASCAL _export WEP(int nParam)
  175. {
  176. return 1;
  177. }
  178. #endif /* not WIN32 */
  179. #endif /* XP_WIN */
  180. /* ------------------------------------------------------------------
  181. * MACINTOSH STUBS
  182. * ------------------------------------------------------------------
  183. */
  184. #if defined(XP_MACOSX)
  185. #ifdef STANDALONE_REGISTRY
  186. extern XP_File vr_fileOpen(const char *name, const char * mode)
  187. {
  188. XP_File fh = NULL;
  189. struct stat st;
  190. errno = 0; /* reset errno (only if we're using stdio) */
  191. if ( name != NULL ) {
  192. if ( stat( name, &st ) == 0 )
  193. fh = fopen( name, XP_FILE_UPDATE_BIN );
  194. else
  195. {
  196. /* should never get here! */
  197. fh = fopen( name, XP_FILE_TRUNCATE_BIN );
  198. }
  199. }
  200. return fh;
  201. }
  202. #endif /*STANDALONE_REGISTRY*/
  203. extern void vr_findGlobalRegName()
  204. {
  205. OSErr err;
  206. FSRef foundRef;
  207. err = FSFindFolder(kLocalDomain, kDomainLibraryFolderType, kDontCreateFolder, &foundRef);
  208. if (err == noErr)
  209. {
  210. FSRef parentRef;
  211. err = FSMakeFSRefUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
  212. kTextEncodingUnknown, &parentRef);
  213. if (err == fnfErr)
  214. {
  215. err = FSCreateDirectoryUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
  216. kFSCatInfoNone, NULL, &parentRef, NULL, NULL);
  217. }
  218. if (err == noErr)
  219. {
  220. FSRef regRef;
  221. err = FSMakeFSRefUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXRegName), kOSXRegName,
  222. kTextEncodingUnknown, &regRef);
  223. if (err == fnfErr)
  224. {
  225. FSCatalogInfo catalogInfo;
  226. FileInfo fileInfo = { 'REGS', 'MOSS', 0, { 0, 0 }, 0 };
  227. memmove(&(catalogInfo.finderInfo), &fileInfo, sizeof(FileInfo));
  228. err = FSCreateFileUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXRegName), kOSXRegName,
  229. kFSCatInfoFinderInfo, &catalogInfo, &regRef, NULL);
  230. }
  231. if (err == noErr)
  232. {
  233. UInt8 pathBuf[PATH_MAX];
  234. err = FSRefMakePath(&regRef, pathBuf, sizeof(pathBuf));
  235. if (err == noErr)
  236. globalRegName = XP_STRDUP((const char*)pathBuf);
  237. }
  238. }
  239. }
  240. }
  241. extern char* vr_findVerRegName()
  242. {
  243. OSErr err;
  244. FSRef foundRef;
  245. err = FSFindFolder(kLocalDomain, kDomainLibraryFolderType, kDontCreateFolder, &foundRef);
  246. if (err == noErr)
  247. {
  248. FSRef parentRef;
  249. err = FSMakeFSRefUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
  250. kTextEncodingUnknown, &parentRef);
  251. if (err == fnfErr)
  252. {
  253. err = FSCreateDirectoryUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
  254. kFSCatInfoNone, NULL, &parentRef, NULL, NULL);
  255. }
  256. if (err == noErr)
  257. {
  258. FSRef regRef;
  259. err = FSMakeFSRefUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXVersRegName), kOSXVersRegName,
  260. kTextEncodingUnknown, &regRef);
  261. if (err == fnfErr)
  262. {
  263. FSCatalogInfo catalogInfo;
  264. FileInfo fileInfo = { 'REGS', 'MOSS', 0, { 0, 0 }, 0 };
  265. memmove(&(catalogInfo.finderInfo), &fileInfo, sizeof(FileInfo));
  266. err = FSCreateFileUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXVersRegName), kOSXVersRegName,
  267. kFSCatInfoFinderInfo, &catalogInfo, &regRef, NULL);
  268. }
  269. if (err == noErr)
  270. {
  271. UInt8 pathBuf[PATH_MAX];
  272. err = FSRefMakePath(&regRef, pathBuf, sizeof(pathBuf));
  273. if (err == noErr)
  274. verRegName = XP_STRDUP((const char*)pathBuf);
  275. }
  276. }
  277. }
  278. return verRegName;
  279. }
  280. #endif /* XP_MACOSX */
  281. /* ------------------------------------------------------------------
  282. * UNIX STUBS
  283. * ------------------------------------------------------------------
  284. */
  285. #if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
  286. #include <stdlib.h>
  287. #ifdef XP_OS2
  288. #include <io.h>
  289. #define W_OK 0x02 /*evil hack from the docs...*/
  290. #else
  291. #include <unistd.h>
  292. #endif
  293. #include "NSReg.h"
  294. #include "VerReg.h"
  295. char *TheRegistry = "registry";
  296. char *Flist;
  297. REGERR vr_ParseVersion(char *verstr, VERSION *result);
  298. #if defined(XP_UNIX) && !defined(XP_MACOSX)
  299. #ifdef STANDALONE_REGISTRY
  300. extern XP_File vr_fileOpen (const char *name, const char * mode)
  301. {
  302. XP_File fh = NULL;
  303. struct stat st;
  304. if ( name != NULL ) {
  305. if ( stat( name, &st ) == 0 )
  306. fh = fopen( name, XP_FILE_UPDATE_BIN );
  307. else
  308. fh = fopen( name, XP_FILE_TRUNCATE_BIN );
  309. }
  310. return fh;
  311. }
  312. #endif /*STANDALONE_REGISTRY*/
  313. extern void vr_findGlobalRegName ()
  314. {
  315. #ifndef STANDALONE_REGISTRY
  316. char *def = NULL;
  317. char *home = getenv("HOME");
  318. if (home != NULL) {
  319. def = (char *) XP_ALLOC(XP_STRLEN(home) + XP_STRLEN(DEF_REG)+1);
  320. if (def != NULL) {
  321. XP_STRCPY(def, home);
  322. XP_STRCAT(def, DEF_REG);
  323. }
  324. }
  325. if (def != NULL) {
  326. globalRegName = XP_STRDUP(def);
  327. } else {
  328. globalRegName = XP_STRDUP(TheRegistry);
  329. }
  330. XP_FREEIF(def);
  331. #else
  332. globalRegName = XP_STRDUP(TheRegistry);
  333. #endif /*STANDALONE_REGISTRY*/
  334. }
  335. char* vr_findVerRegName ()
  336. {
  337. if ( verRegName != NULL )
  338. return verRegName;
  339. #ifndef STANDALONE_REGISTRY
  340. {
  341. char *def = NULL;
  342. char *home = getenv("HOME");
  343. if (home != NULL) {
  344. def = (char *) XP_ALLOC(XP_STRLEN(home) + XP_STRLEN(DEF_VERREG)+1);
  345. if (def != NULL) {
  346. XP_STRCPY(def, home);
  347. XP_STRCAT(def, DEF_VERREG);
  348. }
  349. }
  350. if (def != NULL) {
  351. verRegName = XP_STRDUP(def);
  352. }
  353. XP_FREEIF(def);
  354. }
  355. #else
  356. verRegName = XP_STRDUP(TheRegistry);
  357. #endif /*STANDALONE_REGISTRY*/
  358. return verRegName;
  359. }
  360. #endif /*XP_UNIX*/
  361. /* ------------------------------------------------------------------
  362. * BeOS STUBS
  363. * ------------------------------------------------------------------
  364. */
  365. #ifdef XP_BEOS
  366. #ifdef STANDALONE_REGISTRY
  367. extern XP_File vr_fileOpen (const char *name, const char * mode)
  368. {
  369. XP_File fh = NULL;
  370. struct stat st;
  371. if ( name != NULL ) {
  372. if ( stat( name, &st ) == 0 )
  373. fh = fopen( name, XP_FILE_UPDATE_BIN );
  374. else
  375. fh = fopen( name, XP_FILE_TRUNCATE_BIN );
  376. }
  377. return fh;
  378. }
  379. #endif /*STANDALONE_REGISTRY*/
  380. extern void vr_findGlobalRegName ()
  381. {
  382. #ifndef STANDALONE_REGISTRY
  383. char *def = NULL;
  384. char settings[1024];
  385. find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, settings, sizeof(settings));
  386. if (settings != NULL) {
  387. def = (char *) XP_ALLOC(XP_STRLEN(settings) + XP_STRLEN(BEOS_REG)+1);
  388. if (def != NULL) {
  389. XP_STRCPY(def, settings);
  390. XP_STRCAT(def, BEOS_REG);
  391. }
  392. }
  393. if (def != NULL) {
  394. globalRegName = XP_STRDUP(def);
  395. } else {
  396. globalRegName = XP_STRDUP(TheRegistry);
  397. }
  398. XP_FREEIF(def);
  399. #else
  400. globalRegName = XP_STRDUP(TheRegistry);
  401. #endif /*STANDALONE_REGISTRY*/
  402. }
  403. char* vr_findVerRegName ()
  404. {
  405. if ( verRegName != NULL )
  406. return verRegName;
  407. #ifndef STANDALONE_REGISTRY
  408. {
  409. char *def = NULL;
  410. char settings[1024];
  411. find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, settings, sizeof(settings));
  412. if (settings != NULL) {
  413. def = (char *) XP_ALLOC(XP_STRLEN(settings) + XP_STRLEN(BEOS_VERREG)+1);
  414. if (def != NULL) {
  415. XP_STRCPY(def, settings);
  416. XP_STRCAT(def, BEOS_VERREG);
  417. }
  418. }
  419. if (def != NULL) {
  420. verRegName = XP_STRDUP(def);
  421. }
  422. XP_FREEIF(def);
  423. }
  424. #else
  425. verRegName = XP_STRDUP(TheRegistry);
  426. #endif /*STANDALONE_REGISTRY*/
  427. return verRegName;
  428. }
  429. #endif /*XP_BEOS*/
  430. #endif /* XP_UNIX || XP_OS2 */