~kris/9p

9hist

ref: fd71a85ff948f7c3058aaa8e67195015d11709ce 9hist/port/devdup.c -rw-r--r-- 1.8 KiB
fd71a85f — David du Colombier Plan 9 from Bell Labs 1991-04-25 35 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"

#include	"devtab.h"


int
dupgen(Chan *c, Dirtab *tab, int ntab, int s, Dir *dp)
{
	char buf[8];
	Chan *f;
	static int perm[] = { 0400, 0200, 0600, 0 };

	if(s >= NFD)
		return -1;
	if((f=u->fd[s]) == 0)
		return 0;
	sprint(buf, "%ld", s);
	devdir(c, (Qid){s, 0}, buf, 0, perm[f->mode&3], dp);
	return 1;
}

void
dupinit(void)
{
}

void
dupreset(void)
{
}

Chan *
dupattach(char *spec)
{
	return devattach('d', spec);
}

Chan *
dupclone(Chan *c, Chan *nc)
{
	return devclone(c, nc);
}

Chan*
dupclwalk(Chan *c, char *name)
{
	return devclwalk(c, name);
}

int
dupwalk(Chan *c, char *name)
{
	return devwalk(c, name, (Dirtab *)0, 0, dupgen);
}

void
dupstat(Chan *c, char *db)
{
	int fd;
	Chan *f;

	if(c->qid.path == CHDIR){
		devstat(c, db, (Dirtab *)0, 0L, dupgen);
	}else{
		fd = c->qid.path;
		SET(f);
		if(fd<0 || NFD<=fd || (f=u->fd[fd])==0)
			error(Ebadfd);
		(*devtab[f->type].stat)(f, (char*)db);
	}
}

Chan *
dupopen(Chan *c, int omode)
{
	Chan *f;

	if(c->qid.path == CHDIR){
		if(omode != 0)
			error(Eisdir);
		c->mode = 0;
		c->flag |= COPEN;
		c->offset = 0;
		return c;
	}
	fdtochan(c->qid.path, openmode(omode));	/* error check only */
	f = u->fd[c->qid.path];
	close(c);
	incref(f);
	if(omode & OCEXEC)
		f->flag |= CCEXEC;
	return f;
}

void
dupcreate(Chan *c, char *name, int omode, ulong perm)
{
	error(Eperm);
}

void
dupremove(Chan *c)
{
	error(Eperm);
}

void
dupwstat(Chan *c, char *dp)
{
	error(Egreg);
}

void
dupclose(Chan *c)
{
}

long
dupread(Chan *c, void *va, long n, ulong offset)
{
	char *a = va;

	if(c->qid.path != CHDIR)
		panic("dupread");
	return devdirread(c, a, n, (Dirtab *)0, 0L, dupgen);
}

long
dupwrite(Chan *c, void *va, long n, ulong offset)
{
	panic("dupwrite");
}