Skip to content

Commit ad70ca3

Browse files
chenzhijiaGUIDINGLI
authored andcommitted
libs/libc/unistd: add getpass function
Add getpass function to pass tlpi example: https://man7.org/tlpi/code/online/dist/users_groups/check_password.c.html Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
1 parent c0039d6 commit ad70ca3

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

include/unistd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ int syncfs(int fd);
469469
int profil(FAR unsigned short *buf, size_t bufsiz,
470470
size_t offset, unsigned int scale);
471471

472+
FAR char *getpass(FAR const char *prompt);
473+
472474
#if CONFIG_FORTIFY_SOURCE > 0
473475
fortify_function(getcwd) FAR char *getcwd(FAR char *buf,
474476
size_t size)

libs/libc/libc.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"getoptindp","unistd.h","","FAR int *"
106106
"getoptoptp","unistd.h","","FAR int *"
107107
"getpriority","sys/resource.h","","int","int","id_t"
108+
"getpass","unistd.h","","FAR const char *"
108109
"getpwnam_r","pwd.h","","int","FAR const char *","FAR struct passwd *","FAR char *","size_t","FAR struct passwd **"
109110
"getpwuid_r","pwd.h","","int","uid_t","FAR struct passwd *","FAR char *","size_t","FAR struct passwd **"
110111
"getrandom","sys/random.h","!defined(CONFIG_BUILD_KERNEL)","ssize_t","FAR void *","size_t","unsigned int"

libs/libc/unistd/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ set(SRCS
6464
lib_getpgrp.c
6565
lib_getpgid.c
6666
lib_lockf.c
67-
lib_flock.c)
67+
lib_flock.c
68+
lib_getpass.c)
6869

6970
if(NOT CONFIG_SCHED_USER_IDENTITY)
7071
list(

libs/libc/unistd/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CSRCS += lib_setrlimit.c lib_getrlimit.c lib_setpriority.c lib_getpriority.c
3232
CSRCS += lib_futimes.c lib_lutimes.c lib_gethostname.c lib_sethostname.c
3333
CSRCS += lib_fchownat.c lib_linkat.c lib_readlinkat.c lib_symlinkat.c
3434
CSRCS += lib_unlinkat.c lib_usleep.c lib_getpgrp.c lib_getpgid.c
35-
CSRCS += lib_lockf.c lib_flock.c
35+
CSRCS += lib_lockf.c lib_flock.c lib_getpass.c
3636

3737
ifneq ($(CONFIG_SCHED_USER_IDENTITY),y)
3838
CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c

libs/libc/unistd/lib_getpass.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)