Examples of getmntent setmntent endmntent usage in Linux

Source: Internet
Author: User

The mntent structure is defined in <mntent.h>, as follows:struct Mntent {
Char *mnt_fsname; /* Name of mounted file system */
Char *mnt_dir; /* File system path Prefix */
Char *mnt_type; /* mount type (see MNTENT.H) */
Char *mnt_opts; /* mount options (see MNTENT.H) */
int mnt_freq; /* Dump frequency in days */
int Mnt_passno; /* Pass number on parallel fsck */

};

The structure can correspond to the data of each row in/etc/mtab or/etc/fstab, if the program needs to access/etc/mtab or/etc/fstab files, then the function getmntent with Linux comes directly to the data of a row, Very convenient. Setmntent can create an FD based on the file and open type specified by the parameter, and the FD can pass in the Getmntent function to get a row of data into the mntent structure, as in the following example:

[CPP]View Plaincopy
  1. #include <mntent.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. int main ()
  6. {
  7. struct mntent *m;
  8. FILE *f = NULL;
  9. f = setmntent ("/etc/fstab","R"); //open file for describing the mounted filesystems
  10. if (!f)
  11. printf ("error:%s\n", Strerror (errno));
  12. While ((M = Getmntent (f))) //read next line
  13. printf ("drive%s, name%s,type%s,opt%s\n", M->mnt_dir, m->mnt_fsname,m->mnt_type,m->mnt_opts);
  14. Endmntent (f); //close file for describing the mounted filesystems
  15. return 0;
  16. }

It is important to note that getmntent is not reentrant function, if a program in a number of places at the same time call getmntent, may not get the desired results, then you can use the Getmntent_r function instead, the prototype is as follows:

struct Mntent *getmntent_r (FILE *fp, struct mntent *mntbuf, char *buf, int buflen);

Getmntent_r will store the data in the user-supplied memory (MNTBUF), rather than being managed by the system.

Addmntent (file *fp, const struct mntent *mnt) can append the last row of data to the file that the FP points to.

Examples of getmntent setmntent endmntent usage in Linux

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.