/*
 * Blame server.  Assigns blame to the person of your choice.
 *
 * Usage: blame
 *	(reads one line from standard input)
 *
 * To compile:
 *	gcc blame.c -o blame
 *
 * Install under inetd as follows:
 *  blame	stream	tcp	nowait	root	/path/to/blame	blame
 *
 * Copyright 2009 by Feckless C. Coder, PostDoc.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_BUFFER 256  /* maximum name size */

int buffer[INPUT_BUFFER]; /* storing input data */

/*
 * read input, copy into p 
 * gets() is insecure and prints a warning
 *    so we use this instead
 */
void saveinput(int *p)
{
	int c;
	
	while ((c=getchar()) != EOF)
		*p++ = c;
	*p = EOF;
}

/*
 * read data from p to s
 */
void getline(char *s, int *p)
{
	int c;
	
	for(; (c=*p++)!= EOF; *s++ = c);
	*s = '\0';
}

/*
 * convert newlines to nulls in place
 */
void purgenewlines(char *s)
{
	int l;

	l = strlen(s);

	while (l--)
		if (s[l] == '\n')
			s[l] = '\0';
}

/*
 * copy data from p to scapegoat
 *   print on the screen after reformatting
 */
int loop(int *p)
{
	char scapegoat[INPUT_BUFFER];

	getline (scapegoat, p);
	/* this check ensures there's no buffer overflow */
	if (strlen(scapegoat) < INPUT_BUFFER) {
		purgenewlines(scapegoat);
		printf("It's all %s's fault.\n", scapegoat);
		return 1;
	} else {
		return 0;
        }
}

/*
 * save input into buffer, then start loop
 */
int main()
{
        saveinput(buffer);
	while(loop(buffer));
	return 0;
}

