[MKHIVE] Fixes for the previous fixes.
[reactos.git] / sdk / tools / mkhive / mkhive.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003, 2006 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS hive maker
22 * FILE: tools/mkhive/mkhive.c
23 * PURPOSE: Hive maker
24 * PROGRAMMERS: Eric Kohl
25 * Hervé Poussineau
26 * Hermès Bélusca-Maïto
27 */
28
29 #include <limits.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "mkhive.h"
34
35 #ifdef _MSC_VER
36 #include <stdlib.h>
37 #define PATH_MAX _MAX_PATH
38 #endif // _MSC_VER
39
40 #ifndef _WIN32
41 #ifndef PATH_MAX
42 #define PATH_MAX 260
43 #endif
44 #define DIR_SEPARATOR_CHAR '/'
45 #define DIR_SEPARATOR_STRING "/"
46 #else
47 #define DIR_SEPARATOR_CHAR '\\'
48 #define DIR_SEPARATOR_STRING "\\"
49 #endif
50
51
52 void usage(void)
53 {
54 printf("Usage: mkhive [-?] -h:hive1[,hiveN...] [-u] -d:<dstdir> <inffiles>\n\n"
55 " -h:hiveN - Comma-separated list of hives to create. Possible values are:\n"
56 " SETUPREG, SYSTEM, SOFTWARE, DEFAULT, SAM, SECURITY, BCD.\n"
57 " -u - Generate file names in uppercase (default: lowercase) (TEMPORARY FLAG!).\n"
58 " -d:dstdir - The binary hive files are created in this directory.\n"
59 " inffiles - List of INF files with full path.\n"
60 " -? - Displays this help screen.\n");
61 }
62
63 void convert_path(char *dst, char *src)
64 {
65 int i;
66
67 i = 0;
68 while (src[i] != 0)
69 {
70 #ifdef _WIN32
71 if (src[i] == '/')
72 {
73 dst[i] = '\\';
74 }
75 #else
76 if (src[i] == '\\')
77 {
78 dst[i] = '/';
79 }
80 #endif
81 else
82 {
83 dst[i] = src[i];
84 }
85
86 i++;
87 }
88 dst[i] = 0;
89 }
90
91 int main(int argc, char *argv[])
92 {
93 INT ret;
94 UINT i;
95 PSTR ptr;
96 BOOL UpperCaseFileName = FALSE;
97 PCSTR HiveList = NULL;
98 CHAR DestPath[PATH_MAX] = "";
99 CHAR FileName[PATH_MAX];
100
101 if (argc < 4)
102 {
103 usage();
104 return -1;
105 }
106
107 printf("Binary hive maker\n");
108
109 /* Read the options */
110 for (i = 1; i < argc && *argv[i] == '-'; i++)
111 {
112 if (argv[i][1] == '?' && argv[i][2] == 0)
113 {
114 usage();
115 return 0;
116 }
117
118 if (argv[i][1] == 'u' && argv[i][2] == 0)
119 {
120 UpperCaseFileName = TRUE;
121 }
122 else
123 if (argv[i][1] == 'h' && (argv[i][2] == ':' || argv[i][2] == '='))
124 {
125 HiveList = argv[i] + 3;
126 }
127 else if (argv[i][1] == 'd' && (argv[i][2] == ':' || argv[i][2] == '='))
128 {
129 convert_path(DestPath, argv[i] + 3);
130 }
131 else
132 {
133 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
134 return -1;
135 }
136 }
137
138 /* Check whether we have all the parameters needed */
139 if (!HiveList || !*HiveList)
140 {
141 fprintf(stderr, "The mandatory list of hives is missing.\n");
142 return -1;
143 }
144 if (!*DestPath)
145 {
146 fprintf(stderr, "The mandatory output directory is missing.\n");
147 return -1;
148 }
149 if (i >= argc)
150 {
151 fprintf(stderr, "Not enough parameters, or the list of INF files is missing.\n");
152 return -1;
153 }
154
155 /* Initialize the registry */
156 RegInitializeRegistry(HiveList);
157
158 /* Default to failure */
159 ret = -1;
160
161 /* Now we should have the list of INF files: parse it */
162 for (; i < argc; ++i)
163 {
164 convert_path(FileName, argv[i]);
165 if (!ImportRegistryFile(FileName))
166 goto Quit;
167 }
168
169 for (i = 0; i < MAX_NUMBER_OF_REGISTRY_HIVES; ++i)
170 {
171 /* Skip this registry hive if it's not in the list */
172 if (!strstr(HiveList, RegistryHives[i].HiveName))
173 continue;
174
175 strcpy(FileName, DestPath);
176 strcat(FileName, DIR_SEPARATOR_STRING);
177
178 ptr = FileName + strlen(FileName);
179
180 strcat(FileName, RegistryHives[i].HiveName);
181
182 /* Exception for the special setup registry hive */
183 // if (strcmp(RegistryHives[i].HiveName, "SETUPREG") == 0)
184 if (i == 0)
185 strcat(FileName, ".HIV");
186
187 /* Adjust file name case if needed */
188 if (UpperCaseFileName)
189 {
190 for (; *ptr; ++ptr)
191 *ptr = toupper(*ptr);
192 }
193 else
194 {
195 for (; *ptr; ++ptr)
196 *ptr = tolower(*ptr);
197 }
198
199 if (!ExportBinaryHive(FileName, RegistryHives[i].CmHive))
200 goto Quit;
201
202 /* If we happen to deal with the special setup registry hive, stop there */
203 // if (strcmp(RegistryHives[i].HiveName, "SETUPREG") == 0)
204 if (i == 0)
205 break;
206 }
207
208 /* Success */
209 ret = 0;
210
211 Quit:
212 /* Shut down the registry */
213 RegShutdownRegistry();
214
215 if (ret == 0)
216 printf(" Done.\n");
217
218 return ret;
219 }
220
221 /* EOF */