1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define TOOL(name) int name##_main(int, char**);
- #include "tools.h"
- #undef TOOL
- static struct {
- const char* name;
- int (*func)(int, char**);
- } tools[] = {
- #define TOOL(name) { #name, name##_main },
- #include "tools.h"
- #undef TOOL
- { 0, 0 },
- };
- static void SIGPIPE_handler(int signal) {
-
-
-
- _exit(0);
- }
- int main(int argc, char** argv) {
-
-
-
-
- signal(SIGPIPE, SIGPIPE_handler);
- char* cmd = strrchr(argv[0], '/');
- char* name = cmd ? (cmd + 1) : argv[0];
- for (size_t i = 0; tools[i].name; i++) {
- if (!strcmp(tools[i].name, name)) {
- return tools[i].func(argc, argv);
- }
- }
- printf("%s: no such tool\n", argv[0]);
- return 127;
- }
- int toolbox_main(int argc, char** argv) {
-
- if (argc > 1) {
- return main(argc - 1, argv + 1);
- }
-
- for (size_t i = 1; tools[i].name; i++) {
- printf("%s%c", tools[i].name, tools[i+1].name ? ' ' : '\n');
- }
- return 0;
- }
|