DataMuseum.dk

Presents historical artifacts from the history of:

CP/M

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about CP/M

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦550c0aea3⟧ TextFile

    Length: 7168 (0x1c00)
    Types: TextFile
    Names: »STDLIB2.C«

Derivation

└─⟦b35f94715⟧ Bits:30003295 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
└─⟦b35f94715⟧ Bits:30005324 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
    └─ ⟦this⟧ »STDLIB2.C« 

TextFile

/*
	STDLIB2.C -- for BDS C v1.50 	12/29/82
	Copyright (c) 1982 by Leor Zolman

	This file contains the following library functions sources:

	printf 		lprintf	*	fprintf		sprintf		_spr
	scanf		fscanf		sscanf		_scn
	fgets
	puts		fputs
	swapin

	* --  << Not yet documented in the v1.50 User's Guide. >>

	Note:	The maximum output line length for all formatted output
		functions is now unlimited, due to some new technology,
		although the limit of any one "format conversion" is still
		MAXLINE characters.

*/

#include <bdscio.h>

#define DEV_LST 2		/* List device for lprintf */

char toupper(), isdigit();

printf(format)
char *format;
æ
	int putchar();
	return _spr(&format, &putchar);	/* use "_spr" to form the output */
å

int lprintf(format)
char *format;
æ
	int _fputc();
	return _spr(&format, &_fputc, DEV_LST);
å

sprintf(buffer,format)
char *buffer, *format;
æ
	int _sspr();
	_spr(&format, &_sspr, &buffer);	/* call _spr to do all the work */
	*buffer = 'Ø0';
å

_sspr(c,strptr)
char **strptr;
æ
	*(*strptr)++ = c;
å

int fprintf(iobuf,format)
char *format;
FILE *iobuf;
æ
	int _fputc();
	return _spr(&format, &_fputc, iobuf);
å

int _fputc(c,iobuf)
æ
	if (c == 'Øn')
		if (putc('Ør',iobuf) == ERROR)
			return ERROR;
	if (putc(c,iobuf) == ERROR)
		return ERROR;
	return OK;
å	

int scanf(format)
char *format;
æ
	char lineÆMAXLINEÅ;
	gets(line);			/* get a line of input from user */
	return _scn(line,&format);	/* and scan it with "_scn"	 */
å

int sscanf(line,format)
char *line, *format;
æ
	return _scn(line,&format);	/* let _scn do all the work */
å


int fscanf(iobuf,format)
char *format;
FILE *iobuf;
æ
	char textÆMAXLINEÅ;
	if (!fgets(text,iobuf))
		return ERROR;
	return _scn(text,&format);
å


_spr(fmt,putcf,arg1)
int (*putcf)();
char **fmt;
æ
	char _uspr(), c, base, *sptr, *format;
	char wbufÆMAXLINEÅ, *wptr, pf, ljflag, zfflag;
	int width, precision,  *args;

	format = *fmt++;    /* fmt first points to the format string	*/
	args = fmt;	    /* now fmt points to the first arg value	*/

	while (c = *format++)
	  if (c == '%') æ
	    wptr = wbuf;
	    precision = 6;
	    ljflag = pf = zfflag = 0;

	    if (*format == '-') æ
		    format++;
		    ljflag++;
	     å


	    if (*format == '0') zfflag++;	/* test for zero-fill */

	    width = (isdigit(*format)) ? _gv2(&format) : 0;

	    if ((c = *format++) == '.') æ
		    precision = _gv2(&format);
		    pf++;
		    c = *format++;
	     å

	    switch(toupper(c)) æ

		case 'D':  if (*args < 0) æ
				*wptr++ = '-';
				*args = -*args;
				width--;
			    å

		case 'U':  base = 10; goto val;

		case 'X':  base = 16; goto val;

		case 'O':  base = 8;  /* note that arbitrary bases can be
				         added easily before this line */

		     val:  width -= _uspr(&wptr,*args++,base);
			   goto pad;

		case 'C':  *wptr++ = *args++;
			   width--;
			   goto pad;

		case 'S':  if (!pf) precision = 200;
			   sptr = *args++;
			   while (*sptr && precision) æ
				*wptr++ = *sptr++;
				precision--;
				width--;
			    å

		     pad:  *wptr = 'Ø0';
		     pad2: wptr = wbuf;
			   if (!ljflag)
				while (width-- > 0)
					if ((*putcf)(zfflag ? '0' : ' ',arg1)
						== ERROR) return ERROR;;

			   while (*wptr)
				if ((*putcf)(*wptr++,arg1) == ERROR) 
					return ERROR;

			   if (ljflag)
				while (width-- > 0)
					if ((*putcf)(' ',arg1) == ERROR)
						return ERROR;
			   break;

		case NULL:
			   return OK;

		default:   if ((*putcf)(c,arg1) == ERROR)
				return ERROR;
	     å
	  å
	  else if ((*putcf)(c,arg1) == ERROR)
			return ERROR;
	return OK;
å

/*
	Internal routine used by "_spr" to perform ascii-
	to-decimal conversion and update an associated pointer:
*/

int _gv2(sptr)
char **sptr;
æ
	int n;
	n = 0;
	while (isdigit(**sptr)) n = 10 * n + *(*sptr)++ - '0';
	return n;
å

char _uspr(string, n, base)
char **string;
unsigned n;
æ
	char length;
	if (n<base) æ
		*(*string)++ = (n < 10) ? n + '0' : n + 55;
		return 1;
	å
	length = _uspr(string, n/base, base);
	_uspr(string, n%base, base);
	return length + 1;
å


/*
	General formatted input conversion routine:
*/

int _scn(line,fmt)
char *line, **fmt;
æ
	char sf, c, base, n, *sptr, *format;
	int sign, val, **args;

	format = *fmt++;	/* fmt first points to the format string */
	args = fmt;		/* now it points to the arg list */

	n = 0;
	while (c = *format++)
	æ
	   if (isspace(c)) continue;	/* skip white space in format string */
	   if (c != '%')		/* if not %, must match text */
	    æ
		if (c != _igs(&line)) return n;
		else line++;
	    å
	   else		/* process conversion */
	    æ
		sign = 1;
		base = 10;
		sf = 0;
		if ((c = *format++) == '*')
		 æ
			sf++;		/* if "*" given, supress assignment */
			c = *format++;
		 å
		switch (toupper(c))
		 æ
		   case 'X': base = 16;
			     goto doval;

		   case 'O': base = 8;
			     goto doval;

		   case 'D': if (_igs(&line) == '-') æ
				sign = -1;
				line++;
			      å

	   doval:  case 'U': val = 0;
			     if (_bc(_igs(&line),base) == ERROR)
				return n;
			     while ((c = _bc(*line++,base)) != 255)
				val = val * base + c;
			     line--;
			     break;

		   case 'S': _igs(&line);
			     sptr = *args;
			     while (c = *line++)   æ
				if (c == *format) æ
					format++;
					break;
				 å
				if (!sf) *sptr++ = c;
			      å				
			     if (!sf) æ
				n++;
				*sptr = 'Ø0';
				args++;
			      å
			     continue;

		   case 'C': if (!sf) æ
				poke(*args++, *line);
				n++;
			     å
			     line++;
			     continue;

		   default:  return n;
		 å
		if (!sf)
		 æ
			**args++ = val * sign;
			n++;
		 å
	    å
	   if (!*line) return n;	/* if end of input string, return */
	å
	return n;
å

char _igs(sptr)
char **sptr;
æ
	char c;
	while (isspace(c = **sptr)) ++*sptr;
	return (c);
å

int _bc(c,b)
char c,b;
æ
	if (isalpha(c = toupper(c))) c -= 55;
         else  if (isdigit(c))  c -= 0x30;
	 else return ERROR;
	if (c > b-1) return ERROR;
		else return c;
å

puts(s)
char *s;
æ
	while (*s) putchar(*s++);
å


char *fgets(s,iobuf)
char *s;
FILE *iobuf;
æ
	int count, c;
	char *cptr;
	count = MAXLINE - 2;
	cptr = s;
	if ( (c = getc(iobuf)) == CPMEOF øø c == EOF) return NULL;

	do æ
		if ((*cptr++ = c) == 'Øn') æ
		  if (cptr>s+1 && *(cptr-2) == 'Ør')
			*(--cptr - 1) = 'Øn';
		  break;
		å
	 å while (count-- && (c=getc(iobuf)) != EOF && c != CPMEOF);

	if (c == CPMEOF) ungetc(c,iobuf);	/* push back control-Z */
	*cptr = 'Ø0';
	return s;
å


fputs(s,iobuf)
char *s;
FILE *iobuf;
æ
	char c;
	while (c = *s++) æ
		if (c == 'Øn') putc('Ør',iobuf);
		if (putc(c,iobuf) == ERROR) return ERROR;
	å
	return OK;
å

swapin(name,addr)
char *name;
æ
	int fd;
	if (( fd = open(name,0)) == ERROR)
		return ERROR;

	if ((read(fd,addr,512)) < 0) æ
		close(fd);
		return ERROR;
	å

	close(fd);
	return OK;
å

«eof»