xref: /csrg-svn/sbin/mount/mount.c (revision 1442)
1*1442Sbill static char *sccsid = "@(#)mount.c	4.3 (Berkeley) 10/15/80";
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 
18*1442Sbill int	ro;
191057Sbill main(argc, argv)
201057Sbill char **argv;
211057Sbill {
221057Sbill 	register struct mtab *mp;
231057Sbill 	register char *np;
241057Sbill 	int mf;
251057Sbill 
261057Sbill 	mountall = 0;
271057Sbill 	mf = open("/etc/mtab", 0);
281057Sbill 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
291057Sbill 	if (argc==1) {
301057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
311057Sbill 			if (mp->file[0])
321057Sbill 				printf("%s on %s\n", mp->spec, mp->file);
331057Sbill 		exit(0);
341057Sbill 	}
351057Sbill 
361057Sbill 	if (argc == 2){
371057Sbill 		if (strcmp(argv[1], "-a") == 0)
381057Sbill 			mountall++;
391057Sbill 		else {
40*1442Sbill 			fprintf(stdout,"arg count\n");
411057Sbill 			exit(1);
421057Sbill 		}
431057Sbill 	}
441057Sbill 
451057Sbill 	if (!mountall){
461057Sbill 		ro = 0;
471057Sbill 		if(argc > 3)
481057Sbill 			ro++;
49*1442Sbill 		if (mountfs(argv[1], argv[2], ro)){
50*1442Sbill 			perror("mount");
511057Sbill 			exit(1);
52*1442Sbill 		}
531057Sbill 	} else {
54*1442Sbill 		struct	fstab	*fsp;
55*1442Sbill 		close(2); dup(1);
56*1442Sbill 		if (setfsent() == 0)
57*1442Sbill 			perror(FSTAB), exit(1);
58*1442Sbill 		while ( (fsp = getfsent()) != 0){
59*1442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
601057Sbill 				continue;
61*1442Sbill 			ro = !strcmp(fsp->fs_type, FSTAB_RO);
62*1442Sbill 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
631414Sbill 				continue;
64*1442Sbill 			if (mountfs(fsp->fs_spec, fsp->fs_file, ro))
65*1442Sbill 				failed(fsp);
66*1442Sbill 			else
67*1442Sbill 				succeed(fsp);
681057Sbill 		}
69*1442Sbill 		endfsent();
701057Sbill 	}
711057Sbill 	exit(0);
721057Sbill }
73*1442Sbill failed(fsp)
74*1442Sbill 	register	struct	fstab *fsp;
75*1442Sbill {
76*1442Sbill 	extern int errno;
77*1442Sbill 	extern char *sys_errlist[];
78*1442Sbill 	int err = errno;
79*1442Sbill 	printf("Attempt to mount ");
80*1442Sbill 	location(fsp);
81*1442Sbill 	printf("FAILED: %s\n", sys_errlist[err]);
82*1442Sbill }
83*1442Sbill succeed(fsp)
84*1442Sbill 	register	struct	fstab *fsp;
85*1442Sbill {
86*1442Sbill 	printf("Mounted ");
87*1442Sbill 	location(fsp);
88*1442Sbill 	printf("\n");
89*1442Sbill }
90*1442Sbill location(fsp)
91*1442Sbill 	register	struct	fstab *fsp;
92*1442Sbill {
93*1442Sbill 	extern	int	ro;
94*1442Sbill 	printf("%s on %s %s ",
95*1442Sbill 		fsp->fs_file, fsp->fs_spec,
96*1442Sbill 		ro ? "(Read Only)" : "");
97*1442Sbill }
981057Sbill 
991057Sbill mountfs(spec, name, ro)
1001057Sbill 	char	*spec, *name;
1011057Sbill 	int	ro;
1021057Sbill {
1031057Sbill 	register	char	*np;
1041057Sbill 	register	struct	mtab	*mp;
1051057Sbill 	int	mf;
1061057Sbill 
1071057Sbill 	if(mount(spec, name, ro) < 0) {
1081057Sbill 		return(1);
1091057Sbill 	}
1101057Sbill 	np = spec;
1111057Sbill 	while(*np++)
1121057Sbill 		;
1131057Sbill 	np--;
1141057Sbill 	while(*--np == '/')
1151057Sbill 		*np = '\0';
1161057Sbill 	while(np > spec && *--np != '/')
1171057Sbill 		;
1181057Sbill 	if(*np == '/')
1191057Sbill 		np++;
1201057Sbill 	spec = np;
1211057Sbill 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
1221057Sbill 		if (mp->file[0] == 0) {
1231057Sbill 			for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
1241057Sbill 				if ((*np++ = *spec++) == 0)
1251057Sbill 					spec--;
1261057Sbill 			for (np = mp->file; np < &mp->file[NAMSIZ-1];)
1271057Sbill 				if ((*np++ = *name++) == 0)
1281057Sbill 					name--;
1291057Sbill 			mp = &mtab[NMOUNT];
1301057Sbill 			while ((--mp)->file[0] == 0);
1311057Sbill 			mf = creat("/etc/mtab", 0644);
1321057Sbill 			write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
1331414Sbill 			return(0);
1341057Sbill 		}
1351057Sbill 	}
1361057Sbill 	return(0);
1371057Sbill }
138