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 */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1990 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <mntent.h>
320Sstevel@tonic-gate #include <sys/file.h>
33*722Smuffin #include <malloc.h>
34*722Smuffin
35*722Smuffin static int mntprtent(FILE *, struct mntent *);
360Sstevel@tonic-gate
370Sstevel@tonic-gate static struct mntent *mntp;
380Sstevel@tonic-gate
390Sstevel@tonic-gate struct mntent *
_mnt(void)40*722Smuffin _mnt(void)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate
430Sstevel@tonic-gate if (mntp == 0)
440Sstevel@tonic-gate mntp = (struct mntent *)calloc(1, sizeof (struct mntent));
450Sstevel@tonic-gate return (mntp);
460Sstevel@tonic-gate }
470Sstevel@tonic-gate
480Sstevel@tonic-gate static char *
mntstr(char ** p)49*722Smuffin mntstr(char **p)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate unsigned char *cp = (unsigned char *) *p;
520Sstevel@tonic-gate unsigned char *retstr;
530Sstevel@tonic-gate
540Sstevel@tonic-gate while (*cp && isspace(*cp))
550Sstevel@tonic-gate cp++;
560Sstevel@tonic-gate retstr = cp;
570Sstevel@tonic-gate while (*cp && !isspace(*cp))
580Sstevel@tonic-gate cp++;
590Sstevel@tonic-gate if (*cp) {
600Sstevel@tonic-gate *cp = '\0';
610Sstevel@tonic-gate cp++;
620Sstevel@tonic-gate }
630Sstevel@tonic-gate *p = (char *) cp;
640Sstevel@tonic-gate return ((char *)retstr);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate static int
mntdigit(char ** p)68*722Smuffin mntdigit(char **p)
690Sstevel@tonic-gate {
70*722Smuffin int value = 0;
710Sstevel@tonic-gate unsigned char *cp = (unsigned char *) *p;
720Sstevel@tonic-gate
730Sstevel@tonic-gate while (*cp && isspace(*cp))
740Sstevel@tonic-gate cp++;
750Sstevel@tonic-gate for (; *cp && isdigit(*cp); cp++) {
760Sstevel@tonic-gate value *= 10;
770Sstevel@tonic-gate value += *cp - '0';
780Sstevel@tonic-gate }
790Sstevel@tonic-gate while (*cp && !isspace(*cp))
800Sstevel@tonic-gate cp++;
810Sstevel@tonic-gate if (*cp) {
820Sstevel@tonic-gate *cp = '\0';
830Sstevel@tonic-gate cp++;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate *p = (char *) cp;
860Sstevel@tonic-gate return (value);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
89*722Smuffin static int
mnttabscan(FILE * mnttabp,struct mntent * mnt)90*722Smuffin mnttabscan(FILE *mnttabp, struct mntent *mnt)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate static char *line = NULL;
930Sstevel@tonic-gate char *cp;
940Sstevel@tonic-gate
950Sstevel@tonic-gate if (line == NULL)
960Sstevel@tonic-gate line = (char *)malloc(BUFSIZ+1);
970Sstevel@tonic-gate do {
980Sstevel@tonic-gate cp = fgets(line, BUFSIZ, mnttabp);
990Sstevel@tonic-gate if (cp == NULL) {
1000Sstevel@tonic-gate return (EOF);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate } while (*cp == '#');
1030Sstevel@tonic-gate mnt->mnt_fsname = mntstr(&cp);
1040Sstevel@tonic-gate if (*cp == '\0')
1050Sstevel@tonic-gate return (1);
1060Sstevel@tonic-gate mnt->mnt_dir = mntstr(&cp);
1070Sstevel@tonic-gate if (*cp == '\0')
1080Sstevel@tonic-gate return (2);
1090Sstevel@tonic-gate mnt->mnt_type = mntstr(&cp);
1100Sstevel@tonic-gate if (*cp == '\0')
1110Sstevel@tonic-gate return (3);
1120Sstevel@tonic-gate mnt->mnt_opts = mntstr(&cp);
1130Sstevel@tonic-gate if (*cp == '\0')
1140Sstevel@tonic-gate return (4);
1150Sstevel@tonic-gate mnt->mnt_freq = mntdigit(&cp);
1160Sstevel@tonic-gate if (*cp == '\0')
1170Sstevel@tonic-gate return (5);
1180Sstevel@tonic-gate mnt->mnt_passno = mntdigit(&cp);
1190Sstevel@tonic-gate return (6);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate FILE *
setmntent(char * fname,char * flag)123*722Smuffin setmntent(char *fname, char *flag)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate FILE *mnttabp;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if ((mnttabp = fopen(fname, flag)) == NULL) {
1280Sstevel@tonic-gate return (NULL);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate for (; *flag ; flag++) {
1310Sstevel@tonic-gate if (*flag == 'w' || *flag == 'a' || *flag == '+') {
1320Sstevel@tonic-gate if (flock(fileno(mnttabp), LOCK_EX) < 0) {
1330Sstevel@tonic-gate fclose(mnttabp);
1340Sstevel@tonic-gate return (NULL);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate return (mnttabp);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate int
endmntent(FILE * mnttabp)143*722Smuffin endmntent(FILE *mnttabp)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if (mnttabp) {
1470Sstevel@tonic-gate fclose(mnttabp);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate return (1);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate struct mntent *
getmntent(FILE * mnttabp)153*722Smuffin getmntent(FILE *mnttabp)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate int nfields;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (mnttabp == 0)
1580Sstevel@tonic-gate return ((struct mntent *)0);
1590Sstevel@tonic-gate if (_mnt() == 0)
1600Sstevel@tonic-gate return ((struct mntent *)0);
1610Sstevel@tonic-gate nfields = mnttabscan(mnttabp, mntp);
1620Sstevel@tonic-gate if (nfields == EOF || nfields != 6)
1630Sstevel@tonic-gate return ((struct mntent *)0);
1640Sstevel@tonic-gate return (mntp);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
167*722Smuffin int
addmntent(FILE * mnttabp,struct mntent * mnt)168*722Smuffin addmntent(FILE *mnttabp, struct mntent *mnt)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate if (fseek(mnttabp, 0L, 2) < 0)
1710Sstevel@tonic-gate return (1);
1720Sstevel@tonic-gate if (mnt == (struct mntent *)0)
1730Sstevel@tonic-gate return (1);
1740Sstevel@tonic-gate if (mnt->mnt_fsname == NULL || mnt->mnt_dir == NULL ||
1750Sstevel@tonic-gate mnt->mnt_type == NULL || mnt->mnt_opts == NULL)
1760Sstevel@tonic-gate return (1);
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate mntprtent(mnttabp, mnt);
1790Sstevel@tonic-gate return (0);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate static char *
mntopt(char ** p)183*722Smuffin mntopt(char **p)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate unsigned char *cp = (unsigned char *) *p;
1860Sstevel@tonic-gate unsigned char *retstr;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate while (*cp && isspace(*cp))
1890Sstevel@tonic-gate cp++;
1900Sstevel@tonic-gate retstr = cp;
1910Sstevel@tonic-gate while (*cp && *cp != ',')
1920Sstevel@tonic-gate cp++;
1930Sstevel@tonic-gate if (*cp) {
1940Sstevel@tonic-gate *cp = '\0';
1950Sstevel@tonic-gate cp++;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate *p = (char *) cp;
1980Sstevel@tonic-gate return ((char *)retstr);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate char *
hasmntopt(struct mntent * mnt,char * opt)202*722Smuffin hasmntopt(struct mntent *mnt, char *opt)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate char *f, *opts;
2050Sstevel@tonic-gate static char *tmpopts;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (tmpopts == 0) {
2080Sstevel@tonic-gate tmpopts = (char *)calloc(256, sizeof (char));
2090Sstevel@tonic-gate if (tmpopts == 0)
2100Sstevel@tonic-gate return (0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate strcpy(tmpopts, mnt->mnt_opts);
2130Sstevel@tonic-gate opts = tmpopts;
2140Sstevel@tonic-gate f = mntopt(&opts);
2150Sstevel@tonic-gate for (; *f; f = mntopt(&opts)) {
2160Sstevel@tonic-gate if (strncmp(opt, f, strlen(opt)) == 0)
2170Sstevel@tonic-gate return (f - tmpopts + mnt->mnt_opts);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate return (NULL);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
222*722Smuffin static int
mntprtent(FILE * mnttabp,struct mntent * mnt)223*722Smuffin mntprtent(FILE *mnttabp, struct mntent *mnt)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate fprintf(mnttabp, "%s %s %s %s %d %d\n",
2260Sstevel@tonic-gate mnt->mnt_fsname,
2270Sstevel@tonic-gate mnt->mnt_dir,
2280Sstevel@tonic-gate mnt->mnt_type,
2290Sstevel@tonic-gate mnt->mnt_opts,
2300Sstevel@tonic-gate mnt->mnt_freq,
2310Sstevel@tonic-gate mnt->mnt_passno);
232*722Smuffin return (0);
2330Sstevel@tonic-gate }
234