xref: /csrg-svn/sbin/mount/mount.c (revision 12808)
110811Ssam #ifndef lint
2*12808Ssam static char *sccsid = "@(#)mount.c	4.10 (Berkeley) 05/28/83";
310811Ssam #endif
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
8*12808Ssam #include <sys/param.h>
9*12808Ssam 
1010811Ssam #include <stdio.h>
1110811Ssam #include <fstab.h>
12*12808Ssam #include <mtab.h>
131057Sbill 
14*12808Ssam #define	DNMAX	(sizeof (mtab[0].m_dname) - 1)
15*12808Ssam #define	PNMAX	(sizeof (mtab[0].m_path) - 1)
161057Sbill 
17*12808Ssam struct	mtab mtab[NMOUNT];
181057Sbill 
194460Sroot int	all;
201442Sbill int	ro;
214010Sroot int	fake;
224460Sroot int	verbose;
23*12808Ssam char	*index(), *rindex();
245073Sroot 
251057Sbill main(argc, argv)
265073Sroot 	int argc;
275073Sroot 	char **argv;
281057Sbill {
291057Sbill 	register struct mtab *mp;
301057Sbill 	register char *np;
311057Sbill 	int mf;
32*12808Ssam 	char *type = FSTAB_RW;
331057Sbill 
341057Sbill 	mf = open("/etc/mtab", 0);
35*12808Ssam 	read(mf, (char *)mtab, sizeof (mtab));
36*12808Ssam 	if (argc == 1) {
371057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
38*12808Ssam 			if (mp->m_path[0] != '\0')
39*12808Ssam 				prmtab(mp);
401057Sbill 		exit(0);
411057Sbill 	}
424460Sroot top:
434460Sroot 	if (argc > 1) {
445073Sroot 		if (!strcmp(argv[1], "-a")) {
454460Sroot 			all++;
464460Sroot 			argc--, argv++;
474460Sroot 			goto top;
481057Sbill 		}
495073Sroot 		if (!strcmp(argv[1], "-r")) {
50*12808Ssam 			type = FSTAB_RO;
515073Sroot 			argc--, argv++;
525073Sroot 			goto top;
535073Sroot 		}
544460Sroot 		if (!strcmp(argv[1], "-f")) {
554460Sroot 			fake++;
564460Sroot 			argc--, argv++;
574460Sroot 			goto top;
584460Sroot 		}
594460Sroot 		if (!strcmp(argv[1], "-v")) {
604460Sroot 			verbose++;
614460Sroot 			argc--, argv++;
624460Sroot 			goto top;
634460Sroot 		}
641057Sbill 	}
654460Sroot 	if (all) {
665073Sroot 		struct fstab *fsp;
675073Sroot 
684460Sroot 		if (argc > 1)
694460Sroot 			goto argcnt;
701442Sbill 		close(2); dup(1);
711442Sbill 		if (setfsent() == 0)
721442Sbill 			perror(FSTAB), exit(1);
73*12808Ssam 		while ((fsp = getfsent()) != 0) {
741442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
751057Sbill 				continue;
76*12808Ssam 			if (strcmp(fsp->fs_type, FSTAB_RO) &&
77*12808Ssam 			    strcmp(fsp->fs_type, FSTAB_RW) &&
7812727Ssam 			    strcmp(fsp->fs_type, FSTAB_RQ))
791414Sbill 				continue;
80*12808Ssam 			mountfs(fsp->fs_spec, fsp->fs_file, fsp->fs_type);
811057Sbill 		}
824460Sroot 		exit(0);
831057Sbill 	}
84*12808Ssam 	if (argc == 2) {
85*12808Ssam 		struct fstab *fs;
86*12808Ssam 
87*12808Ssam 		if (setfsent() == 0)
88*12808Ssam 			perror(FSTAB), exit(1);
89*12808Ssam 		fs = getfsfile(argv[1]);
90*12808Ssam 		if (fs == NULL)
91*12808Ssam 			goto argcnt;
92*12808Ssam 		mountfs(fs->fs_spec, fs->fs_file, type);
93*12808Ssam 		exit(0);
94*12808Ssam 	}
9510811Ssam 	if (argc != 3) {
964460Sroot argcnt:
976400Sroot 		fprintf(stderr,
98*12808Ssam     "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ] [ dir ]\n");
994460Sroot 		exit(1);
1004460Sroot 	}
101*12808Ssam 	mountfs(argv[1], argv[2], type);
1021057Sbill }
1031057Sbill 
104*12808Ssam prmtab(mp)
105*12808Ssam 	register struct mtab *mp;
1061057Sbill {
107*12808Ssam 
108*12808Ssam 	printf("%s on %s", mp->m_dname, mp->m_path);
109*12808Ssam 	if (strcmp(mp->m_type, FSTAB_RO) == 0)
110*12808Ssam 		printf("\t(read-only)");
111*12808Ssam 	if (strcmp(mp->m_type, FSTAB_RQ) == 0)
112*12808Ssam 		printf("\t(with quotas)");
113*12808Ssam 	putchar('\n');
114*12808Ssam }
115*12808Ssam 
116*12808Ssam mountfs(spec, name, type)
117*12808Ssam 	char *spec, *name, *type;
118*12808Ssam {
1194460Sroot 	register char *np;
1204460Sroot 	register struct mtab *mp;
1214460Sroot 	int mf;
1221057Sbill 
123*12808Ssam 	if (!fake) {
124*12808Ssam 		if (mount(spec, name, strcmp(type, FSTAB_RO) == 0) < 0) {
1254460Sroot 			fprintf(stderr, "%s on ", spec);
1264460Sroot 			perror(name);
1274460Sroot 			return;
1284460Sroot 		}
129*12808Ssam 		/* we don't do quotas.... */
130*12808Ssam 		if (strcmp(type, FSTAB_RQ) == 0)
131*12808Ssam 			type = FSTAB_RW;
1324460Sroot 	}
133*12808Ssam 	np = index(spec, '\0');
1344460Sroot 	while (*--np == '/')
1351057Sbill 		*np = '\0';
136*12808Ssam 	np = rindex(spec, '/');
137*12808Ssam 	if (np) {
138*12808Ssam 		*np++ = '\0';
139*12808Ssam 		spec = np;
140*12808Ssam 	}
1414460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
142*12808Ssam 		if (strcmp(mp->m_dname, spec) == 0)
1434460Sroot 			goto replace;
1444460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
145*12808Ssam 		if (mp->m_path[0] == '\0')
1464460Sroot 			goto replace;
1474460Sroot 	return;
1484460Sroot replace:
149*12808Ssam 	strncpy(mp->m_dname, spec, DNMAX);
150*12808Ssam 	mp->m_dname[DNMAX] = '\0';
151*12808Ssam 	strncpy(mp->m_path, name, PNMAX);
152*12808Ssam 	mp->m_path[PNMAX] = '\0';
153*12808Ssam 	strcpy(mp->m_type, type);
154*12808Ssam 	if (verbose)
155*12808Ssam 		prmtab(mp);
156*12808Ssam 	mp = mtab + NMOUNT - 1;
157*12808Ssam 	while (mp > mtab && mp->m_path[0] == '\0')
158*12808Ssam 		--mp;
1594460Sroot 	mf = creat("/etc/mtab", 0644);
160*12808Ssam 	write(mf, (char *)mtab, (mp - mtab + 1) * sizeof (struct mtab));
1614460Sroot 	close(mf);
1624460Sroot 	return;
1631057Sbill }
164