vtoytool.c 2.65 KB
Newer Older
longpanda's avatar
longpanda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/******************************************************************************
 * vtoytool.c  ---- ventoy os tool
 *
 * Copyright (c) 2020, longpanda <admin@ventoy.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef int (*main_func)(int argc, char **argv);

typedef struct cmd_def
{
    const char *cmd;
    main_func func;
}cmd_def;

int vtoydump_main(int argc, char **argv);
int vtoydm_main(int argc, char **argv);
int vtoytool_install(int argc, char **argv);
int vtoyloader_main(int argc, char **argv);
longpanda's avatar
longpanda committed
38
int vtoyvine_main(int argc, char **argv);
longpanda's avatar
longpanda committed
39
40
int vtoyksym_main(int argc, char **argv);
int vtoykmod_main(int argc, char **argv);
longpanda's avatar
longpanda committed
41
42
43
44

static char *g_vtoytool_name = NULL;
static cmd_def g_cmd_list[] = 
{
longpanda's avatar
longpanda committed
45
    { "vine_patch_loader",  vtoyvine_main  },
longpanda's avatar
longpanda committed
46
47
48
    { "vtoydump",    vtoydump_main    },
    { "vtoydm",      vtoydm_main      },
    { "loader",      vtoyloader_main  },
longpanda's avatar
longpanda committed
49
50
    { "vtoyksym",    vtoyksym_main  },
    { "vtoykmod",    vtoykmod_main  },
longpanda's avatar
longpanda committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    { "--install",   vtoytool_install },
};


int vtoytool_install(int argc, char **argv)
{
    int i;
    char toolpath[128];
    char filepath[128];
    
    for (i = 0; i < sizeof(g_cmd_list) / sizeof(g_cmd_list[0]); i++)
    {
        if (g_cmd_list[i].cmd[0] != '-')
        {
            snprintf(toolpath, sizeof(toolpath), "/ventoy/tool/%s", g_vtoytool_name);
            snprintf(filepath, sizeof(filepath), "/ventoy/tool/%s", g_cmd_list[i].cmd);
            link(toolpath, filepath);
        }
    }
    
    return 0;
}

int main(int argc, char **argv)
{
    int i;
    
    if ((g_vtoytool_name = strstr(argv[0], "vtoytool")) != NULL)
    {
        argc--;
        argv++;
    }

    if (argc == 0)
    {
        fprintf(stderr, "Invalid param number\n");
        return 1;
    }

    for (i = 0; i < sizeof(g_cmd_list) / sizeof(g_cmd_list[0]); i++)
    {
        if (strstr(argv[0], g_cmd_list[i].cmd))
        {
            return g_cmd_list[i].func(argc, argv);
        }
    }

    fprintf(stderr, "Invalid cmd %s\n", argv[0]);
    return 1;
}