The Linux shell can get the current file system mount information by looking at the/etc/mtab or/proc/mounts file, for example:
Read/etc/mtab or/proc/mounts within the program, parsing strings is cumbersome, you can use the convenient functions provided by mntent:
FILE *setmntent (constcharconstchar *type); struct mntent *getmntent (FILE *filep); int endmntent (FILE *filep);
(1) Setmntent used to open/etc/mtab or the same format table file parameter filename is the path to the table file (for example,/etc/mtab), the parameter type is the mode of the open file (with open type, for example "R" is read-only) On success, returns the file pointer (for the mntent operation), returns Null (2) On failure, getmntent is used to read each line of the file, parses each row's parameters into the mntent structure, and the storage space for the mntent structure is statically allocated (no need for free). The value of the structure is overwritten at the next getmntent mntent structure definition:
structmntent {Char*mnt_fsname;/*file system corresponding device path or server address*/ Char*mnt_dir;/*the system path to which the file system is mounted*/ Char*mnt_type;/*file System types: UFS, NFS, etc.*/ Char*mnt_opts;/*file System mount parameters, separated by commas*/ intMnt_freq;/*File system backup frequency (in days)*/ intMnt_passno;/*The order of power on fsck, if 0, does not check*/ };
The parameter Filep is setmntent returns a pointer to mntent when the file pointer is returned successfully, returns Null (3) When the error is returned, Endmntent is used to close the Open table file and always returns 1
Sample program:
#include <stdio.h>#include<mntent.h>#include<errno.h>#include<string.h>intMainvoid){ Char*filename ="/proc/mounts"; FILE*Mntfile; structMntent *mntent; Mntfile= setmntent (filename,"R"); if(!mntfile) {printf ("Failed to read mTAB file, error [%s]\n", Strerror (errno)); return-1; } 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 0;}
View Code
Get file system mount information in "Linux" program