OS: fixed segfault.

parent 5a861f69
...@@ -19,9 +19,20 @@ ...@@ -19,9 +19,20 @@
#include "input.lex.h" #include "input.lex.h"
#include "input.l.h" #include "input.l.h"
/*
* Buffer size of path concatenation.
*/
#define PATH_LENGTH 1024 #define PATH_LENGTH 1024
/*
* When the shell is attached to a terminal, this value is true. If the shell
* is attached to a pipe, this value is false.
*/
int is_interactive = 0; int is_interactive = 0;
/*
* Toggle debug messages. Only useful dring the development.
*/
int is_debug_mode = 1; int is_debug_mode = 1;
/* /*
...@@ -49,7 +60,6 @@ inline int base10len(unsigned int n) { ...@@ -49,7 +60,6 @@ inline int base10len(unsigned int n) {
return len + 1; return len + 1;
} }
/* /*
* Display a debug message, when running in debug mode, in stderr. * Display a debug message, when running in debug mode, in stderr.
*/ */
...@@ -133,6 +143,9 @@ shell_args *shell_pop_args() { ...@@ -133,6 +143,9 @@ shell_args *shell_pop_args() {
return args; return args;
} }
/*
* Execute the binary and its parsed arguments.
*/
void shell_exec(shell_args *args) { void shell_exec(shell_args *args) {
assert(args && args->arg); assert(args && args->arg);
shell_debug("exec `%s'", args->arg); shell_debug("exec `%s'", args->arg);
...@@ -177,10 +190,11 @@ void shell_exec_process(shell_args *args) { ...@@ -177,10 +190,11 @@ void shell_exec_process(shell_args *args) {
// Convert argument structure to list of strings. And add a NULL pointer as // Convert argument structure to list of strings. And add a NULL pointer as
// a marker for the end of the list. The first string should point to the // a marker for the end of the list. The first string should point to the
// binary being executed. // binary being executed.
assert(argc >= 1);
char **argv = malloc((argc+1) * sizeof(char *)); char **argv = malloc((argc+1) * sizeof(char *));
argv[0] = bin; argv[0] = bin;
shell_args *cur = args + sizeof(shell_args *); shell_args *cur = args + sizeof(shell_args *);
for( int a = 0; a < argc && cur; cur = args->next, a++) for( int a = 1; a < argc && cur; cur = args->next, a++)
argv[a] = strdup(cur->arg); argv[a] = strdup(cur->arg);
argv[argc] = NULL; argv[argc] = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment