/* -----------------------------------------------------:ts=8-------------
 *
 * FPRINTS
 *
 * A simplified version of fprintf, which only supports %s format.
 *
 * Usage: int count = fprints(BPTR struct FileHandle, char *format,
 *                            char *argument, ... );
 * The format string consists of characters which are output, except for
 * the combination `%s' which is replaced by the string pointed to by
 * the corresponding `argument'.
 * A non-recognized character following the % is output literally, so
 * you may use %% to output a single percent sign.
 * The returned value `count' is the number of characters that should
 * have been written, or 0 if an error has been detected.
 * Note that errors are NOT detected.
 *
 * ----------------------------------------------------------------------- */

#define BUFSIZE		80
#define OVERKILL	0

#define PutChar(b,c)		{(b).Buf[(b).Count++]=c;}
#define MayFlushBuffer(b)	{if((b).Count>=BUFSIZE) FlushBuffer(b);}
#define FlushBuffer(b)\
		{Write((b).Fh, &(b).Buf, (long)(b).Count); (b).Count=0;}

typedef struct {
	unsigned short	Count;	    	  /* Number of chars in the buffer */
	long		Fh;		  /* FileHandle assiciated with it */
	char		Buf[BUFSIZE+OVERKILL];
} BUFFER;

static BUFFER Buffer;					 /* NOT re-entrant */

/*VARARGS*/		     /* Does anyone know a nice sweet lint for me? */

int fprints(file, format, arguments)
long file;
register char *format;
char *arguments;
{
	register char **argptr = &arguments;
	register char *argchar;
	register char ch;
	int total = 0;

	Buffer.Fh = file;
	Buffer.Count = 0;

	while (ch = *format++) {     /* Until the end of the format string */
		if ((ch == '%') && ((ch = *format++) == 's')) {
			argchar = *argptr++;
			while (ch = *argchar++) {     /* Copy the argument */
				PutChar(Buffer, ch);
				MayFlushBuffer(Buffer);
				total++;
			}
			continue;
		} else {      /* A normal character: copy it to the buffer */
			PutChar(Buffer, ch);
			MayFlushBuffer(Buffer);
			total++;
		}
	}					/* End while format string */

	if (Buffer.Count)
		FlushBuffer(Buffer);   /* Write out the rest of the buffer */

	return total;
}

#if 0					     /* Currently not used anymore */

int fputs(file, string)
long file;
char *string;
{
	return Write(file, string, strlen(string));
}

#endif
