Skip to content

Instantly share code, notes, and snippets.

@ryanmjacobs
Last active August 29, 2015 14:15
Show Gist options
  • Save ryanmjacobs/32d2054ef2cb960c6775 to your computer and use it in GitHub Desktop.
Save ryanmjacobs/32d2054ef2cb960c6775 to your computer and use it in GitHub Desktop.
/**
* NOTE:
* The shell script at the bottom of the page is
* more reliable and less buggy. Go use that instead.
*
* Usage: pop [path to cd-drive]
* Compile: gcc pop.c -o pop
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
int cdrom;
void sigint(int signal) {
close(cdrom);
exit(0);
}
int main(int argc, char **argv) {
const char *path;
signal(SIGINT, sigint);
path = (argc >= 2 ? argv[1] : "/dev/sr0");
if ((cdrom = open(path, O_RDONLY | O_NONBLOCK)) < 0) {
printf("Unable to open device: %s\n", path);
return 1;
} else {
printf("Watching device: %s\n", path);
}
while (1) {
if (ioctl(cdrom, CDROM_DRIVE_STATUS) != CDS_TRAY_OPEN) {
puts("pop");
ioctl(cdrom, CDROMEJECT);
}
usleep(500000);
}
close(cdrom);
return 0;
}
#!/bin/sh
################################################################################
# pop.sh
#
# The eject command only ejects if the tray is closed, so we don't need to
# worry about checking the tray status.
#
# Run: ./pop.sh & exit
################################################################################
while true; do
eject
sleep 0.5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment