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

Re: cd-lock/unlock



it's slightly buggy, as I parsed argv[0] a bit wrong.  I discovered the
basename() function, which fixes that up.

here's the fixed version.

On Wed, 2002-07-31 at 00:12, Shaya Potter wrote:
> Perhaps other people have had similiar problem to me, in that the cd
> tray on their laptop tends to get ejected easily (at least on my
> thinkpad the button seems to be pressed easily).  I wrote this little
> tiny program that when called as cd-lock (or cd-unlock) calls the
> CDROM_LOCKDOOR ioctl with the appropriate integer value to lock and
> unlock the appropriate device.
> 
> just compile it as gcc cd-door.c -o cd-lock
> 
> then cp cd-lock to cd-unlock, and then you can just call it as "cd-lock
> device".
> 
> posting as others might fine usefull.  perhaps this already exists
> somewhere, but a cursory check couldn't find it and it was quicker to
> write it than to continue the search.
> 
> 
> 
> 
> ----
> 

> //Written by Shaya Potter <spotter@cs.columbia.edu>
> //Placed in the Public Domain
> 
> #include <sys/ioctl.h>
> #include <stdio.h>
> #include <fcntl.h>
> #include <linux/cdrom.h>
> #include <string.h>
> 
> int main(int argc, char *argv[])
> {
> 	int fd;
> 	int lock;
> 
> 	char *cmd, *cmd1;
> 
> 	cmd = argv[0];
> 	while (*cmd != 0) {
> 		if (*cmd == '/')
> 			cmd1 = cmd + 1;
> 		cmd++;
> 	}
> 
> 	if (!strcmp(cmd1, "cd-lock"))
> 		lock = 1;
> 	else if (!strcmp(cmd1, "cd-unlock"))
> 		lock = 0;
> 	else {
> 		printf("program must be called cd-lock or cd-unlock\n");
> 		return 1;
> 	}
> 
> 	if (argc != 2) {
> 		printf("not enough options\nUsage:\n\t%s device\n", cmd1);
> 		return 2;
> 	}
> 			
> 
> 	if ((fd = open(argv[1], O_RDONLY)) <= 0) {
> 		perror("open failed");
> 		return 3;
> 	}
> 
> 	if (ioctl(fd, CDROM_LOCKDOOR, lock)) {
> 		perror("ioctl failed");
> 		return 4;
> 	}
> }
> 	

//Written by Shaya Potter <spotter@cs.columbia.edu>
//Placed in the Public Domain

#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <string.h>
#include <libgen.h>

int main(int argc, char *argv[])
{
	int fd;
	int lock;

	char *cmd1;

	cmd1 = basename(argv[0]);

	if (!strcmp(cmd1, "cd-lock"))
		lock = 1;
	else if (!strcmp(cmd1, "cd-unlock"))
		lock = 0;
	else {
		printf("program must be called cd-lock or cd-unlock\n");
		return 1;
	}

	if (argc != 2) {
		printf("not enough options\nUsage:\n\t%s device\n", cmd1);
		return 2;
	}
			

	if ((fd = open(argv[1], O_RDONLY)) <= 0) {
		perror("open failed");
		return 3;
	}

	if (ioctl(fd, CDROM_LOCKDOOR, lock)) {
		perror("ioctl failed");
		return 4;
	}

	return 0;
}
	

Reply to: