2 * Copyright 2005-2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Ingo Weinhold <bonefish@users.sourceforge.net>
8 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
9 * Distributed under the terms of the NewOS License.
16 #include <KernelExport.h>
22 #define DEVICE_NAME "dprintf"
24 int32 api_version
= B_CUR_DRIVER_API_VERSION
;
28 dprintf_open(const char *name
, uint32 flags
, void **cookie
)
36 dprintf_close(void *cookie
)
43 dprintf_freecookie(void *cookie
)
50 dprintf_ioctl(void *cookie
, uint32 op
, void *buffer
, size_t length
)
53 // let isatty() think we are a terminal
54 // (this lets libroot use unbuffered I/O)
63 dprintf_read(void *cookie
, off_t pos
, void *buffer
, size_t *length
)
71 dprintf_write(void *cookie
, off_t pos
, const void *buffer
, size_t *_length
)
73 const char *str
= (const char*)buffer
;
75 int bytesLeft
= *_length
;
76 while (bytesLeft
> 0) {
77 int chunkSize
= strnlen(str
, bytesLeft
);
85 if (chunkSize
== bytesLeft
) {
86 // no null-byte in the remainder of the buffer
87 // we need to copy to a local buffer and null-terminate
88 while (bytesLeft
> 0) {
89 chunkSize
= bytesLeft
;
91 char localBuffer
[512];
92 if (bytesLeft
> (int)sizeof(localBuffer
) - 1)
93 chunkSize
= (int)sizeof(localBuffer
) - 1;
94 memcpy(localBuffer
, str
, chunkSize
);
95 localBuffer
[chunkSize
] = '\0';
97 debug_puts(localBuffer
, chunkSize
);
100 bytesLeft
-= chunkSize
;
103 // null-terminated chunk -- just write it
104 debug_puts(str
, chunkSize
);
106 str
+= chunkSize
+ 1;
107 bytesLeft
-= chunkSize
+ 1;
126 publish_devices(void)
128 static const char *devices
[] = {
138 find_device(const char *name
)
140 static device_hooks hooks
= {
147 /* Leave select/deselect/readv/writev undefined. The kernel will
148 * use its own default implementation. The basic hooks above this
149 * line MUST be defined, however. */
156 if (!strcmp(name
, DEVICE_NAME
))