Contents of /trunk/mkinitrd-magellan/klibc/usr/kinit/getarg.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 1378 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 1378 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | #include <string.h> |
2 | #include "kinit.h" |
3 | |
4 | /* |
5 | * Routines that hunt for a specific argument. Please note that |
6 | * they actually search the array backwards. That is because on the |
7 | * kernel command lines, it's legal to override an earlier argument |
8 | * with a later argument. |
9 | */ |
10 | |
11 | /* |
12 | * Was this boolean argument passed? If so return the index in the |
13 | * argv array for it. For conflicting boolean options, use the |
14 | * one with the higher index. The only case when the return value |
15 | * can be equal, is when they're both zero; so equality can be used |
16 | * as the default option choice. |
17 | * |
18 | * In other words, if two options "a" and "b" are opposites, and "a" |
19 | * is the default, this can be coded as: |
20 | * |
21 | * if (get_flag(argc,argv,"a") >= get_flag(argc,argv,"b")) |
22 | * do_a_stuff(); |
23 | * else |
24 | * do_b_stuff(); |
25 | */ |
26 | int get_flag(int argc, char *argv[], const char *name) |
27 | { |
28 | int i; |
29 | |
30 | for (i = argc-1; i > 0; i--) { |
31 | if (!strcmp(argv[i], name)) |
32 | return i; |
33 | } |
34 | return 0; |
35 | } |
36 | |
37 | /* |
38 | * Was this textual parameter (foo=option) passed? |
39 | * |
40 | * This returns the latest instance of such an option in the argv array. |
41 | */ |
42 | char *get_arg(int argc, char *argv[], const char *name) |
43 | { |
44 | int len = strlen(name); |
45 | char *ret = NULL; |
46 | int i; |
47 | |
48 | for (i = argc-1; i > 0; i--) { |
49 | if (argv[i] && strncmp(argv[i], name, len) == 0 && |
50 | (argv[i][len] != '\0')) { |
51 | ret = argv[i] + len; |
52 | break; |
53 | } |
54 | } |
55 | |
56 | return ret; |
57 | } |