xref: /NextBSD/contrib/ntp/lib/isc/win32/keyboard.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*
2  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id: keyboard.c,v 1.7 2007/06/19 23:47:19 tbox Exp $ */
19 
20 #include <config.h>
21 
22 #include <sys/types.h>
23 
24 #include <windows.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 
32 #include <io.h>
33 
34 #include <isc/keyboard.h>
35 #include <isc/util.h>
36 
37 isc_result_t
isc_keyboard_open(isc_keyboard_t * keyboard)38 isc_keyboard_open(isc_keyboard_t *keyboard) {
39 	int fd;
40 
41 	REQUIRE(keyboard != NULL);
42 
43 	fd = _fileno(stdin);
44 	if (fd < 0)
45 		return (ISC_R_IOERROR);
46 
47 	keyboard->fd = fd;
48 
49 	keyboard->result = ISC_R_SUCCESS;
50 
51 	return (ISC_R_SUCCESS);
52 }
53 
54 isc_result_t
isc_keyboard_close(isc_keyboard_t * keyboard,unsigned int sleeptime)55 isc_keyboard_close(isc_keyboard_t *keyboard, unsigned int sleeptime) {
56 	REQUIRE(keyboard != NULL);
57 
58 	if (sleeptime > 0 && keyboard->result != ISC_R_CANCELED)
59 		(void)Sleep(sleeptime*1000);
60 
61 	keyboard->fd = -1;
62 
63 	return (ISC_R_SUCCESS);
64 }
65 
66 isc_result_t
isc_keyboard_getchar(isc_keyboard_t * keyboard,unsigned char * cp)67 isc_keyboard_getchar(isc_keyboard_t *keyboard, unsigned char *cp) {
68 	ssize_t cc;
69 	unsigned char c;
70 
71 	REQUIRE(keyboard != NULL);
72 	REQUIRE(cp != NULL);
73 
74 	cc = read(keyboard->fd, &c, 1);
75 	if (cc < 0) {
76 		keyboard->result = ISC_R_IOERROR;
77 		return (keyboard->result);
78 	}
79 
80 	*cp = c;
81 
82 	return (ISC_R_SUCCESS);
83 }
84 
85 isc_boolean_t
isc_keyboard_canceled(isc_keyboard_t * keyboard)86 isc_keyboard_canceled(isc_keyboard_t *keyboard) {
87 	return (ISC_TF(keyboard->result == ISC_R_CANCELED));
88 }
89 
90