/*		Q/C V3.2 Utility Library		*/
/*     Copyright (c) 1984 Quality Computer Systems	*/
/*			11/25/84			*/

#include "stdio.h"

#ifdef PORTABLE
/* copy one string to another for exactly n characters */
strncpy(s1, s2, n)
char *s1, *s2;
	{
	char *p;

	p = s1;
	while (--n >= 0) {
		if (*p++ = *s2) /* copy and check for end of s2 */
			++s2;	/* not at end so get next char */
		}
	return s1;
	}
#else
strncpy() {
#	asm 8080
	PUSH	B	;save stack frame
	LXI	H,4
	DAD	SP
	MOV	C,M	;get n
	INX	H
	MOV	B,M
	INX	H
	MOV	E,M	;get s2
	INX	H
	MOV	D,M
	INX	H
	MOV	A,M	;get s1
	INX	H
	MOV	H,M
	MOV	L,A
	PUSH	H	;save s1 for return
?sncy1: MOV	A,C	;check count
	ORA	B	;if zero, we're thru
	JZ	?sncy2
	DCX	B	;count down
	LDAX	D	;next character in s2
	MOV	M,A	;copy to s1
	INX	H
	ORA	A	;end of s2?
	JZ	?sncy1	;if so, pad s1 with zeros
	INX	D	;else, go to next char in s2
	JMP	?sncy1
?sncy2: POP	H	;return s1
	POP	B	;restore stack frame
	MOV	A,H	;set Z flag
	ORA	L
#	endasm
	}
#endif

