
1000 '
1010 '/*  :
1020 '    :   chk 用ライブラリ    (Runser用)
1030 '    :
1040 '    :   標準ライブラリのいくつかをサポートする
1050 '    :
1060 '    :   filename "chklib.c"
1070 '    :
1080 '*/
1090 '
1100 '#define stdin   0
1110 '#define stdout  1
1120 '#define stderr  2
1130 '#define stdprt  3
1140 '
1150 'typedef char    FILE;
1160 '
1170 '#define NFIL    2
1180 'static          _FSflg[NFIL] = { 0,0 };
1190 'FILE            *_FSfcb[NFIL];
1200 'unsigned char   _FSbuf[304*NFIL];
1210 '
1220 '#undef  SLEN
1230 '#define SLEN    256
1240 '/* #define DEBUG */
1250 ' 
1260 'FILE *fopen(fname,md)
1270 ' char *fname,*md;
1280 '{   int     c,i;
1290 '    int     rw,er;
1300 '    FILE    *fp;
1310 '
1320 '    rw = 0;
1330 '    for( i=0 ; i<NFIL ; ++i )
1340 '    {   if( _FSflg[i]==0 )
1350 '        {   _FSflg[i] = -1;
1360 '            fp = (int)_FSbuf+i*304;
1370 '            strfil(fp,304,0);       /* Clear work area */
1380 '#ifdef  DEBUG
1390 '            printf("fcb %04x , %04x\n",_FSbuf,fp);
1400 '#endif
1410 '            goto Open;
1420 '        }
1430 '    }
1440 '#ifdef  DEBUG
1450 '    print ("@ Flg Err\n");
1460 '#endif
1470 '    return 0;   /* error */
1480 'Open:
1490 '    while( c = *md++ )
1500 '    {   if( c=='r' )
1510 '            rw += 1;
1520 '        else if( c=='w' )
1530 '            rw += 2;
1540 '    }
1550 '    switch( rw )
1560 '    {   case 1: /* read */
1570 '            fp[32] = 1;
1580 '            er = open(fname,fp);
1590 '            break;
1600 '        case 2: /* write */
1610 '            fp[11] = 0;    /* タイプナンバ */
1620 '            fp[12] =-1;    /* ASCII */
1630 '            fp[15] = 0;    /* ファイル属性の指定 */
1640 '            fp[25] = 0x00; /* エディションナンバの指定 */
1650 '            fp[32] = 2;
1660 '            er = open(fname,fp);
1670 '            break;
1680 '        default:
1690 '#ifdef  DEBUG
1700 '            print ("@ Mode Err\n");
1710 '#endif
1720 '            return( 0 );
1730 '    }
1740 '    return( er==ERR ? 0 : (int)fp );
1750 '}
1760 '
1770 'char _fprt = FALSE;
1780 '
1790 'putc(c,fp)
1800 ' int c;
1810 ' FILE *fp;
1820 '{   char p;
1830 '
1840 '#ifdef  DEBUG
1850 '    printf("putc c = %02x ,(fp=%04x)\n",c,fp);
1860 '#endif
1870 '    switch( (int)fp )
1880 '    {   case stdin:
1890 '            return( -1 );
1900 '        case stdout:
1910 '        case stderr:
1920 '            Pflg = FALSE;
1930 '            putchar( c );
1940 '            break;
1950 '        case stdprt:
1960 '            if( _fprt==FALSE )
1970 '            {   if( prtinit()==ERR )
1980 '                {   print ("@ Printer not ready!");
1990 '                    exit(0);
2000 '                }
2010 '                _fprt = TRUE;
2020 '            }
2030 '            Pflg = TRUE;
2040 '            putchar( c );
2050 '            break;
2060 '        default:
2070 '            p = c;
2080 '            write(fp,&p,1);
2090 '    }
2100 '}
2110 '
2120 'fprintf(fp,cs)
2130 ' FILE *fp;
2140 ' STRING cs;
2150 '{   char    buf[SLEN];
2160 '    char    *p;
2170 '
2180 '#ifdef  DEBUG
2190 '    printf("fprintf (fp=%04x)\n",fp);
2200 '#endif
2210 '    _sprintf(buf,cs,(int *)&cs+1);
2220 '    switch( (int)fp )
2230 '    {   case stdout:
2240 '        case stderr:
2250 '            Pflg = 0;
2260 '            print (buf);
2270 '            break;
2280 '        case stdprt:
2290 '            if( _fprt==FALSE )
2300 '            {   if( prtinit()==ERR )
2310 '                {   print ("@ Printer not ready!");
2320 '                    exit(0);
2330 '                }
2340 '                _fprt = TRUE;
2350 '            }
2360 '            Pflg = TRUE;
2370 '            printf(buf);
2380 '            break;
2390 '        default:
2400 '            p = buf;
2410 '            while( *p )
2420 '                write(fp,p++,1);
2430 '    }
2440 '}
2450 '
2460 'fgets(buf,n,fp)
2470 ' char   *buf;
2480 ' int    n;
2490 ' FILE   *fp;
2500 '{   int     c,i;
2510 '    char    ch;
2520 '
2530 '    switch( (int)fp )
2540 '    {   case stdin:
2550 '            i = input(buf,n);
2560 '            return( i<0 ? 0 : (int)buf+i );
2570 '        case stdout:
2580 '        case stderr:
2590 '        case stdprt:
2600 '            return( 0 );
2610 '        default:
2620 '#ifdef  DEBUG
2630 '        printf("fgets (fp=%04x)\n",fp);
2640 '#endif
2650 '        --n;
2660 '        for(i = 0 ; i<n ; ++i )
2670 '        {   if( (c=read(fp,&ch,1))==ERR )
2680 '            {   *buf = '\0';
2690 '                return( 0 );    /* ERROR */
2700 '            }
2710 '            if( c==EOF || ch==0x1A )        /* EOF */
2720 '            {   *buf = '\0';
2730 '                return( i ? 0x1A : 0 );
2740 '            }
2750 '            if( ch=='\b' )                  /* Back Scape */
2760 '            {   if( i )
2770 '                {   --buf;
2780 '                    --i;
2790 '                }
2800 '                --i;
2810 '                continue;
2820 '            }
2830 '            if( (*buf++ = ch)=='\n')
2840 '                break;
2850 '        }
2860 '        *buf++ = '\0';
2870 '        return( buf );
2880 '    }
2890 '}
2900 '
2910 'fclose( fp )
2920 ' FILE   *fp;
2930 '{   int     i;
2940 '
2950 '    if( fp!=stdin && fp!=stdout && fp!=stderr && fp!=stdprt )
2960 '    {   i = ((int)_FSbuf-(int)fp)/304;
2970 '        close( fp );
2980 '        _FSflg[i] = 0;
2990 '    }
3000 '}
3010 '
3020 'isspace( c )
3030 ' int     c;
3040 '{   return( c==' ' || c=='\n' || c=='\t' );
3050 '}
3060 '
