]> git.sesse.net Git - vlc/blob - src/config/dirs_macos.c
compilation fix
[vlc] / src / config / dirs_macos.c
1 /*****************************************************************************
2  * dirs_macos.c: MacOS directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * Copyright © 2007-2009 Rémi Denis-Courmont
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
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 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 <vlc_common.h>
29
30 #include "../libvlc.h"
31 #include <vlc_charset.h>
32 #include <vlc_configuration.h>
33
34 #include <unistd.h>
35 #include <pwd.h>
36 #include <assert.h>
37 #include <limits.h>
38
39 const char *config_GetDataDir( void )
40 {
41     static char path[PATH_MAX] = "";
42
43 #warning FIXME: thread-safety!
44     if( *path == '\0' )
45     {
46         snprintf( path, sizeof( path ), "%s" DIR_SEP "share", psz_vlcpath );
47         path[sizeof( path ) - 1] = '\0';
48     }
49     return path;
50 }
51
52 static const char *GetDir(void)
53 {
54     /* FIXME: a full memory page here - quite a waste... */
55     static char homedir[PATH_MAX] = "";
56
57     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
58     pthread_mutex_lock (&lock);
59
60     if (!*homedir)
61     {
62         const char *psz_localhome = getenv( "HOME" );
63 #if defined(HAVE_GETPWUID_R)
64         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
65         if (psz_localhome == NULL)
66         {
67             struct passwd pw, *res;
68
69             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
70                 psz_localhome = pw.pw_dir;
71         }
72 #endif
73         if (psz_localhome == NULL)
74             psz_localhome = getenv( "TMP" );
75         if (psz_localhome == NULL)
76             psz_localhome = "/tmp";
77
78         const char *uhomedir = FromLocale (psz_localhome);
79         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
80         homedir[sizeof (homedir) - 1] = '\0';
81         LocaleFree (uhomedir);
82     }
83     pthread_mutex_unlock (&lock);
84     return homedir;
85 }
86
87 const char *config_GetConfDir( void )
88 {
89     static char path[PATH_MAX] = "";
90
91 #warning FIXME: system config is not the same as shared app data...
92     if( *path == '\0' )
93     {
94         snprintf( path, sizeof( path ), "%s" DIR_SEP "share", /* FIXME: Duh? */
95                   psz_vlcpath );
96         path[sizeof( path ) - 1] = '\0';
97     }
98     return path;
99 }
100
101 static char *config_GetHomeDir (void)
102 {
103     /* 1/ Try $HOME  */
104     const char *home = getenv ("HOME");
105 #if defined(HAVE_GETPWUID_R)
106     /* 2/ Try /etc/passwd */
107     char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
108     if (home == NULL)
109     {
110         struct passwd pw, *res;
111
112         if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
113             home = pw.pw_dir;
114     }
115 #endif
116     /* 3/ Desperately try $TMP */
117     if (home == NULL)
118         home = getenv( "TMP" );
119     /* 4/ Beyond hope, hard-code /tmp */
120     if (home == NULL)
121         home = "/tmp";
122
123     return FromLocaleDup (home);
124 }
125
126 static char *config_GetAppDir (void)
127 {
128     char *psz_dir;
129     const char *psz_parent = GetDir ();
130
131     if( asprintf( &psz_dir, "%s/Library/Preferences/VLC", psz_parent ) == -1 )
132         psz_dir = NULL;
133     return psz_dir;
134 }
135
136 char *config_GetUserDir (vlc_userdir_t type)
137 {
138     switch (type)
139     {
140         case VLC_HOME_DIR:
141             return config_GetHomeDir ();
142         case VLC_CONFIG_DIR:
143         case VLC_DATA_DIR:
144         case VLC_CACHE_DIR:
145             return config_GetAppDir ();
146
147         case VLC_DESKTOP_DIR:
148         case VLC_DOWNLOAD_DIR:
149         case VLC_TEMPLATES_DIR:
150         case VLC_PUBLICSHARE_DIR:
151         case VLC_DOCUMENTS_DIR:
152         case VLC_MUSIC_DIR:
153         case VLC_PICTURES_DIR:
154         case VLC_VIDEOS_DIR:
155 #warning FIXME not implemented
156             return config_GetUserDir (VLC_HOME_DIR);;
157     }
158     assert (0);
159 }