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