add ls arg parsing
This commit is contained in:
parent
06d5e61af6
commit
f72a7b9c4a
45
ls.c
45
ls.c
|
@ -25,6 +25,10 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define TRACE_APP_NAME "ls"
|
||||
//#include "lib/trace.h"
|
||||
#define TRACE(...)
|
||||
|
||||
#include "lib/list.h"
|
||||
|
||||
#define MIN_COL_SPACING 2
|
||||
|
@ -122,12 +126,15 @@ static void print_entry(struct tfile * file, int colwidth) {
|
|||
|
||||
static int print_username(char * _out, int uid) {
|
||||
|
||||
TRACE("getpwuid");
|
||||
struct passwd * p = getpwuid(uid);
|
||||
int out = 0;
|
||||
|
||||
if (p) {
|
||||
TRACE("p is set");
|
||||
out = sprintf(_out, "%s", p->pw_name);
|
||||
} else {
|
||||
TRACE("p is not set");
|
||||
out = sprintf(_out, "%d", uid);
|
||||
}
|
||||
|
||||
|
@ -153,18 +160,22 @@ static void update_column_widths(int * widths, struct tfile * file) {
|
|||
int n;
|
||||
|
||||
/* Links */
|
||||
TRACE("links");
|
||||
n = sprintf(tmp, "%d", file->statbuf.st_nlink);
|
||||
if (n > widths[0]) widths[0] = n;
|
||||
|
||||
/* User */
|
||||
TRACE("user");
|
||||
n = print_username(tmp, file->statbuf.st_uid);
|
||||
if (n > widths[1]) widths[1] = n;
|
||||
|
||||
/* Group */
|
||||
TRACE("group");
|
||||
n = print_username(tmp, file->statbuf.st_gid);
|
||||
if (n > widths[2]) widths[2] = n;
|
||||
|
||||
/* File size */
|
||||
TRACE("file size");
|
||||
if (human_readable) {
|
||||
n = print_human_readable_size(tmp, file->statbuf.st_size);
|
||||
} else {
|
||||
|
@ -254,10 +265,12 @@ static void show_usage(int argc, char * argv[]) {
|
|||
|
||||
static void display_tfiles(struct tfile ** ents_array, int numents) {
|
||||
if (long_mode) {
|
||||
TRACE("long mode display, column lengths");
|
||||
int widths[4] = {0,0,0,0};
|
||||
for (int i = 0; i < numents; i++) {
|
||||
update_column_widths(widths, ents_array[i]);
|
||||
}
|
||||
TRACE("actual printing");
|
||||
for (int i = 0; i < numents; i++) {
|
||||
print_entry_long(widths, ents_array[i]);
|
||||
}
|
||||
|
@ -302,6 +315,7 @@ static int display_dir(char * p) {
|
|||
/* Read the entries in the directory */
|
||||
list_t * ents_list = list_create();
|
||||
|
||||
TRACE("reading entries");
|
||||
struct dirent * ent = readdir(dirp);
|
||||
while (ent != NULL) {
|
||||
if (show_hidden || (ent->d_name[0] != '.')) {
|
||||
|
@ -324,6 +338,8 @@ static int display_dir(char * p) {
|
|||
}
|
||||
closedir(dirp);
|
||||
|
||||
TRACE("copying");
|
||||
|
||||
/* Now, copy those entries into an array (for sorting) */
|
||||
|
||||
struct tfile ** file_arr = malloc(sizeof(struct tfile *) * ents_list->length);
|
||||
|
@ -334,8 +350,10 @@ static int display_dir(char * p) {
|
|||
|
||||
list_free(ents_list);
|
||||
|
||||
TRACE("sorting");
|
||||
qsort(file_arr, index, sizeof(struct tfile *), filecmp_notypesort);
|
||||
|
||||
TRACE("displaying");
|
||||
display_tfiles(file_arr, index);
|
||||
|
||||
free(file_arr);
|
||||
|
@ -369,6 +387,29 @@ int main (int argc, char * argv[]) {
|
|||
}
|
||||
#else
|
||||
int optind = 1;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (argv[i][0] == '-') {
|
||||
char *c = &argv[i][1];
|
||||
optind++;
|
||||
while (*c) {
|
||||
switch (*c) {
|
||||
case 'a':
|
||||
show_hidden = 1;
|
||||
break;
|
||||
case 'h':
|
||||
human_readable = 1;
|
||||
break;
|
||||
case 'l':
|
||||
long_mode = 1;
|
||||
break;
|
||||
case '?':
|
||||
show_usage(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
#endif
|
||||
|
@ -382,7 +423,7 @@ int main (int argc, char * argv[]) {
|
|||
}
|
||||
|
||||
|
||||
stdout_is_tty = isatty(STDOUT_FILENO) || !!getenv("PRETEND_STDOUT_IS_TTY");
|
||||
stdout_is_tty = isatty(STDOUT_FILENO);
|
||||
|
||||
#if 0
|
||||
if (long_mode) {
|
||||
|
@ -395,6 +436,7 @@ int main (int argc, char * argv[]) {
|
|||
#endif
|
||||
|
||||
if (stdout_is_tty) {
|
||||
TRACE("getting display size");
|
||||
struct winsize w;
|
||||
ioctl(1, TIOCGWINSZ, &w);
|
||||
term_width = w.ws_col;
|
||||
|
@ -405,6 +447,7 @@ int main (int argc, char * argv[]) {
|
|||
int out = 0;
|
||||
|
||||
if (argc == 1 || optind == argc) {
|
||||
TRACE("no file to look up");
|
||||
display_dir(p);
|
||||
} else {
|
||||
list_t * files = list_create();
|
||||
|
|
Loading…
Reference in New Issue