]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
1 | /* |
2 | * Copyright (c) 2000, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* See concat.h. Note that we canonicalize the result, this avoids | |
17 | multiple slashes being interpreted as UNCs. */ | |
18 | ||
b24c88b3 RC |
19 | #if 0 |
20 | static const char *cvsid = | |
21 | "\n%%% $Id$\n"; | |
22 | #endif | |
23 | ||
8507f105 | 24 | |
23c9e63c DD |
25 | #include <stdarg.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
a351e48c | 28 | #include "concat.h" |
23c9e63c DD |
29 | |
30 | char * | |
a351e48c CF |
31 | concat (const char *s, ...) |
32 | { | |
33 | va_list v; | |
34 | ||
35 | va_start (v, s); | |
36 | ||
37 | return vconcat (s, v); | |
38 | } | |
39 | ||
40 | char * | |
41 | vconcat (const char *s, va_list v) | |
23c9e63c | 42 | { |
ed3e8b9b | 43 | int len; |
23c9e63c | 44 | char *rv, *arg; |
a351e48c CF |
45 | va_list save_v = v; |
46 | int unc; | |
23c9e63c DD |
47 | |
48 | if (!s) | |
49 | return 0; | |
50 | ||
ed3e8b9b DD |
51 | len = strlen (s); |
52 | ||
a351e48c CF |
53 | unc = SLASH_P (*s) && SLASH_P (s[1]); |
54 | ||
23c9e63c DD |
55 | while (1) |
56 | { | |
1fd6d0a2 | 57 | arg = va_arg (v, char *); |
23c9e63c DD |
58 | if (arg == 0) |
59 | break; | |
1fd6d0a2 | 60 | len += strlen (arg); |
23c9e63c | 61 | } |
1fd6d0a2 | 62 | va_end (v); |
23c9e63c | 63 | |
85b43844 | 64 | rv = (char *) malloc (len + 1); |
1fd6d0a2 | 65 | strcpy (rv, s); |
a351e48c | 66 | v = save_v; |
23c9e63c | 67 | while (1) |
b24c88b3 RC |
68 | { |
69 | arg = va_arg (v, char *); | |
70 | if (arg == 0) | |
71 | break; | |
72 | strcat (rv, arg); | |
73 | } | |
1fd6d0a2 | 74 | va_end (v); |
23c9e63c | 75 | |
a351e48c CF |
76 | char *d, *p; |
77 | for (p = rv; *p; p++) | |
78 | if (*p == '\\') | |
79 | *p = '/'; | |
80 | ||
23c9e63c DD |
81 | /* concat is only used for urls and files, so we can safely |
82 | canonicalize the results */ | |
a351e48c | 83 | for (p = d = rv; *p; p++) |
23c9e63c | 84 | { |
a351e48c | 85 | *d++ = *p; |
23c9e63c | 86 | /* special case for URLs */ |
a351e48c CF |
87 | if (*p == ':' && p[1] == '/' && p[2] == '/' && p > rv + 1) |
88 | { | |
89 | *d++ = *++p; | |
90 | *d++ = *++p; | |
91 | } | |
92 | else if (*p == '/' || *p == '\\') | |
23c9e63c | 93 | { |
a351e48c CF |
94 | if (p == rv && unc) |
95 | p++; | |
96 | while (p[1] == '/') | |
97 | p++; | |
23c9e63c | 98 | } |
23c9e63c DD |
99 | } |
100 | *d = 0; | |
101 | ||
102 | return rv; | |
103 | } | |
f57c332f DD |
104 | |
105 | char * | |
106 | backslash (char *s) | |
107 | { | |
108 | for (char *t = s; *t; t++) | |
109 | if (*t == '/') | |
110 | *t = '\\'; | |
111 | return s; | |
112 | } |