xref: /csrg-svn/sbin/mount/mount.c (revision 1414)
1*1414Sbill static char *sccsid = "@(#)mount.c	4.2 (Berkeley) 10/13/80";
21057Sbill #include <stdio.h>
31057Sbill #include <fstab.h>
41057Sbill 
51057Sbill /*
6*1414Sbill  * 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 
181057Sbill main(argc, argv)
191057Sbill char **argv;
201057Sbill {
211057Sbill 	register int ro;
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 {
401057Sbill 			fprintf(stderr,"arg count\n");
411057Sbill 			exit(1);
421057Sbill 		}
431057Sbill 	}
441057Sbill 
451057Sbill 	if (!mountall){
461057Sbill 		ro = 0;
471057Sbill 		if(argc > 3)
481057Sbill 			ro++;
491057Sbill 		if (mountfs(argv[1], argv[2], ro))
501057Sbill 			exit(1);
511057Sbill 	} else {
521057Sbill 		FILE	*fs_file;
531057Sbill 		struct	fstab	fs;
54*1414Sbill 		if ((fs_file = fopen(FSTAB, "r")) == NULL){
551057Sbill 			perror(FSTAB);
561057Sbill 			exit(1);
571057Sbill 		}
581057Sbill 		while (!feof(fs_file)){
59*1414Sbill 			int ro;
601057Sbill 			fscanf(fs_file, FSTABFMT, FSTABARG(&fs));
611057Sbill 			if (strcmp(fs.fs_file, "/") == 0)
621057Sbill 				continue;
63*1414Sbill 			ro = !strcmp(fs.fs_type, "ro");
64*1414Sbill 			if (ro==0 && strcmp(fs.fs_type, "rw"))
65*1414Sbill 				continue;
661057Sbill 			fprintf(stderr, "Mounting %s on %s %s",
671057Sbill 				fs.fs_file, fs.fs_spec,
68*1414Sbill 				ro ? "(Read Only)\n" : "\n");
69*1414Sbill 			mountfs(fs.fs_spec, fs.fs_file, ro);
701057Sbill 		}
711057Sbill 		fclose(fs_file);
721057Sbill 	}
731057Sbill 	exit(0);
741057Sbill }
751057Sbill 
761057Sbill mountfs(spec, name, ro)
771057Sbill 	char	*spec, *name;
781057Sbill 	int	ro;
791057Sbill {
801057Sbill 	register	char	*np;
811057Sbill 	register	struct	mtab	*mp;
821057Sbill 	int	mf;
831057Sbill 
841057Sbill 	if(mount(spec, name, ro) < 0) {
851057Sbill 		perror("mount");
861057Sbill 		return(1);
871057Sbill 	}
881057Sbill 	np = spec;
891057Sbill 	while(*np++)
901057Sbill 		;
911057Sbill 	np--;
921057Sbill 	while(*--np == '/')
931057Sbill 		*np = '\0';
941057Sbill 	while(np > spec && *--np != '/')
951057Sbill 		;
961057Sbill 	if(*np == '/')
971057Sbill 		np++;
981057Sbill 	spec = np;
991057Sbill 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
1001057Sbill 		if (mp->file[0] == 0) {
1011057Sbill 			for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
1021057Sbill 				if ((*np++ = *spec++) == 0)
1031057Sbill 					spec--;
1041057Sbill 			for (np = mp->file; np < &mp->file[NAMSIZ-1];)
1051057Sbill 				if ((*np++ = *name++) == 0)
1061057Sbill 					name--;
1071057Sbill 			mp = &mtab[NMOUNT];
1081057Sbill 			while ((--mp)->file[0] == 0);
1091057Sbill 			mf = creat("/etc/mtab", 0644);
1101057Sbill 			write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
111*1414Sbill 			return(0);
1121057Sbill 		}
1131057Sbill 	}
1141057Sbill 	return(0);
1151057Sbill }
116