]>
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
11 #define HAVE_SYS_TIME_H
19 #ifdef HAVE_SYS_TIME_H
22 #include <sys/types.h>
27 #ifdef HAVE_SYS_TIME_H
42 struct subst_entry
*next
;
45 static struct subst_entry
*subst_table
= 0;
47 static int add_subst(char *name
, char *value
)
49 struct subst_entry
*ent
= 0;
51 ent
= (struct subst_entry
*) malloc(sizeof(struct subst_entry
));
54 ent
->name
= (char *) malloc(strlen(name
)+1);
57 ent
->value
= (char *) malloc(strlen(value
)+1);
60 strcpy(ent
->name
, name
);
61 strcpy(ent
->value
, value
);
62 ent
->next
= subst_table
;
73 static struct subst_entry
*fetch_subst_entry(char *name
)
75 struct subst_entry
*ent
;
77 for (ent
= subst_table
; ent
; ent
= ent
->next
) {
78 if (strcmp(name
, ent
->name
) == 0)
85 * Given the starting and ending position of the replacement name,
86 * check to see if it is valid, and pull it out if it is.
88 static char *get_subst_symbol(const char *begin
, size_t len
, char prefix
)
90 static char replace_name
[128];
97 if (len
> sizeof(replace_name
)-2)
99 memcpy(start
, begin
, len
);
103 * The substitution variable must all be in the of [0-9A-Za-z_].
104 * If it isn't, this must be an invalid symbol name.
106 for (cp
= start
; *cp
; cp
++) {
107 if (!(*cp
>= 'a' && *cp
<= 'z') &&
108 !(*cp
>= 'A' && *cp
<= 'Z') &&
109 !(*cp
>= '0' && *cp
<= '9') &&
113 return (replace_name
);
116 static void replace_string(char *begin
, char *end
, char *newstr
)
118 int replace_len
, len
;
120 replace_len
= strlen(newstr
);
122 if (replace_len
== 0)
123 memmove(begin
, end
+1, strlen(end
)+1);
124 else if (replace_len
!= len
+1)
125 memmove(end
+(replace_len
-len
-1), end
,
127 memcpy(begin
, newstr
, replace_len
);
130 static void substitute_line(char *line
)
132 char *ptr
, *name_ptr
, *end_ptr
;
133 struct subst_entry
*ent
;
138 * Expand all @FOO@ substitutions
142 name_ptr
= strchr(ptr
, '@');
145 if (*(++name_ptr
) == '@') {
147 * Handle tytso@@mit.edu --> tytso@mit.edu
149 memmove(name_ptr
-1, name_ptr
, strlen(name_ptr
)+1);
153 end_ptr
= strchr(name_ptr
, '@');
156 len
= end_ptr
- name_ptr
;
157 replace_name
= get_subst_symbol(name_ptr
, len
, 0);
162 ent
= fetch_subst_entry(replace_name
);
164 fprintf(stderr
, "Unfound expansion: '%s'\n",
170 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
171 replace_name
, ent
->value
);
174 replace_string(ptr
, end_ptr
, ent
->value
);
175 if ((ent
->value
[0] == '@') &&
176 (strlen(replace_name
) == strlen(ent
->value
)-2) &&
177 !strncmp(replace_name
, ent
->value
+1,
178 strlen(ent
->value
)-2))
179 /* avoid an infinite loop */
180 ptr
+= strlen(ent
->value
);
183 * Now do a second pass to expand ${FOO}
187 name_ptr
= strchr(ptr
, '$');
190 if (*(++name_ptr
) != '{') {
195 end_ptr
= strchr(name_ptr
, '}');
198 len
= end_ptr
- name_ptr
;
199 replace_name
= get_subst_symbol(name_ptr
, len
, '$');
204 ent
= fetch_subst_entry(replace_name
);
210 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
211 replace_name
, ent
->value
);
214 replace_string(ptr
, end_ptr
, ent
->value
);
218 static void parse_config_file(FILE *f
)
224 memset(line
, 0, sizeof(line
));
225 if (fgets(line
, sizeof(line
), f
) == NULL
)
228 * Strip newlines and comments.
230 cp
= strchr(line
, '\n');
233 cp
= strchr(line
, '#');
237 * Skip trailing and leading whitespace
239 for (cp
= line
+ strlen(line
) - 1; cp
>= line
; cp
--) {
240 if (*cp
== ' ' || *cp
== '\t')
246 while (*cp
&& isspace(*cp
))
255 * Ignore future extensions
260 * Parse substitutions
262 for (cp
= ptr
; *cp
; cp
++)
266 for (cp
++; *cp
; cp
++)
270 printf("Substitute: '%s' for '%s'\n", ptr
, cp
? cp
: "<NULL>");
277 * Return 0 if the files are different, 1 if the files are the same.
279 static int compare_file(FILE *old_f
, FILE *new_f
)
281 char oldbuf
[2048], newbuf
[2048], *oldcp
, *newcp
;
285 oldcp
= fgets(oldbuf
, sizeof(oldbuf
), old_f
);
286 newcp
= fgets(newbuf
, sizeof(newbuf
), new_f
);
287 if (!oldcp
&& !newcp
) {
291 if (!oldcp
|| !newcp
|| strcmp(oldbuf
, newbuf
)) {
299 void set_utimes(const char *filename
, int fd
, const struct timeval times
[2])
302 if (futimes(fd
, times
) < 0)
305 if (utimes(filename
, times
) < 0)
310 ut
.actime
= times
[0].tv_sec
;
311 ut
.modtime
= times
[1].tv_sec
;
312 if (utime(filename
, &ut
) < 0)
318 int main(int argc
, char **argv
)
323 FILE *in
, *out
, *old
= NULL
;
324 char *outfn
= NULL
, *newfn
= NULL
;
326 int adjust_timestamp
= 0;
329 struct timeval tv
[2];
331 while ((c
= getopt (argc
, argv
, "f:tv")) != EOF
) {
334 in
= fopen(optarg
, "r");
339 parse_config_file(in
);
349 fprintf(stderr
, "%s: [-f config-file] [file]\n",
355 in
= fopen(argv
[optind
], "r");
357 perror(argv
[optind
]);
365 outfn
= argv
[optind
];
366 newfn
= (char *) malloc(strlen(outfn
)+20);
368 fprintf(stderr
, "Memory error! Exiting.\n");
371 strcpy(newfn
, outfn
);
372 strcat(newfn
, ".new");
373 fd
= open(newfn
, O_CREAT
|O_TRUNC
|O_RDWR
, 0444);
378 out
= fdopen(fd
, "w+");
384 fd
= open(outfn
, O_RDONLY
);
386 /* save the original atime, if possible */
387 if (fstat(fd
, &stbuf
) == 0) {
388 #if HAVE_STRUCT_STAT_ST_ATIM
389 tv
[0].tv_sec
= stbuf
.st_atim
.tv_sec
;
390 tv
[0].tv_usec
= stbuf
.st_atim
.tv_nsec
/ 1000;
392 tv
[0].tv_sec
= stbuf
.st_atime
;
397 old
= fdopen(fd
, "r");
407 if (fgets(line
, sizeof(line
), in
) == NULL
)
409 substitute_line(line
);
416 if (old
&& compare_file(old
, out
)) {
418 printf("No change, keeping %s.\n", outfn
);
419 if (adjust_timestamp
) {
421 printf("Updating modtime for %s\n", outfn
);
422 if (gettimeofday(&tv
[1], NULL
) < 0) {
423 perror("gettimeofday");
429 printf("Using original atime\n");
430 set_utimes(outfn
, fileno(old
), tv
);
433 if (unlink(newfn
) < 0)
437 printf("Creating or replacing %s.\n", outfn
);
442 if (rename(newfn
, outfn
) < 0) {