?login_element?

Subversion Repositories NedoOS

Rev

Rev 630 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         STDLIB2.C -- for BDS C v1.6 --  1/86
  3.         Copyright (c) 1982, 1986 by BD Software, Inc.
  4.  
  5.         The files STDLIB1.C, STDLIB2.C and STDLIB3.C contain the source
  6.         listings for all functions present in the DEFF.CRL library object
  7.         file. (Note: DEFF2.CRL contains the .CSM-coded portions of the
  8.         library.)
  9.  
  10.         STDLIB2.C contains mainly formatted text I/O functions:
  11.  
  12.         printf  fprintf sprintf lprintf _spr
  13.         scanf   fscanf  sscanf  _scn
  14.         getline puts
  15.         putdec
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. #define iseof(x) (x==CPMEOF || x==EOF || x==0)
  22.  
  23. char toupper(), isdigit();
  24.  
  25. printf(format)
  26. char *format;
  27. {
  28.         int putchar();
  29.         return _spr(&format, &putchar);
  30. }
  31.  
  32. int fprintf(fp, format)
  33. char *format;
  34. FILE *fp;
  35. {
  36.         int _fputc();
  37.         return _spr(&format, &_fputc, fp);
  38. }
  39.  
  40. sprintf(buffer, format)
  41. char *buffer, *format;
  42. {
  43.         int _sspr();
  44.         _spr(&format, &_sspr, &buffer);
  45.         *buffer = '\0';
  46. }
  47.  
  48. _sspr(c,strptr)
  49. char **strptr;
  50. {
  51.         *(*strptr)++ = c;
  52. }
  53.  
  54. int lprintf(format)
  55. char *format;
  56. {
  57.         int _fputc();
  58.         return _spr(&format, &_fputc, stdlst);
  59. }
  60.  
  61. int _fputc(c,fp)
  62. FILE *fp;
  63. {
  64.         if (c == '\n')
  65.                 if (fputc('\r', fp) == ERROR)
  66.                         return ERROR;
  67.         if (fputc(c, fp) == ERROR)
  68.                 return ERROR;
  69.         return OK;
  70. }      
  71.  
  72. int scanf(format)
  73. char *format;
  74. {
  75.         int getchar(),ungetch();
  76.         return _scn(&format, getchar, ungetch);
  77. }
  78.  
  79. int fscanf(fp,format)
  80. char *format;
  81. FILE *fp;
  82. {
  83.         int fgetc(),ungetc();
  84.         return _scn(&format, fgetc, ungetc, fp);
  85. }
  86.  
  87. int sscanf(line,format)
  88. char *line, *format;
  89. {
  90.         int _sgetc(), _sungetc();
  91.         return _scn(&format, &_sgetc, &_sungetc, &line);
  92. }
  93.  
  94.  
  95. /*
  96.         Internal routine used by "_spr" to perform ascii-
  97.         to-decimal conversion and update an associated pointer:
  98. */
  99.  
  100. int _gv2(sptr)
  101. char **sptr;
  102. {
  103.         int n;
  104.         n = 0;
  105.         while (isdigit(**sptr)) n = 10 * n + *(*sptr)++ - '0';
  106.         return n;
  107. }
  108.  
  109. char _uspr(string, n, base)
  110. char **string;
  111. unsigned n;
  112. {
  113.         char length;
  114.         if (n<base) {
  115.                 *(*string)++ = (n < 10) ? n + '0' : n + 55;
  116.                 return 1;
  117.         }
  118.         length = _uspr(string, n/base, base);
  119.         _uspr(string, n%base, base);
  120.         return length + 1;
  121. }
  122.  
  123.  
  124. int _bc(c,b)
  125. char c,b;
  126. {
  127.         if (isalpha(c = toupper(c)))
  128.                 c -= 55;
  129.         else if (isdigit(c))
  130.                 c -= 0x30;
  131.         else
  132.                 return ERROR;
  133.  
  134.         if (c > b-1)
  135.                 return ERROR;
  136.         else
  137.                 return c;
  138. }
  139.  
  140. _spr(fmt,putcf,arg1)
  141. int (*putcf)();
  142. char **fmt;
  143. {
  144.         char _uspr(), c, base, *format, ljflag, prefill, *wptr;
  145.         char wbuf[20];  /* 20 is enough for all but %s */
  146.         int length, *args, width, precision;
  147.         format = *fmt++;    /* fmt first points to the format string    */
  148.         args = fmt;         /* now fmt points to the first arg value    */
  149.  
  150.         while (c = *format++)
  151.           if (c == '%') {
  152.             wptr = wbuf;
  153.             precision = 32767;
  154.             width = ljflag = 0;
  155.             prefill = ' ';
  156.  
  157.             if (*format == '-') {
  158.                     format++;
  159.                     ljflag=1;
  160.              }
  161.  
  162.             if (*format == '0') prefill = '0';
  163.  
  164.             if (isdigit(*format)) width = _gv2(&format);
  165.  
  166.             if (*format == '.') {
  167.                     format++;
  168.                     precision = _gv2(&format);
  169.              }
  170.  
  171.             if (*format == 'l') format++;       /* no longs here */
  172.  
  173.             switch(c = *format++) {
  174.                 case 'd':  if (*args < 0) {
  175.                                 *wptr++ = '-';
  176.                                 *args = -*args;
  177.                                 width--;
  178.                             }
  179.                 case 'u':  base = 10; goto val;
  180.                 case 'b':  base = 2; goto val;
  181.                 case 'x':  base = 16; goto val;
  182.                 case 'o':  base = 8;
  183.  
  184.                 val:       width -= _uspr(&wptr,*args++,base);
  185.                            goto pad;
  186.  
  187.                 case 'c':  *wptr++ = *args++;
  188.                            width--;
  189.                            goto pad;
  190.  
  191.                 pad:       *wptr = '\0';
  192.                            length = strlen(wptr = wbuf);
  193.                 pad7:           /* don't modify the string at wptr */
  194.                            if (!ljflag)
  195.                                 while (width-- > 0)
  196.                                   if ((*putcf)(prefill,arg1) == ERROR)
  197.                                         return ERROR;;
  198.                            while (length--)
  199.                                 if ((*putcf)(*wptr++,arg1) == ERROR)
  200.                                         return ERROR;
  201.                            if (ljflag)
  202.                                 while (width-- > 0)
  203.                                         if ((*putcf)(' ',arg1) == ERROR)
  204.                                                 return ERROR;
  205.                            break;
  206.  
  207.                 case 's':
  208.                            wptr = *args++;
  209.                            length = strlen(wptr);
  210.                            if (precision < length) length=precision;
  211.                            width -= length;
  212.                            goto pad7;
  213.  
  214.                 case NULL:
  215.                            return OK;
  216.  
  217.                 default:   if ((*putcf)(c,arg1) == ERROR) return ERROR;
  218.              }
  219.           }
  220.           else if ((*putcf)(c,arg1) == ERROR) return ERROR;
  221.         return OK;
  222. }
  223.  
  224. _sgetc(sp)
  225. char **sp;
  226. {
  227.         return *(*sp)++;
  228. }
  229.  
  230. _sungetc(ch,sp)
  231. char **sp,ch;
  232. {
  233.         --*sp;
  234. }
  235.  
  236. union {
  237.         char byte;
  238.         int word;
  239. };
  240.  
  241. int _scn(arglist,get,unget,ioarg)
  242. int (*get)(), (*unget)();
  243. int **arglist;
  244. {
  245.         char assign, c, base, n, *sptr, *format, matchit;
  246.         char shortf;
  247.         int sign, val, width, peek;
  248.  
  249.         format = *arglist++;    /* format points to the format string */
  250.  
  251.         n = 0;
  252.         while (1)
  253.         {
  254.            if (!(c = *format++)) goto alldone;
  255.            if (isspace(c)) continue;    /* skip white space in format string */
  256.            if (c!='%')
  257.            {
  258.                 if(_GetNonWh(get,ioarg,&peek)) goto eofdone;
  259.                 if(c!=peek) goto undone;
  260.            }
  261.            else         /* process conversion */
  262.            {
  263.                 sign = 1; shortf=FALSE;
  264.                 if ('*'==*format) {
  265.                         format++;
  266.                         assign = FALSE;}
  267.                 else assign = TRUE;
  268.                 if (isdigit(*format)) width = _gv2(&format);
  269.                 else width = 32767;
  270.                 if ('l'==*format) format++;             /* no longs */
  271.                 switch (c=*format++) {
  272.                         case 'x': base = 16;    goto doval;
  273.                         case 'o': base = 8;     goto doval;
  274.                         case 'b': base = 2;     goto doval;
  275.                         case 'h': shortf = TRUE;        /* short is char */
  276.                         case 'd':
  277.                         case 'u': base = 10;
  278.                                 doval:
  279.                                 if (_GetNonWh(get,ioarg,&peek))
  280.                                         goto eofdone;
  281.                                 width--;
  282.                                 if (peek=='-') {
  283.                                         sign = -1;
  284.                                         peek=(*get)(ioarg);
  285.                                         if (!width--)
  286.                                                 continue;
  287.                                         }
  288.                                 if ((val=_bc(peek,base)) == ERROR)
  289.                                         goto undone;
  290.                                 while (1) {
  291.                                         peek = (*get)(ioarg);
  292.                                         if (iseof(peek) || !width--)
  293.                                                 break;
  294.                                         if (peek=='x' && c=='x')
  295.                                                 continue;
  296.                                         if ((c = _bc(peek,base)) == 255)
  297.                                                 break;
  298.                                         val = val * base + c;
  299.                                 }
  300.                                 if (assign) {
  301.                                         val *= sign;
  302.                                         if (shortf)
  303.                                                 (*arglist++)->byte = val;
  304.                                         else
  305.                                                 (*arglist++)->word = val;
  306.                                         n++;}
  307.                                 if (iseof(peek))
  308.                                         goto eofdone;
  309.                                 (*unget)(peek,ioarg);
  310.                                 continue;
  311.  
  312.                         case 's':
  313.                                 if (_GetNonWh(get,ioarg,&peek)) goto eofdone;
  314.                                 matchit = ('%'==*format) ? ' ' : *format;
  315.                                 sptr = *arglist;
  316.                                 while (1) {
  317.                                         if (isspace(peek))
  318.                                                 break;
  319.                                         if (peek == matchit) {
  320.                                                 format++;
  321.                                                 break;
  322.                                          }
  323.                                         if (assign) *sptr++ = peek;
  324.                                         if (!--width)
  325.                                                 break;
  326.                                         peek=(*get)(ioarg);
  327.                                         if (iseof(peek))
  328.                                                 break;
  329.                                  }
  330.                                 if (assign) {
  331.                                         n++;
  332.                                         *sptr = '\0';
  333.                                         arglist++;
  334.                                  }
  335.                                 if (iseof(peek))
  336.                                         goto eofdone;
  337.                                 continue;
  338.  
  339.                         case 'c': peek=(*get)(ioarg);
  340.                                 if (iseof(peek))
  341.                                         goto eofdone;
  342.                                 if (assign) {
  343.                                         (*arglist++)->byte = peek;
  344.                                         n++;
  345.                                 }
  346.                                 continue;
  347.  
  348.                         case '%':       /* allow % to be read with "%%" */
  349.                                 if (_GetNonWh(get,ioarg,&peek))
  350.                                         goto eofdone;
  351.                                 if ('%' != peek)
  352.                                         goto undone;
  353.                                 continue;
  354.  
  355.                         default: goto alldone;
  356.                  }
  357.             }
  358.         }
  359.         eofdone:        (*unget)(CPMEOF,ioarg);
  360.                         return n ? n : EOF;
  361.         undone:         (*unget)(peek,ioarg);
  362.         alldone:        return n;
  363. }
  364.  
  365. int _GetNonWh(get,ioarg,apeek)          /* get next non white character */
  366. int (*get)();                                   /* return TRUE if EOF */
  367. int *apeek;
  368. {
  369.         int c;
  370.         while (isspace(c = (*get)(ioarg)))
  371.                 ;
  372.         *apeek = c;
  373.         return iseof(c);
  374. }
  375.  
  376. getline(s,lim)
  377. char s[];
  378. int lim;
  379. {
  380.         int c,i;
  381.         i=0;
  382.         while (--lim>0) {
  383.                 c=getchar();
  384.                 if (c==EOF) break;
  385.                 if (c==CPMEOF) {
  386.                         ungetch(CPMEOF);        /* don't pass ^Z */
  387.                         break;
  388.                         }
  389.                 s[i++] = c;
  390.                 if (c == '\n') break;
  391.         }
  392.         s[i] = '\0';
  393.         return i;
  394. }
  395.  
  396. puts(s)
  397. char *s;
  398. {
  399.         while (*s) putchar(*s++);
  400. }
  401.  
  402. /* Print out a decimal number: */
  403.  
  404. putdec(n)
  405. int n;
  406. {
  407.         if (n < 0)
  408.         {
  409.                 putchar('-');
  410.                 putdec(-n);
  411.                 return;
  412.         }
  413.        
  414.         if (n > 9)
  415.                 putdec(n/10);
  416.  
  417.         putchar(n - n/10*10 + '0');
  418. }
  419.