2018-11-29 05:45:23 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
2019-02-03 04:14:41 +03:00
|
|
|
#include <time.h>
|
2019-02-08 04:38:21 +03:00
|
|
|
#include <fcntl.h>
|
2018-11-29 05:45:23 +03:00
|
|
|
#include <sys/ioctl.h>
|
2019-01-31 07:05:57 +03:00
|
|
|
#include <sys/stat.h>
|
2018-12-04 02:27:16 +03:00
|
|
|
#include <AK/AKString.h>
|
2018-11-29 05:45:23 +03:00
|
|
|
#include <AK/Vector.h>
|
2018-10-24 10:48:24 +03:00
|
|
|
|
2018-11-17 03:04:00 +03:00
|
|
|
static int do_dir(const char* path);
|
2018-11-29 05:45:23 +03:00
|
|
|
static int do_dir_short(const char* path);
|
|
|
|
|
|
|
|
static bool flag_colorize = true;
|
|
|
|
static bool flag_long = false;
|
|
|
|
static bool flag_show_dotfiles = false;
|
2019-01-31 19:34:24 +03:00
|
|
|
static bool flag_show_inode = false;
|
2018-11-17 03:04:00 +03:00
|
|
|
|
2018-11-09 12:18:04 +03:00
|
|
|
int main(int argc, char** argv)
|
2018-10-24 10:48:24 +03:00
|
|
|
{
|
2019-01-31 19:34:24 +03:00
|
|
|
static const char* valid_option_characters = "laiG";
|
2018-11-29 05:45:23 +03:00
|
|
|
int opt;
|
2019-01-31 19:34:24 +03:00
|
|
|
while ((opt = getopt(argc, argv, valid_option_characters)) != -1) {
|
2018-11-29 05:45:23 +03:00
|
|
|
switch (opt) {
|
|
|
|
case 'a':
|
|
|
|
flag_show_dotfiles = true;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
flag_long = true;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
flag_colorize = false;
|
|
|
|
break;
|
2019-01-31 19:34:24 +03:00
|
|
|
case 'i':
|
|
|
|
flag_show_inode = true;
|
|
|
|
break;
|
2018-11-29 05:45:23 +03:00
|
|
|
default:
|
2019-05-27 15:30:09 +03:00
|
|
|
fprintf(stderr, "usage: ls [-%s] [paths...]\n", valid_option_characters);
|
2018-11-29 05:45:23 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 15:30:09 +03:00
|
|
|
int status;
|
|
|
|
if (optind >= argc) {
|
|
|
|
if (flag_long) {
|
|
|
|
status = do_dir(".");
|
|
|
|
} else {
|
|
|
|
status = do_dir_short(".");
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
} else {
|
|
|
|
bool show_names = !(optind+1 >= argc);
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2019-05-27 15:30:09 +03:00
|
|
|
for (; optind < argc; optind++) {
|
|
|
|
if (show_names) {
|
|
|
|
printf("%s:\n", argv[optind]);
|
|
|
|
}
|
|
|
|
if (flag_long) {
|
|
|
|
status = do_dir(argv[optind]);
|
|
|
|
} else {
|
|
|
|
status = do_dir_short(argv[optind]);
|
|
|
|
}
|
|
|
|
if (status != 0) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 00:06:55 +03:00
|
|
|
void get_geometry(int& rows, int& columns)
|
2018-11-29 05:45:23 +03:00
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
ioctl(0, TIOCGWINSZ, &ws);
|
|
|
|
rows = ws.ws_row;
|
|
|
|
columns = ws.ws_col;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_name(struct stat& st, const char* name, const char* path_for_link_resolution = nullptr)
|
|
|
|
{
|
|
|
|
int nprinted = strlen(name);
|
|
|
|
if (!flag_colorize) {
|
|
|
|
printf("%s", name);
|
|
|
|
} else {
|
|
|
|
const char* begin_color = "";
|
|
|
|
const char* end_color = "\033[0m";
|
|
|
|
|
|
|
|
if (S_ISLNK(st.st_mode))
|
|
|
|
begin_color = "\033[36;1m";
|
|
|
|
else if (S_ISDIR(st.st_mode))
|
|
|
|
begin_color = "\033[34;1m";
|
|
|
|
else if (st.st_mode & 0111)
|
|
|
|
begin_color = "\033[32;1m";
|
2019-02-14 19:30:58 +03:00
|
|
|
else if (S_ISSOCK(st.st_mode))
|
|
|
|
begin_color = "\033[35;1m";
|
2018-11-29 05:45:23 +03:00
|
|
|
else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
|
|
|
begin_color = "\033[33;1m";
|
|
|
|
printf("%s%s%s", begin_color, name, end_color);
|
2018-11-17 03:04:00 +03:00
|
|
|
}
|
2018-11-29 05:45:23 +03:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
if (path_for_link_resolution) {
|
|
|
|
char linkbuf[256];
|
|
|
|
ssize_t nread = readlink(path_for_link_resolution, linkbuf, sizeof(linkbuf));
|
|
|
|
if (nread < 0) {
|
|
|
|
perror("readlink failed");
|
|
|
|
} else {
|
|
|
|
nprinted += printf(" -> %s", linkbuf);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nprinted += printf("@");
|
|
|
|
}
|
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
|
|
|
nprinted += printf("/");
|
|
|
|
} else if (st.st_mode & 0111) {
|
|
|
|
nprinted += printf("*");
|
|
|
|
}
|
|
|
|
return nprinted;
|
2018-11-17 03:04:00 +03:00
|
|
|
}
|
2018-10-28 02:17:48 +03:00
|
|
|
|
2019-05-27 04:41:22 +03:00
|
|
|
bool print_filesystem_object(const char* path, const char* name) {
|
|
|
|
struct stat st;
|
|
|
|
int rc = lstat(path, &st);
|
|
|
|
if (rc == -1) {
|
|
|
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag_show_inode)
|
|
|
|
printf("%08u ", st.st_ino);
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
printf("d");
|
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
printf("l");
|
|
|
|
else if (S_ISBLK(st.st_mode))
|
|
|
|
printf("b");
|
|
|
|
else if (S_ISCHR(st.st_mode))
|
|
|
|
printf("c");
|
|
|
|
else if (S_ISFIFO(st.st_mode))
|
|
|
|
printf("f");
|
|
|
|
else if (S_ISSOCK(st.st_mode))
|
|
|
|
printf("s");
|
|
|
|
else if (S_ISREG(st.st_mode))
|
|
|
|
printf("-");
|
|
|
|
else
|
|
|
|
printf("?");
|
|
|
|
|
|
|
|
printf("%c%c%c%c%c%c%c%c",
|
|
|
|
st.st_mode & S_IRUSR ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWUSR ? 'w' : '-',
|
|
|
|
st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
|
|
|
|
st.st_mode & S_IRGRP ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWGRP ? 'w' : '-',
|
|
|
|
st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
|
|
|
|
st.st_mode & S_IROTH ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWOTH ? 'w' : '-'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (st.st_mode & S_ISVTX)
|
|
|
|
printf("t");
|
|
|
|
else
|
|
|
|
printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
|
|
|
|
|
|
|
|
printf(" %4u %4u", st.st_uid, st.st_gid);
|
|
|
|
|
|
|
|
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
|
|
|
printf(" %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
|
|
|
|
else
|
|
|
|
printf(" %10u ", st.st_size);
|
|
|
|
|
|
|
|
auto* tm = localtime(&st.st_mtime);
|
|
|
|
printf(" %4u-%02u-%02u %02u:%02u:%02u ",
|
|
|
|
tm->tm_year + 1900,
|
|
|
|
tm->tm_mon + 1,
|
|
|
|
tm->tm_mday,
|
|
|
|
tm->tm_hour,
|
|
|
|
tm->tm_min,
|
|
|
|
tm->tm_sec);
|
|
|
|
|
|
|
|
print_name(st, name, path);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-17 03:04:00 +03:00
|
|
|
int do_dir(const char* path)
|
|
|
|
{
|
|
|
|
DIR* dirp = opendir(path);
|
2018-10-24 13:43:52 +03:00
|
|
|
if (!dirp) {
|
2019-05-27 15:13:25 +03:00
|
|
|
if (errno == ENOTDIR) {
|
|
|
|
if (print_filesystem_object(path, path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
2018-11-17 03:04:00 +03:00
|
|
|
perror("opendir");
|
2018-10-24 10:48:24 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2019-02-22 12:50:59 +03:00
|
|
|
char pathbuf[PATH_MAX];
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2018-10-24 13:43:52 +03:00
|
|
|
while (auto* de = readdir(dirp)) {
|
2018-11-29 05:45:23 +03:00
|
|
|
if (de->d_name[0] == '.' && !flag_show_dotfiles)
|
|
|
|
continue;
|
2018-11-17 03:04:00 +03:00
|
|
|
sprintf(pathbuf, "%s/%s", path, de->d_name);
|
2018-10-27 15:56:52 +03:00
|
|
|
|
2019-05-27 15:13:25 +03:00
|
|
|
if (!print_filesystem_object(pathbuf, de->d_name)) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:52:51 +03:00
|
|
|
|
2019-05-27 04:41:22 +03:00
|
|
|
bool print_filesystem_object_short(const char *path, const char *name, int *nprinted) {
|
2019-05-26 20:52:51 +03:00
|
|
|
struct stat st;
|
|
|
|
int rc = lstat(path, &st);
|
|
|
|
if (rc == -1) {
|
|
|
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
2019-05-27 04:41:22 +03:00
|
|
|
return false;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*nprinted = print_name(st, name);
|
2019-05-27 04:41:22 +03:00
|
|
|
return true;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
|
|
|
|
2018-11-29 05:45:23 +03:00
|
|
|
int do_dir_short(const char* path)
|
|
|
|
{
|
2019-02-26 00:06:55 +03:00
|
|
|
int rows;
|
|
|
|
int columns;
|
2018-11-29 05:45:23 +03:00
|
|
|
get_geometry(rows, columns);
|
|
|
|
|
|
|
|
DIR* dirp = opendir(path);
|
|
|
|
if (!dirp) {
|
2019-05-26 20:52:51 +03:00
|
|
|
if (errno == ENOTDIR) {
|
2019-05-27 04:41:22 +03:00
|
|
|
int nprinted;
|
2019-05-27 15:13:25 +03:00
|
|
|
bool status = print_filesystem_object_short(path, path, &nprinted);
|
2019-05-27 04:41:22 +03:00
|
|
|
printf("\n");
|
2019-05-27 15:13:25 +03:00
|
|
|
if (status) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 2;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
2019-05-27 15:13:25 +03:00
|
|
|
perror("opendir");
|
|
|
|
return 1;
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 15:02:19 +03:00
|
|
|
Vector<String, 1024> names;
|
2019-02-26 00:06:55 +03:00
|
|
|
int longest_name = 0;
|
2018-11-29 05:45:23 +03:00
|
|
|
while (auto* de = readdir(dirp)) {
|
|
|
|
if (de->d_name[0] == '.' && !flag_show_dotfiles)
|
|
|
|
continue;
|
|
|
|
names.append(de->d_name);
|
|
|
|
if (names.last().length() > longest_name)
|
|
|
|
longest_name = names.last().length();
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
|
|
|
|
int printed_on_row = 0;
|
2019-05-26 20:52:51 +03:00
|
|
|
int nprinted;
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2019-02-26 00:06:55 +03:00
|
|
|
for (int i = 0; i < names.size(); ++i) {
|
2018-11-29 05:45:23 +03:00
|
|
|
auto& name = names[i];
|
|
|
|
char pathbuf[256];
|
|
|
|
sprintf(pathbuf, "%s/%s", path, name.characters());
|
2018-10-28 02:17:48 +03:00
|
|
|
|
2019-05-27 04:41:22 +03:00
|
|
|
if (!print_filesystem_object_short(pathbuf, name.characters(), &nprinted)) {
|
|
|
|
return 2;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
2019-02-26 00:06:55 +03:00
|
|
|
int column_width = 14;
|
2018-11-29 05:45:23 +03:00
|
|
|
printed_on_row += column_width;
|
2018-10-28 02:17:48 +03:00
|
|
|
|
2019-02-26 00:06:55 +03:00
|
|
|
for (int i = nprinted; i < column_width; ++i)
|
2018-11-29 05:45:23 +03:00
|
|
|
printf(" ");
|
|
|
|
if ((printed_on_row + column_width) >= columns) {
|
|
|
|
if (i != names.size() - 1)
|
|
|
|
printf("\n");
|
|
|
|
printed_on_row = 0;
|
2018-10-28 16:11:51 +03:00
|
|
|
}
|
2018-10-24 13:43:52 +03:00
|
|
|
}
|
2018-11-29 05:45:23 +03:00
|
|
|
printf("\n");
|
|
|
|
|
2018-10-24 10:48:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|