2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
27 * Copyright (c) 1980, 1988, 1993
28 * The Regents of the University of California. All rights reserved.
30 * The NEXTSTEP Software License Agreement specifies the terms
31 * and conditions for redistribution.
33 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
45 #include <sys/param.h>
47 #include <sys/mount.h>
50 static struct fstab _fs_fstab
;
51 static struct fstab _root_fstab
;
52 static char _root_fstype
[MFSNAMELEN
];
53 static int firstTime
= 1;
54 static int returnedRoot
= 0;
55 static void error
__P((int));
56 static int fstabscan
__P((void));
57 static const char *slash
= "/";
58 static const char *remountedroot
= "/root";
60 /* We don't want to depend on fstab for the root filesystem entry,
61 ** since that can change when disks are added or removed from the system.
62 ** So, we stat "/" and find the _PATH_DEV entry that matches. The devname()
63 ** function can be used as a shortcut if _PATH_DEVDB exists. This returns a
64 ** string like "sd1a", so we need to prepend _PATH_DEV to it.
67 static char *getDevPath(dev_t target_dev
) {
68 static char *dev
= NULL
;
72 dev
= malloc(MAXPATHLEN
);
77 strcpy(dev
, _PATH_DEV
);
79 /* The root device in fstab should always be a block special device */
80 name
= devname(target_dev
, S_IFBLK
);
82 /* No _PATH_DEVDB. We have to search for it the slow way */
85 dirp
= opendir(_PATH_DEV
);
90 while ((ent
= readdir(dirp
)) != NULL
) {
91 /* Look for a block special device */
92 if (ent
->d_type
== DT_BLK
) {
94 strcat(dev
, ent
->d_name
);
95 if (stat(dev
, &devst
) >= 0) {
96 if (devst
.st_rdev
== target_dev
) {
101 /* set dev to _PATH_DEV and try again */
102 dev
[sizeof(_PATH_DEV
) - 1] = '\0';
105 /* We found the _PATH_DEVDB entry */
112 static int initrootentry(struct fstab
*rootentry
)
114 char *rootpath
= (char *)slash
;
115 struct stat rootstat
;
116 struct statfs rootfsinfo
;
118 if (stat(rootpath
, &rootstat
) < 0) {
122 if (statfs(rootpath
, &rootfsinfo
) < 0) {
127 /* Check to make sure we're not looking at a synthetic root: */
128 if (strcmp(rootfsinfo
.f_fstypename
, "synthfs") == 0) {
129 rootpath
= (char *)remountedroot
;
130 if (stat(rootpath
, &rootstat
) < 0) {
134 if (statfs(rootpath
, &rootfsinfo
) < 0) {
140 /* Copy the type name before returning a pointer a pointer to it */
141 strncpy(_root_fstype
, rootfsinfo
.f_fstypename
, MFSNAMELEN
);
143 rootentry
->fs_spec
= getDevPath(rootstat
.st_dev
);
144 rootentry
->fs_file
= rootpath
;
145 rootentry
->fs_vfstype
= _root_fstype
;
146 rootentry
->fs_mntops
= FSTAB_RW
;
147 rootentry
->fs_type
= FSTAB_RW
;
148 rootentry
->fs_freq
= 0;
149 rootentry
->fs_passno
= 1;
154 static int fstabscan()
157 #define MAXLINELENGTH 1024
158 static char *line
= NULL
;
159 char subline
[MAXLINELENGTH
];
166 if (initrootentry(&_root_fstab
) != 0) {
170 _fs_fstab
= _root_fstab
;
178 line
= malloc(MAXLINELENGTH
);
184 if (!(cp
= fgets(line
, sizeof(line
), _fs_fp
)))
186 /* OLD_STYLE_FSTAB */
187 if (!strpbrk(cp
, " \t")) {
188 _fs_fstab
.fs_spec
= strtok(cp
, ":\n");
189 #if defined(__APPLE__)
190 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
193 _fs_fstab
.fs_file
= strtok((char *)NULL
, ":\n");
194 /* Only list the root filesystem once */
195 if (!(strcmp(_fs_fstab
.fs_file
, "/"))) {
198 _fs_fstab
.fs_type
= strtok((char *)NULL
, ":\n");
199 if (_fs_fstab
.fs_type
) {
200 if (!strcmp(_fs_fstab
.fs_type
, FSTAB_XX
))
202 _fs_fstab
.fs_mntops
= _fs_fstab
.fs_type
;
203 _fs_fstab
.fs_vfstype
=
204 strcmp(_fs_fstab
.fs_type
, FSTAB_SW
) ?
206 if ((cp
= strtok((char *)NULL
, ":\n"))) {
207 _fs_fstab
.fs_freq
= atoi(cp
);
208 if ((cp
= strtok((char *)NULL
, ":\n"))) {
209 _fs_fstab
.fs_passno
= atoi(cp
);
216 /* OLD_STYLE_FSTAB */
217 _fs_fstab
.fs_spec
= strtok(cp
, " \t\n");
218 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
220 _fs_fstab
.fs_file
= strtok((char *)NULL
, " \t\n");
221 /* Only list the root filesystem once */
222 if (!(strcmp(_fs_fstab
.fs_file
, "/"))) {
225 _fs_fstab
.fs_vfstype
= strtok((char *)NULL
, " \t\n");
226 _fs_fstab
.fs_mntops
= strtok((char *)NULL
, " \t\n");
227 if (_fs_fstab
.fs_mntops
== NULL
)
229 _fs_fstab
.fs_freq
= 0;
230 _fs_fstab
.fs_passno
= 0;
231 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
) {
232 _fs_fstab
.fs_freq
= atoi(cp
);
233 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
)
234 _fs_fstab
.fs_passno
= atoi(cp
);
236 strcpy(subline
, _fs_fstab
.fs_mntops
);
237 for (typexx
= 0, cp
= strtok(subline
, ","); cp
;
238 cp
= strtok((char *)NULL
, ",")) {
241 if (!strcmp(cp
, FSTAB_RW
)) {
242 _fs_fstab
.fs_type
= FSTAB_RW
;
245 if (!strcmp(cp
, FSTAB_RQ
)) {
246 _fs_fstab
.fs_type
= FSTAB_RQ
;
249 if (!strcmp(cp
, FSTAB_RO
)) {
250 _fs_fstab
.fs_type
= FSTAB_RO
;
253 if (!strcmp(cp
, FSTAB_SW
)) {
254 _fs_fstab
.fs_type
= FSTAB_SW
;
257 if (!strcmp(cp
, FSTAB_XX
)) {
258 _fs_fstab
.fs_type
= FSTAB_XX
;
268 bad
: /* no way to distinguish between EOF and syntax error */
281 return((struct fstab
*)NULL
);
288 register const char *name
;
292 if (!strcmp(_fs_fstab
.fs_spec
, name
))
294 return((struct fstab
*)NULL
);
299 register const char *name
;
303 if (!strcmp(_fs_fstab
.fs_file
, name
))
305 return((struct fstab
*)NULL
);
315 _fs_fp
= fopen(_PATH_FSTAB
, "r");
325 (void)fclose(_fs_fp
);
330 static void error(err
)
335 (void)write(STDERR_FILENO
, "fstab: ", 7);
336 (void)write(STDERR_FILENO
, _PATH_FSTAB
, sizeof(_PATH_FSTAB
) - 1);
337 (void)write(STDERR_FILENO
, ": ", 1);
339 (void)write(STDERR_FILENO
, p
, strlen(p
));
340 (void)write(STDERR_FILENO
, "\n", 1);