@@ -22,32 +22,34 @@ ParsedCommand *parse_command(char *input) {
2222 char * input_copy = strdup (input );
2323 if (!input_copy ) return NULL ;
2424
25+ char * original_input_copy = input_copy ;
2526 input_copy = trim_whitespace (input_copy );
2627 if (strlen (input_copy ) == 0 ) {
2728 PARSER_DEBUG ("Empty command after trimming" );
28- free (input_copy );
29+ free (original_input_copy );
30+ original_input_copy = NULL ;
2931 return NULL ;
3032 }
3133
3234 ParsedCommand * cmd = calloc (1 , sizeof (ParsedCommand ));
3335 if (!cmd ) {
3436 PARSER_DEBUG ("Failed to allocate ParsedCommand" );
35- free (input_copy );
37+ free (original_input_copy );
38+ original_input_copy = NULL ;
3639 return NULL ;
3740 }
3841
3942 // Use calloc to ensure all entries are initialized to NULL
4043 cmd -> args = calloc (MAX_ARGS , sizeof (char * ));
4144 if (!cmd -> args ) {
4245 PARSER_DEBUG ("Failed to allocate args array" );
43- free (input_copy );
46+ free (original_input_copy );
47+ original_input_copy = NULL ;
4448 free (cmd );
4549 return NULL ;
4650 }
47-
4851 int arg_count = 0 ;
4952 char * token , * saveptr = NULL ;
50-
5153 // Tokenize and process input
5254 token = strtok_r (input_copy , " \t" , & saveptr );
5355 while (token != NULL && arg_count < MAX_ARGS - 1 ) {
@@ -81,13 +83,17 @@ ParsedCommand *parse_command(char *input) {
8183 // Get next token
8284 token = strtok_r (NULL , " \t" , & saveptr );
8385 }
84-
8586 // Ensure NULL termination
8687 cmd -> args [arg_count ] = NULL ;
8788
8889 PARSER_DEBUG ("Command parsed with %d arguments" , arg_count );
89-
90- free (input_copy );
90+
91+ if (original_input_copy ){
92+ free (original_input_copy );
93+ original_input_copy = NULL ;
94+ }
95+
96+
9197 return cmd ;
9298}
9399
@@ -103,4 +109,4 @@ void free_parsed_command(ParsedCommand *cmd) {
103109 free (cmd -> input_file );
104110 free (cmd -> output_file );
105111 free (cmd );
106- }
112+ }
0 commit comments