2012-03-11 14:57:13 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
ls.cpp
|
|
|
|
Lists directory contents.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2011-08-27 21:26:11 +00:00
|
|
|
#include <stdio.h>
|
2011-11-20 23:02:53 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2011-11-21 11:19:57 +00:00
|
|
|
#include <unistd.h>
|
2011-11-22 16:26:47 +00:00
|
|
|
#include <errno.h>
|
2011-11-26 10:00:45 +00:00
|
|
|
#include <error.h>
|
2012-01-14 23:47:38 +00:00
|
|
|
#include <dirent.h>
|
2012-03-01 13:37:28 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <libmaxsi/platform.h>
|
|
|
|
#include <libmaxsi/process.h>
|
2011-11-03 17:26:43 +00:00
|
|
|
|
2011-11-20 23:02:53 +00:00
|
|
|
int ls(const char* path)
|
2011-08-27 21:26:11 +00:00
|
|
|
{
|
2012-01-14 23:47:38 +00:00
|
|
|
DIR* dir = opendir(path);
|
|
|
|
if ( !dir ) { error(2, errno, "%s", path); return 2; }
|
2011-11-20 23:02:53 +00:00
|
|
|
|
2012-01-14 23:47:38 +00:00
|
|
|
struct dirent* entry;
|
|
|
|
while ( (entry = readdir(dir)) )
|
2011-11-03 17:26:43 +00:00
|
|
|
{
|
2012-01-14 23:47:38 +00:00
|
|
|
printf("%s\n", entry->d_name);
|
2011-11-03 17:26:43 +00:00
|
|
|
}
|
2012-01-14 23:47:38 +00:00
|
|
|
|
|
|
|
if ( derror(dir) )
|
|
|
|
{
|
|
|
|
error(2, errno, "readdir: %s", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return 0;
|
2011-11-20 23:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-03-01 13:37:28 +00:00
|
|
|
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";
|
2012-03-02 14:00:11 +00:00
|
|
|
const char* argv[] = { columner, NULL };
|
|
|
|
execv(columner, (char* const*) argv);
|
2012-03-01 13:37:28 +00:00
|
|
|
error(127, errno, "%s", columner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-21 11:19:57 +00:00
|
|
|
const size_t CWD_SIZE = 512;
|
|
|
|
char cwd[CWD_SIZE];
|
|
|
|
const char* path = getcwd(cwd, CWD_SIZE);
|
|
|
|
if ( !path ) { path = "."; }
|
|
|
|
|
2011-11-20 23:02:53 +00:00
|
|
|
if ( 1 < argc ) { path = argv[1]; }
|
2011-08-27 21:26:11 +00:00
|
|
|
|
2012-03-01 13:37:28 +00:00
|
|
|
int result = ls(path);
|
|
|
|
|
|
|
|
int status;
|
|
|
|
if ( childpid ) { close(1); wait(&status); }
|
|
|
|
return result;
|
2011-08-27 21:26:11 +00:00
|
|
|
}
|
2012-03-01 13:37:28 +00:00
|
|
|
|