Commit | Line | Data |
---|---|---|
4ba29bcc DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
a3c5c1bf | 13 | #ifndef lint |
967ab7fa | 14 | static char sccsid[] = "@(#)mount.c 5.2 (Berkeley) %G%"; |
4ba29bcc | 15 | #endif not lint |
9dc9fb3d BJ |
16 | |
17 | /* | |
943c4275 | 18 | * mount |
9dc9fb3d | 19 | */ |
7a6d69cb SL |
20 | #include <sys/param.h> |
21 | ||
a3c5c1bf SL |
22 | #include <stdio.h> |
23 | #include <fstab.h> | |
7a6d69cb | 24 | #include <mtab.h> |
78b57350 | 25 | #include <errno.h> |
9dc9fb3d | 26 | |
7a6d69cb SL |
27 | #define DNMAX (sizeof (mtab[0].m_dname) - 1) |
28 | #define PNMAX (sizeof (mtab[0].m_path) - 1) | |
9dc9fb3d | 29 | |
7a6d69cb | 30 | struct mtab mtab[NMOUNT]; |
9dc9fb3d | 31 | |
df611cb9 | 32 | int all; |
abab6b8f | 33 | int ro; |
adf52a13 | 34 | int fake; |
df611cb9 | 35 | int verbose; |
7a6d69cb | 36 | char *index(), *rindex(); |
62c2bd36 | 37 | |
9dc9fb3d | 38 | main(argc, argv) |
62c2bd36 BJ |
39 | int argc; |
40 | char **argv; | |
9dc9fb3d | 41 | { |
9dc9fb3d BJ |
42 | register struct mtab *mp; |
43 | register char *np; | |
44 | int mf; | |
7a6d69cb | 45 | char *type = FSTAB_RW; |
9dc9fb3d | 46 | |
9dc9fb3d | 47 | mf = open("/etc/mtab", 0); |
7a6d69cb SL |
48 | read(mf, (char *)mtab, sizeof (mtab)); |
49 | if (argc == 1) { | |
9dc9fb3d | 50 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) |
7a6d69cb SL |
51 | if (mp->m_path[0] != '\0') |
52 | prmtab(mp); | |
9dc9fb3d BJ |
53 | exit(0); |
54 | } | |
df611cb9 BJ |
55 | top: |
56 | if (argc > 1) { | |
62c2bd36 | 57 | if (!strcmp(argv[1], "-a")) { |
df611cb9 BJ |
58 | all++; |
59 | argc--, argv++; | |
60 | goto top; | |
9dc9fb3d | 61 | } |
62c2bd36 | 62 | if (!strcmp(argv[1], "-r")) { |
7a6d69cb | 63 | type = FSTAB_RO; |
62c2bd36 BJ |
64 | argc--, argv++; |
65 | goto top; | |
66 | } | |
df611cb9 BJ |
67 | if (!strcmp(argv[1], "-f")) { |
68 | fake++; | |
69 | argc--, argv++; | |
70 | goto top; | |
9dc9fb3d | 71 | } |
df611cb9 BJ |
72 | if (!strcmp(argv[1], "-v")) { |
73 | verbose++; | |
74 | argc--, argv++; | |
75 | goto top; | |
76 | } | |
77 | } | |
78 | if (all) { | |
62c2bd36 BJ |
79 | struct fstab *fsp; |
80 | ||
df611cb9 BJ |
81 | if (argc > 1) |
82 | goto argcnt; | |
abab6b8f BJ |
83 | close(2); dup(1); |
84 | if (setfsent() == 0) | |
85 | perror(FSTAB), exit(1); | |
7a6d69cb | 86 | while ((fsp = getfsent()) != 0) { |
abab6b8f | 87 | if (strcmp(fsp->fs_file, "/") == 0) |
9dc9fb3d | 88 | continue; |
7a6d69cb SL |
89 | if (strcmp(fsp->fs_type, FSTAB_RO) && |
90 | strcmp(fsp->fs_type, FSTAB_RW) && | |
fa95fc59 | 91 | strcmp(fsp->fs_type, FSTAB_RQ)) |
943c4275 | 92 | continue; |
7a6d69cb | 93 | mountfs(fsp->fs_spec, fsp->fs_file, fsp->fs_type); |
9dc9fb3d | 94 | } |
df611cb9 | 95 | exit(0); |
9dc9fb3d | 96 | } |
7a6d69cb SL |
97 | if (argc == 2) { |
98 | struct fstab *fs; | |
99 | ||
100 | if (setfsent() == 0) | |
101 | perror(FSTAB), exit(1); | |
102 | fs = getfsfile(argv[1]); | |
103 | if (fs == NULL) | |
104 | goto argcnt; | |
967ab7fa MK |
105 | if (strcmp(fs->fs_type, FSTAB_RO) && |
106 | strcmp(fs->fs_type, FSTAB_RW) && | |
107 | strcmp(fs->fs_type, FSTAB_RQ)) | |
108 | goto argcnt; | |
109 | mountfs(fs->fs_spec, fs->fs_file, fs->fs_type); | |
7a6d69cb SL |
110 | exit(0); |
111 | } | |
a3c5c1bf | 112 | if (argc != 3) { |
df611cb9 | 113 | argcnt: |
046739cf | 114 | fprintf(stderr, |
7a6d69cb | 115 | "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ] [ dir ]\n"); |
df611cb9 BJ |
116 | exit(1); |
117 | } | |
7a6d69cb | 118 | mountfs(argv[1], argv[2], type); |
abab6b8f | 119 | } |
9dc9fb3d | 120 | |
7a6d69cb SL |
121 | prmtab(mp) |
122 | register struct mtab *mp; | |
123 | { | |
124 | ||
125 | printf("%s on %s", mp->m_dname, mp->m_path); | |
126 | if (strcmp(mp->m_type, FSTAB_RO) == 0) | |
127 | printf("\t(read-only)"); | |
128 | if (strcmp(mp->m_type, FSTAB_RQ) == 0) | |
129 | printf("\t(with quotas)"); | |
130 | putchar('\n'); | |
131 | } | |
132 | ||
133 | mountfs(spec, name, type) | |
134 | char *spec, *name, *type; | |
9dc9fb3d | 135 | { |
df611cb9 BJ |
136 | register char *np; |
137 | register struct mtab *mp; | |
138 | int mf; | |
9dc9fb3d | 139 | |
7a6d69cb SL |
140 | if (!fake) { |
141 | if (mount(spec, name, strcmp(type, FSTAB_RO) == 0) < 0) { | |
78b57350 SL |
142 | extern int errno; |
143 | char *cp; | |
144 | ||
df611cb9 | 145 | fprintf(stderr, "%s on ", spec); |
78b57350 SL |
146 | switch (errno) { |
147 | ||
148 | case EMFILE: | |
149 | cp = "Mount table full"; | |
150 | break; | |
151 | ||
152 | case EINVAL: | |
153 | cp = "Bogus super block"; | |
154 | break; | |
155 | ||
156 | default: | |
157 | perror(name); | |
158 | return; | |
159 | } | |
160 | fprintf(stderr, "%s: %s\n", name, cp); | |
df611cb9 BJ |
161 | return; |
162 | } | |
7a6d69cb SL |
163 | /* we don't do quotas.... */ |
164 | if (strcmp(type, FSTAB_RQ) == 0) | |
165 | type = FSTAB_RW; | |
df611cb9 | 166 | } |
7a6d69cb | 167 | np = index(spec, '\0'); |
df611cb9 | 168 | while (*--np == '/') |
9dc9fb3d | 169 | *np = '\0'; |
7a6d69cb SL |
170 | np = rindex(spec, '/'); |
171 | if (np) { | |
172 | *np++ = '\0'; | |
173 | spec = np; | |
174 | } | |
df611cb9 | 175 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) |
7a6d69cb | 176 | if (strcmp(mp->m_dname, spec) == 0) |
df611cb9 BJ |
177 | goto replace; |
178 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) | |
7a6d69cb | 179 | if (mp->m_path[0] == '\0') |
df611cb9 BJ |
180 | goto replace; |
181 | return; | |
182 | replace: | |
7a6d69cb SL |
183 | strncpy(mp->m_dname, spec, DNMAX); |
184 | mp->m_dname[DNMAX] = '\0'; | |
185 | strncpy(mp->m_path, name, PNMAX); | |
186 | mp->m_path[PNMAX] = '\0'; | |
187 | strcpy(mp->m_type, type); | |
188 | if (verbose) | |
189 | prmtab(mp); | |
190 | mp = mtab + NMOUNT - 1; | |
191 | while (mp > mtab && mp->m_path[0] == '\0') | |
192 | --mp; | |
df611cb9 | 193 | mf = creat("/etc/mtab", 0644); |
7a6d69cb | 194 | write(mf, (char *)mtab, (mp - mtab + 1) * sizeof (struct mtab)); |
df611cb9 BJ |
195 | close(mf); |
196 | return; | |
9dc9fb3d | 197 | } |