xref: /original-bsd/sbin/mount/mount.c (revision 552e81d8)
1 static char *sccsid = "@(#)mount.c	4.1 (Berkeley) 10/01/80";
2 #include <stdio.h>
3 #include <fstab.h>
4 
5 /*
6  *	Mount file systems.
7  *
8  *	mount -a	Mount all file systems, as determined from the
9  *			file /etc/fstab.
10  *	If the name entry in /etc/fstab is "/", don't mount.
11  *	If the read only entry in /etc/fstab is "ro", mount read only
12  *	The special file names in /etc/fstab are the block devices;
13  *		this is what we want to mount.
14  *	Tries to mount all of the files in /etc/fstab.
15  *
16  *	mount special name	Mount special on name
17  *	mount special name -r	Mount special on name, read/write
18  */
19 
20 int	mountall;
21 #define	NMOUNT	16
22 #define	NAMSIZ	32
23 
24 struct mtab {
25 	char	file[NAMSIZ];
26 	char	spec[NAMSIZ];
27 } mtab[NMOUNT];
28 
29 main(argc, argv)
30 char **argv;
31 {
32 	register int ro;
33 	register struct mtab *mp;
34 	register char *np;
35 	int mf;
36 
37 	mountall = 0;
38 	mf = open("/etc/mtab", 0);
39 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
40 	if (argc==1) {
41 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
42 			if (mp->file[0])
43 				printf("%s on %s\n", mp->spec, mp->file);
44 		exit(0);
45 	}
46 
47 	if (argc == 2){
48 		if (strcmp(argv[1], "-a") == 0)
49 			mountall++;
50 		else {
51 			fprintf(stderr,"arg count\n");
52 			exit(1);
53 		}
54 	}
55 
56 	if (!mountall){
57 		ro = 0;
58 		if(argc > 3)
59 			ro++;
60 		if (mountfs(argv[1], argv[2], ro))
61 			exit(1);
62 	} else {
63 		FILE	*fs_file;
64 		struct	fstab	fs;
65 		if ( (fs_file = fopen(FSTAB, "r")) == NULL){
66 			perror(FSTAB);
67 			exit(1);
68 		}
69 		while (!feof(fs_file)){
70 			fscanf(fs_file, FSTABFMT, FSTABARG(&fs));
71 			if (strcmp(fs.fs_file, "/") == 0)
72 				continue;
73 			fprintf(stderr, "Mounting %s on %s %s",
74 				fs.fs_file, fs.fs_spec,
75 				FSRO(&fs) ? "(Read Only)\n" : "\n");
76 			mountfs(fs.fs_spec, fs.fs_file, FSRO(&fs));
77 		}
78 		fclose(fs_file);
79 	}
80 	exit(0);
81 }
82 
83 mountfs(spec, name, ro)
84 	char	*spec, *name;
85 	int	ro;
86 {
87 	register	char	*np;
88 	register	struct	mtab	*mp;
89 	int	mf;
90 
91 	if(mount(spec, name, ro) < 0) {
92 		perror("mount");
93 		return(1);
94 	}
95 	np = spec;
96 	while(*np++)
97 		;
98 	np--;
99 	while(*--np == '/')
100 		*np = '\0';
101 	while(np > spec && *--np != '/')
102 		;
103 	if(*np == '/')
104 		np++;
105 	spec = np;
106 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
107 		if (mp->file[0] == 0) {
108 			for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
109 				if ((*np++ = *spec++) == 0)
110 					spec--;
111 			for (np = mp->file; np < &mp->file[NAMSIZ-1];)
112 				if ((*np++ = *name++) == 0)
113 					name--;
114 			mp = &mtab[NMOUNT];
115 			while ((--mp)->file[0] == 0);
116 			mf = creat("/etc/mtab", 0644);
117 			write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
118 		}
119 	}
120 	return(0);
121 }
122