/* * addfile(), nextfile(): maintain a circular buffer of file names */ #include "m.h" #define LIBRARY "/opt/m/lib/" #define MAXFILES 512 static char *files[MAXFILES]; static char **start = files; /* location to return for nextfile */ static char **stop = files; /* next free location for addfile() */ static int nfiles = 0; /* number of file names in buffer */ #define inc(p) if( ++p >= files + MAXFILES) p = files void addfile( char *name) { if( nfiles == MAXFILES) { yyerror("file list overflow in addfile()"); return; } if( (*stop = strdup(name)) == NULL) yyerror("strdup() failed in addfile()"); else { inc( stop); ++nfiles; } } FILE *nextfile( void) { FILE *f = NULL; char name[BUFSIZ]; while( nfiles > 0) { #ifdef DEBUG if( prog_debug) dprint("nextfile: %s\n", *start); #endif --nfiles; if( (f = fopen( *start, "r")) == NULL) { (void) strcat( strcpy( name, LIBRARY), *start); if( (f = fopen( name, "r")) == NULL) dprint("could not open %s or %s\n", *start, name); } free( *start); inc(start); if( f) break; } return (f == NULL) ? stdin : f; }