Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ add_subdirectory(cinit)
# Programs loaded from disk
add_subdirectory(foo)
add_subdirectory(bar)
add_subdirectory(init)
add_subdirectory(cat)
add_subdirectory(demo)
add_subdirectory(echo)
add_subdirectory(init)
add_subdirectory(ping)
add_subdirectory(pong)
add_subdirectory(shell)
add_subdirectory(getpid)
add_subdirectory(sleep)
add_subdirectory(time)

message("App targets are ${APPS_TARGETS}")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
set(TARGET getpid)
set(TARGET cat)

add_app(${TARGET})
File renamed without changes.
21 changes: 21 additions & 0 deletions src/apps/cat/src/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stddef.h>

#include "libc/stdio.h"

int main(size_t argc, char ** argv) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return -1;
}

file_t * file = file_open(argv[1], "r");

char c;
while (file_read(file, 1, 1, &c)) {
putc(c);
}

file_close(file);

return 0;
}
4 changes: 2 additions & 2 deletions src/apps/cinit/src/cinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
extern int main(size_t argc, char ** argv);

void __cinit(size_t argc, char ** argv) {
printf("c init\n");
// printf("c init\n");
// TODO init malloc
// TODO is there anything in signals or system calls to setup?
// TODO do stdio handles setup here?
int res = main(argc, argv);
printf("Main returned %d\n", res);
// printf("Main returned %d\n", res);
proc_exit(res);
}
3 changes: 3 additions & 0 deletions src/apps/echo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(TARGET echo)

add_app(${TARGET})
29 changes: 29 additions & 0 deletions src/apps/echo/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ENTRY(__start)

SECTIONS {
. = 0x400000;

.text :
{
*(.text)
}

/* Read-only data. */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}

/* Read-write data (initialized) */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}

/* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}
28 changes: 28 additions & 0 deletions src/apps/echo/src/echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stddef.h>

#include "libc/stdio.h"
#include "libc/string.h"

int main(size_t argc, char ** argv) {
bool next_line = true;
if (argc > 1 && kmemcmp(argv[1], "-n", 2) == 0) {
next_line = false;
}

size_t i = 1;
if (!next_line) {
i++;
}
for (; i < argc; i++) {
puts(argv[i]);
if (i < argc) {
putc(' ');
}
}

if (next_line) {
putc('\n');
}

return 0;
}
7 changes: 0 additions & 7 deletions src/apps/getpid/src/getpid.c

This file was deleted.

86 changes: 0 additions & 86 deletions src/apps/shell/src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@
#include "libc/time.h"
#include "shell.h"

static int echo_cmd(size_t argc, char ** argv) {
bool next_line = true;
if (argc > 1 && kmemcmp(argv[1], "-n", 2) == 0) {
next_line = false;
}

size_t i = 1;
if (!next_line) {
i++;
}
for (; i < argc; i++) {
puts(argv[i]);
if (i < argc) {
putc(' ');
}
}

if (next_line) {
putc('\n');
}

return 0;
}

static int ls_cmd(size_t argc, char ** argv) {
// dir_t dir = dir_open("/");
// if (!dir) {
Expand Down Expand Up @@ -64,74 +40,12 @@ static int ls_cmd(size_t argc, char ** argv) {
return 0;
}

static int cat_cmd(size_t argc, char ** argv) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return -1;
}

file_t * file = file_open(argv[1], "r");

char c;
while (file_read(file, 1, 1, &c)) {
putc(c);
}

file_close(file);

return 0;
}

static int pid_cmd(size_t argc, char ** argv) {
printf("PID is %d\n", getpid());
return 0;
}

static int sleep_cmd(size_t argc, char ** argv) {
if (argc < 2 || (!kstrcmp(argv[1], "-u") && argc < 3)) {
printf("Usage: %s [-u] <seconds>\n", argv[0]);
puts("\nseconds must be at while number that is least 1\n");
puts("-u use microseconds instead of seconds\n");
return -1;
}

if (!kstrcmp(argv[1], "-u")) {
int us = katoi(argv[2]);
if (us < 1) {
printf("Seconds must be a numer that is at least 1\n");
return -1;
}

printf("Sleeping for %d microseconds\n", us);
usleep(us);
}
else {
int seconds = katoi(argv[1]);
if (seconds < 1) {
printf("Seconds must be a numer that is at least 1\n");
return -1;
}

printf("Sleeping for %d sseconds\n", seconds);
sleep(seconds * 1000);
}

puts("Finished sleep\n");

return 0;
}

static int time_cmd(size_t argc, char ** argv) {
printf("The time is %u seconds\n", time());

return 0;
}

void init_commands() {
term_command_add("echo", echo_cmd);
term_command_add("ls", ls_cmd);
term_command_add("cat", cat_cmd);
term_command_add("pid", pid_cmd);
term_command_add("sleep", sleep_cmd);
term_command_add("time", time_cmd);
}
6 changes: 2 additions & 4 deletions src/apps/shell/src/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,12 @@ static void exec_buff() {

// No match was found
else {
int pid = proc_open(argv[0], argc, argv);
// kernel adds filename at argv[0], so here it's a duplicate
int pid = proc_open(argv[0], argc - 1, &argv[1]);
if (pid < 0) {
printf("Unknown command '%s'\n", argv[0]);
term_last_ret = 1;
}
else {
printf("Running command %u\n", pid);
}
}

// Free parsed args
Expand Down
3 changes: 3 additions & 0 deletions src/apps/sleep/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(TARGET sleep)

add_app(${TARGET})
29 changes: 29 additions & 0 deletions src/apps/sleep/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ENTRY(__start)

SECTIONS {
. = 0x400000;

.text :
{
*(.text)
}

/* Read-only data. */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}

/* Read-write data (initialized) */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}

/* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}
39 changes: 39 additions & 0 deletions src/apps/sleep/src/sleep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stddef.h>

#include "libc/stdio.h"
#include "libc/string.h"
#include "libc/time.h"

int main(size_t argc, char ** argv) {
if (argc < 2 || (!kstrcmp(argv[1], "-u") && argc < 3)) {
printf("Usage: %s [-u] <seconds>\n", argv[0]);
puts("\nseconds must be at while number that is least 1\n");
puts("-u use microseconds instead of seconds\n");
return -1;
}

if (!kstrcmp(argv[1], "-u")) {
int us = katoi(argv[2]);
if (us < 1) {
printf("Seconds must be a numer that is at least 1\n");
return -1;
}

printf("Sleeping for %d microseconds\n", us);
usleep(us);
}
else {
int seconds = katoi(argv[1]);
if (seconds < 1) {
printf("Seconds must be a numer that is at least 1\n");
return -1;
}

printf("Sleeping for %d sseconds\n", seconds);
sleep(seconds * 1000);
}

puts("Finished sleep\n");

return 0;
}
3 changes: 3 additions & 0 deletions src/apps/time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(TARGET time)

add_app(${TARGET})
29 changes: 29 additions & 0 deletions src/apps/time/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ENTRY(__start)

SECTIONS {
. = 0x400000;

.text :
{
*(.text)
}

/* Read-only data. */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}

/* Read-write data (initialized) */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}

/* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}
11 changes: 11 additions & 0 deletions src/apps/time/src/time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "libc/time.h"

#include <stddef.h>

#include "libc/stdio.h"

int main(size_t argc, char ** argv) {
printf("The time is %u seconds\n", time());

return 0;
}
Loading