]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
20c695f2497a88f77fad3645ad5d4938ff1f087f
[vlc] / src / misc / configuration.c
1 /*****************************************************************************
2  * configuration.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: configuration.c,v 1.60 2003/07/23 01:13:48 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25
26 #include <stdio.h>                                              /* sprintf() */
27 #include <stdlib.h>                                      /* free(), strtol() */
28 #include <string.h>                                              /* strdup() */
29 #ifdef HAVE_ERRNO_H
30 #   include <errno.h>                                               /* errno */
31 #endif
32
33 #ifdef HAVE_UNISTD_H
34 #    include <unistd.h>                                          /* getuid() */
35 #endif
36
37 #ifdef HAVE_GETOPT_LONG
38 #   ifdef HAVE_GETOPT_H
39 #       include <getopt.h>                                       /* getopt() */
40 #   endif
41 #else
42 #   include "../extras/getopt.h"
43 #endif
44
45 #if defined(HAVE_GETPWUID)
46 #   include <pwd.h>                                            /* getpwuid() */
47 #endif
48
49 #if defined( HAVE_SYS_STAT_H )
50 #   include <sys/stat.h>
51 #endif
52 #if defined( HAVE_SYS_TYPES_H )
53 #   include <sys/types.h>
54 #endif
55 #if defined( WIN32 ) && !defined( UNDER_CE )
56 #   include <direct.h>
57 #endif
58
59 /*****************************************************************************
60  * config_GetType: get the type of a variable (bool, int, float, string)
61  *****************************************************************************
62  * This function is used to get the type of a variable from its name.
63  * Beware, this is quite slow.
64  *****************************************************************************/
65 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
66 {
67     module_config_t *p_config;
68     int i_type;
69
70     p_config = config_FindConfig( p_this, psz_name );
71
72     /* sanity checks */
73     if( !p_config )
74     {
75         return 0;
76     }
77
78     switch( p_config->i_type )
79     {
80     case CONFIG_ITEM_BOOL:
81         i_type = VLC_VAR_BOOL;
82         break;
83
84     case CONFIG_ITEM_INTEGER:
85         i_type = VLC_VAR_INTEGER;
86         break;
87
88     case CONFIG_ITEM_FLOAT:
89         i_type = VLC_VAR_FLOAT;
90         break;
91
92     case CONFIG_ITEM_MODULE:
93     case CONFIG_ITEM_STRING:
94         i_type = VLC_VAR_STRING;
95         break;
96
97     case CONFIG_ITEM_FILE:
98         i_type = VLC_VAR_FILE;
99         break;
100
101     case CONFIG_ITEM_DIRECTORY:
102         i_type = VLC_VAR_DIRECTORY;
103         break;
104
105     default:
106         i_type = 0;
107         break;
108     }
109
110     return i_type;
111 }
112
113 /*****************************************************************************
114  * config_GetInt: get the value of an int variable
115  *****************************************************************************
116  * This function is used to get the value of variables which are internally
117  * represented by an integer (CONFIG_ITEM_INTEGER and
118  * CONFIG_ITEM_BOOL).
119  *****************************************************************************/
120 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
121 {
122     module_config_t *p_config;
123
124     p_config = config_FindConfig( p_this, psz_name );
125
126     /* sanity checks */
127     if( !p_config )
128     {
129         msg_Err( p_this, "option %s does not exist", psz_name );
130         return -1;
131     }
132     if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
133         (p_config->i_type!=CONFIG_ITEM_BOOL) )
134     {
135         msg_Err( p_this, "option %s does not refer to an int", psz_name );
136         return -1;
137     }
138
139     return p_config->i_value;
140 }
141
142 /*****************************************************************************
143  * config_GetFloat: get the value of a float variable
144  *****************************************************************************
145  * This function is used to get the value of variables which are internally
146  * represented by a float (CONFIG_ITEM_FLOAT).
147  *****************************************************************************/
148 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
149 {
150     module_config_t *p_config;
151
152     p_config = config_FindConfig( p_this, psz_name );
153
154     /* sanity checks */
155     if( !p_config )
156     {
157         msg_Err( p_this, "option %s does not exist", psz_name );
158         return -1;
159     }
160     if( p_config->i_type != CONFIG_ITEM_FLOAT )
161     {
162         msg_Err( p_this, "option %s does not refer to a float", psz_name );
163         return -1;
164     }
165
166     return p_config->f_value;
167 }
168
169 /*****************************************************************************
170  * config_GetPsz: get the string value of a string variable
171  *****************************************************************************
172  * This function is used to get the value of variables which are internally
173  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
174  * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
175  *
176  * Important note: remember to free() the returned char* because it's a
177  *   duplicate of the actual value. It isn't safe to return a pointer to the
178  *   actual value as it can be modified at any time.
179  *****************************************************************************/
180 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
181 {
182     module_config_t *p_config;
183     char *psz_value = NULL;
184
185     p_config = config_FindConfig( p_this, psz_name );
186
187     /* sanity checks */
188     if( !p_config )
189     {
190         msg_Err( p_this, "option %s does not exist", psz_name );
191         return NULL;
192     }
193     if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
194         (p_config->i_type!=CONFIG_ITEM_FILE) &&
195         (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
196         (p_config->i_type!=CONFIG_ITEM_MODULE) )
197     {
198         msg_Err( p_this, "option %s does not refer to a string", psz_name );
199         return NULL;
200     }
201
202     /* return a copy of the string */
203     vlc_mutex_lock( p_config->p_lock );
204     if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
205     vlc_mutex_unlock( p_config->p_lock );
206
207     return psz_value;
208 }
209
210 /*****************************************************************************
211  * config_PutPsz: set the string value of a string variable
212  *****************************************************************************
213  * This function is used to set the value of variables which are internally
214  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
215  * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
216  *****************************************************************************/
217 void __config_PutPsz( vlc_object_t *p_this,
218                       const char *psz_name, const char *psz_value )
219 {
220     module_config_t *p_config;
221
222     p_config = config_FindConfig( p_this, psz_name );
223
224     /* sanity checks */
225     if( !p_config )
226     {
227         msg_Warn( p_this, "option %s does not exist", psz_name );
228         return;
229     }
230     if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
231         (p_config->i_type!=CONFIG_ITEM_FILE) &&
232         (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
233         (p_config->i_type!=CONFIG_ITEM_MODULE) )
234     {
235         msg_Err( p_this, "option %s does not refer to a string", psz_name );
236         return;
237     }
238
239     vlc_mutex_lock( p_config->p_lock );
240
241     /* free old string */
242     if( p_config->psz_value ) free( p_config->psz_value );
243
244     if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
245     else p_config->psz_value = NULL;
246
247     vlc_mutex_unlock( p_config->p_lock );
248
249     if( p_config->pf_callback )
250     {
251         p_config->pf_callback( p_this );
252     }
253 }
254
255 /*****************************************************************************
256  * config_PutInt: set the integer value of an int variable
257  *****************************************************************************
258  * This function is used to set the value of variables which are internally
259  * represented by an integer (CONFIG_ITEM_INTEGER and
260  * CONFIG_ITEM_BOOL).
261  *****************************************************************************/
262 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
263 {
264     module_config_t *p_config;
265
266     p_config = config_FindConfig( p_this, psz_name );
267
268     /* sanity checks */
269     if( !p_config )
270     {
271         msg_Warn( p_this, "option %s does not exist", psz_name );
272         return;
273     }
274     if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
275         (p_config->i_type!=CONFIG_ITEM_BOOL) )
276     {
277         msg_Err( p_this, "option %s does not refer to an int", psz_name );
278         return;
279     }
280
281     /* if i_min == i_max == 0, then do not use them */
282     if ((p_config->i_min == 0) && (p_config->i_max == 0))
283     {
284         p_config->i_value = i_value;
285     }
286     else if (i_value < p_config->i_min)
287     {
288         p_config->i_value = p_config->i_min;
289     }
290     else if (i_value > p_config->i_max)
291     {
292         p_config->i_value = p_config->i_max;
293     }
294     else
295     {
296         p_config->i_value = i_value;
297     }
298
299     if( p_config->pf_callback )
300     {
301         p_config->pf_callback( p_this );
302     }
303 }
304
305 /*****************************************************************************
306  * config_PutFloat: set the value of a float variable
307  *****************************************************************************
308  * This function is used to set the value of variables which are internally
309  * represented by a float (CONFIG_ITEM_FLOAT).
310  *****************************************************************************/
311 void __config_PutFloat( vlc_object_t *p_this,
312                         const char *psz_name, float f_value )
313 {
314     module_config_t *p_config;
315
316     p_config = config_FindConfig( p_this, psz_name );
317
318     /* sanity checks */
319     if( !p_config )
320     {
321         msg_Warn( p_this, "option %s does not exist", psz_name );
322         return;
323     }
324     if( p_config->i_type != CONFIG_ITEM_FLOAT )
325     {
326         msg_Err( p_this, "option %s does not refer to a float", psz_name );
327         return;
328     }
329
330     /* if f_min == f_max == 0, then do not use them */
331     if ((p_config->f_min == 0) && (p_config->f_max == 0))
332     {
333         p_config->f_value = f_value;
334     }
335     else if (f_value < p_config->f_min)
336     {
337         p_config->f_value = p_config->f_min;
338     }
339     else if (f_value > p_config->f_max)
340     {
341         p_config->f_value = p_config->f_max;
342     }
343     else
344     {
345         p_config->f_value = f_value;
346     }
347
348     if( p_config->pf_callback )
349     {
350         p_config->pf_callback( p_this );
351     }
352 }
353
354 /*****************************************************************************
355  * config_FindConfig: find the config structure associated with an option.
356  *****************************************************************************
357  * FIXME: This function really needs to be optimized.
358  * FIXME: And now even more.
359  *****************************************************************************/
360 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
361 {
362     vlc_list_t *p_list;
363     module_t *p_parser;
364     module_config_t *p_item;
365     int i_index;
366
367     if( !psz_name ) return NULL;
368
369     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
370
371     for( i_index = 0; i_index < p_list->i_count; i_index++ )
372     {
373         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
374
375         if( !p_parser->i_config_items )
376             continue;
377
378         for( p_item = p_parser->p_config;
379              p_item->i_type != CONFIG_HINT_END;
380              p_item++ )
381         {
382             if( p_item->i_type & CONFIG_HINT )
383                 /* ignore hints */
384                 continue;
385             if( !strcmp( psz_name, p_item->psz_name ) )
386             {
387                 vlc_list_release( p_list );
388                 return p_item;
389             }
390         }
391     }
392
393     vlc_list_release( p_list );
394
395     return NULL;
396 }
397
398 /*****************************************************************************
399  * config_Duplicate: creates a duplicate of a module's configuration data.
400  *****************************************************************************
401  * Unfortunatly we cannot work directly with the module's config data as
402  * this module might be unloaded from memory at any time (remember HideModule).
403  * This is why we need to create an exact copy of the config data.
404  *****************************************************************************/
405 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
406 {
407     int i, j, i_lines = 1;
408     module_config_t *p_item;
409
410     /* Calculate the structure length */
411     p_module->i_config_items = 0;
412     p_module->i_bool_items = 0;
413
414     for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
415     {
416         i_lines++;
417
418         if( p_item->i_type & CONFIG_ITEM )
419         {
420             p_module->i_config_items++;
421         }
422
423         if( p_item->i_type == CONFIG_ITEM_BOOL )
424         {
425             p_module->i_bool_items++;
426         }
427     }
428
429     /* Allocate memory */
430     p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
431                                                      * i_lines );
432     if( p_module->p_config == NULL )
433     {
434         msg_Err( p_module, "config error: can't duplicate p_config" );
435         return;
436     }
437
438     /* Do the duplication job */
439     for( i = 0; i < i_lines ; i++ )
440     {
441         p_module->p_config[i].i_type = p_orig[i].i_type;
442         p_module->p_config[i].i_short = p_orig[i].i_short;
443         p_module->p_config[i].i_value = p_orig[i].i_value;
444         p_module->p_config[i].i_value_orig = p_orig[i].i_value;
445         p_module->p_config[i].i_min = p_orig[i].i_min;
446         p_module->p_config[i].i_max = p_orig[i].i_max;
447         p_module->p_config[i].f_value = p_orig[i].f_value;
448         p_module->p_config[i].f_value_orig = p_orig[i].f_value;
449         p_module->p_config[i].f_min = p_orig[i].f_min;
450         p_module->p_config[i].f_max = p_orig[i].f_max;
451         p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
452         p_module->p_config[i].b_advanced = p_orig[i].b_advanced;
453
454         p_module->p_config[i].psz_type = p_orig[i].psz_type ?
455                                    strdup( p_orig[i].psz_type ) : NULL;
456         p_module->p_config[i].psz_name = p_orig[i].psz_name ?
457                                    strdup( p_orig[i].psz_name ) : NULL;
458         p_module->p_config[i].psz_text = p_orig[i].psz_text ?
459                                    strdup( _(p_orig[i].psz_text) ) : NULL;
460         p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
461                                    strdup( _(p_orig[i].psz_longtext) ) : NULL;
462         p_module->p_config[i].psz_value = p_orig[i].psz_value ?
463                                    strdup( p_orig[i].psz_value ) : NULL;
464         p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
465                                    strdup( p_orig[i].psz_value ) : NULL;
466
467         p_module->p_config[i].p_lock = &p_module->object_lock;
468
469         /* duplicate the string list */
470         p_module->p_config[i].ppsz_list = NULL;
471         if( p_orig[i].ppsz_list )
472         {
473             for( j = 0; p_orig[i].ppsz_list[j]; j++ );
474             p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
475             if( p_module->p_config[i].ppsz_list )
476             {
477                 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
478                     p_module->p_config[i].ppsz_list[j] =
479                         strdup( p_orig[i].ppsz_list[j] );
480             }
481             p_module->p_config[i].ppsz_list[j] = NULL;
482         }
483
484         p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
485     }
486 }
487
488 /*****************************************************************************
489  * config_Free: frees a duplicated module's configuration data.
490  *****************************************************************************
491  * This function frees all the data duplicated by config_Duplicate.
492  *****************************************************************************/
493 void config_Free( module_t *p_module )
494 {
495     module_config_t *p_item = p_module->p_config;
496     int i;
497
498     if( p_item == NULL )
499     {
500         return;
501     }
502
503     for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
504     {
505         if( p_item->psz_type )
506             free( p_item->psz_type );
507
508         if( p_item->psz_name )
509             free( p_item->psz_name );
510
511         if( p_item->psz_text )
512             free( p_item->psz_text );
513
514         if( p_item->psz_longtext )
515             free( p_item->psz_longtext );
516
517         if( p_item->psz_value )
518             free( p_item->psz_value );
519
520         if( p_item->psz_value_orig )
521             free( p_item->psz_value_orig );
522
523         if( p_item->ppsz_list )
524         {
525             for( i = 0; p_item->ppsz_list[i]; i++ )
526                 free(p_item->ppsz_list[i]);
527             free( p_item->ppsz_list );
528         }
529     }
530
531     free( p_module->p_config );
532     p_module->p_config = NULL;
533 }
534
535 /*****************************************************************************
536  * config_SetCallbacks: sets callback functions in the duplicate p_config.
537  *****************************************************************************
538  * Unfortunatly we cannot work directly with the module's config data as
539  * this module might be unloaded from memory at any time (remember HideModule).
540  * This is why we need to duplicate callbacks each time we reload the module.
541  *****************************************************************************/
542 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
543 {
544     while( p_new->i_type != CONFIG_HINT_END )
545     {
546         p_new->pf_callback = p_orig->pf_callback;
547         p_new++;
548         p_orig++;
549     }
550 }
551
552 /*****************************************************************************
553  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
554  *****************************************************************************
555  * We simply undo what we did in config_SetCallbacks.
556  *****************************************************************************/
557 void config_UnsetCallbacks( module_config_t *p_new )
558 {
559     while( p_new->i_type != CONFIG_HINT_END )
560     {
561         p_new->pf_callback = NULL;
562         p_new++;
563     }
564 }
565
566 /*****************************************************************************
567  * config_ResetAll: reset the configuration data for all the modules.
568  *****************************************************************************/
569 void __config_ResetAll( vlc_object_t *p_this )
570 {
571     int i_index, i;
572     vlc_list_t *p_list;
573     module_t *p_module;
574
575     /* Acquire config file lock */
576     vlc_mutex_lock( &p_this->p_vlc->config_lock );
577
578     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
579
580     for( i_index = 0; i_index < p_list->i_count; i_index++ )
581     {
582         p_module = (module_t *)p_list->p_values[i_index].p_object ;
583         if( p_module->b_submodule ) continue;
584
585         for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
586         {
587             p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
588             p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
589             if( p_module->p_config[i].psz_value )
590                 free( p_module->p_config[i].psz_value );
591             p_module->p_config[i].psz_value =
592                 p_module->p_config[i].psz_value_orig ?
593                 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
594         }
595     }
596
597     vlc_list_release( p_list );
598     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
599 }
600
601 /*****************************************************************************
602  * config_LoadConfigFile: loads the configuration file.
603  *****************************************************************************
604  * This function is called to load the config options stored in the config
605  * file.
606  *****************************************************************************/
607 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
608 {
609     vlc_list_t *p_list;
610     module_t *p_parser;
611     module_config_t *p_item;
612     FILE *file;
613     char line[1024];
614     char *p_index, *psz_option_name, *psz_option_value;
615     char *psz_filename, *psz_homedir;
616     int i_index;
617
618     psz_homedir = p_this->p_vlc->psz_homedir;
619     if( !psz_homedir )
620     {
621         msg_Err( p_this, "psz_homedir is null" );
622         return -1;
623     }
624     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
625                                    strlen(psz_homedir) + 1 );
626     if( !psz_filename )
627     {
628         msg_Err( p_this, "out of memory" );
629         return -1;
630     }
631     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
632
633     msg_Dbg( p_this, "opening config file %s", psz_filename );
634
635     /* Acquire config file lock */
636     vlc_mutex_lock( &p_this->p_vlc->config_lock );
637
638     file = fopen( psz_filename, "rt" );
639     if( !file )
640     {
641         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
642         free( psz_filename );
643         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
644         return -1;
645     }
646
647     /* Look for the selected module, if NULL then save everything */
648     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
649
650     for( i_index = 0; i_index < p_list->i_count; i_index++ )
651     {
652         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
653
654         if( psz_module_name
655              && strcmp( psz_module_name, p_parser->psz_object_name ) )
656         {
657             continue;
658         }
659
660         /* The config file is organized in sections, one per module. Look for
661          * the interesting section ( a section is of the form [foo] ) */
662         fseek( file, 0L, SEEK_SET );
663         while( fgets( line, 1024, file ) )
664         {
665             if( (line[0] == '[')
666                && (p_index = strchr(line,']'))
667                && (p_index - &line[1]
668                     == (int)strlen(p_parser->psz_object_name))
669                && !memcmp( &line[1], p_parser->psz_object_name,
670                            strlen(p_parser->psz_object_name) ) )
671             {
672 #if 0
673                 msg_Dbg( p_this, "loading config for module \"%s\"",
674                                  p_parser->psz_object_name );
675 #endif
676
677                 break;
678             }
679         }
680         /* either we found the section or we're at the EOF */
681
682         /* Now try to load the options in this section */
683         while( fgets( line, 1024, file ) )
684         {
685             if( line[0] == '[' ) break; /* end of section */
686
687             /* ignore comments or empty lines */
688             if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
689                 continue;
690
691             /* get rid of line feed */
692             if( line[strlen(line)-1] == '\n' )
693                 line[strlen(line)-1] = (char)0;
694
695             /* look for option name */
696             psz_option_name = line;
697             psz_option_value = NULL;
698             p_index = strchr( line, '=' );
699             if( !p_index ) break; /* this ain't an option!!! */
700
701             *p_index = (char)0;
702             psz_option_value = p_index + 1;
703
704             if( !p_parser->i_config_items )
705             {
706                 continue;
707             }
708
709             /* try to match this option with one of the module's options */
710             for( p_item = p_parser->p_config;
711                  p_item->i_type != CONFIG_HINT_END;
712                  p_item++ )
713             {
714                 if( p_item->i_type & CONFIG_HINT )
715                     /* ignore hints */
716                     continue;
717
718                 if( !strcmp( p_item->psz_name, psz_option_name ) )
719                 {
720                     /* We found it */
721                     switch( p_item->i_type )
722                     {
723                     case CONFIG_ITEM_BOOL:
724                     case CONFIG_ITEM_INTEGER:
725                         if( !*psz_option_value )
726                             break;                    /* ignore empty option */
727                         p_item->i_value = atoi( psz_option_value);
728 #if 0
729                         msg_Dbg( p_this, "option \"%s\", value %i",
730                                  p_item->psz_name, p_item->i_value );
731 #endif
732                         break;
733
734                     case CONFIG_ITEM_FLOAT:
735                         if( !*psz_option_value )
736                             break;                    /* ignore empty option */
737                         p_item->f_value = (float)atof( psz_option_value);
738 #if O
739                         msg_Dbg( p_this, "option \"%s\", value %f",
740                                  p_item->psz_name, (double)p_item->f_value );
741 #endif
742                         break;
743
744                     default:
745                         vlc_mutex_lock( p_item->p_lock );
746
747                         /* free old string */
748                         if( p_item->psz_value )
749                             free( p_item->psz_value );
750
751                         p_item->psz_value = *psz_option_value ?
752                             strdup( psz_option_value ) : NULL;
753
754                         vlc_mutex_unlock( p_item->p_lock );
755
756 #if 0
757                         msg_Dbg( p_this, "option \"%s\", value \"%s\"",
758                                  p_item->psz_name,
759                                  p_item->psz_value ? p_item->psz_value : "" );
760 #endif
761                         break;
762                     }
763                 }
764             }
765         }
766
767     }
768
769     vlc_list_release( p_list );
770
771     fclose( file );
772     free( psz_filename );
773
774     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
775
776     return 0;
777 }
778
779 /*****************************************************************************
780  * config_SaveConfigFile: Save a module's config options.
781  *****************************************************************************
782  * This will save the specified module's config options to the config file.
783  * If psz_module_name is NULL then we save all the modules config options.
784  * It's no use to save the config options that kept their default values, so
785  * we'll try to be a bit clever here.
786  *
787  * When we save we mustn't delete the config options of the modules that
788  * haven't been loaded. So we cannot just create a new config file with the
789  * config structures we've got in memory.
790  * I don't really know how to deal with this nicely, so I will use a completly
791  * dumb method ;-)
792  * I will load the config file in memory, but skipping all the sections of the
793  * modules we want to save. Then I will create a brand new file, dump the file
794  * loaded in memory and then append the sections of the modules we want to
795  * save.
796  * Really stupid no ?
797  *****************************************************************************/
798 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
799 {
800     module_t *p_parser;
801     vlc_list_t *p_list;
802     module_config_t *p_item;
803     FILE *file;
804     char p_line[1024], *p_index2;
805     int i_sizebuf = 0;
806     char *p_bigbuffer, *p_index;
807     vlc_bool_t b_backup;
808     char *psz_filename, *psz_homedir;
809     int i_index;
810
811     /* Acquire config file lock */
812     vlc_mutex_lock( &p_this->p_vlc->config_lock );
813
814     psz_homedir = p_this->p_vlc->psz_homedir;
815     if( !psz_homedir )
816     {
817         msg_Err( p_this, "psz_homedir is null" );
818         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
819         return -1;
820     }
821     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
822                                    strlen(psz_homedir) + 1 );
823     if( !psz_filename )
824     {
825         msg_Err( p_this, "out of memory" );
826         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
827         return -1;
828     }
829     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
830
831 #if defined( UNDER_CE )
832     {
833         wchar_t psz_new[ MAX_PATH ];
834         MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_new, MAX_PATH );
835         if( CreateDirectory( psz_new, NULL ) )
836         {
837             msg_Err( p_this, "could not create %s", psz_filename );
838         }
839     }
840
841 #elif defined( HAVE_ERRNO_H )
842 #   if defined( WIN32 )
843     if( mkdir( psz_filename ) && errno != EEXIST )
844 #   else
845     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
846 #   endif
847     {
848         msg_Err( p_this, "could not create %s (%s)",
849                          psz_filename, strerror(errno) );
850     }
851
852 #else
853     if( mkdir( psz_filename ) )
854     {
855         msg_Err( p_this, "could not create %s", psz_filename );
856     }
857
858 #endif
859
860     strcat( psz_filename, "/" CONFIG_FILE );
861
862
863     msg_Dbg( p_this, "opening config file %s", psz_filename );
864
865     file = fopen( psz_filename, "rt" );
866     if( !file )
867     {
868         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
869     }
870     else
871     {
872         /* look for file size */
873         fseek( file, 0L, SEEK_END );
874         i_sizebuf = ftell( file );
875         fseek( file, 0L, SEEK_SET );
876     }
877
878     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
879     if( !p_bigbuffer )
880     {
881         msg_Err( p_this, "out of memory" );
882         if( file ) fclose( file );
883         free( psz_filename );
884         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
885         return -1;
886     }
887     p_bigbuffer[0] = 0;
888
889     /* List all available modules */
890     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
891
892     /* backup file into memory, we only need to backup the sections we won't
893      * save later on */
894     b_backup = 0;
895     while( file && fgets( p_line, 1024, file ) )
896     {
897         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
898         {
899
900             /* we found a section, check if we need to do a backup */
901             for( i_index = 0; i_index < p_list->i_count; i_index++ )
902             {
903                 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
904
905                 if( ((p_index2 - &p_line[1])
906                        == (int)strlen(p_parser->psz_object_name) )
907                     && !memcmp( &p_line[1], p_parser->psz_object_name,
908                                 strlen(p_parser->psz_object_name) ) )
909                 {
910                     if( !psz_module_name )
911                         break;
912                     else if( !strcmp( psz_module_name,
913                                       p_parser->psz_object_name ) )
914                         break;
915                 }
916             }
917
918             if( i_index == p_list->i_count )
919             {
920                 /* we don't have this section in our list so we need to back
921                  * it up */
922                 *p_index2 = 0;
923                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
924                                  &p_line[1] );
925                 *p_index2 = ']';
926
927                 b_backup = 1;
928             }
929             else
930             {
931                 b_backup = 0;
932             }
933         }
934
935         /* save line if requested and line is valid (doesn't begin with a
936          * space, tab, or eol) */
937         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
938             && (p_line[0] != '\t') )
939         {
940             strcpy( p_index, p_line );
941             p_index += strlen( p_line );
942         }
943     }
944     if( file ) fclose( file );
945
946
947     /*
948      * Save module config in file
949      */
950
951     file = fopen( psz_filename, "wt" );
952     if( !file )
953     {
954         msg_Warn( p_this, "could not open config file %s for writing",
955                           psz_filename );
956         free( psz_filename );
957         vlc_list_release( p_list );
958         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
959         return -1;
960     }
961
962     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
963
964     /* Look for the selected module, if NULL then save everything */
965     for( i_index = 0; i_index < p_list->i_count; i_index++ )
966     {
967         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
968
969         if( psz_module_name && strcmp( psz_module_name,
970                                        p_parser->psz_object_name ) )
971             continue;
972
973         if( !p_parser->i_config_items )
974             continue;
975
976         msg_Dbg( p_this, "saving config for module \"%s\"",
977                          p_parser->psz_object_name );
978
979         fprintf( file, "[%s]", p_parser->psz_object_name );
980         if( p_parser->psz_longname )
981             fprintf( file, " # %s\n\n", p_parser->psz_longname );
982         else
983             fprintf( file, "\n\n" );
984
985         for( p_item = p_parser->p_config;
986              p_item->i_type != CONFIG_HINT_END;
987              p_item++ )
988         {
989             if( p_item->i_type & CONFIG_HINT )
990                 /* ignore hints */
991                 continue;
992
993             switch( p_item->i_type )
994             {
995             case CONFIG_ITEM_BOOL:
996             case CONFIG_ITEM_INTEGER:
997                 if( p_item->psz_text )
998                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
999                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
1000                              _("boolean") : _("integer") );
1001                 if( p_item->i_value == p_item->i_value_orig )
1002                     fprintf( file, "#" );
1003                 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
1004                 break;
1005
1006             case CONFIG_ITEM_FLOAT:
1007                 if( p_item->psz_text )
1008                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1009                              _("float") );
1010                 if( p_item->f_value == p_item->f_value_orig )
1011                     fprintf( file, "#" );
1012                 fprintf( file, "%s=%f\n", p_item->psz_name,
1013                          (double)p_item->f_value );
1014                 break;
1015
1016             default:
1017                 if( p_item->psz_text )
1018                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1019                              _("string") );
1020                 if( (!p_item->psz_value && !p_item->psz_value_orig) ||
1021                     (p_item->psz_value && p_item->psz_value_orig &&
1022                      !strcmp( p_item->psz_value, p_item->psz_value_orig )) )
1023                     fprintf( file, "#" );
1024                 fprintf( file, "%s=%s\n", p_item->psz_name,
1025                          p_item->psz_value ? p_item->psz_value : "" );
1026             }
1027         }
1028
1029         fprintf( file, "\n" );
1030     }
1031
1032     vlc_list_release( p_list );
1033
1034     /*
1035      * Restore old settings from the config in file
1036      */
1037     fputs( p_bigbuffer, file );
1038     free( p_bigbuffer );
1039
1040     fclose( file );
1041     free( psz_filename );
1042     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1043
1044     return 0;
1045 }
1046
1047 /*****************************************************************************
1048  * config_LoadCmdLine: parse command line
1049  *****************************************************************************
1050  * Parse command line for configuration options.
1051  * Now that the module_bank has been initialized, we can dynamically
1052  * generate the longopts structure used by getops. We have to do it this way
1053  * because we don't know (and don't want to know) in advance the configuration
1054  * options used (ie. exported) by each module.
1055  *****************************************************************************/
1056 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1057                           vlc_bool_t b_ignore_errors )
1058 {
1059     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1060     module_t *p_parser;
1061     vlc_list_t *p_list;
1062     module_config_t *p_item;
1063     struct option *p_longopts;
1064     int i_modules_index;
1065
1066     /* Short options */
1067     module_config_t *pp_shortopts[256];
1068     char *psz_shortopts;
1069
1070     /* Set default configuration and copy arguments */
1071     p_this->p_vlc->i_argc    = *pi_argc;
1072     p_this->p_vlc->ppsz_argv = ppsz_argv;
1073
1074     p_this->p_vlc->p_channel = NULL;
1075
1076 #ifdef SYS_DARWIN
1077     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1078      * is the PSN - process serial number (a unique PID-ish thingie)
1079      * still ok for real Darwin & when run from command line */
1080     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1081                                         /* for example -psn_0_9306113 */
1082     {
1083         /* GDMF!... I can't do this or else the MacOSX window server will
1084          * not pick up the PSN and not register the app and we crash...
1085          * hence the following kludge otherwise we'll get confused w/ argv[1]
1086          * being an input file name */
1087 #if 0
1088         ppsz_argv[ 1 ] = NULL;
1089 #endif
1090         *pi_argc = *pi_argc - 1;
1091         pi_argc--;
1092         return 0;
1093     }
1094 #endif
1095
1096     /* List all modules */
1097     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1098
1099     /*
1100      * Generate the longopts and shortopts structures used by getopt_long
1101      */
1102
1103     i_opts = 0;
1104     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1105          i_modules_index++ )
1106     {
1107         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1108
1109         /* count the number of exported configuration options (to allocate
1110          * longopts). We also need to allocate space for too options when
1111          * dealing with boolean to allow for --foo and --no-foo */
1112         i_opts += p_parser->i_config_items
1113                      + 2 * p_parser->i_bool_items;
1114     }
1115
1116     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1117     if( p_longopts == NULL )
1118     {
1119         msg_Err( p_this, "out of memory" );
1120         vlc_list_release( p_list );
1121         return -1;
1122     }
1123
1124     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1125     if( psz_shortopts == NULL )
1126     {
1127         msg_Err( p_this, "out of memory" );
1128         free( p_longopts );
1129         vlc_list_release( p_list );
1130         return -1;
1131     }
1132
1133     /* If we are requested to ignore errors, then we must work on a copy
1134      * of the ppsz_argv array, otherwise getopt_long will reorder it for
1135      * us, ignoring the arity of the options */
1136     if( b_ignore_errors )
1137     {
1138         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1139         if( ppsz_argv == NULL )
1140         {
1141             msg_Err( p_this, "out of memory" );
1142             free( psz_shortopts );
1143             free( p_longopts );
1144             vlc_list_release( p_list );
1145             return -1;
1146         }
1147         memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1148                 *pi_argc * sizeof(char *) );
1149     }
1150
1151     i_shortopts = 0;
1152     for( i_index = 0; i_index < 256; i_index++ )
1153     {
1154         pp_shortopts[i_index] = NULL;
1155     }
1156
1157     /* Fill the p_longopts and psz_shortopts structures */
1158     i_index = 0;
1159     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1160          i_modules_index++ )
1161     {
1162         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1163
1164         if( !p_parser->i_config_items )
1165             continue;
1166
1167         for( p_item = p_parser->p_config;
1168              p_item->i_type != CONFIG_HINT_END;
1169              p_item++ )
1170         {
1171             /* Ignore hints */
1172             if( p_item->i_type & CONFIG_HINT )
1173                 continue;
1174
1175             /* Add item to long options */
1176             p_longopts[i_index].name = strdup( p_item->psz_name );
1177             if( p_longopts[i_index].name == NULL ) continue;
1178             p_longopts[i_index].has_arg =
1179                 (p_item->i_type == CONFIG_ITEM_BOOL)?
1180                                                no_argument : required_argument;
1181             p_longopts[i_index].flag = &flag;
1182             p_longopts[i_index].val = 0;
1183             i_index++;
1184
1185             /* When dealing with bools we also need to add the --no-foo
1186              * option */
1187             if( p_item->i_type == CONFIG_ITEM_BOOL )
1188             {
1189                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1190                 if( psz_name == NULL ) continue;
1191                 strcpy( psz_name, "no" );
1192                 strcat( psz_name, p_item->psz_name );
1193
1194                 p_longopts[i_index].name = psz_name;
1195                 p_longopts[i_index].has_arg = no_argument;
1196                 p_longopts[i_index].flag = &flag;
1197                 p_longopts[i_index].val = 1;
1198                 i_index++;
1199
1200                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1201                 if( psz_name == NULL ) continue;
1202                 strcpy( psz_name, "no-" );
1203                 strcat( psz_name, p_item->psz_name );
1204
1205                 p_longopts[i_index].name = psz_name;
1206                 p_longopts[i_index].has_arg = no_argument;
1207                 p_longopts[i_index].flag = &flag;
1208                 p_longopts[i_index].val = 1;
1209                 i_index++;
1210             }
1211
1212             /* If item also has a short option, add it */
1213             if( p_item->i_short )
1214             {
1215                 pp_shortopts[(int)p_item->i_short] = p_item;
1216                 psz_shortopts[i_shortopts] = p_item->i_short;
1217                 i_shortopts++;
1218                 if( p_item->i_type != CONFIG_ITEM_BOOL )
1219                 {
1220                     psz_shortopts[i_shortopts] = ':';
1221                     i_shortopts++;
1222
1223                     if( p_item->i_short == 'v' )
1224                     {
1225                         psz_shortopts[i_shortopts] = ':';
1226                         i_shortopts++;
1227                     }
1228                 }
1229             }
1230         }
1231     }
1232
1233     /* We don't need the module list anymore */
1234     vlc_list_release( p_list );
1235
1236     /* Close the longopts and shortopts structures */
1237     memset( &p_longopts[i_index], 0, sizeof(struct option) );
1238     psz_shortopts[i_shortopts] = '\0';
1239
1240     /*
1241      * Parse the command line options
1242      */
1243     opterr = 0;
1244     optind = 1;
1245     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1246                                   p_longopts, &i_index ) ) != EOF )
1247     {
1248         /* A long option has been recognized */
1249         if( i_cmd == 0 )
1250         {
1251             module_config_t *p_conf;
1252             char *psz_name = (char *)p_longopts[i_index].name;
1253
1254             /* Check if we deal with a --nofoo or --no-foo long option */
1255             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1256
1257             /* Store the configuration option */
1258             p_conf = config_FindConfig( p_this, psz_name );
1259
1260             if( p_conf ) switch( p_conf->i_type )
1261             {
1262             case CONFIG_ITEM_STRING:
1263             case CONFIG_ITEM_FILE:
1264             case CONFIG_ITEM_DIRECTORY:
1265             case CONFIG_ITEM_MODULE:
1266                 config_PutPsz( p_this, psz_name, optarg );
1267                 break;
1268             case CONFIG_ITEM_INTEGER:
1269                 config_PutInt( p_this, psz_name, atoi(optarg));
1270                 break;
1271             case CONFIG_ITEM_FLOAT:
1272                 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1273                 break;
1274             case CONFIG_ITEM_BOOL:
1275                 config_PutInt( p_this, psz_name, !flag );
1276                 break;
1277             }
1278
1279             continue;
1280         }
1281
1282         /* A short option has been recognized */
1283         if( pp_shortopts[i_cmd] != NULL )
1284         {
1285             switch( pp_shortopts[i_cmd]->i_type )
1286             {
1287             case CONFIG_ITEM_STRING:
1288             case CONFIG_ITEM_FILE:
1289             case CONFIG_ITEM_DIRECTORY:
1290             case CONFIG_ITEM_MODULE:
1291                 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1292                 break;
1293             case CONFIG_ITEM_INTEGER:
1294                 if( i_cmd == 'v' )
1295                 {
1296                     if( optarg )
1297                     {
1298                         if( *optarg == 'v' ) /* eg. -vvv */
1299                         {
1300                             i_verbose++;
1301                             while( *optarg == 'v' )
1302                             {
1303                                 i_verbose++;
1304                                 optarg++;
1305                             }
1306                         }
1307                         else
1308                         {
1309                             i_verbose += atoi( optarg ); /* eg. -v2 */
1310                         }
1311                     }
1312                     else
1313                     {
1314                         i_verbose++; /* -v */
1315                     }
1316                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1317                                            i_verbose );
1318                 }
1319                 else
1320                 {
1321                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1322                                            atoi(optarg) );
1323                 }
1324                 break;
1325             case CONFIG_ITEM_BOOL:
1326                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1327                 break;
1328             }
1329
1330             continue;
1331         }
1332
1333         /* Internal error: unknown option */
1334         if( !b_ignore_errors )
1335         {
1336             fprintf( stderr, "%s: unknown option ",
1337                              p_this->p_vlc->psz_object_name );
1338             if( optopt )
1339             {
1340                 fprintf( stderr, "`-%c'\n", optopt );
1341             }
1342             else
1343             {
1344                 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1345             }
1346             fprintf( stderr, "Try `%s --help' for more information.\n",
1347                              p_this->p_vlc->psz_object_name );
1348
1349             free( p_longopts );
1350             free( psz_shortopts );
1351             return -1;
1352         }
1353     }
1354
1355     /* Free allocated resources */
1356     for( i_index = 0; p_longopts[i_index].name; i_index++ )
1357         free( (char *)p_longopts[i_index].name );
1358     free( p_longopts );
1359     free( psz_shortopts );
1360     if( b_ignore_errors ) free( ppsz_argv );
1361
1362     return 0;
1363 }
1364
1365 /*****************************************************************************
1366  * config_GetHomeDir: find the user's home directory.
1367  *****************************************************************************
1368  * This function will try by different ways to find the user's home path.
1369  * Note that this function is not reentrant, it should be called only once
1370  * at the beginning of main where the result will be stored for later use.
1371  *****************************************************************************/
1372 char *config_GetHomeDir( void )
1373 {
1374     char *p_tmp, *p_homedir = NULL;
1375
1376 #if defined(HAVE_GETPWUID)
1377     struct passwd *p_pw = NULL;
1378 #endif
1379
1380 #if defined(WIN32) || defined(UNDER_CE)
1381     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1382                                                LPTSTR );
1383 #   define CSIDL_FLAG_CREATE 0x8000
1384 #   define CSIDL_APPDATA 0x1A
1385 #   define SHGFP_TYPE_CURRENT 0
1386
1387     HINSTANCE shfolder_dll;
1388     SHGETFOLDERPATH SHGetFolderPath ;
1389
1390     /* load the shfolder dll to retrieve SHGetFolderPath */
1391     if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1392     {
1393         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1394                                                   "SHGetFolderPathA" );
1395         if ( SHGetFolderPath != NULL )
1396         {
1397             p_homedir = (char *)malloc( MAX_PATH );
1398             if( !p_homedir )
1399             {
1400                 return NULL;
1401             }
1402
1403             /* get the "Application Data" folder for the current user */
1404             if( S_OK == SHGetFolderPath( NULL,
1405                                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1406                                          NULL, SHGFP_TYPE_CURRENT,
1407                                          p_homedir ) )
1408             {
1409                 FreeLibrary( shfolder_dll );
1410                 return p_homedir;
1411             }
1412             free( p_homedir );
1413         }
1414         FreeLibrary( shfolder_dll );
1415     }
1416 #endif
1417
1418 #if defined(HAVE_GETPWUID)
1419     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1420 #endif
1421     {
1422         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1423         {
1424             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1425             {
1426                 p_tmp = "/tmp";
1427             }
1428         }
1429
1430         p_homedir = strdup( p_tmp );
1431     }
1432 #if defined(HAVE_GETPWUID)
1433     else
1434     {
1435         p_homedir = strdup( p_pw->pw_dir );
1436     }
1437 #endif
1438
1439     return p_homedir;
1440 }