xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/fstab.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*722Smuffin /*
23*722Smuffin  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
24*722Smuffin  * Use is subject to license terms.
25*722Smuffin  */
26*722Smuffin 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <fstab.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <mntent.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate static	struct fstab *pfs;
350Sstevel@tonic-gate static	FILE *fs_file;
360Sstevel@tonic-gate 
37*722Smuffin static int
fstabscan(struct fstab * fs)38*722Smuffin fstabscan(struct fstab *fs)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	struct mntent *mnt;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	/* skip over all filesystem types except '4.2', 'swap' & 'ignore' */
430Sstevel@tonic-gate 	while (((mnt = getmntent(fs_file)) != NULL) &&
440Sstevel@tonic-gate 		!((strcmp(mnt->mnt_type, MNTTYPE_42) == 0) ||
450Sstevel@tonic-gate 		  (strcmp(mnt->mnt_type, MNTTYPE_SWAP) == 0) ||
460Sstevel@tonic-gate 		  (strcmp(mnt->mnt_type, MNTTYPE_IGNORE) == 0)))
470Sstevel@tonic-gate                 continue;
480Sstevel@tonic-gate 	if (mnt == NULL)
490Sstevel@tonic-gate 		return (EOF);
500Sstevel@tonic-gate 	fs->fs_spec = mnt->mnt_fsname;
510Sstevel@tonic-gate 	fs->fs_file = mnt->mnt_dir;
520Sstevel@tonic-gate 	if (strcmp(mnt->mnt_type, MNTTYPE_IGNORE) == 0) {
530Sstevel@tonic-gate 		strcpy(mnt->mnt_opts, FSTAB_XX);
540Sstevel@tonic-gate 	} else if (strcmp(mnt->mnt_type, MNTTYPE_SWAP) == 0) {
550Sstevel@tonic-gate 		strcpy(mnt->mnt_opts, FSTAB_SW);
560Sstevel@tonic-gate 	} else if (hasmntopt(mnt, MNTOPT_RO)) {
570Sstevel@tonic-gate 		strcpy(mnt->mnt_opts, FSTAB_RO);
580Sstevel@tonic-gate 	} else if (hasmntopt(mnt, MNTOPT_QUOTA)) {
590Sstevel@tonic-gate 		strcpy(mnt->mnt_opts, FSTAB_RQ);
600Sstevel@tonic-gate 	} else {
610Sstevel@tonic-gate 		strcpy(mnt->mnt_opts, FSTAB_RW);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 	fs->fs_type = mnt->mnt_opts;
640Sstevel@tonic-gate 	fs->fs_freq = mnt->mnt_freq;
650Sstevel@tonic-gate 	fs->fs_passno = mnt->mnt_passno;
660Sstevel@tonic-gate 	return (5);
670Sstevel@tonic-gate }
68*722Smuffin 
69*722Smuffin int
setfsent(void)70*722Smuffin setfsent(void)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (fs_file)
740Sstevel@tonic-gate 		endfsent();
750Sstevel@tonic-gate 	if ((fs_file = setmntent(FSTAB, "r")) == NULL) {
760Sstevel@tonic-gate 		fs_file = 0;
770Sstevel@tonic-gate 		return (0);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	return (1);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
82*722Smuffin int
endfsent(void)83*722Smuffin endfsent(void)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (fs_file) {
870Sstevel@tonic-gate 		endmntent(fs_file);
880Sstevel@tonic-gate 		fs_file = 0;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	return (1);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate struct fstab *
getfsent(void)94*722Smuffin getfsent(void)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	int nfields;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if ((fs_file == 0) && (setfsent() == 0))
990Sstevel@tonic-gate 		return ((struct fstab *)0);
1000Sstevel@tonic-gate 	if (pfs == 0) {
1010Sstevel@tonic-gate 		pfs = (struct fstab *)malloc(sizeof (struct fstab));
1020Sstevel@tonic-gate 		if (pfs == 0)
1030Sstevel@tonic-gate 			return (0);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	nfields = fstabscan(pfs);
1060Sstevel@tonic-gate 	if (nfields == EOF || nfields != 5)
1070Sstevel@tonic-gate 		return ((struct fstab *)0);
1080Sstevel@tonic-gate 	return (pfs);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate struct fstab *
getfsspec(char * name)112*722Smuffin getfsspec(char *name)
1130Sstevel@tonic-gate {
114*722Smuffin 	struct fstab *fsp;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (setfsent() == 0)	/* start from the beginning */
1170Sstevel@tonic-gate 		return ((struct fstab *)0);
1180Sstevel@tonic-gate 	while((fsp = getfsent()) != 0)
1190Sstevel@tonic-gate 		if (strcmp(fsp->fs_spec, name) == 0)
1200Sstevel@tonic-gate 			return (fsp);
1210Sstevel@tonic-gate 	return ((struct fstab *)0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate struct fstab *
getfsfile(char * name)125*722Smuffin getfsfile(char *name)
1260Sstevel@tonic-gate {
127*722Smuffin 	struct fstab *fsp;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (setfsent() == 0)	/* start from the beginning */
1300Sstevel@tonic-gate 		return ((struct fstab *)0);
1310Sstevel@tonic-gate 	while ((fsp = getfsent()) != 0)
1320Sstevel@tonic-gate 		if (strcmp(fsp->fs_file, name) == 0)
1330Sstevel@tonic-gate 			return (fsp);
1340Sstevel@tonic-gate 	return ((struct fstab *)0);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate struct fstab *
getfstype(char * type)138*722Smuffin getfstype(char *type)
1390Sstevel@tonic-gate {
140*722Smuffin 	struct fstab *fs;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (setfsent() == 0)
1430Sstevel@tonic-gate 		return ((struct fstab *)0);
1440Sstevel@tonic-gate 	while ((fs = getfsent()) != 0)
1450Sstevel@tonic-gate 		if (strcmp(fs->fs_type, type) == 0)
1460Sstevel@tonic-gate 			return (fs);
1470Sstevel@tonic-gate 	return ((struct fstab *)0);
1480Sstevel@tonic-gate }
149