changeset 35:ee4c761187cf

mpffs-cat implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 30 Jun 2013 16:55:19 +0000
parents 95f61c3b430a
children 390be89892c4
files .hgignore mpffs/Makefile mpffs/cat.c
diffstat 3 files changed, 107 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Jun 30 16:20:28 2013 +0000
+++ b/.hgignore	Sun Jun 30 16:55:19 2013 +0000
@@ -4,6 +4,7 @@
 
 ^mokosrec2bin$
 
+^mpffs/mpffs-cat$
 ^mpffs/mpffs-dbgls$
 ^mpffs/mpffs-ls$
 
--- a/mpffs/Makefile	Sun Jun 30 16:20:28 2013 +0000
+++ b/mpffs/Makefile	Sun Jun 30 16:55:19 2013 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	mpffs-dbgls mpffs-ls
+PROGS=	mpffs-cat mpffs-dbgls mpffs-ls
 INSTDIR=/usr/local/bin
 
 CAT_OBJS=	common.o cat.o find.o
@@ -10,6 +10,9 @@
 
 all:	${PROGS}
 
+mpffs-cat:	${CAT_OBJS}
+	${CC} -o $@ ${CAT_OBJS}
+
 mpffs-dbgls:	${DBGLS_OBJS}
 	${CC} -o $@ ${DBGLS_OBJS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpffs/cat.c	Sun Jun 30 16:55:19 2013 +0000
@@ -0,0 +1,102 @@
+/*
+ * 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);
+}