view miscutil/make-imeisv.c @ 964:a96cb97b66a2

ringtools/imy: fix duplicate definition of tdma_durations[] The bug was reported by Vadim Yanitskiy <fixeria@osmocom.org>, although the present fix is slightly different from the contributed patch: because main.c doesn't need this tdma_durations[] array at all, let's simply remove the reference to this array from main.c rather than turn it into an extern. I no longer remember my original thought flow that resulted (by mistake) in tdma_durations[] being multiply defined in main.c and durations.c. My intent might have been to define all globals in main.c and have the reference in durations.c be an extern - and I missed that extern - but without clear memory, I have no certainty. In any case, having this data array defined in the same module that fills it (durations.c) is sensible, so let's make it the new way.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 19:38:18 +0000
parents 2dcfad8a3ed0
children
line wrap: on
line source

/*
 * This utility constructs a 16-digit IMEISV from a 15-digit IMEI
 * (which must have a valid Luhn check digit) and a 2-digit SV field.
 * It is intended for use in shell scripts.
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

parse_imei_arg(input, buf)
	char *input, *buf;
{
	char *cp;
	int i;

	cp = input;
	if (!isdigit(*cp)) {
inv:		fprintf(stderr,
			"error: IMEI argument must have 15 decimal digits\n");
		exit(1);
	}
	for (i = 0; i < 15; i++) {
		if (ispunct(*cp))
			cp++;
		if (!isdigit(*cp))
			goto inv;
		buf[i] = *cp++;
	}
	if (*cp)
		goto inv;
}

check_luhn(digits)
	char *digits;
{
	int i, dig, sum;

	sum = 0;
	for (i = 0; i < 14; i++) {
		dig = digits[i] - '0';
		if (i & 1) {
			dig *= 2;
			if (dig > 9)
				dig -= 9;
		}
		sum += dig;
	}
	dig = sum % 10;
	if (dig)
		dig = 10 - dig;
	if (digits[14] != dig + '0') {
		fprintf(stderr, "error: given IMEI fails Luhn check\n");
		exit(1);
	}
}

main(argc, argv)
	char **argv;
{
	char imeibuf[15];

	if (argc != 3) {
		fprintf(stderr, "usage: %s IMEI SV\n", argv[0]);
		exit(1);
	}
	parse_imei_arg(argv[1], imeibuf);
	check_luhn(imeibuf);
	if (!isdigit(argv[2][0]) || !isdigit(argv[2][1]) || argv[2][2]) {
		fprintf(stderr,
			"error: SV argument must have 2 decimal digits\n");
		exit(1);
	}
	printf("%.8s-%.6s-%s\n", imeibuf, imeibuf + 8, argv[2]);
	exit(0);
}