* Implement a different way to delete a password from the cache.
[alpine.git] / pico / osdep / read.c
blobae09d5a7c1d9c0e0841f39d043676ee5cece24f2
1 /*
2 * ========================================================================
3 * Copyright 2006 University of Washington
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Keyboard input test and read functions
18 #include <system.h> /* os-dep defs/includes */
19 #include <general.h> /* generally useful definitions */
21 #include "../keydefs.h"
23 #ifndef _WINDOWS
24 #include "raw.h"
25 #endif /* !_WINDOWS */
27 #include "read.h"
30 static time_t _time_of_last_input;
32 time_t
33 time_of_last_input(void)
35 if(_time_of_last_input == 0)
36 _time_of_last_input = time((time_t *) 0);
38 return(_time_of_last_input);
41 #ifndef _WINDOWS
42 #if HAVE_SELECT
44 #ifdef HAVE_SYS_SELECT_H
45 # include <sys/select.h>
46 #endif
50 * Check whether or not a character is ready to be read, or time out.
51 * This version uses a select call.
53 * Args: time_out -- number of seconds before it will time out.
55 * Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
56 * NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
57 * READ_INTR: read was interrupted
58 * READY_TO_READ: input is available
59 * BAIL_OUT: reading input failed, time to bail gracefully
60 * PANIC_NOW: system call error, die now
62 UCS
63 input_ready(int time_out)
65 struct timeval tmo;
66 fd_set readfds, errfds;
67 int res;
69 fflush(stdout);
71 if(time_out > 0){
72 /* Check to see if there are bytes to read with a timeout */
73 FD_ZERO(&readfds);
74 FD_ZERO(&errfds);
75 FD_SET(STDIN_FD, &readfds);
76 FD_SET(STDIN_FD, &errfds);
77 tmo.tv_sec = time_out;
78 tmo.tv_usec = 0;
79 res = select(STDIN_FD+1, &readfds, 0, &errfds, &tmo);
80 if(res < 0){
81 if(errno == EINTR || errno == EAGAIN)
82 return(READ_INTR);
84 return(BAIL_OUT);
87 if(res == 0){ /* the select timed out */
88 #ifndef __CYGWIN__
89 if(getppid() == 1){
90 /* Parent is init! */
91 return(BAIL_OUT);
93 #endif
96 * "15" is the minimum allowed mail check interval.
97 * Anything less, and we're being told to cycle thru
98 * the command loop because some task is pending...
100 return(time_out < IDLE_TIMEOUT ? NO_OP_COMMAND : NO_OP_IDLE);
104 _time_of_last_input = time((time_t *) 0);
105 return(READY_TO_READ);
109 #elif HAVE_POLL
112 #ifdef HAVE_STROPTS_H
113 # include <stropts.h>
114 #endif
116 #ifdef HAVE_SYS_POLL_H
117 # include <poll.h>
118 #endif
121 * Check whether or not a character is ready to be read, or time out.
122 * This version uses a poll call.
124 * Args: time_out -- number of seconds before it will time out.
126 * Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
127 * NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
128 * READ_INTR: read was interrupted
129 * READY_TO_READ: input is available
130 * BAIL_OUT: reading input failed, time to bail gracefully
131 * PANIC_NOW: system call error, die now
134 input_ready(int time_out)
136 struct pollfd pollfd;
137 int res;
139 fflush(stdout);
141 if(time_out > 0){
142 /* Check to see if there are bytes to read with a timeout */
143 pollfd.fd = STDIN_FD;
144 pollfd.events = POLLIN;
145 res = poll (&pollfd, 1, time_out * 1000);
146 if(res >= 0){ /* status bits OK? */
147 if(pollfd.revents & (POLLERR | POLLNVAL))
148 res = -1; /* bad news, exit below! */
149 else if(pollfd.revents & POLLHUP)
150 return(BAIL_OUT);
153 if(res < 0){
154 if(errno == EINTR || errno == EAGAIN)
155 return(READ_INTR);
157 return(PANIC_NOW);
160 if(res == 0){ /* the select timed out */
161 if(getppid() == 1){
162 /* Parent is init! */
163 return(BAIL_OUT);
167 * "15" is the minimum allowed mail check interval.
168 * Anything less, and we're being told to cycle thru
169 * the command loop because some task is pending...
171 return(time_out < IDLE_TIMEOUT ? NO_OP_COMMAND : NO_OP_IDLE);
175 _time_of_last_input = time((time_t *) 0);
176 return(READY_TO_READ);
179 #endif /* HAVE_POLL */
183 * Read one character from STDIN.
185 * Result: -- the single character read
186 * READ_INTR -- read was interrupted
187 * BAIL_OUT -- read error of some sort
190 read_one_char(void)
192 int res;
193 unsigned char c;
195 res = read(STDIN_FD, &c, 1);
197 if(res <= 0){
199 * Error reading from terminal!
200 * The only acceptable failure is being interrupted. If so,
201 * return a value indicating such...
203 if(res < 0 && errno == EINTR)
204 return(READ_INTR);
205 else
206 return(BAIL_OUT);
209 return((int)c);
212 #else /* _WINDOWS */
214 set_time_of_last_input(void)
216 _time_of_last_input = time(0L);
217 return(0);
219 #endif /* _WINDOWS */