xref: /csrg-svn/sbin/mount/mount.c (revision 6400)
1*6400Sroot static char *sccsid = "@(#)mount.c	4.7 (Berkeley) 03/31/82";
21057Sbill #include <stdio.h>
31057Sbill #include <fstab.h>
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
81057Sbill 
91057Sbill #define	NMOUNT	16
101057Sbill #define	NAMSIZ	32
111057Sbill 
121057Sbill struct mtab {
131057Sbill 	char	file[NAMSIZ];
141057Sbill 	char	spec[NAMSIZ];
151057Sbill } mtab[NMOUNT];
161057Sbill 
174460Sroot int	all;
181442Sbill int	ro;
194010Sroot int	fake;
204460Sroot int	verbose;
215073Sroot 
221057Sbill main(argc, argv)
235073Sroot 	int argc;
245073Sroot 	char **argv;
251057Sbill {
261057Sbill 	register struct mtab *mp;
271057Sbill 	register char *np;
281057Sbill 	int mf;
291057Sbill 
301057Sbill 	mf = open("/etc/mtab", 0);
311057Sbill 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
321057Sbill 	if (argc==1) {
331057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
341057Sbill 			if (mp->file[0])
351057Sbill 				printf("%s on %s\n", mp->spec, mp->file);
361057Sbill 		exit(0);
371057Sbill 	}
384460Sroot top:
394460Sroot 	if (argc > 1) {
405073Sroot 		if (!strcmp(argv[1], "-a")) {
414460Sroot 			all++;
424460Sroot 			argc--, argv++;
434460Sroot 			goto top;
441057Sbill 		}
455073Sroot 		if (!strcmp(argv[1], "-r")) {
465073Sroot 			ro++;
475073Sroot 			argc--, argv++;
485073Sroot 			goto top;
495073Sroot 		}
504460Sroot 		if (!strcmp(argv[1], "-f")) {
514460Sroot 			fake++;
524460Sroot 			argc--, argv++;
534460Sroot 			goto top;
544460Sroot 		}
554460Sroot 		if (!strcmp(argv[1], "-v")) {
564460Sroot 			verbose++;
574460Sroot 			argc--, argv++;
584460Sroot 			goto top;
594460Sroot 		}
601057Sbill 	}
614460Sroot 	if (all) {
625073Sroot 		struct fstab *fsp;
635073Sroot 
644460Sroot 		if (argc > 1)
654460Sroot 			goto argcnt;
661442Sbill 		close(2); dup(1);
671442Sbill 		if (setfsent() == 0)
681442Sbill 			perror(FSTAB), exit(1);
694460Sroot 		while ( (fsp = getfsent()) != 0) {
701442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
711057Sbill 				continue;
721442Sbill 			ro = !strcmp(fsp->fs_type, FSTAB_RO);
731442Sbill 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
741414Sbill 				continue;
754460Sroot 			mountfs(fsp->fs_spec, fsp->fs_file, ro);
761057Sbill 		}
774460Sroot 		exit(0);
781057Sbill 	}
794460Sroot 	if (argc < 2 || argc > 3) {
804460Sroot argcnt:
81*6400Sroot 		fprintf(stderr,
82*6400Sroot 	    "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ]\n");
834460Sroot 		exit(1);
844460Sroot 	}
854460Sroot 	mountfs(argv[1], argv[2], ro);
861057Sbill }
871057Sbill 
881057Sbill mountfs(spec, name, ro)
894460Sroot 	char *spec, *name;
901057Sbill {
914460Sroot 	register char *np;
924460Sroot 	register struct mtab *mp;
934460Sroot 	int mf;
941057Sbill 
954460Sroot 	if (fake==0) {
964460Sroot 		if (mount(spec, name, ro) < 0) {
974460Sroot 			fprintf(stderr, "%s on ", spec);
984460Sroot 			perror(name);
994460Sroot 			return;
1004460Sroot 		}
1014460Sroot 	}
1024460Sroot 	if (verbose)
1034460Sroot 		fprintf(stderr, "%s on %s%s\n", spec, name,
1044460Sroot 		    ro ? " read only" : "");
1051057Sbill 	np = spec;
1064460Sroot 	while (*np++)
1071057Sbill 		;
1081057Sbill 	np--;
1094460Sroot 	while (*--np == '/')
1101057Sbill 		*np = '\0';
1114460Sroot 	while (np > spec && *--np != '/')
1121057Sbill 		;
1134460Sroot 	if (*np == '/')
1141057Sbill 		np++;
1151057Sbill 	spec = np;
1164460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
1174460Sroot 		if (!strcmp(mp->spec, spec))
1184460Sroot 			goto replace;
1194460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
1204460Sroot 		if (mp->file[0] == 0)
1214460Sroot 			goto replace;
1224460Sroot 	return;
1234460Sroot replace:
1244460Sroot 	for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
1254460Sroot 		if ((*np++ = *spec++) == 0)
1264460Sroot 			spec--;
1274460Sroot 	for (np = mp->file; np < &mp->file[NAMSIZ-1];)
1284460Sroot 		if ((*np++ = *name++) == 0)
1294460Sroot 			name--;
1304460Sroot 	mp = &mtab[NMOUNT];
1314460Sroot 	while ((--mp)->file[0] == 0);
1324460Sroot 	mf = creat("/etc/mtab", 0644);
1334460Sroot 	write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
1344460Sroot 	close(mf);
1354460Sroot 	return;
1361057Sbill }
137