vfs: check userland buffers before reading them.
[haiku.git] / src / libs / bsd / getpass.c
blob45676c0b3ebf6c272db26429521c0274fdfd9b3f
1 /*
2 * Copyright 2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <termios.h>
14 #include <unistd.h>
17 char *
18 getpass(const char *prompt)
20 static char password[128];
21 struct termios termios;
22 bool changed = false;
24 // Turn off echo
26 if (tcgetattr(fileno(stdin), &termios) == 0) {
27 struct termios noEchoTermios = termios;
29 noEchoTermios.c_lflag &= ~(ECHO | ISIG);
30 changed = tcsetattr(fileno(stdin), TCSAFLUSH, &noEchoTermios) == 0;
33 // Show prompt
34 fputs(prompt, stdout);
35 fflush(stdout);
37 // Read password
38 if (fgets(password, sizeof(password), stdin) != NULL) {
39 size_t length = strlen(password);
41 if (password[length - 1] == '\n')
42 password[length - 1] = '\0';
44 if (changed) {
45 // Manually move to the next line
46 putchar('\n');
50 // Restore termios setting
51 if (changed)
52 tcsetattr(fileno(stdin), TCSAFLUSH, &termios);
54 return password;