]> diplodocus.org Git - nmh/blob - sbr/context_find.c
Fix invalid pointer arithmetic.
[nmh] / sbr / context_find.c
1 /* context_find.c -- find an entry in the context/profile list
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9
10
11 char *
12 context_find (const char *str)
13 {
14 struct node *np;
15
16 str = FENDNULL(str);
17 for (np = m_defs; np; np = np->n_next)
18 if (!strcasecmp(FENDNULL(np->n_name), str))
19 return np->n_field;
20
21 return NULL;
22 }
23
24
25 /*
26 * Helper function to search first, if subtype is non-NULL, for
27 * invoname-string-type/subtype and then, if not yet found,
28 * invoname-string-type. If entry is found but is empty, it is
29 * treated as not found.
30 */
31 char *
32 context_find_by_type (const char *string, const char *type,
33 const char *subtype) {
34 char *value = NULL;
35
36 if (subtype) {
37 char *cp;
38
39 cp = concat (invo_name, "-", string, "-", type, "/", subtype, NULL);
40 if ((value = context_find (cp)) != NULL && *value == '\0') value = NULL;
41 free (cp);
42 }
43
44 if (value == NULL) {
45 char *cp;
46
47 cp = concat (invo_name, "-", string, "-", type, NULL);
48 if ((value = context_find (cp)) != NULL && *value == '\0') value = NULL;
49 free (cp);
50 }
51
52 return value;
53 }
54
55
56 /*
57 * Helper function to search profile an entry with name beginning with prefix.
58 * The search is case insensitive.
59 */
60 int
61 context_find_prefix (const char *prefix) {
62 struct node *np;
63 size_t len;
64
65 len = strlen(prefix);
66 for (np = m_defs; np; np = np->n_next) {
67 if (np->n_name && ! strncasecmp (np->n_name, prefix, len)) {
68 return 1;
69 }
70 }
71
72 return 0;
73 }