View of xos/usr/umount/umount.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include <getopt.h>
#include <error.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mount.h>
#include <errno.h>
 
static void __attribute__ ((noreturn)) die_bad_usage()
{
  fprintf(stderr, "Try `%s --help' for more information.\n", program_invocation_name);
  exit(EXIT_FAILURE);
}
 
static void print_help()
{
  printf("Usage: %s [OPTION] DIR\n", program_invocation_name);
  puts("Umount a file system.");
  putchar('\n');
  puts("  -h, --help  display this help and exit");
}
 
int main(int argc, char **argv)
{
  static const struct option longopts[] = {
    {"help", no_argument, NULL, 'h'},
    {NULL, 0, NULL, 0}
  };
 
  int c;
 
  while ((c = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
    switch (c) {
    case 'h':
      print_help();
      return EXIT_SUCCESS;
    case '?':
      die_bad_usage();
    }
 
  if (optind >= argc) {
    error(0, 0, "missing operand");
    die_bad_usage();
  }
  if (umount(argv[optind]) == -1)
    error(EXIT_FAILURE, errno, "failed to umount %s", argv[optind]);
  return EXIT_SUCCESS;
}