Skip to content

Commit

Permalink
io/lab: Fix strtok_r usage in Mini-shell
Browse files Browse the repository at this point in the history
Currently the code fails to compile because `strtok_r` is not provided
the `saveptr` argument.

This commit fixes this and synchronizes the changes to the solution. It
also removes the `TODO:` marker from one of the comments as there is
nothing to do.

Signed-off-by: Teodorescu Robert-Andrei <[email protected]>
  • Loading branch information
Teodorescu Robert-Andrei committed Dec 11, 2023
1 parent f7d1cd3 commit da0aebe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 8 additions & 7 deletions content/chapters/io/lab/solution/mini-shell/mini_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ static void simple_cmd(char **args)
pid_t wait_ret;
int status;

/** Create a process to execute the command. Use `execvp` to launch the
/**
* Create a process to execute the command. Use `execvp` to launch the
* new process.
*/
pid = fork();
Expand All @@ -66,9 +67,8 @@ static void simple_cmd(char **args)
/* Child process */

/**
* TODO: Call `do_redirect()`. Assume the only file descriptor
* that needs to be redirected is `stdout`. Its file descriptor
* is `STDOUT_FILENO`.
* Call `do_redirect()`. Assume the only file descriptor that needs to
* be redirected is `stdout`. Its file descriptor is `STDOUT_FILENO`.
*/
if (stdout_file != NULL)
do_redirect(STDOUT_FILENO, stdout_file);
Expand Down Expand Up @@ -140,6 +140,7 @@ static int parse_line(char *line)
int idx = 0;
char *token;
char *delim = "=\n";
char *saveptr = NULL;

stdin_file = NULL;
stdout_file = NULL;
Expand All @@ -151,7 +152,7 @@ static int parse_line(char *line)

/* Regular command. */
delim = " \t\n";
token = strtok(line, delim);
token = strtok_r(line, delim, &saveptr);

if (token == NULL)
return ERROR;
Expand All @@ -170,14 +171,14 @@ static int parse_line(char *line)
token++;
stdout_file = strdup(token);
} else {
token = strtok(NULL, delim);
token = strtok_r(NULL, delim, &saveptr);
stdout_file = strdup(token);
}
} else {
args[idx++] = strdup(token);
}

token = strtok(NULL, delim);
token = strtok_r(NULL, delim, &saveptr);
}

args[idx++] = NULL;
Expand Down
14 changes: 7 additions & 7 deletions content/chapters/io/lab/support/mini-shell/mini_shell.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* SPDX-License-Identifier: BSD-3-Clause */
// SPDX-License-Identifier: BSD-3-Clause

#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -58,9 +58,8 @@ static void simple_cmd(char **args)
/* Child process */

/**
* TODO: Call `do_redirect()`. Assume the only file descriptor
* that needs to be redirected is `stdout`. Its file descriptor
* is `STDOUT_FILENO`.
* Call `do_redirect()`. Assume the only file descriptor that needs to
* be redirected is `stdout`. Its file descriptor is `STDOUT_FILENO`.
*/
if (stdout_file != NULL)
do_redirect(STDOUT_FILENO, stdout_file);
Expand Down Expand Up @@ -132,6 +131,7 @@ static int parse_line(char *line)
int idx = 0;
char *token;
char *delim = "=\n";
char *saveptr = NULL;

stdin_file = NULL;
stdout_file = NULL;
Expand All @@ -143,7 +143,7 @@ static int parse_line(char *line)

/* Regular command. */
delim = " \t\n";
token = strtok_r(line, delim);
token = strtok_r(line, delim, &saveptr);

if (token == NULL)
return ERROR;
Expand All @@ -162,14 +162,14 @@ static int parse_line(char *line)
token++;
stdout_file = strdup(token);
} else {
token = strtok_r(NULL, delim);
token = strtok_r(NULL, delim, &saveptr);
stdout_file = strdup(token);
}
} else {
args[idx++] = strdup(token);
}

token = strtok_r(NULL, delim);
token = strtok_r(NULL, delim, &saveptr);
}

args[idx++] = NULL;
Expand Down

0 comments on commit da0aebe

Please sign in to comment.