xref: /csrg-svn/sbin/mount/mount.c (revision 4010)
1*4010Sroot static char *sccsid = "@(#)mount.c	4.4 (Berkeley) 07/12/81";
21057Sbill #include <stdio.h>
31057Sbill #include <fstab.h>
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
81057Sbill 
91057Sbill int	mountall;
101057Sbill #define	NMOUNT	16
111057Sbill #define	NAMSIZ	32
121057Sbill 
131057Sbill struct mtab {
141057Sbill 	char	file[NAMSIZ];
151057Sbill 	char	spec[NAMSIZ];
161057Sbill } mtab[NMOUNT];
171057Sbill 
181442Sbill int	ro;
19*4010Sroot int	fake;
201057Sbill main(argc, argv)
211057Sbill char **argv;
221057Sbill {
231057Sbill 	register struct mtab *mp;
241057Sbill 	register char *np;
251057Sbill 	int mf;
261057Sbill 
271057Sbill 	mountall = 0;
281057Sbill 	mf = open("/etc/mtab", 0);
291057Sbill 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
301057Sbill 	if (argc==1) {
311057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
321057Sbill 			if (mp->file[0])
331057Sbill 				printf("%s on %s\n", mp->spec, mp->file);
341057Sbill 		exit(0);
351057Sbill 	}
361057Sbill 
371057Sbill 	if (argc == 2){
381057Sbill 		if (strcmp(argv[1], "-a") == 0)
391057Sbill 			mountall++;
401057Sbill 		else {
411442Sbill 			fprintf(stdout,"arg count\n");
421057Sbill 			exit(1);
431057Sbill 		}
441057Sbill 	}
45*4010Sroot 	if (!strcmp(argv[1], "-f"))
46*4010Sroot 		fake++, argc--, argv++;
471057Sbill 
481057Sbill 	if (!mountall){
491057Sbill 		ro = 0;
501057Sbill 		if(argc > 3)
511057Sbill 			ro++;
521442Sbill 		if (mountfs(argv[1], argv[2], ro)){
531442Sbill 			perror("mount");
541057Sbill 			exit(1);
551442Sbill 		}
561057Sbill 	} else {
571442Sbill 		struct	fstab	*fsp;
581442Sbill 		close(2); dup(1);
591442Sbill 		if (setfsent() == 0)
601442Sbill 			perror(FSTAB), exit(1);
611442Sbill 		while ( (fsp = getfsent()) != 0){
621442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
631057Sbill 				continue;
641442Sbill 			ro = !strcmp(fsp->fs_type, FSTAB_RO);
651442Sbill 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
661414Sbill 				continue;
671442Sbill 			if (mountfs(fsp->fs_spec, fsp->fs_file, ro))
681442Sbill 				failed(fsp);
691442Sbill 			else
701442Sbill 				succeed(fsp);
711057Sbill 		}
721442Sbill 		endfsent();
731057Sbill 	}
741057Sbill 	exit(0);
751057Sbill }
761442Sbill failed(fsp)
771442Sbill 	register	struct	fstab *fsp;
781442Sbill {
791442Sbill 	extern int errno;
801442Sbill 	extern char *sys_errlist[];
811442Sbill 	int err = errno;
821442Sbill 	printf("Attempt to mount ");
831442Sbill 	location(fsp);
841442Sbill 	printf("FAILED: %s\n", sys_errlist[err]);
851442Sbill }
861442Sbill succeed(fsp)
871442Sbill 	register	struct	fstab *fsp;
881442Sbill {
891442Sbill 	printf("Mounted ");
901442Sbill 	location(fsp);
911442Sbill 	printf("\n");
921442Sbill }
931442Sbill location(fsp)
941442Sbill 	register	struct	fstab *fsp;
951442Sbill {
961442Sbill 	extern	int	ro;
971442Sbill 	printf("%s on %s %s ",
981442Sbill 		fsp->fs_file, fsp->fs_spec,
991442Sbill 		ro ? "(Read Only)" : "");
1001442Sbill }
1011057Sbill 
1021057Sbill mountfs(spec, name, ro)
1031057Sbill 	char	*spec, *name;
1041057Sbill 	int	ro;
1051057Sbill {
1061057Sbill 	register	char	*np;
1071057Sbill 	register	struct	mtab	*mp;
1081057Sbill 	int	mf;
1091057Sbill 
110*4010Sroot 	if(fake==0 && mount(spec, name, ro) < 0)
1111057Sbill 		return(1);
1121057Sbill 	np = spec;
1131057Sbill 	while(*np++)
1141057Sbill 		;
1151057Sbill 	np--;
1161057Sbill 	while(*--np == '/')
1171057Sbill 		*np = '\0';
1181057Sbill 	while(np > spec && *--np != '/')
1191057Sbill 		;
1201057Sbill 	if(*np == '/')
1211057Sbill 		np++;
1221057Sbill 	spec = np;
1231057Sbill 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
1241057Sbill 		if (mp->file[0] == 0) {
1251057Sbill 			for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
1261057Sbill 				if ((*np++ = *spec++) == 0)
1271057Sbill 					spec--;
1281057Sbill 			for (np = mp->file; np < &mp->file[NAMSIZ-1];)
1291057Sbill 				if ((*np++ = *name++) == 0)
1301057Sbill 					name--;
1311057Sbill 			mp = &mtab[NMOUNT];
1321057Sbill 			while ((--mp)->file[0] == 0);
1331057Sbill 			mf = creat("/etc/mtab", 0644);
1341057Sbill 			write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
1351414Sbill 			return(0);
1361057Sbill 		}
1371057Sbill 	}
1381057Sbill 	return(0);
1391057Sbill }
140