src/prompt.c: kill one whitespace from timeout message
[vlock.git] / src / prompt.c
blobad331f16a0203f634a6a41290efd8511ce60ecc6
1 /* prompt.c -- prompt routines for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
10 * the author.
13 * The prompt functions (prompt and prompt_echo_off) were
14 * inspired by/copied from openpam's openpam_ttyconv.c:
16 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote
27 * products derived from this software without specific prior written
28 * permission.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <termios.h>
48 #include <unistd.h>
49 #include <sys/select.h>
50 #include <errno.h>
52 #include "vlock.h"
54 #define PROMPT_BUFFER_SIZE 512
56 char *prompt(const char *msg, const struct timespec *timeout) {
57 char buffer[PROMPT_BUFFER_SIZE];
58 char *result;
59 int len;
60 struct termios term;
61 struct timeval timeout_val;
62 struct timeval *timeout_val_p = NULL;
63 tcflag_t lflag;
64 fd_set readfds;
66 if (timeout != NULL) {
67 timeout_val.tv_sec = timeout->tv_sec;
68 timeout_val.tv_usec = timeout->tv_nsec / 1000;
69 timeout_val_p = &timeout_val;
72 if (msg != NULL) {
73 /* Write out the prompt. */
74 (void) fputs(msg, stderr); fflush(stderr);
77 /* Get the current terminal attributes. */
78 (void) tcgetattr(STDIN_FILENO, &term);
79 /* Save the lflag value. */
80 lflag = term.c_lflag;
81 /* Enable canonical mode. We're only interested in line buffering. */
82 term.c_lflag |= ICANON;
83 /* Disable terminal signals. */
84 term.c_lflag &= ~ISIG;
85 /* Set the terminal attributes. */
86 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
87 /* Discard all unread input characters. */
88 (void) tcflush(STDIN_FILENO, TCIFLUSH);
90 /* Initialize file descriptor set. */
91 FD_ZERO(&readfds);
92 FD_SET(STDIN_FILENO, &readfds);
94 errno = 0;
96 /* Wait until a string was entered. */
97 if (select(STDIN_FILENO+1, &readfds, NULL, NULL, timeout_val_p) != 1) {
98 if (errno)
99 perror("vlock-auth: select() on stdin failed");
100 else
101 fprintf(stderr, "timeout!\n");
103 result = NULL;
104 goto out;
107 /* Read the string from stdin. At most buffer length - 1 bytes, to
108 * leave room for the terminating zero byte. */
109 if ((len = read(STDIN_FILENO, buffer, sizeof buffer - 1)) < 0) {
110 result = NULL;
111 goto out;
114 /* Terminate the string. */
115 buffer[len] = '\0';
117 /* Strip trailing newline characters. */
118 for (len = strlen(buffer); len > 0; --len) {
119 if (buffer[len-1] != '\r' && buffer[len-1] != '\n')
120 break;
123 /* Terminate the string, again. */
124 buffer[len] = '\0';
126 /* Copy the string. */
127 result = strdup(buffer);
129 /* Clear our buffer. */
130 memset(buffer, 0, sizeof buffer);
132 out:
133 /* Restore original terminal attributes. */
134 term.c_lflag = lflag;
135 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
137 return result;
140 char *prompt_echo_off(const char *msg, const struct timespec *timeout) {
141 struct termios term;
142 tcflag_t lflag;
143 char *result;
145 (void) tcgetattr(STDIN_FILENO, &term);
146 lflag = term.c_lflag;
147 term.c_lflag &= ~ECHO;
148 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
150 result = prompt(msg, timeout);
152 term.c_lflag = lflag;
153 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
155 if (result != NULL)
156 fputc('\n', stderr);
158 return result;