fix du, ls

This commit is contained in:
Your Name 2023-11-21 22:18:42 +03:00
parent ba63f06f75
commit 6a631cf9c0
6 changed files with 97 additions and 57 deletions

1
TODO
View File

@ -1,7 +1,6 @@
With "micro-" prefix
*Todo:
**ls (mc print)
**dmesg (portable)
tail
expr

View File

@ -4,53 +4,30 @@
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <stdint.h>
#include "get_stat.h"
#include "make_path.h"
#include "human.c"
unsigned int h_flag;
unsigned int s_flag;
unsigned int b_flag;
unsigned int m_flag;
unsigned int c_flag;
unsigned int b_flag;
unsigned int n_flag;
double total;
void print(double size, const char *filename) {
if (c_flag)
total += size;
off_t blksize;
off_t total;
char c = 0;
void print(off_t size, const char *filename) {
if (h_flag)
printf("%s\t%s%c", mu_humansize(size * blksize), filename, (n_flag) ? '\0' : '\n');
if (h_flag) {
if (size < 1048576 && !m_flag) {
size = size / 1024;
c = 'K';
}
else if (size < 1073741824) {
size = size / 1048576;
c = 'M';
}
else if (size < 1099511627776) {
size = size / 1073741824;
c = 'G';
}
}
/* Kb */
else if (!b_flag)
size = size / 1024;
/* Mb */
else if (m_flag)
size = size / 1048576;
printf("%.1f%c\t%s%c", size, c, filename, (n_flag) ? '\0' : '\n');
else
printf("%jd\t%s%c", (intmax_t)size, filename, (n_flag) ? '\0' : '\n');
}
double du(const char *path, int recurs_flag) {
double sum = 0;
off_t du(const char *path, int recurs_flag) {
off_t sum = 0;
struct stat sb;
if (mu_get_lstat("du", path, &sb))
@ -89,8 +66,17 @@ double du(const char *path, int recurs_flag) {
print(sum, path);
}
/* Get file size */
else {
sum = sb.st_size;
if (b_flag)
sum = sb.st_size;
else
sum = (512 * sb.st_blocks + blksize - 1) / blksize;
if (c_flag)
total += sum;
if (!recurs_flag)
print(sum, path);
}
@ -99,9 +85,10 @@ double du(const char *path, int recurs_flag) {
}
int main(int argc, char **argv) {
blksize = 512;
int opt;
while ((opt = getopt(argc, argv, "hsbmc0")) != -1) {
while ((opt = getopt(argc, argv, "hsbc0")) != -1) {
switch (opt) {
case 'h':
h_flag = 1;
@ -115,11 +102,6 @@ int main(int argc, char **argv) {
b_flag = 1;
break;
case 'm':
b_flag = 1;
m_flag = 1;
break;
case 'c':
c_flag = 1;
break;
@ -129,7 +111,7 @@ int main(int argc, char **argv) {
break;
default:
printf("du [file1 file2...]\n\t[-h Sizes in human readable format]\n\t[-s Display only a total for each argument]\n\t[-b Apparent size] [-m Size in megabyte]\n\t[-c produce a grand total]\n\t[-0 end each output line with NUL, not newline]\n");
printf("du [file1 file2...]\n\t[-h Sizes in human readable format]\n\t[-s Display only a total for each argument]\n\t[-b Size in bytes] [-c produce a grand total]\n\t[-0 end each output line with NUL, not newline]\n");
return 0;
}
}
@ -150,4 +132,3 @@ int main(int argc, char **argv) {
return 0;
}

View File

@ -125,8 +125,8 @@ void GetPerm(struct stat sb) {
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;
int print(const struct d_node *node) {
char suf = ' ';
if (F_flag) {
if (S_ISDIR(node->stats.st_mode))
suf = '/';
@ -136,7 +136,7 @@ void print(const struct d_node *node) {
}
if (!l_flag)
printf("%s%c", node->name, suf);
return printf("%s%c", node->name, suf);
if (l_flag) {
GetPerm(node->stats);
@ -145,18 +145,55 @@ void print(const struct d_node *node) {
char date[14];
if (!strftime(date, sizeof(date), "%b %d %H:%M", tm))
return;
return 0;
struct passwd *pw = getpwuid(node->stats.st_uid);
struct group *gr = getgrgid(node->stats.st_gid);
printf(" %s %s %10jd %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);
return printf(" %s %s %10jd %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);
}
return 0;
}
void col_print(struct d_node **node, size_t files, struct winsize w) {
/* Get max len */
size_t maxlen = 0;
for (size_t i = 0; i < files; i++)
if (strlen(node[i]->name) > maxlen)
maxlen = strlen(node[i]->name) + 3;
size_t ncols = w.ws_col / maxlen;
size_t nrows = files;
if (ncols > 1)
nrows = files / ncols;
int col = 0;
int nexttab = 0;
/* Mc print */
for (size_t i = 0; i < nrows; i++) {
for (size_t j = 0; j < ncols; j++) {
if (i < files) {
if (col > 0) {
nexttab -= col;
for (int k = 0; k < nexttab; k++)
putchar(' ');
col += nexttab;
}
nexttab = col + (int)maxlen;
col += print(node[i * ncols + j]);
}
}
putchar('\n');
col = 0;
}
}
int ls(const char *dir_name, int label, struct winsize w) {
(void)w;
size_t files = 0;
struct d_node **dir = list(dir_name, &files);
if (dir == NULL)
@ -165,6 +202,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
if (label)
printf("\n%s:\n", dir_name);
/* pipe print */
if (!p_flag || l_flag) {
for (size_t i = 0; i < files; i++) {
print(dir[i]);
@ -172,8 +210,9 @@ int ls(const char *dir_name, int label, struct winsize w) {
}
}
/* Todo: sort and print */
else {}
/* mc print */
else
col_print(dir, files, w);
dfree(dir);
return 0;
@ -222,4 +261,3 @@ int main(int argc, char **argv) {
return 0;
}

22
libmu/human.c Normal file
View File

@ -0,0 +1,22 @@
#ifndef _HUMAN_H
#define _HUMAN_H
char *mu_humansize(off_t n) {
static char buf[16];
char *postfixes = "BKMGTPE";
double size = n;
size_t i;
for (i = 0; i < strlen(postfixes) && size >= 1024; i++)
size /= 1024;
if (i)
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
else
snprintf(buf, sizeof(buf), "%ju", (uintmax_t)n);
return buf;
}
#endif

View File

@ -44,7 +44,8 @@ int mu_recurse(const char *prog_name, int link_flag, const char *path, void *arg
closedir(dir);
if (dir_act != NULL)
dir_act(path, arg);
if (dir_act(path, arg))
ret = 1;
return ret;
}

View File

@ -63,4 +63,3 @@ int main(int argc, char **argv) {
free(buf);
return 0;
}