lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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> |
| 6 | |
| 7 | |
| 8 | int |
| 9 | main (int argc, char *argv[]) |
| 10 | { |
| 11 | int result = 0; |
| 12 | struct mntent mef; |
| 13 | struct mntent *mnt = &mef; |
| 14 | FILE *fp; |
| 15 | |
| 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; |
| 22 | |
| 23 | if (hasmntopt (mnt, "defaults")) |
| 24 | printf ("Found!\n"); |
| 25 | else |
| 26 | { |
| 27 | printf ("Didn't find it\n"); |
| 28 | result = 1; |
| 29 | } |
| 30 | |
| 31 | fp = tmpfile (); |
| 32 | if (fp == NULL) |
| 33 | { |
| 34 | printf ("Cannot open temporary file: %m\n"); |
| 35 | result = 1; |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | char buf[1024]; |
| 40 | |
| 41 | /* Write the name entry. */ |
| 42 | addmntent (fp, &mef); |
| 43 | |
| 44 | /* Prepare for reading. */ |
| 45 | rewind (fp); |
| 46 | |
| 47 | /* First, read it raw. */ |
| 48 | if (fgets (buf, sizeof (buf), fp) == NULL) |
| 49 | { |
| 50 | printf ("Cannot read temporary file: %m"); |
| 51 | result = 1; |
| 52 | } |
| 53 | else |
| 54 | if (strcmp (buf, "/dev/hda1 /some\\040dir ext2 defaults 1 2\n") != 0) |
| 55 | { |
| 56 | puts ("Raw file data not correct"); |
| 57 | result = 1; |
| 58 | } |
| 59 | |
| 60 | /* Prepare for reading, part II. */ |
| 61 | rewind (fp); |
| 62 | |
| 63 | /* Now read it cooked. */ |
| 64 | mnt = getmntent (fp); |
| 65 | |
| 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) |
| 72 | { |
| 73 | puts ("Error while reading written entry back in"); |
| 74 | result = 1; |
| 75 | } |
| 76 | |
| 77 | /* Part III: Entry with whitespaces at the end of a line. */ |
| 78 | rewind (fp); |
| 79 | |
| 80 | fputs ("/foo\\040dir /bar\\040dir auto bind \t \n", fp); |
| 81 | |
| 82 | rewind (fp); |
| 83 | |
| 84 | mnt = getmntent (fp); |
| 85 | |
| 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) |
| 92 | { |
| 93 | puts ("Error while reading entry with trailing whitespaces"); |
| 94 | result = 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return result; |
| 99 | } |