Back to index

glibc  2.9
tst-mntent.c
Go to the documentation of this file.
00001 /* Test case by Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
00002    and Ulrich Drepper <drepper@cygnus.com>.  */
00003 #include <mntent.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 
00008 int
00009 main (int argc, char *argv[])
00010 {
00011   int result = 0;
00012   struct mntent mef;
00013   struct mntent *mnt = &mef;
00014   FILE *fp;
00015 
00016   mef.mnt_fsname = strdupa ("/dev/hda1");
00017   mef.mnt_dir = strdupa ("/some dir");
00018   mef.mnt_type = strdupa ("ext2");
00019   mef.mnt_opts = strdupa ("defaults");
00020   mef.mnt_freq = 1;
00021   mef.mnt_passno = 2;
00022 
00023   if (hasmntopt (mnt, "defaults"))
00024     printf ("Found!\n");
00025   else
00026     {
00027       printf ("Didn't find it\n");
00028       result = 1;
00029     }
00030 
00031   fp = tmpfile ();
00032   if (fp == NULL)
00033     {
00034       printf ("Cannot open temporary file: %m\n");
00035       result = 1;
00036     }
00037   else
00038     {
00039       char buf[1024];
00040 
00041       /* Write the name entry.  */
00042       addmntent (fp, &mef);
00043 
00044       /* Prepare for reading.  */
00045       rewind (fp);
00046 
00047       /* First, read it raw.  */
00048       if (fgets (buf, sizeof (buf), fp) == NULL)
00049        {
00050          printf ("Cannot read temporary file: %m");
00051          result = 1;
00052        }
00053       else
00054        if (strcmp (buf, "/dev/hda1 /some\\040dir ext2 defaults 1 2\n") != 0)
00055          {
00056            puts ("Raw file data not correct");
00057            result = 1;
00058          }
00059 
00060       /* Prepare for reading, part II.  */
00061       rewind (fp);
00062 
00063       /* Now read it cooked.  */
00064       mnt = getmntent (fp);
00065 
00066       if (strcmp (mnt->mnt_fsname, "/dev/hda1") != 0
00067          || strcmp (mnt->mnt_dir, "/some dir") != 0
00068          || strcmp (mnt->mnt_type, "ext2") != 0
00069          || strcmp (mnt->mnt_opts, "defaults") != 0
00070          || mnt->mnt_freq != 1
00071          || mnt->mnt_passno != 2)
00072        {
00073          puts ("Error while reading written entry back in");
00074          result = 1;
00075        }
00076     }
00077 
00078   return result;
00079 }