zoukankan      html  css  js  c++  java
  • apue编程之参考df代码写的一个简单的df命令的源代码

    代码:

    #include <stdio.h>
    #include <mntent.h>
    #include <string.h>
    #include <sys/vfs.h>
    static const unsigned long long G = 1024*1024*1024ull;
    static const unsigned long long M = 1024*1024;
    static const unsigned long long K = 1024;
    static char str[20];
    char* kscale(unsigned long b, unsigned long bs)
    {
        unsigned long long size = b * (unsigned long long)bs;
        if (size > G)
        {
            sprintf(str, "%0.2f GB", size/(G*1.0));
            return str;
        }
        else if (size > M)
        {
            sprintf(str, "%0.2f MB", size/(1.0*M));
            return str;
        }
        else if (size > K)
        {
            sprintf(str, "%0.2f K", size/(1.0*K));
            return str;
        }
        else
        {
            sprintf(str, "%0.2f B", size*1.0);
            return str;
        }
    }
    int main(int argc, char *argv[])
    {
        FILE* mount_table;
        struct mntent *mount_entry;
        struct statfs s;
        unsigned long blocks_used;
        unsigned blocks_percent_used;
        const char *disp_units_hdr = NULL;
        mount_table = NULL;
        mount_table = setmntent("/etc/mtab", "r");
        if (!mount_table)
        {
            fprintf(stderr, "set mount entry error
    ");
            return -1;
        }
        disp_units_hdr = "     Size";
        printf("Filesystem           %-15sUsed Available %s Mounted on
    ",
                disp_units_hdr, "Use%");
        while (1) {
            const char *device;
            const char *mount_point;
            if (mount_table) {
                mount_entry = getmntent(mount_table);
                if (!mount_entry) {
                    endmntent(mount_table);
                    break;
                }
            }
            else
                continue;
            device = mount_entry->mnt_fsname;
            mount_point = mount_entry->mnt_dir;
            //fprintf(stderr, "mount info: device=%s mountpoint=%s
    ", device, mount_point);
            if (statfs(mount_point, &s) != 0)
            {
                fprintf(stderr, "statfs failed!
    ");   
                continue;
            }
            if ((s.f_blocks > 0) || !mount_table )
            {
                blocks_used = s.f_blocks - s.f_bfree;
                blocks_percent_used = 0;
                if (blocks_used + s.f_bavail)
                {
                    blocks_percent_used = (blocks_used * 100ULL
                            + (blocks_used + s.f_bavail)/2
                            ) / (blocks_used + s.f_bavail);
                }
                /* GNU coreutils 6.10 skips certain mounts, try to be compatible.  */
                if (strcmp(device, "rootfs") == 0)
                    continue;
                if (printf("
    %-20s" + 1, device) > 20)
                        printf("
    %-20s", "");
                char s1[20];
                char s2[20];
                char s3[20];
                strcpy(s1, kscale(s.f_blocks, s.f_bsize));
                strcpy(s2, kscale(s.f_blocks - s.f_bfree, s.f_bsize));
                strcpy(s3, kscale(s.f_bavail, s.f_bsize));
                printf(" %9s %9s %9s %3u%% %s
    ",
                        s1,
                        s2,
                        s3,
                        blocks_percent_used, mount_point);
            }
        }
        return 0;
    }

    运行效果如下:

    编译:g++ -g -Wall main.cpp
     
    可以生成一个a.out的文件,
     
    下面是运行a.out和df -h的输出对比:
     
    :/tmp/tmp$ df -h
    文件系统            容量  已用 可用 已用% 挂载点
    /dev/sda7             9.4G  6.5G  2.5G  73% /
    none                  1.6G  300K  1.6G   1% /dev
    none                  1.6G  212K  1.6G   1% /dev/shm
    none                  1.6G  296K  1.6G   1% /var/run
    none                  1.6G     0  1.6G   0% /var/lock
    none                  1.6G     0  1.6G   0% /lib/init/rw
    /dev/sda6             113G   87G   26G  77% /media/work_
    /dev/sda9              26G   23G  2.0G  92% /home
     
    /tmp/tmp$ ./a.out
    Filesystem                Size      Used Available Use% Mounted on
    /dev/sda7              9.39 GB   6.45 GB   2.46 GB  72% /
    none                   1.59 GB  300.00 K   1.59 GB   0% /dev
    none                   1.60 GB 1020.00 K   1.59 GB   0% /dev/shm
    none                   1.60 GB  296.00 K   1.59 GB   0% /var/run
    none                   1.60 GB    0.00 B   1.60 GB   0% /var/lock
    none                   1.60 GB    0.00 B   1.60 GB   0% /lib/init/rw
    /dev/sda6            112.62 GB  86.67 GB  25.95 GB  77% /media/work_
    /dev/sda9             25.38 GB  22.10 GB   1.99 GB  92% /home

    专栏:

    微信公众号 - MySQL数据库技术:

    知乎 - 数据库技术 专栏:
    https://zhuanlan.zhihu.com/mysqldb


    博客:

    思否/segmentfault 专栏:
    https://segmentfault.com/u/dbtech

    开源中国/oschina:
    https://my.oschina.net/dbtech

    掘金:
    https://juejin.im/user/5e9d3ed251882538083fed1f/posts

    博客园/cnblogs:
    https://www.cnblogs.com/dbtech 
  • 相关阅读:
    java数组
    python中的换行与不换行
    金融业务知识2---资金的时间价值与跨时期优化
    金融业务知识1---金融和金融系统
    你真的理解正向代理和反向代理吗?
    webpack(9)plugin插件功能的使用
    webpack(8)vue组件化开发的演变过程
    webpack(7)webpack使用vue配置
    webpack(6)webpack处理图片
    webpack(5)webpack处理css文件
  • 原文地址:https://www.cnblogs.com/dbtech/p/5552237.html
Copyright © 2011-2022 走看看