]>
git.ipfire.org Git - thirdparty/kmod.git/blob - tools/kmod.c
2 * kmod - one tool to rule them all
4 * Copyright (C) 2011 ProFUSION embedded systems
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 static const char options_s
[] = "+hV";
29 static const struct option options
[] = {
30 { "help", no_argument
, NULL
, 'h' },
31 { "version", no_argument
, NULL
, 'V' },
35 static const struct kmod_cmd kmod_cmd_help
;
37 static const struct kmod_cmd
*kmod_cmds
[] = {
41 static const struct kmod_cmd
*kmod_compat_cmds
[] = {
42 &kmod_cmd_compat_lsmod
,
43 &kmod_cmd_compat_rmmod
,
44 &kmod_cmd_compat_insmod
,
47 static int kmod_help(int argc
, char *argv
[])
51 printf("Manage kernel modules: list, load, unload, etc\n"
53 "\t%s [options] command [command_options]\n\n"
55 "\t-V, --version show version\n"
56 "\t-h, --help show this help\n\n"
57 "Commands:\n", basename(argv
[0]));
59 for (i
= 0; i
< ARRAY_SIZE(kmod_cmds
); i
++) {
60 if (kmod_cmds
[i
]->help
!= NULL
) {
61 printf(" %-12s %s\n", kmod_cmds
[i
]->name
,
66 puts("\nkmod also handles gracefully if called from following symlinks:");
68 for (i
= 0; i
< ARRAY_SIZE(kmod_compat_cmds
); i
++) {
69 if (kmod_compat_cmds
[i
]->help
!= NULL
) {
70 printf(" %-12s %s\n", kmod_compat_cmds
[i
]->name
,
71 kmod_compat_cmds
[i
]->help
);
78 static const struct kmod_cmd kmod_cmd_help
= {
81 .help
= "Show help message",
84 static int handle_kmod_commands(int argc
, char *argv
[])
93 c
= getopt_long(argc
, argv
, options_s
, options
, NULL
);
99 kmod_help(argc
, argv
);
102 puts("kmod version " VERSION
);
107 fprintf(stderr
, "Error: unexpected getopt_long() value '%c'.\n", c
);
112 if (optind
>= argc
) {
119 for (i
= 0; i
< ARRAY_SIZE(kmod_cmds
); i
++) {
120 if (strcmp(kmod_cmds
[i
]->name
, cmd
) != 0)
123 err
= kmod_cmds
[i
]->cmd(--argc
, ++argv
);
128 fputs("missing or unknown command; "
129 "see 'kmod help' for a list of available commands\n", stderr
);
136 static int handle_kmod_compat_commands(int argc
, char *argv
[])
142 cmd
= basename(argv
[0]);
144 for (i
= 0; i
< ARRAY_SIZE(kmod_compat_cmds
); i
++) {
145 if (strcmp(kmod_compat_cmds
[i
]->name
, cmd
) != 0)
148 err
= kmod_compat_cmds
[i
]->cmd(argc
, argv
);
154 int main(int argc
, char *argv
[])
156 const char *binname
= basename(argv
[0]);
159 if (strcmp(binname
, "kmod") == 0)
160 err
= handle_kmod_commands(argc
, argv
);
162 err
= handle_kmod_compat_commands(argc
, argv
);