xref: /csrg-svn/sbin/mount/mount.c (revision 12727)
110811Ssam #ifndef lint
2*12727Ssam static char *sccsid = "@(#)mount.c	4.9 (Berkeley) 05/25/83";
310811Ssam #endif
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
810811Ssam #include <stdio.h>
910811Ssam #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);
75*12727Ssam 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW) &&
76*12727Ssam 			    strcmp(fsp->fs_type, FSTAB_RQ))
771414Sbill 				continue;
784460Sroot 			mountfs(fsp->fs_spec, fsp->fs_file, ro);
791057Sbill 		}
804460Sroot 		exit(0);
811057Sbill 	}
8210811Ssam 	if (argc != 3) {
834460Sroot argcnt:
846400Sroot 		fprintf(stderr,
856400Sroot 	    "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ]\n");
864460Sroot 		exit(1);
874460Sroot 	}
884460Sroot 	mountfs(argv[1], argv[2], ro);
891057Sbill }
901057Sbill 
911057Sbill mountfs(spec, name, ro)
924460Sroot 	char *spec, *name;
931057Sbill {
944460Sroot 	register char *np;
954460Sroot 	register struct mtab *mp;
964460Sroot 	int mf;
971057Sbill 
984460Sroot 	if (fake==0) {
994460Sroot 		if (mount(spec, name, ro) < 0) {
1004460Sroot 			fprintf(stderr, "%s on ", spec);
1014460Sroot 			perror(name);
1024460Sroot 			return;
1034460Sroot 		}
1044460Sroot 	}
1054460Sroot 	if (verbose)
1064460Sroot 		fprintf(stderr, "%s on %s%s\n", spec, name,
1074460Sroot 		    ro ? " read only" : "");
1081057Sbill 	np = spec;
1094460Sroot 	while (*np++)
1101057Sbill 		;
1111057Sbill 	np--;
1124460Sroot 	while (*--np == '/')
1131057Sbill 		*np = '\0';
1144460Sroot 	while (np > spec && *--np != '/')
1151057Sbill 		;
1164460Sroot 	if (*np == '/')
1171057Sbill 		np++;
1181057Sbill 	spec = np;
1194460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
1204460Sroot 		if (!strcmp(mp->spec, spec))
1214460Sroot 			goto replace;
1224460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
1234460Sroot 		if (mp->file[0] == 0)
1244460Sroot 			goto replace;
1254460Sroot 	return;
1264460Sroot replace:
1274460Sroot 	for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
1284460Sroot 		if ((*np++ = *spec++) == 0)
1294460Sroot 			spec--;
1304460Sroot 	for (np = mp->file; np < &mp->file[NAMSIZ-1];)
1314460Sroot 		if ((*np++ = *name++) == 0)
1324460Sroot 			name--;
1334460Sroot 	mp = &mtab[NMOUNT];
1344460Sroot 	while ((--mp)->file[0] == 0);
1354460Sroot 	mf = creat("/etc/mtab", 0644);
1364460Sroot 	write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
1374460Sroot 	close(mf);
1384460Sroot 	return;
1391057Sbill }
140