This commit is contained in:
Your Name 2023-11-10 13:10:25 +03:00
parent 047beae5b5
commit 026d9a7f78
3 changed files with 51 additions and 11 deletions

3
TODO
View File

@ -5,9 +5,10 @@ With "micro-" prefix
**ls
**tee
tail
expr
uniq
head
cal
od
nice
renice
nohup

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void format(char *str) {
for (size_t i = 0; i < strlen(str); i++) {

View File

@ -8,6 +8,7 @@
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
@ -20,7 +21,12 @@ unsigned int F_flag;
unsigned int p_flag;
struct d_node {
/* basename() */
char *name;
/* For free */
char *full_name;
struct d_node *next;
struct stat stats;
};
@ -34,6 +40,8 @@ struct d_node *stat_file(char *filename) {
if (mu_get_lstat("ls", filename, &file->stats))
return NULL;
file->full_name = filename;
file->name = basename(filename);
return file;
}
@ -57,20 +65,17 @@ struct d_node **list(const char *path, size_t *nfiles) {
continue;
cur = stat_file(full_path);
if (cur == NULL) {
free(full_path);
if (cur == NULL)
continue;
}
free(full_path);
cur->name = ep->d_name;
cur->next = dr;
dr = cur;
files++;
}
closedir(dp);
if (dr == NULL)
return NULL;
@ -89,7 +94,6 @@ struct d_node **list(const char *path, size_t *nfiles) {
break;
}
closedir(dp);
return dir;
}
@ -97,6 +101,7 @@ void dfree(struct d_node **dir) {
struct d_node *cur = dir[0], *next;
while (cur != NULL) {
next = cur->next;
free(cur->full_name);
free(cur);
cur = next;
}
@ -105,6 +110,24 @@ void dfree(struct d_node **dir) {
}
/* Print */
void GetPerm(struct stat sb) {
if (S_ISDIR(sb.st_mode))
putchar('d');
else if (S_ISFIFO(sb.st_mode))
putchar('p');
else if (S_ISSOCK(sb.st_mode))
putchar('s');
else
putchar('-');
printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-');
printf("%c%c%c", (sb.st_mode & S_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-');
printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-');
}
void print(const struct d_node *node) {
char suf = 0;
if (F_flag) {
@ -115,11 +138,26 @@ void print(const struct d_node *node) {
suf = '*';
}
printf("%s%c", node->name, suf);
if (!p_flag && !l_flag)
printf("%s%c", node->name, suf);
if (l_flag) {
GetPerm(node->stats);
struct tm *tm = localtime(&node->stats.st_mtime);
char date[14];
if (!strftime(date, sizeof(date), "%b %d %H:%M", tm))
return;
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
printf(" %s %s %jd %s %s%c", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)node->stats.st_size, date, node->name, suf);
}
}
int ls(const char *dir_name, int label, struct winsize w) {
/* Unused, tmp */
(void)w;
size_t files = 0;
@ -130,7 +168,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
if (label)
printf("\n%s:\n", dir_name);
if (!p_flag)
if (!p_flag || l_flag)
for (size_t i = 0; i < files; i++) {
print(dir[i]);
putchar('\n');