hust testsuite: fix a false positive
[oweals/busybox.git] / miscutils / eject.c
index 94a36c0da3ea9ce87baadd695db047397e3c5fce..16ae250ff6168edebc36b17d499e3918b765f489 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright (C) 2004  Peter Willis <psyphreak@phreaker.net>
  * Copyright (C) 2005  Tito Ragusa <farmatito@tiscali.it>
  *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 /*
  * Most of the dirty work blatantly ripped off from cat.c =)
  */
 
+//usage:#define eject_trivial_usage
+//usage:       "[-t] [-T] [DEVICE]"
+//usage:#define eject_full_usage "\n\n"
+//usage:       "Eject DEVICE or default /dev/cdrom\n"
+//usage:       IF_FEATURE_EJECT_SCSI(
+//usage:     "\n       -s      SCSI device"
+//usage:       )
+//usage:     "\n       -t      Close tray"
+//usage:     "\n       -T      Open/close tray (toggle)"
+
+#include <sys/mount.h>
 #include "libbb.h"
-
-/* various defines swiped from linux/cdrom.h */
-#define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
-#define CDROMEJECT                0x5309  /* Ejects the cdrom media */
-#define CDROM_DRIVE_STATUS        0x5326  /* Get tray position, etc. */
-/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
-#define CDS_TRAY_OPEN        2
+#if ENABLE_FEATURE_EJECT_SCSI
+/* Must be after libbb.h: they need size_t */
+# include "fix_u32.h"
+# include <scsi/sg.h>
+# include <scsi/scsi.h>
+#endif
 
 #define dev_fd 3
 
 /* Code taken from the original eject (http://eject.sourceforge.net/),
  * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
 
-#include <scsi/sg.h>
-#include <scsi/scsi.h>
-
+#if ENABLE_FEATURE_EJECT_SCSI
 static void eject_scsi(const char *dev)
 {
-       static const char sg_commands[3][6] = {
+       static const char sg_commands[3][6] ALIGN1 = {
                { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
                { START_STOP, 0, 0, 0, 1, 0 },
                { START_STOP, 0, 0, 0, 2, 0 }
@@ -64,6 +72,16 @@ static void eject_scsi(const char *dev)
        /* force kernel to reread partition table when new disc is inserted */
        ioctl(dev_fd, BLKRRPART);
 }
+#else
+# define eject_scsi(dev) ((void)0)
+#endif
+
+/* various defines swiped from linux/cdrom.h */
+#define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
+#define CDROMEJECT                0x5309  /* Ejects the cdrom media */
+#define CDROM_DRIVE_STATUS        0x5326  /* Get tray position, etc. */
+/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
+#define CDS_TRAY_OPEN        2
 
 #define FLAG_CLOSE  1
 #define FLAG_SMART  2
@@ -74,7 +92,7 @@ static void eject_cdrom(unsigned flags, const char *dev)
        int cmd = CDROMEJECT;
 
        if (flags & FLAG_CLOSE
-        || (flags & FLAG_SMART && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
+        || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
        ) {
                cmd = CDROMCLOSETRAY;
        }
@@ -102,7 +120,7 @@ int eject_main(int argc UNUSED_PARAM, char **argv)
           eject /dev/cdrom
        */
 
-       xmove_fd(xopen(device, O_RDONLY|O_NONBLOCK), dev_fd);
+       xmove_fd(xopen_nonblocking(device), dev_fd);
 
        if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
                eject_scsi(device);