Commit | Line | Data |
---|---|---|
046739cf | 1 | static char *sccsid = "@(#)mount.c 4.7 (Berkeley) %G%"; |
9dc9fb3d BJ |
2 | #include <stdio.h> |
3 | #include <fstab.h> | |
4 | ||
5 | /* | |
943c4275 | 6 | * mount |
9dc9fb3d BJ |
7 | */ |
8 | ||
9dc9fb3d BJ |
9 | #define NMOUNT 16 |
10 | #define NAMSIZ 32 | |
11 | ||
12 | struct mtab { | |
13 | char file[NAMSIZ]; | |
14 | char spec[NAMSIZ]; | |
15 | } mtab[NMOUNT]; | |
16 | ||
df611cb9 | 17 | int all; |
abab6b8f | 18 | int ro; |
adf52a13 | 19 | int fake; |
df611cb9 | 20 | int verbose; |
62c2bd36 | 21 | |
9dc9fb3d | 22 | main(argc, argv) |
62c2bd36 BJ |
23 | int argc; |
24 | char **argv; | |
9dc9fb3d | 25 | { |
9dc9fb3d BJ |
26 | register struct mtab *mp; |
27 | register char *np; | |
28 | int mf; | |
29 | ||
9dc9fb3d BJ |
30 | mf = open("/etc/mtab", 0); |
31 | read(mf, (char *)mtab, NMOUNT*2*NAMSIZ); | |
32 | if (argc==1) { | |
33 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) | |
34 | if (mp->file[0]) | |
35 | printf("%s on %s\n", mp->spec, mp->file); | |
36 | exit(0); | |
37 | } | |
df611cb9 BJ |
38 | top: |
39 | if (argc > 1) { | |
62c2bd36 | 40 | if (!strcmp(argv[1], "-a")) { |
df611cb9 BJ |
41 | all++; |
42 | argc--, argv++; | |
43 | goto top; | |
9dc9fb3d | 44 | } |
62c2bd36 BJ |
45 | if (!strcmp(argv[1], "-r")) { |
46 | ro++; | |
47 | argc--, argv++; | |
48 | goto top; | |
49 | } | |
df611cb9 BJ |
50 | if (!strcmp(argv[1], "-f")) { |
51 | fake++; | |
52 | argc--, argv++; | |
53 | goto top; | |
9dc9fb3d | 54 | } |
df611cb9 BJ |
55 | if (!strcmp(argv[1], "-v")) { |
56 | verbose++; | |
57 | argc--, argv++; | |
58 | goto top; | |
59 | } | |
60 | } | |
61 | if (all) { | |
62c2bd36 BJ |
62 | struct fstab *fsp; |
63 | ||
df611cb9 BJ |
64 | if (argc > 1) |
65 | goto argcnt; | |
abab6b8f BJ |
66 | close(2); dup(1); |
67 | if (setfsent() == 0) | |
68 | perror(FSTAB), exit(1); | |
df611cb9 | 69 | while ( (fsp = getfsent()) != 0) { |
abab6b8f | 70 | if (strcmp(fsp->fs_file, "/") == 0) |
9dc9fb3d | 71 | continue; |
abab6b8f BJ |
72 | ro = !strcmp(fsp->fs_type, FSTAB_RO); |
73 | if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW)) | |
943c4275 | 74 | continue; |
df611cb9 | 75 | mountfs(fsp->fs_spec, fsp->fs_file, ro); |
9dc9fb3d | 76 | } |
df611cb9 | 77 | exit(0); |
9dc9fb3d | 78 | } |
df611cb9 BJ |
79 | if (argc < 2 || argc > 3) { |
80 | argcnt: | |
046739cf BJ |
81 | fprintf(stderr, |
82 | "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ]\n"); | |
df611cb9 BJ |
83 | exit(1); |
84 | } | |
df611cb9 | 85 | mountfs(argv[1], argv[2], ro); |
abab6b8f | 86 | } |
9dc9fb3d BJ |
87 | |
88 | mountfs(spec, name, ro) | |
df611cb9 | 89 | char *spec, *name; |
9dc9fb3d | 90 | { |
df611cb9 BJ |
91 | register char *np; |
92 | register struct mtab *mp; | |
93 | int mf; | |
9dc9fb3d | 94 | |
df611cb9 BJ |
95 | if (fake==0) { |
96 | if (mount(spec, name, ro) < 0) { | |
97 | fprintf(stderr, "%s on ", spec); | |
98 | perror(name); | |
99 | return; | |
100 | } | |
101 | } | |
102 | if (verbose) | |
103 | fprintf(stderr, "%s on %s%s\n", spec, name, | |
104 | ro ? " read only" : ""); | |
9dc9fb3d | 105 | np = spec; |
df611cb9 | 106 | while (*np++) |
9dc9fb3d BJ |
107 | ; |
108 | np--; | |
df611cb9 | 109 | while (*--np == '/') |
9dc9fb3d | 110 | *np = '\0'; |
df611cb9 | 111 | while (np > spec && *--np != '/') |
9dc9fb3d | 112 | ; |
df611cb9 | 113 | if (*np == '/') |
9dc9fb3d BJ |
114 | np++; |
115 | spec = np; | |
df611cb9 BJ |
116 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) |
117 | if (!strcmp(mp->spec, spec)) | |
118 | goto replace; | |
119 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) | |
120 | if (mp->file[0] == 0) | |
121 | goto replace; | |
122 | return; | |
123 | replace: | |
124 | for (np = mp->spec; np < &mp->spec[NAMSIZ-1];) | |
125 | if ((*np++ = *spec++) == 0) | |
126 | spec--; | |
127 | for (np = mp->file; np < &mp->file[NAMSIZ-1];) | |
128 | if ((*np++ = *name++) == 0) | |
129 | name--; | |
130 | mp = &mtab[NMOUNT]; | |
131 | while ((--mp)->file[0] == 0); | |
132 | mf = creat("/etc/mtab", 0644); | |
133 | write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ); | |
134 | close(mf); | |
135 | return; | |
9dc9fb3d | 136 | } |