Fix memory handling in strxfrm_l [BZ #16009]
[glibc.git] / misc / tst-mntent.c
blob876c89f8ed313de9c9db97fae0831f22fcc3f40d
1 /* Test case by Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
2 and Ulrich Drepper <drepper@cygnus.com>. */
3 #include <mntent.h>
4 #include <stdio.h>
5 #include <string.h>
8 int
9 main (int argc, char *argv[])
11 int result = 0;
12 struct mntent mef;
13 struct mntent *mnt = &mef;
14 FILE *fp;
16 mef.mnt_fsname = strdupa ("/dev/hda1");
17 mef.mnt_dir = strdupa ("/some dir");
18 mef.mnt_type = strdupa ("ext2");
19 mef.mnt_opts = strdupa ("defaults");
20 mef.mnt_freq = 1;
21 mef.mnt_passno = 2;
23 if (hasmntopt (mnt, "defaults"))
24 printf ("Found!\n");
25 else
27 printf ("Didn't find it\n");
28 result = 1;
31 fp = tmpfile ();
32 if (fp == NULL)
34 printf ("Cannot open temporary file: %m\n");
35 result = 1;
37 else
39 char buf[1024];
41 /* Write the name entry. */
42 addmntent (fp, &mef);
44 /* Prepare for reading. */
45 rewind (fp);
47 /* First, read it raw. */
48 if (fgets (buf, sizeof (buf), fp) == NULL)
50 printf ("Cannot read temporary file: %m");
51 result = 1;
53 else
54 if (strcmp (buf, "/dev/hda1 /some\\040dir ext2 defaults 1 2\n") != 0)
56 puts ("Raw file data not correct");
57 result = 1;
60 /* Prepare for reading, part II. */
61 rewind (fp);
63 /* Now read it cooked. */
64 mnt = getmntent (fp);
66 if (strcmp (mnt->mnt_fsname, "/dev/hda1") != 0
67 || strcmp (mnt->mnt_dir, "/some dir") != 0
68 || strcmp (mnt->mnt_type, "ext2") != 0
69 || strcmp (mnt->mnt_opts, "defaults") != 0
70 || mnt->mnt_freq != 1
71 || mnt->mnt_passno != 2)
73 puts ("Error while reading written entry back in");
74 result = 1;
77 /* Part III: Entry with whitespaces at the end of a line. */
78 rewind (fp);
80 fputs ("/foo\\040dir /bar\\040dir auto bind \t \n", fp);
82 rewind (fp);
84 mnt = getmntent (fp);
86 if (strcmp (mnt->mnt_fsname, "/foo dir") != 0
87 || strcmp (mnt->mnt_dir, "/bar dir") != 0
88 || strcmp (mnt->mnt_type, "auto") != 0
89 || strcmp (mnt->mnt_opts, "bind") != 0
90 || mnt->mnt_freq != 0
91 || mnt->mnt_passno != 0)
93 puts ("Error while reading entry with trailing whitespaces");
94 result = 1;
98 return result;