view mpffs/cat.c @ 36:390be89892c4

mpffs-xtr ported over
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 30 Jun 2013 17:24:21 +0000
parents ee4c761187cf
children 8256eec598dd
line wrap: on
line source

/*
 * This module contains the main function and other code specific to mpffs-cat
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include "types.h"
#include "struct.h"

extern char *imgfile;
extern int verbose;

cat_chunk(chi)
	struct chunkinfo *chi;
{
	u8 *dp;
	size_t len;
	int c;

	dp = chi->start;
	len = chi->len;
	while (len) {
		c = *dp++;
		if (!verbose || c >= ' ' && c <= '~' || c == '\n')
			putchar(c);
		else {
			if (c & 0x80) {
				putchar('M');
				putchar('-');
				c &= 0x7F;
			}
			putchar('^');
			if (c == 0x7F)
				putchar('?');
			else
				putchar(c + '@');
		}
		len--;
	}
}

cat_file(headidx)
{
	int ent;
	struct objinfo obj;
	struct chunkinfo chi;

	obj.entryno = headidx;
	get_index_entry(&obj);
	if (obj.type != 0xF1) {
		fprintf(stderr,
		"mpffs-cat: the requested FFS object is not a regular file\n");
		exit(1);
	}
	validate_chunk(&obj);
	size_head_chunk(&obj, &chi);
	if (verbose)
		printf("\n--- file content:\n");
	cat_chunk(&chi);
	for (ent = obj.descend; ent != 0xFFFF; ent = obj.descend) {
		obj.entryno = ent;
		get_index_entry(&obj);
		if (obj.type != 0xF4) {
			fprintf(stderr,
	"file continuation object at index %x: type %02X != expected F4\n",
				ent, obj.type);
			exit(1);
		}
		validate_chunk(&obj);
		size_extra_chunk(&obj, &chi);
		cat_chunk(&chi);
	}
	if (verbose)
		printf("\n-- end quote --\n");
}

usage()
{
	fprintf(stderr, "usage: mpffs-cat [options] ffs-image pathname\n");
	exit(1);
}

main(argc, argv)
	char **argv;
{
	extern int optind;
	int idx;

	parse_cmdline_options(argc, argv);
	if (argc - optind != 2)
		usage();
	imgfile = argv[optind];
	preliminaries();
	idx = find_pathname(argv[optind+1]);
	cat_file(idx);
	exit(0);
}