commit fb0aebf941512d784753f8982cb1bf712980c0c1 Author: Steven Dee Date: Mon Jul 22 10:52:19 2013 -0400 Add df diff --git a/Makefile b/Makefile index da4d0d0..511b89b 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ SRC = \ comm.c \ cp.c \ date.c \ + df.c \ dirname.c \ echo.c \ env.c \ diff --git a/df.c b/df.c new file mode 100644 index 0000000..4525aaa --- /dev/null +++ b/df.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include "util.h" + +static void mnt_show(struct statfs *s); + +int +main(void) +{ + struct statfs *mntbuf; + int len, i; + + len = getmntinfo(&mntbuf, MNT_WAIT); + if(len == 0) { + eprintf("getmntinfo:"); + } + + printf("Filesystem 512-blocks Used Avail Capacity Mounted on\n"); + for (i = 0; i < len; i++) { + mnt_show(&mntbuf[i]); + } + return 0; +} + +static void +mnt_show(struct statfs *s) +{ + u_int64_t total, used, avail; + int capacity; + int bs; + + bs = s->f_bsize / 512; + + total = s->f_blocks * bs; + avail = s->f_bfree * bs; + used = total - avail; + capacity = (used * 100) / total; + + printf("%-12s %9llu %9llu %9llu %5d%% %s\n", + s->f_mntfromname, total, used, avail, capacity, s->f_mntonname); +} diff --git a/util.h b/util.h index 620390a..035b061 100644 --- a/util.h +++ b/util.h @@ -4,6 +4,8 @@ #define UTF8_POINT(c) (((c) & 0xc0) != 0x80) +#undef MIN +#undef MAX #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y))