view simtool/pnnprog.c @ 215:3a2f43460582

fc-simtool pnn-write command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Mar 2021 08:35:57 +0000
parents
children cd66e13eccf3
line wrap: on
line source

/*
 * This module implements functions for admin programming of EF_PNN.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include "curfile.h"

static u_char *
add_field(dp, bytes_avail, namearg, type)
	u_char *dp;
	char *namearg;
	unsigned bytes_avail, type;
{
	u_char gsm7_buf[289];
	unsigned nsept, noct;
	int rc;

	if (bytes_avail < 4) {
		fprintf(stderr,
			"error: PNN record is too short for name element\n");
		return(0);
	}
	rc = qstring_arg_to_gsm7(namearg, gsm7_buf, (bytes_avail-3) * 8 / 7);
	if (rc < 0)
		return(0);
	nsept = rc;
	gsm7_buf[nsept] = 0;
	noct = (nsept * 7 + 7) / 8;
	*dp++ = type;
	*dp++ = noct + 1;
	*dp++ = 0x80 | (nsept & 7);
	gsm7_pack(gsm7_buf, dp, noct);
	dp += noct;
	return dp;
}

cmd_pnn_write(argc, argv)
	char **argv;
{
	int rc;
	unsigned recno;
	u_char record[255], *dp, *endp;

	rc = select_ef_pnn();
	if (rc < 0)
		return(rc);
	recno = strtoul(argv[1], 0, 0);
	if (recno < 1 || recno > curfile_record_count) {
		fprintf(stderr, "error: specified record number is invalid\n");
		return(-1);
	}
	dp = record;
	endp = record + curfile_record_len;
	dp = add_field(dp, endp - dp, argv[2], 0x43);
	if (!dp)
		return(-1);
	if (argv[3]) {
		dp = add_field(dp, endp - dp, argv[3], 0x45);
		if (!dp)
			return(-1);
	}
	while (dp < endp)
		*dp++ = 0xFF;
	return update_rec_op(recno, 0x04, record, curfile_record_len);
}