2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
12 static char rcs_id[] = "$Id: ass80.c,v 2.9 1994/06/24 10:15:23 ceriel Exp $" ;
16 * this file contains several library routines.
19 zero(area,length) char *area; unsigned length ; {
23 * Clear area of length bytes.
28 do *p++=0; while (--n);
32 static void pr_error(string1,a1,a2,a3,a4) char *string1 ; {
36 fprintf(stderr,"%s: ",progname);
38 fprintf(stderr,"file %s",curfile);
40 fprintf(stderr," (%.14s)",archhdr.ar_name);
43 if ( pstate.s_curpro ) {
44 fprintf(stderr,"proc %s, ",pstate.s_curpro->p_name);
46 fprintf(stderr,"line %d: ",line_num);
47 fprintf(stderr,string1,a1,a2,a3,a4);
52 void error(string1,a1,a2,a3,a4) char *string1 ; {
53 pr_error(string1,a1,a2,a3,a4) ;
58 void werror(string1,a1,a2,a3,a4) char *string1 ; {
60 pr_error(string1,a1,a2,a3,a4) ;
67 error("Fatal error: %s",s);
73 FILE *frewind(f) FILE *f ; {
74 /* Rewind a file open for writing and open it for reading */
75 /* Assumption, file descriptor is r/w */
77 tmp=fdopen(dup(fileno(f)),"r");
84 int xgetc(af) register FILE *af; {
87 * read next character; fatal if there isn't one
91 fatal("unexpected end of file");
95 xputc(c,af) register FILE *af; {
96 /* output one character and scream if it gives an error */
98 if ( ferror(af) ) fatal("write error") ;
102 putblk(stream,from,amount)
103 register FILE *stream; register char *from ; register int amount ; {
105 for ( ; amount-- ; from++ ) {
106 fputc(*from,stream) ;
107 if ( ferror(stream) ) fatal("write error") ;
111 int getblk(stream,from,amount)
112 register FILE *stream; register char *from ; register int amount ; {
114 for ( ; amount-- ; from++ ) {
115 *from = fgetc(stream) ;
116 if ( feof(stream) ) return 1 ;
121 xput16(w,f) FILE *f; {
129 xputarb(l,w,f) int l ; cons_t w ; FILE *f ; {
131 xputc( int_cast w,f) ;
143 * note reversed order of bytes.
144 * this is done for faster interpretation.
151 put32(n) cons_t n ; {
152 put16( int_cast (n>>16)) ;
156 put64(n) cons_t n ; {
157 fatal("put64 called") ;
162 * Read one byte from ifile.
164 if (libeof && inpoff >= libeof)
167 return fgetc(ifile) ;
173 * Read one byte from ifile.
178 fatal("Tried to read past end of arentry\n");
180 fatal("end of file on input");
185 cons_t xgetarb(l,f) int l; FILE *f ; {
192 val += ((cons_t)(c = ctrunc(xgetc(f))))<<shift ;
195 if (c == 0377 && shift > 8 && ((shift>>3)&1)) {
196 while (shift < 8*sizeof(cons_t)) {
197 val += ((cons_t)c)<<shift ;
206 * Handle one byte of data.
212 extword(w) cons_t w ; {
213 /* Assemble the word constant w.
214 * NOTE: The bytes are written low to high.
217 for ( i=wordsize ; i-- ; ) {
223 extarb(size,value) int size ; long value ; {
224 /* Assemble the 'size' constant value.
225 * The bytes are again written low to high.
228 for ( i=size ; i-- ; ) {
229 ext8( int_cast value ) ;
234 extadr(a) cons_t a ; {
235 /* Assemble the word constant a.
236 * NOTE: The bytes are written low to high.
239 for ( i=ptrsize ; i-- ; ) {
245 xputa(a,f) cons_t a ; FILE *f ; {
246 /* Assemble the pointer constant a.
247 * NOTE: The bytes are written low to high.
250 for ( i=ptrsize ; i-- ; ) {
251 xputc( int_cast a,f) ;
256 cons_t xgeta(f) FILE *f ; {
257 /* Read the pointer constant a.
258 * NOTE: The bytes were written low to high.
263 for ( i=ptrsize ; i-- ; ) {
264 val += ((cons_t)xgetc(f))<<shift ;
272 amount=(dataoff-lastoff)/size ;
273 if ( amount>MAXBYTE) fatal("Descriptor overflow");
279 if (datamode==mode) { /* in right mode already */
280 switch ( datamode ) {
282 if ( (dataoff-lastoff)/wordsize < MAXBYTE ) return ;
285 if ( dataoff-lastoff < MAXBYTE ) return ;
289 if ( (dataoff-lastoff)/ptrsize < MAXBYTE ) return ;
299 setmode(DATA_NUL) ; /* flush current descriptor */
303 switch(datamode) { /* terminate current mode */
305 break; /* nothing to terminate */
307 lastheader->r_val.rel_i=icount(wordsize) ;
308 lastheader->r_typ = RELHEAD;
312 lastheader->r_val.rel_i=icount(1) ;
313 lastheader->r_typ = RELHEAD;
318 lastheader->r_val.rel_i=icount(ptrsize) ;
319 lastheader->r_typ = RELHEAD;
332 lastheader=data_reloc( chp_cast 0,dataoff,RELNULL);
338 lastheader=data_reloc( chp_cast 0,dataoff,RELNULL);
344 lastheader=data_reloc( chp_cast 0,dataoff,RELNULL);
350 lastheader=data_reloc( chp_cast 0,dataoff,RELNULL);
356 ext8( int_cast consiz) ;
360 ext8( int_cast consiz) ;
364 ext8( int_cast consiz) ;
373 fatal("Unknown mode in setmode") ;
379 register char *fname, *cpname ;
380 static char sfname[] = "tmp.00000";
382 static char name[80] = TMP_DIR ;
385 * This procedure returns a file-descriptor of a temporary
386 * file valid for reading and writing.
387 * After closing the tmpfil-descriptor the file is lost
388 * Calling this routine frees the program from generating uniqe names.
394 *fname++ = (pid&07) + '0';
398 for ( fname=name ; *fname ; fname++ ) ;
400 while ( *fname++ = *cpname++ ) ;
403 if ((fildes = creat(fname, 0600)) < 0)
404 if ((fildes = creat(fname=sfname, 0600)) < 0)
406 if (close(fildes) < 0)
408 } while((fildes = open(fname, 2)) < 0 && count--);
409 if (unlink(fname) < 0)