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*/
58 #include <Carbon/Carbon.h>
63 #include <FindDirectory.h>
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))
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 /* ------------------------------------------------------------------
91 * ------------------------------------------------------------------
97 #ifdef STANDALONE_REGISTRY
98 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
103 if ( name
!= NULL
) {
104 if ( stat( name
, &st
) == 0 )
105 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
107 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
112 #endif /*STANDALONE_REGISTRY*/
114 extern void vr_findGlobalRegName ()
116 char path
[ CCHMAXPATH
];
121 XP_STRCPY(path
, ".");
122 pathlen
= strlen(path
);
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
);
146 /* ------------------------------------------------------------------
148 * ------------------------------------------------------------------
154 #ifdef STANDALONE_REGISTRY
155 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
160 if ( name
!= NULL
) {
161 if ( stat( name
, &st
) == 0 )
162 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
164 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
169 #endif /*STANDALONE_REGISTRY*/
171 extern void vr_findGlobalRegName ()
173 char path
[ PATHLEN
];
176 pathlen
= GetWindowsDirectory(path
, PATHLEN
);
178 XP_FREEIF(globalRegName
);
179 XP_STRCPY( path
+pathlen
, WIN_REG
);
180 globalRegName
= XP_STRDUP(path
);
184 char* vr_findVerRegName()
186 char path
[ PATHLEN
];
189 if ( verRegName
== NULL
)
191 pathlen
= GetWindowsDirectory(path
, PATHLEN
);
193 XP_STRCPY( path
+pathlen
, WIN_VERREG
);
194 verRegName
= XP_STRDUP(path
);
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
)
211 int FAR PASCAL _export
WEP(int nParam
)
215 #endif /* not WIN32 */
220 /* ------------------------------------------------------------------
222 * ------------------------------------------------------------------
225 #if defined(XP_MAC) || defined(XP_MACOSX)
228 #ifdef STANDALONE_REGISTRY
229 extern XP_File
vr_fileOpen(const char *name
, const char * mode
)
234 #ifdef STANDALONE_REGISTRY
235 errno
= 0; /* reset errno (only if we're using stdio) */
238 if ( name
!= NULL
) {
239 if ( stat( name
, &st
) == 0 )
240 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
243 /* should never get here! */
244 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
249 #endif /*STANDALONE_REGISTRY*/
251 #if defined (XP_MACOSX)
252 extern void vr_findGlobalRegName()
257 err
= FSFindFolder(kLocalDomain
, kDomainLibraryFolderType
, kDontCreateFolder
, &foundRef
);
261 err
= FSMakeFSRefUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
262 kTextEncodingUnknown
, &parentRef
);
265 err
= FSCreateDirectoryUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
266 kFSCatInfoNone
, NULL
, &parentRef
, NULL
, NULL
);
271 err
= FSMakeFSRefUnicode(&parentRef
, UNICHAR_ARRAY_LEN(kOSXRegName
), kOSXRegName
,
272 kTextEncodingUnknown
, ®Ref
);
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
, ®Ref
, NULL
);
283 UInt8 pathBuf
[PATH_MAX
];
284 err
= FSRefMakePath(®Ref
, pathBuf
, sizeof(pathBuf
));
286 globalRegName
= XP_STRDUP(pathBuf
);
292 extern void vr_findGlobalRegName()
300 err
= FindFolder(kOnSystemDisk
,'pref', false, &foundVRefNum
, &foundDirID
);
304 err
= FSMakeFSSpec(foundVRefNum
, foundDirID
, MAC_REG
, ®Spec
);
306 if (err
== -43) /* if file doesn't exist */
308 err
= FSpCreate(®Spec
, 'MOSS', 'REGS', smSystemScript
);
316 err
= FSpGetFullPath(®Spec
, &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)
322 globalRegName
= (char *)XP_ALLOC(pathLen
+ 1);
323 XP_STRNCPY(globalRegName
, *thePath
, pathLen
);
324 globalRegName
[pathLen
] = '\0';
326 /* Since we're now using NSPR, this HAS to be a unix path! */
330 globalRegName
= (char*)XP_ALLOC(pathLen
+ 2);
331 src
= *(char**)thePath
;
337 *dst
++ = (c
== ':') ? '/' : c
;
342 DisposeHandle(thePath
);
346 #endif /* XP_MACOSX */
349 extern char* vr_findVerRegName()
354 err
= FSFindFolder(kLocalDomain
, kDomainLibraryFolderType
, kDontCreateFolder
, &foundRef
);
358 err
= FSMakeFSRefUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
359 kTextEncodingUnknown
, &parentRef
);
362 err
= FSCreateDirectoryUnicode(&foundRef
, UNICHAR_ARRAY_LEN(kOSXRegParentName
), kOSXRegParentName
,
363 kFSCatInfoNone
, NULL
, &parentRef
, NULL
, NULL
);
368 err
= FSMakeFSRefUnicode(&parentRef
, UNICHAR_ARRAY_LEN(kOSXVersRegName
), kOSXVersRegName
,
369 kTextEncodingUnknown
, ®Ref
);
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
, ®Ref
, NULL
);
380 UInt8 pathBuf
[PATH_MAX
];
381 err
= FSRefMakePath(®Ref
, pathBuf
, sizeof(pathBuf
));
383 verRegName
= XP_STRDUP(pathBuf
);
390 extern char* vr_findVerRegName()
398 /* quick exit if we have the info */
399 if ( verRegName
!= NULL
)
402 err
= FindFolder(kOnSystemDisk
,'pref', false, &foundVRefNum
, &foundDirID
);
406 err
= FSMakeFSSpec(foundVRefNum
, foundDirID
, MAC_VERREG
, ®Spec
);
408 if (err
== -43) /* if file doesn't exist */
410 err
= FSpCreate(®Spec
, 'MOSS', 'REGS', smSystemScript
);
418 err
= FSpGetFullPath(®Spec
, &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)
424 verRegName
= (char *)XP_ALLOC(pathLen
+ 1);
425 XP_STRNCPY(verRegName
, *thePath
, pathLen
);
426 verRegName
[pathLen
] = '\0';
428 /* Since we're now using NSPR, this HAS to be a unix path! */
432 verRegName
= (char*)XP_ALLOC(pathLen
+ 2);
433 src
= *(char**)thePath
;
439 *dst
++ = (c
== ':') ? '/' : c
;
444 DisposeHandle(thePath
);
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).
456 extern int nr_RenameFile(char *from
, char *to
)
462 FSSpec beforeRenameSpec
;
464 #ifdef STANDALONE_REGISTRY
465 errno
= 0; /* reset errno (only if we're using stdio) */
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
);
492 #ifdef STANDALONE_REGISTRY
496 return (err
== noErr
? 0 : -1);
501 #ifdef STANDALONE_REGISTRY
503 char *strdup(const char *source
)
508 stringLength
= strlen(source
) + 1;
510 newAllocation
= (char *)XP_ALLOC(stringLength
);
511 if (newAllocation
== NULL
)
513 BlockMoveData(source
, newAllocation
, stringLength
);
514 return newAllocation
;
517 int strcasecmp(const char *str1
, const char *str2
)
519 char currentChar1
, currentChar2
;
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')
535 if (currentChar1
!= currentChar2
)
536 return currentChar1
- currentChar2
;
543 return currentChar1
- currentChar2
;
546 int strncasecmp(const char *str1
, const char *str2
, int length
)
548 char currentChar1
, currentChar2
;
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')
564 if (currentChar1
!= currentChar2
)
565 return currentChar1
- currentChar2
;
573 return currentChar1
- currentChar2
;
575 #endif /* XP_MACOSX */
576 #endif /* STANDALONE_REGISTRY */
581 /* ------------------------------------------------------------------
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)
593 #define W_OK 0x02 /*evil hack from the docs...*/
601 char *TheRegistry
= "registry";
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
)
614 if ( name
!= NULL
) {
615 if ( stat( name
, &st
) == 0 )
616 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
618 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
623 #endif /*STANDALONE_REGISTRY*/
625 extern void vr_findGlobalRegName ()
627 #ifndef STANDALONE_REGISTRY
629 char *home
= getenv("HOME");
631 def
= (char *) XP_ALLOC(XP_STRLEN(home
) + XP_STRLEN(DEF_REG
)+1);
633 XP_STRCPY(def
, home
);
634 XP_STRCAT(def
, DEF_REG
);
638 globalRegName
= XP_STRDUP(def
);
640 globalRegName
= XP_STRDUP(TheRegistry
);
644 globalRegName
= XP_STRDUP(TheRegistry
);
645 #endif /*STANDALONE_REGISTRY*/
648 char* vr_findVerRegName ()
650 if ( verRegName
!= NULL
)
653 #ifndef STANDALONE_REGISTRY
656 char *home
= getenv("HOME");
658 def
= (char *) XP_ALLOC(XP_STRLEN(home
) + XP_STRLEN(DEF_VERREG
)+1);
660 XP_STRCPY(def
, home
);
661 XP_STRCAT(def
, DEF_VERREG
);
665 verRegName
= XP_STRDUP(def
);
670 verRegName
= XP_STRDUP(TheRegistry
);
671 #endif /*STANDALONE_REGISTRY*/
678 /* ------------------------------------------------------------------
680 * ------------------------------------------------------------------
685 #ifdef STANDALONE_REGISTRY
686 extern XP_File
vr_fileOpen (const char *name
, const char * mode
)
691 if ( name
!= NULL
) {
692 if ( stat( name
, &st
) == 0 )
693 fh
= fopen( name
, XP_FILE_UPDATE_BIN
);
695 fh
= fopen( name
, XP_FILE_TRUNCATE_BIN
);
700 #endif /*STANDALONE_REGISTRY*/
702 extern void vr_findGlobalRegName ()
704 #ifndef STANDALONE_REGISTRY
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);
711 XP_STRCPY(def
, settings
);
712 XP_STRCAT(def
, BEOS_REG
);
716 globalRegName
= XP_STRDUP(def
);
718 globalRegName
= XP_STRDUP(TheRegistry
);
722 globalRegName
= XP_STRDUP(TheRegistry
);
723 #endif /*STANDALONE_REGISTRY*/
726 char* vr_findVerRegName ()
728 if ( verRegName
!= NULL
)
731 #ifndef STANDALONE_REGISTRY
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);
739 XP_STRCPY(def
, settings
);
740 XP_STRCAT(def
, BEOS_VERREG
);
744 verRegName
= XP_STRDUP(def
);
749 verRegName
= XP_STRDUP(TheRegistry
);
750 #endif /*STANDALONE_REGISTRY*/
757 #endif /* XP_UNIX || XP_OS2 */