]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/fstab.c
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 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
41 #include <sys/param.h>
43 #include <sys/mount.h>
45 static FILE *_fs_fp
= NULL
;
46 static struct fstab _fs_fstab
;
47 static struct fstab _root_fstab
;
48 static char _root_fstype
[MFSNAMELEN
];
49 static int firstTime
= 1;
50 static int returnedRoot
= 0;
51 static void error
__P((int));
52 static int fstabscan
__P((void));
53 static const char *slash
= "/";
54 static const char *remountedroot
= "/root";
56 /* We don't want to depend on fstab for the root filesystem entry,
57 ** since that can change when disks are added or removed from the system.
58 ** So, we stat "/" and find the _PATH_DEV entry that matches. The devname()
59 ** function can be used as a shortcut if _PATH_DEVDB exists. This returns a
60 ** string like "sd1a", so we need to prepend _PATH_DEV to it.
63 static char *getDevPath(dev_t target_dev
) {
64 static char *dev
= NULL
;
68 dev
= malloc(MAXPATHLEN
);
73 strcpy(dev
, _PATH_DEV
);
75 /* The root device in fstab should always be a block special device */
76 name
= devname(target_dev
, S_IFBLK
);
78 /* No _PATH_DEVDB. We have to search for it the slow way */
81 dirp
= opendir(_PATH_DEV
);
86 while ((ent
= readdir(dirp
)) != NULL
) {
87 /* Look for a block special device */
88 if (ent
->d_type
== DT_BLK
) {
90 strcat(dev
, ent
->d_name
);
91 if (stat(dev
, &devst
) >= 0) {
92 if (devst
.st_rdev
== target_dev
) {
97 /* set dev to _PATH_DEV and try again */
98 dev
[sizeof(_PATH_DEV
) - 1] = '\0';
101 /* We found the _PATH_DEVDB entry */
108 static int initrootentry(struct fstab
*rootentry
)
110 char *rootpath
= (char *)slash
;
111 struct stat rootstat
;
112 struct statfs rootfsinfo
;
114 if (stat(rootpath
, &rootstat
) < 0) {
118 if (statfs(rootpath
, &rootfsinfo
) < 0) {
123 /* Check to make sure we're not looking at a synthetic root: */
124 if (strcmp(rootfsinfo
.f_fstypename
, "synthfs") == 0) {
125 rootpath
= (char *)remountedroot
;
126 if (stat(rootpath
, &rootstat
) < 0) {
130 if (statfs(rootpath
, &rootfsinfo
) < 0) {
136 /* Copy the type name before returning a pointer a pointer to it */
137 strncpy(_root_fstype
, rootfsinfo
.f_fstypename
, MFSNAMELEN
);
139 rootentry
->fs_spec
= getDevPath(rootstat
.st_dev
);
140 rootentry
->fs_file
= rootpath
;
141 rootentry
->fs_vfstype
= _root_fstype
;
142 rootentry
->fs_mntops
= FSTAB_RW
;
143 rootentry
->fs_type
= FSTAB_RW
;
144 rootentry
->fs_freq
= 0;
145 rootentry
->fs_passno
= 1;
150 static int fstabscan()
153 #define MAXLINELENGTH 1024
154 static char *line
= NULL
;
155 char subline
[MAXLINELENGTH
];
162 if (initrootentry(&_root_fstab
) != 0) {
166 _fs_fstab
= _root_fstab
;
174 line
= malloc(MAXLINELENGTH
);
180 if (!(cp
= fgets(line
, MAXLINELENGTH
, _fs_fp
)))
182 /* OLD_STYLE_FSTAB */
183 if (!strpbrk(cp
, " \t")) {
184 _fs_fstab
.fs_spec
= strtok(cp
, ":\n");
185 #if defined(__APPLE__)
186 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
189 _fs_fstab
.fs_file
= strtok((char *)NULL
, ":\n");
190 /* Only list the root filesystem once */
191 if (!_fs_fstab
.fs_file
|| !(strcmp(_fs_fstab
.fs_file
, "/"))) {
194 _fs_fstab
.fs_type
= strtok((char *)NULL
, ":\n");
195 if (_fs_fstab
.fs_type
) {
196 if (!strcmp(_fs_fstab
.fs_type
, FSTAB_XX
))
198 _fs_fstab
.fs_mntops
= _fs_fstab
.fs_type
;
199 _fs_fstab
.fs_vfstype
=
200 strcmp(_fs_fstab
.fs_type
, FSTAB_SW
) ?
202 if ((cp
= strtok((char *)NULL
, ":\n"))) {
203 _fs_fstab
.fs_freq
= atoi(cp
);
204 if ((cp
= strtok((char *)NULL
, ":\n"))) {
205 _fs_fstab
.fs_passno
= atoi(cp
);
212 /* OLD_STYLE_FSTAB */
213 _fs_fstab
.fs_spec
= strtok(cp
, " \t\n");
214 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
216 _fs_fstab
.fs_file
= strtok((char *)NULL
, " \t\n");
217 /* Only list the root filesystem once */
218 if (!(strcmp(_fs_fstab
.fs_file
, "/"))) {
221 _fs_fstab
.fs_vfstype
= strtok((char *)NULL
, " \t\n");
222 _fs_fstab
.fs_mntops
= strtok((char *)NULL
, " \t\n");
223 if (_fs_fstab
.fs_mntops
== NULL
)
225 _fs_fstab
.fs_freq
= 0;
226 _fs_fstab
.fs_passno
= 0;
227 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
) {
228 _fs_fstab
.fs_freq
= atoi(cp
);
229 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
)
230 _fs_fstab
.fs_passno
= atoi(cp
);
232 strcpy(subline
, _fs_fstab
.fs_mntops
);
233 for (typexx
= 0, cp
= strtok(subline
, ","); cp
;
234 cp
= strtok((char *)NULL
, ",")) {
237 if (!strcmp(cp
, FSTAB_RW
)) {
238 _fs_fstab
.fs_type
= FSTAB_RW
;
241 if (!strcmp(cp
, FSTAB_RQ
)) {
242 _fs_fstab
.fs_type
= FSTAB_RQ
;
245 if (!strcmp(cp
, FSTAB_RO
)) {
246 _fs_fstab
.fs_type
= FSTAB_RO
;
249 if (!strcmp(cp
, FSTAB_SW
)) {
250 _fs_fstab
.fs_type
= FSTAB_SW
;
253 if (!strcmp(cp
, FSTAB_XX
)) {
254 _fs_fstab
.fs_type
= FSTAB_XX
;
264 bad
: /* no way to distinguish between EOF and syntax error */
277 return((struct fstab
*)NULL
);
284 register const char *name
;
288 if (!strcmp(_fs_fstab
.fs_spec
, name
))
290 return((struct fstab
*)NULL
);
295 register const char *name
;
299 if (!strcmp(_fs_fstab
.fs_file
, name
))
301 return((struct fstab
*)NULL
);
311 _fs_fp
= fopen(_PATH_FSTAB
, "r");
321 (void)fclose(_fs_fp
);
326 static void error(err
)
331 (void)write(STDERR_FILENO
, "fstab: ", 7);
332 (void)write(STDERR_FILENO
, _PATH_FSTAB
, sizeof(_PATH_FSTAB
) - 1);
333 (void)write(STDERR_FILENO
, ": ", 1);
335 (void)write(STDERR_FILENO
, p
, strlen(p
));
336 (void)write(STDERR_FILENO
, "\n", 1);