Patch from Yann Morin to put BLKGETSIZE64 in platform.h had rather a lot of
[oweals/busybox.git] / miscutils / eject.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * eject implementation for busybox
4  *
5  * Copyright (C) 2004  Peter Willis <psyphreak@phreaker.net>
6  * Copyright (C) 2005  Tito Ragusa <farmatito@tiscali.it>
7  *
8  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9  */
10
11 /*
12  * This is a simple hack of eject based on something Erik posted in #uclibc.
13  * Most of the dirty work blatantly ripped off from cat.c =)
14  */
15
16 #include "busybox.h"
17 #include <fcntl.h>
18 #include <sys/ioctl.h>
19 #include <unistd.h>
20 #include <mntent.h>
21
22 /* various defines swiped from linux/cdrom.h */
23 #define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
24 #define CDROMEJECT                0x5309  /* Ejects the cdrom media */
25 #define DEFAULT_CDROM             "/dev/cdrom"
26
27 int eject_main(int argc, char **argv)
28 {
29         unsigned long flags;
30         char *device;
31         struct mntent *m;
32
33         flags = bb_getopt_ulflags(argc, argv, "t");
34         device = argv[optind] ? : DEFAULT_CDROM;
35
36         if ((m = find_mount_point(device, bb_path_mtab_file))) {
37                 if (umount(m->mnt_dir)) {
38                         bb_error_msg_and_die("Can't umount");
39                 } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
40                         erase_mtab(m->mnt_fsname);
41                 }
42         }
43         if (ioctl(bb_xopen(device, (O_RDONLY | O_NONBLOCK)),
44                                 (flags ? CDROMCLOSETRAY : CDROMEJECT))) {
45                 bb_perror_msg_and_die("%s", device);
46         }
47         return (EXIT_SUCCESS);
48 }