/* StringOpsImpl.c  */

/* strcpy
 * Copy string s2 to s1.  s1 must be large enough.
 * return s1
 */

asm ("        export StringOps");

asm ("        exportproc ←strcpy, StringOps");
char *
strcpy(s1, s2)
register char *s1, *s2;
{
	register char *os1;

	os1 = s1;
	while (*s1++ = *s2++)
		;
	return(os1);
}

/* strncpy
 * Copy s2 to s1, truncating or null-padding to always copy n bytes
 * return s1
 */

asm ("        exportproc ←strncpy, StringOps");
char *
strncpy(s1, s2, n)
register char *s1, *s2;
{
	register i;
	register char *os1;

	os1 = s1;
	for (i = 0; i < n; i++)
		if ((*s1++ = *s2++) == '\0') {
			while (++i < n)
				*s1++ = '\0';
			return(os1);
		}
	return(os1);
}

/* strcat
 * Concatenate s2 on the end of s1.  S1's space must be large enough.
 * Return s1.
 */

asm ("        exportproc ←strcat, StringOps");
char *
strcat(s1, s2)
register char *s1, *s2;
{
	register char *os1;

	os1 = s1;
	while (*s1++)
		;
	--s1;
	while (*s1++ = *s2++)
		;
	return(os1);
}

/* strncat
 * Concatenate s2 on the end of s1.  S1's space must be large enough.
 * At most n characters are moved.
 * Return s1.
 */

asm ("        exportproc ←strncat, StringOps");
char *
strncat(s1, s2, n)
register char *s1, *s2;
register n;
{
	register char *os1;

	os1 = s1;
	while (*s1++)
		;
	--s1;
	while (*s1++ = *s2++)
		if (--n < 0) {
			*--s1 = '\0';
			break;
		}
	return(os1);
}

/* strcmp
 * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
 */

asm ("        exportproc ←strcmp, StringOps");
strcmp(s1, s2)
register char *s1, *s2;
{

	while (*s1 == *s2++)
		if (*s1++=='\0')
			return(0);
	return(*s1 - *--s2);
}

/* strncmp
 * Compare strings (at most n bytes):  s1>s2: >0  s1==s2: 0  s1<s2: <0
 */

asm ("        exportproc ←strncmp, StringOps");
strncmp(s1, s2, n)
register char *s1, *s2;
register n;
{

	while (--n >= 0 && *s1 == *s2++)
		if (*s1++ == '\0')
			return(0);
	return(n<0 ? 0 : *s1 - *--s2);
}

/* strlen
 * Returns the number of
 * non-NULL bytes in string argument.
 */

asm ("        exportproc ←strlen, StringOps");
strlen(s)
register char *s;
{
	register n;

	n = 0;
	while (*s++)
		n++;
	return(n);
}