]> git.sesse.net Git - vlc/blob - src/config/file.c
config: if read-only, refuse to save the configuration
[vlc] / src / config / file.c
1 /*****************************************************************************
2  * file.c: configuration file handling
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <errno.h>                                                  /* errno */
29 #include <assert.h>
30 #include <limits.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #ifdef __APPLE__
34 #   include <xlocale.h>
35 #else
36 #include <locale.h>
37 #endif
38
39 #include <vlc_common.h>
40 #include "../libvlc.h"
41 #include "vlc_charset.h"
42 #include "vlc_keys.h"
43
44 #include "configuration.h"
45 #include "modules/modules.h"
46
47 static char *ConfigKeyToString( int );
48
49 static inline char *strdupnull (const char *src)
50 {
51     return src ? strdup (src) : NULL;
52 }
53
54 /**
55  * Get the user's configuration file
56  */
57 static char *config_GetConfigFile( vlc_object_t *obj )
58 {
59     char *psz_file = config_GetPsz( obj, "config" );
60     if( psz_file == NULL )
61     {
62         char *psz_dir = config_GetUserConfDir();
63
64         if( asprintf( &psz_file, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 )
65             psz_file = NULL;
66         free( psz_dir );
67     }
68     return psz_file;
69 }
70
71 static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
72 {
73     char *psz_filename = config_GetConfigFile( p_obj );
74     if( psz_filename == NULL )
75         return NULL;
76
77     msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
78
79     FILE *p_stream = utf8_fopen( psz_filename, "rt" );
80     if( p_stream == NULL && errno != ENOENT )
81     {
82         msg_Err( p_obj, "cannot open config file (%s): %m",
83                  psz_filename );
84
85     }
86 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
87     else if( p_stream == NULL && errno == ENOENT )
88     {
89         /* This is the fallback for pre XDG Base Directory
90          * Specification configs */
91         char *psz_old;
92         if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
93                       config_GetHomeDir() ) != -1 )
94         {
95             p_stream = utf8_fopen( psz_old, "rt" );
96             if( p_stream )
97             {
98                 /* Old config file found. We want to write it at the
99                  * new location now. */
100                 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
101                           "VLC will now use %s.", psz_old, psz_filename );
102                 char *psz_readme;
103                 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
104                               config_GetHomeDir() ) != -1 )
105                 {
106                     FILE *p_readme = utf8_fopen( psz_readme, "wt" );
107                     if( p_readme )
108                     {
109                         fprintf( p_readme, "The VLC media player "
110                                  "configuration folder has moved to comply\n"
111                                  "with the XDG Base Directory Specification "
112                                  "version 0.6. Your\nconfiguration has been "
113                                  "copied to the new location:\n%s\nYou can "
114                                  "delete this directory and all its contents.",
115                                   psz_filename);
116                         fclose( p_readme );
117                     }
118                     free( psz_readme );
119                 }
120             }
121             free( psz_old );
122         }
123     }
124 #endif
125     free( psz_filename );
126     return p_stream;
127 }
128
129
130 static int strtoi (const char *str)
131 {
132     char *end;
133     long l;
134
135     errno = 0;
136     l = strtol (str, &end, 0);
137
138     if (!errno)
139     {
140         if ((l > INT_MAX) || (l < INT_MIN))
141             errno = ERANGE;
142         if (*end)
143             errno = EINVAL;
144     }
145     return (int)l;
146 }
147
148
149 /*****************************************************************************
150  * config_LoadConfigFile: loads the configuration file.
151  *****************************************************************************
152  * This function is called to load the config options stored in the config
153  * file.
154  *****************************************************************************/
155 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
156 {
157     FILE *file;
158
159     file = config_OpenConfigFile (p_this);
160     if (file == NULL)
161         return VLC_EGENERIC;
162
163     /* Look for the selected module, if NULL then save everything */
164     module_t **list = module_list_get (NULL);
165
166     /* Look for UTF-8 Byte Order Mark */
167     char * (*convert) (const char *) = strdupnull;
168     char bom[3];
169
170     if ((fread (bom, 1, 3, file) != 3)
171      || memcmp (bom, "\xEF\xBB\xBF", 3))
172     {
173         convert = FromLocaleDup;
174         rewind (file); /* no BOM, rewind */
175     }
176
177     module_t *module = NULL;
178     char line[1024], section[1022];
179     section[0] = '\0';
180
181     /* Ensure consistent number formatting... */
182     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
183     locale_t baseloc = uselocale (loc);
184
185     while (fgets (line, 1024, file) != NULL)
186     {
187         /* Ignore comments and empty lines */
188         switch (line[0])
189         {
190             case '#':
191             case '\n':
192             case '\0':
193                 continue;
194         }
195
196         if (line[0] == '[')
197         {
198             char *ptr = strchr (line, ']');
199             if (ptr == NULL)
200                 continue; /* syntax error; */
201             *ptr = '\0';
202
203             /* New section ( = a given module) */
204             strcpy (section, line + 1);
205             module = NULL;
206
207             if ((psz_module_name == NULL)
208              || (strcmp (psz_module_name, section) == 0))
209             {
210                 for (int i = 0; list[i]; i++)
211                 {
212                     module_t *m = list[i];
213
214                     if ((strcmp (section, m->psz_object_name) == 0)
215                      && (m->i_config_items > 0)) /* ignore config-less modules */
216                     {
217                         module = m;
218                         if (psz_module_name != NULL)
219                             msg_Dbg (p_this,
220                                      "loading config for module \"%s\"",
221                                      section);
222                         break;
223                     }
224                 }
225             }
226
227             continue;
228         }
229
230         if (module == NULL)
231             continue; /* no need to parse if there is no matching module */
232
233         char *ptr = strchr (line, '\n');
234         if (ptr != NULL)
235             *ptr = '\0';
236
237         /* look for option name */
238         const char *psz_option_name = line;
239
240         ptr = strchr (line, '=');
241         if (ptr == NULL)
242             continue; /* syntax error */
243
244         *ptr = '\0';
245         const char *psz_option_value = ptr + 1;
246
247         /* try to match this option with one of the module's options */
248         for (size_t i = 0; i < module->confsize; i++)
249         {
250             module_config_t *p_item = module->p_config + i;
251
252             if ((p_item->i_type & CONFIG_HINT)
253              || strcmp (p_item->psz_name, psz_option_name))
254                 continue;
255
256             /* We found it */
257             errno = 0;
258
259             vlc_mutex_lock( p_item->p_lock );
260             switch( p_item->i_type )
261             {
262                 case CONFIG_ITEM_BOOL:
263                 case CONFIG_ITEM_INTEGER:
264                 {
265                     long l = strtoi (psz_option_value);
266                     if (errno)
267                         msg_Warn (p_this, "Integer value (%s) for %s: %m",
268                                   psz_option_value, psz_option_name);
269                     else
270                         p_item->saved.i = p_item->value.i = (int)l;
271                     break;
272                 }
273
274                 case CONFIG_ITEM_FLOAT:
275                     if( !*psz_option_value )
276                         break;                    /* ignore empty option */
277                     p_item->value.f = (float)atof (psz_option_value);
278                     p_item->saved.f = p_item->value.f;
279                     break;
280
281                 case CONFIG_ITEM_KEY:
282                     if( !*psz_option_value )
283                         break;                    /* ignore empty option */
284                     p_item->value.i = ConfigStringToKey(psz_option_value);
285                     p_item->saved.i = p_item->value.i;
286                     break;
287
288                 default:
289                     /* free old string */
290                     free( (char*) p_item->value.psz );
291                     free( (char*) p_item->saved.psz );
292
293                     p_item->value.psz = convert (psz_option_value);
294                     p_item->saved.psz = strdupnull (p_item->value.psz);
295                     break;
296             }
297             vlc_mutex_unlock( p_item->p_lock );
298             break;
299         }
300     }
301
302     if (ferror (file))
303     {
304         msg_Err (p_this, "error reading configuration: %m");
305         clearerr (file);
306     }
307     fclose (file);
308
309     module_list_free (list);
310     if (loc != (locale_t)0)
311     {
312         uselocale (baseloc);
313         freelocale (loc);
314     }
315     return 0;
316 }
317
318 /*****************************************************************************
319  * config_CreateDir: Create configuration directory if it doesn't exist.
320  *****************************************************************************/
321 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
322 {
323     if( !psz_dirname || !*psz_dirname ) return -1;
324
325     if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
326         return 0;
327
328     switch( errno )
329     {
330         case EEXIST:
331             return 0;
332
333         case ENOENT:
334         {
335             /* Let's try to create the parent directory */
336             char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
337             strcpy( psz_parent, psz_dirname );
338
339             psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
340             if( psz_end && psz_end != psz_parent )
341             {
342                 *psz_end = '\0';
343                 if( config_CreateDir( p_this, psz_parent ) == 0 )
344                 {
345                     if( !utf8_mkdir( psz_dirname, 0700 ) )
346                         return 0;
347                 }
348             }
349         }
350     }
351
352     msg_Err( p_this, "could not create %s: %m", psz_dirname );
353     return -1;
354 }
355
356 static int
357 config_Write (FILE *file, const char *type, const char *desc,
358               bool comment, const char *name, const char *fmt, ...)
359 {
360     va_list ap;
361     int ret;
362
363     if (desc == NULL)
364         desc = "?";
365
366     if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
367                  comment ? "#" : "", name) < 0)
368         return -1;
369
370     va_start (ap, fmt);
371     ret = vfprintf (file, fmt, ap);
372     va_end (ap);
373     if (ret < 0)
374         return -1;
375
376     if (fputs ("\n\n", file) == EOF)
377         return -1;
378     return 0;
379 }
380
381
382 static int config_PrepareDir (vlc_object_t *obj)
383 {
384     char *psz_configdir = config_GetUserConfDir ();
385     if (psz_configdir == NULL) /* XXX: This should never happen */
386         return -1;
387
388     int ret = config_CreateDir (obj, psz_configdir);
389     free (psz_configdir);
390     return ret;
391 }
392
393 /*****************************************************************************
394  * config_SaveConfigFile: Save a module's config options.
395  *****************************************************************************
396  * This will save the specified module's config options to the config file.
397  * If psz_module_name is NULL then we save all the modules config options.
398  * It's no use to save the config options that kept their default values, so
399  * we'll try to be a bit clever here.
400  *
401  * When we save we mustn't delete the config options of the modules that
402  * haven't been loaded. So we cannot just create a new config file with the
403  * config structures we've got in memory.
404  * I don't really know how to deal with this nicely, so I will use a completly
405  * dumb method ;-)
406  * I will load the config file in memory, but skipping all the sections of the
407  * modules we want to save. Then I will create a brand new file, dump the file
408  * loaded in memory and then append the sections of the modules we want to
409  * save.
410  * Really stupid no ?
411  *****************************************************************************/
412 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
413                            bool b_autosave )
414 {
415     module_t *p_parser;
416     FILE *file = NULL;
417     char *permanent = NULL, *temporary = NULL;
418     char p_line[1024], *p_index2;
419     unsigned long i_sizebuf = 0;
420     char *p_bigbuffer = NULL, *p_index;
421     bool b_backup;
422     int i_index;
423
424     if( config_PrepareDir( p_this ) )
425     {
426         msg_Err( p_this, "no configuration directory" );
427         goto error;
428     }
429
430     file = config_OpenConfigFile( p_this );
431     if( file != NULL )
432     {
433         struct stat st;
434
435         /* Some users make vlcrc read-only to prevent changes.
436          * The atomic replacement scheme breaks this "feature",
437          * so we check for read-only by hand. */
438         if (fstat (fileno (file), &st)
439          || !(st.st_mode & S_IWUSR))
440         {
441             msg_Err (p_this, "configuration file is read-only");
442             goto error;
443         }
444         i_sizebuf = ( st.st_size < LONG_MAX ) ? st.st_size : 0;
445     }
446
447     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
448     if( !p_bigbuffer )
449         goto error;
450     p_bigbuffer[0] = 0;
451
452     /* List all available modules */
453     module_t **list = module_list_get (NULL);
454
455     /* backup file into memory, we only need to backup the sections we won't
456      * save later on */
457     b_backup = false;
458     while( file && fgets( p_line, 1024, file ) )
459     {
460         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
461         {
462
463             /* we found a section, check if we need to do a backup */
464             for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
465             {
466                 if( ((p_index2 - &p_line[1])
467                        == (int)strlen(p_parser->psz_object_name) )
468                     && !memcmp( &p_line[1], p_parser->psz_object_name,
469                                 strlen(p_parser->psz_object_name) ) )
470                 {
471                     if( !psz_module_name )
472                         break;
473                     else if( !strcmp( psz_module_name,
474                                       p_parser->psz_object_name ) )
475                         break;
476                 }
477             }
478
479             if( list[i_index] == NULL )
480             {
481                 /* we don't have this section in our list so we need to back
482                  * it up */
483                 *p_index2 = 0;
484 #if 0
485                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
486                                  &p_line[1] );
487 #endif
488                 *p_index2 = ']';
489
490                 b_backup = true;
491             }
492             else
493             {
494                 b_backup = false;
495             }
496         }
497
498         /* save line if requested and line is valid (doesn't begin with a
499          * space, tab, or eol) */
500         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
501             && (p_line[0] != '\t') )
502         {
503             strcpy( p_index, p_line );
504             p_index += strlen( p_line );
505         }
506     }
507     if( file )
508         fclose( file );
509     file = NULL;
510
511     /*
512      * Save module config in file
513      */
514     permanent = config_GetConfigFile (p_this);
515     if (!permanent)
516     {
517         module_list_free (list);
518         goto error;
519     }
520
521     if (asprintf (&temporary, "%s.%u", permanent,
522 #ifdef UNDER_CE
523                   GetCurrentProcessId ()
524 #else
525                   getpid ()
526 #endif
527                  ) == -1)
528     {
529         temporary = NULL;
530         module_list_free (list);
531         goto error;
532     }
533
534     /* The temporary configuration file is per-PID. Therefore SaveConfigFile()
535      * should be serialized against itself within a given process. */
536     static vlc_mutex_t lock = VLC_STATIC_MUTEX;
537     vlc_mutex_lock (&lock);
538
539     int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
540     if (fd == -1)
541     {
542         vlc_mutex_unlock (&lock);
543         module_list_free (list);
544         goto error;
545     }
546     file = fdopen (fd, "wt");
547     if (file == NULL)
548     {
549         close (fd);
550         vlc_mutex_unlock (&lock);
551         module_list_free (list);
552         goto error;
553     }
554
555     fprintf( file, "\xEF\xBB\xBF###\n###  " COPYRIGHT_MESSAGE "\n###\n\n"
556        "###\n### lines beginning with a '#' character are comments\n###\n\n" );
557
558     /* Ensure consistent number formatting... */
559     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
560     locale_t baseloc = uselocale (loc);
561
562     /* Look for the selected module, if NULL then save everything */
563     for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
564     {
565         module_config_t *p_item, *p_end;
566
567         if( psz_module_name && strcmp( psz_module_name,
568                                        p_parser->psz_object_name ) )
569             continue;
570
571         if( !p_parser->i_config_items )
572             continue;
573
574         if( psz_module_name )
575             msg_Dbg( p_this, "saving config for module \"%s\"",
576                      p_parser->psz_object_name );
577
578         fprintf( file, "[%s]", p_parser->psz_object_name );
579         if( p_parser->psz_longname )
580             fprintf( file, " # %s\n\n", p_parser->psz_longname );
581         else
582             fprintf( file, "\n\n" );
583
584         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
585              p_item < p_end;
586              p_item++ )
587         {
588             if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
589              || p_item->b_removed              /* ignore deprecated option */
590              || p_item->b_unsaveable)          /* ignore volatile option */
591                 continue;
592
593             vlc_mutex_lock (p_item->p_lock);
594
595             /* Do not save the new value in the configuration file
596              * if doing an autosave, and the item is not an "autosaved" one. */
597             bool b_retain = b_autosave && !p_item->b_autosave;
598
599             if (IsConfigIntegerType (p_item->i_type))
600             {
601                 int val = b_retain ? p_item->saved.i : p_item->value.i;
602                 if (p_item->i_type == CONFIG_ITEM_KEY)
603                 {
604                     char *psz_key = ConfigKeyToString (val);
605                     config_Write (file, p_item->psz_text, N_("key"),
606                                   val == p_item->orig.i,
607                                   p_item->psz_name, "%s",
608                                   psz_key ? psz_key : "");
609                     free (psz_key);
610                 }
611                 else
612                     config_Write (file, p_item->psz_text,
613                                   (p_item->i_type == CONFIG_ITEM_BOOL)
614                                       ? N_("boolean") : N_("integer"),
615                                   val == p_item->orig.i,
616                                   p_item->psz_name, "%d", val);
617                 p_item->saved.i = val;
618             }
619             else
620             if (IsConfigFloatType (p_item->i_type))
621             {
622                 float val = b_retain ? p_item->saved.f : p_item->value.f;
623                 config_Write (file, p_item->psz_text, N_("float"),
624                               val == p_item->orig.f,
625                               p_item->psz_name, "%f", val);
626                 p_item->saved.f = val;
627             }
628             else
629             {
630                 const char *psz_value = b_retain ? p_item->saved.psz
631                                                  : p_item->value.psz;
632                 bool modified;
633
634                 assert (IsConfigStringType (p_item->i_type));
635
636                 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
637                     psz_value = p_item->orig.psz;
638
639                 modified =
640                     (psz_value != NULL)
641                         ? ((p_item->orig.psz != NULL)
642                             ? (strcmp (psz_value, p_item->orig.psz) != 0)
643                             : true)
644                         : (p_item->orig.psz != NULL);
645
646                 config_Write (file, p_item->psz_text, N_("string"),
647                               !modified, p_item->psz_name, "%s",
648                               psz_value ? psz_value : "");
649
650                 if ( !b_retain )
651                 {
652
653                     free ((char *)p_item->saved.psz);
654                     if( (psz_value && p_item->orig.psz &&
655                          strcmp( psz_value, p_item->orig.psz )) ||
656                         !psz_value || !p_item->orig.psz)
657                         p_item->saved.psz = strdupnull (psz_value);
658                     else
659                         p_item->saved.psz = NULL;
660                 }
661             }
662
663             if (!b_retain)
664                 p_item->b_dirty = false;
665             vlc_mutex_unlock (p_item->p_lock);
666         }
667     }
668
669     module_list_free (list);
670     if (loc != (locale_t)0)
671     {
672         uselocale (baseloc);
673         freelocale (loc);
674     }
675
676     /*
677      * Restore old settings from the config in file
678      */
679     fputs( p_bigbuffer, file );
680     free( p_bigbuffer );
681
682     /*
683      * Flush to disk and replace atomically
684      */
685     fflush (file); /* Flush from run-time */
686 #ifndef WIN32
687     fdatasync (fd); /* Flush from OS */
688     /* Atomically replace the file... */
689     if (utf8_rename (temporary, permanent))
690         utf8_unlink (temporary);
691     /* (...then synchronize the directory, err, TODO...) */
692     /* ...and finally close the file */
693     vlc_mutex_unlock (&lock);
694 #endif
695     fclose (file);
696 #ifdef WIN32
697     /* Windows cannot remove open files nor overwrite existing ones */
698     utf8_unlink (permanent);
699     if (utf8_rename (temporary, permanent))
700         utf8_unlink (temporary);
701     vlc_mutex_unlock (&lock);
702 #endif
703
704     free (temporary);
705     free (permanent);
706     return 0;
707
708 error:
709     if( file )
710         fclose( file );
711     free (temporary);
712     free (permanent);
713     free( p_bigbuffer );
714     return -1;
715 }
716
717 int config_AutoSaveConfigFile( vlc_object_t *p_this )
718 {
719     size_t i_index;
720     bool save = false;
721
722     assert( p_this );
723
724     /* Check if there's anything to save */
725     module_t **list = module_list_get (NULL);
726     for( i_index = 0; list[i_index] && !save; i_index++ )
727     {
728         module_t *p_parser = list[i_index];
729         module_config_t *p_item, *p_end;
730
731         if( !p_parser->i_config_items ) continue;
732
733         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
734              p_item < p_end && !save;
735              p_item++ )
736         {
737             vlc_mutex_lock (p_item->p_lock);
738             save = p_item->b_autosave && p_item->b_dirty;
739             vlc_mutex_unlock (p_item->p_lock);
740         }
741     }
742     module_list_free (list);
743
744     return save ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
745 }
746
747 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
748 {
749     return SaveConfigFile( p_this, psz_module_name, false );
750 }
751
752 int ConfigStringToKey( const char *psz_key )
753 {
754     int i_key = 0;
755     size_t i;
756     const char *psz_parser = strchr( psz_key, '-' );
757     while( psz_parser && psz_parser != psz_key )
758     {
759         for( i = 0; i < vlc_num_modifiers; ++i )
760         {
761             if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
762                               strlen( vlc_modifiers[i].psz_key_string ) ) )
763             {
764                 i_key |= vlc_modifiers[i].i_key_code;
765             }
766         }
767         psz_key = psz_parser + 1;
768         psz_parser = strchr( psz_key, '-' );
769     }
770     for( i = 0; i < vlc_num_keys; ++i )
771     {
772         if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
773         {
774             i_key |= vlc_keys[i].i_key_code;
775             break;
776         }
777     }
778     return i_key;
779 }
780
781 char *ConfigKeyToString( int i_key )
782 {
783     // Worst case appears to be 45 characters:
784     // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
785     enum { keylen=64 };
786     char *psz_key = malloc( keylen );
787     char *p;
788     size_t index;
789
790     if ( !psz_key )
791     {
792         return NULL;
793     }
794     *psz_key = '\0';
795     p = psz_key;
796
797     for( index = 0; index < vlc_num_modifiers; ++index )
798     {
799         if( i_key & vlc_modifiers[index].i_key_code )
800         {
801             p += snprintf( p, keylen-(psz_key-p), "%s-",
802                            vlc_modifiers[index].psz_key_string );
803         }
804     }
805     for( index = 0; index < vlc_num_keys; ++index )
806     {
807         if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
808         {
809             p += snprintf( p, keylen-(psz_key-p), "%s",
810                            vlc_keys[index].psz_key_string );
811             break;
812         }
813     }
814     return psz_key;
815 }
816