util.c
3.86 KB
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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* Copyright (C) 2008 Banco do Brasil S.A.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @brief Mac Utility functions.
*/
#include <config.h>
#include <stdarg.h>
#include <internals.h>
#include <unistd.h>
#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CFURL.h>
#include <sys/syslimits.h>
#include <lib3270.h>
#include <lib3270/os.h>
static char * concat(char *path, const char *name, size_t *length) {
size_t szCurrent = strlen(path);
if(szCurrent > 1 && path[szCurrent-1] != '/')
strcat(path,"/");
szCurrent += strlen(name);
if(szCurrent >= *length) {
*length += (szCurrent + 1024);
path = lib3270_realloc(path,*length);
}
strcat(path,name);
return path;
}
static char * build_filename(const char *root, const char *str, va_list args) {
size_t szFilename = 1024 + strlen(root);
char * filename = (char *) lib3270_malloc(szFilename);
strcpy(filename,root);
while(str) {
filename = concat(filename,str,&szFilename);
str = va_arg(args, const char *);
}
return (char *) lib3270_realloc(filename,strlen(filename)+1);
}
char * lib3270_build_data_filename(const char *str, ...) {
va_list args;
size_t szPath = PATH_MAX;
lib3270_autoptr(char) path = (char *) lib3270_malloc(szPath);
char *filename = NULL;
//
// Try bundle
//
CFBundleRef mainBundle = CFBundleGetMainBundle();
if (mainBundle) {
CFURLRef url = CFBundleCopyBundleURL(mainBundle);
if (url) {
CFURLGetFileSystemRepresentation(url, true, (UInt8 *) path, szPath);
CFRelease(url);
path = concat(path, "Contents/Resources", &szPath);
if(access(path,R_OK) == 0) {
va_start (args, str);
filename = build_filename(path, str, args);
va_end (args);
return filename;
}
#ifdef DEBUG
else {
debug("No bundle in '%s'",path);
}
#endif // DEBUG
}
}
/*
//
// Try EXE Path
//
{
uint32_t size = szPath;
_NSGetExecutablePath(path, &size);
path[size] = 0;
}
*/
//
// Not found, use the system datadir
//
va_start (args, str);
filename = build_filename(LIB3270_STRINGIZE_VALUE_OF(DATADIR), str, args);
va_end (args);
return filename;
}
char * lib3270_build_config_filename(const char *str, ...) {
va_list args;
va_start (args, str);
char *filename = build_filename(LIB3270_STRINGIZE_VALUE_OF(CONFDIR), str, args);
va_end (args);
return filename;
}
char * lib3270_build_filename(const char *str, ...) {
size_t szFilename = 1024;
char * filename = (char *) lib3270_malloc(szFilename);
char * tempname;
// First build the base filename
memset(filename,0,szFilename);
va_list args;
va_start (args, str);
while(str) {
filename = concat(filename,str,&szFilename);
str = va_arg(args, const char *);
}
va_end (args);
// Check paths
size_t ix;
static const char * paths[] = {
LIB3270_STRINGIZE_VALUE_OF(DATADIR),
LIB3270_STRINGIZE_VALUE_OF(CONFDIR),
"."
};
for(ix = 0; ix < (sizeof(paths)/sizeof(paths[0])); ix++) {
tempname = lib3270_strdup_printf("%s/%s",paths[ix],filename);
if(access(tempname, F_OK) == 0) {
lib3270_free(filename);
return tempname;
}
lib3270_free(tempname);
}
// Not found! Force the standard data dir
tempname = lib3270_strdup_printf("%s/%s",paths[0],filename);
lib3270_free(filename);
return tempname;
}