X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=util%2Fsubst.c;h=8544b6da4c0771a90e815cd5d0b4c612ec2a2a66;hb=0a05b90330d8b505469c72143964b954776be232;hp=445d3186a8657054888e9c03a419d1bb089eb526;hpb=98224fbc1db8387da1fe0fd1bb3be25c18be2e98;p=tools%2Fe2fsprogs.git diff --git a/util/subst.c b/util/subst.c index 445d318..8544b6d 100644 --- a/util/subst.c +++ b/util/subst.c @@ -2,7 +2,7 @@ * subst.c --- substitution program * * Subst is used as a quicky program to do @ substitutions - * + * */ #include @@ -35,9 +35,7 @@ struct subst_entry *subst_table = 0; 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; @@ -54,13 +52,10 @@ static int add_subst(char *name, char *value) 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) @@ -91,7 +86,7 @@ static char *get_subst_symbol(const char *begin, size_t len, char prefix) 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. @@ -154,7 +149,7 @@ static void substitute_line(char *line) } 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; @@ -167,7 +162,7 @@ static void substitute_line(char *line) replace_string(ptr, end_ptr, ent->value); if ((ent->value[0] == '@') && (strlen(replace_name) == strlen(ent->value)-2) && - !strncmp(replace_name, ent->value+1, + !strncmp(replace_name, ent->value+1, strlen(ent->value)-2)) /* avoid an infinite loop */ ptr += strlen(ent->value); @@ -193,7 +188,7 @@ static void substitute_line(char *line) if (!replace_name) { ptr = name_ptr; continue; - } + } ent = fetch_subst_entry(replace_name); if (!ent) { ptr = end_ptr + 1; @@ -279,8 +274,10 @@ static int compare_file(const char *outfn, const char *newfn) 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); @@ -311,7 +308,7 @@ int main(int argc, char **argv) int adjust_timestamp = 0; struct stat stbuf; struct utimbuf ut; - + while ((c = getopt (argc, argv, "f:tv")) != EOF) { switch (c) { case 'f': @@ -330,7 +327,7 @@ int main(int argc, char **argv) verbose++; break; default: - fprintf(stderr, "%s: [-f config-file] [file]\n", + fprintf(stderr, "%s: [-f config-file] [file]\n", argv[0]); break; } @@ -344,7 +341,7 @@ int main(int argc, char **argv) optind++; } else in = stdin; - + if (optind < argc) { outfn = argv[optind]; newfn = (char *) malloc(strlen(outfn)+20); @@ -363,7 +360,7 @@ int main(int argc, char **argv) out = stdout; outfn = 0; } - + while (!feof(in)) { if (fgets(line, sizeof(line), in) == NULL) break;