]>
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
17 #ifdef HAVE_SYS_TIME_H
20 #include <sys/types.h>
25 #ifdef HAVE_SYS_TIME_H
40 struct subst_entry
*next
;
43 static struct subst_entry
*subst_table
= 0;
45 static int add_subst(char *name
, char *value
)
47 struct subst_entry
*ent
= 0;
49 ent
= (struct subst_entry
*) malloc(sizeof(struct subst_entry
));
52 ent
->name
= (char *) malloc(strlen(name
)+1);
55 ent
->value
= (char *) malloc(strlen(value
)+1);
58 strcpy(ent
->name
, name
);
59 strcpy(ent
->value
, value
);
60 ent
->next
= subst_table
;
71 static struct subst_entry
*fetch_subst_entry(char *name
)
73 struct subst_entry
*ent
;
75 for (ent
= subst_table
; ent
; ent
= ent
->next
) {
76 if (strcmp(name
, ent
->name
) == 0)
83 * Given the starting and ending position of the replacement name,
84 * check to see if it is valid, and pull it out if it is.
86 static char *get_subst_symbol(const char *begin
, size_t len
, char prefix
)
88 static char replace_name
[128];
95 if (len
> sizeof(replace_name
)-2)
97 memcpy(start
, begin
, len
);
101 * The substitution variable must all be in the of [0-9A-Za-z_].
102 * If it isn't, this must be an invalid symbol name.
104 for (cp
= start
; *cp
; cp
++) {
105 if (!(*cp
>= 'a' && *cp
<= 'z') &&
106 !(*cp
>= 'A' && *cp
<= 'Z') &&
107 !(*cp
>= '0' && *cp
<= '9') &&
111 return (replace_name
);
114 static void replace_string(char *begin
, char *end
, char *newstr
)
116 int replace_len
, len
;
118 replace_len
= strlen(newstr
);
120 if (replace_len
== 0)
121 memmove(begin
, end
+1, strlen(end
)+1);
122 else if (replace_len
!= len
+1)
123 memmove(end
+(replace_len
-len
-1), end
,
125 memcpy(begin
, newstr
, replace_len
);
128 static void substitute_line(char *line
)
130 char *ptr
, *name_ptr
, *end_ptr
;
131 struct subst_entry
*ent
;
136 * Expand all @FOO@ substitutions
140 name_ptr
= strchr(ptr
, '@');
143 if (*(++name_ptr
) == '@') {
145 * Handle tytso@@mit.edu --> tytso@mit.edu
147 memmove(name_ptr
-1, name_ptr
, strlen(name_ptr
)+1);
151 end_ptr
= strchr(name_ptr
, '@');
154 len
= end_ptr
- name_ptr
;
155 replace_name
= get_subst_symbol(name_ptr
, len
, 0);
160 ent
= fetch_subst_entry(replace_name
);
162 fprintf(stderr
, "Unfound expansion: '%s'\n",
168 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
169 replace_name
, ent
->value
);
172 replace_string(ptr
, end_ptr
, ent
->value
);
173 if ((ent
->value
[0] == '@') &&
174 (strlen(replace_name
) == strlen(ent
->value
)-2) &&
175 !strncmp(replace_name
, ent
->value
+1,
176 strlen(ent
->value
)-2))
177 /* avoid an infinite loop */
178 ptr
+= strlen(ent
->value
);
181 * Now do a second pass to expand ${FOO}
185 name_ptr
= strchr(ptr
, '$');
188 if (*(++name_ptr
) != '{') {
193 end_ptr
= strchr(name_ptr
, '}');
196 len
= end_ptr
- name_ptr
;
197 replace_name
= get_subst_symbol(name_ptr
, len
, '$');
202 ent
= fetch_subst_entry(replace_name
);
208 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
209 replace_name
, ent
->value
);
212 replace_string(ptr
, end_ptr
, ent
->value
);
216 static void parse_config_file(FILE *f
)
222 memset(line
, 0, sizeof(line
));
223 if (fgets(line
, sizeof(line
), f
) == NULL
)
226 * Strip newlines and comments.
228 cp
= strchr(line
, '\n');
231 cp
= strchr(line
, '#');
235 * Skip trailing and leading whitespace
237 for (cp
= line
+ strlen(line
) - 1; cp
>= line
; cp
--) {
238 if (*cp
== ' ' || *cp
== '\t')
244 while (*cp
&& isspace(*cp
))
253 * Ignore future extensions
258 * Parse substitutions
260 for (cp
= ptr
; *cp
; cp
++)
264 for (cp
++; *cp
; cp
++)
268 printf("Substitute: '%s' for '%s'\n", ptr
, cp
? cp
: "<NULL>");
275 * Return 0 if the files are different, 1 if the files are the same.
277 static int compare_file(FILE *old_f
, FILE *new_f
)
279 char oldbuf
[2048], newbuf
[2048], *oldcp
, *newcp
;
283 oldcp
= fgets(oldbuf
, sizeof(oldbuf
), old_f
);
284 newcp
= fgets(newbuf
, sizeof(newbuf
), new_f
);
285 if (!oldcp
&& !newcp
) {
289 if (!oldcp
|| !newcp
|| strcmp(oldbuf
, newbuf
)) {
299 int main(int argc
, char **argv
)
304 FILE *in
, *out
, *old
= NULL
;
305 char *outfn
= NULL
, *newfn
= NULL
;
307 int adjust_timestamp
= 0;
310 struct timeval tv
[2];
312 while ((c
= getopt (argc
, argv
, "f:tv")) != EOF
) {
315 in
= fopen(optarg
, "r");
320 parse_config_file(in
);
330 fprintf(stderr
, "%s: [-f config-file] [file]\n",
336 in
= fopen(argv
[optind
], "r");
338 perror(argv
[optind
]);
346 outfn
= argv
[optind
];
347 newfn
= (char *) malloc(strlen(outfn
)+20);
349 fprintf(stderr
, "Memory error! Exiting.\n");
352 strcpy(newfn
, outfn
);
353 strcat(newfn
, ".new");
354 fd
= open(newfn
, O_CREAT
|O_TRUNC
|O_RDWR
, 0444);
359 out
= fdopen(fd
, "w+");
365 fd
= open(outfn
, O_RDONLY
);
367 /* save the original atime, if possible */
368 if (fstat(fd
, &stbuf
) == 0) {
369 #if HAVE_STRUCT_STAT_ST_ATIM
370 tv
[0].tv_sec
= stbuf
.st_atim
.tv_sec
;
371 tv
[0].tv_usec
= stbuf
.st_atim
.tv_nsec
/ 1000;
373 tv
[0].tv_sec
= stbuf
.st_atime
;
378 old
= fdopen(fd
, "r");
388 if (fgets(line
, sizeof(line
), in
) == NULL
)
390 substitute_line(line
);
397 if (old
&& compare_file(old
, out
)) {
399 printf("No change, keeping %s.\n", outfn
);
400 if (adjust_timestamp
) {
402 printf("Updating modtime for %s\n", outfn
);
403 if (gettimeofday(&tv
[1], NULL
) < 0) {
404 perror("gettimeofday");
410 printf("Using original atime\n");
412 if (futimes(fileno(old
), tv
) < 0)
415 if (utimes(outfn
, tv
) < 0)
420 if (unlink(newfn
) < 0)
424 printf("Creating or replacing %s.\n", outfn
);
429 if (rename(newfn
, outfn
) < 0) {