view rvinterf/etmsync/fswrite.c @ 292:3aa03b9519c0

fc-fsio: fwrite hex string implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 01 Mar 2014 03:09:10 +0000
parents 69e8ae2b5ba2
children ffeea2f9d149
line wrap: on
line source

/*
 * FFS write operation commands
 */

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

extern u_char rvi_msg[];
extern int rvi_msg_len;

do_mkdir_existok(pathname)
	char *pathname;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen;
	struct stat_info stat;

	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_MKDIR;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg_len != 5) {
		printf("error: mkdir response has wrong length\n");
		return(ERROR_TARGET);
	}
	if (!rvi_msg[3])	/* success */
		return(0);
	if (rvi_msg[3] != TMFFS_ERR_EXISTS) {
		report_ffs_err("mkdir", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	/* object already exists: OK if it's a directory, error otherwise */
	rc = do_xlstat(pathname, &stat);
	if (rc)
		return(rc);
	if (stat.type == OT_DIR)
		return(0);
	else {
		printf("error: %s exists and is not a directory\n", pathname);
		return(ERROR_TARGET);
	}
}

cmd_mkdir(argc, argv)
	char **argv;
{
	return do_mkdir_existok(argv[1]);
}

cmd_delete(argc, argv)
	char **argv;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen;

	slen = strlen(argv[1]);
	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_REMOVE;
	*dp++ = slen + 1;
	strcpy(dp, argv[1]);
	dp += slen + 1;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg_len != 5) {
		printf("error: TMFFS_REMOVE response has wrong length\n");
		return(ERROR_TARGET);
	}
	if (rvi_msg[3]) {
		report_ffs_err("ffs_remove", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	return(0);
}

hexdigit(c)
{
	if (isdigit(c))
		return(c - '0');
	else if (isupper(c))
		return(c - 'A' + 10);
	else
		return(c - 'a' + 10);
}

fwrite_hex_string(pathname, strarg)
	char *pathname, *strarg;
{
	u_char buf[256];
	int maxlen, len;
	char *cp;

	maxlen = max_short_file_write(pathname);
	for (cp = strarg, len = 0; ; cp += 2) {
		while (isspace(*cp))
			cp++;
		if (!*cp)
			break;
		if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
			fprintf(stderr, "error: invalid hex string argument\n");
			return(ERROR_USAGE);
		}
		if (len >= maxlen) {
			fprintf(stderr,
			"error: hex string exceeds write packet limit\n");
			return(ERROR_USAGE);
		}
		buf[len++] = hexdigit(cp[0]) << 4 | hexdigit(cp[1]);
	}
	return do_short_fwrite(pathname, buf, len);
}

fwrite_from_file(pathname, srcfile)
	char *pathname, *srcfile;
{
	fprintf(stderr, "This function is not yet implemented\n");
	return(ERROR_BUG);
}

cmd_fwrite(argc, argv)
	char **argv;
{
	if (!strcmp(argv[2], "ascii"))
		return do_short_fwrite(argv[1], argv[3], strlen(argv[3]));
	else if (!strcmp(argv[2], "hex"))
		return fwrite_hex_string(argv[1], argv[3]);
	else if (!strcmp(argv[2], "file"))
		return fwrite_from_file(argv[1], argv[3]);
	else {
		fprintf(stderr,
"error: middle argument to fwrite cmd must be \"ascii\", \"hex\" or \"file\"\n"
			);
		return(ERROR_USAGE);
	}
}