]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - util/subst.c
2 * subst.c --- substitution program
4 * Subst is used as a quicky program to do @ substitutions
23 struct subst_entry
*next
;
26 struct subst_entry
*subst_table
= 0;
28 static int add_subst(char *name
, char *value
)
30 struct subst_entry
*ent
= 0;
34 ent
= malloc(sizeof(struct subst_entry
));
37 ent
->name
= malloc(strlen(name
)+1);
40 ent
->value
= malloc(strlen(value
)+1);
43 strcpy(ent
->name
, name
);
44 strcpy(ent
->value
, value
);
45 ent
->next
= subst_table
;
59 static struct subst_entry
*fetch_subst_entry(char *name
)
61 struct subst_entry
*ent
;
63 for (ent
= subst_table
; ent
; ent
= ent
->next
) {
64 if (strcmp(name
, ent
->name
) == 0)
70 static void substitute_line(char *line
)
72 char *ptr
, *name_ptr
, *end_ptr
;
73 struct subst_entry
*ent
;
74 char replace_name
[128];
79 name_ptr
= strchr(ptr
, '@');
82 end_ptr
= strchr(name_ptr
+1, '@');
85 len
= end_ptr
- name_ptr
- 1;
86 memcpy(replace_name
, name_ptr
+1, len
);
87 replace_name
[len
] = 0;
88 ent
= fetch_subst_entry(replace_name
);
90 fprintf(stderr
, "Unfound expansion: '%s'\n",
96 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
97 replace_name
, ent
->value
);
99 replace_len
= strlen(ent
->value
);
100 if (replace_len
!= len
+2)
101 memmove(end_ptr
+(replace_len
-len
-2), end_ptr
,
103 memcpy(name_ptr
, ent
->value
, replace_len
);
108 static void parse_config_file(FILE *f
)
114 memset(line
, 0, sizeof(line
));
115 if (fgets(line
, sizeof(line
), f
) == NULL
)
118 * Strip newlines and comments.
120 cp
= strchr(line
, '\n');
123 cp
= strchr(line
, '#');
127 * Skip trailing and leading whitespace
129 for (cp
= line
+ strlen(line
) - 1; cp
>= line
; cp
--) {
130 if (*cp
== ' ' || *cp
== '\t')
136 while (*cp
&& isspace(*cp
))
145 * Ignore future extensions
150 * Parse substitutions
152 for (cp
= ptr
; *cp
; cp
++)
156 for (cp
++; *cp
; cp
++)
160 printf("Substitute: '%s' for '%s'\n", ptr
, cp
);
167 * Return 0 if the files are different, 1 if the files are the same.
169 static int compare_file(const char *outfn
, const char *newfn
)
172 char oldbuf
[2048], newbuf
[2048], *oldcp
, *newcp
;
175 old
= fopen(outfn
, "r");
178 new = fopen(newfn
, "r");
183 oldcp
= fgets(oldbuf
, sizeof(oldbuf
), old
);
184 newcp
= fgets(newbuf
, sizeof(newbuf
), new);
185 if (!oldcp
&& !newcp
) {
189 if (!oldcp
|| !newcp
|| strcmp(oldbuf
, newbuf
)) {
200 int main(int argc
, char **argv
)
205 char *outfn
= NULL
, *newfn
= NULL
;
208 while ((c
= getopt (argc
, argv
, "f:v")) != EOF
) {
211 in
= fopen(optarg
, "r");
216 parse_config_file(in
);
223 fprintf(stderr
, "%s: [-f config-file] [file]\n",
229 in
= fopen(argv
[optind
], "r");
231 perror(argv
[optind
]);
239 outfn
= argv
[optind
];
240 newfn
= malloc(strlen(outfn
)+20);
242 fprintf(stderr
, "Memory error! Exiting.\n");
245 strcpy(newfn
, outfn
);
246 strcat(newfn
, ".new");
247 out
= fopen(newfn
, "w");
258 if (fgets(line
, sizeof(line
), in
) == NULL
)
260 substitute_line(line
);
266 if (compare_file(outfn
, newfn
)) {
268 printf("No change, keeping %s.\n", outfn
);
272 printf("Creating or replacing %s.\n", outfn
);
273 rename(newfn
, outfn
);