uncuddle brackets
[oweals/busybox.git] / miscutils / eject.c
1 /*
2  * eject implementation for busybox
3  *
4  * Copyright (C) 2004  Peter Willis <psyphreak@phreaker.net>
5  * Copyright (C) 2005  Tito Ragusa <farmatito@tiscali.it>
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  *
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 <fcntl.h>
17 #include <sys/ioctl.h>
18 #include <unistd.h>
19 #include <sys/mount.h>
20 #include <mntent.h>
21 #include "busybox.h"
22
23 /* various defines swiped from linux/cdrom.h */
24 #define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
25 #define CDROMEJECT                0x5309  /* Ejects the cdrom media */
26 #define DEFAULT_CDROM             "/dev/cdrom"
27
28 int eject_main(int argc, char **argv)
29 {
30         unsigned long flags;
31         char *device;
32         struct mntent *m;
33
34         flags = bb_getopt_ulflags(argc, argv, "t");
35         device = argv[optind] ? : DEFAULT_CDROM;
36
37         if ((m = find_mount_point(device, bb_path_mtab_file))) {
38                 if (umount(m->mnt_dir)) {
39                         bb_error_msg_and_die("Can't umount");
40                 } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
41                         erase_mtab(m->mnt_fsname);
42                 }
43         }
44         if (ioctl(bb_xopen(device, (O_RDONLY | O_NONBLOCK)),
45                                 (flags ? CDROMCLOSETRAY : CDROMEJECT))) {
46                 bb_perror_msg_and_die("%s", device);
47         }
48         return (EXIT_SUCCESS);
49 }