// This may look like C code, but it is really -*- Asm -*-
/*
 *  smoke16/src16/cat.s - copies fd 0 to fd 1
 *  $Revision: 1.7 $
 *  Copyright 1997, 2000, Benjamin Sittler
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define BUFSIZE 1024

#include <smoke16/asm.h>
#include <smoke16/fcntl.h>

	.lcomm buf, BUFSIZE	; // char buf[BUFSIZE];

	.rdata
str0:	.zstring "read"		; // main() {
str1:	.zstring "write"	; //   char *strs[] = {"read", "write"};

	.data
strs:	.word str0
	.word str1

	.text
	.align 1
	FUNCTION(_main)		; //   int bytes;
	push %t0
	push %t1
	mov BUFSIZE, %t0
	mov buf, %t1
loop:	mov %0, %a0		; //   while ((bytes = read(0, buf, BUFSIZE)))
	mov %t1, %a1
	mov %t0, %a2
	call _read
	mov %a0, %a2
	cmp %0, %a0
	be done
	sethi 0, %a3
	add 1, %a0		; //     if (bytes == -1) {
	cmp %0, %a0		; //       perror(strs[0]);
	be error		; // 	   return 1;
	mov %t1, %a1		; //     }
	mov %0, %a0
	add 1, %a0
	call _write		; //     else if (write(1, buf, bytes) == -1) {
	add 1, %a0		; //       perror(strs[1]);
	sethi 2, %a3		; //       return 1;
	cmp %0, %a0		; //     }
	be error
	b loop
error:	srl 8, %a3
	mov strs, %a0
	add %a3, %a0
	mov [%a0], %a0
	call _perror
	mov %0, %a0
	add 1, %a0
done:	pop %t1			; //   return 0;
	pop %t0
	RET			; // }
