toaruos/userspace/core/ls.c

460 lines
10 KiB
C
Raw Normal View History

/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2014 Kevin Lange
*/
2012-01-27 05:12:10 +04:00
/*
* ls
*
* Lists files in a directory, with nice color
* output like any modern ls should have.
2012-01-27 05:12:10 +04:00
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>
#include <dirent.h>
#include <termios.h>
2012-01-27 05:12:10 +04:00
2014-06-06 09:19:55 +04:00
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
2012-12-10 04:03:22 +04:00
#include "lib/list.h"
2012-01-27 05:12:10 +04:00
#define MIN_COL_SPACING 2
#define EXE_COLOR "1;32"
#define DIR_COLOR "1;34"
#define REG_COLOR "0"
#define MEDIA_COLOR ""
#define SYM_COLOR ""
#define BROKEN_COLOR "1;"
2013-03-18 03:32:10 +04:00
#define DEVICE_COLOR "1;33;40"
2014-06-06 09:19:55 +04:00
#define SETUID_COLOR "37;41"
2012-01-27 05:12:10 +04:00
2014-05-28 08:38:30 +04:00
#define DEFAULT_TERM_WIDTH 0
#define DEFAULT_TERM_HEIGHT 0
2012-03-08 05:35:15 +04:00
2012-12-10 04:03:22 +04:00
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
2012-01-27 05:12:10 +04:00
2012-12-10 04:59:55 +04:00
#define LINE_LEN 4096
2014-06-06 08:34:10 +04:00
static int human_readable = 0;
static int stdout_is_tty = 1;
2014-06-06 09:19:55 +04:00
static int this_year = 0;
static int explicit_path_set = 0;
static int show_hidden = 0;
static int long_mode = 0;
static int print_dir = 0;
static int term_width = DEFAULT_TERM_WIDTH;
static int term_height = DEFAULT_TERM_HEIGHT;
struct tfile {
char * name;
struct stat statbuf;
};
static int filecmp(const void * c1, const void * c2) {
const struct tfile * d1 = *(const struct tfile **)c1;
const struct tfile * d2 = *(const struct tfile **)c2;
int a = S_ISDIR(d1->statbuf.st_mode);
int b = S_ISDIR(d2->statbuf.st_mode);
2012-01-27 05:12:10 +04:00
if (a == b) return strcmp(d1->name, d2->name);
else if (a < b) return -1;
else if (a > b) return 1;
2012-01-27 05:12:10 +04:00
}
static int filecmp_notypesort(const void * c1, const void * c2) {
const struct tfile * d1 = *(const struct tfile **)c1;
const struct tfile * d2 = *(const struct tfile **)c2;
2012-01-27 05:12:10 +04:00
return strcmp(d1->name, d2->name);
}
2012-01-27 05:12:10 +04:00
static void print_entry(struct tfile * file, int colwidth) {
2012-12-10 04:03:22 +04:00
const char * ansi_color_str;
if (S_ISDIR(file->statbuf.st_mode)) {
2013-03-18 03:32:10 +04:00
/* Directory */
2012-12-10 04:03:22 +04:00
ansi_color_str = DIR_COLOR;
} else if (file->statbuf.st_mode & 0111) {
2013-03-18 03:32:10 +04:00
/* Executable */
2012-12-10 04:03:22 +04:00
ansi_color_str = EXE_COLOR;
} else if (S_ISBLK(file->statbuf.st_mode) || S_ISCHR(file->statbuf.st_mode)) {
2013-03-18 03:32:10 +04:00
/* Device file */
ansi_color_str = DEVICE_COLOR;
2012-01-27 05:12:10 +04:00
} else {
2013-03-18 03:32:10 +04:00
/* Regular file? */
2012-12-10 04:03:22 +04:00
ansi_color_str = REG_COLOR;
2012-01-27 05:12:10 +04:00
}
2012-12-10 04:03:22 +04:00
/* Print the file name */
2014-06-06 08:34:10 +04:00
if (stdout_is_tty) {
printf("\033[%sm%s\033[0m", ansi_color_str, file->name);
2014-06-06 08:34:10 +04:00
} else {
printf("%s", file->name);
2014-06-06 08:34:10 +04:00
}
2012-01-27 05:12:10 +04:00
2012-12-10 04:03:22 +04:00
/* Pad the rest of the column */
for (int rem = colwidth - strlen(file->name); rem > 0; rem--) {
2012-12-10 04:03:22 +04:00
printf(" ");
2012-01-27 05:12:10 +04:00
}
}
static int print_username(char * _out, int uid) {
2012-12-10 04:59:55 +04:00
FILE * passwd = fopen("/etc/passwd", "r");
char line[LINE_LEN];
2014-06-06 09:19:55 +04:00
int out;
2012-12-10 04:59:55 +04:00
while (fgets(line, LINE_LEN, passwd) != NULL) {
line[strlen(line)-1] = '\0';
char *p, *tokens[10], *last;
int i = 0;
2013-03-18 03:32:10 +04:00
for ((p = strtok_r(line, ":", &last)); p; (p = strtok_r(NULL, ":", &last)), i++) {
2012-12-10 04:59:55 +04:00
if (i < 511) tokens[i] = p;
}
tokens[i] = NULL;
if (atoi(tokens[2]) == uid) {
2014-06-06 09:19:55 +04:00
out = sprintf(_out, "%s", tokens[0]);
2012-12-10 04:59:55 +04:00
fclose(passwd);
2014-06-06 09:19:55 +04:00
return out;
2012-12-10 04:59:55 +04:00
}
}
2014-06-06 09:19:55 +04:00
out = sprintf(_out, "%d", uid);
2012-12-10 04:59:55 +04:00
fclose(passwd);
2014-06-06 09:19:55 +04:00
return out;
2012-12-10 04:59:55 +04:00
}
static int print_human_readable_size(char * _out, size_t s) {
2014-06-06 08:34:10 +04:00
if (s >= 1<<20) {
size_t t = s / (1 << 20);
2014-06-06 09:19:55 +04:00
return sprintf(_out, "%d.%1dM", t, (s - t * (1 << 20)) / ((1 << 20) / 10));
2014-06-06 08:34:10 +04:00
} else if (s >= 1<<10) {
size_t t = s / (1 << 10);
2014-06-06 09:19:55 +04:00
return sprintf(_out, "%d.%1dK", t, (s - t * (1 << 10)) / ((1 << 10) / 10));
2014-06-06 08:34:10 +04:00
} else {
2014-06-06 09:19:55 +04:00
return sprintf(_out, "%d", s);
2014-06-06 08:34:10 +04:00
}
2014-06-06 09:19:55 +04:00
}
static void update_column_widths(int * widths, struct tfile * file) {
2014-06-06 09:19:55 +04:00
char tmp[256];
int n;
/* Links */
n = sprintf(tmp, "%d", file->statbuf.st_nlink);
2014-06-06 09:19:55 +04:00
if (n > widths[0]) widths[0] = n;
2014-06-06 08:34:10 +04:00
2014-06-06 09:19:55 +04:00
/* User */
n = print_username(tmp, file->statbuf.st_uid);
2014-06-06 09:19:55 +04:00
if (n > widths[1]) widths[1] = n;
/* Group */
n = print_username(tmp, file->statbuf.st_gid);
2014-06-06 09:19:55 +04:00
if (n > widths[2]) widths[2] = n;
/* File size */
if (human_readable) {
n = print_human_readable_size(tmp, file->statbuf.st_size);
2014-06-06 09:19:55 +04:00
} else {
n = sprintf(tmp, "%d", file->statbuf.st_size);
2014-06-06 09:19:55 +04:00
}
if (n > widths[3]) widths[3] = n;
2014-06-06 08:34:10 +04:00
}
static void print_entry_long(int * widths, struct tfile * file) {
2012-01-27 05:12:10 +04:00
const char * ansi_color_str;
if (S_ISDIR(file->statbuf.st_mode)) {
2012-01-27 05:12:10 +04:00
// Directory
ansi_color_str = DIR_COLOR;
} else if (file->statbuf.st_mode & S_ISUID) {
2014-06-06 09:19:55 +04:00
ansi_color_str = SETUID_COLOR;
} else if (file->statbuf.st_mode & 0111) {
2012-01-27 05:12:10 +04:00
// Executable
ansi_color_str = EXE_COLOR;
} else if (S_ISBLK(file->statbuf.st_mode) || S_ISCHR(file->statbuf.st_mode)) {
2014-04-02 10:14:58 +04:00
/* Device file */
ansi_color_str = DEVICE_COLOR;
2012-01-27 05:12:10 +04:00
} else {
// Something else
ansi_color_str = REG_COLOR;
}
2012-12-10 04:59:55 +04:00
/* file permissions */
if (S_ISLNK(file->statbuf.st_mode)) { printf("l"); }
else if (S_ISCHR(file->statbuf.st_mode)) { printf("c"); }
else if (S_ISBLK(file->statbuf.st_mode)) { printf("b"); }
else if (S_ISDIR(file->statbuf.st_mode)) { printf("d"); }
else { printf("-"); }
printf( (file->statbuf.st_mode & S_IRUSR) ? "r" : "-");
printf( (file->statbuf.st_mode & S_IWUSR) ? "w" : "-");
if (file->statbuf.st_mode & S_ISUID) {
printf("s");
} else {
printf( (file->statbuf.st_mode & S_IXUSR) ? "x" : "-");
}
printf( (file->statbuf.st_mode & S_IRGRP) ? "r" : "-");
printf( (file->statbuf.st_mode & S_IWGRP) ? "w" : "-");
printf( (file->statbuf.st_mode & S_IXGRP) ? "x" : "-");
printf( (file->statbuf.st_mode & S_IROTH) ? "r" : "-");
printf( (file->statbuf.st_mode & S_IWOTH) ? "w" : "-");
printf( (file->statbuf.st_mode & S_IXOTH) ? "x" : "-");
2012-12-10 04:03:22 +04:00
printf( " %*d ", widths[0], file->statbuf.st_nlink); /* number of links, not supported */
2012-12-10 04:59:55 +04:00
2014-06-06 09:19:55 +04:00
char tmp[100];
print_username(tmp, file->statbuf.st_uid);
2014-06-06 09:19:55 +04:00
printf("%-*s ", widths[1], tmp);
print_username(tmp, file->statbuf.st_gid);
2014-06-06 09:19:55 +04:00
printf("%-*s ", widths[2], tmp);
2012-12-10 04:59:55 +04:00
2014-06-06 08:34:10 +04:00
if (human_readable) {
print_human_readable_size(tmp, file->statbuf.st_size);
2014-06-06 09:19:55 +04:00
printf("%*s ", widths[3], tmp);
2014-06-06 08:34:10 +04:00
} else {
printf("%*d ", widths[3], file->statbuf.st_size);
2014-06-06 08:34:10 +04:00
}
2012-12-10 04:59:55 +04:00
char time_buf[80];
struct tm * timeinfo = localtime(&file->statbuf.st_mtime);
2014-06-06 09:19:55 +04:00
if (timeinfo->tm_year == this_year) {
strftime(time_buf, 80, "%b %d %H:%M", timeinfo);
} else {
strftime(time_buf, 80, "%b %d %Y", timeinfo);
}
2012-12-10 04:59:55 +04:00
printf("%s ", time_buf);
2012-01-27 05:12:10 +04:00
/* Print the file name */
2014-06-06 09:19:55 +04:00
if (stdout_is_tty) {
printf("\033[%sm%s\033[0m", ansi_color_str, file->name);
2014-06-06 09:19:55 +04:00
} else {
printf("%s", file->name);
2014-06-06 09:19:55 +04:00
}
2012-01-27 05:12:10 +04:00
2014-06-06 09:19:55 +04:00
printf("\n");
2012-12-10 04:03:22 +04:00
}
static void show_usage(int argc, char * argv[]) {
2012-12-10 04:03:22 +04:00
printf(
"ls - list files\n"
"\n"
"usage: %s [-lha] [path]\n"
"\n"
" -a \033[3mlist all files (including . files)\033[0m\n"
" -l \033[3muse a long listing format\033[0m\n"
2014-06-06 08:34:10 +04:00
" -h \033[3mhuman-readable file sizes\033[0m\n"
2012-12-10 04:03:22 +04:00
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
2012-01-27 05:12:10 +04:00
}
static void display_tfiles(struct tfile ** ents_array, int numents) {
if (long_mode) {
int widths[4] = {0,0,0,0};
for (int i = 0; i < numents; i++) {
update_column_widths(widths, ents_array[i]);
}
for (int i = 0; i < numents; i++) {
print_entry_long(widths, ents_array[i]);
}
} else {
/* Determine the gridding dimensions */
int ent_max_len = 0;
for (int i = 0; i < numents; i++) {
ent_max_len = MAX(ent_max_len, strlen(ents_array[i]->name));
}
int col_ext = ent_max_len + MIN_COL_SPACING;
int cols = ((term_width - ent_max_len) / col_ext) + 1;
/* Print the entries */
for (int i = 0; i < numents;) {
/* Columns */
print_entry(ents_array[i++], ent_max_len);
for (int j = 0; (i < numents) && (j < (cols-1)); j++) {
printf(" ");
print_entry(ents_array[i++], ent_max_len);
}
printf("\n");
}
}
}
static int display_dir(char * p) {
/* Open the directory */
DIR * dirp = opendir(p);
if (dirp == NULL) {
return 2;
}
if (print_dir) {
printf("%s:\n", p);
}
/* Read the entries in the directory */
list_t * ents_list = list_create();
struct dirent * ent = readdir(dirp);
while (ent != NULL) {
if (show_hidden || (ent->d_name[0] != '.')) {
struct tfile * f = malloc(sizeof(struct tfile));
f->name = strdup(ent->d_name);
char tmp[strlen(p)+strlen(ent->d_name)+1];
sprintf(tmp, "%s/%s", p, ent->d_name);
int t = stat(tmp, &f->statbuf);
list_insert(ents_list, (void *)f);
}
ent = readdir(dirp);
}
closedir(dirp);
/* Now, copy those entries into an array (for sorting) */
struct tfile ** file_arr = malloc(sizeof(struct tfile *) * ents_list->length);
int index = 0;
foreach(node, ents_list) {
file_arr[index++] = (struct tfile *)node->value;
}
list_free(ents_list);
qsort(file_arr, index, sizeof(struct tfile *), filecmp_notypesort);
display_tfiles(file_arr, index);
free(file_arr);
return 0;
}
2012-01-27 05:12:10 +04:00
int main (int argc, char * argv[]) {
/* Parse arguments */
char * p = ".";
if (argc > 1) {
int index, c;
2014-06-06 08:34:10 +04:00
while ((c = getopt(argc, argv, "ahl?")) != -1) {
2012-01-27 05:12:10 +04:00
switch (c) {
case 'a':
show_hidden = 1;
break;
2014-06-06 08:34:10 +04:00
case 'h':
human_readable = 1;
break;
2012-12-10 04:03:22 +04:00
case 'l':
long_mode = 1;
break;
case '?':
show_usage(argc, argv);
return 0;
2012-01-27 05:12:10 +04:00
}
}
if (optind < argc) {
p = argv[optind];
}
if (optind + 1 < argc) {
print_dir = 1;
2012-01-27 05:12:10 +04:00
}
}
stdout_is_tty = isatty(STDOUT_FILENO);
2012-01-27 05:12:10 +04:00
2012-12-10 04:03:22 +04:00
if (long_mode) {
2014-06-06 09:19:55 +04:00
struct tm * timeinfo;
struct timeval now;
gettimeofday(&now, NULL); //time(NULL);
timeinfo = localtime((time_t *)&now.tv_sec);
this_year = timeinfo->tm_year;
}
if (stdout_is_tty) {
struct winsize w;
ioctl(1, TIOCGWINSZ, &w);
term_width = w.ws_col;
term_height = w.ws_row;
term_width -= 1; /* And this just helps clean up our math */
}
int out = 0;
if (argc == 1 || optind == argc) {
display_dir(p);
2012-12-10 04:03:22 +04:00
} else {
list_t * files = list_create();
while (p) {
struct tfile * f = malloc(sizeof(struct tfile));
f->name = p;
int t = stat(p, &f->statbuf);
if (t < 0) {
printf("ls: cannot access %s: No such file or directory\n", p);
free(f);
out = 2;
}
list_insert(files, f);
2012-01-27 05:12:10 +04:00
optind++;
if (optind >= argc) p = NULL;
else p = argv[optind];
2014-05-28 08:38:30 +04:00
}
2012-01-27 05:12:10 +04:00
struct tfile ** file_arr = malloc(sizeof(struct tfile *) * files->length);
int index = 0;
foreach(node, files) {
file_arr[index++] = (struct tfile *)node->value;
}
2012-01-27 05:12:10 +04:00
list_free(files);
2012-01-27 05:12:10 +04:00
qsort(file_arr, index, sizeof(struct tfile *), filecmp);
2012-01-27 05:12:10 +04:00
int first_directory = index;
2012-01-27 05:12:10 +04:00
for (int i = 0; i < index; ++i) {
if (S_ISDIR(file_arr[i]->statbuf.st_mode)) {
first_directory = i;
break;
2012-12-10 04:03:22 +04:00
}
}
2012-12-10 04:03:22 +04:00
if (first_directory) {
display_tfiles(file_arr, first_directory);
2012-12-10 04:03:22 +04:00
}
for (int i = first_directory; i < index; ++i) {
if (i != 0) {
printf("\n");
}
display_dir(file_arr[i]->name);
}
}
2012-01-27 05:12:10 +04:00
return out;
2012-01-27 05:12:10 +04:00
}
/*
* vim: tabstop=4
* vim: shiftwidth=4
* vim: noexpandtab
*/