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
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.
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
53 #endif /*STANDALONE_REGISTRY*/
62 #include "MoreFiles.h"
63 #include "FullPath.h" /* For FSpLocationFromFullPath() */
66 #if defined(XP_MACOSX)
72 #include <FindDirectory.h>
76 /* So that we're not dependent on the size of chars in a wide string literal */
77 static const UniChar kOSXRegParentName
[] =
78 { 'M', 'o', 'z', 'i', 'l', 'l', 'a' };
79 static const UniChar kOSXRegName
[] =
80 { 'G', 'l', 'o', 'b', 'a', 'l', '.', 'r', 'e', 'g', 's' };
81 static const UniChar kOSXVersRegName
[] =
82 { 'V', 'e', 'r', 's', 'i', 'o', 'n', 's', '.', 'r', 'e', 'g', 's' };
84 #define UNICHAR_ARRAY_LEN(s) (sizeof(s) / sizeof(UniChar))
87 #define DEF_REG "/.mozilla/registry"
88 #define WIN_REG "\\mozregistry.dat"
89 #define MAC_REG "\pMozilla Registry"
90 #define BEOS_REG "/mozilla/registry"
92 #define DEF_VERREG "/.mozilla/mozver.dat"
93 #define WIN_VERREG "\\mozver.dat"
94 #define MAC_VERREG "\pMozilla Versions"
95 #define BEOS_VERREG "/mozilla/mozver.dat"
98 /* ------------------------------------------------------------------
100 * ------------------------------------------------------------------
106 #ifdef STANDALONE_REGISTRY
107 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
112 if ( name
!= NULL
) {
113 if ( stat( name
, &st
) == 0 )
114 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
116 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
121 #endif /*STANDALONE_REGISTRY*/
123 extern void vr_findGlobalRegName ()
125 char path
[ CCHMAXPATH
];
130 XP_STRCPY(path
, ".");
131 pathlen
= strlen(path
);
134 XP_STRCPY( path
+pathlen
, WIN_REG
);
135 globalRegName
= XP_STRDUP(path
);
139 char* vr_findVerRegName()
141 /* need to find a global place for the version registry */
142 if ( verRegName
== NULL
)
144 if ( globalRegName
== NULL
)
145 vr_findGlobalRegName();
146 verRegName
= XP_STRDUP(globalRegName
);
155 /* ------------------------------------------------------------------
157 * ------------------------------------------------------------------
163 #ifdef STANDALONE_REGISTRY
164 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
169 if ( name
!= NULL
) {
170 if ( stat( name
, &st
) == 0 )
171 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
173 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
178 #endif /*STANDALONE_REGISTRY*/
180 extern void vr_findGlobalRegName ()
182 char path
[ PATHLEN
];
185 pathlen
= GetWindowsDirectory(path
, PATHLEN
);
187 XP_FREEIF(globalRegName
);
188 XP_STRCPY( path
+pathlen
, WIN_REG
);
189 globalRegName
= XP_STRDUP(path
);
193 char* vr_findVerRegName()
195 char path
[ PATHLEN
];
198 if ( verRegName
== NULL
)
200 pathlen
= GetWindowsDirectory(path
, PATHLEN
);
202 XP_STRCPY( path
+pathlen
, WIN_VERREG
);
203 verRegName
= XP_STRDUP(path
);
210 #if !defined(WIN32) && !defined(__BORLANDC__)
211 int FAR PASCAL _export
WEP(int);
213 int FAR PASCAL
LibMain(HANDLE hInst
, WORD wDataSeg
, WORD wHeapSize
, LPSTR lpszCmdLine
)
220 int FAR PASCAL _export
WEP(int nParam
)
224 #endif /* not WIN32 */
229 /* ------------------------------------------------------------------
231 * ------------------------------------------------------------------
234 #if defined(XP_MAC) || defined(XP_MACOSX)
237 #ifdef STANDALONE_REGISTRY
238 extern XP_File
vr_fileOpen(const char *name
, const char * mode
)
243 #ifdef STANDALONE_REGISTRY
244 errno
= 0; /* reset errno (only if we're using stdio) */
247 if ( name
!= NULL
) {
248 if ( stat( name
, &st
) == 0 )
249 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
252 /* should never get here! */
253 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
258 #endif /*STANDALONE_REGISTRY*/
260 #if defined (XP_MACOSX)
261 extern void vr_findGlobalRegName()
266 err
= FSFindFolder(kLocalDomain
, kDomainLibraryFolderType
, kDontCreateFolder
, &foundRef
);
270 err
= FSMakeFSRefUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
271 kTextEncodingUnknown
, &parentRef
);
274 err
= FSCreateDirectoryUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
275 kFSCatInfoNone
, NULL
, &parentRef
, NULL
, NULL
);
280 err
= FSMakeFSRefUnicode(&parentRef
, UNICHAR_ARRAY_LEN(kOSXRegName
), kOSXRegName
,
281 kTextEncodingUnknown
, ®Ref
);
284 FSCatalogInfo catalogInfo
;
285 FileInfo fileInfo
= { 'REGS', 'MOSS', 0, { 0, 0 }, 0 };
286 BlockMoveData(&fileInfo
, &(catalogInfo
.finderInfo
), sizeof(FileInfo
));
287 err
= FSCreateFileUnicode(&parentRef
, UNICHAR_ARRAY_LEN(kOSXRegName
), kOSXRegName
,
288 kFSCatInfoFinderInfo
, &catalogInfo
, ®Ref
, NULL
);
292 UInt8 pathBuf
[PATH_MAX
];
293 err
= FSRefMakePath(®Ref
, pathBuf
, sizeof(pathBuf
));
295 globalRegName
= XP_STRDUP(pathBuf
);
301 extern void vr_findGlobalRegName()
309 err
= FindFolder(kOnSystemDisk
,'pref', false, &foundVRefNum
, &foundDirID
);
313 err
= FSMakeFSSpec(foundVRefNum
, foundDirID
, MAC_REG
, ®Spec
);
315 if (err
== -43) /* if file doesn't exist */
317 err
= FSpCreate(®Spec
, 'MOSS', 'REGS', smSystemScript
);
325 err
= FSpGetFullPath(®Spec
, &pathLen
, &thePath
);
326 if (err
== noErr
&& thePath
)
328 /* we have no idea if this moves memory, so better lock the handle */
329 #if defined(STANDALONE_REGISTRY) || defined(USE_STDIO_MODES)
331 globalRegName
= (char *)XP_ALLOC(pathLen
+ 1);
332 XP_STRNCPY(globalRegName
, *thePath
, pathLen
);
333 globalRegName
[pathLen
] = '\0';
335 /* Since we're now using NSPR, this HAS to be a unix path! */
339 globalRegName
= (char*)XP_ALLOC(pathLen
+ 2);
340 src
= *(char**)thePath
;
346 *dst
++ = (c
== ':') ? '/' : c
;
351 DisposeHandle(thePath
);
355 #endif /* XP_MACOSX */
358 extern char* vr_findVerRegName()
363 err
= FSFindFolder(kLocalDomain
, kDomainLibraryFolderType
, kDontCreateFolder
, &foundRef
);
367 err
= FSMakeFSRefUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
368 kTextEncodingUnknown
, &parentRef
);
371 err
= FSCreateDirectoryUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
372 kFSCatInfoNone
, NULL
, &parentRef
, NULL
, NULL
);
377 err
= FSMakeFSRefUnicode(&parentRef
, UNICHAR_ARRAY_LEN(kOSXVersRegName
), kOSXVersRegName
,
378 kTextEncodingUnknown
, ®Ref
);
381 FSCatalogInfo catalogInfo
;
382 FileInfo fileInfo
= { 'REGS', 'MOSS', 0, { 0, 0 }, 0 };
383 BlockMoveData(&fileInfo
, &(catalogInfo
.finderInfo
), sizeof(FileInfo
));
384 err
= FSCreateFileUnicode(&parentRef
, UNICHAR_ARRAY_LEN(kOSXVersRegName
), kOSXVersRegName
,
385 kFSCatInfoFinderInfo
, &catalogInfo
, ®Ref
, NULL
);
389 UInt8 pathBuf
[PATH_MAX
];
390 err
= FSRefMakePath(®Ref
, pathBuf
, sizeof(pathBuf
));
392 verRegName
= XP_STRDUP(pathBuf
);
399 extern char* vr_findVerRegName()
407 /* quick exit if we have the info */
408 if ( verRegName
!= NULL
)
411 err
= FindFolder(kOnSystemDisk
,'pref', false, &foundVRefNum
, &foundDirID
);
415 err
= FSMakeFSSpec(foundVRefNum
, foundDirID
, MAC_VERREG
, ®Spec
);
417 if (err
== -43) /* if file doesn't exist */
419 err
= FSpCreate(®Spec
, 'MOSS', 'REGS', smSystemScript
);
427 err
= FSpGetFullPath(®Spec
, &pathLen
, &thePath
);
428 if (err
== noErr
&& thePath
)
430 /* we have no idea if this moves memory, so better lock the handle */
431 #if defined(STANDALONE_REGISTRY) || defined(USE_STDIO_MODES)
433 verRegName
= (char *)XP_ALLOC(pathLen
+ 1);
434 XP_STRNCPY(verRegName
, *thePath
, pathLen
);
435 verRegName
[pathLen
] = '\0';
437 /* Since we're now using NSPR, this HAS to be a unix path! */
441 verRegName
= (char*)XP_ALLOC(pathLen
+ 2);
442 src
= *(char**)thePath
;
448 *dst
++ = (c
== ':') ? '/' : c
;
453 DisposeHandle(thePath
);
459 #endif /* OS_MACOSX */
461 /* Moves and renames a file or directory.
462 Returns 0 on success, -1 on failure (errno contains mac error code).
465 extern int nr_RenameFile(char *from
, char *to
)
471 FSSpec beforeRenameSpec
;
473 #ifdef STANDALONE_REGISTRY
474 errno
= 0; /* reset errno (only if we're using stdio) */
478 err
= FSpLocationFromFullPath(XP_STRLEN(from
), from
, &fromSpec
);
479 if (err
!= noErr
) goto exit
;
481 err
= FSpLocationFromFullPath(XP_STRLEN(to
), to
, &toSpec
);
482 if (err
!= noErr
&& err
!= fnfErr
) goto exit
;
484 /* make an FSSpec for the destination directory */
485 err
= FSMakeFSSpec(toSpec
.vRefNum
, toSpec
.parID
, nil
, &destDirSpec
);
486 if (err
!= noErr
) goto exit
; /* parent directory must exist */
488 /* move it to the directory specified */
489 err
= FSpCatMove(&fromSpec
, &destDirSpec
);
490 if (err
!= noErr
) goto exit
;
492 /* make a new FSSpec for the file or directory in its new location */
493 err
= FSMakeFSSpec(toSpec
.vRefNum
, toSpec
.parID
, fromSpec
.name
, &beforeRenameSpec
);
494 if (err
!= noErr
) goto exit
;
496 /* rename the file or directory */
497 err
= FSpRename(&beforeRenameSpec
, toSpec
.name
);
501 #ifdef STANDALONE_REGISTRY
505 return (err
== noErr
? 0 : -1);
510 #ifdef STANDALONE_REGISTRY
512 char *strdup(const char *source
)
517 stringLength
= strlen(source
) + 1;
519 newAllocation
= (char *)XP_ALLOC(stringLength
);
520 if (newAllocation
== NULL
)
522 BlockMoveData(source
, newAllocation
, stringLength
);
523 return newAllocation
;
526 int strcasecmp(const char *str1
, const char *str2
)
528 char currentChar1
, currentChar2
;
532 currentChar1
= *str1
;
533 currentChar2
= *str2
;
535 if ((currentChar1
>= 'a') && (currentChar1
<= 'z'))
536 currentChar1
+= ('A' - 'a');
538 if ((currentChar2
>= 'a') && (currentChar2
<= 'z'))
539 currentChar2
+= ('A' - 'a');
541 if (currentChar1
== '\0')
544 if (currentChar1
!= currentChar2
)
545 return currentChar1
- currentChar2
;
552 return currentChar1
- currentChar2
;
555 int strncasecmp(const char *str1
, const char *str2
, int length
)
557 char currentChar1
, currentChar2
;
561 currentChar1
= *str1
;
562 currentChar2
= *str2
;
564 if ((currentChar1
>= 'a') && (currentChar1
<= 'z'))
565 currentChar1
+= ('A' - 'a');
567 if ((currentChar2
>= 'a') && (currentChar2
<= 'z'))
568 currentChar2
+= ('A' - 'a');
570 if (currentChar1
== '\0')
573 if (currentChar1
!= currentChar2
)
574 return currentChar1
- currentChar2
;
582 return currentChar1
- currentChar2
;
584 #endif /* XP_MACOSX */
585 #endif /* STANDALONE_REGISTRY */
590 /* ------------------------------------------------------------------
592 * ------------------------------------------------------------------
595 /*allow OS/2 and Macintosh to use this main to test...*/
596 #if (defined(STANDALONE_REGISTRY) && defined(XP_MAC)) || defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
602 #define W_OK 0x02 /*evil hack from the docs...*/
610 char *TheRegistry
= "registry";
613 REGERR
vr_ParseVersion(char *verstr
, VERSION
*result
);
615 #if defined(XP_UNIX) && !defined(XP_MACOSX)
617 #ifdef STANDALONE_REGISTRY
618 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
623 if ( name
!= NULL
) {
624 if ( stat( name
, &st
) == 0 )
625 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
627 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
632 #endif /*STANDALONE_REGISTRY*/
634 extern void vr_findGlobalRegName ()
636 #ifndef STANDALONE_REGISTRY
638 char *home
= getenv("HOME");
640 def
= (char *) XP_ALLOC(XP_STRLEN(home
) + XP_STRLEN(DEF_REG
)+1);
642 XP_STRCPY(def
, home
);
643 XP_STRCAT(def
, DEF_REG
);
647 globalRegName
= XP_STRDUP(def
);
649 globalRegName
= XP_STRDUP(TheRegistry
);
653 globalRegName
= XP_STRDUP(TheRegistry
);
654 #endif /*STANDALONE_REGISTRY*/
657 char* vr_findVerRegName ()
659 if ( verRegName
!= NULL
)
662 #ifndef STANDALONE_REGISTRY
665 char *home
= getenv("HOME");
667 def
= (char *) XP_ALLOC(XP_STRLEN(home
) + XP_STRLEN(DEF_VERREG
)+1);
669 XP_STRCPY(def
, home
);
670 XP_STRCAT(def
, DEF_VERREG
);
674 verRegName
= XP_STRDUP(def
);
679 verRegName
= XP_STRDUP(TheRegistry
);
680 #endif /*STANDALONE_REGISTRY*/
687 /* ------------------------------------------------------------------
689 * ------------------------------------------------------------------
694 #ifdef STANDALONE_REGISTRY
695 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
700 if ( name
!= NULL
) {
701 if ( stat( name
, &st
) == 0 )
702 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
704 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
709 #endif /*STANDALONE_REGISTRY*/
711 extern void vr_findGlobalRegName ()
713 #ifndef STANDALONE_REGISTRY
716 find_directory(B_USER_SETTINGS_DIRECTORY
, -1, false, settings
, sizeof(settings
));
717 if (settings
!= NULL
) {
718 def
= (char *) XP_ALLOC(XP_STRLEN(settings
) + XP_STRLEN(BEOS_REG
)+1);
720 XP_STRCPY(def
, settings
);
721 XP_STRCAT(def
, BEOS_REG
);
725 globalRegName
= XP_STRDUP(def
);
727 globalRegName
= XP_STRDUP(TheRegistry
);
731 globalRegName
= XP_STRDUP(TheRegistry
);
732 #endif /*STANDALONE_REGISTRY*/
735 char* vr_findVerRegName ()
737 if ( verRegName
!= NULL
)
740 #ifndef STANDALONE_REGISTRY
744 find_directory(B_USER_SETTINGS_DIRECTORY
, -1, false, settings
, sizeof(settings
));
745 if (settings
!= NULL
) {
746 def
= (char *) XP_ALLOC(XP_STRLEN(settings
) + XP_STRLEN(BEOS_VERREG
)+1);
748 XP_STRCPY(def
, settings
);
749 XP_STRCAT(def
, BEOS_VERREG
);
753 verRegName
= XP_STRDUP(def
);
758 verRegName
= XP_STRDUP(TheRegistry
);
759 #endif /*STANDALONE_REGISTRY*/
766 #endif /* XP_UNIX || XP_OS2 */