2022年5月15日 星期日

[C/C++] 顯示目前mount的狀態

說明:
顯示目前mount的狀態

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <mntent.h>

#define BOOL                    int
#define TRUE                    0
#define FALSE                   -1
#define MAX_LONG_LENGTH         1024
#define MAX_SHORT_LENGTH        128

typedef struct mountInfo
{
    char  dev_name[MAX_SHORT_LENGTH];
    char  mount_path[MAX_SHORT_LENGTH];
    char  fs_type[MAX_SHORT_LENGTH];
    char  attribute[MAX_SHORT_LENGTH];
    struct mountInfo *next;
} mountInfoS;

BOOL load_file_fun(char *filename, char **p_content)
{
    int fd = -1;    
    int totalsize = 0;
    int readsize = 0;
    char buffer[MAX_SHORT_LENGTH];
    BOOL ret = FALSE;
    
    if ( (fd = open((char *)filename, O_RDONLY)) > 0 )
    {
        totalsize = 0;
        while( (readsize = read(fd, buffer, MAX_SHORT_LENGTH)) > 0)
            totalsize += readsize;

        if (totalsize <= 0)
        {
            printf("[ERROR] %s file size is wrong!\n", filename);
            return ret;
        }    
        
        if (lseek(fd, 0, SEEK_SET) == -1)
        {
            printf("[ERROR] %s can't seek!\n", filename);
            return ret;
        }
        
        *p_content = (char*)malloc( ( totalsize + 1) * sizeof(char));
        memset(*p_content, '\0', totalsize + 1);

        if (*p_content)
        {
            if(read(fd, *p_content, totalsize) > 0 )
            {
                ret = TRUE;
            }
            else
            {
                printf("[ERROR] %s can't read!\n", filename);
                free(*p_content);
            }

            close(fd);
        }
    }
    else
        printf("[ERROR] Open %s fail!\n", filename);

    return ret;    
}

void get_mount_info_fun(mountInfoS **p_output)
{
    char *p_content = NULL;
    char *p_tmp = NULL;
    char *p_feed = NULL;
    char *p_line = NULL;
    int len = 0;
    int loop = 0;
    char *sep = "\t ";
    char * pch;
    mountInfoS *p_new, *p_first, *p_cur;

    p_new = p_first = p_cur = NULL;
    
    if (load_file_fun("/proc/mounts", &p_content) == TRUE)
    {
        p_tmp = p_content;

        while((p_feed = strpbrk(p_tmp, "\n")))
        {
            len = p_feed - p_tmp;
            p_line = (char*)malloc(len + 1);
            memset(p_line, '\0', len + 1);
            strncpy(p_line, p_tmp, len);
            
            pch = strtok(p_line, sep);

            if (pch)
            {
                p_new = malloc(sizeof(mountInfoS));
                memset(p_new, '\0', sizeof(mountInfoS));
                loop = 0;
                strcpy( p_new->dev_name, pch);
                
                while (pch != NULL)
                {
                    pch = strtok (NULL, sep);
                    loop++;
                    
                    if (loop == 1)                    
                        strcpy( p_new->mount_path, pch);
                    else if (loop == 2)                    
                        strcpy( p_new->fs_type, pch);    
                    else if (loop == 3)                    
                        strcpy( p_new->attribute, pch);                            
                }   

                if ( p_first == NULL )
                {
                    p_first = p_cur = p_new;
                }
                else
                {
                    p_cur->next = p_new;
                    p_cur = p_new;
                }                
            }            

            free(p_line);
            p_tmp = p_feed + 1;
        }
    }

    if (p_content)
        free(p_content);
        
    *p_output = p_first;    
}

void free_mount_info_fun(mountInfoS *p_input)
{
    mountInfoS *p_cur = p_input;
    mountInfoS *p_tmp;   
    
    p_tmp = p_cur;
    while( p_cur )
    {
        p_tmp = p_cur;
        p_cur = p_cur->next;
        free( p_tmp );
    }
}

int main(int argc, char *argv[])
{
    mountInfoS *p_info = NULL;
    mountInfoS *p_tmp = NULL;
    
    get_mount_info_fun(&p_info);
    
    if(p_info)
    {
        p_tmp = p_info;
        while(p_tmp)
        {
            printf("dev_name : %s  ", p_tmp->dev_name);
            printf("mount_path : %s  ", p_tmp->mount_path);
            printf("fs_type : %s  ", p_tmp->fs_type);
            printf("attribute : %s\n", p_tmp->attribute);            
            p_tmp = p_tmp->next;
        }
        
        free_mount_info_fun(p_info);
    }
    
    return 0;
}

Result:
dev_name : sysfs  mount_path : /sys  fs_type : sysfs  attribute : rw,nosuid,nodev,noexec,relatime
dev_name : proc  mount_path : /proc  fs_type : proc  attribute : rw,nosuid,nodev,noexec,relatime
dev_name : udev  mount_path : /dev  fs_type : devtmpfs  attribute : rw,nosuid,noexec,relatime,size=16359368k,nr_inodes=4089842,mode=755,inode64
dev_name : devpts  mount_path : /dev/pts  fs_type : devpts  attribute : rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
dev_name : tmpfs  mount_path : /run  fs_type : tmpfs  attribute : rw,nosuid,nodev,noexec,relatime,size=3278556k,mode=755,inode64
......

沒有留言:

張貼留言