aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/bufprint.c
blob: 4626a0fed778280592af8aa5cb45305bf70ee86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/

#include "android/utils/bufprint.h"
#include "android/utils/path.h"
#include "android/utils/debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#  define WIN32_LEAN_AND_MEAN
#  include "windows.h"
#  include "shlobj.h"
#else
#  include <unistd.h>
#  include <sys/stat.h>
#endif

#define  D(...)  VERBOSE_PRINT(init,__VA_ARGS__)


/** USEFUL STRING BUFFER FUNCTIONS
 **/

char*
vbufprint( char*        buffer,
           char*        buffer_end,
           const char*  fmt,
           va_list      args )
{
    int  len = vsnprintf( buffer, buffer_end - buffer, fmt, args );
    if (len < 0 || buffer+len >= buffer_end) {
        if (buffer < buffer_end)
            buffer_end[-1] = 0;
        return buffer_end;
    }
    return buffer + len;
}

char*
bufprint(char*  buffer, char*  end, const char*  fmt, ... )
{
    va_list  args;
    char*    result;

    va_start(args, fmt);
    result = vbufprint(buffer, end, fmt, args);
    va_end(args);
    return  result;
}

/** USEFUL DIRECTORY SUPPORT
 **
 **  bufprint_app_dir() returns the directory where the emulator binary is located
 **
 **  get_android_home() returns a user-specific directory where the emulator will
 **  store its writable data (e.g. config files, profiles, etc...).
 **  on Unix, this is $HOME/.android, on Windows, this is something like
 **  "%USERPROFILE%/Local Settings/AppData/Android" on XP, and something different
 **  on Vista.
 **
 **  both functions return a string that must be freed by the caller
 **/

#ifdef __linux__
char*
bufprint_app_dir(char*  buff, char*  end)
{
    char   path[1024];
    int    len;
    char*  x;

    len = readlink("/proc/self/exe", path, sizeof(path));
    if (len <= 0 || len >= (int)sizeof(path)) goto Fail;
    path[len] = 0;

    x = strrchr(path, '/');
    if (x == 0) goto Fail;
    *x = 0;

    return bufprint(buff, end, "%s", path);
Fail:
    fprintf(stderr,"cannot locate application directory\n");
    exit(1);
    return end;
}

#elif defined(__APPLE__)
/* the following hack is needed in order to build with XCode 3.1
 * don't ask me why, but it seems that there were changes in the
 * GCC compiler that we don't have in our pre-compiled version
 */
#ifndef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
#define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ MAC_OS_X_VERSION_10_4
#endif
#import <Carbon/Carbon.h>
#include <unistd.h>

char*
bufprint_app_dir(char*  buff, char*  end)
{
    ProcessSerialNumber psn;
    CFDictionaryRef     dict;
    CFStringRef         value;
    char                s[PATH_MAX];
    char*               x;

    GetCurrentProcess(&psn);
    dict  = ProcessInformationCopyDictionary(&psn, 0xffffffff);
    value = (CFStringRef)CFDictionaryGetValue(dict,
                                             CFSTR("CFBundleExecutable"));
    CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8);
    x = strrchr(s, '/');
    if (x == 0) goto fail;
    *x = 0;

    return bufprint(buff, end, "%s", s);
fail:
    fprintf(stderr,"cannot locate application directory\n");
    exit(1);
    return end;
}
#elif defined _WIN32
char*
bufprint_app_dir(char*  buff, char*  end)
{
    char   appDir[MAX_PATH];
	int    len;
	char*  sep;

    len = GetModuleFileName( 0, appDir, sizeof(appDir)-1 );
	if (len == 0) {
		fprintf(stderr, "PANIC CITY!!\n");
		exit(1);
	}
	if (len >= (int)sizeof(appDir)) {
		len = sizeof(appDir)-1;
	    appDir[len] = 0;
    }

	sep = strrchr(appDir, '\\');
	if (sep)
	  *sep = 0;

    return bufprint(buff, end, "%s", appDir);
}
#else
char*
bufprint_app_dir(char*  buff, char*  end)
{
    return bufprint(buff, end, ".");
}
#endif

#define  _ANDROID_PATH   ".android"

char*
bufprint_config_path(char*  buff, char*  end)
{
#ifdef _WIN32
    const char*  home = getenv("ANDROID_SDK_HOME");
    if (home != NULL) {
        return bufprint(buff, end, "%s\\%s", home, _ANDROID_PATH );
    } else {
        char  path[MAX_PATH];

        SHGetFolderPath( NULL, CSIDL_PROFILE,
                         NULL, 0, path);

        return bufprint(buff, end, "%s\\%s", path, _ANDROID_PATH );
    }
#else
    const char*  home = getenv("ANDROID_SDK_HOME");
    if (home == NULL)
        home = getenv("HOME");
    if (home == NULL)
        home = "/tmp";
    return bufprint(buff, end, "%s/%s", home, _ANDROID_PATH );
#endif
}

char*
bufprint_config_file(char*  buff, char*  end, const char*  suffix)
{
    char*   p;
    p = bufprint_config_path(buff, end);
    p = bufprint(p, end, PATH_SEP "%s", suffix);
    return p;
}

char*
bufprint_temp_dir(char*  buff, char*  end)
{
#ifdef _WIN32
    char   path[MAX_PATH];
    DWORD  retval;

    retval = GetTempPath( sizeof(path), path );
    if (retval > sizeof(path) || retval == 0) {
        D( "can't locate TEMP directory" );
        strncpy(path, "C:\\Temp", sizeof(path) );
    }
    strncat( path, "\\AndroidEmulator", sizeof(path)-1 );
    path_mkdir(path, 0744);

    return  bufprint(buff, end, "%s", path);
#else
    char path[MAX_PATH];
    const char*  tmppath = getenv("ANDROID_TMP");
    if (!tmppath) {
        const char* user = getenv("USER");
        if (user == NULL || user[0] == '\0')
            user = "unknown";

        snprintf(path, sizeof path, "/tmp/android-%s", user);
        tmppath = path;
    }
    mkdir(tmppath, 0744);
    return  bufprint(buff, end, "%s", tmppath );
#endif
}

char*
bufprint_temp_file(char*  buff, char*  end, const char*  suffix)
{
    char*  p;
    p = bufprint_temp_dir(buff, end);
    p = bufprint(p, end, PATH_SEP "%s", suffix);
    return p;
}