1 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 /* The kernel hints us if the f_flags is valid  */
20 #define ST_VALID 0x0020
21 
22   /* Now fill in the fields we have information for.  */
23   buf->f_bsize = fsbuf.f_bsize;
24 #ifdef _STATFS_F_FRSIZE
25   buf->f_frsize = fsbuf.f_frsize;
26 #else
27   /* No support for f_frsize so set it to the full block size.  */
28   buf->f_frsize = fsbuf.f_bsize;
29 #endif
30   buf->f_blocks = fsbuf.f_blocks;
31   buf->f_bfree = fsbuf.f_bfree;
32   buf->f_bavail = fsbuf.f_bavail;
33   buf->f_files = fsbuf.f_files;
34   buf->f_ffree = fsbuf.f_ffree;
35   if (sizeof (buf->f_fsid) == sizeof (fsbuf.f_fsid))
36     buf->f_fsid = (fsbuf.f_fsid.__val[0]
37 		   | ((unsigned long long int) fsbuf.f_fsid.__val[1]
38 		      << (8 * (sizeof (buf->f_fsid)
39 			       - sizeof (fsbuf.f_fsid.__val[0])))));
40   else
41     /* We cannot help here.  The statvfs element is not large enough to
42        contain both words of the statfs f_fsid field.  */
43     buf->f_fsid = fsbuf.f_fsid.__val[0];
44 #ifdef _STATVFSBUF_F_UNUSED
45   buf->__f_unused = 0;
46 #endif
47   buf->f_namemax = fsbuf.f_namelen;
48   memset (buf->__f_spare, '\0', sizeof(buf->__f_spare));
49   /* XXX I have no idea how to compute f_favail.  Any idea???  */
50   buf->f_favail = buf->f_ffree;
51 
52   /* Determining the flags is tricky.  We have to read /proc/mounts or
53      the /etc/mtab file and search for the entry which matches the given
54      file.  The way we can test for matching filesystem is using the
55      device number.  */
56   buf->f_flag = 0;
57   if (STAT (&st) >= 0
58 #ifdef _STATFS_F_FLAGS
59 	  && (fsbuf.f_flags & ST_VALID) == 0
60 #endif
61 	 ) {
62 	  int save_errno = errno;
63 	  struct mntent mntbuf;
64 	  FILE *mtab;
65 
66 	  mtab = setmntent ("/proc/mounts", "r");
67 	  if (mtab == NULL)
68 		mtab = setmntent (_PATH_MOUNTED, "r");
69 	  if (mtab != NULL) {
70 		char tmpbuf[1024];
71 
72 		while (getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf))) {
73 		  struct stat fsst;
74 
75 		  /* Find out about the device the current entry is for.  */
76 		  if (stat (mntbuf.mnt_dir, &fsst) >= 0
77 			  && st.st_dev == fsst.st_dev) {
78 			  /* Bingo, we found the entry for the device FD is on.
79 				 Now interpret the option string.  */
80 			  char *cp = mntbuf.mnt_opts;
81 			  char *opt;
82 
83 			  while ((opt = strsep (&cp, ",")) != NULL)
84 				if (strcmp (opt, "ro") == 0)
85 				  buf->f_flag |= ST_RDONLY;
86 				else if (strcmp (opt, "nosuid") == 0)
87 				  buf->f_flag |= ST_NOSUID;
88 #ifdef __USE_GNU
89 				else if (strcmp (opt, "noexec") == 0)
90 				  buf->f_flag |= ST_NOEXEC;
91 				else if (strcmp (opt, "nodev") == 0)
92 				  buf->f_flag |= ST_NODEV;
93 				else if (strcmp (opt, "sync") == 0)
94 				  buf->f_flag |= ST_SYNCHRONOUS;
95 				else if (strcmp (opt, "mand") == 0)
96 				  buf->f_flag |= ST_MANDLOCK;
97 				else if (strcmp (opt, "noatime") == 0)
98 				  buf->f_flag |= ST_NOATIME;
99 				else if (strcmp (opt, "nodiratime") == 0)
100 				  buf->f_flag |= ST_NODIRATIME;
101 				else if (strcmp (opt, "relatime") == 0)
102 				  buf->f_flag |= ST_RELATIME;
103 #endif
104 			  /* We can stop looking for more entries.  */
105 			  break;
106 		  }
107 		}
108 		/* Close the file.  */
109 		endmntent (mtab);
110 	  }
111 	  __set_errno (save_errno);
112   }
113 #ifdef _STATFS_F_FLAGS
114 	else
115 	  buf->f_flag = fsbuf.f_flags ^ ST_VALID;
116 #endif
117