diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-09-14 15:24:18 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-09-14 15:24:18 +0000 |
commit | 8417c8c38bdcab79989ceab50382eb6314505c93 (patch) | |
tree | 42acd26014aa20e6964142b6ab2d60a32811a3bb | |
parent | 7bf8f5bc5e2b4e506936f482a5b55f4e0d044c68 (diff) | |
download | busybox-w32-8417c8c38bdcab79989ceab50382eb6314505c93.tar.gz busybox-w32-8417c8c38bdcab79989ceab50382eb6314505c93.tar.bz2 busybox-w32-8417c8c38bdcab79989ceab50382eb6314505c93.zip |
Cleanup memory usage
-rw-r--r-- | editors/sed.c | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/editors/sed.c b/editors/sed.c index 63d5581f0..f0a3c028f 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -153,6 +153,10 @@ static void destroy_cmd_strs(void) | |||
153 | free(sed_cmd->sub_match); | 153 | free(sed_cmd->sub_match); |
154 | } | 154 | } |
155 | free(sed_cmd->replace); | 155 | free(sed_cmd->replace); |
156 | free(sed_cmd->editline); | ||
157 | free(sed_cmd->filename); | ||
158 | free(sed_cmd->translate); | ||
159 | free(sed_cmd->label); | ||
156 | free(sed_cmd); | 160 | free(sed_cmd); |
157 | sed_cmd = sed_cmd_next; | 161 | sed_cmd = sed_cmd_next; |
158 | } | 162 | } |
@@ -803,7 +807,6 @@ static void process_file(FILE * file) | |||
803 | 807 | ||
804 | /* Read one line in advance so we can act on the last line, the '$' address */ | 808 | /* Read one line in advance so we can act on the last line, the '$' address */ |
805 | next_line = bb_get_chomped_line_from_file(file); | 809 | next_line = bb_get_chomped_line_from_file(file); |
806 | |||
807 | linenum++; | 810 | linenum++; |
808 | altered = 0; | 811 | altered = 0; |
809 | force_print = 0; | 812 | force_print = 0; |
@@ -981,10 +984,12 @@ static void process_file(FILE * file) | |||
981 | next_line = NULL; | 984 | next_line = NULL; |
982 | break; | 985 | break; |
983 | case 'n': /* Read next line from input */ | 986 | case 'n': /* Read next line from input */ |
984 | free(pattern_space); | 987 | if (next_line) { |
985 | pattern_space = next_line; | 988 | free(pattern_space); |
986 | next_line = bb_get_chomped_line_from_file(file); | 989 | pattern_space = next_line; |
987 | linenum++; | 990 | next_line = bb_get_chomped_line_from_file(file); |
991 | linenum++; | ||
992 | } | ||
988 | break; | 993 | break; |
989 | case 'N': /* Append the next line to the current line */ | 994 | case 'N': /* Append the next line to the current line */ |
990 | if (next_line) { | 995 | if (next_line) { |
@@ -1043,17 +1048,25 @@ static void process_file(FILE * file) | |||
1043 | hold_space = strdup(pattern_space); | 1048 | hold_space = strdup(pattern_space); |
1044 | break; | 1049 | break; |
1045 | case 'H': { /* Append newline and pattern space to hold space */ | 1050 | case 'H': { /* Append newline and pattern space to hold space */ |
1046 | int hold_space_size = 0; | 1051 | int hold_space_size = 2; |
1052 | int pattern_space_size = 0; | ||
1053 | |||
1047 | if (hold_space) { | 1054 | if (hold_space) { |
1048 | hold_space_size = strlen(hold_space); | 1055 | hold_space_size += strlen(hold_space); |
1049 | } | 1056 | } |
1050 | hold_space = xrealloc(hold_space, hold_space_size + strlen(pattern_space) + 2); | 1057 | if (pattern_space) { |
1051 | if (hold_space_size) { | 1058 | pattern_space_size = strlen(pattern_space); |
1052 | strcat(hold_space, "\n"); | 1059 | } |
1060 | hold_space = xrealloc(hold_space, hold_space_size + pattern_space_size); | ||
1061 | |||
1062 | if (hold_space_size == 2) { | ||
1063 | strcpy(hold_space, "\n"); | ||
1053 | } else { | 1064 | } else { |
1054 | hold_space[0] = '\n'; | 1065 | strcat(hold_space, "\n"); |
1066 | } | ||
1067 | if (pattern_space) { | ||
1068 | strcat(hold_space, pattern_space); | ||
1055 | } | 1069 | } |
1056 | strcat(hold_space, pattern_space); | ||
1057 | break; | 1070 | break; |
1058 | } | 1071 | } |
1059 | case 'x':{ | 1072 | case 'x':{ |
@@ -1116,11 +1129,13 @@ extern int sed_main(int argc, char **argv) | |||
1116 | { | 1129 | { |
1117 | int opt, status = EXIT_SUCCESS; | 1130 | int opt, status = EXIT_SUCCESS; |
1118 | 1131 | ||
1132 | #if 0 /* This doesnt seem to be working */ | ||
1119 | #ifdef CONFIG_FEATURE_CLEAN_UP | 1133 | #ifdef CONFIG_FEATURE_CLEAN_UP |
1120 | /* destroy command strings on exit */ | 1134 | /* destroy command strings on exit */ |
1121 | if (atexit(destroy_cmd_strs) == -1) | 1135 | if (atexit(destroy_cmd_strs) == -1) |
1122 | bb_perror_msg_and_die("atexit"); | 1136 | bb_perror_msg_and_die("atexit"); |
1123 | #endif | 1137 | #endif |
1138 | #endif | ||
1124 | 1139 | ||
1125 | /* do normal option parsing */ | 1140 | /* do normal option parsing */ |
1126 | while ((opt = getopt(argc, argv, "ne:f:")) > 0) { | 1141 | while ((opt = getopt(argc, argv, "ne:f:")) > 0) { |
@@ -1129,10 +1144,7 @@ extern int sed_main(int argc, char **argv) | |||
1129 | be_quiet++; | 1144 | be_quiet++; |
1130 | break; | 1145 | break; |
1131 | case 'e':{ | 1146 | case 'e':{ |
1132 | char *str_cmd = strdup(optarg); | 1147 | add_cmd_str(optarg); |
1133 | |||
1134 | add_cmd_str(str_cmd); | ||
1135 | free(str_cmd); | ||
1136 | break; | 1148 | break; |
1137 | } | 1149 | } |
1138 | case 'f': | 1150 | case 'f': |
@@ -1149,7 +1161,7 @@ extern int sed_main(int argc, char **argv) | |||
1149 | if (argv[optind] == NULL) | 1161 | if (argv[optind] == NULL) |
1150 | bb_show_usage(); | 1162 | bb_show_usage(); |
1151 | else | 1163 | else |
1152 | add_cmd_str(strdup(argv[optind++])); | 1164 | add_cmd_str(argv[optind++]); |
1153 | } | 1165 | } |
1154 | 1166 | ||
1155 | /* argv[(optind)..(argc-1)] should be names of file to process. If no | 1167 | /* 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) | |||
1176 | } | 1188 | } |
1177 | } | 1189 | } |
1178 | 1190 | ||
1191 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
1192 | destroy_cmd_strs(); | ||
1193 | #endif | ||
1179 | return status; | 1194 | return status; |
1180 | } | 1195 | } |