#ident "$Header: file.c 1.7 89/08/07 $"

/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*
 * $Log:	file.c,v $
 */

#include "crash.h"
#include <sys/types.h>
#define INKERNEL
#include <sys/file.h>
#include <sys/var.h>
#undef INKERNEL

struct file *file;
struct file *readfile();
int nfile;

file_init()
{
	struct var v;

	readv(search("file"), &file, sizeof file);
	readv(search("v"), &v, sizeof v);
	nfile = v.v_file;

}

File()
{

	struct file *ff, *f = file;
	register int j;
	char *arg;
	int count, slt;

	if (live)
		file_init();

	printf("SLT REF VNODE     FLAGS\n");
						
	if ((arg = token()) == NULL) {		/* print the works */
		for (j = nfile; j > 0; f++, j--) {
			if ((ff = readfile(f)) == NULL)
				continue;
			if (ff->f_count == 0)
				continue;
			printf("%-3d", nfile - j);
			prfile(ff);
		}
	 } else {				/* selective print */
		slt = atoi(arg);
		f += slt;
		count = atoi(token());
		if (!count) count = 1;
		count = ((count + slt) > nfile) ? (nfile - slt) : count;
		for (j = count; j > 0; f++, j--, slt++) {
			if ((ff = readfile(f)) == NULL)
				continue;
			if (ff->f_count == NULL)
				continue;
			printf("%-3d", slt);
			prfile(ff);
		}
	}	

}

prfile(f)
	register struct file *f;
{

	register char	ch;

	printf(" %-2d %#6x  ", f->f_count, f->f_vnode);

	printf("%s%s%s%s%s%s%s%s%s%s%s\n",
	f->f_flag & FOPEN   ? " opn" : "",
	f->f_flag & FREAD   ? " rd" : "",
	f->f_flag & FWRITE  ? " wrt" : "",
	f->f_flag & FNDELAY ? " dly" : "",
	f->f_flag & FAPPEND ? " app": "",
	f->f_flag & FSYNC   ? " syn": "",
	f->f_flag & FRCACH  ? " ch": "",
	f->f_flag & FNET    ? " net": "",
	f->f_flag & FCREAT  ? " crt": "",
	f->f_flag & FTRUNC  ? " trn": "",
	f->f_flag & FEXCL   ? " exc": "");

	return;

}

struct file *
readfile(f)
	struct file *f;
{

	static struct file filebuf;

	if (readv(f, &filebuf, sizeof filebuf) != sizeof filebuf) {
		printf("read error on file\n");
		return NULL;
	}
	return &filebuf;

}

