1#include <stdio.h>
2#include <string.h>
3#include <fcntl.h>
4#include <unistd.h>
5#include <malloc.h>
6#include <errno.h>
7#include <asm/unistd.h>
8
9extern int delete_module(const char *, unsigned int);
10
11int rmmod_main(int argc, char **argv)
12{
13	int ret, i;
14	char *modname, *dot;
15
16	/* make sure we've got an argument */
17	if (argc < 2) {
18		fprintf(stderr, "usage: rmmod <module>\n");
19		return -1;
20	}
21
22	/* if given /foo/bar/blah.ko, make a weak attempt
23	 * to convert to "blah", just for convenience
24	 */
25	modname = strrchr(argv[1], '/');
26	if (!modname)
27		modname = argv[1];
28	else modname++;
29
30	dot = strchr(argv[1], '.');
31	if (dot)
32		*dot = '\0';
33
34	/* Replace "-" with "_". This would keep rmmod
35	 * compatible with module-init-tools version of
36	 * rmmod
37	 */
38	for (i = 0; modname[i] != '\0'; i++) {
39		if (modname[i] == '-')
40			modname[i] = '_';
41	}
42
43	/* pass it to the kernel */
44	ret = delete_module(modname, O_NONBLOCK | O_EXCL);
45	if (ret != 0) {
46		fprintf(stderr, "rmmod: delete_module '%s' failed (errno %d)\n",
47						modname, errno);
48		return -1;
49	}
50
51	return 0;
52}
53
54