summaryrefslogtreecommitdiffstats
path: root/mount/fstab.c
blob: d13280f8fc00b2df8e9db4aa6c8bf5f8177613f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* $Header: /home/faith/cvs/util-linux/mount/fstab.c,v 1.2 1995/10/07 01:32:03 faith Exp $ */

#include "fstab.h"
#include <stdio.h>

#define streq(s, t)	(strcmp ((s), (t)) == 0)

/* These routines are superseded by mntent(3), but I use them for
   convenience.  Mntent(3) is used in the implementation, so be
   very careful about the static buffers that are returned.  */


static FILE *F_fstab = NULL;

/* Open fstab or rewind if already open.  */
int
setfsent (void)
{
  if (F_fstab)
    return (fseek (F_fstab, 0L, SEEK_SET) == 0);

  F_fstab = setmntent (_PATH_FSTAB, "r");
  return (F_fstab != NULL);
}

/* Close fstab.  */
void
endfsent (void)
{
  endmntent (F_fstab);
}

/* Return next entry in fstab, skipping ignore entries.  I also put
   in some ugly hacks here to skip comments and blank lines.  */
struct mntent *
getfsent (void)
{
  struct mntent *fstab;

  if (!F_fstab && !setfsent())
    return 0;

  for (;;)
    {
      fstab = getmntent (F_fstab);
      if (fstab == NULL)
	{
	  if (!feof (F_fstab) && !ferror (F_fstab))
	    continue;
	  else
	    break;
	}
      else if ((*fstab->mnt_fsname != '#')
	       && !streq (fstab->mnt_type, MNTTYPE_IGNORE))
	break;
    }
  return fstab;
}

/* Find the dir FILE in fstab.  */
struct mntent *
getfsfile (const char *file)
{
  struct mntent *fstab;

  /* Open or rewind fstab.  */
  if (!setfsent ())
    return 0;

  while ((fstab = getfsent ()))
    if (streq (fstab->mnt_dir, file))
      break;

  return fstab;
}

/* Find the device SPEC in fstab.  */
struct mntent *
getfsspec (const char *spec)
{
  struct mntent *fstab;

  /* Open or rewind fstab.  */
  if (!setfsent())
    return 0;

  while ((fstab = getfsent ()))
    if (streq (fstab->mnt_fsname, spec))
      break;

  return fstab;
}