/************************************************************************/
/*                                                                      */
/* Copyright (c) 1988 -                                                 */
/*     The Rector and Visitors of the University of Virginia            */
/*                                                                      */
/* This code may not be distributed further without permission          */
/*      from the University of Virginia.                                */
/* This code is distributed WITHOUT ANY WARRANTY.  No claims are        */
/*      made as to whether it serves any particular purpose or even     */
/*      works at all.                                                   */
/*                                                                      */
/************************************************************************/

#include <stdio.h>
#include "size.h"
#include "runtime.h"

extern int _ARGC;
extern char **_ARGV;
extern void free(), RT_fclose2();

#define CHMASK 0xff		/* prevent sign extension */

int
RT_str_cmp(s1, s2, n)
	register char *s1, *s2;
	register int n;
{
	register int ret;
	while (n--) {
		if (ret = (*s1++ & CHMASK) - (*s2++ & CHMASK))
			return(ret);
	}
	return(ret);
}

char *
RT_new(size)
	unsigned size;
{
	extern char *malloc();
	char *ret;
	if ((ret = malloc(size)) == NULL)
		RT_abort("no more memory available for \"new()\"");
	return(ret);
}

RT_dispose(p, size)
	register char *p;
	int size;
{
	if (p) {
		if (size != -1)    /* disposing a type that has a file */
			RT_fclose2((FILEAREA*)p, (FILEAREA*)(p+size-1));
		free(p);
	}
	else
		RT_abort("nil pointer passed to \"dispose()\"");
}

int
RT_argc()
{
	return (_ARGC);
}

RT_argv(idx, string, leng)
	int idx, leng;
	char *string;
{
	int i;
	char *p;
	if (idx < 0 || idx >= _ARGC)
		RT_abort("\"argv()\" could not fetch non-existent argument");
	leng--;
	p = _ARGV[idx];
	for (i = 0; i < leng; i++)
		*string++ = *p ? *p++ : '\0';
	*string = '\0';
}
