summaryrefslogtreecommitdiff
path: root/minix/commands/mount/mount.c
blob: eafa6681d31a7cd9cc70ce763d7d955e2a7f1bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* mount - mount a file system		Author: Andy Tanenbaum */

#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <unistd.h>
#include <minix/minlib.h>
#include <stdio.h>
#include <fstab.h>

#define MINIX_FS_TYPE "mfs"

int main(int argc, char **argv);
void list(void);
void usage(void);
void update_mtab(char *dev, char *mountpoint, char *fstype, int mountflags);
int mount_all(void);

static int write_mtab = 1;

int main(argc, argv)
int argc;
char *argv[];
{
  int all = 0, i, v = 0, mountflags, srvflags;
  char **ap, *opt, *err, *type, *args, *device;

  if (argc == 1) list();	/* just list /etc/mtab */
  mountflags = 0;
  srvflags = 0;
  type = NULL;
  args = NULL;
  ap = argv+1;
  for (i = 1; i < argc; i++) {
	if (argv[i][0] == '-') {
		opt = argv[i]+1;
		while (*opt != 0) switch (*opt++) {
		case 'r':	mountflags |= MNT_RDONLY;	break;
		case 't':	if (++i == argc) usage();
				type = argv[i];
				break;
		case 'i':	srvflags |= MS_REUSE;		break;
		case 'e':	srvflags |= MS_EXISTING;		break;
		case 'n':	write_mtab = 0;			break;
		case 'o':	if (++i == argc) usage();
				args = argv[i];
				break;
		case 'a':	all = 1; break;
		default:	usage();
		}
	} else {
		*ap++ = argv[i];
	}
  }
  *ap = NULL;
  argc = (ap - argv);

  if (!all && (argc != 3 || *argv[1] == 0)) usage();
  if (all == 1) {
	return mount_all();
  }

  device = argv[1];
  if (!strcmp(device, "none")) device = NULL;

  if ((type == NULL || !strcmp(type, MINIX_FS_TYPE)) && device != NULL) {
	/* auto-detect type and/or version */
	v = fsversion(device, "mount");
	switch (v) {
		case FSVERSION_MFS1:
		case FSVERSION_MFS2:
		case FSVERSION_MFS3: type = MINIX_FS_TYPE; break;
		case FSVERSION_EXT2: type = "ext2"; break;
		case FSVERSION_ISO9660: type = "isofs"; break;
	}
  }

  if (minix_mount(device, argv[2], mountflags, srvflags, type, args) < 0) {
	err = strerror(errno);
	fprintf(stderr, "mount: Can't mount %s on %s: %s\n",
		argv[1], argv[2], err);
	return(EXIT_FAILURE);
  }

  printf("%s is mounted on %s\n", argv[1], argv[2]);
  return(EXIT_SUCCESS);
}

void list()
{
  int n;
  char dev[PATH_MAX], mountpoint[PATH_MAX], type[MNTNAMELEN], flags[MNTFLAGLEN];

  /* Read and print /etc/mtab. */
  n = load_mtab("mount");
  if (n < 0) exit(1);

  while (1) {
	n = get_mtab_entry(dev, mountpoint, type, flags);
	if  (n < 0) break;
	printf("%s on %s type %s (%s)\n", dev, mountpoint, type, flags);
  }
  exit(0);
}

int
has_opt(char *mntopts, char *option)
{
	char *optbuf, *opt;
	int found = 0;

	optbuf = strdup(mntopts);
	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
		if (!strcmp(opt, option)) found = 1;
	}
	free (optbuf);
	return(found);
}


int
mount_all()
{
	struct fstab *fs;
	int ro, mountflags;
	char mountpoint[PATH_MAX];
  	char *device, *err;

	while ((fs = getfsent()) != NULL) {
		ro = 0;
		mountflags = 0;
		device = NULL;
		if (realpath(fs->fs_file, mountpoint) == NULL) {
			fprintf(stderr, "Can't mount on %s\n", fs->fs_file);
			return(EXIT_FAILURE);
		}
		if (has_opt(fs->fs_mntops, "noauto"))
			continue;
		if (!strcmp(mountpoint, "/"))
			continue; /* Not remounting root */
		if (has_opt(fs->fs_mntops, "ro"))
			ro = 1;
		if (ro) {
			mountflags |= MNT_RDONLY;
		}

		device = fs->fs_spec;
		/* passing a null string for block special device means don't
		 * use a device at all and this is what we need to do for
		 * entries starting with "none"
		 */
		if (!strcmp(device, "none"))
			device = NULL;

		if (minix_mount(device, mountpoint, mountflags, 0, fs->fs_vfstype,
		    fs->fs_mntops) != 0) {
			err = strerror(errno);
			fprintf(stderr, "mount: Can't mount %s on %s: %s\n",
				fs->fs_spec, fs->fs_file, err);
			return(EXIT_FAILURE);
		}
	}
	return(EXIT_SUCCESS);
}

void usage()
{
  std_err("Usage: mount [-a] [-r] [-e] [-t type] [-o options] special name\n");
  exit(1);
}