xref: /csrg-svn/sbin/mount/mount.c (revision 10811)
1*10811Ssam #ifndef lint
2*10811Ssam static char *sccsid = "@(#)mount.c	4.8 (Berkeley) 02/09/83";
3*10811Ssam #endif
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
8*10811Ssam #include <stdio.h>
9*10811Ssam #include <fstab.h>
101057Sbill 
111057Sbill #define	NMOUNT	16
121057Sbill #define	NAMSIZ	32
131057Sbill 
141057Sbill struct mtab {
151057Sbill 	char	file[NAMSIZ];
161057Sbill 	char	spec[NAMSIZ];
171057Sbill } mtab[NMOUNT];
181057Sbill 
194460Sroot int	all;
201442Sbill int	ro;
214010Sroot int	fake;
224460Sroot int	verbose;
235073Sroot 
241057Sbill main(argc, argv)
255073Sroot 	int argc;
265073Sroot 	char **argv;
271057Sbill {
281057Sbill 	register struct mtab *mp;
291057Sbill 	register char *np;
301057Sbill 	int mf;
311057Sbill 
321057Sbill 	mf = open("/etc/mtab", 0);
331057Sbill 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
341057Sbill 	if (argc==1) {
351057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
361057Sbill 			if (mp->file[0])
371057Sbill 				printf("%s on %s\n", mp->spec, mp->file);
381057Sbill 		exit(0);
391057Sbill 	}
404460Sroot top:
414460Sroot 	if (argc > 1) {
425073Sroot 		if (!strcmp(argv[1], "-a")) {
434460Sroot 			all++;
444460Sroot 			argc--, argv++;
454460Sroot 			goto top;
461057Sbill 		}
475073Sroot 		if (!strcmp(argv[1], "-r")) {
485073Sroot 			ro++;
495073Sroot 			argc--, argv++;
505073Sroot 			goto top;
515073Sroot 		}
524460Sroot 		if (!strcmp(argv[1], "-f")) {
534460Sroot 			fake++;
544460Sroot 			argc--, argv++;
554460Sroot 			goto top;
564460Sroot 		}
574460Sroot 		if (!strcmp(argv[1], "-v")) {
584460Sroot 			verbose++;
594460Sroot 			argc--, argv++;
604460Sroot 			goto top;
614460Sroot 		}
621057Sbill 	}
634460Sroot 	if (all) {
645073Sroot 		struct fstab *fsp;
655073Sroot 
664460Sroot 		if (argc > 1)
674460Sroot 			goto argcnt;
681442Sbill 		close(2); dup(1);
691442Sbill 		if (setfsent() == 0)
701442Sbill 			perror(FSTAB), exit(1);
714460Sroot 		while ( (fsp = getfsent()) != 0) {
721442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
731057Sbill 				continue;
741442Sbill 			ro = !strcmp(fsp->fs_type, FSTAB_RO);
751442Sbill 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
761414Sbill 				continue;
774460Sroot 			mountfs(fsp->fs_spec, fsp->fs_file, ro);
781057Sbill 		}
794460Sroot 		exit(0);
801057Sbill 	}
81*10811Ssam 	if (argc != 3) {
824460Sroot argcnt:
836400Sroot 		fprintf(stderr,
846400Sroot 	    "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ]\n");
854460Sroot 		exit(1);
864460Sroot 	}
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