prefix.c

Mon, 20 Apr 2020 10:14:32 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 20 Apr 2020 10:14:32 -0500
changeset 119
87e3bb5086e8
parent 117
c7ad4b23a277
permissions
-rw-r--r--

Convert README to markdown

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

mercurial