1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Rewrite ls(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-05-06 21:51:04 +02:00
parent 01402052f6
commit a334ede4c4

View file

@ -28,6 +28,7 @@
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <fcntl.h> #include <fcntl.h>
#include <locale.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@ -37,37 +38,21 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
int current_year;
#if !defined(VERSIONSTR) #if !defined(VERSIONSTR)
#define VERSIONSTR "unknown version" #define VERSIONSTR "unknown version"
#endif #endif
bool directory = false; int current_year;
bool inode = false;
bool longformat = false;
bool showdotdot = false;
bool showdotfiles = false;
bool time_modified = false;
bool colors = false;
pid_t childpid; bool option_colors = false;
bool option_directory = false;
bool option_inode = false;
bool option_long_format = false;
bool option_show_dotdot = false;
bool option_show_dotfiles = false;
bool option_time_modified = false;
void finishoutput() pid_t child_pid;
{
int errnum = errno;
int status;
fflush(stdout);
if ( childpid ) { close(1); wait(&status); }
childpid = 0;
errno = errnum;
}
void ls_perror(const char* s)
{
finishoutput();
perror(s);
}
static struct dirent* dirent_dup(struct dirent* entry) static struct dirent* dirent_dup(struct dirent* entry)
{ {
@ -78,9 +63,23 @@ static struct dirent* dirent_dup(struct dirent* entry)
return copy; return copy;
} }
void finish_output()
{
int errnum = errno;
fflush(stdout);
if ( child_pid )
{
int status;
close(1);
wait(&status);
child_pid = 0;
}
errno = errnum;
}
void ls_error(int status, int errnum, const char* format, ...) void ls_error(int status, int errnum, const char* format, ...)
{ {
finishoutput(); finish_output();
// TODO: The rest is just plain generic gnu_error(3), how about a function // TODO: The rest is just plain generic gnu_error(3), how about a function
// called gnu_verror(3) that accepts a va_list. // called gnu_verror(3) that accepts a va_list.
@ -135,7 +134,7 @@ int sort_dirents(const void* a_void, const void* b_void)
struct dirent* b = *(struct dirent**) b_void; struct dirent* b = *(struct dirent**) b_void;
struct stat a_st; struct stat a_st;
struct stat b_st; struct stat b_st;
if ( time_modified && if ( option_time_modified &&
fstatat(sort_dirents_dirfd, a->d_name, &a_st, 0) == 0 && fstatat(sort_dirents_dirfd, a->d_name, &a_st, 0) == 0 &&
fstatat(sort_dirents_dirfd, b->d_name, &b_st, 0) == 0 ) fstatat(sort_dirents_dirfd, b->d_name, &b_st, 0) == 0 )
{ {
@ -152,49 +151,88 @@ int sort_dirents(const void* a_void, const void* b_void)
return strcmp(a->d_name, b->d_name); return strcmp(a->d_name, b->d_name);
} }
void getentrycolor(const char** pre, const char** post, mode_t mode) int handle_entry_internal(const char* fullpath, const char* name, unsigned char type)
{
*pre = "";
*post = "";
if ( !colors )
return;
if ( S_ISDIR(mode) )
*pre = "\e[36m",
*post = "\e[37m";
else if ( mode & 0111 )
*pre = "\e[32m",
*post = "\e[37m";
}
int handleentry_internal(const char* fullpath, const char* name)
{ {
// TODO: Use openat and fstat. // TODO: Use openat and fstat.
struct stat st; struct stat st;
if ( stat(fullpath, &st) ) bool stat_error = false;
if ( (option_long_format || type == DT_UNKNOWN || type == DT_REG) )
{ {
finishoutput(); if ( stat(fullpath, &st) == 0 )
error(0, errno, "stat: %s", fullpath); {
return 2; if ( S_ISBLK(st.st_mode) )
type = DT_BLK;
if ( S_ISCHR(st.st_mode) )
type = DT_CHR;
if ( S_ISDIR(st.st_mode) )
type = DT_DIR;
if ( S_ISFIFO(st.st_mode) )
type = DT_FIFO;
if ( S_ISLNK(st.st_mode) )
type = DT_LNK;
if ( S_ISREG(st.st_mode) )
type = DT_REG;
if ( S_ISSOCK(st.st_mode) )
type = DT_SOCK;
} }
const char* colorpre; else
const char* colorpost;
getentrycolor(&colorpre, &colorpost, st.st_mode);
if ( inode )
printf("%ju ", (uintmax_t) st.st_ino);
if ( !longformat )
{ {
printf("%s%s%s\n", colorpre, name, colorpost); memset(&st, 0, sizeof(st));
stat_error = true;
}
}
const char* color_pre = "";
const char* color_post = "";
if ( option_colors )
{
color_post = "\e[m";
switch ( type )
{
case DT_UNKNOWN: color_pre = "\e[91m"; break;
case DT_BLK: color_pre = "\e[93m"; break;
case DT_CHR: color_pre = "\e[93m"; break;
case DT_DIR: color_pre = "\e[36m"; break;
case DT_FIFO: color_pre = "\e[33m"; break;
case DT_LNK: color_pre = "\e[96m"; break;
case DT_SOCK: color_pre = "\e[35m"; break;
case DT_REG:
if ( type == DT_REG && !stat_error && (st.st_mode & 0111) )
color_pre = "\e[32m";
else
color_post = "";
break;
default:
color_post = "";
}
}
if ( option_inode )
printf("%ju ", (uintmax_t) st.st_ino);
if ( !option_long_format )
{
printf("%s%s%s\n", color_pre, name, color_post);
return 0; return 0;
} }
char perms[11]; char perms[11];
perms[0] = '?'; perms[0] = '?';
if ( S_ISREG(st.st_mode) ) { perms[0] = '-'; } switch ( type )
if ( S_ISDIR(st.st_mode) ) { perms[0] = 'd'; } {
case DT_UNKNOWN: perms[0] = '?'; break;
case DT_BLK: perms[0] = 'b'; break;
case DT_CHR: perms[0] = 'c'; break;
case DT_DIR: perms[0] = 'd'; break;
case DT_FIFO: perms[0] = 'p'; break;
case DT_LNK: perms[0] = 'l'; break;
case DT_REG: perms[0] = '-'; break;
case DT_SOCK: perms[0] = 's'; break;
default: perms[0] = '?'; break;
}
const char flagnames[] = { 'x', 'w', 'r' }; const char flagnames[] = { 'x', 'w', 'r' };
for ( size_t i = 0; i < 9; i++ ) for ( size_t i = 0; i < 9; i++ )
{ {
bool set = st.st_mode & (1UL<<i); bool set = st.st_mode & (1UL<<i);
perms[9-i] = set ? flagnames[i % 3] : '-'; perms[9-i] = stat_error ? '?' : set ? flagnames[i % 3] : '-';
} }
const char* sizeunit = "B"; const char* sizeunit = "B";
off_t size = st.st_size; off_t size = st.st_size;
@ -216,35 +254,31 @@ int handleentry_internal(const char* fullpath, const char* name)
(uintmax_t) st.st_nlink, (uintmax_t) st.st_nlink,
(uintmax_t) size, sizeunit, (uintmax_t) size, sizeunit,
time_str, time_str,
colorpre, name, colorpost); color_pre, name, color_post);
return 0; return 0;
} }
int handleentry(const char* path, const char* name) int handle_entry(const char* path, const char* name, unsigned char type)
{ {
bool isdotdot = strcmp(name, ".") == 0 || strcmp(name, "..") == 0; bool isdotdot = strcmp(name, ".") == 0 || strcmp(name, "..") == 0;
bool isdotfile = !isdotdot && name[0] == '.'; bool isdotfile = !isdotdot && name[0] == '.';
if ( isdotdot && !showdotdot && !directory ) { return 0; } if ( isdotdot && !option_show_dotdot && !option_directory )
if ( isdotfile && !showdotfiles && !directory ) { return 0; } return 0;
if ( isdotfile && !option_show_dotfiles && !option_directory )
return 0;
char* fullpath = new char[strlen(path) + 1 + strlen(name) + 1]; char* fullpath = new char[strlen(path) + 1 + strlen(name) + 1];
strcpy(fullpath, path); strcpy(fullpath, path);
strcat(fullpath, "/"); strcat(fullpath, "/");
strcat(fullpath, name); strcat(fullpath, name);
int result = handleentry_internal(fullpath, name); int result = handle_entry_internal(fullpath, name, type);
delete[] fullpath; delete[] fullpath;
return result; return result;
} }
int ls(const char* path) int ls(const char* path)
{ {
time_t current_time; if ( option_directory )
struct tm current_year_tm; return handle_entry_internal(path, path, DT_UNKNOWN);
time(&current_time);
localtime_r(&current_time, &current_year_tm);
current_year = current_year_tm.tm_year;
if ( directory )
return handleentry_internal(path, path);
int ret = 1; int ret = 1;
DIR* dir; DIR* dir;
@ -253,20 +287,23 @@ int ls(const char* path)
size_t entriesused = 0; size_t entriesused = 0;
size_t entriessize = sizeof(struct dirent*) * entrieslen; size_t entriessize = sizeof(struct dirent*) * entrieslen;
struct dirent** entries = (struct dirent**) malloc(entriessize); struct dirent** entries = (struct dirent**) malloc(entriessize);
if ( !entries ) { ls_perror("malloc"); goto cleanup_done; } if ( !entries )
{
ls_error(0, errno, "malloc");
goto cleanup_done;
}
dir = opendir(path); dir = opendir(path);
if ( !dir ) if ( !dir )
{ {
if ( errno == ENOTDIR ) if ( errno == ENOTDIR )
return handleentry_internal(path, path); return handle_entry_internal(path, path, DT_UNKNOWN);
perror(path); perror(path);
ret = 2; ret = 2;
goto cleanup_entries; goto cleanup_entries;
} }
struct dirent* entry; while ( struct dirent* entry = readdir(dir) )
while ( (entry = readdir(dir)) )
{ {
if ( entriesused == entrieslen ) if ( entriesused == entrieslen )
{ {
@ -274,18 +311,30 @@ int ls(const char* path)
struct dirent** newentries; struct dirent** newentries;
size_t newentriessize = sizeof(struct dirent*) * newentrieslen; size_t newentriessize = sizeof(struct dirent*) * newentrieslen;
newentries = (struct dirent**) realloc(entries, newentriessize); newentries = (struct dirent**) realloc(entries, newentriessize);
if ( !newentries ) { ls_perror("realloc"); goto cleanup_dir; } if ( !newentries )
{
ls_error(0, errno, "realloc");
goto cleanup_dir;
}
entries = newentries; entries = newentries;
entrieslen = newentrieslen; entrieslen = newentrieslen;
entriessize = newentriessize; entriessize = newentriessize;
} }
struct dirent* copy = dirent_dup(entry); struct dirent* copy = dirent_dup(entry);
if ( !copy ) { ls_perror("malloc"); goto cleanup_dir; } if ( !copy )
{
ls_error(0, errno, "malloc");
goto cleanup_dir;
}
entries[entriesused++] = copy; entries[entriesused++] = copy;
} }
#if defined(sortix) #if defined(sortix)
if ( derror(dir) ) { perror(path); goto cleanup_dir; } if ( derror(dir) )
{
ls_error(0, errno, path);
goto cleanup_dir;
}
#endif #endif
sort_dirents_dirfd = dirfd(dir); sort_dirents_dirfd = dirfd(dir);
@ -293,7 +342,7 @@ int ls(const char* path)
for ( size_t i = 0; i < entriesused; i++ ) for ( size_t i = 0; i < entriesused; i++ )
{ {
if ( handleentry(path, entries[i]->d_name) != 0 ) if ( handle_entry(path, entries[i]->d_name, entries[i]->d_type) != 0 )
goto cleanup_dir; goto cleanup_dir;
} }
@ -309,7 +358,20 @@ cleanup_done:
return ret; return ret;
} }
void version(FILE* fp, const char* argv0) static void compact_arguments(int* argc, char*** argv)
{
for ( int i = 0; i < *argc; i++ )
{
while ( i < *argc && !(*argv)[i] )
{
for ( int n = i; n < *argc; n++ )
(*argv)[n] = (*argv)[n+1];
(*argc)--;
}
}
}
static void version(FILE* fp, const char* argv0)
{ {
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR); 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, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
@ -317,62 +379,52 @@ void version(FILE* fp, const char* argv0)
fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n"); fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n");
} }
void help(FILE* fp, const char* argv0) static void help(FILE* fp, const char* argv0)
{ {
fprintf(fp, "usage: %s [-l] [-a | -A] [<DIR> ...]\n", argv0); fprintf(fp, "Usage: %s [OPTION]... [FILE]...\n", argv0);
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
setlocale(LC_ALL, "");
const char* argv0 = argv[0]; const char* argv0 = argv[0];
for ( int i = 1; i < argc; i++ ) for ( int i = 1; i < argc; i++ )
{ {
const char* arg = argv[i]; const char* arg = argv[i];
if ( arg[0] != '-' || !arg[1] ) { continue; } if ( arg[0] != '-' || !arg[1] )
continue;
argv[i] = NULL; argv[i] = NULL;
if ( !strcmp(arg, "--") ) { break; } if ( !strcmp(arg, "--") )
break;
if ( arg[1] != '-' ) if ( arg[1] != '-' )
{ {
char c; while ( char c = *++arg ) switch ( c )
while ( (c = *++arg) )
{ {
switch ( c ) case 'a': option_show_dotdot = true, option_show_dotfiles = true; break;
{ case 'A': option_show_dotdot = false, option_show_dotfiles = true; break;
case 'd': case 'd': option_directory = true; break;
directory = true; case 'i': option_inode = true; break;
break; case 'l': option_long_format = true; break;
case 'i': case 't': option_time_modified = true; break;
inode = true;
break;
case 'l':
longformat = true;
break;
case 't':
time_modified = true;
break;
case 'a':
showdotdot = true;
showdotfiles = true;
break;
case 'A':
showdotdot = false;
showdotfiles = true;
break;
default: default:
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c); fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
help(stderr, argv0); help(stderr, argv0);
exit(2); exit(2);
} }
} }
}
else if ( !strcmp(arg, "--version") ) else if ( !strcmp(arg, "--version") )
{ version(stdout, argv0), exit(0);
version(stdout, argv0); exit(0);
}
else if ( !strcmp(arg, "--help") ) else if ( !strcmp(arg, "--help") )
{ help(stdout, argv0), exit(0);
help(stdout, argv0); exit(0); else if ( !strcmp(arg, "--all") )
} option_show_dotdot = false, option_show_dotfiles = true;
else if ( !strcmp(arg, "--almost-all") )
option_show_dotdot = false, option_show_dotfiles = true;
else if ( !strcmp(arg, "--directory") )
option_directory = true;
else if ( !strcmp(arg, "--inode") )
option_inode = true;
else else
{ {
fprintf(stderr, "%s: unrecognized option: %s\n", argv0, arg); fprintf(stderr, "%s: unrecognized option: %s\n", argv0, arg);
@ -381,18 +433,28 @@ int main(int argc, char** argv)
} }
} }
if ( isatty(1) ) compact_arguments(&argc, &argv);
colors = true;
childpid = 0; time_t current_time;
bool columnable = !longformat; struct tm current_year_tm;
time(&current_time);
localtime_r(&current_time, &current_year_tm);
current_year = current_year_tm.tm_year;
if ( isatty(1) )
option_colors = true;
child_pid = 0;
bool columnable = !option_long_format;
if ( columnable && isatty(1) ) if ( columnable && isatty(1) )
{ {
int pipes[2]; int pipes[2];
if ( pipe(pipes) ) { perror("pipe"); return 1; } if ( pipe(pipes) )
childpid = fork(); error(1, errno, "pipe");
if ( childpid < 0 ) { perror("fork"); return 1; } child_pid = fork();
if ( childpid ) if ( child_pid < 0 )
error(1, errno, "fork");
if ( child_pid )
{ {
close(1); close(1);
dup(pipes[1]); dup(pipes[1]);
@ -412,22 +474,24 @@ int main(int argc, char** argv)
} }
} }
int result = 0;
if ( 2 <= argc )
{
// TODO: This isn't the strictly correct semantics: // TODO: This isn't the strictly correct semantics:
if ( time_modified ) if ( option_time_modified )
qsort(argv + 1, argc - 1, sizeof(char*), argv_mtim_compare); qsort(argv + 1, argc - 1, sizeof(char*), argv_mtim_compare);
int result = 0;
bool anyargs = false;
for ( int i = 1; i < argc; i++ ) for ( int i = 1; i < argc; i++ )
if ( (result = ls(argv[i])) != 0 )
break;
}
else
{ {
if ( !argv[i] ) { continue; } result = ls(".");
anyargs = true;
if ( (result = ls(argv[i])) ) { break; }
} }
if ( !anyargs ) { result = ls("."); } finish_output();
finishoutput();
return result; return result;
} }