style fixes and pass WARNS=4 and lint from Anon Ymous

This commit is contained in:
christos 2009-01-14 15:47:23 +00:00
parent 4c7a98373d
commit a127d189ed
2 changed files with 149 additions and 143 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.3 2001/08/20 08:21:10 ad Exp $
# $NetBSD: Makefile,v 1.4 2009/01/14 15:47:23 christos Exp $
PROG= eject
SRCS= eject.c
@ -6,4 +6,6 @@ SRCS= eject.c
LDADD+= -lutil
DPADD+= ${LIBUTIL}
WARNS= 4
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: eject.c,v 1.22 2008/07/21 14:19:22 lukem Exp $ */
/* $NetBSD: eject.c,v 1.23 2009/01/14 15:47:23 christos Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
@ -36,7 +36,7 @@ __COPYRIGHT("@(#) Copyright (c) 1999\
#endif /* not lint */
#ifndef lint
__RCSID("$NetBSD: eject.c,v 1.22 2008/07/21 14:19:22 lukem Exp $");
__RCSID("$NetBSD: eject.c,v 1.23 2009/01/14 15:47:23 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -58,49 +58,49 @@ __RCSID("$NetBSD: eject.c,v 1.22 2008/07/21 14:19:22 lukem Exp $");
#include <util.h>
struct nicknames_s {
char *name; /* The name given on the command line. */
char *devname; /* The base name of the device */
const char *name; /* The name given on the command line. */
const char *devname; /* The base name of the device */
int type; /* The type of device, for determining what
* ioctl to use. */
#define TAPE 0x10
#define DISK 0x20
# define TAPE 0x10
# define DISK 0x20
/* OR one of the above with one of the below: */
#define NOTLOADABLE 0x00
#define LOADABLE 0x01
#define FLOPPY 0x2
#define TYPEMASK ((int)~0x01)
} nicknames[] = {
{ "diskette", "fd", DISK | FLOPPY | NOTLOADABLE },
{ "floppy", "fd", DISK | FLOPPY | NOTLOADABLE },
{ "fd", "fd", DISK | FLOPPY | NOTLOADABLE },
{ "sd", "sd", DISK | NOTLOADABLE },
{ "cdrom", "cd", DISK | LOADABLE },
{ "cd", "cd", DISK | LOADABLE },
{ "cdr", "cd", DISK | LOADABLE },
{ "cdrw", "cd", DISK | LOADABLE },
{ "dvdrom", "cd", DISK | LOADABLE },
{ "dvd", "cd", DISK | LOADABLE },
{ "dvdr", "cd", DISK | LOADABLE },
{ "dvdrw", "cd", DISK | LOADABLE },
{ "mcd", "mcd", DISK | LOADABLE }, /* XXX Is this true? */
{ "tape", "st", TAPE | NOTLOADABLE },
{ "st", "st", TAPE | NOTLOADABLE },
{ "dat", "st", TAPE | NOTLOADABLE },
{ "exabyte", "st", TAPE | NOTLOADABLE },
# define NOTLOADABLE 0x00
# define LOADABLE 0x01
# define FLOPPY 0x2
# define TYPEMASK ((int)~0x01)
} nicknames[] = {
{ "diskette", "fd", DISK | FLOPPY | NOTLOADABLE },
{ "floppy", "fd", DISK | FLOPPY | NOTLOADABLE },
{ "fd", "fd", DISK | FLOPPY | NOTLOADABLE },
{ "sd", "sd", DISK | NOTLOADABLE },
{ "cdrom", "cd", DISK | LOADABLE },
{ "cd", "cd", DISK | LOADABLE },
{ "cdr", "cd", DISK | LOADABLE },
{ "cdrw", "cd", DISK | LOADABLE },
{ "dvdrom", "cd", DISK | LOADABLE },
{ "dvd", "cd", DISK | LOADABLE },
{ "dvdr", "cd", DISK | LOADABLE },
{ "dvdrw", "cd", DISK | LOADABLE },
{ "mcd", "mcd", DISK | LOADABLE }, /* XXX Is this true? */
{ "tape", "st", TAPE | NOTLOADABLE },
{ "st", "st", TAPE | NOTLOADABLE },
{ "dat", "st", TAPE | NOTLOADABLE },
{ "exabyte", "st", TAPE | NOTLOADABLE }
};
#define MAXNICKLEN 12 /* at least enough room for the longest
* nickname */
#define MAXDEVLEN (MAXNICKLEN + 7) /* "/dev/r" ... "a" */
struct devtypes_s {
char *name;
const char *name;
int type;
} devtypes[] = {
} devtypes[] = {
{ "diskette", DISK | NOTLOADABLE },
{ "floppy", DISK | NOTLOADABLE },
{ "cdrom", DISK | LOADABLE },
{ "disk", DISK | NOTLOADABLE },
{ "tape", TAPE | NOTLOADABLE },
{ "floppy", DISK | NOTLOADABLE },
{ "cdrom", DISK | LOADABLE },
{ "disk", DISK | NOTLOADABLE },
{ "tape", TAPE | NOTLOADABLE }
};
enum eject_op {
@ -111,28 +111,31 @@ int verbose_f = 0;
int umount_f = 1;
int main(int, char *[]);
void usage(void);
char *nick2dev(char *);
char *nick2rdev(char *);
int guess_devtype(char *);
char *guess_nickname(char *);
void eject_tape(char *, enum eject_op);
void eject_disk(char *, enum eject_op);
void unmount_dev(char *);
__dead static void usage(void);
static char *nick2dev(const char *);
static char *nick2rdev(const char *);
static int guess_devtype(const char *);
static void eject_disk(const char *, enum eject_op);
static void eject_tape(const char *, enum eject_op);
static void unmount_dev(const char *);
int
main(int argc, char *argv[])
{
int ch;
int devtype = -1;
int devtype;
int n, i;
char *devname = NULL;
enum eject_op op = OP_EJECT;
char *dev_name; /* XXX - devname is declared in stdlib.h */
enum eject_op op;
devtype = -1;
dev_name = NULL;
op = OP_EJECT;
while ((ch = getopt(argc, argv, "d:flLnt:Uv")) != -1) {
switch (ch) {
case 'd':
devname = optarg;
dev_name = optarg;
break;
case 'f':
umount_f = 0;
@ -148,17 +151,17 @@ main(int argc, char *argv[])
op = OP_LOCK;
break;
case 'n':
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
n++) {
for (n = 0; n < __arraycount(nicknames); n++) {
struct nicknames_s *np = &nicknames[n];
printf("%s -> %s\n", np->name, nick2dev(np->name));
(void)printf("%s -> %s\n",
np->name, nick2dev(np->name));
}
return (0);
return 0;
case 't':
for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]);
i++) {
if (strcasecmp(devtypes[i].name, optarg) == 0) {
for (i = 0; i < __arraycount(devtypes); i++) {
if (strcasecmp(devtypes[i].name, optarg)
== 0) {
devtype = devtypes[i].type;
break;
}
@ -182,150 +185,153 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
if (devname == NULL) {
if (argc == 0) {
if (dev_name == NULL) {
if (argc == 0)
usage();
/* NOTREACHED */
} else
devname = argv[0];
else
dev_name = argv[0];
}
if (devtype == -1)
devtype = guess_devtype(devname);
devtype = guess_devtype(dev_name);
if (devtype == -1)
errx(1, "%s: unable to determine type of device",
devname);
errx(1, "%s: unable to determine type of device", dev_name);
if (verbose_f) {
printf("device type == ");
(void)printf("device type == ");
if ((devtype & TYPEMASK) == TAPE)
printf("tape\n");
(void)printf("tape\n");
else
printf("disk, floppy, or cdrom\n");
(void)printf("disk, floppy, or cdrom\n");
}
if (umount_f)
unmount_dev(devname);
unmount_dev(dev_name);
/* XXX Tapes and disks have different ioctl's: */
if ((devtype & TYPEMASK) == TAPE)
eject_tape(devname, op);
eject_tape(dev_name, op);
else
eject_disk(devname, op);
eject_disk(dev_name, op);
if (verbose_f)
printf("done.\n");
(void)printf("done.\n");
return (0);
return 0;
}
void
__dead
static void
usage(void)
{
fprintf(stderr, "usage: eject [-fv] [-l | -L | -U] "
(void)fprintf(stderr, "usage: eject [-fv] [-l | -L | -U] "
"[-t device-type] [-d] device\n");
fprintf(stderr, " eject -n\n");
(void)fprintf(stderr, " eject -n\n");
exit(1);
}
int
guess_devtype(char *devname)
static int
guess_devtype(const char *dev_name)
{
int n;
/* Nickname match: */
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
n++) {
if (strncasecmp(nicknames[n].name, devname,
for (n = 0; n < __arraycount(nicknames); n++) {
if (strncasecmp(nicknames[n].name, dev_name,
strlen(nicknames[n].name)) == 0)
return (nicknames[n].type);
return nicknames[n].type;
}
/*
* If we still don't know it, then try to compare vs. dev
* and rdev names that we know.
* If we still don't know it, then try to compare vs. dev and
* rdev names that we know.
*/
/* dev first: */
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
for (n = 0; n < __arraycount(nicknames); n++) {
char *name;
name = nick2dev(nicknames[n].name);
/*
* Assume that the part of the name that distinguishes the
* instance of this device begins with a 0.
* Assume that the part of the name that distinguishes
* the instance of this device begins with a 0.
*/
*(strchr(name, '0')) = '\0';
if (strncmp(name, devname, strlen(name)) == 0)
return (nicknames[n].type);
if (strncmp(name, dev_name, strlen(name)) == 0)
return nicknames[n].type;
}
/* Now rdev: */
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
for (n = 0; n < __arraycount(nicknames); n++) {
char *name = nick2rdev(nicknames[n].name);
*(strchr(name, '0')) = '\0';
if (strncmp(name, devname, strlen(name)) == 0)
return (nicknames[n].type);
if (strncmp(name, dev_name, strlen(name)) == 0)
return nicknames[n].type;
}
/* Not found. */
return (-1);
return -1;
}
/* "floppy5" -> "/dev/fd5a". Yep, this uses a static buffer. */
char *
nick2dev(char *nn)
static char *
nick2dev(const char *nn)
{
static char dev_name[MAXDEVLEN];
int n;
static char devname[MAXDEVLEN];
int devnum = 0;
int devnum;
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
devnum = 0;
for (n = 0; n < __arraycount(nicknames); n++) {
if (strncasecmp(nicknames[n].name, nn,
strlen(nicknames[n].name)) == 0) {
sscanf(nn, "%*[^0-9]%d", &devnum);
sprintf(devname, "/dev/%s%d", nicknames[n].devname,
devnum);
(void)sscanf(nn, "%*[^0-9]%d", &devnum);
(void)sprintf(dev_name, "/dev/%s%d",
nicknames[n].devname, devnum);
if ((nicknames[n].type & TYPEMASK) != TAPE)
strcat(devname, "a");
return (devname);
(void)strcat(dev_name, "a");
return dev_name;
}
}
return (NULL);
return NULL;
}
/* "floppy5" -> "/dev/rfd5c". Static buffer. */
char *
nick2rdev(char *nn)
{
int n;
static char devname[MAXDEVLEN];
int devnum = 0;
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
/* "floppy5" -> "/dev/rfd5c". Static buffer. */
static char *
nick2rdev(const char *nn)
{
static char dev_name[MAXDEVLEN];
int n;
int devnum;
devnum = 0;
for (n = 0; n < __arraycount(nicknames); n++) {
if (strncasecmp(nicknames[n].name, nn,
strlen(nicknames[n].name)) == 0) {
sscanf(nn, "%*[^0-9]%d", &devnum);
sprintf(devname, "/dev/r%s%d", nicknames[n].devname,
devnum);
(void)sscanf(nn, "%*[^0-9]%d", &devnum);
(void)sprintf(dev_name, "/dev/r%s%d",
nicknames[n].devname, devnum);
if ((nicknames[n].type & TYPEMASK) != TAPE) {
strcat(devname, "a");
(void)strcat(dev_name, "a");
if ((nicknames[n].type & FLOPPY) != FLOPPY)
devname[strlen(devname) - 1] += getrawpartition();
dev_name[strlen(dev_name) - 1]
+= getrawpartition();
}
return (devname);
return dev_name;
}
}
return (NULL);
return NULL;
}
/* Unmount all filesystems attached to dev. */
void
unmount_dev(char *name)
static void
unmount_dev(const char *name)
{
struct statvfs *mounts;
int i, nmnts, len;
char *dn;
size_t len;
int i, nmnts;
const char *dn;
nmnts = getmntinfo(&mounts, MNT_NOWAIT);
if (nmnts == 0) {
if (nmnts == 0)
err(1, "getmntinfo");
}
/* Make sure we have a device name: */
dn = nick2dev(name);
if (dn == NULL)
@ -335,31 +341,29 @@ unmount_dev(char *name)
len = strlen(dn);
if (!isdigit((unsigned char)dn[len - 1]))
len--;
if (!isdigit((unsigned char)dn[len - 1])) {
if (!isdigit((unsigned char)dn[len - 1]))
errx(1, "Can't figure out base name for dev name %s", dn);
}
for (i = 0; i < nmnts; i++) {
if (strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
if (verbose_f)
printf("Unmounting %s from %s...\n",
(void)printf("Unmounting %s from %s...\n",
mounts[i].f_mntfromname,
mounts[i].f_mntonname);
if (unmount(mounts[i].f_mntonname, 0) == -1) {
if (unmount(mounts[i].f_mntonname, 0) == -1)
err(1, "unmount: %s", mounts[i].f_mntonname);
}
}
}
return;
}
void
eject_tape(char *name, enum eject_op op)
static void
eject_tape(const char *name, enum eject_op op)
{
struct mtop m;
int fd;
char *dn;
const char *dn;
dn = nick2rdev(name);
if (dn == NULL)
@ -370,7 +374,7 @@ eject_tape(char *name, enum eject_op op)
switch (op) {
case OP_EJECT:
if (verbose_f)
printf("Ejecting %s...\n", dn);
(void)printf("Ejecting %s...\n", dn);
m.mt_op = MTOFFL;
m.mt_count = 0;
@ -387,15 +391,15 @@ eject_tape(char *name, enum eject_op op)
errx(1, "cannot unlock tapes");
/* NOTREACHED */
}
close(fd);
(void)close(fd);
return;
}
void
eject_disk(char *name, enum eject_op op)
static void
eject_disk(const char *name, enum eject_op op)
{
int fd;
char *dn;
const char *dn;
int arg;
dn = nick2rdev(name);
@ -407,14 +411,14 @@ eject_disk(char *name, enum eject_op op)
switch (op) {
case OP_LOAD:
if (verbose_f)
printf("Closing %s...\n", dn);
(void)printf("Closing %s...\n", dn);
if (ioctl(fd, CDIOCCLOSE, NULL) == -1)
err(1, "ioctl: CDIOCCLOSE: %s", dn);
break;
case OP_EJECT:
if (verbose_f)
printf("Ejecting %s...\n", dn);
(void)printf("Ejecting %s...\n", dn);
arg = 0;
if (umount_f == 0) {
@ -428,7 +432,7 @@ eject_disk(char *name, enum eject_op op)
break;
case OP_LOCK:
if (verbose_f)
printf("Locking %s...\n", dn);
(void)printf("Locking %s...\n", dn);
arg = 1;
if (ioctl(fd, DIOCLOCK, &arg) == -1)
@ -436,7 +440,7 @@ eject_disk(char *name, enum eject_op op)
break;
case OP_UNLOCK:
if (verbose_f)
printf("Unlocking %s...\n", dn);
(void)printf("Unlocking %s...\n", dn);
arg = 0;
if (ioctl(fd, DIOCLOCK, &arg) == -1)
@ -444,6 +448,6 @@ eject_disk(char *name, enum eject_op op)
break;
}
close(fd);
(void)close(fd);
return;
}