lazysphere/command/ls.c

801 lines
24 KiB
C
Raw Normal View History

2023-05-16 23:15:01 +00:00
/**
* file: ls.c
* author: Tyler Murphy
*/
2023-05-06 04:39:44 +00:00
#include "command.h"
#include "lslib.h"
2023-04-28 04:36:15 +00:00
#include <grp.h>
#include <pwd.h>
#include <dirent.h>
#include <ftw.h>
#include <limits.h>
2023-05-06 04:39:44 +00:00
#include <stdlib.h>
2023-05-02 22:02:47 +00:00
#include <string.h>
2023-05-06 04:39:44 +00:00
#include <errno.h>
2023-05-16 18:00:22 +00:00
#include <sys/stat.h>
2023-05-06 04:39:44 +00:00
#include <unistd.h>
#include <sys/ioctl.h>
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* quirky colors */
2023-04-30 06:12:02 +00:00
#define FILE_COLOR ANSCII BLACK COLOR
#define DIR_COLOR ANSCII BOLD NEXT NORMAL BLUE COLOR
#define DIR_COLOR_EXEC ANSCII BACKGROUND GREEN NEXT NORMAL BLACK COLOR
2023-05-02 04:37:30 +00:00
#define LINK_COLOR ANSCII BOLD NEXT NORMAL TURQUOISE COLOR
2023-04-30 06:12:02 +00:00
#define SET_UID_COLOR ANSCII BACKGROUND RED NEXT NORMAL WHITE COLOR
#define SET_GID_COLOR ANSCII BACKGROUND YELLOW NEXT NORMAL BLACK COLOR
#define EXEC_COLOR ANSCII BOLD NEXT NORMAL GREEN COLOR
#define BLK_COLOR ANSCII BOLD NEXT NORMAL YELLOW COLOR
2023-05-02 04:37:30 +00:00
#define SOCK_COLOR ANSCII BOLD NEXT NORMAL MAGENTA COLOR
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/**
* Describes when to do something
*/
2023-05-06 04:39:44 +00:00
enum When {
2023-05-16 18:00:22 +00:00
YES, /* always */
NO, /* never */
AUTO /* when not tty */
2023-05-06 04:39:44 +00:00
};
2023-05-16 18:00:22 +00:00
/**
* Flags that are to be used with ls
*/
2023-05-01 22:43:32 +00:00
static struct {
2023-05-16 23:15:01 +00:00
bool hidden; /* -a if to show hidden files */
bool hide_dot; /* -A if to hide dot dirs */
bool more_info; /* -l if to print info as a long list */
bool one_column; /* -1 if to print files as a single column */
bool recurse; /* -R if to recurse directorys */
bool octets; /* -o if to print mode as its octet instead of romanized */
2023-05-16 18:00:22 +00:00
enum When colored; /* --color=never/no/yes/always/auto */
2023-05-01 22:43:32 +00:00
} flags;
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/**
* Keeps track of the files data
*/
2023-04-28 16:31:49 +00:00
struct FileInfo {
2023-05-16 18:00:22 +00:00
struct passwd* usr; /* the files owner user */
struct group* grp; /* the files owner group */
char* name; /* the file name */
char date[13]; /* the files romanized date */
char mode[11]; /* the files mode romanized or octets */
char size[5]; /* the human readable file size */
int links; /* the amount of inode links */
int bytes; /* the amount of bytes in the file*/
bool set_uid; /* if the file is set uid */
bool set_gid; /* if the file is set gid */
bool exec; /* if the binary is executable by the user running ls */
unsigned char type; /* the type of the file */
2023-04-28 16:31:49 +00:00
};
2023-05-16 18:00:22 +00:00
/**
* Keeps track of the biggest data to make the output data look pretty
*/
2023-04-28 16:31:49 +00:00
struct FileListInfo {
int max_link;
int max_usr;
int max_grp;
int max_size;
int max_name;
int total_len;
2023-05-02 04:37:30 +00:00
int total_size;
2023-04-28 16:31:49 +00:00
};
2023-05-16 18:00:22 +00:00
/**
* Gets a directory and fails if it cannot be opened
* @param path the path to the directory
* @returns the directory if found or NULL if not
*/
2023-04-28 04:36:15 +00:00
static DIR* get_directory(char* path) {
DIR* d = opendir(path);
if (d == NULL) {
2023-04-28 17:11:56 +00:00
if (errno == ENOTDIR) {
2023-05-15 01:43:02 +00:00
error_s("`%s` is a a file", path);
2023-04-28 17:11:56 +00:00
} else {
2023-05-15 01:43:02 +00:00
error_s("failed to open directory '%s'", path);
2023-04-28 17:11:56 +00:00
}
2023-04-28 04:36:15 +00:00
}
return d;
}
2023-05-16 18:00:22 +00:00
/**
* Takes in a file and parses its mode to be humand readable
* @param info the file infomation to store the mode into
* @param mode the octet form of the mode
* @param type the type of the file
*/
static void parse_file_mode(struct FileInfo* info, mode_t mode, int type) {
switch (type) {
2023-04-30 06:12:02 +00:00
case DT_BLK:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'b'; /* block device */
2023-04-30 06:12:02 +00:00
break;
case DT_CHR:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'c'; /* something i dunno */
2023-04-30 06:12:02 +00:00
break;
case DT_DIR:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'd'; /* directory */
2023-04-30 06:12:02 +00:00
break;
case DT_FIFO:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'f'; /* fifo */
2023-04-30 06:12:02 +00:00
break;
case DT_LNK:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'l'; /* symlink */
2023-04-30 06:12:02 +00:00
break;
case DT_SOCK:
2023-05-16 18:00:22 +00:00
info->mode[0] = 's'; /* socket */
2023-04-30 06:12:02 +00:00
break;
case DT_UNKNOWN:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'u'; /* unkown (eye emoji) */
2023-04-30 06:12:02 +00:00
break;
case DT_WHT:
2023-05-16 18:00:22 +00:00
info->mode[0] = 'w'; /* tf, wheat? idk */
2023-04-30 06:12:02 +00:00
break;
default:
2023-05-16 18:00:22 +00:00
info->mode[0] = '-'; /* regular file */
2023-04-30 06:12:02 +00:00
break;
}
2023-05-16 18:00:22 +00:00
/* parse info for the user bits */
info->mode[1] = (mode & S_IRUSR) ? 'r' : '-';
info->mode[2] = (mode & S_IWUSR) ? 'w' : '-';
if (mode & S_IXUSR && mode & S_ISUID) {
2023-05-12 21:38:41 +00:00
info->mode[3] = 's';
2023-05-16 18:00:22 +00:00
} else if (mode & S_ISUID) {
2023-05-12 21:38:41 +00:00
info->mode[3] = 'S';
2023-05-16 18:00:22 +00:00
} else if (mode & S_IXUSR) {
2023-05-12 21:38:41 +00:00
info->mode[3] = 'x';
2023-04-28 16:31:49 +00:00
} else {
info->mode[3] = '-';
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* parse info for the group bits */
info->mode[4] = (mode & S_IRGRP) ? 'r' : '-';
info->mode[5] = (mode & S_IWGRP) ? 'w' : '-';
if (mode & S_IXGRP && mode & S_ISGID) {
2023-05-12 21:38:41 +00:00
info->mode[6] = 's';
2023-05-16 18:00:22 +00:00
} else if (mode & S_ISGID) {
2023-05-12 21:38:41 +00:00
info->mode[6] = 'S';
2023-05-16 18:00:22 +00:00
} else if (mode & S_IXGRP) {
2023-05-12 21:38:41 +00:00
info->mode[6] = 'x';
2023-04-28 16:31:49 +00:00
} else {
info->mode[6] = '-';
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* parse info for the other bits */
info->mode[7] = (mode & S_IROTH) ? 'r' : '-';
info->mode[8] = (mode & S_IWOTH) ? 'w' : '-';
if (mode & S_IXOTH && mode & S_ISVTX) {
2023-05-12 21:38:41 +00:00
info->mode[9] = 't';
2023-05-16 18:00:22 +00:00
} else if (mode & S_ISVTX) {
2023-05-12 21:38:41 +00:00
info->mode[9] = 'T';
2023-05-16 18:00:22 +00:00
} else if (mode & S_IXOTH) {
2023-04-28 16:31:49 +00:00
info->mode[9] = 'x';
} else {
info->mode[9] = '-';
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* make a null terminated string */
2023-04-28 16:31:49 +00:00
info->mode[10] = '\0';
2023-05-16 18:00:22 +00:00
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/**
* Gets information about a given file_name in a directory and
* stores it into a fileinfo struct
* @param file_name the name of the file in the directory currently
* stored in the path buffer
* @param info the file info struct to store the data into
* @returns if successfull in reading the file
*/
static bool get_file_info(const char* file_name, struct FileInfo* info) {
uid_t uid;
gid_t gid;
int save, ty;
struct stat s;
size_t file_len;
uid = getuid();
gid = getgid();
/* default to all false */
memset(&s, 0, sizeof(struct stat));
/* push the file to the path buffer to stat */
save = push_path_buffer(file_name);
/* get file stats */
if (lstat(get_path_buffer(), &s) < 0) {
error_s("failed to read file '%s'", get_path_buffer());
pop_path_buffer(save);
return false;
}
/* get file type */
ty = (s.st_mode & S_IFMT) >> 12;
/* parse long list mode as octets or human readable */
if (flags.octets) {
info->mode[0] = '0' + (s.st_mode / (8 * 8 * 8) % 8);
info->mode[1] = '0' + (s.st_mode / (8 * 8 * 1) % 8);
info->mode[2] = '0' + (s.st_mode / (8 * 1 * 1) % 8);
info->mode[3] = '0' + (s.st_mode / (1 * 1 * 1) % 8);
info->mode[4] = '\0';
} else {
parse_file_mode(info, s.st_mode, ty);
}
/* check if the file is executable by the user */
if (
(s.st_mode & S_IXUSR && s.st_uid == uid) ||
(s.st_mode & S_IXGRP && s.st_gid == gid) ||
s.st_mode & S_IXOTH
) {
info->exec = true;
} else {
info->exec = false;
}
/* get setuid and setgid bits */
info->set_uid = s.st_mode & S_ISUID;
info->set_gid = s.st_mode & S_ISGID;
/* get user information */
2023-04-28 16:31:49 +00:00
info->usr = getpwuid(s.st_uid);
2023-05-02 22:02:47 +00:00
if (info->usr == NULL) {
2023-05-16 18:00:22 +00:00
error_s("failed to get user id: `%s`", get_path_buffer());
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
return false;
}
2023-05-16 18:00:22 +00:00
/* get group information */
2023-04-28 16:31:49 +00:00
info->grp = getgrgid(s.st_gid);
2023-05-02 22:02:47 +00:00
if (info->grp == NULL) {
2023-05-16 18:00:22 +00:00
error_s("failed to get user id: `%s`", get_path_buffer());
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
return false;
}
2023-05-16 18:00:22 +00:00
/* update inode links and file type */
2023-04-28 16:31:49 +00:00
info->links = s.st_nlink;
2023-04-30 06:12:02 +00:00
info->type = ty;
2023-04-29 00:32:18 +00:00
2023-05-16 18:00:22 +00:00
/* copy the file name into the info */
2023-05-04 20:10:37 +00:00
file_len = strlen(file_name) + 1;
2023-05-15 01:43:02 +00:00
info->name = xalloc(file_len);
2023-05-02 22:02:47 +00:00
memcpy(info->name, file_name, file_len);
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* get the date and time for the file */
2023-04-28 16:31:49 +00:00
print_file_size(s.st_size, info->size);
2023-04-30 06:12:02 +00:00
print_date_time(s.st_mtim.tv_sec + s.st_mtim.tv_nsec / 1000000000, info->date);
2023-05-16 18:00:22 +00:00
/* convert the bytes into human readable */
2023-05-02 22:02:47 +00:00
info->bytes = (s.st_size + s.st_blksize - 1) / s.st_blksize;
2023-05-02 04:37:30 +00:00
2023-05-16 18:00:22 +00:00
/* cleanup */
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 16:31:49 +00:00
return true;
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/**
* Gets the file color from its mode
* @pram info the file info to get the color from
* @return the anscii escape code for the file color
*/
2023-04-28 16:31:49 +00:00
static char* get_file_color(struct FileInfo* info) {
char* color;
2023-04-29 00:32:18 +00:00
if (info->type == DT_DIR) {
2023-04-30 06:12:02 +00:00
if (info->mode[8] == 'w') {
2023-05-16 18:00:22 +00:00
color = DIR_COLOR_EXEC; /* directory is green if anyone can write it */
2023-04-30 06:12:02 +00:00
} else {
2023-05-16 18:00:22 +00:00
color = DIR_COLOR; /* other whise as the french say it, bleu */
2023-04-30 06:12:02 +00:00
}
2023-04-29 00:32:18 +00:00
} else if (info->type == DT_LNK) {
2023-05-16 18:00:22 +00:00
color = LINK_COLOR; /* symlink color */
2023-05-02 04:37:30 +00:00
} else if (info->type == DT_SOCK) {
2023-05-16 18:00:22 +00:00
color = SOCK_COLOR; /* sockt color */
2023-04-30 06:12:02 +00:00
} else if (
info->type == DT_CHR ||
2023-05-02 04:37:30 +00:00
info->type == DT_BLK
2023-04-30 06:12:02 +00:00
) {
2023-05-16 18:00:22 +00:00
color = BLK_COLOR; /* the weird ones are yellow, cool */
2023-04-28 16:31:49 +00:00
} else {
if (info->set_uid) {
2023-05-16 18:00:22 +00:00
color = SET_UID_COLOR; /* hightlighted red if setuid */
2023-04-28 16:31:49 +00:00
} else if (info->set_gid) {
2023-05-16 18:00:22 +00:00
color = SET_GID_COLOR; /* highlighted yellow if setgid and not setuid */
2023-04-28 16:31:49 +00:00
} else if (info->exec) {
2023-05-16 18:00:22 +00:00
color = EXEC_COLOR; /* green if executable */
2023-04-28 16:31:49 +00:00
} else {
2023-05-16 18:00:22 +00:00
color = FILE_COLOR; /* white if basic and boring */
2023-04-28 04:36:15 +00:00
}
2023-04-28 16:31:49 +00:00
}
return color;
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/**
* Lists a file in long info mode
* @param info the global info for a directory
* @param finfo the file specific info
*/
static void list_file_long(struct FileListInfo* info, struct FileInfo* finfo) {
char* color;
color = get_file_color(finfo);
2023-05-02 04:37:30 +00:00
2023-05-16 18:00:22 +00:00
printf("%s %*d %*s %*s %*s %s %s%s%s", /* print data */
finfo->mode,
info->max_link,
finfo->links,
info->max_usr,
finfo->usr->pw_name,
info->max_grp,
finfo->grp->gr_name,
info->max_size,
finfo->size,
finfo->date,
flags.colored != NO ? color : "", /* if not colored dont color */
finfo->name,
flags.colored != NO ? "\x1b[0m" : ""
);
/* if it is a symlink, add the path where it points to */
if (finfo->type == DT_LNK) {
int save = push_path_buffer(finfo->name);
/* get the link */
char lnk[PATH_MAX];
ssize_t n;
if ((n = readlink(get_path_buffer(), lnk, PATH_MAX)) != -1) {
printf(" -> %.*s\n", (int)n, lnk); /* add if successfull */
} else {
putchar('\n'); /* dont if not */
}
pop_path_buffer(save); /* cleanup */
} else {
putchar('\n'); /* if not link put newline now */
}
}
/**
* Print one file in column mode
* @param finfo the file info to be printed
*/
static void list_file_column(struct FileInfo* finfo) {
2023-05-04 20:10:37 +00:00
char* color;
2023-05-16 18:00:22 +00:00
color = get_file_color(finfo);
printf("%s%s%s\n", /* print the data */
flags.colored != NO ? color : "", finfo->name, /* if not colored dont color */
flags.colored != NO ? "\x1b[0m" : ""
);
}
/**
* List a file normally (without -l or -1)
* @param info the global folder information
* @param finfo the file specific info
* @param tty_width the width of the tty
* @param index, the index in the direcory that has been printed
* @param column_width the width of the column
* @param row_count the count of rows
*/
static void list_file_normal(
struct FileListInfo* info,
struct FileInfo* finfo,
int tty_width,
int index,
int column_width,
int row_count
) {
char* color;
color = get_file_color(finfo); /* get file color */
if (info->total_len > tty_width) { /* snap to column if the files span multiple lines */
if (index != 0 && index % row_count == 0) putchar('\n'); /* if at the end of the column put a new line */
printf("%s%*s%s", flags.colored != NO ? color : "", -column_width,
finfo->name, flags.colored != NO ? "\x1b[0m" : ""); /* if not colored dont color */
} else { /* otherwise just print next to eachother seperated by two spaces */
printf("%s%s%s ", flags.colored != NO ? color : "", finfo->name,
flags.colored != NO ? "\x1b[0m" : "");
2023-05-02 04:37:30 +00:00
}
2023-05-16 18:00:22 +00:00
}
2023-05-02 04:37:30 +00:00
2023-05-16 18:00:22 +00:00
/**
* Get the width of the tty, set to 0 if not a tty
* @param width the int pointer to store the width in
*/
static void get_window_size(int* width) {
struct winsize w;
2023-04-28 16:31:49 +00:00
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
2023-04-28 19:48:55 +00:00
if (!isatty(1)) {
2023-05-16 18:00:22 +00:00
*width = 0;
} else {
*width = w.ws_col;
}
}
/**
* List a set of files
* @param files a array of file info structs containing file data
* @param file_len the amount of files
* @param info the global folder infomation
*/
static void list_files(struct FileInfo* files, int file_len, struct FileListInfo info) {
/* pre define variables to make gcc happy */
int column_width = 0, row_count = 0, i = 0, width = 0;
get_window_size(&width); /* get screen width */
/* if your not a tty, set to column mode and disable color if not forced */
if (width > 0) {
column_width = info.max_name + 1;
row_count = width / column_width;
} else {
2023-05-01 22:43:32 +00:00
flags.one_column = true;
if (flags.colored == AUTO)
flags.colored = NO;
2023-04-28 19:48:55 +00:00
}
2023-05-16 18:00:22 +00:00
/* if set to long list, display folder size */
if (flags.more_info) {
char total[13];
print_file_size(info.total_size, total);
printf("total %s\n", total);
}
2023-04-28 16:31:49 +00:00
2023-05-16 18:00:22 +00:00
/* list files base on list mode */
2023-05-04 20:10:37 +00:00
for (i = 0; i < file_len; i++) {
2023-04-28 16:31:49 +00:00
struct FileInfo finfo = files[i];
2023-05-16 18:00:22 +00:00
if (flags.more_info) { /* -l long list */
list_file_long(&info, &finfo);
} else if (flags.one_column) { /* -1 column list */
list_file_column(&finfo);
} else { /* normal default listing */
list_file_normal(&info, &finfo, width, i, column_width, row_count);
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
free(finfo.name); /* cleanup */
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
if (!flags.more_info) printf("\n"); /* only long long list prints a new line at the end */
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
/**
* Get the number of places a number fills
* @param n the number to check
* @returns the num of places the number filles
*/
2023-04-28 16:31:49 +00:00
static int num_places (int n) {
int r = 1;
if (n < 0) n = (n == INT_MIN) ? INT_MAX: -n;
2023-05-16 18:00:22 +00:00
while (n > 9) { /* keep going for each place */
2023-04-28 16:31:49 +00:00
n /= 10;
r++;
}
return r;
}
2023-05-16 18:00:22 +00:00
/**
* Read and push a file info the given array
* @param file a pointer to the list of files
* @param info the global folder info to update as read
* @param size the amount of files currently in the array
* @param capacity the capacity of the array
* @param file_path the path of the file
*/
2023-04-28 16:31:49 +00:00
static void push_file(
struct FileInfo** files,
struct FileListInfo* info,
int* size, int* capacity,
2023-05-02 22:02:47 +00:00
const char* file_path
2023-04-28 16:31:49 +00:00
) {
2023-05-16 18:00:22 +00:00
/* allocate variables on the stack */
2023-04-28 16:31:49 +00:00
struct FileInfo finfo;
2023-05-04 20:10:37 +00:00
int user_len, group_len, name_len, size_len, link_len;
2023-05-16 18:00:22 +00:00
/* if you fail to get the file info, abort */
2023-05-02 22:02:47 +00:00
if (!get_file_info(file_path, &finfo)) return;
2023-04-28 16:31:49 +00:00
2023-05-16 18:00:22 +00:00
/* if the array is full realloc to make it bigger */
2023-04-28 16:31:49 +00:00
if (*size == *capacity) {
*capacity *= 2;
2023-05-15 01:43:02 +00:00
*files = xrealloc(*files, sizeof(struct FileInfo) * *capacity);
2023-04-28 16:31:49 +00:00
}
2023-05-04 20:10:37 +00:00
2023-05-16 18:00:22 +00:00
/* update user name length if its larger than global */
2023-05-04 20:10:37 +00:00
user_len = strlen(finfo.usr->pw_name);
2023-04-28 16:31:49 +00:00
if (user_len > info->max_usr) info->max_usr = user_len;
2023-05-16 18:00:22 +00:00
/* update group name length if its larger than global */
2023-05-04 20:10:37 +00:00
group_len = strlen(finfo.grp->gr_name);
2023-04-28 16:31:49 +00:00
if (group_len > info->max_grp) info->max_grp = group_len;
2023-05-16 18:00:22 +00:00
/* update file name length if its larger than global */
2023-05-04 20:10:37 +00:00
name_len = strlen(file_path);
2023-04-28 16:31:49 +00:00
if (name_len > info->max_name) info->max_name = name_len;
2023-05-16 18:00:22 +00:00
/* update file size human readable length if its larger than global */
2023-05-04 20:10:37 +00:00
size_len = strlen(finfo.size);
2023-04-28 16:31:49 +00:00
if (size_len > info->max_size) info->max_size = size_len;
2023-05-16 18:00:22 +00:00
/* update inode link place count length if its larger than global */
2023-05-04 20:10:37 +00:00
link_len = num_places(finfo.links);
2023-04-28 16:31:49 +00:00
if (link_len > info->max_link) info->max_link = link_len;
2023-05-16 18:00:22 +00:00
/* update final info */
2023-04-28 16:31:49 +00:00
info->total_len += name_len + 2;
2023-05-02 04:37:30 +00:00
info->total_size += finfo.bytes;
2023-04-28 16:31:49 +00:00
2023-05-16 18:00:22 +00:00
/* insert file data into array */
2023-04-28 16:31:49 +00:00
(*files)[*size] = finfo;
2023-05-16 18:00:22 +00:00
(*size)++; /* increment size counter */
2023-04-28 16:31:49 +00:00
}
2023-05-16 18:00:22 +00:00
/**
* When recursing, read all files and folders, recurses each folder, and prints each file
* @param dir_name the dir_name relative to the current path buffer
*/
2023-05-02 22:02:47 +00:00
static void recurse_directory(char* dir_name) {
2023-05-16 18:00:22 +00:00
/* allocate variables on the stack */
2023-04-28 04:36:15 +00:00
DIR* d;
2023-05-04 20:10:37 +00:00
int capacity, size, save;
2023-04-28 04:36:15 +00:00
struct dirent* file;
2023-05-04 20:10:37 +00:00
struct FileInfo* files;
struct FileListInfo info;
2023-05-16 18:00:22 +00:00
/* push the dir name to the path buffer */
2023-05-04 20:10:37 +00:00
save = push_path_buffer(dir_name);
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* if failed to open dir abort */
2023-05-02 22:02:47 +00:00
d = get_directory(get_path_buffer());
if (d == NULL) {
return;
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* allocate initial array*/
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
2023-05-15 01:43:02 +00:00
files = xalloc(sizeof(struct FileInfo) * capacity);
2023-05-16 18:00:22 +00:00
memset(&info, 0, sizeof(struct FileListInfo)); /* zero out default directory globals */
2023-04-28 16:31:49 +00:00
2023-05-16 18:00:22 +00:00
/* for each file read, recurse if directory, read file if file */
2023-04-28 04:36:15 +00:00
while((file = readdir(d)) != NULL) {
2023-05-16 18:00:22 +00:00
if (!flags.hidden && prefix(".", file->d_name)) continue; /* if not set to show hidden, skip */
if (flags.hide_dot && is_dot_dir(file->d_name)) continue; /* if its set to hide dot skip */
if (file->d_type == DT_DIR && !is_dot_dir(file->d_name)) { /* dont recurse dot files or scary infinite loop */
recurse_directory(file->d_name); /* recurse if directory */
2023-05-02 22:02:47 +00:00
} else {
2023-05-16 18:00:22 +00:00
push_file(&files, &info, &size, &capacity, file->d_name); /* read file if a file */
2023-04-28 04:36:15 +00:00
}
}
2023-05-16 18:00:22 +00:00
/* print dir name */
2023-05-04 20:10:37 +00:00
if (flags.colored == NO) {
2023-05-02 22:02:47 +00:00
printf("\n%s:\n", get_path_buffer());
2023-05-04 20:10:37 +00:00
} else {
2023-05-02 22:02:47 +00:00
printf("\n%s%s:%s\n", DIR_COLOR, get_path_buffer(), FILE_COLOR);
2023-05-04 20:10:37 +00:00
}
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* list files */
2023-05-02 22:02:47 +00:00
list_files(files, size, info);
2023-05-16 18:00:22 +00:00
free(files); /* clean up */
2023-04-28 04:36:15 +00:00
closedir(d);
2023-05-02 22:02:47 +00:00
pop_path_buffer(save);
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
/**
* List directory non recursivly or recursivly
* @param path the name of the directory to search
*/
2023-05-02 22:02:47 +00:00
static void list_directory(char* path) {
2023-05-04 20:10:37 +00:00
2023-05-16 18:00:22 +00:00
/* allocate variables on the stack */
2023-05-04 20:10:37 +00:00
DIR* d;
int capacity, size, save;
struct FileInfo* files;
struct FileListInfo info;
struct dirent* file;
2023-05-16 18:00:22 +00:00
/* if set to recurse, run the recurse function */
2023-05-01 22:43:32 +00:00
if (flags.recurse) {
2023-05-02 22:02:47 +00:00
recurse_directory(path);
return;
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
/* if failed to open directory, abort */
2023-05-04 20:10:37 +00:00
d = get_directory(path);
2023-05-02 22:02:47 +00:00
if (d == NULL) return;
2023-05-16 18:00:22 +00:00
/* push folder to path buffer */
2023-05-04 20:10:37 +00:00
save = push_path_buffer(path);
2023-04-28 04:36:15 +00:00
2023-05-16 18:00:22 +00:00
/* set array defaults */
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
2023-05-16 18:00:22 +00:00
/* allocate file array */
2023-05-15 01:43:02 +00:00
files = xalloc(sizeof(struct FileInfo) * capacity);
2023-05-16 18:00:22 +00:00
memset(&info, 0, sizeof(struct FileListInfo)); /* zero out directory globals */
2023-04-28 16:31:49 +00:00
2023-05-16 18:00:22 +00:00
/* add each file regardless if its a directory or file since not recursing */
2023-04-28 04:36:15 +00:00
while ((file = readdir(d)) != NULL) {
2023-05-16 18:00:22 +00:00
if (!flags.hidden && prefix(".", file->d_name)) continue; /* if not set to show hidden ship */
if (flags.hide_dot && is_dot_dir(file->d_name)) continue; /* if set to hide dot skip */
push_file(&files, &info, &size, &capacity, file->d_name); /* add file to array */
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
/* only list files if there are any to list */
2023-05-02 22:02:47 +00:00
if (size > 0) list_files(files, size, info);
2023-05-16 18:00:22 +00:00
/* cleanup */
free(files);
2023-04-28 04:36:15 +00:00
closedir(d);
2023-05-16 18:00:22 +00:00
pop_path_buffer(save);
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
/* return if a path is a directory */
2023-04-30 06:12:02 +00:00
static bool is_dir(const char* path) {
struct stat s;
if (stat(path, &s) < 0) return false;
return S_ISDIR(s.st_mode);
}
2023-05-16 18:00:22 +00:00
/**
* Goes though each argument and prints the argument if it is a file
* @param start the argument to start at
* @param argc the argument count
* @param argv, the argument data
*/
2023-05-01 22:43:32 +00:00
static void list_file_args(int start, int argc, char** argv) {
2023-05-04 20:10:37 +00:00
2023-05-16 18:00:22 +00:00
/* allocate variables on the stack */
2023-05-04 20:10:37 +00:00
int capacity, size, i;
struct FileInfo* files;
2023-04-30 06:12:02 +00:00
struct FileListInfo info;
2023-05-04 20:10:37 +00:00
2023-05-16 18:00:22 +00:00
/* array defaults */
2023-05-04 20:10:37 +00:00
capacity = 8;
size = 0;
2023-05-16 18:00:22 +00:00
files = xalloc(sizeof(struct FileInfo) * capacity); /* allocate array */
memset(&info, 0, sizeof(struct FileListInfo)); /* zero out globals */
2023-04-30 06:12:02 +00:00
2023-05-04 20:10:37 +00:00
for (i = start; i < argc; i++) {
2023-05-16 18:00:22 +00:00
if (is_dir(argv[i])) continue; /* if the argument is a dir skip */
2023-05-04 20:10:37 +00:00
push_file(&files, &info, &size, &capacity, argv[i]);
2023-04-30 06:12:02 +00:00
}
2023-05-16 18:00:22 +00:00
/* print file args */
2023-05-02 22:02:47 +00:00
if (size > 0) list_files(files, size, info);
2023-04-30 06:12:02 +00:00
2023-05-16 18:00:22 +00:00
/* clean up */
2023-04-30 06:12:02 +00:00
free(files);
}
2023-05-16 18:00:22 +00:00
/**
* Prints help message for ls
*/
2023-05-01 22:43:32 +00:00
static void help(void) {
2023-04-28 04:36:15 +00:00
printf("Usage: ls [FILE]...\n\n");
printf("List directory contents\n\n");
printf("\t-1\tOne column output\n");
printf("\t-l\tLong format\n");
2023-05-16 18:00:22 +00:00
printf("\t-o\tList raw octets instead of romanized file mode\n");
2023-04-28 04:36:15 +00:00
printf("\t-a\tInclude names starting with .\n");
printf("\t-A\tLike -a but without . and ..\n");
printf("\t-R\tRecurse\n");
2023-05-01 22:43:32 +00:00
}
2023-05-16 18:00:22 +00:00
/**
* Takes in each argument that has a single - and parses it
* @param c the character after the -
* @param next the next argument in argv that hasnt been parsed
* @reutrn if the next arg was used or if the arg was invalid
*/
2023-05-01 22:43:32 +00:00
static int short_arg(char c, char* next) {
2023-05-16 18:00:22 +00:00
UNUSED(next); /* next arg unused */
2023-05-01 22:43:32 +00:00
switch (c) {
case 'R':
flags.recurse = true;
break;
case '1':
flags.one_column = true;
break;
case 'A':
flags.hide_dot = true;
flags.hidden = true;
break;
case 'a':
flags.hidden = true;
break;
case 'l':
flags.more_info = true;
break;
2023-05-16 18:00:22 +00:00
case 'o':
flags.octets = true;
break;
2023-05-01 22:43:32 +00:00
default:
2023-05-03 16:17:56 +00:00
return ARG_INVALID;
2023-05-01 22:43:32 +00:00
}
return ARG_UNUSED;
}
2023-05-16 18:00:22 +00:00
/**
* Takes in each long argument (--)
* @param cur the current argument
* @param next the next argument in argv that hasnt been parsed
* @param if the next arg was used or if the arg was invalid
*/
2023-05-01 22:43:32 +00:00
static int long_arg(char* cur, char* next) {
2023-05-16 18:00:22 +00:00
UNUSED(next); /* next arg unused */
if (prefix("--color=", cur)) { /* parse --color argument */
2023-05-01 22:43:32 +00:00
char* arg = cur + 8;
if (streql("yes", arg) || streql("always", arg)) {
flags.colored = YES;
} else if (streql("auto", arg)) {
flags.colored = AUTO;
} else if (streql("no", arg) || streql("never", arg)) {
flags.colored = NO;
} else {
2023-05-03 16:17:56 +00:00
error("invalid color options: %s", arg);
2023-05-01 22:43:32 +00:00
}
} else {
2023-05-16 18:00:22 +00:00
return ARG_IGNORE; /* stop reading arguments */
2023-05-01 22:43:32 +00:00
}
2023-05-16 18:00:22 +00:00
return ARG_UNUSED; /* return */
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
/**
* Lists a directorys contents
*/
2023-05-15 14:57:33 +00:00
COMMAND(ls_main) {
2023-04-28 04:36:15 +00:00
2023-05-04 20:10:37 +00:00
int start, i;
bool titled;
2023-05-16 18:00:22 +00:00
/* initalize flag defaults */
2023-04-28 04:36:15 +00:00
flags.hidden = false;
flags.more_info = false;
flags.hide_dot = false;
flags.one_column = false;
flags.recurse = false;
2023-05-03 16:17:56 +00:00
flags.colored = NO;
2023-05-16 18:00:22 +00:00
flags.octets = false;
2023-04-29 00:32:18 +00:00
2023-05-16 18:00:22 +00:00
/* parse given arguments */
2023-05-04 20:10:37 +00:00
start = parse_args(argc, argv, help, short_arg, long_arg);
2023-05-16 18:00:22 +00:00
/* if not arguments are given list current directory */
2023-04-28 04:36:15 +00:00
if (argc - start == 0) {
2023-05-01 22:43:32 +00:00
list_directory(".");
2023-04-28 04:36:15 +00:00
return EXIT_SUCCESS;
}
2023-05-16 18:00:22 +00:00
/* list all arguments that are files */
2023-05-01 22:43:32 +00:00
list_file_args(start, argc, argv);
2023-04-30 06:12:02 +00:00
2023-05-16 18:00:22 +00:00
/* list each directory in the arguments */
2023-05-04 20:10:37 +00:00
titled = argc - start > 1;
for (i = start; i < argc; i++) {
2023-05-16 18:00:22 +00:00
if (!is_dir(argv[i])) continue; /* if not directory skip */
2023-05-04 20:10:37 +00:00
2023-05-16 18:00:22 +00:00
if (titled && !flags.recurse) { /* list title of title is forced and not set to recurse*/
2023-05-04 20:10:37 +00:00
if (flags.colored != NO) {
2023-04-30 06:12:02 +00:00
printf("\n%s%s:%s\n", DIR_COLOR, argv[i], FILE_COLOR);
2023-05-04 20:10:37 +00:00
} else {
2023-04-30 06:12:02 +00:00
printf("\n%s:\n", argv[i]);
2023-05-04 20:10:37 +00:00
}
2023-04-28 04:36:15 +00:00
}
2023-05-04 20:10:37 +00:00
2023-05-16 18:00:22 +00:00
list_directory(argv[i]); /* parse and list directory */
2023-04-28 04:36:15 +00:00
}
2023-05-16 18:00:22 +00:00
return EXIT_SUCCESS; /* return */
2023-04-28 04:36:15 +00:00
}