Reland bug 121341.
[wine-gecko.git] / modules / libreg / src / vr_stubs.c
blobf407259a3f68661052fabb7f4211af9f5b6d6503
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 #if defined(XP_MAC)
58 #include <Folders.h>
59 #include <Script.h>
60 #include <stdlib.h>
61 #include <Errors.h>
62 #include "MoreFiles.h"
63 #include "FullPath.h" /* For FSpLocationFromFullPath() */
64 #endif
66 #if defined(XP_MACOSX)
67 #include <Folders.h>
68 #include <stdlib.h>
69 #endif
71 #ifdef XP_BEOS
72 #include <FindDirectory.h>
73 #endif
75 #ifdef XP_MACOSX
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))
85 #endif
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 /* ------------------------------------------------------------------
99 * OS/2 STUBS
100 * ------------------------------------------------------------------
102 #ifdef XP_OS2
103 #define INCL_DOS
104 #include <os2.h>
106 #ifdef STANDALONE_REGISTRY
107 extern XP_File vr_fileOpen (const char *name, const char * mode)
109 XP_File fh = NULL;
110 struct stat st;
112 if ( name != NULL ) {
113 if ( stat( name, &st ) == 0 )
114 fh = fopen( name, XP_FILE_UPDATE_BIN );
115 else
116 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
119 return fh;
121 #endif /*STANDALONE_REGISTRY*/
123 extern void vr_findGlobalRegName ()
125 char path[ CCHMAXPATH ];
126 int pathlen;
127 XP_File fh = NULL;
128 struct stat st;
130 XP_STRCPY(path, ".");
131 pathlen = strlen(path);
133 if ( pathlen > 0 ) {
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);
149 return verRegName;
152 #endif /* XP_OS2 */
155 /* ------------------------------------------------------------------
156 * WINDOWS STUBS
157 * ------------------------------------------------------------------
159 #if defined(XP_WIN)
160 #include "windows.h"
161 #define PATHLEN 260
163 #ifdef STANDALONE_REGISTRY
164 extern XP_File vr_fileOpen (const char *name, const char * mode)
166 XP_File fh = NULL;
167 struct stat st;
169 if ( name != NULL ) {
170 if ( stat( name, &st ) == 0 )
171 fh = fopen( name, XP_FILE_UPDATE_BIN );
172 else
173 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
176 return fh;
178 #endif /*STANDALONE_REGISTRY*/
180 extern void vr_findGlobalRegName ()
182 char path[ PATHLEN ];
183 int pathlen;
185 pathlen = GetWindowsDirectory(path, PATHLEN);
186 if ( pathlen > 0 ) {
187 XP_FREEIF(globalRegName);
188 XP_STRCPY( path+pathlen, WIN_REG );
189 globalRegName = XP_STRDUP(path);
193 char* vr_findVerRegName()
195 char path[ PATHLEN ];
196 int pathlen;
198 if ( verRegName == NULL )
200 pathlen = GetWindowsDirectory(path, PATHLEN);
201 if ( pathlen > 0 ) {
202 XP_STRCPY( path+pathlen, WIN_VERREG );
203 verRegName = XP_STRDUP(path);
207 return verRegName;
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)
215 if ( wHeapSize > 0 )
216 UnlockData(0);
217 return 1;
220 int FAR PASCAL _export WEP(int nParam)
222 return 1;
224 #endif /* not WIN32 */
226 #endif /* XP_WIN */
229 /* ------------------------------------------------------------------
230 * MACINTOSH STUBS
231 * ------------------------------------------------------------------
234 #if defined(XP_MAC) || defined(XP_MACOSX)
235 #include <Files.h>
237 #ifdef STANDALONE_REGISTRY
238 extern XP_File vr_fileOpen(const char *name, const char * mode)
240 XP_File fh = NULL;
241 struct stat st;
243 #ifdef STANDALONE_REGISTRY
244 errno = 0; /* reset errno (only if we're using stdio) */
245 #endif
247 if ( name != NULL ) {
248 if ( stat( name, &st ) == 0 )
249 fh = fopen( name, XP_FILE_UPDATE_BIN );
250 else
252 /* should never get here! */
253 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
256 return fh;
258 #endif /*STANDALONE_REGISTRY*/
260 #if defined (XP_MACOSX)
261 extern void vr_findGlobalRegName()
263 OSErr err;
264 FSRef foundRef;
266 err = FSFindFolder(kLocalDomain, kDomainLibraryFolderType, kDontCreateFolder, &foundRef);
267 if (err == noErr)
269 FSRef parentRef;
270 err = FSMakeFSRefUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
271 kTextEncodingUnknown, &parentRef);
272 if (err == fnfErr)
274 err = FSCreateDirectoryUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
275 kFSCatInfoNone, NULL, &parentRef, NULL, NULL);
277 if (err == noErr)
279 FSRef regRef;
280 err = FSMakeFSRefUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXRegName), kOSXRegName,
281 kTextEncodingUnknown, &regRef);
282 if (err == fnfErr)
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, &regRef, NULL);
290 if (err == noErr)
292 UInt8 pathBuf[PATH_MAX];
293 err = FSRefMakePath(&regRef, pathBuf, sizeof(pathBuf));
294 if (err == noErr)
295 globalRegName = XP_STRDUP(pathBuf);
300 #else
301 extern void vr_findGlobalRegName()
303 FSSpec regSpec;
304 OSErr err;
305 short foundVRefNum;
306 long foundDirID;
307 int bCreate = 0;
309 err = FindFolder(kOnSystemDisk,'pref', false, &foundVRefNum, &foundDirID);
311 if (err == noErr)
313 err = FSMakeFSSpec(foundVRefNum, foundDirID, MAC_REG, &regSpec);
315 if (err == -43) /* if file doesn't exist */
317 err = FSpCreate(&regSpec, 'MOSS', 'REGS', smSystemScript);
318 bCreate = 1;
321 if (err == noErr)
323 Handle thePath;
324 short pathLen;
325 err = FSpGetFullPath(&regSpec, &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)
330 HLock(thePath);
331 globalRegName = (char *)XP_ALLOC(pathLen + 1);
332 XP_STRNCPY(globalRegName, *thePath, pathLen);
333 globalRegName[pathLen] = '\0';
334 #else
335 /* Since we're now using NSPR, this HAS to be a unix path! */
336 const char* src;
337 char* dst;
338 HLock(thePath);
339 globalRegName = (char*)XP_ALLOC(pathLen + 2);
340 src = *(char**)thePath;
341 dst = globalRegName;
342 *dst++ = '/';
343 while (pathLen--)
345 char c = *src++;
346 *dst++ = (c == ':') ? '/' : c;
348 *dst = '\0';
349 #endif
351 DisposeHandle(thePath);
355 #endif /* XP_MACOSX */
357 #ifdef XP_MACOSX
358 extern char* vr_findVerRegName()
360 OSErr err;
361 FSRef foundRef;
363 err = FSFindFolder(kLocalDomain, kDomainLibraryFolderType, kDontCreateFolder, &foundRef);
364 if (err == noErr)
366 FSRef parentRef;
367 err = FSMakeFSRefUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
368 kTextEncodingUnknown, &parentRef);
369 if (err == fnfErr)
371 err = FSCreateDirectoryUnicode(&foundRef, UNICHAR_ARRAY_LEN(kOSXRegParentName), kOSXRegParentName,
372 kFSCatInfoNone, NULL, &parentRef, NULL, NULL);
374 if (err == noErr)
376 FSRef regRef;
377 err = FSMakeFSRefUnicode(&parentRef, UNICHAR_ARRAY_LEN(kOSXVersRegName), kOSXVersRegName,
378 kTextEncodingUnknown, &regRef);
379 if (err == fnfErr)
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, &regRef, NULL);
387 if (err == noErr)
389 UInt8 pathBuf[PATH_MAX];
390 err = FSRefMakePath(&regRef, pathBuf, sizeof(pathBuf));
391 if (err == noErr)
392 verRegName = XP_STRDUP(pathBuf);
396 return verRegName;
398 #else
399 extern char* vr_findVerRegName()
401 FSSpec regSpec;
402 OSErr err;
403 short foundVRefNum;
404 long foundDirID;
405 int bCreate = 0;
407 /* quick exit if we have the info */
408 if ( verRegName != NULL )
409 return verRegName;
411 err = FindFolder(kOnSystemDisk,'pref', false, &foundVRefNum, &foundDirID);
413 if (err == noErr)
415 err = FSMakeFSSpec(foundVRefNum, foundDirID, MAC_VERREG, &regSpec);
417 if (err == -43) /* if file doesn't exist */
419 err = FSpCreate(&regSpec, 'MOSS', 'REGS', smSystemScript);
420 bCreate = 1;
423 if (err == noErr)
425 Handle thePath;
426 short pathLen;
427 err = FSpGetFullPath(&regSpec, &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)
432 HLock(thePath);
433 verRegName = (char *)XP_ALLOC(pathLen + 1);
434 XP_STRNCPY(verRegName, *thePath, pathLen);
435 verRegName[pathLen] = '\0';
436 #else
437 /* Since we're now using NSPR, this HAS to be a unix path! */
438 const char* src;
439 char* dst;
440 HLock(thePath);
441 verRegName = (char*)XP_ALLOC(pathLen + 2);
442 src = *(char**)thePath;
443 dst = verRegName;
444 *dst++ = '/';
445 while (pathLen--)
447 char c = *src++;
448 *dst++ = (c == ':') ? '/' : c;
450 *dst = '\0';
451 #endif
453 DisposeHandle(thePath);
457 return verRegName;
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).
464 #ifndef XP_MACOSX
465 extern int nr_RenameFile(char *from, char *to)
467 OSErr err = -1;
468 FSSpec fromSpec;
469 FSSpec toSpec;
470 FSSpec destDirSpec;
471 FSSpec beforeRenameSpec;
473 #ifdef STANDALONE_REGISTRY
474 errno = 0; /* reset errno (only if we're using stdio) */
475 #endif
477 if (from && to) {
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);
500 exit:
501 #ifdef STANDALONE_REGISTRY
502 if (err != noErr)
503 errno = err;
504 #endif
505 return (err == noErr ? 0 : -1);
507 #endif
510 #ifdef STANDALONE_REGISTRY
511 #ifndef XP_MACOSX
512 char *strdup(const char *source)
514 char *newAllocation;
515 size_t stringLength;
517 stringLength = strlen(source) + 1;
519 newAllocation = (char *)XP_ALLOC(stringLength);
520 if (newAllocation == NULL)
521 return NULL;
522 BlockMoveData(source, newAllocation, stringLength);
523 return newAllocation;
526 int strcasecmp(const char *str1, const char *str2)
528 char currentChar1, currentChar2;
530 while (1) {
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')
542 break;
544 if (currentChar1 != currentChar2)
545 return currentChar1 - currentChar2;
547 str1++;
548 str2++;
552 return currentChar1 - currentChar2;
555 int strncasecmp(const char *str1, const char *str2, int length)
557 char currentChar1, currentChar2;
559 while (length > 0) {
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')
571 break;
573 if (currentChar1 != currentChar2)
574 return currentChar1 - currentChar2;
576 str1++;
577 str2++;
579 length--;
582 return currentChar1 - currentChar2;
584 #endif /* XP_MACOSX */
585 #endif /* STANDALONE_REGISTRY */
587 #endif /* XP_MAC */
590 /* ------------------------------------------------------------------
591 * UNIX STUBS
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)
598 #include <stdlib.h>
600 #ifdef XP_OS2
601 #include <io.h>
602 #define W_OK 0x02 /*evil hack from the docs...*/
603 #else
604 #include <unistd.h>
605 #endif
607 #include "NSReg.h"
608 #include "VerReg.h"
610 char *TheRegistry = "registry";
611 char *Flist;
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)
620 XP_File fh = NULL;
621 struct stat st;
623 if ( name != NULL ) {
624 if ( stat( name, &st ) == 0 )
625 fh = fopen( name, XP_FILE_UPDATE_BIN );
626 else
627 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
630 return fh;
632 #endif /*STANDALONE_REGISTRY*/
634 extern void vr_findGlobalRegName ()
636 #ifndef STANDALONE_REGISTRY
637 char *def = NULL;
638 char *home = getenv("HOME");
639 if (home != NULL) {
640 def = (char *) XP_ALLOC(XP_STRLEN(home) + XP_STRLEN(DEF_REG)+1);
641 if (def != NULL) {
642 XP_STRCPY(def, home);
643 XP_STRCAT(def, DEF_REG);
646 if (def != NULL) {
647 globalRegName = XP_STRDUP(def);
648 } else {
649 globalRegName = XP_STRDUP(TheRegistry);
651 XP_FREEIF(def);
652 #else
653 globalRegName = XP_STRDUP(TheRegistry);
654 #endif /*STANDALONE_REGISTRY*/
657 char* vr_findVerRegName ()
659 if ( verRegName != NULL )
660 return verRegName;
662 #ifndef STANDALONE_REGISTRY
664 char *def = NULL;
665 char *home = getenv("HOME");
666 if (home != NULL) {
667 def = (char *) XP_ALLOC(XP_STRLEN(home) + XP_STRLEN(DEF_VERREG)+1);
668 if (def != NULL) {
669 XP_STRCPY(def, home);
670 XP_STRCAT(def, DEF_VERREG);
673 if (def != NULL) {
674 verRegName = XP_STRDUP(def);
676 XP_FREEIF(def);
678 #else
679 verRegName = XP_STRDUP(TheRegistry);
680 #endif /*STANDALONE_REGISTRY*/
682 return verRegName;
685 #endif /*XP_UNIX*/
687 /* ------------------------------------------------------------------
688 * BeOS STUBS
689 * ------------------------------------------------------------------
692 #ifdef XP_BEOS
694 #ifdef STANDALONE_REGISTRY
695 extern XP_File vr_fileOpen (const char *name, const char * mode)
697 XP_File fh = NULL;
698 struct stat st;
700 if ( name != NULL ) {
701 if ( stat( name, &st ) == 0 )
702 fh = fopen( name, XP_FILE_UPDATE_BIN );
703 else
704 fh = fopen( name, XP_FILE_TRUNCATE_BIN );
707 return fh;
709 #endif /*STANDALONE_REGISTRY*/
711 extern void vr_findGlobalRegName ()
713 #ifndef STANDALONE_REGISTRY
714 char *def = NULL;
715 char settings[1024];
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);
719 if (def != NULL) {
720 XP_STRCPY(def, settings);
721 XP_STRCAT(def, BEOS_REG);
724 if (def != NULL) {
725 globalRegName = XP_STRDUP(def);
726 } else {
727 globalRegName = XP_STRDUP(TheRegistry);
729 XP_FREEIF(def);
730 #else
731 globalRegName = XP_STRDUP(TheRegistry);
732 #endif /*STANDALONE_REGISTRY*/
735 char* vr_findVerRegName ()
737 if ( verRegName != NULL )
738 return verRegName;
740 #ifndef STANDALONE_REGISTRY
742 char *def = NULL;
743 char settings[1024];
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);
747 if (def != NULL) {
748 XP_STRCPY(def, settings);
749 XP_STRCAT(def, BEOS_VERREG);
752 if (def != NULL) {
753 verRegName = XP_STRDUP(def);
755 XP_FREEIF(def);
757 #else
758 verRegName = XP_STRDUP(TheRegistry);
759 #endif /*STANDALONE_REGISTRY*/
761 return verRegName;
764 #endif /*XP_BEOS*/
766 #endif /* XP_UNIX || XP_OS2 */