From c017ff902351e69e77376a0fb3bcbc0a05af9238 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 7 Apr 2020 23:40:37 +0300 Subject: [PATCH] Bump version --- Makefile | 48 ++++++++++++++------- ase/ase.c | 30 ++++++++++--- core/bin/date.c | 11 ++--- core/bin/hexd.c | 6 +-- core/bin/login.c | 28 +++++++----- core/bin/ls.c | 12 +++--- core/bin/mount.c | 18 ++++---- core/bin/mouse.c | 105 ++++++++++++++++++++++++--------------------- core/bin/netdump.c | 29 ++++++++++--- core/bin/ping.c | 16 +++++++ init.c | 5 +-- sh/builtin.c | 29 +++++++------ sh/cmd.c | 4 +- sh/readline.c | 4 ++ sh/sh.c | 13 +++--- 15 files changed, 224 insertions(+), 134 deletions(-) create mode 100644 core/bin/ping.c diff --git a/Makefile b/Makefile index 3e31876..77bb80b 100644 --- a/Makefile +++ b/Makefile @@ -11,25 +11,35 @@ DIRS=$(O) \ $(O)/vsh HDRS=$(shell find $(S) -type f -name "*.h") STAGE_BIN=$(STAGE)/init \ + $(STAGE)/bin/ls \ + $(STAGE)/bin/sh \ $(STAGE)/bin/hexd \ - $(STAGE)/bin/date \ $(STAGE)/bin/uname \ + $(STAGE)/bin/rm \ + $(STAGE)/bin/mkdir \ $(STAGE)/bin/mount \ $(STAGE)/bin/umount \ - $(STAGE)/bin/login \ - $(STAGE)/bin/sh \ - $(STAGE)/bin/ls \ - $(STAGE)/bin/rm \ - $(STAGE)/bin/reboot \ - $(STAGE)/bin/mkdir \ - $(STAGE)/bin/netctl \ - $(STAGE)/bin/netmeow \ - $(STAGE)/bin/netdump \ - $(STAGE)/bin/mouse \ - $(STAGE)/bin/vsh + $(STAGE)/bin/date \ + $(STAGE)/bin/login + +# TODO +# newlib: gettimeofday is broken? + +# newlib: port mount()/umount() +# newlib: port reboot() + # $(STAGE)/bin/reboot \ + + + # $(STAGE)/bin/netctl \ + # $(STAGE)/bin/netmeow \ + # $(STAGE)/bin/netdump \ + # $(STAGE)/bin/ping \ + + # $(STAGE)/bin/mouse \ + # $(STAGE)/bin/ase \ + # $(STAGE)/bin/vsh # $(STAGE)/bin/com \ -# $(STAGE)/bin/ase \ # $(STAGE)/bin/su \ sh_OBJS=$(O)/sh/sh.o \ @@ -46,19 +56,25 @@ vsh_OBJS=$(O)/vsh/vsh.o \ usr_CFLAGS=-ggdb \ -msse \ -msse2 \ - -O2 \ + -O0 \ -Wall \ -Werror \ + -Wno-char-subscripts \ -ffreestanding usr_LDFLAGS=-lgcc \ -static -all: mkdirs $(O)/initrd.img +all: mkdirs lua $(O)/initrd.img clean: rm -rf $(O) + make -j5 -C ../lua clean +lua: + make -C ../lua yggdrasil + mkdir -p $(STAGE)/bin + cp ../lua/lua $(STAGE)/bin/lua #chmod 04711 $(STAGE)/bin/su $(O)/initrd.img: mkstage-etc $(STAGE_BIN) @@ -68,7 +84,7 @@ mkdirs: mkdir -p $(DIRS) mkstage-etc: - mkdir -p $(STAGE)/dev $(STAGE)/mnt $(STAGE)/bin + mkdir -p $(STAGE)/dev $(STAGE)/mnt $(STAGE)/bin $(STAGE)/sys cp -r etc $(STAGE) # Application building diff --git a/ase/ase.c b/ase/ase.c index 573d195..bfc03ae 100644 --- a/ase/ase.c +++ b/ase/ase.c @@ -1,9 +1,11 @@ #include #include +#include #include #include #include #include +#include #include #define MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -20,6 +22,7 @@ #define MODE_COMMAND 1 #define MODE_INSERT 2 +static struct termios tc, tc_old; static int term_width = 0; static int term_height = 0; @@ -360,13 +363,13 @@ static void ase_line_insert(struct ase_buffer *buf, int br) { buf->c_col = 0; } else { - //for (ssize_t i = buf->line_count; i > buf->c_row; --i) { - // buf->lines[i].length = buf->lines[i - 1].length; - // memcpy(buf->lines[i].text_data, buf->lines[i - 1].text_data, buf->lines[i - 1].length); - //} - //buf->lines[buf->c_row].length = 0; - //buf->c_col = 0; - //++buf->line_count; + for (ssize_t i = buf->line_count; i > buf->c_row; --i) { + buf->lines[i].length = buf->lines[i - 1].length; + memcpy(buf->lines[i].text_data, buf->lines[i - 1].text_data, buf->lines[i - 1].length); + } + buf->lines[buf->c_row].length = 0; + buf->c_col = 0; + ++buf->line_count; } } @@ -408,6 +411,7 @@ static void key_insert(struct ase_buffer *buf, int ch) { static void cmd_exec(struct ase_buffer *buf) { if (!strcmp(buf->cmd_buf, "q")) { + tcsetattr(STDIN_FILENO, TCSANOW, &tc_old); printf("\033[2J\033[1;1f"); exit(0); } else if (!strncmp(buf->cmd_buf, "e ", 2)) { @@ -434,6 +438,18 @@ int main(int argc, char **argv) { term_width = ws.ws_col; term_height = ws.ws_row; + if (tcgetattr(STDIN_FILENO, &tc_old) != 0) { + perror("Failed to get terminal settings"); + return -1; + } + memcpy(&tc, &tc_old, sizeof(struct termios)); + tc.c_lflag = 0; + tc.c_iflag &= ~ICANON; + if (tcsetattr(STDIN_FILENO, TCSANOW, &tc) != 0) { + perror("Failed to set terminal settings"); + return -1; + } + buf.mode = MODE_NORMAL; buf.cmd_len = 0; diff --git a/core/bin/date.c b/core/bin/date.c index 938ee22..082a5f9 100644 --- a/core/bin/date.c +++ b/core/bin/date.c @@ -1,16 +1,17 @@ +#include +#include +#include #include #include int main(int argc, char **argv) { - // Ignore arguments - struct timeval tv0; + char buf[256]; gettimeofday(&tv0, NULL); struct tm tm0; gmtime_r(&tv0.tv_sec, &tm0); - printf("%04u-%02u-%02u %02u:%02u:%02u\n", - tm0.tm_year, tm0.tm_mon, tm0.tm_mday, - tm0.tm_hour, tm0.tm_min, tm0.tm_sec); + strftime(buf, sizeof(buf), "%c\n", &tm0); + write(STDOUT_FILENO, buf, strlen(buf)); return 0; } diff --git a/core/bin/hexd.c b/core/bin/hexd.c index 0790d57..c745d10 100644 --- a/core/bin/hexd.c +++ b/core/bin/hexd.c @@ -7,12 +7,12 @@ #define LINE_LENGTH 16 static void line_print(size_t off, const char *line, size_t len) { - printf("%08x: ", off); + printf("%08zx: ", off); for (size_t i = 0; i < LINE_LENGTH; ++i) { // XXX: This is needed because I didn't implement h/hh modifiers in printf - uint64_t byte = (uint64_t) line[i] & 0xFF; + uint8_t byte = line[i]; if (i < len) { - printf("%02x", byte); + printf("%02hhx", byte); } else { printf(" "); } diff --git a/core/bin/login.c b/core/bin/login.c index 877364e..7e6706a 100644 --- a/core/bin/login.c +++ b/core/bin/login.c @@ -5,12 +5,14 @@ #include #include #include -#include +#include #include #include #include #include +#include + struct spwd { char *sp_namp; char *sp_pwdp; @@ -62,12 +64,13 @@ static ssize_t getline(char *buf, size_t lim, int echo) { } static int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t buf_size) { - int fd = open("/etc/shadow", O_RDONLY, 0); - if (fd < 0) { - return fd; + int fp = open("/etc/shadow", O_RDONLY, 0); + if (fp < 0) { + return -1; } - while (gets_safe(fd, buf, buf_size) > 0) { + // TODO POSIX + while (gets_safe(fp, buf, buf_size)) { char *p0 = strchr(buf, ':'); if (!p0) { break; @@ -84,13 +87,13 @@ static int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t buf_s continue; } - close(fd); + close(fp); sp->sp_namp = buf; sp->sp_pwdp = p0 + 1; return 0; } - close(fd); + close(fp); errno = ENOENT; return -1; } @@ -105,7 +108,7 @@ static int loginuid(uid_t uid, gid_t gid, const char *sh) { if (sh_pid == 0) { if (setuid(uid) || setgid(gid)) { perror("login"); - exit(-1); + _exit(-1); } setpgid(0, 0); @@ -113,10 +116,10 @@ static int loginuid(uid_t uid, gid_t gid, const char *sh) { ioctl(STDIN_FILENO, TIOCSPGRP, &pid); const char *argp[] = { sh, NULL }; - exit(execve(sh, (char *const *) argp, NULL)); + _exit(execve(sh, (char *const *) argp, NULL)); } else { int st; - waitpid(sh_pid, &st); + waitpid(sh_pid, &st, 0); // Regain control of the terminal int pgid = getpgid(0); ioctl(STDIN_FILENO, TIOCSPGRP, &pgid); @@ -173,11 +176,13 @@ int main(int argc, char **argv) { while (1) { if (attempt == 3) { - puts2("\033[2J\033[1;1f"); + printf("\033[2J\033[1;1f"); + fflush(stdout); attempt = 0; } printf("login: "); + fflush(stdout); if (getline(line_buf, sizeof(line_buf), 1) < 0) { break; } @@ -190,6 +195,7 @@ int main(int argc, char **argv) { if (sp.sp_pwdp[0] != 0) { printf("password: "); + fflush(stdout); if (getline(line_buf, sizeof(line_buf), 0) < 0) { ++attempt; continue; diff --git a/core/bin/ls.c b/core/bin/ls.c index bd59a42..d68fbee 100644 --- a/core/bin/ls.c +++ b/core/bin/ls.c @@ -111,21 +111,21 @@ static int ls_dir(const char *path, int flags) { break; case DT_BLK: case DT_CHR: - write(STDOUT_FILENO, COLOR_DEV, 5); + fputs(COLOR_DEV, stdout); break; case DT_DIR: - write(STDOUT_FILENO, COLOR_DIR, 5); + fputs(COLOR_DIR, stdout); break; default: - write(STDOUT_FILENO, COLOR_UNKNOWN, 5); + fputs(COLOR_UNKNOWN, stdout); break; } } - write(STDOUT_FILENO, ent->d_name, strlen(ent->d_name)); + fputs(ent->d_name, stdout); if (flags & LS_COLOR) { - write(STDOUT_FILENO, COLOR_RESET, 4); + fputs(COLOR_RESET, stdout); } - putchar('\n'); + fputc('\n', stdout); } } diff --git a/core/bin/mount.c b/core/bin/mount.c index 29821e0..8be2375 100644 --- a/core/bin/mount.c +++ b/core/bin/mount.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -91,15 +92,16 @@ int main(int argc, char **argv) { } // Check that "dst" is a directory - if ((res = stat(dst, &st)) != 0) { - perror(dst); - return -1; - } + // TODO: stat() is fucked + //if ((res = stat(dst, &st)) != 0) { + // perror(dst); + // return -1; + //} - if ((st.st_mode & S_IFMT) != S_IFDIR) { - printf("%s: Not a directory\n", dst); - return -1; - } + //if ((st.st_mode & S_IFMT) != S_IFDIR) { + // printf("%s: Not a directory\n", dst); + // return -1; + //} // Do the magic stuff if ((res = mount(src, dst, type, mount_flags, NULL)) != 0) { diff --git a/core/bin/mouse.c b/core/bin/mouse.c index d85b556..307446b 100644 --- a/core/bin/mouse.c +++ b/core/bin/mouse.c @@ -9,9 +9,9 @@ static int running = 1; -static int mx = 40; -static int my = 12; -static double mx_d = 40, my_d = 12; +//static int mx = 40; +//static int my = 12; +//static double mx_d = 40, my_d = 12; static int con_width, con_height; @@ -19,47 +19,47 @@ static void signal_handle(int signum) { running = 0; } -static void kb_handle(char *buf, size_t len) { - if (buf[0] == 'q') { - running = 0; - } -} - -static void ms_handle(char *buf, size_t len) { - for (size_t i = 0; i < len / 5; ++i) { - char type = buf[i * 5 + 0]; - - switch (type) { - case 'd': { - int16_t dx = *(int16_t *) &buf[i * 5 + 1]; - int16_t dy = *(int16_t *) &buf[i * 5 + 3]; - - mx_d += (double) dx / 4.0; - my_d -= (double) dy / 12.0; - - if (mx_d < 0) { - mx_d = 0; - } - if (mx_d >= con_width - 1) { - mx_d = con_width - 2; - } - - if (my_d < 0) { - my_d = 0; - } - if (my_d >= con_height - 1) { - my_d = con_height - 2; - } - - mx = mx_d; - my = my_d; - puts2("\033[2J\033[1;1f"); - printf("\033[%d;%dfX\033[%d;%df", my + 1, mx + 1, my + 1, mx + 1); - } - break; - } - } -} +//static void kb_handle(char *buf, size_t len) { +// if (buf[0] == 'q') { +// running = 0; +// } +//} +// +//static void ms_handle(char *buf, size_t len) { +// for (size_t i = 0; i < len / 5; ++i) { +// char type = buf[i * 5 + 0]; +// +// switch (type) { +// case 'd': { +// int16_t dx = *(int16_t *) &buf[i * 5 + 1]; +// int16_t dy = *(int16_t *) &buf[i * 5 + 3]; +// +// mx_d += (double) dx / 4.0; +// my_d -= (double) dy / 12.0; +// +// if (mx_d < 0) { +// mx_d = 0; +// } +// if (mx_d >= con_width - 1) { +// mx_d = con_width - 2; +// } +// +// if (my_d < 0) { +// my_d = 0; +// } +// if (my_d >= con_height - 1) { +// my_d = con_height - 2; +// } +// +// mx = mx_d; +// my = my_d; +// puts2("\033[2J\033[1;1f"); +// printf("\033[%d;%dfX\033[%d;%df", my + 1, mx + 1, my + 1, mx + 1); +// } +// break; +// } +// } +//} int main(int argc, char **argv) { fd_set fds; @@ -68,7 +68,7 @@ int main(int argc, char **argv) { struct winsize winsz; int kb_fd = STDIN_FILENO, ms_fd; char buf[512]; - ssize_t len; + //ssize_t len; signal(SIGINT, signal_handle); @@ -109,13 +109,20 @@ int main(int argc, char **argv) { if (res != 0) { if (FD_ISSET(ms_fd, &fds)) { - len = read(ms_fd, buf, sizeof(buf)); - ms_handle(buf, len); + read(ms_fd, buf, sizeof(buf)); + printf("Mouse\n"); + //ms_handle(buf, len); } if (FD_ISSET(kb_fd, &fds)) { - len = read(kb_fd, buf, sizeof(buf)); - kb_handle(buf, len); + read(kb_fd, buf, sizeof(buf)); + printf("Keyboard\n"); + //kb_handle(buf, len); + if (buf[0] == 'q') { + break; + } } + } else { + printf("Timeout\n"); } } diff --git a/core/bin/netdump.c b/core/bin/netdump.c index b1703ac..8da9ddb 100644 --- a/core/bin/netdump.c +++ b/core/bin/netdump.c @@ -1,5 +1,6 @@ #include #include +#include #include #define LINE_LENGTH 16 @@ -54,6 +55,9 @@ int main(int argc, char **argv) { size_t salen; char buf[4096]; int fd = socket(AF_PACKET, SOCK_RAW, 0); + fd_set fds; + struct timeval tv; + int res; if (fd < 0) { perror("socket()"); @@ -61,14 +65,29 @@ int main(int argc, char **argv) { } while (1) { - ssize_t len = recvfrom(fd, buf, sizeof(buf), &sa, &salen); + FD_ZERO(&fds); + FD_SET(fd, &fds); + tv.tv_sec = 1; + tv.tv_usec = 0; - if (len < 0) { - perror("recvfrom()"); - break; + res = select(fd + 1, &fds, NULL, NULL, &tv); + + if (res < 0) { + perror("select()"); } - packet_dump(buf, len); + if (res > 0) { + ssize_t len = recvfrom(fd, buf, sizeof(buf), &sa, &salen); + + if (len < 0) { + perror("recvfrom()"); + break; + } + + packet_dump(buf, len); + } else { + printf("Timed out\n"); + } } return 0; diff --git a/core/bin/ping.c b/core/bin/ping.c new file mode 100644 index 0000000..e90ee00 --- /dev/null +++ b/core/bin/ping.c @@ -0,0 +1,16 @@ +#include +#include +#include + +int main(int argc, char **argv) { + int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); + + if (sock < 0) { + perror("socket()"); + return -1; + } + + close(sock); + + return 0; +} diff --git a/init.c b/init.c index 5347209..f19a3ba 100644 --- a/init.c +++ b/init.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -27,8 +26,8 @@ int main(int argc, char **argv) { mount(NULL, "/sys", "sysfs", 0, NULL); mount("/dev/sda1", "/mnt", NULL, 0, NULL); - uint32_t inaddr = 0x0A000001; - netctl("eth0", NETCTL_SET_INADDR, &inaddr); + //uint32_t inaddr = 0x0A000001; + //netctl("eth0", NETCTL_SET_INADDR, &inaddr); return start_login(); } diff --git a/sh/builtin.c b/sh/builtin.c index 03de0ec..ef9449e 100644 --- a/sh/builtin.c +++ b/sh/builtin.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "builtin.h" #include "config.h" @@ -101,19 +102,19 @@ DEF_BUILTIN(stat) { return -1; } - printf("device %u\n", st.st_dev); - printf("inode %u\n", st.st_ino); - printf("mode %u\n", st.st_mode); - printf("nlink %u\n", st.st_nlink); - printf("uid %u\n", st.st_uid); - printf("gid %u\n", st.st_gid); - printf("rdev %u\n", st.st_rdev); - printf("size %u\n", st.st_size); - printf("atime %u\n", st.st_atime); - printf("mtime %u\n", st.st_mtime); - printf("ctime %u\n", st.st_ctime); - printf("blksize %u\n", st.st_blksize); - printf("blocks %u\n", st.st_blocks); + printf("device %u\n", st.st_dev); + printf("inode %u\n", st.st_ino); + printf("mode %u\n", st.st_mode); + printf("nlink %u\n", st.st_nlink); + printf("uid %u\n", st.st_uid); + printf("gid %u\n", st.st_gid); + printf("rdev %u\n", st.st_rdev); + printf("size %u\n", st.st_size); + printf("atime %u\n", st.st_atime); + printf("mtime %u\n", st.st_mtime); + printf("ctime %u\n", st.st_ctime); + printf("blksize %u\n", st.st_blksize); + printf("blocks %u\n", st.st_blocks); return 0; } @@ -193,7 +194,7 @@ DEF_BUILTIN(exec) { } DEF_BUILTIN(clear) { - puts2("\033[2J\033[1;1f"); + printf("\033[2J\033[1;1f"); return 0; } diff --git a/sh/cmd.c b/sh/cmd.c index 584fb98..7bc5a83 100644 --- a/sh/cmd.c +++ b/sh/cmd.c @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include #include #include @@ -65,7 +67,7 @@ static int cmd_spawn(const char *path, const struct cmd_exec *cmd, int *cmd_res) setpgid(0, 0); exit(execve(path, (char *const *) cmd->args, NULL)); } else { - if (waitpid(pid, cmd_res) != 0) { + if (waitpid(pid, cmd_res, 0) != 0) { perror("waitpid()"); } diff --git a/sh/readline.c b/sh/readline.c index c5d9a64..bae5fc7 100644 --- a/sh/readline.c +++ b/sh/readline.c @@ -1,4 +1,5 @@ //#include +#include #include //#define KEY_UP (256) @@ -91,6 +92,9 @@ int readline(char *buf, size_t lim) { buf[len - 1] = 0; } + //printf("Got len = %ld\n", len); + //while (1); + //int len = 0; //int cur = 0; //int chr; diff --git a/sh/sh.c b/sh/sh.c index c97970c..eb606f0 100644 --- a/sh/sh.c +++ b/sh/sh.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -21,7 +22,7 @@ static void update_prompt(void) { struct utsname _u; struct passwd _p; struct passwd *p; - char pwbuf[512]; + char pwbuf[256]; p_uid = getuid(); @@ -32,11 +33,9 @@ static void update_prompt(void) { if (uname(&_u) != 0) { perror("uname()"); - p_hostname[0] = 0; } else { strcpy(p_hostname, _u.nodename); } - if (getpwuid_r(p_uid, &_p, pwbuf, sizeof(pwbuf), &p) != 0) { perror("getpwuid_r()"); p_username[0] = 0; @@ -55,13 +54,13 @@ static void display_prompt(void) { putchar(p_uid == 0 ? '#' : '$'); break; case 'h': - puts2(p_hostname); + fputs(p_hostname, stdout); break; case 'u': - puts2(p_username); + fputs(p_username, stdout); break; case 'd': - puts2(p_cwd); + fputs(p_cwd, stdout); break; default: putchar('%'); @@ -74,6 +73,8 @@ static void display_prompt(void) { ++p; } + + fflush(stdout); } static void signal_handle(int signum) {