]>
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 * 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 fstabscan
__P((void));
57 static char *getRootDev(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
[MAXPATHLEN
];
72 strcpy(dev
, _PATH_DEV
);
74 /* The root device in fstab should always be a block special device */
75 name
= devname(target_dev
, S_IFBLK
);
77 /* No _PATH_DEVDB. We have to search for it the slow way */
80 dirp
= opendir(_PATH_DEV
);
85 while ((ent
= readdir(dirp
)) != NULL
) {
86 /* Look for a block special device */
87 if (ent
->d_type
== DT_BLK
) {
89 strcat(dev
, ent
->d_name
);
90 if (stat(dev
, &devst
) >= 0) {
91 if (devst
.st_rdev
== target_dev
) {
96 /* set dev to _PATH_DEV and try again */
97 dev
[sizeof(_PATH_DEV
) - 1] = '\0';
100 /* We found the _PATH_DEVDB entry */
107 static int initrootentry(struct fstab
*rootentry
)
109 char *rootpath
= slash
;
110 struct stat rootstat
;
111 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
= 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
[MAXLINELENGTH
];
155 char subline
[MAXLINELENGTH
];
162 if (initrootentry(&_root_fstab
) != 0) {
166 _fs_fstab
= _root_fstab
;
173 if (!(cp
= fgets(line
, sizeof(line
), _fs_fp
)))
175 /* OLD_STYLE_FSTAB */
176 if (!strpbrk(cp
, " \t")) {
177 _fs_fstab
.fs_spec
= strtok(cp
, ":\n");
178 #if defined(__APPLE__)
179 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
182 _fs_fstab
.fs_file
= strtok((char *)NULL
, ":\n");
183 /* Only list the root filesystem once */
184 if (!(strcmp(_fs_fstab
.fs_file
, "/"))) {
187 _fs_fstab
.fs_type
= strtok((char *)NULL
, ":\n");
188 if (_fs_fstab
.fs_type
) {
189 if (!strcmp(_fs_fstab
.fs_type
, FSTAB_XX
))
191 _fs_fstab
.fs_mntops
= _fs_fstab
.fs_type
;
192 _fs_fstab
.fs_vfstype
=
193 strcmp(_fs_fstab
.fs_type
, FSTAB_SW
) ?
195 if ((cp
= strtok((char *)NULL
, ":\n"))) {
196 _fs_fstab
.fs_freq
= atoi(cp
);
197 if ((cp
= strtok((char *)NULL
, ":\n"))) {
198 _fs_fstab
.fs_passno
= atoi(cp
);
205 /* OLD_STYLE_FSTAB */
206 _fs_fstab
.fs_spec
= strtok(cp
, " \t\n");
207 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
209 _fs_fstab
.fs_file
= strtok((char *)NULL
, " \t\n");
210 /* Only list the root filesystem once */
211 if (!(strcmp(_fs_fstab
.fs_file
, "/"))) {
214 _fs_fstab
.fs_vfstype
= strtok((char *)NULL
, " \t\n");
215 _fs_fstab
.fs_mntops
= strtok((char *)NULL
, " \t\n");
216 if (_fs_fstab
.fs_mntops
== NULL
)
218 _fs_fstab
.fs_freq
= 0;
219 _fs_fstab
.fs_passno
= 0;
220 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
) {
221 _fs_fstab
.fs_freq
= atoi(cp
);
222 if ((cp
= strtok((char *)NULL
, " \t\n")) != NULL
)
223 _fs_fstab
.fs_passno
= atoi(cp
);
225 strcpy(subline
, _fs_fstab
.fs_mntops
);
226 for (typexx
= 0, cp
= strtok(subline
, ","); cp
;
227 cp
= strtok((char *)NULL
, ",")) {
230 if (!strcmp(cp
, FSTAB_RW
)) {
231 _fs_fstab
.fs_type
= FSTAB_RW
;
234 if (!strcmp(cp
, FSTAB_RQ
)) {
235 _fs_fstab
.fs_type
= FSTAB_RQ
;
238 if (!strcmp(cp
, FSTAB_RO
)) {
239 _fs_fstab
.fs_type
= FSTAB_RO
;
242 if (!strcmp(cp
, FSTAB_SW
)) {
243 _fs_fstab
.fs_type
= FSTAB_SW
;
246 if (!strcmp(cp
, FSTAB_XX
)) {
247 _fs_fstab
.fs_type
= FSTAB_XX
;
257 bad
: /* no way to distinguish between EOF and syntax error */
270 return((struct fstab
*)NULL
);
277 register const char *name
;
281 if (!strcmp(_fs_fstab
.fs_spec
, name
))
283 return((struct fstab
*)NULL
);
288 register const char *name
;
292 if (!strcmp(_fs_fstab
.fs_file
, name
))
294 return((struct fstab
*)NULL
);
304 _fs_fp
= fopen(_PATH_FSTAB
, "r");
314 (void)fclose(_fs_fp
);
319 static void error(err
)
324 (void)write(STDERR_FILENO
, "fstab: ", 7);
325 (void)write(STDERR_FILENO
, _PATH_FSTAB
, sizeof(_PATH_FSTAB
) - 1);
326 (void)write(STDERR_FILENO
, ": ", 1);
328 (void)write(STDERR_FILENO
, p
, strlen(p
));
329 (void)write(STDERR_FILENO
, "\n", 1);