Linux shell可通过查看/etc/mtab或者/proc/mounts文件来获取当前文件系统挂载信息,示例:

程序内读取/etc/mtab或者/proc/mounts,解析字符串较为繁琐,可以使用mntent提供的方便函数:

FILE *setmntent(const char *filename, const char *type);
struct mntent *getmntent(FILE *filep);
int endmntent(FILE *filep);
 
(1)setmntent用来打开/etc/mtab或者同样格式的table文件
        参数 filename为table文件的路径(例如/etc/mtab),参数type为打开文件的模式(与open类型,例如“r”为只读打开)
        成功时,返回FILE指针(用于mntent操作),失败时返回NULL
 
(2)getmntent用来读取文件的每一行,解析每一行的参数到mntent结构,mntent结构的存储空间是静态分配的(不需要free),结构的值会在下一次getmntent时被覆盖
         mntent结构定义: 
struct mntent
{
char *mnt_fsname; /* 文件系统对应的设备路径或者服务器地址 */
char *mnt_dir; /* 文件系统挂载到的系统路径 */
char *mnt_type; /* 文件系统类型: ufs, nfs, 等 */
char *mnt_opts; /* 文件系统挂载参数,以逗号分隔 */
int mnt_freq; /* 文件系统备份频率(以天为单位) */
int mnt_passno; /* 开机fsck的顺序,如果为0,不会进行check */
};
  参数filep是setmntent返回的FILE指针
  成功时返回指向mntent的指针,错误时返回NULL
 
(3)endmntent用来关闭打开的table文件,总是返回1
 

示例程序:

#include <stdio.h>
#include <mntent.h>
#include <errno.h>
#include <string.h> int main(void)
{
char *filename = "/proc/mounts";
FILE *mntfile;
struct mntent *mntent; mntfile = setmntent(filename, "r");
if (!mntfile) {
printf("Failed to read mtab file, error [%s]\n",
strerror(errno));
return -;
} while(mntent = getmntent(mntfile))
printf("%s, %s, %s, %s\n",
mntent->mnt_dir,
mntent->mnt_fsname,
mntent->mnt_type,
mntent->mnt_opts); endmntent(mntfile);
return ;
}

【Linux】程序内获取文件系统挂载信息的更多相关文章

  1. python装饰器内获取函数有用信息方法

    装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...

  2. linux驱动之获取设备树信息

    上一篇文章学习了字符设备的注册,操作过的小伙伴都知道上一篇文章中测试驱动时是通过手动创建设备节点的,现在开始学习怎么自动挂载设备节点和设备树信息的获取,这篇文章中的源码将会是我以后编写字符驱动的模板. ...

  3. Linux:Day8(上) 文件系统挂载和管理工具

    文件系统管理: 将额外文件系统与根文件系统某现存的目录建立起关联关系,进而使得此目录做为其它文件访问入口的行为称之为挂载: 解除此关联关系的过程称之为卸载: 把设备关联挂载点:Mount Point ...

  4. 微信小程序授权获取用户详细信息openid

    小程序获取用户的头像昵称openid之类 第一种使用wx.getUserInfo直接获取微信头像,昵称 wx.getUserInfo({ success: function (res) { that. ...

  5. 在Linux系统上获取命令帮助信息和划分man文档

    使用历史命令history 打完以后前面会有顺序号的比如1 cd2 ls3 pwd如果需要重新执行cd命令则可以执行 !3 命令 命令补全功能 比如你要执行history命令 可以打上histo+键 ...

  6. Linux C 语言 获取系统时间信息

    比如获取当前年份:        /* 获取当前系统时间 暂时不使用        int iyear = 0;        int sysyear = 0;        time_t now;  ...

  7. 微信小程序open-data获取用户的信息样式设置

    效果图 wxml代码 <view class="userinfo"> <!-- 用户头像 --> <view class="userinfo ...

  8. 【微信小程序】获取用户地理位置权限,二次请求授权,逆解析获取地址

    摘要:微信小程序内获取用户地理位置信息授权,被拒绝后二次获取,获取权限后逆解析得到用户所在省市区等.. 场景:商城类小程序,在首页时需展示附近门店,即用户刚进入小程序时就需要获取到用户位置信息 ste ...

  9. Linux 系统磁盘挂载信息文件

    设置文件系统挂载信息的文件(etc/fstab),使得开机能够自动挂载磁盘分区 文件系统挂载 方法一: 直接挂在,临时生效 # 格式化系统(没有格式化就没有文件系统,放不了数据) dd if=/dev ...

随机推荐

  1. Yii2.0 多文件上传

    --------------------------------------------------------------------------------------------------- ...

  2. Jmeter Html 报告优化

    转载自南风_real博客园:http://www.cnblogs.com/jaychang/p/5881525.html 但是最近在查阅相关资料时,发现基本都是重复一篇文章Jmeter使用笔记之htm ...

  3. Citrix 服务器虚拟化之八 Xenserver虚拟机模版

    Citrix 服务器虚拟化之八 Xenserver虚拟机模版 XenServer与VMware不同,Vmware只能将现有的VM转换成模版,而XenServer具有两种方法:一种是将现有 VM 转换为 ...

  4. linux下安装nginx、pcre、zlib、openssl

    1.安装nginx之前需要安装PCRE库的安装  最新下载地址   ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ tar –zxvf p ...

  5. gc 日志格式

    对于 Parallel Scavenge + Parallel Old - 新生代:PSYoungGen - 老年代:ParOldGen - 永久代:PSPermGen 后面方括号内部的 " ...

  6. Hibernate注解:一对多外键关联

    情形:两个表,cms_mode是主表,cms_model_field是子表,cms_model_field的model_id字段关联到cms_model的主键. # # Source for tabl ...

  7. 2015 Multi-University Training Contest 6 Cake

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m soda and today i ...

  8. OC基础(2)

    类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 *:first-child { margin-top: 0 !important; } body > *:last-c ...

  9. UIApplication及UIWindow

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  10. mysql命令整理0919 不定期更新中

    1)新建数据库 create database +database_name:         查询数据库  show databases;       切换数据库   use database_na ...