fix
This commit is contained in:
parent
047beae5b5
commit
026d9a7f78
3
TODO
3
TODO
|
@ -5,9 +5,10 @@ With "micro-" prefix
|
||||||
**ls
|
**ls
|
||||||
**tee
|
**tee
|
||||||
tail
|
tail
|
||||||
|
expr
|
||||||
uniq
|
uniq
|
||||||
head
|
head
|
||||||
cal
|
od
|
||||||
nice
|
nice
|
||||||
renice
|
renice
|
||||||
nohup
|
nohup
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
void format(char *str) {
|
void format(char *str) {
|
||||||
for (size_t i = 0; i < strlen(str); i++) {
|
for (size_t i = 0; i < strlen(str); i++) {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <libgen.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
@ -20,7 +21,12 @@ unsigned int F_flag;
|
||||||
unsigned int p_flag;
|
unsigned int p_flag;
|
||||||
|
|
||||||
struct d_node {
|
struct d_node {
|
||||||
|
/* basename() */
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
|
/* For free */
|
||||||
|
char *full_name;
|
||||||
|
|
||||||
struct d_node *next;
|
struct d_node *next;
|
||||||
struct stat stats;
|
struct stat stats;
|
||||||
};
|
};
|
||||||
|
@ -34,6 +40,8 @@ struct d_node *stat_file(char *filename) {
|
||||||
if (mu_get_lstat("ls", filename, &file->stats))
|
if (mu_get_lstat("ls", filename, &file->stats))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
file->full_name = filename;
|
||||||
|
file->name = basename(filename);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,20 +65,17 @@ struct d_node **list(const char *path, size_t *nfiles) {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
cur = stat_file(full_path);
|
cur = stat_file(full_path);
|
||||||
if (cur == NULL) {
|
if (cur == NULL)
|
||||||
free(full_path);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
free(full_path);
|
|
||||||
|
|
||||||
cur->name = ep->d_name;
|
|
||||||
cur->next = dr;
|
cur->next = dr;
|
||||||
dr = cur;
|
dr = cur;
|
||||||
|
|
||||||
files++;
|
files++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closedir(dp);
|
||||||
|
|
||||||
if (dr == NULL)
|
if (dr == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -89,7 +94,6 @@ struct d_node **list(const char *path, size_t *nfiles) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dp);
|
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +101,7 @@ void dfree(struct d_node **dir) {
|
||||||
struct d_node *cur = dir[0], *next;
|
struct d_node *cur = dir[0], *next;
|
||||||
while (cur != NULL) {
|
while (cur != NULL) {
|
||||||
next = cur->next;
|
next = cur->next;
|
||||||
|
free(cur->full_name);
|
||||||
free(cur);
|
free(cur);
|
||||||
cur = next;
|
cur = next;
|
||||||
}
|
}
|
||||||
|
@ -105,6 +110,24 @@ void dfree(struct d_node **dir) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print */
|
/* 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) {
|
void print(const struct d_node *node) {
|
||||||
char suf = 0;
|
char suf = 0;
|
||||||
if (F_flag) {
|
if (F_flag) {
|
||||||
|
@ -115,11 +138,26 @@ void print(const struct d_node *node) {
|
||||||
suf = '*';
|
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) {
|
int ls(const char *dir_name, int label, struct winsize w) {
|
||||||
/* Unused, tmp */
|
|
||||||
(void)w;
|
(void)w;
|
||||||
|
|
||||||
size_t files = 0;
|
size_t files = 0;
|
||||||
|
@ -130,7 +168,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
|
||||||
if (label)
|
if (label)
|
||||||
printf("\n%s:\n", dir_name);
|
printf("\n%s:\n", dir_name);
|
||||||
|
|
||||||
if (!p_flag)
|
if (!p_flag || l_flag)
|
||||||
for (size_t i = 0; i < files; i++) {
|
for (size_t i = 0; i < files; i++) {
|
||||||
print(dir[i]);
|
print(dir[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user