* subst.c --- substitution program
*
* Subst is used as a quicky program to do @ substitutions
- *
+ *
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <time.h>
#include <utime.h>
#ifdef HAVE_GETOPT_H
static int add_subst(char *name, char *value)
{
struct subst_entry *ent = 0;
- int retval;
-
- retval = ENOMEM;
+
ent = (struct subst_entry *) malloc(sizeof(struct subst_entry));
if (!ent)
goto fail;
return 0;
fail:
if (ent) {
- if (ent->name)
- free(ent->name);
- if (ent->value)
- free(ent->value);
+ free(ent->name);
free(ent);
}
- return retval;
+ return ENOMEM;
}
static struct subst_entry *fetch_subst_entry(char *name)
return NULL;
memcpy(start, begin, len);
start[len] = 0;
-
+
/*
* The substitution variable must all be in the of [0-9A-Za-z_].
* If it isn't, this must be an invalid symbol name.
}
ent = fetch_subst_entry(replace_name);
if (!ent) {
- fprintf(stderr, "Unfound expansion: '%s'\n",
+ fprintf(stderr, "Unfound expansion: '%s'\n",
replace_name);
ptr = end_ptr + 1;
continue;
#endif
ptr = name_ptr-1;
replace_string(ptr, end_ptr, ent->value);
+ if ((ent->value[0] == '@') &&
+ (strlen(replace_name) == strlen(ent->value)-2) &&
+ !strncmp(replace_name, ent->value+1,
+ strlen(ent->value)-2))
+ /* avoid an infinite loop */
+ ptr += strlen(ent->value);
}
/*
* Now do a second pass to expand ${FOO}
if (!replace_name) {
ptr = name_ptr;
continue;
- }
+ }
ent = fetch_subst_entry(replace_name);
if (!ent) {
ptr = end_ptr + 1;
if (!old_f)
return 0;
new_f = fopen(newfn, "r");
- if (!new_f)
+ if (!new_f) {
+ fclose(old_f);
return 0;
+ }
while (1) {
oldcp = fgets(oldbuf, sizeof(oldbuf), old_f);
int adjust_timestamp = 0;
struct stat stbuf;
struct utimbuf ut;
-
+
while ((c = getopt (argc, argv, "f:tv")) != EOF) {
switch (c) {
case 'f':
verbose++;
break;
default:
- fprintf(stderr, "%s: [-f config-file] [file]\n",
+ fprintf(stderr, "%s: [-f config-file] [file]\n",
argv[0]);
break;
}
optind++;
} else
in = stdin;
-
+
if (optind < argc) {
outfn = argv[optind];
newfn = (char *) malloc(strlen(outfn)+20);
out = stdout;
outfn = 0;
}
-
+
while (!feof(in)) {
if (fgets(line, sizeof(line), in) == NULL)
break;
fclose(in);
fclose(out);
if (outfn) {
+ struct stat st;
if (compare_file(outfn, newfn)) {
if (verbose)
printf("No change, keeping %s.\n", outfn);
printf("Creating or replacing %s.\n", outfn);
rename(newfn, outfn);
}
+ /* set read-only to alert user it is a generated file */
+ if (stat(outfn, &st) == 0)
+ chmod(outfn, st.st_mode & ~0222);
}
return (0);
}