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.
This commit is contained in:
parent
caddf8c191
commit
158909033e
1 changed files with 36 additions and 1 deletions
37
utils/ls.cpp
37
utils/ls.cpp
|
@ -5,6 +5,9 @@
|
|||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/wait.h>
|
||||
#include <libmaxsi/platform.h>
|
||||
#include <libmaxsi/process.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue