Compare commits

..

3 commits

5 changed files with 24 additions and 12 deletions

View file

@ -3,11 +3,9 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include "parse.h"
unsigned char invoke(struct Args args) { unsigned char invoke(struct Args args) {
pid_t child = fork(); pid_t child = fork();
if (child == 0 && args.argc > 0) { if (child == 0) {
execvp(args.argv[0], args.argv); execvp(args.argv[0], args.argv);
return 1; return 1;
} }

View file

@ -1,7 +1,13 @@
#ifndef COMMAND_H #ifndef COMMAND_H
#define COMMAND_H #define COMMAND_H
#include "parse.h" #include <sys/types.h>
struct Args {
unsigned int argc;
size_t argv_size;
char **argv;
};
unsigned char invoke(struct Args args); unsigned char invoke(struct Args args);

View file

@ -18,6 +18,19 @@ int main() {
struct Args args = parse(input_line); struct Args args = parse(input_line);
free(input_line); free(input_line);
if (args.argc > 0) {
invoke(args); invoke(args);
} }
{
unsigned int i = 0;
while (i < args.argc) {
free(args.argv[i]);
i++;
}
}
if (args.argv != NULL) {
free(args.argv);
}
}
} }

View file

@ -48,6 +48,7 @@ struct Args parse(char *line) {
struct Args args; struct Args args;
args.argc = 0; args.argc = 0;
args.argv_size = 0; args.argv_size = 0;
args.argv = NULL;
parse_worker(&args, line, 0, line); parse_worker(&args, line, 0, line);

View file

@ -1,13 +1,7 @@
#ifndef PARSE_H #ifndef PARSE_H
#define PARSE_H #define PARSE_H
#include <sys/types.h> #include "command.h"
struct Args {
unsigned int argc;
size_t argv_size;
char **argv;
};
void add_arg(struct Args *args, char *arg_ptr, unsigned int arg_length); void add_arg(struct Args *args, char *arg_ptr, unsigned int arg_length);
void parse_worker(struct Args *args, char *arg_position, unsigned int arg_length, char *line); void parse_worker(struct Args *args, char *arg_position, unsigned int arg_length, char *line);