|
| 1 | +/**************************************************************************** |
| 2 | + * libs/libc/unistd/lib_getpass.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | +#include <termios.h> |
| 27 | +#include <unistd.h> |
| 28 | +#include <fcntl.h> |
| 29 | + |
| 30 | +/**************************************************************************** |
| 31 | + * Private Data |
| 32 | + ****************************************************************************/ |
| 33 | + |
| 34 | +static char g_password[128]; |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Public Functions |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +FAR char *getpass(FAR const char *prompt) |
| 41 | +{ |
| 42 | + struct termios s; |
| 43 | + struct termios t; |
| 44 | + ssize_t total_bytes_read = 0; |
| 45 | + ssize_t bytes_read; |
| 46 | + int fd; |
| 47 | + |
| 48 | + if ((fd = open("/dev/console", O_RDONLY | O_NOCTTY)) < 0) |
| 49 | + { |
| 50 | + fd = STDIN_FILENO; |
| 51 | + } |
| 52 | + |
| 53 | + tcgetattr(fd, &t); |
| 54 | + s = t; |
| 55 | + t.c_lflag &= ~(ECHO | ISIG); |
| 56 | + t.c_lflag |= ICANON; |
| 57 | + t.c_iflag &= ~(INLCR | IGNCR); |
| 58 | + t.c_iflag |= ICRNL; |
| 59 | + tcsetattr(fd, TCSAFLUSH, &t); |
| 60 | + tcdrain(fd); |
| 61 | + |
| 62 | + if (write(STDERR_FILENO, prompt, strlen(prompt)) != strlen(prompt)) |
| 63 | + { |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + while ((bytes_read = read(fd, g_password + total_bytes_read, |
| 68 | + sizeof(g_password) - total_bytes_read)) > 0) |
| 69 | + { |
| 70 | + if (bytes_read > 0 && g_password[total_bytes_read] == '\n') |
| 71 | + { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + total_bytes_read += bytes_read; |
| 76 | + } |
| 77 | + |
| 78 | + if (total_bytes_read >= 0) |
| 79 | + { |
| 80 | + if (total_bytes_read > 0 && g_password[total_bytes_read - 1] == '\n') |
| 81 | + { |
| 82 | + total_bytes_read--; |
| 83 | + } |
| 84 | + |
| 85 | + g_password[total_bytes_read] = 0; |
| 86 | + } |
| 87 | + |
| 88 | + tcsetattr(fd, TCSAFLUSH, &s); |
| 89 | + |
| 90 | + if (fd > STDERR_FILENO) |
| 91 | + { |
| 92 | + close(fd); |
| 93 | + } |
| 94 | + |
| 95 | + return g_password; |
| 96 | +} |
0 commit comments