/*
  Program to unpack an ANSI D-Format file into stream format.
  Christine Gianone, Columbia U, Jan 88.
*/

#include <stdio.h>
#include <ctype.h>

main() {
    char buf[10001];			/* Record buffer */
    int n;				/* Length indicator */

    while ((n = readlen()) > -1) {	/* Read a length field. */
	buf[0] = '\0';			/* Clear record buffer. */
	if (n > 0) {			/* If record not null, */
	    read(0,buf,n);		/*  read it, */
	    buf[n] = '\0';		/*  null-terminate it. */
	}
	puts(buf);			/* Write it out, with newline. */
    }
}

readlen() {				/* Function to read length field. */
    int x, n = 0, i;
    char c;

    for (i = 0; i < 4; i++) {		/* Read 4-character length field. */
	x = read(0,&c,1);		/* Read one character. */
	if (x < 1) {			/* If we got an error, give up. */
	    return(-1);
	}
	if ((c == '^') && (i == 0)) {	/* Skip past leading circumflexes. */
	    i--;
	    continue;
	}
	if (!isdigit(c)) {		/* Make sure character is a digit. */
	    fprintf(stderr,"%s","File not in ANSI D format\n");
	    return(-1);
	}
	n = (10 * n) + (c - '0');	/* Convert to number. */
    }
    return(n-4);			/* Subtract 4 from length and return */
}
