Get file system mount information in "Linux" program

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.