xref: /plan9-contrib/sys/src/cmd/unix/drawterm/kern/devlfd.c (revision 781103c4074deb8af160e8a0da2742ba6b29dc2b)
1 #include	"u.h"
2 #include <errno.h>
3 #include	"lib.h"
4 #include	"dat.h"
5 #include	"fns.h"
6 #include	"error.h"
7 
8 #undef pread
9 #undef pwrite
10 
11 Chan*
12 lfdchan(int fd)
13 {
14 	Chan *c;
15 
16 	c = newchan();
17 	c->type = devno('L', 0);
18 	c->aux = (void*)(uintptr)fd;
19 	c->name = newcname("fd");
20 	c->mode = ORDWR;
21 	c->qid.type = 0;
22 	c->qid.path = 0;
23 	c->qid.vers = 0;
24 	c->dev = 0;
25 	c->offset = 0;
26 	return c;
27 }
28 
29 int
30 lfdfd(int fd)
31 {
32 	return newfd(lfdchan(fd));
33 }
34 
35 static Chan*
36 lfdattach(char *x)
37 {
38 	USED(x);
39 
40 	error(Egreg);
41 	return nil;
42 }
43 
44 static Walkqid*
45 lfdwalk(Chan *c, Chan *nc, char **name, int nname)
46 {
47 	USED(c);
48 	USED(nc);
49 	USED(name);
50 	USED(nname);
51 
52 	error(Egreg);
53 	return nil;
54 }
55 
56 static int
57 lfdstat(Chan *c, uchar *dp, int n)
58 {
59 	USED(c);
60 	USED(dp);
61 	USED(n);
62 	error(Egreg);
63 	return -1;
64 }
65 
66 static Chan*
67 lfdopen(Chan *c, int omode)
68 {
69 	USED(c);
70 	USED(omode);
71 
72 	error(Egreg);
73 	return nil;
74 }
75 
76 static void
77 lfdclose(Chan *c)
78 {
79 	close((int)(uintptr)c->aux);
80 }
81 
82 static long
83 lfdread(Chan *c, void *buf, long n, vlong off)
84 {
85 	USED(off);	/* can't pread on pipes */
86 	n = read((int)(uintptr)c->aux, buf, n);
87 	if(n < 0){
88 		iprint("error %d\n", errno);
89 		oserror();
90 	}
91 	return n;
92 }
93 
94 static long
95 lfdwrite(Chan *c, void *buf, long n, vlong off)
96 {
97 	USED(off);	/* can't pread on pipes */
98 
99 	n = write((int)(uintptr)c->aux, buf, n);
100 	if(n < 0){
101 		iprint("error %d\n", errno);
102 		oserror();
103 	}
104 	return n;
105 }
106 
107 Dev lfddevtab = {
108 	'L',
109 	"lfd",
110 
111 	devreset,
112 	devinit,
113 	devshutdown,
114 	lfdattach,
115 	lfdwalk,
116 	lfdstat,
117 	lfdopen,
118 	devcreate,
119 	lfdclose,
120 	lfdread,
121 	devbread,
122 	lfdwrite,
123 	devbwrite,
124 	devremove,
125 	devwstat,
126 };
127