C: 从系统中获取挂载信息分区的通用函数

来源:互联网 发布:网络教育专升本 编辑:程序博客网 时间:2024/06/05 09:26
#include <errno.h>#include <stdio.h>#include <string.h>static int get_mounts_dev_dir(const char *arg, char **dev, char **dir){    FILE *f;    char mount_dev[256];    char mount_dir[256];    char mount_type[256];    char mount_opts[256];    int mount_freq;    int mount_passno;    int match;    f = fopen("/proc/mounts", "r");    if (!f) {        fprintf(stdout, "could not open /proc/mounts\n");        return -1;    }    do {        match = fscanf(f, "%255s %255s %255s %255s %d %d\n",                       mount_dev, mount_dir, mount_type,                       mount_opts, &mount_freq, &mount_passno);        mount_dev[255] = 0;        mount_dir[255] = 0;        mount_type[255] = 0;        mount_opts[255] = 0;        if (match == 6 &&            (strcmp(arg, mount_dev) == 0 ||             strcmp(arg, mount_dir) == 0)) {            *dev = strdup(mount_dev);            *dir = strdup(mount_dir);            fclose(f);            return 0;        }    } while (match != EOF);    fclose(f);    return -1;}
原创粉丝点击