From 7d7030a2d9d35fc50eaf0c350368f065ec9d3905 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sun, 18 Sep 2022 13:14:30 +0800 Subject: [PATCH] chore: change struct --- .vscode/settings.json | 5 ++++- Makefile | 2 +- line.c | 12 +++--------- line.h | 4 +++- loop.c | 20 ++++++++++++++++++++ loop.h | 4 ++++ main.c | 5 ++--- 7 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 loop.c create mode 100644 loop.h diff --git a/.vscode/settings.json b/.vscode/settings.json index ce6afbf..abc8474 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,9 @@ { "files.associations": { - "stdio.h": "c" + "stdio.h": "c", + "stdlib.h": "c", + "loop.h": "c", + "exec.h": "c" }, "editor.tabSize": 2 } \ No newline at end of file diff --git a/Makefile b/Makefile index 98c32d4..17d7032 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJ=main.o line.o +OBJ=main.o line.o loop.o exec.o CC=gcc CFLAGS= -O3 -Wall diff --git a/line.c b/line.c index d318a5e..16eef5c 100644 --- a/line.c +++ b/line.c @@ -57,6 +57,9 @@ char **splitLine(char *line) { while (token != NULL) { tokens[position] = token; +#ifdef DEBUG + fprintf(stdout, "debug: tokens[%d] = %s\n", position, tokens[position]); +#endif position++; /* if the buffer is full, reallocate */ @@ -76,12 +79,3 @@ char **splitLine(char *line) { tokens[position] = NULL; return tokens; } - -void line() { - char *line; - int status = 1; - char str; - printf("> "); - line = readLine(); - free(line); -} \ No newline at end of file diff --git a/line.h b/line.h index 0bd796a..0d5db46 100644 --- a/line.h +++ b/line.h @@ -1,5 +1,7 @@ #ifndef _LINE_H_ #define _LINE_H_ #define LINE_BUF_SIZE 1024 -void line(); + +char *readLine(void); +char **splitLine(char *line); #endif \ No newline at end of file diff --git a/loop.c b/loop.c new file mode 100644 index 0000000..8f1be78 --- /dev/null +++ b/loop.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include "line.h" +#include "loop.h" +#include "exec.h" + +int loop() { + + char *line; + char **args; + int status = 1; + printf("> "); + line = readLine(); + args = splitLine(line); + status = forkExec(args); + free(line); + free(args); + return 0; +} \ No newline at end of file diff --git a/loop.h b/loop.h new file mode 100644 index 0000000..56ec6fd --- /dev/null +++ b/loop.h @@ -0,0 +1,4 @@ +#ifndef _LOOP_H_ +#define _LOOP_H_ +int loop(); +#endif \ No newline at end of file diff --git a/main.c b/main.c index b4f5958..63696bd 100644 --- a/main.c +++ b/main.c @@ -2,8 +2,7 @@ #include #include #include -#include "line.h" - +#include "loop.h" void intHandler(int dummy) { printf("\nend!\n"); @@ -19,7 +18,7 @@ int main(int argc, char *argv[]) { printf("DEBUG is defined\n"); #endif while (1) { - line(); + loop(); } return 0; } \ No newline at end of file