File: timeout.c

package info (click to toggle)
noflushd 2.7.5-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 792 kB
  • ctags: 302
  • sloc: ansic: 2,537; sh: 1,142; makefile: 85
file content (178 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download | duplicates (5)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * <disk_info.c>
 *
 * Copyright (C) 2000 Daniel Kobras
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * $Id: timeout.c,v 1.6 2001/08/07 22:51:23 nold Exp $
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "timeout.h"
#include "bug.h"

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

struct tolist_s {
	tolist_t	next;
	tolist_t	prev;
	int             timeout;
};

static tolist_t default_to_head=NULL;

static tolist_t append_timeout(tolist_t head, int timeout)
{
	tolist_t entry = malloc(sizeof(struct tolist_s));
	
	if (!entry)
		return NULL;

	entry->timeout = timeout;
	
	if (!head) {
		entry->next = entry->prev = entry;
	} else {
		entry->next = head;
		entry->prev = head->prev;
		head->prev->next = entry;
		head->prev = entry;
	}
	
	return entry;
}

static void free_timeouts(tolist_t head)
{
	tolist_t tmp, t;

	if (!head)
		return;
	
	t = head;
	
	do {
		tmp = t;
		t = t->next;
		free(tmp);

	} while (t != head);
}
			
void timeout_advance_default(void)
{
	if (default_to_head)
		default_to_head=default_to_head->next;
}

/* If timeout was previously 'skip', this routine returns the new timeout
 * value automatically as long as 'skip' corresponds to timeout==0!
 */
#if NFD_TO_SKIP != 0
#error Bummer. NFD_TO_SKIP changed and broke timeout_advance.
#endif
int timeout_advance(tolist_t *head)
{
	int to_old, to_new;
	
	if (!(*head))
		return default_to_head->timeout-default_to_head->prev->timeout;
	
	to_old = (*head)->timeout;
	if (to_old == NFD_TO_USE_DEFAULT)
		to_old = default_to_head->prev->timeout;

	*head = (*head)->next;
	
	to_new = (*head)->timeout;
	if (to_new == NFD_TO_USE_DEFAULT)
		to_new = default_to_head->timeout;

	return to_new - to_old;
}

int timeout_get(tolist_t head)
{
	if (head && head->timeout != NFD_TO_USE_DEFAULT)
		return head->timeout;
	
	/* Return default timeout */

	if (!default_to_head)
		return -1;	/* Error: no default timeout! */
	
	return default_to_head->timeout;
}

/* Take a comma separated list of timeout values and store them in
 * a tolist.
 */
tolist_t timeout_parse(tolist_t head, char *args, int is_default)
{
	char *cur, *tmp;
	int timeout;

	tmp = strdup(args);
	
	do {
		cur = strrchr(tmp, ',');
		if (cur) 
			*cur++ = '\0'; 
		else
			cur = tmp;
		
		if (!strcmp(cur, "default"))
			timeout = NFD_TO_USE_DEFAULT;
		else if (!strcmp(cur, "skip"))
			timeout = NFD_TO_SKIP;
		else 
			timeout = atoi(cur);
		
		if (timeout > 60*timeout && timeout != NFD_TO_USE_DEFAULT) {
			ERR("Illegal timeout value %d", timeout);
			goto err;
		}
		if (is_default && timeout == NFD_TO_USE_DEFAULT) {
			ERR("Illegal default timeout %d", timeout);
			goto err;
		}
		if (NFD_TO_IS_REGULAR(timeout)) 
			timeout *= 60;
		
		head = append_timeout(head, timeout);

	} while (cur != tmp);

	if (is_default)
		default_to_head=head;

	free(tmp);
	return head;
err:
	free_timeouts(head);
	free(tmp);
	return NULL;
}