* Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
* http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
*/
-static void select_speed(int fd, int speed)
+static void select_speed(const struct eject_control *ctl)
{
#ifdef CDROM_SELECT_SPEED
- if (ioctl(fd, CDROM_SELECT_SPEED, speed) != 0)
+ if (ioctl(ctl->fd, CDROM_SELECT_SPEED, ctl->x_arg) != 0)
err(EXIT_FAILURE, _("CD-ROM select speed command failed"));
#else
warnx(_("CD-ROM select speed command not supported by this kernel"));
/*
* List Speed of CD-ROM drive.
*/
-static void list_speeds(const struct eject_control *ctl)
+static void list_speeds(struct eject_control *ctl)
{
#ifdef CDROM_SELECT_SPEED
- int max_speed, curr_speed = 0, prev_speed;
+ int max_speed, curr_speed = 0;
- select_speed(ctl->fd, 0);
+ select_speed(ctl);
max_speed = read_speed(ctl->device);
while (curr_speed < max_speed) {
- prev_speed = curr_speed;
- select_speed(ctl->fd, prev_speed + 1);
+ ctl->x_arg = curr_speed + 1;
+ select_speed(ctl);
curr_speed = read_speed(ctl->device);
- if (curr_speed > prev_speed)
+ if (ctl->x_arg < curr_speed)
printf("%d ", curr_speed);
else
- curr_speed = prev_speed + 1;
+ curr_speed = ctl->x_arg + 1;
}
printf("\n");
}
/* Open a device file. */
-static int open_device(const char *name)
+static void open_device(struct eject_control *ctl)
{
- int fd = open(name, O_RDWR|O_NONBLOCK);
-
- if (fd < 0)
- fd = open(name, O_RDONLY|O_NONBLOCK);
- if (fd == -1)
- err(EXIT_FAILURE, _("cannot open %s"), name);
- return fd;
+ ctl->fd = open(ctl->device, O_RDWR | O_NONBLOCK);
+ if (ctl->fd < 0)
+ ctl->fd = open(ctl->device, O_RDONLY | O_NONBLOCK);
+ if (ctl->fd == -1)
+ err(EXIT_FAILURE, _("cannot open %s"), ctl->device);
}
/*
/* handle -x option */
-static void set_device_speed(const struct eject_control *ctl)
+static void set_device_speed(struct eject_control *ctl)
{
- int fd;
-
if (!ctl->x_option)
return;
else
verbose(ctl, _("setting CD-ROM speed to %ldX"), ctl->x_arg);
- fd = open_device(ctl->device);
- select_speed(fd, ctl->x_arg);
+ open_device(ctl);
+ select_speed(ctl);
exit(EXIT_SUCCESS);
}
/* handle -i option */
if (ctl.i_option) {
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
manual_eject(&ctl);
return EXIT_SUCCESS;
}
verbose(&ctl, _("%s: enabling auto-eject mode"), ctl.device);
else
verbose(&ctl, _("%s: disabling auto-eject mode"), ctl.device);
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
auto_eject(&ctl);
return EXIT_SUCCESS;
}
/* handle -t option */
if (ctl.t_option) {
verbose(&ctl, _("%s: closing tray"), ctl.device);
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
close_tray(ctl.fd);
set_device_speed(&ctl);
return EXIT_SUCCESS;
/* handle -T option */
if (ctl.T_option) {
verbose(&ctl, _("%s: toggling tray"), ctl.device);
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
toggle_tray(ctl.fd);
set_device_speed(&ctl);
return EXIT_SUCCESS;
/* handle -X option */
if (ctl.X_option) {
verbose(&ctl, _("%s: listing CD-ROM speed"), ctl.device);
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
list_speeds(&ctl);
return EXIT_SUCCESS;
}
/* handle -c option */
if (ctl.c_option) {
verbose(&ctl, _("%s: selecting CD-ROM disc #%ld"), ctl.device, ctl.c_arg);
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
changer_select(&ctl);
set_device_speed(&ctl);
return EXIT_SUCCESS;
ctl.r_option = ctl.s_option = ctl.f_option = ctl.q_option = 1;
/* open device */
- ctl.fd = open_device(ctl.device);
+ open_device(&ctl);
/* try various methods of ejecting until it works */
if (ctl.r_option) {