mount-stub.c (1775B)
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _GNU_SOURCE
- #include <errno.h>
- #include <getopt.h> // getopt_long
- #include <stdio.h> // fprintf
- #include <string.h> // strerror
- #include <sys/mount.h> // mount
- static void
- usage()
- {
- fprintf(stderr, "\
- Usage: mount [-t fstype] device mountpoint\n\
- mount [--bind|--rbind|--move] source destination\n\
- ");
- }
- int
- main(int argc, char *argv[])
- {
- char *fstype = NULL;
- int mnt_flags = 0;
- int opt_bind_move = 0;
- struct option longopts[] = {
- /* options exclusive to another, so mnt_flags can be set directly */
- {"bind", no_argument, &opt_bind_move, MS_BIND},
- {"rbind", no_argument, &opt_bind_move, MS_BIND | MS_REC},
- {"move", no_argument, &opt_bind_move, MS_MOVE},
- };
- int c = -1;
- while((c = getopt_long(argc, argv, ":t:", longopts, NULL)) != -1)
- {
- switch(c)
- {
- case 0:
- break;
- case 't':
- fstype = optarg;
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if(opt_bind_move != 0)
- {
- if(argc != 2)
- {
- fprintf(stderr, "mount: Expected 2 arguments, got %d\n", argc);
- usage();
- return 1;
- }
- char *src = argv[0];
- char *dest = argv[1];
- int ret = mount(src, dest, NULL, opt_bind_move, NULL);
- if(ret != 0)
- {
- fprintf(stderr, "mount: Failed mounting %s to %s: %s\n", src, dest, strerror(errno));
- return 1;
- }
- return 0;
- }
- if(argc != 2)
- {
- fprintf(stderr, "mount: Expected 2 arguments, got %d\n", argc);
- usage();
- return 1;
- }
- char *src = argv[0];
- char *dest = argv[1];
- int ret = mount(src, dest, fstype, mnt_flags, NULL);
- if(ret != 0)
- {
- fprintf(stderr, "mount: Failed mounting %s to %s: %s\n", src, dest, strerror(errno));
- return 1;
- }
- return 0;
- }