Cleanup memory usage

This commit is contained in:
Glenn L McGrath
2003-09-14 15:24:18 +00:00
parent 7bf8f5bc5e
commit 8417c8c38b

View File

@ -153,6 +153,10 @@ static void destroy_cmd_strs(void)
free(sed_cmd->sub_match); free(sed_cmd->sub_match);
} }
free(sed_cmd->replace); free(sed_cmd->replace);
free(sed_cmd->editline);
free(sed_cmd->filename);
free(sed_cmd->translate);
free(sed_cmd->label);
free(sed_cmd); free(sed_cmd);
sed_cmd = sed_cmd_next; sed_cmd = sed_cmd_next;
} }
@ -803,7 +807,6 @@ static void process_file(FILE * file)
/* Read one line in advance so we can act on the last line, the '$' address */ /* Read one line in advance so we can act on the last line, the '$' address */
next_line = bb_get_chomped_line_from_file(file); next_line = bb_get_chomped_line_from_file(file);
linenum++; linenum++;
altered = 0; altered = 0;
force_print = 0; force_print = 0;
@ -981,10 +984,12 @@ static void process_file(FILE * file)
next_line = NULL; next_line = NULL;
break; break;
case 'n': /* Read next line from input */ case 'n': /* Read next line from input */
free(pattern_space); if (next_line) {
pattern_space = next_line; free(pattern_space);
next_line = bb_get_chomped_line_from_file(file); pattern_space = next_line;
linenum++; next_line = bb_get_chomped_line_from_file(file);
linenum++;
}
break; break;
case 'N': /* Append the next line to the current line */ case 'N': /* Append the next line to the current line */
if (next_line) { if (next_line) {
@ -1043,17 +1048,25 @@ static void process_file(FILE * file)
hold_space = strdup(pattern_space); hold_space = strdup(pattern_space);
break; break;
case 'H': { /* Append newline and pattern space to hold space */ case 'H': { /* Append newline and pattern space to hold space */
int hold_space_size = 0; int hold_space_size = 2;
int pattern_space_size = 0;
if (hold_space) { if (hold_space) {
hold_space_size = strlen(hold_space); hold_space_size += strlen(hold_space);
} }
hold_space = xrealloc(hold_space, hold_space_size + strlen(pattern_space) + 2); if (pattern_space) {
if (hold_space_size) { pattern_space_size = strlen(pattern_space);
strcat(hold_space, "\n"); }
hold_space = xrealloc(hold_space, hold_space_size + pattern_space_size);
if (hold_space_size == 2) {
strcpy(hold_space, "\n");
} else { } else {
hold_space[0] = '\n'; strcat(hold_space, "\n");
}
if (pattern_space) {
strcat(hold_space, pattern_space);
} }
strcat(hold_space, pattern_space);
break; break;
} }
case 'x':{ case 'x':{
@ -1116,10 +1129,12 @@ extern int sed_main(int argc, char **argv)
{ {
int opt, status = EXIT_SUCCESS; int opt, status = EXIT_SUCCESS;
#if 0 /* This doesnt seem to be working */
#ifdef CONFIG_FEATURE_CLEAN_UP #ifdef CONFIG_FEATURE_CLEAN_UP
/* destroy command strings on exit */ /* destroy command strings on exit */
if (atexit(destroy_cmd_strs) == -1) if (atexit(destroy_cmd_strs) == -1)
bb_perror_msg_and_die("atexit"); bb_perror_msg_and_die("atexit");
#endif
#endif #endif
/* do normal option parsing */ /* do normal option parsing */
@ -1129,10 +1144,7 @@ extern int sed_main(int argc, char **argv)
be_quiet++; be_quiet++;
break; break;
case 'e':{ case 'e':{
char *str_cmd = strdup(optarg); add_cmd_str(optarg);
add_cmd_str(str_cmd);
free(str_cmd);
break; break;
} }
case 'f': case 'f':
@ -1149,7 +1161,7 @@ extern int sed_main(int argc, char **argv)
if (argv[optind] == NULL) if (argv[optind] == NULL)
bb_show_usage(); bb_show_usage();
else else
add_cmd_str(strdup(argv[optind++])); add_cmd_str(argv[optind++]);
} }
/* argv[(optind)..(argc-1)] should be names of file to process. If no /* argv[(optind)..(argc-1)] should be names of file to process. If no
@ -1176,5 +1188,8 @@ extern int sed_main(int argc, char **argv)
} }
} }
#ifdef CONFIG_FEATURE_CLEAN_UP
destroy_cmd_strs();
#endif
return status; return status;
} }