#

/*
 * Print working (current) directory
 */
/*
 * Inode structure as returned from stat
 */
struct	statb
{
	int	i_dev;
	int	i_ino;
	int	i_mode;
	char	i_nlink;
	char	i_uid;
	char	i_gid;
	char	i_size0;
	char	*i_size1;
	int	i_addr[8];
	int	i_atime[2];
	int	i_mtime[2];
};

/* modes */
#define	IALLOC	0100000
#define	IFMT	060000
#define		IFDIR	040000
#define		IFCHR	020000
#define		IFBLK	060000
#define	ILARG	010000
#define	ISUID	04000
#define	ISGID	02000
#define	ISVTX	01000
#define	IREAD	0400
#define	IWRITE	0200
#define	IEXEC	0100

/*
 * Structure of a directory
 */

struct	dir {
	int	d_ino;
	char	d_name[14];
};

char	dot[]	".";
char	dotdot[]	"..";
char	mtabf[]	"/etc/mtab";
char	name[512];
int	file;
int	off	-1;
struct	statb	x;
struct	dir	y;

#define	NAMSIZ	32

struct mtab {
	char	file[NAMSIZ];
	char	spec[NAMSIZ-2];
	char	unused;
	char	uid;
} mtab;

main()
{
	register n;

	for (;;) {
		stat(dot, &x);
		if ((file = open(dotdot,0)) < 0) prname();
		do {
			if (read(file, &y, sizeof(y)) < sizeof(y))
				prname();
		} while (y.d_ino != x.i_ino);
		close(file);
		if (y.d_ino == 1)
			ckmount();
		cat(y.d_name);
		chdir(dotdot);
	}
}

ckmount()
{
	register i;

	if (stat(y.d_name,&x)<0 || chdir("/")<0 || (file=open(mtabf,0))<0)
		prname();
	i = x.i_dev;
	mtab.file[0] = '/';
	do {
		if (mtab.file[0] == 0)
			continue;
		if (stat(mtab.file,&x) < 0)
			break;
		if (x.i_dev==i && (x.i_mode&IFMT)==IFDIR) {
			if (mtab.file[0] == '/' && mtab.file[1] == 0)
				cat("");
			else
				cat(mtab.file);
			break;
		}
	} while (read(file,&mtab,sizeof(mtab)) == sizeof(mtab));
	prname();
}

prname()
{
	if (off<0)
		off = 0;
	else if(off==0) off++;
	name[off] = '\n';
	write(1, name, off+1);
	exit(0);
}

cat(str)
char *str;
{
	register i, j;

	i = -1;
	while (str[++i] != 0);
	if ((off+i+2) > 511)
		prname();
	for(j=off+1; j>=0; --j)
		name[j+i+1] = name[j];
	off=i+off+1;
	name[i] = '/';
	for(--i; i>=0; --i)
		name[i] = str[i];
}
