From 36bd53b36a7846a5aade225436cd1a09da0af7fc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 21 Dec 2018 02:42:30 +0100 Subject: [PATCH] Add a simple /bin/more. --- Kernel/sync.sh | 1 + Userland/.gitignore | 1 + Userland/Makefile | 9 +++++++-- Userland/more.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 Userland/more.cpp diff --git a/Kernel/sync.sh b/Kernel/sync.sh index 5e12dfe948..674564d91e 100755 --- a/Kernel/sync.sh +++ b/Kernel/sync.sh @@ -29,6 +29,7 @@ cp -v ../Userland/tty mnt/bin/tty cp -v ../Userland/mkdir mnt/bin/mkdir cp -v ../Userland/touch mnt/bin/touch cp -v ../Userland/sync mnt/bin/sync +cp -v ../Userland/more mnt/bin/more sh sync-local.sh cp -v kernel.map mnt/ ln -s dir_a mnt/dir_cur diff --git a/Userland/.gitignore b/Userland/.gitignore index a8c5bb5841..a7e9d82205 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -20,3 +20,4 @@ fgrep mkdir touch sync +more diff --git a/Userland/Makefile b/Userland/Makefile index a51834bd8b..ce4de90c04 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -17,7 +17,8 @@ OBJS = \ fgrep.o \ tty.o \ mkdir.o \ - touch.o + touch.o \ + more.o APPS = \ id \ @@ -39,7 +40,8 @@ APPS = \ tty \ mkdir \ touch \ - sync + sync \ + more ARCH_FLAGS = STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc @@ -119,6 +121,9 @@ touch: touch.o sync: sync.o $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a +more: more.o + $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a + .cpp.o: @echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< diff --git a/Userland/more.cpp b/Userland/more.cpp new file mode 100644 index 0000000000..d0050c6b9d --- /dev/null +++ b/Userland/more.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +static int key_fd; + +void wait_for_key() +{ + printf("\033[7m--[ more ]--\033[0m"); + fflush(stdout); + char dummy; + read(key_fd, &dummy, 1); + printf("\n"); +} + +int main(int argc, char** argv) +{ + (void) argc; + (void) argv; + + key_fd = open(ttyname(1), O_RDONLY); + if (key_fd < 0) { + perror("open"); + return 1; + } + + struct winsize ws; + ioctl(1, TIOCGWINSZ, &ws); + + unsigned lines_printed = 0; + while (!feof(stdin)) { + char buffer[BUFSIZ]; + auto* str = fgets(buffer, sizeof(buffer), stdin); + if (!str) + break; + printf(str); + ++lines_printed; + if ((lines_printed % (ws.ws_row - 1)) == 0) { + wait_for_key(); + } + + } + + close(key_fd); + return 0; +}