]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/fstab.c
33164ee905d38e4c853ac0cd2c7ce7c978102822
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
28 * Copyright (c) 1980, 1988, 1993
29 * The Regents of the University of California. All rights reserved.
31 * The NEXTSTEP Software License Agreement specifies the terms
32 * and conditions for redistribution.
34 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
46 #include <sys/param.h>
48 #include <sys/mount.h>
50 static FILE *_fs_fp
= NULL
;
51 static struct fstab _fs_fstab
;
52 static struct fstab _root_fstab
;
53 static char _root_fstype
[MFSNAMELEN
];
54 static int firstTime
= 1;
55 static int returnedRoot
= 0;
56 static void error
__P((int));
57 static int fstabscan
__P((void));
58 static const char *slash
= "/";
59 static const char *remountedroot
= "/root";
61 /* We don't want to depend on fstab for the root filesystem entry,
62 ** since that can change when disks are added or removed from the system.
63 ** So, we stat "/" and find the _PATH_DEV entry that matches. The devname()
64 ** function can be used as a shortcut if _PATH_DEVDB exists. This returns a
65 ** string like "sd1a", so we need to prepend _PATH_DEV to it.
68 static char *getDevPath(dev_t target_dev
) {
69 static char *dev
= NULL
;
73 dev
= malloc(MAXPATHLEN
);
78 strcpy(dev
, _PATH_DEV
);
80 /* The root device in fstab should always be a block special device */
81 name
= devname(target_dev
, S_IFBLK
);
83 /* No _PATH_DEVDB. We have to search for it the slow way */
86 dirp
= opendir(_PATH_DEV
);
91 while ((ent
= readdir(dirp
)) != NULL
) {
92 /* Look for a block special device */
93 if (ent
->d_type
== DT_BLK
) {
95 strcat(dev
, ent
->d_name
);
96 if (stat(dev
, &devst
) >= 0) {
97 if (devst
.st_rdev
== target_dev
) {
102 /* set dev to _PATH_DEV and try again */
103 dev
[sizeof(_PATH_DEV
) - 1] = '\0';
106 /* We found the _PATH_DEVDB entry */
113 static int initrootentry(struct fstab
*rootentry
)
115 char *rootpath
= (char *)slash
;
116 struct stat rootstat
;
117 struct statfs rootfsinfo
;
119 if (stat(rootpath
, &rootstat
) < 0) {
123 if (statfs(rootpath
, &rootfsinfo
) < 0) {
128 /* Check to make sure we're not looking at a synthetic root: */
129 if (strcmp(rootfsinfo
.f_fstypename
, "synthfs") == 0) {
130 rootpath
= (char *)remountedroot
;
131 if (stat(rootpath
, &rootstat
) < 0) {
135 if (statfs(rootpath
, &rootfsinfo
) < 0) {
141 /* Copy the type name before returning a pointer a pointer to it */
142 strncpy(_root_fstype
, rootfsinfo
.f_fstypename
, MFSNAMELEN
);
144 rootentry
->fs_spec
= getDevPath(rootstat
.st_dev
);
145 rootentry
->fs_file
= rootpath
;
146 rootentry
->fs_vfstype
= _root_fstype
;
147 rootentry
->fs_mntops
= FSTAB_RW
;
148 rootentry
->fs_type
= FSTAB_RW
;
149 rootentry
->fs_freq
= 0;
150 rootentry
->fs_passno
= 1;
155 static int fstabscan()
158 #define MAXLINELENGTH 1024
159 static char *line
= NULL
;
160 char subline
[MAXLINELENGTH
];
167 if (initrootentry(&_root_fstab
) != 0) {
171 _fs_fstab
= _root_fstab
;
179 line
= malloc(MAXLINELENGTH
);
185 if (!(cp
= fgets(line
, MAXLINELENGTH
, _fs_fp
)))
187 /* OLD_STYLE_FSTAB */
188 if (!strpbrk(cp
, " \t")) {
189 _fs_fstab
.fs_spec
= strtok(cp
, ":\n");
190 #if defined(__APPLE__)
191 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
194 _fs_fstab
.fs_file
= strtok((char *)NULL
, ":\n");
195 /* Only list the root filesystem once */
196 if (!_fs_fstab
.fs_file
|| !(strcmp(_fs_fstab
.fs_file
, "/"))) {
199 _fs_fstab
.fs_type
= strtok((char *)NULL
, ":\n");
200 if (_fs_fstab
.fs_type
) {
201 if (!strcmp(_fs_fstab
.fs_type
, FSTAB_XX
))
203 _fs_fstab
.fs_mntops
= _fs_fstab
.fs_type
;
204 _fs_fstab
.fs_vfstype
=
205 strcmp(_fs_fstab
.fs_type
, FSTAB_SW
) ?
207 if ((cp
= strtok((char *)NULL
, ":\n"))) {
208 _fs_fstab
.fs_freq
= atoi(cp
);
209 if ((cp
= strtok((char *)NULL
, ":\n"))) {
210 _fs_fstab
.fs_passno
= atoi(cp
);
217 /* OLD_STYLE_FSTAB */
218 _fs_fstab
.fs_spec
= strtok(cp
, " \t\n");
219 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
221 _fs_fstab
.fs_file
= strtok((char *)NULL
, " \t\n");
222 /* Only list the root filesystem once */
223 if (!(strcmp(_fs_fstab
.fs_file
, "/"))) {
226 _fs_fstab
.fs_vfstype
= strtok((char *)NULL
, " \t\n");
227 _fs_fstab
.fs_mntops
= strtok((char *)NULL
, " \t\n");
228 if (_fs_fstab
.fs_mntops
== NULL
)
230 _fs_fstab
.fs_freq
= 0;
231 _fs_fstab
.fs_passno
= 0;
232 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
) {
233 _fs_fstab
.fs_freq
= atoi(cp
);
234 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
)
235 _fs_fstab
.fs_passno
= atoi(cp
);
237 strcpy(subline
, _fs_fstab
.fs_mntops
);
238 for (typexx
= 0, cp
= strtok(subline
, ","); cp
;
239 cp
= strtok((char *)NULL
, ",")) {
242 if (!strcmp(cp
, FSTAB_RW
)) {
243 _fs_fstab
.fs_type
= FSTAB_RW
;
246 if (!strcmp(cp
, FSTAB_RQ
)) {
247 _fs_fstab
.fs_type
= FSTAB_RQ
;
250 if (!strcmp(cp
, FSTAB_RO
)) {
251 _fs_fstab
.fs_type
= FSTAB_RO
;
254 if (!strcmp(cp
, FSTAB_SW
)) {
255 _fs_fstab
.fs_type
= FSTAB_SW
;
258 if (!strcmp(cp
, FSTAB_XX
)) {
259 _fs_fstab
.fs_type
= FSTAB_XX
;
269 bad
: /* no way to distinguish between EOF and syntax error */
282 return((struct fstab
*)NULL
);
289 register const char *name
;
293 if (!strcmp(_fs_fstab
.fs_spec
, name
))
295 return((struct fstab
*)NULL
);
300 register const char *name
;
304 if (!strcmp(_fs_fstab
.fs_file
, name
))
306 return((struct fstab
*)NULL
);
316 _fs_fp
= fopen(_PATH_FSTAB
, "r");
326 (void)fclose(_fs_fp
);
331 static void error(err
)
336 (void)write(STDERR_FILENO
, "fstab: ", 7);
337 (void)write(STDERR_FILENO
, _PATH_FSTAB
, sizeof(_PATH_FSTAB
) - 1);
338 (void)write(STDERR_FILENO
, ": ", 1);
340 (void)write(STDERR_FILENO
, p
, strlen(p
));
341 (void)write(STDERR_FILENO
, "\n", 1);