summarylogtreecommitdiffstats
path: root/01-relocate.patch
blob: 66587a96f4b7f2ee62579c7b8aeeb6381031c1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
diff --git a/.gitignore b/.gitignore
index 04cdd34..5ede546 100644
--- a/.gitignore
+++ b/.gitignore
@@ -51,7 +51,8 @@ Makefile
 /config.status
 /hunspell.pc
 /libtool
-
+build/*
+.vscode/*
 
 #make
 .deps/
diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
index 4c29f1a..cc25476 100644
--- a/src/tools/Makefile.am
+++ b/src/tools/Makefile.am
@@ -12,7 +12,10 @@ unmunch_SOURCES=unmunch.cxx unmunch.h
 example_SOURCES=example.cxx
 example_LDADD = ../hunspell/libhunspell-1.6.la
 
-hunspell_SOURCES=hunspell.cxx
+hunspell_CXXFLAGS = \
+  -DBINDIR=\""$(bindir)"\" \
+  -DDATA_DIR=\""$(datadir)"\"
+hunspell_SOURCES=hunspell.cxx pathtools.cxx pathtools.hxx
 hunspell_LDADD = @LIBINTL@ @LIBICONV@ ../parsers/libparsers.a \
 	../hunspell/libhunspell-1.6.la @CURSESLIB@ @READLINELIB@
 
diff --git a/src/tools/hunspell.cxx b/src/tools/hunspell.cxx
index 399df86..0599794 100644
--- a/src/tools/hunspell.cxx
+++ b/src/tools/hunspell.cxx
@@ -51,6 +51,7 @@
 #include "hunspell.hxx"
 #include "csutil.hxx"
 #include "hunzip.hxx"
+#include "pathtools.hxx"
 
 #define HUNSPELL_VERSION VERSION
 #define INPUTLEN 50
@@ -70,6 +71,16 @@
 
 #ifdef WIN32
 
+#ifdef __MINGW32__
+#define LIBDIR \
+    DATA_DIR "/hunspell:" \
+    DATA_DIR "/myspell:" \
+    DATA_DIR "/myspell/dicts:"
+#define OOODIR \
+    DATA_DIR "/dict/ooo"
+#define USEROOODIR {"/Application Data/OpenOffice.org 2/user/wordbook"}
+#define HOME "%USERPROFILE%"
+#else
 #define LIBDIR "C:\\Hunspell\\"
 #define USEROOODIR { "Application Data\\OpenOffice.org 2\\user\\wordbook" }
 #define OOODIR                                                 \
@@ -79,10 +90,16 @@
   "C:\\Program files\\OpenOffice.org 2.1\\share\\dict\\ooo\\;" \
   "C:\\Program files\\OpenOffice.org 2.0\\share\\dict\\ooo\\"
 #define HOME "%USERPROFILE%\\"
+#endif
 #define DICBASENAME "hunspell_"
 #define LOGFILE "C:\\Hunspell\\log"
+#ifdef __MINGW32__
+#define DIRSEPCH '/'
+#define DIRSEP "/"
+#else
 #define DIRSEPCH '\\'
 #define DIRSEP "\\"
+#endif
 #define PATHSEP ";"
 
 #ifdef __MINGW32__
@@ -172,7 +189,12 @@ char text_conv[MAXLNLEN];
 
 #ifdef HAVE_CURSES_H
 #ifdef HAVE_NCURSESW_H
+#ifdef __MINGW32__
+#include <ncurses/ncurses.h>
+#else
 #include <ncurses.h>
+#endif //__MINGW32__
+
 #else
 #include <curses.h>
 #endif
@@ -2042,11 +2064,20 @@ int main(int argc, char** argv) {
   {
     std::string path_std_str = ".";
     path_std_str.append(PATHSEP); // <- check path in local directory
+#ifndef __MINGW32__
     path_std_str.append(PATHSEP); // <- check path in root directory
+#else
+    path_std_str.append(single_path_relocation(BINDIR, BINDIR)).append(PATHSEP);
+#endif    
     if (getenv("DICPATH")) {
       path_std_str.append(getenv("DICPATH")).append(PATHSEP);
     }
+#ifndef __MINGW32__
     path_std_str.append(LIBDIR).append(PATHSEP);
+#else
+  path_std_str.append(pathlist_relocation(BINDIR, LIBDIR)).append(PATHSEP);
+#endif
+
     if (HOME) {
       const char * userooodir[] = USEROOODIR;
       for(size_t i = 0; i < sizeof(userooodir)/sizeof(userooodir[0]); ++i) {
@@ -2056,7 +2087,11 @@ int main(int argc, char** argv) {
 #endif
         path_std_str.append(userooodir[i]).append(PATHSEP);
       }
+#ifndef __MINGW32__
       path_std_str.append(OOODIR);
+#else
+      path_std_str.append(single_path_relocation(BINDIR, OOODIR)).append(PATHSEP);
+#endif 
     }
     path = mystrdup(path_std_str.c_str());
   }
@@ -2152,6 +2187,7 @@ int main(int argc, char** argv) {
   }
 
   if (arg_files == -1) {
+    fflush(stderr);
     pipe_interface(pMS, format, stdin, NULL);
   } else if (filter_mode != NORMAL) {
     for (int i = arg_files; i < argc; i++) {
diff --git a/src/tools/pathtools.cxx b/src/tools/pathtools.cxx
new file mode 100644
index 0000000..d8ba8cf
--- /dev/null
+++ b/src/tools/pathtools.cxx
@@ -0,0 +1,538 @@
+/*
+      .Some useful path tools.
+        .ASCII only for now.
+   .Written by Ray Donnelly in 2014.
+   .Licensed under CC0 (and anything.
+  .else you need to license it under).
+      .No warranties whatsoever.
+  .email: <mingw.android@gmail.com>.
+ */
+
+#if defined(__APPLE__)
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__)
+#include <alloca.h>
+#endif
+#include <unistd.h>
+
+/* If you don't define this, then get_executable_path()
+   can only use argv[0] which will often not work well */
+#define IMPLEMENT_SYS_GET_EXECUTABLE_PATH
+
+#if defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH)
+#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__)
+/* Nothing needed, unistd.h is enough. */
+#elif defined(__APPLE__)
+#include <mach-o/dyld.h>
+#elif defined(_WIN32)
+#define WIN32_MEAN_AND_LEAN
+#include <windows.h>
+#include <psapi.h>
+#endif
+#endif /* defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) */
+
+#include "pathtools.hxx"
+
+char *
+malloc_copy_string(char const * original)
+{
+  char * result = (char *) malloc (sizeof (char*) * strlen (original)+1);
+  if (result != NULL)
+  {
+    strcpy (result, original);
+  }
+  return result;
+}
+
+void
+sanitise_path(char * path)
+{
+  size_t path_size = strlen (path);
+
+  /* Replace any '\' with '/' */
+  char * path_p = path;
+  while ((path_p = strchr (path_p, '\\')) != NULL)
+  {
+    *path_p = '/';
+  }
+  /* Replace any '//' with '/' */
+  path_p = path;
+  while ((path_p = strstr (path_p, "//")) != NULL)
+  {
+    memmove (path_p, path_p + 1, path_size--);
+  }
+  return;
+}
+
+char *
+get_relative_path(char const * from_in, char const * to_in)
+{
+  size_t from_size = (from_in == NULL) ? 0 : strlen (from_in);
+  size_t to_size = (to_in == NULL) ? 0 : strlen (to_in);
+  size_t max_size = (from_size + to_size) * 2 + 4;
+  char * scratch_space = (char *) alloca (from_size + 1 + to_size + 1 + max_size + max_size);
+  char * from;
+  char * to;
+  char * common_part;
+  char * result;
+  size_t count;
+
+  /* No to, return "./" */
+  if (to_in == NULL)
+  {
+    return malloc_copy_string ("./");
+  }
+
+  /* If alloca failed or no from was given return a copy of to */
+  if (   from_in == NULL
+      || scratch_space == NULL )
+  {
+    return malloc_copy_string (to_in);
+  }
+
+  from = scratch_space;
+  strcpy (from, from_in);
+  to = from + from_size + 1;
+  strcpy (to, to_in);
+  common_part = to + to_size + 1;
+  result = common_part + max_size;
+  simplify_path (from);
+  simplify_path (to);
+
+  result[0] = '\0';
+
+  size_t match_size_dirsep = 0;  /* The match size up to the last /. Always wind back to this - 1 */
+  size_t match_size = 0;         /* The running (and final) match size. */
+  size_t largest_size = (from_size > to_size) ? from_size : to_size;
+  int to_final_is_slash = (to[to_size-1] == '/') ? 1 : 0;
+  char from_c;
+  char to_c;
+  for (match_size = 0; match_size < largest_size; ++match_size)
+  {
+    /* To simplify the logic, always pretend the strings end with '/' */
+    from_c = (match_size < from_size) ? from[match_size] : '/';
+    to_c =   (match_size <   to_size) ?   to[match_size] : '/';
+
+    if (from_c != to_c)
+    {
+      if (from_c != '\0' || to_c != '\0')
+      {
+        match_size = match_size_dirsep;
+      }
+      break;
+    }
+    else if (from_c == '/')
+    {
+      match_size_dirsep = match_size;
+    }
+  }
+  strncpy (common_part, from, match_size);
+  common_part[match_size] = '\0';
+  from += match_size;
+  to += match_size;
+  size_t ndotdots = 0;
+  char const* from_last = from + strlen(from) - 1;
+  while ((from = strchr (from, '/')) && from != from_last)
+  {
+    ++ndotdots;
+    ++from;
+  }
+  for (count = 0; count < ndotdots; ++count)
+  {
+    strcat(result, "../");
+  }
+  if (strlen(to) > 0)
+  {
+    strcat(result, to+1);
+  }
+  /* Make sure that if to ends with '/' result does the same, and
+     vice-versa. */
+  size_t size_result = strlen(result);
+  if ((to_final_is_slash == 1)
+      && (!size_result || result[size_result-1] != '/'))
+  {
+    strcat (result, "/");
+  }
+  else if (!to_final_is_slash
+           && size_result && result[size_result-1] == '/')
+  {
+    result[size_result-1] = '\0';
+  }
+
+  return malloc_copy_string (result);
+}
+
+void
+simplify_path(char * path)
+{
+  ssize_t n_toks = 1; /* in-case we need an empty initial token. */
+  ssize_t i, j;
+  size_t tok_size;
+  size_t in_size = strlen (path);
+  int it_ended_with_a_slash = (path[in_size - 1] == '/') ? 1 : 0;
+  char * result = path;
+  sanitise_path(result);
+  char * result_p = result;
+
+  do
+  {
+    ++n_toks;
+    ++result_p;
+  } while ((result_p = strchr (result_p, '/')) != NULL);
+
+  result_p = result;
+  char const ** toks = (char const **) alloca (sizeof (char const*) * n_toks);
+  n_toks = 0;
+  do
+  {
+    if (result_p > result)
+    {
+      *result_p++ = '\0';
+    }
+    else if (*result_p == '/')
+    {
+      /* A leading / creates an empty initial token. */
+      toks[n_toks++] = result_p;
+      *result_p++ = '\0';
+    }
+    toks[n_toks++] = result_p;
+  } while ((result_p = strchr (result_p, '/')) != NULL);
+
+  /* Remove all non-leading '.' and any '..' we can match
+     with an earlier forward path (i.e. neither '.' nor '..') */
+  for (i = 1; i < n_toks; ++i)
+  {
+    int removals[2] = { -1, -1 };
+    if ( strcmp (toks[i], "." ) == 0)
+    {
+      removals[0] = i;
+    }
+    else if ( strcmp (toks[i], ".." ) == 0)
+    {
+      /* Search backwards for a forward path to collapse.
+         If none are found then the .. also stays. */
+      for (j = i - 1; j > -1; --j)
+      {
+        if ( strcmp (toks[j], "." )
+          && strcmp (toks[j], ".." ) )
+        {
+          removals[0] = j;
+          removals[1] = i;
+          break;
+        }
+      }
+    }
+    for (j = 0; j < 2; ++j)
+    {
+      if (removals[j] >= 0) /* Can become -2 */
+      {
+        --n_toks;
+        memmove (&toks[removals[j]], &toks[removals[j]+1], (n_toks - removals[j])*sizeof (char*));
+        --i;
+        if (!j)
+        {
+          --removals[1];
+        }
+      }
+    }
+  }
+  result_p = result;
+  for (i = 0; i < n_toks; ++i)
+  {
+    tok_size = strlen(toks[i]);
+    memcpy (result_p, toks[i], tok_size);
+    result_p += tok_size;
+    if ((!i || tok_size) && ((i < n_toks - 1) || it_ended_with_a_slash == 1))
+    {
+      *result_p = '/';
+      ++result_p;
+    }
+  }
+  *result_p = '\0';
+}
+
+/* Returns actual_to by calculating the relative path from -> to and
+   applying that to actual_from. An assumption that actual_from is a
+   dir is made, and it may or may not end with a '/' */
+char const *
+get_relocated_path (char const * from, char const * to, char const * actual_from)
+{
+  char const * relative_from_to = get_relative_path (from, to);
+  char * actual_to = (char *) malloc (strlen(actual_from) + 2 + strlen(relative_from_to));
+  return actual_to;
+}
+
+int
+get_executable_path(char const * argv0, char * result, ssize_t max_size)
+{
+  char * system_result = (char *) alloca (max_size);
+  ssize_t system_result_size = -1;
+  ssize_t result_size = -1;
+
+  if (system_result != NULL)
+  {
+#if defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH)
+#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__)
+    system_result_size = readlink("/proc/self/exe", system_result, max_size);
+#elif defined(__APPLE__)
+    uint32_t bufsize = (uint32_t)max_size;
+    if (_NSGetExecutablePath(system_result, &bufsize) == 0)
+    {
+      system_result_size = (ssize_t)bufsize;
+    }
+#elif defined(_WIN32)
+    unsigned long bufsize = (unsigned long)max_size;
+    system_result_size = GetModuleFileNameA(NULL, system_result, bufsize);
+    if (system_result_size == 0 || system_result_size == (ssize_t)bufsize)
+    {
+      /* Error, possibly not enough space. */
+      system_result_size = -1;
+    }
+    else
+    {
+      /* Early conversion to unix slashes instead of more changes
+         everywhere else .. */
+      char * winslash;
+      system_result[system_result_size] = '\0';
+      while ((winslash = strchr (system_result, '\\')) != NULL)
+      {
+        *winslash = '/';
+      }
+    }
+#else
+#warning "Don't know how to get executable path on this system"
+#endif
+#endif /* defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) */
+  }
+  /* Use argv0 as a default in-case of failure */
+  if (system_result_size != -1)
+  {
+    strncpy (result, system_result, system_result_size);
+    result[system_result_size] = '\0';
+  }
+  else
+  {
+    if (argv0 != NULL)
+    {
+      strncpy (result, argv0, max_size);
+      result[max_size-1] = '\0';
+    }
+    else
+    {
+      result[0] = '\0';
+    }
+  }
+  result_size = strlen (result);
+  return result_size;
+}
+
+char const *
+strip_n_prefix_folders(char const * path, size_t n)
+{
+  if (path == NULL)
+  {
+    return NULL;
+  }
+
+  if (path[0] != '/')
+  {
+    return path;
+  }
+
+  char const * last = path;
+  while (n-- && path != NULL)
+  {
+    last = path;
+    path = strchr (path + 1, '/');
+  }
+  return (path == NULL) ? last : path;
+}
+
+void
+strip_n_suffix_folders(char * path, size_t n)
+{
+  if (path == NULL)
+  {
+    return;
+  }
+  while (n--)
+  {
+    if (strrchr (path + 1, '/'))
+    {
+      *strrchr (path + 1, '/') = '\0';
+    }
+    else
+    {
+      return;
+    }
+  }
+  return;
+}
+
+size_t
+split_path_list(char const * path_list, char split_char, char *** arr)
+{
+  size_t path_count;
+  size_t path_list_size;
+  char const * path_list_p;
+
+  path_list_p = path_list;
+  if (path_list == NULL || path_list[0] == '\0')
+  {
+    return 0;
+  }
+  path_list_size = strlen (path_list);
+
+  path_count = 0;
+  do
+  {
+    ++path_count;
+    ++path_list_p;
+  }
+  while ((path_list_p = strchr (path_list_p, split_char)) != NULL);
+
+  /* allocate everything in one go. */
+  char * all_memory = (char *) malloc (sizeof (char *) * path_count + strlen(path_list) + 1);
+  if (all_memory == NULL)
+    return 0;
+  *arr = (char **)all_memory;
+  all_memory += sizeof (char *) * path_count;
+
+  path_count = 0;
+  path_list_p = path_list;
+  char const * next_path_list_p = 0;
+  do
+  {
+    next_path_list_p = strchr (path_list_p, split_char);
+    if (next_path_list_p != NULL)
+    {
+      ++next_path_list_p;
+    }
+    size_t this_size = (next_path_list_p != NULL)
+                       ? next_path_list_p - path_list_p - 1
+                       : &path_list[path_list_size] - path_list_p;
+    memcpy (all_memory, path_list_p, this_size);
+    all_memory[this_size] = '\0';
+    (*arr)[path_count++] = all_memory;
+    all_memory += this_size + 1;
+  } while ((path_list_p = next_path_list_p) != NULL);
+
+  return path_count;
+}
+
+char *
+get_relocated_path_list(char const * from, char const * to_path_list)
+{
+  char exe_path[MAX_PATH];
+  char * temp;
+  get_executable_path (NULL, &exe_path[0], sizeof (exe_path) / sizeof (exe_path[0]));
+  if ((temp = strrchr (exe_path, '/')) != NULL)
+  {
+    temp[1] = '\0';
+  }
+
+  char **arr = NULL;
+  /* Ask Alexey why he added this. Are we not 100% sure
+     that we're dealing with unix paths here? */
+  char split_char = ':';
+  if (strchr (to_path_list, ';'))
+  {
+    split_char = ';';
+  }
+  size_t count = split_path_list (to_path_list, split_char, &arr);
+  int result_size = 1 + (count - 1); /* count - 1 is for ; delim. */
+  size_t exe_path_size = strlen (exe_path);
+  size_t i;
+  /* Space required is:
+     count * (exe_path_size + strlen (rel_to_datadir))
+     rel_to_datadir upper bound is:
+     (count * strlen (from)) + (3 * num_slashes (from))
+     + strlen(arr[i]) + 1.
+     .. pathalogically num_slashes (from) is strlen (from)
+     (from = ////////) */
+  size_t space_required = (count * (exe_path_size + 4 * strlen (from))) + count - 1;
+  for (i = 0; i < count; ++i)
+  {
+    space_required += strlen (arr[i]);
+  }
+  char * scratch = (char *) alloca (space_required);
+  if (scratch == NULL)
+    return NULL;
+  for (i = 0; i < count; ++i)
+  {
+    char * rel_to_datadir = get_relative_path (from, arr[i]);
+    scratch[0] = '\0';
+    arr[i] = scratch;
+    strcat (scratch, exe_path);
+    strcat (scratch, rel_to_datadir);
+    simplify_path (arr[i]);
+    size_t arr_i_size = strlen (arr[i]);
+    result_size += arr_i_size;
+    scratch = arr[i] + arr_i_size + 1;
+  }
+  char * result = (char *) malloc (result_size);
+  if (result == NULL)
+  {
+    return NULL;
+  }
+  result[0] = '\0';
+  for (i = 0; i < count; ++i)
+  {
+    strcat (result, arr[i]);
+    if (i != count-1)
+    {
+#if defined(_WIN32)
+      strcat (result, ";");
+#else
+      strcat (result, ":");
+#endif
+    }
+  }
+  free ((void*)arr);
+  return result;
+}
+
+char *
+single_path_relocation(const char *from, const char *to)
+{
+#if defined(__MINGW32__)
+  char exe_path[PATH_MAX];
+  get_executable_path (NULL, &exe_path[0], sizeof(exe_path)/sizeof(exe_path[0]));
+  if (strrchr (exe_path, '/') != NULL)
+  {
+     strrchr (exe_path, '/')[1] = '\0';
+  }
+  char * rel_to_datadir = get_relative_path (from, to);
+  strcat (exe_path, rel_to_datadir);
+  simplify_path (&exe_path[0]);
+  return malloc_copy_string(exe_path);
+#else
+  return malloc_copy_string(to);
+#endif
+}
+
+char *
+pathlist_relocation(const char *from_path, const char *to_path_list)
+{
+#if defined(__MINGW32__)
+  static char stored_path[PATH_MAX];
+  static int stored = 0;
+  if (stored == 0)
+  {
+    char const * relocated = get_relocated_path_list(from_path, to_path_list);
+    strncpy (stored_path, relocated, PATH_MAX);
+    stored_path[PATH_MAX-1] = '\0';
+    free ((void *)relocated);
+    stored = 1;
+  }
+  return stored_path;
+#else
+  return (to_path_list);
+#endif
+}
diff --git a/src/tools/pathtools.hxx b/src/tools/pathtools.hxx
new file mode 100644
index 0000000..ab2402f
--- /dev/null
+++ b/src/tools/pathtools.hxx
@@ -0,0 +1,106 @@
+/*
+      .Some useful path tools.
+        .ASCII only for now.
+   .Written by Ray Donnelly in 2014.
+   .Licensed under CC0 (and anything.
+  .else you need to license it under).
+      .No warranties whatsoever.
+  .email: <mingw.android@gmail.com>.
+ */
+
+#ifndef PATHTOOLS_H
+#define PATHTOOLS_H
+
+#include <unistd.h>
+#if defined(__APPLE__)
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif
+#include <stdio.h>
+
+char * malloc_copy_string(char const * original);
+
+/* In-place replaces any '\' with '/' and any '//' with '/' */
+void sanitise_path(char * path);
+
+/* Uses a host OS specific function to determine the path of the executable,
+   if IMPLEMENT_SYS_GET_EXECUTABLE_PATH is defined, otherwise uses argv0. */
+int get_executable_path(char const * argv0, char * result, ssize_t max_size);
+
+/* Where possible, in-place removes occourances of '.' and 'path/..' */
+void simplify_path(char * path);
+
+/* Allocates (via malloc) and returns the path to get from from to to. */
+char * get_relative_path(char const * from, char const * to);
+
+size_t split_path_list(char const * path_list, char split_char, char *** arr);
+
+/* Advances path along by the amount that removes n prefix folders. */
+char const *
+strip_n_prefix_folders(char const * path, size_t n);
+
+/* NULL terminates path to remove n suffix folders. */
+void
+strip_n_suffix_folders(char * path, size_t n);
+
+char const * get_relocated_path (char const * from, char const * to, char const * actual_from);
+char * get_relocated_path_list(char const * from, char const * to_path_list);
+
+char * single_path_relocation(const char *from, const char *to);
+char * pathlist_relocation(const char *from_path, const char *to_path_list);
+
+#endif /* PATHTOOLS_H */
+/*
+      .Some useful path tools.
+        .ASCII only for now.
+   .Written by Ray Donnelly in 2014.
+   .Licensed under CC0 (and anything.
+  .else you need to license it under).
+      .No warranties whatsoever.
+  .email: <mingw.android@gmail.com>.
+ */
+
+#ifndef PATHTOOLS_H
+#define PATHTOOLS_H
+
+#include <unistd.h>
+#if defined(__APPLE__)
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif
+#include <stdio.h>
+
+char * malloc_copy_string(char const * original);
+
+/* In-place replaces any '\' with '/' and any '//' with '/' */
+void sanitise_path(char * path);
+
+/* Uses a host OS specific function to determine the path of the executable,
+   if IMPLEMENT_SYS_GET_EXECUTABLE_PATH is defined, otherwise uses argv0. */
+int get_executable_path(char const * argv0, char * result, ssize_t max_size);
+
+/* Where possible, in-place removes occourances of '.' and 'path/..' */
+void simplify_path(char * path);
+
+/* Allocates (via malloc) and returns the path to get from from to to. */
+char * get_relative_path(char const * from, char const * to);
+
+size_t split_path_list(char const * path_list, char split_char, char *** arr);
+
+/* Advances path along by the amount that removes n prefix folders. */
+char const *
+strip_n_prefix_folders(char const * path, size_t n);
+
+/* NULL terminates path to remove n suffix folders. */
+void
+strip_n_suffix_folders(char * path, size_t n);
+
+char const * get_relocated_path (char const * from, char const * to, char const * actual_from);
+char * get_relocated_path_list(char const * from, char const * to_path_list);
+
+char * single_path_relocation(const char *from, const char *to);
+char * pathlist_relocation(const char *from_path, const char *to_path_list);
+
+#endif /* PATHTOOLS_H */