OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / misc / statfs / internal_statvfs.c
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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 libc_hidden_proto(memset)
21 libc_hidden_proto(strcmp)
22 libc_hidden_proto(strsep)
23 libc_hidden_proto(setmntent)
24 libc_hidden_proto(getmntent_r)
25 libc_hidden_proto(endmntent)
26
27   /* Now fill in the fields we have information for.  */
28   buf->f_bsize = fsbuf.f_bsize;
29   /* Linux does not support f_frsize, so set it to the full block size.  */
30   buf->f_frsize = fsbuf.f_bsize;
31   buf->f_blocks = fsbuf.f_blocks;
32   buf->f_bfree = fsbuf.f_bfree;
33   buf->f_bavail = fsbuf.f_bavail;
34   buf->f_files = fsbuf.f_files;
35   buf->f_ffree = fsbuf.f_ffree;
36   if (sizeof (buf->f_fsid) == sizeof (fsbuf.f_fsid))
37     buf->f_fsid = (fsbuf.f_fsid.__val[0]
38                    | ((unsigned long int) fsbuf.f_fsid.__val[1]
39                       << (8 * (sizeof (buf->f_fsid)
40                                - sizeof (fsbuf.f_fsid.__val[0])))));
41   else
42     /* We cannot help here.  The statvfs element is not large enough to
43        contain both words of the statfs f_fsid field.  */
44     buf->f_fsid = fsbuf.f_fsid.__val[0];
45 #ifdef _STATVFSBUF_F_UNUSED
46   buf->__f_unused = 0;
47 #endif
48   buf->f_namemax = fsbuf.f_namelen;
49   memset (buf->__f_spare, '\0', 6 * sizeof (int));
50
51   /* What remains to do is to fill the fields f_favail and f_flag.  */
52
53   /* XXX I have no idea how to compute f_favail.  Any idea???  */
54   buf->f_favail = buf->f_ffree;
55
56   /* Determining the flags is tricky.  We have to read /proc/mounts or
57      the /etc/mtab file and search for the entry which matches the given
58      file.  The way we can test for matching filesystem is using the
59      device number.  */
60   buf->f_flag = 0;
61   if (STAT (&st) >= 0)
62     {
63       int save_errno = errno;
64       struct mntent mntbuf;
65       FILE *mtab;
66
67       mtab = setmntent ("/proc/mounts", "r");
68       if (mtab == NULL)
69         mtab = setmntent (_PATH_MOUNTED, "r");
70
71       if (mtab != NULL)
72         {
73           char tmpbuf[1024];
74
75           while (getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
76             {
77               struct stat fsst;
78
79               /* Find out about the device the current entry is for.  */
80               if (stat (mntbuf.mnt_dir, &fsst) >= 0
81                   && st.st_dev == fsst.st_dev)
82                 {
83                   /* Bingo, we found the entry for the device FD is on.
84                      Now interpret the option string.  */
85                   char *cp = mntbuf.mnt_opts;
86                   char *opt;
87
88                   while ((opt = strsep (&cp, ",")) != NULL)
89                     if (strcmp (opt, "ro") == 0)
90                       buf->f_flag |= ST_RDONLY;
91                     else if (strcmp (opt, "nosuid") == 0)
92                       buf->f_flag |= ST_NOSUID;
93                     else if (strcmp (opt, "noexec") == 0)
94                       buf->f_flag |= ST_NOEXEC;
95                     else if (strcmp (opt, "nodev") == 0)
96                       buf->f_flag |= ST_NODEV;
97                     else if (strcmp (opt, "sync") == 0)
98                       buf->f_flag |= ST_SYNCHRONOUS;
99                     else if (strcmp (opt, "mand") == 0)
100                       buf->f_flag |= ST_MANDLOCK;
101                     else if (strcmp (opt, "noatime") == 0)
102                       buf->f_flag |= ST_NOATIME;
103                     else if (strcmp (opt, "nodiratime") == 0)
104                       buf->f_flag |= ST_NODIRATIME;
105
106                   /* We can stop looking for more entries.  */
107                   break;
108                 }
109             }
110
111           /* Close the file.  */
112           endmntent (mtab);
113         }
114
115       __set_errno (save_errno);
116     }