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
6  *
7  * Authors: Rémi Denis-Courmont
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *          Pierre d'Herbemont <pdherbemont # videolan org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25 
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29 
30 #include <vlc_common.h>
31 #include "../libvlc.h"
32 
33 #include <libgen.h>
34 #include <dlfcn.h>
35 #include <mach-o/dyld.h>
36 
37 #include <CoreFoundation/CoreFoundation.h>
38 
config_GetLibDir(void)39 char *config_GetLibDir (void)
40 {
41     /* Get the full program path and name */
42     /* First try to see if we are linked to the framework */
43     for (unsigned i = 0; i < _dyld_image_count(); i++)
44     {
45         const char *psz_img_name = _dyld_get_image_name(i);
46         const char *p = strstr( psz_img_name, "VLCKit.framework/Versions/" );
47 
48         /* Check for "VLCKit.framework/Versions/Current/VLCKit",
49          * as well as "VLCKit.framework/Versions/A/VLCKit" and
50          * "VLC.framework/Versions/B/VLCKit" */
51         if (p != NULL) {
52             /* Look for the next forward slash */
53             p += 26; /* p_char += strlen(" VLCKit.framework/Versions/" ) */
54             p += strcspn( p, "/" );
55 
56             /* If the string ends with VLCKit then we've found a winner */
57             if (!strcmp( p, "/VLCKit"))
58                 return strdup( dirname(psz_img_name) );
59         }
60 
61         /* Do we end by "VLC"? If so we are the legacy VLC.app that doesn't
62          * link to VLCKit. */
63         size_t len = strlen(psz_img_name);
64         if (len >= 3 && !strcmp( psz_img_name + len - 3, "VLC"))
65             return strdup( dirname(psz_img_name) );
66 
67         /* Do we end by "VLC-Plugin"? oh, we must be the NPAPI plugin */
68         if (len >= 10 && !strcmp( psz_img_name + len - 10, "VLC-Plugin"))
69             return strdup( dirname(psz_img_name) );
70 
71         /* Do we end by "VLC for iOS"? so we are the iOS app */
72         if (len >= 11 && !strcmp( psz_img_name + len - 11, "VLC for iOS"))
73             return strdup( dirname(psz_img_name) );
74 
75         /* Do we end by "VLC-TV"? so we are the tvOS app */
76         if (len >= 6 && !strcmp( psz_img_name + len - 6, "VLC-TV"))
77             return strdup( dirname(psz_img_name) );
78     }
79 
80     /* we are not part of any Mac-style package but were installed
81      * the UNIX way. let's trick-around a bit */
82     Dl_info info;
83     if (dladdr(system_Init, &info)) {
84         char *incompletepath = strdup(dirname( (char *)info.dli_fname ));
85         char *path = NULL;
86         asprintf(&path, "%s/"PACKAGE, incompletepath);
87         free(incompletepath);
88         return path;
89     }
90 
91     /* should never happen */
92     abort ();
93 }
94 
config_GetDataDir(void)95 char *config_GetDataDir (void)
96 {
97     const char *path = getenv ("VLC_DATA_PATH");
98     if (path)
99         return strdup (path);
100 
101     char *vlcpath = config_GetLibDir ();
102     char *datadir;
103 
104     if (asprintf (&datadir, "%s/share", vlcpath) == -1)
105         datadir = NULL;
106 
107     free (vlcpath);
108     return datadir;
109 }
110 
config_GetHomeDir(void)111 static char *config_GetHomeDir (void)
112 {
113     const char *home = getenv ("HOME");
114 
115     if (home == NULL)
116         home = "/tmp";
117 
118     return strdup (home);
119 }
120 
getAppDependentDir(vlc_userdir_t type)121 static char *getAppDependentDir(vlc_userdir_t type)
122 {
123     const char *psz_path;
124     switch (type) {
125         case VLC_CONFIG_DIR:
126             psz_path = "%s/Library/Preferences/%s";
127             break;
128         case VLC_TEMPLATES_DIR:
129         case VLC_DATA_DIR:
130             psz_path = "%s/Library/Application Support/%s";
131             break;
132         case VLC_CACHE_DIR:
133             psz_path = "%s/Library/Caches/%s";
134             break;
135         default:
136             vlc_assert_unreachable();
137             break;
138     }
139 
140     // Default fallback
141     const char *fallback = "org.videolan.vlc";
142     char *name = NULL;
143 
144     CFBundleRef mainBundle = CFBundleGetMainBundle();
145     if (mainBundle) {
146         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
147         if (identifierAsNS) {
148             CFIndex len = CFStringGetLength(identifierAsNS);
149             CFIndex size = CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8);
150             char *identifier = calloc(len + 1, sizeof(char));
151             if (identifier != NULL) {
152                 Boolean ret = CFStringGetCString(identifierAsNS, identifier, size, kCFStringEncodingUTF8);
153                 if (ret)
154                     name = identifier;
155                 else
156                     free(identifier);
157             }
158         }
159     }
160 
161     char *psz_parent = config_GetHomeDir ();
162     char *psz_dir;
163     if ( asprintf( &psz_dir, psz_path, psz_parent, (name) ? name : fallback) == -1 )
164         psz_dir = NULL;
165     free(psz_parent);
166     free(name);
167 
168     return psz_dir;
169 }
170 
config_GetUserDir(vlc_userdir_t type)171 char *config_GetUserDir (vlc_userdir_t type)
172 {
173     const char *psz_path;
174     switch (type) {
175         case VLC_CONFIG_DIR:
176         case VLC_TEMPLATES_DIR:
177         case VLC_DATA_DIR:
178         case VLC_CACHE_DIR:
179             return getAppDependentDir(type);
180 
181         case VLC_DESKTOP_DIR:
182             psz_path = "%s/Desktop";
183             break;
184         case VLC_DOWNLOAD_DIR:
185             psz_path = "%s/Downloads";
186             break;
187         case VLC_DOCUMENTS_DIR:
188             psz_path = "%s/Documents";
189             break;
190         case VLC_MUSIC_DIR:
191             psz_path = "%s/Music";
192             break;
193         case VLC_PICTURES_DIR:
194             psz_path = "%s/Pictures";
195             break;
196         case VLC_VIDEOS_DIR:
197             psz_path = "%s/Movies";
198             break;
199         case VLC_PUBLICSHARE_DIR:
200             psz_path = "%s/Public";
201             break;
202         case VLC_HOME_DIR:
203         default:
204             psz_path = "%s";
205     }
206     char *psz_parent = config_GetHomeDir();
207     char *psz_dir;
208     if (asprintf( &psz_dir, psz_path, psz_parent ) == -1)
209         psz_dir = NULL;
210     free(psz_parent);
211     return psz_dir;
212 }
213