view rvinterf/etmsync/fsbasics.c @ 279:36ad667341fc

fc-fsio: ls implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 24 Feb 2014 07:27:37 +0000
parents f77480d3dd21
children 517e8a428fde
line wrap: on
line source

/*
 * Basic FFS2 operations
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "etm.h"
#include "ffs.h"
#include "tmffs2.h"
#include "limits.h"
#include "exitcodes.h"

extern u_char rvi_msg[];
extern int rvi_msg_len;

cmd_ffs2ver()
{
	u_char cmdpkt[4];
	int rc;

	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_VERSION;
	rc = etm_pkt_exch(cmdpkt, 2);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		printf("FFS2 error %d\n", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 7) {
		printf("error: FFS2 version response has wrong length\n");
		return(ERROR_TARGET);
	}
	printf("FFS2 version: %02X.%02X\n", rvi_msg[5], rvi_msg[4]);
	return(0);
}

do_opendir(pathname, statertn, countrtn)
	char *pathname;
	u_char *statertn;
	int *countrtn;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen;

	slen = strlen(pathname);
	if (slen >= TMFFS_STRING_SIZE) {
		printf("error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_OPENDIR;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		printf("opendir: FFS error %d\n", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 11 || rvi_msg[5] != 4) {
		printf("error: opendir response has wrong length\n");
		return(ERROR_TARGET);
	}
	*countrtn = rvi_msg[4];
	bcopy(rvi_msg + 6, statertn, 4);
	return(0);
}

do_readdir(state, namebuf)
	u_char *state;
	char *namebuf;
{
	u_char cmdpkt[10];
	int rc, slen;

	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_READDIR;
	cmdpkt[3] = 4;
	bcopy(state, cmdpkt+4, 4);
	cmdpkt[8] = TMFFS_STRING_SIZE;
	rc = etm_pkt_exch(cmdpkt, 8);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		printf("readdir: FFS error %d\n", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len < 14) {
malformed:	printf("error: readdir response is malformed\n");
		return(ERROR_TARGET);
	}
	if (rvi_msg[5] != 4)
		goto malformed;
	slen = rvi_msg[10];
	if (slen < 2 || slen > TMFFS_STRING_SIZE)
		goto malformed;
	if (rvi_msg[11 + slen - 1])	/* must be terminating NUL */
		goto malformed;
	bcopy(rvi_msg + 6, state, 4);
	strcpy(namebuf, rvi_msg + 11);
	return(0);
}

cmd_ls(argc, argv)
	char **argv;
{
	u_char state[4];
	char namebuf[TMFFS_STRING_SIZE];
	int nument, i, rc;

	rc = do_opendir(argv[1], state, &nument);
	if (rc)
		return(rc);
	if (!nument) {
		printf("<empty dir>\n");
		return(0);
	}
	for (i = 0; i < nument; i++) {
		rc = do_readdir(state, namebuf);
		if (rc)
			return(rc);
		printf("%s\n", namebuf);
	}
	return(0);
}