2 #include <inc/string.h>
10 // Unlike standard Unix's putchar,
11 // the cputchar function _always_ outputs to the system console.
21 // JOS does, however, support standard _input_ redirection,
22 // allowing the user to redirect script files to the shell and such.
23 // getchar() reads a character from file descriptor 0.
33 // "Real" console file descriptor implementation.
34 // The putchar/getchar functions above will still come here by default,
35 // but now can be redirected to files, pipes, etc., via the fd layer.
37 static ssize_t
devcons_read(struct Fd
*, void*, size_t);
38 static ssize_t
devcons_write(struct Fd
*, const void*, size_t);
39 static int devcons_close(struct Fd
*);
40 static int devcons_stat(struct Fd
*, struct Stat
*);
46 .dev_read
= devcons_read
,
47 .dev_write
= devcons_write
,
48 .dev_close
= devcons_close
,
49 .dev_stat
= devcons_stat
58 if ((r
= fd_lookup(fdnum
, &fd
)) < 0)
60 return fd
->fd_dev_id
== devcons
.dev_id
;
69 if ((r
= fd_alloc(&fd
)) < 0)
71 if ((r
= sys_page_alloc(0, fd
, PTE_P
|PTE_U
|PTE_W
|PTE_SHARE
)) < 0)
73 fd
->fd_dev_id
= devcons
.dev_id
;
74 fd
->fd_omode
= O_RDWR
;
79 devcons_read(struct Fd
*fd
, void *vbuf
, size_t n
)
86 while ((c
= sys_cgetc()) == 0)
90 if (c
== 0x04) // ctl-d is eof
97 devcons_write(struct Fd
*fd
, const void *vbuf
, size_t n
)
102 // mistake: have to nul-terminate arg to sys_cputs,
103 // so we have to copy vbuf into buf in chunks and nul-terminate.
104 for (tot
= 0; tot
< n
; tot
+= m
) {
106 if (m
> sizeof(buf
) - 1)
108 memmove(buf
, (char*)vbuf
+ tot
, m
);
115 devcons_close(struct Fd
*fd
)
123 devcons_stat(struct Fd
*fd
, struct Stat
*stat
)
125 strcpy(stat
->st_name
, "<cons>");