xref: /csrg-svn/sbin/mount/mount.c (revision 21156)
1*21156Sdist /*
2*21156Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21156Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21156Sdist  * specifies the terms and conditions for redistribution.
5*21156Sdist  */
6*21156Sdist 
710811Ssam #ifndef lint
8*21156Sdist char copyright[] =
9*21156Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21156Sdist  All rights reserved.\n";
11*21156Sdist #endif not lint
121057Sbill 
13*21156Sdist #ifndef lint
14*21156Sdist static char sccsid[] = "@(#)mount.c	5.1 (Berkeley) 05/28/85";
15*21156Sdist #endif not lint
16*21156Sdist 
171057Sbill /*
181414Sbill  * mount
191057Sbill  */
2012808Ssam #include <sys/param.h>
2112808Ssam 
2210811Ssam #include <stdio.h>
2310811Ssam #include <fstab.h>
2412808Ssam #include <mtab.h>
2516669Ssam #include <errno.h>
261057Sbill 
2712808Ssam #define	DNMAX	(sizeof (mtab[0].m_dname) - 1)
2812808Ssam #define	PNMAX	(sizeof (mtab[0].m_path) - 1)
291057Sbill 
3012808Ssam struct	mtab mtab[NMOUNT];
311057Sbill 
324460Sroot int	all;
331442Sbill int	ro;
344010Sroot int	fake;
354460Sroot int	verbose;
3612808Ssam char	*index(), *rindex();
375073Sroot 
381057Sbill main(argc, argv)
395073Sroot 	int argc;
405073Sroot 	char **argv;
411057Sbill {
421057Sbill 	register struct mtab *mp;
431057Sbill 	register char *np;
441057Sbill 	int mf;
4512808Ssam 	char *type = FSTAB_RW;
461057Sbill 
471057Sbill 	mf = open("/etc/mtab", 0);
4812808Ssam 	read(mf, (char *)mtab, sizeof (mtab));
4912808Ssam 	if (argc == 1) {
501057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
5112808Ssam 			if (mp->m_path[0] != '\0')
5212808Ssam 				prmtab(mp);
531057Sbill 		exit(0);
541057Sbill 	}
554460Sroot top:
564460Sroot 	if (argc > 1) {
575073Sroot 		if (!strcmp(argv[1], "-a")) {
584460Sroot 			all++;
594460Sroot 			argc--, argv++;
604460Sroot 			goto top;
611057Sbill 		}
625073Sroot 		if (!strcmp(argv[1], "-r")) {
6312808Ssam 			type = FSTAB_RO;
645073Sroot 			argc--, argv++;
655073Sroot 			goto top;
665073Sroot 		}
674460Sroot 		if (!strcmp(argv[1], "-f")) {
684460Sroot 			fake++;
694460Sroot 			argc--, argv++;
704460Sroot 			goto top;
714460Sroot 		}
724460Sroot 		if (!strcmp(argv[1], "-v")) {
734460Sroot 			verbose++;
744460Sroot 			argc--, argv++;
754460Sroot 			goto top;
764460Sroot 		}
771057Sbill 	}
784460Sroot 	if (all) {
795073Sroot 		struct fstab *fsp;
805073Sroot 
814460Sroot 		if (argc > 1)
824460Sroot 			goto argcnt;
831442Sbill 		close(2); dup(1);
841442Sbill 		if (setfsent() == 0)
851442Sbill 			perror(FSTAB), exit(1);
8612808Ssam 		while ((fsp = getfsent()) != 0) {
871442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
881057Sbill 				continue;
8912808Ssam 			if (strcmp(fsp->fs_type, FSTAB_RO) &&
9012808Ssam 			    strcmp(fsp->fs_type, FSTAB_RW) &&
9112727Ssam 			    strcmp(fsp->fs_type, FSTAB_RQ))
921414Sbill 				continue;
9312808Ssam 			mountfs(fsp->fs_spec, fsp->fs_file, fsp->fs_type);
941057Sbill 		}
954460Sroot 		exit(0);
961057Sbill 	}
9712808Ssam 	if (argc == 2) {
9812808Ssam 		struct fstab *fs;
9912808Ssam 
10012808Ssam 		if (setfsent() == 0)
10112808Ssam 			perror(FSTAB), exit(1);
10212808Ssam 		fs = getfsfile(argv[1]);
10312808Ssam 		if (fs == NULL)
10412808Ssam 			goto argcnt;
10512808Ssam 		mountfs(fs->fs_spec, fs->fs_file, type);
10612808Ssam 		exit(0);
10712808Ssam 	}
10810811Ssam 	if (argc != 3) {
1094460Sroot argcnt:
1106400Sroot 		fprintf(stderr,
11112808Ssam     "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ] [ dir ]\n");
1124460Sroot 		exit(1);
1134460Sroot 	}
11412808Ssam 	mountfs(argv[1], argv[2], type);
1151057Sbill }
1161057Sbill 
11712808Ssam prmtab(mp)
11812808Ssam 	register struct mtab *mp;
1191057Sbill {
12012808Ssam 
12112808Ssam 	printf("%s on %s", mp->m_dname, mp->m_path);
12212808Ssam 	if (strcmp(mp->m_type, FSTAB_RO) == 0)
12312808Ssam 		printf("\t(read-only)");
12412808Ssam 	if (strcmp(mp->m_type, FSTAB_RQ) == 0)
12512808Ssam 		printf("\t(with quotas)");
12612808Ssam 	putchar('\n');
12712808Ssam }
12812808Ssam 
12912808Ssam mountfs(spec, name, type)
13012808Ssam 	char *spec, *name, *type;
13112808Ssam {
1324460Sroot 	register char *np;
1334460Sroot 	register struct mtab *mp;
1344460Sroot 	int mf;
1351057Sbill 
13612808Ssam 	if (!fake) {
13712808Ssam 		if (mount(spec, name, strcmp(type, FSTAB_RO) == 0) < 0) {
13816669Ssam 			extern int errno;
13916669Ssam 			char *cp;
14016669Ssam 
1414460Sroot 			fprintf(stderr, "%s on ", spec);
14216669Ssam 			switch (errno) {
14316669Ssam 
14416669Ssam 			case EMFILE:
14516669Ssam 				cp = "Mount table full";
14616669Ssam 				break;
14716669Ssam 
14816669Ssam 			case EINVAL:
14916669Ssam 				cp = "Bogus super block";
15016669Ssam 				break;
15116669Ssam 
15216669Ssam 			default:
15316669Ssam 				perror(name);
15416669Ssam 				return;
15516669Ssam 			}
15616669Ssam 			fprintf(stderr, "%s: %s\n", name, cp);
1574460Sroot 			return;
1584460Sroot 		}
15912808Ssam 		/* we don't do quotas.... */
16012808Ssam 		if (strcmp(type, FSTAB_RQ) == 0)
16112808Ssam 			type = FSTAB_RW;
1624460Sroot 	}
16312808Ssam 	np = index(spec, '\0');
1644460Sroot 	while (*--np == '/')
1651057Sbill 		*np = '\0';
16612808Ssam 	np = rindex(spec, '/');
16712808Ssam 	if (np) {
16812808Ssam 		*np++ = '\0';
16912808Ssam 		spec = np;
17012808Ssam 	}
1714460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
17212808Ssam 		if (strcmp(mp->m_dname, spec) == 0)
1734460Sroot 			goto replace;
1744460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
17512808Ssam 		if (mp->m_path[0] == '\0')
1764460Sroot 			goto replace;
1774460Sroot 	return;
1784460Sroot replace:
17912808Ssam 	strncpy(mp->m_dname, spec, DNMAX);
18012808Ssam 	mp->m_dname[DNMAX] = '\0';
18112808Ssam 	strncpy(mp->m_path, name, PNMAX);
18212808Ssam 	mp->m_path[PNMAX] = '\0';
18312808Ssam 	strcpy(mp->m_type, type);
18412808Ssam 	if (verbose)
18512808Ssam 		prmtab(mp);
18612808Ssam 	mp = mtab + NMOUNT - 1;
18712808Ssam 	while (mp > mtab && mp->m_path[0] == '\0')
18812808Ssam 		--mp;
1894460Sroot 	mf = creat("/etc/mtab", 0644);
19012808Ssam 	write(mf, (char *)mtab, (mp - mtab + 1) * sizeof (struct mtab));
1914460Sroot 	close(mf);
1924460Sroot 	return;
1931057Sbill }
194