|
DataMuseum.dkPresents historical artifacts from the history of: CP/M |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about CP/M Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - download
Length: 6656 (0x1a00) Types: TextFile Names: »STDLIB2.C«
└─⟦23f778bf6⟧ Bits:30005378 BDS C v1.46 & Pascal/MT+ v5.5 (Callan format) └─ ⟦this⟧ »STDLIB2.C« └─⟦4ada80662⟧ Bits:30005446 Pascal/MT+ v5.5 & XREF & BDS C v1.46 └─ ⟦this⟧ »STDLIB2.C«
/* STDLIB2.C -- for BDS C v1.46 -- Leor Zolman, 3/5/82 This file contains the source for the following library functions: printf fprintf sprintf _spr scanf fscanf sscanf _scn fgets puts fputs swapin */ #include "bdscio.h" char toupper(), isdigit(); printf(format) char *format; æ char lineÆMAXLINEÅ; _spr(line,&format); /* use "_spr" to form the output */ puts(line); /* and print out the line */ å 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 fprintf(iobuf,format) char *format; struct _buf *iobuf; æ char textÆMAXLINEÅ; _spr(text,&format); return fputs(text,iobuf); å int fscanf(iobuf,format) char *format; struct _buf *iobuf; æ char textÆMAXLINEÅ; if (!fgets(text,iobuf)) return 0; return _scn(text,&format); å sprintf(buffer,format) char *buffer, *format; æ _spr(buffer,&format); /* call _spr to do all the work */ å int sscanf(line,format) char *line, *format; æ return _scn(line,&format); /* let _scn do all the work */ å _spr(line,fmt) char *line, **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) *line++ = zfflag ? '0' : ' '; while (*line = *wptr++) line++; if (ljflag) while (width-- > 0) *line++ = ' '; break; default: *line++ = c; å å else *line++ = c; *line = 'Ø0'; å /* 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. "line" points to a string containing ascii text to be converted, and "fmt" points to an argument list consisting of first a format string and then a list of pointers to the destination objects. */ 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; struct _buf *iobuf; æ int count, c; char *cptr; count = MAXLINE; 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; struct _buf *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) æ printf("Swapin: cannot open %sØn",name); return ERROR; å if ((read(fd,addr,512)) < 0) æ printf("Swapin: read error on %sØn",name); close(fd); return ERROR; å close(fd); return OK; å «eof»