Split up cat and ls

This commit is contained in:
Ali Mashtizadeh 2022-12-09 23:37:47 -05:00
parent 98a9fc5a6f
commit 88cbddb3ad
7 changed files with 139 additions and 67 deletions

View File

@ -173,8 +173,10 @@ CopyTree('build/include/machine', 'sys/' + env['ARCH'] + '/include', env)
SConscript('sys/SConscript', variant_dir='build/sys')
SConscript('lib/libc/SConscript', variant_dir='build/lib/libc')
#SConscript('lib/liblwip/SConscript', variant_dir='build/lib/liblwip')
SConscript('bin/cat/SConscript', variant_dir='build/bin/cat')
SConscript('bin/ethdump/SConscript', variant_dir='build/bin/ethdump')
SConscript('bin/ethinject/SConscript', variant_dir='build/bin/ethinject')
SConscript('bin/ls/SConscript', variant_dir='build/bin/ls')
SConscript('bin/shell/SConscript', variant_dir='build/bin/shell')
SConscript('sbin/ifconfig/SConscript', variant_dir='build/sbin/ifconfig')
SConscript('sbin/init/SConscript', variant_dir='build/sbin/init')
@ -198,8 +200,10 @@ if env["BOOTDISK"] == "1":
env.Append(BUILDERS = {'BuildImage' : newfs})
bootdisk = env.BuildImage('#build/bootdisk.img', '#release/bootdisk.manifest')
Depends(bootdisk, "#build/tools/newfs_o2fs/newfs_o2fs")
Depends(bootdisk, "#build/bin/cat/cat")
Depends(bootdisk, "#build/bin/ethdump/ethdump")
Depends(bootdisk, "#build/bin/ethinject/ethinject")
Depends(bootdisk, "#build/bin/ls/ls")
Depends(bootdisk, "#build/bin/shell/shell")
Depends(bootdisk, "#build/sbin/ifconfig/ifconfig")
Depends(bootdisk, "#build/sbin/init/init")

23
bin/cat/SConscript Normal file
View File

@ -0,0 +1,23 @@
import sys
Import('env')
init_env = env.Clone()
src = [ ]
src_common = [
"cat.c"
]
src.append(env["CRTBEGIN"])
src.append(src_common)
src.append(env["CRTEND"])
init_env.Append(LINKFLAGS = ['-nostdlib'])
init_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc'])
init_env.Append(CPPPATH = ['#build/include'])
init_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c'])
init_env.Program("cat", src)

40
bin/cat/cat.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Castor Only
#include <syscall.h>
#include <sys/syscall.h>
#include <sys/dirent.h>
int
main(int argc, const char *argv[])
{
int i;
int status, fd;
struct stat sb;
char buf[256];
if (argc != 2) {
fputs("Requires an argument\n", stdout);
return 1;
}
status = OSStat(argv[1], &sb);
if (status != 0) {
fputs("Cannot stat file\n", stdout);
return 0;
}
fd = OSOpen(argv[1], 0);
for (i = 0; i < sb.st_size; i += 256) {
int len = (sb.st_size - i > 256) ? 256 : (sb.st_size - i);
OSRead(fd, &buf, i, len);
OSWrite(1, &buf, 0, len);
}
return 0;
}

23
bin/ls/SConscript Normal file
View File

@ -0,0 +1,23 @@
import sys
Import('env')
init_env = env.Clone()
src = [ ]
src_common = [
"ls.c"
]
src.append(env["CRTBEGIN"])
src.append(src_common)
src.append(env["CRTEND"])
init_env.Append(LINKFLAGS = ['-nostdlib'])
init_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc'])
init_env.Append(CPPPATH = ['#build/include'])
init_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c'])
init_env.Program("ls", src)

47
bin/ls/ls.c Normal file
View File

@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Castor Only
#include <syscall.h>
#include <sys/syscall.h>
#include <sys/dirent.h>
int
main(int argc, const char *argv[])
{
int fd;
int status;
uintptr_t offset = 0;
if (argc != 2) {
fputs("Requires an argument\n", stdout);
return 1;
}
fd = OSOpen(argv[1], 0);
if (fd < 0) {
fputs("Cannot open directory\n", stdout);
return 1;
}
while (1) {
struct dirent de;
status = OSReadDir(fd, (char *)&de, sizeof(de), &offset);
if (status == 0) {
break;
}
if (status < 0) {
fputs("Error\n", stdout);
break;
}
printf("%s\n", de.d_name);
}
return 0;
}

View File

@ -32,13 +32,11 @@ main(int argc, const char *argv[])
void
Cmd_Help(int argc, const char *argv[])
{
printf("cat Print file\n");
printf("bkpt Trigger a kernel breakpoint\n");
printf("date Print current date and time\n");
printf("echo Echo arguments\n");
printf("exit Exit shell\n");
printf("help Display the list of commands\n");
printf("ls List files in a directory\n");
printf("spawn Spawn a process\n");
}
@ -54,67 +52,6 @@ Cmd_Echo(int argc, const char *argv[])
printf("\n");
}
void
Cmd_List(int argc, const char *argv[])
{
int fd;
int status;
uintptr_t offset = 0;
if (argc != 2) {
fputs("Requires an argument\n", stdout);
return;
}
fd = OSOpen(argv[1], 0);
if (fd < 0) {
fputs("Cannot open directory\n", stdout);
return;
}
while (1) {
struct dirent de;
status = OSReadDir(fd, (char *)&de, sizeof(de), &offset);
if (status == 0) {
break;
}
if (status < 0) {
fputs("Error\n", stdout);
break;
}
printf("%s\n", de.d_name);
}
}
void
Cmd_Cat(int argc, const char *argv[])
{
int i;
int status, fd;
struct stat sb;
char buf[256];
if (argc != 2) {
fputs("Requires an argument\n", stdout);
return;
}
status = OSStat(argv[1], &sb);
if (status != 0) {
fputs("Cannot stat file\n", stdout);
return;
}
fd = OSOpen(argv[1], 0);
for (i = 0; i < sb.st_size; i += 256) {
int len = (sb.st_size - i > 256) ? 256 : (sb.st_size - i);
OSRead(fd, &buf, i, len);
OSWrite(1, &buf, 0, len);
}
}
void
Cmd_Date(int argc, const char *argv[])
{
@ -223,16 +160,12 @@ DispatchCommand(char *buf)
Cmd_Help(argc, (const char **)argv);
} else if (strcmp(argv[0], "bkpt") == 0) {
asm volatile("int3");
} else if (strcmp(argv[0], "cat") == 0) {
Cmd_Cat(argc, (const char **)argv);
} else if (strcmp(argv[0], "date") == 0) {
Cmd_Date(argc, (const char **)argv);
} else if (strcmp(argv[0], "echo") == 0) {
Cmd_Echo(argc, (const char **)argv);
} else if (strcmp(argv[0], "exit") == 0) {
exit(0);
} else if (strcmp(argv[0], "ls") == 0) {
Cmd_List(argc, (const char **)argv);
} else if (strcmp(argv[0], "spawn") == 0) {
Cmd_Spawn(argc, (const char **)argv);
} else if (strcmp(argv[0], "#") == 0) {

View File

@ -5,8 +5,10 @@ DIR /
DIR dev
END
DIR bin
FILE cat build/bin/cat/cat
FILE ethdump build/bin/ethdump/ethdump
FILE ethinject build/bin/ethinject/ethinject
FILE ls build/bin/ls/ls
FILE shell build/bin/shell/shell
END
DIR sbin