Skip to content

Instantly share code, notes, and snippets.

@lufia
Last active December 11, 2020 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lufia/05ae814d4c87c91d06fd762347d16e1c to your computer and use it in GitHub Desktop.
Save lufia/05ae814d4c87c91d06fd762347d16e1c to your computer and use it in GitHub Desktop.
dfが何を使って出力しているのか調べたコード
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/mount.h>
// /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdint.h
char *argv0;
void fstats(void);
void fstat(char *);
void printstat(struct statfs *);
uintmax_t bused(struct statfs *);
uintmax_t bsize(struct statfs *, uintmax_t);
int
main(int argc, char **argv)
{
int i;
argv0 = argv[0];
if(argc == 1)
fstats();
else
for(i = 1; i < argc; i++)
fstat(argv[i]);
return 0;
}
void
fstats(void)
{
struct statfs *f, *p, *e;
int n;
n = getmntinfo(&f, MNT_NOWAIT);
if(n < 0){
fprintf(stderr, "%s: getmntinfo: %s\n", argv0, strerror(errno));
return;
}
e = f+n;
for(p = f; p < e; p++){
if(p > f)
printf("\n");
printstat(p);
}
free(f);
}
void
fstat(char *path)
{
struct statfs fs;
if(statfs(path, &fs) < 0){
fprintf(stderr, "%s: statfs: %s\n", argv0, strerror(errno));
exit(1);
}
printstat(&fs);
}
void
printstat(struct statfs *f)
{
printf("mounted on %s\n", f->f_mntonname);
printf("fstype %s\n", f->f_fstypename);
printf("block size %u\n", f->f_iosize);
printf("fragment size %u\n", f->f_bsize);
printf("fragments in a block %u\n", f->f_iosize/f->f_bsize);
printf("used blocks %" PRIuMAX "\n", bsize(f, bused(f)));
printf("avail blocks %" PRIuMAX "\n", bsize(f, f->f_bavail));
printf("total blocks %" PRIuMAX "\n", bsize(f, f->f_blocks));
printf("used inodes %" PRIu64 "\n", f->f_files - f->f_ffree);
printf("free inodes %" PRIu64 "\n", f->f_ffree);
printf("flags");
if(f->f_flags&MNT_RDONLY)
printf(" readonly");
printf("\n");
}
uintmax_t
bused(struct statfs *f)
{
return f->f_blocks - f->f_bfree;
}
uintmax_t
bsize(struct statfs *f, uintmax_t n)
{
return n * f->f_bsize / 512;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <mntent.h>
#include <sys/statvfs.h>
char *argv0;
void fstats(void);
void fstat(char *);
uintmax_t bused(struct statvfs *);
uintmax_t bsize(struct statvfs *, uintmax_t);
int
main(int argc, char **argv)
{
int i;
argv0 = argv[0];
if(argc == 1)
fstats();
else
for(i = 1; i < argc; i++)
fstat(argv[i]);
return 0;
}
void
fstats(void)
{
FILE *r;
struct mntent *p;
r = setmntent("/etc/mtab", "r");
while(p = getmntent(r))
fstat(p->mnt_dir);
endmntent(r);
}
void
fstat(char *path)
{
struct statvfs fs;
if(statvfs(path, &fs) < 0){
fprintf(stderr, "%s: statvfs: %s\n", argv0, strerror(errno));
exit(1);
}
printf("path %s\n", path);
printf("block size %lu\n", fs.f_bsize);
printf("fragment size %lu\n", fs.f_frsize);
printf("fragments in a block %lu\n", fs.f_bsize/fs.f_frsize);
printf("used blocks %" PRIuMAX "\n", bsize(&fs, bused(&fs)));
printf("avail blocks %" PRIuMAX "\n", bsize(&fs, fs.f_bavail));
printf("total blocks %" PRIuMAX "\n", bsize(&fs, fs.f_blocks));
printf("used inodes %u\n", fs.f_files - fs.f_ffree);
printf("free inodes %u\n", fs.f_ffree);
printf("flags");
if(fs.f_flag&ST_RDONLY)
printf(" readonly");
printf("\n");
printf("namelen %lu\n", fs.f_namemax);
printf("\n");
}
uintmax_t
bused(struct statvfs *f)
{
return f->f_blocks - f->f_bfree;
}
uintmax_t
bsize(struct statvfs *f, uintmax_t n)
{
return n * f->f_frsize / 1024;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment