|
1 /* |
|
2 * libtu/prefix.c |
|
3 * |
|
4 * Copyright (c) Tuomo Valkonen 1999-2007. |
|
5 * |
|
6 * You may distribute and modify this library under the terms of either |
|
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. |
|
8 */ |
|
9 |
|
10 #include <string.h> |
|
11 #include "misc.h" |
|
12 #include "locale.h" |
|
13 #include "output.h" |
|
14 |
|
15 static char *the_prefix=NULL; |
|
16 |
|
17 void prefix_set(const char *binloc, const char *dflt) |
|
18 { |
|
19 int i=strlen(binloc); |
|
20 int j=strlen(dflt); |
|
21 |
|
22 if(binloc[0]!='/') |
|
23 die(TR("This relocatable binary should be started with an absolute path.")); |
|
24 |
|
25 while(i>0 && j>0){ |
|
26 if(binloc[i-1]!=dflt[j-1]) |
|
27 break; |
|
28 i--; |
|
29 j--; |
|
30 } |
|
31 |
|
32 the_prefix=scopyn(binloc, i); |
|
33 |
|
34 } |
|
35 |
|
36 |
|
37 char *prefix_add(const char *s) |
|
38 { |
|
39 if(the_prefix==NULL) |
|
40 return scopy(s); |
|
41 else |
|
42 return scat3(the_prefix, "/", s); |
|
43 } |
|
44 |
|
45 |
|
46 bool prefix_wrap_simple(bool (*fn)(const char *s), const char *s) |
|
47 { |
|
48 bool ret=FALSE; |
|
49 |
|
50 if(the_prefix==NULL){ |
|
51 ret=fn(s); |
|
52 }else{ |
|
53 char *s2=prefix_add(s); |
|
54 if(s2!=NULL){ |
|
55 ret=fn(s2); |
|
56 free(s2); |
|
57 } |
|
58 } |
|
59 |
|
60 return ret; |
|
61 } |