comparison loader/registry.c @ 3134:181db9e5a887

avifile sync again... :(
author arpi
date Mon, 26 Nov 2001 01:17:24 +0000
parents 392316004607
children 4c6f41e35d30
comparison
equal deleted inserted replaced
3133:07f2cd9ef85e 3134:181db9e5a887
16 #include <registry.h> 16 #include <registry.h>
17 17
18 //#undef TRACE 18 //#undef TRACE
19 //#define TRACE printf 19 //#define TRACE printf
20 20
21 // ...can be set before init_registry() call
22 char* regpathname = 0;
23
24
25 static char* localregpathname = 0;
26
27 typedef struct reg_handle_s
28 {
29 int handle;
30 char* name;
31 struct reg_handle_s* next;
32 struct reg_handle_s* prev;
33 } reg_handle_t;
34
21 struct reg_value 35 struct reg_value
22 { 36 {
23 int type; 37 int type;
24 char* name; 38 char* name;
25 int len; 39 int len;
26 char* value; 40 char* value;
27 }; 41 };
28 42
29 // ...can be set before init_registry() call 43 static struct reg_value* regs = NULL;
30 char* regpathname = 0; 44 static int reg_size;
31 45 static reg_handle_t* head = NULL;
32 static int reg_size=0;
33 static struct reg_value* regs = 0;
34
35 struct reg_handle_s;
36 typedef struct reg_handle_s
37 {
38 int handle;
39 char* name;
40 struct reg_handle_s* next;
41 struct reg_handle_s* prev;
42 } reg_handle_t;
43
44 static reg_handle_t* head=0;
45 46
46 #define DIR -25 47 #define DIR -25
47 48
48 static void create_registry(void); 49 static void create_registry(void);
49 static void open_registry(void); 50 static void open_registry(void);
50 static void save_registry(void); 51 static void save_registry(void);
51 static void init_registry(void); 52 static void init_registry(void);
52
53
54 53
55 54
56 static void create_registry(void){ 55 static void create_registry(void){
57 if(regs) 56 if(regs)
58 { 57 {
67 regs[1].name=(char*)malloc(5); 66 regs[1].name=(char*)malloc(5);
68 strcpy(regs[1].name, "HKCU"); 67 strcpy(regs[1].name, "HKCU");
69 regs[0].value=regs[1].value=NULL; 68 regs[0].value=regs[1].value=NULL;
70 regs[0].len=regs[1].len=0; 69 regs[0].len=regs[1].len=0;
71 reg_size=2; 70 reg_size=2;
71 head = 0;
72 save_registry(); 72 save_registry();
73 } 73 }
74
74 static void open_registry(void) 75 static void open_registry(void)
75 { 76 {
76 int fd; 77 int fd;
77 int i; 78 int i;
78 unsigned int len; 79 unsigned int len;
79 if(regs) 80 if(regs)
80 { 81 {
81 printf("Multiple open_registry(>\n"); 82 printf("Multiple open_registry(>\n");
82 return; 83 return;
83 } 84 }
84 fd = open(regpathname, O_RDONLY); 85 fd = open(localregpathname, O_RDONLY);
85 if (fd == -1) 86 if (fd == -1)
86 { 87 {
87 printf("Creating new registry\n"); 88 printf("Creating new registry\n");
88 create_registry(); 89 create_registry();
89 return; 90 return;
90 } 91 }
91 read(fd, &reg_size, 4); 92 read(fd, &reg_size, 4);
92 regs=(struct reg_value*)malloc(reg_size*sizeof(struct reg_value)); 93 regs=(struct reg_value*)malloc(reg_size*sizeof(struct reg_value));
94 head = 0;
93 for(i=0; i<reg_size; i++) 95 for(i=0; i<reg_size; i++)
94 { 96 {
95 read(fd,&regs[i].type,4); 97 read(fd,&regs[i].type,4);
96 read(fd,&len,4); 98 read(fd,&len,4);
97 regs[i].name=(char*)malloc(len+1); 99 regs[i].name=(char*)malloc(len+1);
104 regs[i].name[len]=0; 106 regs[i].name[len]=0;
105 read(fd,&regs[i].len,4); 107 read(fd,&regs[i].len,4);
106 regs[i].value=(char*)malloc(regs[i].len+1); 108 regs[i].value=(char*)malloc(regs[i].len+1);
107 if(regs[i].value==0) 109 if(regs[i].value==0)
108 { 110 {
109 free(regs[i].name); 111 free(regs[i].name);
110 reg_size=i+1; 112 reg_size=i+1;
111 goto error; 113 goto error;
112 } 114 }
113 read(fd, regs[i].value, regs[i].len); 115 read(fd, regs[i].value, regs[i].len);
114 regs[i].value[regs[i].len]=0; 116 regs[i].value[regs[i].len]=0;
121 static void save_registry(void) 123 static void save_registry(void)
122 { 124 {
123 int fd, i; 125 int fd, i;
124 if (!regs) 126 if (!regs)
125 init_registry(); 127 init_registry();
126 fd = open(regpathname, O_WRONLY | O_CREAT, 00666); 128 fd = open(localregpathname, O_WRONLY | O_CREAT, 00666);
127 if (fd == -1) 129 if (fd == -1)
128 { 130 {
129 printf("Failed to open registry file '%s' for writing.\n", 131 printf("Failed to open registry file '%s' for writing.\n",
130 regpathname); 132 localregpathname);
131 return; 133 return;
132 } 134 }
133 write(fd, &reg_size, 4); 135 write(fd, &reg_size, 4);
134 for(i=0; i<reg_size; i++) 136 for(i=0; i<reg_size; i++)
135 { 137 {
140 write(fd, &regs[i].len, 4); 142 write(fd, &regs[i].len, 4);
141 write(fd, regs[i].value, regs[i].len); 143 write(fd, regs[i].value, regs[i].len);
142 } 144 }
143 close(fd); 145 close(fd);
144 } 146 }
147
148 void free_registry(void)
149 {
150 reg_handle_t* t = head;
151 while (t)
152 {
153 reg_handle_t* f = t;
154 if (t->name)
155 free(t->name);
156 t=t->prev;
157 free(f);
158 }
159 head = 0;
160 if (regs)
161 {
162 int i;
163 for(i=0; i<reg_size; i++)
164 {
165 free(regs[i].name);
166 free(regs[i].value);
167 }
168 free(regs);
169 regs = 0;
170 }
171 if (localregpathname)
172 {
173 free(localregpathname);
174 localregpathname = 0;
175 }
176 }
177
178
145 static reg_handle_t* find_handle_by_name(const char* name) 179 static reg_handle_t* find_handle_by_name(const char* name)
146 { 180 {
147 reg_handle_t* t; 181 reg_handle_t* t;
148 for(t=head; t; t=t->prev) 182 for(t=head; t; t=t->prev)
149 { 183 {
235 //creating new value in registry 269 //creating new value in registry
236 { 270 {
237 if(regs==0) 271 if(regs==0)
238 create_registry(); 272 create_registry();
239 regs=(struct reg_value*)realloc(regs, sizeof(struct reg_value)*(reg_size+1)); 273 regs=(struct reg_value*)realloc(regs, sizeof(struct reg_value)*(reg_size+1));
274 //regs=(struct reg_value*)my_realloc(regs, sizeof(struct reg_value)*(reg_size+1));
240 v=regs+reg_size; 275 v=regs+reg_size;
241 reg_size++; 276 reg_size++;
242 } 277 }
243 else 278 else
244 //replacing old one 279 //replacing old one
245 { 280 {
246 free(v->value); 281 free(v->value);
247 free(v->name); 282 free(v->name);
248 } 283 }
249 v->type=type; 284 v->type=type;
250 v->len=len; 285 v->len=len;
251 v->value=(char*)malloc(len); 286 v->value=(char*)malloc(len);
252 memcpy(v->value, value, len); 287 memcpy(v->value, value, len);
253 v->name=(char*)malloc(strlen(fullname)+1); 288 v->name=(char*)malloc(strlen(fullname)+1);
254 strcpy(v->name, fullname); 289 strcpy(v->name, fullname);
290 free(fullname);
255 save_registry(); 291 save_registry();
256 return v; 292 return v;
257 } 293 }
258 294
259 static void init_registry(void) 295 static void init_registry(void)
260 { 296 {
261 struct passwd* pwent;
262 TRACE("Initializing registry\n"); 297 TRACE("Initializing registry\n");
263 pwent = getpwuid(geteuid());
264 // can't be free-ed - it's static and probably thread 298 // can't be free-ed - it's static and probably thread
265 // unsafe structure which is stored in glibc 299 // unsafe structure which is stored in glibc
266 300
267 #if 1 301 #ifdef USE_WIN32DLL
302 // MPlayer:
268 regpathname = get_path("registry"); 303 regpathname = get_path("registry");
269 #else 304 #else
270 if (regpathname == 0) 305 // avifile:
271 { 306 if (localregpathname == 0)
272 regpathname = (char*)malloc(strlen(pwent->pw_dir)+20); 307 {
273 strcpy(regpathname, pwent->pw_dir); 308 const char* pthn = regpathname;
274 strcat(regpathname, "/.registry"); 309 if (!regpathname)
310 {
311 struct passwd* pwent;
312 pwent = getpwuid(geteuid());
313 pthn = pwent->pw_dir;
314 }
315
316 localregpathname = (char*)malloc(strlen(pthn)+20);
317 strcpy(localregpathname, pthn);
318 strcat(localregpathname, "/.registry");
275 } 319 }
276 #endif 320 #endif
277 321
278 open_registry(); 322 open_registry();
279 insert_handle(HKEY_LOCAL_MACHINE, "HKLM"); 323 insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
350 head=head->prev; 394 head=head->prev;
351 free(handle); 395 free(handle);
352 return 1; 396 return 1;
353 } 397 }
354 398
355 extern void trapbug(void);
356 long RegQueryValueExA(long key, const char* value, int* reserved, int* type, int* data, int* count) 399 long RegQueryValueExA(long key, const char* value, int* reserved, int* type, int* data, int* count)
357 { 400 {
358 struct reg_value* t; 401 struct reg_value* t;
359 char* c; 402 char* c;
360 TRACE("Querying value %s\n", value); 403 TRACE("Querying value %s\n", value);
365 if (strcmp(value, "AudioReserved001")==0) 408 if (strcmp(value, "AudioReserved001")==0)
366 { 409 {
367 printf("Query for AudioReserved001 %p %p count: %d\n", type, data, *count); 410 printf("Query for AudioReserved001 %p %p count: %d\n", type, data, *count);
368 *(int*)type = REG_DWORD; 411 *(int*)type = REG_DWORD;
369 *(int*)data = 256; 412 *(int*)data = 256;
370 //trapbug();
371 return 0; 413 return 0;
372 } 414 }
373 if(c==NULL) 415 if(c==NULL)
374 return 1; 416 return 1;
375 if((t=find_value_by_name(c))==0) 417 t=find_value_by_name(c);
376 { 418 free(c);
377 free(c); 419 if(t==0)
378 return 2; 420 return 2;
379 }
380 free(c);
381 if(type) 421 if(type)
382 *type=t->type; 422 *type=t->type;
383 if(data) 423 if(data)
384 { 424 {
385 memcpy(data, t->value, (t->len<*count)?t->len:*count); 425 memcpy(data, t->value, (t->len<*count)?t->len:*count);