2 * Copyright (c) 2002 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: substit.c,v 1.5 2003/12/19 01:27:10 steve Exp $"
30 char* substitutions(const char*str
)
32 size_t nbuf
= strlen(str
) + 1;
33 char*buf
= malloc(nbuf
);
38 if ((str
[0] == '$') && (str
[1] == '(')) {
39 /* If I find a $(x) string in the source, replace
40 it in the destination with the contents of the
41 environment variable x. */
44 const char*ep
= strchr(str
, ')');
47 name
= malloc(ep
-str
+1);
48 strncpy(name
, str
, ep
-str
);
58 if (strlen(value
) >= (nbuf
- (cp
-buf
))) {
59 size_t old_size
= cp
- buf
;
60 nbuf
= (cp
- buf
) + strlen(value
) + 1;
61 buf
= realloc(buf
, nbuf
);
69 if ( cp
== (buf
+ nbuf
) ) {
70 size_t old_size
= nbuf
;
72 buf
= realloc(buf
, nbuf
);
80 /* Add the trailing nul to the string, and reallocate the
81 buffer to be a tight fit. */
82 if ( cp
== (buf
+ nbuf
) ) {
83 size_t old_size
= nbuf
;
85 buf
= realloc(buf
, nbuf
);
90 buf
= realloc(buf
, nbuf
);
99 * Revision 1.5 2003/12/19 01:27:10 steve
100 * Fix various unsigned compare warnings.
102 * Revision 1.4 2002/08/12 01:35:01 steve
103 * conditional ident string using autoconfig.
105 * Revision 1.3 2002/08/11 23:47:04 steve
106 * Add missing Log and Ident strings.
108 * Revision 1.2 2002/06/25 01:33:01 steve
109 * include malloc.h only when available.
111 * Revision 1.1 2002/06/23 20:10:51 steve
112 * Variable substitution in command files.