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
|
/* kdefault.c: Expand extra colons.
(This is not named default.c because then the OSF/1 make tries to
make a program `default' from it, since we have a target `default';
and OSF/1 make doesn't understand .PHONY.)
Copyright 1993, 1994, 1996, 2008 Karl Berry.
Copyright 2002, 2005 Olaf Weber.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, see <http://www.gnu.org/licenses/>. */
#include <kpathsea/config.h>
#include <kpathsea/c-pathch.h>
#include <kpathsea/default.h>
/* Check for leading colon first, then trailing, then doubled, since
that is fastest. Usually it will be leading or trailing. */
string
kpse_expand_default P2C(const_string, path, const_string, fallback)
{
unsigned path_length;
string expansion;
/* The default path better not be null. */
assert (fallback);
if (path == NULL)
expansion = xstrdup (fallback);
/* Solitary or leading :? */
else if (IS_ENV_SEP (*path))
{
expansion = path[1] == 0 ? xstrdup (fallback) : concat (fallback, path);
}
/* Sorry about the assignment in the middle of the expression, but
conventions were made to be flouted and all that. I don't see the
point of calling strlen twice or complicating the logic just to
avoid the assignment (especially now that I've pointed it out at
such great length). */
else if (path[(path_length = strlen (path)) - 1] == ENV_SEP)
expansion = concat (path, fallback);
/* OK, not leading or trailing. Check for doubled. */
else
{
const_string loc;
for (loc = path; *loc; loc++)
if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1]))
break;
if (*loc)
{ /* We have a doubled colon. */
expansion = (string)xmalloc (path_length + strlen(fallback) + 1);
/* Copy stuff up to and including the first colon. */
strncpy (expansion, path, loc - path + 1);
expansion[loc - path + 1] = 0;
/* Copy in FALLBACK, and then the rest of PATH. */
strcat (expansion, fallback);
strcat (expansion, loc + 1);
}
else
{ /* No doubled colon. */
expansion = xstrdup(path);
}
}
return expansion;
}
#ifdef TEST
void
test_expand_default (const_string path, const_string def)
{
string answer;
printf ("Expansion of `%s':\t", path ? path : "(nil)");
answer = kpse_expand_default (path, def);
puts (answer);
}
int
main ()
{
string default_path = "default";
test_expand_default (NULL, default_path);
test_expand_default ("", default_path);
test_expand_default ("none", default_path);
test_expand_default (ENV_SEP_STRING, default_path);
test_expand_default (ENV_SEP_STRING "first", default_path);
test_expand_default ("last" ENV_SEP_STRING, default_path);
test_expand_default ("middle" ENV_SEP_STRING ENV_SEP_STRING "elddim", default_path);
return 0;
}
#endif /* TEST */
/*
Local variables:
standalone-compile-command: "gcc -g -I. -I.. -DTEST default.c kpathsea.a"
End:
*/
|