sortix-mirror/utils/ls.cpp

434 lines
10 KiB
C++
Raw Normal View History

/*******************************************************************************
2014-03-06 20:27:23 +00:00
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
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.
*******************************************************************************/
2012-03-11 16:06:25 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
2012-03-11 16:06:25 +00:00
#include <dirent.h>
#include <errno.h>
#include <error.h>
2012-03-11 16:06:25 +00:00
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int current_year;
2012-03-11 16:06:25 +00:00
2013-11-04 12:42:05 +00:00
#if !defined(VERSIONSTR)
#define VERSIONSTR "unknown version"
#endif
2014-03-06 20:27:23 +00:00
bool directory = false;
2014-03-06 20:31:41 +00:00
bool inode = false;
2012-03-11 16:06:25 +00:00
bool longformat = false;
bool showdotdot = false;
bool showdotfiles = false;
2014-03-06 20:43:49 +00:00
bool time_modified = false;
bool colors = false;
2012-03-11 16:06:25 +00:00
pid_t childpid;
void finishoutput()
{
2012-09-06 16:08:06 +00:00
int errnum = errno;
2012-03-11 16:06:25 +00:00
int status;
fflush(stdout);
if ( childpid ) { close(1); wait(&status); }
childpid = 0;
2012-09-06 16:08:06 +00:00
errno = errnum;
}
void ls_perror(const char* s)
{
finishoutput();
perror(s);
}
static struct dirent* dirent_dup(struct dirent* entry)
{
struct dirent* copy = (struct dirent*) malloc(entry->d_reclen);
if ( !copy )
return NULL;
memcpy(copy, entry, entry->d_reclen);
return copy;
}
void ls_error(int status, int errnum, const char* format, ...)
{
finishoutput();
// TODO: The rest is just plain generic gnu_error(3), how about a function
// called gnu_verror(3) that accepts a va_list.
fprintf(stderr, "%s: ", program_invocation_name);
va_list list;
va_start(list, format);
vfprintf(stderr, format, list);
va_end(list);
if ( errnum )
fprintf(stderr, ": %s", strerror(errnum));
fprintf(stderr, "\n");
if ( status )
exit(status);
}
2014-03-06 20:43:49 +00:00
int argv_mtim_compare(const void* a_void, const void* b_void)
{
const char* a = *(const char**) a_void;
const char* b = *(const char**) b_void;
if ( !a && b )
return 1;
if ( a && !b )
return -1;
if ( !a && !b )
return 0;
struct stat a_st;
struct stat b_st;
if ( stat(a, &a_st) == 0 && stat(b, &b_st) == 0 )
{
if ( a_st.st_mtim.tv_sec < b_st.st_mtim.tv_sec )
return 1;
if ( a_st.st_mtim.tv_sec > b_st.st_mtim.tv_sec )
return -1;
if ( a_st.st_mtim.tv_nsec < b_st.st_mtim.tv_nsec )
return 1;
if ( a_st.st_mtim.tv_nsec > b_st.st_mtim.tv_nsec )
return -1;
return 0;
}
return strcmp(a, b);
}
int sort_dirents_dirfd;
2012-09-06 16:08:06 +00:00
int sort_dirents(const void* a_void, const void* b_void)
{
struct dirent* a = *(struct dirent**) a_void;
struct dirent* b = *(struct dirent**) b_void;
2014-03-06 20:43:49 +00:00
struct stat a_st;
struct stat b_st;
if ( time_modified &&
fstatat(sort_dirents_dirfd, a->d_name, &a_st, 0) == 0 &&
fstatat(sort_dirents_dirfd, b->d_name, &b_st, 0) == 0 )
{
if ( a_st.st_mtim.tv_sec < b_st.st_mtim.tv_sec )
return 1;
if ( a_st.st_mtim.tv_sec > b_st.st_mtim.tv_sec )
return -1;
if ( a_st.st_mtim.tv_nsec < b_st.st_mtim.tv_nsec )
return 1;
if ( a_st.st_mtim.tv_nsec > b_st.st_mtim.tv_nsec )
return -1;
return 0;
}
2012-09-06 16:08:06 +00:00
return strcmp(a->d_name, b->d_name);
2012-03-11 16:06:25 +00:00
}
void getentrycolor(const char** pre, const char** post, mode_t mode)
{
*pre = "";
*post = "";
2013-11-17 13:07:42 +00:00
if ( !colors )
return;
if ( S_ISDIR(mode) )
*pre = "\e[36m",
*post = "\e[37m";
2013-11-17 13:07:42 +00:00
else if ( mode & 0111 )
*pre = "\e[32m",
*post = "\e[37m";
}
2014-03-06 20:27:23 +00:00
int handleentry_internal(const char* fullpath, const char* name)
2012-03-11 16:06:25 +00:00
{
// TODO: Use openat and fstat.
struct stat st;
if ( stat(fullpath, &st) )
{
finishoutput();
error(0, errno, "stat: %s", fullpath);
return 2;
}
const char* colorpre;
const char* colorpost;
getentrycolor(&colorpre, &colorpost, st.st_mode);
2014-03-06 20:31:41 +00:00
if ( inode )
printf("%ju ", (uintmax_t) st.st_ino);
if ( !longformat )
{
printf("%s%s%s\n", colorpre, name, colorpost);
return 0;
}
2012-03-11 16:06:25 +00:00
char perms[11];
perms[0] = '?';
if ( S_ISREG(st.st_mode) ) { perms[0] = '-'; }
if ( S_ISDIR(st.st_mode) ) { perms[0] = 'd'; }
const char flagnames[] = { 'x', 'w', 'r' };
for ( size_t i = 0; i < 9; i++ )
{
bool set = st.st_mode & (1UL<<i);
perms[9-i] = set ? flagnames[i % 3] : '-';
}
const char* sizeunit = "B";
off_t size = st.st_size;
if ( 1023 < size ) { size /= 1024UL; sizeunit = "K"; }
if ( 1023 < size ) { size /= 1024UL; sizeunit = "M"; }
if ( 1023 < size ) { size /= 1024UL; sizeunit = "G"; }
if ( 1023 < size ) { size /= 1024UL; sizeunit = "T"; }
if ( 1023 < size ) { size /= 1024UL; sizeunit = "P"; }
perms[10] = 0;
struct tm mod_tm;
localtime_r(&st.st_mtim.tv_sec, &mod_tm);
char time_str[64];
if ( current_year == mod_tm.tm_year )
strftime(time_str, 64, "%b %e %H:%M", &mod_tm);
else
strftime(time_str, 64, "%b %e %Y", &mod_tm);
printf("%s %3ju root root %4ju%s %s %s%s%s\n",
perms,
(uintmax_t) st.st_nlink,
(uintmax_t) size, sizeunit,
time_str,
colorpre, name, colorpost);
2012-03-11 16:06:25 +00:00
return 0;
}
2014-03-06 20:27:23 +00:00
int handleentry(const char* path, const char* name)
{
bool isdotdot = strcmp(name, ".") == 0 || strcmp(name, "..") == 0;
bool isdotfile = !isdotdot && name[0] == '.';
if ( isdotdot && !showdotdot && !directory ) { return 0; }
if ( isdotfile && !showdotfiles && !directory ) { return 0; }
char* fullpath = new char[strlen(path) + 1 + strlen(name) + 1];
strcpy(fullpath, path);
strcat(fullpath, "/");
strcat(fullpath, name);
int result = handleentry_internal(fullpath, name);
delete[] fullpath;
return result;
}
int ls(const char* path)
2011-08-27 21:26:11 +00:00
{
time_t current_time;
struct tm current_year_tm;
time(&current_time);
localtime_r(&current_time, &current_year_tm);
current_year = current_year_tm.tm_year;
2014-03-06 20:27:23 +00:00
if ( directory )
return handleentry_internal(path, path);
2012-09-06 16:08:06 +00:00
int ret = 1;
DIR* dir;
const size_t DEFAULT_ENTRIES_LEN = 4UL;
size_t entrieslen = DEFAULT_ENTRIES_LEN;
size_t entriesused = 0;
size_t entriessize = sizeof(struct dirent*) * entrieslen;
struct dirent** entries = (struct dirent**) malloc(entriessize);
if ( !entries ) { ls_perror("malloc"); goto cleanup_done; }
dir = opendir(path);
2014-03-06 20:27:23 +00:00
if ( !dir )
{
if ( errno == ENOTDIR )
return handleentry_internal(path, path);
perror(path);
ret = 2;
goto cleanup_entries;
}
2012-03-11 16:06:25 +00:00
2012-01-14 23:47:38 +00:00
struct dirent* entry;
while ( (entry = readdir(dir)) )
{
2012-09-06 16:08:06 +00:00
if ( entriesused == entrieslen )
{
size_t newentrieslen = entrieslen * 2UL;
struct dirent** newentries;
size_t newentriessize = sizeof(struct dirent*) * newentrieslen;
newentries = (struct dirent**) realloc(entries, newentriessize);
if ( !newentries ) { ls_perror("realloc"); goto cleanup_dir; }
entries = newentries;
entrieslen = newentrieslen;
entriessize = newentriessize;
}
struct dirent* copy = dirent_dup(entry);
if ( !copy ) { ls_perror("malloc"); goto cleanup_dir; }
entries[entriesused++] = copy;
}
2012-01-14 23:47:38 +00:00
2012-03-11 16:06:25 +00:00
#if defined(sortix)
2012-09-06 16:08:06 +00:00
if ( derror(dir) ) { perror(path); goto cleanup_dir; }
#endif
2014-03-06 20:43:49 +00:00
sort_dirents_dirfd = dirfd(dir);
2012-09-06 16:08:06 +00:00
qsort(entries, entriesused, sizeof(*entries), sort_dirents);
for ( size_t i = 0; i < entriesused; i++ )
2012-01-14 23:47:38 +00:00
{
2012-09-06 16:08:06 +00:00
if ( handleentry(path, entries[i]->d_name) != 0 )
goto cleanup_dir;
2012-01-14 23:47:38 +00:00
}
2013-04-23 18:03:57 +00:00
ret = 0;
2012-09-06 16:08:06 +00:00
cleanup_dir:
2012-01-14 23:47:38 +00:00
closedir(dir);
2012-09-06 16:08:06 +00:00
cleanup_entries:
for ( size_t i = 0; i < entriesused; i++ )
free(entries[i]);
free(entries);
cleanup_done:
return ret;
}
2012-03-11 16:06:25 +00:00
void version(FILE* fp, const char* argv0)
{
2013-11-04 12:42:05 +00:00
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
fprintf(fp, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
fprintf(fp, "This is free software: you are free to change and redistribute it.\n");
fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n");
2012-03-11 16:06:25 +00:00
}
void help(FILE* fp, const char* argv0)
{
fprintf(fp, "usage: %s [-l] [-a | -A] [<DIR> ...]\n", argv0);
2012-03-11 16:06:25 +00:00
}
2014-03-06 20:43:49 +00:00
int main(int argc, char** argv)
{
2012-03-11 16:06:25 +00:00
const char* argv0 = argv[0];
2014-02-28 21:52:20 +00:00
for ( int i = 1; i < argc; i++ )
2012-03-11 16:06:25 +00:00
{
const char* arg = argv[i];
2014-02-28 21:52:20 +00:00
if ( arg[0] != '-' || !arg[1] ) { continue; }
2012-03-11 16:06:25 +00:00
argv[i] = NULL;
if ( !strcmp(arg, "--") ) { break; }
if ( arg[1] != '-' )
{
char c;
while ( (c = *++arg) )
{
switch ( c )
{
2014-03-06 20:27:23 +00:00
case 'd':
directory = true;
break;
2014-03-06 20:31:41 +00:00
case 'i':
inode = true;
break;
2012-03-11 16:06:25 +00:00
case 'l':
longformat = true;
break;
2014-03-06 20:43:49 +00:00
case 't':
time_modified = true;
break;
2012-03-11 16:06:25 +00:00
case 'a':
showdotdot = true;
showdotfiles = true;
break;
case 'A':
showdotdot = false;
showdotfiles = true;
break;
default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
help(stderr, argv0);
2012-03-11 16:06:25 +00:00
exit(2);
}
}
}
else if ( !strcmp(arg, "--version") )
{
version(stdout, argv0); exit(0);
}
else if ( !strcmp(arg, "--help") )
{
help(stdout, argv0); exit(0);
}
else
{
fprintf(stderr, "%s: unrecognized option: %s\n", argv0, arg);
help(stderr, argv0);
2012-03-11 16:06:25 +00:00
exit(2);
}
}
if ( isatty(1) )
colors = true;
2012-03-11 16:06:25 +00:00
childpid = 0;
bool columnable = !longformat;
if ( columnable && 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, NULL };
2012-03-11 16:06:25 +00:00
execvp(columner, (char* const*) argv);
error(127, errno, "%s", columner);
}
}
2014-03-06 20:43:49 +00:00
// TODO: This isn't the strictly correct semantics:
if ( time_modified )
qsort(argv + 1, argc - 1, sizeof(char*), argv_mtim_compare);
2012-03-11 16:06:25 +00:00
int result = 0;
bool anyargs = false;
for ( int i = 1; i < argc; i++ )
{
if ( !argv[i] ) { continue; }
anyargs = true;
if ( (result = ls(argv[i])) ) { break; }
}
2012-03-11 16:06:25 +00:00
if ( !anyargs ) { result = ls("."); }
2011-08-27 21:26:11 +00:00
2012-03-11 16:06:25 +00:00
finishoutput();
return result;
2011-08-27 21:26:11 +00:00
}