xref: /csrg-svn/sbin/mount/mount.c (revision 5073)
1*5073Sroot static char *sccsid = "@(#)mount.c	4.6 (Berkeley) 11/25/81";
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;
21*5073Sroot 
221057Sbill main(argc, argv)
23*5073Sroot 	int argc;
24*5073Sroot 	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) {
40*5073Sroot 		if (!strcmp(argv[1], "-a")) {
414460Sroot 			all++;
424460Sroot 			argc--, argv++;
434460Sroot 			goto top;
441057Sbill 		}
45*5073Sroot 		if (!strcmp(argv[1], "-r")) {
46*5073Sroot 			ro++;
47*5073Sroot 			argc--, argv++;
48*5073Sroot 			goto top;
49*5073Sroot 		}
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) {
62*5073Sroot 		struct fstab *fsp;
63*5073Sroot 
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:
814460Sroot 		printf("arg count\n");
824460Sroot 		exit(1);
834460Sroot 	}
844460Sroot 	ro = 0;
854460Sroot 	if(argc > 3)
864460Sroot 		ro++;
874460Sroot 	mountfs(argv[1], argv[2], ro);
881057Sbill }
891057Sbill 
901057Sbill mountfs(spec, name, ro)
914460Sroot 	char *spec, *name;
921057Sbill {
934460Sroot 	register char *np;
944460Sroot 	register struct mtab *mp;
954460Sroot 	int mf;
961057Sbill 
974460Sroot 	if (fake==0) {
984460Sroot 		if (mount(spec, name, ro) < 0) {
994460Sroot 			fprintf(stderr, "%s on ", spec);
1004460Sroot 			perror(name);
1014460Sroot 			return;
1024460Sroot 		}
1034460Sroot 	}
1044460Sroot 	if (verbose)
1054460Sroot 		fprintf(stderr, "%s on %s%s\n", spec, name,
1064460Sroot 		    ro ? " read only" : "");
1071057Sbill 	np = spec;
1084460Sroot 	while (*np++)
1091057Sbill 		;
1101057Sbill 	np--;
1114460Sroot 	while (*--np == '/')
1121057Sbill 		*np = '\0';
1134460Sroot 	while (np > spec && *--np != '/')
1141057Sbill 		;
1154460Sroot 	if (*np == '/')
1161057Sbill 		np++;
1171057Sbill 	spec = np;
1184460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
1194460Sroot 		if (!strcmp(mp->spec, spec))
1204460Sroot 			goto replace;
1214460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
1224460Sroot 		if (mp->file[0] == 0)
1234460Sroot 			goto replace;
1244460Sroot 	return;
1254460Sroot replace:
1264460Sroot 	for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
1274460Sroot 		if ((*np++ = *spec++) == 0)
1284460Sroot 			spec--;
1294460Sroot 	for (np = mp->file; np < &mp->file[NAMSIZ-1];)
1304460Sroot 		if ((*np++ = *name++) == 0)
1314460Sroot 			name--;
1324460Sroot 	mp = &mtab[NMOUNT];
1334460Sroot 	while ((--mp)->file[0] == 0);
1344460Sroot 	mf = creat("/etc/mtab", 0644);
1354460Sroot 	write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
1364460Sroot 	close(mf);
1374460Sroot 	return;
1381057Sbill }
139