view dds.c @ 247:2fc4ac8017af

Instruct Emacs to follow Labwindow CVI coding style
author Daniele Nicolodi <daniele@grinta.net>
date Fri, 27 Feb 2015 18:55:19 +0100
parents fcc988c6f841
children
line wrap: on
line source

#ifdef _CVI_
#include <winsock2.h>
#include <ansi_c.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#endif


int msend(int fd, char *buffer, int n)
{
	buffer[n++] = '\r';
	buffer[n++] = '\n';

	return send(fd, buffer, n, 0);
}


int mrecv(int fd, char *buffer, int len)
{
	int n;

	n = recv(fd, buffer, len, 0);
	if (n < 0)
		return n;

	if ((buffer[--n] != '\n') || (buffer[--n] != '\r'))
		return -1;

	buffer[n] = '\0';

	return n;
}


int command(int fd, char *frmt, ...)
{
	int r, n;
	char buffer[1024];
	va_list v;

	va_start(v, frmt);
	n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v);
	va_end(v);

	r = msend(fd, buffer, n);
	if (r < 0)
		return r;

	r = mrecv(fd, buffer, sizeof(buffer));
	if (r < 0)
		return r;

	if (strcmp(buffer, "OK") != 0)
		return -1;

	return 0;
}