@@ -192,22 +192,18 @@ char *lsh_read_line(void)
192
192
*/
193
193
char * * lsh_split_line (char * line )
194
194
{
195
- int bufsize = LSH_TOK_BUFSIZE , position = 0 , toklen ;
196
- char * * tokens = malloc (bufsize * sizeof (char ));
197
- char * token , * token_copy ;
195
+ int bufsize = LSH_TOK_BUFSIZE , position = 0 ;
196
+ char * * tokens = malloc (bufsize * sizeof (char * ));
197
+ char * token ;
198
198
199
199
token = strtok (line , LSH_TOK_DELIM );
200
200
while (token != NULL ) {
201
- toklen = strlen (token );
202
- token_copy = malloc ((toklen + 1 ) * sizeof (char ));
203
- strcpy (token_copy , token );
204
-
205
- tokens [position ] = token_copy ;
201
+ tokens [position ] = token ;
206
202
position ++ ;
207
203
208
- if (position < = bufsize ) {
204
+ if (position > = bufsize ) {
209
205
bufsize += LSH_TOK_BUFSIZE ;
210
- tokens = realloc (tokens , bufsize );
206
+ tokens = realloc (tokens , bufsize * sizeof ( char * ) );
211
207
if (!tokens ) {
212
208
fprintf (stderr , "lsh: allocation error\n" );
213
209
exit (EXIT_FAILURE );
@@ -220,20 +216,6 @@ char **lsh_split_line(char *line)
220
216
return tokens ;
221
217
}
222
218
223
- /**
224
- @brief Free null-terminated array of strings.
225
- @param args List to free.
226
- */
227
- void lsh_free_args (char * * args )
228
- {
229
- char * * iter = args ;
230
- while (* iter != NULL ) {
231
- free (* iter );
232
- iter ++ ;
233
- }
234
- free (args );
235
- }
236
-
237
219
/**
238
220
@brief Loop getting input and executing it.
239
221
*/
@@ -250,7 +232,7 @@ void lsh_loop(void)
250
232
status = lsh_execute (args );
251
233
252
234
free (line );
253
- lsh_free_args (args );
235
+ free (args );
254
236
} while (status );
255
237
}
256
238
0 commit comments