xref: /csrg-svn/sbin/mount/mount.c (revision 16669)
110811Ssam #ifndef lint
2*16669Ssam static char *sccsid = "@(#)mount.c	4.11 (Berkeley) 07/05/84";
310811Ssam #endif
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
812808Ssam #include <sys/param.h>
912808Ssam 
1010811Ssam #include <stdio.h>
1110811Ssam #include <fstab.h>
1212808Ssam #include <mtab.h>
13*16669Ssam #include <errno.h>
141057Sbill 
1512808Ssam #define	DNMAX	(sizeof (mtab[0].m_dname) - 1)
1612808Ssam #define	PNMAX	(sizeof (mtab[0].m_path) - 1)
171057Sbill 
1812808Ssam struct	mtab mtab[NMOUNT];
191057Sbill 
204460Sroot int	all;
211442Sbill int	ro;
224010Sroot int	fake;
234460Sroot int	verbose;
2412808Ssam char	*index(), *rindex();
255073Sroot 
261057Sbill main(argc, argv)
275073Sroot 	int argc;
285073Sroot 	char **argv;
291057Sbill {
301057Sbill 	register struct mtab *mp;
311057Sbill 	register char *np;
321057Sbill 	int mf;
3312808Ssam 	char *type = FSTAB_RW;
341057Sbill 
351057Sbill 	mf = open("/etc/mtab", 0);
3612808Ssam 	read(mf, (char *)mtab, sizeof (mtab));
3712808Ssam 	if (argc == 1) {
381057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
3912808Ssam 			if (mp->m_path[0] != '\0')
4012808Ssam 				prmtab(mp);
411057Sbill 		exit(0);
421057Sbill 	}
434460Sroot top:
444460Sroot 	if (argc > 1) {
455073Sroot 		if (!strcmp(argv[1], "-a")) {
464460Sroot 			all++;
474460Sroot 			argc--, argv++;
484460Sroot 			goto top;
491057Sbill 		}
505073Sroot 		if (!strcmp(argv[1], "-r")) {
5112808Ssam 			type = FSTAB_RO;
525073Sroot 			argc--, argv++;
535073Sroot 			goto top;
545073Sroot 		}
554460Sroot 		if (!strcmp(argv[1], "-f")) {
564460Sroot 			fake++;
574460Sroot 			argc--, argv++;
584460Sroot 			goto top;
594460Sroot 		}
604460Sroot 		if (!strcmp(argv[1], "-v")) {
614460Sroot 			verbose++;
624460Sroot 			argc--, argv++;
634460Sroot 			goto top;
644460Sroot 		}
651057Sbill 	}
664460Sroot 	if (all) {
675073Sroot 		struct fstab *fsp;
685073Sroot 
694460Sroot 		if (argc > 1)
704460Sroot 			goto argcnt;
711442Sbill 		close(2); dup(1);
721442Sbill 		if (setfsent() == 0)
731442Sbill 			perror(FSTAB), exit(1);
7412808Ssam 		while ((fsp = getfsent()) != 0) {
751442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
761057Sbill 				continue;
7712808Ssam 			if (strcmp(fsp->fs_type, FSTAB_RO) &&
7812808Ssam 			    strcmp(fsp->fs_type, FSTAB_RW) &&
7912727Ssam 			    strcmp(fsp->fs_type, FSTAB_RQ))
801414Sbill 				continue;
8112808Ssam 			mountfs(fsp->fs_spec, fsp->fs_file, fsp->fs_type);
821057Sbill 		}
834460Sroot 		exit(0);
841057Sbill 	}
8512808Ssam 	if (argc == 2) {
8612808Ssam 		struct fstab *fs;
8712808Ssam 
8812808Ssam 		if (setfsent() == 0)
8912808Ssam 			perror(FSTAB), exit(1);
9012808Ssam 		fs = getfsfile(argv[1]);
9112808Ssam 		if (fs == NULL)
9212808Ssam 			goto argcnt;
9312808Ssam 		mountfs(fs->fs_spec, fs->fs_file, type);
9412808Ssam 		exit(0);
9512808Ssam 	}
9610811Ssam 	if (argc != 3) {
974460Sroot argcnt:
986400Sroot 		fprintf(stderr,
9912808Ssam     "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ] [ dir ]\n");
1004460Sroot 		exit(1);
1014460Sroot 	}
10212808Ssam 	mountfs(argv[1], argv[2], type);
1031057Sbill }
1041057Sbill 
10512808Ssam prmtab(mp)
10612808Ssam 	register struct mtab *mp;
1071057Sbill {
10812808Ssam 
10912808Ssam 	printf("%s on %s", mp->m_dname, mp->m_path);
11012808Ssam 	if (strcmp(mp->m_type, FSTAB_RO) == 0)
11112808Ssam 		printf("\t(read-only)");
11212808Ssam 	if (strcmp(mp->m_type, FSTAB_RQ) == 0)
11312808Ssam 		printf("\t(with quotas)");
11412808Ssam 	putchar('\n');
11512808Ssam }
11612808Ssam 
11712808Ssam mountfs(spec, name, type)
11812808Ssam 	char *spec, *name, *type;
11912808Ssam {
1204460Sroot 	register char *np;
1214460Sroot 	register struct mtab *mp;
1224460Sroot 	int mf;
1231057Sbill 
12412808Ssam 	if (!fake) {
12512808Ssam 		if (mount(spec, name, strcmp(type, FSTAB_RO) == 0) < 0) {
126*16669Ssam 			extern int errno;
127*16669Ssam 			char *cp;
128*16669Ssam 
1294460Sroot 			fprintf(stderr, "%s on ", spec);
130*16669Ssam 			switch (errno) {
131*16669Ssam 
132*16669Ssam 			case EMFILE:
133*16669Ssam 				cp = "Mount table full";
134*16669Ssam 				break;
135*16669Ssam 
136*16669Ssam 			case EINVAL:
137*16669Ssam 				cp = "Bogus super block";
138*16669Ssam 				break;
139*16669Ssam 
140*16669Ssam 			default:
141*16669Ssam 				perror(name);
142*16669Ssam 				return;
143*16669Ssam 			}
144*16669Ssam 			fprintf(stderr, "%s: %s\n", name, cp);
1454460Sroot 			return;
1464460Sroot 		}
14712808Ssam 		/* we don't do quotas.... */
14812808Ssam 		if (strcmp(type, FSTAB_RQ) == 0)
14912808Ssam 			type = FSTAB_RW;
1504460Sroot 	}
15112808Ssam 	np = index(spec, '\0');
1524460Sroot 	while (*--np == '/')
1531057Sbill 		*np = '\0';
15412808Ssam 	np = rindex(spec, '/');
15512808Ssam 	if (np) {
15612808Ssam 		*np++ = '\0';
15712808Ssam 		spec = np;
15812808Ssam 	}
1594460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
16012808Ssam 		if (strcmp(mp->m_dname, spec) == 0)
1614460Sroot 			goto replace;
1624460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
16312808Ssam 		if (mp->m_path[0] == '\0')
1644460Sroot 			goto replace;
1654460Sroot 	return;
1664460Sroot replace:
16712808Ssam 	strncpy(mp->m_dname, spec, DNMAX);
16812808Ssam 	mp->m_dname[DNMAX] = '\0';
16912808Ssam 	strncpy(mp->m_path, name, PNMAX);
17012808Ssam 	mp->m_path[PNMAX] = '\0';
17112808Ssam 	strcpy(mp->m_type, type);
17212808Ssam 	if (verbose)
17312808Ssam 		prmtab(mp);
17412808Ssam 	mp = mtab + NMOUNT - 1;
17512808Ssam 	while (mp > mtab && mp->m_path[0] == '\0')
17612808Ssam 		--mp;
1774460Sroot 	mf = creat("/etc/mtab", 0644);
17812808Ssam 	write(mf, (char *)mtab, (mp - mtab + 1) * sizeof (struct mtab));
1794460Sroot 	close(mf);
1804460Sroot 	return;
1811057Sbill }
182