1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : adsocket.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include <unistd.h> |
---|
12 | |
---|
13 | #include <climits> |
---|
14 | #include <cstdarg> |
---|
15 | #include <cctype> |
---|
16 | |
---|
17 | #include <netdb.h> |
---|
18 | #include <netinet/tcp.h> |
---|
19 | #include <signal.h> |
---|
20 | #include <sys/mman.h> |
---|
21 | #include <sys/socket.h> |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <sys/time.h> |
---|
24 | #include <sys/un.h> |
---|
25 | |
---|
26 | #if defined(DARWIN) |
---|
27 | # include <sys/sysctl.h> |
---|
28 | #endif // DARWIN |
---|
29 | |
---|
30 | #include <arb_cs.h> |
---|
31 | #include <arb_str.h> |
---|
32 | #include <arb_strbuf.h> |
---|
33 | #include <arb_file.h> |
---|
34 | #include <arb_sleep.h> |
---|
35 | #include <arb_pathlen.h> |
---|
36 | |
---|
37 | #include "gb_comm.h" |
---|
38 | #include "gb_data.h" |
---|
39 | #include "gb_localdata.h" |
---|
40 | |
---|
41 | #include <SigHandler.h> |
---|
42 | |
---|
43 | #include <algorithm> |
---|
44 | #include <arb_misc.h> |
---|
45 | #include <arb_defs.h> |
---|
46 | |
---|
47 | // ------------------------------------------------ |
---|
48 | // private read and write socket functions |
---|
49 | |
---|
50 | void gbcm_read_flush() { |
---|
51 | gb_local->write_ptr = gb_local->write_buffer; |
---|
52 | gb_local->write_free = gb_local->write_bufsize; |
---|
53 | } |
---|
54 | |
---|
55 | static long gbcm_read_buffered(int socket, char *ptr, long size) { |
---|
56 | /* write_ptr ptr to not read data |
---|
57 | write_free = write_bufsize-size of non read data; |
---|
58 | */ |
---|
59 | long holding; |
---|
60 | holding = gb_local->write_bufsize - gb_local->write_free; |
---|
61 | if (holding <= 0) { |
---|
62 | holding = read(socket, gb_local->write_buffer, (size_t)gb_local->write_bufsize); |
---|
63 | |
---|
64 | if (holding < 0) |
---|
65 | { |
---|
66 | fprintf(stderr, "Cannot read data from client: len=%li (%s, errno %i)\n", |
---|
67 | holding, strerror(errno), errno); |
---|
68 | return 0; |
---|
69 | } |
---|
70 | gbcm_read_flush(); |
---|
71 | gb_local->write_free-=holding; |
---|
72 | } |
---|
73 | if (size>holding) size = holding; |
---|
74 | memcpy(ptr, gb_local->write_ptr, (int)size); |
---|
75 | gb_local->write_ptr += size; |
---|
76 | gb_local->write_free += size; |
---|
77 | return size; |
---|
78 | } |
---|
79 | |
---|
80 | long gbcm_read(int socket, char *ptr, long size) { |
---|
81 | long leftsize = size; |
---|
82 | while (leftsize) { |
---|
83 | long readsize = gbcm_read_buffered(socket, ptr, leftsize); |
---|
84 | if (readsize<=0) return 0; |
---|
85 | ptr += readsize; |
---|
86 | leftsize -= readsize; |
---|
87 | } |
---|
88 | |
---|
89 | return size; |
---|
90 | } |
---|
91 | |
---|
92 | GBCM_ServerResult gbcm_write_flush(int socket) { |
---|
93 | long leftsize = gb_local->write_ptr - gb_local->write_buffer; |
---|
94 | ssize_t writesize; |
---|
95 | char *ptr = gb_local->write_buffer; |
---|
96 | |
---|
97 | // once we're done, the buffer will be free |
---|
98 | gb_local->write_free = gb_local->write_bufsize; |
---|
99 | gb_local->write_ptr = gb_local->write_buffer; |
---|
100 | |
---|
101 | while (leftsize) { |
---|
102 | #ifdef MSG_NOSIGNAL |
---|
103 | // Linux has MSG_NOSIGNAL, but not SO_NOSIGPIPE |
---|
104 | // prevent SIGPIPE here |
---|
105 | writesize = send(socket, ptr, leftsize, MSG_NOSIGNAL); |
---|
106 | #else |
---|
107 | writesize = write(socket, ptr, leftsize); |
---|
108 | #endif |
---|
109 | |
---|
110 | if (writesize<0) { |
---|
111 | if (gb_local->iamclient) { |
---|
112 | fprintf(stderr, |
---|
113 | "Client (pid=%i) terminating after failure to contact database (%s).", |
---|
114 | getpid(), strerror(errno)); |
---|
115 | exit(EXIT_SUCCESS); |
---|
116 | } |
---|
117 | else { |
---|
118 | fprintf(stderr, "Error sending data to client (%s).", strerror(errno)); |
---|
119 | return GBCM_SERVER_FAULT; |
---|
120 | } |
---|
121 | } |
---|
122 | ptr += writesize; |
---|
123 | leftsize -= writesize; |
---|
124 | } |
---|
125 | |
---|
126 | return GBCM_SERVER_OK; |
---|
127 | } |
---|
128 | |
---|
129 | GBCM_ServerResult gbcm_write(int socket, const char *ptr, long size) { |
---|
130 | while (size >= gb_local->write_free) { |
---|
131 | memcpy(gb_local->write_ptr, ptr, (int)gb_local->write_free); |
---|
132 | gb_local->write_ptr += gb_local->write_free; |
---|
133 | size -= gb_local->write_free; |
---|
134 | ptr += gb_local->write_free; |
---|
135 | |
---|
136 | gb_local->write_free = 0; |
---|
137 | if (gbcm_write_flush(socket)) return GBCM_SERVER_FAULT; |
---|
138 | } |
---|
139 | memcpy(gb_local->write_ptr, ptr, (int)size); |
---|
140 | gb_local->write_ptr += size; |
---|
141 | gb_local->write_free -= size; |
---|
142 | return GBCM_SERVER_OK; |
---|
143 | } |
---|
144 | |
---|
145 | GB_ERROR gbcm_open_socket(const char *path, bool do_connect, int *psocket, char **unix_name) { |
---|
146 | if (path && strcmp(path, ":") == 0) { |
---|
147 | path = GBS_read_arb_tcp("ARB_DB_SERVER"); |
---|
148 | if (!path) { |
---|
149 | return GB_await_error(); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return arb_open_socket(path, do_connect, psocket, unix_name); |
---|
154 | } |
---|
155 | |
---|
156 | #if defined(WARN_TODO) |
---|
157 | #warning gbcms_close is unused |
---|
158 | #endif |
---|
159 | long gbcms_close(gbcmc_comm *link) { |
---|
160 | if (link->socket) { |
---|
161 | close(link->socket); |
---|
162 | link->socket = 0; |
---|
163 | if (link->unix_name) { |
---|
164 | unlink(link->unix_name); |
---|
165 | } |
---|
166 | } |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | gbcmc_comm *gbcmc_open(const char *path) { |
---|
171 | gbcmc_comm *link = ARB_calloc<gbcmc_comm>(1); |
---|
172 | GB_ERROR err = gbcm_open_socket(path, true, &link->socket, &link->unix_name); |
---|
173 | |
---|
174 | if (err) { |
---|
175 | if (link->unix_name) free(link->unix_name); // @@@ |
---|
176 | free(link); |
---|
177 | if (*err) { |
---|
178 | GB_internal_errorf("ARB_DB_CLIENT_OPEN\n(Reason: %s)", err); |
---|
179 | } |
---|
180 | return 0; |
---|
181 | } |
---|
182 | gb_local->iamclient = true; |
---|
183 | return link; |
---|
184 | } |
---|
185 | |
---|
186 | long gbcm_write_two(int socket, long a, long c) { |
---|
187 | long ia[3]; |
---|
188 | ia[0] = a; |
---|
189 | ia[1] = 3; |
---|
190 | ia[2] = c; |
---|
191 | if (!socket) return 1; |
---|
192 | return gbcm_write(socket, (const char *)ia, sizeof(long)*3); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | GBCM_ServerResult gbcm_read_two(int socket, long a, long *b, long *c) { |
---|
197 | /*! read two values: length and any user long |
---|
198 | * |
---|
199 | * if data is send by gbcm_write_two() then @param b should be zero |
---|
200 | * and is not used! |
---|
201 | */ |
---|
202 | |
---|
203 | long ia[3]; |
---|
204 | long size; |
---|
205 | size = gbcm_read(socket, (char *)&(ia[0]), sizeof(long)*3); |
---|
206 | if (size != sizeof(long) * 3) { |
---|
207 | GB_internal_errorf("receive failed: %zu bytes expected, %li got, keyword %lX", |
---|
208 | sizeof(long) * 3, size, a); |
---|
209 | return GBCM_SERVER_FAULT; |
---|
210 | } |
---|
211 | if (ia[0] != a) { |
---|
212 | GB_internal_errorf("received keyword failed %lx != %lx\n", ia[0], a); |
---|
213 | return GBCM_SERVER_FAULT; |
---|
214 | } |
---|
215 | if (b) { |
---|
216 | *b = ia[1]; |
---|
217 | } |
---|
218 | else { |
---|
219 | if (ia[1]!=3) { |
---|
220 | GB_internal_error("receive failed: size not 3\n"); |
---|
221 | return GBCM_SERVER_FAULT; |
---|
222 | } |
---|
223 | } |
---|
224 | *c = ia[2]; |
---|
225 | return GBCM_SERVER_OK; |
---|
226 | } |
---|
227 | |
---|
228 | GBCM_ServerResult gbcm_write_string(int socket, const char *key) { |
---|
229 | if (key) { |
---|
230 | size_t len = strlen(key); |
---|
231 | gbcm_write_long(socket, len); |
---|
232 | if (len) gbcm_write(socket, key, len); |
---|
233 | } |
---|
234 | else { |
---|
235 | gbcm_write_long(socket, -1); |
---|
236 | } |
---|
237 | return GBCM_SERVER_OK; |
---|
238 | } |
---|
239 | |
---|
240 | char *gbcm_read_string(int socket) |
---|
241 | { |
---|
242 | char *key; |
---|
243 | long len = gbcm_read_long(socket); |
---|
244 | |
---|
245 | if (len) { |
---|
246 | if (len>0) { |
---|
247 | ARB_calloc(key, len+1); |
---|
248 | gbcm_read(socket, key, len); |
---|
249 | } |
---|
250 | else { |
---|
251 | key = 0; |
---|
252 | } |
---|
253 | } |
---|
254 | else { |
---|
255 | key = ARB_strdup(""); |
---|
256 | } |
---|
257 | |
---|
258 | return key; |
---|
259 | } |
---|
260 | |
---|
261 | GBCM_ServerResult gbcm_write_long(int socket, long data) { |
---|
262 | gbcm_write(socket, (char*)&data, sizeof(data)); |
---|
263 | return GBCM_SERVER_OK; |
---|
264 | } |
---|
265 | |
---|
266 | long gbcm_read_long(int socket) { |
---|
267 | long data; |
---|
268 | gbcm_read(socket, (char*)&data, sizeof(data)); |
---|
269 | return data; |
---|
270 | } |
---|
271 | |
---|
272 | char *GB_read_fp(FILE *in) { |
---|
273 | /*! like GB_read_file(), but works on already open file |
---|
274 | * (useful together with GB_fopen_tempfile()) |
---|
275 | * |
---|
276 | * Note: File should be opened in text-mode (e.g. "rt") |
---|
277 | */ |
---|
278 | |
---|
279 | GBS_strstruct *buf = GBS_stropen(4096); |
---|
280 | int c; |
---|
281 | |
---|
282 | while (EOF != (c = getc(in))) { |
---|
283 | GBS_chrcat(buf, c); |
---|
284 | } |
---|
285 | return GBS_strclose(buf); |
---|
286 | } |
---|
287 | |
---|
288 | char *GB_read_file(const char *path) { // consider using class FileContent instead |
---|
289 | /*! read content of file 'path' into string (heap-copy) |
---|
290 | * |
---|
291 | * if path is '-', read from STDIN |
---|
292 | * |
---|
293 | * @return NULL in case of error (use GB_await_error() to get the message) |
---|
294 | */ |
---|
295 | char *result = 0; |
---|
296 | |
---|
297 | if (strcmp(path, "-") == 0) { |
---|
298 | result = GB_read_fp(stdin); |
---|
299 | } |
---|
300 | else { |
---|
301 | char *epath = GBS_eval_env(path); |
---|
302 | |
---|
303 | if (epath) { |
---|
304 | FILE *in = fopen(epath, "rt"); |
---|
305 | |
---|
306 | if (!in) GB_export_error(GB_IO_error("reading", epath)); |
---|
307 | else { |
---|
308 | long data_size = GB_size_of_file(epath); |
---|
309 | |
---|
310 | if (data_size >= 0) { |
---|
311 | result = ARB_alloc<char>(data_size+1); |
---|
312 | |
---|
313 | data_size = fread(result, 1, data_size, in); |
---|
314 | result[data_size] = 0; |
---|
315 | } |
---|
316 | fclose(in); |
---|
317 | } |
---|
318 | } |
---|
319 | free(epath); |
---|
320 | } |
---|
321 | return result; |
---|
322 | } |
---|
323 | |
---|
324 | char *GB_map_FILE(FILE *in, int writeable) { |
---|
325 | int fi = fileno(in); |
---|
326 | size_t size = GB_size_of_FILE(in); |
---|
327 | char *buffer; |
---|
328 | if (size<=0) { |
---|
329 | GB_export_error("GB_map_file: sorry file not found"); |
---|
330 | return NULL; |
---|
331 | } |
---|
332 | if (writeable) { |
---|
333 | buffer = (char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fi, 0); |
---|
334 | } |
---|
335 | else { |
---|
336 | buffer = (char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fi, 0); |
---|
337 | } |
---|
338 | if (buffer == MAP_FAILED) { |
---|
339 | GB_export_errorf("GB_map_file: Error: Out of Memory: mmap failed (errno: %i)", errno); |
---|
340 | return NULL; |
---|
341 | } |
---|
342 | return buffer; |
---|
343 | } |
---|
344 | |
---|
345 | char *GB_map_file(const char *path, int writeable) { |
---|
346 | FILE *in; |
---|
347 | char *buffer; |
---|
348 | in = fopen(path, "r"); |
---|
349 | if (!in) { |
---|
350 | GB_export_errorf("GB_map_file: sorry file '%s' not readable", path); |
---|
351 | return NULL; |
---|
352 | } |
---|
353 | buffer = GB_map_FILE(in, writeable); |
---|
354 | fclose(in); |
---|
355 | return buffer; |
---|
356 | } |
---|
357 | |
---|
358 | GB_ULONG GB_time_of_day() { |
---|
359 | timeval tp; |
---|
360 | if (gettimeofday(&tp, 0)) return 0; |
---|
361 | return tp.tv_sec; |
---|
362 | } |
---|
363 | |
---|
364 | GB_ERROR GB_textprint(const char *path) { |
---|
365 | // goes to header: __ATTR__USERESULT |
---|
366 | char *fpath = GBS_eval_env(path); |
---|
367 | char *quoted_fpath = GBK_singlequote(fpath); |
---|
368 | const char *command = GBS_global_string("arb_textprint %s &", quoted_fpath); |
---|
369 | GB_ERROR error = GBK_system(command); |
---|
370 | error = GB_failedTo_error("print textfile", fpath, error); |
---|
371 | free(quoted_fpath); |
---|
372 | free(fpath); |
---|
373 | return error; |
---|
374 | } |
---|
375 | |
---|
376 | // -------------------------------------------------------------------------------- |
---|
377 | |
---|
378 | static GB_CSTR GB_getenvPATH() { |
---|
379 | static const char *path = 0; |
---|
380 | if (!path) { |
---|
381 | path = ARB_getenv_ignore_empty("PATH"); |
---|
382 | if (!path) { |
---|
383 | path = GBS_eval_env("/bin:/usr/bin:$(ARBHOME)/bin"); |
---|
384 | GB_informationf("Your PATH variable is empty - using '%s' as search path.", path); |
---|
385 | } |
---|
386 | else { |
---|
387 | char *arbbin = GBS_eval_env("$(ARBHOME)/bin"); |
---|
388 | if (strstr(path, arbbin) == 0) { |
---|
389 | GB_warningf("Your PATH variable does not contain '%s'. Things may not work as expected.", arbbin); |
---|
390 | } |
---|
391 | free(arbbin); |
---|
392 | } |
---|
393 | } |
---|
394 | return path; |
---|
395 | } |
---|
396 | |
---|
397 | // -------------------------------------------------------------------------------- |
---|
398 | // Functions to find an executable |
---|
399 | |
---|
400 | static char *GB_find_executable(GB_CSTR description_of_executable, ...) { |
---|
401 | // goes to header: __ATTR__SENTINEL |
---|
402 | /* search the path for an executable with any of the given names (...) |
---|
403 | * if any is found, it's full path is returned |
---|
404 | * if none is found, a warning call is returned (which can be executed without harm) |
---|
405 | */ |
---|
406 | |
---|
407 | GB_CSTR name; |
---|
408 | char *found = 0; |
---|
409 | va_list args; |
---|
410 | |
---|
411 | va_start(args, description_of_executable); |
---|
412 | while (!found && (name = va_arg(args, GB_CSTR)) != 0) found = ARB_executable(name, GB_getenvPATH()); |
---|
413 | va_end(args); |
---|
414 | |
---|
415 | if (!found) { // none of the executables has been found |
---|
416 | char *looked_for; |
---|
417 | char *msg; |
---|
418 | { |
---|
419 | GBS_strstruct *buf = GBS_stropen(100); |
---|
420 | int first = 1; |
---|
421 | |
---|
422 | va_start(args, description_of_executable); |
---|
423 | while ((name = va_arg(args, GB_CSTR)) != 0) { |
---|
424 | if (!first) GBS_strcat(buf, ", "); |
---|
425 | first = 0; |
---|
426 | GBS_strcat(buf, name); |
---|
427 | } |
---|
428 | va_end(args); |
---|
429 | looked_for = GBS_strclose(buf); |
---|
430 | } |
---|
431 | |
---|
432 | msg = GBS_global_string_copy("Could not find a %s (looked for: %s)", description_of_executable, looked_for); |
---|
433 | GB_warning(msg); |
---|
434 | found = GBS_global_string_copy("echo \"%s\" ; arb_ign Parameters", msg); |
---|
435 | free(msg); |
---|
436 | free(looked_for); |
---|
437 | } |
---|
438 | else { |
---|
439 | GB_informationf("Using %s '%s' ('%s')", description_of_executable, name, found); |
---|
440 | } |
---|
441 | return found; |
---|
442 | } |
---|
443 | |
---|
444 | // -------------------------------------------------------------------------------- |
---|
445 | // Functions to access the environment variables used by ARB: |
---|
446 | |
---|
447 | static char *getenv_executable(GB_CSTR envvar) { |
---|
448 | // get full path of executable defined by 'envvar' |
---|
449 | // returns 0 if |
---|
450 | // - envvar not defined or |
---|
451 | // - not defining an executable (warns about that) |
---|
452 | |
---|
453 | char *result = 0; |
---|
454 | const char *exe_name = ARB_getenv_ignore_empty(envvar); |
---|
455 | |
---|
456 | if (exe_name) { |
---|
457 | result = ARB_executable(exe_name, GB_getenvPATH()); |
---|
458 | if (!result) { |
---|
459 | GB_warningf("Environment variable '%s' contains '%s' (which is not an executable)", envvar, exe_name); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | return result; |
---|
464 | } |
---|
465 | |
---|
466 | static char *getenv_existing_directory(GB_CSTR envvar) { |
---|
467 | // get full path of directory defined by 'envvar' |
---|
468 | // return 0 if |
---|
469 | // - envvar is not defined or |
---|
470 | // - does not point to a directory (warns about that) |
---|
471 | |
---|
472 | char *result = 0; |
---|
473 | const char *dir_name = ARB_getenv_ignore_empty(envvar); |
---|
474 | |
---|
475 | if (dir_name) { |
---|
476 | if (GB_is_directory(dir_name)) { |
---|
477 | result = ARB_strdup(dir_name); |
---|
478 | } |
---|
479 | else { |
---|
480 | GB_warningf("Environment variable '%s' should contain the path of an existing directory.\n" |
---|
481 | "(current content '%s' has been ignored.)", envvar, dir_name); |
---|
482 | } |
---|
483 | } |
---|
484 | return result; |
---|
485 | } |
---|
486 | |
---|
487 | static void GB_setenv(const char *var, const char *value) { |
---|
488 | if (setenv(var, value, 1) != 0) { |
---|
489 | GB_warningf("Could not set environment variable '%s'. This might cause problems in subprocesses.\n" |
---|
490 | "(Reason: %s)", var, strerror(errno)); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | GB_CSTR GB_getenvARB_XTERM() { |
---|
495 | static const char *xterm = 0; |
---|
496 | if (!xterm) { |
---|
497 | xterm = ARB_getenv_ignore_empty("ARB_XTERM"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_XTERM |
---|
498 | if (!xterm) xterm = "xterm -sl 1000 -sb -geometry 150x60"; |
---|
499 | } |
---|
500 | return xterm; |
---|
501 | } |
---|
502 | |
---|
503 | static GB_CSTR GB_getenvARB_XCMD() { |
---|
504 | static const char *xcmd = 0; |
---|
505 | if (!xcmd) { |
---|
506 | xcmd = ARB_getenv_ignore_empty("ARB_XCMD"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_XCMD |
---|
507 | if (!xcmd) { |
---|
508 | const char *xterm = GB_getenvARB_XTERM(); |
---|
509 | gb_assert(xterm); |
---|
510 | xcmd = GBS_global_string_copy("%s -e", xterm); |
---|
511 | } |
---|
512 | } |
---|
513 | return xcmd; |
---|
514 | } |
---|
515 | |
---|
516 | GB_CSTR GB_getenvUSER() { |
---|
517 | static const char *user = 0; |
---|
518 | if (!user) { |
---|
519 | user = ARB_getenv_ignore_empty("USER"); |
---|
520 | if (!user) user = ARB_getenv_ignore_empty("LOGNAME"); |
---|
521 | if (!user) { |
---|
522 | user = ARB_getenv_ignore_empty("HOME"); |
---|
523 | if (user && strrchr(user, '/')) user = strrchr(user, '/')+1; |
---|
524 | } |
---|
525 | if (!user) { |
---|
526 | fprintf(stderr, "WARNING: Cannot identify user: environment variables USER, LOGNAME and HOME not set\n"); |
---|
527 | user = "UnknownUser"; |
---|
528 | } |
---|
529 | } |
---|
530 | return user; |
---|
531 | } |
---|
532 | |
---|
533 | |
---|
534 | static GB_CSTR GB_getenvHOME() { |
---|
535 | static SmartCharPtr Home; |
---|
536 | if (Home.isNull()) { |
---|
537 | char *home = getenv_existing_directory("HOME"); |
---|
538 | if (!home) { |
---|
539 | home = nulldup(GB_getcwd()); |
---|
540 | if (!home) home = ARB_strdup("."); |
---|
541 | fprintf(stderr, "WARNING: Cannot identify user's home directory: environment variable HOME not set\n" |
---|
542 | "Using current directory (%s) as home.\n", home); |
---|
543 | } |
---|
544 | gb_assert(home); |
---|
545 | Home = home; |
---|
546 | } |
---|
547 | return &*Home; |
---|
548 | } |
---|
549 | |
---|
550 | GB_CSTR GB_getenvARBHOME() { |
---|
551 | static SmartCharPtr Arbhome; |
---|
552 | if (Arbhome.isNull()) { |
---|
553 | char *arbhome = getenv_existing_directory("ARBHOME"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBHOME |
---|
554 | if (!arbhome) { |
---|
555 | fprintf(stderr, "Fatal ERROR: Environment Variable ARBHOME not found !!!\n" |
---|
556 | " Please set 'ARBHOME' to the installation path of ARB\n"); |
---|
557 | exit(EXIT_FAILURE); |
---|
558 | } |
---|
559 | Arbhome = arbhome; |
---|
560 | } |
---|
561 | return &*Arbhome; |
---|
562 | } |
---|
563 | |
---|
564 | GB_CSTR GB_getenvARBMACRO() { |
---|
565 | static const char *am = 0; |
---|
566 | if (!am) { |
---|
567 | am = getenv_existing_directory("ARBMACRO"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBMACRO |
---|
568 | if (!am) am = ARB_strdup(GB_path_in_ARBLIB("macros")); |
---|
569 | } |
---|
570 | return am; |
---|
571 | } |
---|
572 | |
---|
573 | static char *getenv_autodirectory(const char *envvar, const char *defaultDirectory) { |
---|
574 | // if environment variable 'envvar' contains an existing directory -> use that |
---|
575 | // otherwise fallback to 'defaultDirectory' (create if not existing) |
---|
576 | // return heap-copy of full directory name |
---|
577 | char *dir = getenv_existing_directory(envvar); |
---|
578 | if (!dir) { |
---|
579 | dir = GBS_eval_env(defaultDirectory); |
---|
580 | if (!GB_is_directory(dir)) { |
---|
581 | GB_ERROR error = GB_create_directory(dir); |
---|
582 | if (error) GB_warning(error); |
---|
583 | } |
---|
584 | } |
---|
585 | return dir; |
---|
586 | } |
---|
587 | |
---|
588 | GB_CSTR GB_getenvARB_PROP() { |
---|
589 | static SmartCharPtr ArbProps; |
---|
590 | if (ArbProps.isNull()) ArbProps = getenv_autodirectory("ARB_PROP", GB_path_in_HOME(".arb_prop")); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_PROP |
---|
591 | return &*ArbProps; |
---|
592 | } |
---|
593 | |
---|
594 | GB_CSTR GB_getenvARBMACROHOME() { |
---|
595 | static SmartCharPtr ArbMacroHome; |
---|
596 | if (ArbMacroHome.isNull()) ArbMacroHome = getenv_autodirectory("ARBMACROHOME", GB_path_in_arbprop("macros")); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBMACROHOME |
---|
597 | return &*ArbMacroHome; |
---|
598 | } |
---|
599 | |
---|
600 | GB_CSTR GB_getenvARBCONFIG() { |
---|
601 | static SmartCharPtr ArbConfig; |
---|
602 | if (ArbConfig.isNull()) ArbConfig = getenv_autodirectory("ARBCONFIG", GB_path_in_arbprop("cfgSave")); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARBCONFIG |
---|
603 | return &*ArbConfig; |
---|
604 | } |
---|
605 | |
---|
606 | GB_CSTR GB_getenvARB_GS() { |
---|
607 | static const char *gs = 0; |
---|
608 | if (!gs) { |
---|
609 | gs = getenv_executable("ARB_GS"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_GS |
---|
610 | if (!gs) gs = GB_find_executable("Postscript viewer", "gv", "ghostview", NULL); |
---|
611 | } |
---|
612 | return gs; |
---|
613 | } |
---|
614 | |
---|
615 | GB_CSTR GB_getenvARB_PDFVIEW() { |
---|
616 | static const char *pdfview = 0; |
---|
617 | if (!pdfview) { |
---|
618 | pdfview = getenv_executable("ARB_PDFVIEW"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_PDFVIEW |
---|
619 | if (!pdfview) pdfview = GB_find_executable("PDF viewer", "epdfview", "xpdf", "kpdf", "acroread", "gv", NULL); |
---|
620 | } |
---|
621 | return pdfview; |
---|
622 | } |
---|
623 | |
---|
624 | GB_CSTR GB_getenvARB_TEXTEDIT() { |
---|
625 | static const char *editor = 0; |
---|
626 | if (!editor) { |
---|
627 | editor = getenv_executable("ARB_TEXTEDIT"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_TEXTEDIT |
---|
628 | if (!editor) editor = "arb_textedit"; // a smart editor shell script |
---|
629 | } |
---|
630 | return editor; |
---|
631 | } |
---|
632 | |
---|
633 | GB_CSTR GB_getenvDOCPATH() { |
---|
634 | static const char *dp = 0; |
---|
635 | if (!dp) { |
---|
636 | char *res = getenv_existing_directory("ARB_DOC"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_DOC |
---|
637 | if (res) dp = res; |
---|
638 | else dp = ARB_strdup(GB_path_in_ARBLIB("help")); |
---|
639 | } |
---|
640 | return dp; |
---|
641 | } |
---|
642 | |
---|
643 | GB_CSTR GB_getenvHTMLDOCPATH() { |
---|
644 | static const char *dp = 0; |
---|
645 | if (!dp) { |
---|
646 | char *res = getenv_existing_directory("ARB_HTMLDOC"); // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp@ARB_HTMLDOC |
---|
647 | if (res) dp = res; |
---|
648 | else dp = ARB_strdup(GB_path_in_ARBLIB("help_html")); |
---|
649 | } |
---|
650 | return dp; |
---|
651 | |
---|
652 | } |
---|
653 | |
---|
654 | static gb_getenv_hook getenv_hook = NULL; |
---|
655 | |
---|
656 | NOT4PERL gb_getenv_hook GB_install_getenv_hook(gb_getenv_hook hook) { |
---|
657 | // Install 'hook' to be called by GB_getenv(). |
---|
658 | // If the 'hook' returns NULL, normal expansion takes place. |
---|
659 | // Otherwise GB_getenv() returns result from 'hook' |
---|
660 | |
---|
661 | gb_getenv_hook oldHook = getenv_hook; |
---|
662 | getenv_hook = hook; |
---|
663 | return oldHook; |
---|
664 | } |
---|
665 | |
---|
666 | GB_CSTR GB_getenv(const char *env) { |
---|
667 | if (getenv_hook) { |
---|
668 | const char *result = getenv_hook(env); |
---|
669 | if (result) return result; |
---|
670 | } |
---|
671 | if (strncmp(env, "ARB", 3) == 0) { |
---|
672 | // doc in ../HELP_SOURCE/oldhelp/arb_envar.hlp |
---|
673 | |
---|
674 | if (strcmp(env, "ARBHOME") == 0) return GB_getenvARBHOME(); |
---|
675 | if (strcmp(env, "ARB_PROP") == 0) return GB_getenvARB_PROP(); |
---|
676 | if (strcmp(env, "ARBCONFIG") == 0) return GB_getenvARBCONFIG(); |
---|
677 | if (strcmp(env, "ARBMACROHOME") == 0) return GB_getenvARBMACROHOME(); |
---|
678 | if (strcmp(env, "ARBMACRO") == 0) return GB_getenvARBMACRO(); |
---|
679 | |
---|
680 | if (strcmp(env, "ARB_GS") == 0) return GB_getenvARB_GS(); |
---|
681 | if (strcmp(env, "ARB_PDFVIEW") == 0) return GB_getenvARB_PDFVIEW(); |
---|
682 | if (strcmp(env, "ARB_DOC") == 0) return GB_getenvDOCPATH(); |
---|
683 | if (strcmp(env, "ARB_TEXTEDIT") == 0) return GB_getenvARB_TEXTEDIT(); |
---|
684 | if (strcmp(env, "ARB_XTERM") == 0) return GB_getenvARB_XTERM(); |
---|
685 | if (strcmp(env, "ARB_XCMD") == 0) return GB_getenvARB_XCMD(); |
---|
686 | } |
---|
687 | else { |
---|
688 | if (strcmp(env, "HOME") == 0) return GB_getenvHOME(); |
---|
689 | if (strcmp(env, "USER") == 0) return GB_getenvUSER(); |
---|
690 | } |
---|
691 | |
---|
692 | return ARB_getenv_ignore_empty(env); |
---|
693 | } |
---|
694 | |
---|
695 | struct export_environment { |
---|
696 | export_environment() { |
---|
697 | // set all variables needed in ARB subprocesses |
---|
698 | GB_setenv("ARB_XCMD", GB_getenvARB_XCMD()); |
---|
699 | } |
---|
700 | }; |
---|
701 | |
---|
702 | static export_environment expenv; |
---|
703 | |
---|
704 | bool GB_host_is_local(const char *hostname) { |
---|
705 | // returns true if host is local |
---|
706 | |
---|
707 | arb_assert(hostname); |
---|
708 | arb_assert(hostname[0]); |
---|
709 | |
---|
710 | return |
---|
711 | ARB_stricmp(hostname, "localhost") == 0 || |
---|
712 | ARB_strBeginsWith(hostname, "127.0.0.") || |
---|
713 | ARB_stricmp(hostname, arb_gethostname()) == 0; |
---|
714 | } |
---|
715 | |
---|
716 | static GB_ULONG get_physical_memory() { |
---|
717 | // Returns the physical available memory size in k available for one process |
---|
718 | static GB_ULONG physical_memsize = 0; |
---|
719 | if (!physical_memsize) { |
---|
720 | GB_ULONG memsize; // real existing memory in k |
---|
721 | #if defined(LINUX) |
---|
722 | { |
---|
723 | long pagesize = sysconf(_SC_PAGESIZE); |
---|
724 | long pages = sysconf(_SC_PHYS_PAGES); |
---|
725 | |
---|
726 | memsize = (pagesize/1024) * pages; |
---|
727 | } |
---|
728 | #elif defined(DARWIN) |
---|
729 | #warning memsize detection needs to be tested for Darwin |
---|
730 | { |
---|
731 | int mib[2]; |
---|
732 | uint64_t bytes; |
---|
733 | size_t len; |
---|
734 | |
---|
735 | mib[0] = CTL_HW; |
---|
736 | mib[1] = HW_MEMSIZE; // uint64_t: physical ram size |
---|
737 | len = sizeof(bytes); |
---|
738 | sysctl(mib, 2, &bytes, &len, NULL, 0); |
---|
739 | |
---|
740 | memsize = bytes/1024; |
---|
741 | } |
---|
742 | #else |
---|
743 | memsize = 1024*1024; // assume 1 Gb |
---|
744 | printf("\n" |
---|
745 | "Warning: ARB is not prepared to detect the memory size on your system!\n" |
---|
746 | " (it assumes you have %ul Mb, but does not use more)\n\n", memsize/1024); |
---|
747 | #endif |
---|
748 | |
---|
749 | GB_ULONG net_memsize = memsize - 10240; // reduce by 10Mb |
---|
750 | |
---|
751 | // detect max allocateable memory by ... allocating |
---|
752 | GB_ULONG max_malloc_try = net_memsize*1024; |
---|
753 | GB_ULONG max_malloc = 0; |
---|
754 | { |
---|
755 | GB_ULONG step_size = 4096; |
---|
756 | void *head = 0; |
---|
757 | |
---|
758 | do { |
---|
759 | void **tmp; |
---|
760 | while ((tmp=(void**)malloc(step_size))) { // do NOT use ARB_alloc here! |
---|
761 | *tmp = head; |
---|
762 | head = tmp; |
---|
763 | max_malloc += step_size; |
---|
764 | if (max_malloc >= max_malloc_try) break; |
---|
765 | step_size *= 2; |
---|
766 | } |
---|
767 | } while ((step_size=step_size/2) > sizeof(void*)); |
---|
768 | |
---|
769 | while (head) freeset(head, *(void**)head); |
---|
770 | max_malloc /= 1024; |
---|
771 | } |
---|
772 | |
---|
773 | physical_memsize = std::min(net_memsize, max_malloc); |
---|
774 | |
---|
775 | #if defined(DEBUG) && 0 |
---|
776 | printf("- memsize(real) = %20lu k\n", memsize); |
---|
777 | printf("- memsize(net) = %20lu k\n", net_memsize); |
---|
778 | printf("- memsize(max_malloc) = %20lu k\n", max_malloc); |
---|
779 | #endif // DEBUG |
---|
780 | |
---|
781 | GB_informationf("Visible memory: %s", GBS_readable_size(physical_memsize*1024, "b")); |
---|
782 | } |
---|
783 | |
---|
784 | arb_assert(physical_memsize>0); |
---|
785 | return physical_memsize; |
---|
786 | } |
---|
787 | |
---|
788 | static GB_ULONG parse_env_mem_definition(const char *env_override, GB_ERROR& error) { |
---|
789 | const char *end; |
---|
790 | GB_ULONG num = strtoul(env_override, const_cast<char**>(&end), 10); |
---|
791 | |
---|
792 | error = NULL; |
---|
793 | |
---|
794 | bool valid = num>0 || env_override[0] == '0'; |
---|
795 | if (valid) { |
---|
796 | const char *formatSpec = end; |
---|
797 | double factor = 1; |
---|
798 | |
---|
799 | switch (tolower(formatSpec[0])) { |
---|
800 | case 0: |
---|
801 | num = GB_ULONG(num/1024.0+0.5); // byte->kb |
---|
802 | break; // no format given |
---|
803 | |
---|
804 | case 'g': factor *= 1024; |
---|
805 | case 'm': factor *= 1024; |
---|
806 | case 'k': break; |
---|
807 | |
---|
808 | case '%': |
---|
809 | factor = num/100.0; |
---|
810 | num = get_physical_memory(); |
---|
811 | break; |
---|
812 | |
---|
813 | default: valid = false; break; |
---|
814 | } |
---|
815 | |
---|
816 | if (valid) return GB_ULONG(num*factor+0.5); |
---|
817 | } |
---|
818 | |
---|
819 | error = "expected digits (optionally followed by k, M, G or %)"; |
---|
820 | return 0; |
---|
821 | } |
---|
822 | |
---|
823 | GB_ULONG GB_get_usable_memory() { |
---|
824 | // memory allowed to be used by a single ARB process (in kbyte) |
---|
825 | |
---|
826 | static GB_ULONG useable_memory = 0; |
---|
827 | if (!useable_memory) { |
---|
828 | bool allow_fallback = true; |
---|
829 | const char *env_override = GB_getenv("ARB_MEMORY"); |
---|
830 | const char *via_whom; |
---|
831 | if (env_override) { |
---|
832 | via_whom = "via envar ARB_MEMORY"; |
---|
833 | } |
---|
834 | else { |
---|
835 | FALLBACK: |
---|
836 | env_override = "90%"; // ARB processes do not use more than 90% of physical memory |
---|
837 | via_whom = "by internal default"; |
---|
838 | allow_fallback = false; |
---|
839 | } |
---|
840 | |
---|
841 | gb_assert(env_override); |
---|
842 | |
---|
843 | GB_ERROR env_error; |
---|
844 | GB_ULONG env_memory = parse_env_mem_definition(env_override, env_error); |
---|
845 | if (env_error) { |
---|
846 | GB_warningf("Ignoring invalid setting '%s' %s (%s)", env_override, via_whom, env_error); |
---|
847 | if (allow_fallback) goto FALLBACK; |
---|
848 | GBK_terminate("failed to detect usable memory"); |
---|
849 | } |
---|
850 | |
---|
851 | GB_informationf("Restricting used memory (%s '%s') to %s", via_whom, env_override, GBS_readable_size(env_memory*1024, "b")); |
---|
852 | if (!allow_fallback) { |
---|
853 | GB_informationf("Note: Setting envar ARB_MEMORY will override that restriction (percentage or absolute memsize)"); |
---|
854 | } |
---|
855 | useable_memory = env_memory; |
---|
856 | gb_assert(useable_memory>0 && useable_memory<get_physical_memory()); |
---|
857 | } |
---|
858 | return useable_memory; |
---|
859 | } |
---|
860 | |
---|
861 | // --------------------------- |
---|
862 | // external commands |
---|
863 | |
---|
864 | NOT4PERL GB_ERROR GB_xcmd(const char *cmd, XCMD_TYPE exectype) { |
---|
865 | // goes to header: __ATTR__USERESULT_TODO |
---|
866 | |
---|
867 | // runs a command in an xterm |
---|
868 | |
---|
869 | bool background = exectype & _XCMD__ASYNC; // true -> run asynchronous |
---|
870 | bool wait_only_if_error = !(exectype & _XCMD__WAITKEY); // true -> asynchronous does wait for keypress only if cmd fails |
---|
871 | |
---|
872 | gb_assert(exectype != XCMD_SYNC_WAITKEY); // @@@ previously unused; check how it works and whether that makes sense; fix! |
---|
873 | |
---|
874 | const int BUFSIZE = 1024; |
---|
875 | GBS_strstruct system_call(BUFSIZE); |
---|
876 | |
---|
877 | const char *xcmd = GB_getenvARB_XCMD(); |
---|
878 | |
---|
879 | system_call.put('('); |
---|
880 | system_call.cat(xcmd); |
---|
881 | |
---|
882 | { |
---|
883 | GBS_strstruct bash_command(BUFSIZE); |
---|
884 | |
---|
885 | bash_command.cat("LD_LIBRARY_PATH="); |
---|
886 | { |
---|
887 | char *dquoted_library_path = GBK_doublequote(GB_getenv("LD_LIBRARY_PATH")); |
---|
888 | bash_command.cat(dquoted_library_path); |
---|
889 | free(dquoted_library_path); |
---|
890 | } |
---|
891 | bash_command.cat(";export LD_LIBRARY_PATH; ("); |
---|
892 | bash_command.cat(cmd); |
---|
893 | |
---|
894 | const char *wait_commands = "echo; echo Press ENTER to close this window; read a"; |
---|
895 | if (wait_only_if_error) { |
---|
896 | bash_command.cat(") || ("); |
---|
897 | bash_command.cat(wait_commands); |
---|
898 | } |
---|
899 | else if (background) { |
---|
900 | bash_command.cat("; "); |
---|
901 | bash_command.cat(wait_commands); |
---|
902 | } |
---|
903 | bash_command.put(')'); |
---|
904 | |
---|
905 | system_call.cat(" bash -c "); |
---|
906 | char *squoted_bash_command = GBK_singlequote(bash_command.get_data()); |
---|
907 | system_call.cat(squoted_bash_command); |
---|
908 | free(squoted_bash_command); |
---|
909 | } |
---|
910 | system_call.cat(" )"); |
---|
911 | if (background) system_call.cat(" &"); |
---|
912 | |
---|
913 | return GBK_system(system_call.get_data()); |
---|
914 | } |
---|
915 | |
---|
916 | // --------------------------------------------- |
---|
917 | // path completion (parts former located in AWT) |
---|
918 | // @@@ whole section (+ corresponding tests) should move to adfile.cxx |
---|
919 | |
---|
920 | static int path_toggle = 0; |
---|
921 | static char path_buf[2][ARB_PATH_MAX]; |
---|
922 | |
---|
923 | static char *use_other_path_buf() { |
---|
924 | path_toggle = 1-path_toggle; |
---|
925 | return path_buf[path_toggle]; |
---|
926 | } |
---|
927 | |
---|
928 | GB_CSTR GB_append_suffix(const char *name, const char *suffix) { |
---|
929 | // if suffix != NULL -> append .suffix |
---|
930 | // (automatically removes duplicated '.'s) |
---|
931 | |
---|
932 | GB_CSTR result = name; |
---|
933 | if (suffix) { |
---|
934 | while (suffix[0] == '.') suffix++; |
---|
935 | if (suffix[0]) { |
---|
936 | result = GBS_global_string_to_buffer(use_other_path_buf(), ARB_PATH_MAX, "%s.%s", name, suffix); |
---|
937 | } |
---|
938 | } |
---|
939 | return result; |
---|
940 | } |
---|
941 | |
---|
942 | GB_CSTR GB_canonical_path(const char *anypath) { |
---|
943 | // expands '~' '..' symbolic links etc in 'anypath'. |
---|
944 | // |
---|
945 | // Never returns NULL (if called correctly) |
---|
946 | // Instead might return non-canonical path (when a directory |
---|
947 | // in 'anypath' does not exist) |
---|
948 | |
---|
949 | GB_CSTR result = NULL; |
---|
950 | if (!anypath) { |
---|
951 | GB_export_error("NULL path (internal error)"); |
---|
952 | } |
---|
953 | else if (!anypath[0]) { |
---|
954 | result = "/"; |
---|
955 | } |
---|
956 | else if (strlen(anypath) >= ARB_PATH_MAX) { |
---|
957 | GB_export_errorf("Path too long (> %i chars)", ARB_PATH_MAX-1); |
---|
958 | } |
---|
959 | else { |
---|
960 | if (anypath[0] == '~' && (!anypath[1] || anypath[1] == '/')) { |
---|
961 | GB_CSTR home = GB_getenvHOME(); |
---|
962 | GB_CSTR homeexp = GBS_global_string("%s%s", home, anypath+1); |
---|
963 | result = GB_canonical_path(homeexp); |
---|
964 | GBS_reuse_buffer(homeexp); |
---|
965 | } |
---|
966 | else { |
---|
967 | result = realpath(anypath, path_buf[1-path_toggle]); |
---|
968 | if (result) { |
---|
969 | path_toggle = 1-path_toggle; |
---|
970 | } |
---|
971 | else { // realpath failed (happens e.g. when using a non-existing path, e.g. if user entered the name of a new file) |
---|
972 | // => content of path_buf[path_toggle] is UNDEFINED! |
---|
973 | char *dir, *fullname; |
---|
974 | GB_split_full_path(anypath, &dir, &fullname, NULL, NULL); |
---|
975 | |
---|
976 | const char *canonical_dir = NULL; |
---|
977 | if (!dir) { |
---|
978 | gb_assert(!strchr(anypath, '/')); |
---|
979 | canonical_dir = GB_canonical_path("."); // use working directory |
---|
980 | } |
---|
981 | else { |
---|
982 | gb_assert(strcmp(dir, anypath) != 0); // avoid deadlock |
---|
983 | canonical_dir = GB_canonical_path(dir); |
---|
984 | } |
---|
985 | gb_assert(canonical_dir); |
---|
986 | |
---|
987 | // manually resolve '.' and '..' in non-existing parent directories |
---|
988 | if (strcmp(fullname, "..") == 0) { |
---|
989 | char *parent; |
---|
990 | GB_split_full_path(canonical_dir, &parent, NULL, NULL, NULL); |
---|
991 | if (parent) { |
---|
992 | result = strcpy(use_other_path_buf(), parent); |
---|
993 | free(parent); |
---|
994 | } |
---|
995 | } |
---|
996 | else if (strcmp(fullname, ".") == 0) { |
---|
997 | result = canonical_dir; |
---|
998 | } |
---|
999 | |
---|
1000 | if (!result) result = GB_concat_path(canonical_dir, fullname); |
---|
1001 | |
---|
1002 | free(dir); |
---|
1003 | free(fullname); |
---|
1004 | } |
---|
1005 | } |
---|
1006 | gb_assert(result); |
---|
1007 | } |
---|
1008 | return result; |
---|
1009 | } |
---|
1010 | |
---|
1011 | GB_CSTR GB_concat_path(GB_CSTR anypath_left, GB_CSTR anypath_right) { |
---|
1012 | // concats left and right part of a path. |
---|
1013 | // '/' is inserted in-between |
---|
1014 | // |
---|
1015 | // if one of the arguments is NULL => returns the other argument |
---|
1016 | // if both arguments are NULL => return NULL (@@@ maybe forbid?) |
---|
1017 | |
---|
1018 | GB_CSTR result = NULL; |
---|
1019 | |
---|
1020 | if (anypath_right) { |
---|
1021 | if (anypath_right[0] == '/') { |
---|
1022 | result = GB_concat_path(anypath_left, anypath_right+1); |
---|
1023 | } |
---|
1024 | else if (anypath_left && anypath_left[0]) { |
---|
1025 | if (anypath_left[strlen(anypath_left)-1] == '/') { |
---|
1026 | result = GBS_global_string_to_buffer(use_other_path_buf(), sizeof(path_buf[0]), "%s%s", anypath_left, anypath_right); |
---|
1027 | } |
---|
1028 | else { |
---|
1029 | result = GBS_global_string_to_buffer(use_other_path_buf(), sizeof(path_buf[0]), "%s/%s", anypath_left, anypath_right); |
---|
1030 | } |
---|
1031 | } |
---|
1032 | else { |
---|
1033 | result = anypath_right; |
---|
1034 | } |
---|
1035 | } |
---|
1036 | else { |
---|
1037 | result = anypath_left; |
---|
1038 | } |
---|
1039 | |
---|
1040 | return result; |
---|
1041 | } |
---|
1042 | |
---|
1043 | GB_CSTR GB_concat_full_path(const char *anypath_left, const char *anypath_right) { |
---|
1044 | // like GB_concat_path(), but returns the canonical path |
---|
1045 | GB_CSTR result = GB_concat_path(anypath_left, anypath_right); |
---|
1046 | |
---|
1047 | gb_assert(result != anypath_left); // consider using GB_canonical_path() directly |
---|
1048 | gb_assert(result != anypath_right); |
---|
1049 | |
---|
1050 | if (result) result = GB_canonical_path(result); |
---|
1051 | return result; |
---|
1052 | } |
---|
1053 | |
---|
1054 | inline bool is_absolute_path(const char *path) { return path[0] == '/' || path[0] == '~'; } |
---|
1055 | inline bool is_name_of_envvar(const char *name) { |
---|
1056 | for (int i = 0; name[i]; ++i) { |
---|
1057 | if (isalnum(name[i]) || name[i] == '_') continue; |
---|
1058 | return false; |
---|
1059 | } |
---|
1060 | return true; |
---|
1061 | } |
---|
1062 | |
---|
1063 | GB_CSTR GB_unfold_in_directory(const char *relative_directory, const char *path) { |
---|
1064 | // If 'path' is an absolute path, return canonical path. |
---|
1065 | // |
---|
1066 | // Otherwise unfolds relative 'path' using 'relative_directory' as start directory. |
---|
1067 | |
---|
1068 | if (is_absolute_path(path)) return GB_canonical_path(path); |
---|
1069 | return GB_concat_full_path(relative_directory, path); |
---|
1070 | } |
---|
1071 | |
---|
1072 | GB_CSTR GB_unfold_path(const char *pwd_envar, const char *path) { |
---|
1073 | // If 'path' is an absolute path, return canonical path. |
---|
1074 | // |
---|
1075 | // Otherwise unfolds relative 'path' using content of environment |
---|
1076 | // variable 'pwd_envar' as start directory. |
---|
1077 | // If environment variable is not defined, fall-back to current directory |
---|
1078 | |
---|
1079 | gb_assert(is_name_of_envvar(pwd_envar)); |
---|
1080 | if (is_absolute_path(path)) { |
---|
1081 | return GB_canonical_path(path); |
---|
1082 | } |
---|
1083 | |
---|
1084 | const char *pwd = GB_getenv(pwd_envar); |
---|
1085 | if (!pwd) pwd = GB_getcwd(); // @@@ really wanted ? |
---|
1086 | return GB_concat_full_path(pwd, path); |
---|
1087 | } |
---|
1088 | |
---|
1089 | static GB_CSTR GB_path_in_ARBHOME(const char *relative_path_left, const char *anypath_right) { |
---|
1090 | return GB_path_in_ARBHOME(GB_concat_path(relative_path_left, anypath_right)); |
---|
1091 | } |
---|
1092 | |
---|
1093 | GB_CSTR GB_path_in_ARBHOME(const char *relative_path) { |
---|
1094 | return GB_unfold_path("ARBHOME", relative_path); |
---|
1095 | } |
---|
1096 | GB_CSTR GB_path_in_ARBLIB(const char *relative_path) { |
---|
1097 | return GB_path_in_ARBHOME("lib", relative_path); |
---|
1098 | } |
---|
1099 | GB_CSTR GB_path_in_HOME(const char *relative_path) { |
---|
1100 | return GB_unfold_path("HOME", relative_path); |
---|
1101 | } |
---|
1102 | GB_CSTR GB_path_in_arbprop(const char *relative_path) { |
---|
1103 | return GB_unfold_path("ARB_PROP", relative_path); |
---|
1104 | } |
---|
1105 | GB_CSTR GB_path_in_ARBLIB(const char *relative_path_left, const char *anypath_right) { |
---|
1106 | return GB_path_in_ARBLIB(GB_concat_path(relative_path_left, anypath_right)); |
---|
1107 | } |
---|
1108 | GB_CSTR GB_path_in_arb_temp(const char *relative_path) { |
---|
1109 | return GB_path_in_HOME(GB_concat_path(".arb_tmp", relative_path)); |
---|
1110 | } |
---|
1111 | |
---|
1112 | #define GB_PATH_TMP GB_path_in_arb_temp("tmp") // = "~/.arb_tmp/tmp" (used wherever '/tmp' was used in the past) |
---|
1113 | |
---|
1114 | FILE *GB_fopen_tempfile(const char *filename, const char *fmode, char **res_fullname) { |
---|
1115 | // fopens a tempfile |
---|
1116 | // |
---|
1117 | // Returns |
---|
1118 | // - NULL in case of error (which is exported then) |
---|
1119 | // - otherwise returns open filehandle |
---|
1120 | // |
---|
1121 | // Always sets |
---|
1122 | // - heap-copy of used filename in 'res_fullname' (if res_fullname != NULL) |
---|
1123 | // (even if fopen failed) |
---|
1124 | |
---|
1125 | char *file = ARB_strdup(GB_concat_path(GB_PATH_TMP, filename)); |
---|
1126 | GB_ERROR error = GB_create_parent_directory(file); |
---|
1127 | FILE *fp = NULL; |
---|
1128 | |
---|
1129 | if (!error) { |
---|
1130 | bool write = strpbrk(fmode, "wa") != 0; |
---|
1131 | |
---|
1132 | fp = fopen(file, fmode); |
---|
1133 | if (fp) { |
---|
1134 | // make file private |
---|
1135 | if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) != 0) { |
---|
1136 | error = GB_IO_error("changing permissions of", file); |
---|
1137 | } |
---|
1138 | } |
---|
1139 | else { |
---|
1140 | error = GB_IO_error(GBS_global_string("opening(%s) tempfile", write ? "write" : "read"), file); |
---|
1141 | } |
---|
1142 | |
---|
1143 | if (res_fullname) { |
---|
1144 | *res_fullname = file ? ARB_strdup(file) : 0; |
---|
1145 | } |
---|
1146 | } |
---|
1147 | |
---|
1148 | if (error) { |
---|
1149 | // don't care if anything fails here.. |
---|
1150 | if (fp) { fclose(fp); fp = 0; } |
---|
1151 | if (file) unlink(file); |
---|
1152 | GB_export_error(error); |
---|
1153 | } |
---|
1154 | |
---|
1155 | free(file); |
---|
1156 | |
---|
1157 | return fp; |
---|
1158 | } |
---|
1159 | |
---|
1160 | char *GB_create_tempfile(const char *name) { |
---|
1161 | // creates a tempfile and returns full name of created file |
---|
1162 | // returns NULL in case of error (which is exported then) |
---|
1163 | |
---|
1164 | char *fullname; |
---|
1165 | FILE *out = GB_fopen_tempfile(name, "wt", &fullname); |
---|
1166 | |
---|
1167 | if (out) fclose(out); |
---|
1168 | return fullname; |
---|
1169 | } |
---|
1170 | |
---|
1171 | char *GB_unique_filename(const char *name_prefix, const char *suffix) { |
---|
1172 | // generates a unique (enough) filename |
---|
1173 | // |
---|
1174 | // scheme: name_prefix_USER_PID_COUNT.suffix |
---|
1175 | |
---|
1176 | static int counter = 0; |
---|
1177 | return GBS_global_string_copy("%s_%s_%i_%i.%s", |
---|
1178 | name_prefix, |
---|
1179 | GB_getenvUSER(), getpid(), counter++, |
---|
1180 | suffix); |
---|
1181 | } |
---|
1182 | |
---|
1183 | static GB_HASH *files_to_remove_on_exit = 0; |
---|
1184 | static long exit_remove_file(const char *file, long, void *) { |
---|
1185 | if (unlink(file) != 0) { |
---|
1186 | fprintf(stderr, "Warning: %s\n", GB_IO_error("removing", file)); |
---|
1187 | } |
---|
1188 | return 0; |
---|
1189 | } |
---|
1190 | static void exit_removal() { |
---|
1191 | if (files_to_remove_on_exit) { |
---|
1192 | GBS_hash_do_loop(files_to_remove_on_exit, exit_remove_file, NULL); |
---|
1193 | GBS_free_hash(files_to_remove_on_exit); |
---|
1194 | files_to_remove_on_exit = NULL; |
---|
1195 | } |
---|
1196 | } |
---|
1197 | void GB_remove_on_exit(const char *filename) { |
---|
1198 | // mark a file for removal on exit |
---|
1199 | |
---|
1200 | if (!files_to_remove_on_exit) { |
---|
1201 | files_to_remove_on_exit = GBS_create_hash(20, GB_MIND_CASE); |
---|
1202 | GB_atexit(exit_removal); |
---|
1203 | } |
---|
1204 | GBS_write_hash(files_to_remove_on_exit, filename, 1); |
---|
1205 | } |
---|
1206 | |
---|
1207 | void GB_split_full_path(const char *fullpath, char **res_dir, char **res_fullname, char **res_name_only, char **res_suffix) { |
---|
1208 | // Takes a file (or directory) name and splits it into "path/name.suffix". |
---|
1209 | // If result pointers (res_*) are non-NULL, they are assigned heap-copies of the split parts. |
---|
1210 | // If parts are not valid (e.g. cause 'fullpath' doesn't have a .suffix) the corresponding result pointer |
---|
1211 | // is set to NULL. |
---|
1212 | // |
---|
1213 | // The '/' and '.' characters at the split-positions will be removed (not included in the results-strings). |
---|
1214 | // Exceptions: |
---|
1215 | // - the '.' in 'res_fullname' |
---|
1216 | // - the '/' if directory part is the rootdir |
---|
1217 | // |
---|
1218 | // Note: |
---|
1219 | // - if the filename starts with '.' (and that is the only '.' in the filename, an empty filename is returned: "") |
---|
1220 | |
---|
1221 | if (fullpath && fullpath[0]) { |
---|
1222 | const char *lslash = strrchr(fullpath, '/'); |
---|
1223 | const char *name_start = lslash ? lslash+1 : fullpath; |
---|
1224 | const char *ldot = strrchr(lslash ? lslash : fullpath, '.'); |
---|
1225 | const char *terminal = strchr(name_start, 0); |
---|
1226 | |
---|
1227 | gb_assert(terminal); |
---|
1228 | gb_assert(name_start); |
---|
1229 | gb_assert(terminal > fullpath); // ensure (terminal-1) is a valid character position in path |
---|
1230 | |
---|
1231 | if (!lslash && fullpath[0] == '.' && (fullpath[1] == 0 || (fullpath[1] == '.' && fullpath[2] == 0))) { // '.' and '..' |
---|
1232 | if (res_dir) *res_dir = ARB_strdup(fullpath); |
---|
1233 | if (res_fullname) *res_fullname = NULL; |
---|
1234 | if (res_name_only) *res_name_only = NULL; |
---|
1235 | if (res_suffix) *res_suffix = NULL; |
---|
1236 | } |
---|
1237 | else { |
---|
1238 | if (res_dir) *res_dir = lslash ? ARB_strpartdup(fullpath, lslash == fullpath ? lslash : lslash-1) : NULL; |
---|
1239 | if (res_fullname) *res_fullname = ARB_strpartdup(name_start, terminal-1); |
---|
1240 | if (res_name_only) *res_name_only = ARB_strpartdup(name_start, ldot ? ldot-1 : terminal-1); |
---|
1241 | if (res_suffix) *res_suffix = ldot ? ARB_strpartdup(ldot+1, terminal-1) : NULL; |
---|
1242 | } |
---|
1243 | } |
---|
1244 | else { |
---|
1245 | if (res_dir) *res_dir = NULL; |
---|
1246 | if (res_fullname) *res_fullname = NULL; |
---|
1247 | if (res_name_only) *res_name_only = NULL; |
---|
1248 | if (res_suffix) *res_suffix = NULL; |
---|
1249 | } |
---|
1250 | } |
---|
1251 | |
---|
1252 | |
---|
1253 | // -------------------------------------------------------------------------------- |
---|
1254 | |
---|
1255 | #ifdef UNIT_TESTS |
---|
1256 | |
---|
1257 | #include <test_unit.h> |
---|
1258 | |
---|
1259 | #define TEST_EXPECT_IS_CANONICAL(file) \ |
---|
1260 | do { \ |
---|
1261 | char *dup = ARB_strdup(file); \ |
---|
1262 | TEST_EXPECT_EQUAL(GB_canonical_path(dup), dup); \ |
---|
1263 | free(dup); \ |
---|
1264 | } while(0) |
---|
1265 | |
---|
1266 | #define TEST_EXPECT_CANONICAL_TO(not_cano,cano) \ |
---|
1267 | do { \ |
---|
1268 | char *arb_not_cano = ARB_strdup(GB_concat_path(arbhome, not_cano)); \ |
---|
1269 | char *arb_cano = ARB_strdup(GB_concat_path(arbhome, cano)); \ |
---|
1270 | TEST_EXPECT_EQUAL(GB_canonical_path(arb_not_cano), arb_cano); \ |
---|
1271 | free(arb_cano); \ |
---|
1272 | free(arb_not_cano); \ |
---|
1273 | } while (0) |
---|
1274 | |
---|
1275 | static arb_test::match_expectation path_splits_into(const char *path, const char *Edir, const char *Enameext, const char *Ename, const char *Eext) { |
---|
1276 | using namespace arb_test; |
---|
1277 | expectation_group expected; |
---|
1278 | |
---|
1279 | char *Sdir,*Snameext,*Sname,*Sext; |
---|
1280 | GB_split_full_path(path, &Sdir, &Snameext, &Sname, &Sext); |
---|
1281 | |
---|
1282 | expected.add(that(Sdir).is_equal_to(Edir)); |
---|
1283 | expected.add(that(Snameext).is_equal_to(Enameext)); |
---|
1284 | expected.add(that(Sname).is_equal_to(Ename)); |
---|
1285 | expected.add(that(Sext).is_equal_to(Eext)); |
---|
1286 | |
---|
1287 | free(Sdir); |
---|
1288 | free(Snameext); |
---|
1289 | free(Sname); |
---|
1290 | free(Sext); |
---|
1291 | |
---|
1292 | return all().ofgroup(expected); |
---|
1293 | } |
---|
1294 | |
---|
1295 | #define TEST_EXPECT_PATH_SPLITS_INTO(path,dir,nameext,name,ext) TEST_EXPECTATION(path_splits_into(path,dir,nameext,name,ext)) |
---|
1296 | #define TEST_EXPECT_PATH_SPLITS_INTO__BROKEN(path,dir,nameext,name,ext) TEST_EXPECTATION__BROKEN(path_splits_into(path,dir,nameext,name,ext)) |
---|
1297 | |
---|
1298 | static arb_test::match_expectation path_splits_reversible(const char *path) { |
---|
1299 | using namespace arb_test; |
---|
1300 | expectation_group expected; |
---|
1301 | |
---|
1302 | char *Sdir,*Snameext,*Sname,*Sext; |
---|
1303 | GB_split_full_path(path, &Sdir, &Snameext, &Sname, &Sext); |
---|
1304 | |
---|
1305 | expected.add(that(GB_append_suffix(Sname, Sext)).is_equal_to(Snameext)); // GB_append_suffix should reverse name.ext-split |
---|
1306 | expected.add(that(GB_concat_path(Sdir, Snameext)).is_equal_to(path)); // GB_concat_path should reverse dir/file-split |
---|
1307 | |
---|
1308 | free(Sdir); |
---|
1309 | free(Snameext); |
---|
1310 | free(Sname); |
---|
1311 | free(Sext); |
---|
1312 | |
---|
1313 | return all().ofgroup(expected); |
---|
1314 | } |
---|
1315 | |
---|
1316 | #define TEST_SPLIT_REVERSIBILITY(path) TEST_EXPECTATION(path_splits_reversible(path)) |
---|
1317 | #define TEST_SPLIT_REVERSIBILITY__BROKEN(path) TEST_EXPECTATION__BROKEN(path_splits_reversible(path)) |
---|
1318 | |
---|
1319 | void TEST_paths() { |
---|
1320 | // test GB_concat_path |
---|
1321 | TEST_EXPECT_EQUAL(GB_concat_path("a", NULL), "a"); |
---|
1322 | TEST_EXPECT_EQUAL(GB_concat_path(NULL, "b"), "b"); |
---|
1323 | TEST_EXPECT_EQUAL(GB_concat_path("a", "b"), "a/b"); |
---|
1324 | |
---|
1325 | TEST_EXPECT_EQUAL(GB_concat_path("/", "test.fig"), "/test.fig"); |
---|
1326 | |
---|
1327 | // test GB_split_full_path |
---|
1328 | TEST_EXPECT_PATH_SPLITS_INTO("dir/sub/.ext", "dir/sub", ".ext", "", "ext"); |
---|
1329 | TEST_EXPECT_PATH_SPLITS_INTO("/root/sub/file.notext.ext", "/root/sub", "file.notext.ext", "file.notext", "ext"); |
---|
1330 | |
---|
1331 | TEST_EXPECT_PATH_SPLITS_INTO("./file.ext", ".", "file.ext", "file", "ext"); |
---|
1332 | TEST_EXPECT_PATH_SPLITS_INTO("/file", "/", "file", "file", NULL); |
---|
1333 | TEST_EXPECT_PATH_SPLITS_INTO(".", ".", NULL, NULL, NULL); |
---|
1334 | |
---|
1335 | // test reversibility of GB_split_full_path and GB_concat_path/GB_append_suffix |
---|
1336 | { |
---|
1337 | const char *prefix[] = { |
---|
1338 | "", |
---|
1339 | "dir/", |
---|
1340 | "dir/sub/", |
---|
1341 | "/dir/", |
---|
1342 | "/dir/sub/", |
---|
1343 | "/", |
---|
1344 | "./", |
---|
1345 | "../", |
---|
1346 | }; |
---|
1347 | |
---|
1348 | for (size_t d = 0; d<ARRAY_ELEMS(prefix); ++d) { |
---|
1349 | TEST_ANNOTATE(GBS_global_string("prefix='%s'", prefix[d])); |
---|
1350 | |
---|
1351 | TEST_SPLIT_REVERSIBILITY(GBS_global_string("%sfile.ext", prefix[d])); |
---|
1352 | TEST_SPLIT_REVERSIBILITY(GBS_global_string("%sfile", prefix[d])); |
---|
1353 | TEST_SPLIT_REVERSIBILITY(GBS_global_string("%s.ext", prefix[d])); |
---|
1354 | if (prefix[d][0]) { // empty string "" reverts to NULL |
---|
1355 | TEST_SPLIT_REVERSIBILITY(prefix[d]); |
---|
1356 | } |
---|
1357 | } |
---|
1358 | } |
---|
1359 | |
---|
1360 | // GB_canonical_path basics |
---|
1361 | TEST_EXPECT_CONTAINS(GB_canonical_path("./bla"), "UNIT_TESTER/run/bla"); |
---|
1362 | TEST_EXPECT_CONTAINS(GB_canonical_path("bla"), "UNIT_TESTER/run/bla"); |
---|
1363 | |
---|
1364 | { |
---|
1365 | char *arbhome = ARB_strdup(GB_getenvARBHOME()); |
---|
1366 | const char* nosuchfile = "nosuchfile"; |
---|
1367 | const char* somefile = "arb_README.txt"; |
---|
1368 | |
---|
1369 | char *somefile_in_arbhome = ARB_strdup(GB_concat_path(arbhome, somefile)); |
---|
1370 | char *nosuchfile_in_arbhome = ARB_strdup(GB_concat_path(arbhome, nosuchfile)); |
---|
1371 | char *nosuchpath_in_arbhome = ARB_strdup(GB_concat_path(arbhome, "nosuchpath")); |
---|
1372 | char *somepath_in_arbhome = ARB_strdup(GB_concat_path(arbhome, "lib")); |
---|
1373 | char *file_in_nosuchpath = ARB_strdup(GB_concat_path(nosuchpath_in_arbhome, "whatever")); |
---|
1374 | |
---|
1375 | TEST_REJECT(GB_is_directory(nosuchpath_in_arbhome)); |
---|
1376 | |
---|
1377 | // test GB_get_full_path |
---|
1378 | TEST_EXPECT_IS_CANONICAL(somefile_in_arbhome); |
---|
1379 | TEST_EXPECT_IS_CANONICAL(nosuchpath_in_arbhome); |
---|
1380 | TEST_EXPECT_IS_CANONICAL(file_in_nosuchpath); |
---|
1381 | |
---|
1382 | TEST_EXPECT_IS_CANONICAL("/sbin"); // existing (most likely) |
---|
1383 | #if !defined(DARWIN) |
---|
1384 | // TEST_DISABLED_OSX: fails for darwin on jenkins (/tmp seems to be a symbolic link there) |
---|
1385 | TEST_EXPECT_IS_CANONICAL("/tmp/arbtest.fig"); |
---|
1386 | #endif |
---|
1387 | TEST_EXPECT_IS_CANONICAL("/arbtest.fig"); // not existing (most likely) |
---|
1388 | |
---|
1389 | TEST_EXPECT_CANONICAL_TO("./PARSIMONY/./../ARBDB/./arbdb.h", "ARBDB/arbdb.h"); // test parent-path |
---|
1390 | TEST_EXPECT_CANONICAL_TO("INCLUDE/arbdb.h", "ARBDB/arbdb.h"); // test symbolic link to file |
---|
1391 | TEST_EXPECT_CANONICAL_TO("NAMES_COM/AISC/aisc.pa", "AISC_COM/AISC/aisc.pa"); // test symbolic link to directory |
---|
1392 | TEST_EXPECT_CANONICAL_TO("./NAMES_COM/AISC/..", "AISC_COM"); // test parent-path through links |
---|
1393 | |
---|
1394 | TEST_EXPECT_CANONICAL_TO("./PARSIMONY/./../ARBDB/../nosuchpath", "nosuchpath"); // nosuchpath does not exist, but involved parent dirs do |
---|
1395 | // test resolving of non-existent parent dirs: |
---|
1396 | TEST_EXPECT_CANONICAL_TO("./PARSIMONY/./../nosuchpath/../ARBDB", "ARBDB"); |
---|
1397 | TEST_EXPECT_CANONICAL_TO("./nosuchpath/./../ARBDB", "ARBDB"); |
---|
1398 | |
---|
1399 | // test GB_unfold_path |
---|
1400 | TEST_EXPECT_EQUAL(GB_unfold_path("ARBHOME", somefile), somefile_in_arbhome); |
---|
1401 | TEST_EXPECT_EQUAL(GB_unfold_path("ARBHOME", nosuchfile), nosuchfile_in_arbhome); |
---|
1402 | |
---|
1403 | char *inhome = ARB_strdup(GB_unfold_path("HOME", "whatever")); |
---|
1404 | TEST_EXPECT_EQUAL(inhome, GB_canonical_path("~/whatever")); |
---|
1405 | free(inhome); |
---|
1406 | |
---|
1407 | // test GB_unfold_in_directory |
---|
1408 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(arbhome, somefile), somefile_in_arbhome); |
---|
1409 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(nosuchpath_in_arbhome, somefile_in_arbhome), somefile_in_arbhome); |
---|
1410 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(arbhome, nosuchfile), nosuchfile_in_arbhome); |
---|
1411 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(nosuchpath_in_arbhome, "whatever"), file_in_nosuchpath); |
---|
1412 | TEST_EXPECT_EQUAL(GB_unfold_in_directory(somepath_in_arbhome, "../nosuchfile"), nosuchfile_in_arbhome); |
---|
1413 | |
---|
1414 | // test unfolding absolute paths (HOME is ignored) |
---|
1415 | TEST_EXPECT_EQUAL(GB_unfold_path("HOME", arbhome), arbhome); |
---|
1416 | TEST_EXPECT_EQUAL(GB_unfold_path("HOME", somefile_in_arbhome), somefile_in_arbhome); |
---|
1417 | TEST_EXPECT_EQUAL(GB_unfold_path("HOME", nosuchfile_in_arbhome), nosuchfile_in_arbhome); |
---|
1418 | |
---|
1419 | // test GB_path_in_ARBHOME |
---|
1420 | TEST_EXPECT_EQUAL(GB_path_in_ARBHOME(somefile), somefile_in_arbhome); |
---|
1421 | TEST_EXPECT_EQUAL(GB_path_in_ARBHOME(nosuchfile), nosuchfile_in_arbhome); |
---|
1422 | |
---|
1423 | free(file_in_nosuchpath); |
---|
1424 | free(somepath_in_arbhome); |
---|
1425 | free(nosuchpath_in_arbhome); |
---|
1426 | free(nosuchfile_in_arbhome); |
---|
1427 | free(somefile_in_arbhome); |
---|
1428 | free(arbhome); |
---|
1429 | } |
---|
1430 | |
---|
1431 | TEST_EXPECT_EQUAL(GB_path_in_ARBLIB("help"), GB_path_in_ARBHOME("lib", "help")); |
---|
1432 | |
---|
1433 | } |
---|
1434 | |
---|
1435 | // ---------------------------------------- |
---|
1436 | |
---|
1437 | class TestFile : virtual Noncopyable { |
---|
1438 | const char *name; |
---|
1439 | bool open(const char *mode) { |
---|
1440 | FILE *out = fopen(name, mode); |
---|
1441 | if (out) fclose(out); |
---|
1442 | return out; |
---|
1443 | } |
---|
1444 | void create() { ASSERT_RESULT(bool, true, open("w")); } |
---|
1445 | void unlink() { ::unlink(name); } |
---|
1446 | public: |
---|
1447 | TestFile(const char *name_) : name(name_) { create(); } |
---|
1448 | ~TestFile() { if (exists()) unlink(); } |
---|
1449 | const char *get_name() const { return name; } |
---|
1450 | bool exists() { return open("r"); } |
---|
1451 | }; |
---|
1452 | |
---|
1453 | void TEST_GB_remove_on_exit() { |
---|
1454 | { |
---|
1455 | // first test class TestFile |
---|
1456 | TestFile file("test1"); |
---|
1457 | TEST_EXPECT(file.exists()); |
---|
1458 | TEST_EXPECT(TestFile(file.get_name()).exists()); // removes the file |
---|
1459 | TEST_REJECT(file.exists()); |
---|
1460 | } |
---|
1461 | |
---|
1462 | TestFile t("test1"); |
---|
1463 | { |
---|
1464 | GB_shell shell; |
---|
1465 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
1466 | |
---|
1467 | GB_remove_on_exit(t.get_name()); |
---|
1468 | GB_close(gb_main); |
---|
1469 | } |
---|
1470 | TEST_REJECT(t.exists()); |
---|
1471 | } |
---|
1472 | |
---|
1473 | void TEST_some_paths() { |
---|
1474 | gb_getenv_hook old = GB_install_getenv_hook(arb_test::fakeenv); |
---|
1475 | { |
---|
1476 | // ../UNIT_TESTER/run/homefake |
---|
1477 | |
---|
1478 | TEST_EXPECT_CONTAINS__BROKEN(GB_getenvHOME(), "/UNIT_TESTER/run/homefake"); // GB_getenvHOME() ignores the hook |
---|
1479 | // @@@ this is a general problem - unit tested code cannot use GB_getenvHOME() w/o problems |
---|
1480 | |
---|
1481 | TEST_EXPECT_CONTAINS(GB_getenvARB_PROP(), "/UNIT_TESTER/run/homefake/.arb_prop"); |
---|
1482 | TEST_EXPECT_CONTAINS(GB_getenvARBMACRO(), "/lib/macros"); |
---|
1483 | |
---|
1484 | TEST_EXPECT_CONTAINS(GB_getenvARBCONFIG(), "/UNIT_TESTER/run/homefake/.arb_prop/cfgSave"); |
---|
1485 | TEST_EXPECT_CONTAINS(GB_getenvARBMACROHOME(), "/UNIT_TESTER/run/homefake/.arb_prop/macros"); // works in [11068] |
---|
1486 | } |
---|
1487 | TEST_EXPECT_EQUAL((void*)arb_test::fakeenv, (void*)GB_install_getenv_hook(old)); |
---|
1488 | } |
---|
1489 | |
---|
1490 | #endif // UNIT_TESTS |
---|
1491 | |
---|