add: echo
This commit is contained in:
parent
daa6112e59
commit
bb2ea44383
@ -10,7 +10,7 @@ env *init_envs() {
|
|||||||
envs[0].value = "/";
|
envs[0].value = "/";
|
||||||
|
|
||||||
envs[1].key = "USER";
|
envs[1].key = "USER";
|
||||||
envs[1].value = "guams";
|
envs[1].value = "iatic";
|
||||||
|
|
||||||
envs[2].key = "HOST";
|
envs[2].key = "HOST";
|
||||||
envs[2].value = "tartempion";
|
envs[2].value = "tartempion";
|
||||||
|
|||||||
68
src/exec.c
68
src/exec.c
@ -31,15 +31,71 @@ int do_cd(disk *d, char *path) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "env.h"
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int do_echo(char **args) {
|
||||||
|
if (args[0] == NULL) {
|
||||||
|
printf("\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char result[4096] = {0};
|
||||||
|
int pos = 0;
|
||||||
|
|
||||||
|
for (int i = 0; args[i] != NULL; i++) {
|
||||||
|
char *word = args[i];
|
||||||
|
|
||||||
|
for (int j = 0; word[j] != '\0'; j++) {
|
||||||
|
if (word[j] == '$' && word[j + 1] != '\0') {
|
||||||
|
j++;
|
||||||
|
int start = j;
|
||||||
|
|
||||||
|
while (word[j] != '\0' && (isalnum(word[j]) || word[j] == '_')) {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = j - start;
|
||||||
|
if (len > 0) {
|
||||||
|
char key[64];
|
||||||
|
strncpy(key, &word[start], len);
|
||||||
|
key[len] = '\0';
|
||||||
|
|
||||||
|
char *val = get_env_value(key);
|
||||||
|
if (val != NULL) {
|
||||||
|
int val_len = strlen(val);
|
||||||
|
strcpy(&result[pos], val);
|
||||||
|
pos += val_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
j--;
|
||||||
|
} else {
|
||||||
|
result[pos++] = word[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args[i + 1] != NULL) {
|
||||||
|
result[pos++] = ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result[pos] = '\0';
|
||||||
|
printf("%s\n", result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int child_process_job(disk *d, char **args) {
|
int child_process_job(disk *d, char **args) {
|
||||||
if (strcmp("ls", args[0]) == 0) {
|
if (strcmp("ls", args[0]) == 0) {
|
||||||
do_ls(d, args[1]);
|
return do_ls(d, args[1]);
|
||||||
} else if (strcmp("mkdir", args[0]) == 0) {
|
} else if (strcmp("mkdir", args[0]) == 0) {
|
||||||
do_mkdir(d, args[1]);
|
return do_mkdir(d, args[1]);
|
||||||
} else if (strcmp("touch", args[0]) == 0) {
|
} else if (strcmp("touch", args[0]) == 0) {
|
||||||
do_touch(d, args[1]);
|
return do_touch(d, args[1]);
|
||||||
} else if (strcmp("df", args[0]) == 0) {
|
} else if (strcmp("df", args[0]) == 0) {
|
||||||
do_df(d);
|
return do_df(d);
|
||||||
} else {
|
} else {
|
||||||
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
|
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
|
||||||
}
|
}
|
||||||
@ -49,6 +105,8 @@ int child_process_job(disk *d, char **args) {
|
|||||||
int execute_cmd(disk *d, char **args) {
|
int execute_cmd(disk *d, char **args) {
|
||||||
if (strcmp(args[0], "cd") == 0) {
|
if (strcmp(args[0], "cd") == 0) {
|
||||||
return do_cd(d, args[1]);
|
return do_cd(d, args[1]);
|
||||||
|
} else if (strcmp(args[0], "echo") == 0) {
|
||||||
|
return do_echo(&args[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int pid = fork();
|
int pid = fork();
|
||||||
@ -58,7 +116,7 @@ int execute_cmd(disk *d, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
child_process_job(d, args);
|
return child_process_job(d, args);
|
||||||
} else {
|
} else {
|
||||||
int status;
|
int status;
|
||||||
waitpid(pid, &status, WUNTRACED);
|
waitpid(pid, &status, WUNTRACED);
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
int is_builtin_cmd(char *executable);
|
int is_builtin_cmd(char *executable);
|
||||||
int execute_cmd(disk *d, char **args);
|
int execute_cmd(disk *d, char **args);
|
||||||
Loading…
Reference in New Issue
Block a user