/*
 *  makembr -- ログを埋めこんだMBRを生成
 *
 *  revision history:
 *	0.0: Aug.  1, 2006 by Dai ISHIJIMA (for wpout07)
 *	0.1: Sep. 12, 2007 (for wpout09, FreeBSD 6.2R)
 */

#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdlib.h>

#define STDIN 0
#define STDOUT 1
#define STDERR 2

#ifndef EOF
#define EOF -1
#endif

#define NEWLINE	'\n'
#define CRET	'\r'

#define MBRSIZ	512
#define MESGLEN	398
#define TBLOFF	0x01be
#define MESG	(TBLOFF - MESGLEN)
#define MAXLINE 50

/* 一文字入力 */
int getch(int fd)
{
    unsigned char ch;
    
    if (read(fd, &ch, 1) <= 0) {
	return(EOF);
    }
    return(ch);
}


int main()
{
    unsigned char mbrbuf[MBRSIZ];
    int ch;
    int len;
    int pos;

    /* とりあえず512バイト読む */
    read(STDIN, mbrbuf, MBRSIZ);
    /* その次からが書き込むログ */
    len = 0;
    pos = 0;
    while ((len < MESGLEN) && ((ch = getch(STDIN)) != EOF)) {
	if (ch == NEWLINE) {
	    mbrbuf[MESG + len] = CRET;
	    ++len;
	    mbrbuf[MESG + len] = NEWLINE;
	    ++len;
	    pos = 0;
	}
	else if (pos < MAXLINE) {
	    mbrbuf[MESG + len] = ch;
	    ++len;
	    ++pos;
	}
	else if (pos == MAXLINE) {
	    mbrbuf[MESG + len] = '.';
	    ++len;
	    ++pos;
	    mbrbuf[MESG + len] = '.';
	    ++len;
	    ++pos;
	    mbrbuf[MESG + len] = '.';
	    ++len;
	    ++pos;
	}
    }
    if (len < MESGLEN) {
	mbrbuf[MESG + len] = 0;
    }
    write(STDOUT, mbrbuf, MBRSIZ);
    exit(0);
}

/* Local Variables: */
/* compile-command:"cc -Wall -O -s -static -o makembr makembr.c" */
/* End: */
