Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/eject Add -L and -U options to eject(1) that call DI...



details:   https://anonhg.NetBSD.org/src/rev/922dd8d4912e
branches:  trunk
changeset: 515866:922dd8d4912e
user:      bjh21 <bjh21%NetBSD.org@localhost>
date:      Sat Oct 06 15:43:33 2001 +0000

description:
Add -L and -U options to eject(1) that call DIOCLOCK.  Also generally clean
stuff up, correct the usage message and update the manpage to match.

Note that the new options aren't wonderfully useful, since cd(4) at least
promptly unlocks the drawer when eject closes the device.

diffstat:

 usr.bin/eject/eject.1 |  25 ++++++++++--
 usr.bin/eject/eject.c |  96 ++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 93 insertions(+), 28 deletions(-)

diffs (269 lines):

diff -r 61aad1b62273 -r 922dd8d4912e usr.bin/eject/eject.1
--- a/usr.bin/eject/eject.1     Sat Oct 06 15:03:34 2001 +0000
+++ b/usr.bin/eject/eject.1     Sat Oct 06 15:43:33 2001 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: eject.1,v 1.8 2001/02/27 19:05:56 cgd Exp $
+.\" $NetBSD: eject.1,v 1.9 2001/10/06 15:43:33 bjh21 Exp $
 .\"
 .\" Copyright (c) 1998 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -34,7 +34,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd October 9, 1995
+.Dd October 6, 2001
 .Dt EJECT 1
 .Os
 .Sh NAME
@@ -42,10 +42,13 @@
 .Nd eject a floppy disk, cdrom or tape
 .Sh SYNOPSIS
 .Nm
-.Op Fl flnv
-.Op Fl d Ar raw-device
+.Op Fl fv
+.Op Fl l | L | U
 .Op Fl t Ar device-type
-.Op Ar device
+.Op Fl d
+.Ar device
+.Nm ""
+.Fl n
 .Sh DESCRIPTION
 The
 .Nm
@@ -71,6 +74,12 @@
 Force the eject operation without attempting to unmount any filesystems first.
 .It Fl l
 Load media in the drive (only supporded for the cdrom device type).
+.It Fl L
+Lock the media into the drive (but see
+.Sx BUGS
+below).
+.It Fl U
+Unlock the media from the drive.
 .It Fl n
 List the built-in nicknames on standard output.
 .It Fl t Ar device-type
@@ -88,3 +97,9 @@
 Display some of the actions taken on standard output.
 .El
 .Pp
+.Sh BUGS
+Most disk drivers automatically lock the media on the first open and
+unlock it on the last close, making
+.Ql eject -L
+almost useless, since when it closes the device, it gets unlocked
+again.
diff -r 61aad1b62273 -r 922dd8d4912e usr.bin/eject/eject.c
--- a/usr.bin/eject/eject.c     Sat Oct 06 15:03:34 2001 +0000
+++ b/usr.bin/eject/eject.c     Sat Oct 06 15:43:33 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eject.c,v 1.15 2001/10/06 14:29:55 bjh21 Exp $ */
+/*     $NetBSD: eject.c,v 1.16 2001/10/06 15:43:33 bjh21 Exp $ */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -43,7 +43,7 @@
 #endif                         /* not lint */
 
 #ifndef lint
-__RCSID("$NetBSD: eject.c,v 1.15 2001/10/06 14:29:55 bjh21 Exp $");
+__RCSID("$NetBSD: eject.c,v 1.16 2001/10/06 15:43:33 bjh21 Exp $");
 #endif                         /* not lint */
 
 #include <sys/types.h>
@@ -109,9 +109,12 @@
        { "tape", TAPE | NOTLOADABLE },
 };
 
+enum eject_op {
+       OP_EJECT, OP_LOAD, OP_LOCK, OP_UNLOCK
+};
+
 int verbose_f = 0;
 int umount_f = 1;
-int load_f = 0;
 
 int main(int, char *[]);
 void usage(void);
@@ -119,8 +122,8 @@
 char *nick2rdev(char *);
 int guess_devtype(char *);
 char *guess_nickname(char *);
-void eject_tape(char *);
-void eject_disk(char *);
+void eject_tape(char *, enum eject_op);
+void eject_disk(char *, enum eject_op);
 void unmount_dev(char *);
 
 int
@@ -130,8 +133,9 @@
        int devtype = -1;
        int n, i;
        char *devname = NULL;
+       enum eject_op op = OP_EJECT;
 
-       while ((ch = getopt(argc, argv, "d:flnt:v")) != -1) {
+       while ((ch = getopt(argc, argv, "d:flLnt:Uv")) != -1) {
                switch (ch) {
                case 'd':
                        devname = optarg;
@@ -140,7 +144,14 @@
                        umount_f = 0;
                        break;
                case 'l':
-                       load_f = 1;
+                       if (op != OP_EJECT)
+                               usage();
+                       op = OP_LOAD;
+                       break;
+               case 'L':
+                       if (op != OP_EJECT)
+                               usage();
+                       op = OP_LOCK;
                        break;
                case 'n':
                        for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
@@ -159,13 +170,17 @@
                                }
                        }
                        if (devtype == -1)
-                               errx(1, "%s: unknown device type\n", optarg);
+                               errx(1, "%s: unknown device type", optarg);
+                       break;
+               case 'U':
+                       if (op != OP_EJECT)
+                               usage();
+                       op = OP_UNLOCK;
                        break;
                case 'v':
                        verbose_f = 1;
                        break;
                default:
-                       warnx("-%c: unknown switch", ch);
                        usage();
                        /* NOTREACHED */
                }
@@ -197,9 +212,9 @@
 
        /* XXX Tapes and disks have different ioctl's: */
        if ((devtype & TYPEMASK) == TAPE)
-               eject_tape(devname);
+               eject_tape(devname, op);
        else
-               eject_disk(devname);
+               eject_disk(devname, op);
 
        if (verbose_f)
                printf("done.\n");
@@ -211,7 +226,10 @@
 usage(void)
 {
 
-       errx(1, "Usage: eject [-n][-f][-v][-l][-t type][-d] device | nickname");
+       fprintf(stderr, "Usage: eject [-fv] [-l | -L | -U] "
+           "[-t device-type] [-d] device\n");
+       fprintf(stderr, "       eject -n\n");
+       exit(1);
 }
 
 int
@@ -342,7 +360,7 @@
 }
 
 void
-eject_tape(char *name)
+eject_tape(char *name, enum eject_op op)
 {
        struct mtop m;
        int fd;
@@ -354,19 +372,32 @@
        fd = open(dn, O_RDONLY);
        if (fd == -1)
                err(1, "open: %s", dn);
-       if (verbose_f)
-               printf("Ejecting %s...\n", dn);
+       switch (op) {
+       case OP_EJECT:
+               if (verbose_f)
+                       printf("Ejecting %s...\n", dn);
 
-       m.mt_op = MTOFFL;
-       m.mt_count = 0;
-       if (ioctl(fd, MTIOCTOP, &m) == -1)
-               err(1, "ioctl: MTIOCTOP: %s", dn);
+               m.mt_op = MTOFFL;
+               m.mt_count = 0;
+               if (ioctl(fd, MTIOCTOP, &m) == -1)
+                       err(1, "ioctl: MTIOCTOP: %s", dn);
+               break;
+       case OP_LOAD:
+               errx(1, "cannot load tapes");
+               /* NOTREACHED */
+       case OP_LOCK:
+               errx(1, "cannot lock tapes");
+               /* NOTREACHED */
+       case OP_UNLOCK:
+               errx(1, "cannot unlock tapes");
+               /* NOTREACHED */
+       }
        close(fd);
        return;
 }
 
 void
-eject_disk(char *name)
+eject_disk(char *name, enum eject_op op)
 {
        int fd;
        char *dn;
@@ -378,13 +409,15 @@
        fd = open(dn, O_RDONLY);
        if (fd == -1)
                err(1, "open: %s", dn);
-       if (load_f) {
+       switch (op) {
+       case OP_LOAD:
                if (verbose_f)
                        printf("Closing %s...\n", dn);
 
                if (ioctl(fd, CDIOCCLOSE, NULL) == -1)
                        err(1, "ioctl: CDIOCCLOSE: %s", dn);
-       } else {
+               break;
+       case OP_EJECT:
                if (verbose_f)
                        printf("Ejecting %s...\n", dn);
 
@@ -392,11 +425,28 @@
                if (umount_f == 0) {
                        /* force eject, unlock the device first */
                        if (ioctl(fd, DIOCLOCK, &arg) == -1)
-                               err(1, "ioctl: DIOCEJECT: %s", dn);
+                               err(1, "ioctl: DIOCLOCK: %s", dn);
                        arg = 1;
                }
                if (ioctl(fd, DIOCEJECT, &arg) == -1)
                        err(1, "ioctl: DIOCEJECT: %s", dn);
+               break;
+       case OP_LOCK:
+               if (verbose_f)
+                       printf("Locking %s...\n", dn);
+
+               arg = 1;
+               if (ioctl(fd, DIOCLOCK, &arg) == -1)
+                       err(1, "ioctl: DIOCLOCK: %s", dn);
+               break;
+       case OP_UNLOCK:
+               if (verbose_f)
+                       printf("Unlocking %s...\n", dn);
+
+               arg = 0;
+               if (ioctl(fd, DIOCLOCK, &arg) == -1)
+                       err(1, "ioctl: DIOCLOCK: %s", dn);
+               break;
        }
 
        close(fd);



Home | Main Index | Thread Index | Old Index