All pastes #459347 Raw Edit

speckal

public c v1 · immutable
#459347 ·published 2007-04-26 12:27 UTC
rendered paste body
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <signal.h>#define ELO_EVENT_PATH "/dev/input/event3"#define BYTES_PER_LINE 10#define MAX_READ_ERRORS 10//#ifdef NDEBUG//#define assert(c,s)//#else//#define assert(c,s) { if (!c) { fprintf(stderr,"ASSERTION FAILED '%.32s': %.1024s\n",##c,s); exit(-1); }//#endifvolatile int _userAbort;void PrintByte(char byte){	static int xPos = 0;	++xPos;	char trailingChar;	if (xPos == BYTES_PER_LINE)	{		xPos = 0;		trailingChar = '\n';	}	else	{		trailingChar = ' ';	}		char printChar = (isprint(byte) ? byte : ' ');		printf ("0x%02hhX %c   %c", byte, printChar, trailingChar);}void SignalHandler(int signal){	_userAbort = 1;}int main(){//	FILE * fpElo = fopen(ELO_EVENT_PATH, "rb");//	if (fpElo == NULL)	int fdElo = open(ELO_EVENT_PATH, O_RDONLY | O_SYNC | O_NOCTTY);	if (fdElo == -1)	{		perror ("Open ELO event node failed");		return (-1);	}	int errorCount = 0;	putchar ('\n');	_userAbort = 0;	signal (SIGTERM, SignalHandler);	signal (SIGINT, SignalHandler);	signal (SIGQUIT, SignalHandler);	while (_userAbort == 0)	{		char bytes[8];		//int bytesRead = fread(&byte, 1, 1, fpElo);		int bytesRead = read(fdElo, bytes, 8);		if (bytesRead > 0)		{			PrintByte (bytes[0]);		}		else if (bytesRead == -1)		{//			fprintf (stderr, "fread error %i\n", error);			perror ("Read ELO event node failed");			++errorCount;			if (errorCount == MAX_READ_ERRORS)			{				break;			}		}		else		{			fprintf(stderr,"read() returned 0");		}	}	signal (SIGTERM, SIG_DFL);	signal (SIGINT, SIG_DFL);	signal (SIGQUIT, SIG_DFL);	putchar ('\n');	//fclose (fpElo);	close (fdElo);	return (0);}