~registry/kmod/master

« back to all changes in this revision

Viewing changes to test/test-lookup.c

  • Committer: Lucas De Marchi
  • Date: 2011-12-12 12:30:01 UTC
  • mfrom: (161.1.30)
  • Revision ID: git-v1:2fee2f13ce5f6b17dd672389de422217c56105e2
Merge branch 'use-mmap'

Conflicts:
        libkmod/libkmod.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#include <inttypes.h>
7
7
#include <string.h>
8
8
#include <libkmod.h>
9
 
 
 
9
#include <getopt.h>
 
10
 
 
11
static const char cmdoptions_short[] = "lh";
 
12
static const struct option cmdoptions[] = {
 
13
        {"load-resources", no_argument, 0, 'l'},
 
14
        {"help", no_argument, 0, 'h'},
 
15
        {NULL, 0, 0, 0}
 
16
};
 
17
 
 
18
static void help(const char *progname)
 
19
{
 
20
        const struct option *itr_opt;
 
21
        const char *itr_short;
 
22
        printf("Usage:\n"
 
23
               "\t%s [options] <name-to-lookup>\n"
 
24
               "Options:\n",
 
25
               progname);
 
26
        for (itr_opt = cmdoptions, itr_short = cmdoptions_short;
 
27
             itr_opt->name != NULL; itr_opt++, itr_short++)
 
28
                printf("\t-%c, --%s\n", *itr_short, itr_opt->name);
 
29
}
10
30
 
11
31
int main(int argc, char *argv[])
12
32
{
13
 
        const char *alias;
 
33
        const char *alias = NULL;
14
34
        struct kmod_ctx *ctx;
15
35
        struct kmod_list *list = NULL, *l;
 
36
        char *options;
 
37
        int load_resources = 0;
16
38
        int err;
17
39
 
18
40
        printf("libkmod version %s\n", VERSION);
19
41
 
20
 
        if (argc < 2) {
 
42
        for (;;) {
 
43
                int c, idx = 0;
 
44
                c = getopt_long(argc, argv, cmdoptions_short, cmdoptions, &idx);
 
45
                if (c == -1)
 
46
                        break;
 
47
                switch (c) {
 
48
                case 'l':
 
49
                        load_resources = 1;
 
50
                        break;
 
51
                case 'h':
 
52
                        help(argv[0]);
 
53
                        return 0;
 
54
                case '?':
 
55
                        return -1;
 
56
                default:
 
57
                        fprintf(stderr,
 
58
                                "ERR: unexpected getopt_long() value %c\n", c);
 
59
                        return -1;
 
60
                }
 
61
        }
 
62
 
 
63
        if (optind >= argc) {
21
64
                fprintf(stderr, "ERR: Provide an alias name\n");
22
65
                return EXIT_FAILURE;
23
66
        }
24
67
 
25
 
        alias = argv[1];
 
68
        alias = argv[optind];
26
69
 
27
 
        ctx = kmod_new(NULL);
28
 
        if (ctx == NULL)
 
70
        ctx = kmod_new(NULL, NULL);
 
71
        if (ctx == NULL) {
 
72
                kmod_unref(ctx);
29
73
                exit(EXIT_FAILURE);
 
74
        }
 
75
 
 
76
        if (load_resources) {
 
77
                err = kmod_load_resources(ctx);
 
78
                if (err < 0) {
 
79
                        printf("Could not load resources: %s\n",
 
80
                               strerror(-err));
 
81
                        kmod_unref(ctx);
 
82
                        exit(EXIT_FAILURE);
 
83
                }
 
84
        }
30
85
 
31
86
        err = kmod_module_new_from_lookup(ctx, alias, &list);
32
87
        if (err < 0)
39
94
 
40
95
        kmod_list_foreach(l, list) {
41
96
                struct kmod_module *mod = kmod_module_get_module(l);
 
97
                const char *str;
 
98
 
42
99
                printf("\t%s\n", kmod_module_get_name(mod));
 
100
                str = kmod_module_get_options(mod);
 
101
                if (str)
 
102
                        printf("\t\toptions: '%s'\n", str);
 
103
                str = kmod_module_get_install_commands(mod);
 
104
                if (str)
 
105
                        printf("\t\tinstall commands: '%s'\n", str);
 
106
                str = kmod_module_get_remove_commands(mod);
 
107
                if (str)
 
108
                        printf("\t\tremove commands: '%s'\n", str);
43
109
                kmod_module_unref(mod);
44
110
        }
45
111
 
 
112
        err = kmod_resolve_alias_options(ctx, alias, &options);
 
113
        if (err == 0) {
 
114
                printf("Alias options: '%s'\n", options);
 
115
                free(options);
 
116
        }
 
117
 
46
118
        kmod_module_unref_list(list);
47
119
        kmod_unref(ctx);
48
120