summaryrefslogtreecommitdiffstats
path: root/usr/kinit/getarg.c
blob: fcce247efb2a4c359d8ad44e07bfe3bff27bbaa2 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string.h>
#include "kinit.h"

/*
 * Routines that hunt for a specific argument.  Please note that
 * they actually search the array backwards.  That is because on the
 * kernel command lines, it's legal to override an earlier argument
 * with a later argument.
 */

/*
 * Was this boolean argument passed?  If so return the index in the
 * argv array for it.  For conflicting boolean options, use the
 * one with the higher index.  The only case when the return value
 * can be equal, is when they're both zero; so equality can be used
 * as the default option choice.
 *
 * In other words, if two options "a" and "b" are opposites, and "a"
 * is the default, this can be coded as:
 *
 * if (get_flag(argc,argv,"a") >= get_flag(argc,argv,"b"))
 * 	do_a_stuff();
 * else
 *	do_b_stuff();
 */
int get_flag(int argc, char *argv[], const char *name)
{
	int i;

	for (i = argc-1; i > 0; i--) {
		if (!strcmp(argv[i], name))
			return i;
	}
	return 0;
}

/*
 * Was this textual parameter (foo=option) passed?
 *
 * This returns the latest instance of such an option in the argv array.
 */
char *get_arg(int argc, char *argv[], const char *name)
{
	int len = strlen(name);
	char *ret = NULL;
	int i;

	for (i = argc-1; i > 0; i--) {
		if (argv[i] && strncmp(argv[i], name, len) == 0 &&
		    (argv[i][len] != '\0')) {
			ret = argv[i] + len;
			break;
		}
	}

	return ret;
}