From 158909033e212596901f2f39aee12dff7c64d478 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 1 Mar 2012 14:37:28 +0100 Subject: [PATCH] ls(1) now pipe(2) into column(1) if stdout(3) isatty(2). This saves precious screen space as Sortix only supports 25-line ttys. --- utils/ls.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/utils/ls.cpp b/utils/ls.cpp index b806d174..19df676c 100644 --- a/utils/ls.cpp +++ b/utils/ls.cpp @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include int ls(const char* path) { @@ -32,6 +35,33 @@ int ls(const char* path) int main(int argc, char* argv[]) { + pid_t childpid = 0; + if ( isatty(1) ) + { + int pipes[2]; + if ( pipe(pipes) ) { perror("pipe"); return 1; } + childpid = fork(); + if ( childpid < 0 ) { perror("fork"); return 1; } + if ( childpid ) + { + close(1); + dup(pipes[1]); + close(pipes[0]); + close(pipes[1]); + } + else + { + close(0); + dup(pipes[0]); + close(pipes[0]); + close(pipes[1]); + const char* columner = "column"; + const char* argv[] = { columner }; + Maxsi::Process::Execute(columner, 1, argv); + error(127, errno, "%s", columner); + } + } + const size_t CWD_SIZE = 512; char cwd[CWD_SIZE]; const char* path = getcwd(cwd, CWD_SIZE); @@ -39,5 +69,10 @@ int main(int argc, char* argv[]) if ( 1 < argc ) { path = argv[1]; } - return ls(path); + int result = ls(path); + + int status; + if ( childpid ) { close(1); wait(&status); } + return result; } +