Fix regression in 1.4.10 where following redirects to relative URLs on
[monitoring-plugins.git] / lib / utils_disk.c
blob9324f5f32d35fd707ccb71158ee3c55543912ae1
1 /****************************************************************************
2 * Utils for check_disk
4 * License: GPL
5 * Copyright (c) 1999-2006 nagios-plugins team
7 * Last Modified: $Date$
9 * Description:
11 * This file contains utilities for check_disk. These are tested by libtap
13 * License Information:
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 2 of the License, or
18 * (at your option) any later version.
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.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * $Id$
31 *****************************************************************************/
33 #include "common.h"
34 #include "utils_disk.h"
36 void
37 np_add_name (struct name_list **list, const char *name)
39 struct name_list *new_entry;
40 new_entry = (struct name_list *) malloc (sizeof *new_entry);
41 new_entry->name = (char *) name;
42 new_entry->next = *list;
43 *list = new_entry;
46 /* Initialises a new parameter at the end of list */
47 struct parameter_list *
48 np_add_parameter(struct parameter_list **list, const char *name)
50 struct parameter_list *current = *list;
51 struct parameter_list *new_path;
52 new_path = (struct parameter_list *) malloc (sizeof *new_path);
53 new_path->name = (char *) name;
54 new_path->best_match = NULL;
55 new_path->name_next = NULL;
56 new_path->freespace_bytes = NULL;
57 new_path->freespace_units = NULL;
58 new_path->freespace_percent = NULL;
59 new_path->usedspace_bytes = NULL;
60 new_path->usedspace_units = NULL;
61 new_path->usedspace_percent = NULL;
62 new_path->usedinodes_percent = NULL;
63 new_path->freeinodes_percent = NULL;
64 new_path->group = NULL;
66 if (current == NULL) {
67 *list = new_path;
68 } else {
69 while (current->name_next) {
70 current = current->name_next;
72 current->name_next = new_path;
74 return new_path;
77 /* Delete a given parameter from list and return pointer to next element*/
78 struct parameter_list *
79 np_del_parameter(struct parameter_list *item, struct parameter_list *prev)
81 struct parameter_list *next;
82 if (item->name_next)
83 next = item->name_next;
84 else
85 next = NULL;
88 free(item);
89 if (prev)
90 prev->name_next = next;
92 return next;
97 /* returns a pointer to the struct found in the list */
98 struct parameter_list *
99 np_find_parameter(struct parameter_list *list, const char *name)
101 struct parameter_list *temp_list;
102 for (temp_list = list; temp_list; temp_list = temp_list->name_next) {
103 if (! strcmp(temp_list->name, name))
104 return temp_list;
107 return NULL;
110 void
111 np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
113 struct parameter_list *d;
114 for (d = desired; d; d= d->name_next) {
115 if (! d->best_match) {
116 struct mount_entry *me;
117 size_t name_len = strlen(d->name);
118 size_t best_match_len = 0;
119 struct mount_entry *best_match = NULL;
121 /* set best match if path name exactly matches a mounted device name */
122 for (me = mount_list; me; me = me->me_next) {
123 if (strcmp(me->me_devname, d->name)==0)
124 best_match = me;
127 /* set best match by directory name if no match was found by devname */
128 if (! best_match) {
129 for (me = mount_list; me; me = me->me_next) {
130 size_t len = strlen (me->me_mountdir);
131 if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
132 (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
133 || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
135 best_match = me;
136 best_match_len = len;
141 if (best_match) {
142 d->best_match = best_match;
143 } else {
144 d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */
150 /* Returns TRUE if name is in list */
152 np_find_name (struct name_list *list, const char *name)
154 const struct name_list *n;
156 if (list == NULL || name == NULL) {
157 return FALSE;
159 for (n = list; n; n = n->next) {
160 if (!strcmp(name, n->name)) {
161 return TRUE;
164 return FALSE;
168 np_seen_name(struct name_list *list, const char *name)
170 const struct name_list *s;
171 for (s = list; s; s=s->next) {
172 if (!strcmp(s->name, name)) {
173 return TRUE;
176 return FALSE;
180 np_regex_match_mount_entry (struct mount_entry* me, regex_t* re)
182 if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 ||
183 regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) {
184 return TRUE;
185 } else {
186 return FALSE;