Files
haiku-beta6/src/bin/mount.c
T

102 lines
2.2 KiB
C
Raw Normal View History

2002-07-25 02:56:47 +00:00
/*
* Copyright 2001-2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
2002-07-25 02:56:47 +00:00
/** Mounts a volume with the specified file system */
2002-07-25 02:56:47 +00:00
#include <fs_volume.h>
#include <sys/stat.h>
2002-07-25 02:56:47 +00:00
#include <unistd.h>
#include <stdio.h>
2002-07-25 02:56:47 +00:00
#include <string.h>
static void
2002-07-25 02:56:47 +00:00
usage(const char *programName)
{
printf("usage: %s [-ro] [-t fstype] [-p parameter] [device] directory\n"
2002-07-25 02:56:47 +00:00
"\t-ro\tmounts the volume read-only\n"
"\t-t\tspecifies the file system to use (defaults to automatic recognition)\n"
"\t-p\tspecifies parameters to pass to the file system (-o also accepted)\n"
"\tif device is not specified, NULL is passed (for in-memory filesystems)\n",programName);
2006-08-30 09:46:42 +00:00
exit(1);
2002-07-25 02:56:47 +00:00
}
int
main(int argc, char **argv)
{
const char *programName = argv[0];
const char *device = NULL;
const char *mountPoint;
2002-07-25 02:56:47 +00:00
const char *parameter = NULL;
const char *fs = NULL;
struct stat mountStat;
dev_t volume;
uint32 flags = 0;
2002-07-25 02:56:47 +00:00
/* prettify the program name */
if (strrchr(programName, '/'))
programName = strrchr(programName, '/') + 1;
/* get all options */
while (*++argv) {
char *arg = *argv;
2006-08-16 10:50:03 +00:00
argc--;
2002-07-25 02:56:47 +00:00
if (*arg != '-')
break;
if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0)
flags |= B_MOUNT_READ_ONLY;
2006-08-16 10:50:03 +00:00
else if (!strcmp(arg, "t") && fs == NULL) {
if (argc <= 1)
break;
2002-07-25 02:56:47 +00:00
fs = *++argv;
2006-08-16 10:50:03 +00:00
argc--;
} else if ((!strcmp(arg, "p") || !strcmp(arg, "o")) && parameter == NULL) {
2006-08-16 10:50:03 +00:00
if (argc <= 1)
break;
2002-07-25 02:56:47 +00:00
parameter = *++argv;
2006-08-16 10:50:03 +00:00
argc--;
} else
2002-07-25 02:56:47 +00:00
usage(programName);
}
/* check the arguments */
if (argc > 1) {
device = argv[0];
argv++;
argc--;
}
mountPoint = argv[0];
2002-07-25 02:56:47 +00:00
if (mountPoint == NULL)
2002-07-25 02:56:47 +00:00
usage(programName);
if (stat(mountPoint, &mountStat) < 0) {
fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint);
2006-08-30 09:46:42 +00:00
return 1;
2002-07-25 02:56:47 +00:00
}
if (!S_ISDIR(mountStat.st_mode)) {
fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint);
2006-08-30 09:46:42 +00:00
return 1;
2002-07-25 02:56:47 +00:00
}
/* do the work */
volume = fs_mount_volume(mountPoint, device, fs, flags, parameter);
if (volume < B_OK) {
fprintf(stderr, "%s: %s\n", programName, strerror(volume));
2006-08-30 09:46:42 +00:00
return 1;
2002-07-25 02:56:47 +00:00
}
return 0;
}