File: milestone.c

package info (click to toggle)
uptimed 1%3A0.4.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: ansic: 2,684; sh: 217; perl: 166; makefile: 51
file content (140 lines) | stat: -rw-r--r-- 3,023 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
/*
uptimed - Copyright (c) 1998-2004 Rob Kaper <rob@unixcode.org>
        - read_uptime code for BSD and Solaris platforms taken from upclient
          package by Martijn Broenland.

   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; version 2 dated June, 1991.

   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., 675 Mass Ave., Cambridge, MA 02139, USA.
*/

#include "milestone.h"

Milestone *milestone_list = NULL;
static Milestone *milestone_last = NULL;

Milestone *add_milestone(time_t time, char *desc)
{
	Milestone *m, *tmpm, *mprev=NULL;

	/* Allocate memory for the new entry. */	
	if ((m=malloc(sizeof(Milestone))) == NULL)
	{
		printf("error mallocing milestone struct. this is serious shit! exiting.\n");
		exit(1);
	}

	/* Copy boottime, uptime and systeminfo into memory. */
	m->time = time;
	strncpy(m->desc, desc, SYSMAX);
	m->desc[SYSMAX]='\0';

	/* Add the entry to the linked list. */
	for(tmpm=milestone_list;tmpm;tmpm=tmpm->next)
	{
		if (m->time < tmpm->time)
		{
			m->next=tmpm;
			if (tmpm==milestone_list) return milestone_list = m;
			else return mprev->next = m;
		}
		else mprev = tmpm;
	}

	m->next = NULL;
	if (milestone_last) milestone_last->next = m;
	else milestone_list = m;
	return milestone_last = m;
}

void del_milestone(Milestone *m)
{
	Milestone *tmpm=milestone_list;
	
	if (m==milestone_list)
	{
		milestone_list=m->next;
		if (!milestone_list) milestone_last = NULL;
	}
	else
	{
		for (tmpm=milestone_list; tmpm->next && m!=tmpm->next; tmpm=tmpm->next);
		if (!m->next) milestone_last = tmpm;
		tmpm->next = m->next;
	}
	free(m);
}

Milestone *find_next_milestone(time_t offset)
{
	Milestone *m;

	for (m=milestone_list;m && m->time < offset;m=m->next);

	if (m)
		return m;
	else
		return NULL;	 
}

time_t scantime(char *str) {
	char	*end;
	size_t	 len;
	time_t	 multiplier;

	/* Find the last char in string. */
	len = strlen(str);
	end = str + len - 1;

 	if (isdigit(*end))
 	{
 		/* It's a digit, so input was in seconds. */
 		multiplier = 1;
	}
	else
	{
		switch(tolower(*end))
		{
			case 's':
				/* Seconds. */
				multiplier = 1;
				break;

			case 'h':
				/* Hours */
				multiplier = 3600;
				break;

			case 'd':
				/* Days. */
				multiplier = 86400;
				break;

			case 'w':
				/* Weeks. */
				multiplier = 7 * 86400;
				break;

			case 'y':
				/* Solar years. */
				multiplier = (time_t) (365.24219 * 86400.00);
				break;

			default:
				/* Garbage. */
				multiplier = 0;
				break;
		}
		*end = '\0';		/* remove the time factor byte. */
	}
	return atol(str)*multiplier;
}