/***************
*
* g:\exe\txl\src\txlbtm.c
*/
#include "txl.h"

void bottomcut(char *val[])
{
	FILE *fp, *fr;
	int  minus, cnt = 127;
	long tsize;
	char *offs, *line;
	char *tname = "~TXLTMP~.$$$";
	struct stat stbuf;

	fprintf(stderr, "TXL: BOTTOM EXTRACTER\n");
	if (stat(val[0], &stbuf)) {
		fprintf(stderr, "TXL:no exist '%s'", val[0]);
		Exit(1);
	}
	if (stbuf.st_size < 127) {
		cnt = stbuf.st_size;
	}
	if ((line = calloc(cnt + 1, 1)) == NULL) {
		errexit("out of memory");
	}
	if ((fp = fopen(val[0], "rb")) == NULL) {
		free(line);
		fprintf(stderr, "TXL:can't open '%s'", val[0]);
		Exit(1);
	}
	fseek(fp, -(cnt), SEEK_END);
	fread(line, 1, cnt, fp);
	if (line[cnt - 1] == 0x0a) {
		line[cnt - 2] = NUL;
	}
	if (line[cnt - 1] == 0x1a) {
		if (line[cnt - 2] == 0x0a) {
			line[cnt - 3] = NUL;
		}
	}
	if ((offs = strrchr(line, '\n')) == NULL) {
		fclose(fp);
		free(line);
		errexit("bottom line is too long. (more than 80 bytes?)");
	}
	minus = line + cnt - (offs + 1);
	fclose(fp);
	free(line);
	if ((line = malloc(32767)) == NULL) {
		errexit("out of memory");
	}

	strcpy(line1, val[0]);
	if ((offs = jstrrchr(line1, '\\')) != NULL) {
		strcpy(offs+1, tname);
	}
	else if ((offs = jstrrchr(line1, ':')) != NULL) {
		strcpy(offs+1, tname);
	}
	else {
		strcpy(line1, tname);
	}
	rename(val[0], line1);
	tsize = stbuf.st_size - minus;
	if ((fp = fopen(val[0], "wb")) == NULL) {
		free(line);
		fprintf(stderr, "TXL:can't open '%s'", val[0]);
		Exit(1);
	}
	if ((fr = fopen(line1, "rb")) == NULL) {
		free(line);
		fprintf(stderr, "TXL:can't open '%s'", line1);
		Exit(1);
	}
	for (; tsize > 0; tsize -= 32767) {
		cnt = ((tsize > 32767) ? 32767 : tsize);
		cnt = fread(line, 1, cnt, fr);
		fwrite(line, 1, cnt, fp);
	}
	fclose(fp);
	fclose(fr);
	free(line);
	remove(line1);
	Exit(0);
}

void bottomdsp(char *val[], int level)
{
	FILE *fp;
	char *offs, *line;
	struct stat stbuf;
	int cnt = 255;

	fprintf(stderr, "TXL: BOTTOM EXTRACTER\n");
	if (stat(val[0], &stbuf)) {
		fprintf(stderr, "TXL:no exist '%s'", val[0]);
		Exit(1);
	}
	if (stbuf.st_size < 255) {
		cnt = stbuf.st_size;
	}
	if ((line = calloc(cnt + 1, 1)) == NULL) {
		errexit("out of memory");
	}
	if ((fp = fopen(val[0], "rb")) == NULL) {
		free(line);
		fprintf(stderr, "TXL:can't open '%s'", val[0]);
		Exit(1);
	}
	fseek(fp, -(cnt), SEEK_END);
	fread(line1, 1, cnt, fp);
	if (line1[cnt - 1] == 0x0a) {
		line1[cnt - 2] = NUL;
	}
	if (line1[cnt - 1] == 0x1a) {
		if (line1[cnt - 2] == 0x0a) {
			line1[cnt - 3] = NUL;
		}
	}
	if ((offs = strrchr(line1, '\n')) == NULL) {
		fclose(fp);
		free(line);
		errexit("bottom line is too long. (more than 80 bytes)");
	}
	if (level > 1) {
		*(offs - 1) = NUL;
		if ((offs = strrchr(line1, '\n')) == NULL) {
			fclose(fp);
			free(line);
			errexit("before bottom line is too long. (more than 80 bytes)");
		}
	}
	fputs(offs + 1, fpmes);
	fputc('\n', fpmes);
	free(line);
	Exit(0);
}
