]> cygwin.com Git - cygwin-apps/setup.git/blob - concat.cc
Use parse_filename method to parse filenames throughout. Use get_root_dir to
[cygwin-apps/setup.git] / concat.cc
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
19 static char *cvsid = "\n%%% $Id$\n";
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "concat.h"
25
26 char *
27 concat (const char *s, ...)
28 {
29 va_list v;
30
31 va_start (v, s);
32
33 return vconcat (s, v);
34 }
35
36 char *
37 vconcat (const char *s, va_list v)
38 {
39 int len;
40 char *rv, *arg;
41 va_list save_v = v;
42 int unc;
43
44 if (!s)
45 return 0;
46
47 len = strlen (s);
48
49 unc = SLASH_P (*s) && SLASH_P (s[1]);
50
51 while (1)
52 {
53 arg = va_arg (v, char *);
54 if (arg == 0)
55 break;
56 len += strlen (arg);
57 }
58 va_end (v);
59
60 rv = (char *) malloc (len + 1);
61 strcpy (rv, s);
62 v = save_v;
63 while (1)
64 {
65 arg = va_arg (v, char *);
66 if (arg == 0)
67 break;
68 strcat (rv, arg);
69 }
70 va_end (v);
71
72 char *d, *p;
73 for (p = rv; *p; p++)
74 if (*p == '\\')
75 *p = '/';
76
77 /* concat is only used for urls and files, so we can safely
78 canonicalize the results */
79 for (p = d = rv; *p; p++)
80 {
81 *d++ = *p;
82 /* special case for URLs */
83 if (*p == ':' && p[1] == '/' && p[2] == '/' && p > rv + 1)
84 {
85 *d++ = *++p;
86 *d++ = *++p;
87 }
88 else if (*p == '/' || *p == '\\')
89 {
90 if (p == rv && unc)
91 p++;
92 while (p[1] == '/')
93 p++;
94 }
95 }
96 *d = 0;
97
98 return rv;
99 }
100
101 char *
102 backslash (char *s)
103 {
104 for (char *t = s; *t; t++)
105 if (*t == '/')
106 *t = '\\';
107 return s;
108 }
This page took 0.038536 seconds and 5 git commands to generate.