getopt_ulflags -> getopt32.
[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 <mntent.h>
18
19 /* various defines swiped from linux/cdrom.h */
20 #define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
21 #define CDROMEJECT                0x5309  /* Ejects the cdrom media */
22 #define DEFAULT_CDROM             "/dev/cdrom"
23
24 #define FLAG_CLOSE  1
25 #define FLAG_SMART  2
26
27 int eject_main(int argc, char **argv)
28 {
29         unsigned long flags;
30         char *device;
31         struct mntent *m;
32         int dev;
33
34         /*opt_complementary = "t--T:T--t";*/
35         flags = getopt32(argc, argv, "tT");
36         device = argv[optind] ? : DEFAULT_CDROM;
37
38         m = find_mount_point(device, bb_path_mtab_file);
39         if (m) {
40                 if (umount(m->mnt_dir)) {
41                         bb_error_msg_and_die("can't umount");
42                 } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
43                         erase_mtab(m->mnt_fsname);
44                 }
45         }
46
47         dev = xopen(device, O_RDONLY|O_NONBLOCK);
48
49         if (flags & FLAG_CLOSE) goto close_tray;
50
51         if (ioctl(dev, CDROMEJECT)) {
52 close_tray:
53                 if (ioctl(dev, CDROMCLOSETRAY))
54                         bb_perror_msg_and_die("%s", device);
55         }
56
57         if (ENABLE_FEATURE_CLEAN_UP) close(dev);
58
59         return EXIT_SUCCESS;
60 }