Annotation of researchv10dc/libI77/old/err.c, revision 1.1

1.1     ! root        1: /*     @(#)err.c       1.3     */
        !             2: /*     3.0 SID #       1.3     */
        !             3: #include <sys/types.h>
        !             4: #include <sys/stat.h>
        !             5: #include <stdio.h>
        !             6: #include "fio.h"
        !             7: #define STR(x) (x==NULL?"":x)
        !             8: 
        !             9: /*global definitions*/
        !            10: unit units[MXUNIT];    /*unit table*/
        !            11: flag init;     /*0 on entry, 1 after initializations*/
        !            12: cilist *elist; /*active external io list*/
        !            13: flag reading;  /*1 if reading, 0 if writing*/
        !            14: flag cplus,cblank;
        !            15: char *fmtbuf;
        !            16: flag external; /*1 if external io, 0 if internal */
        !            17: int (*doed)(),(*doned)();
        !            18: int (*doend)(),(*donewrec)(),(*dorevert)();
        !            19: flag sequential;       /*1 if sequential io, 0 if direct*/
        !            20: flag formatted;        /*1 if formatted io, 0 if unformatted*/
        !            21: int (*getn)(),(*putn)();       /*for formatted io*/
        !            22: FILE *cf;      /*current file*/
        !            23: unit *curunit; /*current unit*/
        !            24: int recpos;    /*place in current record*/
        !            25: int cursor,scale;
        !            26: 
        !            27: /*error messages*/
        !            28: char *F_err[] =
        !            29: {
        !            30:        "error in format",                              /* 100 */
        !            31:        "illegal unit number",                          /* 101 */
        !            32:        "formatted io not allowed",                     /* 102 */
        !            33:        "unformatted io not allowed",                   /* 103 */
        !            34:        "direct io not allowed",                        /* 104 */
        !            35:        "sequential io not allowed",                    /* 105 */
        !            36:        "can't backspace file",                         /* 106 */
        !            37:        "null file name",                               /* 107 */
        !            38:        "can't stat file",                              /* 108 */
        !            39:        "unit not connected",                           /* 109 */
        !            40:        "off end of record",                            /* 110 */
        !            41:        "truncation failed in endfile",                 /* 111 */
        !            42:        "incomprehensible list input",                  /* 112 */
        !            43:        "out of free space",                            /* 113 */
        !            44:        "unit not connected",                           /* 114 */
        !            45:        "read unexpected character",                    /* 115 */
        !            46:        "blank logical input field",                    /* 116 */
        !            47:        "bad variable type",                            /* 117 */
        !            48:        "bad namelist name",                            /* 118 */
        !            49:        "variable not in namelist",                     /* 119 */
        !            50:        "no end record",                                /* 120 */
        !            51:        "variable count incorrect"                      /* 121 */
        !            52: };
        !            53: #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
        !            54: fatal(n,s) char *s;
        !            55: {
        !            56:        if(n<100 && n>=0) perror(s); /*SYSDEP*/
        !            57:        else if(n>=(int)MAXERR)
        !            58:        {       fprintf(stderr,"%s: illegal error number %d\n",s,n);
        !            59:        }
        !            60:        else if(n<0) fprintf(stderr,"%s: end of file %d\n",s,n);
        !            61:        else
        !            62:                fprintf(stderr,"%s: %s\n",s,F_err[n-100]);
        !            63:        fprintf(stderr,"apparent state: unit %d named %s\n",curunit-units,
        !            64:                STR(curunit->ufnm));
        !            65:        fprintf(stderr,"last format: %s\n",STR(fmtbuf));
        !            66:        fprintf(stderr,"lately %s %s %s %s IO\n",reading?"reading":"writing",
        !            67:                sequential?"sequential":"direct",formatted?"formatted":"unformatted",
        !            68:                external?"external":"internal");
        !            69:        _cleanup();
        !            70:        abort();
        !            71: }
        !            72: /*initialization routine*/
        !            73: f_init()
        !            74: {      unit *p;
        !            75:        init=1;
        !            76:        p= &units[0];
        !            77:        p->ufd=stderr;
        !            78:        p->useek=canseek(stderr);
        !            79:        if(isatty(fileno(stderr))) {
        !            80:                setbuf(stderr, malloc(BUFSIZ));
        !            81:                setvbuf(stderr, _IOLBF, 0, 0);
        !            82:        }       /* wastes space, but win for debugging in windows */
        !            83:        p->ufmt=1;
        !            84:        p->uwrt=1;
        !            85:        p = &units[5];
        !            86:        p->ufd=stdin;
        !            87:        p->useek=canseek(stdin);
        !            88:        p->ufmt=1;
        !            89:        p->uwrt=0;
        !            90:        p= &units[6];
        !            91:        p->ufd=stdout;
        !            92:        p->useek=canseek(stdout);
        !            93:        /* IOLBUF and setvbuf only in system 5+ */
        !            94:        if(isatty(fileno(stdout))) {
        !            95:                extern char * _sobuf;
        !            96:                setbuf(stdout, _sobuf);
        !            97:                setvbuf(stdout, _IOLBF, 0, 0);  /* the buf arg in setvbuf? */
        !            98:                p->useek = 1;   /* only within a record no bigger than BUFSIZ */
        !            99:        }
        !           100:        p->ufmt=1;
        !           101:        p->uwrt=1;
        !           102: }
        !           103: canseek(f) FILE *f; /*SYSDEP*/
        !           104: {      struct stat x;
        !           105:        if(fstat(fileno(f),&x) < 0)
        !           106:                return(0);
        !           107:        switch(x.st_mode & S_IFMT) {
        !           108:        case S_IFDIR:
        !           109:        case S_IFREG:
        !           110:                if(x.st_nlink > 0)      /* !pipe */
        !           111:                        return(1);
        !           112:                else
        !           113:                        return(0);
        !           114:        case S_IFCHR:
        !           115:                if(isatty(fileno(f)))
        !           116:                        return(0);
        !           117:                return(1);
        !           118:        case S_IFBLK:
        !           119:                return(1);
        !           120:        }
        !           121:        return(0);      /* who knows what it is? */
        !           122: }
        !           123: nowreading(x) unit *x;
        !           124: {
        !           125:        long loc;
        !           126:        x->uwrt=0;
        !           127:        loc=ftell(x->ufd);
        !           128:        if(freopen(x->ufnm,"r",x->ufd) == NULL)
        !           129:                return(1);
        !           130:        (void) fseek(x->ufd,loc,0);
        !           131:        return(0);
        !           132: }
        !           133: nowwriting(x) unit *x;
        !           134: {
        !           135:        long loc;
        !           136:        loc=ftell(x->ufd);
        !           137:        x->uwrt=1;
        !           138:        if(!freopen(x->ufnm,"r+",x->ufd) && !freopen(x->ufnm, "w+", x->ufd))
        !           139:                return(1);
        !           140:        (void) fseek(x->ufd,loc,0);
        !           141:        return(0);
        !           142: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.