Skip to content

Commit cc49e6c

Browse files
slipstreamslipstream
slipstream
authored and
slipstream
committed
fix history
1 parent 099a380 commit cc49e6c

9 files changed

+20
-16
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"files.associations": {
33
"command_exec.h": "c",
44
"history_log.h": "c",
5-
"pipe_redirect.h": "c"
5+
"pipe_redirect.h": "c",
6+
"string.h": "c",
7+
"stdio.h": "c"
68
}
79
}

build/lite-shell

-41.3 KB
Binary file not shown.

build/obj/command_exec.o

-2 KB
Binary file not shown.

build/obj/history_log.o

-216 Bytes
Binary file not shown.

build/obj/main.o

-632 Bytes
Binary file not shown.

build/obj/pipe_redirect.o

-568 Bytes
Binary file not shown.

include/command_exec.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef COMMAND_EXEC_H
22
#define COMMAND_EXEC_H
33

4-
void handle_command(char** arguments, int arg_count, char** history, int* history_line, char* initial_directory);
4+
void handle_command(char* command, char** arguments, int arg_count, char** history, int* history_line, char* initial_directory);
55

66
#endif

src/command_exec.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
int exec_extern_cmd(char** arguments);
1616
int remove_folder(char* path);
1717

18-
void handle_command(char** arguments, int arg_count, char** history, int* history_line, char* initial_directory) {
18+
void handle_command(char* command, char** arguments, int arg_count, char** history, int* history_line, char* initial_directory) {
1919
char buf[512];
2020
if (strcmp(arguments[0], "pwd2") == 0) {
21-
add_cmd_his(arguments, history, history_line);
21+
add_cmd_his(command, history, history_line);
2222
if (getcwd(buf, sizeof(buf)) == NULL) {
2323
perror("getcwd failed");
2424
log_error(arguments[0], "getcwd failed");
@@ -28,13 +28,13 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
2828
}
2929

3030
if (strcmp(arguments[0], "echo2") == 0) {
31-
add_cmd_his(arguments, history, history_line);
31+
add_cmd_his(command, history, history_line);
3232
printf("%s\n", arguments[1]);
3333
return;
3434
}
3535

3636
if (strcmp(arguments[0], "cd2") == 0) {
37-
add_cmd_his(arguments, history, history_line);
37+
add_cmd_his(command, history, history_line);
3838
if (arg_count < 2) {
3939
arguments[1] = initial_directory;
4040
}
@@ -49,7 +49,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
4949
}
5050

5151
if (strcmp(arguments[0], "ls2") == 0) {
52-
add_cmd_his(arguments, history, history_line);
52+
add_cmd_his(command, history, history_line);
5353
if (arg_count < 2) {
5454
arguments[1] = ".";
5555
}
@@ -75,7 +75,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
7575
}
7676

7777
if (strcmp(arguments[0], "touch2") == 0) {
78-
add_cmd_his(arguments, history, history_line);
78+
add_cmd_his(command, history, history_line);
7979
if (arg_count < 2) {
8080
arguments[1] = "filename";
8181
}
@@ -91,7 +91,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
9191
}
9292

9393
if (strcmp(arguments[0], "cat2") == 0) {
94-
add_cmd_his(arguments, history, history_line);
94+
add_cmd_his(command, history, history_line);
9595
if (arg_count < 2) {
9696
perror("not enough argument");
9797
log_error(arguments[0], "not enough argument");
@@ -118,7 +118,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
118118
}
119119

120120
if (strcmp(arguments[0], "cp2") == 0) {
121-
add_cmd_his(arguments, history, history_line);
121+
add_cmd_his(command, history, history_line);
122122
if (arg_count < 3) {
123123
perror("not enough argument");
124124
log_error(arguments[0], "not enough argument");
@@ -158,7 +158,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
158158
}
159159

160160
if (strcmp(arguments[0], "rename2") == 0) {
161-
add_cmd_his(arguments, history, history_line);
161+
add_cmd_his(command, history, history_line);
162162
if (arg_count < 3) {
163163
perror("not enough argument");
164164
log_error(arguments[0], "not enough argument");
@@ -172,7 +172,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
172172
}
173173

174174
if (strcmp(arguments[0], "rm2") == 0) {
175-
add_cmd_his(arguments, history, history_line);
175+
add_cmd_his(command, history, history_line);
176176
if (arg_count < 2) {
177177
perror("not enough argument");
178178
log_error(arguments[0], "not enough argument");
@@ -201,7 +201,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
201201
for (int i = 0; i < *history_line; i++) {
202202
printf("%s\n", history[i]);
203203
}
204-
add_cmd_his(arguments, history, history_line);
204+
add_cmd_his(command, history, history_line);
205205
return;
206206
}
207207

@@ -211,7 +211,7 @@ void handle_command(char** arguments, int arg_count, char** history, int* histor
211211
printf("Command not found or execution failed: %s\n", arguments[0]);
212212
log_error(arguments[0], "Command not found or execution failed");
213213
} else {
214-
add_cmd_his(arguments, history, history_line);
214+
add_cmd_his(command, history, history_line);
215215
}
216216
}
217217

src/main.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ int main() {
7373

7474
int arg_count = 0;
7575
int insert_point = 0;
76-
char* token = strtok(command, " ");
76+
char command_copy[256]; // 假设最大长度为 256
77+
strcpy(command_copy, command);
78+
char* token = strtok(command_copy, " ");
7779
while (token != NULL && arg_count < MAX_NUM_ARGUMENTS) {
7880
arguments[arg_count++] = token;
7981
token = strtok(NULL, " ");
@@ -108,7 +110,7 @@ int main() {
108110
arg_count = (insert_point + 1);
109111

110112
// Command execution logic
111-
handle_command(arguments, arg_count, history, &history_line, initial_directory);
113+
handle_command(command, arguments, arg_count, history, &history_line, initial_directory);
112114
}
113115

114116
printf("######## Quitting Shelldemo ########\n");

0 commit comments

Comments
 (0)