Easy df command on Linux

Just create the df command using statfs (). You can also use statvfs (). Do not use setmntent () and endmntent () because the contents are fopen () / fclose () But / etc / mtab Can I see it directly?

What is the essence of eliminating high-performance parts such as the human-readable option and the exclude option?

df.c


#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <unistd.h>

typedef struct _df_t {
   struct mntent* mnt;
   struct statfs* fs;
} DfData;

int get_mount_record(FILE* mtab, DfData* df)
{
  df->mnt = getmntent(mtab);
  if (df->mnt) {
    if (df->mnt->mnt_dir == NULL) return 0;
    if (statfs(df->mnt->mnt_dir, df->fs) != 0) return 0;
    return 1;
  }
  return 0;
}

#define DF_KILO_RECORD_FMT  "%-25s %10lu %10lu %10lu %4s %-10s\n"

void print_kilobyte(DfData* df) {
  size_t total_size = df->fs->f_blocks * df->fs->f_bsize / 1024;
  size_t avail_size = df->fs->f_bavail * df->fs->f_bsize / 1024;
  size_t used_size = total_size - avail_size;
  char percent[4] = {0};
  if (df->fs->f_blocks == 0)
     snprintf(percent, sizeof(percent), "-");
  else
     snprintf(percent, sizeof(percent), "%lu%%", (100 * used_size / total_size));

  printf(DF_KILO_RECORD_FMT, df->mnt->mnt_fsname, total_size, used_size, avail_size, percent, df->mnt->mnt_dir);
  return;
}

void show_kilobyte(FILE* mtab) {
  DfData* df = (DfData*)malloc(sizeof(DfData));
  df->fs = (struct statfs *)malloc(sizeof(struct statfs));

  while(get_mount_record(mtab, df))
       print_kilobyte(df);

  free(df->fs);
  free(df);
  return;
}

int main(void)
{
  FILE* mtab = setmntent(_PATH_MOUNTED, "r");
  show_kilobyte(mtab);
  endmntent(mtab);
  exit(0);
}

Execution result.

$ gcc -Wall -Wextra -O2 df.o -o df
$ ./df
rootfs                      30909700   19431160   11478540  62% /
sysfs                              0          0          0    - /sys
proc                               0          0          0    - /proc
devtmpfs                     2441664          0    2441664   0% /dev
securityfs                         0          0          0    - /sys/kernel/security
tmpfs                        2325352          0    2325352   0% /dev/shm
devpts                             0          0          0    - /dev/pts
tmpfs                        2325352       8676    2316676   0% /run
tmpfs                        2325352          0    2325352   0% /sys/fs/cgroup
cgroup                             0          0          0    - /sys/fs/cgroup/systemd
pstore                             0          0          0    - /sys/fs/pstore
cgroup                             0          0          0    - /sys/fs/cgroup/cpuset
cgroup                             0          0          0    - /sys/fs/cgroup/pids
cgroup                             0          0          0    - /sys/fs/cgroup/net_cls,net_prio
cgroup                             0          0          0    - /sys/fs/cgroup/blkio
cgroup                             0          0          0    - /sys/fs/cgroup/perf_event
cgroup                             0          0          0    - /sys/fs/cgroup/devices
cgroup                             0          0          0    - /sys/fs/cgroup/freezer
cgroup                             0          0          0    - /sys/fs/cgroup/hugetlb
cgroup                             0          0          0    - /sys/fs/cgroup/cpu,cpuacct
cgroup                             0          0          0    - /sys/fs/cgroup/memory
configfs                           0          0          0    - /sys/kernel/config
/dev/mapper/centos-root     30909700   19431160   11478540  62% /
systemd-1                          0          0          0    - /proc/sys/fs/binfmt_misc
mqueue                             0          0          0    - /dev/mqueue
hugetlbfs                          0          0          0    - /dev/hugepages
debugfs                            0          0          0    - /sys/kernel/debug
binfmt_misc                        0          0          0    - /proc/sys/fs/binfmt_misc
nfsd                               0          0          0    - /proc/fs/nfsd
/dev/xvda1                    508588     296336     212252  58% /boot
sunrpc                             0          0          0    - /var/lib/nfs/rpc_pipefs
none                               0          0          0    - /proc/xen
tmpfs                         465072          0     465072   0% /run/user/1000

Recommended Posts

Easy df command on Linux
Linux command # 4
Linux command # 3
Linux command # 5
Completion of docker command on Linux
Linux command list
[Linux] Search command
Linux command <Basic 2>
Linux [directory command]
Status check command used (sometimes) on linux
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
Linux [shell command]
[Linux] Command / Knowledge
My linux command
Linux command <Basic 1>
Linux command collection
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
Easy way to load CPU / memory on Linux
Try to create a new command on linux
Daemonizing processes on Linux
[Linux] Volume configuration command
Linux command cheat sheet
jblas on Arch Linux
Linux command (sequential update)
Linux (WSL) on Windows
NAT router on Linux
Linux basic command memorandum
Linux command [File operation]
Develop .NET on Linux
Wake on lan on Linux
[Linux] Basic command summary
Linux command for self-collection
linux command error collection 1
Monitor traffic on Linux
Update vscode on linux
Try NeosVR on Linux
Linux command line shortcut
Check capacity on Linux
LiveUSB creation on Linux
Linux operation on Win10
Multiply PDF by OCR on command line on Linux (Ubuntu)
NTP configuration memo on Linux
Install Minecraft on Arch Linux
[Linux] tar.gz compression / decompression command
Install Linux on your Chromebox
Downgrade Mcomix on Arch Linux
What is Linux? [Command list]
Search for large files on Linux from the command line
Use Github Desktop on Linux
Install the JDK on Linux
Elixir = Comfortable on Linux Mint
Recording and playback on Linux
Check TTL on Linux router
Read core voltage on Linux