demux: libmp4: fix reading last iinf entry v0
[vlc.git] / src / darwin / dirs.c
blob6c4c538f6e06c30fd763ca40ade268967c75610e
1 /*****************************************************************************
2 * darwin_dirs.c: Darwin directories configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
5 * Copyright (C) 2007-2012 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * Pierre d'Herbemont <pdherbemont # videolan org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_charset.h>
32 #include "../libvlc.h"
34 #include <libgen.h>
35 #include <dlfcn.h>
36 #include <mach-o/dyld.h>
38 #include <CoreFoundation/CoreFoundation.h>
40 char *config_GetLibDir (void)
42 /* Get the full program path and name */
43 /* First try to see if we are linked to the framework */
44 for (unsigned i = 0; i < _dyld_image_count(); i++)
46 const char *psz_img_name = _dyld_get_image_name(i);
47 const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
49 /* Check for "VLCKit.framework/Versions/Current/VLCKit",
50 * as well as "VLCKit.framework/Versions/A/VLCKit" and
51 * "VLC.framework/Versions/B/VLCKit" */
52 if (p != NULL) {
53 /* Look for the next forward slash */
54 p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
55 p += strcspn( p, "/" );
57 /* If the string ends with VLCKit then we've found a winner */
58 if (!strcmp( p, "/VLCKit"))
59 return strdup( dirname(psz_img_name) );
62 /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
63 * link to VLCKit. */
64 size_t len = strlen(psz_img_name);
65 if (len >= 3 && !strcmp( psz_img_name + len - 3, "VLC"))
66 return strdup( dirname(psz_img_name) );
68 /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
69 if (len >= 10 && !strcmp( psz_img_name + len - 10, "VLC-Plugin"))
70 return strdup( dirname(psz_img_name) );
72 /* Do we end by "VLC for iOS"? so we are the iOS app */
73 if (len >= 11 && !strcmp( psz_img_name + len - 11, "VLC for iOS"))
74 return strdup( dirname(psz_img_name) );
76 /* Do we end by "VLC-TV"? so we are the tvOS app */
77 if (len >= 6 && !strcmp( psz_img_name + len - 6, "VLC-TV"))
78 return strdup( dirname(psz_img_name) );
81 /* we are not part of any Mac-style package but were installed
82 * the UNIX way. let's trick-around a bit */
83 Dl_info info;
84 if (dladdr(system_Init, &info)) {
85 char *incompletepath = strdup(dirname( (char *)info.dli_fname ));
86 char *path = NULL;
87 asprintf(&path, "%s/"PACKAGE, incompletepath);
88 free(incompletepath);
89 return path;
92 /* should never happen */
93 abort ();
96 static char *config_GetDataDir(void)
98 const char *path = getenv ("VLC_DATA_PATH");
99 if (path)
100 return strdup (path);
102 char *vlcpath = config_GetLibDir ();
103 char *datadir;
105 if (asprintf (&datadir, "%s/share", vlcpath) == -1)
106 datadir = NULL;
108 free (vlcpath);
109 return datadir;
112 char *config_GetSysPath(vlc_sysdir_t type, const char *filename)
114 char *dir = NULL;
116 switch (type)
118 case VLC_PKG_DATA_DIR:
119 dir = config_GetDataDir();
120 break;
121 case VLC_PKG_LIB_DIR:
122 case VLC_PKG_LIBEXEC_DIR:
123 dir = config_GetLibDir();
124 break;
125 case VLC_SYSDATA_DIR:
126 break;
127 case VLC_LOCALE_DIR:
128 dir = config_GetSysPath(VLC_PKG_DATA_DIR, "locale");
129 break;
130 default:
131 vlc_assert_unreachable();
134 if (filename == NULL || unlikely(dir == NULL))
135 return dir;
137 char *path;
138 asprintf(&path, "%s/%s", dir, filename);
139 free(dir);
140 return path;
143 static char *config_GetHomeDir (void)
145 const char *home = getenv ("HOME");
147 if (home == NULL)
148 home = "/tmp";
150 return strdup (home);
153 static char *getAppDependentDir(vlc_userdir_t type)
155 const char *psz_path;
156 switch (type) {
157 case VLC_CONFIG_DIR:
158 psz_path = "%s/Library/Preferences/%s";
159 break;
160 case VLC_TEMPLATES_DIR:
161 case VLC_USERDATA_DIR:
162 psz_path = "%s/Library/Application Support/%s";
163 break;
164 case VLC_CACHE_DIR:
165 psz_path = "%s/Library/Caches/%s";
166 break;
167 default:
168 vlc_assert_unreachable();
169 break;
172 // Default fallback
173 const char *fallback = "org.videolan.vlc";
174 char *name = NULL;
176 CFBundleRef mainBundle = CFBundleGetMainBundle();
177 if (mainBundle) {
178 CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
179 if (identifierAsNS)
180 name = FromCFString(identifierAsNS, kCFStringEncodingUTF8);
183 char *psz_parent = config_GetHomeDir ();
184 char *psz_dir;
185 if ( asprintf( &psz_dir, psz_path, psz_parent, (name) ? name : fallback) == -1 )
186 psz_dir = NULL;
187 free(psz_parent);
188 free(name);
190 return psz_dir;
193 char *config_GetUserDir (vlc_userdir_t type)
195 const char *psz_path;
196 switch (type) {
197 case VLC_CONFIG_DIR:
198 case VLC_TEMPLATES_DIR:
199 case VLC_USERDATA_DIR:
200 case VLC_CACHE_DIR:
201 return getAppDependentDir(type);
203 case VLC_DESKTOP_DIR:
204 psz_path = "%s/Desktop";
205 break;
206 case VLC_DOWNLOAD_DIR:
207 psz_path = "%s/Downloads";
208 break;
209 case VLC_DOCUMENTS_DIR:
210 psz_path = "%s/Documents";
211 break;
212 case VLC_MUSIC_DIR:
213 psz_path = "%s/Music";
214 break;
215 case VLC_PICTURES_DIR:
216 psz_path = "%s/Pictures";
217 break;
218 case VLC_VIDEOS_DIR:
219 psz_path = "%s/Movies";
220 break;
221 case VLC_PUBLICSHARE_DIR:
222 psz_path = "%s/Public";
223 break;
224 case VLC_HOME_DIR:
225 default:
226 psz_path = "%s";
228 char *psz_parent = config_GetHomeDir();
229 char *psz_dir;
230 if (asprintf( &psz_dir, psz_path, psz_parent ) == -1)
231 psz_dir = NULL;
232 free(psz_parent);
233 return psz_dir;