fixed
This commit is contained in:
parent
ae7c8a4668
commit
0e63796bb1
3
config.h
3
config.h
|
@ -10,9 +10,6 @@
|
|||
/* format for printf (head) */
|
||||
#define HEAD_FMT "==> %s <==\n"
|
||||
|
||||
/* block size (du) */
|
||||
#define BLK_SIZE 512
|
||||
|
||||
/* mount config */
|
||||
#define MOUNT_CFG "/etc/fstab"
|
||||
#define MOUNT_DEF_FS "ext4"
|
||||
|
|
|
@ -28,7 +28,7 @@ int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path
|
|||
return 0;
|
||||
}
|
||||
|
||||
int mu_get_stats(const char *restrict prog_name, int flag, const char *restrict path, struct stat *restrict stat_path) {
|
||||
int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path) {
|
||||
if (flag)
|
||||
return mu_get_lstat(prog_name, path, stat_path);
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef _GET_STRING_H
|
||||
#define _GET_STRING_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
@ -8,7 +9,9 @@ int mu_get_string(const char *prog_name, char *buf, const size_t len) {
|
|||
off_t rbytes = read(STDIN_FILENO, buf, len);
|
||||
|
||||
if (rbytes <= 0) {
|
||||
fprintf(stderr, "%s: %s\n", prog_name, strerror(errno));
|
||||
if (prog_name != NULL)
|
||||
fprintf(stderr, "%s: %s\n", prog_name, strerror(errno));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,14 +3,16 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
char *mu_humansize(off_t n) {
|
||||
static char buf[16];
|
||||
#define MU_HUMAN_BUF_SIZE 16
|
||||
|
||||
char *mu_humansize(off_t n, off_t block) {
|
||||
static char buf[MU_HUMAN_BUF_SIZE + 1];
|
||||
char *postfixes = "BKMGTPE";
|
||||
|
||||
double size = n;
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(postfixes) && size >= 1024; i++)
|
||||
size /= 1024;
|
||||
for (i = 0; i < strlen(postfixes) && size >= block; i++)
|
||||
size /= block;
|
||||
|
||||
if (i)
|
||||
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *mu_make_path(const char *restrict prog_name, const char *restrict src, const char *restrict dst) {
|
||||
char *mu_make_path(const char *prog_name, const char *src, const char *dst) {
|
||||
int flag = 0;
|
||||
|
||||
if (src == NULL) {
|
||||
flag = 1;
|
||||
src = "";
|
||||
|
|
|
@ -10,14 +10,18 @@ int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
|
|||
return 0;
|
||||
|
||||
if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') {
|
||||
fprintf(stderr, "%s: Access denied\n", prog_name);
|
||||
if (prog_name != NULL)
|
||||
fprintf(stderr, "%s: Access denied\n", prog_name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(pass, pw->pw_passwd))
|
||||
return 0;
|
||||
|
||||
fprintf(stderr, "%s: Incorrect password\n", prog_name);
|
||||
if (prog_name != NULL)
|
||||
fprintf(stderr, "%s: Incorrect password\n", prog_name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "get_stat.h"
|
||||
|
||||
int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) {
|
||||
|
||||
struct stat sb;
|
||||
if (mu_get_stats(prog_name, link_flag, path, &sb))
|
||||
return 1;
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include "get_stat.h"
|
||||
|
||||
const char *fmts[] = {
|
||||
"%R",
|
||||
|
@ -43,13 +45,18 @@ time_t parse_date(char *str) {
|
|||
int main(int argc, char **argv) {
|
||||
time_t t = time(NULL);
|
||||
char *fmt = "%a %b %e %H:%M:%S %Z %Y";
|
||||
char *r_flag = 0;
|
||||
|
||||
/* For -s flag */
|
||||
struct timespec ts;
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "s:d:u")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "s:d:r:u")) != -1) {
|
||||
switch (opt) {
|
||||
case 'r':
|
||||
r_flag = optarg;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
ts.tv_sec = parse_date(optarg);
|
||||
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
|
||||
|
@ -72,7 +79,7 @@ int main(int argc, char **argv) {
|
|||
break;
|
||||
|
||||
default:
|
||||
printf("date [+\"fmt\"]\n\t[-s DATE Set new date]\n\t[-d DATE Print new date]\n\t[-u Work in UTC]\n");
|
||||
printf("date [+\"fmt\"]\n\t[-s DATE Set new date]\n\t[-d DATE Print new date]\n\t[-u Work in UTC]\n\t[-r FILE Display last modification time of FILE]\n");
|
||||
printf("\nFormats:\n");
|
||||
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++)
|
||||
printf("\t%s\n", fmts[i]);
|
||||
|
@ -88,6 +95,14 @@ int main(int argc, char **argv) {
|
|||
if (argv[0][0] == '+')
|
||||
fmt = argv[0] + 1;
|
||||
|
||||
struct stat sb;
|
||||
if (r_flag) {
|
||||
if (mu_get_stat("date", r_flag, &sb))
|
||||
return 1;
|
||||
|
||||
t = sb.st_mtime;
|
||||
}
|
||||
|
||||
struct tm *tm = localtime(&t);
|
||||
|
||||
char buf[256];
|
||||
|
@ -96,3 +111,4 @@ int main(int argc, char **argv) {
|
|||
puts(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ void summary(void) {
|
|||
fprintf(stderr, "%zu+%zu records in\n", infull, inpart);
|
||||
fprintf(stderr, "%zu+%zu records out\n", outfull, outpart);
|
||||
|
||||
fprintf(stderr, "%s total bytes copied\n", mu_humansize(tbytes));
|
||||
fprintf(stderr, "%s total bytes copied\n", mu_humansize(tbytes, 1024));
|
||||
}
|
||||
|
||||
int openfile(int flag, char *path, int mode) {
|
||||
|
@ -153,7 +153,7 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
char *obuf = ibuf;
|
||||
char *obuf = NULL;
|
||||
if (ibs != obs) {
|
||||
obuf = malloc(obs);
|
||||
if (obuf == NULL) {
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
project_dir=$(pwd)
|
||||
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC
|
|
@ -0,0 +1,92 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <mntent.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include "human.h"
|
||||
#include "config.h"
|
||||
|
||||
unsigned int a_flag;
|
||||
unsigned int h_flag;
|
||||
off_t block = 1024;
|
||||
|
||||
void print_human(const char *src, const off_t total, const off_t used, const off_t avail, const off_t capacity, const char *dst) {
|
||||
char total_s[MU_HUMAN_BUF_SIZE + 1];
|
||||
char used_s[MU_HUMAN_BUF_SIZE + 1];
|
||||
char avail_s[MU_HUMAN_BUF_SIZE + 1];
|
||||
|
||||
snprintf(total_s, sizeof(total_s), "%s", mu_humansize(total * block, block));
|
||||
snprintf(used_s, sizeof(used_s), "%s", mu_humansize(used * block, block));
|
||||
snprintf(avail_s, sizeof(avail_s), "%s", mu_humansize(avail * block, block));
|
||||
|
||||
printf("%-20s %5s %5s %5s %7ju%% %s\n", src, total_s, used_s, avail_s, capacity, dst);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "hHka")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
h_flag = 1;
|
||||
break;
|
||||
|
||||
case 'H':
|
||||
h_flag = 1;
|
||||
block = 1000;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
a_flag = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("df\n\t[-h Human readable (1024)] [-a Show all]\n\t[-H Human readable (1000)]\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
FILE *fp = setmntent("/proc/mounts", "r");
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "df: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("[Filesystem] [Size] [Used] [Avail] [Capacity] [Mounted on]\n");
|
||||
|
||||
int ret = 0;
|
||||
struct mntent *me;
|
||||
struct statvfs disk;
|
||||
while ((me = getmntent(fp)) != NULL) {
|
||||
if (!strcmp(me->mnt_fsname, "none"))
|
||||
continue;
|
||||
|
||||
if (!a_flag)
|
||||
if (!strncmp(me->mnt_fsname, "fs", 2))
|
||||
continue;
|
||||
|
||||
if (statvfs(me->mnt_dir, &disk) < 0) {
|
||||
ret = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
off_t bs = disk.f_frsize / block;
|
||||
off_t total = disk.f_blocks * bs;
|
||||
off_t avail = disk.f_bfree * bs;
|
||||
off_t used = total - avail;
|
||||
off_t capacity = (used * 100) / (avail + used) + 1;
|
||||
|
||||
if (h_flag)
|
||||
print_human(me->mnt_fsname, total, used, avail, capacity, me->mnt_dir);
|
||||
|
||||
else
|
||||
printf("%-20s %7ju %7ju %7ju %7ju%% %s\n", me->mnt_fsname, total, used, avail, capacity, me->mnt_dir);
|
||||
}
|
||||
|
||||
endmntent(fp);
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -8,7 +8,6 @@
|
|||
#include "get_stat.h"
|
||||
#include "make_path.h"
|
||||
#include "human.h"
|
||||
#include "config.h"
|
||||
|
||||
unsigned int h_flag;
|
||||
unsigned int s_flag;
|
||||
|
@ -19,10 +18,10 @@ off_t total;
|
|||
|
||||
void print(off_t size, const char *filename) {
|
||||
if (h_flag)
|
||||
printf("%s\t%s%c", mu_humansize(size * BLK_SIZE), filename, n_flag);
|
||||
printf("%s\t%s%c", mu_humansize(size, 1024), filename, n_flag);
|
||||
|
||||
else
|
||||
printf("%jd\t%s%c", (intmax_t)size * BLK_SIZE / 1024, filename, n_flag);
|
||||
printf("%jd\t%s%c", (intmax_t)size / 1024, filename, n_flag);
|
||||
}
|
||||
|
||||
off_t du(const char *path, int recurs_flag) {
|
||||
|
@ -67,7 +66,7 @@ off_t du(const char *path, int recurs_flag) {
|
|||
|
||||
/* Get file size */
|
||||
else {
|
||||
sum = (512 * sb.st_blocks + BLK_SIZE - 1) / BLK_SIZE;
|
||||
sum = 512 * sb.st_blocks + 512;
|
||||
|
||||
if (c_flag)
|
||||
total += sum;
|
||||
|
|
|
@ -204,9 +204,9 @@ int print(const struct d_node *node) {
|
|||
ret += printf("%7ju ", (uintmax_t)node->stats.st_ino);
|
||||
|
||||
if (s_flag) {
|
||||
off_t size = (512 * node->stats.st_blocks + BLK_SIZE - 1) / BLK_SIZE;
|
||||
off_t size = 512 * node->stats.st_blocks + 512;
|
||||
if (h_flag)
|
||||
ret += printf("%7s ", mu_humansize(size * BLK_SIZE));
|
||||
ret += printf("%7s ", mu_humansize(size, 1024));
|
||||
|
||||
else
|
||||
ret += printf("%7ju ", (uintmax_t)size);
|
||||
|
@ -224,7 +224,7 @@ int print(const struct d_node *node) {
|
|||
char *gr_name = (gr != 0) ? gr->gr_name : "nobody";
|
||||
char *pw_name = (pw != 0) ? pw->pw_name : "nobody";
|
||||
if (h_flag)
|
||||
ret += printf(" %4ju %2s %2s %6s %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, mu_humansize(node->stats.st_size), get_date(node->stats.st_mtime));
|
||||
ret += printf(" %4ju %2s %2s %6s %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, mu_humansize(node->stats.st_size, 1024), get_date(node->stats.st_mtime));
|
||||
|
||||
else
|
||||
ret += printf(" %4ju %2s %2s %10jd %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, (uintmax_t)node->stats.st_size, get_date(node->stats.st_mtime));
|
||||
|
|
Loading…
Reference in New Issue