1: #include <sys/types.h> 2: #include <sys/stat.h> 3: #include <stdio.h> 4: #include "fio.h" 5: #define STR(x) (x==NULL?"":x) 6: 7: /*global definitions*/ 8: unit units[MXUNIT]; /*unit table*/ 9: flag init; /*0 on entry, 1 after initializations*/ 10: cilist *elist; /*active external io list*/ 11: flag reading; /*1 if reading, 0 if writing*/ 12: flag cplus,cblank; 13: char *fmtbuf; 14: flag external; /*1 if external io, 0 if internal */ 15: int (*doed)(),(*doned)(); 16: int (*doend)(),(*donewrec)(),(*dorevert)(); 17: flag sequential; /*1 if sequential io, 0 if direct*/ 18: flag formatted; /*1 if formatted io, 0 if unformatted*/ 19: int (*getn)(),(*putn)(); /*for formatted io*/ 20: FILE *cf; /*current file*/ 21: unit *curunit; /*current unit*/ 22: int recpos; /*place in current record*/ 23: int cursor,scale; 24: 25: /*error messages*/ 26: char *F_err[] 27: { 28: "error in format", 29: "illegal unit number", 30: "formatted io not allowed", 31: "unformatted io not allowed", 32: "direct io not allowed", 33: "sequential io not allowed", 34: "can't backspace file", 35: "null file name", 36: "can't stat file", 37: "unit not connected", 38: "off end of record", 39: "truncation failed in endfile", 40: "incomprehensible list input", 41: "out of free space", 42: "unit not connected", 43: "read unexpected character", 44: "blank logical input field", 45: }; 46: #define MAXERR (sizeof(F_err)/sizeof(char *)+100) 47: fatal(n,s) char *s; 48: { 49: if(n<100 && n>=0) perror(s); /*SYSDEP*/ 50: else if(n>=(int)MAXERR) 51: { fprintf(stderr,"%s: illegal error number %d\n",s,n); 52: } 53: else if(n<0) fprintf(stderr,"%s: end of file %d\n",s,n); 54: else 55: fprintf(stderr,"%s: %s\n",s,F_err[n-100]); 56: fprintf(stderr,"apparent state: unit %d named %s\n",curunit-units, 57: STR(curunit->ufnm)); 58: fprintf(stderr,"last format: %s\n",STR(fmtbuf)); 59: fprintf(stderr,"lately %s %s %s %s IO\n",reading?"reading":"writing", 60: sequential?"sequential":"direct",formatted?"formatted":"unformatted", 61: external?"external":"internal"); 62: _cleanup(); 63: abort(); 64: } 65: /*initialization routine*/ 66: f_init() 67: { unit *p; 68: init=1; 69: p= &units[0]; 70: p->ufd=stderr; 71: p->useek=canseek(stderr); 72: p->ufmt=1; 73: p->uwrt=1; 74: p = &units[5]; 75: p->ufd=stdin; 76: p->useek=canseek(stdin); 77: p->ufmt=1; 78: p->uwrt=0; 79: p= &units[6]; 80: p->ufd=stdout; 81: p->useek=canseek(stdout); 82: p->ufmt=1; 83: p->uwrt=1; 84: } 85: canseek(f) FILE *f; /*SYSDEP*/ 86: { struct stat x; 87: fstat(fileno(f),&x); 88: if(x.st_nlink > 0 /*pipe*/ && !isatty(fileno(f))) 89: { 90: return(1); 91: } 92: return(0); 93: } 94: nowreading(x) unit *x; 95: { 96: long loc; 97: x->uwrt=0; 98: loc=ftell(x->ufd); 99: freopen(x->ufnm,"r",x->ufd); 100: fseek(x->ufd,loc,0); 101: } 102: nowwriting(x) unit *x; 103: { 104: long loc; 105: loc=ftell(x->ufd); 106: x->uwrt=1; 107: freopen(x->ufnm,"a",x->ufd); 108: fseek(x->ufd,loc,0); 109: }