c92b85069ea43e9c6b5c089c19cac94d0a772d5f
1 /*****************************************************************************
2 *
3 * Library for check_disk
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains utilities for check_disk. These are tested by libtap
13 *
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 * $Id$
29 *
30 *****************************************************************************/
32 #include "common.h"
33 #include "utils_disk.h"
35 void
36 np_add_name (struct name_list **list, const char *name)
37 {
38 struct name_list *new_entry;
39 new_entry = (struct name_list *) malloc (sizeof *new_entry);
40 new_entry->name = (char *) name;
41 new_entry->next = *list;
42 *list = new_entry;
43 }
45 /* Initialises a new parameter at the end of list */
46 struct parameter_list *
47 np_add_parameter(struct parameter_list **list, const char *name)
48 {
49 struct parameter_list *current = *list;
50 struct parameter_list *new_path;
51 new_path = (struct parameter_list *) malloc (sizeof *new_path);
52 new_path->name = (char *) name;
53 new_path->best_match = NULL;
54 new_path->name_next = NULL;
55 new_path->freespace_bytes = NULL;
56 new_path->freespace_units = NULL;
57 new_path->freespace_percent = NULL;
58 new_path->usedspace_bytes = NULL;
59 new_path->usedspace_units = NULL;
60 new_path->usedspace_percent = NULL;
61 new_path->usedinodes_percent = NULL;
62 new_path->freeinodes_percent = NULL;
63 new_path->group = NULL;
65 if (current == NULL) {
66 *list = new_path;
67 } else {
68 while (current->name_next) {
69 current = current->name_next;
70 }
71 current->name_next = new_path;
72 }
73 return new_path;
74 }
76 /* Delete a given parameter from list and return pointer to next element*/
77 struct parameter_list *
78 np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
79 {
80 struct parameter_list *next;
81 if (item->name_next)
82 next = item->name_next;
83 else
84 next = NULL;
87 free(item);
88 if (prev)
89 prev->name_next = next;
91 return next;
93 }
96 /* returns a pointer to the struct found in the list */
97 struct parameter_list *
98 np_find_parameter(struct parameter_list *list, const char *name)
99 {
100 struct parameter_list *temp_list;
101 for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
102 if (! strcmp(temp_list->name, name))
103 return temp_list;
104 }
106 return NULL;
107 }
109 void
110 np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
111 {
112 struct parameter_list *d;
113 for (d = desired; d; d= d->name_next) {
114 if (! d->best_match) {
115 struct mount_entry *me;
116 size_t name_len = strlen(d->name);
117 size_t best_match_len = 0;
118 struct mount_entry *best_match = NULL;
120 /* set best match if path name exactly matches a mounted device name */
121 for (me = mount_list; me; me = me->me_next) {
122 if (strcmp(me->me_devname, d->name)==0)
123 best_match = me;
124 }
126 /* set best match by directory name if no match was found by devname */
127 if (! best_match) {
128 for (me = mount_list; me; me = me->me_next) {
129 size_t len = strlen (me->me_mountdir);
130 if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
131 (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
132 || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
133 {
134 best_match = me;
135 best_match_len = len;
136 }
137 }
138 }
140 if (best_match) {
141 d->best_match = best_match;
142 } else {
143 d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
144 }
145 }
146 }
147 }
149 /* Returns TRUE if name is in list */
150 int
151 np_find_name (struct name_list *list, const char *name)
152 {
153 const struct name_list *n;
155 if (list == NULL || name == NULL) {
156 return FALSE;
157 }
158 for (n = list; n; n = n->next) {
159 if (!strcmp(name, n->name)) {
160 return TRUE;
161 }
162 }
163 return FALSE;
164 }
166 int
167 np_seen_name(struct name_list *list, const char *name)
168 {
169 const struct name_list *s;
170 for (s = list; s; s=s->next) {
171 if (!strcmp(s->name, name)) {
172 return TRUE;
173 }
174 }
175 return FALSE;
176 }
178 int
179 np_regex_match_mount_entry (struct mount_entry* me, regex_t* re)
180 {
181 if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
182 regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
183 return TRUE;
184 } else {
185 return FALSE;
186 }
187 }