view simtool/pbdump.c @ 9:dc565e91069d

pb-dump meaty function factored out into libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 00:28:06 +0000
parents 4a9bf783491d
children 72a24b8538eb
line wrap: on
line source

/*
 * This module implements pb-dump and pb-dump-rec commands.
 */

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

cmd_pb_dump(argc, argv)
	char **argv;
{
	int rc;
	FILE *outf;
	unsigned recno;

	rc = phonebook_op_common(argv[1]);
	if (rc < 0)
		return(rc);
	if (argv[2]) {
		outf = fopen(argv[2], "w");
		if (!outf) {
			perror(argv[2]);
			return(-1);
		}
	} else
		outf = stdout;
	for (recno = 1; recno <= curfile_record_count; recno++) {
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0) {
			if (argv[2])
				fclose(outf);
			return(rc);
		}
		if (check_simresp_all_blank())
			continue;
		dump_phonebook_record(recno, outf);
	}
	if (argv[2])
		fclose(outf);
	return(0);
}

cmd_pb_dump_rec(argc, argv)
	char **argv;
{
	int rc;
	unsigned recno, startrec, endrec;

	rc = phonebook_op_common(argv[1]);
	if (rc < 0)
		return(rc);
	startrec = strtoul(argv[2], 0, 0);
	if (startrec < 1 || startrec > curfile_record_count) {
		fprintf(stderr,
			"error: specified starting record number is invalid\n");
		return(-1);
	}
	if (argv[3]) {
		endrec = strtoul(argv[3], 0, 0);
		if (endrec < 1 || endrec > curfile_record_count) {
			fprintf(stderr,
			"error: specified final record number is invalid\n");
			return(-1);
		}
		if (startrec > endrec) {
			fprintf(stderr,
				"error: reverse record range specified\n");
			return(-1);
		}
	} else
		endrec = startrec;
	for (recno = startrec; recno <= endrec; recno++) {
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0)
			return(rc);
		if (check_simresp_all_blank())
			continue;
		dump_phonebook_record(recno, stdout);
	}
	return(0);
}