view mpffs/cat.c @ 167:c25367bb7656

objgrep: written, compiles
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 04 Jul 2014 00:54:33 +0000
parents 7ceab8bfacb3
children
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;

int cat_v;

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

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

void
extra_chunk_callback(obj, opaque)
	struct objinfo *obj;
	u_long opaque;
{
	struct chunkinfo chi;

	size_extra_chunk(obj, &chi);
	cat_chunk(&chi);
}

cat_file(headidx)
{
	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);
	cat_chunk(&chi);
	iterate_extra_chunks(obj.descend, extra_chunk_callback, (u_long) 0);
	if (cat_v)
		putchar('\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();
	if (verbose) {
		cat_v = 1;
		verbose--;
	}
	imgfile = argv[optind];
	preliminaries();
	idx = find_pathname(argv[optind+1]);
	cat_file(idx);
	exit(0);
}