~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/programs/reg/reg.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * Copyright 2008 Andrew Riedi
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2.1 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with this library; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 17  */
 18 
 19 #include <windows.h>
 20 #include <wine/unicode.h>
 21 #include "reg.h"
 22 
 23 static int reg_printfW(const WCHAR *msg, ...)
 24 {
 25     va_list va_args;
 26     int wlen;
 27     DWORD count, ret;
 28     WCHAR msg_buffer[8192];
 29 
 30     va_start(va_args, msg);
 31     vsprintfW(msg_buffer, msg, va_args);
 32     va_end(va_args);
 33 
 34     wlen = lstrlenW(msg_buffer);
 35     ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
 36     if (!ret)
 37     {
 38         DWORD len;
 39         char  *msgA;
 40 
 41         len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
 42             NULL, 0, NULL, NULL);
 43         msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
 44         if (!msgA)
 45             return 0;
 46 
 47         WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
 48             NULL, NULL);
 49         WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
 50         HeapFree(GetProcessHeap(), 0, msgA);
 51     }
 52 
 53     return count;
 54 }
 55 
 56 static int reg_message(int msg)
 57 {
 58     static const WCHAR formatW[] = {'%','s',0};
 59     WCHAR msg_buffer[8192];
 60 
 61     LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
 62         sizeof(msg_buffer)/sizeof(WCHAR));
 63     return reg_printfW(formatW, msg_buffer);
 64 }
 65 
 66 static HKEY get_rootkey(LPWSTR key)
 67 {
 68     static const WCHAR szHKLM[] = {'H','K','L','M',0};
 69     static const WCHAR szHKCU[] = {'H','K','C','U',0};
 70     static const WCHAR szHKCR[] = {'H','K','C','R',0};
 71     static const WCHAR szHKU[] = {'H','K','U',0};
 72     static const WCHAR szHKCC[] = {'H','K','C','C',0};
 73 
 74     if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKLM,4)==2)
 75         return HKEY_LOCAL_MACHINE;
 76     else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCU,4)==2)
 77         return HKEY_CURRENT_USER;
 78     else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCR,4)==2)
 79         return HKEY_CLASSES_ROOT;
 80     else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,3,szHKU,3)==2)
 81         return HKEY_USERS;
 82     else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCC,4)==2)
 83         return HKEY_CURRENT_CONFIG;
 84     else return NULL;
 85 }
 86 
 87 static DWORD get_regtype(LPWSTR type)
 88 {
 89     static const WCHAR szREG_SZ[] = {'R','E','G','_','S','Z',0};
 90     static const WCHAR szREG_MULTI_SZ[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
 91     static const WCHAR szREG_DWORD_BIG_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
 92     static const WCHAR szREG_DWORD[] = {'R','E','G','_','D','W','O','R','D',0};
 93     static const WCHAR szREG_BINARY[] = {'R','E','G','_','B','I','N','A','R','Y',0};
 94     static const WCHAR szREG_DWORD_LITTLE_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
 95     static const WCHAR szREG_NONE[] = {'R','E','G','_','N','O','N','E',0};
 96     static const WCHAR szREG_EXPAND_SZ[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
 97 
 98     if (!type)
 99         return REG_SZ;
100 
101     if (lstrcmpiW(type,szREG_SZ)==0) return REG_SZ;
102     if (lstrcmpiW(type,szREG_DWORD)==0) return REG_DWORD;
103     if (lstrcmpiW(type,szREG_MULTI_SZ)==0) return REG_MULTI_SZ;
104     if (lstrcmpiW(type,szREG_EXPAND_SZ)==0) return REG_EXPAND_SZ;
105     if (lstrcmpiW(type,szREG_DWORD_BIG_ENDIAN)==0) return REG_DWORD_BIG_ENDIAN;
106     if (lstrcmpiW(type,szREG_DWORD_LITTLE_ENDIAN)==0) return REG_DWORD_LITTLE_ENDIAN;
107     if (lstrcmpiW(type,szREG_BINARY)==0) return REG_BINARY;
108     if (lstrcmpiW(type,szREG_NONE)==0) return REG_NONE;
109 
110     return -1;
111 }
112 
113 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
114 {
115     LPBYTE out_data = NULL;
116     *reg_count = 0;
117 
118     switch (reg_type)
119     {
120         case REG_SZ:
121         {
122             *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
123             out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
124             lstrcpyW((LPWSTR)out_data,data);
125             break;
126         }
127         default:
128         {
129             static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0};
130             reg_printfW(unhandled, reg_type,data);
131         }
132     }
133 
134     return out_data;
135 }
136 
137 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
138     WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
139 {
140     static const WCHAR stubW[] = {'A','D','D',' ','-',' ','%','s',
141         ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
142     LPWSTR p;
143     HKEY root,subkey;
144 
145     reg_printfW(stubW, key_name, value_name, value_empty, type, data, force);
146 
147     if (key_name[0]=='\\' && key_name[1]=='\\')
148     {
149         reg_message(STRING_NO_REMOTE);
150         return 1;
151     }
152 
153     p = strchrW(key_name,'\\');
154     if (!p)
155     {
156         reg_message(STRING_INVALID_KEY);
157         return 1;
158     }
159     p++;
160 
161     root = get_rootkey(key_name);
162     if (!root)
163     {
164         reg_message(STRING_INVALID_KEY);
165         return 1;
166     }
167 
168     if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
169     {
170         reg_message(STRING_INVALID_KEY);
171         return 1;
172     }
173 
174     if (value_name || data)
175     {
176         DWORD reg_type;
177         DWORD reg_count = 0;
178         BYTE* reg_data = NULL;
179 
180         if (!force)
181         {
182             if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
183             {
184                 /* FIXME:  Prompt for overwrite */
185             }
186         }
187 
188         reg_type = get_regtype(type);
189         if (reg_type == -1)
190         {
191             RegCloseKey(subkey);
192             reg_message(STRING_INVALID_CMDLINE);
193             return 1;
194         }
195 
196         if (data)
197             reg_data = get_regdata(data,reg_type,separator,&reg_count);
198 
199         RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
200         HeapFree(GetProcessHeap(),0,reg_data);
201     }
202 
203     RegCloseKey(subkey);
204     reg_message(STRING_SUCCESS);
205 
206     return 0;
207 }
208 
209 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
210     BOOL value_all, BOOL force)
211 {
212     LPWSTR p;
213     HKEY root,subkey;
214 
215     static const WCHAR stubW[] = {'D','E','L','E','T','E',
216         ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
217         ,0};
218     reg_printfW(stubW, key_name, value_name, value_empty, value_all, force);
219 
220     if (key_name[0]=='\\' && key_name[1]=='\\')
221     {
222         reg_message(STRING_NO_REMOTE);
223         return 1;
224     }
225 
226     p = strchrW(key_name,'\\');
227     if (!p)
228     {
229         reg_message(STRING_INVALID_KEY);
230         return 1;
231     }
232     p++;
233 
234     root = get_rootkey(key_name);
235     if (!root)
236     {
237         reg_message(STRING_INVALID_KEY);
238         return 1;
239     }
240 
241     if (value_name && value_empty)
242     {
243         reg_message(STRING_INVALID_CMDLINE);
244         return 1;
245     }
246 
247     if (value_empty && value_all)
248     {
249         reg_message(STRING_INVALID_CMDLINE);
250         return 1;
251     }
252 
253     if (!force)
254     {
255         /* FIXME:  Prompt for delete */
256     }
257 
258     if (!value_name)
259     {
260         if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
261         {
262             reg_message(STRING_CANNOT_FIND);
263             return 1;
264         }
265         reg_message(STRING_SUCCESS);
266         return 0;
267     }
268 
269     if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
270     {
271         reg_message(STRING_CANNOT_FIND);
272         return 1;
273     }
274 
275     if (value_all)
276     {
277         LPWSTR szValue;
278         DWORD maxValue;
279         DWORD count;
280         LONG rc;
281 
282         rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
283             &maxValue, NULL, NULL, NULL);
284         if (rc != ERROR_SUCCESS)
285         {
286             /* FIXME: failure */
287             RegCloseKey(subkey);
288             return 1;
289         }
290         maxValue++;
291         szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
292 
293         while (1)
294         {
295             count = maxValue;
296             rc = RegEnumValueW(subkey, 0, value_name, &count, NULL, NULL, NULL, NULL);
297             if (rc == ERROR_SUCCESS)
298             {
299                 rc = RegDeleteValueW(subkey,value_name);
300                 if (rc != ERROR_SUCCESS)
301                     break;
302             }
303             else break;
304         }
305         if (rc != ERROR_SUCCESS)
306         {
307             /* FIXME  delete failed */
308         }
309     }
310     else if (value_name)
311     {
312         if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
313         {
314             RegCloseKey(subkey);
315             reg_message(STRING_CANNOT_FIND);
316             return 1;
317         }
318     }
319     else if (value_empty)
320     {
321         RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
322     }
323 
324     RegCloseKey(subkey);
325     reg_message(STRING_SUCCESS);
326     return 0;
327 }
328 
329 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
330     BOOL subkey)
331 {
332     static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
333         '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
334     reg_printfW(stubW, key_name, value_name, value_empty, subkey);
335 
336     return 1;
337 }
338 
339 int wmain(int argc, WCHAR *argvW[])
340 {
341     int i;
342 
343     static const WCHAR addW[] = {'a','d','d',0};
344     static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
345     static const WCHAR queryW[] = {'q','u','e','r','y',0};
346     static const WCHAR slashDW[] = {'/','d',0};
347     static const WCHAR slashFW[] = {'/','f',0};
348     static const WCHAR slashHW[] = {'/','h',0};
349     static const WCHAR slashSW[] = {'/','s',0};
350     static const WCHAR slashTW[] = {'/','t',0};
351     static const WCHAR slashVW[] = {'/','v',0};
352     static const WCHAR slashVAW[] = {'/','v','a',0};
353     static const WCHAR slashVEW[] = {'/','v','e',0};
354     static const WCHAR slashHelpW[] = {'/','?',0};
355 
356     if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
357                  || !lstrcmpiW(argvW[1], slashHW))
358     {
359         reg_message(STRING_USAGE);
360         return 0;
361     }
362 
363     if (!lstrcmpiW(argvW[1], addW))
364     {
365         WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
366         BOOL value_empty = FALSE, force = FALSE;
367 
368         if (argc < 3)
369         {
370             reg_message(STRING_INVALID_CMDLINE);
371             return 1;
372         }
373         else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
374                                !lstrcmpiW(argvW[2], slashHW)))
375         {
376             reg_message(STRING_ADD_USAGE);
377             return 0;
378         }
379 
380         key_name = argvW[2];
381         for (i = 1; i < argc; i++)
382         {
383             if (!lstrcmpiW(argvW[i], slashVW))
384                 value_name = argvW[++i];
385             else if (!lstrcmpiW(argvW[i], slashVEW))
386                 value_empty = TRUE;
387             else if (!lstrcmpiW(argvW[i], slashTW))
388                 type = argvW[++i];
389             else if (!lstrcmpiW(argvW[i], slashSW))
390                 separator = argvW[++i][0];
391             else if (!lstrcmpiW(argvW[i], slashDW))
392                 data = argvW[++i];
393             else if (!lstrcmpiW(argvW[i], slashFW))
394                 force = TRUE;
395         }
396         return reg_add(key_name, value_name, value_empty, type, separator,
397             data, force);
398     }
399     else if (!lstrcmpiW(argvW[1], deleteW))
400     {
401         WCHAR *key_name, *value_name = NULL;
402         BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
403 
404         if (argc < 3)
405         {
406             reg_message(STRING_INVALID_CMDLINE);
407             return 1;
408         }
409         else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
410                                !lstrcmpiW(argvW[2], slashHW)))
411         {
412             reg_message(STRING_DELETE_USAGE);
413             return 0;
414         }
415 
416         key_name = argvW[2];
417         for (i = 1; i < argc; i++)
418         {
419             if (!lstrcmpiW(argvW[i], slashVW))
420                 value_name = argvW[++i];
421             else if (!lstrcmpiW(argvW[i], slashVEW))
422                 value_empty = TRUE;
423             else if (!lstrcmpiW(argvW[i], slashVAW))
424                 value_all = TRUE;
425             else if (!lstrcmpiW(argvW[i], slashFW))
426                 force = TRUE;
427         }
428         return reg_delete(key_name, value_name, value_empty, value_all, force);
429     }
430     else if (!lstrcmpiW(argvW[1], queryW))
431     {
432         WCHAR *key_name, *value_name = NULL;
433         BOOL value_empty = FALSE, subkey = FALSE;
434 
435         if (argc < 3)
436         {
437             reg_message(STRING_INVALID_CMDLINE);
438             return 1;
439         }
440         else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
441                                !lstrcmpiW(argvW[2], slashHW)))
442         {
443             reg_message(STRING_QUERY_USAGE);
444             return 0;
445         }
446 
447         key_name = argvW[2];
448         for (i = 1; i < argc; i++)
449         {
450             if (!lstrcmpiW(argvW[i], slashVW))
451                 value_name = argvW[++i];
452             else if (!lstrcmpiW(argvW[i], slashVEW))
453                 value_empty = TRUE;
454             else if (!lstrcmpiW(argvW[i], slashSW))
455                 subkey = TRUE;
456         }
457         return reg_query(key_name, value_name, value_empty, subkey);
458     }
459     else
460     {
461         reg_message(STRING_INVALID_CMDLINE);
462         return 1;
463     }
464 }
465 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.