Browse Source

shell and ls wip

master
Mathieu Serandour 4 months ago
parent
commit
8d9ced9135
  1. 5
      programs/Makefile
  2. 37
      programs/ls.c
  3. 41
      programs/shell.c

5
programs/Makefile

@ -13,7 +13,7 @@ CFLAGS := -O3 -fno-inline -mno-red-zone -mgeneral-regs-only \
CFILES :=$(shell find -name "*.c")
EFILES := $(CFILES:.c=.elf)
BIN_DIR := ../disk_root/bin/
debug: CFLAGS += -g
debug: LDFLAGS += -g
@ -22,7 +22,8 @@ debug: all
release: all
all: $(EFILES)
cp *.elf ../disk_root/bin/
cp shell.elf $(BIN_DIR)/sh
cp ls.elf $(BIN_DIR)/ls
%.elf: %.c ../blib/blib.a

37
programs/ls.c

@ -2,7 +2,44 @@
#include <stdio.h>
typedef struct DIR {
int fd;
// number of elements
size_t len;
// next element to be
// read by vfs_readdir
size_t cur;
struct dirent dirent[0];
// struct dirent dirent_buf;
} DIR;
int main(int argc, char** argv) {
DIR* dir;
struct dirent* ent;
if (argc == 1) {
dir = opendir(".");
} else {
dir = opendir(argv[1]);
}
if (dir == NULL) {
printf("cannot open directory\n");
return 1;
}
while ((ent = readdir(dir)) != NULL) {
printf("%s\t", ent->d_name);
}
printf("\n");
closedir(dir);
return 0;
}

41
programs/shell.c

@ -33,6 +33,8 @@ const char* version_string =
"type help to list commands\n\n"
;
static char* cwd = NULL;
/*
int print_input_end() {
@ -82,13 +84,29 @@ void print_cow(void) {
close(file);
}
static void cd(const char* path) {
chdir(path);
free(cwd);
cwd = getcwd(NULL, 0);
}
/**
* execute the builtin command
* if it is one,
* else return 0
*/
static int builtin_cmd(const char* cmd) {
if(strcmp(cmd, "help") == 0)
static int builtin_cmd(const char** argv) {
if(strcmp(argv[0], "cd") == 0) {
if(!argv[1]) {
printf("cd: missing argument\n");
return 1;
}
cd(argv[1]);
}
else if(strcmp(argv[0], "help") == 0)
printf("\n"
"help\n"
"\tlist all commands\n"
@ -103,11 +121,11 @@ static int builtin_cmd(const char* cmd) {
"\tprint a cow\n"
"\n"
"\n");
else if(strcmp(cmd, "version") == 0)
else if(strcmp(argv[0], "version") == 0)
printf(version_string);
else if(strcmp(cmd, "cow") == 0)
else if(strcmp(argv[0], "cow") == 0)
print_cow();
else if(strcmp(cmd, "exit") == 0)
else if(strcmp(argv[0], "exit") == 0)
exit(0);
else
return 0;
@ -134,15 +152,21 @@ static char** convert_cmdline(char* cmdline) {
static void execute(char* cmd) {
if(builtin_cmd(cmd))
return;
// convert to argv
char** argv = convert_cmdline(cmd);
if(!argv[0])
return;
if(builtin_cmd(argv))
return;
// execute
int ret = forkexec(argv);
if(ret)
{
// an error occured.
@ -161,7 +185,6 @@ static void execute(char* cmd) {
}
static char* cwd = NULL;
void print_prompt(void) {
printf("%s > _\b", cwd);

Loading…
Cancel
Save