Permalink
Browse files

refactor parse_shell_path() to tokenize like parse_command_line()

  • Loading branch information...
1 parent 70b2555 commit d09a1d4cec75fe094686ab106ef0ac991911c3cd @koblenski committed Jan 11, 2017
Showing with 12 additions and 10 deletions.
  1. +12 −10 shell.cpp
View
@@ -61,25 +61,27 @@ char **parse_shell_path() {
char *shell_path = getenv("SHELLPATH");
if (shell_path == NULL) return NULL;
- int path_count = count_char(shell_path, ':') + 1;
+ char *shell_path_dup = (char *)malloc((strlen(shell_path) + 1) * sizeof(char));
+ if (shell_path_dup == NULL) {
+ puts("ERROR: Out of memory in parse_shell_path() allocating shell_path_dup");
+ exit(EXIT_FAILURE);
+ }
+ strcpy(shell_path_dup, shell_path);
+
+ int path_count = count_char(shell_path_dup, ':') + 1;
char **command_paths = (char **)malloc((path_count + 1) * sizeof(char *));
if (command_paths == NULL) {
puts("ERROR: Out of memory in parse_shell_path() allocating command_paths");
exit(EXIT_FAILURE);
}
int i = 0;
- for (char *path = strtok(shell_path, ":"); path != NULL; path = strtok(NULL, ":"), i++) {
- command_paths[i] = (char *)malloc(strlen(path) * sizeof(char));
- if (command_paths[i] == NULL) {
- printf("ERROR: Out of memory in parse_shell_path() allocating command_paths[%d]\n", i);
- exit(EXIT_FAILURE);
- }
- strcpy(command_paths[i], path);
+ for (command_paths[i] = strtok(shell_path_dup, ":");
+ command_paths[i] != NULL;
+ command_paths[i] = strtok(NULL, ":")) {
+ i++;
}
- command_paths[i] = NULL;
-
return command_paths;
}

0 comments on commit d09a1d4

Please sign in to comment.