]>
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 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
31 #ifdef HAVE_SYS_TIME_H
46 struct subst_entry
*next
;
49 static struct subst_entry
*subst_table
= 0;
51 static int add_subst(char *name
, char *value
)
53 struct subst_entry
*ent
= 0;
55 ent
= (struct subst_entry
*) malloc(sizeof(struct subst_entry
));
58 ent
->name
= (char *) malloc(strlen(name
)+1);
61 ent
->value
= (char *) malloc(strlen(value
)+1);
64 strcpy(ent
->name
, name
);
65 strcpy(ent
->value
, value
);
66 ent
->next
= subst_table
;
77 static struct subst_entry
*fetch_subst_entry(char *name
)
79 struct subst_entry
*ent
;
81 for (ent
= subst_table
; ent
; ent
= ent
->next
) {
82 if (strcmp(name
, ent
->name
) == 0)
89 * Given the starting and ending position of the replacement name,
90 * check to see if it is valid, and pull it out if it is.
92 static char *get_subst_symbol(const char *begin
, size_t len
, char prefix
)
94 static char replace_name
[128];
101 if (len
> sizeof(replace_name
)-2)
103 memcpy(start
, begin
, len
);
107 * The substitution variable must all be in the of [0-9A-Za-z_].
108 * If it isn't, this must be an invalid symbol name.
110 for (cp
= start
; *cp
; cp
++) {
111 if (!(*cp
>= 'a' && *cp
<= 'z') &&
112 !(*cp
>= 'A' && *cp
<= 'Z') &&
113 !(*cp
>= '0' && *cp
<= '9') &&
117 return (replace_name
);
120 static void replace_string(char *begin
, char *end
, char *newstr
)
122 int replace_len
, len
;
124 replace_len
= strlen(newstr
);
126 if (replace_len
== 0)
127 memmove(begin
, end
+1, strlen(end
)+1);
128 else if (replace_len
!= len
+1)
129 memmove(end
+(replace_len
-len
-1), end
,
131 memcpy(begin
, newstr
, replace_len
);
134 static void substitute_line(char *line
)
136 char *ptr
, *name_ptr
, *end_ptr
;
137 struct subst_entry
*ent
;
142 * Expand all @FOO@ substitutions
146 name_ptr
= strchr(ptr
, '@');
149 if (*(++name_ptr
) == '@') {
151 * Handle tytso@@mit.edu --> tytso@mit.edu
153 memmove(name_ptr
-1, name_ptr
, strlen(name_ptr
)+1);
157 end_ptr
= strchr(name_ptr
, '@');
160 len
= end_ptr
- name_ptr
;
161 replace_name
= get_subst_symbol(name_ptr
, len
, 0);
166 ent
= fetch_subst_entry(replace_name
);
168 fprintf(stderr
, "Unfound expansion: '%s'\n",
174 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
175 replace_name
, ent
->value
);
178 replace_string(ptr
, end_ptr
, ent
->value
);
179 if ((ent
->value
[0] == '@') &&
180 (strlen(replace_name
) == strlen(ent
->value
)-2) &&
181 !strncmp(replace_name
, ent
->value
+1,
182 strlen(ent
->value
)-2))
183 /* avoid an infinite loop */
184 ptr
+= strlen(ent
->value
);
187 * Now do a second pass to expand ${FOO}
191 name_ptr
= strchr(ptr
, '$');
194 if (*(++name_ptr
) != '{') {
199 end_ptr
= strchr(name_ptr
, '}');
202 len
= end_ptr
- name_ptr
;
203 replace_name
= get_subst_symbol(name_ptr
, len
, '$');
208 ent
= fetch_subst_entry(replace_name
);
214 fprintf(stderr
, "Replace name = '%s' with '%s'\n",
215 replace_name
, ent
->value
);
218 replace_string(ptr
, end_ptr
, ent
->value
);
222 static void parse_config_file(FILE *f
)
228 memset(line
, 0, sizeof(line
));
229 if (fgets(line
, sizeof(line
), f
) == NULL
)
232 * Strip newlines and comments.
234 cp
= strchr(line
, '\n');
237 cp
= strchr(line
, '#');
241 * Skip trailing and leading whitespace
243 for (cp
= line
+ strlen(line
) - 1; cp
>= line
; cp
--) {
244 if (*cp
== ' ' || *cp
== '\t')
250 while (*cp
&& isspace(*cp
))
259 * Ignore future extensions
264 * Parse substitutions
266 for (cp
= ptr
; *cp
; cp
++)
270 for (cp
++; *cp
; cp
++)
274 printf("Substitute: '%s' for '%s'\n", ptr
, cp
? cp
: "<NULL>");
281 * Return 0 if the files are different, 1 if the files are the same.
283 static int compare_file(FILE *old_f
, FILE *new_f
)
285 char oldbuf
[2048], newbuf
[2048], *oldcp
, *newcp
;
289 oldcp
= fgets(oldbuf
, sizeof(oldbuf
), old_f
);
290 newcp
= fgets(newbuf
, sizeof(newbuf
), new_f
);
291 if (!oldcp
&& !newcp
) {
295 if (!oldcp
|| !newcp
|| strcmp(oldbuf
, newbuf
)) {
303 void set_utimes(const char *filename
, int fd
, const struct timeval times
[2])
306 if (futimes(fd
, times
) < 0)
309 if (utimes(filename
, times
) < 0)
314 ut
.actime
= times
[0].tv_sec
;
315 ut
.modtime
= times
[1].tv_sec
;
316 if (utime(filename
, &ut
) < 0)
322 int main(int argc
, char **argv
)
327 FILE *in
, *out
, *old
= NULL
;
328 char *outfn
= NULL
, *newfn
= NULL
;
330 int adjust_timestamp
= 0;
333 struct timeval tv
[2];
335 while ((c
= getopt (argc
, argv
, "f:tv")) != EOF
) {
338 in
= fopen(optarg
, "r");
343 parse_config_file(in
);
353 fprintf(stderr
, "%s: [-f config-file] [file]\n",
359 in
= fopen(argv
[optind
], "r");
361 perror(argv
[optind
]);
369 outfn
= argv
[optind
];
370 newfn
= (char *) malloc(strlen(outfn
)+20);
372 fprintf(stderr
, "Memory error! Exiting.\n");
375 strcpy(newfn
, outfn
);
376 strcat(newfn
, ".new");
377 ofd
= open(newfn
, O_CREAT
|O_TRUNC
|O_RDWR
, 0644);
382 out
= fdopen(ofd
, "w+");
388 fd
= open(outfn
, O_RDONLY
);
390 /* save the original atime, if possible */
391 if (fstat(fd
, &stbuf
) == 0) {
392 #if HAVE_STRUCT_STAT_ST_ATIM
393 tv
[0].tv_sec
= stbuf
.st_atim
.tv_sec
;
394 tv
[0].tv_usec
= stbuf
.st_atim
.tv_nsec
/ 1000;
396 tv
[0].tv_sec
= stbuf
.st_atime
;
401 old
= fdopen(fd
, "r");
411 if (fgets(line
, sizeof(line
), in
) == NULL
)
413 substitute_line(line
);
420 if (old
&& compare_file(old
, out
)) {
422 printf("No change, keeping %s.\n", outfn
);
423 if (adjust_timestamp
) {
425 printf("Updating modtime for %s\n", outfn
);
426 if (gettimeofday(&tv
[1], NULL
) < 0) {
427 perror("gettimeofday");
433 printf("Using original atime\n");
434 set_utimes(outfn
, fileno(old
), tv
);
437 (void) fchmod(ofd
, 0444);
439 if (unlink(newfn
) < 0)
443 printf("Creating or replacing %s.\n", outfn
);
445 (void) fchmod(ofd
, 0444);
450 if (rename(newfn
, outfn
) < 0) {