view libutil/imsi_entry.c @ 275:def9f6e4f49e default tip

doc/Use-outside-USA: Fake-NANP-numbers article is here
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 Nov 2023 21:49:19 -0800
parents 40e5097437fa
children
line wrap: on
line source

/*
 * The library function implemented in this module supports IMSI entry
 * at UI level, either in the standard form (long string of digits)
 * or in the shorthand notation introduced in fc-sim-tools.
 */

#include <ctype.h>
#include <string.h>
#include <strings.h>

grok_imsi_user_arg(arg, dest)
	char *arg, *dest;
{
	char *cp, *dp;
	int n, tail_len, remain;

	if (!isdigit(*arg))
		return(-1);
	cp = arg;
	dp = dest;
	n = 0;
	while (isdigit(*cp)) {
		if (n >= 15)
			return(-1);
		*dp++ = *cp++;
		n++;
	}
	if (!*cp) {
		if (n < 6)
			return(-1);
		*dp = '\0';
		return(0);
	}
	if (*cp != '-')
		return(-1);
	cp++;
	tail_len = strlen(cp);
	if (!tail_len)
		return(-1);
	remain = 15 - n;
	if (remain < tail_len + 1)
		return(-1);
	while (remain > tail_len) {
		*dp++ = '0';
		remain--;
	}
	while (*cp) {
		if (!isdigit(*cp))
			return(-1);
		*dp++ = *cp++;
	}
	*dp = '\0';
	return(0);
}