Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / libreg / src / vr_stubs.c
blobd02727a85f17db42327ff5ada3d76e19e55558fd
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 /* this file contains stubs needed to build the registry routines
39 * into a stand-alone library for use with our installers
42 #ifdef STANDALONE_REGISTRY
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
48 #else
50 #include "prtypes.h"
51 #include "plstr.h"
53 #endif /*STANDALONE_REGISTRY*/
55 #include "vr_stubs.h"
57 #ifdef XP_MACOSX
58 #include <Carbon/Carbon.h>
59 #include <stdlib.h>
60 #endif
62 #ifdef XP_BEOS
63 #include <FindDirectory.h>
64 #endif
66 #ifdef XP_MACOSX
67 /* So that we're not dependent on the size of chars in a wide string literal */
68 static const UniChar kOSXRegParentName[] =
69 { 'M', 'o', 'z', 'i', 'l', 'l', 'a' };
70 static const UniChar kOSXRegName[] =
71 { 'G', 'l', 'o', 'b', 'a', 'l', '.', 'r', 'e', 'g', 's' };
72 static const UniChar kOSXVersRegName[] =
73 { 'V', 'e', 'r', 's', 'i', 'o', 'n', 's', '.', 'r', 'e', 'g', 's' };
75 #define UNICHAR_ARRAY_LEN(s) (sizeof(s) / sizeof(UniChar))
76 #endif
78 #define DEF_REG "/.mozilla/registry"
79 #define WIN_REG "\\mozregistry.dat"
80 #define MAC_REG "\pMozilla Registry"
81 #define BEOS_REG "/mozilla/registry"
83 #define DEF_VERREG "/.mozilla/mozver.dat"
84 #define WIN_VERREG "\\mozver.dat"
85 #define MAC_VERREG "\pMozilla Versions"
86 #define BEOS_VERREG "/mozilla/mozver.dat"
89 /* ------------------------------------------------------------------
90 * OS/2 STUBS
91 * ------------------------------------------------------------------
93 #ifdef XP_OS2
94 #define INCL_DOS
95 #include <os2.h>
97 #ifdef STANDALONE_REGISTRY
98 extern XP_File vr_fileOpen (const char *name, const char * mode)
100 XP_File fh = NULL;
101 struct stat st;
103 if ( name != NULL ) {
104 if ( stat( name, &st ) == 0 )
105 fh = fopen( name, XP_FILE_UPDATE_BIN );
106 else
107 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
110 return fh;
112 #endif /*STANDALONE_REGISTRY*/
114 extern void vr_findGlobalRegName ()
116 char path[ CCHMAXPATH ];
117 int pathlen;
118 XP_File fh = NULL;
119 struct stat st;
121 XP_STRCPY(path, ".");
122 pathlen = strlen(path);
124 if ( pathlen > 0 ) {
125 XP_STRCPY( path+pathlen, WIN_REG );
126 globalRegName = XP_STRDUP(path);
130 char* vr_findVerRegName()
132 /* need to find a global place for the version registry */
133 if ( verRegName == NULL )
135 if ( globalRegName == NULL)
136 vr_findGlobalRegName();
137 verRegName = XP_STRDUP(globalRegName);
140 return verRegName;
143 #endif /* XP_OS2 */
146 /* ------------------------------------------------------------------
147 * WINDOWS STUBS
148 * ------------------------------------------------------------------
150 #if defined(XP_WIN)
151 #include "windows.h"
152 #define PATHLEN 260
154 #ifdef STANDALONE_REGISTRY
155 extern XP_File vr_fileOpen (const char *name, const char * mode)
157 XP_File fh = NULL;
158 struct stat st;
160 if ( name != NULL ) {
161 if ( stat( name, &st ) == 0 )
162 fh = fopen( name, XP_FILE_UPDATE_BIN );
163 else
164 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
167 return fh;
169 #endif /*STANDALONE_REGISTRY*/
171 extern void vr_findGlobalRegName ()
173 char path[ PATHLEN ];
174 int pathlen;
176 pathlen = GetWindowsDirectory(path, PATHLEN);
177 if ( pathlen > 0 ) {
178 XP_FREEIF(globalRegName);
179 XP_STRCPY( path+pathlen, WIN_REG );
180 globalRegName = XP_STRDUP(path);
184 char* vr_findVerRegName()
186 char path[ PATHLEN ];
187 int pathlen;
189 if ( verRegName == NULL )
191 pathlen = GetWindowsDirectory(path, PATHLEN);
192 if ( pathlen > 0 ) {
193 XP_STRCPY( path+pathlen, WIN_VERREG );
194 verRegName = XP_STRDUP(path);
198 return verRegName;
201 #if !defined(WIN32) && !defined(__BORLANDC__)
202 int FAR PASCAL _export WEP(int);
204 int FAR PASCAL LibMain(HANDLE hInst, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
206 if ( wHeapSize > 0 )
207 UnlockData(0);
208 return 1;
211 int FAR PASCAL _export WEP(int nParam)
213 return 1;
215 #endif /* not WIN32 */
217 #endif /* XP_WIN */
220 /* ------------------------------------------------------------------
221 * MACINTOSH STUBS
222 * ------------------------------------------------------------------
225 #if defined(XP_MAC) || defined(XP_MACOSX)
226 #include <Files.h>
228 #ifdef STANDALONE_REGISTRY
229 extern XP_File vr_fileOpen(const char *name, const char * mode)
231 XP_File fh = NULL;
232 struct stat st;
234 #ifdef STANDALONE_REGISTRY
235 errno = 0; /* reset errno (only if we're using stdio) */
236 #endif
238 if ( name != NULL ) {
239 if ( stat( name, &st ) == 0 )
240 fh = fopen( name, XP_FILE_UPDATE_BIN );
241 else
243 /* should never get here! */
244 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
247 return fh;
249 #endif /*STANDALONE_REGISTRY*/
251 #if defined (XP_MACOSX)
252 extern void vr_findGlobalRegName()
254 OSErr err;
255 FSRef foundRef;
257 err = FSFindFolder(kLocalDomain, kDomainLibraryFolderType, kDontCreateFolder, &foundRef);
258 if (err == noErr)
260 FSRef parentRef;
261 err = FSMakeFSRefUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
262 kTextEncodingUnknown, &parentRef);
263 if (err == fnfErr)
265 err = FSCreateDirectoryUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
266 kFSCatInfoNone, NULL, &parentRef, NULL, NULL);
268 if (err == noErr)
270 FSRef regRef;
271 err = FSMakeFSRefUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXRegName), kOSXRegName,
272 kTextEncodingUnknown, &regRef);
273 if (err == fnfErr)
275 FSCatalogInfo catalogInfo;
276 FileInfo fileInfo = { 'REGS', 'MOSS', 0, { 0, 0 }, 0 };
277 BlockMoveData(&fileInfo, &(catalogInfo.finderInfo), sizeof(FileInfo));
278 err = FSCreateFileUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXRegName), kOSXRegName,
279 kFSCatInfoFinderInfo, &catalogInfo, &regRef, NULL);
281 if (err == noErr)
283 UInt8 pathBuf[PATH_MAX];
284 err = FSRefMakePath(&regRef, pathBuf, sizeof(pathBuf));
285 if (err == noErr)
286 globalRegName = XP_STRDUP(pathBuf);
291 #else
292 extern void vr_findGlobalRegName()
294 FSSpec regSpec;
295 OSErr err;
296 short foundVRefNum;
297 long foundDirID;
298 int bCreate = 0;
300 err = FindFolder(kOnSystemDisk,'pref', false, &foundVRefNum, &foundDirID);
302 if (err == noErr)
304 err = FSMakeFSSpec(foundVRefNum, foundDirID, MAC_REG, &regSpec);
306 if (err == -43) /* if file doesn't exist */
308 err = FSpCreate(&regSpec, 'MOSS', 'REGS', smSystemScript);
309 bCreate = 1;
312 if (err == noErr)
314 Handle thePath;
315 short pathLen;
316 err = FSpGetFullPath(&regSpec, &pathLen, &thePath);
317 if (err == noErr && thePath)
319 /* we have no idea if this moves memory, so better lock the handle */
320 #if defined(STANDALONE_REGISTRY) || defined(USE_STDIO_MODES)
321 HLock(thePath);
322 globalRegName = (char *)XP_ALLOC(pathLen + 1);
323 XP_STRNCPY(globalRegName, *thePath, pathLen);
324 globalRegName[pathLen] = '\0';
325 #else
326 /* Since we're now using NSPR, this HAS to be a unix path! */
327 const char* src;
328 char* dst;
329 HLock(thePath);
330 globalRegName = (char*)XP_ALLOC(pathLen + 2);
331 src = *(char**)thePath;
332 dst = globalRegName;
333 *dst++ = '/';
334 while (pathLen--)
336 char c = *src++;
337 *dst++ = (c == ':') ? '/' : c;
339 *dst = '\0';
340 #endif
342 DisposeHandle(thePath);
346 #endif /* XP_MACOSX */
348 #ifdef XP_MACOSX
349 extern char* vr_findVerRegName()
351 OSErr err;
352 FSRef foundRef;
354 err = FSFindFolder(kLocalDomain, kDomainLibraryFolderType, kDontCreateFolder, &foundRef);
355 if (err == noErr)
357 FSRef parentRef;
358 err = FSMakeFSRefUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
359 kTextEncodingUnknown, &parentRef);
360 if (err == fnfErr)
362 err = FSCreateDirectoryUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
363 kFSCatInfoNone, NULL, &parentRef, NULL, NULL);
365 if (err == noErr)
367 FSRef regRef;
368 err = FSMakeFSRefUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXVersRegName), kOSXVersRegName,
369 kTextEncodingUnknown, &regRef);
370 if (err == fnfErr)
372 FSCatalogInfo catalogInfo;
373 FileInfo fileInfo = { 'REGS', 'MOSS', 0, { 0, 0 }, 0 };
374 BlockMoveData(&fileInfo, &(catalogInfo.finderInfo), sizeof(FileInfo));
375 err = FSCreateFileUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXVersRegName), kOSXVersRegName,
376 kFSCatInfoFinderInfo, &catalogInfo, &regRef, NULL);
378 if (err == noErr)
380 UInt8 pathBuf[PATH_MAX];
381 err = FSRefMakePath(&regRef, pathBuf, sizeof(pathBuf));
382 if (err == noErr)
383 verRegName = XP_STRDUP(pathBuf);
387 return verRegName;
389 #else
390 extern char* vr_findVerRegName()
392 FSSpec regSpec;
393 OSErr err;
394 short foundVRefNum;
395 long foundDirID;
396 int bCreate = 0;
398 /* quick exit if we have the info */
399 if ( verRegName != NULL )
400 return verRegName;
402 err = FindFolder(kOnSystemDisk,'pref', false, &foundVRefNum, &foundDirID);
404 if (err == noErr)
406 err = FSMakeFSSpec(foundVRefNum, foundDirID, MAC_VERREG, &regSpec);
408 if (err == -43) /* if file doesn't exist */
410 err = FSpCreate(&regSpec, 'MOSS', 'REGS', smSystemScript);
411 bCreate = 1;
414 if (err == noErr)
416 Handle thePath;
417 short pathLen;
418 err = FSpGetFullPath(&regSpec, &pathLen, &thePath);
419 if (err == noErr && thePath)
421 /* we have no idea if this moves memory, so better lock the handle */
422 #if defined(STANDALONE_REGISTRY) || defined(USE_STDIO_MODES)
423 HLock(thePath);
424 verRegName = (char *)XP_ALLOC(pathLen + 1);
425 XP_STRNCPY(verRegName, *thePath, pathLen);
426 verRegName[pathLen] = '\0';
427 #else
428 /* Since we're now using NSPR, this HAS to be a unix path! */
429 const char* src;
430 char* dst;
431 HLock(thePath);
432 verRegName = (char*)XP_ALLOC(pathLen + 2);
433 src = *(char**)thePath;
434 dst = verRegName;
435 *dst++ = '/';
436 while (pathLen--)
438 char c = *src++;
439 *dst++ = (c == ':') ? '/' : c;
441 *dst = '\0';
442 #endif
444 DisposeHandle(thePath);
448 return verRegName;
450 #endif /* OS_MACOSX */
452 /* Moves and renames a file or directory.
453 Returns 0 on success, -1 on failure (errno contains mac error code).
455 #ifndef XP_MACOSX
456 extern int nr_RenameFile(char *from, char *to)
458 OSErr err = -1;
459 FSSpec fromSpec;
460 FSSpec toSpec;
461 FSSpec destDirSpec;
462 FSSpec beforeRenameSpec;
464 #ifdef STANDALONE_REGISTRY
465 errno = 0; /* reset errno (only if we're using stdio) */
466 #endif
468 if (from && to) {
469 err = FSpLocationFromFullPath(XP_STRLEN(from), from, &fromSpec);
470 if (err != noErr) goto exit;
472 err = FSpLocationFromFullPath(XP_STRLEN(to), to, &toSpec);
473 if (err != noErr && err != fnfErr) goto exit;
475 /* make an FSSpec for the destination directory */
476 err = FSMakeFSSpec(toSpec.vRefNum, toSpec.parID, nil, &destDirSpec);
477 if (err != noErr) goto exit; /* parent directory must exist */
479 /* move it to the directory specified */
480 err = FSpCatMove(&fromSpec, &destDirSpec);
481 if (err != noErr) goto exit;
483 /* make a new FSSpec for the file or directory in its new location */
484 err = FSMakeFSSpec(toSpec.vRefNum, toSpec.parID, fromSpec.name, &beforeRenameSpec);
485 if (err != noErr) goto exit;
487 /* rename the file or directory */
488 err = FSpRename(&beforeRenameSpec, toSpec.name);
491 exit:
492 #ifdef STANDALONE_REGISTRY
493 if (err != noErr)
494 errno = err;
495 #endif
496 return (err == noErr ? 0 : -1);
498 #endif
501 #ifdef STANDALONE_REGISTRY
502 #ifndef XP_MACOSX
503 char *strdup(const char *source)
505 char *newAllocation;
506 size_t stringLength;
508 stringLength = strlen(source) + 1;
510 newAllocation = (char *)XP_ALLOC(stringLength);
511 if (newAllocation == NULL)
512 return NULL;
513 BlockMoveData(source, newAllocation, stringLength);
514 return newAllocation;
517 int strcasecmp(const char *str1, const char *str2)
519 char currentChar1, currentChar2;
521 while (1) {
523 currentChar1 = *str1;
524 currentChar2 = *str2;
526 if ((currentChar1 >= 'a') && (currentChar1 <= 'z'))
527 currentChar1 += ('A' - 'a');
529 if ((currentChar2 >= 'a') && (currentChar2 <= 'z'))
530 currentChar2 += ('A' - 'a');
532 if (currentChar1 == '\0')
533 break;
535 if (currentChar1 != currentChar2)
536 return currentChar1 - currentChar2;
538 str1++;
539 str2++;
543 return currentChar1 - currentChar2;
546 int strncasecmp(const char *str1, const char *str2, int length)
548 char currentChar1, currentChar2;
550 while (length > 0) {
552 currentChar1 = *str1;
553 currentChar2 = *str2;
555 if ((currentChar1 >= 'a') && (currentChar1 <= 'z'))
556 currentChar1 += ('A' - 'a');
558 if ((currentChar2 >= 'a') && (currentChar2 <= 'z'))
559 currentChar2 += ('A' - 'a');
561 if (currentChar1 == '\0')
562 break;
564 if (currentChar1 != currentChar2)
565 return currentChar1 - currentChar2;
567 str1++;
568 str2++;
570 length--;
573 return currentChar1 - currentChar2;
575 #endif /* XP_MACOSX */
576 #endif /* STANDALONE_REGISTRY */
578 #endif /* XP_MAC */
581 /* ------------------------------------------------------------------
582 * UNIX STUBS
583 * ------------------------------------------------------------------
586 /*allow OS/2 and Macintosh to use this main to test...*/
587 #if (defined(STANDALONE_REGISTRY) && defined(XP_MAC)) || defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
589 #include <stdlib.h>
591 #ifdef XP_OS2
592 #include <io.h>
593 #define W_OK 0x02 /*evil hack from the docs...*/
594 #else
595 #include <unistd.h>
596 #endif
598 #include "NSReg.h"
599 #include "VerReg.h"
601 char *TheRegistry = "registry";
602 char *Flist;
604 REGERR vr_ParseVersion(char *verstr, VERSION *result);
606 #if defined(XP_UNIX) && !defined(XP_MACOSX)
608 #ifdef STANDALONE_REGISTRY
609 extern XP_File vr_fileOpen (const char *name, const char * mode)
611 XP_File fh = NULL;
612 struct stat st;
614 if ( name != NULL ) {
615 if ( stat( name, &st ) == 0 )
616 fh = fopen( name, XP_FILE_UPDATE_BIN );
617 else
618 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
621 return fh;
623 #endif /*STANDALONE_REGISTRY*/
625 extern void vr_findGlobalRegName ()
627 #ifndef STANDALONE_REGISTRY
628 char *def = NULL;
629 char *home = getenv("HOME");
630 if (home != NULL) {
631 def = (char *) XP_ALLOC(XP_STRLEN(home) + XP_STRLEN(DEF_REG)+1);
632 if (def != NULL) {
633 XP_STRCPY(def, home);
634 XP_STRCAT(def, DEF_REG);
637 if (def != NULL) {
638 globalRegName = XP_STRDUP(def);
639 } else {
640 globalRegName = XP_STRDUP(TheRegistry);
642 XP_FREEIF(def);
643 #else
644 globalRegName = XP_STRDUP(TheRegistry);
645 #endif /*STANDALONE_REGISTRY*/
648 char* vr_findVerRegName ()
650 if ( verRegName != NULL )
651 return verRegName;
653 #ifndef STANDALONE_REGISTRY
655 char *def = NULL;
656 char *home = getenv("HOME");
657 if (home != NULL) {
658 def = (char *) XP_ALLOC(XP_STRLEN(home) + XP_STRLEN(DEF_VERREG)+1);
659 if (def != NULL) {
660 XP_STRCPY(def, home);
661 XP_STRCAT(def, DEF_VERREG);
664 if (def != NULL) {
665 verRegName = XP_STRDUP(def);
667 XP_FREEIF(def);
669 #else
670 verRegName = XP_STRDUP(TheRegistry);
671 #endif /*STANDALONE_REGISTRY*/
673 return verRegName;
676 #endif /*XP_UNIX*/
678 /* ------------------------------------------------------------------
679 * BeOS STUBS
680 * ------------------------------------------------------------------
683 #ifdef XP_BEOS
685 #ifdef STANDALONE_REGISTRY
686 extern XP_File vr_fileOpen (const char *name, const char * mode)
688 XP_File fh = NULL;
689 struct stat st;
691 if ( name != NULL ) {
692 if ( stat( name, &st ) == 0 )
693 fh = fopen( name, XP_FILE_UPDATE_BIN );
694 else
695 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
698 return fh;
700 #endif /*STANDALONE_REGISTRY*/
702 extern void vr_findGlobalRegName ()
704 #ifndef STANDALONE_REGISTRY
705 char *def = NULL;
706 char settings[1024];
707 find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, settings, sizeof(settings));
708 if (settings != NULL) {
709 def = (char *) XP_ALLOC(XP_STRLEN(settings) + XP_STRLEN(BEOS_REG)+1);
710 if (def != NULL) {
711 XP_STRCPY(def, settings);
712 XP_STRCAT(def, BEOS_REG);
715 if (def != NULL) {
716 globalRegName = XP_STRDUP(def);
717 } else {
718 globalRegName = XP_STRDUP(TheRegistry);
720 XP_FREEIF(def);
721 #else
722 globalRegName = XP_STRDUP(TheRegistry);
723 #endif /*STANDALONE_REGISTRY*/
726 char* vr_findVerRegName ()
728 if ( verRegName != NULL )
729 return verRegName;
731 #ifndef STANDALONE_REGISTRY
733 char *def = NULL;
734 char settings[1024];
735 find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, settings, sizeof(settings));
736 if (settings != NULL) {
737 def = (char *) XP_ALLOC(XP_STRLEN(settings) + XP_STRLEN(BEOS_VERREG)+1);
738 if (def != NULL) {
739 XP_STRCPY(def, settings);
740 XP_STRCAT(def, BEOS_VERREG);
743 if (def != NULL) {
744 verRegName = XP_STRDUP(def);
746 XP_FREEIF(def);
748 #else
749 verRegName = XP_STRDUP(TheRegistry);
750 #endif /*STANDALONE_REGISTRY*/
752 return verRegName;
755 #endif /*XP_BEOS*/
757 #endif /* XP_UNIX || XP_OS2 */