summaryrefslogtreecommitdiffabout
path: root/src/version.c
blob: e769e310ef3e68b5e41d9d8eff1b49d83de5b9a1 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2016 Sergey Poznyakoff

   Grecs 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 3 of the License, or (at your
   option) any later version.

   Grecs 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 Grecs. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <grecs.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "gitid.h"

const char *grecs_vcs_id = "$Id: " GRECS_GIT_ID " $";

struct grecs_version_info *
grecs_version_split(const char *vstr)
{
	char *p;
	size_t len;
	struct grecs_version_info *pv = grecs_zalloc(sizeof(*pv));
	
	pv->buffer = grecs_strdup(vstr);
	len = strcspn(pv->buffer, " \t");
	if (pv->buffer[len]) {
		pv->package = pv->buffer;
		pv->buffer[len++] = 0;
		for (; pv->buffer[len] && isspace(pv->buffer[len]); len++)
			;
	} else {
		pv->package = NULL;
		len = 0;
	}
	if (!pv->buffer[len]) {
		free(pv->buffer);
		free(pv);
		return NULL;
	}
	pv->version = pv->buffer + len;
	pv->major = strtoul(pv->version, &p, 10);
	if (*p && *p == '.') {
		pv->minor = strtoul(p + 1, &p, 10);
		if (*p && *p == '.') {
			char *q;
			
			pv->patch = strtoul(p + 1, &q, 10);
			if (q == p + 1)
				pv->patch = 0;
			else
				p = q;
		}
	}
	pv->suffix = p;
	return pv;
}

void
grecs_version_info_free(struct grecs_version_info *pv)
{
	if (pv) {
		if (pv->buffer)
			grecs_free(pv->buffer);
		free(pv);
	}
}

struct grecs_version_info *
grecs_version(void)
{
	struct grecs_version_info *pv = grecs_zalloc(sizeof(*pv));
	size_t size = 0;
	
	pv->package = PACKAGE_NAME;
#ifdef GRECS_VERSION_PATCHLEVEL
# ifdef GRECS_VERSION_SUFFIX
	grecs_asprintf(&pv->buffer, &size,
		       "%d.%d.%d%s",
		       GRECS_VERSION_MAJOR, GRECS_VERSION_MINOR,
		       GRECS_VERSION_PATCHLEVEL, GRECS_VERSION_SUFFIX);
# else
	grecs_asprintf(&pv->buffer, &size,
		       "%d.%d.%d",
		       GRECS_VERSION_MAJOR, GRECS_VERSION_MINOR,
		       GRECS_VERSION_PATCHLEVEL);
# endif	
#else
# ifdef GRECS_VERSION_SUFFIX
	grecs_asprintf(&pv->buffer, &size,
		       "%d.%d%s",
		       GRECS_VERSION_MAJOR, GRECS_VERSION_MINOR,
		       GRECS_VERSION_SUFFIX);
# else
	grecs_asprintf(&pv->buffer, &size,
		       "%d.%d",
		       GRECS_VERSION_MAJOR, GRECS_VERSION_MINOR);
# endif
#endif
	pv->version = pv->buffer;
	pv->major = GRECS_VERSION_MAJOR;
	pv->minor = GRECS_VERSION_MINOR;
#ifdef GRECS_VERSION_PATCHLEVEL
	pv->patch = GRECS_VERSION_PATCHLEVEL;
#endif
#ifdef GRECS_VERSION_SUFFIX
	pv->suffix = GRECS_VERSION_SUFFIX;
#endif
	pv->id = GRECS_GIT_ID;
	return pv;
}

int
grecs_version_info_cmp(struct grecs_version_info *vx,
		       struct grecs_version_info *vy,
		       int *pres)
{
	if (vx->package && vy->package && strcmp(vx->package, vy->package))
		return 1;
	else if (vx->major > vy->major)
		*pres = 1;
	else if (vx->major < vy->major)
		*pres = -1;
	else if (vx->minor > vy->minor)
		*pres = 1;
	else if (vx->minor < vy->minor)
		*pres = -1;
	else if (vx->patch > vy->patch)
		*pres = 1;
	else if (vx->patch < vy->patch)
		*pres = -1;
	else if (vx->suffix && vy->suffix)
		*pres = strcmp(vx->suffix, vy->suffix);
	else
		*pres = 0;
	return 0;
}

int
grecs_version_cmp(const char *vstr1, const char *vstr2, int *pres)
{
	struct grecs_version_info *v1, *v2;
	int rc;
	
	if (!vstr1 || !vstr2)
		return -1;
	v1 = grecs_version_split(vstr1);
	if (!v1)
		return -1;
	v2 = grecs_version_split(vstr2);
	if (!v2) {
		grecs_version_info_free(v1);
		return -1;
	}
	rc = grecs_version_info_cmp(v1, v2, pres);
	grecs_version_info_free(v1);
	grecs_version_info_free(v2);
	return rc;
}

int
grecs_version_ok(const char *vstr)
{
	struct grecs_version_info *vmy, *vreq;
	int rc, res;
	
	if (!vstr)
		return -1;
	vreq = grecs_version_split(vstr);
	if (!vreq)
		return -1;
	vmy = grecs_version();
	if (vreq->suffix && !vmy->suffix)
		vmy->suffix = "";
	rc = grecs_version_info_cmp(vmy, vreq, &res);
	grecs_version_info_free(vmy);
	grecs_version_info_free(vreq);
	return rc == 0 && res >= 0;
}

Return to:

Send suggestions and report system problems to the System administrator.