summaryrefslogtreecommitdiff
path: root/src/driver/slbt_version_info.c
blob: 5b280752d3bc2e82ee72cc6c813189e8c8a455e0 (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
/*******************************************************************/
/*  slibtool: a skinny libtool implementation, written in C        */
/*  Copyright (C) 2016--2021  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>

#include <slibtool/slibtool.h>
#include "slibtool_version.h"
#include "slibtool_driver_impl.h"
#include "slibtool_errinfo_impl.h"
#include "argv/argv.h"


int slbt_init_version_info(
	struct slbt_driver_ctx_impl *	ictx,
	struct slbt_version_info *	verinfo)
{
	int	current;
	int	revision;
	int	age;
	int	colons;
	int	fmtcnt;
	const char * ch;

	if (!verinfo->verinfo && !verinfo->vernumber)
		return 0;

	if (verinfo->vernumber) {
		sscanf(verinfo->vernumber,"%d:%d:%d",
			&verinfo->major,
			&verinfo->minor,
			&verinfo->revision);
		return 0;
	}

	current = revision = age = 0;

	for (colons=0, ch=verinfo->verinfo; *ch; ch++)
		if (*ch == ':')
			colons++;

	fmtcnt = sscanf(verinfo->verinfo,"%d:%d:%d",
		&current,&revision,&age);

	if (!fmtcnt || (fmtcnt > 3) || (fmtcnt != colons + 1)) {
		slbt_dprintf(ictx->fdctx.fderr,
			"%s: error: invalid version info: "
			"supported argument format is %%d[:%%d[:%%d]].\n",
			slbt_program_name(ictx->cctx.targv[0]));
		return -1;
	}

	if (current < age) {
		if (ictx->cctx.drvflags & SLBT_DRIVER_VERBOSITY_ERRORS)
			slbt_dprintf(ictx->fdctx.fderr,
				"%s: error: invalid version info: "
				"<current> may not be smaller than <age>.\n",
				slbt_program_name(ictx->cctx.targv[0]));
		return -1;
	}

	verinfo->major    = current - age;
	verinfo->minor    = age;
	verinfo->revision = revision;

	return 0;
}